xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c (revision a61c16258a4720065972cf04fcfee1caa6ea5fc0)
1 /*
2  * Copyright 2014 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/firmware.h>
27 #include <linux/pm_runtime.h>
28 
29 #include "amdgpu.h"
30 #include "amdgpu_gfx.h"
31 #include "amdgpu_rlc.h"
32 #include "amdgpu_ras.h"
33 #include "amdgpu_reset.h"
34 #include "amdgpu_xcp.h"
35 #include "amdgpu_xgmi.h"
36 
37 /* delay 0.1 second to enable gfx off feature */
38 #define GFX_OFF_DELAY_ENABLE         msecs_to_jiffies(100)
39 
40 #define GFX_OFF_NO_DELAY 0
41 
42 /*
43  * GPU GFX IP block helpers function.
44  */
45 
46 int amdgpu_gfx_mec_queue_to_bit(struct amdgpu_device *adev, int mec,
47 				int pipe, int queue)
48 {
49 	int bit = 0;
50 
51 	bit += mec * adev->gfx.mec.num_pipe_per_mec
52 		* adev->gfx.mec.num_queue_per_pipe;
53 	bit += pipe * adev->gfx.mec.num_queue_per_pipe;
54 	bit += queue;
55 
56 	return bit;
57 }
58 
59 void amdgpu_queue_mask_bit_to_mec_queue(struct amdgpu_device *adev, int bit,
60 				 int *mec, int *pipe, int *queue)
61 {
62 	*queue = bit % adev->gfx.mec.num_queue_per_pipe;
63 	*pipe = (bit / adev->gfx.mec.num_queue_per_pipe)
64 		% adev->gfx.mec.num_pipe_per_mec;
65 	*mec = (bit / adev->gfx.mec.num_queue_per_pipe)
66 	       / adev->gfx.mec.num_pipe_per_mec;
67 
68 }
69 
70 bool amdgpu_gfx_is_mec_queue_enabled(struct amdgpu_device *adev,
71 				     int xcc_id, int mec, int pipe, int queue)
72 {
73 	return test_bit(amdgpu_gfx_mec_queue_to_bit(adev, mec, pipe, queue),
74 			adev->gfx.mec_bitmap[xcc_id].queue_bitmap);
75 }
76 
77 static int amdgpu_gfx_me_queue_to_bit(struct amdgpu_device *adev,
78 				      int me, int pipe, int queue)
79 {
80 	int num_queue_per_pipe = 1; /* we only enable 1 KGQ per pipe */
81 	int bit = 0;
82 
83 	bit += me * adev->gfx.me.num_pipe_per_me
84 		* num_queue_per_pipe;
85 	bit += pipe * num_queue_per_pipe;
86 	bit += queue;
87 
88 	return bit;
89 }
90 
91 bool amdgpu_gfx_is_me_queue_enabled(struct amdgpu_device *adev,
92 				    int me, int pipe, int queue)
93 {
94 	return test_bit(amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue),
95 			adev->gfx.me.queue_bitmap);
96 }
97 
98 /**
99  * amdgpu_gfx_parse_disable_cu - Parse the disable_cu module parameter
100  *
101  * @mask: array in which the per-shader array disable masks will be stored
102  * @max_se: number of SEs
103  * @max_sh: number of SHs
104  *
105  * The bitmask of CUs to be disabled in the shader array determined by se and
106  * sh is stored in mask[se * max_sh + sh].
107  */
108 void amdgpu_gfx_parse_disable_cu(unsigned int *mask, unsigned int max_se, unsigned int max_sh)
109 {
110 	unsigned int se, sh, cu;
111 	const char *p;
112 
113 	memset(mask, 0, sizeof(*mask) * max_se * max_sh);
114 
115 	if (!amdgpu_disable_cu || !*amdgpu_disable_cu)
116 		return;
117 
118 	p = amdgpu_disable_cu;
119 	for (;;) {
120 		char *next;
121 		int ret = sscanf(p, "%u.%u.%u", &se, &sh, &cu);
122 
123 		if (ret < 3) {
124 			DRM_ERROR("amdgpu: could not parse disable_cu\n");
125 			return;
126 		}
127 
128 		if (se < max_se && sh < max_sh && cu < 16) {
129 			DRM_INFO("amdgpu: disabling CU %u.%u.%u\n", se, sh, cu);
130 			mask[se * max_sh + sh] |= 1u << cu;
131 		} else {
132 			DRM_ERROR("amdgpu: disable_cu %u.%u.%u is out of range\n",
133 				  se, sh, cu);
134 		}
135 
136 		next = strchr(p, ',');
137 		if (!next)
138 			break;
139 		p = next + 1;
140 	}
141 }
142 
143 static bool amdgpu_gfx_is_graphics_multipipe_capable(struct amdgpu_device *adev)
144 {
145 	return amdgpu_async_gfx_ring && adev->gfx.me.num_pipe_per_me > 1;
146 }
147 
148 static bool amdgpu_gfx_is_compute_multipipe_capable(struct amdgpu_device *adev)
149 {
150 	if (amdgpu_compute_multipipe != -1) {
151 		DRM_INFO("amdgpu: forcing compute pipe policy %d\n",
152 			 amdgpu_compute_multipipe);
153 		return amdgpu_compute_multipipe == 1;
154 	}
155 
156 	if (amdgpu_ip_version(adev, GC_HWIP, 0) > IP_VERSION(9, 0, 0))
157 		return true;
158 
159 	/* FIXME: spreading the queues across pipes causes perf regressions
160 	 * on POLARIS11 compute workloads */
161 	if (adev->asic_type == CHIP_POLARIS11)
162 		return false;
163 
164 	return adev->gfx.mec.num_mec > 1;
165 }
166 
167 bool amdgpu_gfx_is_high_priority_graphics_queue(struct amdgpu_device *adev,
168 						struct amdgpu_ring *ring)
169 {
170 	int queue = ring->queue;
171 	int pipe = ring->pipe;
172 
173 	/* Policy: use pipe1 queue0 as high priority graphics queue if we
174 	 * have more than one gfx pipe.
175 	 */
176 	if (amdgpu_gfx_is_graphics_multipipe_capable(adev) &&
177 	    adev->gfx.num_gfx_rings > 1 && pipe == 1 && queue == 0) {
178 		int me = ring->me;
179 		int bit;
180 
181 		bit = amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue);
182 		if (ring == &adev->gfx.gfx_ring[bit])
183 			return true;
184 	}
185 
186 	return false;
187 }
188 
189 bool amdgpu_gfx_is_high_priority_compute_queue(struct amdgpu_device *adev,
190 					       struct amdgpu_ring *ring)
191 {
192 	/* Policy: use 1st queue as high priority compute queue if we
193 	 * have more than one compute queue.
194 	 */
195 	if (adev->gfx.num_compute_rings > 1 &&
196 	    ring == &adev->gfx.compute_ring[0])
197 		return true;
198 
199 	return false;
200 }
201 
202 void amdgpu_gfx_compute_queue_acquire(struct amdgpu_device *adev)
203 {
204 	int i, j, queue, pipe;
205 	bool multipipe_policy = amdgpu_gfx_is_compute_multipipe_capable(adev);
206 	int max_queues_per_mec = min(adev->gfx.mec.num_pipe_per_mec *
207 				     adev->gfx.mec.num_queue_per_pipe,
208 				     adev->gfx.num_compute_rings);
209 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
210 
211 	if (multipipe_policy) {
212 		/* policy: make queues evenly cross all pipes on MEC1 only
213 		 * for multiple xcc, just use the original policy for simplicity */
214 		for (j = 0; j < num_xcc; j++) {
215 			for (i = 0; i < max_queues_per_mec; i++) {
216 				pipe = i % adev->gfx.mec.num_pipe_per_mec;
217 				queue = (i / adev->gfx.mec.num_pipe_per_mec) %
218 					 adev->gfx.mec.num_queue_per_pipe;
219 
220 				set_bit(pipe * adev->gfx.mec.num_queue_per_pipe + queue,
221 					adev->gfx.mec_bitmap[j].queue_bitmap);
222 			}
223 		}
224 	} else {
225 		/* policy: amdgpu owns all queues in the given pipe */
226 		for (j = 0; j < num_xcc; j++) {
227 			for (i = 0; i < max_queues_per_mec; ++i)
228 				set_bit(i, adev->gfx.mec_bitmap[j].queue_bitmap);
229 		}
230 	}
231 
232 	for (j = 0; j < num_xcc; j++) {
233 		dev_dbg(adev->dev, "mec queue bitmap weight=%d\n",
234 			bitmap_weight(adev->gfx.mec_bitmap[j].queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES));
235 	}
236 }
237 
238 void amdgpu_gfx_graphics_queue_acquire(struct amdgpu_device *adev)
239 {
240 	int i, queue, pipe;
241 	bool multipipe_policy = amdgpu_gfx_is_graphics_multipipe_capable(adev);
242 	int num_queue_per_pipe = 1; /* we only enable 1 KGQ per pipe */
243 	int max_queues_per_me = adev->gfx.me.num_pipe_per_me * num_queue_per_pipe;
244 
245 	if (multipipe_policy) {
246 		/* policy: amdgpu owns the first queue per pipe at this stage
247 		 * will extend to mulitple queues per pipe later */
248 		for (i = 0; i < max_queues_per_me; i++) {
249 			pipe = i % adev->gfx.me.num_pipe_per_me;
250 			queue = (i / adev->gfx.me.num_pipe_per_me) %
251 				num_queue_per_pipe;
252 
253 			set_bit(pipe * num_queue_per_pipe + queue,
254 				adev->gfx.me.queue_bitmap);
255 		}
256 	} else {
257 		for (i = 0; i < max_queues_per_me; ++i)
258 			set_bit(i, adev->gfx.me.queue_bitmap);
259 	}
260 
261 	/* update the number of active graphics rings */
262 	if (adev->gfx.num_gfx_rings)
263 		adev->gfx.num_gfx_rings =
264 			bitmap_weight(adev->gfx.me.queue_bitmap, AMDGPU_MAX_GFX_QUEUES);
265 }
266 
267 static int amdgpu_gfx_kiq_acquire(struct amdgpu_device *adev,
268 				  struct amdgpu_ring *ring, int xcc_id)
269 {
270 	int queue_bit;
271 	int mec, pipe, queue;
272 
273 	queue_bit = adev->gfx.mec.num_mec
274 		    * adev->gfx.mec.num_pipe_per_mec
275 		    * adev->gfx.mec.num_queue_per_pipe;
276 
277 	while (--queue_bit >= 0) {
278 		if (test_bit(queue_bit, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
279 			continue;
280 
281 		amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
282 
283 		/*
284 		 * 1. Using pipes 2/3 from MEC 2 seems cause problems.
285 		 * 2. It must use queue id 0, because CGPG_IDLE/SAVE/LOAD/RUN
286 		 * only can be issued on queue 0.
287 		 */
288 		if ((mec == 1 && pipe > 1) || queue != 0)
289 			continue;
290 
291 		ring->me = mec + 1;
292 		ring->pipe = pipe;
293 		ring->queue = queue;
294 
295 		return 0;
296 	}
297 
298 	dev_err(adev->dev, "Failed to find a queue for KIQ\n");
299 	return -EINVAL;
300 }
301 
302 int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev, int xcc_id)
303 {
304 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
305 	struct amdgpu_irq_src *irq = &kiq->irq;
306 	struct amdgpu_ring *ring = &kiq->ring;
307 	int r = 0;
308 
309 	spin_lock_init(&kiq->ring_lock);
310 
311 	ring->adev = NULL;
312 	ring->ring_obj = NULL;
313 	ring->use_doorbell = true;
314 	ring->xcc_id = xcc_id;
315 	ring->vm_hub = AMDGPU_GFXHUB(xcc_id);
316 	ring->doorbell_index =
317 		(adev->doorbell_index.kiq +
318 		 xcc_id * adev->doorbell_index.xcc_doorbell_range)
319 		<< 1;
320 
321 	r = amdgpu_gfx_kiq_acquire(adev, ring, xcc_id);
322 	if (r)
323 		return r;
324 
325 	ring->eop_gpu_addr = kiq->eop_gpu_addr;
326 	ring->no_scheduler = true;
327 	snprintf(ring->name, sizeof(ring->name), "kiq_%hhu.%hhu.%hhu.%hhu",
328 		 (unsigned char)xcc_id, (unsigned char)ring->me,
329 		 (unsigned char)ring->pipe, (unsigned char)ring->queue);
330 	r = amdgpu_ring_init(adev, ring, 1024, irq, AMDGPU_CP_KIQ_IRQ_DRIVER0,
331 			     AMDGPU_RING_PRIO_DEFAULT, NULL);
332 	if (r)
333 		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
334 
335 	return r;
336 }
337 
338 void amdgpu_gfx_kiq_free_ring(struct amdgpu_ring *ring)
339 {
340 	amdgpu_ring_fini(ring);
341 }
342 
343 void amdgpu_gfx_kiq_fini(struct amdgpu_device *adev, int xcc_id)
344 {
345 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
346 
347 	amdgpu_bo_free_kernel(&kiq->eop_obj, &kiq->eop_gpu_addr, NULL);
348 }
349 
350 int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
351 			unsigned int hpd_size, int xcc_id)
352 {
353 	int r;
354 	u32 *hpd;
355 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
356 
357 	r = amdgpu_bo_create_kernel(adev, hpd_size, PAGE_SIZE,
358 				    AMDGPU_GEM_DOMAIN_GTT, &kiq->eop_obj,
359 				    &kiq->eop_gpu_addr, (void **)&hpd);
360 	if (r) {
361 		dev_warn(adev->dev, "failed to create KIQ bo (%d).\n", r);
362 		return r;
363 	}
364 
365 	memset(hpd, 0, hpd_size);
366 
367 	r = amdgpu_bo_reserve(kiq->eop_obj, true);
368 	if (unlikely(r != 0))
369 		dev_warn(adev->dev, "(%d) reserve kiq eop bo failed\n", r);
370 	amdgpu_bo_kunmap(kiq->eop_obj);
371 	amdgpu_bo_unreserve(kiq->eop_obj);
372 
373 	return 0;
374 }
375 
376 /* create MQD for each compute/gfx queue */
377 int amdgpu_gfx_mqd_sw_init(struct amdgpu_device *adev,
378 			   unsigned int mqd_size, int xcc_id)
379 {
380 	int r, i, j;
381 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
382 	struct amdgpu_ring *ring = &kiq->ring;
383 	u32 domain = AMDGPU_GEM_DOMAIN_GTT;
384 
385 #if !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
386 	/* Only enable on gfx10 and 11 for now to avoid changing behavior on older chips */
387 	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 0, 0))
388 		domain |= AMDGPU_GEM_DOMAIN_VRAM;
389 #endif
390 
391 	/* create MQD for KIQ */
392 	if (!adev->enable_mes_kiq && !ring->mqd_obj) {
393 		/* originaly the KIQ MQD is put in GTT domain, but for SRIOV VRAM domain is a must
394 		 * otherwise hypervisor trigger SAVE_VF fail after driver unloaded which mean MQD
395 		 * deallocated and gart_unbind, to strict diverage we decide to use VRAM domain for
396 		 * KIQ MQD no matter SRIOV or Bare-metal
397 		 */
398 		r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
399 					    AMDGPU_GEM_DOMAIN_VRAM |
400 					    AMDGPU_GEM_DOMAIN_GTT,
401 					    &ring->mqd_obj,
402 					    &ring->mqd_gpu_addr,
403 					    &ring->mqd_ptr);
404 		if (r) {
405 			dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
406 			return r;
407 		}
408 
409 		/* prepare MQD backup */
410 		kiq->mqd_backup = kzalloc(mqd_size, GFP_KERNEL);
411 		if (!kiq->mqd_backup) {
412 			dev_warn(adev->dev,
413 				 "no memory to create MQD backup for ring %s\n", ring->name);
414 			return -ENOMEM;
415 		}
416 	}
417 
418 	if (adev->asic_type >= CHIP_NAVI10 && amdgpu_async_gfx_ring) {
419 		/* create MQD for each KGQ */
420 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
421 			ring = &adev->gfx.gfx_ring[i];
422 			if (!ring->mqd_obj) {
423 				r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
424 							    domain, &ring->mqd_obj,
425 							    &ring->mqd_gpu_addr, &ring->mqd_ptr);
426 				if (r) {
427 					dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
428 					return r;
429 				}
430 
431 				ring->mqd_size = mqd_size;
432 				/* prepare MQD backup */
433 				adev->gfx.me.mqd_backup[i] = kzalloc(mqd_size, GFP_KERNEL);
434 				if (!adev->gfx.me.mqd_backup[i]) {
435 					dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
436 					return -ENOMEM;
437 				}
438 			}
439 		}
440 	}
441 
442 	/* create MQD for each KCQ */
443 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
444 		j = i + xcc_id * adev->gfx.num_compute_rings;
445 		ring = &adev->gfx.compute_ring[j];
446 		if (!ring->mqd_obj) {
447 			r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
448 						    domain, &ring->mqd_obj,
449 						    &ring->mqd_gpu_addr, &ring->mqd_ptr);
450 			if (r) {
451 				dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
452 				return r;
453 			}
454 
455 			ring->mqd_size = mqd_size;
456 			/* prepare MQD backup */
457 			adev->gfx.mec.mqd_backup[j] = kzalloc(mqd_size, GFP_KERNEL);
458 			if (!adev->gfx.mec.mqd_backup[j]) {
459 				dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
460 				return -ENOMEM;
461 			}
462 		}
463 	}
464 
465 	return 0;
466 }
467 
468 void amdgpu_gfx_mqd_sw_fini(struct amdgpu_device *adev, int xcc_id)
469 {
470 	struct amdgpu_ring *ring = NULL;
471 	int i, j;
472 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
473 
474 	if (adev->asic_type >= CHIP_NAVI10 && amdgpu_async_gfx_ring) {
475 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
476 			ring = &adev->gfx.gfx_ring[i];
477 			kfree(adev->gfx.me.mqd_backup[i]);
478 			amdgpu_bo_free_kernel(&ring->mqd_obj,
479 					      &ring->mqd_gpu_addr,
480 					      &ring->mqd_ptr);
481 		}
482 	}
483 
484 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
485 		j = i + xcc_id * adev->gfx.num_compute_rings;
486 		ring = &adev->gfx.compute_ring[j];
487 		kfree(adev->gfx.mec.mqd_backup[j]);
488 		amdgpu_bo_free_kernel(&ring->mqd_obj,
489 				      &ring->mqd_gpu_addr,
490 				      &ring->mqd_ptr);
491 	}
492 
493 	ring = &kiq->ring;
494 	kfree(kiq->mqd_backup);
495 	amdgpu_bo_free_kernel(&ring->mqd_obj,
496 			      &ring->mqd_gpu_addr,
497 			      &ring->mqd_ptr);
498 }
499 
500 int amdgpu_gfx_disable_kcq(struct amdgpu_device *adev, int xcc_id)
501 {
502 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
503 	struct amdgpu_ring *kiq_ring = &kiq->ring;
504 	int i, r = 0;
505 	int j;
506 
507 	if (adev->enable_mes) {
508 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
509 			j = i + xcc_id * adev->gfx.num_compute_rings;
510 			amdgpu_mes_unmap_legacy_queue(adev,
511 						   &adev->gfx.compute_ring[j],
512 						   RESET_QUEUES, 0, 0);
513 		}
514 		return 0;
515 	}
516 
517 	if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
518 		return -EINVAL;
519 
520 	if (!kiq_ring->sched.ready || amdgpu_in_reset(adev))
521 		return 0;
522 
523 	spin_lock(&kiq->ring_lock);
524 	if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
525 					adev->gfx.num_compute_rings)) {
526 		spin_unlock(&kiq->ring_lock);
527 		return -ENOMEM;
528 	}
529 
530 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
531 		j = i + xcc_id * adev->gfx.num_compute_rings;
532 		kiq->pmf->kiq_unmap_queues(kiq_ring,
533 					   &adev->gfx.compute_ring[j],
534 					   RESET_QUEUES, 0, 0);
535 	}
536 	/* Submit unmap queue packet */
537 	amdgpu_ring_commit(kiq_ring);
538 	/*
539 	 * Ring test will do a basic scratch register change check. Just run
540 	 * this to ensure that unmap queues that is submitted before got
541 	 * processed successfully before returning.
542 	 */
543 	r = amdgpu_ring_test_helper(kiq_ring);
544 
545 	spin_unlock(&kiq->ring_lock);
546 
547 	return r;
548 }
549 
550 int amdgpu_gfx_disable_kgq(struct amdgpu_device *adev, int xcc_id)
551 {
552 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
553 	struct amdgpu_ring *kiq_ring = &kiq->ring;
554 	int i, r = 0;
555 	int j;
556 
557 	if (adev->enable_mes) {
558 		if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
559 			for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
560 				j = i + xcc_id * adev->gfx.num_gfx_rings;
561 				amdgpu_mes_unmap_legacy_queue(adev,
562 						      &adev->gfx.gfx_ring[j],
563 						      PREEMPT_QUEUES, 0, 0);
564 			}
565 		}
566 		return 0;
567 	}
568 
569 	if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
570 		return -EINVAL;
571 
572 	if (!adev->gfx.kiq[0].ring.sched.ready || amdgpu_in_reset(adev))
573 		return 0;
574 
575 	if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
576 		spin_lock(&kiq->ring_lock);
577 		if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
578 						adev->gfx.num_gfx_rings)) {
579 			spin_unlock(&kiq->ring_lock);
580 			return -ENOMEM;
581 		}
582 
583 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
584 			j = i + xcc_id * adev->gfx.num_gfx_rings;
585 			kiq->pmf->kiq_unmap_queues(kiq_ring,
586 						   &adev->gfx.gfx_ring[j],
587 						   PREEMPT_QUEUES, 0, 0);
588 		}
589 		/* Submit unmap queue packet */
590 		amdgpu_ring_commit(kiq_ring);
591 
592 		/*
593 		 * Ring test will do a basic scratch register change check.
594 		 * Just run this to ensure that unmap queues that is submitted
595 		 * before got processed successfully before returning.
596 		 */
597 		r = amdgpu_ring_test_helper(kiq_ring);
598 		spin_unlock(&kiq->ring_lock);
599 	}
600 
601 	return r;
602 }
603 
604 int amdgpu_queue_mask_bit_to_set_resource_bit(struct amdgpu_device *adev,
605 					int queue_bit)
606 {
607 	int mec, pipe, queue;
608 	int set_resource_bit = 0;
609 
610 	amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
611 
612 	set_resource_bit = mec * 4 * 8 + pipe * 8 + queue;
613 
614 	return set_resource_bit;
615 }
616 
617 static int amdgpu_gfx_mes_enable_kcq(struct amdgpu_device *adev, int xcc_id)
618 {
619 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
620 	struct amdgpu_ring *kiq_ring = &kiq->ring;
621 	uint64_t queue_mask = ~0ULL;
622 	int r, i, j;
623 
624 	amdgpu_device_flush_hdp(adev, NULL);
625 
626 	if (!adev->enable_uni_mes) {
627 		spin_lock(&kiq->ring_lock);
628 		r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->set_resources_size);
629 		if (r) {
630 			dev_err(adev->dev, "Failed to lock KIQ (%d).\n", r);
631 			spin_unlock(&kiq->ring_lock);
632 			return r;
633 		}
634 
635 		kiq->pmf->kiq_set_resources(kiq_ring, queue_mask);
636 		r = amdgpu_ring_test_helper(kiq_ring);
637 		spin_unlock(&kiq->ring_lock);
638 		if (r)
639 			dev_err(adev->dev, "KIQ failed to set resources\n");
640 	}
641 
642 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
643 		j = i + xcc_id * adev->gfx.num_compute_rings;
644 		r = amdgpu_mes_map_legacy_queue(adev,
645 						&adev->gfx.compute_ring[j]);
646 		if (r) {
647 			dev_err(adev->dev, "failed to map compute queue\n");
648 			return r;
649 		}
650 	}
651 
652 	return 0;
653 }
654 
655 int amdgpu_gfx_enable_kcq(struct amdgpu_device *adev, int xcc_id)
656 {
657 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
658 	struct amdgpu_ring *kiq_ring = &kiq->ring;
659 	uint64_t queue_mask = 0;
660 	int r, i, j;
661 
662 	if (adev->mes.enable_legacy_queue_map)
663 		return amdgpu_gfx_mes_enable_kcq(adev, xcc_id);
664 
665 	if (!kiq->pmf || !kiq->pmf->kiq_map_queues || !kiq->pmf->kiq_set_resources)
666 		return -EINVAL;
667 
668 	for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
669 		if (!test_bit(i, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
670 			continue;
671 
672 		/* This situation may be hit in the future if a new HW
673 		 * generation exposes more than 64 queues. If so, the
674 		 * definition of queue_mask needs updating */
675 		if (WARN_ON(i > (sizeof(queue_mask)*8))) {
676 			DRM_ERROR("Invalid KCQ enabled: %d\n", i);
677 			break;
678 		}
679 
680 		queue_mask |= (1ull << amdgpu_queue_mask_bit_to_set_resource_bit(adev, i));
681 	}
682 
683 	amdgpu_device_flush_hdp(adev, NULL);
684 
685 	DRM_INFO("kiq ring mec %d pipe %d q %d\n", kiq_ring->me, kiq_ring->pipe,
686 		 kiq_ring->queue);
687 
688 	spin_lock(&kiq->ring_lock);
689 	r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
690 					adev->gfx.num_compute_rings +
691 					kiq->pmf->set_resources_size);
692 	if (r) {
693 		DRM_ERROR("Failed to lock KIQ (%d).\n", r);
694 		spin_unlock(&kiq->ring_lock);
695 		return r;
696 	}
697 
698 	kiq->pmf->kiq_set_resources(kiq_ring, queue_mask);
699 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
700 		j = i + xcc_id * adev->gfx.num_compute_rings;
701 		kiq->pmf->kiq_map_queues(kiq_ring,
702 					 &adev->gfx.compute_ring[j]);
703 	}
704 	/* Submit map queue packet */
705 	amdgpu_ring_commit(kiq_ring);
706 	/*
707 	 * Ring test will do a basic scratch register change check. Just run
708 	 * this to ensure that map queues that is submitted before got
709 	 * processed successfully before returning.
710 	 */
711 	r = amdgpu_ring_test_helper(kiq_ring);
712 	spin_unlock(&kiq->ring_lock);
713 	if (r)
714 		DRM_ERROR("KCQ enable failed\n");
715 
716 	return r;
717 }
718 
719 int amdgpu_gfx_enable_kgq(struct amdgpu_device *adev, int xcc_id)
720 {
721 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
722 	struct amdgpu_ring *kiq_ring = &kiq->ring;
723 	int r, i, j;
724 
725 	if (!kiq->pmf || !kiq->pmf->kiq_map_queues)
726 		return -EINVAL;
727 
728 	amdgpu_device_flush_hdp(adev, NULL);
729 
730 	if (adev->mes.enable_legacy_queue_map) {
731 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
732 			j = i + xcc_id * adev->gfx.num_gfx_rings;
733 			r = amdgpu_mes_map_legacy_queue(adev,
734 							&adev->gfx.gfx_ring[j]);
735 			if (r) {
736 				DRM_ERROR("failed to map gfx queue\n");
737 				return r;
738 			}
739 		}
740 
741 		return 0;
742 	}
743 
744 	spin_lock(&kiq->ring_lock);
745 	/* No need to map kcq on the slave */
746 	if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
747 		r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
748 						adev->gfx.num_gfx_rings);
749 		if (r) {
750 			DRM_ERROR("Failed to lock KIQ (%d).\n", r);
751 			spin_unlock(&kiq->ring_lock);
752 			return r;
753 		}
754 
755 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
756 			j = i + xcc_id * adev->gfx.num_gfx_rings;
757 			kiq->pmf->kiq_map_queues(kiq_ring,
758 						 &adev->gfx.gfx_ring[j]);
759 		}
760 	}
761 	/* Submit map queue packet */
762 	amdgpu_ring_commit(kiq_ring);
763 	/*
764 	 * Ring test will do a basic scratch register change check. Just run
765 	 * this to ensure that map queues that is submitted before got
766 	 * processed successfully before returning.
767 	 */
768 	r = amdgpu_ring_test_helper(kiq_ring);
769 	spin_unlock(&kiq->ring_lock);
770 	if (r)
771 		DRM_ERROR("KGQ enable failed\n");
772 
773 	return r;
774 }
775 
776 static void amdgpu_gfx_do_off_ctrl(struct amdgpu_device *adev, bool enable,
777 				   bool no_delay)
778 {
779 	unsigned long delay = GFX_OFF_DELAY_ENABLE;
780 
781 	if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
782 		return;
783 
784 	mutex_lock(&adev->gfx.gfx_off_mutex);
785 
786 	if (enable) {
787 		/* If the count is already 0, it means there's an imbalance bug somewhere.
788 		 * Note that the bug may be in a different caller than the one which triggers the
789 		 * WARN_ON_ONCE.
790 		 */
791 		if (WARN_ON_ONCE(adev->gfx.gfx_off_req_count == 0))
792 			goto unlock;
793 
794 		adev->gfx.gfx_off_req_count--;
795 
796 		if (adev->gfx.gfx_off_req_count == 0 &&
797 		    !adev->gfx.gfx_off_state) {
798 			/* If going to s2idle, no need to wait */
799 			if (no_delay) {
800 				if (!amdgpu_dpm_set_powergating_by_smu(adev,
801 						AMD_IP_BLOCK_TYPE_GFX, true, 0))
802 					adev->gfx.gfx_off_state = true;
803 			} else {
804 				schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
805 					      delay);
806 			}
807 		}
808 	} else {
809 		if (adev->gfx.gfx_off_req_count == 0) {
810 			cancel_delayed_work_sync(&adev->gfx.gfx_off_delay_work);
811 
812 			if (adev->gfx.gfx_off_state &&
813 			    !amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false, 0)) {
814 				adev->gfx.gfx_off_state = false;
815 
816 				if (adev->gfx.funcs->init_spm_golden) {
817 					dev_dbg(adev->dev,
818 						"GFXOFF is disabled, re-init SPM golden settings\n");
819 					amdgpu_gfx_init_spm_golden(adev);
820 				}
821 			}
822 		}
823 
824 		adev->gfx.gfx_off_req_count++;
825 	}
826 
827 unlock:
828 	mutex_unlock(&adev->gfx.gfx_off_mutex);
829 }
830 
831 /* amdgpu_gfx_off_ctrl - Handle gfx off feature enable/disable
832  *
833  * @adev: amdgpu_device pointer
834  * @bool enable true: enable gfx off feature, false: disable gfx off feature
835  *
836  * 1. gfx off feature will be enabled by gfx ip after gfx cg pg enabled.
837  * 2. other client can send request to disable gfx off feature, the request should be honored.
838  * 3. other client can cancel their request of disable gfx off feature
839  * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
840  *
841  * gfx off allow will be delayed by GFX_OFF_DELAY_ENABLE ms.
842  */
843 void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
844 {
845 	/* If going to s2idle, no need to wait */
846 	bool no_delay = adev->in_s0ix ? true : false;
847 
848 	amdgpu_gfx_do_off_ctrl(adev, enable, no_delay);
849 }
850 
851 /* amdgpu_gfx_off_ctrl_immediate - Handle gfx off feature enable/disable
852  *
853  * @adev: amdgpu_device pointer
854  * @bool enable true: enable gfx off feature, false: disable gfx off feature
855  *
856  * 1. gfx off feature will be enabled by gfx ip after gfx cg pg enabled.
857  * 2. other client can send request to disable gfx off feature, the request should be honored.
858  * 3. other client can cancel their request of disable gfx off feature
859  * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
860  *
861  * gfx off allow will be issued immediately.
862  */
863 void amdgpu_gfx_off_ctrl_immediate(struct amdgpu_device *adev, bool enable)
864 {
865 	amdgpu_gfx_do_off_ctrl(adev, enable, true);
866 }
867 
868 int amdgpu_set_gfx_off_residency(struct amdgpu_device *adev, bool value)
869 {
870 	int r = 0;
871 
872 	mutex_lock(&adev->gfx.gfx_off_mutex);
873 
874 	r = amdgpu_dpm_set_residency_gfxoff(adev, value);
875 
876 	mutex_unlock(&adev->gfx.gfx_off_mutex);
877 
878 	return r;
879 }
880 
881 int amdgpu_get_gfx_off_residency(struct amdgpu_device *adev, u32 *value)
882 {
883 	int r = 0;
884 
885 	mutex_lock(&adev->gfx.gfx_off_mutex);
886 
887 	r = amdgpu_dpm_get_residency_gfxoff(adev, value);
888 
889 	mutex_unlock(&adev->gfx.gfx_off_mutex);
890 
891 	return r;
892 }
893 
894 int amdgpu_get_gfx_off_entrycount(struct amdgpu_device *adev, u64 *value)
895 {
896 	int r = 0;
897 
898 	mutex_lock(&adev->gfx.gfx_off_mutex);
899 
900 	r = amdgpu_dpm_get_entrycount_gfxoff(adev, value);
901 
902 	mutex_unlock(&adev->gfx.gfx_off_mutex);
903 
904 	return r;
905 }
906 
907 int amdgpu_get_gfx_off_status(struct amdgpu_device *adev, uint32_t *value)
908 {
909 
910 	int r = 0;
911 
912 	mutex_lock(&adev->gfx.gfx_off_mutex);
913 
914 	r = amdgpu_dpm_get_status_gfxoff(adev, value);
915 
916 	mutex_unlock(&adev->gfx.gfx_off_mutex);
917 
918 	return r;
919 }
920 
921 int amdgpu_gfx_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
922 {
923 	int r;
924 
925 	if (amdgpu_ras_is_supported(adev, ras_block->block)) {
926 		if (!amdgpu_persistent_edc_harvesting_supported(adev)) {
927 			r = amdgpu_ras_reset_error_status(adev, AMDGPU_RAS_BLOCK__GFX);
928 			if (r)
929 				return r;
930 		}
931 
932 		r = amdgpu_ras_block_late_init(adev, ras_block);
933 		if (r)
934 			return r;
935 
936 		if (amdgpu_sriov_vf(adev))
937 			return r;
938 
939 		if (adev->gfx.cp_ecc_error_irq.funcs) {
940 			r = amdgpu_irq_get(adev, &adev->gfx.cp_ecc_error_irq, 0);
941 			if (r)
942 				goto late_fini;
943 		}
944 	} else {
945 		amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
946 	}
947 
948 	return 0;
949 late_fini:
950 	amdgpu_ras_block_late_fini(adev, ras_block);
951 	return r;
952 }
953 
954 int amdgpu_gfx_ras_sw_init(struct amdgpu_device *adev)
955 {
956 	int err = 0;
957 	struct amdgpu_gfx_ras *ras = NULL;
958 
959 	/* adev->gfx.ras is NULL, which means gfx does not
960 	 * support ras function, then do nothing here.
961 	 */
962 	if (!adev->gfx.ras)
963 		return 0;
964 
965 	ras = adev->gfx.ras;
966 
967 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
968 	if (err) {
969 		dev_err(adev->dev, "Failed to register gfx ras block!\n");
970 		return err;
971 	}
972 
973 	strcpy(ras->ras_block.ras_comm.name, "gfx");
974 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__GFX;
975 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
976 	adev->gfx.ras_if = &ras->ras_block.ras_comm;
977 
978 	/* If not define special ras_late_init function, use gfx default ras_late_init */
979 	if (!ras->ras_block.ras_late_init)
980 		ras->ras_block.ras_late_init = amdgpu_gfx_ras_late_init;
981 
982 	/* If not defined special ras_cb function, use default ras_cb */
983 	if (!ras->ras_block.ras_cb)
984 		ras->ras_block.ras_cb = amdgpu_gfx_process_ras_data_cb;
985 
986 	return 0;
987 }
988 
989 int amdgpu_gfx_poison_consumption_handler(struct amdgpu_device *adev,
990 						struct amdgpu_iv_entry *entry)
991 {
992 	if (adev->gfx.ras && adev->gfx.ras->poison_consumption_handler)
993 		return adev->gfx.ras->poison_consumption_handler(adev, entry);
994 
995 	return 0;
996 }
997 
998 int amdgpu_gfx_process_ras_data_cb(struct amdgpu_device *adev,
999 		void *err_data,
1000 		struct amdgpu_iv_entry *entry)
1001 {
1002 	/* TODO ue will trigger an interrupt.
1003 	 *
1004 	 * When “Full RAS” is enabled, the per-IP interrupt sources should
1005 	 * be disabled and the driver should only look for the aggregated
1006 	 * interrupt via sync flood
1007 	 */
1008 	if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__GFX)) {
1009 		kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
1010 		if (adev->gfx.ras && adev->gfx.ras->ras_block.hw_ops &&
1011 		    adev->gfx.ras->ras_block.hw_ops->query_ras_error_count)
1012 			adev->gfx.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data);
1013 		amdgpu_ras_reset_gpu(adev);
1014 	}
1015 	return AMDGPU_RAS_SUCCESS;
1016 }
1017 
1018 int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
1019 				  struct amdgpu_irq_src *source,
1020 				  struct amdgpu_iv_entry *entry)
1021 {
1022 	struct ras_common_if *ras_if = adev->gfx.ras_if;
1023 	struct ras_dispatch_if ih_data = {
1024 		.entry = entry,
1025 	};
1026 
1027 	if (!ras_if)
1028 		return 0;
1029 
1030 	ih_data.head = *ras_if;
1031 
1032 	DRM_ERROR("CP ECC ERROR IRQ\n");
1033 	amdgpu_ras_interrupt_dispatch(adev, &ih_data);
1034 	return 0;
1035 }
1036 
1037 void amdgpu_gfx_ras_error_func(struct amdgpu_device *adev,
1038 		void *ras_error_status,
1039 		void (*func)(struct amdgpu_device *adev, void *ras_error_status,
1040 				int xcc_id))
1041 {
1042 	int i;
1043 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
1044 	uint32_t xcc_mask = GENMASK(num_xcc - 1, 0);
1045 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
1046 
1047 	if (err_data) {
1048 		err_data->ue_count = 0;
1049 		err_data->ce_count = 0;
1050 	}
1051 
1052 	for_each_inst(i, xcc_mask)
1053 		func(adev, ras_error_status, i);
1054 }
1055 
1056 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg, uint32_t xcc_id)
1057 {
1058 	signed long r, cnt = 0;
1059 	unsigned long flags;
1060 	uint32_t seq, reg_val_offs = 0, value = 0;
1061 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
1062 	struct amdgpu_ring *ring = &kiq->ring;
1063 
1064 	if (amdgpu_device_skip_hw_access(adev))
1065 		return 0;
1066 
1067 	if (adev->mes.ring[0].sched.ready)
1068 		return amdgpu_mes_rreg(adev, reg);
1069 
1070 	BUG_ON(!ring->funcs->emit_rreg);
1071 
1072 	spin_lock_irqsave(&kiq->ring_lock, flags);
1073 	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
1074 		pr_err("critical bug! too many kiq readers\n");
1075 		goto failed_unlock;
1076 	}
1077 	r = amdgpu_ring_alloc(ring, 32);
1078 	if (r)
1079 		goto failed_unlock;
1080 
1081 	amdgpu_ring_emit_rreg(ring, reg, reg_val_offs);
1082 	r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
1083 	if (r)
1084 		goto failed_undo;
1085 
1086 	amdgpu_ring_commit(ring);
1087 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1088 
1089 	r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1090 
1091 	/* don't wait anymore for gpu reset case because this way may
1092 	 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1093 	 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1094 	 * never return if we keep waiting in virt_kiq_rreg, which cause
1095 	 * gpu_recover() hang there.
1096 	 *
1097 	 * also don't wait anymore for IRQ context
1098 	 * */
1099 	if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1100 		goto failed_kiq_read;
1101 
1102 	might_sleep();
1103 	while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1104 		msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1105 		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1106 	}
1107 
1108 	if (cnt > MAX_KIQ_REG_TRY)
1109 		goto failed_kiq_read;
1110 
1111 	mb();
1112 	value = adev->wb.wb[reg_val_offs];
1113 	amdgpu_device_wb_free(adev, reg_val_offs);
1114 	return value;
1115 
1116 failed_undo:
1117 	amdgpu_ring_undo(ring);
1118 failed_unlock:
1119 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1120 failed_kiq_read:
1121 	if (reg_val_offs)
1122 		amdgpu_device_wb_free(adev, reg_val_offs);
1123 	dev_err(adev->dev, "failed to read reg:%x\n", reg);
1124 	return ~0;
1125 }
1126 
1127 void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v, uint32_t xcc_id)
1128 {
1129 	signed long r, cnt = 0;
1130 	unsigned long flags;
1131 	uint32_t seq;
1132 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
1133 	struct amdgpu_ring *ring = &kiq->ring;
1134 
1135 	BUG_ON(!ring->funcs->emit_wreg);
1136 
1137 	if (amdgpu_device_skip_hw_access(adev))
1138 		return;
1139 
1140 	if (adev->mes.ring[0].sched.ready) {
1141 		amdgpu_mes_wreg(adev, reg, v);
1142 		return;
1143 	}
1144 
1145 	spin_lock_irqsave(&kiq->ring_lock, flags);
1146 	r = amdgpu_ring_alloc(ring, 32);
1147 	if (r)
1148 		goto failed_unlock;
1149 
1150 	amdgpu_ring_emit_wreg(ring, reg, v);
1151 	r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
1152 	if (r)
1153 		goto failed_undo;
1154 
1155 	amdgpu_ring_commit(ring);
1156 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1157 
1158 	r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1159 
1160 	/* don't wait anymore for gpu reset case because this way may
1161 	 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1162 	 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1163 	 * never return if we keep waiting in virt_kiq_rreg, which cause
1164 	 * gpu_recover() hang there.
1165 	 *
1166 	 * also don't wait anymore for IRQ context
1167 	 * */
1168 	if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1169 		goto failed_kiq_write;
1170 
1171 	might_sleep();
1172 	while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1173 
1174 		msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1175 		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1176 	}
1177 
1178 	if (cnt > MAX_KIQ_REG_TRY)
1179 		goto failed_kiq_write;
1180 
1181 	return;
1182 
1183 failed_undo:
1184 	amdgpu_ring_undo(ring);
1185 failed_unlock:
1186 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1187 failed_kiq_write:
1188 	dev_err(adev->dev, "failed to write reg:%x\n", reg);
1189 }
1190 
1191 int amdgpu_gfx_get_num_kcq(struct amdgpu_device *adev)
1192 {
1193 	if (amdgpu_num_kcq == -1) {
1194 		return 8;
1195 	} else if (amdgpu_num_kcq > 8 || amdgpu_num_kcq < 0) {
1196 		dev_warn(adev->dev, "set kernel compute queue number to 8 due to invalid parameter provided by user\n");
1197 		return 8;
1198 	}
1199 	return amdgpu_num_kcq;
1200 }
1201 
1202 void amdgpu_gfx_cp_init_microcode(struct amdgpu_device *adev,
1203 				  uint32_t ucode_id)
1204 {
1205 	const struct gfx_firmware_header_v1_0 *cp_hdr;
1206 	const struct gfx_firmware_header_v2_0 *cp_hdr_v2_0;
1207 	struct amdgpu_firmware_info *info = NULL;
1208 	const struct firmware *ucode_fw;
1209 	unsigned int fw_size;
1210 
1211 	switch (ucode_id) {
1212 	case AMDGPU_UCODE_ID_CP_PFP:
1213 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1214 			adev->gfx.pfp_fw->data;
1215 		adev->gfx.pfp_fw_version =
1216 			le32_to_cpu(cp_hdr->header.ucode_version);
1217 		adev->gfx.pfp_feature_version =
1218 			le32_to_cpu(cp_hdr->ucode_feature_version);
1219 		ucode_fw = adev->gfx.pfp_fw;
1220 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1221 		break;
1222 	case AMDGPU_UCODE_ID_CP_RS64_PFP:
1223 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1224 			adev->gfx.pfp_fw->data;
1225 		adev->gfx.pfp_fw_version =
1226 			le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1227 		adev->gfx.pfp_feature_version =
1228 			le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1229 		ucode_fw = adev->gfx.pfp_fw;
1230 		fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1231 		break;
1232 	case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
1233 	case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
1234 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1235 			adev->gfx.pfp_fw->data;
1236 		ucode_fw = adev->gfx.pfp_fw;
1237 		fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1238 		break;
1239 	case AMDGPU_UCODE_ID_CP_ME:
1240 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1241 			adev->gfx.me_fw->data;
1242 		adev->gfx.me_fw_version =
1243 			le32_to_cpu(cp_hdr->header.ucode_version);
1244 		adev->gfx.me_feature_version =
1245 			le32_to_cpu(cp_hdr->ucode_feature_version);
1246 		ucode_fw = adev->gfx.me_fw;
1247 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1248 		break;
1249 	case AMDGPU_UCODE_ID_CP_RS64_ME:
1250 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1251 			adev->gfx.me_fw->data;
1252 		adev->gfx.me_fw_version =
1253 			le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1254 		adev->gfx.me_feature_version =
1255 			le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1256 		ucode_fw = adev->gfx.me_fw;
1257 		fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1258 		break;
1259 	case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
1260 	case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
1261 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1262 			adev->gfx.me_fw->data;
1263 		ucode_fw = adev->gfx.me_fw;
1264 		fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1265 		break;
1266 	case AMDGPU_UCODE_ID_CP_CE:
1267 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1268 			adev->gfx.ce_fw->data;
1269 		adev->gfx.ce_fw_version =
1270 			le32_to_cpu(cp_hdr->header.ucode_version);
1271 		adev->gfx.ce_feature_version =
1272 			le32_to_cpu(cp_hdr->ucode_feature_version);
1273 		ucode_fw = adev->gfx.ce_fw;
1274 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1275 		break;
1276 	case AMDGPU_UCODE_ID_CP_MEC1:
1277 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1278 			adev->gfx.mec_fw->data;
1279 		adev->gfx.mec_fw_version =
1280 			le32_to_cpu(cp_hdr->header.ucode_version);
1281 		adev->gfx.mec_feature_version =
1282 			le32_to_cpu(cp_hdr->ucode_feature_version);
1283 		ucode_fw = adev->gfx.mec_fw;
1284 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1285 			  le32_to_cpu(cp_hdr->jt_size) * 4;
1286 		break;
1287 	case AMDGPU_UCODE_ID_CP_MEC1_JT:
1288 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1289 			adev->gfx.mec_fw->data;
1290 		ucode_fw = adev->gfx.mec_fw;
1291 		fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1292 		break;
1293 	case AMDGPU_UCODE_ID_CP_MEC2:
1294 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1295 			adev->gfx.mec2_fw->data;
1296 		adev->gfx.mec2_fw_version =
1297 			le32_to_cpu(cp_hdr->header.ucode_version);
1298 		adev->gfx.mec2_feature_version =
1299 			le32_to_cpu(cp_hdr->ucode_feature_version);
1300 		ucode_fw = adev->gfx.mec2_fw;
1301 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1302 			  le32_to_cpu(cp_hdr->jt_size) * 4;
1303 		break;
1304 	case AMDGPU_UCODE_ID_CP_MEC2_JT:
1305 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1306 			adev->gfx.mec2_fw->data;
1307 		ucode_fw = adev->gfx.mec2_fw;
1308 		fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1309 		break;
1310 	case AMDGPU_UCODE_ID_CP_RS64_MEC:
1311 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1312 			adev->gfx.mec_fw->data;
1313 		adev->gfx.mec_fw_version =
1314 			le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1315 		adev->gfx.mec_feature_version =
1316 			le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1317 		ucode_fw = adev->gfx.mec_fw;
1318 		fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1319 		break;
1320 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
1321 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
1322 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
1323 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
1324 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1325 			adev->gfx.mec_fw->data;
1326 		ucode_fw = adev->gfx.mec_fw;
1327 		fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1328 		break;
1329 	default:
1330 		dev_err(adev->dev, "Invalid ucode id %u\n", ucode_id);
1331 		return;
1332 	}
1333 
1334 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1335 		info = &adev->firmware.ucode[ucode_id];
1336 		info->ucode_id = ucode_id;
1337 		info->fw = ucode_fw;
1338 		adev->firmware.fw_size += ALIGN(fw_size, PAGE_SIZE);
1339 	}
1340 }
1341 
1342 bool amdgpu_gfx_is_master_xcc(struct amdgpu_device *adev, int xcc_id)
1343 {
1344 	return !(xcc_id % (adev->gfx.num_xcc_per_xcp ?
1345 			adev->gfx.num_xcc_per_xcp : 1));
1346 }
1347 
1348 static ssize_t amdgpu_gfx_get_current_compute_partition(struct device *dev,
1349 						struct device_attribute *addr,
1350 						char *buf)
1351 {
1352 	struct drm_device *ddev = dev_get_drvdata(dev);
1353 	struct amdgpu_device *adev = drm_to_adev(ddev);
1354 	int mode;
1355 
1356 	mode = amdgpu_xcp_query_partition_mode(adev->xcp_mgr,
1357 					       AMDGPU_XCP_FL_NONE);
1358 
1359 	return sysfs_emit(buf, "%s\n", amdgpu_gfx_compute_mode_desc(mode));
1360 }
1361 
1362 static ssize_t amdgpu_gfx_set_compute_partition(struct device *dev,
1363 						struct device_attribute *addr,
1364 						const char *buf, size_t count)
1365 {
1366 	struct drm_device *ddev = dev_get_drvdata(dev);
1367 	struct amdgpu_device *adev = drm_to_adev(ddev);
1368 	enum amdgpu_gfx_partition mode;
1369 	int ret = 0, num_xcc;
1370 
1371 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1372 	if (num_xcc % 2 != 0)
1373 		return -EINVAL;
1374 
1375 	if (!strncasecmp("SPX", buf, strlen("SPX"))) {
1376 		mode = AMDGPU_SPX_PARTITION_MODE;
1377 	} else if (!strncasecmp("DPX", buf, strlen("DPX"))) {
1378 		/*
1379 		 * DPX mode needs AIDs to be in multiple of 2.
1380 		 * Each AID connects 2 XCCs.
1381 		 */
1382 		if (num_xcc%4)
1383 			return -EINVAL;
1384 		mode = AMDGPU_DPX_PARTITION_MODE;
1385 	} else if (!strncasecmp("TPX", buf, strlen("TPX"))) {
1386 		if (num_xcc != 6)
1387 			return -EINVAL;
1388 		mode = AMDGPU_TPX_PARTITION_MODE;
1389 	} else if (!strncasecmp("QPX", buf, strlen("QPX"))) {
1390 		if (num_xcc != 8)
1391 			return -EINVAL;
1392 		mode = AMDGPU_QPX_PARTITION_MODE;
1393 	} else if (!strncasecmp("CPX", buf, strlen("CPX"))) {
1394 		mode = AMDGPU_CPX_PARTITION_MODE;
1395 	} else {
1396 		return -EINVAL;
1397 	}
1398 
1399 	ret = amdgpu_xcp_switch_partition_mode(adev->xcp_mgr, mode);
1400 
1401 	if (ret)
1402 		return ret;
1403 
1404 	return count;
1405 }
1406 
1407 static const char *xcp_desc[] = {
1408 	[AMDGPU_SPX_PARTITION_MODE] = "SPX",
1409 	[AMDGPU_DPX_PARTITION_MODE] = "DPX",
1410 	[AMDGPU_TPX_PARTITION_MODE] = "TPX",
1411 	[AMDGPU_QPX_PARTITION_MODE] = "QPX",
1412 	[AMDGPU_CPX_PARTITION_MODE] = "CPX",
1413 };
1414 
1415 static ssize_t amdgpu_gfx_get_available_compute_partition(struct device *dev,
1416 						struct device_attribute *addr,
1417 						char *buf)
1418 {
1419 	struct drm_device *ddev = dev_get_drvdata(dev);
1420 	struct amdgpu_device *adev = drm_to_adev(ddev);
1421 	struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1422 	int size = 0, mode;
1423 	char *sep = "";
1424 
1425 	if (!xcp_mgr || !xcp_mgr->avail_xcp_modes)
1426 		return sysfs_emit(buf, "Not supported\n");
1427 
1428 	for_each_inst(mode, xcp_mgr->avail_xcp_modes) {
1429 		size += sysfs_emit_at(buf, size, "%s%s", sep, xcp_desc[mode]);
1430 		sep = ", ";
1431 	}
1432 
1433 	size += sysfs_emit_at(buf, size, "\n");
1434 
1435 	return size;
1436 }
1437 
1438 static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring)
1439 {
1440 	struct amdgpu_device *adev = ring->adev;
1441 	struct drm_gpu_scheduler *sched = &ring->sched;
1442 	struct drm_sched_entity entity;
1443 	static atomic_t counter;
1444 	struct dma_fence *f;
1445 	struct amdgpu_job *job;
1446 	struct amdgpu_ib *ib;
1447 	void *owner;
1448 	int i, r;
1449 
1450 	/* Initialize the scheduler entity */
1451 	r = drm_sched_entity_init(&entity, DRM_SCHED_PRIORITY_NORMAL,
1452 				  &sched, 1, NULL);
1453 	if (r) {
1454 		dev_err(adev->dev, "Failed setting up GFX kernel entity.\n");
1455 		goto err;
1456 	}
1457 
1458 	/*
1459 	 * Use some unique dummy value as the owner to make sure we execute
1460 	 * the cleaner shader on each submission. The value just need to change
1461 	 * for each submission and is otherwise meaningless.
1462 	 */
1463 	owner = (void *)(unsigned long)atomic_inc_return(&counter);
1464 
1465 	r = amdgpu_job_alloc_with_ib(ring->adev, &entity, owner,
1466 				     64, 0, &job);
1467 	if (r)
1468 		goto err;
1469 
1470 	job->enforce_isolation = true;
1471 	/* always run the cleaner shader */
1472 	job->run_cleaner_shader = true;
1473 
1474 	ib = &job->ibs[0];
1475 	for (i = 0; i <= ring->funcs->align_mask; ++i)
1476 		ib->ptr[i] = ring->funcs->nop;
1477 	ib->length_dw = ring->funcs->align_mask + 1;
1478 
1479 	f = amdgpu_job_submit(job);
1480 
1481 	r = dma_fence_wait(f, false);
1482 	if (r)
1483 		goto err;
1484 
1485 	dma_fence_put(f);
1486 
1487 	/* Clean up the scheduler entity */
1488 	drm_sched_entity_destroy(&entity);
1489 	return 0;
1490 
1491 err:
1492 	return r;
1493 }
1494 
1495 static int amdgpu_gfx_run_cleaner_shader(struct amdgpu_device *adev, int xcp_id)
1496 {
1497 	int num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1498 	struct amdgpu_ring *ring;
1499 	int num_xcc_to_clear;
1500 	int i, r, xcc_id;
1501 
1502 	if (adev->gfx.num_xcc_per_xcp)
1503 		num_xcc_to_clear = adev->gfx.num_xcc_per_xcp;
1504 	else
1505 		num_xcc_to_clear = 1;
1506 
1507 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1508 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
1509 			ring = &adev->gfx.compute_ring[i + xcc_id * adev->gfx.num_compute_rings];
1510 			if ((ring->xcp_id == xcp_id) && ring->sched.ready) {
1511 				r = amdgpu_gfx_run_cleaner_shader_job(ring);
1512 				if (r)
1513 					return r;
1514 				num_xcc_to_clear--;
1515 				break;
1516 			}
1517 		}
1518 	}
1519 
1520 	if (num_xcc_to_clear)
1521 		return -ENOENT;
1522 
1523 	return 0;
1524 }
1525 
1526 /**
1527  * amdgpu_gfx_set_run_cleaner_shader - Execute the AMDGPU GFX Cleaner Shader
1528  * @dev: The device structure
1529  * @attr: The device attribute structure
1530  * @buf: The buffer containing the input data
1531  * @count: The size of the input data
1532  *
1533  * Provides the sysfs interface to manually run a cleaner shader, which is
1534  * used to clear the GPU state between different tasks. Writing a value to the
1535  * 'run_cleaner_shader' sysfs file triggers the cleaner shader execution.
1536  * The value written corresponds to the partition index on multi-partition
1537  * devices. On single-partition devices, the value should be '0'.
1538  *
1539  * The cleaner shader clears the Local Data Store (LDS) and General Purpose
1540  * Registers (GPRs) to ensure data isolation between GPU workloads.
1541  *
1542  * Return: The number of bytes written to the sysfs file.
1543  */
1544 static ssize_t amdgpu_gfx_set_run_cleaner_shader(struct device *dev,
1545 						 struct device_attribute *attr,
1546 						 const char *buf,
1547 						 size_t count)
1548 {
1549 	struct drm_device *ddev = dev_get_drvdata(dev);
1550 	struct amdgpu_device *adev = drm_to_adev(ddev);
1551 	int ret;
1552 	long value;
1553 
1554 	if (amdgpu_in_reset(adev))
1555 		return -EPERM;
1556 	if (adev->in_suspend && !adev->in_runpm)
1557 		return -EPERM;
1558 
1559 	if (adev->gfx.disable_kq)
1560 		return -EPERM;
1561 
1562 	ret = kstrtol(buf, 0, &value);
1563 
1564 	if (ret)
1565 		return -EINVAL;
1566 
1567 	if (value < 0)
1568 		return -EINVAL;
1569 
1570 	if (adev->xcp_mgr) {
1571 		if (value >= adev->xcp_mgr->num_xcps)
1572 			return -EINVAL;
1573 	} else {
1574 		if (value > 1)
1575 			return -EINVAL;
1576 	}
1577 
1578 	ret = pm_runtime_get_sync(ddev->dev);
1579 	if (ret < 0) {
1580 		pm_runtime_put_autosuspend(ddev->dev);
1581 		return ret;
1582 	}
1583 
1584 	ret = amdgpu_gfx_run_cleaner_shader(adev, value);
1585 
1586 	pm_runtime_mark_last_busy(ddev->dev);
1587 	pm_runtime_put_autosuspend(ddev->dev);
1588 
1589 	if (ret)
1590 		return ret;
1591 
1592 	return count;
1593 }
1594 
1595 /**
1596  * amdgpu_gfx_get_enforce_isolation - Query AMDGPU GFX Enforce Isolation Settings
1597  * @dev: The device structure
1598  * @attr: The device attribute structure
1599  * @buf: The buffer to store the output data
1600  *
1601  * Provides the sysfs read interface to get the current settings of the 'enforce_isolation'
1602  * feature for each GPU partition. Reading from the 'enforce_isolation'
1603  * sysfs file returns the isolation settings for all partitions, where '0'
1604  * indicates disabled, '1' indicates enabled, and '2' indicates enabled in legacy mode.
1605  *
1606  * Return: The number of bytes read from the sysfs file.
1607  */
1608 static ssize_t amdgpu_gfx_get_enforce_isolation(struct device *dev,
1609 						struct device_attribute *attr,
1610 						char *buf)
1611 {
1612 	struct drm_device *ddev = dev_get_drvdata(dev);
1613 	struct amdgpu_device *adev = drm_to_adev(ddev);
1614 	int i;
1615 	ssize_t size = 0;
1616 
1617 	if (adev->xcp_mgr) {
1618 		for (i = 0; i < adev->xcp_mgr->num_xcps; i++) {
1619 			size += sysfs_emit_at(buf, size, "%u", adev->enforce_isolation[i]);
1620 			if (i < (adev->xcp_mgr->num_xcps - 1))
1621 				size += sysfs_emit_at(buf, size, " ");
1622 		}
1623 		buf[size++] = '\n';
1624 	} else {
1625 		size = sysfs_emit_at(buf, 0, "%u\n", adev->enforce_isolation[0]);
1626 	}
1627 
1628 	return size;
1629 }
1630 
1631 /**
1632  * amdgpu_gfx_set_enforce_isolation - Control AMDGPU GFX Enforce Isolation
1633  * @dev: The device structure
1634  * @attr: The device attribute structure
1635  * @buf: The buffer containing the input data
1636  * @count: The size of the input data
1637  *
1638  * This function allows control over the 'enforce_isolation' feature, which
1639  * serializes access to the graphics engine. Writing '1', '2', or '0' to the
1640  * 'enforce_isolation' sysfs file enables (full or legacy) or disables process
1641  * isolation for each partition. The input should specify the setting for all
1642  * partitions.
1643  *
1644  * Return: The number of bytes written to the sysfs file.
1645  */
1646 static ssize_t amdgpu_gfx_set_enforce_isolation(struct device *dev,
1647 						struct device_attribute *attr,
1648 						const char *buf, size_t count)
1649 {
1650 	struct drm_device *ddev = dev_get_drvdata(dev);
1651 	struct amdgpu_device *adev = drm_to_adev(ddev);
1652 	long partition_values[MAX_XCP] = {0};
1653 	int ret, i, num_partitions;
1654 	const char *input_buf = buf;
1655 
1656 	for (i = 0; i < (adev->xcp_mgr ? adev->xcp_mgr->num_xcps : 1); i++) {
1657 		ret = sscanf(input_buf, "%ld", &partition_values[i]);
1658 		if (ret <= 0)
1659 			break;
1660 
1661 		/* Move the pointer to the next value in the string */
1662 		input_buf = strchr(input_buf, ' ');
1663 		if (input_buf) {
1664 			input_buf++;
1665 		} else {
1666 			i++;
1667 			break;
1668 		}
1669 	}
1670 	num_partitions = i;
1671 
1672 	if (adev->xcp_mgr && num_partitions != adev->xcp_mgr->num_xcps)
1673 		return -EINVAL;
1674 
1675 	if (!adev->xcp_mgr && num_partitions != 1)
1676 		return -EINVAL;
1677 
1678 	for (i = 0; i < num_partitions; i++) {
1679 		if (partition_values[i] != 0 &&
1680 		    partition_values[i] != 1 &&
1681 		    partition_values[i] != 2)
1682 			return -EINVAL;
1683 	}
1684 
1685 	mutex_lock(&adev->enforce_isolation_mutex);
1686 	for (i = 0; i < num_partitions; i++) {
1687 		switch (partition_values[i]) {
1688 		case 0:
1689 		default:
1690 			adev->enforce_isolation[i] = AMDGPU_ENFORCE_ISOLATION_DISABLE;
1691 			break;
1692 		case 1:
1693 			adev->enforce_isolation[i] =
1694 				AMDGPU_ENFORCE_ISOLATION_ENABLE;
1695 			break;
1696 		case 2:
1697 			adev->enforce_isolation[i] =
1698 				AMDGPU_ENFORCE_ISOLATION_ENABLE_LEGACY;
1699 			break;
1700 		}
1701 	}
1702 	mutex_unlock(&adev->enforce_isolation_mutex);
1703 
1704 	amdgpu_mes_update_enforce_isolation(adev);
1705 
1706 	return count;
1707 }
1708 
1709 static ssize_t amdgpu_gfx_get_gfx_reset_mask(struct device *dev,
1710 						struct device_attribute *attr,
1711 						char *buf)
1712 {
1713 	struct drm_device *ddev = dev_get_drvdata(dev);
1714 	struct amdgpu_device *adev = drm_to_adev(ddev);
1715 
1716 	if (!adev)
1717 		return -ENODEV;
1718 
1719 	return amdgpu_show_reset_mask(buf, adev->gfx.gfx_supported_reset);
1720 }
1721 
1722 static ssize_t amdgpu_gfx_get_compute_reset_mask(struct device *dev,
1723 						struct device_attribute *attr,
1724 						char *buf)
1725 {
1726 	struct drm_device *ddev = dev_get_drvdata(dev);
1727 	struct amdgpu_device *adev = drm_to_adev(ddev);
1728 
1729 	if (!adev)
1730 		return -ENODEV;
1731 
1732 	return amdgpu_show_reset_mask(buf, adev->gfx.compute_supported_reset);
1733 }
1734 
1735 static DEVICE_ATTR(run_cleaner_shader, 0200,
1736 		   NULL, amdgpu_gfx_set_run_cleaner_shader);
1737 
1738 static DEVICE_ATTR(enforce_isolation, 0644,
1739 		   amdgpu_gfx_get_enforce_isolation,
1740 		   amdgpu_gfx_set_enforce_isolation);
1741 
1742 static DEVICE_ATTR(current_compute_partition, 0644,
1743 		   amdgpu_gfx_get_current_compute_partition,
1744 		   amdgpu_gfx_set_compute_partition);
1745 
1746 static DEVICE_ATTR(available_compute_partition, 0444,
1747 		   amdgpu_gfx_get_available_compute_partition, NULL);
1748 static DEVICE_ATTR(gfx_reset_mask, 0444,
1749 		   amdgpu_gfx_get_gfx_reset_mask, NULL);
1750 
1751 static DEVICE_ATTR(compute_reset_mask, 0444,
1752 		   amdgpu_gfx_get_compute_reset_mask, NULL);
1753 
1754 static int amdgpu_gfx_sysfs_xcp_init(struct amdgpu_device *adev)
1755 {
1756 	struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1757 	bool xcp_switch_supported;
1758 	int r;
1759 
1760 	if (!xcp_mgr)
1761 		return 0;
1762 
1763 	xcp_switch_supported =
1764 		(xcp_mgr->funcs && xcp_mgr->funcs->switch_partition_mode);
1765 
1766 	if (!xcp_switch_supported)
1767 		dev_attr_current_compute_partition.attr.mode &=
1768 			~(S_IWUSR | S_IWGRP | S_IWOTH);
1769 
1770 	r = device_create_file(adev->dev, &dev_attr_current_compute_partition);
1771 	if (r)
1772 		return r;
1773 
1774 	if (xcp_switch_supported)
1775 		r = device_create_file(adev->dev,
1776 				       &dev_attr_available_compute_partition);
1777 
1778 	return r;
1779 }
1780 
1781 static void amdgpu_gfx_sysfs_xcp_fini(struct amdgpu_device *adev)
1782 {
1783 	struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1784 	bool xcp_switch_supported;
1785 
1786 	if (!xcp_mgr)
1787 		return;
1788 
1789 	xcp_switch_supported =
1790 		(xcp_mgr->funcs && xcp_mgr->funcs->switch_partition_mode);
1791 	device_remove_file(adev->dev, &dev_attr_current_compute_partition);
1792 
1793 	if (xcp_switch_supported)
1794 		device_remove_file(adev->dev,
1795 				   &dev_attr_available_compute_partition);
1796 }
1797 
1798 static int amdgpu_gfx_sysfs_isolation_shader_init(struct amdgpu_device *adev)
1799 {
1800 	int r;
1801 
1802 	r = device_create_file(adev->dev, &dev_attr_enforce_isolation);
1803 	if (r)
1804 		return r;
1805 	if (adev->gfx.enable_cleaner_shader)
1806 		r = device_create_file(adev->dev, &dev_attr_run_cleaner_shader);
1807 
1808 	return r;
1809 }
1810 
1811 static void amdgpu_gfx_sysfs_isolation_shader_fini(struct amdgpu_device *adev)
1812 {
1813 	device_remove_file(adev->dev, &dev_attr_enforce_isolation);
1814 	if (adev->gfx.enable_cleaner_shader)
1815 		device_remove_file(adev->dev, &dev_attr_run_cleaner_shader);
1816 }
1817 
1818 static int amdgpu_gfx_sysfs_reset_mask_init(struct amdgpu_device *adev)
1819 {
1820 	int r = 0;
1821 
1822 	if (!amdgpu_gpu_recovery)
1823 		return r;
1824 
1825 	if (adev->gfx.num_gfx_rings) {
1826 		r = device_create_file(adev->dev, &dev_attr_gfx_reset_mask);
1827 		if (r)
1828 			return r;
1829 	}
1830 
1831 	if (adev->gfx.num_compute_rings) {
1832 		r = device_create_file(adev->dev, &dev_attr_compute_reset_mask);
1833 		if (r)
1834 			return r;
1835 	}
1836 
1837 	return r;
1838 }
1839 
1840 static void amdgpu_gfx_sysfs_reset_mask_fini(struct amdgpu_device *adev)
1841 {
1842 	if (!amdgpu_gpu_recovery)
1843 		return;
1844 
1845 	if (adev->gfx.num_gfx_rings)
1846 		device_remove_file(adev->dev, &dev_attr_gfx_reset_mask);
1847 
1848 	if (adev->gfx.num_compute_rings)
1849 		device_remove_file(adev->dev, &dev_attr_compute_reset_mask);
1850 }
1851 
1852 int amdgpu_gfx_sysfs_init(struct amdgpu_device *adev)
1853 {
1854 	int r;
1855 
1856 	r = amdgpu_gfx_sysfs_xcp_init(adev);
1857 	if (r) {
1858 		dev_err(adev->dev, "failed to create xcp sysfs files");
1859 		return r;
1860 	}
1861 
1862 	r = amdgpu_gfx_sysfs_isolation_shader_init(adev);
1863 	if (r)
1864 		dev_err(adev->dev, "failed to create isolation sysfs files");
1865 
1866 	r = amdgpu_gfx_sysfs_reset_mask_init(adev);
1867 	if (r)
1868 		dev_err(adev->dev, "failed to create reset mask sysfs files");
1869 
1870 	return r;
1871 }
1872 
1873 void amdgpu_gfx_sysfs_fini(struct amdgpu_device *adev)
1874 {
1875 	if (adev->dev->kobj.sd) {
1876 		amdgpu_gfx_sysfs_xcp_fini(adev);
1877 		amdgpu_gfx_sysfs_isolation_shader_fini(adev);
1878 		amdgpu_gfx_sysfs_reset_mask_fini(adev);
1879 	}
1880 }
1881 
1882 int amdgpu_gfx_cleaner_shader_sw_init(struct amdgpu_device *adev,
1883 				      unsigned int cleaner_shader_size)
1884 {
1885 	if (!adev->gfx.enable_cleaner_shader)
1886 		return -EOPNOTSUPP;
1887 
1888 	return amdgpu_bo_create_kernel(adev, cleaner_shader_size, PAGE_SIZE,
1889 				       AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT,
1890 				       &adev->gfx.cleaner_shader_obj,
1891 				       &adev->gfx.cleaner_shader_gpu_addr,
1892 				       (void **)&adev->gfx.cleaner_shader_cpu_ptr);
1893 }
1894 
1895 void amdgpu_gfx_cleaner_shader_sw_fini(struct amdgpu_device *adev)
1896 {
1897 	if (!adev->gfx.enable_cleaner_shader)
1898 		return;
1899 
1900 	amdgpu_bo_free_kernel(&adev->gfx.cleaner_shader_obj,
1901 			      &adev->gfx.cleaner_shader_gpu_addr,
1902 			      (void **)&adev->gfx.cleaner_shader_cpu_ptr);
1903 }
1904 
1905 void amdgpu_gfx_cleaner_shader_init(struct amdgpu_device *adev,
1906 				    unsigned int cleaner_shader_size,
1907 				    const void *cleaner_shader_ptr)
1908 {
1909 	if (!adev->gfx.enable_cleaner_shader)
1910 		return;
1911 
1912 	if (adev->gfx.cleaner_shader_cpu_ptr && cleaner_shader_ptr)
1913 		memcpy_toio(adev->gfx.cleaner_shader_cpu_ptr, cleaner_shader_ptr,
1914 			    cleaner_shader_size);
1915 }
1916 
1917 /**
1918  * amdgpu_gfx_kfd_sch_ctrl - Control the KFD scheduler from the KGD (Graphics Driver)
1919  * @adev: amdgpu_device pointer
1920  * @idx: Index of the scheduler to control
1921  * @enable: Whether to enable or disable the KFD scheduler
1922  *
1923  * This function is used to control the KFD (Kernel Fusion Driver) scheduler
1924  * from the KGD. It is part of the cleaner shader feature. This function plays
1925  * a key role in enforcing process isolation on the GPU.
1926  *
1927  * The function uses a reference count mechanism (kfd_sch_req_count) to keep
1928  * track of the number of requests to enable the KFD scheduler. When a request
1929  * to enable the KFD scheduler is made, the reference count is decremented.
1930  * When the reference count reaches zero, a delayed work is scheduled to
1931  * enforce isolation after a delay of GFX_SLICE_PERIOD.
1932  *
1933  * When a request to disable the KFD scheduler is made, the function first
1934  * checks if the reference count is zero. If it is, it cancels the delayed work
1935  * for enforcing isolation and checks if the KFD scheduler is active. If the
1936  * KFD scheduler is active, it sends a request to stop the KFD scheduler and
1937  * sets the KFD scheduler state to inactive. Then, it increments the reference
1938  * count.
1939  *
1940  * The function is synchronized using the kfd_sch_mutex to ensure that the KFD
1941  * scheduler state and reference count are updated atomically.
1942  *
1943  * Note: If the reference count is already zero when a request to enable the
1944  * KFD scheduler is made, it means there's an imbalance bug somewhere. The
1945  * function triggers a warning in this case.
1946  */
1947 static void amdgpu_gfx_kfd_sch_ctrl(struct amdgpu_device *adev, u32 idx,
1948 				    bool enable)
1949 {
1950 	mutex_lock(&adev->gfx.kfd_sch_mutex);
1951 
1952 	if (enable) {
1953 		/* If the count is already 0, it means there's an imbalance bug somewhere.
1954 		 * Note that the bug may be in a different caller than the one which triggers the
1955 		 * WARN_ON_ONCE.
1956 		 */
1957 		if (WARN_ON_ONCE(adev->gfx.kfd_sch_req_count[idx] == 0)) {
1958 			dev_err(adev->dev, "Attempted to enable KFD scheduler when reference count is already zero\n");
1959 			goto unlock;
1960 		}
1961 
1962 		adev->gfx.kfd_sch_req_count[idx]--;
1963 
1964 		if (adev->gfx.kfd_sch_req_count[idx] == 0 &&
1965 		    adev->gfx.kfd_sch_inactive[idx]) {
1966 			schedule_delayed_work(&adev->gfx.enforce_isolation[idx].work,
1967 					      msecs_to_jiffies(adev->gfx.enforce_isolation_time[idx]));
1968 		}
1969 	} else {
1970 		if (adev->gfx.kfd_sch_req_count[idx] == 0) {
1971 			cancel_delayed_work_sync(&adev->gfx.enforce_isolation[idx].work);
1972 			if (!adev->gfx.kfd_sch_inactive[idx]) {
1973 				amdgpu_amdkfd_stop_sched(adev, idx);
1974 				adev->gfx.kfd_sch_inactive[idx] = true;
1975 			}
1976 		}
1977 
1978 		adev->gfx.kfd_sch_req_count[idx]++;
1979 	}
1980 
1981 unlock:
1982 	mutex_unlock(&adev->gfx.kfd_sch_mutex);
1983 }
1984 
1985 /**
1986  * amdgpu_gfx_enforce_isolation_handler - work handler for enforcing shader isolation
1987  *
1988  * @work: work_struct.
1989  *
1990  * This function is the work handler for enforcing shader isolation on AMD GPUs.
1991  * It counts the number of emitted fences for each GFX and compute ring. If there
1992  * are any fences, it schedules the `enforce_isolation_work` to be run after a
1993  * delay of `GFX_SLICE_PERIOD`. If there are no fences, it signals the Kernel Fusion
1994  * Driver (KFD) to resume the runqueue. The function is synchronized using the
1995  * `enforce_isolation_mutex`.
1996  */
1997 void amdgpu_gfx_enforce_isolation_handler(struct work_struct *work)
1998 {
1999 	struct amdgpu_isolation_work *isolation_work =
2000 		container_of(work, struct amdgpu_isolation_work, work.work);
2001 	struct amdgpu_device *adev = isolation_work->adev;
2002 	u32 i, idx, fences = 0;
2003 
2004 	if (isolation_work->xcp_id == AMDGPU_XCP_NO_PARTITION)
2005 		idx = 0;
2006 	else
2007 		idx = isolation_work->xcp_id;
2008 
2009 	if (idx >= MAX_XCP)
2010 		return;
2011 
2012 	mutex_lock(&adev->enforce_isolation_mutex);
2013 	for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i) {
2014 		if (isolation_work->xcp_id == adev->gfx.gfx_ring[i].xcp_id)
2015 			fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
2016 	}
2017 	for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i) {
2018 		if (isolation_work->xcp_id == adev->gfx.compute_ring[i].xcp_id)
2019 			fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
2020 	}
2021 	if (fences) {
2022 		/* we've already had our timeslice, so let's wrap this up */
2023 		schedule_delayed_work(&adev->gfx.enforce_isolation[idx].work,
2024 				      msecs_to_jiffies(1));
2025 	} else {
2026 		/* Tell KFD to resume the runqueue */
2027 		if (adev->kfd.init_complete) {
2028 			WARN_ON_ONCE(!adev->gfx.kfd_sch_inactive[idx]);
2029 			WARN_ON_ONCE(adev->gfx.kfd_sch_req_count[idx]);
2030 			amdgpu_amdkfd_start_sched(adev, idx);
2031 			adev->gfx.kfd_sch_inactive[idx] = false;
2032 		}
2033 	}
2034 	mutex_unlock(&adev->enforce_isolation_mutex);
2035 }
2036 
2037 /**
2038  * amdgpu_gfx_enforce_isolation_wait_for_kfd - Manage KFD wait period for process isolation
2039  * @adev: amdgpu_device pointer
2040  * @idx: Index of the GPU partition
2041  *
2042  * When kernel submissions come in, the jobs are given a time slice and once
2043  * that time slice is up, if there are KFD user queues active, kernel
2044  * submissions are blocked until KFD has had its time slice. Once the KFD time
2045  * slice is up, KFD user queues are preempted and kernel submissions are
2046  * unblocked and allowed to run again.
2047  */
2048 static void
2049 amdgpu_gfx_enforce_isolation_wait_for_kfd(struct amdgpu_device *adev,
2050 					  u32 idx)
2051 {
2052 	unsigned long cjiffies;
2053 	bool wait = false;
2054 
2055 	mutex_lock(&adev->enforce_isolation_mutex);
2056 	if (adev->enforce_isolation[idx] == AMDGPU_ENFORCE_ISOLATION_ENABLE) {
2057 		/* set the initial values if nothing is set */
2058 		if (!adev->gfx.enforce_isolation_jiffies[idx]) {
2059 			adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
2060 			adev->gfx.enforce_isolation_time[idx] =	GFX_SLICE_PERIOD_MS;
2061 		}
2062 		/* Make sure KFD gets a chance to run */
2063 		if (amdgpu_amdkfd_compute_active(adev, idx)) {
2064 			cjiffies = jiffies;
2065 			if (time_after(cjiffies, adev->gfx.enforce_isolation_jiffies[idx])) {
2066 				cjiffies -= adev->gfx.enforce_isolation_jiffies[idx];
2067 				if ((jiffies_to_msecs(cjiffies) >= GFX_SLICE_PERIOD_MS)) {
2068 					/* if our time is up, let KGD work drain before scheduling more */
2069 					wait = true;
2070 					/* reset the timer period */
2071 					adev->gfx.enforce_isolation_time[idx] =	GFX_SLICE_PERIOD_MS;
2072 				} else {
2073 					/* set the timer period to what's left in our time slice */
2074 					adev->gfx.enforce_isolation_time[idx] =
2075 						GFX_SLICE_PERIOD_MS - jiffies_to_msecs(cjiffies);
2076 				}
2077 			} else {
2078 				/* if jiffies wrap around we will just wait a little longer */
2079 				adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
2080 			}
2081 		} else {
2082 			/* if there is no KFD work, then set the full slice period */
2083 			adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
2084 			adev->gfx.enforce_isolation_time[idx] = GFX_SLICE_PERIOD_MS;
2085 		}
2086 	}
2087 	mutex_unlock(&adev->enforce_isolation_mutex);
2088 
2089 	if (wait)
2090 		msleep(GFX_SLICE_PERIOD_MS);
2091 }
2092 
2093 /**
2094  * amdgpu_gfx_enforce_isolation_ring_begin_use - Begin use of a ring with enforced isolation
2095  * @ring: Pointer to the amdgpu_ring structure
2096  *
2097  * Ring begin_use helper implementation for gfx which serializes access to the
2098  * gfx IP between kernel submission IOCTLs and KFD user queues when isolation
2099  * enforcement is enabled. The kernel submission IOCTLs and KFD user queues
2100  * each get a time slice when both are active.
2101  */
2102 void amdgpu_gfx_enforce_isolation_ring_begin_use(struct amdgpu_ring *ring)
2103 {
2104 	struct amdgpu_device *adev = ring->adev;
2105 	u32 idx;
2106 	bool sched_work = false;
2107 
2108 	if (!adev->gfx.enable_cleaner_shader)
2109 		return;
2110 
2111 	if (ring->xcp_id == AMDGPU_XCP_NO_PARTITION)
2112 		idx = 0;
2113 	else
2114 		idx = ring->xcp_id;
2115 
2116 	if (idx >= MAX_XCP)
2117 		return;
2118 
2119 	/* Don't submit more work until KFD has had some time */
2120 	amdgpu_gfx_enforce_isolation_wait_for_kfd(adev, idx);
2121 
2122 	mutex_lock(&adev->enforce_isolation_mutex);
2123 	if (adev->enforce_isolation[idx] == AMDGPU_ENFORCE_ISOLATION_ENABLE) {
2124 		if (adev->kfd.init_complete)
2125 			sched_work = true;
2126 	}
2127 	mutex_unlock(&adev->enforce_isolation_mutex);
2128 
2129 	if (sched_work)
2130 		amdgpu_gfx_kfd_sch_ctrl(adev, idx, false);
2131 }
2132 
2133 /**
2134  * amdgpu_gfx_enforce_isolation_ring_end_use - End use of a ring with enforced isolation
2135  * @ring: Pointer to the amdgpu_ring structure
2136  *
2137  * Ring end_use helper implementation for gfx which serializes access to the
2138  * gfx IP between kernel submission IOCTLs and KFD user queues when isolation
2139  * enforcement is enabled. The kernel submission IOCTLs and KFD user queues
2140  * each get a time slice when both are active.
2141  */
2142 void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
2143 {
2144 	struct amdgpu_device *adev = ring->adev;
2145 	u32 idx;
2146 	bool sched_work = false;
2147 
2148 	if (!adev->gfx.enable_cleaner_shader)
2149 		return;
2150 
2151 	if (ring->xcp_id == AMDGPU_XCP_NO_PARTITION)
2152 		idx = 0;
2153 	else
2154 		idx = ring->xcp_id;
2155 
2156 	if (idx >= MAX_XCP)
2157 		return;
2158 
2159 	mutex_lock(&adev->enforce_isolation_mutex);
2160 	if (adev->enforce_isolation[idx] == AMDGPU_ENFORCE_ISOLATION_ENABLE) {
2161 		if (adev->kfd.init_complete)
2162 			sched_work = true;
2163 	}
2164 	mutex_unlock(&adev->enforce_isolation_mutex);
2165 
2166 	if (sched_work)
2167 		amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);
2168 }
2169 
2170 void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
2171 {
2172 	struct amdgpu_device *adev =
2173 		container_of(work, struct amdgpu_device, gfx.idle_work.work);
2174 	enum PP_SMC_POWER_PROFILE profile;
2175 	u32 i, fences = 0;
2176 	int r;
2177 
2178 	if (adev->gfx.num_gfx_rings)
2179 		profile = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
2180 	else
2181 		profile = PP_SMC_POWER_PROFILE_COMPUTE;
2182 
2183 	for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
2184 		fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
2185 	for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
2186 		fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
2187 	if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)) {
2188 		mutex_lock(&adev->gfx.workload_profile_mutex);
2189 		if (adev->gfx.workload_profile_active) {
2190 			r = amdgpu_dpm_switch_power_profile(adev, profile, false);
2191 			if (r)
2192 				dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
2193 					 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
2194 					 "fullscreen 3D" : "compute");
2195 			adev->gfx.workload_profile_active = false;
2196 		}
2197 		mutex_unlock(&adev->gfx.workload_profile_mutex);
2198 	} else {
2199 		schedule_delayed_work(&adev->gfx.idle_work, GFX_PROFILE_IDLE_TIMEOUT);
2200 	}
2201 }
2202 
2203 void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
2204 {
2205 	struct amdgpu_device *adev = ring->adev;
2206 	enum PP_SMC_POWER_PROFILE profile;
2207 	int r;
2208 
2209 	if (adev->gfx.num_gfx_rings)
2210 		profile = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
2211 	else
2212 		profile = PP_SMC_POWER_PROFILE_COMPUTE;
2213 
2214 	atomic_inc(&adev->gfx.total_submission_cnt);
2215 
2216 	cancel_delayed_work_sync(&adev->gfx.idle_work);
2217 
2218 	/* We can safely return early here because we've cancelled the
2219 	 * the delayed work so there is no one else to set it to false
2220 	 * and we don't care if someone else sets it to true.
2221 	 */
2222 	if (adev->gfx.workload_profile_active)
2223 		return;
2224 
2225 	mutex_lock(&adev->gfx.workload_profile_mutex);
2226 	if (!adev->gfx.workload_profile_active) {
2227 		r = amdgpu_dpm_switch_power_profile(adev, profile, true);
2228 		if (r)
2229 			dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
2230 				 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
2231 				 "fullscreen 3D" : "compute");
2232 		adev->gfx.workload_profile_active = true;
2233 	}
2234 	mutex_unlock(&adev->gfx.workload_profile_mutex);
2235 }
2236 
2237 void amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring *ring)
2238 {
2239 	atomic_dec(&ring->adev->gfx.total_submission_cnt);
2240 
2241 	schedule_delayed_work(&ring->adev->gfx.idle_work, GFX_PROFILE_IDLE_TIMEOUT);
2242 }
2243 
2244 /*
2245  * debugfs for to enable/disable gfx job submission to specific core.
2246  */
2247 #if defined(CONFIG_DEBUG_FS)
2248 static int amdgpu_debugfs_gfx_sched_mask_set(void *data, u64 val)
2249 {
2250 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
2251 	u32 i;
2252 	u64 mask = 0;
2253 	struct amdgpu_ring *ring;
2254 
2255 	if (!adev)
2256 		return -ENODEV;
2257 
2258 	mask = (1ULL << adev->gfx.num_gfx_rings) - 1;
2259 	if ((val & mask) == 0)
2260 		return -EINVAL;
2261 
2262 	for (i = 0; i < adev->gfx.num_gfx_rings; ++i) {
2263 		ring = &adev->gfx.gfx_ring[i];
2264 		if (val & (1 << i))
2265 			ring->sched.ready = true;
2266 		else
2267 			ring->sched.ready = false;
2268 	}
2269 	/* publish sched.ready flag update effective immediately across smp */
2270 	smp_rmb();
2271 	return 0;
2272 }
2273 
2274 static int amdgpu_debugfs_gfx_sched_mask_get(void *data, u64 *val)
2275 {
2276 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
2277 	u32 i;
2278 	u64 mask = 0;
2279 	struct amdgpu_ring *ring;
2280 
2281 	if (!adev)
2282 		return -ENODEV;
2283 	for (i = 0; i < adev->gfx.num_gfx_rings; ++i) {
2284 		ring = &adev->gfx.gfx_ring[i];
2285 		if (ring->sched.ready)
2286 			mask |= 1ULL << i;
2287 	}
2288 
2289 	*val = mask;
2290 	return 0;
2291 }
2292 
2293 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gfx_sched_mask_fops,
2294 			 amdgpu_debugfs_gfx_sched_mask_get,
2295 			 amdgpu_debugfs_gfx_sched_mask_set, "%llx\n");
2296 
2297 #endif
2298 
2299 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev)
2300 {
2301 #if defined(CONFIG_DEBUG_FS)
2302 	struct drm_minor *minor = adev_to_drm(adev)->primary;
2303 	struct dentry *root = minor->debugfs_root;
2304 	char name[32];
2305 
2306 	if (!(adev->gfx.num_gfx_rings > 1))
2307 		return;
2308 	sprintf(name, "amdgpu_gfx_sched_mask");
2309 	debugfs_create_file(name, 0600, root, adev,
2310 			    &amdgpu_debugfs_gfx_sched_mask_fops);
2311 #endif
2312 }
2313 
2314 /*
2315  * debugfs for to enable/disable compute job submission to specific core.
2316  */
2317 #if defined(CONFIG_DEBUG_FS)
2318 static int amdgpu_debugfs_compute_sched_mask_set(void *data, u64 val)
2319 {
2320 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
2321 	u32 i;
2322 	u64 mask = 0;
2323 	struct amdgpu_ring *ring;
2324 
2325 	if (!adev)
2326 		return -ENODEV;
2327 
2328 	mask = (1ULL << adev->gfx.num_compute_rings) - 1;
2329 	if ((val & mask) == 0)
2330 		return -EINVAL;
2331 
2332 	for (i = 0; i < adev->gfx.num_compute_rings; ++i) {
2333 		ring = &adev->gfx.compute_ring[i];
2334 		if (val & (1 << i))
2335 			ring->sched.ready = true;
2336 		else
2337 			ring->sched.ready = false;
2338 	}
2339 
2340 	/* publish sched.ready flag update effective immediately across smp */
2341 	smp_rmb();
2342 	return 0;
2343 }
2344 
2345 static int amdgpu_debugfs_compute_sched_mask_get(void *data, u64 *val)
2346 {
2347 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
2348 	u32 i;
2349 	u64 mask = 0;
2350 	struct amdgpu_ring *ring;
2351 
2352 	if (!adev)
2353 		return -ENODEV;
2354 	for (i = 0; i < adev->gfx.num_compute_rings; ++i) {
2355 		ring = &adev->gfx.compute_ring[i];
2356 		if (ring->sched.ready)
2357 			mask |= 1ULL << i;
2358 	}
2359 
2360 	*val = mask;
2361 	return 0;
2362 }
2363 
2364 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_compute_sched_mask_fops,
2365 			 amdgpu_debugfs_compute_sched_mask_get,
2366 			 amdgpu_debugfs_compute_sched_mask_set, "%llx\n");
2367 
2368 #endif
2369 
2370 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
2371 {
2372 #if defined(CONFIG_DEBUG_FS)
2373 	struct drm_minor *minor = adev_to_drm(adev)->primary;
2374 	struct dentry *root = minor->debugfs_root;
2375 	char name[32];
2376 
2377 	if (!(adev->gfx.num_compute_rings > 1))
2378 		return;
2379 	sprintf(name, "amdgpu_compute_sched_mask");
2380 	debugfs_create_file(name, 0600, root, adev,
2381 			    &amdgpu_debugfs_compute_sched_mask_fops);
2382 #endif
2383 }
2384