1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2023 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 <drm/drm_auth.h>
26 #include <drm/drm_exec.h>
27 #include <linux/pm_runtime.h>
28 #include <drm/drm_drv.h>
29
30 #include "amdgpu.h"
31 #include "amdgpu_reset.h"
32 #include "amdgpu_vm.h"
33 #include "amdgpu_userq.h"
34 #include "amdgpu_hmm.h"
35 #include "amdgpu_userq_fence.h"
36
amdgpu_userq_get_supported_ip_mask(struct amdgpu_device * adev)37 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
38 {
39 int i;
40 u32 userq_ip_mask = 0;
41
42 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {
43 if (adev->userq_funcs[i])
44 userq_ip_mask |= (1 << i);
45 }
46
47 return userq_ip_mask;
48 }
49
amdgpu_userq_is_reset_type_supported(struct amdgpu_device * adev,enum amdgpu_ring_type ring_type,int reset_type)50 static bool amdgpu_userq_is_reset_type_supported(struct amdgpu_device *adev,
51 enum amdgpu_ring_type ring_type, int reset_type)
52 {
53
54 if (ring_type < 0 || ring_type >= AMDGPU_RING_TYPE_MAX)
55 return false;
56
57 switch (ring_type) {
58 case AMDGPU_RING_TYPE_GFX:
59 if (adev->gfx.gfx_supported_reset & reset_type)
60 return true;
61 break;
62 case AMDGPU_RING_TYPE_COMPUTE:
63 if (adev->gfx.compute_supported_reset & reset_type)
64 return true;
65 break;
66 case AMDGPU_RING_TYPE_SDMA:
67 if (adev->sdma.supported_reset & reset_type)
68 return true;
69 break;
70 case AMDGPU_RING_TYPE_VCN_DEC:
71 case AMDGPU_RING_TYPE_VCN_ENC:
72 if (adev->vcn.supported_reset & reset_type)
73 return true;
74 break;
75 case AMDGPU_RING_TYPE_VCN_JPEG:
76 if (adev->jpeg.supported_reset & reset_type)
77 return true;
78 break;
79 default:
80 break;
81 }
82 return false;
83 }
84
amdgpu_userq_mgr_reset_work(struct work_struct * work)85 static void amdgpu_userq_mgr_reset_work(struct work_struct *work)
86 {
87 struct amdgpu_userq_mgr *uq_mgr =
88 container_of(work, struct amdgpu_userq_mgr,
89 reset_work);
90 struct amdgpu_device *adev = uq_mgr->adev;
91 const int queue_types[] = {
92 AMDGPU_RING_TYPE_COMPUTE,
93 AMDGPU_RING_TYPE_GFX,
94 AMDGPU_RING_TYPE_SDMA
95 };
96 const int num_queue_types = ARRAY_SIZE(queue_types);
97 bool gpu_reset = false;
98 int i, r;
99
100 if (unlikely(adev->debug_disable_gpu_ring_reset)) {
101 dev_err(adev->dev, "userq reset disabled by debug mask\n");
102 return;
103 }
104
105 /*
106 * If GPU recovery feature is disabled system-wide,
107 * skip all reset detection logic
108 */
109 if (!amdgpu_gpu_recovery)
110 return;
111
112 /*
113 * Iterate through all queue types to detect and reset problematic queues
114 * Process each queue type in the defined order
115 */
116 for (i = 0; i < num_queue_types; i++) {
117 int ring_type = queue_types[i];
118 const struct amdgpu_userq_funcs *funcs =
119 adev->userq_funcs[ring_type];
120
121 if (!amdgpu_userq_is_reset_type_supported(adev, ring_type,
122 AMDGPU_RESET_TYPE_PER_QUEUE))
123 continue;
124
125 if (atomic_read(&uq_mgr->userq_count[ring_type]) > 0 &&
126 funcs && funcs->detect_and_reset) {
127 r = funcs->detect_and_reset(adev, ring_type);
128 if (r) {
129 gpu_reset = true;
130 break;
131 }
132 }
133 }
134
135 if (gpu_reset) {
136 struct amdgpu_reset_context reset_context;
137
138 memset(&reset_context, 0, sizeof(reset_context));
139
140 reset_context.method = AMD_RESET_METHOD_NONE;
141 reset_context.reset_req_dev = adev;
142 reset_context.src = AMDGPU_RESET_SRC_USERQ;
143 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
144 /*set_bit(AMDGPU_SKIP_COREDUMP, &reset_context.flags);*/
145
146 amdgpu_device_gpu_recover(adev, NULL, &reset_context);
147 }
148 }
149
amdgpu_userq_hang_detect_work(struct work_struct * work)150 static void amdgpu_userq_hang_detect_work(struct work_struct *work)
151 {
152 struct amdgpu_usermode_queue *queue =
153 container_of(work, struct amdgpu_usermode_queue,
154 hang_detect_work.work);
155
156 /*
157 * Don't schedule the work here! Scheduling or queue work from one reset
158 * handler to another is illegal if you don't take extra precautions!
159 */
160 amdgpu_userq_mgr_reset_work(&queue->userq_mgr->reset_work);
161 }
162
163 /*
164 * Start hang detection for a user queue fence. A delayed work will be scheduled
165 * to reset the queues when the fence doesn't signal in time.
166 */
amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue * queue)167 void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue)
168 {
169 struct amdgpu_device *adev;
170 unsigned long timeout_ms;
171
172 adev = queue->userq_mgr->adev;
173 /* Determine timeout based on queue type */
174 switch (queue->queue_type) {
175 case AMDGPU_RING_TYPE_GFX:
176 timeout_ms = adev->gfx_timeout;
177 break;
178 case AMDGPU_RING_TYPE_COMPUTE:
179 timeout_ms = adev->compute_timeout;
180 break;
181 case AMDGPU_RING_TYPE_SDMA:
182 timeout_ms = adev->sdma_timeout;
183 break;
184 default:
185 timeout_ms = adev->gfx_timeout;
186 break;
187 }
188
189 queue_delayed_work(adev->reset_domain->wq, &queue->hang_detect_work,
190 msecs_to_jiffies(timeout_ms));
191 }
192
amdgpu_userq_process_fence_irq(struct amdgpu_device * adev,u32 doorbell)193 void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell)
194 {
195 struct xarray *xa = &adev->userq_doorbell_xa;
196 struct amdgpu_usermode_queue *queue;
197 unsigned long flags;
198 int r;
199
200 xa_lock_irqsave(xa, flags);
201 queue = xa_load(xa, doorbell);
202 if (queue) {
203 r = amdgpu_userq_fence_driver_process(queue->fence_drv);
204 /*
205 * We are in interrupt context here, this *can't* wait for
206 * reset work to finish.
207 */
208 if (r >= 0)
209 cancel_delayed_work(&queue->hang_detect_work);
210
211 /* Restart the timer when there are still fences pending */
212 if (r == 1)
213 amdgpu_userq_start_hang_detect_work(queue);
214 }
215 xa_unlock_irqrestore(xa, flags);
216 }
217
amdgpu_userq_input_va_validate(struct amdgpu_device * adev,struct amdgpu_usermode_queue * queue,u64 addr,u64 expected_size,u64 * va_out)218 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
219 struct amdgpu_usermode_queue *queue,
220 u64 addr, u64 expected_size,
221 u64 *va_out)
222 {
223 struct amdgpu_bo_va_mapping *va_map;
224 struct amdgpu_vm *vm = queue->vm;
225 u64 user_addr;
226 u64 size;
227
228 /* Caller must hold vm->root.bo reservation */
229 dma_resv_assert_held(queue->vm->root.bo->tbo.base.resv);
230
231 user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT;
232 size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
233
234 va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
235 if (!va_map)
236 return -EINVAL;
237
238 /* Only validate the userq whether resident in the VM mapping range */
239 if (user_addr >= va_map->start &&
240 va_map->last - user_addr + 1 >= size) {
241 va_map->bo_va->userq_va_mapped = true;
242 *va_out = user_addr;
243 return 0;
244 }
245
246 return -EINVAL;
247 }
248
amdgpu_userq_buffer_va_mapped(struct amdgpu_vm * vm,u64 addr)249 static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
250 {
251 struct amdgpu_bo_va_mapping *mapping;
252 bool r;
253
254 dma_resv_assert_held(vm->root.bo->tbo.base.resv);
255
256 mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
257 if (!IS_ERR_OR_NULL(mapping) && mapping->bo_va->userq_va_mapped)
258 r = true;
259 else
260 r = false;
261
262 return r;
263 }
264
amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue * queue)265 static bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue)
266 {
267 int i, r = 0;
268
269 for (i = 0; i < ARRAY_SIZE(queue->userq_vas.va_array); i++) {
270 if (!queue->userq_vas.va_array[i])
271 continue;
272 r += amdgpu_userq_buffer_va_mapped(queue->vm,
273 queue->userq_vas.va_array[i]);
274 dev_dbg(queue->userq_mgr->adev->dev,
275 "validate the userq mapping:%p va:%llx r:%d\n",
276 queue, queue->userq_vas.va_array[i], r);
277 }
278
279 if (r != 0)
280 return true;
281
282 return false;
283 }
284
285
286
amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue * queue)287 static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue)
288 {
289 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
290 struct amdgpu_device *adev = uq_mgr->adev;
291 const struct amdgpu_userq_funcs *userq_funcs =
292 adev->userq_funcs[queue->queue_type];
293 int r;
294
295 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) {
296 r = userq_funcs->preempt(queue);
297 if (r) {
298 queue->state = AMDGPU_USERQ_STATE_HUNG;
299 return r;
300 } else {
301 queue->state = AMDGPU_USERQ_STATE_PREEMPTED;
302 }
303 }
304 return 0;
305 }
306
amdgpu_userq_restore_helper(struct amdgpu_usermode_queue * queue)307 static int amdgpu_userq_restore_helper(struct amdgpu_usermode_queue *queue)
308 {
309 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
310 struct amdgpu_device *adev = uq_mgr->adev;
311 const struct amdgpu_userq_funcs *userq_funcs =
312 adev->userq_funcs[queue->queue_type];
313 int r = 0;
314
315 if (queue->state == AMDGPU_USERQ_STATE_PREEMPTED) {
316 r = userq_funcs->restore(queue);
317 if (r) {
318 queue->state = AMDGPU_USERQ_STATE_HUNG;
319 } else {
320 queue->state = AMDGPU_USERQ_STATE_MAPPED;
321 }
322 }
323
324 return r;
325 }
326
amdgpu_userq_unmap_helper(struct amdgpu_usermode_queue * queue)327 static int amdgpu_userq_unmap_helper(struct amdgpu_usermode_queue *queue)
328 {
329 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
330 struct amdgpu_device *adev = uq_mgr->adev;
331 const struct amdgpu_userq_funcs *userq_funcs =
332 adev->userq_funcs[queue->queue_type];
333 int r;
334
335 if ((queue->state == AMDGPU_USERQ_STATE_MAPPED) ||
336 (queue->state == AMDGPU_USERQ_STATE_PREEMPTED)) {
337
338 r = userq_funcs->unmap(queue);
339 if (r) {
340 queue->state = AMDGPU_USERQ_STATE_HUNG;
341 return r;
342 } else {
343 queue->state = AMDGPU_USERQ_STATE_UNMAPPED;
344 }
345 }
346
347 return 0;
348 }
349
amdgpu_userq_map_helper(struct amdgpu_usermode_queue * queue)350 static int amdgpu_userq_map_helper(struct amdgpu_usermode_queue *queue)
351 {
352 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
353 struct amdgpu_device *adev = uq_mgr->adev;
354 const struct amdgpu_userq_funcs *userq_funcs =
355 adev->userq_funcs[queue->queue_type];
356 int r;
357
358 if (queue->state == AMDGPU_USERQ_STATE_UNMAPPED) {
359 r = userq_funcs->map(queue);
360 if (r) {
361 queue->state = AMDGPU_USERQ_STATE_HUNG;
362 return r;
363 } else {
364 queue->state = AMDGPU_USERQ_STATE_MAPPED;
365 }
366 }
367
368 return 0;
369 }
370
amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue * queue)371 static void amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue *queue)
372 {
373 struct dma_fence *f = queue->last_fence;
374
375 if (!f)
376 return;
377
378 dma_fence_wait(f, false);
379 }
380
amdgpu_userq_cleanup(struct amdgpu_usermode_queue * queue)381 static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue)
382 {
383 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
384 struct amdgpu_device *adev = uq_mgr->adev;
385
386 /* Wait for mode-1 reset to complete */
387 down_read(&adev->reset_domain->sem);
388
389 /* Use interrupt-safe locking since IRQ handlers may access these XArrays */
390 xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index);
391 amdgpu_userq_fence_driver_free(queue);
392 queue->fence_drv = NULL;
393
394 up_read(&adev->reset_domain->sem);
395 }
396
397 /**
398 * amdgpu_userq_ensure_ev_fence - ensure a valid, unsignaled eviction fence exists
399 * @uq_mgr: the usermode queue manager for this process
400 * @evf_mgr: the eviction fence manager to check and rearm
401 *
402 * Ensures that a valid and not yet signaled eviction fence is attached to the
403 * usermode queue before any queue operations proceed. If it is signalled, then
404 * rearm a new eviction fence.
405 */
406 void
amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr * uq_mgr,struct amdgpu_eviction_fence_mgr * evf_mgr)407 amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
408 struct amdgpu_eviction_fence_mgr *evf_mgr)
409 {
410 struct dma_fence *ev_fence;
411
412 retry:
413 /* Flush any pending resume work to create ev_fence */
414 flush_delayed_work(&uq_mgr->resume_work);
415
416 mutex_lock(&uq_mgr->userq_mutex);
417 ev_fence = amdgpu_evf_mgr_get_fence(evf_mgr);
418 if (dma_fence_is_signaled(ev_fence)) {
419 dma_fence_put(ev_fence);
420 mutex_unlock(&uq_mgr->userq_mutex);
421 /*
422 * Looks like there was no pending resume work,
423 * add one now to create a valid eviction fence
424 */
425 schedule_delayed_work(&uq_mgr->resume_work, 0);
426 goto retry;
427 }
428 dma_fence_put(ev_fence);
429 }
430
431
432
433 static int
amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr * uq_mgr,struct amdgpu_db_info * db_info,struct drm_file * filp,u64 * index)434 amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,
435 struct amdgpu_db_info *db_info,
436 struct drm_file *filp,
437 u64 *index)
438 {
439 u64 doorbell_index;
440 struct drm_gem_object *gobj;
441 struct amdgpu_userq_obj *db_obj = db_info->db_obj;
442 int r, db_size;
443
444 gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle);
445 if (gobj == NULL) {
446 drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n");
447 return -EINVAL;
448 }
449
450 db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
451 drm_gem_object_put(gobj);
452
453 r = amdgpu_bo_reserve(db_obj->obj, true);
454 if (r) {
455 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n");
456 goto unref_bo;
457 }
458
459 /* Pin the BO before generating the index, unpin in queue destroy */
460 r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL);
461 if (r) {
462 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n");
463 goto unresv_bo;
464 }
465
466 switch (db_info->queue_type) {
467 case AMDGPU_HW_IP_GFX:
468 case AMDGPU_HW_IP_COMPUTE:
469 case AMDGPU_HW_IP_DMA:
470 db_size = sizeof(u64);
471 break;
472 default:
473 drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n",
474 db_info->queue_type);
475 r = -EINVAL;
476 goto unpin_bo;
477 }
478
479 /* Validate doorbell_offset is within the doorbell BO */
480 if ((u64)db_info->doorbell_offset * db_size + db_size >
481 amdgpu_bo_size(db_obj->obj)) {
482 r = -EINVAL;
483 goto unpin_bo;
484 }
485
486 doorbell_index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj,
487 db_info->doorbell_offset, db_size);
488 drm_dbg_driver(adev_to_drm(uq_mgr->adev),
489 "[Usermode queues] doorbell index=%lld\n", doorbell_index);
490 amdgpu_bo_unreserve(db_obj->obj);
491 *index = doorbell_index;
492 return 0;
493
494 unpin_bo:
495 amdgpu_bo_unpin(db_obj->obj);
496 unresv_bo:
497 amdgpu_bo_unreserve(db_obj->obj);
498 unref_bo:
499 amdgpu_bo_unref(&db_obj->obj);
500 return r;
501 }
502
503 static int
amdgpu_userq_destroy(struct amdgpu_userq_mgr * uq_mgr,struct amdgpu_usermode_queue * queue)504 amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue)
505 {
506 struct amdgpu_device *adev = uq_mgr->adev;
507 const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
508 int r = 0;
509
510 cancel_delayed_work_sync(&uq_mgr->resume_work);
511
512 /* Cancel any pending hang detection work and cleanup */
513 cancel_delayed_work_sync(&queue->hang_detect_work);
514
515 mutex_lock(&uq_mgr->userq_mutex);
516 amdgpu_userq_wait_for_last_fence(queue);
517
518 #if defined(CONFIG_DEBUG_FS)
519 debugfs_remove_recursive(queue->debugfs_queue);
520 #endif
521 r = amdgpu_userq_unmap_helper(queue);
522 atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
523 amdgpu_userq_cleanup(queue);
524 mutex_unlock(&uq_mgr->userq_mutex);
525
526 /*
527 * A failed unmap means MES could not remove the hung queue and is now
528 * unresponsive. Recover the GPU here so the wedged MES does not fail
529 * the next, unrelated queue submission and trigger a reset attributed
530 * to an innocent workload.
531 */
532 if (r)
533 queue_work(adev->reset_domain->wq, &uq_mgr->reset_work);
534
535 cancel_delayed_work_sync(&queue->hang_detect_work);
536 uq_funcs->mqd_destroy(queue);
537 queue->userq_mgr = NULL;
538
539 amdgpu_bo_reserve(queue->db_obj.obj, true);
540 amdgpu_bo_unpin(queue->db_obj.obj);
541 amdgpu_bo_unreserve(queue->db_obj.obj);
542 amdgpu_bo_unref(&queue->db_obj.obj);
543
544 kfree(queue);
545
546 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
547
548 return r;
549 }
550
amdgpu_userq_kref_destroy(struct kref * kref)551 static void amdgpu_userq_kref_destroy(struct kref *kref)
552 {
553 int r;
554 struct amdgpu_usermode_queue *queue =
555 container_of(kref, struct amdgpu_usermode_queue, refcount);
556 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
557
558 r = amdgpu_userq_destroy(uq_mgr, queue);
559 if (r)
560 drm_file_err(uq_mgr->file, "Failed to destroy usermode queue %d\n", r);
561 }
562
amdgpu_userq_get(struct amdgpu_userq_mgr * uq_mgr,u32 qid)563 struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr, u32 qid)
564 {
565 struct amdgpu_usermode_queue *queue;
566
567 xa_lock(&uq_mgr->userq_xa);
568 queue = xa_load(&uq_mgr->userq_xa, qid);
569 if (queue)
570 kref_get(&queue->refcount);
571 xa_unlock(&uq_mgr->userq_xa);
572
573 return queue;
574 }
575
amdgpu_userq_put(struct amdgpu_usermode_queue * queue)576 void amdgpu_userq_put(struct amdgpu_usermode_queue *queue)
577 {
578 if (queue)
579 kref_put(&queue->refcount, amdgpu_userq_kref_destroy);
580 }
581
amdgpu_userq_priority_permit(struct drm_file * filp,int priority)582 static int amdgpu_userq_priority_permit(struct drm_file *filp,
583 int priority)
584 {
585 if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH)
586 return 0;
587
588 if (capable(CAP_SYS_NICE))
589 return 0;
590
591 if (drm_is_current_master(filp))
592 return 0;
593
594 return -EACCES;
595 }
596
597 static int
amdgpu_userq_create(struct drm_file * filp,union drm_amdgpu_userq * args)598 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
599 {
600 struct amdgpu_fpriv *fpriv = filp->driver_priv;
601 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
602 struct amdgpu_device *adev = uq_mgr->adev;
603 const struct amdgpu_userq_funcs *uq_funcs;
604 struct amdgpu_usermode_queue *queue;
605 struct amdgpu_db_info db_info;
606 uint64_t index;
607 int priority;
608 u32 qid;
609 int r;
610
611 priority =
612 (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK)
613 >> AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
614 r = amdgpu_userq_priority_permit(filp, priority);
615 if (r)
616 return r;
617
618 r = pm_runtime_resume_and_get(adev_to_drm(adev)->dev);
619 if (r < 0) {
620 drm_file_err(uq_mgr->file, "pm_runtime_resume_and_get() failed for userqueue create\n");
621 return r;
622 }
623
624 uq_funcs = adev->userq_funcs[args->in.ip_type];
625 if (!uq_funcs) {
626 r = -EINVAL;
627 goto err_pm_runtime;
628 }
629
630 queue = kzalloc_obj(struct amdgpu_usermode_queue);
631 if (!queue) {
632 r = -ENOMEM;
633 goto err_pm_runtime;
634 }
635
636 kref_init(&queue->refcount);
637 queue->doorbell_handle = args->in.doorbell_handle;
638 queue->queue_type = args->in.ip_type;
639 queue->vm = &fpriv->vm;
640 queue->priority = priority;
641 queue->userq_mgr = uq_mgr;
642 INIT_DELAYED_WORK(&queue->hang_detect_work,
643 amdgpu_userq_hang_detect_work);
644
645 r = amdgpu_userq_fence_driver_alloc(adev, &queue->fence_drv);
646 if (r)
647 goto free_queue;
648
649 xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC);
650 mutex_init(&queue->fence_drv_lock);
651 /* Make sure the queue can actually run with those virtual addresses. */
652 r = amdgpu_bo_reserve(fpriv->vm.root.bo, false);
653 if (r)
654 goto free_fence_drv;
655
656 if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va,
657 args->in.queue_size,
658 &queue->userq_vas.va.queue_rb) ||
659 amdgpu_userq_input_va_validate(adev, queue, args->in.rptr_va,
660 AMDGPU_GPU_PAGE_SIZE,
661 &queue->userq_vas.va.rptr) ||
662 amdgpu_userq_input_va_validate(adev, queue, args->in.wptr_va,
663 AMDGPU_GPU_PAGE_SIZE,
664 &queue->userq_vas.va.wptr)) {
665 r = -EINVAL;
666 amdgpu_bo_unreserve(fpriv->vm.root.bo);
667 goto free_fence_drv;
668 }
669 amdgpu_bo_unreserve(fpriv->vm.root.bo);
670
671 /* Convert relative doorbell offset into absolute doorbell index */
672 db_info.queue_type = queue->queue_type;
673 db_info.doorbell_handle = queue->doorbell_handle;
674 db_info.db_obj = &queue->db_obj;
675 db_info.doorbell_offset = args->in.doorbell_offset;
676 r = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp, &index);
677 if (r) {
678 drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n");
679 goto free_fence_drv;
680 }
681
682 queue->doorbell_index = index;
683 r = uq_funcs->mqd_create(queue, &args->in);
684 if (r) {
685 drm_file_err(uq_mgr->file, "Failed to create Queue\n");
686 goto clean_doorbell_bo;
687 }
688
689 /* Update VM owner at userq submit-time for page-fault attribution. */
690 amdgpu_vm_set_task_info(&fpriv->vm);
691
692 r = xa_insert_irq(&adev->userq_doorbell_xa, index, queue,
693 GFP_KERNEL);
694 if (r)
695 goto clean_mqd;
696
697 amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
698
699 /* don't map the queue if scheduling is halted */
700 if (!adev->userq_halt_for_enforce_isolation ||
701 ((queue->queue_type != AMDGPU_HW_IP_GFX) &&
702 (queue->queue_type != AMDGPU_HW_IP_COMPUTE))) {
703 /* Serialize the map against an in-progress GPU reset (MES is
704 * unresponsive during recovery), matching amdgpu_userq_cleanup().
705 */
706 down_read(&adev->reset_domain->sem);
707 r = amdgpu_userq_map_helper(queue);
708 up_read(&adev->reset_domain->sem);
709 if (r) {
710 drm_file_err(uq_mgr->file, "Failed to map Queue\n");
711 mutex_unlock(&uq_mgr->userq_mutex);
712 goto erase_doorbell;
713 }
714 }
715
716 atomic_inc(&uq_mgr->userq_count[queue->queue_type]);
717 mutex_unlock(&uq_mgr->userq_mutex);
718
719 r = xa_alloc(&uq_mgr->userq_xa, &qid, queue,
720 XA_LIMIT(1, AMDGPU_MAX_USERQ_COUNT),
721 GFP_KERNEL);
722 if (r) {
723 /*
724 * This drops the last reference which should take care of
725 * all cleanup.
726 */
727 amdgpu_userq_put(queue);
728 return r;
729 }
730
731 amdgpu_debugfs_userq_init(filp, queue, qid);
732 args->out.queue_id = qid;
733 return 0;
734
735 erase_doorbell:
736 xa_erase_irq(&adev->userq_doorbell_xa, index);
737 clean_mqd:
738 uq_funcs->mqd_destroy(queue);
739 clean_doorbell_bo:
740 amdgpu_bo_reserve(queue->db_obj.obj, true);
741 amdgpu_bo_unpin(queue->db_obj.obj);
742 amdgpu_bo_unreserve(queue->db_obj.obj);
743 amdgpu_bo_unref(&queue->db_obj.obj);
744 free_fence_drv:
745 amdgpu_userq_fence_driver_free(queue);
746 free_queue:
747 kfree(queue);
748 err_pm_runtime:
749 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
750 return r;
751 }
752
amdgpu_userq_input_args_validate(struct drm_device * dev,union drm_amdgpu_userq * args,struct drm_file * filp)753 static int amdgpu_userq_input_args_validate(struct drm_device *dev,
754 union drm_amdgpu_userq *args,
755 struct drm_file *filp)
756 {
757 struct amdgpu_device *adev = drm_to_adev(dev);
758
759 switch (args->in.op) {
760 case AMDGPU_USERQ_OP_CREATE:
761 if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
762 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))
763 return -EINVAL;
764 /* Usermode queues are only supported for GFX IP as of now */
765 if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
766 args->in.ip_type != AMDGPU_HW_IP_DMA &&
767 args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
768 drm_file_err(filp, "Usermode queue doesn't support IP type %u\n",
769 args->in.ip_type);
770 return -EINVAL;
771 }
772
773 if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
774 (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
775 (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
776 !amdgpu_is_tmz(adev)) {
777 drm_file_err(filp, "Secure only supported on GFX/Compute queues\n");
778 return -EINVAL;
779 }
780
781 if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
782 args->in.queue_va == 0 ||
783 args->in.queue_size == 0) {
784 drm_file_err(filp, "invalidate userq queue va or size\n");
785 return -EINVAL;
786 }
787
788 if (!is_power_of_2(args->in.queue_size)) {
789 drm_file_err(filp, "Queue size must be a power of 2\n");
790 return -EINVAL;
791 }
792
793 if (args->in.queue_size < AMDGPU_GPU_PAGE_SIZE) {
794 drm_file_err(filp, "Queue size smaller than AMDGPU_GPU_PAGE_SIZE\n");
795 return -EINVAL;
796 }
797
798 if (!args->in.wptr_va || !args->in.rptr_va) {
799 drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
800 return -EINVAL;
801 }
802 break;
803 case AMDGPU_USERQ_OP_FREE:
804 if (args->in.ip_type ||
805 args->in.doorbell_handle ||
806 args->in.doorbell_offset ||
807 args->in.flags ||
808 args->in.queue_va ||
809 args->in.queue_size ||
810 args->in.rptr_va ||
811 args->in.wptr_va ||
812 args->in.mqd ||
813 args->in.mqd_size)
814 return -EINVAL;
815 break;
816 default:
817 return -EINVAL;
818 }
819
820 return 0;
821 }
822
amdgpu_userq_enabled(struct drm_device * dev)823 bool amdgpu_userq_enabled(struct drm_device *dev)
824 {
825 struct amdgpu_device *adev = drm_to_adev(dev);
826 int i;
827
828 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {
829 if (adev->userq_funcs[i])
830 return true;
831 }
832
833 return false;
834 }
835
amdgpu_userq_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)836 int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
837 struct drm_file *filp)
838 {
839 union drm_amdgpu_userq *args = data;
840 struct amdgpu_fpriv *fpriv = filp->driver_priv;
841 struct amdgpu_usermode_queue *queue;
842 int r = 0;
843
844 if (!amdgpu_userq_enabled(dev))
845 return -ENOTSUPP;
846
847 if (amdgpu_userq_input_args_validate(dev, args, filp) < 0)
848 return -EINVAL;
849
850 switch (args->in.op) {
851 case AMDGPU_USERQ_OP_CREATE:
852 r = amdgpu_userq_create(filp, args);
853 if (r)
854 drm_file_err(filp, "Failed to create usermode queue\n");
855 break;
856
857 case AMDGPU_USERQ_OP_FREE: {
858 xa_lock(&fpriv->userq_mgr.userq_xa);
859 queue = __xa_erase(&fpriv->userq_mgr.userq_xa, args->in.queue_id);
860 xa_unlock(&fpriv->userq_mgr.userq_xa);
861 if (!queue)
862 return -ENOENT;
863
864 amdgpu_userq_put(queue);
865 break;
866 }
867
868 default:
869 drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
870 return -EINVAL;
871 }
872
873 return r;
874 }
875
876 static int
amdgpu_userq_restore_all(struct amdgpu_userq_mgr * uq_mgr)877 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
878 {
879 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
880 struct amdgpu_vm *vm = &fpriv->vm;
881 struct amdgpu_usermode_queue *queue;
882 unsigned long queue_id;
883 int ret = 0, r;
884
885
886 if (amdgpu_bo_reserve(vm->root.bo, false))
887 return false;
888
889 mutex_lock(&uq_mgr->userq_mutex);
890 /* Resume all the queues for this process */
891 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
892
893 if (!amdgpu_userq_buffer_vas_mapped(queue)) {
894 drm_file_err(uq_mgr->file,
895 "trying restore queue without va mapping\n");
896 queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
897 continue;
898 }
899
900 r = amdgpu_userq_map_helper(queue);
901 if (r)
902 ret = r;
903
904 }
905 mutex_unlock(&uq_mgr->userq_mutex);
906 amdgpu_bo_unreserve(vm->root.bo);
907
908 if (ret)
909 drm_file_err(uq_mgr->file,
910 "Failed to map all the queues, restore failed ret=%d\n", ret);
911 return ret;
912 }
913
amdgpu_userq_validate_vm(void * param,struct amdgpu_bo * bo)914 static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo)
915 {
916 struct ttm_operation_ctx ctx = { false, false };
917
918 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
919 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
920 }
921
922 /* Handle all BOs on the invalidated list, validate them and update the PTs */
923 static int
amdgpu_userq_bo_validate(struct amdgpu_device * adev,struct drm_exec * exec,struct amdgpu_vm * vm)924 amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,
925 struct amdgpu_vm *vm)
926 {
927 struct ttm_operation_ctx ctx = { false, false };
928 struct amdgpu_bo_va *bo_va;
929 struct amdgpu_bo *bo;
930 int ret;
931
932 spin_lock(&vm->individual_lock);
933 while (!list_empty(&vm->always_valid.evicted)) {
934 bo_va = list_first_entry(&vm->always_valid.evicted,
935 struct amdgpu_bo_va,
936 base.vm_status);
937 spin_unlock(&vm->individual_lock);
938
939 bo = bo_va->base.bo;
940 ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 2);
941 if (unlikely(ret))
942 return ret;
943
944 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
945 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
946 if (ret)
947 return ret;
948
949 /* This moves the bo_va to the idle list */
950 ret = amdgpu_vm_bo_update(adev, bo_va, false);
951 if (ret)
952 return ret;
953
954 spin_lock(&vm->individual_lock);
955 }
956 spin_unlock(&vm->individual_lock);
957
958 return 0;
959 }
960
961 /* Make sure the whole VM is ready to be used */
962 static int
amdgpu_userq_vm_validate(struct amdgpu_userq_mgr * uq_mgr)963 amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)
964 {
965 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
966 bool invalidated = false, new_addition = false;
967 struct ttm_operation_ctx ctx = { true, false };
968 struct amdgpu_device *adev = uq_mgr->adev;
969 struct amdgpu_hmm_range *range;
970 struct amdgpu_vm *vm = &fpriv->vm;
971 unsigned long key, tmp_key;
972 struct amdgpu_bo_va *bo_va;
973 struct amdgpu_usermode_queue *queue;
974 struct amdgpu_bo *bo;
975 struct drm_exec exec;
976 struct xarray xa;
977 int ret;
978
979 xa_init(&xa);
980
981 retry_lock:
982 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
983 drm_exec_until_all_locked(&exec) {
984 ret = amdgpu_vm_lock_pd(vm, &exec, 1);
985 drm_exec_retry_on_contention(&exec);
986 if (unlikely(ret))
987 goto unlock_all;
988
989 ret = amdgpu_vm_lock_individual(vm, &exec, TTM_NUM_MOVE_FENCES + 1);
990 drm_exec_retry_on_contention(&exec);
991 if (unlikely(ret))
992 goto unlock_all;
993
994 /* This validates PDs, PTs and per VM BOs */
995 ret = amdgpu_vm_validate(adev, vm, NULL,
996 amdgpu_userq_validate_vm,
997 NULL);
998 if (unlikely(ret))
999 goto unlock_all;
1000
1001 /* This locks and validates the remaining evicted BOs */
1002 ret = amdgpu_userq_bo_validate(adev, &exec, vm);
1003 drm_exec_retry_on_contention(&exec);
1004 if (unlikely(ret))
1005 goto unlock_all;
1006 }
1007
1008 if (invalidated) {
1009 xa_for_each(&xa, tmp_key, range) {
1010 bo = range->bo;
1011 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
1012 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
1013 if (ret)
1014 goto unlock_all;
1015
1016 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
1017
1018 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
1019 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
1020 if (ret)
1021 goto unlock_all;
1022 }
1023 invalidated = false;
1024 }
1025
1026 ret = amdgpu_vm_handle_moved(adev, vm, NULL);
1027 if (ret)
1028 goto unlock_all;
1029
1030 key = 0;
1031 /* Validate User Ptr BOs */
1032 list_for_each_entry(bo_va, &vm->always_valid.idle, base.vm_status) {
1033 bo = bo_va->base.bo;
1034 if (!bo)
1035 continue;
1036
1037 if (!amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
1038 continue;
1039
1040 range = xa_load(&xa, key);
1041 if (range && range->bo != bo) {
1042 xa_erase(&xa, key);
1043 amdgpu_hmm_range_free(range);
1044 range = NULL;
1045 }
1046
1047 if (!range) {
1048 range = amdgpu_hmm_range_alloc(bo);
1049 if (!range) {
1050 ret = -ENOMEM;
1051 goto unlock_all;
1052 }
1053
1054 xa_store(&xa, key, range, GFP_KERNEL);
1055 new_addition = true;
1056 }
1057 key++;
1058 }
1059
1060 if (new_addition) {
1061 drm_exec_fini(&exec);
1062 xa_for_each(&xa, tmp_key, range) {
1063 if (!range)
1064 continue;
1065 bo = range->bo;
1066 ret = amdgpu_ttm_tt_get_user_pages(bo, range);
1067 if (ret)
1068 goto free_ranges;
1069 }
1070
1071 invalidated = true;
1072 new_addition = false;
1073 goto retry_lock;
1074 }
1075
1076 ret = amdgpu_vm_update_pdes(adev, vm, false);
1077 if (ret)
1078 goto unlock_all;
1079
1080 /*
1081 * We need to wait for all VM updates to finish before restarting the
1082 * queues. Using the idle list like that is now ok since everything is
1083 * locked in place.
1084 */
1085 list_for_each_entry(bo_va, &vm->always_valid.idle, base.vm_status)
1086 dma_fence_wait(bo_va->last_pt_update, false);
1087 dma_fence_wait(vm->last_update, false);
1088
1089 xa_for_each(&uq_mgr->userq_xa, tmp_key, queue) {
1090 bo = queue->wptr_obj.obj;
1091 if (!bo) {
1092 ret = -EINVAL;
1093 goto unlock_all;
1094 }
1095
1096 ret = amdgpu_ttm_alloc_gart(&bo->tbo);
1097 if (unlikely(ret)) {
1098 drm_file_err(uq_mgr->file,
1099 "failed to bind wptr bo to gart on resume, qid=%lu ret=%d\n",
1100 tmp_key, ret);
1101 goto unlock_all;
1102 }
1103
1104 queue->wptr_obj.gpu_addr = amdgpu_bo_gpu_offset(bo);
1105 }
1106
1107 ret = amdgpu_evf_mgr_rearm(&fpriv->evf_mgr, &exec);
1108 if (ret)
1109 drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n");
1110
1111 unlock_all:
1112 drm_exec_fini(&exec);
1113 free_ranges:
1114 xa_for_each(&xa, tmp_key, range) {
1115 if (!range)
1116 continue;
1117 bo = range->bo;
1118 amdgpu_hmm_range_free(range);
1119 }
1120 xa_destroy(&xa);
1121 return ret;
1122 }
1123
amdgpu_userq_restore_worker(struct work_struct * work)1124 static void amdgpu_userq_restore_worker(struct work_struct *work)
1125 {
1126 struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
1127 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
1128 struct dma_fence *ev_fence;
1129 int ret;
1130
1131 ev_fence = amdgpu_evf_mgr_get_fence(&fpriv->evf_mgr);
1132 if (!dma_fence_is_signaled(ev_fence))
1133 goto put_fence;
1134
1135 ret = amdgpu_userq_vm_validate(uq_mgr);
1136 if (ret) {
1137 drm_file_err(uq_mgr->file, "Failed to validate BOs to restore ret=%d\n", ret);
1138 goto put_fence;
1139 }
1140
1141 amdgpu_userq_restore_all(uq_mgr);
1142
1143 put_fence:
1144 dma_fence_put(ev_fence);
1145 }
1146
1147 static int
amdgpu_userq_evict_all(struct amdgpu_userq_mgr * uq_mgr)1148 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
1149 {
1150 struct amdgpu_usermode_queue *queue;
1151 unsigned long queue_id;
1152 int ret = 0, r;
1153
1154 /* Try to unmap all the queues in this process ctx */
1155 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
1156 r = amdgpu_userq_unmap_helper(queue);
1157 if (r)
1158 ret = r;
1159 }
1160
1161 if (ret) {
1162 drm_file_err(uq_mgr->file,
1163 "Couldn't unmap all the queues, eviction failed ret=%d\n", ret);
1164 amdgpu_reset_domain_schedule(uq_mgr->adev->reset_domain,
1165 &uq_mgr->reset_work);
1166 flush_work(&uq_mgr->reset_work);
1167 }
1168 return ret;
1169 }
1170
1171 static void
amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr * uq_mgr)1172 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr)
1173 {
1174 struct amdgpu_usermode_queue *queue;
1175 unsigned long queue_id;
1176
1177 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
1178 struct dma_fence *f = queue->last_fence;
1179
1180 if (!f)
1181 continue;
1182
1183 dma_fence_wait(f, false);
1184 }
1185 }
1186
1187 void
amdgpu_userq_evict(struct amdgpu_userq_mgr * uq_mgr)1188 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr)
1189 {
1190 /* Wait for any pending userqueue fence work to finish */
1191 amdgpu_userq_wait_for_signal(uq_mgr);
1192 amdgpu_userq_evict_all(uq_mgr);
1193 }
1194
amdgpu_userq_mgr_init(struct amdgpu_userq_mgr * userq_mgr,struct drm_file * file_priv,struct amdgpu_device * adev)1195 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
1196 struct amdgpu_device *adev)
1197 {
1198 mutex_init(&userq_mgr->userq_mutex);
1199 xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC);
1200 userq_mgr->adev = adev;
1201 userq_mgr->file = file_priv;
1202
1203 INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
1204 INIT_WORK(&userq_mgr->reset_work, amdgpu_userq_mgr_reset_work);
1205 return 0;
1206 }
1207
amdgpu_userq_mgr_cancel_reset_work(struct amdgpu_device * adev)1208 void amdgpu_userq_mgr_cancel_reset_work(struct amdgpu_device *adev)
1209 {
1210 struct xarray *xa = &adev->userq_doorbell_xa;
1211 struct amdgpu_usermode_queue *queue;
1212 unsigned long flags, queue_id;
1213
1214 xa_lock_irqsave(xa, flags);
1215 xa_for_each(xa, queue_id, queue) {
1216 cancel_delayed_work(&queue->hang_detect_work);
1217 cancel_work(&queue->userq_mgr->reset_work);
1218 }
1219 xa_unlock_irqrestore(xa, flags);
1220 }
1221
amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr * userq_mgr)1222 void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr)
1223 {
1224 cancel_delayed_work_sync(&userq_mgr->resume_work);
1225 }
1226
amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr * userq_mgr)1227 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
1228 {
1229 struct amdgpu_usermode_queue *queue;
1230 unsigned long queue_id = 0;
1231
1232 for (;;) {
1233 xa_lock(&userq_mgr->userq_xa);
1234 queue = xa_find(&userq_mgr->userq_xa, &queue_id, ULONG_MAX,
1235 XA_PRESENT);
1236 if (queue)
1237 __xa_erase(&userq_mgr->userq_xa, queue_id);
1238 xa_unlock(&userq_mgr->userq_xa);
1239
1240 if (!queue)
1241 break;
1242
1243 amdgpu_userq_put(queue);
1244 }
1245
1246 xa_destroy(&userq_mgr->userq_xa);
1247
1248 /*
1249 * Drain any in-flight reset_work. By this point all queues are freed
1250 * and userq_count is 0, so if reset_work starts now it exits early.
1251 * We still need to wait in case it was already executing gpu_recover.
1252 */
1253 cancel_work_sync(&userq_mgr->reset_work);
1254
1255 mutex_destroy(&userq_mgr->userq_mutex);
1256 }
1257
amdgpu_userq_suspend(struct amdgpu_device * adev)1258 int amdgpu_userq_suspend(struct amdgpu_device *adev)
1259 {
1260 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1261 struct amdgpu_usermode_queue *queue;
1262 struct amdgpu_userq_mgr *uqm;
1263 unsigned long queue_id;
1264 int r;
1265
1266 if (!ip_mask)
1267 return 0;
1268
1269 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1270 uqm = queue->userq_mgr;
1271 cancel_delayed_work_sync(&uqm->resume_work);
1272 guard(mutex)(&uqm->userq_mutex);
1273 if (adev->in_s0ix)
1274 r = amdgpu_userq_preempt_helper(queue);
1275 else
1276 r = amdgpu_userq_unmap_helper(queue);
1277 if (r)
1278 return r;
1279 }
1280 return 0;
1281 }
1282
amdgpu_userq_resume(struct amdgpu_device * adev)1283 int amdgpu_userq_resume(struct amdgpu_device *adev)
1284 {
1285 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1286 struct amdgpu_usermode_queue *queue;
1287 struct amdgpu_userq_mgr *uqm;
1288 unsigned long queue_id;
1289 int r;
1290
1291 if (!ip_mask)
1292 return 0;
1293
1294 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1295 uqm = queue->userq_mgr;
1296 guard(mutex)(&uqm->userq_mutex);
1297 if (adev->in_s0ix)
1298 r = amdgpu_userq_restore_helper(queue);
1299 else
1300 r = amdgpu_userq_map_helper(queue);
1301 if (r)
1302 return r;
1303 }
1304
1305 return 0;
1306 }
1307
amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device * adev,u32 idx)1308 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
1309 u32 idx)
1310 {
1311 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1312 struct amdgpu_usermode_queue *queue;
1313 struct amdgpu_userq_mgr *uqm;
1314 unsigned long queue_id;
1315 int ret = 0, r;
1316
1317 /* only need to stop gfx/compute */
1318 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
1319 return 0;
1320
1321 if (adev->userq_halt_for_enforce_isolation)
1322 dev_warn(adev->dev, "userq scheduling already stopped!\n");
1323 adev->userq_halt_for_enforce_isolation = true;
1324 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1325 uqm = queue->userq_mgr;
1326 cancel_delayed_work_sync(&uqm->resume_work);
1327 mutex_lock(&uqm->userq_mutex);
1328 if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
1329 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
1330 (queue->xcp_id == idx)) {
1331 r = amdgpu_userq_preempt_helper(queue);
1332 if (r)
1333 ret = r;
1334 }
1335 mutex_unlock(&uqm->userq_mutex);
1336 }
1337
1338 return ret;
1339 }
1340
amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device * adev,u32 idx)1341 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
1342 u32 idx)
1343 {
1344 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1345 struct amdgpu_usermode_queue *queue;
1346 struct amdgpu_userq_mgr *uqm;
1347 unsigned long queue_id;
1348 int ret = 0, r;
1349
1350 /* only need to stop gfx/compute */
1351 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
1352 return 0;
1353
1354 if (!adev->userq_halt_for_enforce_isolation)
1355 dev_warn(adev->dev, "userq scheduling already started!\n");
1356
1357 adev->userq_halt_for_enforce_isolation = false;
1358
1359 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1360 uqm = queue->userq_mgr;
1361 mutex_lock(&uqm->userq_mutex);
1362 if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
1363 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
1364 (queue->xcp_id == idx)) {
1365 r = amdgpu_userq_restore_helper(queue);
1366 if (r)
1367 ret = r;
1368 }
1369 mutex_unlock(&uqm->userq_mutex);
1370 }
1371
1372 return ret;
1373 }
1374
amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device * adev,struct amdgpu_bo_va_mapping * mapping)1375 void amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
1376 struct amdgpu_bo_va_mapping *mapping)
1377 {
1378 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1379 struct amdgpu_bo_va *bo_va = mapping->bo_va;
1380 struct dma_resv *resv = bo_va->base.bo->tbo.base.resv;
1381
1382 if (!ip_mask)
1383 return;
1384
1385 /**
1386 * The userq VA mapping reservation should include the eviction fence.
1387 * Note: The eviction fence may be attached to different BOs and this
1388 * unmap is only for one kind of userq VAs, so at this point suppose
1389 * the eviction fence is always unsignaled.
1390 */
1391 dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
1392 false, MAX_SCHEDULE_TIMEOUT);
1393 }
1394
amdgpu_userq_pre_reset(struct amdgpu_device * adev)1395 void amdgpu_userq_pre_reset(struct amdgpu_device *adev)
1396 {
1397 const struct amdgpu_userq_funcs *userq_funcs;
1398 struct amdgpu_usermode_queue *queue;
1399 unsigned long queue_id;
1400
1401 /* TODO: We probably need a new lock for the queue state */
1402 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1403 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) {
1404 userq_funcs = adev->userq_funcs[queue->queue_type];
1405 userq_funcs->unmap(queue);
1406 /* just mark all queues as hung at this point.
1407 * if unmap succeeds, we could map again
1408 * in amdgpu_userq_post_reset() if vram is not lost
1409 */
1410 queue->state = AMDGPU_USERQ_STATE_HUNG;
1411 }
1412 /* Force-complete any pending fence regardless of queue state so
1413 * that eviction/suspend and queue teardown waiters don't block
1414 * forever on a fence that will never signal after the reset.
1415 */
1416 amdgpu_userq_fence_driver_force_completion(queue);
1417 }
1418 }
1419
amdgpu_userq_post_reset(struct amdgpu_device * adev,bool vram_lost)1420 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost)
1421 {
1422 /* if any queue state is AMDGPU_USERQ_STATE_UNMAPPED
1423 * at this point, we should be able to map it again
1424 * and continue if vram is not lost.
1425 */
1426 struct amdgpu_usermode_queue *queue;
1427 const struct amdgpu_userq_funcs *userq_funcs;
1428 unsigned long queue_id;
1429 int r = 0;
1430
1431 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1432 if (queue->state == AMDGPU_USERQ_STATE_HUNG && !vram_lost) {
1433 userq_funcs = adev->userq_funcs[queue->queue_type];
1434 /* Re-map queue */
1435 r = userq_funcs->map(queue);
1436 if (r) {
1437 dev_err(adev->dev, "Failed to remap queue %ld\n", queue_id);
1438 continue;
1439 }
1440 queue->state = AMDGPU_USERQ_STATE_MAPPED;
1441 }
1442 }
1443
1444 return r;
1445 }
1446