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