blob: b68642b47b7b0e41c4cfc55017916dbc225ba398 [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
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800316int amdgpu_vm_free_job(struct amdgpu_job *job)
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800317{
318 int i;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800319 for (i = 0; i < job->num_ibs; i++)
320 amdgpu_ib_free(job->adev, &job->ibs[i]);
321 kfree(job->ibs);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800322 return 0;
323}
324
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400325/**
326 * amdgpu_vm_clear_bo - initially clear the page dir/table
327 *
328 * @adev: amdgpu_device pointer
329 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800330 *
331 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332 */
333static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
334 struct amdgpu_bo *bo)
335{
336 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800337 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800338 struct amdgpu_ib *ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339 unsigned entries;
340 uint64_t addr;
341 int r;
342
monk.liuca952612015-05-25 14:44:05 +0800343 r = reservation_object_reserve_shared(bo->tbo.resv);
344 if (r)
345 return r;
346
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
348 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800349 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400350
351 addr = amdgpu_bo_gpu_offset(bo);
352 entries = amdgpu_bo_size(bo) / 8;
353
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800354 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
355 if (!ib)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800356 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357
Christian Königb07c60c2016-01-31 12:29:04 +0100358 r = amdgpu_ib_get(adev, NULL, 64, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400359 if (r)
360 goto error_free;
361
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800362 ib->length_dw = 0;
363
Christian König9ab21462015-11-30 14:19:26 +0100364 amdgpu_vm_update_pages(adev, NULL, 0, ib, addr, 0, entries, 0, 0);
Christian König9e5d53092016-01-31 12:20:55 +0100365 amdgpu_ring_pad_ib(ring, ib);
Christian Königb6ea2f32016-02-03 22:39:01 +0100366
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800367 WARN_ON(ib->length_dw > 64);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800368 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
369 &amdgpu_vm_free_job,
370 AMDGPU_FENCE_OWNER_VM,
371 &fence);
372 if (!r)
373 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800374 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800375 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800376
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800378 amdgpu_ib_free(adev, ib);
379 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800381error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382 return r;
383}
384
385/**
Christian Königb07c9d22015-11-30 13:26:07 +0100386 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400387 *
Christian Königb07c9d22015-11-30 13:26:07 +0100388 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389 * @addr: the unmapped addr
390 *
391 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100392 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400393 */
Christian Königb07c9d22015-11-30 13:26:07 +0100394uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400395{
396 uint64_t result;
397
Christian Königb07c9d22015-11-30 13:26:07 +0100398 if (pages_addr) {
399 /* page table offset */
400 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400401
Christian Königb07c9d22015-11-30 13:26:07 +0100402 /* in case cpu page size != gpu page size*/
403 result |= addr & (~PAGE_MASK);
404
405 } else {
406 /* No mapping required */
407 result = addr;
408 }
409
410 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400411
412 return result;
413}
414
415/**
416 * amdgpu_vm_update_pdes - make sure that page directory is valid
417 *
418 * @adev: amdgpu_device pointer
419 * @vm: requested vm
420 * @start: start of GPU address range
421 * @end: end of GPU address range
422 *
423 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100424 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400426 */
427int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
428 struct amdgpu_vm *vm)
429{
430 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
431 struct amdgpu_bo *pd = vm->page_directory;
432 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
433 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
434 uint64_t last_pde = ~0, last_pt = ~0;
435 unsigned count = 0, pt_idx, ndw;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800436 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800437 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800438
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400439 int r;
440
441 /* padding, etc. */
442 ndw = 64;
443
444 /* assume the worst case */
445 ndw += vm->max_pde_used * 6;
446
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800447 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
448 if (!ib)
449 return -ENOMEM;
450
Christian Königb07c60c2016-01-31 12:29:04 +0100451 r = amdgpu_ib_get(adev, NULL, ndw * 4, ib);
Sudip Mukherjee7a574552015-10-08 19:28:01 +0530452 if (r) {
453 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454 return r;
Sudip Mukherjee7a574552015-10-08 19:28:01 +0530455 }
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800456 ib->length_dw = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457
458 /* walk over the address space and update the page directory */
459 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100460 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400461 uint64_t pde, pt;
462
463 if (bo == NULL)
464 continue;
465
466 pt = amdgpu_bo_gpu_offset(bo);
467 if (vm->page_tables[pt_idx].addr == pt)
468 continue;
469 vm->page_tables[pt_idx].addr = pt;
470
471 pde = pd_addr + pt_idx * 8;
472 if (((last_pde + 8 * count) != pde) ||
473 ((last_pt + incr * count) != pt)) {
474
475 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100476 amdgpu_vm_update_pages(adev, NULL, 0, ib,
477 last_pde, last_pt,
478 count, incr,
479 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480 }
481
482 count = 1;
483 last_pde = pde;
484 last_pt = pt;
485 } else {
486 ++count;
487 }
488 }
489
490 if (count)
Christian König9ab21462015-11-30 14:19:26 +0100491 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
492 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800494 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100495 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800496 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
497 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800498 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
499 &amdgpu_vm_free_job,
500 AMDGPU_FENCE_OWNER_VM,
501 &fence);
502 if (r)
503 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200504
Chunming Zhou4af9f072015-08-03 12:57:31 +0800505 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200506 fence_put(vm->page_directory_fence);
507 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800508 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509 }
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800510
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800511 if (ib->length_dw == 0) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800512 amdgpu_ib_free(adev, ib);
513 kfree(ib);
514 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515
516 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800517
518error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800519 amdgpu_ib_free(adev, ib);
520 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800521 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522}
523
524/**
525 * amdgpu_vm_frag_ptes - add fragment information to PTEs
526 *
527 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100528 * @gtt: GART instance to use for mapping
529 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 * @ib: IB for the update
531 * @pe_start: first PTE to handle
532 * @pe_end: last PTE to handle
533 * @addr: addr those PTEs should point to
534 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535 */
536static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100537 struct amdgpu_gart *gtt,
538 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 struct amdgpu_ib *ib,
540 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100541 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542{
543 /**
544 * The MC L1 TLB supports variable sized pages, based on a fragment
545 * field in the PTE. When this field is set to a non-zero value, page
546 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
547 * flags are considered valid for all PTEs within the fragment range
548 * and corresponding mappings are assumed to be physically contiguous.
549 *
550 * The L1 TLB can store a single PTE for the whole fragment,
551 * significantly increasing the space available for translation
552 * caching. This leads to large improvements in throughput when the
553 * TLB is under pressure.
554 *
555 * The L2 TLB distributes small and large fragments into two
556 * asymmetric partitions. The large fragment cache is significantly
557 * larger. Thus, we try to use large fragments wherever possible.
558 * Userspace can support this by aligning virtual base address and
559 * allocation size to the fragment size.
560 */
561
562 /* SI and newer are optimized for 64KB */
563 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
564 uint64_t frag_align = 0x80;
565
566 uint64_t frag_start = ALIGN(pe_start, frag_align);
567 uint64_t frag_end = pe_end & ~(frag_align - 1);
568
569 unsigned count;
570
Christian König31f6c1f2016-01-26 12:37:49 +0100571 /* Abort early if there isn't anything to do */
572 if (pe_start == pe_end)
573 return;
574
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400575 /* system pages are non continuously */
Christian König9ab21462015-11-30 14:19:26 +0100576 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577
578 count = (pe_end - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100579 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
580 addr, count, AMDGPU_GPU_PAGE_SIZE,
581 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 return;
583 }
584
585 /* handle the 4K area at the beginning */
586 if (pe_start != frag_start) {
587 count = (frag_start - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100588 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
589 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400590 addr += AMDGPU_GPU_PAGE_SIZE * count;
591 }
592
593 /* handle the area in the middle */
594 count = (frag_end - frag_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100595 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
596 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597
598 /* handle the 4K area at the end */
599 if (frag_end != pe_end) {
600 addr += AMDGPU_GPU_PAGE_SIZE * count;
601 count = (pe_end - frag_end) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100602 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
603 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 }
605}
606
607/**
608 * amdgpu_vm_update_ptes - make sure that page tables are valid
609 *
610 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100611 * @gtt: GART instance to use for mapping
612 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400613 * @vm: requested vm
614 * @start: start of GPU address range
615 * @end: end of GPU address range
616 * @dst: destination address to map to
617 * @flags: mapping flags
618 *
Christian König8843dbb2016-01-26 12:17:11 +0100619 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620 */
Christian Königa1e08d32016-01-26 11:40:46 +0100621static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
622 struct amdgpu_gart *gtt,
623 uint32_t gtt_flags,
624 struct amdgpu_vm *vm,
625 struct amdgpu_ib *ib,
626 uint64_t start, uint64_t end,
627 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628{
Christian König31f6c1f2016-01-26 12:37:49 +0100629 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
630
631 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400632 uint64_t addr;
633
634 /* walk over the address space and update the page tables */
635 for (addr = start; addr < end; ) {
636 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100637 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100639 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640
641 if ((addr & ~mask) == (end & ~mask))
642 nptes = end - addr;
643 else
644 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
645
Christian König31f6c1f2016-01-26 12:37:49 +0100646 pe_start = amdgpu_bo_gpu_offset(pt);
647 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648
Christian König31f6c1f2016-01-26 12:37:49 +0100649 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650
Christian König31f6c1f2016-01-26 12:37:49 +0100651 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
652 last_pe_start, last_pe_end,
653 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654
Christian König31f6c1f2016-01-26 12:37:49 +0100655 last_pe_start = pe_start;
656 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400657 last_dst = dst;
658 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100659 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 }
661
662 addr += nptes;
663 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
664 }
665
Christian König31f6c1f2016-01-26 12:37:49 +0100666 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
667 last_pe_start, last_pe_end,
668 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400669}
670
671/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
673 *
674 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100675 * @gtt: GART instance to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 * @gtt_flags: flags as they are used for GTT
Christian Königa14faa62016-01-25 14:27:31 +0100677 * @vm: requested vm
678 * @start: start of mapped range
679 * @last: last mapped entry
680 * @flags: flags for the entries
681 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 * @fence: optional resulting fence
683 *
Christian Königa14faa62016-01-25 14:27:31 +0100684 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 */
687static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100688 struct amdgpu_gart *gtt,
689 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100691 uint64_t start, uint64_t last,
692 uint32_t flags, uint64_t addr,
693 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694{
695 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100696 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 unsigned nptes, ncmds, ndw;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800698 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800699 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 int r;
701
Christian Königa1e08d32016-01-26 11:40:46 +0100702 /* sync to everything on unmapping */
703 if (!(flags & AMDGPU_PTE_VALID))
704 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
705
Christian Königa14faa62016-01-25 14:27:31 +0100706 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707
708 /*
709 * reserve space for one command every (1 << BLOCK_SIZE)
710 * entries or 2k dwords (whatever is smaller)
711 */
712 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
713
714 /* padding, etc. */
715 ndw = 64;
716
Christian König9ab21462015-11-30 14:19:26 +0100717 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400718 /* only copy commands needed */
719 ndw += ncmds * 7;
720
Christian König9ab21462015-11-30 14:19:26 +0100721 } else if (gtt) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722 /* header for write data commands */
723 ndw += ncmds * 4;
724
725 /* body of write data command */
726 ndw += nptes * 2;
727
728 } else {
729 /* set page commands needed */
730 ndw += ncmds * 10;
731
732 /* two extra commands for begin/end of fragment */
733 ndw += 2 * 10;
734 }
735
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800736 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
737 if (!ib)
738 return -ENOMEM;
739
Christian Königb07c60c2016-01-31 12:29:04 +0100740 r = amdgpu_ib_get(adev, NULL, ndw * 4, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800741 if (r) {
742 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400743 return r;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800744 }
745
Christian Königa1e08d32016-01-26 11:40:46 +0100746 r = amdgpu_sync_resv(adev, &ib->sync, vm->page_directory->tbo.resv,
747 owner);
748 if (r)
749 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400750
Christian Königa1e08d32016-01-26 11:40:46 +0100751 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
752 if (r)
753 goto error_free;
754
755 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
756 addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757
Christian König9e5d53092016-01-31 12:20:55 +0100758 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800759 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800760 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
761 &amdgpu_vm_free_job,
762 AMDGPU_FENCE_OWNER_VM,
763 &f);
764 if (r)
765 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766
Christian Königbf60efd2015-09-04 10:47:56 +0200767 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800768 if (fence) {
769 fence_put(*fence);
770 *fence = fence_get(f);
771 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800772 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400773 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800774
775error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800776 amdgpu_ib_free(adev, ib);
777 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800778 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779}
780
781/**
Christian Königa14faa62016-01-25 14:27:31 +0100782 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
783 *
784 * @adev: amdgpu_device pointer
785 * @gtt: GART instance to use for mapping
786 * @vm: requested vm
787 * @mapping: mapped range and flags to use for the update
788 * @addr: addr to set the area to
789 * @gtt_flags: flags as they are used for GTT
790 * @fence: optional resulting fence
791 *
792 * Split the mapping into smaller chunks so that each update fits
793 * into a SDMA IB.
794 * Returns 0 for success, -EINVAL for failure.
795 */
796static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
797 struct amdgpu_gart *gtt,
798 uint32_t gtt_flags,
799 struct amdgpu_vm *vm,
800 struct amdgpu_bo_va_mapping *mapping,
801 uint64_t addr, struct fence **fence)
802{
803 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
804
805 uint64_t start = mapping->it.start;
806 uint32_t flags = gtt_flags;
807 int r;
808
809 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
810 * but in case of something, we filter the flags in first place
811 */
812 if (!(mapping->flags & AMDGPU_PTE_READABLE))
813 flags &= ~AMDGPU_PTE_READABLE;
814 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
815 flags &= ~AMDGPU_PTE_WRITEABLE;
816
817 trace_amdgpu_vm_bo_update(mapping);
818
819 addr += mapping->offset;
820
821 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
822 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
823 start, mapping->it.last,
824 flags, addr, fence);
825
826 while (start != mapping->it.last + 1) {
827 uint64_t last;
828
829 last = min((uint64_t)mapping->it.last, start + max_size);
830 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
831 start, last, flags, addr,
832 fence);
833 if (r)
834 return r;
835
836 start = last + 1;
837 addr += max_size;
838 }
839
840 return 0;
841}
842
843/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
845 *
846 * @adev: amdgpu_device pointer
847 * @bo_va: requested BO and VM object
848 * @mem: ttm mem
849 *
850 * Fill in the page table entries for @bo_va.
851 * Returns 0 for success, -EINVAL for failure.
852 *
853 * Object have to be reserved and mutex must be locked!
854 */
855int amdgpu_vm_bo_update(struct amdgpu_device *adev,
856 struct amdgpu_bo_va *bo_va,
857 struct ttm_mem_reg *mem)
858{
859 struct amdgpu_vm *vm = bo_va->vm;
860 struct amdgpu_bo_va_mapping *mapping;
Christian König9ab21462015-11-30 14:19:26 +0100861 struct amdgpu_gart *gtt = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400862 uint32_t flags;
863 uint64_t addr;
864 int r;
865
866 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200867 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100868 switch (mem->mem_type) {
869 case TTM_PL_TT:
870 gtt = &bo_va->bo->adev->gart;
871 break;
872
873 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100875 break;
876
877 default:
878 break;
879 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880 } else {
881 addr = 0;
882 }
883
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
885
Christian König7fc11952015-07-30 11:53:42 +0200886 spin_lock(&vm->status_lock);
887 if (!list_empty(&bo_va->vm_status))
888 list_splice_init(&bo_va->valids, &bo_va->invalids);
889 spin_unlock(&vm->status_lock);
890
891 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa14faa62016-01-25 14:27:31 +0100892 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
893 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894 if (r)
895 return r;
896 }
897
Christian Königd6c10f62015-09-28 12:00:23 +0200898 if (trace_amdgpu_vm_bo_mapping_enabled()) {
899 list_for_each_entry(mapping, &bo_va->valids, list)
900 trace_amdgpu_vm_bo_mapping(mapping);
901
902 list_for_each_entry(mapping, &bo_va->invalids, list)
903 trace_amdgpu_vm_bo_mapping(mapping);
904 }
905
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400906 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800907 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400908 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200909 if (!mem)
910 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400911 spin_unlock(&vm->status_lock);
912
913 return 0;
914}
915
916/**
917 * amdgpu_vm_clear_freed - clear freed BOs in the PT
918 *
919 * @adev: amdgpu_device pointer
920 * @vm: requested vm
921 *
922 * Make sure all freed BOs are cleared in the PT.
923 * Returns 0 for success.
924 *
925 * PTs have to be reserved and mutex must be locked!
926 */
927int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
928 struct amdgpu_vm *vm)
929{
930 struct amdgpu_bo_va_mapping *mapping;
931 int r;
932
jimqu81d75a32015-12-04 17:17:00 +0800933 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400934 while (!list_empty(&vm->freed)) {
935 mapping = list_first_entry(&vm->freed,
936 struct amdgpu_bo_va_mapping, list);
937 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800938 spin_unlock(&vm->freed_lock);
Christian Königa14faa62016-01-25 14:27:31 +0100939 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
940 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400941 kfree(mapping);
942 if (r)
943 return r;
944
jimqu81d75a32015-12-04 17:17:00 +0800945 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 }
jimqu81d75a32015-12-04 17:17:00 +0800947 spin_unlock(&vm->freed_lock);
948
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400949 return 0;
950
951}
952
953/**
954 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
955 *
956 * @adev: amdgpu_device pointer
957 * @vm: requested vm
958 *
959 * Make sure all invalidated BOs are cleared in the PT.
960 * Returns 0 for success.
961 *
962 * PTs have to be reserved and mutex must be locked!
963 */
964int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800965 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966{
monk.liucfe2c972015-05-26 15:01:54 +0800967 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200968 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969
970 spin_lock(&vm->status_lock);
971 while (!list_empty(&vm->invalidated)) {
972 bo_va = list_first_entry(&vm->invalidated,
973 struct amdgpu_bo_va, vm_status);
974 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800975 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800977 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978 if (r)
979 return r;
980
981 spin_lock(&vm->status_lock);
982 }
983 spin_unlock(&vm->status_lock);
984
monk.liucfe2c972015-05-26 15:01:54 +0800985 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800986 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200987
988 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989}
990
991/**
992 * amdgpu_vm_bo_add - add a bo to a specific vm
993 *
994 * @adev: amdgpu_device pointer
995 * @vm: requested vm
996 * @bo: amdgpu buffer object
997 *
Christian König8843dbb2016-01-26 12:17:11 +0100998 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999 * Add @bo to the list of bos associated with the vm
1000 * Returns newly added bo_va or NULL for failure
1001 *
1002 * Object has to be reserved!
1003 */
1004struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1005 struct amdgpu_vm *vm,
1006 struct amdgpu_bo *bo)
1007{
1008 struct amdgpu_bo_va *bo_va;
1009
1010 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1011 if (bo_va == NULL) {
1012 return NULL;
1013 }
1014 bo_va->vm = vm;
1015 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016 bo_va->ref_count = 1;
1017 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001018 INIT_LIST_HEAD(&bo_va->valids);
1019 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001021 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001022 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023
1024 return bo_va;
1025}
1026
1027/**
1028 * amdgpu_vm_bo_map - map bo inside a vm
1029 *
1030 * @adev: amdgpu_device pointer
1031 * @bo_va: bo_va to store the address
1032 * @saddr: where to map the BO
1033 * @offset: requested offset in the BO
1034 * @flags: attributes of pages (read/write/valid/etc.)
1035 *
1036 * Add a mapping of the BO at the specefied addr into the VM.
1037 * Returns 0 for success, error for failure.
1038 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001039 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001040 */
1041int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1042 struct amdgpu_bo_va *bo_va,
1043 uint64_t saddr, uint64_t offset,
1044 uint64_t size, uint32_t flags)
1045{
1046 struct amdgpu_bo_va_mapping *mapping;
1047 struct amdgpu_vm *vm = bo_va->vm;
1048 struct interval_tree_node *it;
1049 unsigned last_pfn, pt_idx;
1050 uint64_t eaddr;
1051 int r;
1052
Christian König0be52de2015-05-18 14:37:27 +02001053 /* validate the parameters */
1054 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001055 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001056 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001057
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001058 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001059 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001060 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001062
1063 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001064 if (last_pfn >= adev->vm_manager.max_pfn) {
1065 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001066 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001067 return -EINVAL;
1068 }
1069
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001070 saddr /= AMDGPU_GPU_PAGE_SIZE;
1071 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1072
Chunming Zhouc25867d2015-11-13 13:32:01 +08001073 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001074 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001075 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076 if (it) {
1077 struct amdgpu_bo_va_mapping *tmp;
1078 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1079 /* bo and tmp overlap, invalid addr */
1080 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1081 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1082 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001084 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001085 }
1086
1087 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1088 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001089 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001090 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091 }
1092
1093 INIT_LIST_HEAD(&mapping->list);
1094 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001095 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001096 mapping->offset = offset;
1097 mapping->flags = flags;
1098
Chunming Zhou69b576a2015-11-18 11:17:39 +08001099 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001100 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001101 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001102 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001103 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001104 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001105 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 /* Make sure the page tables are allocated */
1108 saddr >>= amdgpu_vm_block_size;
1109 eaddr >>= amdgpu_vm_block_size;
1110
1111 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1112
1113 if (eaddr > vm->max_pde_used)
1114 vm->max_pde_used = eaddr;
1115
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 /* walk over the address space and allocate the page tables */
1117 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001118 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001119 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001120 struct amdgpu_bo *pt;
1121
Christian Königee1782c2015-12-11 21:01:23 +01001122 entry = &vm->page_tables[pt_idx].entry;
1123 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124 continue;
1125
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001126 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1127 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001128 AMDGPU_GEM_DOMAIN_VRAM,
1129 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001130 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001131 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001132 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001133
Christian König82b9c552015-11-27 16:49:00 +01001134 /* Keep a reference to the page table to avoid freeing
1135 * them up in the wrong order.
1136 */
1137 pt->parent = amdgpu_bo_ref(vm->page_directory);
1138
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001139 r = amdgpu_vm_clear_bo(adev, pt);
1140 if (r) {
1141 amdgpu_bo_unref(&pt);
1142 goto error_free;
1143 }
1144
Christian Königee1782c2015-12-11 21:01:23 +01001145 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001146 entry->priority = 0;
1147 entry->tv.bo = &entry->robj->tbo;
1148 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150 }
1151
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152 return 0;
1153
1154error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001155 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001156 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001157 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001158 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001159 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160 kfree(mapping);
1161
Chunming Zhouf48b2652015-10-16 14:06:19 +08001162error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001163 return r;
1164}
1165
1166/**
1167 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1168 *
1169 * @adev: amdgpu_device pointer
1170 * @bo_va: bo_va to remove the address from
1171 * @saddr: where to the BO is mapped
1172 *
1173 * Remove a mapping of the BO at the specefied addr from the VM.
1174 * Returns 0 for success, error for failure.
1175 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001176 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177 */
1178int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1179 struct amdgpu_bo_va *bo_va,
1180 uint64_t saddr)
1181{
1182 struct amdgpu_bo_va_mapping *mapping;
1183 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001184 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001185
Christian König6c7fc502015-06-05 20:56:17 +02001186 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001187 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001188 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001189 if (mapping->it.start == saddr)
1190 break;
1191 }
1192
Christian König7fc11952015-07-30 11:53:42 +02001193 if (&mapping->list == &bo_va->valids) {
1194 valid = false;
1195
1196 list_for_each_entry(mapping, &bo_va->invalids, list) {
1197 if (mapping->it.start == saddr)
1198 break;
1199 }
1200
Chunming Zhou69b576a2015-11-18 11:17:39 +08001201 if (&mapping->list == &bo_va->invalids) {
1202 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001203 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001204 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001205 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001206 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001208 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001210 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001211 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212
jimqu81d75a32015-12-04 17:17:00 +08001213 if (valid) {
1214 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001216 spin_unlock(&vm->freed_lock);
1217 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001218 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001219 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001220
1221 return 0;
1222}
1223
1224/**
1225 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1226 *
1227 * @adev: amdgpu_device pointer
1228 * @bo_va: requested bo_va
1229 *
Christian König8843dbb2016-01-26 12:17:11 +01001230 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231 *
1232 * Object have to be reserved!
1233 */
1234void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1235 struct amdgpu_bo_va *bo_va)
1236{
1237 struct amdgpu_bo_va_mapping *mapping, *next;
1238 struct amdgpu_vm *vm = bo_va->vm;
1239
1240 list_del(&bo_va->bo_list);
1241
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001242 spin_lock(&vm->status_lock);
1243 list_del(&bo_va->vm_status);
1244 spin_unlock(&vm->status_lock);
1245
Christian König7fc11952015-07-30 11:53:42 +02001246 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001248 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001249 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001250 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001251 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001252 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001253 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001254 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001255 }
Christian König7fc11952015-07-30 11:53:42 +02001256 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1257 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001258 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001259 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001260 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001261 kfree(mapping);
1262 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001263 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001264 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001265 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266}
1267
1268/**
1269 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1270 *
1271 * @adev: amdgpu_device pointer
1272 * @vm: requested vm
1273 * @bo: amdgpu buffer object
1274 *
Christian König8843dbb2016-01-26 12:17:11 +01001275 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 */
1277void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1278 struct amdgpu_bo *bo)
1279{
1280 struct amdgpu_bo_va *bo_va;
1281
1282 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001283 spin_lock(&bo_va->vm->status_lock);
1284 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001285 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001286 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001287 }
1288}
1289
1290/**
1291 * amdgpu_vm_init - initialize a vm instance
1292 *
1293 * @adev: amdgpu_device pointer
1294 * @vm: requested vm
1295 *
Christian König8843dbb2016-01-26 12:17:11 +01001296 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297 */
1298int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1299{
1300 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1301 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001302 unsigned pd_size, pd_entries;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 int i, r;
1304
1305 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1306 vm->ids[i].id = 0;
1307 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001309 vm->va = RB_ROOT;
1310 spin_lock_init(&vm->status_lock);
1311 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001312 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001313 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001314 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001315 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001316 pd_size = amdgpu_vm_directory_size(adev);
1317 pd_entries = amdgpu_vm_num_pdes(adev);
1318
1319 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001320 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001321 if (vm->page_tables == NULL) {
1322 DRM_ERROR("Cannot allocate memory for page table array\n");
1323 return -ENOMEM;
1324 }
1325
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001326 vm->page_directory_fence = NULL;
1327
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001328 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001329 AMDGPU_GEM_DOMAIN_VRAM,
1330 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001331 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001332 if (r)
1333 return r;
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001334 r = amdgpu_bo_reserve(vm->page_directory, false);
1335 if (r) {
1336 amdgpu_bo_unref(&vm->page_directory);
1337 vm->page_directory = NULL;
1338 return r;
1339 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001340 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001341 amdgpu_bo_unreserve(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001342 if (r) {
1343 amdgpu_bo_unref(&vm->page_directory);
1344 vm->page_directory = NULL;
1345 return r;
1346 }
1347
1348 return 0;
1349}
1350
1351/**
1352 * amdgpu_vm_fini - tear down a vm instance
1353 *
1354 * @adev: amdgpu_device pointer
1355 * @vm: requested vm
1356 *
Christian König8843dbb2016-01-26 12:17:11 +01001357 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 * Unbind the VM and remove all bos from the vm bo list
1359 */
1360void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1361{
1362 struct amdgpu_bo_va_mapping *mapping, *tmp;
1363 int i;
1364
1365 if (!RB_EMPTY_ROOT(&vm->va)) {
1366 dev_err(adev->dev, "still active bo inside vm\n");
1367 }
1368 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1369 list_del(&mapping->list);
1370 interval_tree_remove(&mapping->it, &vm->va);
1371 kfree(mapping);
1372 }
1373 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1374 list_del(&mapping->list);
1375 kfree(mapping);
1376 }
1377
1378 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001379 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001380 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001381
1382 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001383 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König1c16c0a2015-11-14 21:31:40 +01001385 unsigned id = vm->ids[i].id;
1386
1387 atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner,
1388 (long)vm, 0);
Chunming Zhou3c623382015-08-20 18:33:59 +08001389 fence_put(vm->ids[i].flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001390 }
1391
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392}
Christian Königea89f8c2015-11-15 20:52:06 +01001393
1394/**
Christian Königa9a78b32016-01-21 10:19:11 +01001395 * amdgpu_vm_manager_init - init the VM manager
1396 *
1397 * @adev: amdgpu_device pointer
1398 *
1399 * Initialize the VM manager structures
1400 */
1401void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1402{
1403 unsigned i;
1404
1405 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1406
1407 /* skip over VMID 0, since it is the system VM */
1408 for (i = 1; i < adev->vm_manager.num_ids; ++i)
1409 list_add_tail(&adev->vm_manager.ids[i].list,
1410 &adev->vm_manager.ids_lru);
1411}
1412
1413/**
Christian Königea89f8c2015-11-15 20:52:06 +01001414 * amdgpu_vm_manager_fini - cleanup VM manager
1415 *
1416 * @adev: amdgpu_device pointer
1417 *
1418 * Cleanup the VM manager and free resources.
1419 */
1420void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1421{
1422 unsigned i;
1423
1424 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001425 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001426}