blob: 0572d6072baafdbfecb965e37f2a8c761a67c52c [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),
416 AMDGPU_GPU_PAGE_SIZE, true,
Christian König13307f72018-01-24 17:19:04 +0100417 AMDGPU_GEM_DOMAIN_VRAM, flags,
Christian König8febe612018-01-24 19:55:32 +0100418 NULL, 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önigc0e51932017-04-03 14:16:07 +0200594 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100595 int r;
Christian Königd564a062016-03-01 15:51:53 +0100596
Christian König620f7742017-12-18 16:53:03 +0100597 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
Christian Königf7d015b2017-04-03 14:28:26 +0200598 gds_switch_needed = true;
599 vm_flush_needed = true;
600 }
Christian König971fe9a92016-03-01 15:09:25 +0100601
Monk Liu8fdf0742017-06-06 17:25:13 +0800602 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200603 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100604
Christian Königc0e51932017-04-03 14:16:07 +0200605 if (ring->funcs->init_cond_exec)
606 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100607
Monk Liu8fdf0742017-06-06 17:25:13 +0800608 if (need_pipe_sync)
609 amdgpu_ring_emit_pipeline_sync(ring);
610
Christian Königf7d015b2017-04-03 14:28:26 +0200611 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200612 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800613
Christian Königc4f46f22017-12-18 17:08:25 +0100614 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian König5a4633c2018-01-08 14:48:11 +0100615 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->pasid,
616 job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800617
Christian Königc0e51932017-04-03 14:16:07 +0200618 r = amdgpu_fence_emit(ring, &fence);
619 if (r)
620 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800621
Christian König76456702017-04-06 17:52:39 +0200622 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200623 dma_fence_put(id->last_flush);
624 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800625 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200626 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200627 }
Monk Liue9d672b2017-03-15 12:18:57 +0800628
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800629 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200630 id->gds_base = job->gds_base;
631 id->gds_size = job->gds_size;
632 id->gws_base = job->gws_base;
633 id->gws_size = job->gws_size;
634 id->oa_base = job->oa_base;
635 id->oa_size = job->oa_size;
Christian Königc4f46f22017-12-18 17:08:25 +0100636 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
Christian Königc0e51932017-04-03 14:16:07 +0200637 job->gds_size, job->gws_base,
638 job->gws_size, job->oa_base,
639 job->oa_size);
640 }
641
642 if (ring->funcs->patch_cond_exec)
643 amdgpu_ring_patch_cond_exec(ring, patch_offset);
644
645 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
646 if (ring->funcs->emit_switch_buffer) {
647 amdgpu_ring_emit_switch_buffer(ring);
648 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 }
Christian König41d9eb22016-03-01 16:46:18 +0100650 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100651}
652
653/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
655 *
656 * @vm: requested vm
657 * @bo: requested buffer object
658 *
Christian König8843dbb2016-01-26 12:17:11 +0100659 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 * Search inside the @bos vm list for the requested vm
661 * Returns the found bo_va or NULL if none is found
662 *
663 * Object has to be reserved!
664 */
665struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
666 struct amdgpu_bo *bo)
667{
668 struct amdgpu_bo_va *bo_va;
669
Christian Königec681542017-08-01 10:51:43 +0200670 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
671 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 return bo_va;
673 }
674 }
675 return NULL;
676}
677
678/**
Christian Königafef8b82016-08-12 13:29:18 +0200679 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 *
Christian König29efc4f2016-08-04 14:52:50 +0200681 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100682 * @bo: PD/PT to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400683 * @pe: addr of the page entry
684 * @addr: dst addr to write into pe
685 * @count: number of page entries to update
686 * @incr: increase next addr by incr bytes
687 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688 *
689 * Traces the parameters and calls the right asic functions
690 * to setup the page table using the DMA.
691 */
Christian Königafef8b82016-08-12 13:29:18 +0200692static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100693 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200694 uint64_t pe, uint64_t addr,
695 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800696 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697{
Christian König373ac642018-01-16 16:54:25 +0100698 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200699 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700
Christian Königafef8b82016-08-12 13:29:18 +0200701 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200702 amdgpu_vm_write_pte(params->adev, params->ib, pe,
703 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704
705 } else {
Christian König27c5f362016-08-04 15:02:49 +0200706 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 count, incr, flags);
708 }
709}
710
711/**
Christian Königafef8b82016-08-12 13:29:18 +0200712 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
713 *
714 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100715 * @bo: PD/PT to update
Christian Königafef8b82016-08-12 13:29:18 +0200716 * @pe: addr of the page entry
717 * @addr: dst addr to write into pe
718 * @count: number of page entries to update
719 * @incr: increase next addr by incr bytes
720 * @flags: hw access flags
721 *
722 * Traces the parameters and calls the DMA function to copy the PTEs.
723 */
724static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100725 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200726 uint64_t pe, uint64_t addr,
727 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800728 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200729{
Christian Königec2f05f2016-09-25 16:11:52 +0200730 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200731
Christian König373ac642018-01-16 16:54:25 +0100732 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200733 trace_amdgpu_vm_copy_ptes(pe, src, count);
734
735 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200736}
737
738/**
Christian Königb07c9d22015-11-30 13:26:07 +0100739 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400740 *
Christian Königb07c9d22015-11-30 13:26:07 +0100741 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400742 * @addr: the unmapped addr
743 *
744 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100745 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200747static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748{
749 uint64_t result;
750
Christian Königde9ea7b2016-08-12 11:33:30 +0200751 /* page table offset */
752 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753
Christian Königde9ea7b2016-08-12 11:33:30 +0200754 /* in case cpu page size != gpu page size*/
755 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100756
757 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758
759 return result;
760}
761
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400762/**
763 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
764 *
765 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100766 * @bo: PD/PT to update
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400767 * @pe: kmap addr of the page entry
768 * @addr: dst addr to write into pe
769 * @count: number of page entries to update
770 * @incr: increase next addr by incr bytes
771 * @flags: hw access flags
772 *
773 * Write count number of PT/PD entries directly.
774 */
775static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100776 struct amdgpu_bo *bo,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400777 uint64_t pe, uint64_t addr,
778 unsigned count, uint32_t incr,
779 uint64_t flags)
780{
781 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400782 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400783
Christian König373ac642018-01-16 16:54:25 +0100784 pe += (unsigned long)amdgpu_bo_kptr(bo);
785
Christian König03918b32017-07-11 17:15:37 +0200786 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
787
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400788 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400789 value = params->pages_addr ?
790 amdgpu_vm_map_gart(params->pages_addr, addr) :
791 addr;
Christian König132f34e2018-01-12 15:26:08 +0100792 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
793 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400794 addr += incr;
795 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400796}
797
Christian Königa33cab72017-07-11 17:13:00 +0200798static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
799 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400800{
801 struct amdgpu_sync sync;
802 int r;
803
804 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -0400805 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400806 r = amdgpu_sync_wait(&sync, true);
807 amdgpu_sync_free(&sync);
808
809 return r;
810}
811
Christian Königf8991ba2016-09-16 15:36:49 +0200812/*
Christian König6989f242017-11-30 19:08:05 +0100813 * amdgpu_vm_update_pde - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200814 *
Christian König6989f242017-11-30 19:08:05 +0100815 * @param: parameters for the update
Christian Königf8991ba2016-09-16 15:36:49 +0200816 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200817 * @parent: parent directory
Christian König6989f242017-11-30 19:08:05 +0100818 * @entry: entry to update
Christian Königf8991ba2016-09-16 15:36:49 +0200819 *
Christian König6989f242017-11-30 19:08:05 +0100820 * Makes sure the requested entry in parent is up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200821 */
Christian König6989f242017-11-30 19:08:05 +0100822static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
823 struct amdgpu_vm *vm,
824 struct amdgpu_vm_pt *parent,
825 struct amdgpu_vm_pt *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400826{
Christian König373ac642018-01-16 16:54:25 +0100827 struct amdgpu_bo *bo = parent->base.bo, *pbo;
Christian König3de676d2017-11-29 13:27:26 +0100828 uint64_t pde, pt, flags;
829 unsigned level;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800830
Christian König6989f242017-11-30 19:08:05 +0100831 /* Don't update huge pages here */
832 if (entry->huge)
833 return;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400834
Christian König373ac642018-01-16 16:54:25 +0100835 for (level = 0, pbo = bo->parent; pbo; ++level)
Christian König3de676d2017-11-29 13:27:26 +0100836 pbo = pbo->parent;
837
Chunming Zhou196f7482017-12-13 14:22:54 +0800838 level += params->adev->vm_manager.root_level;
Christian König373ac642018-01-16 16:54:25 +0100839 pt = amdgpu_bo_gpu_offset(entry->base.bo);
Christian König3de676d2017-11-29 13:27:26 +0100840 flags = AMDGPU_PTE_VALID;
Christian König132f34e2018-01-12 15:26:08 +0100841 amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
Christian König373ac642018-01-16 16:54:25 +0100842 pde = (entry - parent->entries) * 8;
843 if (bo->shadow)
844 params->func(params, bo->shadow, pde, pt, 1, 0, flags);
845 params->func(params, bo, pde, pt, 1, 0, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846}
847
Christian König194d2162016-10-12 15:13:52 +0200848/*
Christian König92456b92017-05-12 16:09:26 +0200849 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
850 *
851 * @parent: parent PD
852 *
853 * Mark all PD level as invalid after an error.
854 */
Christian König8f19cd72017-11-30 15:28:03 +0100855static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
856 struct amdgpu_vm *vm,
857 struct amdgpu_vm_pt *parent,
858 unsigned level)
Christian König92456b92017-05-12 16:09:26 +0200859{
Christian König8f19cd72017-11-30 15:28:03 +0100860 unsigned pt_idx, num_entries;
Christian König92456b92017-05-12 16:09:26 +0200861
862 /*
863 * Recurse into the subdirectories. This recursion is harmless because
864 * we only have a maximum of 5 layers.
865 */
Christian König8f19cd72017-11-30 15:28:03 +0100866 num_entries = amdgpu_vm_num_entries(adev, level);
867 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
Christian König92456b92017-05-12 16:09:26 +0200868 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
869
Christian König3f3333f2017-08-03 14:02:13 +0200870 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +0200871 continue;
872
Christian Königea097292017-08-09 14:15:46 +0200873 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +0200874 if (list_empty(&entry->base.vm_status))
875 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +0200876 spin_unlock(&vm->status_lock);
Christian König8f19cd72017-11-30 15:28:03 +0100877 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
Christian König92456b92017-05-12 16:09:26 +0200878 }
879}
880
881/*
Christian König194d2162016-10-12 15:13:52 +0200882 * amdgpu_vm_update_directories - make sure that all directories are valid
883 *
884 * @adev: amdgpu_device pointer
885 * @vm: requested vm
886 *
887 * Makes sure all directories are up to date.
888 * Returns 0 for success, error for failure.
889 */
890int amdgpu_vm_update_directories(struct amdgpu_device *adev,
891 struct amdgpu_vm *vm)
892{
Christian König6989f242017-11-30 19:08:05 +0100893 struct amdgpu_pte_update_params params;
894 struct amdgpu_job *job;
895 unsigned ndw = 0;
Dan Carpenter78aa02c2017-09-30 11:14:13 +0300896 int r = 0;
Christian König92456b92017-05-12 16:09:26 +0200897
Christian König6989f242017-11-30 19:08:05 +0100898 if (list_empty(&vm->relocated))
899 return 0;
900
901restart:
902 memset(&params, 0, sizeof(params));
903 params.adev = adev;
904
905 if (vm->use_cpu_for_update) {
906 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
907 if (unlikely(r))
908 return r;
909
910 params.func = amdgpu_vm_cpu_set_ptes;
911 } else {
912 ndw = 512 * 8;
913 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
914 if (r)
915 return r;
916
917 params.ib = &job->ibs[0];
918 params.func = amdgpu_vm_do_set_ptes;
919 }
920
Christian Königea097292017-08-09 14:15:46 +0200921 spin_lock(&vm->status_lock);
922 while (!list_empty(&vm->relocated)) {
Christian König6989f242017-11-30 19:08:05 +0100923 struct amdgpu_vm_bo_base *bo_base, *parent;
924 struct amdgpu_vm_pt *pt, *entry;
Christian Königea097292017-08-09 14:15:46 +0200925 struct amdgpu_bo *bo;
926
927 bo_base = list_first_entry(&vm->relocated,
928 struct amdgpu_vm_bo_base,
929 vm_status);
Christian König6989f242017-11-30 19:08:05 +0100930 list_del_init(&bo_base->vm_status);
Christian Königea097292017-08-09 14:15:46 +0200931 spin_unlock(&vm->status_lock);
932
933 bo = bo_base->bo->parent;
Christian König6989f242017-11-30 19:08:05 +0100934 if (!bo) {
Christian Königea097292017-08-09 14:15:46 +0200935 spin_lock(&vm->status_lock);
Christian König6989f242017-11-30 19:08:05 +0100936 continue;
Christian Königea097292017-08-09 14:15:46 +0200937 }
Christian König6989f242017-11-30 19:08:05 +0100938
939 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
940 bo_list);
941 pt = container_of(parent, struct amdgpu_vm_pt, base);
942 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
943
944 amdgpu_vm_update_pde(&params, vm, pt, entry);
945
946 spin_lock(&vm->status_lock);
947 if (!vm->use_cpu_for_update &&
948 (ndw - params.ib->length_dw) < 32)
949 break;
Christian Königea097292017-08-09 14:15:46 +0200950 }
951 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +0200952
Christian König68c62302017-07-11 17:23:29 +0200953 if (vm->use_cpu_for_update) {
954 /* Flush HDP */
955 mb();
Christian König69882562018-01-19 14:17:40 +0100956 amdgpu_asic_flush_hdp(adev, NULL);
Christian König6989f242017-11-30 19:08:05 +0100957 } else if (params.ib->length_dw == 0) {
958 amdgpu_job_free(job);
959 } else {
960 struct amdgpu_bo *root = vm->root.base.bo;
961 struct amdgpu_ring *ring;
962 struct dma_fence *fence;
963
964 ring = container_of(vm->entity.sched, struct amdgpu_ring,
965 sched);
966
967 amdgpu_ring_pad_ib(ring, params.ib);
968 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
969 AMDGPU_FENCE_OWNER_VM, false);
Christian König6989f242017-11-30 19:08:05 +0100970 WARN_ON(params.ib->length_dw > ndw);
971 r = amdgpu_job_submit(job, ring, &vm->entity,
972 AMDGPU_FENCE_OWNER_VM, &fence);
973 if (r)
974 goto error;
975
976 amdgpu_bo_fence(root, fence, true);
977 dma_fence_put(vm->last_update);
978 vm->last_update = fence;
Christian König68c62302017-07-11 17:23:29 +0200979 }
980
Christian König6989f242017-11-30 19:08:05 +0100981 if (!list_empty(&vm->relocated))
982 goto restart;
983
984 return 0;
985
986error:
Chunming Zhou196f7482017-12-13 14:22:54 +0800987 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
988 adev->vm_manager.root_level);
Christian König6989f242017-11-30 19:08:05 +0100989 amdgpu_job_free(job);
Christian König92456b92017-05-12 16:09:26 +0200990 return r;
Christian König194d2162016-10-12 15:13:52 +0200991}
992
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400993/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400994 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +0200995 *
996 * @p: see amdgpu_pte_update_params definition
997 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400998 * @entry: resulting entry or NULL
999 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001000 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001001 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001002 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001003void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1004 struct amdgpu_vm_pt **entry,
1005 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001006{
Chunming Zhou196f7482017-12-13 14:22:54 +08001007 unsigned level = p->adev->vm_manager.root_level;
Christian König4e2cb642016-10-25 15:52:28 +02001008
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001009 *parent = NULL;
1010 *entry = &p->vm->root;
1011 while ((*entry)->entries) {
Christian Könige3a1b322017-12-01 13:28:46 +01001012 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
Christian König50783142017-11-27 14:01:51 +01001013
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001014 *parent = *entry;
Christian Könige3a1b322017-12-01 13:28:46 +01001015 *entry = &(*entry)->entries[addr >> shift];
1016 addr &= (1ULL << shift) - 1;
Christian König4e2cb642016-10-25 15:52:28 +02001017 }
1018
Chunming Zhou196f7482017-12-13 14:22:54 +08001019 if (level != AMDGPU_VM_PTB)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001020 *entry = NULL;
1021}
Christian König4e2cb642016-10-25 15:52:28 +02001022
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001023/**
1024 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1025 *
1026 * @p: see amdgpu_pte_update_params definition
1027 * @entry: vm_pt entry to check
1028 * @parent: parent entry
1029 * @nptes: number of PTEs updated with this operation
1030 * @dst: destination address where the PTEs should point to
1031 * @flags: access flags fro the PTEs
1032 *
1033 * Check if we can update the PD with a huge page.
1034 */
Christian Königec5207c2017-08-03 19:24:06 +02001035static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1036 struct amdgpu_vm_pt *entry,
1037 struct amdgpu_vm_pt *parent,
1038 unsigned nptes, uint64_t dst,
1039 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001040{
Christian König373ac642018-01-16 16:54:25 +01001041 uint64_t pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001042
1043 /* In the case of a mixed PT the PDE must point to it*/
Christian König3cc1d3e2017-12-21 15:47:28 +01001044 if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
1045 nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
Christian König4ab40162017-08-03 20:30:50 +02001046 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001047 flags |= AMDGPU_PDE_PTE;
1048 }
1049
Christian König3cc1d3e2017-12-21 15:47:28 +01001050 if (!(flags & AMDGPU_PDE_PTE)) {
1051 if (entry->huge) {
1052 /* Add the entry to the relocated list to update it. */
1053 entry->huge = false;
1054 spin_lock(&p->vm->status_lock);
1055 list_move(&entry->base.vm_status, &p->vm->relocated);
1056 spin_unlock(&p->vm->status_lock);
1057 }
Christian Königec5207c2017-08-03 19:24:06 +02001058 return;
Christian König3cc1d3e2017-12-21 15:47:28 +01001059 }
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001060
Christian König3cc1d3e2017-12-21 15:47:28 +01001061 entry->huge = true;
Christian König132f34e2018-01-12 15:26:08 +01001062 amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
Christian König3de676d2017-11-29 13:27:26 +01001063
Christian König373ac642018-01-16 16:54:25 +01001064 pde = (entry - parent->entries) * 8;
1065 if (parent->base.bo->shadow)
1066 p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
1067 p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
Christian König4e2cb642016-10-25 15:52:28 +02001068}
1069
1070/**
Christian König92696dd2016-08-05 13:56:35 +02001071 * amdgpu_vm_update_ptes - make sure that page tables are valid
1072 *
1073 * @params: see amdgpu_pte_update_params definition
1074 * @vm: requested vm
1075 * @start: start of GPU address range
1076 * @end: end of GPU address range
1077 * @dst: destination address to map to, the next dst inside the function
1078 * @flags: mapping flags
1079 *
1080 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001081 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001082 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001083static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001084 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001085 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001086{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001087 struct amdgpu_device *adev = params->adev;
1088 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001089
Christian König301654a2017-05-16 14:30:27 +02001090 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001091 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001092 unsigned nptes;
Christian König92696dd2016-08-05 13:56:35 +02001093
1094 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001095 for (addr = start; addr < end; addr += nptes,
1096 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1097 struct amdgpu_vm_pt *entry, *parent;
1098
1099 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1100 if (!entry)
1101 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001102
Christian König92696dd2016-08-05 13:56:35 +02001103 if ((addr & ~mask) == (end & ~mask))
1104 nptes = end - addr;
1105 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001106 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001107
Christian Königec5207c2017-08-03 19:24:06 +02001108 amdgpu_vm_handle_huge_pages(params, entry, parent,
1109 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001110 /* We don't need to update PTEs for huge pages */
Christian König78eb2f02017-11-30 15:41:28 +01001111 if (entry->huge)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001112 continue;
1113
Christian König3f3333f2017-08-03 14:02:13 +02001114 pt = entry->base.bo;
Christian König373ac642018-01-16 16:54:25 +01001115 pe_start = (addr & mask) * 8;
1116 if (pt->shadow)
1117 params->func(params, pt->shadow, pe_start, dst, nptes,
1118 AMDGPU_GPU_PAGE_SIZE, flags);
1119 params->func(params, pt, pe_start, dst, nptes,
Christian König301654a2017-05-16 14:30:27 +02001120 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001121 }
1122
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001123 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001124}
1125
1126/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001127 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1128 *
Christian König29efc4f2016-08-04 14:52:50 +02001129 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001130 * @vm: requested vm
1131 * @start: first PTE to handle
1132 * @end: last PTE to handle
1133 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001135 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001137static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001138 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001139 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140{
1141 /**
1142 * The MC L1 TLB supports variable sized pages, based on a fragment
1143 * field in the PTE. When this field is set to a non-zero value, page
1144 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1145 * flags are considered valid for all PTEs within the fragment range
1146 * and corresponding mappings are assumed to be physically contiguous.
1147 *
1148 * The L1 TLB can store a single PTE for the whole fragment,
1149 * significantly increasing the space available for translation
1150 * caching. This leads to large improvements in throughput when the
1151 * TLB is under pressure.
1152 *
1153 * The L2 TLB distributes small and large fragments into two
1154 * asymmetric partitions. The large fragment cache is significantly
1155 * larger. Thus, we try to use large fragments wherever possible.
1156 * Userspace can support this by aligning virtual base address and
1157 * allocation size to the fragment size.
1158 */
Roger He6849d472017-08-30 13:01:19 +08001159 unsigned max_frag = params->adev->vm_manager.fragment_size;
1160 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001161
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001163 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001164 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001165
Roger He6849d472017-08-30 13:01:19 +08001166 while (start != end) {
1167 uint64_t frag_flags, frag_end;
1168 unsigned frag;
1169
1170 /* This intentionally wraps around if no bit is set */
1171 frag = min((unsigned)ffs(start) - 1,
1172 (unsigned)fls64(end - start) - 1);
1173 if (frag >= max_frag) {
1174 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1175 frag_end = end & ~((1ULL << max_frag) - 1);
1176 } else {
1177 frag_flags = AMDGPU_PTE_FRAG(frag);
1178 frag_end = start + (1 << frag);
1179 }
1180
1181 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1182 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001183 if (r)
1184 return r;
Roger He6849d472017-08-30 13:01:19 +08001185
1186 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1187 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 }
1189
Roger He6849d472017-08-30 13:01:19 +08001190 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001191}
1192
1193/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001194 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1195 *
1196 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001197 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001198 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001199 * @vm: requested vm
1200 * @start: start of mapped range
1201 * @last: last mapped entry
1202 * @flags: flags for the entries
1203 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001204 * @fence: optional resulting fence
1205 *
Christian Königa14faa62016-01-25 14:27:31 +01001206 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208 */
1209static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001210 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001211 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001213 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001214 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001215 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001216{
Christian König2d55e452016-02-08 17:37:38 +01001217 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001218 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001220 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001221 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001222 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 int r;
1224
Christian Königafef8b82016-08-12 13:29:18 +02001225 memset(&params, 0, sizeof(params));
1226 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001227 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001228
Christian Königa33cab72017-07-11 17:13:00 +02001229 /* sync to everything on unmapping */
1230 if (!(flags & AMDGPU_PTE_VALID))
1231 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1232
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001233 if (vm->use_cpu_for_update) {
1234 /* params.src is used as flag to indicate system Memory */
1235 if (pages_addr)
1236 params.src = ~0;
1237
1238 /* Wait for PT BOs to be free. PTs share the same resv. object
1239 * as the root PD BO
1240 */
Christian Königa33cab72017-07-11 17:13:00 +02001241 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001242 if (unlikely(r))
1243 return r;
1244
1245 params.func = amdgpu_vm_cpu_set_ptes;
1246 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001247 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1248 addr, flags);
1249 }
1250
Christian König2d55e452016-02-08 17:37:38 +01001251 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001252
Christian Königa14faa62016-01-25 14:27:31 +01001253 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001254
1255 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001256 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001258 *
1259 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260 */
Emily Deng104bd2c2017-12-29 13:13:08 +08001261 if (vm->root.base.bo->shadow)
1262 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1263 else
1264 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001265
1266 /* padding, etc. */
1267 ndw = 64;
1268
Christian König570144c2017-08-30 15:38:45 +02001269 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001270 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001271 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001272
Christian Königb0456f92016-08-11 14:06:54 +02001273 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001274 ndw += nptes * 2;
1275
Christian Königafef8b82016-08-12 13:29:18 +02001276 params.func = amdgpu_vm_do_copy_ptes;
1277
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278 } else {
1279 /* set page commands needed */
Christian König44e1bae2018-01-24 19:58:45 +01001280 ndw += ncmds * 10;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281
Roger He6849d472017-08-30 13:01:19 +08001282 /* extra commands for begin/end fragments */
Christian König44e1bae2018-01-24 19:58:45 +01001283 ndw += 2 * 10 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001284
1285 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001286 }
1287
Christian Königd71518b2016-02-01 12:20:25 +01001288 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1289 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001291
Christian König29efc4f2016-08-04 14:52:50 +02001292 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001293
Christian König570144c2017-08-30 15:38:45 +02001294 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001295 uint64_t *pte;
1296 unsigned i;
1297
1298 /* Put the PTEs at the end of the IB. */
1299 i = ndw - nptes * 2;
1300 pte= (uint64_t *)&(job->ibs->ptr[i]);
1301 params.src = job->ibs->gpu_addr + i * 4;
1302
1303 for (i = 0; i < nptes; ++i) {
1304 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1305 AMDGPU_GPU_PAGE_SIZE);
1306 pte[i] |= flags;
1307 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001308 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001309 }
1310
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -05001311 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
Christian König3cabaa52016-06-06 10:17:58 +02001312 if (r)
1313 goto error_free;
1314
Christian König3f3333f2017-08-03 14:02:13 +02001315 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001316 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001317 if (r)
1318 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001319
Christian König3f3333f2017-08-03 14:02:13 +02001320 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001321 if (r)
1322 goto error_free;
1323
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001324 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1325 if (r)
1326 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001327
Christian König29efc4f2016-08-04 14:52:50 +02001328 amdgpu_ring_pad_ib(ring, params.ib);
1329 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001330 r = amdgpu_job_submit(job, ring, &vm->entity,
1331 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001332 if (r)
1333 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001334
Christian König3f3333f2017-08-03 14:02:13 +02001335 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001336 dma_fence_put(*fence);
1337 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001338 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001339
1340error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001341 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001342 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001343}
1344
1345/**
Christian Königa14faa62016-01-25 14:27:31 +01001346 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1347 *
1348 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001349 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001350 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001351 * @vm: requested vm
1352 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001353 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001354 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001355 * @fence: optional resulting fence
1356 *
1357 * Split the mapping into smaller chunks so that each update fits
1358 * into a SDMA IB.
1359 * Returns 0 for success, -EINVAL for failure.
1360 */
1361static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001362 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001363 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001364 struct amdgpu_vm *vm,
1365 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001366 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001367 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001368 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001369{
Christian König9fc8fc72017-09-18 13:58:30 +02001370 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001371 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001372 int r;
1373
1374 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1375 * but in case of something, we filter the flags in first place
1376 */
1377 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1378 flags &= ~AMDGPU_PTE_READABLE;
1379 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1380 flags &= ~AMDGPU_PTE_WRITEABLE;
1381
Alex Xie15b31c52017-03-03 16:47:11 -05001382 flags &= ~AMDGPU_PTE_EXECUTABLE;
1383 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1384
Alex Xieb0fd18b2017-03-03 16:49:39 -05001385 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1386 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1387
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001388 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1389 (adev->asic_type >= CHIP_VEGA10)) {
1390 flags |= AMDGPU_PTE_PRT;
1391 flags &= ~AMDGPU_PTE_VALID;
1392 }
1393
Christian Königa14faa62016-01-25 14:27:31 +01001394 trace_amdgpu_vm_bo_update(mapping);
1395
Christian König63e0ba42016-08-16 17:38:37 +02001396 pfn = mapping->offset >> PAGE_SHIFT;
1397 if (nodes) {
1398 while (pfn >= nodes->size) {
1399 pfn -= nodes->size;
1400 ++nodes;
1401 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001402 }
Christian Königa14faa62016-01-25 14:27:31 +01001403
Christian König63e0ba42016-08-16 17:38:37 +02001404 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001405 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001406 uint64_t max_entries;
1407 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001408
Christian König63e0ba42016-08-16 17:38:37 +02001409 if (nodes) {
1410 addr = nodes->start << PAGE_SHIFT;
1411 max_entries = (nodes->size - pfn) *
1412 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1413 } else {
1414 addr = 0;
1415 max_entries = S64_MAX;
1416 }
Christian Königa14faa62016-01-25 14:27:31 +01001417
Christian König63e0ba42016-08-16 17:38:37 +02001418 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001419 uint64_t count;
1420
Christian König457e0fe2017-08-22 12:50:46 +02001421 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001422 for (count = 1; count < max_entries; ++count) {
1423 uint64_t idx = pfn + count;
1424
1425 if (pages_addr[idx] !=
1426 (pages_addr[idx - 1] + PAGE_SIZE))
1427 break;
1428 }
1429
1430 if (count < min_linear_pages) {
1431 addr = pfn << PAGE_SHIFT;
1432 dma_addr = pages_addr;
1433 } else {
1434 addr = pages_addr[pfn];
1435 max_entries = count;
1436 }
1437
Christian König63e0ba42016-08-16 17:38:37 +02001438 } else if (flags & AMDGPU_PTE_VALID) {
1439 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001440 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001441 }
Christian König63e0ba42016-08-16 17:38:37 +02001442
Christian Königa9f87f62017-03-30 14:03:59 +02001443 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001444 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001445 start, last, flags, addr,
1446 fence);
1447 if (r)
1448 return r;
1449
Christian König63e0ba42016-08-16 17:38:37 +02001450 pfn += last - start + 1;
1451 if (nodes && nodes->size == pfn) {
1452 pfn = 0;
1453 ++nodes;
1454 }
Christian Königa14faa62016-01-25 14:27:31 +01001455 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001456
Christian Königa9f87f62017-03-30 14:03:59 +02001457 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001458
1459 return 0;
1460}
1461
1462/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001463 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1464 *
1465 * @adev: amdgpu_device pointer
1466 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001467 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001468 *
1469 * Fill in the page table entries for @bo_va.
1470 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001471 */
1472int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1473 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001474 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001475{
Christian Königec681542017-08-01 10:51:43 +02001476 struct amdgpu_bo *bo = bo_va->base.bo;
1477 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001478 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001479 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001480 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001481 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001482 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001483 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484 int r;
1485
Christian Königec681542017-08-01 10:51:43 +02001486 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001487 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001488 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001489 exclusive = NULL;
1490 } else {
Christian König8358dce2016-03-30 10:50:25 +02001491 struct ttm_dma_tt *ttm;
1492
Christian Königec681542017-08-01 10:51:43 +02001493 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001494 nodes = mem->mm_node;
1495 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001496 ttm = container_of(bo_va->base.bo->tbo.ttm,
1497 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001498 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001499 }
Christian Königec681542017-08-01 10:51:43 +02001500 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001501 }
1502
Christian König457e0fe2017-08-22 12:50:46 +02001503 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001504 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001505 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001506 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001507
Christian König4e55eb32017-09-11 16:54:59 +02001508 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1509 last_update = &vm->last_update;
1510 else
1511 last_update = &bo_va->last_pt_update;
1512
Christian König3d7d4d32017-08-23 16:13:33 +02001513 if (!clear && bo_va->base.moved) {
1514 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001515 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001516
Christian Königcb7b6ec2017-08-15 17:08:12 +02001517 } else if (bo_va->cleared != clear) {
1518 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001519 }
Christian König7fc11952015-07-30 11:53:42 +02001520
1521 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001522 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001523 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001524 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525 if (r)
1526 return r;
1527 }
1528
Christian König68c62302017-07-11 17:23:29 +02001529 if (vm->use_cpu_for_update) {
1530 /* Flush HDP */
1531 mb();
Christian König69882562018-01-19 14:17:40 +01001532 amdgpu_asic_flush_hdp(adev, NULL);
Christian König68c62302017-07-11 17:23:29 +02001533 }
1534
Christian Königcb7b6ec2017-08-15 17:08:12 +02001535 spin_lock(&vm->status_lock);
1536 list_del_init(&bo_va->base.vm_status);
1537 spin_unlock(&vm->status_lock);
1538
1539 list_splice_init(&bo_va->invalids, &bo_va->valids);
1540 bo_va->cleared = clear;
1541
1542 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1543 list_for_each_entry(mapping, &bo_va->valids, list)
1544 trace_amdgpu_vm_bo_mapping(mapping);
1545 }
1546
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001547 return 0;
1548}
1549
1550/**
Christian König284710f2017-01-30 11:09:31 +01001551 * amdgpu_vm_update_prt_state - update the global PRT state
1552 */
1553static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1554{
1555 unsigned long flags;
1556 bool enable;
1557
1558 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001559 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König132f34e2018-01-12 15:26:08 +01001560 adev->gmc.gmc_funcs->set_prt(adev, enable);
Christian König284710f2017-01-30 11:09:31 +01001561 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1562}
1563
1564/**
Christian König4388fc22017-03-13 10:13:36 +01001565 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001566 */
1567static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1568{
Christian König132f34e2018-01-12 15:26:08 +01001569 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001570 return;
1571
Christian König451bc8e2017-02-14 16:02:52 +01001572 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1573 amdgpu_vm_update_prt_state(adev);
1574}
1575
1576/**
Christian König0b15f2f2017-02-14 15:47:03 +01001577 * amdgpu_vm_prt_put - drop a PRT user
1578 */
1579static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1580{
Christian König451bc8e2017-02-14 16:02:52 +01001581 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001582 amdgpu_vm_update_prt_state(adev);
1583}
1584
1585/**
Christian König451bc8e2017-02-14 16:02:52 +01001586 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001587 */
1588static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1589{
1590 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1591
Christian König0b15f2f2017-02-14 15:47:03 +01001592 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001593 kfree(cb);
1594}
1595
1596/**
Christian König451bc8e2017-02-14 16:02:52 +01001597 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1598 */
1599static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1600 struct dma_fence *fence)
1601{
Christian König4388fc22017-03-13 10:13:36 +01001602 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001603
Christian König132f34e2018-01-12 15:26:08 +01001604 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001605 return;
1606
1607 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001608 if (!cb) {
1609 /* Last resort when we are OOM */
1610 if (fence)
1611 dma_fence_wait(fence, false);
1612
Dan Carpenter486a68f2017-04-03 21:41:39 +03001613 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001614 } else {
1615 cb->adev = adev;
1616 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1617 amdgpu_vm_prt_cb))
1618 amdgpu_vm_prt_cb(fence, &cb->cb);
1619 }
1620}
1621
1622/**
Christian König284710f2017-01-30 11:09:31 +01001623 * amdgpu_vm_free_mapping - free a mapping
1624 *
1625 * @adev: amdgpu_device pointer
1626 * @vm: requested vm
1627 * @mapping: mapping to be freed
1628 * @fence: fence of the unmap operation
1629 *
1630 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1631 */
1632static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1633 struct amdgpu_vm *vm,
1634 struct amdgpu_bo_va_mapping *mapping,
1635 struct dma_fence *fence)
1636{
Christian König451bc8e2017-02-14 16:02:52 +01001637 if (mapping->flags & AMDGPU_PTE_PRT)
1638 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001639 kfree(mapping);
1640}
1641
1642/**
Christian König451bc8e2017-02-14 16:02:52 +01001643 * amdgpu_vm_prt_fini - finish all prt mappings
1644 *
1645 * @adev: amdgpu_device pointer
1646 * @vm: requested vm
1647 *
1648 * Register a cleanup callback to disable PRT support after VM dies.
1649 */
1650static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1651{
Christian König3f3333f2017-08-03 14:02:13 +02001652 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001653 struct dma_fence *excl, **shared;
1654 unsigned i, shared_count;
1655 int r;
1656
1657 r = reservation_object_get_fences_rcu(resv, &excl,
1658 &shared_count, &shared);
1659 if (r) {
1660 /* Not enough memory to grab the fence list, as last resort
1661 * block for all the fences to complete.
1662 */
1663 reservation_object_wait_timeout_rcu(resv, true, false,
1664 MAX_SCHEDULE_TIMEOUT);
1665 return;
1666 }
1667
1668 /* Add a callback for each fence in the reservation object */
1669 amdgpu_vm_prt_get(adev);
1670 amdgpu_vm_add_prt_cb(adev, excl);
1671
1672 for (i = 0; i < shared_count; ++i) {
1673 amdgpu_vm_prt_get(adev);
1674 amdgpu_vm_add_prt_cb(adev, shared[i]);
1675 }
1676
1677 kfree(shared);
1678}
1679
1680/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001681 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1682 *
1683 * @adev: amdgpu_device pointer
1684 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001685 * @fence: optional resulting fence (unchanged if no work needed to be done
1686 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001687 *
1688 * Make sure all freed BOs are cleared in the PT.
1689 * Returns 0 for success.
1690 *
1691 * PTs have to be reserved and mutex must be locked!
1692 */
1693int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001694 struct amdgpu_vm *vm,
1695 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001696{
1697 struct amdgpu_bo_va_mapping *mapping;
Christian König45843122018-01-25 18:36:15 +01001698 uint64_t init_pte_value = 0;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001699 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 int r;
1701
1702 while (!list_empty(&vm->freed)) {
1703 mapping = list_first_entry(&vm->freed,
1704 struct amdgpu_bo_va_mapping, list);
1705 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001706
Christian König45843122018-01-25 18:36:15 +01001707 if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
Yong Zhao6d16dac2017-08-31 15:55:00 -04001708 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001709
Christian König570144c2017-08-30 15:38:45 +02001710 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001711 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001712 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001713 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001714 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001715 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001716 return r;
Christian König284710f2017-01-30 11:09:31 +01001717 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001718 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001719
1720 if (fence && f) {
1721 dma_fence_put(*fence);
1722 *fence = f;
1723 } else {
1724 dma_fence_put(f);
1725 }
1726
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001727 return 0;
1728
1729}
1730
1731/**
Christian König73fb16e2017-08-16 11:13:48 +02001732 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001733 *
1734 * @adev: amdgpu_device pointer
1735 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02001736 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737 *
Christian König73fb16e2017-08-16 11:13:48 +02001738 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739 * Returns 0 for success.
1740 *
Christian König73fb16e2017-08-16 11:13:48 +02001741 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001742 */
Christian König73fb16e2017-08-16 11:13:48 +02001743int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02001744 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001745{
Christian König73fb16e2017-08-16 11:13:48 +02001746 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02001747 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001748
1749 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02001750 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02001751 struct amdgpu_bo_va *bo_va;
Christian Königec363e02017-09-01 20:34:27 +02001752 struct reservation_object *resv;
Christian König4e55eb32017-09-11 16:54:59 +02001753
Christian König27c7b9a2017-08-01 11:27:36 +02001754 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02001755 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001756 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001757
Christian Königec363e02017-09-01 20:34:27 +02001758 resv = bo_va->base.bo->tbo.resv;
1759
Christian König73fb16e2017-08-16 11:13:48 +02001760 /* Per VM BOs never need to bo cleared in the page tables */
Christian Königec363e02017-09-01 20:34:27 +02001761 if (resv == vm->root.base.bo->tbo.resv)
1762 clear = false;
1763 /* Try to reserve the BO to avoid clearing its ptes */
Christian König9b8cad22018-01-03 13:36:22 +01001764 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
Christian Königec363e02017-09-01 20:34:27 +02001765 clear = false;
1766 /* Somebody else is using the BO right now */
1767 else
1768 clear = true;
Christian König73fb16e2017-08-16 11:13:48 +02001769
1770 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001771 if (r)
1772 return r;
1773
Christian Königec363e02017-09-01 20:34:27 +02001774 if (!clear && resv != vm->root.base.bo->tbo.resv)
1775 reservation_object_unlock(resv);
1776
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001777 spin_lock(&vm->status_lock);
1778 }
1779 spin_unlock(&vm->status_lock);
1780
Christian König91e1a522015-07-06 22:06:40 +02001781 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001782}
1783
1784/**
1785 * amdgpu_vm_bo_add - add a bo to a specific vm
1786 *
1787 * @adev: amdgpu_device pointer
1788 * @vm: requested vm
1789 * @bo: amdgpu buffer object
1790 *
Christian König8843dbb2016-01-26 12:17:11 +01001791 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001792 * Add @bo to the list of bos associated with the vm
1793 * Returns newly added bo_va or NULL for failure
1794 *
1795 * Object has to be reserved!
1796 */
1797struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1798 struct amdgpu_vm *vm,
1799 struct amdgpu_bo *bo)
1800{
1801 struct amdgpu_bo_va *bo_va;
1802
1803 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1804 if (bo_va == NULL) {
1805 return NULL;
1806 }
Christian Königec681542017-08-01 10:51:43 +02001807 bo_va->base.vm = vm;
1808 bo_va->base.bo = bo;
1809 INIT_LIST_HEAD(&bo_va->base.bo_list);
1810 INIT_LIST_HEAD(&bo_va->base.vm_status);
1811
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001812 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02001813 INIT_LIST_HEAD(&bo_va->valids);
1814 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01001815
Christian König727ffdf2017-12-22 17:13:03 +01001816 if (!bo)
1817 return bo_va;
1818
1819 list_add_tail(&bo_va->base.bo_list, &bo->va);
1820
1821 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
1822 return bo_va;
1823
1824 if (bo->preferred_domains &
1825 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
1826 return bo_va;
1827
1828 /*
1829 * We checked all the prerequisites, but it looks like this per VM BO
1830 * is currently evicted. add the BO to the evicted list to make sure it
1831 * is validated on next VM use to avoid fault.
1832 * */
1833 spin_lock(&vm->status_lock);
1834 list_move_tail(&bo_va->base.vm_status, &vm->evicted);
1835 spin_unlock(&vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001836
1837 return bo_va;
1838}
1839
Christian König73fb16e2017-08-16 11:13:48 +02001840
1841/**
1842 * amdgpu_vm_bo_insert_mapping - insert a new mapping
1843 *
1844 * @adev: amdgpu_device pointer
1845 * @bo_va: bo_va to store the address
1846 * @mapping: the mapping to insert
1847 *
1848 * Insert a new mapping into all structures.
1849 */
1850static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1851 struct amdgpu_bo_va *bo_va,
1852 struct amdgpu_bo_va_mapping *mapping)
1853{
1854 struct amdgpu_vm *vm = bo_va->base.vm;
1855 struct amdgpu_bo *bo = bo_va->base.bo;
1856
Christian Königaebc5e62017-09-06 16:55:16 +02001857 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02001858 list_add(&mapping->list, &bo_va->invalids);
1859 amdgpu_vm_it_insert(mapping, &vm->va);
1860
1861 if (mapping->flags & AMDGPU_PTE_PRT)
1862 amdgpu_vm_prt_get(adev);
1863
1864 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1865 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001866 if (list_empty(&bo_va->base.vm_status))
1867 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02001868 spin_unlock(&vm->status_lock);
1869 }
1870 trace_amdgpu_vm_bo_map(bo_va, mapping);
1871}
1872
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001873/**
1874 * amdgpu_vm_bo_map - map bo inside a vm
1875 *
1876 * @adev: amdgpu_device pointer
1877 * @bo_va: bo_va to store the address
1878 * @saddr: where to map the BO
1879 * @offset: requested offset in the BO
1880 * @flags: attributes of pages (read/write/valid/etc.)
1881 *
1882 * Add a mapping of the BO at the specefied addr into the VM.
1883 * Returns 0 for success, error for failure.
1884 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001885 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886 */
1887int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1888 struct amdgpu_bo_va *bo_va,
1889 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001890 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001891{
Christian Königa9f87f62017-03-30 14:03:59 +02001892 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02001893 struct amdgpu_bo *bo = bo_va->base.bo;
1894 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001895 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896
Christian König0be52de2015-05-18 14:37:27 +02001897 /* validate the parameters */
1898 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001899 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001900 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001901
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001902 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001903 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001904 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001905 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001906 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001907
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001908 saddr /= AMDGPU_GPU_PAGE_SIZE;
1909 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1910
Christian Königa9f87f62017-03-30 14:03:59 +02001911 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1912 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001913 /* bo and tmp overlap, invalid addr */
1914 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02001915 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02001916 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01001917 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001918 }
1919
1920 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001921 if (!mapping)
1922 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923
Christian Königa9f87f62017-03-30 14:03:59 +02001924 mapping->start = saddr;
1925 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001926 mapping->offset = offset;
1927 mapping->flags = flags;
1928
Christian König73fb16e2017-08-16 11:13:48 +02001929 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01001930
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001932}
1933
1934/**
Christian König80f95c52017-03-13 10:13:39 +01001935 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1936 *
1937 * @adev: amdgpu_device pointer
1938 * @bo_va: bo_va to store the address
1939 * @saddr: where to map the BO
1940 * @offset: requested offset in the BO
1941 * @flags: attributes of pages (read/write/valid/etc.)
1942 *
1943 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1944 * mappings as we do so.
1945 * Returns 0 for success, error for failure.
1946 *
1947 * Object has to be reserved and unreserved outside!
1948 */
1949int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1950 struct amdgpu_bo_va *bo_va,
1951 uint64_t saddr, uint64_t offset,
1952 uint64_t size, uint64_t flags)
1953{
1954 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02001955 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01001956 uint64_t eaddr;
1957 int r;
1958
1959 /* validate the parameters */
1960 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1961 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1962 return -EINVAL;
1963
1964 /* make sure object fit at this offset */
1965 eaddr = saddr + size - 1;
1966 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001967 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01001968 return -EINVAL;
1969
1970 /* Allocate all the needed memory */
1971 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1972 if (!mapping)
1973 return -ENOMEM;
1974
Christian Königec681542017-08-01 10:51:43 +02001975 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01001976 if (r) {
1977 kfree(mapping);
1978 return r;
1979 }
1980
1981 saddr /= AMDGPU_GPU_PAGE_SIZE;
1982 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1983
Christian Königa9f87f62017-03-30 14:03:59 +02001984 mapping->start = saddr;
1985 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01001986 mapping->offset = offset;
1987 mapping->flags = flags;
1988
Christian König73fb16e2017-08-16 11:13:48 +02001989 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01001990
1991 return 0;
1992}
1993
1994/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001995 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1996 *
1997 * @adev: amdgpu_device pointer
1998 * @bo_va: bo_va to remove the address from
1999 * @saddr: where to the BO is mapped
2000 *
2001 * Remove a mapping of the BO at the specefied addr from the VM.
2002 * Returns 0 for success, error for failure.
2003 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002004 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 */
2006int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2007 struct amdgpu_bo_va *bo_va,
2008 uint64_t saddr)
2009{
2010 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002011 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002012 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013
Christian König6c7fc502015-06-05 20:56:17 +02002014 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002015
Christian König7fc11952015-07-30 11:53:42 +02002016 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002017 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002018 break;
2019 }
2020
Christian König7fc11952015-07-30 11:53:42 +02002021 if (&mapping->list == &bo_va->valids) {
2022 valid = false;
2023
2024 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002025 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002026 break;
2027 }
2028
Christian König32b41ac2016-03-08 18:03:27 +01002029 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002030 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 }
Christian König32b41ac2016-03-08 18:03:27 +01002032
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002033 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002034 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002035 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002036 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037
Christian Könige17841b2016-03-08 17:52:01 +01002038 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002039 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002040 else
Christian König284710f2017-01-30 11:09:31 +01002041 amdgpu_vm_free_mapping(adev, vm, mapping,
2042 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002043
2044 return 0;
2045}
2046
2047/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002048 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2049 *
2050 * @adev: amdgpu_device pointer
2051 * @vm: VM structure to use
2052 * @saddr: start of the range
2053 * @size: size of the range
2054 *
2055 * Remove all mappings in a range, split them as appropriate.
2056 * Returns 0 for success, error for failure.
2057 */
2058int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2059 struct amdgpu_vm *vm,
2060 uint64_t saddr, uint64_t size)
2061{
2062 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002063 LIST_HEAD(removed);
2064 uint64_t eaddr;
2065
2066 eaddr = saddr + size - 1;
2067 saddr /= AMDGPU_GPU_PAGE_SIZE;
2068 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2069
2070 /* Allocate all the needed memory */
2071 before = kzalloc(sizeof(*before), GFP_KERNEL);
2072 if (!before)
2073 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002074 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002075
2076 after = kzalloc(sizeof(*after), GFP_KERNEL);
2077 if (!after) {
2078 kfree(before);
2079 return -ENOMEM;
2080 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002081 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002082
2083 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002084 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2085 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002086 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002087 if (tmp->start < saddr) {
2088 before->start = tmp->start;
2089 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002090 before->offset = tmp->offset;
2091 before->flags = tmp->flags;
2092 list_add(&before->list, &tmp->list);
2093 }
2094
2095 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002096 if (tmp->last > eaddr) {
2097 after->start = eaddr + 1;
2098 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002099 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002100 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002101 after->flags = tmp->flags;
2102 list_add(&after->list, &tmp->list);
2103 }
2104
2105 list_del(&tmp->list);
2106 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002107
2108 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002109 }
2110
2111 /* And free them up */
2112 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002113 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002114 list_del(&tmp->list);
2115
Christian Königa9f87f62017-03-30 14:03:59 +02002116 if (tmp->start < saddr)
2117 tmp->start = saddr;
2118 if (tmp->last > eaddr)
2119 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002120
Christian Königaebc5e62017-09-06 16:55:16 +02002121 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002122 list_add(&tmp->list, &vm->freed);
2123 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2124 }
2125
Junwei Zhang27f6d612017-03-16 16:09:24 +08002126 /* Insert partial mapping before the range */
2127 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002128 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002129 if (before->flags & AMDGPU_PTE_PRT)
2130 amdgpu_vm_prt_get(adev);
2131 } else {
2132 kfree(before);
2133 }
2134
2135 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002136 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002137 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002138 if (after->flags & AMDGPU_PTE_PRT)
2139 amdgpu_vm_prt_get(adev);
2140 } else {
2141 kfree(after);
2142 }
2143
2144 return 0;
2145}
2146
2147/**
Christian Königaebc5e62017-09-06 16:55:16 +02002148 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2149 *
2150 * @vm: the requested VM
2151 *
2152 * Find a mapping by it's address.
2153 */
2154struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2155 uint64_t addr)
2156{
2157 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2158}
2159
2160/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002161 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2162 *
2163 * @adev: amdgpu_device pointer
2164 * @bo_va: requested bo_va
2165 *
Christian König8843dbb2016-01-26 12:17:11 +01002166 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002167 *
2168 * Object have to be reserved!
2169 */
2170void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2171 struct amdgpu_bo_va *bo_va)
2172{
2173 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002174 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175
Christian Königec681542017-08-01 10:51:43 +02002176 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002178 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002179 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180 spin_unlock(&vm->status_lock);
2181
Christian König7fc11952015-07-30 11:53:42 +02002182 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002184 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002185 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002186 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002187 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188 }
Christian König7fc11952015-07-30 11:53:42 +02002189 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2190 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002191 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002192 amdgpu_vm_free_mapping(adev, vm, mapping,
2193 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002194 }
Christian König32b41ac2016-03-08 18:03:27 +01002195
Chris Wilsonf54d1862016-10-25 13:00:45 +01002196 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002197 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198}
2199
2200/**
2201 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2202 *
2203 * @adev: amdgpu_device pointer
2204 * @vm: requested vm
2205 * @bo: amdgpu buffer object
2206 *
Christian König8843dbb2016-01-26 12:17:11 +01002207 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208 */
2209void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002210 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002211{
Christian Königec681542017-08-01 10:51:43 +02002212 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213
Christian Königec681542017-08-01 10:51:43 +02002214 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002215 struct amdgpu_vm *vm = bo_base->vm;
2216
Christian König3d7d4d32017-08-23 16:13:33 +02002217 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002218 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2219 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002220 if (bo->tbo.type == ttm_bo_type_kernel)
2221 list_move(&bo_base->vm_status, &vm->evicted);
2222 else
2223 list_move_tail(&bo_base->vm_status,
2224 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002225 spin_unlock(&bo_base->vm->status_lock);
2226 continue;
2227 }
2228
Christian Königea097292017-08-09 14:15:46 +02002229 if (bo->tbo.type == ttm_bo_type_kernel) {
2230 spin_lock(&bo_base->vm->status_lock);
2231 if (list_empty(&bo_base->vm_status))
2232 list_add(&bo_base->vm_status, &vm->relocated);
2233 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002234 continue;
Christian Königea097292017-08-09 14:15:46 +02002235 }
Christian König3f3333f2017-08-03 14:02:13 +02002236
Christian Königec681542017-08-01 10:51:43 +02002237 spin_lock(&bo_base->vm->status_lock);
2238 if (list_empty(&bo_base->vm_status))
Christian König481c2e92017-09-01 14:46:19 +02002239 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002240 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002241 }
2242}
2243
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002244static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2245{
2246 /* Total bits covered by PD + PTs */
2247 unsigned bits = ilog2(vm_size) + 18;
2248
2249 /* Make sure the PD is 4K in size up to 8GB address space.
2250 Above that split equal between PD and PTs */
2251 if (vm_size <= 8)
2252 return (bits - 9);
2253 else
2254 return ((bits + 3) / 2);
2255}
2256
2257/**
Roger Hed07f14b2017-08-15 16:05:59 +08002258 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002259 *
2260 * @adev: amdgpu_device pointer
2261 * @vm_size: the default vm size if it's set auto
2262 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002263void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +01002264 uint32_t fragment_size_default, unsigned max_level,
2265 unsigned max_bits)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002266{
Christian König36539dc2017-11-23 11:16:05 +01002267 uint64_t tmp;
2268
2269 /* adjust vm size first */
Christian Königf3368122017-11-23 12:57:18 +01002270 if (amdgpu_vm_size != -1) {
2271 unsigned max_size = 1 << (max_bits - 30);
2272
Christian Königfdd5faa2017-11-04 16:51:44 +01002273 vm_size = amdgpu_vm_size;
Christian Königf3368122017-11-23 12:57:18 +01002274 if (vm_size > max_size) {
2275 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2276 amdgpu_vm_size, max_size);
2277 vm_size = max_size;
2278 }
2279 }
Christian Königfdd5faa2017-11-04 16:51:44 +01002280
2281 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Christian König36539dc2017-11-23 11:16:05 +01002282
2283 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
Christian König97489122017-11-27 16:22:05 +01002284 if (amdgpu_vm_block_size != -1)
2285 tmp >>= amdgpu_vm_block_size - 9;
Christian König36539dc2017-11-23 11:16:05 +01002286 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2287 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
Chunming Zhou196f7482017-12-13 14:22:54 +08002288 switch (adev->vm_manager.num_level) {
2289 case 3:
2290 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2291 break;
2292 case 2:
2293 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2294 break;
2295 case 1:
2296 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2297 break;
2298 default:
2299 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2300 }
Christian Königb38f41e2017-11-22 17:00:35 +01002301 /* block size depends on vm size and hw setup*/
Christian König97489122017-11-27 16:22:05 +01002302 if (amdgpu_vm_block_size != -1)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002303 adev->vm_manager.block_size =
Christian König97489122017-11-27 16:22:05 +01002304 min((unsigned)amdgpu_vm_block_size, max_bits
2305 - AMDGPU_GPU_PAGE_SHIFT
2306 - 9 * adev->vm_manager.num_level);
2307 else if (adev->vm_manager.num_level > 1)
2308 adev->vm_manager.block_size = 9;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002309 else
Christian König97489122017-11-27 16:22:05 +01002310 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002311
Christian Königb38f41e2017-11-22 17:00:35 +01002312 if (amdgpu_vm_fragment_size == -1)
2313 adev->vm_manager.fragment_size = fragment_size_default;
2314 else
2315 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
Roger Hed07f14b2017-08-15 16:05:59 +08002316
Christian König36539dc2017-11-23 11:16:05 +01002317 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2318 vm_size, adev->vm_manager.num_level + 1,
2319 adev->vm_manager.block_size,
Christian Königfdd5faa2017-11-04 16:51:44 +01002320 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002321}
2322
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002323/**
2324 * amdgpu_vm_init - initialize a vm instance
2325 *
2326 * @adev: amdgpu_device pointer
2327 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002328 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002329 *
Christian König8843dbb2016-01-26 12:17:11 +01002330 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002332int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002333 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002334{
2335 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002336 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002337 unsigned ring_instance;
2338 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002339 struct drm_sched_rq *rq;
Christian Königd3aab672018-01-24 14:57:02 +01002340 unsigned long size;
Christian König13307f72018-01-24 17:19:04 +01002341 uint64_t flags;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002342 int r, i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002343
Davidlohr Buesof808c132017-09-08 16:15:08 -07002344 vm->va = RB_ROOT_CACHED;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002345 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2346 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002347 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002348 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002349 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002350 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002351 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002352
Christian König2bd9ccf2016-02-01 12:53:58 +01002353 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002354
2355 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2356 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2357 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002358 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2359 r = drm_sched_entity_init(&ring->sched, &vm->entity,
Monk Liub3eebe32017-10-23 12:23:29 +08002360 rq, amdgpu_sched_jobs, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002361 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002362 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002363
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002364 vm->pte_support_ats = false;
2365
2366 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002367 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2368 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002369
Christian König13307f72018-01-24 17:19:04 +01002370 if (adev->asic_type == CHIP_RAVEN)
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002371 vm->pte_support_ats = true;
Christian König13307f72018-01-24 17:19:04 +01002372 } else {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002373 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2374 AMDGPU_VM_USE_CPU_FOR_GFX);
Christian König13307f72018-01-24 17:19:04 +01002375 }
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002376 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2377 vm->use_cpu_for_update ? "CPU" : "SDMA");
2378 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2379 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002380 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002381
Christian König13307f72018-01-24 17:19:04 +01002382 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002383 if (vm->use_cpu_for_update)
2384 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2385 else
2386 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2387 AMDGPU_GEM_CREATE_SHADOW);
2388
Christian Königd3aab672018-01-24 14:57:02 +01002389 size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
2390 r = amdgpu_bo_create(adev, size, align, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König8febe612018-01-24 19:55:32 +01002391 flags, NULL, NULL, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002393 goto error_free_sched_entity;
2394
Christian Königd3aab672018-01-24 14:57:02 +01002395 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2396 if (r)
2397 goto error_free_root;
2398
Christian König13307f72018-01-24 17:19:04 +01002399 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
Christian König45843122018-01-25 18:36:15 +01002400 adev->vm_manager.root_level,
2401 vm->pte_support_ats);
Christian König13307f72018-01-24 17:19:04 +01002402 if (r)
2403 goto error_unreserve;
2404
Christian König3f3333f2017-08-03 14:02:13 +02002405 vm->root.base.vm = vm;
2406 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
Christian Königd3aab672018-01-24 14:57:02 +01002407 list_add_tail(&vm->root.base.vm_status, &vm->evicted);
2408 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König0a096fb2017-07-12 10:01:48 +02002409
Felix Kuehling02208442017-08-25 20:40:26 -04002410 if (pasid) {
2411 unsigned long flags;
2412
2413 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2414 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2415 GFP_ATOMIC);
2416 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2417 if (r < 0)
2418 goto error_free_root;
2419
2420 vm->pasid = pasid;
2421 }
2422
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002423 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002424 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002425
2426 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002427
Christian König13307f72018-01-24 17:19:04 +01002428error_unreserve:
2429 amdgpu_bo_unreserve(vm->root.base.bo);
2430
Christian König67003a12016-10-12 14:46:26 +02002431error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002432 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2433 amdgpu_bo_unref(&vm->root.base.bo);
2434 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002435
2436error_free_sched_entity:
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002437 drm_sched_entity_fini(&ring->sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002438
2439 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002440}
2441
2442/**
Christian Königf566ceb2016-10-27 20:04:38 +02002443 * amdgpu_vm_free_levels - free PD/PT levels
2444 *
Christian König8f19cd72017-11-30 15:28:03 +01002445 * @adev: amdgpu device structure
2446 * @parent: PD/PT starting level to free
2447 * @level: level of parent structure
Christian Königf566ceb2016-10-27 20:04:38 +02002448 *
2449 * Free the page directory or page table level and all sub levels.
2450 */
Christian König8f19cd72017-11-30 15:28:03 +01002451static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2452 struct amdgpu_vm_pt *parent,
2453 unsigned level)
Christian Königf566ceb2016-10-27 20:04:38 +02002454{
Christian König8f19cd72017-11-30 15:28:03 +01002455 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +02002456
Christian König8f19cd72017-11-30 15:28:03 +01002457 if (parent->base.bo) {
2458 list_del(&parent->base.bo_list);
2459 list_del(&parent->base.vm_status);
2460 amdgpu_bo_unref(&parent->base.bo->shadow);
2461 amdgpu_bo_unref(&parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002462 }
2463
Christian König8f19cd72017-11-30 15:28:03 +01002464 if (parent->entries)
2465 for (i = 0; i < num_entries; i++)
2466 amdgpu_vm_free_levels(adev, &parent->entries[i],
2467 level + 1);
Christian Königf566ceb2016-10-27 20:04:38 +02002468
Christian König8f19cd72017-11-30 15:28:03 +01002469 kvfree(parent->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002470}
2471
2472/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002473 * amdgpu_vm_fini - tear down a vm instance
2474 *
2475 * @adev: amdgpu_device pointer
2476 * @vm: requested vm
2477 *
Christian König8843dbb2016-01-26 12:17:11 +01002478 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479 * Unbind the VM and remove all bos from the vm bo list
2480 */
2481void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2482{
2483 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König132f34e2018-01-12 15:26:08 +01002484 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002485 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002486 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002487 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002489 /* Clear pending page faults from IH when the VM is destroyed */
2490 while (kfifo_get(&vm->faults, &fault))
2491 amdgpu_ih_clear_fault(adev, fault);
2492
Felix Kuehling02208442017-08-25 20:40:26 -04002493 if (vm->pasid) {
2494 unsigned long flags;
2495
2496 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2497 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2498 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2499 }
2500
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002501 drm_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002502
Davidlohr Buesof808c132017-09-08 16:15:08 -07002503 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002504 dev_err(adev->dev, "still active bo inside vm\n");
2505 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002506 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2507 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002508 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002509 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002510 kfree(mapping);
2511 }
2512 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002513 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002514 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002515 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002516 }
Christian König284710f2017-01-30 11:09:31 +01002517
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002518 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002519 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520 }
2521
Christian König2642cf12017-10-13 17:24:31 +02002522 root = amdgpu_bo_ref(vm->root.base.bo);
2523 r = amdgpu_bo_reserve(root, true);
2524 if (r) {
2525 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2526 } else {
Chunming Zhou196f7482017-12-13 14:22:54 +08002527 amdgpu_vm_free_levels(adev, &vm->root,
2528 adev->vm_manager.root_level);
Christian König2642cf12017-10-13 17:24:31 +02002529 amdgpu_bo_unreserve(root);
2530 }
2531 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002532 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002533 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
Christian König620f7742017-12-18 16:53:03 +01002534 amdgpu_vmid_free_reserved(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002535}
Christian Königea89f8c2015-11-15 20:52:06 +01002536
2537/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002538 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2539 *
2540 * @adev: amdgpu_device pointer
2541 * @pasid: PASID do identify the VM
2542 *
2543 * This function is expected to be called in interrupt context. Returns
2544 * true if there was fault credit, false otherwise
2545 */
2546bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2547 unsigned int pasid)
2548{
2549 struct amdgpu_vm *vm;
2550
2551 spin_lock(&adev->vm_manager.pasid_lock);
2552 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
Christian Königd9589392018-01-09 19:18:59 +01002553 if (!vm) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002554 /* VM not found, can't track fault credit */
Christian Königd9589392018-01-09 19:18:59 +01002555 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002556 return true;
Christian Königd9589392018-01-09 19:18:59 +01002557 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002558
2559 /* No lock needed. only accessed by IRQ handler */
Christian Königd9589392018-01-09 19:18:59 +01002560 if (!vm->fault_credit) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002561 /* Too many faults in this VM */
Christian Königd9589392018-01-09 19:18:59 +01002562 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002563 return false;
Christian Königd9589392018-01-09 19:18:59 +01002564 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002565
2566 vm->fault_credit--;
Christian Königd9589392018-01-09 19:18:59 +01002567 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002568 return true;
2569}
2570
2571/**
Christian Königa9a78b32016-01-21 10:19:11 +01002572 * amdgpu_vm_manager_init - init the VM manager
2573 *
2574 * @adev: amdgpu_device pointer
2575 *
2576 * Initialize the VM manager structures
2577 */
2578void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2579{
Christian König620f7742017-12-18 16:53:03 +01002580 unsigned i;
Christian Königa9a78b32016-01-21 10:19:11 +01002581
Christian König620f7742017-12-18 16:53:03 +01002582 amdgpu_vmid_mgr_init(adev);
Christian König2d55e452016-02-08 17:37:38 +01002583
Chris Wilsonf54d1862016-10-25 13:00:45 +01002584 adev->vm_manager.fence_context =
2585 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002586 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2587 adev->vm_manager.seqno[i] = 0;
2588
Christian König2d55e452016-02-08 17:37:38 +01002589 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian König284710f2017-01-30 11:09:31 +01002590 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002591 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002592
2593 /* If not overridden by the user, by default, only in large BAR systems
2594 * Compute VM tables will be updated by CPU
2595 */
2596#ifdef CONFIG_X86_64
2597 if (amdgpu_vm_update_mode == -1) {
2598 if (amdgpu_vm_is_large_bar(adev))
2599 adev->vm_manager.vm_update_mode =
2600 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2601 else
2602 adev->vm_manager.vm_update_mode = 0;
2603 } else
2604 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2605#else
2606 adev->vm_manager.vm_update_mode = 0;
2607#endif
2608
Felix Kuehling02208442017-08-25 20:40:26 -04002609 idr_init(&adev->vm_manager.pasid_idr);
2610 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002611}
2612
2613/**
Christian Königea89f8c2015-11-15 20:52:06 +01002614 * amdgpu_vm_manager_fini - cleanup VM manager
2615 *
2616 * @adev: amdgpu_device pointer
2617 *
2618 * Cleanup the VM manager and free resources.
2619 */
2620void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2621{
Felix Kuehling02208442017-08-25 20:40:26 -04002622 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2623 idr_destroy(&adev->vm_manager.pasid_idr);
2624
Christian König620f7742017-12-18 16:53:03 +01002625 amdgpu_vmid_mgr_fini(adev);
Christian Königea89f8c2015-11-15 20:52:06 +01002626}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002627
2628int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2629{
2630 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002631 struct amdgpu_device *adev = dev->dev_private;
2632 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2633 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002634
2635 switch (args->in.op) {
2636 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002637 /* current, we only have requirement to reserve vmid from gfxhub */
Christian König620f7742017-12-18 16:53:03 +01002638 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002639 if (r)
2640 return r;
2641 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002642 case AMDGPU_VM_OP_UNRESERVE_VMID:
Christian König620f7742017-12-18 16:53:03 +01002643 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002644 break;
2645 default:
2646 return -EINVAL;
2647 }
2648
2649 return 0;
2650}