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>
31 #include <drm/drm_debugfs.h>
36 * amdgpu_debugfs_add_files - Add simple debugfs entries
38 * @adev: Device to attach debugfs entries to
39 * @files: Array of function callbacks that respond to reads
40 * @nfiles: Number of callbacks to register
43 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
44 const struct drm_info_list *files,
49 for (i = 0; i < adev->debugfs_count; i++) {
50 if (adev->debugfs[i].files == files) {
51 /* Already registered */
56 i = adev->debugfs_count + 1;
57 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
58 DRM_ERROR("Reached maximum number of debugfs components.\n");
59 DRM_ERROR("Report so we increase "
60 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
63 adev->debugfs[adev->debugfs_count].files = files;
64 adev->debugfs[adev->debugfs_count].num_files = nfiles;
65 adev->debugfs_count = i;
66 #if defined(CONFIG_DEBUG_FS)
67 drm_debugfs_create_files(files, nfiles,
68 adev->ddev->primary->debugfs_root,
74 #if defined(CONFIG_DEBUG_FS)
77 * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes
79 * @read: True if reading
80 * @f: open file handle
81 * @buf: User buffer to write/read to
82 * @size: Number of bytes to write/read
83 * @pos: Offset to seek to
85 * This debugfs entry has special meaning on the offset being sought.
86 * Various bits have different meanings:
88 * Bit 62: Indicates a GRBM bank switch is needed
89 * Bit 61: Indicates a SRBM bank switch is needed (implies bit 62 is
91 * Bits 24..33: The SE or ME selector if needed
92 * Bits 34..43: The SH (or SA) or PIPE selector if needed
93 * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed
95 * Bit 23: Indicates that the PM power gating lock should be held
96 * This is necessary to read registers that might be
97 * unreliable during a power gating transistion.
99 * The lower bits are the BYTE offset of the register to read. This
100 * allows reading multiple registers in a single call and having
101 * the returned size reflect that.
103 static int amdgpu_debugfs_process_reg_op(bool read, struct file *f,
104 char __user *buf, size_t size, loff_t *pos)
106 struct amdgpu_device *adev = file_inode(f)->i_private;
109 bool pm_pg_lock, use_bank, use_ring;
110 unsigned instance_bank, sh_bank, se_bank, me, pipe, queue, vmid;
112 pm_pg_lock = use_bank = use_ring = false;
113 instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0;
115 if (size & 0x3 || *pos & 0x3 ||
116 ((*pos & (1ULL << 62)) && (*pos & (1ULL << 61))))
119 /* are we reading registers for which a PG lock is necessary? */
120 pm_pg_lock = (*pos >> 23) & 1;
122 if (*pos & (1ULL << 62)) {
123 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
124 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
125 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
127 if (se_bank == 0x3FF)
128 se_bank = 0xFFFFFFFF;
129 if (sh_bank == 0x3FF)
130 sh_bank = 0xFFFFFFFF;
131 if (instance_bank == 0x3FF)
132 instance_bank = 0xFFFFFFFF;
134 } else if (*pos & (1ULL << 61)) {
136 me = (*pos & GENMASK_ULL(33, 24)) >> 24;
137 pipe = (*pos & GENMASK_ULL(43, 34)) >> 34;
138 queue = (*pos & GENMASK_ULL(53, 44)) >> 44;
139 vmid = (*pos & GENMASK_ULL(58, 54)) >> 54;
143 use_bank = use_ring = false;
146 *pos &= (1UL << 22) - 1;
148 r = pm_runtime_get_sync(adev->ddev->dev);
153 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
154 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) {
155 pm_runtime_mark_last_busy(adev->ddev->dev);
156 pm_runtime_put_autosuspend(adev->ddev->dev);
159 mutex_lock(&adev->grbm_idx_mutex);
160 amdgpu_gfx_select_se_sh(adev, se_bank,
161 sh_bank, instance_bank);
162 } else if (use_ring) {
163 mutex_lock(&adev->srbm_mutex);
164 amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid);
168 mutex_lock(&adev->pm.mutex);
174 value = RREG32(*pos >> 2);
175 r = put_user(value, (uint32_t *)buf);
177 r = get_user(value, (uint32_t *)buf);
179 WREG32(*pos >> 2, value);
194 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
195 mutex_unlock(&adev->grbm_idx_mutex);
196 } else if (use_ring) {
197 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0);
198 mutex_unlock(&adev->srbm_mutex);
202 mutex_unlock(&adev->pm.mutex);
204 pm_runtime_mark_last_busy(adev->ddev->dev);
205 pm_runtime_put_autosuspend(adev->ddev->dev);
211 * amdgpu_debugfs_regs_read - Callback for reading MMIO registers
213 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
214 size_t size, loff_t *pos)
216 return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos);
220 * amdgpu_debugfs_regs_write - Callback for writing MMIO registers
222 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
223 size_t size, loff_t *pos)
225 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos);
230 * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register
232 * @f: open file handle
233 * @buf: User buffer to store read data in
234 * @size: Number of bytes to read
235 * @pos: Offset to seek to
237 * The lower bits are the BYTE offset of the register to read. This
238 * allows reading multiple registers in a single call and having
239 * the returned size reflect that.
241 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
242 size_t size, loff_t *pos)
244 struct amdgpu_device *adev = file_inode(f)->i_private;
248 if (size & 0x3 || *pos & 0x3)
251 r = pm_runtime_get_sync(adev->ddev->dev);
258 value = RREG32_PCIE(*pos >> 2);
259 r = put_user(value, (uint32_t *)buf);
261 pm_runtime_mark_last_busy(adev->ddev->dev);
262 pm_runtime_put_autosuspend(adev->ddev->dev);
272 pm_runtime_mark_last_busy(adev->ddev->dev);
273 pm_runtime_put_autosuspend(adev->ddev->dev);
279 * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register
281 * @f: open file handle
282 * @buf: User buffer to write data from
283 * @size: Number of bytes to write
284 * @pos: Offset to seek to
286 * The lower bits are the BYTE offset of the register to write. This
287 * allows writing multiple registers in a single call and having
288 * the returned size reflect that.
290 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
291 size_t size, loff_t *pos)
293 struct amdgpu_device *adev = file_inode(f)->i_private;
297 if (size & 0x3 || *pos & 0x3)
300 r = pm_runtime_get_sync(adev->ddev->dev);
307 r = get_user(value, (uint32_t *)buf);
309 pm_runtime_mark_last_busy(adev->ddev->dev);
310 pm_runtime_put_autosuspend(adev->ddev->dev);
314 WREG32_PCIE(*pos >> 2, value);
322 pm_runtime_mark_last_busy(adev->ddev->dev);
323 pm_runtime_put_autosuspend(adev->ddev->dev);
329 * amdgpu_debugfs_regs_didt_read - Read from a DIDT register
331 * @f: open file handle
332 * @buf: User buffer to store read data in
333 * @size: Number of bytes to read
334 * @pos: Offset to seek to
336 * The lower bits are the BYTE offset of the register to read. This
337 * allows reading multiple registers in a single call and having
338 * the returned size reflect that.
340 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
341 size_t size, loff_t *pos)
343 struct amdgpu_device *adev = file_inode(f)->i_private;
347 if (size & 0x3 || *pos & 0x3)
350 r = pm_runtime_get_sync(adev->ddev->dev);
357 value = RREG32_DIDT(*pos >> 2);
358 r = put_user(value, (uint32_t *)buf);
360 pm_runtime_mark_last_busy(adev->ddev->dev);
361 pm_runtime_put_autosuspend(adev->ddev->dev);
371 pm_runtime_mark_last_busy(adev->ddev->dev);
372 pm_runtime_put_autosuspend(adev->ddev->dev);
378 * amdgpu_debugfs_regs_didt_write - Write to a DIDT register
380 * @f: open file handle
381 * @buf: User buffer to write data from
382 * @size: Number of bytes to write
383 * @pos: Offset to seek to
385 * The lower bits are the BYTE offset of the register to write. This
386 * allows writing multiple registers in a single call and having
387 * the returned size reflect that.
389 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
390 size_t size, loff_t *pos)
392 struct amdgpu_device *adev = file_inode(f)->i_private;
396 if (size & 0x3 || *pos & 0x3)
399 r = pm_runtime_get_sync(adev->ddev->dev);
406 r = get_user(value, (uint32_t *)buf);
408 pm_runtime_mark_last_busy(adev->ddev->dev);
409 pm_runtime_put_autosuspend(adev->ddev->dev);
413 WREG32_DIDT(*pos >> 2, value);
421 pm_runtime_mark_last_busy(adev->ddev->dev);
422 pm_runtime_put_autosuspend(adev->ddev->dev);
428 * amdgpu_debugfs_regs_smc_read - Read from a SMC register
430 * @f: open file handle
431 * @buf: User buffer to store read data in
432 * @size: Number of bytes to read
433 * @pos: Offset to seek to
435 * The lower bits are the BYTE offset of the register to read. This
436 * allows reading multiple registers in a single call and having
437 * the returned size reflect that.
439 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
440 size_t size, loff_t *pos)
442 struct amdgpu_device *adev = file_inode(f)->i_private;
446 if (size & 0x3 || *pos & 0x3)
449 r = pm_runtime_get_sync(adev->ddev->dev);
456 value = RREG32_SMC(*pos);
457 r = put_user(value, (uint32_t *)buf);
459 pm_runtime_mark_last_busy(adev->ddev->dev);
460 pm_runtime_put_autosuspend(adev->ddev->dev);
470 pm_runtime_mark_last_busy(adev->ddev->dev);
471 pm_runtime_put_autosuspend(adev->ddev->dev);
477 * amdgpu_debugfs_regs_smc_write - Write to a SMC register
479 * @f: open file handle
480 * @buf: User buffer to write data from
481 * @size: Number of bytes to write
482 * @pos: Offset to seek to
484 * The lower bits are the BYTE offset of the register to write. This
485 * allows writing multiple registers in a single call and having
486 * the returned size reflect that.
488 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
489 size_t size, loff_t *pos)
491 struct amdgpu_device *adev = file_inode(f)->i_private;
495 if (size & 0x3 || *pos & 0x3)
498 r = pm_runtime_get_sync(adev->ddev->dev);
505 r = get_user(value, (uint32_t *)buf);
507 pm_runtime_mark_last_busy(adev->ddev->dev);
508 pm_runtime_put_autosuspend(adev->ddev->dev);
512 WREG32_SMC(*pos, value);
520 pm_runtime_mark_last_busy(adev->ddev->dev);
521 pm_runtime_put_autosuspend(adev->ddev->dev);
527 * amdgpu_debugfs_gca_config_read - Read from gfx config data
529 * @f: open file handle
530 * @buf: User buffer to store read data in
531 * @size: Number of bytes to read
532 * @pos: Offset to seek to
534 * This file is used to access configuration data in a somewhat
535 * stable fashion. The format is a series of DWORDs with the first
536 * indicating which revision it is. New content is appended to the
537 * end so that older software can still read the data.
540 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
541 size_t size, loff_t *pos)
543 struct amdgpu_device *adev = file_inode(f)->i_private;
546 uint32_t *config, no_regs = 0;
548 if (size & 0x3 || *pos & 0x3)
551 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
555 /* version, increment each time something is added */
556 config[no_regs++] = 3;
557 config[no_regs++] = adev->gfx.config.max_shader_engines;
558 config[no_regs++] = adev->gfx.config.max_tile_pipes;
559 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
560 config[no_regs++] = adev->gfx.config.max_sh_per_se;
561 config[no_regs++] = adev->gfx.config.max_backends_per_se;
562 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
563 config[no_regs++] = adev->gfx.config.max_gprs;
564 config[no_regs++] = adev->gfx.config.max_gs_threads;
565 config[no_regs++] = adev->gfx.config.max_hw_contexts;
566 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
567 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
568 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
569 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
570 config[no_regs++] = adev->gfx.config.num_tile_pipes;
571 config[no_regs++] = adev->gfx.config.backend_enable_mask;
572 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
573 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
574 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
575 config[no_regs++] = adev->gfx.config.num_gpus;
576 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
577 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
578 config[no_regs++] = adev->gfx.config.gb_addr_config;
579 config[no_regs++] = adev->gfx.config.num_rbs;
582 config[no_regs++] = adev->rev_id;
583 config[no_regs++] = adev->pg_flags;
584 config[no_regs++] = adev->cg_flags;
587 config[no_regs++] = adev->family;
588 config[no_regs++] = adev->external_rev_id;
591 config[no_regs++] = adev->pdev->device;
592 config[no_regs++] = adev->pdev->revision;
593 config[no_regs++] = adev->pdev->subsystem_device;
594 config[no_regs++] = adev->pdev->subsystem_vendor;
596 while (size && (*pos < no_regs * 4)) {
599 value = config[*pos >> 2];
600 r = put_user(value, (uint32_t *)buf);
617 * amdgpu_debugfs_sensor_read - Read from the powerplay sensors
619 * @f: open file handle
620 * @buf: User buffer to store read data in
621 * @size: Number of bytes to read
622 * @pos: Offset to seek to
624 * The offset is treated as the BYTE address of one of the sensors
625 * enumerated in amd/include/kgd_pp_interface.h under the
626 * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK
627 * you would use the offset 3 * 4 = 12.
629 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
630 size_t size, loff_t *pos)
632 struct amdgpu_device *adev = file_inode(f)->i_private;
633 int idx, x, outsize, r, valuesize;
636 if (size & 3 || *pos & 0x3)
639 if (!adev->pm.dpm_enabled)
642 /* convert offset to sensor number */
645 valuesize = sizeof(values);
647 r = pm_runtime_get_sync(adev->ddev->dev);
651 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
653 pm_runtime_mark_last_busy(adev->ddev->dev);
654 pm_runtime_put_autosuspend(adev->ddev->dev);
659 if (size > valuesize)
666 r = put_user(values[x++], (int32_t *)buf);
673 return !r ? outsize : r;
676 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data
678 * @f: open file handle
679 * @buf: User buffer to store read data in
680 * @size: Number of bytes to read
681 * @pos: Offset to seek to
683 * The offset being sought changes which wave that the status data
684 * will be returned for. The bits are used as follows:
686 * Bits 0..6: Byte offset into data
687 * Bits 7..14: SE selector
688 * Bits 15..22: SH/SA selector
689 * Bits 23..30: CU/{WGP+SIMD} selector
690 * Bits 31..36: WAVE ID selector
691 * Bits 37..44: SIMD ID selector
693 * The returned data begins with one DWORD of version information
694 * Followed by WAVE STATUS registers relevant to the GFX IP version
695 * being used. See gfx_v8_0_read_wave_data() for an example output.
697 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
698 size_t size, loff_t *pos)
700 struct amdgpu_device *adev = f->f_inode->i_private;
703 uint32_t offset, se, sh, cu, wave, simd, data[32];
705 if (size & 3 || *pos & 3)
709 offset = (*pos & GENMASK_ULL(6, 0));
710 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
711 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
712 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
713 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
714 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
716 r = pm_runtime_get_sync(adev->ddev->dev);
720 /* switch to the specific se/sh/cu */
721 mutex_lock(&adev->grbm_idx_mutex);
722 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
725 if (adev->gfx.funcs->read_wave_data)
726 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
728 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
729 mutex_unlock(&adev->grbm_idx_mutex);
731 pm_runtime_mark_last_busy(adev->ddev->dev);
732 pm_runtime_put_autosuspend(adev->ddev->dev);
737 while (size && (offset < x * 4)) {
740 value = data[offset >> 2];
741 r = put_user(value, (uint32_t *)buf);
754 /** amdgpu_debugfs_gpr_read - Read wave gprs
756 * @f: open file handle
757 * @buf: User buffer to store read data in
758 * @size: Number of bytes to read
759 * @pos: Offset to seek to
761 * The offset being sought changes which wave that the status data
762 * will be returned for. The bits are used as follows:
764 * Bits 0..11: Byte offset into data
765 * Bits 12..19: SE selector
766 * Bits 20..27: SH/SA selector
767 * Bits 28..35: CU/{WGP+SIMD} selector
768 * Bits 36..43: WAVE ID selector
769 * Bits 37..44: SIMD ID selector
770 * Bits 52..59: Thread selector
771 * Bits 60..61: Bank selector (VGPR=0,SGPR=1)
773 * The return data comes from the SGPR or VGPR register bank for
774 * the selected operational unit.
776 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
777 size_t size, loff_t *pos)
779 struct amdgpu_device *adev = f->f_inode->i_private;
782 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
784 if (size & 3 || *pos & 3)
788 offset = *pos & GENMASK_ULL(11, 0);
789 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
790 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
791 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
792 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
793 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
794 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
795 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
797 data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
801 r = pm_runtime_get_sync(adev->ddev->dev);
805 /* switch to the specific se/sh/cu */
806 mutex_lock(&adev->grbm_idx_mutex);
807 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
810 if (adev->gfx.funcs->read_wave_vgprs)
811 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
813 if (adev->gfx.funcs->read_wave_sgprs)
814 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
817 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
818 mutex_unlock(&adev->grbm_idx_mutex);
820 pm_runtime_mark_last_busy(adev->ddev->dev);
821 pm_runtime_put_autosuspend(adev->ddev->dev);
826 value = data[offset++];
827 r = put_user(value, (uint32_t *)buf);
843 static const struct file_operations amdgpu_debugfs_regs_fops = {
844 .owner = THIS_MODULE,
845 .read = amdgpu_debugfs_regs_read,
846 .write = amdgpu_debugfs_regs_write,
847 .llseek = default_llseek
849 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
850 .owner = THIS_MODULE,
851 .read = amdgpu_debugfs_regs_didt_read,
852 .write = amdgpu_debugfs_regs_didt_write,
853 .llseek = default_llseek
855 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
856 .owner = THIS_MODULE,
857 .read = amdgpu_debugfs_regs_pcie_read,
858 .write = amdgpu_debugfs_regs_pcie_write,
859 .llseek = default_llseek
861 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
862 .owner = THIS_MODULE,
863 .read = amdgpu_debugfs_regs_smc_read,
864 .write = amdgpu_debugfs_regs_smc_write,
865 .llseek = default_llseek
868 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
869 .owner = THIS_MODULE,
870 .read = amdgpu_debugfs_gca_config_read,
871 .llseek = default_llseek
874 static const struct file_operations amdgpu_debugfs_sensors_fops = {
875 .owner = THIS_MODULE,
876 .read = amdgpu_debugfs_sensor_read,
877 .llseek = default_llseek
880 static const struct file_operations amdgpu_debugfs_wave_fops = {
881 .owner = THIS_MODULE,
882 .read = amdgpu_debugfs_wave_read,
883 .llseek = default_llseek
885 static const struct file_operations amdgpu_debugfs_gpr_fops = {
886 .owner = THIS_MODULE,
887 .read = amdgpu_debugfs_gpr_read,
888 .llseek = default_llseek
891 static const struct file_operations *debugfs_regs[] = {
892 &amdgpu_debugfs_regs_fops,
893 &amdgpu_debugfs_regs_didt_fops,
894 &amdgpu_debugfs_regs_pcie_fops,
895 &amdgpu_debugfs_regs_smc_fops,
896 &amdgpu_debugfs_gca_config_fops,
897 &amdgpu_debugfs_sensors_fops,
898 &amdgpu_debugfs_wave_fops,
899 &amdgpu_debugfs_gpr_fops,
902 static const char *debugfs_regs_names[] = {
914 * amdgpu_debugfs_regs_init - Initialize debugfs entries that provide
917 * @adev: The device to attach the debugfs entries to
919 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
921 struct drm_minor *minor = adev->ddev->primary;
922 struct dentry *ent, *root = minor->debugfs_root;
925 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
926 ent = debugfs_create_file(debugfs_regs_names[i],
927 S_IFREG | S_IRUGO, root,
928 adev, debugfs_regs[i]);
929 if (!i && !IS_ERR_OR_NULL(ent))
930 i_size_write(ent->d_inode, adev->rmmio_size);
931 adev->debugfs_regs[i] = ent;
937 void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
941 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
942 if (adev->debugfs_regs[i]) {
943 debugfs_remove(adev->debugfs_regs[i]);
944 adev->debugfs_regs[i] = NULL;
949 static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
951 struct drm_info_node *node = (struct drm_info_node *) m->private;
952 struct drm_device *dev = node->minor->dev;
953 struct amdgpu_device *adev = dev->dev_private;
956 r = pm_runtime_get_sync(dev->dev);
960 /* Avoid accidently unparking the sched thread during GPU reset */
961 mutex_lock(&adev->lock_reset);
963 /* hold on the scheduler */
964 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
965 struct amdgpu_ring *ring = adev->rings[i];
967 if (!ring || !ring->sched.thread)
969 kthread_park(ring->sched.thread);
972 seq_printf(m, "run ib test:\n");
973 r = amdgpu_ib_ring_tests(adev);
975 seq_printf(m, "ib ring tests failed (%d).\n", r);
977 seq_printf(m, "ib ring tests passed.\n");
979 /* go on the scheduler */
980 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
981 struct amdgpu_ring *ring = adev->rings[i];
983 if (!ring || !ring->sched.thread)
985 kthread_unpark(ring->sched.thread);
988 mutex_unlock(&adev->lock_reset);
990 pm_runtime_mark_last_busy(dev->dev);
991 pm_runtime_put_autosuspend(dev->dev);
996 static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
998 struct drm_info_node *node = (struct drm_info_node *) m->private;
999 struct drm_device *dev = node->minor->dev;
1000 struct amdgpu_device *adev = dev->dev_private;
1002 seq_write(m, adev->bios, adev->bios_size);
1006 static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data)
1008 struct drm_info_node *node = (struct drm_info_node *)m->private;
1009 struct drm_device *dev = node->minor->dev;
1010 struct amdgpu_device *adev = dev->dev_private;
1013 r = pm_runtime_get_sync(dev->dev);
1017 seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev));
1019 pm_runtime_mark_last_busy(dev->dev);
1020 pm_runtime_put_autosuspend(dev->dev);
1025 static int amdgpu_debugfs_evict_gtt(struct seq_file *m, void *data)
1027 struct drm_info_node *node = (struct drm_info_node *)m->private;
1028 struct drm_device *dev = node->minor->dev;
1029 struct amdgpu_device *adev = dev->dev_private;
1032 r = pm_runtime_get_sync(dev->dev);
1036 seq_printf(m, "(%d)\n", ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_TT));
1038 pm_runtime_mark_last_busy(dev->dev);
1039 pm_runtime_put_autosuspend(dev->dev);
1044 static const struct drm_info_list amdgpu_debugfs_list[] = {
1045 {"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump},
1046 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib},
1047 {"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram},
1048 {"amdgpu_evict_gtt", &amdgpu_debugfs_evict_gtt},
1051 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring,
1052 struct dma_fence **fences)
1054 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1055 uint32_t sync_seq, last_seq;
1057 last_seq = atomic_read(&ring->fence_drv.last_seq);
1058 sync_seq = ring->fence_drv.sync_seq;
1060 last_seq &= drv->num_fences_mask;
1061 sync_seq &= drv->num_fences_mask;
1064 struct dma_fence *fence, **ptr;
1067 last_seq &= drv->num_fences_mask;
1068 ptr = &drv->fences[last_seq];
1070 fence = rcu_dereference_protected(*ptr, 1);
1071 RCU_INIT_POINTER(*ptr, NULL);
1076 fences[last_seq] = fence;
1078 } while (last_seq != sync_seq);
1081 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences,
1085 struct dma_fence *fence;
1087 for (i = 0; i < length; i++) {
1091 dma_fence_signal(fence);
1092 dma_fence_put(fence);
1096 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched)
1098 struct drm_sched_job *s_job;
1099 struct dma_fence *fence;
1101 spin_lock(&sched->job_list_lock);
1102 list_for_each_entry(s_job, &sched->ring_mirror_list, node) {
1103 fence = sched->ops->run_job(s_job);
1104 dma_fence_put(fence);
1106 spin_unlock(&sched->job_list_lock);
1109 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring)
1111 struct amdgpu_job *job;
1112 struct drm_sched_job *s_job;
1113 uint32_t preempt_seq;
1114 struct dma_fence *fence, **ptr;
1115 struct amdgpu_fence_driver *drv = &ring->fence_drv;
1116 struct drm_gpu_scheduler *sched = &ring->sched;
1118 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
1121 preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2));
1122 if (preempt_seq <= atomic_read(&drv->last_seq))
1125 preempt_seq &= drv->num_fences_mask;
1126 ptr = &drv->fences[preempt_seq];
1127 fence = rcu_dereference_protected(*ptr, 1);
1129 spin_lock(&sched->job_list_lock);
1130 list_for_each_entry(s_job, &sched->ring_mirror_list, node) {
1131 job = to_amdgpu_job(s_job);
1132 if (job->fence == fence)
1133 /* mark the job as preempted */
1134 job->preemption_status |= AMDGPU_IB_PREEMPTED;
1136 spin_unlock(&sched->job_list_lock);
1139 static int amdgpu_debugfs_ib_preempt(void *data, u64 val)
1141 int r, resched, length;
1142 struct amdgpu_ring *ring;
1143 struct dma_fence **fences = NULL;
1144 struct amdgpu_device *adev = (struct amdgpu_device *)data;
1146 if (val >= AMDGPU_MAX_RINGS)
1149 ring = adev->rings[val];
1151 if (!ring || !ring->funcs->preempt_ib || !ring->sched.thread)
1154 /* the last preemption failed */
1155 if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr))
1158 length = ring->fence_drv.num_fences_mask + 1;
1159 fences = kcalloc(length, sizeof(void *), GFP_KERNEL);
1163 /* Avoid accidently unparking the sched thread during GPU reset */
1164 mutex_lock(&adev->lock_reset);
1166 /* stop the scheduler */
1167 kthread_park(ring->sched.thread);
1169 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1171 /* preempt the IB */
1172 r = amdgpu_ring_preempt_ib(ring);
1174 DRM_WARN("failed to preempt ring %d\n", ring->idx);
1178 amdgpu_fence_process(ring);
1180 if (atomic_read(&ring->fence_drv.last_seq) !=
1181 ring->fence_drv.sync_seq) {
1182 DRM_INFO("ring %d was preempted\n", ring->idx);
1184 amdgpu_ib_preempt_mark_partial_job(ring);
1186 /* swap out the old fences */
1187 amdgpu_ib_preempt_fences_swap(ring, fences);
1189 amdgpu_fence_driver_force_completion(ring);
1191 /* resubmit unfinished jobs */
1192 amdgpu_ib_preempt_job_recovery(&ring->sched);
1194 /* wait for jobs finished */
1195 amdgpu_fence_wait_empty(ring);
1197 /* signal the old fences */
1198 amdgpu_ib_preempt_signal_fences(fences, length);
1202 /* restart the scheduler */
1203 kthread_unpark(ring->sched.thread);
1205 mutex_unlock(&adev->lock_reset);
1207 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1214 DEFINE_SIMPLE_ATTRIBUTE(fops_ib_preempt, NULL,
1215 amdgpu_debugfs_ib_preempt, "%llu\n");
1217 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1219 adev->debugfs_preempt =
1220 debugfs_create_file("amdgpu_preempt_ib", 0600,
1221 adev->ddev->primary->debugfs_root, adev,
1223 if (!(adev->debugfs_preempt)) {
1224 DRM_ERROR("unable to create amdgpu_preempt_ib debugsfs file\n");
1228 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list,
1229 ARRAY_SIZE(amdgpu_debugfs_list));
1232 void amdgpu_debugfs_preempt_cleanup(struct amdgpu_device *adev)
1234 debugfs_remove(adev->debugfs_preempt);
1238 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1242 void amdgpu_debugfs_preempt_cleanup(struct amdgpu_device *adev) { }
1243 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
1247 void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }