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 cancel_delayed_work_sync(&queue->hang_detect_work);
527 uq_funcs->mqd_destroy(queue);
528 queue->userq_mgr = NULL;
529
530 amdgpu_bo_reserve(queue->db_obj.obj, true);
531 amdgpu_bo_unpin(queue->db_obj.obj);
532 amdgpu_bo_unreserve(queue->db_obj.obj);
533 amdgpu_bo_unref(&queue->db_obj.obj);
534
535 kfree(queue);
536
537 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
538
539 return r;
540 }
541
amdgpu_userq_kref_destroy(struct kref * kref)542 static void amdgpu_userq_kref_destroy(struct kref *kref)
543 {
544 int r;
545 struct amdgpu_usermode_queue *queue =
546 container_of(kref, struct amdgpu_usermode_queue, refcount);
547 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
548
549 r = amdgpu_userq_destroy(uq_mgr, queue);
550 if (r)
551 drm_file_err(uq_mgr->file, "Failed to destroy usermode queue %d\n", r);
552 }
553
amdgpu_userq_get(struct amdgpu_userq_mgr * uq_mgr,u32 qid)554 struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr, u32 qid)
555 {
556 struct amdgpu_usermode_queue *queue;
557
558 xa_lock(&uq_mgr->userq_xa);
559 queue = xa_load(&uq_mgr->userq_xa, qid);
560 if (queue)
561 kref_get(&queue->refcount);
562 xa_unlock(&uq_mgr->userq_xa);
563
564 return queue;
565 }
566
amdgpu_userq_put(struct amdgpu_usermode_queue * queue)567 void amdgpu_userq_put(struct amdgpu_usermode_queue *queue)
568 {
569 if (queue)
570 kref_put(&queue->refcount, amdgpu_userq_kref_destroy);
571 }
572
amdgpu_userq_priority_permit(struct drm_file * filp,int priority)573 static int amdgpu_userq_priority_permit(struct drm_file *filp,
574 int priority)
575 {
576 if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH)
577 return 0;
578
579 if (capable(CAP_SYS_NICE))
580 return 0;
581
582 if (drm_is_current_master(filp))
583 return 0;
584
585 return -EACCES;
586 }
587
588 static int
amdgpu_userq_create(struct drm_file * filp,union drm_amdgpu_userq * args)589 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
590 {
591 struct amdgpu_fpriv *fpriv = filp->driver_priv;
592 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
593 struct amdgpu_device *adev = uq_mgr->adev;
594 const struct amdgpu_userq_funcs *uq_funcs;
595 struct amdgpu_usermode_queue *queue;
596 struct amdgpu_db_info db_info;
597 uint64_t index;
598 int priority;
599 u32 qid;
600 int r;
601
602 priority =
603 (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK)
604 >> AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
605 r = amdgpu_userq_priority_permit(filp, priority);
606 if (r)
607 return r;
608
609 r = pm_runtime_resume_and_get(adev_to_drm(adev)->dev);
610 if (r < 0) {
611 drm_file_err(uq_mgr->file, "pm_runtime_resume_and_get() failed for userqueue create\n");
612 return r;
613 }
614
615 uq_funcs = adev->userq_funcs[args->in.ip_type];
616 if (!uq_funcs) {
617 r = -EINVAL;
618 goto err_pm_runtime;
619 }
620
621 queue = kzalloc_obj(struct amdgpu_usermode_queue);
622 if (!queue) {
623 r = -ENOMEM;
624 goto err_pm_runtime;
625 }
626
627 kref_init(&queue->refcount);
628 queue->doorbell_handle = args->in.doorbell_handle;
629 queue->queue_type = args->in.ip_type;
630 queue->vm = &fpriv->vm;
631 queue->priority = priority;
632 queue->userq_mgr = uq_mgr;
633 INIT_DELAYED_WORK(&queue->hang_detect_work,
634 amdgpu_userq_hang_detect_work);
635
636 r = amdgpu_userq_fence_driver_alloc(adev, &queue->fence_drv);
637 if (r)
638 goto free_queue;
639
640 xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC);
641 mutex_init(&queue->fence_drv_lock);
642 /* Make sure the queue can actually run with those virtual addresses. */
643 r = amdgpu_bo_reserve(fpriv->vm.root.bo, false);
644 if (r)
645 goto free_fence_drv;
646
647 if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va,
648 args->in.queue_size,
649 &queue->userq_vas.va.queue_rb) ||
650 amdgpu_userq_input_va_validate(adev, queue, args->in.rptr_va,
651 AMDGPU_GPU_PAGE_SIZE,
652 &queue->userq_vas.va.rptr) ||
653 amdgpu_userq_input_va_validate(adev, queue, args->in.wptr_va,
654 AMDGPU_GPU_PAGE_SIZE,
655 &queue->userq_vas.va.wptr)) {
656 r = -EINVAL;
657 amdgpu_bo_unreserve(fpriv->vm.root.bo);
658 goto free_fence_drv;
659 }
660 amdgpu_bo_unreserve(fpriv->vm.root.bo);
661
662 /* Convert relative doorbell offset into absolute doorbell index */
663 db_info.queue_type = queue->queue_type;
664 db_info.doorbell_handle = queue->doorbell_handle;
665 db_info.db_obj = &queue->db_obj;
666 db_info.doorbell_offset = args->in.doorbell_offset;
667 r = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp, &index);
668 if (r) {
669 drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n");
670 goto free_fence_drv;
671 }
672
673 queue->doorbell_index = index;
674 r = uq_funcs->mqd_create(queue, &args->in);
675 if (r) {
676 drm_file_err(uq_mgr->file, "Failed to create Queue\n");
677 goto clean_doorbell_bo;
678 }
679
680 /* Update VM owner at userq submit-time for page-fault attribution. */
681 amdgpu_vm_set_task_info(&fpriv->vm);
682
683 r = xa_err(xa_store_irq(&adev->userq_doorbell_xa, index, queue,
684 GFP_KERNEL));
685 if (r)
686 goto clean_mqd;
687
688 amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
689
690 /* don't map the queue if scheduling is halted */
691 if (!adev->userq_halt_for_enforce_isolation ||
692 ((queue->queue_type != AMDGPU_HW_IP_GFX) &&
693 (queue->queue_type != AMDGPU_HW_IP_COMPUTE))) {
694 r = amdgpu_userq_map_helper(queue);
695 if (r) {
696 drm_file_err(uq_mgr->file, "Failed to map Queue\n");
697 mutex_unlock(&uq_mgr->userq_mutex);
698 goto erase_doorbell;
699 }
700 }
701
702 atomic_inc(&uq_mgr->userq_count[queue->queue_type]);
703 mutex_unlock(&uq_mgr->userq_mutex);
704
705 r = xa_alloc(&uq_mgr->userq_xa, &qid, queue,
706 XA_LIMIT(1, AMDGPU_MAX_USERQ_COUNT),
707 GFP_KERNEL);
708 if (r) {
709 /*
710 * This drops the last reference which should take care of
711 * all cleanup.
712 */
713 amdgpu_userq_put(queue);
714 return r;
715 }
716
717 amdgpu_debugfs_userq_init(filp, queue, qid);
718 args->out.queue_id = qid;
719 return 0;
720
721 erase_doorbell:
722 xa_erase_irq(&adev->userq_doorbell_xa, index);
723 clean_mqd:
724 uq_funcs->mqd_destroy(queue);
725 clean_doorbell_bo:
726 amdgpu_bo_reserve(queue->db_obj.obj, true);
727 amdgpu_bo_unpin(queue->db_obj.obj);
728 amdgpu_bo_unreserve(queue->db_obj.obj);
729 amdgpu_bo_unref(&queue->db_obj.obj);
730 free_fence_drv:
731 amdgpu_userq_fence_driver_free(queue);
732 free_queue:
733 kfree(queue);
734 err_pm_runtime:
735 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
736 return r;
737 }
738
amdgpu_userq_input_args_validate(struct drm_device * dev,union drm_amdgpu_userq * args,struct drm_file * filp)739 static int amdgpu_userq_input_args_validate(struct drm_device *dev,
740 union drm_amdgpu_userq *args,
741 struct drm_file *filp)
742 {
743 struct amdgpu_device *adev = drm_to_adev(dev);
744
745 switch (args->in.op) {
746 case AMDGPU_USERQ_OP_CREATE:
747 if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
748 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))
749 return -EINVAL;
750 /* Usermode queues are only supported for GFX IP as of now */
751 if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
752 args->in.ip_type != AMDGPU_HW_IP_DMA &&
753 args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
754 drm_file_err(filp, "Usermode queue doesn't support IP type %u\n",
755 args->in.ip_type);
756 return -EINVAL;
757 }
758
759 if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
760 (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
761 (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
762 !amdgpu_is_tmz(adev)) {
763 drm_file_err(filp, "Secure only supported on GFX/Compute queues\n");
764 return -EINVAL;
765 }
766
767 if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
768 args->in.queue_va == 0 ||
769 args->in.queue_size == 0) {
770 drm_file_err(filp, "invalidate userq queue va or size\n");
771 return -EINVAL;
772 }
773
774 if (!is_power_of_2(args->in.queue_size)) {
775 drm_file_err(filp, "Queue size must be a power of 2\n");
776 return -EINVAL;
777 }
778
779 if (args->in.queue_size < AMDGPU_GPU_PAGE_SIZE) {
780 drm_file_err(filp, "Queue size smaller than AMDGPU_GPU_PAGE_SIZE\n");
781 return -EINVAL;
782 }
783
784 if (!args->in.wptr_va || !args->in.rptr_va) {
785 drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
786 return -EINVAL;
787 }
788 break;
789 case AMDGPU_USERQ_OP_FREE:
790 if (args->in.ip_type ||
791 args->in.doorbell_handle ||
792 args->in.doorbell_offset ||
793 args->in.flags ||
794 args->in.queue_va ||
795 args->in.queue_size ||
796 args->in.rptr_va ||
797 args->in.wptr_va ||
798 args->in.mqd ||
799 args->in.mqd_size)
800 return -EINVAL;
801 break;
802 default:
803 return -EINVAL;
804 }
805
806 return 0;
807 }
808
amdgpu_userq_enabled(struct drm_device * dev)809 bool amdgpu_userq_enabled(struct drm_device *dev)
810 {
811 struct amdgpu_device *adev = drm_to_adev(dev);
812 int i;
813
814 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {
815 if (adev->userq_funcs[i])
816 return true;
817 }
818
819 return false;
820 }
821
amdgpu_userq_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)822 int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
823 struct drm_file *filp)
824 {
825 union drm_amdgpu_userq *args = data;
826 struct amdgpu_fpriv *fpriv = filp->driver_priv;
827 struct amdgpu_usermode_queue *queue;
828 int r = 0;
829
830 if (!amdgpu_userq_enabled(dev))
831 return -ENOTSUPP;
832
833 if (amdgpu_userq_input_args_validate(dev, args, filp) < 0)
834 return -EINVAL;
835
836 switch (args->in.op) {
837 case AMDGPU_USERQ_OP_CREATE:
838 r = amdgpu_userq_create(filp, args);
839 if (r)
840 drm_file_err(filp, "Failed to create usermode queue\n");
841 break;
842
843 case AMDGPU_USERQ_OP_FREE: {
844 xa_lock(&fpriv->userq_mgr.userq_xa);
845 queue = __xa_erase(&fpriv->userq_mgr.userq_xa, args->in.queue_id);
846 xa_unlock(&fpriv->userq_mgr.userq_xa);
847 if (!queue)
848 return -ENOENT;
849
850 amdgpu_userq_put(queue);
851 break;
852 }
853
854 default:
855 drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
856 return -EINVAL;
857 }
858
859 return r;
860 }
861
862 static int
amdgpu_userq_restore_all(struct amdgpu_userq_mgr * uq_mgr)863 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
864 {
865 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
866 struct amdgpu_vm *vm = &fpriv->vm;
867 struct amdgpu_usermode_queue *queue;
868 unsigned long queue_id;
869 int ret = 0, r;
870
871
872 if (amdgpu_bo_reserve(vm->root.bo, false))
873 return false;
874
875 mutex_lock(&uq_mgr->userq_mutex);
876 /* Resume all the queues for this process */
877 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
878
879 if (!amdgpu_userq_buffer_vas_mapped(queue)) {
880 drm_file_err(uq_mgr->file,
881 "trying restore queue without va mapping\n");
882 queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
883 continue;
884 }
885
886 r = amdgpu_userq_map_helper(queue);
887 if (r)
888 ret = r;
889
890 }
891 mutex_unlock(&uq_mgr->userq_mutex);
892 amdgpu_bo_unreserve(vm->root.bo);
893
894 if (ret)
895 drm_file_err(uq_mgr->file,
896 "Failed to map all the queues, restore failed ret=%d\n", ret);
897 return ret;
898 }
899
amdgpu_userq_validate_vm(void * param,struct amdgpu_bo * bo)900 static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo)
901 {
902 struct ttm_operation_ctx ctx = { false, false };
903
904 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
905 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
906 }
907
908 /* Handle all BOs on the invalidated list, validate them and update the PTs */
909 static int
amdgpu_userq_bo_validate(struct amdgpu_device * adev,struct drm_exec * exec,struct amdgpu_vm * vm)910 amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,
911 struct amdgpu_vm *vm)
912 {
913 struct ttm_operation_ctx ctx = { false, false };
914 struct amdgpu_bo_va *bo_va;
915 struct amdgpu_bo *bo;
916 int ret;
917
918 spin_lock(&vm->status_lock);
919 while (!list_empty(&vm->invalidated)) {
920 bo_va = list_first_entry(&vm->invalidated,
921 struct amdgpu_bo_va,
922 base.vm_status);
923 spin_unlock(&vm->status_lock);
924
925 bo = bo_va->base.bo;
926 ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 2);
927 if (unlikely(ret))
928 return ret;
929
930 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
931 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
932 if (ret)
933 return ret;
934
935 /* This moves the bo_va to the done list */
936 ret = amdgpu_vm_bo_update(adev, bo_va, false);
937 if (ret)
938 return ret;
939
940 spin_lock(&vm->status_lock);
941 }
942 spin_unlock(&vm->status_lock);
943
944 return 0;
945 }
946
947 /* Make sure the whole VM is ready to be used */
948 static int
amdgpu_userq_vm_validate(struct amdgpu_userq_mgr * uq_mgr)949 amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)
950 {
951 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
952 bool invalidated = false, new_addition = false;
953 struct ttm_operation_ctx ctx = { true, false };
954 struct amdgpu_device *adev = uq_mgr->adev;
955 struct amdgpu_hmm_range *range;
956 struct amdgpu_vm *vm = &fpriv->vm;
957 unsigned long key, tmp_key;
958 struct amdgpu_bo_va *bo_va;
959 struct amdgpu_bo *bo;
960 struct drm_exec exec;
961 struct xarray xa;
962 int ret;
963
964 xa_init(&xa);
965
966 retry_lock:
967 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
968 drm_exec_until_all_locked(&exec) {
969 ret = amdgpu_vm_lock_pd(vm, &exec, 1);
970 drm_exec_retry_on_contention(&exec);
971 if (unlikely(ret))
972 goto unlock_all;
973
974 ret = amdgpu_vm_lock_done_list(vm, &exec, 1);
975 drm_exec_retry_on_contention(&exec);
976 if (unlikely(ret))
977 goto unlock_all;
978
979 /* This validates PDs, PTs and per VM BOs */
980 ret = amdgpu_vm_validate(adev, vm, NULL,
981 amdgpu_userq_validate_vm,
982 NULL);
983 if (unlikely(ret))
984 goto unlock_all;
985
986 /* This locks and validates the remaining evicted BOs */
987 ret = amdgpu_userq_bo_validate(adev, &exec, vm);
988 drm_exec_retry_on_contention(&exec);
989 if (unlikely(ret))
990 goto unlock_all;
991 }
992
993 if (invalidated) {
994 xa_for_each(&xa, tmp_key, range) {
995 bo = range->bo;
996 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
997 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
998 if (ret)
999 goto unlock_all;
1000
1001 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
1002
1003 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
1004 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
1005 if (ret)
1006 goto unlock_all;
1007 }
1008 invalidated = false;
1009 }
1010
1011 ret = amdgpu_vm_handle_moved(adev, vm, NULL);
1012 if (ret)
1013 goto unlock_all;
1014
1015 key = 0;
1016 /* Validate User Ptr BOs */
1017 list_for_each_entry(bo_va, &vm->done, base.vm_status) {
1018 bo = bo_va->base.bo;
1019 if (!bo)
1020 continue;
1021
1022 if (!amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
1023 continue;
1024
1025 range = xa_load(&xa, key);
1026 if (range && range->bo != bo) {
1027 xa_erase(&xa, key);
1028 amdgpu_hmm_range_free(range);
1029 range = NULL;
1030 }
1031
1032 if (!range) {
1033 range = amdgpu_hmm_range_alloc(bo);
1034 if (!range) {
1035 ret = -ENOMEM;
1036 goto unlock_all;
1037 }
1038
1039 xa_store(&xa, key, range, GFP_KERNEL);
1040 new_addition = true;
1041 }
1042 key++;
1043 }
1044
1045 if (new_addition) {
1046 drm_exec_fini(&exec);
1047 xa_for_each(&xa, tmp_key, range) {
1048 if (!range)
1049 continue;
1050 bo = range->bo;
1051 ret = amdgpu_ttm_tt_get_user_pages(bo, range);
1052 if (ret)
1053 goto free_ranges;
1054 }
1055
1056 invalidated = true;
1057 new_addition = false;
1058 goto retry_lock;
1059 }
1060
1061 ret = amdgpu_vm_update_pdes(adev, vm, false);
1062 if (ret)
1063 goto unlock_all;
1064
1065 /*
1066 * We need to wait for all VM updates to finish before restarting the
1067 * queues. Using the done list like that is now ok since everything is
1068 * locked in place.
1069 */
1070 list_for_each_entry(bo_va, &vm->done, base.vm_status)
1071 dma_fence_wait(bo_va->last_pt_update, false);
1072 dma_fence_wait(vm->last_update, false);
1073
1074 ret = amdgpu_evf_mgr_rearm(&fpriv->evf_mgr, &exec);
1075 if (ret)
1076 drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n");
1077
1078 unlock_all:
1079 drm_exec_fini(&exec);
1080 free_ranges:
1081 xa_for_each(&xa, tmp_key, range) {
1082 if (!range)
1083 continue;
1084 bo = range->bo;
1085 amdgpu_hmm_range_free(range);
1086 }
1087 xa_destroy(&xa);
1088 return ret;
1089 }
1090
amdgpu_userq_restore_worker(struct work_struct * work)1091 static void amdgpu_userq_restore_worker(struct work_struct *work)
1092 {
1093 struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
1094 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
1095 struct dma_fence *ev_fence;
1096 int ret;
1097
1098 ev_fence = amdgpu_evf_mgr_get_fence(&fpriv->evf_mgr);
1099 if (!dma_fence_is_signaled(ev_fence))
1100 goto put_fence;
1101
1102 ret = amdgpu_userq_vm_validate(uq_mgr);
1103 if (ret) {
1104 drm_file_err(uq_mgr->file, "Failed to validate BOs to restore ret=%d\n", ret);
1105 goto put_fence;
1106 }
1107
1108 amdgpu_userq_restore_all(uq_mgr);
1109
1110 put_fence:
1111 dma_fence_put(ev_fence);
1112 }
1113
1114 static int
amdgpu_userq_evict_all(struct amdgpu_userq_mgr * uq_mgr)1115 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
1116 {
1117 struct amdgpu_usermode_queue *queue;
1118 unsigned long queue_id;
1119 int ret = 0, r;
1120
1121 /* Try to unmap all the queues in this process ctx */
1122 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
1123 r = amdgpu_userq_unmap_helper(queue);
1124 if (r)
1125 ret = r;
1126 }
1127
1128 if (ret) {
1129 drm_file_err(uq_mgr->file,
1130 "Couldn't unmap all the queues, eviction failed ret=%d\n", ret);
1131 amdgpu_reset_domain_schedule(uq_mgr->adev->reset_domain,
1132 &uq_mgr->reset_work);
1133 flush_work(&uq_mgr->reset_work);
1134 }
1135 return ret;
1136 }
1137
1138 static void
amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr * uq_mgr)1139 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr)
1140 {
1141 struct amdgpu_usermode_queue *queue;
1142 unsigned long queue_id;
1143
1144 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
1145 struct dma_fence *f = queue->last_fence;
1146
1147 if (!f)
1148 continue;
1149
1150 dma_fence_wait(f, false);
1151 }
1152 }
1153
1154 void
amdgpu_userq_evict(struct amdgpu_userq_mgr * uq_mgr)1155 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr)
1156 {
1157 /* Wait for any pending userqueue fence work to finish */
1158 amdgpu_userq_wait_for_signal(uq_mgr);
1159 amdgpu_userq_evict_all(uq_mgr);
1160 }
1161
amdgpu_userq_mgr_init(struct amdgpu_userq_mgr * userq_mgr,struct drm_file * file_priv,struct amdgpu_device * adev)1162 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
1163 struct amdgpu_device *adev)
1164 {
1165 mutex_init(&userq_mgr->userq_mutex);
1166 xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC);
1167 userq_mgr->adev = adev;
1168 userq_mgr->file = file_priv;
1169
1170 INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
1171 INIT_WORK(&userq_mgr->reset_work, amdgpu_userq_mgr_reset_work);
1172 return 0;
1173 }
1174
amdgpu_userq_mgr_cancel_reset_work(struct amdgpu_device * adev)1175 void amdgpu_userq_mgr_cancel_reset_work(struct amdgpu_device *adev)
1176 {
1177 struct xarray *xa = &adev->userq_doorbell_xa;
1178 struct amdgpu_usermode_queue *queue;
1179 unsigned long flags, queue_id;
1180
1181 xa_lock_irqsave(xa, flags);
1182 xa_for_each(xa, queue_id, queue) {
1183 cancel_delayed_work(&queue->hang_detect_work);
1184 cancel_work(&queue->userq_mgr->reset_work);
1185 }
1186 xa_unlock_irqrestore(xa, flags);
1187 }
1188
amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr * userq_mgr)1189 void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr)
1190 {
1191 cancel_delayed_work_sync(&userq_mgr->resume_work);
1192 }
1193
amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr * userq_mgr)1194 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
1195 {
1196 struct amdgpu_usermode_queue *queue;
1197 unsigned long queue_id = 0;
1198
1199 for (;;) {
1200 xa_lock(&userq_mgr->userq_xa);
1201 queue = xa_find(&userq_mgr->userq_xa, &queue_id, ULONG_MAX,
1202 XA_PRESENT);
1203 if (queue)
1204 __xa_erase(&userq_mgr->userq_xa, queue_id);
1205 xa_unlock(&userq_mgr->userq_xa);
1206
1207 if (!queue)
1208 break;
1209
1210 amdgpu_userq_put(queue);
1211 }
1212
1213 xa_destroy(&userq_mgr->userq_xa);
1214
1215 /*
1216 * Drain any in-flight reset_work. By this point all queues are freed
1217 * and userq_count is 0, so if reset_work starts now it exits early.
1218 * We still need to wait in case it was already executing gpu_recover.
1219 */
1220 cancel_work_sync(&userq_mgr->reset_work);
1221
1222 mutex_destroy(&userq_mgr->userq_mutex);
1223 }
1224
amdgpu_userq_suspend(struct amdgpu_device * adev)1225 int amdgpu_userq_suspend(struct amdgpu_device *adev)
1226 {
1227 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1228 struct amdgpu_usermode_queue *queue;
1229 struct amdgpu_userq_mgr *uqm;
1230 unsigned long queue_id;
1231 int r;
1232
1233 if (!ip_mask)
1234 return 0;
1235
1236 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1237 uqm = queue->userq_mgr;
1238 cancel_delayed_work_sync(&uqm->resume_work);
1239 guard(mutex)(&uqm->userq_mutex);
1240 if (adev->in_s0ix)
1241 r = amdgpu_userq_preempt_helper(queue);
1242 else
1243 r = amdgpu_userq_unmap_helper(queue);
1244 if (r)
1245 return r;
1246 }
1247 return 0;
1248 }
1249
amdgpu_userq_resume(struct amdgpu_device * adev)1250 int amdgpu_userq_resume(struct amdgpu_device *adev)
1251 {
1252 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1253 struct amdgpu_usermode_queue *queue;
1254 struct amdgpu_userq_mgr *uqm;
1255 unsigned long queue_id;
1256 int r;
1257
1258 if (!ip_mask)
1259 return 0;
1260
1261 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1262 uqm = queue->userq_mgr;
1263 guard(mutex)(&uqm->userq_mutex);
1264 if (adev->in_s0ix)
1265 r = amdgpu_userq_restore_helper(queue);
1266 else
1267 r = amdgpu_userq_map_helper(queue);
1268 if (r)
1269 return r;
1270 }
1271
1272 return 0;
1273 }
1274
amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device * adev,u32 idx)1275 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
1276 u32 idx)
1277 {
1278 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1279 struct amdgpu_usermode_queue *queue;
1280 struct amdgpu_userq_mgr *uqm;
1281 unsigned long queue_id;
1282 int ret = 0, r;
1283
1284 /* only need to stop gfx/compute */
1285 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
1286 return 0;
1287
1288 if (adev->userq_halt_for_enforce_isolation)
1289 dev_warn(adev->dev, "userq scheduling already stopped!\n");
1290 adev->userq_halt_for_enforce_isolation = true;
1291 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1292 uqm = queue->userq_mgr;
1293 cancel_delayed_work_sync(&uqm->resume_work);
1294 mutex_lock(&uqm->userq_mutex);
1295 if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
1296 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
1297 (queue->xcp_id == idx)) {
1298 r = amdgpu_userq_preempt_helper(queue);
1299 if (r)
1300 ret = r;
1301 }
1302 mutex_unlock(&uqm->userq_mutex);
1303 }
1304
1305 return ret;
1306 }
1307
amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device * adev,u32 idx)1308 int amdgpu_userq_start_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 started!\n");
1323
1324 adev->userq_halt_for_enforce_isolation = false;
1325
1326 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1327 uqm = queue->userq_mgr;
1328 mutex_lock(&uqm->userq_mutex);
1329 if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
1330 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
1331 (queue->xcp_id == idx)) {
1332 r = amdgpu_userq_restore_helper(queue);
1333 if (r)
1334 ret = r;
1335 }
1336 mutex_unlock(&uqm->userq_mutex);
1337 }
1338
1339 return ret;
1340 }
1341
amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device * adev,struct amdgpu_bo_va_mapping * mapping)1342 void amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
1343 struct amdgpu_bo_va_mapping *mapping)
1344 {
1345 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1346 struct amdgpu_bo_va *bo_va = mapping->bo_va;
1347 struct dma_resv *resv = bo_va->base.bo->tbo.base.resv;
1348
1349 if (!ip_mask)
1350 return;
1351
1352 /**
1353 * The userq VA mapping reservation should include the eviction fence.
1354 * Note: The eviction fence may be attached to different BOs and this
1355 * unmap is only for one kind of userq VAs, so at this point suppose
1356 * the eviction fence is always unsignaled.
1357 */
1358 dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
1359 false, MAX_SCHEDULE_TIMEOUT);
1360 }
1361
amdgpu_userq_pre_reset(struct amdgpu_device * adev)1362 void amdgpu_userq_pre_reset(struct amdgpu_device *adev)
1363 {
1364 const struct amdgpu_userq_funcs *userq_funcs;
1365 struct amdgpu_usermode_queue *queue;
1366 unsigned long queue_id;
1367
1368 /* TODO: We probably need a new lock for the queue state */
1369 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1370 if (queue->state != AMDGPU_USERQ_STATE_MAPPED)
1371 continue;
1372
1373 userq_funcs = adev->userq_funcs[queue->queue_type];
1374 userq_funcs->unmap(queue);
1375 /* just mark all queues as hung at this point.
1376 * if unmap succeeds, we could map again
1377 * in amdgpu_userq_post_reset() if vram is not lost
1378 */
1379 queue->state = AMDGPU_USERQ_STATE_HUNG;
1380 amdgpu_userq_fence_driver_force_completion(queue);
1381 }
1382 }
1383
amdgpu_userq_post_reset(struct amdgpu_device * adev,bool vram_lost)1384 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost)
1385 {
1386 /* if any queue state is AMDGPU_USERQ_STATE_UNMAPPED
1387 * at this point, we should be able to map it again
1388 * and continue if vram is not lost.
1389 */
1390 struct amdgpu_usermode_queue *queue;
1391 const struct amdgpu_userq_funcs *userq_funcs;
1392 unsigned long queue_id;
1393 int r = 0;
1394
1395 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1396 if (queue->state == AMDGPU_USERQ_STATE_HUNG && !vram_lost) {
1397 userq_funcs = adev->userq_funcs[queue->queue_type];
1398 /* Re-map queue */
1399 r = userq_funcs->map(queue);
1400 if (r) {
1401 dev_err(adev->dev, "Failed to remap queue %ld\n", queue_id);
1402 continue;
1403 }
1404 queue->state = AMDGPU_USERQ_STATE_MAPPED;
1405 }
1406 }
1407
1408 return r;
1409 }
1410