drm/amdgpu: use BOs GART instance for mapping addresses v4
[deliverable/linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_vm.c
CommitLineData
d38ceaf9
AD
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 <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/**
56467ebf 78 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
d38ceaf9
AD
79 *
80 * @vm: vm providing the BOs
3c0eea6c 81 * @validated: head of validation list
56467ebf 82 * @entry: entry to add
d38ceaf9
AD
83 *
84 * Add the page directory to the list of BOs to
56467ebf 85 * validate for command submission.
d38ceaf9 86 */
56467ebf
CK
87void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
88 struct list_head *validated,
89 struct amdgpu_bo_list_entry *entry)
d38ceaf9 90{
56467ebf 91 entry->robj = vm->page_directory;
56467ebf
CK
92 entry->priority = 0;
93 entry->tv.bo = &vm->page_directory->tbo;
94 entry->tv.shared = true;
95 list_add(&entry->tv.head, validated);
96}
d38ceaf9 97
56467ebf 98/**
ee1782c3 99 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
56467ebf
CK
100 *
101 * @vm: vm providing the BOs
3c0eea6c 102 * @duplicates: head of duplicates list
d38ceaf9 103 *
ee1782c3
CK
104 * Add the page directory to the BO duplicates list
105 * for command submission.
d38ceaf9 106 */
ee1782c3 107void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
d38ceaf9 108{
ee1782c3 109 unsigned i;
d38ceaf9
AD
110
111 /* add the vm page table to the list */
ee1782c3
CK
112 for (i = 0; i <= vm->max_pde_used; ++i) {
113 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
114
115 if (!entry->robj)
d38ceaf9
AD
116 continue;
117
ee1782c3 118 list_add(&entry->tv.head, duplicates);
d38ceaf9 119 }
eceb8a15
CK
120
121}
122
123/**
124 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
125 *
126 * @adev: amdgpu device instance
127 * @vm: vm providing the BOs
128 *
129 * Move the PT BOs to the tail of the LRU.
130 */
131void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
132 struct amdgpu_vm *vm)
133{
134 struct ttm_bo_global *glob = adev->mman.bdev.glob;
135 unsigned i;
136
137 spin_lock(&glob->lru_lock);
138 for (i = 0; i <= vm->max_pde_used; ++i) {
139 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
140
141 if (!entry->robj)
142 continue;
143
144 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
145 }
146 spin_unlock(&glob->lru_lock);
d38ceaf9
AD
147}
148
149/**
150 * amdgpu_vm_grab_id - allocate the next free VMID
151 *
d38ceaf9 152 * @vm: vm to allocate id for
7f8a5290
CK
153 * @ring: ring we want to submit job to
154 * @sync: sync object where we add dependencies
94dd0a4a 155 * @fence: fence protecting ID from reuse
d38ceaf9 156 *
7f8a5290 157 * Allocate an id for the vm, adding fences to the sync obj as necessary.
d38ceaf9 158 *
7f8a5290 159 * Global mutex must be locked!
d38ceaf9 160 */
7f8a5290 161int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
94dd0a4a 162 struct amdgpu_sync *sync, struct fence *fence)
d38ceaf9 163{
d38ceaf9
AD
164 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
165 struct amdgpu_device *adev = ring->adev;
a9a78b32
CK
166 struct amdgpu_vm_manager_id *id;
167 int r;
d38ceaf9 168
94dd0a4a
CK
169 mutex_lock(&adev->vm_manager.lock);
170
d38ceaf9 171 /* check if the id is still valid */
1c16c0a7 172 if (vm_id->id) {
1c16c0a7
CK
173 long owner;
174
a9a78b32
CK
175 id = &adev->vm_manager.ids[vm_id->id];
176 owner = atomic_long_read(&id->owner);
1c16c0a7 177 if (owner == (long)vm) {
a9a78b32 178 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
165e4e07 179 trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx);
d38ceaf9 180
a9a78b32
CK
181 fence_put(id->active);
182 id->active = fence_get(fence);
d38ceaf9 183
94dd0a4a 184 mutex_unlock(&adev->vm_manager.lock);
7f8a5290 185 return 0;
d38ceaf9 186 }
d38ceaf9
AD
187 }
188
a9a78b32
CK
189 /* we definately need to flush */
190 vm_id->pd_gpu_addr = ~0ll;
7f8a5290 191
a9a78b32
CK
192 id = list_first_entry(&adev->vm_manager.ids_lru,
193 struct amdgpu_vm_manager_id,
194 list);
195 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
196 atomic_long_set(&id->owner, (long)vm);
94dd0a4a 197
a9a78b32
CK
198 vm_id->id = id - adev->vm_manager.ids;
199 trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx);
94dd0a4a 200
a9a78b32 201 r = amdgpu_sync_fence(ring->adev, sync, id->active);
94dd0a4a 202
a9a78b32
CK
203 if (!r) {
204 fence_put(id->active);
205 id->active = fence_get(fence);
d38ceaf9
AD
206 }
207
94dd0a4a 208 mutex_unlock(&adev->vm_manager.lock);
a9a78b32 209 return r;
d38ceaf9
AD
210}
211
212/**
213 * amdgpu_vm_flush - hardware flush the vm
214 *
215 * @ring: ring to use for flush
216 * @vm: vm we want to flush
217 * @updates: last vm update that we waited for
218 *
219 * Flush the vm (cayman+).
220 *
221 * Global and local mutex must be locked!
222 */
223void amdgpu_vm_flush(struct amdgpu_ring *ring,
224 struct amdgpu_vm *vm,
3c62338c 225 struct fence *updates)
d38ceaf9
AD
226{
227 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
228 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
3c62338c 229 struct fence *flushed_updates = vm_id->flushed_updates;
b56c2285 230 bool is_later;
3c62338c 231
b56c2285
CK
232 if (!flushed_updates)
233 is_later = true;
234 else if (!updates)
235 is_later = false;
236 else
237 is_later = fence_is_later(updates, flushed_updates);
d38ceaf9 238
b56c2285 239 if (pd_addr != vm_id->pd_gpu_addr || is_later) {
d38ceaf9 240 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
b56c2285 241 if (is_later) {
3c62338c
CZ
242 vm_id->flushed_updates = fence_get(updates);
243 fence_put(flushed_updates);
244 }
d38ceaf9
AD
245 vm_id->pd_gpu_addr = pd_addr;
246 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
247 }
248}
249
d38ceaf9
AD
250/**
251 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
252 *
253 * @vm: requested vm
254 * @bo: requested buffer object
255 *
256 * Find @bo inside the requested vm (cayman+).
257 * Search inside the @bos vm list for the requested vm
258 * Returns the found bo_va or NULL if none is found
259 *
260 * Object has to be reserved!
261 */
262struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
263 struct amdgpu_bo *bo)
264{
265 struct amdgpu_bo_va *bo_va;
266
267 list_for_each_entry(bo_va, &bo->va, bo_list) {
268 if (bo_va->vm == vm) {
269 return bo_va;
270 }
271 }
272 return NULL;
273}
274
275/**
276 * amdgpu_vm_update_pages - helper to call the right asic function
277 *
278 * @adev: amdgpu_device pointer
9ab21462
CK
279 * @gtt: GART instance to use for mapping
280 * @gtt_flags: GTT hw access flags
d38ceaf9
AD
281 * @ib: indirect buffer to fill with commands
282 * @pe: addr of the page entry
283 * @addr: dst addr to write into pe
284 * @count: number of page entries to update
285 * @incr: increase next addr by incr bytes
286 * @flags: hw access flags
d38ceaf9
AD
287 *
288 * Traces the parameters and calls the right asic functions
289 * to setup the page table using the DMA.
290 */
291static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
9ab21462
CK
292 struct amdgpu_gart *gtt,
293 uint32_t gtt_flags,
d38ceaf9
AD
294 struct amdgpu_ib *ib,
295 uint64_t pe, uint64_t addr,
296 unsigned count, uint32_t incr,
9ab21462 297 uint32_t flags)
d38ceaf9
AD
298{
299 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
300
9ab21462
CK
301 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
302 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
d38ceaf9
AD
303 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
304
9ab21462
CK
305 } else if (gtt) {
306 dma_addr_t *pages_addr = gtt->pages_addr;
b07c9d2a
CK
307 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
308 count, incr, flags);
309
310 } else if (count < 3) {
311 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
312 count, incr, flags);
d38ceaf9
AD
313
314 } else {
315 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
316 count, incr, flags);
317 }
318}
319
4c7eb91c 320int amdgpu_vm_free_job(struct amdgpu_job *job)
d5fc5e82
CZ
321{
322 int i;
4c7eb91c
JZ
323 for (i = 0; i < job->num_ibs; i++)
324 amdgpu_ib_free(job->adev, &job->ibs[i]);
325 kfree(job->ibs);
d5fc5e82
CZ
326 return 0;
327}
328
d38ceaf9
AD
329/**
330 * amdgpu_vm_clear_bo - initially clear the page dir/table
331 *
332 * @adev: amdgpu_device pointer
333 * @bo: bo to clear
ef9f0a83
CZ
334 *
335 * need to reserve bo first before calling it.
d38ceaf9
AD
336 */
337static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
338 struct amdgpu_bo *bo)
339{
340 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
4af9f07c 341 struct fence *fence = NULL;
d5fc5e82 342 struct amdgpu_ib *ib;
d38ceaf9
AD
343 unsigned entries;
344 uint64_t addr;
345 int r;
346
ca952613 347 r = reservation_object_reserve_shared(bo->tbo.resv);
348 if (r)
349 return r;
350
d38ceaf9
AD
351 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
352 if (r)
ef9f0a83 353 goto error;
d38ceaf9
AD
354
355 addr = amdgpu_bo_gpu_offset(bo);
356 entries = amdgpu_bo_size(bo) / 8;
357
d5fc5e82
CZ
358 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
359 if (!ib)
ef9f0a83 360 goto error;
d38ceaf9 361
d5fc5e82 362 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
d38ceaf9
AD
363 if (r)
364 goto error_free;
365
d5fc5e82
CZ
366 ib->length_dw = 0;
367
9ab21462
CK
368 amdgpu_vm_update_pages(adev, NULL, 0, ib, addr, 0, entries, 0, 0);
369
d5fc5e82
CZ
370 amdgpu_vm_pad_ib(adev, ib);
371 WARN_ON(ib->length_dw > 64);
4af9f07c
CZ
372 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
373 &amdgpu_vm_free_job,
374 AMDGPU_FENCE_OWNER_VM,
375 &fence);
376 if (!r)
377 amdgpu_bo_fence(bo, fence, true);
281b4223 378 fence_put(fence);
cadf97b1 379 return 0;
ef9f0a83 380
d38ceaf9 381error_free:
d5fc5e82
CZ
382 amdgpu_ib_free(adev, ib);
383 kfree(ib);
d38ceaf9 384
ef9f0a83 385error:
d38ceaf9
AD
386 return r;
387}
388
389/**
b07c9d2a 390 * amdgpu_vm_map_gart - Resolve gart mapping of addr
d38ceaf9 391 *
b07c9d2a 392 * @pages_addr: optional DMA address to use for lookup
d38ceaf9
AD
393 * @addr: the unmapped addr
394 *
395 * Look up the physical address of the page that the pte resolves
b07c9d2a 396 * to and return the pointer for the page table entry.
d38ceaf9 397 */
b07c9d2a 398uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
d38ceaf9
AD
399{
400 uint64_t result;
401
b07c9d2a
CK
402 if (pages_addr) {
403 /* page table offset */
404 result = pages_addr[addr >> PAGE_SHIFT];
405
406 /* in case cpu page size != gpu page size*/
407 result |= addr & (~PAGE_MASK);
408
409 } else {
410 /* No mapping required */
411 result = addr;
412 }
d38ceaf9 413
b07c9d2a 414 result &= 0xFFFFFFFFFFFFF000ULL;
d38ceaf9
AD
415
416 return result;
417}
418
419/**
420 * amdgpu_vm_update_pdes - make sure that page directory is valid
421 *
422 * @adev: amdgpu_device pointer
423 * @vm: requested vm
424 * @start: start of GPU address range
425 * @end: end of GPU address range
426 *
427 * Allocates new page tables if necessary
428 * and updates the page directory (cayman+).
429 * Returns 0 for success, error for failure.
430 *
431 * Global and local mutex must be locked!
432 */
433int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
434 struct amdgpu_vm *vm)
435{
436 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
437 struct amdgpu_bo *pd = vm->page_directory;
438 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
439 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
440 uint64_t last_pde = ~0, last_pt = ~0;
441 unsigned count = 0, pt_idx, ndw;
d5fc5e82 442 struct amdgpu_ib *ib;
4af9f07c 443 struct fence *fence = NULL;
d5fc5e82 444
d38ceaf9
AD
445 int r;
446
447 /* padding, etc. */
448 ndw = 64;
449
450 /* assume the worst case */
451 ndw += vm->max_pde_used * 6;
452
d5fc5e82
CZ
453 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
454 if (!ib)
455 return -ENOMEM;
456
457 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
7a574557
SM
458 if (r) {
459 kfree(ib);
d38ceaf9 460 return r;
7a574557 461 }
d5fc5e82 462 ib->length_dw = 0;
d38ceaf9
AD
463
464 /* walk over the address space and update the page directory */
465 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
ee1782c3 466 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
d38ceaf9
AD
467 uint64_t pde, pt;
468
469 if (bo == NULL)
470 continue;
471
472 pt = amdgpu_bo_gpu_offset(bo);
473 if (vm->page_tables[pt_idx].addr == pt)
474 continue;
475 vm->page_tables[pt_idx].addr = pt;
476
477 pde = pd_addr + pt_idx * 8;
478 if (((last_pde + 8 * count) != pde) ||
479 ((last_pt + incr * count) != pt)) {
480
481 if (count) {
9ab21462
CK
482 amdgpu_vm_update_pages(adev, NULL, 0, ib,
483 last_pde, last_pt,
484 count, incr,
485 AMDGPU_PTE_VALID);
d38ceaf9
AD
486 }
487
488 count = 1;
489 last_pde = pde;
490 last_pt = pt;
491 } else {
492 ++count;
493 }
494 }
495
496 if (count)
9ab21462
CK
497 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
498 count, incr, AMDGPU_PTE_VALID);
d38ceaf9 499
d5fc5e82
CZ
500 if (ib->length_dw != 0) {
501 amdgpu_vm_pad_ib(adev, ib);
502 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
503 WARN_ON(ib->length_dw > ndw);
4af9f07c
CZ
504 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
505 &amdgpu_vm_free_job,
506 AMDGPU_FENCE_OWNER_VM,
507 &fence);
508 if (r)
509 goto error_free;
05906dec 510
4af9f07c 511 amdgpu_bo_fence(pd, fence, true);
05906dec
BN
512 fence_put(vm->page_directory_fence);
513 vm->page_directory_fence = fence_get(fence);
281b4223 514 fence_put(fence);
d38ceaf9 515 }
d5fc5e82 516
cadf97b1 517 if (ib->length_dw == 0) {
d5fc5e82
CZ
518 amdgpu_ib_free(adev, ib);
519 kfree(ib);
520 }
d38ceaf9
AD
521
522 return 0;
d5fc5e82
CZ
523
524error_free:
d5fc5e82
CZ
525 amdgpu_ib_free(adev, ib);
526 kfree(ib);
4af9f07c 527 return r;
d38ceaf9
AD
528}
529
530/**
531 * amdgpu_vm_frag_ptes - add fragment information to PTEs
532 *
533 * @adev: amdgpu_device pointer
9ab21462
CK
534 * @gtt: GART instance to use for mapping
535 * @gtt_flags: GTT hw mapping flags
d38ceaf9
AD
536 * @ib: IB for the update
537 * @pe_start: first PTE to handle
538 * @pe_end: last PTE to handle
539 * @addr: addr those PTEs should point to
540 * @flags: hw mapping flags
d38ceaf9
AD
541 *
542 * Global and local mutex must be locked!
543 */
544static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
9ab21462
CK
545 struct amdgpu_gart *gtt,
546 uint32_t gtt_flags,
d38ceaf9
AD
547 struct amdgpu_ib *ib,
548 uint64_t pe_start, uint64_t pe_end,
9ab21462 549 uint64_t addr, uint32_t flags)
d38ceaf9
AD
550{
551 /**
552 * The MC L1 TLB supports variable sized pages, based on a fragment
553 * field in the PTE. When this field is set to a non-zero value, page
554 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
555 * flags are considered valid for all PTEs within the fragment range
556 * and corresponding mappings are assumed to be physically contiguous.
557 *
558 * The L1 TLB can store a single PTE for the whole fragment,
559 * significantly increasing the space available for translation
560 * caching. This leads to large improvements in throughput when the
561 * TLB is under pressure.
562 *
563 * The L2 TLB distributes small and large fragments into two
564 * asymmetric partitions. The large fragment cache is significantly
565 * larger. Thus, we try to use large fragments wherever possible.
566 * Userspace can support this by aligning virtual base address and
567 * allocation size to the fragment size.
568 */
569
570 /* SI and newer are optimized for 64KB */
571 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
572 uint64_t frag_align = 0x80;
573
574 uint64_t frag_start = ALIGN(pe_start, frag_align);
575 uint64_t frag_end = pe_end & ~(frag_align - 1);
576
577 unsigned count;
578
579 /* system pages are non continuously */
9ab21462 580 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
d38ceaf9
AD
581
582 count = (pe_end - pe_start) / 8;
9ab21462
CK
583 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
584 addr, count, AMDGPU_GPU_PAGE_SIZE,
585 flags);
d38ceaf9
AD
586 return;
587 }
588
589 /* handle the 4K area at the beginning */
590 if (pe_start != frag_start) {
591 count = (frag_start - pe_start) / 8;
9ab21462
CK
592 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
593 count, AMDGPU_GPU_PAGE_SIZE, flags);
d38ceaf9
AD
594 addr += AMDGPU_GPU_PAGE_SIZE * count;
595 }
596
597 /* handle the area in the middle */
598 count = (frag_end - frag_start) / 8;
9ab21462
CK
599 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
600 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
d38ceaf9
AD
601
602 /* handle the 4K area at the end */
603 if (frag_end != pe_end) {
604 addr += AMDGPU_GPU_PAGE_SIZE * count;
605 count = (pe_end - frag_end) / 8;
9ab21462
CK
606 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
607 count, AMDGPU_GPU_PAGE_SIZE, flags);
d38ceaf9
AD
608 }
609}
610
611/**
612 * amdgpu_vm_update_ptes - make sure that page tables are valid
613 *
614 * @adev: amdgpu_device pointer
9ab21462
CK
615 * @gtt: GART instance to use for mapping
616 * @gtt_flags: GTT hw mapping flags
d38ceaf9
AD
617 * @vm: requested vm
618 * @start: start of GPU address range
619 * @end: end of GPU address range
620 * @dst: destination address to map to
621 * @flags: mapping flags
622 *
623 * Update the page tables in the range @start - @end (cayman+).
624 *
625 * Global and local mutex must be locked!
626 */
627static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
9ab21462
CK
628 struct amdgpu_gart *gtt,
629 uint32_t gtt_flags,
d38ceaf9
AD
630 struct amdgpu_vm *vm,
631 struct amdgpu_ib *ib,
632 uint64_t start, uint64_t end,
9ab21462 633 uint64_t dst, uint32_t flags)
d38ceaf9
AD
634{
635 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
636 uint64_t last_pte = ~0, last_dst = ~0;
a60c4232 637 void *owner = AMDGPU_FENCE_OWNER_VM;
d38ceaf9
AD
638 unsigned count = 0;
639 uint64_t addr;
640
a60c4232
CK
641 /* sync to everything on unmapping */
642 if (!(flags & AMDGPU_PTE_VALID))
643 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
644
d38ceaf9
AD
645 /* walk over the address space and update the page tables */
646 for (addr = start; addr < end; ) {
647 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
ee1782c3 648 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
d38ceaf9
AD
649 unsigned nptes;
650 uint64_t pte;
651 int r;
652
a60c4232 653 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner);
d38ceaf9
AD
654 r = reservation_object_reserve_shared(pt->tbo.resv);
655 if (r)
656 return r;
657
658 if ((addr & ~mask) == (end & ~mask))
659 nptes = end - addr;
660 else
661 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
662
663 pte = amdgpu_bo_gpu_offset(pt);
664 pte += (addr & mask) * 8;
665
666 if ((last_pte + 8 * count) != pte) {
667
668 if (count) {
9ab21462
CK
669 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
670 last_pte, last_pte + 8 * count,
671 last_dst, flags);
d38ceaf9
AD
672 }
673
674 count = nptes;
675 last_pte = pte;
676 last_dst = dst;
677 } else {
678 count += nptes;
679 }
680
681 addr += nptes;
682 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
683 }
684
685 if (count) {
9ab21462
CK
686 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
687 last_pte, last_pte + 8 * count,
688 last_dst, flags);
d38ceaf9
AD
689 }
690
691 return 0;
692}
693
d38ceaf9
AD
694/**
695 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
696 *
697 * @adev: amdgpu_device pointer
9ab21462 698 * @gtt: GART instance to use for mapping
d38ceaf9
AD
699 * @vm: requested vm
700 * @mapping: mapped range and flags to use for the update
701 * @addr: addr to set the area to
702 * @gtt_flags: flags as they are used for GTT
703 * @fence: optional resulting fence
704 *
705 * Fill in the page table entries for @mapping.
706 * Returns 0 for success, -EINVAL for failure.
707 *
708 * Object have to be reserved and mutex must be locked!
709 */
710static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
9ab21462
CK
711 struct amdgpu_gart *gtt,
712 uint32_t gtt_flags,
d38ceaf9
AD
713 struct amdgpu_vm *vm,
714 struct amdgpu_bo_va_mapping *mapping,
9ab21462 715 uint64_t addr, struct fence **fence)
d38ceaf9
AD
716{
717 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
718 unsigned nptes, ncmds, ndw;
719 uint32_t flags = gtt_flags;
d5fc5e82 720 struct amdgpu_ib *ib;
4af9f07c 721 struct fence *f = NULL;
d38ceaf9
AD
722 int r;
723
724 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
725 * but in case of something, we filter the flags in first place
726 */
727 if (!(mapping->flags & AMDGPU_PTE_READABLE))
728 flags &= ~AMDGPU_PTE_READABLE;
729 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
730 flags &= ~AMDGPU_PTE_WRITEABLE;
731
732 trace_amdgpu_vm_bo_update(mapping);
733
734 nptes = mapping->it.last - mapping->it.start + 1;
735
736 /*
737 * reserve space for one command every (1 << BLOCK_SIZE)
738 * entries or 2k dwords (whatever is smaller)
739 */
740 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
741
742 /* padding, etc. */
743 ndw = 64;
744
9ab21462 745 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
d38ceaf9
AD
746 /* only copy commands needed */
747 ndw += ncmds * 7;
748
9ab21462 749 } else if (gtt) {
d38ceaf9
AD
750 /* header for write data commands */
751 ndw += ncmds * 4;
752
753 /* body of write data command */
754 ndw += nptes * 2;
755
756 } else {
757 /* set page commands needed */
758 ndw += ncmds * 10;
759
760 /* two extra commands for begin/end of fragment */
761 ndw += 2 * 10;
762 }
763
d5fc5e82
CZ
764 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
765 if (!ib)
766 return -ENOMEM;
767
768 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
769 if (r) {
770 kfree(ib);
d38ceaf9 771 return r;
d5fc5e82
CZ
772 }
773
774 ib->length_dw = 0;
d38ceaf9 775
9ab21462
CK
776 r = amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib,
777 mapping->it.start, mapping->it.last + 1,
778 addr + mapping->offset, flags);
d38ceaf9
AD
779
780 if (r) {
d5fc5e82
CZ
781 amdgpu_ib_free(adev, ib);
782 kfree(ib);
d38ceaf9
AD
783 return r;
784 }
785
d5fc5e82
CZ
786 amdgpu_vm_pad_ib(adev, ib);
787 WARN_ON(ib->length_dw > ndw);
4af9f07c
CZ
788 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
789 &amdgpu_vm_free_job,
790 AMDGPU_FENCE_OWNER_VM,
791 &f);
792 if (r)
793 goto error_free;
d38ceaf9 794
bf60efd3 795 amdgpu_bo_fence(vm->page_directory, f, true);
4af9f07c
CZ
796 if (fence) {
797 fence_put(*fence);
798 *fence = fence_get(f);
799 }
281b4223 800 fence_put(f);
d38ceaf9 801 return 0;
d5fc5e82
CZ
802
803error_free:
d5fc5e82
CZ
804 amdgpu_ib_free(adev, ib);
805 kfree(ib);
4af9f07c 806 return r;
d38ceaf9
AD
807}
808
809/**
810 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
811 *
812 * @adev: amdgpu_device pointer
813 * @bo_va: requested BO and VM object
814 * @mem: ttm mem
815 *
816 * Fill in the page table entries for @bo_va.
817 * Returns 0 for success, -EINVAL for failure.
818 *
819 * Object have to be reserved and mutex must be locked!
820 */
821int amdgpu_vm_bo_update(struct amdgpu_device *adev,
822 struct amdgpu_bo_va *bo_va,
823 struct ttm_mem_reg *mem)
824{
825 struct amdgpu_vm *vm = bo_va->vm;
826 struct amdgpu_bo_va_mapping *mapping;
9ab21462 827 struct amdgpu_gart *gtt = NULL;
d38ceaf9
AD
828 uint32_t flags;
829 uint64_t addr;
830 int r;
831
832 if (mem) {
b7d698d7 833 addr = (u64)mem->start << PAGE_SHIFT;
9ab21462
CK
834 switch (mem->mem_type) {
835 case TTM_PL_TT:
836 gtt = &bo_va->bo->adev->gart;
837 break;
838
839 case TTM_PL_VRAM:
d38ceaf9 840 addr += adev->vm_manager.vram_base_offset;
9ab21462
CK
841 break;
842
843 default:
844 break;
845 }
d38ceaf9
AD
846 } else {
847 addr = 0;
848 }
849
d38ceaf9
AD
850 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
851
7fc11959
CK
852 spin_lock(&vm->status_lock);
853 if (!list_empty(&bo_va->vm_status))
854 list_splice_init(&bo_va->valids, &bo_va->invalids);
855 spin_unlock(&vm->status_lock);
856
857 list_for_each_entry(mapping, &bo_va->invalids, list) {
9ab21462
CK
858 r = amdgpu_vm_bo_update_mapping(adev, gtt, flags, vm, mapping, addr,
859 &bo_va->last_pt_update);
d38ceaf9
AD
860 if (r)
861 return r;
862 }
863
d6c10f6b
CK
864 if (trace_amdgpu_vm_bo_mapping_enabled()) {
865 list_for_each_entry(mapping, &bo_va->valids, list)
866 trace_amdgpu_vm_bo_mapping(mapping);
867
868 list_for_each_entry(mapping, &bo_va->invalids, list)
869 trace_amdgpu_vm_bo_mapping(mapping);
870 }
871
d38ceaf9 872 spin_lock(&vm->status_lock);
6d1d0ef7 873 list_splice_init(&bo_va->invalids, &bo_va->valids);
d38ceaf9 874 list_del_init(&bo_va->vm_status);
7fc11959
CK
875 if (!mem)
876 list_add(&bo_va->vm_status, &vm->cleared);
d38ceaf9
AD
877 spin_unlock(&vm->status_lock);
878
879 return 0;
880}
881
882/**
883 * amdgpu_vm_clear_freed - clear freed BOs in the PT
884 *
885 * @adev: amdgpu_device pointer
886 * @vm: requested vm
887 *
888 * Make sure all freed BOs are cleared in the PT.
889 * Returns 0 for success.
890 *
891 * PTs have to be reserved and mutex must be locked!
892 */
893int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
894 struct amdgpu_vm *vm)
895{
896 struct amdgpu_bo_va_mapping *mapping;
897 int r;
898
81d75a30 899 spin_lock(&vm->freed_lock);
d38ceaf9
AD
900 while (!list_empty(&vm->freed)) {
901 mapping = list_first_entry(&vm->freed,
902 struct amdgpu_bo_va_mapping, list);
903 list_del(&mapping->list);
81d75a30 904 spin_unlock(&vm->freed_lock);
9ab21462
CK
905 r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, vm, mapping,
906 0, NULL);
d38ceaf9
AD
907 kfree(mapping);
908 if (r)
909 return r;
910
81d75a30 911 spin_lock(&vm->freed_lock);
d38ceaf9 912 }
81d75a30 913 spin_unlock(&vm->freed_lock);
914
d38ceaf9
AD
915 return 0;
916
917}
918
919/**
920 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
921 *
922 * @adev: amdgpu_device pointer
923 * @vm: requested vm
924 *
925 * Make sure all invalidated BOs are cleared in the PT.
926 * Returns 0 for success.
927 *
928 * PTs have to be reserved and mutex must be locked!
929 */
930int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
cfe2c978 931 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
d38ceaf9 932{
cfe2c978 933 struct amdgpu_bo_va *bo_va = NULL;
91e1a520 934 int r = 0;
d38ceaf9
AD
935
936 spin_lock(&vm->status_lock);
937 while (!list_empty(&vm->invalidated)) {
938 bo_va = list_first_entry(&vm->invalidated,
939 struct amdgpu_bo_va, vm_status);
940 spin_unlock(&vm->status_lock);
69b576a1 941 mutex_lock(&bo_va->mutex);
d38ceaf9 942 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
69b576a1 943 mutex_unlock(&bo_va->mutex);
d38ceaf9
AD
944 if (r)
945 return r;
946
947 spin_lock(&vm->status_lock);
948 }
949 spin_unlock(&vm->status_lock);
950
cfe2c978 951 if (bo_va)
bb1e38a4 952 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
91e1a520
CK
953
954 return r;
d38ceaf9
AD
955}
956
957/**
958 * amdgpu_vm_bo_add - add a bo to a specific vm
959 *
960 * @adev: amdgpu_device pointer
961 * @vm: requested vm
962 * @bo: amdgpu buffer object
963 *
964 * Add @bo into the requested vm (cayman+).
965 * Add @bo to the list of bos associated with the vm
966 * Returns newly added bo_va or NULL for failure
967 *
968 * Object has to be reserved!
969 */
970struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
971 struct amdgpu_vm *vm,
972 struct amdgpu_bo *bo)
973{
974 struct amdgpu_bo_va *bo_va;
975
976 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
977 if (bo_va == NULL) {
978 return NULL;
979 }
980 bo_va->vm = vm;
981 bo_va->bo = bo;
d38ceaf9
AD
982 bo_va->ref_count = 1;
983 INIT_LIST_HEAD(&bo_va->bo_list);
7fc11959
CK
984 INIT_LIST_HEAD(&bo_va->valids);
985 INIT_LIST_HEAD(&bo_va->invalids);
d38ceaf9 986 INIT_LIST_HEAD(&bo_va->vm_status);
69b576a1 987 mutex_init(&bo_va->mutex);
d38ceaf9 988 list_add_tail(&bo_va->bo_list, &bo->va);
d38ceaf9
AD
989
990 return bo_va;
991}
992
993/**
994 * amdgpu_vm_bo_map - map bo inside a vm
995 *
996 * @adev: amdgpu_device pointer
997 * @bo_va: bo_va to store the address
998 * @saddr: where to map the BO
999 * @offset: requested offset in the BO
1000 * @flags: attributes of pages (read/write/valid/etc.)
1001 *
1002 * Add a mapping of the BO at the specefied addr into the VM.
1003 * Returns 0 for success, error for failure.
1004 *
49b02b18 1005 * Object has to be reserved and unreserved outside!
d38ceaf9
AD
1006 */
1007int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1008 struct amdgpu_bo_va *bo_va,
1009 uint64_t saddr, uint64_t offset,
1010 uint64_t size, uint32_t flags)
1011{
1012 struct amdgpu_bo_va_mapping *mapping;
1013 struct amdgpu_vm *vm = bo_va->vm;
1014 struct interval_tree_node *it;
1015 unsigned last_pfn, pt_idx;
1016 uint64_t eaddr;
1017 int r;
1018
0be52de9
CK
1019 /* validate the parameters */
1020 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
49b02b18 1021 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
0be52de9 1022 return -EINVAL;
0be52de9 1023
d38ceaf9 1024 /* make sure object fit at this offset */
005ae95e 1025 eaddr = saddr + size - 1;
49b02b18 1026 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
d38ceaf9 1027 return -EINVAL;
d38ceaf9
AD
1028
1029 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
005ae95e
FK
1030 if (last_pfn >= adev->vm_manager.max_pfn) {
1031 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
d38ceaf9 1032 last_pfn, adev->vm_manager.max_pfn);
d38ceaf9
AD
1033 return -EINVAL;
1034 }
1035
d38ceaf9
AD
1036 saddr /= AMDGPU_GPU_PAGE_SIZE;
1037 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1038
c25867df 1039 spin_lock(&vm->it_lock);
005ae95e 1040 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
c25867df 1041 spin_unlock(&vm->it_lock);
d38ceaf9
AD
1042 if (it) {
1043 struct amdgpu_bo_va_mapping *tmp;
1044 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1045 /* bo and tmp overlap, invalid addr */
1046 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1047 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1048 tmp->it.start, tmp->it.last + 1);
d38ceaf9 1049 r = -EINVAL;
f48b2659 1050 goto error;
d38ceaf9
AD
1051 }
1052
1053 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1054 if (!mapping) {
d38ceaf9 1055 r = -ENOMEM;
f48b2659 1056 goto error;
d38ceaf9
AD
1057 }
1058
1059 INIT_LIST_HEAD(&mapping->list);
1060 mapping->it.start = saddr;
005ae95e 1061 mapping->it.last = eaddr;
d38ceaf9
AD
1062 mapping->offset = offset;
1063 mapping->flags = flags;
1064
69b576a1 1065 mutex_lock(&bo_va->mutex);
7fc11959 1066 list_add(&mapping->list, &bo_va->invalids);
69b576a1 1067 mutex_unlock(&bo_va->mutex);
c25867df 1068 spin_lock(&vm->it_lock);
d38ceaf9 1069 interval_tree_insert(&mapping->it, &vm->va);
c25867df 1070 spin_unlock(&vm->it_lock);
93e3e438 1071 trace_amdgpu_vm_bo_map(bo_va, mapping);
d38ceaf9
AD
1072
1073 /* Make sure the page tables are allocated */
1074 saddr >>= amdgpu_vm_block_size;
1075 eaddr >>= amdgpu_vm_block_size;
1076
1077 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1078
1079 if (eaddr > vm->max_pde_used)
1080 vm->max_pde_used = eaddr;
1081
d38ceaf9
AD
1082 /* walk over the address space and allocate the page tables */
1083 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
bf60efd3 1084 struct reservation_object *resv = vm->page_directory->tbo.resv;
ee1782c3 1085 struct amdgpu_bo_list_entry *entry;
d38ceaf9
AD
1086 struct amdgpu_bo *pt;
1087
ee1782c3
CK
1088 entry = &vm->page_tables[pt_idx].entry;
1089 if (entry->robj)
d38ceaf9
AD
1090 continue;
1091
d38ceaf9
AD
1092 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1093 AMDGPU_GPU_PAGE_SIZE, true,
857d913d
AD
1094 AMDGPU_GEM_DOMAIN_VRAM,
1095 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
bf60efd3 1096 NULL, resv, &pt);
49b02b18 1097 if (r)
d38ceaf9 1098 goto error_free;
49b02b18 1099
82b9c55b
CK
1100 /* Keep a reference to the page table to avoid freeing
1101 * them up in the wrong order.
1102 */
1103 pt->parent = amdgpu_bo_ref(vm->page_directory);
1104
d38ceaf9
AD
1105 r = amdgpu_vm_clear_bo(adev, pt);
1106 if (r) {
1107 amdgpu_bo_unref(&pt);
1108 goto error_free;
1109 }
1110
ee1782c3 1111 entry->robj = pt;
ee1782c3
CK
1112 entry->priority = 0;
1113 entry->tv.bo = &entry->robj->tbo;
1114 entry->tv.shared = true;
d38ceaf9 1115 vm->page_tables[pt_idx].addr = 0;
d38ceaf9
AD
1116 }
1117
d38ceaf9
AD
1118 return 0;
1119
1120error_free:
d38ceaf9 1121 list_del(&mapping->list);
c25867df 1122 spin_lock(&vm->it_lock);
d38ceaf9 1123 interval_tree_remove(&mapping->it, &vm->va);
c25867df 1124 spin_unlock(&vm->it_lock);
93e3e438 1125 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
d38ceaf9
AD
1126 kfree(mapping);
1127
f48b2659 1128error:
d38ceaf9
AD
1129 return r;
1130}
1131
1132/**
1133 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1134 *
1135 * @adev: amdgpu_device pointer
1136 * @bo_va: bo_va to remove the address from
1137 * @saddr: where to the BO is mapped
1138 *
1139 * Remove a mapping of the BO at the specefied addr from the VM.
1140 * Returns 0 for success, error for failure.
1141 *
49b02b18 1142 * Object has to be reserved and unreserved outside!
d38ceaf9
AD
1143 */
1144int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1145 struct amdgpu_bo_va *bo_va,
1146 uint64_t saddr)
1147{
1148 struct amdgpu_bo_va_mapping *mapping;
1149 struct amdgpu_vm *vm = bo_va->vm;
7fc11959 1150 bool valid = true;
d38ceaf9 1151
6c7fc503 1152 saddr /= AMDGPU_GPU_PAGE_SIZE;
69b576a1 1153 mutex_lock(&bo_va->mutex);
7fc11959 1154 list_for_each_entry(mapping, &bo_va->valids, list) {
d38ceaf9
AD
1155 if (mapping->it.start == saddr)
1156 break;
1157 }
1158
7fc11959
CK
1159 if (&mapping->list == &bo_va->valids) {
1160 valid = false;
1161
1162 list_for_each_entry(mapping, &bo_va->invalids, list) {
1163 if (mapping->it.start == saddr)
1164 break;
1165 }
1166
69b576a1
CZ
1167 if (&mapping->list == &bo_va->invalids) {
1168 mutex_unlock(&bo_va->mutex);
7fc11959 1169 return -ENOENT;
69b576a1 1170 }
d38ceaf9 1171 }
69b576a1 1172 mutex_unlock(&bo_va->mutex);
d38ceaf9 1173 list_del(&mapping->list);
c25867df 1174 spin_lock(&vm->it_lock);
d38ceaf9 1175 interval_tree_remove(&mapping->it, &vm->va);
c25867df 1176 spin_unlock(&vm->it_lock);
93e3e438 1177 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
d38ceaf9 1178
81d75a30 1179 if (valid) {
1180 spin_lock(&vm->freed_lock);
d38ceaf9 1181 list_add(&mapping->list, &vm->freed);
81d75a30 1182 spin_unlock(&vm->freed_lock);
1183 } else {
d38ceaf9 1184 kfree(mapping);
81d75a30 1185 }
d38ceaf9
AD
1186
1187 return 0;
1188}
1189
1190/**
1191 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1192 *
1193 * @adev: amdgpu_device pointer
1194 * @bo_va: requested bo_va
1195 *
1196 * Remove @bo_va->bo from the requested vm (cayman+).
1197 *
1198 * Object have to be reserved!
1199 */
1200void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1201 struct amdgpu_bo_va *bo_va)
1202{
1203 struct amdgpu_bo_va_mapping *mapping, *next;
1204 struct amdgpu_vm *vm = bo_va->vm;
1205
1206 list_del(&bo_va->bo_list);
1207
d38ceaf9
AD
1208 spin_lock(&vm->status_lock);
1209 list_del(&bo_va->vm_status);
1210 spin_unlock(&vm->status_lock);
1211
7fc11959 1212 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
d38ceaf9 1213 list_del(&mapping->list);
c25867df 1214 spin_lock(&vm->it_lock);
d38ceaf9 1215 interval_tree_remove(&mapping->it, &vm->va);
c25867df 1216 spin_unlock(&vm->it_lock);
93e3e438 1217 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
81d75a30 1218 spin_lock(&vm->freed_lock);
7fc11959 1219 list_add(&mapping->list, &vm->freed);
81d75a30 1220 spin_unlock(&vm->freed_lock);
7fc11959
CK
1221 }
1222 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1223 list_del(&mapping->list);
c25867df 1224 spin_lock(&vm->it_lock);
7fc11959 1225 interval_tree_remove(&mapping->it, &vm->va);
c25867df 1226 spin_unlock(&vm->it_lock);
7fc11959 1227 kfree(mapping);
d38ceaf9 1228 }
bb1e38a4 1229 fence_put(bo_va->last_pt_update);
69b576a1 1230 mutex_destroy(&bo_va->mutex);
d38ceaf9 1231 kfree(bo_va);
d38ceaf9
AD
1232}
1233
1234/**
1235 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1236 *
1237 * @adev: amdgpu_device pointer
1238 * @vm: requested vm
1239 * @bo: amdgpu buffer object
1240 *
1241 * Mark @bo as invalid (cayman+).
1242 */
1243void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1244 struct amdgpu_bo *bo)
1245{
1246 struct amdgpu_bo_va *bo_va;
1247
1248 list_for_each_entry(bo_va, &bo->va, bo_list) {
7fc11959
CK
1249 spin_lock(&bo_va->vm->status_lock);
1250 if (list_empty(&bo_va->vm_status))
d38ceaf9 1251 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
7fc11959 1252 spin_unlock(&bo_va->vm->status_lock);
d38ceaf9
AD
1253 }
1254}
1255
1256/**
1257 * amdgpu_vm_init - initialize a vm instance
1258 *
1259 * @adev: amdgpu_device pointer
1260 * @vm: requested vm
1261 *
1262 * Init @vm fields (cayman+).
1263 */
1264int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1265{
1266 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1267 AMDGPU_VM_PTE_COUNT * 8);
9571e1d8 1268 unsigned pd_size, pd_entries;
d38ceaf9
AD
1269 int i, r;
1270
1271 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1272 vm->ids[i].id = 0;
1273 vm->ids[i].flushed_updates = NULL;
d38ceaf9 1274 }
d38ceaf9
AD
1275 vm->va = RB_ROOT;
1276 spin_lock_init(&vm->status_lock);
1277 INIT_LIST_HEAD(&vm->invalidated);
7fc11959 1278 INIT_LIST_HEAD(&vm->cleared);
d38ceaf9 1279 INIT_LIST_HEAD(&vm->freed);
c25867df 1280 spin_lock_init(&vm->it_lock);
81d75a30 1281 spin_lock_init(&vm->freed_lock);
d38ceaf9
AD
1282 pd_size = amdgpu_vm_directory_size(adev);
1283 pd_entries = amdgpu_vm_num_pdes(adev);
1284
1285 /* allocate page table array */
9571e1d8 1286 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
d38ceaf9
AD
1287 if (vm->page_tables == NULL) {
1288 DRM_ERROR("Cannot allocate memory for page table array\n");
1289 return -ENOMEM;
1290 }
1291
05906dec
BN
1292 vm->page_directory_fence = NULL;
1293
d38ceaf9 1294 r = amdgpu_bo_create(adev, pd_size, align, true,
857d913d
AD
1295 AMDGPU_GEM_DOMAIN_VRAM,
1296 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
72d7668b 1297 NULL, NULL, &vm->page_directory);
d38ceaf9
AD
1298 if (r)
1299 return r;
ef9f0a83
CZ
1300 r = amdgpu_bo_reserve(vm->page_directory, false);
1301 if (r) {
1302 amdgpu_bo_unref(&vm->page_directory);
1303 vm->page_directory = NULL;
1304 return r;
1305 }
d38ceaf9 1306 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
ef9f0a83 1307 amdgpu_bo_unreserve(vm->page_directory);
d38ceaf9
AD
1308 if (r) {
1309 amdgpu_bo_unref(&vm->page_directory);
1310 vm->page_directory = NULL;
1311 return r;
1312 }
1313
1314 return 0;
1315}
1316
1317/**
1318 * amdgpu_vm_fini - tear down a vm instance
1319 *
1320 * @adev: amdgpu_device pointer
1321 * @vm: requested vm
1322 *
1323 * Tear down @vm (cayman+).
1324 * Unbind the VM and remove all bos from the vm bo list
1325 */
1326void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1327{
1328 struct amdgpu_bo_va_mapping *mapping, *tmp;
1329 int i;
1330
1331 if (!RB_EMPTY_ROOT(&vm->va)) {
1332 dev_err(adev->dev, "still active bo inside vm\n");
1333 }
1334 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1335 list_del(&mapping->list);
1336 interval_tree_remove(&mapping->it, &vm->va);
1337 kfree(mapping);
1338 }
1339 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1340 list_del(&mapping->list);
1341 kfree(mapping);
1342 }
1343
1344 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
ee1782c3 1345 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
9571e1d8 1346 drm_free_large(vm->page_tables);
d38ceaf9
AD
1347
1348 amdgpu_bo_unref(&vm->page_directory);
05906dec 1349 fence_put(vm->page_directory_fence);
d38ceaf9 1350 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1c16c0a7
CK
1351 unsigned id = vm->ids[i].id;
1352
1353 atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner,
1354 (long)vm, 0);
3c62338c 1355 fence_put(vm->ids[i].flushed_updates);
d38ceaf9
AD
1356 }
1357
d38ceaf9 1358}
ea89f8c9 1359
a9a78b32
CK
1360/**
1361 * amdgpu_vm_manager_init - init the VM manager
1362 *
1363 * @adev: amdgpu_device pointer
1364 *
1365 * Initialize the VM manager structures
1366 */
1367void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1368{
1369 unsigned i;
1370
1371 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1372
1373 /* skip over VMID 0, since it is the system VM */
1374 for (i = 1; i < adev->vm_manager.num_ids; ++i)
1375 list_add_tail(&adev->vm_manager.ids[i].list,
1376 &adev->vm_manager.ids_lru);
1377}
1378
ea89f8c9
CK
1379/**
1380 * amdgpu_vm_manager_fini - cleanup VM manager
1381 *
1382 * @adev: amdgpu_device pointer
1383 *
1384 * Cleanup the VM manager and free resources.
1385 */
1386void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1387{
1388 unsigned i;
1389
1390 for (i = 0; i < AMDGPU_NUM_VM; ++i)
1c16c0a7 1391 fence_put(adev->vm_manager.ids[i].active);
ea89f8c9 1392}
This page took 0.140517 seconds and 5 git commands to generate.