blob: 05a1ea998fd6dcfc7a3166b94b6e53f1ee7da993 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 * Authors: Christian König <christian.koenig@amd.com>
26 */
27
28#include <linux/firmware.h>
29#include <linux/module.h>
30#include <drm/drmP.h>
31#include <drm/drm.h>
32
33#include "amdgpu.h"
34#include "amdgpu_pm.h"
35#include "amdgpu_vce.h"
36#include "cikd.h"
37
38/* 1 second timeout */
Christian König182830a2016-07-01 17:43:57 +020039#define VCE_IDLE_TIMEOUT msecs_to_jiffies(1000)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040
41/* Firmware Names */
42#ifdef CONFIG_DRM_AMDGPU_CIK
43#define FIRMWARE_BONAIRE "radeon/bonaire_vce.bin"
Christian Königedf600d2016-05-03 15:54:54 +020044#define FIRMWARE_KABINI "radeon/kabini_vce.bin"
45#define FIRMWARE_KAVERI "radeon/kaveri_vce.bin"
46#define FIRMWARE_HAWAII "radeon/hawaii_vce.bin"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040047#define FIRMWARE_MULLINS "radeon/mullins_vce.bin"
48#endif
Jammy Zhouc65444f2015-05-13 22:49:04 +080049#define FIRMWARE_TONGA "amdgpu/tonga_vce.bin"
50#define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin"
Alex Deucher188a9bc2015-07-27 14:24:14 -040051#define FIRMWARE_FIJI "amdgpu/fiji_vce.bin"
Samuel Licfaba562015-10-08 16:27:55 -040052#define FIRMWARE_STONEY "amdgpu/stoney_vce.bin"
Flora Cui2cc0c0b2016-03-14 18:33:29 -040053#define FIRMWARE_POLARIS10 "amdgpu/polaris10_vce.bin"
54#define FIRMWARE_POLARIS11 "amdgpu/polaris11_vce.bin"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040055
56#ifdef CONFIG_DRM_AMDGPU_CIK
57MODULE_FIRMWARE(FIRMWARE_BONAIRE);
58MODULE_FIRMWARE(FIRMWARE_KABINI);
59MODULE_FIRMWARE(FIRMWARE_KAVERI);
60MODULE_FIRMWARE(FIRMWARE_HAWAII);
61MODULE_FIRMWARE(FIRMWARE_MULLINS);
62#endif
63MODULE_FIRMWARE(FIRMWARE_TONGA);
64MODULE_FIRMWARE(FIRMWARE_CARRIZO);
Alex Deucher188a9bc2015-07-27 14:24:14 -040065MODULE_FIRMWARE(FIRMWARE_FIJI);
Samuel Licfaba562015-10-08 16:27:55 -040066MODULE_FIRMWARE(FIRMWARE_STONEY);
Flora Cui2cc0c0b2016-03-14 18:33:29 -040067MODULE_FIRMWARE(FIRMWARE_POLARIS10);
68MODULE_FIRMWARE(FIRMWARE_POLARIS11);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069
70static void amdgpu_vce_idle_work_handler(struct work_struct *work);
71
72/**
73 * amdgpu_vce_init - allocate memory, load vce firmware
74 *
75 * @adev: amdgpu_device pointer
76 *
77 * First step to get VCE online, allocate memory and load the firmware
78 */
Leo Liue9822622015-05-06 14:31:27 -040079int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080{
Christian Königc5949892016-02-10 17:43:00 +010081 struct amdgpu_ring *ring;
82 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 const char *fw_name;
84 const struct common_firmware_header *hdr;
85 unsigned ucode_version, version_major, version_minor, binary_id;
86 int i, r;
87
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 switch (adev->asic_type) {
89#ifdef CONFIG_DRM_AMDGPU_CIK
90 case CHIP_BONAIRE:
91 fw_name = FIRMWARE_BONAIRE;
92 break;
93 case CHIP_KAVERI:
94 fw_name = FIRMWARE_KAVERI;
95 break;
96 case CHIP_KABINI:
97 fw_name = FIRMWARE_KABINI;
98 break;
99 case CHIP_HAWAII:
100 fw_name = FIRMWARE_HAWAII;
101 break;
102 case CHIP_MULLINS:
103 fw_name = FIRMWARE_MULLINS;
104 break;
105#endif
106 case CHIP_TONGA:
107 fw_name = FIRMWARE_TONGA;
108 break;
109 case CHIP_CARRIZO:
110 fw_name = FIRMWARE_CARRIZO;
111 break;
Alex Deucher188a9bc2015-07-27 14:24:14 -0400112 case CHIP_FIJI:
113 fw_name = FIRMWARE_FIJI;
114 break;
Samuel Licfaba562015-10-08 16:27:55 -0400115 case CHIP_STONEY:
116 fw_name = FIRMWARE_STONEY;
117 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400118 case CHIP_POLARIS10:
119 fw_name = FIRMWARE_POLARIS10;
Sonny Jiang1b4eeea2016-03-11 14:33:40 -0500120 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400121 case CHIP_POLARIS11:
122 fw_name = FIRMWARE_POLARIS11;
Sonny Jiang1b4eeea2016-03-11 14:33:40 -0500123 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124
125 default:
126 return -EINVAL;
127 }
128
129 r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
130 if (r) {
131 dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
132 fw_name);
133 return r;
134 }
135
136 r = amdgpu_ucode_validate(adev->vce.fw);
137 if (r) {
138 dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
139 fw_name);
140 release_firmware(adev->vce.fw);
141 adev->vce.fw = NULL;
142 return r;
143 }
144
145 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
146
147 ucode_version = le32_to_cpu(hdr->ucode_version);
148 version_major = (ucode_version >> 20) & 0xfff;
149 version_minor = (ucode_version >> 8) & 0xfff;
150 binary_id = ucode_version & 0xff;
151 DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
152 version_major, version_minor, binary_id);
153 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
154 (binary_id << 8));
155
156 /* allocate firmware, stack and heap BO */
157
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158 r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -0400159 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200160 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
161 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200162 NULL, NULL, &adev->vce.vcpu_bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163 if (r) {
164 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
165 return r;
166 }
167
168 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
169 if (r) {
170 amdgpu_bo_unref(&adev->vce.vcpu_bo);
171 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
172 return r;
173 }
174
175 r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
176 &adev->vce.gpu_addr);
177 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
178 if (r) {
179 amdgpu_bo_unref(&adev->vce.vcpu_bo);
180 dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
181 return r;
182 }
183
Christian Königc5949892016-02-10 17:43:00 +0100184
185 ring = &adev->vce.ring[0];
186 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
187 r = amd_sched_entity_init(&ring->sched, &adev->vce.entity,
188 rq, amdgpu_sched_jobs);
189 if (r != 0) {
190 DRM_ERROR("Failed setting up VCE run queue.\n");
191 return r;
192 }
193
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400194 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
195 atomic_set(&adev->vce.handles[i], 0);
196 adev->vce.filp[i] = NULL;
197 }
198
Christian Königebff4852016-07-20 16:53:36 +0200199 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
200 mutex_init(&adev->vce.idle_mutex);
201
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202 return 0;
203}
204
205/**
206 * amdgpu_vce_fini - free memory
207 *
208 * @adev: amdgpu_device pointer
209 *
210 * Last step on VCE teardown, free firmware memory
211 */
212int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
213{
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300214 unsigned i;
215
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216 if (adev->vce.vcpu_bo == NULL)
217 return 0;
218
Christian Königc5949892016-02-10 17:43:00 +0100219 amd_sched_entity_fini(&adev->vce.ring[0].sched, &adev->vce.entity);
220
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400221 amdgpu_bo_unref(&adev->vce.vcpu_bo);
222
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300223 for (i = 0; i < adev->vce.num_rings; i++)
224 amdgpu_ring_fini(&adev->vce.ring[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225
226 release_firmware(adev->vce.fw);
Christian Königebff4852016-07-20 16:53:36 +0200227 mutex_destroy(&adev->vce.idle_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228
229 return 0;
230}
231
232/**
233 * amdgpu_vce_suspend - unpin VCE fw memory
234 *
235 * @adev: amdgpu_device pointer
236 *
237 */
238int amdgpu_vce_suspend(struct amdgpu_device *adev)
239{
240 int i;
241
242 if (adev->vce.vcpu_bo == NULL)
243 return 0;
244
245 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
246 if (atomic_read(&adev->vce.handles[i]))
247 break;
248
249 if (i == AMDGPU_MAX_VCE_HANDLES)
250 return 0;
251
Rex Zhu85cc88f2016-04-12 19:25:52 +0800252 cancel_delayed_work_sync(&adev->vce.idle_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400253 /* TODO: suspending running encoding sessions isn't supported */
254 return -EINVAL;
255}
256
257/**
258 * amdgpu_vce_resume - pin VCE fw memory
259 *
260 * @adev: amdgpu_device pointer
261 *
262 */
263int amdgpu_vce_resume(struct amdgpu_device *adev)
264{
265 void *cpu_addr;
266 const struct common_firmware_header *hdr;
267 unsigned offset;
268 int r;
269
270 if (adev->vce.vcpu_bo == NULL)
271 return -EINVAL;
272
273 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
274 if (r) {
275 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
276 return r;
277 }
278
279 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
280 if (r) {
281 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
282 dev_err(adev->dev, "(%d) VCE map failed\n", r);
283 return r;
284 }
285
286 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
287 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
Christian König7b4d3e22016-08-23 11:18:59 +0200288 memcpy_toio(cpu_addr, adev->vce.fw->data + offset,
289 adev->vce.fw->size - offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290
291 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
292
293 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
294
295 return 0;
296}
297
298/**
299 * amdgpu_vce_idle_work_handler - power off VCE
300 *
301 * @work: pointer to work structure
302 *
303 * power of VCE when it's not used any more
304 */
305static void amdgpu_vce_idle_work_handler(struct work_struct *work)
306{
307 struct amdgpu_device *adev =
308 container_of(work, struct amdgpu_device, vce.idle_work.work);
Alex Deucher24c5fe52016-09-26 15:19:14 -0400309 unsigned i, count = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400310
Alex Deucher24c5fe52016-09-26 15:19:14 -0400311 for (i = 0; i < adev->vce.num_rings; i++)
312 count += amdgpu_fence_count_emitted(&adev->vce.ring[i]);
313
314 if (count == 0) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315 if (adev->pm.dpm_enabled) {
316 amdgpu_dpm_enable_vce(adev, false);
317 } else {
318 amdgpu_asic_set_vce_clocks(adev, 0, 0);
319 }
320 } else {
Christian König182830a2016-07-01 17:43:57 +0200321 schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400322 }
323}
324
325/**
Christian Königebff4852016-07-20 16:53:36 +0200326 * amdgpu_vce_ring_begin_use - power up VCE
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400327 *
Christian Königebff4852016-07-20 16:53:36 +0200328 * @ring: amdgpu ring
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400329 *
330 * Make sure VCE is powerd up when we want to use it
331 */
Christian Königebff4852016-07-20 16:53:36 +0200332void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400333{
Christian Königebff4852016-07-20 16:53:36 +0200334 struct amdgpu_device *adev = ring->adev;
335 bool set_clocks;
Christian König182830a2016-07-01 17:43:57 +0200336
Christian Königebff4852016-07-20 16:53:36 +0200337 mutex_lock(&adev->vce.idle_mutex);
338 set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
Christian König182830a2016-07-01 17:43:57 +0200339 if (set_clocks) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400340 if (adev->pm.dpm_enabled) {
341 amdgpu_dpm_enable_vce(adev, true);
342 } else {
343 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
344 }
345 }
Christian Königebff4852016-07-20 16:53:36 +0200346 mutex_unlock(&adev->vce.idle_mutex);
347}
348
349/**
350 * amdgpu_vce_ring_end_use - power VCE down
351 *
352 * @ring: amdgpu ring
353 *
354 * Schedule work to power VCE down again
355 */
356void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring)
357{
358 schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400359}
360
361/**
362 * amdgpu_vce_free_handles - free still open VCE handles
363 *
364 * @adev: amdgpu_device pointer
365 * @filp: drm file pointer
366 *
367 * Close all VCE handles still open by this file pointer
368 */
369void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
370{
371 struct amdgpu_ring *ring = &adev->vce.ring[0];
372 int i, r;
373 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
374 uint32_t handle = atomic_read(&adev->vce.handles[i]);
Christian König182830a2016-07-01 17:43:57 +0200375
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376 if (!handle || adev->vce.filp[i] != filp)
377 continue;
378
Christian König9f2ade32016-02-03 16:50:56 +0100379 r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380 if (r)
381 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
382
383 adev->vce.filp[i] = NULL;
384 atomic_set(&adev->vce.handles[i], 0);
385 }
386}
387
388/**
389 * amdgpu_vce_get_create_msg - generate a VCE create msg
390 *
391 * @adev: amdgpu_device pointer
392 * @ring: ring we should submit the msg to
393 * @handle: VCE session handle to use
394 * @fence: optional fence to return
395 *
396 * Open up a stream for HW test
397 */
398int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
Chunming Zhoued40bfb2015-08-03 13:28:16 +0800399 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400400{
401 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100402 struct amdgpu_job *job;
403 struct amdgpu_ib *ib;
Chunming Zhou17635522015-08-03 11:43:19 +0800404 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405 uint64_t dummy;
406 int i, r;
407
Christian Königd71518b2016-02-01 12:20:25 +0100408 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
409 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400410 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100411
412 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400413
Chunming Zhou81287652015-07-03 14:18:26 +0800414 dummy = ib->gpu_addr + 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415
416 /* stitch together an VCE create msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800417 ib->length_dw = 0;
418 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
419 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
420 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400421
Leo Liud66f8e42015-11-18 11:57:33 -0500422 if ((ring->adev->vce.fw_version >> 24) >= 52)
423 ib->ptr[ib->length_dw++] = 0x00000040; /* len */
424 else
425 ib->ptr[ib->length_dw++] = 0x00000030; /* len */
Chunming Zhou81287652015-07-03 14:18:26 +0800426 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
427 ib->ptr[ib->length_dw++] = 0x00000000;
428 ib->ptr[ib->length_dw++] = 0x00000042;
429 ib->ptr[ib->length_dw++] = 0x0000000a;
430 ib->ptr[ib->length_dw++] = 0x00000001;
431 ib->ptr[ib->length_dw++] = 0x00000080;
432 ib->ptr[ib->length_dw++] = 0x00000060;
433 ib->ptr[ib->length_dw++] = 0x00000100;
434 ib->ptr[ib->length_dw++] = 0x00000100;
435 ib->ptr[ib->length_dw++] = 0x0000000c;
436 ib->ptr[ib->length_dw++] = 0x00000000;
Leo Liud66f8e42015-11-18 11:57:33 -0500437 if ((ring->adev->vce.fw_version >> 24) >= 52) {
438 ib->ptr[ib->length_dw++] = 0x00000000;
439 ib->ptr[ib->length_dw++] = 0x00000000;
440 ib->ptr[ib->length_dw++] = 0x00000000;
441 ib->ptr[ib->length_dw++] = 0x00000000;
442 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400443
Chunming Zhou81287652015-07-03 14:18:26 +0800444 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
445 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
446 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
447 ib->ptr[ib->length_dw++] = dummy;
448 ib->ptr[ib->length_dw++] = 0x00000001;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449
Chunming Zhou81287652015-07-03 14:18:26 +0800450 for (i = ib->length_dw; i < ib_size_dw; ++i)
451 ib->ptr[i] = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452
Monk Liuc5637832016-04-19 20:11:32 +0800453 r = amdgpu_ib_schedule(ring, 1, ib, NULL, NULL, &f);
Christian König22a77cf2016-07-05 14:48:17 +0200454 job->fence = fence_get(f);
Chunming Zhou81287652015-07-03 14:18:26 +0800455 if (r)
456 goto err;
Christian König9f2ade32016-02-03 16:50:56 +0100457
458 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459 if (fence)
Chunming Zhou17635522015-08-03 11:43:19 +0800460 *fence = fence_get(f);
Chunming Zhou281b4222015-08-12 12:58:31 +0800461 fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800462 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100463
Chunming Zhou81287652015-07-03 14:18:26 +0800464err:
Christian Königd71518b2016-02-01 12:20:25 +0100465 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466 return r;
467}
468
469/**
470 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
471 *
472 * @adev: amdgpu_device pointer
473 * @ring: ring we should submit the msg to
474 * @handle: VCE session handle to use
475 * @fence: optional fence to return
476 *
477 * Close up a stream for HW test or if userspace failed to do so
478 */
479int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
Christian König9f2ade32016-02-03 16:50:56 +0100480 bool direct, struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481{
482 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100483 struct amdgpu_job *job;
484 struct amdgpu_ib *ib;
Chunming Zhou17635522015-08-03 11:43:19 +0800485 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 int i, r;
487
Christian Königd71518b2016-02-01 12:20:25 +0100488 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
489 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491
Christian Königd71518b2016-02-01 12:20:25 +0100492 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493
494 /* stitch together an VCE destroy msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800495 ib->length_dw = 0;
496 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
497 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
498 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499
Rex Zhu99453a92016-07-21 20:46:55 +0800500 ib->ptr[ib->length_dw++] = 0x00000020; /* len */
501 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
502 ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
503 ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
504 ib->ptr[ib->length_dw++] = 0x00000000;
505 ib->ptr[ib->length_dw++] = 0x00000000;
506 ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
507 ib->ptr[ib->length_dw++] = 0x00000000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508
Chunming Zhou81287652015-07-03 14:18:26 +0800509 ib->ptr[ib->length_dw++] = 0x00000008; /* len */
510 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511
Chunming Zhou81287652015-07-03 14:18:26 +0800512 for (i = ib->length_dw; i < ib_size_dw; ++i)
513 ib->ptr[i] = 0x0;
Christian König9f2ade32016-02-03 16:50:56 +0100514
515 if (direct) {
Monk Liuc5637832016-04-19 20:11:32 +0800516 r = amdgpu_ib_schedule(ring, 1, ib, NULL, NULL, &f);
Christian König22a77cf2016-07-05 14:48:17 +0200517 job->fence = fence_get(f);
Christian König9f2ade32016-02-03 16:50:56 +0100518 if (r)
519 goto err;
520
521 amdgpu_job_free(job);
522 } else {
Christian Königc5949892016-02-10 17:43:00 +0100523 r = amdgpu_job_submit(job, ring, &ring->adev->vce.entity,
Christian König9f2ade32016-02-03 16:50:56 +0100524 AMDGPU_FENCE_OWNER_UNDEFINED, &f);
525 if (r)
526 goto err;
527 }
528
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 if (fence)
Chunming Zhou17635522015-08-03 11:43:19 +0800530 *fence = fence_get(f);
Chunming Zhou281b4222015-08-12 12:58:31 +0800531 fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800532 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100533
Chunming Zhou81287652015-07-03 14:18:26 +0800534err:
Christian Königd71518b2016-02-01 12:20:25 +0100535 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 return r;
537}
538
539/**
540 * amdgpu_vce_cs_reloc - command submission relocation
541 *
542 * @p: parser context
543 * @lo: address of lower dword
544 * @hi: address of higher dword
Christian Königf1689ec2015-06-11 20:56:18 +0200545 * @size: minimum size
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546 *
547 * Patch relocation inside command stream with real buffer address
548 */
Christian Königf1689ec2015-06-11 20:56:18 +0200549static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
Christian Königdc783302015-06-12 14:16:20 +0200550 int lo, int hi, unsigned size, uint32_t index)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551{
552 struct amdgpu_bo_va_mapping *mapping;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 struct amdgpu_bo *bo;
554 uint64_t addr;
555
Christian Königdc783302015-06-12 14:16:20 +0200556 if (index == 0xffffffff)
557 index = 0;
558
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
560 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
Christian Königdc783302015-06-12 14:16:20 +0200561 addr += ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562
563 mapping = amdgpu_cs_find_mapping(p, addr, &bo);
564 if (mapping == NULL) {
Christian Königdc783302015-06-12 14:16:20 +0200565 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
566 addr, lo, hi, size, index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400567 return -EINVAL;
568 }
569
Christian Königf1689ec2015-06-11 20:56:18 +0200570 if ((addr + (uint64_t)size) >
571 ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
572 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
573 addr, lo, hi);
574 return -EINVAL;
575 }
576
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
578 addr += amdgpu_bo_gpu_offset(bo);
Christian Königdc783302015-06-12 14:16:20 +0200579 addr -= ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580
Christian König7270f832016-01-31 11:00:41 +0100581 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
582 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583
584 return 0;
585}
586
587/**
Christian Königf1689ec2015-06-11 20:56:18 +0200588 * amdgpu_vce_validate_handle - validate stream handle
589 *
590 * @p: parser context
591 * @handle: handle to validate
Christian König2f4b9362015-06-11 21:33:55 +0200592 * @allocated: allocated a new handle?
Christian Königf1689ec2015-06-11 20:56:18 +0200593 *
594 * Validates the handle and return the found session index or -EINVAL
595 * we we don't have another free session index.
596 */
597static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
Christian Könige5223212016-07-01 22:19:25 +0200598 uint32_t handle, uint32_t *allocated)
Christian Königf1689ec2015-06-11 20:56:18 +0200599{
600 unsigned i;
601
602 /* validate the handle */
603 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
Christian König2f4b9362015-06-11 21:33:55 +0200604 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
605 if (p->adev->vce.filp[i] != p->filp) {
606 DRM_ERROR("VCE handle collision detected!\n");
607 return -EINVAL;
608 }
Christian Königf1689ec2015-06-11 20:56:18 +0200609 return i;
Christian König2f4b9362015-06-11 21:33:55 +0200610 }
Christian Königf1689ec2015-06-11 20:56:18 +0200611 }
612
613 /* handle not found try to alloc a new one */
614 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
615 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
616 p->adev->vce.filp[i] = p->filp;
617 p->adev->vce.img_size[i] = 0;
Christian Könige5223212016-07-01 22:19:25 +0200618 *allocated |= 1 << i;
Christian Königf1689ec2015-06-11 20:56:18 +0200619 return i;
620 }
621 }
622
623 DRM_ERROR("No more free VCE handles!\n");
624 return -EINVAL;
625}
626
627/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628 * amdgpu_vce_cs_parse - parse and validate the command stream
629 *
630 * @p: parser context
631 *
632 */
633int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
634{
Christian König50838c82016-02-03 13:44:52 +0100635 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
Christian Königdc783302015-06-12 14:16:20 +0200636 unsigned fb_idx = 0, bs_idx = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200637 int session_idx = -1;
Christian Könige5223212016-07-01 22:19:25 +0200638 uint32_t destroyed = 0;
639 uint32_t created = 0;
640 uint32_t allocated = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200641 uint32_t tmp, handle = 0;
642 uint32_t *size = &tmp;
Christian Königc855e252016-09-05 17:00:57 +0200643 int i, r, idx = 0;
644
Christian König45088ef2016-10-05 16:49:19 +0200645 p->job->vm = NULL;
646 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
647
Christian Königc855e252016-09-05 17:00:57 +0200648 r = amdgpu_cs_sysvm_access_required(p);
649 if (r)
650 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 while (idx < ib->length_dw) {
653 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
654 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
655
656 if ((len < 8) || (len & 3)) {
657 DRM_ERROR("invalid VCE command length (%d)!\n", len);
Christian König2f4b9362015-06-11 21:33:55 +0200658 r = -EINVAL;
659 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 }
661
662 switch (cmd) {
Christian König182830a2016-07-01 17:43:57 +0200663 case 0x00000001: /* session */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
Christian König2f4b9362015-06-11 21:33:55 +0200665 session_idx = amdgpu_vce_validate_handle(p, handle,
666 &allocated);
Christian Könige5223212016-07-01 22:19:25 +0200667 if (session_idx < 0) {
668 r = session_idx;
669 goto out;
670 }
Christian Königf1689ec2015-06-11 20:56:18 +0200671 size = &p->adev->vce.img_size[session_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 break;
673
Christian König182830a2016-07-01 17:43:57 +0200674 case 0x00000002: /* task info */
Christian Königdc783302015-06-12 14:16:20 +0200675 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
676 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
Christian Königf1689ec2015-06-11 20:56:18 +0200677 break;
678
Christian König182830a2016-07-01 17:43:57 +0200679 case 0x01000001: /* create */
Christian Könige5223212016-07-01 22:19:25 +0200680 created |= 1 << session_idx;
681 if (destroyed & (1 << session_idx)) {
682 destroyed &= ~(1 << session_idx);
683 allocated |= 1 << session_idx;
684
685 } else if (!(allocated & (1 << session_idx))) {
Christian König2f4b9362015-06-11 21:33:55 +0200686 DRM_ERROR("Handle already in use!\n");
687 r = -EINVAL;
688 goto out;
689 }
690
Christian Königf1689ec2015-06-11 20:56:18 +0200691 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
692 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
693 8 * 3 / 2;
694 break;
695
Christian König182830a2016-07-01 17:43:57 +0200696 case 0x04000001: /* config extension */
697 case 0x04000002: /* pic control */
698 case 0x04000005: /* rate control */
699 case 0x04000007: /* motion estimation */
700 case 0x04000008: /* rdo */
701 case 0x04000009: /* vui */
702 case 0x05000002: /* auxiliary buffer */
Alex Deucher4f827782016-09-21 14:57:06 -0400703 case 0x05000009: /* clock table */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 break;
705
Alex Deucher5eeda8a2016-09-23 17:22:42 -0400706 case 0x0500000c: /* hw config */
707 switch (p->adev->asic_type) {
708#ifdef CONFIG_DRM_AMDGPU_CIK
709 case CHIP_KAVERI:
710 case CHIP_MULLINS:
711#endif
712 case CHIP_CARRIZO:
713 break;
714 default:
715 r = -EINVAL;
716 goto out;
717 }
718 break;
719
Christian König182830a2016-07-01 17:43:57 +0200720 case 0x03000001: /* encode */
Christian Königf1689ec2015-06-11 20:56:18 +0200721 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
Christian Königdc783302015-06-12 14:16:20 +0200722 *size, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200724 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400725
Christian Königf1689ec2015-06-11 20:56:18 +0200726 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
Christian Königdc783302015-06-12 14:16:20 +0200727 *size / 3, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200729 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 break;
731
Christian König182830a2016-07-01 17:43:57 +0200732 case 0x02000001: /* destroy */
Christian Könige5223212016-07-01 22:19:25 +0200733 destroyed |= 1 << session_idx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 break;
735
Christian König182830a2016-07-01 17:43:57 +0200736 case 0x05000001: /* context buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200737 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200738 *size * 2, 0);
Christian Königf1689ec2015-06-11 20:56:18 +0200739 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200740 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200741 break;
742
Christian König182830a2016-07-01 17:43:57 +0200743 case 0x05000004: /* video bitstream buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200744 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
745 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200746 tmp, bs_idx);
Christian Königf1689ec2015-06-11 20:56:18 +0200747 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200748 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200749 break;
750
Christian König182830a2016-07-01 17:43:57 +0200751 case 0x05000005: /* feedback buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200752 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200753 4096, fb_idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400754 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200755 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756 break;
757
758 default:
759 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
Christian König2f4b9362015-06-11 21:33:55 +0200760 r = -EINVAL;
761 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762 }
763
Christian Königf1689ec2015-06-11 20:56:18 +0200764 if (session_idx == -1) {
765 DRM_ERROR("no session command at start of IB\n");
Christian König2f4b9362015-06-11 21:33:55 +0200766 r = -EINVAL;
767 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200768 }
769
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770 idx += len / 4;
771 }
772
Christian Könige5223212016-07-01 22:19:25 +0200773 if (allocated & ~created) {
Christian König2f4b9362015-06-11 21:33:55 +0200774 DRM_ERROR("New session without create command!\n");
775 r = -ENOENT;
776 }
777
778out:
Christian Könige5223212016-07-01 22:19:25 +0200779 if (!r) {
780 /* No error, free all destroyed handle slots */
781 tmp = destroyed;
782 } else {
783 /* Error during parsing, free all allocated handle slots */
784 tmp = allocated;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785 }
786
Christian Könige5223212016-07-01 22:19:25 +0200787 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
788 if (tmp & (1 << i))
789 atomic_set(&p->adev->vce.handles[i], 0);
790
Christian König2f4b9362015-06-11 21:33:55 +0200791 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400792}
793
794/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400795 * amdgpu_vce_ring_emit_ib - execute indirect buffer
796 *
797 * @ring: engine to use
798 * @ib: the IB to execute
799 *
800 */
Christian Königd88bf582016-05-06 17:50:03 +0200801void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib,
802 unsigned vm_id, bool ctx_switch)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400803{
804 amdgpu_ring_write(ring, VCE_CMD_IB);
805 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
806 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
807 amdgpu_ring_write(ring, ib->length_dw);
808}
809
810/**
811 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
812 *
813 * @ring: engine to use
814 * @fence: the fence
815 *
816 */
817void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +0800818 unsigned flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819{
Chunming Zhou890ee232015-06-01 14:35:03 +0800820 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400821
822 amdgpu_ring_write(ring, VCE_CMD_FENCE);
823 amdgpu_ring_write(ring, addr);
824 amdgpu_ring_write(ring, upper_32_bits(addr));
825 amdgpu_ring_write(ring, seq);
826 amdgpu_ring_write(ring, VCE_CMD_TRAP);
827 amdgpu_ring_write(ring, VCE_CMD_END);
828}
829
830/**
831 * amdgpu_vce_ring_test_ring - test if VCE ring is working
832 *
833 * @ring: the engine to test on
834 *
835 */
836int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
837{
838 struct amdgpu_device *adev = ring->adev;
839 uint32_t rptr = amdgpu_ring_get_rptr(ring);
840 unsigned i;
841 int r;
842
Christian Königa27de352016-01-21 11:28:53 +0100843 r = amdgpu_ring_alloc(ring, 16);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 if (r) {
845 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
846 ring->idx, r);
847 return r;
848 }
849 amdgpu_ring_write(ring, VCE_CMD_END);
Christian Königa27de352016-01-21 11:28:53 +0100850 amdgpu_ring_commit(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400851
852 for (i = 0; i < adev->usec_timeout; i++) {
853 if (amdgpu_ring_get_rptr(ring) != rptr)
854 break;
855 DRM_UDELAY(1);
856 }
857
858 if (i < adev->usec_timeout) {
859 DRM_INFO("ring test on %d succeeded in %d usecs\n",
860 ring->idx, i);
861 } else {
862 DRM_ERROR("amdgpu: ring %d test failed\n",
863 ring->idx);
864 r = -ETIMEDOUT;
865 }
866
867 return r;
868}
869
870/**
871 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
872 *
873 * @ring: the engine to test on
874 *
875 */
Christian Königbbec97a2016-07-05 21:07:17 +0200876int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877{
Chunming Zhoued40bfb2015-08-03 13:28:16 +0800878 struct fence *fence = NULL;
Christian Königbbec97a2016-07-05 21:07:17 +0200879 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880
Alex Deucher6f0359f2016-08-24 17:15:33 -0400881 /* skip vce ring1/2 ib test for now, since it's not reliable */
882 if (ring != &ring->adev->vce.ring[0])
Leo Liu898e50d2015-09-04 15:08:55 -0400883 return 0;
884
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
886 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200887 DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400888 goto error;
889 }
890
Christian König9f2ade32016-02-03 16:50:56 +0100891 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200893 DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894 goto error;
895 }
896
Christian Königbbec97a2016-07-05 21:07:17 +0200897 r = fence_wait_timeout(fence, false, timeout);
898 if (r == 0) {
899 DRM_ERROR("amdgpu: IB test timed out.\n");
900 r = -ETIMEDOUT;
901 } else if (r < 0) {
902 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400903 } else {
904 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
Christian Königbbec97a2016-07-05 21:07:17 +0200905 r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400906 }
907error:
Chunming Zhoued40bfb2015-08-03 13:28:16 +0800908 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400909 return r;
910}