]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
Revert "drm/amdkfd: Fix sdma queue allocate race condition"
[mirror_ubuntu-eoan-kernel.git] / drivers / gpu / drm / amd / amdkfd / kfd_device_queue_manager.c
CommitLineData
64c7f8cf
BG
1/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
26103436
FK
24#include <linux/ratelimit.h>
25#include <linux/printk.h>
64c7f8cf
BG
26#include <linux/slab.h>
27#include <linux/list.h>
28#include <linux/types.h>
64c7f8cf 29#include <linux/bitops.h>
99331a51 30#include <linux/sched.h>
64c7f8cf
BG
31#include "kfd_priv.h"
32#include "kfd_device_queue_manager.h"
33#include "kfd_mqd_manager.h"
34#include "cik_regs.h"
35#include "kfd_kernel_queue.h"
5b87245f 36#include "amdgpu_amdkfd.h"
64c7f8cf
BG
37
38/* Size of the per-pipe EOP queue */
39#define CIK_HPD_EOP_BYTES_LOG2 11
40#define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2)
41
64c7f8cf
BG
42static int set_pasid_vmid_mapping(struct device_queue_manager *dqm,
43 unsigned int pasid, unsigned int vmid);
44
c4744e24
YZ
45static int execute_queues_cpsch(struct device_queue_manager *dqm,
46 enum kfd_unmap_queues_filter filter,
47 uint32_t filter_param);
7da2bcf8 48static int unmap_queues_cpsch(struct device_queue_manager *dqm,
4465f466
YZ
49 enum kfd_unmap_queues_filter filter,
50 uint32_t filter_param);
64c7f8cf 51
60a00956
FK
52static int map_queues_cpsch(struct device_queue_manager *dqm);
53
bcea3081 54static void deallocate_sdma_queue(struct device_queue_manager *dqm,
1b4670f6 55 struct queue *q);
64c7f8cf 56
d39b7737
OZ
57static inline void deallocate_hqd(struct device_queue_manager *dqm,
58 struct queue *q);
59static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q);
60static int allocate_sdma_queue(struct device_queue_manager *dqm,
61 struct queue *q);
73ea648d
SL
62static void kfd_process_hw_exception(struct work_struct *work);
63
bcea3081
BG
64static inline
65enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
64c7f8cf 66{
1b4670f6 67 if (type == KFD_QUEUE_TYPE_SDMA || type == KFD_QUEUE_TYPE_SDMA_XGMI)
85d258f9
BG
68 return KFD_MQD_TYPE_SDMA;
69 return KFD_MQD_TYPE_CP;
64c7f8cf
BG
70}
71
d0b63bb3
AR
72static bool is_pipe_enabled(struct device_queue_manager *dqm, int mec, int pipe)
73{
74 int i;
75 int pipe_offset = mec * dqm->dev->shared_resources.num_pipe_per_mec
76 + pipe * dqm->dev->shared_resources.num_queue_per_pipe;
77
78 /* queue is available for KFD usage if bit is 1 */
79 for (i = 0; i < dqm->dev->shared_resources.num_queue_per_pipe; ++i)
80 if (test_bit(pipe_offset + i,
81 dqm->dev->shared_resources.queue_bitmap))
82 return true;
83 return false;
84}
85
d0b63bb3 86unsigned int get_queues_num(struct device_queue_manager *dqm)
64ea8f4a 87{
d0b63bb3
AR
88 return bitmap_weight(dqm->dev->shared_resources.queue_bitmap,
89 KGD_MAX_QUEUES);
64ea8f4a
OG
90}
91
d0b63bb3 92unsigned int get_queues_per_pipe(struct device_queue_manager *dqm)
64c7f8cf 93{
d0b63bb3
AR
94 return dqm->dev->shared_resources.num_queue_per_pipe;
95}
96
97unsigned int get_pipes_per_mec(struct device_queue_manager *dqm)
98{
d0b63bb3 99 return dqm->dev->shared_resources.num_pipe_per_mec;
64c7f8cf
BG
100}
101
98bb9222
YZ
102static unsigned int get_num_sdma_engines(struct device_queue_manager *dqm)
103{
104 return dqm->dev->device_info->num_sdma_engines;
105}
106
1b4670f6
OZ
107static unsigned int get_num_xgmi_sdma_engines(struct device_queue_manager *dqm)
108{
109 return dqm->dev->device_info->num_xgmi_sdma_engines;
110}
111
98bb9222
YZ
112unsigned int get_num_sdma_queues(struct device_queue_manager *dqm)
113{
114 return dqm->dev->device_info->num_sdma_engines
d5094189 115 * dqm->dev->device_info->num_sdma_queues_per_engine;
98bb9222
YZ
116}
117
1b4670f6
OZ
118unsigned int get_num_xgmi_sdma_queues(struct device_queue_manager *dqm)
119{
120 return dqm->dev->device_info->num_xgmi_sdma_engines
121 * dqm->dev->device_info->num_sdma_queues_per_engine;
122}
123
a22fc854 124void program_sh_mem_settings(struct device_queue_manager *dqm,
64c7f8cf
BG
125 struct qcm_process_device *qpd)
126{
cea405b1
XZ
127 return dqm->dev->kfd2kgd->program_sh_mem_settings(
128 dqm->dev->kgd, qpd->vmid,
64c7f8cf
BG
129 qpd->sh_mem_config,
130 qpd->sh_mem_ape1_base,
131 qpd->sh_mem_ape1_limit,
132 qpd->sh_mem_bases);
133}
134
ef568db7
FK
135static int allocate_doorbell(struct qcm_process_device *qpd, struct queue *q)
136{
137 struct kfd_dev *dev = qpd->dqm->dev;
138
139 if (!KFD_IS_SOC15(dev->device_info->asic_family)) {
140 /* On pre-SOC15 chips we need to use the queue ID to
141 * preserve the user mode ABI.
142 */
143 q->doorbell_id = q->properties.queue_id;
1b4670f6
OZ
144 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
145 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
234441dd
YZ
146 /* For SDMA queues on SOC15 with 8-byte doorbell, use static
147 * doorbell assignments based on the engine and queue id.
148 * The doobell index distance between RLC (2*i) and (2*i+1)
149 * for a SDMA engine is 512.
ef568db7 150 */
234441dd
YZ
151 uint32_t *idx_offset =
152 dev->shared_resources.sdma_doorbell_idx;
153
154 q->doorbell_id = idx_offset[q->properties.sdma_engine_id]
155 + (q->properties.sdma_queue_id & 1)
156 * KFD_QUEUE_DOORBELL_MIRROR_OFFSET
157 + (q->properties.sdma_queue_id >> 1);
ef568db7
FK
158 } else {
159 /* For CP queues on SOC15 reserve a free doorbell ID */
160 unsigned int found;
161
162 found = find_first_zero_bit(qpd->doorbell_bitmap,
163 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
164 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
165 pr_debug("No doorbells available");
166 return -EBUSY;
167 }
168 set_bit(found, qpd->doorbell_bitmap);
169 q->doorbell_id = found;
170 }
171
172 q->properties.doorbell_off =
173 kfd_doorbell_id_to_offset(dev, q->process,
174 q->doorbell_id);
175
176 return 0;
177}
178
179static void deallocate_doorbell(struct qcm_process_device *qpd,
180 struct queue *q)
181{
182 unsigned int old;
183 struct kfd_dev *dev = qpd->dqm->dev;
184
185 if (!KFD_IS_SOC15(dev->device_info->asic_family) ||
1b4670f6
OZ
186 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
187 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
ef568db7
FK
188 return;
189
190 old = test_and_clear_bit(q->doorbell_id, qpd->doorbell_bitmap);
191 WARN_ON(!old);
192}
193
64c7f8cf
BG
194static int allocate_vmid(struct device_queue_manager *dqm,
195 struct qcm_process_device *qpd,
196 struct queue *q)
197{
198 int bit, allocated_vmid;
199
200 if (dqm->vmid_bitmap == 0)
201 return -ENOMEM;
202
4252bf68
HK
203 bit = ffs(dqm->vmid_bitmap) - 1;
204 dqm->vmid_bitmap &= ~(1 << bit);
64c7f8cf 205
44008d7a 206 allocated_vmid = bit + dqm->dev->vm_info.first_vmid_kfd;
79775b62 207 pr_debug("vmid allocation %d\n", allocated_vmid);
64c7f8cf
BG
208 qpd->vmid = allocated_vmid;
209 q->properties.vmid = allocated_vmid;
210
211 set_pasid_vmid_mapping(dqm, q->process->pasid, q->properties.vmid);
212 program_sh_mem_settings(dqm, qpd);
213
403575c4
FK
214 /* qpd->page_table_base is set earlier when register_process()
215 * is called, i.e. when the first queue is created.
216 */
217 dqm->dev->kfd2kgd->set_vm_context_page_table_base(dqm->dev->kgd,
218 qpd->vmid,
219 qpd->page_table_base);
220 /* invalidate the VM context after pasid and vmid mapping is set up */
221 kfd_flush_tlb(qpd_to_pdd(qpd));
222
d39b7737
OZ
223 dqm->dev->kfd2kgd->set_scratch_backing_va(
224 dqm->dev->kgd, qpd->sh_hidden_private_base, qpd->vmid);
225
64c7f8cf
BG
226 return 0;
227}
228
552764b6
FK
229static int flush_texture_cache_nocpsch(struct kfd_dev *kdev,
230 struct qcm_process_device *qpd)
231{
f6e27ff1
FK
232 const struct packet_manager_funcs *pmf = qpd->dqm->packets.pmf;
233 int ret;
552764b6
FK
234
235 if (!qpd->ib_kaddr)
236 return -ENOMEM;
237
f6e27ff1
FK
238 ret = pmf->release_mem(qpd->ib_base, (uint32_t *)qpd->ib_kaddr);
239 if (ret)
240 return ret;
552764b6 241
5b87245f 242 return amdgpu_amdkfd_submit_ib(kdev->kgd, KGD_ENGINE_MEC1, qpd->vmid,
f6e27ff1
FK
243 qpd->ib_base, (uint32_t *)qpd->ib_kaddr,
244 pmf->release_mem_size / sizeof(uint32_t));
552764b6
FK
245}
246
64c7f8cf
BG
247static void deallocate_vmid(struct device_queue_manager *dqm,
248 struct qcm_process_device *qpd,
249 struct queue *q)
250{
44008d7a 251 int bit = qpd->vmid - dqm->dev->vm_info.first_vmid_kfd;
64c7f8cf 252
552764b6
FK
253 /* On GFX v7, CP doesn't flush TC at dequeue */
254 if (q->device->device_info->asic_family == CHIP_HAWAII)
255 if (flush_texture_cache_nocpsch(q->device, qpd))
256 pr_err("Failed to flush TC\n");
257
403575c4
FK
258 kfd_flush_tlb(qpd_to_pdd(qpd));
259
2030664b
BG
260 /* Release the vmid mapping */
261 set_pasid_vmid_mapping(dqm, 0, qpd->vmid);
262
4252bf68 263 dqm->vmid_bitmap |= (1 << bit);
64c7f8cf
BG
264 qpd->vmid = 0;
265 q->properties.vmid = 0;
266}
267
268static int create_queue_nocpsch(struct device_queue_manager *dqm,
269 struct queue *q,
b46cb7d7 270 struct qcm_process_device *qpd)
64c7f8cf 271{
d39b7737 272 struct mqd_manager *mqd_mgr;
64c7f8cf
BG
273 int retval;
274
64c7f8cf
BG
275 print_queue(q);
276
06b89b38
OZ
277 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
278 q->properties.type)];
279 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
280 if (!q->mqd_mem_obj)
281 return -ENOMEM;
282
efeaed4d 283 dqm_lock(dqm);
64c7f8cf 284
b8cbab04 285 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
79775b62 286 pr_warn("Can't create new usermode queue because %d queues were already created\n",
b8cbab04 287 dqm->total_queue_count);
ab7c1648
KR
288 retval = -EPERM;
289 goto out_unlock;
b8cbab04
OG
290 }
291
64c7f8cf
BG
292 if (list_empty(&qpd->queues_list)) {
293 retval = allocate_vmid(dqm, qpd, q);
ab7c1648
KR
294 if (retval)
295 goto out_unlock;
64c7f8cf 296 }
64c7f8cf 297 q->properties.vmid = qpd->vmid;
26103436 298 /*
bb2d2128
FK
299 * Eviction state logic: mark all queues as evicted, even ones
300 * not currently active. Restoring inactive queues later only
301 * updates the is_evicted flag but is a no-op otherwise.
26103436 302 */
bb2d2128 303 q->properties.is_evicted = !!qpd->evicted;
64c7f8cf 304
373d7080
FK
305 q->properties.tba_addr = qpd->tba_addr;
306 q->properties.tma_addr = qpd->tma_addr;
307
d39b7737
OZ
308 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) {
309 retval = allocate_hqd(dqm, q);
310 if (retval)
311 goto deallocate_vmid;
312 pr_debug("Loading mqd to hqd on pipe %d, queue %d\n",
313 q->pipe, q->queue);
314 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
315 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
316 retval = allocate_sdma_queue(dqm, q);
317 if (retval)
318 goto deallocate_vmid;
319 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
320 }
321
322 retval = allocate_doorbell(qpd, q);
323 if (retval)
324 goto out_deallocate_hqd;
325
8636e53c
OZ
326 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
327 &q->gart_mqd_addr, &q->properties);
d39b7737
OZ
328 if (q->properties.is_active) {
329
330 if (WARN(q->process->mm != current->mm,
331 "should only run in user thread"))
332 retval = -EFAULT;
333 else
334 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
335 q->queue, &q->properties, current->mm);
336 if (retval)
06b89b38 337 goto out_deallocate_doorbell;
64c7f8cf
BG
338 }
339
340 list_add(&q->list, &qpd->queues_list);
bc920fd4 341 qpd->queue_count++;
b6819cec
JC
342 if (q->properties.is_active)
343 dqm->queue_count++;
64c7f8cf 344
bcea3081
BG
345 if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
346 dqm->sdma_queue_count++;
1b4670f6
OZ
347 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
348 dqm->xgmi_sdma_queue_count++;
64c7f8cf 349
b8cbab04
OG
350 /*
351 * Unconditionally increment this counter, regardless of the queue's
352 * type or whether the queue is active.
353 */
354 dqm->total_queue_count++;
355 pr_debug("Total of %d queues are accountable so far\n",
356 dqm->total_queue_count);
06b89b38
OZ
357 dqm_unlock(dqm);
358 return retval;
b8cbab04 359
d39b7737
OZ
360out_deallocate_doorbell:
361 deallocate_doorbell(qpd, q);
362out_deallocate_hqd:
363 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
364 deallocate_hqd(dqm, q);
365 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
366 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
367 deallocate_sdma_queue(dqm, q);
368deallocate_vmid:
369 if (list_empty(&qpd->queues_list))
370 deallocate_vmid(dqm, qpd, q);
ab7c1648 371out_unlock:
efeaed4d 372 dqm_unlock(dqm);
06b89b38 373 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
ab7c1648 374 return retval;
64c7f8cf
BG
375}
376
377static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q)
378{
379 bool set;
f0ec5b99 380 int pipe, bit, i;
64c7f8cf
BG
381
382 set = false;
383
8eabaf54
KR
384 for (pipe = dqm->next_pipe_to_allocate, i = 0;
385 i < get_pipes_per_mec(dqm);
d0b63bb3
AR
386 pipe = ((pipe + 1) % get_pipes_per_mec(dqm)), ++i) {
387
388 if (!is_pipe_enabled(dqm, 0, pipe))
389 continue;
390
64c7f8cf 391 if (dqm->allocated_queues[pipe] != 0) {
4252bf68
HK
392 bit = ffs(dqm->allocated_queues[pipe]) - 1;
393 dqm->allocated_queues[pipe] &= ~(1 << bit);
64c7f8cf
BG
394 q->pipe = pipe;
395 q->queue = bit;
396 set = true;
397 break;
398 }
399 }
400
991ca8ee 401 if (!set)
64c7f8cf
BG
402 return -EBUSY;
403
79775b62 404 pr_debug("hqd slot - pipe %d, queue %d\n", q->pipe, q->queue);
64c7f8cf 405 /* horizontal hqd allocation */
d0b63bb3 406 dqm->next_pipe_to_allocate = (pipe + 1) % get_pipes_per_mec(dqm);
64c7f8cf
BG
407
408 return 0;
409}
410
411static inline void deallocate_hqd(struct device_queue_manager *dqm,
412 struct queue *q)
413{
4252bf68 414 dqm->allocated_queues[q->pipe] |= (1 << q->queue);
64c7f8cf
BG
415}
416
9fd3f1bf
FK
417/* Access to DQM has to be locked before calling destroy_queue_nocpsch_locked
418 * to avoid asynchronized access
419 */
420static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,
64c7f8cf
BG
421 struct qcm_process_device *qpd,
422 struct queue *q)
423{
424 int retval;
8d5f3552 425 struct mqd_manager *mqd_mgr;
64c7f8cf 426
fdfa090b
OZ
427 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
428 q->properties.type)];
64c7f8cf 429
c2e1b3a4 430 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) {
c2e1b3a4
BG
431 deallocate_hqd(dqm, q);
432 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
c2e1b3a4 433 dqm->sdma_queue_count--;
1b4670f6
OZ
434 deallocate_sdma_queue(dqm, q);
435 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
436 dqm->xgmi_sdma_queue_count--;
437 deallocate_sdma_queue(dqm, q);
7113cd65 438 } else {
79775b62 439 pr_debug("q->properties.type %d is invalid\n",
7113cd65 440 q->properties.type);
9fd3f1bf 441 return -EINVAL;
64c7f8cf 442 }
9fd3f1bf 443 dqm->total_queue_count--;
64c7f8cf 444
ef568db7
FK
445 deallocate_doorbell(qpd, q);
446
8d5f3552 447 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
c2e1b3a4 448 KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
b90e3fbe 449 KFD_UNMAP_LATENCY_MS,
64c7f8cf 450 q->pipe, q->queue);
9fd3f1bf
FK
451 if (retval == -ETIME)
452 qpd->reset_wavefronts = true;
64c7f8cf 453
8636e53c 454 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
64c7f8cf
BG
455
456 list_del(&q->list);
9fd3f1bf
FK
457 if (list_empty(&qpd->queues_list)) {
458 if (qpd->reset_wavefronts) {
459 pr_warn("Resetting wave fronts (nocpsch) on dev %p\n",
460 dqm->dev);
461 /* dbgdev_wave_reset_wavefronts has to be called before
462 * deallocate_vmid(), i.e. when vmid is still in use.
463 */
464 dbgdev_wave_reset_wavefronts(dqm->dev,
465 qpd->pqm->process);
466 qpd->reset_wavefronts = false;
467 }
468
64c7f8cf 469 deallocate_vmid(dqm, qpd, q);
9fd3f1bf 470 }
bc920fd4 471 qpd->queue_count--;
b6819cec
JC
472 if (q->properties.is_active)
473 dqm->queue_count--;
b8cbab04 474
9fd3f1bf
FK
475 return retval;
476}
b8cbab04 477
9fd3f1bf
FK
478static int destroy_queue_nocpsch(struct device_queue_manager *dqm,
479 struct qcm_process_device *qpd,
480 struct queue *q)
481{
482 int retval;
483
efeaed4d 484 dqm_lock(dqm);
9fd3f1bf 485 retval = destroy_queue_nocpsch_locked(dqm, qpd, q);
efeaed4d 486 dqm_unlock(dqm);
9fd3f1bf 487
64c7f8cf
BG
488 return retval;
489}
490
491static int update_queue(struct device_queue_manager *dqm, struct queue *q)
492{
8636e53c 493 int retval = 0;
8d5f3552 494 struct mqd_manager *mqd_mgr;
26103436 495 struct kfd_process_device *pdd;
b6ffbab8 496 bool prev_active = false;
64c7f8cf 497
efeaed4d 498 dqm_lock(dqm);
26103436
FK
499 pdd = kfd_get_process_device_data(q->device, q->process);
500 if (!pdd) {
501 retval = -ENODEV;
502 goto out_unlock;
503 }
fdfa090b
OZ
504 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
505 q->properties.type)];
64c7f8cf 506
60a00956
FK
507 /* Save previous activity state for counters */
508 prev_active = q->properties.is_active;
509
510 /* Make sure the queue is unmapped before updating the MQD */
d146c5a7 511 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
60a00956
FK
512 retval = unmap_queues_cpsch(dqm,
513 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
894a8293 514 if (retval) {
60a00956
FK
515 pr_err("unmap queue failed\n");
516 goto out_unlock;
517 }
894a8293 518 } else if (prev_active &&
60a00956 519 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
1b4670f6
OZ
520 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
521 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
8d5f3552 522 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
60a00956
FK
523 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN,
524 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
525 if (retval) {
526 pr_err("destroy mqd failed\n");
527 goto out_unlock;
528 }
529 }
530
8636e53c 531 mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties);
60a00956 532
096d1a3e
FK
533 /*
534 * check active state vs. the previous state and modify
535 * counter accordingly. map_queues_cpsch uses the
536 * dqm->queue_count to determine whether a new runlist must be
537 * uploaded.
538 */
539 if (q->properties.is_active && !prev_active)
540 dqm->queue_count++;
541 else if (!q->properties.is_active && prev_active)
542 dqm->queue_count--;
543
d146c5a7 544 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS)
60a00956 545 retval = map_queues_cpsch(dqm);
894a8293 546 else if (q->properties.is_active &&
60a00956 547 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
1b4670f6
OZ
548 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
549 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
1b19aa5a
FK
550 if (WARN(q->process->mm != current->mm,
551 "should only run in user thread"))
552 retval = -EFAULT;
553 else
554 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd,
555 q->pipe, q->queue,
556 &q->properties, current->mm);
557 }
b6ffbab8 558
ab7c1648 559out_unlock:
efeaed4d 560 dqm_unlock(dqm);
64c7f8cf
BG
561 return retval;
562}
563
26103436
FK
564static int evict_process_queues_nocpsch(struct device_queue_manager *dqm,
565 struct qcm_process_device *qpd)
566{
567 struct queue *q;
8d5f3552 568 struct mqd_manager *mqd_mgr;
26103436 569 struct kfd_process_device *pdd;
bb2d2128 570 int retval, ret = 0;
26103436 571
efeaed4d 572 dqm_lock(dqm);
26103436
FK
573 if (qpd->evicted++ > 0) /* already evicted, do nothing */
574 goto out;
575
576 pdd = qpd_to_pdd(qpd);
577 pr_info_ratelimited("Evicting PASID %u queues\n",
578 pdd->process->pasid);
579
bb2d2128
FK
580 /* Mark all queues as evicted. Deactivate all active queues on
581 * the qpd.
582 */
26103436 583 list_for_each_entry(q, &qpd->queues_list, list) {
bb2d2128 584 q->properties.is_evicted = true;
26103436
FK
585 if (!q->properties.is_active)
586 continue;
bb2d2128 587
fdfa090b
OZ
588 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
589 q->properties.type)];
26103436 590 q->properties.is_active = false;
8d5f3552 591 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
26103436
FK
592 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN,
593 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
bb2d2128
FK
594 if (retval && !ret)
595 /* Return the first error, but keep going to
596 * maintain a consistent eviction state
597 */
598 ret = retval;
26103436
FK
599 dqm->queue_count--;
600 }
601
602out:
efeaed4d 603 dqm_unlock(dqm);
bb2d2128 604 return ret;
26103436
FK
605}
606
607static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
608 struct qcm_process_device *qpd)
609{
610 struct queue *q;
611 struct kfd_process_device *pdd;
612 int retval = 0;
613
efeaed4d 614 dqm_lock(dqm);
26103436
FK
615 if (qpd->evicted++ > 0) /* already evicted, do nothing */
616 goto out;
617
618 pdd = qpd_to_pdd(qpd);
619 pr_info_ratelimited("Evicting PASID %u queues\n",
620 pdd->process->pasid);
621
bb2d2128
FK
622 /* Mark all queues as evicted. Deactivate all active queues on
623 * the qpd.
624 */
26103436 625 list_for_each_entry(q, &qpd->queues_list, list) {
bb2d2128 626 q->properties.is_evicted = true;
26103436
FK
627 if (!q->properties.is_active)
628 continue;
bb2d2128 629
26103436
FK
630 q->properties.is_active = false;
631 dqm->queue_count--;
632 }
633 retval = execute_queues_cpsch(dqm,
634 qpd->is_debug ?
635 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES :
636 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
637
638out:
efeaed4d 639 dqm_unlock(dqm);
26103436
FK
640 return retval;
641}
642
643static int restore_process_queues_nocpsch(struct device_queue_manager *dqm,
644 struct qcm_process_device *qpd)
645{
1b19aa5a 646 struct mm_struct *mm = NULL;
26103436 647 struct queue *q;
8d5f3552 648 struct mqd_manager *mqd_mgr;
26103436 649 struct kfd_process_device *pdd;
e715c6d0 650 uint64_t pd_base;
bb2d2128 651 int retval, ret = 0;
26103436
FK
652
653 pdd = qpd_to_pdd(qpd);
654 /* Retrieve PD base */
5b87245f 655 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->vm);
26103436 656
efeaed4d 657 dqm_lock(dqm);
26103436
FK
658 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
659 goto out;
660 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
661 qpd->evicted--;
662 goto out;
663 }
664
665 pr_info_ratelimited("Restoring PASID %u queues\n",
666 pdd->process->pasid);
667
668 /* Update PD Base in QPD */
669 qpd->page_table_base = pd_base;
e715c6d0 670 pr_debug("Updated PD address to 0x%llx\n", pd_base);
26103436
FK
671
672 if (!list_empty(&qpd->queues_list)) {
673 dqm->dev->kfd2kgd->set_vm_context_page_table_base(
674 dqm->dev->kgd,
675 qpd->vmid,
676 qpd->page_table_base);
677 kfd_flush_tlb(pdd);
678 }
679
1b19aa5a
FK
680 /* Take a safe reference to the mm_struct, which may otherwise
681 * disappear even while the kfd_process is still referenced.
682 */
683 mm = get_task_mm(pdd->process->lead_thread);
684 if (!mm) {
bb2d2128 685 ret = -EFAULT;
1b19aa5a
FK
686 goto out;
687 }
688
bb2d2128
FK
689 /* Remove the eviction flags. Activate queues that are not
690 * inactive for other reasons.
691 */
26103436 692 list_for_each_entry(q, &qpd->queues_list, list) {
bb2d2128
FK
693 q->properties.is_evicted = false;
694 if (!QUEUE_IS_ACTIVE(q->properties))
26103436 695 continue;
bb2d2128 696
fdfa090b
OZ
697 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
698 q->properties.type)];
26103436 699 q->properties.is_active = true;
8d5f3552 700 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
1b19aa5a 701 q->queue, &q->properties, mm);
bb2d2128
FK
702 if (retval && !ret)
703 /* Return the first error, but keep going to
704 * maintain a consistent eviction state
705 */
706 ret = retval;
26103436
FK
707 dqm->queue_count++;
708 }
709 qpd->evicted = 0;
710out:
1b19aa5a
FK
711 if (mm)
712 mmput(mm);
efeaed4d 713 dqm_unlock(dqm);
bb2d2128 714 return ret;
26103436
FK
715}
716
717static int restore_process_queues_cpsch(struct device_queue_manager *dqm,
718 struct qcm_process_device *qpd)
719{
720 struct queue *q;
721 struct kfd_process_device *pdd;
e715c6d0 722 uint64_t pd_base;
26103436
FK
723 int retval = 0;
724
725 pdd = qpd_to_pdd(qpd);
726 /* Retrieve PD base */
5b87245f 727 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->vm);
26103436 728
efeaed4d 729 dqm_lock(dqm);
26103436
FK
730 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
731 goto out;
732 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
733 qpd->evicted--;
734 goto out;
735 }
736
737 pr_info_ratelimited("Restoring PASID %u queues\n",
738 pdd->process->pasid);
739
740 /* Update PD Base in QPD */
741 qpd->page_table_base = pd_base;
e715c6d0 742 pr_debug("Updated PD address to 0x%llx\n", pd_base);
26103436
FK
743
744 /* activate all active queues on the qpd */
745 list_for_each_entry(q, &qpd->queues_list, list) {
26103436 746 q->properties.is_evicted = false;
bb2d2128
FK
747 if (!QUEUE_IS_ACTIVE(q->properties))
748 continue;
749
26103436
FK
750 q->properties.is_active = true;
751 dqm->queue_count++;
752 }
753 retval = execute_queues_cpsch(dqm,
754 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
bb2d2128 755 qpd->evicted = 0;
26103436 756out:
efeaed4d 757 dqm_unlock(dqm);
26103436
FK
758 return retval;
759}
760
58dcd5bf 761static int register_process(struct device_queue_manager *dqm,
64c7f8cf
BG
762 struct qcm_process_device *qpd)
763{
764 struct device_process_node *n;
403575c4 765 struct kfd_process_device *pdd;
e715c6d0 766 uint64_t pd_base;
a22fc854 767 int retval;
64c7f8cf 768
dbf56ab1 769 n = kzalloc(sizeof(*n), GFP_KERNEL);
64c7f8cf
BG
770 if (!n)
771 return -ENOMEM;
772
773 n->qpd = qpd;
774
403575c4
FK
775 pdd = qpd_to_pdd(qpd);
776 /* Retrieve PD base */
5b87245f 777 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->vm);
403575c4 778
efeaed4d 779 dqm_lock(dqm);
64c7f8cf
BG
780 list_add(&n->list, &dqm->queues);
781
403575c4
FK
782 /* Update PD Base in QPD */
783 qpd->page_table_base = pd_base;
e715c6d0 784 pr_debug("Updated PD address to 0x%llx\n", pd_base);
403575c4 785
bfd5e378 786 retval = dqm->asic_ops.update_qpd(dqm, qpd);
a22fc854 787
f756e631 788 dqm->processes_count++;
64c7f8cf 789
efeaed4d 790 dqm_unlock(dqm);
64c7f8cf 791
32cce8bc
FK
792 /* Outside the DQM lock because under the DQM lock we can't do
793 * reclaim or take other locks that others hold while reclaiming.
794 */
795 kfd_inc_compute_active(dqm->dev);
796
a22fc854 797 return retval;
64c7f8cf
BG
798}
799
58dcd5bf 800static int unregister_process(struct device_queue_manager *dqm,
64c7f8cf
BG
801 struct qcm_process_device *qpd)
802{
803 int retval;
804 struct device_process_node *cur, *next;
805
1e5ec956
OG
806 pr_debug("qpd->queues_list is %s\n",
807 list_empty(&qpd->queues_list) ? "empty" : "not empty");
64c7f8cf
BG
808
809 retval = 0;
efeaed4d 810 dqm_lock(dqm);
64c7f8cf
BG
811
812 list_for_each_entry_safe(cur, next, &dqm->queues, list) {
813 if (qpd == cur->qpd) {
814 list_del(&cur->list);
f5d896bb 815 kfree(cur);
f756e631 816 dqm->processes_count--;
64c7f8cf
BG
817 goto out;
818 }
819 }
820 /* qpd not found in dqm list */
821 retval = 1;
822out:
efeaed4d 823 dqm_unlock(dqm);
32cce8bc
FK
824
825 /* Outside the DQM lock because under the DQM lock we can't do
826 * reclaim or take other locks that others hold while reclaiming.
827 */
828 if (!retval)
829 kfd_dec_compute_active(dqm->dev);
830
64c7f8cf
BG
831 return retval;
832}
833
834static int
835set_pasid_vmid_mapping(struct device_queue_manager *dqm, unsigned int pasid,
836 unsigned int vmid)
837{
cea405b1 838 return dqm->dev->kfd2kgd->set_pasid_vmid_mapping(
deb99d7c 839 dqm->dev->kgd, pasid, vmid);
64c7f8cf
BG
840}
841
2249d558
AL
842static void init_interrupts(struct device_queue_manager *dqm)
843{
844 unsigned int i;
845
d0b63bb3
AR
846 for (i = 0 ; i < get_pipes_per_mec(dqm) ; i++)
847 if (is_pipe_enabled(dqm, 0, i))
848 dqm->dev->kfd2kgd->init_interrupts(dqm->dev->kgd, i);
2249d558
AL
849}
850
64c7f8cf
BG
851static int initialize_nocpsch(struct device_queue_manager *dqm)
852{
86194cf8 853 int pipe, queue;
64c7f8cf 854
79775b62 855 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
64c7f8cf 856
ab7c1648
KR
857 dqm->allocated_queues = kcalloc(get_pipes_per_mec(dqm),
858 sizeof(unsigned int), GFP_KERNEL);
859 if (!dqm->allocated_queues)
860 return -ENOMEM;
861
efeaed4d 862 mutex_init(&dqm->lock_hidden);
64c7f8cf
BG
863 INIT_LIST_HEAD(&dqm->queues);
864 dqm->queue_count = dqm->next_pipe_to_allocate = 0;
bcea3081 865 dqm->sdma_queue_count = 0;
1b4670f6 866 dqm->xgmi_sdma_queue_count = 0;
64c7f8cf 867
86194cf8
FK
868 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
869 int pipe_offset = pipe * get_queues_per_pipe(dqm);
870
871 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++)
872 if (test_bit(pipe_offset + queue,
873 dqm->dev->shared_resources.queue_bitmap))
874 dqm->allocated_queues[pipe] |= 1 << queue;
875 }
64c7f8cf 876
44008d7a 877 dqm->vmid_bitmap = (1 << dqm->dev->vm_info.vmid_num_kfd) - 1;
cb77ee7c 878 dqm->sdma_bitmap = (1ULL << get_num_sdma_queues(dqm)) - 1;
1b4670f6 879 dqm->xgmi_sdma_bitmap = (1ULL << get_num_xgmi_sdma_queues(dqm)) - 1;
64c7f8cf 880
64c7f8cf
BG
881 return 0;
882}
883
58dcd5bf 884static void uninitialize(struct device_queue_manager *dqm)
64c7f8cf 885{
6f9d54fd
OG
886 int i;
887
32fa8219 888 WARN_ON(dqm->queue_count > 0 || dqm->processes_count > 0);
64c7f8cf
BG
889
890 kfree(dqm->allocated_queues);
6f9d54fd 891 for (i = 0 ; i < KFD_MQD_TYPE_MAX ; i++)
8d5f3552 892 kfree(dqm->mqd_mgrs[i]);
efeaed4d 893 mutex_destroy(&dqm->lock_hidden);
a86aa3ca 894 kfd_gtt_sa_free(dqm->dev, dqm->pipeline_mem);
64c7f8cf
BG
895}
896
897static int start_nocpsch(struct device_queue_manager *dqm)
898{
2249d558 899 init_interrupts(dqm);
552764b6 900 return pm_init(&dqm->packets, dqm);
64c7f8cf
BG
901}
902
903static int stop_nocpsch(struct device_queue_manager *dqm)
904{
552764b6 905 pm_uninit(&dqm->packets);
64c7f8cf
BG
906 return 0;
907}
908
bcea3081 909static int allocate_sdma_queue(struct device_queue_manager *dqm,
e78579aa 910 struct queue *q)
bcea3081
BG
911{
912 int bit;
913
1b4670f6
OZ
914 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
915 if (dqm->sdma_bitmap == 0)
916 return -ENOMEM;
917 bit = __ffs64(dqm->sdma_bitmap);
918 dqm->sdma_bitmap &= ~(1ULL << bit);
919 q->sdma_id = bit;
920 q->properties.sdma_engine_id = q->sdma_id %
921 get_num_sdma_engines(dqm);
922 q->properties.sdma_queue_id = q->sdma_id /
923 get_num_sdma_engines(dqm);
924 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
925 if (dqm->xgmi_sdma_bitmap == 0)
926 return -ENOMEM;
927 bit = __ffs64(dqm->xgmi_sdma_bitmap);
928 dqm->xgmi_sdma_bitmap &= ~(1ULL << bit);
929 q->sdma_id = bit;
930 /* sdma_engine_id is sdma id including
931 * both PCIe-optimized SDMAs and XGMI-
932 * optimized SDMAs. The calculation below
933 * assumes the first N engines are always
934 * PCIe-optimized ones
935 */
936 q->properties.sdma_engine_id = get_num_sdma_engines(dqm) +
937 q->sdma_id % get_num_xgmi_sdma_engines(dqm);
938 q->properties.sdma_queue_id = q->sdma_id /
939 get_num_xgmi_sdma_engines(dqm);
940 }
e78579aa 941
e78579aa
YZ
942 pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);
943 pr_debug("SDMA queue id: %d\n", q->properties.sdma_queue_id);
bcea3081
BG
944
945 return 0;
946}
947
948static void deallocate_sdma_queue(struct device_queue_manager *dqm,
1b4670f6 949 struct queue *q)
bcea3081 950{
1b4670f6
OZ
951 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
952 if (q->sdma_id >= get_num_sdma_queues(dqm))
953 return;
954 dqm->sdma_bitmap |= (1ULL << q->sdma_id);
955 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
956 if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm))
957 return;
958 dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id);
959 }
bcea3081
BG
960}
961
64c7f8cf
BG
962/*
963 * Device Queue Manager implementation for cp scheduler
964 */
965
966static int set_sched_resources(struct device_queue_manager *dqm)
967{
d0b63bb3 968 int i, mec;
64c7f8cf 969 struct scheduling_resources res;
64c7f8cf 970
44008d7a 971 res.vmid_mask = dqm->dev->shared_resources.compute_vmid_bitmap;
d0b63bb3
AR
972
973 res.queue_mask = 0;
974 for (i = 0; i < KGD_MAX_QUEUES; ++i) {
975 mec = (i / dqm->dev->shared_resources.num_queue_per_pipe)
976 / dqm->dev->shared_resources.num_pipe_per_mec;
977
978 if (!test_bit(i, dqm->dev->shared_resources.queue_bitmap))
979 continue;
980
981 /* only acquire queues from the first MEC */
982 if (mec > 0)
983 continue;
984
985 /* This situation may be hit in the future if a new HW
986 * generation exposes more than 64 queues. If so, the
8eabaf54
KR
987 * definition of res.queue_mask needs updating
988 */
1d11ee89 989 if (WARN_ON(i >= (sizeof(res.queue_mask)*8))) {
d0b63bb3
AR
990 pr_err("Invalid queue enabled by amdgpu: %d\n", i);
991 break;
992 }
993
994 res.queue_mask |= (1ull << i);
995 }
64c7f8cf
BG
996 res.gws_mask = res.oac_mask = res.gds_heap_base =
997 res.gds_heap_size = 0;
998
79775b62
KR
999 pr_debug("Scheduling resources:\n"
1000 "vmid mask: 0x%8X\n"
1001 "queue mask: 0x%8llX\n",
64c7f8cf
BG
1002 res.vmid_mask, res.queue_mask);
1003
1004 return pm_send_set_resources(&dqm->packets, &res);
1005}
1006
1007static int initialize_cpsch(struct device_queue_manager *dqm)
1008{
79775b62 1009 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
64c7f8cf 1010
efeaed4d 1011 mutex_init(&dqm->lock_hidden);
64c7f8cf
BG
1012 INIT_LIST_HEAD(&dqm->queues);
1013 dqm->queue_count = dqm->processes_count = 0;
bcea3081 1014 dqm->sdma_queue_count = 0;
1b4670f6 1015 dqm->xgmi_sdma_queue_count = 0;
64c7f8cf 1016 dqm->active_runlist = false;
cb77ee7c 1017 dqm->sdma_bitmap = (1ULL << get_num_sdma_queues(dqm)) - 1;
1b4670f6 1018 dqm->xgmi_sdma_bitmap = (1ULL << get_num_xgmi_sdma_queues(dqm)) - 1;
64c7f8cf 1019
73ea648d
SL
1020 INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
1021
bfd5e378 1022 return 0;
64c7f8cf
BG
1023}
1024
1025static int start_cpsch(struct device_queue_manager *dqm)
1026{
64c7f8cf
BG
1027 int retval;
1028
64c7f8cf
BG
1029 retval = 0;
1030
1031 retval = pm_init(&dqm->packets, dqm);
4eacc26b 1032 if (retval)
64c7f8cf
BG
1033 goto fail_packet_manager_init;
1034
1035 retval = set_sched_resources(dqm);
4eacc26b 1036 if (retval)
64c7f8cf
BG
1037 goto fail_set_sched_resources;
1038
79775b62 1039 pr_debug("Allocating fence memory\n");
64c7f8cf
BG
1040
1041 /* allocate fence memory on the gart */
a86aa3ca
OG
1042 retval = kfd_gtt_sa_allocate(dqm->dev, sizeof(*dqm->fence_addr),
1043 &dqm->fence_mem);
64c7f8cf 1044
4eacc26b 1045 if (retval)
64c7f8cf
BG
1046 goto fail_allocate_vidmem;
1047
1048 dqm->fence_addr = dqm->fence_mem->cpu_ptr;
1049 dqm->fence_gpu_addr = dqm->fence_mem->gpu_addr;
2249d558
AL
1050
1051 init_interrupts(dqm);
1052
efeaed4d 1053 dqm_lock(dqm);
73ea648d
SL
1054 /* clear hang status when driver try to start the hw scheduler */
1055 dqm->is_hws_hang = false;
c4744e24 1056 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
efeaed4d 1057 dqm_unlock(dqm);
64c7f8cf
BG
1058
1059 return 0;
1060fail_allocate_vidmem:
1061fail_set_sched_resources:
1062 pm_uninit(&dqm->packets);
1063fail_packet_manager_init:
1064 return retval;
1065}
1066
1067static int stop_cpsch(struct device_queue_manager *dqm)
1068{
efeaed4d 1069 dqm_lock(dqm);
4465f466 1070 unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0);
efeaed4d 1071 dqm_unlock(dqm);
64c7f8cf 1072
a86aa3ca 1073 kfd_gtt_sa_free(dqm->dev, dqm->fence_mem);
64c7f8cf
BG
1074 pm_uninit(&dqm->packets);
1075
1076 return 0;
1077}
1078
1079static int create_kernel_queue_cpsch(struct device_queue_manager *dqm,
1080 struct kernel_queue *kq,
1081 struct qcm_process_device *qpd)
1082{
efeaed4d 1083 dqm_lock(dqm);
b8cbab04 1084 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
79775b62 1085 pr_warn("Can't create new kernel queue because %d queues were already created\n",
b8cbab04 1086 dqm->total_queue_count);
efeaed4d 1087 dqm_unlock(dqm);
b8cbab04
OG
1088 return -EPERM;
1089 }
1090
1091 /*
1092 * Unconditionally increment this counter, regardless of the queue's
1093 * type or whether the queue is active.
1094 */
1095 dqm->total_queue_count++;
1096 pr_debug("Total of %d queues are accountable so far\n",
1097 dqm->total_queue_count);
1098
64c7f8cf
BG
1099 list_add(&kq->list, &qpd->priv_queue_list);
1100 dqm->queue_count++;
1101 qpd->is_debug = true;
c4744e24 1102 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
efeaed4d 1103 dqm_unlock(dqm);
64c7f8cf
BG
1104
1105 return 0;
1106}
1107
1108static void destroy_kernel_queue_cpsch(struct device_queue_manager *dqm,
1109 struct kernel_queue *kq,
1110 struct qcm_process_device *qpd)
1111{
efeaed4d 1112 dqm_lock(dqm);
64c7f8cf
BG
1113 list_del(&kq->list);
1114 dqm->queue_count--;
1115 qpd->is_debug = false;
c4744e24 1116 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0);
b8cbab04
OG
1117 /*
1118 * Unconditionally decrement this counter, regardless of the queue's
1119 * type.
1120 */
8b58f261 1121 dqm->total_queue_count--;
b8cbab04
OG
1122 pr_debug("Total of %d queues are accountable so far\n",
1123 dqm->total_queue_count);
efeaed4d 1124 dqm_unlock(dqm);
64c7f8cf
BG
1125}
1126
1127static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
b46cb7d7 1128 struct qcm_process_device *qpd)
64c7f8cf
BG
1129{
1130 int retval;
8d5f3552 1131 struct mqd_manager *mqd_mgr;
64c7f8cf 1132
b8cbab04 1133 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
79775b62 1134 pr_warn("Can't create new usermode queue because %d queues were already created\n",
b8cbab04 1135 dqm->total_queue_count);
70d488fb
OZ
1136 retval = -EPERM;
1137 goto out;
b8cbab04
OG
1138 }
1139
1b4670f6
OZ
1140 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1141 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
e78579aa 1142 retval = allocate_sdma_queue(dqm, q);
894a8293 1143 if (retval)
70d488fb 1144 goto out;
e139cd2a 1145 }
ef568db7
FK
1146
1147 retval = allocate_doorbell(qpd, q);
1148 if (retval)
1149 goto out_deallocate_sdma_queue;
1150
70d488fb
OZ
1151 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1152 q->properties.type)];
26103436 1153 /*
bb2d2128
FK
1154 * Eviction state logic: mark all queues as evicted, even ones
1155 * not currently active. Restoring inactive queues later only
1156 * updates the is_evicted flag but is a no-op otherwise.
26103436 1157 */
bb2d2128 1158 q->properties.is_evicted = !!qpd->evicted;
eec0b4cf
OZ
1159 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1160 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1161 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
373d7080
FK
1162 q->properties.tba_addr = qpd->tba_addr;
1163 q->properties.tma_addr = qpd->tma_addr;
70d488fb
OZ
1164 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
1165 if (!q->mqd_mem_obj) {
1166 retval = -ENOMEM;
1167 goto out_deallocate_doorbell;
1168 }
8636e53c
OZ
1169 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
1170 &q->gart_mqd_addr, &q->properties);
70d488fb 1171 dqm_lock(dqm);
89cd9d23 1172
64c7f8cf 1173 list_add(&q->list, &qpd->queues_list);
bc920fd4 1174 qpd->queue_count++;
64c7f8cf
BG
1175 if (q->properties.is_active) {
1176 dqm->queue_count++;
c4744e24
YZ
1177 retval = execute_queues_cpsch(dqm,
1178 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
64c7f8cf
BG
1179 }
1180
bcea3081 1181 if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
8eabaf54 1182 dqm->sdma_queue_count++;
1b4670f6
OZ
1183 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1184 dqm->xgmi_sdma_queue_count++;
b8cbab04
OG
1185 /*
1186 * Unconditionally increment this counter, regardless of the queue's
1187 * type or whether the queue is active.
1188 */
1189 dqm->total_queue_count++;
1190
1191 pr_debug("Total of %d queues are accountable so far\n",
1192 dqm->total_queue_count);
1193
efeaed4d 1194 dqm_unlock(dqm);
72a01d23
FK
1195 return retval;
1196
70d488fb
OZ
1197out_deallocate_doorbell:
1198 deallocate_doorbell(qpd, q);
72a01d23 1199out_deallocate_sdma_queue:
1b4670f6
OZ
1200 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1201 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1202 deallocate_sdma_queue(dqm, q);
70d488fb 1203out:
64c7f8cf
BG
1204 return retval;
1205}
1206
788bf83d 1207int amdkfd_fence_wait_timeout(unsigned int *fence_addr,
d80d19bd 1208 unsigned int fence_value,
8c72c3d7 1209 unsigned int timeout_ms)
64c7f8cf 1210{
8c72c3d7 1211 unsigned long end_jiffies = msecs_to_jiffies(timeout_ms) + jiffies;
64c7f8cf
BG
1212
1213 while (*fence_addr != fence_value) {
8c72c3d7 1214 if (time_after(jiffies, end_jiffies)) {
79775b62 1215 pr_err("qcm fence wait loop timeout expired\n");
0e9a860c
YZ
1216 /* In HWS case, this is used to halt the driver thread
1217 * in order not to mess up CP states before doing
1218 * scandumps for FW debugging.
1219 */
1220 while (halt_if_hws_hang)
1221 schedule();
1222
64c7f8cf
BG
1223 return -ETIME;
1224 }
99331a51 1225 schedule();
64c7f8cf
BG
1226 }
1227
1228 return 0;
1229}
1230
065e4bdf 1231static int unmap_sdma_queues(struct device_queue_manager *dqm)
bcea3081 1232{
065e4bdf
OZ
1233 int i, retval = 0;
1234
1b4670f6
OZ
1235 for (i = 0; i < dqm->dev->device_info->num_sdma_engines +
1236 dqm->dev->device_info->num_xgmi_sdma_engines; i++) {
065e4bdf
OZ
1237 retval = pm_send_unmap_queue(&dqm->packets, KFD_QUEUE_TYPE_SDMA,
1238 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, false, i);
1239 if (retval)
1240 return retval;
1241 }
1242 return retval;
bcea3081
BG
1243}
1244
60a00956
FK
1245/* dqm->lock mutex has to be locked before calling this function */
1246static int map_queues_cpsch(struct device_queue_manager *dqm)
1247{
1248 int retval;
1249
1250 if (dqm->queue_count <= 0 || dqm->processes_count <= 0)
1251 return 0;
1252
1253 if (dqm->active_runlist)
1254 return 0;
1255
1256 retval = pm_send_runlist(&dqm->packets, &dqm->queues);
1257 if (retval) {
1258 pr_err("failed to execute runlist\n");
1259 return retval;
1260 }
1261 dqm->active_runlist = true;
1262
1263 return retval;
1264}
1265
ac30c783 1266/* dqm->lock mutex has to be locked before calling this function */
7da2bcf8 1267static int unmap_queues_cpsch(struct device_queue_manager *dqm,
4465f466
YZ
1268 enum kfd_unmap_queues_filter filter,
1269 uint32_t filter_param)
64c7f8cf 1270{
9fd3f1bf 1271 int retval = 0;
64c7f8cf 1272
73ea648d
SL
1273 if (dqm->is_hws_hang)
1274 return -EIO;
991ca8ee 1275 if (!dqm->active_runlist)
ac30c783 1276 return retval;
bcea3081 1277
1b4670f6
OZ
1278 pr_debug("Before destroying queues, sdma queue count is : %u, xgmi sdma queue count is : %u\n",
1279 dqm->sdma_queue_count, dqm->xgmi_sdma_queue_count);
bcea3081 1280
1b4670f6 1281 if (dqm->sdma_queue_count > 0 || dqm->xgmi_sdma_queue_count)
065e4bdf 1282 unmap_sdma_queues(dqm);
bcea3081 1283
64c7f8cf 1284 retval = pm_send_unmap_queue(&dqm->packets, KFD_QUEUE_TYPE_COMPUTE,
4465f466 1285 filter, filter_param, false, 0);
4eacc26b 1286 if (retval)
ac30c783 1287 return retval;
64c7f8cf
BG
1288
1289 *dqm->fence_addr = KFD_FENCE_INIT;
1290 pm_send_query_status(&dqm->packets, dqm->fence_gpu_addr,
1291 KFD_FENCE_COMPLETED);
1292 /* should be timed out */
c3447e81 1293 retval = amdkfd_fence_wait_timeout(dqm->fence_addr, KFD_FENCE_COMPLETED,
64c7f8cf 1294 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS);
9fd3f1bf 1295 if (retval)
ac30c783 1296 return retval;
9fd3f1bf 1297
64c7f8cf
BG
1298 pm_release_ib(&dqm->packets);
1299 dqm->active_runlist = false;
1300
64c7f8cf
BG
1301 return retval;
1302}
1303
ac30c783 1304/* dqm->lock mutex has to be locked before calling this function */
c4744e24
YZ
1305static int execute_queues_cpsch(struct device_queue_manager *dqm,
1306 enum kfd_unmap_queues_filter filter,
1307 uint32_t filter_param)
64c7f8cf
BG
1308{
1309 int retval;
1310
73ea648d
SL
1311 if (dqm->is_hws_hang)
1312 return -EIO;
c4744e24 1313 retval = unmap_queues_cpsch(dqm, filter, filter_param);
4eacc26b 1314 if (retval) {
c4744e24 1315 pr_err("The cp might be in an unrecoverable state due to an unsuccessful queues preemption\n");
73ea648d
SL
1316 dqm->is_hws_hang = true;
1317 schedule_work(&dqm->hw_exception_work);
ac30c783 1318 return retval;
64c7f8cf
BG
1319 }
1320
60a00956 1321 return map_queues_cpsch(dqm);
64c7f8cf
BG
1322}
1323
1324static int destroy_queue_cpsch(struct device_queue_manager *dqm,
1325 struct qcm_process_device *qpd,
1326 struct queue *q)
1327{
1328 int retval;
8d5f3552 1329 struct mqd_manager *mqd_mgr;
992839ad 1330
64c7f8cf
BG
1331 retval = 0;
1332
1333 /* remove queue from list to prevent rescheduling after preemption */
efeaed4d 1334 dqm_lock(dqm);
992839ad
YS
1335
1336 if (qpd->is_debug) {
1337 /*
1338 * error, currently we do not allow to destroy a queue
1339 * of a currently debugged process
1340 */
1341 retval = -EBUSY;
1342 goto failed_try_destroy_debugged_queue;
1343
1344 }
1345
fdfa090b
OZ
1346 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1347 q->properties.type)];
64c7f8cf 1348
ef568db7
FK
1349 deallocate_doorbell(qpd, q);
1350
e139cd2a 1351 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
bcea3081 1352 dqm->sdma_queue_count--;
1b4670f6
OZ
1353 deallocate_sdma_queue(dqm, q);
1354 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1355 dqm->xgmi_sdma_queue_count--;
1356 deallocate_sdma_queue(dqm, q);
e139cd2a 1357 }
bcea3081 1358
64c7f8cf 1359 list_del(&q->list);
bc920fd4 1360 qpd->queue_count--;
40a526dc 1361 if (q->properties.is_active) {
b6819cec 1362 dqm->queue_count--;
40a526dc 1363 retval = execute_queues_cpsch(dqm,
9fd3f1bf 1364 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
40a526dc
YZ
1365 if (retval == -ETIME)
1366 qpd->reset_wavefronts = true;
1367 }
64c7f8cf 1368
b8cbab04
OG
1369 /*
1370 * Unconditionally decrement this counter, regardless of the queue's
1371 * type
1372 */
1373 dqm->total_queue_count--;
1374 pr_debug("Total of %d queues are accountable so far\n",
1375 dqm->total_queue_count);
64c7f8cf 1376
efeaed4d 1377 dqm_unlock(dqm);
64c7f8cf 1378
8636e53c
OZ
1379 /* Do free_mqd after dqm_unlock(dqm) to avoid circular locking */
1380 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
89cd9d23 1381
9e827224 1382 return retval;
64c7f8cf 1383
992839ad
YS
1384failed_try_destroy_debugged_queue:
1385
efeaed4d 1386 dqm_unlock(dqm);
64c7f8cf
BG
1387 return retval;
1388}
1389
1390/*
1391 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
1392 * stay in user mode.
1393 */
1394#define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
1395/* APE1 limit is inclusive and 64K aligned. */
1396#define APE1_LIMIT_ALIGNMENT 0xFFFF
1397
1398static bool set_cache_memory_policy(struct device_queue_manager *dqm,
1399 struct qcm_process_device *qpd,
1400 enum cache_policy default_policy,
1401 enum cache_policy alternate_policy,
1402 void __user *alternate_aperture_base,
1403 uint64_t alternate_aperture_size)
1404{
bed4f110
FK
1405 bool retval = true;
1406
1407 if (!dqm->asic_ops.set_cache_memory_policy)
1408 return retval;
64c7f8cf 1409
efeaed4d 1410 dqm_lock(dqm);
64c7f8cf
BG
1411
1412 if (alternate_aperture_size == 0) {
1413 /* base > limit disables APE1 */
1414 qpd->sh_mem_ape1_base = 1;
1415 qpd->sh_mem_ape1_limit = 0;
1416 } else {
1417 /*
1418 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
1419 * SH_MEM_APE1_BASE[31:0], 0x0000 }
1420 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
1421 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
1422 * Verify that the base and size parameters can be
1423 * represented in this format and convert them.
1424 * Additionally restrict APE1 to user-mode addresses.
1425 */
1426
1427 uint64_t base = (uintptr_t)alternate_aperture_base;
1428 uint64_t limit = base + alternate_aperture_size - 1;
1429
ab7c1648
KR
1430 if (limit <= base || (base & APE1_FIXED_BITS_MASK) != 0 ||
1431 (limit & APE1_FIXED_BITS_MASK) != APE1_LIMIT_ALIGNMENT) {
1432 retval = false;
64c7f8cf 1433 goto out;
ab7c1648 1434 }
64c7f8cf
BG
1435
1436 qpd->sh_mem_ape1_base = base >> 16;
1437 qpd->sh_mem_ape1_limit = limit >> 16;
1438 }
1439
bfd5e378 1440 retval = dqm->asic_ops.set_cache_memory_policy(
a22fc854
BG
1441 dqm,
1442 qpd,
1443 default_policy,
1444 alternate_policy,
1445 alternate_aperture_base,
1446 alternate_aperture_size);
64c7f8cf 1447
d146c5a7 1448 if ((dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) && (qpd->vmid != 0))
64c7f8cf
BG
1449 program_sh_mem_settings(dqm, qpd);
1450
79775b62 1451 pr_debug("sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
64c7f8cf
BG
1452 qpd->sh_mem_config, qpd->sh_mem_ape1_base,
1453 qpd->sh_mem_ape1_limit);
1454
64c7f8cf 1455out:
efeaed4d 1456 dqm_unlock(dqm);
ab7c1648 1457 return retval;
64c7f8cf
BG
1458}
1459
d7b9bd22
FK
1460static int set_trap_handler(struct device_queue_manager *dqm,
1461 struct qcm_process_device *qpd,
1462 uint64_t tba_addr,
1463 uint64_t tma_addr)
1464{
1465 uint64_t *tma;
1466
1467 if (dqm->dev->cwsr_enabled) {
1468 /* Jump from CWSR trap handler to user trap */
1469 tma = (uint64_t *)(qpd->cwsr_kaddr + KFD_CWSR_TMA_OFFSET);
1470 tma[0] = tba_addr;
1471 tma[1] = tma_addr;
1472 } else {
1473 qpd->tba_addr = tba_addr;
1474 qpd->tma_addr = tma_addr;
1475 }
1476
1477 return 0;
1478}
1479
9fd3f1bf
FK
1480static int process_termination_nocpsch(struct device_queue_manager *dqm,
1481 struct qcm_process_device *qpd)
1482{
1483 struct queue *q, *next;
1484 struct device_process_node *cur, *next_dpn;
1485 int retval = 0;
32cce8bc 1486 bool found = false;
9fd3f1bf 1487
efeaed4d 1488 dqm_lock(dqm);
9fd3f1bf
FK
1489
1490 /* Clear all user mode queues */
1491 list_for_each_entry_safe(q, next, &qpd->queues_list, list) {
1492 int ret;
1493
1494 ret = destroy_queue_nocpsch_locked(dqm, qpd, q);
1495 if (ret)
1496 retval = ret;
1497 }
1498
1499 /* Unregister process */
1500 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
1501 if (qpd == cur->qpd) {
1502 list_del(&cur->list);
1503 kfree(cur);
1504 dqm->processes_count--;
32cce8bc 1505 found = true;
9fd3f1bf
FK
1506 break;
1507 }
1508 }
1509
efeaed4d 1510 dqm_unlock(dqm);
32cce8bc
FK
1511
1512 /* Outside the DQM lock because under the DQM lock we can't do
1513 * reclaim or take other locks that others hold while reclaiming.
1514 */
1515 if (found)
1516 kfd_dec_compute_active(dqm->dev);
1517
9fd3f1bf
FK
1518 return retval;
1519}
1520
5df099e8
JC
1521static int get_wave_state(struct device_queue_manager *dqm,
1522 struct queue *q,
1523 void __user *ctl_stack,
1524 u32 *ctl_stack_used_size,
1525 u32 *save_area_used_size)
1526{
4e6c6fc1 1527 struct mqd_manager *mqd_mgr;
5df099e8
JC
1528 int r;
1529
1530 dqm_lock(dqm);
1531
1532 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE ||
1533 q->properties.is_active || !q->device->cwsr_enabled) {
1534 r = -EINVAL;
1535 goto dqm_unlock;
1536 }
1537
fdfa090b 1538 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_COMPUTE];
5df099e8 1539
4e6c6fc1 1540 if (!mqd_mgr->get_wave_state) {
5df099e8
JC
1541 r = -EINVAL;
1542 goto dqm_unlock;
1543 }
1544
4e6c6fc1
YZ
1545 r = mqd_mgr->get_wave_state(mqd_mgr, q->mqd, ctl_stack,
1546 ctl_stack_used_size, save_area_used_size);
5df099e8
JC
1547
1548dqm_unlock:
1549 dqm_unlock(dqm);
1550 return r;
1551}
9fd3f1bf
FK
1552
1553static int process_termination_cpsch(struct device_queue_manager *dqm,
1554 struct qcm_process_device *qpd)
1555{
1556 int retval;
1557 struct queue *q, *next;
1558 struct kernel_queue *kq, *kq_next;
8d5f3552 1559 struct mqd_manager *mqd_mgr;
9fd3f1bf
FK
1560 struct device_process_node *cur, *next_dpn;
1561 enum kfd_unmap_queues_filter filter =
1562 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES;
32cce8bc 1563 bool found = false;
9fd3f1bf
FK
1564
1565 retval = 0;
1566
efeaed4d 1567 dqm_lock(dqm);
9fd3f1bf
FK
1568
1569 /* Clean all kernel queues */
1570 list_for_each_entry_safe(kq, kq_next, &qpd->priv_queue_list, list) {
1571 list_del(&kq->list);
1572 dqm->queue_count--;
1573 qpd->is_debug = false;
1574 dqm->total_queue_count--;
1575 filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
1576 }
1577
1578 /* Clear all user mode queues */
1579 list_for_each_entry(q, &qpd->queues_list, list) {
72a01d23 1580 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
9fd3f1bf 1581 dqm->sdma_queue_count--;
1b4670f6
OZ
1582 deallocate_sdma_queue(dqm, q);
1583 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1584 dqm->xgmi_sdma_queue_count--;
1585 deallocate_sdma_queue(dqm, q);
72a01d23 1586 }
9fd3f1bf
FK
1587
1588 if (q->properties.is_active)
1589 dqm->queue_count--;
1590
1591 dqm->total_queue_count--;
1592 }
1593
1594 /* Unregister process */
1595 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
1596 if (qpd == cur->qpd) {
1597 list_del(&cur->list);
1598 kfree(cur);
1599 dqm->processes_count--;
32cce8bc 1600 found = true;
9fd3f1bf
FK
1601 break;
1602 }
1603 }
1604
1605 retval = execute_queues_cpsch(dqm, filter, 0);
73ea648d 1606 if ((!dqm->is_hws_hang) && (retval || qpd->reset_wavefronts)) {
9fd3f1bf
FK
1607 pr_warn("Resetting wave fronts (cpsch) on dev %p\n", dqm->dev);
1608 dbgdev_wave_reset_wavefronts(dqm->dev, qpd->pqm->process);
1609 qpd->reset_wavefronts = false;
1610 }
1611
89cd9d23
PY
1612 dqm_unlock(dqm);
1613
32cce8bc
FK
1614 /* Outside the DQM lock because under the DQM lock we can't do
1615 * reclaim or take other locks that others hold while reclaiming.
1616 */
1617 if (found)
1618 kfd_dec_compute_active(dqm->dev);
1619
89cd9d23 1620 /* Lastly, free mqd resources.
8636e53c 1621 * Do free_mqd() after dqm_unlock to avoid circular locking.
89cd9d23 1622 */
9fd3f1bf 1623 list_for_each_entry_safe(q, next, &qpd->queues_list, list) {
fdfa090b
OZ
1624 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1625 q->properties.type)];
9fd3f1bf 1626 list_del(&q->list);
bc920fd4 1627 qpd->queue_count--;
8636e53c 1628 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
9fd3f1bf
FK
1629 }
1630
9fd3f1bf
FK
1631 return retval;
1632}
1633
fdfa090b
OZ
1634static int init_mqd_managers(struct device_queue_manager *dqm)
1635{
1636 int i, j;
1637 struct mqd_manager *mqd_mgr;
1638
1639 for (i = 0; i < KFD_MQD_TYPE_MAX; i++) {
1640 mqd_mgr = dqm->asic_ops.mqd_manager_init(i, dqm->dev);
1641 if (!mqd_mgr) {
1642 pr_err("mqd manager [%d] initialization failed\n", i);
1643 goto out_free;
1644 }
1645 dqm->mqd_mgrs[i] = mqd_mgr;
1646 }
1647
1648 return 0;
1649
1650out_free:
1651 for (j = 0; j < i; j++) {
1652 kfree(dqm->mqd_mgrs[j]);
1653 dqm->mqd_mgrs[j] = NULL;
1654 }
1655
1656 return -ENOMEM;
1657}
11614c36
OZ
1658
1659/* Allocate one hiq mqd (HWS) and all SDMA mqd in a continuous trunk*/
1660static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
1661{
1662 int retval;
1663 struct kfd_dev *dev = dqm->dev;
1664 struct kfd_mem_obj *mem_obj = &dqm->hiq_sdma_mqd;
1665 uint32_t size = dqm->mqd_mgrs[KFD_MQD_TYPE_SDMA]->mqd_size *
1666 dev->device_info->num_sdma_engines *
1667 dev->device_info->num_sdma_queues_per_engine +
1668 dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size;
1669
1670 retval = amdgpu_amdkfd_alloc_gtt_mem(dev->kgd, size,
1671 &(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
1672 (void *)&(mem_obj->cpu_ptr), true);
1673
1674 return retval;
1675}
1676
64c7f8cf
BG
1677struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev)
1678{
1679 struct device_queue_manager *dqm;
1680
79775b62 1681 pr_debug("Loading device queue manager\n");
a22fc854 1682
dbf56ab1 1683 dqm = kzalloc(sizeof(*dqm), GFP_KERNEL);
64c7f8cf
BG
1684 if (!dqm)
1685 return NULL;
1686
d146c5a7
FK
1687 switch (dev->device_info->asic_family) {
1688 /* HWS is not available on Hawaii. */
1689 case CHIP_HAWAII:
1690 /* HWS depends on CWSR for timely dequeue. CWSR is not
1691 * available on Tonga.
1692 *
1693 * FIXME: This argument also applies to Kaveri.
1694 */
1695 case CHIP_TONGA:
1696 dqm->sched_policy = KFD_SCHED_POLICY_NO_HWS;
1697 break;
1698 default:
1699 dqm->sched_policy = sched_policy;
1700 break;
1701 }
1702
64c7f8cf 1703 dqm->dev = dev;
d146c5a7 1704 switch (dqm->sched_policy) {
64c7f8cf
BG
1705 case KFD_SCHED_POLICY_HWS:
1706 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION:
1707 /* initialize dqm for cp scheduling */
45c9a5e4
OG
1708 dqm->ops.create_queue = create_queue_cpsch;
1709 dqm->ops.initialize = initialize_cpsch;
1710 dqm->ops.start = start_cpsch;
1711 dqm->ops.stop = stop_cpsch;
1712 dqm->ops.destroy_queue = destroy_queue_cpsch;
1713 dqm->ops.update_queue = update_queue;
58dcd5bf
YZ
1714 dqm->ops.register_process = register_process;
1715 dqm->ops.unregister_process = unregister_process;
1716 dqm->ops.uninitialize = uninitialize;
45c9a5e4
OG
1717 dqm->ops.create_kernel_queue = create_kernel_queue_cpsch;
1718 dqm->ops.destroy_kernel_queue = destroy_kernel_queue_cpsch;
1719 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
d7b9bd22 1720 dqm->ops.set_trap_handler = set_trap_handler;
9fd3f1bf 1721 dqm->ops.process_termination = process_termination_cpsch;
26103436
FK
1722 dqm->ops.evict_process_queues = evict_process_queues_cpsch;
1723 dqm->ops.restore_process_queues = restore_process_queues_cpsch;
5df099e8 1724 dqm->ops.get_wave_state = get_wave_state;
64c7f8cf
BG
1725 break;
1726 case KFD_SCHED_POLICY_NO_HWS:
1727 /* initialize dqm for no cp scheduling */
45c9a5e4
OG
1728 dqm->ops.start = start_nocpsch;
1729 dqm->ops.stop = stop_nocpsch;
1730 dqm->ops.create_queue = create_queue_nocpsch;
1731 dqm->ops.destroy_queue = destroy_queue_nocpsch;
1732 dqm->ops.update_queue = update_queue;
58dcd5bf
YZ
1733 dqm->ops.register_process = register_process;
1734 dqm->ops.unregister_process = unregister_process;
45c9a5e4 1735 dqm->ops.initialize = initialize_nocpsch;
58dcd5bf 1736 dqm->ops.uninitialize = uninitialize;
45c9a5e4 1737 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
d7b9bd22 1738 dqm->ops.set_trap_handler = set_trap_handler;
9fd3f1bf 1739 dqm->ops.process_termination = process_termination_nocpsch;
26103436
FK
1740 dqm->ops.evict_process_queues = evict_process_queues_nocpsch;
1741 dqm->ops.restore_process_queues =
1742 restore_process_queues_nocpsch;
5df099e8 1743 dqm->ops.get_wave_state = get_wave_state;
64c7f8cf
BG
1744 break;
1745 default:
d146c5a7 1746 pr_err("Invalid scheduling policy %d\n", dqm->sched_policy);
32fa8219 1747 goto out_free;
64c7f8cf
BG
1748 }
1749
a22fc854
BG
1750 switch (dev->device_info->asic_family) {
1751 case CHIP_CARRIZO:
bfd5e378 1752 device_queue_manager_init_vi(&dqm->asic_ops);
300dec95
OG
1753 break;
1754
a22fc854 1755 case CHIP_KAVERI:
bfd5e378 1756 device_queue_manager_init_cik(&dqm->asic_ops);
300dec95 1757 break;
97672cbe
FK
1758
1759 case CHIP_HAWAII:
1760 device_queue_manager_init_cik_hawaii(&dqm->asic_ops);
1761 break;
1762
1763 case CHIP_TONGA:
1764 case CHIP_FIJI:
1765 case CHIP_POLARIS10:
1766 case CHIP_POLARIS11:
846a44d7 1767 case CHIP_POLARIS12:
ed81cd6e 1768 case CHIP_VEGAM:
97672cbe
FK
1769 device_queue_manager_init_vi_tonga(&dqm->asic_ops);
1770 break;
bed4f110
FK
1771
1772 case CHIP_VEGA10:
846a44d7 1773 case CHIP_VEGA12:
22a3a294 1774 case CHIP_VEGA20:
bed4f110
FK
1775 case CHIP_RAVEN:
1776 device_queue_manager_init_v9(&dqm->asic_ops);
1777 break;
e596b903
YZ
1778 default:
1779 WARN(1, "Unexpected ASIC family %u",
1780 dev->device_info->asic_family);
1781 goto out_free;
a22fc854
BG
1782 }
1783
fdfa090b
OZ
1784 if (init_mqd_managers(dqm))
1785 goto out_free;
1786
11614c36
OZ
1787 if (allocate_hiq_sdma_mqd(dqm)) {
1788 pr_err("Failed to allocate hiq sdma mqd trunk buffer\n");
1789 goto out_free;
1790 }
1791
32fa8219
FK
1792 if (!dqm->ops.initialize(dqm))
1793 return dqm;
64c7f8cf 1794
32fa8219
FK
1795out_free:
1796 kfree(dqm);
1797 return NULL;
64c7f8cf
BG
1798}
1799
11614c36
OZ
1800void deallocate_hiq_sdma_mqd(struct kfd_dev *dev, struct kfd_mem_obj *mqd)
1801{
1802 WARN(!mqd, "No hiq sdma mqd trunk to free");
1803
1804 amdgpu_amdkfd_free_gtt_mem(dev->kgd, mqd->gtt_mem);
1805}
1806
64c7f8cf
BG
1807void device_queue_manager_uninit(struct device_queue_manager *dqm)
1808{
45c9a5e4 1809 dqm->ops.uninitialize(dqm);
11614c36 1810 deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd);
64c7f8cf
BG
1811 kfree(dqm);
1812}
851a645e 1813
2640c3fa 1814int kfd_process_vm_fault(struct device_queue_manager *dqm,
1815 unsigned int pasid)
1816{
1817 struct kfd_process_device *pdd;
1818 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
1819 int ret = 0;
1820
1821 if (!p)
1822 return -EINVAL;
1823 pdd = kfd_get_process_device_data(dqm->dev, p);
1824 if (pdd)
1825 ret = dqm->ops.evict_process_queues(dqm, &pdd->qpd);
1826 kfd_unref_process(p);
1827
1828 return ret;
1829}
1830
73ea648d
SL
1831static void kfd_process_hw_exception(struct work_struct *work)
1832{
1833 struct device_queue_manager *dqm = container_of(work,
1834 struct device_queue_manager, hw_exception_work);
5b87245f 1835 amdgpu_amdkfd_gpu_reset(dqm->dev->kgd);
73ea648d
SL
1836}
1837
851a645e
FK
1838#if defined(CONFIG_DEBUG_FS)
1839
1840static void seq_reg_dump(struct seq_file *m,
1841 uint32_t (*dump)[2], uint32_t n_regs)
1842{
1843 uint32_t i, count;
1844
1845 for (i = 0, count = 0; i < n_regs; i++) {
1846 if (count == 0 ||
1847 dump[i-1][0] + sizeof(uint32_t) != dump[i][0]) {
1848 seq_printf(m, "%s %08x: %08x",
1849 i ? "\n" : "",
1850 dump[i][0], dump[i][1]);
1851 count = 7;
1852 } else {
1853 seq_printf(m, " %08x", dump[i][1]);
1854 count--;
1855 }
1856 }
1857
1858 seq_puts(m, "\n");
1859}
1860
1861int dqm_debugfs_hqds(struct seq_file *m, void *data)
1862{
1863 struct device_queue_manager *dqm = data;
1864 uint32_t (*dump)[2], n_regs;
1865 int pipe, queue;
1866 int r = 0;
1867
24f48a42
OZ
1868 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->kgd,
1869 KFD_CIK_HIQ_PIPE, KFD_CIK_HIQ_QUEUE, &dump, &n_regs);
1870 if (!r) {
1871 seq_printf(m, " HIQ on MEC %d Pipe %d Queue %d\n",
1872 KFD_CIK_HIQ_PIPE/get_pipes_per_mec(dqm)+1,
1873 KFD_CIK_HIQ_PIPE%get_pipes_per_mec(dqm),
1874 KFD_CIK_HIQ_QUEUE);
1875 seq_reg_dump(m, dump, n_regs);
1876
1877 kfree(dump);
1878 }
1879
851a645e
FK
1880 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
1881 int pipe_offset = pipe * get_queues_per_pipe(dqm);
1882
1883 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) {
1884 if (!test_bit(pipe_offset + queue,
1885 dqm->dev->shared_resources.queue_bitmap))
1886 continue;
1887
1888 r = dqm->dev->kfd2kgd->hqd_dump(
1889 dqm->dev->kgd, pipe, queue, &dump, &n_regs);
1890 if (r)
1891 break;
1892
1893 seq_printf(m, " CP Pipe %d, Queue %d\n",
1894 pipe, queue);
1895 seq_reg_dump(m, dump, n_regs);
1896
1897 kfree(dump);
1898 }
1899 }
1900
98bb9222 1901 for (pipe = 0; pipe < get_num_sdma_engines(dqm); pipe++) {
d5094189
SL
1902 for (queue = 0;
1903 queue < dqm->dev->device_info->num_sdma_queues_per_engine;
1904 queue++) {
851a645e
FK
1905 r = dqm->dev->kfd2kgd->hqd_sdma_dump(
1906 dqm->dev->kgd, pipe, queue, &dump, &n_regs);
1907 if (r)
1908 break;
1909
1910 seq_printf(m, " SDMA Engine %d, RLC %d\n",
1911 pipe, queue);
1912 seq_reg_dump(m, dump, n_regs);
1913
1914 kfree(dump);
1915 }
1916 }
1917
1918 return r;
1919}
1920
a29ec470
SL
1921int dqm_debugfs_execute_queues(struct device_queue_manager *dqm)
1922{
1923 int r = 0;
1924
1925 dqm_lock(dqm);
1926 dqm->active_runlist = true;
1927 r = execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0);
1928 dqm_unlock(dqm);
1929
1930 return r;
1931}
1932
851a645e 1933#endif