blob: fd8395f25723980da3ec0e45e0be7ab343ec1119 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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 <drm/drmP.h>
29#include <drm/amdgpu_drm.h>
30#include "amdgpu.h"
31#include "amdgpu_trace.h"
32
33/*
34 * GPUVM
35 * GPUVM is similar to the legacy gart on older asics, however
36 * rather than there being a single global gart table
37 * for the entire GPU, there are multiple VM page tables active
38 * at any given time. The VM page tables can contain a mix
39 * vram pages and system memory pages and system memory pages
40 * can be mapped as snooped (cached system pages) or unsnooped
41 * (uncached system pages).
42 * Each VM has an ID associated with it and there is a page table
43 * associated with each VMID. When execting a command buffer,
44 * the kernel tells the the ring what VMID to use for that command
45 * buffer. VMIDs are allocated dynamically as commands are submitted.
46 * The userspace drivers maintain their own address space and the kernel
47 * sets up their pages tables accordingly when they submit their
48 * command buffers and a VMID is assigned.
49 * Cayman/Trinity support up to 8 active VMs at any given time;
50 * SI supports 16.
51 */
52
53/**
54 * amdgpu_vm_num_pde - return the number of page directory entries
55 *
56 * @adev: amdgpu_device pointer
57 *
58 * Calculate the number of page directory entries (cayman+).
59 */
60static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
61{
62 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
63}
64
65/**
66 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
67 *
68 * @adev: amdgpu_device pointer
69 *
70 * Calculate the size of the page directory in bytes (cayman+).
71 */
72static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
73{
74 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
75}
76
77/**
78 * amdgpu_vm_get_bos - add the vm BOs to a validation list
79 *
80 * @vm: vm providing the BOs
81 * @head: head of validation list
82 *
83 * Add the page directory to the list of BOs to
84 * validate for command submission (cayman+).
85 */
86struct amdgpu_bo_list_entry *amdgpu_vm_get_bos(struct amdgpu_device *adev,
87 struct amdgpu_vm *vm,
88 struct list_head *head)
89{
90 struct amdgpu_bo_list_entry *list;
91 unsigned i, idx;
92
monk.liu3d5a08c2015-05-26 10:22:41 +080093 mutex_lock(&vm->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 list = drm_malloc_ab(vm->max_pde_used + 2,
95 sizeof(struct amdgpu_bo_list_entry));
monk.liu3d5a08c2015-05-26 10:22:41 +080096 if (!list) {
97 mutex_unlock(&vm->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 return NULL;
monk.liu3d5a08c2015-05-26 10:22:41 +080099 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100
101 /* add the vm page table to the list */
102 list[0].robj = vm->page_directory;
103 list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
104 list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
105 list[0].priority = 0;
106 list[0].tv.bo = &vm->page_directory->tbo;
107 list[0].tv.shared = true;
108 list_add(&list[0].tv.head, head);
109
110 for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
111 if (!vm->page_tables[i].bo)
112 continue;
113
114 list[idx].robj = vm->page_tables[i].bo;
115 list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
116 list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
117 list[idx].priority = 0;
118 list[idx].tv.bo = &list[idx].robj->tbo;
119 list[idx].tv.shared = true;
120 list_add(&list[idx++].tv.head, head);
121 }
monk.liu3d5a08c2015-05-26 10:22:41 +0800122 mutex_unlock(&vm->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123
124 return list;
125}
126
127/**
128 * amdgpu_vm_grab_id - allocate the next free VMID
129 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200131 * @ring: ring we want to submit job to
132 * @sync: sync object where we add dependencies
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 *
Christian König7f8a5292015-07-20 16:09:40 +0200134 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 *
Christian König7f8a5292015-07-20 16:09:40 +0200136 * Global mutex must be locked!
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 */
Christian König7f8a5292015-07-20 16:09:40 +0200138int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
139 struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140{
141 struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {};
142 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
143 struct amdgpu_device *adev = ring->adev;
144
145 unsigned choices[2] = {};
146 unsigned i;
147
148 /* check if the id is still valid */
149 if (vm_id->id && vm_id->last_id_use &&
150 vm_id->last_id_use == adev->vm_manager.active[vm_id->id])
Christian König7f8a5292015-07-20 16:09:40 +0200151 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152
153 /* we definately need to flush */
154 vm_id->pd_gpu_addr = ~0ll;
155
156 /* skip over VMID 0, since it is the system VM */
157 for (i = 1; i < adev->vm_manager.nvm; ++i) {
158 struct amdgpu_fence *fence = adev->vm_manager.active[i];
159
160 if (fence == NULL) {
161 /* found a free one */
162 vm_id->id = i;
163 trace_amdgpu_vm_grab_id(i, ring->idx);
Christian König7f8a5292015-07-20 16:09:40 +0200164 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 }
166
167 if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) {
168 best[fence->ring->idx] = fence;
169 choices[fence->ring == ring ? 0 : 1] = i;
170 }
171 }
172
173 for (i = 0; i < 2; ++i) {
174 if (choices[i]) {
Christian König7f8a5292015-07-20 16:09:40 +0200175 struct amdgpu_fence *fence;
176
177 fence = adev->vm_manager.active[choices[i]];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178 vm_id->id = choices[i];
Christian König7f8a5292015-07-20 16:09:40 +0200179
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180 trace_amdgpu_vm_grab_id(choices[i], ring->idx);
Christian König7f8a5292015-07-20 16:09:40 +0200181 return amdgpu_sync_fence(ring->adev, sync, &fence->base);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182 }
183 }
184
185 /* should never happen */
186 BUG();
Christian König7f8a5292015-07-20 16:09:40 +0200187 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188}
189
190/**
191 * amdgpu_vm_flush - hardware flush the vm
192 *
193 * @ring: ring to use for flush
194 * @vm: vm we want to flush
195 * @updates: last vm update that we waited for
196 *
197 * Flush the vm (cayman+).
198 *
199 * Global and local mutex must be locked!
200 */
201void amdgpu_vm_flush(struct amdgpu_ring *ring,
202 struct amdgpu_vm *vm,
203 struct amdgpu_fence *updates)
204{
205 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
206 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
Christian Königfc8fa5e2015-07-20 15:47:30 +0200207 struct amdgpu_fence *flushed_updates = vm_id->flushed_updates;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208
Christian Königfc8fa5e2015-07-20 15:47:30 +0200209 if (pd_addr != vm_id->pd_gpu_addr || !flushed_updates ||
210 (updates && amdgpu_fence_is_earlier(flushed_updates, updates))) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211
212 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
Christian Königfc8fa5e2015-07-20 15:47:30 +0200213 vm_id->flushed_updates = amdgpu_fence_ref(
214 amdgpu_fence_later(flushed_updates, updates));
215 amdgpu_fence_unref(&flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216 vm_id->pd_gpu_addr = pd_addr;
217 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
218 }
219}
220
221/**
222 * amdgpu_vm_fence - remember fence for vm
223 *
224 * @adev: amdgpu_device pointer
225 * @vm: vm we want to fence
226 * @fence: fence to remember
227 *
228 * Fence the vm (cayman+).
229 * Set the fence used to protect page table and id.
230 *
231 * Global and local mutex must be locked!
232 */
233void amdgpu_vm_fence(struct amdgpu_device *adev,
234 struct amdgpu_vm *vm,
235 struct amdgpu_fence *fence)
236{
237 unsigned ridx = fence->ring->idx;
238 unsigned vm_id = vm->ids[ridx].id;
239
240 amdgpu_fence_unref(&adev->vm_manager.active[vm_id]);
241 adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence);
242
243 amdgpu_fence_unref(&vm->ids[ridx].last_id_use);
244 vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence);
245}
246
247/**
248 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
249 *
250 * @vm: requested vm
251 * @bo: requested buffer object
252 *
253 * Find @bo inside the requested vm (cayman+).
254 * Search inside the @bos vm list for the requested vm
255 * Returns the found bo_va or NULL if none is found
256 *
257 * Object has to be reserved!
258 */
259struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
260 struct amdgpu_bo *bo)
261{
262 struct amdgpu_bo_va *bo_va;
263
264 list_for_each_entry(bo_va, &bo->va, bo_list) {
265 if (bo_va->vm == vm) {
266 return bo_va;
267 }
268 }
269 return NULL;
270}
271
272/**
273 * amdgpu_vm_update_pages - helper to call the right asic function
274 *
275 * @adev: amdgpu_device pointer
276 * @ib: indirect buffer to fill with commands
277 * @pe: addr of the page entry
278 * @addr: dst addr to write into pe
279 * @count: number of page entries to update
280 * @incr: increase next addr by incr bytes
281 * @flags: hw access flags
282 * @gtt_flags: GTT hw access flags
283 *
284 * Traces the parameters and calls the right asic functions
285 * to setup the page table using the DMA.
286 */
287static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
288 struct amdgpu_ib *ib,
289 uint64_t pe, uint64_t addr,
290 unsigned count, uint32_t incr,
291 uint32_t flags, uint32_t gtt_flags)
292{
293 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
294
295 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
296 uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
297 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
298
299 } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
300 amdgpu_vm_write_pte(adev, ib, pe, addr,
301 count, incr, flags);
302
303 } else {
304 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
305 count, incr, flags);
306 }
307}
308
309/**
310 * amdgpu_vm_clear_bo - initially clear the page dir/table
311 *
312 * @adev: amdgpu_device pointer
313 * @bo: bo to clear
314 */
315static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
316 struct amdgpu_bo *bo)
317{
318 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
319 struct amdgpu_ib ib;
320 unsigned entries;
321 uint64_t addr;
322 int r;
323
324 r = amdgpu_bo_reserve(bo, false);
325 if (r)
326 return r;
327
monk.liuca952612015-05-25 14:44:05 +0800328 r = reservation_object_reserve_shared(bo->tbo.resv);
329 if (r)
330 return r;
331
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
333 if (r)
334 goto error_unreserve;
335
336 addr = amdgpu_bo_gpu_offset(bo);
337 entries = amdgpu_bo_size(bo) / 8;
338
339 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, &ib);
340 if (r)
341 goto error_unreserve;
342
343 ib.length_dw = 0;
344
345 amdgpu_vm_update_pages(adev, &ib, addr, 0, entries, 0, 0, 0);
346 amdgpu_vm_pad_ib(adev, &ib);
347 WARN_ON(ib.length_dw > 64);
348
349 r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
350 if (r)
351 goto error_free;
352
monk.liuaeb0aeac2015-05-26 15:03:45 +0800353 amdgpu_bo_fence(bo, ib.fence, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400354
355error_free:
356 amdgpu_ib_free(adev, &ib);
357
358error_unreserve:
359 amdgpu_bo_unreserve(bo);
360 return r;
361}
362
363/**
364 * amdgpu_vm_map_gart - get the physical address of a gart page
365 *
366 * @adev: amdgpu_device pointer
367 * @addr: the unmapped addr
368 *
369 * Look up the physical address of the page that the pte resolves
370 * to (cayman+).
371 * Returns the physical address of the page.
372 */
373uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
374{
375 uint64_t result;
376
377 /* page table offset */
378 result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
379
380 /* in case cpu page size != gpu page size*/
381 result |= addr & (~PAGE_MASK);
382
383 return result;
384}
385
386/**
387 * amdgpu_vm_update_pdes - make sure that page directory is valid
388 *
389 * @adev: amdgpu_device pointer
390 * @vm: requested vm
391 * @start: start of GPU address range
392 * @end: end of GPU address range
393 *
394 * Allocates new page tables if necessary
395 * and updates the page directory (cayman+).
396 * Returns 0 for success, error for failure.
397 *
398 * Global and local mutex must be locked!
399 */
400int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
401 struct amdgpu_vm *vm)
402{
403 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
404 struct amdgpu_bo *pd = vm->page_directory;
405 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
406 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
407 uint64_t last_pde = ~0, last_pt = ~0;
408 unsigned count = 0, pt_idx, ndw;
409 struct amdgpu_ib ib;
410 int r;
411
412 /* padding, etc. */
413 ndw = 64;
414
415 /* assume the worst case */
416 ndw += vm->max_pde_used * 6;
417
418 /* update too big for an IB */
419 if (ndw > 0xfffff)
420 return -ENOMEM;
421
422 r = amdgpu_ib_get(ring, NULL, ndw * 4, &ib);
423 if (r)
424 return r;
425 ib.length_dw = 0;
426
427 /* walk over the address space and update the page directory */
428 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
429 struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
430 uint64_t pde, pt;
431
432 if (bo == NULL)
433 continue;
434
435 pt = amdgpu_bo_gpu_offset(bo);
436 if (vm->page_tables[pt_idx].addr == pt)
437 continue;
438 vm->page_tables[pt_idx].addr = pt;
439
440 pde = pd_addr + pt_idx * 8;
441 if (((last_pde + 8 * count) != pde) ||
442 ((last_pt + incr * count) != pt)) {
443
444 if (count) {
445 amdgpu_vm_update_pages(adev, &ib, last_pde,
446 last_pt, count, incr,
447 AMDGPU_PTE_VALID, 0);
448 }
449
450 count = 1;
451 last_pde = pde;
452 last_pt = pt;
453 } else {
454 ++count;
455 }
456 }
457
458 if (count)
459 amdgpu_vm_update_pages(adev, &ib, last_pde, last_pt, count,
460 incr, AMDGPU_PTE_VALID, 0);
461
462 if (ib.length_dw != 0) {
463 amdgpu_vm_pad_ib(adev, &ib);
464 amdgpu_sync_resv(adev, &ib.sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
465 WARN_ON(ib.length_dw > ndw);
466 r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
467 if (r) {
468 amdgpu_ib_free(adev, &ib);
469 return r;
470 }
monk.liuaeb0aeac2015-05-26 15:03:45 +0800471 amdgpu_bo_fence(pd, ib.fence, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472 }
473 amdgpu_ib_free(adev, &ib);
474
475 return 0;
476}
477
478/**
479 * amdgpu_vm_frag_ptes - add fragment information to PTEs
480 *
481 * @adev: amdgpu_device pointer
482 * @ib: IB for the update
483 * @pe_start: first PTE to handle
484 * @pe_end: last PTE to handle
485 * @addr: addr those PTEs should point to
486 * @flags: hw mapping flags
487 * @gtt_flags: GTT hw mapping flags
488 *
489 * Global and local mutex must be locked!
490 */
491static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
492 struct amdgpu_ib *ib,
493 uint64_t pe_start, uint64_t pe_end,
494 uint64_t addr, uint32_t flags,
495 uint32_t gtt_flags)
496{
497 /**
498 * The MC L1 TLB supports variable sized pages, based on a fragment
499 * field in the PTE. When this field is set to a non-zero value, page
500 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
501 * flags are considered valid for all PTEs within the fragment range
502 * and corresponding mappings are assumed to be physically contiguous.
503 *
504 * The L1 TLB can store a single PTE for the whole fragment,
505 * significantly increasing the space available for translation
506 * caching. This leads to large improvements in throughput when the
507 * TLB is under pressure.
508 *
509 * The L2 TLB distributes small and large fragments into two
510 * asymmetric partitions. The large fragment cache is significantly
511 * larger. Thus, we try to use large fragments wherever possible.
512 * Userspace can support this by aligning virtual base address and
513 * allocation size to the fragment size.
514 */
515
516 /* SI and newer are optimized for 64KB */
517 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
518 uint64_t frag_align = 0x80;
519
520 uint64_t frag_start = ALIGN(pe_start, frag_align);
521 uint64_t frag_end = pe_end & ~(frag_align - 1);
522
523 unsigned count;
524
525 /* system pages are non continuously */
526 if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
527 (frag_start >= frag_end)) {
528
529 count = (pe_end - pe_start) / 8;
530 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
531 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
532 return;
533 }
534
535 /* handle the 4K area at the beginning */
536 if (pe_start != frag_start) {
537 count = (frag_start - pe_start) / 8;
538 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
539 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
540 addr += AMDGPU_GPU_PAGE_SIZE * count;
541 }
542
543 /* handle the area in the middle */
544 count = (frag_end - frag_start) / 8;
545 amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
546 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
547 gtt_flags);
548
549 /* handle the 4K area at the end */
550 if (frag_end != pe_end) {
551 addr += AMDGPU_GPU_PAGE_SIZE * count;
552 count = (pe_end - frag_end) / 8;
553 amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
554 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
555 }
556}
557
558/**
559 * amdgpu_vm_update_ptes - make sure that page tables are valid
560 *
561 * @adev: amdgpu_device pointer
562 * @vm: requested vm
563 * @start: start of GPU address range
564 * @end: end of GPU address range
565 * @dst: destination address to map to
566 * @flags: mapping flags
567 *
568 * Update the page tables in the range @start - @end (cayman+).
569 *
570 * Global and local mutex must be locked!
571 */
572static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
573 struct amdgpu_vm *vm,
574 struct amdgpu_ib *ib,
575 uint64_t start, uint64_t end,
576 uint64_t dst, uint32_t flags,
577 uint32_t gtt_flags)
578{
579 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
580 uint64_t last_pte = ~0, last_dst = ~0;
581 unsigned count = 0;
582 uint64_t addr;
583
584 /* walk over the address space and update the page tables */
585 for (addr = start; addr < end; ) {
586 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
587 struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
588 unsigned nptes;
589 uint64_t pte;
590 int r;
591
592 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv,
593 AMDGPU_FENCE_OWNER_VM);
594 r = reservation_object_reserve_shared(pt->tbo.resv);
595 if (r)
596 return r;
597
598 if ((addr & ~mask) == (end & ~mask))
599 nptes = end - addr;
600 else
601 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
602
603 pte = amdgpu_bo_gpu_offset(pt);
604 pte += (addr & mask) * 8;
605
606 if ((last_pte + 8 * count) != pte) {
607
608 if (count) {
609 amdgpu_vm_frag_ptes(adev, ib, last_pte,
610 last_pte + 8 * count,
611 last_dst, flags,
612 gtt_flags);
613 }
614
615 count = nptes;
616 last_pte = pte;
617 last_dst = dst;
618 } else {
619 count += nptes;
620 }
621
622 addr += nptes;
623 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
624 }
625
626 if (count) {
627 amdgpu_vm_frag_ptes(adev, ib, last_pte,
628 last_pte + 8 * count,
629 last_dst, flags, gtt_flags);
630 }
631
632 return 0;
633}
634
635/**
636 * amdgpu_vm_fence_pts - fence page tables after an update
637 *
638 * @vm: requested vm
639 * @start: start of GPU address range
640 * @end: end of GPU address range
641 * @fence: fence to use
642 *
643 * Fence the page tables in the range @start - @end (cayman+).
644 *
645 * Global and local mutex must be locked!
646 */
647static void amdgpu_vm_fence_pts(struct amdgpu_vm *vm,
648 uint64_t start, uint64_t end,
649 struct amdgpu_fence *fence)
650{
651 unsigned i;
652
653 start >>= amdgpu_vm_block_size;
654 end >>= amdgpu_vm_block_size;
655
656 for (i = start; i <= end; ++i)
657 amdgpu_bo_fence(vm->page_tables[i].bo, fence, true);
658}
659
660/**
661 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
662 *
663 * @adev: amdgpu_device pointer
664 * @vm: requested vm
665 * @mapping: mapped range and flags to use for the update
666 * @addr: addr to set the area to
667 * @gtt_flags: flags as they are used for GTT
668 * @fence: optional resulting fence
669 *
670 * Fill in the page table entries for @mapping.
671 * Returns 0 for success, -EINVAL for failure.
672 *
673 * Object have to be reserved and mutex must be locked!
674 */
675static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
676 struct amdgpu_vm *vm,
677 struct amdgpu_bo_va_mapping *mapping,
678 uint64_t addr, uint32_t gtt_flags,
679 struct amdgpu_fence **fence)
680{
681 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
682 unsigned nptes, ncmds, ndw;
683 uint32_t flags = gtt_flags;
684 struct amdgpu_ib ib;
685 int r;
686
687 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
688 * but in case of something, we filter the flags in first place
689 */
690 if (!(mapping->flags & AMDGPU_PTE_READABLE))
691 flags &= ~AMDGPU_PTE_READABLE;
692 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
693 flags &= ~AMDGPU_PTE_WRITEABLE;
694
695 trace_amdgpu_vm_bo_update(mapping);
696
697 nptes = mapping->it.last - mapping->it.start + 1;
698
699 /*
700 * reserve space for one command every (1 << BLOCK_SIZE)
701 * entries or 2k dwords (whatever is smaller)
702 */
703 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
704
705 /* padding, etc. */
706 ndw = 64;
707
708 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
709 /* only copy commands needed */
710 ndw += ncmds * 7;
711
712 } else if (flags & AMDGPU_PTE_SYSTEM) {
713 /* header for write data commands */
714 ndw += ncmds * 4;
715
716 /* body of write data command */
717 ndw += nptes * 2;
718
719 } else {
720 /* set page commands needed */
721 ndw += ncmds * 10;
722
723 /* two extra commands for begin/end of fragment */
724 ndw += 2 * 10;
725 }
726
727 /* update too big for an IB */
728 if (ndw > 0xfffff)
729 return -ENOMEM;
730
731 r = amdgpu_ib_get(ring, NULL, ndw * 4, &ib);
732 if (r)
733 return r;
734 ib.length_dw = 0;
735
736 if (!(flags & AMDGPU_PTE_VALID)) {
737 unsigned i;
738
739 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
740 struct amdgpu_fence *f = vm->ids[i].last_id_use;
Christian König91e1a522015-07-06 22:06:40 +0200741 r = amdgpu_sync_fence(adev, &ib.sync, &f->base);
742 if (r)
743 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744 }
745 }
746
747 r = amdgpu_vm_update_ptes(adev, vm, &ib, mapping->it.start,
748 mapping->it.last + 1, addr + mapping->offset,
749 flags, gtt_flags);
750
751 if (r) {
752 amdgpu_ib_free(adev, &ib);
753 return r;
754 }
755
756 amdgpu_vm_pad_ib(adev, &ib);
757 WARN_ON(ib.length_dw > ndw);
758
759 r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
760 if (r) {
761 amdgpu_ib_free(adev, &ib);
762 return r;
763 }
764 amdgpu_vm_fence_pts(vm, mapping->it.start,
765 mapping->it.last + 1, ib.fence);
766 if (fence) {
767 amdgpu_fence_unref(fence);
768 *fence = amdgpu_fence_ref(ib.fence);
769 }
770 amdgpu_ib_free(adev, &ib);
771
772 return 0;
773}
774
775/**
776 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
777 *
778 * @adev: amdgpu_device pointer
779 * @bo_va: requested BO and VM object
780 * @mem: ttm mem
781 *
782 * Fill in the page table entries for @bo_va.
783 * Returns 0 for success, -EINVAL for failure.
784 *
785 * Object have to be reserved and mutex must be locked!
786 */
787int amdgpu_vm_bo_update(struct amdgpu_device *adev,
788 struct amdgpu_bo_va *bo_va,
789 struct ttm_mem_reg *mem)
790{
791 struct amdgpu_vm *vm = bo_va->vm;
792 struct amdgpu_bo_va_mapping *mapping;
793 uint32_t flags;
794 uint64_t addr;
795 int r;
796
797 if (mem) {
798 addr = mem->start << PAGE_SHIFT;
799 if (mem->mem_type != TTM_PL_TT)
800 addr += adev->vm_manager.vram_base_offset;
801 } else {
802 addr = 0;
803 }
804
805 if (addr == bo_va->addr)
806 return 0;
807
808 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
809
810 list_for_each_entry(mapping, &bo_va->mappings, list) {
811 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
812 flags, &bo_va->last_pt_update);
813 if (r)
814 return r;
815 }
816
817 bo_va->addr = addr;
818 spin_lock(&vm->status_lock);
819 list_del_init(&bo_va->vm_status);
820 spin_unlock(&vm->status_lock);
821
822 return 0;
823}
824
825/**
826 * amdgpu_vm_clear_freed - clear freed BOs in the PT
827 *
828 * @adev: amdgpu_device pointer
829 * @vm: requested vm
830 *
831 * Make sure all freed BOs are cleared in the PT.
832 * Returns 0 for success.
833 *
834 * PTs have to be reserved and mutex must be locked!
835 */
836int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
837 struct amdgpu_vm *vm)
838{
839 struct amdgpu_bo_va_mapping *mapping;
840 int r;
841
842 while (!list_empty(&vm->freed)) {
843 mapping = list_first_entry(&vm->freed,
844 struct amdgpu_bo_va_mapping, list);
845 list_del(&mapping->list);
846
847 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
848 kfree(mapping);
849 if (r)
850 return r;
851
852 }
853 return 0;
854
855}
856
857/**
858 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
859 *
860 * @adev: amdgpu_device pointer
861 * @vm: requested vm
862 *
863 * Make sure all invalidated BOs are cleared in the PT.
864 * Returns 0 for success.
865 *
866 * PTs have to be reserved and mutex must be locked!
867 */
868int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800869 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400870{
monk.liucfe2c972015-05-26 15:01:54 +0800871 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200872 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873
874 spin_lock(&vm->status_lock);
875 while (!list_empty(&vm->invalidated)) {
876 bo_va = list_first_entry(&vm->invalidated,
877 struct amdgpu_bo_va, vm_status);
878 spin_unlock(&vm->status_lock);
879
880 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
881 if (r)
882 return r;
883
884 spin_lock(&vm->status_lock);
885 }
886 spin_unlock(&vm->status_lock);
887
monk.liucfe2c972015-05-26 15:01:54 +0800888 if (bo_va)
Christian König91e1a522015-07-06 22:06:40 +0200889 r = amdgpu_sync_fence(adev, sync, &bo_va->last_pt_update->base);
890
891 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892}
893
894/**
895 * amdgpu_vm_bo_add - add a bo to a specific vm
896 *
897 * @adev: amdgpu_device pointer
898 * @vm: requested vm
899 * @bo: amdgpu buffer object
900 *
901 * Add @bo into the requested vm (cayman+).
902 * Add @bo to the list of bos associated with the vm
903 * Returns newly added bo_va or NULL for failure
904 *
905 * Object has to be reserved!
906 */
907struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
908 struct amdgpu_vm *vm,
909 struct amdgpu_bo *bo)
910{
911 struct amdgpu_bo_va *bo_va;
912
913 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
914 if (bo_va == NULL) {
915 return NULL;
916 }
917 bo_va->vm = vm;
918 bo_va->bo = bo;
919 bo_va->addr = 0;
920 bo_va->ref_count = 1;
921 INIT_LIST_HEAD(&bo_va->bo_list);
922 INIT_LIST_HEAD(&bo_va->mappings);
923 INIT_LIST_HEAD(&bo_va->vm_status);
924
925 mutex_lock(&vm->mutex);
926 list_add_tail(&bo_va->bo_list, &bo->va);
927 mutex_unlock(&vm->mutex);
928
929 return bo_va;
930}
931
932/**
933 * amdgpu_vm_bo_map - map bo inside a vm
934 *
935 * @adev: amdgpu_device pointer
936 * @bo_va: bo_va to store the address
937 * @saddr: where to map the BO
938 * @offset: requested offset in the BO
939 * @flags: attributes of pages (read/write/valid/etc.)
940 *
941 * Add a mapping of the BO at the specefied addr into the VM.
942 * Returns 0 for success, error for failure.
943 *
944 * Object has to be reserved and gets unreserved by this function!
945 */
946int amdgpu_vm_bo_map(struct amdgpu_device *adev,
947 struct amdgpu_bo_va *bo_va,
948 uint64_t saddr, uint64_t offset,
949 uint64_t size, uint32_t flags)
950{
951 struct amdgpu_bo_va_mapping *mapping;
952 struct amdgpu_vm *vm = bo_va->vm;
953 struct interval_tree_node *it;
954 unsigned last_pfn, pt_idx;
955 uint64_t eaddr;
956 int r;
957
Christian König0be52de2015-05-18 14:37:27 +0200958 /* validate the parameters */
959 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
960 size == 0 || size & AMDGPU_GPU_PAGE_MASK) {
961 amdgpu_bo_unreserve(bo_va->bo);
962 return -EINVAL;
963 }
964
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 /* make sure object fit at this offset */
966 eaddr = saddr + size;
967 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) {
968 amdgpu_bo_unreserve(bo_va->bo);
969 return -EINVAL;
970 }
971
972 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
973 if (last_pfn > adev->vm_manager.max_pfn) {
974 dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n",
975 last_pfn, adev->vm_manager.max_pfn);
976 amdgpu_bo_unreserve(bo_va->bo);
977 return -EINVAL;
978 }
979
980 mutex_lock(&vm->mutex);
981
982 saddr /= AMDGPU_GPU_PAGE_SIZE;
983 eaddr /= AMDGPU_GPU_PAGE_SIZE;
984
985 it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1);
986 if (it) {
987 struct amdgpu_bo_va_mapping *tmp;
988 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
989 /* bo and tmp overlap, invalid addr */
990 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
991 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
992 tmp->it.start, tmp->it.last + 1);
993 amdgpu_bo_unreserve(bo_va->bo);
994 r = -EINVAL;
995 goto error_unlock;
996 }
997
998 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
999 if (!mapping) {
1000 amdgpu_bo_unreserve(bo_va->bo);
1001 r = -ENOMEM;
1002 goto error_unlock;
1003 }
1004
1005 INIT_LIST_HEAD(&mapping->list);
1006 mapping->it.start = saddr;
1007 mapping->it.last = eaddr - 1;
1008 mapping->offset = offset;
1009 mapping->flags = flags;
1010
1011 list_add(&mapping->list, &bo_va->mappings);
1012 interval_tree_insert(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001013 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001014
Christian König7e9fca02015-06-05 10:06:04 +02001015 bo_va->addr = 0;
1016
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001017 /* Make sure the page tables are allocated */
1018 saddr >>= amdgpu_vm_block_size;
1019 eaddr >>= amdgpu_vm_block_size;
1020
1021 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1022
1023 if (eaddr > vm->max_pde_used)
1024 vm->max_pde_used = eaddr;
1025
1026 amdgpu_bo_unreserve(bo_va->bo);
1027
1028 /* walk over the address space and allocate the page tables */
1029 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
1030 struct amdgpu_bo *pt;
1031
1032 if (vm->page_tables[pt_idx].bo)
1033 continue;
1034
1035 /* drop mutex to allocate and clear page table */
1036 mutex_unlock(&vm->mutex);
1037
1038 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1039 AMDGPU_GPU_PAGE_SIZE, true,
1040 AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &pt);
1041 if (r)
1042 goto error_free;
1043
1044 r = amdgpu_vm_clear_bo(adev, pt);
1045 if (r) {
1046 amdgpu_bo_unref(&pt);
1047 goto error_free;
1048 }
1049
1050 /* aquire mutex again */
1051 mutex_lock(&vm->mutex);
1052 if (vm->page_tables[pt_idx].bo) {
1053 /* someone else allocated the pt in the meantime */
1054 mutex_unlock(&vm->mutex);
1055 amdgpu_bo_unref(&pt);
1056 mutex_lock(&vm->mutex);
1057 continue;
1058 }
1059
1060 vm->page_tables[pt_idx].addr = 0;
1061 vm->page_tables[pt_idx].bo = pt;
1062 }
1063
1064 mutex_unlock(&vm->mutex);
1065 return 0;
1066
1067error_free:
1068 mutex_lock(&vm->mutex);
1069 list_del(&mapping->list);
1070 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001071 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072 kfree(mapping);
1073
1074error_unlock:
1075 mutex_unlock(&vm->mutex);
1076 return r;
1077}
1078
1079/**
1080 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1081 *
1082 * @adev: amdgpu_device pointer
1083 * @bo_va: bo_va to remove the address from
1084 * @saddr: where to the BO is mapped
1085 *
1086 * Remove a mapping of the BO at the specefied addr from the VM.
1087 * Returns 0 for success, error for failure.
1088 *
1089 * Object has to be reserved and gets unreserved by this function!
1090 */
1091int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1092 struct amdgpu_bo_va *bo_va,
1093 uint64_t saddr)
1094{
1095 struct amdgpu_bo_va_mapping *mapping;
1096 struct amdgpu_vm *vm = bo_va->vm;
1097
Christian König6c7fc502015-06-05 20:56:17 +02001098 saddr /= AMDGPU_GPU_PAGE_SIZE;
1099
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 list_for_each_entry(mapping, &bo_va->mappings, list) {
1101 if (mapping->it.start == saddr)
1102 break;
1103 }
1104
1105 if (&mapping->list == &bo_va->mappings) {
1106 amdgpu_bo_unreserve(bo_va->bo);
1107 return -ENOENT;
1108 }
1109
1110 mutex_lock(&vm->mutex);
1111 list_del(&mapping->list);
1112 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001113 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001114
1115 if (bo_va->addr) {
1116 /* clear the old address */
1117 list_add(&mapping->list, &vm->freed);
1118 } else {
1119 kfree(mapping);
1120 }
1121 mutex_unlock(&vm->mutex);
1122 amdgpu_bo_unreserve(bo_va->bo);
1123
1124 return 0;
1125}
1126
1127/**
1128 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1129 *
1130 * @adev: amdgpu_device pointer
1131 * @bo_va: requested bo_va
1132 *
1133 * Remove @bo_va->bo from the requested vm (cayman+).
1134 *
1135 * Object have to be reserved!
1136 */
1137void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1138 struct amdgpu_bo_va *bo_va)
1139{
1140 struct amdgpu_bo_va_mapping *mapping, *next;
1141 struct amdgpu_vm *vm = bo_va->vm;
1142
1143 list_del(&bo_va->bo_list);
1144
1145 mutex_lock(&vm->mutex);
1146
1147 spin_lock(&vm->status_lock);
1148 list_del(&bo_va->vm_status);
1149 spin_unlock(&vm->status_lock);
1150
1151 list_for_each_entry_safe(mapping, next, &bo_va->mappings, list) {
1152 list_del(&mapping->list);
1153 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001154 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001155 if (bo_va->addr)
1156 list_add(&mapping->list, &vm->freed);
1157 else
1158 kfree(mapping);
1159 }
1160 amdgpu_fence_unref(&bo_va->last_pt_update);
1161 kfree(bo_va);
1162
1163 mutex_unlock(&vm->mutex);
1164}
1165
1166/**
1167 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1168 *
1169 * @adev: amdgpu_device pointer
1170 * @vm: requested vm
1171 * @bo: amdgpu buffer object
1172 *
1173 * Mark @bo as invalid (cayman+).
1174 */
1175void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1176 struct amdgpu_bo *bo)
1177{
1178 struct amdgpu_bo_va *bo_va;
1179
1180 list_for_each_entry(bo_va, &bo->va, bo_list) {
1181 if (bo_va->addr) {
1182 spin_lock(&bo_va->vm->status_lock);
1183 list_del(&bo_va->vm_status);
1184 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
1185 spin_unlock(&bo_va->vm->status_lock);
1186 }
1187 }
1188}
1189
1190/**
1191 * amdgpu_vm_init - initialize a vm instance
1192 *
1193 * @adev: amdgpu_device pointer
1194 * @vm: requested vm
1195 *
1196 * Init @vm fields (cayman+).
1197 */
1198int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1199{
1200 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1201 AMDGPU_VM_PTE_COUNT * 8);
1202 unsigned pd_size, pd_entries, pts_size;
1203 int i, r;
1204
1205 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1206 vm->ids[i].id = 0;
1207 vm->ids[i].flushed_updates = NULL;
1208 vm->ids[i].last_id_use = NULL;
1209 }
1210 mutex_init(&vm->mutex);
1211 vm->va = RB_ROOT;
1212 spin_lock_init(&vm->status_lock);
1213 INIT_LIST_HEAD(&vm->invalidated);
1214 INIT_LIST_HEAD(&vm->freed);
1215
1216 pd_size = amdgpu_vm_directory_size(adev);
1217 pd_entries = amdgpu_vm_num_pdes(adev);
1218
1219 /* allocate page table array */
1220 pts_size = pd_entries * sizeof(struct amdgpu_vm_pt);
1221 vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
1222 if (vm->page_tables == NULL) {
1223 DRM_ERROR("Cannot allocate memory for page table array\n");
1224 return -ENOMEM;
1225 }
1226
1227 r = amdgpu_bo_create(adev, pd_size, align, true,
1228 AMDGPU_GEM_DOMAIN_VRAM, 0,
1229 NULL, &vm->page_directory);
1230 if (r)
1231 return r;
1232
1233 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
1234 if (r) {
1235 amdgpu_bo_unref(&vm->page_directory);
1236 vm->page_directory = NULL;
1237 return r;
1238 }
1239
1240 return 0;
1241}
1242
1243/**
1244 * amdgpu_vm_fini - tear down a vm instance
1245 *
1246 * @adev: amdgpu_device pointer
1247 * @vm: requested vm
1248 *
1249 * Tear down @vm (cayman+).
1250 * Unbind the VM and remove all bos from the vm bo list
1251 */
1252void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1253{
1254 struct amdgpu_bo_va_mapping *mapping, *tmp;
1255 int i;
1256
1257 if (!RB_EMPTY_ROOT(&vm->va)) {
1258 dev_err(adev->dev, "still active bo inside vm\n");
1259 }
1260 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1261 list_del(&mapping->list);
1262 interval_tree_remove(&mapping->it, &vm->va);
1263 kfree(mapping);
1264 }
1265 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1266 list_del(&mapping->list);
1267 kfree(mapping);
1268 }
1269
1270 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
1271 amdgpu_bo_unref(&vm->page_tables[i].bo);
1272 kfree(vm->page_tables);
1273
1274 amdgpu_bo_unref(&vm->page_directory);
1275
1276 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1277 amdgpu_fence_unref(&vm->ids[i].flushed_updates);
1278 amdgpu_fence_unref(&vm->ids[i].last_id_use);
1279 }
1280
1281 mutex_destroy(&vm->mutex);
1282}