xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c (revision fc2591175507709191c2010a7eb466837496750d)
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  *
24  */
25 
26 #include <linux/kthread.h>
27 #include <linux/pci.h>
28 #include <linux/uaccess.h>
29 #include <linux/pm_runtime.h>
30 
31 #include "amdgpu.h"
32 #include "amdgpu_pm.h"
33 #include "amdgpu_dm_debugfs.h"
34 #include "amdgpu_ras.h"
35 #include "amdgpu_rap.h"
36 #include "amdgpu_securedisplay.h"
37 #include "amdgpu_fw_attestation.h"
38 #include "amdgpu_umr.h"
39 
40 #include "amdgpu_reset.h"
41 #include "amdgpu_psp_ta.h"
42 #include "amdgpu_userq.h"
43 
44 #if defined(CONFIG_DEBUG_FS)
45 
46 /**
47  * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes
48  *
49  * @read: True if reading
50  * @f: open file handle
51  * @buf: User buffer to write/read to
52  * @size: Number of bytes to write/read
53  * @pos:  Offset to seek to
54  *
55  * This debugfs entry has special meaning on the offset being sought.
56  * Various bits have different meanings:
57  *
58  * Bit 62:  Indicates a GRBM bank switch is needed
59  * Bit 61:  Indicates a SRBM bank switch is needed (implies bit 62 is
60  *	    zero)
61  * Bits 24..33: The SE or ME selector if needed
62  * Bits 34..43: The SH (or SA) or PIPE selector if needed
63  * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed
64  *
65  * Bit 23:  Indicates that the PM power gating lock should be held
66  *	    This is necessary to read registers that might be
67  *	    unreliable during a power gating transistion.
68  *
69  * The lower bits are the BYTE offset of the register to read.  This
70  * allows reading multiple registers in a single call and having
71  * the returned size reflect that.
72  */
73 static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
74 		char __user *buf, size_t size, loff_t *pos)
75 {
76 	struct amdgpu_device *adev = file_inode(f)->i_private;
77 	ssize_t result = 0;
78 	int r;
79 	bool pm_pg_lock, use_bank, use_ring;
80 	unsigned int instance_bank, sh_bank, se_bank, me, pipe, queue, vmid;
81 
82 	pm_pg_lock = use_bank = use_ring = false;
83 	instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0;
84 
85 	if (size & 0x3 || *pos & 0x3 ||
86 			((*pos & (1ULL << 62)) && (*pos & (1ULL << 61))))
87 		return -EINVAL;
88 
89 	/* are we reading registers for which a PG lock is necessary? */
90 	pm_pg_lock = (*pos >> 23) & 1;
91 
92 	if (*pos & (1ULL << 62)) {
93 		se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
94 		sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
95 		instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
96 
97 		if (se_bank == 0x3FF)
98 			se_bank = 0xFFFFFFFF;
99 		if (sh_bank == 0x3FF)
100 			sh_bank = 0xFFFFFFFF;
101 		if (instance_bank == 0x3FF)
102 			instance_bank = 0xFFFFFFFF;
103 		use_bank = true;
104 	} else if (*pos & (1ULL << 61)) {
105 
106 		me = (*pos & GENMASK_ULL(33, 24)) >> 24;
107 		pipe = (*pos & GENMASK_ULL(43, 34)) >> 34;
108 		queue = (*pos & GENMASK_ULL(53, 44)) >> 44;
109 		vmid = (*pos & GENMASK_ULL(58, 54)) >> 54;
110 
111 		use_ring = true;
112 	} else {
113 		use_bank = use_ring = false;
114 	}
115 
116 	*pos &= (1UL << 22) - 1;
117 
118 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
119 	if (r < 0) {
120 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
121 		return r;
122 	}
123 
124 	r = amdgpu_virt_enable_access_debugfs(adev);
125 	if (r < 0) {
126 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
127 		return r;
128 	}
129 
130 	if (use_bank) {
131 		if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
132 		    (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) {
133 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
134 			amdgpu_virt_disable_access_debugfs(adev);
135 			return -EINVAL;
136 		}
137 		mutex_lock(&adev->grbm_idx_mutex);
138 		amdgpu_gfx_select_se_sh(adev, se_bank,
139 					sh_bank, instance_bank, 0);
140 	} else if (use_ring) {
141 		mutex_lock(&adev->srbm_mutex);
142 		amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid, 0);
143 	}
144 
145 	if (pm_pg_lock)
146 		mutex_lock(&adev->pm.mutex);
147 
148 	while (size) {
149 		uint32_t value;
150 
151 		if (read) {
152 			value = RREG32(*pos >> 2);
153 			r = put_user(value, (uint32_t *)buf);
154 		} else {
155 			r = get_user(value, (uint32_t *)buf);
156 			if (!r)
157 				amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0);
158 		}
159 		if (r) {
160 			result = r;
161 			goto end;
162 		}
163 
164 		result += 4;
165 		buf += 4;
166 		*pos += 4;
167 		size -= 4;
168 	}
169 
170 end:
171 	if (use_bank) {
172 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
173 		mutex_unlock(&adev->grbm_idx_mutex);
174 	} else if (use_ring) {
175 		amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0, 0);
176 		mutex_unlock(&adev->srbm_mutex);
177 	}
178 
179 	if (pm_pg_lock)
180 		mutex_unlock(&adev->pm.mutex);
181 
182 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
183 
184 	amdgpu_virt_disable_access_debugfs(adev);
185 	return result;
186 }
187 
188 /*
189  * amdgpu_debugfs_regs_read - Callback for reading MMIO registers
190  */
191 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
192 					size_t size, loff_t *pos)
193 {
194 	return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos);
195 }
196 
197 /*
198  * amdgpu_debugfs_regs_write - Callback for writing MMIO registers
199  */
200 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
201 					 size_t size, loff_t *pos)
202 {
203 	return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos);
204 }
205 
206 static int amdgpu_debugfs_regs2_open(struct inode *inode, struct file *file)
207 {
208 	struct amdgpu_debugfs_regs2_data *rd;
209 
210 	rd = kzalloc_obj(*rd);
211 	if (!rd)
212 		return -ENOMEM;
213 	rd->adev = file_inode(file)->i_private;
214 	file->private_data = rd;
215 	mutex_init(&rd->lock);
216 
217 	return 0;
218 }
219 
220 static int amdgpu_debugfs_regs2_release(struct inode *inode, struct file *file)
221 {
222 	struct amdgpu_debugfs_regs2_data *rd = file->private_data;
223 
224 	mutex_destroy(&rd->lock);
225 	kfree(file->private_data);
226 	return 0;
227 }
228 
229 static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 offset, size_t size, int write_en)
230 {
231 	struct amdgpu_debugfs_regs2_data *rd = f->private_data;
232 	struct amdgpu_device *adev = rd->adev;
233 	ssize_t result = 0;
234 	int r;
235 	uint32_t value;
236 
237 	if (size & 0x3 || offset & 0x3)
238 		return -EINVAL;
239 
240 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
241 	if (r < 0) {
242 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
243 		return r;
244 	}
245 
246 	r = amdgpu_virt_enable_access_debugfs(adev);
247 	if (r < 0) {
248 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
249 		return r;
250 	}
251 
252 	mutex_lock(&rd->lock);
253 
254 	if (rd->id.use_grbm) {
255 		if ((rd->id.grbm.sh != 0xFFFFFFFF && rd->id.grbm.sh >= adev->gfx.config.max_sh_per_se) ||
256 		    (rd->id.grbm.se != 0xFFFFFFFF && rd->id.grbm.se >= adev->gfx.config.max_shader_engines)) {
257 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
258 			amdgpu_virt_disable_access_debugfs(adev);
259 			mutex_unlock(&rd->lock);
260 			return -EINVAL;
261 		}
262 		mutex_lock(&adev->grbm_idx_mutex);
263 		amdgpu_gfx_select_se_sh(adev, rd->id.grbm.se,
264 						  rd->id.grbm.sh,
265 						  rd->id.grbm.instance, rd->id.xcc_id);
266 	}
267 
268 	if (rd->id.use_srbm) {
269 		mutex_lock(&adev->srbm_mutex);
270 		amdgpu_gfx_select_me_pipe_q(adev, rd->id.srbm.me, rd->id.srbm.pipe,
271 					    rd->id.srbm.queue, rd->id.srbm.vmid, rd->id.xcc_id);
272 	}
273 
274 	if (rd->id.pg_lock)
275 		mutex_lock(&adev->pm.mutex);
276 
277 	while (size) {
278 		if (!write_en) {
279 			value = RREG32(offset >> 2);
280 			r = put_user(value, (uint32_t *)buf);
281 		} else {
282 			r = get_user(value, (uint32_t *)buf);
283 			if (!r)
284 				amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value, rd->id.xcc_id);
285 		}
286 		if (r) {
287 			result = r;
288 			goto end;
289 		}
290 		offset += 4;
291 		size -= 4;
292 		result += 4;
293 		buf += 4;
294 	}
295 end:
296 	if (rd->id.use_grbm) {
297 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id);
298 		mutex_unlock(&adev->grbm_idx_mutex);
299 	}
300 
301 	if (rd->id.use_srbm) {
302 		amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0, rd->id.xcc_id);
303 		mutex_unlock(&adev->srbm_mutex);
304 	}
305 
306 	if (rd->id.pg_lock)
307 		mutex_unlock(&adev->pm.mutex);
308 
309 	mutex_unlock(&rd->lock);
310 
311 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
312 
313 	amdgpu_virt_disable_access_debugfs(adev);
314 	return result;
315 }
316 
317 static long amdgpu_debugfs_regs2_ioctl(struct file *f, unsigned int cmd, unsigned long data)
318 {
319 	struct amdgpu_debugfs_regs2_data *rd = f->private_data;
320 	struct amdgpu_debugfs_regs2_iocdata v1_data;
321 	int r;
322 
323 	mutex_lock(&rd->lock);
324 
325 	switch (cmd) {
326 	case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE_V2:
327 		r = copy_from_user(&rd->id, (struct amdgpu_debugfs_regs2_iocdata_v2 *)data,
328 				   sizeof(rd->id));
329 		if (r)
330 			r = -EINVAL;
331 		goto done;
332 	case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE:
333 		r = copy_from_user(&v1_data, (struct amdgpu_debugfs_regs2_iocdata *)data,
334 				   sizeof(v1_data));
335 		if (r) {
336 			r = -EINVAL;
337 			goto done;
338 		}
339 		goto v1_copy;
340 	default:
341 		r = -EINVAL;
342 		goto done;
343 	}
344 
345 v1_copy:
346 	rd->id.use_srbm = v1_data.use_srbm;
347 	rd->id.use_grbm = v1_data.use_grbm;
348 	rd->id.pg_lock = v1_data.pg_lock;
349 	rd->id.grbm.se = v1_data.grbm.se;
350 	rd->id.grbm.sh = v1_data.grbm.sh;
351 	rd->id.grbm.instance = v1_data.grbm.instance;
352 	rd->id.srbm.me = v1_data.srbm.me;
353 	rd->id.srbm.pipe = v1_data.srbm.pipe;
354 	rd->id.srbm.queue = v1_data.srbm.queue;
355 	rd->id.xcc_id = 0;
356 done:
357 	mutex_unlock(&rd->lock);
358 	return r;
359 }
360 
361 static ssize_t amdgpu_debugfs_regs2_read(struct file *f, char __user *buf, size_t size, loff_t *pos)
362 {
363 	return amdgpu_debugfs_regs2_op(f, buf, *pos, size, 0);
364 }
365 
366 static ssize_t amdgpu_debugfs_regs2_write(struct file *f, const char __user *buf, size_t size, loff_t *pos)
367 {
368 	return amdgpu_debugfs_regs2_op(f, (char __user *)buf, *pos, size, 1);
369 }
370 
371 static int amdgpu_debugfs_gprwave_open(struct inode *inode, struct file *file)
372 {
373 	struct amdgpu_debugfs_gprwave_data *rd;
374 
375 	rd = kzalloc_obj(*rd);
376 	if (!rd)
377 		return -ENOMEM;
378 	rd->adev = file_inode(file)->i_private;
379 	file->private_data = rd;
380 	mutex_init(&rd->lock);
381 
382 	return 0;
383 }
384 
385 static int amdgpu_debugfs_gprwave_release(struct inode *inode, struct file *file)
386 {
387 	struct amdgpu_debugfs_gprwave_data *rd = file->private_data;
388 
389 	mutex_destroy(&rd->lock);
390 	kfree(file->private_data);
391 	return 0;
392 }
393 
394 static ssize_t amdgpu_debugfs_gprwave_read(struct file *f, char __user *buf, size_t size, loff_t *pos)
395 {
396 	struct amdgpu_debugfs_gprwave_data *rd = f->private_data;
397 	struct amdgpu_device *adev = rd->adev;
398 	ssize_t result = 0;
399 	int r;
400 	uint32_t *data, x;
401 
402 	if (size > 4096 || size & 0x3 || *pos & 0x3)
403 		return -EINVAL;
404 
405 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
406 	if (r < 0) {
407 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
408 		return r;
409 	}
410 
411 	r = amdgpu_virt_enable_access_debugfs(adev);
412 	if (r < 0) {
413 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
414 		return r;
415 	}
416 
417 	data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
418 	if (!data) {
419 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
420 		amdgpu_virt_disable_access_debugfs(adev);
421 		return -ENOMEM;
422 	}
423 
424 	/* switch to the specific se/sh/cu */
425 	mutex_lock(&adev->grbm_idx_mutex);
426 	amdgpu_gfx_select_se_sh(adev, rd->id.se, rd->id.sh, rd->id.cu, rd->id.xcc_id);
427 
428 	if (!rd->id.gpr_or_wave) {
429 		x = 0;
430 		if (adev->gfx.funcs->read_wave_data)
431 			adev->gfx.funcs->read_wave_data(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, data, &x);
432 	} else {
433 		x = size >> 2;
434 		if (rd->id.gpr.vpgr_or_sgpr) {
435 			if (adev->gfx.funcs->read_wave_vgprs)
436 				adev->gfx.funcs->read_wave_vgprs(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, rd->id.gpr.thread, *pos, size>>2, data);
437 		} else {
438 			if (adev->gfx.funcs->read_wave_sgprs)
439 				adev->gfx.funcs->read_wave_sgprs(adev, rd->id.xcc_id, rd->id.simd, rd->id.wave, *pos, size>>2, data);
440 		}
441 	}
442 
443 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, rd->id.xcc_id);
444 	mutex_unlock(&adev->grbm_idx_mutex);
445 
446 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
447 
448 	if (!x) {
449 		result = -EINVAL;
450 		goto done;
451 	}
452 
453 	while (size && (*pos < x * 4)) {
454 		uint32_t value;
455 
456 		value = data[*pos >> 2];
457 		r = put_user(value, (uint32_t *)buf);
458 		if (r) {
459 			result = r;
460 			goto done;
461 		}
462 
463 		result += 4;
464 		buf += 4;
465 		*pos += 4;
466 		size -= 4;
467 	}
468 
469 done:
470 	amdgpu_virt_disable_access_debugfs(adev);
471 	kfree(data);
472 	return result;
473 }
474 
475 static long amdgpu_debugfs_gprwave_ioctl(struct file *f, unsigned int cmd, unsigned long data)
476 {
477 	struct amdgpu_debugfs_gprwave_data *rd = f->private_data;
478 	int r = 0;
479 
480 	mutex_lock(&rd->lock);
481 
482 	switch (cmd) {
483 	case AMDGPU_DEBUGFS_GPRWAVE_IOC_SET_STATE:
484 		if (copy_from_user(&rd->id,
485 				   (struct amdgpu_debugfs_gprwave_iocdata *)data,
486 				   sizeof(rd->id)))
487 			r = -EFAULT;
488 		goto done;
489 	default:
490 		r = -EINVAL;
491 		goto done;
492 	}
493 
494 done:
495 	mutex_unlock(&rd->lock);
496 	return r;
497 }
498 
499 
500 
501 
502 /**
503  * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register
504  *
505  * @f: open file handle
506  * @buf: User buffer to store read data in
507  * @size: Number of bytes to read
508  * @pos:  Offset to seek to
509  *
510  * The lower bits are the BYTE offset of the register to read.  This
511  * allows reading multiple registers in a single call and having
512  * the returned size reflect that.
513  */
514 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
515 					size_t size, loff_t *pos)
516 {
517 	struct amdgpu_device *adev = file_inode(f)->i_private;
518 	ssize_t result = 0;
519 	int r;
520 
521 	if (size & 0x3 || *pos & 0x3)
522 		return -EINVAL;
523 
524 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
525 	if (r < 0) {
526 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
527 		return r;
528 	}
529 
530 	r = amdgpu_virt_enable_access_debugfs(adev);
531 	if (r < 0) {
532 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
533 		return r;
534 	}
535 
536 	while (size) {
537 		uint32_t value;
538 
539 		if (upper_32_bits(*pos))
540 			value = RREG32_PCIE_EXT(*pos);
541 		else
542 			value = RREG32_PCIE(*pos);
543 
544 		r = put_user(value, (uint32_t *)buf);
545 		if (r)
546 			goto out;
547 
548 		result += 4;
549 		buf += 4;
550 		*pos += 4;
551 		size -= 4;
552 	}
553 
554 	r = result;
555 out:
556 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
557 	amdgpu_virt_disable_access_debugfs(adev);
558 	return r;
559 }
560 
561 /**
562  * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register
563  *
564  * @f: open file handle
565  * @buf: User buffer to write data from
566  * @size: Number of bytes to write
567  * @pos:  Offset to seek to
568  *
569  * The lower bits are the BYTE offset of the register to write.  This
570  * allows writing multiple registers in a single call and having
571  * the returned size reflect that.
572  */
573 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
574 					 size_t size, loff_t *pos)
575 {
576 	struct amdgpu_device *adev = file_inode(f)->i_private;
577 	ssize_t result = 0;
578 	int r;
579 
580 	if (size & 0x3 || *pos & 0x3)
581 		return -EINVAL;
582 
583 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
584 	if (r < 0) {
585 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
586 		return r;
587 	}
588 
589 	r = amdgpu_virt_enable_access_debugfs(adev);
590 	if (r < 0) {
591 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
592 		return r;
593 	}
594 
595 	while (size) {
596 		uint32_t value;
597 
598 		r = get_user(value, (uint32_t *)buf);
599 		if (r)
600 			goto out;
601 
602 		if (upper_32_bits(*pos))
603 			WREG32_PCIE_EXT(*pos, value);
604 		else
605 			WREG32_PCIE(*pos, value);
606 
607 		result += 4;
608 		buf += 4;
609 		*pos += 4;
610 		size -= 4;
611 	}
612 
613 	r = result;
614 out:
615 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
616 	amdgpu_virt_disable_access_debugfs(adev);
617 	return r;
618 }
619 
620 /**
621  * amdgpu_debugfs_regs_didt_read - Read from a DIDT register
622  *
623  * @f: open file handle
624  * @buf: User buffer to store read data in
625  * @size: Number of bytes to read
626  * @pos:  Offset to seek to
627  *
628  * The lower bits are the BYTE offset of the register to read.  This
629  * allows reading multiple registers in a single call and having
630  * the returned size reflect that.
631  */
632 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
633 					size_t size, loff_t *pos)
634 {
635 	struct amdgpu_device *adev = file_inode(f)->i_private;
636 	ssize_t result = 0;
637 	int r;
638 
639 	if (size & 0x3 || *pos & 0x3)
640 		return -EINVAL;
641 
642 	if (!adev->reg.didt.rreg)
643 		return -EOPNOTSUPP;
644 
645 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
646 	if (r < 0) {
647 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
648 		return r;
649 	}
650 
651 	r = amdgpu_virt_enable_access_debugfs(adev);
652 	if (r < 0) {
653 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
654 		return r;
655 	}
656 
657 	while (size) {
658 		uint32_t value;
659 
660 		value = RREG32_DIDT(*pos >> 2);
661 		r = put_user(value, (uint32_t *)buf);
662 		if (r)
663 			goto out;
664 
665 		result += 4;
666 		buf += 4;
667 		*pos += 4;
668 		size -= 4;
669 	}
670 
671 	r = result;
672 out:
673 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
674 	amdgpu_virt_disable_access_debugfs(adev);
675 	return r;
676 }
677 
678 /**
679  * amdgpu_debugfs_regs_didt_write - Write to a DIDT register
680  *
681  * @f: open file handle
682  * @buf: User buffer to write data from
683  * @size: Number of bytes to write
684  * @pos:  Offset to seek to
685  *
686  * The lower bits are the BYTE offset of the register to write.  This
687  * allows writing multiple registers in a single call and having
688  * the returned size reflect that.
689  */
690 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
691 					 size_t size, loff_t *pos)
692 {
693 	struct amdgpu_device *adev = file_inode(f)->i_private;
694 	ssize_t result = 0;
695 	int r;
696 
697 	if (size & 0x3 || *pos & 0x3)
698 		return -EINVAL;
699 
700 	if (!adev->reg.didt.wreg)
701 		return -EOPNOTSUPP;
702 
703 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
704 	if (r < 0) {
705 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
706 		return r;
707 	}
708 
709 	r = amdgpu_virt_enable_access_debugfs(adev);
710 	if (r < 0) {
711 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
712 		return r;
713 	}
714 
715 	while (size) {
716 		uint32_t value;
717 
718 		r = get_user(value, (uint32_t *)buf);
719 		if (r)
720 			goto out;
721 
722 		WREG32_DIDT(*pos >> 2, value);
723 
724 		result += 4;
725 		buf += 4;
726 		*pos += 4;
727 		size -= 4;
728 	}
729 
730 	r = result;
731 out:
732 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
733 	amdgpu_virt_disable_access_debugfs(adev);
734 	return r;
735 }
736 
737 /**
738  * amdgpu_debugfs_regs_smc_read - Read from a SMC register
739  *
740  * @f: open file handle
741  * @buf: User buffer to store read data in
742  * @size: Number of bytes to read
743  * @pos:  Offset to seek to
744  *
745  * The lower bits are the BYTE offset of the register to read.  This
746  * allows reading multiple registers in a single call and having
747  * the returned size reflect that.
748  */
749 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
750 					size_t size, loff_t *pos)
751 {
752 	struct amdgpu_device *adev = file_inode(f)->i_private;
753 	ssize_t result = 0;
754 	int r;
755 
756 	if (!adev->reg.smc.rreg)
757 		return -EOPNOTSUPP;
758 
759 	if (size & 0x3 || *pos & 0x3)
760 		return -EINVAL;
761 
762 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
763 	if (r < 0) {
764 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
765 		return r;
766 	}
767 
768 	r = amdgpu_virt_enable_access_debugfs(adev);
769 	if (r < 0) {
770 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
771 		return r;
772 	}
773 
774 	while (size) {
775 		uint32_t value;
776 
777 		value = RREG32_SMC(*pos);
778 		r = put_user(value, (uint32_t *)buf);
779 		if (r)
780 			goto out;
781 
782 		result += 4;
783 		buf += 4;
784 		*pos += 4;
785 		size -= 4;
786 	}
787 
788 	r = result;
789 out:
790 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
791 	amdgpu_virt_disable_access_debugfs(adev);
792 	return r;
793 }
794 
795 /**
796  * amdgpu_debugfs_regs_smc_write - Write to a SMC register
797  *
798  * @f: open file handle
799  * @buf: User buffer to write data from
800  * @size: Number of bytes to write
801  * @pos:  Offset to seek to
802  *
803  * The lower bits are the BYTE offset of the register to write.  This
804  * allows writing multiple registers in a single call and having
805  * the returned size reflect that.
806  */
807 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
808 					 size_t size, loff_t *pos)
809 {
810 	struct amdgpu_device *adev = file_inode(f)->i_private;
811 	ssize_t result = 0;
812 	int r;
813 
814 	if (!adev->reg.smc.wreg)
815 		return -EOPNOTSUPP;
816 
817 	if (size & 0x3 || *pos & 0x3)
818 		return -EINVAL;
819 
820 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
821 	if (r < 0) {
822 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
823 		return r;
824 	}
825 
826 	r = amdgpu_virt_enable_access_debugfs(adev);
827 	if (r < 0) {
828 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
829 		return r;
830 	}
831 
832 	while (size) {
833 		uint32_t value;
834 
835 		r = get_user(value, (uint32_t *)buf);
836 		if (r)
837 			goto out;
838 
839 		WREG32_SMC(*pos, value);
840 
841 		result += 4;
842 		buf += 4;
843 		*pos += 4;
844 		size -= 4;
845 	}
846 
847 	r = result;
848 out:
849 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
850 	amdgpu_virt_disable_access_debugfs(adev);
851 	return r;
852 }
853 
854 /**
855  * amdgpu_debugfs_gca_config_read - Read from gfx config data
856  *
857  * @f: open file handle
858  * @buf: User buffer to store read data in
859  * @size: Number of bytes to read
860  * @pos:  Offset to seek to
861  *
862  * This file is used to access configuration data in a somewhat
863  * stable fashion.  The format is a series of DWORDs with the first
864  * indicating which revision it is.  New content is appended to the
865  * end so that older software can still read the data.
866  */
867 
868 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
869 					size_t size, loff_t *pos)
870 {
871 	struct amdgpu_device *adev = file_inode(f)->i_private;
872 	ssize_t result = 0;
873 	int r;
874 	uint32_t *config, no_regs = 0;
875 
876 	if (size & 0x3 || *pos & 0x3)
877 		return -EINVAL;
878 
879 	config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
880 	if (!config)
881 		return -ENOMEM;
882 
883 	/* version, increment each time something is added */
884 	config[no_regs++] = 5;
885 	config[no_regs++] = adev->gfx.config.max_shader_engines;
886 	config[no_regs++] = adev->gfx.config.max_tile_pipes;
887 	config[no_regs++] = adev->gfx.config.max_cu_per_sh;
888 	config[no_regs++] = adev->gfx.config.max_sh_per_se;
889 	config[no_regs++] = adev->gfx.config.max_backends_per_se;
890 	config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
891 	config[no_regs++] = adev->gfx.config.max_gprs;
892 	config[no_regs++] = adev->gfx.config.max_gs_threads;
893 	config[no_regs++] = adev->gfx.config.max_hw_contexts;
894 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
895 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
896 	config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
897 	config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
898 	config[no_regs++] = adev->gfx.config.num_tile_pipes;
899 	config[no_regs++] = adev->gfx.config.backend_enable_mask;
900 	config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
901 	config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
902 	config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
903 	config[no_regs++] = adev->gfx.config.num_gpus;
904 	config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
905 	config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
906 	config[no_regs++] = adev->gfx.config.gb_addr_config;
907 	config[no_regs++] = adev->gfx.config.num_rbs;
908 
909 	/* rev==1 */
910 	config[no_regs++] = adev->rev_id;
911 	config[no_regs++] = adev->pg_flags;
912 	config[no_regs++] = lower_32_bits(adev->cg_flags);
913 
914 	/* rev==2 */
915 	config[no_regs++] = adev->family;
916 	config[no_regs++] = adev->external_rev_id;
917 
918 	/* rev==3 */
919 	config[no_regs++] = adev->pdev->device;
920 	config[no_regs++] = adev->pdev->revision;
921 	config[no_regs++] = adev->pdev->subsystem_device;
922 	config[no_regs++] = adev->pdev->subsystem_vendor;
923 
924 	/* rev==4 APU flag */
925 	config[no_regs++] = adev->flags & AMD_IS_APU ? 1 : 0;
926 
927 	/* rev==5 PG/CG flag upper 32bit */
928 	config[no_regs++] = 0;
929 	config[no_regs++] = upper_32_bits(adev->cg_flags);
930 
931 	while (size && (*pos < no_regs * 4)) {
932 		uint32_t value;
933 
934 		value = config[*pos >> 2];
935 		r = put_user(value, (uint32_t *)buf);
936 		if (r) {
937 			kfree(config);
938 			return r;
939 		}
940 
941 		result += 4;
942 		buf += 4;
943 		*pos += 4;
944 		size -= 4;
945 	}
946 
947 	kfree(config);
948 	return result;
949 }
950 
951 /**
952  * amdgpu_debugfs_sensor_read - Read from the powerplay sensors
953  *
954  * @f: open file handle
955  * @buf: User buffer to store read data in
956  * @size: Number of bytes to read
957  * @pos:  Offset to seek to
958  *
959  * The offset is treated as the BYTE address of one of the sensors
960  * enumerated in amd/include/kgd_pp_interface.h under the
961  * 'amd_pp_sensors' enumeration.  For instance to read the UVD VCLK
962  * you would use the offset 3 * 4 = 12.
963  */
964 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
965 					size_t size, loff_t *pos)
966 {
967 	struct amdgpu_device *adev = file_inode(f)->i_private;
968 	int idx, x, outsize, r, valuesize;
969 	uint32_t values[16];
970 
971 	if (size & 3 || *pos & 0x3)
972 		return -EINVAL;
973 
974 	if (!adev->pm.dpm_enabled)
975 		return -EINVAL;
976 
977 	/* convert offset to sensor number */
978 	idx = *pos >> 2;
979 
980 	valuesize = sizeof(values);
981 
982 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
983 	if (r < 0) {
984 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
985 		return r;
986 	}
987 
988 	r = amdgpu_virt_enable_access_debugfs(adev);
989 	if (r < 0) {
990 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
991 		return r;
992 	}
993 
994 	r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
995 
996 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
997 
998 	if (r) {
999 		amdgpu_virt_disable_access_debugfs(adev);
1000 		return r;
1001 	}
1002 
1003 	if (size > valuesize) {
1004 		amdgpu_virt_disable_access_debugfs(adev);
1005 		return -EINVAL;
1006 	}
1007 
1008 	outsize = 0;
1009 	x = 0;
1010 	if (!r) {
1011 		while (size) {
1012 			r = put_user(values[x++], (int32_t *)buf);
1013 			buf += 4;
1014 			size -= 4;
1015 			outsize += 4;
1016 		}
1017 	}
1018 
1019 	amdgpu_virt_disable_access_debugfs(adev);
1020 	return !r ? outsize : r;
1021 }
1022 
1023 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data
1024  *
1025  * @f: open file handle
1026  * @buf: User buffer to store read data in
1027  * @size: Number of bytes to read
1028  * @pos:  Offset to seek to
1029  *
1030  * The offset being sought changes which wave that the status data
1031  * will be returned for.  The bits are used as follows:
1032  *
1033  * Bits 0..6:	Byte offset into data
1034  * Bits 7..14:	SE selector
1035  * Bits 15..22:	SH/SA selector
1036  * Bits 23..30: CU/{WGP+SIMD} selector
1037  * Bits 31..36: WAVE ID selector
1038  * Bits 37..44: SIMD ID selector
1039  *
1040  * The returned data begins with one DWORD of version information
1041  * Followed by WAVE STATUS registers relevant to the GFX IP version
1042  * being used.  See gfx_v8_0_read_wave_data() for an example output.
1043  */
1044 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
1045 					size_t size, loff_t *pos)
1046 {
1047 	struct amdgpu_device *adev = f->f_inode->i_private;
1048 	int r, x;
1049 	ssize_t result = 0;
1050 	uint32_t offset, se, sh, cu, wave, simd, data[32];
1051 
1052 	if (size & 3 || *pos & 3)
1053 		return -EINVAL;
1054 
1055 	/* decode offset */
1056 	offset = (*pos & GENMASK_ULL(6, 0));
1057 	se = (*pos & GENMASK_ULL(14, 7)) >> 7;
1058 	sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
1059 	cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
1060 	wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
1061 	simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
1062 
1063 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1064 	if (r < 0) {
1065 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1066 		return r;
1067 	}
1068 
1069 	r = amdgpu_virt_enable_access_debugfs(adev);
1070 	if (r < 0) {
1071 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1072 		return r;
1073 	}
1074 
1075 	/* switch to the specific se/sh/cu */
1076 	mutex_lock(&adev->grbm_idx_mutex);
1077 	amdgpu_gfx_select_se_sh(adev, se, sh, cu, 0);
1078 
1079 	x = 0;
1080 	if (adev->gfx.funcs->read_wave_data)
1081 		adev->gfx.funcs->read_wave_data(adev, 0, simd, wave, data, &x);
1082 
1083 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0);
1084 	mutex_unlock(&adev->grbm_idx_mutex);
1085 
1086 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1087 
1088 	if (!x) {
1089 		amdgpu_virt_disable_access_debugfs(adev);
1090 		return -EINVAL;
1091 	}
1092 
1093 	while (size && (offset < x * 4)) {
1094 		uint32_t value;
1095 
1096 		value = data[offset >> 2];
1097 		r = put_user(value, (uint32_t *)buf);
1098 		if (r) {
1099 			amdgpu_virt_disable_access_debugfs(adev);
1100 			return r;
1101 		}
1102 
1103 		result += 4;
1104 		buf += 4;
1105 		offset += 4;
1106 		size -= 4;
1107 	}
1108 
1109 	amdgpu_virt_disable_access_debugfs(adev);
1110 	return result;
1111 }
1112 
1113 /** amdgpu_debugfs_gpr_read - Read wave gprs
1114  *
1115  * @f: open file handle
1116  * @buf: User buffer to store read data in
1117  * @size: Number of bytes to read
1118  * @pos:  Offset to seek to
1119  *
1120  * The offset being sought changes which wave that the status data
1121  * will be returned for.  The bits are used as follows:
1122  *
1123  * Bits 0..11:	Byte offset into data
1124  * Bits 12..19:	SE selector
1125  * Bits 20..27:	SH/SA selector
1126  * Bits 28..35: CU/{WGP+SIMD} selector
1127  * Bits 36..43: WAVE ID selector
1128  * Bits 37..44: SIMD ID selector
1129  * Bits 52..59: Thread selector
1130  * Bits 60..61: Bank selector (VGPR=0,SGPR=1)
1131  *
1132  * The return data comes from the SGPR or VGPR register bank for
1133  * the selected operational unit.
1134  */
1135 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1136 					size_t size, loff_t *pos)
1137 {
1138 	struct amdgpu_device *adev = f->f_inode->i_private;
1139 	int r;
1140 	ssize_t result = 0;
1141 	uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
1142 
1143 	if (size > 4096 || size & 3 || *pos & 3)
1144 		return -EINVAL;
1145 
1146 	/* decode offset */
1147 	offset = (*pos & GENMASK_ULL(11, 0)) >> 2;
1148 	se = (*pos & GENMASK_ULL(19, 12)) >> 12;
1149 	sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
1150 	cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
1151 	wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
1152 	simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
1153 	thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
1154 	bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
1155 
1156 	data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
1157 	if (!data)
1158 		return -ENOMEM;
1159 
1160 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1161 	if (r < 0)
1162 		goto err;
1163 
1164 	r = amdgpu_virt_enable_access_debugfs(adev);
1165 	if (r < 0)
1166 		goto err;
1167 
1168 	/* switch to the specific se/sh/cu */
1169 	mutex_lock(&adev->grbm_idx_mutex);
1170 	amdgpu_gfx_select_se_sh(adev, se, sh, cu, 0);
1171 
1172 	if (bank == 0) {
1173 		if (adev->gfx.funcs->read_wave_vgprs)
1174 			adev->gfx.funcs->read_wave_vgprs(adev, 0, simd, wave, thread, offset, size>>2, data);
1175 	} else {
1176 		if (adev->gfx.funcs->read_wave_sgprs)
1177 			adev->gfx.funcs->read_wave_sgprs(adev, 0, simd, wave, offset, size>>2, data);
1178 	}
1179 
1180 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0);
1181 	mutex_unlock(&adev->grbm_idx_mutex);
1182 
1183 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1184 
1185 	while (size) {
1186 		uint32_t value;
1187 
1188 		value = data[result >> 2];
1189 		r = put_user(value, (uint32_t *)buf);
1190 		if (r) {
1191 			amdgpu_virt_disable_access_debugfs(adev);
1192 			goto err;
1193 		}
1194 
1195 		result += 4;
1196 		buf += 4;
1197 		size -= 4;
1198 	}
1199 
1200 	kfree(data);
1201 	amdgpu_virt_disable_access_debugfs(adev);
1202 	return result;
1203 
1204 err:
1205 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1206 	kfree(data);
1207 	return r;
1208 }
1209 
1210 /**
1211  * amdgpu_debugfs_gfxoff_residency_read - Read GFXOFF residency
1212  *
1213  * @f: open file handle
1214  * @buf: User buffer to store read data in
1215  * @size: Number of bytes to read
1216  * @pos:  Offset to seek to
1217  *
1218  * Read the last residency value logged. It doesn't auto update, one needs to
1219  * stop logging before getting the current value.
1220  */
1221 static ssize_t amdgpu_debugfs_gfxoff_residency_read(struct file *f, char __user *buf,
1222 						    size_t size, loff_t *pos)
1223 {
1224 	struct amdgpu_device *adev = file_inode(f)->i_private;
1225 	ssize_t result = 0;
1226 	int r;
1227 
1228 	if (size & 0x3 || *pos & 0x3)
1229 		return -EINVAL;
1230 
1231 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1232 	if (r < 0) {
1233 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1234 		return r;
1235 	}
1236 
1237 	while (size) {
1238 		uint32_t value;
1239 
1240 		r = amdgpu_get_gfx_off_residency(adev, &value);
1241 		if (r)
1242 			goto out;
1243 
1244 		r = put_user(value, (uint32_t *)buf);
1245 		if (r)
1246 			goto out;
1247 
1248 		result += 4;
1249 		buf += 4;
1250 		*pos += 4;
1251 		size -= 4;
1252 	}
1253 
1254 	r = result;
1255 out:
1256 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1257 
1258 	return r;
1259 }
1260 
1261 /**
1262  * amdgpu_debugfs_gfxoff_residency_write - Log GFXOFF Residency
1263  *
1264  * @f: open file handle
1265  * @buf: User buffer to write data from
1266  * @size: Number of bytes to write
1267  * @pos:  Offset to seek to
1268  *
1269  * Write a 32-bit non-zero to start logging; write a 32-bit zero to stop
1270  */
1271 static ssize_t amdgpu_debugfs_gfxoff_residency_write(struct file *f, const char __user *buf,
1272 						     size_t size, loff_t *pos)
1273 {
1274 	struct amdgpu_device *adev = file_inode(f)->i_private;
1275 	ssize_t result = 0;
1276 	int r;
1277 
1278 	if (size & 0x3 || *pos & 0x3)
1279 		return -EINVAL;
1280 
1281 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1282 	if (r < 0) {
1283 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1284 		return r;
1285 	}
1286 
1287 	while (size) {
1288 		u32 value;
1289 
1290 		r = get_user(value, (uint32_t *)buf);
1291 		if (r)
1292 			goto out;
1293 
1294 		amdgpu_set_gfx_off_residency(adev, value ? true : false);
1295 
1296 		result += 4;
1297 		buf += 4;
1298 		*pos += 4;
1299 		size -= 4;
1300 	}
1301 
1302 	r = result;
1303 out:
1304 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1305 
1306 	return r;
1307 }
1308 
1309 
1310 /**
1311  * amdgpu_debugfs_gfxoff_count_read - Read GFXOFF entry count
1312  *
1313  * @f: open file handle
1314  * @buf: User buffer to store read data in
1315  * @size: Number of bytes to read
1316  * @pos:  Offset to seek to
1317  */
1318 static ssize_t amdgpu_debugfs_gfxoff_count_read(struct file *f, char __user *buf,
1319 						size_t size, loff_t *pos)
1320 {
1321 	struct amdgpu_device *adev = file_inode(f)->i_private;
1322 	ssize_t result = 0;
1323 	int r;
1324 
1325 	if (size & 0x3 || *pos & 0x3)
1326 		return -EINVAL;
1327 
1328 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1329 	if (r < 0) {
1330 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1331 		return r;
1332 	}
1333 
1334 	while (size) {
1335 		u64 value = 0;
1336 
1337 		r = amdgpu_get_gfx_off_entrycount(adev, &value);
1338 		if (r)
1339 			goto out;
1340 
1341 		r = put_user(value, (u64 *)buf);
1342 		if (r)
1343 			goto out;
1344 
1345 		result += 4;
1346 		buf += 4;
1347 		*pos += 4;
1348 		size -= 4;
1349 	}
1350 
1351 	r = result;
1352 out:
1353 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1354 
1355 	return r;
1356 }
1357 
1358 /**
1359  * amdgpu_debugfs_gfxoff_write - Enable/disable GFXOFF
1360  *
1361  * @f: open file handle
1362  * @buf: User buffer to write data from
1363  * @size: Number of bytes to write
1364  * @pos:  Offset to seek to
1365  *
1366  * Write a 32-bit zero to disable or a 32-bit non-zero to enable
1367  */
1368 static ssize_t amdgpu_debugfs_gfxoff_write(struct file *f, const char __user *buf,
1369 					 size_t size, loff_t *pos)
1370 {
1371 	struct amdgpu_device *adev = file_inode(f)->i_private;
1372 	ssize_t result = 0;
1373 	int r;
1374 
1375 	if (size & 0x3 || *pos & 0x3)
1376 		return -EINVAL;
1377 
1378 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1379 	if (r < 0) {
1380 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1381 		return r;
1382 	}
1383 
1384 	while (size) {
1385 		uint32_t value;
1386 
1387 		r = get_user(value, (uint32_t *)buf);
1388 		if (r)
1389 			goto out;
1390 
1391 		amdgpu_gfx_off_ctrl(adev, value ? true : false);
1392 
1393 		result += 4;
1394 		buf += 4;
1395 		*pos += 4;
1396 		size -= 4;
1397 	}
1398 
1399 	r = result;
1400 out:
1401 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1402 
1403 	return r;
1404 }
1405 
1406 
1407 /**
1408  * amdgpu_debugfs_gfxoff_read - read gfxoff status
1409  *
1410  * @f: open file handle
1411  * @buf: User buffer to store read data in
1412  * @size: Number of bytes to read
1413  * @pos:  Offset to seek to
1414  */
1415 static ssize_t amdgpu_debugfs_gfxoff_read(struct file *f, char __user *buf,
1416 					 size_t size, loff_t *pos)
1417 {
1418 	struct amdgpu_device *adev = file_inode(f)->i_private;
1419 	ssize_t result = 0;
1420 	int r;
1421 
1422 	if (size & 0x3 || *pos & 0x3)
1423 		return -EINVAL;
1424 
1425 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1426 	if (r < 0) {
1427 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1428 		return r;
1429 	}
1430 
1431 	while (size) {
1432 		u32 value = adev->gfx.gfx_off_state;
1433 
1434 		r = put_user(value, (u32 *)buf);
1435 		if (r)
1436 			goto out;
1437 
1438 		result += 4;
1439 		buf += 4;
1440 		*pos += 4;
1441 		size -= 4;
1442 	}
1443 
1444 	r = result;
1445 out:
1446 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1447 
1448 	return r;
1449 }
1450 
1451 static ssize_t amdgpu_debugfs_gfxoff_status_read(struct file *f, char __user *buf,
1452 						 size_t size, loff_t *pos)
1453 {
1454 	struct amdgpu_device *adev = file_inode(f)->i_private;
1455 	ssize_t result = 0;
1456 	int r;
1457 
1458 	if (size & 0x3 || *pos & 0x3)
1459 		return -EINVAL;
1460 
1461 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1462 	if (r < 0) {
1463 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1464 		return r;
1465 	}
1466 
1467 	while (size) {
1468 		u32 value;
1469 
1470 		r = amdgpu_get_gfx_off_status(adev, &value);
1471 		if (r)
1472 			goto out;
1473 
1474 		r = put_user(value, (u32 *)buf);
1475 		if (r)
1476 			goto out;
1477 
1478 		result += 4;
1479 		buf += 4;
1480 		*pos += 4;
1481 		size -= 4;
1482 	}
1483 
1484 	r = result;
1485 out:
1486 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1487 
1488 	return r;
1489 }
1490 
1491 static const struct file_operations amdgpu_debugfs_regs2_fops = {
1492 	.owner = THIS_MODULE,
1493 	.unlocked_ioctl = amdgpu_debugfs_regs2_ioctl,
1494 	.read = amdgpu_debugfs_regs2_read,
1495 	.write = amdgpu_debugfs_regs2_write,
1496 	.open = amdgpu_debugfs_regs2_open,
1497 	.release = amdgpu_debugfs_regs2_release,
1498 	.llseek = default_llseek
1499 };
1500 
1501 static const struct file_operations amdgpu_debugfs_gprwave_fops = {
1502 	.owner = THIS_MODULE,
1503 	.unlocked_ioctl = amdgpu_debugfs_gprwave_ioctl,
1504 	.read = amdgpu_debugfs_gprwave_read,
1505 	.open = amdgpu_debugfs_gprwave_open,
1506 	.release = amdgpu_debugfs_gprwave_release,
1507 	.llseek = default_llseek
1508 };
1509 
1510 static const struct file_operations amdgpu_debugfs_regs_fops = {
1511 	.owner = THIS_MODULE,
1512 	.read = amdgpu_debugfs_regs_read,
1513 	.write = amdgpu_debugfs_regs_write,
1514 	.llseek = default_llseek
1515 };
1516 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
1517 	.owner = THIS_MODULE,
1518 	.read = amdgpu_debugfs_regs_didt_read,
1519 	.write = amdgpu_debugfs_regs_didt_write,
1520 	.llseek = default_llseek
1521 };
1522 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
1523 	.owner = THIS_MODULE,
1524 	.read = amdgpu_debugfs_regs_pcie_read,
1525 	.write = amdgpu_debugfs_regs_pcie_write,
1526 	.llseek = default_llseek
1527 };
1528 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
1529 	.owner = THIS_MODULE,
1530 	.read = amdgpu_debugfs_regs_smc_read,
1531 	.write = amdgpu_debugfs_regs_smc_write,
1532 	.llseek = default_llseek
1533 };
1534 
1535 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
1536 	.owner = THIS_MODULE,
1537 	.read = amdgpu_debugfs_gca_config_read,
1538 	.llseek = default_llseek
1539 };
1540 
1541 static const struct file_operations amdgpu_debugfs_sensors_fops = {
1542 	.owner = THIS_MODULE,
1543 	.read = amdgpu_debugfs_sensor_read,
1544 	.llseek = default_llseek
1545 };
1546 
1547 static const struct file_operations amdgpu_debugfs_wave_fops = {
1548 	.owner = THIS_MODULE,
1549 	.read = amdgpu_debugfs_wave_read,
1550 	.llseek = default_llseek
1551 };
1552 static const struct file_operations amdgpu_debugfs_gpr_fops = {
1553 	.owner = THIS_MODULE,
1554 	.read = amdgpu_debugfs_gpr_read,
1555 	.llseek = default_llseek
1556 };
1557 
1558 static const struct file_operations amdgpu_debugfs_gfxoff_fops = {
1559 	.owner = THIS_MODULE,
1560 	.read = amdgpu_debugfs_gfxoff_read,
1561 	.write = amdgpu_debugfs_gfxoff_write,
1562 	.llseek = default_llseek
1563 };
1564 
1565 static const struct file_operations amdgpu_debugfs_gfxoff_status_fops = {
1566 	.owner = THIS_MODULE,
1567 	.read = amdgpu_debugfs_gfxoff_status_read,
1568 	.llseek = default_llseek
1569 };
1570 
1571 static const struct file_operations amdgpu_debugfs_gfxoff_count_fops = {
1572 	.owner = THIS_MODULE,
1573 	.read = amdgpu_debugfs_gfxoff_count_read,
1574 	.llseek = default_llseek
1575 };
1576 
1577 static const struct file_operations amdgpu_debugfs_gfxoff_residency_fops = {
1578 	.owner = THIS_MODULE,
1579 	.read = amdgpu_debugfs_gfxoff_residency_read,
1580 	.write = amdgpu_debugfs_gfxoff_residency_write,
1581 	.llseek = default_llseek
1582 };
1583 
1584 static const struct file_operations *debugfs_regs[] = {
1585 	&amdgpu_debugfs_regs_fops,
1586 	&amdgpu_debugfs_regs2_fops,
1587 	&amdgpu_debugfs_gprwave_fops,
1588 	&amdgpu_debugfs_regs_didt_fops,
1589 	&amdgpu_debugfs_regs_pcie_fops,
1590 	&amdgpu_debugfs_regs_smc_fops,
1591 	&amdgpu_debugfs_gca_config_fops,
1592 	&amdgpu_debugfs_sensors_fops,
1593 	&amdgpu_debugfs_wave_fops,
1594 	&amdgpu_debugfs_gpr_fops,
1595 	&amdgpu_debugfs_gfxoff_fops,
1596 	&amdgpu_debugfs_gfxoff_status_fops,
1597 	&amdgpu_debugfs_gfxoff_count_fops,
1598 	&amdgpu_debugfs_gfxoff_residency_fops,
1599 };
1600 
1601 static const char * const debugfs_regs_names[] = {
1602 	"amdgpu_regs",
1603 	"amdgpu_regs2",
1604 	"amdgpu_gprwave",
1605 	"amdgpu_regs_didt",
1606 	"amdgpu_regs_pcie",
1607 	"amdgpu_regs_smc",
1608 	"amdgpu_gca_config",
1609 	"amdgpu_sensors",
1610 	"amdgpu_wave",
1611 	"amdgpu_gpr",
1612 	"amdgpu_gfxoff",
1613 	"amdgpu_gfxoff_status",
1614 	"amdgpu_gfxoff_count",
1615 	"amdgpu_gfxoff_residency",
1616 };
1617 
1618 /**
1619  * amdgpu_debugfs_regs_init -	Initialize debugfs entries that provide
1620  *				register access.
1621  *
1622  * @adev: The device to attach the debugfs entries to
1623  */
1624 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
1625 {
1626 	struct drm_minor *minor = adev_to_drm(adev)->primary;
1627 	struct dentry *ent, *root = minor->debugfs_root;
1628 	unsigned int i;
1629 
1630 	for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
1631 		ent = debugfs_create_file(debugfs_regs_names[i],
1632 					  S_IFREG | 0400, root,
1633 					  adev, debugfs_regs[i]);
1634 		if (!i && !IS_ERR_OR_NULL(ent))
1635 			i_size_write(ent->d_inode, adev->rmmio_size);
1636 	}
1637 
1638 	return 0;
1639 }
1640 
1641 static int amdgpu_debugfs_test_ib_show(struct seq_file *m, void *unused)
1642 {
1643 	struct amdgpu_device *adev = m->private;
1644 	struct drm_device *dev = adev_to_drm(adev);
1645 	int r = 0, i;
1646 
1647 	r = pm_runtime_get_sync(dev->dev);
1648 	if (r < 0) {
1649 		pm_runtime_put_autosuspend(dev->dev);
1650 		return r;
1651 	}
1652 
1653 	/* Avoid accidently unparking the sched thread during GPU reset */
1654 	r = down_write_killable(&adev->reset_domain->sem);
1655 	if (r)
1656 		return r;
1657 
1658 	/* hold on the scheduler */
1659 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1660 		struct amdgpu_ring *ring = adev->rings[i];
1661 
1662 		if (!amdgpu_ring_sched_ready(ring))
1663 			continue;
1664 		drm_sched_wqueue_stop(&ring->sched);
1665 	}
1666 
1667 	seq_puts(m, "run ib test:\n");
1668 	r = amdgpu_ib_ring_tests(adev);
1669 	if (r)
1670 		seq_printf(m, "ib ring tests failed (%d).\n", r);
1671 	else
1672 		seq_puts(m, "ib ring tests passed.\n");
1673 
1674 	/* go on the scheduler */
1675 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1676 		struct amdgpu_ring *ring = adev->rings[i];
1677 
1678 		if (!amdgpu_ring_sched_ready(ring))
1679 			continue;
1680 		drm_sched_wqueue_start(&ring->sched);
1681 	}
1682 
1683 	up_write(&adev->reset_domain->sem);
1684 
1685 	pm_runtime_put_autosuspend(dev->dev);
1686 
1687 	return 0;
1688 }
1689 
1690 static int amdgpu_debugfs_evict_vram(void *data, u64 *val)
1691 {
1692 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1693 	struct drm_device *dev = adev_to_drm(adev);
1694 	int r;
1695 
1696 	r = pm_runtime_get_sync(dev->dev);
1697 	if (r < 0) {
1698 		pm_runtime_put_autosuspend(dev->dev);
1699 		return r;
1700 	}
1701 
1702 	*val = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM);
1703 
1704 	pm_runtime_put_autosuspend(dev->dev);
1705 
1706 	return 0;
1707 }
1708 
1709 
1710 static int amdgpu_debugfs_evict_gtt(void *data, u64 *val)
1711 {
1712 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1713 	struct drm_device *dev = adev_to_drm(adev);
1714 	int r;
1715 
1716 	r = pm_runtime_get_sync(dev->dev);
1717 	if (r < 0) {
1718 		pm_runtime_put_autosuspend(dev->dev);
1719 		return r;
1720 	}
1721 
1722 	*val = amdgpu_ttm_evict_resources(adev, TTM_PL_TT);
1723 
1724 	pm_runtime_put_autosuspend(dev->dev);
1725 
1726 	return 0;
1727 }
1728 
1729 static int amdgpu_debugfs_benchmark(void *data, u64 val)
1730 {
1731 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1732 	struct drm_device *dev = adev_to_drm(adev);
1733 	int r;
1734 
1735 	r = pm_runtime_get_sync(dev->dev);
1736 	if (r < 0) {
1737 		pm_runtime_put_autosuspend(dev->dev);
1738 		return r;
1739 	}
1740 
1741 	r = amdgpu_benchmark(adev, val);
1742 
1743 	pm_runtime_put_autosuspend(dev->dev);
1744 
1745 	return r;
1746 }
1747 
1748 static int amdgpu_debugfs_vm_info_show(struct seq_file *m, void *unused)
1749 {
1750 	struct amdgpu_device *adev = m->private;
1751 	struct drm_device *dev = adev_to_drm(adev);
1752 	struct drm_file *file;
1753 	int r;
1754 
1755 	r = mutex_lock_interruptible(&dev->filelist_mutex);
1756 	if (r)
1757 		return r;
1758 
1759 	list_for_each_entry(file, &dev->filelist, lhead) {
1760 		struct amdgpu_fpriv *fpriv = file->driver_priv;
1761 		struct amdgpu_vm *vm = &fpriv->vm;
1762 		struct amdgpu_task_info *ti;
1763 
1764 		ti = amdgpu_vm_get_task_info_vm(vm);
1765 		if (ti) {
1766 			seq_printf(m, "pid:%d\tProcess:%s ----------\n", ti->task.pid, ti->process_name);
1767 			amdgpu_vm_put_task_info(ti);
1768 		}
1769 
1770 		r = amdgpu_bo_reserve(vm->root.bo, true);
1771 		if (r)
1772 			break;
1773 		amdgpu_debugfs_vm_bo_info(vm, m);
1774 		amdgpu_bo_unreserve(vm->root.bo);
1775 	}
1776 
1777 	mutex_unlock(&dev->filelist_mutex);
1778 
1779 	return r;
1780 }
1781 
1782 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_test_ib);
1783 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_vm_info);
1784 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_vram_fops, amdgpu_debugfs_evict_vram,
1785 			 NULL, "%lld\n");
1786 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_gtt_fops, amdgpu_debugfs_evict_gtt,
1787 			 NULL, "%lld\n");
1788 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_benchmark_fops, NULL, amdgpu_debugfs_benchmark,
1789 			 "%lld\n");
1790 
1791 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring,
1792 					  struct dma_fence **fences)
1793 {
1794 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
1795 	uint32_t sync_seq, last_seq;
1796 
1797 	last_seq = atomic_read(&ring->fence_drv.last_seq);
1798 	sync_seq = ring->fence_drv.sync_seq;
1799 
1800 	last_seq &= drv->num_fences_mask;
1801 	sync_seq &= drv->num_fences_mask;
1802 
1803 	do {
1804 		struct dma_fence *fence, **ptr;
1805 
1806 		++last_seq;
1807 		last_seq &= drv->num_fences_mask;
1808 		ptr = &drv->fences[last_seq];
1809 
1810 		fence = rcu_dereference_protected(*ptr, 1);
1811 		RCU_INIT_POINTER(*ptr, NULL);
1812 
1813 		if (!fence)
1814 			continue;
1815 
1816 		fences[last_seq] = fence;
1817 
1818 	} while (last_seq != sync_seq);
1819 }
1820 
1821 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences,
1822 					    int length)
1823 {
1824 	int i;
1825 	struct dma_fence *fence;
1826 
1827 	for (i = 0; i < length; i++) {
1828 		fence = fences[i];
1829 		if (!fence)
1830 			continue;
1831 		dma_fence_signal(fence);
1832 		dma_fence_put(fence);
1833 	}
1834 }
1835 
1836 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched)
1837 {
1838 	struct drm_sched_job *s_job;
1839 	struct dma_fence *fence;
1840 
1841 	spin_lock(&sched->job_list_lock);
1842 	list_for_each_entry(s_job, &sched->pending_list, list) {
1843 		fence = sched->ops->run_job(s_job);
1844 		dma_fence_put(fence);
1845 	}
1846 	spin_unlock(&sched->job_list_lock);
1847 }
1848 
1849 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring)
1850 {
1851 	struct amdgpu_job *job;
1852 	struct drm_sched_job *s_job, *tmp;
1853 	uint32_t preempt_seq;
1854 	struct dma_fence *fence, **ptr;
1855 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
1856 	struct drm_gpu_scheduler *sched = &ring->sched;
1857 	bool preempted = true;
1858 
1859 	if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
1860 		return;
1861 
1862 	preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2));
1863 	if (preempt_seq <= atomic_read(&drv->last_seq)) {
1864 		preempted = false;
1865 		goto no_preempt;
1866 	}
1867 
1868 	preempt_seq &= drv->num_fences_mask;
1869 	ptr = &drv->fences[preempt_seq];
1870 	fence = rcu_dereference_protected(*ptr, 1);
1871 
1872 no_preempt:
1873 	spin_lock(&sched->job_list_lock);
1874 	list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
1875 		if (dma_fence_is_signaled(&s_job->s_fence->finished)) {
1876 			/* remove job from ring_mirror_list */
1877 			list_del_init(&s_job->list);
1878 			sched->ops->free_job(s_job);
1879 			continue;
1880 		}
1881 		job = to_amdgpu_job(s_job);
1882 		if (preempted && (&job->hw_fence->base) == fence)
1883 			/* mark the job as preempted */
1884 			job->preemption_status |= AMDGPU_IB_PREEMPTED;
1885 	}
1886 	spin_unlock(&sched->job_list_lock);
1887 }
1888 
1889 static int amdgpu_debugfs_ib_preempt(void *data, u64 val)
1890 {
1891 	int r, length;
1892 	struct amdgpu_ring *ring;
1893 	struct dma_fence **fences = NULL;
1894 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1895 
1896 	if (val >= AMDGPU_MAX_RINGS)
1897 		return -EINVAL;
1898 
1899 	ring = adev->rings[val];
1900 
1901 	if (!amdgpu_ring_sched_ready(ring) ||
1902 	    !ring->funcs->preempt_ib)
1903 		return -EINVAL;
1904 
1905 	/* the last preemption failed */
1906 	if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr))
1907 		return -EBUSY;
1908 
1909 	length = ring->fence_drv.num_fences_mask + 1;
1910 	fences = kcalloc(length, sizeof(void *), GFP_KERNEL);
1911 	if (!fences)
1912 		return -ENOMEM;
1913 
1914 	/* Avoid accidently unparking the sched thread during GPU reset */
1915 	r = down_read_killable(&adev->reset_domain->sem);
1916 	if (r)
1917 		goto pro_end;
1918 
1919 	/* stop the scheduler */
1920 	drm_sched_wqueue_stop(&ring->sched);
1921 
1922 	/* preempt the IB */
1923 	r = amdgpu_ring_preempt_ib(ring);
1924 	if (r) {
1925 		drm_warn(adev_to_drm(adev), "failed to preempt ring %d\n", ring->idx);
1926 		goto failure;
1927 	}
1928 
1929 	amdgpu_fence_process(ring);
1930 
1931 	if (atomic_read(&ring->fence_drv.last_seq) !=
1932 	    ring->fence_drv.sync_seq) {
1933 		drm_info(adev_to_drm(adev), "ring %d was preempted\n", ring->idx);
1934 
1935 		amdgpu_ib_preempt_mark_partial_job(ring);
1936 
1937 		/* swap out the old fences */
1938 		amdgpu_ib_preempt_fences_swap(ring, fences);
1939 
1940 		amdgpu_fence_driver_force_completion(ring);
1941 
1942 		/* resubmit unfinished jobs */
1943 		amdgpu_ib_preempt_job_recovery(&ring->sched);
1944 
1945 		/* wait for jobs finished */
1946 		amdgpu_fence_wait_empty(ring);
1947 
1948 		/* signal the old fences */
1949 		amdgpu_ib_preempt_signal_fences(fences, length);
1950 	}
1951 
1952 failure:
1953 	/* restart the scheduler */
1954 	drm_sched_wqueue_start(&ring->sched);
1955 
1956 	up_read(&adev->reset_domain->sem);
1957 
1958 pro_end:
1959 	kfree(fences);
1960 
1961 	return r;
1962 }
1963 
1964 static int amdgpu_debugfs_sclk_set(void *data, u64 val)
1965 {
1966 	int ret = 0;
1967 	uint32_t max_freq, min_freq;
1968 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1969 
1970 	if (amdgpu_sriov_multi_vf_mode(adev))
1971 		return -EINVAL;
1972 
1973 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1974 	if (ret < 0) {
1975 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1976 		return ret;
1977 	}
1978 
1979 	ret = amdgpu_dpm_get_dpm_freq_range(adev, PP_SCLK, &min_freq, &max_freq);
1980 	if (ret == -EOPNOTSUPP) {
1981 		ret = 0;
1982 		goto out;
1983 	}
1984 	if (ret || val > max_freq || val < min_freq) {
1985 		ret = -EINVAL;
1986 		goto out;
1987 	}
1988 
1989 	ret = amdgpu_dpm_set_soft_freq_range(adev, PP_SCLK, (uint32_t)val, (uint32_t)val);
1990 	if (ret)
1991 		ret = -EINVAL;
1992 
1993 out:
1994 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1995 
1996 	return ret;
1997 }
1998 
1999 DEFINE_DEBUGFS_ATTRIBUTE(fops_ib_preempt, NULL,
2000 			amdgpu_debugfs_ib_preempt, "%llu\n");
2001 
2002 DEFINE_DEBUGFS_ATTRIBUTE(fops_sclk_set, NULL,
2003 			amdgpu_debugfs_sclk_set, "%llu\n");
2004 
2005 int amdgpu_debugfs_init(struct amdgpu_device *adev)
2006 {
2007 	struct dentry *root = adev_to_drm(adev)->primary->debugfs_root;
2008 	struct dentry *ent;
2009 	int r, i;
2010 
2011 	if (!debugfs_initialized())
2012 		return 0;
2013 
2014 	debugfs_create_x32("amdgpu_smu_debug", 0600, root,
2015 			   &adev->pm.smu_debug_mask);
2016 
2017 	ent = debugfs_create_file("amdgpu_preempt_ib", 0600, root, adev,
2018 				  &fops_ib_preempt);
2019 	if (IS_ERR(ent)) {
2020 		drm_err(adev_to_drm(adev),
2021 			"unable to create amdgpu_preempt_ib debugsfs file\n");
2022 		return PTR_ERR(ent);
2023 	}
2024 
2025 	ent = debugfs_create_file("amdgpu_force_sclk", 0200, root, adev,
2026 				  &fops_sclk_set);
2027 	if (IS_ERR(ent)) {
2028 		drm_err(adev_to_drm(adev),
2029 			"unable to create amdgpu_set_sclk debugsfs file\n");
2030 		return PTR_ERR(ent);
2031 	}
2032 
2033 	/* Register debugfs entries for amdgpu_ttm */
2034 	amdgpu_ttm_debugfs_init(adev);
2035 	amdgpu_debugfs_pm_init(adev);
2036 	amdgpu_debugfs_sa_init(adev);
2037 	amdgpu_debugfs_fence_init(adev);
2038 	amdgpu_debugfs_gem_init(adev);
2039 
2040 	r = amdgpu_debugfs_regs_init(adev);
2041 	if (r)
2042 		drm_err(adev_to_drm(adev), "registering register debugfs failed (%d).\n", r);
2043 
2044 	amdgpu_debugfs_firmware_init(adev);
2045 	amdgpu_ta_if_debugfs_init(adev);
2046 
2047 	amdgpu_debugfs_mes_event_log_init(adev);
2048 
2049 #if defined(CONFIG_DRM_AMD_DC)
2050 	if (adev->dc_enabled)
2051 		dtn_debugfs_init(adev);
2052 #endif
2053 
2054 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2055 		struct amdgpu_ring *ring = adev->rings[i];
2056 
2057 		if (!ring)
2058 			continue;
2059 
2060 		amdgpu_debugfs_ring_init(adev, ring);
2061 	}
2062 
2063 	for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
2064 		if (!amdgpu_vcnfw_log)
2065 			break;
2066 
2067 		if (adev->vcn.harvest_config & (1 << i))
2068 			continue;
2069 
2070 		amdgpu_debugfs_vcn_fwlog_init(adev, i, &adev->vcn.inst[i]);
2071 	}
2072 
2073 	if (amdgpu_umsch_mm & amdgpu_umsch_mm_fwlog)
2074 		amdgpu_debugfs_umsch_fwlog_init(adev, &adev->umsch_mm);
2075 
2076 	amdgpu_debugfs_vcn_sched_mask_init(adev);
2077 	amdgpu_debugfs_jpeg_sched_mask_init(adev);
2078 	amdgpu_debugfs_gfx_sched_mask_init(adev);
2079 	amdgpu_debugfs_compute_sched_mask_init(adev);
2080 	amdgpu_debugfs_sdma_sched_mask_init(adev);
2081 
2082 	amdgpu_ras_debugfs_create_all(adev);
2083 	amdgpu_rap_debugfs_init(adev);
2084 	amdgpu_securedisplay_debugfs_init(adev);
2085 	amdgpu_fw_attestation_debugfs_init(adev);
2086 	amdgpu_psp_debugfs_init(adev);
2087 
2088 	debugfs_create_file("amdgpu_evict_vram", 0400, root, adev,
2089 			    &amdgpu_evict_vram_fops);
2090 	debugfs_create_file("amdgpu_evict_gtt", 0400, root, adev,
2091 			    &amdgpu_evict_gtt_fops);
2092 	debugfs_create_file("amdgpu_test_ib", 0400, root, adev,
2093 			    &amdgpu_debugfs_test_ib_fops);
2094 	debugfs_create_file("amdgpu_vm_info", 0444, root, adev,
2095 			    &amdgpu_debugfs_vm_info_fops);
2096 	debugfs_create_file("amdgpu_benchmark", 0200, root, adev,
2097 			    &amdgpu_benchmark_fops);
2098 
2099 	adev->debugfs_vbios_blob.data = adev->bios;
2100 	adev->debugfs_vbios_blob.size = adev->bios_size;
2101 	debugfs_create_blob("amdgpu_vbios", 0444, root,
2102 			    &adev->debugfs_vbios_blob);
2103 
2104 	if (adev->discovery.debugfs_blob.size)
2105 		debugfs_create_blob("amdgpu_discovery", 0444, root,
2106 				    &adev->discovery.debugfs_blob);
2107 
2108 	return 0;
2109 }
2110 
2111 static int amdgpu_pt_info_read(struct seq_file *m, void *unused)
2112 {
2113 	struct drm_file *file;
2114 	struct amdgpu_fpriv *fpriv;
2115 	struct amdgpu_bo *root_bo;
2116 	struct amdgpu_device *adev;
2117 	int r;
2118 
2119 	file = m->private;
2120 	if (!file)
2121 		return -EINVAL;
2122 
2123 	adev = drm_to_adev(file->minor->dev);
2124 	fpriv = file->driver_priv;
2125 	if (!fpriv || !fpriv->vm.root.bo)
2126 		return -ENODEV;
2127 
2128 	root_bo = amdgpu_bo_ref(fpriv->vm.root.bo);
2129 	r = amdgpu_bo_reserve(root_bo, true);
2130 	if (r) {
2131 		amdgpu_bo_unref(&root_bo);
2132 		return -EINVAL;
2133 	}
2134 
2135 	seq_printf(m, "pd_address: 0x%llx\n", amdgpu_gmc_pd_addr(fpriv->vm.root.bo));
2136 	seq_printf(m, "max_pfn: 0x%llx\n", adev->vm_manager.max_pfn);
2137 	seq_printf(m, "num_level: 0x%x\n", adev->vm_manager.num_level);
2138 	seq_printf(m, "block_size: 0x%x\n", adev->vm_manager.block_size);
2139 	seq_printf(m, "fragment_size: 0x%x\n", adev->vm_manager.fragment_size);
2140 
2141 	amdgpu_bo_unreserve(root_bo);
2142 	amdgpu_bo_unref(&root_bo);
2143 
2144 	return 0;
2145 }
2146 
2147 static int amdgpu_pt_info_open(struct inode *inode, struct file *file)
2148 {
2149 	return single_open(file, amdgpu_pt_info_read, inode->i_private);
2150 }
2151 
2152 static const struct file_operations amdgpu_pt_info_fops = {
2153 	.owner = THIS_MODULE,
2154 	.open = amdgpu_pt_info_open,
2155 	.read = seq_read,
2156 	.llseek = seq_lseek,
2157 	.release = single_release,
2158 };
2159 
2160 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused)
2161 {
2162 	struct amdgpu_usermode_queue *queue = m->private;
2163 	struct amdgpu_bo *bo;
2164 	int r;
2165 
2166 	if (!queue || !queue->mqd.obj)
2167 		return -EINVAL;
2168 
2169 	bo = amdgpu_bo_ref(queue->mqd.obj);
2170 	r = amdgpu_bo_reserve(bo, true);
2171 	if (r) {
2172 		amdgpu_bo_unref(&bo);
2173 		return -EINVAL;
2174 	}
2175 
2176 	seq_printf(m, "queue_type: %d\n", queue->queue_type);
2177 	seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj));
2178 
2179 	amdgpu_bo_unreserve(bo);
2180 	amdgpu_bo_unref(&bo);
2181 
2182 	return 0;
2183 }
2184 
2185 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file)
2186 {
2187 	return single_open(file, amdgpu_mqd_info_read, inode->i_private);
2188 }
2189 
2190 static const struct file_operations amdgpu_mqd_info_fops = {
2191 	.owner = THIS_MODULE,
2192 	.open = amdgpu_mqd_info_open,
2193 	.read = seq_read,
2194 	.llseek = seq_lseek,
2195 	.release = single_release,
2196 };
2197 
2198 void amdgpu_debugfs_userq_init(struct drm_file *file, struct amdgpu_usermode_queue *queue, int qid)
2199 {
2200 	char queue_name[32];
2201 
2202 	scnprintf(queue_name, sizeof(queue_name), "queue_%d", qid);
2203 	queue->debugfs_queue = debugfs_create_dir(queue_name, file->debugfs_client);
2204 	debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops);
2205 }
2206 
2207 void amdgpu_debugfs_vm_init(struct drm_file *file)
2208 {
2209 	debugfs_create_file("vm_pagetable_info", 0444, file->debugfs_client, file,
2210 			    &amdgpu_pt_info_fops);
2211 }
2212 
2213 #else
2214 int amdgpu_debugfs_init(struct amdgpu_device *adev)
2215 {
2216 	return 0;
2217 }
2218 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2219 {
2220 	return 0;
2221 }
2222 void amdgpu_debugfs_vm_init(struct drm_file *file)
2223 {
2224 }
2225 void amdgpu_debugfs_userq_init(struct drm_file *file,
2226 			       struct amdgpu_usermode_queue *queue,
2227 			       int qid)
2228 {
2229 }
2230 #endif
2231