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>
31 #include <drm/drm_debugfs.h>
34 #include "amdgpu_pm.h"
35 #include "amdgpu_dm_debugfs.h"
36 #include "amdgpu_ras.h"
37 #include "amdgpu_rap.h"
40 * amdgpu_debugfs_add_files - Add simple debugfs entries
42 * @adev: Device to attach debugfs entries to
43 * @files: Array of function callbacks that respond to reads
44 * @nfiles: Number of callbacks to register
47 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
48 const struct drm_info_list *files,
53 for (i = 0; i < adev->debugfs_count; i++) {
54 if (adev->debugfs[i].files == files) {
55 /* Already registered */
60 i = adev->debugfs_count + 1;
61 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
62 DRM_ERROR("Reached maximum number of debugfs components.\n");
63 DRM_ERROR("Report so we increase "
64 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
67 adev->debugfs[adev->debugfs_count].files = files;
68 adev->debugfs[adev->debugfs_count].num_files = nfiles;
69 adev->debugfs_count = i;
70 #if defined(CONFIG_DEBUG_FS)
71 drm_debugfs_create_files(files, nfiles,
72 adev_to_drm(adev)->primary->debugfs_root,
73 adev_to_drm(adev)->primary);
78 int amdgpu_debugfs_wait_dump(struct amdgpu_device *adev)
80 #if defined(CONFIG_DEBUG_FS)
81 unsigned long timeout = 600 * HZ;
84 wake_up_interruptible(&adev->autodump.gpu_hang);
86 ret = wait_for_completion_interruptible_timeout(&adev->autodump.dumping, timeout);
88 pr_err("autodump: timeout, move on to gpu recovery\n");
95 #if defined(CONFIG_DEBUG_FS)
97 static int amdgpu_debugfs_autodump_open(struct inode *inode, struct file *file)
99 struct amdgpu_device *adev = inode->i_private;
102 file->private_data = adev;
104 ret = down_read_killable(&adev->reset_sem);
108 if (adev->autodump.dumping.done) {
109 reinit_completion(&adev->autodump.dumping);
115 up_read(&adev->reset_sem);
120 static int amdgpu_debugfs_autodump_release(struct inode *inode, struct file *file)
122 struct amdgpu_device *adev = file->private_data;
124 complete_all(&adev->autodump.dumping);
128 static unsigned int amdgpu_debugfs_autodump_poll(struct file *file, struct poll_table_struct *poll_table)
130 struct amdgpu_device *adev = file->private_data;
132 poll_wait(file, &adev->autodump.gpu_hang, poll_table);
134 if (amdgpu_in_reset(adev))
135 return POLLIN | POLLRDNORM | POLLWRNORM;
140 static const struct file_operations autodump_debug_fops = {
141 .owner = THIS_MODULE,
142 .open = amdgpu_debugfs_autodump_open,
143 .poll = amdgpu_debugfs_autodump_poll,
144 .release = amdgpu_debugfs_autodump_release,
147 static void amdgpu_debugfs_autodump_init(struct amdgpu_device *adev)
149 init_completion(&adev->autodump.dumping);
150 complete_all(&adev->autodump.dumping);
151 init_waitqueue_head(&adev->autodump.gpu_hang);
153 debugfs_create_file("amdgpu_autodump", 0600,
154 adev_to_drm(adev)->primary->debugfs_root,
155 adev, &autodump_debug_fops);
159 * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes
161 * @read: True if reading
162 * @f: open file handle
163 * @buf: User buffer to write/read to
164 * @size: Number of bytes to write/read
165 * @pos: Offset to seek to
167 * This debugfs entry has special meaning on the offset being sought.
168 * Various bits have different meanings:
170 * Bit 62: Indicates a GRBM bank switch is needed
171 * Bit 61: Indicates a SRBM bank switch is needed (implies bit 62 is
173 * Bits 24..33: The SE or ME selector if needed
174 * Bits 34..43: The SH (or SA) or PIPE selector if needed
175 * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed
177 * Bit 23: Indicates that the PM power gating lock should be held
178 * This is necessary to read registers that might be
179 * unreliable during a power gating transistion.
181 * The lower bits are the BYTE offset of the register to read. This
182 * allows reading multiple registers in a single call and having
183 * the returned size reflect that.
185 static int amdgpu_debugfs_process_reg_op(bool read, struct file *f,
186 char __user *buf, size_t size, loff_t *pos)
188 struct amdgpu_device *adev = file_inode(f)->i_private;
191 bool pm_pg_lock, use_bank, use_ring;
192 unsigned instance_bank, sh_bank, se_bank, me, pipe, queue, vmid;
194 pm_pg_lock = use_bank = use_ring = false;
195 instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0;
197 if (size & 0x3 || *pos & 0x3 ||
198 ((*pos & (1ULL << 62)) && (*pos & (1ULL << 61))))
201 /* are we reading registers for which a PG lock is necessary? */
202 pm_pg_lock = (*pos >> 23) & 1;
204 if (*pos & (1ULL << 62)) {
205 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
206 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
207 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
209 if (se_bank == 0x3FF)
210 se_bank = 0xFFFFFFFF;
211 if (sh_bank == 0x3FF)
212 sh_bank = 0xFFFFFFFF;
213 if (instance_bank == 0x3FF)
214 instance_bank = 0xFFFFFFFF;
216 } else if (*pos & (1ULL << 61)) {
218 me = (*pos & GENMASK_ULL(33, 24)) >> 24;
219 pipe = (*pos & GENMASK_ULL(43, 34)) >> 34;
220 queue = (*pos & GENMASK_ULL(53, 44)) >> 44;
221 vmid = (*pos & GENMASK_ULL(58, 54)) >> 54;
225 use_bank = use_ring = false;
228 *pos &= (1UL << 22) - 1;
230 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
232 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
236 r = amdgpu_virt_enable_access_debugfs(adev);
238 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
243 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
244 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) {
245 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
246 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
247 amdgpu_virt_disable_access_debugfs(adev);
250 mutex_lock(&adev->grbm_idx_mutex);
251 amdgpu_gfx_select_se_sh(adev, se_bank,
252 sh_bank, instance_bank);
253 } else if (use_ring) {
254 mutex_lock(&adev->srbm_mutex);
255 amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid);
259 mutex_lock(&adev->pm.mutex);
265 value = RREG32(*pos >> 2);
266 r = put_user(value, (uint32_t *)buf);
268 r = get_user(value, (uint32_t *)buf);
270 amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value);
285 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
286 mutex_unlock(&adev->grbm_idx_mutex);
287 } else if (use_ring) {
288 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0);
289 mutex_unlock(&adev->srbm_mutex);
293 mutex_unlock(&adev->pm.mutex);
295 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
296 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
298 amdgpu_virt_disable_access_debugfs(adev);
303 * amdgpu_debugfs_regs_read - Callback for reading MMIO registers
305 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
306 size_t size, loff_t *pos)
308 return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos);
312 * amdgpu_debugfs_regs_write - Callback for writing MMIO registers
314 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
315 size_t size, loff_t *pos)
317 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos);
322 * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register
324 * @f: open file handle
325 * @buf: User buffer to store read data in
326 * @size: Number of bytes to read
327 * @pos: Offset to seek to
329 * The lower bits are the BYTE offset of the register to read. This
330 * allows reading multiple registers in a single call and having
331 * the returned size reflect that.
333 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
334 size_t size, loff_t *pos)
336 struct amdgpu_device *adev = file_inode(f)->i_private;
340 if (size & 0x3 || *pos & 0x3)
343 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
345 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
349 r = amdgpu_virt_enable_access_debugfs(adev);
351 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
358 value = RREG32_PCIE(*pos >> 2);
359 r = put_user(value, (uint32_t *)buf);
361 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
362 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
363 amdgpu_virt_disable_access_debugfs(adev);
373 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
374 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
376 amdgpu_virt_disable_access_debugfs(adev);
381 * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register
383 * @f: open file handle
384 * @buf: User buffer to write data from
385 * @size: Number of bytes to write
386 * @pos: Offset to seek to
388 * The lower bits are the BYTE offset of the register to write. This
389 * allows writing multiple registers in a single call and having
390 * the returned size reflect that.
392 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
393 size_t size, loff_t *pos)
395 struct amdgpu_device *adev = file_inode(f)->i_private;
399 if (size & 0x3 || *pos & 0x3)
402 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
404 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
408 r = amdgpu_virt_enable_access_debugfs(adev);
410 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
417 r = get_user(value, (uint32_t *)buf);
419 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
420 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
421 amdgpu_virt_disable_access_debugfs(adev);
425 WREG32_PCIE(*pos >> 2, value);
433 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
434 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
436 amdgpu_virt_disable_access_debugfs(adev);
441 * amdgpu_debugfs_regs_didt_read - Read from a DIDT register
443 * @f: open file handle
444 * @buf: User buffer to store read data in
445 * @size: Number of bytes to read
446 * @pos: Offset to seek to
448 * The lower bits are the BYTE offset of the register to read. This
449 * allows reading multiple registers in a single call and having
450 * the returned size reflect that.
452 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
453 size_t size, loff_t *pos)
455 struct amdgpu_device *adev = file_inode(f)->i_private;
459 if (size & 0x3 || *pos & 0x3)
462 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
464 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
468 r = amdgpu_virt_enable_access_debugfs(adev);
470 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
477 value = RREG32_DIDT(*pos >> 2);
478 r = put_user(value, (uint32_t *)buf);
480 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
481 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
482 amdgpu_virt_disable_access_debugfs(adev);
492 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
493 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
495 amdgpu_virt_disable_access_debugfs(adev);
500 * amdgpu_debugfs_regs_didt_write - Write to a DIDT register
502 * @f: open file handle
503 * @buf: User buffer to write data from
504 * @size: Number of bytes to write
505 * @pos: Offset to seek to
507 * The lower bits are the BYTE offset of the register to write. This
508 * allows writing multiple registers in a single call and having
509 * the returned size reflect that.
511 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
512 size_t size, loff_t *pos)
514 struct amdgpu_device *adev = file_inode(f)->i_private;
518 if (size & 0x3 || *pos & 0x3)
521 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
523 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
527 r = amdgpu_virt_enable_access_debugfs(adev);
529 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
536 r = get_user(value, (uint32_t *)buf);
538 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
539 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
540 amdgpu_virt_disable_access_debugfs(adev);
544 WREG32_DIDT(*pos >> 2, value);
552 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
553 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
555 amdgpu_virt_disable_access_debugfs(adev);
560 * amdgpu_debugfs_regs_smc_read - Read from a SMC register
562 * @f: open file handle
563 * @buf: User buffer to store read data in
564 * @size: Number of bytes to read
565 * @pos: Offset to seek to
567 * The lower bits are the BYTE offset of the register to read. This
568 * allows reading multiple registers in a single call and having
569 * the returned size reflect that.
571 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
572 size_t size, loff_t *pos)
574 struct amdgpu_device *adev = file_inode(f)->i_private;
578 if (size & 0x3 || *pos & 0x3)
581 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
583 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
587 r = amdgpu_virt_enable_access_debugfs(adev);
589 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
596 value = RREG32_SMC(*pos);
597 r = put_user(value, (uint32_t *)buf);
599 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
600 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
601 amdgpu_virt_disable_access_debugfs(adev);
611 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
612 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
614 amdgpu_virt_disable_access_debugfs(adev);
619 * amdgpu_debugfs_regs_smc_write - Write to a SMC register
621 * @f: open file handle
622 * @buf: User buffer to write data from
623 * @size: Number of bytes to write
624 * @pos: Offset to seek to
626 * The lower bits are the BYTE offset of the register to write. This
627 * allows writing multiple registers in a single call and having
628 * the returned size reflect that.
630 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
631 size_t size, loff_t *pos)
633 struct amdgpu_device *adev = file_inode(f)->i_private;
637 if (size & 0x3 || *pos & 0x3)
640 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
642 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
646 r = amdgpu_virt_enable_access_debugfs(adev);
648 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
655 r = get_user(value, (uint32_t *)buf);
657 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
658 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
659 amdgpu_virt_disable_access_debugfs(adev);
663 WREG32_SMC(*pos, value);
671 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
672 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
674 amdgpu_virt_disable_access_debugfs(adev);
679 * amdgpu_debugfs_gca_config_read - Read from gfx config data
681 * @f: open file handle
682 * @buf: User buffer to store read data in
683 * @size: Number of bytes to read
684 * @pos: Offset to seek to
686 * This file is used to access configuration data in a somewhat
687 * stable fashion. The format is a series of DWORDs with the first
688 * indicating which revision it is. New content is appended to the
689 * end so that older software can still read the data.
692 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
693 size_t size, loff_t *pos)
695 struct amdgpu_device *adev = file_inode(f)->i_private;
698 uint32_t *config, no_regs = 0;
700 if (size & 0x3 || *pos & 0x3)
703 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
707 /* version, increment each time something is added */
708 config[no_regs++] = 3;
709 config[no_regs++] = adev->gfx.config.max_shader_engines;
710 config[no_regs++] = adev->gfx.config.max_tile_pipes;
711 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
712 config[no_regs++] = adev->gfx.config.max_sh_per_se;
713 config[no_regs++] = adev->gfx.config.max_backends_per_se;
714 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
715 config[no_regs++] = adev->gfx.config.max_gprs;
716 config[no_regs++] = adev->gfx.config.max_gs_threads;
717 config[no_regs++] = adev->gfx.config.max_hw_contexts;
718 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
719 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
720 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
721 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
722 config[no_regs++] = adev->gfx.config.num_tile_pipes;
723 config[no_regs++] = adev->gfx.config.backend_enable_mask;
724 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
725 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
726 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
727 config[no_regs++] = adev->gfx.config.num_gpus;
728 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
729 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
730 config[no_regs++] = adev->gfx.config.gb_addr_config;
731 config[no_regs++] = adev->gfx.config.num_rbs;
734 config[no_regs++] = adev->rev_id;
735 config[no_regs++] = adev->pg_flags;
736 config[no_regs++] = adev->cg_flags;
739 config[no_regs++] = adev->family;
740 config[no_regs++] = adev->external_rev_id;
743 config[no_regs++] = adev->pdev->device;
744 config[no_regs++] = adev->pdev->revision;
745 config[no_regs++] = adev->pdev->subsystem_device;
746 config[no_regs++] = adev->pdev->subsystem_vendor;
748 while (size && (*pos < no_regs * 4)) {
751 value = config[*pos >> 2];
752 r = put_user(value, (uint32_t *)buf);
769 * amdgpu_debugfs_sensor_read - Read from the powerplay sensors
771 * @f: open file handle
772 * @buf: User buffer to store read data in
773 * @size: Number of bytes to read
774 * @pos: Offset to seek to
776 * The offset is treated as the BYTE address of one of the sensors
777 * enumerated in amd/include/kgd_pp_interface.h under the
778 * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK
779 * you would use the offset 3 * 4 = 12.
781 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
782 size_t size, loff_t *pos)
784 struct amdgpu_device *adev = file_inode(f)->i_private;
785 int idx, x, outsize, r, valuesize;
788 if (size & 3 || *pos & 0x3)
791 if (!adev->pm.dpm_enabled)
794 /* convert offset to sensor number */
797 valuesize = sizeof(values);
799 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
801 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
805 r = amdgpu_virt_enable_access_debugfs(adev);
807 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
811 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
813 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
814 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
817 amdgpu_virt_disable_access_debugfs(adev);
821 if (size > valuesize) {
822 amdgpu_virt_disable_access_debugfs(adev);
830 r = put_user(values[x++], (int32_t *)buf);
837 amdgpu_virt_disable_access_debugfs(adev);
838 return !r ? outsize : r;
841 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data
843 * @f: open file handle
844 * @buf: User buffer to store read data in
845 * @size: Number of bytes to read
846 * @pos: Offset to seek to
848 * The offset being sought changes which wave that the status data
849 * will be returned for. The bits are used as follows:
851 * Bits 0..6: Byte offset into data
852 * Bits 7..14: SE selector
853 * Bits 15..22: SH/SA selector
854 * Bits 23..30: CU/{WGP+SIMD} selector
855 * Bits 31..36: WAVE ID selector
856 * Bits 37..44: SIMD ID selector
858 * The returned data begins with one DWORD of version information
859 * Followed by WAVE STATUS registers relevant to the GFX IP version
860 * being used. See gfx_v8_0_read_wave_data() for an example output.
862 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
863 size_t size, loff_t *pos)
865 struct amdgpu_device *adev = f->f_inode->i_private;
868 uint32_t offset, se, sh, cu, wave, simd, data[32];
870 if (size & 3 || *pos & 3)
874 offset = (*pos & GENMASK_ULL(6, 0));
875 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
876 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
877 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
878 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
879 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
881 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
883 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
887 r = amdgpu_virt_enable_access_debugfs(adev);
889 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
893 /* switch to the specific se/sh/cu */
894 mutex_lock(&adev->grbm_idx_mutex);
895 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
898 if (adev->gfx.funcs->read_wave_data)
899 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
901 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
902 mutex_unlock(&adev->grbm_idx_mutex);
904 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
905 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
908 amdgpu_virt_disable_access_debugfs(adev);
912 while (size && (offset < x * 4)) {
915 value = data[offset >> 2];
916 r = put_user(value, (uint32_t *)buf);
918 amdgpu_virt_disable_access_debugfs(adev);
928 amdgpu_virt_disable_access_debugfs(adev);
932 /** amdgpu_debugfs_gpr_read - Read wave gprs
934 * @f: open file handle
935 * @buf: User buffer to store read data in
936 * @size: Number of bytes to read
937 * @pos: Offset to seek to
939 * The offset being sought changes which wave that the status data
940 * will be returned for. The bits are used as follows:
942 * Bits 0..11: Byte offset into data
943 * Bits 12..19: SE selector
944 * Bits 20..27: SH/SA selector
945 * Bits 28..35: CU/{WGP+SIMD} selector
946 * Bits 36..43: WAVE ID selector
947 * Bits 37..44: SIMD ID selector
948 * Bits 52..59: Thread selector
949 * Bits 60..61: Bank selector (VGPR=0,SGPR=1)
951 * The return data comes from the SGPR or VGPR register bank for
952 * the selected operational unit.
954 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
955 size_t size, loff_t *pos)
957 struct amdgpu_device *adev = f->f_inode->i_private;
960 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
962 if (size > 4096 || size & 3 || *pos & 3)
966 offset = (*pos & GENMASK_ULL(11, 0)) >> 2;
967 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
968 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
969 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
970 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
971 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
972 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
973 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
975 data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
979 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
983 r = amdgpu_virt_enable_access_debugfs(adev);
987 /* switch to the specific se/sh/cu */
988 mutex_lock(&adev->grbm_idx_mutex);
989 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
992 if (adev->gfx.funcs->read_wave_vgprs)
993 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
995 if (adev->gfx.funcs->read_wave_sgprs)
996 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
999 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1000 mutex_unlock(&adev->grbm_idx_mutex);
1002 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1003 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1008 value = data[result >> 2];
1009 r = put_user(value, (uint32_t *)buf);
1011 amdgpu_virt_disable_access_debugfs(adev);
1021 amdgpu_virt_disable_access_debugfs(adev);
1025 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1031 * amdgpu_debugfs_regs_gfxoff_write - Enable/disable GFXOFF
1033 * @f: open file handle
1034 * @buf: User buffer to write data from
1035 * @size: Number of bytes to write
1036 * @pos: Offset to seek to
1038 * Write a 32-bit zero to disable or a 32-bit non-zero to enable
1040 static ssize_t amdgpu_debugfs_gfxoff_write(struct file *f, const char __user *buf,
1041 size_t size, loff_t *pos)
1043 struct amdgpu_device *adev = file_inode(f)->i_private;
1047 if (size & 0x3 || *pos & 0x3)
1050 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1052 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1059 r = get_user(value, (uint32_t *)buf);
1061 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1062 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1066 amdgpu_gfx_off_ctrl(adev, value ? true : false);
1074 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1075 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1082 * amdgpu_debugfs_regs_gfxoff_status - read gfxoff status
1084 * @f: open file handle
1085 * @buf: User buffer to store read data in
1086 * @size: Number of bytes to read
1087 * @pos: Offset to seek to
1089 static ssize_t amdgpu_debugfs_gfxoff_read(struct file *f, char __user *buf,
1090 size_t size, loff_t *pos)
1092 struct amdgpu_device *adev = file_inode(f)->i_private;
1096 if (size & 0x3 || *pos & 0x3)
1099 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1106 r = amdgpu_get_gfx_off_status(adev, &value);
1108 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1109 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1113 r = put_user(value, (uint32_t *)buf);
1115 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1116 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1126 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1127 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1132 static const struct file_operations amdgpu_debugfs_regs_fops = {
1133 .owner = THIS_MODULE,
1134 .read = amdgpu_debugfs_regs_read,
1135 .write = amdgpu_debugfs_regs_write,
1136 .llseek = default_llseek
1138 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
1139 .owner = THIS_MODULE,
1140 .read = amdgpu_debugfs_regs_didt_read,
1141 .write = amdgpu_debugfs_regs_didt_write,
1142 .llseek = default_llseek
1144 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
1145 .owner = THIS_MODULE,
1146 .read = amdgpu_debugfs_regs_pcie_read,
1147 .write = amdgpu_debugfs_regs_pcie_write,
1148 .llseek = default_llseek
1150 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
1151 .owner = THIS_MODULE,
1152 .read = amdgpu_debugfs_regs_smc_read,
1153 .write = amdgpu_debugfs_regs_smc_write,
1154 .llseek = default_llseek
1157 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
1158 .owner = THIS_MODULE,
1159 .read = amdgpu_debugfs_gca_config_read,
1160 .llseek = default_llseek
1163 static const struct file_operations amdgpu_debugfs_sensors_fops = {
1164 .owner = THIS_MODULE,
1165 .read = amdgpu_debugfs_sensor_read,
1166 .llseek = default_llseek
1169 static const struct file_operations amdgpu_debugfs_wave_fops = {
1170 .owner = THIS_MODULE,
1171 .read = amdgpu_debugfs_wave_read,
1172 .llseek = default_llseek
1174 static const struct file_operations amdgpu_debugfs_gpr_fops = {
1175 .owner = THIS_MODULE,
1176 .read = amdgpu_debugfs_gpr_read,
1177 .llseek = default_llseek
1180 static const struct file_operations amdgpu_debugfs_gfxoff_fops = {
1181 .owner = THIS_MODULE,
1182 .read = amdgpu_debugfs_gfxoff_read,
1183 .write = amdgpu_debugfs_gfxoff_write,
1184 .llseek = default_llseek
1187 static const struct file_operations *debugfs_regs[] = {
1188 &amdgpu_debugfs_regs_fops,
1189 &amdgpu_debugfs_regs_didt_fops,
1190 &amdgpu_debugfs_regs_pcie_fops,
1191 &amdgpu_debugfs_regs_smc_fops,
1192 &amdgpu_debugfs_gca_config_fops,
1193 &amdgpu_debugfs_sensors_fops,
1194 &amdgpu_debugfs_wave_fops,
1195 &amdgpu_debugfs_gpr_fops,
1196 &amdgpu_debugfs_gfxoff_fops,
1199 static const char *debugfs_regs_names[] = {
1204 "amdgpu_gca_config",
1212 * amdgpu_debugfs_regs_init - Initialize debugfs entries that provide
1215 * @adev: The device to attach the debugfs entries to
1217 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
1219 struct drm_minor *minor = adev_to_drm(adev)->primary;
1220 struct dentry *ent, *root = minor->debugfs_root;
1223 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
1224 ent = debugfs_create_file(debugfs_regs_names[i],
1225 S_IFREG | S_IRUGO, root,
1226 adev, debugfs_regs[i]);
1227 if (!i && !IS_ERR_OR_NULL(ent))
1228 i_size_write(ent->d_inode, adev->rmmio_size);
1229 adev->debugfs_regs[i] = ent;
1235 static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
1237 struct drm_info_node *node = (struct drm_info_node *) m->private;
1238 struct drm_device *dev = node->minor->dev;
1239 struct amdgpu_device *adev = drm_to_adev(dev);
1242 r = pm_runtime_get_sync(dev->dev);
1244 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1248 /* Avoid accidently unparking the sched thread during GPU reset */
1249 r = down_read_killable(&adev->reset_sem);
1253 /* hold on the scheduler */
1254 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1255 struct amdgpu_ring *ring = adev->rings[i];
1257 if (!ring || !ring->sched.thread)
1259 kthread_park(ring->sched.thread);
1262 seq_printf(m, "run ib test:\n");
1263 r = amdgpu_ib_ring_tests(adev);
1265 seq_printf(m, "ib ring tests failed (%d).\n", r);
1267 seq_printf(m, "ib ring tests passed.\n");
1269 /* go on the scheduler */
1270 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1271 struct amdgpu_ring *ring = adev->rings[i];
1273 if (!ring || !ring->sched.thread)
1275 kthread_unpark(ring->sched.thread);
1278 up_read(&adev->reset_sem);
1280 pm_runtime_mark_last_busy(dev->dev);
1281 pm_runtime_put_autosuspend(dev->dev);
1286 static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
1288 struct drm_info_node *node = (struct drm_info_node *) m->private;
1289 struct drm_device *dev = node->minor->dev;
1290 struct amdgpu_device *adev = drm_to_adev(dev);
1292 seq_write(m, adev->bios, adev->bios_size);
1296 static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data)
1298 struct drm_info_node *node = (struct drm_info_node *)m->private;
1299 struct drm_device *dev = node->minor->dev;
1300 struct amdgpu_device *adev = drm_to_adev(dev);
1303 r = pm_runtime_get_sync(dev->dev);
1305 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1309 seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev));
1311 pm_runtime_mark_last_busy(dev->dev);
1312 pm_runtime_put_autosuspend(dev->dev);
1317 static int amdgpu_debugfs_evict_gtt(struct seq_file *m, void *data)
1319 struct drm_info_node *node = (struct drm_info_node *)m->private;
1320 struct drm_device *dev = node->minor->dev;
1321 struct amdgpu_device *adev = drm_to_adev(dev);
1324 r = pm_runtime_get_sync(dev->dev);
1326 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1330 seq_printf(m, "(%d)\n", ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_TT));
1332 pm_runtime_mark_last_busy(dev->dev);
1333 pm_runtime_put_autosuspend(dev->dev);
1338 static const struct drm_info_list amdgpu_debugfs_list[] = {
1339 {"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump},
1340 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib},
1341 {"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram},
1342 {"amdgpu_evict_gtt", &amdgpu_debugfs_evict_gtt},
1345 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring,
1346 struct dma_fence **fences)
1348 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1349 uint32_t sync_seq, last_seq;
1351 last_seq = atomic_read(&ring->fence_drv.last_seq);
1352 sync_seq = ring->fence_drv.sync_seq;
1354 last_seq &= drv->num_fences_mask;
1355 sync_seq &= drv->num_fences_mask;
1358 struct dma_fence *fence, **ptr;
1361 last_seq &= drv->num_fences_mask;
1362 ptr = &drv->fences[last_seq];
1364 fence = rcu_dereference_protected(*ptr, 1);
1365 RCU_INIT_POINTER(*ptr, NULL);
1370 fences[last_seq] = fence;
1372 } while (last_seq != sync_seq);
1375 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences,
1379 struct dma_fence *fence;
1381 for (i = 0; i < length; i++) {
1385 dma_fence_signal(fence);
1386 dma_fence_put(fence);
1390 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched)
1392 struct drm_sched_job *s_job;
1393 struct dma_fence *fence;
1395 spin_lock(&sched->job_list_lock);
1396 list_for_each_entry(s_job, &sched->ring_mirror_list, node) {
1397 fence = sched->ops->run_job(s_job);
1398 dma_fence_put(fence);
1400 spin_unlock(&sched->job_list_lock);
1403 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring)
1405 struct amdgpu_job *job;
1406 struct drm_sched_job *s_job, *tmp;
1407 uint32_t preempt_seq;
1408 struct dma_fence *fence, **ptr;
1409 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1410 struct drm_gpu_scheduler *sched = &ring->sched;
1411 bool preempted = true;
1413 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
1416 preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2));
1417 if (preempt_seq <= atomic_read(&drv->last_seq)) {
1422 preempt_seq &= drv->num_fences_mask;
1423 ptr = &drv->fences[preempt_seq];
1424 fence = rcu_dereference_protected(*ptr, 1);
1427 spin_lock(&sched->job_list_lock);
1428 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
1429 if (dma_fence_is_signaled(&s_job->s_fence->finished)) {
1430 /* remove job from ring_mirror_list */
1431 list_del_init(&s_job->node);
1432 sched->ops->free_job(s_job);
1435 job = to_amdgpu_job(s_job);
1436 if (preempted && job->fence == fence)
1437 /* mark the job as preempted */
1438 job->preemption_status |= AMDGPU_IB_PREEMPTED;
1440 spin_unlock(&sched->job_list_lock);
1443 static int amdgpu_debugfs_ib_preempt(void *data, u64 val)
1445 int r, resched, length;
1446 struct amdgpu_ring *ring;
1447 struct dma_fence **fences = NULL;
1448 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1450 if (val >= AMDGPU_MAX_RINGS)
1453 ring = adev->rings[val];
1455 if (!ring || !ring->funcs->preempt_ib || !ring->sched.thread)
1458 /* the last preemption failed */
1459 if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr))
1462 length = ring->fence_drv.num_fences_mask + 1;
1463 fences = kcalloc(length, sizeof(void *), GFP_KERNEL);
1467 /* Avoid accidently unparking the sched thread during GPU reset */
1468 r = down_read_killable(&adev->reset_sem);
1472 /* stop the scheduler */
1473 kthread_park(ring->sched.thread);
1475 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1477 /* preempt the IB */
1478 r = amdgpu_ring_preempt_ib(ring);
1480 DRM_WARN("failed to preempt ring %d\n", ring->idx);
1484 amdgpu_fence_process(ring);
1486 if (atomic_read(&ring->fence_drv.last_seq) !=
1487 ring->fence_drv.sync_seq) {
1488 DRM_INFO("ring %d was preempted\n", ring->idx);
1490 amdgpu_ib_preempt_mark_partial_job(ring);
1492 /* swap out the old fences */
1493 amdgpu_ib_preempt_fences_swap(ring, fences);
1495 amdgpu_fence_driver_force_completion(ring);
1497 /* resubmit unfinished jobs */
1498 amdgpu_ib_preempt_job_recovery(&ring->sched);
1500 /* wait for jobs finished */
1501 amdgpu_fence_wait_empty(ring);
1503 /* signal the old fences */
1504 amdgpu_ib_preempt_signal_fences(fences, length);
1508 /* restart the scheduler */
1509 kthread_unpark(ring->sched.thread);
1511 up_read(&adev->reset_sem);
1513 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1521 static int amdgpu_debugfs_sclk_set(void *data, u64 val)
1524 uint32_t max_freq, min_freq;
1525 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1527 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1530 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1532 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1536 if (is_support_sw_smu(adev)) {
1537 ret = smu_get_dpm_freq_range(&adev->smu, SMU_SCLK, &min_freq, &max_freq);
1538 if (ret || val > max_freq || val < min_freq)
1540 ret = smu_set_soft_freq_range(&adev->smu, SMU_SCLK, (uint32_t)val, (uint32_t)val);
1545 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1546 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1554 DEFINE_SIMPLE_ATTRIBUTE(fops_ib_preempt, NULL,
1555 amdgpu_debugfs_ib_preempt, "%llu\n");
1557 DEFINE_SIMPLE_ATTRIBUTE(fops_sclk_set, NULL,
1558 amdgpu_debugfs_sclk_set, "%llu\n");
1560 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1564 adev->debugfs_preempt =
1565 debugfs_create_file("amdgpu_preempt_ib", 0600,
1566 adev_to_drm(adev)->primary->debugfs_root, adev,
1568 if (!(adev->debugfs_preempt)) {
1569 DRM_ERROR("unable to create amdgpu_preempt_ib debugsfs file\n");
1573 adev->smu.debugfs_sclk =
1574 debugfs_create_file("amdgpu_force_sclk", 0200,
1575 adev_to_drm(adev)->primary->debugfs_root, adev,
1577 if (!(adev->smu.debugfs_sclk)) {
1578 DRM_ERROR("unable to create amdgpu_set_sclk debugsfs file\n");
1582 /* Register debugfs entries for amdgpu_ttm */
1583 r = amdgpu_ttm_debugfs_init(adev);
1585 DRM_ERROR("Failed to init debugfs\n");
1589 r = amdgpu_debugfs_pm_init(adev);
1591 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1595 if (amdgpu_debugfs_sa_init(adev)) {
1596 dev_err(adev->dev, "failed to register debugfs file for SA\n");
1599 if (amdgpu_debugfs_fence_init(adev))
1600 dev_err(adev->dev, "fence debugfs file creation failed\n");
1602 r = amdgpu_debugfs_gem_init(adev);
1604 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1606 r = amdgpu_debugfs_regs_init(adev);
1608 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1610 r = amdgpu_debugfs_firmware_init(adev);
1612 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1614 #if defined(CONFIG_DRM_AMD_DC)
1615 if (amdgpu_device_has_dc_support(adev)) {
1616 if (dtn_debugfs_init(adev))
1617 DRM_ERROR("amdgpu: failed initialize dtn debugfs support.\n");
1621 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1622 struct amdgpu_ring *ring = adev->rings[i];
1627 if (amdgpu_debugfs_ring_init(adev, ring)) {
1628 DRM_ERROR("Failed to register debugfs file for rings !\n");
1632 amdgpu_ras_debugfs_create_all(adev);
1634 amdgpu_debugfs_autodump_init(adev);
1636 amdgpu_rap_debugfs_init(adev);
1638 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list,
1639 ARRAY_SIZE(amdgpu_debugfs_list));
1643 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1647 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)