]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
drm/amdgpu: send SDMA/GFX IB tests directly to the ring again
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_vce.c
CommitLineData
d38ceaf9
AD
1/*
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
c65444fe
JZ
49#define FIRMWARE_TONGA "amdgpu/tonga_vce.bin"
50#define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin"
188a9bcd 51#define FIRMWARE_FIJI "amdgpu/fiji_vce.bin"
cfaba566 52#define FIRMWARE_STONEY "amdgpu/stoney_vce.bin"
d38ceaf9
AD
53
54#ifdef CONFIG_DRM_AMDGPU_CIK
55MODULE_FIRMWARE(FIRMWARE_BONAIRE);
56MODULE_FIRMWARE(FIRMWARE_KABINI);
57MODULE_FIRMWARE(FIRMWARE_KAVERI);
58MODULE_FIRMWARE(FIRMWARE_HAWAII);
59MODULE_FIRMWARE(FIRMWARE_MULLINS);
60#endif
61MODULE_FIRMWARE(FIRMWARE_TONGA);
62MODULE_FIRMWARE(FIRMWARE_CARRIZO);
188a9bcd 63MODULE_FIRMWARE(FIRMWARE_FIJI);
cfaba566 64MODULE_FIRMWARE(FIRMWARE_STONEY);
d38ceaf9
AD
65
66static void amdgpu_vce_idle_work_handler(struct work_struct *work);
67
68/**
69 * amdgpu_vce_init - allocate memory, load vce firmware
70 *
71 * @adev: amdgpu_device pointer
72 *
73 * First step to get VCE online, allocate memory and load the firmware
74 */
e9822622 75int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
d38ceaf9 76{
d38ceaf9
AD
77 const char *fw_name;
78 const struct common_firmware_header *hdr;
79 unsigned ucode_version, version_major, version_minor, binary_id;
80 int i, r;
81
82 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
83
84 switch (adev->asic_type) {
85#ifdef CONFIG_DRM_AMDGPU_CIK
86 case CHIP_BONAIRE:
87 fw_name = FIRMWARE_BONAIRE;
88 break;
89 case CHIP_KAVERI:
90 fw_name = FIRMWARE_KAVERI;
91 break;
92 case CHIP_KABINI:
93 fw_name = FIRMWARE_KABINI;
94 break;
95 case CHIP_HAWAII:
96 fw_name = FIRMWARE_HAWAII;
97 break;
98 case CHIP_MULLINS:
99 fw_name = FIRMWARE_MULLINS;
100 break;
101#endif
102 case CHIP_TONGA:
103 fw_name = FIRMWARE_TONGA;
104 break;
105 case CHIP_CARRIZO:
106 fw_name = FIRMWARE_CARRIZO;
107 break;
188a9bcd
AD
108 case CHIP_FIJI:
109 fw_name = FIRMWARE_FIJI;
110 break;
cfaba566
SL
111 case CHIP_STONEY:
112 fw_name = FIRMWARE_STONEY;
113 break;
d38ceaf9
AD
114
115 default:
116 return -EINVAL;
117 }
118
119 r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
120 if (r) {
121 dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
122 fw_name);
123 return r;
124 }
125
126 r = amdgpu_ucode_validate(adev->vce.fw);
127 if (r) {
128 dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
129 fw_name);
130 release_firmware(adev->vce.fw);
131 adev->vce.fw = NULL;
132 return r;
133 }
134
135 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
136
137 ucode_version = le32_to_cpu(hdr->ucode_version);
138 version_major = (ucode_version >> 20) & 0xfff;
139 version_minor = (ucode_version >> 8) & 0xfff;
140 binary_id = ucode_version & 0xff;
141 DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
142 version_major, version_minor, binary_id);
143 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
144 (binary_id << 8));
145
146 /* allocate firmware, stack and heap BO */
147
d38ceaf9 148 r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
857d913d
AD
149 AMDGPU_GEM_DOMAIN_VRAM,
150 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
72d7668b 151 NULL, NULL, &adev->vce.vcpu_bo);
d38ceaf9
AD
152 if (r) {
153 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
154 return r;
155 }
156
157 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
158 if (r) {
159 amdgpu_bo_unref(&adev->vce.vcpu_bo);
160 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
161 return r;
162 }
163
164 r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
165 &adev->vce.gpu_addr);
166 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
167 if (r) {
168 amdgpu_bo_unref(&adev->vce.vcpu_bo);
169 dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
170 return r;
171 }
172
173 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
174 atomic_set(&adev->vce.handles[i], 0);
175 adev->vce.filp[i] = NULL;
176 }
177
178 return 0;
179}
180
181/**
182 * amdgpu_vce_fini - free memory
183 *
184 * @adev: amdgpu_device pointer
185 *
186 * Last step on VCE teardown, free firmware memory
187 */
188int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
189{
190 if (adev->vce.vcpu_bo == NULL)
191 return 0;
192
193 amdgpu_bo_unref(&adev->vce.vcpu_bo);
194
195 amdgpu_ring_fini(&adev->vce.ring[0]);
196 amdgpu_ring_fini(&adev->vce.ring[1]);
197
198 release_firmware(adev->vce.fw);
199
200 return 0;
201}
202
203/**
204 * amdgpu_vce_suspend - unpin VCE fw memory
205 *
206 * @adev: amdgpu_device pointer
207 *
208 */
209int amdgpu_vce_suspend(struct amdgpu_device *adev)
210{
211 int i;
212
213 if (adev->vce.vcpu_bo == NULL)
214 return 0;
215
216 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
217 if (atomic_read(&adev->vce.handles[i]))
218 break;
219
220 if (i == AMDGPU_MAX_VCE_HANDLES)
221 return 0;
222
223 /* TODO: suspending running encoding sessions isn't supported */
224 return -EINVAL;
225}
226
227/**
228 * amdgpu_vce_resume - pin VCE fw memory
229 *
230 * @adev: amdgpu_device pointer
231 *
232 */
233int amdgpu_vce_resume(struct amdgpu_device *adev)
234{
235 void *cpu_addr;
236 const struct common_firmware_header *hdr;
237 unsigned offset;
238 int r;
239
240 if (adev->vce.vcpu_bo == NULL)
241 return -EINVAL;
242
243 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
244 if (r) {
245 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
246 return r;
247 }
248
249 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
250 if (r) {
251 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
252 dev_err(adev->dev, "(%d) VCE map failed\n", r);
253 return r;
254 }
255
256 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
257 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
258 memcpy(cpu_addr, (adev->vce.fw->data) + offset,
259 (adev->vce.fw->size) - offset);
260
261 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
262
263 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
264
265 return 0;
266}
267
268/**
269 * amdgpu_vce_idle_work_handler - power off VCE
270 *
271 * @work: pointer to work structure
272 *
273 * power of VCE when it's not used any more
274 */
275static void amdgpu_vce_idle_work_handler(struct work_struct *work)
276{
277 struct amdgpu_device *adev =
278 container_of(work, struct amdgpu_device, vce.idle_work.work);
279
280 if ((amdgpu_fence_count_emitted(&adev->vce.ring[0]) == 0) &&
281 (amdgpu_fence_count_emitted(&adev->vce.ring[1]) == 0)) {
282 if (adev->pm.dpm_enabled) {
283 amdgpu_dpm_enable_vce(adev, false);
284 } else {
285 amdgpu_asic_set_vce_clocks(adev, 0, 0);
286 }
287 } else {
288 schedule_delayed_work(&adev->vce.idle_work,
289 msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
290 }
291}
292
293/**
294 * amdgpu_vce_note_usage - power up VCE
295 *
296 * @adev: amdgpu_device pointer
297 *
298 * Make sure VCE is powerd up when we want to use it
299 */
300static void amdgpu_vce_note_usage(struct amdgpu_device *adev)
301{
302 bool streams_changed = false;
303 bool set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
304 set_clocks &= schedule_delayed_work(&adev->vce.idle_work,
305 msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
306
307 if (adev->pm.dpm_enabled) {
308 /* XXX figure out if the streams changed */
309 streams_changed = false;
310 }
311
312 if (set_clocks || streams_changed) {
313 if (adev->pm.dpm_enabled) {
314 amdgpu_dpm_enable_vce(adev, true);
315 } else {
316 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
317 }
318 }
319}
320
321/**
322 * amdgpu_vce_free_handles - free still open VCE handles
323 *
324 * @adev: amdgpu_device pointer
325 * @filp: drm file pointer
326 *
327 * Close all VCE handles still open by this file pointer
328 */
329void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
330{
331 struct amdgpu_ring *ring = &adev->vce.ring[0];
332 int i, r;
333 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
334 uint32_t handle = atomic_read(&adev->vce.handles[i]);
335 if (!handle || adev->vce.filp[i] != filp)
336 continue;
337
338 amdgpu_vce_note_usage(adev);
339
340 r = amdgpu_vce_get_destroy_msg(ring, handle, NULL);
341 if (r)
342 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
343
344 adev->vce.filp[i] = NULL;
345 atomic_set(&adev->vce.handles[i], 0);
346 }
347}
348
8128765c 349static int amdgpu_vce_free_job(
4c7eb91c 350 struct amdgpu_job *job)
8128765c 351{
4c7eb91c
JZ
352 amdgpu_ib_free(job->adev, job->ibs);
353 kfree(job->ibs);
8128765c
CZ
354 return 0;
355}
356
d38ceaf9
AD
357/**
358 * amdgpu_vce_get_create_msg - generate a VCE create msg
359 *
360 * @adev: amdgpu_device pointer
361 * @ring: ring we should submit the msg to
362 * @handle: VCE session handle to use
363 * @fence: optional fence to return
364 *
365 * Open up a stream for HW test
366 */
367int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
ed40bfb8 368 struct fence **fence)
d38ceaf9
AD
369{
370 const unsigned ib_size_dw = 1024;
8128765c 371 struct amdgpu_ib *ib = NULL;
1763552e 372 struct fence *f = NULL;
8128765c 373 struct amdgpu_device *adev = ring->adev;
d38ceaf9
AD
374 uint64_t dummy;
375 int i, r;
376
8128765c
CZ
377 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
378 if (!ib)
379 return -ENOMEM;
b07c60c0 380 r = amdgpu_ib_get(adev, NULL, ib_size_dw * 4, ib);
d38ceaf9
AD
381 if (r) {
382 DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
8128765c 383 kfree(ib);
d38ceaf9
AD
384 return r;
385 }
386
8128765c 387 dummy = ib->gpu_addr + 1024;
d38ceaf9
AD
388
389 /* stitch together an VCE create msg */
8128765c
CZ
390 ib->length_dw = 0;
391 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
392 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
393 ib->ptr[ib->length_dw++] = handle;
394
d66f8e48
LL
395 if ((ring->adev->vce.fw_version >> 24) >= 52)
396 ib->ptr[ib->length_dw++] = 0x00000040; /* len */
397 else
398 ib->ptr[ib->length_dw++] = 0x00000030; /* len */
8128765c
CZ
399 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
400 ib->ptr[ib->length_dw++] = 0x00000000;
401 ib->ptr[ib->length_dw++] = 0x00000042;
402 ib->ptr[ib->length_dw++] = 0x0000000a;
403 ib->ptr[ib->length_dw++] = 0x00000001;
404 ib->ptr[ib->length_dw++] = 0x00000080;
405 ib->ptr[ib->length_dw++] = 0x00000060;
406 ib->ptr[ib->length_dw++] = 0x00000100;
407 ib->ptr[ib->length_dw++] = 0x00000100;
408 ib->ptr[ib->length_dw++] = 0x0000000c;
409 ib->ptr[ib->length_dw++] = 0x00000000;
d66f8e48
LL
410 if ((ring->adev->vce.fw_version >> 24) >= 52) {
411 ib->ptr[ib->length_dw++] = 0x00000000;
412 ib->ptr[ib->length_dw++] = 0x00000000;
413 ib->ptr[ib->length_dw++] = 0x00000000;
414 ib->ptr[ib->length_dw++] = 0x00000000;
415 }
8128765c
CZ
416
417 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
418 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
419 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
420 ib->ptr[ib->length_dw++] = dummy;
421 ib->ptr[ib->length_dw++] = 0x00000001;
422
423 for (i = ib->length_dw; i < ib_size_dw; ++i)
424 ib->ptr[i] = 0x0;
425
426 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
427 &amdgpu_vce_free_job,
1763552e
CZ
428 AMDGPU_FENCE_OWNER_UNDEFINED,
429 &f);
8128765c
CZ
430 if (r)
431 goto err;
d38ceaf9 432 if (fence)
1763552e 433 *fence = fence_get(f);
281b4223 434 fence_put(f);
cadf97b1 435 return 0;
8128765c
CZ
436err:
437 amdgpu_ib_free(adev, ib);
438 kfree(ib);
d38ceaf9
AD
439 return r;
440}
441
442/**
443 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
444 *
445 * @adev: amdgpu_device pointer
446 * @ring: ring we should submit the msg to
447 * @handle: VCE session handle to use
448 * @fence: optional fence to return
449 *
450 * Close up a stream for HW test or if userspace failed to do so
451 */
452int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
ed40bfb8 453 struct fence **fence)
d38ceaf9
AD
454{
455 const unsigned ib_size_dw = 1024;
8128765c 456 struct amdgpu_ib *ib = NULL;
1763552e 457 struct fence *f = NULL;
8128765c 458 struct amdgpu_device *adev = ring->adev;
d38ceaf9
AD
459 uint64_t dummy;
460 int i, r;
461
8128765c
CZ
462 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
463 if (!ib)
464 return -ENOMEM;
465
b07c60c0 466 r = amdgpu_ib_get(adev, NULL, ib_size_dw * 4, ib);
d38ceaf9 467 if (r) {
8128765c 468 kfree(ib);
d38ceaf9
AD
469 DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
470 return r;
471 }
472
8128765c 473 dummy = ib->gpu_addr + 1024;
d38ceaf9
AD
474
475 /* stitch together an VCE destroy msg */
8128765c
CZ
476 ib->length_dw = 0;
477 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
478 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
479 ib->ptr[ib->length_dw++] = handle;
480
481 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
482 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
483 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
484 ib->ptr[ib->length_dw++] = dummy;
485 ib->ptr[ib->length_dw++] = 0x00000001;
486
487 ib->ptr[ib->length_dw++] = 0x00000008; /* len */
488 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
489
490 for (i = ib->length_dw; i < ib_size_dw; ++i)
491 ib->ptr[i] = 0x0;
492 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
493 &amdgpu_vce_free_job,
1763552e
CZ
494 AMDGPU_FENCE_OWNER_UNDEFINED,
495 &f);
8128765c
CZ
496 if (r)
497 goto err;
d38ceaf9 498 if (fence)
1763552e 499 *fence = fence_get(f);
281b4223 500 fence_put(f);
cadf97b1 501 return 0;
8128765c
CZ
502err:
503 amdgpu_ib_free(adev, ib);
504 kfree(ib);
d38ceaf9
AD
505 return r;
506}
507
508/**
509 * amdgpu_vce_cs_reloc - command submission relocation
510 *
511 * @p: parser context
512 * @lo: address of lower dword
513 * @hi: address of higher dword
f1689ec1 514 * @size: minimum size
d38ceaf9
AD
515 *
516 * Patch relocation inside command stream with real buffer address
517 */
f1689ec1 518static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
dc78330a 519 int lo, int hi, unsigned size, uint32_t index)
d38ceaf9
AD
520{
521 struct amdgpu_bo_va_mapping *mapping;
d38ceaf9
AD
522 struct amdgpu_bo *bo;
523 uint64_t addr;
524
dc78330a
CK
525 if (index == 0xffffffff)
526 index = 0;
527
d38ceaf9
AD
528 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
529 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
dc78330a 530 addr += ((uint64_t)size) * ((uint64_t)index);
d38ceaf9
AD
531
532 mapping = amdgpu_cs_find_mapping(p, addr, &bo);
533 if (mapping == NULL) {
dc78330a
CK
534 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
535 addr, lo, hi, size, index);
d38ceaf9
AD
536 return -EINVAL;
537 }
538
f1689ec1
CK
539 if ((addr + (uint64_t)size) >
540 ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
541 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
542 addr, lo, hi);
543 return -EINVAL;
544 }
545
d38ceaf9
AD
546 addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
547 addr += amdgpu_bo_gpu_offset(bo);
dc78330a 548 addr -= ((uint64_t)size) * ((uint64_t)index);
d38ceaf9 549
7270f839
CK
550 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
551 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
d38ceaf9
AD
552
553 return 0;
554}
555
f1689ec1
CK
556/**
557 * amdgpu_vce_validate_handle - validate stream handle
558 *
559 * @p: parser context
560 * @handle: handle to validate
2f4b9368 561 * @allocated: allocated a new handle?
f1689ec1
CK
562 *
563 * Validates the handle and return the found session index or -EINVAL
564 * we we don't have another free session index.
565 */
566static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
2f4b9368 567 uint32_t handle, bool *allocated)
f1689ec1
CK
568{
569 unsigned i;
570
2f4b9368
CK
571 *allocated = false;
572
f1689ec1
CK
573 /* validate the handle */
574 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
2f4b9368
CK
575 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
576 if (p->adev->vce.filp[i] != p->filp) {
577 DRM_ERROR("VCE handle collision detected!\n");
578 return -EINVAL;
579 }
f1689ec1 580 return i;
2f4b9368 581 }
f1689ec1
CK
582 }
583
584 /* handle not found try to alloc a new one */
585 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
586 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
587 p->adev->vce.filp[i] = p->filp;
588 p->adev->vce.img_size[i] = 0;
2f4b9368 589 *allocated = true;
f1689ec1
CK
590 return i;
591 }
592 }
593
594 DRM_ERROR("No more free VCE handles!\n");
595 return -EINVAL;
596}
597
d38ceaf9
AD
598/**
599 * amdgpu_vce_cs_parse - parse and validate the command stream
600 *
601 * @p: parser context
602 *
603 */
604int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
605{
50838c8c 606 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
dc78330a 607 unsigned fb_idx = 0, bs_idx = 0;
f1689ec1
CK
608 int session_idx = -1;
609 bool destroyed = false;
2f4b9368
CK
610 bool created = false;
611 bool allocated = false;
f1689ec1
CK
612 uint32_t tmp, handle = 0;
613 uint32_t *size = &tmp;
2f4b9368 614 int i, r = 0, idx = 0;
d38ceaf9
AD
615
616 amdgpu_vce_note_usage(p->adev);
617
618 while (idx < ib->length_dw) {
619 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
620 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
621
622 if ((len < 8) || (len & 3)) {
623 DRM_ERROR("invalid VCE command length (%d)!\n", len);
2f4b9368
CK
624 r = -EINVAL;
625 goto out;
d38ceaf9
AD
626 }
627
f1689ec1
CK
628 if (destroyed) {
629 DRM_ERROR("No other command allowed after destroy!\n");
2f4b9368
CK
630 r = -EINVAL;
631 goto out;
f1689ec1
CK
632 }
633
d38ceaf9
AD
634 switch (cmd) {
635 case 0x00000001: // session
636 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
2f4b9368
CK
637 session_idx = amdgpu_vce_validate_handle(p, handle,
638 &allocated);
f1689ec1
CK
639 if (session_idx < 0)
640 return session_idx;
641 size = &p->adev->vce.img_size[session_idx];
d38ceaf9
AD
642 break;
643
644 case 0x00000002: // task info
dc78330a
CK
645 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
646 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
f1689ec1
CK
647 break;
648
d38ceaf9 649 case 0x01000001: // create
2f4b9368
CK
650 created = true;
651 if (!allocated) {
652 DRM_ERROR("Handle already in use!\n");
653 r = -EINVAL;
654 goto out;
655 }
656
f1689ec1
CK
657 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
658 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
659 8 * 3 / 2;
660 break;
661
d38ceaf9
AD
662 case 0x04000001: // config extension
663 case 0x04000002: // pic control
664 case 0x04000005: // rate control
665 case 0x04000007: // motion estimation
666 case 0x04000008: // rdo
667 case 0x04000009: // vui
668 case 0x05000002: // auxiliary buffer
669 break;
670
671 case 0x03000001: // encode
f1689ec1 672 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
dc78330a 673 *size, 0);
d38ceaf9 674 if (r)
2f4b9368 675 goto out;
d38ceaf9 676
f1689ec1 677 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
dc78330a 678 *size / 3, 0);
d38ceaf9 679 if (r)
2f4b9368 680 goto out;
d38ceaf9
AD
681 break;
682
683 case 0x02000001: // destroy
f1689ec1 684 destroyed = true;
d38ceaf9
AD
685 break;
686
687 case 0x05000001: // context buffer
f1689ec1 688 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
dc78330a 689 *size * 2, 0);
f1689ec1 690 if (r)
2f4b9368 691 goto out;
f1689ec1
CK
692 break;
693
d38ceaf9 694 case 0x05000004: // video bitstream buffer
f1689ec1
CK
695 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
696 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
dc78330a 697 tmp, bs_idx);
f1689ec1 698 if (r)
2f4b9368 699 goto out;
f1689ec1
CK
700 break;
701
d38ceaf9 702 case 0x05000005: // feedback buffer
f1689ec1 703 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
dc78330a 704 4096, fb_idx);
d38ceaf9 705 if (r)
2f4b9368 706 goto out;
d38ceaf9
AD
707 break;
708
709 default:
710 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
2f4b9368
CK
711 r = -EINVAL;
712 goto out;
d38ceaf9
AD
713 }
714
f1689ec1
CK
715 if (session_idx == -1) {
716 DRM_ERROR("no session command at start of IB\n");
2f4b9368
CK
717 r = -EINVAL;
718 goto out;
f1689ec1
CK
719 }
720
d38ceaf9
AD
721 idx += len / 4;
722 }
723
2f4b9368
CK
724 if (allocated && !created) {
725 DRM_ERROR("New session without create command!\n");
726 r = -ENOENT;
727 }
728
729out:
730 if ((!r && destroyed) || (r && allocated)) {
731 /*
732 * IB contains a destroy msg or we have allocated an
733 * handle and got an error, anyway free the handle
734 */
d38ceaf9
AD
735 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
736 atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
d38ceaf9
AD
737 }
738
2f4b9368 739 return r;
d38ceaf9
AD
740}
741
d38ceaf9
AD
742/**
743 * amdgpu_vce_ring_emit_ib - execute indirect buffer
744 *
745 * @ring: engine to use
746 * @ib: the IB to execute
747 *
748 */
749void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
750{
751 amdgpu_ring_write(ring, VCE_CMD_IB);
752 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
753 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
754 amdgpu_ring_write(ring, ib->length_dw);
755}
756
757/**
758 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
759 *
760 * @ring: engine to use
761 * @fence: the fence
762 *
763 */
764void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
890ee23f 765 unsigned flags)
d38ceaf9 766{
890ee23f 767 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
d38ceaf9
AD
768
769 amdgpu_ring_write(ring, VCE_CMD_FENCE);
770 amdgpu_ring_write(ring, addr);
771 amdgpu_ring_write(ring, upper_32_bits(addr));
772 amdgpu_ring_write(ring, seq);
773 amdgpu_ring_write(ring, VCE_CMD_TRAP);
774 amdgpu_ring_write(ring, VCE_CMD_END);
775}
776
777/**
778 * amdgpu_vce_ring_test_ring - test if VCE ring is working
779 *
780 * @ring: the engine to test on
781 *
782 */
783int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
784{
785 struct amdgpu_device *adev = ring->adev;
786 uint32_t rptr = amdgpu_ring_get_rptr(ring);
787 unsigned i;
788 int r;
789
a27de35c 790 r = amdgpu_ring_alloc(ring, 16);
d38ceaf9
AD
791 if (r) {
792 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
793 ring->idx, r);
794 return r;
795 }
796 amdgpu_ring_write(ring, VCE_CMD_END);
a27de35c 797 amdgpu_ring_commit(ring);
d38ceaf9
AD
798
799 for (i = 0; i < adev->usec_timeout; i++) {
800 if (amdgpu_ring_get_rptr(ring) != rptr)
801 break;
802 DRM_UDELAY(1);
803 }
804
805 if (i < adev->usec_timeout) {
806 DRM_INFO("ring test on %d succeeded in %d usecs\n",
807 ring->idx, i);
808 } else {
809 DRM_ERROR("amdgpu: ring %d test failed\n",
810 ring->idx);
811 r = -ETIMEDOUT;
812 }
813
814 return r;
815}
816
817/**
818 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
819 *
820 * @ring: the engine to test on
821 *
822 */
823int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
824{
ed40bfb8 825 struct fence *fence = NULL;
d38ceaf9
AD
826 int r;
827
898e50d4
LL
828 /* skip vce ring1 ib test for now, since it's not reliable */
829 if (ring == &ring->adev->vce.ring[1])
830 return 0;
831
d38ceaf9
AD
832 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
833 if (r) {
834 DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
835 goto error;
836 }
837
838 r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
839 if (r) {
840 DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
841 goto error;
842 }
843
ed40bfb8 844 r = fence_wait(fence, false);
d38ceaf9
AD
845 if (r) {
846 DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
847 } else {
848 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
849 }
850error:
ed40bfb8 851 fence_put(fence);
d38ceaf9
AD
852 return r;
853}