Loading...
1/*
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23#include "amdgpu_ids.h"
24
25#include <linux/idr.h>
26#include <linux/dma-fence-array.h>
27
28
29#include "amdgpu.h"
30#include "amdgpu_trace.h"
31
32/*
33 * PASID manager
34 *
35 * PASIDs are global address space identifiers that can be shared
36 * between the GPU, an IOMMU and the driver. VMs on different devices
37 * may use the same PASID if they share the same address
38 * space. Therefore PASIDs are allocated using a global IDA. VMs are
39 * looked up from the PASID per amdgpu_device.
40 */
41static DEFINE_IDA(amdgpu_pasid_ida);
42
43/* Helper to free pasid from a fence callback */
44struct amdgpu_pasid_cb {
45 struct dma_fence_cb cb;
46 u32 pasid;
47};
48
49/**
50 * amdgpu_pasid_alloc - Allocate a PASID
51 * @bits: Maximum width of the PASID in bits, must be at least 1
52 *
53 * Allocates a PASID of the given width while keeping smaller PASIDs
54 * available if possible.
55 *
56 * Returns a positive integer on success. Returns %-EINVAL if bits==0.
57 * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
58 * memory allocation failure.
59 */
60int amdgpu_pasid_alloc(unsigned int bits)
61{
62 int pasid = -EINVAL;
63
64 for (bits = min(bits, 31U); bits > 0; bits--) {
65 pasid = ida_simple_get(&amdgpu_pasid_ida,
66 1U << (bits - 1), 1U << bits,
67 GFP_KERNEL);
68 if (pasid != -ENOSPC)
69 break;
70 }
71
72 if (pasid >= 0)
73 trace_amdgpu_pasid_allocated(pasid);
74
75 return pasid;
76}
77
78/**
79 * amdgpu_pasid_free - Free a PASID
80 * @pasid: PASID to free
81 */
82void amdgpu_pasid_free(u32 pasid)
83{
84 trace_amdgpu_pasid_freed(pasid);
85 ida_simple_remove(&amdgpu_pasid_ida, pasid);
86}
87
88static void amdgpu_pasid_free_cb(struct dma_fence *fence,
89 struct dma_fence_cb *_cb)
90{
91 struct amdgpu_pasid_cb *cb =
92 container_of(_cb, struct amdgpu_pasid_cb, cb);
93
94 amdgpu_pasid_free(cb->pasid);
95 dma_fence_put(fence);
96 kfree(cb);
97}
98
99/**
100 * amdgpu_pasid_free_delayed - free pasid when fences signal
101 *
102 * @resv: reservation object with the fences to wait for
103 * @pasid: pasid to free
104 *
105 * Free the pasid only after all the fences in resv are signaled.
106 */
107void amdgpu_pasid_free_delayed(struct dma_resv *resv,
108 u32 pasid)
109{
110 struct amdgpu_pasid_cb *cb;
111 struct dma_fence *fence;
112 int r;
113
114 r = dma_resv_get_singleton(resv, DMA_RESV_USAGE_BOOKKEEP, &fence);
115 if (r)
116 goto fallback;
117
118 if (!fence) {
119 amdgpu_pasid_free(pasid);
120 return;
121 }
122
123 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
124 if (!cb) {
125 /* Last resort when we are OOM */
126 dma_fence_wait(fence, false);
127 dma_fence_put(fence);
128 amdgpu_pasid_free(pasid);
129 } else {
130 cb->pasid = pasid;
131 if (dma_fence_add_callback(fence, &cb->cb,
132 amdgpu_pasid_free_cb))
133 amdgpu_pasid_free_cb(fence, &cb->cb);
134 }
135
136 return;
137
138fallback:
139 /* Not enough memory for the delayed delete, as last resort
140 * block for all the fences to complete.
141 */
142 dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
143 false, MAX_SCHEDULE_TIMEOUT);
144 amdgpu_pasid_free(pasid);
145}
146
147/*
148 * VMID manager
149 *
150 * VMIDs are a per VMHUB identifier for page tables handling.
151 */
152
153/**
154 * amdgpu_vmid_had_gpu_reset - check if reset occured since last use
155 *
156 * @adev: amdgpu_device pointer
157 * @id: VMID structure
158 *
159 * Check if GPU reset occured since last use of the VMID.
160 */
161bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
162 struct amdgpu_vmid *id)
163{
164 return id->current_gpu_reset_count !=
165 atomic_read(&adev->gpu_reset_counter);
166}
167
168/* Check if we need to switch to another set of resources */
169static bool amdgpu_vmid_gds_switch_needed(struct amdgpu_vmid *id,
170 struct amdgpu_job *job)
171{
172 return id->gds_base != job->gds_base ||
173 id->gds_size != job->gds_size ||
174 id->gws_base != job->gws_base ||
175 id->gws_size != job->gws_size ||
176 id->oa_base != job->oa_base ||
177 id->oa_size != job->oa_size;
178}
179
180/* Check if the id is compatible with the job */
181static bool amdgpu_vmid_compatible(struct amdgpu_vmid *id,
182 struct amdgpu_job *job)
183{
184 return id->pd_gpu_addr == job->vm_pd_addr &&
185 !amdgpu_vmid_gds_switch_needed(id, job);
186}
187
188/**
189 * amdgpu_vmid_grab_idle - grab idle VMID
190 *
191 * @vm: vm to allocate id for
192 * @ring: ring we want to submit job to
193 * @idle: resulting idle VMID
194 * @fence: fence to wait for if no id could be grabbed
195 *
196 * Try to find an idle VMID, if none is idle add a fence to wait to the sync
197 * object. Returns -ENOMEM when we are out of memory.
198 */
199static int amdgpu_vmid_grab_idle(struct amdgpu_vm *vm,
200 struct amdgpu_ring *ring,
201 struct amdgpu_vmid **idle,
202 struct dma_fence **fence)
203{
204 struct amdgpu_device *adev = ring->adev;
205 unsigned vmhub = ring->funcs->vmhub;
206 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
207 struct dma_fence **fences;
208 unsigned i;
209
210 if (!dma_fence_is_signaled(ring->vmid_wait)) {
211 *fence = dma_fence_get(ring->vmid_wait);
212 return 0;
213 }
214
215 fences = kmalloc_array(id_mgr->num_ids, sizeof(void *), GFP_KERNEL);
216 if (!fences)
217 return -ENOMEM;
218
219 /* Check if we have an idle VMID */
220 i = 0;
221 list_for_each_entry((*idle), &id_mgr->ids_lru, list) {
222 /* Don't use per engine and per process VMID at the same time */
223 struct amdgpu_ring *r = adev->vm_manager.concurrent_flush ?
224 NULL : ring;
225
226 fences[i] = amdgpu_sync_peek_fence(&(*idle)->active, r);
227 if (!fences[i])
228 break;
229 ++i;
230 }
231
232 /* If we can't find a idle VMID to use, wait till one becomes available */
233 if (&(*idle)->list == &id_mgr->ids_lru) {
234 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
235 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
236 struct dma_fence_array *array;
237 unsigned j;
238
239 *idle = NULL;
240 for (j = 0; j < i; ++j)
241 dma_fence_get(fences[j]);
242
243 array = dma_fence_array_create(i, fences, fence_context,
244 seqno, true);
245 if (!array) {
246 for (j = 0; j < i; ++j)
247 dma_fence_put(fences[j]);
248 kfree(fences);
249 return -ENOMEM;
250 }
251
252 *fence = dma_fence_get(&array->base);
253 dma_fence_put(ring->vmid_wait);
254 ring->vmid_wait = &array->base;
255 return 0;
256 }
257 kfree(fences);
258
259 return 0;
260}
261
262/**
263 * amdgpu_vmid_grab_reserved - try to assign reserved VMID
264 *
265 * @vm: vm to allocate id for
266 * @ring: ring we want to submit job to
267 * @job: job who wants to use the VMID
268 * @id: resulting VMID
269 * @fence: fence to wait for if no id could be grabbed
270 *
271 * Try to assign a reserved VMID.
272 */
273static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
274 struct amdgpu_ring *ring,
275 struct amdgpu_job *job,
276 struct amdgpu_vmid **id,
277 struct dma_fence **fence)
278{
279 struct amdgpu_device *adev = ring->adev;
280 unsigned vmhub = ring->funcs->vmhub;
281 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
282 uint64_t fence_context = adev->fence_context + ring->idx;
283 bool needs_flush = vm->use_cpu_for_update;
284 uint64_t updates = amdgpu_vm_tlb_seq(vm);
285 int r;
286
287 *id = id_mgr->reserved;
288 if ((*id)->owner != vm->immediate.fence_context ||
289 !amdgpu_vmid_compatible(*id, job) ||
290 (*id)->flushed_updates < updates ||
291 !(*id)->last_flush ||
292 ((*id)->last_flush->context != fence_context &&
293 !dma_fence_is_signaled((*id)->last_flush))) {
294 struct dma_fence *tmp;
295
296 /* Don't use per engine and per process VMID at the same time */
297 if (adev->vm_manager.concurrent_flush)
298 ring = NULL;
299
300 /* to prevent one context starved by another context */
301 (*id)->pd_gpu_addr = 0;
302 tmp = amdgpu_sync_peek_fence(&(*id)->active, ring);
303 if (tmp) {
304 *id = NULL;
305 *fence = dma_fence_get(tmp);
306 return 0;
307 }
308 needs_flush = true;
309 }
310
311 /* Good we can use this VMID. Remember this submission as
312 * user of the VMID.
313 */
314 r = amdgpu_sync_fence(&(*id)->active, &job->base.s_fence->finished);
315 if (r)
316 return r;
317
318 job->vm_needs_flush = needs_flush;
319 job->spm_update_needed = true;
320 return 0;
321}
322
323/**
324 * amdgpu_vmid_grab_used - try to reuse a VMID
325 *
326 * @vm: vm to allocate id for
327 * @ring: ring we want to submit job to
328 * @job: job who wants to use the VMID
329 * @id: resulting VMID
330 * @fence: fence to wait for if no id could be grabbed
331 *
332 * Try to reuse a VMID for this submission.
333 */
334static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
335 struct amdgpu_ring *ring,
336 struct amdgpu_job *job,
337 struct amdgpu_vmid **id,
338 struct dma_fence **fence)
339{
340 struct amdgpu_device *adev = ring->adev;
341 unsigned vmhub = ring->funcs->vmhub;
342 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
343 uint64_t fence_context = adev->fence_context + ring->idx;
344 uint64_t updates = amdgpu_vm_tlb_seq(vm);
345 int r;
346
347 job->vm_needs_flush = vm->use_cpu_for_update;
348
349 /* Check if we can use a VMID already assigned to this VM */
350 list_for_each_entry_reverse((*id), &id_mgr->ids_lru, list) {
351 bool needs_flush = vm->use_cpu_for_update;
352
353 /* Check all the prerequisites to using this VMID */
354 if ((*id)->owner != vm->immediate.fence_context)
355 continue;
356
357 if (!amdgpu_vmid_compatible(*id, job))
358 continue;
359
360 if (!(*id)->last_flush ||
361 ((*id)->last_flush->context != fence_context &&
362 !dma_fence_is_signaled((*id)->last_flush)))
363 needs_flush = true;
364
365 if ((*id)->flushed_updates < updates)
366 needs_flush = true;
367
368 if (needs_flush && !adev->vm_manager.concurrent_flush)
369 continue;
370
371 /* Good, we can use this VMID. Remember this submission as
372 * user of the VMID.
373 */
374 r = amdgpu_sync_fence(&(*id)->active,
375 &job->base.s_fence->finished);
376 if (r)
377 return r;
378
379 job->vm_needs_flush |= needs_flush;
380 return 0;
381 }
382
383 *id = NULL;
384 return 0;
385}
386
387/**
388 * amdgpu_vmid_grab - allocate the next free VMID
389 *
390 * @vm: vm to allocate id for
391 * @ring: ring we want to submit job to
392 * @job: job who wants to use the VMID
393 * @fence: fence to wait for if no id could be grabbed
394 *
395 * Allocate an id for the vm, adding fences to the sync obj as necessary.
396 */
397int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
398 struct amdgpu_job *job, struct dma_fence **fence)
399{
400 struct amdgpu_device *adev = ring->adev;
401 unsigned vmhub = ring->funcs->vmhub;
402 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
403 struct amdgpu_vmid *idle = NULL;
404 struct amdgpu_vmid *id = NULL;
405 int r = 0;
406
407 mutex_lock(&id_mgr->lock);
408 r = amdgpu_vmid_grab_idle(vm, ring, &idle, fence);
409 if (r || !idle)
410 goto error;
411
412 if (vm->reserved_vmid[vmhub]) {
413 r = amdgpu_vmid_grab_reserved(vm, ring, job, &id, fence);
414 if (r || !id)
415 goto error;
416 } else {
417 r = amdgpu_vmid_grab_used(vm, ring, job, &id, fence);
418 if (r)
419 goto error;
420
421 if (!id) {
422 /* Still no ID to use? Then use the idle one found earlier */
423 id = idle;
424
425 /* Remember this submission as user of the VMID */
426 r = amdgpu_sync_fence(&id->active,
427 &job->base.s_fence->finished);
428 if (r)
429 goto error;
430
431 job->vm_needs_flush = true;
432 }
433
434 list_move_tail(&id->list, &id_mgr->ids_lru);
435 }
436
437 job->gds_switch_needed = amdgpu_vmid_gds_switch_needed(id, job);
438 if (job->vm_needs_flush) {
439 id->flushed_updates = amdgpu_vm_tlb_seq(vm);
440 dma_fence_put(id->last_flush);
441 id->last_flush = NULL;
442 }
443 job->vmid = id - id_mgr->ids;
444 job->pasid = vm->pasid;
445
446 id->gds_base = job->gds_base;
447 id->gds_size = job->gds_size;
448 id->gws_base = job->gws_base;
449 id->gws_size = job->gws_size;
450 id->oa_base = job->oa_base;
451 id->oa_size = job->oa_size;
452 id->pd_gpu_addr = job->vm_pd_addr;
453 id->owner = vm->immediate.fence_context;
454
455 trace_amdgpu_vm_grab_id(vm, ring, job);
456
457error:
458 mutex_unlock(&id_mgr->lock);
459 return r;
460}
461
462int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
463 struct amdgpu_vm *vm,
464 unsigned vmhub)
465{
466 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
467
468 mutex_lock(&id_mgr->lock);
469 if (vm->reserved_vmid[vmhub])
470 goto unlock;
471
472 ++id_mgr->reserved_use_count;
473 if (!id_mgr->reserved) {
474 struct amdgpu_vmid *id;
475
476 id = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid,
477 list);
478 /* Remove from normal round robin handling */
479 list_del_init(&id->list);
480 id_mgr->reserved = id;
481 }
482 vm->reserved_vmid[vmhub] = true;
483
484unlock:
485 mutex_unlock(&id_mgr->lock);
486 return 0;
487}
488
489void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
490 struct amdgpu_vm *vm,
491 unsigned vmhub)
492{
493 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
494
495 mutex_lock(&id_mgr->lock);
496 if (vm->reserved_vmid[vmhub] &&
497 !--id_mgr->reserved_use_count) {
498 /* give the reserved ID back to normal round robin */
499 list_add(&id_mgr->reserved->list, &id_mgr->ids_lru);
500 id_mgr->reserved = NULL;
501 }
502 vm->reserved_vmid[vmhub] = false;
503 mutex_unlock(&id_mgr->lock);
504}
505
506/**
507 * amdgpu_vmid_reset - reset VMID to zero
508 *
509 * @adev: amdgpu device structure
510 * @vmhub: vmhub type
511 * @vmid: vmid number to use
512 *
513 * Reset saved GDW, GWS and OA to force switch on next flush.
514 */
515void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
516 unsigned vmid)
517{
518 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
519 struct amdgpu_vmid *id = &id_mgr->ids[vmid];
520
521 mutex_lock(&id_mgr->lock);
522 id->owner = 0;
523 id->gds_base = 0;
524 id->gds_size = 0;
525 id->gws_base = 0;
526 id->gws_size = 0;
527 id->oa_base = 0;
528 id->oa_size = 0;
529 mutex_unlock(&id_mgr->lock);
530}
531
532/**
533 * amdgpu_vmid_reset_all - reset VMID to zero
534 *
535 * @adev: amdgpu device structure
536 *
537 * Reset VMID to force flush on next use
538 */
539void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
540{
541 unsigned i, j;
542
543 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
544 struct amdgpu_vmid_mgr *id_mgr =
545 &adev->vm_manager.id_mgr[i];
546
547 for (j = 1; j < id_mgr->num_ids; ++j)
548 amdgpu_vmid_reset(adev, i, j);
549 }
550}
551
552/**
553 * amdgpu_vmid_mgr_init - init the VMID manager
554 *
555 * @adev: amdgpu_device pointer
556 *
557 * Initialize the VM manager structures
558 */
559void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
560{
561 unsigned i, j;
562
563 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
564 struct amdgpu_vmid_mgr *id_mgr =
565 &adev->vm_manager.id_mgr[i];
566
567 mutex_init(&id_mgr->lock);
568 INIT_LIST_HEAD(&id_mgr->ids_lru);
569 id_mgr->reserved_use_count = 0;
570
571 /* manage only VMIDs not used by KFD */
572 id_mgr->num_ids = adev->vm_manager.first_kfd_vmid;
573
574 /* skip over VMID 0, since it is the system VM */
575 for (j = 1; j < id_mgr->num_ids; ++j) {
576 amdgpu_vmid_reset(adev, i, j);
577 amdgpu_sync_create(&id_mgr->ids[j].active);
578 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
579 }
580 }
581}
582
583/**
584 * amdgpu_vmid_mgr_fini - cleanup VM manager
585 *
586 * @adev: amdgpu_device pointer
587 *
588 * Cleanup the VM manager and free resources.
589 */
590void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
591{
592 unsigned i, j;
593
594 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
595 struct amdgpu_vmid_mgr *id_mgr =
596 &adev->vm_manager.id_mgr[i];
597
598 mutex_destroy(&id_mgr->lock);
599 for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
600 struct amdgpu_vmid *id = &id_mgr->ids[j];
601
602 amdgpu_sync_free(&id->active);
603 dma_fence_put(id->last_flush);
604 dma_fence_put(id->pasid_mapping);
605 }
606 }
607}
1/*
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23#include "amdgpu_ids.h"
24
25#include <linux/idr.h>
26#include <linux/dma-fence-array.h>
27
28
29#include "amdgpu.h"
30#include "amdgpu_trace.h"
31
32/*
33 * PASID manager
34 *
35 * PASIDs are global address space identifiers that can be shared
36 * between the GPU, an IOMMU and the driver. VMs on different devices
37 * may use the same PASID if they share the same address
38 * space. Therefore PASIDs are allocated using a global IDA. VMs are
39 * looked up from the PASID per amdgpu_device.
40 */
41static DEFINE_IDA(amdgpu_pasid_ida);
42
43/* Helper to free pasid from a fence callback */
44struct amdgpu_pasid_cb {
45 struct dma_fence_cb cb;
46 u32 pasid;
47};
48
49/**
50 * amdgpu_pasid_alloc - Allocate a PASID
51 * @bits: Maximum width of the PASID in bits, must be at least 1
52 *
53 * Allocates a PASID of the given width while keeping smaller PASIDs
54 * available if possible.
55 *
56 * Returns a positive integer on success. Returns %-EINVAL if bits==0.
57 * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
58 * memory allocation failure.
59 */
60int amdgpu_pasid_alloc(unsigned int bits)
61{
62 int pasid = -EINVAL;
63
64 for (bits = min(bits, 31U); bits > 0; bits--) {
65 pasid = ida_alloc_range(&amdgpu_pasid_ida, 1U << (bits - 1),
66 (1U << bits) - 1, GFP_KERNEL);
67 if (pasid != -ENOSPC)
68 break;
69 }
70
71 if (pasid >= 0)
72 trace_amdgpu_pasid_allocated(pasid);
73
74 return pasid;
75}
76
77/**
78 * amdgpu_pasid_free - Free a PASID
79 * @pasid: PASID to free
80 */
81void amdgpu_pasid_free(u32 pasid)
82{
83 trace_amdgpu_pasid_freed(pasid);
84 ida_free(&amdgpu_pasid_ida, pasid);
85}
86
87static void amdgpu_pasid_free_cb(struct dma_fence *fence,
88 struct dma_fence_cb *_cb)
89{
90 struct amdgpu_pasid_cb *cb =
91 container_of(_cb, struct amdgpu_pasid_cb, cb);
92
93 amdgpu_pasid_free(cb->pasid);
94 dma_fence_put(fence);
95 kfree(cb);
96}
97
98/**
99 * amdgpu_pasid_free_delayed - free pasid when fences signal
100 *
101 * @resv: reservation object with the fences to wait for
102 * @pasid: pasid to free
103 *
104 * Free the pasid only after all the fences in resv are signaled.
105 */
106void amdgpu_pasid_free_delayed(struct dma_resv *resv,
107 u32 pasid)
108{
109 struct amdgpu_pasid_cb *cb;
110 struct dma_fence *fence;
111 int r;
112
113 r = dma_resv_get_singleton(resv, DMA_RESV_USAGE_BOOKKEEP, &fence);
114 if (r)
115 goto fallback;
116
117 if (!fence) {
118 amdgpu_pasid_free(pasid);
119 return;
120 }
121
122 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
123 if (!cb) {
124 /* Last resort when we are OOM */
125 dma_fence_wait(fence, false);
126 dma_fence_put(fence);
127 amdgpu_pasid_free(pasid);
128 } else {
129 cb->pasid = pasid;
130 if (dma_fence_add_callback(fence, &cb->cb,
131 amdgpu_pasid_free_cb))
132 amdgpu_pasid_free_cb(fence, &cb->cb);
133 }
134
135 return;
136
137fallback:
138 /* Not enough memory for the delayed delete, as last resort
139 * block for all the fences to complete.
140 */
141 dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
142 false, MAX_SCHEDULE_TIMEOUT);
143 amdgpu_pasid_free(pasid);
144}
145
146/*
147 * VMID manager
148 *
149 * VMIDs are a per VMHUB identifier for page tables handling.
150 */
151
152/**
153 * amdgpu_vmid_had_gpu_reset - check if reset occured since last use
154 *
155 * @adev: amdgpu_device pointer
156 * @id: VMID structure
157 *
158 * Check if GPU reset occured since last use of the VMID.
159 */
160bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
161 struct amdgpu_vmid *id)
162{
163 return id->current_gpu_reset_count !=
164 atomic_read(&adev->gpu_reset_counter);
165}
166
167/* Check if we need to switch to another set of resources */
168static bool amdgpu_vmid_gds_switch_needed(struct amdgpu_vmid *id,
169 struct amdgpu_job *job)
170{
171 return id->gds_base != job->gds_base ||
172 id->gds_size != job->gds_size ||
173 id->gws_base != job->gws_base ||
174 id->gws_size != job->gws_size ||
175 id->oa_base != job->oa_base ||
176 id->oa_size != job->oa_size;
177}
178
179/* Check if the id is compatible with the job */
180static bool amdgpu_vmid_compatible(struct amdgpu_vmid *id,
181 struct amdgpu_job *job)
182{
183 return id->pd_gpu_addr == job->vm_pd_addr &&
184 !amdgpu_vmid_gds_switch_needed(id, job);
185}
186
187/**
188 * amdgpu_vmid_grab_idle - grab idle VMID
189 *
190 * @ring: ring we want to submit job to
191 * @idle: resulting idle VMID
192 * @fence: fence to wait for if no id could be grabbed
193 *
194 * Try to find an idle VMID, if none is idle add a fence to wait to the sync
195 * object. Returns -ENOMEM when we are out of memory.
196 */
197static int amdgpu_vmid_grab_idle(struct amdgpu_ring *ring,
198 struct amdgpu_vmid **idle,
199 struct dma_fence **fence)
200{
201 struct amdgpu_device *adev = ring->adev;
202 unsigned vmhub = ring->vm_hub;
203 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
204 struct dma_fence **fences;
205 unsigned i;
206
207 if (!dma_fence_is_signaled(ring->vmid_wait)) {
208 *fence = dma_fence_get(ring->vmid_wait);
209 return 0;
210 }
211
212 fences = kmalloc_array(id_mgr->num_ids, sizeof(void *), GFP_KERNEL);
213 if (!fences)
214 return -ENOMEM;
215
216 /* Check if we have an idle VMID */
217 i = 0;
218 list_for_each_entry((*idle), &id_mgr->ids_lru, list) {
219 /* Don't use per engine and per process VMID at the same time */
220 struct amdgpu_ring *r = adev->vm_manager.concurrent_flush ?
221 NULL : ring;
222
223 fences[i] = amdgpu_sync_peek_fence(&(*idle)->active, r);
224 if (!fences[i])
225 break;
226 ++i;
227 }
228
229 /* If we can't find a idle VMID to use, wait till one becomes available */
230 if (&(*idle)->list == &id_mgr->ids_lru) {
231 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
232 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
233 struct dma_fence_array *array;
234 unsigned j;
235
236 *idle = NULL;
237 for (j = 0; j < i; ++j)
238 dma_fence_get(fences[j]);
239
240 array = dma_fence_array_create(i, fences, fence_context,
241 seqno, true);
242 if (!array) {
243 for (j = 0; j < i; ++j)
244 dma_fence_put(fences[j]);
245 kfree(fences);
246 return -ENOMEM;
247 }
248
249 *fence = dma_fence_get(&array->base);
250 dma_fence_put(ring->vmid_wait);
251 ring->vmid_wait = &array->base;
252 return 0;
253 }
254 kfree(fences);
255
256 return 0;
257}
258
259/**
260 * amdgpu_vmid_grab_reserved - try to assign reserved VMID
261 *
262 * @vm: vm to allocate id for
263 * @ring: ring we want to submit job to
264 * @job: job who wants to use the VMID
265 * @id: resulting VMID
266 * @fence: fence to wait for if no id could be grabbed
267 *
268 * Try to assign a reserved VMID.
269 */
270static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
271 struct amdgpu_ring *ring,
272 struct amdgpu_job *job,
273 struct amdgpu_vmid **id,
274 struct dma_fence **fence)
275{
276 struct amdgpu_device *adev = ring->adev;
277 unsigned vmhub = ring->vm_hub;
278 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
279 uint64_t fence_context = adev->fence_context + ring->idx;
280 bool needs_flush = vm->use_cpu_for_update;
281 uint64_t updates = amdgpu_vm_tlb_seq(vm);
282 int r;
283
284 *id = id_mgr->reserved;
285 if ((*id)->owner != vm->immediate.fence_context ||
286 !amdgpu_vmid_compatible(*id, job) ||
287 (*id)->flushed_updates < updates ||
288 !(*id)->last_flush ||
289 ((*id)->last_flush->context != fence_context &&
290 !dma_fence_is_signaled((*id)->last_flush))) {
291 struct dma_fence *tmp;
292
293 /* Wait for the gang to be assembled before using a
294 * reserved VMID or otherwise the gang could deadlock.
295 */
296 tmp = amdgpu_device_get_gang(adev);
297 if (!dma_fence_is_signaled(tmp) && tmp != job->gang_submit) {
298 *id = NULL;
299 *fence = tmp;
300 return 0;
301 }
302 dma_fence_put(tmp);
303
304 /* Make sure the id is owned by the gang before proceeding */
305 if (!job->gang_submit ||
306 (*id)->owner != vm->immediate.fence_context) {
307
308 /* Don't use per engine and per process VMID at the
309 * same time
310 */
311 if (adev->vm_manager.concurrent_flush)
312 ring = NULL;
313
314 /* to prevent one context starved by another context */
315 (*id)->pd_gpu_addr = 0;
316 tmp = amdgpu_sync_peek_fence(&(*id)->active, ring);
317 if (tmp) {
318 *id = NULL;
319 *fence = dma_fence_get(tmp);
320 return 0;
321 }
322 }
323 needs_flush = true;
324 }
325
326 /* Good we can use this VMID. Remember this submission as
327 * user of the VMID.
328 */
329 r = amdgpu_sync_fence(&(*id)->active, &job->base.s_fence->finished);
330 if (r)
331 return r;
332
333 job->vm_needs_flush = needs_flush;
334 job->spm_update_needed = true;
335 return 0;
336}
337
338/**
339 * amdgpu_vmid_grab_used - try to reuse a VMID
340 *
341 * @vm: vm to allocate id for
342 * @ring: ring we want to submit job to
343 * @job: job who wants to use the VMID
344 * @id: resulting VMID
345 *
346 * Try to reuse a VMID for this submission.
347 */
348static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
349 struct amdgpu_ring *ring,
350 struct amdgpu_job *job,
351 struct amdgpu_vmid **id)
352{
353 struct amdgpu_device *adev = ring->adev;
354 unsigned vmhub = ring->vm_hub;
355 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
356 uint64_t fence_context = adev->fence_context + ring->idx;
357 uint64_t updates = amdgpu_vm_tlb_seq(vm);
358 int r;
359
360 job->vm_needs_flush = vm->use_cpu_for_update;
361
362 /* Check if we can use a VMID already assigned to this VM */
363 list_for_each_entry_reverse((*id), &id_mgr->ids_lru, list) {
364 bool needs_flush = vm->use_cpu_for_update;
365
366 /* Check all the prerequisites to using this VMID */
367 if ((*id)->owner != vm->immediate.fence_context)
368 continue;
369
370 if (!amdgpu_vmid_compatible(*id, job))
371 continue;
372
373 if (!(*id)->last_flush ||
374 ((*id)->last_flush->context != fence_context &&
375 !dma_fence_is_signaled((*id)->last_flush)))
376 needs_flush = true;
377
378 if ((*id)->flushed_updates < updates)
379 needs_flush = true;
380
381 if (needs_flush && !adev->vm_manager.concurrent_flush)
382 continue;
383
384 /* Good, we can use this VMID. Remember this submission as
385 * user of the VMID.
386 */
387 r = amdgpu_sync_fence(&(*id)->active,
388 &job->base.s_fence->finished);
389 if (r)
390 return r;
391
392 job->vm_needs_flush |= needs_flush;
393 return 0;
394 }
395
396 *id = NULL;
397 return 0;
398}
399
400/**
401 * amdgpu_vmid_grab - allocate the next free VMID
402 *
403 * @vm: vm to allocate id for
404 * @ring: ring we want to submit job to
405 * @job: job who wants to use the VMID
406 * @fence: fence to wait for if no id could be grabbed
407 *
408 * Allocate an id for the vm, adding fences to the sync obj as necessary.
409 */
410int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
411 struct amdgpu_job *job, struct dma_fence **fence)
412{
413 struct amdgpu_device *adev = ring->adev;
414 unsigned vmhub = ring->vm_hub;
415 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
416 struct amdgpu_vmid *idle = NULL;
417 struct amdgpu_vmid *id = NULL;
418 int r = 0;
419
420 mutex_lock(&id_mgr->lock);
421 r = amdgpu_vmid_grab_idle(ring, &idle, fence);
422 if (r || !idle)
423 goto error;
424
425 if (amdgpu_vmid_uses_reserved(adev, vm, vmhub)) {
426 r = amdgpu_vmid_grab_reserved(vm, ring, job, &id, fence);
427 if (r || !id)
428 goto error;
429 } else {
430 r = amdgpu_vmid_grab_used(vm, ring, job, &id);
431 if (r)
432 goto error;
433
434 if (!id) {
435 /* Still no ID to use? Then use the idle one found earlier */
436 id = idle;
437
438 /* Remember this submission as user of the VMID */
439 r = amdgpu_sync_fence(&id->active,
440 &job->base.s_fence->finished);
441 if (r)
442 goto error;
443
444 job->vm_needs_flush = true;
445 }
446
447 list_move_tail(&id->list, &id_mgr->ids_lru);
448 }
449
450 job->gds_switch_needed = amdgpu_vmid_gds_switch_needed(id, job);
451 if (job->vm_needs_flush) {
452 id->flushed_updates = amdgpu_vm_tlb_seq(vm);
453 dma_fence_put(id->last_flush);
454 id->last_flush = NULL;
455 }
456 job->vmid = id - id_mgr->ids;
457 job->pasid = vm->pasid;
458
459 id->gds_base = job->gds_base;
460 id->gds_size = job->gds_size;
461 id->gws_base = job->gws_base;
462 id->gws_size = job->gws_size;
463 id->oa_base = job->oa_base;
464 id->oa_size = job->oa_size;
465 id->pd_gpu_addr = job->vm_pd_addr;
466 id->owner = vm->immediate.fence_context;
467
468 trace_amdgpu_vm_grab_id(vm, ring, job);
469
470error:
471 mutex_unlock(&id_mgr->lock);
472 return r;
473}
474
475/*
476 * amdgpu_vmid_uses_reserved - check if a VM will use a reserved VMID
477 * @adev: amdgpu_device pointer
478 * @vm: the VM to check
479 * @vmhub: the VMHUB which will be used
480 *
481 * Returns: True if the VM will use a reserved VMID.
482 */
483bool amdgpu_vmid_uses_reserved(struct amdgpu_device *adev,
484 struct amdgpu_vm *vm, unsigned int vmhub)
485{
486 return vm->reserved_vmid[vmhub] ||
487 (adev->enforce_isolation[(vm->root.bo->xcp_id != AMDGPU_XCP_NO_PARTITION) ?
488 vm->root.bo->xcp_id : 0] &&
489 AMDGPU_IS_GFXHUB(vmhub));
490}
491
492int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
493 unsigned vmhub)
494{
495 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
496
497 mutex_lock(&id_mgr->lock);
498
499 ++id_mgr->reserved_use_count;
500 if (!id_mgr->reserved) {
501 struct amdgpu_vmid *id;
502
503 id = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid,
504 list);
505 /* Remove from normal round robin handling */
506 list_del_init(&id->list);
507 id_mgr->reserved = id;
508 }
509
510 mutex_unlock(&id_mgr->lock);
511 return 0;
512}
513
514void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
515 unsigned vmhub)
516{
517 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
518
519 mutex_lock(&id_mgr->lock);
520 if (!--id_mgr->reserved_use_count) {
521 /* give the reserved ID back to normal round robin */
522 list_add(&id_mgr->reserved->list, &id_mgr->ids_lru);
523 id_mgr->reserved = NULL;
524 }
525
526 mutex_unlock(&id_mgr->lock);
527}
528
529/**
530 * amdgpu_vmid_reset - reset VMID to zero
531 *
532 * @adev: amdgpu device structure
533 * @vmhub: vmhub type
534 * @vmid: vmid number to use
535 *
536 * Reset saved GDW, GWS and OA to force switch on next flush.
537 */
538void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
539 unsigned vmid)
540{
541 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
542 struct amdgpu_vmid *id = &id_mgr->ids[vmid];
543
544 mutex_lock(&id_mgr->lock);
545 id->owner = 0;
546 id->gds_base = 0;
547 id->gds_size = 0;
548 id->gws_base = 0;
549 id->gws_size = 0;
550 id->oa_base = 0;
551 id->oa_size = 0;
552 mutex_unlock(&id_mgr->lock);
553}
554
555/**
556 * amdgpu_vmid_reset_all - reset VMID to zero
557 *
558 * @adev: amdgpu device structure
559 *
560 * Reset VMID to force flush on next use
561 */
562void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
563{
564 unsigned i, j;
565
566 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
567 struct amdgpu_vmid_mgr *id_mgr =
568 &adev->vm_manager.id_mgr[i];
569
570 for (j = 1; j < id_mgr->num_ids; ++j)
571 amdgpu_vmid_reset(adev, i, j);
572 }
573}
574
575/**
576 * amdgpu_vmid_mgr_init - init the VMID manager
577 *
578 * @adev: amdgpu_device pointer
579 *
580 * Initialize the VM manager structures
581 */
582void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
583{
584 unsigned i, j;
585
586 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
587 struct amdgpu_vmid_mgr *id_mgr =
588 &adev->vm_manager.id_mgr[i];
589
590 mutex_init(&id_mgr->lock);
591 INIT_LIST_HEAD(&id_mgr->ids_lru);
592 id_mgr->reserved_use_count = 0;
593
594 /* manage only VMIDs not used by KFD */
595 id_mgr->num_ids = adev->vm_manager.first_kfd_vmid;
596
597 /* skip over VMID 0, since it is the system VM */
598 for (j = 1; j < id_mgr->num_ids; ++j) {
599 amdgpu_vmid_reset(adev, i, j);
600 amdgpu_sync_create(&id_mgr->ids[j].active);
601 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
602 }
603 }
604 /* alloc a default reserved vmid to enforce isolation */
605 for (i = 0; i < (adev->xcp_mgr ? adev->xcp_mgr->num_xcps : 1); i++) {
606 if (adev->enforce_isolation[i])
607 amdgpu_vmid_alloc_reserved(adev, AMDGPU_GFXHUB(i));
608 }
609}
610
611/**
612 * amdgpu_vmid_mgr_fini - cleanup VM manager
613 *
614 * @adev: amdgpu_device pointer
615 *
616 * Cleanup the VM manager and free resources.
617 */
618void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
619{
620 unsigned i, j;
621
622 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
623 struct amdgpu_vmid_mgr *id_mgr =
624 &adev->vm_manager.id_mgr[i];
625
626 mutex_destroy(&id_mgr->lock);
627 for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
628 struct amdgpu_vmid *id = &id_mgr->ids[j];
629
630 amdgpu_sync_free(&id->active);
631 dma_fence_put(id->last_flush);
632 dma_fence_put(id->pasid_mapping);
633 }
634 }
635}