blob: fbe958f7cb5bec523e48170cfa0c5579c59b7bf9 [file] [log] [blame]
Christian König620f7742017-12-18 16:53:03 +01001/*
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#include <drm/drmP.h>
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
Christian König4b5f7552018-01-05 11:16:22 +010043/* Helper to free pasid from a fence callback */
44struct amdgpu_pasid_cb {
45 struct dma_fence_cb cb;
46 unsigned int pasid;
47};
48
Christian König620f7742017-12-18 16:53:03 +010049/**
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
Christian Königc35ff182018-01-09 19:32:58 +010072 if (pasid >= 0)
73 trace_amdgpu_pasid_allocated(pasid);
74
Christian König620f7742017-12-18 16:53:03 +010075 return pasid;
76}
77
78/**
79 * amdgpu_pasid_free - Free a PASID
80 * @pasid: PASID to free
81 */
82void amdgpu_pasid_free(unsigned int pasid)
83{
Christian Königc35ff182018-01-09 19:32:58 +010084 trace_amdgpu_pasid_freed(pasid);
Christian König620f7742017-12-18 16:53:03 +010085 ida_simple_remove(&amdgpu_pasid_ida, pasid);
86}
87
Christian König4b5f7552018-01-05 11:16:22 +010088static 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 reservation_object *resv,
108 unsigned int pasid)
109{
110 struct dma_fence *fence, **fences;
111 struct amdgpu_pasid_cb *cb;
112 unsigned count;
113 int r;
114
115 r = reservation_object_get_fences_rcu(resv, NULL, &count, &fences);
116 if (r)
117 goto fallback;
118
119 if (count == 0) {
120 amdgpu_pasid_free(pasid);
121 return;
122 }
123
124 if (count == 1) {
125 fence = fences[0];
126 kfree(fences);
127 } else {
128 uint64_t context = dma_fence_context_alloc(1);
129 struct dma_fence_array *array;
130
131 array = dma_fence_array_create(count, fences, context,
132 1, false);
133 if (!array) {
134 kfree(fences);
135 goto fallback;
136 }
137 fence = &array->base;
138 }
139
140 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
141 if (!cb) {
142 /* Last resort when we are OOM */
143 dma_fence_wait(fence, false);
144 dma_fence_put(fence);
145 amdgpu_pasid_free(pasid);
146 } else {
147 cb->pasid = pasid;
148 if (dma_fence_add_callback(fence, &cb->cb,
149 amdgpu_pasid_free_cb))
150 amdgpu_pasid_free_cb(fence, &cb->cb);
151 }
152
153 return;
154
155fallback:
156 /* Not enough memory for the delayed delete, as last resort
157 * block for all the fences to complete.
158 */
159 reservation_object_wait_timeout_rcu(resv, true, false,
160 MAX_SCHEDULE_TIMEOUT);
161 amdgpu_pasid_free(pasid);
162}
163
Christian König620f7742017-12-18 16:53:03 +0100164/*
165 * VMID manager
166 *
167 * VMIDs are a per VMHUB identifier for page tables handling.
168 */
169
170/**
171 * amdgpu_vmid_had_gpu_reset - check if reset occured since last use
172 *
173 * @adev: amdgpu_device pointer
174 * @id: VMID structure
175 *
176 * Check if GPU reset occured since last use of the VMID.
177 */
178bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
179 struct amdgpu_vmid *id)
180{
181 return id->current_gpu_reset_count !=
182 atomic_read(&adev->gpu_reset_counter);
183}
184
Christian König3a80e922018-01-31 11:10:19 +0100185/**
186 * amdgpu_vm_grab_idle - grab idle VMID
187 *
188 * @vm: vm to allocate id for
189 * @ring: ring we want to submit job to
190 * @sync: sync object where we add dependencies
191 * @idle: resulting idle VMID
192 *
193 * Try to find an idle VMID, if none is idle add a fence to wait to the sync
194 * object. Returns -ENOMEM when we are out of memory.
195 */
196static int amdgpu_vmid_grab_idle(struct amdgpu_vm *vm,
197 struct amdgpu_ring *ring,
198 struct amdgpu_sync *sync,
199 struct amdgpu_vmid **idle)
200{
201 struct amdgpu_device *adev = ring->adev;
202 unsigned vmhub = ring->funcs->vmhub;
203 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
204 struct dma_fence **fences;
205 unsigned i;
206 int r;
207
208 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
209 if (!fences)
210 return -ENOMEM;
211
212 /* Check if we have an idle VMID */
213 i = 0;
214 list_for_each_entry((*idle), &id_mgr->ids_lru, list) {
215 fences[i] = amdgpu_sync_peek_fence(&(*idle)->active, ring);
216 if (!fences[i])
217 break;
218 ++i;
219 }
220
221 /* If we can't find a idle VMID to use, wait till one becomes available */
222 if (&(*idle)->list == &id_mgr->ids_lru) {
223 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
224 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
225 struct dma_fence_array *array;
226 unsigned j;
227
228 *idle = NULL;
229 for (j = 0; j < i; ++j)
230 dma_fence_get(fences[j]);
231
232 array = dma_fence_array_create(i, fences, fence_context,
233 seqno, true);
234 if (!array) {
235 for (j = 0; j < i; ++j)
236 dma_fence_put(fences[j]);
237 kfree(fences);
238 return -ENOMEM;
239 }
240
241 r = amdgpu_sync_fence(adev, sync, &array->base, false);
242 dma_fence_put(&array->base);
243 return r;
244
245 }
246 kfree(fences);
247
248 return 0;
249}
250
Christian König620f7742017-12-18 16:53:03 +0100251/* idr_mgr->lock must be held */
252static int amdgpu_vmid_grab_reserved_locked(struct amdgpu_vm *vm,
253 struct amdgpu_ring *ring,
254 struct amdgpu_sync *sync,
255 struct dma_fence *fence,
256 struct amdgpu_job *job)
257{
258 struct amdgpu_device *adev = ring->adev;
259 unsigned vmhub = ring->funcs->vmhub;
260 uint64_t fence_context = adev->fence_context + ring->idx;
261 struct amdgpu_vmid *id = vm->reserved_vmid[vmhub];
262 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
263 struct dma_fence *updates = sync->last_vm_update;
264 int r = 0;
265 struct dma_fence *flushed, *tmp;
266 bool needs_flush = vm->use_cpu_for_update;
267
268 flushed = id->flushed_updates;
269 if ((amdgpu_vmid_had_gpu_reset(adev, id)) ||
Christian König0e36b9b2017-12-18 17:10:01 +0100270 (atomic64_read(&id->owner) != vm->entity.fence_context) ||
Christian König620f7742017-12-18 16:53:03 +0100271 (job->vm_pd_addr != id->pd_gpu_addr) ||
272 (updates && (!flushed || updates->context != flushed->context ||
273 dma_fence_is_later(updates, flushed))) ||
274 (!id->last_flush || (id->last_flush->context != fence_context &&
275 !dma_fence_is_signaled(id->last_flush)))) {
276 needs_flush = true;
277 /* to prevent one context starved by another context */
278 id->pd_gpu_addr = 0;
279 tmp = amdgpu_sync_peek_fence(&id->active, ring);
280 if (tmp) {
281 r = amdgpu_sync_fence(adev, sync, tmp, false);
282 return r;
283 }
284 }
285
286 /* Good we can use this VMID. Remember this submission as
287 * user of the VMID.
288 */
289 r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
290 if (r)
291 goto out;
292
293 if (updates && (!flushed || updates->context != flushed->context ||
294 dma_fence_is_later(updates, flushed))) {
295 dma_fence_put(id->flushed_updates);
296 id->flushed_updates = dma_fence_get(updates);
297 }
298 id->pd_gpu_addr = job->vm_pd_addr;
Christian König0e36b9b2017-12-18 17:10:01 +0100299 atomic64_set(&id->owner, vm->entity.fence_context);
Christian König620f7742017-12-18 16:53:03 +0100300 job->vm_needs_flush = needs_flush;
301 if (needs_flush) {
302 dma_fence_put(id->last_flush);
303 id->last_flush = NULL;
304 }
Christian Königc4f46f22017-12-18 17:08:25 +0100305 job->vmid = id - id_mgr->ids;
Christian König5a4633c2018-01-08 14:48:11 +0100306 job->pasid = vm->pasid;
Christian König620f7742017-12-18 16:53:03 +0100307 trace_amdgpu_vm_grab_id(vm, ring, job);
308out:
309 return r;
310}
311
312/**
313 * amdgpu_vm_grab_id - allocate the next free VMID
314 *
315 * @vm: vm to allocate id for
316 * @ring: ring we want to submit job to
317 * @sync: sync object where we add dependencies
318 * @fence: fence protecting ID from reuse
319 *
320 * Allocate an id for the vm, adding fences to the sync obj as necessary.
321 */
322int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
323 struct amdgpu_sync *sync, struct dma_fence *fence,
324 struct amdgpu_job *job)
325{
326 struct amdgpu_device *adev = ring->adev;
327 unsigned vmhub = ring->funcs->vmhub;
328 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
329 uint64_t fence_context = adev->fence_context + ring->idx;
330 struct dma_fence *updates = sync->last_vm_update;
331 struct amdgpu_vmid *id, *idle;
Christian König620f7742017-12-18 16:53:03 +0100332 int r = 0;
333
334 mutex_lock(&id_mgr->lock);
Christian König3a80e922018-01-31 11:10:19 +0100335 r = amdgpu_vmid_grab_idle(vm, ring, sync, &idle);
336 if (r || !idle)
337 goto error;
Christian König620f7742017-12-18 16:53:03 +0100338
Christian König8fe27f82018-01-31 10:16:26 +0100339 if (vm->reserved_vmid[vmhub]) {
340 r = amdgpu_vmid_grab_reserved_locked(vm, ring, sync,
341 fence, job);
342 mutex_unlock(&id_mgr->lock);
343 return r;
344 }
345
Christian König620f7742017-12-18 16:53:03 +0100346 job->vm_needs_flush = vm->use_cpu_for_update;
347 /* Check if we can use a VMID already assigned to this VM */
348 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
349 struct dma_fence *flushed;
350 bool needs_flush = vm->use_cpu_for_update;
351
352 /* Check all the prerequisites to using this VMID */
353 if (amdgpu_vmid_had_gpu_reset(adev, id))
354 continue;
355
Christian König0e36b9b2017-12-18 17:10:01 +0100356 if (atomic64_read(&id->owner) != vm->entity.fence_context)
Christian König620f7742017-12-18 16:53:03 +0100357 continue;
358
359 if (job->vm_pd_addr != id->pd_gpu_addr)
360 continue;
361
362 if (!id->last_flush ||
363 (id->last_flush->context != fence_context &&
364 !dma_fence_is_signaled(id->last_flush)))
365 needs_flush = true;
366
367 flushed = id->flushed_updates;
368 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
369 needs_flush = true;
370
371 /* Concurrent flushes are only possible starting with Vega10 */
372 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
373 continue;
374
375 /* Good we can use this VMID. Remember this submission as
376 * user of the VMID.
377 */
378 r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
379 if (r)
380 goto error;
381
382 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
383 dma_fence_put(id->flushed_updates);
384 id->flushed_updates = dma_fence_get(updates);
385 }
386
387 if (needs_flush)
388 goto needs_flush;
389 else
390 goto no_flush_needed;
391
Fengguang Wu02d170e2018-01-05 07:06:46 +0800392 }
Christian König620f7742017-12-18 16:53:03 +0100393
394 /* Still no ID to use? Then use the idle one found earlier */
395 id = idle;
396
397 /* Remember this submission as user of the VMID */
398 r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
399 if (r)
400 goto error;
401
402 id->pd_gpu_addr = job->vm_pd_addr;
403 dma_fence_put(id->flushed_updates);
404 id->flushed_updates = dma_fence_get(updates);
Christian König0e36b9b2017-12-18 17:10:01 +0100405 atomic64_set(&id->owner, vm->entity.fence_context);
Christian König620f7742017-12-18 16:53:03 +0100406
407needs_flush:
408 job->vm_needs_flush = true;
409 dma_fence_put(id->last_flush);
410 id->last_flush = NULL;
411
412no_flush_needed:
413 list_move_tail(&id->list, &id_mgr->ids_lru);
414
Christian Königc4f46f22017-12-18 17:08:25 +0100415 job->vmid = id - id_mgr->ids;
Christian König5a4633c2018-01-08 14:48:11 +0100416 job->pasid = vm->pasid;
Christian König620f7742017-12-18 16:53:03 +0100417 trace_amdgpu_vm_grab_id(vm, ring, job);
418
419error:
420 mutex_unlock(&id_mgr->lock);
421 return r;
422}
423
424int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
425 struct amdgpu_vm *vm,
426 unsigned vmhub)
427{
428 struct amdgpu_vmid_mgr *id_mgr;
429 struct amdgpu_vmid *idle;
430 int r = 0;
431
432 id_mgr = &adev->vm_manager.id_mgr[vmhub];
433 mutex_lock(&id_mgr->lock);
434 if (vm->reserved_vmid[vmhub])
435 goto unlock;
436 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
437 AMDGPU_VM_MAX_RESERVED_VMID) {
438 DRM_ERROR("Over limitation of reserved vmid\n");
439 atomic_dec(&id_mgr->reserved_vmid_num);
440 r = -EINVAL;
441 goto unlock;
442 }
443 /* Select the first entry VMID */
444 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid, list);
445 list_del_init(&idle->list);
446 vm->reserved_vmid[vmhub] = idle;
447 mutex_unlock(&id_mgr->lock);
448
449 return 0;
450unlock:
451 mutex_unlock(&id_mgr->lock);
452 return r;
453}
454
455void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
456 struct amdgpu_vm *vm,
457 unsigned vmhub)
458{
459 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
460
461 mutex_lock(&id_mgr->lock);
462 if (vm->reserved_vmid[vmhub]) {
463 list_add(&vm->reserved_vmid[vmhub]->list,
464 &id_mgr->ids_lru);
465 vm->reserved_vmid[vmhub] = NULL;
466 atomic_dec(&id_mgr->reserved_vmid_num);
467 }
468 mutex_unlock(&id_mgr->lock);
469}
470
471/**
472 * amdgpu_vmid_reset - reset VMID to zero
473 *
474 * @adev: amdgpu device structure
Christian Königc4f46f22017-12-18 17:08:25 +0100475 * @vmid: vmid number to use
Christian König620f7742017-12-18 16:53:03 +0100476 *
477 * Reset saved GDW, GWS and OA to force switch on next flush.
478 */
479void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
480 unsigned vmid)
481{
482 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
483 struct amdgpu_vmid *id = &id_mgr->ids[vmid];
484
485 atomic64_set(&id->owner, 0);
486 id->gds_base = 0;
487 id->gds_size = 0;
488 id->gws_base = 0;
489 id->gws_size = 0;
490 id->oa_base = 0;
491 id->oa_size = 0;
492}
493
494/**
495 * amdgpu_vmid_reset_all - reset VMID to zero
496 *
497 * @adev: amdgpu device structure
498 *
499 * Reset VMID to force flush on next use
500 */
501void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
502{
503 unsigned i, j;
504
505 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
506 struct amdgpu_vmid_mgr *id_mgr =
507 &adev->vm_manager.id_mgr[i];
508
509 for (j = 1; j < id_mgr->num_ids; ++j)
510 amdgpu_vmid_reset(adev, i, j);
511 }
512}
513
514/**
515 * amdgpu_vmid_mgr_init - init the VMID manager
516 *
517 * @adev: amdgpu_device pointer
518 *
519 * Initialize the VM manager structures
520 */
521void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
522{
523 unsigned i, j;
524
525 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
526 struct amdgpu_vmid_mgr *id_mgr =
527 &adev->vm_manager.id_mgr[i];
528
529 mutex_init(&id_mgr->lock);
530 INIT_LIST_HEAD(&id_mgr->ids_lru);
531 atomic_set(&id_mgr->reserved_vmid_num, 0);
532
533 /* skip over VMID 0, since it is the system VM */
534 for (j = 1; j < id_mgr->num_ids; ++j) {
535 amdgpu_vmid_reset(adev, i, j);
536 amdgpu_sync_create(&id_mgr->ids[i].active);
537 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
538 }
539 }
540
541 adev->vm_manager.fence_context =
542 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
543 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
544 adev->vm_manager.seqno[i] = 0;
545}
546
547/**
548 * amdgpu_vmid_mgr_fini - cleanup VM manager
549 *
550 * @adev: amdgpu_device pointer
551 *
552 * Cleanup the VM manager and free resources.
553 */
554void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
555{
556 unsigned i, j;
557
558 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
559 struct amdgpu_vmid_mgr *id_mgr =
560 &adev->vm_manager.id_mgr[i];
561
562 mutex_destroy(&id_mgr->lock);
563 for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
564 struct amdgpu_vmid *id = &id_mgr->ids[j];
565
566 amdgpu_sync_free(&id->active);
567 dma_fence_put(id->flushed_updates);
568 dma_fence_put(id->last_flush);
569 }
570 }
571}