blob: 15580fb8546efd4b78272f6149634cfe3a503b7f [file] [log] [blame]
Christian Königf2ba57b2013-04-08 12:41:29 +02001/*
2 * Copyright 2011 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 */
26/*
27 * Authors:
28 * Christian König <deathsimple@vodafone.de>
29 */
30
31#include <linux/firmware.h>
32#include <linux/module.h>
33#include <drm/drmP.h>
34#include <drm/drm.h>
35
36#include "radeon.h"
37#include "r600d.h"
38
39/* Firmware Names */
40#define FIRMWARE_RV710 "radeon/RV710_uvd.bin"
41#define FIRMWARE_CYPRESS "radeon/CYPRESS_uvd.bin"
42#define FIRMWARE_SUMO "radeon/SUMO_uvd.bin"
43#define FIRMWARE_TAHITI "radeon/TAHITI_uvd.bin"
44
45MODULE_FIRMWARE(FIRMWARE_RV710);
46MODULE_FIRMWARE(FIRMWARE_CYPRESS);
47MODULE_FIRMWARE(FIRMWARE_SUMO);
48MODULE_FIRMWARE(FIRMWARE_TAHITI);
49
50int radeon_uvd_init(struct radeon_device *rdev)
51{
52 struct platform_device *pdev;
53 unsigned long bo_size;
54 const char *fw_name;
55 int i, r;
56
57 pdev = platform_device_register_simple("radeon_uvd", 0, NULL, 0);
58 r = IS_ERR(pdev);
59 if (r) {
60 dev_err(rdev->dev, "radeon_uvd: Failed to register firmware\n");
61 return -EINVAL;
62 }
63
64 switch (rdev->family) {
65 case CHIP_RV710:
66 case CHIP_RV730:
67 case CHIP_RV740:
68 fw_name = FIRMWARE_RV710;
69 break;
70
71 case CHIP_CYPRESS:
72 case CHIP_HEMLOCK:
73 case CHIP_JUNIPER:
74 case CHIP_REDWOOD:
75 case CHIP_CEDAR:
76 fw_name = FIRMWARE_CYPRESS;
77 break;
78
79 case CHIP_SUMO:
80 case CHIP_SUMO2:
81 case CHIP_PALM:
82 case CHIP_CAYMAN:
83 case CHIP_BARTS:
84 case CHIP_TURKS:
85 case CHIP_CAICOS:
86 fw_name = FIRMWARE_SUMO;
87 break;
88
89 case CHIP_TAHITI:
90 case CHIP_VERDE:
91 case CHIP_PITCAIRN:
92 case CHIP_ARUBA:
93 fw_name = FIRMWARE_TAHITI;
94 break;
95
96 default:
97 return -EINVAL;
98 }
99
100 r = request_firmware(&rdev->uvd_fw, fw_name, &pdev->dev);
101 if (r) {
102 dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
103 fw_name);
104 platform_device_unregister(pdev);
105 return r;
106 }
107
108 platform_device_unregister(pdev);
109
Christian Königd7c605a2013-04-14 12:47:59 +0200110 bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
Christian Königf2ba57b2013-04-08 12:41:29 +0200111 RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE;
112 r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
113 RADEON_GEM_DOMAIN_VRAM, NULL, &rdev->uvd.vcpu_bo);
114 if (r) {
115 dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
116 return r;
117 }
118
119 r = radeon_uvd_resume(rdev);
120 if (r)
121 return r;
122
123 memset(rdev->uvd.cpu_addr, 0, bo_size);
124 memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
125
126 r = radeon_uvd_suspend(rdev);
127 if (r)
128 return r;
129
130 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
131 atomic_set(&rdev->uvd.handles[i], 0);
132 rdev->uvd.filp[i] = NULL;
133 }
134
135 return 0;
136}
137
138void radeon_uvd_fini(struct radeon_device *rdev)
139{
140 radeon_uvd_suspend(rdev);
141 radeon_bo_unref(&rdev->uvd.vcpu_bo);
142}
143
144int radeon_uvd_suspend(struct radeon_device *rdev)
145{
146 int r;
147
148 if (rdev->uvd.vcpu_bo == NULL)
149 return 0;
150
151 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
152 if (!r) {
153 radeon_bo_kunmap(rdev->uvd.vcpu_bo);
154 radeon_bo_unpin(rdev->uvd.vcpu_bo);
155 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
156 }
157 return r;
158}
159
160int radeon_uvd_resume(struct radeon_device *rdev)
161{
162 int r;
163
164 if (rdev->uvd.vcpu_bo == NULL)
165 return -EINVAL;
166
167 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
168 if (r) {
169 radeon_bo_unref(&rdev->uvd.vcpu_bo);
170 dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
171 return r;
172 }
173
174 r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
175 &rdev->uvd.gpu_addr);
176 if (r) {
177 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
178 radeon_bo_unref(&rdev->uvd.vcpu_bo);
179 dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
180 return r;
181 }
182
183 r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
184 if (r) {
185 dev_err(rdev->dev, "(%d) UVD map failed\n", r);
186 return r;
187 }
188
189 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
190
Christian Königec5891f2013-04-08 12:41:36 +0200191 radeon_set_uvd_clocks(rdev, 53300, 40000);
192
Christian Königf2ba57b2013-04-08 12:41:29 +0200193 return 0;
194}
195
196void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
197{
198 rbo->placement.fpfn = 0 >> PAGE_SHIFT;
199 rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
200}
201
202void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
203{
204 int i, r;
205 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
206 if (rdev->uvd.filp[i] == filp) {
207 uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
208 struct radeon_fence *fence;
209
210 r = radeon_uvd_get_destroy_msg(rdev,
211 R600_RING_TYPE_UVD_INDEX, handle, &fence);
212 if (r) {
213 DRM_ERROR("Error destroying UVD (%d)!\n", r);
214 continue;
215 }
216
217 radeon_fence_wait(fence, false);
218 radeon_fence_unref(&fence);
219
220 rdev->uvd.filp[i] = NULL;
221 atomic_set(&rdev->uvd.handles[i], 0);
222 }
223 }
224}
225
226static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
227{
228 unsigned stream_type = msg[4];
229 unsigned width = msg[6];
230 unsigned height = msg[7];
231 unsigned dpb_size = msg[9];
232 unsigned pitch = msg[28];
233
234 unsigned width_in_mb = width / 16;
235 unsigned height_in_mb = ALIGN(height / 16, 2);
236
237 unsigned image_size, tmp, min_dpb_size;
238
239 image_size = width * height;
240 image_size += image_size / 2;
241 image_size = ALIGN(image_size, 1024);
242
243 switch (stream_type) {
244 case 0: /* H264 */
245
246 /* reference picture buffer */
247 min_dpb_size = image_size * 17;
248
249 /* macroblock context buffer */
250 min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
251
252 /* IT surface buffer */
253 min_dpb_size += width_in_mb * height_in_mb * 32;
254 break;
255
256 case 1: /* VC1 */
257
258 /* reference picture buffer */
259 min_dpb_size = image_size * 3;
260
261 /* CONTEXT_BUFFER */
262 min_dpb_size += width_in_mb * height_in_mb * 128;
263
264 /* IT surface buffer */
265 min_dpb_size += width_in_mb * 64;
266
267 /* DB surface buffer */
268 min_dpb_size += width_in_mb * 128;
269
270 /* BP */
271 tmp = max(width_in_mb, height_in_mb);
272 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
273 break;
274
275 case 3: /* MPEG2 */
276
277 /* reference picture buffer */
278 min_dpb_size = image_size * 3;
279 break;
280
281 case 4: /* MPEG4 */
282
283 /* reference picture buffer */
284 min_dpb_size = image_size * 3;
285
286 /* CM */
287 min_dpb_size += width_in_mb * height_in_mb * 64;
288
289 /* IT surface buffer */
290 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
291 break;
292
293 default:
294 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
295 return -EINVAL;
296 }
297
298 if (width > pitch) {
299 DRM_ERROR("Invalid UVD decoding target pitch!\n");
300 return -EINVAL;
301 }
302
303 if (dpb_size < min_dpb_size) {
304 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
305 dpb_size, min_dpb_size);
306 return -EINVAL;
307 }
308
309 buf_sizes[0x1] = dpb_size;
310 buf_sizes[0x2] = image_size;
311 return 0;
312}
313
314static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
315 unsigned offset, unsigned buf_sizes[])
316{
317 int32_t *msg, msg_type, handle;
318 void *ptr;
319
320 int i, r;
321
322 if (offset & 0x3F) {
323 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
324 return -EINVAL;
325 }
326
327 r = radeon_bo_kmap(bo, &ptr);
328 if (r)
329 return r;
330
331 msg = ptr + offset;
332
333 msg_type = msg[1];
334 handle = msg[2];
335
336 if (handle == 0) {
337 DRM_ERROR("Invalid UVD handle!\n");
338 return -EINVAL;
339 }
340
341 if (msg_type == 1) {
342 /* it's a decode msg, calc buffer sizes */
343 r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
344 radeon_bo_kunmap(bo);
345 if (r)
346 return r;
347
348 } else if (msg_type == 2) {
349 /* it's a destroy msg, free the handle */
350 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
351 atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
352 radeon_bo_kunmap(bo);
353 return 0;
354 } else {
355 /* it's a create msg, no special handling needed */
356 radeon_bo_kunmap(bo);
357 }
358
359 /* create or decode, validate the handle */
360 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
361 if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
362 return 0;
363 }
364
365 /* handle not found try to alloc a new one */
366 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
367 if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
368 p->rdev->uvd.filp[i] = p->filp;
369 return 0;
370 }
371 }
372
373 DRM_ERROR("No more free UVD handles!\n");
374 return -EINVAL;
375}
376
377static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
378 int data0, int data1,
379 unsigned buf_sizes[])
380{
381 struct radeon_cs_chunk *relocs_chunk;
382 struct radeon_cs_reloc *reloc;
383 unsigned idx, cmd, offset;
384 uint64_t start, end;
385 int r;
386
387 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
388 offset = radeon_get_ib_value(p, data0);
389 idx = radeon_get_ib_value(p, data1);
390 if (idx >= relocs_chunk->length_dw) {
391 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
392 idx, relocs_chunk->length_dw);
393 return -EINVAL;
394 }
395
396 reloc = p->relocs_ptr[(idx / 4)];
397 start = reloc->lobj.gpu_offset;
398 end = start + radeon_bo_size(reloc->robj);
399 start += offset;
400
401 p->ib.ptr[data0] = start & 0xFFFFFFFF;
402 p->ib.ptr[data1] = start >> 32;
403
404 cmd = radeon_get_ib_value(p, p->idx) >> 1;
405
406 if (cmd < 0x4) {
407 if ((end - start) < buf_sizes[cmd]) {
408 DRM_ERROR("buffer to small (%d / %d)!\n",
409 (unsigned)(end - start), buf_sizes[cmd]);
410 return -EINVAL;
411 }
412
413 } else if (cmd != 0x100) {
414 DRM_ERROR("invalid UVD command %X!\n", cmd);
415 return -EINVAL;
416 }
417
Christian Königa92c7d52013-04-14 12:45:43 +0200418 if ((start >> 28) != (end >> 28)) {
Christian Königf2ba57b2013-04-08 12:41:29 +0200419 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
420 start, end);
421 return -EINVAL;
422 }
423
Christian Königa92c7d52013-04-14 12:45:43 +0200424 /* TODO: is this still necessary on NI+ ? */
425 if ((cmd == 0 || cmd == 0x3) &&
426 (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
427 DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
428 start, end);
429 return -EINVAL;
430 }
431
432 if (cmd == 0) {
433 r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
434 if (r)
435 return r;
436 }
437
Christian Königf2ba57b2013-04-08 12:41:29 +0200438 return 0;
439}
440
441static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
442 struct radeon_cs_packet *pkt,
443 int *data0, int *data1,
444 unsigned buf_sizes[])
445{
446 int i, r;
447
448 p->idx++;
449 for (i = 0; i <= pkt->count; ++i) {
450 switch (pkt->reg + i*4) {
451 case UVD_GPCOM_VCPU_DATA0:
452 *data0 = p->idx;
453 break;
454 case UVD_GPCOM_VCPU_DATA1:
455 *data1 = p->idx;
456 break;
457 case UVD_GPCOM_VCPU_CMD:
458 r = radeon_uvd_cs_reloc(p, *data0, *data1, buf_sizes);
459 if (r)
460 return r;
461 break;
462 case UVD_ENGINE_CNTL:
463 break;
464 default:
465 DRM_ERROR("Invalid reg 0x%X!\n",
466 pkt->reg + i*4);
467 return -EINVAL;
468 }
469 p->idx++;
470 }
471 return 0;
472}
473
474int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
475{
476 struct radeon_cs_packet pkt;
477 int r, data0 = 0, data1 = 0;
478
479 /* minimum buffer sizes */
480 unsigned buf_sizes[] = {
481 [0x00000000] = 2048,
482 [0x00000001] = 32 * 1024 * 1024,
483 [0x00000002] = 2048 * 1152 * 3,
484 [0x00000003] = 2048,
485 };
486
487 if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
488 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
489 p->chunks[p->chunk_ib_idx].length_dw);
490 return -EINVAL;
491 }
492
493 if (p->chunk_relocs_idx == -1) {
494 DRM_ERROR("No relocation chunk !\n");
495 return -EINVAL;
496 }
497
498
499 do {
500 r = radeon_cs_packet_parse(p, &pkt, p->idx);
501 if (r)
502 return r;
503 switch (pkt.type) {
504 case RADEON_PACKET_TYPE0:
505 r = radeon_uvd_cs_reg(p, &pkt, &data0,
506 &data1, buf_sizes);
507 if (r)
508 return r;
509 break;
510 case RADEON_PACKET_TYPE2:
511 p->idx += pkt.count + 2;
512 break;
513 default:
514 DRM_ERROR("Unknown packet type %d !\n", pkt.type);
515 return -EINVAL;
516 }
517 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
518 return 0;
519}
520
521static int radeon_uvd_send_msg(struct radeon_device *rdev,
522 int ring, struct radeon_bo *bo,
523 struct radeon_fence **fence)
524{
525 struct ttm_validate_buffer tv;
526 struct list_head head;
527 struct radeon_ib ib;
528 uint64_t addr;
529 int i, r;
530
531 memset(&tv, 0, sizeof(tv));
532 tv.bo = &bo->tbo;
533
534 INIT_LIST_HEAD(&head);
535 list_add(&tv.head, &head);
536
537 r = ttm_eu_reserve_buffers(&head);
538 if (r)
539 return r;
540
541 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
542 radeon_uvd_force_into_uvd_segment(bo);
543
544 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
545 if (r) {
546 ttm_eu_backoff_reservation(&head);
547 return r;
548 }
549
550 r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
551 if (r) {
552 ttm_eu_backoff_reservation(&head);
553 return r;
554 }
555
556 addr = radeon_bo_gpu_offset(bo);
557 ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
558 ib.ptr[1] = addr;
559 ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
560 ib.ptr[3] = addr >> 32;
561 ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
562 ib.ptr[5] = 0;
563 for (i = 6; i < 16; ++i)
564 ib.ptr[i] = PACKET2(0);
565 ib.length_dw = 16;
566
567 r = radeon_ib_schedule(rdev, &ib, NULL);
568 if (r) {
569 ttm_eu_backoff_reservation(&head);
570 return r;
571 }
572 ttm_eu_fence_buffer_objects(&head, ib.fence);
573
574 if (fence)
575 *fence = radeon_fence_ref(ib.fence);
576
577 radeon_ib_free(rdev, &ib);
578 radeon_bo_unref(&bo);
579 return 0;
580}
581
582/* multiple fence commands without any stream commands in between can
583 crash the vcpu so just try to emmit a dummy create/destroy msg to
584 avoid this */
585int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
586 uint32_t handle, struct radeon_fence **fence)
587{
588 struct radeon_bo *bo;
589 uint32_t *msg;
590 int r, i;
591
592 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
593 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
594 if (r)
595 return r;
596
597 r = radeon_bo_reserve(bo, false);
598 if (r) {
599 radeon_bo_unref(&bo);
600 return r;
601 }
602
603 r = radeon_bo_kmap(bo, (void **)&msg);
604 if (r) {
605 radeon_bo_unreserve(bo);
606 radeon_bo_unref(&bo);
607 return r;
608 }
609
610 /* stitch together an UVD create msg */
611 msg[0] = 0x00000de4;
612 msg[1] = 0x00000000;
613 msg[2] = handle;
614 msg[3] = 0x00000000;
615 msg[4] = 0x00000000;
616 msg[5] = 0x00000000;
617 msg[6] = 0x00000000;
618 msg[7] = 0x00000780;
619 msg[8] = 0x00000440;
620 msg[9] = 0x00000000;
621 msg[10] = 0x01b37000;
622 for (i = 11; i < 1024; ++i)
623 msg[i] = 0x0;
624
625 radeon_bo_kunmap(bo);
626 radeon_bo_unreserve(bo);
627
628 return radeon_uvd_send_msg(rdev, ring, bo, fence);
629}
630
631int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
632 uint32_t handle, struct radeon_fence **fence)
633{
634 struct radeon_bo *bo;
635 uint32_t *msg;
636 int r, i;
637
638 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
639 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
640 if (r)
641 return r;
642
643 r = radeon_bo_reserve(bo, false);
644 if (r) {
645 radeon_bo_unref(&bo);
646 return r;
647 }
648
649 r = radeon_bo_kmap(bo, (void **)&msg);
650 if (r) {
651 radeon_bo_unreserve(bo);
652 radeon_bo_unref(&bo);
653 return r;
654 }
655
656 /* stitch together an UVD destroy msg */
657 msg[0] = 0x00000de4;
658 msg[1] = 0x00000002;
659 msg[2] = handle;
660 msg[3] = 0x00000000;
661 for (i = 4; i < 1024; ++i)
662 msg[i] = 0x0;
663
664 radeon_bo_kunmap(bo);
665 radeon_bo_unreserve(bo);
666
667 return radeon_uvd_send_msg(rdev, ring, bo, fence);
668}