blob: 364db7c45c67f2f7ac62360ce12c5321adabefec [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
Christian Königa8bd1be2016-03-03 10:50:01 +0100191 r = amdgpu_sync_fence(ring->adev, sync,
192 id->mgr_id->active);
193 if (r) {
194 mutex_unlock(&adev->vm_manager.lock);
195 return r;
196 }
197
Christian König4ff37a82016-02-26 16:18:26 +0100198 fence_put(id->mgr_id->active);
199 id->mgr_id->active = fence_get(fence);
200
201 list_move_tail(&id->mgr_id->list,
202 &adev->vm_manager.ids_lru);
203
204 *vm_id = id->mgr_id - adev->vm_manager.ids;
205 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
Christian König22073fe2016-02-26 16:18:36 +0100206 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id,
207 *vm_pd_addr);
Christian Königa9a78b32016-01-21 10:19:11 +0100208
Christian König94dd0a42016-01-18 17:01:42 +0100209 mutex_unlock(&adev->vm_manager.lock);
Christian König1c16c0a2015-11-14 21:31:40 +0100210 return 0;
211 }
Christian König39ff8442015-09-28 12:01:20 +0200212 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400213
Christian König4ff37a82016-02-26 16:18:26 +0100214 id->mgr_id = list_first_entry(&adev->vm_manager.ids_lru,
215 struct amdgpu_vm_manager_id,
216 list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400217
Christian König4ff37a82016-02-26 16:18:26 +0100218 r = amdgpu_sync_fence(ring->adev, sync, id->mgr_id->active);
Christian Königa9a78b32016-01-21 10:19:11 +0100219 if (!r) {
Christian König4ff37a82016-02-26 16:18:26 +0100220 fence_put(id->mgr_id->active);
221 id->mgr_id->active = fence_get(fence);
222
223 fence_put(id->flushed_updates);
224 id->flushed_updates = fence_get(updates);
225
226 id->pd_gpu_addr = pd_addr;
227
228 list_move_tail(&id->mgr_id->list, &adev->vm_manager.ids_lru);
229 atomic_long_set(&id->mgr_id->owner, (long)id);
230
231 *vm_id = id->mgr_id - adev->vm_manager.ids;
232 *vm_pd_addr = pd_addr;
Christian König22073fe2016-02-26 16:18:36 +0100233 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234 }
235
Christian König94dd0a42016-01-18 17:01:42 +0100236 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100237 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238}
239
240/**
241 * amdgpu_vm_flush - hardware flush the vm
242 *
243 * @ring: ring to use for flush
Christian König4ff37a82016-02-26 16:18:26 +0100244 * @vmid: vmid number to use
245 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246 *
Christian König4ff37a82016-02-26 16:18:26 +0100247 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400248 */
249void amdgpu_vm_flush(struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100250 unsigned vmid,
251 uint64_t pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400252{
Christian König4ff37a82016-02-26 16:18:26 +0100253 if (pd_addr != AMDGPU_VM_NO_FLUSH) {
254 trace_amdgpu_vm_flush(pd_addr, ring->idx, vmid);
255 amdgpu_ring_emit_vm_flush(ring, vmid, pd_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400256 }
257}
258
259/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400260 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
261 *
262 * @vm: requested vm
263 * @bo: requested buffer object
264 *
Christian König8843dbb2016-01-26 12:17:11 +0100265 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 * Search inside the @bos vm list for the requested vm
267 * Returns the found bo_va or NULL if none is found
268 *
269 * Object has to be reserved!
270 */
271struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
272 struct amdgpu_bo *bo)
273{
274 struct amdgpu_bo_va *bo_va;
275
276 list_for_each_entry(bo_va, &bo->va, bo_list) {
277 if (bo_va->vm == vm) {
278 return bo_va;
279 }
280 }
281 return NULL;
282}
283
284/**
285 * amdgpu_vm_update_pages - helper to call the right asic function
286 *
287 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100288 * @gtt: GART instance to use for mapping
289 * @gtt_flags: GTT hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290 * @ib: indirect buffer to fill with commands
291 * @pe: addr of the page entry
292 * @addr: dst addr to write into pe
293 * @count: number of page entries to update
294 * @incr: increase next addr by incr bytes
295 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 *
297 * Traces the parameters and calls the right asic functions
298 * to setup the page table using the DMA.
299 */
300static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100301 struct amdgpu_gart *gtt,
302 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 struct amdgpu_ib *ib,
304 uint64_t pe, uint64_t addr,
305 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100306 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400307{
308 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
309
Christian König9ab21462015-11-30 14:19:26 +0100310 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
311 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400312 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
313
Christian König9ab21462015-11-30 14:19:26 +0100314 } else if (gtt) {
315 dma_addr_t *pages_addr = gtt->pages_addr;
Christian Königb07c9d22015-11-30 13:26:07 +0100316 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
317 count, incr, flags);
318
319 } else if (count < 3) {
320 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
321 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400322
323 } else {
324 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
325 count, incr, flags);
326 }
327}
328
329/**
330 * amdgpu_vm_clear_bo - initially clear the page dir/table
331 *
332 * @adev: amdgpu_device pointer
333 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800334 *
335 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400336 */
337static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100338 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339 struct amdgpu_bo *bo)
340{
Christian König2d55e452016-02-08 17:37:38 +0100341 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800342 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100343 struct amdgpu_job *job;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400344 unsigned entries;
345 uint64_t addr;
346 int r;
347
Christian König2d55e452016-02-08 17:37:38 +0100348 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
349
monk.liuca952612015-05-25 14:44:05 +0800350 r = reservation_object_reserve_shared(bo->tbo.resv);
351 if (r)
352 return r;
353
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400354 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
355 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800356 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357
358 addr = amdgpu_bo_gpu_offset(bo);
359 entries = amdgpu_bo_size(bo) / 8;
360
Christian Königd71518b2016-02-01 12:20:25 +0100361 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
362 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800363 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400364
Christian Königd71518b2016-02-01 12:20:25 +0100365 amdgpu_vm_update_pages(adev, NULL, 0, &job->ibs[0], addr, 0, entries,
366 0, 0);
367 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
368
369 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100370 r = amdgpu_job_submit(job, ring, &vm->entity,
371 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400372 if (r)
373 goto error_free;
374
Christian Königd71518b2016-02-01 12:20:25 +0100375 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800376 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800377 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800378
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400379error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100380 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800382error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400383 return r;
384}
385
386/**
Christian Königb07c9d22015-11-30 13:26:07 +0100387 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388 *
Christian Königb07c9d22015-11-30 13:26:07 +0100389 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400390 * @addr: the unmapped addr
391 *
392 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100393 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400394 */
Christian Königb07c9d22015-11-30 13:26:07 +0100395uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400396{
397 uint64_t result;
398
Christian Königb07c9d22015-11-30 13:26:07 +0100399 if (pages_addr) {
400 /* page table offset */
401 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400402
Christian Königb07c9d22015-11-30 13:26:07 +0100403 /* in case cpu page size != gpu page size*/
404 result |= addr & (~PAGE_MASK);
405
406 } else {
407 /* No mapping required */
408 result = addr;
409 }
410
411 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400412
413 return result;
414}
415
416/**
417 * amdgpu_vm_update_pdes - make sure that page directory is valid
418 *
419 * @adev: amdgpu_device pointer
420 * @vm: requested vm
421 * @start: start of GPU address range
422 * @end: end of GPU address range
423 *
424 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100425 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400426 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400427 */
428int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
429 struct amdgpu_vm *vm)
430{
Christian König2d55e452016-02-08 17:37:38 +0100431 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432 struct amdgpu_bo *pd = vm->page_directory;
433 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
434 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
435 uint64_t last_pde = ~0, last_pt = ~0;
436 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100437 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800438 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800439 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800440
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400441 int r;
442
Christian König2d55e452016-02-08 17:37:38 +0100443 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
444
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445 /* padding, etc. */
446 ndw = 64;
447
448 /* assume the worst case */
449 ndw += vm->max_pde_used * 6;
450
Christian Königd71518b2016-02-01 12:20:25 +0100451 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
452 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100454
455 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400456
457 /* walk over the address space and update the page directory */
458 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100459 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 uint64_t pde, pt;
461
462 if (bo == NULL)
463 continue;
464
465 pt = amdgpu_bo_gpu_offset(bo);
466 if (vm->page_tables[pt_idx].addr == pt)
467 continue;
468 vm->page_tables[pt_idx].addr = pt;
469
470 pde = pd_addr + pt_idx * 8;
471 if (((last_pde + 8 * count) != pde) ||
472 ((last_pt + incr * count) != pt)) {
473
474 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100475 amdgpu_vm_update_pages(adev, NULL, 0, ib,
476 last_pde, last_pt,
477 count, incr,
478 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479 }
480
481 count = 1;
482 last_pde = pde;
483 last_pt = pt;
484 } else {
485 ++count;
486 }
487 }
488
489 if (count)
Christian König9ab21462015-11-30 14:19:26 +0100490 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
491 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400492
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800493 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100494 amdgpu_ring_pad_ib(ring, ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100495 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
496 AMDGPU_FENCE_OWNER_VM);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800497 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100498 r = amdgpu_job_submit(job, ring, &vm->entity,
499 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800500 if (r)
501 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200502
Chunming Zhou4af9f072015-08-03 12:57:31 +0800503 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200504 fence_put(vm->page_directory_fence);
505 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800506 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800507
Christian Königd71518b2016-02-01 12:20:25 +0100508 } else {
509 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800510 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511
512 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800513
514error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100515 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800516 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517}
518
519/**
520 * amdgpu_vm_frag_ptes - add fragment information to PTEs
521 *
522 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100523 * @gtt: GART instance to use for mapping
524 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 * @ib: IB for the update
526 * @pe_start: first PTE to handle
527 * @pe_end: last PTE to handle
528 * @addr: addr those PTEs should point to
529 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 */
531static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100532 struct amdgpu_gart *gtt,
533 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 struct amdgpu_ib *ib,
535 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100536 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537{
538 /**
539 * The MC L1 TLB supports variable sized pages, based on a fragment
540 * field in the PTE. When this field is set to a non-zero value, page
541 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
542 * flags are considered valid for all PTEs within the fragment range
543 * and corresponding mappings are assumed to be physically contiguous.
544 *
545 * The L1 TLB can store a single PTE for the whole fragment,
546 * significantly increasing the space available for translation
547 * caching. This leads to large improvements in throughput when the
548 * TLB is under pressure.
549 *
550 * The L2 TLB distributes small and large fragments into two
551 * asymmetric partitions. The large fragment cache is significantly
552 * larger. Thus, we try to use large fragments wherever possible.
553 * Userspace can support this by aligning virtual base address and
554 * allocation size to the fragment size.
555 */
556
557 /* SI and newer are optimized for 64KB */
558 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
559 uint64_t frag_align = 0x80;
560
561 uint64_t frag_start = ALIGN(pe_start, frag_align);
562 uint64_t frag_end = pe_end & ~(frag_align - 1);
563
564 unsigned count;
565
Christian König31f6c1f2016-01-26 12:37:49 +0100566 /* Abort early if there isn't anything to do */
567 if (pe_start == pe_end)
568 return;
569
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 /* system pages are non continuously */
Christian König9ab21462015-11-30 14:19:26 +0100571 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572
573 count = (pe_end - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100574 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
575 addr, count, AMDGPU_GPU_PAGE_SIZE,
576 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 return;
578 }
579
580 /* handle the 4K area at the beginning */
581 if (pe_start != frag_start) {
582 count = (frag_start - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100583 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
584 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585 addr += AMDGPU_GPU_PAGE_SIZE * count;
586 }
587
588 /* handle the area in the middle */
589 count = (frag_end - frag_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100590 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
591 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592
593 /* handle the 4K area at the end */
594 if (frag_end != pe_end) {
595 addr += AMDGPU_GPU_PAGE_SIZE * count;
596 count = (pe_end - frag_end) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100597 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
598 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 }
600}
601
602/**
603 * amdgpu_vm_update_ptes - make sure that page tables are valid
604 *
605 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100606 * @gtt: GART instance to use for mapping
607 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 * @vm: requested vm
609 * @start: start of GPU address range
610 * @end: end of GPU address range
611 * @dst: destination address to map to
612 * @flags: mapping flags
613 *
Christian König8843dbb2016-01-26 12:17:11 +0100614 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 */
Christian Königa1e08d32016-01-26 11:40:46 +0100616static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
617 struct amdgpu_gart *gtt,
618 uint32_t gtt_flags,
619 struct amdgpu_vm *vm,
620 struct amdgpu_ib *ib,
621 uint64_t start, uint64_t end,
622 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623{
Christian König31f6c1f2016-01-26 12:37:49 +0100624 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
625
626 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 uint64_t addr;
628
629 /* walk over the address space and update the page tables */
630 for (addr = start; addr < end; ) {
631 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100632 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100634 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635
636 if ((addr & ~mask) == (end & ~mask))
637 nptes = end - addr;
638 else
639 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
640
Christian König31f6c1f2016-01-26 12:37:49 +0100641 pe_start = amdgpu_bo_gpu_offset(pt);
642 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643
Christian König31f6c1f2016-01-26 12:37:49 +0100644 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645
Christian König31f6c1f2016-01-26 12:37:49 +0100646 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
647 last_pe_start, last_pe_end,
648 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649
Christian König31f6c1f2016-01-26 12:37:49 +0100650 last_pe_start = pe_start;
651 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 last_dst = dst;
653 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100654 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655 }
656
657 addr += nptes;
658 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
659 }
660
Christian König31f6c1f2016-01-26 12:37:49 +0100661 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
662 last_pe_start, last_pe_end,
663 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664}
665
666/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
668 *
669 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100670 * @gtt: GART instance to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 * @gtt_flags: flags as they are used for GTT
Christian Königa14faa62016-01-25 14:27:31 +0100672 * @vm: requested vm
673 * @start: start of mapped range
674 * @last: last mapped entry
675 * @flags: flags for the entries
676 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 * @fence: optional resulting fence
678 *
Christian Königa14faa62016-01-25 14:27:31 +0100679 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 */
682static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100683 struct amdgpu_gart *gtt,
684 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100686 uint64_t start, uint64_t last,
687 uint32_t flags, uint64_t addr,
688 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689{
Christian König2d55e452016-02-08 17:37:38 +0100690 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100691 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100693 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800694 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800695 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 int r;
697
Christian König2d55e452016-02-08 17:37:38 +0100698 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
699
Christian Königa1e08d32016-01-26 11:40:46 +0100700 /* sync to everything on unmapping */
701 if (!(flags & AMDGPU_PTE_VALID))
702 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
703
Christian Königa14faa62016-01-25 14:27:31 +0100704 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705
706 /*
707 * reserve space for one command every (1 << BLOCK_SIZE)
708 * entries or 2k dwords (whatever is smaller)
709 */
710 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
711
712 /* padding, etc. */
713 ndw = 64;
714
Christian König9ab21462015-11-30 14:19:26 +0100715 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716 /* only copy commands needed */
717 ndw += ncmds * 7;
718
Christian König9ab21462015-11-30 14:19:26 +0100719 } else if (gtt) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 /* header for write data commands */
721 ndw += ncmds * 4;
722
723 /* body of write data command */
724 ndw += nptes * 2;
725
726 } else {
727 /* set page commands needed */
728 ndw += ncmds * 10;
729
730 /* two extra commands for begin/end of fragment */
731 ndw += 2 * 10;
732 }
733
Christian Königd71518b2016-02-01 12:20:25 +0100734 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
735 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400736 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100737
738 ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800739
Christian Könige86f9ce2016-02-08 12:13:05 +0100740 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100741 owner);
742 if (r)
743 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744
Christian Königa1e08d32016-01-26 11:40:46 +0100745 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
746 if (r)
747 goto error_free;
748
749 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
750 addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751
Christian König9e5d53092016-01-31 12:20:55 +0100752 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800753 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100754 r = amdgpu_job_submit(job, ring, &vm->entity,
755 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800756 if (r)
757 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758
Christian Königbf60efd2015-09-04 10:47:56 +0200759 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800760 if (fence) {
761 fence_put(*fence);
762 *fence = fence_get(f);
763 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800764 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800766
767error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100768 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800769 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770}
771
772/**
Christian Königa14faa62016-01-25 14:27:31 +0100773 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
774 *
775 * @adev: amdgpu_device pointer
776 * @gtt: GART instance to use for mapping
777 * @vm: requested vm
778 * @mapping: mapped range and flags to use for the update
779 * @addr: addr to set the area to
780 * @gtt_flags: flags as they are used for GTT
781 * @fence: optional resulting fence
782 *
783 * Split the mapping into smaller chunks so that each update fits
784 * into a SDMA IB.
785 * Returns 0 for success, -EINVAL for failure.
786 */
787static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
788 struct amdgpu_gart *gtt,
789 uint32_t gtt_flags,
790 struct amdgpu_vm *vm,
791 struct amdgpu_bo_va_mapping *mapping,
792 uint64_t addr, struct fence **fence)
793{
794 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
795
796 uint64_t start = mapping->it.start;
797 uint32_t flags = gtt_flags;
798 int r;
799
800 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
801 * but in case of something, we filter the flags in first place
802 */
803 if (!(mapping->flags & AMDGPU_PTE_READABLE))
804 flags &= ~AMDGPU_PTE_READABLE;
805 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
806 flags &= ~AMDGPU_PTE_WRITEABLE;
807
808 trace_amdgpu_vm_bo_update(mapping);
809
810 addr += mapping->offset;
811
812 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
813 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
814 start, mapping->it.last,
815 flags, addr, fence);
816
817 while (start != mapping->it.last + 1) {
818 uint64_t last;
819
820 last = min((uint64_t)mapping->it.last, start + max_size);
821 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
822 start, last, flags, addr,
823 fence);
824 if (r)
825 return r;
826
827 start = last + 1;
828 addr += max_size;
829 }
830
831 return 0;
832}
833
834/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400835 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
836 *
837 * @adev: amdgpu_device pointer
838 * @bo_va: requested BO and VM object
839 * @mem: ttm mem
840 *
841 * Fill in the page table entries for @bo_va.
842 * Returns 0 for success, -EINVAL for failure.
843 *
844 * Object have to be reserved and mutex must be locked!
845 */
846int amdgpu_vm_bo_update(struct amdgpu_device *adev,
847 struct amdgpu_bo_va *bo_va,
848 struct ttm_mem_reg *mem)
849{
850 struct amdgpu_vm *vm = bo_va->vm;
851 struct amdgpu_bo_va_mapping *mapping;
Christian König9ab21462015-11-30 14:19:26 +0100852 struct amdgpu_gart *gtt = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400853 uint32_t flags;
854 uint64_t addr;
855 int r;
856
857 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200858 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100859 switch (mem->mem_type) {
860 case TTM_PL_TT:
861 gtt = &bo_va->bo->adev->gart;
862 break;
863
864 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100866 break;
867
868 default:
869 break;
870 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400871 } else {
872 addr = 0;
873 }
874
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
876
Christian König7fc11952015-07-30 11:53:42 +0200877 spin_lock(&vm->status_lock);
878 if (!list_empty(&bo_va->vm_status))
879 list_splice_init(&bo_va->valids, &bo_va->invalids);
880 spin_unlock(&vm->status_lock);
881
882 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa14faa62016-01-25 14:27:31 +0100883 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
884 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885 if (r)
886 return r;
887 }
888
Christian Königd6c10f62015-09-28 12:00:23 +0200889 if (trace_amdgpu_vm_bo_mapping_enabled()) {
890 list_for_each_entry(mapping, &bo_va->valids, list)
891 trace_amdgpu_vm_bo_mapping(mapping);
892
893 list_for_each_entry(mapping, &bo_va->invalids, list)
894 trace_amdgpu_vm_bo_mapping(mapping);
895 }
896
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400897 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800898 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200900 if (!mem)
901 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400902 spin_unlock(&vm->status_lock);
903
904 return 0;
905}
906
907/**
908 * amdgpu_vm_clear_freed - clear freed BOs in the PT
909 *
910 * @adev: amdgpu_device pointer
911 * @vm: requested vm
912 *
913 * Make sure all freed BOs are cleared in the PT.
914 * Returns 0 for success.
915 *
916 * PTs have to be reserved and mutex must be locked!
917 */
918int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
919 struct amdgpu_vm *vm)
920{
921 struct amdgpu_bo_va_mapping *mapping;
922 int r;
923
jimqu81d75a32015-12-04 17:17:00 +0800924 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925 while (!list_empty(&vm->freed)) {
926 mapping = list_first_entry(&vm->freed,
927 struct amdgpu_bo_va_mapping, list);
928 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800929 spin_unlock(&vm->freed_lock);
Christian Königa14faa62016-01-25 14:27:31 +0100930 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
931 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400932 kfree(mapping);
933 if (r)
934 return r;
935
jimqu81d75a32015-12-04 17:17:00 +0800936 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937 }
jimqu81d75a32015-12-04 17:17:00 +0800938 spin_unlock(&vm->freed_lock);
939
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940 return 0;
941
942}
943
944/**
945 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
946 *
947 * @adev: amdgpu_device pointer
948 * @vm: requested vm
949 *
950 * Make sure all invalidated BOs are cleared in the PT.
951 * Returns 0 for success.
952 *
953 * PTs have to be reserved and mutex must be locked!
954 */
955int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800956 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400957{
monk.liucfe2c972015-05-26 15:01:54 +0800958 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200959 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960
961 spin_lock(&vm->status_lock);
962 while (!list_empty(&vm->invalidated)) {
963 bo_va = list_first_entry(&vm->invalidated,
964 struct amdgpu_bo_va, vm_status);
965 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800966 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400967 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800968 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969 if (r)
970 return r;
971
972 spin_lock(&vm->status_lock);
973 }
974 spin_unlock(&vm->status_lock);
975
monk.liucfe2c972015-05-26 15:01:54 +0800976 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800977 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200978
979 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980}
981
982/**
983 * amdgpu_vm_bo_add - add a bo to a specific vm
984 *
985 * @adev: amdgpu_device pointer
986 * @vm: requested vm
987 * @bo: amdgpu buffer object
988 *
Christian König8843dbb2016-01-26 12:17:11 +0100989 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990 * Add @bo to the list of bos associated with the vm
991 * Returns newly added bo_va or NULL for failure
992 *
993 * Object has to be reserved!
994 */
995struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
996 struct amdgpu_vm *vm,
997 struct amdgpu_bo *bo)
998{
999 struct amdgpu_bo_va *bo_va;
1000
1001 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1002 if (bo_va == NULL) {
1003 return NULL;
1004 }
1005 bo_va->vm = vm;
1006 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007 bo_va->ref_count = 1;
1008 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001009 INIT_LIST_HEAD(&bo_va->valids);
1010 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001011 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001012 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001013 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001014
1015 return bo_va;
1016}
1017
1018/**
1019 * amdgpu_vm_bo_map - map bo inside a vm
1020 *
1021 * @adev: amdgpu_device pointer
1022 * @bo_va: bo_va to store the address
1023 * @saddr: where to map the BO
1024 * @offset: requested offset in the BO
1025 * @flags: attributes of pages (read/write/valid/etc.)
1026 *
1027 * Add a mapping of the BO at the specefied addr into the VM.
1028 * Returns 0 for success, error for failure.
1029 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001030 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001031 */
1032int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1033 struct amdgpu_bo_va *bo_va,
1034 uint64_t saddr, uint64_t offset,
1035 uint64_t size, uint32_t flags)
1036{
1037 struct amdgpu_bo_va_mapping *mapping;
1038 struct amdgpu_vm *vm = bo_va->vm;
1039 struct interval_tree_node *it;
1040 unsigned last_pfn, pt_idx;
1041 uint64_t eaddr;
1042 int r;
1043
Christian König0be52de2015-05-18 14:37:27 +02001044 /* validate the parameters */
1045 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001046 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001047 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001048
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001049 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001050 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001051 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001052 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001053
1054 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001055 if (last_pfn >= adev->vm_manager.max_pfn) {
1056 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001057 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001058 return -EINVAL;
1059 }
1060
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 saddr /= AMDGPU_GPU_PAGE_SIZE;
1062 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1063
Chunming Zhouc25867d2015-11-13 13:32:01 +08001064 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001065 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001066 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001067 if (it) {
1068 struct amdgpu_bo_va_mapping *tmp;
1069 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1070 /* bo and tmp overlap, invalid addr */
1071 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1072 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1073 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001075 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076 }
1077
1078 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1079 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001081 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001082 }
1083
1084 INIT_LIST_HEAD(&mapping->list);
1085 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001086 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087 mapping->offset = offset;
1088 mapping->flags = flags;
1089
Chunming Zhou69b576a2015-11-18 11:17:39 +08001090 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001091 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001092 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001093 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001094 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001095 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001096 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098 /* Make sure the page tables are allocated */
1099 saddr >>= amdgpu_vm_block_size;
1100 eaddr >>= amdgpu_vm_block_size;
1101
1102 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1103
1104 if (eaddr > vm->max_pde_used)
1105 vm->max_pde_used = eaddr;
1106
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 /* walk over the address space and allocate the page tables */
1108 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001109 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001110 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001111 struct amdgpu_bo *pt;
1112
Christian Königee1782c2015-12-11 21:01:23 +01001113 entry = &vm->page_tables[pt_idx].entry;
1114 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001115 continue;
1116
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1118 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001119 AMDGPU_GEM_DOMAIN_VRAM,
1120 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001121 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001122 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001123 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001124
Christian König82b9c552015-11-27 16:49:00 +01001125 /* Keep a reference to the page table to avoid freeing
1126 * them up in the wrong order.
1127 */
1128 pt->parent = amdgpu_bo_ref(vm->page_directory);
1129
Christian König2bd9ccf2016-02-01 12:53:58 +01001130 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001131 if (r) {
1132 amdgpu_bo_unref(&pt);
1133 goto error_free;
1134 }
1135
Christian Königee1782c2015-12-11 21:01:23 +01001136 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001137 entry->priority = 0;
1138 entry->tv.bo = &entry->robj->tbo;
1139 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001141 }
1142
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001143 return 0;
1144
1145error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001147 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001148 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001149 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001150 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151 kfree(mapping);
1152
Chunming Zhouf48b2652015-10-16 14:06:19 +08001153error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 return r;
1155}
1156
1157/**
1158 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1159 *
1160 * @adev: amdgpu_device pointer
1161 * @bo_va: bo_va to remove the address from
1162 * @saddr: where to the BO is mapped
1163 *
1164 * Remove a mapping of the BO at the specefied addr from the VM.
1165 * Returns 0 for success, error for failure.
1166 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001167 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168 */
1169int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1170 struct amdgpu_bo_va *bo_va,
1171 uint64_t saddr)
1172{
1173 struct amdgpu_bo_va_mapping *mapping;
1174 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001175 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001176
Christian König6c7fc502015-06-05 20:56:17 +02001177 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001178 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001179 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001180 if (mapping->it.start == saddr)
1181 break;
1182 }
1183
Christian König7fc11952015-07-30 11:53:42 +02001184 if (&mapping->list == &bo_va->valids) {
1185 valid = false;
1186
1187 list_for_each_entry(mapping, &bo_va->invalids, list) {
1188 if (mapping->it.start == saddr)
1189 break;
1190 }
1191
Chunming Zhou69b576a2015-11-18 11:17:39 +08001192 if (&mapping->list == &bo_va->invalids) {
1193 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001194 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001195 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001197 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001198 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001199 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001200 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001201 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001202 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001203
jimqu81d75a32015-12-04 17:17:00 +08001204 if (valid) {
1205 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001206 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001207 spin_unlock(&vm->freed_lock);
1208 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001210 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211
1212 return 0;
1213}
1214
1215/**
1216 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1217 *
1218 * @adev: amdgpu_device pointer
1219 * @bo_va: requested bo_va
1220 *
Christian König8843dbb2016-01-26 12:17:11 +01001221 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 *
1223 * Object have to be reserved!
1224 */
1225void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1226 struct amdgpu_bo_va *bo_va)
1227{
1228 struct amdgpu_bo_va_mapping *mapping, *next;
1229 struct amdgpu_vm *vm = bo_va->vm;
1230
1231 list_del(&bo_va->bo_list);
1232
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233 spin_lock(&vm->status_lock);
1234 list_del(&bo_va->vm_status);
1235 spin_unlock(&vm->status_lock);
1236
Christian König7fc11952015-07-30 11:53:42 +02001237 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001238 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001239 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001240 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001241 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001242 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001243 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001244 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001245 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246 }
Christian König7fc11952015-07-30 11:53:42 +02001247 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1248 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001249 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001250 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001251 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001252 kfree(mapping);
1253 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001254 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001255 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257}
1258
1259/**
1260 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1261 *
1262 * @adev: amdgpu_device pointer
1263 * @vm: requested vm
1264 * @bo: amdgpu buffer object
1265 *
Christian König8843dbb2016-01-26 12:17:11 +01001266 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001267 */
1268void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1269 struct amdgpu_bo *bo)
1270{
1271 struct amdgpu_bo_va *bo_va;
1272
1273 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001274 spin_lock(&bo_va->vm->status_lock);
1275 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001277 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278 }
1279}
1280
1281/**
1282 * amdgpu_vm_init - initialize a vm instance
1283 *
1284 * @adev: amdgpu_device pointer
1285 * @vm: requested vm
1286 *
Christian König8843dbb2016-01-26 12:17:11 +01001287 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001288 */
1289int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1290{
1291 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1292 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001293 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001294 unsigned ring_instance;
1295 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001296 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297 int i, r;
1298
1299 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001300 vm->ids[i].mgr_id = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001302 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 vm->va = RB_ROOT;
1304 spin_lock_init(&vm->status_lock);
1305 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001306 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001308 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001309 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001310 pd_size = amdgpu_vm_directory_size(adev);
1311 pd_entries = amdgpu_vm_num_pdes(adev);
1312
1313 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001314 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 if (vm->page_tables == NULL) {
1316 DRM_ERROR("Cannot allocate memory for page table array\n");
1317 return -ENOMEM;
1318 }
1319
Christian König2bd9ccf2016-02-01 12:53:58 +01001320 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001321
1322 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1323 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1324 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001325 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1326 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1327 rq, amdgpu_sched_jobs);
1328 if (r)
1329 return r;
1330
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001331 vm->page_directory_fence = NULL;
1332
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001333 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001334 AMDGPU_GEM_DOMAIN_VRAM,
1335 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001336 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001337 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001338 goto error_free_sched_entity;
1339
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001340 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001341 if (r)
1342 goto error_free_page_directory;
1343
1344 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001345 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001346 if (r)
1347 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348
1349 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001350
1351error_free_page_directory:
1352 amdgpu_bo_unref(&vm->page_directory);
1353 vm->page_directory = NULL;
1354
1355error_free_sched_entity:
1356 amd_sched_entity_fini(&ring->sched, &vm->entity);
1357
1358 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001359}
1360
1361/**
1362 * amdgpu_vm_fini - tear down a vm instance
1363 *
1364 * @adev: amdgpu_device pointer
1365 * @vm: requested vm
1366 *
Christian König8843dbb2016-01-26 12:17:11 +01001367 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001368 * Unbind the VM and remove all bos from the vm bo list
1369 */
1370void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1371{
1372 struct amdgpu_bo_va_mapping *mapping, *tmp;
1373 int i;
1374
Christian König2d55e452016-02-08 17:37:38 +01001375 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001376
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 if (!RB_EMPTY_ROOT(&vm->va)) {
1378 dev_err(adev->dev, "still active bo inside vm\n");
1379 }
1380 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1381 list_del(&mapping->list);
1382 interval_tree_remove(&mapping->it, &vm->va);
1383 kfree(mapping);
1384 }
1385 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1386 list_del(&mapping->list);
1387 kfree(mapping);
1388 }
1389
1390 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001391 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001392 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001393
1394 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001395 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001397 struct amdgpu_vm_id *id = &vm->ids[i];
Christian König1c16c0a2015-11-14 21:31:40 +01001398
Christian König4ff37a82016-02-26 16:18:26 +01001399 if (id->mgr_id)
1400 atomic_long_cmpxchg(&id->mgr_id->owner,
1401 (long)id, 0);
1402 fence_put(id->flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001404}
Christian Königea89f8c2015-11-15 20:52:06 +01001405
1406/**
Christian Königa9a78b32016-01-21 10:19:11 +01001407 * amdgpu_vm_manager_init - init the VM manager
1408 *
1409 * @adev: amdgpu_device pointer
1410 *
1411 * Initialize the VM manager structures
1412 */
1413void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1414{
1415 unsigned i;
1416
1417 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1418
1419 /* skip over VMID 0, since it is the system VM */
1420 for (i = 1; i < adev->vm_manager.num_ids; ++i)
1421 list_add_tail(&adev->vm_manager.ids[i].list,
1422 &adev->vm_manager.ids_lru);
Christian König2d55e452016-02-08 17:37:38 +01001423
1424 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001425}
1426
1427/**
Christian Königea89f8c2015-11-15 20:52:06 +01001428 * amdgpu_vm_manager_fini - cleanup VM manager
1429 *
1430 * @adev: amdgpu_device pointer
1431 *
1432 * Cleanup the VM manager and free resources.
1433 */
1434void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1435{
1436 unsigned i;
1437
1438 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001439 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001440}