blob: fb003089f73cfe4efeceedc4c90279787645f20d [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 *
Christian König8843dbb2016-01-26 12:17:11 +010058 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059 */
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 *
Christian König8843dbb2016-01-26 12:17:11 +010070 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040071 */
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/**
Christian König56467eb2015-12-11 15:16:32 +010078 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079 *
80 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +010081 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +010082 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 *
84 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +010085 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 */
Christian König56467eb2015-12-11 15:16:32 +010087void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
88 struct list_head *validated,
89 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090{
Christian König56467eb2015-12-11 15:16:32 +010091 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +010092 entry->priority = 0;
93 entry->tv.bo = &vm->page_directory->tbo;
94 entry->tv.shared = true;
95 list_add(&entry->tv.head, validated);
96}
Alex Deucherd38ceaf2015-04-20 16:55:21 -040097
Christian König56467eb2015-12-11 15:16:32 +010098/**
Christian Königee1782c2015-12-11 21:01:23 +010099 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100100 *
101 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100102 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103 *
Christian Königee1782c2015-12-11 21:01:23 +0100104 * Add the page directory to the BO duplicates list
105 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 */
Christian Königee1782c2015-12-11 21:01:23 +0100107void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108{
Christian Königee1782c2015-12-11 21:01:23 +0100109 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110
111 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100112 for (i = 0; i <= vm->max_pde_used; ++i) {
113 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114
Christian Königee1782c2015-12-11 21:01:23 +0100115 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 continue;
117
Christian Königee1782c2015-12-11 21:01:23 +0100118 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 }
Christian Königeceb8a12016-01-11 15:35:21 +0100120
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);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147}
148
149/**
150 * amdgpu_vm_grab_id - allocate the next free VMID
151 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200153 * @ring: ring we want to submit job to
154 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100155 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 *
Christian König7f8a5292015-07-20 16:09:40 +0200157 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158 */
Christian König7f8a5292015-07-20 16:09:40 +0200159int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König94dd0a42016-01-18 17:01:42 +0100160 struct amdgpu_sync *sync, struct fence *fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
163 struct amdgpu_device *adev = ring->adev;
Christian Königa9a78b32016-01-21 10:19:11 +0100164 struct amdgpu_vm_manager_id *id;
165 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166
Christian König94dd0a42016-01-18 17:01:42 +0100167 mutex_lock(&adev->vm_manager.lock);
168
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 /* check if the id is still valid */
Christian König1c16c0a2015-11-14 21:31:40 +0100170 if (vm_id->id) {
Christian König1c16c0a2015-11-14 21:31:40 +0100171 long owner;
172
Christian Königa9a78b32016-01-21 10:19:11 +0100173 id = &adev->vm_manager.ids[vm_id->id];
174 owner = atomic_long_read(&id->owner);
Christian König1c16c0a2015-11-14 21:31:40 +0100175 if (owner == (long)vm) {
Christian Königa9a78b32016-01-21 10:19:11 +0100176 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König165e4e02016-01-07 18:15:22 +0100177 trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx);
Christian Königa9a78b32016-01-21 10:19:11 +0100178
179 fence_put(id->active);
180 id->active = fence_get(fence);
181
Christian König94dd0a42016-01-18 17:01:42 +0100182 mutex_unlock(&adev->vm_manager.lock);
Christian König1c16c0a2015-11-14 21:31:40 +0100183 return 0;
184 }
Christian König39ff8442015-09-28 12:01:20 +0200185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186
187 /* we definately need to flush */
188 vm_id->pd_gpu_addr = ~0ll;
189
Christian Königa9a78b32016-01-21 10:19:11 +0100190 id = list_first_entry(&adev->vm_manager.ids_lru,
191 struct amdgpu_vm_manager_id,
192 list);
193 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
194 atomic_long_set(&id->owner, (long)vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195
Christian Königa9a78b32016-01-21 10:19:11 +0100196 vm_id->id = id - adev->vm_manager.ids;
197 trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198
Christian Königa9a78b32016-01-21 10:19:11 +0100199 r = amdgpu_sync_fence(ring->adev, sync, id->active);
200
201 if (!r) {
202 fence_put(id->active);
203 id->active = fence_get(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204 }
205
Christian König94dd0a42016-01-18 17:01:42 +0100206 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100207 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208}
209
210/**
211 * amdgpu_vm_flush - hardware flush the vm
212 *
213 * @ring: ring to use for flush
214 * @vm: vm we want to flush
215 * @updates: last vm update that we waited for
216 *
Christian König8843dbb2016-01-26 12:17:11 +0100217 * Flush the vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400218 */
219void amdgpu_vm_flush(struct amdgpu_ring *ring,
220 struct amdgpu_vm *vm,
Chunming Zhou3c623382015-08-20 18:33:59 +0800221 struct fence *updates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400222{
223 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
224 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
Chunming Zhou3c623382015-08-20 18:33:59 +0800225 struct fence *flushed_updates = vm_id->flushed_updates;
Christian Königb56c2282015-10-29 17:01:19 +0100226 bool is_later;
Chunming Zhou3c623382015-08-20 18:33:59 +0800227
Christian Königb56c2282015-10-29 17:01:19 +0100228 if (!flushed_updates)
229 is_later = true;
230 else if (!updates)
231 is_later = false;
232 else
233 is_later = fence_is_later(updates, flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234
Christian Königb56c2282015-10-29 17:01:19 +0100235 if (pd_addr != vm_id->pd_gpu_addr || is_later) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
Christian Königb56c2282015-10-29 17:01:19 +0100237 if (is_later) {
Chunming Zhou3c623382015-08-20 18:33:59 +0800238 vm_id->flushed_updates = fence_get(updates);
239 fence_put(flushed_updates);
240 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241 vm_id->pd_gpu_addr = pd_addr;
242 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
243 }
244}
245
246/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400247 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
248 *
249 * @vm: requested vm
250 * @bo: requested buffer object
251 *
Christian König8843dbb2016-01-26 12:17:11 +0100252 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400253 * Search inside the @bos vm list for the requested vm
254 * Returns the found bo_va or NULL if none is found
255 *
256 * Object has to be reserved!
257 */
258struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
259 struct amdgpu_bo *bo)
260{
261 struct amdgpu_bo_va *bo_va;
262
263 list_for_each_entry(bo_va, &bo->va, bo_list) {
264 if (bo_va->vm == vm) {
265 return bo_va;
266 }
267 }
268 return NULL;
269}
270
271/**
272 * amdgpu_vm_update_pages - helper to call the right asic function
273 *
274 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100275 * @gtt: GART instance to use for mapping
276 * @gtt_flags: GTT hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400277 * @ib: indirect buffer to fill with commands
278 * @pe: addr of the page entry
279 * @addr: dst addr to write into pe
280 * @count: number of page entries to update
281 * @incr: increase next addr by incr bytes
282 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283 *
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,
Christian König9ab21462015-11-30 14:19:26 +0100288 struct amdgpu_gart *gtt,
289 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290 struct amdgpu_ib *ib,
291 uint64_t pe, uint64_t addr,
292 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100293 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294{
295 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
296
Christian König9ab21462015-11-30 14:19:26 +0100297 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
298 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
300
Christian König9ab21462015-11-30 14:19:26 +0100301 } else if (gtt) {
302 dma_addr_t *pages_addr = gtt->pages_addr;
Christian Königb07c9d22015-11-30 13:26:07 +0100303 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
304 count, incr, flags);
305
306 } else if (count < 3) {
307 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
308 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400309
310 } else {
311 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
312 count, incr, flags);
313 }
314}
315
316/**
317 * amdgpu_vm_clear_bo - initially clear the page dir/table
318 *
319 * @adev: amdgpu_device pointer
320 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800321 *
322 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400323 */
324static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
325 struct amdgpu_bo *bo)
326{
327 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800328 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100329 struct amdgpu_job *job;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400330 unsigned entries;
331 uint64_t addr;
332 int r;
333
monk.liuca952612015-05-25 14:44:05 +0800334 r = reservation_object_reserve_shared(bo->tbo.resv);
335 if (r)
336 return r;
337
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400338 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
339 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800340 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400341
342 addr = amdgpu_bo_gpu_offset(bo);
343 entries = amdgpu_bo_size(bo) / 8;
344
Christian Königd71518b2016-02-01 12:20:25 +0100345 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
346 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800347 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348
Christian Königd71518b2016-02-01 12:20:25 +0100349 amdgpu_vm_update_pages(adev, NULL, 0, &job->ibs[0], addr, 0, entries,
350 0, 0);
351 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
352
353 WARN_ON(job->ibs[0].length_dw > 64);
354 r = amdgpu_job_submit(job, ring, AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400355 if (r)
356 goto error_free;
357
Christian Königd71518b2016-02-01 12:20:25 +0100358 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800359 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800360 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800361
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400362error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100363 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400364
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800365error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400366 return r;
367}
368
369/**
Christian Königb07c9d22015-11-30 13:26:07 +0100370 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400371 *
Christian Königb07c9d22015-11-30 13:26:07 +0100372 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400373 * @addr: the unmapped addr
374 *
375 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100376 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377 */
Christian Königb07c9d22015-11-30 13:26:07 +0100378uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400379{
380 uint64_t result;
381
Christian Königb07c9d22015-11-30 13:26:07 +0100382 if (pages_addr) {
383 /* page table offset */
384 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400385
Christian Königb07c9d22015-11-30 13:26:07 +0100386 /* in case cpu page size != gpu page size*/
387 result |= addr & (~PAGE_MASK);
388
389 } else {
390 /* No mapping required */
391 result = addr;
392 }
393
394 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400395
396 return result;
397}
398
399/**
400 * amdgpu_vm_update_pdes - make sure that page directory is valid
401 *
402 * @adev: amdgpu_device pointer
403 * @vm: requested vm
404 * @start: start of GPU address range
405 * @end: end of GPU address range
406 *
407 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100408 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400410 */
411int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
412 struct amdgpu_vm *vm)
413{
414 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
415 struct amdgpu_bo *pd = vm->page_directory;
416 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
417 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
418 uint64_t last_pde = ~0, last_pt = ~0;
419 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100420 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800421 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800422 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800423
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424 int r;
425
426 /* padding, etc. */
427 ndw = 64;
428
429 /* assume the worst case */
430 ndw += vm->max_pde_used * 6;
431
Christian Königd71518b2016-02-01 12:20:25 +0100432 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
433 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100435
436 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437
438 /* walk over the address space and update the page directory */
439 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100440 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400441 uint64_t pde, pt;
442
443 if (bo == NULL)
444 continue;
445
446 pt = amdgpu_bo_gpu_offset(bo);
447 if (vm->page_tables[pt_idx].addr == pt)
448 continue;
449 vm->page_tables[pt_idx].addr = pt;
450
451 pde = pd_addr + pt_idx * 8;
452 if (((last_pde + 8 * count) != pde) ||
453 ((last_pt + incr * count) != pt)) {
454
455 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100456 amdgpu_vm_update_pages(adev, NULL, 0, ib,
457 last_pde, last_pt,
458 count, incr,
459 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 }
461
462 count = 1;
463 last_pde = pde;
464 last_pt = pt;
465 } else {
466 ++count;
467 }
468 }
469
470 if (count)
Christian König9ab21462015-11-30 14:19:26 +0100471 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
472 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800474 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100475 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800476 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
477 WARN_ON(ib->length_dw > ndw);
Christian Königd71518b2016-02-01 12:20:25 +0100478 r = amdgpu_job_submit(job, ring, AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800479 if (r)
480 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200481
Chunming Zhou4af9f072015-08-03 12:57:31 +0800482 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200483 fence_put(vm->page_directory_fence);
484 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800485 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800486
Christian Königd71518b2016-02-01 12:20:25 +0100487 } else {
488 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800489 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490
491 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800492
493error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100494 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800495 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496}
497
498/**
499 * amdgpu_vm_frag_ptes - add fragment information to PTEs
500 *
501 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100502 * @gtt: GART instance to use for mapping
503 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 * @ib: IB for the update
505 * @pe_start: first PTE to handle
506 * @pe_end: last PTE to handle
507 * @addr: addr those PTEs should point to
508 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509 */
510static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100511 struct amdgpu_gart *gtt,
512 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 struct amdgpu_ib *ib,
514 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100515 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400516{
517 /**
518 * The MC L1 TLB supports variable sized pages, based on a fragment
519 * field in the PTE. When this field is set to a non-zero value, page
520 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
521 * flags are considered valid for all PTEs within the fragment range
522 * and corresponding mappings are assumed to be physically contiguous.
523 *
524 * The L1 TLB can store a single PTE for the whole fragment,
525 * significantly increasing the space available for translation
526 * caching. This leads to large improvements in throughput when the
527 * TLB is under pressure.
528 *
529 * The L2 TLB distributes small and large fragments into two
530 * asymmetric partitions. The large fragment cache is significantly
531 * larger. Thus, we try to use large fragments wherever possible.
532 * Userspace can support this by aligning virtual base address and
533 * allocation size to the fragment size.
534 */
535
536 /* SI and newer are optimized for 64KB */
537 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
538 uint64_t frag_align = 0x80;
539
540 uint64_t frag_start = ALIGN(pe_start, frag_align);
541 uint64_t frag_end = pe_end & ~(frag_align - 1);
542
543 unsigned count;
544
Christian König31f6c1f2016-01-26 12:37:49 +0100545 /* Abort early if there isn't anything to do */
546 if (pe_start == pe_end)
547 return;
548
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400549 /* system pages are non continuously */
Christian König9ab21462015-11-30 14:19:26 +0100550 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551
552 count = (pe_end - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100553 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
554 addr, count, AMDGPU_GPU_PAGE_SIZE,
555 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 return;
557 }
558
559 /* handle the 4K area at the beginning */
560 if (pe_start != frag_start) {
561 count = (frag_start - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100562 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
563 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 addr += AMDGPU_GPU_PAGE_SIZE * count;
565 }
566
567 /* handle the area in the middle */
568 count = (frag_end - frag_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100569 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
570 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571
572 /* handle the 4K area at the end */
573 if (frag_end != pe_end) {
574 addr += AMDGPU_GPU_PAGE_SIZE * count;
575 count = (pe_end - frag_end) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100576 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
577 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578 }
579}
580
581/**
582 * amdgpu_vm_update_ptes - make sure that page tables are valid
583 *
584 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100585 * @gtt: GART instance to use for mapping
586 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 * @vm: requested vm
588 * @start: start of GPU address range
589 * @end: end of GPU address range
590 * @dst: destination address to map to
591 * @flags: mapping flags
592 *
Christian König8843dbb2016-01-26 12:17:11 +0100593 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 */
Christian Königa1e08d32016-01-26 11:40:46 +0100595static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
596 struct amdgpu_gart *gtt,
597 uint32_t gtt_flags,
598 struct amdgpu_vm *vm,
599 struct amdgpu_ib *ib,
600 uint64_t start, uint64_t end,
601 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400602{
Christian König31f6c1f2016-01-26 12:37:49 +0100603 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
604
605 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400606 uint64_t addr;
607
608 /* walk over the address space and update the page tables */
609 for (addr = start; addr < end; ) {
610 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100611 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400612 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100613 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614
615 if ((addr & ~mask) == (end & ~mask))
616 nptes = end - addr;
617 else
618 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
619
Christian König31f6c1f2016-01-26 12:37:49 +0100620 pe_start = amdgpu_bo_gpu_offset(pt);
621 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622
Christian König31f6c1f2016-01-26 12:37:49 +0100623 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624
Christian König31f6c1f2016-01-26 12:37:49 +0100625 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
626 last_pe_start, last_pe_end,
627 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628
Christian König31f6c1f2016-01-26 12:37:49 +0100629 last_pe_start = pe_start;
630 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400631 last_dst = dst;
632 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100633 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634 }
635
636 addr += nptes;
637 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
638 }
639
Christian König31f6c1f2016-01-26 12:37:49 +0100640 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
641 last_pe_start, last_pe_end,
642 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643}
644
645/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
647 *
648 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100649 * @gtt: GART instance to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 * @gtt_flags: flags as they are used for GTT
Christian Königa14faa62016-01-25 14:27:31 +0100651 * @vm: requested vm
652 * @start: start of mapped range
653 * @last: last mapped entry
654 * @flags: flags for the entries
655 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 * @fence: optional resulting fence
657 *
Christian Königa14faa62016-01-25 14:27:31 +0100658 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 */
661static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100662 struct amdgpu_gart *gtt,
663 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100665 uint64_t start, uint64_t last,
666 uint32_t flags, uint64_t addr,
667 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668{
669 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100670 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100672 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800673 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800674 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675 int r;
676
Christian Königa1e08d32016-01-26 11:40:46 +0100677 /* sync to everything on unmapping */
678 if (!(flags & AMDGPU_PTE_VALID))
679 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
680
Christian Königa14faa62016-01-25 14:27:31 +0100681 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682
683 /*
684 * reserve space for one command every (1 << BLOCK_SIZE)
685 * entries or 2k dwords (whatever is smaller)
686 */
687 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
688
689 /* padding, etc. */
690 ndw = 64;
691
Christian König9ab21462015-11-30 14:19:26 +0100692 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 /* only copy commands needed */
694 ndw += ncmds * 7;
695
Christian König9ab21462015-11-30 14:19:26 +0100696 } else if (gtt) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 /* header for write data commands */
698 ndw += ncmds * 4;
699
700 /* body of write data command */
701 ndw += nptes * 2;
702
703 } else {
704 /* set page commands needed */
705 ndw += ncmds * 10;
706
707 /* two extra commands for begin/end of fragment */
708 ndw += 2 * 10;
709 }
710
Christian Königd71518b2016-02-01 12:20:25 +0100711 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
712 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100714
715 ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800716
Christian Königa1e08d32016-01-26 11:40:46 +0100717 r = amdgpu_sync_resv(adev, &ib->sync, vm->page_directory->tbo.resv,
718 owner);
719 if (r)
720 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400721
Christian Königa1e08d32016-01-26 11:40:46 +0100722 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
723 if (r)
724 goto error_free;
725
726 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
727 addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728
Christian König9e5d53092016-01-31 12:20:55 +0100729 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800730 WARN_ON(ib->length_dw > ndw);
Christian Königd71518b2016-02-01 12:20:25 +0100731 r = amdgpu_job_submit(job, ring, AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800732 if (r)
733 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734
Christian Königbf60efd2015-09-04 10:47:56 +0200735 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800736 if (fence) {
737 fence_put(*fence);
738 *fence = fence_get(f);
739 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800740 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800742
743error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100744 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800745 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746}
747
748/**
Christian Königa14faa62016-01-25 14:27:31 +0100749 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
750 *
751 * @adev: amdgpu_device pointer
752 * @gtt: GART instance to use for mapping
753 * @vm: requested vm
754 * @mapping: mapped range and flags to use for the update
755 * @addr: addr to set the area to
756 * @gtt_flags: flags as they are used for GTT
757 * @fence: optional resulting fence
758 *
759 * Split the mapping into smaller chunks so that each update fits
760 * into a SDMA IB.
761 * Returns 0 for success, -EINVAL for failure.
762 */
763static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
764 struct amdgpu_gart *gtt,
765 uint32_t gtt_flags,
766 struct amdgpu_vm *vm,
767 struct amdgpu_bo_va_mapping *mapping,
768 uint64_t addr, struct fence **fence)
769{
770 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
771
772 uint64_t start = mapping->it.start;
773 uint32_t flags = gtt_flags;
774 int r;
775
776 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
777 * but in case of something, we filter the flags in first place
778 */
779 if (!(mapping->flags & AMDGPU_PTE_READABLE))
780 flags &= ~AMDGPU_PTE_READABLE;
781 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
782 flags &= ~AMDGPU_PTE_WRITEABLE;
783
784 trace_amdgpu_vm_bo_update(mapping);
785
786 addr += mapping->offset;
787
788 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
789 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
790 start, mapping->it.last,
791 flags, addr, fence);
792
793 while (start != mapping->it.last + 1) {
794 uint64_t last;
795
796 last = min((uint64_t)mapping->it.last, start + max_size);
797 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
798 start, last, flags, addr,
799 fence);
800 if (r)
801 return r;
802
803 start = last + 1;
804 addr += max_size;
805 }
806
807 return 0;
808}
809
810/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400811 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
812 *
813 * @adev: amdgpu_device pointer
814 * @bo_va: requested BO and VM object
815 * @mem: ttm mem
816 *
817 * Fill in the page table entries for @bo_va.
818 * Returns 0 for success, -EINVAL for failure.
819 *
820 * Object have to be reserved and mutex must be locked!
821 */
822int amdgpu_vm_bo_update(struct amdgpu_device *adev,
823 struct amdgpu_bo_va *bo_va,
824 struct ttm_mem_reg *mem)
825{
826 struct amdgpu_vm *vm = bo_va->vm;
827 struct amdgpu_bo_va_mapping *mapping;
Christian König9ab21462015-11-30 14:19:26 +0100828 struct amdgpu_gart *gtt = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829 uint32_t flags;
830 uint64_t addr;
831 int r;
832
833 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200834 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100835 switch (mem->mem_type) {
836 case TTM_PL_TT:
837 gtt = &bo_va->bo->adev->gart;
838 break;
839
840 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100842 break;
843
844 default:
845 break;
846 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400847 } else {
848 addr = 0;
849 }
850
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400851 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
852
Christian König7fc11952015-07-30 11:53:42 +0200853 spin_lock(&vm->status_lock);
854 if (!list_empty(&bo_va->vm_status))
855 list_splice_init(&bo_va->valids, &bo_va->invalids);
856 spin_unlock(&vm->status_lock);
857
858 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa14faa62016-01-25 14:27:31 +0100859 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
860 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861 if (r)
862 return r;
863 }
864
Christian Königd6c10f62015-09-28 12:00:23 +0200865 if (trace_amdgpu_vm_bo_mapping_enabled()) {
866 list_for_each_entry(mapping, &bo_va->valids, list)
867 trace_amdgpu_vm_bo_mapping(mapping);
868
869 list_for_each_entry(mapping, &bo_va->invalids, list)
870 trace_amdgpu_vm_bo_mapping(mapping);
871 }
872
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800874 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200876 if (!mem)
877 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878 spin_unlock(&vm->status_lock);
879
880 return 0;
881}
882
883/**
884 * amdgpu_vm_clear_freed - clear freed BOs in the PT
885 *
886 * @adev: amdgpu_device pointer
887 * @vm: requested vm
888 *
889 * Make sure all freed BOs are cleared in the PT.
890 * Returns 0 for success.
891 *
892 * PTs have to be reserved and mutex must be locked!
893 */
894int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
895 struct amdgpu_vm *vm)
896{
897 struct amdgpu_bo_va_mapping *mapping;
898 int r;
899
jimqu81d75a32015-12-04 17:17:00 +0800900 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400901 while (!list_empty(&vm->freed)) {
902 mapping = list_first_entry(&vm->freed,
903 struct amdgpu_bo_va_mapping, list);
904 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800905 spin_unlock(&vm->freed_lock);
Christian Königa14faa62016-01-25 14:27:31 +0100906 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
907 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400908 kfree(mapping);
909 if (r)
910 return r;
911
jimqu81d75a32015-12-04 17:17:00 +0800912 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 }
jimqu81d75a32015-12-04 17:17:00 +0800914 spin_unlock(&vm->freed_lock);
915
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400916 return 0;
917
918}
919
920/**
921 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
922 *
923 * @adev: amdgpu_device pointer
924 * @vm: requested vm
925 *
926 * Make sure all invalidated BOs are cleared in the PT.
927 * Returns 0 for success.
928 *
929 * PTs have to be reserved and mutex must be locked!
930 */
931int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800932 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933{
monk.liucfe2c972015-05-26 15:01:54 +0800934 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200935 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936
937 spin_lock(&vm->status_lock);
938 while (!list_empty(&vm->invalidated)) {
939 bo_va = list_first_entry(&vm->invalidated,
940 struct amdgpu_bo_va, vm_status);
941 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800942 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800944 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400945 if (r)
946 return r;
947
948 spin_lock(&vm->status_lock);
949 }
950 spin_unlock(&vm->status_lock);
951
monk.liucfe2c972015-05-26 15:01:54 +0800952 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800953 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200954
955 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956}
957
958/**
959 * amdgpu_vm_bo_add - add a bo to a specific vm
960 *
961 * @adev: amdgpu_device pointer
962 * @vm: requested vm
963 * @bo: amdgpu buffer object
964 *
Christian König8843dbb2016-01-26 12:17:11 +0100965 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966 * Add @bo to the list of bos associated with the vm
967 * Returns newly added bo_va or NULL for failure
968 *
969 * Object has to be reserved!
970 */
971struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
972 struct amdgpu_vm *vm,
973 struct amdgpu_bo *bo)
974{
975 struct amdgpu_bo_va *bo_va;
976
977 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
978 if (bo_va == NULL) {
979 return NULL;
980 }
981 bo_va->vm = vm;
982 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400983 bo_va->ref_count = 1;
984 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +0200985 INIT_LIST_HEAD(&bo_va->valids);
986 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400987 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800988 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990
991 return bo_va;
992}
993
994/**
995 * amdgpu_vm_bo_map - map bo inside a vm
996 *
997 * @adev: amdgpu_device pointer
998 * @bo_va: bo_va to store the address
999 * @saddr: where to map the BO
1000 * @offset: requested offset in the BO
1001 * @flags: attributes of pages (read/write/valid/etc.)
1002 *
1003 * Add a mapping of the BO at the specefied addr into the VM.
1004 * Returns 0 for success, error for failure.
1005 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001006 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007 */
1008int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1009 struct amdgpu_bo_va *bo_va,
1010 uint64_t saddr, uint64_t offset,
1011 uint64_t size, uint32_t flags)
1012{
1013 struct amdgpu_bo_va_mapping *mapping;
1014 struct amdgpu_vm *vm = bo_va->vm;
1015 struct interval_tree_node *it;
1016 unsigned last_pfn, pt_idx;
1017 uint64_t eaddr;
1018 int r;
1019
Christian König0be52de2015-05-18 14:37:27 +02001020 /* validate the parameters */
1021 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001022 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001023 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001024
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001025 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001026 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001027 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001028 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029
1030 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001031 if (last_pfn >= adev->vm_manager.max_pfn) {
1032 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001034 return -EINVAL;
1035 }
1036
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001037 saddr /= AMDGPU_GPU_PAGE_SIZE;
1038 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1039
Chunming Zhouc25867d2015-11-13 13:32:01 +08001040 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001041 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001042 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001043 if (it) {
1044 struct amdgpu_bo_va_mapping *tmp;
1045 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1046 /* bo and tmp overlap, invalid addr */
1047 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1048 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1049 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001051 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001052 }
1053
1054 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1055 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001056 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001057 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001058 }
1059
1060 INIT_LIST_HEAD(&mapping->list);
1061 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001062 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001063 mapping->offset = offset;
1064 mapping->flags = flags;
1065
Chunming Zhou69b576a2015-11-18 11:17:39 +08001066 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001067 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001068 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001069 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001070 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001071 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001072 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074 /* Make sure the page tables are allocated */
1075 saddr >>= amdgpu_vm_block_size;
1076 eaddr >>= amdgpu_vm_block_size;
1077
1078 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1079
1080 if (eaddr > vm->max_pde_used)
1081 vm->max_pde_used = eaddr;
1082
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083 /* walk over the address space and allocate the page tables */
1084 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001085 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001086 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087 struct amdgpu_bo *pt;
1088
Christian Königee1782c2015-12-11 21:01:23 +01001089 entry = &vm->page_tables[pt_idx].entry;
1090 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091 continue;
1092
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001093 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1094 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001095 AMDGPU_GEM_DOMAIN_VRAM,
1096 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001097 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001098 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001100
Christian König82b9c552015-11-27 16:49:00 +01001101 /* Keep a reference to the page table to avoid freeing
1102 * them up in the wrong order.
1103 */
1104 pt->parent = amdgpu_bo_ref(vm->page_directory);
1105
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 r = amdgpu_vm_clear_bo(adev, pt);
1107 if (r) {
1108 amdgpu_bo_unref(&pt);
1109 goto error_free;
1110 }
1111
Christian Königee1782c2015-12-11 21:01:23 +01001112 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001113 entry->priority = 0;
1114 entry->tv.bo = &entry->robj->tbo;
1115 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117 }
1118
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001119 return 0;
1120
1121error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001122 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001123 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001125 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001126 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001127 kfree(mapping);
1128
Chunming Zhouf48b2652015-10-16 14:06:19 +08001129error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 return r;
1131}
1132
1133/**
1134 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1135 *
1136 * @adev: amdgpu_device pointer
1137 * @bo_va: bo_va to remove the address from
1138 * @saddr: where to the BO is mapped
1139 *
1140 * Remove a mapping of the BO at the specefied addr from the VM.
1141 * Returns 0 for success, error for failure.
1142 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001143 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144 */
1145int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1146 struct amdgpu_bo_va *bo_va,
1147 uint64_t saddr)
1148{
1149 struct amdgpu_bo_va_mapping *mapping;
1150 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001151 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152
Christian König6c7fc502015-06-05 20:56:17 +02001153 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001154 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001155 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156 if (mapping->it.start == saddr)
1157 break;
1158 }
1159
Christian König7fc11952015-07-30 11:53:42 +02001160 if (&mapping->list == &bo_va->valids) {
1161 valid = false;
1162
1163 list_for_each_entry(mapping, &bo_va->invalids, list) {
1164 if (mapping->it.start == saddr)
1165 break;
1166 }
1167
Chunming Zhou69b576a2015-11-18 11:17:39 +08001168 if (&mapping->list == &bo_va->invalids) {
1169 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001170 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001171 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001173 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001175 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001176 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001177 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001178 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001179
jimqu81d75a32015-12-04 17:17:00 +08001180 if (valid) {
1181 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001182 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001183 spin_unlock(&vm->freed_lock);
1184 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001185 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001186 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187
1188 return 0;
1189}
1190
1191/**
1192 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1193 *
1194 * @adev: amdgpu_device pointer
1195 * @bo_va: requested bo_va
1196 *
Christian König8843dbb2016-01-26 12:17:11 +01001197 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001198 *
1199 * Object have to be reserved!
1200 */
1201void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1202 struct amdgpu_bo_va *bo_va)
1203{
1204 struct amdgpu_bo_va_mapping *mapping, *next;
1205 struct amdgpu_vm *vm = bo_va->vm;
1206
1207 list_del(&bo_va->bo_list);
1208
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209 spin_lock(&vm->status_lock);
1210 list_del(&bo_va->vm_status);
1211 spin_unlock(&vm->status_lock);
1212
Christian König7fc11952015-07-30 11:53:42 +02001213 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001215 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001216 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001217 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001218 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001219 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001220 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001221 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 }
Christian König7fc11952015-07-30 11:53:42 +02001223 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1224 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001225 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001226 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001227 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001228 kfree(mapping);
1229 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001230 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001231 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233}
1234
1235/**
1236 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1237 *
1238 * @adev: amdgpu_device pointer
1239 * @vm: requested vm
1240 * @bo: amdgpu buffer object
1241 *
Christian König8843dbb2016-01-26 12:17:11 +01001242 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001243 */
1244void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1245 struct amdgpu_bo *bo)
1246{
1247 struct amdgpu_bo_va *bo_va;
1248
1249 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001250 spin_lock(&bo_va->vm->status_lock);
1251 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001253 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001254 }
1255}
1256
1257/**
1258 * amdgpu_vm_init - initialize a vm instance
1259 *
1260 * @adev: amdgpu_device pointer
1261 * @vm: requested vm
1262 *
Christian König8843dbb2016-01-26 12:17:11 +01001263 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001264 */
1265int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1266{
1267 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1268 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001269 unsigned pd_size, pd_entries;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270 int i, r;
1271
1272 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1273 vm->ids[i].id = 0;
1274 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001275 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 vm->va = RB_ROOT;
1277 spin_lock_init(&vm->status_lock);
1278 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001279 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001281 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001282 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 pd_size = amdgpu_vm_directory_size(adev);
1284 pd_entries = amdgpu_vm_num_pdes(adev);
1285
1286 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001287 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001288 if (vm->page_tables == NULL) {
1289 DRM_ERROR("Cannot allocate memory for page table array\n");
1290 return -ENOMEM;
1291 }
1292
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001293 vm->page_directory_fence = NULL;
1294
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001296 AMDGPU_GEM_DOMAIN_VRAM,
1297 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001298 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299 if (r)
1300 return r;
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001301 r = amdgpu_bo_reserve(vm->page_directory, false);
1302 if (r) {
1303 amdgpu_bo_unref(&vm->page_directory);
1304 vm->page_directory = NULL;
1305 return r;
1306 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001308 amdgpu_bo_unreserve(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001309 if (r) {
1310 amdgpu_bo_unref(&vm->page_directory);
1311 vm->page_directory = NULL;
1312 return r;
1313 }
1314
1315 return 0;
1316}
1317
1318/**
1319 * amdgpu_vm_fini - tear down a vm instance
1320 *
1321 * @adev: amdgpu_device pointer
1322 * @vm: requested vm
1323 *
Christian König8843dbb2016-01-26 12:17:11 +01001324 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001325 * Unbind the VM and remove all bos from the vm bo list
1326 */
1327void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1328{
1329 struct amdgpu_bo_va_mapping *mapping, *tmp;
1330 int i;
1331
1332 if (!RB_EMPTY_ROOT(&vm->va)) {
1333 dev_err(adev->dev, "still active bo inside vm\n");
1334 }
1335 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1336 list_del(&mapping->list);
1337 interval_tree_remove(&mapping->it, &vm->va);
1338 kfree(mapping);
1339 }
1340 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1341 list_del(&mapping->list);
1342 kfree(mapping);
1343 }
1344
1345 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001346 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001347 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348
1349 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001350 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001351 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König1c16c0a2015-11-14 21:31:40 +01001352 unsigned id = vm->ids[i].id;
1353
1354 atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner,
1355 (long)vm, 0);
Chunming Zhou3c623382015-08-20 18:33:59 +08001356 fence_put(vm->ids[i].flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001357 }
1358
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001359}
Christian Königea89f8c2015-11-15 20:52:06 +01001360
1361/**
Christian Königa9a78b32016-01-21 10:19:11 +01001362 * amdgpu_vm_manager_init - init the VM manager
1363 *
1364 * @adev: amdgpu_device pointer
1365 *
1366 * Initialize the VM manager structures
1367 */
1368void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1369{
1370 unsigned i;
1371
1372 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1373
1374 /* skip over VMID 0, since it is the system VM */
1375 for (i = 1; i < adev->vm_manager.num_ids; ++i)
1376 list_add_tail(&adev->vm_manager.ids[i].list,
1377 &adev->vm_manager.ids_lru);
1378}
1379
1380/**
Christian Königea89f8c2015-11-15 20:52:06 +01001381 * amdgpu_vm_manager_fini - cleanup VM manager
1382 *
1383 * @adev: amdgpu_device pointer
1384 *
1385 * Cleanup the VM manager and free resources.
1386 */
1387void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1388{
1389 unsigned i;
1390
1391 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001392 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001393}