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"
48 MODULE_FIRMWARE(FIRMWARE_RV710);
49 MODULE_FIRMWARE(FIRMWARE_CYPRESS);
50 MODULE_FIRMWARE(FIRMWARE_SUMO);
51 MODULE_FIRMWARE(FIRMWARE_TAHITI);
53 static void radeon_uvd_idle_work_handler(struct work_struct *work);
55 int radeon_uvd_init(struct radeon_device *rdev)
57 struct platform_device *pdev;
58 unsigned long bo_size;
62 INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
64 pdev = platform_device_register_simple("radeon_uvd", 0, NULL, 0);
67 dev_err(rdev->dev, "radeon_uvd: Failed to register firmware\n");
71 switch (rdev->family) {
75 fw_name = FIRMWARE_RV710;
83 fw_name = FIRMWARE_CYPRESS;
93 fw_name = FIRMWARE_SUMO;
100 fw_name = FIRMWARE_TAHITI;
107 r = request_firmware(&rdev->uvd_fw, fw_name, &pdev->dev);
109 dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
111 platform_device_unregister(pdev);
115 platform_device_unregister(pdev);
117 bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
118 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);
122 dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
126 r = radeon_uvd_resume(rdev);
130 memset(rdev->uvd.cpu_addr, 0, bo_size);
131 memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
133 r = radeon_uvd_suspend(rdev);
137 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
138 atomic_set(&rdev->uvd.handles[i], 0);
139 rdev->uvd.filp[i] = NULL;
145 void radeon_uvd_fini(struct radeon_device *rdev)
147 radeon_uvd_suspend(rdev);
148 radeon_bo_unref(&rdev->uvd.vcpu_bo);
151 int radeon_uvd_suspend(struct radeon_device *rdev)
155 if (rdev->uvd.vcpu_bo == NULL)
158 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
160 radeon_bo_kunmap(rdev->uvd.vcpu_bo);
161 radeon_bo_unpin(rdev->uvd.vcpu_bo);
162 rdev->uvd.cpu_addr = NULL;
163 if (!radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_CPU, NULL)) {
164 radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
166 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
168 if (rdev->uvd.cpu_addr) {
169 radeon_fence_driver_start_ring(rdev, R600_RING_TYPE_UVD_INDEX);
171 rdev->fence_drv[R600_RING_TYPE_UVD_INDEX].cpu_addr = NULL;
177 int radeon_uvd_resume(struct radeon_device *rdev)
181 if (rdev->uvd.vcpu_bo == NULL)
184 r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
186 radeon_bo_unref(&rdev->uvd.vcpu_bo);
187 dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
191 /* Have been pin in cpu unmap unpin */
192 radeon_bo_kunmap(rdev->uvd.vcpu_bo);
193 radeon_bo_unpin(rdev->uvd.vcpu_bo);
195 r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
196 &rdev->uvd.gpu_addr);
198 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
199 radeon_bo_unref(&rdev->uvd.vcpu_bo);
200 dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
204 r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
206 dev_err(rdev->dev, "(%d) UVD map failed\n", r);
210 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
215 void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
217 rbo->placement.fpfn = 0 >> PAGE_SHIFT;
218 rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
221 void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
224 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
225 if (rdev->uvd.filp[i] == filp) {
226 uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
227 struct radeon_fence *fence;
229 r = radeon_uvd_get_destroy_msg(rdev,
230 R600_RING_TYPE_UVD_INDEX, handle, &fence);
232 DRM_ERROR("Error destroying UVD (%d)!\n", r);
236 radeon_fence_wait(fence, false);
237 radeon_fence_unref(&fence);
239 rdev->uvd.filp[i] = NULL;
240 atomic_set(&rdev->uvd.handles[i], 0);
245 static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
247 unsigned stream_type = msg[4];
248 unsigned width = msg[6];
249 unsigned height = msg[7];
250 unsigned dpb_size = msg[9];
251 unsigned pitch = msg[28];
253 unsigned width_in_mb = width / 16;
254 unsigned height_in_mb = ALIGN(height / 16, 2);
256 unsigned image_size, tmp, min_dpb_size;
258 image_size = width * height;
259 image_size += image_size / 2;
260 image_size = ALIGN(image_size, 1024);
262 switch (stream_type) {
265 /* reference picture buffer */
266 min_dpb_size = image_size * 17;
268 /* macroblock context buffer */
269 min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
271 /* IT surface buffer */
272 min_dpb_size += width_in_mb * height_in_mb * 32;
277 /* reference picture buffer */
278 min_dpb_size = image_size * 3;
281 min_dpb_size += width_in_mb * height_in_mb * 128;
283 /* IT surface buffer */
284 min_dpb_size += width_in_mb * 64;
286 /* DB surface buffer */
287 min_dpb_size += width_in_mb * 128;
290 tmp = max(width_in_mb, height_in_mb);
291 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
296 /* reference picture buffer */
297 min_dpb_size = image_size * 3;
302 /* reference picture buffer */
303 min_dpb_size = image_size * 3;
306 min_dpb_size += width_in_mb * height_in_mb * 64;
308 /* IT surface buffer */
309 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
313 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
318 DRM_ERROR("Invalid UVD decoding target pitch!\n");
322 if (dpb_size < min_dpb_size) {
323 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
324 dpb_size, min_dpb_size);
328 buf_sizes[0x1] = dpb_size;
329 buf_sizes[0x2] = image_size;
333 static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
334 unsigned offset, unsigned buf_sizes[])
336 int32_t *msg, msg_type, handle;
342 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
346 r = radeon_bo_kmap(bo, &ptr);
356 DRM_ERROR("Invalid UVD handle!\n");
361 /* it's a decode msg, calc buffer sizes */
362 r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
363 radeon_bo_kunmap(bo);
367 } else if (msg_type == 2) {
368 /* it's a destroy msg, free the handle */
369 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
370 atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
371 radeon_bo_kunmap(bo);
374 /* it's a create msg, no special handling needed */
375 radeon_bo_kunmap(bo);
378 /* create or decode, validate the handle */
379 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
380 if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
384 /* handle not found try to alloc a new one */
385 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
386 if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
387 p->rdev->uvd.filp[i] = p->filp;
392 DRM_ERROR("No more free UVD handles!\n");
396 static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
397 int data0, int data1,
398 unsigned buf_sizes[])
400 struct radeon_cs_chunk *relocs_chunk;
401 struct radeon_cs_reloc *reloc;
402 unsigned idx, cmd, offset;
406 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
407 offset = radeon_get_ib_value(p, data0);
408 idx = radeon_get_ib_value(p, data1);
409 if (idx >= relocs_chunk->length_dw) {
410 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
411 idx, relocs_chunk->length_dw);
415 reloc = p->relocs_ptr[(idx / 4)];
416 start = reloc->lobj.gpu_offset;
417 end = start + radeon_bo_size(reloc->robj);
420 p->ib.ptr[data0] = start & 0xFFFFFFFF;
421 p->ib.ptr[data1] = start >> 32;
423 cmd = radeon_get_ib_value(p, p->idx) >> 1;
426 if ((end - start) < buf_sizes[cmd]) {
427 DRM_ERROR("buffer to small (%d / %d)!\n",
428 (unsigned)(end - start), buf_sizes[cmd]);
432 } else if (cmd != 0x100) {
433 DRM_ERROR("invalid UVD command %X!\n", cmd);
437 if ((start >> 28) != (end >> 28)) {
438 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
443 /* TODO: is this still necessary on NI+ ? */
444 if ((cmd == 0 || cmd == 0x3) &&
445 (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
446 DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
452 r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
460 static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
461 struct radeon_cs_packet *pkt,
462 int *data0, int *data1,
463 unsigned buf_sizes[])
468 for (i = 0; i <= pkt->count; ++i) {
469 switch (pkt->reg + i*4) {
470 case UVD_GPCOM_VCPU_DATA0:
473 case UVD_GPCOM_VCPU_DATA1:
476 case UVD_GPCOM_VCPU_CMD:
477 r = radeon_uvd_cs_reloc(p, *data0, *data1, buf_sizes);
481 case UVD_ENGINE_CNTL:
484 DRM_ERROR("Invalid reg 0x%X!\n",
493 int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
495 struct radeon_cs_packet pkt;
496 int r, data0 = 0, data1 = 0;
498 /* minimum buffer sizes */
499 unsigned buf_sizes[] = {
501 [0x00000001] = 32 * 1024 * 1024,
502 [0x00000002] = 2048 * 1152 * 3,
506 if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
507 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
508 p->chunks[p->chunk_ib_idx].length_dw);
512 if (p->chunk_relocs_idx == -1) {
513 DRM_ERROR("No relocation chunk !\n");
519 r = radeon_cs_packet_parse(p, &pkt, p->idx);
523 case RADEON_PACKET_TYPE0:
524 r = radeon_uvd_cs_reg(p, &pkt, &data0,
529 case RADEON_PACKET_TYPE2:
530 p->idx += pkt.count + 2;
533 DRM_ERROR("Unknown packet type %d !\n", pkt.type);
536 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
540 static int radeon_uvd_send_msg(struct radeon_device *rdev,
541 int ring, struct radeon_bo *bo,
542 struct radeon_fence **fence)
544 struct ttm_validate_buffer tv;
545 struct list_head head;
550 memset(&tv, 0, sizeof(tv));
553 INIT_LIST_HEAD(&head);
554 list_add(&tv.head, &head);
556 r = ttm_eu_reserve_buffers(&head);
560 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
561 radeon_uvd_force_into_uvd_segment(bo);
563 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
565 ttm_eu_backoff_reservation(&head);
569 r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
571 ttm_eu_backoff_reservation(&head);
575 addr = radeon_bo_gpu_offset(bo);
576 ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
578 ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
579 ib.ptr[3] = addr >> 32;
580 ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
582 for (i = 6; i < 16; ++i)
583 ib.ptr[i] = PACKET2(0);
586 r = radeon_ib_schedule(rdev, &ib, NULL);
588 ttm_eu_backoff_reservation(&head);
591 ttm_eu_fence_buffer_objects(&head, ib.fence);
594 *fence = radeon_fence_ref(ib.fence);
596 radeon_ib_free(rdev, &ib);
597 radeon_bo_unref(&bo);
601 /* multiple fence commands without any stream commands in between can
602 crash the vcpu so just try to emmit a dummy create/destroy msg to
604 int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
605 uint32_t handle, struct radeon_fence **fence)
607 struct radeon_bo *bo;
611 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
612 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
616 r = radeon_bo_reserve(bo, false);
618 radeon_bo_unref(&bo);
622 r = radeon_bo_kmap(bo, (void **)&msg);
624 radeon_bo_unreserve(bo);
625 radeon_bo_unref(&bo);
629 /* stitch together an UVD create msg */
640 msg[10] = 0x01b37000;
641 for (i = 11; i < 1024; ++i)
644 radeon_bo_kunmap(bo);
645 radeon_bo_unreserve(bo);
647 return radeon_uvd_send_msg(rdev, ring, bo, fence);
650 int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
651 uint32_t handle, struct radeon_fence **fence)
653 struct radeon_bo *bo;
657 r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
658 RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
662 r = radeon_bo_reserve(bo, false);
664 radeon_bo_unref(&bo);
668 r = radeon_bo_kmap(bo, (void **)&msg);
670 radeon_bo_unreserve(bo);
671 radeon_bo_unref(&bo);
675 /* stitch together an UVD destroy msg */
680 for (i = 4; i < 1024; ++i)
683 radeon_bo_kunmap(bo);
684 radeon_bo_unreserve(bo);
686 return radeon_uvd_send_msg(rdev, ring, bo, fence);
689 static void radeon_uvd_idle_work_handler(struct work_struct *work)
691 struct radeon_device *rdev =
692 container_of(work, struct radeon_device, uvd.idle_work.work);
694 if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0)
695 radeon_set_uvd_clocks(rdev, 0, 0);
697 schedule_delayed_work(&rdev->uvd.idle_work,
698 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
701 void radeon_uvd_note_usage(struct radeon_device *rdev)
703 bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
704 set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
705 msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
707 radeon_set_uvd_clocks(rdev, 53300, 40000);
710 static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
711 unsigned target_freq,
715 unsigned post_div = vco_freq / target_freq;
717 /* adjust to post divider minimum value */
718 if (post_div < pd_min)
721 /* we alway need a frequency less than or equal the target */
722 if ((vco_freq / post_div) > target_freq)
725 /* post dividers above a certain value must be even */
726 if (post_div > pd_even && post_div % 2)
733 * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
735 * @rdev: radeon_device pointer
738 * @vco_min: minimum VCO frequency
739 * @vco_max: maximum VCO frequency
740 * @fb_factor: factor to multiply vco freq with
741 * @fb_mask: limit and bitmask for feedback divider
742 * @pd_min: post divider minimum
743 * @pd_max: post divider maximum
744 * @pd_even: post divider must be even above this value
745 * @optimal_fb_div: resulting feedback divider
746 * @optimal_vclk_div: resulting vclk post divider
747 * @optimal_dclk_div: resulting dclk post divider
749 * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
750 * Returns zero on success -EINVAL on error.
752 int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
753 unsigned vclk, unsigned dclk,
754 unsigned vco_min, unsigned vco_max,
755 unsigned fb_factor, unsigned fb_mask,
756 unsigned pd_min, unsigned pd_max,
758 unsigned *optimal_fb_div,
759 unsigned *optimal_vclk_div,
760 unsigned *optimal_dclk_div)
762 unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
764 /* start off with something large */
765 unsigned optimal_score = ~0;
767 /* loop through vco from low to high */
768 vco_min = max(max(vco_min, vclk), dclk);
769 for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
771 uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
772 unsigned vclk_div, dclk_div, score;
774 do_div(fb_div, ref_freq);
776 /* fb div out of range ? */
777 if (fb_div > fb_mask)
778 break; /* it can oly get worse */
782 /* calc vclk divider with current vco freq */
783 vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
785 if (vclk_div > pd_max)
786 break; /* vco is too big, it has to stop */
788 /* calc dclk divider with current vco freq */
789 dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
791 if (vclk_div > pd_max)
792 break; /* vco is too big, it has to stop */
794 /* calc score with current vco freq */
795 score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
797 /* determine if this vco setting is better than current optimal settings */
798 if (score < optimal_score) {
799 *optimal_fb_div = fb_div;
800 *optimal_vclk_div = vclk_div;
801 *optimal_dclk_div = dclk_div;
802 optimal_score = score;
803 if (optimal_score == 0)
804 break; /* it can't get better than this */
808 /* did we found a valid setup ? */
809 if (optimal_score == ~0)
815 int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
816 unsigned cg_upll_func_cntl)
820 /* make sure UPLL_CTLREQ is deasserted */
821 WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
825 /* assert UPLL_CTLREQ */
826 WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
828 /* wait for CTLACK and CTLACK2 to get asserted */
829 for (i = 0; i < 100; ++i) {
830 uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
831 if ((RREG32(cg_upll_func_cntl) & mask) == mask)
836 /* deassert UPLL_CTLREQ */
837 WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
840 DRM_ERROR("Timeout setting UVD clocks!\n");