blob: 3bd430e180b51438cbe9ac8492cb5879117316a7 [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>
Christian Königa9f87f62017-03-30 14:03:59 +020029#include <linux/interval_tree_generic.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030#include <drm/drmP.h>
31#include <drm/amdgpu_drm.h>
32#include "amdgpu.h"
33#include "amdgpu_trace.h"
34
35/*
36 * GPUVM
37 * GPUVM is similar to the legacy gart on older asics, however
38 * rather than there being a single global gart table
39 * for the entire GPU, there are multiple VM page tables active
40 * at any given time. The VM page tables can contain a mix
41 * vram pages and system memory pages and system memory pages
42 * can be mapped as snooped (cached system pages) or unsnooped
43 * (uncached system pages).
44 * Each VM has an ID associated with it and there is a page table
45 * associated with each VMID. When execting a command buffer,
46 * the kernel tells the the ring what VMID to use for that command
47 * buffer. VMIDs are allocated dynamically as commands are submitted.
48 * The userspace drivers maintain their own address space and the kernel
49 * sets up their pages tables accordingly when they submit their
50 * command buffers and a VMID is assigned.
51 * Cayman/Trinity support up to 8 active VMs at any given time;
52 * SI supports 16.
53 */
54
Christian Königa9f87f62017-03-30 14:03:59 +020055#define START(node) ((node)->start)
56#define LAST(node) ((node)->last)
57
58INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
59 START, LAST, static, amdgpu_vm_it)
60
61#undef START
62#undef LAST
63
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040064/* Local structure. Encapsulate some VM table update parameters to reduce
65 * the number of function parameters
66 */
Christian König29efc4f2016-08-04 14:52:50 +020067struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020068 /* amdgpu device we do this update for */
69 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +020070 /* optional amdgpu_vm we do this update for */
71 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040072 /* address where to copy page table entries from */
73 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040074 /* indirect buffer to fill with commands */
75 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020076 /* Function which actually does the update */
77 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
78 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080079 uint64_t flags);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -040080 /* The next two are used during VM update by CPU
81 * DMA addresses to use for mapping
82 * Kernel pointer of PD/PT BO that needs to be updated
83 */
84 dma_addr_t *pages_addr;
85 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040086};
87
Christian König284710f2017-01-30 11:09:31 +010088/* Helper to disable partial resident texture feature from a fence callback */
89struct amdgpu_prt_cb {
90 struct amdgpu_device *adev;
91 struct dma_fence_cb cb;
92};
93
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094/**
Christian König72a7ec52016-10-19 11:03:57 +020095 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 *
97 * @adev: amdgpu_device pointer
98 *
Christian König72a7ec52016-10-19 11:03:57 +020099 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100 */
Christian König72a7ec52016-10-19 11:03:57 +0200101static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
102 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103{
Christian König72a7ec52016-10-19 11:03:57 +0200104 if (level == 0)
105 /* For the root directory */
106 return adev->vm_manager.max_pfn >>
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800107 (adev->vm_manager.block_size *
108 adev->vm_manager.num_level);
Christian König72a7ec52016-10-19 11:03:57 +0200109 else if (level == adev->vm_manager.num_level)
110 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800111 return AMDGPU_VM_PTE_COUNT(adev);
Christian König72a7ec52016-10-19 11:03:57 +0200112 else
113 /* Everything in between */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800114 return 1 << adev->vm_manager.block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115}
116
117/**
Christian König72a7ec52016-10-19 11:03:57 +0200118 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 *
120 * @adev: amdgpu_device pointer
121 *
Christian König72a7ec52016-10-19 11:03:57 +0200122 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 */
Christian König72a7ec52016-10-19 11:03:57 +0200124static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125{
Christian König72a7ec52016-10-19 11:03:57 +0200126 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127}
128
129/**
Christian König56467eb2015-12-11 15:16:32 +0100130 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 *
132 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100133 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100134 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 *
136 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100137 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 */
Christian König56467eb2015-12-11 15:16:32 +0100139void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
140 struct list_head *validated,
141 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142{
Christian König67003a12016-10-12 14:46:26 +0200143 entry->robj = vm->root.bo;
Christian König56467eb2015-12-11 15:16:32 +0100144 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200145 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100146 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100147 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100148 list_add(&entry->tv.head, validated);
149}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150
Christian König56467eb2015-12-11 15:16:32 +0100151/**
Christian König670fecc2016-10-12 15:36:57 +0200152 * amdgpu_vm_validate_layer - validate a single page table level
153 *
154 * @parent: parent page table level
155 * @validate: callback to do the validation
156 * @param: parameter for the validation callback
157 *
158 * Validate the page table BOs on command submission if neccessary.
159 */
160static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
161 int (*validate)(void *, struct amdgpu_bo *),
Christian Königb6369222017-08-03 11:44:01 -0400162 void *param, bool use_cpu_for_update,
163 struct ttm_bo_global *glob)
Christian König670fecc2016-10-12 15:36:57 +0200164{
165 unsigned i;
166 int r;
167
Christian König0a096fb2017-07-12 10:01:48 +0200168 if (use_cpu_for_update) {
169 r = amdgpu_bo_kmap(parent->bo, NULL);
170 if (r)
171 return r;
172 }
173
Christian König670fecc2016-10-12 15:36:57 +0200174 if (!parent->entries)
175 return 0;
176
177 for (i = 0; i <= parent->last_entry_used; ++i) {
178 struct amdgpu_vm_pt *entry = &parent->entries[i];
179
180 if (!entry->bo)
181 continue;
182
183 r = validate(param, entry->bo);
184 if (r)
185 return r;
186
Christian Königb6369222017-08-03 11:44:01 -0400187 spin_lock(&glob->lru_lock);
188 ttm_bo_move_to_lru_tail(&entry->bo->tbo);
189 if (entry->bo->shadow)
190 ttm_bo_move_to_lru_tail(&entry->bo->shadow->tbo);
191 spin_unlock(&glob->lru_lock);
192
Christian König670fecc2016-10-12 15:36:57 +0200193 /*
194 * Recurse into the sub directory. This is harmless because we
195 * have only a maximum of 5 layers.
196 */
Christian König0a096fb2017-07-12 10:01:48 +0200197 r = amdgpu_vm_validate_level(entry, validate, param,
Christian Königb6369222017-08-03 11:44:01 -0400198 use_cpu_for_update, glob);
Christian König670fecc2016-10-12 15:36:57 +0200199 if (r)
200 return r;
201 }
202
203 return r;
204}
205
206/**
Christian Königf7da30d2016-09-28 12:03:04 +0200207 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100208 *
Christian König5a712a82016-06-21 16:28:15 +0200209 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100210 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200211 * @validate: callback to do the validation
212 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400213 *
Christian Königf7da30d2016-09-28 12:03:04 +0200214 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215 */
Christian Königf7da30d2016-09-28 12:03:04 +0200216int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
217 int (*validate)(void *p, struct amdgpu_bo *bo),
218 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400219{
Christian König5a712a82016-06-21 16:28:15 +0200220 uint64_t num_evictions;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400221
Christian König5a712a82016-06-21 16:28:15 +0200222 /* We only need to validate the page tables
223 * if they aren't already valid.
224 */
225 num_evictions = atomic64_read(&adev->num_evictions);
226 if (num_evictions == vm->last_eviction_counter)
Christian Königf7da30d2016-09-28 12:03:04 +0200227 return 0;
Christian König5a712a82016-06-21 16:28:15 +0200228
Christian König0a096fb2017-07-12 10:01:48 +0200229 return amdgpu_vm_validate_level(&vm->root, validate, param,
Christian Königb6369222017-08-03 11:44:01 -0400230 vm->use_cpu_for_update,
231 adev->mman.bdev.glob);
Christian Königeceb8a12016-01-11 15:35:21 +0100232}
233
234/**
Christian Königf566ceb2016-10-27 20:04:38 +0200235 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
236 *
237 * @adev: amdgpu_device pointer
238 * @vm: requested vm
239 * @saddr: start of the address range
240 * @eaddr: end of the address range
241 *
242 * Make sure the page directories and page tables are allocated
243 */
244static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
245 struct amdgpu_vm *vm,
246 struct amdgpu_vm_pt *parent,
247 uint64_t saddr, uint64_t eaddr,
248 unsigned level)
249{
250 unsigned shift = (adev->vm_manager.num_level - level) *
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800251 adev->vm_manager.block_size;
Christian Königf566ceb2016-10-27 20:04:38 +0200252 unsigned pt_idx, from, to;
253 int r;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400254 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400255 uint64_t init_value = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200256
257 if (!parent->entries) {
258 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
259
Michal Hocko20981052017-05-17 14:23:12 +0200260 parent->entries = kvmalloc_array(num_entries,
261 sizeof(struct amdgpu_vm_pt),
262 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200263 if (!parent->entries)
264 return -ENOMEM;
265 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
266 }
267
Felix Kuehling1866bac2017-03-28 20:36:12 -0400268 from = saddr >> shift;
269 to = eaddr >> shift;
270 if (from >= amdgpu_vm_num_entries(adev, level) ||
271 to >= amdgpu_vm_num_entries(adev, level))
272 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200273
274 if (to > parent->last_entry_used)
275 parent->last_entry_used = to;
276
277 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400278 saddr = saddr & ((1 << shift) - 1);
279 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200280
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400281 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
282 AMDGPU_GEM_CREATE_VRAM_CLEARED;
283 if (vm->use_cpu_for_update)
284 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
285 else
286 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
287 AMDGPU_GEM_CREATE_SHADOW);
288
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400289 if (vm->pte_support_ats) {
290 init_value = AMDGPU_PTE_SYSTEM;
291 if (level != adev->vm_manager.num_level - 1)
292 init_value |= AMDGPU_PDE_PTE;
293 }
294
Christian Königf566ceb2016-10-27 20:04:38 +0200295 /* walk over the address space and allocate the page tables */
296 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
297 struct reservation_object *resv = vm->root.bo->tbo.resv;
298 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
299 struct amdgpu_bo *pt;
300
301 if (!entry->bo) {
302 r = amdgpu_bo_create(adev,
303 amdgpu_vm_bo_size(adev, level),
304 AMDGPU_GPU_PAGE_SIZE, true,
305 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400306 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400307 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200308 if (r)
309 return r;
310
Christian König0a096fb2017-07-12 10:01:48 +0200311 if (vm->use_cpu_for_update) {
312 r = amdgpu_bo_kmap(pt, NULL);
313 if (r) {
314 amdgpu_bo_unref(&pt);
315 return r;
316 }
317 }
318
Christian Königf566ceb2016-10-27 20:04:38 +0200319 /* Keep a reference to the root directory to avoid
320 * freeing them up in the wrong order.
321 */
322 pt->parent = amdgpu_bo_ref(vm->root.bo);
323
324 entry->bo = pt;
325 entry->addr = 0;
326 }
327
328 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400329 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
330 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
331 ((1 << shift) - 1);
332 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
333 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200334 if (r)
335 return r;
336 }
337 }
338
339 return 0;
340}
341
Christian König663e4572017-03-13 10:13:37 +0100342/**
343 * amdgpu_vm_alloc_pts - Allocate page tables.
344 *
345 * @adev: amdgpu_device pointer
346 * @vm: VM to allocate page tables for
347 * @saddr: Start address which needs to be allocated
348 * @size: Size from start address we need.
349 *
350 * Make sure the page tables are allocated.
351 */
352int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
353 struct amdgpu_vm *vm,
354 uint64_t saddr, uint64_t size)
355{
Felix Kuehling22770e52017-03-28 20:24:53 -0400356 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100357 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100358
359 /* validate the parameters */
360 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
361 return -EINVAL;
362
363 eaddr = saddr + size - 1;
364 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
365 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400366 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100367 last_pfn, adev->vm_manager.max_pfn);
368 return -EINVAL;
369 }
370
371 saddr /= AMDGPU_GPU_PAGE_SIZE;
372 eaddr /= AMDGPU_GPU_PAGE_SIZE;
373
Christian Königf566ceb2016-10-27 20:04:38 +0200374 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100375}
376
Christian König641e9402017-04-03 13:59:25 +0200377/**
378 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
379 *
380 * @adev: amdgpu_device pointer
381 * @id: VMID structure
382 *
383 * Check if GPU reset occured since last use of the VMID.
384 */
385static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
386 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800387{
388 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200389 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800390}
391
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800392static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
393{
394 return !!vm->reserved_vmid[vmhub];
395}
396
397/* idr_mgr->lock must be held */
398static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
399 struct amdgpu_ring *ring,
400 struct amdgpu_sync *sync,
401 struct dma_fence *fence,
402 struct amdgpu_job *job)
403{
404 struct amdgpu_device *adev = ring->adev;
405 unsigned vmhub = ring->funcs->vmhub;
406 uint64_t fence_context = adev->fence_context + ring->idx;
407 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
408 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
409 struct dma_fence *updates = sync->last_vm_update;
410 int r = 0;
411 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200412 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800413
414 flushed = id->flushed_updates;
415 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
416 (atomic64_read(&id->owner) != vm->client_id) ||
417 (job->vm_pd_addr != id->pd_gpu_addr) ||
418 (updates && (!flushed || updates->context != flushed->context ||
419 dma_fence_is_later(updates, flushed))) ||
420 (!id->last_flush || (id->last_flush->context != fence_context &&
421 !dma_fence_is_signaled(id->last_flush)))) {
422 needs_flush = true;
423 /* to prevent one context starved by another context */
424 id->pd_gpu_addr = 0;
425 tmp = amdgpu_sync_peek_fence(&id->active, ring);
426 if (tmp) {
427 r = amdgpu_sync_fence(adev, sync, tmp);
428 return r;
429 }
430 }
431
432 /* Good we can use this VMID. Remember this submission as
433 * user of the VMID.
434 */
435 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
436 if (r)
437 goto out;
438
439 if (updates && (!flushed || updates->context != flushed->context ||
440 dma_fence_is_later(updates, flushed))) {
441 dma_fence_put(id->flushed_updates);
442 id->flushed_updates = dma_fence_get(updates);
443 }
444 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800445 atomic64_set(&id->owner, vm->client_id);
446 job->vm_needs_flush = needs_flush;
447 if (needs_flush) {
448 dma_fence_put(id->last_flush);
449 id->last_flush = NULL;
450 }
451 job->vm_id = id - id_mgr->ids;
452 trace_amdgpu_vm_grab_id(vm, ring, job);
453out:
454 return r;
455}
456
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457/**
458 * amdgpu_vm_grab_id - allocate the next free VMID
459 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200461 * @ring: ring we want to submit job to
462 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100463 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 *
Christian König7f8a5292015-07-20 16:09:40 +0200465 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466 */
Christian König7f8a5292015-07-20 16:09:40 +0200467int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100468 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800469 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200472 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200473 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200474 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100475 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200476 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100477 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200478 unsigned i;
479 int r = 0;
480
Christian König76456702017-04-06 17:52:39 +0200481 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800482 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
483 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
484 mutex_unlock(&id_mgr->lock);
485 return r;
486 }
487 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
488 if (!fences) {
489 mutex_unlock(&id_mgr->lock);
490 return -ENOMEM;
491 }
Christian König36fd7c52016-05-23 15:30:08 +0200492 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200493 i = 0;
Christian König76456702017-04-06 17:52:39 +0200494 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200495 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
496 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200497 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200498 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200499 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100500
Christian König1fbb2e92016-06-01 10:47:36 +0200501 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200502 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200503 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
504 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100505 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200506 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200507
Christian König1fbb2e92016-06-01 10:47:36 +0200508 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100509 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200510
Chris Wilsonf54d1862016-10-25 13:00:45 +0100511 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200512 seqno, true);
513 if (!array) {
514 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100515 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200516 kfree(fences);
517 r = -ENOMEM;
518 goto error;
519 }
Christian König8d76001e2016-05-23 16:00:32 +0200520
Christian König8d76001e2016-05-23 16:00:32 +0200521
Christian König1fbb2e92016-06-01 10:47:36 +0200522 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100523 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200524 if (r)
525 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200526
Christian König76456702017-04-06 17:52:39 +0200527 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200528 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200529
Christian König1fbb2e92016-06-01 10:47:36 +0200530 }
531 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200532
Christian König6f1ceab2017-07-11 16:59:21 +0200533 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200534 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200535 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100536 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200537 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200538
Christian König1fbb2e92016-06-01 10:47:36 +0200539 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200540 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800541 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200542
543 if (atomic64_read(&id->owner) != vm->client_id)
544 continue;
545
Chunming Zhoufd53be32016-07-01 17:59:01 +0800546 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200547 continue;
548
Christian König87c910d2017-03-30 16:56:20 +0200549 if (!id->last_flush ||
550 (id->last_flush->context != fence_context &&
551 !dma_fence_is_signaled(id->last_flush)))
552 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200553
554 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200555 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
556 needs_flush = true;
557
558 /* Concurrent flushes are only possible starting with Vega10 */
559 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200560 continue;
561
Christian König3dab83b2016-06-01 13:31:17 +0200562 /* Good we can use this VMID. Remember this submission as
563 * user of the VMID.
564 */
Christian König1fbb2e92016-06-01 10:47:36 +0200565 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
566 if (r)
567 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200568
Christian König87c910d2017-03-30 16:56:20 +0200569 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
570 dma_fence_put(id->flushed_updates);
571 id->flushed_updates = dma_fence_get(updates);
572 }
Christian König8d76001e2016-05-23 16:00:32 +0200573
Christian König87c910d2017-03-30 16:56:20 +0200574 if (needs_flush)
575 goto needs_flush;
576 else
577 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200578
Christian König4f618e72017-04-06 15:18:21 +0200579 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800580
Christian König1fbb2e92016-06-01 10:47:36 +0200581 /* Still no ID to use? Then use the idle one found earlier */
582 id = idle;
583
584 /* Remember this submission as user of the VMID */
585 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100586 if (r)
587 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100588
Christian König87c910d2017-03-30 16:56:20 +0200589 id->pd_gpu_addr = job->vm_pd_addr;
590 dma_fence_put(id->flushed_updates);
591 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200592 atomic64_set(&id->owner, vm->client_id);
593
594needs_flush:
595 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100596 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100597 id->last_flush = NULL;
598
Christian König87c910d2017-03-30 16:56:20 +0200599no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200600 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601
Christian König76456702017-04-06 17:52:39 +0200602 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200603 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100604
605error:
Christian König76456702017-04-06 17:52:39 +0200606 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100607 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608}
609
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800610static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
611 struct amdgpu_vm *vm,
612 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400613{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800614 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400615
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800616 mutex_lock(&id_mgr->lock);
617 if (vm->reserved_vmid[vmhub]) {
618 list_add(&vm->reserved_vmid[vmhub]->list,
619 &id_mgr->ids_lru);
620 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800621 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400622 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800623 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400624}
625
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800626static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
627 struct amdgpu_vm *vm,
628 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500629{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800630 struct amdgpu_vm_id_manager *id_mgr;
631 struct amdgpu_vm_id *idle;
632 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500633
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800634 id_mgr = &adev->vm_manager.id_mgr[vmhub];
635 mutex_lock(&id_mgr->lock);
636 if (vm->reserved_vmid[vmhub])
637 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800638 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
639 AMDGPU_VM_MAX_RESERVED_VMID) {
640 DRM_ERROR("Over limitation of reserved vmid\n");
641 atomic_dec(&id_mgr->reserved_vmid_num);
642 r = -EINVAL;
643 goto unlock;
644 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800645 /* Select the first entry VMID */
646 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
647 list_del_init(&idle->list);
648 vm->reserved_vmid[vmhub] = idle;
649 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500650
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800651 return 0;
652unlock:
653 mutex_unlock(&id_mgr->lock);
654 return r;
655}
656
Alex Xiee59c0202017-06-01 09:42:59 -0400657/**
658 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
659 *
660 * @adev: amdgpu_device pointer
661 */
662void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
663{
664 const struct amdgpu_ip_block *ip_block;
665 bool has_compute_vm_bug;
666 struct amdgpu_ring *ring;
667 int i;
668
669 has_compute_vm_bug = false;
670
671 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
672 if (ip_block) {
673 /* Compute has a VM bug for GFX version < 7.
674 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
675 if (ip_block->version->major <= 7)
676 has_compute_vm_bug = true;
677 else if (ip_block->version->major == 8)
678 if (adev->gfx.mec_fw_version < 673)
679 has_compute_vm_bug = true;
680 }
681
682 for (i = 0; i < adev->num_rings; i++) {
683 ring = adev->rings[i];
684 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
685 /* only compute rings */
686 ring->has_compute_vm_bug = has_compute_vm_bug;
687 else
688 ring->has_compute_vm_bug = false;
689 }
690}
691
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400692bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
693 struct amdgpu_job *job)
694{
695 struct amdgpu_device *adev = ring->adev;
696 unsigned vmhub = ring->funcs->vmhub;
697 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
698 struct amdgpu_vm_id *id;
699 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400700 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400701
702 if (job->vm_id == 0)
703 return false;
704 id = &id_mgr->ids[job->vm_id];
705 gds_switch_needed = ring->funcs->emit_gds_switch && (
706 id->gds_base != job->gds_base ||
707 id->gds_size != job->gds_size ||
708 id->gws_base != job->gws_base ||
709 id->gws_size != job->gws_size ||
710 id->oa_base != job->oa_base ||
711 id->oa_size != job->oa_size);
712
713 if (amdgpu_vm_had_gpu_reset(adev, id))
714 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400715
716 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400717}
718
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400719static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
720{
721 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500722}
723
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724/**
725 * amdgpu_vm_flush - hardware flush the vm
726 *
727 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100728 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100729 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 *
Christian König4ff37a82016-02-26 16:18:26 +0100731 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800733int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734{
Christian König971fe9a92016-03-01 15:09:25 +0100735 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200736 unsigned vmhub = ring->funcs->vmhub;
737 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
738 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100739 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800740 id->gds_base != job->gds_base ||
741 id->gds_size != job->gds_size ||
742 id->gws_base != job->gws_base ||
743 id->gws_size != job->gws_size ||
744 id->oa_base != job->oa_base ||
745 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800746 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200747 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100748 int r;
Christian Königd564a062016-03-01 15:51:53 +0100749
Christian Königf7d015b2017-04-03 14:28:26 +0200750 if (amdgpu_vm_had_gpu_reset(adev, id)) {
751 gds_switch_needed = true;
752 vm_flush_needed = true;
753 }
Christian König971fe9a92016-03-01 15:09:25 +0100754
Monk Liu8fdf0742017-06-06 17:25:13 +0800755 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200756 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100757
Christian Königc0e51932017-04-03 14:16:07 +0200758 if (ring->funcs->init_cond_exec)
759 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100760
Monk Liu8fdf0742017-06-06 17:25:13 +0800761 if (need_pipe_sync)
762 amdgpu_ring_emit_pipeline_sync(ring);
763
Christian Königf7d015b2017-04-03 14:28:26 +0200764 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200765 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800766
Christian König9a94f5a2017-05-12 14:46:23 +0200767 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
768 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800769
Christian Königc0e51932017-04-03 14:16:07 +0200770 r = amdgpu_fence_emit(ring, &fence);
771 if (r)
772 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800773
Christian König76456702017-04-06 17:52:39 +0200774 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200775 dma_fence_put(id->last_flush);
776 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800777 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200778 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200779 }
Monk Liue9d672b2017-03-15 12:18:57 +0800780
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800781 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200782 id->gds_base = job->gds_base;
783 id->gds_size = job->gds_size;
784 id->gws_base = job->gws_base;
785 id->gws_size = job->gws_size;
786 id->oa_base = job->oa_base;
787 id->oa_size = job->oa_size;
788 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
789 job->gds_size, job->gws_base,
790 job->gws_size, job->oa_base,
791 job->oa_size);
792 }
793
794 if (ring->funcs->patch_cond_exec)
795 amdgpu_ring_patch_cond_exec(ring, patch_offset);
796
797 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
798 if (ring->funcs->emit_switch_buffer) {
799 amdgpu_ring_emit_switch_buffer(ring);
800 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801 }
Christian König41d9eb22016-03-01 16:46:18 +0100802 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100803}
804
805/**
806 * amdgpu_vm_reset_id - reset VMID to zero
807 *
808 * @adev: amdgpu device structure
809 * @vm_id: vmid number to use
810 *
811 * Reset saved GDW, GWS and OA to force switch on next flush.
812 */
Christian König76456702017-04-06 17:52:39 +0200813void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
814 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100815{
Christian König76456702017-04-06 17:52:39 +0200816 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
817 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100818
Christian Königb3c85a02017-05-10 20:06:58 +0200819 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100820 id->gds_base = 0;
821 id->gds_size = 0;
822 id->gws_base = 0;
823 id->gws_size = 0;
824 id->oa_base = 0;
825 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400826}
827
828/**
Christian Königb3c85a02017-05-10 20:06:58 +0200829 * amdgpu_vm_reset_all_id - reset VMID to zero
830 *
831 * @adev: amdgpu device structure
832 *
833 * Reset VMID to force flush on next use
834 */
835void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
836{
837 unsigned i, j;
838
839 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
840 struct amdgpu_vm_id_manager *id_mgr =
841 &adev->vm_manager.id_mgr[i];
842
843 for (j = 1; j < id_mgr->num_ids; ++j)
844 amdgpu_vm_reset_id(adev, i, j);
845 }
846}
847
848/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400849 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
850 *
851 * @vm: requested vm
852 * @bo: requested buffer object
853 *
Christian König8843dbb2016-01-26 12:17:11 +0100854 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400855 * Search inside the @bos vm list for the requested vm
856 * Returns the found bo_va or NULL if none is found
857 *
858 * Object has to be reserved!
859 */
860struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
861 struct amdgpu_bo *bo)
862{
863 struct amdgpu_bo_va *bo_va;
864
Christian Königec681542017-08-01 10:51:43 +0200865 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
866 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400867 return bo_va;
868 }
869 }
870 return NULL;
871}
872
873/**
Christian Königafef8b82016-08-12 13:29:18 +0200874 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 *
Christian König29efc4f2016-08-04 14:52:50 +0200876 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877 * @pe: addr of the page entry
878 * @addr: dst addr to write into pe
879 * @count: number of page entries to update
880 * @incr: increase next addr by incr bytes
881 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400882 *
883 * Traces the parameters and calls the right asic functions
884 * to setup the page table using the DMA.
885 */
Christian Königafef8b82016-08-12 13:29:18 +0200886static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
887 uint64_t pe, uint64_t addr,
888 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800889 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400890{
Christian Königec2f05f2016-09-25 16:11:52 +0200891 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892
Christian Königafef8b82016-08-12 13:29:18 +0200893 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200894 amdgpu_vm_write_pte(params->adev, params->ib, pe,
895 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400896
897 } else {
Christian König27c5f362016-08-04 15:02:49 +0200898 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899 count, incr, flags);
900 }
901}
902
903/**
Christian Königafef8b82016-08-12 13:29:18 +0200904 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
905 *
906 * @params: see amdgpu_pte_update_params definition
907 * @pe: addr of the page entry
908 * @addr: dst addr to write into pe
909 * @count: number of page entries to update
910 * @incr: increase next addr by incr bytes
911 * @flags: hw access flags
912 *
913 * Traces the parameters and calls the DMA function to copy the PTEs.
914 */
915static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
916 uint64_t pe, uint64_t addr,
917 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800918 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200919{
Christian Königec2f05f2016-09-25 16:11:52 +0200920 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200921
Christian Königec2f05f2016-09-25 16:11:52 +0200922
923 trace_amdgpu_vm_copy_ptes(pe, src, count);
924
925 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200926}
927
928/**
Christian Königb07c9d22015-11-30 13:26:07 +0100929 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400930 *
Christian Königb07c9d22015-11-30 13:26:07 +0100931 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400932 * @addr: the unmapped addr
933 *
934 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100935 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200937static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938{
939 uint64_t result;
940
Christian Königde9ea7b2016-08-12 11:33:30 +0200941 /* page table offset */
942 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943
Christian Königde9ea7b2016-08-12 11:33:30 +0200944 /* in case cpu page size != gpu page size*/
945 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100946
947 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948
949 return result;
950}
951
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400952/**
953 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
954 *
955 * @params: see amdgpu_pte_update_params definition
956 * @pe: kmap addr of the page entry
957 * @addr: dst addr to write into pe
958 * @count: number of page entries to update
959 * @incr: increase next addr by incr bytes
960 * @flags: hw access flags
961 *
962 * Write count number of PT/PD entries directly.
963 */
964static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
965 uint64_t pe, uint64_t addr,
966 unsigned count, uint32_t incr,
967 uint64_t flags)
968{
969 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400970 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400971
Christian König03918b32017-07-11 17:15:37 +0200972 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
973
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400974 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400975 value = params->pages_addr ?
976 amdgpu_vm_map_gart(params->pages_addr, addr) :
977 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -0400978 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400979 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400980 addr += incr;
981 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400982}
983
Christian Königa33cab72017-07-11 17:13:00 +0200984static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
985 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400986{
987 struct amdgpu_sync sync;
988 int r;
989
990 amdgpu_sync_create(&sync);
Christian Königa33cab72017-07-11 17:13:00 +0200991 amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.resv, owner);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400992 r = amdgpu_sync_wait(&sync, true);
993 amdgpu_sync_free(&sync);
994
995 return r;
996}
997
Christian Königf8991ba2016-09-16 15:36:49 +0200998/*
Christian König194d2162016-10-12 15:13:52 +0200999 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +02001000 *
1001 * @adev: amdgpu_device pointer
1002 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +02001003 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +02001004 *
Christian König194d2162016-10-12 15:13:52 +02001005 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +02001006 * Returns 0 for success, error for failure.
1007 */
Christian König194d2162016-10-12 15:13:52 +02001008static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1009 struct amdgpu_vm *vm,
1010 struct amdgpu_vm_pt *parent,
1011 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001012{
Christian Königf8991ba2016-09-16 15:36:49 +02001013 struct amdgpu_bo *shadow;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001014 struct amdgpu_ring *ring = NULL;
1015 uint64_t pd_addr, shadow_addr = 0;
Christian König194d2162016-10-12 15:13:52 +02001016 uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
Christian Königf8991ba2016-09-16 15:36:49 +02001017 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001018 unsigned count = 0, pt_idx, ndw = 0;
Christian Königd71518b2016-02-01 12:20:25 +01001019 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001020 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +10001021 struct dma_fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001022
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023 int r;
1024
Christian König194d2162016-10-12 15:13:52 +02001025 if (!parent->entries)
1026 return 0;
Christian Königd71518b2016-02-01 12:20:25 +01001027
Christian König27c5f362016-08-04 15:02:49 +02001028 memset(&params, 0, sizeof(params));
1029 params.adev = adev;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001030 shadow = parent->bo->shadow;
1031
Alex Deucher69277982017-07-13 15:37:11 -04001032 if (vm->use_cpu_for_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001033 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->bo);
Christian Königa33cab72017-07-11 17:13:00 +02001034 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001035 if (unlikely(r))
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001036 return r;
Christian König0a096fb2017-07-12 10:01:48 +02001037
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001038 params.func = amdgpu_vm_cpu_set_ptes;
1039 } else {
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001040 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1041 sched);
1042
1043 /* padding, etc. */
1044 ndw = 64;
1045
1046 /* assume the worst case */
1047 ndw += parent->last_entry_used * 6;
1048
1049 pd_addr = amdgpu_bo_gpu_offset(parent->bo);
1050
1051 if (shadow) {
1052 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1053 ndw *= 2;
1054 } else {
1055 shadow_addr = 0;
1056 }
1057
1058 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1059 if (r)
1060 return r;
1061
1062 params.ib = &job->ibs[0];
1063 params.func = amdgpu_vm_do_set_ptes;
1064 }
1065
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001066
Christian König194d2162016-10-12 15:13:52 +02001067 /* walk over the address space and update the directory */
1068 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1069 struct amdgpu_bo *bo = parent->entries[pt_idx].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001070 uint64_t pde, pt;
1071
1072 if (bo == NULL)
1073 continue;
1074
1075 pt = amdgpu_bo_gpu_offset(bo);
Christian König53e2e912017-05-15 15:19:10 +02001076 pt = amdgpu_gart_get_vm_pde(adev, pt);
Christian König4ab40162017-08-03 20:30:50 +02001077 /* Don't update huge pages here */
1078 if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
1079 parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
Christian Königf8991ba2016-09-16 15:36:49 +02001080 continue;
1081
Christian König4ab40162017-08-03 20:30:50 +02001082 parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083
1084 pde = pd_addr + pt_idx * 8;
1085 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001086 ((last_pt + incr * count) != pt) ||
1087 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001088
1089 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +02001090 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001091 params.func(&params,
1092 last_shadow,
1093 last_pt, count,
1094 incr,
1095 AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001096
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001097 params.func(&params, last_pde,
1098 last_pt, count, incr,
1099 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 }
1101
1102 count = 1;
1103 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001104 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001105 last_pt = pt;
1106 } else {
1107 ++count;
1108 }
1109 }
1110
Christian Königf8991ba2016-09-16 15:36:49 +02001111 if (count) {
Christian König67003a12016-10-12 14:46:26 +02001112 if (vm->root.bo->shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001113 params.func(&params, last_shadow, last_pt,
1114 count, incr, AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001115
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001116 params.func(&params, last_pde, last_pt,
1117 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001118 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001119
Christian König0a096fb2017-07-12 10:01:48 +02001120 if (!vm->use_cpu_for_update) {
1121 if (params.ib->length_dw == 0) {
1122 amdgpu_job_free(job);
1123 } else {
1124 amdgpu_ring_pad_ib(ring, params.ib);
1125 amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv,
Christian König194d2162016-10-12 15:13:52 +02001126 AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001127 if (shadow)
1128 amdgpu_sync_resv(adev, &job->sync,
1129 shadow->tbo.resv,
1130 AMDGPU_FENCE_OWNER_VM);
Christian Königf8991ba2016-09-16 15:36:49 +02001131
Christian König0a096fb2017-07-12 10:01:48 +02001132 WARN_ON(params.ib->length_dw > ndw);
1133 r = amdgpu_job_submit(job, ring, &vm->entity,
1134 AMDGPU_FENCE_OWNER_VM, &fence);
1135 if (r)
1136 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001137
Christian König0a096fb2017-07-12 10:01:48 +02001138 amdgpu_bo_fence(parent->bo, fence, true);
1139 dma_fence_put(vm->last_dir_update);
1140 vm->last_dir_update = dma_fence_get(fence);
1141 dma_fence_put(fence);
1142 }
Christian König194d2162016-10-12 15:13:52 +02001143 }
1144 /*
1145 * Recurse into the subdirectories. This recursion is harmless because
1146 * we only have a maximum of 5 layers.
1147 */
1148 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1149 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1150
1151 if (!entry->bo)
1152 continue;
1153
1154 r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
1155 if (r)
1156 return r;
1157 }
Christian Königf8991ba2016-09-16 15:36:49 +02001158
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001160
1161error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001162 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001163 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001164}
1165
Christian König194d2162016-10-12 15:13:52 +02001166/*
Christian König92456b92017-05-12 16:09:26 +02001167 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1168 *
1169 * @parent: parent PD
1170 *
1171 * Mark all PD level as invalid after an error.
1172 */
1173static void amdgpu_vm_invalidate_level(struct amdgpu_vm_pt *parent)
1174{
1175 unsigned pt_idx;
1176
1177 /*
1178 * Recurse into the subdirectories. This recursion is harmless because
1179 * we only have a maximum of 5 layers.
1180 */
1181 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1182 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1183
1184 if (!entry->bo)
1185 continue;
1186
1187 entry->addr = ~0ULL;
1188 amdgpu_vm_invalidate_level(entry);
1189 }
1190}
1191
1192/*
Christian König194d2162016-10-12 15:13:52 +02001193 * amdgpu_vm_update_directories - make sure that all directories are valid
1194 *
1195 * @adev: amdgpu_device pointer
1196 * @vm: requested vm
1197 *
1198 * Makes sure all directories are up to date.
1199 * Returns 0 for success, error for failure.
1200 */
1201int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1202 struct amdgpu_vm *vm)
1203{
Christian König92456b92017-05-12 16:09:26 +02001204 int r;
1205
1206 r = amdgpu_vm_update_level(adev, vm, &vm->root, 0);
1207 if (r)
1208 amdgpu_vm_invalidate_level(&vm->root);
1209
Christian König68c62302017-07-11 17:23:29 +02001210 if (vm->use_cpu_for_update) {
1211 /* Flush HDP */
1212 mb();
1213 amdgpu_gart_flush_gpu_tlb(adev, 0);
1214 }
1215
Christian König92456b92017-05-12 16:09:26 +02001216 return r;
Christian König194d2162016-10-12 15:13:52 +02001217}
1218
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001220 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001221 *
1222 * @p: see amdgpu_pte_update_params definition
1223 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001224 * @entry: resulting entry or NULL
1225 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001226 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001227 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001228 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001229void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1230 struct amdgpu_vm_pt **entry,
1231 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001232{
Christian König4e2cb642016-10-25 15:52:28 +02001233 unsigned idx, level = p->adev->vm_manager.num_level;
1234
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001235 *parent = NULL;
1236 *entry = &p->vm->root;
1237 while ((*entry)->entries) {
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001238 idx = addr >> (p->adev->vm_manager.block_size * level--);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001239 idx %= amdgpu_bo_size((*entry)->bo) / 8;
1240 *parent = *entry;
1241 *entry = &(*entry)->entries[idx];
Christian König4e2cb642016-10-25 15:52:28 +02001242 }
1243
1244 if (level)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001245 *entry = NULL;
1246}
Christian König4e2cb642016-10-25 15:52:28 +02001247
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001248/**
1249 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1250 *
1251 * @p: see amdgpu_pte_update_params definition
1252 * @entry: vm_pt entry to check
1253 * @parent: parent entry
1254 * @nptes: number of PTEs updated with this operation
1255 * @dst: destination address where the PTEs should point to
1256 * @flags: access flags fro the PTEs
1257 *
1258 * Check if we can update the PD with a huge page.
1259 */
Christian Königec5207c2017-08-03 19:24:06 +02001260static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1261 struct amdgpu_vm_pt *entry,
1262 struct amdgpu_vm_pt *parent,
1263 unsigned nptes, uint64_t dst,
1264 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001265{
1266 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1267 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001268
1269 /* In the case of a mixed PT the PDE must point to it*/
1270 if (p->adev->asic_type < CHIP_VEGA10 ||
1271 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehling38a87912017-08-17 16:37:49 -04001272 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001273 !(flags & AMDGPU_PTE_VALID)) {
1274
1275 dst = amdgpu_bo_gpu_offset(entry->bo);
1276 dst = amdgpu_gart_get_vm_pde(p->adev, dst);
1277 flags = AMDGPU_PTE_VALID;
1278 } else {
Christian König4ab40162017-08-03 20:30:50 +02001279 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001280 flags |= AMDGPU_PDE_PTE;
1281 }
1282
Christian König4ab40162017-08-03 20:30:50 +02001283 if (entry->addr == (dst | flags))
Christian Königec5207c2017-08-03 19:24:06 +02001284 return;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001285
Christian König4ab40162017-08-03 20:30:50 +02001286 entry->addr = (dst | flags);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001287
1288 if (use_cpu_update) {
Felix Kuehling38a87912017-08-17 16:37:49 -04001289 /* In case a huge page is replaced with a system
1290 * memory mapping, p->pages_addr != NULL and
1291 * amdgpu_vm_cpu_set_ptes would try to translate dst
1292 * through amdgpu_vm_map_gart. But dst is already a
1293 * GPU address (of the page table). Disable
1294 * amdgpu_vm_map_gart temporarily.
1295 */
1296 dma_addr_t *tmp;
1297
1298 tmp = p->pages_addr;
1299 p->pages_addr = NULL;
1300
Christian Königec5207c2017-08-03 19:24:06 +02001301 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001302 pde = pd_addr + (entry - parent->entries) * 8;
1303 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehling38a87912017-08-17 16:37:49 -04001304
1305 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001306 } else {
1307 if (parent->bo->shadow) {
1308 pd_addr = amdgpu_bo_gpu_offset(parent->bo->shadow);
1309 pde = pd_addr + (entry - parent->entries) * 8;
1310 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1311 }
1312 pd_addr = amdgpu_bo_gpu_offset(parent->bo);
1313 pde = pd_addr + (entry - parent->entries) * 8;
1314 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1315 }
Christian König4e2cb642016-10-25 15:52:28 +02001316}
1317
1318/**
Christian König92696dd2016-08-05 13:56:35 +02001319 * amdgpu_vm_update_ptes - make sure that page tables are valid
1320 *
1321 * @params: see amdgpu_pte_update_params definition
1322 * @vm: requested vm
1323 * @start: start of GPU address range
1324 * @end: end of GPU address range
1325 * @dst: destination address to map to, the next dst inside the function
1326 * @flags: mapping flags
1327 *
1328 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001329 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001330 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001331static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001332 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001333 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001334{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001335 struct amdgpu_device *adev = params->adev;
1336 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001337
Christian König301654a2017-05-16 14:30:27 +02001338 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001339 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001340 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001341 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001342
1343 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001344 for (addr = start; addr < end; addr += nptes,
1345 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1346 struct amdgpu_vm_pt *entry, *parent;
1347
1348 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1349 if (!entry)
1350 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001351
Christian König92696dd2016-08-05 13:56:35 +02001352 if ((addr & ~mask) == (end & ~mask))
1353 nptes = end - addr;
1354 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001355 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001356
Christian Königec5207c2017-08-03 19:24:06 +02001357 amdgpu_vm_handle_huge_pages(params, entry, parent,
1358 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001359 /* We don't need to update PTEs for huge pages */
1360 if (entry->addr & AMDGPU_PDE_PTE)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001361 continue;
1362
1363 pt = entry->bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001364 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001365 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001366 } else {
1367 if (pt->shadow) {
1368 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1369 pe_start += (addr & mask) * 8;
1370 params->func(params, pe_start, dst, nptes,
1371 AMDGPU_GPU_PAGE_SIZE, flags);
1372 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001373 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001374 }
Christian König92696dd2016-08-05 13:56:35 +02001375
Christian König301654a2017-05-16 14:30:27 +02001376 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001377 params->func(params, pe_start, dst, nptes,
1378 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001379 }
1380
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001381 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001382}
1383
1384/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1386 *
Christian König29efc4f2016-08-04 14:52:50 +02001387 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001388 * @vm: requested vm
1389 * @start: first PTE to handle
1390 * @end: last PTE to handle
1391 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001393 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001394 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001395static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001396 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001397 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398{
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001399 int r;
1400
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001401 /**
1402 * The MC L1 TLB supports variable sized pages, based on a fragment
1403 * field in the PTE. When this field is set to a non-zero value, page
1404 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1405 * flags are considered valid for all PTEs within the fragment range
1406 * and corresponding mappings are assumed to be physically contiguous.
1407 *
1408 * The L1 TLB can store a single PTE for the whole fragment,
1409 * significantly increasing the space available for translation
1410 * caching. This leads to large improvements in throughput when the
1411 * TLB is under pressure.
1412 *
1413 * The L2 TLB distributes small and large fragments into two
1414 * asymmetric partitions. The large fragment cache is significantly
1415 * larger. Thus, we try to use large fragments wherever possible.
1416 * Userspace can support this by aligning virtual base address and
1417 * allocation size to the fragment size.
1418 */
Roger Hee618d302017-08-11 20:00:41 +08001419 unsigned pages_per_frag = params->adev->vm_manager.fragment_size;
Christian König6be7adb2017-05-23 18:35:22 +02001420 uint64_t frag_flags = AMDGPU_PTE_FRAG(pages_per_frag);
1421 uint64_t frag_align = 1 << pages_per_frag;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422
Christian König92696dd2016-08-05 13:56:35 +02001423 uint64_t frag_start = ALIGN(start, frag_align);
1424 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +01001425
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001426 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +02001427 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001428 (frag_start >= frag_end))
1429 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001430
1431 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +02001432 if (start != frag_start) {
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001433 r = amdgpu_vm_update_ptes(params, start, frag_start,
1434 dst, flags);
1435 if (r)
1436 return r;
Christian König92696dd2016-08-05 13:56:35 +02001437 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001438 }
1439
1440 /* handle the area in the middle */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001441 r = amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
1442 flags | frag_flags);
1443 if (r)
1444 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001445
1446 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +02001447 if (frag_end != end) {
1448 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001449 r = amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001450 }
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001451 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001452}
1453
1454/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001455 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1456 *
1457 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001458 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001459 * @src: address where to copy page table entries from
1460 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001461 * @vm: requested vm
1462 * @start: start of mapped range
1463 * @last: last mapped entry
1464 * @flags: flags for the entries
1465 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001466 * @fence: optional resulting fence
1467 *
Christian Königa14faa62016-01-25 14:27:31 +01001468 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001469 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470 */
1471static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001472 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001473 uint64_t src,
1474 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001475 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001476 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001477 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001478 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001479{
Christian König2d55e452016-02-08 17:37:38 +01001480 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001481 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001483 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001484 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001485 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001486 int r;
1487
Christian Königafef8b82016-08-12 13:29:18 +02001488 memset(&params, 0, sizeof(params));
1489 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001490 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001491 params.src = src;
1492
Christian Königa33cab72017-07-11 17:13:00 +02001493 /* sync to everything on unmapping */
1494 if (!(flags & AMDGPU_PTE_VALID))
1495 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1496
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001497 if (vm->use_cpu_for_update) {
1498 /* params.src is used as flag to indicate system Memory */
1499 if (pages_addr)
1500 params.src = ~0;
1501
1502 /* Wait for PT BOs to be free. PTs share the same resv. object
1503 * as the root PD BO
1504 */
Christian Königa33cab72017-07-11 17:13:00 +02001505 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001506 if (unlikely(r))
1507 return r;
1508
1509 params.func = amdgpu_vm_cpu_set_ptes;
1510 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001511 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1512 addr, flags);
1513 }
1514
Christian König2d55e452016-02-08 17:37:38 +01001515 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001516
Christian Königa14faa62016-01-25 14:27:31 +01001517 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001518
1519 /*
1520 * reserve space for one command every (1 << BLOCK_SIZE)
1521 * entries or 2k dwords (whatever is smaller)
1522 */
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001523 ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001524
1525 /* padding, etc. */
1526 ndw = 64;
1527
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001528 /* one PDE write for each huge page */
1529 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1530
Christian Königb0456f92016-08-11 14:06:54 +02001531 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001532 /* only copy commands needed */
1533 ndw += ncmds * 7;
1534
Christian Königafef8b82016-08-12 13:29:18 +02001535 params.func = amdgpu_vm_do_copy_ptes;
1536
Christian Königb0456f92016-08-11 14:06:54 +02001537 } else if (pages_addr) {
1538 /* copy commands needed */
1539 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001540
Christian Königb0456f92016-08-11 14:06:54 +02001541 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 ndw += nptes * 2;
1543
Christian Königafef8b82016-08-12 13:29:18 +02001544 params.func = amdgpu_vm_do_copy_ptes;
1545
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546 } else {
1547 /* set page commands needed */
1548 ndw += ncmds * 10;
1549
1550 /* two extra commands for begin/end of fragment */
1551 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001552
1553 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001554 }
1555
Christian Königd71518b2016-02-01 12:20:25 +01001556 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1557 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001558 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001559
Christian König29efc4f2016-08-04 14:52:50 +02001560 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001561
Christian Königb0456f92016-08-11 14:06:54 +02001562 if (!src && pages_addr) {
1563 uint64_t *pte;
1564 unsigned i;
1565
1566 /* Put the PTEs at the end of the IB. */
1567 i = ndw - nptes * 2;
1568 pte= (uint64_t *)&(job->ibs->ptr[i]);
1569 params.src = job->ibs->gpu_addr + i * 4;
1570
1571 for (i = 0; i < nptes; ++i) {
1572 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1573 AMDGPU_GPU_PAGE_SIZE);
1574 pte[i] |= flags;
1575 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001576 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001577 }
1578
Christian König3cabaa52016-06-06 10:17:58 +02001579 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1580 if (r)
1581 goto error_free;
1582
Christian König67003a12016-10-12 14:46:26 +02001583 r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001584 owner);
1585 if (r)
1586 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001587
Christian König67003a12016-10-12 14:46:26 +02001588 r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001589 if (r)
1590 goto error_free;
1591
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001592 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1593 if (r)
1594 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001595
Christian König29efc4f2016-08-04 14:52:50 +02001596 amdgpu_ring_pad_ib(ring, params.ib);
1597 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001598 r = amdgpu_job_submit(job, ring, &vm->entity,
1599 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001600 if (r)
1601 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602
Christian König67003a12016-10-12 14:46:26 +02001603 amdgpu_bo_fence(vm->root.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001604 dma_fence_put(*fence);
1605 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001607
1608error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001609 amdgpu_job_free(job);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001610 amdgpu_vm_invalidate_level(&vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001611 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612}
1613
1614/**
Christian Königa14faa62016-01-25 14:27:31 +01001615 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1616 *
1617 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001618 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001619 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001620 * @vm: requested vm
1621 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001622 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001623 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001624 * @fence: optional resulting fence
1625 *
1626 * Split the mapping into smaller chunks so that each update fits
1627 * into a SDMA IB.
1628 * Returns 0 for success, -EINVAL for failure.
1629 */
1630static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001631 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001632 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001633 struct amdgpu_vm *vm,
1634 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001635 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001636 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001637 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001638{
Christian Königa9f87f62017-03-30 14:03:59 +02001639 uint64_t pfn, src = 0, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001640 int r;
1641
1642 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1643 * but in case of something, we filter the flags in first place
1644 */
1645 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1646 flags &= ~AMDGPU_PTE_READABLE;
1647 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1648 flags &= ~AMDGPU_PTE_WRITEABLE;
1649
Alex Xie15b31c52017-03-03 16:47:11 -05001650 flags &= ~AMDGPU_PTE_EXECUTABLE;
1651 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1652
Alex Xieb0fd18b2017-03-03 16:49:39 -05001653 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1654 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1655
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001656 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1657 (adev->asic_type >= CHIP_VEGA10)) {
1658 flags |= AMDGPU_PTE_PRT;
1659 flags &= ~AMDGPU_PTE_VALID;
1660 }
1661
Christian Königa14faa62016-01-25 14:27:31 +01001662 trace_amdgpu_vm_bo_update(mapping);
1663
Christian König63e0ba42016-08-16 17:38:37 +02001664 pfn = mapping->offset >> PAGE_SHIFT;
1665 if (nodes) {
1666 while (pfn >= nodes->size) {
1667 pfn -= nodes->size;
1668 ++nodes;
1669 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001670 }
Christian Königa14faa62016-01-25 14:27:31 +01001671
Christian König63e0ba42016-08-16 17:38:37 +02001672 do {
1673 uint64_t max_entries;
1674 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001675
Christian König63e0ba42016-08-16 17:38:37 +02001676 if (nodes) {
1677 addr = nodes->start << PAGE_SHIFT;
1678 max_entries = (nodes->size - pfn) *
1679 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1680 } else {
1681 addr = 0;
1682 max_entries = S64_MAX;
1683 }
Christian Königa14faa62016-01-25 14:27:31 +01001684
Christian König63e0ba42016-08-16 17:38:37 +02001685 if (pages_addr) {
Christian Königfebb84a2017-08-22 12:50:46 +02001686 max_entries = min(max_entries, 16ull * 1024ull);
Christian König63e0ba42016-08-16 17:38:37 +02001687 addr = 0;
1688 } else if (flags & AMDGPU_PTE_VALID) {
1689 addr += adev->vm_manager.vram_base_offset;
1690 }
1691 addr += pfn << PAGE_SHIFT;
1692
Christian Königa9f87f62017-03-30 14:03:59 +02001693 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001694 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1695 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001696 start, last, flags, addr,
1697 fence);
1698 if (r)
1699 return r;
1700
Christian König63e0ba42016-08-16 17:38:37 +02001701 pfn += last - start + 1;
1702 if (nodes && nodes->size == pfn) {
1703 pfn = 0;
1704 ++nodes;
1705 }
Christian Königa14faa62016-01-25 14:27:31 +01001706 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001707
Christian Königa9f87f62017-03-30 14:03:59 +02001708 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001709
1710 return 0;
1711}
1712
1713/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001714 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1715 *
1716 * @adev: amdgpu_device pointer
1717 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001718 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001719 *
1720 * Fill in the page table entries for @bo_va.
1721 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 */
1723int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1724 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001725 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001726{
Christian Königec681542017-08-01 10:51:43 +02001727 struct amdgpu_bo *bo = bo_va->base.bo;
1728 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001729 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001730 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001731 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001732 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001733 struct dma_fence *exclusive;
Christian Königfebb84a2017-08-22 12:50:46 +02001734 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001735 int r;
1736
Christian Königec681542017-08-01 10:51:43 +02001737 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001738 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001739 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001740 exclusive = NULL;
1741 } else {
Christian König8358dce2016-03-30 10:50:25 +02001742 struct ttm_dma_tt *ttm;
1743
Christian Königec681542017-08-01 10:51:43 +02001744 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001745 nodes = mem->mm_node;
1746 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001747 ttm = container_of(bo_va->base.bo->tbo.ttm,
1748 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001749 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001750 }
Christian Königec681542017-08-01 10:51:43 +02001751 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752 }
1753
Christian Königfebb84a2017-08-22 12:50:46 +02001754 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001755 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian Königfebb84a2017-08-22 12:50:46 +02001756 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001757 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758
Christian König7fc11952015-07-30 11:53:42 +02001759 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02001760 if (!list_empty(&bo_va->base.vm_status))
Christian König7fc11952015-07-30 11:53:42 +02001761 list_splice_init(&bo_va->valids, &bo_va->invalids);
1762 spin_unlock(&vm->status_lock);
1763
1764 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königfebb84a2017-08-22 12:50:46 +02001765 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001766 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001767 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001768 if (r)
1769 return r;
1770 }
1771
Christian Königd6c10f62015-09-28 12:00:23 +02001772 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1773 list_for_each_entry(mapping, &bo_va->valids, list)
1774 trace_amdgpu_vm_bo_mapping(mapping);
1775
1776 list_for_each_entry(mapping, &bo_va->invalids, list)
1777 trace_amdgpu_vm_bo_mapping(mapping);
1778 }
1779
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001781 list_splice_init(&bo_va->invalids, &bo_va->valids);
Christian Königec681542017-08-01 10:51:43 +02001782 list_del_init(&bo_va->base.vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001783 if (clear)
Christian Königec681542017-08-01 10:51:43 +02001784 list_add(&bo_va->base.vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785 spin_unlock(&vm->status_lock);
1786
Christian König68c62302017-07-11 17:23:29 +02001787 if (vm->use_cpu_for_update) {
1788 /* Flush HDP */
1789 mb();
1790 amdgpu_gart_flush_gpu_tlb(adev, 0);
1791 }
1792
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793 return 0;
1794}
1795
1796/**
Christian König284710f2017-01-30 11:09:31 +01001797 * amdgpu_vm_update_prt_state - update the global PRT state
1798 */
1799static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1800{
1801 unsigned long flags;
1802 bool enable;
1803
1804 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001805 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001806 adev->gart.gart_funcs->set_prt(adev, enable);
1807 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1808}
1809
1810/**
Christian König4388fc22017-03-13 10:13:36 +01001811 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001812 */
1813static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1814{
Christian König4388fc22017-03-13 10:13:36 +01001815 if (!adev->gart.gart_funcs->set_prt)
1816 return;
1817
Christian König451bc8e2017-02-14 16:02:52 +01001818 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1819 amdgpu_vm_update_prt_state(adev);
1820}
1821
1822/**
Christian König0b15f2f2017-02-14 15:47:03 +01001823 * amdgpu_vm_prt_put - drop a PRT user
1824 */
1825static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1826{
Christian König451bc8e2017-02-14 16:02:52 +01001827 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001828 amdgpu_vm_update_prt_state(adev);
1829}
1830
1831/**
Christian König451bc8e2017-02-14 16:02:52 +01001832 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001833 */
1834static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1835{
1836 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1837
Christian König0b15f2f2017-02-14 15:47:03 +01001838 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001839 kfree(cb);
1840}
1841
1842/**
Christian König451bc8e2017-02-14 16:02:52 +01001843 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1844 */
1845static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1846 struct dma_fence *fence)
1847{
Christian König4388fc22017-03-13 10:13:36 +01001848 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001849
Christian König4388fc22017-03-13 10:13:36 +01001850 if (!adev->gart.gart_funcs->set_prt)
1851 return;
1852
1853 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001854 if (!cb) {
1855 /* Last resort when we are OOM */
1856 if (fence)
1857 dma_fence_wait(fence, false);
1858
Dan Carpenter486a68f2017-04-03 21:41:39 +03001859 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001860 } else {
1861 cb->adev = adev;
1862 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1863 amdgpu_vm_prt_cb))
1864 amdgpu_vm_prt_cb(fence, &cb->cb);
1865 }
1866}
1867
1868/**
Christian König284710f2017-01-30 11:09:31 +01001869 * amdgpu_vm_free_mapping - free a mapping
1870 *
1871 * @adev: amdgpu_device pointer
1872 * @vm: requested vm
1873 * @mapping: mapping to be freed
1874 * @fence: fence of the unmap operation
1875 *
1876 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1877 */
1878static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1879 struct amdgpu_vm *vm,
1880 struct amdgpu_bo_va_mapping *mapping,
1881 struct dma_fence *fence)
1882{
Christian König451bc8e2017-02-14 16:02:52 +01001883 if (mapping->flags & AMDGPU_PTE_PRT)
1884 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001885 kfree(mapping);
1886}
1887
1888/**
Christian König451bc8e2017-02-14 16:02:52 +01001889 * amdgpu_vm_prt_fini - finish all prt mappings
1890 *
1891 * @adev: amdgpu_device pointer
1892 * @vm: requested vm
1893 *
1894 * Register a cleanup callback to disable PRT support after VM dies.
1895 */
1896static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1897{
Christian König67003a12016-10-12 14:46:26 +02001898 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001899 struct dma_fence *excl, **shared;
1900 unsigned i, shared_count;
1901 int r;
1902
1903 r = reservation_object_get_fences_rcu(resv, &excl,
1904 &shared_count, &shared);
1905 if (r) {
1906 /* Not enough memory to grab the fence list, as last resort
1907 * block for all the fences to complete.
1908 */
1909 reservation_object_wait_timeout_rcu(resv, true, false,
1910 MAX_SCHEDULE_TIMEOUT);
1911 return;
1912 }
1913
1914 /* Add a callback for each fence in the reservation object */
1915 amdgpu_vm_prt_get(adev);
1916 amdgpu_vm_add_prt_cb(adev, excl);
1917
1918 for (i = 0; i < shared_count; ++i) {
1919 amdgpu_vm_prt_get(adev);
1920 amdgpu_vm_add_prt_cb(adev, shared[i]);
1921 }
1922
1923 kfree(shared);
1924}
1925
1926/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001927 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1928 *
1929 * @adev: amdgpu_device pointer
1930 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001931 * @fence: optional resulting fence (unchanged if no work needed to be done
1932 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001933 *
1934 * Make sure all freed BOs are cleared in the PT.
1935 * Returns 0 for success.
1936 *
1937 * PTs have to be reserved and mutex must be locked!
1938 */
1939int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001940 struct amdgpu_vm *vm,
1941 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001942{
1943 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001944 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001946 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001947
1948 while (!list_empty(&vm->freed)) {
1949 mapping = list_first_entry(&vm->freed,
1950 struct amdgpu_bo_va_mapping, list);
1951 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001952
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001953 if (vm->pte_support_ats)
1954 init_pte_value = AMDGPU_PTE_SYSTEM;
1955
Christian Königfc6aa332017-04-19 14:41:19 +02001956 r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm,
1957 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001958 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001959 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001960 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001961 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962 return r;
Christian König284710f2017-01-30 11:09:31 +01001963 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001964 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001965
1966 if (fence && f) {
1967 dma_fence_put(*fence);
1968 *fence = f;
1969 } else {
1970 dma_fence_put(f);
1971 }
1972
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001973 return 0;
1974
1975}
1976
1977/**
Christian König27c7b9a2017-08-01 11:27:36 +02001978 * amdgpu_vm_clear_moved - clear moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001979 *
1980 * @adev: amdgpu_device pointer
1981 * @vm: requested vm
1982 *
Christian König27c7b9a2017-08-01 11:27:36 +02001983 * Make sure all moved BOs are cleared in the PT.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 * Returns 0 for success.
1985 *
1986 * PTs have to be reserved and mutex must be locked!
1987 */
Christian König27c7b9a2017-08-01 11:27:36 +02001988int amdgpu_vm_clear_moved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1989 struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001990{
monk.liucfe2c972015-05-26 15:01:54 +08001991 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001992 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001993
1994 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02001995 while (!list_empty(&vm->moved)) {
1996 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02001997 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001999
Christian König99e124f2016-08-16 14:43:17 +02002000 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002001 if (r)
2002 return r;
2003
2004 spin_lock(&vm->status_lock);
2005 }
2006 spin_unlock(&vm->status_lock);
2007
monk.liucfe2c972015-05-26 15:01:54 +08002008 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08002009 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02002010
2011 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002012}
2013
2014/**
2015 * amdgpu_vm_bo_add - add a bo to a specific vm
2016 *
2017 * @adev: amdgpu_device pointer
2018 * @vm: requested vm
2019 * @bo: amdgpu buffer object
2020 *
Christian König8843dbb2016-01-26 12:17:11 +01002021 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002022 * Add @bo to the list of bos associated with the vm
2023 * Returns newly added bo_va or NULL for failure
2024 *
2025 * Object has to be reserved!
2026 */
2027struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2028 struct amdgpu_vm *vm,
2029 struct amdgpu_bo *bo)
2030{
2031 struct amdgpu_bo_va *bo_va;
2032
2033 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2034 if (bo_va == NULL) {
2035 return NULL;
2036 }
Christian Königec681542017-08-01 10:51:43 +02002037 bo_va->base.vm = vm;
2038 bo_va->base.bo = bo;
2039 INIT_LIST_HEAD(&bo_va->base.bo_list);
2040 INIT_LIST_HEAD(&bo_va->base.vm_status);
2041
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002043 INIT_LIST_HEAD(&bo_va->valids);
2044 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002045
Christian Königa5f6b5b2017-01-30 11:01:38 +01002046 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002047 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048
2049 return bo_va;
2050}
2051
2052/**
2053 * amdgpu_vm_bo_map - map bo inside a vm
2054 *
2055 * @adev: amdgpu_device pointer
2056 * @bo_va: bo_va to store the address
2057 * @saddr: where to map the BO
2058 * @offset: requested offset in the BO
2059 * @flags: attributes of pages (read/write/valid/etc.)
2060 *
2061 * Add a mapping of the BO at the specefied addr into the VM.
2062 * Returns 0 for success, error for failure.
2063 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002064 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065 */
2066int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2067 struct amdgpu_bo_va *bo_va,
2068 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002069 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002070{
Christian Königa9f87f62017-03-30 14:03:59 +02002071 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002072 struct amdgpu_bo *bo = bo_va->base.bo;
2073 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002074 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075
Christian König0be52de2015-05-18 14:37:27 +02002076 /* validate the parameters */
2077 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002078 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002079 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002080
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002082 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002083 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002084 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002086
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087 saddr /= AMDGPU_GPU_PAGE_SIZE;
2088 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2089
Christian Königa9f87f62017-03-30 14:03:59 +02002090 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2091 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002092 /* bo and tmp overlap, invalid addr */
2093 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002094 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002095 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002096 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097 }
2098
2099 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002100 if (!mapping)
2101 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002102
2103 INIT_LIST_HEAD(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002104 mapping->start = saddr;
2105 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002106 mapping->offset = offset;
2107 mapping->flags = flags;
2108
Christian König7fc11952015-07-30 11:53:42 +02002109 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02002110 amdgpu_vm_it_insert(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111
Christian König4388fc22017-03-13 10:13:36 +01002112 if (flags & AMDGPU_PTE_PRT)
2113 amdgpu_vm_prt_get(adev);
2114
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002115 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002116}
2117
2118/**
Christian König80f95c52017-03-13 10:13:39 +01002119 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2120 *
2121 * @adev: amdgpu_device pointer
2122 * @bo_va: bo_va to store the address
2123 * @saddr: where to map the BO
2124 * @offset: requested offset in the BO
2125 * @flags: attributes of pages (read/write/valid/etc.)
2126 *
2127 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2128 * mappings as we do so.
2129 * Returns 0 for success, error for failure.
2130 *
2131 * Object has to be reserved and unreserved outside!
2132 */
2133int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2134 struct amdgpu_bo_va *bo_va,
2135 uint64_t saddr, uint64_t offset,
2136 uint64_t size, uint64_t flags)
2137{
2138 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002139 struct amdgpu_bo *bo = bo_va->base.bo;
2140 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König80f95c52017-03-13 10:13:39 +01002141 uint64_t eaddr;
2142 int r;
2143
2144 /* validate the parameters */
2145 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2146 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2147 return -EINVAL;
2148
2149 /* make sure object fit at this offset */
2150 eaddr = saddr + size - 1;
2151 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002152 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002153 return -EINVAL;
2154
2155 /* Allocate all the needed memory */
2156 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2157 if (!mapping)
2158 return -ENOMEM;
2159
Christian Königec681542017-08-01 10:51:43 +02002160 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002161 if (r) {
2162 kfree(mapping);
2163 return r;
2164 }
2165
2166 saddr /= AMDGPU_GPU_PAGE_SIZE;
2167 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2168
Christian Königa9f87f62017-03-30 14:03:59 +02002169 mapping->start = saddr;
2170 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002171 mapping->offset = offset;
2172 mapping->flags = flags;
2173
2174 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02002175 amdgpu_vm_it_insert(mapping, &vm->va);
Christian König80f95c52017-03-13 10:13:39 +01002176
2177 if (flags & AMDGPU_PTE_PRT)
2178 amdgpu_vm_prt_get(adev);
2179
2180 return 0;
2181}
2182
2183/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002184 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2185 *
2186 * @adev: amdgpu_device pointer
2187 * @bo_va: bo_va to remove the address from
2188 * @saddr: where to the BO is mapped
2189 *
2190 * Remove a mapping of the BO at the specefied addr from the VM.
2191 * Returns 0 for success, error for failure.
2192 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002193 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002194 */
2195int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2196 struct amdgpu_bo_va *bo_va,
2197 uint64_t saddr)
2198{
2199 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002200 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002201 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202
Christian König6c7fc502015-06-05 20:56:17 +02002203 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002204
Christian König7fc11952015-07-30 11:53:42 +02002205 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002206 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 break;
2208 }
2209
Christian König7fc11952015-07-30 11:53:42 +02002210 if (&mapping->list == &bo_va->valids) {
2211 valid = false;
2212
2213 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002214 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002215 break;
2216 }
2217
Christian König32b41ac2016-03-08 18:03:27 +01002218 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002219 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002220 }
Christian König32b41ac2016-03-08 18:03:27 +01002221
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002222 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002223 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002224 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002225
Christian Könige17841b2016-03-08 17:52:01 +01002226 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002228 else
Christian König284710f2017-01-30 11:09:31 +01002229 amdgpu_vm_free_mapping(adev, vm, mapping,
2230 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002231
2232 return 0;
2233}
2234
2235/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002236 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2237 *
2238 * @adev: amdgpu_device pointer
2239 * @vm: VM structure to use
2240 * @saddr: start of the range
2241 * @size: size of the range
2242 *
2243 * Remove all mappings in a range, split them as appropriate.
2244 * Returns 0 for success, error for failure.
2245 */
2246int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2247 struct amdgpu_vm *vm,
2248 uint64_t saddr, uint64_t size)
2249{
2250 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002251 LIST_HEAD(removed);
2252 uint64_t eaddr;
2253
2254 eaddr = saddr + size - 1;
2255 saddr /= AMDGPU_GPU_PAGE_SIZE;
2256 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2257
2258 /* Allocate all the needed memory */
2259 before = kzalloc(sizeof(*before), GFP_KERNEL);
2260 if (!before)
2261 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002262 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002263
2264 after = kzalloc(sizeof(*after), GFP_KERNEL);
2265 if (!after) {
2266 kfree(before);
2267 return -ENOMEM;
2268 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002269 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002270
2271 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002272 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2273 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002274 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002275 if (tmp->start < saddr) {
2276 before->start = tmp->start;
2277 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002278 before->offset = tmp->offset;
2279 before->flags = tmp->flags;
2280 list_add(&before->list, &tmp->list);
2281 }
2282
2283 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002284 if (tmp->last > eaddr) {
2285 after->start = eaddr + 1;
2286 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002287 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002288 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002289 after->flags = tmp->flags;
2290 list_add(&after->list, &tmp->list);
2291 }
2292
2293 list_del(&tmp->list);
2294 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002295
2296 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002297 }
2298
2299 /* And free them up */
2300 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002301 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002302 list_del(&tmp->list);
2303
Christian Königa9f87f62017-03-30 14:03:59 +02002304 if (tmp->start < saddr)
2305 tmp->start = saddr;
2306 if (tmp->last > eaddr)
2307 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002308
2309 list_add(&tmp->list, &vm->freed);
2310 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2311 }
2312
Junwei Zhang27f6d612017-03-16 16:09:24 +08002313 /* Insert partial mapping before the range */
2314 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002315 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002316 if (before->flags & AMDGPU_PTE_PRT)
2317 amdgpu_vm_prt_get(adev);
2318 } else {
2319 kfree(before);
2320 }
2321
2322 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002323 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002324 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002325 if (after->flags & AMDGPU_PTE_PRT)
2326 amdgpu_vm_prt_get(adev);
2327 } else {
2328 kfree(after);
2329 }
2330
2331 return 0;
2332}
2333
2334/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002335 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2336 *
2337 * @adev: amdgpu_device pointer
2338 * @bo_va: requested bo_va
2339 *
Christian König8843dbb2016-01-26 12:17:11 +01002340 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002341 *
2342 * Object have to be reserved!
2343 */
2344void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2345 struct amdgpu_bo_va *bo_va)
2346{
2347 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002348 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002349
Christian Königec681542017-08-01 10:51:43 +02002350 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002351
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002352 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002353 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002354 spin_unlock(&vm->status_lock);
2355
Christian König7fc11952015-07-30 11:53:42 +02002356 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002357 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002358 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002359 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002360 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002361 }
Christian König7fc11952015-07-30 11:53:42 +02002362 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2363 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002364 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002365 amdgpu_vm_free_mapping(adev, vm, mapping,
2366 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002367 }
Christian König32b41ac2016-03-08 18:03:27 +01002368
Chris Wilsonf54d1862016-10-25 13:00:45 +01002369 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002370 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002371}
2372
2373/**
2374 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2375 *
2376 * @adev: amdgpu_device pointer
2377 * @vm: requested vm
2378 * @bo: amdgpu buffer object
2379 *
Christian König8843dbb2016-01-26 12:17:11 +01002380 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002381 */
2382void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
2383 struct amdgpu_bo *bo)
2384{
Christian Königec681542017-08-01 10:51:43 +02002385 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002386
Christian Königec681542017-08-01 10:51:43 +02002387 list_for_each_entry(bo_base, &bo->va, bo_list) {
2388 spin_lock(&bo_base->vm->status_lock);
2389 if (list_empty(&bo_base->vm_status))
2390 list_add(&bo_base->vm_status,
Christian König27c7b9a2017-08-01 11:27:36 +02002391 &bo_base->vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002392 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002393 }
2394}
2395
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002396static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2397{
2398 /* Total bits covered by PD + PTs */
2399 unsigned bits = ilog2(vm_size) + 18;
2400
2401 /* Make sure the PD is 4K in size up to 8GB address space.
2402 Above that split equal between PD and PTs */
2403 if (vm_size <= 8)
2404 return (bits - 9);
2405 else
2406 return ((bits + 3) / 2);
2407}
2408
2409/**
Roger Hed07f14b2017-08-15 16:05:59 +08002410 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2411 *
2412 * @adev: amdgpu_device pointer
2413 * @fragment_size_default: the default fragment size if it's set auto
2414 */
2415void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev, uint32_t fragment_size_default)
2416{
2417 if (amdgpu_vm_fragment_size == -1)
2418 adev->vm_manager.fragment_size = fragment_size_default;
2419 else
2420 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2421}
2422
2423/**
2424 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002425 *
2426 * @adev: amdgpu_device pointer
2427 * @vm_size: the default vm size if it's set auto
2428 */
Roger Hed07f14b2017-08-15 16:05:59 +08002429void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size, uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002430{
2431 /* adjust vm size firstly */
2432 if (amdgpu_vm_size == -1)
2433 adev->vm_manager.vm_size = vm_size;
2434 else
2435 adev->vm_manager.vm_size = amdgpu_vm_size;
2436
2437 /* block size depends on vm size */
2438 if (amdgpu_vm_block_size == -1)
2439 adev->vm_manager.block_size =
2440 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2441 else
2442 adev->vm_manager.block_size = amdgpu_vm_block_size;
2443
Roger Hed07f14b2017-08-15 16:05:59 +08002444 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2445
2446 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2447 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2448 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002449}
2450
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002451/**
2452 * amdgpu_vm_init - initialize a vm instance
2453 *
2454 * @adev: amdgpu_device pointer
2455 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002456 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002457 *
Christian König8843dbb2016-01-26 12:17:11 +01002458 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002459 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002460int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2461 int vm_context)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002462{
2463 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002464 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002465 unsigned ring_instance;
2466 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002467 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002468 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002469 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002470 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002471
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002472 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002473 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002474 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2475 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476 spin_lock_init(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002477 INIT_LIST_HEAD(&vm->moved);
Christian König7fc11952015-07-30 11:53:42 +02002478 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002480
Christian König2bd9ccf2016-02-01 12:53:58 +01002481 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002482
2483 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2484 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2485 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002486 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2487 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2488 rq, amdgpu_sched_jobs);
2489 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002490 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002491
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002492 vm->pte_support_ats = false;
2493
2494 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002495 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2496 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002497
2498 if (adev->asic_type == CHIP_RAVEN) {
2499 vm->pte_support_ats = true;
2500 init_pde_value = AMDGPU_PTE_SYSTEM | AMDGPU_PDE_PTE;
2501 }
2502 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002503 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2504 AMDGPU_VM_USE_CPU_FOR_GFX);
2505 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2506 vm->use_cpu_for_update ? "CPU" : "SDMA");
2507 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2508 "CPU update of VM recommended only for large BAR system\n");
Christian Königa24960f2016-10-12 13:20:52 +02002509 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002510
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002511 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2512 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2513 if (vm->use_cpu_for_update)
2514 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2515 else
2516 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2517 AMDGPU_GEM_CREATE_SHADOW);
2518
Christian Königf566ceb2016-10-27 20:04:38 +02002519 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002520 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002521 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002522 NULL, NULL, init_pde_value, &vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002523 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002524 goto error_free_sched_entity;
2525
Christian König67003a12016-10-12 14:46:26 +02002526 r = amdgpu_bo_reserve(vm->root.bo, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01002527 if (r)
Christian König67003a12016-10-12 14:46:26 +02002528 goto error_free_root;
Christian König2bd9ccf2016-02-01 12:53:58 +01002529
Christian König5a712a82016-06-21 16:28:15 +02002530 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König0a096fb2017-07-12 10:01:48 +02002531
2532 if (vm->use_cpu_for_update) {
2533 r = amdgpu_bo_kmap(vm->root.bo, NULL);
2534 if (r)
2535 goto error_free_root;
2536 }
2537
Christian König67003a12016-10-12 14:46:26 +02002538 amdgpu_bo_unreserve(vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002539
2540 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002541
Christian König67003a12016-10-12 14:46:26 +02002542error_free_root:
2543 amdgpu_bo_unref(&vm->root.bo->shadow);
2544 amdgpu_bo_unref(&vm->root.bo);
2545 vm->root.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002546
2547error_free_sched_entity:
2548 amd_sched_entity_fini(&ring->sched, &vm->entity);
2549
2550 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002551}
2552
2553/**
Christian Königf566ceb2016-10-27 20:04:38 +02002554 * amdgpu_vm_free_levels - free PD/PT levels
2555 *
2556 * @level: PD/PT starting level to free
2557 *
2558 * Free the page directory or page table level and all sub levels.
2559 */
2560static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2561{
2562 unsigned i;
2563
2564 if (level->bo) {
2565 amdgpu_bo_unref(&level->bo->shadow);
2566 amdgpu_bo_unref(&level->bo);
2567 }
2568
2569 if (level->entries)
2570 for (i = 0; i <= level->last_entry_used; i++)
2571 amdgpu_vm_free_levels(&level->entries[i]);
2572
Michal Hocko20981052017-05-17 14:23:12 +02002573 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002574}
2575
2576/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002577 * amdgpu_vm_fini - tear down a vm instance
2578 *
2579 * @adev: amdgpu_device pointer
2580 * @vm: requested vm
2581 *
Christian König8843dbb2016-01-26 12:17:11 +01002582 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002583 * Unbind the VM and remove all bos from the vm bo list
2584 */
2585void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2586{
2587 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002588 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002589 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002590
Christian König2d55e452016-02-08 17:37:38 +01002591 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002592
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002593 if (!RB_EMPTY_ROOT(&vm->va)) {
2594 dev_err(adev->dev, "still active bo inside vm\n");
2595 }
Christian Königa9f87f62017-03-30 14:03:59 +02002596 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002597 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002598 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002599 kfree(mapping);
2600 }
2601 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002602 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002603 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002604 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002605 }
Christian König284710f2017-01-30 11:09:31 +01002606
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002607 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002608 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002609 }
2610
Christian Königf566ceb2016-10-27 20:04:38 +02002611 amdgpu_vm_free_levels(&vm->root);
Christian Königa24960f2016-10-12 13:20:52 +02002612 dma_fence_put(vm->last_dir_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002613 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2614 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002615}
Christian Königea89f8c2015-11-15 20:52:06 +01002616
2617/**
Christian Königa9a78b32016-01-21 10:19:11 +01002618 * amdgpu_vm_manager_init - init the VM manager
2619 *
2620 * @adev: amdgpu_device pointer
2621 *
2622 * Initialize the VM manager structures
2623 */
2624void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2625{
Christian König76456702017-04-06 17:52:39 +02002626 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002627
Christian König76456702017-04-06 17:52:39 +02002628 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2629 struct amdgpu_vm_id_manager *id_mgr =
2630 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002631
Christian König76456702017-04-06 17:52:39 +02002632 mutex_init(&id_mgr->lock);
2633 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002634 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002635
2636 /* skip over VMID 0, since it is the system VM */
2637 for (j = 1; j < id_mgr->num_ids; ++j) {
2638 amdgpu_vm_reset_id(adev, i, j);
2639 amdgpu_sync_create(&id_mgr->ids[i].active);
2640 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2641 }
Christian König971fe9a92016-03-01 15:09:25 +01002642 }
Christian König2d55e452016-02-08 17:37:38 +01002643
Chris Wilsonf54d1862016-10-25 13:00:45 +01002644 adev->vm_manager.fence_context =
2645 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002646 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2647 adev->vm_manager.seqno[i] = 0;
2648
Christian König2d55e452016-02-08 17:37:38 +01002649 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002650 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002651 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002652 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002653
2654 /* If not overridden by the user, by default, only in large BAR systems
2655 * Compute VM tables will be updated by CPU
2656 */
2657#ifdef CONFIG_X86_64
2658 if (amdgpu_vm_update_mode == -1) {
2659 if (amdgpu_vm_is_large_bar(adev))
2660 adev->vm_manager.vm_update_mode =
2661 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2662 else
2663 adev->vm_manager.vm_update_mode = 0;
2664 } else
2665 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2666#else
2667 adev->vm_manager.vm_update_mode = 0;
2668#endif
2669
Christian Königa9a78b32016-01-21 10:19:11 +01002670}
2671
2672/**
Christian Königea89f8c2015-11-15 20:52:06 +01002673 * amdgpu_vm_manager_fini - cleanup VM manager
2674 *
2675 * @adev: amdgpu_device pointer
2676 *
2677 * Cleanup the VM manager and free resources.
2678 */
2679void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2680{
Christian König76456702017-04-06 17:52:39 +02002681 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002682
Christian König76456702017-04-06 17:52:39 +02002683 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2684 struct amdgpu_vm_id_manager *id_mgr =
2685 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002686
Christian König76456702017-04-06 17:52:39 +02002687 mutex_destroy(&id_mgr->lock);
2688 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2689 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2690
2691 amdgpu_sync_free(&id->active);
2692 dma_fence_put(id->flushed_updates);
2693 dma_fence_put(id->last_flush);
2694 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002695 }
Christian Königea89f8c2015-11-15 20:52:06 +01002696}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002697
2698int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2699{
2700 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002701 struct amdgpu_device *adev = dev->dev_private;
2702 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2703 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002704
2705 switch (args->in.op) {
2706 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002707 /* current, we only have requirement to reserve vmid from gfxhub */
2708 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2709 AMDGPU_GFXHUB);
2710 if (r)
2711 return r;
2712 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002713 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002714 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002715 break;
2716 default:
2717 return -EINVAL;
2718 }
2719
2720 return 0;
2721}