blob: e3c6ffac7f5c3f51c9d168c227a61b3b9e1a268b [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 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence-array.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <drm/drmP.h>
30#include <drm/amdgpu_drm.h>
31#include "amdgpu.h"
32#include "amdgpu_trace.h"
33
34/*
35 * GPUVM
36 * GPUVM is similar to the legacy gart on older asics, however
37 * rather than there being a single global gart table
38 * for the entire GPU, there are multiple VM page tables active
39 * at any given time. The VM page tables can contain a mix
40 * vram pages and system memory pages and system memory pages
41 * can be mapped as snooped (cached system pages) or unsnooped
42 * (uncached system pages).
43 * Each VM has an ID associated with it and there is a page table
44 * associated with each VMID. When execting a command buffer,
45 * the kernel tells the the ring what VMID to use for that command
46 * buffer. VMIDs are allocated dynamically as commands are submitted.
47 * The userspace drivers maintain their own address space and the kernel
48 * sets up their pages tables accordingly when they submit their
49 * command buffers and a VMID is assigned.
50 * Cayman/Trinity support up to 8 active VMs at any given time;
51 * SI supports 16.
52 */
53
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040054/* Local structure. Encapsulate some VM table update parameters to reduce
55 * the number of function parameters
56 */
Christian König29efc4f2016-08-04 14:52:50 +020057struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020058 /* amdgpu device we do this update for */
59 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +020060 /* optional amdgpu_vm we do this update for */
61 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040062 /* address where to copy page table entries from */
63 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040064 /* indirect buffer to fill with commands */
65 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020066 /* Function which actually does the update */
67 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
68 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080069 uint64_t flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +080070 /* indicate update pt or its shadow */
71 bool shadow;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040072};
73
Christian König284710f2017-01-30 11:09:31 +010074/* Helper to disable partial resident texture feature from a fence callback */
75struct amdgpu_prt_cb {
76 struct amdgpu_device *adev;
77 struct dma_fence_cb cb;
78};
79
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080/**
Christian König72a7ec52016-10-19 11:03:57 +020081 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082 *
83 * @adev: amdgpu_device pointer
84 *
Christian König72a7ec52016-10-19 11:03:57 +020085 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 */
Christian König72a7ec52016-10-19 11:03:57 +020087static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
88 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089{
Christian König72a7ec52016-10-19 11:03:57 +020090 if (level == 0)
91 /* For the root directory */
92 return adev->vm_manager.max_pfn >>
93 (amdgpu_vm_block_size * adev->vm_manager.num_level);
94 else if (level == adev->vm_manager.num_level)
95 /* For the page tables on the leaves */
96 return AMDGPU_VM_PTE_COUNT;
97 else
98 /* Everything in between */
99 return 1 << amdgpu_vm_block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100}
101
102/**
Christian König72a7ec52016-10-19 11:03:57 +0200103 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 *
105 * @adev: amdgpu_device pointer
106 *
Christian König72a7ec52016-10-19 11:03:57 +0200107 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 */
Christian König72a7ec52016-10-19 11:03:57 +0200109static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110{
Christian König72a7ec52016-10-19 11:03:57 +0200111 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112}
113
114/**
Christian König56467eb2015-12-11 15:16:32 +0100115 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 *
117 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100118 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100119 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 *
121 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100122 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 */
Christian König56467eb2015-12-11 15:16:32 +0100124void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
125 struct list_head *validated,
126 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127{
Christian König67003a12016-10-12 14:46:26 +0200128 entry->robj = vm->root.bo;
Christian König56467eb2015-12-11 15:16:32 +0100129 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200130 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100131 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100132 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100133 list_add(&entry->tv.head, validated);
134}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135
Christian König56467eb2015-12-11 15:16:32 +0100136/**
Christian König670fecc2016-10-12 15:36:57 +0200137 * amdgpu_vm_validate_layer - validate a single page table level
138 *
139 * @parent: parent page table level
140 * @validate: callback to do the validation
141 * @param: parameter for the validation callback
142 *
143 * Validate the page table BOs on command submission if neccessary.
144 */
145static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
146 int (*validate)(void *, struct amdgpu_bo *),
147 void *param)
148{
149 unsigned i;
150 int r;
151
152 if (!parent->entries)
153 return 0;
154
155 for (i = 0; i <= parent->last_entry_used; ++i) {
156 struct amdgpu_vm_pt *entry = &parent->entries[i];
157
158 if (!entry->bo)
159 continue;
160
161 r = validate(param, entry->bo);
162 if (r)
163 return r;
164
165 /*
166 * Recurse into the sub directory. This is harmless because we
167 * have only a maximum of 5 layers.
168 */
169 r = amdgpu_vm_validate_level(entry, validate, param);
170 if (r)
171 return r;
172 }
173
174 return r;
175}
176
177/**
Christian Königf7da30d2016-09-28 12:03:04 +0200178 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100179 *
Christian König5a712a82016-06-21 16:28:15 +0200180 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100181 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200182 * @validate: callback to do the validation
183 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184 *
Christian Königf7da30d2016-09-28 12:03:04 +0200185 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186 */
Christian Königf7da30d2016-09-28 12:03:04 +0200187int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
188 int (*validate)(void *p, struct amdgpu_bo *bo),
189 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190{
Christian König5a712a82016-06-21 16:28:15 +0200191 uint64_t num_evictions;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192
Christian König5a712a82016-06-21 16:28:15 +0200193 /* We only need to validate the page tables
194 * if they aren't already valid.
195 */
196 num_evictions = atomic64_read(&adev->num_evictions);
197 if (num_evictions == vm->last_eviction_counter)
Christian Königf7da30d2016-09-28 12:03:04 +0200198 return 0;
Christian König5a712a82016-06-21 16:28:15 +0200199
Christian König670fecc2016-10-12 15:36:57 +0200200 return amdgpu_vm_validate_level(&vm->root, validate, param);
Christian Königeceb8a12016-01-11 15:35:21 +0100201}
202
203/**
Christian Königd711e132016-10-13 10:20:53 +0200204 * amdgpu_vm_move_level_in_lru - move one level of PT BOs to the LRU tail
205 *
206 * @adev: amdgpu device instance
207 * @vm: vm providing the BOs
208 *
209 * Move the PT BOs to the tail of the LRU.
210 */
211static void amdgpu_vm_move_level_in_lru(struct amdgpu_vm_pt *parent)
212{
213 unsigned i;
214
215 if (!parent->entries)
216 return;
217
218 for (i = 0; i <= parent->last_entry_used; ++i) {
219 struct amdgpu_vm_pt *entry = &parent->entries[i];
220
221 if (!entry->bo)
222 continue;
223
224 ttm_bo_move_to_lru_tail(&entry->bo->tbo);
225 amdgpu_vm_move_level_in_lru(entry);
226 }
227}
228
229/**
Christian Königeceb8a12016-01-11 15:35:21 +0100230 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
231 *
232 * @adev: amdgpu device instance
233 * @vm: vm providing the BOs
234 *
235 * Move the PT BOs to the tail of the LRU.
236 */
237void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
238 struct amdgpu_vm *vm)
239{
240 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Christian Königeceb8a12016-01-11 15:35:21 +0100241
242 spin_lock(&glob->lru_lock);
Christian Königd711e132016-10-13 10:20:53 +0200243 amdgpu_vm_move_level_in_lru(&vm->root);
Christian Königeceb8a12016-01-11 15:35:21 +0100244 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400245}
246
Christian König663e4572017-03-13 10:13:37 +0100247/**
248 * amdgpu_vm_alloc_pts - Allocate page tables.
249 *
250 * @adev: amdgpu_device pointer
251 * @vm: VM to allocate page tables for
252 * @saddr: Start address which needs to be allocated
253 * @size: Size from start address we need.
254 *
255 * Make sure the page tables are allocated.
256 */
257int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
258 struct amdgpu_vm *vm,
259 uint64_t saddr, uint64_t size)
260{
261 unsigned last_pfn, pt_idx;
262 uint64_t eaddr;
263 int r;
264
265 /* validate the parameters */
266 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
267 return -EINVAL;
268
269 eaddr = saddr + size - 1;
270 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
271 if (last_pfn >= adev->vm_manager.max_pfn) {
272 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
273 last_pfn, adev->vm_manager.max_pfn);
274 return -EINVAL;
275 }
276
277 saddr /= AMDGPU_GPU_PAGE_SIZE;
278 eaddr /= AMDGPU_GPU_PAGE_SIZE;
279
280 saddr >>= amdgpu_vm_block_size;
281 eaddr >>= amdgpu_vm_block_size;
282
Christian König72a7ec52016-10-19 11:03:57 +0200283 BUG_ON(eaddr >= amdgpu_vm_num_entries(adev, 0));
Christian König663e4572017-03-13 10:13:37 +0100284
Christian König67003a12016-10-12 14:46:26 +0200285 if (eaddr > vm->root.last_entry_used)
286 vm->root.last_entry_used = eaddr;
Christian König663e4572017-03-13 10:13:37 +0100287
288 /* walk over the address space and allocate the page tables */
289 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian König67003a12016-10-12 14:46:26 +0200290 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König663e4572017-03-13 10:13:37 +0100291 struct amdgpu_bo *pt;
292
Christian König67003a12016-10-12 14:46:26 +0200293 if (vm->root.entries[pt_idx].bo)
Christian König663e4572017-03-13 10:13:37 +0100294 continue;
295
296 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
297 AMDGPU_GPU_PAGE_SIZE, true,
298 AMDGPU_GEM_DOMAIN_VRAM,
299 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
300 AMDGPU_GEM_CREATE_SHADOW |
301 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
302 AMDGPU_GEM_CREATE_VRAM_CLEARED,
303 NULL, resv, &pt);
304 if (r)
305 return r;
306
307 /* Keep a reference to the page table to avoid freeing
308 * them up in the wrong order.
309 */
Christian König67003a12016-10-12 14:46:26 +0200310 pt->parent = amdgpu_bo_ref(vm->root.bo);
Christian König663e4572017-03-13 10:13:37 +0100311
Christian König67003a12016-10-12 14:46:26 +0200312 vm->root.entries[pt_idx].bo = pt;
313 vm->root.entries[pt_idx].addr = 0;
Christian König663e4572017-03-13 10:13:37 +0100314 }
315
316 return 0;
317}
318
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800319static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
320 struct amdgpu_vm_id *id)
321{
322 return id->current_gpu_reset_count !=
323 atomic_read(&adev->gpu_reset_counter) ? true : false;
324}
325
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400326/**
327 * amdgpu_vm_grab_id - allocate the next free VMID
328 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400329 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200330 * @ring: ring we want to submit job to
331 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100332 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400333 *
Christian König7f8a5292015-07-20 16:09:40 +0200334 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400335 */
Christian König7f8a5292015-07-20 16:09:40 +0200336int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100337 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800338 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400340 struct amdgpu_device *adev = ring->adev;
Christian König090b7672016-07-08 10:21:02 +0200341 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100342 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200343 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100344 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200345 unsigned i;
346 int r = 0;
347
348 fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
349 GFP_KERNEL);
350 if (!fences)
351 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400352
Christian König94dd0a42016-01-18 17:01:42 +0100353 mutex_lock(&adev->vm_manager.lock);
354
Christian König36fd7c52016-05-23 15:30:08 +0200355 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200356 i = 0;
Christian König8d76001e2016-05-23 16:00:32 +0200357 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200358 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
359 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200360 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200361 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200362 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100363
Christian König1fbb2e92016-06-01 10:47:36 +0200364 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König8d76001e2016-05-23 16:00:32 +0200365 if (&idle->list == &adev->vm_manager.ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200366 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
367 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100368 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200369 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200370
Christian König1fbb2e92016-06-01 10:47:36 +0200371 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100372 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200373
Chris Wilsonf54d1862016-10-25 13:00:45 +0100374 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200375 seqno, true);
376 if (!array) {
377 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100378 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200379 kfree(fences);
380 r = -ENOMEM;
381 goto error;
382 }
Christian König8d76001e2016-05-23 16:00:32 +0200383
Christian König8d76001e2016-05-23 16:00:32 +0200384
Christian König1fbb2e92016-06-01 10:47:36 +0200385 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100386 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200387 if (r)
388 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200389
Christian König1fbb2e92016-06-01 10:47:36 +0200390 mutex_unlock(&adev->vm_manager.lock);
391 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200392
Christian König1fbb2e92016-06-01 10:47:36 +0200393 }
394 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200395
Chunming Zhoufd53be32016-07-01 17:59:01 +0800396 job->vm_needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200397 /* Check if we can use a VMID already assigned to this VM */
398 i = ring->idx;
399 do {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100400 struct dma_fence *flushed;
Christian König8d76001e2016-05-23 16:00:32 +0200401
Christian König1fbb2e92016-06-01 10:47:36 +0200402 id = vm->ids[i++];
403 if (i == AMDGPU_MAX_RINGS)
404 i = 0;
405
406 /* Check all the prerequisites to using this VMID */
407 if (!id)
408 continue;
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800409 if (amdgpu_vm_is_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800410 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200411
412 if (atomic64_read(&id->owner) != vm->client_id)
413 continue;
414
Chunming Zhoufd53be32016-07-01 17:59:01 +0800415 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200416 continue;
417
Christian König090b7672016-07-08 10:21:02 +0200418 if (!id->last_flush)
419 continue;
420
421 if (id->last_flush->context != fence_context &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100422 !dma_fence_is_signaled(id->last_flush))
Christian König1fbb2e92016-06-01 10:47:36 +0200423 continue;
424
425 flushed = id->flushed_updates;
426 if (updates &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100427 (!flushed || dma_fence_is_later(updates, flushed)))
Christian König1fbb2e92016-06-01 10:47:36 +0200428 continue;
429
Christian König3dab83b2016-06-01 13:31:17 +0200430 /* Good we can use this VMID. Remember this submission as
431 * user of the VMID.
432 */
Christian König1fbb2e92016-06-01 10:47:36 +0200433 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
434 if (r)
435 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200436
Chunming Zhou6adb0512016-06-27 17:06:01 +0800437 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König1fbb2e92016-06-01 10:47:36 +0200438 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
439 vm->ids[ring->idx] = id;
Christian König8d76001e2016-05-23 16:00:32 +0200440
Chunming Zhoufd53be32016-07-01 17:59:01 +0800441 job->vm_id = id - adev->vm_manager.ids;
442 job->vm_needs_flush = false;
Christian König0c0fdf12016-07-08 10:48:24 +0200443 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König8d76001e2016-05-23 16:00:32 +0200444
Christian König1fbb2e92016-06-01 10:47:36 +0200445 mutex_unlock(&adev->vm_manager.lock);
446 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200447
Christian König1fbb2e92016-06-01 10:47:36 +0200448 } while (i != ring->idx);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800449
Christian König1fbb2e92016-06-01 10:47:36 +0200450 /* Still no ID to use? Then use the idle one found earlier */
451 id = idle;
452
453 /* Remember this submission as user of the VMID */
454 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100455 if (r)
456 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100457
Chris Wilsonf54d1862016-10-25 13:00:45 +0100458 dma_fence_put(id->first);
459 id->first = dma_fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100460
Chris Wilsonf54d1862016-10-25 13:00:45 +0100461 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100462 id->last_flush = NULL;
463
Chris Wilsonf54d1862016-10-25 13:00:45 +0100464 dma_fence_put(id->flushed_updates);
465 id->flushed_updates = dma_fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100466
Chunming Zhoufd53be32016-07-01 17:59:01 +0800467 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhoub46b8a82016-06-27 17:04:23 +0800468 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König832a9022016-02-15 12:33:02 +0100469 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König0ea54b92016-05-04 10:20:01 +0200470 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100471 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472
Chunming Zhoufd53be32016-07-01 17:59:01 +0800473 job->vm_id = id - adev->vm_manager.ids;
Christian König0c0fdf12016-07-08 10:48:24 +0200474 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König832a9022016-02-15 12:33:02 +0100475
476error:
Christian König94dd0a42016-01-18 17:01:42 +0100477 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100478 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479}
480
Alex Deucher93dcc372016-06-17 17:05:15 -0400481static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
482{
483 struct amdgpu_device *adev = ring->adev;
Alex Deuchera1255102016-10-13 17:41:13 -0400484 const struct amdgpu_ip_block *ip_block;
Alex Deucher93dcc372016-06-17 17:05:15 -0400485
Christian König21cd9422016-10-05 15:36:39 +0200486 if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
Alex Deucher93dcc372016-06-17 17:05:15 -0400487 /* only compute rings */
488 return false;
489
490 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
491 if (!ip_block)
492 return false;
493
Alex Deuchera1255102016-10-13 17:41:13 -0400494 if (ip_block->version->major <= 7) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400495 /* gfx7 has no workaround */
496 return true;
Alex Deuchera1255102016-10-13 17:41:13 -0400497 } else if (ip_block->version->major == 8) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400498 if (adev->gfx.mec_fw_version >= 673)
499 /* gfx8 is fixed in MEC firmware 673 */
500 return false;
501 else
502 return true;
503 }
504 return false;
505}
506
Alex Xiee60f8db2017-03-09 11:36:26 -0500507static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr)
508{
509 u64 addr = mc_addr;
510
511 if (adev->mc.mc_funcs && adev->mc.mc_funcs->adjust_mc_addr)
512 addr = adev->mc.mc_funcs->adjust_mc_addr(adev, addr);
513
514 return addr;
515}
516
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517/**
518 * amdgpu_vm_flush - hardware flush the vm
519 *
520 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100521 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100522 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 *
Christian König4ff37a82016-02-26 16:18:26 +0100524 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 */
Chunming Zhoufd53be32016-07-01 17:59:01 +0800526int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527{
Christian König971fe9a92016-03-01 15:09:25 +0100528 struct amdgpu_device *adev = ring->adev;
Chunming Zhoufd53be32016-07-01 17:59:01 +0800529 struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100530 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800531 id->gds_base != job->gds_base ||
532 id->gds_size != job->gds_size ||
533 id->gws_base != job->gws_base ||
534 id->gws_size != job->gws_size ||
535 id->oa_base != job->oa_base ||
536 id->oa_size != job->oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100537 int r;
Christian Königd564a062016-03-01 15:51:53 +0100538
539 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800540 job->vm_needs_flush || gds_switch_needed ||
Alex Deucher93dcc372016-06-17 17:05:15 -0400541 amdgpu_vm_ring_has_compute_vm_bug(ring)))
Christian Königd564a062016-03-01 15:51:53 +0100542 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100543
Chunming Zhouaa1c8902016-06-30 13:56:02 +0800544 if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
545 amdgpu_vm_is_gpu_reset(adev, id))) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100546 struct dma_fence *fence;
Alex Xiee60f8db2017-03-09 11:36:26 -0500547 u64 pd_addr = amdgpu_vm_adjust_mc_addr(adev, job->vm_pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100548
Alex Xiee60f8db2017-03-09 11:36:26 -0500549 trace_amdgpu_vm_flush(pd_addr, ring->idx, job->vm_id);
550 amdgpu_ring_emit_vm_flush(ring, job->vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100551
Christian König3dab83b2016-06-01 13:31:17 +0200552 r = amdgpu_fence_emit(ring, &fence);
553 if (r)
554 return r;
555
Christian König41d9eb22016-03-01 16:46:18 +0100556 mutex_lock(&adev->vm_manager.lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100557 dma_fence_put(id->last_flush);
Christian König3dab83b2016-06-01 13:31:17 +0200558 id->last_flush = fence;
Christian König41d9eb22016-03-01 16:46:18 +0100559 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 }
Christian Königcffadc82016-03-01 13:34:49 +0100561
Christian Königd564a062016-03-01 15:51:53 +0100562 if (gds_switch_needed) {
Chunming Zhoufd53be32016-07-01 17:59:01 +0800563 id->gds_base = job->gds_base;
564 id->gds_size = job->gds_size;
565 id->gws_base = job->gws_base;
566 id->gws_size = job->gws_size;
567 id->oa_base = job->oa_base;
568 id->oa_size = job->oa_size;
569 amdgpu_ring_emit_gds_switch(ring, job->vm_id,
570 job->gds_base, job->gds_size,
571 job->gws_base, job->gws_size,
572 job->oa_base, job->oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100573 }
Christian König41d9eb22016-03-01 16:46:18 +0100574
575 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100576}
577
578/**
579 * amdgpu_vm_reset_id - reset VMID to zero
580 *
581 * @adev: amdgpu device structure
582 * @vm_id: vmid number to use
583 *
584 * Reset saved GDW, GWS and OA to force switch on next flush.
585 */
586void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
587{
Christian Königbcb1ba32016-03-08 15:40:11 +0100588 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100589
Christian Königbcb1ba32016-03-08 15:40:11 +0100590 id->gds_base = 0;
591 id->gds_size = 0;
592 id->gws_base = 0;
593 id->gws_size = 0;
594 id->oa_base = 0;
595 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400596}
597
598/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
600 *
601 * @vm: requested vm
602 * @bo: requested buffer object
603 *
Christian König8843dbb2016-01-26 12:17:11 +0100604 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400605 * Search inside the @bos vm list for the requested vm
606 * Returns the found bo_va or NULL if none is found
607 *
608 * Object has to be reserved!
609 */
610struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
611 struct amdgpu_bo *bo)
612{
613 struct amdgpu_bo_va *bo_va;
614
615 list_for_each_entry(bo_va, &bo->va, bo_list) {
616 if (bo_va->vm == vm) {
617 return bo_va;
618 }
619 }
620 return NULL;
621}
622
623/**
Christian Königafef8b82016-08-12 13:29:18 +0200624 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 *
Christian König29efc4f2016-08-04 14:52:50 +0200626 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 * @pe: addr of the page entry
628 * @addr: dst addr to write into pe
629 * @count: number of page entries to update
630 * @incr: increase next addr by incr bytes
631 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400632 *
633 * Traces the parameters and calls the right asic functions
634 * to setup the page table using the DMA.
635 */
Christian Königafef8b82016-08-12 13:29:18 +0200636static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
637 uint64_t pe, uint64_t addr,
638 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800639 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640{
Christian Königec2f05f2016-09-25 16:11:52 +0200641 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642
Christian Königafef8b82016-08-12 13:29:18 +0200643 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200644 amdgpu_vm_write_pte(params->adev, params->ib, pe,
645 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646
647 } else {
Christian König27c5f362016-08-04 15:02:49 +0200648 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 count, incr, flags);
650 }
651}
652
653/**
Christian Königafef8b82016-08-12 13:29:18 +0200654 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
655 *
656 * @params: see amdgpu_pte_update_params definition
657 * @pe: addr of the page entry
658 * @addr: dst addr to write into pe
659 * @count: number of page entries to update
660 * @incr: increase next addr by incr bytes
661 * @flags: hw access flags
662 *
663 * Traces the parameters and calls the DMA function to copy the PTEs.
664 */
665static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
666 uint64_t pe, uint64_t addr,
667 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800668 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200669{
Christian Königec2f05f2016-09-25 16:11:52 +0200670 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200671
Christian Königec2f05f2016-09-25 16:11:52 +0200672
673 trace_amdgpu_vm_copy_ptes(pe, src, count);
674
675 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200676}
677
678/**
Christian Königb07c9d22015-11-30 13:26:07 +0100679 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 *
Christian Königb07c9d22015-11-30 13:26:07 +0100681 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 * @addr: the unmapped addr
683 *
684 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100685 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200687static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688{
689 uint64_t result;
690
Christian Königde9ea7b2016-08-12 11:33:30 +0200691 /* page table offset */
692 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693
Christian Königde9ea7b2016-08-12 11:33:30 +0200694 /* in case cpu page size != gpu page size*/
695 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100696
697 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698
699 return result;
700}
701
Christian Königf8991ba2016-09-16 15:36:49 +0200702/*
Christian König194d2162016-10-12 15:13:52 +0200703 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200704 *
705 * @adev: amdgpu_device pointer
706 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200707 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +0200708 *
Christian König194d2162016-10-12 15:13:52 +0200709 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200710 * Returns 0 for success, error for failure.
711 */
Christian König194d2162016-10-12 15:13:52 +0200712static int amdgpu_vm_update_level(struct amdgpu_device *adev,
713 struct amdgpu_vm *vm,
714 struct amdgpu_vm_pt *parent,
715 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716{
Christian Königf8991ba2016-09-16 15:36:49 +0200717 struct amdgpu_bo *shadow;
Christian König2d55e452016-02-08 17:37:38 +0100718 struct amdgpu_ring *ring;
Christian Königf8991ba2016-09-16 15:36:49 +0200719 uint64_t pd_addr, shadow_addr;
Christian König194d2162016-10-12 15:13:52 +0200720 uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
Christian Königf8991ba2016-09-16 15:36:49 +0200721 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100723 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200724 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +1000725 struct dma_fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800726
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727 int r;
728
Christian König194d2162016-10-12 15:13:52 +0200729 if (!parent->entries)
730 return 0;
Christian König2d55e452016-02-08 17:37:38 +0100731 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
732
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400733 /* padding, etc. */
734 ndw = 64;
735
736 /* assume the worst case */
Christian König194d2162016-10-12 15:13:52 +0200737 ndw += parent->last_entry_used * 6;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738
Christian König194d2162016-10-12 15:13:52 +0200739 pd_addr = amdgpu_bo_gpu_offset(parent->bo);
740
741 shadow = parent->bo->shadow;
Christian Königf8991ba2016-09-16 15:36:49 +0200742 if (shadow) {
743 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
744 if (r)
745 return r;
746 shadow_addr = amdgpu_bo_gpu_offset(shadow);
747 ndw *= 2;
748 } else {
749 shadow_addr = 0;
750 }
751
Christian Königd71518b2016-02-01 12:20:25 +0100752 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
753 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400754 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100755
Christian König27c5f362016-08-04 15:02:49 +0200756 memset(&params, 0, sizeof(params));
757 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200758 params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759
Christian König194d2162016-10-12 15:13:52 +0200760 /* walk over the address space and update the directory */
761 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
762 struct amdgpu_bo *bo = parent->entries[pt_idx].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763 uint64_t pde, pt;
764
765 if (bo == NULL)
766 continue;
767
Christian König0fc86832016-09-16 11:46:23 +0200768 if (bo->shadow) {
Christian Königf8991ba2016-09-16 15:36:49 +0200769 struct amdgpu_bo *pt_shadow = bo->shadow;
Christian König0fc86832016-09-16 11:46:23 +0200770
Christian Königf8991ba2016-09-16 15:36:49 +0200771 r = amdgpu_ttm_bind(&pt_shadow->tbo,
772 &pt_shadow->tbo.mem);
Christian König0fc86832016-09-16 11:46:23 +0200773 if (r)
774 return r;
775 }
776
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400777 pt = amdgpu_bo_gpu_offset(bo);
Christian König194d2162016-10-12 15:13:52 +0200778 if (parent->entries[pt_idx].addr == pt)
Christian Königf8991ba2016-09-16 15:36:49 +0200779 continue;
780
Christian König194d2162016-10-12 15:13:52 +0200781 parent->entries[pt_idx].addr = pt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400782
783 pde = pd_addr + pt_idx * 8;
784 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +0200785 ((last_pt + incr * count) != pt) ||
786 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400787
788 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500789 uint64_t pt_addr =
790 amdgpu_vm_adjust_mc_addr(adev, last_pt);
791
Christian Königf8991ba2016-09-16 15:36:49 +0200792 if (shadow)
793 amdgpu_vm_do_set_ptes(&params,
794 last_shadow,
Alex Xiee60f8db2017-03-09 11:36:26 -0500795 pt_addr, count,
Christian Königf8991ba2016-09-16 15:36:49 +0200796 incr,
797 AMDGPU_PTE_VALID);
798
Christian Königafef8b82016-08-12 13:29:18 +0200799 amdgpu_vm_do_set_ptes(&params, last_pde,
Alex Xiee60f8db2017-03-09 11:36:26 -0500800 pt_addr, count, incr,
Christian Königafef8b82016-08-12 13:29:18 +0200801 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400802 }
803
804 count = 1;
805 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +0200806 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400807 last_pt = pt;
808 } else {
809 ++count;
810 }
811 }
812
Christian Königf8991ba2016-09-16 15:36:49 +0200813 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500814 uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt);
815
Christian König67003a12016-10-12 14:46:26 +0200816 if (vm->root.bo->shadow)
Alex Xiee60f8db2017-03-09 11:36:26 -0500817 amdgpu_vm_do_set_ptes(&params, last_shadow, pt_addr,
Christian Königf8991ba2016-09-16 15:36:49 +0200818 count, incr, AMDGPU_PTE_VALID);
819
Alex Xiee60f8db2017-03-09 11:36:26 -0500820 amdgpu_vm_do_set_ptes(&params, last_pde, pt_addr,
Christian Königafef8b82016-08-12 13:29:18 +0200821 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800822 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823
Christian Königf8991ba2016-09-16 15:36:49 +0200824 if (params.ib->length_dw == 0) {
825 amdgpu_job_free(job);
Christian König194d2162016-10-12 15:13:52 +0200826 } else {
827 amdgpu_ring_pad_ib(ring, params.ib);
828 amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv,
Christian Königf8991ba2016-09-16 15:36:49 +0200829 AMDGPU_FENCE_OWNER_VM);
Christian König194d2162016-10-12 15:13:52 +0200830 if (shadow)
831 amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
832 AMDGPU_FENCE_OWNER_VM);
Christian Königf8991ba2016-09-16 15:36:49 +0200833
Christian König194d2162016-10-12 15:13:52 +0200834 WARN_ON(params.ib->length_dw > ndw);
835 r = amdgpu_job_submit(job, ring, &vm->entity,
836 AMDGPU_FENCE_OWNER_VM, &fence);
837 if (r)
838 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +0200839
Christian König194d2162016-10-12 15:13:52 +0200840 amdgpu_bo_fence(parent->bo, fence, true);
841 dma_fence_put(vm->last_dir_update);
842 vm->last_dir_update = dma_fence_get(fence);
843 dma_fence_put(fence);
844 }
845 /*
846 * Recurse into the subdirectories. This recursion is harmless because
847 * we only have a maximum of 5 layers.
848 */
849 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
850 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
851
852 if (!entry->bo)
853 continue;
854
855 r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
856 if (r)
857 return r;
858 }
Christian Königf8991ba2016-09-16 15:36:49 +0200859
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400860 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800861
862error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100863 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800864 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865}
866
Christian König194d2162016-10-12 15:13:52 +0200867/*
868 * amdgpu_vm_update_directories - make sure that all directories are valid
869 *
870 * @adev: amdgpu_device pointer
871 * @vm: requested vm
872 *
873 * Makes sure all directories are up to date.
874 * Returns 0 for success, error for failure.
875 */
876int amdgpu_vm_update_directories(struct amdgpu_device *adev,
877 struct amdgpu_vm *vm)
878{
879 return amdgpu_vm_update_level(adev, vm, &vm->root, 0);
880}
881
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400882/**
Christian König92696dd2016-08-05 13:56:35 +0200883 * amdgpu_vm_update_ptes - make sure that page tables are valid
884 *
885 * @params: see amdgpu_pte_update_params definition
886 * @vm: requested vm
887 * @start: start of GPU address range
888 * @end: end of GPU address range
889 * @dst: destination address to map to, the next dst inside the function
890 * @flags: mapping flags
891 *
892 * Update the page tables in the range @start - @end.
893 */
894static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200895 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800896 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +0200897{
898 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
899
900 uint64_t cur_pe_start, cur_nptes, cur_dst;
901 uint64_t addr; /* next GPU address to be updated */
902 uint64_t pt_idx;
903 struct amdgpu_bo *pt;
904 unsigned nptes; /* next number of ptes to be updated */
905 uint64_t next_pe_start;
906
907 /* initialize the variables */
908 addr = start;
909 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200910 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800911 if (params->shadow) {
912 if (!pt->shadow)
913 return;
Christian König914b4dc2016-09-28 12:27:37 +0200914 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800915 }
Christian König92696dd2016-08-05 13:56:35 +0200916 if ((addr & ~mask) == (end & ~mask))
917 nptes = end - addr;
918 else
919 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
920
921 cur_pe_start = amdgpu_bo_gpu_offset(pt);
922 cur_pe_start += (addr & mask) * 8;
923 cur_nptes = nptes;
924 cur_dst = dst;
925
926 /* for next ptb*/
927 addr += nptes;
928 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
929
930 /* walk over the address space and update the page tables */
931 while (addr < end) {
932 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200933 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800934 if (params->shadow) {
935 if (!pt->shadow)
936 return;
Christian König914b4dc2016-09-28 12:27:37 +0200937 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800938 }
Christian König92696dd2016-08-05 13:56:35 +0200939
940 if ((addr & ~mask) == (end & ~mask))
941 nptes = end - addr;
942 else
943 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
944
945 next_pe_start = amdgpu_bo_gpu_offset(pt);
946 next_pe_start += (addr & mask) * 8;
947
Christian König96105e52016-08-12 12:59:59 +0200948 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
949 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
Christian König92696dd2016-08-05 13:56:35 +0200950 /* The next ptb is consecutive to current ptb.
Christian Königafef8b82016-08-12 13:29:18 +0200951 * Don't call the update function now.
Christian König92696dd2016-08-05 13:56:35 +0200952 * Will update two ptbs together in future.
953 */
954 cur_nptes += nptes;
955 } else {
Christian Königafef8b82016-08-12 13:29:18 +0200956 params->func(params, cur_pe_start, cur_dst, cur_nptes,
957 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200958
959 cur_pe_start = next_pe_start;
960 cur_nptes = nptes;
961 cur_dst = dst;
962 }
963
964 /* for next ptb*/
965 addr += nptes;
966 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
967 }
968
Christian Königafef8b82016-08-12 13:29:18 +0200969 params->func(params, cur_pe_start, cur_dst, cur_nptes,
970 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200971}
972
973/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 * amdgpu_vm_frag_ptes - add fragment information to PTEs
975 *
Christian König29efc4f2016-08-04 14:52:50 +0200976 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +0200977 * @vm: requested vm
978 * @start: first PTE to handle
979 * @end: last PTE to handle
980 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982 */
Christian König27c5f362016-08-04 15:02:49 +0200983static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200984 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800985 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986{
987 /**
988 * The MC L1 TLB supports variable sized pages, based on a fragment
989 * field in the PTE. When this field is set to a non-zero value, page
990 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
991 * flags are considered valid for all PTEs within the fragment range
992 * and corresponding mappings are assumed to be physically contiguous.
993 *
994 * The L1 TLB can store a single PTE for the whole fragment,
995 * significantly increasing the space available for translation
996 * caching. This leads to large improvements in throughput when the
997 * TLB is under pressure.
998 *
999 * The L2 TLB distributes small and large fragments into two
1000 * asymmetric partitions. The large fragment cache is significantly
1001 * larger. Thus, we try to use large fragments wherever possible.
1002 * Userspace can support this by aligning virtual base address and
1003 * allocation size to the fragment size.
1004 */
1005
Christian König80366172016-10-04 13:39:43 +02001006 /* SI and newer are optimized for 64KB */
1007 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
1008 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009
Christian König92696dd2016-08-05 13:56:35 +02001010 uint64_t frag_start = ALIGN(start, frag_align);
1011 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +01001012
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001013 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +02001014 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Christian König92696dd2016-08-05 13:56:35 +02001015 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016
Christian König49ac8a22016-10-13 15:09:08 +02001017 amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001018 return;
1019 }
1020
1021 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +02001022 if (start != frag_start) {
Christian König49ac8a22016-10-13 15:09:08 +02001023 amdgpu_vm_update_ptes(params, start, frag_start,
Christian König92696dd2016-08-05 13:56:35 +02001024 dst, flags);
1025 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001026 }
1027
1028 /* handle the area in the middle */
Christian König49ac8a22016-10-13 15:09:08 +02001029 amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
Christian König80366172016-10-04 13:39:43 +02001030 flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001031
1032 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +02001033 if (frag_end != end) {
1034 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Christian König49ac8a22016-10-13 15:09:08 +02001035 amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001036 }
1037}
1038
1039/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001040 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1041 *
1042 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001043 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001044 * @src: address where to copy page table entries from
1045 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001046 * @vm: requested vm
1047 * @start: start of mapped range
1048 * @last: last mapped entry
1049 * @flags: flags for the entries
1050 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001051 * @fence: optional resulting fence
1052 *
Christian Königa14faa62016-01-25 14:27:31 +01001053 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001054 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001055 */
1056static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001057 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001058 uint64_t src,
1059 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001060 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001061 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001062 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001063 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001064{
Christian König2d55e452016-02-08 17:37:38 +01001065 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001066 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001067 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001068 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001069 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001070 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001071 int r;
1072
Christian Königafef8b82016-08-12 13:29:18 +02001073 memset(&params, 0, sizeof(params));
1074 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001075 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001076 params.src = src;
1077
Christian König2d55e452016-02-08 17:37:38 +01001078 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001079
Christian Königa1e08d32016-01-26 11:40:46 +01001080 /* sync to everything on unmapping */
1081 if (!(flags & AMDGPU_PTE_VALID))
1082 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1083
Christian Königa14faa62016-01-25 14:27:31 +01001084 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001085
1086 /*
1087 * reserve space for one command every (1 << BLOCK_SIZE)
1088 * entries or 2k dwords (whatever is smaller)
1089 */
1090 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
1091
1092 /* padding, etc. */
1093 ndw = 64;
1094
Christian Königb0456f92016-08-11 14:06:54 +02001095 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001096 /* only copy commands needed */
1097 ndw += ncmds * 7;
1098
Christian Königafef8b82016-08-12 13:29:18 +02001099 params.func = amdgpu_vm_do_copy_ptes;
1100
Christian Königb0456f92016-08-11 14:06:54 +02001101 } else if (pages_addr) {
1102 /* copy commands needed */
1103 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001104
Christian Königb0456f92016-08-11 14:06:54 +02001105 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 ndw += nptes * 2;
1107
Christian Königafef8b82016-08-12 13:29:18 +02001108 params.func = amdgpu_vm_do_copy_ptes;
1109
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001110 } else {
1111 /* set page commands needed */
1112 ndw += ncmds * 10;
1113
1114 /* two extra commands for begin/end of fragment */
1115 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001116
1117 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118 }
1119
Christian Königd71518b2016-02-01 12:20:25 +01001120 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1121 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001122 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001123
Christian König29efc4f2016-08-04 14:52:50 +02001124 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001125
Christian Königb0456f92016-08-11 14:06:54 +02001126 if (!src && pages_addr) {
1127 uint64_t *pte;
1128 unsigned i;
1129
1130 /* Put the PTEs at the end of the IB. */
1131 i = ndw - nptes * 2;
1132 pte= (uint64_t *)&(job->ibs->ptr[i]);
1133 params.src = job->ibs->gpu_addr + i * 4;
1134
1135 for (i = 0; i < nptes; ++i) {
1136 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1137 AMDGPU_GPU_PAGE_SIZE);
1138 pte[i] |= flags;
1139 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001140 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001141 }
1142
Christian König3cabaa52016-06-06 10:17:58 +02001143 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1144 if (r)
1145 goto error_free;
1146
Christian König67003a12016-10-12 14:46:26 +02001147 r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001148 owner);
1149 if (r)
1150 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151
Christian König67003a12016-10-12 14:46:26 +02001152 r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001153 if (r)
1154 goto error_free;
1155
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001156 params.shadow = true;
Christian König49ac8a22016-10-13 15:09:08 +02001157 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001158 params.shadow = false;
Christian König49ac8a22016-10-13 15:09:08 +02001159 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160
Christian König29efc4f2016-08-04 14:52:50 +02001161 amdgpu_ring_pad_ib(ring, params.ib);
1162 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001163 r = amdgpu_job_submit(job, ring, &vm->entity,
1164 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001165 if (r)
1166 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001167
Christian König67003a12016-10-12 14:46:26 +02001168 amdgpu_bo_fence(vm->root.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001169 dma_fence_put(*fence);
1170 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001172
1173error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001174 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001175 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001176}
1177
1178/**
Christian Königa14faa62016-01-25 14:27:31 +01001179 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1180 *
1181 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001182 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001183 * @gtt_flags: flags as they are used for GTT
1184 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001185 * @vm: requested vm
1186 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001187 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001188 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001189 * @fence: optional resulting fence
1190 *
1191 * Split the mapping into smaller chunks so that each update fits
1192 * into a SDMA IB.
1193 * Returns 0 for success, -EINVAL for failure.
1194 */
1195static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001196 struct dma_fence *exclusive,
Chunming Zhou6b777602016-09-21 16:19:19 +08001197 uint64_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +02001198 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001199 struct amdgpu_vm *vm,
1200 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001201 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001202 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001203 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001204{
Christian König63e0ba42016-08-16 17:38:37 +02001205 uint64_t pfn, src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +01001206 int r;
1207
1208 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1209 * but in case of something, we filter the flags in first place
1210 */
1211 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1212 flags &= ~AMDGPU_PTE_READABLE;
1213 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1214 flags &= ~AMDGPU_PTE_WRITEABLE;
1215
Alex Xie15b31c52017-03-03 16:47:11 -05001216 flags &= ~AMDGPU_PTE_EXECUTABLE;
1217 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1218
Alex Xieb0fd18b2017-03-03 16:49:39 -05001219 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1220 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1221
Christian Königa14faa62016-01-25 14:27:31 +01001222 trace_amdgpu_vm_bo_update(mapping);
1223
Christian König63e0ba42016-08-16 17:38:37 +02001224 pfn = mapping->offset >> PAGE_SHIFT;
1225 if (nodes) {
1226 while (pfn >= nodes->size) {
1227 pfn -= nodes->size;
1228 ++nodes;
1229 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001230 }
Christian Königa14faa62016-01-25 14:27:31 +01001231
Christian König63e0ba42016-08-16 17:38:37 +02001232 do {
1233 uint64_t max_entries;
1234 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001235
Christian König63e0ba42016-08-16 17:38:37 +02001236 if (nodes) {
1237 addr = nodes->start << PAGE_SHIFT;
1238 max_entries = (nodes->size - pfn) *
1239 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1240 } else {
1241 addr = 0;
1242 max_entries = S64_MAX;
1243 }
Christian Königa14faa62016-01-25 14:27:31 +01001244
Christian König63e0ba42016-08-16 17:38:37 +02001245 if (pages_addr) {
1246 if (flags == gtt_flags)
1247 src = adev->gart.table_addr +
1248 (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
1249 else
1250 max_entries = min(max_entries, 16ull * 1024ull);
1251 addr = 0;
1252 } else if (flags & AMDGPU_PTE_VALID) {
1253 addr += adev->vm_manager.vram_base_offset;
1254 }
1255 addr += pfn << PAGE_SHIFT;
1256
1257 last = min((uint64_t)mapping->it.last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001258 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1259 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001260 start, last, flags, addr,
1261 fence);
1262 if (r)
1263 return r;
1264
Christian König63e0ba42016-08-16 17:38:37 +02001265 pfn += last - start + 1;
1266 if (nodes && nodes->size == pfn) {
1267 pfn = 0;
1268 ++nodes;
1269 }
Christian Königa14faa62016-01-25 14:27:31 +01001270 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001271
1272 } while (unlikely(start != mapping->it.last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001273
1274 return 0;
1275}
1276
1277/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1279 *
1280 * @adev: amdgpu_device pointer
1281 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001282 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 *
1284 * Fill in the page table entries for @bo_va.
1285 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001286 */
1287int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1288 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001289 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290{
1291 struct amdgpu_vm *vm = bo_va->vm;
1292 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001293 dma_addr_t *pages_addr = NULL;
Chunming Zhou6b777602016-09-21 16:19:19 +08001294 uint64_t gtt_flags, flags;
Christian König99e124f2016-08-16 14:43:17 +02001295 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001296 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001297 struct dma_fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298 int r;
1299
Christian Königa5f6b5b2017-01-30 11:01:38 +01001300 if (clear || !bo_va->bo) {
Christian König99e124f2016-08-16 14:43:17 +02001301 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001302 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001303 exclusive = NULL;
1304 } else {
Christian König8358dce2016-03-30 10:50:25 +02001305 struct ttm_dma_tt *ttm;
1306
Christian König99e124f2016-08-16 14:43:17 +02001307 mem = &bo_va->bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001308 nodes = mem->mm_node;
1309 if (mem->mem_type == TTM_PL_TT) {
Christian König8358dce2016-03-30 10:50:25 +02001310 ttm = container_of(bo_va->bo->tbo.ttm, struct
1311 ttm_dma_tt, ttm);
1312 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001313 }
Christian König3cabaa52016-06-06 10:17:58 +02001314 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 }
1316
Christian Königa5f6b5b2017-01-30 11:01:38 +01001317 if (bo_va->bo) {
1318 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1319 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1320 adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
1321 flags : 0;
1322 } else {
1323 flags = 0x0;
1324 gtt_flags = ~0x0;
1325 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001326
Christian König7fc11952015-07-30 11:53:42 +02001327 spin_lock(&vm->status_lock);
1328 if (!list_empty(&bo_va->vm_status))
1329 list_splice_init(&bo_va->valids, &bo_va->invalids);
1330 spin_unlock(&vm->status_lock);
1331
1332 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001333 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1334 gtt_flags, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001335 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001336 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001337 if (r)
1338 return r;
1339 }
1340
Christian Königd6c10f62015-09-28 12:00:23 +02001341 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1342 list_for_each_entry(mapping, &bo_va->valids, list)
1343 trace_amdgpu_vm_bo_mapping(mapping);
1344
1345 list_for_each_entry(mapping, &bo_va->invalids, list)
1346 trace_amdgpu_vm_bo_mapping(mapping);
1347 }
1348
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001350 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001351 list_del_init(&bo_va->vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001352 if (clear)
Christian König7fc11952015-07-30 11:53:42 +02001353 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001354 spin_unlock(&vm->status_lock);
1355
1356 return 0;
1357}
1358
1359/**
Christian König284710f2017-01-30 11:09:31 +01001360 * amdgpu_vm_update_prt_state - update the global PRT state
1361 */
1362static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1363{
1364 unsigned long flags;
1365 bool enable;
1366
1367 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001368 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001369 adev->gart.gart_funcs->set_prt(adev, enable);
1370 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1371}
1372
1373/**
Christian König4388fc22017-03-13 10:13:36 +01001374 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001375 */
1376static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1377{
Christian König4388fc22017-03-13 10:13:36 +01001378 if (!adev->gart.gart_funcs->set_prt)
1379 return;
1380
Christian König451bc8e2017-02-14 16:02:52 +01001381 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1382 amdgpu_vm_update_prt_state(adev);
1383}
1384
1385/**
Christian König0b15f2f2017-02-14 15:47:03 +01001386 * amdgpu_vm_prt_put - drop a PRT user
1387 */
1388static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1389{
Christian König451bc8e2017-02-14 16:02:52 +01001390 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001391 amdgpu_vm_update_prt_state(adev);
1392}
1393
1394/**
Christian König451bc8e2017-02-14 16:02:52 +01001395 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001396 */
1397static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1398{
1399 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1400
Christian König0b15f2f2017-02-14 15:47:03 +01001401 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001402 kfree(cb);
1403}
1404
1405/**
Christian König451bc8e2017-02-14 16:02:52 +01001406 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1407 */
1408static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1409 struct dma_fence *fence)
1410{
Christian König4388fc22017-03-13 10:13:36 +01001411 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001412
Christian König4388fc22017-03-13 10:13:36 +01001413 if (!adev->gart.gart_funcs->set_prt)
1414 return;
1415
1416 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001417 if (!cb) {
1418 /* Last resort when we are OOM */
1419 if (fence)
1420 dma_fence_wait(fence, false);
1421
1422 amdgpu_vm_prt_put(cb->adev);
1423 } else {
1424 cb->adev = adev;
1425 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1426 amdgpu_vm_prt_cb))
1427 amdgpu_vm_prt_cb(fence, &cb->cb);
1428 }
1429}
1430
1431/**
Christian König284710f2017-01-30 11:09:31 +01001432 * amdgpu_vm_free_mapping - free a mapping
1433 *
1434 * @adev: amdgpu_device pointer
1435 * @vm: requested vm
1436 * @mapping: mapping to be freed
1437 * @fence: fence of the unmap operation
1438 *
1439 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1440 */
1441static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1442 struct amdgpu_vm *vm,
1443 struct amdgpu_bo_va_mapping *mapping,
1444 struct dma_fence *fence)
1445{
Christian König451bc8e2017-02-14 16:02:52 +01001446 if (mapping->flags & AMDGPU_PTE_PRT)
1447 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001448 kfree(mapping);
1449}
1450
1451/**
Christian König451bc8e2017-02-14 16:02:52 +01001452 * amdgpu_vm_prt_fini - finish all prt mappings
1453 *
1454 * @adev: amdgpu_device pointer
1455 * @vm: requested vm
1456 *
1457 * Register a cleanup callback to disable PRT support after VM dies.
1458 */
1459static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1460{
Christian König67003a12016-10-12 14:46:26 +02001461 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001462 struct dma_fence *excl, **shared;
1463 unsigned i, shared_count;
1464 int r;
1465
1466 r = reservation_object_get_fences_rcu(resv, &excl,
1467 &shared_count, &shared);
1468 if (r) {
1469 /* Not enough memory to grab the fence list, as last resort
1470 * block for all the fences to complete.
1471 */
1472 reservation_object_wait_timeout_rcu(resv, true, false,
1473 MAX_SCHEDULE_TIMEOUT);
1474 return;
1475 }
1476
1477 /* Add a callback for each fence in the reservation object */
1478 amdgpu_vm_prt_get(adev);
1479 amdgpu_vm_add_prt_cb(adev, excl);
1480
1481 for (i = 0; i < shared_count; ++i) {
1482 amdgpu_vm_prt_get(adev);
1483 amdgpu_vm_add_prt_cb(adev, shared[i]);
1484 }
1485
1486 kfree(shared);
1487}
1488
1489/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001490 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1491 *
1492 * @adev: amdgpu_device pointer
1493 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001494 * @fence: optional resulting fence (unchanged if no work needed to be done
1495 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001496 *
1497 * Make sure all freed BOs are cleared in the PT.
1498 * Returns 0 for success.
1499 *
1500 * PTs have to be reserved and mutex must be locked!
1501 */
1502int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001503 struct amdgpu_vm *vm,
1504 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505{
1506 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001507 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001508 int r;
1509
1510 while (!list_empty(&vm->freed)) {
1511 mapping = list_first_entry(&vm->freed,
1512 struct amdgpu_bo_va_mapping, list);
1513 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001514
Christian König3cabaa52016-06-06 10:17:58 +02001515 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001516 0, 0, &f);
1517 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001518 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001519 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001520 return r;
Christian König284710f2017-01-30 11:09:31 +01001521 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001522 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001523
1524 if (fence && f) {
1525 dma_fence_put(*fence);
1526 *fence = f;
1527 } else {
1528 dma_fence_put(f);
1529 }
1530
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001531 return 0;
1532
1533}
1534
1535/**
1536 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1537 *
1538 * @adev: amdgpu_device pointer
1539 * @vm: requested vm
1540 *
1541 * Make sure all invalidated BOs are cleared in the PT.
1542 * Returns 0 for success.
1543 *
1544 * PTs have to be reserved and mutex must be locked!
1545 */
1546int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001547 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001548{
monk.liucfe2c972015-05-26 15:01:54 +08001549 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001550 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551
1552 spin_lock(&vm->status_lock);
1553 while (!list_empty(&vm->invalidated)) {
1554 bo_va = list_first_entry(&vm->invalidated,
1555 struct amdgpu_bo_va, vm_status);
1556 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001557
Christian König99e124f2016-08-16 14:43:17 +02001558 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001559 if (r)
1560 return r;
1561
1562 spin_lock(&vm->status_lock);
1563 }
1564 spin_unlock(&vm->status_lock);
1565
monk.liucfe2c972015-05-26 15:01:54 +08001566 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001567 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001568
1569 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001570}
1571
1572/**
1573 * amdgpu_vm_bo_add - add a bo to a specific vm
1574 *
1575 * @adev: amdgpu_device pointer
1576 * @vm: requested vm
1577 * @bo: amdgpu buffer object
1578 *
Christian König8843dbb2016-01-26 12:17:11 +01001579 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580 * Add @bo to the list of bos associated with the vm
1581 * Returns newly added bo_va or NULL for failure
1582 *
1583 * Object has to be reserved!
1584 */
1585struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1586 struct amdgpu_vm *vm,
1587 struct amdgpu_bo *bo)
1588{
1589 struct amdgpu_bo_va *bo_va;
1590
1591 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1592 if (bo_va == NULL) {
1593 return NULL;
1594 }
1595 bo_va->vm = vm;
1596 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001597 bo_va->ref_count = 1;
1598 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001599 INIT_LIST_HEAD(&bo_va->valids);
1600 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001602
Christian Königa5f6b5b2017-01-30 11:01:38 +01001603 if (bo)
1604 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001605
1606 return bo_va;
1607}
1608
1609/**
1610 * amdgpu_vm_bo_map - map bo inside a vm
1611 *
1612 * @adev: amdgpu_device pointer
1613 * @bo_va: bo_va to store the address
1614 * @saddr: where to map the BO
1615 * @offset: requested offset in the BO
1616 * @flags: attributes of pages (read/write/valid/etc.)
1617 *
1618 * Add a mapping of the BO at the specefied addr into the VM.
1619 * Returns 0 for success, error for failure.
1620 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001621 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001622 */
1623int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1624 struct amdgpu_bo_va *bo_va,
1625 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001626 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001627{
1628 struct amdgpu_bo_va_mapping *mapping;
1629 struct amdgpu_vm *vm = bo_va->vm;
1630 struct interval_tree_node *it;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001631 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001632
Christian König0be52de2015-05-18 14:37:27 +02001633 /* validate the parameters */
1634 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001635 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001636 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001637
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001639 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001640 if (saddr >= eaddr ||
1641 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001644 saddr /= AMDGPU_GPU_PAGE_SIZE;
1645 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1646
Felix Kuehling005ae952015-11-23 17:43:48 -05001647 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001648 if (it) {
1649 struct amdgpu_bo_va_mapping *tmp;
1650 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1651 /* bo and tmp overlap, invalid addr */
1652 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1653 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1654 tmp->it.start, tmp->it.last + 1);
Christian König663e4572017-03-13 10:13:37 +01001655 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656 }
1657
1658 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001659 if (!mapping)
1660 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001661
1662 INIT_LIST_HEAD(&mapping->list);
1663 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001664 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001665 mapping->offset = offset;
1666 mapping->flags = flags;
1667
Christian König7fc11952015-07-30 11:53:42 +02001668 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669 interval_tree_insert(&mapping->it, &vm->va);
1670
Christian König4388fc22017-03-13 10:13:36 +01001671 if (flags & AMDGPU_PTE_PRT)
1672 amdgpu_vm_prt_get(adev);
1673
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675}
1676
1677/**
Christian König80f95c52017-03-13 10:13:39 +01001678 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1679 *
1680 * @adev: amdgpu_device pointer
1681 * @bo_va: bo_va to store the address
1682 * @saddr: where to map the BO
1683 * @offset: requested offset in the BO
1684 * @flags: attributes of pages (read/write/valid/etc.)
1685 *
1686 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1687 * mappings as we do so.
1688 * Returns 0 for success, error for failure.
1689 *
1690 * Object has to be reserved and unreserved outside!
1691 */
1692int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1693 struct amdgpu_bo_va *bo_va,
1694 uint64_t saddr, uint64_t offset,
1695 uint64_t size, uint64_t flags)
1696{
1697 struct amdgpu_bo_va_mapping *mapping;
1698 struct amdgpu_vm *vm = bo_va->vm;
1699 uint64_t eaddr;
1700 int r;
1701
1702 /* validate the parameters */
1703 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1704 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1705 return -EINVAL;
1706
1707 /* make sure object fit at this offset */
1708 eaddr = saddr + size - 1;
1709 if (saddr >= eaddr ||
1710 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
1711 return -EINVAL;
1712
1713 /* Allocate all the needed memory */
1714 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1715 if (!mapping)
1716 return -ENOMEM;
1717
1718 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
1719 if (r) {
1720 kfree(mapping);
1721 return r;
1722 }
1723
1724 saddr /= AMDGPU_GPU_PAGE_SIZE;
1725 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1726
1727 mapping->it.start = saddr;
1728 mapping->it.last = eaddr;
1729 mapping->offset = offset;
1730 mapping->flags = flags;
1731
1732 list_add(&mapping->list, &bo_va->invalids);
1733 interval_tree_insert(&mapping->it, &vm->va);
1734
1735 if (flags & AMDGPU_PTE_PRT)
1736 amdgpu_vm_prt_get(adev);
1737
1738 return 0;
1739}
1740
1741/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001742 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1743 *
1744 * @adev: amdgpu_device pointer
1745 * @bo_va: bo_va to remove the address from
1746 * @saddr: where to the BO is mapped
1747 *
1748 * Remove a mapping of the BO at the specefied addr from the VM.
1749 * Returns 0 for success, error for failure.
1750 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001751 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752 */
1753int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1754 struct amdgpu_bo_va *bo_va,
1755 uint64_t saddr)
1756{
1757 struct amdgpu_bo_va_mapping *mapping;
1758 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001759 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001760
Christian König6c7fc502015-06-05 20:56:17 +02001761 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001762
Christian König7fc11952015-07-30 11:53:42 +02001763 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001764 if (mapping->it.start == saddr)
1765 break;
1766 }
1767
Christian König7fc11952015-07-30 11:53:42 +02001768 if (&mapping->list == &bo_va->valids) {
1769 valid = false;
1770
1771 list_for_each_entry(mapping, &bo_va->invalids, list) {
1772 if (mapping->it.start == saddr)
1773 break;
1774 }
1775
Christian König32b41ac2016-03-08 18:03:27 +01001776 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001777 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 }
Christian König32b41ac2016-03-08 18:03:27 +01001779
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780 list_del(&mapping->list);
1781 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001782 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783
Christian Könige17841b2016-03-08 17:52:01 +01001784 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001786 else
Christian König284710f2017-01-30 11:09:31 +01001787 amdgpu_vm_free_mapping(adev, vm, mapping,
1788 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789
1790 return 0;
1791}
1792
1793/**
Christian Königdc54d3d2017-03-13 10:13:38 +01001794 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1795 *
1796 * @adev: amdgpu_device pointer
1797 * @vm: VM structure to use
1798 * @saddr: start of the range
1799 * @size: size of the range
1800 *
1801 * Remove all mappings in a range, split them as appropriate.
1802 * Returns 0 for success, error for failure.
1803 */
1804int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1805 struct amdgpu_vm *vm,
1806 uint64_t saddr, uint64_t size)
1807{
1808 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1809 struct interval_tree_node *it;
1810 LIST_HEAD(removed);
1811 uint64_t eaddr;
1812
1813 eaddr = saddr + size - 1;
1814 saddr /= AMDGPU_GPU_PAGE_SIZE;
1815 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1816
1817 /* Allocate all the needed memory */
1818 before = kzalloc(sizeof(*before), GFP_KERNEL);
1819 if (!before)
1820 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08001821 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001822
1823 after = kzalloc(sizeof(*after), GFP_KERNEL);
1824 if (!after) {
1825 kfree(before);
1826 return -ENOMEM;
1827 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08001828 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001829
1830 /* Now gather all removed mappings */
1831 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
1832 while (it) {
1833 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1834 it = interval_tree_iter_next(it, saddr, eaddr);
1835
1836 /* Remember mapping split at the start */
1837 if (tmp->it.start < saddr) {
Junwei Zhang27f6d612017-03-16 16:09:24 +08001838 before->it.start = tmp->it.start;
Christian Königdc54d3d2017-03-13 10:13:38 +01001839 before->it.last = saddr - 1;
1840 before->offset = tmp->offset;
1841 before->flags = tmp->flags;
1842 list_add(&before->list, &tmp->list);
1843 }
1844
1845 /* Remember mapping split at the end */
1846 if (tmp->it.last > eaddr) {
1847 after->it.start = eaddr + 1;
1848 after->it.last = tmp->it.last;
1849 after->offset = tmp->offset;
1850 after->offset += after->it.start - tmp->it.start;
1851 after->flags = tmp->flags;
1852 list_add(&after->list, &tmp->list);
1853 }
1854
1855 list_del(&tmp->list);
1856 list_add(&tmp->list, &removed);
1857 }
1858
1859 /* And free them up */
1860 list_for_each_entry_safe(tmp, next, &removed, list) {
1861 interval_tree_remove(&tmp->it, &vm->va);
1862 list_del(&tmp->list);
1863
1864 if (tmp->it.start < saddr)
1865 tmp->it.start = saddr;
1866 if (tmp->it.last > eaddr)
1867 tmp->it.last = eaddr;
1868
1869 list_add(&tmp->list, &vm->freed);
1870 trace_amdgpu_vm_bo_unmap(NULL, tmp);
1871 }
1872
Junwei Zhang27f6d612017-03-16 16:09:24 +08001873 /* Insert partial mapping before the range */
1874 if (!list_empty(&before->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001875 interval_tree_insert(&before->it, &vm->va);
1876 if (before->flags & AMDGPU_PTE_PRT)
1877 amdgpu_vm_prt_get(adev);
1878 } else {
1879 kfree(before);
1880 }
1881
1882 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08001883 if (!list_empty(&after->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001884 interval_tree_insert(&after->it, &vm->va);
1885 if (after->flags & AMDGPU_PTE_PRT)
1886 amdgpu_vm_prt_get(adev);
1887 } else {
1888 kfree(after);
1889 }
1890
1891 return 0;
1892}
1893
1894/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001895 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1896 *
1897 * @adev: amdgpu_device pointer
1898 * @bo_va: requested bo_va
1899 *
Christian König8843dbb2016-01-26 12:17:11 +01001900 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001901 *
1902 * Object have to be reserved!
1903 */
1904void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1905 struct amdgpu_bo_va *bo_va)
1906{
1907 struct amdgpu_bo_va_mapping *mapping, *next;
1908 struct amdgpu_vm *vm = bo_va->vm;
1909
1910 list_del(&bo_va->bo_list);
1911
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001912 spin_lock(&vm->status_lock);
1913 list_del(&bo_va->vm_status);
1914 spin_unlock(&vm->status_lock);
1915
Christian König7fc11952015-07-30 11:53:42 +02001916 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001917 list_del(&mapping->list);
1918 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001919 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001920 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001921 }
Christian König7fc11952015-07-30 11:53:42 +02001922 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1923 list_del(&mapping->list);
1924 interval_tree_remove(&mapping->it, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01001925 amdgpu_vm_free_mapping(adev, vm, mapping,
1926 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02001927 }
Christian König32b41ac2016-03-08 18:03:27 +01001928
Chris Wilsonf54d1862016-10-25 13:00:45 +01001929 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001930 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931}
1932
1933/**
1934 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1935 *
1936 * @adev: amdgpu_device pointer
1937 * @vm: requested vm
1938 * @bo: amdgpu buffer object
1939 *
Christian König8843dbb2016-01-26 12:17:11 +01001940 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001941 */
1942void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1943 struct amdgpu_bo *bo)
1944{
1945 struct amdgpu_bo_va *bo_va;
1946
1947 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001948 spin_lock(&bo_va->vm->status_lock);
1949 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001950 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001951 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001952 }
1953}
1954
1955/**
1956 * amdgpu_vm_init - initialize a vm instance
1957 *
1958 * @adev: amdgpu_device pointer
1959 * @vm: requested vm
1960 *
Christian König8843dbb2016-01-26 12:17:11 +01001961 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962 */
1963int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1964{
1965 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1966 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001967 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001968 unsigned ring_instance;
1969 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001970 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001971 int i, r;
1972
Christian Königbcb1ba32016-03-08 15:40:11 +01001973 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1974 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001976 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001977 spin_lock_init(&vm->status_lock);
1978 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001979 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001980 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001981
Christian König72a7ec52016-10-19 11:03:57 +02001982 pd_size = amdgpu_vm_bo_size(adev, 0);
1983 pd_entries = amdgpu_vm_num_entries(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984
1985 /* allocate page table array */
Christian König67003a12016-10-12 14:46:26 +02001986 vm->root.entries = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
1987 if (vm->root.entries == NULL) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001988 DRM_ERROR("Cannot allocate memory for page table array\n");
1989 return -ENOMEM;
1990 }
1991
Christian König2bd9ccf2016-02-01 12:53:58 +01001992 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001993
1994 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1995 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1996 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001997 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1998 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1999 rq, amdgpu_sched_jobs);
2000 if (r)
Chunming Zhou64827ad2016-07-28 17:20:32 +08002001 goto err;
Christian König2bd9ccf2016-02-01 12:53:58 +01002002
Christian Königa24960f2016-10-12 13:20:52 +02002003 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002004
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002006 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08002007 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
Christian König03f48dd2016-08-15 17:00:22 +02002008 AMDGPU_GEM_CREATE_SHADOW |
Christian König617859e2016-11-17 15:40:02 +01002009 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2010 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Christian König67003a12016-10-12 14:46:26 +02002011 NULL, NULL, &vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002012 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002013 goto error_free_sched_entity;
2014
Christian König67003a12016-10-12 14:46:26 +02002015 r = amdgpu_bo_reserve(vm->root.bo, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01002016 if (r)
Christian König67003a12016-10-12 14:46:26 +02002017 goto error_free_root;
Christian König2bd9ccf2016-02-01 12:53:58 +01002018
Christian König5a712a82016-06-21 16:28:15 +02002019 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König67003a12016-10-12 14:46:26 +02002020 amdgpu_bo_unreserve(vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002021
2022 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002023
Christian König67003a12016-10-12 14:46:26 +02002024error_free_root:
2025 amdgpu_bo_unref(&vm->root.bo->shadow);
2026 amdgpu_bo_unref(&vm->root.bo);
2027 vm->root.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002028
2029error_free_sched_entity:
2030 amd_sched_entity_fini(&ring->sched, &vm->entity);
2031
Chunming Zhou64827ad2016-07-28 17:20:32 +08002032err:
Christian König67003a12016-10-12 14:46:26 +02002033 drm_free_large(vm->root.entries);
Chunming Zhou64827ad2016-07-28 17:20:32 +08002034
Christian König2bd9ccf2016-02-01 12:53:58 +01002035 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036}
2037
2038/**
2039 * amdgpu_vm_fini - tear down a vm instance
2040 *
2041 * @adev: amdgpu_device pointer
2042 * @vm: requested vm
2043 *
Christian König8843dbb2016-01-26 12:17:11 +01002044 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002045 * Unbind the VM and remove all bos from the vm bo list
2046 */
2047void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2048{
2049 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002050 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002051 int i;
2052
Christian König2d55e452016-02-08 17:37:38 +01002053 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002054
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002055 if (!RB_EMPTY_ROOT(&vm->va)) {
2056 dev_err(adev->dev, "still active bo inside vm\n");
2057 }
2058 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
2059 list_del(&mapping->list);
2060 interval_tree_remove(&mapping->it, &vm->va);
2061 kfree(mapping);
2062 }
2063 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002064 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002065 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002066 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002067 }
Christian König284710f2017-01-30 11:09:31 +01002068
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002070 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071 }
2072
Christian König72a7ec52016-10-19 11:03:57 +02002073 for (i = 0; i < amdgpu_vm_num_entries(adev, 0); i++) {
Christian König67003a12016-10-12 14:46:26 +02002074 struct amdgpu_bo *pt = vm->root.entries[i].bo;
Christian König2698f622016-09-16 13:06:09 +02002075
2076 if (!pt)
2077 continue;
2078
2079 amdgpu_bo_unref(&pt->shadow);
2080 amdgpu_bo_unref(&pt);
Chunming Zhou1baa4392016-08-04 13:59:32 +08002081 }
Christian König67003a12016-10-12 14:46:26 +02002082 drm_free_large(vm->root.entries);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002083
Christian König67003a12016-10-12 14:46:26 +02002084 amdgpu_bo_unref(&vm->root.bo->shadow);
2085 amdgpu_bo_unref(&vm->root.bo);
Christian Königa24960f2016-10-12 13:20:52 +02002086 dma_fence_put(vm->last_dir_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087}
Christian Königea89f8c2015-11-15 20:52:06 +01002088
2089/**
Christian Königa9a78b32016-01-21 10:19:11 +01002090 * amdgpu_vm_manager_init - init the VM manager
2091 *
2092 * @adev: amdgpu_device pointer
2093 *
2094 * Initialize the VM manager structures
2095 */
2096void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2097{
2098 unsigned i;
2099
2100 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
2101
2102 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01002103 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
2104 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01002105 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01002106 list_add_tail(&adev->vm_manager.ids[i].list,
2107 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01002108 }
Christian König2d55e452016-02-08 17:37:38 +01002109
Chris Wilsonf54d1862016-10-25 13:00:45 +01002110 adev->vm_manager.fence_context =
2111 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002112 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2113 adev->vm_manager.seqno[i] = 0;
2114
Christian König2d55e452016-02-08 17:37:38 +01002115 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002116 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002117 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002118 atomic_set(&adev->vm_manager.num_prt_users, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01002119}
2120
2121/**
Christian Königea89f8c2015-11-15 20:52:06 +01002122 * amdgpu_vm_manager_fini - cleanup VM manager
2123 *
2124 * @adev: amdgpu_device pointer
2125 *
2126 * Cleanup the VM manager and free resources.
2127 */
2128void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2129{
2130 unsigned i;
2131
Christian Königbcb1ba32016-03-08 15:40:11 +01002132 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
2133 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
2134
Chris Wilsonf54d1862016-10-25 13:00:45 +01002135 dma_fence_put(adev->vm_manager.ids[i].first);
Christian König832a9022016-02-15 12:33:02 +01002136 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Chris Wilsonf54d1862016-10-25 13:00:45 +01002137 dma_fence_put(id->flushed_updates);
Dave Airlie7b624ad2016-11-07 09:37:09 +10002138 dma_fence_put(id->last_flush);
Christian Königbcb1ba32016-03-08 15:40:11 +01002139 }
Christian Königea89f8c2015-11-15 20:52:06 +01002140}