blob: 3ecde81821ad48b47fce6df96e036295f3ceb495 [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);
Chunming Zhou4c7e8852016-08-15 11:46:21 +080080 /* indicate update pt or its shadow */
81 bool shadow;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040082};
83
Christian König284710f2017-01-30 11:09:31 +010084/* Helper to disable partial resident texture feature from a fence callback */
85struct amdgpu_prt_cb {
86 struct amdgpu_device *adev;
87 struct dma_fence_cb cb;
88};
89
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090/**
Christian König72a7ec52016-10-19 11:03:57 +020091 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092 *
93 * @adev: amdgpu_device pointer
94 *
Christian König72a7ec52016-10-19 11:03:57 +020095 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 */
Christian König72a7ec52016-10-19 11:03:57 +020097static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
98 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099{
Christian König72a7ec52016-10-19 11:03:57 +0200100 if (level == 0)
101 /* For the root directory */
102 return adev->vm_manager.max_pfn >>
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800103 (adev->vm_manager.block_size *
104 adev->vm_manager.num_level);
Christian König72a7ec52016-10-19 11:03:57 +0200105 else if (level == adev->vm_manager.num_level)
106 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800107 return AMDGPU_VM_PTE_COUNT(adev);
Christian König72a7ec52016-10-19 11:03:57 +0200108 else
109 /* Everything in between */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800110 return 1 << adev->vm_manager.block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111}
112
113/**
Christian König72a7ec52016-10-19 11:03:57 +0200114 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 *
116 * @adev: amdgpu_device pointer
117 *
Christian König72a7ec52016-10-19 11:03:57 +0200118 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 */
Christian König72a7ec52016-10-19 11:03:57 +0200120static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121{
Christian König72a7ec52016-10-19 11:03:57 +0200122 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123}
124
125/**
Christian König56467eb2015-12-11 15:16:32 +0100126 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 *
128 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100129 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100130 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 *
132 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100133 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 */
Christian König56467eb2015-12-11 15:16:32 +0100135void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
136 struct list_head *validated,
137 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138{
Christian König67003a12016-10-12 14:46:26 +0200139 entry->robj = vm->root.bo;
Christian König56467eb2015-12-11 15:16:32 +0100140 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200141 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100142 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100143 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100144 list_add(&entry->tv.head, validated);
145}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146
Christian König56467eb2015-12-11 15:16:32 +0100147/**
Christian König670fecc2016-10-12 15:36:57 +0200148 * amdgpu_vm_validate_layer - validate a single page table level
149 *
150 * @parent: parent page table level
151 * @validate: callback to do the validation
152 * @param: parameter for the validation callback
153 *
154 * Validate the page table BOs on command submission if neccessary.
155 */
156static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
157 int (*validate)(void *, struct amdgpu_bo *),
158 void *param)
159{
160 unsigned i;
161 int r;
162
163 if (!parent->entries)
164 return 0;
165
166 for (i = 0; i <= parent->last_entry_used; ++i) {
167 struct amdgpu_vm_pt *entry = &parent->entries[i];
168
169 if (!entry->bo)
170 continue;
171
172 r = validate(param, entry->bo);
173 if (r)
174 return r;
175
176 /*
177 * Recurse into the sub directory. This is harmless because we
178 * have only a maximum of 5 layers.
179 */
180 r = amdgpu_vm_validate_level(entry, validate, param);
181 if (r)
182 return r;
183 }
184
185 return r;
186}
187
188/**
Christian Königf7da30d2016-09-28 12:03:04 +0200189 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100190 *
Christian König5a712a82016-06-21 16:28:15 +0200191 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100192 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200193 * @validate: callback to do the validation
194 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195 *
Christian Königf7da30d2016-09-28 12:03:04 +0200196 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197 */
Christian Königf7da30d2016-09-28 12:03:04 +0200198int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
199 int (*validate)(void *p, struct amdgpu_bo *bo),
200 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400201{
Christian König5a712a82016-06-21 16:28:15 +0200202 uint64_t num_evictions;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400203
Christian König5a712a82016-06-21 16:28:15 +0200204 /* We only need to validate the page tables
205 * if they aren't already valid.
206 */
207 num_evictions = atomic64_read(&adev->num_evictions);
208 if (num_evictions == vm->last_eviction_counter)
Christian Königf7da30d2016-09-28 12:03:04 +0200209 return 0;
Christian König5a712a82016-06-21 16:28:15 +0200210
Christian König670fecc2016-10-12 15:36:57 +0200211 return amdgpu_vm_validate_level(&vm->root, validate, param);
Christian Königeceb8a12016-01-11 15:35:21 +0100212}
213
214/**
Christian Königd711e132016-10-13 10:20:53 +0200215 * amdgpu_vm_move_level_in_lru - move one level of PT BOs to the LRU tail
216 *
217 * @adev: amdgpu device instance
218 * @vm: vm providing the BOs
219 *
220 * Move the PT BOs to the tail of the LRU.
221 */
222static void amdgpu_vm_move_level_in_lru(struct amdgpu_vm_pt *parent)
223{
224 unsigned i;
225
226 if (!parent->entries)
227 return;
228
229 for (i = 0; i <= parent->last_entry_used; ++i) {
230 struct amdgpu_vm_pt *entry = &parent->entries[i];
231
232 if (!entry->bo)
233 continue;
234
235 ttm_bo_move_to_lru_tail(&entry->bo->tbo);
236 amdgpu_vm_move_level_in_lru(entry);
237 }
238}
239
240/**
Christian Königeceb8a12016-01-11 15:35:21 +0100241 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
242 *
243 * @adev: amdgpu device instance
244 * @vm: vm providing the BOs
245 *
246 * Move the PT BOs to the tail of the LRU.
247 */
248void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
249 struct amdgpu_vm *vm)
250{
251 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Christian Königeceb8a12016-01-11 15:35:21 +0100252
253 spin_lock(&glob->lru_lock);
Christian Königd711e132016-10-13 10:20:53 +0200254 amdgpu_vm_move_level_in_lru(&vm->root);
Christian Königeceb8a12016-01-11 15:35:21 +0100255 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400256}
257
Christian Königf566ceb2016-10-27 20:04:38 +0200258 /**
259 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
260 *
261 * @adev: amdgpu_device pointer
262 * @vm: requested vm
263 * @saddr: start of the address range
264 * @eaddr: end of the address range
265 *
266 * Make sure the page directories and page tables are allocated
267 */
268static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
269 struct amdgpu_vm *vm,
270 struct amdgpu_vm_pt *parent,
271 uint64_t saddr, uint64_t eaddr,
272 unsigned level)
273{
274 unsigned shift = (adev->vm_manager.num_level - level) *
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800275 adev->vm_manager.block_size;
Christian Königf566ceb2016-10-27 20:04:38 +0200276 unsigned pt_idx, from, to;
277 int r;
278
279 if (!parent->entries) {
280 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
281
282 parent->entries = drm_calloc_large(num_entries,
283 sizeof(struct amdgpu_vm_pt));
284 if (!parent->entries)
285 return -ENOMEM;
286 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
287 }
288
Felix Kuehling1866bac2017-03-28 20:36:12 -0400289 from = saddr >> shift;
290 to = eaddr >> shift;
291 if (from >= amdgpu_vm_num_entries(adev, level) ||
292 to >= amdgpu_vm_num_entries(adev, level))
293 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200294
295 if (to > parent->last_entry_used)
296 parent->last_entry_used = to;
297
298 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400299 saddr = saddr & ((1 << shift) - 1);
300 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200301
302 /* walk over the address space and allocate the page tables */
303 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
304 struct reservation_object *resv = vm->root.bo->tbo.resv;
305 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
306 struct amdgpu_bo *pt;
307
308 if (!entry->bo) {
309 r = amdgpu_bo_create(adev,
310 amdgpu_vm_bo_size(adev, level),
311 AMDGPU_GPU_PAGE_SIZE, true,
312 AMDGPU_GEM_DOMAIN_VRAM,
313 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
314 AMDGPU_GEM_CREATE_SHADOW |
315 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
316 AMDGPU_GEM_CREATE_VRAM_CLEARED,
317 NULL, resv, &pt);
318 if (r)
319 return r;
320
321 /* Keep a reference to the root directory to avoid
322 * freeing them up in the wrong order.
323 */
324 pt->parent = amdgpu_bo_ref(vm->root.bo);
325
326 entry->bo = pt;
327 entry->addr = 0;
328 }
329
330 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400331 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
332 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
333 ((1 << shift) - 1);
334 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
335 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200336 if (r)
337 return r;
338 }
339 }
340
341 return 0;
342}
343
Christian König663e4572017-03-13 10:13:37 +0100344/**
345 * amdgpu_vm_alloc_pts - Allocate page tables.
346 *
347 * @adev: amdgpu_device pointer
348 * @vm: VM to allocate page tables for
349 * @saddr: Start address which needs to be allocated
350 * @size: Size from start address we need.
351 *
352 * Make sure the page tables are allocated.
353 */
354int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
355 struct amdgpu_vm *vm,
356 uint64_t saddr, uint64_t size)
357{
Felix Kuehling22770e52017-03-28 20:24:53 -0400358 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100359 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100360
361 /* validate the parameters */
362 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
363 return -EINVAL;
364
365 eaddr = saddr + size - 1;
366 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
367 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400368 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100369 last_pfn, adev->vm_manager.max_pfn);
370 return -EINVAL;
371 }
372
373 saddr /= AMDGPU_GPU_PAGE_SIZE;
374 eaddr /= AMDGPU_GPU_PAGE_SIZE;
375
Christian Königf566ceb2016-10-27 20:04:38 +0200376 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100377}
378
Christian König641e9402017-04-03 13:59:25 +0200379/**
380 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
381 *
382 * @adev: amdgpu_device pointer
383 * @id: VMID structure
384 *
385 * Check if GPU reset occured since last use of the VMID.
386 */
387static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
388 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800389{
390 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200391 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800392}
393
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800394static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
395{
396 return !!vm->reserved_vmid[vmhub];
397}
398
399/* idr_mgr->lock must be held */
400static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
401 struct amdgpu_ring *ring,
402 struct amdgpu_sync *sync,
403 struct dma_fence *fence,
404 struct amdgpu_job *job)
405{
406 struct amdgpu_device *adev = ring->adev;
407 unsigned vmhub = ring->funcs->vmhub;
408 uint64_t fence_context = adev->fence_context + ring->idx;
409 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
410 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
411 struct dma_fence *updates = sync->last_vm_update;
412 int r = 0;
413 struct dma_fence *flushed, *tmp;
414 bool needs_flush = false;
415
416 flushed = id->flushed_updates;
417 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
418 (atomic64_read(&id->owner) != vm->client_id) ||
419 (job->vm_pd_addr != id->pd_gpu_addr) ||
420 (updates && (!flushed || updates->context != flushed->context ||
421 dma_fence_is_later(updates, flushed))) ||
422 (!id->last_flush || (id->last_flush->context != fence_context &&
423 !dma_fence_is_signaled(id->last_flush)))) {
424 needs_flush = true;
425 /* to prevent one context starved by another context */
426 id->pd_gpu_addr = 0;
427 tmp = amdgpu_sync_peek_fence(&id->active, ring);
428 if (tmp) {
429 r = amdgpu_sync_fence(adev, sync, tmp);
430 return r;
431 }
432 }
433
434 /* Good we can use this VMID. Remember this submission as
435 * user of the VMID.
436 */
437 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
438 if (r)
439 goto out;
440
441 if (updates && (!flushed || updates->context != flushed->context ||
442 dma_fence_is_later(updates, flushed))) {
443 dma_fence_put(id->flushed_updates);
444 id->flushed_updates = dma_fence_get(updates);
445 }
446 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800447 atomic64_set(&id->owner, vm->client_id);
448 job->vm_needs_flush = needs_flush;
449 if (needs_flush) {
450 dma_fence_put(id->last_flush);
451 id->last_flush = NULL;
452 }
453 job->vm_id = id - id_mgr->ids;
454 trace_amdgpu_vm_grab_id(vm, ring, job);
455out:
456 return r;
457}
458
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459/**
460 * amdgpu_vm_grab_id - allocate the next free VMID
461 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200463 * @ring: ring we want to submit job to
464 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100465 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466 *
Christian König7f8a5292015-07-20 16:09:40 +0200467 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400468 */
Christian König7f8a5292015-07-20 16:09:40 +0200469int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100470 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800471 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200474 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200475 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200476 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100477 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200478 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100479 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200480 unsigned i;
481 int r = 0;
482
Christian König76456702017-04-06 17:52:39 +0200483 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800484 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
485 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
486 mutex_unlock(&id_mgr->lock);
487 return r;
488 }
489 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
490 if (!fences) {
491 mutex_unlock(&id_mgr->lock);
492 return -ENOMEM;
493 }
Christian König36fd7c52016-05-23 15:30:08 +0200494 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200495 i = 0;
Christian König76456702017-04-06 17:52:39 +0200496 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200497 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
498 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200499 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200500 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200501 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100502
Christian König1fbb2e92016-06-01 10:47:36 +0200503 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200504 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200505 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
506 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100507 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200508 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200509
Christian König1fbb2e92016-06-01 10:47:36 +0200510 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100511 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200512
Chris Wilsonf54d1862016-10-25 13:00:45 +0100513 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200514 seqno, true);
515 if (!array) {
516 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100517 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200518 kfree(fences);
519 r = -ENOMEM;
520 goto error;
521 }
Christian König8d76001e2016-05-23 16:00:32 +0200522
Christian König8d76001e2016-05-23 16:00:32 +0200523
Christian König1fbb2e92016-06-01 10:47:36 +0200524 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100525 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200526 if (r)
527 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200528
Christian König76456702017-04-06 17:52:39 +0200529 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200530 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200531
Christian König1fbb2e92016-06-01 10:47:36 +0200532 }
533 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200534
Christian König87c910d2017-03-30 16:56:20 +0200535 job->vm_needs_flush = false;
Christian König1fbb2e92016-06-01 10:47:36 +0200536 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200537 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100538 struct dma_fence *flushed;
Christian König87c910d2017-03-30 16:56:20 +0200539 bool needs_flush = false;
Christian König8d76001e2016-05-23 16:00:32 +0200540
Christian König1fbb2e92016-06-01 10:47:36 +0200541 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200542 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800543 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200544
545 if (atomic64_read(&id->owner) != vm->client_id)
546 continue;
547
Chunming Zhoufd53be32016-07-01 17:59:01 +0800548 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200549 continue;
550
Christian König87c910d2017-03-30 16:56:20 +0200551 if (!id->last_flush ||
552 (id->last_flush->context != fence_context &&
553 !dma_fence_is_signaled(id->last_flush)))
554 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200555
556 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200557 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
558 needs_flush = true;
559
560 /* Concurrent flushes are only possible starting with Vega10 */
561 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200562 continue;
563
Christian König3dab83b2016-06-01 13:31:17 +0200564 /* Good we can use this VMID. Remember this submission as
565 * user of the VMID.
566 */
Christian König1fbb2e92016-06-01 10:47:36 +0200567 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
568 if (r)
569 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200570
Christian König87c910d2017-03-30 16:56:20 +0200571 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
572 dma_fence_put(id->flushed_updates);
573 id->flushed_updates = dma_fence_get(updates);
574 }
Christian König8d76001e2016-05-23 16:00:32 +0200575
Christian König87c910d2017-03-30 16:56:20 +0200576 if (needs_flush)
577 goto needs_flush;
578 else
579 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200580
Christian König4f618e72017-04-06 15:18:21 +0200581 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800582
Christian König1fbb2e92016-06-01 10:47:36 +0200583 /* Still no ID to use? Then use the idle one found earlier */
584 id = idle;
585
586 /* Remember this submission as user of the VMID */
587 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100588 if (r)
589 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100590
Christian König87c910d2017-03-30 16:56:20 +0200591 id->pd_gpu_addr = job->vm_pd_addr;
592 dma_fence_put(id->flushed_updates);
593 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200594 atomic64_set(&id->owner, vm->client_id);
595
596needs_flush:
597 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100598 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100599 id->last_flush = NULL;
600
Christian König87c910d2017-03-30 16:56:20 +0200601no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200602 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603
Christian König76456702017-04-06 17:52:39 +0200604 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200605 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100606
607error:
Christian König76456702017-04-06 17:52:39 +0200608 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100609 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610}
611
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800612static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
613 struct amdgpu_vm *vm,
614 unsigned vmhub)
615{
616 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
617
618 mutex_lock(&id_mgr->lock);
619 if (vm->reserved_vmid[vmhub]) {
620 list_add(&vm->reserved_vmid[vmhub]->list,
621 &id_mgr->ids_lru);
622 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800623 atomic_dec(&id_mgr->reserved_vmid_num);
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800624 }
625 mutex_unlock(&id_mgr->lock);
626}
627
628static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
629 struct amdgpu_vm *vm,
630 unsigned vmhub)
631{
632 struct amdgpu_vm_id_manager *id_mgr;
633 struct amdgpu_vm_id *idle;
634 int r = 0;
635
636 id_mgr = &adev->vm_manager.id_mgr[vmhub];
637 mutex_lock(&id_mgr->lock);
638 if (vm->reserved_vmid[vmhub])
639 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800640 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
641 AMDGPU_VM_MAX_RESERVED_VMID) {
642 DRM_ERROR("Over limitation of reserved vmid\n");
643 atomic_dec(&id_mgr->reserved_vmid_num);
644 r = -EINVAL;
645 goto unlock;
646 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800647 /* Select the first entry VMID */
648 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
649 list_del_init(&idle->list);
650 vm->reserved_vmid[vmhub] = idle;
651 mutex_unlock(&id_mgr->lock);
652
653 return 0;
654unlock:
655 mutex_unlock(&id_mgr->lock);
656 return r;
657}
658
Alex Deucher93dcc372016-06-17 17:05:15 -0400659static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
660{
661 struct amdgpu_device *adev = ring->adev;
Alex Deuchera1255102016-10-13 17:41:13 -0400662 const struct amdgpu_ip_block *ip_block;
Alex Deucher93dcc372016-06-17 17:05:15 -0400663
Christian König21cd9422016-10-05 15:36:39 +0200664 if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
Alex Deucher93dcc372016-06-17 17:05:15 -0400665 /* only compute rings */
666 return false;
667
668 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
669 if (!ip_block)
670 return false;
671
Alex Deuchera1255102016-10-13 17:41:13 -0400672 if (ip_block->version->major <= 7) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400673 /* gfx7 has no workaround */
674 return true;
Alex Deuchera1255102016-10-13 17:41:13 -0400675 } else if (ip_block->version->major == 8) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400676 if (adev->gfx.mec_fw_version >= 673)
677 /* gfx8 is fixed in MEC firmware 673 */
678 return false;
679 else
680 return true;
681 }
682 return false;
683}
684
Alex Xiee60f8db2017-03-09 11:36:26 -0500685static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr)
686{
687 u64 addr = mc_addr;
688
Christian Königf75e2372017-03-30 15:55:07 +0200689 if (adev->gart.gart_funcs->adjust_mc_addr)
690 addr = adev->gart.gart_funcs->adjust_mc_addr(adev, addr);
Alex Xiee60f8db2017-03-09 11:36:26 -0500691
692 return addr;
693}
694
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400695bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
696 struct amdgpu_job *job)
697{
698 struct amdgpu_device *adev = ring->adev;
699 unsigned vmhub = ring->funcs->vmhub;
700 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
701 struct amdgpu_vm_id *id;
702 bool gds_switch_needed;
703 bool vm_flush_needed = job->vm_needs_flush ||
704 amdgpu_vm_ring_has_compute_vm_bug(ring);
705
706 if (job->vm_id == 0)
707 return false;
708 id = &id_mgr->ids[job->vm_id];
709 gds_switch_needed = ring->funcs->emit_gds_switch && (
710 id->gds_base != job->gds_base ||
711 id->gds_size != job->gds_size ||
712 id->gws_base != job->gws_base ||
713 id->gws_size != job->gws_size ||
714 id->oa_base != job->oa_base ||
715 id->oa_size != job->oa_size);
716
717 if (amdgpu_vm_had_gpu_reset(adev, id))
718 return true;
719 if (!vm_flush_needed && !gds_switch_needed)
720 return false;
721 return true;
722}
723
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724/**
725 * amdgpu_vm_flush - hardware flush the vm
726 *
727 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100728 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100729 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 *
Christian König4ff37a82016-02-26 16:18:26 +0100731 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 */
Chunming Zhoufd53be32016-07-01 17:59:01 +0800733int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734{
Christian König971fe9a92016-03-01 15:09:25 +0100735 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200736 unsigned vmhub = ring->funcs->vmhub;
737 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
738 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100739 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800740 id->gds_base != job->gds_base ||
741 id->gds_size != job->gds_size ||
742 id->gws_base != job->gws_base ||
743 id->gws_size != job->gws_size ||
744 id->oa_base != job->oa_base ||
745 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800746 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200747 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100748 int r;
Christian Königd564a062016-03-01 15:51:53 +0100749
Christian Königf7d015b2017-04-03 14:28:26 +0200750 if (amdgpu_vm_had_gpu_reset(adev, id)) {
751 gds_switch_needed = true;
752 vm_flush_needed = true;
753 }
Christian König971fe9a92016-03-01 15:09:25 +0100754
Christian Königf7d015b2017-04-03 14:28:26 +0200755 if (!vm_flush_needed && !gds_switch_needed)
756 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100757
Christian Königc0e51932017-04-03 14:16:07 +0200758 if (ring->funcs->init_cond_exec)
759 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100760
Christian Königf7d015b2017-04-03 14:28:26 +0200761 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200762 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800763
Christian König9a94f5a2017-05-12 14:46:23 +0200764 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
765 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800766
Christian Königc0e51932017-04-03 14:16:07 +0200767 r = amdgpu_fence_emit(ring, &fence);
768 if (r)
769 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800770
Christian König76456702017-04-06 17:52:39 +0200771 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200772 dma_fence_put(id->last_flush);
773 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800774 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200775 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200776 }
Monk Liue9d672b2017-03-15 12:18:57 +0800777
Chunming Zhouca7962d2017-05-11 18:22:17 +0800778 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200779 id->gds_base = job->gds_base;
780 id->gds_size = job->gds_size;
781 id->gws_base = job->gws_base;
782 id->gws_size = job->gws_size;
783 id->oa_base = job->oa_base;
784 id->oa_size = job->oa_size;
785 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
786 job->gds_size, job->gws_base,
787 job->gws_size, job->oa_base,
788 job->oa_size);
789 }
790
791 if (ring->funcs->patch_cond_exec)
792 amdgpu_ring_patch_cond_exec(ring, patch_offset);
793
794 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
795 if (ring->funcs->emit_switch_buffer) {
796 amdgpu_ring_emit_switch_buffer(ring);
797 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798 }
Christian König41d9eb22016-03-01 16:46:18 +0100799 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100800}
801
802/**
803 * amdgpu_vm_reset_id - reset VMID to zero
804 *
805 * @adev: amdgpu device structure
806 * @vm_id: vmid number to use
807 *
808 * Reset saved GDW, GWS and OA to force switch on next flush.
809 */
Christian König76456702017-04-06 17:52:39 +0200810void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
811 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100812{
Christian König76456702017-04-06 17:52:39 +0200813 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
814 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100815
Christian König32601d42017-05-10 20:06:58 +0200816 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100817 id->gds_base = 0;
818 id->gds_size = 0;
819 id->gws_base = 0;
820 id->gws_size = 0;
821 id->oa_base = 0;
822 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823}
824
825/**
Christian König32601d42017-05-10 20:06:58 +0200826 * amdgpu_vm_reset_all_id - reset VMID to zero
827 *
828 * @adev: amdgpu device structure
829 *
830 * Reset VMID to force flush on next use
831 */
832void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
833{
834 unsigned i, j;
835
836 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
837 struct amdgpu_vm_id_manager *id_mgr =
838 &adev->vm_manager.id_mgr[i];
839
840 for (j = 1; j < id_mgr->num_ids; ++j)
841 amdgpu_vm_reset_id(adev, i, j);
842 }
843}
844
845/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
847 *
848 * @vm: requested vm
849 * @bo: requested buffer object
850 *
Christian König8843dbb2016-01-26 12:17:11 +0100851 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400852 * Search inside the @bos vm list for the requested vm
853 * Returns the found bo_va or NULL if none is found
854 *
855 * Object has to be reserved!
856 */
857struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
858 struct amdgpu_bo *bo)
859{
860 struct amdgpu_bo_va *bo_va;
861
862 list_for_each_entry(bo_va, &bo->va, bo_list) {
863 if (bo_va->vm == vm) {
864 return bo_va;
865 }
866 }
867 return NULL;
868}
869
870/**
Christian Königafef8b82016-08-12 13:29:18 +0200871 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400872 *
Christian König29efc4f2016-08-04 14:52:50 +0200873 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 * @pe: addr of the page entry
875 * @addr: dst addr to write into pe
876 * @count: number of page entries to update
877 * @incr: increase next addr by incr bytes
878 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400879 *
880 * Traces the parameters and calls the right asic functions
881 * to setup the page table using the DMA.
882 */
Christian Königafef8b82016-08-12 13:29:18 +0200883static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
884 uint64_t pe, uint64_t addr,
885 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800886 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400887{
Christian Königec2f05f2016-09-25 16:11:52 +0200888 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889
Christian Königafef8b82016-08-12 13:29:18 +0200890 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200891 amdgpu_vm_write_pte(params->adev, params->ib, pe,
892 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400893
894 } else {
Christian König27c5f362016-08-04 15:02:49 +0200895 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400896 count, incr, flags);
897 }
898}
899
900/**
Christian Königafef8b82016-08-12 13:29:18 +0200901 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
902 *
903 * @params: see amdgpu_pte_update_params definition
904 * @pe: addr of the page entry
905 * @addr: dst addr to write into pe
906 * @count: number of page entries to update
907 * @incr: increase next addr by incr bytes
908 * @flags: hw access flags
909 *
910 * Traces the parameters and calls the DMA function to copy the PTEs.
911 */
912static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
913 uint64_t pe, uint64_t addr,
914 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800915 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200916{
Christian Königec2f05f2016-09-25 16:11:52 +0200917 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200918
Christian Königec2f05f2016-09-25 16:11:52 +0200919
920 trace_amdgpu_vm_copy_ptes(pe, src, count);
921
922 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200923}
924
925/**
Christian Königb07c9d22015-11-30 13:26:07 +0100926 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927 *
Christian Königb07c9d22015-11-30 13:26:07 +0100928 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 * @addr: the unmapped addr
930 *
931 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100932 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200934static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935{
936 uint64_t result;
937
Christian Königde9ea7b2016-08-12 11:33:30 +0200938 /* page table offset */
939 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940
Christian Königde9ea7b2016-08-12 11:33:30 +0200941 /* in case cpu page size != gpu page size*/
942 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100943
944 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400945
946 return result;
947}
948
Christian Königf8991ba2016-09-16 15:36:49 +0200949/*
Christian König194d2162016-10-12 15:13:52 +0200950 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200951 *
952 * @adev: amdgpu_device pointer
953 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200954 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +0200955 *
Christian König194d2162016-10-12 15:13:52 +0200956 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200957 * Returns 0 for success, error for failure.
958 */
Christian König194d2162016-10-12 15:13:52 +0200959static int amdgpu_vm_update_level(struct amdgpu_device *adev,
960 struct amdgpu_vm *vm,
961 struct amdgpu_vm_pt *parent,
962 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963{
Christian Königf8991ba2016-09-16 15:36:49 +0200964 struct amdgpu_bo *shadow;
Christian König2d55e452016-02-08 17:37:38 +0100965 struct amdgpu_ring *ring;
Christian Königf8991ba2016-09-16 15:36:49 +0200966 uint64_t pd_addr, shadow_addr;
Christian König194d2162016-10-12 15:13:52 +0200967 uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
Christian Königf8991ba2016-09-16 15:36:49 +0200968 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100970 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200971 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +1000972 struct dma_fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800973
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 int r;
975
Christian König194d2162016-10-12 15:13:52 +0200976 if (!parent->entries)
977 return 0;
Christian König2d55e452016-02-08 17:37:38 +0100978 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
979
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980 /* padding, etc. */
981 ndw = 64;
982
983 /* assume the worst case */
Christian König194d2162016-10-12 15:13:52 +0200984 ndw += parent->last_entry_used * 6;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985
Christian König194d2162016-10-12 15:13:52 +0200986 pd_addr = amdgpu_bo_gpu_offset(parent->bo);
987
988 shadow = parent->bo->shadow;
Christian Königf8991ba2016-09-16 15:36:49 +0200989 if (shadow) {
990 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
991 if (r)
992 return r;
993 shadow_addr = amdgpu_bo_gpu_offset(shadow);
994 ndw *= 2;
995 } else {
996 shadow_addr = 0;
997 }
998
Christian Königd71518b2016-02-01 12:20:25 +0100999 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1000 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001002
Christian König27c5f362016-08-04 15:02:49 +02001003 memset(&params, 0, sizeof(params));
1004 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +02001005 params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006
Christian König194d2162016-10-12 15:13:52 +02001007 /* walk over the address space and update the directory */
1008 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1009 struct amdgpu_bo *bo = parent->entries[pt_idx].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 uint64_t pde, pt;
1011
1012 if (bo == NULL)
1013 continue;
1014
Christian König0fc86832016-09-16 11:46:23 +02001015 if (bo->shadow) {
Christian Königf8991ba2016-09-16 15:36:49 +02001016 struct amdgpu_bo *pt_shadow = bo->shadow;
Christian König0fc86832016-09-16 11:46:23 +02001017
Christian Königf8991ba2016-09-16 15:36:49 +02001018 r = amdgpu_ttm_bind(&pt_shadow->tbo,
1019 &pt_shadow->tbo.mem);
Christian König0fc86832016-09-16 11:46:23 +02001020 if (r)
1021 return r;
1022 }
1023
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024 pt = amdgpu_bo_gpu_offset(bo);
Christian König194d2162016-10-12 15:13:52 +02001025 if (parent->entries[pt_idx].addr == pt)
Christian Königf8991ba2016-09-16 15:36:49 +02001026 continue;
1027
Christian König194d2162016-10-12 15:13:52 +02001028 parent->entries[pt_idx].addr = pt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029
1030 pde = pd_addr + pt_idx * 8;
1031 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001032 ((last_pt + incr * count) != pt) ||
1033 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001034
1035 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -05001036 uint64_t pt_addr =
1037 amdgpu_vm_adjust_mc_addr(adev, last_pt);
1038
Christian Königf8991ba2016-09-16 15:36:49 +02001039 if (shadow)
1040 amdgpu_vm_do_set_ptes(&params,
1041 last_shadow,
Alex Xiee60f8db2017-03-09 11:36:26 -05001042 pt_addr, count,
Christian Königf8991ba2016-09-16 15:36:49 +02001043 incr,
1044 AMDGPU_PTE_VALID);
1045
Christian Königafef8b82016-08-12 13:29:18 +02001046 amdgpu_vm_do_set_ptes(&params, last_pde,
Alex Xiee60f8db2017-03-09 11:36:26 -05001047 pt_addr, count, incr,
Christian Königafef8b82016-08-12 13:29:18 +02001048 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001049 }
1050
1051 count = 1;
1052 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001053 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001054 last_pt = pt;
1055 } else {
1056 ++count;
1057 }
1058 }
1059
Christian Königf8991ba2016-09-16 15:36:49 +02001060 if (count) {
Alex Xiee60f8db2017-03-09 11:36:26 -05001061 uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt);
1062
Christian König67003a12016-10-12 14:46:26 +02001063 if (vm->root.bo->shadow)
Alex Xiee60f8db2017-03-09 11:36:26 -05001064 amdgpu_vm_do_set_ptes(&params, last_shadow, pt_addr,
Christian Königf8991ba2016-09-16 15:36:49 +02001065 count, incr, AMDGPU_PTE_VALID);
1066
Alex Xiee60f8db2017-03-09 11:36:26 -05001067 amdgpu_vm_do_set_ptes(&params, last_pde, pt_addr,
Christian Königafef8b82016-08-12 13:29:18 +02001068 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001069 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001070
Christian Königf8991ba2016-09-16 15:36:49 +02001071 if (params.ib->length_dw == 0) {
1072 amdgpu_job_free(job);
Christian König194d2162016-10-12 15:13:52 +02001073 } else {
1074 amdgpu_ring_pad_ib(ring, params.ib);
1075 amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv,
Christian Königf8991ba2016-09-16 15:36:49 +02001076 AMDGPU_FENCE_OWNER_VM);
Christian König194d2162016-10-12 15:13:52 +02001077 if (shadow)
1078 amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
1079 AMDGPU_FENCE_OWNER_VM);
Christian Königf8991ba2016-09-16 15:36:49 +02001080
Christian König194d2162016-10-12 15:13:52 +02001081 WARN_ON(params.ib->length_dw > ndw);
1082 r = amdgpu_job_submit(job, ring, &vm->entity,
1083 AMDGPU_FENCE_OWNER_VM, &fence);
1084 if (r)
1085 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001086
Christian König194d2162016-10-12 15:13:52 +02001087 amdgpu_bo_fence(parent->bo, fence, true);
1088 dma_fence_put(vm->last_dir_update);
1089 vm->last_dir_update = dma_fence_get(fence);
1090 dma_fence_put(fence);
1091 }
1092 /*
1093 * Recurse into the subdirectories. This recursion is harmless because
1094 * we only have a maximum of 5 layers.
1095 */
1096 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1097 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1098
1099 if (!entry->bo)
1100 continue;
1101
1102 r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
1103 if (r)
1104 return r;
1105 }
Christian Königf8991ba2016-09-16 15:36:49 +02001106
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001108
1109error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001110 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001111 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001112}
1113
Christian König194d2162016-10-12 15:13:52 +02001114/*
1115 * amdgpu_vm_update_directories - make sure that all directories are valid
1116 *
1117 * @adev: amdgpu_device pointer
1118 * @vm: requested vm
1119 *
1120 * Makes sure all directories are up to date.
1121 * Returns 0 for success, error for failure.
1122 */
1123int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1124 struct amdgpu_vm *vm)
1125{
1126 return amdgpu_vm_update_level(adev, vm, &vm->root, 0);
1127}
1128
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129/**
Christian König4e2cb642016-10-25 15:52:28 +02001130 * amdgpu_vm_find_pt - find the page table for an address
1131 *
1132 * @p: see amdgpu_pte_update_params definition
1133 * @addr: virtual address in question
1134 *
1135 * Find the page table BO for a virtual address, return NULL when none found.
1136 */
1137static struct amdgpu_bo *amdgpu_vm_get_pt(struct amdgpu_pte_update_params *p,
1138 uint64_t addr)
1139{
1140 struct amdgpu_vm_pt *entry = &p->vm->root;
1141 unsigned idx, level = p->adev->vm_manager.num_level;
1142
1143 while (entry->entries) {
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001144 idx = addr >> (p->adev->vm_manager.block_size * level--);
Christian König4e2cb642016-10-25 15:52:28 +02001145 idx %= amdgpu_bo_size(entry->bo) / 8;
1146 entry = &entry->entries[idx];
1147 }
1148
1149 if (level)
1150 return NULL;
1151
1152 return entry->bo;
1153}
1154
1155/**
Christian König92696dd2016-08-05 13:56:35 +02001156 * amdgpu_vm_update_ptes - make sure that page tables are valid
1157 *
1158 * @params: see amdgpu_pte_update_params definition
1159 * @vm: requested vm
1160 * @start: start of GPU address range
1161 * @end: end of GPU address range
1162 * @dst: destination address to map to, the next dst inside the function
1163 * @flags: mapping flags
1164 *
1165 * Update the page tables in the range @start - @end.
1166 */
1167static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001168 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001169 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001170{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001171 struct amdgpu_device *adev = params->adev;
1172 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001173
1174 uint64_t cur_pe_start, cur_nptes, cur_dst;
1175 uint64_t addr; /* next GPU address to be updated */
Christian König92696dd2016-08-05 13:56:35 +02001176 struct amdgpu_bo *pt;
1177 unsigned nptes; /* next number of ptes to be updated */
1178 uint64_t next_pe_start;
1179
1180 /* initialize the variables */
1181 addr = start;
Christian König4e2cb642016-10-25 15:52:28 +02001182 pt = amdgpu_vm_get_pt(params, addr);
Felix Kuehling1866bac2017-03-28 20:36:12 -04001183 if (!pt) {
1184 pr_err("PT not found, aborting update_ptes\n");
Christian König4e2cb642016-10-25 15:52:28 +02001185 return;
Felix Kuehling1866bac2017-03-28 20:36:12 -04001186 }
Christian König4e2cb642016-10-25 15:52:28 +02001187
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001188 if (params->shadow) {
1189 if (!pt->shadow)
1190 return;
Christian König914b4dc2016-09-28 12:27:37 +02001191 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001192 }
Christian König92696dd2016-08-05 13:56:35 +02001193 if ((addr & ~mask) == (end & ~mask))
1194 nptes = end - addr;
1195 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001196 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001197
1198 cur_pe_start = amdgpu_bo_gpu_offset(pt);
1199 cur_pe_start += (addr & mask) * 8;
1200 cur_nptes = nptes;
1201 cur_dst = dst;
1202
1203 /* for next ptb*/
1204 addr += nptes;
1205 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
1206
1207 /* walk over the address space and update the page tables */
1208 while (addr < end) {
Christian König4e2cb642016-10-25 15:52:28 +02001209 pt = amdgpu_vm_get_pt(params, addr);
Felix Kuehling1866bac2017-03-28 20:36:12 -04001210 if (!pt) {
1211 pr_err("PT not found, aborting update_ptes\n");
Christian König4e2cb642016-10-25 15:52:28 +02001212 return;
Felix Kuehling1866bac2017-03-28 20:36:12 -04001213 }
Christian König4e2cb642016-10-25 15:52:28 +02001214
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001215 if (params->shadow) {
1216 if (!pt->shadow)
1217 return;
Christian König914b4dc2016-09-28 12:27:37 +02001218 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001219 }
Christian König92696dd2016-08-05 13:56:35 +02001220
1221 if ((addr & ~mask) == (end & ~mask))
1222 nptes = end - addr;
1223 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001224 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001225
1226 next_pe_start = amdgpu_bo_gpu_offset(pt);
1227 next_pe_start += (addr & mask) * 8;
1228
Christian König96105e52016-08-12 12:59:59 +02001229 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
1230 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
Christian König92696dd2016-08-05 13:56:35 +02001231 /* The next ptb is consecutive to current ptb.
Christian Königafef8b82016-08-12 13:29:18 +02001232 * Don't call the update function now.
Christian König92696dd2016-08-05 13:56:35 +02001233 * Will update two ptbs together in future.
1234 */
1235 cur_nptes += nptes;
1236 } else {
Christian Königafef8b82016-08-12 13:29:18 +02001237 params->func(params, cur_pe_start, cur_dst, cur_nptes,
1238 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001239
1240 cur_pe_start = next_pe_start;
1241 cur_nptes = nptes;
1242 cur_dst = dst;
1243 }
1244
1245 /* for next ptb*/
1246 addr += nptes;
1247 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
1248 }
1249
Christian Königafef8b82016-08-12 13:29:18 +02001250 params->func(params, cur_pe_start, cur_dst, cur_nptes,
1251 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001252}
1253
1254/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001255 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1256 *
Christian König29efc4f2016-08-04 14:52:50 +02001257 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001258 * @vm: requested vm
1259 * @start: first PTE to handle
1260 * @end: last PTE to handle
1261 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263 */
Christian König27c5f362016-08-04 15:02:49 +02001264static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001265 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001266 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001267{
1268 /**
1269 * The MC L1 TLB supports variable sized pages, based on a fragment
1270 * field in the PTE. When this field is set to a non-zero value, page
1271 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1272 * flags are considered valid for all PTEs within the fragment range
1273 * and corresponding mappings are assumed to be physically contiguous.
1274 *
1275 * The L1 TLB can store a single PTE for the whole fragment,
1276 * significantly increasing the space available for translation
1277 * caching. This leads to large improvements in throughput when the
1278 * TLB is under pressure.
1279 *
1280 * The L2 TLB distributes small and large fragments into two
1281 * asymmetric partitions. The large fragment cache is significantly
1282 * larger. Thus, we try to use large fragments wherever possible.
1283 * Userspace can support this by aligning virtual base address and
1284 * allocation size to the fragment size.
1285 */
1286
Christian König80366172016-10-04 13:39:43 +02001287 /* SI and newer are optimized for 64KB */
1288 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
1289 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290
Christian König92696dd2016-08-05 13:56:35 +02001291 uint64_t frag_start = ALIGN(start, frag_align);
1292 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +01001293
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001294 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +02001295 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Christian König92696dd2016-08-05 13:56:35 +02001296 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297
Christian König49ac8a22016-10-13 15:09:08 +02001298 amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299 return;
1300 }
1301
1302 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +02001303 if (start != frag_start) {
Christian König49ac8a22016-10-13 15:09:08 +02001304 amdgpu_vm_update_ptes(params, start, frag_start,
Christian König92696dd2016-08-05 13:56:35 +02001305 dst, flags);
1306 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 }
1308
1309 /* handle the area in the middle */
Christian König49ac8a22016-10-13 15:09:08 +02001310 amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
Christian König80366172016-10-04 13:39:43 +02001311 flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001312
1313 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +02001314 if (frag_end != end) {
1315 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
Christian König49ac8a22016-10-13 15:09:08 +02001316 amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001317 }
1318}
1319
1320/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001321 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1322 *
1323 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001324 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001325 * @src: address where to copy page table entries from
1326 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001327 * @vm: requested vm
1328 * @start: start of mapped range
1329 * @last: last mapped entry
1330 * @flags: flags for the entries
1331 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001332 * @fence: optional resulting fence
1333 *
Christian Königa14faa62016-01-25 14:27:31 +01001334 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001335 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 */
1337static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001338 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001339 uint64_t src,
1340 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001342 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001343 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001344 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001345{
Christian König2d55e452016-02-08 17:37:38 +01001346 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001347 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001349 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001350 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001351 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001352 int r;
1353
Christian Königafef8b82016-08-12 13:29:18 +02001354 memset(&params, 0, sizeof(params));
1355 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001356 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001357 params.src = src;
1358
Christian König2d55e452016-02-08 17:37:38 +01001359 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001360
Christian Königa1e08d32016-01-26 11:40:46 +01001361 /* sync to everything on unmapping */
1362 if (!(flags & AMDGPU_PTE_VALID))
1363 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1364
Christian Königa14faa62016-01-25 14:27:31 +01001365 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366
1367 /*
1368 * reserve space for one command every (1 << BLOCK_SIZE)
1369 * entries or 2k dwords (whatever is smaller)
1370 */
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001371 ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372
1373 /* padding, etc. */
1374 ndw = 64;
1375
Christian Königb0456f92016-08-11 14:06:54 +02001376 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 /* only copy commands needed */
1378 ndw += ncmds * 7;
1379
Christian Königafef8b82016-08-12 13:29:18 +02001380 params.func = amdgpu_vm_do_copy_ptes;
1381
Christian Königb0456f92016-08-11 14:06:54 +02001382 } else if (pages_addr) {
1383 /* copy commands needed */
1384 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385
Christian Königb0456f92016-08-11 14:06:54 +02001386 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001387 ndw += nptes * 2;
1388
Christian Königafef8b82016-08-12 13:29:18 +02001389 params.func = amdgpu_vm_do_copy_ptes;
1390
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001391 } else {
1392 /* set page commands needed */
1393 ndw += ncmds * 10;
1394
1395 /* two extra commands for begin/end of fragment */
1396 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001397
1398 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001399 }
1400
Christian Königd71518b2016-02-01 12:20:25 +01001401 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1402 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001404
Christian König29efc4f2016-08-04 14:52:50 +02001405 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001406
Christian Königb0456f92016-08-11 14:06:54 +02001407 if (!src && pages_addr) {
1408 uint64_t *pte;
1409 unsigned i;
1410
1411 /* Put the PTEs at the end of the IB. */
1412 i = ndw - nptes * 2;
1413 pte= (uint64_t *)&(job->ibs->ptr[i]);
1414 params.src = job->ibs->gpu_addr + i * 4;
1415
1416 for (i = 0; i < nptes; ++i) {
1417 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1418 AMDGPU_GPU_PAGE_SIZE);
1419 pte[i] |= flags;
1420 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001421 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001422 }
1423
Christian König3cabaa52016-06-06 10:17:58 +02001424 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1425 if (r)
1426 goto error_free;
1427
Christian König67003a12016-10-12 14:46:26 +02001428 r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001429 owner);
1430 if (r)
1431 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432
Christian König67003a12016-10-12 14:46:26 +02001433 r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001434 if (r)
1435 goto error_free;
1436
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001437 params.shadow = true;
Christian König49ac8a22016-10-13 15:09:08 +02001438 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001439 params.shadow = false;
Christian König49ac8a22016-10-13 15:09:08 +02001440 amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001441
Christian König29efc4f2016-08-04 14:52:50 +02001442 amdgpu_ring_pad_ib(ring, params.ib);
1443 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001444 r = amdgpu_job_submit(job, ring, &vm->entity,
1445 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001446 if (r)
1447 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001448
Christian König67003a12016-10-12 14:46:26 +02001449 amdgpu_bo_fence(vm->root.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001450 dma_fence_put(*fence);
1451 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001452 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001453
1454error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001455 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001456 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001457}
1458
1459/**
Christian Königa14faa62016-01-25 14:27:31 +01001460 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1461 *
1462 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001463 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001464 * @gtt_flags: flags as they are used for GTT
1465 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001466 * @vm: requested vm
1467 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001468 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001469 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001470 * @fence: optional resulting fence
1471 *
1472 * Split the mapping into smaller chunks so that each update fits
1473 * into a SDMA IB.
1474 * Returns 0 for success, -EINVAL for failure.
1475 */
1476static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001477 struct dma_fence *exclusive,
Chunming Zhou6b777602016-09-21 16:19:19 +08001478 uint64_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +02001479 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001480 struct amdgpu_vm *vm,
1481 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001482 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001483 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001484 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001485{
Christian Königa9f87f62017-03-30 14:03:59 +02001486 uint64_t pfn, src = 0, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001487 int r;
1488
1489 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1490 * but in case of something, we filter the flags in first place
1491 */
1492 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1493 flags &= ~AMDGPU_PTE_READABLE;
1494 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1495 flags &= ~AMDGPU_PTE_WRITEABLE;
1496
Alex Xie15b31c52017-03-03 16:47:11 -05001497 flags &= ~AMDGPU_PTE_EXECUTABLE;
1498 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1499
Alex Xieb0fd18b2017-03-03 16:49:39 -05001500 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1501 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1502
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001503 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1504 (adev->asic_type >= CHIP_VEGA10)) {
1505 flags |= AMDGPU_PTE_PRT;
1506 flags &= ~AMDGPU_PTE_VALID;
1507 }
1508
Christian Königa14faa62016-01-25 14:27:31 +01001509 trace_amdgpu_vm_bo_update(mapping);
1510
Christian König63e0ba42016-08-16 17:38:37 +02001511 pfn = mapping->offset >> PAGE_SHIFT;
1512 if (nodes) {
1513 while (pfn >= nodes->size) {
1514 pfn -= nodes->size;
1515 ++nodes;
1516 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001517 }
Christian Königa14faa62016-01-25 14:27:31 +01001518
Christian König63e0ba42016-08-16 17:38:37 +02001519 do {
1520 uint64_t max_entries;
1521 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001522
Christian König63e0ba42016-08-16 17:38:37 +02001523 if (nodes) {
1524 addr = nodes->start << PAGE_SHIFT;
1525 max_entries = (nodes->size - pfn) *
1526 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1527 } else {
1528 addr = 0;
1529 max_entries = S64_MAX;
1530 }
Christian Königa14faa62016-01-25 14:27:31 +01001531
Christian König63e0ba42016-08-16 17:38:37 +02001532 if (pages_addr) {
1533 if (flags == gtt_flags)
1534 src = adev->gart.table_addr +
1535 (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
1536 else
1537 max_entries = min(max_entries, 16ull * 1024ull);
1538 addr = 0;
1539 } else if (flags & AMDGPU_PTE_VALID) {
1540 addr += adev->vm_manager.vram_base_offset;
1541 }
1542 addr += pfn << PAGE_SHIFT;
1543
Christian Königa9f87f62017-03-30 14:03:59 +02001544 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001545 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1546 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001547 start, last, flags, addr,
1548 fence);
1549 if (r)
1550 return r;
1551
Christian König63e0ba42016-08-16 17:38:37 +02001552 pfn += last - start + 1;
1553 if (nodes && nodes->size == pfn) {
1554 pfn = 0;
1555 ++nodes;
1556 }
Christian Königa14faa62016-01-25 14:27:31 +01001557 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001558
Christian Königa9f87f62017-03-30 14:03:59 +02001559 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001560
1561 return 0;
1562}
1563
1564/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001565 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1566 *
1567 * @adev: amdgpu_device pointer
1568 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001569 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001570 *
1571 * Fill in the page table entries for @bo_va.
1572 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001573 */
1574int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1575 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001576 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577{
1578 struct amdgpu_vm *vm = bo_va->vm;
1579 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001580 dma_addr_t *pages_addr = NULL;
Chunming Zhou6b777602016-09-21 16:19:19 +08001581 uint64_t gtt_flags, flags;
Christian König99e124f2016-08-16 14:43:17 +02001582 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001583 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001584 struct dma_fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585 int r;
1586
Christian Königa5f6b5b2017-01-30 11:01:38 +01001587 if (clear || !bo_va->bo) {
Christian König99e124f2016-08-16 14:43:17 +02001588 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001589 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001590 exclusive = NULL;
1591 } else {
Christian König8358dce2016-03-30 10:50:25 +02001592 struct ttm_dma_tt *ttm;
1593
Christian König99e124f2016-08-16 14:43:17 +02001594 mem = &bo_va->bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001595 nodes = mem->mm_node;
1596 if (mem->mem_type == TTM_PL_TT) {
Christian König8358dce2016-03-30 10:50:25 +02001597 ttm = container_of(bo_va->bo->tbo.ttm, struct
1598 ttm_dma_tt, ttm);
1599 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001600 }
Christian König3cabaa52016-06-06 10:17:58 +02001601 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602 }
1603
Christian Königa5f6b5b2017-01-30 11:01:38 +01001604 if (bo_va->bo) {
1605 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1606 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1607 adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
1608 flags : 0;
1609 } else {
1610 flags = 0x0;
1611 gtt_flags = ~0x0;
1612 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001613
Christian König7fc11952015-07-30 11:53:42 +02001614 spin_lock(&vm->status_lock);
1615 if (!list_empty(&bo_va->vm_status))
1616 list_splice_init(&bo_va->valids, &bo_va->invalids);
1617 spin_unlock(&vm->status_lock);
1618
1619 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001620 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1621 gtt_flags, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001622 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001623 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001624 if (r)
1625 return r;
1626 }
1627
Christian Königd6c10f62015-09-28 12:00:23 +02001628 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1629 list_for_each_entry(mapping, &bo_va->valids, list)
1630 trace_amdgpu_vm_bo_mapping(mapping);
1631
1632 list_for_each_entry(mapping, &bo_va->invalids, list)
1633 trace_amdgpu_vm_bo_mapping(mapping);
1634 }
1635
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001636 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001637 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638 list_del_init(&bo_va->vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001639 if (clear)
Christian König7fc11952015-07-30 11:53:42 +02001640 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641 spin_unlock(&vm->status_lock);
1642
1643 return 0;
1644}
1645
1646/**
Christian König284710f2017-01-30 11:09:31 +01001647 * amdgpu_vm_update_prt_state - update the global PRT state
1648 */
1649static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1650{
1651 unsigned long flags;
1652 bool enable;
1653
1654 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001655 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001656 adev->gart.gart_funcs->set_prt(adev, enable);
1657 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1658}
1659
1660/**
Christian König4388fc22017-03-13 10:13:36 +01001661 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001662 */
1663static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1664{
Christian König4388fc22017-03-13 10:13:36 +01001665 if (!adev->gart.gart_funcs->set_prt)
1666 return;
1667
Christian König451bc8e2017-02-14 16:02:52 +01001668 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1669 amdgpu_vm_update_prt_state(adev);
1670}
1671
1672/**
Christian König0b15f2f2017-02-14 15:47:03 +01001673 * amdgpu_vm_prt_put - drop a PRT user
1674 */
1675static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1676{
Christian König451bc8e2017-02-14 16:02:52 +01001677 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001678 amdgpu_vm_update_prt_state(adev);
1679}
1680
1681/**
Christian König451bc8e2017-02-14 16:02:52 +01001682 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001683 */
1684static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1685{
1686 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1687
Christian König0b15f2f2017-02-14 15:47:03 +01001688 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001689 kfree(cb);
1690}
1691
1692/**
Christian König451bc8e2017-02-14 16:02:52 +01001693 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1694 */
1695static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1696 struct dma_fence *fence)
1697{
Christian König4388fc22017-03-13 10:13:36 +01001698 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001699
Christian König4388fc22017-03-13 10:13:36 +01001700 if (!adev->gart.gart_funcs->set_prt)
1701 return;
1702
1703 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001704 if (!cb) {
1705 /* Last resort when we are OOM */
1706 if (fence)
1707 dma_fence_wait(fence, false);
1708
Dan Carpenter486a68f2017-04-03 21:41:39 +03001709 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001710 } else {
1711 cb->adev = adev;
1712 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1713 amdgpu_vm_prt_cb))
1714 amdgpu_vm_prt_cb(fence, &cb->cb);
1715 }
1716}
1717
1718/**
Christian König284710f2017-01-30 11:09:31 +01001719 * amdgpu_vm_free_mapping - free a mapping
1720 *
1721 * @adev: amdgpu_device pointer
1722 * @vm: requested vm
1723 * @mapping: mapping to be freed
1724 * @fence: fence of the unmap operation
1725 *
1726 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1727 */
1728static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1729 struct amdgpu_vm *vm,
1730 struct amdgpu_bo_va_mapping *mapping,
1731 struct dma_fence *fence)
1732{
Christian König451bc8e2017-02-14 16:02:52 +01001733 if (mapping->flags & AMDGPU_PTE_PRT)
1734 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001735 kfree(mapping);
1736}
1737
1738/**
Christian König451bc8e2017-02-14 16:02:52 +01001739 * amdgpu_vm_prt_fini - finish all prt mappings
1740 *
1741 * @adev: amdgpu_device pointer
1742 * @vm: requested vm
1743 *
1744 * Register a cleanup callback to disable PRT support after VM dies.
1745 */
1746static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1747{
Christian König67003a12016-10-12 14:46:26 +02001748 struct reservation_object *resv = vm->root.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001749 struct dma_fence *excl, **shared;
1750 unsigned i, shared_count;
1751 int r;
1752
1753 r = reservation_object_get_fences_rcu(resv, &excl,
1754 &shared_count, &shared);
1755 if (r) {
1756 /* Not enough memory to grab the fence list, as last resort
1757 * block for all the fences to complete.
1758 */
1759 reservation_object_wait_timeout_rcu(resv, true, false,
1760 MAX_SCHEDULE_TIMEOUT);
1761 return;
1762 }
1763
1764 /* Add a callback for each fence in the reservation object */
1765 amdgpu_vm_prt_get(adev);
1766 amdgpu_vm_add_prt_cb(adev, excl);
1767
1768 for (i = 0; i < shared_count; ++i) {
1769 amdgpu_vm_prt_get(adev);
1770 amdgpu_vm_add_prt_cb(adev, shared[i]);
1771 }
1772
1773 kfree(shared);
1774}
1775
1776/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001777 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1778 *
1779 * @adev: amdgpu_device pointer
1780 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001781 * @fence: optional resulting fence (unchanged if no work needed to be done
1782 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 *
1784 * Make sure all freed BOs are cleared in the PT.
1785 * Returns 0 for success.
1786 *
1787 * PTs have to be reserved and mutex must be locked!
1788 */
1789int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001790 struct amdgpu_vm *vm,
1791 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001792{
1793 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001794 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795 int r;
1796
1797 while (!list_empty(&vm->freed)) {
1798 mapping = list_first_entry(&vm->freed,
1799 struct amdgpu_bo_va_mapping, list);
1800 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001801
Christian Königfc6aa332017-04-19 14:41:19 +02001802 r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm,
1803 mapping->start, mapping->last,
1804 0, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001805 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001806 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001807 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808 return r;
Christian König284710f2017-01-30 11:09:31 +01001809 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001811
1812 if (fence && f) {
1813 dma_fence_put(*fence);
1814 *fence = f;
1815 } else {
1816 dma_fence_put(f);
1817 }
1818
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819 return 0;
1820
1821}
1822
1823/**
1824 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1825 *
1826 * @adev: amdgpu_device pointer
1827 * @vm: requested vm
1828 *
1829 * Make sure all invalidated BOs are cleared in the PT.
1830 * Returns 0 for success.
1831 *
1832 * PTs have to be reserved and mutex must be locked!
1833 */
1834int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001835 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001836{
monk.liucfe2c972015-05-26 15:01:54 +08001837 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001838 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001839
1840 spin_lock(&vm->status_lock);
1841 while (!list_empty(&vm->invalidated)) {
1842 bo_va = list_first_entry(&vm->invalidated,
1843 struct amdgpu_bo_va, vm_status);
1844 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001845
Christian König99e124f2016-08-16 14:43:17 +02001846 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001847 if (r)
1848 return r;
1849
1850 spin_lock(&vm->status_lock);
1851 }
1852 spin_unlock(&vm->status_lock);
1853
monk.liucfe2c972015-05-26 15:01:54 +08001854 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001855 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001856
1857 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858}
1859
1860/**
1861 * amdgpu_vm_bo_add - add a bo to a specific vm
1862 *
1863 * @adev: amdgpu_device pointer
1864 * @vm: requested vm
1865 * @bo: amdgpu buffer object
1866 *
Christian König8843dbb2016-01-26 12:17:11 +01001867 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868 * Add @bo to the list of bos associated with the vm
1869 * Returns newly added bo_va or NULL for failure
1870 *
1871 * Object has to be reserved!
1872 */
1873struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1874 struct amdgpu_vm *vm,
1875 struct amdgpu_bo *bo)
1876{
1877 struct amdgpu_bo_va *bo_va;
1878
1879 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1880 if (bo_va == NULL) {
1881 return NULL;
1882 }
1883 bo_va->vm = vm;
1884 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001885 bo_va->ref_count = 1;
1886 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001887 INIT_LIST_HEAD(&bo_va->valids);
1888 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001890
Christian Königa5f6b5b2017-01-30 11:01:38 +01001891 if (bo)
1892 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001893
1894 return bo_va;
1895}
1896
1897/**
1898 * amdgpu_vm_bo_map - map bo inside a vm
1899 *
1900 * @adev: amdgpu_device pointer
1901 * @bo_va: bo_va to store the address
1902 * @saddr: where to map the BO
1903 * @offset: requested offset in the BO
1904 * @flags: attributes of pages (read/write/valid/etc.)
1905 *
1906 * Add a mapping of the BO at the specefied addr into the VM.
1907 * Returns 0 for success, error for failure.
1908 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001909 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001910 */
1911int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1912 struct amdgpu_bo_va *bo_va,
1913 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001914 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001915{
Christian Königa9f87f62017-03-30 14:03:59 +02001916 struct amdgpu_bo_va_mapping *mapping, *tmp;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001917 struct amdgpu_vm *vm = bo_va->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001918 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001919
Christian König0be52de2015-05-18 14:37:27 +02001920 /* validate the parameters */
1921 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001922 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001923 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001924
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001925 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001926 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001927 if (saddr >= eaddr ||
1928 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001930
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 saddr /= AMDGPU_GPU_PAGE_SIZE;
1932 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1933
Christian Königa9f87f62017-03-30 14:03:59 +02001934 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1935 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001936 /* bo and tmp overlap, invalid addr */
1937 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königa9f87f62017-03-30 14:03:59 +02001938 "0x%010Lx-0x%010Lx\n", bo_va->bo, saddr, eaddr,
1939 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01001940 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001941 }
1942
1943 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001944 if (!mapping)
1945 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001946
1947 INIT_LIST_HEAD(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02001948 mapping->start = saddr;
1949 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001950 mapping->offset = offset;
1951 mapping->flags = flags;
1952
Christian König7fc11952015-07-30 11:53:42 +02001953 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02001954 amdgpu_vm_it_insert(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001955
Christian König4388fc22017-03-13 10:13:36 +01001956 if (flags & AMDGPU_PTE_PRT)
1957 amdgpu_vm_prt_get(adev);
1958
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001959 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001960}
1961
1962/**
Christian König80f95c52017-03-13 10:13:39 +01001963 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1964 *
1965 * @adev: amdgpu_device pointer
1966 * @bo_va: bo_va to store the address
1967 * @saddr: where to map the BO
1968 * @offset: requested offset in the BO
1969 * @flags: attributes of pages (read/write/valid/etc.)
1970 *
1971 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1972 * mappings as we do so.
1973 * Returns 0 for success, error for failure.
1974 *
1975 * Object has to be reserved and unreserved outside!
1976 */
1977int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1978 struct amdgpu_bo_va *bo_va,
1979 uint64_t saddr, uint64_t offset,
1980 uint64_t size, uint64_t flags)
1981{
1982 struct amdgpu_bo_va_mapping *mapping;
1983 struct amdgpu_vm *vm = bo_va->vm;
1984 uint64_t eaddr;
1985 int r;
1986
1987 /* validate the parameters */
1988 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1989 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1990 return -EINVAL;
1991
1992 /* make sure object fit at this offset */
1993 eaddr = saddr + size - 1;
1994 if (saddr >= eaddr ||
1995 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
1996 return -EINVAL;
1997
1998 /* Allocate all the needed memory */
1999 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2000 if (!mapping)
2001 return -ENOMEM;
2002
2003 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
2004 if (r) {
2005 kfree(mapping);
2006 return r;
2007 }
2008
2009 saddr /= AMDGPU_GPU_PAGE_SIZE;
2010 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2011
Christian Königa9f87f62017-03-30 14:03:59 +02002012 mapping->start = saddr;
2013 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002014 mapping->offset = offset;
2015 mapping->flags = flags;
2016
2017 list_add(&mapping->list, &bo_va->invalids);
Christian Königa9f87f62017-03-30 14:03:59 +02002018 amdgpu_vm_it_insert(mapping, &vm->va);
Christian König80f95c52017-03-13 10:13:39 +01002019
2020 if (flags & AMDGPU_PTE_PRT)
2021 amdgpu_vm_prt_get(adev);
2022
2023 return 0;
2024}
2025
2026/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2028 *
2029 * @adev: amdgpu_device pointer
2030 * @bo_va: bo_va to remove the address from
2031 * @saddr: where to the BO is mapped
2032 *
2033 * Remove a mapping of the BO at the specefied addr from the VM.
2034 * Returns 0 for success, error for failure.
2035 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002036 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037 */
2038int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2039 struct amdgpu_bo_va *bo_va,
2040 uint64_t saddr)
2041{
2042 struct amdgpu_bo_va_mapping *mapping;
2043 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02002044 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002045
Christian König6c7fc502015-06-05 20:56:17 +02002046 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002047
Christian König7fc11952015-07-30 11:53:42 +02002048 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002049 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050 break;
2051 }
2052
Christian König7fc11952015-07-30 11:53:42 +02002053 if (&mapping->list == &bo_va->valids) {
2054 valid = false;
2055
2056 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002057 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002058 break;
2059 }
2060
Christian König32b41ac2016-03-08 18:03:27 +01002061 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002062 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063 }
Christian König32b41ac2016-03-08 18:03:27 +01002064
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002066 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002067 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068
Christian Könige17841b2016-03-08 17:52:01 +01002069 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002070 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002071 else
Christian König284710f2017-01-30 11:09:31 +01002072 amdgpu_vm_free_mapping(adev, vm, mapping,
2073 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002074
2075 return 0;
2076}
2077
2078/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002079 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2080 *
2081 * @adev: amdgpu_device pointer
2082 * @vm: VM structure to use
2083 * @saddr: start of the range
2084 * @size: size of the range
2085 *
2086 * Remove all mappings in a range, split them as appropriate.
2087 * Returns 0 for success, error for failure.
2088 */
2089int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2090 struct amdgpu_vm *vm,
2091 uint64_t saddr, uint64_t size)
2092{
2093 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002094 LIST_HEAD(removed);
2095 uint64_t eaddr;
2096
2097 eaddr = saddr + size - 1;
2098 saddr /= AMDGPU_GPU_PAGE_SIZE;
2099 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2100
2101 /* Allocate all the needed memory */
2102 before = kzalloc(sizeof(*before), GFP_KERNEL);
2103 if (!before)
2104 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002105 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002106
2107 after = kzalloc(sizeof(*after), GFP_KERNEL);
2108 if (!after) {
2109 kfree(before);
2110 return -ENOMEM;
2111 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002112 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002113
2114 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002115 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2116 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002117 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002118 if (tmp->start < saddr) {
2119 before->start = tmp->start;
2120 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002121 before->offset = tmp->offset;
2122 before->flags = tmp->flags;
2123 list_add(&before->list, &tmp->list);
2124 }
2125
2126 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002127 if (tmp->last > eaddr) {
2128 after->start = eaddr + 1;
2129 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002130 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002131 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002132 after->flags = tmp->flags;
2133 list_add(&after->list, &tmp->list);
2134 }
2135
2136 list_del(&tmp->list);
2137 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002138
2139 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002140 }
2141
2142 /* And free them up */
2143 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002144 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002145 list_del(&tmp->list);
2146
Christian Königa9f87f62017-03-30 14:03:59 +02002147 if (tmp->start < saddr)
2148 tmp->start = saddr;
2149 if (tmp->last > eaddr)
2150 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002151
2152 list_add(&tmp->list, &vm->freed);
2153 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2154 }
2155
Junwei Zhang27f6d612017-03-16 16:09:24 +08002156 /* Insert partial mapping before the range */
2157 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002158 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002159 if (before->flags & AMDGPU_PTE_PRT)
2160 amdgpu_vm_prt_get(adev);
2161 } else {
2162 kfree(before);
2163 }
2164
2165 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002166 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002167 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002168 if (after->flags & AMDGPU_PTE_PRT)
2169 amdgpu_vm_prt_get(adev);
2170 } else {
2171 kfree(after);
2172 }
2173
2174 return 0;
2175}
2176
2177/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002178 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2179 *
2180 * @adev: amdgpu_device pointer
2181 * @bo_va: requested bo_va
2182 *
Christian König8843dbb2016-01-26 12:17:11 +01002183 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002184 *
2185 * Object have to be reserved!
2186 */
2187void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2188 struct amdgpu_bo_va *bo_va)
2189{
2190 struct amdgpu_bo_va_mapping *mapping, *next;
2191 struct amdgpu_vm *vm = bo_va->vm;
2192
2193 list_del(&bo_va->bo_list);
2194
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002195 spin_lock(&vm->status_lock);
2196 list_del(&bo_va->vm_status);
2197 spin_unlock(&vm->status_lock);
2198
Christian König7fc11952015-07-30 11:53:42 +02002199 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002200 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002201 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002202 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002203 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002204 }
Christian König7fc11952015-07-30 11:53:42 +02002205 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2206 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002207 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002208 amdgpu_vm_free_mapping(adev, vm, mapping,
2209 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002210 }
Christian König32b41ac2016-03-08 18:03:27 +01002211
Chris Wilsonf54d1862016-10-25 13:00:45 +01002212 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214}
2215
2216/**
2217 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2218 *
2219 * @adev: amdgpu_device pointer
2220 * @vm: requested vm
2221 * @bo: amdgpu buffer object
2222 *
Christian König8843dbb2016-01-26 12:17:11 +01002223 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002224 */
2225void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
2226 struct amdgpu_bo *bo)
2227{
2228 struct amdgpu_bo_va *bo_va;
2229
2230 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02002231 spin_lock(&bo_va->vm->status_lock);
2232 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002233 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02002234 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002235 }
2236}
2237
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002238static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2239{
2240 /* Total bits covered by PD + PTs */
2241 unsigned bits = ilog2(vm_size) + 18;
2242
2243 /* Make sure the PD is 4K in size up to 8GB address space.
2244 Above that split equal between PD and PTs */
2245 if (vm_size <= 8)
2246 return (bits - 9);
2247 else
2248 return ((bits + 3) / 2);
2249}
2250
2251/**
2252 * amdgpu_vm_adjust_size - adjust vm size and block size
2253 *
2254 * @adev: amdgpu_device pointer
2255 * @vm_size: the default vm size if it's set auto
2256 */
2257void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size)
2258{
2259 /* adjust vm size firstly */
2260 if (amdgpu_vm_size == -1)
2261 adev->vm_manager.vm_size = vm_size;
2262 else
2263 adev->vm_manager.vm_size = amdgpu_vm_size;
2264
2265 /* block size depends on vm size */
2266 if (amdgpu_vm_block_size == -1)
2267 adev->vm_manager.block_size =
2268 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2269 else
2270 adev->vm_manager.block_size = amdgpu_vm_block_size;
2271
2272 DRM_INFO("vm size is %llu GB, block size is %u-bit\n",
2273 adev->vm_manager.vm_size, adev->vm_manager.block_size);
2274}
2275
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002276/**
2277 * amdgpu_vm_init - initialize a vm instance
2278 *
2279 * @adev: amdgpu_device pointer
2280 * @vm: requested vm
2281 *
Christian König8843dbb2016-01-26 12:17:11 +01002282 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002283 */
2284int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2285{
2286 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002287 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002288 unsigned ring_instance;
2289 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002290 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002291 int r, i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002293 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002294 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002295 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2296 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002297 spin_lock_init(&vm->status_lock);
2298 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02002299 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002300 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002301
Christian König2bd9ccf2016-02-01 12:53:58 +01002302 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002303
2304 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2305 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2306 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002307 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2308 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2309 rq, amdgpu_sched_jobs);
2310 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002311 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002312
Christian Königa24960f2016-10-12 13:20:52 +02002313 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002314
Christian Königf566ceb2016-10-27 20:04:38 +02002315 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002316 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08002317 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
Christian König03f48dd2016-08-15 17:00:22 +02002318 AMDGPU_GEM_CREATE_SHADOW |
Christian König617859e2016-11-17 15:40:02 +01002319 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2320 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Christian König67003a12016-10-12 14:46:26 +02002321 NULL, NULL, &vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002322 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002323 goto error_free_sched_entity;
2324
Christian König67003a12016-10-12 14:46:26 +02002325 r = amdgpu_bo_reserve(vm->root.bo, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01002326 if (r)
Christian König67003a12016-10-12 14:46:26 +02002327 goto error_free_root;
Christian König2bd9ccf2016-02-01 12:53:58 +01002328
Christian König5a712a82016-06-21 16:28:15 +02002329 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König67003a12016-10-12 14:46:26 +02002330 amdgpu_bo_unreserve(vm->root.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331
2332 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002333
Christian König67003a12016-10-12 14:46:26 +02002334error_free_root:
2335 amdgpu_bo_unref(&vm->root.bo->shadow);
2336 amdgpu_bo_unref(&vm->root.bo);
2337 vm->root.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002338
2339error_free_sched_entity:
2340 amd_sched_entity_fini(&ring->sched, &vm->entity);
2341
2342 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002343}
2344
2345/**
Christian Königf566ceb2016-10-27 20:04:38 +02002346 * amdgpu_vm_free_levels - free PD/PT levels
2347 *
2348 * @level: PD/PT starting level to free
2349 *
2350 * Free the page directory or page table level and all sub levels.
2351 */
2352static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2353{
2354 unsigned i;
2355
2356 if (level->bo) {
2357 amdgpu_bo_unref(&level->bo->shadow);
2358 amdgpu_bo_unref(&level->bo);
2359 }
2360
2361 if (level->entries)
2362 for (i = 0; i <= level->last_entry_used; i++)
2363 amdgpu_vm_free_levels(&level->entries[i]);
2364
2365 drm_free_large(level->entries);
2366}
2367
2368/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002369 * amdgpu_vm_fini - tear down a vm instance
2370 *
2371 * @adev: amdgpu_device pointer
2372 * @vm: requested vm
2373 *
Christian König8843dbb2016-01-26 12:17:11 +01002374 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002375 * Unbind the VM and remove all bos from the vm bo list
2376 */
2377void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2378{
2379 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002380 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002381 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382
Christian König2d55e452016-02-08 17:37:38 +01002383 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002384
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002385 if (!RB_EMPTY_ROOT(&vm->va)) {
2386 dev_err(adev->dev, "still active bo inside vm\n");
2387 }
Christian Königa9f87f62017-03-30 14:03:59 +02002388 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002389 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002390 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002391 kfree(mapping);
2392 }
2393 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002394 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002395 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002396 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002397 }
Christian König284710f2017-01-30 11:09:31 +01002398
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002400 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002401 }
2402
Christian Königf566ceb2016-10-27 20:04:38 +02002403 amdgpu_vm_free_levels(&vm->root);
Christian Königa24960f2016-10-12 13:20:52 +02002404 dma_fence_put(vm->last_dir_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002405 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2406 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002407}
Christian Königea89f8c2015-11-15 20:52:06 +01002408
2409/**
Christian Königa9a78b32016-01-21 10:19:11 +01002410 * amdgpu_vm_manager_init - init the VM manager
2411 *
2412 * @adev: amdgpu_device pointer
2413 *
2414 * Initialize the VM manager structures
2415 */
2416void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2417{
Christian König76456702017-04-06 17:52:39 +02002418 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002419
Christian König76456702017-04-06 17:52:39 +02002420 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2421 struct amdgpu_vm_id_manager *id_mgr =
2422 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002423
Christian König76456702017-04-06 17:52:39 +02002424 mutex_init(&id_mgr->lock);
2425 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002426 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002427
2428 /* skip over VMID 0, since it is the system VM */
2429 for (j = 1; j < id_mgr->num_ids; ++j) {
2430 amdgpu_vm_reset_id(adev, i, j);
2431 amdgpu_sync_create(&id_mgr->ids[i].active);
2432 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2433 }
Christian König971fe9a92016-03-01 15:09:25 +01002434 }
Christian König2d55e452016-02-08 17:37:38 +01002435
Chris Wilsonf54d1862016-10-25 13:00:45 +01002436 adev->vm_manager.fence_context =
2437 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002438 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2439 adev->vm_manager.seqno[i] = 0;
2440
Christian König2d55e452016-02-08 17:37:38 +01002441 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002442 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002443 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002444 atomic_set(&adev->vm_manager.num_prt_users, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01002445}
2446
2447/**
Christian Königea89f8c2015-11-15 20:52:06 +01002448 * amdgpu_vm_manager_fini - cleanup VM manager
2449 *
2450 * @adev: amdgpu_device pointer
2451 *
2452 * Cleanup the VM manager and free resources.
2453 */
2454void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2455{
Christian König76456702017-04-06 17:52:39 +02002456 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002457
Christian König76456702017-04-06 17:52:39 +02002458 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2459 struct amdgpu_vm_id_manager *id_mgr =
2460 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002461
Christian König76456702017-04-06 17:52:39 +02002462 mutex_destroy(&id_mgr->lock);
2463 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2464 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2465
2466 amdgpu_sync_free(&id->active);
2467 dma_fence_put(id->flushed_updates);
2468 dma_fence_put(id->last_flush);
2469 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002470 }
Christian Königea89f8c2015-11-15 20:52:06 +01002471}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002472
2473int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2474{
2475 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002476 struct amdgpu_device *adev = dev->dev_private;
2477 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2478 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002479
2480 switch (args->in.op) {
2481 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002482 /* current, we only have requirement to reserve vmid from gfxhub */
2483 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2484 AMDGPU_GFXHUB);
2485 if (r)
2486 return r;
2487 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002488 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002489 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002490 break;
2491 default:
2492 return -EINVAL;
2493 }
2494
2495 return 0;
2496}