2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
26 #include <linux/kthread.h>
27 #include <linux/pci.h>
28 #include <linux/uaccess.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/poll.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"
40 int amdgpu_debugfs_wait_dump(struct amdgpu_device *adev)
42 #if defined(CONFIG_DEBUG_FS)
43 unsigned long timeout = 600 * HZ;
46 wake_up_interruptible(&adev->autodump.gpu_hang);
48 ret = wait_for_completion_interruptible_timeout(&adev->autodump.dumping, timeout);
50 pr_err("autodump: timeout, move on to gpu recovery\n");
57 #if defined(CONFIG_DEBUG_FS)
59 static int amdgpu_debugfs_autodump_open(struct inode *inode, struct file *file)
61 struct amdgpu_device *adev = inode->i_private;
64 file->private_data = adev;
66 ret = down_read_killable(&adev->reset_sem);
70 if (adev->autodump.dumping.done) {
71 reinit_completion(&adev->autodump.dumping);
77 up_read(&adev->reset_sem);
82 static int amdgpu_debugfs_autodump_release(struct inode *inode, struct file *file)
84 struct amdgpu_device *adev = file->private_data;
86 complete_all(&adev->autodump.dumping);
90 static unsigned int amdgpu_debugfs_autodump_poll(struct file *file, struct poll_table_struct *poll_table)
92 struct amdgpu_device *adev = file->private_data;
94 poll_wait(file, &adev->autodump.gpu_hang, poll_table);
96 if (amdgpu_in_reset(adev))
97 return POLLIN | POLLRDNORM | POLLWRNORM;
102 static const struct file_operations autodump_debug_fops = {
103 .owner = THIS_MODULE,
104 .open = amdgpu_debugfs_autodump_open,
105 .poll = amdgpu_debugfs_autodump_poll,
106 .release = amdgpu_debugfs_autodump_release,
109 static void amdgpu_debugfs_autodump_init(struct amdgpu_device *adev)
111 init_completion(&adev->autodump.dumping);
112 complete_all(&adev->autodump.dumping);
113 init_waitqueue_head(&adev->autodump.gpu_hang);
115 debugfs_create_file("amdgpu_autodump", 0600,
116 adev_to_drm(adev)->primary->debugfs_root,
117 adev, &autodump_debug_fops);
121 * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes
123 * @read: True if reading
124 * @f: open file handle
125 * @buf: User buffer to write/read to
126 * @size: Number of bytes to write/read
127 * @pos: Offset to seek to
129 * This debugfs entry has special meaning on the offset being sought.
130 * Various bits have different meanings:
132 * Bit 62: Indicates a GRBM bank switch is needed
133 * Bit 61: Indicates a SRBM bank switch is needed (implies bit 62 is
135 * Bits 24..33: The SE or ME selector if needed
136 * Bits 34..43: The SH (or SA) or PIPE selector if needed
137 * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed
139 * Bit 23: Indicates that the PM power gating lock should be held
140 * This is necessary to read registers that might be
141 * unreliable during a power gating transistion.
143 * The lower bits are the BYTE offset of the register to read. This
144 * allows reading multiple registers in a single call and having
145 * the returned size reflect that.
147 static int amdgpu_debugfs_process_reg_op(bool read, struct file *f,
148 char __user *buf, size_t size, loff_t *pos)
150 struct amdgpu_device *adev = file_inode(f)->i_private;
153 bool pm_pg_lock, use_bank, use_ring;
154 unsigned instance_bank, sh_bank, se_bank, me, pipe, queue, vmid;
156 pm_pg_lock = use_bank = use_ring = false;
157 instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0;
159 if (size & 0x3 || *pos & 0x3 ||
160 ((*pos & (1ULL << 62)) && (*pos & (1ULL << 61))))
163 /* are we reading registers for which a PG lock is necessary? */
164 pm_pg_lock = (*pos >> 23) & 1;
166 if (*pos & (1ULL << 62)) {
167 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
168 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
169 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
171 if (se_bank == 0x3FF)
172 se_bank = 0xFFFFFFFF;
173 if (sh_bank == 0x3FF)
174 sh_bank = 0xFFFFFFFF;
175 if (instance_bank == 0x3FF)
176 instance_bank = 0xFFFFFFFF;
178 } else if (*pos & (1ULL << 61)) {
180 me = (*pos & GENMASK_ULL(33, 24)) >> 24;
181 pipe = (*pos & GENMASK_ULL(43, 34)) >> 34;
182 queue = (*pos & GENMASK_ULL(53, 44)) >> 44;
183 vmid = (*pos & GENMASK_ULL(58, 54)) >> 54;
187 use_bank = use_ring = false;
190 *pos &= (1UL << 22) - 1;
192 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
194 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
198 r = amdgpu_virt_enable_access_debugfs(adev);
200 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
205 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
206 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) {
207 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
208 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
209 amdgpu_virt_disable_access_debugfs(adev);
212 mutex_lock(&adev->grbm_idx_mutex);
213 amdgpu_gfx_select_se_sh(adev, se_bank,
214 sh_bank, instance_bank);
215 } else if (use_ring) {
216 mutex_lock(&adev->srbm_mutex);
217 amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid);
221 mutex_lock(&adev->pm.mutex);
227 value = RREG32(*pos >> 2);
228 r = put_user(value, (uint32_t *)buf);
230 r = get_user(value, (uint32_t *)buf);
232 amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value);
247 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
248 mutex_unlock(&adev->grbm_idx_mutex);
249 } else if (use_ring) {
250 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0);
251 mutex_unlock(&adev->srbm_mutex);
255 mutex_unlock(&adev->pm.mutex);
257 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
258 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
260 amdgpu_virt_disable_access_debugfs(adev);
265 * amdgpu_debugfs_regs_read - Callback for reading MMIO registers
267 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
268 size_t size, loff_t *pos)
270 return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos);
274 * amdgpu_debugfs_regs_write - Callback for writing MMIO registers
276 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
277 size_t size, loff_t *pos)
279 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos);
284 * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register
286 * @f: open file handle
287 * @buf: User buffer to store read data in
288 * @size: Number of bytes to read
289 * @pos: Offset to seek to
291 * The lower bits are the BYTE offset of the register to read. This
292 * allows reading multiple registers in a single call and having
293 * the returned size reflect that.
295 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
296 size_t size, loff_t *pos)
298 struct amdgpu_device *adev = file_inode(f)->i_private;
302 if (size & 0x3 || *pos & 0x3)
305 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
307 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
311 r = amdgpu_virt_enable_access_debugfs(adev);
313 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
320 value = RREG32_PCIE(*pos);
321 r = put_user(value, (uint32_t *)buf);
323 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
324 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
325 amdgpu_virt_disable_access_debugfs(adev);
335 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
336 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
338 amdgpu_virt_disable_access_debugfs(adev);
343 * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register
345 * @f: open file handle
346 * @buf: User buffer to write data from
347 * @size: Number of bytes to write
348 * @pos: Offset to seek to
350 * The lower bits are the BYTE offset of the register to write. This
351 * allows writing multiple registers in a single call and having
352 * the returned size reflect that.
354 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
355 size_t size, loff_t *pos)
357 struct amdgpu_device *adev = file_inode(f)->i_private;
361 if (size & 0x3 || *pos & 0x3)
364 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
366 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
370 r = amdgpu_virt_enable_access_debugfs(adev);
372 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
379 r = get_user(value, (uint32_t *)buf);
381 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
382 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
383 amdgpu_virt_disable_access_debugfs(adev);
387 WREG32_PCIE(*pos, value);
395 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
396 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
398 amdgpu_virt_disable_access_debugfs(adev);
403 * amdgpu_debugfs_regs_didt_read - Read from a DIDT register
405 * @f: open file handle
406 * @buf: User buffer to store read data in
407 * @size: Number of bytes to read
408 * @pos: Offset to seek to
410 * The lower bits are the BYTE offset of the register to read. This
411 * allows reading multiple registers in a single call and having
412 * the returned size reflect that.
414 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
415 size_t size, loff_t *pos)
417 struct amdgpu_device *adev = file_inode(f)->i_private;
421 if (size & 0x3 || *pos & 0x3)
424 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
426 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
430 r = amdgpu_virt_enable_access_debugfs(adev);
432 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
439 value = RREG32_DIDT(*pos >> 2);
440 r = put_user(value, (uint32_t *)buf);
442 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
443 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
444 amdgpu_virt_disable_access_debugfs(adev);
454 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
455 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
457 amdgpu_virt_disable_access_debugfs(adev);
462 * amdgpu_debugfs_regs_didt_write - Write to a DIDT register
464 * @f: open file handle
465 * @buf: User buffer to write data from
466 * @size: Number of bytes to write
467 * @pos: Offset to seek to
469 * The lower bits are the BYTE offset of the register to write. This
470 * allows writing multiple registers in a single call and having
471 * the returned size reflect that.
473 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
474 size_t size, loff_t *pos)
476 struct amdgpu_device *adev = file_inode(f)->i_private;
480 if (size & 0x3 || *pos & 0x3)
483 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
485 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
489 r = amdgpu_virt_enable_access_debugfs(adev);
491 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
498 r = get_user(value, (uint32_t *)buf);
500 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
501 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
502 amdgpu_virt_disable_access_debugfs(adev);
506 WREG32_DIDT(*pos >> 2, value);
514 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
515 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
517 amdgpu_virt_disable_access_debugfs(adev);
522 * amdgpu_debugfs_regs_smc_read - Read from a SMC register
524 * @f: open file handle
525 * @buf: User buffer to store read data in
526 * @size: Number of bytes to read
527 * @pos: Offset to seek to
529 * The lower bits are the BYTE offset of the register to read. This
530 * allows reading multiple registers in a single call and having
531 * the returned size reflect that.
533 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
534 size_t size, loff_t *pos)
536 struct amdgpu_device *adev = file_inode(f)->i_private;
540 if (size & 0x3 || *pos & 0x3)
543 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
545 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
549 r = amdgpu_virt_enable_access_debugfs(adev);
551 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
558 value = RREG32_SMC(*pos);
559 r = put_user(value, (uint32_t *)buf);
561 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
562 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
563 amdgpu_virt_disable_access_debugfs(adev);
573 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
574 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
576 amdgpu_virt_disable_access_debugfs(adev);
581 * amdgpu_debugfs_regs_smc_write - Write to a SMC register
583 * @f: open file handle
584 * @buf: User buffer to write data from
585 * @size: Number of bytes to write
586 * @pos: Offset to seek to
588 * The lower bits are the BYTE offset of the register to write. This
589 * allows writing multiple registers in a single call and having
590 * the returned size reflect that.
592 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
593 size_t size, loff_t *pos)
595 struct amdgpu_device *adev = file_inode(f)->i_private;
599 if (size & 0x3 || *pos & 0x3)
602 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
604 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
608 r = amdgpu_virt_enable_access_debugfs(adev);
610 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
617 r = get_user(value, (uint32_t *)buf);
619 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
620 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
621 amdgpu_virt_disable_access_debugfs(adev);
625 WREG32_SMC(*pos, value);
633 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
634 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
636 amdgpu_virt_disable_access_debugfs(adev);
641 * amdgpu_debugfs_gca_config_read - Read from gfx config data
643 * @f: open file handle
644 * @buf: User buffer to store read data in
645 * @size: Number of bytes to read
646 * @pos: Offset to seek to
648 * This file is used to access configuration data in a somewhat
649 * stable fashion. The format is a series of DWORDs with the first
650 * indicating which revision it is. New content is appended to the
651 * end so that older software can still read the data.
654 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
655 size_t size, loff_t *pos)
657 struct amdgpu_device *adev = file_inode(f)->i_private;
660 uint32_t *config, no_regs = 0;
662 if (size & 0x3 || *pos & 0x3)
665 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
669 /* version, increment each time something is added */
670 config[no_regs++] = 3;
671 config[no_regs++] = adev->gfx.config.max_shader_engines;
672 config[no_regs++] = adev->gfx.config.max_tile_pipes;
673 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
674 config[no_regs++] = adev->gfx.config.max_sh_per_se;
675 config[no_regs++] = adev->gfx.config.max_backends_per_se;
676 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
677 config[no_regs++] = adev->gfx.config.max_gprs;
678 config[no_regs++] = adev->gfx.config.max_gs_threads;
679 config[no_regs++] = adev->gfx.config.max_hw_contexts;
680 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
681 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
682 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
683 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
684 config[no_regs++] = adev->gfx.config.num_tile_pipes;
685 config[no_regs++] = adev->gfx.config.backend_enable_mask;
686 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
687 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
688 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
689 config[no_regs++] = adev->gfx.config.num_gpus;
690 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
691 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
692 config[no_regs++] = adev->gfx.config.gb_addr_config;
693 config[no_regs++] = adev->gfx.config.num_rbs;
696 config[no_regs++] = adev->rev_id;
697 config[no_regs++] = adev->pg_flags;
698 config[no_regs++] = adev->cg_flags;
701 config[no_regs++] = adev->family;
702 config[no_regs++] = adev->external_rev_id;
705 config[no_regs++] = adev->pdev->device;
706 config[no_regs++] = adev->pdev->revision;
707 config[no_regs++] = adev->pdev->subsystem_device;
708 config[no_regs++] = adev->pdev->subsystem_vendor;
710 while (size && (*pos < no_regs * 4)) {
713 value = config[*pos >> 2];
714 r = put_user(value, (uint32_t *)buf);
731 * amdgpu_debugfs_sensor_read - Read from the powerplay sensors
733 * @f: open file handle
734 * @buf: User buffer to store read data in
735 * @size: Number of bytes to read
736 * @pos: Offset to seek to
738 * The offset is treated as the BYTE address of one of the sensors
739 * enumerated in amd/include/kgd_pp_interface.h under the
740 * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK
741 * you would use the offset 3 * 4 = 12.
743 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
744 size_t size, loff_t *pos)
746 struct amdgpu_device *adev = file_inode(f)->i_private;
747 int idx, x, outsize, r, valuesize;
750 if (size & 3 || *pos & 0x3)
753 if (!adev->pm.dpm_enabled)
756 /* convert offset to sensor number */
759 valuesize = sizeof(values);
761 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
763 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
767 r = amdgpu_virt_enable_access_debugfs(adev);
769 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
773 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
775 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
776 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
779 amdgpu_virt_disable_access_debugfs(adev);
783 if (size > valuesize) {
784 amdgpu_virt_disable_access_debugfs(adev);
792 r = put_user(values[x++], (int32_t *)buf);
799 amdgpu_virt_disable_access_debugfs(adev);
800 return !r ? outsize : r;
803 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data
805 * @f: open file handle
806 * @buf: User buffer to store read data in
807 * @size: Number of bytes to read
808 * @pos: Offset to seek to
810 * The offset being sought changes which wave that the status data
811 * will be returned for. The bits are used as follows:
813 * Bits 0..6: Byte offset into data
814 * Bits 7..14: SE selector
815 * Bits 15..22: SH/SA selector
816 * Bits 23..30: CU/{WGP+SIMD} selector
817 * Bits 31..36: WAVE ID selector
818 * Bits 37..44: SIMD ID selector
820 * The returned data begins with one DWORD of version information
821 * Followed by WAVE STATUS registers relevant to the GFX IP version
822 * being used. See gfx_v8_0_read_wave_data() for an example output.
824 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
825 size_t size, loff_t *pos)
827 struct amdgpu_device *adev = f->f_inode->i_private;
830 uint32_t offset, se, sh, cu, wave, simd, data[32];
832 if (size & 3 || *pos & 3)
836 offset = (*pos & GENMASK_ULL(6, 0));
837 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
838 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
839 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
840 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
841 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
843 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
845 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
849 r = amdgpu_virt_enable_access_debugfs(adev);
851 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
855 /* switch to the specific se/sh/cu */
856 mutex_lock(&adev->grbm_idx_mutex);
857 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
860 if (adev->gfx.funcs->read_wave_data)
861 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
863 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
864 mutex_unlock(&adev->grbm_idx_mutex);
866 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
867 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
870 amdgpu_virt_disable_access_debugfs(adev);
874 while (size && (offset < x * 4)) {
877 value = data[offset >> 2];
878 r = put_user(value, (uint32_t *)buf);
880 amdgpu_virt_disable_access_debugfs(adev);
890 amdgpu_virt_disable_access_debugfs(adev);
894 /** amdgpu_debugfs_gpr_read - Read wave gprs
896 * @f: open file handle
897 * @buf: User buffer to store read data in
898 * @size: Number of bytes to read
899 * @pos: Offset to seek to
901 * The offset being sought changes which wave that the status data
902 * will be returned for. The bits are used as follows:
904 * Bits 0..11: Byte offset into data
905 * Bits 12..19: SE selector
906 * Bits 20..27: SH/SA selector
907 * Bits 28..35: CU/{WGP+SIMD} selector
908 * Bits 36..43: WAVE ID selector
909 * Bits 37..44: SIMD ID selector
910 * Bits 52..59: Thread selector
911 * Bits 60..61: Bank selector (VGPR=0,SGPR=1)
913 * The return data comes from the SGPR or VGPR register bank for
914 * the selected operational unit.
916 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
917 size_t size, loff_t *pos)
919 struct amdgpu_device *adev = f->f_inode->i_private;
922 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
924 if (size > 4096 || size & 3 || *pos & 3)
928 offset = (*pos & GENMASK_ULL(11, 0)) >> 2;
929 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
930 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
931 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
932 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
933 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
934 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
935 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
937 data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
941 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
945 r = amdgpu_virt_enable_access_debugfs(adev);
949 /* switch to the specific se/sh/cu */
950 mutex_lock(&adev->grbm_idx_mutex);
951 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
954 if (adev->gfx.funcs->read_wave_vgprs)
955 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
957 if (adev->gfx.funcs->read_wave_sgprs)
958 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
961 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
962 mutex_unlock(&adev->grbm_idx_mutex);
964 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
965 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
970 value = data[result >> 2];
971 r = put_user(value, (uint32_t *)buf);
973 amdgpu_virt_disable_access_debugfs(adev);
983 amdgpu_virt_disable_access_debugfs(adev);
987 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
993 * amdgpu_debugfs_regs_gfxoff_write - Enable/disable GFXOFF
995 * @f: open file handle
996 * @buf: User buffer to write data from
997 * @size: Number of bytes to write
998 * @pos: Offset to seek to
1000 * Write a 32-bit zero to disable or a 32-bit non-zero to enable
1002 static ssize_t amdgpu_debugfs_gfxoff_write(struct file *f, const char __user *buf,
1003 size_t size, loff_t *pos)
1005 struct amdgpu_device *adev = file_inode(f)->i_private;
1009 if (size & 0x3 || *pos & 0x3)
1012 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1014 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1021 r = get_user(value, (uint32_t *)buf);
1023 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1024 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1028 amdgpu_gfx_off_ctrl(adev, value ? true : false);
1036 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1037 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1044 * amdgpu_debugfs_regs_gfxoff_status - read gfxoff status
1046 * @f: open file handle
1047 * @buf: User buffer to store read data in
1048 * @size: Number of bytes to read
1049 * @pos: Offset to seek to
1051 static ssize_t amdgpu_debugfs_gfxoff_read(struct file *f, char __user *buf,
1052 size_t size, loff_t *pos)
1054 struct amdgpu_device *adev = file_inode(f)->i_private;
1058 if (size & 0x3 || *pos & 0x3)
1061 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1068 r = amdgpu_get_gfx_off_status(adev, &value);
1070 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1071 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1075 r = put_user(value, (uint32_t *)buf);
1077 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1078 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1088 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1089 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1094 static const struct file_operations amdgpu_debugfs_regs_fops = {
1095 .owner = THIS_MODULE,
1096 .read = amdgpu_debugfs_regs_read,
1097 .write = amdgpu_debugfs_regs_write,
1098 .llseek = default_llseek
1100 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
1101 .owner = THIS_MODULE,
1102 .read = amdgpu_debugfs_regs_didt_read,
1103 .write = amdgpu_debugfs_regs_didt_write,
1104 .llseek = default_llseek
1106 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
1107 .owner = THIS_MODULE,
1108 .read = amdgpu_debugfs_regs_pcie_read,
1109 .write = amdgpu_debugfs_regs_pcie_write,
1110 .llseek = default_llseek
1112 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
1113 .owner = THIS_MODULE,
1114 .read = amdgpu_debugfs_regs_smc_read,
1115 .write = amdgpu_debugfs_regs_smc_write,
1116 .llseek = default_llseek
1119 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
1120 .owner = THIS_MODULE,
1121 .read = amdgpu_debugfs_gca_config_read,
1122 .llseek = default_llseek
1125 static const struct file_operations amdgpu_debugfs_sensors_fops = {
1126 .owner = THIS_MODULE,
1127 .read = amdgpu_debugfs_sensor_read,
1128 .llseek = default_llseek
1131 static const struct file_operations amdgpu_debugfs_wave_fops = {
1132 .owner = THIS_MODULE,
1133 .read = amdgpu_debugfs_wave_read,
1134 .llseek = default_llseek
1136 static const struct file_operations amdgpu_debugfs_gpr_fops = {
1137 .owner = THIS_MODULE,
1138 .read = amdgpu_debugfs_gpr_read,
1139 .llseek = default_llseek
1142 static const struct file_operations amdgpu_debugfs_gfxoff_fops = {
1143 .owner = THIS_MODULE,
1144 .read = amdgpu_debugfs_gfxoff_read,
1145 .write = amdgpu_debugfs_gfxoff_write,
1146 .llseek = default_llseek
1149 static const struct file_operations *debugfs_regs[] = {
1150 &amdgpu_debugfs_regs_fops,
1151 &amdgpu_debugfs_regs_didt_fops,
1152 &amdgpu_debugfs_regs_pcie_fops,
1153 &amdgpu_debugfs_regs_smc_fops,
1154 &amdgpu_debugfs_gca_config_fops,
1155 &amdgpu_debugfs_sensors_fops,
1156 &amdgpu_debugfs_wave_fops,
1157 &amdgpu_debugfs_gpr_fops,
1158 &amdgpu_debugfs_gfxoff_fops,
1161 static const char *debugfs_regs_names[] = {
1166 "amdgpu_gca_config",
1174 * amdgpu_debugfs_regs_init - Initialize debugfs entries that provide
1177 * @adev: The device to attach the debugfs entries to
1179 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
1181 struct drm_minor *minor = adev_to_drm(adev)->primary;
1182 struct dentry *ent, *root = minor->debugfs_root;
1185 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
1186 ent = debugfs_create_file(debugfs_regs_names[i],
1187 S_IFREG | S_IRUGO, root,
1188 adev, debugfs_regs[i]);
1189 if (!i && !IS_ERR_OR_NULL(ent))
1190 i_size_write(ent->d_inode, adev->rmmio_size);
1196 static int amdgpu_debugfs_test_ib_show(struct seq_file *m, void *unused)
1198 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
1199 struct drm_device *dev = adev_to_drm(adev);
1202 r = pm_runtime_get_sync(dev->dev);
1204 pm_runtime_put_autosuspend(dev->dev);
1208 /* Avoid accidently unparking the sched thread during GPU reset */
1209 r = down_read_killable(&adev->reset_sem);
1213 /* hold on the scheduler */
1214 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1215 struct amdgpu_ring *ring = adev->rings[i];
1217 if (!ring || !ring->sched.thread)
1219 kthread_park(ring->sched.thread);
1222 seq_printf(m, "run ib test:\n");
1223 r = amdgpu_ib_ring_tests(adev);
1225 seq_printf(m, "ib ring tests failed (%d).\n", r);
1227 seq_printf(m, "ib ring tests passed.\n");
1229 /* go on the scheduler */
1230 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1231 struct amdgpu_ring *ring = adev->rings[i];
1233 if (!ring || !ring->sched.thread)
1235 kthread_unpark(ring->sched.thread);
1238 up_read(&adev->reset_sem);
1240 pm_runtime_mark_last_busy(dev->dev);
1241 pm_runtime_put_autosuspend(dev->dev);
1246 static int amdgpu_debugfs_evict_vram(void *data, u64 *val)
1248 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1249 struct drm_device *dev = adev_to_drm(adev);
1252 r = pm_runtime_get_sync(dev->dev);
1254 pm_runtime_put_autosuspend(dev->dev);
1258 *val = amdgpu_bo_evict_vram(adev);
1260 pm_runtime_mark_last_busy(dev->dev);
1261 pm_runtime_put_autosuspend(dev->dev);
1267 static int amdgpu_debugfs_evict_gtt(void *data, u64 *val)
1269 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1270 struct drm_device *dev = adev_to_drm(adev);
1271 struct ttm_resource_manager *man;
1274 r = pm_runtime_get_sync(dev->dev);
1276 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1280 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
1281 *val = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
1283 pm_runtime_mark_last_busy(dev->dev);
1284 pm_runtime_put_autosuspend(dev->dev);
1290 static int amdgpu_debugfs_vm_info_show(struct seq_file *m, void *unused)
1292 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
1293 struct drm_device *dev = adev_to_drm(adev);
1294 struct drm_file *file;
1297 r = mutex_lock_interruptible(&dev->filelist_mutex);
1301 list_for_each_entry(file, &dev->filelist, lhead) {
1302 struct amdgpu_fpriv *fpriv = file->driver_priv;
1303 struct amdgpu_vm *vm = &fpriv->vm;
1305 seq_printf(m, "pid:%d\tProcess:%s ----------\n",
1306 vm->task_info.pid, vm->task_info.process_name);
1307 r = amdgpu_bo_reserve(vm->root.base.bo, true);
1310 amdgpu_debugfs_vm_bo_info(vm, m);
1311 amdgpu_bo_unreserve(vm->root.base.bo);
1314 mutex_unlock(&dev->filelist_mutex);
1319 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_test_ib);
1320 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_vm_info);
1321 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_vram_fops, amdgpu_debugfs_evict_vram,
1323 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_gtt_fops, amdgpu_debugfs_evict_gtt,
1326 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring,
1327 struct dma_fence **fences)
1329 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1330 uint32_t sync_seq, last_seq;
1332 last_seq = atomic_read(&ring->fence_drv.last_seq);
1333 sync_seq = ring->fence_drv.sync_seq;
1335 last_seq &= drv->num_fences_mask;
1336 sync_seq &= drv->num_fences_mask;
1339 struct dma_fence *fence, **ptr;
1342 last_seq &= drv->num_fences_mask;
1343 ptr = &drv->fences[last_seq];
1345 fence = rcu_dereference_protected(*ptr, 1);
1346 RCU_INIT_POINTER(*ptr, NULL);
1351 fences[last_seq] = fence;
1353 } while (last_seq != sync_seq);
1356 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences,
1360 struct dma_fence *fence;
1362 for (i = 0; i < length; i++) {
1366 dma_fence_signal(fence);
1367 dma_fence_put(fence);
1371 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched)
1373 struct drm_sched_job *s_job;
1374 struct dma_fence *fence;
1376 spin_lock(&sched->job_list_lock);
1377 list_for_each_entry(s_job, &sched->pending_list, list) {
1378 fence = sched->ops->run_job(s_job);
1379 dma_fence_put(fence);
1381 spin_unlock(&sched->job_list_lock);
1384 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring)
1386 struct amdgpu_job *job;
1387 struct drm_sched_job *s_job, *tmp;
1388 uint32_t preempt_seq;
1389 struct dma_fence *fence, **ptr;
1390 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1391 struct drm_gpu_scheduler *sched = &ring->sched;
1392 bool preempted = true;
1394 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
1397 preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2));
1398 if (preempt_seq <= atomic_read(&drv->last_seq)) {
1403 preempt_seq &= drv->num_fences_mask;
1404 ptr = &drv->fences[preempt_seq];
1405 fence = rcu_dereference_protected(*ptr, 1);
1408 spin_lock(&sched->job_list_lock);
1409 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
1410 if (dma_fence_is_signaled(&s_job->s_fence->finished)) {
1411 /* remove job from ring_mirror_list */
1412 list_del_init(&s_job->list);
1413 sched->ops->free_job(s_job);
1416 job = to_amdgpu_job(s_job);
1417 if (preempted && job->fence == fence)
1418 /* mark the job as preempted */
1419 job->preemption_status |= AMDGPU_IB_PREEMPTED;
1421 spin_unlock(&sched->job_list_lock);
1424 static int amdgpu_debugfs_ib_preempt(void *data, u64 val)
1426 int r, resched, length;
1427 struct amdgpu_ring *ring;
1428 struct dma_fence **fences = NULL;
1429 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1431 if (val >= AMDGPU_MAX_RINGS)
1434 ring = adev->rings[val];
1436 if (!ring || !ring->funcs->preempt_ib || !ring->sched.thread)
1439 /* the last preemption failed */
1440 if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr))
1443 length = ring->fence_drv.num_fences_mask + 1;
1444 fences = kcalloc(length, sizeof(void *), GFP_KERNEL);
1448 /* Avoid accidently unparking the sched thread during GPU reset */
1449 r = down_read_killable(&adev->reset_sem);
1453 /* stop the scheduler */
1454 kthread_park(ring->sched.thread);
1456 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1458 /* preempt the IB */
1459 r = amdgpu_ring_preempt_ib(ring);
1461 DRM_WARN("failed to preempt ring %d\n", ring->idx);
1465 amdgpu_fence_process(ring);
1467 if (atomic_read(&ring->fence_drv.last_seq) !=
1468 ring->fence_drv.sync_seq) {
1469 DRM_INFO("ring %d was preempted\n", ring->idx);
1471 amdgpu_ib_preempt_mark_partial_job(ring);
1473 /* swap out the old fences */
1474 amdgpu_ib_preempt_fences_swap(ring, fences);
1476 amdgpu_fence_driver_force_completion(ring);
1478 /* resubmit unfinished jobs */
1479 amdgpu_ib_preempt_job_recovery(&ring->sched);
1481 /* wait for jobs finished */
1482 amdgpu_fence_wait_empty(ring);
1484 /* signal the old fences */
1485 amdgpu_ib_preempt_signal_fences(fences, length);
1489 /* restart the scheduler */
1490 kthread_unpark(ring->sched.thread);
1492 up_read(&adev->reset_sem);
1494 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1502 static int amdgpu_debugfs_sclk_set(void *data, u64 val)
1505 uint32_t max_freq, min_freq;
1506 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1508 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1511 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1513 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1517 if (is_support_sw_smu(adev)) {
1518 ret = smu_get_dpm_freq_range(&adev->smu, SMU_SCLK, &min_freq, &max_freq);
1519 if (ret || val > max_freq || val < min_freq)
1521 ret = smu_set_soft_freq_range(&adev->smu, SMU_SCLK, (uint32_t)val, (uint32_t)val);
1526 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1527 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1535 DEFINE_DEBUGFS_ATTRIBUTE(fops_ib_preempt, NULL,
1536 amdgpu_debugfs_ib_preempt, "%llu\n");
1538 DEFINE_DEBUGFS_ATTRIBUTE(fops_sclk_set, NULL,
1539 amdgpu_debugfs_sclk_set, "%llu\n");
1541 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1543 struct dentry *root = adev_to_drm(adev)->primary->debugfs_root;
1549 ent = debugfs_create_file("amdgpu_preempt_ib", 0600, root, adev,
1552 DRM_ERROR("unable to create amdgpu_preempt_ib debugsfs file\n");
1556 ent = debugfs_create_file("amdgpu_force_sclk", 0200, root, adev,
1559 DRM_ERROR("unable to create amdgpu_set_sclk debugsfs file\n");
1563 /* Register debugfs entries for amdgpu_ttm */
1564 amdgpu_ttm_debugfs_init(adev);
1565 amdgpu_debugfs_pm_init(adev);
1566 amdgpu_debugfs_sa_init(adev);
1567 amdgpu_debugfs_fence_init(adev);
1568 amdgpu_debugfs_gem_init(adev);
1570 r = amdgpu_debugfs_regs_init(adev);
1572 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1574 amdgpu_debugfs_firmware_init(adev);
1576 #if defined(CONFIG_DRM_AMD_DC)
1577 if (amdgpu_device_has_dc_support(adev))
1578 dtn_debugfs_init(adev);
1581 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1582 struct amdgpu_ring *ring = adev->rings[i];
1587 if (amdgpu_debugfs_ring_init(adev, ring)) {
1588 DRM_ERROR("Failed to register debugfs file for rings !\n");
1592 amdgpu_ras_debugfs_create_all(adev);
1593 amdgpu_debugfs_autodump_init(adev);
1594 amdgpu_rap_debugfs_init(adev);
1595 amdgpu_securedisplay_debugfs_init(adev);
1596 amdgpu_fw_attestation_debugfs_init(adev);
1598 debugfs_create_file("amdgpu_evict_vram", 0444, root, adev,
1599 &amdgpu_evict_vram_fops);
1600 debugfs_create_file("amdgpu_evict_gtt", 0444, root, adev,
1601 &amdgpu_evict_gtt_fops);
1602 debugfs_create_file("amdgpu_test_ib", 0444, root, adev,
1603 &amdgpu_debugfs_test_ib_fops);
1604 debugfs_create_file("amdgpu_vm_info", 0444, root, adev,
1605 &amdgpu_debugfs_vm_info_fops);
1607 adev->debugfs_vbios_blob.data = adev->bios;
1608 adev->debugfs_vbios_blob.size = adev->bios_size;
1609 debugfs_create_blob("amdgpu_vbios", 0444, root,
1610 &adev->debugfs_vbios_blob);
1616 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1620 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)