2 * Copyright 2011 Advanced Micro Devices, Inc.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Christian König <deathsimple@vodafone.de>
31 #include <linux/firmware.h>
32 #include <linux/module.h>
39 /* 1 second timeout */
40 #define UVD_IDLE_TIMEOUT_MS 1000
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 #define FIRMWARE_BONAIRE "radeon/BONAIRE_uvd.bin"
49 MODULE_FIRMWARE(FIRMWARE_RV710);
50 MODULE_FIRMWARE(FIRMWARE_CYPRESS);
51 MODULE_FIRMWARE(FIRMWARE_SUMO);
52 MODULE_FIRMWARE(FIRMWARE_TAHITI);
53 MODULE_FIRMWARE(FIRMWARE_BONAIRE);
55 static void radeon_uvd_idle_work_handler(struct work_struct *work);
57 int radeon_uvd_init(struct radeon_device *rdev)
59 struct platform_device *pdev;
60 unsigned long bo_size;
64 INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
66 pdev = platform_device_register_simple("radeon_uvd", 0, NULL, 0);
69 dev_err(rdev->dev, "radeon_uvd: Failed to register firmware\n");
73 switch (rdev->family) {
77 fw_name = FIRMWARE_RV710;
85 fw_name = FIRMWARE_CYPRESS;
95 fw_name = FIRMWARE_SUMO;
102 fw_name = FIRMWARE_TAHITI;
108 fw_name = FIRMWARE_BONAIRE;
115 r = request_firmware(&rdev->uvd_fw, fw_name, &pdev->dev);
117 dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
119 platform_device_unregister(pdev);
123 platform_device_unregister(pdev);
125 bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
126 RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE;
127 r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
128 RADEON_GEM_DOMAIN_VRAM, NULL, &rdev->uvd.vcpu_bo);
130 dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
134 r = radeon_uvd_resume(rdev);
138 memset(rdev->uvd.cpu_addr, 0, bo_size);
139 memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
141 r = radeon_uvd_suspend(rdev);
145 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
146 atomic_set(&rdev->uvd.handles[i], 0);
147 rdev->uvd.filp[i] = NULL;
153 void radeon_uvd_fini(struct radeon_device *rdev)
155 radeon_uvd_suspend(rdev);
156 radeon_bo_unref(&rdev->uvd.vcpu_bo);
159 int radeon_uvd_suspend(struct radeon_device *rdev)
163 if (rdev->uvd.vcpu_bo == NULL)
166 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
168 radeon_bo_kunmap(rdev->uvd.vcpu_bo);
169 radeon_bo_unpin(rdev->uvd.vcpu_bo);
170 rdev->uvd.cpu_addr = NULL;
171 if (!radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_CPU, NULL)) {
172 radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
174 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
176 if (rdev->uvd.cpu_addr) {
177 radeon_fence_driver_start_ring(rdev, R600_RING_TYPE_UVD_INDEX);
179 rdev->fence_drv[R600_RING_TYPE_UVD_INDEX].cpu_addr = NULL;
185 int radeon_uvd_resume(struct radeon_device *rdev)
189 if (rdev->uvd.vcpu_bo == NULL)
192 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
194 radeon_bo_unref(&rdev->uvd.vcpu_bo);
195 dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
199 /* Have been pin in cpu unmap unpin */
200 radeon_bo_kunmap(rdev->uvd.vcpu_bo);
201 radeon_bo_unpin(rdev->uvd.vcpu_bo);
203 r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
204 &rdev->uvd.gpu_addr);
206 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
207 radeon_bo_unref(&rdev->uvd.vcpu_bo);
208 dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
212 r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
214 dev_err(rdev->dev, "(%d) UVD map failed\n", r);
218 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
223 void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
225 rbo->placement.fpfn = 0 >> PAGE_SHIFT;
226 rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
229 void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
232 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
233 if (rdev->uvd.filp[i] == filp) {
234 uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
235 struct radeon_fence *fence;
237 r = radeon_uvd_get_destroy_msg(rdev,
238 R600_RING_TYPE_UVD_INDEX, handle, &fence);
240 DRM_ERROR("Error destroying UVD (%d)!\n", r);
244 radeon_fence_wait(fence, false);
245 radeon_fence_unref(&fence);
247 rdev->uvd.filp[i] = NULL;
248 atomic_set(&rdev->uvd.handles[i], 0);
253 static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
255 unsigned stream_type = msg[4];
256 unsigned width = msg[6];
257 unsigned height = msg[7];
258 unsigned dpb_size = msg[9];
259 unsigned pitch = msg[28];
261 unsigned width_in_mb = width / 16;
262 unsigned height_in_mb = ALIGN(height / 16, 2);
264 unsigned image_size, tmp, min_dpb_size;
266 image_size = width * height;
267 image_size += image_size / 2;
268 image_size = ALIGN(image_size, 1024);
270 switch (stream_type) {
273 /* reference picture buffer */
274 min_dpb_size = image_size * 17;
276 /* macroblock context buffer */
277 min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
279 /* IT surface buffer */
280 min_dpb_size += width_in_mb * height_in_mb * 32;
285 /* reference picture buffer */
286 min_dpb_size = image_size * 3;
289 min_dpb_size += width_in_mb * height_in_mb * 128;
291 /* IT surface buffer */
292 min_dpb_size += width_in_mb * 64;
294 /* DB surface buffer */
295 min_dpb_size += width_in_mb * 128;
298 tmp = max(width_in_mb, height_in_mb);
299 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
304 /* reference picture buffer */
305 min_dpb_size = image_size * 3;
310 /* reference picture buffer */
311 min_dpb_size = image_size * 3;
314 min_dpb_size += width_in_mb * height_in_mb * 64;
316 /* IT surface buffer */
317 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
321 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
326 DRM_ERROR("Invalid UVD decoding target pitch!\n");
330 if (dpb_size < min_dpb_size) {
331 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
332 dpb_size, min_dpb_size);
336 buf_sizes[0x1] = dpb_size;
337 buf_sizes[0x2] = image_size;
341 static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
342 unsigned offset, unsigned buf_sizes[])
344 int32_t *msg, msg_type, handle;
350 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
354 r = radeon_bo_kmap(bo, &ptr);
364 DRM_ERROR("Invalid UVD handle!\n");
369 /* it's a decode msg, calc buffer sizes */
370 r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
371 radeon_bo_kunmap(bo);
375 } else if (msg_type == 2) {
376 /* it's a destroy msg, free the handle */
377 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
378 atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
379 radeon_bo_kunmap(bo);
382 /* it's a create msg, no special handling needed */
383 radeon_bo_kunmap(bo);
386 /* create or decode, validate the handle */
387 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
388 if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
392 /* handle not found try to alloc a new one */
393 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
394 if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
395 p->rdev->uvd.filp[i] = p->filp;
400 DRM_ERROR("No more free UVD handles!\n");
404 static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
405 int data0, int data1,
406 unsigned buf_sizes[])
408 struct radeon_cs_chunk *relocs_chunk;
409 struct radeon_cs_reloc *reloc;
410 unsigned idx, cmd, offset;
414 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
415 offset = radeon_get_ib_value(p, data0);
416 idx = radeon_get_ib_value(p, data1);
417 if (idx >= relocs_chunk->length_dw) {
418 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
419 idx, relocs_chunk->length_dw);
423 reloc = p->relocs_ptr[(idx / 4)];
424 start = reloc->lobj.gpu_offset;
425 end = start + radeon_bo_size(reloc->robj);
428 p->ib.ptr[data0] = start & 0xFFFFFFFF;
429 p->ib.ptr[data1] = start >> 32;
431 cmd = radeon_get_ib_value(p, p->idx) >> 1;
434 if ((end - start) < buf_sizes[cmd]) {
435 DRM_ERROR("buffer to small (%d / %d)!\n",
436 (unsigned)(end - start), buf_sizes[cmd]);
440 } else if (cmd != 0x100) {
441 DRM_ERROR("invalid UVD command %X!\n", cmd);
445 if ((start >> 28) != (end >> 28)) {
446 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
451 /* TODO: is this still necessary on NI+ ? */
452 if ((cmd == 0 || cmd == 0x3) &&
453 (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
454 DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
460 r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
468 static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
469 struct radeon_cs_packet *pkt,
470 int *data0, int *data1,
471 unsigned buf_sizes[])
476 for (i = 0; i <= pkt->count; ++i) {
477 switch (pkt->reg + i*4) {
478 case UVD_GPCOM_VCPU_DATA0:
481 case UVD_GPCOM_VCPU_DATA1:
484 case UVD_GPCOM_VCPU_CMD:
485 r = radeon_uvd_cs_reloc(p, *data0, *data1, buf_sizes);
489 case UVD_ENGINE_CNTL:
492 DRM_ERROR("Invalid reg 0x%X!\n",
501 int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
503 struct radeon_cs_packet pkt;
504 int r, data0 = 0, data1 = 0;
506 /* minimum buffer sizes */
507 unsigned buf_sizes[] = {
509 [0x00000001] = 32 * 1024 * 1024,
510 [0x00000002] = 2048 * 1152 * 3,
514 if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
515 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
516 p->chunks[p->chunk_ib_idx].length_dw);
520 if (p->chunk_relocs_idx == -1) {
521 DRM_ERROR("No relocation chunk !\n");
527 r = radeon_cs_packet_parse(p, &pkt, p->idx);
531 case RADEON_PACKET_TYPE0:
532 r = radeon_uvd_cs_reg(p, &pkt, &data0,
537 case RADEON_PACKET_TYPE2:
538 p->idx += pkt.count + 2;
541 DRM_ERROR("Unknown packet type %d !\n", pkt.type);
544 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
548 static int radeon_uvd_send_msg(struct radeon_device *rdev,
549 int ring, struct radeon_bo *bo,
550 struct radeon_fence **fence)
552 struct ttm_validate_buffer tv;
553 struct list_head head;
558 memset(&tv, 0, sizeof(tv));
561 INIT_LIST_HEAD(&head);
562 list_add(&tv.head, &head);
564 r = ttm_eu_reserve_buffers(&head);
568 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
569 radeon_uvd_force_into_uvd_segment(bo);
571 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
573 ttm_eu_backoff_reservation(&head);
577 r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
579 ttm_eu_backoff_reservation(&head);
583 addr = radeon_bo_gpu_offset(bo);
584 ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
586 ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
587 ib.ptr[3] = addr >> 32;
588 ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
590 for (i = 6; i < 16; ++i)
591 ib.ptr[i] = PACKET2(0);
594 r = radeon_ib_schedule(rdev, &ib, NULL);
596 ttm_eu_backoff_reservation(&head);
599 ttm_eu_fence_buffer_objects(&head, ib.fence);
602 *fence = radeon_fence_ref(ib.fence);
604 radeon_ib_free(rdev, &ib);
605 radeon_bo_unref(&bo);
609 /* multiple fence commands without any stream commands in between can
610 crash the vcpu so just try to emmit a dummy create/destroy msg to
612 int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
613 uint32_t handle, struct radeon_fence **fence)
615 struct radeon_bo *bo;
619 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
620 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
624 r = radeon_bo_reserve(bo, false);
626 radeon_bo_unref(&bo);
630 r = radeon_bo_kmap(bo, (void **)&msg);
632 radeon_bo_unreserve(bo);
633 radeon_bo_unref(&bo);
637 /* stitch together an UVD create msg */
638 msg[0] = cpu_to_le32(0x00000de4);
639 msg[1] = cpu_to_le32(0x00000000);
640 msg[2] = cpu_to_le32(handle);
641 msg[3] = cpu_to_le32(0x00000000);
642 msg[4] = cpu_to_le32(0x00000000);
643 msg[5] = cpu_to_le32(0x00000000);
644 msg[6] = cpu_to_le32(0x00000000);
645 msg[7] = cpu_to_le32(0x00000780);
646 msg[8] = cpu_to_le32(0x00000440);
647 msg[9] = cpu_to_le32(0x00000000);
648 msg[10] = cpu_to_le32(0x01b37000);
649 for (i = 11; i < 1024; ++i)
650 msg[i] = cpu_to_le32(0x0);
652 radeon_bo_kunmap(bo);
653 radeon_bo_unreserve(bo);
655 return radeon_uvd_send_msg(rdev, ring, bo, fence);
658 int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
659 uint32_t handle, struct radeon_fence **fence)
661 struct radeon_bo *bo;
665 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
666 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
670 r = radeon_bo_reserve(bo, false);
672 radeon_bo_unref(&bo);
676 r = radeon_bo_kmap(bo, (void **)&msg);
678 radeon_bo_unreserve(bo);
679 radeon_bo_unref(&bo);
683 /* stitch together an UVD destroy msg */
684 msg[0] = cpu_to_le32(0x00000de4);
685 msg[1] = cpu_to_le32(0x00000002);
686 msg[2] = cpu_to_le32(handle);
687 msg[3] = cpu_to_le32(0x00000000);
688 for (i = 4; i < 1024; ++i)
689 msg[i] = cpu_to_le32(0x0);
691 radeon_bo_kunmap(bo);
692 radeon_bo_unreserve(bo);
694 return radeon_uvd_send_msg(rdev, ring, bo, fence);
697 static void radeon_uvd_idle_work_handler(struct work_struct *work)
699 struct radeon_device *rdev =
700 container_of(work, struct radeon_device, uvd.idle_work.work);
702 if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0)
703 radeon_set_uvd_clocks(rdev, 0, 0);
705 schedule_delayed_work(&rdev->uvd.idle_work,
706 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
709 void radeon_uvd_note_usage(struct radeon_device *rdev)
711 bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
712 set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
713 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
715 radeon_set_uvd_clocks(rdev, 53300, 40000);
718 static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
719 unsigned target_freq,
723 unsigned post_div = vco_freq / target_freq;
725 /* adjust to post divider minimum value */
726 if (post_div < pd_min)
729 /* we alway need a frequency less than or equal the target */
730 if ((vco_freq / post_div) > target_freq)
733 /* post dividers above a certain value must be even */
734 if (post_div > pd_even && post_div % 2)
741 * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
743 * @rdev: radeon_device pointer
746 * @vco_min: minimum VCO frequency
747 * @vco_max: maximum VCO frequency
748 * @fb_factor: factor to multiply vco freq with
749 * @fb_mask: limit and bitmask for feedback divider
750 * @pd_min: post divider minimum
751 * @pd_max: post divider maximum
752 * @pd_even: post divider must be even above this value
753 * @optimal_fb_div: resulting feedback divider
754 * @optimal_vclk_div: resulting vclk post divider
755 * @optimal_dclk_div: resulting dclk post divider
757 * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
758 * Returns zero on success -EINVAL on error.
760 int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
761 unsigned vclk, unsigned dclk,
762 unsigned vco_min, unsigned vco_max,
763 unsigned fb_factor, unsigned fb_mask,
764 unsigned pd_min, unsigned pd_max,
766 unsigned *optimal_fb_div,
767 unsigned *optimal_vclk_div,
768 unsigned *optimal_dclk_div)
770 unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
772 /* start off with something large */
773 unsigned optimal_score = ~0;
775 /* loop through vco from low to high */
776 vco_min = max(max(vco_min, vclk), dclk);
777 for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
779 uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
780 unsigned vclk_div, dclk_div, score;
782 do_div(fb_div, ref_freq);
784 /* fb div out of range ? */
785 if (fb_div > fb_mask)
786 break; /* it can oly get worse */
790 /* calc vclk divider with current vco freq */
791 vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
793 if (vclk_div > pd_max)
794 break; /* vco is too big, it has to stop */
796 /* calc dclk divider with current vco freq */
797 dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
799 if (vclk_div > pd_max)
800 break; /* vco is too big, it has to stop */
802 /* calc score with current vco freq */
803 score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
805 /* determine if this vco setting is better than current optimal settings */
806 if (score < optimal_score) {
807 *optimal_fb_div = fb_div;
808 *optimal_vclk_div = vclk_div;
809 *optimal_dclk_div = dclk_div;
810 optimal_score = score;
811 if (optimal_score == 0)
812 break; /* it can't get better than this */
816 /* did we found a valid setup ? */
817 if (optimal_score == ~0)
823 int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
824 unsigned cg_upll_func_cntl)
828 /* make sure UPLL_CTLREQ is deasserted */
829 WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
833 /* assert UPLL_CTLREQ */
834 WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
836 /* wait for CTLACK and CTLACK2 to get asserted */
837 for (i = 0; i < 100; ++i) {
838 uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
839 if ((RREG32(cg_upll_func_cntl) & mask) == mask)
844 /* deassert UPLL_CTLREQ */
845 WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
848 DRM_ERROR("Timeout setting UVD clocks!\n");