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 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG, 126 AMD_PG_STATE_GATE); 127 else 128 schedule_delayed_work(&adev->jpeg.idle_work, JPEG_IDLE_TIMEOUT); 129 } 130 131 void amdgpu_jpeg_ring_begin_use(struct amdgpu_ring *ring) 132 { 133 struct amdgpu_device *adev = ring->adev; 134 135 atomic_inc(&adev->jpeg.total_submission_cnt); 136 cancel_delayed_work_sync(&adev->jpeg.idle_work); 137 138 mutex_lock(&adev->jpeg.jpeg_pg_lock); 139 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG, 140 AMD_PG_STATE_UNGATE); 141 mutex_unlock(&adev->jpeg.jpeg_pg_lock); 142 } 143 144 void amdgpu_jpeg_ring_end_use(struct amdgpu_ring *ring) 145 { 146 atomic_dec(&ring->adev->jpeg.total_submission_cnt); 147 schedule_delayed_work(&ring->adev->jpeg.idle_work, JPEG_IDLE_TIMEOUT); 148 } 149 150 int amdgpu_jpeg_dec_ring_test_ring(struct amdgpu_ring *ring) 151 { 152 struct amdgpu_device *adev = ring->adev; 153 uint32_t tmp = 0; 154 unsigned i; 155 int r; 156 157 /* JPEG in SRIOV does not support direct register read/write */ 158 if (amdgpu_sriov_vf(adev)) 159 return 0; 160 161 r = amdgpu_ring_alloc(ring, 3); 162 if (r) 163 return r; 164 165 WREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe], 0xCAFEDEAD); 166 /* Add a read register to make sure the write register is executed. */ 167 RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]); 168 169 amdgpu_ring_write(ring, PACKET0(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0)); 170 amdgpu_ring_write(ring, 0xABADCAFE); 171 amdgpu_ring_commit(ring); 172 173 for (i = 0; i < adev->usec_timeout; i++) { 174 tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]); 175 if (tmp == 0xABADCAFE) 176 break; 177 udelay(1); 178 } 179 180 if (i >= adev->usec_timeout) 181 r = -ETIMEDOUT; 182 183 return r; 184 } 185 186 static int amdgpu_jpeg_dec_set_reg(struct amdgpu_ring *ring, uint32_t handle, 187 struct dma_fence **fence) 188 { 189 struct amdgpu_device *adev = ring->adev; 190 struct amdgpu_job *job; 191 struct amdgpu_ib *ib; 192 struct dma_fence *f = NULL; 193 const unsigned ib_size_dw = 16; 194 int i, r; 195 196 r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4, 197 AMDGPU_IB_POOL_DIRECT, &job); 198 if (r) 199 return r; 200 201 ib = &job->ibs[0]; 202 203 ib->ptr[0] = PACKETJ(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0, 0, PACKETJ_TYPE0); 204 ib->ptr[1] = 0xDEADBEEF; 205 for (i = 2; i < 16; i += 2) { 206 ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6); 207 ib->ptr[i+1] = 0; 208 } 209 ib->length_dw = 16; 210 211 r = amdgpu_job_submit_direct(job, ring, &f); 212 if (r) 213 goto err; 214 215 if (fence) 216 *fence = dma_fence_get(f); 217 dma_fence_put(f); 218 219 return 0; 220 221 err: 222 amdgpu_job_free(job); 223 return r; 224 } 225 226 int amdgpu_jpeg_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout) 227 { 228 struct amdgpu_device *adev = ring->adev; 229 uint32_t tmp = 0; 230 unsigned i; 231 struct dma_fence *fence = NULL; 232 long r = 0; 233 234 r = amdgpu_jpeg_dec_set_reg(ring, 1, &fence); 235 if (r) 236 goto error; 237 238 r = dma_fence_wait_timeout(fence, false, timeout); 239 if (r == 0) { 240 r = -ETIMEDOUT; 241 goto error; 242 } else if (r < 0) { 243 goto error; 244 } else { 245 r = 0; 246 } 247 248 if (!amdgpu_sriov_vf(adev)) { 249 for (i = 0; i < adev->usec_timeout; i++) { 250 tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]); 251 if (tmp == 0xDEADBEEF) 252 break; 253 udelay(1); 254 if (amdgpu_emu_mode == 1) 255 udelay(10); 256 } 257 258 if (i >= adev->usec_timeout) 259 r = -ETIMEDOUT; 260 } 261 262 dma_fence_put(fence); 263 error: 264 return r; 265 } 266 267 int amdgpu_jpeg_process_poison_irq(struct amdgpu_device *adev, 268 struct amdgpu_irq_src *source, 269 struct amdgpu_iv_entry *entry) 270 { 271 struct ras_common_if *ras_if = adev->jpeg.ras_if; 272 struct ras_dispatch_if ih_data = { 273 .entry = entry, 274 }; 275 276 if (!ras_if) 277 return 0; 278 279 ih_data.head = *ras_if; 280 amdgpu_ras_interrupt_dispatch(adev, &ih_data); 281 282 return 0; 283 } 284 285 int amdgpu_jpeg_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block) 286 { 287 int r, i; 288 289 r = amdgpu_ras_block_late_init(adev, ras_block); 290 if (r) 291 return r; 292 293 if (amdgpu_ras_is_supported(adev, ras_block->block)) { 294 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 295 if (adev->jpeg.harvest_config & (1 << i) || 296 !adev->jpeg.inst[i].ras_poison_irq.funcs) 297 continue; 298 299 r = amdgpu_irq_get(adev, &adev->jpeg.inst[i].ras_poison_irq, 0); 300 if (r) 301 goto late_fini; 302 } 303 } 304 return 0; 305 306 late_fini: 307 amdgpu_ras_block_late_fini(adev, ras_block); 308 return r; 309 } 310 311 int amdgpu_jpeg_ras_sw_init(struct amdgpu_device *adev) 312 { 313 int err; 314 struct amdgpu_jpeg_ras *ras; 315 316 if (!adev->jpeg.ras) 317 return 0; 318 319 ras = adev->jpeg.ras; 320 err = amdgpu_ras_register_ras_block(adev, &ras->ras_block); 321 if (err) { 322 dev_err(adev->dev, "Failed to register jpeg ras block!\n"); 323 return err; 324 } 325 326 strcpy(ras->ras_block.ras_comm.name, "jpeg"); 327 ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__JPEG; 328 ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__POISON; 329 adev->jpeg.ras_if = &ras->ras_block.ras_comm; 330 331 if (!ras->ras_block.ras_late_init) 332 ras->ras_block.ras_late_init = amdgpu_jpeg_ras_late_init; 333 334 return 0; 335 } 336 337 int amdgpu_jpeg_psp_update_sram(struct amdgpu_device *adev, int inst_idx, 338 enum AMDGPU_UCODE_ID ucode_id) 339 { 340 struct amdgpu_firmware_info ucode = { 341 .ucode_id = AMDGPU_UCODE_ID_JPEG_RAM, 342 .mc_addr = adev->jpeg.inst[inst_idx].dpg_sram_gpu_addr, 343 .ucode_size = ((uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_curr_addr - 344 (uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_cpu_addr), 345 }; 346 347 return psp_execute_ip_fw_load(&adev->psp, &ucode); 348 } 349 350 /* 351 * debugfs for to enable/disable jpeg job submission to specific core. 352 */ 353 #if defined(CONFIG_DEBUG_FS) 354 static int amdgpu_debugfs_jpeg_sched_mask_set(void *data, u64 val) 355 { 356 struct amdgpu_device *adev = (struct amdgpu_device *)data; 357 u32 i, j; 358 u64 mask = 0; 359 struct amdgpu_ring *ring; 360 361 if (!adev) 362 return -ENODEV; 363 364 mask = (1ULL << (adev->jpeg.num_jpeg_inst * adev->jpeg.num_jpeg_rings)) - 1; 365 if ((val & mask) == 0) 366 return -EINVAL; 367 368 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 369 for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) { 370 ring = &adev->jpeg.inst[i].ring_dec[j]; 371 if (val & (1 << ((i * adev->jpeg.num_jpeg_rings) + j))) 372 ring->sched.ready = true; 373 else 374 ring->sched.ready = false; 375 } 376 } 377 /* publish sched.ready flag update effective immediately across smp */ 378 smp_rmb(); 379 return 0; 380 } 381 382 static int amdgpu_debugfs_jpeg_sched_mask_get(void *data, u64 *val) 383 { 384 struct amdgpu_device *adev = (struct amdgpu_device *)data; 385 u32 i, j; 386 u64 mask = 0; 387 struct amdgpu_ring *ring; 388 389 if (!adev) 390 return -ENODEV; 391 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) { 392 for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) { 393 ring = &adev->jpeg.inst[i].ring_dec[j]; 394 if (ring->sched.ready) 395 mask |= 1ULL << ((i * adev->jpeg.num_jpeg_rings) + j); 396 } 397 } 398 *val = mask; 399 return 0; 400 } 401 402 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_jpeg_sched_mask_fops, 403 amdgpu_debugfs_jpeg_sched_mask_get, 404 amdgpu_debugfs_jpeg_sched_mask_set, "%llx\n"); 405 406 #endif 407 408 void amdgpu_debugfs_jpeg_sched_mask_init(struct amdgpu_device *adev) 409 { 410 #if defined(CONFIG_DEBUG_FS) 411 struct drm_minor *minor = adev_to_drm(adev)->primary; 412 struct dentry *root = minor->debugfs_root; 413 char name[32]; 414 415 if (!(adev->jpeg.num_jpeg_inst > 1) && !(adev->jpeg.num_jpeg_rings > 1)) 416 return; 417 sprintf(name, "amdgpu_jpeg_sched_mask"); 418 debugfs_create_file(name, 0600, root, adev, 419 &amdgpu_debugfs_jpeg_sched_mask_fops); 420 #endif 421 } 422 423 static ssize_t amdgpu_get_jpeg_reset_mask(struct device *dev, 424 struct device_attribute *attr, 425 char *buf) 426 { 427 struct drm_device *ddev = dev_get_drvdata(dev); 428 struct amdgpu_device *adev = drm_to_adev(ddev); 429 430 if (!adev) 431 return -ENODEV; 432 433 return amdgpu_show_reset_mask(buf, adev->jpeg.supported_reset); 434 } 435 436 static DEVICE_ATTR(jpeg_reset_mask, 0444, 437 amdgpu_get_jpeg_reset_mask, NULL); 438 439 int amdgpu_jpeg_sysfs_reset_mask_init(struct amdgpu_device *adev) 440 { 441 int r = 0; 442 443 if (adev->jpeg.num_jpeg_inst) { 444 r = device_create_file(adev->dev, &dev_attr_jpeg_reset_mask); 445 if (r) 446 return r; 447 } 448 449 return r; 450 } 451 452 void amdgpu_jpeg_sysfs_reset_mask_fini(struct amdgpu_device *adev) 453 { 454 if (adev->dev->kobj.sd) { 455 if (adev->jpeg.num_jpeg_inst) 456 device_remove_file(adev->dev, &dev_attr_jpeg_reset_mask); 457 } 458 } 459 460 int amdgpu_jpeg_reg_dump_init(struct amdgpu_device *adev, 461 const struct amdgpu_hwip_reg_entry *reg, u32 count) 462 { 463 adev->jpeg.ip_dump = kcalloc(adev->jpeg.num_jpeg_inst * count, 464 sizeof(uint32_t), GFP_KERNEL); 465 if (!adev->jpeg.ip_dump) { 466 DRM_ERROR("Failed to allocate memory for JPEG IP Dump\n"); 467 return -ENOMEM; 468 } 469 adev->jpeg.reg_list = reg; 470 adev->jpeg.reg_count = count; 471 472 return 0; 473 } 474 475 static void amdgpu_jpeg_reg_dump_fini(struct amdgpu_device *adev) 476 { 477 kfree(adev->jpeg.ip_dump); 478 adev->jpeg.reg_list = NULL; 479 adev->jpeg.reg_count = 0; 480 } 481 482 void amdgpu_jpeg_dump_ip_state(struct amdgpu_ip_block *ip_block) 483 { 484 struct amdgpu_device *adev = ip_block->adev; 485 u32 inst_off, inst_id, is_powered; 486 int i, j; 487 488 if (!adev->jpeg.ip_dump) 489 return; 490 491 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) { 492 if (adev->jpeg.harvest_config & (1 << i)) 493 continue; 494 495 inst_id = GET_INST(JPEG, i); 496 inst_off = i * adev->jpeg.reg_count; 497 /* check power status from UVD_JPEG_POWER_STATUS */ 498 adev->jpeg.ip_dump[inst_off] = 499 RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[0], 500 inst_id)); 501 is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1); 502 503 if (is_powered) 504 for (j = 1; j < adev->jpeg.reg_count; j++) 505 adev->jpeg.ip_dump[inst_off + j] = 506 RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[j], 507 inst_id)); 508 } 509 } 510 511 void amdgpu_jpeg_print_ip_state(struct amdgpu_ip_block *ip_block, struct drm_printer *p) 512 { 513 struct amdgpu_device *adev = ip_block->adev; 514 u32 inst_off, is_powered; 515 int i, j; 516 517 if (!adev->jpeg.ip_dump) 518 return; 519 520 drm_printf(p, "num_instances:%d\n", adev->jpeg.num_jpeg_inst); 521 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) { 522 if (adev->jpeg.harvest_config & (1 << i)) { 523 drm_printf(p, "\nHarvested Instance:JPEG%d Skipping dump\n", i); 524 continue; 525 } 526 527 inst_off = i * adev->jpeg.reg_count; 528 is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1); 529 530 if (is_powered) { 531 drm_printf(p, "Active Instance:JPEG%d\n", i); 532 for (j = 0; j < adev->jpeg.reg_count; j++) 533 drm_printf(p, "%-50s \t 0x%08x\n", adev->jpeg.reg_list[j].reg_name, 534 adev->jpeg.ip_dump[inst_off + j]); 535 } else 536 drm_printf(p, "\nInactive Instance:JPEG%d\n", i); 537 } 538 } 539