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 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 126 if (r < 0) { 127 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 128 return r; 129 } 130 131 r = amdgpu_virt_enable_access_debugfs(adev); 132 if (r < 0) { 133 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 134 return r; 135 } 136 137 if (use_bank) { 138 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) || 139 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) { 140 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 141 amdgpu_virt_disable_access_debugfs(adev); 142 return -EINVAL; 143 } 144 mutex_lock(&adev->grbm_idx_mutex); 145 amdgpu_gfx_select_se_sh(adev, se_bank, 146 sh_bank, instance_bank, 0); 147 } else if (use_ring) { 148 mutex_lock(&adev->srbm_mutex); 149 amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid, 0); 150 } 151 152 if (pm_pg_lock) 153 mutex_lock(&adev->pm.mutex); 154 155 while (size) { 156 uint32_t value; 157 158 if (read) { 159 value = RREG32(*pos >> 2); 160 r = put_user(value, (uint32_t *)buf); 161 } else { 162 r = get_user(value, (uint32_t *)buf); 163 if (!r) 164 amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0); 165 } 166 if (r) { 167 result = r; 168 goto end; 169 } 170 171 result += 4; 172 buf += 4; 173 *pos += 4; 174 size -= 4; 175 } 176 177 end: 178 if (use_bank) { 179 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0); 180 mutex_unlock(&adev->grbm_idx_mutex); 181 } else if (use_ring) { 182 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0, 0); 183 mutex_unlock(&adev->srbm_mutex); 184 } 185 186 if (pm_pg_lock) 187 mutex_unlock(&adev->pm.mutex); 188 189 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 190 191 amdgpu_virt_disable_access_debugfs(adev); 192 return result; 193 } 194 195 /* 196 * amdgpu_debugfs_regs_read - Callback for reading MMIO registers 197 */ 198 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf, 199 size_t size, loff_t *pos) 200 { 201 return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos); 202 } 203 204 /* 205 * amdgpu_debugfs_regs_write - Callback for writing MMIO registers 206 */ 207 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf, 208 size_t size, loff_t *pos) 209 { 210 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos); 211 } 212 213 static int amdgpu_debugfs_regs2_open(struct inode *inode, struct file *file) 214 { 215 struct amdgpu_debugfs_regs2_data *rd; 216 217 rd = kzalloc_obj(*rd); 218 if (!rd) 219 return -ENOMEM; 220 rd->adev = file_inode(file)->i_private; 221 file->private_data = rd; 222 mutex_init(&rd->lock); 223 224 return 0; 225 } 226 227 static int amdgpu_debugfs_regs2_release(struct inode *inode, struct file *file) 228 { 229 struct amdgpu_debugfs_regs2_data *rd = file->private_data; 230 231 mutex_destroy(&rd->lock); 232 kfree(file->private_data); 233 return 0; 234 } 235 236 static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 offset, size_t size, int write_en) 237 { 238 struct amdgpu_debugfs_regs2_data *rd = f->private_data; 239 struct amdgpu_device *adev = rd->adev; 240 ssize_t result = 0; 241 int r; 242 uint32_t value; 243 244 if (size & 0x3 || offset & 0x3) 245 return -EINVAL; 246 247 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 248 if (r < 0) { 249 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 250 return r; 251 } 252 253 r = amdgpu_virt_enable_access_debugfs(adev); 254 if (r < 0) { 255 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 256 return r; 257 } 258 259 mutex_lock(&rd->lock); 260 261 if (rd->id.use_grbm) { 262 if ((rd->id.grbm.sh != 0xFFFFFFFF && rd->id.grbm.sh >= adev->gfx.config.max_sh_per_se) || 263 (rd->id.grbm.se != 0xFFFFFFFF && rd->id.grbm.se >= adev->gfx.config.max_shader_engines)) { 264 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 265 amdgpu_virt_disable_access_debugfs(adev); 266 mutex_unlock(&rd->lock); 267 return -EINVAL; 268 } 269 mutex_lock(&adev->grbm_idx_mutex); 270 amdgpu_gfx_select_se_sh(adev, rd->id.grbm.se, 271 rd->id.grbm.sh, 272 rd->id.grbm.instance, rd->id.xcc_id); 273 } 274 275 if (rd->id.use_srbm) { 276 mutex_lock(&adev->srbm_mutex); 277 amdgpu_gfx_select_me_pipe_q(adev, rd->id.srbm.me, rd->id.srbm.pipe, 278 rd->id.srbm.queue, rd->id.srbm.vmid, rd->id.xcc_id); 279 } 280 281 if (rd->id.pg_lock) 282 mutex_lock(&adev->pm.mutex); 283 284 while (size) { 285 if (!write_en) { 286 value = RREG32(offset >> 2); 287 r = put_user(value, (uint32_t *)buf); 288 } else { 289 r = get_user(value, (uint32_t *)buf); 290 if (!r) 291 amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value, rd->id.xcc_id); 292 } 293 if (r) { 294 result = r; 295 goto end; 296 } 297 offset += 4; 298 size -= 4; 299 result += 4; 300 buf += 4; 301 } 302 end: 303 if (rd->id.use_grbm) { 304 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id); 305 mutex_unlock(&adev->grbm_idx_mutex); 306 } 307 308 if (rd->id.use_srbm) { 309 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0, rd->id.xcc_id); 310 mutex_unlock(&adev->srbm_mutex); 311 } 312 313 if (rd->id.pg_lock) 314 mutex_unlock(&adev->pm.mutex); 315 316 mutex_unlock(&rd->lock); 317 318 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 319 320 amdgpu_virt_disable_access_debugfs(adev); 321 return result; 322 } 323 324 static long amdgpu_debugfs_regs2_ioctl(struct file *f, unsigned int cmd, unsigned long data) 325 { 326 struct amdgpu_debugfs_regs2_data *rd = f->private_data; 327 struct amdgpu_debugfs_regs2_iocdata v1_data; 328 int r; 329 330 mutex_lock(&rd->lock); 331 332 switch (cmd) { 333 case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE_V2: 334 r = copy_from_user(&rd->id, (struct amdgpu_debugfs_regs2_iocdata_v2 *)data, 335 sizeof(rd->id)); 336 if (r) 337 r = -EINVAL; 338 goto done; 339 case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE: 340 r = copy_from_user(&v1_data, (struct amdgpu_debugfs_regs2_iocdata *)data, 341 sizeof(v1_data)); 342 if (r) { 343 r = -EINVAL; 344 goto done; 345 } 346 goto v1_copy; 347 default: 348 r = -EINVAL; 349 goto done; 350 } 351 352 v1_copy: 353 rd->id.use_srbm = v1_data.use_srbm; 354 rd->id.use_grbm = v1_data.use_grbm; 355 rd->id.pg_lock = v1_data.pg_lock; 356 rd->id.grbm.se = v1_data.grbm.se; 357 rd->id.grbm.sh = v1_data.grbm.sh; 358 rd->id.grbm.instance = v1_data.grbm.instance; 359 rd->id.srbm.me = v1_data.srbm.me; 360 rd->id.srbm.pipe = v1_data.srbm.pipe; 361 rd->id.srbm.queue = v1_data.srbm.queue; 362 rd->id.xcc_id = 0; 363 done: 364 mutex_unlock(&rd->lock); 365 return r; 366 } 367 368 static ssize_t amdgpu_debugfs_regs2_read(struct file *f, char __user *buf, size_t size, loff_t *pos) 369 { 370 return amdgpu_debugfs_regs2_op(f, buf, *pos, size, 0); 371 } 372 373 static ssize_t amdgpu_debugfs_regs2_write(struct file *f, const char __user *buf, size_t size, loff_t *pos) 374 { 375 return amdgpu_debugfs_regs2_op(f, (char __user *)buf, *pos, size, 1); 376 } 377 378 static int amdgpu_debugfs_gprwave_open(struct inode *inode, struct file *file) 379 { 380 struct amdgpu_debugfs_gprwave_data *rd; 381 382 rd = kzalloc_obj(*rd); 383 if (!rd) 384 return -ENOMEM; 385 rd->adev = file_inode(file)->i_private; 386 file->private_data = rd; 387 mutex_init(&rd->lock); 388 389 return 0; 390 } 391 392 static int amdgpu_debugfs_gprwave_release(struct inode *inode, struct file *file) 393 { 394 struct amdgpu_debugfs_gprwave_data *rd = file->private_data; 395 396 mutex_destroy(&rd->lock); 397 kfree(file->private_data); 398 return 0; 399 } 400 401 static ssize_t amdgpu_debugfs_gprwave_read(struct file *f, char __user *buf, size_t size, loff_t *pos) 402 { 403 struct amdgpu_debugfs_gprwave_data *rd = f->private_data; 404 struct amdgpu_device *adev = rd->adev; 405 ssize_t result = 0; 406 int r; 407 uint32_t *data, x; 408 409 if (size > 4096 || size & 0x3 || *pos & 0x3) 410 return -EINVAL; 411 412 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 413 if (r < 0) { 414 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 415 return r; 416 } 417 418 r = amdgpu_virt_enable_access_debugfs(adev); 419 if (r < 0) { 420 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 421 return r; 422 } 423 424 data = kcalloc(1024, sizeof(*data), GFP_KERNEL); 425 if (!data) { 426 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 427 amdgpu_virt_disable_access_debugfs(adev); 428 return -ENOMEM; 429 } 430 431 /* switch to the specific se/sh/cu */ 432 mutex_lock(&adev->grbm_idx_mutex); 433 amdgpu_gfx_select_se_sh(adev, rd->id.se, rd->id.sh, rd->id.cu, rd->id.xcc_id); 434 435 if (!rd->id.gpr_or_wave) { 436 x = 0; 437 if (adev->gfx.funcs->read_wave_data) 438 adev->gfx.funcs->read_wave_data(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, data, &x); 439 } else { 440 x = size >> 2; 441 if (rd->id.gpr.vpgr_or_sgpr) { 442 if (adev->gfx.funcs->read_wave_vgprs) 443 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); 444 } else { 445 if (adev->gfx.funcs->read_wave_sgprs) 446 adev->gfx.funcs->read_wave_sgprs(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, *pos, size>>2, data); 447 } 448 } 449 450 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, rd->id.xcc_id); 451 mutex_unlock(&adev->grbm_idx_mutex); 452 453 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 454 455 if (!x) { 456 result = -EINVAL; 457 goto done; 458 } 459 460 while (size && (*pos < x * 4)) { 461 uint32_t value; 462 463 value = data[*pos >> 2]; 464 r = put_user(value, (uint32_t *)buf); 465 if (r) { 466 result = r; 467 goto done; 468 } 469 470 result += 4; 471 buf += 4; 472 *pos += 4; 473 size -= 4; 474 } 475 476 done: 477 amdgpu_virt_disable_access_debugfs(adev); 478 kfree(data); 479 return result; 480 } 481 482 static long amdgpu_debugfs_gprwave_ioctl(struct file *f, unsigned int cmd, unsigned long data) 483 { 484 struct amdgpu_debugfs_gprwave_data *rd = f->private_data; 485 int r = 0; 486 487 mutex_lock(&rd->lock); 488 489 switch (cmd) { 490 case AMDGPU_DEBUGFS_GPRWAVE_IOC_SET_STATE: 491 if (copy_from_user(&rd->id, 492 (struct amdgpu_debugfs_gprwave_iocdata *)data, 493 sizeof(rd->id))) 494 r = -EFAULT; 495 goto done; 496 default: 497 r = -EINVAL; 498 goto done; 499 } 500 501 done: 502 mutex_unlock(&rd->lock); 503 return r; 504 } 505 506 507 508 509 /** 510 * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register 511 * 512 * @f: open file handle 513 * @buf: User buffer to store read data in 514 * @size: Number of bytes to read 515 * @pos: Offset to seek to 516 * 517 * The lower bits are the BYTE offset of the register to read. This 518 * allows reading multiple registers in a single call and having 519 * the returned size reflect that. 520 */ 521 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf, 522 size_t size, loff_t *pos) 523 { 524 struct amdgpu_device *adev = file_inode(f)->i_private; 525 ssize_t result = 0; 526 int r; 527 528 if (size & 0x3 || *pos & 0x3) 529 return -EINVAL; 530 531 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 532 if (r < 0) { 533 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 534 return r; 535 } 536 537 r = amdgpu_virt_enable_access_debugfs(adev); 538 if (r < 0) { 539 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 540 return r; 541 } 542 543 while (size) { 544 uint32_t value; 545 546 if (upper_32_bits(*pos)) 547 value = RREG32_PCIE_EXT(*pos); 548 else 549 value = RREG32_PCIE(*pos); 550 551 r = put_user(value, (uint32_t *)buf); 552 if (r) 553 goto out; 554 555 result += 4; 556 buf += 4; 557 *pos += 4; 558 size -= 4; 559 } 560 561 r = result; 562 out: 563 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 564 amdgpu_virt_disable_access_debugfs(adev); 565 return r; 566 } 567 568 /** 569 * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register 570 * 571 * @f: open file handle 572 * @buf: User buffer to write data from 573 * @size: Number of bytes to write 574 * @pos: Offset to seek to 575 * 576 * The lower bits are the BYTE offset of the register to write. This 577 * allows writing multiple registers in a single call and having 578 * the returned size reflect that. 579 */ 580 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf, 581 size_t size, loff_t *pos) 582 { 583 struct amdgpu_device *adev = file_inode(f)->i_private; 584 ssize_t result = 0; 585 int r; 586 587 if (size & 0x3 || *pos & 0x3) 588 return -EINVAL; 589 590 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 591 if (r < 0) { 592 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 593 return r; 594 } 595 596 r = amdgpu_virt_enable_access_debugfs(adev); 597 if (r < 0) { 598 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 599 return r; 600 } 601 602 while (size) { 603 uint32_t value; 604 605 r = get_user(value, (uint32_t *)buf); 606 if (r) 607 goto out; 608 609 if (upper_32_bits(*pos)) 610 WREG32_PCIE_EXT(*pos, value); 611 else 612 WREG32_PCIE(*pos, value); 613 614 result += 4; 615 buf += 4; 616 *pos += 4; 617 size -= 4; 618 } 619 620 r = result; 621 out: 622 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 623 amdgpu_virt_disable_access_debugfs(adev); 624 return r; 625 } 626 627 /** 628 * amdgpu_debugfs_regs_pcie64_read - Read from a 64-bit PCIE register 629 * 630 * @f: open file handle 631 * @buf: User buffer to store read data in 632 * @size: Number of bytes to read 633 * @pos: Offset to seek to 634 */ 635 static ssize_t amdgpu_debugfs_regs_pcie64_read(struct file *f, char __user *buf, 636 size_t size, loff_t *pos) 637 { 638 struct amdgpu_device *adev = file_inode(f)->i_private; 639 ssize_t result = 0; 640 int r; 641 642 if (size & 0x7 || *pos & 0x7) 643 return -EINVAL; 644 645 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 646 if (r < 0) { 647 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 648 return r; 649 } 650 651 r = amdgpu_virt_enable_access_debugfs(adev); 652 if (r < 0) { 653 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 654 return r; 655 } 656 657 while (size) { 658 uint64_t value; 659 660 value = RREG64_PCIE_EXT(*pos); 661 662 r = put_user(value, (uint64_t *)buf); 663 if (r) 664 goto out; 665 666 result += 8; 667 buf += 8; 668 *pos += 8; 669 size -= 8; 670 } 671 672 r = result; 673 out: 674 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 675 amdgpu_virt_disable_access_debugfs(adev); 676 return r; 677 } 678 679 /** 680 * amdgpu_debugfs_regs_pcie64_write - Write to a 64-bit PCIE register 681 * 682 * @f: open file handle 683 * @buf: User buffer to write data from 684 * @size: Number of bytes to write 685 * @pos: Offset to seek to 686 */ 687 static ssize_t amdgpu_debugfs_regs_pcie64_write(struct file *f, const char __user *buf, 688 size_t size, loff_t *pos) 689 { 690 struct amdgpu_device *adev = file_inode(f)->i_private; 691 ssize_t result = 0; 692 int r; 693 694 if (size & 0x7 || *pos & 0x7) 695 return -EINVAL; 696 697 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 698 if (r < 0) { 699 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 700 return r; 701 } 702 703 r = amdgpu_virt_enable_access_debugfs(adev); 704 if (r < 0) { 705 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 706 return r; 707 } 708 709 while (size) { 710 uint64_t value; 711 712 r = get_user(value, (uint64_t *)buf); 713 if (r) 714 goto out; 715 716 WREG64_PCIE_EXT(*pos, value); 717 718 result += 8; 719 buf += 8; 720 *pos += 8; 721 size -= 8; 722 } 723 724 r = result; 725 out: 726 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 727 amdgpu_virt_disable_access_debugfs(adev); 728 return r; 729 } 730 731 /** 732 * amdgpu_debugfs_regs_didt_read - Read from a DIDT register 733 * 734 * @f: open file handle 735 * @buf: User buffer to store read data in 736 * @size: Number of bytes to read 737 * @pos: Offset to seek to 738 * 739 * The lower bits are the BYTE offset of the register to read. This 740 * allows reading multiple registers in a single call and having 741 * the returned size reflect that. 742 */ 743 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf, 744 size_t size, loff_t *pos) 745 { 746 struct amdgpu_device *adev = file_inode(f)->i_private; 747 ssize_t result = 0; 748 int r; 749 750 if (size & 0x3 || *pos & 0x3) 751 return -EINVAL; 752 753 if (!adev->reg.didt.rreg) 754 return -EOPNOTSUPP; 755 756 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 757 if (r < 0) { 758 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 759 return r; 760 } 761 762 r = amdgpu_virt_enable_access_debugfs(adev); 763 if (r < 0) { 764 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 765 return r; 766 } 767 768 while (size) { 769 uint32_t value; 770 771 value = RREG32_DIDT(*pos >> 2); 772 r = put_user(value, (uint32_t *)buf); 773 if (r) 774 goto out; 775 776 result += 4; 777 buf += 4; 778 *pos += 4; 779 size -= 4; 780 } 781 782 r = result; 783 out: 784 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 785 amdgpu_virt_disable_access_debugfs(adev); 786 return r; 787 } 788 789 /** 790 * amdgpu_debugfs_regs_didt_write - Write to a DIDT register 791 * 792 * @f: open file handle 793 * @buf: User buffer to write data from 794 * @size: Number of bytes to write 795 * @pos: Offset to seek to 796 * 797 * The lower bits are the BYTE offset of the register to write. This 798 * allows writing multiple registers in a single call and having 799 * the returned size reflect that. 800 */ 801 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf, 802 size_t size, loff_t *pos) 803 { 804 struct amdgpu_device *adev = file_inode(f)->i_private; 805 ssize_t result = 0; 806 int r; 807 808 if (size & 0x3 || *pos & 0x3) 809 return -EINVAL; 810 811 if (!adev->reg.didt.wreg) 812 return -EOPNOTSUPP; 813 814 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 815 if (r < 0) { 816 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 817 return r; 818 } 819 820 r = amdgpu_virt_enable_access_debugfs(adev); 821 if (r < 0) { 822 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 823 return r; 824 } 825 826 while (size) { 827 uint32_t value; 828 829 r = get_user(value, (uint32_t *)buf); 830 if (r) 831 goto out; 832 833 WREG32_DIDT(*pos >> 2, value); 834 835 result += 4; 836 buf += 4; 837 *pos += 4; 838 size -= 4; 839 } 840 841 r = result; 842 out: 843 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 844 amdgpu_virt_disable_access_debugfs(adev); 845 return r; 846 } 847 848 /** 849 * amdgpu_debugfs_regs_smc_read - Read from a SMC register 850 * 851 * @f: open file handle 852 * @buf: User buffer to store read data in 853 * @size: Number of bytes to read 854 * @pos: Offset to seek to 855 * 856 * The lower bits are the BYTE offset of the register to read. This 857 * allows reading multiple registers in a single call and having 858 * the returned size reflect that. 859 */ 860 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf, 861 size_t size, loff_t *pos) 862 { 863 struct amdgpu_device *adev = file_inode(f)->i_private; 864 ssize_t result = 0; 865 int r; 866 867 if (!adev->reg.smc.rreg) 868 return -EOPNOTSUPP; 869 870 if (size & 0x3 || *pos & 0x3) 871 return -EINVAL; 872 873 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 874 if (r < 0) { 875 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 876 return r; 877 } 878 879 r = amdgpu_virt_enable_access_debugfs(adev); 880 if (r < 0) { 881 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 882 return r; 883 } 884 885 while (size) { 886 uint32_t value; 887 888 value = RREG32_SMC(*pos); 889 r = put_user(value, (uint32_t *)buf); 890 if (r) 891 goto out; 892 893 result += 4; 894 buf += 4; 895 *pos += 4; 896 size -= 4; 897 } 898 899 r = result; 900 out: 901 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 902 amdgpu_virt_disable_access_debugfs(adev); 903 return r; 904 } 905 906 /** 907 * amdgpu_debugfs_regs_smc_write - Write to a SMC register 908 * 909 * @f: open file handle 910 * @buf: User buffer to write data from 911 * @size: Number of bytes to write 912 * @pos: Offset to seek to 913 * 914 * The lower bits are the BYTE offset of the register to write. This 915 * allows writing multiple registers in a single call and having 916 * the returned size reflect that. 917 */ 918 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf, 919 size_t size, loff_t *pos) 920 { 921 struct amdgpu_device *adev = file_inode(f)->i_private; 922 ssize_t result = 0; 923 int r; 924 925 if (!adev->reg.smc.wreg) 926 return -EOPNOTSUPP; 927 928 if (size & 0x3 || *pos & 0x3) 929 return -EINVAL; 930 931 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 932 if (r < 0) { 933 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 934 return r; 935 } 936 937 r = amdgpu_virt_enable_access_debugfs(adev); 938 if (r < 0) { 939 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 940 return r; 941 } 942 943 while (size) { 944 uint32_t value; 945 946 r = get_user(value, (uint32_t *)buf); 947 if (r) 948 goto out; 949 950 WREG32_SMC(*pos, value); 951 952 result += 4; 953 buf += 4; 954 *pos += 4; 955 size -= 4; 956 } 957 958 r = result; 959 out: 960 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 961 amdgpu_virt_disable_access_debugfs(adev); 962 return r; 963 } 964 965 /** 966 * amdgpu_debugfs_gca_config_read - Read from gfx config data 967 * 968 * @f: open file handle 969 * @buf: User buffer to store read data in 970 * @size: Number of bytes to read 971 * @pos: Offset to seek to 972 * 973 * This file is used to access configuration data in a somewhat 974 * stable fashion. The format is a series of DWORDs with the first 975 * indicating which revision it is. New content is appended to the 976 * end so that older software can still read the data. 977 */ 978 979 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf, 980 size_t size, loff_t *pos) 981 { 982 struct amdgpu_device *adev = file_inode(f)->i_private; 983 ssize_t result = 0; 984 int r; 985 uint32_t *config, no_regs = 0; 986 987 if (size & 0x3 || *pos & 0x3) 988 return -EINVAL; 989 990 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL); 991 if (!config) 992 return -ENOMEM; 993 994 /* version, increment each time something is added */ 995 config[no_regs++] = 5; 996 config[no_regs++] = adev->gfx.config.max_shader_engines; 997 config[no_regs++] = adev->gfx.config.max_tile_pipes; 998 config[no_regs++] = adev->gfx.config.max_cu_per_sh; 999 config[no_regs++] = adev->gfx.config.max_sh_per_se; 1000 config[no_regs++] = adev->gfx.config.max_backends_per_se; 1001 config[no_regs++] = adev->gfx.config.max_texture_channel_caches; 1002 config[no_regs++] = adev->gfx.config.max_gprs; 1003 config[no_regs++] = adev->gfx.config.max_gs_threads; 1004 config[no_regs++] = adev->gfx.config.max_hw_contexts; 1005 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend; 1006 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend; 1007 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size; 1008 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size; 1009 config[no_regs++] = adev->gfx.config.num_tile_pipes; 1010 config[no_regs++] = adev->gfx.config.backend_enable_mask; 1011 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes; 1012 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb; 1013 config[no_regs++] = adev->gfx.config.shader_engine_tile_size; 1014 config[no_regs++] = adev->gfx.config.num_gpus; 1015 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size; 1016 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg; 1017 config[no_regs++] = adev->gfx.config.gb_addr_config; 1018 config[no_regs++] = adev->gfx.config.num_rbs; 1019 1020 /* rev==1 */ 1021 config[no_regs++] = adev->rev_id; 1022 config[no_regs++] = adev->pg_flags; 1023 config[no_regs++] = lower_32_bits(adev->cg_flags); 1024 1025 /* rev==2 */ 1026 config[no_regs++] = adev->family; 1027 config[no_regs++] = adev->external_rev_id; 1028 1029 /* rev==3 */ 1030 config[no_regs++] = adev->pdev->device; 1031 config[no_regs++] = adev->pdev->revision; 1032 config[no_regs++] = adev->pdev->subsystem_device; 1033 config[no_regs++] = adev->pdev->subsystem_vendor; 1034 1035 /* rev==4 APU flag */ 1036 config[no_regs++] = adev->flags & AMD_IS_APU ? 1 : 0; 1037 1038 /* rev==5 PG/CG flag upper 32bit */ 1039 config[no_regs++] = 0; 1040 config[no_regs++] = upper_32_bits(adev->cg_flags); 1041 1042 while (size && (*pos < no_regs * 4)) { 1043 uint32_t value; 1044 1045 value = config[*pos >> 2]; 1046 r = put_user(value, (uint32_t *)buf); 1047 if (r) { 1048 kfree(config); 1049 return r; 1050 } 1051 1052 result += 4; 1053 buf += 4; 1054 *pos += 4; 1055 size -= 4; 1056 } 1057 1058 kfree(config); 1059 return result; 1060 } 1061 1062 /** 1063 * amdgpu_debugfs_sensor_read - Read from the powerplay sensors 1064 * 1065 * @f: open file handle 1066 * @buf: User buffer to store read data in 1067 * @size: Number of bytes to read 1068 * @pos: Offset to seek to 1069 * 1070 * The offset is treated as the BYTE address of one of the sensors 1071 * enumerated in amd/include/kgd_pp_interface.h under the 1072 * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK 1073 * you would use the offset 3 * 4 = 12. 1074 */ 1075 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf, 1076 size_t size, loff_t *pos) 1077 { 1078 struct amdgpu_device *adev = file_inode(f)->i_private; 1079 int idx, x, outsize, r, valuesize; 1080 uint32_t values[16]; 1081 1082 if (size & 3 || *pos & 0x3) 1083 return -EINVAL; 1084 1085 if (!adev->pm.dpm_enabled) 1086 return -EINVAL; 1087 1088 /* convert offset to sensor number */ 1089 idx = *pos >> 2; 1090 1091 valuesize = sizeof(values); 1092 1093 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1094 if (r < 0) { 1095 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1096 return r; 1097 } 1098 1099 r = amdgpu_virt_enable_access_debugfs(adev); 1100 if (r < 0) { 1101 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1102 return r; 1103 } 1104 1105 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize); 1106 1107 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1108 1109 if (r) { 1110 amdgpu_virt_disable_access_debugfs(adev); 1111 return r; 1112 } 1113 1114 if (idx == AMDGPU_PP_SENSOR_GPU_AVG_POWER || 1115 idx == AMDGPU_PP_SENSOR_GPU_INPUT_POWER) 1116 values[0] = AMDGPU_DEBUGFS_PWR_MW_TO_Q24_8(values[0]); 1117 1118 if (size > valuesize) { 1119 amdgpu_virt_disable_access_debugfs(adev); 1120 return -EINVAL; 1121 } 1122 1123 outsize = 0; 1124 x = 0; 1125 if (!r) { 1126 while (size) { 1127 r = put_user(values[x++], (int32_t *)buf); 1128 buf += 4; 1129 size -= 4; 1130 outsize += 4; 1131 } 1132 } 1133 1134 amdgpu_virt_disable_access_debugfs(adev); 1135 return !r ? outsize : r; 1136 } 1137 1138 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data 1139 * 1140 * @f: open file handle 1141 * @buf: User buffer to store read data in 1142 * @size: Number of bytes to read 1143 * @pos: Offset to seek to 1144 * 1145 * The offset being sought changes which wave that the status data 1146 * will be returned for. The bits are used as follows: 1147 * 1148 * Bits 0..6: Byte offset into data 1149 * Bits 7..14: SE selector 1150 * Bits 15..22: SH/SA selector 1151 * Bits 23..30: CU/{WGP+SIMD} selector 1152 * Bits 31..36: WAVE ID selector 1153 * Bits 37..44: SIMD ID selector 1154 * 1155 * The returned data begins with one DWORD of version information 1156 * Followed by WAVE STATUS registers relevant to the GFX IP version 1157 * being used. See gfx_v8_0_read_wave_data() for an example output. 1158 */ 1159 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf, 1160 size_t size, loff_t *pos) 1161 { 1162 struct amdgpu_device *adev = f->f_inode->i_private; 1163 int r, x; 1164 ssize_t result = 0; 1165 uint32_t offset, se, sh, cu, wave, simd, data[32]; 1166 1167 if (size & 3 || *pos & 3) 1168 return -EINVAL; 1169 1170 /* decode offset */ 1171 offset = (*pos & GENMASK_ULL(6, 0)); 1172 se = (*pos & GENMASK_ULL(14, 7)) >> 7; 1173 sh = (*pos & GENMASK_ULL(22, 15)) >> 15; 1174 cu = (*pos & GENMASK_ULL(30, 23)) >> 23; 1175 wave = (*pos & GENMASK_ULL(36, 31)) >> 31; 1176 simd = (*pos & GENMASK_ULL(44, 37)) >> 37; 1177 1178 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1179 if (r < 0) { 1180 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1181 return r; 1182 } 1183 1184 r = amdgpu_virt_enable_access_debugfs(adev); 1185 if (r < 0) { 1186 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1187 return r; 1188 } 1189 1190 /* switch to the specific se/sh/cu */ 1191 mutex_lock(&adev->grbm_idx_mutex); 1192 amdgpu_gfx_select_se_sh(adev, se, sh, cu, 0); 1193 1194 x = 0; 1195 if (adev->gfx.funcs->read_wave_data) 1196 adev->gfx.funcs->read_wave_data(adev, 0, simd, wave, data, &x); 1197 1198 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0); 1199 mutex_unlock(&adev->grbm_idx_mutex); 1200 1201 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1202 1203 if (!x) { 1204 amdgpu_virt_disable_access_debugfs(adev); 1205 return -EINVAL; 1206 } 1207 1208 while (size && (offset < x * 4)) { 1209 uint32_t value; 1210 1211 value = data[offset >> 2]; 1212 r = put_user(value, (uint32_t *)buf); 1213 if (r) { 1214 amdgpu_virt_disable_access_debugfs(adev); 1215 return r; 1216 } 1217 1218 result += 4; 1219 buf += 4; 1220 offset += 4; 1221 size -= 4; 1222 } 1223 1224 amdgpu_virt_disable_access_debugfs(adev); 1225 return result; 1226 } 1227 1228 /** amdgpu_debugfs_gpr_read - Read wave gprs 1229 * 1230 * @f: open file handle 1231 * @buf: User buffer to store read data in 1232 * @size: Number of bytes to read 1233 * @pos: Offset to seek to 1234 * 1235 * The offset being sought changes which wave that the status data 1236 * will be returned for. The bits are used as follows: 1237 * 1238 * Bits 0..11: Byte offset into data 1239 * Bits 12..19: SE selector 1240 * Bits 20..27: SH/SA selector 1241 * Bits 28..35: CU/{WGP+SIMD} selector 1242 * Bits 36..43: WAVE ID selector 1243 * Bits 37..44: SIMD ID selector 1244 * Bits 52..59: Thread selector 1245 * Bits 60..61: Bank selector (VGPR=0,SGPR=1) 1246 * 1247 * The return data comes from the SGPR or VGPR register bank for 1248 * the selected operational unit. 1249 */ 1250 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf, 1251 size_t size, loff_t *pos) 1252 { 1253 struct amdgpu_device *adev = f->f_inode->i_private; 1254 int r; 1255 ssize_t result = 0; 1256 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data; 1257 1258 if (size > 4096 || size & 3 || *pos & 3) 1259 return -EINVAL; 1260 1261 /* decode offset */ 1262 offset = (*pos & GENMASK_ULL(11, 0)) >> 2; 1263 se = (*pos & GENMASK_ULL(19, 12)) >> 12; 1264 sh = (*pos & GENMASK_ULL(27, 20)) >> 20; 1265 cu = (*pos & GENMASK_ULL(35, 28)) >> 28; 1266 wave = (*pos & GENMASK_ULL(43, 36)) >> 36; 1267 simd = (*pos & GENMASK_ULL(51, 44)) >> 44; 1268 thread = (*pos & GENMASK_ULL(59, 52)) >> 52; 1269 bank = (*pos & GENMASK_ULL(61, 60)) >> 60; 1270 1271 data = kcalloc(1024, sizeof(*data), GFP_KERNEL); 1272 if (!data) 1273 return -ENOMEM; 1274 1275 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1276 if (r < 0) 1277 goto err; 1278 1279 r = amdgpu_virt_enable_access_debugfs(adev); 1280 if (r < 0) 1281 goto err; 1282 1283 /* switch to the specific se/sh/cu */ 1284 mutex_lock(&adev->grbm_idx_mutex); 1285 amdgpu_gfx_select_se_sh(adev, se, sh, cu, 0); 1286 1287 if (bank == 0) { 1288 if (adev->gfx.funcs->read_wave_vgprs) 1289 adev->gfx.funcs->read_wave_vgprs(adev, 0, simd, wave, thread, offset, size>>2, data); 1290 } else { 1291 if (adev->gfx.funcs->read_wave_sgprs) 1292 adev->gfx.funcs->read_wave_sgprs(adev, 0, simd, wave, offset, size>>2, data); 1293 } 1294 1295 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0); 1296 mutex_unlock(&adev->grbm_idx_mutex); 1297 1298 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1299 1300 while (size) { 1301 uint32_t value; 1302 1303 value = data[result >> 2]; 1304 r = put_user(value, (uint32_t *)buf); 1305 if (r) { 1306 amdgpu_virt_disable_access_debugfs(adev); 1307 goto err; 1308 } 1309 1310 result += 4; 1311 buf += 4; 1312 size -= 4; 1313 } 1314 1315 kfree(data); 1316 amdgpu_virt_disable_access_debugfs(adev); 1317 return result; 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 return r; 1786 1787 /* hold on the scheduler */ 1788 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 1789 struct amdgpu_ring *ring = adev->rings[i]; 1790 1791 if (!amdgpu_ring_sched_ready(ring)) 1792 continue; 1793 drm_sched_wqueue_stop(&ring->sched); 1794 } 1795 1796 seq_puts(m, "run ib test:\n"); 1797 r = amdgpu_ib_ring_tests(adev); 1798 if (r) 1799 seq_printf(m, "ib ring tests failed (%d).\n", r); 1800 else 1801 seq_puts(m, "ib ring tests passed.\n"); 1802 1803 /* go on the scheduler */ 1804 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 1805 struct amdgpu_ring *ring = adev->rings[i]; 1806 1807 if (!amdgpu_ring_sched_ready(ring)) 1808 continue; 1809 drm_sched_wqueue_start(&ring->sched); 1810 } 1811 1812 up_write(&adev->reset_domain->sem); 1813 1814 pm_runtime_put_autosuspend(dev->dev); 1815 1816 return 0; 1817 } 1818 1819 static int amdgpu_debugfs_evict_vram(void *data, u64 *val) 1820 { 1821 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1822 struct drm_device *dev = adev_to_drm(adev); 1823 int r; 1824 1825 r = pm_runtime_get_sync(dev->dev); 1826 if (r < 0) { 1827 pm_runtime_put_autosuspend(dev->dev); 1828 return r; 1829 } 1830 1831 *val = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM); 1832 1833 pm_runtime_put_autosuspend(dev->dev); 1834 1835 return 0; 1836 } 1837 1838 1839 static int amdgpu_debugfs_evict_gtt(void *data, u64 *val) 1840 { 1841 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1842 struct drm_device *dev = adev_to_drm(adev); 1843 int r; 1844 1845 r = pm_runtime_get_sync(dev->dev); 1846 if (r < 0) { 1847 pm_runtime_put_autosuspend(dev->dev); 1848 return r; 1849 } 1850 1851 *val = amdgpu_ttm_evict_resources(adev, TTM_PL_TT); 1852 1853 pm_runtime_put_autosuspend(dev->dev); 1854 1855 return 0; 1856 } 1857 1858 static int amdgpu_debugfs_benchmark(void *data, u64 val) 1859 { 1860 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1861 struct drm_device *dev = adev_to_drm(adev); 1862 int r; 1863 1864 r = pm_runtime_get_sync(dev->dev); 1865 if (r < 0) { 1866 pm_runtime_put_autosuspend(dev->dev); 1867 return r; 1868 } 1869 1870 r = amdgpu_benchmark(adev, val); 1871 1872 pm_runtime_put_autosuspend(dev->dev); 1873 1874 return r; 1875 } 1876 1877 static int amdgpu_debugfs_vm_info_show(struct seq_file *m, void *unused) 1878 { 1879 struct amdgpu_device *adev = m->private; 1880 struct drm_device *dev = adev_to_drm(adev); 1881 struct drm_file *file; 1882 int r; 1883 1884 r = mutex_lock_interruptible(&dev->filelist_mutex); 1885 if (r) 1886 return r; 1887 1888 list_for_each_entry(file, &dev->filelist, lhead) { 1889 struct amdgpu_fpriv *fpriv = file->driver_priv; 1890 struct amdgpu_vm *vm = &fpriv->vm; 1891 struct amdgpu_task_info *ti; 1892 1893 ti = amdgpu_vm_get_task_info_vm(vm); 1894 if (ti) { 1895 seq_printf(m, "pid:%d\tProcess:%s ----------\n", ti->task.pid, ti->process_name); 1896 amdgpu_vm_put_task_info(ti); 1897 } 1898 1899 r = amdgpu_bo_reserve(vm->root.bo, true); 1900 if (r) 1901 break; 1902 amdgpu_debugfs_vm_bo_info(vm, m); 1903 amdgpu_bo_unreserve(vm->root.bo); 1904 } 1905 1906 mutex_unlock(&dev->filelist_mutex); 1907 1908 return r; 1909 } 1910 1911 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_test_ib); 1912 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_vm_info); 1913 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_vram_fops, amdgpu_debugfs_evict_vram, 1914 NULL, "%lld\n"); 1915 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_gtt_fops, amdgpu_debugfs_evict_gtt, 1916 NULL, "%lld\n"); 1917 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_benchmark_fops, NULL, amdgpu_debugfs_benchmark, 1918 "%lld\n"); 1919 1920 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring, 1921 struct dma_fence **fences) 1922 { 1923 struct amdgpu_fence_driver *drv = &ring->fence_drv; 1924 uint32_t sync_seq, last_seq; 1925 1926 last_seq = atomic_read(&ring->fence_drv.last_seq); 1927 sync_seq = ring->fence_drv.sync_seq; 1928 1929 last_seq &= drv->num_fences_mask; 1930 sync_seq &= drv->num_fences_mask; 1931 1932 do { 1933 struct dma_fence *fence, **ptr; 1934 1935 ++last_seq; 1936 last_seq &= drv->num_fences_mask; 1937 ptr = &drv->fences[last_seq]; 1938 1939 fence = rcu_dereference_protected(*ptr, 1); 1940 RCU_INIT_POINTER(*ptr, NULL); 1941 1942 if (!fence) 1943 continue; 1944 1945 fences[last_seq] = fence; 1946 1947 } while (last_seq != sync_seq); 1948 } 1949 1950 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences, 1951 int length) 1952 { 1953 int i; 1954 struct dma_fence *fence; 1955 1956 for (i = 0; i < length; i++) { 1957 fence = fences[i]; 1958 if (!fence) 1959 continue; 1960 dma_fence_signal(fence); 1961 dma_fence_put(fence); 1962 } 1963 } 1964 1965 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched) 1966 { 1967 struct drm_sched_job *s_job; 1968 struct dma_fence *fence; 1969 1970 spin_lock(&sched->job_list_lock); 1971 list_for_each_entry(s_job, &sched->pending_list, list) { 1972 fence = sched->ops->run_job(s_job); 1973 dma_fence_put(fence); 1974 } 1975 spin_unlock(&sched->job_list_lock); 1976 } 1977 1978 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring) 1979 { 1980 struct amdgpu_job *job; 1981 struct drm_sched_job *s_job, *tmp; 1982 uint32_t preempt_seq; 1983 struct dma_fence *fence, **ptr; 1984 struct amdgpu_fence_driver *drv = &ring->fence_drv; 1985 struct drm_gpu_scheduler *sched = &ring->sched; 1986 bool preempted = true; 1987 1988 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX) 1989 return; 1990 1991 preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2)); 1992 if (preempt_seq <= atomic_read(&drv->last_seq)) { 1993 preempted = false; 1994 goto no_preempt; 1995 } 1996 1997 preempt_seq &= drv->num_fences_mask; 1998 ptr = &drv->fences[preempt_seq]; 1999 fence = rcu_dereference_protected(*ptr, 1); 2000 2001 no_preempt: 2002 spin_lock(&sched->job_list_lock); 2003 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) { 2004 if (dma_fence_is_signaled(&s_job->s_fence->finished)) { 2005 /* remove job from ring_mirror_list */ 2006 list_del_init(&s_job->list); 2007 sched->ops->free_job(s_job); 2008 continue; 2009 } 2010 job = to_amdgpu_job(s_job); 2011 if (preempted && (&job->hw_fence->base) == fence) 2012 /* mark the job as preempted */ 2013 job->preemption_status |= AMDGPU_IB_PREEMPTED; 2014 } 2015 spin_unlock(&sched->job_list_lock); 2016 } 2017 2018 static int amdgpu_debugfs_ib_preempt(void *data, u64 val) 2019 { 2020 int r, length; 2021 struct amdgpu_ring *ring; 2022 struct dma_fence **fences = NULL; 2023 struct amdgpu_device *adev = (struct amdgpu_device *)data; 2024 2025 if (val >= AMDGPU_MAX_RINGS) 2026 return -EINVAL; 2027 2028 ring = adev->rings[val]; 2029 2030 if (!amdgpu_ring_sched_ready(ring) || 2031 !ring->funcs->preempt_ib) 2032 return -EINVAL; 2033 2034 /* the last preemption failed */ 2035 if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr)) 2036 return -EBUSY; 2037 2038 length = ring->fence_drv.num_fences_mask + 1; 2039 fences = kcalloc(length, sizeof(void *), GFP_KERNEL); 2040 if (!fences) 2041 return -ENOMEM; 2042 2043 /* Avoid accidently unparking the sched thread during GPU reset */ 2044 r = down_read_killable(&adev->reset_domain->sem); 2045 if (r) 2046 goto pro_end; 2047 2048 /* stop the scheduler */ 2049 drm_sched_wqueue_stop(&ring->sched); 2050 2051 /* preempt the IB */ 2052 r = amdgpu_ring_preempt_ib(ring); 2053 if (r) { 2054 drm_warn(adev_to_drm(adev), "failed to preempt ring %d\n", ring->idx); 2055 goto failure; 2056 } 2057 2058 amdgpu_fence_process(ring); 2059 2060 if (atomic_read(&ring->fence_drv.last_seq) != 2061 ring->fence_drv.sync_seq) { 2062 drm_info(adev_to_drm(adev), "ring %d was preempted\n", ring->idx); 2063 2064 amdgpu_ib_preempt_mark_partial_job(ring); 2065 2066 /* swap out the old fences */ 2067 amdgpu_ib_preempt_fences_swap(ring, fences); 2068 2069 amdgpu_fence_driver_force_completion(ring, NULL); 2070 2071 /* resubmit unfinished jobs */ 2072 amdgpu_ib_preempt_job_recovery(&ring->sched); 2073 2074 /* wait for jobs finished */ 2075 amdgpu_fence_wait_empty(ring); 2076 2077 /* signal the old fences */ 2078 amdgpu_ib_preempt_signal_fences(fences, length); 2079 } 2080 2081 failure: 2082 /* restart the scheduler */ 2083 drm_sched_wqueue_start(&ring->sched); 2084 2085 up_read(&adev->reset_domain->sem); 2086 2087 pro_end: 2088 kfree(fences); 2089 2090 return r; 2091 } 2092 2093 static int amdgpu_debugfs_sclk_set(void *data, u64 val) 2094 { 2095 int ret = 0; 2096 uint32_t max_freq, min_freq; 2097 struct amdgpu_device *adev = (struct amdgpu_device *)data; 2098 2099 if (amdgpu_sriov_multi_vf_mode(adev)) 2100 return -EINVAL; 2101 2102 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2103 if (ret < 0) { 2104 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2105 return ret; 2106 } 2107 2108 ret = amdgpu_dpm_get_dpm_freq_range(adev, PP_SCLK, &min_freq, &max_freq); 2109 if (ret == -EOPNOTSUPP) { 2110 ret = 0; 2111 goto out; 2112 } 2113 if (ret || val > max_freq || val < min_freq) { 2114 ret = -EINVAL; 2115 goto out; 2116 } 2117 2118 ret = amdgpu_dpm_set_soft_freq_range(adev, PP_SCLK, (uint32_t)val, (uint32_t)val); 2119 if (ret) 2120 ret = -EINVAL; 2121 2122 out: 2123 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2124 2125 return ret; 2126 } 2127 2128 DEFINE_DEBUGFS_ATTRIBUTE(fops_ib_preempt, NULL, 2129 amdgpu_debugfs_ib_preempt, "%llu\n"); 2130 2131 DEFINE_DEBUGFS_ATTRIBUTE(fops_sclk_set, NULL, 2132 amdgpu_debugfs_sclk_set, "%llu\n"); 2133 2134 int amdgpu_debugfs_init(struct amdgpu_device *adev) 2135 { 2136 struct dentry *root = adev_to_drm(adev)->primary->debugfs_root; 2137 struct dentry *ent; 2138 int r, i; 2139 2140 if (!debugfs_initialized()) 2141 return 0; 2142 2143 debugfs_create_x32("amdgpu_smu_debug", 0600, root, 2144 &adev->pm.smu_debug_mask); 2145 2146 debugfs_create_x64("unique_id", 0444, root, &adev->unique_id); 2147 debugfs_create_x8("unitid", 0444, root, &adev->unitid); 2148 2149 ent = debugfs_create_file("amdgpu_preempt_ib", 0600, root, adev, 2150 &fops_ib_preempt); 2151 if (IS_ERR(ent)) { 2152 drm_err(adev_to_drm(adev), 2153 "unable to create amdgpu_preempt_ib debugsfs file\n"); 2154 return PTR_ERR(ent); 2155 } 2156 2157 ent = debugfs_create_file("amdgpu_force_sclk", 0200, root, adev, 2158 &fops_sclk_set); 2159 if (IS_ERR(ent)) { 2160 drm_err(adev_to_drm(adev), 2161 "unable to create amdgpu_set_sclk debugsfs file\n"); 2162 return PTR_ERR(ent); 2163 } 2164 2165 /* Register debugfs entries for amdgpu_ttm */ 2166 amdgpu_ttm_debugfs_init(adev); 2167 amdgpu_debugfs_pm_init(adev); 2168 amdgpu_debugfs_sa_init(adev); 2169 amdgpu_debugfs_fence_init(adev); 2170 amdgpu_debugfs_gem_init(adev); 2171 2172 r = amdgpu_debugfs_regs_init(adev); 2173 if (r) 2174 drm_err(adev_to_drm(adev), "registering register debugfs failed (%d).\n", r); 2175 2176 amdgpu_debugfs_firmware_init(adev); 2177 amdgpu_ta_if_debugfs_init(adev); 2178 2179 amdgpu_debugfs_mes_event_log_init(adev); 2180 2181 #if defined(CONFIG_DRM_AMD_DC) 2182 if (adev->dc_enabled) 2183 dtn_debugfs_init(adev); 2184 #endif 2185 2186 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 2187 struct amdgpu_ring *ring = adev->rings[i]; 2188 2189 if (!ring) 2190 continue; 2191 if (ring == &adev->cper.ring_buf && !adev->cper.enabled) 2192 continue; 2193 2194 amdgpu_debugfs_ring_init(adev, ring); 2195 } 2196 2197 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 2198 if (!amdgpu_vcnfw_log) 2199 break; 2200 2201 if (adev->vcn.harvest_config & (1 << i)) 2202 continue; 2203 2204 amdgpu_debugfs_vcn_fwlog_init(adev, i, &adev->vcn.inst[i]); 2205 } 2206 2207 if (amdgpu_umsch_mm & amdgpu_umsch_mm_fwlog) 2208 amdgpu_debugfs_umsch_fwlog_init(adev, &adev->umsch_mm); 2209 2210 amdgpu_debugfs_vcn_sched_mask_init(adev); 2211 amdgpu_debugfs_jpeg_sched_mask_init(adev); 2212 amdgpu_debugfs_gfx_sched_mask_init(adev); 2213 amdgpu_debugfs_compute_sched_mask_init(adev); 2214 amdgpu_debugfs_sdma_sched_mask_init(adev); 2215 2216 amdgpu_ras_debugfs_create_all(adev); 2217 amdgpu_rap_debugfs_init(adev); 2218 amdgpu_securedisplay_debugfs_init(adev); 2219 amdgpu_fw_attestation_debugfs_init(adev); 2220 amdgpu_psp_debugfs_init(adev); 2221 2222 debugfs_create_file("amdgpu_evict_vram", 0400, root, adev, 2223 &amdgpu_evict_vram_fops); 2224 debugfs_create_file("amdgpu_evict_gtt", 0400, root, adev, 2225 &amdgpu_evict_gtt_fops); 2226 debugfs_create_file("amdgpu_test_ib", 0400, root, adev, 2227 &amdgpu_debugfs_test_ib_fops); 2228 debugfs_create_file("amdgpu_vm_info", 0444, root, adev, 2229 &amdgpu_debugfs_vm_info_fops); 2230 debugfs_create_file("amdgpu_benchmark", 0200, root, adev, 2231 &amdgpu_benchmark_fops); 2232 2233 /* Debug-only: bitmap of incoming UALink protocol messages to drop. 2234 * Each bit position corresponds to an enum AMDGPU_UALINK_PROTOCOL_MESSAGES 2235 * value. Setting a bit causes exactly one matching incoming packet to be 2236 * dropped, after which the bit auto-clears and traffic resumes. Used to 2237 * exercise the connection reset paths. 2238 * 2239 * Examples (drop one NPA-REQ): 2240 * echo 0x8 > /sys/kernel/debug/dri/0/amdgpu_ualink_drop_msg_bitmap 2241 * (drop one NPA-RSP): 2242 * echo 0x10 > /sys/kernel/debug/dri/0/amdgpu_ualink_drop_msg_bitmap 2243 */ 2244 debugfs_create_ulong("amdgpu_ualink_drop_msg_bitmap", 0600, root, 2245 &adev->ualink.drop_msg_bitmap); 2246 2247 adev->debugfs_vbios_blob.data = adev->bios; 2248 adev->debugfs_vbios_blob.size = adev->bios_size; 2249 debugfs_create_blob("amdgpu_vbios", 0444, root, 2250 &adev->debugfs_vbios_blob); 2251 2252 if (adev->discovery.debugfs_blob.size) 2253 debugfs_create_blob("amdgpu_discovery", 0444, root, 2254 &adev->discovery.debugfs_blob); 2255 2256 return 0; 2257 } 2258 2259 static int amdgpu_pt_info_read(struct seq_file *m, void *unused) 2260 { 2261 struct drm_file *file; 2262 struct amdgpu_fpriv *fpriv; 2263 struct amdgpu_bo *root_bo; 2264 struct amdgpu_device *adev; 2265 int r; 2266 2267 file = m->private; 2268 if (!file) 2269 return -EINVAL; 2270 2271 adev = drm_to_adev(file->minor->dev); 2272 fpriv = file->driver_priv; 2273 if (!fpriv || !fpriv->vm.root.bo) 2274 return -ENODEV; 2275 2276 root_bo = amdgpu_bo_ref(fpriv->vm.root.bo); 2277 r = amdgpu_bo_reserve(root_bo, true); 2278 if (r) { 2279 amdgpu_bo_unref(&root_bo); 2280 return -EINVAL; 2281 } 2282 2283 seq_printf(m, "pd_address: 0x%llx\n", amdgpu_gmc_pd_addr(fpriv->vm.root.bo)); 2284 seq_printf(m, "max_pfn: 0x%llx\n", adev->vm_manager.max_pfn); 2285 seq_printf(m, "num_level: 0x%x\n", adev->vm_manager.num_level); 2286 seq_printf(m, "block_size: 0x%x\n", adev->vm_manager.block_size); 2287 seq_printf(m, "fragment_size: 0x%x\n", adev->vm_manager.fragment_size); 2288 2289 amdgpu_bo_unreserve(root_bo); 2290 amdgpu_bo_unref(&root_bo); 2291 2292 return 0; 2293 } 2294 2295 static int amdgpu_pt_info_open(struct inode *inode, struct file *file) 2296 { 2297 return single_open(file, amdgpu_pt_info_read, inode->i_private); 2298 } 2299 2300 static const struct file_operations amdgpu_pt_info_fops = { 2301 .owner = THIS_MODULE, 2302 .open = amdgpu_pt_info_open, 2303 .read = seq_read, 2304 .llseek = seq_lseek, 2305 .release = single_release, 2306 }; 2307 2308 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused) 2309 { 2310 struct amdgpu_usermode_queue *queue = m->private; 2311 struct amdgpu_bo *bo; 2312 int r; 2313 2314 if (!queue || !queue->mqd.obj) 2315 return -EINVAL; 2316 2317 bo = amdgpu_bo_ref(queue->mqd.obj); 2318 r = amdgpu_bo_reserve(bo, true); 2319 if (r) { 2320 amdgpu_bo_unref(&bo); 2321 return -EINVAL; 2322 } 2323 2324 seq_printf(m, "queue_type: %d\n", queue->queue_type); 2325 seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj)); 2326 2327 amdgpu_bo_unreserve(bo); 2328 amdgpu_bo_unref(&bo); 2329 2330 return 0; 2331 } 2332 2333 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file) 2334 { 2335 return single_open(file, amdgpu_mqd_info_read, inode->i_private); 2336 } 2337 2338 static const struct file_operations amdgpu_mqd_info_fops = { 2339 .owner = THIS_MODULE, 2340 .open = amdgpu_mqd_info_open, 2341 .read = seq_read, 2342 .llseek = seq_lseek, 2343 .release = single_release, 2344 }; 2345 2346 void amdgpu_debugfs_userq_init(struct drm_file *file, struct amdgpu_usermode_queue *queue, int qid) 2347 { 2348 char queue_name[32]; 2349 2350 scnprintf(queue_name, sizeof(queue_name), "queue_%d", qid); 2351 queue->debugfs_queue = debugfs_create_dir(queue_name, file->debugfs_client); 2352 debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); 2353 } 2354 2355 void amdgpu_debugfs_vm_init(struct drm_file *file) 2356 { 2357 debugfs_create_file("vm_pagetable_info", 0444, file->debugfs_client, file, 2358 &amdgpu_pt_info_fops); 2359 } 2360 2361 #else 2362 int amdgpu_debugfs_init(struct amdgpu_device *adev) 2363 { 2364 return 0; 2365 } 2366 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) 2367 { 2368 return 0; 2369 } 2370 void amdgpu_debugfs_vm_init(struct drm_file *file) 2371 { 2372 } 2373 void amdgpu_debugfs_userq_init(struct drm_file *file, 2374 struct amdgpu_usermode_queue *queue, 2375 int qid) 2376 { 2377 } 2378 #endif 2379