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