blob: 5715429279fb8562f84f777248d65498bebf26b5 [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
Christian König55b51c82013-04-18 15:25:59 +020039/* 1 second timeout */
40#define UVD_IDLE_TIMEOUT_MS 1000
41
Christian Königf2ba57b2013-04-08 12:41:29 +020042/* Firmware Names */
43#define FIRMWARE_RV710 "radeon/RV710_uvd.bin"
44#define FIRMWARE_CYPRESS "radeon/CYPRESS_uvd.bin"
45#define FIRMWARE_SUMO "radeon/SUMO_uvd.bin"
46#define FIRMWARE_TAHITI "radeon/TAHITI_uvd.bin"
47
48MODULE_FIRMWARE(FIRMWARE_RV710);
49MODULE_FIRMWARE(FIRMWARE_CYPRESS);
50MODULE_FIRMWARE(FIRMWARE_SUMO);
51MODULE_FIRMWARE(FIRMWARE_TAHITI);
52
Christian König55b51c82013-04-18 15:25:59 +020053static void radeon_uvd_idle_work_handler(struct work_struct *work);
54
Christian Königf2ba57b2013-04-08 12:41:29 +020055int radeon_uvd_init(struct radeon_device *rdev)
56{
57 struct platform_device *pdev;
58 unsigned long bo_size;
59 const char *fw_name;
60 int i, r;
61
Christian König55b51c82013-04-18 15:25:59 +020062 INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
63
Christian Königf2ba57b2013-04-08 12:41:29 +020064 pdev = platform_device_register_simple("radeon_uvd", 0, NULL, 0);
65 r = IS_ERR(pdev);
66 if (r) {
67 dev_err(rdev->dev, "radeon_uvd: Failed to register firmware\n");
68 return -EINVAL;
69 }
70
71 switch (rdev->family) {
72 case CHIP_RV710:
73 case CHIP_RV730:
74 case CHIP_RV740:
75 fw_name = FIRMWARE_RV710;
76 break;
77
78 case CHIP_CYPRESS:
79 case CHIP_HEMLOCK:
80 case CHIP_JUNIPER:
81 case CHIP_REDWOOD:
82 case CHIP_CEDAR:
83 fw_name = FIRMWARE_CYPRESS;
84 break;
85
86 case CHIP_SUMO:
87 case CHIP_SUMO2:
88 case CHIP_PALM:
89 case CHIP_CAYMAN:
90 case CHIP_BARTS:
91 case CHIP_TURKS:
92 case CHIP_CAICOS:
93 fw_name = FIRMWARE_SUMO;
94 break;
95
96 case CHIP_TAHITI:
97 case CHIP_VERDE:
98 case CHIP_PITCAIRN:
99 case CHIP_ARUBA:
100 fw_name = FIRMWARE_TAHITI;
101 break;
102
103 default:
104 return -EINVAL;
105 }
106
Christian König23b6ec02013-08-05 14:10:55 +0200107 r = request_firmware(&rdev->uvd_fw, fw_name, &pdev->dev);
Christian Königf2ba57b2013-04-08 12:41:29 +0200108 if (r) {
109 dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
110 fw_name);
111 platform_device_unregister(pdev);
112 return r;
113 }
114
115 platform_device_unregister(pdev);
116
Christian König23b6ec02013-08-05 14:10:55 +0200117 bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
Christian Königf2ba57b2013-04-08 12:41:29 +0200118 RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE;
119 r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
120 RADEON_GEM_DOMAIN_VRAM, NULL, &rdev->uvd.vcpu_bo);
121 if (r) {
122 dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
123 return r;
124 }
125
Christian Königf2ba57b2013-04-08 12:41:29 +0200126 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
127 if (r) {
128 radeon_bo_unref(&rdev->uvd.vcpu_bo);
129 dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
130 return r;
131 }
132
133 r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
134 &rdev->uvd.gpu_addr);
135 if (r) {
136 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
137 radeon_bo_unref(&rdev->uvd.vcpu_bo);
138 dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
139 return r;
140 }
141
142 r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
143 if (r) {
144 dev_err(rdev->dev, "(%d) UVD map failed\n", r);
145 return r;
146 }
147
148 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
149
Christian König8ecbcde2013-07-12 10:18:09 -0400150 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
151 atomic_set(&rdev->uvd.handles[i], 0);
152 rdev->uvd.filp[i] = NULL;
153 }
154
155 return 0;
156}
157
158void radeon_uvd_fini(struct radeon_device *rdev)
159{
160 int r;
161
162 if (rdev->uvd.vcpu_bo == NULL)
163 return;
164
165 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
166 if (!r) {
167 radeon_bo_kunmap(rdev->uvd.vcpu_bo);
168 radeon_bo_unpin(rdev->uvd.vcpu_bo);
169 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
170 }
171
172 radeon_bo_unref(&rdev->uvd.vcpu_bo);
Christian König23b6ec02013-08-05 14:10:55 +0200173
174 release_firmware(rdev->uvd_fw);
Christian König8ecbcde2013-07-12 10:18:09 -0400175}
176
177int radeon_uvd_suspend(struct radeon_device *rdev)
178{
179 unsigned size;
Christian König23b6ec02013-08-05 14:10:55 +0200180 void *ptr;
181 int i;
Christian König8ecbcde2013-07-12 10:18:09 -0400182
183 if (rdev->uvd.vcpu_bo == NULL)
184 return 0;
185
Christian König23b6ec02013-08-05 14:10:55 +0200186 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
187 if (atomic_read(&rdev->uvd.handles[i]))
188 break;
189
190 if (i == RADEON_MAX_UVD_HANDLES)
191 return 0;
192
Christian König8ecbcde2013-07-12 10:18:09 -0400193 size = radeon_bo_size(rdev->uvd.vcpu_bo);
Christian König23b6ec02013-08-05 14:10:55 +0200194 size -= rdev->uvd_fw->size;
195
196 ptr = rdev->uvd.cpu_addr;
197 ptr += rdev->uvd_fw->size;
198
Christian König8ecbcde2013-07-12 10:18:09 -0400199 rdev->uvd.saved_bo = kmalloc(size, GFP_KERNEL);
Christian König23b6ec02013-08-05 14:10:55 +0200200 memcpy(rdev->uvd.saved_bo, ptr, size);
Christian König8ecbcde2013-07-12 10:18:09 -0400201
202 return 0;
203}
204
205int radeon_uvd_resume(struct radeon_device *rdev)
206{
Christian König23b6ec02013-08-05 14:10:55 +0200207 unsigned size;
208 void *ptr;
209
Christian König8ecbcde2013-07-12 10:18:09 -0400210 if (rdev->uvd.vcpu_bo == NULL)
211 return -EINVAL;
212
Christian König23b6ec02013-08-05 14:10:55 +0200213 memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
214
215 size = radeon_bo_size(rdev->uvd.vcpu_bo);
216 size -= rdev->uvd_fw->size;
217
218 ptr = rdev->uvd.cpu_addr;
219 ptr += rdev->uvd_fw->size;
220
Christian König8ecbcde2013-07-12 10:18:09 -0400221 if (rdev->uvd.saved_bo != NULL) {
Christian König23b6ec02013-08-05 14:10:55 +0200222 memcpy(ptr, rdev->uvd.saved_bo, size);
Christian König8ecbcde2013-07-12 10:18:09 -0400223 kfree(rdev->uvd.saved_bo);
224 rdev->uvd.saved_bo = NULL;
Christian König23b6ec02013-08-05 14:10:55 +0200225 } else
226 memset(ptr, 0, size);
Christian König8ecbcde2013-07-12 10:18:09 -0400227
Christian Königf2ba57b2013-04-08 12:41:29 +0200228 return 0;
229}
230
231void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
232{
233 rbo->placement.fpfn = 0 >> PAGE_SHIFT;
234 rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
235}
236
237void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
238{
239 int i, r;
240 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
Christian Königce512e52013-08-05 14:10:56 +0200241 uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
242 if (handle != 0 && rdev->uvd.filp[i] == filp) {
Christian Königf2ba57b2013-04-08 12:41:29 +0200243 struct radeon_fence *fence;
244
Christian König08ab0aac2013-10-30 12:56:04 +0100245 radeon_uvd_note_usage(rdev);
246
Christian Königf2ba57b2013-04-08 12:41:29 +0200247 r = radeon_uvd_get_destroy_msg(rdev,
248 R600_RING_TYPE_UVD_INDEX, handle, &fence);
249 if (r) {
250 DRM_ERROR("Error destroying UVD (%d)!\n", r);
251 continue;
252 }
253
254 radeon_fence_wait(fence, false);
255 radeon_fence_unref(&fence);
256
257 rdev->uvd.filp[i] = NULL;
258 atomic_set(&rdev->uvd.handles[i], 0);
259 }
260 }
261}
262
263static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
264{
265 unsigned stream_type = msg[4];
266 unsigned width = msg[6];
267 unsigned height = msg[7];
268 unsigned dpb_size = msg[9];
269 unsigned pitch = msg[28];
270
271 unsigned width_in_mb = width / 16;
272 unsigned height_in_mb = ALIGN(height / 16, 2);
273
274 unsigned image_size, tmp, min_dpb_size;
275
276 image_size = width * height;
277 image_size += image_size / 2;
278 image_size = ALIGN(image_size, 1024);
279
280 switch (stream_type) {
281 case 0: /* H264 */
282
283 /* reference picture buffer */
284 min_dpb_size = image_size * 17;
285
286 /* macroblock context buffer */
287 min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
288
289 /* IT surface buffer */
290 min_dpb_size += width_in_mb * height_in_mb * 32;
291 break;
292
293 case 1: /* VC1 */
294
295 /* reference picture buffer */
296 min_dpb_size = image_size * 3;
297
298 /* CONTEXT_BUFFER */
299 min_dpb_size += width_in_mb * height_in_mb * 128;
300
301 /* IT surface buffer */
302 min_dpb_size += width_in_mb * 64;
303
304 /* DB surface buffer */
305 min_dpb_size += width_in_mb * 128;
306
307 /* BP */
308 tmp = max(width_in_mb, height_in_mb);
309 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
310 break;
311
312 case 3: /* MPEG2 */
313
314 /* reference picture buffer */
315 min_dpb_size = image_size * 3;
316 break;
317
318 case 4: /* MPEG4 */
319
320 /* reference picture buffer */
321 min_dpb_size = image_size * 3;
322
323 /* CM */
324 min_dpb_size += width_in_mb * height_in_mb * 64;
325
326 /* IT surface buffer */
327 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
328 break;
329
330 default:
331 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
332 return -EINVAL;
333 }
334
335 if (width > pitch) {
336 DRM_ERROR("Invalid UVD decoding target pitch!\n");
337 return -EINVAL;
338 }
339
340 if (dpb_size < min_dpb_size) {
341 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
342 dpb_size, min_dpb_size);
343 return -EINVAL;
344 }
345
346 buf_sizes[0x1] = dpb_size;
347 buf_sizes[0x2] = image_size;
348 return 0;
349}
350
351static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
352 unsigned offset, unsigned buf_sizes[])
353{
354 int32_t *msg, msg_type, handle;
355 void *ptr;
356
357 int i, r;
358
359 if (offset & 0x3F) {
360 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
361 return -EINVAL;
362 }
363
Christian König21779242013-08-11 21:27:56 +0200364 if (bo->tbo.sync_obj) {
365 r = radeon_fence_wait(bo->tbo.sync_obj, false);
366 if (r) {
367 DRM_ERROR("Failed waiting for UVD message (%d)!\n", r);
368 return r;
369 }
370 }
371
Christian Königf2ba57b2013-04-08 12:41:29 +0200372 r = radeon_bo_kmap(bo, &ptr);
373 if (r)
374 return r;
375
376 msg = ptr + offset;
377
378 msg_type = msg[1];
379 handle = msg[2];
380
381 if (handle == 0) {
382 DRM_ERROR("Invalid UVD handle!\n");
383 return -EINVAL;
384 }
385
386 if (msg_type == 1) {
387 /* it's a decode msg, calc buffer sizes */
388 r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
389 radeon_bo_kunmap(bo);
390 if (r)
391 return r;
392
393 } else if (msg_type == 2) {
394 /* it's a destroy msg, free the handle */
395 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
396 atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
397 radeon_bo_kunmap(bo);
398 return 0;
399 } else {
400 /* it's a create msg, no special handling needed */
401 radeon_bo_kunmap(bo);
402 }
403
404 /* create or decode, validate the handle */
405 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
406 if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
407 return 0;
408 }
409
410 /* handle not found try to alloc a new one */
411 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
412 if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
413 p->rdev->uvd.filp[i] = p->filp;
414 return 0;
415 }
416 }
417
418 DRM_ERROR("No more free UVD handles!\n");
419 return -EINVAL;
420}
421
422static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
423 int data0, int data1,
424 unsigned buf_sizes[])
425{
426 struct radeon_cs_chunk *relocs_chunk;
427 struct radeon_cs_reloc *reloc;
428 unsigned idx, cmd, offset;
429 uint64_t start, end;
430 int r;
431
432 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
433 offset = radeon_get_ib_value(p, data0);
434 idx = radeon_get_ib_value(p, data1);
435 if (idx >= relocs_chunk->length_dw) {
436 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
437 idx, relocs_chunk->length_dw);
438 return -EINVAL;
439 }
440
441 reloc = p->relocs_ptr[(idx / 4)];
442 start = reloc->lobj.gpu_offset;
443 end = start + radeon_bo_size(reloc->robj);
444 start += offset;
445
446 p->ib.ptr[data0] = start & 0xFFFFFFFF;
447 p->ib.ptr[data1] = start >> 32;
448
449 cmd = radeon_get_ib_value(p, p->idx) >> 1;
450
451 if (cmd < 0x4) {
Leo Liu18cb3172014-04-28 09:40:22 -0400452 if (end <= start) {
453 DRM_ERROR("invalid reloc offset %X!\n", offset);
454 return -EINVAL;
455 }
Christian Königf2ba57b2013-04-08 12:41:29 +0200456 if ((end - start) < buf_sizes[cmd]) {
457 DRM_ERROR("buffer to small (%d / %d)!\n",
458 (unsigned)(end - start), buf_sizes[cmd]);
459 return -EINVAL;
460 }
461
462 } else if (cmd != 0x100) {
463 DRM_ERROR("invalid UVD command %X!\n", cmd);
464 return -EINVAL;
465 }
466
Christian König85724fa2013-12-20 17:48:54 +0100467 if ((start >> 28) != ((end - 1) >> 28)) {
Christian Königf2ba57b2013-04-08 12:41:29 +0200468 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
469 start, end);
470 return -EINVAL;
471 }
472
Christian Königa92c7d52013-04-14 12:45:43 +0200473 /* TODO: is this still necessary on NI+ ? */
474 if ((cmd == 0 || cmd == 0x3) &&
475 (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
476 DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
477 start, end);
478 return -EINVAL;
479 }
480
481 if (cmd == 0) {
482 r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
483 if (r)
484 return r;
485 }
486
Christian Königf2ba57b2013-04-08 12:41:29 +0200487 return 0;
488}
489
490static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
491 struct radeon_cs_packet *pkt,
492 int *data0, int *data1,
493 unsigned buf_sizes[])
494{
495 int i, r;
496
497 p->idx++;
498 for (i = 0; i <= pkt->count; ++i) {
499 switch (pkt->reg + i*4) {
500 case UVD_GPCOM_VCPU_DATA0:
501 *data0 = p->idx;
502 break;
503 case UVD_GPCOM_VCPU_DATA1:
504 *data1 = p->idx;
505 break;
506 case UVD_GPCOM_VCPU_CMD:
507 r = radeon_uvd_cs_reloc(p, *data0, *data1, buf_sizes);
508 if (r)
509 return r;
510 break;
511 case UVD_ENGINE_CNTL:
512 break;
513 default:
514 DRM_ERROR("Invalid reg 0x%X!\n",
515 pkt->reg + i*4);
516 return -EINVAL;
517 }
518 p->idx++;
519 }
520 return 0;
521}
522
523int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
524{
525 struct radeon_cs_packet pkt;
526 int r, data0 = 0, data1 = 0;
527
528 /* minimum buffer sizes */
529 unsigned buf_sizes[] = {
530 [0x00000000] = 2048,
531 [0x00000001] = 32 * 1024 * 1024,
532 [0x00000002] = 2048 * 1152 * 3,
533 [0x00000003] = 2048,
534 };
535
536 if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
537 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
538 p->chunks[p->chunk_ib_idx].length_dw);
539 return -EINVAL;
540 }
541
542 if (p->chunk_relocs_idx == -1) {
543 DRM_ERROR("No relocation chunk !\n");
544 return -EINVAL;
545 }
546
547
548 do {
549 r = radeon_cs_packet_parse(p, &pkt, p->idx);
550 if (r)
551 return r;
552 switch (pkt.type) {
553 case RADEON_PACKET_TYPE0:
554 r = radeon_uvd_cs_reg(p, &pkt, &data0,
555 &data1, buf_sizes);
556 if (r)
557 return r;
558 break;
559 case RADEON_PACKET_TYPE2:
560 p->idx += pkt.count + 2;
561 break;
562 default:
563 DRM_ERROR("Unknown packet type %d !\n", pkt.type);
564 return -EINVAL;
565 }
566 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
567 return 0;
568}
569
570static int radeon_uvd_send_msg(struct radeon_device *rdev,
571 int ring, struct radeon_bo *bo,
572 struct radeon_fence **fence)
573{
574 struct ttm_validate_buffer tv;
575 struct list_head head;
576 struct radeon_ib ib;
577 uint64_t addr;
578 int i, r;
579
580 memset(&tv, 0, sizeof(tv));
581 tv.bo = &bo->tbo;
582
583 INIT_LIST_HEAD(&head);
584 list_add(&tv.head, &head);
585
586 r = ttm_eu_reserve_buffers(&head);
587 if (r)
588 return r;
589
590 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
591 radeon_uvd_force_into_uvd_segment(bo);
592
593 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
594 if (r) {
595 ttm_eu_backoff_reservation(&head);
596 return r;
597 }
598
599 r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
600 if (r) {
601 ttm_eu_backoff_reservation(&head);
602 return r;
603 }
604
605 addr = radeon_bo_gpu_offset(bo);
606 ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
607 ib.ptr[1] = addr;
608 ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
609 ib.ptr[3] = addr >> 32;
610 ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
611 ib.ptr[5] = 0;
612 for (i = 6; i < 16; ++i)
613 ib.ptr[i] = PACKET2(0);
614 ib.length_dw = 16;
615
616 r = radeon_ib_schedule(rdev, &ib, NULL);
617 if (r) {
618 ttm_eu_backoff_reservation(&head);
619 return r;
620 }
621 ttm_eu_fence_buffer_objects(&head, ib.fence);
622
623 if (fence)
624 *fence = radeon_fence_ref(ib.fence);
625
626 radeon_ib_free(rdev, &ib);
627 radeon_bo_unref(&bo);
628 return 0;
629}
630
631/* multiple fence commands without any stream commands in between can
632 crash the vcpu so just try to emmit a dummy create/destroy msg to
633 avoid this */
634int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
635 uint32_t handle, struct radeon_fence **fence)
636{
637 struct radeon_bo *bo;
638 uint32_t *msg;
639 int r, i;
640
641 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
642 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
643 if (r)
644 return r;
645
646 r = radeon_bo_reserve(bo, false);
647 if (r) {
648 radeon_bo_unref(&bo);
649 return r;
650 }
651
652 r = radeon_bo_kmap(bo, (void **)&msg);
653 if (r) {
654 radeon_bo_unreserve(bo);
655 radeon_bo_unref(&bo);
656 return r;
657 }
658
659 /* stitch together an UVD create msg */
Alex Deucherc139b1e2013-06-07 10:04:54 -0400660 msg[0] = cpu_to_le32(0x00000de4);
661 msg[1] = cpu_to_le32(0x00000000);
662 msg[2] = cpu_to_le32(handle);
663 msg[3] = cpu_to_le32(0x00000000);
664 msg[4] = cpu_to_le32(0x00000000);
665 msg[5] = cpu_to_le32(0x00000000);
666 msg[6] = cpu_to_le32(0x00000000);
667 msg[7] = cpu_to_le32(0x00000780);
668 msg[8] = cpu_to_le32(0x00000440);
669 msg[9] = cpu_to_le32(0x00000000);
670 msg[10] = cpu_to_le32(0x01b37000);
Christian Königf2ba57b2013-04-08 12:41:29 +0200671 for (i = 11; i < 1024; ++i)
Alex Deucherc139b1e2013-06-07 10:04:54 -0400672 msg[i] = cpu_to_le32(0x0);
Christian Königf2ba57b2013-04-08 12:41:29 +0200673
674 radeon_bo_kunmap(bo);
675 radeon_bo_unreserve(bo);
676
677 return radeon_uvd_send_msg(rdev, ring, bo, fence);
678}
679
680int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
681 uint32_t handle, struct radeon_fence **fence)
682{
683 struct radeon_bo *bo;
684 uint32_t *msg;
685 int r, i;
686
687 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
688 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
689 if (r)
690 return r;
691
692 r = radeon_bo_reserve(bo, false);
693 if (r) {
694 radeon_bo_unref(&bo);
695 return r;
696 }
697
698 r = radeon_bo_kmap(bo, (void **)&msg);
699 if (r) {
700 radeon_bo_unreserve(bo);
701 radeon_bo_unref(&bo);
702 return r;
703 }
704
705 /* stitch together an UVD destroy msg */
Alex Deucherc139b1e2013-06-07 10:04:54 -0400706 msg[0] = cpu_to_le32(0x00000de4);
707 msg[1] = cpu_to_le32(0x00000002);
708 msg[2] = cpu_to_le32(handle);
709 msg[3] = cpu_to_le32(0x00000000);
Christian Königf2ba57b2013-04-08 12:41:29 +0200710 for (i = 4; i < 1024; ++i)
Alex Deucherc139b1e2013-06-07 10:04:54 -0400711 msg[i] = cpu_to_le32(0x0);
Christian Königf2ba57b2013-04-08 12:41:29 +0200712
713 radeon_bo_kunmap(bo);
714 radeon_bo_unreserve(bo);
715
716 return radeon_uvd_send_msg(rdev, ring, bo, fence);
717}
Christian König55b51c82013-04-18 15:25:59 +0200718
719static void radeon_uvd_idle_work_handler(struct work_struct *work)
720{
721 struct radeon_device *rdev =
722 container_of(work, struct radeon_device, uvd.idle_work.work);
723
724 if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0)
725 radeon_set_uvd_clocks(rdev, 0, 0);
726 else
727 schedule_delayed_work(&rdev->uvd.idle_work,
728 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
729}
730
731void radeon_uvd_note_usage(struct radeon_device *rdev)
732{
733 bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
734 set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
735 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
736 if (set_clocks)
737 radeon_set_uvd_clocks(rdev, 53300, 40000);
738}
Christian Königfacd1122013-04-29 11:55:02 +0200739
740static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
741 unsigned target_freq,
742 unsigned pd_min,
743 unsigned pd_even)
744{
745 unsigned post_div = vco_freq / target_freq;
746
747 /* adjust to post divider minimum value */
748 if (post_div < pd_min)
749 post_div = pd_min;
750
751 /* we alway need a frequency less than or equal the target */
752 if ((vco_freq / post_div) > target_freq)
753 post_div += 1;
754
755 /* post dividers above a certain value must be even */
756 if (post_div > pd_even && post_div % 2)
757 post_div += 1;
758
759 return post_div;
760}
761
762/**
763 * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
764 *
765 * @rdev: radeon_device pointer
766 * @vclk: wanted VCLK
767 * @dclk: wanted DCLK
768 * @vco_min: minimum VCO frequency
769 * @vco_max: maximum VCO frequency
770 * @fb_factor: factor to multiply vco freq with
771 * @fb_mask: limit and bitmask for feedback divider
772 * @pd_min: post divider minimum
773 * @pd_max: post divider maximum
774 * @pd_even: post divider must be even above this value
775 * @optimal_fb_div: resulting feedback divider
776 * @optimal_vclk_div: resulting vclk post divider
777 * @optimal_dclk_div: resulting dclk post divider
778 *
779 * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
780 * Returns zero on success -EINVAL on error.
781 */
782int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
783 unsigned vclk, unsigned dclk,
784 unsigned vco_min, unsigned vco_max,
785 unsigned fb_factor, unsigned fb_mask,
786 unsigned pd_min, unsigned pd_max,
787 unsigned pd_even,
788 unsigned *optimal_fb_div,
789 unsigned *optimal_vclk_div,
790 unsigned *optimal_dclk_div)
791{
792 unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
793
794 /* start off with something large */
795 unsigned optimal_score = ~0;
796
797 /* loop through vco from low to high */
798 vco_min = max(max(vco_min, vclk), dclk);
799 for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
800
801 uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
802 unsigned vclk_div, dclk_div, score;
803
804 do_div(fb_div, ref_freq);
805
806 /* fb div out of range ? */
807 if (fb_div > fb_mask)
808 break; /* it can oly get worse */
809
810 fb_div &= fb_mask;
811
812 /* calc vclk divider with current vco freq */
813 vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
814 pd_min, pd_even);
815 if (vclk_div > pd_max)
816 break; /* vco is too big, it has to stop */
817
818 /* calc dclk divider with current vco freq */
819 dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
820 pd_min, pd_even);
821 if (vclk_div > pd_max)
822 break; /* vco is too big, it has to stop */
823
824 /* calc score with current vco freq */
825 score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
826
827 /* determine if this vco setting is better than current optimal settings */
828 if (score < optimal_score) {
829 *optimal_fb_div = fb_div;
830 *optimal_vclk_div = vclk_div;
831 *optimal_dclk_div = dclk_div;
832 optimal_score = score;
833 if (optimal_score == 0)
834 break; /* it can't get better than this */
835 }
836 }
837
838 /* did we found a valid setup ? */
839 if (optimal_score == ~0)
840 return -EINVAL;
841
842 return 0;
843}
844
845int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
846 unsigned cg_upll_func_cntl)
847{
848 unsigned i;
849
850 /* make sure UPLL_CTLREQ is deasserted */
851 WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
852
853 mdelay(10);
854
855 /* assert UPLL_CTLREQ */
856 WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
857
858 /* wait for CTLACK and CTLACK2 to get asserted */
859 for (i = 0; i < 100; ++i) {
860 uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
861 if ((RREG32(cg_upll_func_cntl) & mask) == mask)
862 break;
863 mdelay(10);
864 }
865
866 /* deassert UPLL_CTLREQ */
867 WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
868
869 if (i == 100) {
870 DRM_ERROR("Timeout setting UVD clocks!\n");
871 return -ETIMEDOUT;
872 }
873
874 return 0;
875}