blob: 24474294c92a05fb5e7e9c8908a05c8d1c8a4441 [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,
Christian König45843122018-01-25 18:36:15 +0100270 struct amdgpu_vm *vm, struct amdgpu_bo *bo,
271 unsigned level, bool pte_support_ats)
Christian König13307f72018-01-24 17:19:04 +0100272{
273 struct ttm_operation_ctx ctx = { true, false };
274 struct dma_fence *fence = NULL;
Christian König45843122018-01-25 18:36:15 +0100275 unsigned entries, ats_entries;
Christian König13307f72018-01-24 17:19:04 +0100276 struct amdgpu_ring *ring;
277 struct amdgpu_job *job;
Christian König45843122018-01-25 18:36:15 +0100278 uint64_t addr;
Christian König13307f72018-01-24 17:19:04 +0100279 int r;
280
Christian König45843122018-01-25 18:36:15 +0100281 addr = amdgpu_bo_gpu_offset(bo);
282 entries = amdgpu_bo_size(bo) / 8;
283
284 if (pte_support_ats) {
285 if (level == adev->vm_manager.root_level) {
286 ats_entries = amdgpu_vm_level_shift(adev, level);
287 ats_entries += AMDGPU_GPU_PAGE_SHIFT;
288 ats_entries = AMDGPU_VA_HOLE_START >> ats_entries;
289 ats_entries = min(ats_entries, entries);
290 entries -= ats_entries;
291 } else {
292 ats_entries = entries;
293 entries = 0;
294 }
Christian König13307f72018-01-24 17:19:04 +0100295 } else {
Christian König45843122018-01-25 18:36:15 +0100296 ats_entries = 0;
Christian König13307f72018-01-24 17:19:04 +0100297 }
298
299 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
300
301 r = reservation_object_reserve_shared(bo->tbo.resv);
302 if (r)
303 return r;
304
305 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
306 if (r)
307 goto error;
308
Christian König13307f72018-01-24 17:19:04 +0100309 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
310 if (r)
311 goto error;
312
Christian König45843122018-01-25 18:36:15 +0100313 if (ats_entries) {
314 uint64_t ats_value;
315
316 ats_value = AMDGPU_PTE_DEFAULT_ATC;
317 if (level != AMDGPU_VM_PTB)
318 ats_value |= AMDGPU_PDE_PTE;
319
320 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
321 ats_entries, 0, ats_value);
322 addr += ats_entries * 8;
323 }
324
325 if (entries)
326 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
327 entries, 0, 0);
328
Christian König13307f72018-01-24 17:19:04 +0100329 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
330
331 WARN_ON(job->ibs[0].length_dw > 64);
Christian König29e83572018-02-04 19:36:52 +0100332 r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
333 AMDGPU_FENCE_OWNER_UNDEFINED, false);
334 if (r)
335 goto error_free;
336
Christian König13307f72018-01-24 17:19:04 +0100337 r = amdgpu_job_submit(job, ring, &vm->entity,
338 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
339 if (r)
340 goto error_free;
341
342 amdgpu_bo_fence(bo, fence, true);
343 dma_fence_put(fence);
Christian Könige61736d2018-02-02 21:05:40 +0100344
345 if (bo->shadow)
346 return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
347 level, pte_support_ats);
348
Christian König13307f72018-01-24 17:19:04 +0100349 return 0;
350
351error_free:
352 amdgpu_job_free(job);
353
354error:
355 return r;
356}
357
358/**
Christian Königf566ceb2016-10-27 20:04:38 +0200359 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
360 *
361 * @adev: amdgpu_device pointer
362 * @vm: requested vm
363 * @saddr: start of the address range
364 * @eaddr: end of the address range
365 *
366 * Make sure the page directories and page tables are allocated
367 */
368static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
369 struct amdgpu_vm *vm,
370 struct amdgpu_vm_pt *parent,
371 uint64_t saddr, uint64_t eaddr,
Christian König45843122018-01-25 18:36:15 +0100372 unsigned level, bool ats)
Christian Königf566ceb2016-10-27 20:04:38 +0200373{
Christian König50783142017-11-27 14:01:51 +0100374 unsigned shift = amdgpu_vm_level_shift(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200375 unsigned pt_idx, from, to;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400376 u64 flags;
Christian König13307f72018-01-24 17:19:04 +0100377 int r;
Christian Königf566ceb2016-10-27 20:04:38 +0200378
379 if (!parent->entries) {
380 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
381
Michal Hocko20981052017-05-17 14:23:12 +0200382 parent->entries = kvmalloc_array(num_entries,
383 sizeof(struct amdgpu_vm_pt),
384 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200385 if (!parent->entries)
386 return -ENOMEM;
387 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
388 }
389
Felix Kuehling1866bac2017-03-28 20:36:12 -0400390 from = saddr >> shift;
391 to = eaddr >> shift;
392 if (from >= amdgpu_vm_num_entries(adev, level) ||
393 to >= amdgpu_vm_num_entries(adev, level))
394 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200395
Christian Königf566ceb2016-10-27 20:04:38 +0200396 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400397 saddr = saddr & ((1 << shift) - 1);
398 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200399
Christian König13307f72018-01-24 17:19:04 +0100400 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400401 if (vm->use_cpu_for_update)
402 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
403 else
404 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
405 AMDGPU_GEM_CREATE_SHADOW);
406
Christian Königf566ceb2016-10-27 20:04:38 +0200407 /* walk over the address space and allocate the page tables */
408 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200409 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200410 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
411 struct amdgpu_bo *pt;
412
Christian König3f3333f2017-08-03 14:02:13 +0200413 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200414 r = amdgpu_bo_create(adev,
415 amdgpu_vm_bo_size(adev, level),
Christian Königeab3de22018-03-14 14:48:17 -0500416 AMDGPU_GPU_PAGE_SIZE,
Christian König13307f72018-01-24 17:19:04 +0100417 AMDGPU_GEM_DOMAIN_VRAM, flags,
Christian Königeab3de22018-03-14 14:48:17 -0500418 ttm_bo_type_kernel, resv, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200419 if (r)
420 return r;
421
Christian König45843122018-01-25 18:36:15 +0100422 r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats);
Christian König13307f72018-01-24 17:19:04 +0100423 if (r) {
Christian Könige5197a42018-02-02 21:00:44 +0100424 amdgpu_bo_unref(&pt->shadow);
Christian König13307f72018-01-24 17:19:04 +0100425 amdgpu_bo_unref(&pt);
426 return r;
427 }
428
Christian König0a096fb2017-07-12 10:01:48 +0200429 if (vm->use_cpu_for_update) {
430 r = amdgpu_bo_kmap(pt, NULL);
431 if (r) {
Christian Könige5197a42018-02-02 21:00:44 +0100432 amdgpu_bo_unref(&pt->shadow);
Christian König0a096fb2017-07-12 10:01:48 +0200433 amdgpu_bo_unref(&pt);
434 return r;
435 }
436 }
437
Christian Königf566ceb2016-10-27 20:04:38 +0200438 /* Keep a reference to the root directory to avoid
439 * freeing them up in the wrong order.
440 */
Christian König0f2fc432017-08-31 10:46:20 +0200441 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200442
Christian König3f3333f2017-08-03 14:02:13 +0200443 entry->base.vm = vm;
444 entry->base.bo = pt;
445 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200446 spin_lock(&vm->status_lock);
447 list_add(&entry->base.vm_status, &vm->relocated);
448 spin_unlock(&vm->status_lock);
Christian Königf566ceb2016-10-27 20:04:38 +0200449 }
450
Chunming Zhou196f7482017-12-13 14:22:54 +0800451 if (level < AMDGPU_VM_PTB) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400452 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
453 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
454 ((1 << shift) - 1);
455 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
Christian König45843122018-01-25 18:36:15 +0100456 sub_eaddr, level, ats);
Christian Königf566ceb2016-10-27 20:04:38 +0200457 if (r)
458 return r;
459 }
460 }
461
462 return 0;
463}
464
Christian König663e4572017-03-13 10:13:37 +0100465/**
466 * amdgpu_vm_alloc_pts - Allocate page tables.
467 *
468 * @adev: amdgpu_device pointer
469 * @vm: VM to allocate page tables for
470 * @saddr: Start address which needs to be allocated
471 * @size: Size from start address we need.
472 *
473 * Make sure the page tables are allocated.
474 */
475int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
476 struct amdgpu_vm *vm,
477 uint64_t saddr, uint64_t size)
478{
Christian König663e4572017-03-13 10:13:37 +0100479 uint64_t eaddr;
Christian König45843122018-01-25 18:36:15 +0100480 bool ats = false;
Christian König663e4572017-03-13 10:13:37 +0100481
482 /* validate the parameters */
483 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
484 return -EINVAL;
485
486 eaddr = saddr + size - 1;
Christian König45843122018-01-25 18:36:15 +0100487
488 if (vm->pte_support_ats)
489 ats = saddr < AMDGPU_VA_HOLE_START;
Christian König663e4572017-03-13 10:13:37 +0100490
491 saddr /= AMDGPU_GPU_PAGE_SIZE;
492 eaddr /= AMDGPU_GPU_PAGE_SIZE;
493
Christian König45843122018-01-25 18:36:15 +0100494 if (eaddr >= adev->vm_manager.max_pfn) {
495 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
496 eaddr, adev->vm_manager.max_pfn);
497 return -EINVAL;
498 }
499
Chunming Zhou196f7482017-12-13 14:22:54 +0800500 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
Christian König45843122018-01-25 18:36:15 +0100501 adev->vm_manager.root_level, ats);
Christian König663e4572017-03-13 10:13:37 +0100502}
503
Christian König641e9402017-04-03 13:59:25 +0200504/**
Alex Xiee59c0202017-06-01 09:42:59 -0400505 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
506 *
507 * @adev: amdgpu_device pointer
508 */
509void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
510{
511 const struct amdgpu_ip_block *ip_block;
512 bool has_compute_vm_bug;
513 struct amdgpu_ring *ring;
514 int i;
515
516 has_compute_vm_bug = false;
517
Alex Deucher2990a1f2017-12-15 16:18:00 -0500518 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
Alex Xiee59c0202017-06-01 09:42:59 -0400519 if (ip_block) {
520 /* Compute has a VM bug for GFX version < 7.
521 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
522 if (ip_block->version->major <= 7)
523 has_compute_vm_bug = true;
524 else if (ip_block->version->major == 8)
525 if (adev->gfx.mec_fw_version < 673)
526 has_compute_vm_bug = true;
527 }
528
529 for (i = 0; i < adev->num_rings; i++) {
530 ring = adev->rings[i];
531 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
532 /* only compute rings */
533 ring->has_compute_vm_bug = has_compute_vm_bug;
534 else
535 ring->has_compute_vm_bug = false;
536 }
537}
538
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400539bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
540 struct amdgpu_job *job)
541{
542 struct amdgpu_device *adev = ring->adev;
543 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100544 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
545 struct amdgpu_vmid *id;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400546 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400547 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400548
Christian Königc4f46f22017-12-18 17:08:25 +0100549 if (job->vmid == 0)
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400550 return false;
Christian Königc4f46f22017-12-18 17:08:25 +0100551 id = &id_mgr->ids[job->vmid];
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400552 gds_switch_needed = ring->funcs->emit_gds_switch && (
553 id->gds_base != job->gds_base ||
554 id->gds_size != job->gds_size ||
555 id->gws_base != job->gws_base ||
556 id->gws_size != job->gws_size ||
557 id->oa_base != job->oa_base ||
558 id->oa_size != job->oa_size);
559
Christian König620f7742017-12-18 16:53:03 +0100560 if (amdgpu_vmid_had_gpu_reset(adev, id))
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400561 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400562
563 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400564}
565
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400566static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
567{
Christian König770d13b2018-01-12 14:52:22 +0100568 return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500569}
570
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571/**
572 * amdgpu_vm_flush - hardware flush the vm
573 *
574 * @ring: ring to use for flush
Christian Königc4f46f22017-12-18 17:08:25 +0100575 * @vmid: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100576 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 *
Christian König4ff37a82016-02-26 16:18:26 +0100578 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800580int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581{
Christian König971fe9a92016-03-01 15:09:25 +0100582 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200583 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100584 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian Königc4f46f22017-12-18 17:08:25 +0100585 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
Christian Königd564a062016-03-01 15:51:53 +0100586 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800587 id->gds_base != job->gds_base ||
588 id->gds_size != job->gds_size ||
589 id->gws_base != job->gws_base ||
590 id->gws_size != job->gws_size ||
591 id->oa_base != job->oa_base ||
592 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800593 bool vm_flush_needed = job->vm_needs_flush;
Christian Königb3cd2852018-02-05 17:38:01 +0100594 bool pasid_mapping_needed = id->pasid != job->pasid ||
595 !id->pasid_mapping ||
596 !dma_fence_is_signaled(id->pasid_mapping);
597 struct dma_fence *fence = NULL;
Christian Königc0e51932017-04-03 14:16:07 +0200598 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100599 int r;
Christian Königd564a062016-03-01 15:51:53 +0100600
Christian König620f7742017-12-18 16:53:03 +0100601 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
Christian Königf7d015b2017-04-03 14:28:26 +0200602 gds_switch_needed = true;
603 vm_flush_needed = true;
Christian Königb3cd2852018-02-05 17:38:01 +0100604 pasid_mapping_needed = true;
Christian Königf7d015b2017-04-03 14:28:26 +0200605 }
Christian König971fe9a92016-03-01 15:09:25 +0100606
Christian Königb3cd2852018-02-05 17:38:01 +0100607 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
608 vm_flush_needed &= !!ring->funcs->emit_vm_flush;
609 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
610 ring->funcs->emit_wreg;
611
Monk Liu8fdf0742017-06-06 17:25:13 +0800612 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200613 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100614
Christian Königc0e51932017-04-03 14:16:07 +0200615 if (ring->funcs->init_cond_exec)
616 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100617
Monk Liu8fdf0742017-06-06 17:25:13 +0800618 if (need_pipe_sync)
619 amdgpu_ring_emit_pipeline_sync(ring);
620
Christian Königb3cd2852018-02-05 17:38:01 +0100621 if (vm_flush_needed) {
Christian Königc4f46f22017-12-18 17:08:25 +0100622 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian Königc633c002018-02-04 10:32:35 +0100623 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian Königb3cd2852018-02-05 17:38:01 +0100624 }
Monk Liue9d672b2017-03-15 12:18:57 +0800625
Christian Königb3cd2852018-02-05 17:38:01 +0100626 if (pasid_mapping_needed)
627 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
628
629 if (vm_flush_needed || pasid_mapping_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200630 r = amdgpu_fence_emit(ring, &fence);
631 if (r)
632 return r;
Christian Königb3cd2852018-02-05 17:38:01 +0100633 }
Monk Liue9d672b2017-03-15 12:18:57 +0800634
Christian Königb3cd2852018-02-05 17:38:01 +0100635 if (vm_flush_needed) {
Christian König76456702017-04-06 17:52:39 +0200636 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200637 dma_fence_put(id->last_flush);
Christian Königb3cd2852018-02-05 17:38:01 +0100638 id->last_flush = dma_fence_get(fence);
639 id->current_gpu_reset_count =
640 atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200641 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200642 }
Monk Liue9d672b2017-03-15 12:18:57 +0800643
Christian Königb3cd2852018-02-05 17:38:01 +0100644 if (pasid_mapping_needed) {
645 id->pasid = job->pasid;
646 dma_fence_put(id->pasid_mapping);
647 id->pasid_mapping = dma_fence_get(fence);
648 }
649 dma_fence_put(fence);
650
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800651 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200652 id->gds_base = job->gds_base;
653 id->gds_size = job->gds_size;
654 id->gws_base = job->gws_base;
655 id->gws_size = job->gws_size;
656 id->oa_base = job->oa_base;
657 id->oa_size = job->oa_size;
Christian Königc4f46f22017-12-18 17:08:25 +0100658 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
Christian Königc0e51932017-04-03 14:16:07 +0200659 job->gds_size, job->gws_base,
660 job->gws_size, job->oa_base,
661 job->oa_size);
662 }
663
664 if (ring->funcs->patch_cond_exec)
665 amdgpu_ring_patch_cond_exec(ring, patch_offset);
666
667 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
668 if (ring->funcs->emit_switch_buffer) {
669 amdgpu_ring_emit_switch_buffer(ring);
670 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 }
Christian König41d9eb22016-03-01 16:46:18 +0100672 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100673}
674
675/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
677 *
678 * @vm: requested vm
679 * @bo: requested buffer object
680 *
Christian König8843dbb2016-01-26 12:17:11 +0100681 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 * Search inside the @bos vm list for the requested vm
683 * Returns the found bo_va or NULL if none is found
684 *
685 * Object has to be reserved!
686 */
687struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
688 struct amdgpu_bo *bo)
689{
690 struct amdgpu_bo_va *bo_va;
691
Christian Königec681542017-08-01 10:51:43 +0200692 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
693 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 return bo_va;
695 }
696 }
697 return NULL;
698}
699
700/**
Christian Königafef8b82016-08-12 13:29:18 +0200701 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 *
Christian König29efc4f2016-08-04 14:52:50 +0200703 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100704 * @bo: PD/PT to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 * @pe: addr of the page entry
706 * @addr: dst addr to write into pe
707 * @count: number of page entries to update
708 * @incr: increase next addr by incr bytes
709 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 *
711 * Traces the parameters and calls the right asic functions
712 * to setup the page table using the DMA.
713 */
Christian Königafef8b82016-08-12 13:29:18 +0200714static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100715 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200716 uint64_t pe, uint64_t addr,
717 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800718 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719{
Christian König373ac642018-01-16 16:54:25 +0100720 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200721 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722
Christian Königafef8b82016-08-12 13:29:18 +0200723 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200724 amdgpu_vm_write_pte(params->adev, params->ib, pe,
725 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726
727 } else {
Christian König27c5f362016-08-04 15:02:49 +0200728 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 count, incr, flags);
730 }
731}
732
733/**
Christian Königafef8b82016-08-12 13:29:18 +0200734 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
735 *
736 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100737 * @bo: PD/PT to update
Christian Königafef8b82016-08-12 13:29:18 +0200738 * @pe: addr of the page entry
739 * @addr: dst addr to write into pe
740 * @count: number of page entries to update
741 * @incr: increase next addr by incr bytes
742 * @flags: hw access flags
743 *
744 * Traces the parameters and calls the DMA function to copy the PTEs.
745 */
746static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100747 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200748 uint64_t pe, uint64_t addr,
749 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800750 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200751{
Christian Königec2f05f2016-09-25 16:11:52 +0200752 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200753
Christian König373ac642018-01-16 16:54:25 +0100754 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200755 trace_amdgpu_vm_copy_ptes(pe, src, count);
756
757 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200758}
759
760/**
Christian Königb07c9d22015-11-30 13:26:07 +0100761 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762 *
Christian Königb07c9d22015-11-30 13:26:07 +0100763 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400764 * @addr: the unmapped addr
765 *
766 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100767 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400768 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200769static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770{
771 uint64_t result;
772
Christian Königde9ea7b2016-08-12 11:33:30 +0200773 /* page table offset */
774 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400775
Christian Königde9ea7b2016-08-12 11:33:30 +0200776 /* in case cpu page size != gpu page size*/
777 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100778
779 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780
781 return result;
782}
783
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400784/**
785 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
786 *
787 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100788 * @bo: PD/PT to update
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400789 * @pe: kmap addr of the page entry
790 * @addr: dst addr to write into pe
791 * @count: number of page entries to update
792 * @incr: increase next addr by incr bytes
793 * @flags: hw access flags
794 *
795 * Write count number of PT/PD entries directly.
796 */
797static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100798 struct amdgpu_bo *bo,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400799 uint64_t pe, uint64_t addr,
800 unsigned count, uint32_t incr,
801 uint64_t flags)
802{
803 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400804 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400805
Christian König373ac642018-01-16 16:54:25 +0100806 pe += (unsigned long)amdgpu_bo_kptr(bo);
807
Christian König03918b32017-07-11 17:15:37 +0200808 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
809
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400810 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400811 value = params->pages_addr ?
812 amdgpu_vm_map_gart(params->pages_addr, addr) :
813 addr;
Christian König132f34e2018-01-12 15:26:08 +0100814 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
815 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400816 addr += incr;
817 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400818}
819
Christian Königa33cab72017-07-11 17:13:00 +0200820static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
821 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400822{
823 struct amdgpu_sync sync;
824 int r;
825
826 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -0400827 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400828 r = amdgpu_sync_wait(&sync, true);
829 amdgpu_sync_free(&sync);
830
831 return r;
832}
833
Christian Königf8991ba2016-09-16 15:36:49 +0200834/*
Christian König6989f242017-11-30 19:08:05 +0100835 * amdgpu_vm_update_pde - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200836 *
Christian König6989f242017-11-30 19:08:05 +0100837 * @param: parameters for the update
Christian Königf8991ba2016-09-16 15:36:49 +0200838 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200839 * @parent: parent directory
Christian König6989f242017-11-30 19:08:05 +0100840 * @entry: entry to update
Christian Königf8991ba2016-09-16 15:36:49 +0200841 *
Christian König6989f242017-11-30 19:08:05 +0100842 * Makes sure the requested entry in parent is up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200843 */
Christian König6989f242017-11-30 19:08:05 +0100844static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
845 struct amdgpu_vm *vm,
846 struct amdgpu_vm_pt *parent,
847 struct amdgpu_vm_pt *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848{
Christian König373ac642018-01-16 16:54:25 +0100849 struct amdgpu_bo *bo = parent->base.bo, *pbo;
Christian König3de676d2017-11-29 13:27:26 +0100850 uint64_t pde, pt, flags;
851 unsigned level;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800852
Christian König6989f242017-11-30 19:08:05 +0100853 /* Don't update huge pages here */
854 if (entry->huge)
855 return;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400856
Christian König373ac642018-01-16 16:54:25 +0100857 for (level = 0, pbo = bo->parent; pbo; ++level)
Christian König3de676d2017-11-29 13:27:26 +0100858 pbo = pbo->parent;
859
Chunming Zhou196f7482017-12-13 14:22:54 +0800860 level += params->adev->vm_manager.root_level;
Christian König373ac642018-01-16 16:54:25 +0100861 pt = amdgpu_bo_gpu_offset(entry->base.bo);
Christian König3de676d2017-11-29 13:27:26 +0100862 flags = AMDGPU_PTE_VALID;
Christian König132f34e2018-01-12 15:26:08 +0100863 amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
Christian König373ac642018-01-16 16:54:25 +0100864 pde = (entry - parent->entries) * 8;
865 if (bo->shadow)
866 params->func(params, bo->shadow, pde, pt, 1, 0, flags);
867 params->func(params, bo, pde, pt, 1, 0, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400868}
869
Christian König194d2162016-10-12 15:13:52 +0200870/*
Christian König92456b92017-05-12 16:09:26 +0200871 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
872 *
873 * @parent: parent PD
874 *
875 * Mark all PD level as invalid after an error.
876 */
Christian König8f19cd72017-11-30 15:28:03 +0100877static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
878 struct amdgpu_vm *vm,
879 struct amdgpu_vm_pt *parent,
880 unsigned level)
Christian König92456b92017-05-12 16:09:26 +0200881{
Christian König8f19cd72017-11-30 15:28:03 +0100882 unsigned pt_idx, num_entries;
Christian König92456b92017-05-12 16:09:26 +0200883
884 /*
885 * Recurse into the subdirectories. This recursion is harmless because
886 * we only have a maximum of 5 layers.
887 */
Christian König8f19cd72017-11-30 15:28:03 +0100888 num_entries = amdgpu_vm_num_entries(adev, level);
889 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
Christian König92456b92017-05-12 16:09:26 +0200890 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
891
Christian König3f3333f2017-08-03 14:02:13 +0200892 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +0200893 continue;
894
Christian Königea097292017-08-09 14:15:46 +0200895 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +0200896 if (list_empty(&entry->base.vm_status))
897 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +0200898 spin_unlock(&vm->status_lock);
Christian König8f19cd72017-11-30 15:28:03 +0100899 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
Christian König92456b92017-05-12 16:09:26 +0200900 }
901}
902
903/*
Christian König194d2162016-10-12 15:13:52 +0200904 * amdgpu_vm_update_directories - make sure that all directories are valid
905 *
906 * @adev: amdgpu_device pointer
907 * @vm: requested vm
908 *
909 * Makes sure all directories are up to date.
910 * Returns 0 for success, error for failure.
911 */
912int amdgpu_vm_update_directories(struct amdgpu_device *adev,
913 struct amdgpu_vm *vm)
914{
Christian König6989f242017-11-30 19:08:05 +0100915 struct amdgpu_pte_update_params params;
916 struct amdgpu_job *job;
917 unsigned ndw = 0;
Dan Carpenter78aa02c2017-09-30 11:14:13 +0300918 int r = 0;
Christian König92456b92017-05-12 16:09:26 +0200919
Christian König6989f242017-11-30 19:08:05 +0100920 if (list_empty(&vm->relocated))
921 return 0;
922
923restart:
924 memset(&params, 0, sizeof(params));
925 params.adev = adev;
926
927 if (vm->use_cpu_for_update) {
928 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
929 if (unlikely(r))
930 return r;
931
932 params.func = amdgpu_vm_cpu_set_ptes;
933 } else {
934 ndw = 512 * 8;
935 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
936 if (r)
937 return r;
938
939 params.ib = &job->ibs[0];
940 params.func = amdgpu_vm_do_set_ptes;
941 }
942
Christian Königea097292017-08-09 14:15:46 +0200943 spin_lock(&vm->status_lock);
944 while (!list_empty(&vm->relocated)) {
Christian König6989f242017-11-30 19:08:05 +0100945 struct amdgpu_vm_bo_base *bo_base, *parent;
946 struct amdgpu_vm_pt *pt, *entry;
Christian Königea097292017-08-09 14:15:46 +0200947 struct amdgpu_bo *bo;
948
949 bo_base = list_first_entry(&vm->relocated,
950 struct amdgpu_vm_bo_base,
951 vm_status);
Christian König6989f242017-11-30 19:08:05 +0100952 list_del_init(&bo_base->vm_status);
Christian Königea097292017-08-09 14:15:46 +0200953 spin_unlock(&vm->status_lock);
954
955 bo = bo_base->bo->parent;
Christian König6989f242017-11-30 19:08:05 +0100956 if (!bo) {
Christian Königea097292017-08-09 14:15:46 +0200957 spin_lock(&vm->status_lock);
Christian König6989f242017-11-30 19:08:05 +0100958 continue;
Christian Königea097292017-08-09 14:15:46 +0200959 }
Christian König6989f242017-11-30 19:08:05 +0100960
961 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
962 bo_list);
963 pt = container_of(parent, struct amdgpu_vm_pt, base);
964 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
965
966 amdgpu_vm_update_pde(&params, vm, pt, entry);
967
968 spin_lock(&vm->status_lock);
969 if (!vm->use_cpu_for_update &&
970 (ndw - params.ib->length_dw) < 32)
971 break;
Christian Königea097292017-08-09 14:15:46 +0200972 }
973 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +0200974
Christian König68c62302017-07-11 17:23:29 +0200975 if (vm->use_cpu_for_update) {
976 /* Flush HDP */
977 mb();
Christian König69882562018-01-19 14:17:40 +0100978 amdgpu_asic_flush_hdp(adev, NULL);
Christian König6989f242017-11-30 19:08:05 +0100979 } else if (params.ib->length_dw == 0) {
980 amdgpu_job_free(job);
981 } else {
982 struct amdgpu_bo *root = vm->root.base.bo;
983 struct amdgpu_ring *ring;
984 struct dma_fence *fence;
985
986 ring = container_of(vm->entity.sched, struct amdgpu_ring,
987 sched);
988
989 amdgpu_ring_pad_ib(ring, params.ib);
990 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
991 AMDGPU_FENCE_OWNER_VM, false);
Christian König6989f242017-11-30 19:08:05 +0100992 WARN_ON(params.ib->length_dw > ndw);
993 r = amdgpu_job_submit(job, ring, &vm->entity,
994 AMDGPU_FENCE_OWNER_VM, &fence);
995 if (r)
996 goto error;
997
998 amdgpu_bo_fence(root, fence, true);
999 dma_fence_put(vm->last_update);
1000 vm->last_update = fence;
Christian König68c62302017-07-11 17:23:29 +02001001 }
1002
Christian König6989f242017-11-30 19:08:05 +01001003 if (!list_empty(&vm->relocated))
1004 goto restart;
1005
1006 return 0;
1007
1008error:
Chunming Zhou196f7482017-12-13 14:22:54 +08001009 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
1010 adev->vm_manager.root_level);
Christian König6989f242017-11-30 19:08:05 +01001011 amdgpu_job_free(job);
Christian König92456b92017-05-12 16:09:26 +02001012 return r;
Christian König194d2162016-10-12 15:13:52 +02001013}
1014
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001015/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001016 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001017 *
1018 * @p: see amdgpu_pte_update_params definition
1019 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001020 * @entry: resulting entry or NULL
1021 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001022 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001023 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001024 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001025void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1026 struct amdgpu_vm_pt **entry,
1027 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001028{
Chunming Zhou196f7482017-12-13 14:22:54 +08001029 unsigned level = p->adev->vm_manager.root_level;
Christian König4e2cb642016-10-25 15:52:28 +02001030
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001031 *parent = NULL;
1032 *entry = &p->vm->root;
1033 while ((*entry)->entries) {
Christian Könige3a1b322017-12-01 13:28:46 +01001034 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
Christian König50783142017-11-27 14:01:51 +01001035
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001036 *parent = *entry;
Christian Könige3a1b322017-12-01 13:28:46 +01001037 *entry = &(*entry)->entries[addr >> shift];
1038 addr &= (1ULL << shift) - 1;
Christian König4e2cb642016-10-25 15:52:28 +02001039 }
1040
Chunming Zhou196f7482017-12-13 14:22:54 +08001041 if (level != AMDGPU_VM_PTB)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001042 *entry = NULL;
1043}
Christian König4e2cb642016-10-25 15:52:28 +02001044
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001045/**
1046 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1047 *
1048 * @p: see amdgpu_pte_update_params definition
1049 * @entry: vm_pt entry to check
1050 * @parent: parent entry
1051 * @nptes: number of PTEs updated with this operation
1052 * @dst: destination address where the PTEs should point to
1053 * @flags: access flags fro the PTEs
1054 *
1055 * Check if we can update the PD with a huge page.
1056 */
Christian Königec5207c2017-08-03 19:24:06 +02001057static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1058 struct amdgpu_vm_pt *entry,
1059 struct amdgpu_vm_pt *parent,
1060 unsigned nptes, uint64_t dst,
1061 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001062{
Christian König373ac642018-01-16 16:54:25 +01001063 uint64_t pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001064
1065 /* In the case of a mixed PT the PDE must point to it*/
Christian König3cc1d3e2017-12-21 15:47:28 +01001066 if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
1067 nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
Christian König4ab40162017-08-03 20:30:50 +02001068 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001069 flags |= AMDGPU_PDE_PTE;
1070 }
1071
Christian König3cc1d3e2017-12-21 15:47:28 +01001072 if (!(flags & AMDGPU_PDE_PTE)) {
1073 if (entry->huge) {
1074 /* Add the entry to the relocated list to update it. */
1075 entry->huge = false;
1076 spin_lock(&p->vm->status_lock);
1077 list_move(&entry->base.vm_status, &p->vm->relocated);
1078 spin_unlock(&p->vm->status_lock);
1079 }
Christian Königec5207c2017-08-03 19:24:06 +02001080 return;
Christian König3cc1d3e2017-12-21 15:47:28 +01001081 }
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001082
Christian König3cc1d3e2017-12-21 15:47:28 +01001083 entry->huge = true;
Christian König132f34e2018-01-12 15:26:08 +01001084 amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
Christian König3de676d2017-11-29 13:27:26 +01001085
Christian König373ac642018-01-16 16:54:25 +01001086 pde = (entry - parent->entries) * 8;
1087 if (parent->base.bo->shadow)
1088 p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
1089 p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
Christian König4e2cb642016-10-25 15:52:28 +02001090}
1091
1092/**
Christian König92696dd2016-08-05 13:56:35 +02001093 * amdgpu_vm_update_ptes - make sure that page tables are valid
1094 *
1095 * @params: see amdgpu_pte_update_params definition
1096 * @vm: requested vm
1097 * @start: start of GPU address range
1098 * @end: end of GPU address range
1099 * @dst: destination address to map to, the next dst inside the function
1100 * @flags: mapping flags
1101 *
1102 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001103 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001104 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001105static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001106 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001107 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001108{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001109 struct amdgpu_device *adev = params->adev;
1110 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001111
Christian König301654a2017-05-16 14:30:27 +02001112 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001113 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001114 unsigned nptes;
Christian König92696dd2016-08-05 13:56:35 +02001115
1116 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001117 for (addr = start; addr < end; addr += nptes,
1118 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1119 struct amdgpu_vm_pt *entry, *parent;
1120
1121 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1122 if (!entry)
1123 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001124
Christian König92696dd2016-08-05 13:56:35 +02001125 if ((addr & ~mask) == (end & ~mask))
1126 nptes = end - addr;
1127 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001128 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001129
Christian Königec5207c2017-08-03 19:24:06 +02001130 amdgpu_vm_handle_huge_pages(params, entry, parent,
1131 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001132 /* We don't need to update PTEs for huge pages */
Christian König78eb2f02017-11-30 15:41:28 +01001133 if (entry->huge)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001134 continue;
1135
Christian König3f3333f2017-08-03 14:02:13 +02001136 pt = entry->base.bo;
Christian König373ac642018-01-16 16:54:25 +01001137 pe_start = (addr & mask) * 8;
1138 if (pt->shadow)
1139 params->func(params, pt->shadow, pe_start, dst, nptes,
1140 AMDGPU_GPU_PAGE_SIZE, flags);
1141 params->func(params, pt, pe_start, dst, nptes,
Christian König301654a2017-05-16 14:30:27 +02001142 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001143 }
1144
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001145 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001146}
1147
1148/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1150 *
Christian König29efc4f2016-08-04 14:52:50 +02001151 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001152 * @vm: requested vm
1153 * @start: first PTE to handle
1154 * @end: last PTE to handle
1155 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001157 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001158 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001159static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001160 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001161 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162{
1163 /**
1164 * The MC L1 TLB supports variable sized pages, based on a fragment
1165 * field in the PTE. When this field is set to a non-zero value, page
1166 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1167 * flags are considered valid for all PTEs within the fragment range
1168 * and corresponding mappings are assumed to be physically contiguous.
1169 *
1170 * The L1 TLB can store a single PTE for the whole fragment,
1171 * significantly increasing the space available for translation
1172 * caching. This leads to large improvements in throughput when the
1173 * TLB is under pressure.
1174 *
1175 * The L2 TLB distributes small and large fragments into two
1176 * asymmetric partitions. The large fragment cache is significantly
1177 * larger. Thus, we try to use large fragments wherever possible.
1178 * Userspace can support this by aligning virtual base address and
1179 * allocation size to the fragment size.
1180 */
Roger He6849d472017-08-30 13:01:19 +08001181 unsigned max_frag = params->adev->vm_manager.fragment_size;
1182 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001183
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001185 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001186 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187
Roger He6849d472017-08-30 13:01:19 +08001188 while (start != end) {
1189 uint64_t frag_flags, frag_end;
1190 unsigned frag;
1191
1192 /* This intentionally wraps around if no bit is set */
1193 frag = min((unsigned)ffs(start) - 1,
1194 (unsigned)fls64(end - start) - 1);
1195 if (frag >= max_frag) {
1196 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1197 frag_end = end & ~((1ULL << max_frag) - 1);
1198 } else {
1199 frag_flags = AMDGPU_PTE_FRAG(frag);
1200 frag_end = start + (1 << frag);
1201 }
1202
1203 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1204 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001205 if (r)
1206 return r;
Roger He6849d472017-08-30 13:01:19 +08001207
1208 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1209 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210 }
1211
Roger He6849d472017-08-30 13:01:19 +08001212 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213}
1214
1215/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001216 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1217 *
1218 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001219 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001220 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001221 * @vm: requested vm
1222 * @start: start of mapped range
1223 * @last: last mapped entry
1224 * @flags: flags for the entries
1225 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226 * @fence: optional resulting fence
1227 *
Christian Königa14faa62016-01-25 14:27:31 +01001228 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001229 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230 */
1231static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001232 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001233 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001234 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001235 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001236 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001237 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001238{
Christian König2d55e452016-02-08 17:37:38 +01001239 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001240 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001241 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001242 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001243 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001244 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001245 int r;
1246
Christian Königafef8b82016-08-12 13:29:18 +02001247 memset(&params, 0, sizeof(params));
1248 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001249 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001250
Christian Königa33cab72017-07-11 17:13:00 +02001251 /* sync to everything on unmapping */
1252 if (!(flags & AMDGPU_PTE_VALID))
1253 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1254
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001255 if (vm->use_cpu_for_update) {
1256 /* params.src is used as flag to indicate system Memory */
1257 if (pages_addr)
1258 params.src = ~0;
1259
1260 /* Wait for PT BOs to be free. PTs share the same resv. object
1261 * as the root PD BO
1262 */
Christian Königa33cab72017-07-11 17:13:00 +02001263 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001264 if (unlikely(r))
1265 return r;
1266
1267 params.func = amdgpu_vm_cpu_set_ptes;
1268 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001269 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1270 addr, flags);
1271 }
1272
Christian König2d55e452016-02-08 17:37:38 +01001273 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001274
Christian Königa14faa62016-01-25 14:27:31 +01001275 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276
1277 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001278 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001279 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001280 *
1281 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 */
Emily Deng104bd2c2017-12-29 13:13:08 +08001283 if (vm->root.base.bo->shadow)
1284 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1285 else
1286 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001287
1288 /* padding, etc. */
1289 ndw = 64;
1290
Christian König570144c2017-08-30 15:38:45 +02001291 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001292 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001293 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001294
Christian Königb0456f92016-08-11 14:06:54 +02001295 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 ndw += nptes * 2;
1297
Christian Königafef8b82016-08-12 13:29:18 +02001298 params.func = amdgpu_vm_do_copy_ptes;
1299
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001300 } else {
1301 /* set page commands needed */
Christian König44e1bae2018-01-24 19:58:45 +01001302 ndw += ncmds * 10;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303
Roger He6849d472017-08-30 13:01:19 +08001304 /* extra commands for begin/end fragments */
Christian König44e1bae2018-01-24 19:58:45 +01001305 ndw += 2 * 10 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001306
1307 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308 }
1309
Christian Königd71518b2016-02-01 12:20:25 +01001310 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1311 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001312 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001313
Christian König29efc4f2016-08-04 14:52:50 +02001314 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001315
Christian König570144c2017-08-30 15:38:45 +02001316 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001317 uint64_t *pte;
1318 unsigned i;
1319
1320 /* Put the PTEs at the end of the IB. */
1321 i = ndw - nptes * 2;
1322 pte= (uint64_t *)&(job->ibs->ptr[i]);
1323 params.src = job->ibs->gpu_addr + i * 4;
1324
1325 for (i = 0; i < nptes; ++i) {
1326 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1327 AMDGPU_GPU_PAGE_SIZE);
1328 pte[i] |= flags;
1329 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001330 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001331 }
1332
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -05001333 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
Christian König3cabaa52016-06-06 10:17:58 +02001334 if (r)
1335 goto error_free;
1336
Christian König3f3333f2017-08-03 14:02:13 +02001337 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001338 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001339 if (r)
1340 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341
Christian König3f3333f2017-08-03 14:02:13 +02001342 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001343 if (r)
1344 goto error_free;
1345
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001346 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1347 if (r)
1348 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349
Christian König29efc4f2016-08-04 14:52:50 +02001350 amdgpu_ring_pad_ib(ring, params.ib);
1351 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001352 r = amdgpu_job_submit(job, ring, &vm->entity,
1353 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001354 if (r)
1355 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356
Christian König3f3333f2017-08-03 14:02:13 +02001357 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001358 dma_fence_put(*fence);
1359 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001360 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001361
1362error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001363 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001364 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001365}
1366
1367/**
Christian Königa14faa62016-01-25 14:27:31 +01001368 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1369 *
1370 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001371 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001372 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001373 * @vm: requested vm
1374 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001375 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001376 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001377 * @fence: optional resulting fence
1378 *
1379 * Split the mapping into smaller chunks so that each update fits
1380 * into a SDMA IB.
1381 * Returns 0 for success, -EINVAL for failure.
1382 */
1383static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001384 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001385 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001386 struct amdgpu_vm *vm,
1387 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001388 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001389 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001390 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001391{
Christian König9fc8fc72017-09-18 13:58:30 +02001392 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001393 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001394 int r;
1395
1396 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1397 * but in case of something, we filter the flags in first place
1398 */
1399 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1400 flags &= ~AMDGPU_PTE_READABLE;
1401 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1402 flags &= ~AMDGPU_PTE_WRITEABLE;
1403
Alex Xie15b31c52017-03-03 16:47:11 -05001404 flags &= ~AMDGPU_PTE_EXECUTABLE;
1405 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1406
Alex Xieb0fd18b2017-03-03 16:49:39 -05001407 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1408 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1409
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001410 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1411 (adev->asic_type >= CHIP_VEGA10)) {
1412 flags |= AMDGPU_PTE_PRT;
1413 flags &= ~AMDGPU_PTE_VALID;
1414 }
1415
Christian Königa14faa62016-01-25 14:27:31 +01001416 trace_amdgpu_vm_bo_update(mapping);
1417
Christian König63e0ba42016-08-16 17:38:37 +02001418 pfn = mapping->offset >> PAGE_SHIFT;
1419 if (nodes) {
1420 while (pfn >= nodes->size) {
1421 pfn -= nodes->size;
1422 ++nodes;
1423 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001424 }
Christian Königa14faa62016-01-25 14:27:31 +01001425
Christian König63e0ba42016-08-16 17:38:37 +02001426 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001427 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001428 uint64_t max_entries;
1429 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001430
Christian König63e0ba42016-08-16 17:38:37 +02001431 if (nodes) {
1432 addr = nodes->start << PAGE_SHIFT;
1433 max_entries = (nodes->size - pfn) *
1434 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1435 } else {
1436 addr = 0;
1437 max_entries = S64_MAX;
1438 }
Christian Königa14faa62016-01-25 14:27:31 +01001439
Christian König63e0ba42016-08-16 17:38:37 +02001440 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001441 uint64_t count;
1442
Christian König457e0fe2017-08-22 12:50:46 +02001443 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001444 for (count = 1; count < max_entries; ++count) {
1445 uint64_t idx = pfn + count;
1446
1447 if (pages_addr[idx] !=
1448 (pages_addr[idx - 1] + PAGE_SIZE))
1449 break;
1450 }
1451
1452 if (count < min_linear_pages) {
1453 addr = pfn << PAGE_SHIFT;
1454 dma_addr = pages_addr;
1455 } else {
1456 addr = pages_addr[pfn];
1457 max_entries = count;
1458 }
1459
Christian König63e0ba42016-08-16 17:38:37 +02001460 } else if (flags & AMDGPU_PTE_VALID) {
1461 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001462 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001463 }
Christian König63e0ba42016-08-16 17:38:37 +02001464
Christian Königa9f87f62017-03-30 14:03:59 +02001465 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001466 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001467 start, last, flags, addr,
1468 fence);
1469 if (r)
1470 return r;
1471
Christian König63e0ba42016-08-16 17:38:37 +02001472 pfn += last - start + 1;
1473 if (nodes && nodes->size == pfn) {
1474 pfn = 0;
1475 ++nodes;
1476 }
Christian Königa14faa62016-01-25 14:27:31 +01001477 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001478
Christian Königa9f87f62017-03-30 14:03:59 +02001479 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001480
1481 return 0;
1482}
1483
1484/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001485 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1486 *
1487 * @adev: amdgpu_device pointer
1488 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001489 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001490 *
1491 * Fill in the page table entries for @bo_va.
1492 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493 */
1494int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1495 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001496 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497{
Christian Königec681542017-08-01 10:51:43 +02001498 struct amdgpu_bo *bo = bo_va->base.bo;
1499 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001500 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001501 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001502 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001503 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001504 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001505 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001506 int r;
1507
Christian Königec681542017-08-01 10:51:43 +02001508 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001509 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001510 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001511 exclusive = NULL;
1512 } else {
Christian König8358dce2016-03-30 10:50:25 +02001513 struct ttm_dma_tt *ttm;
1514
Christian Königec681542017-08-01 10:51:43 +02001515 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001516 nodes = mem->mm_node;
1517 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001518 ttm = container_of(bo_va->base.bo->tbo.ttm,
1519 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001520 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001521 }
Christian Königec681542017-08-01 10:51:43 +02001522 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 }
1524
Christian König457e0fe2017-08-22 12:50:46 +02001525 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001526 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001527 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001528 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001529
Christian König4e55eb32017-09-11 16:54:59 +02001530 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1531 last_update = &vm->last_update;
1532 else
1533 last_update = &bo_va->last_pt_update;
1534
Christian König3d7d4d32017-08-23 16:13:33 +02001535 if (!clear && bo_va->base.moved) {
1536 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001537 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001538
Christian Königcb7b6ec2017-08-15 17:08:12 +02001539 } else if (bo_va->cleared != clear) {
1540 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001541 }
Christian König7fc11952015-07-30 11:53:42 +02001542
1543 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001544 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001545 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001546 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001547 if (r)
1548 return r;
1549 }
1550
Christian König68c62302017-07-11 17:23:29 +02001551 if (vm->use_cpu_for_update) {
1552 /* Flush HDP */
1553 mb();
Christian König69882562018-01-19 14:17:40 +01001554 amdgpu_asic_flush_hdp(adev, NULL);
Christian König68c62302017-07-11 17:23:29 +02001555 }
1556
Christian Königcb7b6ec2017-08-15 17:08:12 +02001557 spin_lock(&vm->status_lock);
1558 list_del_init(&bo_va->base.vm_status);
1559 spin_unlock(&vm->status_lock);
1560
1561 list_splice_init(&bo_va->invalids, &bo_va->valids);
1562 bo_va->cleared = clear;
1563
1564 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1565 list_for_each_entry(mapping, &bo_va->valids, list)
1566 trace_amdgpu_vm_bo_mapping(mapping);
1567 }
1568
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569 return 0;
1570}
1571
1572/**
Christian König284710f2017-01-30 11:09:31 +01001573 * amdgpu_vm_update_prt_state - update the global PRT state
1574 */
1575static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1576{
1577 unsigned long flags;
1578 bool enable;
1579
1580 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001581 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König132f34e2018-01-12 15:26:08 +01001582 adev->gmc.gmc_funcs->set_prt(adev, enable);
Christian König284710f2017-01-30 11:09:31 +01001583 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1584}
1585
1586/**
Christian König4388fc22017-03-13 10:13:36 +01001587 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001588 */
1589static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1590{
Christian König132f34e2018-01-12 15:26:08 +01001591 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001592 return;
1593
Christian König451bc8e2017-02-14 16:02:52 +01001594 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1595 amdgpu_vm_update_prt_state(adev);
1596}
1597
1598/**
Christian König0b15f2f2017-02-14 15:47:03 +01001599 * amdgpu_vm_prt_put - drop a PRT user
1600 */
1601static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1602{
Christian König451bc8e2017-02-14 16:02:52 +01001603 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001604 amdgpu_vm_update_prt_state(adev);
1605}
1606
1607/**
Christian König451bc8e2017-02-14 16:02:52 +01001608 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001609 */
1610static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1611{
1612 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1613
Christian König0b15f2f2017-02-14 15:47:03 +01001614 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001615 kfree(cb);
1616}
1617
1618/**
Christian König451bc8e2017-02-14 16:02:52 +01001619 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1620 */
1621static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1622 struct dma_fence *fence)
1623{
Christian König4388fc22017-03-13 10:13:36 +01001624 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001625
Christian König132f34e2018-01-12 15:26:08 +01001626 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001627 return;
1628
1629 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001630 if (!cb) {
1631 /* Last resort when we are OOM */
1632 if (fence)
1633 dma_fence_wait(fence, false);
1634
Dan Carpenter486a68f2017-04-03 21:41:39 +03001635 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001636 } else {
1637 cb->adev = adev;
1638 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1639 amdgpu_vm_prt_cb))
1640 amdgpu_vm_prt_cb(fence, &cb->cb);
1641 }
1642}
1643
1644/**
Christian König284710f2017-01-30 11:09:31 +01001645 * amdgpu_vm_free_mapping - free a mapping
1646 *
1647 * @adev: amdgpu_device pointer
1648 * @vm: requested vm
1649 * @mapping: mapping to be freed
1650 * @fence: fence of the unmap operation
1651 *
1652 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1653 */
1654static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1655 struct amdgpu_vm *vm,
1656 struct amdgpu_bo_va_mapping *mapping,
1657 struct dma_fence *fence)
1658{
Christian König451bc8e2017-02-14 16:02:52 +01001659 if (mapping->flags & AMDGPU_PTE_PRT)
1660 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001661 kfree(mapping);
1662}
1663
1664/**
Christian König451bc8e2017-02-14 16:02:52 +01001665 * amdgpu_vm_prt_fini - finish all prt mappings
1666 *
1667 * @adev: amdgpu_device pointer
1668 * @vm: requested vm
1669 *
1670 * Register a cleanup callback to disable PRT support after VM dies.
1671 */
1672static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1673{
Christian König3f3333f2017-08-03 14:02:13 +02001674 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001675 struct dma_fence *excl, **shared;
1676 unsigned i, shared_count;
1677 int r;
1678
1679 r = reservation_object_get_fences_rcu(resv, &excl,
1680 &shared_count, &shared);
1681 if (r) {
1682 /* Not enough memory to grab the fence list, as last resort
1683 * block for all the fences to complete.
1684 */
1685 reservation_object_wait_timeout_rcu(resv, true, false,
1686 MAX_SCHEDULE_TIMEOUT);
1687 return;
1688 }
1689
1690 /* Add a callback for each fence in the reservation object */
1691 amdgpu_vm_prt_get(adev);
1692 amdgpu_vm_add_prt_cb(adev, excl);
1693
1694 for (i = 0; i < shared_count; ++i) {
1695 amdgpu_vm_prt_get(adev);
1696 amdgpu_vm_add_prt_cb(adev, shared[i]);
1697 }
1698
1699 kfree(shared);
1700}
1701
1702/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1704 *
1705 * @adev: amdgpu_device pointer
1706 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001707 * @fence: optional resulting fence (unchanged if no work needed to be done
1708 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001709 *
1710 * Make sure all freed BOs are cleared in the PT.
1711 * Returns 0 for success.
1712 *
1713 * PTs have to be reserved and mutex must be locked!
1714 */
1715int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001716 struct amdgpu_vm *vm,
1717 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001718{
1719 struct amdgpu_bo_va_mapping *mapping;
Christian König45843122018-01-25 18:36:15 +01001720 uint64_t init_pte_value = 0;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001721 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 int r;
1723
1724 while (!list_empty(&vm->freed)) {
1725 mapping = list_first_entry(&vm->freed,
1726 struct amdgpu_bo_va_mapping, list);
1727 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001728
Christian König45843122018-01-25 18:36:15 +01001729 if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
Yong Zhao6d16dac2017-08-31 15:55:00 -04001730 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001731
Christian König570144c2017-08-30 15:38:45 +02001732 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001733 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001734 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001735 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001736 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001737 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001738 return r;
Christian König284710f2017-01-30 11:09:31 +01001739 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001741
1742 if (fence && f) {
1743 dma_fence_put(*fence);
1744 *fence = f;
1745 } else {
1746 dma_fence_put(f);
1747 }
1748
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001749 return 0;
1750
1751}
1752
1753/**
Christian König73fb16e2017-08-16 11:13:48 +02001754 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001755 *
1756 * @adev: amdgpu_device pointer
1757 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02001758 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001759 *
Christian König73fb16e2017-08-16 11:13:48 +02001760 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001761 * Returns 0 for success.
1762 *
Christian König73fb16e2017-08-16 11:13:48 +02001763 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001764 */
Christian König73fb16e2017-08-16 11:13:48 +02001765int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02001766 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001767{
Christian König73fb16e2017-08-16 11:13:48 +02001768 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02001769 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001770
1771 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02001772 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02001773 struct amdgpu_bo_va *bo_va;
Christian Königec363e02017-09-01 20:34:27 +02001774 struct reservation_object *resv;
Christian König4e55eb32017-09-11 16:54:59 +02001775
Christian König27c7b9a2017-08-01 11:27:36 +02001776 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02001777 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001779
Christian Königec363e02017-09-01 20:34:27 +02001780 resv = bo_va->base.bo->tbo.resv;
1781
Christian König73fb16e2017-08-16 11:13:48 +02001782 /* Per VM BOs never need to bo cleared in the page tables */
Christian Königec363e02017-09-01 20:34:27 +02001783 if (resv == vm->root.base.bo->tbo.resv)
1784 clear = false;
1785 /* Try to reserve the BO to avoid clearing its ptes */
Christian König9b8cad22018-01-03 13:36:22 +01001786 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
Christian Königec363e02017-09-01 20:34:27 +02001787 clear = false;
1788 /* Somebody else is using the BO right now */
1789 else
1790 clear = true;
Christian König73fb16e2017-08-16 11:13:48 +02001791
1792 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793 if (r)
1794 return r;
1795
Christian Königec363e02017-09-01 20:34:27 +02001796 if (!clear && resv != vm->root.base.bo->tbo.resv)
1797 reservation_object_unlock(resv);
1798
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001799 spin_lock(&vm->status_lock);
1800 }
1801 spin_unlock(&vm->status_lock);
1802
Christian König91e1a522015-07-06 22:06:40 +02001803 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001804}
1805
1806/**
1807 * amdgpu_vm_bo_add - add a bo to a specific vm
1808 *
1809 * @adev: amdgpu_device pointer
1810 * @vm: requested vm
1811 * @bo: amdgpu buffer object
1812 *
Christian König8843dbb2016-01-26 12:17:11 +01001813 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001814 * Add @bo to the list of bos associated with the vm
1815 * Returns newly added bo_va or NULL for failure
1816 *
1817 * Object has to be reserved!
1818 */
1819struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1820 struct amdgpu_vm *vm,
1821 struct amdgpu_bo *bo)
1822{
1823 struct amdgpu_bo_va *bo_va;
1824
1825 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1826 if (bo_va == NULL) {
1827 return NULL;
1828 }
Christian Königec681542017-08-01 10:51:43 +02001829 bo_va->base.vm = vm;
1830 bo_va->base.bo = bo;
1831 INIT_LIST_HEAD(&bo_va->base.bo_list);
1832 INIT_LIST_HEAD(&bo_va->base.vm_status);
1833
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001834 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02001835 INIT_LIST_HEAD(&bo_va->valids);
1836 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01001837
Christian König727ffdf2017-12-22 17:13:03 +01001838 if (!bo)
1839 return bo_va;
1840
1841 list_add_tail(&bo_va->base.bo_list, &bo->va);
1842
1843 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
1844 return bo_va;
1845
1846 if (bo->preferred_domains &
1847 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
1848 return bo_va;
1849
1850 /*
1851 * We checked all the prerequisites, but it looks like this per VM BO
1852 * is currently evicted. add the BO to the evicted list to make sure it
1853 * is validated on next VM use to avoid fault.
1854 * */
1855 spin_lock(&vm->status_lock);
1856 list_move_tail(&bo_va->base.vm_status, &vm->evicted);
1857 spin_unlock(&vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858
1859 return bo_va;
1860}
1861
Christian König73fb16e2017-08-16 11:13:48 +02001862
1863/**
1864 * amdgpu_vm_bo_insert_mapping - insert a new mapping
1865 *
1866 * @adev: amdgpu_device pointer
1867 * @bo_va: bo_va to store the address
1868 * @mapping: the mapping to insert
1869 *
1870 * Insert a new mapping into all structures.
1871 */
1872static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1873 struct amdgpu_bo_va *bo_va,
1874 struct amdgpu_bo_va_mapping *mapping)
1875{
1876 struct amdgpu_vm *vm = bo_va->base.vm;
1877 struct amdgpu_bo *bo = bo_va->base.bo;
1878
Christian Königaebc5e62017-09-06 16:55:16 +02001879 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02001880 list_add(&mapping->list, &bo_va->invalids);
1881 amdgpu_vm_it_insert(mapping, &vm->va);
1882
1883 if (mapping->flags & AMDGPU_PTE_PRT)
1884 amdgpu_vm_prt_get(adev);
1885
1886 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1887 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001888 if (list_empty(&bo_va->base.vm_status))
1889 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02001890 spin_unlock(&vm->status_lock);
1891 }
1892 trace_amdgpu_vm_bo_map(bo_va, mapping);
1893}
1894
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001895/**
1896 * amdgpu_vm_bo_map - map bo inside a vm
1897 *
1898 * @adev: amdgpu_device pointer
1899 * @bo_va: bo_va to store the address
1900 * @saddr: where to map the BO
1901 * @offset: requested offset in the BO
1902 * @flags: attributes of pages (read/write/valid/etc.)
1903 *
1904 * Add a mapping of the BO at the specefied addr into the VM.
1905 * Returns 0 for success, error for failure.
1906 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001907 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001908 */
1909int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1910 struct amdgpu_bo_va *bo_va,
1911 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001912 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001913{
Christian Königa9f87f62017-03-30 14:03:59 +02001914 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02001915 struct amdgpu_bo *bo = bo_va->base.bo;
1916 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001917 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001918
Christian König0be52de2015-05-18 14:37:27 +02001919 /* validate the parameters */
1920 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001921 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001922 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001923
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001924 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001925 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001926 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001927 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001928 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001930 saddr /= AMDGPU_GPU_PAGE_SIZE;
1931 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1932
Christian Königa9f87f62017-03-30 14:03:59 +02001933 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1934 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935 /* bo and tmp overlap, invalid addr */
1936 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02001937 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02001938 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01001939 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940 }
1941
1942 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001943 if (!mapping)
1944 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945
Christian Königa9f87f62017-03-30 14:03:59 +02001946 mapping->start = saddr;
1947 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001948 mapping->offset = offset;
1949 mapping->flags = flags;
1950
Christian König73fb16e2017-08-16 11:13:48 +02001951 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01001952
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001953 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001954}
1955
1956/**
Christian König80f95c52017-03-13 10:13:39 +01001957 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1958 *
1959 * @adev: amdgpu_device pointer
1960 * @bo_va: bo_va to store the address
1961 * @saddr: where to map the BO
1962 * @offset: requested offset in the BO
1963 * @flags: attributes of pages (read/write/valid/etc.)
1964 *
1965 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1966 * mappings as we do so.
1967 * Returns 0 for success, error for failure.
1968 *
1969 * Object has to be reserved and unreserved outside!
1970 */
1971int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1972 struct amdgpu_bo_va *bo_va,
1973 uint64_t saddr, uint64_t offset,
1974 uint64_t size, uint64_t flags)
1975{
1976 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02001977 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01001978 uint64_t eaddr;
1979 int r;
1980
1981 /* validate the parameters */
1982 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1983 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1984 return -EINVAL;
1985
1986 /* make sure object fit at this offset */
1987 eaddr = saddr + size - 1;
1988 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001989 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01001990 return -EINVAL;
1991
1992 /* Allocate all the needed memory */
1993 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1994 if (!mapping)
1995 return -ENOMEM;
1996
Christian Königec681542017-08-01 10:51:43 +02001997 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01001998 if (r) {
1999 kfree(mapping);
2000 return r;
2001 }
2002
2003 saddr /= AMDGPU_GPU_PAGE_SIZE;
2004 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2005
Christian Königa9f87f62017-03-30 14:03:59 +02002006 mapping->start = saddr;
2007 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002008 mapping->offset = offset;
2009 mapping->flags = flags;
2010
Christian König73fb16e2017-08-16 11:13:48 +02002011 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002012
2013 return 0;
2014}
2015
2016/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002017 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2018 *
2019 * @adev: amdgpu_device pointer
2020 * @bo_va: bo_va to remove the address from
2021 * @saddr: where to the BO is mapped
2022 *
2023 * Remove a mapping of the BO at the specefied addr from the VM.
2024 * Returns 0 for success, error for failure.
2025 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002026 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 */
2028int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2029 struct amdgpu_bo_va *bo_va,
2030 uint64_t saddr)
2031{
2032 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002033 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002034 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002035
Christian König6c7fc502015-06-05 20:56:17 +02002036 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002037
Christian König7fc11952015-07-30 11:53:42 +02002038 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002039 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040 break;
2041 }
2042
Christian König7fc11952015-07-30 11:53:42 +02002043 if (&mapping->list == &bo_va->valids) {
2044 valid = false;
2045
2046 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002047 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002048 break;
2049 }
2050
Christian König32b41ac2016-03-08 18:03:27 +01002051 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002052 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002053 }
Christian König32b41ac2016-03-08 18:03:27 +01002054
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002055 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002056 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002057 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002058 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059
Christian Könige17841b2016-03-08 17:52:01 +01002060 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002061 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002062 else
Christian König284710f2017-01-30 11:09:31 +01002063 amdgpu_vm_free_mapping(adev, vm, mapping,
2064 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065
2066 return 0;
2067}
2068
2069/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002070 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2071 *
2072 * @adev: amdgpu_device pointer
2073 * @vm: VM structure to use
2074 * @saddr: start of the range
2075 * @size: size of the range
2076 *
2077 * Remove all mappings in a range, split them as appropriate.
2078 * Returns 0 for success, error for failure.
2079 */
2080int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2081 struct amdgpu_vm *vm,
2082 uint64_t saddr, uint64_t size)
2083{
2084 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002085 LIST_HEAD(removed);
2086 uint64_t eaddr;
2087
2088 eaddr = saddr + size - 1;
2089 saddr /= AMDGPU_GPU_PAGE_SIZE;
2090 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2091
2092 /* Allocate all the needed memory */
2093 before = kzalloc(sizeof(*before), GFP_KERNEL);
2094 if (!before)
2095 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002096 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002097
2098 after = kzalloc(sizeof(*after), GFP_KERNEL);
2099 if (!after) {
2100 kfree(before);
2101 return -ENOMEM;
2102 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002103 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002104
2105 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002106 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2107 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002108 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002109 if (tmp->start < saddr) {
2110 before->start = tmp->start;
2111 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002112 before->offset = tmp->offset;
2113 before->flags = tmp->flags;
2114 list_add(&before->list, &tmp->list);
2115 }
2116
2117 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002118 if (tmp->last > eaddr) {
2119 after->start = eaddr + 1;
2120 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002121 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002122 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002123 after->flags = tmp->flags;
2124 list_add(&after->list, &tmp->list);
2125 }
2126
2127 list_del(&tmp->list);
2128 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002129
2130 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002131 }
2132
2133 /* And free them up */
2134 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002135 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002136 list_del(&tmp->list);
2137
Christian Königa9f87f62017-03-30 14:03:59 +02002138 if (tmp->start < saddr)
2139 tmp->start = saddr;
2140 if (tmp->last > eaddr)
2141 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002142
Christian Königaebc5e62017-09-06 16:55:16 +02002143 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002144 list_add(&tmp->list, &vm->freed);
2145 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2146 }
2147
Junwei Zhang27f6d612017-03-16 16:09:24 +08002148 /* Insert partial mapping before the range */
2149 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002150 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002151 if (before->flags & AMDGPU_PTE_PRT)
2152 amdgpu_vm_prt_get(adev);
2153 } else {
2154 kfree(before);
2155 }
2156
2157 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002158 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002159 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002160 if (after->flags & AMDGPU_PTE_PRT)
2161 amdgpu_vm_prt_get(adev);
2162 } else {
2163 kfree(after);
2164 }
2165
2166 return 0;
2167}
2168
2169/**
Christian Königaebc5e62017-09-06 16:55:16 +02002170 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2171 *
2172 * @vm: the requested VM
2173 *
2174 * Find a mapping by it's address.
2175 */
2176struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2177 uint64_t addr)
2178{
2179 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2180}
2181
2182/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2184 *
2185 * @adev: amdgpu_device pointer
2186 * @bo_va: requested bo_va
2187 *
Christian König8843dbb2016-01-26 12:17:11 +01002188 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002189 *
2190 * Object have to be reserved!
2191 */
2192void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2193 struct amdgpu_bo_va *bo_va)
2194{
2195 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002196 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002197
Christian Königec681542017-08-01 10:51:43 +02002198 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002199
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002200 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002201 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202 spin_unlock(&vm->status_lock);
2203
Christian König7fc11952015-07-30 11:53:42 +02002204 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002205 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002206 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002207 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002208 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002209 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002210 }
Christian König7fc11952015-07-30 11:53:42 +02002211 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2212 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002213 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002214 amdgpu_vm_free_mapping(adev, vm, mapping,
2215 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002216 }
Christian König32b41ac2016-03-08 18:03:27 +01002217
Chris Wilsonf54d1862016-10-25 13:00:45 +01002218 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002220}
2221
2222/**
2223 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2224 *
2225 * @adev: amdgpu_device pointer
2226 * @vm: requested vm
2227 * @bo: amdgpu buffer object
2228 *
Christian König8843dbb2016-01-26 12:17:11 +01002229 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002230 */
2231void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002232 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002233{
Christian Königec681542017-08-01 10:51:43 +02002234 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002235
Christian Königec681542017-08-01 10:51:43 +02002236 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002237 struct amdgpu_vm *vm = bo_base->vm;
2238
Christian König3d7d4d32017-08-23 16:13:33 +02002239 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002240 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2241 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002242 if (bo->tbo.type == ttm_bo_type_kernel)
2243 list_move(&bo_base->vm_status, &vm->evicted);
2244 else
2245 list_move_tail(&bo_base->vm_status,
2246 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002247 spin_unlock(&bo_base->vm->status_lock);
2248 continue;
2249 }
2250
Christian Königea097292017-08-09 14:15:46 +02002251 if (bo->tbo.type == ttm_bo_type_kernel) {
2252 spin_lock(&bo_base->vm->status_lock);
2253 if (list_empty(&bo_base->vm_status))
2254 list_add(&bo_base->vm_status, &vm->relocated);
2255 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002256 continue;
Christian Königea097292017-08-09 14:15:46 +02002257 }
Christian König3f3333f2017-08-03 14:02:13 +02002258
Christian Königec681542017-08-01 10:51:43 +02002259 spin_lock(&bo_base->vm->status_lock);
2260 if (list_empty(&bo_base->vm_status))
Christian König481c2e92017-09-01 14:46:19 +02002261 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002262 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002263 }
2264}
2265
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002266static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2267{
2268 /* Total bits covered by PD + PTs */
2269 unsigned bits = ilog2(vm_size) + 18;
2270
2271 /* Make sure the PD is 4K in size up to 8GB address space.
2272 Above that split equal between PD and PTs */
2273 if (vm_size <= 8)
2274 return (bits - 9);
2275 else
2276 return ((bits + 3) / 2);
2277}
2278
2279/**
Roger Hed07f14b2017-08-15 16:05:59 +08002280 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002281 *
2282 * @adev: amdgpu_device pointer
2283 * @vm_size: the default vm size if it's set auto
2284 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002285void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +01002286 uint32_t fragment_size_default, unsigned max_level,
2287 unsigned max_bits)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002288{
Christian König36539dc2017-11-23 11:16:05 +01002289 uint64_t tmp;
2290
2291 /* adjust vm size first */
Christian Königf3368122017-11-23 12:57:18 +01002292 if (amdgpu_vm_size != -1) {
2293 unsigned max_size = 1 << (max_bits - 30);
2294
Christian Königfdd5faa2017-11-04 16:51:44 +01002295 vm_size = amdgpu_vm_size;
Christian Königf3368122017-11-23 12:57:18 +01002296 if (vm_size > max_size) {
2297 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2298 amdgpu_vm_size, max_size);
2299 vm_size = max_size;
2300 }
2301 }
Christian Königfdd5faa2017-11-04 16:51:44 +01002302
2303 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Christian König36539dc2017-11-23 11:16:05 +01002304
2305 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
Christian König97489122017-11-27 16:22:05 +01002306 if (amdgpu_vm_block_size != -1)
2307 tmp >>= amdgpu_vm_block_size - 9;
Christian König36539dc2017-11-23 11:16:05 +01002308 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2309 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
Chunming Zhou196f7482017-12-13 14:22:54 +08002310 switch (adev->vm_manager.num_level) {
2311 case 3:
2312 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2313 break;
2314 case 2:
2315 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2316 break;
2317 case 1:
2318 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2319 break;
2320 default:
2321 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2322 }
Christian Königb38f41e2017-11-22 17:00:35 +01002323 /* block size depends on vm size and hw setup*/
Christian König97489122017-11-27 16:22:05 +01002324 if (amdgpu_vm_block_size != -1)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002325 adev->vm_manager.block_size =
Christian König97489122017-11-27 16:22:05 +01002326 min((unsigned)amdgpu_vm_block_size, max_bits
2327 - AMDGPU_GPU_PAGE_SHIFT
2328 - 9 * adev->vm_manager.num_level);
2329 else if (adev->vm_manager.num_level > 1)
2330 adev->vm_manager.block_size = 9;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002331 else
Christian König97489122017-11-27 16:22:05 +01002332 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002333
Christian Königb38f41e2017-11-22 17:00:35 +01002334 if (amdgpu_vm_fragment_size == -1)
2335 adev->vm_manager.fragment_size = fragment_size_default;
2336 else
2337 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
Roger Hed07f14b2017-08-15 16:05:59 +08002338
Christian König36539dc2017-11-23 11:16:05 +01002339 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2340 vm_size, adev->vm_manager.num_level + 1,
2341 adev->vm_manager.block_size,
Christian Königfdd5faa2017-11-04 16:51:44 +01002342 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002343}
2344
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345/**
2346 * amdgpu_vm_init - initialize a vm instance
2347 *
2348 * @adev: amdgpu_device pointer
2349 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002350 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002351 *
Christian König8843dbb2016-01-26 12:17:11 +01002352 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002353 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002354int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002355 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002356{
2357 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002358 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002359 unsigned ring_instance;
2360 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002361 struct drm_sched_rq *rq;
Christian Königd3aab672018-01-24 14:57:02 +01002362 unsigned long size;
Christian König13307f72018-01-24 17:19:04 +01002363 uint64_t flags;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002364 int r, i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002365
Davidlohr Buesof808c132017-09-08 16:15:08 -07002366 vm->va = RB_ROOT_CACHED;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002367 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2368 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002369 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002370 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002371 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002372 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002373 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002374
Christian König2bd9ccf2016-02-01 12:53:58 +01002375 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002376
2377 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2378 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2379 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002380 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2381 r = drm_sched_entity_init(&ring->sched, &vm->entity,
Monk Liub3eebe32017-10-23 12:23:29 +08002382 rq, amdgpu_sched_jobs, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002383 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002384 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002385
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002386 vm->pte_support_ats = false;
2387
2388 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002389 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2390 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002391
Christian König13307f72018-01-24 17:19:04 +01002392 if (adev->asic_type == CHIP_RAVEN)
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002393 vm->pte_support_ats = true;
Christian König13307f72018-01-24 17:19:04 +01002394 } else {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002395 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2396 AMDGPU_VM_USE_CPU_FOR_GFX);
Christian König13307f72018-01-24 17:19:04 +01002397 }
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002398 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2399 vm->use_cpu_for_update ? "CPU" : "SDMA");
2400 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2401 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002402 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002403
Christian König13307f72018-01-24 17:19:04 +01002404 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002405 if (vm->use_cpu_for_update)
2406 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2407 else
2408 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2409 AMDGPU_GEM_CREATE_SHADOW);
2410
Christian Königd3aab672018-01-24 14:57:02 +01002411 size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
Christian Königeab3de22018-03-14 14:48:17 -05002412 r = amdgpu_bo_create(adev, size, align, AMDGPU_GEM_DOMAIN_VRAM, flags,
2413 ttm_bo_type_kernel, NULL, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002414 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002415 goto error_free_sched_entity;
2416
Christian Königd3aab672018-01-24 14:57:02 +01002417 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2418 if (r)
2419 goto error_free_root;
2420
Christian König13307f72018-01-24 17:19:04 +01002421 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
Christian König45843122018-01-25 18:36:15 +01002422 adev->vm_manager.root_level,
2423 vm->pte_support_ats);
Christian König13307f72018-01-24 17:19:04 +01002424 if (r)
2425 goto error_unreserve;
2426
Christian König3f3333f2017-08-03 14:02:13 +02002427 vm->root.base.vm = vm;
2428 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
Christian Königd3aab672018-01-24 14:57:02 +01002429 list_add_tail(&vm->root.base.vm_status, &vm->evicted);
2430 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König0a096fb2017-07-12 10:01:48 +02002431
Felix Kuehling02208442017-08-25 20:40:26 -04002432 if (pasid) {
2433 unsigned long flags;
2434
2435 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2436 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2437 GFP_ATOMIC);
2438 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2439 if (r < 0)
2440 goto error_free_root;
2441
2442 vm->pasid = pasid;
2443 }
2444
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002445 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002446 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002447
2448 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002449
Christian König13307f72018-01-24 17:19:04 +01002450error_unreserve:
2451 amdgpu_bo_unreserve(vm->root.base.bo);
2452
Christian König67003a12016-10-12 14:46:26 +02002453error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002454 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2455 amdgpu_bo_unref(&vm->root.base.bo);
2456 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002457
2458error_free_sched_entity:
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002459 drm_sched_entity_fini(&ring->sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002460
2461 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002462}
2463
2464/**
Christian Königf566ceb2016-10-27 20:04:38 +02002465 * amdgpu_vm_free_levels - free PD/PT levels
2466 *
Christian König8f19cd72017-11-30 15:28:03 +01002467 * @adev: amdgpu device structure
2468 * @parent: PD/PT starting level to free
2469 * @level: level of parent structure
Christian Königf566ceb2016-10-27 20:04:38 +02002470 *
2471 * Free the page directory or page table level and all sub levels.
2472 */
Christian König8f19cd72017-11-30 15:28:03 +01002473static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2474 struct amdgpu_vm_pt *parent,
2475 unsigned level)
Christian Königf566ceb2016-10-27 20:04:38 +02002476{
Christian König8f19cd72017-11-30 15:28:03 +01002477 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +02002478
Christian König8f19cd72017-11-30 15:28:03 +01002479 if (parent->base.bo) {
2480 list_del(&parent->base.bo_list);
2481 list_del(&parent->base.vm_status);
2482 amdgpu_bo_unref(&parent->base.bo->shadow);
2483 amdgpu_bo_unref(&parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002484 }
2485
Christian König8f19cd72017-11-30 15:28:03 +01002486 if (parent->entries)
2487 for (i = 0; i < num_entries; i++)
2488 amdgpu_vm_free_levels(adev, &parent->entries[i],
2489 level + 1);
Christian Königf566ceb2016-10-27 20:04:38 +02002490
Christian König8f19cd72017-11-30 15:28:03 +01002491 kvfree(parent->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002492}
2493
2494/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002495 * amdgpu_vm_fini - tear down a vm instance
2496 *
2497 * @adev: amdgpu_device pointer
2498 * @vm: requested vm
2499 *
Christian König8843dbb2016-01-26 12:17:11 +01002500 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002501 * Unbind the VM and remove all bos from the vm bo list
2502 */
2503void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2504{
2505 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König132f34e2018-01-12 15:26:08 +01002506 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002507 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002508 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002509 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002510
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002511 /* Clear pending page faults from IH when the VM is destroyed */
2512 while (kfifo_get(&vm->faults, &fault))
2513 amdgpu_ih_clear_fault(adev, fault);
2514
Felix Kuehling02208442017-08-25 20:40:26 -04002515 if (vm->pasid) {
2516 unsigned long flags;
2517
2518 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2519 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2520 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2521 }
2522
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002523 drm_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002524
Davidlohr Buesof808c132017-09-08 16:15:08 -07002525 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002526 dev_err(adev->dev, "still active bo inside vm\n");
2527 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002528 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2529 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002530 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002531 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002532 kfree(mapping);
2533 }
2534 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002535 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002536 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002537 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002538 }
Christian König284710f2017-01-30 11:09:31 +01002539
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002541 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002542 }
2543
Christian König2642cf12017-10-13 17:24:31 +02002544 root = amdgpu_bo_ref(vm->root.base.bo);
2545 r = amdgpu_bo_reserve(root, true);
2546 if (r) {
2547 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2548 } else {
Chunming Zhou196f7482017-12-13 14:22:54 +08002549 amdgpu_vm_free_levels(adev, &vm->root,
2550 adev->vm_manager.root_level);
Christian König2642cf12017-10-13 17:24:31 +02002551 amdgpu_bo_unreserve(root);
2552 }
2553 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002554 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002555 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
Christian König620f7742017-12-18 16:53:03 +01002556 amdgpu_vmid_free_reserved(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002557}
Christian Königea89f8c2015-11-15 20:52:06 +01002558
2559/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002560 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2561 *
2562 * @adev: amdgpu_device pointer
2563 * @pasid: PASID do identify the VM
2564 *
2565 * This function is expected to be called in interrupt context. Returns
2566 * true if there was fault credit, false otherwise
2567 */
2568bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2569 unsigned int pasid)
2570{
2571 struct amdgpu_vm *vm;
2572
2573 spin_lock(&adev->vm_manager.pasid_lock);
2574 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
Christian Königd9589392018-01-09 19:18:59 +01002575 if (!vm) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002576 /* VM not found, can't track fault credit */
Christian Königd9589392018-01-09 19:18:59 +01002577 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002578 return true;
Christian Königd9589392018-01-09 19:18:59 +01002579 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002580
2581 /* No lock needed. only accessed by IRQ handler */
Christian Königd9589392018-01-09 19:18:59 +01002582 if (!vm->fault_credit) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002583 /* Too many faults in this VM */
Christian Königd9589392018-01-09 19:18:59 +01002584 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002585 return false;
Christian Königd9589392018-01-09 19:18:59 +01002586 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002587
2588 vm->fault_credit--;
Christian Königd9589392018-01-09 19:18:59 +01002589 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002590 return true;
2591}
2592
2593/**
Christian Königa9a78b32016-01-21 10:19:11 +01002594 * amdgpu_vm_manager_init - init the VM manager
2595 *
2596 * @adev: amdgpu_device pointer
2597 *
2598 * Initialize the VM manager structures
2599 */
2600void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2601{
Christian König620f7742017-12-18 16:53:03 +01002602 unsigned i;
Christian Königa9a78b32016-01-21 10:19:11 +01002603
Christian König620f7742017-12-18 16:53:03 +01002604 amdgpu_vmid_mgr_init(adev);
Christian König2d55e452016-02-08 17:37:38 +01002605
Chris Wilsonf54d1862016-10-25 13:00:45 +01002606 adev->vm_manager.fence_context =
2607 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002608 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2609 adev->vm_manager.seqno[i] = 0;
2610
Christian König2d55e452016-02-08 17:37:38 +01002611 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian König284710f2017-01-30 11:09:31 +01002612 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002613 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002614
2615 /* If not overridden by the user, by default, only in large BAR systems
2616 * Compute VM tables will be updated by CPU
2617 */
2618#ifdef CONFIG_X86_64
2619 if (amdgpu_vm_update_mode == -1) {
2620 if (amdgpu_vm_is_large_bar(adev))
2621 adev->vm_manager.vm_update_mode =
2622 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2623 else
2624 adev->vm_manager.vm_update_mode = 0;
2625 } else
2626 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2627#else
2628 adev->vm_manager.vm_update_mode = 0;
2629#endif
2630
Felix Kuehling02208442017-08-25 20:40:26 -04002631 idr_init(&adev->vm_manager.pasid_idr);
2632 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002633}
2634
2635/**
Christian Königea89f8c2015-11-15 20:52:06 +01002636 * amdgpu_vm_manager_fini - cleanup VM manager
2637 *
2638 * @adev: amdgpu_device pointer
2639 *
2640 * Cleanup the VM manager and free resources.
2641 */
2642void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2643{
Felix Kuehling02208442017-08-25 20:40:26 -04002644 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2645 idr_destroy(&adev->vm_manager.pasid_idr);
2646
Christian König620f7742017-12-18 16:53:03 +01002647 amdgpu_vmid_mgr_fini(adev);
Christian Königea89f8c2015-11-15 20:52:06 +01002648}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002649
2650int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2651{
2652 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002653 struct amdgpu_device *adev = dev->dev_private;
2654 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2655 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002656
2657 switch (args->in.op) {
2658 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002659 /* current, we only have requirement to reserve vmid from gfxhub */
Christian König620f7742017-12-18 16:53:03 +01002660 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002661 if (r)
2662 return r;
2663 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002664 case AMDGPU_VM_OP_UNRESERVE_VMID:
Christian König620f7742017-12-18 16:53:03 +01002665 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002666 break;
2667 default:
2668 return -EINVAL;
2669 }
2670
2671 return 0;
2672}