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->ddev->primary->debugfs_root,
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 mutex_lock(&adev->lock_reset);
105 if (adev->autodump.dumping.done) {
106 reinit_completion(&adev->autodump.dumping);
111 mutex_unlock(&adev->lock_reset);
116 static int amdgpu_debugfs_autodump_release(struct inode *inode, struct file *file)
118 struct amdgpu_device *adev = file->private_data;
120 complete_all(&adev->autodump.dumping);
124 static unsigned int amdgpu_debugfs_autodump_poll(struct file *file, struct poll_table_struct *poll_table)
126 struct amdgpu_device *adev = file->private_data;
128 poll_wait(file, &adev->autodump.gpu_hang, poll_table);
130 if (amdgpu_in_reset(adev))
131 return POLLIN | POLLRDNORM | POLLWRNORM;
136 static const struct file_operations autodump_debug_fops = {
137 .owner = THIS_MODULE,
138 .open = amdgpu_debugfs_autodump_open,
139 .poll = amdgpu_debugfs_autodump_poll,
140 .release = amdgpu_debugfs_autodump_release,
143 static void amdgpu_debugfs_autodump_init(struct amdgpu_device *adev)
145 init_completion(&adev->autodump.dumping);
146 complete_all(&adev->autodump.dumping);
147 init_waitqueue_head(&adev->autodump.gpu_hang);
149 debugfs_create_file("amdgpu_autodump", 0600,
150 adev->ddev->primary->debugfs_root,
151 adev, &autodump_debug_fops);
155 * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes
157 * @read: True if reading
158 * @f: open file handle
159 * @buf: User buffer to write/read to
160 * @size: Number of bytes to write/read
161 * @pos: Offset to seek to
163 * This debugfs entry has special meaning on the offset being sought.
164 * Various bits have different meanings:
166 * Bit 62: Indicates a GRBM bank switch is needed
167 * Bit 61: Indicates a SRBM bank switch is needed (implies bit 62 is
169 * Bits 24..33: The SE or ME selector if needed
170 * Bits 34..43: The SH (or SA) or PIPE selector if needed
171 * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed
173 * Bit 23: Indicates that the PM power gating lock should be held
174 * This is necessary to read registers that might be
175 * unreliable during a power gating transistion.
177 * The lower bits are the BYTE offset of the register to read. This
178 * allows reading multiple registers in a single call and having
179 * the returned size reflect that.
181 static int amdgpu_debugfs_process_reg_op(bool read, struct file *f,
182 char __user *buf, size_t size, loff_t *pos)
184 struct amdgpu_device *adev = file_inode(f)->i_private;
187 bool pm_pg_lock, use_bank, use_ring;
188 unsigned instance_bank, sh_bank, se_bank, me, pipe, queue, vmid;
190 pm_pg_lock = use_bank = use_ring = false;
191 instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0;
193 if (size & 0x3 || *pos & 0x3 ||
194 ((*pos & (1ULL << 62)) && (*pos & (1ULL << 61))))
197 /* are we reading registers for which a PG lock is necessary? */
198 pm_pg_lock = (*pos >> 23) & 1;
200 if (*pos & (1ULL << 62)) {
201 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
202 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
203 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
205 if (se_bank == 0x3FF)
206 se_bank = 0xFFFFFFFF;
207 if (sh_bank == 0x3FF)
208 sh_bank = 0xFFFFFFFF;
209 if (instance_bank == 0x3FF)
210 instance_bank = 0xFFFFFFFF;
212 } else if (*pos & (1ULL << 61)) {
214 me = (*pos & GENMASK_ULL(33, 24)) >> 24;
215 pipe = (*pos & GENMASK_ULL(43, 34)) >> 34;
216 queue = (*pos & GENMASK_ULL(53, 44)) >> 44;
217 vmid = (*pos & GENMASK_ULL(58, 54)) >> 54;
221 use_bank = use_ring = false;
224 *pos &= (1UL << 22) - 1;
226 r = pm_runtime_get_sync(adev->ddev->dev);
228 pm_runtime_put_autosuspend(adev->ddev->dev);
232 r = amdgpu_virt_enable_access_debugfs(adev);
234 pm_runtime_put_autosuspend(adev->ddev->dev);
239 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
240 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) {
241 pm_runtime_mark_last_busy(adev->ddev->dev);
242 pm_runtime_put_autosuspend(adev->ddev->dev);
243 amdgpu_virt_disable_access_debugfs(adev);
246 mutex_lock(&adev->grbm_idx_mutex);
247 amdgpu_gfx_select_se_sh(adev, se_bank,
248 sh_bank, instance_bank);
249 } else if (use_ring) {
250 mutex_lock(&adev->srbm_mutex);
251 amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid);
255 mutex_lock(&adev->pm.mutex);
261 value = RREG32(*pos >> 2);
262 r = put_user(value, (uint32_t *)buf);
264 r = get_user(value, (uint32_t *)buf);
266 amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0);
281 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
282 mutex_unlock(&adev->grbm_idx_mutex);
283 } else if (use_ring) {
284 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0);
285 mutex_unlock(&adev->srbm_mutex);
289 mutex_unlock(&adev->pm.mutex);
291 pm_runtime_mark_last_busy(adev->ddev->dev);
292 pm_runtime_put_autosuspend(adev->ddev->dev);
294 amdgpu_virt_disable_access_debugfs(adev);
299 * amdgpu_debugfs_regs_read - Callback for reading MMIO registers
301 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
302 size_t size, loff_t *pos)
304 return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos);
308 * amdgpu_debugfs_regs_write - Callback for writing MMIO registers
310 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
311 size_t size, loff_t *pos)
313 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos);
318 * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register
320 * @f: open file handle
321 * @buf: User buffer to store read data in
322 * @size: Number of bytes to read
323 * @pos: Offset to seek to
325 * The lower bits are the BYTE offset of the register to read. This
326 * allows reading multiple registers in a single call and having
327 * the returned size reflect that.
329 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
330 size_t size, loff_t *pos)
332 struct amdgpu_device *adev = file_inode(f)->i_private;
336 if (size & 0x3 || *pos & 0x3)
339 r = pm_runtime_get_sync(adev->ddev->dev);
341 pm_runtime_put_autosuspend(adev->ddev->dev);
345 r = amdgpu_virt_enable_access_debugfs(adev);
347 pm_runtime_put_autosuspend(adev->ddev->dev);
354 value = RREG32_PCIE(*pos >> 2);
355 r = put_user(value, (uint32_t *)buf);
357 pm_runtime_mark_last_busy(adev->ddev->dev);
358 pm_runtime_put_autosuspend(adev->ddev->dev);
359 amdgpu_virt_disable_access_debugfs(adev);
369 pm_runtime_mark_last_busy(adev->ddev->dev);
370 pm_runtime_put_autosuspend(adev->ddev->dev);
372 amdgpu_virt_disable_access_debugfs(adev);
377 * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register
379 * @f: open file handle
380 * @buf: User buffer to write data from
381 * @size: Number of bytes to write
382 * @pos: Offset to seek to
384 * The lower bits are the BYTE offset of the register to write. This
385 * allows writing multiple registers in a single call and having
386 * the returned size reflect that.
388 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
389 size_t size, loff_t *pos)
391 struct amdgpu_device *adev = file_inode(f)->i_private;
395 if (size & 0x3 || *pos & 0x3)
398 r = pm_runtime_get_sync(adev->ddev->dev);
400 pm_runtime_put_autosuspend(adev->ddev->dev);
404 r = amdgpu_virt_enable_access_debugfs(adev);
406 pm_runtime_put_autosuspend(adev->ddev->dev);
413 r = get_user(value, (uint32_t *)buf);
415 pm_runtime_mark_last_busy(adev->ddev->dev);
416 pm_runtime_put_autosuspend(adev->ddev->dev);
417 amdgpu_virt_disable_access_debugfs(adev);
421 WREG32_PCIE(*pos >> 2, value);
429 pm_runtime_mark_last_busy(adev->ddev->dev);
430 pm_runtime_put_autosuspend(adev->ddev->dev);
432 amdgpu_virt_disable_access_debugfs(adev);
437 * amdgpu_debugfs_regs_didt_read - Read from a DIDT register
439 * @f: open file handle
440 * @buf: User buffer to store read data in
441 * @size: Number of bytes to read
442 * @pos: Offset to seek to
444 * The lower bits are the BYTE offset of the register to read. This
445 * allows reading multiple registers in a single call and having
446 * the returned size reflect that.
448 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
449 size_t size, loff_t *pos)
451 struct amdgpu_device *adev = file_inode(f)->i_private;
455 if (size & 0x3 || *pos & 0x3)
458 r = pm_runtime_get_sync(adev->ddev->dev);
460 pm_runtime_put_autosuspend(adev->ddev->dev);
464 r = amdgpu_virt_enable_access_debugfs(adev);
466 pm_runtime_put_autosuspend(adev->ddev->dev);
473 value = RREG32_DIDT(*pos >> 2);
474 r = put_user(value, (uint32_t *)buf);
476 pm_runtime_mark_last_busy(adev->ddev->dev);
477 pm_runtime_put_autosuspend(adev->ddev->dev);
478 amdgpu_virt_disable_access_debugfs(adev);
488 pm_runtime_mark_last_busy(adev->ddev->dev);
489 pm_runtime_put_autosuspend(adev->ddev->dev);
491 amdgpu_virt_disable_access_debugfs(adev);
496 * amdgpu_debugfs_regs_didt_write - Write to a DIDT register
498 * @f: open file handle
499 * @buf: User buffer to write data from
500 * @size: Number of bytes to write
501 * @pos: Offset to seek to
503 * The lower bits are the BYTE offset of the register to write. This
504 * allows writing multiple registers in a single call and having
505 * the returned size reflect that.
507 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
508 size_t size, loff_t *pos)
510 struct amdgpu_device *adev = file_inode(f)->i_private;
514 if (size & 0x3 || *pos & 0x3)
517 r = pm_runtime_get_sync(adev->ddev->dev);
519 pm_runtime_put_autosuspend(adev->ddev->dev);
523 r = amdgpu_virt_enable_access_debugfs(adev);
525 pm_runtime_put_autosuspend(adev->ddev->dev);
532 r = get_user(value, (uint32_t *)buf);
534 pm_runtime_mark_last_busy(adev->ddev->dev);
535 pm_runtime_put_autosuspend(adev->ddev->dev);
536 amdgpu_virt_disable_access_debugfs(adev);
540 WREG32_DIDT(*pos >> 2, value);
548 pm_runtime_mark_last_busy(adev->ddev->dev);
549 pm_runtime_put_autosuspend(adev->ddev->dev);
551 amdgpu_virt_disable_access_debugfs(adev);
556 * amdgpu_debugfs_regs_smc_read - Read from a SMC register
558 * @f: open file handle
559 * @buf: User buffer to store read data in
560 * @size: Number of bytes to read
561 * @pos: Offset to seek to
563 * The lower bits are the BYTE offset of the register to read. This
564 * allows reading multiple registers in a single call and having
565 * the returned size reflect that.
567 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
568 size_t size, loff_t *pos)
570 struct amdgpu_device *adev = file_inode(f)->i_private;
574 if (size & 0x3 || *pos & 0x3)
577 r = pm_runtime_get_sync(adev->ddev->dev);
579 pm_runtime_put_autosuspend(adev->ddev->dev);
583 r = amdgpu_virt_enable_access_debugfs(adev);
585 pm_runtime_put_autosuspend(adev->ddev->dev);
592 value = RREG32_SMC(*pos);
593 r = put_user(value, (uint32_t *)buf);
595 pm_runtime_mark_last_busy(adev->ddev->dev);
596 pm_runtime_put_autosuspend(adev->ddev->dev);
597 amdgpu_virt_disable_access_debugfs(adev);
607 pm_runtime_mark_last_busy(adev->ddev->dev);
608 pm_runtime_put_autosuspend(adev->ddev->dev);
610 amdgpu_virt_disable_access_debugfs(adev);
615 * amdgpu_debugfs_regs_smc_write - Write to a SMC register
617 * @f: open file handle
618 * @buf: User buffer to write data from
619 * @size: Number of bytes to write
620 * @pos: Offset to seek to
622 * The lower bits are the BYTE offset of the register to write. This
623 * allows writing multiple registers in a single call and having
624 * the returned size reflect that.
626 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
627 size_t size, loff_t *pos)
629 struct amdgpu_device *adev = file_inode(f)->i_private;
633 if (size & 0x3 || *pos & 0x3)
636 r = pm_runtime_get_sync(adev->ddev->dev);
638 pm_runtime_put_autosuspend(adev->ddev->dev);
642 r = amdgpu_virt_enable_access_debugfs(adev);
644 pm_runtime_put_autosuspend(adev->ddev->dev);
651 r = get_user(value, (uint32_t *)buf);
653 pm_runtime_mark_last_busy(adev->ddev->dev);
654 pm_runtime_put_autosuspend(adev->ddev->dev);
655 amdgpu_virt_disable_access_debugfs(adev);
659 WREG32_SMC(*pos, value);
667 pm_runtime_mark_last_busy(adev->ddev->dev);
668 pm_runtime_put_autosuspend(adev->ddev->dev);
670 amdgpu_virt_disable_access_debugfs(adev);
675 * amdgpu_debugfs_gca_config_read - Read from gfx config data
677 * @f: open file handle
678 * @buf: User buffer to store read data in
679 * @size: Number of bytes to read
680 * @pos: Offset to seek to
682 * This file is used to access configuration data in a somewhat
683 * stable fashion. The format is a series of DWORDs with the first
684 * indicating which revision it is. New content is appended to the
685 * end so that older software can still read the data.
688 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
689 size_t size, loff_t *pos)
691 struct amdgpu_device *adev = file_inode(f)->i_private;
694 uint32_t *config, no_regs = 0;
696 if (size & 0x3 || *pos & 0x3)
699 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
703 /* version, increment each time something is added */
704 config[no_regs++] = 3;
705 config[no_regs++] = adev->gfx.config.max_shader_engines;
706 config[no_regs++] = adev->gfx.config.max_tile_pipes;
707 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
708 config[no_regs++] = adev->gfx.config.max_sh_per_se;
709 config[no_regs++] = adev->gfx.config.max_backends_per_se;
710 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
711 config[no_regs++] = adev->gfx.config.max_gprs;
712 config[no_regs++] = adev->gfx.config.max_gs_threads;
713 config[no_regs++] = adev->gfx.config.max_hw_contexts;
714 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
715 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
716 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
717 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
718 config[no_regs++] = adev->gfx.config.num_tile_pipes;
719 config[no_regs++] = adev->gfx.config.backend_enable_mask;
720 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
721 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
722 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
723 config[no_regs++] = adev->gfx.config.num_gpus;
724 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
725 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
726 config[no_regs++] = adev->gfx.config.gb_addr_config;
727 config[no_regs++] = adev->gfx.config.num_rbs;
730 config[no_regs++] = adev->rev_id;
731 config[no_regs++] = adev->pg_flags;
732 config[no_regs++] = adev->cg_flags;
735 config[no_regs++] = adev->family;
736 config[no_regs++] = adev->external_rev_id;
739 config[no_regs++] = adev->pdev->device;
740 config[no_regs++] = adev->pdev->revision;
741 config[no_regs++] = adev->pdev->subsystem_device;
742 config[no_regs++] = adev->pdev->subsystem_vendor;
744 while (size && (*pos < no_regs * 4)) {
747 value = config[*pos >> 2];
748 r = put_user(value, (uint32_t *)buf);
765 * amdgpu_debugfs_sensor_read - Read from the powerplay sensors
767 * @f: open file handle
768 * @buf: User buffer to store read data in
769 * @size: Number of bytes to read
770 * @pos: Offset to seek to
772 * The offset is treated as the BYTE address of one of the sensors
773 * enumerated in amd/include/kgd_pp_interface.h under the
774 * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK
775 * you would use the offset 3 * 4 = 12.
777 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
778 size_t size, loff_t *pos)
780 struct amdgpu_device *adev = file_inode(f)->i_private;
781 int idx, x, outsize, r, valuesize;
784 if (size & 3 || *pos & 0x3)
787 if (!adev->pm.dpm_enabled)
790 /* convert offset to sensor number */
793 valuesize = sizeof(values);
795 r = pm_runtime_get_sync(adev->ddev->dev);
797 pm_runtime_put_autosuspend(adev->ddev->dev);
801 r = amdgpu_virt_enable_access_debugfs(adev);
803 pm_runtime_put_autosuspend(adev->ddev->dev);
807 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
809 pm_runtime_mark_last_busy(adev->ddev->dev);
810 pm_runtime_put_autosuspend(adev->ddev->dev);
813 amdgpu_virt_disable_access_debugfs(adev);
817 if (size > valuesize) {
818 amdgpu_virt_disable_access_debugfs(adev);
826 r = put_user(values[x++], (int32_t *)buf);
833 amdgpu_virt_disable_access_debugfs(adev);
834 return !r ? outsize : r;
837 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data
839 * @f: open file handle
840 * @buf: User buffer to store read data in
841 * @size: Number of bytes to read
842 * @pos: Offset to seek to
844 * The offset being sought changes which wave that the status data
845 * will be returned for. The bits are used as follows:
847 * Bits 0..6: Byte offset into data
848 * Bits 7..14: SE selector
849 * Bits 15..22: SH/SA selector
850 * Bits 23..30: CU/{WGP+SIMD} selector
851 * Bits 31..36: WAVE ID selector
852 * Bits 37..44: SIMD ID selector
854 * The returned data begins with one DWORD of version information
855 * Followed by WAVE STATUS registers relevant to the GFX IP version
856 * being used. See gfx_v8_0_read_wave_data() for an example output.
858 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
859 size_t size, loff_t *pos)
861 struct amdgpu_device *adev = f->f_inode->i_private;
864 uint32_t offset, se, sh, cu, wave, simd, data[32];
866 if (size & 3 || *pos & 3)
870 offset = (*pos & GENMASK_ULL(6, 0));
871 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
872 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
873 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
874 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
875 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
877 r = pm_runtime_get_sync(adev->ddev->dev);
879 pm_runtime_put_autosuspend(adev->ddev->dev);
883 r = amdgpu_virt_enable_access_debugfs(adev);
885 pm_runtime_put_autosuspend(adev->ddev->dev);
889 /* switch to the specific se/sh/cu */
890 mutex_lock(&adev->grbm_idx_mutex);
891 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
894 if (adev->gfx.funcs->read_wave_data)
895 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
897 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
898 mutex_unlock(&adev->grbm_idx_mutex);
900 pm_runtime_mark_last_busy(adev->ddev->dev);
901 pm_runtime_put_autosuspend(adev->ddev->dev);
904 amdgpu_virt_disable_access_debugfs(adev);
908 while (size && (offset < x * 4)) {
911 value = data[offset >> 2];
912 r = put_user(value, (uint32_t *)buf);
914 amdgpu_virt_disable_access_debugfs(adev);
924 amdgpu_virt_disable_access_debugfs(adev);
928 /** amdgpu_debugfs_gpr_read - Read wave gprs
930 * @f: open file handle
931 * @buf: User buffer to store read data in
932 * @size: Number of bytes to read
933 * @pos: Offset to seek to
935 * The offset being sought changes which wave that the status data
936 * will be returned for. The bits are used as follows:
938 * Bits 0..11: Byte offset into data
939 * Bits 12..19: SE selector
940 * Bits 20..27: SH/SA selector
941 * Bits 28..35: CU/{WGP+SIMD} selector
942 * Bits 36..43: WAVE ID selector
943 * Bits 37..44: SIMD ID selector
944 * Bits 52..59: Thread selector
945 * Bits 60..61: Bank selector (VGPR=0,SGPR=1)
947 * The return data comes from the SGPR or VGPR register bank for
948 * the selected operational unit.
950 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
951 size_t size, loff_t *pos)
953 struct amdgpu_device *adev = f->f_inode->i_private;
956 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
958 if (size > 4096 || size & 3 || *pos & 3)
962 offset = (*pos & GENMASK_ULL(11, 0)) >> 2;
963 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
964 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
965 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
966 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
967 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
968 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
969 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
971 data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
975 r = pm_runtime_get_sync(adev->ddev->dev);
979 r = amdgpu_virt_enable_access_debugfs(adev);
983 /* switch to the specific se/sh/cu */
984 mutex_lock(&adev->grbm_idx_mutex);
985 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
988 if (adev->gfx.funcs->read_wave_vgprs)
989 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
991 if (adev->gfx.funcs->read_wave_sgprs)
992 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
995 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
996 mutex_unlock(&adev->grbm_idx_mutex);
998 pm_runtime_mark_last_busy(adev->ddev->dev);
999 pm_runtime_put_autosuspend(adev->ddev->dev);
1004 value = data[result >> 2];
1005 r = put_user(value, (uint32_t *)buf);
1007 amdgpu_virt_disable_access_debugfs(adev);
1017 amdgpu_virt_disable_access_debugfs(adev);
1021 pm_runtime_put_autosuspend(adev->ddev->dev);
1027 * amdgpu_debugfs_regs_gfxoff_write - Enable/disable GFXOFF
1029 * @f: open file handle
1030 * @buf: User buffer to write data from
1031 * @size: Number of bytes to write
1032 * @pos: Offset to seek to
1034 * Write a 32-bit zero to disable or a 32-bit non-zero to enable
1036 static ssize_t amdgpu_debugfs_gfxoff_write(struct file *f, const char __user *buf,
1037 size_t size, loff_t *pos)
1039 struct amdgpu_device *adev = file_inode(f)->i_private;
1043 if (size & 0x3 || *pos & 0x3)
1046 r = pm_runtime_get_sync(adev->ddev->dev);
1048 pm_runtime_put_autosuspend(adev->ddev->dev);
1055 r = get_user(value, (uint32_t *)buf);
1057 pm_runtime_mark_last_busy(adev->ddev->dev);
1058 pm_runtime_put_autosuspend(adev->ddev->dev);
1062 amdgpu_gfx_off_ctrl(adev, value ? true : false);
1070 pm_runtime_mark_last_busy(adev->ddev->dev);
1071 pm_runtime_put_autosuspend(adev->ddev->dev);
1078 * amdgpu_debugfs_regs_gfxoff_status - read gfxoff status
1080 * @f: open file handle
1081 * @buf: User buffer to store read data in
1082 * @size: Number of bytes to read
1083 * @pos: Offset to seek to
1085 static ssize_t amdgpu_debugfs_gfxoff_read(struct file *f, char __user *buf,
1086 size_t size, loff_t *pos)
1088 struct amdgpu_device *adev = file_inode(f)->i_private;
1092 if (size & 0x3 || *pos & 0x3)
1095 r = pm_runtime_get_sync(adev->ddev->dev);
1102 r = amdgpu_get_gfx_off_status(adev, &value);
1104 pm_runtime_mark_last_busy(adev->ddev->dev);
1105 pm_runtime_put_autosuspend(adev->ddev->dev);
1109 r = put_user(value, (uint32_t *)buf);
1111 pm_runtime_mark_last_busy(adev->ddev->dev);
1112 pm_runtime_put_autosuspend(adev->ddev->dev);
1122 pm_runtime_mark_last_busy(adev->ddev->dev);
1123 pm_runtime_put_autosuspend(adev->ddev->dev);
1128 static const struct file_operations amdgpu_debugfs_regs_fops = {
1129 .owner = THIS_MODULE,
1130 .read = amdgpu_debugfs_regs_read,
1131 .write = amdgpu_debugfs_regs_write,
1132 .llseek = default_llseek
1134 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
1135 .owner = THIS_MODULE,
1136 .read = amdgpu_debugfs_regs_didt_read,
1137 .write = amdgpu_debugfs_regs_didt_write,
1138 .llseek = default_llseek
1140 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
1141 .owner = THIS_MODULE,
1142 .read = amdgpu_debugfs_regs_pcie_read,
1143 .write = amdgpu_debugfs_regs_pcie_write,
1144 .llseek = default_llseek
1146 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
1147 .owner = THIS_MODULE,
1148 .read = amdgpu_debugfs_regs_smc_read,
1149 .write = amdgpu_debugfs_regs_smc_write,
1150 .llseek = default_llseek
1153 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
1154 .owner = THIS_MODULE,
1155 .read = amdgpu_debugfs_gca_config_read,
1156 .llseek = default_llseek
1159 static const struct file_operations amdgpu_debugfs_sensors_fops = {
1160 .owner = THIS_MODULE,
1161 .read = amdgpu_debugfs_sensor_read,
1162 .llseek = default_llseek
1165 static const struct file_operations amdgpu_debugfs_wave_fops = {
1166 .owner = THIS_MODULE,
1167 .read = amdgpu_debugfs_wave_read,
1168 .llseek = default_llseek
1170 static const struct file_operations amdgpu_debugfs_gpr_fops = {
1171 .owner = THIS_MODULE,
1172 .read = amdgpu_debugfs_gpr_read,
1173 .llseek = default_llseek
1176 static const struct file_operations amdgpu_debugfs_gfxoff_fops = {
1177 .owner = THIS_MODULE,
1178 .read = amdgpu_debugfs_gfxoff_read,
1179 .write = amdgpu_debugfs_gfxoff_write,
1180 .llseek = default_llseek
1183 static const struct file_operations *debugfs_regs[] = {
1184 &amdgpu_debugfs_regs_fops,
1185 &amdgpu_debugfs_regs_didt_fops,
1186 &amdgpu_debugfs_regs_pcie_fops,
1187 &amdgpu_debugfs_regs_smc_fops,
1188 &amdgpu_debugfs_gca_config_fops,
1189 &amdgpu_debugfs_sensors_fops,
1190 &amdgpu_debugfs_wave_fops,
1191 &amdgpu_debugfs_gpr_fops,
1192 &amdgpu_debugfs_gfxoff_fops,
1195 static const char *debugfs_regs_names[] = {
1200 "amdgpu_gca_config",
1208 * amdgpu_debugfs_regs_init - Initialize debugfs entries that provide
1211 * @adev: The device to attach the debugfs entries to
1213 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
1215 struct drm_minor *minor = adev->ddev->primary;
1216 struct dentry *ent, *root = minor->debugfs_root;
1219 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
1220 ent = debugfs_create_file(debugfs_regs_names[i],
1221 S_IFREG | S_IRUGO, root,
1222 adev, debugfs_regs[i]);
1223 if (!i && !IS_ERR_OR_NULL(ent))
1224 i_size_write(ent->d_inode, adev->rmmio_size);
1225 adev->debugfs_regs[i] = ent;
1231 static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
1233 struct drm_info_node *node = (struct drm_info_node *) m->private;
1234 struct drm_device *dev = node->minor->dev;
1235 struct amdgpu_device *adev = dev->dev_private;
1238 r = pm_runtime_get_sync(dev->dev);
1240 pm_runtime_put_autosuspend(adev->ddev->dev);
1244 /* Avoid accidently unparking the sched thread during GPU reset */
1245 mutex_lock(&adev->lock_reset);
1247 /* hold on the scheduler */
1248 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1249 struct amdgpu_ring *ring = adev->rings[i];
1251 if (!ring || !ring->sched.thread)
1253 kthread_park(ring->sched.thread);
1256 seq_printf(m, "run ib test:\n");
1257 r = amdgpu_ib_ring_tests(adev);
1259 seq_printf(m, "ib ring tests failed (%d).\n", r);
1261 seq_printf(m, "ib ring tests passed.\n");
1263 /* go on the scheduler */
1264 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1265 struct amdgpu_ring *ring = adev->rings[i];
1267 if (!ring || !ring->sched.thread)
1269 kthread_unpark(ring->sched.thread);
1272 mutex_unlock(&adev->lock_reset);
1274 pm_runtime_mark_last_busy(dev->dev);
1275 pm_runtime_put_autosuspend(dev->dev);
1280 static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
1282 struct drm_info_node *node = (struct drm_info_node *) m->private;
1283 struct drm_device *dev = node->minor->dev;
1284 struct amdgpu_device *adev = dev->dev_private;
1286 seq_write(m, adev->bios, adev->bios_size);
1290 static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data)
1292 struct drm_info_node *node = (struct drm_info_node *)m->private;
1293 struct drm_device *dev = node->minor->dev;
1294 struct amdgpu_device *adev = dev->dev_private;
1297 r = pm_runtime_get_sync(dev->dev);
1299 pm_runtime_put_autosuspend(adev->ddev->dev);
1303 seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev));
1305 pm_runtime_mark_last_busy(dev->dev);
1306 pm_runtime_put_autosuspend(dev->dev);
1311 static int amdgpu_debugfs_evict_gtt(struct seq_file *m, void *data)
1313 struct drm_info_node *node = (struct drm_info_node *)m->private;
1314 struct drm_device *dev = node->minor->dev;
1315 struct amdgpu_device *adev = dev->dev_private;
1318 r = pm_runtime_get_sync(dev->dev);
1320 pm_runtime_put_autosuspend(adev->ddev->dev);
1324 seq_printf(m, "(%d)\n", ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_TT));
1326 pm_runtime_mark_last_busy(dev->dev);
1327 pm_runtime_put_autosuspend(dev->dev);
1332 static const struct drm_info_list amdgpu_debugfs_list[] = {
1333 {"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump},
1334 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib},
1335 {"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram},
1336 {"amdgpu_evict_gtt", &amdgpu_debugfs_evict_gtt},
1339 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring,
1340 struct dma_fence **fences)
1342 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1343 uint32_t sync_seq, last_seq;
1345 last_seq = atomic_read(&ring->fence_drv.last_seq);
1346 sync_seq = ring->fence_drv.sync_seq;
1348 last_seq &= drv->num_fences_mask;
1349 sync_seq &= drv->num_fences_mask;
1352 struct dma_fence *fence, **ptr;
1355 last_seq &= drv->num_fences_mask;
1356 ptr = &drv->fences[last_seq];
1358 fence = rcu_dereference_protected(*ptr, 1);
1359 RCU_INIT_POINTER(*ptr, NULL);
1364 fences[last_seq] = fence;
1366 } while (last_seq != sync_seq);
1369 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences,
1373 struct dma_fence *fence;
1375 for (i = 0; i < length; i++) {
1379 dma_fence_signal(fence);
1380 dma_fence_put(fence);
1384 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched)
1386 struct drm_sched_job *s_job;
1387 struct dma_fence *fence;
1389 spin_lock(&sched->job_list_lock);
1390 list_for_each_entry(s_job, &sched->ring_mirror_list, node) {
1391 fence = sched->ops->run_job(s_job);
1392 dma_fence_put(fence);
1394 spin_unlock(&sched->job_list_lock);
1397 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring)
1399 struct amdgpu_job *job;
1400 struct drm_sched_job *s_job, *tmp;
1401 uint32_t preempt_seq;
1402 struct dma_fence *fence, **ptr;
1403 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1404 struct drm_gpu_scheduler *sched = &ring->sched;
1405 bool preempted = true;
1407 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
1410 preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2));
1411 if (preempt_seq <= atomic_read(&drv->last_seq)) {
1416 preempt_seq &= drv->num_fences_mask;
1417 ptr = &drv->fences[preempt_seq];
1418 fence = rcu_dereference_protected(*ptr, 1);
1421 spin_lock(&sched->job_list_lock);
1422 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
1423 if (dma_fence_is_signaled(&s_job->s_fence->finished)) {
1424 /* remove job from ring_mirror_list */
1425 list_del_init(&s_job->node);
1426 sched->ops->free_job(s_job);
1429 job = to_amdgpu_job(s_job);
1430 if (preempted && job->fence == fence)
1431 /* mark the job as preempted */
1432 job->preemption_status |= AMDGPU_IB_PREEMPTED;
1434 spin_unlock(&sched->job_list_lock);
1437 static int amdgpu_debugfs_ib_preempt(void *data, u64 val)
1439 int r, resched, length;
1440 struct amdgpu_ring *ring;
1441 struct dma_fence **fences = NULL;
1442 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1444 if (val >= AMDGPU_MAX_RINGS)
1447 ring = adev->rings[val];
1449 if (!ring || !ring->funcs->preempt_ib || !ring->sched.thread)
1452 /* the last preemption failed */
1453 if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr))
1456 length = ring->fence_drv.num_fences_mask + 1;
1457 fences = kcalloc(length, sizeof(void *), GFP_KERNEL);
1461 /* Avoid accidently unparking the sched thread during GPU reset */
1462 mutex_lock(&adev->lock_reset);
1464 /* stop the scheduler */
1465 kthread_park(ring->sched.thread);
1467 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1469 /* preempt the IB */
1470 r = amdgpu_ring_preempt_ib(ring);
1472 DRM_WARN("failed to preempt ring %d\n", ring->idx);
1476 amdgpu_fence_process(ring);
1478 if (atomic_read(&ring->fence_drv.last_seq) !=
1479 ring->fence_drv.sync_seq) {
1480 DRM_INFO("ring %d was preempted\n", ring->idx);
1482 amdgpu_ib_preempt_mark_partial_job(ring);
1484 /* swap out the old fences */
1485 amdgpu_ib_preempt_fences_swap(ring, fences);
1487 amdgpu_fence_driver_force_completion(ring);
1489 /* resubmit unfinished jobs */
1490 amdgpu_ib_preempt_job_recovery(&ring->sched);
1492 /* wait for jobs finished */
1493 amdgpu_fence_wait_empty(ring);
1495 /* signal the old fences */
1496 amdgpu_ib_preempt_signal_fences(fences, length);
1500 /* restart the scheduler */
1501 kthread_unpark(ring->sched.thread);
1503 mutex_unlock(&adev->lock_reset);
1505 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1512 static int amdgpu_debugfs_sclk_set(void *data, u64 val)
1515 uint32_t max_freq, min_freq;
1516 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1518 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1521 ret = pm_runtime_get_sync(adev->ddev->dev);
1523 pm_runtime_put_autosuspend(adev->ddev->dev);
1527 if (is_support_sw_smu(adev)) {
1528 ret = smu_get_dpm_freq_range(&adev->smu, SMU_SCLK, &min_freq, &max_freq);
1529 if (ret || val > max_freq || val < min_freq)
1531 ret = smu_set_soft_freq_range(&adev->smu, SMU_SCLK, (uint32_t)val, (uint32_t)val);
1536 pm_runtime_mark_last_busy(adev->ddev->dev);
1537 pm_runtime_put_autosuspend(adev->ddev->dev);
1545 DEFINE_SIMPLE_ATTRIBUTE(fops_ib_preempt, NULL,
1546 amdgpu_debugfs_ib_preempt, "%llu\n");
1548 DEFINE_SIMPLE_ATTRIBUTE(fops_sclk_set, NULL,
1549 amdgpu_debugfs_sclk_set, "%llu\n");
1551 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1555 adev->debugfs_preempt =
1556 debugfs_create_file("amdgpu_preempt_ib", 0600,
1557 adev->ddev->primary->debugfs_root, adev,
1559 if (!(adev->debugfs_preempt)) {
1560 DRM_ERROR("unable to create amdgpu_preempt_ib debugsfs file\n");
1564 adev->smu.debugfs_sclk =
1565 debugfs_create_file("amdgpu_force_sclk", 0200,
1566 adev->ddev->primary->debugfs_root, adev,
1568 if (!(adev->smu.debugfs_sclk)) {
1569 DRM_ERROR("unable to create amdgpu_set_sclk debugsfs file\n");
1573 /* Register debugfs entries for amdgpu_ttm */
1574 r = amdgpu_ttm_debugfs_init(adev);
1576 DRM_ERROR("Failed to init debugfs\n");
1580 r = amdgpu_debugfs_pm_init(adev);
1582 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1586 if (amdgpu_debugfs_sa_init(adev)) {
1587 dev_err(adev->dev, "failed to register debugfs file for SA\n");
1590 if (amdgpu_debugfs_fence_init(adev))
1591 dev_err(adev->dev, "fence debugfs file creation failed\n");
1593 r = amdgpu_debugfs_gem_init(adev);
1595 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1597 r = amdgpu_debugfs_regs_init(adev);
1599 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1601 r = amdgpu_debugfs_firmware_init(adev);
1603 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1605 #if defined(CONFIG_DRM_AMD_DC)
1606 if (amdgpu_device_has_dc_support(adev)) {
1607 if (dtn_debugfs_init(adev))
1608 DRM_ERROR("amdgpu: failed initialize dtn debugfs support.\n");
1612 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1613 struct amdgpu_ring *ring = adev->rings[i];
1618 if (amdgpu_debugfs_ring_init(adev, ring)) {
1619 DRM_ERROR("Failed to register debugfs file for rings !\n");
1623 amdgpu_ras_debugfs_create_all(adev);
1625 amdgpu_debugfs_autodump_init(adev);
1627 amdgpu_rap_debugfs_init(adev);
1629 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list,
1630 ARRAY_SIZE(amdgpu_debugfs_list));
1634 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1638 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)