1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 */ 25 26 #include <linux/kthread.h> 27 #include <linux/pci.h> 28 #include <linux/uaccess.h> 29 #include <linux/security.h> 30 #include <linux/pm_runtime.h> 31 32 #include "amdgpu.h" 33 #include "amdgpu_pm.h" 34 #include "amdgpu_dm_debugfs.h" 35 #include "amdgpu_ras.h" 36 #include "amdgpu_rap.h" 37 #include "amdgpu_securedisplay.h" 38 #include "amdgpu_fw_attestation.h" 39 #include "amdgpu_sdma.h" 40 #include "amdgpu_umr.h" 41 42 #include "amdgpu_reset.h" 43 #include "amdgpu_psp_ta.h" 44 #include "amdgpu_userq.h" 45 46 #if defined(CONFIG_DEBUG_FS) 47 48 /* Encode milliwatts in the raw Q24.8 sensor report format used by UMR. */ 49 #define AMDGPU_DEBUGFS_PWR_MW_TO_Q24_8(power_mw) \ 50 DIV_ROUND_CLOSEST_ULL((u64)(power_mw) * BIT(8), \ 51 MILLIWATT_PER_WATT) 52 53 /** 54 * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes 55 * 56 * @read: True if reading 57 * @f: open file handle 58 * @buf: User buffer to write/read to 59 * @size: Number of bytes to write/read 60 * @pos: Offset to seek to 61 * 62 * This debugfs entry has special meaning on the offset being sought. 63 * Various bits have different meanings: 64 * 65 * Bit 62: Indicates a GRBM bank switch is needed 66 * Bit 61: Indicates a SRBM bank switch is needed (implies bit 62 is 67 * zero) 68 * Bits 24..33: The SE or ME selector if needed 69 * Bits 34..43: The SH (or SA) or PIPE selector if needed 70 * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed 71 * 72 * Bit 23: Indicates that the PM power gating lock should be held 73 * This is necessary to read registers that might be 74 * unreliable during a power gating transistion. 75 * 76 * The lower bits are the BYTE offset of the register to read. This 77 * allows reading multiple registers in a single call and having 78 * the returned size reflect that. 79 */ 80 static int amdgpu_debugfs_process_reg_op(bool read, struct file *f, 81 char __user *buf, size_t size, loff_t *pos) 82 { 83 struct amdgpu_device *adev = file_inode(f)->i_private; 84 ssize_t result = 0; 85 int r; 86 bool pm_pg_lock, use_bank, use_ring; 87 unsigned int instance_bank, sh_bank, se_bank, me, pipe, queue, vmid; 88 89 pm_pg_lock = use_bank = use_ring = false; 90 instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0; 91 92 if (size & 0x3 || *pos & 0x3 || 93 ((*pos & (1ULL << 62)) && (*pos & (1ULL << 61)))) 94 return -EINVAL; 95 96 /* are we reading registers for which a PG lock is necessary? */ 97 pm_pg_lock = (*pos >> 23) & 1; 98 99 if (*pos & (1ULL << 62)) { 100 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24; 101 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34; 102 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44; 103 104 if (se_bank == 0x3FF) 105 se_bank = 0xFFFFFFFF; 106 if (sh_bank == 0x3FF) 107 sh_bank = 0xFFFFFFFF; 108 if (instance_bank == 0x3FF) 109 instance_bank = 0xFFFFFFFF; 110 use_bank = true; 111 } else if (*pos & (1ULL << 61)) { 112 113 me = (*pos & GENMASK_ULL(33, 24)) >> 24; 114 pipe = (*pos & GENMASK_ULL(43, 34)) >> 34; 115 queue = (*pos & GENMASK_ULL(53, 44)) >> 44; 116 vmid = (*pos & GENMASK_ULL(58, 54)) >> 54; 117 118 use_ring = true; 119 } else { 120 use_bank = use_ring = false; 121 } 122 123 *pos &= (1UL << 22) - 1; 124 125 if (size > PAGE_SIZE) 126 return -EINVAL; 127 128 /* 129 * Access the user buffer only while none of the locks below are held: 130 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest 131 * inside it. 132 */ 133 u32 *data __free(kfree) = kmalloc(size, GFP_KERNEL); 134 if (!data) 135 return -ENOMEM; 136 if (!read && copy_from_user(data, buf, size)) 137 return -EFAULT; 138 139 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 140 if (r < 0) { 141 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 142 return r; 143 } 144 145 r = amdgpu_virt_enable_access_debugfs(adev); 146 if (r < 0) { 147 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 148 return r; 149 } 150 151 if (use_bank) { 152 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) || 153 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) { 154 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 155 amdgpu_virt_disable_access_debugfs(adev); 156 return -EINVAL; 157 } 158 mutex_lock(&adev->grbm_idx_mutex); 159 amdgpu_gfx_select_se_sh(adev, se_bank, 160 sh_bank, instance_bank, 0); 161 } else if (use_ring) { 162 mutex_lock(&adev->srbm_mutex); 163 amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid, 0); 164 } 165 166 if (pm_pg_lock) 167 mutex_lock(&adev->pm.mutex); 168 169 while (size) { 170 if (read) 171 data[result >> 2] = RREG32(*pos >> 2); 172 else 173 amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, data[result >> 2], 0); 174 175 result += 4; 176 *pos += 4; 177 size -= 4; 178 } 179 180 if (use_bank) { 181 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0); 182 mutex_unlock(&adev->grbm_idx_mutex); 183 } else if (use_ring) { 184 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0, 0); 185 mutex_unlock(&adev->srbm_mutex); 186 } 187 188 if (pm_pg_lock) 189 mutex_unlock(&adev->pm.mutex); 190 191 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 192 193 amdgpu_virt_disable_access_debugfs(adev); 194 195 if (read && copy_to_user(buf, data, result)) 196 return -EFAULT; 197 198 return result; 199 } 200 201 /* 202 * amdgpu_debugfs_regs_read - Callback for reading MMIO registers 203 */ 204 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf, 205 size_t size, loff_t *pos) 206 { 207 return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos); 208 } 209 210 /* 211 * amdgpu_debugfs_regs_write - Callback for writing MMIO registers 212 */ 213 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf, 214 size_t size, loff_t *pos) 215 { 216 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos); 217 } 218 219 static int amdgpu_debugfs_regs2_open(struct inode *inode, struct file *file) 220 { 221 struct amdgpu_debugfs_regs2_data *rd; 222 223 rd = kzalloc_obj(*rd); 224 if (!rd) 225 return -ENOMEM; 226 rd->adev = file_inode(file)->i_private; 227 file->private_data = rd; 228 mutex_init(&rd->lock); 229 230 return 0; 231 } 232 233 static int amdgpu_debugfs_regs2_release(struct inode *inode, struct file *file) 234 { 235 struct amdgpu_debugfs_regs2_data *rd = file->private_data; 236 237 mutex_destroy(&rd->lock); 238 kfree(file->private_data); 239 return 0; 240 } 241 242 static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 offset, size_t size, int write_en) 243 { 244 struct amdgpu_debugfs_regs2_data *rd = f->private_data; 245 struct amdgpu_device *adev = rd->adev; 246 ssize_t result = 0; 247 int r; 248 249 if (size & 0x3 || offset & 0x3) 250 return -EINVAL; 251 252 if (size > PAGE_SIZE) 253 return -EINVAL; 254 255 /* 256 * Access the user buffer only while none of the locks below are held: 257 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest 258 * inside it. 259 */ 260 u32 *data __free(kfree) = kmalloc(size, GFP_KERNEL); 261 if (!data) 262 return -ENOMEM; 263 if (write_en && copy_from_user(data, buf, size)) 264 return -EFAULT; 265 266 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 267 if (r < 0) { 268 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 269 return r; 270 } 271 272 r = amdgpu_virt_enable_access_debugfs(adev); 273 if (r < 0) { 274 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 275 return r; 276 } 277 278 mutex_lock(&rd->lock); 279 280 if (rd->id.use_grbm) { 281 if ((rd->id.grbm.sh != 0xFFFFFFFF && rd->id.grbm.sh >= adev->gfx.config.max_sh_per_se) || 282 (rd->id.grbm.se != 0xFFFFFFFF && rd->id.grbm.se >= adev->gfx.config.max_shader_engines)) { 283 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 284 amdgpu_virt_disable_access_debugfs(adev); 285 mutex_unlock(&rd->lock); 286 return -EINVAL; 287 } 288 mutex_lock(&adev->grbm_idx_mutex); 289 amdgpu_gfx_select_se_sh(adev, rd->id.grbm.se, 290 rd->id.grbm.sh, 291 rd->id.grbm.instance, rd->id.xcc_id); 292 } 293 294 if (rd->id.use_srbm) { 295 mutex_lock(&adev->srbm_mutex); 296 amdgpu_gfx_select_me_pipe_q(adev, rd->id.srbm.me, rd->id.srbm.pipe, 297 rd->id.srbm.queue, rd->id.srbm.vmid, rd->id.xcc_id); 298 } 299 300 if (rd->id.pg_lock) 301 mutex_lock(&adev->pm.mutex); 302 303 while (size) { 304 if (!write_en) 305 data[result >> 2] = RREG32(offset >> 2); 306 else 307 amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, data[result >> 2], 308 rd->id.xcc_id); 309 offset += 4; 310 size -= 4; 311 result += 4; 312 } 313 314 if (rd->id.use_grbm) { 315 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id); 316 mutex_unlock(&adev->grbm_idx_mutex); 317 } 318 319 if (rd->id.use_srbm) { 320 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0, rd->id.xcc_id); 321 mutex_unlock(&adev->srbm_mutex); 322 } 323 324 if (rd->id.pg_lock) 325 mutex_unlock(&adev->pm.mutex); 326 327 mutex_unlock(&rd->lock); 328 329 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 330 331 amdgpu_virt_disable_access_debugfs(adev); 332 333 if (!write_en && copy_to_user(buf, data, result)) 334 return -EFAULT; 335 336 return result; 337 } 338 339 static long amdgpu_debugfs_regs2_ioctl(struct file *f, unsigned int cmd, unsigned long data) 340 { 341 struct amdgpu_debugfs_regs2_data *rd = f->private_data; 342 struct amdgpu_debugfs_regs2_iocdata v1_data; 343 int r; 344 345 mutex_lock(&rd->lock); 346 347 switch (cmd) { 348 case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE_V2: 349 r = copy_from_user(&rd->id, (struct amdgpu_debugfs_regs2_iocdata_v2 *)data, 350 sizeof(rd->id)); 351 if (r) 352 r = -EINVAL; 353 goto done; 354 case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE: 355 r = copy_from_user(&v1_data, (struct amdgpu_debugfs_regs2_iocdata *)data, 356 sizeof(v1_data)); 357 if (r) { 358 r = -EINVAL; 359 goto done; 360 } 361 goto v1_copy; 362 default: 363 r = -EINVAL; 364 goto done; 365 } 366 367 v1_copy: 368 rd->id.use_srbm = v1_data.use_srbm; 369 rd->id.use_grbm = v1_data.use_grbm; 370 rd->id.pg_lock = v1_data.pg_lock; 371 rd->id.grbm.se = v1_data.grbm.se; 372 rd->id.grbm.sh = v1_data.grbm.sh; 373 rd->id.grbm.instance = v1_data.grbm.instance; 374 rd->id.srbm.me = v1_data.srbm.me; 375 rd->id.srbm.pipe = v1_data.srbm.pipe; 376 rd->id.srbm.queue = v1_data.srbm.queue; 377 rd->id.xcc_id = 0; 378 done: 379 mutex_unlock(&rd->lock); 380 return r; 381 } 382 383 static ssize_t amdgpu_debugfs_regs2_read(struct file *f, char __user *buf, size_t size, loff_t *pos) 384 { 385 return amdgpu_debugfs_regs2_op(f, buf, *pos, size, 0); 386 } 387 388 static ssize_t amdgpu_debugfs_regs2_write(struct file *f, const char __user *buf, size_t size, loff_t *pos) 389 { 390 return amdgpu_debugfs_regs2_op(f, (char __user *)buf, *pos, size, 1); 391 } 392 393 static int amdgpu_debugfs_gprwave_open(struct inode *inode, struct file *file) 394 { 395 struct amdgpu_debugfs_gprwave_data *rd; 396 397 rd = kzalloc_obj(*rd); 398 if (!rd) 399 return -ENOMEM; 400 rd->adev = file_inode(file)->i_private; 401 file->private_data = rd; 402 mutex_init(&rd->lock); 403 404 return 0; 405 } 406 407 static int amdgpu_debugfs_gprwave_release(struct inode *inode, struct file *file) 408 { 409 struct amdgpu_debugfs_gprwave_data *rd = file->private_data; 410 411 mutex_destroy(&rd->lock); 412 kfree(file->private_data); 413 return 0; 414 } 415 416 static ssize_t amdgpu_debugfs_gprwave_read(struct file *f, char __user *buf, size_t size, loff_t *pos) 417 { 418 struct amdgpu_debugfs_gprwave_data *rd = f->private_data; 419 struct amdgpu_device *adev = rd->adev; 420 ssize_t result = 0; 421 int r; 422 uint32_t *data, x; 423 424 if (size > 4096 || size & 0x3 || *pos & 0x3) 425 return -EINVAL; 426 427 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 428 if (r < 0) { 429 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 430 return r; 431 } 432 433 r = amdgpu_virt_enable_access_debugfs(adev); 434 if (r < 0) { 435 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 436 return r; 437 } 438 439 data = kzalloc_objs(*data, 1024); 440 if (!data) { 441 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 442 amdgpu_virt_disable_access_debugfs(adev); 443 return -ENOMEM; 444 } 445 446 /* switch to the specific se/sh/cu */ 447 mutex_lock(&adev->grbm_idx_mutex); 448 amdgpu_gfx_select_se_sh(adev, rd->id.se, rd->id.sh, rd->id.cu, rd->id.xcc_id); 449 450 if (!rd->id.gpr_or_wave) { 451 x = 0; 452 if (adev->gfx.funcs->read_wave_data) 453 adev->gfx.funcs->read_wave_data(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, data, &x); 454 } else { 455 x = size >> 2; 456 if (rd->id.gpr.vpgr_or_sgpr) { 457 if (adev->gfx.funcs->read_wave_vgprs) 458 adev->gfx.funcs->read_wave_vgprs(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, rd->id.gpr.thread, *pos, size>>2, data); 459 } else { 460 if (adev->gfx.funcs->read_wave_sgprs) 461 adev->gfx.funcs->read_wave_sgprs(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, *pos, size>>2, data); 462 } 463 } 464 465 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, rd->id.xcc_id); 466 mutex_unlock(&adev->grbm_idx_mutex); 467 468 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 469 470 if (!x) { 471 result = -EINVAL; 472 goto done; 473 } 474 475 while (size && (*pos < x * 4)) { 476 uint32_t value; 477 478 value = data[*pos >> 2]; 479 r = put_user(value, (uint32_t *)buf); 480 if (r) { 481 result = r; 482 goto done; 483 } 484 485 result += 4; 486 buf += 4; 487 *pos += 4; 488 size -= 4; 489 } 490 491 done: 492 amdgpu_virt_disable_access_debugfs(adev); 493 kfree(data); 494 return result; 495 } 496 497 static long amdgpu_debugfs_gprwave_ioctl(struct file *f, unsigned int cmd, unsigned long data) 498 { 499 struct amdgpu_debugfs_gprwave_data *rd = f->private_data; 500 int r = 0; 501 502 mutex_lock(&rd->lock); 503 504 switch (cmd) { 505 case AMDGPU_DEBUGFS_GPRWAVE_IOC_SET_STATE: 506 if (copy_from_user(&rd->id, 507 (struct amdgpu_debugfs_gprwave_iocdata *)data, 508 sizeof(rd->id))) 509 r = -EFAULT; 510 goto done; 511 default: 512 r = -EINVAL; 513 goto done; 514 } 515 516 done: 517 mutex_unlock(&rd->lock); 518 return r; 519 } 520 521 522 523 524 /** 525 * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register 526 * 527 * @f: open file handle 528 * @buf: User buffer to store read data in 529 * @size: Number of bytes to read 530 * @pos: Offset to seek to 531 * 532 * The lower bits are the BYTE offset of the register to read. This 533 * allows reading multiple registers in a single call and having 534 * the returned size reflect that. 535 */ 536 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf, 537 size_t size, loff_t *pos) 538 { 539 struct amdgpu_device *adev = file_inode(f)->i_private; 540 ssize_t result = 0; 541 int r; 542 543 if (size & 0x3 || *pos & 0x3) 544 return -EINVAL; 545 546 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 547 if (r < 0) { 548 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 549 return r; 550 } 551 552 r = amdgpu_virt_enable_access_debugfs(adev); 553 if (r < 0) { 554 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 555 return r; 556 } 557 558 while (size) { 559 uint32_t value; 560 561 if (upper_32_bits(*pos)) 562 value = RREG32_PCIE_EXT(*pos); 563 else 564 value = RREG32_PCIE(*pos); 565 566 r = put_user(value, (uint32_t *)buf); 567 if (r) 568 goto out; 569 570 result += 4; 571 buf += 4; 572 *pos += 4; 573 size -= 4; 574 } 575 576 r = result; 577 out: 578 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 579 amdgpu_virt_disable_access_debugfs(adev); 580 return r; 581 } 582 583 /** 584 * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register 585 * 586 * @f: open file handle 587 * @buf: User buffer to write data from 588 * @size: Number of bytes to write 589 * @pos: Offset to seek to 590 * 591 * The lower bits are the BYTE offset of the register to write. This 592 * allows writing multiple registers in a single call and having 593 * the returned size reflect that. 594 */ 595 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf, 596 size_t size, loff_t *pos) 597 { 598 struct amdgpu_device *adev = file_inode(f)->i_private; 599 ssize_t result = 0; 600 int r; 601 602 if (size & 0x3 || *pos & 0x3) 603 return -EINVAL; 604 605 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 606 if (r < 0) { 607 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 608 return r; 609 } 610 611 r = amdgpu_virt_enable_access_debugfs(adev); 612 if (r < 0) { 613 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 614 return r; 615 } 616 617 while (size) { 618 uint32_t value; 619 620 r = get_user(value, (uint32_t *)buf); 621 if (r) 622 goto out; 623 624 if (upper_32_bits(*pos)) 625 WREG32_PCIE_EXT(*pos, value); 626 else 627 WREG32_PCIE(*pos, value); 628 629 result += 4; 630 buf += 4; 631 *pos += 4; 632 size -= 4; 633 } 634 635 r = result; 636 out: 637 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 638 amdgpu_virt_disable_access_debugfs(adev); 639 return r; 640 } 641 642 /** 643 * amdgpu_debugfs_regs_pcie64_read - Read from a 64-bit PCIE register 644 * 645 * @f: open file handle 646 * @buf: User buffer to store read data in 647 * @size: Number of bytes to read 648 * @pos: Offset to seek to 649 */ 650 static ssize_t amdgpu_debugfs_regs_pcie64_read(struct file *f, char __user *buf, 651 size_t size, loff_t *pos) 652 { 653 struct amdgpu_device *adev = file_inode(f)->i_private; 654 ssize_t result = 0; 655 int r; 656 657 if (size & 0x7 || *pos & 0x7) 658 return -EINVAL; 659 660 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 661 if (r < 0) { 662 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 663 return r; 664 } 665 666 r = amdgpu_virt_enable_access_debugfs(adev); 667 if (r < 0) { 668 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 669 return r; 670 } 671 672 while (size) { 673 uint64_t value; 674 675 value = RREG64_PCIE_EXT(*pos); 676 677 r = put_user(value, (uint64_t *)buf); 678 if (r) 679 goto out; 680 681 result += 8; 682 buf += 8; 683 *pos += 8; 684 size -= 8; 685 } 686 687 r = result; 688 out: 689 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 690 amdgpu_virt_disable_access_debugfs(adev); 691 return r; 692 } 693 694 /** 695 * amdgpu_debugfs_regs_pcie64_write - Write to a 64-bit PCIE register 696 * 697 * @f: open file handle 698 * @buf: User buffer to write data from 699 * @size: Number of bytes to write 700 * @pos: Offset to seek to 701 */ 702 static ssize_t amdgpu_debugfs_regs_pcie64_write(struct file *f, const char __user *buf, 703 size_t size, loff_t *pos) 704 { 705 struct amdgpu_device *adev = file_inode(f)->i_private; 706 ssize_t result = 0; 707 int r; 708 709 if (size & 0x7 || *pos & 0x7) 710 return -EINVAL; 711 712 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 713 if (r < 0) { 714 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 715 return r; 716 } 717 718 r = amdgpu_virt_enable_access_debugfs(adev); 719 if (r < 0) { 720 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 721 return r; 722 } 723 724 while (size) { 725 uint64_t value; 726 727 r = get_user(value, (uint64_t *)buf); 728 if (r) 729 goto out; 730 731 WREG64_PCIE_EXT(*pos, value); 732 733 result += 8; 734 buf += 8; 735 *pos += 8; 736 size -= 8; 737 } 738 739 r = result; 740 out: 741 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 742 amdgpu_virt_disable_access_debugfs(adev); 743 return r; 744 } 745 746 /** 747 * amdgpu_debugfs_regs_didt_read - Read from a DIDT register 748 * 749 * @f: open file handle 750 * @buf: User buffer to store read data in 751 * @size: Number of bytes to read 752 * @pos: Offset to seek to 753 * 754 * The lower bits are the BYTE offset of the register to read. This 755 * allows reading multiple registers in a single call and having 756 * the returned size reflect that. 757 */ 758 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf, 759 size_t size, loff_t *pos) 760 { 761 struct amdgpu_device *adev = file_inode(f)->i_private; 762 ssize_t result = 0; 763 int r; 764 765 if (size & 0x3 || *pos & 0x3) 766 return -EINVAL; 767 768 if (!adev->reg.didt.rreg) 769 return -EOPNOTSUPP; 770 771 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 772 if (r < 0) { 773 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 774 return r; 775 } 776 777 r = amdgpu_virt_enable_access_debugfs(adev); 778 if (r < 0) { 779 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 780 return r; 781 } 782 783 while (size) { 784 uint32_t value; 785 786 value = RREG32_DIDT(*pos >> 2); 787 r = put_user(value, (uint32_t *)buf); 788 if (r) 789 goto out; 790 791 result += 4; 792 buf += 4; 793 *pos += 4; 794 size -= 4; 795 } 796 797 r = result; 798 out: 799 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 800 amdgpu_virt_disable_access_debugfs(adev); 801 return r; 802 } 803 804 /** 805 * amdgpu_debugfs_regs_didt_write - Write to a DIDT register 806 * 807 * @f: open file handle 808 * @buf: User buffer to write data from 809 * @size: Number of bytes to write 810 * @pos: Offset to seek to 811 * 812 * The lower bits are the BYTE offset of the register to write. This 813 * allows writing multiple registers in a single call and having 814 * the returned size reflect that. 815 */ 816 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf, 817 size_t size, loff_t *pos) 818 { 819 struct amdgpu_device *adev = file_inode(f)->i_private; 820 ssize_t result = 0; 821 int r; 822 823 if (size & 0x3 || *pos & 0x3) 824 return -EINVAL; 825 826 if (!adev->reg.didt.wreg) 827 return -EOPNOTSUPP; 828 829 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 830 if (r < 0) { 831 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 832 return r; 833 } 834 835 r = amdgpu_virt_enable_access_debugfs(adev); 836 if (r < 0) { 837 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 838 return r; 839 } 840 841 while (size) { 842 uint32_t value; 843 844 r = get_user(value, (uint32_t *)buf); 845 if (r) 846 goto out; 847 848 WREG32_DIDT(*pos >> 2, value); 849 850 result += 4; 851 buf += 4; 852 *pos += 4; 853 size -= 4; 854 } 855 856 r = result; 857 out: 858 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 859 amdgpu_virt_disable_access_debugfs(adev); 860 return r; 861 } 862 863 /** 864 * amdgpu_debugfs_regs_smc_read - Read from a SMC register 865 * 866 * @f: open file handle 867 * @buf: User buffer to store read data in 868 * @size: Number of bytes to read 869 * @pos: Offset to seek to 870 * 871 * The lower bits are the BYTE offset of the register to read. This 872 * allows reading multiple registers in a single call and having 873 * the returned size reflect that. 874 */ 875 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf, 876 size_t size, loff_t *pos) 877 { 878 struct amdgpu_device *adev = file_inode(f)->i_private; 879 ssize_t result = 0; 880 int r; 881 882 if (!adev->reg.smc.rreg) 883 return -EOPNOTSUPP; 884 885 if (size & 0x3 || *pos & 0x3) 886 return -EINVAL; 887 888 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 889 if (r < 0) { 890 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 891 return r; 892 } 893 894 r = amdgpu_virt_enable_access_debugfs(adev); 895 if (r < 0) { 896 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 897 return r; 898 } 899 900 while (size) { 901 uint32_t value; 902 903 value = RREG32_SMC(*pos); 904 r = put_user(value, (uint32_t *)buf); 905 if (r) 906 goto out; 907 908 result += 4; 909 buf += 4; 910 *pos += 4; 911 size -= 4; 912 } 913 914 r = result; 915 out: 916 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 917 amdgpu_virt_disable_access_debugfs(adev); 918 return r; 919 } 920 921 /** 922 * amdgpu_debugfs_regs_smc_write - Write to a SMC register 923 * 924 * @f: open file handle 925 * @buf: User buffer to write data from 926 * @size: Number of bytes to write 927 * @pos: Offset to seek to 928 * 929 * The lower bits are the BYTE offset of the register to write. This 930 * allows writing multiple registers in a single call and having 931 * the returned size reflect that. 932 */ 933 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf, 934 size_t size, loff_t *pos) 935 { 936 struct amdgpu_device *adev = file_inode(f)->i_private; 937 ssize_t result = 0; 938 int r; 939 940 if (!adev->reg.smc.wreg) 941 return -EOPNOTSUPP; 942 943 if (size & 0x3 || *pos & 0x3) 944 return -EINVAL; 945 946 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 947 if (r < 0) { 948 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 949 return r; 950 } 951 952 r = amdgpu_virt_enable_access_debugfs(adev); 953 if (r < 0) { 954 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 955 return r; 956 } 957 958 while (size) { 959 uint32_t value; 960 961 r = get_user(value, (uint32_t *)buf); 962 if (r) 963 goto out; 964 965 WREG32_SMC(*pos, value); 966 967 result += 4; 968 buf += 4; 969 *pos += 4; 970 size -= 4; 971 } 972 973 r = result; 974 out: 975 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 976 amdgpu_virt_disable_access_debugfs(adev); 977 return r; 978 } 979 980 /** 981 * amdgpu_debugfs_gca_config_read - Read from gfx config data 982 * 983 * @f: open file handle 984 * @buf: User buffer to store read data in 985 * @size: Number of bytes to read 986 * @pos: Offset to seek to 987 * 988 * This file is used to access configuration data in a somewhat 989 * stable fashion. The format is a series of DWORDs with the first 990 * indicating which revision it is. New content is appended to the 991 * end so that older software can still read the data. 992 */ 993 994 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf, 995 size_t size, loff_t *pos) 996 { 997 struct amdgpu_device *adev = file_inode(f)->i_private; 998 ssize_t result = 0; 999 int r; 1000 uint32_t *config, no_regs = 0; 1001 1002 if (size & 0x3 || *pos & 0x3) 1003 return -EINVAL; 1004 1005 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL); 1006 if (!config) 1007 return -ENOMEM; 1008 1009 /* version, increment each time something is added */ 1010 config[no_regs++] = 5; 1011 config[no_regs++] = adev->gfx.config.max_shader_engines; 1012 config[no_regs++] = adev->gfx.config.max_tile_pipes; 1013 config[no_regs++] = adev->gfx.config.max_cu_per_sh; 1014 config[no_regs++] = adev->gfx.config.max_sh_per_se; 1015 config[no_regs++] = adev->gfx.config.max_backends_per_se; 1016 config[no_regs++] = adev->gfx.config.max_texture_channel_caches; 1017 config[no_regs++] = adev->gfx.config.max_gprs; 1018 config[no_regs++] = adev->gfx.config.max_gs_threads; 1019 config[no_regs++] = adev->gfx.config.max_hw_contexts; 1020 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend; 1021 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend; 1022 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size; 1023 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size; 1024 config[no_regs++] = adev->gfx.config.num_tile_pipes; 1025 config[no_regs++] = adev->gfx.config.backend_enable_mask; 1026 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes; 1027 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb; 1028 config[no_regs++] = adev->gfx.config.shader_engine_tile_size; 1029 config[no_regs++] = adev->gfx.config.num_gpus; 1030 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size; 1031 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg; 1032 config[no_regs++] = adev->gfx.config.gb_addr_config; 1033 config[no_regs++] = adev->gfx.config.num_rbs; 1034 1035 /* rev==1 */ 1036 config[no_regs++] = adev->rev_id; 1037 config[no_regs++] = adev->pg_flags; 1038 config[no_regs++] = lower_32_bits(adev->cg_flags); 1039 1040 /* rev==2 */ 1041 config[no_regs++] = adev->family; 1042 config[no_regs++] = adev->external_rev_id; 1043 1044 /* rev==3 */ 1045 config[no_regs++] = adev->pdev->device; 1046 config[no_regs++] = adev->pdev->revision; 1047 config[no_regs++] = adev->pdev->subsystem_device; 1048 config[no_regs++] = adev->pdev->subsystem_vendor; 1049 1050 /* rev==4 APU flag */ 1051 config[no_regs++] = adev->flags & AMD_IS_APU ? 1 : 0; 1052 1053 /* rev==5 PG/CG flag upper 32bit */ 1054 config[no_regs++] = 0; 1055 config[no_regs++] = upper_32_bits(adev->cg_flags); 1056 1057 while (size && (*pos < no_regs * 4)) { 1058 uint32_t value; 1059 1060 value = config[*pos >> 2]; 1061 r = put_user(value, (uint32_t *)buf); 1062 if (r) { 1063 kfree(config); 1064 return r; 1065 } 1066 1067 result += 4; 1068 buf += 4; 1069 *pos += 4; 1070 size -= 4; 1071 } 1072 1073 kfree(config); 1074 return result; 1075 } 1076 1077 /** 1078 * amdgpu_debugfs_sensor_read - Read from the powerplay sensors 1079 * 1080 * @f: open file handle 1081 * @buf: User buffer to store read data in 1082 * @size: Number of bytes to read 1083 * @pos: Offset to seek to 1084 * 1085 * The offset is treated as the BYTE address of one of the sensors 1086 * enumerated in amd/include/kgd_pp_interface.h under the 1087 * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK 1088 * you would use the offset 3 * 4 = 12. 1089 */ 1090 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf, 1091 size_t size, loff_t *pos) 1092 { 1093 struct amdgpu_device *adev = file_inode(f)->i_private; 1094 int idx, x, outsize, r, valuesize; 1095 uint32_t values[16]; 1096 1097 if (size & 3 || *pos & 0x3) 1098 return -EINVAL; 1099 1100 if (!adev->pm.dpm_enabled) 1101 return -EINVAL; 1102 1103 /* convert offset to sensor number */ 1104 idx = *pos >> 2; 1105 1106 valuesize = sizeof(values); 1107 1108 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1109 if (r < 0) { 1110 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1111 return r; 1112 } 1113 1114 r = amdgpu_virt_enable_access_debugfs(adev); 1115 if (r < 0) { 1116 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1117 return r; 1118 } 1119 1120 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize); 1121 1122 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1123 1124 if (r) { 1125 amdgpu_virt_disable_access_debugfs(adev); 1126 return r; 1127 } 1128 1129 if (idx == AMDGPU_PP_SENSOR_GPU_AVG_POWER || 1130 idx == AMDGPU_PP_SENSOR_GPU_INPUT_POWER) 1131 values[0] = AMDGPU_DEBUGFS_PWR_MW_TO_Q24_8(values[0]); 1132 1133 if (size > valuesize) { 1134 amdgpu_virt_disable_access_debugfs(adev); 1135 return -EINVAL; 1136 } 1137 1138 outsize = 0; 1139 x = 0; 1140 if (!r) { 1141 while (size) { 1142 r = put_user(values[x++], (int32_t *)buf); 1143 buf += 4; 1144 size -= 4; 1145 outsize += 4; 1146 } 1147 } 1148 1149 amdgpu_virt_disable_access_debugfs(adev); 1150 return !r ? outsize : r; 1151 } 1152 1153 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data 1154 * 1155 * @f: open file handle 1156 * @buf: User buffer to store read data in 1157 * @size: Number of bytes to read 1158 * @pos: Offset to seek to 1159 * 1160 * The offset being sought changes which wave that the status data 1161 * will be returned for. The bits are used as follows: 1162 * 1163 * Bits 0..6: Byte offset into data 1164 * Bits 7..14: SE selector 1165 * Bits 15..22: SH/SA selector 1166 * Bits 23..30: CU/{WGP+SIMD} selector 1167 * Bits 31..36: WAVE ID selector 1168 * Bits 37..44: SIMD ID selector 1169 * 1170 * The returned data begins with one DWORD of version information 1171 * Followed by WAVE STATUS registers relevant to the GFX IP version 1172 * being used. See gfx_v8_0_read_wave_data() for an example output. 1173 */ 1174 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf, 1175 size_t size, loff_t *pos) 1176 { 1177 struct amdgpu_device *adev = f->f_inode->i_private; 1178 int r, x; 1179 ssize_t result = 0; 1180 uint32_t offset, se, sh, cu, wave, simd, data[32]; 1181 1182 if (size & 3 || *pos & 3) 1183 return -EINVAL; 1184 1185 /* decode offset */ 1186 offset = (*pos & GENMASK_ULL(6, 0)); 1187 se = (*pos & GENMASK_ULL(14, 7)) >> 7; 1188 sh = (*pos & GENMASK_ULL(22, 15)) >> 15; 1189 cu = (*pos & GENMASK_ULL(30, 23)) >> 23; 1190 wave = (*pos & GENMASK_ULL(36, 31)) >> 31; 1191 simd = (*pos & GENMASK_ULL(44, 37)) >> 37; 1192 1193 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1194 if (r < 0) { 1195 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1196 return r; 1197 } 1198 1199 r = amdgpu_virt_enable_access_debugfs(adev); 1200 if (r < 0) { 1201 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1202 return r; 1203 } 1204 1205 /* switch to the specific se/sh/cu */ 1206 mutex_lock(&adev->grbm_idx_mutex); 1207 amdgpu_gfx_select_se_sh(adev, se, sh, cu, 0); 1208 1209 x = 0; 1210 if (adev->gfx.funcs->read_wave_data) 1211 adev->gfx.funcs->read_wave_data(adev, 0, simd, wave, data, &x); 1212 1213 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0); 1214 mutex_unlock(&adev->grbm_idx_mutex); 1215 1216 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1217 1218 if (!x) { 1219 amdgpu_virt_disable_access_debugfs(adev); 1220 return -EINVAL; 1221 } 1222 1223 while (size && (offset < x * 4)) { 1224 uint32_t value; 1225 1226 value = data[offset >> 2]; 1227 r = put_user(value, (uint32_t *)buf); 1228 if (r) { 1229 amdgpu_virt_disable_access_debugfs(adev); 1230 return r; 1231 } 1232 1233 result += 4; 1234 buf += 4; 1235 offset += 4; 1236 size -= 4; 1237 } 1238 1239 amdgpu_virt_disable_access_debugfs(adev); 1240 return result; 1241 } 1242 1243 /** amdgpu_debugfs_gpr_read - Read wave gprs 1244 * 1245 * @f: open file handle 1246 * @buf: User buffer to store read data in 1247 * @size: Number of bytes to read 1248 * @pos: Offset to seek to 1249 * 1250 * The offset being sought changes which wave that the status data 1251 * will be returned for. The bits are used as follows: 1252 * 1253 * Bits 0..11: Byte offset into data 1254 * Bits 12..19: SE selector 1255 * Bits 20..27: SH/SA selector 1256 * Bits 28..35: CU/{WGP+SIMD} selector 1257 * Bits 36..43: WAVE ID selector 1258 * Bits 37..44: SIMD ID selector 1259 * Bits 52..59: Thread selector 1260 * Bits 60..61: Bank selector (VGPR=0,SGPR=1) 1261 * 1262 * The return data comes from the SGPR or VGPR register bank for 1263 * the selected operational unit. 1264 */ 1265 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf, 1266 size_t size, loff_t *pos) 1267 { 1268 struct amdgpu_device *adev = f->f_inode->i_private; 1269 int r; 1270 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data; 1271 1272 if (size > 4096 || size & 3 || *pos & 3) 1273 return -EINVAL; 1274 1275 /* decode offset */ 1276 offset = (*pos & GENMASK_ULL(11, 0)) >> 2; 1277 se = (*pos & GENMASK_ULL(19, 12)) >> 12; 1278 sh = (*pos & GENMASK_ULL(27, 20)) >> 20; 1279 cu = (*pos & GENMASK_ULL(35, 28)) >> 28; 1280 wave = (*pos & GENMASK_ULL(43, 36)) >> 36; 1281 simd = (*pos & GENMASK_ULL(51, 44)) >> 44; 1282 thread = (*pos & GENMASK_ULL(59, 52)) >> 52; 1283 bank = (*pos & GENMASK_ULL(61, 60)) >> 60; 1284 1285 data = kzalloc_objs(*data, 1024); 1286 if (!data) 1287 return -ENOMEM; 1288 1289 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1290 if (r < 0) 1291 goto err; 1292 1293 r = amdgpu_virt_enable_access_debugfs(adev); 1294 if (r < 0) 1295 goto err; 1296 1297 /* switch to the specific se/sh/cu */ 1298 mutex_lock(&adev->grbm_idx_mutex); 1299 amdgpu_gfx_select_se_sh(adev, se, sh, cu, 0); 1300 1301 if (bank == 0) { 1302 if (adev->gfx.funcs->read_wave_vgprs) 1303 adev->gfx.funcs->read_wave_vgprs(adev, 0, simd, wave, thread, offset, size>>2, data); 1304 } else { 1305 if (adev->gfx.funcs->read_wave_sgprs) 1306 adev->gfx.funcs->read_wave_sgprs(adev, 0, simd, wave, offset, size>>2, data); 1307 } 1308 1309 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0); 1310 mutex_unlock(&adev->grbm_idx_mutex); 1311 1312 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1313 amdgpu_virt_disable_access_debugfs(adev); 1314 1315 r = copy_to_user(buf, data, size) ? -EFAULT : size; 1316 kfree(data); 1317 return r; 1318 1319 err: 1320 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1321 kfree(data); 1322 return r; 1323 } 1324 1325 /** 1326 * amdgpu_debugfs_gfxoff_residency_read - Read GFXOFF residency 1327 * 1328 * @f: open file handle 1329 * @buf: User buffer to store read data in 1330 * @size: Number of bytes to read 1331 * @pos: Offset to seek to 1332 * 1333 * Read a live GFXOFF residency sample from firmware. One needs to start logging 1334 * before getting the current value. 1335 */ 1336 static ssize_t amdgpu_debugfs_gfxoff_residency_read(struct file *f, char __user *buf, 1337 size_t size, loff_t *pos) 1338 { 1339 struct amdgpu_device *adev = file_inode(f)->i_private; 1340 ssize_t result = 0; 1341 int r; 1342 1343 if (size & 0x3 || *pos & 0x3) 1344 return -EINVAL; 1345 1346 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1347 if (r < 0) { 1348 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1349 return r; 1350 } 1351 1352 while (size) { 1353 uint32_t value; 1354 1355 r = amdgpu_get_gfx_off_residency(adev, &value); 1356 if (r) 1357 goto out; 1358 1359 r = put_user(value, (uint32_t *)buf); 1360 if (r) 1361 goto out; 1362 1363 result += 4; 1364 buf += 4; 1365 *pos += 4; 1366 size -= 4; 1367 } 1368 1369 r = result; 1370 out: 1371 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1372 1373 return r; 1374 } 1375 1376 /** 1377 * amdgpu_debugfs_gfxoff_residency_write - Log GFXOFF Residency 1378 * 1379 * @f: open file handle 1380 * @buf: User buffer to write data from 1381 * @size: Number of bytes to write 1382 * @pos: Offset to seek to 1383 * 1384 * Write a 32-bit non-zero to start logging; write a 32-bit zero to stop 1385 */ 1386 static ssize_t amdgpu_debugfs_gfxoff_residency_write(struct file *f, const char __user *buf, 1387 size_t size, loff_t *pos) 1388 { 1389 struct amdgpu_device *adev = file_inode(f)->i_private; 1390 ssize_t result = 0; 1391 int r; 1392 1393 if (size & 0x3 || *pos & 0x3) 1394 return -EINVAL; 1395 1396 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1397 if (r < 0) { 1398 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1399 return r; 1400 } 1401 1402 while (size) { 1403 u32 value; 1404 1405 r = get_user(value, (uint32_t *)buf); 1406 if (r) 1407 goto out; 1408 1409 amdgpu_set_gfx_off_residency(adev, value ? true : false); 1410 1411 result += 4; 1412 buf += 4; 1413 *pos += 4; 1414 size -= 4; 1415 } 1416 1417 r = result; 1418 out: 1419 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1420 1421 return r; 1422 } 1423 1424 1425 /** 1426 * amdgpu_debugfs_gfxoff_count_read - Read GFXOFF entry count 1427 * 1428 * @f: open file handle 1429 * @buf: User buffer to store read data in 1430 * @size: Number of bytes to read 1431 * @pos: Offset to seek to 1432 */ 1433 static ssize_t amdgpu_debugfs_gfxoff_count_read(struct file *f, char __user *buf, 1434 size_t size, loff_t *pos) 1435 { 1436 struct amdgpu_device *adev = file_inode(f)->i_private; 1437 ssize_t result = 0; 1438 int r; 1439 1440 if (size & 0x3 || *pos & 0x3) 1441 return -EINVAL; 1442 1443 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1444 if (r < 0) { 1445 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1446 return r; 1447 } 1448 1449 while (size) { 1450 u64 value = 0; 1451 1452 r = amdgpu_get_gfx_off_entrycount(adev, &value); 1453 if (r) 1454 goto out; 1455 1456 r = put_user(value, (u64 *)buf); 1457 if (r) 1458 goto out; 1459 1460 result += 4; 1461 buf += 4; 1462 *pos += 4; 1463 size -= 4; 1464 } 1465 1466 r = result; 1467 out: 1468 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1469 1470 return r; 1471 } 1472 1473 /** 1474 * amdgpu_debugfs_gfxoff_write - Enable/disable GFXOFF 1475 * 1476 * @f: open file handle 1477 * @buf: User buffer to write data from 1478 * @size: Number of bytes to write 1479 * @pos: Offset to seek to 1480 * 1481 * Write a 32-bit zero to disable or a 32-bit non-zero to enable 1482 */ 1483 static ssize_t amdgpu_debugfs_gfxoff_write(struct file *f, const char __user *buf, 1484 size_t size, loff_t *pos) 1485 { 1486 struct amdgpu_device *adev = file_inode(f)->i_private; 1487 ssize_t result = 0; 1488 int r; 1489 1490 if (size & 0x3 || *pos & 0x3) 1491 return -EINVAL; 1492 1493 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1494 if (r < 0) { 1495 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1496 return r; 1497 } 1498 1499 while (size) { 1500 uint32_t value; 1501 1502 r = get_user(value, (uint32_t *)buf); 1503 if (r) 1504 goto out; 1505 1506 amdgpu_gfx_off_ctrl(adev, value ? true : false); 1507 1508 result += 4; 1509 buf += 4; 1510 *pos += 4; 1511 size -= 4; 1512 } 1513 1514 r = result; 1515 out: 1516 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1517 1518 return r; 1519 } 1520 1521 1522 /** 1523 * amdgpu_debugfs_gfxoff_read - read gfxoff status 1524 * 1525 * @f: open file handle 1526 * @buf: User buffer to store read data in 1527 * @size: Number of bytes to read 1528 * @pos: Offset to seek to 1529 */ 1530 static ssize_t amdgpu_debugfs_gfxoff_read(struct file *f, char __user *buf, 1531 size_t size, loff_t *pos) 1532 { 1533 struct amdgpu_device *adev = file_inode(f)->i_private; 1534 ssize_t result = 0; 1535 int r; 1536 1537 if (size & 0x3 || *pos & 0x3) 1538 return -EINVAL; 1539 1540 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1541 if (r < 0) { 1542 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1543 return r; 1544 } 1545 1546 while (size) { 1547 u32 value = adev->gfx.gfx_off_state; 1548 1549 r = put_user(value, (u32 *)buf); 1550 if (r) 1551 goto out; 1552 1553 result += 4; 1554 buf += 4; 1555 *pos += 4; 1556 size -= 4; 1557 } 1558 1559 r = result; 1560 out: 1561 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1562 1563 return r; 1564 } 1565 1566 static ssize_t amdgpu_debugfs_gfxoff_status_read(struct file *f, char __user *buf, 1567 size_t size, loff_t *pos) 1568 { 1569 struct amdgpu_device *adev = file_inode(f)->i_private; 1570 ssize_t result = 0; 1571 int r; 1572 1573 if (size & 0x3 || *pos & 0x3) 1574 return -EINVAL; 1575 1576 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1577 if (r < 0) { 1578 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1579 return r; 1580 } 1581 1582 while (size) { 1583 u32 value; 1584 1585 r = amdgpu_get_gfx_off_status(adev, &value); 1586 if (r) 1587 goto out; 1588 1589 r = put_user(value, (u32 *)buf); 1590 if (r) 1591 goto out; 1592 1593 result += 4; 1594 buf += 4; 1595 *pos += 4; 1596 size -= 4; 1597 } 1598 1599 r = result; 1600 out: 1601 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1602 1603 return r; 1604 } 1605 1606 static const struct file_operations amdgpu_debugfs_regs2_fops = { 1607 .owner = THIS_MODULE, 1608 .unlocked_ioctl = amdgpu_debugfs_regs2_ioctl, 1609 .read = amdgpu_debugfs_regs2_read, 1610 .write = amdgpu_debugfs_regs2_write, 1611 .open = amdgpu_debugfs_regs2_open, 1612 .release = amdgpu_debugfs_regs2_release, 1613 .llseek = default_llseek 1614 }; 1615 1616 static const struct file_operations amdgpu_debugfs_gprwave_fops = { 1617 .owner = THIS_MODULE, 1618 .unlocked_ioctl = amdgpu_debugfs_gprwave_ioctl, 1619 .read = amdgpu_debugfs_gprwave_read, 1620 .open = amdgpu_debugfs_gprwave_open, 1621 .release = amdgpu_debugfs_gprwave_release, 1622 .llseek = default_llseek 1623 }; 1624 1625 static const struct file_operations amdgpu_debugfs_regs_fops = { 1626 .owner = THIS_MODULE, 1627 .read = amdgpu_debugfs_regs_read, 1628 .write = amdgpu_debugfs_regs_write, 1629 .llseek = default_llseek 1630 }; 1631 static const struct file_operations amdgpu_debugfs_regs_didt_fops = { 1632 .owner = THIS_MODULE, 1633 .read = amdgpu_debugfs_regs_didt_read, 1634 .write = amdgpu_debugfs_regs_didt_write, 1635 .llseek = default_llseek 1636 }; 1637 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = { 1638 .owner = THIS_MODULE, 1639 .read = amdgpu_debugfs_regs_pcie_read, 1640 .write = amdgpu_debugfs_regs_pcie_write, 1641 .llseek = default_llseek 1642 }; 1643 static const struct file_operations amdgpu_debugfs_regs_pcie64_fops = { 1644 .owner = THIS_MODULE, 1645 .read = amdgpu_debugfs_regs_pcie64_read, 1646 .write = amdgpu_debugfs_regs_pcie64_write, 1647 .llseek = default_llseek 1648 }; 1649 static const struct file_operations amdgpu_debugfs_regs_smc_fops = { 1650 .owner = THIS_MODULE, 1651 .read = amdgpu_debugfs_regs_smc_read, 1652 .write = amdgpu_debugfs_regs_smc_write, 1653 .llseek = default_llseek 1654 }; 1655 1656 static const struct file_operations amdgpu_debugfs_gca_config_fops = { 1657 .owner = THIS_MODULE, 1658 .read = amdgpu_debugfs_gca_config_read, 1659 .llseek = default_llseek 1660 }; 1661 1662 static const struct file_operations amdgpu_debugfs_sensors_fops = { 1663 .owner = THIS_MODULE, 1664 .read = amdgpu_debugfs_sensor_read, 1665 .llseek = default_llseek 1666 }; 1667 1668 static const struct file_operations amdgpu_debugfs_wave_fops = { 1669 .owner = THIS_MODULE, 1670 .read = amdgpu_debugfs_wave_read, 1671 .llseek = default_llseek 1672 }; 1673 static const struct file_operations amdgpu_debugfs_gpr_fops = { 1674 .owner = THIS_MODULE, 1675 .read = amdgpu_debugfs_gpr_read, 1676 .llseek = default_llseek 1677 }; 1678 1679 static const struct file_operations amdgpu_debugfs_gfxoff_fops = { 1680 .owner = THIS_MODULE, 1681 .read = amdgpu_debugfs_gfxoff_read, 1682 .write = amdgpu_debugfs_gfxoff_write, 1683 .llseek = default_llseek 1684 }; 1685 1686 static const struct file_operations amdgpu_debugfs_gfxoff_status_fops = { 1687 .owner = THIS_MODULE, 1688 .read = amdgpu_debugfs_gfxoff_status_read, 1689 .llseek = default_llseek 1690 }; 1691 1692 static const struct file_operations amdgpu_debugfs_gfxoff_count_fops = { 1693 .owner = THIS_MODULE, 1694 .read = amdgpu_debugfs_gfxoff_count_read, 1695 .llseek = default_llseek 1696 }; 1697 1698 static const struct file_operations amdgpu_debugfs_gfxoff_residency_fops = { 1699 .owner = THIS_MODULE, 1700 .read = amdgpu_debugfs_gfxoff_residency_read, 1701 .write = amdgpu_debugfs_gfxoff_residency_write, 1702 .llseek = default_llseek 1703 }; 1704 1705 static const struct file_operations *debugfs_regs[] = { 1706 &amdgpu_debugfs_regs_fops, 1707 &amdgpu_debugfs_regs2_fops, 1708 &amdgpu_debugfs_gprwave_fops, 1709 &amdgpu_debugfs_regs_didt_fops, 1710 &amdgpu_debugfs_regs_pcie_fops, 1711 &amdgpu_debugfs_regs_pcie64_fops, 1712 &amdgpu_debugfs_regs_smc_fops, 1713 &amdgpu_debugfs_gca_config_fops, 1714 &amdgpu_debugfs_sensors_fops, 1715 &amdgpu_debugfs_wave_fops, 1716 &amdgpu_debugfs_gpr_fops, 1717 &amdgpu_debugfs_gfxoff_fops, 1718 &amdgpu_debugfs_gfxoff_status_fops, 1719 &amdgpu_debugfs_gfxoff_count_fops, 1720 &amdgpu_debugfs_gfxoff_residency_fops, 1721 }; 1722 1723 static const char * const debugfs_regs_names[] = { 1724 "amdgpu_regs", 1725 "amdgpu_regs2", 1726 "amdgpu_gprwave", 1727 "amdgpu_regs_didt", 1728 "amdgpu_regs_pcie", 1729 "amdgpu_regs_pcie64", 1730 "amdgpu_regs_smc", 1731 "amdgpu_gca_config", 1732 "amdgpu_sensors", 1733 "amdgpu_wave", 1734 "amdgpu_gpr", 1735 "amdgpu_gfxoff", 1736 "amdgpu_gfxoff_status", 1737 "amdgpu_gfxoff_count", 1738 "amdgpu_gfxoff_residency", 1739 }; 1740 1741 /** 1742 * amdgpu_debugfs_regs_init - Initialize debugfs entries that provide 1743 * register access. 1744 * 1745 * @adev: The device to attach the debugfs entries to 1746 */ 1747 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) 1748 { 1749 struct drm_minor *minor = adev_to_drm(adev)->primary; 1750 struct dentry *ent, *root = minor->debugfs_root; 1751 unsigned int i; 1752 1753 if (security_locked_down(LOCKDOWN_PCI_ACCESS)) { 1754 drm_info(adev_to_drm(adev), 1755 "amdgpu: HW debugfs nodes disabled (kernel lockdown)\n"); 1756 return 0; 1757 } 1758 1759 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) { 1760 ent = debugfs_create_file(debugfs_regs_names[i], 1761 S_IFREG | 0400, root, 1762 adev, debugfs_regs[i]); 1763 if (!i && !IS_ERR_OR_NULL(ent)) 1764 i_size_write(ent->d_inode, adev->rmmio_size); 1765 } 1766 1767 return 0; 1768 } 1769 1770 static int amdgpu_debugfs_test_ib_show(struct seq_file *m, void *unused) 1771 { 1772 struct amdgpu_device *adev = m->private; 1773 struct drm_device *dev = adev_to_drm(adev); 1774 int r = 0, i; 1775 1776 r = pm_runtime_get_sync(dev->dev); 1777 if (r < 0) { 1778 pm_runtime_put_autosuspend(dev->dev); 1779 return r; 1780 } 1781 1782 /* Avoid accidently unparking the sched thread during GPU reset */ 1783 r = down_write_killable(&adev->reset_domain->sem); 1784 if (r) { 1785 pm_runtime_put_autosuspend(dev->dev); 1786 return r; 1787 } 1788 1789 /* hold on the scheduler */ 1790 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 1791 struct amdgpu_ring *ring = adev->rings[i]; 1792 1793 if (!amdgpu_ring_sched_ready(ring)) 1794 continue; 1795 drm_sched_wqueue_stop(&ring->sched); 1796 } 1797 1798 seq_puts(m, "run ib test:\n"); 1799 r = amdgpu_ib_ring_tests(adev); 1800 if (r) 1801 seq_printf(m, "ib ring tests failed (%d).\n", r); 1802 else 1803 seq_puts(m, "ib ring tests passed.\n"); 1804 1805 /* go on the scheduler */ 1806 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 1807 struct amdgpu_ring *ring = adev->rings[i]; 1808 1809 if (!amdgpu_ring_sched_ready(ring)) 1810 continue; 1811 drm_sched_wqueue_start(&ring->sched); 1812 } 1813 1814 up_write(&adev->reset_domain->sem); 1815 1816 pm_runtime_put_autosuspend(dev->dev); 1817 1818 return 0; 1819 } 1820 1821 static int amdgpu_debugfs_evict_vram(void *data, u64 *val) 1822 { 1823 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1824 struct drm_device *dev = adev_to_drm(adev); 1825 int r; 1826 1827 r = pm_runtime_get_sync(dev->dev); 1828 if (r < 0) { 1829 pm_runtime_put_autosuspend(dev->dev); 1830 return r; 1831 } 1832 1833 *val = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM); 1834 1835 pm_runtime_put_autosuspend(dev->dev); 1836 1837 return 0; 1838 } 1839 1840 1841 static int amdgpu_debugfs_evict_gtt(void *data, u64 *val) 1842 { 1843 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1844 struct drm_device *dev = adev_to_drm(adev); 1845 int r; 1846 1847 r = pm_runtime_get_sync(dev->dev); 1848 if (r < 0) { 1849 pm_runtime_put_autosuspend(dev->dev); 1850 return r; 1851 } 1852 1853 *val = amdgpu_ttm_evict_resources(adev, TTM_PL_TT); 1854 1855 pm_runtime_put_autosuspend(dev->dev); 1856 1857 return 0; 1858 } 1859 1860 static int amdgpu_debugfs_benchmark(void *data, u64 val) 1861 { 1862 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1863 struct drm_device *dev = adev_to_drm(adev); 1864 int r; 1865 1866 r = pm_runtime_get_sync(dev->dev); 1867 if (r < 0) { 1868 pm_runtime_put_autosuspend(dev->dev); 1869 return r; 1870 } 1871 1872 r = amdgpu_benchmark(adev, val); 1873 1874 pm_runtime_put_autosuspend(dev->dev); 1875 1876 return r; 1877 } 1878 1879 static int amdgpu_debugfs_vm_info_show(struct seq_file *m, void *unused) 1880 { 1881 struct amdgpu_device *adev = m->private; 1882 struct drm_device *dev = adev_to_drm(adev); 1883 struct drm_file *file; 1884 int r; 1885 1886 r = mutex_lock_interruptible(&dev->filelist_mutex); 1887 if (r) 1888 return r; 1889 1890 list_for_each_entry(file, &dev->filelist, lhead) { 1891 struct amdgpu_fpriv *fpriv = file->driver_priv; 1892 struct amdgpu_vm *vm = &fpriv->vm; 1893 struct amdgpu_task_info *ti; 1894 1895 ti = amdgpu_vm_get_task_info_vm(vm); 1896 if (ti) { 1897 seq_printf(m, "pid:%d\tProcess:%s ----------\n", ti->task.pid, ti->process_name); 1898 amdgpu_vm_put_task_info(ti); 1899 } 1900 1901 r = amdgpu_bo_reserve(vm->root.bo, true); 1902 if (r) 1903 break; 1904 amdgpu_debugfs_vm_bo_info(vm, m); 1905 amdgpu_bo_unreserve(vm->root.bo); 1906 } 1907 1908 mutex_unlock(&dev->filelist_mutex); 1909 1910 return r; 1911 } 1912 1913 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_test_ib); 1914 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_vm_info); 1915 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_vram_fops, amdgpu_debugfs_evict_vram, 1916 NULL, "%lld\n"); 1917 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_gtt_fops, amdgpu_debugfs_evict_gtt, 1918 NULL, "%lld\n"); 1919 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_benchmark_fops, NULL, amdgpu_debugfs_benchmark, 1920 "%lld\n"); 1921 1922 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring, 1923 struct dma_fence **fences) 1924 { 1925 struct amdgpu_fence_driver *drv = &ring->fence_drv; 1926 uint32_t sync_seq, last_seq; 1927 1928 last_seq = atomic_read(&ring->fence_drv.last_seq); 1929 sync_seq = ring->fence_drv.sync_seq; 1930 1931 last_seq &= drv->num_fences_mask; 1932 sync_seq &= drv->num_fences_mask; 1933 1934 do { 1935 struct dma_fence *fence, **ptr; 1936 1937 ++last_seq; 1938 last_seq &= drv->num_fences_mask; 1939 ptr = &drv->fences[last_seq]; 1940 1941 fence = rcu_dereference_protected(*ptr, 1); 1942 RCU_INIT_POINTER(*ptr, NULL); 1943 1944 if (!fence) 1945 continue; 1946 1947 fences[last_seq] = fence; 1948 1949 } while (last_seq != sync_seq); 1950 } 1951 1952 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences, 1953 int length) 1954 { 1955 int i; 1956 struct dma_fence *fence; 1957 1958 for (i = 0; i < length; i++) { 1959 fence = fences[i]; 1960 if (!fence) 1961 continue; 1962 dma_fence_signal(fence); 1963 dma_fence_put(fence); 1964 } 1965 } 1966 1967 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched) 1968 { 1969 struct drm_sched_job *s_job; 1970 struct dma_fence *fence; 1971 1972 spin_lock(&sched->job_list_lock); 1973 list_for_each_entry(s_job, &sched->pending_list, list) { 1974 fence = sched->ops->run_job(s_job); 1975 dma_fence_put(fence); 1976 } 1977 spin_unlock(&sched->job_list_lock); 1978 } 1979 1980 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring) 1981 { 1982 struct amdgpu_job *job; 1983 struct drm_sched_job *s_job, *tmp; 1984 uint32_t preempt_seq; 1985 struct dma_fence *fence, **ptr; 1986 struct amdgpu_fence_driver *drv = &ring->fence_drv; 1987 struct drm_gpu_scheduler *sched = &ring->sched; 1988 bool preempted = true; 1989 1990 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX) 1991 return; 1992 1993 preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2)); 1994 if (preempt_seq <= atomic_read(&drv->last_seq)) { 1995 preempted = false; 1996 goto no_preempt; 1997 } 1998 1999 preempt_seq &= drv->num_fences_mask; 2000 ptr = &drv->fences[preempt_seq]; 2001 fence = rcu_dereference_protected(*ptr, 1); 2002 2003 no_preempt: 2004 spin_lock(&sched->job_list_lock); 2005 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) { 2006 if (dma_fence_is_signaled(&s_job->s_fence->finished)) { 2007 /* remove job from ring_mirror_list */ 2008 list_del_init(&s_job->list); 2009 sched->ops->free_job(s_job); 2010 continue; 2011 } 2012 job = to_amdgpu_job(s_job); 2013 if (preempted && (&job->hw_fence->base) == fence) 2014 /* mark the job as preempted */ 2015 job->preemption_status |= AMDGPU_IB_PREEMPTED; 2016 } 2017 spin_unlock(&sched->job_list_lock); 2018 } 2019 2020 static int amdgpu_debugfs_ib_preempt(void *data, u64 val) 2021 { 2022 int r, length; 2023 struct amdgpu_ring *ring; 2024 struct dma_fence **fences = NULL; 2025 struct amdgpu_device *adev = (struct amdgpu_device *)data; 2026 2027 if (val >= AMDGPU_MAX_RINGS) 2028 return -EINVAL; 2029 2030 ring = adev->rings[val]; 2031 2032 if (!amdgpu_ring_sched_ready(ring) || 2033 !ring->funcs->preempt_ib) 2034 return -EINVAL; 2035 2036 /* the last preemption failed */ 2037 if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr)) 2038 return -EBUSY; 2039 2040 length = ring->fence_drv.num_fences_mask + 1; 2041 fences = kzalloc_objs(*fences, length); 2042 if (!fences) 2043 return -ENOMEM; 2044 2045 /* Avoid accidently unparking the sched thread during GPU reset */ 2046 r = down_read_killable(&adev->reset_domain->sem); 2047 if (r) 2048 goto pro_end; 2049 2050 /* stop the scheduler */ 2051 drm_sched_wqueue_stop(&ring->sched); 2052 2053 /* preempt the IB */ 2054 r = amdgpu_ring_preempt_ib(ring); 2055 if (r) { 2056 drm_warn(adev_to_drm(adev), "failed to preempt ring %d\n", ring->idx); 2057 goto failure; 2058 } 2059 2060 amdgpu_fence_process(ring); 2061 2062 if (atomic_read(&ring->fence_drv.last_seq) != 2063 ring->fence_drv.sync_seq) { 2064 drm_info(adev_to_drm(adev), "ring %d was preempted\n", ring->idx); 2065 2066 amdgpu_ib_preempt_mark_partial_job(ring); 2067 2068 /* swap out the old fences */ 2069 amdgpu_ib_preempt_fences_swap(ring, fences); 2070 2071 amdgpu_fence_driver_force_completion(ring, NULL); 2072 2073 /* resubmit unfinished jobs */ 2074 amdgpu_ib_preempt_job_recovery(&ring->sched); 2075 2076 /* wait for jobs finished */ 2077 amdgpu_fence_wait_empty(ring); 2078 2079 /* signal the old fences */ 2080 amdgpu_ib_preempt_signal_fences(fences, length); 2081 } 2082 2083 failure: 2084 /* restart the scheduler */ 2085 drm_sched_wqueue_start(&ring->sched); 2086 2087 up_read(&adev->reset_domain->sem); 2088 2089 pro_end: 2090 kfree(fences); 2091 2092 return r; 2093 } 2094 2095 static int amdgpu_debugfs_sclk_set(void *data, u64 val) 2096 { 2097 int ret = 0; 2098 uint32_t max_freq, min_freq; 2099 struct amdgpu_device *adev = (struct amdgpu_device *)data; 2100 2101 if (amdgpu_sriov_multi_vf_mode(adev)) 2102 return -EINVAL; 2103 2104 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2105 if (ret < 0) { 2106 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2107 return ret; 2108 } 2109 2110 ret = amdgpu_dpm_get_dpm_freq_range(adev, PP_SCLK, &min_freq, &max_freq); 2111 if (ret == -EOPNOTSUPP) { 2112 ret = 0; 2113 goto out; 2114 } 2115 if (ret || val > max_freq || val < min_freq) { 2116 ret = -EINVAL; 2117 goto out; 2118 } 2119 2120 ret = amdgpu_dpm_set_soft_freq_range(adev, PP_SCLK, (uint32_t)val, (uint32_t)val); 2121 if (ret) 2122 ret = -EINVAL; 2123 2124 out: 2125 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2126 2127 return ret; 2128 } 2129 2130 DEFINE_DEBUGFS_ATTRIBUTE(fops_ib_preempt, NULL, 2131 amdgpu_debugfs_ib_preempt, "%llu\n"); 2132 2133 DEFINE_DEBUGFS_ATTRIBUTE(fops_sclk_set, NULL, 2134 amdgpu_debugfs_sclk_set, "%llu\n"); 2135 2136 int amdgpu_debugfs_init(struct amdgpu_device *adev) 2137 { 2138 struct dentry *root = adev_to_drm(adev)->primary->debugfs_root; 2139 struct dentry *ent; 2140 int r, i; 2141 2142 if (!debugfs_initialized()) 2143 return 0; 2144 2145 debugfs_create_x32("amdgpu_smu_debug", 0600, root, 2146 &adev->pm.smu_debug_mask); 2147 2148 debugfs_create_x64("unique_id", 0444, root, &adev->unique_id); 2149 debugfs_create_x8("unitid", 0444, root, &adev->unitid); 2150 2151 ent = debugfs_create_file("amdgpu_preempt_ib", 0600, root, adev, 2152 &fops_ib_preempt); 2153 if (IS_ERR(ent)) { 2154 drm_err(adev_to_drm(adev), 2155 "unable to create amdgpu_preempt_ib debugsfs file\n"); 2156 return PTR_ERR(ent); 2157 } 2158 2159 ent = debugfs_create_file("amdgpu_force_sclk", 0200, root, adev, 2160 &fops_sclk_set); 2161 if (IS_ERR(ent)) { 2162 drm_err(adev_to_drm(adev), 2163 "unable to create amdgpu_set_sclk debugsfs file\n"); 2164 return PTR_ERR(ent); 2165 } 2166 2167 /* Register debugfs entries for amdgpu_ttm */ 2168 amdgpu_ttm_debugfs_init(adev); 2169 amdgpu_debugfs_pm_init(adev); 2170 amdgpu_debugfs_sa_init(adev); 2171 amdgpu_debugfs_fence_init(adev); 2172 amdgpu_debugfs_gem_init(adev); 2173 2174 r = amdgpu_debugfs_regs_init(adev); 2175 if (r) 2176 drm_err(adev_to_drm(adev), "registering register debugfs failed (%d).\n", r); 2177 2178 amdgpu_debugfs_firmware_init(adev); 2179 amdgpu_ta_if_debugfs_init(adev); 2180 2181 amdgpu_debugfs_mes_init(adev); 2182 2183 #if defined(CONFIG_DRM_AMD_DC) 2184 if (adev->dc_enabled) 2185 dtn_debugfs_init(adev); 2186 #endif 2187 2188 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 2189 struct amdgpu_ring *ring = adev->rings[i]; 2190 2191 if (!ring) 2192 continue; 2193 if (ring == &adev->cper.ring_buf && !adev->cper.enabled) 2194 continue; 2195 2196 amdgpu_debugfs_ring_init(adev, ring); 2197 } 2198 2199 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 2200 if (!amdgpu_vcnfw_log) 2201 break; 2202 2203 if (adev->vcn.harvest_config & (1 << i)) 2204 continue; 2205 2206 amdgpu_debugfs_vcn_fwlog_init(adev, i, &adev->vcn.inst[i]); 2207 } 2208 2209 if (amdgpu_umsch_mm & amdgpu_umsch_mm_fwlog) 2210 amdgpu_debugfs_umsch_fwlog_init(adev, &adev->umsch_mm); 2211 2212 amdgpu_debugfs_vcn_sched_mask_init(adev); 2213 amdgpu_debugfs_jpeg_sched_mask_init(adev); 2214 amdgpu_debugfs_gfx_sched_mask_init(adev); 2215 amdgpu_debugfs_compute_sched_mask_init(adev); 2216 amdgpu_debugfs_sdma_sched_mask_init(adev); 2217 2218 amdgpu_ras_debugfs_create_all(adev); 2219 amdgpu_rap_debugfs_init(adev); 2220 amdgpu_securedisplay_debugfs_init(adev); 2221 amdgpu_fw_attestation_debugfs_init(adev); 2222 amdgpu_psp_debugfs_init(adev); 2223 2224 debugfs_create_file("amdgpu_evict_vram", 0400, root, adev, 2225 &amdgpu_evict_vram_fops); 2226 debugfs_create_file("amdgpu_evict_gtt", 0400, root, adev, 2227 &amdgpu_evict_gtt_fops); 2228 debugfs_create_file("amdgpu_test_ib", 0400, root, adev, 2229 &amdgpu_debugfs_test_ib_fops); 2230 debugfs_create_file("amdgpu_vm_info", 0444, root, adev, 2231 &amdgpu_debugfs_vm_info_fops); 2232 debugfs_create_file("amdgpu_benchmark", 0200, root, adev, 2233 &amdgpu_benchmark_fops); 2234 2235 /* Debug-only: bitmap of incoming UALink protocol messages to drop. 2236 * Each bit position corresponds to an enum AMDGPU_UALINK_PROTOCOL_MESSAGES 2237 * value. Setting a bit causes exactly one matching incoming packet to be 2238 * dropped, after which the bit auto-clears and traffic resumes. Used to 2239 * exercise the connection reset paths. 2240 * 2241 * Examples (drop one NPA-REQ): 2242 * echo 0x8 > /sys/kernel/debug/dri/0/amdgpu_ualink_drop_msg_bitmap 2243 * (drop one NPA-RSP): 2244 * echo 0x10 > /sys/kernel/debug/dri/0/amdgpu_ualink_drop_msg_bitmap 2245 */ 2246 debugfs_create_ulong("amdgpu_ualink_drop_msg_bitmap", 0600, root, 2247 &adev->ualink.drop_msg_bitmap); 2248 2249 adev->debugfs_vbios_blob.data = adev->bios; 2250 adev->debugfs_vbios_blob.size = adev->bios_size; 2251 debugfs_create_blob("amdgpu_vbios", 0444, root, 2252 &adev->debugfs_vbios_blob); 2253 2254 if (adev->discovery.debugfs_blob.size) 2255 debugfs_create_blob("amdgpu_discovery", 0444, root, 2256 &adev->discovery.debugfs_blob); 2257 2258 return 0; 2259 } 2260 2261 static int amdgpu_pt_info_read(struct seq_file *m, void *unused) 2262 { 2263 struct drm_file *file; 2264 struct amdgpu_fpriv *fpriv; 2265 struct amdgpu_bo *root_bo; 2266 struct amdgpu_device *adev; 2267 int r; 2268 2269 file = m->private; 2270 if (!file) 2271 return -EINVAL; 2272 2273 adev = drm_to_adev(file->minor->dev); 2274 fpriv = file->driver_priv; 2275 if (!fpriv || !fpriv->vm.root.bo) 2276 return -ENODEV; 2277 2278 root_bo = amdgpu_bo_ref(fpriv->vm.root.bo); 2279 r = amdgpu_bo_reserve(root_bo, true); 2280 if (r) { 2281 amdgpu_bo_unref(&root_bo); 2282 return -EINVAL; 2283 } 2284 2285 seq_printf(m, "pd_address: 0x%llx\n", amdgpu_gmc_pd_addr(fpriv->vm.root.bo)); 2286 seq_printf(m, "max_pfn: 0x%llx\n", adev->vm_manager.max_pfn); 2287 seq_printf(m, "num_level: 0x%x\n", adev->vm_manager.num_level); 2288 seq_printf(m, "block_size: 0x%x\n", adev->vm_manager.block_size); 2289 seq_printf(m, "fragment_size: 0x%x\n", adev->vm_manager.fragment_size); 2290 2291 amdgpu_bo_unreserve(root_bo); 2292 amdgpu_bo_unref(&root_bo); 2293 2294 return 0; 2295 } 2296 2297 static int amdgpu_pt_info_open(struct inode *inode, struct file *file) 2298 { 2299 return single_open(file, amdgpu_pt_info_read, inode->i_private); 2300 } 2301 2302 static const struct file_operations amdgpu_pt_info_fops = { 2303 .owner = THIS_MODULE, 2304 .open = amdgpu_pt_info_open, 2305 .read = seq_read, 2306 .llseek = seq_lseek, 2307 .release = single_release, 2308 }; 2309 2310 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused) 2311 { 2312 struct amdgpu_usermode_queue *queue = m->private; 2313 struct amdgpu_bo *bo; 2314 int r; 2315 2316 if (!queue || !queue->mqd.obj) 2317 return -EINVAL; 2318 2319 bo = amdgpu_bo_ref(queue->mqd.obj); 2320 r = amdgpu_bo_reserve(bo, true); 2321 if (r) { 2322 amdgpu_bo_unref(&bo); 2323 return -EINVAL; 2324 } 2325 2326 seq_printf(m, "queue_type: %d\n", queue->queue_type); 2327 seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj)); 2328 2329 amdgpu_bo_unreserve(bo); 2330 amdgpu_bo_unref(&bo); 2331 2332 return 0; 2333 } 2334 2335 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file) 2336 { 2337 return single_open(file, amdgpu_mqd_info_read, inode->i_private); 2338 } 2339 2340 static const struct file_operations amdgpu_mqd_info_fops = { 2341 .owner = THIS_MODULE, 2342 .open = amdgpu_mqd_info_open, 2343 .read = seq_read, 2344 .llseek = seq_lseek, 2345 .release = single_release, 2346 }; 2347 2348 void amdgpu_debugfs_userq_init(struct drm_file *file, struct amdgpu_usermode_queue *queue, int qid) 2349 { 2350 char queue_name[32]; 2351 2352 scnprintf(queue_name, sizeof(queue_name), "queue_%d", qid); 2353 queue->debugfs_queue = debugfs_create_dir(queue_name, file->debugfs_client); 2354 debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); 2355 } 2356 2357 void amdgpu_debugfs_vm_init(struct drm_file *file) 2358 { 2359 debugfs_create_file("vm_pagetable_info", 0444, file->debugfs_client, file, 2360 &amdgpu_pt_info_fops); 2361 } 2362 2363 #else 2364 int amdgpu_debugfs_init(struct amdgpu_device *adev) 2365 { 2366 return 0; 2367 } 2368 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) 2369 { 2370 return 0; 2371 } 2372 void amdgpu_debugfs_vm_init(struct drm_file *file) 2373 { 2374 } 2375 void amdgpu_debugfs_userq_init(struct drm_file *file, 2376 struct amdgpu_usermode_queue *queue, 2377 int qid) 2378 { 2379 } 2380 #endif 2381