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