blob: d9dc8bea5e98f51496683d7beddede288c5b4290 [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
Christian König4ff37a82016-02-26 16:18:26 +010053/* Special value that no flush is necessary */
54#define AMDGPU_VM_NO_FLUSH (~0ll)
55
Alex Deucherd38ceaf2015-04-20 16:55:21 -040056/**
57 * amdgpu_vm_num_pde - return the number of page directory entries
58 *
59 * @adev: amdgpu_device pointer
60 *
Christian König8843dbb2016-01-26 12:17:11 +010061 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062 */
63static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
64{
65 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
66}
67
68/**
69 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
70 *
71 * @adev: amdgpu_device pointer
72 *
Christian König8843dbb2016-01-26 12:17:11 +010073 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 */
75static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
76{
77 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
78}
79
80/**
Christian König56467eb2015-12-11 15:16:32 +010081 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082 *
83 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +010084 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +010085 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 *
87 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +010088 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 */
Christian König56467eb2015-12-11 15:16:32 +010090void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
91 struct list_head *validated,
92 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093{
Christian König56467eb2015-12-11 15:16:32 +010094 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +010095 entry->priority = 0;
96 entry->tv.bo = &vm->page_directory->tbo;
97 entry->tv.shared = true;
98 list_add(&entry->tv.head, validated);
99}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100
Christian König56467eb2015-12-11 15:16:32 +0100101/**
Christian Königee1782c2015-12-11 21:01:23 +0100102 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100103 *
104 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100105 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 *
Christian Königee1782c2015-12-11 21:01:23 +0100107 * Add the page directory to the BO duplicates list
108 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 */
Christian Königee1782c2015-12-11 21:01:23 +0100110void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111{
Christian Königee1782c2015-12-11 21:01:23 +0100112 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113
114 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100115 for (i = 0; i <= vm->max_pde_used; ++i) {
116 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117
Christian Königee1782c2015-12-11 21:01:23 +0100118 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 continue;
120
Christian Königee1782c2015-12-11 21:01:23 +0100121 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 }
Christian Königeceb8a12016-01-11 15:35:21 +0100123
124}
125
126/**
127 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
128 *
129 * @adev: amdgpu device instance
130 * @vm: vm providing the BOs
131 *
132 * Move the PT BOs to the tail of the LRU.
133 */
134void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
135 struct amdgpu_vm *vm)
136{
137 struct ttm_bo_global *glob = adev->mman.bdev.glob;
138 unsigned i;
139
140 spin_lock(&glob->lru_lock);
141 for (i = 0; i <= vm->max_pde_used; ++i) {
142 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
143
144 if (!entry->robj)
145 continue;
146
147 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
148 }
149 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150}
151
152/**
153 * amdgpu_vm_grab_id - allocate the next free VMID
154 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200156 * @ring: ring we want to submit job to
157 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100158 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 *
Christian König7f8a5292015-07-20 16:09:40 +0200160 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161 */
Christian König7f8a5292015-07-20 16:09:40 +0200162int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100163 struct amdgpu_sync *sync, struct fence *fence,
164 unsigned *vm_id, uint64_t *vm_pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165{
Christian König4ff37a82016-02-26 16:18:26 +0100166 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167 struct amdgpu_device *adev = ring->adev;
Christian König4ff37a82016-02-26 16:18:26 +0100168 struct amdgpu_vm_id *id = &vm->ids[ring->idx];
169 struct fence *updates = sync->last_vm_update;
Christian Königa9a78b32016-01-21 10:19:11 +0100170 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171
Christian König94dd0a42016-01-18 17:01:42 +0100172 mutex_lock(&adev->vm_manager.lock);
173
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174 /* check if the id is still valid */
Christian König4ff37a82016-02-26 16:18:26 +0100175 if (id->mgr_id) {
176 struct fence *flushed = id->flushed_updates;
177 bool is_later;
Christian König1c16c0a2015-11-14 21:31:40 +0100178 long owner;
179
Christian König4ff37a82016-02-26 16:18:26 +0100180 if (!flushed)
181 is_later = true;
182 else if (!updates)
183 is_later = false;
184 else
185 is_later = fence_is_later(updates, flushed);
Christian Königa9a78b32016-01-21 10:19:11 +0100186
Christian König4ff37a82016-02-26 16:18:26 +0100187 owner = atomic_long_read(&id->mgr_id->owner);
188 if (!is_later && owner == (long)id &&
189 pd_addr == id->pd_gpu_addr) {
190
191 fence_put(id->mgr_id->active);
192 id->mgr_id->active = fence_get(fence);
193
194 list_move_tail(&id->mgr_id->list,
195 &adev->vm_manager.ids_lru);
196
197 *vm_id = id->mgr_id - adev->vm_manager.ids;
198 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
Christian König22073fe2016-02-26 16:18:36 +0100199 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id,
200 *vm_pd_addr);
Christian Königa9a78b32016-01-21 10:19:11 +0100201
Christian König94dd0a42016-01-18 17:01:42 +0100202 mutex_unlock(&adev->vm_manager.lock);
Christian König1c16c0a2015-11-14 21:31:40 +0100203 return 0;
204 }
Christian König39ff8442015-09-28 12:01:20 +0200205 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206
Christian König4ff37a82016-02-26 16:18:26 +0100207 id->mgr_id = list_first_entry(&adev->vm_manager.ids_lru,
208 struct amdgpu_vm_manager_id,
209 list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210
Christian König4ff37a82016-02-26 16:18:26 +0100211 r = amdgpu_sync_fence(ring->adev, sync, id->mgr_id->active);
Christian Königa9a78b32016-01-21 10:19:11 +0100212 if (!r) {
Christian König4ff37a82016-02-26 16:18:26 +0100213 fence_put(id->mgr_id->active);
214 id->mgr_id->active = fence_get(fence);
215
216 fence_put(id->flushed_updates);
217 id->flushed_updates = fence_get(updates);
218
219 id->pd_gpu_addr = pd_addr;
220
221 list_move_tail(&id->mgr_id->list, &adev->vm_manager.ids_lru);
222 atomic_long_set(&id->mgr_id->owner, (long)id);
223
224 *vm_id = id->mgr_id - adev->vm_manager.ids;
225 *vm_pd_addr = pd_addr;
Christian König22073fe2016-02-26 16:18:36 +0100226 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400227 }
228
Christian König94dd0a42016-01-18 17:01:42 +0100229 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100230 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231}
232
233/**
234 * amdgpu_vm_flush - hardware flush the vm
235 *
236 * @ring: ring to use for flush
Christian König4ff37a82016-02-26 16:18:26 +0100237 * @vmid: vmid number to use
238 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239 *
Christian König4ff37a82016-02-26 16:18:26 +0100240 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241 */
242void amdgpu_vm_flush(struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100243 unsigned vmid,
244 uint64_t pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400245{
Christian König4ff37a82016-02-26 16:18:26 +0100246 if (pd_addr != AMDGPU_VM_NO_FLUSH) {
247 trace_amdgpu_vm_flush(pd_addr, ring->idx, vmid);
248 amdgpu_ring_emit_vm_flush(ring, vmid, pd_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400249 }
250}
251
252/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400253 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
254 *
255 * @vm: requested vm
256 * @bo: requested buffer object
257 *
Christian König8843dbb2016-01-26 12:17:11 +0100258 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400259 * Search inside the @bos vm list for the requested vm
260 * Returns the found bo_va or NULL if none is found
261 *
262 * Object has to be reserved!
263 */
264struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
265 struct amdgpu_bo *bo)
266{
267 struct amdgpu_bo_va *bo_va;
268
269 list_for_each_entry(bo_va, &bo->va, bo_list) {
270 if (bo_va->vm == vm) {
271 return bo_va;
272 }
273 }
274 return NULL;
275}
276
277/**
278 * amdgpu_vm_update_pages - helper to call the right asic function
279 *
280 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100281 * @gtt: GART instance to use for mapping
282 * @gtt_flags: GTT hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283 * @ib: indirect buffer to fill with commands
284 * @pe: addr of the page entry
285 * @addr: dst addr to write into pe
286 * @count: number of page entries to update
287 * @incr: increase next addr by incr bytes
288 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400289 *
290 * Traces the parameters and calls the right asic functions
291 * to setup the page table using the DMA.
292 */
293static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100294 struct amdgpu_gart *gtt,
295 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 struct amdgpu_ib *ib,
297 uint64_t pe, uint64_t addr,
298 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100299 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400300{
301 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
302
Christian König9ab21462015-11-30 14:19:26 +0100303 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
304 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
306
Christian König9ab21462015-11-30 14:19:26 +0100307 } else if (gtt) {
308 dma_addr_t *pages_addr = gtt->pages_addr;
Christian Königb07c9d22015-11-30 13:26:07 +0100309 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
310 count, incr, flags);
311
312 } else if (count < 3) {
313 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
314 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315
316 } else {
317 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
318 count, incr, flags);
319 }
320}
321
322/**
323 * amdgpu_vm_clear_bo - initially clear the page dir/table
324 *
325 * @adev: amdgpu_device pointer
326 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800327 *
328 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400329 */
330static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100331 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332 struct amdgpu_bo *bo)
333{
Christian König2d55e452016-02-08 17:37:38 +0100334 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800335 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100336 struct amdgpu_job *job;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400337 unsigned entries;
338 uint64_t addr;
339 int r;
340
Christian König2d55e452016-02-08 17:37:38 +0100341 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
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
Christian Königd71518b2016-02-01 12:20:25 +0100354 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
355 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800356 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357
Christian Königd71518b2016-02-01 12:20:25 +0100358 amdgpu_vm_update_pages(adev, NULL, 0, &job->ibs[0], addr, 0, entries,
359 0, 0);
360 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
361
362 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100363 r = amdgpu_job_submit(job, ring, &vm->entity,
364 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400365 if (r)
366 goto error_free;
367
Christian Königd71518b2016-02-01 12:20:25 +0100368 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800369 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800370 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800371
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400372error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100373 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400374
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800375error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376 return r;
377}
378
379/**
Christian Königb07c9d22015-11-30 13:26:07 +0100380 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381 *
Christian Königb07c9d22015-11-30 13:26:07 +0100382 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400383 * @addr: the unmapped addr
384 *
385 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100386 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400387 */
Christian Königb07c9d22015-11-30 13:26:07 +0100388uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389{
390 uint64_t result;
391
Christian Königb07c9d22015-11-30 13:26:07 +0100392 if (pages_addr) {
393 /* page table offset */
394 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400395
Christian Königb07c9d22015-11-30 13:26:07 +0100396 /* in case cpu page size != gpu page size*/
397 result |= addr & (~PAGE_MASK);
398
399 } else {
400 /* No mapping required */
401 result = addr;
402 }
403
404 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405
406 return result;
407}
408
409/**
410 * amdgpu_vm_update_pdes - make sure that page directory is valid
411 *
412 * @adev: amdgpu_device pointer
413 * @vm: requested vm
414 * @start: start of GPU address range
415 * @end: end of GPU address range
416 *
417 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100418 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400420 */
421int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
422 struct amdgpu_vm *vm)
423{
Christian König2d55e452016-02-08 17:37:38 +0100424 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425 struct amdgpu_bo *pd = vm->page_directory;
426 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
427 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
428 uint64_t last_pde = ~0, last_pt = ~0;
429 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100430 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800431 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800432 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800433
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 int r;
435
Christian König2d55e452016-02-08 17:37:38 +0100436 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
437
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400438 /* padding, etc. */
439 ndw = 64;
440
441 /* assume the worst case */
442 ndw += vm->max_pde_used * 6;
443
Christian Königd71518b2016-02-01 12:20:25 +0100444 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
445 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400446 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100447
448 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449
450 /* walk over the address space and update the page directory */
451 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100452 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 uint64_t pde, pt;
454
455 if (bo == NULL)
456 continue;
457
458 pt = amdgpu_bo_gpu_offset(bo);
459 if (vm->page_tables[pt_idx].addr == pt)
460 continue;
461 vm->page_tables[pt_idx].addr = pt;
462
463 pde = pd_addr + pt_idx * 8;
464 if (((last_pde + 8 * count) != pde) ||
465 ((last_pt + incr * count) != pt)) {
466
467 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100468 amdgpu_vm_update_pages(adev, NULL, 0, ib,
469 last_pde, last_pt,
470 count, incr,
471 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472 }
473
474 count = 1;
475 last_pde = pde;
476 last_pt = pt;
477 } else {
478 ++count;
479 }
480 }
481
482 if (count)
Christian König9ab21462015-11-30 14:19:26 +0100483 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
484 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400485
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800486 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100487 amdgpu_ring_pad_ib(ring, ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100488 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
489 AMDGPU_FENCE_OWNER_VM);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800490 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100491 r = amdgpu_job_submit(job, ring, &vm->entity,
492 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800493 if (r)
494 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200495
Chunming Zhou4af9f072015-08-03 12:57:31 +0800496 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200497 fence_put(vm->page_directory_fence);
498 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800499 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800500
Christian Königd71518b2016-02-01 12:20:25 +0100501 } else {
502 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800503 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504
505 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800506
507error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100508 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800509 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400510}
511
512/**
513 * amdgpu_vm_frag_ptes - add fragment information to PTEs
514 *
515 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100516 * @gtt: GART instance to use for mapping
517 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518 * @ib: IB for the update
519 * @pe_start: first PTE to handle
520 * @pe_end: last PTE to handle
521 * @addr: addr those PTEs should point to
522 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 */
524static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100525 struct amdgpu_gart *gtt,
526 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527 struct amdgpu_ib *ib,
528 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100529 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530{
531 /**
532 * The MC L1 TLB supports variable sized pages, based on a fragment
533 * field in the PTE. When this field is set to a non-zero value, page
534 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
535 * flags are considered valid for all PTEs within the fragment range
536 * and corresponding mappings are assumed to be physically contiguous.
537 *
538 * The L1 TLB can store a single PTE for the whole fragment,
539 * significantly increasing the space available for translation
540 * caching. This leads to large improvements in throughput when the
541 * TLB is under pressure.
542 *
543 * The L2 TLB distributes small and large fragments into two
544 * asymmetric partitions. The large fragment cache is significantly
545 * larger. Thus, we try to use large fragments wherever possible.
546 * Userspace can support this by aligning virtual base address and
547 * allocation size to the fragment size.
548 */
549
550 /* SI and newer are optimized for 64KB */
551 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
552 uint64_t frag_align = 0x80;
553
554 uint64_t frag_start = ALIGN(pe_start, frag_align);
555 uint64_t frag_end = pe_end & ~(frag_align - 1);
556
557 unsigned count;
558
Christian König31f6c1f2016-01-26 12:37:49 +0100559 /* Abort early if there isn't anything to do */
560 if (pe_start == pe_end)
561 return;
562
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 /* system pages are non continuously */
Christian König9ab21462015-11-30 14:19:26 +0100564 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400565
566 count = (pe_end - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100567 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
568 addr, count, AMDGPU_GPU_PAGE_SIZE,
569 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 return;
571 }
572
573 /* handle the 4K area at the beginning */
574 if (pe_start != frag_start) {
575 count = (frag_start - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100576 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
577 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578 addr += AMDGPU_GPU_PAGE_SIZE * count;
579 }
580
581 /* handle the area in the middle */
582 count = (frag_end - frag_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100583 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
584 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585
586 /* handle the 4K area at the end */
587 if (frag_end != pe_end) {
588 addr += AMDGPU_GPU_PAGE_SIZE * count;
589 count = (pe_end - frag_end) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100590 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
591 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592 }
593}
594
595/**
596 * amdgpu_vm_update_ptes - make sure that page tables are valid
597 *
598 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100599 * @gtt: GART instance to use for mapping
600 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601 * @vm: requested vm
602 * @start: start of GPU address range
603 * @end: end of GPU address range
604 * @dst: destination address to map to
605 * @flags: mapping flags
606 *
Christian König8843dbb2016-01-26 12:17:11 +0100607 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 */
Christian Königa1e08d32016-01-26 11:40:46 +0100609static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
610 struct amdgpu_gart *gtt,
611 uint32_t gtt_flags,
612 struct amdgpu_vm *vm,
613 struct amdgpu_ib *ib,
614 uint64_t start, uint64_t end,
615 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400616{
Christian König31f6c1f2016-01-26 12:37:49 +0100617 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
618
619 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620 uint64_t addr;
621
622 /* walk over the address space and update the page tables */
623 for (addr = start; addr < end; ) {
624 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100625 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100627 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628
629 if ((addr & ~mask) == (end & ~mask))
630 nptes = end - addr;
631 else
632 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
633
Christian König31f6c1f2016-01-26 12:37:49 +0100634 pe_start = amdgpu_bo_gpu_offset(pt);
635 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636
Christian König31f6c1f2016-01-26 12:37:49 +0100637 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638
Christian König31f6c1f2016-01-26 12:37:49 +0100639 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
640 last_pe_start, last_pe_end,
641 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642
Christian König31f6c1f2016-01-26 12:37:49 +0100643 last_pe_start = pe_start;
644 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 last_dst = dst;
646 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100647 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 }
649
650 addr += nptes;
651 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
652 }
653
Christian König31f6c1f2016-01-26 12:37:49 +0100654 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
655 last_pe_start, last_pe_end,
656 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400657}
658
659/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
661 *
662 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100663 * @gtt: GART instance to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 * @gtt_flags: flags as they are used for GTT
Christian Königa14faa62016-01-25 14:27:31 +0100665 * @vm: requested vm
666 * @start: start of mapped range
667 * @last: last mapped entry
668 * @flags: flags for the entries
669 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400670 * @fence: optional resulting fence
671 *
Christian Königa14faa62016-01-25 14:27:31 +0100672 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400674 */
675static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100676 struct amdgpu_gart *gtt,
677 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100679 uint64_t start, uint64_t last,
680 uint32_t flags, uint64_t addr,
681 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682{
Christian König2d55e452016-02-08 17:37:38 +0100683 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100684 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100686 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800687 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800688 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689 int r;
690
Christian König2d55e452016-02-08 17:37:38 +0100691 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
692
Christian Königa1e08d32016-01-26 11:40:46 +0100693 /* sync to everything on unmapping */
694 if (!(flags & AMDGPU_PTE_VALID))
695 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
696
Christian Königa14faa62016-01-25 14:27:31 +0100697 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698
699 /*
700 * reserve space for one command every (1 << BLOCK_SIZE)
701 * entries or 2k dwords (whatever is smaller)
702 */
703 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
704
705 /* padding, etc. */
706 ndw = 64;
707
Christian König9ab21462015-11-30 14:19:26 +0100708 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 /* only copy commands needed */
710 ndw += ncmds * 7;
711
Christian König9ab21462015-11-30 14:19:26 +0100712 } else if (gtt) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 /* header for write data commands */
714 ndw += ncmds * 4;
715
716 /* body of write data command */
717 ndw += nptes * 2;
718
719 } else {
720 /* set page commands needed */
721 ndw += ncmds * 10;
722
723 /* two extra commands for begin/end of fragment */
724 ndw += 2 * 10;
725 }
726
Christian Königd71518b2016-02-01 12:20:25 +0100727 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
728 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100730
731 ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800732
Christian Könige86f9ce2016-02-08 12:13:05 +0100733 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100734 owner);
735 if (r)
736 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737
Christian Königa1e08d32016-01-26 11:40:46 +0100738 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
739 if (r)
740 goto error_free;
741
742 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
743 addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744
Christian König9e5d53092016-01-31 12:20:55 +0100745 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800746 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100747 r = amdgpu_job_submit(job, ring, &vm->entity,
748 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800749 if (r)
750 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751
Christian Königbf60efd2015-09-04 10:47:56 +0200752 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800753 if (fence) {
754 fence_put(*fence);
755 *fence = fence_get(f);
756 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800757 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800759
760error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100761 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800762 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763}
764
765/**
Christian Königa14faa62016-01-25 14:27:31 +0100766 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
767 *
768 * @adev: amdgpu_device pointer
769 * @gtt: GART instance to use for mapping
770 * @vm: requested vm
771 * @mapping: mapped range and flags to use for the update
772 * @addr: addr to set the area to
773 * @gtt_flags: flags as they are used for GTT
774 * @fence: optional resulting fence
775 *
776 * Split the mapping into smaller chunks so that each update fits
777 * into a SDMA IB.
778 * Returns 0 for success, -EINVAL for failure.
779 */
780static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
781 struct amdgpu_gart *gtt,
782 uint32_t gtt_flags,
783 struct amdgpu_vm *vm,
784 struct amdgpu_bo_va_mapping *mapping,
785 uint64_t addr, struct fence **fence)
786{
787 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
788
789 uint64_t start = mapping->it.start;
790 uint32_t flags = gtt_flags;
791 int r;
792
793 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
794 * but in case of something, we filter the flags in first place
795 */
796 if (!(mapping->flags & AMDGPU_PTE_READABLE))
797 flags &= ~AMDGPU_PTE_READABLE;
798 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
799 flags &= ~AMDGPU_PTE_WRITEABLE;
800
801 trace_amdgpu_vm_bo_update(mapping);
802
803 addr += mapping->offset;
804
805 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
806 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
807 start, mapping->it.last,
808 flags, addr, fence);
809
810 while (start != mapping->it.last + 1) {
811 uint64_t last;
812
813 last = min((uint64_t)mapping->it.last, start + max_size);
814 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
815 start, last, flags, addr,
816 fence);
817 if (r)
818 return r;
819
820 start = last + 1;
821 addr += max_size;
822 }
823
824 return 0;
825}
826
827/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400828 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
829 *
830 * @adev: amdgpu_device pointer
831 * @bo_va: requested BO and VM object
832 * @mem: ttm mem
833 *
834 * Fill in the page table entries for @bo_va.
835 * Returns 0 for success, -EINVAL for failure.
836 *
837 * Object have to be reserved and mutex must be locked!
838 */
839int amdgpu_vm_bo_update(struct amdgpu_device *adev,
840 struct amdgpu_bo_va *bo_va,
841 struct ttm_mem_reg *mem)
842{
843 struct amdgpu_vm *vm = bo_va->vm;
844 struct amdgpu_bo_va_mapping *mapping;
Christian König9ab21462015-11-30 14:19:26 +0100845 struct amdgpu_gart *gtt = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 uint32_t flags;
847 uint64_t addr;
848 int r;
849
850 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200851 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100852 switch (mem->mem_type) {
853 case TTM_PL_TT:
854 gtt = &bo_va->bo->adev->gart;
855 break;
856
857 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400858 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100859 break;
860
861 default:
862 break;
863 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400864 } else {
865 addr = 0;
866 }
867
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400868 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
869
Christian König7fc11952015-07-30 11:53:42 +0200870 spin_lock(&vm->status_lock);
871 if (!list_empty(&bo_va->vm_status))
872 list_splice_init(&bo_va->valids, &bo_va->invalids);
873 spin_unlock(&vm->status_lock);
874
875 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa14faa62016-01-25 14:27:31 +0100876 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
877 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878 if (r)
879 return r;
880 }
881
Christian Königd6c10f62015-09-28 12:00:23 +0200882 if (trace_amdgpu_vm_bo_mapping_enabled()) {
883 list_for_each_entry(mapping, &bo_va->valids, list)
884 trace_amdgpu_vm_bo_mapping(mapping);
885
886 list_for_each_entry(mapping, &bo_va->invalids, list)
887 trace_amdgpu_vm_bo_mapping(mapping);
888 }
889
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400890 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800891 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200893 if (!mem)
894 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895 spin_unlock(&vm->status_lock);
896
897 return 0;
898}
899
900/**
901 * amdgpu_vm_clear_freed - clear freed BOs in the PT
902 *
903 * @adev: amdgpu_device pointer
904 * @vm: requested vm
905 *
906 * Make sure all freed BOs are cleared in the PT.
907 * Returns 0 for success.
908 *
909 * PTs have to be reserved and mutex must be locked!
910 */
911int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
912 struct amdgpu_vm *vm)
913{
914 struct amdgpu_bo_va_mapping *mapping;
915 int r;
916
jimqu81d75a32015-12-04 17:17:00 +0800917 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400918 while (!list_empty(&vm->freed)) {
919 mapping = list_first_entry(&vm->freed,
920 struct amdgpu_bo_va_mapping, list);
921 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800922 spin_unlock(&vm->freed_lock);
Christian Königa14faa62016-01-25 14:27:31 +0100923 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
924 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925 kfree(mapping);
926 if (r)
927 return r;
928
jimqu81d75a32015-12-04 17:17:00 +0800929 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400930 }
jimqu81d75a32015-12-04 17:17:00 +0800931 spin_unlock(&vm->freed_lock);
932
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933 return 0;
934
935}
936
937/**
938 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
939 *
940 * @adev: amdgpu_device pointer
941 * @vm: requested vm
942 *
943 * Make sure all invalidated BOs are cleared in the PT.
944 * Returns 0 for success.
945 *
946 * PTs have to be reserved and mutex must be locked!
947 */
948int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800949 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950{
monk.liucfe2c972015-05-26 15:01:54 +0800951 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200952 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953
954 spin_lock(&vm->status_lock);
955 while (!list_empty(&vm->invalidated)) {
956 bo_va = list_first_entry(&vm->invalidated,
957 struct amdgpu_bo_va, vm_status);
958 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800959 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800961 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962 if (r)
963 return r;
964
965 spin_lock(&vm->status_lock);
966 }
967 spin_unlock(&vm->status_lock);
968
monk.liucfe2c972015-05-26 15:01:54 +0800969 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800970 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200971
972 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973}
974
975/**
976 * amdgpu_vm_bo_add - add a bo to a specific vm
977 *
978 * @adev: amdgpu_device pointer
979 * @vm: requested vm
980 * @bo: amdgpu buffer object
981 *
Christian König8843dbb2016-01-26 12:17:11 +0100982 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400983 * Add @bo to the list of bos associated with the vm
984 * Returns newly added bo_va or NULL for failure
985 *
986 * Object has to be reserved!
987 */
988struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
989 struct amdgpu_vm *vm,
990 struct amdgpu_bo *bo)
991{
992 struct amdgpu_bo_va *bo_va;
993
994 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
995 if (bo_va == NULL) {
996 return NULL;
997 }
998 bo_va->vm = vm;
999 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000 bo_va->ref_count = 1;
1001 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001002 INIT_LIST_HEAD(&bo_va->valids);
1003 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001004 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001005 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007
1008 return bo_va;
1009}
1010
1011/**
1012 * amdgpu_vm_bo_map - map bo inside a vm
1013 *
1014 * @adev: amdgpu_device pointer
1015 * @bo_va: bo_va to store the address
1016 * @saddr: where to map the BO
1017 * @offset: requested offset in the BO
1018 * @flags: attributes of pages (read/write/valid/etc.)
1019 *
1020 * Add a mapping of the BO at the specefied addr into the VM.
1021 * Returns 0 for success, error for failure.
1022 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001023 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024 */
1025int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1026 struct amdgpu_bo_va *bo_va,
1027 uint64_t saddr, uint64_t offset,
1028 uint64_t size, uint32_t flags)
1029{
1030 struct amdgpu_bo_va_mapping *mapping;
1031 struct amdgpu_vm *vm = bo_va->vm;
1032 struct interval_tree_node *it;
1033 unsigned last_pfn, pt_idx;
1034 uint64_t eaddr;
1035 int r;
1036
Christian König0be52de2015-05-18 14:37:27 +02001037 /* validate the parameters */
1038 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001039 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001040 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001041
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001042 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001043 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001044 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046
1047 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001048 if (last_pfn >= adev->vm_manager.max_pfn) {
1049 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001051 return -EINVAL;
1052 }
1053
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001054 saddr /= AMDGPU_GPU_PAGE_SIZE;
1055 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1056
Chunming Zhouc25867d2015-11-13 13:32:01 +08001057 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001058 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001059 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001060 if (it) {
1061 struct amdgpu_bo_va_mapping *tmp;
1062 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1063 /* bo and tmp overlap, invalid addr */
1064 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1065 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1066 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001067 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001068 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001069 }
1070
1071 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1072 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001074 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001075 }
1076
1077 INIT_LIST_HEAD(&mapping->list);
1078 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001079 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080 mapping->offset = offset;
1081 mapping->flags = flags;
1082
Chunming Zhou69b576a2015-11-18 11:17:39 +08001083 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001084 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001085 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001086 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001088 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001089 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091 /* Make sure the page tables are allocated */
1092 saddr >>= amdgpu_vm_block_size;
1093 eaddr >>= amdgpu_vm_block_size;
1094
1095 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1096
1097 if (eaddr > vm->max_pde_used)
1098 vm->max_pde_used = eaddr;
1099
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 /* walk over the address space and allocate the page tables */
1101 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001102 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001103 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001104 struct amdgpu_bo *pt;
1105
Christian Königee1782c2015-12-11 21:01:23 +01001106 entry = &vm->page_tables[pt_idx].entry;
1107 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001108 continue;
1109
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001110 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1111 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001112 AMDGPU_GEM_DOMAIN_VRAM,
1113 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001114 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001115 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001117
Christian König82b9c552015-11-27 16:49:00 +01001118 /* Keep a reference to the page table to avoid freeing
1119 * them up in the wrong order.
1120 */
1121 pt->parent = amdgpu_bo_ref(vm->page_directory);
1122
Christian König2bd9ccf2016-02-01 12:53:58 +01001123 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124 if (r) {
1125 amdgpu_bo_unref(&pt);
1126 goto error_free;
1127 }
1128
Christian Königee1782c2015-12-11 21:01:23 +01001129 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001130 entry->priority = 0;
1131 entry->tv.bo = &entry->robj->tbo;
1132 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134 }
1135
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 return 0;
1137
1138error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001139 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001140 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001141 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001142 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001143 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144 kfree(mapping);
1145
Chunming Zhouf48b2652015-10-16 14:06:19 +08001146error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001147 return r;
1148}
1149
1150/**
1151 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1152 *
1153 * @adev: amdgpu_device pointer
1154 * @bo_va: bo_va to remove the address from
1155 * @saddr: where to the BO is mapped
1156 *
1157 * Remove a mapping of the BO at the specefied addr from the VM.
1158 * Returns 0 for success, error for failure.
1159 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001160 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001161 */
1162int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1163 struct amdgpu_bo_va *bo_va,
1164 uint64_t saddr)
1165{
1166 struct amdgpu_bo_va_mapping *mapping;
1167 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001168 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001169
Christian König6c7fc502015-06-05 20:56:17 +02001170 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001171 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001172 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173 if (mapping->it.start == saddr)
1174 break;
1175 }
1176
Christian König7fc11952015-07-30 11:53:42 +02001177 if (&mapping->list == &bo_va->valids) {
1178 valid = false;
1179
1180 list_for_each_entry(mapping, &bo_va->invalids, list) {
1181 if (mapping->it.start == saddr)
1182 break;
1183 }
1184
Chunming Zhou69b576a2015-11-18 11:17:39 +08001185 if (&mapping->list == &bo_va->invalids) {
1186 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001187 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001188 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001189 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001190 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001191 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001192 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001193 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001194 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001195 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196
jimqu81d75a32015-12-04 17:17:00 +08001197 if (valid) {
1198 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001200 spin_unlock(&vm->freed_lock);
1201 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001202 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001203 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001204
1205 return 0;
1206}
1207
1208/**
1209 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1210 *
1211 * @adev: amdgpu_device pointer
1212 * @bo_va: requested bo_va
1213 *
Christian König8843dbb2016-01-26 12:17:11 +01001214 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215 *
1216 * Object have to be reserved!
1217 */
1218void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1219 struct amdgpu_bo_va *bo_va)
1220{
1221 struct amdgpu_bo_va_mapping *mapping, *next;
1222 struct amdgpu_vm *vm = bo_va->vm;
1223
1224 list_del(&bo_va->bo_list);
1225
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226 spin_lock(&vm->status_lock);
1227 list_del(&bo_va->vm_status);
1228 spin_unlock(&vm->status_lock);
1229
Christian König7fc11952015-07-30 11:53:42 +02001230 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001232 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001234 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001235 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001236 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001237 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001238 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 }
Christian König7fc11952015-07-30 11:53:42 +02001240 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1241 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001242 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001243 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001244 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001245 kfree(mapping);
1246 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001247 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001248 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001249 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250}
1251
1252/**
1253 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1254 *
1255 * @adev: amdgpu_device pointer
1256 * @vm: requested vm
1257 * @bo: amdgpu buffer object
1258 *
Christian König8843dbb2016-01-26 12:17:11 +01001259 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260 */
1261void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1262 struct amdgpu_bo *bo)
1263{
1264 struct amdgpu_bo_va *bo_va;
1265
1266 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001267 spin_lock(&bo_va->vm->status_lock);
1268 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001269 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001270 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001271 }
1272}
1273
1274/**
1275 * amdgpu_vm_init - initialize a vm instance
1276 *
1277 * @adev: amdgpu_device pointer
1278 * @vm: requested vm
1279 *
Christian König8843dbb2016-01-26 12:17:11 +01001280 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281 */
1282int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1283{
1284 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1285 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001286 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001287 unsigned ring_instance;
1288 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001289 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 int i, r;
1291
1292 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001293 vm->ids[i].mgr_id = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001294 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 vm->va = RB_ROOT;
1297 spin_lock_init(&vm->status_lock);
1298 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001299 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001300 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001301 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001302 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 pd_size = amdgpu_vm_directory_size(adev);
1304 pd_entries = amdgpu_vm_num_pdes(adev);
1305
1306 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001307 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308 if (vm->page_tables == NULL) {
1309 DRM_ERROR("Cannot allocate memory for page table array\n");
1310 return -ENOMEM;
1311 }
1312
Christian König2bd9ccf2016-02-01 12:53:58 +01001313 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001314
1315 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1316 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1317 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001318 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1319 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1320 rq, amdgpu_sched_jobs);
1321 if (r)
1322 return r;
1323
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001324 vm->page_directory_fence = NULL;
1325
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001326 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001327 AMDGPU_GEM_DOMAIN_VRAM,
1328 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001329 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001330 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001331 goto error_free_sched_entity;
1332
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001333 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001334 if (r)
1335 goto error_free_page_directory;
1336
1337 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001338 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001339 if (r)
1340 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341
1342 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001343
1344error_free_page_directory:
1345 amdgpu_bo_unref(&vm->page_directory);
1346 vm->page_directory = NULL;
1347
1348error_free_sched_entity:
1349 amd_sched_entity_fini(&ring->sched, &vm->entity);
1350
1351 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001352}
1353
1354/**
1355 * amdgpu_vm_fini - tear down a vm instance
1356 *
1357 * @adev: amdgpu_device pointer
1358 * @vm: requested vm
1359 *
Christian König8843dbb2016-01-26 12:17:11 +01001360 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001361 * Unbind the VM and remove all bos from the vm bo list
1362 */
1363void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1364{
1365 struct amdgpu_bo_va_mapping *mapping, *tmp;
1366 int i;
1367
Christian König2d55e452016-02-08 17:37:38 +01001368 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001369
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001370 if (!RB_EMPTY_ROOT(&vm->va)) {
1371 dev_err(adev->dev, "still active bo inside vm\n");
1372 }
1373 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1374 list_del(&mapping->list);
1375 interval_tree_remove(&mapping->it, &vm->va);
1376 kfree(mapping);
1377 }
1378 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1379 list_del(&mapping->list);
1380 kfree(mapping);
1381 }
1382
1383 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001384 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001385 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386
1387 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001388 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001389 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001390 struct amdgpu_vm_id *id = &vm->ids[i];
Christian König1c16c0a2015-11-14 21:31:40 +01001391
Christian König4ff37a82016-02-26 16:18:26 +01001392 if (id->mgr_id)
1393 atomic_long_cmpxchg(&id->mgr_id->owner,
1394 (long)id, 0);
1395 fence_put(id->flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397}
Christian Königea89f8c2015-11-15 20:52:06 +01001398
1399/**
Christian Königa9a78b32016-01-21 10:19:11 +01001400 * amdgpu_vm_manager_init - init the VM manager
1401 *
1402 * @adev: amdgpu_device pointer
1403 *
1404 * Initialize the VM manager structures
1405 */
1406void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1407{
1408 unsigned i;
1409
1410 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1411
1412 /* skip over VMID 0, since it is the system VM */
1413 for (i = 1; i < adev->vm_manager.num_ids; ++i)
1414 list_add_tail(&adev->vm_manager.ids[i].list,
1415 &adev->vm_manager.ids_lru);
Christian König2d55e452016-02-08 17:37:38 +01001416
1417 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001418}
1419
1420/**
Christian Königea89f8c2015-11-15 20:52:06 +01001421 * amdgpu_vm_manager_fini - cleanup VM manager
1422 *
1423 * @adev: amdgpu_device pointer
1424 *
1425 * Cleanup the VM manager and free resources.
1426 */
1427void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1428{
1429 unsigned i;
1430
1431 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001432 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001433}