blob: 8f5e995903138bdc32836259f233ca9a2ab5b1b7 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence-array.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <drm/drmP.h>
30#include <drm/amdgpu_drm.h>
31#include "amdgpu.h"
32#include "amdgpu_trace.h"
33
34/*
35 * GPUVM
36 * GPUVM is similar to the legacy gart on older asics, however
37 * rather than there being a single global gart table
38 * for the entire GPU, there are multiple VM page tables active
39 * at any given time. The VM page tables can contain a mix
40 * vram pages and system memory pages and system memory pages
41 * can be mapped as snooped (cached system pages) or unsnooped
42 * (uncached system pages).
43 * Each VM has an ID associated with it and there is a page table
44 * associated with each VMID. When execting a command buffer,
45 * the kernel tells the the ring what VMID to use for that command
46 * buffer. VMIDs are allocated dynamically as commands are submitted.
47 * The userspace drivers maintain their own address space and the kernel
48 * sets up their pages tables accordingly when they submit their
49 * command buffers and a VMID is assigned.
50 * Cayman/Trinity support up to 8 active VMs at any given time;
51 * SI supports 16.
52 */
53
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040054/* Local structure. Encapsulate some VM table update parameters to reduce
55 * the number of function parameters
56 */
Christian König29efc4f2016-08-04 14:52:50 +020057struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020058 /* amdgpu device we do this update for */
59 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +020060 /* optional amdgpu_vm we do this update for */
61 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040062 /* address where to copy page table entries from */
63 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040064 /* indirect buffer to fill with commands */
65 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020066 /* Function which actually does the update */
67 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
68 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080069 uint64_t flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +080070 /* indicate update pt or its shadow */
71 bool shadow;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040072};
73
Christian König284710f2017-01-30 11:09:31 +010074/* Helper to disable partial resident texture feature from a fence callback */
75struct amdgpu_prt_cb {
76 struct amdgpu_device *adev;
77 struct dma_fence_cb cb;
78};
79
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080/**
Christian König72a7ec52016-10-19 11:03:57 +020081 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082 *
83 * @adev: amdgpu_device pointer
84 *
Christian König72a7ec52016-10-19 11:03:57 +020085 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 */
Christian König72a7ec52016-10-19 11:03:57 +020087static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
88 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089{
Christian König72a7ec52016-10-19 11:03:57 +020090 if (level == 0)
91 /* For the root directory */
92 return adev->vm_manager.max_pfn >>
93 (amdgpu_vm_block_size * adev->vm_manager.num_level);
94 else if (level == adev->vm_manager.num_level)
95 /* For the page tables on the leaves */
96 return AMDGPU_VM_PTE_COUNT;
97 else
98 /* Everything in between */
99 return 1 << amdgpu_vm_block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100}
101
102/**
Christian König72a7ec52016-10-19 11:03:57 +0200103 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 *
105 * @adev: amdgpu_device pointer
106 *
Christian König72a7ec52016-10-19 11:03:57 +0200107 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 */
Christian König72a7ec52016-10-19 11:03:57 +0200109static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110{
Christian König72a7ec52016-10-19 11:03:57 +0200111 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112}
113
114/**
Christian König56467eb2015-12-11 15:16:32 +0100115 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 *
117 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100118 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100119 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 *
121 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100122 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 */
Christian König56467eb2015-12-11 15:16:32 +0100124void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
125 struct list_head *validated,
126 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127{
Christian König67003a12016-10-12 14:46:26 +0200128 entry->robj = vm->root.bo;
Christian König56467eb2015-12-11 15:16:32 +0100129 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200130 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100131 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100132 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100133 list_add(&entry->tv.head, validated);
134}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135
Christian König56467eb2015-12-11 15:16:32 +0100136/**
Christian Königf7da30d2016-09-28 12:03:04 +0200137 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100138 *
Christian König5a712a82016-06-21 16:28:15 +0200139 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100140 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200141 * @validate: callback to do the validation
142 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143 *
Christian Königf7da30d2016-09-28 12:03:04 +0200144 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 */
Christian Königf7da30d2016-09-28 12:03:04 +0200146int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
147 int (*validate)(void *p, struct amdgpu_bo *bo),
148 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149{
Christian König5a712a82016-06-21 16:28:15 +0200150 uint64_t num_evictions;
Christian Königee1782c2015-12-11 21:01:23 +0100151 unsigned i;
Christian Königf7da30d2016-09-28 12:03:04 +0200152 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400153
Christian König5a712a82016-06-21 16:28:15 +0200154 /* We only need to validate the page tables
155 * if they aren't already valid.
156 */
157 num_evictions = atomic64_read(&adev->num_evictions);
158 if (num_evictions == vm->last_eviction_counter)
Christian Königf7da30d2016-09-28 12:03:04 +0200159 return 0;
Christian König5a712a82016-06-21 16:28:15 +0200160
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161 /* add the vm page table to the list */
Christian König67003a12016-10-12 14:46:26 +0200162 for (i = 0; i <= vm->root.last_entry_used; ++i) {
163 struct amdgpu_bo *bo = vm->root.entries[i].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164
Christian König914b4dc2016-09-28 12:27:37 +0200165 if (!bo)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166 continue;
167
Christian König914b4dc2016-09-28 12:27:37 +0200168 r = validate(param, bo);
Christian Königf7da30d2016-09-28 12:03:04 +0200169 if (r)
170 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 }
Christian Königeceb8a12016-01-11 15:35:21 +0100172
Christian Königf7da30d2016-09-28 12:03:04 +0200173 return 0;
Christian Königeceb8a12016-01-11 15:35:21 +0100174}
175
176/**
177 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
178 *
179 * @adev: amdgpu device instance
180 * @vm: vm providing the BOs
181 *
182 * Move the PT BOs to the tail of the LRU.
183 */
184void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
185 struct amdgpu_vm *vm)
186{
187 struct ttm_bo_global *glob = adev->mman.bdev.glob;
188 unsigned i;
189
190 spin_lock(&glob->lru_lock);
Christian König67003a12016-10-12 14:46:26 +0200191 for (i = 0; i <= vm->root.last_entry_used; ++i) {
192 struct amdgpu_bo *bo = vm->root.entries[i].bo;
Christian Königeceb8a12016-01-11 15:35:21 +0100193
Christian König914b4dc2016-09-28 12:27:37 +0200194 if (!bo)
Christian Königeceb8a12016-01-11 15:35:21 +0100195 continue;
196
Christian König914b4dc2016-09-28 12:27:37 +0200197 ttm_bo_move_to_lru_tail(&bo->tbo);
Christian Königeceb8a12016-01-11 15:35:21 +0100198 }
199 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200}
201
Christian König663e4572017-03-13 10:13:37 +0100202/**
203 * amdgpu_vm_alloc_pts - Allocate page tables.
204 *
205 * @adev: amdgpu_device pointer
206 * @vm: VM to allocate page tables for
207 * @saddr: Start address which needs to be allocated
208 * @size: Size from start address we need.
209 *
210 * Make sure the page tables are allocated.
211 */
212int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
213 struct amdgpu_vm *vm,
214 uint64_t saddr, uint64_t size)
215{
216 unsigned last_pfn, pt_idx;
217 uint64_t eaddr;
218 int r;
219
220 /* validate the parameters */
221 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
222 return -EINVAL;
223
224 eaddr = saddr + size - 1;
225 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
226 if (last_pfn >= adev->vm_manager.max_pfn) {
227 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
228 last_pfn, adev->vm_manager.max_pfn);
229 return -EINVAL;
230 }
231
232 saddr /= AMDGPU_GPU_PAGE_SIZE;
233 eaddr /= AMDGPU_GPU_PAGE_SIZE;
234
235 saddr >>= amdgpu_vm_block_size;
236 eaddr >>= amdgpu_vm_block_size;
237
Christian König72a7ec52016-10-19 11:03:57 +0200238 BUG_ON(eaddr >= amdgpu_vm_num_entries(adev, 0));
Christian König663e4572017-03-13 10:13:37 +0100239
Christian König67003a12016-10-12 14:46:26 +0200240 if (eaddr > vm->root.last_entry_used)
241 vm->root.last_entry_used = eaddr;
Christian König663e4572017-03-13 10:13:37 +0100242
243 /* walk over the address space and allocate the page tables */
244 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian König67003a12016-10-12 14:46:26 +0200245 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König663e4572017-03-13 10:13:37 +0100246 struct amdgpu_bo *pt;
247
Christian König67003a12016-10-12 14:46:26 +0200248 if (vm->root.entries[pt_idx].bo)
Christian König663e4572017-03-13 10:13:37 +0100249 continue;
250
251 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
252 AMDGPU_GPU_PAGE_SIZE, true,
253 AMDGPU_GEM_DOMAIN_VRAM,
254 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
255 AMDGPU_GEM_CREATE_SHADOW |
256 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
257 AMDGPU_GEM_CREATE_VRAM_CLEARED,
258 NULL, resv, &pt);
259 if (r)
260 return r;
261
262 /* Keep a reference to the page table to avoid freeing
263 * them up in the wrong order.
264 */
Christian König67003a12016-10-12 14:46:26 +0200265 pt->parent = amdgpu_bo_ref(vm->root.bo);
Christian König663e4572017-03-13 10:13:37 +0100266
Christian König67003a12016-10-12 14:46:26 +0200267 vm->root.entries[pt_idx].bo = pt;
268 vm->root.entries[pt_idx].addr = 0;
Christian König663e4572017-03-13 10:13:37 +0100269 }
270
271 return 0;
272}
273
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800274static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
275 struct amdgpu_vm_id *id)
276{
277 return id->current_gpu_reset_count !=
278 atomic_read(&adev->gpu_reset_counter) ? true : false;
279}
280
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281/**
282 * amdgpu_vm_grab_id - allocate the next free VMID
283 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200285 * @ring: ring we want to submit job to
286 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100287 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400288 *
Christian König7f8a5292015-07-20 16:09:40 +0200289 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290 */
Christian König7f8a5292015-07-20 16:09:40 +0200291int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100292 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800293 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400295 struct amdgpu_device *adev = ring->adev;
Christian König090b7672016-07-08 10:21:02 +0200296 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100297 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200298 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100299 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200300 unsigned i;
301 int r = 0;
302
303 fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
304 GFP_KERNEL);
305 if (!fences)
306 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400307
Christian König94dd0a42016-01-18 17:01:42 +0100308 mutex_lock(&adev->vm_manager.lock);
309
Christian König36fd7c52016-05-23 15:30:08 +0200310 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200311 i = 0;
Christian König8d76001e2016-05-23 16:00:32 +0200312 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200313 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
314 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200315 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200316 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200317 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100318
Christian König1fbb2e92016-06-01 10:47:36 +0200319 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König8d76001e2016-05-23 16:00:32 +0200320 if (&idle->list == &adev->vm_manager.ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200321 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
322 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100323 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200324 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200325
Christian König1fbb2e92016-06-01 10:47:36 +0200326 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100327 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200328
Chris Wilsonf54d1862016-10-25 13:00:45 +0100329 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200330 seqno, true);
331 if (!array) {
332 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100333 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200334 kfree(fences);
335 r = -ENOMEM;
336 goto error;
337 }
Christian König8d76001e2016-05-23 16:00:32 +0200338
Christian König8d76001e2016-05-23 16:00:32 +0200339
Christian König1fbb2e92016-06-01 10:47:36 +0200340 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100341 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200342 if (r)
343 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200344
Christian König1fbb2e92016-06-01 10:47:36 +0200345 mutex_unlock(&adev->vm_manager.lock);
346 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200347
Christian König1fbb2e92016-06-01 10:47:36 +0200348 }
349 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200350
Chunming Zhoufd53be32016-07-01 17:59:01 +0800351 job->vm_needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200352 /* Check if we can use a VMID already assigned to this VM */
353 i = ring->idx;
354 do {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100355 struct dma_fence *flushed;
Christian König8d76001e2016-05-23 16:00:32 +0200356
Christian König1fbb2e92016-06-01 10:47:36 +0200357 id = vm->ids[i++];
358 if (i == AMDGPU_MAX_RINGS)
359 i = 0;
360
361 /* Check all the prerequisites to using this VMID */
362 if (!id)
363 continue;
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800364 if (amdgpu_vm_is_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800365 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200366
367 if (atomic64_read(&id->owner) != vm->client_id)
368 continue;
369
Chunming Zhoufd53be32016-07-01 17:59:01 +0800370 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200371 continue;
372
Christian König090b7672016-07-08 10:21:02 +0200373 if (!id->last_flush)
374 continue;
375
376 if (id->last_flush->context != fence_context &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100377 !dma_fence_is_signaled(id->last_flush))
Christian König1fbb2e92016-06-01 10:47:36 +0200378 continue;
379
380 flushed = id->flushed_updates;
381 if (updates &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100382 (!flushed || dma_fence_is_later(updates, flushed)))
Christian König1fbb2e92016-06-01 10:47:36 +0200383 continue;
384
Christian König3dab83b2016-06-01 13:31:17 +0200385 /* Good we can use this VMID. Remember this submission as
386 * user of the VMID.
387 */
Christian König1fbb2e92016-06-01 10:47:36 +0200388 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
389 if (r)
390 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200391
Chunming Zhou6adb0512016-06-27 17:06:01 +0800392 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König1fbb2e92016-06-01 10:47:36 +0200393 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
394 vm->ids[ring->idx] = id;
Christian König8d76001e2016-05-23 16:00:32 +0200395
Chunming Zhoufd53be32016-07-01 17:59:01 +0800396 job->vm_id = id - adev->vm_manager.ids;
397 job->vm_needs_flush = false;
Christian König0c0fdf12016-07-08 10:48:24 +0200398 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König8d76001e2016-05-23 16:00:32 +0200399
Christian König1fbb2e92016-06-01 10:47:36 +0200400 mutex_unlock(&adev->vm_manager.lock);
401 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200402
Christian König1fbb2e92016-06-01 10:47:36 +0200403 } while (i != ring->idx);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800404
Christian König1fbb2e92016-06-01 10:47:36 +0200405 /* Still no ID to use? Then use the idle one found earlier */
406 id = idle;
407
408 /* Remember this submission as user of the VMID */
409 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100410 if (r)
411 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100412
Chris Wilsonf54d1862016-10-25 13:00:45 +0100413 dma_fence_put(id->first);
414 id->first = dma_fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100415
Chris Wilsonf54d1862016-10-25 13:00:45 +0100416 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100417 id->last_flush = NULL;
418
Chris Wilsonf54d1862016-10-25 13:00:45 +0100419 dma_fence_put(id->flushed_updates);
420 id->flushed_updates = dma_fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100421
Chunming Zhoufd53be32016-07-01 17:59:01 +0800422 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhoub46b8a82016-06-27 17:04:23 +0800423 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König832a9022016-02-15 12:33:02 +0100424 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König0ea54b92016-05-04 10:20:01 +0200425 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100426 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400427
Chunming Zhoufd53be32016-07-01 17:59:01 +0800428 job->vm_id = id - adev->vm_manager.ids;
Christian König0c0fdf12016-07-08 10:48:24 +0200429 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König832a9022016-02-15 12:33:02 +0100430
431error:
Christian König94dd0a42016-01-18 17:01:42 +0100432 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100433 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434}
435
Alex Deucher93dcc372016-06-17 17:05:15 -0400436static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
437{
438 struct amdgpu_device *adev = ring->adev;
Alex Deuchera1255102016-10-13 17:41:13 -0400439 const struct amdgpu_ip_block *ip_block;
Alex Deucher93dcc372016-06-17 17:05:15 -0400440
Christian König21cd9422016-10-05 15:36:39 +0200441 if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
Alex Deucher93dcc372016-06-17 17:05:15 -0400442 /* only compute rings */
443 return false;
444
445 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
446 if (!ip_block)
447 return false;
448
Alex Deuchera1255102016-10-13 17:41:13 -0400449 if (ip_block->version->major <= 7) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400450 /* gfx7 has no workaround */
451 return true;
Alex Deuchera1255102016-10-13 17:41:13 -0400452 } else if (ip_block->version->major == 8) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400453 if (adev->gfx.mec_fw_version >= 673)
454 /* gfx8 is fixed in MEC firmware 673 */
455 return false;
456 else
457 return true;
458 }
459 return false;
460}
461
Alex Xiee60f8db2017-03-09 11:36:26 -0500462static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr)
463{
464 u64 addr = mc_addr;
465
466 if (adev->mc.mc_funcs && adev->mc.mc_funcs->adjust_mc_addr)
467 addr = adev->mc.mc_funcs->adjust_mc_addr(adev, addr);
468
469 return addr;
470}
471
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472/**
473 * amdgpu_vm_flush - hardware flush the vm
474 *
475 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100476 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100477 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400478 *
Christian König4ff37a82016-02-26 16:18:26 +0100479 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480 */
Chunming Zhoufd53be32016-07-01 17:59:01 +0800481int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400482{
Christian König971fe9a92016-03-01 15:09:25 +0100483 struct amdgpu_device *adev = ring->adev;
Chunming Zhoufd53be32016-07-01 17:59:01 +0800484 struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100485 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800486 id->gds_base != job->gds_base ||
487 id->gds_size != job->gds_size ||
488 id->gws_base != job->gws_base ||
489 id->gws_size != job->gws_size ||
490 id->oa_base != job->oa_base ||
491 id->oa_size != job->oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100492 int r;
Christian Königd564a062016-03-01 15:51:53 +0100493
494 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800495 job->vm_needs_flush || gds_switch_needed ||
Alex Deucher93dcc372016-06-17 17:05:15 -0400496 amdgpu_vm_ring_has_compute_vm_bug(ring)))
Christian Königd564a062016-03-01 15:51:53 +0100497 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100498
Chunming Zhouaa1c8902016-06-30 13:56:02 +0800499 if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
500 amdgpu_vm_is_gpu_reset(adev, id))) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100501 struct dma_fence *fence;
Alex Xiee60f8db2017-03-09 11:36:26 -0500502 u64 pd_addr = amdgpu_vm_adjust_mc_addr(adev, job->vm_pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100503
Alex Xiee60f8db2017-03-09 11:36:26 -0500504 trace_amdgpu_vm_flush(pd_addr, ring->idx, job->vm_id);
505 amdgpu_ring_emit_vm_flush(ring, job->vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100506
Christian König3dab83b2016-06-01 13:31:17 +0200507 r = amdgpu_fence_emit(ring, &fence);
508 if (r)
509 return r;
510
Christian König41d9eb22016-03-01 16:46:18 +0100511 mutex_lock(&adev->vm_manager.lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100512 dma_fence_put(id->last_flush);
Christian König3dab83b2016-06-01 13:31:17 +0200513 id->last_flush = fence;
Christian König41d9eb22016-03-01 16:46:18 +0100514 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515 }
Christian Königcffadc82016-03-01 13:34:49 +0100516
Christian Königd564a062016-03-01 15:51:53 +0100517 if (gds_switch_needed) {
Chunming Zhoufd53be32016-07-01 17:59:01 +0800518 id->gds_base = job->gds_base;
519 id->gds_size = job->gds_size;
520 id->gws_base = job->gws_base;
521 id->gws_size = job->gws_size;
522 id->oa_base = job->oa_base;
523 id->oa_size = job->oa_size;
524 amdgpu_ring_emit_gds_switch(ring, job->vm_id,
525 job->gds_base, job->gds_size,
526 job->gws_base, job->gws_size,
527 job->oa_base, job->oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100528 }
Christian König41d9eb22016-03-01 16:46:18 +0100529
530 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100531}
532
533/**
534 * amdgpu_vm_reset_id - reset VMID to zero
535 *
536 * @adev: amdgpu device structure
537 * @vm_id: vmid number to use
538 *
539 * Reset saved GDW, GWS and OA to force switch on next flush.
540 */
541void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
542{
Christian Königbcb1ba32016-03-08 15:40:11 +0100543 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100544
Christian Königbcb1ba32016-03-08 15:40:11 +0100545 id->gds_base = 0;
546 id->gds_size = 0;
547 id->gws_base = 0;
548 id->gws_size = 0;
549 id->oa_base = 0;
550 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551}
552
553/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400554 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
555 *
556 * @vm: requested vm
557 * @bo: requested buffer object
558 *
Christian König8843dbb2016-01-26 12:17:11 +0100559 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 * Search inside the @bos vm list for the requested vm
561 * Returns the found bo_va or NULL if none is found
562 *
563 * Object has to be reserved!
564 */
565struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
566 struct amdgpu_bo *bo)
567{
568 struct amdgpu_bo_va *bo_va;
569
570 list_for_each_entry(bo_va, &bo->va, bo_list) {
571 if (bo_va->vm == vm) {
572 return bo_va;
573 }
574 }
575 return NULL;
576}
577
578/**
Christian Königafef8b82016-08-12 13:29:18 +0200579 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 *
Christian König29efc4f2016-08-04 14:52:50 +0200581 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 * @pe: addr of the page entry
583 * @addr: dst addr to write into pe
584 * @count: number of page entries to update
585 * @incr: increase next addr by incr bytes
586 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 *
588 * Traces the parameters and calls the right asic functions
589 * to setup the page table using the DMA.
590 */
Christian Königafef8b82016-08-12 13:29:18 +0200591static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
592 uint64_t pe, uint64_t addr,
593 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800594 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595{
Christian Königec2f05f2016-09-25 16:11:52 +0200596 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597
Christian Königafef8b82016-08-12 13:29:18 +0200598 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200599 amdgpu_vm_write_pte(params->adev, params->ib, pe,
600 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601
602 } else {
Christian König27c5f362016-08-04 15:02:49 +0200603 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 count, incr, flags);
605 }
606}
607
608/**
Christian Königafef8b82016-08-12 13:29:18 +0200609 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
610 *
611 * @params: see amdgpu_pte_update_params definition
612 * @pe: addr of the page entry
613 * @addr: dst addr to write into pe
614 * @count: number of page entries to update
615 * @incr: increase next addr by incr bytes
616 * @flags: hw access flags
617 *
618 * Traces the parameters and calls the DMA function to copy the PTEs.
619 */
620static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
621 uint64_t pe, uint64_t addr,
622 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800623 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200624{
Christian Königec2f05f2016-09-25 16:11:52 +0200625 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200626
Christian Königec2f05f2016-09-25 16:11:52 +0200627
628 trace_amdgpu_vm_copy_ptes(pe, src, count);
629
630 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200631}
632
633/**
Christian Königb07c9d22015-11-30 13:26:07 +0100634 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635 *
Christian Königb07c9d22015-11-30 13:26:07 +0100636 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 * @addr: the unmapped addr
638 *
639 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100640 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200642static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643{
644 uint64_t result;
645
Christian Königde9ea7b2016-08-12 11:33:30 +0200646 /* page table offset */
647 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648
Christian Königde9ea7b2016-08-12 11:33:30 +0200649 /* in case cpu page size != gpu page size*/
650 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100651
652 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653
654 return result;
655}
656
Christian Königf8991ba2016-09-16 15:36:49 +0200657/*
658 * amdgpu_vm_update_pdes - make sure that page directory is valid
659 *
660 * @adev: amdgpu_device pointer
661 * @vm: requested vm
662 * @start: start of GPU address range
663 * @end: end of GPU address range
664 *
665 * Allocates new page tables if necessary
666 * and updates the page directory.
667 * Returns 0 for success, error for failure.
668 */
669int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
670 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671{
Christian Königf8991ba2016-09-16 15:36:49 +0200672 struct amdgpu_bo *shadow;
Christian König2d55e452016-02-08 17:37:38 +0100673 struct amdgpu_ring *ring;
Christian Königf8991ba2016-09-16 15:36:49 +0200674 uint64_t pd_addr, shadow_addr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
Christian Königf8991ba2016-09-16 15:36:49 +0200676 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100678 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200679 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +1000680 struct dma_fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800681
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 int r;
683
Christian König2d55e452016-02-08 17:37:38 +0100684 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König67003a12016-10-12 14:46:26 +0200685 shadow = vm->root.bo->shadow;
Christian König2d55e452016-02-08 17:37:38 +0100686
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687 /* padding, etc. */
688 ndw = 64;
689
690 /* assume the worst case */
Christian König67003a12016-10-12 14:46:26 +0200691 ndw += vm->root.last_entry_used * 6;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692
Christian König67003a12016-10-12 14:46:26 +0200693 pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
Christian Königf8991ba2016-09-16 15:36:49 +0200694 if (shadow) {
695 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
696 if (r)
697 return r;
698 shadow_addr = amdgpu_bo_gpu_offset(shadow);
699 ndw *= 2;
700 } else {
701 shadow_addr = 0;
702 }
703
Christian Königd71518b2016-02-01 12:20:25 +0100704 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
705 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100707
Christian König27c5f362016-08-04 15:02:49 +0200708 memset(&params, 0, sizeof(params));
709 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200710 params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711
712 /* walk over the address space and update the page directory */
Christian König67003a12016-10-12 14:46:26 +0200713 for (pt_idx = 0; pt_idx <= vm->root.last_entry_used; ++pt_idx) {
714 struct amdgpu_bo *bo = vm->root.entries[pt_idx].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 uint64_t pde, pt;
716
717 if (bo == NULL)
718 continue;
719
Christian König0fc86832016-09-16 11:46:23 +0200720 if (bo->shadow) {
Christian Königf8991ba2016-09-16 15:36:49 +0200721 struct amdgpu_bo *pt_shadow = bo->shadow;
Christian König0fc86832016-09-16 11:46:23 +0200722
Christian Königf8991ba2016-09-16 15:36:49 +0200723 r = amdgpu_ttm_bind(&pt_shadow->tbo,
724 &pt_shadow->tbo.mem);
Christian König0fc86832016-09-16 11:46:23 +0200725 if (r)
726 return r;
727 }
728
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 pt = amdgpu_bo_gpu_offset(bo);
Christian König67003a12016-10-12 14:46:26 +0200730 if (vm->root.entries[pt_idx].addr == pt)
Christian Königf8991ba2016-09-16 15:36:49 +0200731 continue;
732
Christian König67003a12016-10-12 14:46:26 +0200733 vm->root.entries[pt_idx].addr = pt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734
735 pde = pd_addr + pt_idx * 8;
736 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +0200737 ((last_pt + incr * count) != pt) ||
738 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739
740 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500741 uint64_t pt_addr =
742 amdgpu_vm_adjust_mc_addr(adev, last_pt);
743
Christian Königf8991ba2016-09-16 15:36:49 +0200744 if (shadow)
745 amdgpu_vm_do_set_ptes(&params,
746 last_shadow,
Alex Xiee60f8db2017-03-09 11:36:26 -0500747 pt_addr, count,
Christian Königf8991ba2016-09-16 15:36:49 +0200748 incr,
749 AMDGPU_PTE_VALID);
750
Christian Königafef8b82016-08-12 13:29:18 +0200751 amdgpu_vm_do_set_ptes(&params, last_pde,
Alex Xiee60f8db2017-03-09 11:36:26 -0500752 pt_addr, count, incr,
Christian Königafef8b82016-08-12 13:29:18 +0200753 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400754 }
755
756 count = 1;
757 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +0200758 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759 last_pt = pt;
760 } else {
761 ++count;
762 }
763 }
764
Christian Königf8991ba2016-09-16 15:36:49 +0200765 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -0500766 uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt);
767
Christian König67003a12016-10-12 14:46:26 +0200768 if (vm->root.bo->shadow)
Alex Xiee60f8db2017-03-09 11:36:26 -0500769 amdgpu_vm_do_set_ptes(&params, last_shadow, pt_addr,
Christian Königf8991ba2016-09-16 15:36:49 +0200770 count, incr, AMDGPU_PTE_VALID);
771
Alex Xiee60f8db2017-03-09 11:36:26 -0500772 amdgpu_vm_do_set_ptes(&params, last_pde, pt_addr,
Christian Königafef8b82016-08-12 13:29:18 +0200773 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800774 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400775
Christian Königf8991ba2016-09-16 15:36:49 +0200776 if (params.ib->length_dw == 0) {
777 amdgpu_job_free(job);
778 return 0;
779 }
780
781 amdgpu_ring_pad_ib(ring, params.ib);
Christian König67003a12016-10-12 14:46:26 +0200782 amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königf8991ba2016-09-16 15:36:49 +0200783 AMDGPU_FENCE_OWNER_VM);
784 if (shadow)
785 amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
786 AMDGPU_FENCE_OWNER_VM);
787
788 WARN_ON(params.ib->length_dw > ndw);
789 r = amdgpu_job_submit(job, ring, &vm->entity,
790 AMDGPU_FENCE_OWNER_VM, &fence);
791 if (r)
792 goto error_free;
793
Christian König67003a12016-10-12 14:46:26 +0200794 amdgpu_bo_fence(vm->root.bo, fence, true);
Christian Königa24960f2016-10-12 13:20:52 +0200795 dma_fence_put(vm->last_dir_update);
796 vm->last_dir_update = dma_fence_get(fence);
Dave Airlie220196b2016-10-28 11:33:52 +1000797 dma_fence_put(fence);
Christian Königf8991ba2016-09-16 15:36:49 +0200798
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400799 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800800
801error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100802 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800803 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804}
805
806/**
Christian König92696dd2016-08-05 13:56:35 +0200807 * amdgpu_vm_update_ptes - make sure that page tables are valid
808 *
809 * @params: see amdgpu_pte_update_params definition
810 * @vm: requested vm
811 * @start: start of GPU address range
812 * @end: end of GPU address range
813 * @dst: destination address to map to, the next dst inside the function
814 * @flags: mapping flags
815 *
816 * Update the page tables in the range @start - @end.
817 */
818static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200819 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800820 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +0200821{
822 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
823
824 uint64_t cur_pe_start, cur_nptes, cur_dst;
825 uint64_t addr; /* next GPU address to be updated */
826 uint64_t pt_idx;
827 struct amdgpu_bo *pt;
828 unsigned nptes; /* next number of ptes to be updated */
829 uint64_t next_pe_start;
830
831 /* initialize the variables */
832 addr = start;
833 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200834 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800835 if (params->shadow) {
836 if (!pt->shadow)
837 return;
Christian König914b4dc2016-09-28 12:27:37 +0200838 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800839 }
Christian König92696dd2016-08-05 13:56:35 +0200840 if ((addr & ~mask) == (end & ~mask))
841 nptes = end - addr;
842 else
843 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
844
845 cur_pe_start = amdgpu_bo_gpu_offset(pt);
846 cur_pe_start += (addr & mask) * 8;
847 cur_nptes = nptes;
848 cur_dst = dst;
849
850 /* for next ptb*/
851 addr += nptes;
852 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
853
854 /* walk over the address space and update the page tables */
855 while (addr < end) {
856 pt_idx = addr >> amdgpu_vm_block_size;
Christian König67003a12016-10-12 14:46:26 +0200857 pt = params->vm->root.entries[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800858 if (params->shadow) {
859 if (!pt->shadow)
860 return;
Christian König914b4dc2016-09-28 12:27:37 +0200861 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800862 }
Christian König92696dd2016-08-05 13:56:35 +0200863
864 if ((addr & ~mask) == (end & ~mask))
865 nptes = end - addr;
866 else
867 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
868
869 next_pe_start = amdgpu_bo_gpu_offset(pt);
870 next_pe_start += (addr & mask) * 8;
871
Christian König96105e52016-08-12 12:59:59 +0200872 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
873 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
Christian König92696dd2016-08-05 13:56:35 +0200874 /* The next ptb is consecutive to current ptb.
Christian Königafef8b82016-08-12 13:29:18 +0200875 * Don't call the update function now.
Christian König92696dd2016-08-05 13:56:35 +0200876 * Will update two ptbs together in future.
877 */
878 cur_nptes += nptes;
879 } else {
Christian Königafef8b82016-08-12 13:29:18 +0200880 params->func(params, cur_pe_start, cur_dst, cur_nptes,
881 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200882
883 cur_pe_start = next_pe_start;
884 cur_nptes = nptes;
885 cur_dst = dst;
886 }
887
888 /* for next ptb*/
889 addr += nptes;
890 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
891 }
892
Christian Königafef8b82016-08-12 13:29:18 +0200893 params->func(params, cur_pe_start, cur_dst, cur_nptes,
894 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200895}
896
897/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400898 * amdgpu_vm_frag_ptes - add fragment information to PTEs
899 *
Christian König29efc4f2016-08-04 14:52:50 +0200900 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +0200901 * @vm: requested vm
902 * @start: first PTE to handle
903 * @end: last PTE to handle
904 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400905 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400906 */
Christian König27c5f362016-08-04 15:02:49 +0200907static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200908 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800909 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400910{
911 /**
912 * The MC L1 TLB supports variable sized pages, based on a fragment
913 * field in the PTE. When this field is set to a non-zero value, page
914 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
915 * flags are considered valid for all PTEs within the fragment range
916 * and corresponding mappings are assumed to be physically contiguous.
917 *
918 * The L1 TLB can store a single PTE for the whole fragment,
919 * significantly increasing the space available for translation
920 * caching. This leads to large improvements in throughput when the
921 * TLB is under pressure.
922 *
923 * The L2 TLB distributes small and large fragments into two
924 * asymmetric partitions. The large fragment cache is significantly
925 * larger. Thus, we try to use large fragments wherever possible.
926 * Userspace can support this by aligning virtual base address and
927 * allocation size to the fragment size.
928 */
929
Christian König80366172016-10-04 13:39:43 +0200930 /* SI and newer are optimized for 64KB */
931 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
932 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933
Christian König92696dd2016-08-05 13:56:35 +0200934 uint64_t frag_start = ALIGN(start, frag_align);
935 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +0100936
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +0200938 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Christian König92696dd2016-08-05 13:56:35 +0200939 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940
Christian König49ac8a22016-10-13 15:09:08 +0200941 amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942 return;
943 }
944
945 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +0200946 if (start != frag_start) {
Christian König49ac8a22016-10-13 15:09:08 +0200947 amdgpu_vm_update_ptes(params, start, frag_start,
Christian König92696dd2016-08-05 13:56:35 +0200948 dst, flags);
949 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 }
951
952 /* handle the area in the middle */
Christian König49ac8a22016-10-13 15:09:08 +0200953 amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
Christian König80366172016-10-04 13:39:43 +0200954 flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955
956 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +0200957 if (frag_end != end) {
958 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Christian König49ac8a22016-10-13 15:09:08 +0200959 amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960 }
961}
962
963/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400964 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
965 *
966 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +0200967 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +0100968 * @src: address where to copy page table entries from
969 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100970 * @vm: requested vm
971 * @start: start of mapped range
972 * @last: last mapped entry
973 * @flags: flags for the entries
974 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 * @fence: optional resulting fence
976 *
Christian Königa14faa62016-01-25 14:27:31 +0100977 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 */
980static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100981 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100982 uint64_t src,
983 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100985 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +0800986 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100987 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400988{
Christian König2d55e452016-02-08 17:37:38 +0100989 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100990 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400991 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100992 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200993 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100994 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 int r;
996
Christian Königafef8b82016-08-12 13:29:18 +0200997 memset(&params, 0, sizeof(params));
998 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +0200999 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001000 params.src = src;
1001
Christian König2d55e452016-02-08 17:37:38 +01001002 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001003
Christian Königa1e08d32016-01-26 11:40:46 +01001004 /* sync to everything on unmapping */
1005 if (!(flags & AMDGPU_PTE_VALID))
1006 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1007
Christian Königa14faa62016-01-25 14:27:31 +01001008 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009
1010 /*
1011 * reserve space for one command every (1 << BLOCK_SIZE)
1012 * entries or 2k dwords (whatever is smaller)
1013 */
1014 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
1015
1016 /* padding, etc. */
1017 ndw = 64;
1018
Christian Königb0456f92016-08-11 14:06:54 +02001019 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 /* only copy commands needed */
1021 ndw += ncmds * 7;
1022
Christian Königafef8b82016-08-12 13:29:18 +02001023 params.func = amdgpu_vm_do_copy_ptes;
1024
Christian Königb0456f92016-08-11 14:06:54 +02001025 } else if (pages_addr) {
1026 /* copy commands needed */
1027 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001028
Christian Königb0456f92016-08-11 14:06:54 +02001029 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001030 ndw += nptes * 2;
1031
Christian Königafef8b82016-08-12 13:29:18 +02001032 params.func = amdgpu_vm_do_copy_ptes;
1033
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001034 } else {
1035 /* set page commands needed */
1036 ndw += ncmds * 10;
1037
1038 /* two extra commands for begin/end of fragment */
1039 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001040
1041 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001042 }
1043
Christian Königd71518b2016-02-01 12:20:25 +01001044 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1045 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001047
Christian König29efc4f2016-08-04 14:52:50 +02001048 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001049
Christian Königb0456f92016-08-11 14:06:54 +02001050 if (!src && pages_addr) {
1051 uint64_t *pte;
1052 unsigned i;
1053
1054 /* Put the PTEs at the end of the IB. */
1055 i = ndw - nptes * 2;
1056 pte= (uint64_t *)&(job->ibs->ptr[i]);
1057 params.src = job->ibs->gpu_addr + i * 4;
1058
1059 for (i = 0; i < nptes; ++i) {
1060 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1061 AMDGPU_GPU_PAGE_SIZE);
1062 pte[i] |= flags;
1063 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001064 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001065 }
1066
Christian König3cabaa52016-06-06 10:17:58 +02001067 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1068 if (r)
1069 goto error_free;
1070
Christian König67003a12016-10-12 14:46:26 +02001071 r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001072 owner);
1073 if (r)
1074 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001075
Christian König67003a12016-10-12 14:46:26 +02001076 r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001077 if (r)
1078 goto error_free;
1079
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001080 params.shadow = true;
Christian König49ac8a22016-10-13 15:09:08 +02001081 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001082 params.shadow = false;
Christian König49ac8a22016-10-13 15:09:08 +02001083 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001084
Christian König29efc4f2016-08-04 14:52:50 +02001085 amdgpu_ring_pad_ib(ring, params.ib);
1086 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001087 r = amdgpu_job_submit(job, ring, &vm->entity,
1088 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001089 if (r)
1090 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091
Christian König67003a12016-10-12 14:46:26 +02001092 amdgpu_bo_fence(vm->root.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001093 dma_fence_put(*fence);
1094 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001095 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001096
1097error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001098 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001099 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100}
1101
1102/**
Christian Königa14faa62016-01-25 14:27:31 +01001103 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1104 *
1105 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001106 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001107 * @gtt_flags: flags as they are used for GTT
1108 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001109 * @vm: requested vm
1110 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001111 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001112 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001113 * @fence: optional resulting fence
1114 *
1115 * Split the mapping into smaller chunks so that each update fits
1116 * into a SDMA IB.
1117 * Returns 0 for success, -EINVAL for failure.
1118 */
1119static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001120 struct dma_fence *exclusive,
Chunming Zhou6b777602016-09-21 16:19:19 +08001121 uint64_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +02001122 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001123 struct amdgpu_vm *vm,
1124 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001125 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001126 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001127 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001128{
Christian König63e0ba42016-08-16 17:38:37 +02001129 uint64_t pfn, src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +01001130 int r;
1131
1132 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1133 * but in case of something, we filter the flags in first place
1134 */
1135 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1136 flags &= ~AMDGPU_PTE_READABLE;
1137 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1138 flags &= ~AMDGPU_PTE_WRITEABLE;
1139
Alex Xie15b31c52017-03-03 16:47:11 -05001140 flags &= ~AMDGPU_PTE_EXECUTABLE;
1141 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1142
Alex Xieb0fd18b2017-03-03 16:49:39 -05001143 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1144 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1145
Christian Königa14faa62016-01-25 14:27:31 +01001146 trace_amdgpu_vm_bo_update(mapping);
1147
Christian König63e0ba42016-08-16 17:38:37 +02001148 pfn = mapping->offset >> PAGE_SHIFT;
1149 if (nodes) {
1150 while (pfn >= nodes->size) {
1151 pfn -= nodes->size;
1152 ++nodes;
1153 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001154 }
Christian Königa14faa62016-01-25 14:27:31 +01001155
Christian König63e0ba42016-08-16 17:38:37 +02001156 do {
1157 uint64_t max_entries;
1158 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001159
Christian König63e0ba42016-08-16 17:38:37 +02001160 if (nodes) {
1161 addr = nodes->start << PAGE_SHIFT;
1162 max_entries = (nodes->size - pfn) *
1163 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1164 } else {
1165 addr = 0;
1166 max_entries = S64_MAX;
1167 }
Christian Königa14faa62016-01-25 14:27:31 +01001168
Christian König63e0ba42016-08-16 17:38:37 +02001169 if (pages_addr) {
1170 if (flags == gtt_flags)
1171 src = adev->gart.table_addr +
1172 (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
1173 else
1174 max_entries = min(max_entries, 16ull * 1024ull);
1175 addr = 0;
1176 } else if (flags & AMDGPU_PTE_VALID) {
1177 addr += adev->vm_manager.vram_base_offset;
1178 }
1179 addr += pfn << PAGE_SHIFT;
1180
1181 last = min((uint64_t)mapping->it.last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001182 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1183 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001184 start, last, flags, addr,
1185 fence);
1186 if (r)
1187 return r;
1188
Christian König63e0ba42016-08-16 17:38:37 +02001189 pfn += last - start + 1;
1190 if (nodes && nodes->size == pfn) {
1191 pfn = 0;
1192 ++nodes;
1193 }
Christian Königa14faa62016-01-25 14:27:31 +01001194 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001195
1196 } while (unlikely(start != mapping->it.last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001197
1198 return 0;
1199}
1200
1201/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001202 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1203 *
1204 * @adev: amdgpu_device pointer
1205 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001206 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 *
1208 * Fill in the page table entries for @bo_va.
1209 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210 */
1211int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1212 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001213 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214{
1215 struct amdgpu_vm *vm = bo_va->vm;
1216 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001217 dma_addr_t *pages_addr = NULL;
Chunming Zhou6b777602016-09-21 16:19:19 +08001218 uint64_t gtt_flags, flags;
Christian König99e124f2016-08-16 14:43:17 +02001219 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001220 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001221 struct dma_fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 int r;
1223
Christian Königa5f6b5b2017-01-30 11:01:38 +01001224 if (clear || !bo_va->bo) {
Christian König99e124f2016-08-16 14:43:17 +02001225 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001226 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001227 exclusive = NULL;
1228 } else {
Christian König8358dce2016-03-30 10:50:25 +02001229 struct ttm_dma_tt *ttm;
1230
Christian König99e124f2016-08-16 14:43:17 +02001231 mem = &bo_va->bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001232 nodes = mem->mm_node;
1233 if (mem->mem_type == TTM_PL_TT) {
Christian König8358dce2016-03-30 10:50:25 +02001234 ttm = container_of(bo_va->bo->tbo.ttm, struct
1235 ttm_dma_tt, ttm);
1236 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001237 }
Christian König3cabaa52016-06-06 10:17:58 +02001238 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 }
1240
Christian Königa5f6b5b2017-01-30 11:01:38 +01001241 if (bo_va->bo) {
1242 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1243 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1244 adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
1245 flags : 0;
1246 } else {
1247 flags = 0x0;
1248 gtt_flags = ~0x0;
1249 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250
Christian König7fc11952015-07-30 11:53:42 +02001251 spin_lock(&vm->status_lock);
1252 if (!list_empty(&bo_va->vm_status))
1253 list_splice_init(&bo_va->valids, &bo_va->invalids);
1254 spin_unlock(&vm->status_lock);
1255
1256 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001257 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1258 gtt_flags, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001259 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001260 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 if (r)
1262 return r;
1263 }
1264
Christian Königd6c10f62015-09-28 12:00:23 +02001265 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1266 list_for_each_entry(mapping, &bo_va->valids, list)
1267 trace_amdgpu_vm_bo_mapping(mapping);
1268
1269 list_for_each_entry(mapping, &bo_va->invalids, list)
1270 trace_amdgpu_vm_bo_mapping(mapping);
1271 }
1272
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001273 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001274 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001275 list_del_init(&bo_va->vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001276 if (clear)
Christian König7fc11952015-07-30 11:53:42 +02001277 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278 spin_unlock(&vm->status_lock);
1279
1280 return 0;
1281}
1282
1283/**
Christian König284710f2017-01-30 11:09:31 +01001284 * amdgpu_vm_update_prt_state - update the global PRT state
1285 */
1286static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1287{
1288 unsigned long flags;
1289 bool enable;
1290
1291 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001292 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001293 adev->gart.gart_funcs->set_prt(adev, enable);
1294 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1295}
1296
1297/**
Christian König4388fc22017-03-13 10:13:36 +01001298 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001299 */
1300static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1301{
Christian König4388fc22017-03-13 10:13:36 +01001302 if (!adev->gart.gart_funcs->set_prt)
1303 return;
1304
Christian König451bc8e2017-02-14 16:02:52 +01001305 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1306 amdgpu_vm_update_prt_state(adev);
1307}
1308
1309/**
Christian König0b15f2f2017-02-14 15:47:03 +01001310 * amdgpu_vm_prt_put - drop a PRT user
1311 */
1312static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1313{
Christian König451bc8e2017-02-14 16:02:52 +01001314 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001315 amdgpu_vm_update_prt_state(adev);
1316}
1317
1318/**
Christian König451bc8e2017-02-14 16:02:52 +01001319 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001320 */
1321static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1322{
1323 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1324
Christian König0b15f2f2017-02-14 15:47:03 +01001325 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001326 kfree(cb);
1327}
1328
1329/**
Christian König451bc8e2017-02-14 16:02:52 +01001330 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1331 */
1332static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1333 struct dma_fence *fence)
1334{
Christian König4388fc22017-03-13 10:13:36 +01001335 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001336
Christian König4388fc22017-03-13 10:13:36 +01001337 if (!adev->gart.gart_funcs->set_prt)
1338 return;
1339
1340 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001341 if (!cb) {
1342 /* Last resort when we are OOM */
1343 if (fence)
1344 dma_fence_wait(fence, false);
1345
1346 amdgpu_vm_prt_put(cb->adev);
1347 } else {
1348 cb->adev = adev;
1349 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1350 amdgpu_vm_prt_cb))
1351 amdgpu_vm_prt_cb(fence, &cb->cb);
1352 }
1353}
1354
1355/**
Christian König284710f2017-01-30 11:09:31 +01001356 * amdgpu_vm_free_mapping - free a mapping
1357 *
1358 * @adev: amdgpu_device pointer
1359 * @vm: requested vm
1360 * @mapping: mapping to be freed
1361 * @fence: fence of the unmap operation
1362 *
1363 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1364 */
1365static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1366 struct amdgpu_vm *vm,
1367 struct amdgpu_bo_va_mapping *mapping,
1368 struct dma_fence *fence)
1369{
Christian König451bc8e2017-02-14 16:02:52 +01001370 if (mapping->flags & AMDGPU_PTE_PRT)
1371 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001372 kfree(mapping);
1373}
1374
1375/**
Christian König451bc8e2017-02-14 16:02:52 +01001376 * amdgpu_vm_prt_fini - finish all prt mappings
1377 *
1378 * @adev: amdgpu_device pointer
1379 * @vm: requested vm
1380 *
1381 * Register a cleanup callback to disable PRT support after VM dies.
1382 */
1383static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1384{
Christian König67003a12016-10-12 14:46:26 +02001385 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001386 struct dma_fence *excl, **shared;
1387 unsigned i, shared_count;
1388 int r;
1389
1390 r = reservation_object_get_fences_rcu(resv, &excl,
1391 &shared_count, &shared);
1392 if (r) {
1393 /* Not enough memory to grab the fence list, as last resort
1394 * block for all the fences to complete.
1395 */
1396 reservation_object_wait_timeout_rcu(resv, true, false,
1397 MAX_SCHEDULE_TIMEOUT);
1398 return;
1399 }
1400
1401 /* Add a callback for each fence in the reservation object */
1402 amdgpu_vm_prt_get(adev);
1403 amdgpu_vm_add_prt_cb(adev, excl);
1404
1405 for (i = 0; i < shared_count; ++i) {
1406 amdgpu_vm_prt_get(adev);
1407 amdgpu_vm_add_prt_cb(adev, shared[i]);
1408 }
1409
1410 kfree(shared);
1411}
1412
1413/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1415 *
1416 * @adev: amdgpu_device pointer
1417 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001418 * @fence: optional resulting fence (unchanged if no work needed to be done
1419 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420 *
1421 * Make sure all freed BOs are cleared in the PT.
1422 * Returns 0 for success.
1423 *
1424 * PTs have to be reserved and mutex must be locked!
1425 */
1426int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001427 struct amdgpu_vm *vm,
1428 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429{
1430 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001431 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 int r;
1433
1434 while (!list_empty(&vm->freed)) {
1435 mapping = list_first_entry(&vm->freed,
1436 struct amdgpu_bo_va_mapping, list);
1437 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001438
Christian König3cabaa52016-06-06 10:17:58 +02001439 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001440 0, 0, &f);
1441 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001442 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001443 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 return r;
Christian König284710f2017-01-30 11:09:31 +01001445 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001446 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001447
1448 if (fence && f) {
1449 dma_fence_put(*fence);
1450 *fence = f;
1451 } else {
1452 dma_fence_put(f);
1453 }
1454
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001455 return 0;
1456
1457}
1458
1459/**
1460 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1461 *
1462 * @adev: amdgpu_device pointer
1463 * @vm: requested vm
1464 *
1465 * Make sure all invalidated BOs are cleared in the PT.
1466 * Returns 0 for success.
1467 *
1468 * PTs have to be reserved and mutex must be locked!
1469 */
1470int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001471 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001472{
monk.liucfe2c972015-05-26 15:01:54 +08001473 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001474 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001475
1476 spin_lock(&vm->status_lock);
1477 while (!list_empty(&vm->invalidated)) {
1478 bo_va = list_first_entry(&vm->invalidated,
1479 struct amdgpu_bo_va, vm_status);
1480 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001481
Christian König99e124f2016-08-16 14:43:17 +02001482 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001483 if (r)
1484 return r;
1485
1486 spin_lock(&vm->status_lock);
1487 }
1488 spin_unlock(&vm->status_lock);
1489
monk.liucfe2c972015-05-26 15:01:54 +08001490 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001491 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001492
1493 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494}
1495
1496/**
1497 * amdgpu_vm_bo_add - add a bo to a specific vm
1498 *
1499 * @adev: amdgpu_device pointer
1500 * @vm: requested vm
1501 * @bo: amdgpu buffer object
1502 *
Christian König8843dbb2016-01-26 12:17:11 +01001503 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001504 * Add @bo to the list of bos associated with the vm
1505 * Returns newly added bo_va or NULL for failure
1506 *
1507 * Object has to be reserved!
1508 */
1509struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1510 struct amdgpu_vm *vm,
1511 struct amdgpu_bo *bo)
1512{
1513 struct amdgpu_bo_va *bo_va;
1514
1515 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1516 if (bo_va == NULL) {
1517 return NULL;
1518 }
1519 bo_va->vm = vm;
1520 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001521 bo_va->ref_count = 1;
1522 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001523 INIT_LIST_HEAD(&bo_va->valids);
1524 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001526
Christian Königa5f6b5b2017-01-30 11:01:38 +01001527 if (bo)
1528 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001529
1530 return bo_va;
1531}
1532
1533/**
1534 * amdgpu_vm_bo_map - map bo inside a vm
1535 *
1536 * @adev: amdgpu_device pointer
1537 * @bo_va: bo_va to store the address
1538 * @saddr: where to map the BO
1539 * @offset: requested offset in the BO
1540 * @flags: attributes of pages (read/write/valid/etc.)
1541 *
1542 * Add a mapping of the BO at the specefied addr into the VM.
1543 * Returns 0 for success, error for failure.
1544 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001545 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546 */
1547int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1548 struct amdgpu_bo_va *bo_va,
1549 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001550 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551{
1552 struct amdgpu_bo_va_mapping *mapping;
1553 struct amdgpu_vm *vm = bo_va->vm;
1554 struct interval_tree_node *it;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556
Christian König0be52de2015-05-18 14:37:27 +02001557 /* validate the parameters */
1558 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001559 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001560 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001561
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001562 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001563 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001564 if (saddr >= eaddr ||
1565 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001567
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001568 saddr /= AMDGPU_GPU_PAGE_SIZE;
1569 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1570
Felix Kuehling005ae952015-11-23 17:43:48 -05001571 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001572 if (it) {
1573 struct amdgpu_bo_va_mapping *tmp;
1574 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1575 /* bo and tmp overlap, invalid addr */
1576 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1577 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1578 tmp->it.start, tmp->it.last + 1);
Christian König663e4572017-03-13 10:13:37 +01001579 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580 }
1581
1582 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001583 if (!mapping)
1584 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585
1586 INIT_LIST_HEAD(&mapping->list);
1587 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001588 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 mapping->offset = offset;
1590 mapping->flags = flags;
1591
Christian König7fc11952015-07-30 11:53:42 +02001592 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593 interval_tree_insert(&mapping->it, &vm->va);
1594
Christian König4388fc22017-03-13 10:13:36 +01001595 if (flags & AMDGPU_PTE_PRT)
1596 amdgpu_vm_prt_get(adev);
1597
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599}
1600
1601/**
Christian König80f95c52017-03-13 10:13:39 +01001602 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1603 *
1604 * @adev: amdgpu_device pointer
1605 * @bo_va: bo_va to store the address
1606 * @saddr: where to map the BO
1607 * @offset: requested offset in the BO
1608 * @flags: attributes of pages (read/write/valid/etc.)
1609 *
1610 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1611 * mappings as we do so.
1612 * Returns 0 for success, error for failure.
1613 *
1614 * Object has to be reserved and unreserved outside!
1615 */
1616int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1617 struct amdgpu_bo_va *bo_va,
1618 uint64_t saddr, uint64_t offset,
1619 uint64_t size, uint64_t flags)
1620{
1621 struct amdgpu_bo_va_mapping *mapping;
1622 struct amdgpu_vm *vm = bo_va->vm;
1623 uint64_t eaddr;
1624 int r;
1625
1626 /* validate the parameters */
1627 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1628 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1629 return -EINVAL;
1630
1631 /* make sure object fit at this offset */
1632 eaddr = saddr + size - 1;
1633 if (saddr >= eaddr ||
1634 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
1635 return -EINVAL;
1636
1637 /* Allocate all the needed memory */
1638 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1639 if (!mapping)
1640 return -ENOMEM;
1641
1642 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
1643 if (r) {
1644 kfree(mapping);
1645 return r;
1646 }
1647
1648 saddr /= AMDGPU_GPU_PAGE_SIZE;
1649 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1650
1651 mapping->it.start = saddr;
1652 mapping->it.last = eaddr;
1653 mapping->offset = offset;
1654 mapping->flags = flags;
1655
1656 list_add(&mapping->list, &bo_va->invalids);
1657 interval_tree_insert(&mapping->it, &vm->va);
1658
1659 if (flags & AMDGPU_PTE_PRT)
1660 amdgpu_vm_prt_get(adev);
1661
1662 return 0;
1663}
1664
1665/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001666 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1667 *
1668 * @adev: amdgpu_device pointer
1669 * @bo_va: bo_va to remove the address from
1670 * @saddr: where to the BO is mapped
1671 *
1672 * Remove a mapping of the BO at the specefied addr from the VM.
1673 * Returns 0 for success, error for failure.
1674 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001675 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001676 */
1677int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1678 struct amdgpu_bo_va *bo_va,
1679 uint64_t saddr)
1680{
1681 struct amdgpu_bo_va_mapping *mapping;
1682 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001683 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684
Christian König6c7fc502015-06-05 20:56:17 +02001685 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001686
Christian König7fc11952015-07-30 11:53:42 +02001687 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 if (mapping->it.start == saddr)
1689 break;
1690 }
1691
Christian König7fc11952015-07-30 11:53:42 +02001692 if (&mapping->list == &bo_va->valids) {
1693 valid = false;
1694
1695 list_for_each_entry(mapping, &bo_va->invalids, list) {
1696 if (mapping->it.start == saddr)
1697 break;
1698 }
1699
Christian König32b41ac2016-03-08 18:03:27 +01001700 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001701 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001702 }
Christian König32b41ac2016-03-08 18:03:27 +01001703
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001704 list_del(&mapping->list);
1705 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001706 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001707
Christian Könige17841b2016-03-08 17:52:01 +01001708 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001709 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001710 else
Christian König284710f2017-01-30 11:09:31 +01001711 amdgpu_vm_free_mapping(adev, vm, mapping,
1712 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713
1714 return 0;
1715}
1716
1717/**
Christian Königdc54d3d2017-03-13 10:13:38 +01001718 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1719 *
1720 * @adev: amdgpu_device pointer
1721 * @vm: VM structure to use
1722 * @saddr: start of the range
1723 * @size: size of the range
1724 *
1725 * Remove all mappings in a range, split them as appropriate.
1726 * Returns 0 for success, error for failure.
1727 */
1728int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1729 struct amdgpu_vm *vm,
1730 uint64_t saddr, uint64_t size)
1731{
1732 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1733 struct interval_tree_node *it;
1734 LIST_HEAD(removed);
1735 uint64_t eaddr;
1736
1737 eaddr = saddr + size - 1;
1738 saddr /= AMDGPU_GPU_PAGE_SIZE;
1739 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1740
1741 /* Allocate all the needed memory */
1742 before = kzalloc(sizeof(*before), GFP_KERNEL);
1743 if (!before)
1744 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08001745 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001746
1747 after = kzalloc(sizeof(*after), GFP_KERNEL);
1748 if (!after) {
1749 kfree(before);
1750 return -ENOMEM;
1751 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08001752 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001753
1754 /* Now gather all removed mappings */
1755 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
1756 while (it) {
1757 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1758 it = interval_tree_iter_next(it, saddr, eaddr);
1759
1760 /* Remember mapping split at the start */
1761 if (tmp->it.start < saddr) {
Junwei Zhang27f6d612017-03-16 16:09:24 +08001762 before->it.start = tmp->it.start;
Christian Königdc54d3d2017-03-13 10:13:38 +01001763 before->it.last = saddr - 1;
1764 before->offset = tmp->offset;
1765 before->flags = tmp->flags;
1766 list_add(&before->list, &tmp->list);
1767 }
1768
1769 /* Remember mapping split at the end */
1770 if (tmp->it.last > eaddr) {
1771 after->it.start = eaddr + 1;
1772 after->it.last = tmp->it.last;
1773 after->offset = tmp->offset;
1774 after->offset += after->it.start - tmp->it.start;
1775 after->flags = tmp->flags;
1776 list_add(&after->list, &tmp->list);
1777 }
1778
1779 list_del(&tmp->list);
1780 list_add(&tmp->list, &removed);
1781 }
1782
1783 /* And free them up */
1784 list_for_each_entry_safe(tmp, next, &removed, list) {
1785 interval_tree_remove(&tmp->it, &vm->va);
1786 list_del(&tmp->list);
1787
1788 if (tmp->it.start < saddr)
1789 tmp->it.start = saddr;
1790 if (tmp->it.last > eaddr)
1791 tmp->it.last = eaddr;
1792
1793 list_add(&tmp->list, &vm->freed);
1794 trace_amdgpu_vm_bo_unmap(NULL, tmp);
1795 }
1796
Junwei Zhang27f6d612017-03-16 16:09:24 +08001797 /* Insert partial mapping before the range */
1798 if (!list_empty(&before->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001799 interval_tree_insert(&before->it, &vm->va);
1800 if (before->flags & AMDGPU_PTE_PRT)
1801 amdgpu_vm_prt_get(adev);
1802 } else {
1803 kfree(before);
1804 }
1805
1806 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08001807 if (!list_empty(&after->list)) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001808 interval_tree_insert(&after->it, &vm->va);
1809 if (after->flags & AMDGPU_PTE_PRT)
1810 amdgpu_vm_prt_get(adev);
1811 } else {
1812 kfree(after);
1813 }
1814
1815 return 0;
1816}
1817
1818/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1820 *
1821 * @adev: amdgpu_device pointer
1822 * @bo_va: requested bo_va
1823 *
Christian König8843dbb2016-01-26 12:17:11 +01001824 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001825 *
1826 * Object have to be reserved!
1827 */
1828void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1829 struct amdgpu_bo_va *bo_va)
1830{
1831 struct amdgpu_bo_va_mapping *mapping, *next;
1832 struct amdgpu_vm *vm = bo_va->vm;
1833
1834 list_del(&bo_va->bo_list);
1835
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001836 spin_lock(&vm->status_lock);
1837 list_del(&bo_va->vm_status);
1838 spin_unlock(&vm->status_lock);
1839
Christian König7fc11952015-07-30 11:53:42 +02001840 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001841 list_del(&mapping->list);
1842 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001843 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001844 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001845 }
Christian König7fc11952015-07-30 11:53:42 +02001846 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1847 list_del(&mapping->list);
1848 interval_tree_remove(&mapping->it, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01001849 amdgpu_vm_free_mapping(adev, vm, mapping,
1850 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02001851 }
Christian König32b41ac2016-03-08 18:03:27 +01001852
Chris Wilsonf54d1862016-10-25 13:00:45 +01001853 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001854 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855}
1856
1857/**
1858 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1859 *
1860 * @adev: amdgpu_device pointer
1861 * @vm: requested vm
1862 * @bo: amdgpu buffer object
1863 *
Christian König8843dbb2016-01-26 12:17:11 +01001864 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001865 */
1866void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1867 struct amdgpu_bo *bo)
1868{
1869 struct amdgpu_bo_va *bo_va;
1870
1871 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001872 spin_lock(&bo_va->vm->status_lock);
1873 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001874 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001875 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001876 }
1877}
1878
1879/**
1880 * amdgpu_vm_init - initialize a vm instance
1881 *
1882 * @adev: amdgpu_device pointer
1883 * @vm: requested vm
1884 *
Christian König8843dbb2016-01-26 12:17:11 +01001885 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886 */
1887int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1888{
1889 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1890 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001891 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001892 unsigned ring_instance;
1893 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001894 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001895 int i, r;
1896
Christian Königbcb1ba32016-03-08 15:40:11 +01001897 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1898 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001899 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001900 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001901 spin_lock_init(&vm->status_lock);
1902 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001903 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001904 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001905
Christian König72a7ec52016-10-19 11:03:57 +02001906 pd_size = amdgpu_vm_bo_size(adev, 0);
1907 pd_entries = amdgpu_vm_num_entries(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001908
1909 /* allocate page table array */
Christian König67003a12016-10-12 14:46:26 +02001910 vm->root.entries = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
1911 if (vm->root.entries == NULL) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001912 DRM_ERROR("Cannot allocate memory for page table array\n");
1913 return -ENOMEM;
1914 }
1915
Christian König2bd9ccf2016-02-01 12:53:58 +01001916 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001917
1918 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1919 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1920 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001921 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1922 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1923 rq, amdgpu_sched_jobs);
1924 if (r)
Chunming Zhou64827ad2016-07-28 17:20:32 +08001925 goto err;
Christian König2bd9ccf2016-02-01 12:53:58 +01001926
Christian Königa24960f2016-10-12 13:20:52 +02001927 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001928
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001930 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08001931 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
Christian König03f48dd2016-08-15 17:00:22 +02001932 AMDGPU_GEM_CREATE_SHADOW |
Christian König617859e2016-11-17 15:40:02 +01001933 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1934 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Christian König67003a12016-10-12 14:46:26 +02001935 NULL, NULL, &vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001936 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001937 goto error_free_sched_entity;
1938
Christian König67003a12016-10-12 14:46:26 +02001939 r = amdgpu_bo_reserve(vm->root.bo, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001940 if (r)
Christian König67003a12016-10-12 14:46:26 +02001941 goto error_free_root;
Christian König2bd9ccf2016-02-01 12:53:58 +01001942
Christian König5a712a82016-06-21 16:28:15 +02001943 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König67003a12016-10-12 14:46:26 +02001944 amdgpu_bo_unreserve(vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945
1946 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001947
Christian König67003a12016-10-12 14:46:26 +02001948error_free_root:
1949 amdgpu_bo_unref(&vm->root.bo->shadow);
1950 amdgpu_bo_unref(&vm->root.bo);
1951 vm->root.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01001952
1953error_free_sched_entity:
1954 amd_sched_entity_fini(&ring->sched, &vm->entity);
1955
Chunming Zhou64827ad2016-07-28 17:20:32 +08001956err:
Christian König67003a12016-10-12 14:46:26 +02001957 drm_free_large(vm->root.entries);
Chunming Zhou64827ad2016-07-28 17:20:32 +08001958
Christian König2bd9ccf2016-02-01 12:53:58 +01001959 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001960}
1961
1962/**
1963 * amdgpu_vm_fini - tear down a vm instance
1964 *
1965 * @adev: amdgpu_device pointer
1966 * @vm: requested vm
1967 *
Christian König8843dbb2016-01-26 12:17:11 +01001968 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969 * Unbind the VM and remove all bos from the vm bo list
1970 */
1971void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1972{
1973 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01001974 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975 int i;
1976
Christian König2d55e452016-02-08 17:37:38 +01001977 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001978
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001979 if (!RB_EMPTY_ROOT(&vm->va)) {
1980 dev_err(adev->dev, "still active bo inside vm\n");
1981 }
1982 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1983 list_del(&mapping->list);
1984 interval_tree_remove(&mapping->it, &vm->va);
1985 kfree(mapping);
1986 }
1987 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01001988 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01001989 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01001990 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01001991 }
Christian König284710f2017-01-30 11:09:31 +01001992
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001993 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01001994 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001995 }
1996
Christian König72a7ec52016-10-19 11:03:57 +02001997 for (i = 0; i < amdgpu_vm_num_entries(adev, 0); i++) {
Christian König67003a12016-10-12 14:46:26 +02001998 struct amdgpu_bo *pt = vm->root.entries[i].bo;
Christian König2698f622016-09-16 13:06:09 +02001999
2000 if (!pt)
2001 continue;
2002
2003 amdgpu_bo_unref(&pt->shadow);
2004 amdgpu_bo_unref(&pt);
Chunming Zhou1baa4392016-08-04 13:59:32 +08002005 }
Christian König67003a12016-10-12 14:46:26 +02002006 drm_free_large(vm->root.entries);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007
Christian König67003a12016-10-12 14:46:26 +02002008 amdgpu_bo_unref(&vm->root.bo->shadow);
2009 amdgpu_bo_unref(&vm->root.bo);
Christian Königa24960f2016-10-12 13:20:52 +02002010 dma_fence_put(vm->last_dir_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002011}
Christian Königea89f8c2015-11-15 20:52:06 +01002012
2013/**
Christian Königa9a78b32016-01-21 10:19:11 +01002014 * amdgpu_vm_manager_init - init the VM manager
2015 *
2016 * @adev: amdgpu_device pointer
2017 *
2018 * Initialize the VM manager structures
2019 */
2020void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2021{
2022 unsigned i;
2023
2024 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
2025
2026 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01002027 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
2028 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01002029 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01002030 list_add_tail(&adev->vm_manager.ids[i].list,
2031 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01002032 }
Christian König2d55e452016-02-08 17:37:38 +01002033
Chris Wilsonf54d1862016-10-25 13:00:45 +01002034 adev->vm_manager.fence_context =
2035 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002036 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2037 adev->vm_manager.seqno[i] = 0;
2038
Christian König2d55e452016-02-08 17:37:38 +01002039 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002040 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002041 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002042 atomic_set(&adev->vm_manager.num_prt_users, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01002043}
2044
2045/**
Christian Königea89f8c2015-11-15 20:52:06 +01002046 * amdgpu_vm_manager_fini - cleanup VM manager
2047 *
2048 * @adev: amdgpu_device pointer
2049 *
2050 * Cleanup the VM manager and free resources.
2051 */
2052void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2053{
2054 unsigned i;
2055
Christian Königbcb1ba32016-03-08 15:40:11 +01002056 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
2057 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
2058
Chris Wilsonf54d1862016-10-25 13:00:45 +01002059 dma_fence_put(adev->vm_manager.ids[i].first);
Christian König832a9022016-02-15 12:33:02 +01002060 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Chris Wilsonf54d1862016-10-25 13:00:45 +01002061 dma_fence_put(id->flushed_updates);
Dave Airlie7b624ad2016-11-07 09:37:09 +10002062 dma_fence_put(id->last_flush);
Christian Königbcb1ba32016-03-08 15:40:11 +01002063 }
Christian Königea89f8c2015-11-15 20:52:06 +01002064}