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.
27 #include "amdgpu_gfx.h"
28 #include "amdgpu_rlc.h"
30 /* delay 0.1 second to enable gfx off feature */
31 #define GFX_OFF_DELAY_ENABLE msecs_to_jiffies(100)
34 * GPU GFX IP block helpers function.
37 int amdgpu_gfx_queue_to_bit(struct amdgpu_device *adev, int mec,
42 bit += mec * adev->gfx.mec.num_pipe_per_mec
43 * adev->gfx.mec.num_queue_per_pipe;
44 bit += pipe * adev->gfx.mec.num_queue_per_pipe;
50 void amdgpu_gfx_bit_to_queue(struct amdgpu_device *adev, int bit,
51 int *mec, int *pipe, int *queue)
53 *queue = bit % adev->gfx.mec.num_queue_per_pipe;
54 *pipe = (bit / adev->gfx.mec.num_queue_per_pipe)
55 % adev->gfx.mec.num_pipe_per_mec;
56 *mec = (bit / adev->gfx.mec.num_queue_per_pipe)
57 / adev->gfx.mec.num_pipe_per_mec;
61 bool amdgpu_gfx_is_mec_queue_enabled(struct amdgpu_device *adev,
62 int mec, int pipe, int queue)
64 return test_bit(amdgpu_gfx_queue_to_bit(adev, mec, pipe, queue),
65 adev->gfx.mec.queue_bitmap);
69 * amdgpu_gfx_scratch_get - Allocate a scratch register
71 * @adev: amdgpu_device pointer
72 * @reg: scratch register mmio offset
74 * Allocate a CP scratch register for use by the driver (all asics).
75 * Returns 0 on success or -EINVAL on failure.
77 int amdgpu_gfx_scratch_get(struct amdgpu_device *adev, uint32_t *reg)
81 i = ffs(adev->gfx.scratch.free_mask);
82 if (i != 0 && i <= adev->gfx.scratch.num_reg) {
84 adev->gfx.scratch.free_mask &= ~(1u << i);
85 *reg = adev->gfx.scratch.reg_base + i;
92 * amdgpu_gfx_scratch_free - Free a scratch register
94 * @adev: amdgpu_device pointer
95 * @reg: scratch register mmio offset
97 * Free a CP scratch register allocated for use by the driver (all asics)
99 void amdgpu_gfx_scratch_free(struct amdgpu_device *adev, uint32_t reg)
101 adev->gfx.scratch.free_mask |= 1u << (reg - adev->gfx.scratch.reg_base);
105 * amdgpu_gfx_parse_disable_cu - Parse the disable_cu module parameter
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
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].
114 void amdgpu_gfx_parse_disable_cu(unsigned *mask, unsigned max_se, unsigned max_sh)
119 memset(mask, 0, sizeof(*mask) * max_se * max_sh);
121 if (!amdgpu_disable_cu || !*amdgpu_disable_cu)
124 p = amdgpu_disable_cu;
127 int ret = sscanf(p, "%u.%u.%u", &se, &sh, &cu);
129 DRM_ERROR("amdgpu: could not parse disable_cu\n");
133 if (se < max_se && sh < max_sh && cu < 16) {
134 DRM_INFO("amdgpu: disabling CU %u.%u.%u\n", se, sh, cu);
135 mask[se * max_sh + sh] |= 1u << cu;
137 DRM_ERROR("amdgpu: disable_cu %u.%u.%u is out of range\n",
141 next = strchr(p, ',');
148 static bool amdgpu_gfx_is_multipipe_capable(struct amdgpu_device *adev)
150 if (amdgpu_compute_multipipe != -1) {
151 DRM_INFO("amdgpu: forcing compute pipe policy %d\n",
152 amdgpu_compute_multipipe);
153 return amdgpu_compute_multipipe == 1;
156 /* FIXME: spreading the queues across pipes causes perf regressions
157 * on POLARIS11 compute workloads */
158 if (adev->asic_type == CHIP_POLARIS11)
161 return adev->gfx.mec.num_mec > 1;
164 void amdgpu_gfx_compute_queue_acquire(struct amdgpu_device *adev)
166 int i, queue, pipe, mec;
167 bool multipipe_policy = amdgpu_gfx_is_multipipe_capable(adev);
169 /* policy for amdgpu compute queue ownership */
170 for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
171 queue = i % adev->gfx.mec.num_queue_per_pipe;
172 pipe = (i / adev->gfx.mec.num_queue_per_pipe)
173 % adev->gfx.mec.num_pipe_per_mec;
174 mec = (i / adev->gfx.mec.num_queue_per_pipe)
175 / adev->gfx.mec.num_pipe_per_mec;
177 /* we've run out of HW */
178 if (mec >= adev->gfx.mec.num_mec)
181 if (multipipe_policy) {
182 /* policy: amdgpu owns the first two queues of the first MEC */
183 if (mec == 0 && queue < 2)
184 set_bit(i, adev->gfx.mec.queue_bitmap);
186 /* policy: amdgpu owns all queues in the first pipe */
187 if (mec == 0 && pipe == 0)
188 set_bit(i, adev->gfx.mec.queue_bitmap);
192 /* update the number of active compute rings */
193 adev->gfx.num_compute_rings =
194 bitmap_weight(adev->gfx.mec.queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
196 /* If you hit this case and edited the policy, you probably just
197 * need to increase AMDGPU_MAX_COMPUTE_RINGS */
198 if (WARN_ON(adev->gfx.num_compute_rings > AMDGPU_MAX_COMPUTE_RINGS))
199 adev->gfx.num_compute_rings = AMDGPU_MAX_COMPUTE_RINGS;
202 void amdgpu_gfx_graphics_queue_acquire(struct amdgpu_device *adev)
204 int i, queue, pipe, me;
206 for (i = 0; i < AMDGPU_MAX_GFX_QUEUES; ++i) {
207 queue = i % adev->gfx.me.num_queue_per_pipe;
208 pipe = (i / adev->gfx.me.num_queue_per_pipe)
209 % adev->gfx.me.num_pipe_per_me;
210 me = (i / adev->gfx.me.num_queue_per_pipe)
211 / adev->gfx.me.num_pipe_per_me;
213 if (me >= adev->gfx.me.num_me)
215 /* policy: amdgpu owns the first queue per pipe at this stage
216 * will extend to mulitple queues per pipe later */
217 if (me == 0 && queue < 1)
218 set_bit(i, adev->gfx.me.queue_bitmap);
221 /* update the number of active graphics rings */
222 adev->gfx.num_gfx_rings =
223 bitmap_weight(adev->gfx.me.queue_bitmap, AMDGPU_MAX_GFX_QUEUES);
226 static int amdgpu_gfx_kiq_acquire(struct amdgpu_device *adev,
227 struct amdgpu_ring *ring)
230 int mec, pipe, queue;
232 queue_bit = adev->gfx.mec.num_mec
233 * adev->gfx.mec.num_pipe_per_mec
234 * adev->gfx.mec.num_queue_per_pipe;
236 while (queue_bit-- >= 0) {
237 if (test_bit(queue_bit, adev->gfx.mec.queue_bitmap))
240 amdgpu_gfx_bit_to_queue(adev, queue_bit, &mec, &pipe, &queue);
243 * 1. Using pipes 2/3 from MEC 2 seems cause problems.
244 * 2. It must use queue id 0, because CGPG_IDLE/SAVE/LOAD/RUN
245 * only can be issued on queue 0.
247 if ((mec == 1 && pipe > 1) || queue != 0)
257 dev_err(adev->dev, "Failed to find a queue for KIQ\n");
261 int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
262 struct amdgpu_ring *ring,
263 struct amdgpu_irq_src *irq)
265 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
268 spin_lock_init(&kiq->ring_lock);
270 r = amdgpu_device_wb_get(adev, &adev->virt.reg_val_offs);
275 ring->ring_obj = NULL;
276 ring->use_doorbell = true;
277 ring->doorbell_index = adev->doorbell_index.kiq;
279 r = amdgpu_gfx_kiq_acquire(adev, ring);
283 ring->eop_gpu_addr = kiq->eop_gpu_addr;
284 sprintf(ring->name, "kiq_%d.%d.%d", ring->me, ring->pipe, ring->queue);
285 r = amdgpu_ring_init(adev, ring, 1024,
286 irq, AMDGPU_CP_KIQ_IRQ_DRIVER0);
288 dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
293 void amdgpu_gfx_kiq_free_ring(struct amdgpu_ring *ring,
294 struct amdgpu_irq_src *irq)
296 amdgpu_device_wb_free(ring->adev, ring->adev->virt.reg_val_offs);
297 amdgpu_ring_fini(ring);
300 void amdgpu_gfx_kiq_fini(struct amdgpu_device *adev)
302 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
304 amdgpu_bo_free_kernel(&kiq->eop_obj, &kiq->eop_gpu_addr, NULL);
307 int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
312 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
314 r = amdgpu_bo_create_kernel(adev, hpd_size, PAGE_SIZE,
315 AMDGPU_GEM_DOMAIN_GTT, &kiq->eop_obj,
316 &kiq->eop_gpu_addr, (void **)&hpd);
318 dev_warn(adev->dev, "failed to create KIQ bo (%d).\n", r);
322 memset(hpd, 0, hpd_size);
324 r = amdgpu_bo_reserve(kiq->eop_obj, true);
325 if (unlikely(r != 0))
326 dev_warn(adev->dev, "(%d) reserve kiq eop bo failed\n", r);
327 amdgpu_bo_kunmap(kiq->eop_obj);
328 amdgpu_bo_unreserve(kiq->eop_obj);
333 /* create MQD for each compute queue */
334 int amdgpu_gfx_compute_mqd_sw_init(struct amdgpu_device *adev,
337 struct amdgpu_ring *ring = NULL;
340 /* create MQD for KIQ */
341 ring = &adev->gfx.kiq.ring;
342 if (!ring->mqd_obj) {
343 /* originaly the KIQ MQD is put in GTT domain, but for SRIOV VRAM domain is a must
344 * otherwise hypervisor trigger SAVE_VF fail after driver unloaded which mean MQD
345 * deallocated and gart_unbind, to strict diverage we decide to use VRAM domain for
346 * KIQ MQD no matter SRIOV or Bare-metal
348 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
349 AMDGPU_GEM_DOMAIN_VRAM, &ring->mqd_obj,
350 &ring->mqd_gpu_addr, &ring->mqd_ptr);
352 dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
356 /* prepare MQD backup */
357 adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS] = kmalloc(mqd_size, GFP_KERNEL);
358 if (!adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS])
359 dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
362 /* create MQD for each KCQ */
363 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
364 ring = &adev->gfx.compute_ring[i];
365 if (!ring->mqd_obj) {
366 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
367 AMDGPU_GEM_DOMAIN_GTT, &ring->mqd_obj,
368 &ring->mqd_gpu_addr, &ring->mqd_ptr);
370 dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
374 /* prepare MQD backup */
375 adev->gfx.mec.mqd_backup[i] = kmalloc(mqd_size, GFP_KERNEL);
376 if (!adev->gfx.mec.mqd_backup[i])
377 dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
384 void amdgpu_gfx_compute_mqd_sw_fini(struct amdgpu_device *adev)
386 struct amdgpu_ring *ring = NULL;
389 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
390 ring = &adev->gfx.compute_ring[i];
391 kfree(adev->gfx.mec.mqd_backup[i]);
392 amdgpu_bo_free_kernel(&ring->mqd_obj,
397 ring = &adev->gfx.kiq.ring;
398 kfree(adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS]);
399 amdgpu_bo_free_kernel(&ring->mqd_obj,
404 /* amdgpu_gfx_off_ctrl - Handle gfx off feature enable/disable
406 * @adev: amdgpu_device pointer
407 * @bool enable true: enable gfx off feature, false: disable gfx off feature
409 * 1. gfx off feature will be enabled by gfx ip after gfx cg gp enabled.
410 * 2. other client can send request to disable gfx off feature, the request should be honored.
411 * 3. other client can cancel their request of disable gfx off feature
412 * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
415 void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
417 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
420 if (!adev->powerplay.pp_funcs || !adev->powerplay.pp_funcs->set_powergating_by_smu)
424 mutex_lock(&adev->gfx.gfx_off_mutex);
427 adev->gfx.gfx_off_req_count++;
428 else if (adev->gfx.gfx_off_req_count > 0)
429 adev->gfx.gfx_off_req_count--;
431 if (enable && !adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
432 schedule_delayed_work(&adev->gfx.gfx_off_delay_work, GFX_OFF_DELAY_ENABLE);
433 } else if (!enable && adev->gfx.gfx_off_state) {
434 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false))
435 adev->gfx.gfx_off_state = false;
438 mutex_unlock(&adev->gfx.gfx_off_mutex);