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/pm_runtime.h> 30 31 #include "amdgpu.h" 32 #include "amdgpu_pm.h" 33 #include "amdgpu_dm_debugfs.h" 34 #include "amdgpu_ras.h" 35 #include "amdgpu_rap.h" 36 #include "amdgpu_securedisplay.h" 37 #include "amdgpu_fw_attestation.h" 38 #include "amdgpu_umr.h" 39 40 #include "amdgpu_reset.h" 41 42 #if defined(CONFIG_DEBUG_FS) 43 44 /** 45 * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes 46 * 47 * @read: True if reading 48 * @f: open file handle 49 * @buf: User buffer to write/read to 50 * @size: Number of bytes to write/read 51 * @pos: Offset to seek to 52 * 53 * This debugfs entry has special meaning on the offset being sought. 54 * Various bits have different meanings: 55 * 56 * Bit 62: Indicates a GRBM bank switch is needed 57 * Bit 61: Indicates a SRBM bank switch is needed (implies bit 62 is 58 * zero) 59 * Bits 24..33: The SE or ME selector if needed 60 * Bits 34..43: The SH (or SA) or PIPE selector if needed 61 * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed 62 * 63 * Bit 23: Indicates that the PM power gating lock should be held 64 * This is necessary to read registers that might be 65 * unreliable during a power gating transistion. 66 * 67 * The lower bits are the BYTE offset of the register to read. This 68 * allows reading multiple registers in a single call and having 69 * the returned size reflect that. 70 */ 71 static int amdgpu_debugfs_process_reg_op(bool read, struct file *f, 72 char __user *buf, size_t size, loff_t *pos) 73 { 74 struct amdgpu_device *adev = file_inode(f)->i_private; 75 ssize_t result = 0; 76 int r; 77 bool pm_pg_lock, use_bank, use_ring; 78 unsigned instance_bank, sh_bank, se_bank, me, pipe, queue, vmid; 79 80 pm_pg_lock = use_bank = use_ring = false; 81 instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0; 82 83 if (size & 0x3 || *pos & 0x3 || 84 ((*pos & (1ULL << 62)) && (*pos & (1ULL << 61)))) 85 return -EINVAL; 86 87 /* are we reading registers for which a PG lock is necessary? */ 88 pm_pg_lock = (*pos >> 23) & 1; 89 90 if (*pos & (1ULL << 62)) { 91 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24; 92 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34; 93 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44; 94 95 if (se_bank == 0x3FF) 96 se_bank = 0xFFFFFFFF; 97 if (sh_bank == 0x3FF) 98 sh_bank = 0xFFFFFFFF; 99 if (instance_bank == 0x3FF) 100 instance_bank = 0xFFFFFFFF; 101 use_bank = true; 102 } else if (*pos & (1ULL << 61)) { 103 104 me = (*pos & GENMASK_ULL(33, 24)) >> 24; 105 pipe = (*pos & GENMASK_ULL(43, 34)) >> 34; 106 queue = (*pos & GENMASK_ULL(53, 44)) >> 44; 107 vmid = (*pos & GENMASK_ULL(58, 54)) >> 54; 108 109 use_ring = true; 110 } else { 111 use_bank = use_ring = false; 112 } 113 114 *pos &= (1UL << 22) - 1; 115 116 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 117 if (r < 0) { 118 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 119 return r; 120 } 121 122 r = amdgpu_virt_enable_access_debugfs(adev); 123 if (r < 0) { 124 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 125 return r; 126 } 127 128 if (use_bank) { 129 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) || 130 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) { 131 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 132 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 133 amdgpu_virt_disable_access_debugfs(adev); 134 return -EINVAL; 135 } 136 mutex_lock(&adev->grbm_idx_mutex); 137 amdgpu_gfx_select_se_sh(adev, se_bank, 138 sh_bank, instance_bank); 139 } else if (use_ring) { 140 mutex_lock(&adev->srbm_mutex); 141 amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid); 142 } 143 144 if (pm_pg_lock) 145 mutex_lock(&adev->pm.mutex); 146 147 while (size) { 148 uint32_t value; 149 150 if (read) { 151 value = RREG32(*pos >> 2); 152 r = put_user(value, (uint32_t *)buf); 153 } else { 154 r = get_user(value, (uint32_t *)buf); 155 if (!r) 156 amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value); 157 } 158 if (r) { 159 result = r; 160 goto end; 161 } 162 163 result += 4; 164 buf += 4; 165 *pos += 4; 166 size -= 4; 167 } 168 169 end: 170 if (use_bank) { 171 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff); 172 mutex_unlock(&adev->grbm_idx_mutex); 173 } else if (use_ring) { 174 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0); 175 mutex_unlock(&adev->srbm_mutex); 176 } 177 178 if (pm_pg_lock) 179 mutex_unlock(&adev->pm.mutex); 180 181 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 182 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 183 184 amdgpu_virt_disable_access_debugfs(adev); 185 return result; 186 } 187 188 /* 189 * amdgpu_debugfs_regs_read - Callback for reading MMIO registers 190 */ 191 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf, 192 size_t size, loff_t *pos) 193 { 194 return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos); 195 } 196 197 /* 198 * amdgpu_debugfs_regs_write - Callback for writing MMIO registers 199 */ 200 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf, 201 size_t size, loff_t *pos) 202 { 203 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos); 204 } 205 206 static int amdgpu_debugfs_regs2_open(struct inode *inode, struct file *file) 207 { 208 struct amdgpu_debugfs_regs2_data *rd; 209 210 rd = kzalloc(sizeof *rd, GFP_KERNEL); 211 if (!rd) 212 return -ENOMEM; 213 rd->adev = file_inode(file)->i_private; 214 file->private_data = rd; 215 mutex_init(&rd->lock); 216 217 return 0; 218 } 219 220 static int amdgpu_debugfs_regs2_release(struct inode *inode, struct file *file) 221 { 222 struct amdgpu_debugfs_regs2_data *rd = file->private_data; 223 mutex_destroy(&rd->lock); 224 kfree(file->private_data); 225 return 0; 226 } 227 228 static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 offset, size_t size, int write_en) 229 { 230 struct amdgpu_debugfs_regs2_data *rd = f->private_data; 231 struct amdgpu_device *adev = rd->adev; 232 ssize_t result = 0; 233 int r; 234 uint32_t value; 235 236 if (size & 0x3 || offset & 0x3) 237 return -EINVAL; 238 239 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 240 if (r < 0) { 241 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 242 return r; 243 } 244 245 r = amdgpu_virt_enable_access_debugfs(adev); 246 if (r < 0) { 247 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 248 return r; 249 } 250 251 mutex_lock(&rd->lock); 252 253 if (rd->id.use_grbm) { 254 if ((rd->id.grbm.sh != 0xFFFFFFFF && rd->id.grbm.sh >= adev->gfx.config.max_sh_per_se) || 255 (rd->id.grbm.se != 0xFFFFFFFF && rd->id.grbm.se >= adev->gfx.config.max_shader_engines)) { 256 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 257 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 258 amdgpu_virt_disable_access_debugfs(adev); 259 mutex_unlock(&rd->lock); 260 return -EINVAL; 261 } 262 mutex_lock(&adev->grbm_idx_mutex); 263 amdgpu_gfx_select_se_sh(adev, rd->id.grbm.se, 264 rd->id.grbm.sh, 265 rd->id.grbm.instance); 266 } 267 268 if (rd->id.use_srbm) { 269 mutex_lock(&adev->srbm_mutex); 270 amdgpu_gfx_select_me_pipe_q(adev, rd->id.srbm.me, rd->id.srbm.pipe, 271 rd->id.srbm.queue, rd->id.srbm.vmid); 272 } 273 274 if (rd->id.pg_lock) 275 mutex_lock(&adev->pm.mutex); 276 277 while (size) { 278 if (!write_en) { 279 value = RREG32(offset >> 2); 280 r = put_user(value, (uint32_t *)buf); 281 } else { 282 r = get_user(value, (uint32_t *)buf); 283 if (!r) 284 amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value); 285 } 286 if (r) { 287 result = r; 288 goto end; 289 } 290 offset += 4; 291 size -= 4; 292 result += 4; 293 buf += 4; 294 } 295 end: 296 if (rd->id.use_grbm) { 297 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff); 298 mutex_unlock(&adev->grbm_idx_mutex); 299 } 300 301 if (rd->id.use_srbm) { 302 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0); 303 mutex_unlock(&adev->srbm_mutex); 304 } 305 306 if (rd->id.pg_lock) 307 mutex_unlock(&adev->pm.mutex); 308 309 mutex_unlock(&rd->lock); 310 311 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 312 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 313 314 amdgpu_virt_disable_access_debugfs(adev); 315 return result; 316 } 317 318 static long amdgpu_debugfs_regs2_ioctl(struct file *f, unsigned int cmd, unsigned long data) 319 { 320 struct amdgpu_debugfs_regs2_data *rd = f->private_data; 321 int r; 322 323 switch (cmd) { 324 case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE: 325 mutex_lock(&rd->lock); 326 r = copy_from_user(&rd->id, (struct amdgpu_debugfs_regs2_iocdata *)data, sizeof rd->id); 327 mutex_unlock(&rd->lock); 328 return r ? -EINVAL : 0; 329 default: 330 return -EINVAL; 331 } 332 return 0; 333 } 334 335 static ssize_t amdgpu_debugfs_regs2_read(struct file *f, char __user *buf, size_t size, loff_t *pos) 336 { 337 return amdgpu_debugfs_regs2_op(f, buf, *pos, size, 0); 338 } 339 340 static ssize_t amdgpu_debugfs_regs2_write(struct file *f, const char __user *buf, size_t size, loff_t *pos) 341 { 342 return amdgpu_debugfs_regs2_op(f, (char __user *)buf, *pos, size, 1); 343 } 344 345 346 /** 347 * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register 348 * 349 * @f: open file handle 350 * @buf: User buffer to store read data in 351 * @size: Number of bytes to read 352 * @pos: Offset to seek to 353 * 354 * The lower bits are the BYTE offset of the register to read. This 355 * allows reading multiple registers in a single call and having 356 * the returned size reflect that. 357 */ 358 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf, 359 size_t size, loff_t *pos) 360 { 361 struct amdgpu_device *adev = file_inode(f)->i_private; 362 ssize_t result = 0; 363 int r; 364 365 if (size & 0x3 || *pos & 0x3) 366 return -EINVAL; 367 368 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 369 if (r < 0) { 370 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 371 return r; 372 } 373 374 r = amdgpu_virt_enable_access_debugfs(adev); 375 if (r < 0) { 376 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 377 return r; 378 } 379 380 while (size) { 381 uint32_t value; 382 383 value = RREG32_PCIE(*pos); 384 r = put_user(value, (uint32_t *)buf); 385 if (r) { 386 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 387 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 388 amdgpu_virt_disable_access_debugfs(adev); 389 return r; 390 } 391 392 result += 4; 393 buf += 4; 394 *pos += 4; 395 size -= 4; 396 } 397 398 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 399 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 400 401 amdgpu_virt_disable_access_debugfs(adev); 402 return result; 403 } 404 405 /** 406 * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register 407 * 408 * @f: open file handle 409 * @buf: User buffer to write data from 410 * @size: Number of bytes to write 411 * @pos: Offset to seek to 412 * 413 * The lower bits are the BYTE offset of the register to write. This 414 * allows writing multiple registers in a single call and having 415 * the returned size reflect that. 416 */ 417 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf, 418 size_t size, loff_t *pos) 419 { 420 struct amdgpu_device *adev = file_inode(f)->i_private; 421 ssize_t result = 0; 422 int r; 423 424 if (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 while (size) { 440 uint32_t value; 441 442 r = get_user(value, (uint32_t *)buf); 443 if (r) { 444 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 445 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 446 amdgpu_virt_disable_access_debugfs(adev); 447 return r; 448 } 449 450 WREG32_PCIE(*pos, value); 451 452 result += 4; 453 buf += 4; 454 *pos += 4; 455 size -= 4; 456 } 457 458 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 459 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 460 461 amdgpu_virt_disable_access_debugfs(adev); 462 return result; 463 } 464 465 /** 466 * amdgpu_debugfs_regs_didt_read - Read from a DIDT register 467 * 468 * @f: open file handle 469 * @buf: User buffer to store read data in 470 * @size: Number of bytes to read 471 * @pos: Offset to seek to 472 * 473 * The lower bits are the BYTE offset of the register to read. This 474 * allows reading multiple registers in a single call and having 475 * the returned size reflect that. 476 */ 477 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf, 478 size_t size, loff_t *pos) 479 { 480 struct amdgpu_device *adev = file_inode(f)->i_private; 481 ssize_t result = 0; 482 int r; 483 484 if (size & 0x3 || *pos & 0x3) 485 return -EINVAL; 486 487 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 488 if (r < 0) { 489 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 490 return r; 491 } 492 493 r = amdgpu_virt_enable_access_debugfs(adev); 494 if (r < 0) { 495 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 496 return r; 497 } 498 499 while (size) { 500 uint32_t value; 501 502 value = RREG32_DIDT(*pos >> 2); 503 r = put_user(value, (uint32_t *)buf); 504 if (r) { 505 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 506 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 507 amdgpu_virt_disable_access_debugfs(adev); 508 return r; 509 } 510 511 result += 4; 512 buf += 4; 513 *pos += 4; 514 size -= 4; 515 } 516 517 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 518 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 519 520 amdgpu_virt_disable_access_debugfs(adev); 521 return result; 522 } 523 524 /** 525 * amdgpu_debugfs_regs_didt_write - Write to a DIDT register 526 * 527 * @f: open file handle 528 * @buf: User buffer to write data from 529 * @size: Number of bytes to write 530 * @pos: Offset to seek to 531 * 532 * The lower bits are the BYTE offset of the register to write. This 533 * allows writing multiple registers in a single call and having 534 * the returned size reflect that. 535 */ 536 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const 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 r = get_user(value, (uint32_t *)buf); 562 if (r) { 563 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 564 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 565 amdgpu_virt_disable_access_debugfs(adev); 566 return r; 567 } 568 569 WREG32_DIDT(*pos >> 2, value); 570 571 result += 4; 572 buf += 4; 573 *pos += 4; 574 size -= 4; 575 } 576 577 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 578 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 579 580 amdgpu_virt_disable_access_debugfs(adev); 581 return result; 582 } 583 584 /** 585 * amdgpu_debugfs_regs_smc_read - Read from a SMC register 586 * 587 * @f: open file handle 588 * @buf: User buffer to store read data in 589 * @size: Number of bytes to read 590 * @pos: Offset to seek to 591 * 592 * The lower bits are the BYTE offset of the register to read. This 593 * allows reading multiple registers in a single call and having 594 * the returned size reflect that. 595 */ 596 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf, 597 size_t size, loff_t *pos) 598 { 599 struct amdgpu_device *adev = file_inode(f)->i_private; 600 ssize_t result = 0; 601 int r; 602 603 if (size & 0x3 || *pos & 0x3) 604 return -EINVAL; 605 606 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 607 if (r < 0) { 608 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 609 return r; 610 } 611 612 r = amdgpu_virt_enable_access_debugfs(adev); 613 if (r < 0) { 614 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 615 return r; 616 } 617 618 while (size) { 619 uint32_t value; 620 621 value = RREG32_SMC(*pos); 622 r = put_user(value, (uint32_t *)buf); 623 if (r) { 624 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 625 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 626 amdgpu_virt_disable_access_debugfs(adev); 627 return r; 628 } 629 630 result += 4; 631 buf += 4; 632 *pos += 4; 633 size -= 4; 634 } 635 636 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 637 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 638 639 amdgpu_virt_disable_access_debugfs(adev); 640 return result; 641 } 642 643 /** 644 * amdgpu_debugfs_regs_smc_write - Write to a SMC register 645 * 646 * @f: open file handle 647 * @buf: User buffer to write data from 648 * @size: Number of bytes to write 649 * @pos: Offset to seek to 650 * 651 * The lower bits are the BYTE offset of the register to write. This 652 * allows writing multiple registers in a single call and having 653 * the returned size reflect that. 654 */ 655 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf, 656 size_t size, loff_t *pos) 657 { 658 struct amdgpu_device *adev = file_inode(f)->i_private; 659 ssize_t result = 0; 660 int r; 661 662 if (size & 0x3 || *pos & 0x3) 663 return -EINVAL; 664 665 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 666 if (r < 0) { 667 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 668 return r; 669 } 670 671 r = amdgpu_virt_enable_access_debugfs(adev); 672 if (r < 0) { 673 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 674 return r; 675 } 676 677 while (size) { 678 uint32_t value; 679 680 r = get_user(value, (uint32_t *)buf); 681 if (r) { 682 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 683 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 684 amdgpu_virt_disable_access_debugfs(adev); 685 return r; 686 } 687 688 WREG32_SMC(*pos, value); 689 690 result += 4; 691 buf += 4; 692 *pos += 4; 693 size -= 4; 694 } 695 696 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 697 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 698 699 amdgpu_virt_disable_access_debugfs(adev); 700 return result; 701 } 702 703 /** 704 * amdgpu_debugfs_gca_config_read - Read from gfx config data 705 * 706 * @f: open file handle 707 * @buf: User buffer to store read data in 708 * @size: Number of bytes to read 709 * @pos: Offset to seek to 710 * 711 * This file is used to access configuration data in a somewhat 712 * stable fashion. The format is a series of DWORDs with the first 713 * indicating which revision it is. New content is appended to the 714 * end so that older software can still read the data. 715 */ 716 717 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf, 718 size_t size, loff_t *pos) 719 { 720 struct amdgpu_device *adev = file_inode(f)->i_private; 721 ssize_t result = 0; 722 int r; 723 uint32_t *config, no_regs = 0; 724 725 if (size & 0x3 || *pos & 0x3) 726 return -EINVAL; 727 728 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL); 729 if (!config) 730 return -ENOMEM; 731 732 /* version, increment each time something is added */ 733 config[no_regs++] = 3; 734 config[no_regs++] = adev->gfx.config.max_shader_engines; 735 config[no_regs++] = adev->gfx.config.max_tile_pipes; 736 config[no_regs++] = adev->gfx.config.max_cu_per_sh; 737 config[no_regs++] = adev->gfx.config.max_sh_per_se; 738 config[no_regs++] = adev->gfx.config.max_backends_per_se; 739 config[no_regs++] = adev->gfx.config.max_texture_channel_caches; 740 config[no_regs++] = adev->gfx.config.max_gprs; 741 config[no_regs++] = adev->gfx.config.max_gs_threads; 742 config[no_regs++] = adev->gfx.config.max_hw_contexts; 743 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend; 744 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend; 745 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size; 746 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size; 747 config[no_regs++] = adev->gfx.config.num_tile_pipes; 748 config[no_regs++] = adev->gfx.config.backend_enable_mask; 749 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes; 750 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb; 751 config[no_regs++] = adev->gfx.config.shader_engine_tile_size; 752 config[no_regs++] = adev->gfx.config.num_gpus; 753 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size; 754 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg; 755 config[no_regs++] = adev->gfx.config.gb_addr_config; 756 config[no_regs++] = adev->gfx.config.num_rbs; 757 758 /* rev==1 */ 759 config[no_regs++] = adev->rev_id; 760 config[no_regs++] = adev->pg_flags; 761 config[no_regs++] = adev->cg_flags; 762 763 /* rev==2 */ 764 config[no_regs++] = adev->family; 765 config[no_regs++] = adev->external_rev_id; 766 767 /* rev==3 */ 768 config[no_regs++] = adev->pdev->device; 769 config[no_regs++] = adev->pdev->revision; 770 config[no_regs++] = adev->pdev->subsystem_device; 771 config[no_regs++] = adev->pdev->subsystem_vendor; 772 773 while (size && (*pos < no_regs * 4)) { 774 uint32_t value; 775 776 value = config[*pos >> 2]; 777 r = put_user(value, (uint32_t *)buf); 778 if (r) { 779 kfree(config); 780 return r; 781 } 782 783 result += 4; 784 buf += 4; 785 *pos += 4; 786 size -= 4; 787 } 788 789 kfree(config); 790 return result; 791 } 792 793 /** 794 * amdgpu_debugfs_sensor_read - Read from the powerplay sensors 795 * 796 * @f: open file handle 797 * @buf: User buffer to store read data in 798 * @size: Number of bytes to read 799 * @pos: Offset to seek to 800 * 801 * The offset is treated as the BYTE address of one of the sensors 802 * enumerated in amd/include/kgd_pp_interface.h under the 803 * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK 804 * you would use the offset 3 * 4 = 12. 805 */ 806 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf, 807 size_t size, loff_t *pos) 808 { 809 struct amdgpu_device *adev = file_inode(f)->i_private; 810 int idx, x, outsize, r, valuesize; 811 uint32_t values[16]; 812 813 if (size & 3 || *pos & 0x3) 814 return -EINVAL; 815 816 if (!adev->pm.dpm_enabled) 817 return -EINVAL; 818 819 /* convert offset to sensor number */ 820 idx = *pos >> 2; 821 822 valuesize = sizeof(values); 823 824 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 825 if (r < 0) { 826 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 827 return r; 828 } 829 830 r = amdgpu_virt_enable_access_debugfs(adev); 831 if (r < 0) { 832 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 833 return r; 834 } 835 836 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize); 837 838 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 839 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 840 841 if (r) { 842 amdgpu_virt_disable_access_debugfs(adev); 843 return r; 844 } 845 846 if (size > valuesize) { 847 amdgpu_virt_disable_access_debugfs(adev); 848 return -EINVAL; 849 } 850 851 outsize = 0; 852 x = 0; 853 if (!r) { 854 while (size) { 855 r = put_user(values[x++], (int32_t *)buf); 856 buf += 4; 857 size -= 4; 858 outsize += 4; 859 } 860 } 861 862 amdgpu_virt_disable_access_debugfs(adev); 863 return !r ? outsize : r; 864 } 865 866 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data 867 * 868 * @f: open file handle 869 * @buf: User buffer to store read data in 870 * @size: Number of bytes to read 871 * @pos: Offset to seek to 872 * 873 * The offset being sought changes which wave that the status data 874 * will be returned for. The bits are used as follows: 875 * 876 * Bits 0..6: Byte offset into data 877 * Bits 7..14: SE selector 878 * Bits 15..22: SH/SA selector 879 * Bits 23..30: CU/{WGP+SIMD} selector 880 * Bits 31..36: WAVE ID selector 881 * Bits 37..44: SIMD ID selector 882 * 883 * The returned data begins with one DWORD of version information 884 * Followed by WAVE STATUS registers relevant to the GFX IP version 885 * being used. See gfx_v8_0_read_wave_data() for an example output. 886 */ 887 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf, 888 size_t size, loff_t *pos) 889 { 890 struct amdgpu_device *adev = f->f_inode->i_private; 891 int r, x; 892 ssize_t result = 0; 893 uint32_t offset, se, sh, cu, wave, simd, data[32]; 894 895 if (size & 3 || *pos & 3) 896 return -EINVAL; 897 898 /* decode offset */ 899 offset = (*pos & GENMASK_ULL(6, 0)); 900 se = (*pos & GENMASK_ULL(14, 7)) >> 7; 901 sh = (*pos & GENMASK_ULL(22, 15)) >> 15; 902 cu = (*pos & GENMASK_ULL(30, 23)) >> 23; 903 wave = (*pos & GENMASK_ULL(36, 31)) >> 31; 904 simd = (*pos & GENMASK_ULL(44, 37)) >> 37; 905 906 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 907 if (r < 0) { 908 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 909 return r; 910 } 911 912 r = amdgpu_virt_enable_access_debugfs(adev); 913 if (r < 0) { 914 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 915 return r; 916 } 917 918 /* switch to the specific se/sh/cu */ 919 mutex_lock(&adev->grbm_idx_mutex); 920 amdgpu_gfx_select_se_sh(adev, se, sh, cu); 921 922 x = 0; 923 if (adev->gfx.funcs->read_wave_data) 924 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x); 925 926 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); 927 mutex_unlock(&adev->grbm_idx_mutex); 928 929 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 930 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 931 932 if (!x) { 933 amdgpu_virt_disable_access_debugfs(adev); 934 return -EINVAL; 935 } 936 937 while (size && (offset < x * 4)) { 938 uint32_t value; 939 940 value = data[offset >> 2]; 941 r = put_user(value, (uint32_t *)buf); 942 if (r) { 943 amdgpu_virt_disable_access_debugfs(adev); 944 return r; 945 } 946 947 result += 4; 948 buf += 4; 949 offset += 4; 950 size -= 4; 951 } 952 953 amdgpu_virt_disable_access_debugfs(adev); 954 return result; 955 } 956 957 /** amdgpu_debugfs_gpr_read - Read wave gprs 958 * 959 * @f: open file handle 960 * @buf: User buffer to store read data in 961 * @size: Number of bytes to read 962 * @pos: Offset to seek to 963 * 964 * The offset being sought changes which wave that the status data 965 * will be returned for. The bits are used as follows: 966 * 967 * Bits 0..11: Byte offset into data 968 * Bits 12..19: SE selector 969 * Bits 20..27: SH/SA selector 970 * Bits 28..35: CU/{WGP+SIMD} selector 971 * Bits 36..43: WAVE ID selector 972 * Bits 37..44: SIMD ID selector 973 * Bits 52..59: Thread selector 974 * Bits 60..61: Bank selector (VGPR=0,SGPR=1) 975 * 976 * The return data comes from the SGPR or VGPR register bank for 977 * the selected operational unit. 978 */ 979 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf, 980 size_t size, loff_t *pos) 981 { 982 struct amdgpu_device *adev = f->f_inode->i_private; 983 int r; 984 ssize_t result = 0; 985 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data; 986 987 if (size > 4096 || size & 3 || *pos & 3) 988 return -EINVAL; 989 990 /* decode offset */ 991 offset = (*pos & GENMASK_ULL(11, 0)) >> 2; 992 se = (*pos & GENMASK_ULL(19, 12)) >> 12; 993 sh = (*pos & GENMASK_ULL(27, 20)) >> 20; 994 cu = (*pos & GENMASK_ULL(35, 28)) >> 28; 995 wave = (*pos & GENMASK_ULL(43, 36)) >> 36; 996 simd = (*pos & GENMASK_ULL(51, 44)) >> 44; 997 thread = (*pos & GENMASK_ULL(59, 52)) >> 52; 998 bank = (*pos & GENMASK_ULL(61, 60)) >> 60; 999 1000 data = kcalloc(1024, sizeof(*data), GFP_KERNEL); 1001 if (!data) 1002 return -ENOMEM; 1003 1004 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1005 if (r < 0) 1006 goto err; 1007 1008 r = amdgpu_virt_enable_access_debugfs(adev); 1009 if (r < 0) 1010 goto err; 1011 1012 /* switch to the specific se/sh/cu */ 1013 mutex_lock(&adev->grbm_idx_mutex); 1014 amdgpu_gfx_select_se_sh(adev, se, sh, cu); 1015 1016 if (bank == 0) { 1017 if (adev->gfx.funcs->read_wave_vgprs) 1018 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data); 1019 } else { 1020 if (adev->gfx.funcs->read_wave_sgprs) 1021 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data); 1022 } 1023 1024 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); 1025 mutex_unlock(&adev->grbm_idx_mutex); 1026 1027 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 1028 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1029 1030 while (size) { 1031 uint32_t value; 1032 1033 value = data[result >> 2]; 1034 r = put_user(value, (uint32_t *)buf); 1035 if (r) { 1036 amdgpu_virt_disable_access_debugfs(adev); 1037 goto err; 1038 } 1039 1040 result += 4; 1041 buf += 4; 1042 size -= 4; 1043 } 1044 1045 kfree(data); 1046 amdgpu_virt_disable_access_debugfs(adev); 1047 return result; 1048 1049 err: 1050 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1051 kfree(data); 1052 return r; 1053 } 1054 1055 /** 1056 * amdgpu_debugfs_gfxoff_write - Enable/disable GFXOFF 1057 * 1058 * @f: open file handle 1059 * @buf: User buffer to write data from 1060 * @size: Number of bytes to write 1061 * @pos: Offset to seek to 1062 * 1063 * Write a 32-bit zero to disable or a 32-bit non-zero to enable 1064 */ 1065 static ssize_t amdgpu_debugfs_gfxoff_write(struct file *f, const char __user *buf, 1066 size_t size, loff_t *pos) 1067 { 1068 struct amdgpu_device *adev = file_inode(f)->i_private; 1069 ssize_t result = 0; 1070 int r; 1071 1072 if (size & 0x3 || *pos & 0x3) 1073 return -EINVAL; 1074 1075 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1076 if (r < 0) { 1077 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1078 return r; 1079 } 1080 1081 while (size) { 1082 uint32_t value; 1083 1084 r = get_user(value, (uint32_t *)buf); 1085 if (r) { 1086 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 1087 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1088 return r; 1089 } 1090 1091 amdgpu_gfx_off_ctrl(adev, value ? true : false); 1092 1093 result += 4; 1094 buf += 4; 1095 *pos += 4; 1096 size -= 4; 1097 } 1098 1099 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 1100 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1101 1102 return result; 1103 } 1104 1105 1106 /** 1107 * amdgpu_debugfs_gfxoff_read - read gfxoff status 1108 * 1109 * @f: open file handle 1110 * @buf: User buffer to store read data in 1111 * @size: Number of bytes to read 1112 * @pos: Offset to seek to 1113 */ 1114 static ssize_t amdgpu_debugfs_gfxoff_read(struct file *f, char __user *buf, 1115 size_t size, loff_t *pos) 1116 { 1117 struct amdgpu_device *adev = file_inode(f)->i_private; 1118 ssize_t result = 0; 1119 int r; 1120 1121 if (size & 0x3 || *pos & 0x3) 1122 return -EINVAL; 1123 1124 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1125 if (r < 0) 1126 return r; 1127 1128 while (size) { 1129 uint32_t value; 1130 1131 r = amdgpu_get_gfx_off_status(adev, &value); 1132 if (r) { 1133 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 1134 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1135 return r; 1136 } 1137 1138 r = put_user(value, (uint32_t *)buf); 1139 if (r) { 1140 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 1141 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1142 return r; 1143 } 1144 1145 result += 4; 1146 buf += 4; 1147 *pos += 4; 1148 size -= 4; 1149 } 1150 1151 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 1152 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1153 1154 return result; 1155 } 1156 1157 static const struct file_operations amdgpu_debugfs_regs2_fops = { 1158 .owner = THIS_MODULE, 1159 .unlocked_ioctl = amdgpu_debugfs_regs2_ioctl, 1160 .read = amdgpu_debugfs_regs2_read, 1161 .write = amdgpu_debugfs_regs2_write, 1162 .open = amdgpu_debugfs_regs2_open, 1163 .release = amdgpu_debugfs_regs2_release, 1164 .llseek = default_llseek 1165 }; 1166 1167 static const struct file_operations amdgpu_debugfs_regs_fops = { 1168 .owner = THIS_MODULE, 1169 .read = amdgpu_debugfs_regs_read, 1170 .write = amdgpu_debugfs_regs_write, 1171 .llseek = default_llseek 1172 }; 1173 static const struct file_operations amdgpu_debugfs_regs_didt_fops = { 1174 .owner = THIS_MODULE, 1175 .read = amdgpu_debugfs_regs_didt_read, 1176 .write = amdgpu_debugfs_regs_didt_write, 1177 .llseek = default_llseek 1178 }; 1179 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = { 1180 .owner = THIS_MODULE, 1181 .read = amdgpu_debugfs_regs_pcie_read, 1182 .write = amdgpu_debugfs_regs_pcie_write, 1183 .llseek = default_llseek 1184 }; 1185 static const struct file_operations amdgpu_debugfs_regs_smc_fops = { 1186 .owner = THIS_MODULE, 1187 .read = amdgpu_debugfs_regs_smc_read, 1188 .write = amdgpu_debugfs_regs_smc_write, 1189 .llseek = default_llseek 1190 }; 1191 1192 static const struct file_operations amdgpu_debugfs_gca_config_fops = { 1193 .owner = THIS_MODULE, 1194 .read = amdgpu_debugfs_gca_config_read, 1195 .llseek = default_llseek 1196 }; 1197 1198 static const struct file_operations amdgpu_debugfs_sensors_fops = { 1199 .owner = THIS_MODULE, 1200 .read = amdgpu_debugfs_sensor_read, 1201 .llseek = default_llseek 1202 }; 1203 1204 static const struct file_operations amdgpu_debugfs_wave_fops = { 1205 .owner = THIS_MODULE, 1206 .read = amdgpu_debugfs_wave_read, 1207 .llseek = default_llseek 1208 }; 1209 static const struct file_operations amdgpu_debugfs_gpr_fops = { 1210 .owner = THIS_MODULE, 1211 .read = amdgpu_debugfs_gpr_read, 1212 .llseek = default_llseek 1213 }; 1214 1215 static const struct file_operations amdgpu_debugfs_gfxoff_fops = { 1216 .owner = THIS_MODULE, 1217 .read = amdgpu_debugfs_gfxoff_read, 1218 .write = amdgpu_debugfs_gfxoff_write, 1219 .llseek = default_llseek 1220 }; 1221 1222 static const struct file_operations *debugfs_regs[] = { 1223 &amdgpu_debugfs_regs_fops, 1224 &amdgpu_debugfs_regs2_fops, 1225 &amdgpu_debugfs_regs_didt_fops, 1226 &amdgpu_debugfs_regs_pcie_fops, 1227 &amdgpu_debugfs_regs_smc_fops, 1228 &amdgpu_debugfs_gca_config_fops, 1229 &amdgpu_debugfs_sensors_fops, 1230 &amdgpu_debugfs_wave_fops, 1231 &amdgpu_debugfs_gpr_fops, 1232 &amdgpu_debugfs_gfxoff_fops, 1233 }; 1234 1235 static const char *debugfs_regs_names[] = { 1236 "amdgpu_regs", 1237 "amdgpu_regs2", 1238 "amdgpu_regs_didt", 1239 "amdgpu_regs_pcie", 1240 "amdgpu_regs_smc", 1241 "amdgpu_gca_config", 1242 "amdgpu_sensors", 1243 "amdgpu_wave", 1244 "amdgpu_gpr", 1245 "amdgpu_gfxoff", 1246 }; 1247 1248 /** 1249 * amdgpu_debugfs_regs_init - Initialize debugfs entries that provide 1250 * register access. 1251 * 1252 * @adev: The device to attach the debugfs entries to 1253 */ 1254 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) 1255 { 1256 struct drm_minor *minor = adev_to_drm(adev)->primary; 1257 struct dentry *ent, *root = minor->debugfs_root; 1258 unsigned int i; 1259 1260 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) { 1261 ent = debugfs_create_file(debugfs_regs_names[i], 1262 S_IFREG | S_IRUGO, root, 1263 adev, debugfs_regs[i]); 1264 if (!i && !IS_ERR_OR_NULL(ent)) 1265 i_size_write(ent->d_inode, adev->rmmio_size); 1266 } 1267 1268 return 0; 1269 } 1270 1271 static int amdgpu_debugfs_test_ib_show(struct seq_file *m, void *unused) 1272 { 1273 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 1274 struct drm_device *dev = adev_to_drm(adev); 1275 int r = 0, i; 1276 1277 r = pm_runtime_get_sync(dev->dev); 1278 if (r < 0) { 1279 pm_runtime_put_autosuspend(dev->dev); 1280 return r; 1281 } 1282 1283 /* Avoid accidently unparking the sched thread during GPU reset */ 1284 r = down_write_killable(&adev->reset_domain->sem); 1285 if (r) 1286 return r; 1287 1288 /* hold on the scheduler */ 1289 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 1290 struct amdgpu_ring *ring = adev->rings[i]; 1291 1292 if (!ring || !ring->sched.thread) 1293 continue; 1294 kthread_park(ring->sched.thread); 1295 } 1296 1297 seq_printf(m, "run ib test:\n"); 1298 r = amdgpu_ib_ring_tests(adev); 1299 if (r) 1300 seq_printf(m, "ib ring tests failed (%d).\n", r); 1301 else 1302 seq_printf(m, "ib ring tests passed.\n"); 1303 1304 /* go on the scheduler */ 1305 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 1306 struct amdgpu_ring *ring = adev->rings[i]; 1307 1308 if (!ring || !ring->sched.thread) 1309 continue; 1310 kthread_unpark(ring->sched.thread); 1311 } 1312 1313 up_write(&adev->reset_domain->sem); 1314 1315 pm_runtime_mark_last_busy(dev->dev); 1316 pm_runtime_put_autosuspend(dev->dev); 1317 1318 return 0; 1319 } 1320 1321 static int amdgpu_debugfs_evict_vram(void *data, u64 *val) 1322 { 1323 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1324 struct drm_device *dev = adev_to_drm(adev); 1325 int r; 1326 1327 r = pm_runtime_get_sync(dev->dev); 1328 if (r < 0) { 1329 pm_runtime_put_autosuspend(dev->dev); 1330 return r; 1331 } 1332 1333 *val = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM); 1334 1335 pm_runtime_mark_last_busy(dev->dev); 1336 pm_runtime_put_autosuspend(dev->dev); 1337 1338 return 0; 1339 } 1340 1341 1342 static int amdgpu_debugfs_evict_gtt(void *data, u64 *val) 1343 { 1344 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1345 struct drm_device *dev = adev_to_drm(adev); 1346 int r; 1347 1348 r = pm_runtime_get_sync(dev->dev); 1349 if (r < 0) { 1350 pm_runtime_put_autosuspend(dev->dev); 1351 return r; 1352 } 1353 1354 *val = amdgpu_ttm_evict_resources(adev, TTM_PL_TT); 1355 1356 pm_runtime_mark_last_busy(dev->dev); 1357 pm_runtime_put_autosuspend(dev->dev); 1358 1359 return 0; 1360 } 1361 1362 1363 static int amdgpu_debugfs_vm_info_show(struct seq_file *m, void *unused) 1364 { 1365 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 1366 struct drm_device *dev = adev_to_drm(adev); 1367 struct drm_file *file; 1368 int r; 1369 1370 r = mutex_lock_interruptible(&dev->filelist_mutex); 1371 if (r) 1372 return r; 1373 1374 list_for_each_entry(file, &dev->filelist, lhead) { 1375 struct amdgpu_fpriv *fpriv = file->driver_priv; 1376 struct amdgpu_vm *vm = &fpriv->vm; 1377 1378 seq_printf(m, "pid:%d\tProcess:%s ----------\n", 1379 vm->task_info.pid, vm->task_info.process_name); 1380 r = amdgpu_bo_reserve(vm->root.bo, true); 1381 if (r) 1382 break; 1383 amdgpu_debugfs_vm_bo_info(vm, m); 1384 amdgpu_bo_unreserve(vm->root.bo); 1385 } 1386 1387 mutex_unlock(&dev->filelist_mutex); 1388 1389 return r; 1390 } 1391 1392 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_test_ib); 1393 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_vm_info); 1394 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_vram_fops, amdgpu_debugfs_evict_vram, 1395 NULL, "%lld\n"); 1396 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_gtt_fops, amdgpu_debugfs_evict_gtt, 1397 NULL, "%lld\n"); 1398 1399 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring, 1400 struct dma_fence **fences) 1401 { 1402 struct amdgpu_fence_driver *drv = &ring->fence_drv; 1403 uint32_t sync_seq, last_seq; 1404 1405 last_seq = atomic_read(&ring->fence_drv.last_seq); 1406 sync_seq = ring->fence_drv.sync_seq; 1407 1408 last_seq &= drv->num_fences_mask; 1409 sync_seq &= drv->num_fences_mask; 1410 1411 do { 1412 struct dma_fence *fence, **ptr; 1413 1414 ++last_seq; 1415 last_seq &= drv->num_fences_mask; 1416 ptr = &drv->fences[last_seq]; 1417 1418 fence = rcu_dereference_protected(*ptr, 1); 1419 RCU_INIT_POINTER(*ptr, NULL); 1420 1421 if (!fence) 1422 continue; 1423 1424 fences[last_seq] = fence; 1425 1426 } while (last_seq != sync_seq); 1427 } 1428 1429 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences, 1430 int length) 1431 { 1432 int i; 1433 struct dma_fence *fence; 1434 1435 for (i = 0; i < length; i++) { 1436 fence = fences[i]; 1437 if (!fence) 1438 continue; 1439 dma_fence_signal(fence); 1440 dma_fence_put(fence); 1441 } 1442 } 1443 1444 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched) 1445 { 1446 struct drm_sched_job *s_job; 1447 struct dma_fence *fence; 1448 1449 spin_lock(&sched->job_list_lock); 1450 list_for_each_entry(s_job, &sched->pending_list, list) { 1451 fence = sched->ops->run_job(s_job); 1452 dma_fence_put(fence); 1453 } 1454 spin_unlock(&sched->job_list_lock); 1455 } 1456 1457 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring) 1458 { 1459 struct amdgpu_job *job; 1460 struct drm_sched_job *s_job, *tmp; 1461 uint32_t preempt_seq; 1462 struct dma_fence *fence, **ptr; 1463 struct amdgpu_fence_driver *drv = &ring->fence_drv; 1464 struct drm_gpu_scheduler *sched = &ring->sched; 1465 bool preempted = true; 1466 1467 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX) 1468 return; 1469 1470 preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2)); 1471 if (preempt_seq <= atomic_read(&drv->last_seq)) { 1472 preempted = false; 1473 goto no_preempt; 1474 } 1475 1476 preempt_seq &= drv->num_fences_mask; 1477 ptr = &drv->fences[preempt_seq]; 1478 fence = rcu_dereference_protected(*ptr, 1); 1479 1480 no_preempt: 1481 spin_lock(&sched->job_list_lock); 1482 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) { 1483 if (dma_fence_is_signaled(&s_job->s_fence->finished)) { 1484 /* remove job from ring_mirror_list */ 1485 list_del_init(&s_job->list); 1486 sched->ops->free_job(s_job); 1487 continue; 1488 } 1489 job = to_amdgpu_job(s_job); 1490 if (preempted && (&job->hw_fence) == fence) 1491 /* mark the job as preempted */ 1492 job->preemption_status |= AMDGPU_IB_PREEMPTED; 1493 } 1494 spin_unlock(&sched->job_list_lock); 1495 } 1496 1497 static int amdgpu_debugfs_ib_preempt(void *data, u64 val) 1498 { 1499 int r, resched, length; 1500 struct amdgpu_ring *ring; 1501 struct dma_fence **fences = NULL; 1502 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1503 1504 if (val >= AMDGPU_MAX_RINGS) 1505 return -EINVAL; 1506 1507 ring = adev->rings[val]; 1508 1509 if (!ring || !ring->funcs->preempt_ib || !ring->sched.thread) 1510 return -EINVAL; 1511 1512 /* the last preemption failed */ 1513 if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr)) 1514 return -EBUSY; 1515 1516 length = ring->fence_drv.num_fences_mask + 1; 1517 fences = kcalloc(length, sizeof(void *), GFP_KERNEL); 1518 if (!fences) 1519 return -ENOMEM; 1520 1521 /* Avoid accidently unparking the sched thread during GPU reset */ 1522 r = down_read_killable(&adev->reset_domain->sem); 1523 if (r) 1524 goto pro_end; 1525 1526 /* stop the scheduler */ 1527 kthread_park(ring->sched.thread); 1528 1529 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev); 1530 1531 /* preempt the IB */ 1532 r = amdgpu_ring_preempt_ib(ring); 1533 if (r) { 1534 DRM_WARN("failed to preempt ring %d\n", ring->idx); 1535 goto failure; 1536 } 1537 1538 amdgpu_fence_process(ring); 1539 1540 if (atomic_read(&ring->fence_drv.last_seq) != 1541 ring->fence_drv.sync_seq) { 1542 DRM_INFO("ring %d was preempted\n", ring->idx); 1543 1544 amdgpu_ib_preempt_mark_partial_job(ring); 1545 1546 /* swap out the old fences */ 1547 amdgpu_ib_preempt_fences_swap(ring, fences); 1548 1549 amdgpu_fence_driver_force_completion(ring); 1550 1551 /* resubmit unfinished jobs */ 1552 amdgpu_ib_preempt_job_recovery(&ring->sched); 1553 1554 /* wait for jobs finished */ 1555 amdgpu_fence_wait_empty(ring); 1556 1557 /* signal the old fences */ 1558 amdgpu_ib_preempt_signal_fences(fences, length); 1559 } 1560 1561 failure: 1562 /* restart the scheduler */ 1563 kthread_unpark(ring->sched.thread); 1564 1565 up_read(&adev->reset_domain->sem); 1566 1567 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched); 1568 1569 pro_end: 1570 kfree(fences); 1571 1572 return r; 1573 } 1574 1575 static int amdgpu_debugfs_sclk_set(void *data, u64 val) 1576 { 1577 int ret = 0; 1578 uint32_t max_freq, min_freq; 1579 struct amdgpu_device *adev = (struct amdgpu_device *)data; 1580 1581 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1582 return -EINVAL; 1583 1584 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 1585 if (ret < 0) { 1586 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1587 return ret; 1588 } 1589 1590 if (is_support_sw_smu(adev)) { 1591 ret = smu_get_dpm_freq_range(&adev->smu, SMU_SCLK, &min_freq, &max_freq); 1592 if (ret || val > max_freq || val < min_freq) 1593 return -EINVAL; 1594 ret = smu_set_soft_freq_range(&adev->smu, SMU_SCLK, (uint32_t)val, (uint32_t)val); 1595 } else { 1596 return 0; 1597 } 1598 1599 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 1600 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 1601 1602 if (ret) 1603 return -EINVAL; 1604 1605 return 0; 1606 } 1607 1608 DEFINE_DEBUGFS_ATTRIBUTE(fops_ib_preempt, NULL, 1609 amdgpu_debugfs_ib_preempt, "%llu\n"); 1610 1611 DEFINE_DEBUGFS_ATTRIBUTE(fops_sclk_set, NULL, 1612 amdgpu_debugfs_sclk_set, "%llu\n"); 1613 1614 int amdgpu_debugfs_init(struct amdgpu_device *adev) 1615 { 1616 struct dentry *root = adev_to_drm(adev)->primary->debugfs_root; 1617 struct dentry *ent; 1618 int r, i; 1619 1620 if (!debugfs_initialized()) 1621 return 0; 1622 1623 debugfs_create_x32("amdgpu_smu_debug", 0600, root, 1624 &adev->pm.smu_debug_mask); 1625 1626 ent = debugfs_create_file("amdgpu_preempt_ib", 0600, root, adev, 1627 &fops_ib_preempt); 1628 if (IS_ERR(ent)) { 1629 DRM_ERROR("unable to create amdgpu_preempt_ib debugsfs file\n"); 1630 return PTR_ERR(ent); 1631 } 1632 1633 ent = debugfs_create_file("amdgpu_force_sclk", 0200, root, adev, 1634 &fops_sclk_set); 1635 if (IS_ERR(ent)) { 1636 DRM_ERROR("unable to create amdgpu_set_sclk debugsfs file\n"); 1637 return PTR_ERR(ent); 1638 } 1639 1640 /* Register debugfs entries for amdgpu_ttm */ 1641 amdgpu_ttm_debugfs_init(adev); 1642 amdgpu_debugfs_pm_init(adev); 1643 amdgpu_debugfs_sa_init(adev); 1644 amdgpu_debugfs_fence_init(adev); 1645 amdgpu_debugfs_gem_init(adev); 1646 1647 r = amdgpu_debugfs_regs_init(adev); 1648 if (r) 1649 DRM_ERROR("registering register debugfs failed (%d).\n", r); 1650 1651 amdgpu_debugfs_firmware_init(adev); 1652 1653 #if defined(CONFIG_DRM_AMD_DC) 1654 if (amdgpu_device_has_dc_support(adev)) 1655 dtn_debugfs_init(adev); 1656 #endif 1657 1658 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 1659 struct amdgpu_ring *ring = adev->rings[i]; 1660 1661 if (!ring) 1662 continue; 1663 1664 amdgpu_debugfs_ring_init(adev, ring); 1665 } 1666 1667 amdgpu_ras_debugfs_create_all(adev); 1668 amdgpu_rap_debugfs_init(adev); 1669 amdgpu_securedisplay_debugfs_init(adev); 1670 amdgpu_fw_attestation_debugfs_init(adev); 1671 1672 debugfs_create_file("amdgpu_evict_vram", 0444, root, adev, 1673 &amdgpu_evict_vram_fops); 1674 debugfs_create_file("amdgpu_evict_gtt", 0444, root, adev, 1675 &amdgpu_evict_gtt_fops); 1676 debugfs_create_file("amdgpu_test_ib", 0444, root, adev, 1677 &amdgpu_debugfs_test_ib_fops); 1678 debugfs_create_file("amdgpu_vm_info", 0444, root, adev, 1679 &amdgpu_debugfs_vm_info_fops); 1680 1681 adev->debugfs_vbios_blob.data = adev->bios; 1682 adev->debugfs_vbios_blob.size = adev->bios_size; 1683 debugfs_create_blob("amdgpu_vbios", 0444, root, 1684 &adev->debugfs_vbios_blob); 1685 1686 adev->debugfs_discovery_blob.data = adev->mman.discovery_bin; 1687 adev->debugfs_discovery_blob.size = adev->mman.discovery_tmr_size; 1688 debugfs_create_blob("amdgpu_discovery", 0444, root, 1689 &adev->debugfs_discovery_blob); 1690 1691 return 0; 1692 } 1693 1694 #else 1695 int amdgpu_debugfs_init(struct amdgpu_device *adev) 1696 { 1697 return 0; 1698 } 1699 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) 1700 { 1701 return 0; 1702 } 1703 #endif 1704