xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_debugfs.c (revision f84b1df5a16cdd762c93854218de246e79975d3b)
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 
39 #if defined(CONFIG_DEBUG_FS)
40 
41 /**
42  * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes
43  *
44  * @read: True if reading
45  * @f: open file handle
46  * @buf: User buffer to write/read to
47  * @size: Number of bytes to write/read
48  * @pos:  Offset to seek to
49  *
50  * This debugfs entry has special meaning on the offset being sought.
51  * Various bits have different meanings:
52  *
53  * Bit 62:  Indicates a GRBM bank switch is needed
54  * Bit 61:  Indicates a SRBM bank switch is needed (implies bit 62 is
55  * 	    zero)
56  * Bits 24..33: The SE or ME selector if needed
57  * Bits 34..43: The SH (or SA) or PIPE selector if needed
58  * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed
59  *
60  * Bit 23:  Indicates that the PM power gating lock should be held
61  * 	    This is necessary to read registers that might be
62  * 	    unreliable during a power gating transistion.
63  *
64  * The lower bits are the BYTE offset of the register to read.  This
65  * allows reading multiple registers in a single call and having
66  * the returned size reflect that.
67  */
68 static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
69 		char __user *buf, size_t size, loff_t *pos)
70 {
71 	struct amdgpu_device *adev = file_inode(f)->i_private;
72 	ssize_t result = 0;
73 	int r;
74 	bool pm_pg_lock, use_bank, use_ring;
75 	unsigned instance_bank, sh_bank, se_bank, me, pipe, queue, vmid;
76 
77 	pm_pg_lock = use_bank = use_ring = false;
78 	instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0;
79 
80 	if (size & 0x3 || *pos & 0x3 ||
81 			((*pos & (1ULL << 62)) && (*pos & (1ULL << 61))))
82 		return -EINVAL;
83 
84 	/* are we reading registers for which a PG lock is necessary? */
85 	pm_pg_lock = (*pos >> 23) & 1;
86 
87 	if (*pos & (1ULL << 62)) {
88 		se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
89 		sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
90 		instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
91 
92 		if (se_bank == 0x3FF)
93 			se_bank = 0xFFFFFFFF;
94 		if (sh_bank == 0x3FF)
95 			sh_bank = 0xFFFFFFFF;
96 		if (instance_bank == 0x3FF)
97 			instance_bank = 0xFFFFFFFF;
98 		use_bank = true;
99 	} else if (*pos & (1ULL << 61)) {
100 
101 		me = (*pos & GENMASK_ULL(33, 24)) >> 24;
102 		pipe = (*pos & GENMASK_ULL(43, 34)) >> 34;
103 		queue = (*pos & GENMASK_ULL(53, 44)) >> 44;
104 		vmid = (*pos & GENMASK_ULL(58, 54)) >> 54;
105 
106 		use_ring = true;
107 	} else {
108 		use_bank = use_ring = false;
109 	}
110 
111 	*pos &= (1UL << 22) - 1;
112 
113 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
114 	if (r < 0) {
115 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
116 		return r;
117 	}
118 
119 	r = amdgpu_virt_enable_access_debugfs(adev);
120 	if (r < 0) {
121 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
122 		return r;
123 	}
124 
125 	if (use_bank) {
126 		if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
127 		    (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) {
128 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
129 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
130 			amdgpu_virt_disable_access_debugfs(adev);
131 			return -EINVAL;
132 		}
133 		mutex_lock(&adev->grbm_idx_mutex);
134 		amdgpu_gfx_select_se_sh(adev, se_bank,
135 					sh_bank, instance_bank);
136 	} else if (use_ring) {
137 		mutex_lock(&adev->srbm_mutex);
138 		amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid);
139 	}
140 
141 	if (pm_pg_lock)
142 		mutex_lock(&adev->pm.mutex);
143 
144 	while (size) {
145 		uint32_t value;
146 
147 		if (read) {
148 			value = RREG32(*pos >> 2);
149 			r = put_user(value, (uint32_t *)buf);
150 		} else {
151 			r = get_user(value, (uint32_t *)buf);
152 			if (!r)
153 				amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value);
154 		}
155 		if (r) {
156 			result = r;
157 			goto end;
158 		}
159 
160 		result += 4;
161 		buf += 4;
162 		*pos += 4;
163 		size -= 4;
164 	}
165 
166 end:
167 	if (use_bank) {
168 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
169 		mutex_unlock(&adev->grbm_idx_mutex);
170 	} else if (use_ring) {
171 		amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0);
172 		mutex_unlock(&adev->srbm_mutex);
173 	}
174 
175 	if (pm_pg_lock)
176 		mutex_unlock(&adev->pm.mutex);
177 
178 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
179 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
180 
181 	amdgpu_virt_disable_access_debugfs(adev);
182 	return result;
183 }
184 
185 /*
186  * amdgpu_debugfs_regs_read - Callback for reading MMIO registers
187  */
188 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
189 					size_t size, loff_t *pos)
190 {
191 	return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos);
192 }
193 
194 /*
195  * amdgpu_debugfs_regs_write - Callback for writing MMIO registers
196  */
197 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
198 					 size_t size, loff_t *pos)
199 {
200 	return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos);
201 }
202 
203 
204 /**
205  * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register
206  *
207  * @f: open file handle
208  * @buf: User buffer to store read data in
209  * @size: Number of bytes to read
210  * @pos:  Offset to seek to
211  *
212  * The lower bits are the BYTE offset of the register to read.  This
213  * allows reading multiple registers in a single call and having
214  * the returned size reflect that.
215  */
216 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
217 					size_t size, loff_t *pos)
218 {
219 	struct amdgpu_device *adev = file_inode(f)->i_private;
220 	ssize_t result = 0;
221 	int r;
222 
223 	if (size & 0x3 || *pos & 0x3)
224 		return -EINVAL;
225 
226 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
227 	if (r < 0) {
228 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
229 		return r;
230 	}
231 
232 	r = amdgpu_virt_enable_access_debugfs(adev);
233 	if (r < 0) {
234 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
235 		return r;
236 	}
237 
238 	while (size) {
239 		uint32_t value;
240 
241 		value = RREG32_PCIE(*pos);
242 		r = put_user(value, (uint32_t *)buf);
243 		if (r) {
244 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
245 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
246 			amdgpu_virt_disable_access_debugfs(adev);
247 			return r;
248 		}
249 
250 		result += 4;
251 		buf += 4;
252 		*pos += 4;
253 		size -= 4;
254 	}
255 
256 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
257 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
258 
259 	amdgpu_virt_disable_access_debugfs(adev);
260 	return result;
261 }
262 
263 /**
264  * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register
265  *
266  * @f: open file handle
267  * @buf: User buffer to write data from
268  * @size: Number of bytes to write
269  * @pos:  Offset to seek to
270  *
271  * The lower bits are the BYTE offset of the register to write.  This
272  * allows writing multiple registers in a single call and having
273  * the returned size reflect that.
274  */
275 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
276 					 size_t size, loff_t *pos)
277 {
278 	struct amdgpu_device *adev = file_inode(f)->i_private;
279 	ssize_t result = 0;
280 	int r;
281 
282 	if (size & 0x3 || *pos & 0x3)
283 		return -EINVAL;
284 
285 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
286 	if (r < 0) {
287 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
288 		return r;
289 	}
290 
291 	r = amdgpu_virt_enable_access_debugfs(adev);
292 	if (r < 0) {
293 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
294 		return r;
295 	}
296 
297 	while (size) {
298 		uint32_t value;
299 
300 		r = get_user(value, (uint32_t *)buf);
301 		if (r) {
302 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
303 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
304 			amdgpu_virt_disable_access_debugfs(adev);
305 			return r;
306 		}
307 
308 		WREG32_PCIE(*pos, value);
309 
310 		result += 4;
311 		buf += 4;
312 		*pos += 4;
313 		size -= 4;
314 	}
315 
316 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
317 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
318 
319 	amdgpu_virt_disable_access_debugfs(adev);
320 	return result;
321 }
322 
323 /**
324  * amdgpu_debugfs_regs_didt_read - Read from a DIDT register
325  *
326  * @f: open file handle
327  * @buf: User buffer to store read data in
328  * @size: Number of bytes to read
329  * @pos:  Offset to seek to
330  *
331  * The lower bits are the BYTE offset of the register to read.  This
332  * allows reading multiple registers in a single call and having
333  * the returned size reflect that.
334  */
335 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
336 					size_t size, loff_t *pos)
337 {
338 	struct amdgpu_device *adev = file_inode(f)->i_private;
339 	ssize_t result = 0;
340 	int r;
341 
342 	if (size & 0x3 || *pos & 0x3)
343 		return -EINVAL;
344 
345 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
346 	if (r < 0) {
347 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
348 		return r;
349 	}
350 
351 	r = amdgpu_virt_enable_access_debugfs(adev);
352 	if (r < 0) {
353 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
354 		return r;
355 	}
356 
357 	while (size) {
358 		uint32_t value;
359 
360 		value = RREG32_DIDT(*pos >> 2);
361 		r = put_user(value, (uint32_t *)buf);
362 		if (r) {
363 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
364 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
365 			amdgpu_virt_disable_access_debugfs(adev);
366 			return r;
367 		}
368 
369 		result += 4;
370 		buf += 4;
371 		*pos += 4;
372 		size -= 4;
373 	}
374 
375 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
376 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
377 
378 	amdgpu_virt_disable_access_debugfs(adev);
379 	return result;
380 }
381 
382 /**
383  * amdgpu_debugfs_regs_didt_write - Write to a DIDT register
384  *
385  * @f: open file handle
386  * @buf: User buffer to write data from
387  * @size: Number of bytes to write
388  * @pos:  Offset to seek to
389  *
390  * The lower bits are the BYTE offset of the register to write.  This
391  * allows writing multiple registers in a single call and having
392  * the returned size reflect that.
393  */
394 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
395 					 size_t size, loff_t *pos)
396 {
397 	struct amdgpu_device *adev = file_inode(f)->i_private;
398 	ssize_t result = 0;
399 	int r;
400 
401 	if (size & 0x3 || *pos & 0x3)
402 		return -EINVAL;
403 
404 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
405 	if (r < 0) {
406 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
407 		return r;
408 	}
409 
410 	r = amdgpu_virt_enable_access_debugfs(adev);
411 	if (r < 0) {
412 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
413 		return r;
414 	}
415 
416 	while (size) {
417 		uint32_t value;
418 
419 		r = get_user(value, (uint32_t *)buf);
420 		if (r) {
421 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
422 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
423 			amdgpu_virt_disable_access_debugfs(adev);
424 			return r;
425 		}
426 
427 		WREG32_DIDT(*pos >> 2, value);
428 
429 		result += 4;
430 		buf += 4;
431 		*pos += 4;
432 		size -= 4;
433 	}
434 
435 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
436 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
437 
438 	amdgpu_virt_disable_access_debugfs(adev);
439 	return result;
440 }
441 
442 /**
443  * amdgpu_debugfs_regs_smc_read - Read from a SMC register
444  *
445  * @f: open file handle
446  * @buf: User buffer to store read data in
447  * @size: Number of bytes to read
448  * @pos:  Offset to seek to
449  *
450  * The lower bits are the BYTE offset of the register to read.  This
451  * allows reading multiple registers in a single call and having
452  * the returned size reflect that.
453  */
454 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
455 					size_t size, loff_t *pos)
456 {
457 	struct amdgpu_device *adev = file_inode(f)->i_private;
458 	ssize_t result = 0;
459 	int r;
460 
461 	if (size & 0x3 || *pos & 0x3)
462 		return -EINVAL;
463 
464 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
465 	if (r < 0) {
466 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
467 		return r;
468 	}
469 
470 	r = amdgpu_virt_enable_access_debugfs(adev);
471 	if (r < 0) {
472 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
473 		return r;
474 	}
475 
476 	while (size) {
477 		uint32_t value;
478 
479 		value = RREG32_SMC(*pos);
480 		r = put_user(value, (uint32_t *)buf);
481 		if (r) {
482 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
483 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
484 			amdgpu_virt_disable_access_debugfs(adev);
485 			return r;
486 		}
487 
488 		result += 4;
489 		buf += 4;
490 		*pos += 4;
491 		size -= 4;
492 	}
493 
494 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
495 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
496 
497 	amdgpu_virt_disable_access_debugfs(adev);
498 	return result;
499 }
500 
501 /**
502  * amdgpu_debugfs_regs_smc_write - Write to a SMC register
503  *
504  * @f: open file handle
505  * @buf: User buffer to write data from
506  * @size: Number of bytes to write
507  * @pos:  Offset to seek to
508  *
509  * The lower bits are the BYTE offset of the register to write.  This
510  * allows writing multiple registers in a single call and having
511  * the returned size reflect that.
512  */
513 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
514 					 size_t size, loff_t *pos)
515 {
516 	struct amdgpu_device *adev = file_inode(f)->i_private;
517 	ssize_t result = 0;
518 	int r;
519 
520 	if (size & 0x3 || *pos & 0x3)
521 		return -EINVAL;
522 
523 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
524 	if (r < 0) {
525 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
526 		return r;
527 	}
528 
529 	r = amdgpu_virt_enable_access_debugfs(adev);
530 	if (r < 0) {
531 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
532 		return r;
533 	}
534 
535 	while (size) {
536 		uint32_t value;
537 
538 		r = get_user(value, (uint32_t *)buf);
539 		if (r) {
540 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
541 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
542 			amdgpu_virt_disable_access_debugfs(adev);
543 			return r;
544 		}
545 
546 		WREG32_SMC(*pos, value);
547 
548 		result += 4;
549 		buf += 4;
550 		*pos += 4;
551 		size -= 4;
552 	}
553 
554 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
555 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
556 
557 	amdgpu_virt_disable_access_debugfs(adev);
558 	return result;
559 }
560 
561 /**
562  * amdgpu_debugfs_gca_config_read - Read from gfx config data
563  *
564  * @f: open file handle
565  * @buf: User buffer to store read data in
566  * @size: Number of bytes to read
567  * @pos:  Offset to seek to
568  *
569  * This file is used to access configuration data in a somewhat
570  * stable fashion.  The format is a series of DWORDs with the first
571  * indicating which revision it is.  New content is appended to the
572  * end so that older software can still read the data.
573  */
574 
575 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
576 					size_t size, loff_t *pos)
577 {
578 	struct amdgpu_device *adev = file_inode(f)->i_private;
579 	ssize_t result = 0;
580 	int r;
581 	uint32_t *config, no_regs = 0;
582 
583 	if (size & 0x3 || *pos & 0x3)
584 		return -EINVAL;
585 
586 	config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
587 	if (!config)
588 		return -ENOMEM;
589 
590 	/* version, increment each time something is added */
591 	config[no_regs++] = 3;
592 	config[no_regs++] = adev->gfx.config.max_shader_engines;
593 	config[no_regs++] = adev->gfx.config.max_tile_pipes;
594 	config[no_regs++] = adev->gfx.config.max_cu_per_sh;
595 	config[no_regs++] = adev->gfx.config.max_sh_per_se;
596 	config[no_regs++] = adev->gfx.config.max_backends_per_se;
597 	config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
598 	config[no_regs++] = adev->gfx.config.max_gprs;
599 	config[no_regs++] = adev->gfx.config.max_gs_threads;
600 	config[no_regs++] = adev->gfx.config.max_hw_contexts;
601 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
602 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
603 	config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
604 	config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
605 	config[no_regs++] = adev->gfx.config.num_tile_pipes;
606 	config[no_regs++] = adev->gfx.config.backend_enable_mask;
607 	config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
608 	config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
609 	config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
610 	config[no_regs++] = adev->gfx.config.num_gpus;
611 	config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
612 	config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
613 	config[no_regs++] = adev->gfx.config.gb_addr_config;
614 	config[no_regs++] = adev->gfx.config.num_rbs;
615 
616 	/* rev==1 */
617 	config[no_regs++] = adev->rev_id;
618 	config[no_regs++] = adev->pg_flags;
619 	config[no_regs++] = adev->cg_flags;
620 
621 	/* rev==2 */
622 	config[no_regs++] = adev->family;
623 	config[no_regs++] = adev->external_rev_id;
624 
625 	/* rev==3 */
626 	config[no_regs++] = adev->pdev->device;
627 	config[no_regs++] = adev->pdev->revision;
628 	config[no_regs++] = adev->pdev->subsystem_device;
629 	config[no_regs++] = adev->pdev->subsystem_vendor;
630 
631 	while (size && (*pos < no_regs * 4)) {
632 		uint32_t value;
633 
634 		value = config[*pos >> 2];
635 		r = put_user(value, (uint32_t *)buf);
636 		if (r) {
637 			kfree(config);
638 			return r;
639 		}
640 
641 		result += 4;
642 		buf += 4;
643 		*pos += 4;
644 		size -= 4;
645 	}
646 
647 	kfree(config);
648 	return result;
649 }
650 
651 /**
652  * amdgpu_debugfs_sensor_read - Read from the powerplay sensors
653  *
654  * @f: open file handle
655  * @buf: User buffer to store read data in
656  * @size: Number of bytes to read
657  * @pos:  Offset to seek to
658  *
659  * The offset is treated as the BYTE address of one of the sensors
660  * enumerated in amd/include/kgd_pp_interface.h under the
661  * 'amd_pp_sensors' enumeration.  For instance to read the UVD VCLK
662  * you would use the offset 3 * 4 = 12.
663  */
664 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
665 					size_t size, loff_t *pos)
666 {
667 	struct amdgpu_device *adev = file_inode(f)->i_private;
668 	int idx, x, outsize, r, valuesize;
669 	uint32_t values[16];
670 
671 	if (size & 3 || *pos & 0x3)
672 		return -EINVAL;
673 
674 	if (!adev->pm.dpm_enabled)
675 		return -EINVAL;
676 
677 	/* convert offset to sensor number */
678 	idx = *pos >> 2;
679 
680 	valuesize = sizeof(values);
681 
682 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
683 	if (r < 0) {
684 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
685 		return r;
686 	}
687 
688 	r = amdgpu_virt_enable_access_debugfs(adev);
689 	if (r < 0) {
690 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
691 		return r;
692 	}
693 
694 	r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
695 
696 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
697 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
698 
699 	if (r) {
700 		amdgpu_virt_disable_access_debugfs(adev);
701 		return r;
702 	}
703 
704 	if (size > valuesize) {
705 		amdgpu_virt_disable_access_debugfs(adev);
706 		return -EINVAL;
707 	}
708 
709 	outsize = 0;
710 	x = 0;
711 	if (!r) {
712 		while (size) {
713 			r = put_user(values[x++], (int32_t *)buf);
714 			buf += 4;
715 			size -= 4;
716 			outsize += 4;
717 		}
718 	}
719 
720 	amdgpu_virt_disable_access_debugfs(adev);
721 	return !r ? outsize : r;
722 }
723 
724 /** amdgpu_debugfs_wave_read - Read WAVE STATUS data
725  *
726  * @f: open file handle
727  * @buf: User buffer to store read data in
728  * @size: Number of bytes to read
729  * @pos:  Offset to seek to
730  *
731  * The offset being sought changes which wave that the status data
732  * will be returned for.  The bits are used as follows:
733  *
734  * Bits 0..6: 	Byte offset into data
735  * Bits 7..14:	SE selector
736  * Bits 15..22:	SH/SA selector
737  * Bits 23..30: CU/{WGP+SIMD} selector
738  * Bits 31..36: WAVE ID selector
739  * Bits 37..44: SIMD ID selector
740  *
741  * The returned data begins with one DWORD of version information
742  * Followed by WAVE STATUS registers relevant to the GFX IP version
743  * being used.  See gfx_v8_0_read_wave_data() for an example output.
744  */
745 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
746 					size_t size, loff_t *pos)
747 {
748 	struct amdgpu_device *adev = f->f_inode->i_private;
749 	int r, x;
750 	ssize_t result = 0;
751 	uint32_t offset, se, sh, cu, wave, simd, data[32];
752 
753 	if (size & 3 || *pos & 3)
754 		return -EINVAL;
755 
756 	/* decode offset */
757 	offset = (*pos & GENMASK_ULL(6, 0));
758 	se = (*pos & GENMASK_ULL(14, 7)) >> 7;
759 	sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
760 	cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
761 	wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
762 	simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
763 
764 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
765 	if (r < 0) {
766 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
767 		return r;
768 	}
769 
770 	r = amdgpu_virt_enable_access_debugfs(adev);
771 	if (r < 0) {
772 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
773 		return r;
774 	}
775 
776 	/* switch to the specific se/sh/cu */
777 	mutex_lock(&adev->grbm_idx_mutex);
778 	amdgpu_gfx_select_se_sh(adev, se, sh, cu);
779 
780 	x = 0;
781 	if (adev->gfx.funcs->read_wave_data)
782 		adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
783 
784 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
785 	mutex_unlock(&adev->grbm_idx_mutex);
786 
787 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
788 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
789 
790 	if (!x) {
791 		amdgpu_virt_disable_access_debugfs(adev);
792 		return -EINVAL;
793 	}
794 
795 	while (size && (offset < x * 4)) {
796 		uint32_t value;
797 
798 		value = data[offset >> 2];
799 		r = put_user(value, (uint32_t *)buf);
800 		if (r) {
801 			amdgpu_virt_disable_access_debugfs(adev);
802 			return r;
803 		}
804 
805 		result += 4;
806 		buf += 4;
807 		offset += 4;
808 		size -= 4;
809 	}
810 
811 	amdgpu_virt_disable_access_debugfs(adev);
812 	return result;
813 }
814 
815 /** amdgpu_debugfs_gpr_read - Read wave gprs
816  *
817  * @f: open file handle
818  * @buf: User buffer to store read data in
819  * @size: Number of bytes to read
820  * @pos:  Offset to seek to
821  *
822  * The offset being sought changes which wave that the status data
823  * will be returned for.  The bits are used as follows:
824  *
825  * Bits 0..11:	Byte offset into data
826  * Bits 12..19:	SE selector
827  * Bits 20..27:	SH/SA selector
828  * Bits 28..35: CU/{WGP+SIMD} selector
829  * Bits 36..43: WAVE ID selector
830  * Bits 37..44: SIMD ID selector
831  * Bits 52..59: Thread selector
832  * Bits 60..61: Bank selector (VGPR=0,SGPR=1)
833  *
834  * The return data comes from the SGPR or VGPR register bank for
835  * the selected operational unit.
836  */
837 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
838 					size_t size, loff_t *pos)
839 {
840 	struct amdgpu_device *adev = f->f_inode->i_private;
841 	int r;
842 	ssize_t result = 0;
843 	uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
844 
845 	if (size > 4096 || size & 3 || *pos & 3)
846 		return -EINVAL;
847 
848 	/* decode offset */
849 	offset = (*pos & GENMASK_ULL(11, 0)) >> 2;
850 	se = (*pos & GENMASK_ULL(19, 12)) >> 12;
851 	sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
852 	cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
853 	wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
854 	simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
855 	thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
856 	bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
857 
858 	data = kcalloc(1024, sizeof(*data), GFP_KERNEL);
859 	if (!data)
860 		return -ENOMEM;
861 
862 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
863 	if (r < 0)
864 		goto err;
865 
866 	r = amdgpu_virt_enable_access_debugfs(adev);
867 	if (r < 0)
868 		goto err;
869 
870 	/* switch to the specific se/sh/cu */
871 	mutex_lock(&adev->grbm_idx_mutex);
872 	amdgpu_gfx_select_se_sh(adev, se, sh, cu);
873 
874 	if (bank == 0) {
875 		if (adev->gfx.funcs->read_wave_vgprs)
876 			adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
877 	} else {
878 		if (adev->gfx.funcs->read_wave_sgprs)
879 			adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
880 	}
881 
882 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
883 	mutex_unlock(&adev->grbm_idx_mutex);
884 
885 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
886 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
887 
888 	while (size) {
889 		uint32_t value;
890 
891 		value = data[result >> 2];
892 		r = put_user(value, (uint32_t *)buf);
893 		if (r) {
894 			amdgpu_virt_disable_access_debugfs(adev);
895 			goto err;
896 		}
897 
898 		result += 4;
899 		buf += 4;
900 		size -= 4;
901 	}
902 
903 	kfree(data);
904 	amdgpu_virt_disable_access_debugfs(adev);
905 	return result;
906 
907 err:
908 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
909 	kfree(data);
910 	return r;
911 }
912 
913 /**
914  * amdgpu_debugfs_gfxoff_write - Enable/disable GFXOFF
915  *
916  * @f: open file handle
917  * @buf: User buffer to write data from
918  * @size: Number of bytes to write
919  * @pos:  Offset to seek to
920  *
921  * Write a 32-bit zero to disable or a 32-bit non-zero to enable
922  */
923 static ssize_t amdgpu_debugfs_gfxoff_write(struct file *f, const char __user *buf,
924 					 size_t size, loff_t *pos)
925 {
926 	struct amdgpu_device *adev = file_inode(f)->i_private;
927 	ssize_t result = 0;
928 	int r;
929 
930 	if (size & 0x3 || *pos & 0x3)
931 		return -EINVAL;
932 
933 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
934 	if (r < 0) {
935 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
936 		return r;
937 	}
938 
939 	while (size) {
940 		uint32_t value;
941 
942 		r = get_user(value, (uint32_t *)buf);
943 		if (r) {
944 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
945 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
946 			return r;
947 		}
948 
949 		amdgpu_gfx_off_ctrl(adev, value ? true : false);
950 
951 		result += 4;
952 		buf += 4;
953 		*pos += 4;
954 		size -= 4;
955 	}
956 
957 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
958 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
959 
960 	return result;
961 }
962 
963 
964 /**
965  * amdgpu_debugfs_gfxoff_read - read gfxoff status
966  *
967  * @f: open file handle
968  * @buf: User buffer to store read data in
969  * @size: Number of bytes to read
970  * @pos:  Offset to seek to
971  */
972 static ssize_t amdgpu_debugfs_gfxoff_read(struct file *f, char __user *buf,
973 					 size_t size, loff_t *pos)
974 {
975 	struct amdgpu_device *adev = file_inode(f)->i_private;
976 	ssize_t result = 0;
977 	int r;
978 
979 	if (size & 0x3 || *pos & 0x3)
980 		return -EINVAL;
981 
982 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
983 	if (r < 0)
984 		return r;
985 
986 	while (size) {
987 		uint32_t value;
988 
989 		r = amdgpu_get_gfx_off_status(adev, &value);
990 		if (r) {
991 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
992 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
993 			return r;
994 		}
995 
996 		r = put_user(value, (uint32_t *)buf);
997 		if (r) {
998 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
999 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1000 			return r;
1001 		}
1002 
1003 		result += 4;
1004 		buf += 4;
1005 		*pos += 4;
1006 		size -= 4;
1007 	}
1008 
1009 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1010 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1011 
1012 	return result;
1013 }
1014 
1015 static const struct file_operations amdgpu_debugfs_regs_fops = {
1016 	.owner = THIS_MODULE,
1017 	.read = amdgpu_debugfs_regs_read,
1018 	.write = amdgpu_debugfs_regs_write,
1019 	.llseek = default_llseek
1020 };
1021 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
1022 	.owner = THIS_MODULE,
1023 	.read = amdgpu_debugfs_regs_didt_read,
1024 	.write = amdgpu_debugfs_regs_didt_write,
1025 	.llseek = default_llseek
1026 };
1027 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
1028 	.owner = THIS_MODULE,
1029 	.read = amdgpu_debugfs_regs_pcie_read,
1030 	.write = amdgpu_debugfs_regs_pcie_write,
1031 	.llseek = default_llseek
1032 };
1033 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
1034 	.owner = THIS_MODULE,
1035 	.read = amdgpu_debugfs_regs_smc_read,
1036 	.write = amdgpu_debugfs_regs_smc_write,
1037 	.llseek = default_llseek
1038 };
1039 
1040 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
1041 	.owner = THIS_MODULE,
1042 	.read = amdgpu_debugfs_gca_config_read,
1043 	.llseek = default_llseek
1044 };
1045 
1046 static const struct file_operations amdgpu_debugfs_sensors_fops = {
1047 	.owner = THIS_MODULE,
1048 	.read = amdgpu_debugfs_sensor_read,
1049 	.llseek = default_llseek
1050 };
1051 
1052 static const struct file_operations amdgpu_debugfs_wave_fops = {
1053 	.owner = THIS_MODULE,
1054 	.read = amdgpu_debugfs_wave_read,
1055 	.llseek = default_llseek
1056 };
1057 static const struct file_operations amdgpu_debugfs_gpr_fops = {
1058 	.owner = THIS_MODULE,
1059 	.read = amdgpu_debugfs_gpr_read,
1060 	.llseek = default_llseek
1061 };
1062 
1063 static const struct file_operations amdgpu_debugfs_gfxoff_fops = {
1064 	.owner = THIS_MODULE,
1065 	.read = amdgpu_debugfs_gfxoff_read,
1066 	.write = amdgpu_debugfs_gfxoff_write,
1067 	.llseek = default_llseek
1068 };
1069 
1070 static const struct file_operations *debugfs_regs[] = {
1071 	&amdgpu_debugfs_regs_fops,
1072 	&amdgpu_debugfs_regs_didt_fops,
1073 	&amdgpu_debugfs_regs_pcie_fops,
1074 	&amdgpu_debugfs_regs_smc_fops,
1075 	&amdgpu_debugfs_gca_config_fops,
1076 	&amdgpu_debugfs_sensors_fops,
1077 	&amdgpu_debugfs_wave_fops,
1078 	&amdgpu_debugfs_gpr_fops,
1079 	&amdgpu_debugfs_gfxoff_fops,
1080 };
1081 
1082 static const char *debugfs_regs_names[] = {
1083 	"amdgpu_regs",
1084 	"amdgpu_regs_didt",
1085 	"amdgpu_regs_pcie",
1086 	"amdgpu_regs_smc",
1087 	"amdgpu_gca_config",
1088 	"amdgpu_sensors",
1089 	"amdgpu_wave",
1090 	"amdgpu_gpr",
1091 	"amdgpu_gfxoff",
1092 };
1093 
1094 /**
1095  * amdgpu_debugfs_regs_init -	Initialize debugfs entries that provide
1096  * 				register access.
1097  *
1098  * @adev: The device to attach the debugfs entries to
1099  */
1100 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
1101 {
1102 	struct drm_minor *minor = adev_to_drm(adev)->primary;
1103 	struct dentry *ent, *root = minor->debugfs_root;
1104 	unsigned int i;
1105 
1106 	for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
1107 		ent = debugfs_create_file(debugfs_regs_names[i],
1108 					  S_IFREG | S_IRUGO, root,
1109 					  adev, debugfs_regs[i]);
1110 		if (!i && !IS_ERR_OR_NULL(ent))
1111 			i_size_write(ent->d_inode, adev->rmmio_size);
1112 	}
1113 
1114 	return 0;
1115 }
1116 
1117 static int amdgpu_debugfs_test_ib_show(struct seq_file *m, void *unused)
1118 {
1119 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
1120 	struct drm_device *dev = adev_to_drm(adev);
1121 	int r = 0, i;
1122 
1123 	r = pm_runtime_get_sync(dev->dev);
1124 	if (r < 0) {
1125 		pm_runtime_put_autosuspend(dev->dev);
1126 		return r;
1127 	}
1128 
1129 	/* Avoid accidently unparking the sched thread during GPU reset */
1130 	r = down_read_killable(&adev->reset_sem);
1131 	if (r)
1132 		return r;
1133 
1134 	/* hold on the scheduler */
1135 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1136 		struct amdgpu_ring *ring = adev->rings[i];
1137 
1138 		if (!ring || !ring->sched.thread)
1139 			continue;
1140 		kthread_park(ring->sched.thread);
1141 	}
1142 
1143 	seq_printf(m, "run ib test:\n");
1144 	r = amdgpu_ib_ring_tests(adev);
1145 	if (r)
1146 		seq_printf(m, "ib ring tests failed (%d).\n", r);
1147 	else
1148 		seq_printf(m, "ib ring tests passed.\n");
1149 
1150 	/* go on the scheduler */
1151 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1152 		struct amdgpu_ring *ring = adev->rings[i];
1153 
1154 		if (!ring || !ring->sched.thread)
1155 			continue;
1156 		kthread_unpark(ring->sched.thread);
1157 	}
1158 
1159 	up_read(&adev->reset_sem);
1160 
1161 	pm_runtime_mark_last_busy(dev->dev);
1162 	pm_runtime_put_autosuspend(dev->dev);
1163 
1164 	return 0;
1165 }
1166 
1167 static int amdgpu_debugfs_evict_vram(void *data, u64 *val)
1168 {
1169 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1170 	struct drm_device *dev = adev_to_drm(adev);
1171 	int r;
1172 
1173 	r = pm_runtime_get_sync(dev->dev);
1174 	if (r < 0) {
1175 		pm_runtime_put_autosuspend(dev->dev);
1176 		return r;
1177 	}
1178 
1179 	*val = amdgpu_bo_evict_vram(adev);
1180 
1181 	pm_runtime_mark_last_busy(dev->dev);
1182 	pm_runtime_put_autosuspend(dev->dev);
1183 
1184 	return 0;
1185 }
1186 
1187 
1188 static int amdgpu_debugfs_evict_gtt(void *data, u64 *val)
1189 {
1190 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1191 	struct drm_device *dev = adev_to_drm(adev);
1192 	struct ttm_resource_manager *man;
1193 	int r;
1194 
1195 	r = pm_runtime_get_sync(dev->dev);
1196 	if (r < 0) {
1197 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1198 		return r;
1199 	}
1200 
1201 	man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
1202 	*val = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
1203 
1204 	pm_runtime_mark_last_busy(dev->dev);
1205 	pm_runtime_put_autosuspend(dev->dev);
1206 
1207 	return 0;
1208 }
1209 
1210 
1211 static int amdgpu_debugfs_vm_info_show(struct seq_file *m, void *unused)
1212 {
1213 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
1214 	struct drm_device *dev = adev_to_drm(adev);
1215 	struct drm_file *file;
1216 	int r;
1217 
1218 	r = mutex_lock_interruptible(&dev->filelist_mutex);
1219 	if (r)
1220 		return r;
1221 
1222 	list_for_each_entry(file, &dev->filelist, lhead) {
1223 		struct amdgpu_fpriv *fpriv = file->driver_priv;
1224 		struct amdgpu_vm *vm = &fpriv->vm;
1225 
1226 		seq_printf(m, "pid:%d\tProcess:%s ----------\n",
1227 				vm->task_info.pid, vm->task_info.process_name);
1228 		r = amdgpu_bo_reserve(vm->root.bo, true);
1229 		if (r)
1230 			break;
1231 		amdgpu_debugfs_vm_bo_info(vm, m);
1232 		amdgpu_bo_unreserve(vm->root.bo);
1233 	}
1234 
1235 	mutex_unlock(&dev->filelist_mutex);
1236 
1237 	return r;
1238 }
1239 
1240 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_test_ib);
1241 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_vm_info);
1242 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_vram_fops, amdgpu_debugfs_evict_vram,
1243 			 NULL, "%lld\n");
1244 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_evict_gtt_fops, amdgpu_debugfs_evict_gtt,
1245 			 NULL, "%lld\n");
1246 
1247 static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring,
1248 					  struct dma_fence **fences)
1249 {
1250 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
1251 	uint32_t sync_seq, last_seq;
1252 
1253 	last_seq = atomic_read(&ring->fence_drv.last_seq);
1254 	sync_seq = ring->fence_drv.sync_seq;
1255 
1256 	last_seq &= drv->num_fences_mask;
1257 	sync_seq &= drv->num_fences_mask;
1258 
1259 	do {
1260 		struct dma_fence *fence, **ptr;
1261 
1262 		++last_seq;
1263 		last_seq &= drv->num_fences_mask;
1264 		ptr = &drv->fences[last_seq];
1265 
1266 		fence = rcu_dereference_protected(*ptr, 1);
1267 		RCU_INIT_POINTER(*ptr, NULL);
1268 
1269 		if (!fence)
1270 			continue;
1271 
1272 		fences[last_seq] = fence;
1273 
1274 	} while (last_seq != sync_seq);
1275 }
1276 
1277 static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences,
1278 					    int length)
1279 {
1280 	int i;
1281 	struct dma_fence *fence;
1282 
1283 	for (i = 0; i < length; i++) {
1284 		fence = fences[i];
1285 		if (!fence)
1286 			continue;
1287 		dma_fence_signal(fence);
1288 		dma_fence_put(fence);
1289 	}
1290 }
1291 
1292 static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched)
1293 {
1294 	struct drm_sched_job *s_job;
1295 	struct dma_fence *fence;
1296 
1297 	spin_lock(&sched->job_list_lock);
1298 	list_for_each_entry(s_job, &sched->pending_list, list) {
1299 		fence = sched->ops->run_job(s_job);
1300 		dma_fence_put(fence);
1301 	}
1302 	spin_unlock(&sched->job_list_lock);
1303 }
1304 
1305 static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring)
1306 {
1307 	struct amdgpu_job *job;
1308 	struct drm_sched_job *s_job, *tmp;
1309 	uint32_t preempt_seq;
1310 	struct dma_fence *fence, **ptr;
1311 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
1312 	struct drm_gpu_scheduler *sched = &ring->sched;
1313 	bool preempted = true;
1314 
1315 	if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
1316 		return;
1317 
1318 	preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2));
1319 	if (preempt_seq <= atomic_read(&drv->last_seq)) {
1320 		preempted = false;
1321 		goto no_preempt;
1322 	}
1323 
1324 	preempt_seq &= drv->num_fences_mask;
1325 	ptr = &drv->fences[preempt_seq];
1326 	fence = rcu_dereference_protected(*ptr, 1);
1327 
1328 no_preempt:
1329 	spin_lock(&sched->job_list_lock);
1330 	list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
1331 		if (dma_fence_is_signaled(&s_job->s_fence->finished)) {
1332 			/* remove job from ring_mirror_list */
1333 			list_del_init(&s_job->list);
1334 			sched->ops->free_job(s_job);
1335 			continue;
1336 		}
1337 		job = to_amdgpu_job(s_job);
1338 		if (preempted && (&job->hw_fence) == fence)
1339 			/* mark the job as preempted */
1340 			job->preemption_status |= AMDGPU_IB_PREEMPTED;
1341 	}
1342 	spin_unlock(&sched->job_list_lock);
1343 }
1344 
1345 static int amdgpu_debugfs_ib_preempt(void *data, u64 val)
1346 {
1347 	int r, resched, length;
1348 	struct amdgpu_ring *ring;
1349 	struct dma_fence **fences = NULL;
1350 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1351 
1352 	if (val >= AMDGPU_MAX_RINGS)
1353 		return -EINVAL;
1354 
1355 	ring = adev->rings[val];
1356 
1357 	if (!ring || !ring->funcs->preempt_ib || !ring->sched.thread)
1358 		return -EINVAL;
1359 
1360 	/* the last preemption failed */
1361 	if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr))
1362 		return -EBUSY;
1363 
1364 	length = ring->fence_drv.num_fences_mask + 1;
1365 	fences = kcalloc(length, sizeof(void *), GFP_KERNEL);
1366 	if (!fences)
1367 		return -ENOMEM;
1368 
1369 	/* Avoid accidently unparking the sched thread during GPU reset */
1370 	r = down_read_killable(&adev->reset_sem);
1371 	if (r)
1372 		goto pro_end;
1373 
1374 	/* stop the scheduler */
1375 	kthread_park(ring->sched.thread);
1376 
1377 	resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1378 
1379 	/* preempt the IB */
1380 	r = amdgpu_ring_preempt_ib(ring);
1381 	if (r) {
1382 		DRM_WARN("failed to preempt ring %d\n", ring->idx);
1383 		goto failure;
1384 	}
1385 
1386 	amdgpu_fence_process(ring);
1387 
1388 	if (atomic_read(&ring->fence_drv.last_seq) !=
1389 	    ring->fence_drv.sync_seq) {
1390 		DRM_INFO("ring %d was preempted\n", ring->idx);
1391 
1392 		amdgpu_ib_preempt_mark_partial_job(ring);
1393 
1394 		/* swap out the old fences */
1395 		amdgpu_ib_preempt_fences_swap(ring, fences);
1396 
1397 		amdgpu_fence_driver_force_completion(ring);
1398 
1399 		/* resubmit unfinished jobs */
1400 		amdgpu_ib_preempt_job_recovery(&ring->sched);
1401 
1402 		/* wait for jobs finished */
1403 		amdgpu_fence_wait_empty(ring);
1404 
1405 		/* signal the old fences */
1406 		amdgpu_ib_preempt_signal_fences(fences, length);
1407 	}
1408 
1409 failure:
1410 	/* restart the scheduler */
1411 	kthread_unpark(ring->sched.thread);
1412 
1413 	up_read(&adev->reset_sem);
1414 
1415 	ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1416 
1417 pro_end:
1418 	kfree(fences);
1419 
1420 	return r;
1421 }
1422 
1423 static int amdgpu_debugfs_sclk_set(void *data, u64 val)
1424 {
1425 	int ret = 0;
1426 	uint32_t max_freq, min_freq;
1427 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
1428 
1429 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1430 		return -EINVAL;
1431 
1432 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1433 	if (ret < 0) {
1434 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1435 		return ret;
1436 	}
1437 
1438 	if (is_support_sw_smu(adev)) {
1439 		ret = smu_get_dpm_freq_range(&adev->smu, SMU_SCLK, &min_freq, &max_freq);
1440 		if (ret || val > max_freq || val < min_freq)
1441 			return -EINVAL;
1442 		ret = smu_set_soft_freq_range(&adev->smu, SMU_SCLK, (uint32_t)val, (uint32_t)val);
1443 	} else {
1444 		return 0;
1445 	}
1446 
1447 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1448 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1449 
1450 	if (ret)
1451 		return -EINVAL;
1452 
1453 	return 0;
1454 }
1455 
1456 DEFINE_DEBUGFS_ATTRIBUTE(fops_ib_preempt, NULL,
1457 			amdgpu_debugfs_ib_preempt, "%llu\n");
1458 
1459 DEFINE_DEBUGFS_ATTRIBUTE(fops_sclk_set, NULL,
1460 			amdgpu_debugfs_sclk_set, "%llu\n");
1461 
1462 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1463 {
1464 	struct dentry *root = adev_to_drm(adev)->primary->debugfs_root;
1465 	struct dentry *ent;
1466 	int r, i;
1467 
1468 	ent = debugfs_create_file("amdgpu_preempt_ib", 0600, root, adev,
1469 				  &fops_ib_preempt);
1470 	if (IS_ERR(ent)) {
1471 		DRM_ERROR("unable to create amdgpu_preempt_ib debugsfs file\n");
1472 		return PTR_ERR(ent);
1473 	}
1474 
1475 	ent = debugfs_create_file("amdgpu_force_sclk", 0200, root, adev,
1476 				  &fops_sclk_set);
1477 	if (IS_ERR(ent)) {
1478 		DRM_ERROR("unable to create amdgpu_set_sclk debugsfs file\n");
1479 		return PTR_ERR(ent);
1480 	}
1481 
1482 	/* Register debugfs entries for amdgpu_ttm */
1483 	amdgpu_ttm_debugfs_init(adev);
1484 	amdgpu_debugfs_pm_init(adev);
1485 	amdgpu_debugfs_sa_init(adev);
1486 	amdgpu_debugfs_fence_init(adev);
1487 	amdgpu_debugfs_gem_init(adev);
1488 
1489 	r = amdgpu_debugfs_regs_init(adev);
1490 	if (r)
1491 		DRM_ERROR("registering register debugfs failed (%d).\n", r);
1492 
1493 	amdgpu_debugfs_firmware_init(adev);
1494 
1495 #if defined(CONFIG_DRM_AMD_DC)
1496 	if (amdgpu_device_has_dc_support(adev))
1497 		dtn_debugfs_init(adev);
1498 #endif
1499 
1500 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1501 		struct amdgpu_ring *ring = adev->rings[i];
1502 
1503 		if (!ring)
1504 			continue;
1505 
1506 		if (amdgpu_debugfs_ring_init(adev, ring)) {
1507 			DRM_ERROR("Failed to register debugfs file for rings !\n");
1508 		}
1509 	}
1510 
1511 	amdgpu_ras_debugfs_create_all(adev);
1512 	amdgpu_rap_debugfs_init(adev);
1513 	amdgpu_securedisplay_debugfs_init(adev);
1514 	amdgpu_fw_attestation_debugfs_init(adev);
1515 
1516 	debugfs_create_file("amdgpu_evict_vram", 0444, root, adev,
1517 			    &amdgpu_evict_vram_fops);
1518 	debugfs_create_file("amdgpu_evict_gtt", 0444, root, adev,
1519 			    &amdgpu_evict_gtt_fops);
1520 	debugfs_create_file("amdgpu_test_ib", 0444, root, adev,
1521 			    &amdgpu_debugfs_test_ib_fops);
1522 	debugfs_create_file("amdgpu_vm_info", 0444, root, adev,
1523 			    &amdgpu_debugfs_vm_info_fops);
1524 
1525 	adev->debugfs_vbios_blob.data = adev->bios;
1526 	adev->debugfs_vbios_blob.size = adev->bios_size;
1527 	debugfs_create_blob("amdgpu_vbios", 0444, root,
1528 			    &adev->debugfs_vbios_blob);
1529 
1530 	return 0;
1531 }
1532 
1533 #else
1534 int amdgpu_debugfs_init(struct amdgpu_device *adev)
1535 {
1536 	return 0;
1537 }
1538 int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
1539 {
1540 	return 0;
1541 }
1542 #endif
1543