Linux 4.19.133
[linux/fpc-iii.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_gfx.c
blob239bf2a4b3c68be7bfd09510a7196e1967cd8326
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include <drm/drmP.h>
26 #include "amdgpu.h"
27 #include "amdgpu_gfx.h"
30 * GPU scratch registers helpers function.
32 /**
33 * amdgpu_gfx_scratch_get - Allocate a scratch register
35 * @adev: amdgpu_device pointer
36 * @reg: scratch register mmio offset
38 * Allocate a CP scratch register for use by the driver (all asics).
39 * Returns 0 on success or -EINVAL on failure.
41 int amdgpu_gfx_scratch_get(struct amdgpu_device *adev, uint32_t *reg)
43 int i;
45 i = ffs(adev->gfx.scratch.free_mask);
46 if (i != 0 && i <= adev->gfx.scratch.num_reg) {
47 i--;
48 adev->gfx.scratch.free_mask &= ~(1u << i);
49 *reg = adev->gfx.scratch.reg_base + i;
50 return 0;
52 return -EINVAL;
55 /**
56 * amdgpu_gfx_scratch_free - Free a scratch register
58 * @adev: amdgpu_device pointer
59 * @reg: scratch register mmio offset
61 * Free a CP scratch register allocated for use by the driver (all asics)
63 void amdgpu_gfx_scratch_free(struct amdgpu_device *adev, uint32_t reg)
65 adev->gfx.scratch.free_mask |= 1u << (reg - adev->gfx.scratch.reg_base);
68 /**
69 * amdgpu_gfx_parse_disable_cu - Parse the disable_cu module parameter
71 * @mask: array in which the per-shader array disable masks will be stored
72 * @max_se: number of SEs
73 * @max_sh: number of SHs
75 * The bitmask of CUs to be disabled in the shader array determined by se and
76 * sh is stored in mask[se * max_sh + sh].
78 void amdgpu_gfx_parse_disable_cu(unsigned *mask, unsigned max_se, unsigned max_sh)
80 unsigned se, sh, cu;
81 const char *p;
83 memset(mask, 0, sizeof(*mask) * max_se * max_sh);
85 if (!amdgpu_disable_cu || !*amdgpu_disable_cu)
86 return;
88 p = amdgpu_disable_cu;
89 for (;;) {
90 char *next;
91 int ret = sscanf(p, "%u.%u.%u", &se, &sh, &cu);
92 if (ret < 3) {
93 DRM_ERROR("amdgpu: could not parse disable_cu\n");
94 return;
97 if (se < max_se && sh < max_sh && cu < 16) {
98 DRM_INFO("amdgpu: disabling CU %u.%u.%u\n", se, sh, cu);
99 mask[se * max_sh + sh] |= 1u << cu;
100 } else {
101 DRM_ERROR("amdgpu: disable_cu %u.%u.%u is out of range\n",
102 se, sh, cu);
105 next = strchr(p, ',');
106 if (!next)
107 break;
108 p = next + 1;
112 static bool amdgpu_gfx_is_multipipe_capable(struct amdgpu_device *adev)
114 if (amdgpu_compute_multipipe != -1) {
115 DRM_INFO("amdgpu: forcing compute pipe policy %d\n",
116 amdgpu_compute_multipipe);
117 return amdgpu_compute_multipipe == 1;
120 /* FIXME: spreading the queues across pipes causes perf regressions
121 * on POLARIS11 compute workloads */
122 if (adev->asic_type == CHIP_POLARIS11)
123 return false;
125 return adev->gfx.mec.num_mec > 1;
128 void amdgpu_gfx_compute_queue_acquire(struct amdgpu_device *adev)
130 int i, queue, pipe, mec;
131 bool multipipe_policy = amdgpu_gfx_is_multipipe_capable(adev);
133 /* policy for amdgpu compute queue ownership */
134 for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
135 queue = i % adev->gfx.mec.num_queue_per_pipe;
136 pipe = (i / adev->gfx.mec.num_queue_per_pipe)
137 % adev->gfx.mec.num_pipe_per_mec;
138 mec = (i / adev->gfx.mec.num_queue_per_pipe)
139 / adev->gfx.mec.num_pipe_per_mec;
141 /* we've run out of HW */
142 if (mec >= adev->gfx.mec.num_mec)
143 break;
145 if (multipipe_policy) {
146 /* policy: amdgpu owns the first two queues of the first MEC */
147 if (mec == 0 && queue < 2)
148 set_bit(i, adev->gfx.mec.queue_bitmap);
149 } else {
150 /* policy: amdgpu owns all queues in the first pipe */
151 if (mec == 0 && pipe == 0)
152 set_bit(i, adev->gfx.mec.queue_bitmap);
156 /* update the number of active compute rings */
157 adev->gfx.num_compute_rings =
158 bitmap_weight(adev->gfx.mec.queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
160 /* If you hit this case and edited the policy, you probably just
161 * need to increase AMDGPU_MAX_COMPUTE_RINGS */
162 if (WARN_ON(adev->gfx.num_compute_rings > AMDGPU_MAX_COMPUTE_RINGS))
163 adev->gfx.num_compute_rings = AMDGPU_MAX_COMPUTE_RINGS;
166 static int amdgpu_gfx_kiq_acquire(struct amdgpu_device *adev,
167 struct amdgpu_ring *ring)
169 int queue_bit;
170 int mec, pipe, queue;
172 queue_bit = adev->gfx.mec.num_mec
173 * adev->gfx.mec.num_pipe_per_mec
174 * adev->gfx.mec.num_queue_per_pipe;
176 while (queue_bit-- >= 0) {
177 if (test_bit(queue_bit, adev->gfx.mec.queue_bitmap))
178 continue;
180 amdgpu_gfx_bit_to_queue(adev, queue_bit, &mec, &pipe, &queue);
183 * 1. Using pipes 2/3 from MEC 2 seems cause problems.
184 * 2. It must use queue id 0, because CGPG_IDLE/SAVE/LOAD/RUN
185 * only can be issued on queue 0.
187 if ((mec == 1 && pipe > 1) || queue != 0)
188 continue;
190 ring->me = mec + 1;
191 ring->pipe = pipe;
192 ring->queue = queue;
194 return 0;
197 dev_err(adev->dev, "Failed to find a queue for KIQ\n");
198 return -EINVAL;
201 int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
202 struct amdgpu_ring *ring,
203 struct amdgpu_irq_src *irq)
205 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
206 int r = 0;
208 spin_lock_init(&kiq->ring_lock);
210 r = amdgpu_device_wb_get(adev, &adev->virt.reg_val_offs);
211 if (r)
212 return r;
214 ring->adev = NULL;
215 ring->ring_obj = NULL;
216 ring->use_doorbell = true;
217 ring->doorbell_index = AMDGPU_DOORBELL_KIQ;
219 r = amdgpu_gfx_kiq_acquire(adev, ring);
220 if (r)
221 return r;
223 ring->eop_gpu_addr = kiq->eop_gpu_addr;
224 sprintf(ring->name, "kiq_%d.%d.%d", ring->me, ring->pipe, ring->queue);
225 r = amdgpu_ring_init(adev, ring, 1024,
226 irq, AMDGPU_CP_KIQ_IRQ_DRIVER0);
227 if (r)
228 dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
230 return r;
233 void amdgpu_gfx_kiq_free_ring(struct amdgpu_ring *ring,
234 struct amdgpu_irq_src *irq)
236 amdgpu_device_wb_free(ring->adev, ring->adev->virt.reg_val_offs);
237 amdgpu_ring_fini(ring);
240 void amdgpu_gfx_kiq_fini(struct amdgpu_device *adev)
242 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
244 amdgpu_bo_free_kernel(&kiq->eop_obj, &kiq->eop_gpu_addr, NULL);
247 int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
248 unsigned hpd_size)
250 int r;
251 u32 *hpd;
252 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
254 r = amdgpu_bo_create_kernel(adev, hpd_size, PAGE_SIZE,
255 AMDGPU_GEM_DOMAIN_GTT, &kiq->eop_obj,
256 &kiq->eop_gpu_addr, (void **)&hpd);
257 if (r) {
258 dev_warn(adev->dev, "failed to create KIQ bo (%d).\n", r);
259 return r;
262 memset(hpd, 0, hpd_size);
264 r = amdgpu_bo_reserve(kiq->eop_obj, true);
265 if (unlikely(r != 0))
266 dev_warn(adev->dev, "(%d) reserve kiq eop bo failed\n", r);
267 amdgpu_bo_kunmap(kiq->eop_obj);
268 amdgpu_bo_unreserve(kiq->eop_obj);
270 return 0;
273 /* create MQD for each compute queue */
274 int amdgpu_gfx_compute_mqd_sw_init(struct amdgpu_device *adev,
275 unsigned mqd_size)
277 struct amdgpu_ring *ring = NULL;
278 int r, i;
280 /* create MQD for KIQ */
281 ring = &adev->gfx.kiq.ring;
282 if (!ring->mqd_obj) {
283 /* originaly the KIQ MQD is put in GTT domain, but for SRIOV VRAM domain is a must
284 * otherwise hypervisor trigger SAVE_VF fail after driver unloaded which mean MQD
285 * deallocated and gart_unbind, to strict diverage we decide to use VRAM domain for
286 * KIQ MQD no matter SRIOV or Bare-metal
288 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
289 AMDGPU_GEM_DOMAIN_VRAM, &ring->mqd_obj,
290 &ring->mqd_gpu_addr, &ring->mqd_ptr);
291 if (r) {
292 dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
293 return r;
296 /* prepare MQD backup */
297 adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS] = kmalloc(mqd_size, GFP_KERNEL);
298 if (!adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS])
299 dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
302 /* create MQD for each KCQ */
303 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
304 ring = &adev->gfx.compute_ring[i];
305 if (!ring->mqd_obj) {
306 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
307 AMDGPU_GEM_DOMAIN_GTT, &ring->mqd_obj,
308 &ring->mqd_gpu_addr, &ring->mqd_ptr);
309 if (r) {
310 dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
311 return r;
314 /* prepare MQD backup */
315 adev->gfx.mec.mqd_backup[i] = kmalloc(mqd_size, GFP_KERNEL);
316 if (!adev->gfx.mec.mqd_backup[i])
317 dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
321 return 0;
324 void amdgpu_gfx_compute_mqd_sw_fini(struct amdgpu_device *adev)
326 struct amdgpu_ring *ring = NULL;
327 int i;
329 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
330 ring = &adev->gfx.compute_ring[i];
331 kfree(adev->gfx.mec.mqd_backup[i]);
332 amdgpu_bo_free_kernel(&ring->mqd_obj,
333 &ring->mqd_gpu_addr,
334 &ring->mqd_ptr);
337 ring = &adev->gfx.kiq.ring;
338 kfree(adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS]);
339 amdgpu_bo_free_kernel(&ring->mqd_obj,
340 &ring->mqd_gpu_addr,
341 &ring->mqd_ptr);