blob: 6ff3c1bf035e176c5ed619385aa8411a6fa243af [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);
199 list_del_init(&bo_base->vm_status);
200 }
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önig3f3333f2017-08-03 14:02:13 +0200312 pt->parent = amdgpu_bo_ref(vm->root.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);
317 INIT_LIST_HEAD(&entry->base.vm_status);
Christian Königf566ceb2016-10-27 20:04:38 +0200318 entry->addr = 0;
319 }
320
321 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400322 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
323 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
324 ((1 << shift) - 1);
325 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
326 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200327 if (r)
328 return r;
329 }
330 }
331
332 return 0;
333}
334
Christian König663e4572017-03-13 10:13:37 +0100335/**
336 * amdgpu_vm_alloc_pts - Allocate page tables.
337 *
338 * @adev: amdgpu_device pointer
339 * @vm: VM to allocate page tables for
340 * @saddr: Start address which needs to be allocated
341 * @size: Size from start address we need.
342 *
343 * Make sure the page tables are allocated.
344 */
345int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
346 struct amdgpu_vm *vm,
347 uint64_t saddr, uint64_t size)
348{
Felix Kuehling22770e52017-03-28 20:24:53 -0400349 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100350 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100351
352 /* validate the parameters */
353 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
354 return -EINVAL;
355
356 eaddr = saddr + size - 1;
357 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
358 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400359 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100360 last_pfn, adev->vm_manager.max_pfn);
361 return -EINVAL;
362 }
363
364 saddr /= AMDGPU_GPU_PAGE_SIZE;
365 eaddr /= AMDGPU_GPU_PAGE_SIZE;
366
Christian Königf566ceb2016-10-27 20:04:38 +0200367 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100368}
369
Christian König641e9402017-04-03 13:59:25 +0200370/**
371 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
372 *
373 * @adev: amdgpu_device pointer
374 * @id: VMID structure
375 *
376 * Check if GPU reset occured since last use of the VMID.
377 */
378static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
379 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800380{
381 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200382 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800383}
384
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800385static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
386{
387 return !!vm->reserved_vmid[vmhub];
388}
389
390/* idr_mgr->lock must be held */
391static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
392 struct amdgpu_ring *ring,
393 struct amdgpu_sync *sync,
394 struct dma_fence *fence,
395 struct amdgpu_job *job)
396{
397 struct amdgpu_device *adev = ring->adev;
398 unsigned vmhub = ring->funcs->vmhub;
399 uint64_t fence_context = adev->fence_context + ring->idx;
400 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
401 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
402 struct dma_fence *updates = sync->last_vm_update;
403 int r = 0;
404 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200405 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800406
407 flushed = id->flushed_updates;
408 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
409 (atomic64_read(&id->owner) != vm->client_id) ||
410 (job->vm_pd_addr != id->pd_gpu_addr) ||
411 (updates && (!flushed || updates->context != flushed->context ||
412 dma_fence_is_later(updates, flushed))) ||
413 (!id->last_flush || (id->last_flush->context != fence_context &&
414 !dma_fence_is_signaled(id->last_flush)))) {
415 needs_flush = true;
416 /* to prevent one context starved by another context */
417 id->pd_gpu_addr = 0;
418 tmp = amdgpu_sync_peek_fence(&id->active, ring);
419 if (tmp) {
420 r = amdgpu_sync_fence(adev, sync, tmp);
421 return r;
422 }
423 }
424
425 /* Good we can use this VMID. Remember this submission as
426 * user of the VMID.
427 */
428 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
429 if (r)
430 goto out;
431
432 if (updates && (!flushed || updates->context != flushed->context ||
433 dma_fence_is_later(updates, flushed))) {
434 dma_fence_put(id->flushed_updates);
435 id->flushed_updates = dma_fence_get(updates);
436 }
437 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800438 atomic64_set(&id->owner, vm->client_id);
439 job->vm_needs_flush = needs_flush;
440 if (needs_flush) {
441 dma_fence_put(id->last_flush);
442 id->last_flush = NULL;
443 }
444 job->vm_id = id - id_mgr->ids;
445 trace_amdgpu_vm_grab_id(vm, ring, job);
446out:
447 return r;
448}
449
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450/**
451 * amdgpu_vm_grab_id - allocate the next free VMID
452 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200454 * @ring: ring we want to submit job to
455 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100456 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457 *
Christian König7f8a5292015-07-20 16:09:40 +0200458 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459 */
Christian König7f8a5292015-07-20 16:09:40 +0200460int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100461 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800462 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200465 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200466 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200467 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100468 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200469 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100470 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200471 unsigned i;
472 int r = 0;
473
Christian König76456702017-04-06 17:52:39 +0200474 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800475 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
476 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
477 mutex_unlock(&id_mgr->lock);
478 return r;
479 }
480 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
481 if (!fences) {
482 mutex_unlock(&id_mgr->lock);
483 return -ENOMEM;
484 }
Christian König36fd7c52016-05-23 15:30:08 +0200485 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200486 i = 0;
Christian König76456702017-04-06 17:52:39 +0200487 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200488 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
489 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200490 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200491 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200492 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100493
Christian König1fbb2e92016-06-01 10:47:36 +0200494 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200495 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200496 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
497 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100498 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200499 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200500
Christian König1fbb2e92016-06-01 10:47:36 +0200501 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100502 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200503
Chris Wilsonf54d1862016-10-25 13:00:45 +0100504 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200505 seqno, true);
506 if (!array) {
507 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100508 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200509 kfree(fences);
510 r = -ENOMEM;
511 goto error;
512 }
Christian König8d76001e2016-05-23 16:00:32 +0200513
Christian König8d76001e2016-05-23 16:00:32 +0200514
Christian König1fbb2e92016-06-01 10:47:36 +0200515 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100516 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200517 if (r)
518 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200519
Christian König76456702017-04-06 17:52:39 +0200520 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200521 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200522
Christian König1fbb2e92016-06-01 10:47:36 +0200523 }
524 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200525
Christian König6f1ceab2017-07-11 16:59:21 +0200526 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200527 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200528 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100529 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200530 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200531
Christian König1fbb2e92016-06-01 10:47:36 +0200532 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200533 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800534 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200535
536 if (atomic64_read(&id->owner) != vm->client_id)
537 continue;
538
Chunming Zhoufd53be32016-07-01 17:59:01 +0800539 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200540 continue;
541
Christian König87c910d2017-03-30 16:56:20 +0200542 if (!id->last_flush ||
543 (id->last_flush->context != fence_context &&
544 !dma_fence_is_signaled(id->last_flush)))
545 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200546
547 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200548 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
549 needs_flush = true;
550
551 /* Concurrent flushes are only possible starting with Vega10 */
552 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200553 continue;
554
Christian König3dab83b2016-06-01 13:31:17 +0200555 /* Good we can use this VMID. Remember this submission as
556 * user of the VMID.
557 */
Christian König1fbb2e92016-06-01 10:47:36 +0200558 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
559 if (r)
560 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200561
Christian König87c910d2017-03-30 16:56:20 +0200562 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
563 dma_fence_put(id->flushed_updates);
564 id->flushed_updates = dma_fence_get(updates);
565 }
Christian König8d76001e2016-05-23 16:00:32 +0200566
Christian König87c910d2017-03-30 16:56:20 +0200567 if (needs_flush)
568 goto needs_flush;
569 else
570 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200571
Christian König4f618e72017-04-06 15:18:21 +0200572 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800573
Christian König1fbb2e92016-06-01 10:47:36 +0200574 /* Still no ID to use? Then use the idle one found earlier */
575 id = idle;
576
577 /* Remember this submission as user of the VMID */
578 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100579 if (r)
580 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100581
Christian König87c910d2017-03-30 16:56:20 +0200582 id->pd_gpu_addr = job->vm_pd_addr;
583 dma_fence_put(id->flushed_updates);
584 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200585 atomic64_set(&id->owner, vm->client_id);
586
587needs_flush:
588 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100589 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100590 id->last_flush = NULL;
591
Christian König87c910d2017-03-30 16:56:20 +0200592no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200593 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594
Christian König76456702017-04-06 17:52:39 +0200595 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200596 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100597
598error:
Christian König76456702017-04-06 17:52:39 +0200599 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100600 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601}
602
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800603static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
604 struct amdgpu_vm *vm,
605 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400606{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800607 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400608
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800609 mutex_lock(&id_mgr->lock);
610 if (vm->reserved_vmid[vmhub]) {
611 list_add(&vm->reserved_vmid[vmhub]->list,
612 &id_mgr->ids_lru);
613 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800614 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400615 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800616 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400617}
618
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800619static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
620 struct amdgpu_vm *vm,
621 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500622{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800623 struct amdgpu_vm_id_manager *id_mgr;
624 struct amdgpu_vm_id *idle;
625 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500626
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800627 id_mgr = &adev->vm_manager.id_mgr[vmhub];
628 mutex_lock(&id_mgr->lock);
629 if (vm->reserved_vmid[vmhub])
630 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800631 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
632 AMDGPU_VM_MAX_RESERVED_VMID) {
633 DRM_ERROR("Over limitation of reserved vmid\n");
634 atomic_dec(&id_mgr->reserved_vmid_num);
635 r = -EINVAL;
636 goto unlock;
637 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800638 /* Select the first entry VMID */
639 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
640 list_del_init(&idle->list);
641 vm->reserved_vmid[vmhub] = idle;
642 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500643
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800644 return 0;
645unlock:
646 mutex_unlock(&id_mgr->lock);
647 return r;
648}
649
Alex Xiee59c0202017-06-01 09:42:59 -0400650/**
651 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
652 *
653 * @adev: amdgpu_device pointer
654 */
655void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
656{
657 const struct amdgpu_ip_block *ip_block;
658 bool has_compute_vm_bug;
659 struct amdgpu_ring *ring;
660 int i;
661
662 has_compute_vm_bug = false;
663
664 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
665 if (ip_block) {
666 /* Compute has a VM bug for GFX version < 7.
667 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
668 if (ip_block->version->major <= 7)
669 has_compute_vm_bug = true;
670 else if (ip_block->version->major == 8)
671 if (adev->gfx.mec_fw_version < 673)
672 has_compute_vm_bug = true;
673 }
674
675 for (i = 0; i < adev->num_rings; i++) {
676 ring = adev->rings[i];
677 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
678 /* only compute rings */
679 ring->has_compute_vm_bug = has_compute_vm_bug;
680 else
681 ring->has_compute_vm_bug = false;
682 }
683}
684
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400685bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
686 struct amdgpu_job *job)
687{
688 struct amdgpu_device *adev = ring->adev;
689 unsigned vmhub = ring->funcs->vmhub;
690 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
691 struct amdgpu_vm_id *id;
692 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400693 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400694
695 if (job->vm_id == 0)
696 return false;
697 id = &id_mgr->ids[job->vm_id];
698 gds_switch_needed = ring->funcs->emit_gds_switch && (
699 id->gds_base != job->gds_base ||
700 id->gds_size != job->gds_size ||
701 id->gws_base != job->gws_base ||
702 id->gws_size != job->gws_size ||
703 id->oa_base != job->oa_base ||
704 id->oa_size != job->oa_size);
705
706 if (amdgpu_vm_had_gpu_reset(adev, id))
707 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400708
709 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400710}
711
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400712static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
713{
714 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500715}
716
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717/**
718 * amdgpu_vm_flush - hardware flush the vm
719 *
720 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100721 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100722 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 *
Christian König4ff37a82016-02-26 16:18:26 +0100724 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400725 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800726int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727{
Christian König971fe9a92016-03-01 15:09:25 +0100728 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200729 unsigned vmhub = ring->funcs->vmhub;
730 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
731 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100732 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800733 id->gds_base != job->gds_base ||
734 id->gds_size != job->gds_size ||
735 id->gws_base != job->gws_base ||
736 id->gws_size != job->gws_size ||
737 id->oa_base != job->oa_base ||
738 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800739 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200740 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100741 int r;
Christian Königd564a062016-03-01 15:51:53 +0100742
Christian Königf7d015b2017-04-03 14:28:26 +0200743 if (amdgpu_vm_had_gpu_reset(adev, id)) {
744 gds_switch_needed = true;
745 vm_flush_needed = true;
746 }
Christian König971fe9a92016-03-01 15:09:25 +0100747
Monk Liu8fdf0742017-06-06 17:25:13 +0800748 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200749 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100750
Christian Königc0e51932017-04-03 14:16:07 +0200751 if (ring->funcs->init_cond_exec)
752 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100753
Monk Liu8fdf0742017-06-06 17:25:13 +0800754 if (need_pipe_sync)
755 amdgpu_ring_emit_pipeline_sync(ring);
756
Christian Königf7d015b2017-04-03 14:28:26 +0200757 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200758 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800759
Christian König9a94f5a2017-05-12 14:46:23 +0200760 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
761 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800762
Christian Königc0e51932017-04-03 14:16:07 +0200763 r = amdgpu_fence_emit(ring, &fence);
764 if (r)
765 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800766
Christian König76456702017-04-06 17:52:39 +0200767 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200768 dma_fence_put(id->last_flush);
769 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800770 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200771 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200772 }
Monk Liue9d672b2017-03-15 12:18:57 +0800773
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800774 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200775 id->gds_base = job->gds_base;
776 id->gds_size = job->gds_size;
777 id->gws_base = job->gws_base;
778 id->gws_size = job->gws_size;
779 id->oa_base = job->oa_base;
780 id->oa_size = job->oa_size;
781 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
782 job->gds_size, job->gws_base,
783 job->gws_size, job->oa_base,
784 job->oa_size);
785 }
786
787 if (ring->funcs->patch_cond_exec)
788 amdgpu_ring_patch_cond_exec(ring, patch_offset);
789
790 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
791 if (ring->funcs->emit_switch_buffer) {
792 amdgpu_ring_emit_switch_buffer(ring);
793 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794 }
Christian König41d9eb22016-03-01 16:46:18 +0100795 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100796}
797
798/**
799 * amdgpu_vm_reset_id - reset VMID to zero
800 *
801 * @adev: amdgpu device structure
802 * @vm_id: vmid number to use
803 *
804 * Reset saved GDW, GWS and OA to force switch on next flush.
805 */
Christian König76456702017-04-06 17:52:39 +0200806void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
807 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100808{
Christian König76456702017-04-06 17:52:39 +0200809 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
810 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100811
Christian Königb3c85a02017-05-10 20:06:58 +0200812 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100813 id->gds_base = 0;
814 id->gds_size = 0;
815 id->gws_base = 0;
816 id->gws_size = 0;
817 id->oa_base = 0;
818 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819}
820
821/**
Christian Königb3c85a02017-05-10 20:06:58 +0200822 * amdgpu_vm_reset_all_id - reset VMID to zero
823 *
824 * @adev: amdgpu device structure
825 *
826 * Reset VMID to force flush on next use
827 */
828void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
829{
830 unsigned i, j;
831
832 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
833 struct amdgpu_vm_id_manager *id_mgr =
834 &adev->vm_manager.id_mgr[i];
835
836 for (j = 1; j < id_mgr->num_ids; ++j)
837 amdgpu_vm_reset_id(adev, i, j);
838 }
839}
840
841/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
843 *
844 * @vm: requested vm
845 * @bo: requested buffer object
846 *
Christian König8843dbb2016-01-26 12:17:11 +0100847 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 * Search inside the @bos vm list for the requested vm
849 * Returns the found bo_va or NULL if none is found
850 *
851 * Object has to be reserved!
852 */
853struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
854 struct amdgpu_bo *bo)
855{
856 struct amdgpu_bo_va *bo_va;
857
Christian Königec681542017-08-01 10:51:43 +0200858 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
859 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400860 return bo_va;
861 }
862 }
863 return NULL;
864}
865
866/**
Christian Königafef8b82016-08-12 13:29:18 +0200867 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400868 *
Christian König29efc4f2016-08-04 14:52:50 +0200869 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400870 * @pe: addr of the page entry
871 * @addr: dst addr to write into pe
872 * @count: number of page entries to update
873 * @incr: increase next addr by incr bytes
874 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 *
876 * Traces the parameters and calls the right asic functions
877 * to setup the page table using the DMA.
878 */
Christian Königafef8b82016-08-12 13:29:18 +0200879static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
880 uint64_t pe, uint64_t addr,
881 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800882 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400883{
Christian Königec2f05f2016-09-25 16:11:52 +0200884 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885
Christian Königafef8b82016-08-12 13:29:18 +0200886 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200887 amdgpu_vm_write_pte(params->adev, params->ib, pe,
888 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889
890 } else {
Christian König27c5f362016-08-04 15:02:49 +0200891 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892 count, incr, flags);
893 }
894}
895
896/**
Christian Königafef8b82016-08-12 13:29:18 +0200897 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
898 *
899 * @params: see amdgpu_pte_update_params definition
900 * @pe: addr of the page entry
901 * @addr: dst addr to write into pe
902 * @count: number of page entries to update
903 * @incr: increase next addr by incr bytes
904 * @flags: hw access flags
905 *
906 * Traces the parameters and calls the DMA function to copy the PTEs.
907 */
908static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
909 uint64_t pe, uint64_t addr,
910 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800911 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200912{
Christian Königec2f05f2016-09-25 16:11:52 +0200913 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200914
Christian Königec2f05f2016-09-25 16:11:52 +0200915
916 trace_amdgpu_vm_copy_ptes(pe, src, count);
917
918 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200919}
920
921/**
Christian Königb07c9d22015-11-30 13:26:07 +0100922 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400923 *
Christian Königb07c9d22015-11-30 13:26:07 +0100924 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925 * @addr: the unmapped addr
926 *
927 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100928 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200930static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931{
932 uint64_t result;
933
Christian Königde9ea7b2016-08-12 11:33:30 +0200934 /* page table offset */
935 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936
Christian Königde9ea7b2016-08-12 11:33:30 +0200937 /* in case cpu page size != gpu page size*/
938 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100939
940 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400941
942 return result;
943}
944
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400945/**
946 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
947 *
948 * @params: see amdgpu_pte_update_params definition
949 * @pe: kmap addr of the page entry
950 * @addr: dst addr to write into pe
951 * @count: number of page entries to update
952 * @incr: increase next addr by incr bytes
953 * @flags: hw access flags
954 *
955 * Write count number of PT/PD entries directly.
956 */
957static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
958 uint64_t pe, uint64_t addr,
959 unsigned count, uint32_t incr,
960 uint64_t flags)
961{
962 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400963 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400964
Christian König03918b32017-07-11 17:15:37 +0200965 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
966
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400967 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400968 value = params->pages_addr ?
969 amdgpu_vm_map_gart(params->pages_addr, addr) :
970 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -0400971 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400972 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400973 addr += incr;
974 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400975}
976
Christian Königa33cab72017-07-11 17:13:00 +0200977static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
978 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400979{
980 struct amdgpu_sync sync;
981 int r;
982
983 amdgpu_sync_create(&sync);
Christian König3f3333f2017-08-03 14:02:13 +0200984 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400985 r = amdgpu_sync_wait(&sync, true);
986 amdgpu_sync_free(&sync);
987
988 return r;
989}
990
Christian Königf8991ba2016-09-16 15:36:49 +0200991/*
Christian König194d2162016-10-12 15:13:52 +0200992 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200993 *
994 * @adev: amdgpu_device pointer
995 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200996 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +0200997 *
Christian König194d2162016-10-12 15:13:52 +0200998 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200999 * Returns 0 for success, error for failure.
1000 */
Christian König194d2162016-10-12 15:13:52 +02001001static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1002 struct amdgpu_vm *vm,
1003 struct amdgpu_vm_pt *parent,
1004 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005{
Christian Königf8991ba2016-09-16 15:36:49 +02001006 struct amdgpu_bo *shadow;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001007 struct amdgpu_ring *ring = NULL;
1008 uint64_t pd_addr, shadow_addr = 0;
Christian König194d2162016-10-12 15:13:52 +02001009 uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
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;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001015
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016 int r;
1017
Christian König194d2162016-10-12 15:13:52 +02001018 if (!parent->entries)
1019 return 0;
Christian Königd71518b2016-02-01 12:20:25 +01001020
Christian König27c5f362016-08-04 15:02:49 +02001021 memset(&params, 0, sizeof(params));
1022 params.adev = adev;
Christian König3f3333f2017-08-03 14:02:13 +02001023 shadow = parent->base.bo->shadow;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001024
Alex Deucher69277982017-07-13 15:37:11 -04001025 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02001026 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Christian Königa33cab72017-07-11 17:13:00 +02001027 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001028 if (unlikely(r))
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001029 return r;
Christian König0a096fb2017-07-12 10:01:48 +02001030
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001031 params.func = amdgpu_vm_cpu_set_ptes;
1032 } else {
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001033 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1034 sched);
1035
1036 /* padding, etc. */
1037 ndw = 64;
1038
1039 /* assume the worst case */
1040 ndw += parent->last_entry_used * 6;
1041
Christian König3f3333f2017-08-03 14:02:13 +02001042 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001043
1044 if (shadow) {
1045 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1046 ndw *= 2;
1047 } else {
1048 shadow_addr = 0;
1049 }
1050
1051 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1052 if (r)
1053 return r;
1054
1055 params.ib = &job->ibs[0];
1056 params.func = amdgpu_vm_do_set_ptes;
1057 }
1058
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001059
Christian König194d2162016-10-12 15:13:52 +02001060 /* walk over the address space and update the directory */
1061 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +02001062 struct amdgpu_bo *bo = parent->entries[pt_idx].base.bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001063 uint64_t pde, pt;
1064
1065 if (bo == NULL)
1066 continue;
1067
1068 pt = amdgpu_bo_gpu_offset(bo);
Christian König53e2e912017-05-15 15:19:10 +02001069 pt = amdgpu_gart_get_vm_pde(adev, pt);
Christian König4ab40162017-08-03 20:30:50 +02001070 /* Don't update huge pages here */
1071 if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
1072 parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
Christian Königf8991ba2016-09-16 15:36:49 +02001073 continue;
1074
Christian König4ab40162017-08-03 20:30:50 +02001075 parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076
1077 pde = pd_addr + pt_idx * 8;
1078 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001079 ((last_pt + incr * count) != pt) ||
1080 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081
1082 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +02001083 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001084 params.func(&params,
1085 last_shadow,
1086 last_pt, count,
1087 incr,
1088 AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001089
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001090 params.func(&params, last_pde,
1091 last_pt, count, incr,
1092 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001093 }
1094
1095 count = 1;
1096 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001097 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098 last_pt = pt;
1099 } else {
1100 ++count;
1101 }
1102 }
1103
Christian Königf8991ba2016-09-16 15:36:49 +02001104 if (count) {
Christian König3f3333f2017-08-03 14:02:13 +02001105 if (vm->root.base.bo->shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001106 params.func(&params, last_shadow, last_pt,
1107 count, incr, AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001108
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001109 params.func(&params, last_pde, last_pt,
1110 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001111 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001112
Christian König0a096fb2017-07-12 10:01:48 +02001113 if (!vm->use_cpu_for_update) {
1114 if (params.ib->length_dw == 0) {
1115 amdgpu_job_free(job);
1116 } else {
1117 amdgpu_ring_pad_ib(ring, params.ib);
Christian König3f3333f2017-08-03 14:02:13 +02001118 amdgpu_sync_resv(adev, &job->sync,
1119 parent->base.bo->tbo.resv,
Christian König194d2162016-10-12 15:13:52 +02001120 AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001121 if (shadow)
1122 amdgpu_sync_resv(adev, &job->sync,
1123 shadow->tbo.resv,
1124 AMDGPU_FENCE_OWNER_VM);
Christian Königf8991ba2016-09-16 15:36:49 +02001125
Christian König0a096fb2017-07-12 10:01:48 +02001126 WARN_ON(params.ib->length_dw > ndw);
1127 r = amdgpu_job_submit(job, ring, &vm->entity,
1128 AMDGPU_FENCE_OWNER_VM, &fence);
1129 if (r)
1130 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001131
Christian König3f3333f2017-08-03 14:02:13 +02001132 amdgpu_bo_fence(parent->base.bo, fence, true);
Christian König0a096fb2017-07-12 10:01:48 +02001133 dma_fence_put(vm->last_dir_update);
1134 vm->last_dir_update = dma_fence_get(fence);
1135 dma_fence_put(fence);
1136 }
Christian König194d2162016-10-12 15:13:52 +02001137 }
1138 /*
1139 * Recurse into the subdirectories. This recursion is harmless because
1140 * we only have a maximum of 5 layers.
1141 */
1142 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1143 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1144
Christian König3f3333f2017-08-03 14:02:13 +02001145 if (!entry->base.bo)
Christian König194d2162016-10-12 15:13:52 +02001146 continue;
1147
1148 r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
1149 if (r)
1150 return r;
1151 }
Christian Königf8991ba2016-09-16 15:36:49 +02001152
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001154
1155error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001156 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001157 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001158}
1159
Christian König194d2162016-10-12 15:13:52 +02001160/*
Christian König92456b92017-05-12 16:09:26 +02001161 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1162 *
1163 * @parent: parent PD
1164 *
1165 * Mark all PD level as invalid after an error.
1166 */
1167static void amdgpu_vm_invalidate_level(struct amdgpu_vm_pt *parent)
1168{
1169 unsigned pt_idx;
1170
1171 /*
1172 * Recurse into the subdirectories. This recursion is harmless because
1173 * we only have a maximum of 5 layers.
1174 */
1175 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1176 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1177
Christian König3f3333f2017-08-03 14:02:13 +02001178 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001179 continue;
1180
1181 entry->addr = ~0ULL;
1182 amdgpu_vm_invalidate_level(entry);
1183 }
1184}
1185
1186/*
Christian König194d2162016-10-12 15:13:52 +02001187 * amdgpu_vm_update_directories - make sure that all directories are valid
1188 *
1189 * @adev: amdgpu_device pointer
1190 * @vm: requested vm
1191 *
1192 * Makes sure all directories are up to date.
1193 * Returns 0 for success, error for failure.
1194 */
1195int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1196 struct amdgpu_vm *vm)
1197{
Christian König92456b92017-05-12 16:09:26 +02001198 int r;
1199
1200 r = amdgpu_vm_update_level(adev, vm, &vm->root, 0);
1201 if (r)
1202 amdgpu_vm_invalidate_level(&vm->root);
1203
Christian König68c62302017-07-11 17:23:29 +02001204 if (vm->use_cpu_for_update) {
1205 /* Flush HDP */
1206 mb();
1207 amdgpu_gart_flush_gpu_tlb(adev, 0);
1208 }
1209
Christian König92456b92017-05-12 16:09:26 +02001210 return r;
Christian König194d2162016-10-12 15:13:52 +02001211}
1212
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001214 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001215 *
1216 * @p: see amdgpu_pte_update_params definition
1217 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001218 * @entry: resulting entry or NULL
1219 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001220 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001221 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001222 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001223void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1224 struct amdgpu_vm_pt **entry,
1225 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001226{
Christian König4e2cb642016-10-25 15:52:28 +02001227 unsigned idx, level = p->adev->vm_manager.num_level;
1228
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001229 *parent = NULL;
1230 *entry = &p->vm->root;
1231 while ((*entry)->entries) {
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001232 idx = addr >> (p->adev->vm_manager.block_size * level--);
Christian König3f3333f2017-08-03 14:02:13 +02001233 idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001234 *parent = *entry;
1235 *entry = &(*entry)->entries[idx];
Christian König4e2cb642016-10-25 15:52:28 +02001236 }
1237
1238 if (level)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001239 *entry = NULL;
1240}
Christian König4e2cb642016-10-25 15:52:28 +02001241
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001242/**
1243 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1244 *
1245 * @p: see amdgpu_pte_update_params definition
1246 * @entry: vm_pt entry to check
1247 * @parent: parent entry
1248 * @nptes: number of PTEs updated with this operation
1249 * @dst: destination address where the PTEs should point to
1250 * @flags: access flags fro the PTEs
1251 *
1252 * Check if we can update the PD with a huge page.
1253 */
Christian Königec5207c2017-08-03 19:24:06 +02001254static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1255 struct amdgpu_vm_pt *entry,
1256 struct amdgpu_vm_pt *parent,
1257 unsigned nptes, uint64_t dst,
1258 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001259{
1260 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1261 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001262
1263 /* In the case of a mixed PT the PDE must point to it*/
1264 if (p->adev->asic_type < CHIP_VEGA10 ||
1265 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehling38a87912017-08-17 16:37:49 -04001266 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001267 !(flags & AMDGPU_PTE_VALID)) {
1268
Christian König3f3333f2017-08-03 14:02:13 +02001269 dst = amdgpu_bo_gpu_offset(entry->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001270 dst = amdgpu_gart_get_vm_pde(p->adev, dst);
1271 flags = AMDGPU_PTE_VALID;
1272 } else {
Christian König4ab40162017-08-03 20:30:50 +02001273 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001274 flags |= AMDGPU_PDE_PTE;
1275 }
1276
Christian König4ab40162017-08-03 20:30:50 +02001277 if (entry->addr == (dst | flags))
Christian Königec5207c2017-08-03 19:24:06 +02001278 return;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001279
Christian König4ab40162017-08-03 20:30:50 +02001280 entry->addr = (dst | flags);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001281
1282 if (use_cpu_update) {
Felix Kuehling38a87912017-08-17 16:37:49 -04001283 /* In case a huge page is replaced with a system
1284 * memory mapping, p->pages_addr != NULL and
1285 * amdgpu_vm_cpu_set_ptes would try to translate dst
1286 * through amdgpu_vm_map_gart. But dst is already a
1287 * GPU address (of the page table). Disable
1288 * amdgpu_vm_map_gart temporarily.
1289 */
1290 dma_addr_t *tmp;
1291
1292 tmp = p->pages_addr;
1293 p->pages_addr = NULL;
1294
Christian König3f3333f2017-08-03 14:02:13 +02001295 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001296 pde = pd_addr + (entry - parent->entries) * 8;
1297 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehling38a87912017-08-17 16:37:49 -04001298
1299 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001300 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001301 if (parent->base.bo->shadow) {
1302 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001303 pde = pd_addr + (entry - parent->entries) * 8;
1304 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1305 }
Christian König3f3333f2017-08-03 14:02:13 +02001306 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001307 pde = pd_addr + (entry - parent->entries) * 8;
1308 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1309 }
Christian König4e2cb642016-10-25 15:52:28 +02001310}
1311
1312/**
Christian König92696dd2016-08-05 13:56:35 +02001313 * amdgpu_vm_update_ptes - make sure that page tables are valid
1314 *
1315 * @params: see amdgpu_pte_update_params definition
1316 * @vm: requested vm
1317 * @start: start of GPU address range
1318 * @end: end of GPU address range
1319 * @dst: destination address to map to, the next dst inside the function
1320 * @flags: mapping flags
1321 *
1322 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001323 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001324 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001325static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001326 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001327 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001328{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001329 struct amdgpu_device *adev = params->adev;
1330 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001331
Christian König301654a2017-05-16 14:30:27 +02001332 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001333 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001334 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001335 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001336
1337 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001338 for (addr = start; addr < end; addr += nptes,
1339 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1340 struct amdgpu_vm_pt *entry, *parent;
1341
1342 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1343 if (!entry)
1344 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001345
Christian König92696dd2016-08-05 13:56:35 +02001346 if ((addr & ~mask) == (end & ~mask))
1347 nptes = end - addr;
1348 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001349 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001350
Christian Königec5207c2017-08-03 19:24:06 +02001351 amdgpu_vm_handle_huge_pages(params, entry, parent,
1352 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001353 /* We don't need to update PTEs for huge pages */
1354 if (entry->addr & AMDGPU_PDE_PTE)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001355 continue;
1356
Christian König3f3333f2017-08-03 14:02:13 +02001357 pt = entry->base.bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001358 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001359 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001360 } else {
1361 if (pt->shadow) {
1362 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1363 pe_start += (addr & mask) * 8;
1364 params->func(params, pe_start, dst, nptes,
1365 AMDGPU_GPU_PAGE_SIZE, flags);
1366 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001367 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001368 }
Christian König92696dd2016-08-05 13:56:35 +02001369
Christian König301654a2017-05-16 14:30:27 +02001370 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001371 params->func(params, pe_start, dst, nptes,
1372 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001373 }
1374
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001375 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001376}
1377
1378/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1380 *
Christian König29efc4f2016-08-04 14:52:50 +02001381 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001382 * @vm: requested vm
1383 * @start: first PTE to handle
1384 * @end: last PTE to handle
1385 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001387 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001388 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001389static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001390 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001391 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392{
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001393 int r;
1394
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001395 /**
1396 * The MC L1 TLB supports variable sized pages, based on a fragment
1397 * field in the PTE. When this field is set to a non-zero value, page
1398 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1399 * flags are considered valid for all PTEs within the fragment range
1400 * and corresponding mappings are assumed to be physically contiguous.
1401 *
1402 * The L1 TLB can store a single PTE for the whole fragment,
1403 * significantly increasing the space available for translation
1404 * caching. This leads to large improvements in throughput when the
1405 * TLB is under pressure.
1406 *
1407 * The L2 TLB distributes small and large fragments into two
1408 * asymmetric partitions. The large fragment cache is significantly
1409 * larger. Thus, we try to use large fragments wherever possible.
1410 * Userspace can support this by aligning virtual base address and
1411 * allocation size to the fragment size.
1412 */
Roger Hee618d302017-08-11 20:00:41 +08001413 unsigned pages_per_frag = params->adev->vm_manager.fragment_size;
Christian König6be7adb2017-05-23 18:35:22 +02001414 uint64_t frag_flags = AMDGPU_PTE_FRAG(pages_per_frag);
1415 uint64_t frag_align = 1 << pages_per_frag;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001416
Christian König92696dd2016-08-05 13:56:35 +02001417 uint64_t frag_start = ALIGN(start, frag_align);
1418 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +01001419
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +02001421 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001422 (frag_start >= frag_end))
1423 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001424
1425 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +02001426 if (start != frag_start) {
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001427 r = amdgpu_vm_update_ptes(params, start, frag_start,
1428 dst, flags);
1429 if (r)
1430 return r;
Christian König92696dd2016-08-05 13:56:35 +02001431 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 }
1433
1434 /* handle the area in the middle */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001435 r = amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
1436 flags | frag_flags);
1437 if (r)
1438 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001439
1440 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +02001441 if (frag_end != end) {
1442 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001443 r = amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 }
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001445 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001446}
1447
1448/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001449 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1450 *
1451 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001452 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001453 * @src: address where to copy page table entries from
1454 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001455 * @vm: requested vm
1456 * @start: start of mapped range
1457 * @last: last mapped entry
1458 * @flags: flags for the entries
1459 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001460 * @fence: optional resulting fence
1461 *
Christian Königa14faa62016-01-25 14:27:31 +01001462 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001463 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001464 */
1465static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001466 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001467 uint64_t src,
1468 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001469 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001470 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001471 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001472 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001473{
Christian König2d55e452016-02-08 17:37:38 +01001474 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001475 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001477 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001478 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001479 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480 int r;
1481
Christian Königafef8b82016-08-12 13:29:18 +02001482 memset(&params, 0, sizeof(params));
1483 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001484 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001485 params.src = src;
1486
Christian Königa33cab72017-07-11 17:13:00 +02001487 /* sync to everything on unmapping */
1488 if (!(flags & AMDGPU_PTE_VALID))
1489 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1490
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001491 if (vm->use_cpu_for_update) {
1492 /* params.src is used as flag to indicate system Memory */
1493 if (pages_addr)
1494 params.src = ~0;
1495
1496 /* Wait for PT BOs to be free. PTs share the same resv. object
1497 * as the root PD BO
1498 */
Christian Königa33cab72017-07-11 17:13:00 +02001499 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001500 if (unlikely(r))
1501 return r;
1502
1503 params.func = amdgpu_vm_cpu_set_ptes;
1504 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001505 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1506 addr, flags);
1507 }
1508
Christian König2d55e452016-02-08 17:37:38 +01001509 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001510
Christian Königa14faa62016-01-25 14:27:31 +01001511 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001512
1513 /*
1514 * reserve space for one command every (1 << BLOCK_SIZE)
1515 * entries or 2k dwords (whatever is smaller)
1516 */
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001517 ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001518
1519 /* padding, etc. */
1520 ndw = 64;
1521
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001522 /* one PDE write for each huge page */
1523 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1524
Christian Königb0456f92016-08-11 14:06:54 +02001525 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001526 /* only copy commands needed */
1527 ndw += ncmds * 7;
1528
Christian Königafef8b82016-08-12 13:29:18 +02001529 params.func = amdgpu_vm_do_copy_ptes;
1530
Christian Königb0456f92016-08-11 14:06:54 +02001531 } else if (pages_addr) {
1532 /* copy commands needed */
1533 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001534
Christian Königb0456f92016-08-11 14:06:54 +02001535 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536 ndw += nptes * 2;
1537
Christian Königafef8b82016-08-12 13:29:18 +02001538 params.func = amdgpu_vm_do_copy_ptes;
1539
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001540 } else {
1541 /* set page commands needed */
1542 ndw += ncmds * 10;
1543
1544 /* two extra commands for begin/end of fragment */
1545 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001546
1547 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001548 }
1549
Christian Königd71518b2016-02-01 12:20:25 +01001550 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1551 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001553
Christian König29efc4f2016-08-04 14:52:50 +02001554 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001555
Christian Königb0456f92016-08-11 14:06:54 +02001556 if (!src && pages_addr) {
1557 uint64_t *pte;
1558 unsigned i;
1559
1560 /* Put the PTEs at the end of the IB. */
1561 i = ndw - nptes * 2;
1562 pte= (uint64_t *)&(job->ibs->ptr[i]);
1563 params.src = job->ibs->gpu_addr + i * 4;
1564
1565 for (i = 0; i < nptes; ++i) {
1566 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1567 AMDGPU_GPU_PAGE_SIZE);
1568 pte[i] |= flags;
1569 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001570 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001571 }
1572
Christian König3cabaa52016-06-06 10:17:58 +02001573 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1574 if (r)
1575 goto error_free;
1576
Christian König3f3333f2017-08-03 14:02:13 +02001577 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001578 owner);
1579 if (r)
1580 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001581
Christian König3f3333f2017-08-03 14:02:13 +02001582 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001583 if (r)
1584 goto error_free;
1585
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001586 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1587 if (r)
1588 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589
Christian König29efc4f2016-08-04 14:52:50 +02001590 amdgpu_ring_pad_ib(ring, params.ib);
1591 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001592 r = amdgpu_job_submit(job, ring, &vm->entity,
1593 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001594 if (r)
1595 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596
Christian König3f3333f2017-08-03 14:02:13 +02001597 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001598 dma_fence_put(*fence);
1599 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001600 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001601
1602error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001603 amdgpu_job_free(job);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001604 amdgpu_vm_invalidate_level(&vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001605 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606}
1607
1608/**
Christian Königa14faa62016-01-25 14:27:31 +01001609 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1610 *
1611 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001612 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001613 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001614 * @vm: requested vm
1615 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001616 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001617 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001618 * @fence: optional resulting fence
1619 *
1620 * Split the mapping into smaller chunks so that each update fits
1621 * into a SDMA IB.
1622 * Returns 0 for success, -EINVAL for failure.
1623 */
1624static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001625 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001626 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001627 struct amdgpu_vm *vm,
1628 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001629 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001630 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001631 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001632{
Christian Königa9f87f62017-03-30 14:03:59 +02001633 uint64_t pfn, src = 0, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001634 int r;
1635
1636 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1637 * but in case of something, we filter the flags in first place
1638 */
1639 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1640 flags &= ~AMDGPU_PTE_READABLE;
1641 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1642 flags &= ~AMDGPU_PTE_WRITEABLE;
1643
Alex Xie15b31c52017-03-03 16:47:11 -05001644 flags &= ~AMDGPU_PTE_EXECUTABLE;
1645 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1646
Alex Xieb0fd18b2017-03-03 16:49:39 -05001647 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1648 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1649
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001650 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1651 (adev->asic_type >= CHIP_VEGA10)) {
1652 flags |= AMDGPU_PTE_PRT;
1653 flags &= ~AMDGPU_PTE_VALID;
1654 }
1655
Christian Königa14faa62016-01-25 14:27:31 +01001656 trace_amdgpu_vm_bo_update(mapping);
1657
Christian König63e0ba42016-08-16 17:38:37 +02001658 pfn = mapping->offset >> PAGE_SHIFT;
1659 if (nodes) {
1660 while (pfn >= nodes->size) {
1661 pfn -= nodes->size;
1662 ++nodes;
1663 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001664 }
Christian Königa14faa62016-01-25 14:27:31 +01001665
Christian König63e0ba42016-08-16 17:38:37 +02001666 do {
1667 uint64_t max_entries;
1668 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001669
Christian König63e0ba42016-08-16 17:38:37 +02001670 if (nodes) {
1671 addr = nodes->start << PAGE_SHIFT;
1672 max_entries = (nodes->size - pfn) *
1673 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1674 } else {
1675 addr = 0;
1676 max_entries = S64_MAX;
1677 }
Christian Königa14faa62016-01-25 14:27:31 +01001678
Christian König63e0ba42016-08-16 17:38:37 +02001679 if (pages_addr) {
Christian Königfebb84a2017-08-22 12:50:46 +02001680 max_entries = min(max_entries, 16ull * 1024ull);
Christian König63e0ba42016-08-16 17:38:37 +02001681 addr = 0;
1682 } else if (flags & AMDGPU_PTE_VALID) {
1683 addr += adev->vm_manager.vram_base_offset;
1684 }
1685 addr += pfn << PAGE_SHIFT;
1686
Christian Königa9f87f62017-03-30 14:03:59 +02001687 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001688 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1689 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001690 start, last, flags, addr,
1691 fence);
1692 if (r)
1693 return r;
1694
Christian König63e0ba42016-08-16 17:38:37 +02001695 pfn += last - start + 1;
1696 if (nodes && nodes->size == pfn) {
1697 pfn = 0;
1698 ++nodes;
1699 }
Christian Königa14faa62016-01-25 14:27:31 +01001700 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001701
Christian Königa9f87f62017-03-30 14:03:59 +02001702 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001703
1704 return 0;
1705}
1706
1707/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001708 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1709 *
1710 * @adev: amdgpu_device pointer
1711 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001712 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 *
1714 * Fill in the page table entries for @bo_va.
1715 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001716 */
1717int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1718 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001719 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001720{
Christian Königec681542017-08-01 10:51:43 +02001721 struct amdgpu_bo *bo = bo_va->base.bo;
1722 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001723 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001724 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001725 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001726 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001727 struct dma_fence *exclusive;
Christian Königfebb84a2017-08-22 12:50:46 +02001728 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001729 int r;
1730
Christian Königec681542017-08-01 10:51:43 +02001731 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001732 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001733 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001734 exclusive = NULL;
1735 } else {
Christian König8358dce2016-03-30 10:50:25 +02001736 struct ttm_dma_tt *ttm;
1737
Christian Königec681542017-08-01 10:51:43 +02001738 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001739 nodes = mem->mm_node;
1740 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001741 ttm = container_of(bo_va->base.bo->tbo.ttm,
1742 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001743 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001744 }
Christian Königec681542017-08-01 10:51:43 +02001745 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001746 }
1747
Christian Königfebb84a2017-08-22 12:50:46 +02001748 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001749 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian Königfebb84a2017-08-22 12:50:46 +02001750 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001751 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752
Christian König3d7d4d32017-08-23 16:13:33 +02001753 if (!clear && bo_va->base.moved) {
1754 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001755 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001756
Christian Königcb7b6ec2017-08-15 17:08:12 +02001757 } else if (bo_va->cleared != clear) {
1758 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001759 }
Christian König7fc11952015-07-30 11:53:42 +02001760
1761 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königfebb84a2017-08-22 12:50:46 +02001762 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001763 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001764 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001765 if (r)
1766 return r;
1767 }
1768
Christian König68c62302017-07-11 17:23:29 +02001769 if (vm->use_cpu_for_update) {
1770 /* Flush HDP */
1771 mb();
1772 amdgpu_gart_flush_gpu_tlb(adev, 0);
1773 }
1774
Christian Königcb7b6ec2017-08-15 17:08:12 +02001775 spin_lock(&vm->status_lock);
1776 list_del_init(&bo_va->base.vm_status);
1777 spin_unlock(&vm->status_lock);
1778
1779 list_splice_init(&bo_va->invalids, &bo_va->valids);
1780 bo_va->cleared = clear;
1781
1782 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1783 list_for_each_entry(mapping, &bo_va->valids, list)
1784 trace_amdgpu_vm_bo_mapping(mapping);
1785 }
1786
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001787 return 0;
1788}
1789
1790/**
Christian König284710f2017-01-30 11:09:31 +01001791 * amdgpu_vm_update_prt_state - update the global PRT state
1792 */
1793static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1794{
1795 unsigned long flags;
1796 bool enable;
1797
1798 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001799 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001800 adev->gart.gart_funcs->set_prt(adev, enable);
1801 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1802}
1803
1804/**
Christian König4388fc22017-03-13 10:13:36 +01001805 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001806 */
1807static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1808{
Christian König4388fc22017-03-13 10:13:36 +01001809 if (!adev->gart.gart_funcs->set_prt)
1810 return;
1811
Christian König451bc8e2017-02-14 16:02:52 +01001812 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1813 amdgpu_vm_update_prt_state(adev);
1814}
1815
1816/**
Christian König0b15f2f2017-02-14 15:47:03 +01001817 * amdgpu_vm_prt_put - drop a PRT user
1818 */
1819static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1820{
Christian König451bc8e2017-02-14 16:02:52 +01001821 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001822 amdgpu_vm_update_prt_state(adev);
1823}
1824
1825/**
Christian König451bc8e2017-02-14 16:02:52 +01001826 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001827 */
1828static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1829{
1830 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1831
Christian König0b15f2f2017-02-14 15:47:03 +01001832 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001833 kfree(cb);
1834}
1835
1836/**
Christian König451bc8e2017-02-14 16:02:52 +01001837 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1838 */
1839static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1840 struct dma_fence *fence)
1841{
Christian König4388fc22017-03-13 10:13:36 +01001842 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001843
Christian König4388fc22017-03-13 10:13:36 +01001844 if (!adev->gart.gart_funcs->set_prt)
1845 return;
1846
1847 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001848 if (!cb) {
1849 /* Last resort when we are OOM */
1850 if (fence)
1851 dma_fence_wait(fence, false);
1852
Dan Carpenter486a68f2017-04-03 21:41:39 +03001853 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001854 } else {
1855 cb->adev = adev;
1856 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1857 amdgpu_vm_prt_cb))
1858 amdgpu_vm_prt_cb(fence, &cb->cb);
1859 }
1860}
1861
1862/**
Christian König284710f2017-01-30 11:09:31 +01001863 * amdgpu_vm_free_mapping - free a mapping
1864 *
1865 * @adev: amdgpu_device pointer
1866 * @vm: requested vm
1867 * @mapping: mapping to be freed
1868 * @fence: fence of the unmap operation
1869 *
1870 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1871 */
1872static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1873 struct amdgpu_vm *vm,
1874 struct amdgpu_bo_va_mapping *mapping,
1875 struct dma_fence *fence)
1876{
Christian König451bc8e2017-02-14 16:02:52 +01001877 if (mapping->flags & AMDGPU_PTE_PRT)
1878 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001879 kfree(mapping);
1880}
1881
1882/**
Christian König451bc8e2017-02-14 16:02:52 +01001883 * amdgpu_vm_prt_fini - finish all prt mappings
1884 *
1885 * @adev: amdgpu_device pointer
1886 * @vm: requested vm
1887 *
1888 * Register a cleanup callback to disable PRT support after VM dies.
1889 */
1890static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1891{
Christian König3f3333f2017-08-03 14:02:13 +02001892 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001893 struct dma_fence *excl, **shared;
1894 unsigned i, shared_count;
1895 int r;
1896
1897 r = reservation_object_get_fences_rcu(resv, &excl,
1898 &shared_count, &shared);
1899 if (r) {
1900 /* Not enough memory to grab the fence list, as last resort
1901 * block for all the fences to complete.
1902 */
1903 reservation_object_wait_timeout_rcu(resv, true, false,
1904 MAX_SCHEDULE_TIMEOUT);
1905 return;
1906 }
1907
1908 /* Add a callback for each fence in the reservation object */
1909 amdgpu_vm_prt_get(adev);
1910 amdgpu_vm_add_prt_cb(adev, excl);
1911
1912 for (i = 0; i < shared_count; ++i) {
1913 amdgpu_vm_prt_get(adev);
1914 amdgpu_vm_add_prt_cb(adev, shared[i]);
1915 }
1916
1917 kfree(shared);
1918}
1919
1920/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001921 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1922 *
1923 * @adev: amdgpu_device pointer
1924 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001925 * @fence: optional resulting fence (unchanged if no work needed to be done
1926 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001927 *
1928 * Make sure all freed BOs are cleared in the PT.
1929 * Returns 0 for success.
1930 *
1931 * PTs have to be reserved and mutex must be locked!
1932 */
1933int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001934 struct amdgpu_vm *vm,
1935 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001936{
1937 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001938 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001940 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001941
1942 while (!list_empty(&vm->freed)) {
1943 mapping = list_first_entry(&vm->freed,
1944 struct amdgpu_bo_va_mapping, list);
1945 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001946
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001947 if (vm->pte_support_ats)
1948 init_pte_value = AMDGPU_PTE_SYSTEM;
1949
Christian Königfc6aa332017-04-19 14:41:19 +02001950 r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm,
1951 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001952 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001953 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001954 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001955 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001956 return r;
Christian König284710f2017-01-30 11:09:31 +01001957 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001959
1960 if (fence && f) {
1961 dma_fence_put(*fence);
1962 *fence = f;
1963 } else {
1964 dma_fence_put(f);
1965 }
1966
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001967 return 0;
1968
1969}
1970
1971/**
Christian König27c7b9a2017-08-01 11:27:36 +02001972 * amdgpu_vm_clear_moved - clear moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001973 *
1974 * @adev: amdgpu_device pointer
1975 * @vm: requested vm
1976 *
Christian König27c7b9a2017-08-01 11:27:36 +02001977 * Make sure all moved BOs are cleared in the PT.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001978 * Returns 0 for success.
1979 *
1980 * PTs have to be reserved and mutex must be locked!
1981 */
Christian König27c7b9a2017-08-01 11:27:36 +02001982int amdgpu_vm_clear_moved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1983 struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984{
monk.liucfe2c972015-05-26 15:01:54 +08001985 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001986 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001987
1988 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02001989 while (!list_empty(&vm->moved)) {
1990 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02001991 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001992 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001993
Christian König99e124f2016-08-16 14:43:17 +02001994 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001995 if (r)
1996 return r;
1997
1998 spin_lock(&vm->status_lock);
1999 }
2000 spin_unlock(&vm->status_lock);
2001
monk.liucfe2c972015-05-26 15:01:54 +08002002 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08002003 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02002004
2005 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006}
2007
2008/**
2009 * amdgpu_vm_bo_add - add a bo to a specific vm
2010 *
2011 * @adev: amdgpu_device pointer
2012 * @vm: requested vm
2013 * @bo: amdgpu buffer object
2014 *
Christian König8843dbb2016-01-26 12:17:11 +01002015 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002016 * Add @bo to the list of bos associated with the vm
2017 * Returns newly added bo_va or NULL for failure
2018 *
2019 * Object has to be reserved!
2020 */
2021struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2022 struct amdgpu_vm *vm,
2023 struct amdgpu_bo *bo)
2024{
2025 struct amdgpu_bo_va *bo_va;
2026
2027 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2028 if (bo_va == NULL) {
2029 return NULL;
2030 }
Christian Königec681542017-08-01 10:51:43 +02002031 bo_va->base.vm = vm;
2032 bo_va->base.bo = bo;
2033 INIT_LIST_HEAD(&bo_va->base.bo_list);
2034 INIT_LIST_HEAD(&bo_va->base.vm_status);
2035
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002037 INIT_LIST_HEAD(&bo_va->valids);
2038 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002039
Christian Königa5f6b5b2017-01-30 11:01:38 +01002040 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002041 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042
2043 return bo_va;
2044}
2045
2046/**
2047 * amdgpu_vm_bo_map - map bo inside a vm
2048 *
2049 * @adev: amdgpu_device pointer
2050 * @bo_va: bo_va to store the address
2051 * @saddr: where to map the BO
2052 * @offset: requested offset in the BO
2053 * @flags: attributes of pages (read/write/valid/etc.)
2054 *
2055 * Add a mapping of the BO at the specefied addr into the VM.
2056 * Returns 0 for success, error for failure.
2057 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002058 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059 */
2060int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2061 struct amdgpu_bo_va *bo_va,
2062 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002063 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002064{
Christian Königa9f87f62017-03-30 14:03:59 +02002065 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002066 struct amdgpu_bo *bo = bo_va->base.bo;
2067 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069
Christian König0be52de2015-05-18 14:37:27 +02002070 /* validate the parameters */
2071 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002072 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002073 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002074
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002076 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002077 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002078 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081 saddr /= AMDGPU_GPU_PAGE_SIZE;
2082 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2083
Christian Königa9f87f62017-03-30 14:03:59 +02002084 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2085 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002086 /* bo and tmp overlap, invalid addr */
2087 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002088 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002089 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002090 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091 }
2092
2093 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002094 if (!mapping)
2095 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002096
2097 INIT_LIST_HEAD(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002098 mapping->start = saddr;
2099 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002100 mapping->offset = offset;
2101 mapping->flags = flags;
2102
Christian König7fc11952015-07-30 11:53:42 +02002103 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02002104 amdgpu_vm_it_insert(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002105
Christian König4388fc22017-03-13 10:13:36 +01002106 if (flags & AMDGPU_PTE_PRT)
2107 amdgpu_vm_prt_get(adev);
Christian König87f64a72017-08-23 14:05:48 +02002108 trace_amdgpu_vm_bo_map(bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002109
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002110 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111}
2112
2113/**
Christian König80f95c52017-03-13 10:13:39 +01002114 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2115 *
2116 * @adev: amdgpu_device pointer
2117 * @bo_va: bo_va to store the address
2118 * @saddr: where to map the BO
2119 * @offset: requested offset in the BO
2120 * @flags: attributes of pages (read/write/valid/etc.)
2121 *
2122 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2123 * mappings as we do so.
2124 * Returns 0 for success, error for failure.
2125 *
2126 * Object has to be reserved and unreserved outside!
2127 */
2128int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2129 struct amdgpu_bo_va *bo_va,
2130 uint64_t saddr, uint64_t offset,
2131 uint64_t size, uint64_t flags)
2132{
2133 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002134 struct amdgpu_bo *bo = bo_va->base.bo;
2135 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König80f95c52017-03-13 10:13:39 +01002136 uint64_t eaddr;
2137 int r;
2138
2139 /* validate the parameters */
2140 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2141 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2142 return -EINVAL;
2143
2144 /* make sure object fit at this offset */
2145 eaddr = saddr + size - 1;
2146 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002147 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002148 return -EINVAL;
2149
2150 /* Allocate all the needed memory */
2151 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2152 if (!mapping)
2153 return -ENOMEM;
2154
Christian Königec681542017-08-01 10:51:43 +02002155 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002156 if (r) {
2157 kfree(mapping);
2158 return r;
2159 }
2160
2161 saddr /= AMDGPU_GPU_PAGE_SIZE;
2162 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2163
Christian Königa9f87f62017-03-30 14:03:59 +02002164 mapping->start = saddr;
2165 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002166 mapping->offset = offset;
2167 mapping->flags = flags;
2168
2169 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02002170 amdgpu_vm_it_insert(mapping, &vm->va);
Christian König80f95c52017-03-13 10:13:39 +01002171
2172 if (flags & AMDGPU_PTE_PRT)
2173 amdgpu_vm_prt_get(adev);
Christian König87f64a72017-08-23 14:05:48 +02002174 trace_amdgpu_vm_bo_map(bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002175
2176 return 0;
2177}
2178
2179/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2181 *
2182 * @adev: amdgpu_device pointer
2183 * @bo_va: bo_va to remove the address from
2184 * @saddr: where to the BO is mapped
2185 *
2186 * Remove a mapping of the BO at the specefied addr from the VM.
2187 * Returns 0 for success, error for failure.
2188 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002189 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002190 */
2191int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2192 struct amdgpu_bo_va *bo_va,
2193 uint64_t saddr)
2194{
2195 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002196 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002197 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198
Christian König6c7fc502015-06-05 20:56:17 +02002199 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002200
Christian König7fc11952015-07-30 11:53:42 +02002201 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002202 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203 break;
2204 }
2205
Christian König7fc11952015-07-30 11:53:42 +02002206 if (&mapping->list == &bo_va->valids) {
2207 valid = false;
2208
2209 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002210 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002211 break;
2212 }
2213
Christian König32b41ac2016-03-08 18:03:27 +01002214 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002215 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002216 }
Christian König32b41ac2016-03-08 18:03:27 +01002217
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002218 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002219 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002220 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002221
Christian Könige17841b2016-03-08 17:52:01 +01002222 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002223 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002224 else
Christian König284710f2017-01-30 11:09:31 +01002225 amdgpu_vm_free_mapping(adev, vm, mapping,
2226 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227
2228 return 0;
2229}
2230
2231/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002232 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2233 *
2234 * @adev: amdgpu_device pointer
2235 * @vm: VM structure to use
2236 * @saddr: start of the range
2237 * @size: size of the range
2238 *
2239 * Remove all mappings in a range, split them as appropriate.
2240 * Returns 0 for success, error for failure.
2241 */
2242int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2243 struct amdgpu_vm *vm,
2244 uint64_t saddr, uint64_t size)
2245{
2246 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002247 LIST_HEAD(removed);
2248 uint64_t eaddr;
2249
2250 eaddr = saddr + size - 1;
2251 saddr /= AMDGPU_GPU_PAGE_SIZE;
2252 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2253
2254 /* Allocate all the needed memory */
2255 before = kzalloc(sizeof(*before), GFP_KERNEL);
2256 if (!before)
2257 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002258 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002259
2260 after = kzalloc(sizeof(*after), GFP_KERNEL);
2261 if (!after) {
2262 kfree(before);
2263 return -ENOMEM;
2264 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002265 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002266
2267 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002268 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2269 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002270 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002271 if (tmp->start < saddr) {
2272 before->start = tmp->start;
2273 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002274 before->offset = tmp->offset;
2275 before->flags = tmp->flags;
2276 list_add(&before->list, &tmp->list);
2277 }
2278
2279 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002280 if (tmp->last > eaddr) {
2281 after->start = eaddr + 1;
2282 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002283 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002284 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002285 after->flags = tmp->flags;
2286 list_add(&after->list, &tmp->list);
2287 }
2288
2289 list_del(&tmp->list);
2290 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002291
2292 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002293 }
2294
2295 /* And free them up */
2296 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002297 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002298 list_del(&tmp->list);
2299
Christian Königa9f87f62017-03-30 14:03:59 +02002300 if (tmp->start < saddr)
2301 tmp->start = saddr;
2302 if (tmp->last > eaddr)
2303 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002304
2305 list_add(&tmp->list, &vm->freed);
2306 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2307 }
2308
Junwei Zhang27f6d612017-03-16 16:09:24 +08002309 /* Insert partial mapping before the range */
2310 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002311 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002312 if (before->flags & AMDGPU_PTE_PRT)
2313 amdgpu_vm_prt_get(adev);
2314 } else {
2315 kfree(before);
2316 }
2317
2318 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002319 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002320 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002321 if (after->flags & AMDGPU_PTE_PRT)
2322 amdgpu_vm_prt_get(adev);
2323 } else {
2324 kfree(after);
2325 }
2326
2327 return 0;
2328}
2329
2330/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2332 *
2333 * @adev: amdgpu_device pointer
2334 * @bo_va: requested bo_va
2335 *
Christian König8843dbb2016-01-26 12:17:11 +01002336 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002337 *
2338 * Object have to be reserved!
2339 */
2340void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2341 struct amdgpu_bo_va *bo_va)
2342{
2343 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002344 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345
Christian Königec681542017-08-01 10:51:43 +02002346 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002347
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002348 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002349 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002350 spin_unlock(&vm->status_lock);
2351
Christian König7fc11952015-07-30 11:53:42 +02002352 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002353 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002354 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002355 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002356 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002357 }
Christian König7fc11952015-07-30 11:53:42 +02002358 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2359 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002360 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002361 amdgpu_vm_free_mapping(adev, vm, mapping,
2362 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002363 }
Christian König32b41ac2016-03-08 18:03:27 +01002364
Chris Wilsonf54d1862016-10-25 13:00:45 +01002365 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002366 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002367}
2368
2369/**
2370 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2371 *
2372 * @adev: amdgpu_device pointer
2373 * @vm: requested vm
2374 * @bo: amdgpu buffer object
2375 *
Christian König8843dbb2016-01-26 12:17:11 +01002376 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002377 */
2378void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002379 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002380{
Christian Königec681542017-08-01 10:51:43 +02002381 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382
Christian Königec681542017-08-01 10:51:43 +02002383 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002384 struct amdgpu_vm *vm = bo_base->vm;
2385
Christian König3d7d4d32017-08-23 16:13:33 +02002386 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002387 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2388 spin_lock(&bo_base->vm->status_lock);
2389 list_move(&bo_base->vm_status, &vm->evicted);
2390 spin_unlock(&bo_base->vm->status_lock);
2391 continue;
2392 }
2393
2394 /* Don't add page tables to the moved state */
2395 if (bo->tbo.type == ttm_bo_type_kernel)
2396 continue;
2397
Christian Königec681542017-08-01 10:51:43 +02002398 spin_lock(&bo_base->vm->status_lock);
Christian Königcb7b6ec2017-08-15 17:08:12 +02002399 list_move(&bo_base->vm_status, &bo_base->vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002400 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002401 }
2402}
2403
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002404static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2405{
2406 /* Total bits covered by PD + PTs */
2407 unsigned bits = ilog2(vm_size) + 18;
2408
2409 /* Make sure the PD is 4K in size up to 8GB address space.
2410 Above that split equal between PD and PTs */
2411 if (vm_size <= 8)
2412 return (bits - 9);
2413 else
2414 return ((bits + 3) / 2);
2415}
2416
2417/**
Roger Hed07f14b2017-08-15 16:05:59 +08002418 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2419 *
2420 * @adev: amdgpu_device pointer
2421 * @fragment_size_default: the default fragment size if it's set auto
2422 */
2423void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev, uint32_t fragment_size_default)
2424{
2425 if (amdgpu_vm_fragment_size == -1)
2426 adev->vm_manager.fragment_size = fragment_size_default;
2427 else
2428 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2429}
2430
2431/**
2432 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002433 *
2434 * @adev: amdgpu_device pointer
2435 * @vm_size: the default vm size if it's set auto
2436 */
Roger Hed07f14b2017-08-15 16:05:59 +08002437void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size, uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002438{
2439 /* adjust vm size firstly */
2440 if (amdgpu_vm_size == -1)
2441 adev->vm_manager.vm_size = vm_size;
2442 else
2443 adev->vm_manager.vm_size = amdgpu_vm_size;
2444
2445 /* block size depends on vm size */
2446 if (amdgpu_vm_block_size == -1)
2447 adev->vm_manager.block_size =
2448 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2449 else
2450 adev->vm_manager.block_size = amdgpu_vm_block_size;
2451
Roger Hed07f14b2017-08-15 16:05:59 +08002452 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2453
2454 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2455 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2456 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002457}
2458
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002459/**
2460 * amdgpu_vm_init - initialize a vm instance
2461 *
2462 * @adev: amdgpu_device pointer
2463 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002464 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002465 *
Christian König8843dbb2016-01-26 12:17:11 +01002466 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002467 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002468int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2469 int vm_context)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002470{
2471 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002472 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002473 unsigned ring_instance;
2474 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002475 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002476 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002477 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002478 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002480 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002481 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002482 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2483 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002484 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002485 INIT_LIST_HEAD(&vm->evicted);
Christian König27c7b9a2017-08-01 11:27:36 +02002486 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002487 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002488
Christian König2bd9ccf2016-02-01 12:53:58 +01002489 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002490
2491 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2492 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2493 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002494 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2495 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2496 rq, amdgpu_sched_jobs);
2497 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002498 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002499
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002500 vm->pte_support_ats = false;
2501
2502 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002503 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2504 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002505
2506 if (adev->asic_type == CHIP_RAVEN) {
2507 vm->pte_support_ats = true;
2508 init_pde_value = AMDGPU_PTE_SYSTEM | AMDGPU_PDE_PTE;
2509 }
2510 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002511 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2512 AMDGPU_VM_USE_CPU_FOR_GFX);
2513 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2514 vm->use_cpu_for_update ? "CPU" : "SDMA");
2515 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2516 "CPU update of VM recommended only for large BAR system\n");
Christian Königa24960f2016-10-12 13:20:52 +02002517 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002518
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002519 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2520 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2521 if (vm->use_cpu_for_update)
2522 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2523 else
2524 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2525 AMDGPU_GEM_CREATE_SHADOW);
2526
Christian Königf566ceb2016-10-27 20:04:38 +02002527 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002528 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002529 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002530 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002531 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002532 goto error_free_sched_entity;
2533
Christian König3f3333f2017-08-03 14:02:13 +02002534 vm->root.base.vm = vm;
2535 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2536 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002537
2538 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002539 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002540 if (r)
2541 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002542
Christian König3f3333f2017-08-03 14:02:13 +02002543 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
2544 if (r)
2545 goto error_free_root;
2546 amdgpu_bo_unreserve(vm->root.base.bo);
2547 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002548
2549 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002550
Christian König67003a12016-10-12 14:46:26 +02002551error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002552 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2553 amdgpu_bo_unref(&vm->root.base.bo);
2554 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002555
2556error_free_sched_entity:
2557 amd_sched_entity_fini(&ring->sched, &vm->entity);
2558
2559 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002560}
2561
2562/**
Christian Königf566ceb2016-10-27 20:04:38 +02002563 * amdgpu_vm_free_levels - free PD/PT levels
2564 *
2565 * @level: PD/PT starting level to free
2566 *
2567 * Free the page directory or page table level and all sub levels.
2568 */
2569static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2570{
2571 unsigned i;
2572
Christian König3f3333f2017-08-03 14:02:13 +02002573 if (level->base.bo) {
2574 list_del(&level->base.bo_list);
2575 list_del(&level->base.vm_status);
2576 amdgpu_bo_unref(&level->base.bo->shadow);
2577 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002578 }
2579
2580 if (level->entries)
2581 for (i = 0; i <= level->last_entry_used; i++)
2582 amdgpu_vm_free_levels(&level->entries[i]);
2583
Michal Hocko20981052017-05-17 14:23:12 +02002584 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002585}
2586
2587/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588 * amdgpu_vm_fini - tear down a vm instance
2589 *
2590 * @adev: amdgpu_device pointer
2591 * @vm: requested vm
2592 *
Christian König8843dbb2016-01-26 12:17:11 +01002593 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002594 * Unbind the VM and remove all bos from the vm bo list
2595 */
2596void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2597{
2598 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002599 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002600 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002601
Christian König2d55e452016-02-08 17:37:38 +01002602 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002603
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002604 if (!RB_EMPTY_ROOT(&vm->va)) {
2605 dev_err(adev->dev, "still active bo inside vm\n");
2606 }
Christian Königa9f87f62017-03-30 14:03:59 +02002607 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002609 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002610 kfree(mapping);
2611 }
2612 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002613 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002614 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002615 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002616 }
Christian König284710f2017-01-30 11:09:31 +01002617
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002618 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002619 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002620 }
2621
Christian Königf566ceb2016-10-27 20:04:38 +02002622 amdgpu_vm_free_levels(&vm->root);
Christian Königa24960f2016-10-12 13:20:52 +02002623 dma_fence_put(vm->last_dir_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002624 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2625 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002626}
Christian Königea89f8c2015-11-15 20:52:06 +01002627
2628/**
Christian Königa9a78b32016-01-21 10:19:11 +01002629 * amdgpu_vm_manager_init - init the VM manager
2630 *
2631 * @adev: amdgpu_device pointer
2632 *
2633 * Initialize the VM manager structures
2634 */
2635void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2636{
Christian König76456702017-04-06 17:52:39 +02002637 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002638
Christian König76456702017-04-06 17:52:39 +02002639 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2640 struct amdgpu_vm_id_manager *id_mgr =
2641 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002642
Christian König76456702017-04-06 17:52:39 +02002643 mutex_init(&id_mgr->lock);
2644 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002645 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002646
2647 /* skip over VMID 0, since it is the system VM */
2648 for (j = 1; j < id_mgr->num_ids; ++j) {
2649 amdgpu_vm_reset_id(adev, i, j);
2650 amdgpu_sync_create(&id_mgr->ids[i].active);
2651 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2652 }
Christian König971fe9a92016-03-01 15:09:25 +01002653 }
Christian König2d55e452016-02-08 17:37:38 +01002654
Chris Wilsonf54d1862016-10-25 13:00:45 +01002655 adev->vm_manager.fence_context =
2656 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002657 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2658 adev->vm_manager.seqno[i] = 0;
2659
Christian König2d55e452016-02-08 17:37:38 +01002660 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002661 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002662 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002663 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002664
2665 /* If not overridden by the user, by default, only in large BAR systems
2666 * Compute VM tables will be updated by CPU
2667 */
2668#ifdef CONFIG_X86_64
2669 if (amdgpu_vm_update_mode == -1) {
2670 if (amdgpu_vm_is_large_bar(adev))
2671 adev->vm_manager.vm_update_mode =
2672 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2673 else
2674 adev->vm_manager.vm_update_mode = 0;
2675 } else
2676 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2677#else
2678 adev->vm_manager.vm_update_mode = 0;
2679#endif
2680
Christian Königa9a78b32016-01-21 10:19:11 +01002681}
2682
2683/**
Christian Königea89f8c2015-11-15 20:52:06 +01002684 * amdgpu_vm_manager_fini - cleanup VM manager
2685 *
2686 * @adev: amdgpu_device pointer
2687 *
2688 * Cleanup the VM manager and free resources.
2689 */
2690void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2691{
Christian König76456702017-04-06 17:52:39 +02002692 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002693
Christian König76456702017-04-06 17:52:39 +02002694 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2695 struct amdgpu_vm_id_manager *id_mgr =
2696 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002697
Christian König76456702017-04-06 17:52:39 +02002698 mutex_destroy(&id_mgr->lock);
2699 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2700 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2701
2702 amdgpu_sync_free(&id->active);
2703 dma_fence_put(id->flushed_updates);
2704 dma_fence_put(id->last_flush);
2705 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002706 }
Christian Königea89f8c2015-11-15 20:52:06 +01002707}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002708
2709int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2710{
2711 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002712 struct amdgpu_device *adev = dev->dev_private;
2713 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2714 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002715
2716 switch (args->in.op) {
2717 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002718 /* current, we only have requirement to reserve vmid from gfxhub */
2719 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2720 AMDGPU_GFXHUB);
2721 if (r)
2722 return r;
2723 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002724 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002725 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002726 break;
2727 default:
2728 return -EINVAL;
2729 }
2730
2731 return 0;
2732}