blob: d48ea0f0d825395c2b906afd87f8d393677647f5 [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/*
703 * amdgpu_vm_update_pdes - make sure that page directory is valid
704 *
705 * @adev: amdgpu_device pointer
706 * @vm: requested vm
707 * @start: start of GPU address range
708 * @end: end of GPU address range
709 *
710 * Allocates new page tables if necessary
711 * and updates the page directory.
712 * Returns 0 for success, error for failure.
713 */
714int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
715 struct amdgpu_vm *vm)
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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
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önig2d55e452016-02-08 17:37:38 +0100729 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König67003a12016-10-12 14:46:26 +0200730 shadow = vm->root.bo->shadow;
Christian König2d55e452016-02-08 17:37:38 +0100731
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 /* padding, etc. */
733 ndw = 64;
734
735 /* assume the worst case */
Christian König67003a12016-10-12 14:46:26 +0200736 ndw += vm->root.last_entry_used * 6;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737
Christian König67003a12016-10-12 14:46:26 +0200738 pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
Christian Königf8991ba2016-09-16 15:36:49 +0200739 if (shadow) {
740 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
741 if (r)
742 return r;
743 shadow_addr = amdgpu_bo_gpu_offset(shadow);
744 ndw *= 2;
745 } else {
746 shadow_addr = 0;
747 }
748
Christian Königd71518b2016-02-01 12:20:25 +0100749 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
750 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100752
Christian König27c5f362016-08-04 15:02:49 +0200753 memset(&params, 0, sizeof(params));
754 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200755 params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756
757 /* walk over the address space and update the page directory */
Christian König67003a12016-10-12 14:46:26 +0200758 for (pt_idx = 0; pt_idx <= vm->root.last_entry_used; ++pt_idx) {
759 struct amdgpu_bo *bo = vm->root.entries[pt_idx].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400760 uint64_t pde, pt;
761
762 if (bo == NULL)
763 continue;
764
Christian König0fc86832016-09-16 11:46:23 +0200765 if (bo->shadow) {
Christian Königf8991ba2016-09-16 15:36:49 +0200766 struct amdgpu_bo *pt_shadow = bo->shadow;
Christian König0fc86832016-09-16 11:46:23 +0200767
Christian Königf8991ba2016-09-16 15:36:49 +0200768 r = amdgpu_ttm_bind(&pt_shadow->tbo,
769 &pt_shadow->tbo.mem);
Christian König0fc86832016-09-16 11:46:23 +0200770 if (r)
771 return r;
772 }
773
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400774 pt = amdgpu_bo_gpu_offset(bo);
Christian König67003a12016-10-12 14:46:26 +0200775 if (vm->root.entries[pt_idx].addr == pt)
Christian Königf8991ba2016-09-16 15:36:49 +0200776 continue;
777
Christian König67003a12016-10-12 14:46:26 +0200778 vm->root.entries[pt_idx].addr = pt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779
780 pde = pd_addr + pt_idx * 8;
781 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +0200782 ((last_pt + incr * count) != pt) ||
783 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784
785 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500786 uint64_t pt_addr =
787 amdgpu_vm_adjust_mc_addr(adev, last_pt);
788
Christian Königf8991ba2016-09-16 15:36:49 +0200789 if (shadow)
790 amdgpu_vm_do_set_ptes(&params,
791 last_shadow,
Alex Xiee60f8db2017-03-09 11:36:26 -0500792 pt_addr, count,
Christian Königf8991ba2016-09-16 15:36:49 +0200793 incr,
794 AMDGPU_PTE_VALID);
795
Christian Königafef8b82016-08-12 13:29:18 +0200796 amdgpu_vm_do_set_ptes(&params, last_pde,
Alex Xiee60f8db2017-03-09 11:36:26 -0500797 pt_addr, count, incr,
Christian Königafef8b82016-08-12 13:29:18 +0200798 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400799 }
800
801 count = 1;
802 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +0200803 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 last_pt = pt;
805 } else {
806 ++count;
807 }
808 }
809
Christian Königf8991ba2016-09-16 15:36:49 +0200810 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500811 uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt);
812
Christian König67003a12016-10-12 14:46:26 +0200813 if (vm->root.bo->shadow)
Alex Xiee60f8db2017-03-09 11:36:26 -0500814 amdgpu_vm_do_set_ptes(&params, last_shadow, pt_addr,
Christian Königf8991ba2016-09-16 15:36:49 +0200815 count, incr, AMDGPU_PTE_VALID);
816
Alex Xiee60f8db2017-03-09 11:36:26 -0500817 amdgpu_vm_do_set_ptes(&params, last_pde, pt_addr,
Christian Königafef8b82016-08-12 13:29:18 +0200818 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800819 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820
Christian Königf8991ba2016-09-16 15:36:49 +0200821 if (params.ib->length_dw == 0) {
822 amdgpu_job_free(job);
823 return 0;
824 }
825
826 amdgpu_ring_pad_ib(ring, params.ib);
Christian König67003a12016-10-12 14:46:26 +0200827 amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königf8991ba2016-09-16 15:36:49 +0200828 AMDGPU_FENCE_OWNER_VM);
829 if (shadow)
830 amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
831 AMDGPU_FENCE_OWNER_VM);
832
833 WARN_ON(params.ib->length_dw > ndw);
834 r = amdgpu_job_submit(job, ring, &vm->entity,
835 AMDGPU_FENCE_OWNER_VM, &fence);
836 if (r)
837 goto error_free;
838
Christian König67003a12016-10-12 14:46:26 +0200839 amdgpu_bo_fence(vm->root.bo, fence, true);
Christian Königa24960f2016-10-12 13:20:52 +0200840 dma_fence_put(vm->last_dir_update);
841 vm->last_dir_update = dma_fence_get(fence);
Dave Airlie220196b2016-10-28 11:33:52 +1000842 dma_fence_put(fence);
Christian Königf8991ba2016-09-16 15:36:49 +0200843
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800845
846error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100847 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800848 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400849}
850
851/**
Christian König92696dd2016-08-05 13:56:35 +0200852 * amdgpu_vm_update_ptes - make sure that page tables are valid
853 *
854 * @params: see amdgpu_pte_update_params definition
855 * @vm: requested vm
856 * @start: start of GPU address range
857 * @end: end of GPU address range
858 * @dst: destination address to map to, the next dst inside the function
859 * @flags: mapping flags
860 *
861 * Update the page tables in the range @start - @end.
862 */
863static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200864 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800865 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +0200866{
867 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
868
869 uint64_t cur_pe_start, cur_nptes, cur_dst;
870 uint64_t addr; /* next GPU address to be updated */
871 uint64_t pt_idx;
872 struct amdgpu_bo *pt;
873 unsigned nptes; /* next number of ptes to be updated */
874 uint64_t next_pe_start;
875
876 /* initialize the variables */
877 addr = start;
878 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200879 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800880 if (params->shadow) {
881 if (!pt->shadow)
882 return;
Christian König914b4dc2016-09-28 12:27:37 +0200883 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800884 }
Christian König92696dd2016-08-05 13:56:35 +0200885 if ((addr & ~mask) == (end & ~mask))
886 nptes = end - addr;
887 else
888 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
889
890 cur_pe_start = amdgpu_bo_gpu_offset(pt);
891 cur_pe_start += (addr & mask) * 8;
892 cur_nptes = nptes;
893 cur_dst = dst;
894
895 /* for next ptb*/
896 addr += nptes;
897 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
898
899 /* walk over the address space and update the page tables */
900 while (addr < end) {
901 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200902 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800903 if (params->shadow) {
904 if (!pt->shadow)
905 return;
Christian König914b4dc2016-09-28 12:27:37 +0200906 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800907 }
Christian König92696dd2016-08-05 13:56:35 +0200908
909 if ((addr & ~mask) == (end & ~mask))
910 nptes = end - addr;
911 else
912 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
913
914 next_pe_start = amdgpu_bo_gpu_offset(pt);
915 next_pe_start += (addr & mask) * 8;
916
Christian König96105e52016-08-12 12:59:59 +0200917 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
918 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
Christian König92696dd2016-08-05 13:56:35 +0200919 /* The next ptb is consecutive to current ptb.
Christian Königafef8b82016-08-12 13:29:18 +0200920 * Don't call the update function now.
Christian König92696dd2016-08-05 13:56:35 +0200921 * Will update two ptbs together in future.
922 */
923 cur_nptes += nptes;
924 } else {
Christian Königafef8b82016-08-12 13:29:18 +0200925 params->func(params, cur_pe_start, cur_dst, cur_nptes,
926 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200927
928 cur_pe_start = next_pe_start;
929 cur_nptes = nptes;
930 cur_dst = dst;
931 }
932
933 /* for next ptb*/
934 addr += nptes;
935 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
936 }
937
Christian Königafef8b82016-08-12 13:29:18 +0200938 params->func(params, cur_pe_start, cur_dst, cur_nptes,
939 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200940}
941
942/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943 * amdgpu_vm_frag_ptes - add fragment information to PTEs
944 *
Christian König29efc4f2016-08-04 14:52:50 +0200945 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +0200946 * @vm: requested vm
947 * @start: first PTE to handle
948 * @end: last PTE to handle
949 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951 */
Christian König27c5f362016-08-04 15:02:49 +0200952static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200953 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800954 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955{
956 /**
957 * The MC L1 TLB supports variable sized pages, based on a fragment
958 * field in the PTE. When this field is set to a non-zero value, page
959 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
960 * flags are considered valid for all PTEs within the fragment range
961 * and corresponding mappings are assumed to be physically contiguous.
962 *
963 * The L1 TLB can store a single PTE for the whole fragment,
964 * significantly increasing the space available for translation
965 * caching. This leads to large improvements in throughput when the
966 * TLB is under pressure.
967 *
968 * The L2 TLB distributes small and large fragments into two
969 * asymmetric partitions. The large fragment cache is significantly
970 * larger. Thus, we try to use large fragments wherever possible.
971 * Userspace can support this by aligning virtual base address and
972 * allocation size to the fragment size.
973 */
974
Christian König80366172016-10-04 13:39:43 +0200975 /* SI and newer are optimized for 64KB */
976 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
977 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978
Christian König92696dd2016-08-05 13:56:35 +0200979 uint64_t frag_start = ALIGN(start, frag_align);
980 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +0100981
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +0200983 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Christian König92696dd2016-08-05 13:56:35 +0200984 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985
Christian König49ac8a22016-10-13 15:09:08 +0200986 amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400987 return;
988 }
989
990 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +0200991 if (start != frag_start) {
Christian König49ac8a22016-10-13 15:09:08 +0200992 amdgpu_vm_update_ptes(params, start, frag_start,
Christian König92696dd2016-08-05 13:56:35 +0200993 dst, flags);
994 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 }
996
997 /* handle the area in the middle */
Christian König49ac8a22016-10-13 15:09:08 +0200998 amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
Christian König80366172016-10-04 13:39:43 +0200999 flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000
1001 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +02001002 if (frag_end != end) {
1003 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Christian König49ac8a22016-10-13 15:09:08 +02001004 amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005 }
1006}
1007
1008/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1010 *
1011 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001012 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001013 * @src: address where to copy page table entries from
1014 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001015 * @vm: requested vm
1016 * @start: start of mapped range
1017 * @last: last mapped entry
1018 * @flags: flags for the entries
1019 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 * @fence: optional resulting fence
1021 *
Christian Königa14faa62016-01-25 14:27:31 +01001022 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024 */
1025static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001026 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001027 uint64_t src,
1028 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001030 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001031 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001032 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033{
Christian König2d55e452016-02-08 17:37:38 +01001034 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001035 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001036 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001037 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001038 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001039 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001040 int r;
1041
Christian Königafef8b82016-08-12 13:29:18 +02001042 memset(&params, 0, sizeof(params));
1043 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001044 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001045 params.src = src;
1046
Christian König2d55e452016-02-08 17:37:38 +01001047 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001048
Christian Königa1e08d32016-01-26 11:40:46 +01001049 /* sync to everything on unmapping */
1050 if (!(flags & AMDGPU_PTE_VALID))
1051 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1052
Christian Königa14faa62016-01-25 14:27:31 +01001053 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001054
1055 /*
1056 * reserve space for one command every (1 << BLOCK_SIZE)
1057 * entries or 2k dwords (whatever is smaller)
1058 */
1059 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
1060
1061 /* padding, etc. */
1062 ndw = 64;
1063
Christian Königb0456f92016-08-11 14:06:54 +02001064 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001065 /* only copy commands needed */
1066 ndw += ncmds * 7;
1067
Christian Königafef8b82016-08-12 13:29:18 +02001068 params.func = amdgpu_vm_do_copy_ptes;
1069
Christian Königb0456f92016-08-11 14:06:54 +02001070 } else if (pages_addr) {
1071 /* copy commands needed */
1072 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073
Christian Königb0456f92016-08-11 14:06:54 +02001074 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001075 ndw += nptes * 2;
1076
Christian Königafef8b82016-08-12 13:29:18 +02001077 params.func = amdgpu_vm_do_copy_ptes;
1078
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001079 } else {
1080 /* set page commands needed */
1081 ndw += ncmds * 10;
1082
1083 /* two extra commands for begin/end of fragment */
1084 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001085
1086 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087 }
1088
Christian Königd71518b2016-02-01 12:20:25 +01001089 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1090 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001092
Christian König29efc4f2016-08-04 14:52:50 +02001093 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001094
Christian Königb0456f92016-08-11 14:06:54 +02001095 if (!src && pages_addr) {
1096 uint64_t *pte;
1097 unsigned i;
1098
1099 /* Put the PTEs at the end of the IB. */
1100 i = ndw - nptes * 2;
1101 pte= (uint64_t *)&(job->ibs->ptr[i]);
1102 params.src = job->ibs->gpu_addr + i * 4;
1103
1104 for (i = 0; i < nptes; ++i) {
1105 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1106 AMDGPU_GPU_PAGE_SIZE);
1107 pte[i] |= flags;
1108 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001109 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001110 }
1111
Christian König3cabaa52016-06-06 10:17:58 +02001112 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1113 if (r)
1114 goto error_free;
1115
Christian König67003a12016-10-12 14:46:26 +02001116 r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001117 owner);
1118 if (r)
1119 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001120
Christian König67003a12016-10-12 14:46:26 +02001121 r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001122 if (r)
1123 goto error_free;
1124
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001125 params.shadow = true;
Christian König49ac8a22016-10-13 15:09:08 +02001126 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001127 params.shadow = false;
Christian König49ac8a22016-10-13 15:09:08 +02001128 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129
Christian König29efc4f2016-08-04 14:52:50 +02001130 amdgpu_ring_pad_ib(ring, params.ib);
1131 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001132 r = amdgpu_job_submit(job, ring, &vm->entity,
1133 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001134 if (r)
1135 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136
Christian König67003a12016-10-12 14:46:26 +02001137 amdgpu_bo_fence(vm->root.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001138 dma_fence_put(*fence);
1139 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001141
1142error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001143 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001144 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001145}
1146
1147/**
Christian Königa14faa62016-01-25 14:27:31 +01001148 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1149 *
1150 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001151 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001152 * @gtt_flags: flags as they are used for GTT
1153 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001154 * @vm: requested vm
1155 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001156 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001157 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001158 * @fence: optional resulting fence
1159 *
1160 * Split the mapping into smaller chunks so that each update fits
1161 * into a SDMA IB.
1162 * Returns 0 for success, -EINVAL for failure.
1163 */
1164static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001165 struct dma_fence *exclusive,
Chunming Zhou6b777602016-09-21 16:19:19 +08001166 uint64_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +02001167 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001168 struct amdgpu_vm *vm,
1169 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001170 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001171 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001172 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001173{
Christian König63e0ba42016-08-16 17:38:37 +02001174 uint64_t pfn, src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +01001175 int r;
1176
1177 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1178 * but in case of something, we filter the flags in first place
1179 */
1180 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1181 flags &= ~AMDGPU_PTE_READABLE;
1182 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1183 flags &= ~AMDGPU_PTE_WRITEABLE;
1184
Alex Xie15b31c52017-03-03 16:47:11 -05001185 flags &= ~AMDGPU_PTE_EXECUTABLE;
1186 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1187
Alex Xieb0fd18b2017-03-03 16:49:39 -05001188 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1189 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1190
Christian Königa14faa62016-01-25 14:27:31 +01001191 trace_amdgpu_vm_bo_update(mapping);
1192
Christian König63e0ba42016-08-16 17:38:37 +02001193 pfn = mapping->offset >> PAGE_SHIFT;
1194 if (nodes) {
1195 while (pfn >= nodes->size) {
1196 pfn -= nodes->size;
1197 ++nodes;
1198 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001199 }
Christian Königa14faa62016-01-25 14:27:31 +01001200
Christian König63e0ba42016-08-16 17:38:37 +02001201 do {
1202 uint64_t max_entries;
1203 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001204
Christian König63e0ba42016-08-16 17:38:37 +02001205 if (nodes) {
1206 addr = nodes->start << PAGE_SHIFT;
1207 max_entries = (nodes->size - pfn) *
1208 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1209 } else {
1210 addr = 0;
1211 max_entries = S64_MAX;
1212 }
Christian Königa14faa62016-01-25 14:27:31 +01001213
Christian König63e0ba42016-08-16 17:38:37 +02001214 if (pages_addr) {
1215 if (flags == gtt_flags)
1216 src = adev->gart.table_addr +
1217 (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
1218 else
1219 max_entries = min(max_entries, 16ull * 1024ull);
1220 addr = 0;
1221 } else if (flags & AMDGPU_PTE_VALID) {
1222 addr += adev->vm_manager.vram_base_offset;
1223 }
1224 addr += pfn << PAGE_SHIFT;
1225
1226 last = min((uint64_t)mapping->it.last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001227 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1228 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001229 start, last, flags, addr,
1230 fence);
1231 if (r)
1232 return r;
1233
Christian König63e0ba42016-08-16 17:38:37 +02001234 pfn += last - start + 1;
1235 if (nodes && nodes->size == pfn) {
1236 pfn = 0;
1237 ++nodes;
1238 }
Christian Königa14faa62016-01-25 14:27:31 +01001239 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001240
1241 } while (unlikely(start != mapping->it.last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001242
1243 return 0;
1244}
1245
1246/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1248 *
1249 * @adev: amdgpu_device pointer
1250 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001251 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 *
1253 * Fill in the page table entries for @bo_va.
1254 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001255 */
1256int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1257 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001258 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259{
1260 struct amdgpu_vm *vm = bo_va->vm;
1261 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001262 dma_addr_t *pages_addr = NULL;
Chunming Zhou6b777602016-09-21 16:19:19 +08001263 uint64_t gtt_flags, flags;
Christian König99e124f2016-08-16 14:43:17 +02001264 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001265 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001266 struct dma_fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001267 int r;
1268
Christian Königa5f6b5b2017-01-30 11:01:38 +01001269 if (clear || !bo_va->bo) {
Christian König99e124f2016-08-16 14:43:17 +02001270 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001271 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001272 exclusive = NULL;
1273 } else {
Christian König8358dce2016-03-30 10:50:25 +02001274 struct ttm_dma_tt *ttm;
1275
Christian König99e124f2016-08-16 14:43:17 +02001276 mem = &bo_va->bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001277 nodes = mem->mm_node;
1278 if (mem->mem_type == TTM_PL_TT) {
Christian König8358dce2016-03-30 10:50:25 +02001279 ttm = container_of(bo_va->bo->tbo.ttm, struct
1280 ttm_dma_tt, ttm);
1281 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001282 }
Christian König3cabaa52016-06-06 10:17:58 +02001283 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284 }
1285
Christian Königa5f6b5b2017-01-30 11:01:38 +01001286 if (bo_va->bo) {
1287 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1288 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1289 adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
1290 flags : 0;
1291 } else {
1292 flags = 0x0;
1293 gtt_flags = ~0x0;
1294 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295
Christian König7fc11952015-07-30 11:53:42 +02001296 spin_lock(&vm->status_lock);
1297 if (!list_empty(&bo_va->vm_status))
1298 list_splice_init(&bo_va->valids, &bo_va->invalids);
1299 spin_unlock(&vm->status_lock);
1300
1301 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001302 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1303 gtt_flags, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001304 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001305 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306 if (r)
1307 return r;
1308 }
1309
Christian Königd6c10f62015-09-28 12:00:23 +02001310 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1311 list_for_each_entry(mapping, &bo_va->valids, list)
1312 trace_amdgpu_vm_bo_mapping(mapping);
1313
1314 list_for_each_entry(mapping, &bo_va->invalids, list)
1315 trace_amdgpu_vm_bo_mapping(mapping);
1316 }
1317
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001319 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320 list_del_init(&bo_va->vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001321 if (clear)
Christian König7fc11952015-07-30 11:53:42 +02001322 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323 spin_unlock(&vm->status_lock);
1324
1325 return 0;
1326}
1327
1328/**
Christian König284710f2017-01-30 11:09:31 +01001329 * amdgpu_vm_update_prt_state - update the global PRT state
1330 */
1331static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1332{
1333 unsigned long flags;
1334 bool enable;
1335
1336 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001337 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001338 adev->gart.gart_funcs->set_prt(adev, enable);
1339 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1340}
1341
1342/**
Christian König4388fc22017-03-13 10:13:36 +01001343 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001344 */
1345static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1346{
Christian König4388fc22017-03-13 10:13:36 +01001347 if (!adev->gart.gart_funcs->set_prt)
1348 return;
1349
Christian König451bc8e2017-02-14 16:02:52 +01001350 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1351 amdgpu_vm_update_prt_state(adev);
1352}
1353
1354/**
Christian König0b15f2f2017-02-14 15:47:03 +01001355 * amdgpu_vm_prt_put - drop a PRT user
1356 */
1357static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1358{
Christian König451bc8e2017-02-14 16:02:52 +01001359 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001360 amdgpu_vm_update_prt_state(adev);
1361}
1362
1363/**
Christian König451bc8e2017-02-14 16:02:52 +01001364 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001365 */
1366static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1367{
1368 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1369
Christian König0b15f2f2017-02-14 15:47:03 +01001370 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001371 kfree(cb);
1372}
1373
1374/**
Christian König451bc8e2017-02-14 16:02:52 +01001375 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1376 */
1377static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1378 struct dma_fence *fence)
1379{
Christian König4388fc22017-03-13 10:13:36 +01001380 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001381
Christian König4388fc22017-03-13 10:13:36 +01001382 if (!adev->gart.gart_funcs->set_prt)
1383 return;
1384
1385 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001386 if (!cb) {
1387 /* Last resort when we are OOM */
1388 if (fence)
1389 dma_fence_wait(fence, false);
1390
1391 amdgpu_vm_prt_put(cb->adev);
1392 } else {
1393 cb->adev = adev;
1394 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1395 amdgpu_vm_prt_cb))
1396 amdgpu_vm_prt_cb(fence, &cb->cb);
1397 }
1398}
1399
1400/**
Christian König284710f2017-01-30 11:09:31 +01001401 * amdgpu_vm_free_mapping - free a mapping
1402 *
1403 * @adev: amdgpu_device pointer
1404 * @vm: requested vm
1405 * @mapping: mapping to be freed
1406 * @fence: fence of the unmap operation
1407 *
1408 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1409 */
1410static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1411 struct amdgpu_vm *vm,
1412 struct amdgpu_bo_va_mapping *mapping,
1413 struct dma_fence *fence)
1414{
Christian König451bc8e2017-02-14 16:02:52 +01001415 if (mapping->flags & AMDGPU_PTE_PRT)
1416 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001417 kfree(mapping);
1418}
1419
1420/**
Christian König451bc8e2017-02-14 16:02:52 +01001421 * amdgpu_vm_prt_fini - finish all prt mappings
1422 *
1423 * @adev: amdgpu_device pointer
1424 * @vm: requested vm
1425 *
1426 * Register a cleanup callback to disable PRT support after VM dies.
1427 */
1428static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1429{
Christian König67003a12016-10-12 14:46:26 +02001430 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001431 struct dma_fence *excl, **shared;
1432 unsigned i, shared_count;
1433 int r;
1434
1435 r = reservation_object_get_fences_rcu(resv, &excl,
1436 &shared_count, &shared);
1437 if (r) {
1438 /* Not enough memory to grab the fence list, as last resort
1439 * block for all the fences to complete.
1440 */
1441 reservation_object_wait_timeout_rcu(resv, true, false,
1442 MAX_SCHEDULE_TIMEOUT);
1443 return;
1444 }
1445
1446 /* Add a callback for each fence in the reservation object */
1447 amdgpu_vm_prt_get(adev);
1448 amdgpu_vm_add_prt_cb(adev, excl);
1449
1450 for (i = 0; i < shared_count; ++i) {
1451 amdgpu_vm_prt_get(adev);
1452 amdgpu_vm_add_prt_cb(adev, shared[i]);
1453 }
1454
1455 kfree(shared);
1456}
1457
1458/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001459 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1460 *
1461 * @adev: amdgpu_device pointer
1462 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001463 * @fence: optional resulting fence (unchanged if no work needed to be done
1464 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001465 *
1466 * Make sure all freed BOs are cleared in the PT.
1467 * Returns 0 for success.
1468 *
1469 * PTs have to be reserved and mutex must be locked!
1470 */
1471int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001472 struct amdgpu_vm *vm,
1473 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001474{
1475 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001476 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001477 int r;
1478
1479 while (!list_empty(&vm->freed)) {
1480 mapping = list_first_entry(&vm->freed,
1481 struct amdgpu_bo_va_mapping, list);
1482 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001483
Christian König3cabaa52016-06-06 10:17:58 +02001484 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001485 0, 0, &f);
1486 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001487 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001488 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489 return r;
Christian König284710f2017-01-30 11:09:31 +01001490 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001492
1493 if (fence && f) {
1494 dma_fence_put(*fence);
1495 *fence = f;
1496 } else {
1497 dma_fence_put(f);
1498 }
1499
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001500 return 0;
1501
1502}
1503
1504/**
1505 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1506 *
1507 * @adev: amdgpu_device pointer
1508 * @vm: requested vm
1509 *
1510 * Make sure all invalidated BOs are cleared in the PT.
1511 * Returns 0 for success.
1512 *
1513 * PTs have to be reserved and mutex must be locked!
1514 */
1515int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001516 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517{
monk.liucfe2c972015-05-26 15:01:54 +08001518 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001519 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001520
1521 spin_lock(&vm->status_lock);
1522 while (!list_empty(&vm->invalidated)) {
1523 bo_va = list_first_entry(&vm->invalidated,
1524 struct amdgpu_bo_va, vm_status);
1525 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001526
Christian König99e124f2016-08-16 14:43:17 +02001527 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001528 if (r)
1529 return r;
1530
1531 spin_lock(&vm->status_lock);
1532 }
1533 spin_unlock(&vm->status_lock);
1534
monk.liucfe2c972015-05-26 15:01:54 +08001535 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001536 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001537
1538 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539}
1540
1541/**
1542 * amdgpu_vm_bo_add - add a bo to a specific vm
1543 *
1544 * @adev: amdgpu_device pointer
1545 * @vm: requested vm
1546 * @bo: amdgpu buffer object
1547 *
Christian König8843dbb2016-01-26 12:17:11 +01001548 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001549 * Add @bo to the list of bos associated with the vm
1550 * Returns newly added bo_va or NULL for failure
1551 *
1552 * Object has to be reserved!
1553 */
1554struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1555 struct amdgpu_vm *vm,
1556 struct amdgpu_bo *bo)
1557{
1558 struct amdgpu_bo_va *bo_va;
1559
1560 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1561 if (bo_va == NULL) {
1562 return NULL;
1563 }
1564 bo_va->vm = vm;
1565 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 bo_va->ref_count = 1;
1567 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001568 INIT_LIST_HEAD(&bo_va->valids);
1569 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001570 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001571
Christian Königa5f6b5b2017-01-30 11:01:38 +01001572 if (bo)
1573 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001574
1575 return bo_va;
1576}
1577
1578/**
1579 * amdgpu_vm_bo_map - map bo inside a vm
1580 *
1581 * @adev: amdgpu_device pointer
1582 * @bo_va: bo_va to store the address
1583 * @saddr: where to map the BO
1584 * @offset: requested offset in the BO
1585 * @flags: attributes of pages (read/write/valid/etc.)
1586 *
1587 * Add a mapping of the BO at the specefied addr into the VM.
1588 * Returns 0 for success, error for failure.
1589 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001590 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591 */
1592int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1593 struct amdgpu_bo_va *bo_va,
1594 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001595 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596{
1597 struct amdgpu_bo_va_mapping *mapping;
1598 struct amdgpu_vm *vm = bo_va->vm;
1599 struct interval_tree_node *it;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001600 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601
Christian König0be52de2015-05-18 14:37:27 +02001602 /* validate the parameters */
1603 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001604 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001605 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001606
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001608 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001609 if (saddr >= eaddr ||
1610 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001613 saddr /= AMDGPU_GPU_PAGE_SIZE;
1614 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1615
Felix Kuehling005ae952015-11-23 17:43:48 -05001616 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 if (it) {
1618 struct amdgpu_bo_va_mapping *tmp;
1619 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1620 /* bo and tmp overlap, invalid addr */
1621 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1622 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1623 tmp->it.start, tmp->it.last + 1);
Christian König663e4572017-03-13 10:13:37 +01001624 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001625 }
1626
1627 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001628 if (!mapping)
1629 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001630
1631 INIT_LIST_HEAD(&mapping->list);
1632 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001633 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 mapping->offset = offset;
1635 mapping->flags = flags;
1636
Christian König7fc11952015-07-30 11:53:42 +02001637 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638 interval_tree_insert(&mapping->it, &vm->va);
1639
Christian König4388fc22017-03-13 10:13:36 +01001640 if (flags & AMDGPU_PTE_PRT)
1641 amdgpu_vm_prt_get(adev);
1642
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001644}
1645
1646/**
Christian König80f95c52017-03-13 10:13:39 +01001647 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1648 *
1649 * @adev: amdgpu_device pointer
1650 * @bo_va: bo_va to store the address
1651 * @saddr: where to map the BO
1652 * @offset: requested offset in the BO
1653 * @flags: attributes of pages (read/write/valid/etc.)
1654 *
1655 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1656 * mappings as we do so.
1657 * Returns 0 for success, error for failure.
1658 *
1659 * Object has to be reserved and unreserved outside!
1660 */
1661int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1662 struct amdgpu_bo_va *bo_va,
1663 uint64_t saddr, uint64_t offset,
1664 uint64_t size, uint64_t flags)
1665{
1666 struct amdgpu_bo_va_mapping *mapping;
1667 struct amdgpu_vm *vm = bo_va->vm;
1668 uint64_t eaddr;
1669 int r;
1670
1671 /* validate the parameters */
1672 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1673 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1674 return -EINVAL;
1675
1676 /* make sure object fit at this offset */
1677 eaddr = saddr + size - 1;
1678 if (saddr >= eaddr ||
1679 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
1680 return -EINVAL;
1681
1682 /* Allocate all the needed memory */
1683 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1684 if (!mapping)
1685 return -ENOMEM;
1686
1687 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
1688 if (r) {
1689 kfree(mapping);
1690 return r;
1691 }
1692
1693 saddr /= AMDGPU_GPU_PAGE_SIZE;
1694 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1695
1696 mapping->it.start = saddr;
1697 mapping->it.last = eaddr;
1698 mapping->offset = offset;
1699 mapping->flags = flags;
1700
1701 list_add(&mapping->list, &bo_va->invalids);
1702 interval_tree_insert(&mapping->it, &vm->va);
1703
1704 if (flags & AMDGPU_PTE_PRT)
1705 amdgpu_vm_prt_get(adev);
1706
1707 return 0;
1708}
1709
1710/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001711 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1712 *
1713 * @adev: amdgpu_device pointer
1714 * @bo_va: bo_va to remove the address from
1715 * @saddr: where to the BO is mapped
1716 *
1717 * Remove a mapping of the BO at the specefied addr from the VM.
1718 * Returns 0 for success, error for failure.
1719 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001720 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001721 */
1722int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1723 struct amdgpu_bo_va *bo_va,
1724 uint64_t saddr)
1725{
1726 struct amdgpu_bo_va_mapping *mapping;
1727 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001728 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001729
Christian König6c7fc502015-06-05 20:56:17 +02001730 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001731
Christian König7fc11952015-07-30 11:53:42 +02001732 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001733 if (mapping->it.start == saddr)
1734 break;
1735 }
1736
Christian König7fc11952015-07-30 11:53:42 +02001737 if (&mapping->list == &bo_va->valids) {
1738 valid = false;
1739
1740 list_for_each_entry(mapping, &bo_va->invalids, list) {
1741 if (mapping->it.start == saddr)
1742 break;
1743 }
1744
Christian König32b41ac2016-03-08 18:03:27 +01001745 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001746 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001747 }
Christian König32b41ac2016-03-08 18:03:27 +01001748
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001749 list_del(&mapping->list);
1750 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001751 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752
Christian Könige17841b2016-03-08 17:52:01 +01001753 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001754 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001755 else
Christian König284710f2017-01-30 11:09:31 +01001756 amdgpu_vm_free_mapping(adev, vm, mapping,
1757 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758
1759 return 0;
1760}
1761
1762/**
Christian Königdc54d3d2017-03-13 10:13:38 +01001763 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1764 *
1765 * @adev: amdgpu_device pointer
1766 * @vm: VM structure to use
1767 * @saddr: start of the range
1768 * @size: size of the range
1769 *
1770 * Remove all mappings in a range, split them as appropriate.
1771 * Returns 0 for success, error for failure.
1772 */
1773int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1774 struct amdgpu_vm *vm,
1775 uint64_t saddr, uint64_t size)
1776{
1777 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1778 struct interval_tree_node *it;
1779 LIST_HEAD(removed);
1780 uint64_t eaddr;
1781
1782 eaddr = saddr + size - 1;
1783 saddr /= AMDGPU_GPU_PAGE_SIZE;
1784 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1785
1786 /* Allocate all the needed memory */
1787 before = kzalloc(sizeof(*before), GFP_KERNEL);
1788 if (!before)
1789 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08001790 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001791
1792 after = kzalloc(sizeof(*after), GFP_KERNEL);
1793 if (!after) {
1794 kfree(before);
1795 return -ENOMEM;
1796 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08001797 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001798
1799 /* Now gather all removed mappings */
1800 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
1801 while (it) {
1802 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1803 it = interval_tree_iter_next(it, saddr, eaddr);
1804
1805 /* Remember mapping split at the start */
1806 if (tmp->it.start < saddr) {
Junwei Zhang27f6d612017-03-16 16:09:24 +08001807 before->it.start = tmp->it.start;
Christian Königdc54d3d2017-03-13 10:13:38 +01001808 before->it.last = saddr - 1;
1809 before->offset = tmp->offset;
1810 before->flags = tmp->flags;
1811 list_add(&before->list, &tmp->list);
1812 }
1813
1814 /* Remember mapping split at the end */
1815 if (tmp->it.last > eaddr) {
1816 after->it.start = eaddr + 1;
1817 after->it.last = tmp->it.last;
1818 after->offset = tmp->offset;
1819 after->offset += after->it.start - tmp->it.start;
1820 after->flags = tmp->flags;
1821 list_add(&after->list, &tmp->list);
1822 }
1823
1824 list_del(&tmp->list);
1825 list_add(&tmp->list, &removed);
1826 }
1827
1828 /* And free them up */
1829 list_for_each_entry_safe(tmp, next, &removed, list) {
1830 interval_tree_remove(&tmp->it, &vm->va);
1831 list_del(&tmp->list);
1832
1833 if (tmp->it.start < saddr)
1834 tmp->it.start = saddr;
1835 if (tmp->it.last > eaddr)
1836 tmp->it.last = eaddr;
1837
1838 list_add(&tmp->list, &vm->freed);
1839 trace_amdgpu_vm_bo_unmap(NULL, tmp);
1840 }
1841
Junwei Zhang27f6d612017-03-16 16:09:24 +08001842 /* Insert partial mapping before the range */
1843 if (!list_empty(&before->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001844 interval_tree_insert(&before->it, &vm->va);
1845 if (before->flags & AMDGPU_PTE_PRT)
1846 amdgpu_vm_prt_get(adev);
1847 } else {
1848 kfree(before);
1849 }
1850
1851 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08001852 if (!list_empty(&after->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001853 interval_tree_insert(&after->it, &vm->va);
1854 if (after->flags & AMDGPU_PTE_PRT)
1855 amdgpu_vm_prt_get(adev);
1856 } else {
1857 kfree(after);
1858 }
1859
1860 return 0;
1861}
1862
1863/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1865 *
1866 * @adev: amdgpu_device pointer
1867 * @bo_va: requested bo_va
1868 *
Christian König8843dbb2016-01-26 12:17:11 +01001869 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001870 *
1871 * Object have to be reserved!
1872 */
1873void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1874 struct amdgpu_bo_va *bo_va)
1875{
1876 struct amdgpu_bo_va_mapping *mapping, *next;
1877 struct amdgpu_vm *vm = bo_va->vm;
1878
1879 list_del(&bo_va->bo_list);
1880
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001881 spin_lock(&vm->status_lock);
1882 list_del(&bo_va->vm_status);
1883 spin_unlock(&vm->status_lock);
1884
Christian König7fc11952015-07-30 11:53:42 +02001885 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886 list_del(&mapping->list);
1887 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001888 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001889 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001890 }
Christian König7fc11952015-07-30 11:53:42 +02001891 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1892 list_del(&mapping->list);
1893 interval_tree_remove(&mapping->it, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01001894 amdgpu_vm_free_mapping(adev, vm, mapping,
1895 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02001896 }
Christian König32b41ac2016-03-08 18:03:27 +01001897
Chris Wilsonf54d1862016-10-25 13:00:45 +01001898 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001899 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001900}
1901
1902/**
1903 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1904 *
1905 * @adev: amdgpu_device pointer
1906 * @vm: requested vm
1907 * @bo: amdgpu buffer object
1908 *
Christian König8843dbb2016-01-26 12:17:11 +01001909 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001910 */
1911void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1912 struct amdgpu_bo *bo)
1913{
1914 struct amdgpu_bo_va *bo_va;
1915
1916 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001917 spin_lock(&bo_va->vm->status_lock);
1918 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001919 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001920 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001921 }
1922}
1923
1924/**
1925 * amdgpu_vm_init - initialize a vm instance
1926 *
1927 * @adev: amdgpu_device pointer
1928 * @vm: requested vm
1929 *
Christian König8843dbb2016-01-26 12:17:11 +01001930 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 */
1932int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1933{
1934 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1935 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001936 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001937 unsigned ring_instance;
1938 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001939 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940 int i, r;
1941
Christian Königbcb1ba32016-03-08 15:40:11 +01001942 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1943 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001944 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001945 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001946 spin_lock_init(&vm->status_lock);
1947 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001948 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001949 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001950
Christian König72a7ec52016-10-19 11:03:57 +02001951 pd_size = amdgpu_vm_bo_size(adev, 0);
1952 pd_entries = amdgpu_vm_num_entries(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001953
1954 /* allocate page table array */
Christian König67003a12016-10-12 14:46:26 +02001955 vm->root.entries = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
1956 if (vm->root.entries == NULL) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001957 DRM_ERROR("Cannot allocate memory for page table array\n");
1958 return -ENOMEM;
1959 }
1960
Christian König2bd9ccf2016-02-01 12:53:58 +01001961 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001962
1963 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1964 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1965 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001966 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1967 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1968 rq, amdgpu_sched_jobs);
1969 if (r)
Chunming Zhou64827ad2016-07-28 17:20:32 +08001970 goto err;
Christian König2bd9ccf2016-02-01 12:53:58 +01001971
Christian Königa24960f2016-10-12 13:20:52 +02001972 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001973
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001974 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001975 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08001976 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
Christian König03f48dd2016-08-15 17:00:22 +02001977 AMDGPU_GEM_CREATE_SHADOW |
Christian König617859e2016-11-17 15:40:02 +01001978 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1979 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Christian König67003a12016-10-12 14:46:26 +02001980 NULL, NULL, &vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001981 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001982 goto error_free_sched_entity;
1983
Christian König67003a12016-10-12 14:46:26 +02001984 r = amdgpu_bo_reserve(vm->root.bo, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001985 if (r)
Christian König67003a12016-10-12 14:46:26 +02001986 goto error_free_root;
Christian König2bd9ccf2016-02-01 12:53:58 +01001987
Christian König5a712a82016-06-21 16:28:15 +02001988 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König67003a12016-10-12 14:46:26 +02001989 amdgpu_bo_unreserve(vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001990
1991 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001992
Christian König67003a12016-10-12 14:46:26 +02001993error_free_root:
1994 amdgpu_bo_unref(&vm->root.bo->shadow);
1995 amdgpu_bo_unref(&vm->root.bo);
1996 vm->root.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01001997
1998error_free_sched_entity:
1999 amd_sched_entity_fini(&ring->sched, &vm->entity);
2000
Chunming Zhou64827ad2016-07-28 17:20:32 +08002001err:
Christian König67003a12016-10-12 14:46:26 +02002002 drm_free_large(vm->root.entries);
Chunming Zhou64827ad2016-07-28 17:20:32 +08002003
Christian König2bd9ccf2016-02-01 12:53:58 +01002004 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005}
2006
2007/**
2008 * amdgpu_vm_fini - tear down a vm instance
2009 *
2010 * @adev: amdgpu_device pointer
2011 * @vm: requested vm
2012 *
Christian König8843dbb2016-01-26 12:17:11 +01002013 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002014 * Unbind the VM and remove all bos from the vm bo list
2015 */
2016void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2017{
2018 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002019 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002020 int i;
2021
Christian König2d55e452016-02-08 17:37:38 +01002022 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002023
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002024 if (!RB_EMPTY_ROOT(&vm->va)) {
2025 dev_err(adev->dev, "still active bo inside vm\n");
2026 }
2027 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
2028 list_del(&mapping->list);
2029 interval_tree_remove(&mapping->it, &vm->va);
2030 kfree(mapping);
2031 }
2032 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002033 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002034 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002035 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002036 }
Christian König284710f2017-01-30 11:09:31 +01002037
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002039 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040 }
2041
Christian König72a7ec52016-10-19 11:03:57 +02002042 for (i = 0; i < amdgpu_vm_num_entries(adev, 0); i++) {
Christian König67003a12016-10-12 14:46:26 +02002043 struct amdgpu_bo *pt = vm->root.entries[i].bo;
Christian König2698f622016-09-16 13:06:09 +02002044
2045 if (!pt)
2046 continue;
2047
2048 amdgpu_bo_unref(&pt->shadow);
2049 amdgpu_bo_unref(&pt);
Chunming Zhou1baa4392016-08-04 13:59:32 +08002050 }
Christian König67003a12016-10-12 14:46:26 +02002051 drm_free_large(vm->root.entries);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002052
Christian König67003a12016-10-12 14:46:26 +02002053 amdgpu_bo_unref(&vm->root.bo->shadow);
2054 amdgpu_bo_unref(&vm->root.bo);
Christian Königa24960f2016-10-12 13:20:52 +02002055 dma_fence_put(vm->last_dir_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002056}
Christian Königea89f8c2015-11-15 20:52:06 +01002057
2058/**
Christian Königa9a78b32016-01-21 10:19:11 +01002059 * amdgpu_vm_manager_init - init the VM manager
2060 *
2061 * @adev: amdgpu_device pointer
2062 *
2063 * Initialize the VM manager structures
2064 */
2065void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2066{
2067 unsigned i;
2068
2069 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
2070
2071 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01002072 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
2073 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01002074 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01002075 list_add_tail(&adev->vm_manager.ids[i].list,
2076 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01002077 }
Christian König2d55e452016-02-08 17:37:38 +01002078
Chris Wilsonf54d1862016-10-25 13:00:45 +01002079 adev->vm_manager.fence_context =
2080 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002081 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2082 adev->vm_manager.seqno[i] = 0;
2083
Christian König2d55e452016-02-08 17:37:38 +01002084 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002085 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002086 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002087 atomic_set(&adev->vm_manager.num_prt_users, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01002088}
2089
2090/**
Christian Königea89f8c2015-11-15 20:52:06 +01002091 * amdgpu_vm_manager_fini - cleanup VM manager
2092 *
2093 * @adev: amdgpu_device pointer
2094 *
2095 * Cleanup the VM manager and free resources.
2096 */
2097void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2098{
2099 unsigned i;
2100
Christian Königbcb1ba32016-03-08 15:40:11 +01002101 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
2102 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
2103
Chris Wilsonf54d1862016-10-25 13:00:45 +01002104 dma_fence_put(adev->vm_manager.ids[i].first);
Christian König832a9022016-02-15 12:33:02 +01002105 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Chris Wilsonf54d1862016-10-25 13:00:45 +01002106 dma_fence_put(id->flushed_updates);
Dave Airlie7b624ad2016-11-07 09:37:09 +10002107 dma_fence_put(id->last_flush);
Christian Königbcb1ba32016-03-08 15:40:11 +01002108 }
Christian Königea89f8c2015-11-15 20:52:06 +01002109}