blob: e7e75d2fe8a4bd669e0a17f0f917ea067219dba9 [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/**
81 * amdgpu_vm_num_pde - return the number of page directory entries
82 *
83 * @adev: amdgpu_device pointer
84 *
Christian König8843dbb2016-01-26 12:17:11 +010085 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 */
87static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
88{
89 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
90}
91
92/**
93 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
94 *
95 * @adev: amdgpu_device pointer
96 *
Christian König8843dbb2016-01-26 12:17:11 +010097 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 */
99static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
100{
101 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
102}
103
104/**
Christian König56467eb2015-12-11 15:16:32 +0100105 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 *
107 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100108 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100109 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110 *
111 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100112 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 */
Christian König56467eb2015-12-11 15:16:32 +0100114void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
115 struct list_head *validated,
116 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117{
Christian König67003a12016-10-12 14:46:26 +0200118 entry->robj = vm->root.bo;
Christian König56467eb2015-12-11 15:16:32 +0100119 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200120 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100121 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100122 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100123 list_add(&entry->tv.head, validated);
124}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125
Christian König56467eb2015-12-11 15:16:32 +0100126/**
Christian Königf7da30d2016-09-28 12:03:04 +0200127 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100128 *
Christian König5a712a82016-06-21 16:28:15 +0200129 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100130 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200131 * @validate: callback to do the validation
132 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 *
Christian Königf7da30d2016-09-28 12:03:04 +0200134 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 */
Christian Königf7da30d2016-09-28 12:03:04 +0200136int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
137 int (*validate)(void *p, struct amdgpu_bo *bo),
138 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400139{
Christian König5a712a82016-06-21 16:28:15 +0200140 uint64_t num_evictions;
Christian Königee1782c2015-12-11 21:01:23 +0100141 unsigned i;
Christian Königf7da30d2016-09-28 12:03:04 +0200142 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143
Christian König5a712a82016-06-21 16:28:15 +0200144 /* We only need to validate the page tables
145 * if they aren't already valid.
146 */
147 num_evictions = atomic64_read(&adev->num_evictions);
148 if (num_evictions == vm->last_eviction_counter)
Christian Königf7da30d2016-09-28 12:03:04 +0200149 return 0;
Christian König5a712a82016-06-21 16:28:15 +0200150
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151 /* add the vm page table to the list */
Christian König67003a12016-10-12 14:46:26 +0200152 for (i = 0; i <= vm->root.last_entry_used; ++i) {
153 struct amdgpu_bo *bo = vm->root.entries[i].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154
Christian König914b4dc2016-09-28 12:27:37 +0200155 if (!bo)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 continue;
157
Christian König914b4dc2016-09-28 12:27:37 +0200158 r = validate(param, bo);
Christian Königf7da30d2016-09-28 12:03:04 +0200159 if (r)
160 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161 }
Christian Königeceb8a12016-01-11 15:35:21 +0100162
Christian Königf7da30d2016-09-28 12:03:04 +0200163 return 0;
Christian Königeceb8a12016-01-11 15:35:21 +0100164}
165
166/**
167 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
168 *
169 * @adev: amdgpu device instance
170 * @vm: vm providing the BOs
171 *
172 * Move the PT BOs to the tail of the LRU.
173 */
174void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
175 struct amdgpu_vm *vm)
176{
177 struct ttm_bo_global *glob = adev->mman.bdev.glob;
178 unsigned i;
179
180 spin_lock(&glob->lru_lock);
Christian König67003a12016-10-12 14:46:26 +0200181 for (i = 0; i <= vm->root.last_entry_used; ++i) {
182 struct amdgpu_bo *bo = vm->root.entries[i].bo;
Christian Königeceb8a12016-01-11 15:35:21 +0100183
Christian König914b4dc2016-09-28 12:27:37 +0200184 if (!bo)
Christian Königeceb8a12016-01-11 15:35:21 +0100185 continue;
186
Christian König914b4dc2016-09-28 12:27:37 +0200187 ttm_bo_move_to_lru_tail(&bo->tbo);
Christian Königeceb8a12016-01-11 15:35:21 +0100188 }
189 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190}
191
Christian König663e4572017-03-13 10:13:37 +0100192/**
193 * amdgpu_vm_alloc_pts - Allocate page tables.
194 *
195 * @adev: amdgpu_device pointer
196 * @vm: VM to allocate page tables for
197 * @saddr: Start address which needs to be allocated
198 * @size: Size from start address we need.
199 *
200 * Make sure the page tables are allocated.
201 */
202int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
203 struct amdgpu_vm *vm,
204 uint64_t saddr, uint64_t size)
205{
206 unsigned last_pfn, pt_idx;
207 uint64_t eaddr;
208 int r;
209
210 /* validate the parameters */
211 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
212 return -EINVAL;
213
214 eaddr = saddr + size - 1;
215 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
216 if (last_pfn >= adev->vm_manager.max_pfn) {
217 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
218 last_pfn, adev->vm_manager.max_pfn);
219 return -EINVAL;
220 }
221
222 saddr /= AMDGPU_GPU_PAGE_SIZE;
223 eaddr /= AMDGPU_GPU_PAGE_SIZE;
224
225 saddr >>= amdgpu_vm_block_size;
226 eaddr >>= amdgpu_vm_block_size;
227
228 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
229
Christian König67003a12016-10-12 14:46:26 +0200230 if (eaddr > vm->root.last_entry_used)
231 vm->root.last_entry_used = eaddr;
Christian König663e4572017-03-13 10:13:37 +0100232
233 /* walk over the address space and allocate the page tables */
234 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian König67003a12016-10-12 14:46:26 +0200235 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König663e4572017-03-13 10:13:37 +0100236 struct amdgpu_bo *pt;
237
Christian König67003a12016-10-12 14:46:26 +0200238 if (vm->root.entries[pt_idx].bo)
Christian König663e4572017-03-13 10:13:37 +0100239 continue;
240
241 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
242 AMDGPU_GPU_PAGE_SIZE, true,
243 AMDGPU_GEM_DOMAIN_VRAM,
244 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
245 AMDGPU_GEM_CREATE_SHADOW |
246 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
247 AMDGPU_GEM_CREATE_VRAM_CLEARED,
248 NULL, resv, &pt);
249 if (r)
250 return r;
251
252 /* Keep a reference to the page table to avoid freeing
253 * them up in the wrong order.
254 */
Christian König67003a12016-10-12 14:46:26 +0200255 pt->parent = amdgpu_bo_ref(vm->root.bo);
Christian König663e4572017-03-13 10:13:37 +0100256
Christian König67003a12016-10-12 14:46:26 +0200257 vm->root.entries[pt_idx].bo = pt;
258 vm->root.entries[pt_idx].addr = 0;
Christian König663e4572017-03-13 10:13:37 +0100259 }
260
261 return 0;
262}
263
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800264static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
265 struct amdgpu_vm_id *id)
266{
267 return id->current_gpu_reset_count !=
268 atomic_read(&adev->gpu_reset_counter) ? true : false;
269}
270
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400271/**
272 * amdgpu_vm_grab_id - allocate the next free VMID
273 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400274 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200275 * @ring: ring we want to submit job to
276 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100277 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278 *
Christian König7f8a5292015-07-20 16:09:40 +0200279 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400280 */
Christian König7f8a5292015-07-20 16:09:40 +0200281int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100282 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800283 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285 struct amdgpu_device *adev = ring->adev;
Christian König090b7672016-07-08 10:21:02 +0200286 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100287 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200288 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100289 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200290 unsigned i;
291 int r = 0;
292
293 fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
294 GFP_KERNEL);
295 if (!fences)
296 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297
Christian König94dd0a42016-01-18 17:01:42 +0100298 mutex_lock(&adev->vm_manager.lock);
299
Christian König36fd7c52016-05-23 15:30:08 +0200300 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200301 i = 0;
Christian König8d76001e2016-05-23 16:00:32 +0200302 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200303 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
304 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200305 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200306 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200307 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100308
Christian König1fbb2e92016-06-01 10:47:36 +0200309 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König8d76001e2016-05-23 16:00:32 +0200310 if (&idle->list == &adev->vm_manager.ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200311 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
312 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100313 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200314 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200315
Christian König1fbb2e92016-06-01 10:47:36 +0200316 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100317 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200318
Chris Wilsonf54d1862016-10-25 13:00:45 +0100319 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200320 seqno, true);
321 if (!array) {
322 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100323 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200324 kfree(fences);
325 r = -ENOMEM;
326 goto error;
327 }
Christian König8d76001e2016-05-23 16:00:32 +0200328
Christian König8d76001e2016-05-23 16:00:32 +0200329
Christian König1fbb2e92016-06-01 10:47:36 +0200330 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100331 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200332 if (r)
333 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200334
Christian König1fbb2e92016-06-01 10:47:36 +0200335 mutex_unlock(&adev->vm_manager.lock);
336 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200337
Christian König1fbb2e92016-06-01 10:47:36 +0200338 }
339 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200340
Chunming Zhoufd53be32016-07-01 17:59:01 +0800341 job->vm_needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200342 /* Check if we can use a VMID already assigned to this VM */
343 i = ring->idx;
344 do {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100345 struct dma_fence *flushed;
Christian König8d76001e2016-05-23 16:00:32 +0200346
Christian König1fbb2e92016-06-01 10:47:36 +0200347 id = vm->ids[i++];
348 if (i == AMDGPU_MAX_RINGS)
349 i = 0;
350
351 /* Check all the prerequisites to using this VMID */
352 if (!id)
353 continue;
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800354 if (amdgpu_vm_is_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800355 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200356
357 if (atomic64_read(&id->owner) != vm->client_id)
358 continue;
359
Chunming Zhoufd53be32016-07-01 17:59:01 +0800360 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200361 continue;
362
Christian König090b7672016-07-08 10:21:02 +0200363 if (!id->last_flush)
364 continue;
365
366 if (id->last_flush->context != fence_context &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100367 !dma_fence_is_signaled(id->last_flush))
Christian König1fbb2e92016-06-01 10:47:36 +0200368 continue;
369
370 flushed = id->flushed_updates;
371 if (updates &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100372 (!flushed || dma_fence_is_later(updates, flushed)))
Christian König1fbb2e92016-06-01 10:47:36 +0200373 continue;
374
Christian König3dab83b2016-06-01 13:31:17 +0200375 /* Good we can use this VMID. Remember this submission as
376 * user of the VMID.
377 */
Christian König1fbb2e92016-06-01 10:47:36 +0200378 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
379 if (r)
380 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200381
Chunming Zhou6adb0512016-06-27 17:06:01 +0800382 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König1fbb2e92016-06-01 10:47:36 +0200383 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
384 vm->ids[ring->idx] = id;
Christian König8d76001e2016-05-23 16:00:32 +0200385
Chunming Zhoufd53be32016-07-01 17:59:01 +0800386 job->vm_id = id - adev->vm_manager.ids;
387 job->vm_needs_flush = false;
Christian König0c0fdf12016-07-08 10:48:24 +0200388 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
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 } while (i != ring->idx);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800394
Christian König1fbb2e92016-06-01 10:47:36 +0200395 /* Still no ID to use? Then use the idle one found earlier */
396 id = idle;
397
398 /* Remember this submission as user of the VMID */
399 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100400 if (r)
401 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100402
Chris Wilsonf54d1862016-10-25 13:00:45 +0100403 dma_fence_put(id->first);
404 id->first = dma_fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100405
Chris Wilsonf54d1862016-10-25 13:00:45 +0100406 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100407 id->last_flush = NULL;
408
Chris Wilsonf54d1862016-10-25 13:00:45 +0100409 dma_fence_put(id->flushed_updates);
410 id->flushed_updates = dma_fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100411
Chunming Zhoufd53be32016-07-01 17:59:01 +0800412 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhoub46b8a82016-06-27 17:04:23 +0800413 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König832a9022016-02-15 12:33:02 +0100414 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König0ea54b92016-05-04 10:20:01 +0200415 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100416 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417
Chunming Zhoufd53be32016-07-01 17:59:01 +0800418 job->vm_id = id - adev->vm_manager.ids;
Christian König0c0fdf12016-07-08 10:48:24 +0200419 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König832a9022016-02-15 12:33:02 +0100420
421error:
Christian König94dd0a42016-01-18 17:01:42 +0100422 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100423 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424}
425
Alex Deucher93dcc372016-06-17 17:05:15 -0400426static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
427{
428 struct amdgpu_device *adev = ring->adev;
Alex Deuchera1255102016-10-13 17:41:13 -0400429 const struct amdgpu_ip_block *ip_block;
Alex Deucher93dcc372016-06-17 17:05:15 -0400430
Christian König21cd9422016-10-05 15:36:39 +0200431 if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
Alex Deucher93dcc372016-06-17 17:05:15 -0400432 /* only compute rings */
433 return false;
434
435 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
436 if (!ip_block)
437 return false;
438
Alex Deuchera1255102016-10-13 17:41:13 -0400439 if (ip_block->version->major <= 7) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400440 /* gfx7 has no workaround */
441 return true;
Alex Deuchera1255102016-10-13 17:41:13 -0400442 } else if (ip_block->version->major == 8) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400443 if (adev->gfx.mec_fw_version >= 673)
444 /* gfx8 is fixed in MEC firmware 673 */
445 return false;
446 else
447 return true;
448 }
449 return false;
450}
451
Alex Xiee60f8db2017-03-09 11:36:26 -0500452static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr)
453{
454 u64 addr = mc_addr;
455
456 if (adev->mc.mc_funcs && adev->mc.mc_funcs->adjust_mc_addr)
457 addr = adev->mc.mc_funcs->adjust_mc_addr(adev, addr);
458
459 return addr;
460}
461
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462/**
463 * amdgpu_vm_flush - hardware flush the vm
464 *
465 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100466 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100467 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400468 *
Christian König4ff37a82016-02-26 16:18:26 +0100469 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470 */
Chunming Zhoufd53be32016-07-01 17:59:01 +0800471int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472{
Christian König971fe9a92016-03-01 15:09:25 +0100473 struct amdgpu_device *adev = ring->adev;
Chunming Zhoufd53be32016-07-01 17:59:01 +0800474 struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100475 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800476 id->gds_base != job->gds_base ||
477 id->gds_size != job->gds_size ||
478 id->gws_base != job->gws_base ||
479 id->gws_size != job->gws_size ||
480 id->oa_base != job->oa_base ||
481 id->oa_size != job->oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100482 int r;
Christian Königd564a062016-03-01 15:51:53 +0100483
484 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800485 job->vm_needs_flush || gds_switch_needed ||
Alex Deucher93dcc372016-06-17 17:05:15 -0400486 amdgpu_vm_ring_has_compute_vm_bug(ring)))
Christian Königd564a062016-03-01 15:51:53 +0100487 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100488
Chunming Zhouaa1c8902016-06-30 13:56:02 +0800489 if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
490 amdgpu_vm_is_gpu_reset(adev, id))) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100491 struct dma_fence *fence;
Alex Xiee60f8db2017-03-09 11:36:26 -0500492 u64 pd_addr = amdgpu_vm_adjust_mc_addr(adev, job->vm_pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100493
Alex Xiee60f8db2017-03-09 11:36:26 -0500494 trace_amdgpu_vm_flush(pd_addr, ring->idx, job->vm_id);
495 amdgpu_ring_emit_vm_flush(ring, job->vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100496
Christian König3dab83b2016-06-01 13:31:17 +0200497 r = amdgpu_fence_emit(ring, &fence);
498 if (r)
499 return r;
500
Christian König41d9eb22016-03-01 16:46:18 +0100501 mutex_lock(&adev->vm_manager.lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100502 dma_fence_put(id->last_flush);
Christian König3dab83b2016-06-01 13:31:17 +0200503 id->last_flush = fence;
Christian König41d9eb22016-03-01 16:46:18 +0100504 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 }
Christian Königcffadc82016-03-01 13:34:49 +0100506
Christian Königd564a062016-03-01 15:51:53 +0100507 if (gds_switch_needed) {
Chunming Zhoufd53be32016-07-01 17:59:01 +0800508 id->gds_base = job->gds_base;
509 id->gds_size = job->gds_size;
510 id->gws_base = job->gws_base;
511 id->gws_size = job->gws_size;
512 id->oa_base = job->oa_base;
513 id->oa_size = job->oa_size;
514 amdgpu_ring_emit_gds_switch(ring, job->vm_id,
515 job->gds_base, job->gds_size,
516 job->gws_base, job->gws_size,
517 job->oa_base, job->oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100518 }
Christian König41d9eb22016-03-01 16:46:18 +0100519
520 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100521}
522
523/**
524 * amdgpu_vm_reset_id - reset VMID to zero
525 *
526 * @adev: amdgpu device structure
527 * @vm_id: vmid number to use
528 *
529 * Reset saved GDW, GWS and OA to force switch on next flush.
530 */
531void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
532{
Christian Königbcb1ba32016-03-08 15:40:11 +0100533 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100534
Christian Königbcb1ba32016-03-08 15:40:11 +0100535 id->gds_base = 0;
536 id->gds_size = 0;
537 id->gws_base = 0;
538 id->gws_size = 0;
539 id->oa_base = 0;
540 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541}
542
543/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
545 *
546 * @vm: requested vm
547 * @bo: requested buffer object
548 *
Christian König8843dbb2016-01-26 12:17:11 +0100549 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550 * Search inside the @bos vm list for the requested vm
551 * Returns the found bo_va or NULL if none is found
552 *
553 * Object has to be reserved!
554 */
555struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
556 struct amdgpu_bo *bo)
557{
558 struct amdgpu_bo_va *bo_va;
559
560 list_for_each_entry(bo_va, &bo->va, bo_list) {
561 if (bo_va->vm == vm) {
562 return bo_va;
563 }
564 }
565 return NULL;
566}
567
568/**
Christian Königafef8b82016-08-12 13:29:18 +0200569 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 *
Christian König29efc4f2016-08-04 14:52:50 +0200571 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572 * @pe: addr of the page entry
573 * @addr: dst addr to write into pe
574 * @count: number of page entries to update
575 * @incr: increase next addr by incr bytes
576 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 *
578 * Traces the parameters and calls the right asic functions
579 * to setup the page table using the DMA.
580 */
Christian Königafef8b82016-08-12 13:29:18 +0200581static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
582 uint64_t pe, uint64_t addr,
583 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800584 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585{
Christian Königec2f05f2016-09-25 16:11:52 +0200586 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587
Christian Königafef8b82016-08-12 13:29:18 +0200588 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200589 amdgpu_vm_write_pte(params->adev, params->ib, pe,
590 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591
592 } else {
Christian König27c5f362016-08-04 15:02:49 +0200593 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 count, incr, flags);
595 }
596}
597
598/**
Christian Königafef8b82016-08-12 13:29:18 +0200599 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
600 *
601 * @params: see amdgpu_pte_update_params definition
602 * @pe: addr of the page entry
603 * @addr: dst addr to write into pe
604 * @count: number of page entries to update
605 * @incr: increase next addr by incr bytes
606 * @flags: hw access flags
607 *
608 * Traces the parameters and calls the DMA function to copy the PTEs.
609 */
610static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
611 uint64_t pe, uint64_t addr,
612 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800613 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200614{
Christian Königec2f05f2016-09-25 16:11:52 +0200615 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200616
Christian Königec2f05f2016-09-25 16:11:52 +0200617
618 trace_amdgpu_vm_copy_ptes(pe, src, count);
619
620 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200621}
622
623/**
Christian Königb07c9d22015-11-30 13:26:07 +0100624 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 *
Christian Königb07c9d22015-11-30 13:26:07 +0100626 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 * @addr: the unmapped addr
628 *
629 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100630 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400631 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200632static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633{
634 uint64_t result;
635
Christian Königde9ea7b2016-08-12 11:33:30 +0200636 /* page table offset */
637 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638
Christian Königde9ea7b2016-08-12 11:33:30 +0200639 /* in case cpu page size != gpu page size*/
640 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100641
642 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643
644 return result;
645}
646
Christian Königf8991ba2016-09-16 15:36:49 +0200647/*
648 * amdgpu_vm_update_pdes - make sure that page directory is valid
649 *
650 * @adev: amdgpu_device pointer
651 * @vm: requested vm
652 * @start: start of GPU address range
653 * @end: end of GPU address range
654 *
655 * Allocates new page tables if necessary
656 * and updates the page directory.
657 * Returns 0 for success, error for failure.
658 */
659int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
660 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661{
Christian Königf8991ba2016-09-16 15:36:49 +0200662 struct amdgpu_bo *shadow;
Christian König2d55e452016-02-08 17:37:38 +0100663 struct amdgpu_ring *ring;
Christian Königf8991ba2016-09-16 15:36:49 +0200664 uint64_t pd_addr, shadow_addr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
Christian Königf8991ba2016-09-16 15:36:49 +0200666 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100668 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200669 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +1000670 struct dma_fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800671
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 int r;
673
Christian König2d55e452016-02-08 17:37:38 +0100674 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König67003a12016-10-12 14:46:26 +0200675 shadow = vm->root.bo->shadow;
Christian König2d55e452016-02-08 17:37:38 +0100676
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 /* padding, etc. */
678 ndw = 64;
679
680 /* assume the worst case */
Christian König67003a12016-10-12 14:46:26 +0200681 ndw += vm->root.last_entry_used * 6;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682
Christian König67003a12016-10-12 14:46:26 +0200683 pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
Christian Königf8991ba2016-09-16 15:36:49 +0200684 if (shadow) {
685 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
686 if (r)
687 return r;
688 shadow_addr = amdgpu_bo_gpu_offset(shadow);
689 ndw *= 2;
690 } else {
691 shadow_addr = 0;
692 }
693
Christian Königd71518b2016-02-01 12:20:25 +0100694 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
695 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100697
Christian König27c5f362016-08-04 15:02:49 +0200698 memset(&params, 0, sizeof(params));
699 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200700 params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701
702 /* walk over the address space and update the page directory */
Christian König67003a12016-10-12 14:46:26 +0200703 for (pt_idx = 0; pt_idx <= vm->root.last_entry_used; ++pt_idx) {
704 struct amdgpu_bo *bo = vm->root.entries[pt_idx].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 uint64_t pde, pt;
706
707 if (bo == NULL)
708 continue;
709
Christian König0fc86832016-09-16 11:46:23 +0200710 if (bo->shadow) {
Christian Königf8991ba2016-09-16 15:36:49 +0200711 struct amdgpu_bo *pt_shadow = bo->shadow;
Christian König0fc86832016-09-16 11:46:23 +0200712
Christian Königf8991ba2016-09-16 15:36:49 +0200713 r = amdgpu_ttm_bind(&pt_shadow->tbo,
714 &pt_shadow->tbo.mem);
Christian König0fc86832016-09-16 11:46:23 +0200715 if (r)
716 return r;
717 }
718
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719 pt = amdgpu_bo_gpu_offset(bo);
Christian König67003a12016-10-12 14:46:26 +0200720 if (vm->root.entries[pt_idx].addr == pt)
Christian Königf8991ba2016-09-16 15:36:49 +0200721 continue;
722
Christian König67003a12016-10-12 14:46:26 +0200723 vm->root.entries[pt_idx].addr = pt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724
725 pde = pd_addr + pt_idx * 8;
726 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +0200727 ((last_pt + incr * count) != pt) ||
728 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729
730 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500731 uint64_t pt_addr =
732 amdgpu_vm_adjust_mc_addr(adev, last_pt);
733
Christian Königf8991ba2016-09-16 15:36:49 +0200734 if (shadow)
735 amdgpu_vm_do_set_ptes(&params,
736 last_shadow,
Alex Xiee60f8db2017-03-09 11:36:26 -0500737 pt_addr, count,
Christian Königf8991ba2016-09-16 15:36:49 +0200738 incr,
739 AMDGPU_PTE_VALID);
740
Christian Königafef8b82016-08-12 13:29:18 +0200741 amdgpu_vm_do_set_ptes(&params, last_pde,
Alex Xiee60f8db2017-03-09 11:36:26 -0500742 pt_addr, count, incr,
Christian Königafef8b82016-08-12 13:29:18 +0200743 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744 }
745
746 count = 1;
747 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +0200748 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749 last_pt = pt;
750 } else {
751 ++count;
752 }
753 }
754
Christian Königf8991ba2016-09-16 15:36:49 +0200755 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500756 uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt);
757
Christian König67003a12016-10-12 14:46:26 +0200758 if (vm->root.bo->shadow)
Alex Xiee60f8db2017-03-09 11:36:26 -0500759 amdgpu_vm_do_set_ptes(&params, last_shadow, pt_addr,
Christian Königf8991ba2016-09-16 15:36:49 +0200760 count, incr, AMDGPU_PTE_VALID);
761
Alex Xiee60f8db2017-03-09 11:36:26 -0500762 amdgpu_vm_do_set_ptes(&params, last_pde, pt_addr,
Christian Königafef8b82016-08-12 13:29:18 +0200763 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800764 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765
Christian Königf8991ba2016-09-16 15:36:49 +0200766 if (params.ib->length_dw == 0) {
767 amdgpu_job_free(job);
768 return 0;
769 }
770
771 amdgpu_ring_pad_ib(ring, params.ib);
Christian König67003a12016-10-12 14:46:26 +0200772 amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königf8991ba2016-09-16 15:36:49 +0200773 AMDGPU_FENCE_OWNER_VM);
774 if (shadow)
775 amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
776 AMDGPU_FENCE_OWNER_VM);
777
778 WARN_ON(params.ib->length_dw > ndw);
779 r = amdgpu_job_submit(job, ring, &vm->entity,
780 AMDGPU_FENCE_OWNER_VM, &fence);
781 if (r)
782 goto error_free;
783
Christian König67003a12016-10-12 14:46:26 +0200784 amdgpu_bo_fence(vm->root.bo, fence, true);
Christian Königa24960f2016-10-12 13:20:52 +0200785 dma_fence_put(vm->last_dir_update);
786 vm->last_dir_update = dma_fence_get(fence);
Dave Airlie220196b2016-10-28 11:33:52 +1000787 dma_fence_put(fence);
Christian Königf8991ba2016-09-16 15:36:49 +0200788
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800790
791error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100792 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800793 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794}
795
796/**
Christian König92696dd2016-08-05 13:56:35 +0200797 * amdgpu_vm_update_ptes - make sure that page tables are valid
798 *
799 * @params: see amdgpu_pte_update_params definition
800 * @vm: requested vm
801 * @start: start of GPU address range
802 * @end: end of GPU address range
803 * @dst: destination address to map to, the next dst inside the function
804 * @flags: mapping flags
805 *
806 * Update the page tables in the range @start - @end.
807 */
808static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200809 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800810 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +0200811{
812 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
813
814 uint64_t cur_pe_start, cur_nptes, cur_dst;
815 uint64_t addr; /* next GPU address to be updated */
816 uint64_t pt_idx;
817 struct amdgpu_bo *pt;
818 unsigned nptes; /* next number of ptes to be updated */
819 uint64_t next_pe_start;
820
821 /* initialize the variables */
822 addr = start;
823 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200824 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800825 if (params->shadow) {
826 if (!pt->shadow)
827 return;
Christian König914b4dc2016-09-28 12:27:37 +0200828 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800829 }
Christian König92696dd2016-08-05 13:56:35 +0200830 if ((addr & ~mask) == (end & ~mask))
831 nptes = end - addr;
832 else
833 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
834
835 cur_pe_start = amdgpu_bo_gpu_offset(pt);
836 cur_pe_start += (addr & mask) * 8;
837 cur_nptes = nptes;
838 cur_dst = dst;
839
840 /* for next ptb*/
841 addr += nptes;
842 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
843
844 /* walk over the address space and update the page tables */
845 while (addr < end) {
846 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200847 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800848 if (params->shadow) {
849 if (!pt->shadow)
850 return;
Christian König914b4dc2016-09-28 12:27:37 +0200851 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800852 }
Christian König92696dd2016-08-05 13:56:35 +0200853
854 if ((addr & ~mask) == (end & ~mask))
855 nptes = end - addr;
856 else
857 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
858
859 next_pe_start = amdgpu_bo_gpu_offset(pt);
860 next_pe_start += (addr & mask) * 8;
861
Christian König96105e52016-08-12 12:59:59 +0200862 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
863 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
Christian König92696dd2016-08-05 13:56:35 +0200864 /* The next ptb is consecutive to current ptb.
Christian Königafef8b82016-08-12 13:29:18 +0200865 * Don't call the update function now.
Christian König92696dd2016-08-05 13:56:35 +0200866 * Will update two ptbs together in future.
867 */
868 cur_nptes += nptes;
869 } else {
Christian Königafef8b82016-08-12 13:29:18 +0200870 params->func(params, cur_pe_start, cur_dst, cur_nptes,
871 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200872
873 cur_pe_start = next_pe_start;
874 cur_nptes = nptes;
875 cur_dst = dst;
876 }
877
878 /* for next ptb*/
879 addr += nptes;
880 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
881 }
882
Christian Königafef8b82016-08-12 13:29:18 +0200883 params->func(params, cur_pe_start, cur_dst, cur_nptes,
884 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200885}
886
887/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400888 * amdgpu_vm_frag_ptes - add fragment information to PTEs
889 *
Christian König29efc4f2016-08-04 14:52:50 +0200890 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +0200891 * @vm: requested vm
892 * @start: first PTE to handle
893 * @end: last PTE to handle
894 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400896 */
Christian König27c5f362016-08-04 15:02:49 +0200897static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200898 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800899 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400900{
901 /**
902 * The MC L1 TLB supports variable sized pages, based on a fragment
903 * field in the PTE. When this field is set to a non-zero value, page
904 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
905 * flags are considered valid for all PTEs within the fragment range
906 * and corresponding mappings are assumed to be physically contiguous.
907 *
908 * The L1 TLB can store a single PTE for the whole fragment,
909 * significantly increasing the space available for translation
910 * caching. This leads to large improvements in throughput when the
911 * TLB is under pressure.
912 *
913 * The L2 TLB distributes small and large fragments into two
914 * asymmetric partitions. The large fragment cache is significantly
915 * larger. Thus, we try to use large fragments wherever possible.
916 * Userspace can support this by aligning virtual base address and
917 * allocation size to the fragment size.
918 */
919
Christian König80366172016-10-04 13:39:43 +0200920 /* SI and newer are optimized for 64KB */
921 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
922 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400923
Christian König92696dd2016-08-05 13:56:35 +0200924 uint64_t frag_start = ALIGN(start, frag_align);
925 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +0100926
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +0200928 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Christian König92696dd2016-08-05 13:56:35 +0200929 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400930
Christian König49ac8a22016-10-13 15:09:08 +0200931 amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400932 return;
933 }
934
935 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +0200936 if (start != frag_start) {
Christian König49ac8a22016-10-13 15:09:08 +0200937 amdgpu_vm_update_ptes(params, start, frag_start,
Christian König92696dd2016-08-05 13:56:35 +0200938 dst, flags);
939 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940 }
941
942 /* handle the area in the middle */
Christian König49ac8a22016-10-13 15:09:08 +0200943 amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
Christian König80366172016-10-04 13:39:43 +0200944 flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400945
946 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +0200947 if (frag_end != end) {
948 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Christian König49ac8a22016-10-13 15:09:08 +0200949 amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 }
951}
952
953/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
955 *
956 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +0200957 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +0100958 * @src: address where to copy page table entries from
959 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100960 * @vm: requested vm
961 * @start: start of mapped range
962 * @last: last mapped entry
963 * @flags: flags for the entries
964 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 * @fence: optional resulting fence
966 *
Christian Königa14faa62016-01-25 14:27:31 +0100967 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969 */
970static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100971 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100972 uint64_t src,
973 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100975 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +0800976 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100977 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978{
Christian König2d55e452016-02-08 17:37:38 +0100979 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100980 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100982 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200983 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100984 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985 int r;
986
Christian Königafef8b82016-08-12 13:29:18 +0200987 memset(&params, 0, sizeof(params));
988 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +0200989 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +0200990 params.src = src;
991
Christian König2d55e452016-02-08 17:37:38 +0100992 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +0200993
Christian Königa1e08d32016-01-26 11:40:46 +0100994 /* sync to everything on unmapping */
995 if (!(flags & AMDGPU_PTE_VALID))
996 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
997
Christian Königa14faa62016-01-25 14:27:31 +0100998 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999
1000 /*
1001 * reserve space for one command every (1 << BLOCK_SIZE)
1002 * entries or 2k dwords (whatever is smaller)
1003 */
1004 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
1005
1006 /* padding, etc. */
1007 ndw = 64;
1008
Christian Königb0456f92016-08-11 14:06:54 +02001009 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 /* only copy commands needed */
1011 ndw += ncmds * 7;
1012
Christian Königafef8b82016-08-12 13:29:18 +02001013 params.func = amdgpu_vm_do_copy_ptes;
1014
Christian Königb0456f92016-08-11 14:06:54 +02001015 } else if (pages_addr) {
1016 /* copy commands needed */
1017 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001018
Christian Königb0456f92016-08-11 14:06:54 +02001019 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 ndw += nptes * 2;
1021
Christian Königafef8b82016-08-12 13:29:18 +02001022 params.func = amdgpu_vm_do_copy_ptes;
1023
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024 } else {
1025 /* set page commands needed */
1026 ndw += ncmds * 10;
1027
1028 /* two extra commands for begin/end of fragment */
1029 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001030
1031 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001032 }
1033
Christian Königd71518b2016-02-01 12:20:25 +01001034 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1035 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001036 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001037
Christian König29efc4f2016-08-04 14:52:50 +02001038 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001039
Christian Königb0456f92016-08-11 14:06:54 +02001040 if (!src && pages_addr) {
1041 uint64_t *pte;
1042 unsigned i;
1043
1044 /* Put the PTEs at the end of the IB. */
1045 i = ndw - nptes * 2;
1046 pte= (uint64_t *)&(job->ibs->ptr[i]);
1047 params.src = job->ibs->gpu_addr + i * 4;
1048
1049 for (i = 0; i < nptes; ++i) {
1050 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1051 AMDGPU_GPU_PAGE_SIZE);
1052 pte[i] |= flags;
1053 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001054 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001055 }
1056
Christian König3cabaa52016-06-06 10:17:58 +02001057 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1058 if (r)
1059 goto error_free;
1060
Christian König67003a12016-10-12 14:46:26 +02001061 r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001062 owner);
1063 if (r)
1064 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001065
Christian König67003a12016-10-12 14:46:26 +02001066 r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001067 if (r)
1068 goto error_free;
1069
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001070 params.shadow = true;
Christian König49ac8a22016-10-13 15:09:08 +02001071 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001072 params.shadow = false;
Christian König49ac8a22016-10-13 15:09:08 +02001073 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074
Christian König29efc4f2016-08-04 14:52:50 +02001075 amdgpu_ring_pad_ib(ring, params.ib);
1076 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001077 r = amdgpu_job_submit(job, ring, &vm->entity,
1078 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001079 if (r)
1080 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081
Christian König67003a12016-10-12 14:46:26 +02001082 amdgpu_bo_fence(vm->root.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001083 dma_fence_put(*fence);
1084 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001085 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001086
1087error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001088 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001089 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090}
1091
1092/**
Christian Königa14faa62016-01-25 14:27:31 +01001093 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1094 *
1095 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001096 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001097 * @gtt_flags: flags as they are used for GTT
1098 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001099 * @vm: requested vm
1100 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001101 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001102 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001103 * @fence: optional resulting fence
1104 *
1105 * Split the mapping into smaller chunks so that each update fits
1106 * into a SDMA IB.
1107 * Returns 0 for success, -EINVAL for failure.
1108 */
1109static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001110 struct dma_fence *exclusive,
Chunming Zhou6b777602016-09-21 16:19:19 +08001111 uint64_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +02001112 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001113 struct amdgpu_vm *vm,
1114 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001115 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001116 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001117 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001118{
Christian König63e0ba42016-08-16 17:38:37 +02001119 uint64_t pfn, src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +01001120 int r;
1121
1122 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1123 * but in case of something, we filter the flags in first place
1124 */
1125 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1126 flags &= ~AMDGPU_PTE_READABLE;
1127 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1128 flags &= ~AMDGPU_PTE_WRITEABLE;
1129
Alex Xie15b31c52017-03-03 16:47:11 -05001130 flags &= ~AMDGPU_PTE_EXECUTABLE;
1131 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1132
Alex Xieb0fd18b2017-03-03 16:49:39 -05001133 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1134 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1135
Christian Königa14faa62016-01-25 14:27:31 +01001136 trace_amdgpu_vm_bo_update(mapping);
1137
Christian König63e0ba42016-08-16 17:38:37 +02001138 pfn = mapping->offset >> PAGE_SHIFT;
1139 if (nodes) {
1140 while (pfn >= nodes->size) {
1141 pfn -= nodes->size;
1142 ++nodes;
1143 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001144 }
Christian Königa14faa62016-01-25 14:27:31 +01001145
Christian König63e0ba42016-08-16 17:38:37 +02001146 do {
1147 uint64_t max_entries;
1148 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001149
Christian König63e0ba42016-08-16 17:38:37 +02001150 if (nodes) {
1151 addr = nodes->start << PAGE_SHIFT;
1152 max_entries = (nodes->size - pfn) *
1153 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1154 } else {
1155 addr = 0;
1156 max_entries = S64_MAX;
1157 }
Christian Königa14faa62016-01-25 14:27:31 +01001158
Christian König63e0ba42016-08-16 17:38:37 +02001159 if (pages_addr) {
1160 if (flags == gtt_flags)
1161 src = adev->gart.table_addr +
1162 (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
1163 else
1164 max_entries = min(max_entries, 16ull * 1024ull);
1165 addr = 0;
1166 } else if (flags & AMDGPU_PTE_VALID) {
1167 addr += adev->vm_manager.vram_base_offset;
1168 }
1169 addr += pfn << PAGE_SHIFT;
1170
1171 last = min((uint64_t)mapping->it.last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001172 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1173 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001174 start, last, flags, addr,
1175 fence);
1176 if (r)
1177 return r;
1178
Christian König63e0ba42016-08-16 17:38:37 +02001179 pfn += last - start + 1;
1180 if (nodes && nodes->size == pfn) {
1181 pfn = 0;
1182 ++nodes;
1183 }
Christian Königa14faa62016-01-25 14:27:31 +01001184 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001185
1186 } while (unlikely(start != mapping->it.last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001187
1188 return 0;
1189}
1190
1191/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1193 *
1194 * @adev: amdgpu_device pointer
1195 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001196 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 *
1198 * Fill in the page table entries for @bo_va.
1199 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001200 */
1201int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1202 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001203 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001204{
1205 struct amdgpu_vm *vm = bo_va->vm;
1206 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001207 dma_addr_t *pages_addr = NULL;
Chunming Zhou6b777602016-09-21 16:19:19 +08001208 uint64_t gtt_flags, flags;
Christian König99e124f2016-08-16 14:43:17 +02001209 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001210 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001211 struct dma_fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 int r;
1213
Christian Königa5f6b5b2017-01-30 11:01:38 +01001214 if (clear || !bo_va->bo) {
Christian König99e124f2016-08-16 14:43:17 +02001215 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001216 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001217 exclusive = NULL;
1218 } else {
Christian König8358dce2016-03-30 10:50:25 +02001219 struct ttm_dma_tt *ttm;
1220
Christian König99e124f2016-08-16 14:43:17 +02001221 mem = &bo_va->bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001222 nodes = mem->mm_node;
1223 if (mem->mem_type == TTM_PL_TT) {
Christian König8358dce2016-03-30 10:50:25 +02001224 ttm = container_of(bo_va->bo->tbo.ttm, struct
1225 ttm_dma_tt, ttm);
1226 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001227 }
Christian König3cabaa52016-06-06 10:17:58 +02001228 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001229 }
1230
Christian Königa5f6b5b2017-01-30 11:01:38 +01001231 if (bo_va->bo) {
1232 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1233 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1234 adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
1235 flags : 0;
1236 } else {
1237 flags = 0x0;
1238 gtt_flags = ~0x0;
1239 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001240
Christian König7fc11952015-07-30 11:53:42 +02001241 spin_lock(&vm->status_lock);
1242 if (!list_empty(&bo_va->vm_status))
1243 list_splice_init(&bo_va->valids, &bo_va->invalids);
1244 spin_unlock(&vm->status_lock);
1245
1246 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001247 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1248 gtt_flags, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001249 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001250 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001251 if (r)
1252 return r;
1253 }
1254
Christian Königd6c10f62015-09-28 12:00:23 +02001255 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1256 list_for_each_entry(mapping, &bo_va->valids, list)
1257 trace_amdgpu_vm_bo_mapping(mapping);
1258
1259 list_for_each_entry(mapping, &bo_va->invalids, list)
1260 trace_amdgpu_vm_bo_mapping(mapping);
1261 }
1262
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001264 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001265 list_del_init(&bo_va->vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001266 if (clear)
Christian König7fc11952015-07-30 11:53:42 +02001267 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001268 spin_unlock(&vm->status_lock);
1269
1270 return 0;
1271}
1272
1273/**
Christian König284710f2017-01-30 11:09:31 +01001274 * amdgpu_vm_update_prt_state - update the global PRT state
1275 */
1276static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1277{
1278 unsigned long flags;
1279 bool enable;
1280
1281 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001282 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001283 adev->gart.gart_funcs->set_prt(adev, enable);
1284 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1285}
1286
1287/**
Christian König4388fc22017-03-13 10:13:36 +01001288 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001289 */
1290static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1291{
Christian König4388fc22017-03-13 10:13:36 +01001292 if (!adev->gart.gart_funcs->set_prt)
1293 return;
1294
Christian König451bc8e2017-02-14 16:02:52 +01001295 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1296 amdgpu_vm_update_prt_state(adev);
1297}
1298
1299/**
Christian König0b15f2f2017-02-14 15:47:03 +01001300 * amdgpu_vm_prt_put - drop a PRT user
1301 */
1302static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1303{
Christian König451bc8e2017-02-14 16:02:52 +01001304 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001305 amdgpu_vm_update_prt_state(adev);
1306}
1307
1308/**
Christian König451bc8e2017-02-14 16:02:52 +01001309 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001310 */
1311static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1312{
1313 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1314
Christian König0b15f2f2017-02-14 15:47:03 +01001315 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001316 kfree(cb);
1317}
1318
1319/**
Christian König451bc8e2017-02-14 16:02:52 +01001320 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1321 */
1322static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1323 struct dma_fence *fence)
1324{
Christian König4388fc22017-03-13 10:13:36 +01001325 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001326
Christian König4388fc22017-03-13 10:13:36 +01001327 if (!adev->gart.gart_funcs->set_prt)
1328 return;
1329
1330 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001331 if (!cb) {
1332 /* Last resort when we are OOM */
1333 if (fence)
1334 dma_fence_wait(fence, false);
1335
1336 amdgpu_vm_prt_put(cb->adev);
1337 } else {
1338 cb->adev = adev;
1339 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1340 amdgpu_vm_prt_cb))
1341 amdgpu_vm_prt_cb(fence, &cb->cb);
1342 }
1343}
1344
1345/**
Christian König284710f2017-01-30 11:09:31 +01001346 * amdgpu_vm_free_mapping - free a mapping
1347 *
1348 * @adev: amdgpu_device pointer
1349 * @vm: requested vm
1350 * @mapping: mapping to be freed
1351 * @fence: fence of the unmap operation
1352 *
1353 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1354 */
1355static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1356 struct amdgpu_vm *vm,
1357 struct amdgpu_bo_va_mapping *mapping,
1358 struct dma_fence *fence)
1359{
Christian König451bc8e2017-02-14 16:02:52 +01001360 if (mapping->flags & AMDGPU_PTE_PRT)
1361 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001362 kfree(mapping);
1363}
1364
1365/**
Christian König451bc8e2017-02-14 16:02:52 +01001366 * amdgpu_vm_prt_fini - finish all prt mappings
1367 *
1368 * @adev: amdgpu_device pointer
1369 * @vm: requested vm
1370 *
1371 * Register a cleanup callback to disable PRT support after VM dies.
1372 */
1373static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1374{
Christian König67003a12016-10-12 14:46:26 +02001375 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001376 struct dma_fence *excl, **shared;
1377 unsigned i, shared_count;
1378 int r;
1379
1380 r = reservation_object_get_fences_rcu(resv, &excl,
1381 &shared_count, &shared);
1382 if (r) {
1383 /* Not enough memory to grab the fence list, as last resort
1384 * block for all the fences to complete.
1385 */
1386 reservation_object_wait_timeout_rcu(resv, true, false,
1387 MAX_SCHEDULE_TIMEOUT);
1388 return;
1389 }
1390
1391 /* Add a callback for each fence in the reservation object */
1392 amdgpu_vm_prt_get(adev);
1393 amdgpu_vm_add_prt_cb(adev, excl);
1394
1395 for (i = 0; i < shared_count; ++i) {
1396 amdgpu_vm_prt_get(adev);
1397 amdgpu_vm_add_prt_cb(adev, shared[i]);
1398 }
1399
1400 kfree(shared);
1401}
1402
1403/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001404 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1405 *
1406 * @adev: amdgpu_device pointer
1407 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001408 * @fence: optional resulting fence (unchanged if no work needed to be done
1409 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410 *
1411 * Make sure all freed BOs are cleared in the PT.
1412 * Returns 0 for success.
1413 *
1414 * PTs have to be reserved and mutex must be locked!
1415 */
1416int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001417 struct amdgpu_vm *vm,
1418 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419{
1420 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001421 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422 int r;
1423
1424 while (!list_empty(&vm->freed)) {
1425 mapping = list_first_entry(&vm->freed,
1426 struct amdgpu_bo_va_mapping, list);
1427 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001428
Christian König3cabaa52016-06-06 10:17:58 +02001429 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001430 0, 0, &f);
1431 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001432 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001433 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001434 return r;
Christian König284710f2017-01-30 11:09:31 +01001435 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001436 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001437
1438 if (fence && f) {
1439 dma_fence_put(*fence);
1440 *fence = f;
1441 } else {
1442 dma_fence_put(f);
1443 }
1444
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001445 return 0;
1446
1447}
1448
1449/**
1450 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1451 *
1452 * @adev: amdgpu_device pointer
1453 * @vm: requested vm
1454 *
1455 * Make sure all invalidated BOs are cleared in the PT.
1456 * Returns 0 for success.
1457 *
1458 * PTs have to be reserved and mutex must be locked!
1459 */
1460int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001461 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001462{
monk.liucfe2c972015-05-26 15:01:54 +08001463 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001464 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001465
1466 spin_lock(&vm->status_lock);
1467 while (!list_empty(&vm->invalidated)) {
1468 bo_va = list_first_entry(&vm->invalidated,
1469 struct amdgpu_bo_va, vm_status);
1470 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001471
Christian König99e124f2016-08-16 14:43:17 +02001472 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001473 if (r)
1474 return r;
1475
1476 spin_lock(&vm->status_lock);
1477 }
1478 spin_unlock(&vm->status_lock);
1479
monk.liucfe2c972015-05-26 15:01:54 +08001480 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001481 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001482
1483 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484}
1485
1486/**
1487 * amdgpu_vm_bo_add - add a bo to a specific vm
1488 *
1489 * @adev: amdgpu_device pointer
1490 * @vm: requested vm
1491 * @bo: amdgpu buffer object
1492 *
Christian König8843dbb2016-01-26 12:17:11 +01001493 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494 * Add @bo to the list of bos associated with the vm
1495 * Returns newly added bo_va or NULL for failure
1496 *
1497 * Object has to be reserved!
1498 */
1499struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1500 struct amdgpu_vm *vm,
1501 struct amdgpu_bo *bo)
1502{
1503 struct amdgpu_bo_va *bo_va;
1504
1505 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1506 if (bo_va == NULL) {
1507 return NULL;
1508 }
1509 bo_va->vm = vm;
1510 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001511 bo_va->ref_count = 1;
1512 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001513 INIT_LIST_HEAD(&bo_va->valids);
1514 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001515 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001516
Christian Königa5f6b5b2017-01-30 11:01:38 +01001517 if (bo)
1518 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001519
1520 return bo_va;
1521}
1522
1523/**
1524 * amdgpu_vm_bo_map - map bo inside a vm
1525 *
1526 * @adev: amdgpu_device pointer
1527 * @bo_va: bo_va to store the address
1528 * @saddr: where to map the BO
1529 * @offset: requested offset in the BO
1530 * @flags: attributes of pages (read/write/valid/etc.)
1531 *
1532 * Add a mapping of the BO at the specefied addr into the VM.
1533 * Returns 0 for success, error for failure.
1534 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001535 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536 */
1537int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1538 struct amdgpu_bo_va *bo_va,
1539 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001540 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541{
1542 struct amdgpu_bo_va_mapping *mapping;
1543 struct amdgpu_vm *vm = bo_va->vm;
1544 struct interval_tree_node *it;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001545 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546
Christian König0be52de2015-05-18 14:37:27 +02001547 /* validate the parameters */
1548 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001549 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001550 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001551
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001553 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001554 if (saddr >= eaddr ||
1555 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001558 saddr /= AMDGPU_GPU_PAGE_SIZE;
1559 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1560
Felix Kuehling005ae952015-11-23 17:43:48 -05001561 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001562 if (it) {
1563 struct amdgpu_bo_va_mapping *tmp;
1564 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1565 /* bo and tmp overlap, invalid addr */
1566 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1567 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1568 tmp->it.start, tmp->it.last + 1);
Christian König663e4572017-03-13 10:13:37 +01001569 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001570 }
1571
1572 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001573 if (!mapping)
1574 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001575
1576 INIT_LIST_HEAD(&mapping->list);
1577 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001578 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001579 mapping->offset = offset;
1580 mapping->flags = flags;
1581
Christian König7fc11952015-07-30 11:53:42 +02001582 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583 interval_tree_insert(&mapping->it, &vm->va);
1584
Christian König4388fc22017-03-13 10:13:36 +01001585 if (flags & AMDGPU_PTE_PRT)
1586 amdgpu_vm_prt_get(adev);
1587
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001588 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589}
1590
1591/**
Christian König80f95c52017-03-13 10:13:39 +01001592 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1593 *
1594 * @adev: amdgpu_device pointer
1595 * @bo_va: bo_va to store the address
1596 * @saddr: where to map the BO
1597 * @offset: requested offset in the BO
1598 * @flags: attributes of pages (read/write/valid/etc.)
1599 *
1600 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1601 * mappings as we do so.
1602 * Returns 0 for success, error for failure.
1603 *
1604 * Object has to be reserved and unreserved outside!
1605 */
1606int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1607 struct amdgpu_bo_va *bo_va,
1608 uint64_t saddr, uint64_t offset,
1609 uint64_t size, uint64_t flags)
1610{
1611 struct amdgpu_bo_va_mapping *mapping;
1612 struct amdgpu_vm *vm = bo_va->vm;
1613 uint64_t eaddr;
1614 int r;
1615
1616 /* validate the parameters */
1617 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1618 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1619 return -EINVAL;
1620
1621 /* make sure object fit at this offset */
1622 eaddr = saddr + size - 1;
1623 if (saddr >= eaddr ||
1624 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
1625 return -EINVAL;
1626
1627 /* Allocate all the needed memory */
1628 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1629 if (!mapping)
1630 return -ENOMEM;
1631
1632 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
1633 if (r) {
1634 kfree(mapping);
1635 return r;
1636 }
1637
1638 saddr /= AMDGPU_GPU_PAGE_SIZE;
1639 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1640
1641 mapping->it.start = saddr;
1642 mapping->it.last = eaddr;
1643 mapping->offset = offset;
1644 mapping->flags = flags;
1645
1646 list_add(&mapping->list, &bo_va->invalids);
1647 interval_tree_insert(&mapping->it, &vm->va);
1648
1649 if (flags & AMDGPU_PTE_PRT)
1650 amdgpu_vm_prt_get(adev);
1651
1652 return 0;
1653}
1654
1655/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1657 *
1658 * @adev: amdgpu_device pointer
1659 * @bo_va: bo_va to remove the address from
1660 * @saddr: where to the BO is mapped
1661 *
1662 * Remove a mapping of the BO at the specefied addr from the VM.
1663 * Returns 0 for success, error for failure.
1664 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001665 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001666 */
1667int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1668 struct amdgpu_bo_va *bo_va,
1669 uint64_t saddr)
1670{
1671 struct amdgpu_bo_va_mapping *mapping;
1672 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001673 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674
Christian König6c7fc502015-06-05 20:56:17 +02001675 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001676
Christian König7fc11952015-07-30 11:53:42 +02001677 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 if (mapping->it.start == saddr)
1679 break;
1680 }
1681
Christian König7fc11952015-07-30 11:53:42 +02001682 if (&mapping->list == &bo_va->valids) {
1683 valid = false;
1684
1685 list_for_each_entry(mapping, &bo_va->invalids, list) {
1686 if (mapping->it.start == saddr)
1687 break;
1688 }
1689
Christian König32b41ac2016-03-08 18:03:27 +01001690 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001691 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001692 }
Christian König32b41ac2016-03-08 18:03:27 +01001693
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001694 list_del(&mapping->list);
1695 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001696 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001697
Christian Könige17841b2016-03-08 17:52:01 +01001698 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001699 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001700 else
Christian König284710f2017-01-30 11:09:31 +01001701 amdgpu_vm_free_mapping(adev, vm, mapping,
1702 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703
1704 return 0;
1705}
1706
1707/**
Christian Königdc54d3d2017-03-13 10:13:38 +01001708 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1709 *
1710 * @adev: amdgpu_device pointer
1711 * @vm: VM structure to use
1712 * @saddr: start of the range
1713 * @size: size of the range
1714 *
1715 * Remove all mappings in a range, split them as appropriate.
1716 * Returns 0 for success, error for failure.
1717 */
1718int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1719 struct amdgpu_vm *vm,
1720 uint64_t saddr, uint64_t size)
1721{
1722 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1723 struct interval_tree_node *it;
1724 LIST_HEAD(removed);
1725 uint64_t eaddr;
1726
1727 eaddr = saddr + size - 1;
1728 saddr /= AMDGPU_GPU_PAGE_SIZE;
1729 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1730
1731 /* Allocate all the needed memory */
1732 before = kzalloc(sizeof(*before), GFP_KERNEL);
1733 if (!before)
1734 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08001735 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001736
1737 after = kzalloc(sizeof(*after), GFP_KERNEL);
1738 if (!after) {
1739 kfree(before);
1740 return -ENOMEM;
1741 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08001742 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001743
1744 /* Now gather all removed mappings */
1745 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
1746 while (it) {
1747 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1748 it = interval_tree_iter_next(it, saddr, eaddr);
1749
1750 /* Remember mapping split at the start */
1751 if (tmp->it.start < saddr) {
Junwei Zhang27f6d612017-03-16 16:09:24 +08001752 before->it.start = tmp->it.start;
Christian Königdc54d3d2017-03-13 10:13:38 +01001753 before->it.last = saddr - 1;
1754 before->offset = tmp->offset;
1755 before->flags = tmp->flags;
1756 list_add(&before->list, &tmp->list);
1757 }
1758
1759 /* Remember mapping split at the end */
1760 if (tmp->it.last > eaddr) {
1761 after->it.start = eaddr + 1;
1762 after->it.last = tmp->it.last;
1763 after->offset = tmp->offset;
1764 after->offset += after->it.start - tmp->it.start;
1765 after->flags = tmp->flags;
1766 list_add(&after->list, &tmp->list);
1767 }
1768
1769 list_del(&tmp->list);
1770 list_add(&tmp->list, &removed);
1771 }
1772
1773 /* And free them up */
1774 list_for_each_entry_safe(tmp, next, &removed, list) {
1775 interval_tree_remove(&tmp->it, &vm->va);
1776 list_del(&tmp->list);
1777
1778 if (tmp->it.start < saddr)
1779 tmp->it.start = saddr;
1780 if (tmp->it.last > eaddr)
1781 tmp->it.last = eaddr;
1782
1783 list_add(&tmp->list, &vm->freed);
1784 trace_amdgpu_vm_bo_unmap(NULL, tmp);
1785 }
1786
Junwei Zhang27f6d612017-03-16 16:09:24 +08001787 /* Insert partial mapping before the range */
1788 if (!list_empty(&before->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001789 interval_tree_insert(&before->it, &vm->va);
1790 if (before->flags & AMDGPU_PTE_PRT)
1791 amdgpu_vm_prt_get(adev);
1792 } else {
1793 kfree(before);
1794 }
1795
1796 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08001797 if (!list_empty(&after->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001798 interval_tree_insert(&after->it, &vm->va);
1799 if (after->flags & AMDGPU_PTE_PRT)
1800 amdgpu_vm_prt_get(adev);
1801 } else {
1802 kfree(after);
1803 }
1804
1805 return 0;
1806}
1807
1808/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001809 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1810 *
1811 * @adev: amdgpu_device pointer
1812 * @bo_va: requested bo_va
1813 *
Christian König8843dbb2016-01-26 12:17:11 +01001814 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 *
1816 * Object have to be reserved!
1817 */
1818void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1819 struct amdgpu_bo_va *bo_va)
1820{
1821 struct amdgpu_bo_va_mapping *mapping, *next;
1822 struct amdgpu_vm *vm = bo_va->vm;
1823
1824 list_del(&bo_va->bo_list);
1825
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001826 spin_lock(&vm->status_lock);
1827 list_del(&bo_va->vm_status);
1828 spin_unlock(&vm->status_lock);
1829
Christian König7fc11952015-07-30 11:53:42 +02001830 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001831 list_del(&mapping->list);
1832 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001833 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001834 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001835 }
Christian König7fc11952015-07-30 11:53:42 +02001836 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1837 list_del(&mapping->list);
1838 interval_tree_remove(&mapping->it, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01001839 amdgpu_vm_free_mapping(adev, vm, mapping,
1840 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02001841 }
Christian König32b41ac2016-03-08 18:03:27 +01001842
Chris Wilsonf54d1862016-10-25 13:00:45 +01001843 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001844 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001845}
1846
1847/**
1848 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1849 *
1850 * @adev: amdgpu_device pointer
1851 * @vm: requested vm
1852 * @bo: amdgpu buffer object
1853 *
Christian König8843dbb2016-01-26 12:17:11 +01001854 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855 */
1856void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1857 struct amdgpu_bo *bo)
1858{
1859 struct amdgpu_bo_va *bo_va;
1860
1861 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001862 spin_lock(&bo_va->vm->status_lock);
1863 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001865 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001866 }
1867}
1868
1869/**
1870 * amdgpu_vm_init - initialize a vm instance
1871 *
1872 * @adev: amdgpu_device pointer
1873 * @vm: requested vm
1874 *
Christian König8843dbb2016-01-26 12:17:11 +01001875 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001876 */
1877int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1878{
1879 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1880 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001881 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001882 unsigned ring_instance;
1883 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001884 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001885 int i, r;
1886
Christian Königbcb1ba32016-03-08 15:40:11 +01001887 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1888 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001890 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001891 spin_lock_init(&vm->status_lock);
1892 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001893 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001894 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001895
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896 pd_size = amdgpu_vm_directory_size(adev);
1897 pd_entries = amdgpu_vm_num_pdes(adev);
1898
1899 /* allocate page table array */
Christian König67003a12016-10-12 14:46:26 +02001900 vm->root.entries = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
1901 if (vm->root.entries == NULL) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001902 DRM_ERROR("Cannot allocate memory for page table array\n");
1903 return -ENOMEM;
1904 }
1905
Christian König2bd9ccf2016-02-01 12:53:58 +01001906 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001907
1908 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1909 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1910 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001911 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1912 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1913 rq, amdgpu_sched_jobs);
1914 if (r)
Chunming Zhou64827ad2016-07-28 17:20:32 +08001915 goto err;
Christian König2bd9ccf2016-02-01 12:53:58 +01001916
Christian Königa24960f2016-10-12 13:20:52 +02001917 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001918
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001919 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001920 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08001921 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
Christian König03f48dd2016-08-15 17:00:22 +02001922 AMDGPU_GEM_CREATE_SHADOW |
Christian König617859e2016-11-17 15:40:02 +01001923 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1924 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Christian König67003a12016-10-12 14:46:26 +02001925 NULL, NULL, &vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001926 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001927 goto error_free_sched_entity;
1928
Christian König67003a12016-10-12 14:46:26 +02001929 r = amdgpu_bo_reserve(vm->root.bo, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001930 if (r)
Christian König67003a12016-10-12 14:46:26 +02001931 goto error_free_root;
Christian König2bd9ccf2016-02-01 12:53:58 +01001932
Christian König5a712a82016-06-21 16:28:15 +02001933 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König67003a12016-10-12 14:46:26 +02001934 amdgpu_bo_unreserve(vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935
1936 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001937
Christian König67003a12016-10-12 14:46:26 +02001938error_free_root:
1939 amdgpu_bo_unref(&vm->root.bo->shadow);
1940 amdgpu_bo_unref(&vm->root.bo);
1941 vm->root.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01001942
1943error_free_sched_entity:
1944 amd_sched_entity_fini(&ring->sched, &vm->entity);
1945
Chunming Zhou64827ad2016-07-28 17:20:32 +08001946err:
Christian König67003a12016-10-12 14:46:26 +02001947 drm_free_large(vm->root.entries);
Chunming Zhou64827ad2016-07-28 17:20:32 +08001948
Christian König2bd9ccf2016-02-01 12:53:58 +01001949 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001950}
1951
1952/**
1953 * amdgpu_vm_fini - tear down a vm instance
1954 *
1955 * @adev: amdgpu_device pointer
1956 * @vm: requested vm
1957 *
Christian König8843dbb2016-01-26 12:17:11 +01001958 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001959 * Unbind the VM and remove all bos from the vm bo list
1960 */
1961void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1962{
1963 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01001964 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001965 int i;
1966
Christian König2d55e452016-02-08 17:37:38 +01001967 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001968
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969 if (!RB_EMPTY_ROOT(&vm->va)) {
1970 dev_err(adev->dev, "still active bo inside vm\n");
1971 }
1972 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1973 list_del(&mapping->list);
1974 interval_tree_remove(&mapping->it, &vm->va);
1975 kfree(mapping);
1976 }
1977 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01001978 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01001979 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01001980 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01001981 }
Christian König284710f2017-01-30 11:09:31 +01001982
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001983 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01001984 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001985 }
1986
Chunming Zhou1baa4392016-08-04 13:59:32 +08001987 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) {
Christian König67003a12016-10-12 14:46:26 +02001988 struct amdgpu_bo *pt = vm->root.entries[i].bo;
Christian König2698f622016-09-16 13:06:09 +02001989
1990 if (!pt)
1991 continue;
1992
1993 amdgpu_bo_unref(&pt->shadow);
1994 amdgpu_bo_unref(&pt);
Chunming Zhou1baa4392016-08-04 13:59:32 +08001995 }
Christian König67003a12016-10-12 14:46:26 +02001996 drm_free_large(vm->root.entries);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001997
Christian König67003a12016-10-12 14:46:26 +02001998 amdgpu_bo_unref(&vm->root.bo->shadow);
1999 amdgpu_bo_unref(&vm->root.bo);
Christian Königa24960f2016-10-12 13:20:52 +02002000 dma_fence_put(vm->last_dir_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002001}
Christian Königea89f8c2015-11-15 20:52:06 +01002002
2003/**
Christian Königa9a78b32016-01-21 10:19:11 +01002004 * amdgpu_vm_manager_init - init the VM manager
2005 *
2006 * @adev: amdgpu_device pointer
2007 *
2008 * Initialize the VM manager structures
2009 */
2010void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2011{
2012 unsigned i;
2013
2014 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
2015
2016 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01002017 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
2018 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01002019 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01002020 list_add_tail(&adev->vm_manager.ids[i].list,
2021 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01002022 }
Christian König2d55e452016-02-08 17:37:38 +01002023
Chris Wilsonf54d1862016-10-25 13:00:45 +01002024 adev->vm_manager.fence_context =
2025 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002026 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2027 adev->vm_manager.seqno[i] = 0;
2028
Christian König2d55e452016-02-08 17:37:38 +01002029 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002030 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002031 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002032 atomic_set(&adev->vm_manager.num_prt_users, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01002033}
2034
2035/**
Christian Königea89f8c2015-11-15 20:52:06 +01002036 * amdgpu_vm_manager_fini - cleanup VM manager
2037 *
2038 * @adev: amdgpu_device pointer
2039 *
2040 * Cleanup the VM manager and free resources.
2041 */
2042void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2043{
2044 unsigned i;
2045
Christian Königbcb1ba32016-03-08 15:40:11 +01002046 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
2047 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
2048
Chris Wilsonf54d1862016-10-25 13:00:45 +01002049 dma_fence_put(adev->vm_manager.ids[i].first);
Christian König832a9022016-02-15 12:33:02 +01002050 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Chris Wilsonf54d1862016-10-25 13:00:45 +01002051 dma_fence_put(id->flushed_updates);
Dave Airlie7b624ad2016-11-07 09:37:09 +10002052 dma_fence_put(id->last_flush);
Christian Königbcb1ba32016-03-08 15:40:11 +01002053 }
Christian Königea89f8c2015-11-15 20:52:06 +01002054}