blob: 05a192e95e5d4be403d8b84bc9c93753525366cd [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
110 bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 4) +
111 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
191 return 0;
192}
193
194void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
195{
196 rbo->placement.fpfn = 0 >> PAGE_SHIFT;
197 rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
198}
199
200void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
201{
202 int i, r;
203 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
204 if (rdev->uvd.filp[i] == filp) {
205 uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
206 struct radeon_fence *fence;
207
208 r = radeon_uvd_get_destroy_msg(rdev,
209 R600_RING_TYPE_UVD_INDEX, handle, &fence);
210 if (r) {
211 DRM_ERROR("Error destroying UVD (%d)!\n", r);
212 continue;
213 }
214
215 radeon_fence_wait(fence, false);
216 radeon_fence_unref(&fence);
217
218 rdev->uvd.filp[i] = NULL;
219 atomic_set(&rdev->uvd.handles[i], 0);
220 }
221 }
222}
223
224static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
225{
226 unsigned stream_type = msg[4];
227 unsigned width = msg[6];
228 unsigned height = msg[7];
229 unsigned dpb_size = msg[9];
230 unsigned pitch = msg[28];
231
232 unsigned width_in_mb = width / 16;
233 unsigned height_in_mb = ALIGN(height / 16, 2);
234
235 unsigned image_size, tmp, min_dpb_size;
236
237 image_size = width * height;
238 image_size += image_size / 2;
239 image_size = ALIGN(image_size, 1024);
240
241 switch (stream_type) {
242 case 0: /* H264 */
243
244 /* reference picture buffer */
245 min_dpb_size = image_size * 17;
246
247 /* macroblock context buffer */
248 min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
249
250 /* IT surface buffer */
251 min_dpb_size += width_in_mb * height_in_mb * 32;
252 break;
253
254 case 1: /* VC1 */
255
256 /* reference picture buffer */
257 min_dpb_size = image_size * 3;
258
259 /* CONTEXT_BUFFER */
260 min_dpb_size += width_in_mb * height_in_mb * 128;
261
262 /* IT surface buffer */
263 min_dpb_size += width_in_mb * 64;
264
265 /* DB surface buffer */
266 min_dpb_size += width_in_mb * 128;
267
268 /* BP */
269 tmp = max(width_in_mb, height_in_mb);
270 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
271 break;
272
273 case 3: /* MPEG2 */
274
275 /* reference picture buffer */
276 min_dpb_size = image_size * 3;
277 break;
278
279 case 4: /* MPEG4 */
280
281 /* reference picture buffer */
282 min_dpb_size = image_size * 3;
283
284 /* CM */
285 min_dpb_size += width_in_mb * height_in_mb * 64;
286
287 /* IT surface buffer */
288 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
289 break;
290
291 default:
292 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
293 return -EINVAL;
294 }
295
296 if (width > pitch) {
297 DRM_ERROR("Invalid UVD decoding target pitch!\n");
298 return -EINVAL;
299 }
300
301 if (dpb_size < min_dpb_size) {
302 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
303 dpb_size, min_dpb_size);
304 return -EINVAL;
305 }
306
307 buf_sizes[0x1] = dpb_size;
308 buf_sizes[0x2] = image_size;
309 return 0;
310}
311
312static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
313 unsigned offset, unsigned buf_sizes[])
314{
315 int32_t *msg, msg_type, handle;
316 void *ptr;
317
318 int i, r;
319
320 if (offset & 0x3F) {
321 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
322 return -EINVAL;
323 }
324
325 r = radeon_bo_kmap(bo, &ptr);
326 if (r)
327 return r;
328
329 msg = ptr + offset;
330
331 msg_type = msg[1];
332 handle = msg[2];
333
334 if (handle == 0) {
335 DRM_ERROR("Invalid UVD handle!\n");
336 return -EINVAL;
337 }
338
339 if (msg_type == 1) {
340 /* it's a decode msg, calc buffer sizes */
341 r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
342 radeon_bo_kunmap(bo);
343 if (r)
344 return r;
345
346 } else if (msg_type == 2) {
347 /* it's a destroy msg, free the handle */
348 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
349 atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
350 radeon_bo_kunmap(bo);
351 return 0;
352 } else {
353 /* it's a create msg, no special handling needed */
354 radeon_bo_kunmap(bo);
355 }
356
357 /* create or decode, validate the handle */
358 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
359 if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
360 return 0;
361 }
362
363 /* handle not found try to alloc a new one */
364 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
365 if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
366 p->rdev->uvd.filp[i] = p->filp;
367 return 0;
368 }
369 }
370
371 DRM_ERROR("No more free UVD handles!\n");
372 return -EINVAL;
373}
374
375static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
376 int data0, int data1,
377 unsigned buf_sizes[])
378{
379 struct radeon_cs_chunk *relocs_chunk;
380 struct radeon_cs_reloc *reloc;
381 unsigned idx, cmd, offset;
382 uint64_t start, end;
383 int r;
384
385 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
386 offset = radeon_get_ib_value(p, data0);
387 idx = radeon_get_ib_value(p, data1);
388 if (idx >= relocs_chunk->length_dw) {
389 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
390 idx, relocs_chunk->length_dw);
391 return -EINVAL;
392 }
393
394 reloc = p->relocs_ptr[(idx / 4)];
395 start = reloc->lobj.gpu_offset;
396 end = start + radeon_bo_size(reloc->robj);
397 start += offset;
398
399 p->ib.ptr[data0] = start & 0xFFFFFFFF;
400 p->ib.ptr[data1] = start >> 32;
401
402 cmd = radeon_get_ib_value(p, p->idx) >> 1;
403
404 if (cmd < 0x4) {
405 if ((end - start) < buf_sizes[cmd]) {
406 DRM_ERROR("buffer to small (%d / %d)!\n",
407 (unsigned)(end - start), buf_sizes[cmd]);
408 return -EINVAL;
409 }
410
411 } else if (cmd != 0x100) {
412 DRM_ERROR("invalid UVD command %X!\n", cmd);
413 return -EINVAL;
414 }
415
416 if (cmd == 0) {
417 if (end & 0xFFFFFFFFF0000000) {
418 DRM_ERROR("msg buffer %LX-%LX out of 256MB segment!\n",
419 start, end);
420 return -EINVAL;
421 }
422
423 r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
424 if (r)
425 return r;
426 }
427
428 if ((start & 0xFFFFFFFFF0000000) != (end & 0xFFFFFFFFF0000000)) {
429 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
430 start, end);
431 return -EINVAL;
432 }
433
434 return 0;
435}
436
437static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
438 struct radeon_cs_packet *pkt,
439 int *data0, int *data1,
440 unsigned buf_sizes[])
441{
442 int i, r;
443
444 p->idx++;
445 for (i = 0; i <= pkt->count; ++i) {
446 switch (pkt->reg + i*4) {
447 case UVD_GPCOM_VCPU_DATA0:
448 *data0 = p->idx;
449 break;
450 case UVD_GPCOM_VCPU_DATA1:
451 *data1 = p->idx;
452 break;
453 case UVD_GPCOM_VCPU_CMD:
454 r = radeon_uvd_cs_reloc(p, *data0, *data1, buf_sizes);
455 if (r)
456 return r;
457 break;
458 case UVD_ENGINE_CNTL:
459 break;
460 default:
461 DRM_ERROR("Invalid reg 0x%X!\n",
462 pkt->reg + i*4);
463 return -EINVAL;
464 }
465 p->idx++;
466 }
467 return 0;
468}
469
470int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
471{
472 struct radeon_cs_packet pkt;
473 int r, data0 = 0, data1 = 0;
474
475 /* minimum buffer sizes */
476 unsigned buf_sizes[] = {
477 [0x00000000] = 2048,
478 [0x00000001] = 32 * 1024 * 1024,
479 [0x00000002] = 2048 * 1152 * 3,
480 [0x00000003] = 2048,
481 };
482
483 if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
484 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
485 p->chunks[p->chunk_ib_idx].length_dw);
486 return -EINVAL;
487 }
488
489 if (p->chunk_relocs_idx == -1) {
490 DRM_ERROR("No relocation chunk !\n");
491 return -EINVAL;
492 }
493
494
495 do {
496 r = radeon_cs_packet_parse(p, &pkt, p->idx);
497 if (r)
498 return r;
499 switch (pkt.type) {
500 case RADEON_PACKET_TYPE0:
501 r = radeon_uvd_cs_reg(p, &pkt, &data0,
502 &data1, buf_sizes);
503 if (r)
504 return r;
505 break;
506 case RADEON_PACKET_TYPE2:
507 p->idx += pkt.count + 2;
508 break;
509 default:
510 DRM_ERROR("Unknown packet type %d !\n", pkt.type);
511 return -EINVAL;
512 }
513 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
514 return 0;
515}
516
517static int radeon_uvd_send_msg(struct radeon_device *rdev,
518 int ring, struct radeon_bo *bo,
519 struct radeon_fence **fence)
520{
521 struct ttm_validate_buffer tv;
522 struct list_head head;
523 struct radeon_ib ib;
524 uint64_t addr;
525 int i, r;
526
527 memset(&tv, 0, sizeof(tv));
528 tv.bo = &bo->tbo;
529
530 INIT_LIST_HEAD(&head);
531 list_add(&tv.head, &head);
532
533 r = ttm_eu_reserve_buffers(&head);
534 if (r)
535 return r;
536
537 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
538 radeon_uvd_force_into_uvd_segment(bo);
539
540 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
541 if (r) {
542 ttm_eu_backoff_reservation(&head);
543 return r;
544 }
545
546 r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
547 if (r) {
548 ttm_eu_backoff_reservation(&head);
549 return r;
550 }
551
552 addr = radeon_bo_gpu_offset(bo);
553 ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
554 ib.ptr[1] = addr;
555 ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
556 ib.ptr[3] = addr >> 32;
557 ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
558 ib.ptr[5] = 0;
559 for (i = 6; i < 16; ++i)
560 ib.ptr[i] = PACKET2(0);
561 ib.length_dw = 16;
562
563 r = radeon_ib_schedule(rdev, &ib, NULL);
564 if (r) {
565 ttm_eu_backoff_reservation(&head);
566 return r;
567 }
568 ttm_eu_fence_buffer_objects(&head, ib.fence);
569
570 if (fence)
571 *fence = radeon_fence_ref(ib.fence);
572
573 radeon_ib_free(rdev, &ib);
574 radeon_bo_unref(&bo);
575 return 0;
576}
577
578/* multiple fence commands without any stream commands in between can
579 crash the vcpu so just try to emmit a dummy create/destroy msg to
580 avoid this */
581int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
582 uint32_t handle, struct radeon_fence **fence)
583{
584 struct radeon_bo *bo;
585 uint32_t *msg;
586 int r, i;
587
588 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
589 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
590 if (r)
591 return r;
592
593 r = radeon_bo_reserve(bo, false);
594 if (r) {
595 radeon_bo_unref(&bo);
596 return r;
597 }
598
599 r = radeon_bo_kmap(bo, (void **)&msg);
600 if (r) {
601 radeon_bo_unreserve(bo);
602 radeon_bo_unref(&bo);
603 return r;
604 }
605
606 /* stitch together an UVD create msg */
607 msg[0] = 0x00000de4;
608 msg[1] = 0x00000000;
609 msg[2] = handle;
610 msg[3] = 0x00000000;
611 msg[4] = 0x00000000;
612 msg[5] = 0x00000000;
613 msg[6] = 0x00000000;
614 msg[7] = 0x00000780;
615 msg[8] = 0x00000440;
616 msg[9] = 0x00000000;
617 msg[10] = 0x01b37000;
618 for (i = 11; i < 1024; ++i)
619 msg[i] = 0x0;
620
621 radeon_bo_kunmap(bo);
622 radeon_bo_unreserve(bo);
623
624 return radeon_uvd_send_msg(rdev, ring, bo, fence);
625}
626
627int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
628 uint32_t handle, struct radeon_fence **fence)
629{
630 struct radeon_bo *bo;
631 uint32_t *msg;
632 int r, i;
633
634 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
635 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
636 if (r)
637 return r;
638
639 r = radeon_bo_reserve(bo, false);
640 if (r) {
641 radeon_bo_unref(&bo);
642 return r;
643 }
644
645 r = radeon_bo_kmap(bo, (void **)&msg);
646 if (r) {
647 radeon_bo_unreserve(bo);
648 radeon_bo_unref(&bo);
649 return r;
650 }
651
652 /* stitch together an UVD destroy msg */
653 msg[0] = 0x00000de4;
654 msg[1] = 0x00000002;
655 msg[2] = handle;
656 msg[3] = 0x00000000;
657 for (i = 4; i < 1024; ++i)
658 msg[i] = 0x0;
659
660 radeon_bo_kunmap(bo);
661 radeon_bo_unreserve(bo);
662
663 return radeon_uvd_send_msg(rdev, ring, bo, fence);
664}