blob: cb1bff7425503512f3316f0425a2cfe0b7e45ead [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 */
39#define VCE_IDLE_TIMEOUT_MS 1000
40
41/* Firmware Names */
42#ifdef CONFIG_DRM_AMDGPU_CIK
43#define FIRMWARE_BONAIRE "radeon/bonaire_vce.bin"
44#define FIRMWARE_KABINI "radeon/kabini_vce.bin"
45#define FIRMWARE_KAVERI "radeon/kaveri_vce.bin"
46#define FIRMWARE_HAWAII "radeon/hawaii_vce.bin"
47#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 Deucherd38ceaf2015-04-20 16:55:21 -040051
52#ifdef CONFIG_DRM_AMDGPU_CIK
53MODULE_FIRMWARE(FIRMWARE_BONAIRE);
54MODULE_FIRMWARE(FIRMWARE_KABINI);
55MODULE_FIRMWARE(FIRMWARE_KAVERI);
56MODULE_FIRMWARE(FIRMWARE_HAWAII);
57MODULE_FIRMWARE(FIRMWARE_MULLINS);
58#endif
59MODULE_FIRMWARE(FIRMWARE_TONGA);
60MODULE_FIRMWARE(FIRMWARE_CARRIZO);
61
62static void amdgpu_vce_idle_work_handler(struct work_struct *work);
63
64/**
65 * amdgpu_vce_init - allocate memory, load vce firmware
66 *
67 * @adev: amdgpu_device pointer
68 *
69 * First step to get VCE online, allocate memory and load the firmware
70 */
Leo Liue9822622015-05-06 14:31:27 -040071int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072{
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073 const char *fw_name;
74 const struct common_firmware_header *hdr;
75 unsigned ucode_version, version_major, version_minor, binary_id;
76 int i, r;
77
78 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
79
80 switch (adev->asic_type) {
81#ifdef CONFIG_DRM_AMDGPU_CIK
82 case CHIP_BONAIRE:
83 fw_name = FIRMWARE_BONAIRE;
84 break;
85 case CHIP_KAVERI:
86 fw_name = FIRMWARE_KAVERI;
87 break;
88 case CHIP_KABINI:
89 fw_name = FIRMWARE_KABINI;
90 break;
91 case CHIP_HAWAII:
92 fw_name = FIRMWARE_HAWAII;
93 break;
94 case CHIP_MULLINS:
95 fw_name = FIRMWARE_MULLINS;
96 break;
97#endif
98 case CHIP_TONGA:
99 fw_name = FIRMWARE_TONGA;
100 break;
101 case CHIP_CARRIZO:
102 fw_name = FIRMWARE_CARRIZO;
103 break;
104
105 default:
106 return -EINVAL;
107 }
108
109 r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
110 if (r) {
111 dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
112 fw_name);
113 return r;
114 }
115
116 r = amdgpu_ucode_validate(adev->vce.fw);
117 if (r) {
118 dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
119 fw_name);
120 release_firmware(adev->vce.fw);
121 adev->vce.fw = NULL;
122 return r;
123 }
124
125 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
126
127 ucode_version = le32_to_cpu(hdr->ucode_version);
128 version_major = (ucode_version >> 20) & 0xfff;
129 version_minor = (ucode_version >> 8) & 0xfff;
130 binary_id = ucode_version & 0xff;
131 DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
132 version_major, version_minor, binary_id);
133 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
134 (binary_id << 8));
135
136 /* allocate firmware, stack and heap BO */
137
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
139 AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &adev->vce.vcpu_bo);
140 if (r) {
141 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
142 return r;
143 }
144
145 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
146 if (r) {
147 amdgpu_bo_unref(&adev->vce.vcpu_bo);
148 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
149 return r;
150 }
151
152 r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
153 &adev->vce.gpu_addr);
154 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
155 if (r) {
156 amdgpu_bo_unref(&adev->vce.vcpu_bo);
157 dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
158 return r;
159 }
160
161 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
162 atomic_set(&adev->vce.handles[i], 0);
163 adev->vce.filp[i] = NULL;
164 }
165
166 return 0;
167}
168
169/**
170 * amdgpu_vce_fini - free memory
171 *
172 * @adev: amdgpu_device pointer
173 *
174 * Last step on VCE teardown, free firmware memory
175 */
176int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
177{
178 if (adev->vce.vcpu_bo == NULL)
179 return 0;
180
181 amdgpu_bo_unref(&adev->vce.vcpu_bo);
182
183 amdgpu_ring_fini(&adev->vce.ring[0]);
184 amdgpu_ring_fini(&adev->vce.ring[1]);
185
186 release_firmware(adev->vce.fw);
187
188 return 0;
189}
190
191/**
192 * amdgpu_vce_suspend - unpin VCE fw memory
193 *
194 * @adev: amdgpu_device pointer
195 *
196 */
197int amdgpu_vce_suspend(struct amdgpu_device *adev)
198{
199 int i;
200
201 if (adev->vce.vcpu_bo == NULL)
202 return 0;
203
204 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
205 if (atomic_read(&adev->vce.handles[i]))
206 break;
207
208 if (i == AMDGPU_MAX_VCE_HANDLES)
209 return 0;
210
211 /* TODO: suspending running encoding sessions isn't supported */
212 return -EINVAL;
213}
214
215/**
216 * amdgpu_vce_resume - pin VCE fw memory
217 *
218 * @adev: amdgpu_device pointer
219 *
220 */
221int amdgpu_vce_resume(struct amdgpu_device *adev)
222{
223 void *cpu_addr;
224 const struct common_firmware_header *hdr;
225 unsigned offset;
226 int r;
227
228 if (adev->vce.vcpu_bo == NULL)
229 return -EINVAL;
230
231 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
232 if (r) {
233 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
234 return r;
235 }
236
237 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
238 if (r) {
239 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
240 dev_err(adev->dev, "(%d) VCE map failed\n", r);
241 return r;
242 }
243
244 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
245 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
246 memcpy(cpu_addr, (adev->vce.fw->data) + offset,
247 (adev->vce.fw->size) - offset);
248
249 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
250
251 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
252
253 return 0;
254}
255
256/**
257 * amdgpu_vce_idle_work_handler - power off VCE
258 *
259 * @work: pointer to work structure
260 *
261 * power of VCE when it's not used any more
262 */
263static void amdgpu_vce_idle_work_handler(struct work_struct *work)
264{
265 struct amdgpu_device *adev =
266 container_of(work, struct amdgpu_device, vce.idle_work.work);
267
268 if ((amdgpu_fence_count_emitted(&adev->vce.ring[0]) == 0) &&
269 (amdgpu_fence_count_emitted(&adev->vce.ring[1]) == 0)) {
270 if (adev->pm.dpm_enabled) {
271 amdgpu_dpm_enable_vce(adev, false);
272 } else {
273 amdgpu_asic_set_vce_clocks(adev, 0, 0);
274 }
275 } else {
276 schedule_delayed_work(&adev->vce.idle_work,
277 msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
278 }
279}
280
281/**
282 * amdgpu_vce_note_usage - power up VCE
283 *
284 * @adev: amdgpu_device pointer
285 *
286 * Make sure VCE is powerd up when we want to use it
287 */
288static void amdgpu_vce_note_usage(struct amdgpu_device *adev)
289{
290 bool streams_changed = false;
291 bool set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
292 set_clocks &= schedule_delayed_work(&adev->vce.idle_work,
293 msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
294
295 if (adev->pm.dpm_enabled) {
296 /* XXX figure out if the streams changed */
297 streams_changed = false;
298 }
299
300 if (set_clocks || streams_changed) {
301 if (adev->pm.dpm_enabled) {
302 amdgpu_dpm_enable_vce(adev, true);
303 } else {
304 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
305 }
306 }
307}
308
309/**
310 * amdgpu_vce_free_handles - free still open VCE handles
311 *
312 * @adev: amdgpu_device pointer
313 * @filp: drm file pointer
314 *
315 * Close all VCE handles still open by this file pointer
316 */
317void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
318{
319 struct amdgpu_ring *ring = &adev->vce.ring[0];
320 int i, r;
321 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
322 uint32_t handle = atomic_read(&adev->vce.handles[i]);
323 if (!handle || adev->vce.filp[i] != filp)
324 continue;
325
326 amdgpu_vce_note_usage(adev);
327
328 r = amdgpu_vce_get_destroy_msg(ring, handle, NULL);
329 if (r)
330 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
331
332 adev->vce.filp[i] = NULL;
333 atomic_set(&adev->vce.handles[i], 0);
334 }
335}
336
337/**
338 * amdgpu_vce_get_create_msg - generate a VCE create msg
339 *
340 * @adev: amdgpu_device pointer
341 * @ring: ring we should submit the msg to
342 * @handle: VCE session handle to use
343 * @fence: optional fence to return
344 *
345 * Open up a stream for HW test
346 */
347int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
348 struct amdgpu_fence **fence)
349{
350 const unsigned ib_size_dw = 1024;
351 struct amdgpu_ib ib;
352 uint64_t dummy;
353 int i, r;
354
355 r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, &ib);
356 if (r) {
357 DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
358 return r;
359 }
360
361 dummy = ib.gpu_addr + 1024;
362
363 /* stitch together an VCE create msg */
364 ib.length_dw = 0;
365 ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
366 ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
367 ib.ptr[ib.length_dw++] = handle;
368
369 ib.ptr[ib.length_dw++] = 0x00000030; /* len */
370 ib.ptr[ib.length_dw++] = 0x01000001; /* create cmd */
371 ib.ptr[ib.length_dw++] = 0x00000000;
372 ib.ptr[ib.length_dw++] = 0x00000042;
373 ib.ptr[ib.length_dw++] = 0x0000000a;
374 ib.ptr[ib.length_dw++] = 0x00000001;
375 ib.ptr[ib.length_dw++] = 0x00000080;
376 ib.ptr[ib.length_dw++] = 0x00000060;
377 ib.ptr[ib.length_dw++] = 0x00000100;
378 ib.ptr[ib.length_dw++] = 0x00000100;
379 ib.ptr[ib.length_dw++] = 0x0000000c;
380 ib.ptr[ib.length_dw++] = 0x00000000;
381
382 ib.ptr[ib.length_dw++] = 0x00000014; /* len */
383 ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
384 ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
385 ib.ptr[ib.length_dw++] = dummy;
386 ib.ptr[ib.length_dw++] = 0x00000001;
387
388 for (i = ib.length_dw; i < ib_size_dw; ++i)
389 ib.ptr[i] = 0x0;
390
391 r = amdgpu_ib_schedule(ring->adev, 1, &ib, AMDGPU_FENCE_OWNER_UNDEFINED);
392 if (r) {
393 DRM_ERROR("amdgpu: failed to schedule ib (%d).\n", r);
394 }
395
396 if (fence)
397 *fence = amdgpu_fence_ref(ib.fence);
398
399 amdgpu_ib_free(ring->adev, &ib);
400
401 return r;
402}
403
404/**
405 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
406 *
407 * @adev: amdgpu_device pointer
408 * @ring: ring we should submit the msg to
409 * @handle: VCE session handle to use
410 * @fence: optional fence to return
411 *
412 * Close up a stream for HW test or if userspace failed to do so
413 */
414int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
415 struct amdgpu_fence **fence)
416{
417 const unsigned ib_size_dw = 1024;
418 struct amdgpu_ib ib;
419 uint64_t dummy;
420 int i, r;
421
422 r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, &ib);
423 if (r) {
424 DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
425 return r;
426 }
427
428 dummy = ib.gpu_addr + 1024;
429
430 /* stitch together an VCE destroy msg */
431 ib.length_dw = 0;
432 ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
433 ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
434 ib.ptr[ib.length_dw++] = handle;
435
436 ib.ptr[ib.length_dw++] = 0x00000014; /* len */
437 ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
438 ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
439 ib.ptr[ib.length_dw++] = dummy;
440 ib.ptr[ib.length_dw++] = 0x00000001;
441
442 ib.ptr[ib.length_dw++] = 0x00000008; /* len */
443 ib.ptr[ib.length_dw++] = 0x02000001; /* destroy cmd */
444
445 for (i = ib.length_dw; i < ib_size_dw; ++i)
446 ib.ptr[i] = 0x0;
447
448 r = amdgpu_ib_schedule(ring->adev, 1, &ib, AMDGPU_FENCE_OWNER_UNDEFINED);
449 if (r) {
450 DRM_ERROR("amdgpu: failed to schedule ib (%d).\n", r);
451 }
452
453 if (fence)
454 *fence = amdgpu_fence_ref(ib.fence);
455
456 amdgpu_ib_free(ring->adev, &ib);
457
458 return r;
459}
460
461/**
462 * amdgpu_vce_cs_reloc - command submission relocation
463 *
464 * @p: parser context
465 * @lo: address of lower dword
466 * @hi: address of higher dword
Christian Königf1689ec2015-06-11 20:56:18 +0200467 * @size: minimum size
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400468 *
469 * Patch relocation inside command stream with real buffer address
470 */
Christian Königf1689ec2015-06-11 20:56:18 +0200471static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
472 int lo, int hi, unsigned size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473{
474 struct amdgpu_bo_va_mapping *mapping;
475 struct amdgpu_ib *ib = &p->ibs[ib_idx];
476 struct amdgpu_bo *bo;
477 uint64_t addr;
478
479 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
480 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
481
482 mapping = amdgpu_cs_find_mapping(p, addr, &bo);
483 if (mapping == NULL) {
484 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d\n",
485 addr, lo, hi);
486 return -EINVAL;
487 }
488
Christian Königf1689ec2015-06-11 20:56:18 +0200489 if ((addr + (uint64_t)size) >
490 ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
491 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
492 addr, lo, hi);
493 return -EINVAL;
494 }
495
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496 addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
497 addr += amdgpu_bo_gpu_offset(bo);
498
499 ib->ptr[lo] = addr & 0xFFFFFFFF;
500 ib->ptr[hi] = addr >> 32;
501
502 return 0;
503}
504
505/**
Christian Königf1689ec2015-06-11 20:56:18 +0200506 * amdgpu_vce_validate_handle - validate stream handle
507 *
508 * @p: parser context
509 * @handle: handle to validate
510 *
511 * Validates the handle and return the found session index or -EINVAL
512 * we we don't have another free session index.
513 */
514static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
515 uint32_t handle)
516{
517 unsigned i;
518
519 /* validate the handle */
520 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
521 if (atomic_read(&p->adev->vce.handles[i]) == handle)
522 return i;
523 }
524
525 /* handle not found try to alloc a new one */
526 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
527 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
528 p->adev->vce.filp[i] = p->filp;
529 p->adev->vce.img_size[i] = 0;
530 return i;
531 }
532 }
533
534 DRM_ERROR("No more free VCE handles!\n");
535 return -EINVAL;
536}
537
538/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 * amdgpu_vce_cs_parse - parse and validate the command stream
540 *
541 * @p: parser context
542 *
543 */
544int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
545{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546 struct amdgpu_ib *ib = &p->ibs[ib_idx];
Christian Königf1689ec2015-06-11 20:56:18 +0200547 int session_idx = -1;
548 bool destroyed = false;
549 uint32_t tmp, handle = 0;
550 uint32_t *size = &tmp;
551 int i, r, idx = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400552
553 amdgpu_vce_note_usage(p->adev);
554
555 while (idx < ib->length_dw) {
556 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
557 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
558
559 if ((len < 8) || (len & 3)) {
560 DRM_ERROR("invalid VCE command length (%d)!\n", len);
561 return -EINVAL;
562 }
563
Christian Königf1689ec2015-06-11 20:56:18 +0200564 if (destroyed) {
565 DRM_ERROR("No other command allowed after destroy!\n");
566 return -EINVAL;
567 }
568
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569 switch (cmd) {
570 case 0x00000001: // session
571 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
Christian Königf1689ec2015-06-11 20:56:18 +0200572 session_idx = amdgpu_vce_validate_handle(p, handle);
573 if (session_idx < 0)
574 return session_idx;
575 size = &p->adev->vce.img_size[session_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576 break;
577
578 case 0x00000002: // task info
Christian Königf1689ec2015-06-11 20:56:18 +0200579 break;
580
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 case 0x01000001: // create
Christian Königf1689ec2015-06-11 20:56:18 +0200582 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
583 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
584 8 * 3 / 2;
585 break;
586
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 case 0x04000001: // config extension
588 case 0x04000002: // pic control
589 case 0x04000005: // rate control
590 case 0x04000007: // motion estimation
591 case 0x04000008: // rdo
592 case 0x04000009: // vui
593 case 0x05000002: // auxiliary buffer
594 break;
595
596 case 0x03000001: // encode
Christian Königf1689ec2015-06-11 20:56:18 +0200597 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
598 *size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 if (r)
600 return r;
601
Christian Königf1689ec2015-06-11 20:56:18 +0200602 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
603 *size / 3);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 if (r)
605 return r;
606 break;
607
608 case 0x02000001: // destroy
Christian Königf1689ec2015-06-11 20:56:18 +0200609 destroyed = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610 break;
611
612 case 0x05000001: // context buffer
Christian Königf1689ec2015-06-11 20:56:18 +0200613 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
614 *size * 2);
615 if (r)
616 return r;
617 break;
618
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 case 0x05000004: // video bitstream buffer
Christian Königf1689ec2015-06-11 20:56:18 +0200620 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
621 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
622 tmp);
623 if (r)
624 return r;
625 break;
626
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 case 0x05000005: // feedback buffer
Christian Königf1689ec2015-06-11 20:56:18 +0200628 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
629 4096);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 if (r)
631 return r;
632 break;
633
634 default:
635 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
636 return -EINVAL;
637 }
638
Christian Königf1689ec2015-06-11 20:56:18 +0200639 if (session_idx == -1) {
640 DRM_ERROR("no session command at start of IB\n");
641 return -EINVAL;
642 }
643
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 idx += len / 4;
645 }
646
Christian Königf1689ec2015-06-11 20:56:18 +0200647 if (destroyed) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 /* IB contains a destroy msg, free the handle */
649 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
650 atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651 }
652
Christian Königf1689ec2015-06-11 20:56:18 +0200653 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654}
655
656/**
657 * amdgpu_vce_ring_emit_semaphore - emit a semaphore command
658 *
659 * @ring: engine to use
660 * @semaphore: address of semaphore
661 * @emit_wait: true=emit wait, false=emit signal
662 *
663 */
664bool amdgpu_vce_ring_emit_semaphore(struct amdgpu_ring *ring,
665 struct amdgpu_semaphore *semaphore,
666 bool emit_wait)
667{
668 uint64_t addr = semaphore->gpu_addr;
669
670 amdgpu_ring_write(ring, VCE_CMD_SEMAPHORE);
671 amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
672 amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
673 amdgpu_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
674 if (!emit_wait)
675 amdgpu_ring_write(ring, VCE_CMD_END);
676
677 return true;
678}
679
680/**
681 * amdgpu_vce_ring_emit_ib - execute indirect buffer
682 *
683 * @ring: engine to use
684 * @ib: the IB to execute
685 *
686 */
687void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
688{
689 amdgpu_ring_write(ring, VCE_CMD_IB);
690 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
691 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
692 amdgpu_ring_write(ring, ib->length_dw);
693}
694
695/**
696 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
697 *
698 * @ring: engine to use
699 * @fence: the fence
700 *
701 */
702void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +0800703 unsigned flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704{
Chunming Zhou890ee232015-06-01 14:35:03 +0800705 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706
707 amdgpu_ring_write(ring, VCE_CMD_FENCE);
708 amdgpu_ring_write(ring, addr);
709 amdgpu_ring_write(ring, upper_32_bits(addr));
710 amdgpu_ring_write(ring, seq);
711 amdgpu_ring_write(ring, VCE_CMD_TRAP);
712 amdgpu_ring_write(ring, VCE_CMD_END);
713}
714
715/**
716 * amdgpu_vce_ring_test_ring - test if VCE ring is working
717 *
718 * @ring: the engine to test on
719 *
720 */
721int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
722{
723 struct amdgpu_device *adev = ring->adev;
724 uint32_t rptr = amdgpu_ring_get_rptr(ring);
725 unsigned i;
726 int r;
727
728 r = amdgpu_ring_lock(ring, 16);
729 if (r) {
730 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
731 ring->idx, r);
732 return r;
733 }
734 amdgpu_ring_write(ring, VCE_CMD_END);
735 amdgpu_ring_unlock_commit(ring);
736
737 for (i = 0; i < adev->usec_timeout; i++) {
738 if (amdgpu_ring_get_rptr(ring) != rptr)
739 break;
740 DRM_UDELAY(1);
741 }
742
743 if (i < adev->usec_timeout) {
744 DRM_INFO("ring test on %d succeeded in %d usecs\n",
745 ring->idx, i);
746 } else {
747 DRM_ERROR("amdgpu: ring %d test failed\n",
748 ring->idx);
749 r = -ETIMEDOUT;
750 }
751
752 return r;
753}
754
755/**
756 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
757 *
758 * @ring: the engine to test on
759 *
760 */
761int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
762{
763 struct amdgpu_fence *fence = NULL;
764 int r;
765
766 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
767 if (r) {
768 DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
769 goto error;
770 }
771
772 r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
773 if (r) {
774 DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
775 goto error;
776 }
777
778 r = amdgpu_fence_wait(fence, false);
779 if (r) {
780 DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
781 } else {
782 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
783 }
784error:
785 amdgpu_fence_unref(&fence);
786 return r;
787}