blob: 252445f578f6c2ce3aa41e7728a91d0ca8a4cf12 [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;
Christian König2f568db2016-02-23 12:36:59 +010098 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +010099 list_add(&entry->tv.head, validated);
100}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101
Christian König56467eb2015-12-11 15:16:32 +0100102/**
Christian Königee1782c2015-12-11 21:01:23 +0100103 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100104 *
105 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100106 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107 *
Christian Königee1782c2015-12-11 21:01:23 +0100108 * Add the page directory to the BO duplicates list
109 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110 */
Christian Königee1782c2015-12-11 21:01:23 +0100111void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112{
Christian Königee1782c2015-12-11 21:01:23 +0100113 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114
115 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100116 for (i = 0; i <= vm->max_pde_used; ++i) {
117 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118
Christian Königee1782c2015-12-11 21:01:23 +0100119 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 continue;
121
Christian Königee1782c2015-12-11 21:01:23 +0100122 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 }
Christian Königeceb8a12016-01-11 15:35:21 +0100124
125}
126
127/**
128 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
129 *
130 * @adev: amdgpu device instance
131 * @vm: vm providing the BOs
132 *
133 * Move the PT BOs to the tail of the LRU.
134 */
135void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
136 struct amdgpu_vm *vm)
137{
138 struct ttm_bo_global *glob = adev->mman.bdev.glob;
139 unsigned i;
140
141 spin_lock(&glob->lru_lock);
142 for (i = 0; i <= vm->max_pde_used; ++i) {
143 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
144
145 if (!entry->robj)
146 continue;
147
148 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
149 }
150 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151}
152
153/**
154 * amdgpu_vm_grab_id - allocate the next free VMID
155 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200157 * @ring: ring we want to submit job to
158 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100159 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 *
Christian König7f8a5292015-07-20 16:09:40 +0200161 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162 */
Christian König7f8a5292015-07-20 16:09:40 +0200163int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100164 struct amdgpu_sync *sync, struct fence *fence,
165 unsigned *vm_id, uint64_t *vm_pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166{
Christian König4ff37a82016-02-26 16:18:26 +0100167 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 struct amdgpu_device *adev = ring->adev;
Christian Königbcb1ba32016-03-08 15:40:11 +0100169 struct amdgpu_vm_id *id = vm->ids[ring->idx];
Christian König4ff37a82016-02-26 16:18:26 +0100170 struct fence *updates = sync->last_vm_update;
Christian Königa9a78b32016-01-21 10:19:11 +0100171 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172
Christian König94dd0a42016-01-18 17:01:42 +0100173 mutex_lock(&adev->vm_manager.lock);
174
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175 /* check if the id is still valid */
Christian Königbcb1ba32016-03-08 15:40:11 +0100176 if (id) {
Christian König4ff37a82016-02-26 16:18:26 +0100177 struct fence *flushed = id->flushed_updates;
Christian Königbcb1ba32016-03-08 15:40:11 +0100178 long owner = atomic_long_read(&id->owner);
179 bool usable = pd_addr == id->pd_gpu_addr;
Christian König1c16c0a2015-11-14 21:31:40 +0100180
Christian Königbcb1ba32016-03-08 15:40:11 +0100181 if (owner != (long)&vm->ids[ring->idx])
182 usable = false;
183 else if (!flushed)
184 usable = false;
Christian König4ff37a82016-02-26 16:18:26 +0100185 else if (!updates)
Christian Königbcb1ba32016-03-08 15:40:11 +0100186 usable = true;
Christian König4ff37a82016-02-26 16:18:26 +0100187 else
Christian Königbcb1ba32016-03-08 15:40:11 +0100188 usable = !fence_is_later(updates, flushed);
Christian Königa9a78b32016-01-21 10:19:11 +0100189
Christian Königbcb1ba32016-03-08 15:40:11 +0100190 if (usable) {
Christian König4ff37a82016-02-26 16:18:26 +0100191
Christian König832a9022016-02-15 12:33:02 +0100192 r = amdgpu_sync_fence(ring->adev, sync, id->first);
193 if (r)
194 goto error;
Christian Königa8bd1be2016-03-03 10:50:01 +0100195
Christian König832a9022016-02-15 12:33:02 +0100196 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
197 if (r)
198 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100199
Christian Königbcb1ba32016-03-08 15:40:11 +0100200 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König4ff37a82016-02-26 16:18:26 +0100201
Christian Königbcb1ba32016-03-08 15:40:11 +0100202 *vm_id = id - adev->vm_manager.ids;
Christian König4ff37a82016-02-26 16:18:26 +0100203 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
Christian König22073fe2016-02-26 16:18:36 +0100204 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id,
205 *vm_pd_addr);
Christian Königa9a78b32016-01-21 10:19:11 +0100206
Christian König94dd0a42016-01-18 17:01:42 +0100207 mutex_unlock(&adev->vm_manager.lock);
Christian König1c16c0a2015-11-14 21:31:40 +0100208 return 0;
209 }
Christian König39ff8442015-09-28 12:01:20 +0200210 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211
Christian Königbcb1ba32016-03-08 15:40:11 +0100212 id = list_first_entry(&adev->vm_manager.ids_lru,
213 struct amdgpu_vm_id,
214 list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215
Christian König832a9022016-02-15 12:33:02 +0100216 if (!amdgpu_sync_is_idle(&id->active)) {
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800217 struct list_head *head = &adev->vm_manager.ids_lru;
Christian König832a9022016-02-15 12:33:02 +0100218 struct amdgpu_vm_id *tmp;
Christian Königbcb1ba32016-03-08 15:40:11 +0100219
220 list_for_each_entry_safe(id, tmp, &adev->vm_manager.ids_lru,
221 list) {
Christian König832a9022016-02-15 12:33:02 +0100222 if (amdgpu_sync_is_idle(&id->active)) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100223 list_move(&id->list, head);
224 head = &id->list;
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800225 }
226 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100227 id = list_first_entry(&adev->vm_manager.ids_lru,
228 struct amdgpu_vm_id,
229 list);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800230 }
231
Christian König832a9022016-02-15 12:33:02 +0100232 r = amdgpu_sync_cycle_fences(sync, &id->active, fence);
233 if (r)
234 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100235
Christian König832a9022016-02-15 12:33:02 +0100236 fence_put(id->first);
237 id->first = fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100238
Christian König41d9eb22016-03-01 16:46:18 +0100239 fence_put(id->last_flush);
240 id->last_flush = NULL;
241
Christian König832a9022016-02-15 12:33:02 +0100242 fence_put(id->flushed_updates);
243 id->flushed_updates = fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100244
Christian König832a9022016-02-15 12:33:02 +0100245 id->pd_gpu_addr = pd_addr;
Christian König4ff37a82016-02-26 16:18:26 +0100246
Christian König832a9022016-02-15 12:33:02 +0100247 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
248 atomic_long_set(&id->owner, (long)id);
249 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250
Christian König832a9022016-02-15 12:33:02 +0100251 *vm_id = id - adev->vm_manager.ids;
252 *vm_pd_addr = pd_addr;
253 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
254
255error:
Christian König94dd0a42016-01-18 17:01:42 +0100256 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100257 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400258}
259
260/**
261 * amdgpu_vm_flush - hardware flush the vm
262 *
263 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100264 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100265 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 *
Christian König4ff37a82016-02-26 16:18:26 +0100267 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268 */
Christian König41d9eb22016-03-01 16:46:18 +0100269int amdgpu_vm_flush(struct amdgpu_ring *ring,
270 unsigned vm_id, uint64_t pd_addr,
271 uint32_t gds_base, uint32_t gds_size,
272 uint32_t gws_base, uint32_t gws_size,
273 uint32_t oa_base, uint32_t oa_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400274{
Christian König971fe9a92016-03-01 15:09:25 +0100275 struct amdgpu_device *adev = ring->adev;
Christian Königbcb1ba32016-03-08 15:40:11 +0100276 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100277 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Christian Königbcb1ba32016-03-08 15:40:11 +0100278 id->gds_base != gds_base ||
279 id->gds_size != gds_size ||
280 id->gws_base != gws_base ||
281 id->gws_size != gws_size ||
282 id->oa_base != oa_base ||
283 id->oa_size != oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100284 int r;
Christian Königd564a062016-03-01 15:51:53 +0100285
286 if (ring->funcs->emit_pipeline_sync && (
287 pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed))
288 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100289
Christian König4ff37a82016-02-26 16:18:26 +0100290 if (pd_addr != AMDGPU_VM_NO_FLUSH) {
Christian König41d9eb22016-03-01 16:46:18 +0100291 struct fence *fence;
292
Christian Königcffadc82016-03-01 13:34:49 +0100293 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
294 amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100295 r = amdgpu_fence_emit(ring, &fence);
296 if (r)
297 return r;
298
299 mutex_lock(&adev->vm_manager.lock);
300 fence_put(id->last_flush);
301 id->last_flush = fence;
302 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 }
Christian Königcffadc82016-03-01 13:34:49 +0100304
Christian Königd564a062016-03-01 15:51:53 +0100305 if (gds_switch_needed) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100306 id->gds_base = gds_base;
307 id->gds_size = gds_size;
308 id->gws_base = gws_base;
309 id->gws_size = gws_size;
310 id->oa_base = oa_base;
311 id->oa_size = oa_size;
Christian Königcffadc82016-03-01 13:34:49 +0100312 amdgpu_ring_emit_gds_switch(ring, vm_id,
313 gds_base, gds_size,
314 gws_base, gws_size,
315 oa_base, oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100316 }
Christian König41d9eb22016-03-01 16:46:18 +0100317
318 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100319}
320
321/**
322 * amdgpu_vm_reset_id - reset VMID to zero
323 *
324 * @adev: amdgpu device structure
325 * @vm_id: vmid number to use
326 *
327 * Reset saved GDW, GWS and OA to force switch on next flush.
328 */
329void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
330{
Christian Königbcb1ba32016-03-08 15:40:11 +0100331 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100332
Christian Königbcb1ba32016-03-08 15:40:11 +0100333 id->gds_base = 0;
334 id->gds_size = 0;
335 id->gws_base = 0;
336 id->gws_size = 0;
337 id->oa_base = 0;
338 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339}
340
341/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
343 *
344 * @vm: requested vm
345 * @bo: requested buffer object
346 *
Christian König8843dbb2016-01-26 12:17:11 +0100347 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 * Search inside the @bos vm list for the requested vm
349 * Returns the found bo_va or NULL if none is found
350 *
351 * Object has to be reserved!
352 */
353struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
354 struct amdgpu_bo *bo)
355{
356 struct amdgpu_bo_va *bo_va;
357
358 list_for_each_entry(bo_va, &bo->va, bo_list) {
359 if (bo_va->vm == vm) {
360 return bo_va;
361 }
362 }
363 return NULL;
364}
365
366/**
367 * amdgpu_vm_update_pages - helper to call the right asic function
368 *
369 * @adev: amdgpu_device pointer
Christian Königfa3ab3c2016-03-18 21:00:35 +0100370 * @src: address where to copy page table entries from
371 * @pages_addr: DMA addresses to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400372 * @ib: indirect buffer to fill with commands
373 * @pe: addr of the page entry
374 * @addr: dst addr to write into pe
375 * @count: number of page entries to update
376 * @incr: increase next addr by incr bytes
377 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378 *
379 * Traces the parameters and calls the right asic functions
380 * to setup the page table using the DMA.
381 */
382static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100383 uint64_t src,
384 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400385 struct amdgpu_ib *ib,
386 uint64_t pe, uint64_t addr,
387 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100388 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389{
390 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
391
Christian Königfa3ab3c2016-03-18 21:00:35 +0100392 if (src) {
393 src += (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400394 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
395
Christian Königfa3ab3c2016-03-18 21:00:35 +0100396 } else if (pages_addr) {
Christian Königb07c9d22015-11-30 13:26:07 +0100397 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
398 count, incr, flags);
399
400 } else if (count < 3) {
401 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
402 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400403
404 } else {
405 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
406 count, incr, flags);
407 }
408}
409
410/**
411 * amdgpu_vm_clear_bo - initially clear the page dir/table
412 *
413 * @adev: amdgpu_device pointer
414 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800415 *
416 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417 */
418static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100419 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400420 struct amdgpu_bo *bo)
421{
Christian König2d55e452016-02-08 17:37:38 +0100422 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800423 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100424 struct amdgpu_job *job;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425 unsigned entries;
426 uint64_t addr;
427 int r;
428
Christian König2d55e452016-02-08 17:37:38 +0100429 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
430
monk.liuca952612015-05-25 14:44:05 +0800431 r = reservation_object_reserve_shared(bo->tbo.resv);
432 if (r)
433 return r;
434
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400435 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
436 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800437 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400438
439 addr = amdgpu_bo_gpu_offset(bo);
440 entries = amdgpu_bo_size(bo) / 8;
441
Christian Königd71518b2016-02-01 12:20:25 +0100442 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
443 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800444 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445
Christian Königfa3ab3c2016-03-18 21:00:35 +0100446 amdgpu_vm_update_pages(adev, 0, NULL, &job->ibs[0], addr, 0, entries,
Christian Königd71518b2016-02-01 12:20:25 +0100447 0, 0);
448 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
449
450 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100451 r = amdgpu_job_submit(job, ring, &vm->entity,
452 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 if (r)
454 goto error_free;
455
Christian Königd71518b2016-02-01 12:20:25 +0100456 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800457 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800458 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800459
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100461 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800463error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 return r;
465}
466
467/**
Christian Königb07c9d22015-11-30 13:26:07 +0100468 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469 *
Christian Königb07c9d22015-11-30 13:26:07 +0100470 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471 * @addr: the unmapped addr
472 *
473 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100474 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475 */
Christian Königb07c9d22015-11-30 13:26:07 +0100476uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400477{
478 uint64_t result;
479
Christian Königb07c9d22015-11-30 13:26:07 +0100480 if (pages_addr) {
481 /* page table offset */
482 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400483
Christian Königb07c9d22015-11-30 13:26:07 +0100484 /* in case cpu page size != gpu page size*/
485 result |= addr & (~PAGE_MASK);
486
487 } else {
488 /* No mapping required */
489 result = addr;
490 }
491
492 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493
494 return result;
495}
496
497/**
498 * amdgpu_vm_update_pdes - make sure that page directory is valid
499 *
500 * @adev: amdgpu_device pointer
501 * @vm: requested vm
502 * @start: start of GPU address range
503 * @end: end of GPU address range
504 *
505 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100506 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508 */
509int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
510 struct amdgpu_vm *vm)
511{
Christian König2d55e452016-02-08 17:37:38 +0100512 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 struct amdgpu_bo *pd = vm->page_directory;
514 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
515 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
516 uint64_t last_pde = ~0, last_pt = ~0;
517 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100518 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800519 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800520 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800521
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 int r;
523
Christian König2d55e452016-02-08 17:37:38 +0100524 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
525
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526 /* padding, etc. */
527 ndw = 64;
528
529 /* assume the worst case */
530 ndw += vm->max_pde_used * 6;
531
Christian Königd71518b2016-02-01 12:20:25 +0100532 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
533 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100535
536 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537
538 /* walk over the address space and update the page directory */
539 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100540 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 uint64_t pde, pt;
542
543 if (bo == NULL)
544 continue;
545
546 pt = amdgpu_bo_gpu_offset(bo);
547 if (vm->page_tables[pt_idx].addr == pt)
548 continue;
549 vm->page_tables[pt_idx].addr = pt;
550
551 pde = pd_addr + pt_idx * 8;
552 if (((last_pde + 8 * count) != pde) ||
553 ((last_pt + incr * count) != pt)) {
554
555 if (count) {
Christian Königfa3ab3c2016-03-18 21:00:35 +0100556 amdgpu_vm_update_pages(adev, 0, NULL, ib,
Christian König9ab21462015-11-30 14:19:26 +0100557 last_pde, last_pt,
558 count, incr,
559 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 }
561
562 count = 1;
563 last_pde = pde;
564 last_pt = pt;
565 } else {
566 ++count;
567 }
568 }
569
570 if (count)
Christian Königfa3ab3c2016-03-18 21:00:35 +0100571 amdgpu_vm_update_pages(adev, 0, NULL, ib, last_pde, last_pt,
Christian König9ab21462015-11-30 14:19:26 +0100572 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800574 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100575 amdgpu_ring_pad_ib(ring, ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100576 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
577 AMDGPU_FENCE_OWNER_VM);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800578 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100579 r = amdgpu_job_submit(job, ring, &vm->entity,
580 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800581 if (r)
582 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200583
Chunming Zhou4af9f072015-08-03 12:57:31 +0800584 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200585 fence_put(vm->page_directory_fence);
586 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800587 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800588
Christian Königd71518b2016-02-01 12:20:25 +0100589 } else {
590 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800591 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592
593 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800594
595error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100596 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800597 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598}
599
600/**
601 * amdgpu_vm_frag_ptes - add fragment information to PTEs
602 *
603 * @adev: amdgpu_device pointer
Christian Königfa3ab3c2016-03-18 21:00:35 +0100604 * @src: address where to copy page table entries from
605 * @pages_addr: DMA addresses to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400606 * @ib: IB for the update
607 * @pe_start: first PTE to handle
608 * @pe_end: last PTE to handle
609 * @addr: addr those PTEs should point to
610 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611 */
612static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100613 uint64_t src,
614 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 struct amdgpu_ib *ib,
616 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100617 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618{
619 /**
620 * The MC L1 TLB supports variable sized pages, based on a fragment
621 * field in the PTE. When this field is set to a non-zero value, page
622 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
623 * flags are considered valid for all PTEs within the fragment range
624 * and corresponding mappings are assumed to be physically contiguous.
625 *
626 * The L1 TLB can store a single PTE for the whole fragment,
627 * significantly increasing the space available for translation
628 * caching. This leads to large improvements in throughput when the
629 * TLB is under pressure.
630 *
631 * The L2 TLB distributes small and large fragments into two
632 * asymmetric partitions. The large fragment cache is significantly
633 * larger. Thus, we try to use large fragments wherever possible.
634 * Userspace can support this by aligning virtual base address and
635 * allocation size to the fragment size.
636 */
637
638 /* SI and newer are optimized for 64KB */
639 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
640 uint64_t frag_align = 0x80;
641
642 uint64_t frag_start = ALIGN(pe_start, frag_align);
643 uint64_t frag_end = pe_end & ~(frag_align - 1);
644
645 unsigned count;
646
Christian König31f6c1f2016-01-26 12:37:49 +0100647 /* Abort early if there isn't anything to do */
648 if (pe_start == pe_end)
649 return;
650
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651 /* system pages are non continuously */
Christian Königfa3ab3c2016-03-18 21:00:35 +0100652 if (src || pages_addr || !(flags & AMDGPU_PTE_VALID) ||
653 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654
655 count = (pe_end - pe_start) / 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100656 amdgpu_vm_update_pages(adev, src, pages_addr, ib, pe_start,
Christian König9ab21462015-11-30 14:19:26 +0100657 addr, count, AMDGPU_GPU_PAGE_SIZE,
658 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 return;
660 }
661
662 /* handle the 4K area at the beginning */
663 if (pe_start != frag_start) {
664 count = (frag_start - pe_start) / 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100665 amdgpu_vm_update_pages(adev, 0, NULL, ib, pe_start, addr,
Christian König9ab21462015-11-30 14:19:26 +0100666 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 addr += AMDGPU_GPU_PAGE_SIZE * count;
668 }
669
670 /* handle the area in the middle */
671 count = (frag_end - frag_start) / 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100672 amdgpu_vm_update_pages(adev, 0, NULL, ib, frag_start, addr, count,
Christian König9ab21462015-11-30 14:19:26 +0100673 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400674
675 /* handle the 4K area at the end */
676 if (frag_end != pe_end) {
677 addr += AMDGPU_GPU_PAGE_SIZE * count;
678 count = (pe_end - frag_end) / 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100679 amdgpu_vm_update_pages(adev, 0, NULL, ib, frag_end, addr,
Christian König9ab21462015-11-30 14:19:26 +0100680 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 }
682}
683
684/**
685 * amdgpu_vm_update_ptes - make sure that page tables are valid
686 *
687 * @adev: amdgpu_device pointer
Christian Königfa3ab3c2016-03-18 21:00:35 +0100688 * @src: address where to copy page table entries from
689 * @pages_addr: DMA addresses to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 * @vm: requested vm
691 * @start: start of GPU address range
692 * @end: end of GPU address range
693 * @dst: destination address to map to
694 * @flags: mapping flags
695 *
Christian König8843dbb2016-01-26 12:17:11 +0100696 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 */
Christian Königa1e08d32016-01-26 11:40:46 +0100698static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100699 uint64_t src,
700 dma_addr_t *pages_addr,
Christian Königa1e08d32016-01-26 11:40:46 +0100701 struct amdgpu_vm *vm,
702 struct amdgpu_ib *ib,
703 uint64_t start, uint64_t end,
704 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705{
Christian König31f6c1f2016-01-26 12:37:49 +0100706 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
707
708 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 uint64_t addr;
710
711 /* walk over the address space and update the page tables */
712 for (addr = start; addr < end; ) {
713 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100714 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100716 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717
718 if ((addr & ~mask) == (end & ~mask))
719 nptes = end - addr;
720 else
721 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
722
Christian König31f6c1f2016-01-26 12:37:49 +0100723 pe_start = amdgpu_bo_gpu_offset(pt);
724 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400725
Christian König31f6c1f2016-01-26 12:37:49 +0100726 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727
Christian Königfa3ab3c2016-03-18 21:00:35 +0100728 amdgpu_vm_frag_ptes(adev, src, pages_addr, ib,
Christian König31f6c1f2016-01-26 12:37:49 +0100729 last_pe_start, last_pe_end,
730 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731
Christian König31f6c1f2016-01-26 12:37:49 +0100732 last_pe_start = pe_start;
733 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 last_dst = dst;
735 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100736 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 }
738
739 addr += nptes;
740 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
741 }
742
Christian Königfa3ab3c2016-03-18 21:00:35 +0100743 amdgpu_vm_frag_ptes(adev, src, pages_addr, ib, last_pe_start,
744 last_pe_end, last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400745}
746
747/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
749 *
750 * @adev: amdgpu_device pointer
Christian Königfa3ab3c2016-03-18 21:00:35 +0100751 * @src: address where to copy page table entries from
752 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100753 * @vm: requested vm
754 * @start: start of mapped range
755 * @last: last mapped entry
756 * @flags: flags for the entries
757 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 * @fence: optional resulting fence
759 *
Christian Königa14faa62016-01-25 14:27:31 +0100760 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762 */
763static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100764 uint64_t src,
765 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100767 uint64_t start, uint64_t last,
768 uint32_t flags, uint64_t addr,
769 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770{
Christian König2d55e452016-02-08 17:37:38 +0100771 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100772 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400773 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100774 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800775 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800776 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400777 int r;
778
Christian König2d55e452016-02-08 17:37:38 +0100779 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
780
Christian Königa1e08d32016-01-26 11:40:46 +0100781 /* sync to everything on unmapping */
782 if (!(flags & AMDGPU_PTE_VALID))
783 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
784
Christian Königa14faa62016-01-25 14:27:31 +0100785 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786
787 /*
788 * reserve space for one command every (1 << BLOCK_SIZE)
789 * entries or 2k dwords (whatever is smaller)
790 */
791 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
792
793 /* padding, etc. */
794 ndw = 64;
795
Christian Königfa3ab3c2016-03-18 21:00:35 +0100796 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797 /* only copy commands needed */
798 ndw += ncmds * 7;
799
Christian Königfa3ab3c2016-03-18 21:00:35 +0100800 } else if (pages_addr) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801 /* header for write data commands */
802 ndw += ncmds * 4;
803
804 /* body of write data command */
805 ndw += nptes * 2;
806
807 } else {
808 /* set page commands needed */
809 ndw += ncmds * 10;
810
811 /* two extra commands for begin/end of fragment */
812 ndw += 2 * 10;
813 }
814
Christian Königd71518b2016-02-01 12:20:25 +0100815 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
816 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400817 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100818
819 ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800820
Christian Könige86f9ce2016-02-08 12:13:05 +0100821 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100822 owner);
823 if (r)
824 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400825
Christian Königa1e08d32016-01-26 11:40:46 +0100826 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
827 if (r)
828 goto error_free;
829
Christian Königfa3ab3c2016-03-18 21:00:35 +0100830 amdgpu_vm_update_ptes(adev, src, pages_addr, vm, ib, start,
831 last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400832
Christian König9e5d53092016-01-31 12:20:55 +0100833 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800834 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100835 r = amdgpu_job_submit(job, ring, &vm->entity,
836 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800837 if (r)
838 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400839
Christian Königbf60efd2015-09-04 10:47:56 +0200840 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800841 if (fence) {
842 fence_put(*fence);
843 *fence = fence_get(f);
844 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800845 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800847
848error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100849 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800850 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400851}
852
853/**
Christian Königa14faa62016-01-25 14:27:31 +0100854 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
855 *
856 * @adev: amdgpu_device pointer
Christian König8358dce2016-03-30 10:50:25 +0200857 * @gtt_flags: flags as they are used for GTT
858 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100859 * @vm: requested vm
860 * @mapping: mapped range and flags to use for the update
861 * @addr: addr to set the area to
Christian König8358dce2016-03-30 10:50:25 +0200862 * @flags: HW flags for the mapping
Christian Königa14faa62016-01-25 14:27:31 +0100863 * @fence: optional resulting fence
864 *
865 * Split the mapping into smaller chunks so that each update fits
866 * into a SDMA IB.
867 * Returns 0 for success, -EINVAL for failure.
868 */
869static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Christian Königa14faa62016-01-25 14:27:31 +0100870 uint32_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +0200871 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +0100872 struct amdgpu_vm *vm,
873 struct amdgpu_bo_va_mapping *mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100874 uint32_t flags, uint64_t addr,
875 struct fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +0100876{
877 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
878
Christian Königfa3ab3c2016-03-18 21:00:35 +0100879 uint64_t src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +0100880 int r;
881
882 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
883 * but in case of something, we filter the flags in first place
884 */
885 if (!(mapping->flags & AMDGPU_PTE_READABLE))
886 flags &= ~AMDGPU_PTE_READABLE;
887 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
888 flags &= ~AMDGPU_PTE_WRITEABLE;
889
890 trace_amdgpu_vm_bo_update(mapping);
891
Christian König8358dce2016-03-30 10:50:25 +0200892 if (pages_addr) {
Christian Königfa3ab3c2016-03-18 21:00:35 +0100893 if (flags == gtt_flags)
894 src = adev->gart.table_addr + (addr >> 12) * 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100895 addr = 0;
896 }
Christian Königa14faa62016-01-25 14:27:31 +0100897 addr += mapping->offset;
898
Christian König8358dce2016-03-30 10:50:25 +0200899 if (!pages_addr || src)
Christian Königfa3ab3c2016-03-18 21:00:35 +0100900 return amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100901 start, mapping->it.last,
902 flags, addr, fence);
903
904 while (start != mapping->it.last + 1) {
905 uint64_t last;
906
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500907 last = min((uint64_t)mapping->it.last, start + max_size - 1);
Christian Königfa3ab3c2016-03-18 21:00:35 +0100908 r = amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100909 start, last, flags, addr,
910 fence);
911 if (r)
912 return r;
913
914 start = last + 1;
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500915 addr += max_size * AMDGPU_GPU_PAGE_SIZE;
Christian Königa14faa62016-01-25 14:27:31 +0100916 }
917
918 return 0;
919}
920
921/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
923 *
924 * @adev: amdgpu_device pointer
925 * @bo_va: requested BO and VM object
926 * @mem: ttm mem
927 *
928 * Fill in the page table entries for @bo_va.
929 * Returns 0 for success, -EINVAL for failure.
930 *
931 * Object have to be reserved and mutex must be locked!
932 */
933int amdgpu_vm_bo_update(struct amdgpu_device *adev,
934 struct amdgpu_bo_va *bo_va,
935 struct ttm_mem_reg *mem)
936{
937 struct amdgpu_vm *vm = bo_va->vm;
938 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +0200939 dma_addr_t *pages_addr = NULL;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100940 uint32_t gtt_flags, flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400941 uint64_t addr;
942 int r;
943
944 if (mem) {
Christian König8358dce2016-03-30 10:50:25 +0200945 struct ttm_dma_tt *ttm;
946
Christian Königb7d698d2015-09-07 12:32:09 +0200947 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100948 switch (mem->mem_type) {
949 case TTM_PL_TT:
Christian König8358dce2016-03-30 10:50:25 +0200950 ttm = container_of(bo_va->bo->tbo.ttm, struct
951 ttm_dma_tt, ttm);
952 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +0100953 break;
954
955 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100957 break;
958
959 default:
960 break;
961 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962 } else {
963 addr = 0;
964 }
965
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
Christian Königfa3ab3c2016-03-18 21:00:35 +0100967 gtt_flags = (adev == bo_va->bo->adev) ? flags : 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968
Christian König7fc11952015-07-30 11:53:42 +0200969 spin_lock(&vm->status_lock);
970 if (!list_empty(&bo_va->vm_status))
971 list_splice_init(&bo_va->valids, &bo_va->invalids);
972 spin_unlock(&vm->status_lock);
973
974 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König8358dce2016-03-30 10:50:25 +0200975 r = amdgpu_vm_bo_split_mapping(adev, gtt_flags, pages_addr, vm,
976 mapping, flags, addr,
977 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978 if (r)
979 return r;
980 }
981
Christian Königd6c10f62015-09-28 12:00:23 +0200982 if (trace_amdgpu_vm_bo_mapping_enabled()) {
983 list_for_each_entry(mapping, &bo_va->valids, list)
984 trace_amdgpu_vm_bo_mapping(mapping);
985
986 list_for_each_entry(mapping, &bo_va->invalids, list)
987 trace_amdgpu_vm_bo_mapping(mapping);
988 }
989
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800991 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400992 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200993 if (!mem)
994 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 spin_unlock(&vm->status_lock);
996
997 return 0;
998}
999
1000/**
1001 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1002 *
1003 * @adev: amdgpu_device pointer
1004 * @vm: requested vm
1005 *
1006 * Make sure all freed BOs are cleared in the PT.
1007 * Returns 0 for success.
1008 *
1009 * PTs have to be reserved and mutex must be locked!
1010 */
1011int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1012 struct amdgpu_vm *vm)
1013{
1014 struct amdgpu_bo_va_mapping *mapping;
1015 int r;
1016
1017 while (!list_empty(&vm->freed)) {
1018 mapping = list_first_entry(&vm->freed,
1019 struct amdgpu_bo_va_mapping, list);
1020 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001021
Christian König8358dce2016-03-30 10:50:25 +02001022 r = amdgpu_vm_bo_split_mapping(adev, 0, NULL, vm, mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001023 0, 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024 kfree(mapping);
1025 if (r)
1026 return r;
1027
1028 }
1029 return 0;
1030
1031}
1032
1033/**
1034 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1035 *
1036 * @adev: amdgpu_device pointer
1037 * @vm: requested vm
1038 *
1039 * Make sure all invalidated BOs are cleared in the PT.
1040 * Returns 0 for success.
1041 *
1042 * PTs have to be reserved and mutex must be locked!
1043 */
1044int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001045 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046{
monk.liucfe2c972015-05-26 15:01:54 +08001047 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001048 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001049
1050 spin_lock(&vm->status_lock);
1051 while (!list_empty(&vm->invalidated)) {
1052 bo_va = list_first_entry(&vm->invalidated,
1053 struct amdgpu_bo_va, vm_status);
1054 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001055
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001056 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
1057 if (r)
1058 return r;
1059
1060 spin_lock(&vm->status_lock);
1061 }
1062 spin_unlock(&vm->status_lock);
1063
monk.liucfe2c972015-05-26 15:01:54 +08001064 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001065 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001066
1067 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068}
1069
1070/**
1071 * amdgpu_vm_bo_add - add a bo to a specific vm
1072 *
1073 * @adev: amdgpu_device pointer
1074 * @vm: requested vm
1075 * @bo: amdgpu buffer object
1076 *
Christian König8843dbb2016-01-26 12:17:11 +01001077 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001078 * Add @bo to the list of bos associated with the vm
1079 * Returns newly added bo_va or NULL for failure
1080 *
1081 * Object has to be reserved!
1082 */
1083struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1084 struct amdgpu_vm *vm,
1085 struct amdgpu_bo *bo)
1086{
1087 struct amdgpu_bo_va *bo_va;
1088
1089 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1090 if (bo_va == NULL) {
1091 return NULL;
1092 }
1093 bo_va->vm = vm;
1094 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001095 bo_va->ref_count = 1;
1096 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001097 INIT_LIST_HEAD(&bo_va->valids);
1098 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001100
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001102
1103 return bo_va;
1104}
1105
1106/**
1107 * amdgpu_vm_bo_map - map bo inside a vm
1108 *
1109 * @adev: amdgpu_device pointer
1110 * @bo_va: bo_va to store the address
1111 * @saddr: where to map the BO
1112 * @offset: requested offset in the BO
1113 * @flags: attributes of pages (read/write/valid/etc.)
1114 *
1115 * Add a mapping of the BO at the specefied addr into the VM.
1116 * Returns 0 for success, error for failure.
1117 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001118 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001119 */
1120int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1121 struct amdgpu_bo_va *bo_va,
1122 uint64_t saddr, uint64_t offset,
1123 uint64_t size, uint32_t flags)
1124{
1125 struct amdgpu_bo_va_mapping *mapping;
1126 struct amdgpu_vm *vm = bo_va->vm;
1127 struct interval_tree_node *it;
1128 unsigned last_pfn, pt_idx;
1129 uint64_t eaddr;
1130 int r;
1131
Christian König0be52de2015-05-18 14:37:27 +02001132 /* validate the parameters */
1133 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001134 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001135 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001136
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001137 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001138 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001139 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001141
1142 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001143 if (last_pfn >= adev->vm_manager.max_pfn) {
1144 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001145 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146 return -EINVAL;
1147 }
1148
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149 saddr /= AMDGPU_GPU_PAGE_SIZE;
1150 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1151
Felix Kuehling005ae952015-11-23 17:43:48 -05001152 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153 if (it) {
1154 struct amdgpu_bo_va_mapping *tmp;
1155 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1156 /* bo and tmp overlap, invalid addr */
1157 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1158 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1159 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001161 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162 }
1163
1164 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1165 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001166 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001167 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168 }
1169
1170 INIT_LIST_HEAD(&mapping->list);
1171 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001172 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173 mapping->offset = offset;
1174 mapping->flags = flags;
1175
Christian König7fc11952015-07-30 11:53:42 +02001176 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177 interval_tree_insert(&mapping->it, &vm->va);
1178
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001179 /* Make sure the page tables are allocated */
1180 saddr >>= amdgpu_vm_block_size;
1181 eaddr >>= amdgpu_vm_block_size;
1182
1183 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1184
1185 if (eaddr > vm->max_pde_used)
1186 vm->max_pde_used = eaddr;
1187
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 /* walk over the address space and allocate the page tables */
1189 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001190 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001191 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 struct amdgpu_bo *pt;
1193
Christian Königee1782c2015-12-11 21:01:23 +01001194 entry = &vm->page_tables[pt_idx].entry;
1195 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196 continue;
1197
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001198 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1199 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001200 AMDGPU_GEM_DOMAIN_VRAM,
1201 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001202 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001203 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001204 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001205
Christian König82b9c552015-11-27 16:49:00 +01001206 /* Keep a reference to the page table to avoid freeing
1207 * them up in the wrong order.
1208 */
1209 pt->parent = amdgpu_bo_ref(vm->page_directory);
1210
Christian König2bd9ccf2016-02-01 12:53:58 +01001211 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 if (r) {
1213 amdgpu_bo_unref(&pt);
1214 goto error_free;
1215 }
1216
Christian Königee1782c2015-12-11 21:01:23 +01001217 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001218 entry->priority = 0;
1219 entry->tv.bo = &entry->robj->tbo;
1220 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +01001221 entry->user_pages = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 }
1224
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 return 0;
1226
1227error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001228 list_del(&mapping->list);
1229 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001230 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231 kfree(mapping);
1232
Chunming Zhouf48b2652015-10-16 14:06:19 +08001233error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001234 return r;
1235}
1236
1237/**
1238 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1239 *
1240 * @adev: amdgpu_device pointer
1241 * @bo_va: bo_va to remove the address from
1242 * @saddr: where to the BO is mapped
1243 *
1244 * Remove a mapping of the BO at the specefied addr from the VM.
1245 * Returns 0 for success, error for failure.
1246 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001247 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001248 */
1249int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1250 struct amdgpu_bo_va *bo_va,
1251 uint64_t saddr)
1252{
1253 struct amdgpu_bo_va_mapping *mapping;
1254 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001255 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256
Christian König6c7fc502015-06-05 20:56:17 +02001257 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001258
Christian König7fc11952015-07-30 11:53:42 +02001259 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260 if (mapping->it.start == saddr)
1261 break;
1262 }
1263
Christian König7fc11952015-07-30 11:53:42 +02001264 if (&mapping->list == &bo_va->valids) {
1265 valid = false;
1266
1267 list_for_each_entry(mapping, &bo_va->invalids, list) {
1268 if (mapping->it.start == saddr)
1269 break;
1270 }
1271
Christian König32b41ac2016-03-08 18:03:27 +01001272 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001273 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001274 }
Christian König32b41ac2016-03-08 18:03:27 +01001275
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 list_del(&mapping->list);
1277 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001278 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001279
Christian Könige17841b2016-03-08 17:52:01 +01001280 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001282 else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 kfree(mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284
1285 return 0;
1286}
1287
1288/**
1289 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1290 *
1291 * @adev: amdgpu_device pointer
1292 * @bo_va: requested bo_va
1293 *
Christian König8843dbb2016-01-26 12:17:11 +01001294 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 *
1296 * Object have to be reserved!
1297 */
1298void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1299 struct amdgpu_bo_va *bo_va)
1300{
1301 struct amdgpu_bo_va_mapping *mapping, *next;
1302 struct amdgpu_vm *vm = bo_va->vm;
1303
1304 list_del(&bo_va->bo_list);
1305
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306 spin_lock(&vm->status_lock);
1307 list_del(&bo_va->vm_status);
1308 spin_unlock(&vm->status_lock);
1309
Christian König7fc11952015-07-30 11:53:42 +02001310 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001311 list_del(&mapping->list);
1312 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001313 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001314 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 }
Christian König7fc11952015-07-30 11:53:42 +02001316 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1317 list_del(&mapping->list);
1318 interval_tree_remove(&mapping->it, &vm->va);
1319 kfree(mapping);
1320 }
Christian König32b41ac2016-03-08 18:03:27 +01001321
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001322 fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001324}
1325
1326/**
1327 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1328 *
1329 * @adev: amdgpu_device pointer
1330 * @vm: requested vm
1331 * @bo: amdgpu buffer object
1332 *
Christian König8843dbb2016-01-26 12:17:11 +01001333 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001334 */
1335void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1336 struct amdgpu_bo *bo)
1337{
1338 struct amdgpu_bo_va *bo_va;
1339
1340 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001341 spin_lock(&bo_va->vm->status_lock);
1342 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001343 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001344 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001345 }
1346}
1347
1348/**
1349 * amdgpu_vm_init - initialize a vm instance
1350 *
1351 * @adev: amdgpu_device pointer
1352 * @vm: requested vm
1353 *
Christian König8843dbb2016-01-26 12:17:11 +01001354 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001355 */
1356int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1357{
1358 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1359 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001360 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001361 unsigned ring_instance;
1362 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001363 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001364 int i, r;
1365
Christian Königbcb1ba32016-03-08 15:40:11 +01001366 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1367 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001368 vm->va = RB_ROOT;
1369 spin_lock_init(&vm->status_lock);
1370 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001371 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001373
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001374 pd_size = amdgpu_vm_directory_size(adev);
1375 pd_entries = amdgpu_vm_num_pdes(adev);
1376
1377 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001378 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 if (vm->page_tables == NULL) {
1380 DRM_ERROR("Cannot allocate memory for page table array\n");
1381 return -ENOMEM;
1382 }
1383
Christian König2bd9ccf2016-02-01 12:53:58 +01001384 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001385
1386 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1387 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1388 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001389 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1390 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1391 rq, amdgpu_sched_jobs);
1392 if (r)
1393 return r;
1394
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001395 vm->page_directory_fence = NULL;
1396
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001398 AMDGPU_GEM_DOMAIN_VRAM,
1399 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001400 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001401 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001402 goto error_free_sched_entity;
1403
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001404 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001405 if (r)
1406 goto error_free_page_directory;
1407
1408 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001409 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001410 if (r)
1411 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412
1413 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001414
1415error_free_page_directory:
1416 amdgpu_bo_unref(&vm->page_directory);
1417 vm->page_directory = NULL;
1418
1419error_free_sched_entity:
1420 amd_sched_entity_fini(&ring->sched, &vm->entity);
1421
1422 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001423}
1424
1425/**
1426 * amdgpu_vm_fini - tear down a vm instance
1427 *
1428 * @adev: amdgpu_device pointer
1429 * @vm: requested vm
1430 *
Christian König8843dbb2016-01-26 12:17:11 +01001431 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 * Unbind the VM and remove all bos from the vm bo list
1433 */
1434void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1435{
1436 struct amdgpu_bo_va_mapping *mapping, *tmp;
1437 int i;
1438
Christian König2d55e452016-02-08 17:37:38 +01001439 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001440
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001441 if (!RB_EMPTY_ROOT(&vm->va)) {
1442 dev_err(adev->dev, "still active bo inside vm\n");
1443 }
1444 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1445 list_del(&mapping->list);
1446 interval_tree_remove(&mapping->it, &vm->va);
1447 kfree(mapping);
1448 }
1449 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1450 list_del(&mapping->list);
1451 kfree(mapping);
1452 }
1453
1454 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001455 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001456 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001457
1458 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001459 fence_put(vm->page_directory_fence);
Christian König20250212016-03-08 17:58:35 +01001460
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001461 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian Königbcb1ba32016-03-08 15:40:11 +01001462 struct amdgpu_vm_id *id = vm->ids[i];
Christian König1c16c0a2015-11-14 21:31:40 +01001463
Christian Königbcb1ba32016-03-08 15:40:11 +01001464 if (!id)
1465 continue;
1466
1467 atomic_long_cmpxchg(&id->owner, (long)&vm->ids[i], 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001468 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001469}
Christian Königea89f8c2015-11-15 20:52:06 +01001470
1471/**
Christian Königa9a78b32016-01-21 10:19:11 +01001472 * amdgpu_vm_manager_init - init the VM manager
1473 *
1474 * @adev: amdgpu_device pointer
1475 *
1476 * Initialize the VM manager structures
1477 */
1478void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1479{
1480 unsigned i;
1481
1482 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1483
1484 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001485 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1486 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01001487 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01001488 list_add_tail(&adev->vm_manager.ids[i].list,
1489 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001490 }
Christian König2d55e452016-02-08 17:37:38 +01001491
1492 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001493}
1494
1495/**
Christian Königea89f8c2015-11-15 20:52:06 +01001496 * amdgpu_vm_manager_fini - cleanup VM manager
1497 *
1498 * @adev: amdgpu_device pointer
1499 *
1500 * Cleanup the VM manager and free resources.
1501 */
1502void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1503{
1504 unsigned i;
1505
Christian Königbcb1ba32016-03-08 15:40:11 +01001506 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
1507 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
1508
Christian König832a9022016-02-15 12:33:02 +01001509 fence_put(adev->vm_manager.ids[i].first);
1510 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Christian Königbcb1ba32016-03-08 15:40:11 +01001511 fence_put(id->flushed_updates);
1512 }
Christian Königea89f8c2015-11-15 20:52:06 +01001513}