xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c (revision 0a8d25285feb68608acdf778983ee5f4d72707e8)
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 "amdgpu.h"
28 #include "amdgpu_gfx.h"
29 #include "amdgpu_rlc.h"
30 #include "amdgpu_ras.h"
31 #include "amdgpu_xcp.h"
32 #include "amdgpu_xgmi.h"
33 
34 /* delay 0.1 second to enable gfx off feature */
35 #define GFX_OFF_DELAY_ENABLE         msecs_to_jiffies(100)
36 
37 #define GFX_OFF_NO_DELAY 0
38 
39 /*
40  * GPU GFX IP block helpers function.
41  */
42 
43 int amdgpu_gfx_mec_queue_to_bit(struct amdgpu_device *adev, int mec,
44 				int pipe, int queue)
45 {
46 	int bit = 0;
47 
48 	bit += mec * adev->gfx.mec.num_pipe_per_mec
49 		* adev->gfx.mec.num_queue_per_pipe;
50 	bit += pipe * adev->gfx.mec.num_queue_per_pipe;
51 	bit += queue;
52 
53 	return bit;
54 }
55 
56 void amdgpu_queue_mask_bit_to_mec_queue(struct amdgpu_device *adev, int bit,
57 				 int *mec, int *pipe, int *queue)
58 {
59 	*queue = bit % adev->gfx.mec.num_queue_per_pipe;
60 	*pipe = (bit / adev->gfx.mec.num_queue_per_pipe)
61 		% adev->gfx.mec.num_pipe_per_mec;
62 	*mec = (bit / adev->gfx.mec.num_queue_per_pipe)
63 	       / adev->gfx.mec.num_pipe_per_mec;
64 
65 }
66 
67 bool amdgpu_gfx_is_mec_queue_enabled(struct amdgpu_device *adev,
68 				     int xcc_id, int mec, int pipe, int queue)
69 {
70 	return test_bit(amdgpu_gfx_mec_queue_to_bit(adev, mec, pipe, queue),
71 			adev->gfx.mec_bitmap[xcc_id].queue_bitmap);
72 }
73 
74 int amdgpu_gfx_me_queue_to_bit(struct amdgpu_device *adev,
75 			       int me, int pipe, int queue)
76 {
77 	int bit = 0;
78 
79 	bit += me * adev->gfx.me.num_pipe_per_me
80 		* adev->gfx.me.num_queue_per_pipe;
81 	bit += pipe * adev->gfx.me.num_queue_per_pipe;
82 	bit += queue;
83 
84 	return bit;
85 }
86 
87 void amdgpu_gfx_bit_to_me_queue(struct amdgpu_device *adev, int bit,
88 				int *me, int *pipe, int *queue)
89 {
90 	*queue = bit % adev->gfx.me.num_queue_per_pipe;
91 	*pipe = (bit / adev->gfx.me.num_queue_per_pipe)
92 		% adev->gfx.me.num_pipe_per_me;
93 	*me = (bit / adev->gfx.me.num_queue_per_pipe)
94 		/ adev->gfx.me.num_pipe_per_me;
95 }
96 
97 bool amdgpu_gfx_is_me_queue_enabled(struct amdgpu_device *adev,
98 				    int me, int pipe, int queue)
99 {
100 	return test_bit(amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue),
101 			adev->gfx.me.queue_bitmap);
102 }
103 
104 /**
105  * amdgpu_gfx_parse_disable_cu - Parse the disable_cu module parameter
106  *
107  * @mask: array in which the per-shader array disable masks will be stored
108  * @max_se: number of SEs
109  * @max_sh: number of SHs
110  *
111  * The bitmask of CUs to be disabled in the shader array determined by se and
112  * sh is stored in mask[se * max_sh + sh].
113  */
114 void amdgpu_gfx_parse_disable_cu(unsigned int *mask, unsigned int max_se, unsigned int max_sh)
115 {
116 	unsigned int se, sh, cu;
117 	const char *p;
118 
119 	memset(mask, 0, sizeof(*mask) * max_se * max_sh);
120 
121 	if (!amdgpu_disable_cu || !*amdgpu_disable_cu)
122 		return;
123 
124 	p = amdgpu_disable_cu;
125 	for (;;) {
126 		char *next;
127 		int ret = sscanf(p, "%u.%u.%u", &se, &sh, &cu);
128 
129 		if (ret < 3) {
130 			DRM_ERROR("amdgpu: could not parse disable_cu\n");
131 			return;
132 		}
133 
134 		if (se < max_se && sh < max_sh && cu < 16) {
135 			DRM_INFO("amdgpu: disabling CU %u.%u.%u\n", se, sh, cu);
136 			mask[se * max_sh + sh] |= 1u << cu;
137 		} else {
138 			DRM_ERROR("amdgpu: disable_cu %u.%u.%u is out of range\n",
139 				  se, sh, cu);
140 		}
141 
142 		next = strchr(p, ',');
143 		if (!next)
144 			break;
145 		p = next + 1;
146 	}
147 }
148 
149 static bool amdgpu_gfx_is_graphics_multipipe_capable(struct amdgpu_device *adev)
150 {
151 	return amdgpu_async_gfx_ring && adev->gfx.me.num_pipe_per_me > 1;
152 }
153 
154 static bool amdgpu_gfx_is_compute_multipipe_capable(struct amdgpu_device *adev)
155 {
156 	if (amdgpu_compute_multipipe != -1) {
157 		DRM_INFO("amdgpu: forcing compute pipe policy %d\n",
158 			 amdgpu_compute_multipipe);
159 		return amdgpu_compute_multipipe == 1;
160 	}
161 
162 	if (amdgpu_ip_version(adev, GC_HWIP, 0) > IP_VERSION(9, 0, 0))
163 		return true;
164 
165 	/* FIXME: spreading the queues across pipes causes perf regressions
166 	 * on POLARIS11 compute workloads */
167 	if (adev->asic_type == CHIP_POLARIS11)
168 		return false;
169 
170 	return adev->gfx.mec.num_mec > 1;
171 }
172 
173 bool amdgpu_gfx_is_high_priority_graphics_queue(struct amdgpu_device *adev,
174 						struct amdgpu_ring *ring)
175 {
176 	int queue = ring->queue;
177 	int pipe = ring->pipe;
178 
179 	/* Policy: use pipe1 queue0 as high priority graphics queue if we
180 	 * have more than one gfx pipe.
181 	 */
182 	if (amdgpu_gfx_is_graphics_multipipe_capable(adev) &&
183 	    adev->gfx.num_gfx_rings > 1 && pipe == 1 && queue == 0) {
184 		int me = ring->me;
185 		int bit;
186 
187 		bit = amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue);
188 		if (ring == &adev->gfx.gfx_ring[bit])
189 			return true;
190 	}
191 
192 	return false;
193 }
194 
195 bool amdgpu_gfx_is_high_priority_compute_queue(struct amdgpu_device *adev,
196 					       struct amdgpu_ring *ring)
197 {
198 	/* Policy: use 1st queue as high priority compute queue if we
199 	 * have more than one compute queue.
200 	 */
201 	if (adev->gfx.num_compute_rings > 1 &&
202 	    ring == &adev->gfx.compute_ring[0])
203 		return true;
204 
205 	return false;
206 }
207 
208 void amdgpu_gfx_compute_queue_acquire(struct amdgpu_device *adev)
209 {
210 	int i, j, queue, pipe;
211 	bool multipipe_policy = amdgpu_gfx_is_compute_multipipe_capable(adev);
212 	int max_queues_per_mec = min(adev->gfx.mec.num_pipe_per_mec *
213 				     adev->gfx.mec.num_queue_per_pipe,
214 				     adev->gfx.num_compute_rings);
215 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
216 
217 	if (multipipe_policy) {
218 		/* policy: make queues evenly cross all pipes on MEC1 only
219 		 * for multiple xcc, just use the original policy for simplicity */
220 		for (j = 0; j < num_xcc; j++) {
221 			for (i = 0; i < max_queues_per_mec; i++) {
222 				pipe = i % adev->gfx.mec.num_pipe_per_mec;
223 				queue = (i / adev->gfx.mec.num_pipe_per_mec) %
224 					 adev->gfx.mec.num_queue_per_pipe;
225 
226 				set_bit(pipe * adev->gfx.mec.num_queue_per_pipe + queue,
227 					adev->gfx.mec_bitmap[j].queue_bitmap);
228 			}
229 		}
230 	} else {
231 		/* policy: amdgpu owns all queues in the given pipe */
232 		for (j = 0; j < num_xcc; j++) {
233 			for (i = 0; i < max_queues_per_mec; ++i)
234 				set_bit(i, adev->gfx.mec_bitmap[j].queue_bitmap);
235 		}
236 	}
237 
238 	for (j = 0; j < num_xcc; j++) {
239 		dev_dbg(adev->dev, "mec queue bitmap weight=%d\n",
240 			bitmap_weight(adev->gfx.mec_bitmap[j].queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES));
241 	}
242 }
243 
244 void amdgpu_gfx_graphics_queue_acquire(struct amdgpu_device *adev)
245 {
246 	int i, queue, pipe;
247 	bool multipipe_policy = amdgpu_gfx_is_graphics_multipipe_capable(adev);
248 	int max_queues_per_me = adev->gfx.me.num_pipe_per_me *
249 					adev->gfx.me.num_queue_per_pipe;
250 
251 	if (multipipe_policy) {
252 		/* policy: amdgpu owns the first queue per pipe at this stage
253 		 * will extend to mulitple queues per pipe later */
254 		for (i = 0; i < max_queues_per_me; i++) {
255 			pipe = i % adev->gfx.me.num_pipe_per_me;
256 			queue = (i / adev->gfx.me.num_pipe_per_me) %
257 				adev->gfx.me.num_queue_per_pipe;
258 
259 			set_bit(pipe * adev->gfx.me.num_queue_per_pipe + queue,
260 				adev->gfx.me.queue_bitmap);
261 		}
262 	} else {
263 		for (i = 0; i < max_queues_per_me; ++i)
264 			set_bit(i, adev->gfx.me.queue_bitmap);
265 	}
266 
267 	/* update the number of active graphics rings */
268 	adev->gfx.num_gfx_rings =
269 		bitmap_weight(adev->gfx.me.queue_bitmap, AMDGPU_MAX_GFX_QUEUES);
270 }
271 
272 static int amdgpu_gfx_kiq_acquire(struct amdgpu_device *adev,
273 				  struct amdgpu_ring *ring, int xcc_id)
274 {
275 	int queue_bit;
276 	int mec, pipe, queue;
277 
278 	queue_bit = adev->gfx.mec.num_mec
279 		    * adev->gfx.mec.num_pipe_per_mec
280 		    * adev->gfx.mec.num_queue_per_pipe;
281 
282 	while (--queue_bit >= 0) {
283 		if (test_bit(queue_bit, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
284 			continue;
285 
286 		amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
287 
288 		/*
289 		 * 1. Using pipes 2/3 from MEC 2 seems cause problems.
290 		 * 2. It must use queue id 0, because CGPG_IDLE/SAVE/LOAD/RUN
291 		 * only can be issued on queue 0.
292 		 */
293 		if ((mec == 1 && pipe > 1) || queue != 0)
294 			continue;
295 
296 		ring->me = mec + 1;
297 		ring->pipe = pipe;
298 		ring->queue = queue;
299 
300 		return 0;
301 	}
302 
303 	dev_err(adev->dev, "Failed to find a queue for KIQ\n");
304 	return -EINVAL;
305 }
306 
307 int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev, int xcc_id)
308 {
309 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
310 	struct amdgpu_irq_src *irq = &kiq->irq;
311 	struct amdgpu_ring *ring = &kiq->ring;
312 	int r = 0;
313 
314 	spin_lock_init(&kiq->ring_lock);
315 
316 	ring->adev = NULL;
317 	ring->ring_obj = NULL;
318 	ring->use_doorbell = true;
319 	ring->xcc_id = xcc_id;
320 	ring->vm_hub = AMDGPU_GFXHUB(xcc_id);
321 	ring->doorbell_index =
322 		(adev->doorbell_index.kiq +
323 		 xcc_id * adev->doorbell_index.xcc_doorbell_range)
324 		<< 1;
325 
326 	r = amdgpu_gfx_kiq_acquire(adev, ring, xcc_id);
327 	if (r)
328 		return r;
329 
330 	ring->eop_gpu_addr = kiq->eop_gpu_addr;
331 	ring->no_scheduler = true;
332 	snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
333 		 xcc_id, ring->me, ring->pipe, ring->queue);
334 	r = amdgpu_ring_init(adev, ring, 1024, irq, AMDGPU_CP_KIQ_IRQ_DRIVER0,
335 			     AMDGPU_RING_PRIO_DEFAULT, NULL);
336 	if (r)
337 		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
338 
339 	return r;
340 }
341 
342 void amdgpu_gfx_kiq_free_ring(struct amdgpu_ring *ring)
343 {
344 	amdgpu_ring_fini(ring);
345 }
346 
347 void amdgpu_gfx_kiq_fini(struct amdgpu_device *adev, int xcc_id)
348 {
349 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
350 
351 	amdgpu_bo_free_kernel(&kiq->eop_obj, &kiq->eop_gpu_addr, NULL);
352 }
353 
354 int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
355 			unsigned int hpd_size, int xcc_id)
356 {
357 	int r;
358 	u32 *hpd;
359 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
360 
361 	r = amdgpu_bo_create_kernel(adev, hpd_size, PAGE_SIZE,
362 				    AMDGPU_GEM_DOMAIN_GTT, &kiq->eop_obj,
363 				    &kiq->eop_gpu_addr, (void **)&hpd);
364 	if (r) {
365 		dev_warn(adev->dev, "failed to create KIQ bo (%d).\n", r);
366 		return r;
367 	}
368 
369 	memset(hpd, 0, hpd_size);
370 
371 	r = amdgpu_bo_reserve(kiq->eop_obj, true);
372 	if (unlikely(r != 0))
373 		dev_warn(adev->dev, "(%d) reserve kiq eop bo failed\n", r);
374 	amdgpu_bo_kunmap(kiq->eop_obj);
375 	amdgpu_bo_unreserve(kiq->eop_obj);
376 
377 	return 0;
378 }
379 
380 /* create MQD for each compute/gfx queue */
381 int amdgpu_gfx_mqd_sw_init(struct amdgpu_device *adev,
382 			   unsigned int mqd_size, int xcc_id)
383 {
384 	int r, i, j;
385 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
386 	struct amdgpu_ring *ring = &kiq->ring;
387 	u32 domain = AMDGPU_GEM_DOMAIN_GTT;
388 
389 #if !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
390 	/* Only enable on gfx10 and 11 for now to avoid changing behavior on older chips */
391 	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 0, 0))
392 		domain |= AMDGPU_GEM_DOMAIN_VRAM;
393 #endif
394 
395 	/* create MQD for KIQ */
396 	if (!adev->enable_mes_kiq && !ring->mqd_obj) {
397 		/* originaly the KIQ MQD is put in GTT domain, but for SRIOV VRAM domain is a must
398 		 * otherwise hypervisor trigger SAVE_VF fail after driver unloaded which mean MQD
399 		 * deallocated and gart_unbind, to strict diverage we decide to use VRAM domain for
400 		 * KIQ MQD no matter SRIOV or Bare-metal
401 		 */
402 		r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
403 					    AMDGPU_GEM_DOMAIN_VRAM |
404 					    AMDGPU_GEM_DOMAIN_GTT,
405 					    &ring->mqd_obj,
406 					    &ring->mqd_gpu_addr,
407 					    &ring->mqd_ptr);
408 		if (r) {
409 			dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
410 			return r;
411 		}
412 
413 		/* prepare MQD backup */
414 		kiq->mqd_backup = kmalloc(mqd_size, GFP_KERNEL);
415 		if (!kiq->mqd_backup) {
416 			dev_warn(adev->dev,
417 				 "no memory to create MQD backup for ring %s\n", ring->name);
418 			return -ENOMEM;
419 		}
420 	}
421 
422 	if (adev->asic_type >= CHIP_NAVI10 && amdgpu_async_gfx_ring) {
423 		/* create MQD for each KGQ */
424 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
425 			ring = &adev->gfx.gfx_ring[i];
426 			if (!ring->mqd_obj) {
427 				r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
428 							    domain, &ring->mqd_obj,
429 							    &ring->mqd_gpu_addr, &ring->mqd_ptr);
430 				if (r) {
431 					dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
432 					return r;
433 				}
434 
435 				ring->mqd_size = mqd_size;
436 				/* prepare MQD backup */
437 				adev->gfx.me.mqd_backup[i] = kmalloc(mqd_size, GFP_KERNEL);
438 				if (!adev->gfx.me.mqd_backup[i]) {
439 					dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
440 					return -ENOMEM;
441 				}
442 			}
443 		}
444 	}
445 
446 	/* create MQD for each KCQ */
447 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
448 		j = i + xcc_id * adev->gfx.num_compute_rings;
449 		ring = &adev->gfx.compute_ring[j];
450 		if (!ring->mqd_obj) {
451 			r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
452 						    domain, &ring->mqd_obj,
453 						    &ring->mqd_gpu_addr, &ring->mqd_ptr);
454 			if (r) {
455 				dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
456 				return r;
457 			}
458 
459 			ring->mqd_size = mqd_size;
460 			/* prepare MQD backup */
461 			adev->gfx.mec.mqd_backup[j] = kmalloc(mqd_size, GFP_KERNEL);
462 			if (!adev->gfx.mec.mqd_backup[j]) {
463 				dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
464 				return -ENOMEM;
465 			}
466 		}
467 	}
468 
469 	return 0;
470 }
471 
472 void amdgpu_gfx_mqd_sw_fini(struct amdgpu_device *adev, int xcc_id)
473 {
474 	struct amdgpu_ring *ring = NULL;
475 	int i, j;
476 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
477 
478 	if (adev->asic_type >= CHIP_NAVI10 && amdgpu_async_gfx_ring) {
479 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
480 			ring = &adev->gfx.gfx_ring[i];
481 			kfree(adev->gfx.me.mqd_backup[i]);
482 			amdgpu_bo_free_kernel(&ring->mqd_obj,
483 					      &ring->mqd_gpu_addr,
484 					      &ring->mqd_ptr);
485 		}
486 	}
487 
488 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
489 		j = i + xcc_id * adev->gfx.num_compute_rings;
490 		ring = &adev->gfx.compute_ring[j];
491 		kfree(adev->gfx.mec.mqd_backup[j]);
492 		amdgpu_bo_free_kernel(&ring->mqd_obj,
493 				      &ring->mqd_gpu_addr,
494 				      &ring->mqd_ptr);
495 	}
496 
497 	ring = &kiq->ring;
498 	kfree(kiq->mqd_backup);
499 	amdgpu_bo_free_kernel(&ring->mqd_obj,
500 			      &ring->mqd_gpu_addr,
501 			      &ring->mqd_ptr);
502 }
503 
504 int amdgpu_gfx_disable_kcq(struct amdgpu_device *adev, int xcc_id)
505 {
506 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
507 	struct amdgpu_ring *kiq_ring = &kiq->ring;
508 	struct amdgpu_hive_info *hive;
509 	struct amdgpu_ras *ras;
510 	int hive_ras_recovery = 0;
511 	int i, r = 0;
512 	int j;
513 
514 	if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
515 		return -EINVAL;
516 
517 	spin_lock(&kiq->ring_lock);
518 	if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
519 					adev->gfx.num_compute_rings)) {
520 		spin_unlock(&kiq->ring_lock);
521 		return -ENOMEM;
522 	}
523 
524 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
525 		j = i + xcc_id * adev->gfx.num_compute_rings;
526 		kiq->pmf->kiq_unmap_queues(kiq_ring,
527 					   &adev->gfx.compute_ring[j],
528 					   RESET_QUEUES, 0, 0);
529 	}
530 
531 	/**
532 	 * This is workaround: only skip kiq_ring test
533 	 * during ras recovery in suspend stage for gfx9.4.3
534 	 */
535 	hive = amdgpu_get_xgmi_hive(adev);
536 	if (hive) {
537 		hive_ras_recovery = atomic_read(&hive->ras_recovery);
538 		amdgpu_put_xgmi_hive(hive);
539 	}
540 
541 	ras = amdgpu_ras_get_context(adev);
542 	if ((amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) ||
543 	     amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4)) &&
544 		ras && (atomic_read(&ras->in_recovery) || hive_ras_recovery)) {
545 		spin_unlock(&kiq->ring_lock);
546 		return 0;
547 	}
548 
549 	if (kiq_ring->sched.ready && !adev->job_hang)
550 		r = amdgpu_ring_test_helper(kiq_ring);
551 	spin_unlock(&kiq->ring_lock);
552 
553 	return r;
554 }
555 
556 int amdgpu_gfx_disable_kgq(struct amdgpu_device *adev, int xcc_id)
557 {
558 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
559 	struct amdgpu_ring *kiq_ring = &kiq->ring;
560 	int i, r = 0;
561 	int j;
562 
563 	if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
564 		return -EINVAL;
565 
566 	spin_lock(&kiq->ring_lock);
567 	if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
568 		if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
569 						adev->gfx.num_gfx_rings)) {
570 			spin_unlock(&kiq->ring_lock);
571 			return -ENOMEM;
572 		}
573 
574 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
575 			j = i + xcc_id * adev->gfx.num_gfx_rings;
576 			kiq->pmf->kiq_unmap_queues(kiq_ring,
577 						   &adev->gfx.gfx_ring[j],
578 						   PREEMPT_QUEUES, 0, 0);
579 		}
580 	}
581 
582 	if (adev->gfx.kiq[0].ring.sched.ready && !adev->job_hang)
583 		r = amdgpu_ring_test_helper(kiq_ring);
584 	spin_unlock(&kiq->ring_lock);
585 
586 	return r;
587 }
588 
589 int amdgpu_queue_mask_bit_to_set_resource_bit(struct amdgpu_device *adev,
590 					int queue_bit)
591 {
592 	int mec, pipe, queue;
593 	int set_resource_bit = 0;
594 
595 	amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
596 
597 	set_resource_bit = mec * 4 * 8 + pipe * 8 + queue;
598 
599 	return set_resource_bit;
600 }
601 
602 int amdgpu_gfx_enable_kcq(struct amdgpu_device *adev, int xcc_id)
603 {
604 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
605 	struct amdgpu_ring *kiq_ring = &kiq->ring;
606 	uint64_t queue_mask = 0;
607 	int r, i, j;
608 
609 	if (!kiq->pmf || !kiq->pmf->kiq_map_queues || !kiq->pmf->kiq_set_resources)
610 		return -EINVAL;
611 
612 	for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
613 		if (!test_bit(i, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
614 			continue;
615 
616 		/* This situation may be hit in the future if a new HW
617 		 * generation exposes more than 64 queues. If so, the
618 		 * definition of queue_mask needs updating */
619 		if (WARN_ON(i > (sizeof(queue_mask)*8))) {
620 			DRM_ERROR("Invalid KCQ enabled: %d\n", i);
621 			break;
622 		}
623 
624 		queue_mask |= (1ull << amdgpu_queue_mask_bit_to_set_resource_bit(adev, i));
625 	}
626 
627 	amdgpu_device_flush_hdp(adev, NULL);
628 
629 	if (adev->enable_mes)
630 		queue_mask = ~0ULL;
631 
632 	DRM_INFO("kiq ring mec %d pipe %d q %d\n", kiq_ring->me, kiq_ring->pipe,
633 		 kiq_ring->queue);
634 
635 	spin_lock(&kiq->ring_lock);
636 	r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
637 					adev->gfx.num_compute_rings +
638 					kiq->pmf->set_resources_size);
639 	if (r) {
640 		DRM_ERROR("Failed to lock KIQ (%d).\n", r);
641 		spin_unlock(&kiq->ring_lock);
642 		return r;
643 	}
644 
645 	kiq->pmf->kiq_set_resources(kiq_ring, queue_mask);
646 
647 	if (!adev->enable_mes) {
648 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
649 			j = i + xcc_id * adev->gfx.num_compute_rings;
650 			kiq->pmf->kiq_map_queues(kiq_ring,
651 						 &adev->gfx.compute_ring[j]);
652 		}
653 	}
654 
655 	r = amdgpu_ring_test_helper(kiq_ring);
656 	spin_unlock(&kiq->ring_lock);
657 	if (r)
658 		DRM_ERROR("KCQ enable failed\n");
659 
660 	if (adev->enable_mes || adev->enable_uni_mes) {
661 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
662 			j = i + xcc_id * adev->gfx.num_compute_rings;
663 			r = amdgpu_mes_map_legacy_queue(adev,
664 					       &adev->gfx.compute_ring[j]);
665 			if (r) {
666 				DRM_ERROR("failed to map compute queue\n");
667 				return r;
668 			}
669 		}
670 
671 		return 0;
672 	}
673 
674 	return r;
675 }
676 
677 int amdgpu_gfx_enable_kgq(struct amdgpu_device *adev, int xcc_id)
678 {
679 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
680 	struct amdgpu_ring *kiq_ring = &kiq->ring;
681 	int r, i, j;
682 
683 	if (!kiq->pmf || !kiq->pmf->kiq_map_queues)
684 		return -EINVAL;
685 
686 	amdgpu_device_flush_hdp(adev, NULL);
687 
688 	if (adev->enable_mes || adev->enable_uni_mes) {
689 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
690 			j = i + xcc_id * adev->gfx.num_gfx_rings;
691 			r = amdgpu_mes_map_legacy_queue(adev,
692 							&adev->gfx.gfx_ring[j]);
693 			if (r) {
694 				DRM_ERROR("failed to map gfx queue\n");
695 				return r;
696 			}
697 		}
698 
699 		return 0;
700 	}
701 
702 	spin_lock(&kiq->ring_lock);
703 	/* No need to map kcq on the slave */
704 	if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
705 		r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
706 						adev->gfx.num_gfx_rings);
707 		if (r) {
708 			DRM_ERROR("Failed to lock KIQ (%d).\n", r);
709 			spin_unlock(&kiq->ring_lock);
710 			return r;
711 		}
712 
713 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
714 			j = i + xcc_id * adev->gfx.num_gfx_rings;
715 			kiq->pmf->kiq_map_queues(kiq_ring,
716 						 &adev->gfx.gfx_ring[j]);
717 		}
718 	}
719 
720 	r = amdgpu_ring_test_helper(kiq_ring);
721 	spin_unlock(&kiq->ring_lock);
722 	if (r)
723 		DRM_ERROR("KGQ enable failed\n");
724 
725 	return r;
726 }
727 
728 /* amdgpu_gfx_off_ctrl - Handle gfx off feature enable/disable
729  *
730  * @adev: amdgpu_device pointer
731  * @bool enable true: enable gfx off feature, false: disable gfx off feature
732  *
733  * 1. gfx off feature will be enabled by gfx ip after gfx cg gp enabled.
734  * 2. other client can send request to disable gfx off feature, the request should be honored.
735  * 3. other client can cancel their request of disable gfx off feature
736  * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
737  */
738 
739 void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
740 {
741 	unsigned long delay = GFX_OFF_DELAY_ENABLE;
742 
743 	if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
744 		return;
745 
746 	mutex_lock(&adev->gfx.gfx_off_mutex);
747 
748 	if (enable) {
749 		/* If the count is already 0, it means there's an imbalance bug somewhere.
750 		 * Note that the bug may be in a different caller than the one which triggers the
751 		 * WARN_ON_ONCE.
752 		 */
753 		if (WARN_ON_ONCE(adev->gfx.gfx_off_req_count == 0))
754 			goto unlock;
755 
756 		adev->gfx.gfx_off_req_count--;
757 
758 		if (adev->gfx.gfx_off_req_count == 0 &&
759 		    !adev->gfx.gfx_off_state) {
760 			/* If going to s2idle, no need to wait */
761 			if (adev->in_s0ix) {
762 				if (!amdgpu_dpm_set_powergating_by_smu(adev,
763 						AMD_IP_BLOCK_TYPE_GFX, true))
764 					adev->gfx.gfx_off_state = true;
765 			} else {
766 				schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
767 					      delay);
768 			}
769 		}
770 	} else {
771 		if (adev->gfx.gfx_off_req_count == 0) {
772 			cancel_delayed_work_sync(&adev->gfx.gfx_off_delay_work);
773 
774 			if (adev->gfx.gfx_off_state &&
775 			    !amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false)) {
776 				adev->gfx.gfx_off_state = false;
777 
778 				if (adev->gfx.funcs->init_spm_golden) {
779 					dev_dbg(adev->dev,
780 						"GFXOFF is disabled, re-init SPM golden settings\n");
781 					amdgpu_gfx_init_spm_golden(adev);
782 				}
783 			}
784 		}
785 
786 		adev->gfx.gfx_off_req_count++;
787 	}
788 
789 unlock:
790 	mutex_unlock(&adev->gfx.gfx_off_mutex);
791 }
792 
793 int amdgpu_set_gfx_off_residency(struct amdgpu_device *adev, bool value)
794 {
795 	int r = 0;
796 
797 	mutex_lock(&adev->gfx.gfx_off_mutex);
798 
799 	r = amdgpu_dpm_set_residency_gfxoff(adev, value);
800 
801 	mutex_unlock(&adev->gfx.gfx_off_mutex);
802 
803 	return r;
804 }
805 
806 int amdgpu_get_gfx_off_residency(struct amdgpu_device *adev, u32 *value)
807 {
808 	int r = 0;
809 
810 	mutex_lock(&adev->gfx.gfx_off_mutex);
811 
812 	r = amdgpu_dpm_get_residency_gfxoff(adev, value);
813 
814 	mutex_unlock(&adev->gfx.gfx_off_mutex);
815 
816 	return r;
817 }
818 
819 int amdgpu_get_gfx_off_entrycount(struct amdgpu_device *adev, u64 *value)
820 {
821 	int r = 0;
822 
823 	mutex_lock(&adev->gfx.gfx_off_mutex);
824 
825 	r = amdgpu_dpm_get_entrycount_gfxoff(adev, value);
826 
827 	mutex_unlock(&adev->gfx.gfx_off_mutex);
828 
829 	return r;
830 }
831 
832 int amdgpu_get_gfx_off_status(struct amdgpu_device *adev, uint32_t *value)
833 {
834 
835 	int r = 0;
836 
837 	mutex_lock(&adev->gfx.gfx_off_mutex);
838 
839 	r = amdgpu_dpm_get_status_gfxoff(adev, value);
840 
841 	mutex_unlock(&adev->gfx.gfx_off_mutex);
842 
843 	return r;
844 }
845 
846 int amdgpu_gfx_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
847 {
848 	int r;
849 
850 	if (amdgpu_ras_is_supported(adev, ras_block->block)) {
851 		if (!amdgpu_persistent_edc_harvesting_supported(adev))
852 			amdgpu_ras_reset_error_status(adev, AMDGPU_RAS_BLOCK__GFX);
853 
854 		r = amdgpu_ras_block_late_init(adev, ras_block);
855 		if (r)
856 			return r;
857 
858 		if (adev->gfx.cp_ecc_error_irq.funcs) {
859 			r = amdgpu_irq_get(adev, &adev->gfx.cp_ecc_error_irq, 0);
860 			if (r)
861 				goto late_fini;
862 		}
863 	} else {
864 		amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
865 	}
866 
867 	return 0;
868 late_fini:
869 	amdgpu_ras_block_late_fini(adev, ras_block);
870 	return r;
871 }
872 
873 int amdgpu_gfx_ras_sw_init(struct amdgpu_device *adev)
874 {
875 	int err = 0;
876 	struct amdgpu_gfx_ras *ras = NULL;
877 
878 	/* adev->gfx.ras is NULL, which means gfx does not
879 	 * support ras function, then do nothing here.
880 	 */
881 	if (!adev->gfx.ras)
882 		return 0;
883 
884 	ras = adev->gfx.ras;
885 
886 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
887 	if (err) {
888 		dev_err(adev->dev, "Failed to register gfx ras block!\n");
889 		return err;
890 	}
891 
892 	strcpy(ras->ras_block.ras_comm.name, "gfx");
893 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__GFX;
894 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
895 	adev->gfx.ras_if = &ras->ras_block.ras_comm;
896 
897 	/* If not define special ras_late_init function, use gfx default ras_late_init */
898 	if (!ras->ras_block.ras_late_init)
899 		ras->ras_block.ras_late_init = amdgpu_gfx_ras_late_init;
900 
901 	/* If not defined special ras_cb function, use default ras_cb */
902 	if (!ras->ras_block.ras_cb)
903 		ras->ras_block.ras_cb = amdgpu_gfx_process_ras_data_cb;
904 
905 	return 0;
906 }
907 
908 int amdgpu_gfx_poison_consumption_handler(struct amdgpu_device *adev,
909 						struct amdgpu_iv_entry *entry)
910 {
911 	if (adev->gfx.ras && adev->gfx.ras->poison_consumption_handler)
912 		return adev->gfx.ras->poison_consumption_handler(adev, entry);
913 
914 	return 0;
915 }
916 
917 int amdgpu_gfx_process_ras_data_cb(struct amdgpu_device *adev,
918 		void *err_data,
919 		struct amdgpu_iv_entry *entry)
920 {
921 	/* TODO ue will trigger an interrupt.
922 	 *
923 	 * When “Full RAS” is enabled, the per-IP interrupt sources should
924 	 * be disabled and the driver should only look for the aggregated
925 	 * interrupt via sync flood
926 	 */
927 	if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__GFX)) {
928 		kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
929 		if (adev->gfx.ras && adev->gfx.ras->ras_block.hw_ops &&
930 		    adev->gfx.ras->ras_block.hw_ops->query_ras_error_count)
931 			adev->gfx.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data);
932 		amdgpu_ras_reset_gpu(adev);
933 	}
934 	return AMDGPU_RAS_SUCCESS;
935 }
936 
937 int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
938 				  struct amdgpu_irq_src *source,
939 				  struct amdgpu_iv_entry *entry)
940 {
941 	struct ras_common_if *ras_if = adev->gfx.ras_if;
942 	struct ras_dispatch_if ih_data = {
943 		.entry = entry,
944 	};
945 
946 	if (!ras_if)
947 		return 0;
948 
949 	ih_data.head = *ras_if;
950 
951 	DRM_ERROR("CP ECC ERROR IRQ\n");
952 	amdgpu_ras_interrupt_dispatch(adev, &ih_data);
953 	return 0;
954 }
955 
956 void amdgpu_gfx_ras_error_func(struct amdgpu_device *adev,
957 		void *ras_error_status,
958 		void (*func)(struct amdgpu_device *adev, void *ras_error_status,
959 				int xcc_id))
960 {
961 	int i;
962 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
963 	uint32_t xcc_mask = GENMASK(num_xcc - 1, 0);
964 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
965 
966 	if (err_data) {
967 		err_data->ue_count = 0;
968 		err_data->ce_count = 0;
969 	}
970 
971 	for_each_inst(i, xcc_mask)
972 		func(adev, ras_error_status, i);
973 }
974 
975 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg, uint32_t xcc_id)
976 {
977 	signed long r, cnt = 0;
978 	unsigned long flags;
979 	uint32_t seq, reg_val_offs = 0, value = 0;
980 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
981 	struct amdgpu_ring *ring = &kiq->ring;
982 
983 	if (amdgpu_device_skip_hw_access(adev))
984 		return 0;
985 
986 	if (adev->mes.ring.sched.ready)
987 		return amdgpu_mes_rreg(adev, reg);
988 
989 	BUG_ON(!ring->funcs->emit_rreg);
990 
991 	spin_lock_irqsave(&kiq->ring_lock, flags);
992 	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
993 		pr_err("critical bug! too many kiq readers\n");
994 		goto failed_unlock;
995 	}
996 	amdgpu_ring_alloc(ring, 32);
997 	amdgpu_ring_emit_rreg(ring, reg, reg_val_offs);
998 	r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
999 	if (r)
1000 		goto failed_undo;
1001 
1002 	amdgpu_ring_commit(ring);
1003 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1004 
1005 	r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1006 
1007 	/* don't wait anymore for gpu reset case because this way may
1008 	 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1009 	 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1010 	 * never return if we keep waiting in virt_kiq_rreg, which cause
1011 	 * gpu_recover() hang there.
1012 	 *
1013 	 * also don't wait anymore for IRQ context
1014 	 * */
1015 	if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1016 		goto failed_kiq_read;
1017 
1018 	might_sleep();
1019 	while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1020 		msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1021 		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1022 	}
1023 
1024 	if (cnt > MAX_KIQ_REG_TRY)
1025 		goto failed_kiq_read;
1026 
1027 	mb();
1028 	value = adev->wb.wb[reg_val_offs];
1029 	amdgpu_device_wb_free(adev, reg_val_offs);
1030 	return value;
1031 
1032 failed_undo:
1033 	amdgpu_ring_undo(ring);
1034 failed_unlock:
1035 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1036 failed_kiq_read:
1037 	if (reg_val_offs)
1038 		amdgpu_device_wb_free(adev, reg_val_offs);
1039 	dev_err(adev->dev, "failed to read reg:%x\n", reg);
1040 	return ~0;
1041 }
1042 
1043 void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v, uint32_t xcc_id)
1044 {
1045 	signed long r, cnt = 0;
1046 	unsigned long flags;
1047 	uint32_t seq;
1048 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
1049 	struct amdgpu_ring *ring = &kiq->ring;
1050 
1051 	BUG_ON(!ring->funcs->emit_wreg);
1052 
1053 	if (amdgpu_device_skip_hw_access(adev))
1054 		return;
1055 
1056 	if (adev->mes.ring.sched.ready) {
1057 		amdgpu_mes_wreg(adev, reg, v);
1058 		return;
1059 	}
1060 
1061 	spin_lock_irqsave(&kiq->ring_lock, flags);
1062 	amdgpu_ring_alloc(ring, 32);
1063 	amdgpu_ring_emit_wreg(ring, reg, v);
1064 	r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
1065 	if (r)
1066 		goto failed_undo;
1067 
1068 	amdgpu_ring_commit(ring);
1069 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1070 
1071 	r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1072 
1073 	/* don't wait anymore for gpu reset case because this way may
1074 	 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1075 	 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1076 	 * never return if we keep waiting in virt_kiq_rreg, which cause
1077 	 * gpu_recover() hang there.
1078 	 *
1079 	 * also don't wait anymore for IRQ context
1080 	 * */
1081 	if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1082 		goto failed_kiq_write;
1083 
1084 	might_sleep();
1085 	while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1086 
1087 		msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1088 		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1089 	}
1090 
1091 	if (cnt > MAX_KIQ_REG_TRY)
1092 		goto failed_kiq_write;
1093 
1094 	return;
1095 
1096 failed_undo:
1097 	amdgpu_ring_undo(ring);
1098 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
1099 failed_kiq_write:
1100 	dev_err(adev->dev, "failed to write reg:%x\n", reg);
1101 }
1102 
1103 int amdgpu_gfx_get_num_kcq(struct amdgpu_device *adev)
1104 {
1105 	if (amdgpu_num_kcq == -1) {
1106 		return 8;
1107 	} else if (amdgpu_num_kcq > 8 || amdgpu_num_kcq < 0) {
1108 		dev_warn(adev->dev, "set kernel compute queue number to 8 due to invalid parameter provided by user\n");
1109 		return 8;
1110 	}
1111 	return amdgpu_num_kcq;
1112 }
1113 
1114 void amdgpu_gfx_cp_init_microcode(struct amdgpu_device *adev,
1115 				  uint32_t ucode_id)
1116 {
1117 	const struct gfx_firmware_header_v1_0 *cp_hdr;
1118 	const struct gfx_firmware_header_v2_0 *cp_hdr_v2_0;
1119 	struct amdgpu_firmware_info *info = NULL;
1120 	const struct firmware *ucode_fw;
1121 	unsigned int fw_size;
1122 
1123 	switch (ucode_id) {
1124 	case AMDGPU_UCODE_ID_CP_PFP:
1125 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1126 			adev->gfx.pfp_fw->data;
1127 		adev->gfx.pfp_fw_version =
1128 			le32_to_cpu(cp_hdr->header.ucode_version);
1129 		adev->gfx.pfp_feature_version =
1130 			le32_to_cpu(cp_hdr->ucode_feature_version);
1131 		ucode_fw = adev->gfx.pfp_fw;
1132 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1133 		break;
1134 	case AMDGPU_UCODE_ID_CP_RS64_PFP:
1135 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1136 			adev->gfx.pfp_fw->data;
1137 		adev->gfx.pfp_fw_version =
1138 			le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1139 		adev->gfx.pfp_feature_version =
1140 			le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1141 		ucode_fw = adev->gfx.pfp_fw;
1142 		fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1143 		break;
1144 	case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
1145 	case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
1146 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1147 			adev->gfx.pfp_fw->data;
1148 		ucode_fw = adev->gfx.pfp_fw;
1149 		fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1150 		break;
1151 	case AMDGPU_UCODE_ID_CP_ME:
1152 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1153 			adev->gfx.me_fw->data;
1154 		adev->gfx.me_fw_version =
1155 			le32_to_cpu(cp_hdr->header.ucode_version);
1156 		adev->gfx.me_feature_version =
1157 			le32_to_cpu(cp_hdr->ucode_feature_version);
1158 		ucode_fw = adev->gfx.me_fw;
1159 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1160 		break;
1161 	case AMDGPU_UCODE_ID_CP_RS64_ME:
1162 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1163 			adev->gfx.me_fw->data;
1164 		adev->gfx.me_fw_version =
1165 			le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1166 		adev->gfx.me_feature_version =
1167 			le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1168 		ucode_fw = adev->gfx.me_fw;
1169 		fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1170 		break;
1171 	case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
1172 	case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
1173 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1174 			adev->gfx.me_fw->data;
1175 		ucode_fw = adev->gfx.me_fw;
1176 		fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1177 		break;
1178 	case AMDGPU_UCODE_ID_CP_CE:
1179 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1180 			adev->gfx.ce_fw->data;
1181 		adev->gfx.ce_fw_version =
1182 			le32_to_cpu(cp_hdr->header.ucode_version);
1183 		adev->gfx.ce_feature_version =
1184 			le32_to_cpu(cp_hdr->ucode_feature_version);
1185 		ucode_fw = adev->gfx.ce_fw;
1186 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1187 		break;
1188 	case AMDGPU_UCODE_ID_CP_MEC1:
1189 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1190 			adev->gfx.mec_fw->data;
1191 		adev->gfx.mec_fw_version =
1192 			le32_to_cpu(cp_hdr->header.ucode_version);
1193 		adev->gfx.mec_feature_version =
1194 			le32_to_cpu(cp_hdr->ucode_feature_version);
1195 		ucode_fw = adev->gfx.mec_fw;
1196 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1197 			  le32_to_cpu(cp_hdr->jt_size) * 4;
1198 		break;
1199 	case AMDGPU_UCODE_ID_CP_MEC1_JT:
1200 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1201 			adev->gfx.mec_fw->data;
1202 		ucode_fw = adev->gfx.mec_fw;
1203 		fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1204 		break;
1205 	case AMDGPU_UCODE_ID_CP_MEC2:
1206 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1207 			adev->gfx.mec2_fw->data;
1208 		adev->gfx.mec2_fw_version =
1209 			le32_to_cpu(cp_hdr->header.ucode_version);
1210 		adev->gfx.mec2_feature_version =
1211 			le32_to_cpu(cp_hdr->ucode_feature_version);
1212 		ucode_fw = adev->gfx.mec2_fw;
1213 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1214 			  le32_to_cpu(cp_hdr->jt_size) * 4;
1215 		break;
1216 	case AMDGPU_UCODE_ID_CP_MEC2_JT:
1217 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1218 			adev->gfx.mec2_fw->data;
1219 		ucode_fw = adev->gfx.mec2_fw;
1220 		fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1221 		break;
1222 	case AMDGPU_UCODE_ID_CP_RS64_MEC:
1223 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1224 			adev->gfx.mec_fw->data;
1225 		adev->gfx.mec_fw_version =
1226 			le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1227 		adev->gfx.mec_feature_version =
1228 			le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1229 		ucode_fw = adev->gfx.mec_fw;
1230 		fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1231 		break;
1232 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
1233 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
1234 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
1235 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
1236 		cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1237 			adev->gfx.mec_fw->data;
1238 		ucode_fw = adev->gfx.mec_fw;
1239 		fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1240 		break;
1241 	default:
1242 		dev_err(adev->dev, "Invalid ucode id %u\n", ucode_id);
1243 		return;
1244 	}
1245 
1246 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1247 		info = &adev->firmware.ucode[ucode_id];
1248 		info->ucode_id = ucode_id;
1249 		info->fw = ucode_fw;
1250 		adev->firmware.fw_size += ALIGN(fw_size, PAGE_SIZE);
1251 	}
1252 }
1253 
1254 bool amdgpu_gfx_is_master_xcc(struct amdgpu_device *adev, int xcc_id)
1255 {
1256 	return !(xcc_id % (adev->gfx.num_xcc_per_xcp ?
1257 			adev->gfx.num_xcc_per_xcp : 1));
1258 }
1259 
1260 static ssize_t amdgpu_gfx_get_current_compute_partition(struct device *dev,
1261 						struct device_attribute *addr,
1262 						char *buf)
1263 {
1264 	struct drm_device *ddev = dev_get_drvdata(dev);
1265 	struct amdgpu_device *adev = drm_to_adev(ddev);
1266 	int mode;
1267 
1268 	mode = amdgpu_xcp_query_partition_mode(adev->xcp_mgr,
1269 					       AMDGPU_XCP_FL_NONE);
1270 
1271 	return sysfs_emit(buf, "%s\n", amdgpu_gfx_compute_mode_desc(mode));
1272 }
1273 
1274 static ssize_t amdgpu_gfx_set_compute_partition(struct device *dev,
1275 						struct device_attribute *addr,
1276 						const char *buf, size_t count)
1277 {
1278 	struct drm_device *ddev = dev_get_drvdata(dev);
1279 	struct amdgpu_device *adev = drm_to_adev(ddev);
1280 	enum amdgpu_gfx_partition mode;
1281 	int ret = 0, num_xcc;
1282 
1283 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1284 	if (num_xcc % 2 != 0)
1285 		return -EINVAL;
1286 
1287 	if (!strncasecmp("SPX", buf, strlen("SPX"))) {
1288 		mode = AMDGPU_SPX_PARTITION_MODE;
1289 	} else if (!strncasecmp("DPX", buf, strlen("DPX"))) {
1290 		/*
1291 		 * DPX mode needs AIDs to be in multiple of 2.
1292 		 * Each AID connects 2 XCCs.
1293 		 */
1294 		if (num_xcc%4)
1295 			return -EINVAL;
1296 		mode = AMDGPU_DPX_PARTITION_MODE;
1297 	} else if (!strncasecmp("TPX", buf, strlen("TPX"))) {
1298 		if (num_xcc != 6)
1299 			return -EINVAL;
1300 		mode = AMDGPU_TPX_PARTITION_MODE;
1301 	} else if (!strncasecmp("QPX", buf, strlen("QPX"))) {
1302 		if (num_xcc != 8)
1303 			return -EINVAL;
1304 		mode = AMDGPU_QPX_PARTITION_MODE;
1305 	} else if (!strncasecmp("CPX", buf, strlen("CPX"))) {
1306 		mode = AMDGPU_CPX_PARTITION_MODE;
1307 	} else {
1308 		return -EINVAL;
1309 	}
1310 
1311 	ret = amdgpu_xcp_switch_partition_mode(adev->xcp_mgr, mode);
1312 
1313 	if (ret)
1314 		return ret;
1315 
1316 	return count;
1317 }
1318 
1319 static ssize_t amdgpu_gfx_get_available_compute_partition(struct device *dev,
1320 						struct device_attribute *addr,
1321 						char *buf)
1322 {
1323 	struct drm_device *ddev = dev_get_drvdata(dev);
1324 	struct amdgpu_device *adev = drm_to_adev(ddev);
1325 	char *supported_partition;
1326 
1327 	/* TBD */
1328 	switch (NUM_XCC(adev->gfx.xcc_mask)) {
1329 	case 8:
1330 		supported_partition = "SPX, DPX, QPX, CPX";
1331 		break;
1332 	case 6:
1333 		supported_partition = "SPX, TPX, CPX";
1334 		break;
1335 	case 4:
1336 		supported_partition = "SPX, DPX, CPX";
1337 		break;
1338 	/* this seems only existing in emulation phase */
1339 	case 2:
1340 		supported_partition = "SPX, CPX";
1341 		break;
1342 	default:
1343 		supported_partition = "Not supported";
1344 		break;
1345 	}
1346 
1347 	return sysfs_emit(buf, "%s\n", supported_partition);
1348 }
1349 
1350 static DEVICE_ATTR(current_compute_partition, 0644,
1351 		   amdgpu_gfx_get_current_compute_partition,
1352 		   amdgpu_gfx_set_compute_partition);
1353 
1354 static DEVICE_ATTR(available_compute_partition, 0444,
1355 		   amdgpu_gfx_get_available_compute_partition, NULL);
1356 
1357 int amdgpu_gfx_sysfs_init(struct amdgpu_device *adev)
1358 {
1359 	int r;
1360 
1361 	r = device_create_file(adev->dev, &dev_attr_current_compute_partition);
1362 	if (r)
1363 		return r;
1364 
1365 	r = device_create_file(adev->dev, &dev_attr_available_compute_partition);
1366 
1367 	return r;
1368 }
1369 
1370 void amdgpu_gfx_sysfs_fini(struct amdgpu_device *adev)
1371 {
1372 	device_remove_file(adev->dev, &dev_attr_current_compute_partition);
1373 	device_remove_file(adev->dev, &dev_attr_available_compute_partition);
1374 }
1375