blob: ba909245fef5027d052365fb8356669262a543e6 [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;
199 trace_amdgpu_vm_grab_id(vm, *vm_id, ring->idx);
Christian Königa9a78b32016-01-21 10:19:11 +0100200
Christian König94dd0a42016-01-18 17:01:42 +0100201 mutex_unlock(&adev->vm_manager.lock);
Christian König1c16c0a2015-11-14 21:31:40 +0100202 return 0;
203 }
Christian König39ff8442015-09-28 12:01:20 +0200204 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400205
Christian König4ff37a82016-02-26 16:18:26 +0100206 id->mgr_id = list_first_entry(&adev->vm_manager.ids_lru,
207 struct amdgpu_vm_manager_id,
208 list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400209
Christian König4ff37a82016-02-26 16:18:26 +0100210 r = amdgpu_sync_fence(ring->adev, sync, id->mgr_id->active);
Christian Königa9a78b32016-01-21 10:19:11 +0100211 if (!r) {
Christian König4ff37a82016-02-26 16:18:26 +0100212 fence_put(id->mgr_id->active);
213 id->mgr_id->active = fence_get(fence);
214
215 fence_put(id->flushed_updates);
216 id->flushed_updates = fence_get(updates);
217
218 id->pd_gpu_addr = pd_addr;
219
220 list_move_tail(&id->mgr_id->list, &adev->vm_manager.ids_lru);
221 atomic_long_set(&id->mgr_id->owner, (long)id);
222
223 *vm_id = id->mgr_id - adev->vm_manager.ids;
224 *vm_pd_addr = pd_addr;
225 trace_amdgpu_vm_grab_id(vm, *vm_id, ring->idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226 }
227
Christian König94dd0a42016-01-18 17:01:42 +0100228 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100229 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400230}
231
232/**
233 * amdgpu_vm_flush - hardware flush the vm
234 *
235 * @ring: ring to use for flush
Christian König4ff37a82016-02-26 16:18:26 +0100236 * @vmid: vmid number to use
237 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238 *
Christian König4ff37a82016-02-26 16:18:26 +0100239 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 */
241void amdgpu_vm_flush(struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100242 unsigned vmid,
243 uint64_t pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400244{
Christian König4ff37a82016-02-26 16:18:26 +0100245 if (pd_addr != AMDGPU_VM_NO_FLUSH) {
246 trace_amdgpu_vm_flush(pd_addr, ring->idx, vmid);
247 amdgpu_ring_emit_vm_flush(ring, vmid, pd_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400248 }
249}
250
251/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400252 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
253 *
254 * @vm: requested vm
255 * @bo: requested buffer object
256 *
Christian König8843dbb2016-01-26 12:17:11 +0100257 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400258 * Search inside the @bos vm list for the requested vm
259 * Returns the found bo_va or NULL if none is found
260 *
261 * Object has to be reserved!
262 */
263struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
264 struct amdgpu_bo *bo)
265{
266 struct amdgpu_bo_va *bo_va;
267
268 list_for_each_entry(bo_va, &bo->va, bo_list) {
269 if (bo_va->vm == vm) {
270 return bo_va;
271 }
272 }
273 return NULL;
274}
275
276/**
277 * amdgpu_vm_update_pages - helper to call the right asic function
278 *
279 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100280 * @gtt: GART instance to use for mapping
281 * @gtt_flags: GTT hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282 * @ib: indirect buffer to fill with commands
283 * @pe: addr of the page entry
284 * @addr: dst addr to write into pe
285 * @count: number of page entries to update
286 * @incr: increase next addr by incr bytes
287 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400288 *
289 * Traces the parameters and calls the right asic functions
290 * to setup the page table using the DMA.
291 */
292static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100293 struct amdgpu_gart *gtt,
294 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400295 struct amdgpu_ib *ib,
296 uint64_t pe, uint64_t addr,
297 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100298 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299{
300 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
301
Christian König9ab21462015-11-30 14:19:26 +0100302 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
303 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400304 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
305
Christian König9ab21462015-11-30 14:19:26 +0100306 } else if (gtt) {
307 dma_addr_t *pages_addr = gtt->pages_addr;
Christian Königb07c9d22015-11-30 13:26:07 +0100308 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
309 count, incr, flags);
310
311 } else if (count < 3) {
312 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
313 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400314
315 } else {
316 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
317 count, incr, flags);
318 }
319}
320
321/**
322 * amdgpu_vm_clear_bo - initially clear the page dir/table
323 *
324 * @adev: amdgpu_device pointer
325 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800326 *
327 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400328 */
329static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100330 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331 struct amdgpu_bo *bo)
332{
Christian König2d55e452016-02-08 17:37:38 +0100333 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800334 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100335 struct amdgpu_job *job;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400336 unsigned entries;
337 uint64_t addr;
338 int r;
339
Christian König2d55e452016-02-08 17:37:38 +0100340 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
341
monk.liuca952612015-05-25 14:44:05 +0800342 r = reservation_object_reserve_shared(bo->tbo.resv);
343 if (r)
344 return r;
345
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
347 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800348 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400349
350 addr = amdgpu_bo_gpu_offset(bo);
351 entries = amdgpu_bo_size(bo) / 8;
352
Christian Königd71518b2016-02-01 12:20:25 +0100353 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
354 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800355 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356
Christian Königd71518b2016-02-01 12:20:25 +0100357 amdgpu_vm_update_pages(adev, NULL, 0, &job->ibs[0], addr, 0, entries,
358 0, 0);
359 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
360
361 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100362 r = amdgpu_job_submit(job, ring, &vm->entity,
363 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400364 if (r)
365 goto error_free;
366
Christian Königd71518b2016-02-01 12:20:25 +0100367 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800368 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800369 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800370
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400371error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100372 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400373
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800374error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400375 return r;
376}
377
378/**
Christian Königb07c9d22015-11-30 13:26:07 +0100379 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380 *
Christian Königb07c9d22015-11-30 13:26:07 +0100381 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382 * @addr: the unmapped addr
383 *
384 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100385 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400386 */
Christian Königb07c9d22015-11-30 13:26:07 +0100387uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388{
389 uint64_t result;
390
Christian Königb07c9d22015-11-30 13:26:07 +0100391 if (pages_addr) {
392 /* page table offset */
393 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400394
Christian Königb07c9d22015-11-30 13:26:07 +0100395 /* in case cpu page size != gpu page size*/
396 result |= addr & (~PAGE_MASK);
397
398 } else {
399 /* No mapping required */
400 result = addr;
401 }
402
403 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400404
405 return result;
406}
407
408/**
409 * amdgpu_vm_update_pdes - make sure that page directory is valid
410 *
411 * @adev: amdgpu_device pointer
412 * @vm: requested vm
413 * @start: start of GPU address range
414 * @end: end of GPU address range
415 *
416 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100417 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400418 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419 */
420int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
421 struct amdgpu_vm *vm)
422{
Christian König2d55e452016-02-08 17:37:38 +0100423 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424 struct amdgpu_bo *pd = vm->page_directory;
425 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
426 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
427 uint64_t last_pde = ~0, last_pt = ~0;
428 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100429 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800430 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800431 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800432
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400433 int r;
434
Christian König2d55e452016-02-08 17:37:38 +0100435 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
436
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437 /* padding, etc. */
438 ndw = 64;
439
440 /* assume the worst case */
441 ndw += vm->max_pde_used * 6;
442
Christian Königd71518b2016-02-01 12:20:25 +0100443 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
444 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100446
447 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448
449 /* walk over the address space and update the page directory */
450 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100451 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452 uint64_t pde, pt;
453
454 if (bo == NULL)
455 continue;
456
457 pt = amdgpu_bo_gpu_offset(bo);
458 if (vm->page_tables[pt_idx].addr == pt)
459 continue;
460 vm->page_tables[pt_idx].addr = pt;
461
462 pde = pd_addr + pt_idx * 8;
463 if (((last_pde + 8 * count) != pde) ||
464 ((last_pt + incr * count) != pt)) {
465
466 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100467 amdgpu_vm_update_pages(adev, NULL, 0, ib,
468 last_pde, last_pt,
469 count, incr,
470 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471 }
472
473 count = 1;
474 last_pde = pde;
475 last_pt = pt;
476 } else {
477 ++count;
478 }
479 }
480
481 if (count)
Christian König9ab21462015-11-30 14:19:26 +0100482 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
483 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400484
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800485 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100486 amdgpu_ring_pad_ib(ring, ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100487 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
488 AMDGPU_FENCE_OWNER_VM);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800489 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100490 r = amdgpu_job_submit(job, ring, &vm->entity,
491 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800492 if (r)
493 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200494
Chunming Zhou4af9f072015-08-03 12:57:31 +0800495 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200496 fence_put(vm->page_directory_fence);
497 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800498 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800499
Christian Königd71518b2016-02-01 12:20:25 +0100500 } else {
501 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800502 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503
504 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800505
506error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100507 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800508 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509}
510
511/**
512 * amdgpu_vm_frag_ptes - add fragment information to PTEs
513 *
514 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100515 * @gtt: GART instance to use for mapping
516 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517 * @ib: IB for the update
518 * @pe_start: first PTE to handle
519 * @pe_end: last PTE to handle
520 * @addr: addr those PTEs should point to
521 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 */
523static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100524 struct amdgpu_gart *gtt,
525 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526 struct amdgpu_ib *ib,
527 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100528 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529{
530 /**
531 * The MC L1 TLB supports variable sized pages, based on a fragment
532 * field in the PTE. When this field is set to a non-zero value, page
533 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
534 * flags are considered valid for all PTEs within the fragment range
535 * and corresponding mappings are assumed to be physically contiguous.
536 *
537 * The L1 TLB can store a single PTE for the whole fragment,
538 * significantly increasing the space available for translation
539 * caching. This leads to large improvements in throughput when the
540 * TLB is under pressure.
541 *
542 * The L2 TLB distributes small and large fragments into two
543 * asymmetric partitions. The large fragment cache is significantly
544 * larger. Thus, we try to use large fragments wherever possible.
545 * Userspace can support this by aligning virtual base address and
546 * allocation size to the fragment size.
547 */
548
549 /* SI and newer are optimized for 64KB */
550 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
551 uint64_t frag_align = 0x80;
552
553 uint64_t frag_start = ALIGN(pe_start, frag_align);
554 uint64_t frag_end = pe_end & ~(frag_align - 1);
555
556 unsigned count;
557
Christian König31f6c1f2016-01-26 12:37:49 +0100558 /* Abort early if there isn't anything to do */
559 if (pe_start == pe_end)
560 return;
561
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 /* system pages are non continuously */
Christian König9ab21462015-11-30 14:19:26 +0100563 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564
565 count = (pe_end - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100566 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
567 addr, count, AMDGPU_GPU_PAGE_SIZE,
568 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569 return;
570 }
571
572 /* handle the 4K area at the beginning */
573 if (pe_start != frag_start) {
574 count = (frag_start - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100575 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
576 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 addr += AMDGPU_GPU_PAGE_SIZE * count;
578 }
579
580 /* handle the area in the middle */
581 count = (frag_end - frag_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100582 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
583 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584
585 /* handle the 4K area at the end */
586 if (frag_end != pe_end) {
587 addr += AMDGPU_GPU_PAGE_SIZE * count;
588 count = (pe_end - frag_end) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100589 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
590 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 }
592}
593
594/**
595 * amdgpu_vm_update_ptes - make sure that page tables are valid
596 *
597 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100598 * @gtt: GART instance to use for mapping
599 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600 * @vm: requested vm
601 * @start: start of GPU address range
602 * @end: end of GPU address range
603 * @dst: destination address to map to
604 * @flags: mapping flags
605 *
Christian König8843dbb2016-01-26 12:17:11 +0100606 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400607 */
Christian Königa1e08d32016-01-26 11:40:46 +0100608static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
609 struct amdgpu_gart *gtt,
610 uint32_t gtt_flags,
611 struct amdgpu_vm *vm,
612 struct amdgpu_ib *ib,
613 uint64_t start, uint64_t end,
614 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615{
Christian König31f6c1f2016-01-26 12:37:49 +0100616 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
617
618 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 uint64_t addr;
620
621 /* walk over the address space and update the page tables */
622 for (addr = start; addr < end; ) {
623 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100624 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100626 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627
628 if ((addr & ~mask) == (end & ~mask))
629 nptes = end - addr;
630 else
631 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
632
Christian König31f6c1f2016-01-26 12:37:49 +0100633 pe_start = amdgpu_bo_gpu_offset(pt);
634 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635
Christian König31f6c1f2016-01-26 12:37:49 +0100636 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637
Christian König31f6c1f2016-01-26 12:37:49 +0100638 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
639 last_pe_start, last_pe_end,
640 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641
Christian König31f6c1f2016-01-26 12:37:49 +0100642 last_pe_start = pe_start;
643 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 last_dst = dst;
645 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100646 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 }
648
649 addr += nptes;
650 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
651 }
652
Christian König31f6c1f2016-01-26 12:37:49 +0100653 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
654 last_pe_start, last_pe_end,
655 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656}
657
658/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
660 *
661 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100662 * @gtt: GART instance to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400663 * @gtt_flags: flags as they are used for GTT
Christian Königa14faa62016-01-25 14:27:31 +0100664 * @vm: requested vm
665 * @start: start of mapped range
666 * @last: last mapped entry
667 * @flags: flags for the entries
668 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400669 * @fence: optional resulting fence
670 *
Christian Königa14faa62016-01-25 14:27:31 +0100671 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 */
674static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100675 struct amdgpu_gart *gtt,
676 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100678 uint64_t start, uint64_t last,
679 uint32_t flags, uint64_t addr,
680 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681{
Christian König2d55e452016-02-08 17:37:38 +0100682 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100683 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100685 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800686 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800687 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688 int r;
689
Christian König2d55e452016-02-08 17:37:38 +0100690 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
691
Christian Königa1e08d32016-01-26 11:40:46 +0100692 /* sync to everything on unmapping */
693 if (!(flags & AMDGPU_PTE_VALID))
694 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
695
Christian Königa14faa62016-01-25 14:27:31 +0100696 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697
698 /*
699 * reserve space for one command every (1 << BLOCK_SIZE)
700 * entries or 2k dwords (whatever is smaller)
701 */
702 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
703
704 /* padding, etc. */
705 ndw = 64;
706
Christian König9ab21462015-11-30 14:19:26 +0100707 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708 /* only copy commands needed */
709 ndw += ncmds * 7;
710
Christian König9ab21462015-11-30 14:19:26 +0100711 } else if (gtt) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712 /* header for write data commands */
713 ndw += ncmds * 4;
714
715 /* body of write data command */
716 ndw += nptes * 2;
717
718 } else {
719 /* set page commands needed */
720 ndw += ncmds * 10;
721
722 /* two extra commands for begin/end of fragment */
723 ndw += 2 * 10;
724 }
725
Christian Königd71518b2016-02-01 12:20:25 +0100726 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
727 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100729
730 ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800731
Christian Könige86f9ce2016-02-08 12:13:05 +0100732 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100733 owner);
734 if (r)
735 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400736
Christian Königa1e08d32016-01-26 11:40:46 +0100737 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
738 if (r)
739 goto error_free;
740
741 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
742 addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400743
Christian König9e5d53092016-01-31 12:20:55 +0100744 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800745 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100746 r = amdgpu_job_submit(job, ring, &vm->entity,
747 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800748 if (r)
749 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400750
Christian Königbf60efd2015-09-04 10:47:56 +0200751 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800752 if (fence) {
753 fence_put(*fence);
754 *fence = fence_get(f);
755 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800756 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800758
759error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100760 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800761 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762}
763
764/**
Christian Königa14faa62016-01-25 14:27:31 +0100765 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
766 *
767 * @adev: amdgpu_device pointer
768 * @gtt: GART instance to use for mapping
769 * @vm: requested vm
770 * @mapping: mapped range and flags to use for the update
771 * @addr: addr to set the area to
772 * @gtt_flags: flags as they are used for GTT
773 * @fence: optional resulting fence
774 *
775 * Split the mapping into smaller chunks so that each update fits
776 * into a SDMA IB.
777 * Returns 0 for success, -EINVAL for failure.
778 */
779static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
780 struct amdgpu_gart *gtt,
781 uint32_t gtt_flags,
782 struct amdgpu_vm *vm,
783 struct amdgpu_bo_va_mapping *mapping,
784 uint64_t addr, struct fence **fence)
785{
786 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
787
788 uint64_t start = mapping->it.start;
789 uint32_t flags = gtt_flags;
790 int r;
791
792 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
793 * but in case of something, we filter the flags in first place
794 */
795 if (!(mapping->flags & AMDGPU_PTE_READABLE))
796 flags &= ~AMDGPU_PTE_READABLE;
797 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
798 flags &= ~AMDGPU_PTE_WRITEABLE;
799
800 trace_amdgpu_vm_bo_update(mapping);
801
802 addr += mapping->offset;
803
804 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
805 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
806 start, mapping->it.last,
807 flags, addr, fence);
808
809 while (start != mapping->it.last + 1) {
810 uint64_t last;
811
812 last = min((uint64_t)mapping->it.last, start + max_size);
813 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
814 start, last, flags, addr,
815 fence);
816 if (r)
817 return r;
818
819 start = last + 1;
820 addr += max_size;
821 }
822
823 return 0;
824}
825
826/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400827 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
828 *
829 * @adev: amdgpu_device pointer
830 * @bo_va: requested BO and VM object
831 * @mem: ttm mem
832 *
833 * Fill in the page table entries for @bo_va.
834 * Returns 0 for success, -EINVAL for failure.
835 *
836 * Object have to be reserved and mutex must be locked!
837 */
838int amdgpu_vm_bo_update(struct amdgpu_device *adev,
839 struct amdgpu_bo_va *bo_va,
840 struct ttm_mem_reg *mem)
841{
842 struct amdgpu_vm *vm = bo_va->vm;
843 struct amdgpu_bo_va_mapping *mapping;
Christian König9ab21462015-11-30 14:19:26 +0100844 struct amdgpu_gart *gtt = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400845 uint32_t flags;
846 uint64_t addr;
847 int r;
848
849 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200850 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100851 switch (mem->mem_type) {
852 case TTM_PL_TT:
853 gtt = &bo_va->bo->adev->gart;
854 break;
855
856 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100858 break;
859
860 default:
861 break;
862 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400863 } else {
864 addr = 0;
865 }
866
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400867 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
868
Christian König7fc11952015-07-30 11:53:42 +0200869 spin_lock(&vm->status_lock);
870 if (!list_empty(&bo_va->vm_status))
871 list_splice_init(&bo_va->valids, &bo_va->invalids);
872 spin_unlock(&vm->status_lock);
873
874 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa14faa62016-01-25 14:27:31 +0100875 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
876 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877 if (r)
878 return r;
879 }
880
Christian Königd6c10f62015-09-28 12:00:23 +0200881 if (trace_amdgpu_vm_bo_mapping_enabled()) {
882 list_for_each_entry(mapping, &bo_va->valids, list)
883 trace_amdgpu_vm_bo_mapping(mapping);
884
885 list_for_each_entry(mapping, &bo_va->invalids, list)
886 trace_amdgpu_vm_bo_mapping(mapping);
887 }
888
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800890 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400891 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200892 if (!mem)
893 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894 spin_unlock(&vm->status_lock);
895
896 return 0;
897}
898
899/**
900 * amdgpu_vm_clear_freed - clear freed BOs in the PT
901 *
902 * @adev: amdgpu_device pointer
903 * @vm: requested vm
904 *
905 * Make sure all freed BOs are cleared in the PT.
906 * Returns 0 for success.
907 *
908 * PTs have to be reserved and mutex must be locked!
909 */
910int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
911 struct amdgpu_vm *vm)
912{
913 struct amdgpu_bo_va_mapping *mapping;
914 int r;
915
jimqu81d75a32015-12-04 17:17:00 +0800916 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400917 while (!list_empty(&vm->freed)) {
918 mapping = list_first_entry(&vm->freed,
919 struct amdgpu_bo_va_mapping, list);
920 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800921 spin_unlock(&vm->freed_lock);
Christian Königa14faa62016-01-25 14:27:31 +0100922 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
923 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400924 kfree(mapping);
925 if (r)
926 return r;
927
jimqu81d75a32015-12-04 17:17:00 +0800928 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 }
jimqu81d75a32015-12-04 17:17:00 +0800930 spin_unlock(&vm->freed_lock);
931
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400932 return 0;
933
934}
935
936/**
937 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
938 *
939 * @adev: amdgpu_device pointer
940 * @vm: requested vm
941 *
942 * Make sure all invalidated BOs are cleared in the PT.
943 * Returns 0 for success.
944 *
945 * PTs have to be reserved and mutex must be locked!
946 */
947int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800948 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400949{
monk.liucfe2c972015-05-26 15:01:54 +0800950 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200951 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952
953 spin_lock(&vm->status_lock);
954 while (!list_empty(&vm->invalidated)) {
955 bo_va = list_first_entry(&vm->invalidated,
956 struct amdgpu_bo_va, vm_status);
957 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800958 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400959 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800960 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961 if (r)
962 return r;
963
964 spin_lock(&vm->status_lock);
965 }
966 spin_unlock(&vm->status_lock);
967
monk.liucfe2c972015-05-26 15:01:54 +0800968 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800969 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200970
971 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972}
973
974/**
975 * amdgpu_vm_bo_add - add a bo to a specific vm
976 *
977 * @adev: amdgpu_device pointer
978 * @vm: requested vm
979 * @bo: amdgpu buffer object
980 *
Christian König8843dbb2016-01-26 12:17:11 +0100981 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982 * Add @bo to the list of bos associated with the vm
983 * Returns newly added bo_va or NULL for failure
984 *
985 * Object has to be reserved!
986 */
987struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
988 struct amdgpu_vm *vm,
989 struct amdgpu_bo *bo)
990{
991 struct amdgpu_bo_va *bo_va;
992
993 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
994 if (bo_va == NULL) {
995 return NULL;
996 }
997 bo_va->vm = vm;
998 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999 bo_va->ref_count = 1;
1000 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001001 INIT_LIST_HEAD(&bo_va->valids);
1002 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001003 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001004 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006
1007 return bo_va;
1008}
1009
1010/**
1011 * amdgpu_vm_bo_map - map bo inside a vm
1012 *
1013 * @adev: amdgpu_device pointer
1014 * @bo_va: bo_va to store the address
1015 * @saddr: where to map the BO
1016 * @offset: requested offset in the BO
1017 * @flags: attributes of pages (read/write/valid/etc.)
1018 *
1019 * Add a mapping of the BO at the specefied addr into the VM.
1020 * Returns 0 for success, error for failure.
1021 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001022 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023 */
1024int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1025 struct amdgpu_bo_va *bo_va,
1026 uint64_t saddr, uint64_t offset,
1027 uint64_t size, uint32_t flags)
1028{
1029 struct amdgpu_bo_va_mapping *mapping;
1030 struct amdgpu_vm *vm = bo_va->vm;
1031 struct interval_tree_node *it;
1032 unsigned last_pfn, pt_idx;
1033 uint64_t eaddr;
1034 int r;
1035
Christian König0be52de2015-05-18 14:37:27 +02001036 /* validate the parameters */
1037 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001038 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001039 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001040
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001042 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001043 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001044 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045
1046 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001047 if (last_pfn >= adev->vm_manager.max_pfn) {
1048 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001049 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050 return -EINVAL;
1051 }
1052
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001053 saddr /= AMDGPU_GPU_PAGE_SIZE;
1054 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1055
Chunming Zhouc25867d2015-11-13 13:32:01 +08001056 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001057 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001058 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001059 if (it) {
1060 struct amdgpu_bo_va_mapping *tmp;
1061 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1062 /* bo and tmp overlap, invalid addr */
1063 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1064 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1065 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001066 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001067 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068 }
1069
1070 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1071 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001073 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074 }
1075
1076 INIT_LIST_HEAD(&mapping->list);
1077 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001078 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001079 mapping->offset = offset;
1080 mapping->flags = flags;
1081
Chunming Zhou69b576a2015-11-18 11:17:39 +08001082 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001083 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001084 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001085 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001086 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001087 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001088 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001089
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090 /* Make sure the page tables are allocated */
1091 saddr >>= amdgpu_vm_block_size;
1092 eaddr >>= amdgpu_vm_block_size;
1093
1094 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1095
1096 if (eaddr > vm->max_pde_used)
1097 vm->max_pde_used = eaddr;
1098
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 /* walk over the address space and allocate the page tables */
1100 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001101 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001102 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001103 struct amdgpu_bo *pt;
1104
Christian Königee1782c2015-12-11 21:01:23 +01001105 entry = &vm->page_tables[pt_idx].entry;
1106 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 continue;
1108
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001109 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1110 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001111 AMDGPU_GEM_DOMAIN_VRAM,
1112 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001113 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001114 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001115 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001116
Christian König82b9c552015-11-27 16:49:00 +01001117 /* Keep a reference to the page table to avoid freeing
1118 * them up in the wrong order.
1119 */
1120 pt->parent = amdgpu_bo_ref(vm->page_directory);
1121
Christian König2bd9ccf2016-02-01 12:53:58 +01001122 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001123 if (r) {
1124 amdgpu_bo_unref(&pt);
1125 goto error_free;
1126 }
1127
Christian Königee1782c2015-12-11 21:01:23 +01001128 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001129 entry->priority = 0;
1130 entry->tv.bo = &entry->robj->tbo;
1131 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001132 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133 }
1134
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001135 return 0;
1136
1137error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001138 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001139 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001141 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001142 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001143 kfree(mapping);
1144
Chunming Zhouf48b2652015-10-16 14:06:19 +08001145error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146 return r;
1147}
1148
1149/**
1150 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1151 *
1152 * @adev: amdgpu_device pointer
1153 * @bo_va: bo_va to remove the address from
1154 * @saddr: where to the BO is mapped
1155 *
1156 * Remove a mapping of the BO at the specefied addr from the VM.
1157 * Returns 0 for success, error for failure.
1158 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001159 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160 */
1161int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1162 struct amdgpu_bo_va *bo_va,
1163 uint64_t saddr)
1164{
1165 struct amdgpu_bo_va_mapping *mapping;
1166 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001167 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168
Christian König6c7fc502015-06-05 20:56:17 +02001169 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001170 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001171 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 if (mapping->it.start == saddr)
1173 break;
1174 }
1175
Christian König7fc11952015-07-30 11:53:42 +02001176 if (&mapping->list == &bo_va->valids) {
1177 valid = false;
1178
1179 list_for_each_entry(mapping, &bo_va->invalids, list) {
1180 if (mapping->it.start == saddr)
1181 break;
1182 }
1183
Chunming Zhou69b576a2015-11-18 11:17:39 +08001184 if (&mapping->list == &bo_va->invalids) {
1185 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001186 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001187 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001189 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001190 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001191 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001193 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001194 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195
jimqu81d75a32015-12-04 17:17:00 +08001196 if (valid) {
1197 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001198 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001199 spin_unlock(&vm->freed_lock);
1200 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001202 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001203
1204 return 0;
1205}
1206
1207/**
1208 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1209 *
1210 * @adev: amdgpu_device pointer
1211 * @bo_va: requested bo_va
1212 *
Christian König8843dbb2016-01-26 12:17:11 +01001213 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214 *
1215 * Object have to be reserved!
1216 */
1217void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1218 struct amdgpu_bo_va *bo_va)
1219{
1220 struct amdgpu_bo_va_mapping *mapping, *next;
1221 struct amdgpu_vm *vm = bo_va->vm;
1222
1223 list_del(&bo_va->bo_list);
1224
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 spin_lock(&vm->status_lock);
1226 list_del(&bo_va->vm_status);
1227 spin_unlock(&vm->status_lock);
1228
Christian König7fc11952015-07-30 11:53:42 +02001229 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001231 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001233 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001234 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001235 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001236 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001237 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001238 }
Christian König7fc11952015-07-30 11:53:42 +02001239 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1240 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001241 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001242 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001243 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001244 kfree(mapping);
1245 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001246 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001247 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001248 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001249}
1250
1251/**
1252 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1253 *
1254 * @adev: amdgpu_device pointer
1255 * @vm: requested vm
1256 * @bo: amdgpu buffer object
1257 *
Christian König8843dbb2016-01-26 12:17:11 +01001258 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 */
1260void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1261 struct amdgpu_bo *bo)
1262{
1263 struct amdgpu_bo_va *bo_va;
1264
1265 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001266 spin_lock(&bo_va->vm->status_lock);
1267 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001268 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001269 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270 }
1271}
1272
1273/**
1274 * amdgpu_vm_init - initialize a vm instance
1275 *
1276 * @adev: amdgpu_device pointer
1277 * @vm: requested vm
1278 *
Christian König8843dbb2016-01-26 12:17:11 +01001279 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 */
1281int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1282{
1283 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1284 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001285 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001286 unsigned ring_instance;
1287 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001288 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289 int i, r;
1290
1291 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001292 vm->ids[i].mgr_id = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001293 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001294 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 vm->va = RB_ROOT;
1296 spin_lock_init(&vm->status_lock);
1297 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001298 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001300 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001301 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001302 pd_size = amdgpu_vm_directory_size(adev);
1303 pd_entries = amdgpu_vm_num_pdes(adev);
1304
1305 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001306 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 if (vm->page_tables == NULL) {
1308 DRM_ERROR("Cannot allocate memory for page table array\n");
1309 return -ENOMEM;
1310 }
1311
Christian König2bd9ccf2016-02-01 12:53:58 +01001312 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001313
1314 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1315 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1316 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001317 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1318 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1319 rq, amdgpu_sched_jobs);
1320 if (r)
1321 return r;
1322
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001323 vm->page_directory_fence = NULL;
1324
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001325 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001326 AMDGPU_GEM_DOMAIN_VRAM,
1327 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001328 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001329 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001330 goto error_free_sched_entity;
1331
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001332 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001333 if (r)
1334 goto error_free_page_directory;
1335
1336 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001337 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001338 if (r)
1339 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001340
1341 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001342
1343error_free_page_directory:
1344 amdgpu_bo_unref(&vm->page_directory);
1345 vm->page_directory = NULL;
1346
1347error_free_sched_entity:
1348 amd_sched_entity_fini(&ring->sched, &vm->entity);
1349
1350 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001351}
1352
1353/**
1354 * amdgpu_vm_fini - tear down a vm instance
1355 *
1356 * @adev: amdgpu_device pointer
1357 * @vm: requested vm
1358 *
Christian König8843dbb2016-01-26 12:17:11 +01001359 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001360 * Unbind the VM and remove all bos from the vm bo list
1361 */
1362void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1363{
1364 struct amdgpu_bo_va_mapping *mapping, *tmp;
1365 int i;
1366
Christian König2d55e452016-02-08 17:37:38 +01001367 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001368
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001369 if (!RB_EMPTY_ROOT(&vm->va)) {
1370 dev_err(adev->dev, "still active bo inside vm\n");
1371 }
1372 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1373 list_del(&mapping->list);
1374 interval_tree_remove(&mapping->it, &vm->va);
1375 kfree(mapping);
1376 }
1377 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1378 list_del(&mapping->list);
1379 kfree(mapping);
1380 }
1381
1382 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001383 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001384 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385
1386 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001387 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001388 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001389 struct amdgpu_vm_id *id = &vm->ids[i];
Christian König1c16c0a2015-11-14 21:31:40 +01001390
Christian König4ff37a82016-02-26 16:18:26 +01001391 if (id->mgr_id)
1392 atomic_long_cmpxchg(&id->mgr_id->owner,
1393 (long)id, 0);
1394 fence_put(id->flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001395 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396}
Christian Königea89f8c2015-11-15 20:52:06 +01001397
1398/**
Christian Königa9a78b32016-01-21 10:19:11 +01001399 * amdgpu_vm_manager_init - init the VM manager
1400 *
1401 * @adev: amdgpu_device pointer
1402 *
1403 * Initialize the VM manager structures
1404 */
1405void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1406{
1407 unsigned i;
1408
1409 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1410
1411 /* skip over VMID 0, since it is the system VM */
1412 for (i = 1; i < adev->vm_manager.num_ids; ++i)
1413 list_add_tail(&adev->vm_manager.ids[i].list,
1414 &adev->vm_manager.ids_lru);
Christian König2d55e452016-02-08 17:37:38 +01001415
1416 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001417}
1418
1419/**
Christian Königea89f8c2015-11-15 20:52:06 +01001420 * amdgpu_vm_manager_fini - cleanup VM manager
1421 *
1422 * @adev: amdgpu_device pointer
1423 *
1424 * Cleanup the VM manager and free resources.
1425 */
1426void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1427{
1428 unsigned i;
1429
1430 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001431 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001432}