amdgpu_uvd.c revision 1.1 1 /* $NetBSD: amdgpu_uvd.c,v 1.1 2018/08/27 01:34:44 riastradh Exp $ */
2
3 /*
4 * Copyright 2011 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 *
27 */
28 /*
29 * Authors:
30 * Christian Knig <deathsimple (at) vodafone.de>
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: amdgpu_uvd.c,v 1.1 2018/08/27 01:34:44 riastradh Exp $");
35
36 #include <linux/firmware.h>
37 #include <linux/module.h>
38 #include <drm/drmP.h>
39 #include <drm/drm.h>
40
41 #include "amdgpu.h"
42 #include "amdgpu_pm.h"
43 #include "amdgpu_uvd.h"
44 #include "cikd.h"
45 #include "uvd/uvd_4_2_d.h"
46
47 /* 1 second timeout */
48 #define UVD_IDLE_TIMEOUT_MS 1000
49
50 /* Firmware Names */
51 #ifdef CONFIG_DRM_AMDGPU_CIK
52 #define FIRMWARE_BONAIRE "radeon/bonaire_uvd.bin"
53 #define FIRMWARE_KABINI "radeon/kabini_uvd.bin"
54 #define FIRMWARE_KAVERI "radeon/kaveri_uvd.bin"
55 #define FIRMWARE_HAWAII "radeon/hawaii_uvd.bin"
56 #define FIRMWARE_MULLINS "radeon/mullins_uvd.bin"
57 #endif
58 #define FIRMWARE_TONGA "amdgpu/tonga_uvd.bin"
59 #define FIRMWARE_CARRIZO "amdgpu/carrizo_uvd.bin"
60 #define FIRMWARE_FIJI "amdgpu/fiji_uvd.bin"
61 #define FIRMWARE_STONEY "amdgpu/stoney_uvd.bin"
62
63 /**
64 * amdgpu_uvd_cs_ctx - Command submission parser context
65 *
66 * Used for emulating virtual memory support on UVD 4.2.
67 */
68 struct amdgpu_uvd_cs_ctx {
69 struct amdgpu_cs_parser *parser;
70 unsigned reg, count;
71 unsigned data0, data1;
72 unsigned idx;
73 unsigned ib_idx;
74
75 /* does the IB has a msg command */
76 bool has_msg_cmd;
77
78 /* minimum buffer sizes */
79 unsigned *buf_sizes;
80 };
81
82 #ifdef CONFIG_DRM_AMDGPU_CIK
83 MODULE_FIRMWARE(FIRMWARE_BONAIRE);
84 MODULE_FIRMWARE(FIRMWARE_KABINI);
85 MODULE_FIRMWARE(FIRMWARE_KAVERI);
86 MODULE_FIRMWARE(FIRMWARE_HAWAII);
87 MODULE_FIRMWARE(FIRMWARE_MULLINS);
88 #endif
89 MODULE_FIRMWARE(FIRMWARE_TONGA);
90 MODULE_FIRMWARE(FIRMWARE_CARRIZO);
91 MODULE_FIRMWARE(FIRMWARE_FIJI);
92 MODULE_FIRMWARE(FIRMWARE_STONEY);
93
94 static void amdgpu_uvd_note_usage(struct amdgpu_device *adev);
95 static void amdgpu_uvd_idle_work_handler(struct work_struct *work);
96
97 int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
98 {
99 unsigned long bo_size;
100 const char *fw_name;
101 const struct common_firmware_header *hdr;
102 unsigned version_major, version_minor, family_id;
103 int i, r;
104
105 INIT_DELAYED_WORK(&adev->uvd.idle_work, amdgpu_uvd_idle_work_handler);
106
107 switch (adev->asic_type) {
108 #ifdef CONFIG_DRM_AMDGPU_CIK
109 case CHIP_BONAIRE:
110 fw_name = FIRMWARE_BONAIRE;
111 break;
112 case CHIP_KABINI:
113 fw_name = FIRMWARE_KABINI;
114 break;
115 case CHIP_KAVERI:
116 fw_name = FIRMWARE_KAVERI;
117 break;
118 case CHIP_HAWAII:
119 fw_name = FIRMWARE_HAWAII;
120 break;
121 case CHIP_MULLINS:
122 fw_name = FIRMWARE_MULLINS;
123 break;
124 #endif
125 case CHIP_TONGA:
126 fw_name = FIRMWARE_TONGA;
127 break;
128 case CHIP_FIJI:
129 fw_name = FIRMWARE_FIJI;
130 break;
131 case CHIP_CARRIZO:
132 fw_name = FIRMWARE_CARRIZO;
133 break;
134 case CHIP_STONEY:
135 fw_name = FIRMWARE_STONEY;
136 break;
137 default:
138 return -EINVAL;
139 }
140
141 r = request_firmware(&adev->uvd.fw, fw_name, adev->dev);
142 if (r) {
143 dev_err(adev->dev, "amdgpu_uvd: Can't load firmware \"%s\"\n",
144 fw_name);
145 return r;
146 }
147
148 r = amdgpu_ucode_validate(adev->uvd.fw);
149 if (r) {
150 dev_err(adev->dev, "amdgpu_uvd: Can't validate firmware \"%s\"\n",
151 fw_name);
152 release_firmware(adev->uvd.fw);
153 adev->uvd.fw = NULL;
154 return r;
155 }
156
157 hdr = (const struct common_firmware_header *)adev->uvd.fw->data;
158 family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
159 version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
160 version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
161 DRM_INFO("Found UVD firmware Version: %hu.%hu Family ID: %hu\n",
162 version_major, version_minor, family_id);
163
164 adev->uvd.fw_version = ((version_major << 24) | (version_minor << 16) |
165 (family_id << 8));
166
167 bo_size = AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8)
168 + AMDGPU_UVD_STACK_SIZE + AMDGPU_UVD_HEAP_SIZE;
169 r = amdgpu_bo_create(adev, bo_size, PAGE_SIZE, true,
170 AMDGPU_GEM_DOMAIN_VRAM,
171 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
172 NULL, NULL, &adev->uvd.vcpu_bo);
173 if (r) {
174 dev_err(adev->dev, "(%d) failed to allocate UVD bo\n", r);
175 return r;
176 }
177
178 r = amdgpu_bo_reserve(adev->uvd.vcpu_bo, false);
179 if (r) {
180 amdgpu_bo_unref(&adev->uvd.vcpu_bo);
181 dev_err(adev->dev, "(%d) failed to reserve UVD bo\n", r);
182 return r;
183 }
184
185 r = amdgpu_bo_pin(adev->uvd.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
186 &adev->uvd.gpu_addr);
187 if (r) {
188 amdgpu_bo_unreserve(adev->uvd.vcpu_bo);
189 amdgpu_bo_unref(&adev->uvd.vcpu_bo);
190 dev_err(adev->dev, "(%d) UVD bo pin failed\n", r);
191 return r;
192 }
193
194 r = amdgpu_bo_kmap(adev->uvd.vcpu_bo, &adev->uvd.cpu_addr);
195 if (r) {
196 dev_err(adev->dev, "(%d) UVD map failed\n", r);
197 return r;
198 }
199
200 amdgpu_bo_unreserve(adev->uvd.vcpu_bo);
201
202 for (i = 0; i < AMDGPU_MAX_UVD_HANDLES; ++i) {
203 atomic_set(&adev->uvd.handles[i], 0);
204 adev->uvd.filp[i] = NULL;
205 }
206
207 /* from uvd v5.0 HW addressing capacity increased to 64 bits */
208 if (!amdgpu_ip_block_version_cmp(adev, AMD_IP_BLOCK_TYPE_UVD, 5, 0))
209 adev->uvd.address_64_bit = true;
210
211 return 0;
212 }
213
214 int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
215 {
216 int r;
217
218 if (adev->uvd.vcpu_bo == NULL)
219 return 0;
220
221 r = amdgpu_bo_reserve(adev->uvd.vcpu_bo, false);
222 if (!r) {
223 amdgpu_bo_kunmap(adev->uvd.vcpu_bo);
224 amdgpu_bo_unpin(adev->uvd.vcpu_bo);
225 amdgpu_bo_unreserve(adev->uvd.vcpu_bo);
226 }
227
228 amdgpu_bo_unref(&adev->uvd.vcpu_bo);
229
230 amdgpu_ring_fini(&adev->uvd.ring);
231
232 release_firmware(adev->uvd.fw);
233
234 return 0;
235 }
236
237 int amdgpu_uvd_suspend(struct amdgpu_device *adev)
238 {
239 struct amdgpu_ring *ring = &adev->uvd.ring;
240 int i, r;
241
242 if (adev->uvd.vcpu_bo == NULL)
243 return 0;
244
245 for (i = 0; i < AMDGPU_MAX_UVD_HANDLES; ++i) {
246 uint32_t handle = atomic_read(&adev->uvd.handles[i]);
247 if (handle != 0) {
248 struct fence *fence;
249
250 amdgpu_uvd_note_usage(adev);
251
252 r = amdgpu_uvd_get_destroy_msg(ring, handle, &fence);
253 if (r) {
254 DRM_ERROR("Error destroying UVD (%d)!\n", r);
255 continue;
256 }
257
258 fence_wait(fence, false);
259 fence_put(fence);
260
261 adev->uvd.filp[i] = NULL;
262 atomic_set(&adev->uvd.handles[i], 0);
263 }
264 }
265
266 return 0;
267 }
268
269 int amdgpu_uvd_resume(struct amdgpu_device *adev)
270 {
271 unsigned size;
272 void *ptr;
273 const struct common_firmware_header *hdr;
274 unsigned offset;
275
276 if (adev->uvd.vcpu_bo == NULL)
277 return -EINVAL;
278
279 hdr = (const struct common_firmware_header *)adev->uvd.fw->data;
280 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
281 memcpy(adev->uvd.cpu_addr, (adev->uvd.fw->data) + offset,
282 (adev->uvd.fw->size) - offset);
283
284 cancel_delayed_work_sync(&adev->uvd.idle_work);
285
286 size = amdgpu_bo_size(adev->uvd.vcpu_bo);
287 size -= le32_to_cpu(hdr->ucode_size_bytes);
288 ptr = adev->uvd.cpu_addr;
289 ptr += le32_to_cpu(hdr->ucode_size_bytes);
290
291 memset(ptr, 0, size);
292
293 return 0;
294 }
295
296 void amdgpu_uvd_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
297 {
298 struct amdgpu_ring *ring = &adev->uvd.ring;
299 int i, r;
300
301 for (i = 0; i < AMDGPU_MAX_UVD_HANDLES; ++i) {
302 uint32_t handle = atomic_read(&adev->uvd.handles[i]);
303 if (handle != 0 && adev->uvd.filp[i] == filp) {
304 struct fence *fence;
305
306 amdgpu_uvd_note_usage(adev);
307
308 r = amdgpu_uvd_get_destroy_msg(ring, handle, &fence);
309 if (r) {
310 DRM_ERROR("Error destroying UVD (%d)!\n", r);
311 continue;
312 }
313
314 fence_wait(fence, false);
315 fence_put(fence);
316
317 adev->uvd.filp[i] = NULL;
318 atomic_set(&adev->uvd.handles[i], 0);
319 }
320 }
321 }
322
323 static void amdgpu_uvd_force_into_uvd_segment(struct amdgpu_bo *rbo)
324 {
325 int i;
326 for (i = 0; i < rbo->placement.num_placement; ++i) {
327 rbo->placements[i].fpfn = 0 >> PAGE_SHIFT;
328 rbo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
329 }
330 }
331
332 /**
333 * amdgpu_uvd_cs_pass1 - first parsing round
334 *
335 * @ctx: UVD parser context
336 *
337 * Make sure UVD message and feedback buffers are in VRAM and
338 * nobody is violating an 256MB boundary.
339 */
340 static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx)
341 {
342 struct amdgpu_bo_va_mapping *mapping;
343 struct amdgpu_bo *bo;
344 uint32_t cmd, lo, hi;
345 uint64_t addr;
346 int r = 0;
347
348 lo = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data0);
349 hi = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data1);
350 addr = ((uint64_t)lo) | (((uint64_t)hi) << 32);
351
352 mapping = amdgpu_cs_find_mapping(ctx->parser, addr, &bo);
353 if (mapping == NULL) {
354 DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
355 return -EINVAL;
356 }
357
358 if (!ctx->parser->adev->uvd.address_64_bit) {
359 /* check if it's a message or feedback command */
360 cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
361 if (cmd == 0x0 || cmd == 0x3) {
362 /* yes, force it into VRAM */
363 uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
364 amdgpu_ttm_placement_from_domain(bo, domain);
365 }
366 amdgpu_uvd_force_into_uvd_segment(bo);
367
368 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
369 }
370
371 return r;
372 }
373
374 /**
375 * amdgpu_uvd_cs_msg_decode - handle UVD decode message
376 *
377 * @msg: pointer to message structure
378 * @buf_sizes: returned buffer sizes
379 *
380 * Peek into the decode message and calculate the necessary buffer sizes.
381 */
382 static int amdgpu_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
383 {
384 unsigned stream_type = msg[4];
385 unsigned width = msg[6];
386 unsigned height = msg[7];
387 unsigned dpb_size = msg[9];
388 unsigned pitch = msg[28];
389 unsigned level = msg[57];
390
391 unsigned width_in_mb = width / 16;
392 unsigned height_in_mb = ALIGN(height / 16, 2);
393 unsigned fs_in_mb = width_in_mb * height_in_mb;
394
395 unsigned image_size, tmp, min_dpb_size, num_dpb_buffer;
396 unsigned min_ctx_size = 0;
397
398 image_size = width * height;
399 image_size += image_size / 2;
400 image_size = ALIGN(image_size, 1024);
401
402 switch (stream_type) {
403 case 0: /* H264 */
404 case 7: /* H264 Perf */
405 switch(level) {
406 case 30:
407 num_dpb_buffer = 8100 / fs_in_mb;
408 break;
409 case 31:
410 num_dpb_buffer = 18000 / fs_in_mb;
411 break;
412 case 32:
413 num_dpb_buffer = 20480 / fs_in_mb;
414 break;
415 case 41:
416 num_dpb_buffer = 32768 / fs_in_mb;
417 break;
418 case 42:
419 num_dpb_buffer = 34816 / fs_in_mb;
420 break;
421 case 50:
422 num_dpb_buffer = 110400 / fs_in_mb;
423 break;
424 case 51:
425 num_dpb_buffer = 184320 / fs_in_mb;
426 break;
427 default:
428 num_dpb_buffer = 184320 / fs_in_mb;
429 break;
430 }
431 num_dpb_buffer++;
432 if (num_dpb_buffer > 17)
433 num_dpb_buffer = 17;
434
435 /* reference picture buffer */
436 min_dpb_size = image_size * num_dpb_buffer;
437
438 /* macroblock context buffer */
439 min_dpb_size += width_in_mb * height_in_mb * num_dpb_buffer * 192;
440
441 /* IT surface buffer */
442 min_dpb_size += width_in_mb * height_in_mb * 32;
443 break;
444
445 case 1: /* VC1 */
446
447 /* reference picture buffer */
448 min_dpb_size = image_size * 3;
449
450 /* CONTEXT_BUFFER */
451 min_dpb_size += width_in_mb * height_in_mb * 128;
452
453 /* IT surface buffer */
454 min_dpb_size += width_in_mb * 64;
455
456 /* DB surface buffer */
457 min_dpb_size += width_in_mb * 128;
458
459 /* BP */
460 tmp = max(width_in_mb, height_in_mb);
461 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
462 break;
463
464 case 3: /* MPEG2 */
465
466 /* reference picture buffer */
467 min_dpb_size = image_size * 3;
468 break;
469
470 case 4: /* MPEG4 */
471
472 /* reference picture buffer */
473 min_dpb_size = image_size * 3;
474
475 /* CM */
476 min_dpb_size += width_in_mb * height_in_mb * 64;
477
478 /* IT surface buffer */
479 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
480 break;
481
482 case 16: /* H265 */
483 image_size = (ALIGN(width, 16) * ALIGN(height, 16) * 3) / 2;
484 image_size = ALIGN(image_size, 256);
485
486 num_dpb_buffer = (le32_to_cpu(msg[59]) & 0xff) + 2;
487 min_dpb_size = image_size * num_dpb_buffer;
488 min_ctx_size = ((width + 255) / 16) * ((height + 255) / 16)
489 * 16 * num_dpb_buffer + 52 * 1024;
490 break;
491
492 default:
493 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
494 return -EINVAL;
495 }
496
497 if (width > pitch) {
498 DRM_ERROR("Invalid UVD decoding target pitch!\n");
499 return -EINVAL;
500 }
501
502 if (dpb_size < min_dpb_size) {
503 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
504 dpb_size, min_dpb_size);
505 return -EINVAL;
506 }
507
508 buf_sizes[0x1] = dpb_size;
509 buf_sizes[0x2] = image_size;
510 buf_sizes[0x4] = min_ctx_size;
511 return 0;
512 }
513
514 /**
515 * amdgpu_uvd_cs_msg - handle UVD message
516 *
517 * @ctx: UVD parser context
518 * @bo: buffer object containing the message
519 * @offset: offset into the buffer object
520 *
521 * Peek into the UVD message and extract the session id.
522 * Make sure that we don't open up to many sessions.
523 */
524 static int amdgpu_uvd_cs_msg(struct amdgpu_uvd_cs_ctx *ctx,
525 struct amdgpu_bo *bo, unsigned offset)
526 {
527 struct amdgpu_device *adev = ctx->parser->adev;
528 int32_t *msg, msg_type, handle;
529 void *ptr;
530 long r;
531 int i;
532
533 if (offset & 0x3F) {
534 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
535 return -EINVAL;
536 }
537
538 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, true, false,
539 MAX_SCHEDULE_TIMEOUT);
540 if (r < 0) {
541 DRM_ERROR("Failed waiting for UVD message (%ld)!\n", r);
542 return r;
543 }
544
545 r = amdgpu_bo_kmap(bo, &ptr);
546 if (r) {
547 DRM_ERROR("Failed mapping the UVD message (%ld)!\n", r);
548 return r;
549 }
550
551 msg = ptr + offset;
552
553 msg_type = msg[1];
554 handle = msg[2];
555
556 if (handle == 0) {
557 DRM_ERROR("Invalid UVD handle!\n");
558 return -EINVAL;
559 }
560
561 switch (msg_type) {
562 case 0:
563 /* it's a create msg, calc image size (width * height) */
564 amdgpu_bo_kunmap(bo);
565
566 /* try to alloc a new handle */
567 for (i = 0; i < AMDGPU_MAX_UVD_HANDLES; ++i) {
568 if (atomic_read(&adev->uvd.handles[i]) == handle) {
569 DRM_ERROR("Handle 0x%x already in use!\n", handle);
570 return -EINVAL;
571 }
572
573 if (!atomic_cmpxchg(&adev->uvd.handles[i], 0, handle)) {
574 adev->uvd.filp[i] = ctx->parser->filp;
575 return 0;
576 }
577 }
578
579 DRM_ERROR("No more free UVD handles!\n");
580 return -EINVAL;
581
582 case 1:
583 /* it's a decode msg, calc buffer sizes */
584 r = amdgpu_uvd_cs_msg_decode(msg, ctx->buf_sizes);
585 amdgpu_bo_kunmap(bo);
586 if (r)
587 return r;
588
589 /* validate the handle */
590 for (i = 0; i < AMDGPU_MAX_UVD_HANDLES; ++i) {
591 if (atomic_read(&adev->uvd.handles[i]) == handle) {
592 if (adev->uvd.filp[i] != ctx->parser->filp) {
593 DRM_ERROR("UVD handle collision detected!\n");
594 return -EINVAL;
595 }
596 return 0;
597 }
598 }
599
600 DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
601 return -ENOENT;
602
603 case 2:
604 /* it's a destroy msg, free the handle */
605 for (i = 0; i < AMDGPU_MAX_UVD_HANDLES; ++i)
606 atomic_cmpxchg(&adev->uvd.handles[i], handle, 0);
607 amdgpu_bo_kunmap(bo);
608 return 0;
609
610 default:
611 DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
612 return -EINVAL;
613 }
614 BUG();
615 return -EINVAL;
616 }
617
618 /**
619 * amdgpu_uvd_cs_pass2 - second parsing round
620 *
621 * @ctx: UVD parser context
622 *
623 * Patch buffer addresses, make sure buffer sizes are correct.
624 */
625 static int amdgpu_uvd_cs_pass2(struct amdgpu_uvd_cs_ctx *ctx)
626 {
627 struct amdgpu_bo_va_mapping *mapping;
628 struct amdgpu_bo *bo;
629 struct amdgpu_ib *ib;
630 uint32_t cmd, lo, hi;
631 uint64_t start, end;
632 uint64_t addr;
633 int r;
634
635 lo = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data0);
636 hi = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data1);
637 addr = ((uint64_t)lo) | (((uint64_t)hi) << 32);
638
639 mapping = amdgpu_cs_find_mapping(ctx->parser, addr, &bo);
640 if (mapping == NULL)
641 return -EINVAL;
642
643 start = amdgpu_bo_gpu_offset(bo);
644
645 end = (mapping->it.last + 1 - mapping->it.start);
646 end = end * AMDGPU_GPU_PAGE_SIZE + start;
647
648 addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
649 start += addr;
650
651 ib = &ctx->parser->ibs[ctx->ib_idx];
652 ib->ptr[ctx->data0] = start & 0xFFFFFFFF;
653 ib->ptr[ctx->data1] = start >> 32;
654
655 cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
656 if (cmd < 0x4) {
657 if ((end - start) < ctx->buf_sizes[cmd]) {
658 DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
659 (unsigned)(end - start),
660 ctx->buf_sizes[cmd]);
661 return -EINVAL;
662 }
663
664 } else if (cmd == 0x206) {
665 if ((end - start) < ctx->buf_sizes[4]) {
666 DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
667 (unsigned)(end - start),
668 ctx->buf_sizes[4]);
669 return -EINVAL;
670 }
671 } else if ((cmd != 0x100) && (cmd != 0x204)) {
672 DRM_ERROR("invalid UVD command %X!\n", cmd);
673 return -EINVAL;
674 }
675
676 if (!ctx->parser->adev->uvd.address_64_bit) {
677 if ((start >> 28) != ((end - 1) >> 28)) {
678 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
679 start, end);
680 return -EINVAL;
681 }
682
683 if ((cmd == 0 || cmd == 0x3) &&
684 (start >> 28) != (ctx->parser->adev->uvd.gpu_addr >> 28)) {
685 DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
686 start, end);
687 return -EINVAL;
688 }
689 }
690
691 if (cmd == 0) {
692 ctx->has_msg_cmd = true;
693 r = amdgpu_uvd_cs_msg(ctx, bo, addr);
694 if (r)
695 return r;
696 } else if (!ctx->has_msg_cmd) {
697 DRM_ERROR("Message needed before other commands are send!\n");
698 return -EINVAL;
699 }
700
701 return 0;
702 }
703
704 /**
705 * amdgpu_uvd_cs_reg - parse register writes
706 *
707 * @ctx: UVD parser context
708 * @cb: callback function
709 *
710 * Parse the register writes, call cb on each complete command.
711 */
712 static int amdgpu_uvd_cs_reg(struct amdgpu_uvd_cs_ctx *ctx,
713 int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
714 {
715 struct amdgpu_ib *ib = &ctx->parser->ibs[ctx->ib_idx];
716 int i, r;
717
718 ctx->idx++;
719 for (i = 0; i <= ctx->count; ++i) {
720 unsigned reg = ctx->reg + i;
721
722 if (ctx->idx >= ib->length_dw) {
723 DRM_ERROR("Register command after end of CS!\n");
724 return -EINVAL;
725 }
726
727 switch (reg) {
728 case mmUVD_GPCOM_VCPU_DATA0:
729 ctx->data0 = ctx->idx;
730 break;
731 case mmUVD_GPCOM_VCPU_DATA1:
732 ctx->data1 = ctx->idx;
733 break;
734 case mmUVD_GPCOM_VCPU_CMD:
735 r = cb(ctx);
736 if (r)
737 return r;
738 break;
739 case mmUVD_ENGINE_CNTL:
740 break;
741 default:
742 DRM_ERROR("Invalid reg 0x%X!\n", reg);
743 return -EINVAL;
744 }
745 ctx->idx++;
746 }
747 return 0;
748 }
749
750 /**
751 * amdgpu_uvd_cs_packets - parse UVD packets
752 *
753 * @ctx: UVD parser context
754 * @cb: callback function
755 *
756 * Parse the command stream packets.
757 */
758 static int amdgpu_uvd_cs_packets(struct amdgpu_uvd_cs_ctx *ctx,
759 int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
760 {
761 struct amdgpu_ib *ib = &ctx->parser->ibs[ctx->ib_idx];
762 int r;
763
764 for (ctx->idx = 0 ; ctx->idx < ib->length_dw; ) {
765 uint32_t cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx);
766 unsigned type = CP_PACKET_GET_TYPE(cmd);
767 switch (type) {
768 case PACKET_TYPE0:
769 ctx->reg = CP_PACKET0_GET_REG(cmd);
770 ctx->count = CP_PACKET_GET_COUNT(cmd);
771 r = amdgpu_uvd_cs_reg(ctx, cb);
772 if (r)
773 return r;
774 break;
775 case PACKET_TYPE2:
776 ++ctx->idx;
777 break;
778 default:
779 DRM_ERROR("Unknown packet type %d !\n", type);
780 return -EINVAL;
781 }
782 }
783 return 0;
784 }
785
786 /**
787 * amdgpu_uvd_ring_parse_cs - UVD command submission parser
788 *
789 * @parser: Command submission parser context
790 *
791 * Parse the command stream, patch in addresses as necessary.
792 */
793 int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser, uint32_t ib_idx)
794 {
795 struct amdgpu_uvd_cs_ctx ctx = {};
796 unsigned buf_sizes[] = {
797 [0x00000000] = 2048,
798 [0x00000001] = 0xFFFFFFFF,
799 [0x00000002] = 0xFFFFFFFF,
800 [0x00000003] = 2048,
801 [0x00000004] = 0xFFFFFFFF,
802 };
803 struct amdgpu_ib *ib = &parser->ibs[ib_idx];
804 int r;
805
806 if (ib->length_dw % 16) {
807 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
808 ib->length_dw);
809 return -EINVAL;
810 }
811
812 ctx.parser = parser;
813 ctx.buf_sizes = buf_sizes;
814 ctx.ib_idx = ib_idx;
815
816 /* first round, make sure the buffers are actually in the UVD segment */
817 r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass1);
818 if (r)
819 return r;
820
821 /* second round, patch buffer addresses into the command stream */
822 r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass2);
823 if (r)
824 return r;
825
826 if (!ctx.has_msg_cmd) {
827 DRM_ERROR("UVD-IBs need a msg command!\n");
828 return -EINVAL;
829 }
830
831 amdgpu_uvd_note_usage(ctx.parser->adev);
832
833 return 0;
834 }
835
836 static int amdgpu_uvd_free_job(
837 struct amdgpu_job *job)
838 {
839 amdgpu_ib_free(job->adev, job->ibs);
840 kfree(job->ibs);
841 return 0;
842 }
843
844 static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring,
845 struct amdgpu_bo *bo,
846 struct fence **fence)
847 {
848 struct ttm_validate_buffer tv;
849 struct ww_acquire_ctx ticket;
850 struct list_head head;
851 struct amdgpu_ib *ib = NULL;
852 struct fence *f = NULL;
853 struct amdgpu_device *adev = ring->adev;
854 uint64_t addr;
855 int i, r;
856
857 memset(&tv, 0, sizeof(tv));
858 tv.bo = &bo->tbo;
859
860 INIT_LIST_HEAD(&head);
861 list_add(&tv.head, &head);
862
863 r = ttm_eu_reserve_buffers(&ticket, &head, true, NULL);
864 if (r)
865 return r;
866
867 if (!bo->adev->uvd.address_64_bit) {
868 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
869 amdgpu_uvd_force_into_uvd_segment(bo);
870 }
871
872 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
873 if (r)
874 goto err;
875 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
876 if (!ib) {
877 r = -ENOMEM;
878 goto err;
879 }
880 r = amdgpu_ib_get(ring, NULL, 64, ib);
881 if (r)
882 goto err1;
883
884 addr = amdgpu_bo_gpu_offset(bo);
885 ib->ptr[0] = PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0);
886 ib->ptr[1] = addr;
887 ib->ptr[2] = PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0);
888 ib->ptr[3] = addr >> 32;
889 ib->ptr[4] = PACKET0(mmUVD_GPCOM_VCPU_CMD, 0);
890 ib->ptr[5] = 0;
891 for (i = 6; i < 16; ++i)
892 ib->ptr[i] = PACKET2(0);
893 ib->length_dw = 16;
894
895 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
896 &amdgpu_uvd_free_job,
897 AMDGPU_FENCE_OWNER_UNDEFINED,
898 &f);
899 if (r)
900 goto err2;
901
902 ttm_eu_fence_buffer_objects(&ticket, &head, f);
903
904 if (fence)
905 *fence = fence_get(f);
906 amdgpu_bo_unref(&bo);
907 fence_put(f);
908 if (amdgpu_enable_scheduler)
909 return 0;
910
911 amdgpu_ib_free(ring->adev, ib);
912 kfree(ib);
913 return 0;
914 err2:
915 amdgpu_ib_free(ring->adev, ib);
916 err1:
917 kfree(ib);
918 err:
919 ttm_eu_backoff_reservation(&ticket, &head);
920 return r;
921 }
922
923 /* multiple fence commands without any stream commands in between can
924 crash the vcpu so just try to emmit a dummy create/destroy msg to
925 avoid this */
926 int amdgpu_uvd_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
927 struct fence **fence)
928 {
929 struct amdgpu_device *adev = ring->adev;
930 struct amdgpu_bo *bo;
931 uint32_t *msg;
932 int r, i;
933
934 r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
935 AMDGPU_GEM_DOMAIN_VRAM,
936 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
937 NULL, NULL, &bo);
938 if (r)
939 return r;
940
941 r = amdgpu_bo_reserve(bo, false);
942 if (r) {
943 amdgpu_bo_unref(&bo);
944 return r;
945 }
946
947 r = amdgpu_bo_kmap(bo, (void **)&msg);
948 if (r) {
949 amdgpu_bo_unreserve(bo);
950 amdgpu_bo_unref(&bo);
951 return r;
952 }
953
954 /* stitch together an UVD create msg */
955 msg[0] = cpu_to_le32(0x00000de4);
956 msg[1] = cpu_to_le32(0x00000000);
957 msg[2] = cpu_to_le32(handle);
958 msg[3] = cpu_to_le32(0x00000000);
959 msg[4] = cpu_to_le32(0x00000000);
960 msg[5] = cpu_to_le32(0x00000000);
961 msg[6] = cpu_to_le32(0x00000000);
962 msg[7] = cpu_to_le32(0x00000780);
963 msg[8] = cpu_to_le32(0x00000440);
964 msg[9] = cpu_to_le32(0x00000000);
965 msg[10] = cpu_to_le32(0x01b37000);
966 for (i = 11; i < 1024; ++i)
967 msg[i] = cpu_to_le32(0x0);
968
969 amdgpu_bo_kunmap(bo);
970 amdgpu_bo_unreserve(bo);
971
972 return amdgpu_uvd_send_msg(ring, bo, fence);
973 }
974
975 int amdgpu_uvd_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
976 struct fence **fence)
977 {
978 struct amdgpu_device *adev = ring->adev;
979 struct amdgpu_bo *bo;
980 uint32_t *msg;
981 int r, i;
982
983 r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
984 AMDGPU_GEM_DOMAIN_VRAM,
985 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
986 NULL, NULL, &bo);
987 if (r)
988 return r;
989
990 r = amdgpu_bo_reserve(bo, false);
991 if (r) {
992 amdgpu_bo_unref(&bo);
993 return r;
994 }
995
996 r = amdgpu_bo_kmap(bo, (void **)&msg);
997 if (r) {
998 amdgpu_bo_unreserve(bo);
999 amdgpu_bo_unref(&bo);
1000 return r;
1001 }
1002
1003 /* stitch together an UVD destroy msg */
1004 msg[0] = cpu_to_le32(0x00000de4);
1005 msg[1] = cpu_to_le32(0x00000002);
1006 msg[2] = cpu_to_le32(handle);
1007 msg[3] = cpu_to_le32(0x00000000);
1008 for (i = 4; i < 1024; ++i)
1009 msg[i] = cpu_to_le32(0x0);
1010
1011 amdgpu_bo_kunmap(bo);
1012 amdgpu_bo_unreserve(bo);
1013
1014 return amdgpu_uvd_send_msg(ring, bo, fence);
1015 }
1016
1017 static void amdgpu_uvd_idle_work_handler(struct work_struct *work)
1018 {
1019 struct amdgpu_device *adev =
1020 container_of(work, struct amdgpu_device, uvd.idle_work.work);
1021 unsigned i, fences, handles = 0;
1022
1023 fences = amdgpu_fence_count_emitted(&adev->uvd.ring);
1024
1025 for (i = 0; i < AMDGPU_MAX_UVD_HANDLES; ++i)
1026 if (atomic_read(&adev->uvd.handles[i]))
1027 ++handles;
1028
1029 if (fences == 0 && handles == 0) {
1030 if (adev->pm.dpm_enabled) {
1031 amdgpu_dpm_enable_uvd(adev, false);
1032 } else {
1033 amdgpu_asic_set_uvd_clocks(adev, 0, 0);
1034 }
1035 } else {
1036 schedule_delayed_work(&adev->uvd.idle_work,
1037 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
1038 }
1039 }
1040
1041 static void amdgpu_uvd_note_usage(struct amdgpu_device *adev)
1042 {
1043 bool set_clocks = !cancel_delayed_work_sync(&adev->uvd.idle_work);
1044 set_clocks &= schedule_delayed_work(&adev->uvd.idle_work,
1045 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
1046
1047 if (set_clocks) {
1048 if (adev->pm.dpm_enabled) {
1049 amdgpu_dpm_enable_uvd(adev, true);
1050 } else {
1051 amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
1052 }
1053 }
1054 }
1055