xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_debugfs.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2008 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  * Copyright 2008 Red Hat Inc.
4*b843c749SSergey Zigachev  * Copyright 2009 Jerome Glisse.
5*b843c749SSergey Zigachev  *
6*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
7*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
8*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
9*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
11*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
12*b843c749SSergey Zigachev  *
13*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
14*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
15*b843c749SSergey Zigachev  *
16*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
23*b843c749SSergey Zigachev  *
24*b843c749SSergey Zigachev  */
25*b843c749SSergey Zigachev 
26*b843c749SSergey Zigachev #include <linux/kthread.h>
27*b843c749SSergey Zigachev #include <drm/drmP.h>
28*b843c749SSergey Zigachev #include <linux/debugfs.h>
29*b843c749SSergey Zigachev #include "amdgpu.h"
30*b843c749SSergey Zigachev 
31*b843c749SSergey Zigachev /**
32*b843c749SSergey Zigachev  * amdgpu_debugfs_add_files - Add simple debugfs entries
33*b843c749SSergey Zigachev  *
34*b843c749SSergey Zigachev  * @adev:  Device to attach debugfs entries to
35*b843c749SSergey Zigachev  * @files:  Array of function callbacks that respond to reads
36*b843c749SSergey Zigachev  * @nfiles: Number of callbacks to register
37*b843c749SSergey Zigachev  *
38*b843c749SSergey Zigachev  */
amdgpu_debugfs_add_files(struct amdgpu_device * adev,const struct drm_info_list * files,unsigned nfiles)39*b843c749SSergey Zigachev int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
40*b843c749SSergey Zigachev 			     const struct drm_info_list *files,
41*b843c749SSergey Zigachev 			     unsigned nfiles)
42*b843c749SSergey Zigachev {
43*b843c749SSergey Zigachev 	unsigned i;
44*b843c749SSergey Zigachev 
45*b843c749SSergey Zigachev 	for (i = 0; i < adev->debugfs_count; i++) {
46*b843c749SSergey Zigachev 		if (adev->debugfs[i].files == files) {
47*b843c749SSergey Zigachev 			/* Already registered */
48*b843c749SSergey Zigachev 			return 0;
49*b843c749SSergey Zigachev 		}
50*b843c749SSergey Zigachev 	}
51*b843c749SSergey Zigachev 
52*b843c749SSergey Zigachev 	i = adev->debugfs_count + 1;
53*b843c749SSergey Zigachev 	if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
54*b843c749SSergey Zigachev 		DRM_ERROR("Reached maximum number of debugfs components.\n");
55*b843c749SSergey Zigachev 		DRM_ERROR("Report so we increase "
56*b843c749SSergey Zigachev 			  "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
57*b843c749SSergey Zigachev 		return -EINVAL;
58*b843c749SSergey Zigachev 	}
59*b843c749SSergey Zigachev 	adev->debugfs[adev->debugfs_count].files = files;
60*b843c749SSergey Zigachev 	adev->debugfs[adev->debugfs_count].num_files = nfiles;
61*b843c749SSergey Zigachev 	adev->debugfs_count = i;
62*b843c749SSergey Zigachev #if defined(CONFIG_DEBUG_FS)
63*b843c749SSergey Zigachev 	drm_debugfs_create_files(files, nfiles,
64*b843c749SSergey Zigachev 				 adev->ddev->primary->debugfs_root,
65*b843c749SSergey Zigachev 				 adev->ddev->primary);
66*b843c749SSergey Zigachev #endif
67*b843c749SSergey Zigachev 	return 0;
68*b843c749SSergey Zigachev }
69*b843c749SSergey Zigachev 
70*b843c749SSergey Zigachev #if defined(CONFIG_DEBUG_FS)
71*b843c749SSergey Zigachev 
72*b843c749SSergey Zigachev /**
73*b843c749SSergey Zigachev  * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes
74*b843c749SSergey Zigachev  *
75*b843c749SSergey Zigachev  * @read: True if reading
76*b843c749SSergey Zigachev  * @f: open file handle
77*b843c749SSergey Zigachev  * @buf: User buffer to write/read to
78*b843c749SSergey Zigachev  * @size: Number of bytes to write/read
79*b843c749SSergey Zigachev  * @pos:  Offset to seek to
80*b843c749SSergey Zigachev  *
81*b843c749SSergey Zigachev  * This debugfs entry has special meaning on the offset being sought.
82*b843c749SSergey Zigachev  * Various bits have different meanings:
83*b843c749SSergey Zigachev  *
84*b843c749SSergey Zigachev  * Bit 62:  Indicates a GRBM bank switch is needed
85*b843c749SSergey Zigachev  * Bit 61:  Indicates a SRBM bank switch is needed (implies bit 62 is
86*b843c749SSergey Zigachev  * 			zero)
87*b843c749SSergey Zigachev  * Bits 24..33: The SE or ME selector if needed
88*b843c749SSergey Zigachev  * Bits 34..43: The SH (or SA) or PIPE selector if needed
89*b843c749SSergey Zigachev  * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed
90*b843c749SSergey Zigachev  *
91*b843c749SSergey Zigachev  * Bit 23:  Indicates that the PM power gating lock should be held
92*b843c749SSergey Zigachev  * 			This is necessary to read registers that might be
93*b843c749SSergey Zigachev  * 			unreliable during a power gating transistion.
94*b843c749SSergey Zigachev  *
95*b843c749SSergey Zigachev  * The lower bits are the BYTE offset of the register to read.  This
96*b843c749SSergey Zigachev  * allows reading multiple registers in a single call and having
97*b843c749SSergey Zigachev  * the returned size reflect that.
98*b843c749SSergey Zigachev  */
amdgpu_debugfs_process_reg_op(bool read,struct file * f,char __user * buf,size_t size,loff_t * pos)99*b843c749SSergey Zigachev static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
100*b843c749SSergey Zigachev 		char __user *buf, size_t size, loff_t *pos)
101*b843c749SSergey Zigachev {
102*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
103*b843c749SSergey Zigachev 	ssize_t result = 0;
104*b843c749SSergey Zigachev 	int r;
105*b843c749SSergey Zigachev 	bool pm_pg_lock, use_bank, use_ring;
106*b843c749SSergey Zigachev 	unsigned instance_bank, sh_bank, se_bank, me, pipe, queue;
107*b843c749SSergey Zigachev 
108*b843c749SSergey Zigachev 	pm_pg_lock = use_bank = use_ring = false;
109*b843c749SSergey Zigachev 	instance_bank = sh_bank = se_bank = me = pipe = queue = 0;
110*b843c749SSergey Zigachev 
111*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3 ||
112*b843c749SSergey Zigachev 			((*pos & (1ULL << 62)) && (*pos & (1ULL << 61))))
113*b843c749SSergey Zigachev 		return -EINVAL;
114*b843c749SSergey Zigachev 
115*b843c749SSergey Zigachev 	/* are we reading registers for which a PG lock is necessary? */
116*b843c749SSergey Zigachev 	pm_pg_lock = (*pos >> 23) & 1;
117*b843c749SSergey Zigachev 
118*b843c749SSergey Zigachev 	if (*pos & (1ULL << 62)) {
119*b843c749SSergey Zigachev 		se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
120*b843c749SSergey Zigachev 		sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
121*b843c749SSergey Zigachev 		instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
122*b843c749SSergey Zigachev 
123*b843c749SSergey Zigachev 		if (se_bank == 0x3FF)
124*b843c749SSergey Zigachev 			se_bank = 0xFFFFFFFF;
125*b843c749SSergey Zigachev 		if (sh_bank == 0x3FF)
126*b843c749SSergey Zigachev 			sh_bank = 0xFFFFFFFF;
127*b843c749SSergey Zigachev 		if (instance_bank == 0x3FF)
128*b843c749SSergey Zigachev 			instance_bank = 0xFFFFFFFF;
129*b843c749SSergey Zigachev 		use_bank = 1;
130*b843c749SSergey Zigachev 	} else if (*pos & (1ULL << 61)) {
131*b843c749SSergey Zigachev 
132*b843c749SSergey Zigachev 		me = (*pos & GENMASK_ULL(33, 24)) >> 24;
133*b843c749SSergey Zigachev 		pipe = (*pos & GENMASK_ULL(43, 34)) >> 34;
134*b843c749SSergey Zigachev 		queue = (*pos & GENMASK_ULL(53, 44)) >> 44;
135*b843c749SSergey Zigachev 
136*b843c749SSergey Zigachev 		use_ring = 1;
137*b843c749SSergey Zigachev 	} else {
138*b843c749SSergey Zigachev 		use_bank = use_ring = 0;
139*b843c749SSergey Zigachev 	}
140*b843c749SSergey Zigachev 
141*b843c749SSergey Zigachev 	*pos &= (1UL << 22) - 1;
142*b843c749SSergey Zigachev 
143*b843c749SSergey Zigachev 	if (use_bank) {
144*b843c749SSergey Zigachev 		if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
145*b843c749SSergey Zigachev 		    (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
146*b843c749SSergey Zigachev 			return -EINVAL;
147*b843c749SSergey Zigachev 		mutex_lock(&adev->grbm_idx_mutex);
148*b843c749SSergey Zigachev 		amdgpu_gfx_select_se_sh(adev, se_bank,
149*b843c749SSergey Zigachev 					sh_bank, instance_bank);
150*b843c749SSergey Zigachev 	} else if (use_ring) {
151*b843c749SSergey Zigachev 		mutex_lock(&adev->srbm_mutex);
152*b843c749SSergey Zigachev 		amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue);
153*b843c749SSergey Zigachev 	}
154*b843c749SSergey Zigachev 
155*b843c749SSergey Zigachev 	if (pm_pg_lock)
156*b843c749SSergey Zigachev 		mutex_lock(&adev->pm.mutex);
157*b843c749SSergey Zigachev 
158*b843c749SSergey Zigachev 	while (size) {
159*b843c749SSergey Zigachev 		uint32_t value;
160*b843c749SSergey Zigachev 
161*b843c749SSergey Zigachev 		if (*pos > adev->rmmio_size)
162*b843c749SSergey Zigachev 			goto end;
163*b843c749SSergey Zigachev 
164*b843c749SSergey Zigachev 		if (read) {
165*b843c749SSergey Zigachev 			value = RREG32(*pos >> 2);
166*b843c749SSergey Zigachev 			r = put_user(value, (uint32_t *)buf);
167*b843c749SSergey Zigachev 		} else {
168*b843c749SSergey Zigachev 			r = get_user(value, (uint32_t *)buf);
169*b843c749SSergey Zigachev 			if (!r)
170*b843c749SSergey Zigachev 				WREG32(*pos >> 2, value);
171*b843c749SSergey Zigachev 		}
172*b843c749SSergey Zigachev 		if (r) {
173*b843c749SSergey Zigachev 			result = r;
174*b843c749SSergey Zigachev 			goto end;
175*b843c749SSergey Zigachev 		}
176*b843c749SSergey Zigachev 
177*b843c749SSergey Zigachev 		result += 4;
178*b843c749SSergey Zigachev 		buf += 4;
179*b843c749SSergey Zigachev 		*pos += 4;
180*b843c749SSergey Zigachev 		size -= 4;
181*b843c749SSergey Zigachev 	}
182*b843c749SSergey Zigachev 
183*b843c749SSergey Zigachev end:
184*b843c749SSergey Zigachev 	if (use_bank) {
185*b843c749SSergey Zigachev 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
186*b843c749SSergey Zigachev 		mutex_unlock(&adev->grbm_idx_mutex);
187*b843c749SSergey Zigachev 	} else if (use_ring) {
188*b843c749SSergey Zigachev 		amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0);
189*b843c749SSergey Zigachev 		mutex_unlock(&adev->srbm_mutex);
190*b843c749SSergey Zigachev 	}
191*b843c749SSergey Zigachev 
192*b843c749SSergey Zigachev 	if (pm_pg_lock)
193*b843c749SSergey Zigachev 		mutex_unlock(&adev->pm.mutex);
194*b843c749SSergey Zigachev 
195*b843c749SSergey Zigachev 	return result;
196*b843c749SSergey Zigachev }
197*b843c749SSergey Zigachev 
198*b843c749SSergey Zigachev /**
199*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_read - Callback for reading MMIO registers
200*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_read(struct file * f,char __user * buf,size_t size,loff_t * pos)201*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
202*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
203*b843c749SSergey Zigachev {
204*b843c749SSergey Zigachev 	return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos);
205*b843c749SSergey Zigachev }
206*b843c749SSergey Zigachev 
207*b843c749SSergey Zigachev /**
208*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_write - Callback for writing MMIO registers
209*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)210*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
211*b843c749SSergey Zigachev 					 size_t size, loff_t *pos)
212*b843c749SSergey Zigachev {
213*b843c749SSergey Zigachev 	return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos);
214*b843c749SSergey Zigachev }
215*b843c749SSergey Zigachev 
216*b843c749SSergey Zigachev 
217*b843c749SSergey Zigachev /**
218*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register
219*b843c749SSergey Zigachev  *
220*b843c749SSergey Zigachev  * @f: open file handle
221*b843c749SSergey Zigachev  * @buf: User buffer to store read data in
222*b843c749SSergey Zigachev  * @size: Number of bytes to read
223*b843c749SSergey Zigachev  * @pos:  Offset to seek to
224*b843c749SSergey Zigachev  *
225*b843c749SSergey Zigachev  * The lower bits are the BYTE offset of the register to read.  This
226*b843c749SSergey Zigachev  * allows reading multiple registers in a single call and having
227*b843c749SSergey Zigachev  * the returned size reflect that.
228*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_pcie_read(struct file * f,char __user * buf,size_t size,loff_t * pos)229*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
230*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
231*b843c749SSergey Zigachev {
232*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
233*b843c749SSergey Zigachev 	ssize_t result = 0;
234*b843c749SSergey Zigachev 	int r;
235*b843c749SSergey Zigachev 
236*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
237*b843c749SSergey Zigachev 		return -EINVAL;
238*b843c749SSergey Zigachev 
239*b843c749SSergey Zigachev 	while (size) {
240*b843c749SSergey Zigachev 		uint32_t value;
241*b843c749SSergey Zigachev 
242*b843c749SSergey Zigachev 		value = RREG32_PCIE(*pos);
243*b843c749SSergey Zigachev 		r = put_user(value, (uint32_t *)buf);
244*b843c749SSergey Zigachev 		if (r)
245*b843c749SSergey Zigachev 			return r;
246*b843c749SSergey Zigachev 
247*b843c749SSergey Zigachev 		result += 4;
248*b843c749SSergey Zigachev 		buf += 4;
249*b843c749SSergey Zigachev 		*pos += 4;
250*b843c749SSergey Zigachev 		size -= 4;
251*b843c749SSergey Zigachev 	}
252*b843c749SSergey Zigachev 
253*b843c749SSergey Zigachev 	return result;
254*b843c749SSergey Zigachev }
255*b843c749SSergey Zigachev 
256*b843c749SSergey Zigachev /**
257*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register
258*b843c749SSergey Zigachev  *
259*b843c749SSergey Zigachev  * @f: open file handle
260*b843c749SSergey Zigachev  * @buf: User buffer to write data from
261*b843c749SSergey Zigachev  * @size: Number of bytes to write
262*b843c749SSergey Zigachev  * @pos:  Offset to seek to
263*b843c749SSergey Zigachev  *
264*b843c749SSergey Zigachev  * The lower bits are the BYTE offset of the register to write.  This
265*b843c749SSergey Zigachev  * allows writing multiple registers in a single call and having
266*b843c749SSergey Zigachev  * the returned size reflect that.
267*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_pcie_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)268*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
269*b843c749SSergey Zigachev 					 size_t size, loff_t *pos)
270*b843c749SSergey Zigachev {
271*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
272*b843c749SSergey Zigachev 	ssize_t result = 0;
273*b843c749SSergey Zigachev 	int r;
274*b843c749SSergey Zigachev 
275*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
276*b843c749SSergey Zigachev 		return -EINVAL;
277*b843c749SSergey Zigachev 
278*b843c749SSergey Zigachev 	while (size) {
279*b843c749SSergey Zigachev 		uint32_t value;
280*b843c749SSergey Zigachev 
281*b843c749SSergey Zigachev 		r = get_user(value, (uint32_t *)buf);
282*b843c749SSergey Zigachev 		if (r)
283*b843c749SSergey Zigachev 			return r;
284*b843c749SSergey Zigachev 
285*b843c749SSergey Zigachev 		WREG32_PCIE(*pos, value);
286*b843c749SSergey Zigachev 
287*b843c749SSergey Zigachev 		result += 4;
288*b843c749SSergey Zigachev 		buf += 4;
289*b843c749SSergey Zigachev 		*pos += 4;
290*b843c749SSergey Zigachev 		size -= 4;
291*b843c749SSergey Zigachev 	}
292*b843c749SSergey Zigachev 
293*b843c749SSergey Zigachev 	return result;
294*b843c749SSergey Zigachev }
295*b843c749SSergey Zigachev 
296*b843c749SSergey Zigachev /**
297*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_didt_read - Read from a DIDT register
298*b843c749SSergey Zigachev  *
299*b843c749SSergey Zigachev  * @f: open file handle
300*b843c749SSergey Zigachev  * @buf: User buffer to store read data in
301*b843c749SSergey Zigachev  * @size: Number of bytes to read
302*b843c749SSergey Zigachev  * @pos:  Offset to seek to
303*b843c749SSergey Zigachev  *
304*b843c749SSergey Zigachev  * The lower bits are the BYTE offset of the register to read.  This
305*b843c749SSergey Zigachev  * allows reading multiple registers in a single call and having
306*b843c749SSergey Zigachev  * the returned size reflect that.
307*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_didt_read(struct file * f,char __user * buf,size_t size,loff_t * pos)308*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
309*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
310*b843c749SSergey Zigachev {
311*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
312*b843c749SSergey Zigachev 	ssize_t result = 0;
313*b843c749SSergey Zigachev 	int r;
314*b843c749SSergey Zigachev 
315*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
316*b843c749SSergey Zigachev 		return -EINVAL;
317*b843c749SSergey Zigachev 
318*b843c749SSergey Zigachev 	while (size) {
319*b843c749SSergey Zigachev 		uint32_t value;
320*b843c749SSergey Zigachev 
321*b843c749SSergey Zigachev 		value = RREG32_DIDT(*pos >> 2);
322*b843c749SSergey Zigachev 		r = put_user(value, (uint32_t *)buf);
323*b843c749SSergey Zigachev 		if (r)
324*b843c749SSergey Zigachev 			return r;
325*b843c749SSergey Zigachev 
326*b843c749SSergey Zigachev 		result += 4;
327*b843c749SSergey Zigachev 		buf += 4;
328*b843c749SSergey Zigachev 		*pos += 4;
329*b843c749SSergey Zigachev 		size -= 4;
330*b843c749SSergey Zigachev 	}
331*b843c749SSergey Zigachev 
332*b843c749SSergey Zigachev 	return result;
333*b843c749SSergey Zigachev }
334*b843c749SSergey Zigachev 
335*b843c749SSergey Zigachev /**
336*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_didt_write - Write to a DIDT register
337*b843c749SSergey Zigachev  *
338*b843c749SSergey Zigachev  * @f: open file handle
339*b843c749SSergey Zigachev  * @buf: User buffer to write data from
340*b843c749SSergey Zigachev  * @size: Number of bytes to write
341*b843c749SSergey Zigachev  * @pos:  Offset to seek to
342*b843c749SSergey Zigachev  *
343*b843c749SSergey Zigachev  * The lower bits are the BYTE offset of the register to write.  This
344*b843c749SSergey Zigachev  * allows writing multiple registers in a single call and having
345*b843c749SSergey Zigachev  * the returned size reflect that.
346*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_didt_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)347*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
348*b843c749SSergey Zigachev 					 size_t size, loff_t *pos)
349*b843c749SSergey Zigachev {
350*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
351*b843c749SSergey Zigachev 	ssize_t result = 0;
352*b843c749SSergey Zigachev 	int r;
353*b843c749SSergey Zigachev 
354*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
355*b843c749SSergey Zigachev 		return -EINVAL;
356*b843c749SSergey Zigachev 
357*b843c749SSergey Zigachev 	while (size) {
358*b843c749SSergey Zigachev 		uint32_t value;
359*b843c749SSergey Zigachev 
360*b843c749SSergey Zigachev 		r = get_user(value, (uint32_t *)buf);
361*b843c749SSergey Zigachev 		if (r)
362*b843c749SSergey Zigachev 			return r;
363*b843c749SSergey Zigachev 
364*b843c749SSergey Zigachev 		WREG32_DIDT(*pos >> 2, value);
365*b843c749SSergey Zigachev 
366*b843c749SSergey Zigachev 		result += 4;
367*b843c749SSergey Zigachev 		buf += 4;
368*b843c749SSergey Zigachev 		*pos += 4;
369*b843c749SSergey Zigachev 		size -= 4;
370*b843c749SSergey Zigachev 	}
371*b843c749SSergey Zigachev 
372*b843c749SSergey Zigachev 	return result;
373*b843c749SSergey Zigachev }
374*b843c749SSergey Zigachev 
375*b843c749SSergey Zigachev /**
376*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_smc_read - Read from a SMC register
377*b843c749SSergey Zigachev  *
378*b843c749SSergey Zigachev  * @f: open file handle
379*b843c749SSergey Zigachev  * @buf: User buffer to store read data in
380*b843c749SSergey Zigachev  * @size: Number of bytes to read
381*b843c749SSergey Zigachev  * @pos:  Offset to seek to
382*b843c749SSergey Zigachev  *
383*b843c749SSergey Zigachev  * The lower bits are the BYTE offset of the register to read.  This
384*b843c749SSergey Zigachev  * allows reading multiple registers in a single call and having
385*b843c749SSergey Zigachev  * the returned size reflect that.
386*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_smc_read(struct file * f,char __user * buf,size_t size,loff_t * pos)387*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
388*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
389*b843c749SSergey Zigachev {
390*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
391*b843c749SSergey Zigachev 	ssize_t result = 0;
392*b843c749SSergey Zigachev 	int r;
393*b843c749SSergey Zigachev 
394*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
395*b843c749SSergey Zigachev 		return -EINVAL;
396*b843c749SSergey Zigachev 
397*b843c749SSergey Zigachev 	while (size) {
398*b843c749SSergey Zigachev 		uint32_t value;
399*b843c749SSergey Zigachev 
400*b843c749SSergey Zigachev 		value = RREG32_SMC(*pos);
401*b843c749SSergey Zigachev 		r = put_user(value, (uint32_t *)buf);
402*b843c749SSergey Zigachev 		if (r)
403*b843c749SSergey Zigachev 			return r;
404*b843c749SSergey Zigachev 
405*b843c749SSergey Zigachev 		result += 4;
406*b843c749SSergey Zigachev 		buf += 4;
407*b843c749SSergey Zigachev 		*pos += 4;
408*b843c749SSergey Zigachev 		size -= 4;
409*b843c749SSergey Zigachev 	}
410*b843c749SSergey Zigachev 
411*b843c749SSergey Zigachev 	return result;
412*b843c749SSergey Zigachev }
413*b843c749SSergey Zigachev 
414*b843c749SSergey Zigachev /**
415*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_smc_write - Write to a SMC register
416*b843c749SSergey Zigachev  *
417*b843c749SSergey Zigachev  * @f: open file handle
418*b843c749SSergey Zigachev  * @buf: User buffer to write data from
419*b843c749SSergey Zigachev  * @size: Number of bytes to write
420*b843c749SSergey Zigachev  * @pos:  Offset to seek to
421*b843c749SSergey Zigachev  *
422*b843c749SSergey Zigachev  * The lower bits are the BYTE offset of the register to write.  This
423*b843c749SSergey Zigachev  * allows writing multiple registers in a single call and having
424*b843c749SSergey Zigachev  * the returned size reflect that.
425*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_smc_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)426*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
427*b843c749SSergey Zigachev 					 size_t size, loff_t *pos)
428*b843c749SSergey Zigachev {
429*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
430*b843c749SSergey Zigachev 	ssize_t result = 0;
431*b843c749SSergey Zigachev 	int r;
432*b843c749SSergey Zigachev 
433*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
434*b843c749SSergey Zigachev 		return -EINVAL;
435*b843c749SSergey Zigachev 
436*b843c749SSergey Zigachev 	while (size) {
437*b843c749SSergey Zigachev 		uint32_t value;
438*b843c749SSergey Zigachev 
439*b843c749SSergey Zigachev 		r = get_user(value, (uint32_t *)buf);
440*b843c749SSergey Zigachev 		if (r)
441*b843c749SSergey Zigachev 			return r;
442*b843c749SSergey Zigachev 
443*b843c749SSergey Zigachev 		WREG32_SMC(*pos, value);
444*b843c749SSergey Zigachev 
445*b843c749SSergey Zigachev 		result += 4;
446*b843c749SSergey Zigachev 		buf += 4;
447*b843c749SSergey Zigachev 		*pos += 4;
448*b843c749SSergey Zigachev 		size -= 4;
449*b843c749SSergey Zigachev 	}
450*b843c749SSergey Zigachev 
451*b843c749SSergey Zigachev 	return result;
452*b843c749SSergey Zigachev }
453*b843c749SSergey Zigachev 
454*b843c749SSergey Zigachev /**
455*b843c749SSergey Zigachev  * amdgpu_debugfs_gca_config_read - Read from gfx config data
456*b843c749SSergey Zigachev  *
457*b843c749SSergey Zigachev  * @f: open file handle
458*b843c749SSergey Zigachev  * @buf: User buffer to store read data in
459*b843c749SSergey Zigachev  * @size: Number of bytes to read
460*b843c749SSergey Zigachev  * @pos:  Offset to seek to
461*b843c749SSergey Zigachev  *
462*b843c749SSergey Zigachev  * This file is used to access configuration data in a somewhat
463*b843c749SSergey Zigachev  * stable fashion.  The format is a series of DWORDs with the first
464*b843c749SSergey Zigachev  * indicating which revision it is.  New content is appended to the
465*b843c749SSergey Zigachev  * end so that older software can still read the data.
466*b843c749SSergey Zigachev  */
467*b843c749SSergey Zigachev 
amdgpu_debugfs_gca_config_read(struct file * f,char __user * buf,size_t size,loff_t * pos)468*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
469*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
470*b843c749SSergey Zigachev {
471*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
472*b843c749SSergey Zigachev 	ssize_t result = 0;
473*b843c749SSergey Zigachev 	int r;
474*b843c749SSergey Zigachev 	uint32_t *config, no_regs = 0;
475*b843c749SSergey Zigachev 
476*b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
477*b843c749SSergey Zigachev 		return -EINVAL;
478*b843c749SSergey Zigachev 
479*b843c749SSergey Zigachev 	config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
480*b843c749SSergey Zigachev 	if (!config)
481*b843c749SSergey Zigachev 		return -ENOMEM;
482*b843c749SSergey Zigachev 
483*b843c749SSergey Zigachev 	/* version, increment each time something is added */
484*b843c749SSergey Zigachev 	config[no_regs++] = 3;
485*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_shader_engines;
486*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_tile_pipes;
487*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_cu_per_sh;
488*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_sh_per_se;
489*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_backends_per_se;
490*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
491*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_gprs;
492*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_gs_threads;
493*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.max_hw_contexts;
494*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
495*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
496*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
497*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
498*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.num_tile_pipes;
499*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.backend_enable_mask;
500*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
501*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
502*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
503*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.num_gpus;
504*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
505*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
506*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.gb_addr_config;
507*b843c749SSergey Zigachev 	config[no_regs++] = adev->gfx.config.num_rbs;
508*b843c749SSergey Zigachev 
509*b843c749SSergey Zigachev 	/* rev==1 */
510*b843c749SSergey Zigachev 	config[no_regs++] = adev->rev_id;
511*b843c749SSergey Zigachev 	config[no_regs++] = adev->pg_flags;
512*b843c749SSergey Zigachev 	config[no_regs++] = adev->cg_flags;
513*b843c749SSergey Zigachev 
514*b843c749SSergey Zigachev 	/* rev==2 */
515*b843c749SSergey Zigachev 	config[no_regs++] = adev->family;
516*b843c749SSergey Zigachev 	config[no_regs++] = adev->external_rev_id;
517*b843c749SSergey Zigachev 
518*b843c749SSergey Zigachev 	/* rev==3 */
519*b843c749SSergey Zigachev 	config[no_regs++] = adev->pdev->device;
520*b843c749SSergey Zigachev 	config[no_regs++] = adev->pdev->revision;
521*b843c749SSergey Zigachev 	config[no_regs++] = adev->pdev->subsystem_device;
522*b843c749SSergey Zigachev 	config[no_regs++] = adev->pdev->subsystem_vendor;
523*b843c749SSergey Zigachev 
524*b843c749SSergey Zigachev 	while (size && (*pos < no_regs * 4)) {
525*b843c749SSergey Zigachev 		uint32_t value;
526*b843c749SSergey Zigachev 
527*b843c749SSergey Zigachev 		value = config[*pos >> 2];
528*b843c749SSergey Zigachev 		r = put_user(value, (uint32_t *)buf);
529*b843c749SSergey Zigachev 		if (r) {
530*b843c749SSergey Zigachev 			kfree(config);
531*b843c749SSergey Zigachev 			return r;
532*b843c749SSergey Zigachev 		}
533*b843c749SSergey Zigachev 
534*b843c749SSergey Zigachev 		result += 4;
535*b843c749SSergey Zigachev 		buf += 4;
536*b843c749SSergey Zigachev 		*pos += 4;
537*b843c749SSergey Zigachev 		size -= 4;
538*b843c749SSergey Zigachev 	}
539*b843c749SSergey Zigachev 
540*b843c749SSergey Zigachev 	kfree(config);
541*b843c749SSergey Zigachev 	return result;
542*b843c749SSergey Zigachev }
543*b843c749SSergey Zigachev 
544*b843c749SSergey Zigachev /**
545*b843c749SSergey Zigachev  * amdgpu_debugfs_sensor_read - Read from the powerplay sensors
546*b843c749SSergey Zigachev  *
547*b843c749SSergey Zigachev  * @f: open file handle
548*b843c749SSergey Zigachev  * @buf: User buffer to store read data in
549*b843c749SSergey Zigachev  * @size: Number of bytes to read
550*b843c749SSergey Zigachev  * @pos:  Offset to seek to
551*b843c749SSergey Zigachev  *
552*b843c749SSergey Zigachev  * The offset is treated as the BYTE address of one of the sensors
553*b843c749SSergey Zigachev  * enumerated in amd/include/kgd_pp_interface.h under the
554*b843c749SSergey Zigachev  * 'amd_pp_sensors' enumeration.  For instance to read the UVD VCLK
555*b843c749SSergey Zigachev  * you would use the offset 3 * 4 = 12.
556*b843c749SSergey Zigachev  */
amdgpu_debugfs_sensor_read(struct file * f,char __user * buf,size_t size,loff_t * pos)557*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
558*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
559*b843c749SSergey Zigachev {
560*b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
561*b843c749SSergey Zigachev 	int idx, x, outsize, r, valuesize;
562*b843c749SSergey Zigachev 	uint32_t values[16];
563*b843c749SSergey Zigachev 
564*b843c749SSergey Zigachev 	if (size & 3 || *pos & 0x3)
565*b843c749SSergey Zigachev 		return -EINVAL;
566*b843c749SSergey Zigachev 
567*b843c749SSergey Zigachev 	if (!adev->pm.dpm_enabled)
568*b843c749SSergey Zigachev 		return -EINVAL;
569*b843c749SSergey Zigachev 
570*b843c749SSergey Zigachev 	/* convert offset to sensor number */
571*b843c749SSergey Zigachev 	idx = *pos >> 2;
572*b843c749SSergey Zigachev 
573*b843c749SSergey Zigachev 	valuesize = sizeof(values);
574*b843c749SSergey Zigachev 	if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
575*b843c749SSergey Zigachev 		r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
576*b843c749SSergey Zigachev 	else
577*b843c749SSergey Zigachev 		return -EINVAL;
578*b843c749SSergey Zigachev 
579*b843c749SSergey Zigachev 	if (size > valuesize)
580*b843c749SSergey Zigachev 		return -EINVAL;
581*b843c749SSergey Zigachev 
582*b843c749SSergey Zigachev 	outsize = 0;
583*b843c749SSergey Zigachev 	x = 0;
584*b843c749SSergey Zigachev 	if (!r) {
585*b843c749SSergey Zigachev 		while (size) {
586*b843c749SSergey Zigachev 			r = put_user(values[x++], (int32_t *)buf);
587*b843c749SSergey Zigachev 			buf += 4;
588*b843c749SSergey Zigachev 			size -= 4;
589*b843c749SSergey Zigachev 			outsize += 4;
590*b843c749SSergey Zigachev 		}
591*b843c749SSergey Zigachev 	}
592*b843c749SSergey Zigachev 
593*b843c749SSergey Zigachev 	return !r ? outsize : r;
594*b843c749SSergey Zigachev }
595*b843c749SSergey Zigachev 
596*b843c749SSergey Zigachev /** amdgpu_debugfs_wave_read - Read WAVE STATUS data
597*b843c749SSergey Zigachev  *
598*b843c749SSergey Zigachev  * @f: open file handle
599*b843c749SSergey Zigachev  * @buf: User buffer to store read data in
600*b843c749SSergey Zigachev  * @size: Number of bytes to read
601*b843c749SSergey Zigachev  * @pos:  Offset to seek to
602*b843c749SSergey Zigachev  *
603*b843c749SSergey Zigachev  * The offset being sought changes which wave that the status data
604*b843c749SSergey Zigachev  * will be returned for.  The bits are used as follows:
605*b843c749SSergey Zigachev  *
606*b843c749SSergey Zigachev  * Bits 0..6: 	Byte offset into data
607*b843c749SSergey Zigachev  * Bits 7..14:	SE selector
608*b843c749SSergey Zigachev  * Bits 15..22:	SH/SA selector
609*b843c749SSergey Zigachev  * Bits 23..30: CU/{WGP+SIMD} selector
610*b843c749SSergey Zigachev  * Bits 31..36: WAVE ID selector
611*b843c749SSergey Zigachev  * Bits 37..44: SIMD ID selector
612*b843c749SSergey Zigachev  *
613*b843c749SSergey Zigachev  * The returned data begins with one DWORD of version information
614*b843c749SSergey Zigachev  * Followed by WAVE STATUS registers relevant to the GFX IP version
615*b843c749SSergey Zigachev  * being used.  See gfx_v8_0_read_wave_data() for an example output.
616*b843c749SSergey Zigachev  */
amdgpu_debugfs_wave_read(struct file * f,char __user * buf,size_t size,loff_t * pos)617*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
618*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
619*b843c749SSergey Zigachev {
620*b843c749SSergey Zigachev 	struct amdgpu_device *adev = f->f_inode->i_private;
621*b843c749SSergey Zigachev 	int r, x;
622*b843c749SSergey Zigachev 	ssize_t result=0;
623*b843c749SSergey Zigachev 	uint32_t offset, se, sh, cu, wave, simd, data[32];
624*b843c749SSergey Zigachev 
625*b843c749SSergey Zigachev 	if (size & 3 || *pos & 3)
626*b843c749SSergey Zigachev 		return -EINVAL;
627*b843c749SSergey Zigachev 
628*b843c749SSergey Zigachev 	/* decode offset */
629*b843c749SSergey Zigachev 	offset = (*pos & GENMASK_ULL(6, 0));
630*b843c749SSergey Zigachev 	se = (*pos & GENMASK_ULL(14, 7)) >> 7;
631*b843c749SSergey Zigachev 	sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
632*b843c749SSergey Zigachev 	cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
633*b843c749SSergey Zigachev 	wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
634*b843c749SSergey Zigachev 	simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
635*b843c749SSergey Zigachev 
636*b843c749SSergey Zigachev 	/* switch to the specific se/sh/cu */
637*b843c749SSergey Zigachev 	mutex_lock(&adev->grbm_idx_mutex);
638*b843c749SSergey Zigachev 	amdgpu_gfx_select_se_sh(adev, se, sh, cu);
639*b843c749SSergey Zigachev 
640*b843c749SSergey Zigachev 	x = 0;
641*b843c749SSergey Zigachev 	if (adev->gfx.funcs->read_wave_data)
642*b843c749SSergey Zigachev 		adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
643*b843c749SSergey Zigachev 
644*b843c749SSergey Zigachev 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
645*b843c749SSergey Zigachev 	mutex_unlock(&adev->grbm_idx_mutex);
646*b843c749SSergey Zigachev 
647*b843c749SSergey Zigachev 	if (!x)
648*b843c749SSergey Zigachev 		return -EINVAL;
649*b843c749SSergey Zigachev 
650*b843c749SSergey Zigachev 	while (size && (offset < x * 4)) {
651*b843c749SSergey Zigachev 		uint32_t value;
652*b843c749SSergey Zigachev 
653*b843c749SSergey Zigachev 		value = data[offset >> 2];
654*b843c749SSergey Zigachev 		r = put_user(value, (uint32_t *)buf);
655*b843c749SSergey Zigachev 		if (r)
656*b843c749SSergey Zigachev 			return r;
657*b843c749SSergey Zigachev 
658*b843c749SSergey Zigachev 		result += 4;
659*b843c749SSergey Zigachev 		buf += 4;
660*b843c749SSergey Zigachev 		offset += 4;
661*b843c749SSergey Zigachev 		size -= 4;
662*b843c749SSergey Zigachev 	}
663*b843c749SSergey Zigachev 
664*b843c749SSergey Zigachev 	return result;
665*b843c749SSergey Zigachev }
666*b843c749SSergey Zigachev 
667*b843c749SSergey Zigachev /** amdgpu_debugfs_gpr_read - Read wave gprs
668*b843c749SSergey Zigachev  *
669*b843c749SSergey Zigachev  * @f: open file handle
670*b843c749SSergey Zigachev  * @buf: User buffer to store read data in
671*b843c749SSergey Zigachev  * @size: Number of bytes to read
672*b843c749SSergey Zigachev  * @pos:  Offset to seek to
673*b843c749SSergey Zigachev  *
674*b843c749SSergey Zigachev  * The offset being sought changes which wave that the status data
675*b843c749SSergey Zigachev  * will be returned for.  The bits are used as follows:
676*b843c749SSergey Zigachev  *
677*b843c749SSergey Zigachev  * Bits 0..11:	Byte offset into data
678*b843c749SSergey Zigachev  * Bits 12..19:	SE selector
679*b843c749SSergey Zigachev  * Bits 20..27:	SH/SA selector
680*b843c749SSergey Zigachev  * Bits 28..35: CU/{WGP+SIMD} selector
681*b843c749SSergey Zigachev  * Bits 36..43: WAVE ID selector
682*b843c749SSergey Zigachev  * Bits 37..44: SIMD ID selector
683*b843c749SSergey Zigachev  * Bits 52..59: Thread selector
684*b843c749SSergey Zigachev  * Bits 60..61: Bank selector (VGPR=0,SGPR=1)
685*b843c749SSergey Zigachev  *
686*b843c749SSergey Zigachev  * The return data comes from the SGPR or VGPR register bank for
687*b843c749SSergey Zigachev  * the selected operational unit.
688*b843c749SSergey Zigachev  */
amdgpu_debugfs_gpr_read(struct file * f,char __user * buf,size_t size,loff_t * pos)689*b843c749SSergey Zigachev static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
690*b843c749SSergey Zigachev 					size_t size, loff_t *pos)
691*b843c749SSergey Zigachev {
692*b843c749SSergey Zigachev 	struct amdgpu_device *adev = f->f_inode->i_private;
693*b843c749SSergey Zigachev 	int r;
694*b843c749SSergey Zigachev 	ssize_t result = 0;
695*b843c749SSergey Zigachev 	uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
696*b843c749SSergey Zigachev 
697*b843c749SSergey Zigachev 	if (size > 4096 || size & 3 || *pos & 3)
698*b843c749SSergey Zigachev 		return -EINVAL;
699*b843c749SSergey Zigachev 
700*b843c749SSergey Zigachev 	/* decode offset */
701*b843c749SSergey Zigachev 	offset = (*pos & GENMASK_ULL(11, 0)) >> 2;
702*b843c749SSergey Zigachev 	se = (*pos & GENMASK_ULL(19, 12)) >> 12;
703*b843c749SSergey Zigachev 	sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
704*b843c749SSergey Zigachev 	cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
705*b843c749SSergey Zigachev 	wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
706*b843c749SSergey Zigachev 	simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
707*b843c749SSergey Zigachev 	thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
708*b843c749SSergey Zigachev 	bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
709*b843c749SSergey Zigachev 
710*b843c749SSergey Zigachev 	data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
711*b843c749SSergey Zigachev 	if (!data)
712*b843c749SSergey Zigachev 		return -ENOMEM;
713*b843c749SSergey Zigachev 
714*b843c749SSergey Zigachev 	/* switch to the specific se/sh/cu */
715*b843c749SSergey Zigachev 	mutex_lock(&adev->grbm_idx_mutex);
716*b843c749SSergey Zigachev 	amdgpu_gfx_select_se_sh(adev, se, sh, cu);
717*b843c749SSergey Zigachev 
718*b843c749SSergey Zigachev 	if (bank == 0) {
719*b843c749SSergey Zigachev 		if (adev->gfx.funcs->read_wave_vgprs)
720*b843c749SSergey Zigachev 			adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
721*b843c749SSergey Zigachev 	} else {
722*b843c749SSergey Zigachev 		if (adev->gfx.funcs->read_wave_sgprs)
723*b843c749SSergey Zigachev 			adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
724*b843c749SSergey Zigachev 	}
725*b843c749SSergey Zigachev 
726*b843c749SSergey Zigachev 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
727*b843c749SSergey Zigachev 	mutex_unlock(&adev->grbm_idx_mutex);
728*b843c749SSergey Zigachev 
729*b843c749SSergey Zigachev 	while (size) {
730*b843c749SSergey Zigachev 		uint32_t value;
731*b843c749SSergey Zigachev 
732*b843c749SSergey Zigachev 		value = data[result >> 2];
733*b843c749SSergey Zigachev 		r = put_user(value, (uint32_t *)buf);
734*b843c749SSergey Zigachev 		if (r) {
735*b843c749SSergey Zigachev 			result = r;
736*b843c749SSergey Zigachev 			goto err;
737*b843c749SSergey Zigachev 		}
738*b843c749SSergey Zigachev 
739*b843c749SSergey Zigachev 		result += 4;
740*b843c749SSergey Zigachev 		buf += 4;
741*b843c749SSergey Zigachev 		size -= 4;
742*b843c749SSergey Zigachev 	}
743*b843c749SSergey Zigachev 
744*b843c749SSergey Zigachev err:
745*b843c749SSergey Zigachev 	kfree(data);
746*b843c749SSergey Zigachev 	return result;
747*b843c749SSergey Zigachev }
748*b843c749SSergey Zigachev 
749*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_regs_fops = {
750*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
751*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_regs_read,
752*b843c749SSergey Zigachev 	.write = amdgpu_debugfs_regs_write,
753*b843c749SSergey Zigachev 	.llseek = default_llseek
754*b843c749SSergey Zigachev };
755*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
756*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
757*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_regs_didt_read,
758*b843c749SSergey Zigachev 	.write = amdgpu_debugfs_regs_didt_write,
759*b843c749SSergey Zigachev 	.llseek = default_llseek
760*b843c749SSergey Zigachev };
761*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
762*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
763*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_regs_pcie_read,
764*b843c749SSergey Zigachev 	.write = amdgpu_debugfs_regs_pcie_write,
765*b843c749SSergey Zigachev 	.llseek = default_llseek
766*b843c749SSergey Zigachev };
767*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
768*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
769*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_regs_smc_read,
770*b843c749SSergey Zigachev 	.write = amdgpu_debugfs_regs_smc_write,
771*b843c749SSergey Zigachev 	.llseek = default_llseek
772*b843c749SSergey Zigachev };
773*b843c749SSergey Zigachev 
774*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_gca_config_fops = {
775*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
776*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_gca_config_read,
777*b843c749SSergey Zigachev 	.llseek = default_llseek
778*b843c749SSergey Zigachev };
779*b843c749SSergey Zigachev 
780*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_sensors_fops = {
781*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
782*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_sensor_read,
783*b843c749SSergey Zigachev 	.llseek = default_llseek
784*b843c749SSergey Zigachev };
785*b843c749SSergey Zigachev 
786*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_wave_fops = {
787*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
788*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_wave_read,
789*b843c749SSergey Zigachev 	.llseek = default_llseek
790*b843c749SSergey Zigachev };
791*b843c749SSergey Zigachev static const struct file_operations amdgpu_debugfs_gpr_fops = {
792*b843c749SSergey Zigachev 	.owner = THIS_MODULE,
793*b843c749SSergey Zigachev 	.read = amdgpu_debugfs_gpr_read,
794*b843c749SSergey Zigachev 	.llseek = default_llseek
795*b843c749SSergey Zigachev };
796*b843c749SSergey Zigachev 
797*b843c749SSergey Zigachev static const struct file_operations *debugfs_regs[] = {
798*b843c749SSergey Zigachev 	&amdgpu_debugfs_regs_fops,
799*b843c749SSergey Zigachev 	&amdgpu_debugfs_regs_didt_fops,
800*b843c749SSergey Zigachev 	&amdgpu_debugfs_regs_pcie_fops,
801*b843c749SSergey Zigachev 	&amdgpu_debugfs_regs_smc_fops,
802*b843c749SSergey Zigachev 	&amdgpu_debugfs_gca_config_fops,
803*b843c749SSergey Zigachev 	&amdgpu_debugfs_sensors_fops,
804*b843c749SSergey Zigachev 	&amdgpu_debugfs_wave_fops,
805*b843c749SSergey Zigachev 	&amdgpu_debugfs_gpr_fops,
806*b843c749SSergey Zigachev };
807*b843c749SSergey Zigachev 
808*b843c749SSergey Zigachev static const char *debugfs_regs_names[] = {
809*b843c749SSergey Zigachev 	"amdgpu_regs",
810*b843c749SSergey Zigachev 	"amdgpu_regs_didt",
811*b843c749SSergey Zigachev 	"amdgpu_regs_pcie",
812*b843c749SSergey Zigachev 	"amdgpu_regs_smc",
813*b843c749SSergey Zigachev 	"amdgpu_gca_config",
814*b843c749SSergey Zigachev 	"amdgpu_sensors",
815*b843c749SSergey Zigachev 	"amdgpu_wave",
816*b843c749SSergey Zigachev 	"amdgpu_gpr",
817*b843c749SSergey Zigachev };
818*b843c749SSergey Zigachev 
819*b843c749SSergey Zigachev /**
820*b843c749SSergey Zigachev  * amdgpu_debugfs_regs_init -	Initialize debugfs entries that provide
821*b843c749SSergey Zigachev  * 								register access.
822*b843c749SSergey Zigachev  *
823*b843c749SSergey Zigachev  * @adev: The device to attach the debugfs entries to
824*b843c749SSergey Zigachev  */
amdgpu_debugfs_regs_init(struct amdgpu_device * adev)825*b843c749SSergey Zigachev int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
826*b843c749SSergey Zigachev {
827*b843c749SSergey Zigachev 	struct drm_minor *minor = adev->ddev->primary;
828*b843c749SSergey Zigachev 	struct dentry *ent, *root = minor->debugfs_root;
829*b843c749SSergey Zigachev 	unsigned i, j;
830*b843c749SSergey Zigachev 
831*b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
832*b843c749SSergey Zigachev 		ent = debugfs_create_file(debugfs_regs_names[i],
833*b843c749SSergey Zigachev 					  S_IFREG | S_IRUGO, root,
834*b843c749SSergey Zigachev 					  adev, debugfs_regs[i]);
835*b843c749SSergey Zigachev 		if (IS_ERR(ent)) {
836*b843c749SSergey Zigachev 			for (j = 0; j < i; j++) {
837*b843c749SSergey Zigachev 				debugfs_remove(adev->debugfs_regs[i]);
838*b843c749SSergey Zigachev 				adev->debugfs_regs[i] = NULL;
839*b843c749SSergey Zigachev 			}
840*b843c749SSergey Zigachev 			return PTR_ERR(ent);
841*b843c749SSergey Zigachev 		}
842*b843c749SSergey Zigachev 
843*b843c749SSergey Zigachev 		if (!i)
844*b843c749SSergey Zigachev 			i_size_write(ent->d_inode, adev->rmmio_size);
845*b843c749SSergey Zigachev 		adev->debugfs_regs[i] = ent;
846*b843c749SSergey Zigachev 	}
847*b843c749SSergey Zigachev 
848*b843c749SSergey Zigachev 	return 0;
849*b843c749SSergey Zigachev }
850*b843c749SSergey Zigachev 
amdgpu_debugfs_regs_cleanup(struct amdgpu_device * adev)851*b843c749SSergey Zigachev void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
852*b843c749SSergey Zigachev {
853*b843c749SSergey Zigachev 	unsigned i;
854*b843c749SSergey Zigachev 
855*b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
856*b843c749SSergey Zigachev 		if (adev->debugfs_regs[i]) {
857*b843c749SSergey Zigachev 			debugfs_remove(adev->debugfs_regs[i]);
858*b843c749SSergey Zigachev 			adev->debugfs_regs[i] = NULL;
859*b843c749SSergey Zigachev 		}
860*b843c749SSergey Zigachev 	}
861*b843c749SSergey Zigachev }
862*b843c749SSergey Zigachev 
amdgpu_debugfs_test_ib(struct seq_file * m,void * data)863*b843c749SSergey Zigachev static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
864*b843c749SSergey Zigachev {
865*b843c749SSergey Zigachev 	struct drm_info_node *node = (struct drm_info_node *) m->private;
866*b843c749SSergey Zigachev 	struct drm_device *dev = node->minor->dev;
867*b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
868*b843c749SSergey Zigachev 	int r = 0, i;
869*b843c749SSergey Zigachev 
870*b843c749SSergey Zigachev 	/* hold on the scheduler */
871*b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
872*b843c749SSergey Zigachev 		struct amdgpu_ring *ring = adev->rings[i];
873*b843c749SSergey Zigachev 
874*b843c749SSergey Zigachev 		if (!ring || !ring->sched.thread)
875*b843c749SSergey Zigachev 			continue;
876*b843c749SSergey Zigachev 		kthread_park(ring->sched.thread);
877*b843c749SSergey Zigachev 	}
878*b843c749SSergey Zigachev 
879*b843c749SSergey Zigachev 	seq_printf(m, "run ib test:\n");
880*b843c749SSergey Zigachev 	r = amdgpu_ib_ring_tests(adev);
881*b843c749SSergey Zigachev 	if (r)
882*b843c749SSergey Zigachev 		seq_printf(m, "ib ring tests failed (%d).\n", r);
883*b843c749SSergey Zigachev 	else
884*b843c749SSergey Zigachev 		seq_printf(m, "ib ring tests passed.\n");
885*b843c749SSergey Zigachev 
886*b843c749SSergey Zigachev 	/* go on the scheduler */
887*b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
888*b843c749SSergey Zigachev 		struct amdgpu_ring *ring = adev->rings[i];
889*b843c749SSergey Zigachev 
890*b843c749SSergey Zigachev 		if (!ring || !ring->sched.thread)
891*b843c749SSergey Zigachev 			continue;
892*b843c749SSergey Zigachev 		kthread_unpark(ring->sched.thread);
893*b843c749SSergey Zigachev 	}
894*b843c749SSergey Zigachev 
895*b843c749SSergey Zigachev 	return 0;
896*b843c749SSergey Zigachev }
897*b843c749SSergey Zigachev 
amdgpu_debugfs_get_vbios_dump(struct seq_file * m,void * data)898*b843c749SSergey Zigachev static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
899*b843c749SSergey Zigachev {
900*b843c749SSergey Zigachev 	struct drm_info_node *node = (struct drm_info_node *) m->private;
901*b843c749SSergey Zigachev 	struct drm_device *dev = node->minor->dev;
902*b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
903*b843c749SSergey Zigachev 
904*b843c749SSergey Zigachev 	seq_write(m, adev->bios, adev->bios_size);
905*b843c749SSergey Zigachev 	return 0;
906*b843c749SSergey Zigachev }
907*b843c749SSergey Zigachev 
amdgpu_debugfs_evict_vram(struct seq_file * m,void * data)908*b843c749SSergey Zigachev static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data)
909*b843c749SSergey Zigachev {
910*b843c749SSergey Zigachev 	struct drm_info_node *node = (struct drm_info_node *)m->private;
911*b843c749SSergey Zigachev 	struct drm_device *dev = node->minor->dev;
912*b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
913*b843c749SSergey Zigachev 
914*b843c749SSergey Zigachev 	seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev));
915*b843c749SSergey Zigachev 	return 0;
916*b843c749SSergey Zigachev }
917*b843c749SSergey Zigachev 
amdgpu_debugfs_evict_gtt(struct seq_file * m,void * data)918*b843c749SSergey Zigachev static int amdgpu_debugfs_evict_gtt(struct seq_file *m, void *data)
919*b843c749SSergey Zigachev {
920*b843c749SSergey Zigachev 	struct drm_info_node *node = (struct drm_info_node *)m->private;
921*b843c749SSergey Zigachev 	struct drm_device *dev = node->minor->dev;
922*b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
923*b843c749SSergey Zigachev 
924*b843c749SSergey Zigachev 	seq_printf(m, "(%d)\n", ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_TT));
925*b843c749SSergey Zigachev 	return 0;
926*b843c749SSergey Zigachev }
927*b843c749SSergey Zigachev 
928*b843c749SSergey Zigachev static const struct drm_info_list amdgpu_debugfs_list[] = {
929*b843c749SSergey Zigachev 	{"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump},
930*b843c749SSergey Zigachev 	{"amdgpu_test_ib", &amdgpu_debugfs_test_ib},
931*b843c749SSergey Zigachev 	{"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram},
932*b843c749SSergey Zigachev 	{"amdgpu_evict_gtt", &amdgpu_debugfs_evict_gtt},
933*b843c749SSergey Zigachev };
934*b843c749SSergey Zigachev 
amdgpu_debugfs_init(struct amdgpu_device * adev)935*b843c749SSergey Zigachev int amdgpu_debugfs_init(struct amdgpu_device *adev)
936*b843c749SSergey Zigachev {
937*b843c749SSergey Zigachev 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list,
938*b843c749SSergey Zigachev 					ARRAY_SIZE(amdgpu_debugfs_list));
939*b843c749SSergey Zigachev }
940*b843c749SSergey Zigachev 
941*b843c749SSergey Zigachev #else
amdgpu_debugfs_init(struct amdgpu_device * adev)942*b843c749SSergey Zigachev int amdgpu_debugfs_init(struct amdgpu_device *adev)
943*b843c749SSergey Zigachev {
944*b843c749SSergey Zigachev 	return 0;
945*b843c749SSergey Zigachev }
amdgpu_debugfs_regs_init(struct amdgpu_device * adev)946*b843c749SSergey Zigachev int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
947*b843c749SSergey Zigachev {
948*b843c749SSergey Zigachev 	return 0;
949*b843c749SSergey Zigachev }
amdgpu_debugfs_regs_cleanup(struct amdgpu_device * adev)950*b843c749SSergey Zigachev void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
951*b843c749SSergey Zigachev #endif
952