blob: 8ac3bcf9873ff7ed6097bcf66d16e08cdd890ca0 [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>
Felix Kuehling02208442017-08-25 20:40:26 -040030#include <linux/idr.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031#include <drm/drmP.h>
32#include <drm/amdgpu_drm.h>
33#include "amdgpu.h"
34#include "amdgpu_trace.h"
35
36/*
37 * GPUVM
38 * GPUVM is similar to the legacy gart on older asics, however
39 * rather than there being a single global gart table
40 * for the entire GPU, there are multiple VM page tables active
41 * at any given time. The VM page tables can contain a mix
42 * vram pages and system memory pages and system memory pages
43 * can be mapped as snooped (cached system pages) or unsnooped
44 * (uncached system pages).
45 * Each VM has an ID associated with it and there is a page table
46 * associated with each VMID. When execting a command buffer,
47 * the kernel tells the the ring what VMID to use for that command
48 * buffer. VMIDs are allocated dynamically as commands are submitted.
49 * The userspace drivers maintain their own address space and the kernel
50 * sets up their pages tables accordingly when they submit their
51 * command buffers and a VMID is assigned.
52 * Cayman/Trinity support up to 8 active VMs at any given time;
53 * SI supports 16.
54 */
55
Christian Königa9f87f62017-03-30 14:03:59 +020056#define START(node) ((node)->start)
57#define LAST(node) ((node)->last)
58
59INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
60 START, LAST, static, amdgpu_vm_it)
61
62#undef START
63#undef LAST
64
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040065/* Local structure. Encapsulate some VM table update parameters to reduce
66 * the number of function parameters
67 */
Christian König29efc4f2016-08-04 14:52:50 +020068struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020069 /* amdgpu device we do this update for */
70 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +020071 /* optional amdgpu_vm we do this update for */
72 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040073 /* address where to copy page table entries from */
74 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040075 /* indirect buffer to fill with commands */
76 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020077 /* Function which actually does the update */
Christian König373ac642018-01-16 16:54:25 +010078 void (*func)(struct amdgpu_pte_update_params *params,
79 struct amdgpu_bo *bo, uint64_t pe,
Christian Königafef8b82016-08-12 13:29:18 +020080 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080081 uint64_t flags);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -040082 /* The next two are used during VM update by CPU
83 * DMA addresses to use for mapping
84 * Kernel pointer of PD/PT BO that needs to be updated
85 */
86 dma_addr_t *pages_addr;
87 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040088};
89
Christian König284710f2017-01-30 11:09:31 +010090/* Helper to disable partial resident texture feature from a fence callback */
91struct amdgpu_prt_cb {
92 struct amdgpu_device *adev;
93 struct dma_fence_cb cb;
94};
95
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096/**
Christian König50783142017-11-27 14:01:51 +010097 * amdgpu_vm_level_shift - return the addr shift for each level
98 *
99 * @adev: amdgpu_device pointer
100 *
101 * Returns the number of bits the pfn needs to be right shifted for a level.
102 */
103static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
104 unsigned level)
105{
Chunming Zhou196f7482017-12-13 14:22:54 +0800106 unsigned shift = 0xff;
107
108 switch (level) {
109 case AMDGPU_VM_PDB2:
110 case AMDGPU_VM_PDB1:
111 case AMDGPU_VM_PDB0:
112 shift = 9 * (AMDGPU_VM_PDB0 - level) +
Christian König50783142017-11-27 14:01:51 +0100113 adev->vm_manager.block_size;
Chunming Zhou196f7482017-12-13 14:22:54 +0800114 break;
115 case AMDGPU_VM_PTB:
116 shift = 0;
117 break;
118 default:
119 dev_err(adev->dev, "the level%d isn't supported.\n", level);
120 }
121
122 return shift;
Christian König50783142017-11-27 14:01:51 +0100123}
124
125/**
Christian König72a7ec52016-10-19 11:03:57 +0200126 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 *
128 * @adev: amdgpu_device pointer
129 *
Christian König72a7ec52016-10-19 11:03:57 +0200130 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 */
Christian König72a7ec52016-10-19 11:03:57 +0200132static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
133 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134{
Chunming Zhou196f7482017-12-13 14:22:54 +0800135 unsigned shift = amdgpu_vm_level_shift(adev,
136 adev->vm_manager.root_level);
Christian König0410c5e2017-11-20 14:29:01 +0100137
Chunming Zhou196f7482017-12-13 14:22:54 +0800138 if (level == adev->vm_manager.root_level)
Christian König72a7ec52016-10-19 11:03:57 +0200139 /* For the root directory */
Christian König0410c5e2017-11-20 14:29:01 +0100140 return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
Chunming Zhou196f7482017-12-13 14:22:54 +0800141 else if (level != AMDGPU_VM_PTB)
Christian König0410c5e2017-11-20 14:29:01 +0100142 /* Everything in between */
143 return 512;
144 else
Christian König72a7ec52016-10-19 11:03:57 +0200145 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800146 return AMDGPU_VM_PTE_COUNT(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147}
148
149/**
Christian König72a7ec52016-10-19 11:03:57 +0200150 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151 *
152 * @adev: amdgpu_device pointer
153 *
Christian König72a7ec52016-10-19 11:03:57 +0200154 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155 */
Christian König72a7ec52016-10-19 11:03:57 +0200156static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157{
Christian König72a7ec52016-10-19 11:03:57 +0200158 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159}
160
161/**
Christian König56467eb2015-12-11 15:16:32 +0100162 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163 *
164 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100165 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100166 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167 *
168 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100169 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170 */
Christian König56467eb2015-12-11 15:16:32 +0100171void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
172 struct list_head *validated,
173 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174{
Christian König3f3333f2017-08-03 14:02:13 +0200175 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100176 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200177 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100178 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100179 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100180 list_add(&entry->tv.head, validated);
181}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182
Christian König56467eb2015-12-11 15:16:32 +0100183/**
Christian Königf7da30d2016-09-28 12:03:04 +0200184 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100185 *
Christian König5a712a82016-06-21 16:28:15 +0200186 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100187 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200188 * @validate: callback to do the validation
189 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190 *
Christian Königf7da30d2016-09-28 12:03:04 +0200191 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192 */
Christian Königf7da30d2016-09-28 12:03:04 +0200193int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
194 int (*validate)(void *p, struct amdgpu_bo *bo),
195 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196{
Christian König3f3333f2017-08-03 14:02:13 +0200197 struct ttm_bo_global *glob = adev->mman.bdev.glob;
198 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199
Christian König3f3333f2017-08-03 14:02:13 +0200200 spin_lock(&vm->status_lock);
201 while (!list_empty(&vm->evicted)) {
202 struct amdgpu_vm_bo_base *bo_base;
203 struct amdgpu_bo *bo;
Christian König5a712a82016-06-21 16:28:15 +0200204
Christian König3f3333f2017-08-03 14:02:13 +0200205 bo_base = list_first_entry(&vm->evicted,
206 struct amdgpu_vm_bo_base,
207 vm_status);
208 spin_unlock(&vm->status_lock);
Christian Königeceb8a12016-01-11 15:35:21 +0100209
Christian König3f3333f2017-08-03 14:02:13 +0200210 bo = bo_base->bo;
211 BUG_ON(!bo);
212 if (bo->parent) {
213 r = validate(param, bo);
214 if (r)
215 return r;
Christian König34d7be52017-08-24 12:32:55 +0200216
Christian König3f3333f2017-08-03 14:02:13 +0200217 spin_lock(&glob->lru_lock);
218 ttm_bo_move_to_lru_tail(&bo->tbo);
219 if (bo->shadow)
220 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
221 spin_unlock(&glob->lru_lock);
222 }
223
Christian König73fb16e2017-08-16 11:13:48 +0200224 if (bo->tbo.type == ttm_bo_type_kernel &&
225 vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +0200226 r = amdgpu_bo_kmap(bo, NULL);
227 if (r)
228 return r;
229 }
230
231 spin_lock(&vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200232 if (bo->tbo.type != ttm_bo_type_kernel)
233 list_move(&bo_base->vm_status, &vm->moved);
234 else
235 list_move(&bo_base->vm_status, &vm->relocated);
Christian König3f3333f2017-08-03 14:02:13 +0200236 }
237 spin_unlock(&vm->status_lock);
Christian König34d7be52017-08-24 12:32:55 +0200238
239 return 0;
240}
241
242/**
243 * amdgpu_vm_ready - check VM is ready for updates
244 *
Christian König34d7be52017-08-24 12:32:55 +0200245 * @vm: VM to check
246 *
247 * Check if all VM PDs/PTs are ready for updates
248 */
Christian König3f3333f2017-08-03 14:02:13 +0200249bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200250{
Christian König3f3333f2017-08-03 14:02:13 +0200251 bool ready;
Christian König34d7be52017-08-24 12:32:55 +0200252
Christian König3f3333f2017-08-03 14:02:13 +0200253 spin_lock(&vm->status_lock);
254 ready = list_empty(&vm->evicted);
255 spin_unlock(&vm->status_lock);
256
257 return ready;
Christian Königeceb8a12016-01-11 15:35:21 +0100258}
259
260/**
Christian König13307f72018-01-24 17:19:04 +0100261 * amdgpu_vm_clear_bo - initially clear the PDs/PTs
262 *
263 * @adev: amdgpu_device pointer
264 * @bo: BO to clear
265 * @level: level this BO is at
266 *
267 * Root PD needs to be reserved when calling this.
268 */
269static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
270 struct amdgpu_vm *vm,
271 struct amdgpu_bo *bo,
272 unsigned level)
273{
274 struct ttm_operation_ctx ctx = { true, false };
275 struct dma_fence *fence = NULL;
276 uint64_t addr, init_value;
277 struct amdgpu_ring *ring;
278 struct amdgpu_job *job;
279 unsigned entries;
280 int r;
281
282 if (vm->pte_support_ats) {
283 init_value = AMDGPU_PTE_DEFAULT_ATC;
284 if (level != AMDGPU_VM_PTB)
285 init_value |= AMDGPU_PDE_PTE;
286 } else {
287 init_value = 0;
288 }
289
290 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
291
292 r = reservation_object_reserve_shared(bo->tbo.resv);
293 if (r)
294 return r;
295
296 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
297 if (r)
298 goto error;
299
300 addr = amdgpu_bo_gpu_offset(bo);
301 entries = amdgpu_bo_size(bo) / 8;
302
303 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
304 if (r)
305 goto error;
306
307 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
308 entries, 0, init_value);
309 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
310
311 WARN_ON(job->ibs[0].length_dw > 64);
312 r = amdgpu_job_submit(job, ring, &vm->entity,
313 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
314 if (r)
315 goto error_free;
316
317 amdgpu_bo_fence(bo, fence, true);
318 dma_fence_put(fence);
319 return 0;
320
321error_free:
322 amdgpu_job_free(job);
323
324error:
325 return r;
326}
327
328/**
Christian Königf566ceb2016-10-27 20:04:38 +0200329 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
330 *
331 * @adev: amdgpu_device pointer
332 * @vm: requested vm
333 * @saddr: start of the address range
334 * @eaddr: end of the address range
335 *
336 * Make sure the page directories and page tables are allocated
337 */
338static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
339 struct amdgpu_vm *vm,
340 struct amdgpu_vm_pt *parent,
341 uint64_t saddr, uint64_t eaddr,
342 unsigned level)
343{
Christian König50783142017-11-27 14:01:51 +0100344 unsigned shift = amdgpu_vm_level_shift(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200345 unsigned pt_idx, from, to;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400346 u64 flags;
Christian König13307f72018-01-24 17:19:04 +0100347 int r;
Christian Königf566ceb2016-10-27 20:04:38 +0200348
349 if (!parent->entries) {
350 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
351
Michal Hocko20981052017-05-17 14:23:12 +0200352 parent->entries = kvmalloc_array(num_entries,
353 sizeof(struct amdgpu_vm_pt),
354 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200355 if (!parent->entries)
356 return -ENOMEM;
357 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
358 }
359
Felix Kuehling1866bac2017-03-28 20:36:12 -0400360 from = saddr >> shift;
361 to = eaddr >> shift;
362 if (from >= amdgpu_vm_num_entries(adev, level) ||
363 to >= amdgpu_vm_num_entries(adev, level))
364 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200365
Christian Königf566ceb2016-10-27 20:04:38 +0200366 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400367 saddr = saddr & ((1 << shift) - 1);
368 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200369
Christian König13307f72018-01-24 17:19:04 +0100370 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400371 if (vm->use_cpu_for_update)
372 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
373 else
374 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
375 AMDGPU_GEM_CREATE_SHADOW);
376
Christian Königf566ceb2016-10-27 20:04:38 +0200377 /* walk over the address space and allocate the page tables */
378 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200379 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200380 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
381 struct amdgpu_bo *pt;
382
Christian König3f3333f2017-08-03 14:02:13 +0200383 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200384 r = amdgpu_bo_create(adev,
385 amdgpu_vm_bo_size(adev, level),
386 AMDGPU_GPU_PAGE_SIZE, true,
Christian König13307f72018-01-24 17:19:04 +0100387 AMDGPU_GEM_DOMAIN_VRAM, flags,
388 NULL, resv, 0, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200389 if (r)
390 return r;
391
Christian König13307f72018-01-24 17:19:04 +0100392 r = amdgpu_vm_clear_bo(adev, vm, pt, level);
393 if (r) {
394 amdgpu_bo_unref(&pt);
395 return r;
396 }
397
Christian König0a096fb2017-07-12 10:01:48 +0200398 if (vm->use_cpu_for_update) {
399 r = amdgpu_bo_kmap(pt, NULL);
400 if (r) {
401 amdgpu_bo_unref(&pt);
402 return r;
403 }
404 }
405
Christian Königf566ceb2016-10-27 20:04:38 +0200406 /* Keep a reference to the root directory to avoid
407 * freeing them up in the wrong order.
408 */
Christian König0f2fc432017-08-31 10:46:20 +0200409 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200410
Christian König3f3333f2017-08-03 14:02:13 +0200411 entry->base.vm = vm;
412 entry->base.bo = pt;
413 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200414 spin_lock(&vm->status_lock);
415 list_add(&entry->base.vm_status, &vm->relocated);
416 spin_unlock(&vm->status_lock);
Christian Königf566ceb2016-10-27 20:04:38 +0200417 }
418
Chunming Zhou196f7482017-12-13 14:22:54 +0800419 if (level < AMDGPU_VM_PTB) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400420 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
421 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
422 ((1 << shift) - 1);
423 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
424 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200425 if (r)
426 return r;
427 }
428 }
429
430 return 0;
431}
432
Christian König663e4572017-03-13 10:13:37 +0100433/**
434 * amdgpu_vm_alloc_pts - Allocate page tables.
435 *
436 * @adev: amdgpu_device pointer
437 * @vm: VM to allocate page tables for
438 * @saddr: Start address which needs to be allocated
439 * @size: Size from start address we need.
440 *
441 * Make sure the page tables are allocated.
442 */
443int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
444 struct amdgpu_vm *vm,
445 uint64_t saddr, uint64_t size)
446{
Felix Kuehling22770e52017-03-28 20:24:53 -0400447 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100448 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100449
450 /* validate the parameters */
451 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
452 return -EINVAL;
453
454 eaddr = saddr + size - 1;
455 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
456 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400457 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100458 last_pfn, adev->vm_manager.max_pfn);
459 return -EINVAL;
460 }
461
462 saddr /= AMDGPU_GPU_PAGE_SIZE;
463 eaddr /= AMDGPU_GPU_PAGE_SIZE;
464
Chunming Zhou196f7482017-12-13 14:22:54 +0800465 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
466 adev->vm_manager.root_level);
Christian König663e4572017-03-13 10:13:37 +0100467}
468
Christian König641e9402017-04-03 13:59:25 +0200469/**
Alex Xiee59c0202017-06-01 09:42:59 -0400470 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
471 *
472 * @adev: amdgpu_device pointer
473 */
474void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
475{
476 const struct amdgpu_ip_block *ip_block;
477 bool has_compute_vm_bug;
478 struct amdgpu_ring *ring;
479 int i;
480
481 has_compute_vm_bug = false;
482
Alex Deucher2990a1f2017-12-15 16:18:00 -0500483 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
Alex Xiee59c0202017-06-01 09:42:59 -0400484 if (ip_block) {
485 /* Compute has a VM bug for GFX version < 7.
486 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
487 if (ip_block->version->major <= 7)
488 has_compute_vm_bug = true;
489 else if (ip_block->version->major == 8)
490 if (adev->gfx.mec_fw_version < 673)
491 has_compute_vm_bug = true;
492 }
493
494 for (i = 0; i < adev->num_rings; i++) {
495 ring = adev->rings[i];
496 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
497 /* only compute rings */
498 ring->has_compute_vm_bug = has_compute_vm_bug;
499 else
500 ring->has_compute_vm_bug = false;
501 }
502}
503
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400504bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
505 struct amdgpu_job *job)
506{
507 struct amdgpu_device *adev = ring->adev;
508 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100509 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
510 struct amdgpu_vmid *id;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400511 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400512 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400513
Christian Königc4f46f22017-12-18 17:08:25 +0100514 if (job->vmid == 0)
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400515 return false;
Christian Königc4f46f22017-12-18 17:08:25 +0100516 id = &id_mgr->ids[job->vmid];
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400517 gds_switch_needed = ring->funcs->emit_gds_switch && (
518 id->gds_base != job->gds_base ||
519 id->gds_size != job->gds_size ||
520 id->gws_base != job->gws_base ||
521 id->gws_size != job->gws_size ||
522 id->oa_base != job->oa_base ||
523 id->oa_size != job->oa_size);
524
Christian König620f7742017-12-18 16:53:03 +0100525 if (amdgpu_vmid_had_gpu_reset(adev, id))
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400526 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400527
528 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400529}
530
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400531static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
532{
Christian König770d13b2018-01-12 14:52:22 +0100533 return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500534}
535
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536/**
537 * amdgpu_vm_flush - hardware flush the vm
538 *
539 * @ring: ring to use for flush
Christian Königc4f46f22017-12-18 17:08:25 +0100540 * @vmid: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100541 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 *
Christian König4ff37a82016-02-26 16:18:26 +0100543 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800545int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546{
Christian König971fe9a92016-03-01 15:09:25 +0100547 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200548 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100549 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian Königc4f46f22017-12-18 17:08:25 +0100550 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
Christian Königd564a062016-03-01 15:51:53 +0100551 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800552 id->gds_base != job->gds_base ||
553 id->gds_size != job->gds_size ||
554 id->gws_base != job->gws_base ||
555 id->gws_size != job->gws_size ||
556 id->oa_base != job->oa_base ||
557 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800558 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200559 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100560 int r;
Christian Königd564a062016-03-01 15:51:53 +0100561
Christian König620f7742017-12-18 16:53:03 +0100562 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
Christian Königf7d015b2017-04-03 14:28:26 +0200563 gds_switch_needed = true;
564 vm_flush_needed = true;
565 }
Christian König971fe9a92016-03-01 15:09:25 +0100566
Monk Liu8fdf0742017-06-06 17:25:13 +0800567 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200568 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100569
Christian Königc0e51932017-04-03 14:16:07 +0200570 if (ring->funcs->init_cond_exec)
571 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100572
Monk Liu8fdf0742017-06-06 17:25:13 +0800573 if (need_pipe_sync)
574 amdgpu_ring_emit_pipeline_sync(ring);
575
Christian Königf7d015b2017-04-03 14:28:26 +0200576 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200577 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800578
Christian Königc4f46f22017-12-18 17:08:25 +0100579 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian König5a4633c2018-01-08 14:48:11 +0100580 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->pasid,
581 job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800582
Christian Königc0e51932017-04-03 14:16:07 +0200583 r = amdgpu_fence_emit(ring, &fence);
584 if (r)
585 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800586
Christian König76456702017-04-06 17:52:39 +0200587 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200588 dma_fence_put(id->last_flush);
589 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800590 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200591 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200592 }
Monk Liue9d672b2017-03-15 12:18:57 +0800593
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800594 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200595 id->gds_base = job->gds_base;
596 id->gds_size = job->gds_size;
597 id->gws_base = job->gws_base;
598 id->gws_size = job->gws_size;
599 id->oa_base = job->oa_base;
600 id->oa_size = job->oa_size;
Christian Königc4f46f22017-12-18 17:08:25 +0100601 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
Christian Königc0e51932017-04-03 14:16:07 +0200602 job->gds_size, job->gws_base,
603 job->gws_size, job->oa_base,
604 job->oa_size);
605 }
606
607 if (ring->funcs->patch_cond_exec)
608 amdgpu_ring_patch_cond_exec(ring, patch_offset);
609
610 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
611 if (ring->funcs->emit_switch_buffer) {
612 amdgpu_ring_emit_switch_buffer(ring);
613 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 }
Christian König41d9eb22016-03-01 16:46:18 +0100615 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100616}
617
618/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
620 *
621 * @vm: requested vm
622 * @bo: requested buffer object
623 *
Christian König8843dbb2016-01-26 12:17:11 +0100624 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 * Search inside the @bos vm list for the requested vm
626 * Returns the found bo_va or NULL if none is found
627 *
628 * Object has to be reserved!
629 */
630struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
631 struct amdgpu_bo *bo)
632{
633 struct amdgpu_bo_va *bo_va;
634
Christian Königec681542017-08-01 10:51:43 +0200635 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
636 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 return bo_va;
638 }
639 }
640 return NULL;
641}
642
643/**
Christian Königafef8b82016-08-12 13:29:18 +0200644 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 *
Christian König29efc4f2016-08-04 14:52:50 +0200646 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100647 * @bo: PD/PT to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 * @pe: addr of the page entry
649 * @addr: dst addr to write into pe
650 * @count: number of page entries to update
651 * @incr: increase next addr by incr bytes
652 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653 *
654 * Traces the parameters and calls the right asic functions
655 * to setup the page table using the DMA.
656 */
Christian Königafef8b82016-08-12 13:29:18 +0200657static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100658 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200659 uint64_t pe, uint64_t addr,
660 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800661 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662{
Christian König373ac642018-01-16 16:54:25 +0100663 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200664 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665
Christian Königafef8b82016-08-12 13:29:18 +0200666 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200667 amdgpu_vm_write_pte(params->adev, params->ib, pe,
668 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400669
670 } else {
Christian König27c5f362016-08-04 15:02:49 +0200671 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 count, incr, flags);
673 }
674}
675
676/**
Christian Königafef8b82016-08-12 13:29:18 +0200677 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
678 *
679 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100680 * @bo: PD/PT to update
Christian Königafef8b82016-08-12 13:29:18 +0200681 * @pe: addr of the page entry
682 * @addr: dst addr to write into pe
683 * @count: number of page entries to update
684 * @incr: increase next addr by incr bytes
685 * @flags: hw access flags
686 *
687 * Traces the parameters and calls the DMA function to copy the PTEs.
688 */
689static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100690 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200691 uint64_t pe, uint64_t addr,
692 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800693 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200694{
Christian Königec2f05f2016-09-25 16:11:52 +0200695 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200696
Christian König373ac642018-01-16 16:54:25 +0100697 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200698 trace_amdgpu_vm_copy_ptes(pe, src, count);
699
700 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200701}
702
703/**
Christian Königb07c9d22015-11-30 13:26:07 +0100704 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 *
Christian Königb07c9d22015-11-30 13:26:07 +0100706 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 * @addr: the unmapped addr
708 *
709 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100710 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200712static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713{
714 uint64_t result;
715
Christian Königde9ea7b2016-08-12 11:33:30 +0200716 /* page table offset */
717 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400718
Christian Königde9ea7b2016-08-12 11:33:30 +0200719 /* in case cpu page size != gpu page size*/
720 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100721
722 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723
724 return result;
725}
726
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400727/**
728 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
729 *
730 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100731 * @bo: PD/PT to update
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400732 * @pe: kmap addr of the page entry
733 * @addr: dst addr to write into pe
734 * @count: number of page entries to update
735 * @incr: increase next addr by incr bytes
736 * @flags: hw access flags
737 *
738 * Write count number of PT/PD entries directly.
739 */
740static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100741 struct amdgpu_bo *bo,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400742 uint64_t pe, uint64_t addr,
743 unsigned count, uint32_t incr,
744 uint64_t flags)
745{
746 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400747 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400748
Christian König373ac642018-01-16 16:54:25 +0100749 pe += (unsigned long)amdgpu_bo_kptr(bo);
750
Christian König03918b32017-07-11 17:15:37 +0200751 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
752
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400753 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400754 value = params->pages_addr ?
755 amdgpu_vm_map_gart(params->pages_addr, addr) :
756 addr;
Christian König132f34e2018-01-12 15:26:08 +0100757 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
758 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400759 addr += incr;
760 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400761}
762
Christian Königa33cab72017-07-11 17:13:00 +0200763static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
764 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400765{
766 struct amdgpu_sync sync;
767 int r;
768
769 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -0400770 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400771 r = amdgpu_sync_wait(&sync, true);
772 amdgpu_sync_free(&sync);
773
774 return r;
775}
776
Christian Königf8991ba2016-09-16 15:36:49 +0200777/*
Christian König6989f242017-11-30 19:08:05 +0100778 * amdgpu_vm_update_pde - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200779 *
Christian König6989f242017-11-30 19:08:05 +0100780 * @param: parameters for the update
Christian Königf8991ba2016-09-16 15:36:49 +0200781 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200782 * @parent: parent directory
Christian König6989f242017-11-30 19:08:05 +0100783 * @entry: entry to update
Christian Königf8991ba2016-09-16 15:36:49 +0200784 *
Christian König6989f242017-11-30 19:08:05 +0100785 * Makes sure the requested entry in parent is up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200786 */
Christian König6989f242017-11-30 19:08:05 +0100787static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
788 struct amdgpu_vm *vm,
789 struct amdgpu_vm_pt *parent,
790 struct amdgpu_vm_pt *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400791{
Christian König373ac642018-01-16 16:54:25 +0100792 struct amdgpu_bo *bo = parent->base.bo, *pbo;
Christian König3de676d2017-11-29 13:27:26 +0100793 uint64_t pde, pt, flags;
794 unsigned level;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800795
Christian König6989f242017-11-30 19:08:05 +0100796 /* Don't update huge pages here */
797 if (entry->huge)
798 return;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400799
Christian König373ac642018-01-16 16:54:25 +0100800 for (level = 0, pbo = bo->parent; pbo; ++level)
Christian König3de676d2017-11-29 13:27:26 +0100801 pbo = pbo->parent;
802
Chunming Zhou196f7482017-12-13 14:22:54 +0800803 level += params->adev->vm_manager.root_level;
Christian König373ac642018-01-16 16:54:25 +0100804 pt = amdgpu_bo_gpu_offset(entry->base.bo);
Christian König3de676d2017-11-29 13:27:26 +0100805 flags = AMDGPU_PTE_VALID;
Christian König132f34e2018-01-12 15:26:08 +0100806 amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
Christian König373ac642018-01-16 16:54:25 +0100807 pde = (entry - parent->entries) * 8;
808 if (bo->shadow)
809 params->func(params, bo->shadow, pde, pt, 1, 0, flags);
810 params->func(params, bo, pde, pt, 1, 0, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400811}
812
Christian König194d2162016-10-12 15:13:52 +0200813/*
Christian König92456b92017-05-12 16:09:26 +0200814 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
815 *
816 * @parent: parent PD
817 *
818 * Mark all PD level as invalid after an error.
819 */
Christian König8f19cd72017-11-30 15:28:03 +0100820static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
821 struct amdgpu_vm *vm,
822 struct amdgpu_vm_pt *parent,
823 unsigned level)
Christian König92456b92017-05-12 16:09:26 +0200824{
Christian König8f19cd72017-11-30 15:28:03 +0100825 unsigned pt_idx, num_entries;
Christian König92456b92017-05-12 16:09:26 +0200826
827 /*
828 * Recurse into the subdirectories. This recursion is harmless because
829 * we only have a maximum of 5 layers.
830 */
Christian König8f19cd72017-11-30 15:28:03 +0100831 num_entries = amdgpu_vm_num_entries(adev, level);
832 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
Christian König92456b92017-05-12 16:09:26 +0200833 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
834
Christian König3f3333f2017-08-03 14:02:13 +0200835 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +0200836 continue;
837
Christian Königea097292017-08-09 14:15:46 +0200838 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +0200839 if (list_empty(&entry->base.vm_status))
840 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +0200841 spin_unlock(&vm->status_lock);
Christian König8f19cd72017-11-30 15:28:03 +0100842 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
Christian König92456b92017-05-12 16:09:26 +0200843 }
844}
845
846/*
Christian König194d2162016-10-12 15:13:52 +0200847 * amdgpu_vm_update_directories - make sure that all directories are valid
848 *
849 * @adev: amdgpu_device pointer
850 * @vm: requested vm
851 *
852 * Makes sure all directories are up to date.
853 * Returns 0 for success, error for failure.
854 */
855int amdgpu_vm_update_directories(struct amdgpu_device *adev,
856 struct amdgpu_vm *vm)
857{
Christian König6989f242017-11-30 19:08:05 +0100858 struct amdgpu_pte_update_params params;
859 struct amdgpu_job *job;
860 unsigned ndw = 0;
Dan Carpenter78aa02c2017-09-30 11:14:13 +0300861 int r = 0;
Christian König92456b92017-05-12 16:09:26 +0200862
Christian König6989f242017-11-30 19:08:05 +0100863 if (list_empty(&vm->relocated))
864 return 0;
865
866restart:
867 memset(&params, 0, sizeof(params));
868 params.adev = adev;
869
870 if (vm->use_cpu_for_update) {
871 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
872 if (unlikely(r))
873 return r;
874
875 params.func = amdgpu_vm_cpu_set_ptes;
876 } else {
877 ndw = 512 * 8;
878 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
879 if (r)
880 return r;
881
882 params.ib = &job->ibs[0];
883 params.func = amdgpu_vm_do_set_ptes;
884 }
885
Christian Königea097292017-08-09 14:15:46 +0200886 spin_lock(&vm->status_lock);
887 while (!list_empty(&vm->relocated)) {
Christian König6989f242017-11-30 19:08:05 +0100888 struct amdgpu_vm_bo_base *bo_base, *parent;
889 struct amdgpu_vm_pt *pt, *entry;
Christian Königea097292017-08-09 14:15:46 +0200890 struct amdgpu_bo *bo;
891
892 bo_base = list_first_entry(&vm->relocated,
893 struct amdgpu_vm_bo_base,
894 vm_status);
Christian König6989f242017-11-30 19:08:05 +0100895 list_del_init(&bo_base->vm_status);
Christian Königea097292017-08-09 14:15:46 +0200896 spin_unlock(&vm->status_lock);
897
898 bo = bo_base->bo->parent;
Christian König6989f242017-11-30 19:08:05 +0100899 if (!bo) {
Christian Königea097292017-08-09 14:15:46 +0200900 spin_lock(&vm->status_lock);
Christian König6989f242017-11-30 19:08:05 +0100901 continue;
Christian Königea097292017-08-09 14:15:46 +0200902 }
Christian König6989f242017-11-30 19:08:05 +0100903
904 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
905 bo_list);
906 pt = container_of(parent, struct amdgpu_vm_pt, base);
907 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
908
909 amdgpu_vm_update_pde(&params, vm, pt, entry);
910
911 spin_lock(&vm->status_lock);
912 if (!vm->use_cpu_for_update &&
913 (ndw - params.ib->length_dw) < 32)
914 break;
Christian Königea097292017-08-09 14:15:46 +0200915 }
916 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +0200917
Christian König68c62302017-07-11 17:23:29 +0200918 if (vm->use_cpu_for_update) {
919 /* Flush HDP */
920 mb();
Christian König69882562018-01-19 14:17:40 +0100921 amdgpu_asic_flush_hdp(adev, NULL);
Christian König6989f242017-11-30 19:08:05 +0100922 } else if (params.ib->length_dw == 0) {
923 amdgpu_job_free(job);
924 } else {
925 struct amdgpu_bo *root = vm->root.base.bo;
926 struct amdgpu_ring *ring;
927 struct dma_fence *fence;
928
929 ring = container_of(vm->entity.sched, struct amdgpu_ring,
930 sched);
931
932 amdgpu_ring_pad_ib(ring, params.ib);
933 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
934 AMDGPU_FENCE_OWNER_VM, false);
Christian König6989f242017-11-30 19:08:05 +0100935 WARN_ON(params.ib->length_dw > ndw);
936 r = amdgpu_job_submit(job, ring, &vm->entity,
937 AMDGPU_FENCE_OWNER_VM, &fence);
938 if (r)
939 goto error;
940
941 amdgpu_bo_fence(root, fence, true);
942 dma_fence_put(vm->last_update);
943 vm->last_update = fence;
Christian König68c62302017-07-11 17:23:29 +0200944 }
945
Christian König6989f242017-11-30 19:08:05 +0100946 if (!list_empty(&vm->relocated))
947 goto restart;
948
949 return 0;
950
951error:
Chunming Zhou196f7482017-12-13 14:22:54 +0800952 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
953 adev->vm_manager.root_level);
Christian König6989f242017-11-30 19:08:05 +0100954 amdgpu_job_free(job);
Christian König92456b92017-05-12 16:09:26 +0200955 return r;
Christian König194d2162016-10-12 15:13:52 +0200956}
957
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400958/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400959 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +0200960 *
961 * @p: see amdgpu_pte_update_params definition
962 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400963 * @entry: resulting entry or NULL
964 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +0200965 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400966 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +0200967 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400968void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
969 struct amdgpu_vm_pt **entry,
970 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +0200971{
Chunming Zhou196f7482017-12-13 14:22:54 +0800972 unsigned level = p->adev->vm_manager.root_level;
Christian König4e2cb642016-10-25 15:52:28 +0200973
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400974 *parent = NULL;
975 *entry = &p->vm->root;
976 while ((*entry)->entries) {
Christian Könige3a1b322017-12-01 13:28:46 +0100977 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
Christian König50783142017-11-27 14:01:51 +0100978
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400979 *parent = *entry;
Christian Könige3a1b322017-12-01 13:28:46 +0100980 *entry = &(*entry)->entries[addr >> shift];
981 addr &= (1ULL << shift) - 1;
Christian König4e2cb642016-10-25 15:52:28 +0200982 }
983
Chunming Zhou196f7482017-12-13 14:22:54 +0800984 if (level != AMDGPU_VM_PTB)
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400985 *entry = NULL;
986}
Christian König4e2cb642016-10-25 15:52:28 +0200987
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400988/**
989 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
990 *
991 * @p: see amdgpu_pte_update_params definition
992 * @entry: vm_pt entry to check
993 * @parent: parent entry
994 * @nptes: number of PTEs updated with this operation
995 * @dst: destination address where the PTEs should point to
996 * @flags: access flags fro the PTEs
997 *
998 * Check if we can update the PD with a huge page.
999 */
Christian Königec5207c2017-08-03 19:24:06 +02001000static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1001 struct amdgpu_vm_pt *entry,
1002 struct amdgpu_vm_pt *parent,
1003 unsigned nptes, uint64_t dst,
1004 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001005{
Christian König373ac642018-01-16 16:54:25 +01001006 uint64_t pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001007
1008 /* In the case of a mixed PT the PDE must point to it*/
Christian König3cc1d3e2017-12-21 15:47:28 +01001009 if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
1010 nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
Christian König4ab40162017-08-03 20:30:50 +02001011 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001012 flags |= AMDGPU_PDE_PTE;
1013 }
1014
Christian König3cc1d3e2017-12-21 15:47:28 +01001015 if (!(flags & AMDGPU_PDE_PTE)) {
1016 if (entry->huge) {
1017 /* Add the entry to the relocated list to update it. */
1018 entry->huge = false;
1019 spin_lock(&p->vm->status_lock);
1020 list_move(&entry->base.vm_status, &p->vm->relocated);
1021 spin_unlock(&p->vm->status_lock);
1022 }
Christian Königec5207c2017-08-03 19:24:06 +02001023 return;
Christian König3cc1d3e2017-12-21 15:47:28 +01001024 }
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001025
Christian König3cc1d3e2017-12-21 15:47:28 +01001026 entry->huge = true;
Christian König132f34e2018-01-12 15:26:08 +01001027 amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
Christian König3de676d2017-11-29 13:27:26 +01001028
Christian König373ac642018-01-16 16:54:25 +01001029 pde = (entry - parent->entries) * 8;
1030 if (parent->base.bo->shadow)
1031 p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
1032 p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
Christian König4e2cb642016-10-25 15:52:28 +02001033}
1034
1035/**
Christian König92696dd2016-08-05 13:56:35 +02001036 * amdgpu_vm_update_ptes - make sure that page tables are valid
1037 *
1038 * @params: see amdgpu_pte_update_params definition
1039 * @vm: requested vm
1040 * @start: start of GPU address range
1041 * @end: end of GPU address range
1042 * @dst: destination address to map to, the next dst inside the function
1043 * @flags: mapping flags
1044 *
1045 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001046 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001047 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001048static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001049 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001050 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001051{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001052 struct amdgpu_device *adev = params->adev;
1053 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001054
Christian König301654a2017-05-16 14:30:27 +02001055 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001056 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001057 unsigned nptes;
Christian König92696dd2016-08-05 13:56:35 +02001058
1059 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001060 for (addr = start; addr < end; addr += nptes,
1061 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1062 struct amdgpu_vm_pt *entry, *parent;
1063
1064 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1065 if (!entry)
1066 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001067
Christian König92696dd2016-08-05 13:56:35 +02001068 if ((addr & ~mask) == (end & ~mask))
1069 nptes = end - addr;
1070 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001071 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001072
Christian Königec5207c2017-08-03 19:24:06 +02001073 amdgpu_vm_handle_huge_pages(params, entry, parent,
1074 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001075 /* We don't need to update PTEs for huge pages */
Christian König78eb2f02017-11-30 15:41:28 +01001076 if (entry->huge)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001077 continue;
1078
Christian König3f3333f2017-08-03 14:02:13 +02001079 pt = entry->base.bo;
Christian König373ac642018-01-16 16:54:25 +01001080 pe_start = (addr & mask) * 8;
1081 if (pt->shadow)
1082 params->func(params, pt->shadow, pe_start, dst, nptes,
1083 AMDGPU_GPU_PAGE_SIZE, flags);
1084 params->func(params, pt, pe_start, dst, nptes,
Christian König301654a2017-05-16 14:30:27 +02001085 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001086 }
1087
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001088 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001089}
1090
1091/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001092 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1093 *
Christian König29efc4f2016-08-04 14:52:50 +02001094 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001095 * @vm: requested vm
1096 * @start: first PTE to handle
1097 * @end: last PTE to handle
1098 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001100 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001102static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001103 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001104 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001105{
1106 /**
1107 * The MC L1 TLB supports variable sized pages, based on a fragment
1108 * field in the PTE. When this field is set to a non-zero value, page
1109 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1110 * flags are considered valid for all PTEs within the fragment range
1111 * and corresponding mappings are assumed to be physically contiguous.
1112 *
1113 * The L1 TLB can store a single PTE for the whole fragment,
1114 * significantly increasing the space available for translation
1115 * caching. This leads to large improvements in throughput when the
1116 * TLB is under pressure.
1117 *
1118 * The L2 TLB distributes small and large fragments into two
1119 * asymmetric partitions. The large fragment cache is significantly
1120 * larger. Thus, we try to use large fragments wherever possible.
1121 * Userspace can support this by aligning virtual base address and
1122 * allocation size to the fragment size.
1123 */
Roger He6849d472017-08-30 13:01:19 +08001124 unsigned max_frag = params->adev->vm_manager.fragment_size;
1125 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001126
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001127 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001128 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001129 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130
Roger He6849d472017-08-30 13:01:19 +08001131 while (start != end) {
1132 uint64_t frag_flags, frag_end;
1133 unsigned frag;
1134
1135 /* This intentionally wraps around if no bit is set */
1136 frag = min((unsigned)ffs(start) - 1,
1137 (unsigned)fls64(end - start) - 1);
1138 if (frag >= max_frag) {
1139 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1140 frag_end = end & ~((1ULL << max_frag) - 1);
1141 } else {
1142 frag_flags = AMDGPU_PTE_FRAG(frag);
1143 frag_end = start + (1 << frag);
1144 }
1145
1146 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1147 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001148 if (r)
1149 return r;
Roger He6849d472017-08-30 13:01:19 +08001150
1151 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1152 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153 }
1154
Roger He6849d472017-08-30 13:01:19 +08001155 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156}
1157
1158/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1160 *
1161 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001162 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001163 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001164 * @vm: requested vm
1165 * @start: start of mapped range
1166 * @last: last mapped entry
1167 * @flags: flags for the entries
1168 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001169 * @fence: optional resulting fence
1170 *
Christian Königa14faa62016-01-25 14:27:31 +01001171 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173 */
1174static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001175 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001176 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001178 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001179 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001180 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181{
Christian König2d55e452016-02-08 17:37:38 +01001182 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001183 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001185 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001186 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001187 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 int r;
1189
Christian Königafef8b82016-08-12 13:29:18 +02001190 memset(&params, 0, sizeof(params));
1191 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001192 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001193
Christian Königa33cab72017-07-11 17:13:00 +02001194 /* sync to everything on unmapping */
1195 if (!(flags & AMDGPU_PTE_VALID))
1196 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1197
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001198 if (vm->use_cpu_for_update) {
1199 /* params.src is used as flag to indicate system Memory */
1200 if (pages_addr)
1201 params.src = ~0;
1202
1203 /* Wait for PT BOs to be free. PTs share the same resv. object
1204 * as the root PD BO
1205 */
Christian Königa33cab72017-07-11 17:13:00 +02001206 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001207 if (unlikely(r))
1208 return r;
1209
1210 params.func = amdgpu_vm_cpu_set_ptes;
1211 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001212 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1213 addr, flags);
1214 }
1215
Christian König2d55e452016-02-08 17:37:38 +01001216 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001217
Christian Königa14faa62016-01-25 14:27:31 +01001218 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219
1220 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001221 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001223 *
1224 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 */
Emily Deng104bd2c2017-12-29 13:13:08 +08001226 if (vm->root.base.bo->shadow)
1227 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1228 else
1229 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230
1231 /* padding, etc. */
1232 ndw = 64;
1233
Christian König570144c2017-08-30 15:38:45 +02001234 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001235 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001236 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001237
Christian Königb0456f92016-08-11 14:06:54 +02001238 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 ndw += nptes * 2;
1240
Christian Königafef8b82016-08-12 13:29:18 +02001241 params.func = amdgpu_vm_do_copy_ptes;
1242
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001243 } else {
1244 /* set page commands needed */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001245 ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246
Roger He6849d472017-08-30 13:01:19 +08001247 /* extra commands for begin/end fragments */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001248 ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
1249 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001250
1251 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 }
1253
Christian Königd71518b2016-02-01 12:20:25 +01001254 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1255 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001257
Christian König29efc4f2016-08-04 14:52:50 +02001258 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001259
Christian König570144c2017-08-30 15:38:45 +02001260 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001261 uint64_t *pte;
1262 unsigned i;
1263
1264 /* Put the PTEs at the end of the IB. */
1265 i = ndw - nptes * 2;
1266 pte= (uint64_t *)&(job->ibs->ptr[i]);
1267 params.src = job->ibs->gpu_addr + i * 4;
1268
1269 for (i = 0; i < nptes; ++i) {
1270 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1271 AMDGPU_GPU_PAGE_SIZE);
1272 pte[i] |= flags;
1273 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001274 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001275 }
1276
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -05001277 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
Christian König3cabaa52016-06-06 10:17:58 +02001278 if (r)
1279 goto error_free;
1280
Christian König3f3333f2017-08-03 14:02:13 +02001281 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001282 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001283 if (r)
1284 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001285
Christian König3f3333f2017-08-03 14:02:13 +02001286 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001287 if (r)
1288 goto error_free;
1289
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001290 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1291 if (r)
1292 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001293
Christian König29efc4f2016-08-04 14:52:50 +02001294 amdgpu_ring_pad_ib(ring, params.ib);
1295 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001296 r = amdgpu_job_submit(job, ring, &vm->entity,
1297 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001298 if (r)
1299 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001300
Christian König3f3333f2017-08-03 14:02:13 +02001301 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001302 dma_fence_put(*fence);
1303 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001304 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001305
1306error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001307 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001308 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001309}
1310
1311/**
Christian Königa14faa62016-01-25 14:27:31 +01001312 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1313 *
1314 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001315 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001316 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001317 * @vm: requested vm
1318 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001319 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001320 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001321 * @fence: optional resulting fence
1322 *
1323 * Split the mapping into smaller chunks so that each update fits
1324 * into a SDMA IB.
1325 * Returns 0 for success, -EINVAL for failure.
1326 */
1327static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001328 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001329 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001330 struct amdgpu_vm *vm,
1331 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001332 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001333 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001334 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001335{
Christian König9fc8fc72017-09-18 13:58:30 +02001336 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001337 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001338 int r;
1339
1340 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1341 * but in case of something, we filter the flags in first place
1342 */
1343 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1344 flags &= ~AMDGPU_PTE_READABLE;
1345 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1346 flags &= ~AMDGPU_PTE_WRITEABLE;
1347
Alex Xie15b31c52017-03-03 16:47:11 -05001348 flags &= ~AMDGPU_PTE_EXECUTABLE;
1349 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1350
Alex Xieb0fd18b2017-03-03 16:49:39 -05001351 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1352 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1353
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001354 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1355 (adev->asic_type >= CHIP_VEGA10)) {
1356 flags |= AMDGPU_PTE_PRT;
1357 flags &= ~AMDGPU_PTE_VALID;
1358 }
1359
Christian Königa14faa62016-01-25 14:27:31 +01001360 trace_amdgpu_vm_bo_update(mapping);
1361
Christian König63e0ba42016-08-16 17:38:37 +02001362 pfn = mapping->offset >> PAGE_SHIFT;
1363 if (nodes) {
1364 while (pfn >= nodes->size) {
1365 pfn -= nodes->size;
1366 ++nodes;
1367 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001368 }
Christian Königa14faa62016-01-25 14:27:31 +01001369
Christian König63e0ba42016-08-16 17:38:37 +02001370 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001371 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001372 uint64_t max_entries;
1373 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001374
Christian König63e0ba42016-08-16 17:38:37 +02001375 if (nodes) {
1376 addr = nodes->start << PAGE_SHIFT;
1377 max_entries = (nodes->size - pfn) *
1378 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1379 } else {
1380 addr = 0;
1381 max_entries = S64_MAX;
1382 }
Christian Königa14faa62016-01-25 14:27:31 +01001383
Christian König63e0ba42016-08-16 17:38:37 +02001384 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001385 uint64_t count;
1386
Christian König457e0fe2017-08-22 12:50:46 +02001387 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001388 for (count = 1; count < max_entries; ++count) {
1389 uint64_t idx = pfn + count;
1390
1391 if (pages_addr[idx] !=
1392 (pages_addr[idx - 1] + PAGE_SIZE))
1393 break;
1394 }
1395
1396 if (count < min_linear_pages) {
1397 addr = pfn << PAGE_SHIFT;
1398 dma_addr = pages_addr;
1399 } else {
1400 addr = pages_addr[pfn];
1401 max_entries = count;
1402 }
1403
Christian König63e0ba42016-08-16 17:38:37 +02001404 } else if (flags & AMDGPU_PTE_VALID) {
1405 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001406 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001407 }
Christian König63e0ba42016-08-16 17:38:37 +02001408
Christian Königa9f87f62017-03-30 14:03:59 +02001409 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001410 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001411 start, last, flags, addr,
1412 fence);
1413 if (r)
1414 return r;
1415
Christian König63e0ba42016-08-16 17:38:37 +02001416 pfn += last - start + 1;
1417 if (nodes && nodes->size == pfn) {
1418 pfn = 0;
1419 ++nodes;
1420 }
Christian Königa14faa62016-01-25 14:27:31 +01001421 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001422
Christian Königa9f87f62017-03-30 14:03:59 +02001423 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001424
1425 return 0;
1426}
1427
1428/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1430 *
1431 * @adev: amdgpu_device pointer
1432 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001433 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001434 *
1435 * Fill in the page table entries for @bo_va.
1436 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437 */
1438int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1439 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001440 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001441{
Christian Königec681542017-08-01 10:51:43 +02001442 struct amdgpu_bo *bo = bo_va->base.bo;
1443 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001445 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001446 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001447 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001448 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001449 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001450 int r;
1451
Christian Königec681542017-08-01 10:51:43 +02001452 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001453 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001454 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001455 exclusive = NULL;
1456 } else {
Christian König8358dce2016-03-30 10:50:25 +02001457 struct ttm_dma_tt *ttm;
1458
Christian Königec681542017-08-01 10:51:43 +02001459 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001460 nodes = mem->mm_node;
1461 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001462 ttm = container_of(bo_va->base.bo->tbo.ttm,
1463 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001464 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001465 }
Christian Königec681542017-08-01 10:51:43 +02001466 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001467 }
1468
Christian König457e0fe2017-08-22 12:50:46 +02001469 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001470 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001471 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001472 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001473
Christian König4e55eb32017-09-11 16:54:59 +02001474 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1475 last_update = &vm->last_update;
1476 else
1477 last_update = &bo_va->last_pt_update;
1478
Christian König3d7d4d32017-08-23 16:13:33 +02001479 if (!clear && bo_va->base.moved) {
1480 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001481 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001482
Christian Königcb7b6ec2017-08-15 17:08:12 +02001483 } else if (bo_va->cleared != clear) {
1484 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001485 }
Christian König7fc11952015-07-30 11:53:42 +02001486
1487 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001488 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001489 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001490 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491 if (r)
1492 return r;
1493 }
1494
Christian König68c62302017-07-11 17:23:29 +02001495 if (vm->use_cpu_for_update) {
1496 /* Flush HDP */
1497 mb();
Christian König69882562018-01-19 14:17:40 +01001498 amdgpu_asic_flush_hdp(adev, NULL);
Christian König68c62302017-07-11 17:23:29 +02001499 }
1500
Christian Königcb7b6ec2017-08-15 17:08:12 +02001501 spin_lock(&vm->status_lock);
1502 list_del_init(&bo_va->base.vm_status);
1503 spin_unlock(&vm->status_lock);
1504
1505 list_splice_init(&bo_va->invalids, &bo_va->valids);
1506 bo_va->cleared = clear;
1507
1508 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1509 list_for_each_entry(mapping, &bo_va->valids, list)
1510 trace_amdgpu_vm_bo_mapping(mapping);
1511 }
1512
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513 return 0;
1514}
1515
1516/**
Christian König284710f2017-01-30 11:09:31 +01001517 * amdgpu_vm_update_prt_state - update the global PRT state
1518 */
1519static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1520{
1521 unsigned long flags;
1522 bool enable;
1523
1524 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001525 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König132f34e2018-01-12 15:26:08 +01001526 adev->gmc.gmc_funcs->set_prt(adev, enable);
Christian König284710f2017-01-30 11:09:31 +01001527 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1528}
1529
1530/**
Christian König4388fc22017-03-13 10:13:36 +01001531 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001532 */
1533static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1534{
Christian König132f34e2018-01-12 15:26:08 +01001535 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001536 return;
1537
Christian König451bc8e2017-02-14 16:02:52 +01001538 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1539 amdgpu_vm_update_prt_state(adev);
1540}
1541
1542/**
Christian König0b15f2f2017-02-14 15:47:03 +01001543 * amdgpu_vm_prt_put - drop a PRT user
1544 */
1545static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1546{
Christian König451bc8e2017-02-14 16:02:52 +01001547 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001548 amdgpu_vm_update_prt_state(adev);
1549}
1550
1551/**
Christian König451bc8e2017-02-14 16:02:52 +01001552 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001553 */
1554static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1555{
1556 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1557
Christian König0b15f2f2017-02-14 15:47:03 +01001558 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001559 kfree(cb);
1560}
1561
1562/**
Christian König451bc8e2017-02-14 16:02:52 +01001563 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1564 */
1565static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1566 struct dma_fence *fence)
1567{
Christian König4388fc22017-03-13 10:13:36 +01001568 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001569
Christian König132f34e2018-01-12 15:26:08 +01001570 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001571 return;
1572
1573 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001574 if (!cb) {
1575 /* Last resort when we are OOM */
1576 if (fence)
1577 dma_fence_wait(fence, false);
1578
Dan Carpenter486a68f2017-04-03 21:41:39 +03001579 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001580 } else {
1581 cb->adev = adev;
1582 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1583 amdgpu_vm_prt_cb))
1584 amdgpu_vm_prt_cb(fence, &cb->cb);
1585 }
1586}
1587
1588/**
Christian König284710f2017-01-30 11:09:31 +01001589 * amdgpu_vm_free_mapping - free a mapping
1590 *
1591 * @adev: amdgpu_device pointer
1592 * @vm: requested vm
1593 * @mapping: mapping to be freed
1594 * @fence: fence of the unmap operation
1595 *
1596 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1597 */
1598static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1599 struct amdgpu_vm *vm,
1600 struct amdgpu_bo_va_mapping *mapping,
1601 struct dma_fence *fence)
1602{
Christian König451bc8e2017-02-14 16:02:52 +01001603 if (mapping->flags & AMDGPU_PTE_PRT)
1604 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001605 kfree(mapping);
1606}
1607
1608/**
Christian König451bc8e2017-02-14 16:02:52 +01001609 * amdgpu_vm_prt_fini - finish all prt mappings
1610 *
1611 * @adev: amdgpu_device pointer
1612 * @vm: requested vm
1613 *
1614 * Register a cleanup callback to disable PRT support after VM dies.
1615 */
1616static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1617{
Christian König3f3333f2017-08-03 14:02:13 +02001618 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001619 struct dma_fence *excl, **shared;
1620 unsigned i, shared_count;
1621 int r;
1622
1623 r = reservation_object_get_fences_rcu(resv, &excl,
1624 &shared_count, &shared);
1625 if (r) {
1626 /* Not enough memory to grab the fence list, as last resort
1627 * block for all the fences to complete.
1628 */
1629 reservation_object_wait_timeout_rcu(resv, true, false,
1630 MAX_SCHEDULE_TIMEOUT);
1631 return;
1632 }
1633
1634 /* Add a callback for each fence in the reservation object */
1635 amdgpu_vm_prt_get(adev);
1636 amdgpu_vm_add_prt_cb(adev, excl);
1637
1638 for (i = 0; i < shared_count; ++i) {
1639 amdgpu_vm_prt_get(adev);
1640 amdgpu_vm_add_prt_cb(adev, shared[i]);
1641 }
1642
1643 kfree(shared);
1644}
1645
1646/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1648 *
1649 * @adev: amdgpu_device pointer
1650 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001651 * @fence: optional resulting fence (unchanged if no work needed to be done
1652 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001653 *
1654 * Make sure all freed BOs are cleared in the PT.
1655 * Returns 0 for success.
1656 *
1657 * PTs have to be reserved and mutex must be locked!
1658 */
1659int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001660 struct amdgpu_vm *vm,
1661 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001662{
1663 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001664 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001665 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001666 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667
1668 while (!list_empty(&vm->freed)) {
1669 mapping = list_first_entry(&vm->freed,
1670 struct amdgpu_bo_va_mapping, list);
1671 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001672
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001673 if (vm->pte_support_ats)
Yong Zhao6d16dac2017-08-31 15:55:00 -04001674 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001675
Christian König570144c2017-08-30 15:38:45 +02001676 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001677 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001678 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001679 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001680 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001681 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001682 return r;
Christian König284710f2017-01-30 11:09:31 +01001683 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001685
1686 if (fence && f) {
1687 dma_fence_put(*fence);
1688 *fence = f;
1689 } else {
1690 dma_fence_put(f);
1691 }
1692
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001693 return 0;
1694
1695}
1696
1697/**
Christian König73fb16e2017-08-16 11:13:48 +02001698 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001699 *
1700 * @adev: amdgpu_device pointer
1701 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02001702 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 *
Christian König73fb16e2017-08-16 11:13:48 +02001704 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 * Returns 0 for success.
1706 *
Christian König73fb16e2017-08-16 11:13:48 +02001707 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001708 */
Christian König73fb16e2017-08-16 11:13:48 +02001709int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02001710 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001711{
Christian König73fb16e2017-08-16 11:13:48 +02001712 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02001713 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001714
1715 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02001716 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02001717 struct amdgpu_bo_va *bo_va;
Christian Königec363e02017-09-01 20:34:27 +02001718 struct reservation_object *resv;
Christian König4e55eb32017-09-11 16:54:59 +02001719
Christian König27c7b9a2017-08-01 11:27:36 +02001720 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02001721 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001723
Christian Königec363e02017-09-01 20:34:27 +02001724 resv = bo_va->base.bo->tbo.resv;
1725
Christian König73fb16e2017-08-16 11:13:48 +02001726 /* Per VM BOs never need to bo cleared in the page tables */
Christian Königec363e02017-09-01 20:34:27 +02001727 if (resv == vm->root.base.bo->tbo.resv)
1728 clear = false;
1729 /* Try to reserve the BO to avoid clearing its ptes */
Christian König9b8cad22018-01-03 13:36:22 +01001730 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
Christian Königec363e02017-09-01 20:34:27 +02001731 clear = false;
1732 /* Somebody else is using the BO right now */
1733 else
1734 clear = true;
Christian König73fb16e2017-08-16 11:13:48 +02001735
1736 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737 if (r)
1738 return r;
1739
Christian Königec363e02017-09-01 20:34:27 +02001740 if (!clear && resv != vm->root.base.bo->tbo.resv)
1741 reservation_object_unlock(resv);
1742
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001743 spin_lock(&vm->status_lock);
1744 }
1745 spin_unlock(&vm->status_lock);
1746
Christian König91e1a522015-07-06 22:06:40 +02001747 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001748}
1749
1750/**
1751 * amdgpu_vm_bo_add - add a bo to a specific vm
1752 *
1753 * @adev: amdgpu_device pointer
1754 * @vm: requested vm
1755 * @bo: amdgpu buffer object
1756 *
Christian König8843dbb2016-01-26 12:17:11 +01001757 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758 * Add @bo to the list of bos associated with the vm
1759 * Returns newly added bo_va or NULL for failure
1760 *
1761 * Object has to be reserved!
1762 */
1763struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1764 struct amdgpu_vm *vm,
1765 struct amdgpu_bo *bo)
1766{
1767 struct amdgpu_bo_va *bo_va;
1768
1769 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1770 if (bo_va == NULL) {
1771 return NULL;
1772 }
Christian Königec681542017-08-01 10:51:43 +02001773 bo_va->base.vm = vm;
1774 bo_va->base.bo = bo;
1775 INIT_LIST_HEAD(&bo_va->base.bo_list);
1776 INIT_LIST_HEAD(&bo_va->base.vm_status);
1777
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02001779 INIT_LIST_HEAD(&bo_va->valids);
1780 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01001781
Christian König727ffdf2017-12-22 17:13:03 +01001782 if (!bo)
1783 return bo_va;
1784
1785 list_add_tail(&bo_va->base.bo_list, &bo->va);
1786
1787 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
1788 return bo_va;
1789
1790 if (bo->preferred_domains &
1791 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
1792 return bo_va;
1793
1794 /*
1795 * We checked all the prerequisites, but it looks like this per VM BO
1796 * is currently evicted. add the BO to the evicted list to make sure it
1797 * is validated on next VM use to avoid fault.
1798 * */
1799 spin_lock(&vm->status_lock);
1800 list_move_tail(&bo_va->base.vm_status, &vm->evicted);
1801 spin_unlock(&vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001802
1803 return bo_va;
1804}
1805
Christian König73fb16e2017-08-16 11:13:48 +02001806
1807/**
1808 * amdgpu_vm_bo_insert_mapping - insert a new mapping
1809 *
1810 * @adev: amdgpu_device pointer
1811 * @bo_va: bo_va to store the address
1812 * @mapping: the mapping to insert
1813 *
1814 * Insert a new mapping into all structures.
1815 */
1816static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1817 struct amdgpu_bo_va *bo_va,
1818 struct amdgpu_bo_va_mapping *mapping)
1819{
1820 struct amdgpu_vm *vm = bo_va->base.vm;
1821 struct amdgpu_bo *bo = bo_va->base.bo;
1822
Christian Königaebc5e62017-09-06 16:55:16 +02001823 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02001824 list_add(&mapping->list, &bo_va->invalids);
1825 amdgpu_vm_it_insert(mapping, &vm->va);
1826
1827 if (mapping->flags & AMDGPU_PTE_PRT)
1828 amdgpu_vm_prt_get(adev);
1829
1830 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1831 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001832 if (list_empty(&bo_va->base.vm_status))
1833 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02001834 spin_unlock(&vm->status_lock);
1835 }
1836 trace_amdgpu_vm_bo_map(bo_va, mapping);
1837}
1838
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001839/**
1840 * amdgpu_vm_bo_map - map bo inside a vm
1841 *
1842 * @adev: amdgpu_device pointer
1843 * @bo_va: bo_va to store the address
1844 * @saddr: where to map the BO
1845 * @offset: requested offset in the BO
1846 * @flags: attributes of pages (read/write/valid/etc.)
1847 *
1848 * Add a mapping of the BO at the specefied addr into the VM.
1849 * Returns 0 for success, error for failure.
1850 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001851 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001852 */
1853int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1854 struct amdgpu_bo_va *bo_va,
1855 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001856 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857{
Christian Königa9f87f62017-03-30 14:03:59 +02001858 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02001859 struct amdgpu_bo *bo = bo_va->base.bo;
1860 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001861 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001862
Christian König0be52de2015-05-18 14:37:27 +02001863 /* validate the parameters */
1864 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001865 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001866 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001867
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001869 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001870 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001871 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001872 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001873
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001874 saddr /= AMDGPU_GPU_PAGE_SIZE;
1875 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1876
Christian Königa9f87f62017-03-30 14:03:59 +02001877 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1878 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001879 /* bo and tmp overlap, invalid addr */
1880 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02001881 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02001882 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01001883 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001884 }
1885
1886 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001887 if (!mapping)
1888 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889
Christian Königa9f87f62017-03-30 14:03:59 +02001890 mapping->start = saddr;
1891 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001892 mapping->offset = offset;
1893 mapping->flags = flags;
1894
Christian König73fb16e2017-08-16 11:13:48 +02001895 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01001896
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001897 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001898}
1899
1900/**
Christian König80f95c52017-03-13 10:13:39 +01001901 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1902 *
1903 * @adev: amdgpu_device pointer
1904 * @bo_va: bo_va to store the address
1905 * @saddr: where to map the BO
1906 * @offset: requested offset in the BO
1907 * @flags: attributes of pages (read/write/valid/etc.)
1908 *
1909 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1910 * mappings as we do so.
1911 * Returns 0 for success, error for failure.
1912 *
1913 * Object has to be reserved and unreserved outside!
1914 */
1915int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1916 struct amdgpu_bo_va *bo_va,
1917 uint64_t saddr, uint64_t offset,
1918 uint64_t size, uint64_t flags)
1919{
1920 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02001921 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01001922 uint64_t eaddr;
1923 int r;
1924
1925 /* validate the parameters */
1926 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1927 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1928 return -EINVAL;
1929
1930 /* make sure object fit at this offset */
1931 eaddr = saddr + size - 1;
1932 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001933 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01001934 return -EINVAL;
1935
1936 /* Allocate all the needed memory */
1937 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1938 if (!mapping)
1939 return -ENOMEM;
1940
Christian Königec681542017-08-01 10:51:43 +02001941 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01001942 if (r) {
1943 kfree(mapping);
1944 return r;
1945 }
1946
1947 saddr /= AMDGPU_GPU_PAGE_SIZE;
1948 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1949
Christian Königa9f87f62017-03-30 14:03:59 +02001950 mapping->start = saddr;
1951 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01001952 mapping->offset = offset;
1953 mapping->flags = flags;
1954
Christian König73fb16e2017-08-16 11:13:48 +02001955 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01001956
1957 return 0;
1958}
1959
1960/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001961 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1962 *
1963 * @adev: amdgpu_device pointer
1964 * @bo_va: bo_va to remove the address from
1965 * @saddr: where to the BO is mapped
1966 *
1967 * Remove a mapping of the BO at the specefied addr from the VM.
1968 * Returns 0 for success, error for failure.
1969 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001970 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001971 */
1972int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1973 struct amdgpu_bo_va *bo_va,
1974 uint64_t saddr)
1975{
1976 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02001977 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02001978 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001979
Christian König6c7fc502015-06-05 20:56:17 +02001980 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001981
Christian König7fc11952015-07-30 11:53:42 +02001982 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001983 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 break;
1985 }
1986
Christian König7fc11952015-07-30 11:53:42 +02001987 if (&mapping->list == &bo_va->valids) {
1988 valid = false;
1989
1990 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001991 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02001992 break;
1993 }
1994
Christian König32b41ac2016-03-08 18:03:27 +01001995 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001996 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001997 }
Christian König32b41ac2016-03-08 18:03:27 +01001998
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001999 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002000 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002001 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002002 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002003
Christian Könige17841b2016-03-08 17:52:01 +01002004 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002006 else
Christian König284710f2017-01-30 11:09:31 +01002007 amdgpu_vm_free_mapping(adev, vm, mapping,
2008 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009
2010 return 0;
2011}
2012
2013/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002014 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2015 *
2016 * @adev: amdgpu_device pointer
2017 * @vm: VM structure to use
2018 * @saddr: start of the range
2019 * @size: size of the range
2020 *
2021 * Remove all mappings in a range, split them as appropriate.
2022 * Returns 0 for success, error for failure.
2023 */
2024int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2025 struct amdgpu_vm *vm,
2026 uint64_t saddr, uint64_t size)
2027{
2028 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002029 LIST_HEAD(removed);
2030 uint64_t eaddr;
2031
2032 eaddr = saddr + size - 1;
2033 saddr /= AMDGPU_GPU_PAGE_SIZE;
2034 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2035
2036 /* Allocate all the needed memory */
2037 before = kzalloc(sizeof(*before), GFP_KERNEL);
2038 if (!before)
2039 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002040 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002041
2042 after = kzalloc(sizeof(*after), GFP_KERNEL);
2043 if (!after) {
2044 kfree(before);
2045 return -ENOMEM;
2046 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002047 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002048
2049 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002050 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2051 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002052 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002053 if (tmp->start < saddr) {
2054 before->start = tmp->start;
2055 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002056 before->offset = tmp->offset;
2057 before->flags = tmp->flags;
2058 list_add(&before->list, &tmp->list);
2059 }
2060
2061 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002062 if (tmp->last > eaddr) {
2063 after->start = eaddr + 1;
2064 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002065 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002066 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002067 after->flags = tmp->flags;
2068 list_add(&after->list, &tmp->list);
2069 }
2070
2071 list_del(&tmp->list);
2072 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002073
2074 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002075 }
2076
2077 /* And free them up */
2078 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002079 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002080 list_del(&tmp->list);
2081
Christian Königa9f87f62017-03-30 14:03:59 +02002082 if (tmp->start < saddr)
2083 tmp->start = saddr;
2084 if (tmp->last > eaddr)
2085 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002086
Christian Königaebc5e62017-09-06 16:55:16 +02002087 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002088 list_add(&tmp->list, &vm->freed);
2089 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2090 }
2091
Junwei Zhang27f6d612017-03-16 16:09:24 +08002092 /* Insert partial mapping before the range */
2093 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002094 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002095 if (before->flags & AMDGPU_PTE_PRT)
2096 amdgpu_vm_prt_get(adev);
2097 } else {
2098 kfree(before);
2099 }
2100
2101 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002102 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002103 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002104 if (after->flags & AMDGPU_PTE_PRT)
2105 amdgpu_vm_prt_get(adev);
2106 } else {
2107 kfree(after);
2108 }
2109
2110 return 0;
2111}
2112
2113/**
Christian Königaebc5e62017-09-06 16:55:16 +02002114 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2115 *
2116 * @vm: the requested VM
2117 *
2118 * Find a mapping by it's address.
2119 */
2120struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2121 uint64_t addr)
2122{
2123 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2124}
2125
2126/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002127 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2128 *
2129 * @adev: amdgpu_device pointer
2130 * @bo_va: requested bo_va
2131 *
Christian König8843dbb2016-01-26 12:17:11 +01002132 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002133 *
2134 * Object have to be reserved!
2135 */
2136void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2137 struct amdgpu_bo_va *bo_va)
2138{
2139 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002140 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002141
Christian Königec681542017-08-01 10:51:43 +02002142 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002143
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002144 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002145 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002146 spin_unlock(&vm->status_lock);
2147
Christian König7fc11952015-07-30 11:53:42 +02002148 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002149 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002150 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002151 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002152 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002153 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002154 }
Christian König7fc11952015-07-30 11:53:42 +02002155 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2156 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002157 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002158 amdgpu_vm_free_mapping(adev, vm, mapping,
2159 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002160 }
Christian König32b41ac2016-03-08 18:03:27 +01002161
Chris Wilsonf54d1862016-10-25 13:00:45 +01002162 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002163 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002164}
2165
2166/**
2167 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2168 *
2169 * @adev: amdgpu_device pointer
2170 * @vm: requested vm
2171 * @bo: amdgpu buffer object
2172 *
Christian König8843dbb2016-01-26 12:17:11 +01002173 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002174 */
2175void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002176 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177{
Christian Königec681542017-08-01 10:51:43 +02002178 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179
Christian Königec681542017-08-01 10:51:43 +02002180 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002181 struct amdgpu_vm *vm = bo_base->vm;
2182
Christian König3d7d4d32017-08-23 16:13:33 +02002183 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002184 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2185 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002186 if (bo->tbo.type == ttm_bo_type_kernel)
2187 list_move(&bo_base->vm_status, &vm->evicted);
2188 else
2189 list_move_tail(&bo_base->vm_status,
2190 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002191 spin_unlock(&bo_base->vm->status_lock);
2192 continue;
2193 }
2194
Christian Königea097292017-08-09 14:15:46 +02002195 if (bo->tbo.type == ttm_bo_type_kernel) {
2196 spin_lock(&bo_base->vm->status_lock);
2197 if (list_empty(&bo_base->vm_status))
2198 list_add(&bo_base->vm_status, &vm->relocated);
2199 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002200 continue;
Christian Königea097292017-08-09 14:15:46 +02002201 }
Christian König3f3333f2017-08-03 14:02:13 +02002202
Christian Königec681542017-08-01 10:51:43 +02002203 spin_lock(&bo_base->vm->status_lock);
2204 if (list_empty(&bo_base->vm_status))
Christian König481c2e92017-09-01 14:46:19 +02002205 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002206 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 }
2208}
2209
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002210static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2211{
2212 /* Total bits covered by PD + PTs */
2213 unsigned bits = ilog2(vm_size) + 18;
2214
2215 /* Make sure the PD is 4K in size up to 8GB address space.
2216 Above that split equal between PD and PTs */
2217 if (vm_size <= 8)
2218 return (bits - 9);
2219 else
2220 return ((bits + 3) / 2);
2221}
2222
2223/**
Roger Hed07f14b2017-08-15 16:05:59 +08002224 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002225 *
2226 * @adev: amdgpu_device pointer
2227 * @vm_size: the default vm size if it's set auto
2228 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002229void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +01002230 uint32_t fragment_size_default, unsigned max_level,
2231 unsigned max_bits)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002232{
Christian König36539dc2017-11-23 11:16:05 +01002233 uint64_t tmp;
2234
2235 /* adjust vm size first */
Christian Königf3368122017-11-23 12:57:18 +01002236 if (amdgpu_vm_size != -1) {
2237 unsigned max_size = 1 << (max_bits - 30);
2238
Christian Königfdd5faa2017-11-04 16:51:44 +01002239 vm_size = amdgpu_vm_size;
Christian Königf3368122017-11-23 12:57:18 +01002240 if (vm_size > max_size) {
2241 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2242 amdgpu_vm_size, max_size);
2243 vm_size = max_size;
2244 }
2245 }
Christian Königfdd5faa2017-11-04 16:51:44 +01002246
2247 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Christian König36539dc2017-11-23 11:16:05 +01002248
2249 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
Christian König97489122017-11-27 16:22:05 +01002250 if (amdgpu_vm_block_size != -1)
2251 tmp >>= amdgpu_vm_block_size - 9;
Christian König36539dc2017-11-23 11:16:05 +01002252 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2253 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
Chunming Zhou196f7482017-12-13 14:22:54 +08002254 switch (adev->vm_manager.num_level) {
2255 case 3:
2256 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2257 break;
2258 case 2:
2259 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2260 break;
2261 case 1:
2262 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2263 break;
2264 default:
2265 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2266 }
Christian Königb38f41e2017-11-22 17:00:35 +01002267 /* block size depends on vm size and hw setup*/
Christian König97489122017-11-27 16:22:05 +01002268 if (amdgpu_vm_block_size != -1)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002269 adev->vm_manager.block_size =
Christian König97489122017-11-27 16:22:05 +01002270 min((unsigned)amdgpu_vm_block_size, max_bits
2271 - AMDGPU_GPU_PAGE_SHIFT
2272 - 9 * adev->vm_manager.num_level);
2273 else if (adev->vm_manager.num_level > 1)
2274 adev->vm_manager.block_size = 9;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002275 else
Christian König97489122017-11-27 16:22:05 +01002276 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002277
Christian Königb38f41e2017-11-22 17:00:35 +01002278 if (amdgpu_vm_fragment_size == -1)
2279 adev->vm_manager.fragment_size = fragment_size_default;
2280 else
2281 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
Roger Hed07f14b2017-08-15 16:05:59 +08002282
Christian König36539dc2017-11-23 11:16:05 +01002283 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2284 vm_size, adev->vm_manager.num_level + 1,
2285 adev->vm_manager.block_size,
Christian Königfdd5faa2017-11-04 16:51:44 +01002286 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002287}
2288
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002289/**
2290 * amdgpu_vm_init - initialize a vm instance
2291 *
2292 * @adev: amdgpu_device pointer
2293 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002294 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002295 *
Christian König8843dbb2016-01-26 12:17:11 +01002296 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002297 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002298int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002299 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002300{
2301 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002302 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002303 unsigned ring_instance;
2304 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002305 struct drm_sched_rq *rq;
Christian Königd3aab672018-01-24 14:57:02 +01002306 unsigned long size;
Christian König13307f72018-01-24 17:19:04 +01002307 uint64_t flags;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002308 int r, i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002309
Davidlohr Buesof808c132017-09-08 16:15:08 -07002310 vm->va = RB_ROOT_CACHED;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002311 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2312 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002313 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002314 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002315 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002316 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002317 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002318
Christian König2bd9ccf2016-02-01 12:53:58 +01002319 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002320
2321 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2322 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2323 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002324 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2325 r = drm_sched_entity_init(&ring->sched, &vm->entity,
Monk Liub3eebe32017-10-23 12:23:29 +08002326 rq, amdgpu_sched_jobs, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002327 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002328 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002329
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002330 vm->pte_support_ats = false;
2331
2332 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002333 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2334 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002335
Christian König13307f72018-01-24 17:19:04 +01002336 if (adev->asic_type == CHIP_RAVEN)
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002337 vm->pte_support_ats = true;
Christian König13307f72018-01-24 17:19:04 +01002338 } else {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002339 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2340 AMDGPU_VM_USE_CPU_FOR_GFX);
Christian König13307f72018-01-24 17:19:04 +01002341 }
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002342 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2343 vm->use_cpu_for_update ? "CPU" : "SDMA");
2344 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2345 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002346 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002347
Christian König13307f72018-01-24 17:19:04 +01002348 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002349 if (vm->use_cpu_for_update)
2350 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2351 else
2352 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2353 AMDGPU_GEM_CREATE_SHADOW);
2354
Christian Königd3aab672018-01-24 14:57:02 +01002355 size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
2356 r = amdgpu_bo_create(adev, size, align, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König13307f72018-01-24 17:19:04 +01002357 flags, NULL, NULL, 0,
Christian Königd3aab672018-01-24 14:57:02 +01002358 &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002359 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002360 goto error_free_sched_entity;
2361
Christian Königd3aab672018-01-24 14:57:02 +01002362 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2363 if (r)
2364 goto error_free_root;
2365
Christian König13307f72018-01-24 17:19:04 +01002366 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
2367 adev->vm_manager.root_level);
2368 if (r)
2369 goto error_unreserve;
2370
Christian König3f3333f2017-08-03 14:02:13 +02002371 vm->root.base.vm = vm;
2372 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
Christian Königd3aab672018-01-24 14:57:02 +01002373 list_add_tail(&vm->root.base.vm_status, &vm->evicted);
2374 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König0a096fb2017-07-12 10:01:48 +02002375
Felix Kuehling02208442017-08-25 20:40:26 -04002376 if (pasid) {
2377 unsigned long flags;
2378
2379 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2380 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2381 GFP_ATOMIC);
2382 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2383 if (r < 0)
2384 goto error_free_root;
2385
2386 vm->pasid = pasid;
2387 }
2388
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002389 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002390 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002391
2392 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002393
Christian König13307f72018-01-24 17:19:04 +01002394error_unreserve:
2395 amdgpu_bo_unreserve(vm->root.base.bo);
2396
Christian König67003a12016-10-12 14:46:26 +02002397error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002398 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2399 amdgpu_bo_unref(&vm->root.base.bo);
2400 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002401
2402error_free_sched_entity:
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002403 drm_sched_entity_fini(&ring->sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002404
2405 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406}
2407
2408/**
Christian Königf566ceb2016-10-27 20:04:38 +02002409 * amdgpu_vm_free_levels - free PD/PT levels
2410 *
Christian König8f19cd72017-11-30 15:28:03 +01002411 * @adev: amdgpu device structure
2412 * @parent: PD/PT starting level to free
2413 * @level: level of parent structure
Christian Königf566ceb2016-10-27 20:04:38 +02002414 *
2415 * Free the page directory or page table level and all sub levels.
2416 */
Christian König8f19cd72017-11-30 15:28:03 +01002417static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2418 struct amdgpu_vm_pt *parent,
2419 unsigned level)
Christian Königf566ceb2016-10-27 20:04:38 +02002420{
Christian König8f19cd72017-11-30 15:28:03 +01002421 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +02002422
Christian König8f19cd72017-11-30 15:28:03 +01002423 if (parent->base.bo) {
2424 list_del(&parent->base.bo_list);
2425 list_del(&parent->base.vm_status);
2426 amdgpu_bo_unref(&parent->base.bo->shadow);
2427 amdgpu_bo_unref(&parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002428 }
2429
Christian König8f19cd72017-11-30 15:28:03 +01002430 if (parent->entries)
2431 for (i = 0; i < num_entries; i++)
2432 amdgpu_vm_free_levels(adev, &parent->entries[i],
2433 level + 1);
Christian Königf566ceb2016-10-27 20:04:38 +02002434
Christian König8f19cd72017-11-30 15:28:03 +01002435 kvfree(parent->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002436}
2437
2438/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002439 * amdgpu_vm_fini - tear down a vm instance
2440 *
2441 * @adev: amdgpu_device pointer
2442 * @vm: requested vm
2443 *
Christian König8843dbb2016-01-26 12:17:11 +01002444 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002445 * Unbind the VM and remove all bos from the vm bo list
2446 */
2447void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2448{
2449 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König132f34e2018-01-12 15:26:08 +01002450 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002451 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002452 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002453 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002454
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002455 /* Clear pending page faults from IH when the VM is destroyed */
2456 while (kfifo_get(&vm->faults, &fault))
2457 amdgpu_ih_clear_fault(adev, fault);
2458
Felix Kuehling02208442017-08-25 20:40:26 -04002459 if (vm->pasid) {
2460 unsigned long flags;
2461
2462 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2463 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2464 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2465 }
2466
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002467 drm_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002468
Davidlohr Buesof808c132017-09-08 16:15:08 -07002469 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002470 dev_err(adev->dev, "still active bo inside vm\n");
2471 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002472 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2473 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002474 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002475 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476 kfree(mapping);
2477 }
2478 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002479 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002480 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002481 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002482 }
Christian König284710f2017-01-30 11:09:31 +01002483
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002484 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002485 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002486 }
2487
Christian König2642cf12017-10-13 17:24:31 +02002488 root = amdgpu_bo_ref(vm->root.base.bo);
2489 r = amdgpu_bo_reserve(root, true);
2490 if (r) {
2491 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2492 } else {
Chunming Zhou196f7482017-12-13 14:22:54 +08002493 amdgpu_vm_free_levels(adev, &vm->root,
2494 adev->vm_manager.root_level);
Christian König2642cf12017-10-13 17:24:31 +02002495 amdgpu_bo_unreserve(root);
2496 }
2497 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002498 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002499 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
Christian König620f7742017-12-18 16:53:03 +01002500 amdgpu_vmid_free_reserved(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002501}
Christian Königea89f8c2015-11-15 20:52:06 +01002502
2503/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002504 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2505 *
2506 * @adev: amdgpu_device pointer
2507 * @pasid: PASID do identify the VM
2508 *
2509 * This function is expected to be called in interrupt context. Returns
2510 * true if there was fault credit, false otherwise
2511 */
2512bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2513 unsigned int pasid)
2514{
2515 struct amdgpu_vm *vm;
2516
2517 spin_lock(&adev->vm_manager.pasid_lock);
2518 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
Christian Königd9589392018-01-09 19:18:59 +01002519 if (!vm) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002520 /* VM not found, can't track fault credit */
Christian Königd9589392018-01-09 19:18:59 +01002521 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002522 return true;
Christian Königd9589392018-01-09 19:18:59 +01002523 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002524
2525 /* No lock needed. only accessed by IRQ handler */
Christian Königd9589392018-01-09 19:18:59 +01002526 if (!vm->fault_credit) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002527 /* Too many faults in this VM */
Christian Königd9589392018-01-09 19:18:59 +01002528 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002529 return false;
Christian Königd9589392018-01-09 19:18:59 +01002530 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002531
2532 vm->fault_credit--;
Christian Königd9589392018-01-09 19:18:59 +01002533 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002534 return true;
2535}
2536
2537/**
Christian Königa9a78b32016-01-21 10:19:11 +01002538 * amdgpu_vm_manager_init - init the VM manager
2539 *
2540 * @adev: amdgpu_device pointer
2541 *
2542 * Initialize the VM manager structures
2543 */
2544void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2545{
Christian König620f7742017-12-18 16:53:03 +01002546 unsigned i;
Christian Königa9a78b32016-01-21 10:19:11 +01002547
Christian König620f7742017-12-18 16:53:03 +01002548 amdgpu_vmid_mgr_init(adev);
Christian König2d55e452016-02-08 17:37:38 +01002549
Chris Wilsonf54d1862016-10-25 13:00:45 +01002550 adev->vm_manager.fence_context =
2551 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002552 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2553 adev->vm_manager.seqno[i] = 0;
2554
Christian König2d55e452016-02-08 17:37:38 +01002555 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian König284710f2017-01-30 11:09:31 +01002556 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002557 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002558
2559 /* If not overridden by the user, by default, only in large BAR systems
2560 * Compute VM tables will be updated by CPU
2561 */
2562#ifdef CONFIG_X86_64
2563 if (amdgpu_vm_update_mode == -1) {
2564 if (amdgpu_vm_is_large_bar(adev))
2565 adev->vm_manager.vm_update_mode =
2566 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2567 else
2568 adev->vm_manager.vm_update_mode = 0;
2569 } else
2570 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2571#else
2572 adev->vm_manager.vm_update_mode = 0;
2573#endif
2574
Felix Kuehling02208442017-08-25 20:40:26 -04002575 idr_init(&adev->vm_manager.pasid_idr);
2576 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002577}
2578
2579/**
Christian Königea89f8c2015-11-15 20:52:06 +01002580 * amdgpu_vm_manager_fini - cleanup VM manager
2581 *
2582 * @adev: amdgpu_device pointer
2583 *
2584 * Cleanup the VM manager and free resources.
2585 */
2586void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2587{
Felix Kuehling02208442017-08-25 20:40:26 -04002588 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2589 idr_destroy(&adev->vm_manager.pasid_idr);
2590
Christian König620f7742017-12-18 16:53:03 +01002591 amdgpu_vmid_mgr_fini(adev);
Christian Königea89f8c2015-11-15 20:52:06 +01002592}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002593
2594int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2595{
2596 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002597 struct amdgpu_device *adev = dev->dev_private;
2598 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2599 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002600
2601 switch (args->in.op) {
2602 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002603 /* current, we only have requirement to reserve vmid from gfxhub */
Christian König620f7742017-12-18 16:53:03 +01002604 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002605 if (r)
2606 return r;
2607 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002608 case AMDGPU_VM_OP_UNRESERVE_VMID:
Christian König620f7742017-12-18 16:53:03 +01002609 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002610 break;
2611 default:
2612 return -EINVAL;
2613 }
2614
2615 return 0;
2616}