1 /* 2 * Copyright 2019 Advanced Micro Devices, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 19 * USE OR OTHER DEALINGS IN THE SOFTWARE. 20 * 21 * The above copyright notice and this permission notice (including the 22 * next paragraph) shall be included in all copies or substantial portions 23 * of the Software. 24 * 25 */ 26 27 #include "amdgpu.h" 28 #include "amdgpu_jpeg.h" 29 #include "amdgpu_pm.h" 30 #include "soc15d.h" 31 #include "soc15_common.h" 32 33 #define JPEG_IDLE_TIMEOUT msecs_to_jiffies(1000) 34 35 static void amdgpu_jpeg_idle_work_handler(struct work_struct *work); 36 static void amdgpu_jpeg_reg_dump_fini(struct amdgpu_device *adev); 37 38 int amdgpu_jpeg_sw_init(struct amdgpu_device *adev) 39 { 40 int i, r; 41 42 INIT_DELAYED_WORK(&adev->jpeg.idle_work, amdgpu_jpeg_idle_work_handler); 43 mutex_init(&adev->jpeg.jpeg_pg_lock); 44 atomic_set(&adev->jpeg.total_submission_cnt, 0); 45 46 if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) && 47 (adev->pg_flags & AMD_PG_SUPPORT_JPEG_DPG)) 48 adev->jpeg.indirect_sram = true; 49 50 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) { 51 if (adev->jpeg.harvest_config & (1U << i)) 52 continue; 53 54 if (adev->jpeg.indirect_sram) { 55 r = amdgpu_bo_create_kernel(adev, 64 * 2 * 4, PAGE_SIZE, 56 AMDGPU_GEM_DOMAIN_VRAM | 57 AMDGPU_GEM_DOMAIN_GTT, 58 &adev->jpeg.inst[i].dpg_sram_bo, 59 &adev->jpeg.inst[i].dpg_sram_gpu_addr, 60 &adev->jpeg.inst[i].dpg_sram_cpu_addr); 61 if (r) { 62 dev_err(adev->dev, 63 "JPEG %d (%d) failed to allocate DPG bo\n", i, r); 64 return r; 65 } 66 } 67 } 68 69 return 0; 70 } 71 72 int amdgpu_jpeg_sw_fini(struct amdgpu_device *adev) 73 { 74 int i, j; 75 76 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 77 if (adev->jpeg.harvest_config & (1U << i)) 78 continue; 79 80 amdgpu_bo_free_kernel( 81 &adev->jpeg.inst[i].dpg_sram_bo, 82 &adev->jpeg.inst[i].dpg_sram_gpu_addr, 83 (void **)&adev->jpeg.inst[i].dpg_sram_cpu_addr); 84 85 for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) 86 amdgpu_ring_fini(&adev->jpeg.inst[i].ring_dec[j]); 87 } 88 89 if (adev->jpeg.reg_list) 90 amdgpu_jpeg_reg_dump_fini(adev); 91 92 mutex_destroy(&adev->jpeg.jpeg_pg_lock); 93 94 return 0; 95 } 96 97 int amdgpu_jpeg_suspend(struct amdgpu_device *adev) 98 { 99 cancel_delayed_work_sync(&adev->jpeg.idle_work); 100 101 return 0; 102 } 103 104 int amdgpu_jpeg_resume(struct amdgpu_device *adev) 105 { 106 return 0; 107 } 108 109 static void amdgpu_jpeg_idle_work_handler(struct work_struct *work) 110 { 111 struct amdgpu_device *adev = 112 container_of(work, struct amdgpu_device, jpeg.idle_work.work); 113 unsigned int fences = 0; 114 unsigned int i, j; 115 116 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 117 if (adev->jpeg.harvest_config & (1U << i)) 118 continue; 119 120 for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) 121 fences += amdgpu_fence_count_emitted(&adev->jpeg.inst[i].ring_dec[j]); 122 } 123 124 if (!fences && !atomic_read(&adev->jpeg.total_submission_cnt)) { 125 mutex_lock(&adev->jpeg.jpeg_pg_lock); 126 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG, 127 AMD_PG_STATE_GATE); 128 mutex_unlock(&adev->jpeg.jpeg_pg_lock); 129 } else 130 schedule_delayed_work(&adev->jpeg.idle_work, JPEG_IDLE_TIMEOUT); 131 } 132 133 void amdgpu_jpeg_ring_begin_use(struct amdgpu_ring *ring) 134 { 135 struct amdgpu_device *adev = ring->adev; 136 137 if (!atomic_fetch_inc(&adev->jpeg.total_submission_cnt)) 138 cancel_delayed_work_sync(&adev->jpeg.idle_work); 139 140 mutex_lock(&adev->jpeg.jpeg_pg_lock); 141 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG, 142 AMD_PG_STATE_UNGATE); 143 mutex_unlock(&adev->jpeg.jpeg_pg_lock); 144 } 145 146 void amdgpu_jpeg_ring_end_use(struct amdgpu_ring *ring) 147 { 148 if (atomic_dec_and_test(&ring->adev->jpeg.total_submission_cnt)) 149 schedule_delayed_work(&ring->adev->jpeg.idle_work, 150 JPEG_IDLE_TIMEOUT); 151 } 152 153 int amdgpu_jpeg_dec_ring_test_ring(struct amdgpu_ring *ring) 154 { 155 struct amdgpu_device *adev = ring->adev; 156 uint32_t tmp = 0; 157 unsigned i; 158 int r; 159 160 /* JPEG in SRIOV does not support direct register read/write */ 161 if (amdgpu_sriov_vf(adev)) 162 return 0; 163 164 r = amdgpu_ring_alloc(ring, 3); 165 if (r) 166 return r; 167 168 WREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe], 0xCAFEDEAD); 169 /* Add a read register to make sure the write register is executed. */ 170 RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]); 171 172 amdgpu_ring_write(ring, PACKET0(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0)); 173 amdgpu_ring_write(ring, 0xABADCAFE); 174 amdgpu_ring_commit(ring); 175 176 for (i = 0; i < adev->usec_timeout; i++) { 177 tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]); 178 if (tmp == 0xABADCAFE) 179 break; 180 udelay(1); 181 } 182 183 if (i >= adev->usec_timeout) 184 r = -ETIMEDOUT; 185 186 return r; 187 } 188 189 static int amdgpu_jpeg_dec_set_reg(struct amdgpu_ring *ring, uint32_t handle, 190 struct dma_fence **fence) 191 { 192 struct amdgpu_device *adev = ring->adev; 193 struct amdgpu_job *job; 194 struct amdgpu_ib *ib; 195 struct dma_fence *f = NULL; 196 const unsigned ib_size_dw = 16; 197 int i, r; 198 199 r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4, 200 AMDGPU_IB_POOL_DIRECT, &job, 201 AMDGPU_KERNEL_JOB_ID_VCN_RING_TEST); 202 if (r) 203 return r; 204 205 ib = &job->ibs[0]; 206 207 ib->ptr[0] = PACKETJ(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0, 0, PACKETJ_TYPE0); 208 ib->ptr[1] = 0xDEADBEEF; 209 for (i = 2; i < 16; i += 2) { 210 ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6); 211 ib->ptr[i+1] = 0; 212 } 213 ib->length_dw = 16; 214 215 r = amdgpu_job_submit_direct(job, ring, &f); 216 if (r) 217 goto err; 218 219 if (fence) 220 *fence = dma_fence_get(f); 221 dma_fence_put(f); 222 223 return 0; 224 225 err: 226 amdgpu_job_free(job); 227 return r; 228 } 229 230 int amdgpu_jpeg_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout) 231 { 232 struct amdgpu_device *adev = ring->adev; 233 uint32_t tmp = 0; 234 unsigned i; 235 struct dma_fence *fence = NULL; 236 long r = 0; 237 238 r = amdgpu_jpeg_dec_set_reg(ring, 1, &fence); 239 if (r) 240 goto error; 241 242 r = dma_fence_wait_timeout(fence, false, timeout); 243 if (r == 0) { 244 r = -ETIMEDOUT; 245 goto error; 246 } else if (r < 0) { 247 goto error; 248 } else { 249 r = 0; 250 } 251 252 if (!amdgpu_sriov_vf(adev)) { 253 for (i = 0; i < adev->usec_timeout; i++) { 254 tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]); 255 if (tmp == 0xDEADBEEF) 256 break; 257 udelay(1); 258 if (amdgpu_emu_mode == 1) 259 udelay(10); 260 } 261 262 if (i >= adev->usec_timeout) 263 r = -ETIMEDOUT; 264 } 265 266 dma_fence_put(fence); 267 error: 268 return r; 269 } 270 271 int amdgpu_jpeg_process_poison_irq(struct amdgpu_device *adev, 272 struct amdgpu_irq_src *source, 273 struct amdgpu_iv_entry *entry) 274 { 275 struct ras_common_if *ras_if = adev->jpeg.ras_if; 276 struct ras_dispatch_if ih_data = { 277 .entry = entry, 278 }; 279 280 if (!ras_if) 281 return 0; 282 283 ih_data.head = *ras_if; 284 amdgpu_ras_interrupt_dispatch(adev, &ih_data); 285 286 return 0; 287 } 288 289 int amdgpu_jpeg_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block) 290 { 291 int r, i; 292 293 r = amdgpu_ras_block_late_init(adev, ras_block); 294 if (r) 295 return r; 296 297 if (amdgpu_ras_is_supported(adev, ras_block->block)) { 298 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 299 if (adev->jpeg.harvest_config & (1 << i) || 300 !adev->jpeg.inst[i].ras_poison_irq.funcs) 301 continue; 302 303 r = amdgpu_irq_get(adev, &adev->jpeg.inst[i].ras_poison_irq, 0); 304 if (r) 305 goto late_fini; 306 } 307 } 308 return 0; 309 310 late_fini: 311 amdgpu_ras_block_late_fini(adev, ras_block); 312 return r; 313 } 314 315 int amdgpu_jpeg_ras_sw_init(struct amdgpu_device *adev) 316 { 317 int err; 318 struct amdgpu_jpeg_ras *ras; 319 320 if (!adev->jpeg.ras) 321 return 0; 322 323 ras = adev->jpeg.ras; 324 err = amdgpu_ras_register_ras_block(adev, &ras->ras_block); 325 if (err) { 326 dev_err(adev->dev, "Failed to register jpeg ras block!\n"); 327 return err; 328 } 329 330 strcpy(ras->ras_block.ras_comm.name, "jpeg"); 331 ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__JPEG; 332 ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__POISON; 333 adev->jpeg.ras_if = &ras->ras_block.ras_comm; 334 335 if (!ras->ras_block.ras_late_init) 336 ras->ras_block.ras_late_init = amdgpu_jpeg_ras_late_init; 337 338 return 0; 339 } 340 341 int amdgpu_jpeg_psp_update_sram(struct amdgpu_device *adev, int inst_idx, 342 enum AMDGPU_UCODE_ID ucode_id) 343 { 344 struct amdgpu_firmware_info ucode = { 345 .ucode_id = AMDGPU_UCODE_ID_JPEG_RAM, 346 .mc_addr = adev->jpeg.inst[inst_idx].dpg_sram_gpu_addr, 347 .ucode_size = ((uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_curr_addr - 348 (uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_cpu_addr), 349 }; 350 351 return psp_execute_ip_fw_load(&adev->psp, &ucode); 352 } 353 354 /* 355 * debugfs for to enable/disable jpeg job submission to specific core. 356 */ 357 #if defined(CONFIG_DEBUG_FS) 358 static int amdgpu_debugfs_jpeg_sched_mask_set(void *data, u64 val) 359 { 360 struct amdgpu_device *adev = (struct amdgpu_device *)data; 361 u32 i, j; 362 u64 mask = 0; 363 struct amdgpu_ring *ring; 364 365 if (!adev) 366 return -ENODEV; 367 368 mask = (1ULL << (adev->jpeg.num_jpeg_inst * adev->jpeg.num_jpeg_rings)) - 1; 369 if ((val & mask) == 0) 370 return -EINVAL; 371 372 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 373 for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) { 374 ring = &adev->jpeg.inst[i].ring_dec[j]; 375 if (val & (BIT_ULL((i * adev->jpeg.num_jpeg_rings) + j))) 376 ring->sched.ready = true; 377 else 378 ring->sched.ready = false; 379 } 380 } 381 /* publish sched.ready flag update effective immediately across smp */ 382 smp_rmb(); 383 return 0; 384 } 385 386 static int amdgpu_debugfs_jpeg_sched_mask_get(void *data, u64 *val) 387 { 388 struct amdgpu_device *adev = (struct amdgpu_device *)data; 389 u32 i, j; 390 u64 mask = 0; 391 struct amdgpu_ring *ring; 392 393 if (!adev) 394 return -ENODEV; 395 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 396 for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) { 397 ring = &adev->jpeg.inst[i].ring_dec[j]; 398 if (ring->sched.ready) 399 mask |= 1ULL << ((i * adev->jpeg.num_jpeg_rings) + j); 400 } 401 } 402 *val = mask; 403 return 0; 404 } 405 406 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_jpeg_sched_mask_fops, 407 amdgpu_debugfs_jpeg_sched_mask_get, 408 amdgpu_debugfs_jpeg_sched_mask_set, "%llx\n"); 409 410 #endif 411 412 void amdgpu_debugfs_jpeg_sched_mask_init(struct amdgpu_device *adev) 413 { 414 #if defined(CONFIG_DEBUG_FS) 415 struct drm_minor *minor = adev_to_drm(adev)->primary; 416 struct dentry *root = minor->debugfs_root; 417 char name[32]; 418 419 if (!(adev->jpeg.num_jpeg_inst > 1) && !(adev->jpeg.num_jpeg_rings > 1)) 420 return; 421 sprintf(name, "amdgpu_jpeg_sched_mask"); 422 debugfs_create_file(name, 0600, root, adev, 423 &amdgpu_debugfs_jpeg_sched_mask_fops); 424 #endif 425 } 426 427 static ssize_t amdgpu_get_jpeg_reset_mask(struct device *dev, 428 struct device_attribute *attr, 429 char *buf) 430 { 431 struct drm_device *ddev = dev_get_drvdata(dev); 432 struct amdgpu_device *adev = drm_to_adev(ddev); 433 434 if (!adev) 435 return -ENODEV; 436 437 return amdgpu_show_reset_mask(buf, adev->jpeg.supported_reset); 438 } 439 440 static DEVICE_ATTR(jpeg_reset_mask, 0444, 441 amdgpu_get_jpeg_reset_mask, NULL); 442 443 int amdgpu_jpeg_sysfs_reset_mask_init(struct amdgpu_device *adev) 444 { 445 int r = 0; 446 447 if (adev->jpeg.num_jpeg_inst) { 448 r = device_create_file(adev->dev, &dev_attr_jpeg_reset_mask); 449 if (r) 450 return r; 451 } 452 453 return r; 454 } 455 456 void amdgpu_jpeg_sysfs_reset_mask_fini(struct amdgpu_device *adev) 457 { 458 if (adev->dev->kobj.sd) { 459 if (adev->jpeg.num_jpeg_inst) 460 device_remove_file(adev->dev, &dev_attr_jpeg_reset_mask); 461 } 462 } 463 464 int amdgpu_jpeg_reg_dump_init(struct amdgpu_device *adev, 465 const struct amdgpu_hwip_reg_entry *reg, u32 count) 466 { 467 adev->jpeg.ip_dump = kcalloc(adev->jpeg.num_jpeg_inst * count, 468 sizeof(uint32_t), GFP_KERNEL); 469 if (!adev->jpeg.ip_dump) { 470 dev_err(adev->dev, 471 "Failed to allocate memory for JPEG IP Dump\n"); 472 return -ENOMEM; 473 } 474 adev->jpeg.reg_list = reg; 475 adev->jpeg.reg_count = count; 476 477 return 0; 478 } 479 480 static void amdgpu_jpeg_reg_dump_fini(struct amdgpu_device *adev) 481 { 482 kfree(adev->jpeg.ip_dump); 483 adev->jpeg.reg_list = NULL; 484 adev->jpeg.reg_count = 0; 485 } 486 487 void amdgpu_jpeg_dump_ip_state(struct amdgpu_ip_block *ip_block) 488 { 489 struct amdgpu_device *adev = ip_block->adev; 490 u32 inst_off, inst_id, is_powered; 491 int i, j; 492 493 if (!adev->jpeg.ip_dump) 494 return; 495 496 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) { 497 if (adev->jpeg.harvest_config & (1 << i)) 498 continue; 499 500 inst_id = GET_INST(JPEG, i); 501 inst_off = i * adev->jpeg.reg_count; 502 /* check power status from UVD_JPEG_POWER_STATUS */ 503 adev->jpeg.ip_dump[inst_off] = 504 RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[0], 505 inst_id)); 506 is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1); 507 508 if (is_powered) 509 for (j = 1; j < adev->jpeg.reg_count; j++) 510 adev->jpeg.ip_dump[inst_off + j] = 511 RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[j], 512 inst_id)); 513 } 514 } 515 516 void amdgpu_jpeg_print_ip_state(struct amdgpu_ip_block *ip_block, struct drm_printer *p) 517 { 518 struct amdgpu_device *adev = ip_block->adev; 519 u32 inst_off, is_powered; 520 int i, j; 521 522 if (!adev->jpeg.ip_dump) 523 return; 524 525 drm_printf(p, "num_instances:%d\n", adev->jpeg.num_jpeg_inst); 526 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) { 527 if (adev->jpeg.harvest_config & (1 << i)) { 528 drm_printf(p, "\nHarvested Instance:JPEG%d Skipping dump\n", i); 529 continue; 530 } 531 532 inst_off = i * adev->jpeg.reg_count; 533 is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1); 534 535 if (is_powered) { 536 drm_printf(p, "Active Instance:JPEG%d\n", i); 537 for (j = 0; j < adev->jpeg.reg_count; j++) 538 drm_printf(p, "%-50s \t 0x%08x\n", adev->jpeg.reg_list[j].reg_name, 539 adev->jpeg.ip_dump[inst_off + j]); 540 } else 541 drm_printf(p, "\nInactive Instance:JPEG%d\n", i); 542 } 543 } 544 545 static inline bool amdgpu_jpeg_reg_valid(u32 reg) 546 { 547 if (reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END || 548 (reg >= JPEG_ATOMIC_RANGE_START && reg <= JPEG_ATOMIC_RANGE_END)) 549 return false; 550 else 551 return true; 552 } 553 554 /** 555 * amdgpu_jpeg_dec_parse_cs - command submission parser 556 * 557 * @parser: Command submission parser context 558 * @job: the job to parse 559 * @ib: the IB to parse 560 * 561 * Parse the command stream, return -EINVAL for invalid packet, 562 * 0 otherwise 563 */ 564 565 int amdgpu_jpeg_dec_parse_cs(struct amdgpu_cs_parser *parser, 566 struct amdgpu_job *job, 567 struct amdgpu_ib *ib) 568 { 569 u32 i, reg, res, cond, type; 570 struct amdgpu_device *adev = parser->adev; 571 572 for (i = 0; i < ib->length_dw ; i += 2) { 573 reg = CP_PACKETJ_GET_REG(ib->ptr[i]); 574 res = CP_PACKETJ_GET_RES(ib->ptr[i]); 575 cond = CP_PACKETJ_GET_COND(ib->ptr[i]); 576 type = CP_PACKETJ_GET_TYPE(ib->ptr[i]); 577 578 if (res) /* only support 0 at the moment */ 579 return -EINVAL; 580 581 switch (type) { 582 case PACKETJ_TYPE0: 583 if (cond != PACKETJ_CONDITION_CHECK0 || 584 !amdgpu_jpeg_reg_valid(reg)) { 585 dev_err(adev->dev, "Invalid packet [0x%08x]!\n", ib->ptr[i]); 586 return -EINVAL; 587 } 588 break; 589 case PACKETJ_TYPE3: 590 if (cond != PACKETJ_CONDITION_CHECK3 || 591 !amdgpu_jpeg_reg_valid(reg)) { 592 dev_err(adev->dev, "Invalid packet [0x%08x]!\n", ib->ptr[i]); 593 return -EINVAL; 594 } 595 break; 596 case PACKETJ_TYPE6: 597 if (ib->ptr[i] == CP_PACKETJ_NOP) 598 continue; 599 dev_err(adev->dev, "Invalid packet [0x%08x]!\n", ib->ptr[i]); 600 return -EINVAL; 601 default: 602 dev_err(adev->dev, "Unknown packet type %d !\n", type); 603 return -EINVAL; 604 } 605 } 606 607 return 0; 608 } 609