1 /* 2 * Copyright 2013 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 * Authors: Christian König <christian.koenig@amd.com> 26 */ 27 28 #include <linux/firmware.h> 29 #include <linux/module.h> 30 31 #include <drm/drm.h> 32 #include <drm/drm_drv.h> 33 34 #include "amdgpu.h" 35 #include "amdgpu_pm.h" 36 #include "amdgpu_vce.h" 37 #include "amdgpu_cs.h" 38 #include "cikd.h" 39 40 /* 1 second timeout */ 41 #define VCE_IDLE_TIMEOUT msecs_to_jiffies(1000) 42 43 /* Firmware Names */ 44 #ifdef CONFIG_DRM_AMDGPU_SI 45 #define FIRMWARE_VCE_V1_0 "amdgpu/vce_1_0_0.bin" 46 #endif 47 #ifdef CONFIG_DRM_AMDGPU_CIK 48 #define FIRMWARE_BONAIRE "amdgpu/bonaire_vce.bin" 49 #define FIRMWARE_KABINI "amdgpu/kabini_vce.bin" 50 #define FIRMWARE_KAVERI "amdgpu/kaveri_vce.bin" 51 #define FIRMWARE_HAWAII "amdgpu/hawaii_vce.bin" 52 #define FIRMWARE_MULLINS "amdgpu/mullins_vce.bin" 53 #endif 54 #define FIRMWARE_TONGA "amdgpu/tonga_vce.bin" 55 #define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin" 56 #define FIRMWARE_FIJI "amdgpu/fiji_vce.bin" 57 #define FIRMWARE_STONEY "amdgpu/stoney_vce.bin" 58 #define FIRMWARE_POLARIS10 "amdgpu/polaris10_vce.bin" 59 #define FIRMWARE_POLARIS11 "amdgpu/polaris11_vce.bin" 60 #define FIRMWARE_POLARIS12 "amdgpu/polaris12_vce.bin" 61 #define FIRMWARE_VEGAM "amdgpu/vegam_vce.bin" 62 63 #define FIRMWARE_VEGA10 "amdgpu/vega10_vce.bin" 64 #define FIRMWARE_VEGA12 "amdgpu/vega12_vce.bin" 65 #define FIRMWARE_VEGA20 "amdgpu/vega20_vce.bin" 66 67 #ifdef CONFIG_DRM_AMDGPU_SI 68 MODULE_FIRMWARE(FIRMWARE_VCE_V1_0); 69 #endif 70 #ifdef CONFIG_DRM_AMDGPU_CIK 71 MODULE_FIRMWARE(FIRMWARE_BONAIRE); 72 MODULE_FIRMWARE(FIRMWARE_KABINI); 73 MODULE_FIRMWARE(FIRMWARE_KAVERI); 74 MODULE_FIRMWARE(FIRMWARE_HAWAII); 75 MODULE_FIRMWARE(FIRMWARE_MULLINS); 76 #endif 77 MODULE_FIRMWARE(FIRMWARE_TONGA); 78 MODULE_FIRMWARE(FIRMWARE_CARRIZO); 79 MODULE_FIRMWARE(FIRMWARE_FIJI); 80 MODULE_FIRMWARE(FIRMWARE_STONEY); 81 MODULE_FIRMWARE(FIRMWARE_POLARIS10); 82 MODULE_FIRMWARE(FIRMWARE_POLARIS11); 83 MODULE_FIRMWARE(FIRMWARE_POLARIS12); 84 MODULE_FIRMWARE(FIRMWARE_VEGAM); 85 86 MODULE_FIRMWARE(FIRMWARE_VEGA10); 87 MODULE_FIRMWARE(FIRMWARE_VEGA12); 88 MODULE_FIRMWARE(FIRMWARE_VEGA20); 89 90 static void amdgpu_vce_idle_work_handler(struct work_struct *work); 91 static int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle, 92 struct dma_fence **fence); 93 static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle, 94 bool direct, struct dma_fence **fence); 95 96 /** 97 * amdgpu_vce_firmware_name() - determine the firmware file name for VCE 98 * 99 * @adev: amdgpu_device pointer 100 * 101 * Each chip that has VCE IP may need a different firmware. 102 * This function returns the name of the VCE firmware file 103 * appropriate for the current chip. 104 */ 105 static const char *amdgpu_vce_firmware_name(struct amdgpu_device *adev) 106 { 107 switch (adev->asic_type) { 108 #ifdef CONFIG_DRM_AMDGPU_SI 109 case CHIP_PITCAIRN: 110 case CHIP_TAHITI: 111 case CHIP_VERDE: 112 return FIRMWARE_VCE_V1_0; 113 #endif 114 #ifdef CONFIG_DRM_AMDGPU_CIK 115 case CHIP_BONAIRE: 116 return FIRMWARE_BONAIRE; 117 case CHIP_KAVERI: 118 return FIRMWARE_KAVERI; 119 case CHIP_KABINI: 120 return FIRMWARE_KABINI; 121 case CHIP_HAWAII: 122 return FIRMWARE_HAWAII; 123 case CHIP_MULLINS: 124 return FIRMWARE_MULLINS; 125 #endif 126 case CHIP_TONGA: 127 return FIRMWARE_TONGA; 128 case CHIP_CARRIZO: 129 return FIRMWARE_CARRIZO; 130 case CHIP_FIJI: 131 return FIRMWARE_FIJI; 132 case CHIP_STONEY: 133 return FIRMWARE_STONEY; 134 case CHIP_POLARIS10: 135 return FIRMWARE_POLARIS10; 136 case CHIP_POLARIS11: 137 return FIRMWARE_POLARIS11; 138 case CHIP_POLARIS12: 139 return FIRMWARE_POLARIS12; 140 case CHIP_VEGAM: 141 return FIRMWARE_VEGAM; 142 case CHIP_VEGA10: 143 return FIRMWARE_VEGA10; 144 case CHIP_VEGA12: 145 return FIRMWARE_VEGA12; 146 case CHIP_VEGA20: 147 return FIRMWARE_VEGA20; 148 149 default: 150 return NULL; 151 } 152 } 153 154 /** 155 * amdgpu_vce_early_init() - try to load VCE firmware 156 * 157 * @adev: amdgpu_device pointer 158 * 159 * Tries to load the VCE firmware. 160 * 161 * When not found, returns ENOENT so that the driver can 162 * still load and initialize the rest of the IP blocks. 163 * The GPU can function just fine without VCE, they will just 164 * not support video encoding. 165 */ 166 int amdgpu_vce_early_init(struct amdgpu_device *adev) 167 { 168 const char *fw_name = amdgpu_vce_firmware_name(adev); 169 const struct common_firmware_header *hdr; 170 unsigned int ucode_version, version_major, version_minor, binary_id; 171 int r; 172 173 if (!fw_name) 174 return -ENOENT; 175 176 r = amdgpu_ucode_request(adev, &adev->vce.fw, AMDGPU_UCODE_REQUIRED, "%s", fw_name); 177 if (r) { 178 dev_err(adev->dev, 179 "amdgpu_vce: Firmware \"%s\" not found or failed to validate (%d)\n", 180 fw_name, r); 181 182 amdgpu_ucode_release(&adev->vce.fw); 183 return -ENOENT; 184 } 185 186 hdr = (const struct common_firmware_header *)adev->vce.fw->data; 187 188 ucode_version = le32_to_cpu(hdr->ucode_version); 189 version_major = (ucode_version >> 20) & 0xfff; 190 version_minor = (ucode_version >> 8) & 0xfff; 191 binary_id = ucode_version & 0xff; 192 dev_info(adev->dev, "Found VCE firmware Version: %d.%d Binary ID: %d\n", 193 version_major, version_minor, binary_id); 194 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) | 195 (binary_id << 8)); 196 197 return 0; 198 } 199 200 /** 201 * amdgpu_vce_sw_init() - allocate memory for VCE BO 202 * 203 * @adev: amdgpu_device pointer 204 * @size: size for the new BO 205 * 206 * First step to get VCE online: allocate memory for VCE BO. 207 * The VCE firmware binary is copied into the VCE BO later, 208 * in amdgpu_vce_resume. The VCE executes its code from the 209 * VCE BO and also uses the space in this BO for its stack and data. 210 * 211 * Ideally this BO should be placed in VRAM for optimal performance, 212 * although technically it also runs from system RAM (albeit slowly). 213 */ 214 int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size) 215 { 216 int i, r; 217 218 if (!adev->vce.fw) 219 return -ENOENT; 220 221 r = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE, 222 AMDGPU_GEM_DOMAIN_VRAM | 223 AMDGPU_GEM_DOMAIN_GTT, 224 &adev->vce.vcpu_bo, 225 &adev->vce.gpu_addr, &adev->vce.cpu_addr); 226 if (r) { 227 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r); 228 return r; 229 } 230 231 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { 232 atomic_set(&adev->vce.handles[i], 0); 233 adev->vce.filp[i] = NULL; 234 } 235 236 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler); 237 mutex_init(&adev->vce.idle_mutex); 238 239 return 0; 240 } 241 242 /** 243 * amdgpu_vce_sw_fini - free memory 244 * 245 * @adev: amdgpu_device pointer 246 * 247 * Last step on VCE teardown, free firmware memory 248 */ 249 int amdgpu_vce_sw_fini(struct amdgpu_device *adev) 250 { 251 unsigned int i; 252 253 if (adev->vce.vcpu_bo == NULL) 254 return 0; 255 256 if (adev->vce.entity.rq) 257 drm_sched_entity_destroy(&adev->vce.entity); 258 259 for (i = 0; i < adev->vce.num_rings; i++) 260 amdgpu_ring_fini(&adev->vce.ring[i]); 261 262 amdgpu_ucode_release(&adev->vce.fw); 263 mutex_destroy(&adev->vce.idle_mutex); 264 265 amdgpu_bo_free_kernel(&adev->vce.vcpu_bo, &adev->vce.gpu_addr, 266 (void **)&adev->vce.cpu_addr); 267 268 return 0; 269 } 270 271 /** 272 * amdgpu_vce_entity_init - init entity 273 * 274 * @adev: amdgpu_device pointer 275 * @ring: amdgpu_ring pointer to check 276 * 277 * Initialize the entity used for handle management in the kernel driver. 278 */ 279 int amdgpu_vce_entity_init(struct amdgpu_device *adev, struct amdgpu_ring *ring) 280 { 281 if (ring == &adev->vce.ring[0]) { 282 struct drm_gpu_scheduler *sched = &ring->sched; 283 int r; 284 285 r = drm_sched_entity_init(&adev->vce.entity, DRM_SCHED_PRIORITY_NORMAL, 286 &sched, 1, NULL); 287 if (r != 0) { 288 DRM_ERROR("Failed setting up VCE run queue.\n"); 289 return r; 290 } 291 } 292 293 return 0; 294 } 295 296 /** 297 * amdgpu_vce_suspend - unpin VCE fw memory 298 * 299 * @adev: amdgpu_device pointer 300 * 301 */ 302 int amdgpu_vce_suspend(struct amdgpu_device *adev) 303 { 304 int i; 305 306 cancel_delayed_work_sync(&adev->vce.idle_work); 307 308 if (adev->vce.vcpu_bo == NULL) 309 return 0; 310 311 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) 312 if (atomic_read(&adev->vce.handles[i])) 313 break; 314 315 if (i == AMDGPU_MAX_VCE_HANDLES) 316 return 0; 317 318 /* TODO: suspending running encoding sessions isn't supported */ 319 return -EINVAL; 320 } 321 322 /** 323 * amdgpu_vce_resume - pin VCE fw memory 324 * 325 * @adev: amdgpu_device pointer 326 * 327 */ 328 int amdgpu_vce_resume(struct amdgpu_device *adev) 329 { 330 const struct common_firmware_header *hdr; 331 unsigned int offset; 332 int idx; 333 334 if (adev->vce.vcpu_bo == NULL) 335 return -EINVAL; 336 337 hdr = (const struct common_firmware_header *)adev->vce.fw->data; 338 offset = le32_to_cpu(hdr->ucode_array_offset_bytes); 339 340 if (drm_dev_enter(adev_to_drm(adev), &idx)) { 341 memset_io(adev->vce.cpu_addr, 0, amdgpu_bo_size(adev->vce.vcpu_bo)); 342 memcpy_toio(adev->vce.cpu_addr, adev->vce.fw->data + offset, 343 adev->vce.fw->size - offset); 344 drm_dev_exit(idx); 345 } 346 347 return 0; 348 } 349 350 /** 351 * amdgpu_vce_idle_work_handler - power off VCE 352 * 353 * @work: pointer to work structure 354 * 355 * power of VCE when it's not used any more 356 */ 357 static void amdgpu_vce_idle_work_handler(struct work_struct *work) 358 { 359 struct amdgpu_device *adev = 360 container_of(work, struct amdgpu_device, vce.idle_work.work); 361 unsigned int i, count = 0; 362 363 for (i = 0; i < adev->vce.num_rings; i++) 364 count += amdgpu_fence_count_emitted(&adev->vce.ring[i]); 365 366 if (count == 0) { 367 if (adev->pm.dpm_enabled) { 368 amdgpu_dpm_enable_vce(adev, false); 369 } else { 370 amdgpu_asic_set_vce_clocks(adev, 0, 0); 371 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE, 372 AMD_PG_STATE_GATE); 373 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE, 374 AMD_CG_STATE_GATE); 375 } 376 } else { 377 schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT); 378 } 379 } 380 381 /** 382 * amdgpu_vce_ring_begin_use - power up VCE 383 * 384 * @ring: amdgpu ring 385 * 386 * Make sure VCE is powerd up when we want to use it 387 */ 388 void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring) 389 { 390 struct amdgpu_device *adev = ring->adev; 391 bool set_clocks; 392 393 if (amdgpu_sriov_vf(adev)) 394 return; 395 396 mutex_lock(&adev->vce.idle_mutex); 397 set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work); 398 if (set_clocks) { 399 if (adev->pm.dpm_enabled) { 400 amdgpu_dpm_enable_vce(adev, true); 401 } else { 402 amdgpu_asic_set_vce_clocks(adev, 53300, 40000); 403 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE, 404 AMD_CG_STATE_UNGATE); 405 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE, 406 AMD_PG_STATE_UNGATE); 407 408 } 409 } 410 mutex_unlock(&adev->vce.idle_mutex); 411 } 412 413 /** 414 * amdgpu_vce_ring_end_use - power VCE down 415 * 416 * @ring: amdgpu ring 417 * 418 * Schedule work to power VCE down again 419 */ 420 void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring) 421 { 422 if (!amdgpu_sriov_vf(ring->adev)) 423 schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT); 424 } 425 426 /** 427 * amdgpu_vce_free_handles - free still open VCE handles 428 * 429 * @adev: amdgpu_device pointer 430 * @filp: drm file pointer 431 * 432 * Close all VCE handles still open by this file pointer 433 */ 434 void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp) 435 { 436 struct amdgpu_ring *ring = &adev->vce.ring[0]; 437 int i, r; 438 439 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { 440 uint32_t handle = atomic_read(&adev->vce.handles[i]); 441 442 if (!handle || adev->vce.filp[i] != filp) 443 continue; 444 445 r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL); 446 if (r) 447 DRM_ERROR("Error destroying VCE handle (%d)!\n", r); 448 449 adev->vce.filp[i] = NULL; 450 atomic_set(&adev->vce.handles[i], 0); 451 } 452 } 453 454 /** 455 * amdgpu_vce_get_create_msg - generate a VCE create msg 456 * 457 * @ring: ring we should submit the msg to 458 * @handle: VCE session handle to use 459 * @fence: optional fence to return 460 * 461 * Open up a stream for HW test 462 */ 463 static int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle, 464 struct dma_fence **fence) 465 { 466 const unsigned int ib_size_dw = 1024; 467 struct amdgpu_job *job; 468 struct amdgpu_ib *ib; 469 struct amdgpu_ib ib_msg; 470 struct dma_fence *f = NULL; 471 uint64_t addr; 472 int i, r; 473 474 r = amdgpu_job_alloc_with_ib(ring->adev, NULL, 475 AMDGPU_FENCE_OWNER_UNDEFINED, 476 ib_size_dw * 4, AMDGPU_IB_POOL_DIRECT, 477 AMDGPU_KERNEL_JOB_ID_VCN_RING_TEST, 478 &job); 479 if (r) 480 return r; 481 482 memset(&ib_msg, 0, sizeof(ib_msg)); 483 /* only one gpu page is needed, alloc +1 page to make addr aligned. */ 484 r = amdgpu_ib_get(ring->adev, NULL, AMDGPU_GPU_PAGE_SIZE * 2, 485 AMDGPU_IB_POOL_DIRECT, 486 &ib_msg); 487 if (r) 488 goto err; 489 490 ib = &job->ibs[0]; 491 /* let addr point to page boundary */ 492 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg.gpu_addr); 493 494 /* stitch together an VCE create msg */ 495 ib->length_dw = 0; 496 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */ 497 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */ 498 ib->ptr[ib->length_dw++] = handle; 499 500 if ((ring->adev->vce.fw_version >> 24) >= 52) 501 ib->ptr[ib->length_dw++] = 0x00000040; /* len */ 502 else 503 ib->ptr[ib->length_dw++] = 0x00000030; /* len */ 504 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */ 505 ib->ptr[ib->length_dw++] = 0x00000000; 506 ib->ptr[ib->length_dw++] = 0x00000042; 507 ib->ptr[ib->length_dw++] = 0x0000000a; 508 ib->ptr[ib->length_dw++] = 0x00000001; 509 ib->ptr[ib->length_dw++] = 0x00000080; 510 ib->ptr[ib->length_dw++] = 0x00000060; 511 ib->ptr[ib->length_dw++] = 0x00000100; 512 ib->ptr[ib->length_dw++] = 0x00000100; 513 ib->ptr[ib->length_dw++] = 0x0000000c; 514 ib->ptr[ib->length_dw++] = 0x00000000; 515 if ((ring->adev->vce.fw_version >> 24) >= 52) { 516 ib->ptr[ib->length_dw++] = 0x00000000; 517 ib->ptr[ib->length_dw++] = 0x00000000; 518 ib->ptr[ib->length_dw++] = 0x00000000; 519 ib->ptr[ib->length_dw++] = 0x00000000; 520 } 521 522 ib->ptr[ib->length_dw++] = 0x00000014; /* len */ 523 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */ 524 ib->ptr[ib->length_dw++] = upper_32_bits(addr); 525 ib->ptr[ib->length_dw++] = addr; 526 ib->ptr[ib->length_dw++] = 0x00000001; 527 528 for (i = ib->length_dw; i < ib_size_dw; ++i) 529 ib->ptr[i] = 0x0; 530 531 r = amdgpu_job_submit_direct(job, ring, &f); 532 amdgpu_ib_free(&ib_msg, f); 533 if (r) 534 goto err; 535 536 if (fence) 537 *fence = dma_fence_get(f); 538 dma_fence_put(f); 539 return 0; 540 541 err: 542 amdgpu_job_free(job); 543 return r; 544 } 545 546 /** 547 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg 548 * 549 * @ring: ring we should submit the msg to 550 * @handle: VCE session handle to use 551 * @direct: direct or delayed pool 552 * @fence: optional fence to return 553 * 554 * Close up a stream for HW test or if userspace failed to do so 555 */ 556 static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle, 557 bool direct, struct dma_fence **fence) 558 { 559 const unsigned int ib_size_dw = 1024; 560 struct amdgpu_job *job; 561 struct amdgpu_ib *ib; 562 struct dma_fence *f = NULL; 563 int i, r; 564 565 r = amdgpu_job_alloc_with_ib(ring->adev, 566 direct ? NULL : &ring->adev->vce.entity, 567 AMDGPU_FENCE_OWNER_UNDEFINED, 568 ib_size_dw * 4, 569 direct ? AMDGPU_IB_POOL_DIRECT : 570 AMDGPU_IB_POOL_DELAYED, 571 AMDGPU_KERNEL_JOB_ID_VCN_RING_TEST, 572 &job); 573 if (r) 574 return r; 575 576 ib = &job->ibs[0]; 577 578 /* stitch together an VCE destroy msg */ 579 ib->length_dw = 0; 580 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */ 581 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */ 582 ib->ptr[ib->length_dw++] = handle; 583 584 ib->ptr[ib->length_dw++] = 0x00000020; /* len */ 585 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */ 586 ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */ 587 ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */ 588 ib->ptr[ib->length_dw++] = 0x00000000; 589 ib->ptr[ib->length_dw++] = 0x00000000; 590 ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */ 591 ib->ptr[ib->length_dw++] = 0x00000000; 592 593 ib->ptr[ib->length_dw++] = 0x00000008; /* len */ 594 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */ 595 596 for (i = ib->length_dw; i < ib_size_dw; ++i) 597 ib->ptr[i] = 0x0; 598 599 if (direct) 600 r = amdgpu_job_submit_direct(job, ring, &f); 601 else 602 f = amdgpu_job_submit(job); 603 if (r) 604 goto err; 605 606 if (fence) 607 *fence = dma_fence_get(f); 608 dma_fence_put(f); 609 return 0; 610 611 err: 612 amdgpu_job_free(job); 613 return r; 614 } 615 616 /** 617 * amdgpu_vce_validate_bo - make sure not to cross 4GB boundary 618 * 619 * @p: cs parser 620 * @ib: indirect buffer to use 621 * @lo: address of lower dword 622 * @hi: address of higher dword 623 * @size: minimum size 624 * @index: bs/fb index 625 * 626 * Make sure that no BO cross a 4GB boundary. 627 */ 628 static int amdgpu_vce_validate_bo(struct amdgpu_cs_parser *p, 629 struct amdgpu_ib *ib, int lo, int hi, 630 unsigned int size, int32_t index) 631 { 632 int64_t offset = ((uint64_t)size) * ((int64_t)index); 633 struct ttm_operation_ctx ctx = { false, false }; 634 struct amdgpu_bo_va_mapping *mapping; 635 unsigned int i, fpfn, lpfn; 636 struct amdgpu_bo *bo; 637 uint64_t addr; 638 int r; 639 640 addr = ((uint64_t)amdgpu_ib_get_value(ib, lo)) | 641 ((uint64_t)amdgpu_ib_get_value(ib, hi)) << 32; 642 if (index >= 0) { 643 addr += offset; 644 fpfn = PAGE_ALIGN(offset) >> PAGE_SHIFT; 645 lpfn = 0x100000000ULL >> PAGE_SHIFT; 646 } else { 647 fpfn = 0; 648 lpfn = (0x100000000ULL - PAGE_ALIGN(offset)) >> PAGE_SHIFT; 649 } 650 651 r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping); 652 if (r) { 653 DRM_ERROR("Can't find BO for addr 0x%010llx %d %d %d %d\n", 654 addr, lo, hi, size, index); 655 return r; 656 } 657 658 for (i = 0; i < bo->placement.num_placement; ++i) { 659 bo->placements[i].fpfn = max(bo->placements[i].fpfn, fpfn); 660 bo->placements[i].lpfn = bo->placements[i].lpfn ? 661 min(bo->placements[i].lpfn, lpfn) : lpfn; 662 } 663 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 664 } 665 666 667 /** 668 * amdgpu_vce_cs_reloc - command submission relocation 669 * 670 * @p: parser context 671 * @ib: indirect buffer to use 672 * @lo: address of lower dword 673 * @hi: address of higher dword 674 * @size: minimum size 675 * @index: bs/fb index 676 * 677 * Patch relocation inside command stream with real buffer address 678 */ 679 static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, struct amdgpu_ib *ib, 680 int lo, int hi, unsigned int size, uint32_t index) 681 { 682 struct amdgpu_bo_va_mapping *mapping; 683 struct amdgpu_bo *bo; 684 uint64_t addr; 685 int r; 686 687 if (lo >= ib->length_dw || hi >= ib->length_dw) 688 return -EINVAL; 689 690 if (index == 0xffffffff) 691 index = 0; 692 693 addr = ((uint64_t)amdgpu_ib_get_value(ib, lo)) | 694 ((uint64_t)amdgpu_ib_get_value(ib, hi)) << 32; 695 addr += ((uint64_t)size) * ((uint64_t)index); 696 697 r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping); 698 if (r) { 699 DRM_ERROR("Can't find BO for addr 0x%010llx %d %d %d %d\n", 700 addr, lo, hi, size, index); 701 return r; 702 } 703 704 if ((addr + (uint64_t)size) > 705 (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE) { 706 DRM_ERROR("BO too small for addr 0x%010llx %d %d\n", 707 addr, lo, hi); 708 return -EINVAL; 709 } 710 711 addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE; 712 addr += amdgpu_bo_gpu_offset(bo); 713 addr -= ((uint64_t)size) * ((uint64_t)index); 714 715 amdgpu_ib_set_value(ib, lo, lower_32_bits(addr)); 716 amdgpu_ib_set_value(ib, hi, upper_32_bits(addr)); 717 718 return 0; 719 } 720 721 /** 722 * amdgpu_vce_validate_handle - validate stream handle 723 * 724 * @p: parser context 725 * @handle: handle to validate 726 * @allocated: allocated a new handle? 727 * 728 * Validates the handle and return the found session index or -EINVAL 729 * we don't have another free session index. 730 */ 731 static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p, 732 uint32_t handle, uint32_t *allocated) 733 { 734 unsigned int i; 735 736 /* validate the handle */ 737 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { 738 if (atomic_read(&p->adev->vce.handles[i]) == handle) { 739 if (p->adev->vce.filp[i] != p->filp) { 740 DRM_ERROR("VCE handle collision detected!\n"); 741 return -EINVAL; 742 } 743 return i; 744 } 745 } 746 747 /* handle not found try to alloc a new one */ 748 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { 749 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) { 750 p->adev->vce.filp[i] = p->filp; 751 p->adev->vce.img_size[i] = 0; 752 *allocated |= 1 << i; 753 return i; 754 } 755 } 756 757 DRM_ERROR("No more free VCE handles!\n"); 758 return -EINVAL; 759 } 760 761 /** 762 * amdgpu_vce_ring_parse_cs - parse and validate the command stream 763 * 764 * @p: parser context 765 * @job: the job to parse 766 * @ib: the IB to patch 767 */ 768 int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, 769 struct amdgpu_job *job, 770 struct amdgpu_ib *ib) 771 { 772 unsigned int fb_idx = 0, bs_idx = 0; 773 int session_idx = -1; 774 uint32_t destroyed = 0; 775 uint32_t created = 0; 776 uint32_t allocated = 0; 777 uint32_t tmp, handle = 0; 778 uint32_t dummy = 0xffffffff; 779 uint32_t luma_pitch, chroma_pitch, height; 780 uint32_t *size = &dummy; 781 unsigned int idx; 782 int i, r = 0; 783 784 job->vm = NULL; 785 786 for (idx = 0; idx < ib->length_dw;) { 787 uint32_t len = amdgpu_ib_get_value(ib, idx); 788 uint32_t cmd = amdgpu_ib_get_value(ib, idx + 1); 789 790 if ((len < 8) || (len & 3)) { 791 DRM_ERROR("invalid VCE command length (%d)!\n", len); 792 r = -EINVAL; 793 goto out; 794 } 795 796 switch (cmd) { 797 case 0x00000002: /* task info */ 798 fb_idx = amdgpu_ib_get_value(ib, idx + 6); 799 bs_idx = amdgpu_ib_get_value(ib, idx + 7); 800 break; 801 802 case 0x03000001: /* encode */ 803 r = amdgpu_vce_validate_bo(p, ib, idx + 10, idx + 9, 804 0, 0); 805 if (r) 806 goto out; 807 808 r = amdgpu_vce_validate_bo(p, ib, idx + 12, idx + 11, 809 0, 0); 810 if (r) 811 goto out; 812 break; 813 814 case 0x05000001: /* context buffer */ 815 r = amdgpu_vce_validate_bo(p, ib, idx + 3, idx + 2, 816 0, 0); 817 if (r) 818 goto out; 819 break; 820 821 case 0x05000004: /* video bitstream buffer */ 822 tmp = amdgpu_ib_get_value(ib, idx + 4); 823 r = amdgpu_vce_validate_bo(p, ib, idx + 3, idx + 2, 824 tmp, bs_idx); 825 if (r) 826 goto out; 827 break; 828 829 case 0x05000005: /* feedback buffer */ 830 r = amdgpu_vce_validate_bo(p, ib, idx + 3, idx + 2, 831 4096, fb_idx); 832 if (r) 833 goto out; 834 break; 835 836 case 0x0500000d: /* MV buffer */ 837 r = amdgpu_vce_validate_bo(p, ib, idx + 3, idx + 2, 838 0, 0); 839 if (r) 840 goto out; 841 842 r = amdgpu_vce_validate_bo(p, ib, idx + 8, idx + 7, 843 0, 0); 844 if (r) 845 goto out; 846 break; 847 } 848 849 idx += len / 4; 850 } 851 852 for (idx = 0; idx < ib->length_dw;) { 853 uint32_t len = amdgpu_ib_get_value(ib, idx); 854 uint32_t cmd = amdgpu_ib_get_value(ib, idx + 1); 855 856 switch (cmd) { 857 case 0x00000001: /* session */ 858 handle = amdgpu_ib_get_value(ib, idx + 2); 859 session_idx = amdgpu_vce_validate_handle(p, handle, 860 &allocated); 861 if (session_idx < 0) { 862 r = session_idx; 863 goto out; 864 } 865 size = &p->adev->vce.img_size[session_idx]; 866 break; 867 868 case 0x00000002: /* task info */ 869 fb_idx = amdgpu_ib_get_value(ib, idx + 6); 870 bs_idx = amdgpu_ib_get_value(ib, idx + 7); 871 break; 872 873 case 0x01000001: /* create */ 874 created |= 1 << session_idx; 875 if (destroyed & (1 << session_idx)) { 876 destroyed &= ~(1 << session_idx); 877 allocated |= 1 << session_idx; 878 879 } else if (!(allocated & (1 << session_idx))) { 880 DRM_ERROR("Handle already in use!\n"); 881 r = -EINVAL; 882 goto out; 883 } 884 885 luma_pitch = amdgpu_ib_get_value(ib, idx + 8); 886 chroma_pitch = amdgpu_ib_get_value(ib, idx + 9); 887 height = amdgpu_ib_get_value(ib, idx + 10); /* encRefYHeightInQw */ 888 889 if (luma_pitch == 0 || chroma_pitch == 0 || height == 0 || 890 luma_pitch > 4096 || chroma_pitch > 4096 || height > 2304 / 8) { 891 DRM_ERROR("invalid VCE ref image size: (%u %u)x%u\n", 892 luma_pitch, chroma_pitch, height * 8); 893 r = -EINVAL; 894 goto out; 895 } 896 897 *size = luma_pitch * height * 8 + chroma_pitch * height * 8 / 2; 898 899 break; 900 901 case 0x04000001: /* config extension */ 902 case 0x04000002: /* pic control */ 903 case 0x04000005: /* rate control */ 904 case 0x04000007: /* motion estimation */ 905 case 0x04000008: /* rdo */ 906 case 0x04000009: /* vui */ 907 case 0x05000002: /* auxiliary buffer */ 908 case 0x05000009: /* clock table */ 909 break; 910 911 case 0x0500000c: /* hw config */ 912 switch (p->adev->asic_type) { 913 #ifdef CONFIG_DRM_AMDGPU_CIK 914 case CHIP_KAVERI: 915 case CHIP_MULLINS: 916 #endif 917 case CHIP_CARRIZO: 918 break; 919 default: 920 r = -EINVAL; 921 goto out; 922 } 923 break; 924 925 case 0x03000001: /* encode */ 926 height = amdgpu_ib_get_value(ib, idx + 13); 927 luma_pitch = amdgpu_ib_get_value(ib, idx + 14); 928 chroma_pitch = amdgpu_ib_get_value(ib, idx + 15); 929 930 if (luma_pitch == 0 || chroma_pitch == 0 || height == 0 || 931 luma_pitch > 4096 || chroma_pitch > 4096 || height > 2304) { 932 DRM_ERROR("invalid VCE image size: (%u %u)x%u\n", 933 luma_pitch, chroma_pitch, height); 934 r = -EINVAL; 935 goto out; 936 } 937 938 r = amdgpu_vce_cs_reloc(p, ib, idx + 10, idx + 9, 939 luma_pitch * height, 0); 940 if (r) 941 goto out; 942 943 r = amdgpu_vce_cs_reloc(p, ib, idx + 12, idx + 11, 944 chroma_pitch * height / 2, 0); 945 if (r) 946 goto out; 947 break; 948 949 case 0x02000001: /* destroy */ 950 destroyed |= 1 << session_idx; 951 break; 952 953 case 0x05000001: /* context buffer */ 954 r = amdgpu_vce_cs_reloc(p, ib, idx + 3, idx + 2, 955 *size * 2, 0); 956 if (r) 957 goto out; 958 break; 959 960 case 0x05000004: /* video bitstream buffer */ 961 tmp = amdgpu_ib_get_value(ib, idx + 4); 962 r = amdgpu_vce_cs_reloc(p, ib, idx + 3, idx + 2, 963 tmp, bs_idx); 964 if (r) 965 goto out; 966 break; 967 968 case 0x05000005: /* feedback buffer */ 969 r = amdgpu_vce_cs_reloc(p, ib, idx + 3, idx + 2, 970 4096, fb_idx); 971 if (r) 972 goto out; 973 break; 974 975 case 0x0500000d: /* MV buffer */ 976 r = amdgpu_vce_cs_reloc(p, ib, idx + 3, 977 idx + 2, *size, 0); 978 if (r) 979 goto out; 980 981 r = amdgpu_vce_cs_reloc(p, ib, idx + 8, 982 idx + 7, *size / 12, 0); 983 if (r) 984 goto out; 985 break; 986 987 default: 988 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd); 989 r = -EINVAL; 990 goto out; 991 } 992 993 if (session_idx == -1) { 994 DRM_ERROR("no session command at start of IB\n"); 995 r = -EINVAL; 996 goto out; 997 } 998 999 idx += len / 4; 1000 } 1001 1002 if (allocated & ~created) { 1003 DRM_ERROR("New session without create command!\n"); 1004 r = -ENOENT; 1005 } 1006 1007 out: 1008 if (!r) { 1009 /* No error, free all destroyed handle slots */ 1010 tmp = destroyed; 1011 } else { 1012 /* Error during parsing, free all allocated handle slots */ 1013 tmp = allocated; 1014 } 1015 1016 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) 1017 if (tmp & (1 << i)) 1018 atomic_set(&p->adev->vce.handles[i], 0); 1019 1020 return r; 1021 } 1022 1023 /** 1024 * amdgpu_vce_ring_parse_cs_vm - parse the command stream in VM mode 1025 * 1026 * @p: parser context 1027 * @job: the job to parse 1028 * @ib: the IB to patch 1029 */ 1030 int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, 1031 struct amdgpu_job *job, 1032 struct amdgpu_ib *ib) 1033 { 1034 int session_idx = -1; 1035 uint32_t destroyed = 0; 1036 uint32_t created = 0; 1037 uint32_t allocated = 0; 1038 uint32_t tmp, handle = 0; 1039 int i, r = 0, idx = 0; 1040 1041 while (idx < ib->length_dw) { 1042 uint32_t len = amdgpu_ib_get_value(ib, idx); 1043 uint32_t cmd = amdgpu_ib_get_value(ib, idx + 1); 1044 1045 if ((len < 8) || (len & 3)) { 1046 DRM_ERROR("invalid VCE command length (%d)!\n", len); 1047 r = -EINVAL; 1048 goto out; 1049 } 1050 1051 switch (cmd) { 1052 case 0x00000001: /* session */ 1053 handle = amdgpu_ib_get_value(ib, idx + 2); 1054 session_idx = amdgpu_vce_validate_handle(p, handle, 1055 &allocated); 1056 if (session_idx < 0) { 1057 r = session_idx; 1058 goto out; 1059 } 1060 break; 1061 1062 case 0x01000001: /* create */ 1063 created |= 1 << session_idx; 1064 if (destroyed & (1 << session_idx)) { 1065 destroyed &= ~(1 << session_idx); 1066 allocated |= 1 << session_idx; 1067 1068 } else if (!(allocated & (1 << session_idx))) { 1069 DRM_ERROR("Handle already in use!\n"); 1070 r = -EINVAL; 1071 goto out; 1072 } 1073 1074 break; 1075 1076 case 0x02000001: /* destroy */ 1077 destroyed |= 1 << session_idx; 1078 break; 1079 1080 default: 1081 break; 1082 } 1083 1084 if (session_idx == -1) { 1085 DRM_ERROR("no session command at start of IB\n"); 1086 r = -EINVAL; 1087 goto out; 1088 } 1089 1090 idx += len / 4; 1091 } 1092 1093 if (allocated & ~created) { 1094 DRM_ERROR("New session without create command!\n"); 1095 r = -ENOENT; 1096 } 1097 1098 out: 1099 if (!r) { 1100 /* No error, free all destroyed handle slots */ 1101 tmp = destroyed; 1102 } else { 1103 /* Error during parsing, free all allocated handle slots */ 1104 tmp = allocated; 1105 } 1106 1107 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) 1108 if (tmp & (1 << i)) 1109 atomic_set(&p->adev->vce.handles[i], 0); 1110 1111 return r; 1112 } 1113 1114 /** 1115 * amdgpu_vce_ring_emit_ib - execute indirect buffer 1116 * 1117 * @ring: engine to use 1118 * @job: job to retrieve vmid from 1119 * @ib: the IB to execute 1120 * @flags: unused 1121 * 1122 */ 1123 void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, 1124 struct amdgpu_job *job, 1125 struct amdgpu_ib *ib, 1126 uint32_t flags) 1127 { 1128 amdgpu_ring_write(ring, VCE_CMD_IB); 1129 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr)); 1130 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr)); 1131 amdgpu_ring_write(ring, ib->length_dw); 1132 } 1133 1134 /** 1135 * amdgpu_vce_ring_emit_fence - add a fence command to the ring 1136 * 1137 * @ring: engine to use 1138 * @addr: address 1139 * @seq: sequence number 1140 * @flags: fence related flags 1141 * 1142 */ 1143 void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq, 1144 unsigned int flags) 1145 { 1146 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT); 1147 1148 amdgpu_ring_write(ring, VCE_CMD_FENCE); 1149 amdgpu_ring_write(ring, addr); 1150 amdgpu_ring_write(ring, upper_32_bits(addr)); 1151 amdgpu_ring_write(ring, seq); 1152 amdgpu_ring_write(ring, VCE_CMD_TRAP); 1153 amdgpu_ring_write(ring, VCE_CMD_END); 1154 } 1155 1156 /** 1157 * amdgpu_vce_ring_test_ring - test if VCE ring is working 1158 * 1159 * @ring: the engine to test on 1160 * 1161 */ 1162 int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring) 1163 { 1164 struct amdgpu_device *adev = ring->adev; 1165 uint32_t rptr; 1166 unsigned int i; 1167 int r, timeout = adev->usec_timeout; 1168 1169 /* skip ring test for sriov*/ 1170 if (amdgpu_sriov_vf(adev)) 1171 return 0; 1172 1173 r = amdgpu_ring_alloc(ring, 16); 1174 if (r) 1175 return r; 1176 1177 rptr = amdgpu_ring_get_rptr(ring); 1178 1179 amdgpu_ring_write(ring, VCE_CMD_END); 1180 amdgpu_ring_commit(ring); 1181 1182 for (i = 0; i < timeout; i++) { 1183 if (amdgpu_ring_get_rptr(ring) != rptr) 1184 break; 1185 udelay(1); 1186 } 1187 1188 if (i >= timeout) 1189 r = -ETIMEDOUT; 1190 1191 return r; 1192 } 1193 1194 /** 1195 * amdgpu_vce_ring_test_ib - test if VCE IBs are working 1196 * 1197 * @ring: the engine to test on 1198 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT 1199 * 1200 */ 1201 int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout) 1202 { 1203 struct dma_fence *fence = NULL; 1204 long r; 1205 1206 /* skip vce ring1/2 ib test for now, since it's not reliable */ 1207 if (ring != &ring->adev->vce.ring[0]) 1208 return 0; 1209 1210 r = amdgpu_vce_get_create_msg(ring, 1, NULL); 1211 if (r) 1212 goto error; 1213 1214 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence); 1215 if (r) 1216 goto error; 1217 1218 r = dma_fence_wait_timeout(fence, false, timeout); 1219 if (r == 0) 1220 r = -ETIMEDOUT; 1221 else if (r > 0) 1222 r = 0; 1223 1224 error: 1225 dma_fence_put(fence); 1226 return r; 1227 } 1228 1229 enum amdgpu_ring_priority_level amdgpu_vce_get_ring_prio(int ring) 1230 { 1231 switch (ring) { 1232 case 0: 1233 return AMDGPU_RING_PRIO_0; 1234 case 1: 1235 return AMDGPU_RING_PRIO_1; 1236 case 2: 1237 return AMDGPU_RING_PRIO_2; 1238 default: 1239 return AMDGPU_RING_PRIO_0; 1240 } 1241 } 1242