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