Home | History | Annotate | Line # | Download | only in radeon
radeon_uvd.c revision 1.2.30.1
      1 /*	$NetBSD: radeon_uvd.c,v 1.2.30.1 2018/09/06 06:56:33 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright 2011 Advanced Micro Devices, Inc.
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sub license, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     18  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  *
     23  * The above copyright notice and this permission notice (including the
     24  * next paragraph) shall be included in all copies or substantial portions
     25  * of the Software.
     26  *
     27  */
     28 /*
     29  * Authors:
     30  *    Christian Knig <deathsimple (at) vodafone.de>
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: radeon_uvd.c,v 1.2.30.1 2018/09/06 06:56:33 pgoyette Exp $");
     35 
     36 #include <linux/firmware.h>
     37 #include <linux/module.h>
     38 #include <drm/drmP.h>
     39 #include <drm/drm.h>
     40 
     41 #include "radeon.h"
     42 #include "r600d.h"
     43 
     44 /* 1 second timeout */
     45 #define UVD_IDLE_TIMEOUT_MS	1000
     46 
     47 /* Firmware Names */
     48 #define FIRMWARE_R600		"radeon/R600_uvd.bin"
     49 #define FIRMWARE_RS780		"radeon/RS780_uvd.bin"
     50 #define FIRMWARE_RV770		"radeon/RV770_uvd.bin"
     51 #define FIRMWARE_RV710		"radeon/RV710_uvd.bin"
     52 #define FIRMWARE_CYPRESS	"radeon/CYPRESS_uvd.bin"
     53 #define FIRMWARE_SUMO		"radeon/SUMO_uvd.bin"
     54 #define FIRMWARE_TAHITI		"radeon/TAHITI_uvd.bin"
     55 #define FIRMWARE_BONAIRE	"radeon/BONAIRE_uvd.bin"
     56 
     57 MODULE_FIRMWARE(FIRMWARE_R600);
     58 MODULE_FIRMWARE(FIRMWARE_RS780);
     59 MODULE_FIRMWARE(FIRMWARE_RV770);
     60 MODULE_FIRMWARE(FIRMWARE_RV710);
     61 MODULE_FIRMWARE(FIRMWARE_CYPRESS);
     62 MODULE_FIRMWARE(FIRMWARE_SUMO);
     63 MODULE_FIRMWARE(FIRMWARE_TAHITI);
     64 MODULE_FIRMWARE(FIRMWARE_BONAIRE);
     65 
     66 static void radeon_uvd_idle_work_handler(struct work_struct *work);
     67 
     68 int radeon_uvd_init(struct radeon_device *rdev)
     69 {
     70 	unsigned long bo_size;
     71 	const char *fw_name;
     72 	int i, r;
     73 
     74 	INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
     75 
     76 	switch (rdev->family) {
     77 	case CHIP_RV610:
     78 	case CHIP_RV630:
     79 	case CHIP_RV670:
     80 	case CHIP_RV620:
     81 	case CHIP_RV635:
     82 		fw_name = FIRMWARE_R600;
     83 		break;
     84 
     85 	case CHIP_RS780:
     86 	case CHIP_RS880:
     87 		fw_name = FIRMWARE_RS780;
     88 		break;
     89 
     90 	case CHIP_RV770:
     91 		fw_name = FIRMWARE_RV770;
     92 		break;
     93 
     94 	case CHIP_RV710:
     95 	case CHIP_RV730:
     96 	case CHIP_RV740:
     97 		fw_name = FIRMWARE_RV710;
     98 		break;
     99 
    100 	case CHIP_CYPRESS:
    101 	case CHIP_HEMLOCK:
    102 	case CHIP_JUNIPER:
    103 	case CHIP_REDWOOD:
    104 	case CHIP_CEDAR:
    105 		fw_name = FIRMWARE_CYPRESS;
    106 		break;
    107 
    108 	case CHIP_SUMO:
    109 	case CHIP_SUMO2:
    110 	case CHIP_PALM:
    111 	case CHIP_CAYMAN:
    112 	case CHIP_BARTS:
    113 	case CHIP_TURKS:
    114 	case CHIP_CAICOS:
    115 		fw_name = FIRMWARE_SUMO;
    116 		break;
    117 
    118 	case CHIP_TAHITI:
    119 	case CHIP_VERDE:
    120 	case CHIP_PITCAIRN:
    121 	case CHIP_ARUBA:
    122 	case CHIP_OLAND:
    123 		fw_name = FIRMWARE_TAHITI;
    124 		break;
    125 
    126 	case CHIP_BONAIRE:
    127 	case CHIP_KABINI:
    128 	case CHIP_KAVERI:
    129 	case CHIP_HAWAII:
    130 	case CHIP_MULLINS:
    131 		fw_name = FIRMWARE_BONAIRE;
    132 		break;
    133 
    134 	default:
    135 		return -EINVAL;
    136 	}
    137 
    138 	r = request_firmware(&rdev->uvd_fw, fw_name, rdev->dev);
    139 	if (r) {
    140 		dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
    141 			fw_name);
    142 		return r;
    143 	}
    144 
    145 	bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
    146 		  RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE +
    147 		  RADEON_GPU_PAGE_SIZE;
    148 	r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
    149 			     RADEON_GEM_DOMAIN_VRAM, 0, NULL,
    150 			     NULL, &rdev->uvd.vcpu_bo);
    151 	if (r) {
    152 		dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
    153 		return r;
    154 	}
    155 
    156 	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
    157 	if (r) {
    158 		radeon_bo_unref(&rdev->uvd.vcpu_bo);
    159 		dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
    160 		return r;
    161 	}
    162 
    163 	r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
    164 			  &rdev->uvd.gpu_addr);
    165 	if (r) {
    166 		radeon_bo_unreserve(rdev->uvd.vcpu_bo);
    167 		radeon_bo_unref(&rdev->uvd.vcpu_bo);
    168 		dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
    169 		return r;
    170 	}
    171 
    172 	r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
    173 	if (r) {
    174 		dev_err(rdev->dev, "(%d) UVD map failed\n", r);
    175 		return r;
    176 	}
    177 
    178 	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
    179 
    180 	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
    181 		atomic_set(&rdev->uvd.handles[i], 0);
    182 		rdev->uvd.filp[i] = NULL;
    183 		rdev->uvd.img_size[i] = 0;
    184 	}
    185 
    186 	return 0;
    187 }
    188 
    189 void radeon_uvd_fini(struct radeon_device *rdev)
    190 {
    191 	int r;
    192 
    193 	if (rdev->uvd.vcpu_bo == NULL)
    194 		return;
    195 
    196 	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
    197 	if (!r) {
    198 		radeon_bo_kunmap(rdev->uvd.vcpu_bo);
    199 		radeon_bo_unpin(rdev->uvd.vcpu_bo);
    200 		radeon_bo_unreserve(rdev->uvd.vcpu_bo);
    201 	}
    202 
    203 	radeon_bo_unref(&rdev->uvd.vcpu_bo);
    204 
    205 	radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_UVD_INDEX]);
    206 
    207 	release_firmware(rdev->uvd_fw);
    208 }
    209 
    210 int radeon_uvd_suspend(struct radeon_device *rdev)
    211 {
    212 	int i, r;
    213 
    214 	if (rdev->uvd.vcpu_bo == NULL)
    215 		return 0;
    216 
    217 	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
    218 		uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
    219 		if (handle != 0) {
    220 			struct radeon_fence *fence;
    221 
    222 			radeon_uvd_note_usage(rdev);
    223 
    224 			r = radeon_uvd_get_destroy_msg(rdev,
    225 				R600_RING_TYPE_UVD_INDEX, handle, &fence);
    226 			if (r) {
    227 				DRM_ERROR("Error destroying UVD (%d)!\n", r);
    228 				continue;
    229 			}
    230 
    231 			radeon_fence_wait(fence, false);
    232 			radeon_fence_unref(&fence);
    233 
    234 			rdev->uvd.filp[i] = NULL;
    235 			atomic_set(&rdev->uvd.handles[i], 0);
    236 		}
    237 	}
    238 
    239 	return 0;
    240 }
    241 
    242 int radeon_uvd_resume(struct radeon_device *rdev)
    243 {
    244 	unsigned size;
    245 	uint8_t *ptr;
    246 
    247 	if (rdev->uvd.vcpu_bo == NULL)
    248 		return -EINVAL;
    249 
    250 	memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
    251 
    252 	size = radeon_bo_size(rdev->uvd.vcpu_bo);
    253 	size -= rdev->uvd_fw->size;
    254 
    255 	ptr = rdev->uvd.cpu_addr;
    256 	ptr += rdev->uvd_fw->size;
    257 
    258 	memset(ptr, 0, size);
    259 
    260 	return 0;
    261 }
    262 
    263 void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo,
    264 				       uint32_t allowed_domains)
    265 {
    266 	int i;
    267 
    268 	for (i = 0; i < rbo->placement.num_placement; ++i) {
    269 		rbo->placements[i].fpfn = 0 >> PAGE_SHIFT;
    270 		rbo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
    271 	}
    272 
    273 	/* If it must be in VRAM it must be in the first segment as well */
    274 	if (allowed_domains == RADEON_GEM_DOMAIN_VRAM)
    275 		return;
    276 
    277 	/* abort if we already have more than one placement */
    278 	if (rbo->placement.num_placement > 1)
    279 		return;
    280 
    281 	/* add another 256MB segment */
    282 	rbo->placements[1] = rbo->placements[0];
    283 	rbo->placements[1].fpfn += (256 * 1024 * 1024) >> PAGE_SHIFT;
    284 	rbo->placements[1].lpfn += (256 * 1024 * 1024) >> PAGE_SHIFT;
    285 	rbo->placement.num_placement++;
    286 	rbo->placement.num_busy_placement++;
    287 }
    288 
    289 void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
    290 {
    291 	int i, r;
    292 	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
    293 		uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
    294 		if (handle != 0 && rdev->uvd.filp[i] == filp) {
    295 			struct radeon_fence *fence;
    296 
    297 			radeon_uvd_note_usage(rdev);
    298 
    299 			r = radeon_uvd_get_destroy_msg(rdev,
    300 				R600_RING_TYPE_UVD_INDEX, handle, &fence);
    301 			if (r) {
    302 				DRM_ERROR("Error destroying UVD (%d)!\n", r);
    303 				continue;
    304 			}
    305 
    306 			radeon_fence_wait(fence, false);
    307 			radeon_fence_unref(&fence);
    308 
    309 			rdev->uvd.filp[i] = NULL;
    310 			atomic_set(&rdev->uvd.handles[i], 0);
    311 		}
    312 	}
    313 }
    314 
    315 static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
    316 {
    317 	unsigned stream_type = msg[4];
    318 	unsigned width = msg[6];
    319 	unsigned height = msg[7];
    320 	unsigned dpb_size = msg[9];
    321 	unsigned pitch = msg[28];
    322 
    323 	unsigned width_in_mb = width / 16;
    324 #ifdef __NetBSD__		/* XXX ALIGN means something else.  */
    325 	unsigned height_in_mb = round_up(height / 16, 2);
    326 #else
    327 	unsigned height_in_mb = ALIGN(height / 16, 2);
    328 #endif
    329 
    330 	unsigned image_size, tmp, min_dpb_size;
    331 
    332 	image_size = width * height;
    333 	image_size += image_size / 2;
    334 #ifdef __NetBSD__		/* XXX ALIGN means something else.  */
    335 	image_size = round_up(image_size, 1024);
    336 #else
    337 	image_size = ALIGN(image_size, 1024);
    338 #endif
    339 
    340 	switch (stream_type) {
    341 	case 0: /* H264 */
    342 
    343 		/* reference picture buffer */
    344 		min_dpb_size = image_size * 17;
    345 
    346 		/* macroblock context buffer */
    347 		min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
    348 
    349 		/* IT surface buffer */
    350 		min_dpb_size += width_in_mb * height_in_mb * 32;
    351 		break;
    352 
    353 	case 1: /* VC1 */
    354 
    355 		/* reference picture buffer */
    356 		min_dpb_size = image_size * 3;
    357 
    358 		/* CONTEXT_BUFFER */
    359 		min_dpb_size += width_in_mb * height_in_mb * 128;
    360 
    361 		/* IT surface buffer */
    362 		min_dpb_size += width_in_mb * 64;
    363 
    364 		/* DB surface buffer */
    365 		min_dpb_size += width_in_mb * 128;
    366 
    367 		/* BP */
    368 		tmp = max(width_in_mb, height_in_mb);
    369 #ifdef __NetBSD__		/* XXX ALIGN means something else.  */
    370 		min_dpb_size += round_up(tmp * 7 * 16, 64);
    371 #else
    372 		min_dpb_size += ALIGN(tmp * 7 * 16, 64);
    373 #endif
    374 		break;
    375 
    376 	case 3: /* MPEG2 */
    377 
    378 		/* reference picture buffer */
    379 		min_dpb_size = image_size * 3;
    380 		break;
    381 
    382 	case 4: /* MPEG4 */
    383 
    384 		/* reference picture buffer */
    385 		min_dpb_size = image_size * 3;
    386 
    387 		/* CM */
    388 		min_dpb_size += width_in_mb * height_in_mb * 64;
    389 
    390 		/* IT surface buffer */
    391 #ifdef __NetBSD__		/* XXX ALIGN means something else.  */
    392 		min_dpb_size += round_up(width_in_mb * height_in_mb * 32, 64);
    393 #else
    394 		min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
    395 #endif
    396 		break;
    397 
    398 	default:
    399 		DRM_ERROR("UVD codec not handled %d!\n", stream_type);
    400 		return -EINVAL;
    401 	}
    402 
    403 	if (width > pitch) {
    404 		DRM_ERROR("Invalid UVD decoding target pitch!\n");
    405 		return -EINVAL;
    406 	}
    407 
    408 	if (dpb_size < min_dpb_size) {
    409 		DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
    410 			  dpb_size, min_dpb_size);
    411 		return -EINVAL;
    412 	}
    413 
    414 	buf_sizes[0x1] = dpb_size;
    415 	buf_sizes[0x2] = image_size;
    416 	return 0;
    417 }
    418 
    419 static int radeon_uvd_validate_codec(struct radeon_cs_parser *p,
    420 				     unsigned stream_type)
    421 {
    422 	switch (stream_type) {
    423 	case 0: /* H264 */
    424 	case 1: /* VC1 */
    425 		/* always supported */
    426 		return 0;
    427 
    428 	case 3: /* MPEG2 */
    429 	case 4: /* MPEG4 */
    430 		/* only since UVD 3 */
    431 		if (p->rdev->family >= CHIP_PALM)
    432 			return 0;
    433 
    434 		/* fall through */
    435 	default:
    436 		DRM_ERROR("UVD codec not supported by hardware %d!\n",
    437 			  stream_type);
    438 		return -EINVAL;
    439 	}
    440 }
    441 
    442 static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
    443 			     unsigned offset, unsigned buf_sizes[])
    444 {
    445 	int32_t *msg, msg_type, handle;
    446 	unsigned img_size = 0;
    447 	struct fence *f;
    448 	void *ptr;
    449 
    450 	int i, r;
    451 
    452 	if (offset & 0x3F) {
    453 		DRM_ERROR("UVD messages must be 64 byte aligned!\n");
    454 		return -EINVAL;
    455 	}
    456 
    457 	f = reservation_object_get_excl(bo->tbo.resv);
    458 	if (f) {
    459 		r = radeon_fence_wait((struct radeon_fence *)f, false);
    460 		if (r) {
    461 			DRM_ERROR("Failed waiting for UVD message (%d)!\n", r);
    462 			return r;
    463 		}
    464 	}
    465 
    466 	r = radeon_bo_kmap(bo, &ptr);
    467 	if (r) {
    468 		DRM_ERROR("Failed mapping the UVD message (%d)!\n", r);
    469 		return r;
    470 	}
    471 
    472 	msg = (int32_t *)((uint8_t *)ptr + offset);
    473 
    474 	msg_type = msg[1];
    475 	handle = msg[2];
    476 
    477 	if (handle == 0) {
    478 		DRM_ERROR("Invalid UVD handle!\n");
    479 		return -EINVAL;
    480 	}
    481 
    482 	switch (msg_type) {
    483 	case 0:
    484 		/* it's a create msg, calc image size (width * height) */
    485 		img_size = msg[7] * msg[8];
    486 
    487 		r = radeon_uvd_validate_codec(p, msg[4]);
    488 		radeon_bo_kunmap(bo);
    489 		if (r)
    490 			return r;
    491 
    492 		/* try to alloc a new handle */
    493 		for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
    494 			if (atomic_read(&p->rdev->uvd.handles[i]) == handle) {
    495 				DRM_ERROR("Handle 0x%x already in use!\n", handle);
    496 				return -EINVAL;
    497 			}
    498 
    499 			if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
    500 				p->rdev->uvd.filp[i] = p->filp;
    501 				p->rdev->uvd.img_size[i] = img_size;
    502 				return 0;
    503 			}
    504 		}
    505 
    506 		DRM_ERROR("No more free UVD handles!\n");
    507 		return -EINVAL;
    508 
    509 	case 1:
    510 		/* it's a decode msg, validate codec and calc buffer sizes */
    511 		r = radeon_uvd_validate_codec(p, msg[4]);
    512 		if (!r)
    513 			r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
    514 		radeon_bo_kunmap(bo);
    515 		if (r)
    516 			return r;
    517 
    518 		/* validate the handle */
    519 		for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
    520 			if (atomic_read(&p->rdev->uvd.handles[i]) == handle) {
    521 				if (p->rdev->uvd.filp[i] != p->filp) {
    522 					DRM_ERROR("UVD handle collision detected!\n");
    523 					return -EINVAL;
    524 				}
    525 				return 0;
    526 			}
    527 		}
    528 
    529 		DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
    530 		return -ENOENT;
    531 
    532 	case 2:
    533 		/* it's a destroy msg, free the handle */
    534 		for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
    535 			atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
    536 		radeon_bo_kunmap(bo);
    537 		return 0;
    538 
    539 	default:
    540 
    541 		DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
    542 		return -EINVAL;
    543 	}
    544 
    545 	BUG();
    546 	return -EINVAL;
    547 }
    548 
    549 static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
    550 			       int data0, int data1,
    551 			       unsigned buf_sizes[], bool *has_msg_cmd)
    552 {
    553 	struct radeon_cs_chunk *relocs_chunk;
    554 	struct radeon_bo_list *reloc;
    555 	unsigned idx, cmd, offset;
    556 	uint64_t start, end;
    557 	int r;
    558 
    559 	relocs_chunk = p->chunk_relocs;
    560 	offset = radeon_get_ib_value(p, data0);
    561 	idx = radeon_get_ib_value(p, data1);
    562 	if (idx >= relocs_chunk->length_dw) {
    563 		DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
    564 			  idx, relocs_chunk->length_dw);
    565 		return -EINVAL;
    566 	}
    567 
    568 	reloc = &p->relocs[(idx / 4)];
    569 	start = reloc->gpu_offset;
    570 	end = start + radeon_bo_size(reloc->robj);
    571 	start += offset;
    572 
    573 	p->ib.ptr[data0] = start & 0xFFFFFFFF;
    574 	p->ib.ptr[data1] = start >> 32;
    575 
    576 	cmd = radeon_get_ib_value(p, p->idx) >> 1;
    577 
    578 	if (cmd < 0x4) {
    579 		if (end <= start) {
    580 			DRM_ERROR("invalid reloc offset %X!\n", offset);
    581 			return -EINVAL;
    582 		}
    583 		if ((end - start) < buf_sizes[cmd]) {
    584 			DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
    585 				  (unsigned)(end - start), buf_sizes[cmd]);
    586 			return -EINVAL;
    587 		}
    588 
    589 	} else if (cmd != 0x100) {
    590 		DRM_ERROR("invalid UVD command %X!\n", cmd);
    591 		return -EINVAL;
    592 	}
    593 
    594 	if ((start >> 28) != ((end - 1) >> 28)) {
    595 		DRM_ERROR("reloc %"PRIX64"-%"PRIX64" crossing 256MB boundary!\n",
    596 			  start, end);
    597 		return -EINVAL;
    598 	}
    599 
    600 	/* TODO: is this still necessary on NI+ ? */
    601 	if ((cmd == 0 || cmd == 0x3) &&
    602 	    (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
    603 		DRM_ERROR("msg/fb buffer %"PRIX64"-%"PRIX64" out of 256MB segment!\n",
    604 			  start, end);
    605 		return -EINVAL;
    606 	}
    607 
    608 	if (cmd == 0) {
    609 		if (*has_msg_cmd) {
    610 			DRM_ERROR("More than one message in a UVD-IB!\n");
    611 			return -EINVAL;
    612 		}
    613 		*has_msg_cmd = true;
    614 		r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
    615 		if (r)
    616 			return r;
    617 	} else if (!*has_msg_cmd) {
    618 		DRM_ERROR("Message needed before other commands are send!\n");
    619 		return -EINVAL;
    620 	}
    621 
    622 	return 0;
    623 }
    624 
    625 static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
    626 			     struct radeon_cs_packet *pkt,
    627 			     int *data0, int *data1,
    628 			     unsigned buf_sizes[],
    629 			     bool *has_msg_cmd)
    630 {
    631 	int i, r;
    632 
    633 	p->idx++;
    634 	for (i = 0; i <= pkt->count; ++i) {
    635 		switch (pkt->reg + i*4) {
    636 		case UVD_GPCOM_VCPU_DATA0:
    637 			*data0 = p->idx;
    638 			break;
    639 		case UVD_GPCOM_VCPU_DATA1:
    640 			*data1 = p->idx;
    641 			break;
    642 		case UVD_GPCOM_VCPU_CMD:
    643 			r = radeon_uvd_cs_reloc(p, *data0, *data1,
    644 						buf_sizes, has_msg_cmd);
    645 			if (r)
    646 				return r;
    647 			break;
    648 		case UVD_ENGINE_CNTL:
    649 			break;
    650 		default:
    651 			DRM_ERROR("Invalid reg 0x%X!\n",
    652 				  pkt->reg + i*4);
    653 			return -EINVAL;
    654 		}
    655 		p->idx++;
    656 	}
    657 	return 0;
    658 }
    659 
    660 int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
    661 {
    662 	struct radeon_cs_packet pkt;
    663 	int r, data0 = 0, data1 = 0;
    664 
    665 	/* does the IB has a msg command */
    666 	bool has_msg_cmd = false;
    667 
    668 	/* minimum buffer sizes */
    669 	unsigned buf_sizes[] = {
    670 		[0x00000000]	=	2048,
    671 		[0x00000001]	=	32 * 1024 * 1024,
    672 		[0x00000002]	=	2048 * 1152 * 3,
    673 		[0x00000003]	=	2048,
    674 	};
    675 
    676 	if (p->chunk_ib->length_dw % 16) {
    677 		DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
    678 			  p->chunk_ib->length_dw);
    679 		return -EINVAL;
    680 	}
    681 
    682 	if (p->chunk_relocs == NULL) {
    683 		DRM_ERROR("No relocation chunk !\n");
    684 		return -EINVAL;
    685 	}
    686 
    687 
    688 	do {
    689 		r = radeon_cs_packet_parse(p, &pkt, p->idx);
    690 		if (r)
    691 			return r;
    692 		switch (pkt.type) {
    693 		case RADEON_PACKET_TYPE0:
    694 			r = radeon_uvd_cs_reg(p, &pkt, &data0, &data1,
    695 					      buf_sizes, &has_msg_cmd);
    696 			if (r)
    697 				return r;
    698 			break;
    699 		case RADEON_PACKET_TYPE2:
    700 			p->idx += pkt.count + 2;
    701 			break;
    702 		default:
    703 			DRM_ERROR("Unknown packet type %d !\n", pkt.type);
    704 			return -EINVAL;
    705 		}
    706 	} while (p->idx < p->chunk_ib->length_dw);
    707 
    708 	if (!has_msg_cmd) {
    709 		DRM_ERROR("UVD-IBs need a msg command!\n");
    710 		return -EINVAL;
    711 	}
    712 
    713 	return 0;
    714 }
    715 
    716 static int radeon_uvd_send_msg(struct radeon_device *rdev,
    717 			       int ring, uint64_t addr,
    718 			       struct radeon_fence **fence)
    719 {
    720 	struct radeon_ib ib;
    721 	int i, r;
    722 
    723 	r = radeon_ib_get(rdev, ring, &ib, NULL, 64);
    724 	if (r)
    725 		return r;
    726 
    727 	ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
    728 	ib.ptr[1] = addr;
    729 	ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
    730 	ib.ptr[3] = addr >> 32;
    731 	ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
    732 	ib.ptr[5] = 0;
    733 	for (i = 6; i < 16; ++i)
    734 		ib.ptr[i] = PACKET2(0);
    735 	ib.length_dw = 16;
    736 
    737 	r = radeon_ib_schedule(rdev, &ib, NULL, false);
    738 
    739 	if (fence)
    740 		*fence = radeon_fence_ref(ib.fence);
    741 
    742 	radeon_ib_free(rdev, &ib);
    743 	return r;
    744 }
    745 
    746 /* multiple fence commands without any stream commands in between can
    747    crash the vcpu so just try to emmit a dummy create/destroy msg to
    748    avoid this */
    749 int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
    750 			      uint32_t handle, struct radeon_fence **fence)
    751 {
    752 	/* we use the last page of the vcpu bo for the UVD message */
    753 	uint64_t offs = radeon_bo_size(rdev->uvd.vcpu_bo) -
    754 		RADEON_GPU_PAGE_SIZE;
    755 
    756 	uint32_t *msg = (void *)((char *)rdev->uvd.cpu_addr + offs);
    757 	uint64_t addr = rdev->uvd.gpu_addr + offs;
    758 
    759 	int r, i;
    760 
    761 	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, true);
    762 	if (r)
    763 		return r;
    764 
    765 	/* stitch together an UVD create msg */
    766 	msg[0] = cpu_to_le32(0x00000de4);
    767 	msg[1] = cpu_to_le32(0x00000000);
    768 	msg[2] = cpu_to_le32(handle);
    769 	msg[3] = cpu_to_le32(0x00000000);
    770 	msg[4] = cpu_to_le32(0x00000000);
    771 	msg[5] = cpu_to_le32(0x00000000);
    772 	msg[6] = cpu_to_le32(0x00000000);
    773 	msg[7] = cpu_to_le32(0x00000780);
    774 	msg[8] = cpu_to_le32(0x00000440);
    775 	msg[9] = cpu_to_le32(0x00000000);
    776 	msg[10] = cpu_to_le32(0x01b37000);
    777 	for (i = 11; i < 1024; ++i)
    778 		msg[i] = cpu_to_le32(0x0);
    779 
    780 	r = radeon_uvd_send_msg(rdev, ring, addr, fence);
    781 	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
    782 	return r;
    783 }
    784 
    785 int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
    786 			       uint32_t handle, struct radeon_fence **fence)
    787 {
    788 	/* we use the last page of the vcpu bo for the UVD message */
    789 	uint64_t offs = radeon_bo_size(rdev->uvd.vcpu_bo) -
    790 		RADEON_GPU_PAGE_SIZE;
    791 
    792 	uint32_t *msg = (void *)((char *)rdev->uvd.cpu_addr + offs);
    793 	uint64_t addr = rdev->uvd.gpu_addr + offs;
    794 
    795 	int r, i;
    796 
    797 	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, true);
    798 	if (r)
    799 		return r;
    800 
    801 	/* stitch together an UVD destroy msg */
    802 	msg[0] = cpu_to_le32(0x00000de4);
    803 	msg[1] = cpu_to_le32(0x00000002);
    804 	msg[2] = cpu_to_le32(handle);
    805 	msg[3] = cpu_to_le32(0x00000000);
    806 	for (i = 4; i < 1024; ++i)
    807 		msg[i] = cpu_to_le32(0x0);
    808 
    809 	r = radeon_uvd_send_msg(rdev, ring, addr, fence);
    810 	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
    811 	return r;
    812 }
    813 
    814 /**
    815  * radeon_uvd_count_handles - count number of open streams
    816  *
    817  * @rdev: radeon_device pointer
    818  * @sd: number of SD streams
    819  * @hd: number of HD streams
    820  *
    821  * Count the number of open SD/HD streams as a hint for power mangement
    822  */
    823 static void radeon_uvd_count_handles(struct radeon_device *rdev,
    824 				     unsigned *sd, unsigned *hd)
    825 {
    826 	unsigned i;
    827 
    828 	*sd = 0;
    829 	*hd = 0;
    830 
    831 	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
    832 		if (!atomic_read(&rdev->uvd.handles[i]))
    833 			continue;
    834 
    835 		if (rdev->uvd.img_size[i] >= 720*576)
    836 			++(*hd);
    837 		else
    838 			++(*sd);
    839 	}
    840 }
    841 
    842 static void radeon_uvd_idle_work_handler(struct work_struct *work)
    843 {
    844 	struct radeon_device *rdev =
    845 		container_of(work, struct radeon_device, uvd.idle_work.work);
    846 
    847 	if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0) {
    848 		if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
    849 			radeon_uvd_count_handles(rdev, &rdev->pm.dpm.sd,
    850 						 &rdev->pm.dpm.hd);
    851 			radeon_dpm_enable_uvd(rdev, false);
    852 		} else {
    853 			radeon_set_uvd_clocks(rdev, 0, 0);
    854 		}
    855 	} else {
    856 		schedule_delayed_work(&rdev->uvd.idle_work,
    857 				      msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
    858 	}
    859 }
    860 
    861 void radeon_uvd_note_usage(struct radeon_device *rdev)
    862 {
    863 	bool streams_changed = false;
    864 	bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
    865 	set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
    866 					    msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
    867 
    868 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
    869 		unsigned hd = 0, sd = 0;
    870 		radeon_uvd_count_handles(rdev, &sd, &hd);
    871 		if ((rdev->pm.dpm.sd != sd) ||
    872 		    (rdev->pm.dpm.hd != hd)) {
    873 			rdev->pm.dpm.sd = sd;
    874 			rdev->pm.dpm.hd = hd;
    875 			/* disable this for now */
    876 			/*streams_changed = true;*/
    877 		}
    878 	}
    879 
    880 	if (set_clocks || streams_changed) {
    881 		if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
    882 			radeon_dpm_enable_uvd(rdev, true);
    883 		} else {
    884 			radeon_set_uvd_clocks(rdev, 53300, 40000);
    885 		}
    886 	}
    887 }
    888 
    889 static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
    890 					      unsigned target_freq,
    891 					      unsigned pd_min,
    892 					      unsigned pd_even)
    893 {
    894 	unsigned post_div = vco_freq / target_freq;
    895 
    896 	/* adjust to post divider minimum value */
    897 	if (post_div < pd_min)
    898 		post_div = pd_min;
    899 
    900 	/* we alway need a frequency less than or equal the target */
    901 	if ((vco_freq / post_div) > target_freq)
    902 		post_div += 1;
    903 
    904 	/* post dividers above a certain value must be even */
    905 	if (post_div > pd_even && post_div % 2)
    906 		post_div += 1;
    907 
    908 	return post_div;
    909 }
    910 
    911 /**
    912  * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
    913  *
    914  * @rdev: radeon_device pointer
    915  * @vclk: wanted VCLK
    916  * @dclk: wanted DCLK
    917  * @vco_min: minimum VCO frequency
    918  * @vco_max: maximum VCO frequency
    919  * @fb_factor: factor to multiply vco freq with
    920  * @fb_mask: limit and bitmask for feedback divider
    921  * @pd_min: post divider minimum
    922  * @pd_max: post divider maximum
    923  * @pd_even: post divider must be even above this value
    924  * @optimal_fb_div: resulting feedback divider
    925  * @optimal_vclk_div: resulting vclk post divider
    926  * @optimal_dclk_div: resulting dclk post divider
    927  *
    928  * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
    929  * Returns zero on success -EINVAL on error.
    930  */
    931 int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
    932 				  unsigned vclk, unsigned dclk,
    933 				  unsigned vco_min, unsigned vco_max,
    934 				  unsigned fb_factor, unsigned fb_mask,
    935 				  unsigned pd_min, unsigned pd_max,
    936 				  unsigned pd_even,
    937 				  unsigned *optimal_fb_div,
    938 				  unsigned *optimal_vclk_div,
    939 				  unsigned *optimal_dclk_div)
    940 {
    941 	unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
    942 
    943 	/* start off with something large */
    944 	unsigned optimal_score = ~0;
    945 
    946 	/* loop through vco from low to high */
    947 	vco_min = max(max(vco_min, vclk), dclk);
    948 	for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
    949 
    950 		uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
    951 		unsigned vclk_div, dclk_div, score;
    952 
    953 		do_div(fb_div, ref_freq);
    954 
    955 		/* fb div out of range ? */
    956 		if (fb_div > fb_mask)
    957 			break; /* it can oly get worse */
    958 
    959 		fb_div &= fb_mask;
    960 
    961 		/* calc vclk divider with current vco freq */
    962 		vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
    963 							 pd_min, pd_even);
    964 		if (vclk_div > pd_max)
    965 			break; /* vco is too big, it has to stop */
    966 
    967 		/* calc dclk divider with current vco freq */
    968 		dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
    969 							 pd_min, pd_even);
    970 		if (dclk_div > pd_max)
    971 			break; /* vco is too big, it has to stop */
    972 
    973 		/* calc score with current vco freq */
    974 		score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
    975 
    976 		/* determine if this vco setting is better than current optimal settings */
    977 		if (score < optimal_score) {
    978 			*optimal_fb_div = fb_div;
    979 			*optimal_vclk_div = vclk_div;
    980 			*optimal_dclk_div = dclk_div;
    981 			optimal_score = score;
    982 			if (optimal_score == 0)
    983 				break; /* it can't get better than this */
    984 		}
    985 	}
    986 
    987 	/* did we found a valid setup ? */
    988 	if (optimal_score == ~0)
    989 		return -EINVAL;
    990 
    991 	return 0;
    992 }
    993 
    994 int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
    995 				unsigned cg_upll_func_cntl)
    996 {
    997 	unsigned i;
    998 
    999 	/* make sure UPLL_CTLREQ is deasserted */
   1000 	WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
   1001 
   1002 	mdelay(10);
   1003 
   1004 	/* assert UPLL_CTLREQ */
   1005 	WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
   1006 
   1007 	/* wait for CTLACK and CTLACK2 to get asserted */
   1008 	for (i = 0; i < 100; ++i) {
   1009 		uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
   1010 		if ((RREG32(cg_upll_func_cntl) & mask) == mask)
   1011 			break;
   1012 		mdelay(10);
   1013 	}
   1014 
   1015 	/* deassert UPLL_CTLREQ */
   1016 	WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
   1017 
   1018 	if (i == 100) {
   1019 		DRM_ERROR("Timeout setting UVD clocks!\n");
   1020 		return -ETIMEDOUT;
   1021 	}
   1022 
   1023 	return 0;
   1024 }
   1025