blob: d3c48557555cae6e63869da684685e10f24e6e0f [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence-array.h>
Christian Königa9f87f62017-03-30 14:03:59 +020029#include <linux/interval_tree_generic.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030#include <drm/drmP.h>
31#include <drm/amdgpu_drm.h>
32#include "amdgpu.h"
33#include "amdgpu_trace.h"
34
35/*
36 * GPUVM
37 * GPUVM is similar to the legacy gart on older asics, however
38 * rather than there being a single global gart table
39 * for the entire GPU, there are multiple VM page tables active
40 * at any given time. The VM page tables can contain a mix
41 * vram pages and system memory pages and system memory pages
42 * can be mapped as snooped (cached system pages) or unsnooped
43 * (uncached system pages).
44 * Each VM has an ID associated with it and there is a page table
45 * associated with each VMID. When execting a command buffer,
46 * the kernel tells the the ring what VMID to use for that command
47 * buffer. VMIDs are allocated dynamically as commands are submitted.
48 * The userspace drivers maintain their own address space and the kernel
49 * sets up their pages tables accordingly when they submit their
50 * command buffers and a VMID is assigned.
51 * Cayman/Trinity support up to 8 active VMs at any given time;
52 * SI supports 16.
53 */
54
Christian Königa9f87f62017-03-30 14:03:59 +020055#define START(node) ((node)->start)
56#define LAST(node) ((node)->last)
57
58INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
59 START, LAST, static, amdgpu_vm_it)
60
61#undef START
62#undef LAST
63
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040064/* Local structure. Encapsulate some VM table update parameters to reduce
65 * the number of function parameters
66 */
Christian König29efc4f2016-08-04 14:52:50 +020067struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020068 /* amdgpu device we do this update for */
69 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +020070 /* optional amdgpu_vm we do this update for */
71 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040072 /* address where to copy page table entries from */
73 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040074 /* indirect buffer to fill with commands */
75 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020076 /* Function which actually does the update */
77 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
78 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080079 uint64_t flags);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -040080 /* The next two are used during VM update by CPU
81 * DMA addresses to use for mapping
82 * Kernel pointer of PD/PT BO that needs to be updated
83 */
84 dma_addr_t *pages_addr;
85 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040086};
87
Christian König284710f2017-01-30 11:09:31 +010088/* Helper to disable partial resident texture feature from a fence callback */
89struct amdgpu_prt_cb {
90 struct amdgpu_device *adev;
91 struct dma_fence_cb cb;
92};
93
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094/**
Christian König72a7ec52016-10-19 11:03:57 +020095 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 *
97 * @adev: amdgpu_device pointer
98 *
Christian König72a7ec52016-10-19 11:03:57 +020099 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100 */
Christian König72a7ec52016-10-19 11:03:57 +0200101static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
102 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103{
Christian König72a7ec52016-10-19 11:03:57 +0200104 if (level == 0)
105 /* For the root directory */
106 return adev->vm_manager.max_pfn >>
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800107 (adev->vm_manager.block_size *
108 adev->vm_manager.num_level);
Christian König72a7ec52016-10-19 11:03:57 +0200109 else if (level == adev->vm_manager.num_level)
110 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800111 return AMDGPU_VM_PTE_COUNT(adev);
Christian König72a7ec52016-10-19 11:03:57 +0200112 else
113 /* Everything in between */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800114 return 1 << adev->vm_manager.block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115}
116
117/**
Christian König72a7ec52016-10-19 11:03:57 +0200118 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 *
120 * @adev: amdgpu_device pointer
121 *
Christian König72a7ec52016-10-19 11:03:57 +0200122 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 */
Christian König72a7ec52016-10-19 11:03:57 +0200124static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125{
Christian König72a7ec52016-10-19 11:03:57 +0200126 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127}
128
129/**
Christian König56467eb2015-12-11 15:16:32 +0100130 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 *
132 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100133 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100134 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 *
136 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100137 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 */
Christian König56467eb2015-12-11 15:16:32 +0100139void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
140 struct list_head *validated,
141 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142{
Christian König3f3333f2017-08-03 14:02:13 +0200143 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100144 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200145 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100146 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100147 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100148 list_add(&entry->tv.head, validated);
149}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150
Christian König56467eb2015-12-11 15:16:32 +0100151/**
Christian Königf7da30d2016-09-28 12:03:04 +0200152 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100153 *
Christian König5a712a82016-06-21 16:28:15 +0200154 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100155 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200156 * @validate: callback to do the validation
157 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158 *
Christian Königf7da30d2016-09-28 12:03:04 +0200159 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 */
Christian Königf7da30d2016-09-28 12:03:04 +0200161int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
162 int (*validate)(void *p, struct amdgpu_bo *bo),
163 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164{
Christian König3f3333f2017-08-03 14:02:13 +0200165 struct ttm_bo_global *glob = adev->mman.bdev.glob;
166 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167
Christian König3f3333f2017-08-03 14:02:13 +0200168 spin_lock(&vm->status_lock);
169 while (!list_empty(&vm->evicted)) {
170 struct amdgpu_vm_bo_base *bo_base;
171 struct amdgpu_bo *bo;
Christian König5a712a82016-06-21 16:28:15 +0200172
Christian König3f3333f2017-08-03 14:02:13 +0200173 bo_base = list_first_entry(&vm->evicted,
174 struct amdgpu_vm_bo_base,
175 vm_status);
176 spin_unlock(&vm->status_lock);
Christian Königeceb8a12016-01-11 15:35:21 +0100177
Christian König3f3333f2017-08-03 14:02:13 +0200178 bo = bo_base->bo;
179 BUG_ON(!bo);
180 if (bo->parent) {
181 r = validate(param, bo);
182 if (r)
183 return r;
Christian König34d7be52017-08-24 12:32:55 +0200184
Christian König3f3333f2017-08-03 14:02:13 +0200185 spin_lock(&glob->lru_lock);
186 ttm_bo_move_to_lru_tail(&bo->tbo);
187 if (bo->shadow)
188 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
189 spin_unlock(&glob->lru_lock);
190 }
191
192 if (vm->use_cpu_for_update) {
193 r = amdgpu_bo_kmap(bo, NULL);
194 if (r)
195 return r;
196 }
197
198 spin_lock(&vm->status_lock);
Christian Königea097292017-08-09 14:15:46 +0200199 list_move(&bo_base->vm_status, &vm->relocated);
Christian König3f3333f2017-08-03 14:02:13 +0200200 }
201 spin_unlock(&vm->status_lock);
Christian König34d7be52017-08-24 12:32:55 +0200202
203 return 0;
204}
205
206/**
207 * amdgpu_vm_ready - check VM is ready for updates
208 *
Christian König34d7be52017-08-24 12:32:55 +0200209 * @vm: VM to check
210 *
211 * Check if all VM PDs/PTs are ready for updates
212 */
Christian König3f3333f2017-08-03 14:02:13 +0200213bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200214{
Christian König3f3333f2017-08-03 14:02:13 +0200215 bool ready;
Christian König34d7be52017-08-24 12:32:55 +0200216
Christian König3f3333f2017-08-03 14:02:13 +0200217 spin_lock(&vm->status_lock);
218 ready = list_empty(&vm->evicted);
219 spin_unlock(&vm->status_lock);
220
221 return ready;
Christian König34d7be52017-08-24 12:32:55 +0200222}
223
224/**
Christian Königf566ceb2016-10-27 20:04:38 +0200225 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
226 *
227 * @adev: amdgpu_device pointer
228 * @vm: requested vm
229 * @saddr: start of the address range
230 * @eaddr: end of the address range
231 *
232 * Make sure the page directories and page tables are allocated
233 */
234static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
235 struct amdgpu_vm *vm,
236 struct amdgpu_vm_pt *parent,
237 uint64_t saddr, uint64_t eaddr,
238 unsigned level)
239{
240 unsigned shift = (adev->vm_manager.num_level - level) *
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800241 adev->vm_manager.block_size;
Christian Königf566ceb2016-10-27 20:04:38 +0200242 unsigned pt_idx, from, to;
243 int r;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400244 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400245 uint64_t init_value = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200246
247 if (!parent->entries) {
248 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
249
Michal Hocko20981052017-05-17 14:23:12 +0200250 parent->entries = kvmalloc_array(num_entries,
251 sizeof(struct amdgpu_vm_pt),
252 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200253 if (!parent->entries)
254 return -ENOMEM;
255 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
256 }
257
Felix Kuehling1866bac2017-03-28 20:36:12 -0400258 from = saddr >> shift;
259 to = eaddr >> shift;
260 if (from >= amdgpu_vm_num_entries(adev, level) ||
261 to >= amdgpu_vm_num_entries(adev, level))
262 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200263
264 if (to > parent->last_entry_used)
265 parent->last_entry_used = to;
266
267 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400268 saddr = saddr & ((1 << shift) - 1);
269 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200270
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400271 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
272 AMDGPU_GEM_CREATE_VRAM_CLEARED;
273 if (vm->use_cpu_for_update)
274 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
275 else
276 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
277 AMDGPU_GEM_CREATE_SHADOW);
278
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400279 if (vm->pte_support_ats) {
280 init_value = AMDGPU_PTE_SYSTEM;
281 if (level != adev->vm_manager.num_level - 1)
282 init_value |= AMDGPU_PDE_PTE;
283 }
284
Christian Königf566ceb2016-10-27 20:04:38 +0200285 /* walk over the address space and allocate the page tables */
286 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200287 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200288 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
289 struct amdgpu_bo *pt;
290
Christian König3f3333f2017-08-03 14:02:13 +0200291 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200292 r = amdgpu_bo_create(adev,
293 amdgpu_vm_bo_size(adev, level),
294 AMDGPU_GPU_PAGE_SIZE, true,
295 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400296 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400297 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200298 if (r)
299 return r;
300
Christian König0a096fb2017-07-12 10:01:48 +0200301 if (vm->use_cpu_for_update) {
302 r = amdgpu_bo_kmap(pt, NULL);
303 if (r) {
304 amdgpu_bo_unref(&pt);
305 return r;
306 }
307 }
308
Christian Königf566ceb2016-10-27 20:04:38 +0200309 /* Keep a reference to the root directory to avoid
310 * freeing them up in the wrong order.
311 */
Christian König0f2fc432017-08-31 10:46:20 +0200312 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200313
Christian König3f3333f2017-08-03 14:02:13 +0200314 entry->base.vm = vm;
315 entry->base.bo = pt;
316 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200317 spin_lock(&vm->status_lock);
318 list_add(&entry->base.vm_status, &vm->relocated);
319 spin_unlock(&vm->status_lock);
Christian König0f2fc432017-08-31 10:46:20 +0200320 entry->addr = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200321 }
322
323 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400324 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
325 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
326 ((1 << shift) - 1);
327 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
328 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200329 if (r)
330 return r;
331 }
332 }
333
334 return 0;
335}
336
Christian König663e4572017-03-13 10:13:37 +0100337/**
338 * amdgpu_vm_alloc_pts - Allocate page tables.
339 *
340 * @adev: amdgpu_device pointer
341 * @vm: VM to allocate page tables for
342 * @saddr: Start address which needs to be allocated
343 * @size: Size from start address we need.
344 *
345 * Make sure the page tables are allocated.
346 */
347int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
348 struct amdgpu_vm *vm,
349 uint64_t saddr, uint64_t size)
350{
Felix Kuehling22770e52017-03-28 20:24:53 -0400351 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100352 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100353
354 /* validate the parameters */
355 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
356 return -EINVAL;
357
358 eaddr = saddr + size - 1;
359 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
360 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400361 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100362 last_pfn, adev->vm_manager.max_pfn);
363 return -EINVAL;
364 }
365
366 saddr /= AMDGPU_GPU_PAGE_SIZE;
367 eaddr /= AMDGPU_GPU_PAGE_SIZE;
368
Christian Königf566ceb2016-10-27 20:04:38 +0200369 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100370}
371
Christian König641e9402017-04-03 13:59:25 +0200372/**
373 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
374 *
375 * @adev: amdgpu_device pointer
376 * @id: VMID structure
377 *
378 * Check if GPU reset occured since last use of the VMID.
379 */
380static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
381 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800382{
383 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200384 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800385}
386
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800387static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
388{
389 return !!vm->reserved_vmid[vmhub];
390}
391
392/* idr_mgr->lock must be held */
393static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
394 struct amdgpu_ring *ring,
395 struct amdgpu_sync *sync,
396 struct dma_fence *fence,
397 struct amdgpu_job *job)
398{
399 struct amdgpu_device *adev = ring->adev;
400 unsigned vmhub = ring->funcs->vmhub;
401 uint64_t fence_context = adev->fence_context + ring->idx;
402 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
403 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
404 struct dma_fence *updates = sync->last_vm_update;
405 int r = 0;
406 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200407 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800408
409 flushed = id->flushed_updates;
410 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
411 (atomic64_read(&id->owner) != vm->client_id) ||
412 (job->vm_pd_addr != id->pd_gpu_addr) ||
413 (updates && (!flushed || updates->context != flushed->context ||
414 dma_fence_is_later(updates, flushed))) ||
415 (!id->last_flush || (id->last_flush->context != fence_context &&
416 !dma_fence_is_signaled(id->last_flush)))) {
417 needs_flush = true;
418 /* to prevent one context starved by another context */
419 id->pd_gpu_addr = 0;
420 tmp = amdgpu_sync_peek_fence(&id->active, ring);
421 if (tmp) {
422 r = amdgpu_sync_fence(adev, sync, tmp);
423 return r;
424 }
425 }
426
427 /* Good we can use this VMID. Remember this submission as
428 * user of the VMID.
429 */
430 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
431 if (r)
432 goto out;
433
434 if (updates && (!flushed || updates->context != flushed->context ||
435 dma_fence_is_later(updates, flushed))) {
436 dma_fence_put(id->flushed_updates);
437 id->flushed_updates = dma_fence_get(updates);
438 }
439 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800440 atomic64_set(&id->owner, vm->client_id);
441 job->vm_needs_flush = needs_flush;
442 if (needs_flush) {
443 dma_fence_put(id->last_flush);
444 id->last_flush = NULL;
445 }
446 job->vm_id = id - id_mgr->ids;
447 trace_amdgpu_vm_grab_id(vm, ring, job);
448out:
449 return r;
450}
451
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452/**
453 * amdgpu_vm_grab_id - allocate the next free VMID
454 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400455 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200456 * @ring: ring we want to submit job to
457 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100458 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459 *
Christian König7f8a5292015-07-20 16:09:40 +0200460 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400461 */
Christian König7f8a5292015-07-20 16:09:40 +0200462int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100463 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800464 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400465{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200467 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200468 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200469 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100470 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200471 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100472 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200473 unsigned i;
474 int r = 0;
475
Christian König76456702017-04-06 17:52:39 +0200476 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800477 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
478 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
479 mutex_unlock(&id_mgr->lock);
480 return r;
481 }
482 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
483 if (!fences) {
484 mutex_unlock(&id_mgr->lock);
485 return -ENOMEM;
486 }
Christian König36fd7c52016-05-23 15:30:08 +0200487 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200488 i = 0;
Christian König76456702017-04-06 17:52:39 +0200489 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200490 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
491 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200492 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200493 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200494 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100495
Christian König1fbb2e92016-06-01 10:47:36 +0200496 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200497 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200498 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
499 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100500 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200501 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200502
Christian König1fbb2e92016-06-01 10:47:36 +0200503 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100504 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200505
Chris Wilsonf54d1862016-10-25 13:00:45 +0100506 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200507 seqno, true);
508 if (!array) {
509 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100510 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200511 kfree(fences);
512 r = -ENOMEM;
513 goto error;
514 }
Christian König8d76001e2016-05-23 16:00:32 +0200515
Christian König8d76001e2016-05-23 16:00:32 +0200516
Christian König1fbb2e92016-06-01 10:47:36 +0200517 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100518 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200519 if (r)
520 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200521
Christian König76456702017-04-06 17:52:39 +0200522 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200523 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200524
Christian König1fbb2e92016-06-01 10:47:36 +0200525 }
526 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200527
Christian König6f1ceab2017-07-11 16:59:21 +0200528 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200529 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200530 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100531 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200532 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200533
Christian König1fbb2e92016-06-01 10:47:36 +0200534 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200535 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800536 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200537
538 if (atomic64_read(&id->owner) != vm->client_id)
539 continue;
540
Chunming Zhoufd53be32016-07-01 17:59:01 +0800541 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200542 continue;
543
Christian König87c910d2017-03-30 16:56:20 +0200544 if (!id->last_flush ||
545 (id->last_flush->context != fence_context &&
546 !dma_fence_is_signaled(id->last_flush)))
547 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200548
549 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200550 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
551 needs_flush = true;
552
553 /* Concurrent flushes are only possible starting with Vega10 */
554 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200555 continue;
556
Christian König3dab83b2016-06-01 13:31:17 +0200557 /* Good we can use this VMID. Remember this submission as
558 * user of the VMID.
559 */
Christian König1fbb2e92016-06-01 10:47:36 +0200560 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
561 if (r)
562 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200563
Christian König87c910d2017-03-30 16:56:20 +0200564 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
565 dma_fence_put(id->flushed_updates);
566 id->flushed_updates = dma_fence_get(updates);
567 }
Christian König8d76001e2016-05-23 16:00:32 +0200568
Christian König87c910d2017-03-30 16:56:20 +0200569 if (needs_flush)
570 goto needs_flush;
571 else
572 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200573
Christian König4f618e72017-04-06 15:18:21 +0200574 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800575
Christian König1fbb2e92016-06-01 10:47:36 +0200576 /* Still no ID to use? Then use the idle one found earlier */
577 id = idle;
578
579 /* Remember this submission as user of the VMID */
580 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100581 if (r)
582 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100583
Christian König87c910d2017-03-30 16:56:20 +0200584 id->pd_gpu_addr = job->vm_pd_addr;
585 dma_fence_put(id->flushed_updates);
586 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200587 atomic64_set(&id->owner, vm->client_id);
588
589needs_flush:
590 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100591 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100592 id->last_flush = NULL;
593
Christian König87c910d2017-03-30 16:56:20 +0200594no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200595 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400596
Christian König76456702017-04-06 17:52:39 +0200597 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200598 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100599
600error:
Christian König76456702017-04-06 17:52:39 +0200601 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100602 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603}
604
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800605static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
606 struct amdgpu_vm *vm,
607 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400608{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800609 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400610
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800611 mutex_lock(&id_mgr->lock);
612 if (vm->reserved_vmid[vmhub]) {
613 list_add(&vm->reserved_vmid[vmhub]->list,
614 &id_mgr->ids_lru);
615 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800616 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400617 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800618 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400619}
620
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800621static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
622 struct amdgpu_vm *vm,
623 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500624{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800625 struct amdgpu_vm_id_manager *id_mgr;
626 struct amdgpu_vm_id *idle;
627 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500628
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800629 id_mgr = &adev->vm_manager.id_mgr[vmhub];
630 mutex_lock(&id_mgr->lock);
631 if (vm->reserved_vmid[vmhub])
632 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800633 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
634 AMDGPU_VM_MAX_RESERVED_VMID) {
635 DRM_ERROR("Over limitation of reserved vmid\n");
636 atomic_dec(&id_mgr->reserved_vmid_num);
637 r = -EINVAL;
638 goto unlock;
639 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800640 /* Select the first entry VMID */
641 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
642 list_del_init(&idle->list);
643 vm->reserved_vmid[vmhub] = idle;
644 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500645
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800646 return 0;
647unlock:
648 mutex_unlock(&id_mgr->lock);
649 return r;
650}
651
Alex Xiee59c0202017-06-01 09:42:59 -0400652/**
653 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
654 *
655 * @adev: amdgpu_device pointer
656 */
657void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
658{
659 const struct amdgpu_ip_block *ip_block;
660 bool has_compute_vm_bug;
661 struct amdgpu_ring *ring;
662 int i;
663
664 has_compute_vm_bug = false;
665
666 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
667 if (ip_block) {
668 /* Compute has a VM bug for GFX version < 7.
669 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
670 if (ip_block->version->major <= 7)
671 has_compute_vm_bug = true;
672 else if (ip_block->version->major == 8)
673 if (adev->gfx.mec_fw_version < 673)
674 has_compute_vm_bug = true;
675 }
676
677 for (i = 0; i < adev->num_rings; i++) {
678 ring = adev->rings[i];
679 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
680 /* only compute rings */
681 ring->has_compute_vm_bug = has_compute_vm_bug;
682 else
683 ring->has_compute_vm_bug = false;
684 }
685}
686
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400687bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
688 struct amdgpu_job *job)
689{
690 struct amdgpu_device *adev = ring->adev;
691 unsigned vmhub = ring->funcs->vmhub;
692 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
693 struct amdgpu_vm_id *id;
694 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400695 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400696
697 if (job->vm_id == 0)
698 return false;
699 id = &id_mgr->ids[job->vm_id];
700 gds_switch_needed = ring->funcs->emit_gds_switch && (
701 id->gds_base != job->gds_base ||
702 id->gds_size != job->gds_size ||
703 id->gws_base != job->gws_base ||
704 id->gws_size != job->gws_size ||
705 id->oa_base != job->oa_base ||
706 id->oa_size != job->oa_size);
707
708 if (amdgpu_vm_had_gpu_reset(adev, id))
709 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400710
711 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400712}
713
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400714static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
715{
716 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500717}
718
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719/**
720 * amdgpu_vm_flush - hardware flush the vm
721 *
722 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100723 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100724 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400725 *
Christian König4ff37a82016-02-26 16:18:26 +0100726 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800728int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729{
Christian König971fe9a92016-03-01 15:09:25 +0100730 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200731 unsigned vmhub = ring->funcs->vmhub;
732 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
733 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100734 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800735 id->gds_base != job->gds_base ||
736 id->gds_size != job->gds_size ||
737 id->gws_base != job->gws_base ||
738 id->gws_size != job->gws_size ||
739 id->oa_base != job->oa_base ||
740 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800741 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200742 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100743 int r;
Christian Königd564a062016-03-01 15:51:53 +0100744
Christian Königf7d015b2017-04-03 14:28:26 +0200745 if (amdgpu_vm_had_gpu_reset(adev, id)) {
746 gds_switch_needed = true;
747 vm_flush_needed = true;
748 }
Christian König971fe9a92016-03-01 15:09:25 +0100749
Monk Liu8fdf0742017-06-06 17:25:13 +0800750 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200751 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100752
Christian Königc0e51932017-04-03 14:16:07 +0200753 if (ring->funcs->init_cond_exec)
754 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100755
Monk Liu8fdf0742017-06-06 17:25:13 +0800756 if (need_pipe_sync)
757 amdgpu_ring_emit_pipeline_sync(ring);
758
Christian Königf7d015b2017-04-03 14:28:26 +0200759 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200760 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800761
Christian König9a94f5a2017-05-12 14:46:23 +0200762 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
763 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800764
Christian Königc0e51932017-04-03 14:16:07 +0200765 r = amdgpu_fence_emit(ring, &fence);
766 if (r)
767 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800768
Christian König76456702017-04-06 17:52:39 +0200769 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200770 dma_fence_put(id->last_flush);
771 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800772 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200773 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200774 }
Monk Liue9d672b2017-03-15 12:18:57 +0800775
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800776 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200777 id->gds_base = job->gds_base;
778 id->gds_size = job->gds_size;
779 id->gws_base = job->gws_base;
780 id->gws_size = job->gws_size;
781 id->oa_base = job->oa_base;
782 id->oa_size = job->oa_size;
783 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
784 job->gds_size, job->gws_base,
785 job->gws_size, job->oa_base,
786 job->oa_size);
787 }
788
789 if (ring->funcs->patch_cond_exec)
790 amdgpu_ring_patch_cond_exec(ring, patch_offset);
791
792 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
793 if (ring->funcs->emit_switch_buffer) {
794 amdgpu_ring_emit_switch_buffer(ring);
795 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796 }
Christian König41d9eb22016-03-01 16:46:18 +0100797 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100798}
799
800/**
801 * amdgpu_vm_reset_id - reset VMID to zero
802 *
803 * @adev: amdgpu device structure
804 * @vm_id: vmid number to use
805 *
806 * Reset saved GDW, GWS and OA to force switch on next flush.
807 */
Christian König76456702017-04-06 17:52:39 +0200808void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
809 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100810{
Christian König76456702017-04-06 17:52:39 +0200811 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
812 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100813
Christian Königb3c85a02017-05-10 20:06:58 +0200814 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100815 id->gds_base = 0;
816 id->gds_size = 0;
817 id->gws_base = 0;
818 id->gws_size = 0;
819 id->oa_base = 0;
820 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400821}
822
823/**
Christian Königb3c85a02017-05-10 20:06:58 +0200824 * amdgpu_vm_reset_all_id - reset VMID to zero
825 *
826 * @adev: amdgpu device structure
827 *
828 * Reset VMID to force flush on next use
829 */
830void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
831{
832 unsigned i, j;
833
834 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
835 struct amdgpu_vm_id_manager *id_mgr =
836 &adev->vm_manager.id_mgr[i];
837
838 for (j = 1; j < id_mgr->num_ids; ++j)
839 amdgpu_vm_reset_id(adev, i, j);
840 }
841}
842
843/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
845 *
846 * @vm: requested vm
847 * @bo: requested buffer object
848 *
Christian König8843dbb2016-01-26 12:17:11 +0100849 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400850 * Search inside the @bos vm list for the requested vm
851 * Returns the found bo_va or NULL if none is found
852 *
853 * Object has to be reserved!
854 */
855struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
856 struct amdgpu_bo *bo)
857{
858 struct amdgpu_bo_va *bo_va;
859
Christian Königec681542017-08-01 10:51:43 +0200860 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
861 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400862 return bo_va;
863 }
864 }
865 return NULL;
866}
867
868/**
Christian Königafef8b82016-08-12 13:29:18 +0200869 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400870 *
Christian König29efc4f2016-08-04 14:52:50 +0200871 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400872 * @pe: addr of the page entry
873 * @addr: dst addr to write into pe
874 * @count: number of page entries to update
875 * @incr: increase next addr by incr bytes
876 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877 *
878 * Traces the parameters and calls the right asic functions
879 * to setup the page table using the DMA.
880 */
Christian Königafef8b82016-08-12 13:29:18 +0200881static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
882 uint64_t pe, uint64_t addr,
883 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800884 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885{
Christian Königec2f05f2016-09-25 16:11:52 +0200886 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400887
Christian Königafef8b82016-08-12 13:29:18 +0200888 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200889 amdgpu_vm_write_pte(params->adev, params->ib, pe,
890 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400891
892 } else {
Christian König27c5f362016-08-04 15:02:49 +0200893 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894 count, incr, flags);
895 }
896}
897
898/**
Christian Königafef8b82016-08-12 13:29:18 +0200899 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
900 *
901 * @params: see amdgpu_pte_update_params definition
902 * @pe: addr of the page entry
903 * @addr: dst addr to write into pe
904 * @count: number of page entries to update
905 * @incr: increase next addr by incr bytes
906 * @flags: hw access flags
907 *
908 * Traces the parameters and calls the DMA function to copy the PTEs.
909 */
910static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
911 uint64_t pe, uint64_t addr,
912 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800913 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200914{
Christian Königec2f05f2016-09-25 16:11:52 +0200915 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200916
Christian Königec2f05f2016-09-25 16:11:52 +0200917
918 trace_amdgpu_vm_copy_ptes(pe, src, count);
919
920 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200921}
922
923/**
Christian Königb07c9d22015-11-30 13:26:07 +0100924 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925 *
Christian Königb07c9d22015-11-30 13:26:07 +0100926 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927 * @addr: the unmapped addr
928 *
929 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100930 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200932static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933{
934 uint64_t result;
935
Christian Königde9ea7b2016-08-12 11:33:30 +0200936 /* page table offset */
937 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938
Christian Königde9ea7b2016-08-12 11:33:30 +0200939 /* in case cpu page size != gpu page size*/
940 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100941
942 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943
944 return result;
945}
946
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400947/**
948 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
949 *
950 * @params: see amdgpu_pte_update_params definition
951 * @pe: kmap addr of the page entry
952 * @addr: dst addr to write into pe
953 * @count: number of page entries to update
954 * @incr: increase next addr by incr bytes
955 * @flags: hw access flags
956 *
957 * Write count number of PT/PD entries directly.
958 */
959static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
960 uint64_t pe, uint64_t addr,
961 unsigned count, uint32_t incr,
962 uint64_t flags)
963{
964 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400965 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400966
Christian König03918b32017-07-11 17:15:37 +0200967 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
968
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400969 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400970 value = params->pages_addr ?
971 amdgpu_vm_map_gart(params->pages_addr, addr) :
972 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -0400973 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400974 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400975 addr += incr;
976 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400977}
978
Christian Königa33cab72017-07-11 17:13:00 +0200979static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
980 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400981{
982 struct amdgpu_sync sync;
983 int r;
984
985 amdgpu_sync_create(&sync);
Christian König3f3333f2017-08-03 14:02:13 +0200986 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400987 r = amdgpu_sync_wait(&sync, true);
988 amdgpu_sync_free(&sync);
989
990 return r;
991}
992
Christian Königf8991ba2016-09-16 15:36:49 +0200993/*
Christian König194d2162016-10-12 15:13:52 +0200994 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200995 *
996 * @adev: amdgpu_device pointer
997 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200998 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +0200999 *
Christian König194d2162016-10-12 15:13:52 +02001000 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +02001001 * Returns 0 for success, error for failure.
1002 */
Christian König194d2162016-10-12 15:13:52 +02001003static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1004 struct amdgpu_vm *vm,
Christian Königea097292017-08-09 14:15:46 +02001005 struct amdgpu_vm_pt *parent)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006{
Christian Königf8991ba2016-09-16 15:36:49 +02001007 struct amdgpu_bo *shadow;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001008 struct amdgpu_ring *ring = NULL;
1009 uint64_t pd_addr, shadow_addr = 0;
Christian Königf8991ba2016-09-16 15:36:49 +02001010 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001011 unsigned count = 0, pt_idx, ndw = 0;
Christian Königd71518b2016-02-01 12:20:25 +01001012 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001013 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +10001014 struct dma_fence *fence = NULL;
Christian Königea097292017-08-09 14:15:46 +02001015 uint32_t incr;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001016
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001017 int r;
1018
Christian König194d2162016-10-12 15:13:52 +02001019 if (!parent->entries)
1020 return 0;
Christian Königd71518b2016-02-01 12:20:25 +01001021
Christian König27c5f362016-08-04 15:02:49 +02001022 memset(&params, 0, sizeof(params));
1023 params.adev = adev;
Christian König3f3333f2017-08-03 14:02:13 +02001024 shadow = parent->base.bo->shadow;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001025
Alex Deucher69277982017-07-13 15:37:11 -04001026 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02001027 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Christian Königa33cab72017-07-11 17:13:00 +02001028 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001029 if (unlikely(r))
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001030 return r;
Christian König0a096fb2017-07-12 10:01:48 +02001031
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001032 params.func = amdgpu_vm_cpu_set_ptes;
1033 } else {
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001034 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1035 sched);
1036
1037 /* padding, etc. */
1038 ndw = 64;
1039
1040 /* assume the worst case */
1041 ndw += parent->last_entry_used * 6;
1042
Christian König3f3333f2017-08-03 14:02:13 +02001043 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001044
1045 if (shadow) {
1046 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1047 ndw *= 2;
1048 } else {
1049 shadow_addr = 0;
1050 }
1051
1052 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1053 if (r)
1054 return r;
1055
1056 params.ib = &job->ibs[0];
1057 params.func = amdgpu_vm_do_set_ptes;
1058 }
1059
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001060
Christian König194d2162016-10-12 15:13:52 +02001061 /* walk over the address space and update the directory */
1062 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
Christian Königea097292017-08-09 14:15:46 +02001063 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1064 struct amdgpu_bo *bo = entry->base.bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001065 uint64_t pde, pt;
1066
1067 if (bo == NULL)
1068 continue;
1069
Christian Königea097292017-08-09 14:15:46 +02001070 spin_lock(&vm->status_lock);
1071 list_del_init(&entry->base.vm_status);
1072 spin_unlock(&vm->status_lock);
1073
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074 pt = amdgpu_bo_gpu_offset(bo);
Christian König53e2e912017-05-15 15:19:10 +02001075 pt = amdgpu_gart_get_vm_pde(adev, pt);
Christian König4ab40162017-08-03 20:30:50 +02001076 /* Don't update huge pages here */
1077 if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
1078 parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
Christian Königf8991ba2016-09-16 15:36:49 +02001079 continue;
1080
Christian König4ab40162017-08-03 20:30:50 +02001081 parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001082
1083 pde = pd_addr + pt_idx * 8;
Christian Königea097292017-08-09 14:15:46 +02001084 incr = amdgpu_bo_size(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001085 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001086 ((last_pt + incr * count) != pt) ||
1087 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001088
1089 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +02001090 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001091 params.func(&params,
1092 last_shadow,
1093 last_pt, count,
1094 incr,
1095 AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001096
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001097 params.func(&params, last_pde,
1098 last_pt, count, incr,
1099 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 }
1101
1102 count = 1;
1103 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001104 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001105 last_pt = pt;
1106 } else {
1107 ++count;
1108 }
1109 }
1110
Christian Königf8991ba2016-09-16 15:36:49 +02001111 if (count) {
Christian König3f3333f2017-08-03 14:02:13 +02001112 if (vm->root.base.bo->shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001113 params.func(&params, last_shadow, last_pt,
1114 count, incr, AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001115
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001116 params.func(&params, last_pde, last_pt,
1117 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001118 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001119
Christian König0a096fb2017-07-12 10:01:48 +02001120 if (!vm->use_cpu_for_update) {
1121 if (params.ib->length_dw == 0) {
1122 amdgpu_job_free(job);
1123 } else {
1124 amdgpu_ring_pad_ib(ring, params.ib);
Christian König3f3333f2017-08-03 14:02:13 +02001125 amdgpu_sync_resv(adev, &job->sync,
1126 parent->base.bo->tbo.resv,
Christian König194d2162016-10-12 15:13:52 +02001127 AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001128 if (shadow)
1129 amdgpu_sync_resv(adev, &job->sync,
1130 shadow->tbo.resv,
1131 AMDGPU_FENCE_OWNER_VM);
Christian Königf8991ba2016-09-16 15:36:49 +02001132
Christian König0a096fb2017-07-12 10:01:48 +02001133 WARN_ON(params.ib->length_dw > ndw);
1134 r = amdgpu_job_submit(job, ring, &vm->entity,
1135 AMDGPU_FENCE_OWNER_VM, &fence);
1136 if (r)
1137 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001138
Christian König3f3333f2017-08-03 14:02:13 +02001139 amdgpu_bo_fence(parent->base.bo, fence, true);
Christian König0a096fb2017-07-12 10:01:48 +02001140 dma_fence_put(vm->last_dir_update);
1141 vm->last_dir_update = dma_fence_get(fence);
1142 dma_fence_put(fence);
1143 }
Christian König194d2162016-10-12 15:13:52 +02001144 }
Christian Königf8991ba2016-09-16 15:36:49 +02001145
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001147
1148error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001149 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001150 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151}
1152
Christian König194d2162016-10-12 15:13:52 +02001153/*
Christian König92456b92017-05-12 16:09:26 +02001154 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1155 *
1156 * @parent: parent PD
1157 *
1158 * Mark all PD level as invalid after an error.
1159 */
Christian Königea097292017-08-09 14:15:46 +02001160static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
1161 struct amdgpu_vm_pt *parent)
Christian König92456b92017-05-12 16:09:26 +02001162{
1163 unsigned pt_idx;
1164
1165 /*
1166 * Recurse into the subdirectories. This recursion is harmless because
1167 * we only have a maximum of 5 layers.
1168 */
1169 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1170 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1171
Christian König3f3333f2017-08-03 14:02:13 +02001172 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001173 continue;
1174
1175 entry->addr = ~0ULL;
Christian Königea097292017-08-09 14:15:46 +02001176 spin_lock(&vm->status_lock);
1177 list_move(&entry->base.vm_status, &vm->relocated);
1178 spin_unlock(&vm->status_lock);
1179 amdgpu_vm_invalidate_level(vm, entry);
Christian König92456b92017-05-12 16:09:26 +02001180 }
1181}
1182
1183/*
Christian König194d2162016-10-12 15:13:52 +02001184 * amdgpu_vm_update_directories - make sure that all directories are valid
1185 *
1186 * @adev: amdgpu_device pointer
1187 * @vm: requested vm
1188 *
1189 * Makes sure all directories are up to date.
1190 * Returns 0 for success, error for failure.
1191 */
1192int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1193 struct amdgpu_vm *vm)
1194{
Christian König92456b92017-05-12 16:09:26 +02001195 int r;
1196
Christian Königea097292017-08-09 14:15:46 +02001197 spin_lock(&vm->status_lock);
1198 while (!list_empty(&vm->relocated)) {
1199 struct amdgpu_vm_bo_base *bo_base;
1200 struct amdgpu_bo *bo;
1201
1202 bo_base = list_first_entry(&vm->relocated,
1203 struct amdgpu_vm_bo_base,
1204 vm_status);
1205 spin_unlock(&vm->status_lock);
1206
1207 bo = bo_base->bo->parent;
1208 if (bo) {
1209 struct amdgpu_vm_bo_base *parent;
1210 struct amdgpu_vm_pt *pt;
1211
1212 parent = list_first_entry(&bo->va,
1213 struct amdgpu_vm_bo_base,
1214 bo_list);
1215 pt = container_of(parent, struct amdgpu_vm_pt, base);
1216
1217 r = amdgpu_vm_update_level(adev, vm, pt);
1218 if (r) {
1219 amdgpu_vm_invalidate_level(vm, &vm->root);
1220 return r;
1221 }
1222 spin_lock(&vm->status_lock);
1223 } else {
1224 spin_lock(&vm->status_lock);
1225 list_del_init(&bo_base->vm_status);
1226 }
1227 }
1228 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +02001229
Christian König68c62302017-07-11 17:23:29 +02001230 if (vm->use_cpu_for_update) {
1231 /* Flush HDP */
1232 mb();
1233 amdgpu_gart_flush_gpu_tlb(adev, 0);
1234 }
1235
Christian König92456b92017-05-12 16:09:26 +02001236 return r;
Christian König194d2162016-10-12 15:13:52 +02001237}
1238
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001240 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001241 *
1242 * @p: see amdgpu_pte_update_params definition
1243 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001244 * @entry: resulting entry or NULL
1245 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001246 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001247 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001248 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001249void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1250 struct amdgpu_vm_pt **entry,
1251 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001252{
Christian König4e2cb642016-10-25 15:52:28 +02001253 unsigned idx, level = p->adev->vm_manager.num_level;
1254
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001255 *parent = NULL;
1256 *entry = &p->vm->root;
1257 while ((*entry)->entries) {
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001258 idx = addr >> (p->adev->vm_manager.block_size * level--);
Christian König3f3333f2017-08-03 14:02:13 +02001259 idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001260 *parent = *entry;
1261 *entry = &(*entry)->entries[idx];
Christian König4e2cb642016-10-25 15:52:28 +02001262 }
1263
1264 if (level)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001265 *entry = NULL;
1266}
Christian König4e2cb642016-10-25 15:52:28 +02001267
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001268/**
1269 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1270 *
1271 * @p: see amdgpu_pte_update_params definition
1272 * @entry: vm_pt entry to check
1273 * @parent: parent entry
1274 * @nptes: number of PTEs updated with this operation
1275 * @dst: destination address where the PTEs should point to
1276 * @flags: access flags fro the PTEs
1277 *
1278 * Check if we can update the PD with a huge page.
1279 */
Christian Königec5207c2017-08-03 19:24:06 +02001280static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1281 struct amdgpu_vm_pt *entry,
1282 struct amdgpu_vm_pt *parent,
1283 unsigned nptes, uint64_t dst,
1284 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001285{
1286 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1287 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001288
1289 /* In the case of a mixed PT the PDE must point to it*/
1290 if (p->adev->asic_type < CHIP_VEGA10 ||
1291 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehling38a87912017-08-17 16:37:49 -04001292 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001293 !(flags & AMDGPU_PTE_VALID)) {
1294
Christian König3f3333f2017-08-03 14:02:13 +02001295 dst = amdgpu_bo_gpu_offset(entry->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001296 dst = amdgpu_gart_get_vm_pde(p->adev, dst);
1297 flags = AMDGPU_PTE_VALID;
1298 } else {
Christian König4ab40162017-08-03 20:30:50 +02001299 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001300 flags |= AMDGPU_PDE_PTE;
1301 }
1302
Christian König4ab40162017-08-03 20:30:50 +02001303 if (entry->addr == (dst | flags))
Christian Königec5207c2017-08-03 19:24:06 +02001304 return;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001305
Christian König4ab40162017-08-03 20:30:50 +02001306 entry->addr = (dst | flags);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001307
1308 if (use_cpu_update) {
Felix Kuehling38a87912017-08-17 16:37:49 -04001309 /* In case a huge page is replaced with a system
1310 * memory mapping, p->pages_addr != NULL and
1311 * amdgpu_vm_cpu_set_ptes would try to translate dst
1312 * through amdgpu_vm_map_gart. But dst is already a
1313 * GPU address (of the page table). Disable
1314 * amdgpu_vm_map_gart temporarily.
1315 */
1316 dma_addr_t *tmp;
1317
1318 tmp = p->pages_addr;
1319 p->pages_addr = NULL;
1320
Christian König3f3333f2017-08-03 14:02:13 +02001321 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001322 pde = pd_addr + (entry - parent->entries) * 8;
1323 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehling38a87912017-08-17 16:37:49 -04001324
1325 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001326 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001327 if (parent->base.bo->shadow) {
1328 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001329 pde = pd_addr + (entry - parent->entries) * 8;
1330 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1331 }
Christian König3f3333f2017-08-03 14:02:13 +02001332 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001333 pde = pd_addr + (entry - parent->entries) * 8;
1334 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1335 }
Christian König4e2cb642016-10-25 15:52:28 +02001336}
1337
1338/**
Christian König92696dd2016-08-05 13:56:35 +02001339 * amdgpu_vm_update_ptes - make sure that page tables are valid
1340 *
1341 * @params: see amdgpu_pte_update_params definition
1342 * @vm: requested vm
1343 * @start: start of GPU address range
1344 * @end: end of GPU address range
1345 * @dst: destination address to map to, the next dst inside the function
1346 * @flags: mapping flags
1347 *
1348 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001349 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001350 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001351static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001352 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001353 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001354{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001355 struct amdgpu_device *adev = params->adev;
1356 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001357
Christian König301654a2017-05-16 14:30:27 +02001358 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001359 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001360 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001361 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001362
1363 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001364 for (addr = start; addr < end; addr += nptes,
1365 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1366 struct amdgpu_vm_pt *entry, *parent;
1367
1368 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1369 if (!entry)
1370 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001371
Christian König92696dd2016-08-05 13:56:35 +02001372 if ((addr & ~mask) == (end & ~mask))
1373 nptes = end - addr;
1374 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001375 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001376
Christian Königec5207c2017-08-03 19:24:06 +02001377 amdgpu_vm_handle_huge_pages(params, entry, parent,
1378 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001379 /* We don't need to update PTEs for huge pages */
1380 if (entry->addr & AMDGPU_PDE_PTE)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001381 continue;
1382
Christian König3f3333f2017-08-03 14:02:13 +02001383 pt = entry->base.bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001384 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001385 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001386 } else {
1387 if (pt->shadow) {
1388 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1389 pe_start += (addr & mask) * 8;
1390 params->func(params, pe_start, dst, nptes,
1391 AMDGPU_GPU_PAGE_SIZE, flags);
1392 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001393 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001394 }
Christian König92696dd2016-08-05 13:56:35 +02001395
Christian König301654a2017-05-16 14:30:27 +02001396 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001397 params->func(params, pe_start, dst, nptes,
1398 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001399 }
1400
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001401 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001402}
1403
1404/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1406 *
Christian König29efc4f2016-08-04 14:52:50 +02001407 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001408 * @vm: requested vm
1409 * @start: first PTE to handle
1410 * @end: last PTE to handle
1411 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001413 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001415static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001416 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001417 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418{
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001419 int r;
1420
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001421 /**
1422 * The MC L1 TLB supports variable sized pages, based on a fragment
1423 * field in the PTE. When this field is set to a non-zero value, page
1424 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1425 * flags are considered valid for all PTEs within the fragment range
1426 * and corresponding mappings are assumed to be physically contiguous.
1427 *
1428 * The L1 TLB can store a single PTE for the whole fragment,
1429 * significantly increasing the space available for translation
1430 * caching. This leads to large improvements in throughput when the
1431 * TLB is under pressure.
1432 *
1433 * The L2 TLB distributes small and large fragments into two
1434 * asymmetric partitions. The large fragment cache is significantly
1435 * larger. Thus, we try to use large fragments wherever possible.
1436 * Userspace can support this by aligning virtual base address and
1437 * allocation size to the fragment size.
1438 */
Roger Hee618d302017-08-11 20:00:41 +08001439 unsigned pages_per_frag = params->adev->vm_manager.fragment_size;
Christian König6be7adb2017-05-23 18:35:22 +02001440 uint64_t frag_flags = AMDGPU_PTE_FRAG(pages_per_frag);
1441 uint64_t frag_align = 1 << pages_per_frag;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001442
Christian König92696dd2016-08-05 13:56:35 +02001443 uint64_t frag_start = ALIGN(start, frag_align);
1444 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +01001445
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001446 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +02001447 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001448 (frag_start >= frag_end))
1449 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001450
1451 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +02001452 if (start != frag_start) {
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001453 r = amdgpu_vm_update_ptes(params, start, frag_start,
1454 dst, flags);
1455 if (r)
1456 return r;
Christian König92696dd2016-08-05 13:56:35 +02001457 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458 }
1459
1460 /* handle the area in the middle */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001461 r = amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
1462 flags | frag_flags);
1463 if (r)
1464 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001465
1466 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +02001467 if (frag_end != end) {
1468 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001469 r = amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470 }
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001471 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001472}
1473
1474/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001475 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1476 *
1477 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001478 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001479 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001480 * @vm: requested vm
1481 * @start: start of mapped range
1482 * @last: last mapped entry
1483 * @flags: flags for the entries
1484 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001485 * @fence: optional resulting fence
1486 *
Christian Königa14faa62016-01-25 14:27:31 +01001487 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489 */
1490static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001491 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001492 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001494 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001495 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001496 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497{
Christian König2d55e452016-02-08 17:37:38 +01001498 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001499 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001500 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001501 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001502 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001503 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001504 int r;
1505
Christian Königafef8b82016-08-12 13:29:18 +02001506 memset(&params, 0, sizeof(params));
1507 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001508 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001509
Christian Königa33cab72017-07-11 17:13:00 +02001510 /* sync to everything on unmapping */
1511 if (!(flags & AMDGPU_PTE_VALID))
1512 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1513
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001514 if (vm->use_cpu_for_update) {
1515 /* params.src is used as flag to indicate system Memory */
1516 if (pages_addr)
1517 params.src = ~0;
1518
1519 /* Wait for PT BOs to be free. PTs share the same resv. object
1520 * as the root PD BO
1521 */
Christian Königa33cab72017-07-11 17:13:00 +02001522 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001523 if (unlikely(r))
1524 return r;
1525
1526 params.func = amdgpu_vm_cpu_set_ptes;
1527 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001528 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1529 addr, flags);
1530 }
1531
Christian König2d55e452016-02-08 17:37:38 +01001532 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001533
Christian Königa14faa62016-01-25 14:27:31 +01001534 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001535
1536 /*
1537 * reserve space for one command every (1 << BLOCK_SIZE)
1538 * entries or 2k dwords (whatever is smaller)
1539 */
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001540 ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541
1542 /* padding, etc. */
1543 ndw = 64;
1544
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001545 /* one PDE write for each huge page */
1546 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1547
Christian König570144c2017-08-30 15:38:45 +02001548 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001549 /* copy commands needed */
1550 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551
Christian Königb0456f92016-08-11 14:06:54 +02001552 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553 ndw += nptes * 2;
1554
Christian Königafef8b82016-08-12 13:29:18 +02001555 params.func = amdgpu_vm_do_copy_ptes;
1556
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 } else {
1558 /* set page commands needed */
1559 ndw += ncmds * 10;
1560
1561 /* two extra commands for begin/end of fragment */
1562 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001563
1564 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001565 }
1566
Christian Königd71518b2016-02-01 12:20:25 +01001567 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1568 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001570
Christian König29efc4f2016-08-04 14:52:50 +02001571 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001572
Christian König570144c2017-08-30 15:38:45 +02001573 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001574 uint64_t *pte;
1575 unsigned i;
1576
1577 /* Put the PTEs at the end of the IB. */
1578 i = ndw - nptes * 2;
1579 pte= (uint64_t *)&(job->ibs->ptr[i]);
1580 params.src = job->ibs->gpu_addr + i * 4;
1581
1582 for (i = 0; i < nptes; ++i) {
1583 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1584 AMDGPU_GPU_PAGE_SIZE);
1585 pte[i] |= flags;
1586 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001587 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001588 }
1589
Christian König3cabaa52016-06-06 10:17:58 +02001590 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1591 if (r)
1592 goto error_free;
1593
Christian König3f3333f2017-08-03 14:02:13 +02001594 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001595 owner);
1596 if (r)
1597 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598
Christian König3f3333f2017-08-03 14:02:13 +02001599 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001600 if (r)
1601 goto error_free;
1602
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001603 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1604 if (r)
1605 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606
Christian König29efc4f2016-08-04 14:52:50 +02001607 amdgpu_ring_pad_ib(ring, params.ib);
1608 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001609 r = amdgpu_job_submit(job, ring, &vm->entity,
1610 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001611 if (r)
1612 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001613
Christian König3f3333f2017-08-03 14:02:13 +02001614 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001615 dma_fence_put(*fence);
1616 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001618
1619error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001620 amdgpu_job_free(job);
Christian Königea097292017-08-09 14:15:46 +02001621 amdgpu_vm_invalidate_level(vm, &vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001622 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623}
1624
1625/**
Christian Königa14faa62016-01-25 14:27:31 +01001626 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1627 *
1628 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001629 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001630 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001631 * @vm: requested vm
1632 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001633 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001634 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001635 * @fence: optional resulting fence
1636 *
1637 * Split the mapping into smaller chunks so that each update fits
1638 * into a SDMA IB.
1639 * Returns 0 for success, -EINVAL for failure.
1640 */
1641static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001642 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001643 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001644 struct amdgpu_vm *vm,
1645 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001646 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001647 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001648 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001649{
Christian König570144c2017-08-30 15:38:45 +02001650 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001651 int r;
1652
1653 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1654 * but in case of something, we filter the flags in first place
1655 */
1656 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1657 flags &= ~AMDGPU_PTE_READABLE;
1658 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1659 flags &= ~AMDGPU_PTE_WRITEABLE;
1660
Alex Xie15b31c52017-03-03 16:47:11 -05001661 flags &= ~AMDGPU_PTE_EXECUTABLE;
1662 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1663
Alex Xieb0fd18b2017-03-03 16:49:39 -05001664 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1665 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1666
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001667 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1668 (adev->asic_type >= CHIP_VEGA10)) {
1669 flags |= AMDGPU_PTE_PRT;
1670 flags &= ~AMDGPU_PTE_VALID;
1671 }
1672
Christian Königa14faa62016-01-25 14:27:31 +01001673 trace_amdgpu_vm_bo_update(mapping);
1674
Christian König63e0ba42016-08-16 17:38:37 +02001675 pfn = mapping->offset >> PAGE_SHIFT;
1676 if (nodes) {
1677 while (pfn >= nodes->size) {
1678 pfn -= nodes->size;
1679 ++nodes;
1680 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001681 }
Christian Königa14faa62016-01-25 14:27:31 +01001682
Christian König63e0ba42016-08-16 17:38:37 +02001683 do {
1684 uint64_t max_entries;
1685 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001686
Christian König63e0ba42016-08-16 17:38:37 +02001687 if (nodes) {
1688 addr = nodes->start << PAGE_SHIFT;
1689 max_entries = (nodes->size - pfn) *
1690 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1691 } else {
1692 addr = 0;
1693 max_entries = S64_MAX;
1694 }
Christian Königa14faa62016-01-25 14:27:31 +01001695
Christian König63e0ba42016-08-16 17:38:37 +02001696 if (pages_addr) {
Christian Königfebb84a2017-08-22 12:50:46 +02001697 max_entries = min(max_entries, 16ull * 1024ull);
Christian König63e0ba42016-08-16 17:38:37 +02001698 addr = 0;
1699 } else if (flags & AMDGPU_PTE_VALID) {
1700 addr += adev->vm_manager.vram_base_offset;
1701 }
1702 addr += pfn << PAGE_SHIFT;
1703
Christian Königa9f87f62017-03-30 14:03:59 +02001704 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König570144c2017-08-30 15:38:45 +02001705 r = amdgpu_vm_bo_update_mapping(adev, exclusive, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001706 start, last, flags, addr,
1707 fence);
1708 if (r)
1709 return r;
1710
Christian König63e0ba42016-08-16 17:38:37 +02001711 pfn += last - start + 1;
1712 if (nodes && nodes->size == pfn) {
1713 pfn = 0;
1714 ++nodes;
1715 }
Christian Königa14faa62016-01-25 14:27:31 +01001716 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001717
Christian Königa9f87f62017-03-30 14:03:59 +02001718 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001719
1720 return 0;
1721}
1722
1723/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001724 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1725 *
1726 * @adev: amdgpu_device pointer
1727 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001728 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001729 *
1730 * Fill in the page table entries for @bo_va.
1731 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001732 */
1733int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1734 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001735 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001736{
Christian Königec681542017-08-01 10:51:43 +02001737 struct amdgpu_bo *bo = bo_va->base.bo;
1738 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001740 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001741 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001742 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001743 struct dma_fence *exclusive;
Christian Königfebb84a2017-08-22 12:50:46 +02001744 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001745 int r;
1746
Christian Königec681542017-08-01 10:51:43 +02001747 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001748 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001749 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001750 exclusive = NULL;
1751 } else {
Christian König8358dce2016-03-30 10:50:25 +02001752 struct ttm_dma_tt *ttm;
1753
Christian Königec681542017-08-01 10:51:43 +02001754 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001755 nodes = mem->mm_node;
1756 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001757 ttm = container_of(bo_va->base.bo->tbo.ttm,
1758 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001759 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001760 }
Christian Königec681542017-08-01 10:51:43 +02001761 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001762 }
1763
Christian Königfebb84a2017-08-22 12:50:46 +02001764 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001765 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian Königfebb84a2017-08-22 12:50:46 +02001766 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001767 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001768
Christian König3d7d4d32017-08-23 16:13:33 +02001769 if (!clear && bo_va->base.moved) {
1770 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001771 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001772
Christian Königcb7b6ec2017-08-15 17:08:12 +02001773 } else if (bo_va->cleared != clear) {
1774 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001775 }
Christian König7fc11952015-07-30 11:53:42 +02001776
1777 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königfebb84a2017-08-22 12:50:46 +02001778 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001779 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001780 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001781 if (r)
1782 return r;
1783 }
1784
Christian König68c62302017-07-11 17:23:29 +02001785 if (vm->use_cpu_for_update) {
1786 /* Flush HDP */
1787 mb();
1788 amdgpu_gart_flush_gpu_tlb(adev, 0);
1789 }
1790
Christian Königcb7b6ec2017-08-15 17:08:12 +02001791 spin_lock(&vm->status_lock);
1792 list_del_init(&bo_va->base.vm_status);
1793 spin_unlock(&vm->status_lock);
1794
1795 list_splice_init(&bo_va->invalids, &bo_va->valids);
1796 bo_va->cleared = clear;
1797
1798 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1799 list_for_each_entry(mapping, &bo_va->valids, list)
1800 trace_amdgpu_vm_bo_mapping(mapping);
1801 }
1802
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001803 return 0;
1804}
1805
1806/**
Christian König284710f2017-01-30 11:09:31 +01001807 * amdgpu_vm_update_prt_state - update the global PRT state
1808 */
1809static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1810{
1811 unsigned long flags;
1812 bool enable;
1813
1814 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001815 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001816 adev->gart.gart_funcs->set_prt(adev, enable);
1817 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1818}
1819
1820/**
Christian König4388fc22017-03-13 10:13:36 +01001821 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001822 */
1823static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1824{
Christian König4388fc22017-03-13 10:13:36 +01001825 if (!adev->gart.gart_funcs->set_prt)
1826 return;
1827
Christian König451bc8e2017-02-14 16:02:52 +01001828 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1829 amdgpu_vm_update_prt_state(adev);
1830}
1831
1832/**
Christian König0b15f2f2017-02-14 15:47:03 +01001833 * amdgpu_vm_prt_put - drop a PRT user
1834 */
1835static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1836{
Christian König451bc8e2017-02-14 16:02:52 +01001837 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001838 amdgpu_vm_update_prt_state(adev);
1839}
1840
1841/**
Christian König451bc8e2017-02-14 16:02:52 +01001842 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001843 */
1844static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1845{
1846 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1847
Christian König0b15f2f2017-02-14 15:47:03 +01001848 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001849 kfree(cb);
1850}
1851
1852/**
Christian König451bc8e2017-02-14 16:02:52 +01001853 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1854 */
1855static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1856 struct dma_fence *fence)
1857{
Christian König4388fc22017-03-13 10:13:36 +01001858 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001859
Christian König4388fc22017-03-13 10:13:36 +01001860 if (!adev->gart.gart_funcs->set_prt)
1861 return;
1862
1863 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001864 if (!cb) {
1865 /* Last resort when we are OOM */
1866 if (fence)
1867 dma_fence_wait(fence, false);
1868
Dan Carpenter486a68f2017-04-03 21:41:39 +03001869 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001870 } else {
1871 cb->adev = adev;
1872 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1873 amdgpu_vm_prt_cb))
1874 amdgpu_vm_prt_cb(fence, &cb->cb);
1875 }
1876}
1877
1878/**
Christian König284710f2017-01-30 11:09:31 +01001879 * amdgpu_vm_free_mapping - free a mapping
1880 *
1881 * @adev: amdgpu_device pointer
1882 * @vm: requested vm
1883 * @mapping: mapping to be freed
1884 * @fence: fence of the unmap operation
1885 *
1886 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1887 */
1888static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1889 struct amdgpu_vm *vm,
1890 struct amdgpu_bo_va_mapping *mapping,
1891 struct dma_fence *fence)
1892{
Christian König451bc8e2017-02-14 16:02:52 +01001893 if (mapping->flags & AMDGPU_PTE_PRT)
1894 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001895 kfree(mapping);
1896}
1897
1898/**
Christian König451bc8e2017-02-14 16:02:52 +01001899 * amdgpu_vm_prt_fini - finish all prt mappings
1900 *
1901 * @adev: amdgpu_device pointer
1902 * @vm: requested vm
1903 *
1904 * Register a cleanup callback to disable PRT support after VM dies.
1905 */
1906static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1907{
Christian König3f3333f2017-08-03 14:02:13 +02001908 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001909 struct dma_fence *excl, **shared;
1910 unsigned i, shared_count;
1911 int r;
1912
1913 r = reservation_object_get_fences_rcu(resv, &excl,
1914 &shared_count, &shared);
1915 if (r) {
1916 /* Not enough memory to grab the fence list, as last resort
1917 * block for all the fences to complete.
1918 */
1919 reservation_object_wait_timeout_rcu(resv, true, false,
1920 MAX_SCHEDULE_TIMEOUT);
1921 return;
1922 }
1923
1924 /* Add a callback for each fence in the reservation object */
1925 amdgpu_vm_prt_get(adev);
1926 amdgpu_vm_add_prt_cb(adev, excl);
1927
1928 for (i = 0; i < shared_count; ++i) {
1929 amdgpu_vm_prt_get(adev);
1930 amdgpu_vm_add_prt_cb(adev, shared[i]);
1931 }
1932
1933 kfree(shared);
1934}
1935
1936/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001937 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1938 *
1939 * @adev: amdgpu_device pointer
1940 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001941 * @fence: optional resulting fence (unchanged if no work needed to be done
1942 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001943 *
1944 * Make sure all freed BOs are cleared in the PT.
1945 * Returns 0 for success.
1946 *
1947 * PTs have to be reserved and mutex must be locked!
1948 */
1949int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001950 struct amdgpu_vm *vm,
1951 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001952{
1953 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001954 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001955 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001956 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001957
1958 while (!list_empty(&vm->freed)) {
1959 mapping = list_first_entry(&vm->freed,
1960 struct amdgpu_bo_va_mapping, list);
1961 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001962
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001963 if (vm->pte_support_ats)
1964 init_pte_value = AMDGPU_PTE_SYSTEM;
1965
Christian König570144c2017-08-30 15:38:45 +02001966 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001967 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001968 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001969 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001970 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001971 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001972 return r;
Christian König284710f2017-01-30 11:09:31 +01001973 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001974 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001975
1976 if (fence && f) {
1977 dma_fence_put(*fence);
1978 *fence = f;
1979 } else {
1980 dma_fence_put(f);
1981 }
1982
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001983 return 0;
1984
1985}
1986
1987/**
Christian König27c7b9a2017-08-01 11:27:36 +02001988 * amdgpu_vm_clear_moved - clear moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001989 *
1990 * @adev: amdgpu_device pointer
1991 * @vm: requested vm
1992 *
Christian König27c7b9a2017-08-01 11:27:36 +02001993 * Make sure all moved BOs are cleared in the PT.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001994 * Returns 0 for success.
1995 *
1996 * PTs have to be reserved and mutex must be locked!
1997 */
Christian König27c7b9a2017-08-01 11:27:36 +02001998int amdgpu_vm_clear_moved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1999 struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002000{
monk.liucfe2c972015-05-26 15:01:54 +08002001 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02002002 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002003
2004 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002005 while (!list_empty(&vm->moved)) {
2006 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002007 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002008 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002009
Christian König99e124f2016-08-16 14:43:17 +02002010 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002011 if (r)
2012 return r;
2013
2014 spin_lock(&vm->status_lock);
2015 }
2016 spin_unlock(&vm->status_lock);
2017
monk.liucfe2c972015-05-26 15:01:54 +08002018 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08002019 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02002020
2021 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002022}
2023
2024/**
2025 * amdgpu_vm_bo_add - add a bo to a specific vm
2026 *
2027 * @adev: amdgpu_device pointer
2028 * @vm: requested vm
2029 * @bo: amdgpu buffer object
2030 *
Christian König8843dbb2016-01-26 12:17:11 +01002031 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002032 * Add @bo to the list of bos associated with the vm
2033 * Returns newly added bo_va or NULL for failure
2034 *
2035 * Object has to be reserved!
2036 */
2037struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2038 struct amdgpu_vm *vm,
2039 struct amdgpu_bo *bo)
2040{
2041 struct amdgpu_bo_va *bo_va;
2042
2043 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2044 if (bo_va == NULL) {
2045 return NULL;
2046 }
Christian Königec681542017-08-01 10:51:43 +02002047 bo_va->base.vm = vm;
2048 bo_va->base.bo = bo;
2049 INIT_LIST_HEAD(&bo_va->base.bo_list);
2050 INIT_LIST_HEAD(&bo_va->base.vm_status);
2051
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002052 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002053 INIT_LIST_HEAD(&bo_va->valids);
2054 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002055
Christian Königa5f6b5b2017-01-30 11:01:38 +01002056 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002057 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002058
2059 return bo_va;
2060}
2061
2062/**
2063 * amdgpu_vm_bo_map - map bo inside a vm
2064 *
2065 * @adev: amdgpu_device pointer
2066 * @bo_va: bo_va to store the address
2067 * @saddr: where to map the BO
2068 * @offset: requested offset in the BO
2069 * @flags: attributes of pages (read/write/valid/etc.)
2070 *
2071 * Add a mapping of the BO at the specefied addr into the VM.
2072 * Returns 0 for success, error for failure.
2073 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002074 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075 */
2076int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2077 struct amdgpu_bo_va *bo_va,
2078 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002079 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080{
Christian Königa9f87f62017-03-30 14:03:59 +02002081 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002082 struct amdgpu_bo *bo = bo_va->base.bo;
2083 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002084 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085
Christian König0be52de2015-05-18 14:37:27 +02002086 /* validate the parameters */
2087 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002088 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002089 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002090
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002092 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002093 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002094 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002095 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002096
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097 saddr /= AMDGPU_GPU_PAGE_SIZE;
2098 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2099
Christian Königa9f87f62017-03-30 14:03:59 +02002100 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2101 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002102 /* bo and tmp overlap, invalid addr */
2103 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002104 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002105 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002106 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002107 }
2108
2109 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002110 if (!mapping)
2111 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002112
2113 INIT_LIST_HEAD(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002114 mapping->start = saddr;
2115 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002116 mapping->offset = offset;
2117 mapping->flags = flags;
2118
Christian König7fc11952015-07-30 11:53:42 +02002119 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02002120 amdgpu_vm_it_insert(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002121
Christian König4388fc22017-03-13 10:13:36 +01002122 if (flags & AMDGPU_PTE_PRT)
2123 amdgpu_vm_prt_get(adev);
Christian König87f64a72017-08-23 14:05:48 +02002124 trace_amdgpu_vm_bo_map(bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002125
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002126 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002127}
2128
2129/**
Christian König80f95c52017-03-13 10:13:39 +01002130 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2131 *
2132 * @adev: amdgpu_device pointer
2133 * @bo_va: bo_va to store the address
2134 * @saddr: where to map the BO
2135 * @offset: requested offset in the BO
2136 * @flags: attributes of pages (read/write/valid/etc.)
2137 *
2138 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2139 * mappings as we do so.
2140 * Returns 0 for success, error for failure.
2141 *
2142 * Object has to be reserved and unreserved outside!
2143 */
2144int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2145 struct amdgpu_bo_va *bo_va,
2146 uint64_t saddr, uint64_t offset,
2147 uint64_t size, uint64_t flags)
2148{
2149 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002150 struct amdgpu_bo *bo = bo_va->base.bo;
2151 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König80f95c52017-03-13 10:13:39 +01002152 uint64_t eaddr;
2153 int r;
2154
2155 /* validate the parameters */
2156 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2157 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2158 return -EINVAL;
2159
2160 /* make sure object fit at this offset */
2161 eaddr = saddr + size - 1;
2162 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002163 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002164 return -EINVAL;
2165
2166 /* Allocate all the needed memory */
2167 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2168 if (!mapping)
2169 return -ENOMEM;
2170
Christian Königec681542017-08-01 10:51:43 +02002171 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002172 if (r) {
2173 kfree(mapping);
2174 return r;
2175 }
2176
2177 saddr /= AMDGPU_GPU_PAGE_SIZE;
2178 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2179
Christian Königa9f87f62017-03-30 14:03:59 +02002180 mapping->start = saddr;
2181 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002182 mapping->offset = offset;
2183 mapping->flags = flags;
2184
2185 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02002186 amdgpu_vm_it_insert(mapping, &vm->va);
Christian König80f95c52017-03-13 10:13:39 +01002187
2188 if (flags & AMDGPU_PTE_PRT)
2189 amdgpu_vm_prt_get(adev);
Christian König87f64a72017-08-23 14:05:48 +02002190 trace_amdgpu_vm_bo_map(bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002191
2192 return 0;
2193}
2194
2195/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002196 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2197 *
2198 * @adev: amdgpu_device pointer
2199 * @bo_va: bo_va to remove the address from
2200 * @saddr: where to the BO is mapped
2201 *
2202 * Remove a mapping of the BO at the specefied addr from the VM.
2203 * Returns 0 for success, error for failure.
2204 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002205 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002206 */
2207int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2208 struct amdgpu_bo_va *bo_va,
2209 uint64_t saddr)
2210{
2211 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002212 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002213 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214
Christian König6c7fc502015-06-05 20:56:17 +02002215 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002216
Christian König7fc11952015-07-30 11:53:42 +02002217 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002218 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 break;
2220 }
2221
Christian König7fc11952015-07-30 11:53:42 +02002222 if (&mapping->list == &bo_va->valids) {
2223 valid = false;
2224
2225 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002226 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002227 break;
2228 }
2229
Christian König32b41ac2016-03-08 18:03:27 +01002230 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002231 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232 }
Christian König32b41ac2016-03-08 18:03:27 +01002233
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002234 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002235 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002236 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002237
Christian Könige17841b2016-03-08 17:52:01 +01002238 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002239 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002240 else
Christian König284710f2017-01-30 11:09:31 +01002241 amdgpu_vm_free_mapping(adev, vm, mapping,
2242 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243
2244 return 0;
2245}
2246
2247/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002248 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2249 *
2250 * @adev: amdgpu_device pointer
2251 * @vm: VM structure to use
2252 * @saddr: start of the range
2253 * @size: size of the range
2254 *
2255 * Remove all mappings in a range, split them as appropriate.
2256 * Returns 0 for success, error for failure.
2257 */
2258int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2259 struct amdgpu_vm *vm,
2260 uint64_t saddr, uint64_t size)
2261{
2262 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002263 LIST_HEAD(removed);
2264 uint64_t eaddr;
2265
2266 eaddr = saddr + size - 1;
2267 saddr /= AMDGPU_GPU_PAGE_SIZE;
2268 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2269
2270 /* Allocate all the needed memory */
2271 before = kzalloc(sizeof(*before), GFP_KERNEL);
2272 if (!before)
2273 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002274 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002275
2276 after = kzalloc(sizeof(*after), GFP_KERNEL);
2277 if (!after) {
2278 kfree(before);
2279 return -ENOMEM;
2280 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002281 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002282
2283 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002284 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2285 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002286 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002287 if (tmp->start < saddr) {
2288 before->start = tmp->start;
2289 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002290 before->offset = tmp->offset;
2291 before->flags = tmp->flags;
2292 list_add(&before->list, &tmp->list);
2293 }
2294
2295 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002296 if (tmp->last > eaddr) {
2297 after->start = eaddr + 1;
2298 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002299 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002300 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002301 after->flags = tmp->flags;
2302 list_add(&after->list, &tmp->list);
2303 }
2304
2305 list_del(&tmp->list);
2306 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002307
2308 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002309 }
2310
2311 /* And free them up */
2312 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002313 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002314 list_del(&tmp->list);
2315
Christian Königa9f87f62017-03-30 14:03:59 +02002316 if (tmp->start < saddr)
2317 tmp->start = saddr;
2318 if (tmp->last > eaddr)
2319 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002320
2321 list_add(&tmp->list, &vm->freed);
2322 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2323 }
2324
Junwei Zhang27f6d612017-03-16 16:09:24 +08002325 /* Insert partial mapping before the range */
2326 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002327 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002328 if (before->flags & AMDGPU_PTE_PRT)
2329 amdgpu_vm_prt_get(adev);
2330 } else {
2331 kfree(before);
2332 }
2333
2334 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002335 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002336 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002337 if (after->flags & AMDGPU_PTE_PRT)
2338 amdgpu_vm_prt_get(adev);
2339 } else {
2340 kfree(after);
2341 }
2342
2343 return 0;
2344}
2345
2346/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002347 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2348 *
2349 * @adev: amdgpu_device pointer
2350 * @bo_va: requested bo_va
2351 *
Christian König8843dbb2016-01-26 12:17:11 +01002352 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002353 *
2354 * Object have to be reserved!
2355 */
2356void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2357 struct amdgpu_bo_va *bo_va)
2358{
2359 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002360 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002361
Christian Königec681542017-08-01 10:51:43 +02002362 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002363
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002364 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002365 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002366 spin_unlock(&vm->status_lock);
2367
Christian König7fc11952015-07-30 11:53:42 +02002368 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002369 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002370 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002371 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002372 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002373 }
Christian König7fc11952015-07-30 11:53:42 +02002374 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2375 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002376 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002377 amdgpu_vm_free_mapping(adev, vm, mapping,
2378 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002379 }
Christian König32b41ac2016-03-08 18:03:27 +01002380
Chris Wilsonf54d1862016-10-25 13:00:45 +01002381 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002383}
2384
2385/**
2386 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2387 *
2388 * @adev: amdgpu_device pointer
2389 * @vm: requested vm
2390 * @bo: amdgpu buffer object
2391 *
Christian König8843dbb2016-01-26 12:17:11 +01002392 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002393 */
2394void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002395 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002396{
Christian Königec681542017-08-01 10:51:43 +02002397 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002398
Christian Königec681542017-08-01 10:51:43 +02002399 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002400 struct amdgpu_vm *vm = bo_base->vm;
2401
Christian König3d7d4d32017-08-23 16:13:33 +02002402 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002403 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2404 spin_lock(&bo_base->vm->status_lock);
2405 list_move(&bo_base->vm_status, &vm->evicted);
2406 spin_unlock(&bo_base->vm->status_lock);
2407 continue;
2408 }
2409
Christian Königea097292017-08-09 14:15:46 +02002410 if (bo->tbo.type == ttm_bo_type_kernel) {
2411 spin_lock(&bo_base->vm->status_lock);
2412 if (list_empty(&bo_base->vm_status))
2413 list_add(&bo_base->vm_status, &vm->relocated);
2414 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002415 continue;
Christian Königea097292017-08-09 14:15:46 +02002416 }
Christian König3f3333f2017-08-03 14:02:13 +02002417
Christian Königec681542017-08-01 10:51:43 +02002418 spin_lock(&bo_base->vm->status_lock);
Christian Königcb7b6ec2017-08-15 17:08:12 +02002419 list_move(&bo_base->vm_status, &bo_base->vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002420 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002421 }
2422}
2423
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002424static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2425{
2426 /* Total bits covered by PD + PTs */
2427 unsigned bits = ilog2(vm_size) + 18;
2428
2429 /* Make sure the PD is 4K in size up to 8GB address space.
2430 Above that split equal between PD and PTs */
2431 if (vm_size <= 8)
2432 return (bits - 9);
2433 else
2434 return ((bits + 3) / 2);
2435}
2436
2437/**
Roger Hed07f14b2017-08-15 16:05:59 +08002438 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2439 *
2440 * @adev: amdgpu_device pointer
2441 * @fragment_size_default: the default fragment size if it's set auto
2442 */
2443void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev, uint32_t fragment_size_default)
2444{
2445 if (amdgpu_vm_fragment_size == -1)
2446 adev->vm_manager.fragment_size = fragment_size_default;
2447 else
2448 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2449}
2450
2451/**
2452 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002453 *
2454 * @adev: amdgpu_device pointer
2455 * @vm_size: the default vm size if it's set auto
2456 */
Roger Hed07f14b2017-08-15 16:05:59 +08002457void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size, uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002458{
2459 /* adjust vm size firstly */
2460 if (amdgpu_vm_size == -1)
2461 adev->vm_manager.vm_size = vm_size;
2462 else
2463 adev->vm_manager.vm_size = amdgpu_vm_size;
2464
2465 /* block size depends on vm size */
2466 if (amdgpu_vm_block_size == -1)
2467 adev->vm_manager.block_size =
2468 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2469 else
2470 adev->vm_manager.block_size = amdgpu_vm_block_size;
2471
Roger Hed07f14b2017-08-15 16:05:59 +08002472 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2473
2474 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2475 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2476 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002477}
2478
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479/**
2480 * amdgpu_vm_init - initialize a vm instance
2481 *
2482 * @adev: amdgpu_device pointer
2483 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002484 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002485 *
Christian König8843dbb2016-01-26 12:17:11 +01002486 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002487 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002488int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2489 int vm_context)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002490{
2491 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002492 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002493 unsigned ring_instance;
2494 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002495 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002496 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002497 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002498 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002500 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002501 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002502 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2503 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002504 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002505 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002506 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002507 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002508 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002509
Christian König2bd9ccf2016-02-01 12:53:58 +01002510 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002511
2512 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2513 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2514 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002515 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2516 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2517 rq, amdgpu_sched_jobs);
2518 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002519 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002520
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002521 vm->pte_support_ats = false;
2522
2523 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002524 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2525 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002526
2527 if (adev->asic_type == CHIP_RAVEN) {
2528 vm->pte_support_ats = true;
2529 init_pde_value = AMDGPU_PTE_SYSTEM | AMDGPU_PDE_PTE;
2530 }
2531 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002532 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2533 AMDGPU_VM_USE_CPU_FOR_GFX);
2534 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2535 vm->use_cpu_for_update ? "CPU" : "SDMA");
2536 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2537 "CPU update of VM recommended only for large BAR system\n");
Christian Königa24960f2016-10-12 13:20:52 +02002538 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002539
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002540 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2541 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2542 if (vm->use_cpu_for_update)
2543 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2544 else
2545 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2546 AMDGPU_GEM_CREATE_SHADOW);
2547
Christian Königf566ceb2016-10-27 20:04:38 +02002548 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002549 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002550 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002551 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002552 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002553 goto error_free_sched_entity;
2554
Christian König3f3333f2017-08-03 14:02:13 +02002555 vm->root.base.vm = vm;
2556 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2557 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002558
2559 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002560 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002561 if (r)
2562 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002563
Christian König3f3333f2017-08-03 14:02:13 +02002564 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
2565 if (r)
2566 goto error_free_root;
2567 amdgpu_bo_unreserve(vm->root.base.bo);
2568 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002569
2570 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002571
Christian König67003a12016-10-12 14:46:26 +02002572error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002573 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2574 amdgpu_bo_unref(&vm->root.base.bo);
2575 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002576
2577error_free_sched_entity:
2578 amd_sched_entity_fini(&ring->sched, &vm->entity);
2579
2580 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002581}
2582
2583/**
Christian Königf566ceb2016-10-27 20:04:38 +02002584 * amdgpu_vm_free_levels - free PD/PT levels
2585 *
2586 * @level: PD/PT starting level to free
2587 *
2588 * Free the page directory or page table level and all sub levels.
2589 */
2590static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2591{
2592 unsigned i;
2593
Christian König3f3333f2017-08-03 14:02:13 +02002594 if (level->base.bo) {
2595 list_del(&level->base.bo_list);
2596 list_del(&level->base.vm_status);
2597 amdgpu_bo_unref(&level->base.bo->shadow);
2598 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002599 }
2600
2601 if (level->entries)
2602 for (i = 0; i <= level->last_entry_used; i++)
2603 amdgpu_vm_free_levels(&level->entries[i]);
2604
Michal Hocko20981052017-05-17 14:23:12 +02002605 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002606}
2607
2608/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002609 * amdgpu_vm_fini - tear down a vm instance
2610 *
2611 * @adev: amdgpu_device pointer
2612 * @vm: requested vm
2613 *
Christian König8843dbb2016-01-26 12:17:11 +01002614 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002615 * Unbind the VM and remove all bos from the vm bo list
2616 */
2617void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2618{
2619 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002620 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002621 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002622
Christian König2d55e452016-02-08 17:37:38 +01002623 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002624
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002625 if (!RB_EMPTY_ROOT(&vm->va)) {
2626 dev_err(adev->dev, "still active bo inside vm\n");
2627 }
Christian Königa9f87f62017-03-30 14:03:59 +02002628 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002629 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002630 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002631 kfree(mapping);
2632 }
2633 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002634 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002635 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002636 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002637 }
Christian König284710f2017-01-30 11:09:31 +01002638
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002639 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002640 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002641 }
2642
Christian Königf566ceb2016-10-27 20:04:38 +02002643 amdgpu_vm_free_levels(&vm->root);
Christian Königa24960f2016-10-12 13:20:52 +02002644 dma_fence_put(vm->last_dir_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002645 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2646 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002647}
Christian Königea89f8c2015-11-15 20:52:06 +01002648
2649/**
Christian Königa9a78b32016-01-21 10:19:11 +01002650 * amdgpu_vm_manager_init - init the VM manager
2651 *
2652 * @adev: amdgpu_device pointer
2653 *
2654 * Initialize the VM manager structures
2655 */
2656void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2657{
Christian König76456702017-04-06 17:52:39 +02002658 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002659
Christian König76456702017-04-06 17:52:39 +02002660 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2661 struct amdgpu_vm_id_manager *id_mgr =
2662 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002663
Christian König76456702017-04-06 17:52:39 +02002664 mutex_init(&id_mgr->lock);
2665 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002666 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002667
2668 /* skip over VMID 0, since it is the system VM */
2669 for (j = 1; j < id_mgr->num_ids; ++j) {
2670 amdgpu_vm_reset_id(adev, i, j);
2671 amdgpu_sync_create(&id_mgr->ids[i].active);
2672 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2673 }
Christian König971fe9a92016-03-01 15:09:25 +01002674 }
Christian König2d55e452016-02-08 17:37:38 +01002675
Chris Wilsonf54d1862016-10-25 13:00:45 +01002676 adev->vm_manager.fence_context =
2677 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002678 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2679 adev->vm_manager.seqno[i] = 0;
2680
Christian König2d55e452016-02-08 17:37:38 +01002681 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002682 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002683 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002684 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002685
2686 /* If not overridden by the user, by default, only in large BAR systems
2687 * Compute VM tables will be updated by CPU
2688 */
2689#ifdef CONFIG_X86_64
2690 if (amdgpu_vm_update_mode == -1) {
2691 if (amdgpu_vm_is_large_bar(adev))
2692 adev->vm_manager.vm_update_mode =
2693 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2694 else
2695 adev->vm_manager.vm_update_mode = 0;
2696 } else
2697 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2698#else
2699 adev->vm_manager.vm_update_mode = 0;
2700#endif
2701
Christian Königa9a78b32016-01-21 10:19:11 +01002702}
2703
2704/**
Christian Königea89f8c2015-11-15 20:52:06 +01002705 * amdgpu_vm_manager_fini - cleanup VM manager
2706 *
2707 * @adev: amdgpu_device pointer
2708 *
2709 * Cleanup the VM manager and free resources.
2710 */
2711void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2712{
Christian König76456702017-04-06 17:52:39 +02002713 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002714
Christian König76456702017-04-06 17:52:39 +02002715 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2716 struct amdgpu_vm_id_manager *id_mgr =
2717 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002718
Christian König76456702017-04-06 17:52:39 +02002719 mutex_destroy(&id_mgr->lock);
2720 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2721 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2722
2723 amdgpu_sync_free(&id->active);
2724 dma_fence_put(id->flushed_updates);
2725 dma_fence_put(id->last_flush);
2726 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002727 }
Christian Königea89f8c2015-11-15 20:52:06 +01002728}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002729
2730int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2731{
2732 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002733 struct amdgpu_device *adev = dev->dev_private;
2734 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2735 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002736
2737 switch (args->in.op) {
2738 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002739 /* current, we only have requirement to reserve vmid from gfxhub */
2740 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2741 AMDGPU_GFXHUB);
2742 if (r)
2743 return r;
2744 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002745 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002746 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002747 break;
2748 default:
2749 return -EINVAL;
2750 }
2751
2752 return 0;
2753}