blob: 161652bb3d6f4aaecb7082c9657dd3f94bfb6b8b [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <drm/drmP.h>
29#include <drm/amdgpu_drm.h>
30#include "amdgpu.h"
31#include "amdgpu_trace.h"
32
33/*
34 * GPUVM
35 * GPUVM is similar to the legacy gart on older asics, however
36 * rather than there being a single global gart table
37 * for the entire GPU, there are multiple VM page tables active
38 * at any given time. The VM page tables can contain a mix
39 * vram pages and system memory pages and system memory pages
40 * can be mapped as snooped (cached system pages) or unsnooped
41 * (uncached system pages).
42 * Each VM has an ID associated with it and there is a page table
43 * associated with each VMID. When execting a command buffer,
44 * the kernel tells the the ring what VMID to use for that command
45 * buffer. VMIDs are allocated dynamically as commands are submitted.
46 * The userspace drivers maintain their own address space and the kernel
47 * sets up their pages tables accordingly when they submit their
48 * command buffers and a VMID is assigned.
49 * Cayman/Trinity support up to 8 active VMs at any given time;
50 * SI supports 16.
51 */
52
53/**
54 * amdgpu_vm_num_pde - return the number of page directory entries
55 *
56 * @adev: amdgpu_device pointer
57 *
58 * Calculate the number of page directory entries (cayman+).
59 */
60static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
61{
62 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
63}
64
65/**
66 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
67 *
68 * @adev: amdgpu_device pointer
69 *
70 * Calculate the size of the page directory in bytes (cayman+).
71 */
72static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
73{
74 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
75}
76
77/**
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 +0200159 * Global mutex must be locked!
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 */
Christian König7f8a5292015-07-20 16:09:40 +0200161int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König94dd0a42016-01-18 17:01:42 +0100162 struct amdgpu_sync *sync, struct fence *fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
165 struct amdgpu_device *adev = ring->adev;
Christian Königa9a78b32016-01-21 10:19:11 +0100166 struct amdgpu_vm_manager_id *id;
167 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168
Christian König94dd0a42016-01-18 17:01:42 +0100169 mutex_lock(&adev->vm_manager.lock);
170
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 /* check if the id is still valid */
Christian König1c16c0a2015-11-14 21:31:40 +0100172 if (vm_id->id) {
Christian König1c16c0a2015-11-14 21:31:40 +0100173 long owner;
174
Christian Königa9a78b32016-01-21 10:19:11 +0100175 id = &adev->vm_manager.ids[vm_id->id];
176 owner = atomic_long_read(&id->owner);
Christian König1c16c0a2015-11-14 21:31:40 +0100177 if (owner == (long)vm) {
Christian Königa9a78b32016-01-21 10:19:11 +0100178 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König165e4e02016-01-07 18:15:22 +0100179 trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx);
Christian Königa9a78b32016-01-21 10:19:11 +0100180
181 fence_put(id->active);
182 id->active = fence_get(fence);
183
Christian König94dd0a42016-01-18 17:01:42 +0100184 mutex_unlock(&adev->vm_manager.lock);
Christian König1c16c0a2015-11-14 21:31:40 +0100185 return 0;
186 }
Christian König39ff8442015-09-28 12:01:20 +0200187 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188
189 /* we definately need to flush */
190 vm_id->pd_gpu_addr = ~0ll;
191
Christian Königa9a78b32016-01-21 10:19:11 +0100192 id = list_first_entry(&adev->vm_manager.ids_lru,
193 struct amdgpu_vm_manager_id,
194 list);
195 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
196 atomic_long_set(&id->owner, (long)vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197
Christian Königa9a78b32016-01-21 10:19:11 +0100198 vm_id->id = id - adev->vm_manager.ids;
199 trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200
Christian Königa9a78b32016-01-21 10:19:11 +0100201 r = amdgpu_sync_fence(ring->adev, sync, id->active);
202
203 if (!r) {
204 fence_put(id->active);
205 id->active = fence_get(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 }
207
Christian König94dd0a42016-01-18 17:01:42 +0100208 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100209 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210}
211
212/**
213 * amdgpu_vm_flush - hardware flush the vm
214 *
215 * @ring: ring to use for flush
216 * @vm: vm we want to flush
217 * @updates: last vm update that we waited for
218 *
219 * Flush the vm (cayman+).
220 *
221 * Global and local mutex must be locked!
222 */
223void amdgpu_vm_flush(struct amdgpu_ring *ring,
224 struct amdgpu_vm *vm,
Chunming Zhou3c623382015-08-20 18:33:59 +0800225 struct fence *updates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226{
227 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
228 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
Chunming Zhou3c623382015-08-20 18:33:59 +0800229 struct fence *flushed_updates = vm_id->flushed_updates;
Christian Königb56c2282015-10-29 17:01:19 +0100230 bool is_later;
Chunming Zhou3c623382015-08-20 18:33:59 +0800231
Christian Königb56c2282015-10-29 17:01:19 +0100232 if (!flushed_updates)
233 is_later = true;
234 else if (!updates)
235 is_later = false;
236 else
237 is_later = fence_is_later(updates, flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238
Christian Königb56c2282015-10-29 17:01:19 +0100239 if (pd_addr != vm_id->pd_gpu_addr || is_later) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
Christian Königb56c2282015-10-29 17:01:19 +0100241 if (is_later) {
Chunming Zhou3c623382015-08-20 18:33:59 +0800242 vm_id->flushed_updates = fence_get(updates);
243 fence_put(flushed_updates);
244 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400245 vm_id->pd_gpu_addr = pd_addr;
246 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
247 }
248}
249
250/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400251 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
252 *
253 * @vm: requested vm
254 * @bo: requested buffer object
255 *
256 * Find @bo inside the requested vm (cayman+).
257 * Search inside the @bos vm list for the requested vm
258 * Returns the found bo_va or NULL if none is found
259 *
260 * Object has to be reserved!
261 */
262struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
263 struct amdgpu_bo *bo)
264{
265 struct amdgpu_bo_va *bo_va;
266
267 list_for_each_entry(bo_va, &bo->va, bo_list) {
268 if (bo_va->vm == vm) {
269 return bo_va;
270 }
271 }
272 return NULL;
273}
274
275/**
276 * amdgpu_vm_update_pages - helper to call the right asic function
277 *
278 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100279 * @gtt: GART instance to use for mapping
280 * @gtt_flags: GTT hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281 * @ib: indirect buffer to fill with commands
282 * @pe: addr of the page entry
283 * @addr: dst addr to write into pe
284 * @count: number of page entries to update
285 * @incr: increase next addr by incr bytes
286 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400287 *
288 * Traces the parameters and calls the right asic functions
289 * to setup the page table using the DMA.
290 */
291static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100292 struct amdgpu_gart *gtt,
293 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294 struct amdgpu_ib *ib,
295 uint64_t pe, uint64_t addr,
296 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100297 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298{
299 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
300
Christian König9ab21462015-11-30 14:19:26 +0100301 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
302 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
304
Christian König9ab21462015-11-30 14:19:26 +0100305 } else if (gtt) {
306 dma_addr_t *pages_addr = gtt->pages_addr;
Christian Königb07c9d22015-11-30 13:26:07 +0100307 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
308 count, incr, flags);
309
310 } else if (count < 3) {
311 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
312 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400313
314 } else {
315 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
316 count, incr, flags);
317 }
318}
319
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800320int amdgpu_vm_free_job(struct amdgpu_job *job)
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800321{
322 int i;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800323 for (i = 0; i < job->num_ibs; i++)
324 amdgpu_ib_free(job->adev, &job->ibs[i]);
325 kfree(job->ibs);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800326 return 0;
327}
328
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400329/**
330 * amdgpu_vm_clear_bo - initially clear the page dir/table
331 *
332 * @adev: amdgpu_device pointer
333 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800334 *
335 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400336 */
337static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
338 struct amdgpu_bo *bo)
339{
340 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800341 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800342 struct amdgpu_ib *ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400343 unsigned entries;
344 uint64_t addr;
345 int r;
346
monk.liuca952612015-05-25 14:44:05 +0800347 r = reservation_object_reserve_shared(bo->tbo.resv);
348 if (r)
349 return r;
350
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400351 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
352 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800353 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400354
355 addr = amdgpu_bo_gpu_offset(bo);
356 entries = amdgpu_bo_size(bo) / 8;
357
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800358 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
359 if (!ib)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800360 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400361
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800362 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400363 if (r)
364 goto error_free;
365
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800366 ib->length_dw = 0;
367
Christian König9ab21462015-11-30 14:19:26 +0100368 amdgpu_vm_update_pages(adev, NULL, 0, ib, addr, 0, entries, 0, 0);
369
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800370 amdgpu_vm_pad_ib(adev, ib);
371 WARN_ON(ib->length_dw > 64);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800372 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
373 &amdgpu_vm_free_job,
374 AMDGPU_FENCE_OWNER_VM,
375 &fence);
376 if (!r)
377 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800378 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800379 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800380
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800382 amdgpu_ib_free(adev, ib);
383 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400384
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800385error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400386 return r;
387}
388
389/**
Christian Königb07c9d22015-11-30 13:26:07 +0100390 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400391 *
Christian Königb07c9d22015-11-30 13:26:07 +0100392 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400393 * @addr: the unmapped addr
394 *
395 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100396 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400397 */
Christian Königb07c9d22015-11-30 13:26:07 +0100398uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400399{
400 uint64_t result;
401
Christian Königb07c9d22015-11-30 13:26:07 +0100402 if (pages_addr) {
403 /* page table offset */
404 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405
Christian Königb07c9d22015-11-30 13:26:07 +0100406 /* in case cpu page size != gpu page size*/
407 result |= addr & (~PAGE_MASK);
408
409 } else {
410 /* No mapping required */
411 result = addr;
412 }
413
414 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415
416 return result;
417}
418
419/**
420 * amdgpu_vm_update_pdes - make sure that page directory is valid
421 *
422 * @adev: amdgpu_device pointer
423 * @vm: requested vm
424 * @start: start of GPU address range
425 * @end: end of GPU address range
426 *
427 * Allocates new page tables if necessary
428 * and updates the page directory (cayman+).
429 * Returns 0 for success, error for failure.
430 *
431 * Global and local mutex must be locked!
432 */
433int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
434 struct amdgpu_vm *vm)
435{
436 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
437 struct amdgpu_bo *pd = vm->page_directory;
438 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
439 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
440 uint64_t last_pde = ~0, last_pt = ~0;
441 unsigned count = 0, pt_idx, ndw;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800442 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800443 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800444
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445 int r;
446
447 /* padding, etc. */
448 ndw = 64;
449
450 /* assume the worst case */
451 ndw += vm->max_pde_used * 6;
452
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800453 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
454 if (!ib)
455 return -ENOMEM;
456
457 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
Sudip Mukherjee7a574552015-10-08 19:28:01 +0530458 if (r) {
459 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 return r;
Sudip Mukherjee7a574552015-10-08 19:28:01 +0530461 }
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800462 ib->length_dw = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463
464 /* walk over the address space and update the page directory */
465 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100466 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400467 uint64_t pde, pt;
468
469 if (bo == NULL)
470 continue;
471
472 pt = amdgpu_bo_gpu_offset(bo);
473 if (vm->page_tables[pt_idx].addr == pt)
474 continue;
475 vm->page_tables[pt_idx].addr = pt;
476
477 pde = pd_addr + pt_idx * 8;
478 if (((last_pde + 8 * count) != pde) ||
479 ((last_pt + incr * count) != pt)) {
480
481 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100482 amdgpu_vm_update_pages(adev, NULL, 0, ib,
483 last_pde, last_pt,
484 count, incr,
485 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 }
487
488 count = 1;
489 last_pde = pde;
490 last_pt = pt;
491 } else {
492 ++count;
493 }
494 }
495
496 if (count)
Christian König9ab21462015-11-30 14:19:26 +0100497 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
498 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800500 if (ib->length_dw != 0) {
501 amdgpu_vm_pad_ib(adev, ib);
502 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
503 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800504 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
505 &amdgpu_vm_free_job,
506 AMDGPU_FENCE_OWNER_VM,
507 &fence);
508 if (r)
509 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200510
Chunming Zhou4af9f072015-08-03 12:57:31 +0800511 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200512 fence_put(vm->page_directory_fence);
513 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800514 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515 }
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800516
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800517 if (ib->length_dw == 0) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800518 amdgpu_ib_free(adev, ib);
519 kfree(ib);
520 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521
522 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800523
524error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800525 amdgpu_ib_free(adev, ib);
526 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800527 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528}
529
530/**
531 * amdgpu_vm_frag_ptes - add fragment information to PTEs
532 *
533 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100534 * @gtt: GART instance to use for mapping
535 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 * @ib: IB for the update
537 * @pe_start: first PTE to handle
538 * @pe_end: last PTE to handle
539 * @addr: addr those PTEs should point to
540 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 *
542 * Global and local mutex must be locked!
543 */
544static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100545 struct amdgpu_gart *gtt,
546 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 struct amdgpu_ib *ib,
548 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100549 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550{
551 /**
552 * The MC L1 TLB supports variable sized pages, based on a fragment
553 * field in the PTE. When this field is set to a non-zero value, page
554 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
555 * flags are considered valid for all PTEs within the fragment range
556 * and corresponding mappings are assumed to be physically contiguous.
557 *
558 * The L1 TLB can store a single PTE for the whole fragment,
559 * significantly increasing the space available for translation
560 * caching. This leads to large improvements in throughput when the
561 * TLB is under pressure.
562 *
563 * The L2 TLB distributes small and large fragments into two
564 * asymmetric partitions. The large fragment cache is significantly
565 * larger. Thus, we try to use large fragments wherever possible.
566 * Userspace can support this by aligning virtual base address and
567 * allocation size to the fragment size.
568 */
569
570 /* SI and newer are optimized for 64KB */
571 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
572 uint64_t frag_align = 0x80;
573
574 uint64_t frag_start = ALIGN(pe_start, frag_align);
575 uint64_t frag_end = pe_end & ~(frag_align - 1);
576
577 unsigned count;
578
579 /* system pages are non continuously */
Christian König9ab21462015-11-30 14:19:26 +0100580 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581
582 count = (pe_end - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100583 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
584 addr, count, AMDGPU_GPU_PAGE_SIZE,
585 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 return;
587 }
588
589 /* handle the 4K area at the beginning */
590 if (pe_start != frag_start) {
591 count = (frag_start - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100592 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
593 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 addr += AMDGPU_GPU_PAGE_SIZE * count;
595 }
596
597 /* handle the area in the middle */
598 count = (frag_end - frag_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100599 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
600 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601
602 /* handle the 4K area at the end */
603 if (frag_end != pe_end) {
604 addr += AMDGPU_GPU_PAGE_SIZE * count;
605 count = (pe_end - frag_end) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100606 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
607 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 }
609}
610
611/**
612 * amdgpu_vm_update_ptes - make sure that page tables are valid
613 *
614 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100615 * @gtt: GART instance to use for mapping
616 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400617 * @vm: requested vm
618 * @start: start of GPU address range
619 * @end: end of GPU address range
620 * @dst: destination address to map to
621 * @flags: mapping flags
622 *
623 * Update the page tables in the range @start - @end (cayman+).
624 *
625 * Global and local mutex must be locked!
626 */
Christian Königa1e08d32016-01-26 11:40:46 +0100627static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
628 struct amdgpu_gart *gtt,
629 uint32_t gtt_flags,
630 struct amdgpu_vm *vm,
631 struct amdgpu_ib *ib,
632 uint64_t start, uint64_t end,
633 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634{
635 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
636 uint64_t last_pte = ~0, last_dst = ~0;
637 unsigned count = 0;
638 uint64_t addr;
639
640 /* walk over the address space and update the page tables */
641 for (addr = start; addr < end; ) {
642 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100643 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 unsigned nptes;
645 uint64_t pte;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646
647 if ((addr & ~mask) == (end & ~mask))
648 nptes = end - addr;
649 else
650 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
651
652 pte = amdgpu_bo_gpu_offset(pt);
653 pte += (addr & mask) * 8;
654
655 if ((last_pte + 8 * count) != pte) {
656
657 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100658 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
659 last_pte, last_pte + 8 * count,
660 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661 }
662
663 count = nptes;
664 last_pte = pte;
665 last_dst = dst;
666 } else {
667 count += nptes;
668 }
669
670 addr += nptes;
671 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
672 }
673
674 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100675 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
676 last_pte, last_pte + 8 * count,
677 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400679}
680
681/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
683 *
684 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100685 * @gtt: GART instance to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 * @gtt_flags: flags as they are used for GTT
Christian Königa14faa62016-01-25 14:27:31 +0100687 * @vm: requested vm
688 * @start: start of mapped range
689 * @last: last mapped entry
690 * @flags: flags for the entries
691 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692 * @fence: optional resulting fence
693 *
Christian Königa14faa62016-01-25 14:27:31 +0100694 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 */
697static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100698 struct amdgpu_gart *gtt,
699 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100701 uint64_t start, uint64_t last,
702 uint32_t flags, uint64_t addr,
703 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704{
705 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100706 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 unsigned nptes, ncmds, ndw;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800708 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800709 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 int r;
711
Christian Königa1e08d32016-01-26 11:40:46 +0100712 /* sync to everything on unmapping */
713 if (!(flags & AMDGPU_PTE_VALID))
714 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
715
Christian Königa14faa62016-01-25 14:27:31 +0100716 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717
718 /*
719 * reserve space for one command every (1 << BLOCK_SIZE)
720 * entries or 2k dwords (whatever is smaller)
721 */
722 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
723
724 /* padding, etc. */
725 ndw = 64;
726
Christian König9ab21462015-11-30 14:19:26 +0100727 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728 /* only copy commands needed */
729 ndw += ncmds * 7;
730
Christian König9ab21462015-11-30 14:19:26 +0100731 } else if (gtt) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 /* header for write data commands */
733 ndw += ncmds * 4;
734
735 /* body of write data command */
736 ndw += nptes * 2;
737
738 } else {
739 /* set page commands needed */
740 ndw += ncmds * 10;
741
742 /* two extra commands for begin/end of fragment */
743 ndw += 2 * 10;
744 }
745
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800746 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
747 if (!ib)
748 return -ENOMEM;
749
750 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
751 if (r) {
752 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 return r;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800754 }
755
Christian Königa1e08d32016-01-26 11:40:46 +0100756 r = amdgpu_sync_resv(adev, &ib->sync, vm->page_directory->tbo.resv,
757 owner);
758 if (r)
759 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400760
Christian Königa1e08d32016-01-26 11:40:46 +0100761 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
762 if (r)
763 goto error_free;
764
765 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
766 addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400767
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800768 amdgpu_vm_pad_ib(adev, ib);
769 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800770 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
771 &amdgpu_vm_free_job,
772 AMDGPU_FENCE_OWNER_VM,
773 &f);
774 if (r)
775 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776
Christian Königbf60efd2015-09-04 10:47:56 +0200777 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800778 if (fence) {
779 fence_put(*fence);
780 *fence = fence_get(f);
781 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800782 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400783 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800784
785error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800786 amdgpu_ib_free(adev, ib);
787 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800788 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789}
790
791/**
Christian Königa14faa62016-01-25 14:27:31 +0100792 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
793 *
794 * @adev: amdgpu_device pointer
795 * @gtt: GART instance to use for mapping
796 * @vm: requested vm
797 * @mapping: mapped range and flags to use for the update
798 * @addr: addr to set the area to
799 * @gtt_flags: flags as they are used for GTT
800 * @fence: optional resulting fence
801 *
802 * Split the mapping into smaller chunks so that each update fits
803 * into a SDMA IB.
804 * Returns 0 for success, -EINVAL for failure.
805 */
806static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
807 struct amdgpu_gart *gtt,
808 uint32_t gtt_flags,
809 struct amdgpu_vm *vm,
810 struct amdgpu_bo_va_mapping *mapping,
811 uint64_t addr, struct fence **fence)
812{
813 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
814
815 uint64_t start = mapping->it.start;
816 uint32_t flags = gtt_flags;
817 int r;
818
819 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
820 * but in case of something, we filter the flags in first place
821 */
822 if (!(mapping->flags & AMDGPU_PTE_READABLE))
823 flags &= ~AMDGPU_PTE_READABLE;
824 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
825 flags &= ~AMDGPU_PTE_WRITEABLE;
826
827 trace_amdgpu_vm_bo_update(mapping);
828
829 addr += mapping->offset;
830
831 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
832 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
833 start, mapping->it.last,
834 flags, addr, fence);
835
836 while (start != mapping->it.last + 1) {
837 uint64_t last;
838
839 last = min((uint64_t)mapping->it.last, start + max_size);
840 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
841 start, last, flags, addr,
842 fence);
843 if (r)
844 return r;
845
846 start = last + 1;
847 addr += max_size;
848 }
849
850 return 0;
851}
852
853/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
855 *
856 * @adev: amdgpu_device pointer
857 * @bo_va: requested BO and VM object
858 * @mem: ttm mem
859 *
860 * Fill in the page table entries for @bo_va.
861 * Returns 0 for success, -EINVAL for failure.
862 *
863 * Object have to be reserved and mutex must be locked!
864 */
865int amdgpu_vm_bo_update(struct amdgpu_device *adev,
866 struct amdgpu_bo_va *bo_va,
867 struct ttm_mem_reg *mem)
868{
869 struct amdgpu_vm *vm = bo_va->vm;
870 struct amdgpu_bo_va_mapping *mapping;
Christian König9ab21462015-11-30 14:19:26 +0100871 struct amdgpu_gart *gtt = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400872 uint32_t flags;
873 uint64_t addr;
874 int r;
875
876 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200877 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100878 switch (mem->mem_type) {
879 case TTM_PL_TT:
880 gtt = &bo_va->bo->adev->gart;
881 break;
882
883 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100885 break;
886
887 default:
888 break;
889 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400890 } else {
891 addr = 0;
892 }
893
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
895
Christian König7fc11952015-07-30 11:53:42 +0200896 spin_lock(&vm->status_lock);
897 if (!list_empty(&bo_va->vm_status))
898 list_splice_init(&bo_va->valids, &bo_va->invalids);
899 spin_unlock(&vm->status_lock);
900
901 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa14faa62016-01-25 14:27:31 +0100902 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
903 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400904 if (r)
905 return r;
906 }
907
Christian Königd6c10f62015-09-28 12:00:23 +0200908 if (trace_amdgpu_vm_bo_mapping_enabled()) {
909 list_for_each_entry(mapping, &bo_va->valids, list)
910 trace_amdgpu_vm_bo_mapping(mapping);
911
912 list_for_each_entry(mapping, &bo_va->invalids, list)
913 trace_amdgpu_vm_bo_mapping(mapping);
914 }
915
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400916 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800917 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400918 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200919 if (!mem)
920 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921 spin_unlock(&vm->status_lock);
922
923 return 0;
924}
925
926/**
927 * amdgpu_vm_clear_freed - clear freed BOs in the PT
928 *
929 * @adev: amdgpu_device pointer
930 * @vm: requested vm
931 *
932 * Make sure all freed BOs are cleared in the PT.
933 * Returns 0 for success.
934 *
935 * PTs have to be reserved and mutex must be locked!
936 */
937int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
938 struct amdgpu_vm *vm)
939{
940 struct amdgpu_bo_va_mapping *mapping;
941 int r;
942
jimqu81d75a32015-12-04 17:17:00 +0800943 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400944 while (!list_empty(&vm->freed)) {
945 mapping = list_first_entry(&vm->freed,
946 struct amdgpu_bo_va_mapping, list);
947 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800948 spin_unlock(&vm->freed_lock);
Christian Königa14faa62016-01-25 14:27:31 +0100949 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
950 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951 kfree(mapping);
952 if (r)
953 return r;
954
jimqu81d75a32015-12-04 17:17:00 +0800955 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956 }
jimqu81d75a32015-12-04 17:17:00 +0800957 spin_unlock(&vm->freed_lock);
958
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400959 return 0;
960
961}
962
963/**
964 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
965 *
966 * @adev: amdgpu_device pointer
967 * @vm: requested vm
968 *
969 * Make sure all invalidated BOs are cleared in the PT.
970 * Returns 0 for success.
971 *
972 * PTs have to be reserved and mutex must be locked!
973 */
974int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800975 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976{
monk.liucfe2c972015-05-26 15:01:54 +0800977 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200978 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979
980 spin_lock(&vm->status_lock);
981 while (!list_empty(&vm->invalidated)) {
982 bo_va = list_first_entry(&vm->invalidated,
983 struct amdgpu_bo_va, vm_status);
984 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800985 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800987 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400988 if (r)
989 return r;
990
991 spin_lock(&vm->status_lock);
992 }
993 spin_unlock(&vm->status_lock);
994
monk.liucfe2c972015-05-26 15:01:54 +0800995 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800996 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200997
998 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999}
1000
1001/**
1002 * amdgpu_vm_bo_add - add a bo to a specific vm
1003 *
1004 * @adev: amdgpu_device pointer
1005 * @vm: requested vm
1006 * @bo: amdgpu buffer object
1007 *
1008 * Add @bo into the requested vm (cayman+).
1009 * Add @bo to the list of bos associated with the vm
1010 * Returns newly added bo_va or NULL for failure
1011 *
1012 * Object has to be reserved!
1013 */
1014struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1015 struct amdgpu_vm *vm,
1016 struct amdgpu_bo *bo)
1017{
1018 struct amdgpu_bo_va *bo_va;
1019
1020 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1021 if (bo_va == NULL) {
1022 return NULL;
1023 }
1024 bo_va->vm = vm;
1025 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001026 bo_va->ref_count = 1;
1027 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001028 INIT_LIST_HEAD(&bo_va->valids);
1029 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001030 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001031 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001032 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033
1034 return bo_va;
1035}
1036
1037/**
1038 * amdgpu_vm_bo_map - map bo inside a vm
1039 *
1040 * @adev: amdgpu_device pointer
1041 * @bo_va: bo_va to store the address
1042 * @saddr: where to map the BO
1043 * @offset: requested offset in the BO
1044 * @flags: attributes of pages (read/write/valid/etc.)
1045 *
1046 * Add a mapping of the BO at the specefied addr into the VM.
1047 * Returns 0 for success, error for failure.
1048 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001049 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050 */
1051int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1052 struct amdgpu_bo_va *bo_va,
1053 uint64_t saddr, uint64_t offset,
1054 uint64_t size, uint32_t flags)
1055{
1056 struct amdgpu_bo_va_mapping *mapping;
1057 struct amdgpu_vm *vm = bo_va->vm;
1058 struct interval_tree_node *it;
1059 unsigned last_pfn, pt_idx;
1060 uint64_t eaddr;
1061 int r;
1062
Christian König0be52de2015-05-18 14:37:27 +02001063 /* validate the parameters */
1064 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001065 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001066 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001067
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001069 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001070 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001071 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072
1073 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001074 if (last_pfn >= adev->vm_manager.max_pfn) {
1075 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001077 return -EINVAL;
1078 }
1079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080 saddr /= AMDGPU_GPU_PAGE_SIZE;
1081 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1082
Chunming Zhouc25867d2015-11-13 13:32:01 +08001083 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001084 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001085 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001086 if (it) {
1087 struct amdgpu_bo_va_mapping *tmp;
1088 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1089 /* bo and tmp overlap, invalid addr */
1090 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1091 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1092 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001093 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001094 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001095 }
1096
1097 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1098 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001100 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 }
1102
1103 INIT_LIST_HEAD(&mapping->list);
1104 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001105 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 mapping->offset = offset;
1107 mapping->flags = flags;
1108
Chunming Zhou69b576a2015-11-18 11:17:39 +08001109 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001110 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001111 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001112 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001113 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001114 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001115 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117 /* Make sure the page tables are allocated */
1118 saddr >>= amdgpu_vm_block_size;
1119 eaddr >>= amdgpu_vm_block_size;
1120
1121 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1122
1123 if (eaddr > vm->max_pde_used)
1124 vm->max_pde_used = eaddr;
1125
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001126 /* walk over the address space and allocate the page tables */
1127 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001128 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001129 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 struct amdgpu_bo *pt;
1131
Christian Königee1782c2015-12-11 21:01:23 +01001132 entry = &vm->page_tables[pt_idx].entry;
1133 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134 continue;
1135
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1137 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001138 AMDGPU_GEM_DOMAIN_VRAM,
1139 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001140 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001141 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001142 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001143
Christian König82b9c552015-11-27 16:49:00 +01001144 /* Keep a reference to the page table to avoid freeing
1145 * them up in the wrong order.
1146 */
1147 pt->parent = amdgpu_bo_ref(vm->page_directory);
1148
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149 r = amdgpu_vm_clear_bo(adev, pt);
1150 if (r) {
1151 amdgpu_bo_unref(&pt);
1152 goto error_free;
1153 }
1154
Christian Königee1782c2015-12-11 21:01:23 +01001155 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001156 entry->priority = 0;
1157 entry->tv.bo = &entry->robj->tbo;
1158 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160 }
1161
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162 return 0;
1163
1164error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001165 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001166 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001167 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001168 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001169 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001170 kfree(mapping);
1171
Chunming Zhouf48b2652015-10-16 14:06:19 +08001172error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173 return r;
1174}
1175
1176/**
1177 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1178 *
1179 * @adev: amdgpu_device pointer
1180 * @bo_va: bo_va to remove the address from
1181 * @saddr: where to the BO is mapped
1182 *
1183 * Remove a mapping of the BO at the specefied addr from the VM.
1184 * Returns 0 for success, error for failure.
1185 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001186 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187 */
1188int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1189 struct amdgpu_bo_va *bo_va,
1190 uint64_t saddr)
1191{
1192 struct amdgpu_bo_va_mapping *mapping;
1193 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001194 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195
Christian König6c7fc502015-06-05 20:56:17 +02001196 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001197 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001198 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 if (mapping->it.start == saddr)
1200 break;
1201 }
1202
Christian König7fc11952015-07-30 11:53:42 +02001203 if (&mapping->list == &bo_va->valids) {
1204 valid = false;
1205
1206 list_for_each_entry(mapping, &bo_va->invalids, list) {
1207 if (mapping->it.start == saddr)
1208 break;
1209 }
1210
Chunming Zhou69b576a2015-11-18 11:17:39 +08001211 if (&mapping->list == &bo_va->invalids) {
1212 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001213 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001214 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001216 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001218 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001220 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001221 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222
jimqu81d75a32015-12-04 17:17:00 +08001223 if (valid) {
1224 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001226 spin_unlock(&vm->freed_lock);
1227 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001228 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001229 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230
1231 return 0;
1232}
1233
1234/**
1235 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1236 *
1237 * @adev: amdgpu_device pointer
1238 * @bo_va: requested bo_va
1239 *
1240 * Remove @bo_va->bo from the requested vm (cayman+).
1241 *
1242 * Object have to be reserved!
1243 */
1244void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1245 struct amdgpu_bo_va *bo_va)
1246{
1247 struct amdgpu_bo_va_mapping *mapping, *next;
1248 struct amdgpu_vm *vm = bo_va->vm;
1249
1250 list_del(&bo_va->bo_list);
1251
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 spin_lock(&vm->status_lock);
1253 list_del(&bo_va->vm_status);
1254 spin_unlock(&vm->status_lock);
1255
Christian König7fc11952015-07-30 11:53:42 +02001256 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001258 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001260 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001261 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001262 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001263 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001264 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001265 }
Christian König7fc11952015-07-30 11:53:42 +02001266 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1267 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001268 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001269 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001270 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001271 kfree(mapping);
1272 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001273 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001274 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001275 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276}
1277
1278/**
1279 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1280 *
1281 * @adev: amdgpu_device pointer
1282 * @vm: requested vm
1283 * @bo: amdgpu buffer object
1284 *
1285 * Mark @bo as invalid (cayman+).
1286 */
1287void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1288 struct amdgpu_bo *bo)
1289{
1290 struct amdgpu_bo_va *bo_va;
1291
1292 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001293 spin_lock(&bo_va->vm->status_lock);
1294 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001296 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297 }
1298}
1299
1300/**
1301 * amdgpu_vm_init - initialize a vm instance
1302 *
1303 * @adev: amdgpu_device pointer
1304 * @vm: requested vm
1305 *
1306 * Init @vm fields (cayman+).
1307 */
1308int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1309{
1310 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1311 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001312 unsigned pd_size, pd_entries;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001313 int i, r;
1314
1315 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1316 vm->ids[i].id = 0;
1317 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001319 vm->va = RB_ROOT;
1320 spin_lock_init(&vm->status_lock);
1321 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001322 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001324 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001325 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001326 pd_size = amdgpu_vm_directory_size(adev);
1327 pd_entries = amdgpu_vm_num_pdes(adev);
1328
1329 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001330 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001331 if (vm->page_tables == NULL) {
1332 DRM_ERROR("Cannot allocate memory for page table array\n");
1333 return -ENOMEM;
1334 }
1335
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001336 vm->page_directory_fence = NULL;
1337
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001338 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001339 AMDGPU_GEM_DOMAIN_VRAM,
1340 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001341 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001342 if (r)
1343 return r;
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001344 r = amdgpu_bo_reserve(vm->page_directory, false);
1345 if (r) {
1346 amdgpu_bo_unref(&vm->page_directory);
1347 vm->page_directory = NULL;
1348 return r;
1349 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001350 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001351 amdgpu_bo_unreserve(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001352 if (r) {
1353 amdgpu_bo_unref(&vm->page_directory);
1354 vm->page_directory = NULL;
1355 return r;
1356 }
1357
1358 return 0;
1359}
1360
1361/**
1362 * amdgpu_vm_fini - tear down a vm instance
1363 *
1364 * @adev: amdgpu_device pointer
1365 * @vm: requested vm
1366 *
1367 * Tear down @vm (cayman+).
1368 * Unbind the VM and remove all bos from the vm bo list
1369 */
1370void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1371{
1372 struct amdgpu_bo_va_mapping *mapping, *tmp;
1373 int i;
1374
1375 if (!RB_EMPTY_ROOT(&vm->va)) {
1376 dev_err(adev->dev, "still active bo inside vm\n");
1377 }
1378 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1379 list_del(&mapping->list);
1380 interval_tree_remove(&mapping->it, &vm->va);
1381 kfree(mapping);
1382 }
1383 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1384 list_del(&mapping->list);
1385 kfree(mapping);
1386 }
1387
1388 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001389 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001390 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001391
1392 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001393 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001394 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König1c16c0a2015-11-14 21:31:40 +01001395 unsigned id = vm->ids[i].id;
1396
1397 atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner,
1398 (long)vm, 0);
Chunming Zhou3c623382015-08-20 18:33:59 +08001399 fence_put(vm->ids[i].flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001400 }
1401
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001402}
Christian Königea89f8c2015-11-15 20:52:06 +01001403
1404/**
Christian Königa9a78b32016-01-21 10:19:11 +01001405 * amdgpu_vm_manager_init - init the VM manager
1406 *
1407 * @adev: amdgpu_device pointer
1408 *
1409 * Initialize the VM manager structures
1410 */
1411void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1412{
1413 unsigned i;
1414
1415 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1416
1417 /* skip over VMID 0, since it is the system VM */
1418 for (i = 1; i < adev->vm_manager.num_ids; ++i)
1419 list_add_tail(&adev->vm_manager.ids[i].list,
1420 &adev->vm_manager.ids_lru);
1421}
1422
1423/**
Christian Königea89f8c2015-11-15 20:52:06 +01001424 * amdgpu_vm_manager_fini - cleanup VM manager
1425 *
1426 * @adev: amdgpu_device pointer
1427 *
1428 * Cleanup the VM manager and free resources.
1429 */
1430void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1431{
1432 unsigned i;
1433
1434 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001435 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001436}