blob: a0896c761108112a88e551a9ede410791eb4a3f8 [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önigcffadc82016-03-01 13:34:49 +0100244 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100245 * @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önigcffadc82016-03-01 13:34:49 +0100250 unsigned vm_id, uint64_t pd_addr,
251 uint32_t gds_base, uint32_t gds_size,
252 uint32_t gws_base, uint32_t gws_size,
253 uint32_t oa_base, uint32_t oa_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254{
Christian König971fe9a92016-03-01 15:09:25 +0100255 struct amdgpu_device *adev = ring->adev;
256 struct amdgpu_vm_manager_id *mgr_id = &adev->vm_manager.ids[vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100257 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
258 mgr_id->gds_base != gds_base ||
259 mgr_id->gds_size != gds_size ||
260 mgr_id->gws_base != gws_base ||
261 mgr_id->gws_size != gws_size ||
262 mgr_id->oa_base != oa_base ||
263 mgr_id->oa_size != oa_size);
264
265 if (ring->funcs->emit_pipeline_sync && (
266 pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed))
267 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100268
Christian König4ff37a82016-02-26 16:18:26 +0100269 if (pd_addr != AMDGPU_VM_NO_FLUSH) {
Christian Königcffadc82016-03-01 13:34:49 +0100270 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
271 amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272 }
Christian Königcffadc82016-03-01 13:34:49 +0100273
Christian Königd564a062016-03-01 15:51:53 +0100274 if (gds_switch_needed) {
Christian König971fe9a92016-03-01 15:09:25 +0100275 mgr_id->gds_base = gds_base;
276 mgr_id->gds_size = gds_size;
277 mgr_id->gws_base = gws_base;
278 mgr_id->gws_size = gws_size;
279 mgr_id->oa_base = oa_base;
280 mgr_id->oa_size = oa_size;
Christian Königcffadc82016-03-01 13:34:49 +0100281 amdgpu_ring_emit_gds_switch(ring, vm_id,
282 gds_base, gds_size,
283 gws_base, gws_size,
284 oa_base, oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100285 }
286}
287
288/**
289 * amdgpu_vm_reset_id - reset VMID to zero
290 *
291 * @adev: amdgpu device structure
292 * @vm_id: vmid number to use
293 *
294 * Reset saved GDW, GWS and OA to force switch on next flush.
295 */
296void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
297{
298 struct amdgpu_vm_manager_id *mgr_id = &adev->vm_manager.ids[vm_id];
299
300 mgr_id->gds_base = 0;
301 mgr_id->gds_size = 0;
302 mgr_id->gws_base = 0;
303 mgr_id->gws_size = 0;
304 mgr_id->oa_base = 0;
305 mgr_id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306}
307
308/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400309 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
310 *
311 * @vm: requested vm
312 * @bo: requested buffer object
313 *
Christian König8843dbb2016-01-26 12:17:11 +0100314 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315 * Search inside the @bos vm list for the requested vm
316 * Returns the found bo_va or NULL if none is found
317 *
318 * Object has to be reserved!
319 */
320struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
321 struct amdgpu_bo *bo)
322{
323 struct amdgpu_bo_va *bo_va;
324
325 list_for_each_entry(bo_va, &bo->va, bo_list) {
326 if (bo_va->vm == vm) {
327 return bo_va;
328 }
329 }
330 return NULL;
331}
332
333/**
334 * amdgpu_vm_update_pages - helper to call the right asic function
335 *
336 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100337 * @gtt: GART instance to use for mapping
338 * @gtt_flags: GTT hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339 * @ib: indirect buffer to fill with commands
340 * @pe: addr of the page entry
341 * @addr: dst addr to write into pe
342 * @count: number of page entries to update
343 * @incr: increase next addr by incr bytes
344 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400345 *
346 * Traces the parameters and calls the right asic functions
347 * to setup the page table using the DMA.
348 */
349static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100350 struct amdgpu_gart *gtt,
351 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400352 struct amdgpu_ib *ib,
353 uint64_t pe, uint64_t addr,
354 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100355 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356{
357 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
358
Christian König9ab21462015-11-30 14:19:26 +0100359 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
360 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400361 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
362
Christian König9ab21462015-11-30 14:19:26 +0100363 } else if (gtt) {
364 dma_addr_t *pages_addr = gtt->pages_addr;
Christian Königb07c9d22015-11-30 13:26:07 +0100365 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
366 count, incr, flags);
367
368 } else if (count < 3) {
369 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
370 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400371
372 } else {
373 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
374 count, incr, flags);
375 }
376}
377
378/**
379 * amdgpu_vm_clear_bo - initially clear the page dir/table
380 *
381 * @adev: amdgpu_device pointer
382 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800383 *
384 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400385 */
386static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100387 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388 struct amdgpu_bo *bo)
389{
Christian König2d55e452016-02-08 17:37:38 +0100390 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800391 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100392 struct amdgpu_job *job;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400393 unsigned entries;
394 uint64_t addr;
395 int r;
396
Christian König2d55e452016-02-08 17:37:38 +0100397 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
398
monk.liuca952612015-05-25 14:44:05 +0800399 r = reservation_object_reserve_shared(bo->tbo.resv);
400 if (r)
401 return r;
402
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400403 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
404 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800405 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400406
407 addr = amdgpu_bo_gpu_offset(bo);
408 entries = amdgpu_bo_size(bo) / 8;
409
Christian Königd71518b2016-02-01 12:20:25 +0100410 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
411 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800412 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400413
Christian Königd71518b2016-02-01 12:20:25 +0100414 amdgpu_vm_update_pages(adev, NULL, 0, &job->ibs[0], addr, 0, entries,
415 0, 0);
416 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
417
418 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100419 r = amdgpu_job_submit(job, ring, &vm->entity,
420 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400421 if (r)
422 goto error_free;
423
Christian Königd71518b2016-02-01 12:20:25 +0100424 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800425 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800426 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800427
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100429 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400430
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800431error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432 return r;
433}
434
435/**
Christian Königb07c9d22015-11-30 13:26:07 +0100436 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437 *
Christian Königb07c9d22015-11-30 13:26:07 +0100438 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400439 * @addr: the unmapped addr
440 *
441 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100442 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400443 */
Christian Königb07c9d22015-11-30 13:26:07 +0100444uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445{
446 uint64_t result;
447
Christian Königb07c9d22015-11-30 13:26:07 +0100448 if (pages_addr) {
449 /* page table offset */
450 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451
Christian Königb07c9d22015-11-30 13:26:07 +0100452 /* in case cpu page size != gpu page size*/
453 result |= addr & (~PAGE_MASK);
454
455 } else {
456 /* No mapping required */
457 result = addr;
458 }
459
460 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400461
462 return result;
463}
464
465/**
466 * amdgpu_vm_update_pdes - make sure that page directory is valid
467 *
468 * @adev: amdgpu_device pointer
469 * @vm: requested vm
470 * @start: start of GPU address range
471 * @end: end of GPU address range
472 *
473 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100474 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400476 */
477int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
478 struct amdgpu_vm *vm)
479{
Christian König2d55e452016-02-08 17:37:38 +0100480 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481 struct amdgpu_bo *pd = vm->page_directory;
482 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
483 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
484 uint64_t last_pde = ~0, last_pt = ~0;
485 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100486 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800487 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800488 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800489
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490 int r;
491
Christian König2d55e452016-02-08 17:37:38 +0100492 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
493
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400494 /* padding, etc. */
495 ndw = 64;
496
497 /* assume the worst case */
498 ndw += vm->max_pde_used * 6;
499
Christian Königd71518b2016-02-01 12:20:25 +0100500 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
501 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400502 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100503
504 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505
506 /* walk over the address space and update the page directory */
507 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100508 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509 uint64_t pde, pt;
510
511 if (bo == NULL)
512 continue;
513
514 pt = amdgpu_bo_gpu_offset(bo);
515 if (vm->page_tables[pt_idx].addr == pt)
516 continue;
517 vm->page_tables[pt_idx].addr = pt;
518
519 pde = pd_addr + pt_idx * 8;
520 if (((last_pde + 8 * count) != pde) ||
521 ((last_pt + incr * count) != pt)) {
522
523 if (count) {
Christian König9ab21462015-11-30 14:19:26 +0100524 amdgpu_vm_update_pages(adev, NULL, 0, ib,
525 last_pde, last_pt,
526 count, incr,
527 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528 }
529
530 count = 1;
531 last_pde = pde;
532 last_pt = pt;
533 } else {
534 ++count;
535 }
536 }
537
538 if (count)
Christian König9ab21462015-11-30 14:19:26 +0100539 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
540 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800542 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100543 amdgpu_ring_pad_ib(ring, ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100544 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
545 AMDGPU_FENCE_OWNER_VM);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800546 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100547 r = amdgpu_job_submit(job, ring, &vm->entity,
548 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800549 if (r)
550 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200551
Chunming Zhou4af9f072015-08-03 12:57:31 +0800552 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200553 fence_put(vm->page_directory_fence);
554 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800555 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800556
Christian Königd71518b2016-02-01 12:20:25 +0100557 } else {
558 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800559 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560
561 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800562
563error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100564 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800565 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400566}
567
568/**
569 * amdgpu_vm_frag_ptes - add fragment information to PTEs
570 *
571 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100572 * @gtt: GART instance to use for mapping
573 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574 * @ib: IB for the update
575 * @pe_start: first PTE to handle
576 * @pe_end: last PTE to handle
577 * @addr: addr those PTEs should point to
578 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 */
580static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100581 struct amdgpu_gart *gtt,
582 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 struct amdgpu_ib *ib,
584 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100585 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586{
587 /**
588 * The MC L1 TLB supports variable sized pages, based on a fragment
589 * field in the PTE. When this field is set to a non-zero value, page
590 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
591 * flags are considered valid for all PTEs within the fragment range
592 * and corresponding mappings are assumed to be physically contiguous.
593 *
594 * The L1 TLB can store a single PTE for the whole fragment,
595 * significantly increasing the space available for translation
596 * caching. This leads to large improvements in throughput when the
597 * TLB is under pressure.
598 *
599 * The L2 TLB distributes small and large fragments into two
600 * asymmetric partitions. The large fragment cache is significantly
601 * larger. Thus, we try to use large fragments wherever possible.
602 * Userspace can support this by aligning virtual base address and
603 * allocation size to the fragment size.
604 */
605
606 /* SI and newer are optimized for 64KB */
607 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
608 uint64_t frag_align = 0x80;
609
610 uint64_t frag_start = ALIGN(pe_start, frag_align);
611 uint64_t frag_end = pe_end & ~(frag_align - 1);
612
613 unsigned count;
614
Christian König31f6c1f2016-01-26 12:37:49 +0100615 /* Abort early if there isn't anything to do */
616 if (pe_start == pe_end)
617 return;
618
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 /* system pages are non continuously */
Christian König9ab21462015-11-30 14:19:26 +0100620 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400621
622 count = (pe_end - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100623 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
624 addr, count, AMDGPU_GPU_PAGE_SIZE,
625 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 return;
627 }
628
629 /* handle the 4K area at the beginning */
630 if (pe_start != frag_start) {
631 count = (frag_start - pe_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100632 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
633 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634 addr += AMDGPU_GPU_PAGE_SIZE * count;
635 }
636
637 /* handle the area in the middle */
638 count = (frag_end - frag_start) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100639 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
640 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641
642 /* handle the 4K area at the end */
643 if (frag_end != pe_end) {
644 addr += AMDGPU_GPU_PAGE_SIZE * count;
645 count = (pe_end - frag_end) / 8;
Christian König9ab21462015-11-30 14:19:26 +0100646 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
647 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 }
649}
650
651/**
652 * amdgpu_vm_update_ptes - make sure that page tables are valid
653 *
654 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100655 * @gtt: GART instance to use for mapping
656 * @gtt_flags: GTT hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400657 * @vm: requested vm
658 * @start: start of GPU address range
659 * @end: end of GPU address range
660 * @dst: destination address to map to
661 * @flags: mapping flags
662 *
Christian König8843dbb2016-01-26 12:17:11 +0100663 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 */
Christian Königa1e08d32016-01-26 11:40:46 +0100665static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
666 struct amdgpu_gart *gtt,
667 uint32_t gtt_flags,
668 struct amdgpu_vm *vm,
669 struct amdgpu_ib *ib,
670 uint64_t start, uint64_t end,
671 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672{
Christian König31f6c1f2016-01-26 12:37:49 +0100673 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
674
675 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 uint64_t addr;
677
678 /* walk over the address space and update the page tables */
679 for (addr = start; addr < end; ) {
680 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100681 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100683 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684
685 if ((addr & ~mask) == (end & ~mask))
686 nptes = end - addr;
687 else
688 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
689
Christian König31f6c1f2016-01-26 12:37:49 +0100690 pe_start = amdgpu_bo_gpu_offset(pt);
691 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692
Christian König31f6c1f2016-01-26 12:37:49 +0100693 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694
Christian König31f6c1f2016-01-26 12:37:49 +0100695 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
696 last_pe_start, last_pe_end,
697 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698
Christian König31f6c1f2016-01-26 12:37:49 +0100699 last_pe_start = pe_start;
700 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 last_dst = dst;
702 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100703 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 }
705
706 addr += nptes;
707 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
708 }
709
Christian König31f6c1f2016-01-26 12:37:49 +0100710 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
711 last_pe_start, last_pe_end,
712 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713}
714
715/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
717 *
718 * @adev: amdgpu_device pointer
Christian König9ab21462015-11-30 14:19:26 +0100719 * @gtt: GART instance to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 * @gtt_flags: flags as they are used for GTT
Christian Königa14faa62016-01-25 14:27:31 +0100721 * @vm: requested vm
722 * @start: start of mapped range
723 * @last: last mapped entry
724 * @flags: flags for the entries
725 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726 * @fence: optional resulting fence
727 *
Christian Königa14faa62016-01-25 14:27:31 +0100728 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 */
731static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König9ab21462015-11-30 14:19:26 +0100732 struct amdgpu_gart *gtt,
733 uint32_t gtt_flags,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100735 uint64_t start, uint64_t last,
736 uint32_t flags, uint64_t addr,
737 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738{
Christian König2d55e452016-02-08 17:37:38 +0100739 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100740 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100742 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800743 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800744 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400745 int r;
746
Christian König2d55e452016-02-08 17:37:38 +0100747 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
748
Christian Königa1e08d32016-01-26 11:40:46 +0100749 /* sync to everything on unmapping */
750 if (!(flags & AMDGPU_PTE_VALID))
751 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
752
Christian Königa14faa62016-01-25 14:27:31 +0100753 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400754
755 /*
756 * reserve space for one command every (1 << BLOCK_SIZE)
757 * entries or 2k dwords (whatever is smaller)
758 */
759 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
760
761 /* padding, etc. */
762 ndw = 64;
763
Christian König9ab21462015-11-30 14:19:26 +0100764 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765 /* only copy commands needed */
766 ndw += ncmds * 7;
767
Christian König9ab21462015-11-30 14:19:26 +0100768 } else if (gtt) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769 /* header for write data commands */
770 ndw += ncmds * 4;
771
772 /* body of write data command */
773 ndw += nptes * 2;
774
775 } else {
776 /* set page commands needed */
777 ndw += ncmds * 10;
778
779 /* two extra commands for begin/end of fragment */
780 ndw += 2 * 10;
781 }
782
Christian Königd71518b2016-02-01 12:20:25 +0100783 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
784 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100786
787 ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800788
Christian Könige86f9ce2016-02-08 12:13:05 +0100789 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100790 owner);
791 if (r)
792 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400793
Christian Königa1e08d32016-01-26 11:40:46 +0100794 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
795 if (r)
796 goto error_free;
797
798 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
799 addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400800
Christian König9e5d53092016-01-31 12:20:55 +0100801 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800802 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100803 r = amdgpu_job_submit(job, ring, &vm->entity,
804 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800805 if (r)
806 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400807
Christian Königbf60efd2015-09-04 10:47:56 +0200808 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800809 if (fence) {
810 fence_put(*fence);
811 *fence = fence_get(f);
812 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800813 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800815
816error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100817 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800818 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819}
820
821/**
Christian Königa14faa62016-01-25 14:27:31 +0100822 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
823 *
824 * @adev: amdgpu_device pointer
825 * @gtt: GART instance to use for mapping
826 * @vm: requested vm
827 * @mapping: mapped range and flags to use for the update
828 * @addr: addr to set the area to
829 * @gtt_flags: flags as they are used for GTT
830 * @fence: optional resulting fence
831 *
832 * Split the mapping into smaller chunks so that each update fits
833 * into a SDMA IB.
834 * Returns 0 for success, -EINVAL for failure.
835 */
836static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
837 struct amdgpu_gart *gtt,
838 uint32_t gtt_flags,
839 struct amdgpu_vm *vm,
840 struct amdgpu_bo_va_mapping *mapping,
841 uint64_t addr, struct fence **fence)
842{
843 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
844
845 uint64_t start = mapping->it.start;
846 uint32_t flags = gtt_flags;
847 int r;
848
849 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
850 * but in case of something, we filter the flags in first place
851 */
852 if (!(mapping->flags & AMDGPU_PTE_READABLE))
853 flags &= ~AMDGPU_PTE_READABLE;
854 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
855 flags &= ~AMDGPU_PTE_WRITEABLE;
856
857 trace_amdgpu_vm_bo_update(mapping);
858
859 addr += mapping->offset;
860
861 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
862 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
863 start, mapping->it.last,
864 flags, addr, fence);
865
866 while (start != mapping->it.last + 1) {
867 uint64_t last;
868
869 last = min((uint64_t)mapping->it.last, start + max_size);
870 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
871 start, last, flags, addr,
872 fence);
873 if (r)
874 return r;
875
876 start = last + 1;
877 addr += max_size;
878 }
879
880 return 0;
881}
882
883/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
885 *
886 * @adev: amdgpu_device pointer
887 * @bo_va: requested BO and VM object
888 * @mem: ttm mem
889 *
890 * Fill in the page table entries for @bo_va.
891 * Returns 0 for success, -EINVAL for failure.
892 *
893 * Object have to be reserved and mutex must be locked!
894 */
895int amdgpu_vm_bo_update(struct amdgpu_device *adev,
896 struct amdgpu_bo_va *bo_va,
897 struct ttm_mem_reg *mem)
898{
899 struct amdgpu_vm *vm = bo_va->vm;
900 struct amdgpu_bo_va_mapping *mapping;
Christian König9ab21462015-11-30 14:19:26 +0100901 struct amdgpu_gart *gtt = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400902 uint32_t flags;
903 uint64_t addr;
904 int r;
905
906 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200907 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100908 switch (mem->mem_type) {
909 case TTM_PL_TT:
910 gtt = &bo_va->bo->adev->gart;
911 break;
912
913 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400914 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100915 break;
916
917 default:
918 break;
919 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400920 } else {
921 addr = 0;
922 }
923
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400924 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
925
Christian König7fc11952015-07-30 11:53:42 +0200926 spin_lock(&vm->status_lock);
927 if (!list_empty(&bo_va->vm_status))
928 list_splice_init(&bo_va->valids, &bo_va->invalids);
929 spin_unlock(&vm->status_lock);
930
931 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa14faa62016-01-25 14:27:31 +0100932 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
933 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400934 if (r)
935 return r;
936 }
937
Christian Königd6c10f62015-09-28 12:00:23 +0200938 if (trace_amdgpu_vm_bo_mapping_enabled()) {
939 list_for_each_entry(mapping, &bo_va->valids, list)
940 trace_amdgpu_vm_bo_mapping(mapping);
941
942 list_for_each_entry(mapping, &bo_va->invalids, list)
943 trace_amdgpu_vm_bo_mapping(mapping);
944 }
945
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800947 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200949 if (!mem)
950 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951 spin_unlock(&vm->status_lock);
952
953 return 0;
954}
955
956/**
957 * amdgpu_vm_clear_freed - clear freed BOs in the PT
958 *
959 * @adev: amdgpu_device pointer
960 * @vm: requested vm
961 *
962 * Make sure all freed BOs are cleared in the PT.
963 * Returns 0 for success.
964 *
965 * PTs have to be reserved and mutex must be locked!
966 */
967int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
968 struct amdgpu_vm *vm)
969{
970 struct amdgpu_bo_va_mapping *mapping;
971 int r;
972
jimqu81d75a32015-12-04 17:17:00 +0800973 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 while (!list_empty(&vm->freed)) {
975 mapping = list_first_entry(&vm->freed,
976 struct amdgpu_bo_va_mapping, list);
977 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800978 spin_unlock(&vm->freed_lock);
Christian Königa14faa62016-01-25 14:27:31 +0100979 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
980 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 kfree(mapping);
982 if (r)
983 return r;
984
jimqu81d75a32015-12-04 17:17:00 +0800985 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 }
jimqu81d75a32015-12-04 17:17:00 +0800987 spin_unlock(&vm->freed_lock);
988
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989 return 0;
990
991}
992
993/**
994 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
995 *
996 * @adev: amdgpu_device pointer
997 * @vm: requested vm
998 *
999 * Make sure all invalidated BOs are cleared in the PT.
1000 * Returns 0 for success.
1001 *
1002 * PTs have to be reserved and mutex must be locked!
1003 */
1004int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001005 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006{
monk.liucfe2c972015-05-26 15:01:54 +08001007 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001008 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009
1010 spin_lock(&vm->status_lock);
1011 while (!list_empty(&vm->invalidated)) {
1012 bo_va = list_first_entry(&vm->invalidated,
1013 struct amdgpu_bo_va, vm_status);
1014 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001015 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001017 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001018 if (r)
1019 return r;
1020
1021 spin_lock(&vm->status_lock);
1022 }
1023 spin_unlock(&vm->status_lock);
1024
monk.liucfe2c972015-05-26 15:01:54 +08001025 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001026 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001027
1028 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029}
1030
1031/**
1032 * amdgpu_vm_bo_add - add a bo to a specific vm
1033 *
1034 * @adev: amdgpu_device pointer
1035 * @vm: requested vm
1036 * @bo: amdgpu buffer object
1037 *
Christian König8843dbb2016-01-26 12:17:11 +01001038 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001039 * Add @bo to the list of bos associated with the vm
1040 * Returns newly added bo_va or NULL for failure
1041 *
1042 * Object has to be reserved!
1043 */
1044struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1045 struct amdgpu_vm *vm,
1046 struct amdgpu_bo *bo)
1047{
1048 struct amdgpu_bo_va *bo_va;
1049
1050 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1051 if (bo_va == NULL) {
1052 return NULL;
1053 }
1054 bo_va->vm = vm;
1055 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001056 bo_va->ref_count = 1;
1057 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001058 INIT_LIST_HEAD(&bo_va->valids);
1059 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001060 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001061 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001062 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001063
1064 return bo_va;
1065}
1066
1067/**
1068 * amdgpu_vm_bo_map - map bo inside a vm
1069 *
1070 * @adev: amdgpu_device pointer
1071 * @bo_va: bo_va to store the address
1072 * @saddr: where to map the BO
1073 * @offset: requested offset in the BO
1074 * @flags: attributes of pages (read/write/valid/etc.)
1075 *
1076 * Add a mapping of the BO at the specefied addr into the VM.
1077 * Returns 0 for success, error for failure.
1078 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001079 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080 */
1081int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1082 struct amdgpu_bo_va *bo_va,
1083 uint64_t saddr, uint64_t offset,
1084 uint64_t size, uint32_t flags)
1085{
1086 struct amdgpu_bo_va_mapping *mapping;
1087 struct amdgpu_vm *vm = bo_va->vm;
1088 struct interval_tree_node *it;
1089 unsigned last_pfn, pt_idx;
1090 uint64_t eaddr;
1091 int r;
1092
Christian König0be52de2015-05-18 14:37:27 +02001093 /* validate the parameters */
1094 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001095 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001096 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001097
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001099 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001100 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001102
1103 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001104 if (last_pfn >= adev->vm_manager.max_pfn) {
1105 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 return -EINVAL;
1108 }
1109
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001110 saddr /= AMDGPU_GPU_PAGE_SIZE;
1111 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1112
Chunming Zhouc25867d2015-11-13 13:32:01 +08001113 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001114 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001115 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 if (it) {
1117 struct amdgpu_bo_va_mapping *tmp;
1118 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1119 /* bo and tmp overlap, invalid addr */
1120 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1121 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1122 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001123 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001124 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 }
1126
1127 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1128 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001130 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001131 }
1132
1133 INIT_LIST_HEAD(&mapping->list);
1134 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001135 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 mapping->offset = offset;
1137 mapping->flags = flags;
1138
Chunming Zhou69b576a2015-11-18 11:17:39 +08001139 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001140 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001141 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001142 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001143 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001144 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001145 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001147 /* Make sure the page tables are allocated */
1148 saddr >>= amdgpu_vm_block_size;
1149 eaddr >>= amdgpu_vm_block_size;
1150
1151 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1152
1153 if (eaddr > vm->max_pde_used)
1154 vm->max_pde_used = eaddr;
1155
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156 /* walk over the address space and allocate the page tables */
1157 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001158 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001159 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160 struct amdgpu_bo *pt;
1161
Christian Königee1782c2015-12-11 21:01:23 +01001162 entry = &vm->page_tables[pt_idx].entry;
1163 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001164 continue;
1165
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001166 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1167 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001168 AMDGPU_GEM_DOMAIN_VRAM,
1169 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001170 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001171 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001173
Christian König82b9c552015-11-27 16:49:00 +01001174 /* Keep a reference to the page table to avoid freeing
1175 * them up in the wrong order.
1176 */
1177 pt->parent = amdgpu_bo_ref(vm->page_directory);
1178
Christian König2bd9ccf2016-02-01 12:53:58 +01001179 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001180 if (r) {
1181 amdgpu_bo_unref(&pt);
1182 goto error_free;
1183 }
1184
Christian Königee1782c2015-12-11 21:01:23 +01001185 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001186 entry->priority = 0;
1187 entry->tv.bo = &entry->robj->tbo;
1188 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001189 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001190 }
1191
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 return 0;
1193
1194error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001196 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001198 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001199 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001200 kfree(mapping);
1201
Chunming Zhouf48b2652015-10-16 14:06:19 +08001202error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001203 return r;
1204}
1205
1206/**
1207 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1208 *
1209 * @adev: amdgpu_device pointer
1210 * @bo_va: bo_va to remove the address from
1211 * @saddr: where to the BO is mapped
1212 *
1213 * Remove a mapping of the BO at the specefied addr from the VM.
1214 * Returns 0 for success, error for failure.
1215 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001216 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217 */
1218int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1219 struct amdgpu_bo_va *bo_va,
1220 uint64_t saddr)
1221{
1222 struct amdgpu_bo_va_mapping *mapping;
1223 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001224 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225
Christian König6c7fc502015-06-05 20:56:17 +02001226 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001227 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001228 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001229 if (mapping->it.start == saddr)
1230 break;
1231 }
1232
Christian König7fc11952015-07-30 11:53:42 +02001233 if (&mapping->list == &bo_va->valids) {
1234 valid = false;
1235
1236 list_for_each_entry(mapping, &bo_va->invalids, list) {
1237 if (mapping->it.start == saddr)
1238 break;
1239 }
1240
Chunming Zhou69b576a2015-11-18 11:17:39 +08001241 if (&mapping->list == &bo_va->invalids) {
1242 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001243 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001244 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001245 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001246 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001248 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001249 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001250 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001251 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252
jimqu81d75a32015-12-04 17:17:00 +08001253 if (valid) {
1254 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001255 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001256 spin_unlock(&vm->freed_lock);
1257 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001258 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001259 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260
1261 return 0;
1262}
1263
1264/**
1265 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1266 *
1267 * @adev: amdgpu_device pointer
1268 * @bo_va: requested bo_va
1269 *
Christian König8843dbb2016-01-26 12:17:11 +01001270 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001271 *
1272 * Object have to be reserved!
1273 */
1274void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1275 struct amdgpu_bo_va *bo_va)
1276{
1277 struct amdgpu_bo_va_mapping *mapping, *next;
1278 struct amdgpu_vm *vm = bo_va->vm;
1279
1280 list_del(&bo_va->bo_list);
1281
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 spin_lock(&vm->status_lock);
1283 list_del(&bo_va->vm_status);
1284 spin_unlock(&vm->status_lock);
1285
Christian König7fc11952015-07-30 11:53:42 +02001286 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001287 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001288 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001290 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001291 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001292 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001293 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001294 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 }
Christian König7fc11952015-07-30 11:53:42 +02001296 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1297 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001298 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001299 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001300 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001301 kfree(mapping);
1302 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001303 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001304 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001305 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306}
1307
1308/**
1309 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1310 *
1311 * @adev: amdgpu_device pointer
1312 * @vm: requested vm
1313 * @bo: amdgpu buffer object
1314 *
Christian König8843dbb2016-01-26 12:17:11 +01001315 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001316 */
1317void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1318 struct amdgpu_bo *bo)
1319{
1320 struct amdgpu_bo_va *bo_va;
1321
1322 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001323 spin_lock(&bo_va->vm->status_lock);
1324 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001325 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001326 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001327 }
1328}
1329
1330/**
1331 * amdgpu_vm_init - initialize a vm instance
1332 *
1333 * @adev: amdgpu_device pointer
1334 * @vm: requested vm
1335 *
Christian König8843dbb2016-01-26 12:17:11 +01001336 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001337 */
1338int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1339{
1340 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1341 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001342 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001343 unsigned ring_instance;
1344 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001345 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001346 int i, r;
1347
1348 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001349 vm->ids[i].mgr_id = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001350 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001351 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001352 vm->va = RB_ROOT;
1353 spin_lock_init(&vm->status_lock);
1354 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001355 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001357 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001358 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001359 pd_size = amdgpu_vm_directory_size(adev);
1360 pd_entries = amdgpu_vm_num_pdes(adev);
1361
1362 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001363 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001364 if (vm->page_tables == NULL) {
1365 DRM_ERROR("Cannot allocate memory for page table array\n");
1366 return -ENOMEM;
1367 }
1368
Christian König2bd9ccf2016-02-01 12:53:58 +01001369 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001370
1371 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1372 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1373 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001374 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1375 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1376 rq, amdgpu_sched_jobs);
1377 if (r)
1378 return r;
1379
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001380 vm->page_directory_fence = NULL;
1381
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001382 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001383 AMDGPU_GEM_DOMAIN_VRAM,
1384 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001385 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001387 goto error_free_sched_entity;
1388
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001389 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001390 if (r)
1391 goto error_free_page_directory;
1392
1393 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001394 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001395 if (r)
1396 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397
1398 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001399
1400error_free_page_directory:
1401 amdgpu_bo_unref(&vm->page_directory);
1402 vm->page_directory = NULL;
1403
1404error_free_sched_entity:
1405 amd_sched_entity_fini(&ring->sched, &vm->entity);
1406
1407 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001408}
1409
1410/**
1411 * amdgpu_vm_fini - tear down a vm instance
1412 *
1413 * @adev: amdgpu_device pointer
1414 * @vm: requested vm
1415 *
Christian König8843dbb2016-01-26 12:17:11 +01001416 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417 * Unbind the VM and remove all bos from the vm bo list
1418 */
1419void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1420{
1421 struct amdgpu_bo_va_mapping *mapping, *tmp;
1422 int i;
1423
Christian König2d55e452016-02-08 17:37:38 +01001424 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001425
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001426 if (!RB_EMPTY_ROOT(&vm->va)) {
1427 dev_err(adev->dev, "still active bo inside vm\n");
1428 }
1429 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1430 list_del(&mapping->list);
1431 interval_tree_remove(&mapping->it, &vm->va);
1432 kfree(mapping);
1433 }
1434 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1435 list_del(&mapping->list);
1436 kfree(mapping);
1437 }
1438
1439 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001440 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001441 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001442
1443 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001444 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001445 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König4ff37a82016-02-26 16:18:26 +01001446 struct amdgpu_vm_id *id = &vm->ids[i];
Christian König1c16c0a2015-11-14 21:31:40 +01001447
Christian König4ff37a82016-02-26 16:18:26 +01001448 if (id->mgr_id)
1449 atomic_long_cmpxchg(&id->mgr_id->owner,
1450 (long)id, 0);
1451 fence_put(id->flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001452 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001453}
Christian Königea89f8c2015-11-15 20:52:06 +01001454
1455/**
Christian Königa9a78b32016-01-21 10:19:11 +01001456 * amdgpu_vm_manager_init - init the VM manager
1457 *
1458 * @adev: amdgpu_device pointer
1459 *
1460 * Initialize the VM manager structures
1461 */
1462void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1463{
1464 unsigned i;
1465
1466 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1467
1468 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001469 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1470 amdgpu_vm_reset_id(adev, i);
Christian Königa9a78b32016-01-21 10:19:11 +01001471 list_add_tail(&adev->vm_manager.ids[i].list,
1472 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001473 }
Christian König2d55e452016-02-08 17:37:38 +01001474
1475 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001476}
1477
1478/**
Christian Königea89f8c2015-11-15 20:52:06 +01001479 * amdgpu_vm_manager_fini - cleanup VM manager
1480 *
1481 * @adev: amdgpu_device pointer
1482 *
1483 * Cleanup the VM manager and free resources.
1484 */
1485void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1486{
1487 unsigned i;
1488
1489 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001490 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001491}