1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 #include <linux/slab.h>
26 #include <linux/list.h>
27 #include "kfd_device_queue_manager.h"
28 #include "kfd_priv.h"
29 #include "kfd_kernel_queue.h"
30 #include "amdgpu_amdkfd.h"
31 #include "amdgpu_reset.h"
32
get_queue_by_qid(struct process_queue_manager * pqm,unsigned int qid)33 static inline struct process_queue_node *get_queue_by_qid(
34 struct process_queue_manager *pqm, unsigned int qid)
35 {
36 struct process_queue_node *pqn;
37
38 list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
39 if ((pqn->q && pqn->q->properties.queue_id == qid) ||
40 (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
41 return pqn;
42 }
43
44 return NULL;
45 }
46
assign_queue_slot_by_qid(struct process_queue_manager * pqm,unsigned int qid)47 static int assign_queue_slot_by_qid(struct process_queue_manager *pqm,
48 unsigned int qid)
49 {
50 if (qid >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
51 return -EINVAL;
52
53 if (__test_and_set_bit(qid, pqm->queue_slot_bitmap)) {
54 pr_err("Cannot create new queue because requested qid(%u) is in use\n", qid);
55 return -ENOSPC;
56 }
57
58 return 0;
59 }
60
find_available_queue_slot(struct process_queue_manager * pqm,unsigned int * qid)61 static int find_available_queue_slot(struct process_queue_manager *pqm,
62 unsigned int *qid)
63 {
64 unsigned long found;
65
66 found = find_first_zero_bit(pqm->queue_slot_bitmap,
67 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
68
69 pr_debug("The new slot id %lu\n", found);
70
71 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
72 pr_info("Cannot open more queues for process with pasid 0x%x\n",
73 pqm->process->pasid);
74 return -ENOMEM;
75 }
76
77 set_bit(found, pqm->queue_slot_bitmap);
78 *qid = found;
79
80 return 0;
81 }
82
kfd_process_dequeue_from_device(struct kfd_process_device * pdd)83 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
84 {
85 struct kfd_node *dev = pdd->dev;
86
87 if (pdd->already_dequeued)
88 return;
89
90 dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
91 if (dev->kfd->shared_resources.enable_mes &&
92 down_read_trylock(&dev->adev->reset_domain->sem)) {
93 amdgpu_mes_flush_shader_debugger(dev->adev,
94 pdd->proc_ctx_gpu_addr);
95 up_read(&dev->adev->reset_domain->sem);
96 }
97 pdd->already_dequeued = true;
98 }
99
pqm_set_gws(struct process_queue_manager * pqm,unsigned int qid,void * gws)100 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
101 void *gws)
102 {
103 struct mqd_update_info minfo = {0};
104 struct kfd_node *dev = NULL;
105 struct process_queue_node *pqn;
106 struct kfd_process_device *pdd;
107 struct kgd_mem *mem = NULL;
108 int ret;
109
110 pqn = get_queue_by_qid(pqm, qid);
111 if (!pqn) {
112 pr_err("Queue id does not match any known queue\n");
113 return -EINVAL;
114 }
115
116 if (pqn->q)
117 dev = pqn->q->device;
118 if (WARN_ON(!dev))
119 return -ENODEV;
120
121 pdd = kfd_get_process_device_data(dev, pqm->process);
122 if (!pdd) {
123 pr_err("Process device data doesn't exist\n");
124 return -EINVAL;
125 }
126
127 /* Only allow one queue per process can have GWS assigned */
128 if (gws && pdd->qpd.num_gws)
129 return -EBUSY;
130
131 if (!gws && pdd->qpd.num_gws == 0)
132 return -EINVAL;
133
134 if (KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 3) &&
135 KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 4) &&
136 !dev->kfd->shared_resources.enable_mes) {
137 if (gws)
138 ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info,
139 gws, &mem);
140 else
141 ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info,
142 pqn->q->gws);
143 if (unlikely(ret))
144 return ret;
145 pqn->q->gws = mem;
146 } else {
147 /*
148 * Intentionally set GWS to a non-NULL value
149 * for devices that do not use GWS for global wave
150 * synchronization but require the formality
151 * of setting GWS for cooperative groups.
152 */
153 pqn->q->gws = gws ? ERR_PTR(-ENOMEM) : NULL;
154 }
155
156 pdd->qpd.num_gws = gws ? dev->adev->gds.gws_size : 0;
157 minfo.update_flag = gws ? UPDATE_FLAG_IS_GWS : 0;
158
159 return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
160 pqn->q, &minfo);
161 }
162
kfd_process_dequeue_from_all_devices(struct kfd_process * p)163 void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
164 {
165 int i;
166
167 for (i = 0; i < p->n_pdds; i++)
168 kfd_process_dequeue_from_device(p->pdds[i]);
169 }
170
pqm_init(struct process_queue_manager * pqm,struct kfd_process * p)171 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
172 {
173 INIT_LIST_HEAD(&pqm->queues);
174 pqm->queue_slot_bitmap = bitmap_zalloc(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
175 GFP_KERNEL);
176 if (!pqm->queue_slot_bitmap)
177 return -ENOMEM;
178 pqm->process = p;
179
180 return 0;
181 }
182
pqm_clean_queue_resource(struct process_queue_manager * pqm,struct process_queue_node * pqn)183 static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
184 struct process_queue_node *pqn)
185 {
186 struct kfd_node *dev;
187 struct kfd_process_device *pdd;
188
189 dev = pqn->q->device;
190
191 pdd = kfd_get_process_device_data(dev, pqm->process);
192 if (!pdd) {
193 pr_err("Process device data doesn't exist\n");
194 return;
195 }
196
197 if (pqn->q->gws) {
198 if (KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 3) &&
199 KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 4) &&
200 !dev->kfd->shared_resources.enable_mes)
201 amdgpu_amdkfd_remove_gws_from_process(
202 pqm->process->kgd_process_info, pqn->q->gws);
203 pdd->qpd.num_gws = 0;
204 }
205
206 if (dev->kfd->shared_resources.enable_mes) {
207 amdgpu_amdkfd_free_gtt_mem(dev->adev, &pqn->q->gang_ctx_bo);
208 amdgpu_amdkfd_free_gtt_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
209 }
210 }
211
pqm_uninit(struct process_queue_manager * pqm)212 void pqm_uninit(struct process_queue_manager *pqm)
213 {
214 struct process_queue_node *pqn, *next;
215
216 list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
217 if (pqn->q) {
218 struct kfd_process_device *pdd = kfd_get_process_device_data(pqn->q->device,
219 pqm->process);
220 if (pdd) {
221 kfd_queue_unref_bo_vas(pdd, &pqn->q->properties);
222 kfd_queue_release_buffers(pdd, &pqn->q->properties);
223 } else {
224 WARN_ON(!pdd);
225 }
226 pqm_clean_queue_resource(pqm, pqn);
227 }
228
229 kfd_procfs_del_queue(pqn->q);
230 uninit_queue(pqn->q);
231 list_del(&pqn->process_queue_list);
232 kfree(pqn);
233 }
234
235 bitmap_free(pqm->queue_slot_bitmap);
236 pqm->queue_slot_bitmap = NULL;
237 }
238
init_user_queue(struct process_queue_manager * pqm,struct kfd_node * dev,struct queue ** q,struct queue_properties * q_properties,struct file * f,unsigned int qid)239 static int init_user_queue(struct process_queue_manager *pqm,
240 struct kfd_node *dev, struct queue **q,
241 struct queue_properties *q_properties,
242 struct file *f, unsigned int qid)
243 {
244 int retval;
245
246 /* Doorbell initialized in user space*/
247 q_properties->doorbell_ptr = NULL;
248 q_properties->exception_status = KFD_EC_MASK(EC_QUEUE_NEW);
249
250 /* let DQM handle it*/
251 q_properties->vmid = 0;
252 q_properties->queue_id = qid;
253
254 retval = init_queue(q, q_properties);
255 if (retval != 0)
256 return retval;
257
258 (*q)->device = dev;
259 (*q)->process = pqm->process;
260
261 if (dev->kfd->shared_resources.enable_mes) {
262 retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
263 AMDGPU_MES_GANG_CTX_SIZE,
264 &(*q)->gang_ctx_bo,
265 &(*q)->gang_ctx_gpu_addr,
266 &(*q)->gang_ctx_cpu_ptr,
267 false);
268 if (retval) {
269 pr_err("failed to allocate gang context bo\n");
270 goto cleanup;
271 }
272 memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
273
274 /* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
275 * on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
276 */
277 if (((dev->adev->mes.sched_version & AMDGPU_MES_API_VERSION_MASK)
278 >> AMDGPU_MES_API_VERSION_SHIFT) >= 2) {
279 if (dev->adev != amdgpu_ttm_adev(q_properties->wptr_bo->tbo.bdev)) {
280 pr_err("Queue memory allocated to wrong device\n");
281 retval = -EINVAL;
282 goto free_gang_ctx_bo;
283 }
284
285 retval = amdgpu_amdkfd_map_gtt_bo_to_gart(q_properties->wptr_bo,
286 &(*q)->wptr_bo_gart);
287 if (retval) {
288 pr_err("Failed to map wptr bo to GART\n");
289 goto free_gang_ctx_bo;
290 }
291 }
292 }
293
294 pr_debug("PQM After init queue");
295 return 0;
296
297 free_gang_ctx_bo:
298 amdgpu_amdkfd_free_gtt_mem(dev->adev, (*q)->gang_ctx_bo);
299 cleanup:
300 uninit_queue(*q);
301 *q = NULL;
302 return retval;
303 }
304
pqm_create_queue(struct process_queue_manager * pqm,struct kfd_node * dev,struct file * f,struct queue_properties * properties,unsigned int * qid,const struct kfd_criu_queue_priv_data * q_data,const void * restore_mqd,const void * restore_ctl_stack,uint32_t * p_doorbell_offset_in_process)305 int pqm_create_queue(struct process_queue_manager *pqm,
306 struct kfd_node *dev,
307 struct file *f,
308 struct queue_properties *properties,
309 unsigned int *qid,
310 const struct kfd_criu_queue_priv_data *q_data,
311 const void *restore_mqd,
312 const void *restore_ctl_stack,
313 uint32_t *p_doorbell_offset_in_process)
314 {
315 int retval;
316 struct kfd_process_device *pdd;
317 struct queue *q;
318 struct process_queue_node *pqn;
319 struct kernel_queue *kq;
320 enum kfd_queue_type type = properties->type;
321 unsigned int max_queues = 127; /* HWS limit */
322
323 /*
324 * On GFX 9.4.3, increase the number of queues that
325 * can be created to 255. No HWS limit on GFX 9.4.3.
326 */
327 if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3) ||
328 KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4))
329 max_queues = 255;
330
331 q = NULL;
332 kq = NULL;
333
334 pdd = kfd_get_process_device_data(dev, pqm->process);
335 if (!pdd) {
336 pr_err("Process device data doesn't exist\n");
337 return -1;
338 }
339
340 /*
341 * for debug process, verify that it is within the static queues limit
342 * currently limit is set to half of the total avail HQD slots
343 * If we are just about to create DIQ, the is_debug flag is not set yet
344 * Hence we also check the type as well
345 */
346 if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
347 max_queues = dev->kfd->device_info.max_no_of_hqd/2;
348
349 if (pdd->qpd.queue_count >= max_queues)
350 return -ENOSPC;
351
352 if (q_data) {
353 retval = assign_queue_slot_by_qid(pqm, q_data->q_id);
354 *qid = q_data->q_id;
355 } else
356 retval = find_available_queue_slot(pqm, qid);
357
358 if (retval != 0)
359 return retval;
360
361 if (list_empty(&pdd->qpd.queues_list) &&
362 list_empty(&pdd->qpd.priv_queue_list))
363 dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
364
365 pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
366 if (!pqn) {
367 retval = -ENOMEM;
368 goto err_allocate_pqn;
369 }
370
371 switch (type) {
372 case KFD_QUEUE_TYPE_SDMA:
373 case KFD_QUEUE_TYPE_SDMA_XGMI:
374 case KFD_QUEUE_TYPE_SDMA_BY_ENG_ID:
375 /* SDMA queues are always allocated statically no matter
376 * which scheduler mode is used. We also do not need to
377 * check whether a SDMA queue can be allocated here, because
378 * allocate_sdma_queue() in create_queue() has the
379 * corresponding check logic.
380 */
381 retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
382 if (retval != 0)
383 goto err_create_queue;
384 pqn->q = q;
385 pqn->kq = NULL;
386 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
387 restore_mqd, restore_ctl_stack);
388 print_queue(q);
389 break;
390
391 case KFD_QUEUE_TYPE_COMPUTE:
392 /* check if there is over subscription */
393 if ((dev->dqm->sched_policy ==
394 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
395 ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
396 (dev->dqm->active_queue_count >= get_cp_queues_num(dev->dqm)))) {
397 pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n");
398 retval = -EPERM;
399 goto err_create_queue;
400 }
401
402 retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
403 if (retval != 0)
404 goto err_create_queue;
405 pqn->q = q;
406 pqn->kq = NULL;
407 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
408 restore_mqd, restore_ctl_stack);
409 print_queue(q);
410 break;
411 case KFD_QUEUE_TYPE_DIQ:
412 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
413 if (!kq) {
414 retval = -ENOMEM;
415 goto err_create_queue;
416 }
417 kq->queue->properties.queue_id = *qid;
418 pqn->kq = kq;
419 pqn->q = NULL;
420 retval = kfd_process_drain_interrupts(pdd);
421 if (retval)
422 break;
423
424 retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
425 kq, &pdd->qpd);
426 break;
427 default:
428 WARN(1, "Invalid queue type %d", type);
429 retval = -EINVAL;
430 }
431
432 if (retval != 0) {
433 pr_err("Pasid 0x%x DQM create queue type %d failed. ret %d\n",
434 pqm->process->pasid, type, retval);
435 goto err_create_queue;
436 }
437
438 if (q && p_doorbell_offset_in_process) {
439 /* Return the doorbell offset within the doorbell page
440 * to the caller so it can be passed up to user mode
441 * (in bytes).
442 * relative doorbell index = Absolute doorbell index -
443 * absolute index of first doorbell in the page.
444 */
445 uint32_t first_db_index = amdgpu_doorbell_index_on_bar(pdd->dev->adev,
446 pdd->qpd.proc_doorbells,
447 0,
448 pdd->dev->kfd->device_info.doorbell_size);
449
450 *p_doorbell_offset_in_process = (q->properties.doorbell_off
451 - first_db_index) * sizeof(uint32_t);
452 }
453
454 pr_debug("PQM After DQM create queue\n");
455
456 list_add(&pqn->process_queue_list, &pqm->queues);
457
458 if (q) {
459 pr_debug("PQM done creating queue\n");
460 kfd_procfs_add_queue(q);
461 print_queue_properties(&q->properties);
462 }
463
464 return retval;
465
466 err_create_queue:
467 uninit_queue(q);
468 if (kq)
469 kernel_queue_uninit(kq);
470 kfree(pqn);
471 err_allocate_pqn:
472 /* check if queues list is empty unregister process from device */
473 clear_bit(*qid, pqm->queue_slot_bitmap);
474 if (list_empty(&pdd->qpd.queues_list) &&
475 list_empty(&pdd->qpd.priv_queue_list))
476 dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
477 return retval;
478 }
479
pqm_destroy_queue(struct process_queue_manager * pqm,unsigned int qid)480 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
481 {
482 struct process_queue_node *pqn;
483 struct kfd_process_device *pdd;
484 struct device_queue_manager *dqm;
485 struct kfd_node *dev;
486 int retval;
487
488 dqm = NULL;
489
490 retval = 0;
491
492 pqn = get_queue_by_qid(pqm, qid);
493 if (!pqn) {
494 pr_err("Queue id does not match any known queue\n");
495 return -EINVAL;
496 }
497
498 dev = NULL;
499 if (pqn->kq)
500 dev = pqn->kq->dev;
501 if (pqn->q)
502 dev = pqn->q->device;
503 if (WARN_ON(!dev))
504 return -ENODEV;
505
506 pdd = kfd_get_process_device_data(dev, pqm->process);
507 if (!pdd) {
508 pr_err("Process device data doesn't exist\n");
509 return -1;
510 }
511
512 if (pqn->kq) {
513 /* destroy kernel queue (DIQ) */
514 dqm = pqn->kq->dev->dqm;
515 dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
516 kernel_queue_uninit(pqn->kq);
517 }
518
519 if (pqn->q) {
520 retval = kfd_queue_unref_bo_vas(pdd, &pqn->q->properties);
521 if (retval)
522 goto err_destroy_queue;
523
524 dqm = pqn->q->device->dqm;
525 retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
526 if (retval) {
527 pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
528 pqm->process->pasid,
529 pqn->q->properties.queue_id, retval);
530 if (retval != -ETIME)
531 goto err_destroy_queue;
532 }
533 kfd_procfs_del_queue(pqn->q);
534 kfd_queue_release_buffers(pdd, &pqn->q->properties);
535 pqm_clean_queue_resource(pqm, pqn);
536 uninit_queue(pqn->q);
537 }
538
539 list_del(&pqn->process_queue_list);
540 kfree(pqn);
541 clear_bit(qid, pqm->queue_slot_bitmap);
542
543 if (list_empty(&pdd->qpd.queues_list) &&
544 list_empty(&pdd->qpd.priv_queue_list))
545 dqm->ops.unregister_process(dqm, &pdd->qpd);
546
547 err_destroy_queue:
548 return retval;
549 }
550
pqm_update_queue_properties(struct process_queue_manager * pqm,unsigned int qid,struct queue_properties * p)551 int pqm_update_queue_properties(struct process_queue_manager *pqm,
552 unsigned int qid, struct queue_properties *p)
553 {
554 int retval;
555 struct process_queue_node *pqn;
556
557 pqn = get_queue_by_qid(pqm, qid);
558 if (!pqn || !pqn->q) {
559 pr_debug("No queue %d exists for update operation\n", qid);
560 return -EFAULT;
561 }
562
563 /*
564 * Update with NULL ring address is used to disable the queue
565 */
566 if (p->queue_address && p->queue_size) {
567 struct kfd_process_device *pdd;
568 struct amdgpu_vm *vm;
569 struct queue *q = pqn->q;
570 int err;
571
572 pdd = kfd_get_process_device_data(q->device, q->process);
573 if (!pdd)
574 return -ENODEV;
575 vm = drm_priv_to_vm(pdd->drm_priv);
576 err = amdgpu_bo_reserve(vm->root.bo, false);
577 if (err)
578 return err;
579
580 if (kfd_queue_buffer_get(vm, (void *)p->queue_address, &p->ring_bo,
581 p->queue_size)) {
582 pr_debug("ring buf 0x%llx size 0x%llx not mapped on GPU\n",
583 p->queue_address, p->queue_size);
584 return -EFAULT;
585 }
586
587 kfd_queue_unref_bo_va(vm, &pqn->q->properties.ring_bo);
588 kfd_queue_buffer_put(&pqn->q->properties.ring_bo);
589 amdgpu_bo_unreserve(vm->root.bo);
590
591 pqn->q->properties.ring_bo = p->ring_bo;
592 }
593
594 pqn->q->properties.queue_address = p->queue_address;
595 pqn->q->properties.queue_size = p->queue_size;
596 pqn->q->properties.queue_percent = p->queue_percent;
597 pqn->q->properties.priority = p->priority;
598 pqn->q->properties.pm4_target_xcc = p->pm4_target_xcc;
599
600 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
601 pqn->q, NULL);
602 if (retval != 0)
603 return retval;
604
605 return 0;
606 }
607
pqm_update_mqd(struct process_queue_manager * pqm,unsigned int qid,struct mqd_update_info * minfo)608 int pqm_update_mqd(struct process_queue_manager *pqm,
609 unsigned int qid, struct mqd_update_info *minfo)
610 {
611 int retval;
612 struct process_queue_node *pqn;
613
614 pqn = get_queue_by_qid(pqm, qid);
615 if (!pqn) {
616 pr_debug("No queue %d exists for update operation\n", qid);
617 return -EFAULT;
618 }
619
620 /* CUs are masked for debugger requirements so deny user mask */
621 if (pqn->q->properties.is_dbg_wa && minfo && minfo->cu_mask.ptr)
622 return -EBUSY;
623
624 /* ASICs that have WGPs must enforce pairwise enabled mask checks. */
625 if (minfo && minfo->cu_mask.ptr &&
626 KFD_GC_VERSION(pqn->q->device) >= IP_VERSION(10, 0, 0)) {
627 int i;
628
629 for (i = 0; i < minfo->cu_mask.count; i += 2) {
630 uint32_t cu_pair = (minfo->cu_mask.ptr[i / 32] >> (i % 32)) & 0x3;
631
632 if (cu_pair && cu_pair != 0x3) {
633 pr_debug("CUs must be adjacent pairwise enabled.\n");
634 return -EINVAL;
635 }
636 }
637 }
638
639 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
640 pqn->q, minfo);
641 if (retval != 0)
642 return retval;
643
644 if (minfo && minfo->cu_mask.ptr)
645 pqn->q->properties.is_user_cu_masked = true;
646
647 return 0;
648 }
649
pqm_get_kernel_queue(struct process_queue_manager * pqm,unsigned int qid)650 struct kernel_queue *pqm_get_kernel_queue(
651 struct process_queue_manager *pqm,
652 unsigned int qid)
653 {
654 struct process_queue_node *pqn;
655
656 pqn = get_queue_by_qid(pqm, qid);
657 if (pqn && pqn->kq)
658 return pqn->kq;
659
660 return NULL;
661 }
662
pqm_get_user_queue(struct process_queue_manager * pqm,unsigned int qid)663 struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
664 unsigned int qid)
665 {
666 struct process_queue_node *pqn;
667
668 pqn = get_queue_by_qid(pqm, qid);
669 return pqn ? pqn->q : NULL;
670 }
671
pqm_get_wave_state(struct process_queue_manager * pqm,unsigned int qid,void __user * ctl_stack,u32 * ctl_stack_used_size,u32 * save_area_used_size)672 int pqm_get_wave_state(struct process_queue_manager *pqm,
673 unsigned int qid,
674 void __user *ctl_stack,
675 u32 *ctl_stack_used_size,
676 u32 *save_area_used_size)
677 {
678 struct process_queue_node *pqn;
679
680 pqn = get_queue_by_qid(pqm, qid);
681 if (!pqn) {
682 pr_debug("amdkfd: No queue %d exists for operation\n",
683 qid);
684 return -EFAULT;
685 }
686
687 return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
688 pqn->q,
689 ctl_stack,
690 ctl_stack_used_size,
691 save_area_used_size);
692 }
693
pqm_get_queue_snapshot(struct process_queue_manager * pqm,uint64_t exception_clear_mask,void __user * buf,int * num_qss_entries,uint32_t * entry_size)694 int pqm_get_queue_snapshot(struct process_queue_manager *pqm,
695 uint64_t exception_clear_mask,
696 void __user *buf,
697 int *num_qss_entries,
698 uint32_t *entry_size)
699 {
700 struct process_queue_node *pqn;
701 struct kfd_queue_snapshot_entry src;
702 uint32_t tmp_entry_size = *entry_size, tmp_qss_entries = *num_qss_entries;
703 int r = 0;
704
705 *num_qss_entries = 0;
706 if (!(*entry_size))
707 return -EINVAL;
708
709 *entry_size = min_t(size_t, *entry_size, sizeof(struct kfd_queue_snapshot_entry));
710 mutex_lock(&pqm->process->event_mutex);
711
712 memset(&src, 0, sizeof(src));
713
714 list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
715 if (!pqn->q)
716 continue;
717
718 if (*num_qss_entries < tmp_qss_entries) {
719 set_queue_snapshot_entry(pqn->q, exception_clear_mask, &src);
720
721 if (copy_to_user(buf, &src, *entry_size)) {
722 r = -EFAULT;
723 break;
724 }
725 buf += tmp_entry_size;
726 }
727 *num_qss_entries += 1;
728 }
729
730 mutex_unlock(&pqm->process->event_mutex);
731 return r;
732 }
733
get_queue_data_sizes(struct kfd_process_device * pdd,struct queue * q,uint32_t * mqd_size,uint32_t * ctl_stack_size)734 static int get_queue_data_sizes(struct kfd_process_device *pdd,
735 struct queue *q,
736 uint32_t *mqd_size,
737 uint32_t *ctl_stack_size)
738 {
739 int ret;
740
741 ret = pqm_get_queue_checkpoint_info(&pdd->process->pqm,
742 q->properties.queue_id,
743 mqd_size,
744 ctl_stack_size);
745 if (ret)
746 pr_err("Failed to get queue dump info (%d)\n", ret);
747
748 return ret;
749 }
750
kfd_process_get_queue_info(struct kfd_process * p,uint32_t * num_queues,uint64_t * priv_data_sizes)751 int kfd_process_get_queue_info(struct kfd_process *p,
752 uint32_t *num_queues,
753 uint64_t *priv_data_sizes)
754 {
755 uint32_t extra_data_sizes = 0;
756 struct queue *q;
757 int i;
758 int ret;
759
760 *num_queues = 0;
761
762 /* Run over all PDDs of the process */
763 for (i = 0; i < p->n_pdds; i++) {
764 struct kfd_process_device *pdd = p->pdds[i];
765
766 list_for_each_entry(q, &pdd->qpd.queues_list, list) {
767 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
768 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
769 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
770 uint32_t mqd_size, ctl_stack_size;
771
772 *num_queues = *num_queues + 1;
773
774 ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
775 if (ret)
776 return ret;
777
778 extra_data_sizes += mqd_size + ctl_stack_size;
779 } else {
780 pr_err("Unsupported queue type (%d)\n", q->properties.type);
781 return -EOPNOTSUPP;
782 }
783 }
784 }
785 *priv_data_sizes = extra_data_sizes +
786 (*num_queues * sizeof(struct kfd_criu_queue_priv_data));
787
788 return 0;
789 }
790
pqm_checkpoint_mqd(struct process_queue_manager * pqm,unsigned int qid,void * mqd,void * ctl_stack)791 static int pqm_checkpoint_mqd(struct process_queue_manager *pqm,
792 unsigned int qid,
793 void *mqd,
794 void *ctl_stack)
795 {
796 struct process_queue_node *pqn;
797
798 pqn = get_queue_by_qid(pqm, qid);
799 if (!pqn) {
800 pr_debug("amdkfd: No queue %d exists for operation\n", qid);
801 return -EFAULT;
802 }
803
804 if (!pqn->q->device->dqm->ops.checkpoint_mqd) {
805 pr_err("amdkfd: queue dumping not supported on this device\n");
806 return -EOPNOTSUPP;
807 }
808
809 return pqn->q->device->dqm->ops.checkpoint_mqd(pqn->q->device->dqm,
810 pqn->q, mqd, ctl_stack);
811 }
812
criu_checkpoint_queue(struct kfd_process_device * pdd,struct queue * q,struct kfd_criu_queue_priv_data * q_data)813 static int criu_checkpoint_queue(struct kfd_process_device *pdd,
814 struct queue *q,
815 struct kfd_criu_queue_priv_data *q_data)
816 {
817 uint8_t *mqd, *ctl_stack;
818 int ret;
819
820 mqd = (void *)(q_data + 1);
821 ctl_stack = mqd + q_data->mqd_size;
822
823 q_data->gpu_id = pdd->user_gpu_id;
824 q_data->type = q->properties.type;
825 q_data->format = q->properties.format;
826 q_data->q_id = q->properties.queue_id;
827 q_data->q_address = q->properties.queue_address;
828 q_data->q_size = q->properties.queue_size;
829 q_data->priority = q->properties.priority;
830 q_data->q_percent = q->properties.queue_percent;
831 q_data->read_ptr_addr = (uint64_t)q->properties.read_ptr;
832 q_data->write_ptr_addr = (uint64_t)q->properties.write_ptr;
833 q_data->doorbell_id = q->doorbell_id;
834
835 q_data->sdma_id = q->sdma_id;
836
837 q_data->eop_ring_buffer_address =
838 q->properties.eop_ring_buffer_address;
839
840 q_data->eop_ring_buffer_size = q->properties.eop_ring_buffer_size;
841
842 q_data->ctx_save_restore_area_address =
843 q->properties.ctx_save_restore_area_address;
844
845 q_data->ctx_save_restore_area_size =
846 q->properties.ctx_save_restore_area_size;
847
848 q_data->gws = !!q->gws;
849
850 ret = pqm_checkpoint_mqd(&pdd->process->pqm, q->properties.queue_id, mqd, ctl_stack);
851 if (ret) {
852 pr_err("Failed checkpoint queue_mqd (%d)\n", ret);
853 return ret;
854 }
855
856 pr_debug("Dumping Queue: gpu_id:%x queue_id:%u\n", q_data->gpu_id, q_data->q_id);
857 return ret;
858 }
859
criu_checkpoint_queues_device(struct kfd_process_device * pdd,uint8_t __user * user_priv,unsigned int * q_index,uint64_t * queues_priv_data_offset)860 static int criu_checkpoint_queues_device(struct kfd_process_device *pdd,
861 uint8_t __user *user_priv,
862 unsigned int *q_index,
863 uint64_t *queues_priv_data_offset)
864 {
865 unsigned int q_private_data_size = 0;
866 uint8_t *q_private_data = NULL; /* Local buffer to store individual queue private data */
867 struct queue *q;
868 int ret = 0;
869
870 list_for_each_entry(q, &pdd->qpd.queues_list, list) {
871 struct kfd_criu_queue_priv_data *q_data;
872 uint64_t q_data_size;
873 uint32_t mqd_size;
874 uint32_t ctl_stack_size;
875
876 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE &&
877 q->properties.type != KFD_QUEUE_TYPE_SDMA &&
878 q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI) {
879
880 pr_err("Unsupported queue type (%d)\n", q->properties.type);
881 ret = -EOPNOTSUPP;
882 break;
883 }
884
885 ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
886 if (ret)
887 break;
888
889 q_data_size = sizeof(*q_data) + mqd_size + ctl_stack_size;
890
891 /* Increase local buffer space if needed */
892 if (q_private_data_size < q_data_size) {
893 kfree(q_private_data);
894
895 q_private_data = kzalloc(q_data_size, GFP_KERNEL);
896 if (!q_private_data) {
897 ret = -ENOMEM;
898 break;
899 }
900 q_private_data_size = q_data_size;
901 }
902
903 q_data = (struct kfd_criu_queue_priv_data *)q_private_data;
904
905 /* data stored in this order: priv_data, mqd, ctl_stack */
906 q_data->mqd_size = mqd_size;
907 q_data->ctl_stack_size = ctl_stack_size;
908
909 ret = criu_checkpoint_queue(pdd, q, q_data);
910 if (ret)
911 break;
912
913 q_data->object_type = KFD_CRIU_OBJECT_TYPE_QUEUE;
914
915 ret = copy_to_user(user_priv + *queues_priv_data_offset,
916 q_data, q_data_size);
917 if (ret) {
918 ret = -EFAULT;
919 break;
920 }
921 *queues_priv_data_offset += q_data_size;
922 *q_index = *q_index + 1;
923 }
924
925 kfree(q_private_data);
926
927 return ret;
928 }
929
kfd_criu_checkpoint_queues(struct kfd_process * p,uint8_t __user * user_priv_data,uint64_t * priv_data_offset)930 int kfd_criu_checkpoint_queues(struct kfd_process *p,
931 uint8_t __user *user_priv_data,
932 uint64_t *priv_data_offset)
933 {
934 int ret = 0, pdd_index, q_index = 0;
935
936 for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) {
937 struct kfd_process_device *pdd = p->pdds[pdd_index];
938
939 /*
940 * criu_checkpoint_queues_device will copy data to user and update q_index and
941 * queues_priv_data_offset
942 */
943 ret = criu_checkpoint_queues_device(pdd, user_priv_data, &q_index,
944 priv_data_offset);
945
946 if (ret)
947 break;
948 }
949
950 return ret;
951 }
952
set_queue_properties_from_criu(struct queue_properties * qp,struct kfd_criu_queue_priv_data * q_data)953 static void set_queue_properties_from_criu(struct queue_properties *qp,
954 struct kfd_criu_queue_priv_data *q_data)
955 {
956 qp->is_interop = false;
957 qp->queue_percent = q_data->q_percent;
958 qp->priority = q_data->priority;
959 qp->queue_address = q_data->q_address;
960 qp->queue_size = q_data->q_size;
961 qp->read_ptr = (uint32_t *) q_data->read_ptr_addr;
962 qp->write_ptr = (uint32_t *) q_data->write_ptr_addr;
963 qp->eop_ring_buffer_address = q_data->eop_ring_buffer_address;
964 qp->eop_ring_buffer_size = q_data->eop_ring_buffer_size;
965 qp->ctx_save_restore_area_address = q_data->ctx_save_restore_area_address;
966 qp->ctx_save_restore_area_size = q_data->ctx_save_restore_area_size;
967 qp->ctl_stack_size = q_data->ctl_stack_size;
968 qp->type = q_data->type;
969 qp->format = q_data->format;
970 }
971
kfd_criu_restore_queue(struct kfd_process * p,uint8_t __user * user_priv_ptr,uint64_t * priv_data_offset,uint64_t max_priv_data_size)972 int kfd_criu_restore_queue(struct kfd_process *p,
973 uint8_t __user *user_priv_ptr,
974 uint64_t *priv_data_offset,
975 uint64_t max_priv_data_size)
976 {
977 uint8_t *mqd, *ctl_stack, *q_extra_data = NULL;
978 struct kfd_criu_queue_priv_data *q_data;
979 struct kfd_process_device *pdd;
980 uint64_t q_extra_data_size;
981 struct queue_properties qp;
982 unsigned int queue_id;
983 int ret = 0;
984
985 if (*priv_data_offset + sizeof(*q_data) > max_priv_data_size)
986 return -EINVAL;
987
988 q_data = kmalloc(sizeof(*q_data), GFP_KERNEL);
989 if (!q_data)
990 return -ENOMEM;
991
992 ret = copy_from_user(q_data, user_priv_ptr + *priv_data_offset, sizeof(*q_data));
993 if (ret) {
994 ret = -EFAULT;
995 goto exit;
996 }
997
998 *priv_data_offset += sizeof(*q_data);
999 q_extra_data_size = (uint64_t)q_data->ctl_stack_size + q_data->mqd_size;
1000
1001 if (*priv_data_offset + q_extra_data_size > max_priv_data_size) {
1002 ret = -EINVAL;
1003 goto exit;
1004 }
1005
1006 q_extra_data = kmalloc(q_extra_data_size, GFP_KERNEL);
1007 if (!q_extra_data) {
1008 ret = -ENOMEM;
1009 goto exit;
1010 }
1011
1012 ret = copy_from_user(q_extra_data, user_priv_ptr + *priv_data_offset, q_extra_data_size);
1013 if (ret) {
1014 ret = -EFAULT;
1015 goto exit;
1016 }
1017
1018 *priv_data_offset += q_extra_data_size;
1019
1020 pdd = kfd_process_device_data_by_id(p, q_data->gpu_id);
1021 if (!pdd) {
1022 pr_err("Failed to get pdd\n");
1023 ret = -EINVAL;
1024 goto exit;
1025 }
1026
1027 /* data stored in this order: mqd, ctl_stack */
1028 mqd = q_extra_data;
1029 ctl_stack = mqd + q_data->mqd_size;
1030
1031 memset(&qp, 0, sizeof(qp));
1032 set_queue_properties_from_criu(&qp, q_data);
1033
1034 print_queue_properties(&qp);
1035
1036 ret = pqm_create_queue(&p->pqm, pdd->dev, NULL, &qp, &queue_id, q_data, mqd, ctl_stack,
1037 NULL);
1038 if (ret) {
1039 pr_err("Failed to create new queue err:%d\n", ret);
1040 goto exit;
1041 }
1042
1043 if (q_data->gws)
1044 ret = pqm_set_gws(&p->pqm, q_data->q_id, pdd->dev->gws);
1045
1046 exit:
1047 if (ret)
1048 pr_err("Failed to restore queue (%d)\n", ret);
1049 else
1050 pr_debug("Queue id %d was restored successfully\n", queue_id);
1051
1052 kfree(q_data);
1053 kfree(q_extra_data);
1054
1055 return ret;
1056 }
1057
pqm_get_queue_checkpoint_info(struct process_queue_manager * pqm,unsigned int qid,uint32_t * mqd_size,uint32_t * ctl_stack_size)1058 int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm,
1059 unsigned int qid,
1060 uint32_t *mqd_size,
1061 uint32_t *ctl_stack_size)
1062 {
1063 struct process_queue_node *pqn;
1064
1065 pqn = get_queue_by_qid(pqm, qid);
1066 if (!pqn) {
1067 pr_debug("amdkfd: No queue %d exists for operation\n", qid);
1068 return -EFAULT;
1069 }
1070
1071 if (!pqn->q->device->dqm->ops.get_queue_checkpoint_info) {
1072 pr_err("amdkfd: queue dumping not supported on this device\n");
1073 return -EOPNOTSUPP;
1074 }
1075
1076 pqn->q->device->dqm->ops.get_queue_checkpoint_info(pqn->q->device->dqm,
1077 pqn->q, mqd_size,
1078 ctl_stack_size);
1079 return 0;
1080 }
1081
1082 #if defined(CONFIG_DEBUG_FS)
1083
pqm_debugfs_mqds(struct seq_file * m,void * data)1084 int pqm_debugfs_mqds(struct seq_file *m, void *data)
1085 {
1086 struct process_queue_manager *pqm = data;
1087 struct process_queue_node *pqn;
1088 struct queue *q;
1089 enum KFD_MQD_TYPE mqd_type;
1090 struct mqd_manager *mqd_mgr;
1091 int r = 0, xcc, num_xccs = 1;
1092 void *mqd;
1093 uint64_t size = 0;
1094
1095 list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
1096 if (pqn->q) {
1097 q = pqn->q;
1098 switch (q->properties.type) {
1099 case KFD_QUEUE_TYPE_SDMA:
1100 case KFD_QUEUE_TYPE_SDMA_XGMI:
1101 seq_printf(m, " SDMA queue on device %x\n",
1102 q->device->id);
1103 mqd_type = KFD_MQD_TYPE_SDMA;
1104 break;
1105 case KFD_QUEUE_TYPE_COMPUTE:
1106 seq_printf(m, " Compute queue on device %x\n",
1107 q->device->id);
1108 mqd_type = KFD_MQD_TYPE_CP;
1109 num_xccs = NUM_XCC(q->device->xcc_mask);
1110 break;
1111 default:
1112 seq_printf(m,
1113 " Bad user queue type %d on device %x\n",
1114 q->properties.type, q->device->id);
1115 continue;
1116 }
1117 mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type];
1118 size = mqd_mgr->mqd_stride(mqd_mgr,
1119 &q->properties);
1120 } else if (pqn->kq) {
1121 q = pqn->kq->queue;
1122 mqd_mgr = pqn->kq->mqd_mgr;
1123 switch (q->properties.type) {
1124 case KFD_QUEUE_TYPE_DIQ:
1125 seq_printf(m, " DIQ on device %x\n",
1126 pqn->kq->dev->id);
1127 break;
1128 default:
1129 seq_printf(m,
1130 " Bad kernel queue type %d on device %x\n",
1131 q->properties.type,
1132 pqn->kq->dev->id);
1133 continue;
1134 }
1135 } else {
1136 seq_printf(m,
1137 " Weird: Queue node with neither kernel nor user queue\n");
1138 continue;
1139 }
1140
1141 for (xcc = 0; xcc < num_xccs; xcc++) {
1142 mqd = q->mqd + size * xcc;
1143 r = mqd_mgr->debugfs_show_mqd(m, mqd);
1144 if (r != 0)
1145 break;
1146 }
1147 }
1148
1149 return r;
1150 }
1151
1152 #endif
1153