]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
drm/amdgpu: update pt shadow while updating pt V2
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_vm.c
1 /*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28 #include <linux/fence-array.h>
29 #include <drm/drmP.h>
30 #include <drm/amdgpu_drm.h>
31 #include "amdgpu.h"
32 #include "amdgpu_trace.h"
33
34 /*
35 * GPUVM
36 * GPUVM is similar to the legacy gart on older asics, however
37 * rather than there being a single global gart table
38 * for the entire GPU, there are multiple VM page tables active
39 * at any given time. The VM page tables can contain a mix
40 * vram pages and system memory pages and system memory pages
41 * can be mapped as snooped (cached system pages) or unsnooped
42 * (uncached system pages).
43 * Each VM has an ID associated with it and there is a page table
44 * associated with each VMID. When execting a command buffer,
45 * the kernel tells the the ring what VMID to use for that command
46 * buffer. VMIDs are allocated dynamically as commands are submitted.
47 * The userspace drivers maintain their own address space and the kernel
48 * sets up their pages tables accordingly when they submit their
49 * command buffers and a VMID is assigned.
50 * Cayman/Trinity support up to 8 active VMs at any given time;
51 * SI supports 16.
52 */
53
54 /* Local structure. Encapsulate some VM table update parameters to reduce
55 * the number of function parameters
56 */
57 struct amdgpu_pte_update_params {
58 /* amdgpu device we do this update for */
59 struct amdgpu_device *adev;
60 /* address where to copy page table entries from */
61 uint64_t src;
62 /* indirect buffer to fill with commands */
63 struct amdgpu_ib *ib;
64 /* Function which actually does the update */
65 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
66 uint64_t addr, unsigned count, uint32_t incr,
67 uint32_t flags);
68 /* indicate update pt or its shadow */
69 bool shadow;
70 };
71
72 /**
73 * amdgpu_vm_num_pde - return the number of page directory entries
74 *
75 * @adev: amdgpu_device pointer
76 *
77 * Calculate the number of page directory entries.
78 */
79 static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
80 {
81 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
82 }
83
84 /**
85 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
86 *
87 * @adev: amdgpu_device pointer
88 *
89 * Calculate the size of the page directory in bytes.
90 */
91 static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
92 {
93 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
94 }
95
96 /**
97 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
98 *
99 * @vm: vm providing the BOs
100 * @validated: head of validation list
101 * @entry: entry to add
102 *
103 * Add the page directory to the list of BOs to
104 * validate for command submission.
105 */
106 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
107 struct list_head *validated,
108 struct amdgpu_bo_list_entry *entry)
109 {
110 entry->robj = vm->page_directory;
111 entry->priority = 0;
112 entry->tv.bo = &vm->page_directory->tbo;
113 entry->tv.shared = true;
114 entry->user_pages = NULL;
115 list_add(&entry->tv.head, validated);
116 }
117
118 /**
119 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
120 *
121 * @adev: amdgpu device pointer
122 * @vm: vm providing the BOs
123 * @duplicates: head of duplicates list
124 *
125 * Add the page directory to the BO duplicates list
126 * for command submission.
127 */
128 void amdgpu_vm_get_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
129 struct list_head *duplicates)
130 {
131 uint64_t num_evictions;
132 unsigned i;
133
134 /* We only need to validate the page tables
135 * if they aren't already valid.
136 */
137 num_evictions = atomic64_read(&adev->num_evictions);
138 if (num_evictions == vm->last_eviction_counter)
139 return;
140
141 /* add the vm page table to the list */
142 for (i = 0; i <= vm->max_pde_used; ++i) {
143 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
144
145 if (!entry->robj)
146 continue;
147
148 list_add(&entry->tv.head, duplicates);
149 }
150
151 }
152
153 /**
154 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
155 *
156 * @adev: amdgpu device instance
157 * @vm: vm providing the BOs
158 *
159 * Move the PT BOs to the tail of the LRU.
160 */
161 void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
162 struct amdgpu_vm *vm)
163 {
164 struct ttm_bo_global *glob = adev->mman.bdev.glob;
165 unsigned i;
166
167 spin_lock(&glob->lru_lock);
168 for (i = 0; i <= vm->max_pde_used; ++i) {
169 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
170
171 if (!entry->robj)
172 continue;
173
174 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
175 }
176 spin_unlock(&glob->lru_lock);
177 }
178
179 static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
180 struct amdgpu_vm_id *id)
181 {
182 return id->current_gpu_reset_count !=
183 atomic_read(&adev->gpu_reset_counter) ? true : false;
184 }
185
186 /**
187 * amdgpu_vm_grab_id - allocate the next free VMID
188 *
189 * @vm: vm to allocate id for
190 * @ring: ring we want to submit job to
191 * @sync: sync object where we add dependencies
192 * @fence: fence protecting ID from reuse
193 *
194 * Allocate an id for the vm, adding fences to the sync obj as necessary.
195 */
196 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
197 struct amdgpu_sync *sync, struct fence *fence,
198 struct amdgpu_job *job)
199 {
200 struct amdgpu_device *adev = ring->adev;
201 uint64_t fence_context = adev->fence_context + ring->idx;
202 struct fence *updates = sync->last_vm_update;
203 struct amdgpu_vm_id *id, *idle;
204 struct fence **fences;
205 unsigned i;
206 int r = 0;
207
208 fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
209 GFP_KERNEL);
210 if (!fences)
211 return -ENOMEM;
212
213 mutex_lock(&adev->vm_manager.lock);
214
215 /* Check if we have an idle VMID */
216 i = 0;
217 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
218 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
219 if (!fences[i])
220 break;
221 ++i;
222 }
223
224 /* If we can't find a idle VMID to use, wait till one becomes available */
225 if (&idle->list == &adev->vm_manager.ids_lru) {
226 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
227 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
228 struct fence_array *array;
229 unsigned j;
230
231 for (j = 0; j < i; ++j)
232 fence_get(fences[j]);
233
234 array = fence_array_create(i, fences, fence_context,
235 seqno, true);
236 if (!array) {
237 for (j = 0; j < i; ++j)
238 fence_put(fences[j]);
239 kfree(fences);
240 r = -ENOMEM;
241 goto error;
242 }
243
244
245 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
246 fence_put(&array->base);
247 if (r)
248 goto error;
249
250 mutex_unlock(&adev->vm_manager.lock);
251 return 0;
252
253 }
254 kfree(fences);
255
256 job->vm_needs_flush = true;
257 /* Check if we can use a VMID already assigned to this VM */
258 i = ring->idx;
259 do {
260 struct fence *flushed;
261
262 id = vm->ids[i++];
263 if (i == AMDGPU_MAX_RINGS)
264 i = 0;
265
266 /* Check all the prerequisites to using this VMID */
267 if (!id)
268 continue;
269 if (amdgpu_vm_is_gpu_reset(adev, id))
270 continue;
271
272 if (atomic64_read(&id->owner) != vm->client_id)
273 continue;
274
275 if (job->vm_pd_addr != id->pd_gpu_addr)
276 continue;
277
278 if (!id->last_flush)
279 continue;
280
281 if (id->last_flush->context != fence_context &&
282 !fence_is_signaled(id->last_flush))
283 continue;
284
285 flushed = id->flushed_updates;
286 if (updates &&
287 (!flushed || fence_is_later(updates, flushed)))
288 continue;
289
290 /* Good we can use this VMID. Remember this submission as
291 * user of the VMID.
292 */
293 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
294 if (r)
295 goto error;
296
297 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
298 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
299 vm->ids[ring->idx] = id;
300
301 job->vm_id = id - adev->vm_manager.ids;
302 job->vm_needs_flush = false;
303 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
304
305 mutex_unlock(&adev->vm_manager.lock);
306 return 0;
307
308 } while (i != ring->idx);
309
310 /* Still no ID to use? Then use the idle one found earlier */
311 id = idle;
312
313 /* Remember this submission as user of the VMID */
314 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
315 if (r)
316 goto error;
317
318 fence_put(id->first);
319 id->first = fence_get(fence);
320
321 fence_put(id->last_flush);
322 id->last_flush = NULL;
323
324 fence_put(id->flushed_updates);
325 id->flushed_updates = fence_get(updates);
326
327 id->pd_gpu_addr = job->vm_pd_addr;
328 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
329 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
330 atomic64_set(&id->owner, vm->client_id);
331 vm->ids[ring->idx] = id;
332
333 job->vm_id = id - adev->vm_manager.ids;
334 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
335
336 error:
337 mutex_unlock(&adev->vm_manager.lock);
338 return r;
339 }
340
341 static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
342 {
343 struct amdgpu_device *adev = ring->adev;
344 const struct amdgpu_ip_block_version *ip_block;
345
346 if (ring->type != AMDGPU_RING_TYPE_COMPUTE)
347 /* only compute rings */
348 return false;
349
350 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
351 if (!ip_block)
352 return false;
353
354 if (ip_block->major <= 7) {
355 /* gfx7 has no workaround */
356 return true;
357 } else if (ip_block->major == 8) {
358 if (adev->gfx.mec_fw_version >= 673)
359 /* gfx8 is fixed in MEC firmware 673 */
360 return false;
361 else
362 return true;
363 }
364 return false;
365 }
366
367 /**
368 * amdgpu_vm_flush - hardware flush the vm
369 *
370 * @ring: ring to use for flush
371 * @vm_id: vmid number to use
372 * @pd_addr: address of the page directory
373 *
374 * Emit a VM flush when it is necessary.
375 */
376 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
377 {
378 struct amdgpu_device *adev = ring->adev;
379 struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
380 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
381 id->gds_base != job->gds_base ||
382 id->gds_size != job->gds_size ||
383 id->gws_base != job->gws_base ||
384 id->gws_size != job->gws_size ||
385 id->oa_base != job->oa_base ||
386 id->oa_size != job->oa_size);
387 int r;
388
389 if (ring->funcs->emit_pipeline_sync && (
390 job->vm_needs_flush || gds_switch_needed ||
391 amdgpu_vm_ring_has_compute_vm_bug(ring)))
392 amdgpu_ring_emit_pipeline_sync(ring);
393
394 if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
395 amdgpu_vm_is_gpu_reset(adev, id))) {
396 struct fence *fence;
397
398 trace_amdgpu_vm_flush(job->vm_pd_addr, ring->idx, job->vm_id);
399 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
400
401 r = amdgpu_fence_emit(ring, &fence);
402 if (r)
403 return r;
404
405 mutex_lock(&adev->vm_manager.lock);
406 fence_put(id->last_flush);
407 id->last_flush = fence;
408 mutex_unlock(&adev->vm_manager.lock);
409 }
410
411 if (gds_switch_needed) {
412 id->gds_base = job->gds_base;
413 id->gds_size = job->gds_size;
414 id->gws_base = job->gws_base;
415 id->gws_size = job->gws_size;
416 id->oa_base = job->oa_base;
417 id->oa_size = job->oa_size;
418 amdgpu_ring_emit_gds_switch(ring, job->vm_id,
419 job->gds_base, job->gds_size,
420 job->gws_base, job->gws_size,
421 job->oa_base, job->oa_size);
422 }
423
424 return 0;
425 }
426
427 /**
428 * amdgpu_vm_reset_id - reset VMID to zero
429 *
430 * @adev: amdgpu device structure
431 * @vm_id: vmid number to use
432 *
433 * Reset saved GDW, GWS and OA to force switch on next flush.
434 */
435 void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
436 {
437 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
438
439 id->gds_base = 0;
440 id->gds_size = 0;
441 id->gws_base = 0;
442 id->gws_size = 0;
443 id->oa_base = 0;
444 id->oa_size = 0;
445 }
446
447 /**
448 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
449 *
450 * @vm: requested vm
451 * @bo: requested buffer object
452 *
453 * Find @bo inside the requested vm.
454 * Search inside the @bos vm list for the requested vm
455 * Returns the found bo_va or NULL if none is found
456 *
457 * Object has to be reserved!
458 */
459 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
460 struct amdgpu_bo *bo)
461 {
462 struct amdgpu_bo_va *bo_va;
463
464 list_for_each_entry(bo_va, &bo->va, bo_list) {
465 if (bo_va->vm == vm) {
466 return bo_va;
467 }
468 }
469 return NULL;
470 }
471
472 /**
473 * amdgpu_vm_do_set_ptes - helper to call the right asic function
474 *
475 * @params: see amdgpu_pte_update_params definition
476 * @pe: addr of the page entry
477 * @addr: dst addr to write into pe
478 * @count: number of page entries to update
479 * @incr: increase next addr by incr bytes
480 * @flags: hw access flags
481 *
482 * Traces the parameters and calls the right asic functions
483 * to setup the page table using the DMA.
484 */
485 static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
486 uint64_t pe, uint64_t addr,
487 unsigned count, uint32_t incr,
488 uint32_t flags)
489 {
490 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
491
492 if (count < 3) {
493 amdgpu_vm_write_pte(params->adev, params->ib, pe,
494 addr | flags, count, incr);
495
496 } else {
497 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
498 count, incr, flags);
499 }
500 }
501
502 /**
503 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
504 *
505 * @params: see amdgpu_pte_update_params definition
506 * @pe: addr of the page entry
507 * @addr: dst addr to write into pe
508 * @count: number of page entries to update
509 * @incr: increase next addr by incr bytes
510 * @flags: hw access flags
511 *
512 * Traces the parameters and calls the DMA function to copy the PTEs.
513 */
514 static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
515 uint64_t pe, uint64_t addr,
516 unsigned count, uint32_t incr,
517 uint32_t flags)
518 {
519 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
520
521 amdgpu_vm_copy_pte(params->adev, params->ib, pe,
522 (params->src + (addr >> 12) * 8), count);
523 }
524
525 /**
526 * amdgpu_vm_clear_bo - initially clear the page dir/table
527 *
528 * @adev: amdgpu_device pointer
529 * @bo: bo to clear
530 *
531 * need to reserve bo first before calling it.
532 */
533 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
534 struct amdgpu_vm *vm,
535 struct amdgpu_bo *bo)
536 {
537 struct amdgpu_ring *ring;
538 struct fence *fence = NULL;
539 struct amdgpu_job *job;
540 struct amdgpu_pte_update_params params;
541 unsigned entries;
542 uint64_t addr;
543 int r;
544
545 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
546
547 r = reservation_object_reserve_shared(bo->tbo.resv);
548 if (r)
549 return r;
550
551 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
552 if (r)
553 goto error;
554
555 addr = amdgpu_bo_gpu_offset(bo);
556 entries = amdgpu_bo_size(bo) / 8;
557
558 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
559 if (r)
560 goto error;
561
562 memset(&params, 0, sizeof(params));
563 params.adev = adev;
564 params.ib = &job->ibs[0];
565 amdgpu_vm_do_set_ptes(&params, addr, 0, entries, 0, 0);
566 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
567
568 WARN_ON(job->ibs[0].length_dw > 64);
569 r = amdgpu_job_submit(job, ring, &vm->entity,
570 AMDGPU_FENCE_OWNER_VM, &fence);
571 if (r)
572 goto error_free;
573
574 amdgpu_bo_fence(bo, fence, true);
575 fence_put(fence);
576 return 0;
577
578 error_free:
579 amdgpu_job_free(job);
580
581 error:
582 return r;
583 }
584
585 /**
586 * amdgpu_vm_map_gart - Resolve gart mapping of addr
587 *
588 * @pages_addr: optional DMA address to use for lookup
589 * @addr: the unmapped addr
590 *
591 * Look up the physical address of the page that the pte resolves
592 * to and return the pointer for the page table entry.
593 */
594 static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
595 {
596 uint64_t result;
597
598 /* page table offset */
599 result = pages_addr[addr >> PAGE_SHIFT];
600
601 /* in case cpu page size != gpu page size*/
602 result |= addr & (~PAGE_MASK);
603
604 result &= 0xFFFFFFFFFFFFF000ULL;
605
606 return result;
607 }
608
609 static int amdgpu_vm_update_pd_or_shadow(struct amdgpu_device *adev,
610 struct amdgpu_vm *vm,
611 bool shadow)
612 {
613 struct amdgpu_ring *ring;
614 struct amdgpu_bo *pd = shadow ? vm->page_directory->shadow :
615 vm->page_directory;
616 uint64_t pd_addr;
617 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
618 uint64_t last_pde = ~0, last_pt = ~0;
619 unsigned count = 0, pt_idx, ndw;
620 struct amdgpu_job *job;
621 struct amdgpu_pte_update_params params;
622 struct fence *fence = NULL;
623
624 int r;
625
626 if (!pd)
627 return 0;
628 pd_addr = amdgpu_bo_gpu_offset(pd);
629 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
630
631 /* padding, etc. */
632 ndw = 64;
633
634 /* assume the worst case */
635 ndw += vm->max_pde_used * 6;
636
637 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
638 if (r)
639 return r;
640
641 memset(&params, 0, sizeof(params));
642 params.adev = adev;
643 params.ib = &job->ibs[0];
644
645 /* walk over the address space and update the page directory */
646 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
647 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
648 uint64_t pde, pt;
649
650 if (bo == NULL)
651 continue;
652
653 pt = amdgpu_bo_gpu_offset(bo);
654 if (!shadow) {
655 if (vm->page_tables[pt_idx].addr == pt)
656 continue;
657 vm->page_tables[pt_idx].addr = pt;
658 } else {
659 if (vm->page_tables[pt_idx].shadow_addr == pt)
660 continue;
661 vm->page_tables[pt_idx].shadow_addr = pt;
662 }
663
664 pde = pd_addr + pt_idx * 8;
665 if (((last_pde + 8 * count) != pde) ||
666 ((last_pt + incr * count) != pt) ||
667 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
668
669 if (count) {
670 amdgpu_vm_do_set_ptes(&params, last_pde,
671 last_pt, count, incr,
672 AMDGPU_PTE_VALID);
673 }
674
675 count = 1;
676 last_pde = pde;
677 last_pt = pt;
678 } else {
679 ++count;
680 }
681 }
682
683 if (count)
684 amdgpu_vm_do_set_ptes(&params, last_pde, last_pt,
685 count, incr, AMDGPU_PTE_VALID);
686
687 if (params.ib->length_dw != 0) {
688 amdgpu_ring_pad_ib(ring, params.ib);
689 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
690 AMDGPU_FENCE_OWNER_VM);
691 WARN_ON(params.ib->length_dw > ndw);
692 r = amdgpu_job_submit(job, ring, &vm->entity,
693 AMDGPU_FENCE_OWNER_VM, &fence);
694 if (r)
695 goto error_free;
696
697 amdgpu_bo_fence(pd, fence, true);
698 fence_put(vm->page_directory_fence);
699 vm->page_directory_fence = fence_get(fence);
700 fence_put(fence);
701
702 } else {
703 amdgpu_job_free(job);
704 }
705
706 return 0;
707
708 error_free:
709 amdgpu_job_free(job);
710 return r;
711 }
712
713 /*
714 * amdgpu_vm_update_pdes - make sure that page directory is valid
715 *
716 * @adev: amdgpu_device pointer
717 * @vm: requested vm
718 * @start: start of GPU address range
719 * @end: end of GPU address range
720 *
721 * Allocates new page tables if necessary
722 * and updates the page directory.
723 * Returns 0 for success, error for failure.
724 */
725 int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
726 struct amdgpu_vm *vm)
727 {
728 int r;
729
730 r = amdgpu_vm_update_pd_or_shadow(adev, vm, true);
731 if (r)
732 return r;
733 return amdgpu_vm_update_pd_or_shadow(adev, vm, false);
734 }
735
736 /**
737 * amdgpu_vm_update_ptes - make sure that page tables are valid
738 *
739 * @params: see amdgpu_pte_update_params definition
740 * @vm: requested vm
741 * @start: start of GPU address range
742 * @end: end of GPU address range
743 * @dst: destination address to map to, the next dst inside the function
744 * @flags: mapping flags
745 *
746 * Update the page tables in the range @start - @end.
747 */
748 static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
749 struct amdgpu_vm *vm,
750 uint64_t start, uint64_t end,
751 uint64_t dst, uint32_t flags)
752 {
753 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
754
755 uint64_t cur_pe_start, cur_nptes, cur_dst;
756 uint64_t addr; /* next GPU address to be updated */
757 uint64_t pt_idx;
758 struct amdgpu_bo *pt;
759 unsigned nptes; /* next number of ptes to be updated */
760 uint64_t next_pe_start;
761
762 /* initialize the variables */
763 addr = start;
764 pt_idx = addr >> amdgpu_vm_block_size;
765 pt = vm->page_tables[pt_idx].entry.robj;
766 if (params->shadow) {
767 if (!pt->shadow)
768 return;
769 pt = vm->page_tables[pt_idx].entry.robj->shadow;
770 }
771 if ((addr & ~mask) == (end & ~mask))
772 nptes = end - addr;
773 else
774 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
775
776 cur_pe_start = amdgpu_bo_gpu_offset(pt);
777 cur_pe_start += (addr & mask) * 8;
778 cur_nptes = nptes;
779 cur_dst = dst;
780
781 /* for next ptb*/
782 addr += nptes;
783 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
784
785 /* walk over the address space and update the page tables */
786 while (addr < end) {
787 pt_idx = addr >> amdgpu_vm_block_size;
788 pt = vm->page_tables[pt_idx].entry.robj;
789 if (params->shadow) {
790 if (!pt->shadow)
791 return;
792 pt = vm->page_tables[pt_idx].entry.robj->shadow;
793 }
794
795 if ((addr & ~mask) == (end & ~mask))
796 nptes = end - addr;
797 else
798 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
799
800 next_pe_start = amdgpu_bo_gpu_offset(pt);
801 next_pe_start += (addr & mask) * 8;
802
803 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
804 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
805 /* The next ptb is consecutive to current ptb.
806 * Don't call the update function now.
807 * Will update two ptbs together in future.
808 */
809 cur_nptes += nptes;
810 } else {
811 params->func(params, cur_pe_start, cur_dst, cur_nptes,
812 AMDGPU_GPU_PAGE_SIZE, flags);
813
814 cur_pe_start = next_pe_start;
815 cur_nptes = nptes;
816 cur_dst = dst;
817 }
818
819 /* for next ptb*/
820 addr += nptes;
821 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
822 }
823
824 params->func(params, cur_pe_start, cur_dst, cur_nptes,
825 AMDGPU_GPU_PAGE_SIZE, flags);
826 }
827
828 /*
829 * amdgpu_vm_frag_ptes - add fragment information to PTEs
830 *
831 * @params: see amdgpu_pte_update_params definition
832 * @vm: requested vm
833 * @start: first PTE to handle
834 * @end: last PTE to handle
835 * @dst: addr those PTEs should point to
836 * @flags: hw mapping flags
837 */
838 static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
839 struct amdgpu_vm *vm,
840 uint64_t start, uint64_t end,
841 uint64_t dst, uint32_t flags)
842 {
843 /**
844 * The MC L1 TLB supports variable sized pages, based on a fragment
845 * field in the PTE. When this field is set to a non-zero value, page
846 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
847 * flags are considered valid for all PTEs within the fragment range
848 * and corresponding mappings are assumed to be physically contiguous.
849 *
850 * The L1 TLB can store a single PTE for the whole fragment,
851 * significantly increasing the space available for translation
852 * caching. This leads to large improvements in throughput when the
853 * TLB is under pressure.
854 *
855 * The L2 TLB distributes small and large fragments into two
856 * asymmetric partitions. The large fragment cache is significantly
857 * larger. Thus, we try to use large fragments wherever possible.
858 * Userspace can support this by aligning virtual base address and
859 * allocation size to the fragment size.
860 */
861
862 const uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
863
864 uint64_t frag_start = ALIGN(start, frag_align);
865 uint64_t frag_end = end & ~(frag_align - 1);
866
867 uint32_t frag;
868
869 /* system pages are non continuously */
870 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
871 (frag_start >= frag_end)) {
872
873 amdgpu_vm_update_ptes(params, vm, start, end, dst, flags);
874 return;
875 }
876
877 /* use more than 64KB fragment size if possible */
878 frag = lower_32_bits(frag_start | frag_end);
879 frag = likely(frag) ? __ffs(frag) : 31;
880
881 /* handle the 4K area at the beginning */
882 if (start != frag_start) {
883 amdgpu_vm_update_ptes(params, vm, start, frag_start,
884 dst, flags);
885 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
886 }
887
888 /* handle the area in the middle */
889 amdgpu_vm_update_ptes(params, vm, frag_start, frag_end, dst,
890 flags | AMDGPU_PTE_FRAG(frag));
891
892 /* handle the 4K area at the end */
893 if (frag_end != end) {
894 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
895 amdgpu_vm_update_ptes(params, vm, frag_end, end, dst, flags);
896 }
897 }
898
899 /**
900 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
901 *
902 * @adev: amdgpu_device pointer
903 * @exclusive: fence we need to sync to
904 * @src: address where to copy page table entries from
905 * @pages_addr: DMA addresses to use for mapping
906 * @vm: requested vm
907 * @start: start of mapped range
908 * @last: last mapped entry
909 * @flags: flags for the entries
910 * @addr: addr to set the area to
911 * @fence: optional resulting fence
912 *
913 * Fill in the page table entries between @start and @last.
914 * Returns 0 for success, -EINVAL for failure.
915 */
916 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
917 struct fence *exclusive,
918 uint64_t src,
919 dma_addr_t *pages_addr,
920 struct amdgpu_vm *vm,
921 uint64_t start, uint64_t last,
922 uint32_t flags, uint64_t addr,
923 struct fence **fence)
924 {
925 struct amdgpu_ring *ring;
926 void *owner = AMDGPU_FENCE_OWNER_VM;
927 unsigned nptes, ncmds, ndw;
928 struct amdgpu_job *job;
929 struct amdgpu_pte_update_params params;
930 struct fence *f = NULL;
931 int r;
932
933 memset(&params, 0, sizeof(params));
934 params.adev = adev;
935 params.src = src;
936
937 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
938
939 memset(&params, 0, sizeof(params));
940 params.adev = adev;
941 params.src = src;
942
943 /* sync to everything on unmapping */
944 if (!(flags & AMDGPU_PTE_VALID))
945 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
946
947 nptes = last - start + 1;
948
949 /*
950 * reserve space for one command every (1 << BLOCK_SIZE)
951 * entries or 2k dwords (whatever is smaller)
952 */
953 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
954
955 /* padding, etc. */
956 ndw = 64;
957
958 if (src) {
959 /* only copy commands needed */
960 ndw += ncmds * 7;
961
962 params.func = amdgpu_vm_do_copy_ptes;
963
964 } else if (pages_addr) {
965 /* copy commands needed */
966 ndw += ncmds * 7;
967
968 /* and also PTEs */
969 ndw += nptes * 2;
970
971 params.func = amdgpu_vm_do_copy_ptes;
972
973 } else {
974 /* set page commands needed */
975 ndw += ncmds * 10;
976
977 /* two extra commands for begin/end of fragment */
978 ndw += 2 * 10;
979
980 params.func = amdgpu_vm_do_set_ptes;
981 }
982
983 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
984 if (r)
985 return r;
986
987 params.ib = &job->ibs[0];
988
989 if (!src && pages_addr) {
990 uint64_t *pte;
991 unsigned i;
992
993 /* Put the PTEs at the end of the IB. */
994 i = ndw - nptes * 2;
995 pte= (uint64_t *)&(job->ibs->ptr[i]);
996 params.src = job->ibs->gpu_addr + i * 4;
997
998 for (i = 0; i < nptes; ++i) {
999 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1000 AMDGPU_GPU_PAGE_SIZE);
1001 pte[i] |= flags;
1002 }
1003 }
1004
1005 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1006 if (r)
1007 goto error_free;
1008
1009 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
1010 owner);
1011 if (r)
1012 goto error_free;
1013
1014 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
1015 if (r)
1016 goto error_free;
1017
1018 params.shadow = true;
1019 amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
1020 params.shadow = false;
1021 amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
1022
1023 amdgpu_ring_pad_ib(ring, params.ib);
1024 WARN_ON(params.ib->length_dw > ndw);
1025 r = amdgpu_job_submit(job, ring, &vm->entity,
1026 AMDGPU_FENCE_OWNER_VM, &f);
1027 if (r)
1028 goto error_free;
1029
1030 amdgpu_bo_fence(vm->page_directory, f, true);
1031 if (fence) {
1032 fence_put(*fence);
1033 *fence = fence_get(f);
1034 }
1035 fence_put(f);
1036 return 0;
1037
1038 error_free:
1039 amdgpu_job_free(job);
1040 return r;
1041 }
1042
1043 /**
1044 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1045 *
1046 * @adev: amdgpu_device pointer
1047 * @exclusive: fence we need to sync to
1048 * @gtt_flags: flags as they are used for GTT
1049 * @pages_addr: DMA addresses to use for mapping
1050 * @vm: requested vm
1051 * @mapping: mapped range and flags to use for the update
1052 * @addr: addr to set the area to
1053 * @flags: HW flags for the mapping
1054 * @fence: optional resulting fence
1055 *
1056 * Split the mapping into smaller chunks so that each update fits
1057 * into a SDMA IB.
1058 * Returns 0 for success, -EINVAL for failure.
1059 */
1060 static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
1061 struct fence *exclusive,
1062 uint32_t gtt_flags,
1063 dma_addr_t *pages_addr,
1064 struct amdgpu_vm *vm,
1065 struct amdgpu_bo_va_mapping *mapping,
1066 uint32_t flags, uint64_t addr,
1067 struct fence **fence)
1068 {
1069 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
1070
1071 uint64_t src = 0, start = mapping->it.start;
1072 int r;
1073
1074 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1075 * but in case of something, we filter the flags in first place
1076 */
1077 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1078 flags &= ~AMDGPU_PTE_READABLE;
1079 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1080 flags &= ~AMDGPU_PTE_WRITEABLE;
1081
1082 trace_amdgpu_vm_bo_update(mapping);
1083
1084 if (pages_addr) {
1085 if (flags == gtt_flags)
1086 src = adev->gart.table_addr + (addr >> 12) * 8;
1087 addr = 0;
1088 }
1089 addr += mapping->offset;
1090
1091 if (!pages_addr || src)
1092 return amdgpu_vm_bo_update_mapping(adev, exclusive,
1093 src, pages_addr, vm,
1094 start, mapping->it.last,
1095 flags, addr, fence);
1096
1097 while (start != mapping->it.last + 1) {
1098 uint64_t last;
1099
1100 last = min((uint64_t)mapping->it.last, start + max_size - 1);
1101 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1102 src, pages_addr, vm,
1103 start, last, flags, addr,
1104 fence);
1105 if (r)
1106 return r;
1107
1108 start = last + 1;
1109 addr += max_size * AMDGPU_GPU_PAGE_SIZE;
1110 }
1111
1112 return 0;
1113 }
1114
1115 /**
1116 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1117 *
1118 * @adev: amdgpu_device pointer
1119 * @bo_va: requested BO and VM object
1120 * @mem: ttm mem
1121 *
1122 * Fill in the page table entries for @bo_va.
1123 * Returns 0 for success, -EINVAL for failure.
1124 *
1125 * Object have to be reserved and mutex must be locked!
1126 */
1127 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1128 struct amdgpu_bo_va *bo_va,
1129 struct ttm_mem_reg *mem)
1130 {
1131 struct amdgpu_vm *vm = bo_va->vm;
1132 struct amdgpu_bo_va_mapping *mapping;
1133 dma_addr_t *pages_addr = NULL;
1134 uint32_t gtt_flags, flags;
1135 struct fence *exclusive;
1136 uint64_t addr;
1137 int r;
1138
1139 if (mem) {
1140 struct ttm_dma_tt *ttm;
1141
1142 addr = (u64)mem->start << PAGE_SHIFT;
1143 switch (mem->mem_type) {
1144 case TTM_PL_TT:
1145 ttm = container_of(bo_va->bo->tbo.ttm, struct
1146 ttm_dma_tt, ttm);
1147 pages_addr = ttm->dma_address;
1148 break;
1149
1150 case TTM_PL_VRAM:
1151 addr += adev->vm_manager.vram_base_offset;
1152 break;
1153
1154 default:
1155 break;
1156 }
1157
1158 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
1159 } else {
1160 addr = 0;
1161 exclusive = NULL;
1162 }
1163
1164 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1165 gtt_flags = (adev == bo_va->bo->adev) ? flags : 0;
1166
1167 spin_lock(&vm->status_lock);
1168 if (!list_empty(&bo_va->vm_status))
1169 list_splice_init(&bo_va->valids, &bo_va->invalids);
1170 spin_unlock(&vm->status_lock);
1171
1172 list_for_each_entry(mapping, &bo_va->invalids, list) {
1173 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1174 gtt_flags, pages_addr, vm,
1175 mapping, flags, addr,
1176 &bo_va->last_pt_update);
1177 if (r)
1178 return r;
1179 }
1180
1181 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1182 list_for_each_entry(mapping, &bo_va->valids, list)
1183 trace_amdgpu_vm_bo_mapping(mapping);
1184
1185 list_for_each_entry(mapping, &bo_va->invalids, list)
1186 trace_amdgpu_vm_bo_mapping(mapping);
1187 }
1188
1189 spin_lock(&vm->status_lock);
1190 list_splice_init(&bo_va->invalids, &bo_va->valids);
1191 list_del_init(&bo_va->vm_status);
1192 if (!mem)
1193 list_add(&bo_va->vm_status, &vm->cleared);
1194 spin_unlock(&vm->status_lock);
1195
1196 return 0;
1197 }
1198
1199 /**
1200 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1201 *
1202 * @adev: amdgpu_device pointer
1203 * @vm: requested vm
1204 *
1205 * Make sure all freed BOs are cleared in the PT.
1206 * Returns 0 for success.
1207 *
1208 * PTs have to be reserved and mutex must be locked!
1209 */
1210 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1211 struct amdgpu_vm *vm)
1212 {
1213 struct amdgpu_bo_va_mapping *mapping;
1214 int r;
1215
1216 while (!list_empty(&vm->freed)) {
1217 mapping = list_first_entry(&vm->freed,
1218 struct amdgpu_bo_va_mapping, list);
1219 list_del(&mapping->list);
1220
1221 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
1222 0, 0, NULL);
1223 kfree(mapping);
1224 if (r)
1225 return r;
1226
1227 }
1228 return 0;
1229
1230 }
1231
1232 /**
1233 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1234 *
1235 * @adev: amdgpu_device pointer
1236 * @vm: requested vm
1237 *
1238 * Make sure all invalidated BOs are cleared in the PT.
1239 * Returns 0 for success.
1240 *
1241 * PTs have to be reserved and mutex must be locked!
1242 */
1243 int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
1244 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
1245 {
1246 struct amdgpu_bo_va *bo_va = NULL;
1247 int r = 0;
1248
1249 spin_lock(&vm->status_lock);
1250 while (!list_empty(&vm->invalidated)) {
1251 bo_va = list_first_entry(&vm->invalidated,
1252 struct amdgpu_bo_va, vm_status);
1253 spin_unlock(&vm->status_lock);
1254
1255 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
1256 if (r)
1257 return r;
1258
1259 spin_lock(&vm->status_lock);
1260 }
1261 spin_unlock(&vm->status_lock);
1262
1263 if (bo_va)
1264 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
1265
1266 return r;
1267 }
1268
1269 /**
1270 * amdgpu_vm_bo_add - add a bo to a specific vm
1271 *
1272 * @adev: amdgpu_device pointer
1273 * @vm: requested vm
1274 * @bo: amdgpu buffer object
1275 *
1276 * Add @bo into the requested vm.
1277 * Add @bo to the list of bos associated with the vm
1278 * Returns newly added bo_va or NULL for failure
1279 *
1280 * Object has to be reserved!
1281 */
1282 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1283 struct amdgpu_vm *vm,
1284 struct amdgpu_bo *bo)
1285 {
1286 struct amdgpu_bo_va *bo_va;
1287
1288 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1289 if (bo_va == NULL) {
1290 return NULL;
1291 }
1292 bo_va->vm = vm;
1293 bo_va->bo = bo;
1294 bo_va->ref_count = 1;
1295 INIT_LIST_HEAD(&bo_va->bo_list);
1296 INIT_LIST_HEAD(&bo_va->valids);
1297 INIT_LIST_HEAD(&bo_va->invalids);
1298 INIT_LIST_HEAD(&bo_va->vm_status);
1299
1300 list_add_tail(&bo_va->bo_list, &bo->va);
1301
1302 return bo_va;
1303 }
1304
1305 /**
1306 * amdgpu_vm_bo_map - map bo inside a vm
1307 *
1308 * @adev: amdgpu_device pointer
1309 * @bo_va: bo_va to store the address
1310 * @saddr: where to map the BO
1311 * @offset: requested offset in the BO
1312 * @flags: attributes of pages (read/write/valid/etc.)
1313 *
1314 * Add a mapping of the BO at the specefied addr into the VM.
1315 * Returns 0 for success, error for failure.
1316 *
1317 * Object has to be reserved and unreserved outside!
1318 */
1319 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1320 struct amdgpu_bo_va *bo_va,
1321 uint64_t saddr, uint64_t offset,
1322 uint64_t size, uint32_t flags)
1323 {
1324 struct amdgpu_bo_va_mapping *mapping;
1325 struct amdgpu_vm *vm = bo_va->vm;
1326 struct interval_tree_node *it;
1327 unsigned last_pfn, pt_idx;
1328 uint64_t eaddr;
1329 int r;
1330
1331 /* validate the parameters */
1332 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1333 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1334 return -EINVAL;
1335
1336 /* make sure object fit at this offset */
1337 eaddr = saddr + size - 1;
1338 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
1339 return -EINVAL;
1340
1341 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
1342 if (last_pfn >= adev->vm_manager.max_pfn) {
1343 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
1344 last_pfn, adev->vm_manager.max_pfn);
1345 return -EINVAL;
1346 }
1347
1348 saddr /= AMDGPU_GPU_PAGE_SIZE;
1349 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1350
1351 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
1352 if (it) {
1353 struct amdgpu_bo_va_mapping *tmp;
1354 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1355 /* bo and tmp overlap, invalid addr */
1356 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1357 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1358 tmp->it.start, tmp->it.last + 1);
1359 r = -EINVAL;
1360 goto error;
1361 }
1362
1363 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1364 if (!mapping) {
1365 r = -ENOMEM;
1366 goto error;
1367 }
1368
1369 INIT_LIST_HEAD(&mapping->list);
1370 mapping->it.start = saddr;
1371 mapping->it.last = eaddr;
1372 mapping->offset = offset;
1373 mapping->flags = flags;
1374
1375 list_add(&mapping->list, &bo_va->invalids);
1376 interval_tree_insert(&mapping->it, &vm->va);
1377
1378 /* Make sure the page tables are allocated */
1379 saddr >>= amdgpu_vm_block_size;
1380 eaddr >>= amdgpu_vm_block_size;
1381
1382 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1383
1384 if (eaddr > vm->max_pde_used)
1385 vm->max_pde_used = eaddr;
1386
1387 /* walk over the address space and allocate the page tables */
1388 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
1389 struct reservation_object *resv = vm->page_directory->tbo.resv;
1390 struct amdgpu_bo_list_entry *entry;
1391 struct amdgpu_bo *pt;
1392
1393 entry = &vm->page_tables[pt_idx].entry;
1394 if (entry->robj)
1395 continue;
1396
1397 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1398 AMDGPU_GPU_PAGE_SIZE, true,
1399 AMDGPU_GEM_DOMAIN_VRAM,
1400 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
1401 AMDGPU_GEM_CREATE_SHADOW,
1402 NULL, resv, &pt);
1403 if (r)
1404 goto error_free;
1405
1406 /* Keep a reference to the page table to avoid freeing
1407 * them up in the wrong order.
1408 */
1409 pt->parent = amdgpu_bo_ref(vm->page_directory);
1410
1411 r = amdgpu_vm_clear_bo(adev, vm, pt);
1412 if (r) {
1413 amdgpu_bo_unref(&pt);
1414 goto error_free;
1415 }
1416
1417 entry->robj = pt;
1418 entry->priority = 0;
1419 entry->tv.bo = &entry->robj->tbo;
1420 entry->tv.shared = true;
1421 entry->user_pages = NULL;
1422 vm->page_tables[pt_idx].addr = 0;
1423 }
1424
1425 return 0;
1426
1427 error_free:
1428 list_del(&mapping->list);
1429 interval_tree_remove(&mapping->it, &vm->va);
1430 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1431 kfree(mapping);
1432
1433 error:
1434 return r;
1435 }
1436
1437 /**
1438 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1439 *
1440 * @adev: amdgpu_device pointer
1441 * @bo_va: bo_va to remove the address from
1442 * @saddr: where to the BO is mapped
1443 *
1444 * Remove a mapping of the BO at the specefied addr from the VM.
1445 * Returns 0 for success, error for failure.
1446 *
1447 * Object has to be reserved and unreserved outside!
1448 */
1449 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1450 struct amdgpu_bo_va *bo_va,
1451 uint64_t saddr)
1452 {
1453 struct amdgpu_bo_va_mapping *mapping;
1454 struct amdgpu_vm *vm = bo_va->vm;
1455 bool valid = true;
1456
1457 saddr /= AMDGPU_GPU_PAGE_SIZE;
1458
1459 list_for_each_entry(mapping, &bo_va->valids, list) {
1460 if (mapping->it.start == saddr)
1461 break;
1462 }
1463
1464 if (&mapping->list == &bo_va->valids) {
1465 valid = false;
1466
1467 list_for_each_entry(mapping, &bo_va->invalids, list) {
1468 if (mapping->it.start == saddr)
1469 break;
1470 }
1471
1472 if (&mapping->list == &bo_va->invalids)
1473 return -ENOENT;
1474 }
1475
1476 list_del(&mapping->list);
1477 interval_tree_remove(&mapping->it, &vm->va);
1478 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1479
1480 if (valid)
1481 list_add(&mapping->list, &vm->freed);
1482 else
1483 kfree(mapping);
1484
1485 return 0;
1486 }
1487
1488 /**
1489 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1490 *
1491 * @adev: amdgpu_device pointer
1492 * @bo_va: requested bo_va
1493 *
1494 * Remove @bo_va->bo from the requested vm.
1495 *
1496 * Object have to be reserved!
1497 */
1498 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1499 struct amdgpu_bo_va *bo_va)
1500 {
1501 struct amdgpu_bo_va_mapping *mapping, *next;
1502 struct amdgpu_vm *vm = bo_va->vm;
1503
1504 list_del(&bo_va->bo_list);
1505
1506 spin_lock(&vm->status_lock);
1507 list_del(&bo_va->vm_status);
1508 spin_unlock(&vm->status_lock);
1509
1510 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
1511 list_del(&mapping->list);
1512 interval_tree_remove(&mapping->it, &vm->va);
1513 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1514 list_add(&mapping->list, &vm->freed);
1515 }
1516 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1517 list_del(&mapping->list);
1518 interval_tree_remove(&mapping->it, &vm->va);
1519 kfree(mapping);
1520 }
1521
1522 fence_put(bo_va->last_pt_update);
1523 kfree(bo_va);
1524 }
1525
1526 /**
1527 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1528 *
1529 * @adev: amdgpu_device pointer
1530 * @vm: requested vm
1531 * @bo: amdgpu buffer object
1532 *
1533 * Mark @bo as invalid.
1534 */
1535 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1536 struct amdgpu_bo *bo)
1537 {
1538 struct amdgpu_bo_va *bo_va;
1539
1540 list_for_each_entry(bo_va, &bo->va, bo_list) {
1541 spin_lock(&bo_va->vm->status_lock);
1542 if (list_empty(&bo_va->vm_status))
1543 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
1544 spin_unlock(&bo_va->vm->status_lock);
1545 }
1546 }
1547
1548 /**
1549 * amdgpu_vm_init - initialize a vm instance
1550 *
1551 * @adev: amdgpu_device pointer
1552 * @vm: requested vm
1553 *
1554 * Init @vm fields.
1555 */
1556 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1557 {
1558 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1559 AMDGPU_VM_PTE_COUNT * 8);
1560 unsigned pd_size, pd_entries;
1561 unsigned ring_instance;
1562 struct amdgpu_ring *ring;
1563 struct amd_sched_rq *rq;
1564 int i, r;
1565
1566 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1567 vm->ids[i] = NULL;
1568 vm->va = RB_ROOT;
1569 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
1570 spin_lock_init(&vm->status_lock);
1571 INIT_LIST_HEAD(&vm->invalidated);
1572 INIT_LIST_HEAD(&vm->cleared);
1573 INIT_LIST_HEAD(&vm->freed);
1574
1575 pd_size = amdgpu_vm_directory_size(adev);
1576 pd_entries = amdgpu_vm_num_pdes(adev);
1577
1578 /* allocate page table array */
1579 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
1580 if (vm->page_tables == NULL) {
1581 DRM_ERROR("Cannot allocate memory for page table array\n");
1582 return -ENOMEM;
1583 }
1584
1585 /* create scheduler entity for page table updates */
1586
1587 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1588 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1589 ring = adev->vm_manager.vm_pte_rings[ring_instance];
1590 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1591 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1592 rq, amdgpu_sched_jobs);
1593 if (r)
1594 return r;
1595
1596 vm->page_directory_fence = NULL;
1597
1598 r = amdgpu_bo_create(adev, pd_size, align, true,
1599 AMDGPU_GEM_DOMAIN_VRAM,
1600 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
1601 AMDGPU_GEM_CREATE_SHADOW,
1602 NULL, NULL, &vm->page_directory);
1603 if (r)
1604 goto error_free_sched_entity;
1605
1606 r = amdgpu_bo_reserve(vm->page_directory, false);
1607 if (r)
1608 goto error_free_page_directory;
1609
1610 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
1611 amdgpu_bo_unreserve(vm->page_directory);
1612 if (r)
1613 goto error_free_page_directory;
1614 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
1615
1616 return 0;
1617
1618 error_free_page_directory:
1619 amdgpu_bo_unref(&vm->page_directory);
1620 vm->page_directory = NULL;
1621
1622 error_free_sched_entity:
1623 amd_sched_entity_fini(&ring->sched, &vm->entity);
1624
1625 return r;
1626 }
1627
1628 /**
1629 * amdgpu_vm_fini - tear down a vm instance
1630 *
1631 * @adev: amdgpu_device pointer
1632 * @vm: requested vm
1633 *
1634 * Tear down @vm.
1635 * Unbind the VM and remove all bos from the vm bo list
1636 */
1637 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1638 {
1639 struct amdgpu_bo_va_mapping *mapping, *tmp;
1640 int i;
1641
1642 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
1643
1644 if (!RB_EMPTY_ROOT(&vm->va)) {
1645 dev_err(adev->dev, "still active bo inside vm\n");
1646 }
1647 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1648 list_del(&mapping->list);
1649 interval_tree_remove(&mapping->it, &vm->va);
1650 kfree(mapping);
1651 }
1652 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1653 list_del(&mapping->list);
1654 kfree(mapping);
1655 }
1656
1657 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) {
1658 if (vm->page_tables[i].entry.robj &&
1659 vm->page_tables[i].entry.robj->shadow)
1660 amdgpu_bo_unref(&vm->page_tables[i].entry.robj->shadow);
1661 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
1662 }
1663 drm_free_large(vm->page_tables);
1664
1665 if (vm->page_directory->shadow)
1666 amdgpu_bo_unref(&vm->page_directory->shadow);
1667 amdgpu_bo_unref(&vm->page_directory);
1668 fence_put(vm->page_directory_fence);
1669 }
1670
1671 /**
1672 * amdgpu_vm_manager_init - init the VM manager
1673 *
1674 * @adev: amdgpu_device pointer
1675 *
1676 * Initialize the VM manager structures
1677 */
1678 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1679 {
1680 unsigned i;
1681
1682 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1683
1684 /* skip over VMID 0, since it is the system VM */
1685 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1686 amdgpu_vm_reset_id(adev, i);
1687 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
1688 list_add_tail(&adev->vm_manager.ids[i].list,
1689 &adev->vm_manager.ids_lru);
1690 }
1691
1692 adev->vm_manager.fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1693 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1694 adev->vm_manager.seqno[i] = 0;
1695
1696 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
1697 atomic64_set(&adev->vm_manager.client_counter, 0);
1698 }
1699
1700 /**
1701 * amdgpu_vm_manager_fini - cleanup VM manager
1702 *
1703 * @adev: amdgpu_device pointer
1704 *
1705 * Cleanup the VM manager and free resources.
1706 */
1707 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1708 {
1709 unsigned i;
1710
1711 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
1712 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
1713
1714 fence_put(adev->vm_manager.ids[i].first);
1715 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
1716 fence_put(id->flushed_updates);
1717 }
1718 }