blob: 2da08027ff29b68ec84c7c5e394f18bb638e2dbc [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>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <drm/drmP.h>
30#include <drm/amdgpu_drm.h>
31#include "amdgpu.h"
32#include "amdgpu_trace.h"
33
34/*
35 * GPUVM
36 * GPUVM is similar to the legacy gart on older asics, however
37 * rather than there being a single global gart table
38 * for the entire GPU, there are multiple VM page tables active
39 * at any given time. The VM page tables can contain a mix
40 * vram pages and system memory pages and system memory pages
41 * can be mapped as snooped (cached system pages) or unsnooped
42 * (uncached system pages).
43 * Each VM has an ID associated with it and there is a page table
44 * associated with each VMID. When execting a command buffer,
45 * the kernel tells the the ring what VMID to use for that command
46 * buffer. VMIDs are allocated dynamically as commands are submitted.
47 * The userspace drivers maintain their own address space and the kernel
48 * sets up their pages tables accordingly when they submit their
49 * command buffers and a VMID is assigned.
50 * Cayman/Trinity support up to 8 active VMs at any given time;
51 * SI supports 16.
52 */
53
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040054/* Local structure. Encapsulate some VM table update parameters to reduce
55 * the number of function parameters
56 */
Christian König29efc4f2016-08-04 14:52:50 +020057struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020058 /* amdgpu device we do this update for */
59 struct amdgpu_device *adev;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040060 /* address where to copy page table entries from */
61 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040062 /* indirect buffer to fill with commands */
63 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020064 /* Function which actually does the update */
65 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
66 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080067 uint64_t flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +080068 /* indicate update pt or its shadow */
69 bool shadow;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040070};
71
Christian König284710f2017-01-30 11:09:31 +010072/* Helper to disable partial resident texture feature from a fence callback */
73struct amdgpu_prt_cb {
74 struct amdgpu_device *adev;
75 struct dma_fence_cb cb;
76};
77
Alex Deucherd38ceaf2015-04-20 16:55:21 -040078/**
79 * amdgpu_vm_num_pde - return the number of page directory entries
80 *
81 * @adev: amdgpu_device pointer
82 *
Christian König8843dbb2016-01-26 12:17:11 +010083 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084 */
85static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
86{
87 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
88}
89
90/**
91 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
92 *
93 * @adev: amdgpu_device pointer
94 *
Christian König8843dbb2016-01-26 12:17:11 +010095 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 */
97static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
98{
99 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
100}
101
102/**
Christian König56467eb2015-12-11 15:16:32 +0100103 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 *
105 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100106 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100107 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 *
109 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100110 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111 */
Christian König56467eb2015-12-11 15:16:32 +0100112void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
113 struct list_head *validated,
114 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115{
Christian König56467eb2015-12-11 15:16:32 +0100116 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +0100117 entry->priority = 0;
118 entry->tv.bo = &vm->page_directory->tbo;
119 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100120 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100121 list_add(&entry->tv.head, validated);
122}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123
Christian König56467eb2015-12-11 15:16:32 +0100124/**
Christian Königf7da30d2016-09-28 12:03:04 +0200125 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100126 *
Christian König5a712a82016-06-21 16:28:15 +0200127 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100128 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200129 * @validate: callback to do the validation
130 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 *
Christian Königf7da30d2016-09-28 12:03:04 +0200132 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 */
Christian Königf7da30d2016-09-28 12:03:04 +0200134int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
135 int (*validate)(void *p, struct amdgpu_bo *bo),
136 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137{
Christian König5a712a82016-06-21 16:28:15 +0200138 uint64_t num_evictions;
Christian Königee1782c2015-12-11 21:01:23 +0100139 unsigned i;
Christian Königf7da30d2016-09-28 12:03:04 +0200140 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141
Christian König5a712a82016-06-21 16:28:15 +0200142 /* We only need to validate the page tables
143 * if they aren't already valid.
144 */
145 num_evictions = atomic64_read(&adev->num_evictions);
146 if (num_evictions == vm->last_eviction_counter)
Christian Königf7da30d2016-09-28 12:03:04 +0200147 return 0;
Christian König5a712a82016-06-21 16:28:15 +0200148
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100150 for (i = 0; i <= vm->max_pde_used; ++i) {
Christian König914b4dc2016-09-28 12:27:37 +0200151 struct amdgpu_bo *bo = vm->page_tables[i].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152
Christian König914b4dc2016-09-28 12:27:37 +0200153 if (!bo)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154 continue;
155
Christian König914b4dc2016-09-28 12:27:37 +0200156 r = validate(param, bo);
Christian Königf7da30d2016-09-28 12:03:04 +0200157 if (r)
158 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 }
Christian Königeceb8a12016-01-11 15:35:21 +0100160
Christian Königf7da30d2016-09-28 12:03:04 +0200161 return 0;
Christian Königeceb8a12016-01-11 15:35:21 +0100162}
163
164/**
165 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
166 *
167 * @adev: amdgpu device instance
168 * @vm: vm providing the BOs
169 *
170 * Move the PT BOs to the tail of the LRU.
171 */
172void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
173 struct amdgpu_vm *vm)
174{
175 struct ttm_bo_global *glob = adev->mman.bdev.glob;
176 unsigned i;
177
178 spin_lock(&glob->lru_lock);
179 for (i = 0; i <= vm->max_pde_used; ++i) {
Christian König914b4dc2016-09-28 12:27:37 +0200180 struct amdgpu_bo *bo = vm->page_tables[i].bo;
Christian Königeceb8a12016-01-11 15:35:21 +0100181
Christian König914b4dc2016-09-28 12:27:37 +0200182 if (!bo)
Christian Königeceb8a12016-01-11 15:35:21 +0100183 continue;
184
Christian König914b4dc2016-09-28 12:27:37 +0200185 ttm_bo_move_to_lru_tail(&bo->tbo);
Christian Königeceb8a12016-01-11 15:35:21 +0100186 }
187 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188}
189
Christian König663e4572017-03-13 10:13:37 +0100190/**
191 * amdgpu_vm_alloc_pts - Allocate page tables.
192 *
193 * @adev: amdgpu_device pointer
194 * @vm: VM to allocate page tables for
195 * @saddr: Start address which needs to be allocated
196 * @size: Size from start address we need.
197 *
198 * Make sure the page tables are allocated.
199 */
200int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
201 struct amdgpu_vm *vm,
202 uint64_t saddr, uint64_t size)
203{
204 unsigned last_pfn, pt_idx;
205 uint64_t eaddr;
206 int r;
207
208 /* validate the parameters */
209 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
210 return -EINVAL;
211
212 eaddr = saddr + size - 1;
213 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
214 if (last_pfn >= adev->vm_manager.max_pfn) {
215 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
216 last_pfn, adev->vm_manager.max_pfn);
217 return -EINVAL;
218 }
219
220 saddr /= AMDGPU_GPU_PAGE_SIZE;
221 eaddr /= AMDGPU_GPU_PAGE_SIZE;
222
223 saddr >>= amdgpu_vm_block_size;
224 eaddr >>= amdgpu_vm_block_size;
225
226 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
227
228 if (eaddr > vm->max_pde_used)
229 vm->max_pde_used = eaddr;
230
231 /* walk over the address space and allocate the page tables */
232 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
233 struct reservation_object *resv = vm->page_directory->tbo.resv;
234 struct amdgpu_bo *pt;
235
236 if (vm->page_tables[pt_idx].bo)
237 continue;
238
239 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
240 AMDGPU_GPU_PAGE_SIZE, true,
241 AMDGPU_GEM_DOMAIN_VRAM,
242 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
243 AMDGPU_GEM_CREATE_SHADOW |
244 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
245 AMDGPU_GEM_CREATE_VRAM_CLEARED,
246 NULL, resv, &pt);
247 if (r)
248 return r;
249
250 /* Keep a reference to the page table to avoid freeing
251 * them up in the wrong order.
252 */
253 pt->parent = amdgpu_bo_ref(vm->page_directory);
254
255 vm->page_tables[pt_idx].bo = pt;
256 vm->page_tables[pt_idx].addr = 0;
257 }
258
259 return 0;
260}
261
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800262static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
263 struct amdgpu_vm_id *id)
264{
265 return id->current_gpu_reset_count !=
266 atomic_read(&adev->gpu_reset_counter) ? true : false;
267}
268
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269/**
270 * amdgpu_vm_grab_id - allocate the next free VMID
271 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200273 * @ring: ring we want to submit job to
274 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100275 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400276 *
Christian König7f8a5292015-07-20 16:09:40 +0200277 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278 */
Christian König7f8a5292015-07-20 16:09:40 +0200279int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100280 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800281 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283 struct amdgpu_device *adev = ring->adev;
Christian König090b7672016-07-08 10:21:02 +0200284 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100285 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200286 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100287 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200288 unsigned i;
289 int r = 0;
290
291 fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
292 GFP_KERNEL);
293 if (!fences)
294 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400295
Christian König94dd0a42016-01-18 17:01:42 +0100296 mutex_lock(&adev->vm_manager.lock);
297
Christian König36fd7c52016-05-23 15:30:08 +0200298 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200299 i = 0;
Christian König8d76001e2016-05-23 16:00:32 +0200300 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200301 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
302 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200303 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200304 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200305 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100306
Christian König1fbb2e92016-06-01 10:47:36 +0200307 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König8d76001e2016-05-23 16:00:32 +0200308 if (&idle->list == &adev->vm_manager.ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200309 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
310 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100311 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200312 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200313
Christian König1fbb2e92016-06-01 10:47:36 +0200314 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100315 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200316
Chris Wilsonf54d1862016-10-25 13:00:45 +0100317 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200318 seqno, true);
319 if (!array) {
320 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100321 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200322 kfree(fences);
323 r = -ENOMEM;
324 goto error;
325 }
Christian König8d76001e2016-05-23 16:00:32 +0200326
Christian König8d76001e2016-05-23 16:00:32 +0200327
Christian König1fbb2e92016-06-01 10:47:36 +0200328 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100329 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200330 if (r)
331 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200332
Christian König1fbb2e92016-06-01 10:47:36 +0200333 mutex_unlock(&adev->vm_manager.lock);
334 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200335
Christian König1fbb2e92016-06-01 10:47:36 +0200336 }
337 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200338
Chunming Zhoufd53be32016-07-01 17:59:01 +0800339 job->vm_needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200340 /* Check if we can use a VMID already assigned to this VM */
341 i = ring->idx;
342 do {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100343 struct dma_fence *flushed;
Christian König8d76001e2016-05-23 16:00:32 +0200344
Christian König1fbb2e92016-06-01 10:47:36 +0200345 id = vm->ids[i++];
346 if (i == AMDGPU_MAX_RINGS)
347 i = 0;
348
349 /* Check all the prerequisites to using this VMID */
350 if (!id)
351 continue;
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800352 if (amdgpu_vm_is_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800353 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200354
355 if (atomic64_read(&id->owner) != vm->client_id)
356 continue;
357
Chunming Zhoufd53be32016-07-01 17:59:01 +0800358 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200359 continue;
360
Christian König090b7672016-07-08 10:21:02 +0200361 if (!id->last_flush)
362 continue;
363
364 if (id->last_flush->context != fence_context &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100365 !dma_fence_is_signaled(id->last_flush))
Christian König1fbb2e92016-06-01 10:47:36 +0200366 continue;
367
368 flushed = id->flushed_updates;
369 if (updates &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100370 (!flushed || dma_fence_is_later(updates, flushed)))
Christian König1fbb2e92016-06-01 10:47:36 +0200371 continue;
372
Christian König3dab83b2016-06-01 13:31:17 +0200373 /* Good we can use this VMID. Remember this submission as
374 * user of the VMID.
375 */
Christian König1fbb2e92016-06-01 10:47:36 +0200376 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
377 if (r)
378 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200379
Chunming Zhou6adb0512016-06-27 17:06:01 +0800380 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König1fbb2e92016-06-01 10:47:36 +0200381 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
382 vm->ids[ring->idx] = id;
Christian König8d76001e2016-05-23 16:00:32 +0200383
Chunming Zhoufd53be32016-07-01 17:59:01 +0800384 job->vm_id = id - adev->vm_manager.ids;
385 job->vm_needs_flush = false;
Christian König0c0fdf12016-07-08 10:48:24 +0200386 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König8d76001e2016-05-23 16:00:32 +0200387
Christian König1fbb2e92016-06-01 10:47:36 +0200388 mutex_unlock(&adev->vm_manager.lock);
389 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200390
Christian König1fbb2e92016-06-01 10:47:36 +0200391 } while (i != ring->idx);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800392
Christian König1fbb2e92016-06-01 10:47:36 +0200393 /* Still no ID to use? Then use the idle one found earlier */
394 id = idle;
395
396 /* Remember this submission as user of the VMID */
397 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100398 if (r)
399 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100400
Chris Wilsonf54d1862016-10-25 13:00:45 +0100401 dma_fence_put(id->first);
402 id->first = dma_fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100403
Chris Wilsonf54d1862016-10-25 13:00:45 +0100404 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100405 id->last_flush = NULL;
406
Chris Wilsonf54d1862016-10-25 13:00:45 +0100407 dma_fence_put(id->flushed_updates);
408 id->flushed_updates = dma_fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100409
Chunming Zhoufd53be32016-07-01 17:59:01 +0800410 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhoub46b8a82016-06-27 17:04:23 +0800411 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König832a9022016-02-15 12:33:02 +0100412 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König0ea54b92016-05-04 10:20:01 +0200413 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100414 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415
Chunming Zhoufd53be32016-07-01 17:59:01 +0800416 job->vm_id = id - adev->vm_manager.ids;
Christian König0c0fdf12016-07-08 10:48:24 +0200417 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König832a9022016-02-15 12:33:02 +0100418
419error:
Christian König94dd0a42016-01-18 17:01:42 +0100420 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100421 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400422}
423
Alex Deucher93dcc372016-06-17 17:05:15 -0400424static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
425{
426 struct amdgpu_device *adev = ring->adev;
Alex Deuchera1255102016-10-13 17:41:13 -0400427 const struct amdgpu_ip_block *ip_block;
Alex Deucher93dcc372016-06-17 17:05:15 -0400428
Christian König21cd9422016-10-05 15:36:39 +0200429 if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
Alex Deucher93dcc372016-06-17 17:05:15 -0400430 /* only compute rings */
431 return false;
432
433 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
434 if (!ip_block)
435 return false;
436
Alex Deuchera1255102016-10-13 17:41:13 -0400437 if (ip_block->version->major <= 7) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400438 /* gfx7 has no workaround */
439 return true;
Alex Deuchera1255102016-10-13 17:41:13 -0400440 } else if (ip_block->version->major == 8) {
Alex Deucher93dcc372016-06-17 17:05:15 -0400441 if (adev->gfx.mec_fw_version >= 673)
442 /* gfx8 is fixed in MEC firmware 673 */
443 return false;
444 else
445 return true;
446 }
447 return false;
448}
449
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450/**
451 * amdgpu_vm_flush - hardware flush the vm
452 *
453 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100454 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100455 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400456 *
Christian König4ff37a82016-02-26 16:18:26 +0100457 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400458 */
Chunming Zhoufd53be32016-07-01 17:59:01 +0800459int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460{
Christian König971fe9a92016-03-01 15:09:25 +0100461 struct amdgpu_device *adev = ring->adev;
Chunming Zhoufd53be32016-07-01 17:59:01 +0800462 struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100463 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800464 id->gds_base != job->gds_base ||
465 id->gds_size != job->gds_size ||
466 id->gws_base != job->gws_base ||
467 id->gws_size != job->gws_size ||
468 id->oa_base != job->oa_base ||
469 id->oa_size != job->oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100470 int r;
Christian Königd564a062016-03-01 15:51:53 +0100471
472 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800473 job->vm_needs_flush || gds_switch_needed ||
Alex Deucher93dcc372016-06-17 17:05:15 -0400474 amdgpu_vm_ring_has_compute_vm_bug(ring)))
Christian Königd564a062016-03-01 15:51:53 +0100475 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100476
Chunming Zhouaa1c8902016-06-30 13:56:02 +0800477 if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
478 amdgpu_vm_is_gpu_reset(adev, id))) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100479 struct dma_fence *fence;
Christian König41d9eb22016-03-01 16:46:18 +0100480
Chunming Zhoufd53be32016-07-01 17:59:01 +0800481 trace_amdgpu_vm_flush(job->vm_pd_addr, ring->idx, job->vm_id);
482 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100483
Christian König3dab83b2016-06-01 13:31:17 +0200484 r = amdgpu_fence_emit(ring, &fence);
485 if (r)
486 return r;
487
Christian König41d9eb22016-03-01 16:46:18 +0100488 mutex_lock(&adev->vm_manager.lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100489 dma_fence_put(id->last_flush);
Christian König3dab83b2016-06-01 13:31:17 +0200490 id->last_flush = fence;
Christian König41d9eb22016-03-01 16:46:18 +0100491 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400492 }
Christian Königcffadc82016-03-01 13:34:49 +0100493
Christian Königd564a062016-03-01 15:51:53 +0100494 if (gds_switch_needed) {
Chunming Zhoufd53be32016-07-01 17:59:01 +0800495 id->gds_base = job->gds_base;
496 id->gds_size = job->gds_size;
497 id->gws_base = job->gws_base;
498 id->gws_size = job->gws_size;
499 id->oa_base = job->oa_base;
500 id->oa_size = job->oa_size;
501 amdgpu_ring_emit_gds_switch(ring, job->vm_id,
502 job->gds_base, job->gds_size,
503 job->gws_base, job->gws_size,
504 job->oa_base, job->oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100505 }
Christian König41d9eb22016-03-01 16:46:18 +0100506
507 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100508}
509
510/**
511 * amdgpu_vm_reset_id - reset VMID to zero
512 *
513 * @adev: amdgpu device structure
514 * @vm_id: vmid number to use
515 *
516 * Reset saved GDW, GWS and OA to force switch on next flush.
517 */
518void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
519{
Christian Königbcb1ba32016-03-08 15:40:11 +0100520 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100521
Christian Königbcb1ba32016-03-08 15:40:11 +0100522 id->gds_base = 0;
523 id->gds_size = 0;
524 id->gws_base = 0;
525 id->gws_size = 0;
526 id->oa_base = 0;
527 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528}
529
530/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400531 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
532 *
533 * @vm: requested vm
534 * @bo: requested buffer object
535 *
Christian König8843dbb2016-01-26 12:17:11 +0100536 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537 * Search inside the @bos vm list for the requested vm
538 * Returns the found bo_va or NULL if none is found
539 *
540 * Object has to be reserved!
541 */
542struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
543 struct amdgpu_bo *bo)
544{
545 struct amdgpu_bo_va *bo_va;
546
547 list_for_each_entry(bo_va, &bo->va, bo_list) {
548 if (bo_va->vm == vm) {
549 return bo_va;
550 }
551 }
552 return NULL;
553}
554
555/**
Christian Königafef8b82016-08-12 13:29:18 +0200556 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557 *
Christian König29efc4f2016-08-04 14:52:50 +0200558 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559 * @pe: addr of the page entry
560 * @addr: dst addr to write into pe
561 * @count: number of page entries to update
562 * @incr: increase next addr by incr bytes
563 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 *
565 * Traces the parameters and calls the right asic functions
566 * to setup the page table using the DMA.
567 */
Christian Königafef8b82016-08-12 13:29:18 +0200568static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
569 uint64_t pe, uint64_t addr,
570 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800571 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572{
Christian Königec2f05f2016-09-25 16:11:52 +0200573 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574
Christian Königafef8b82016-08-12 13:29:18 +0200575 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200576 amdgpu_vm_write_pte(params->adev, params->ib, pe,
577 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578
579 } else {
Christian König27c5f362016-08-04 15:02:49 +0200580 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 count, incr, flags);
582 }
583}
584
585/**
Christian Königafef8b82016-08-12 13:29:18 +0200586 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
587 *
588 * @params: see amdgpu_pte_update_params definition
589 * @pe: addr of the page entry
590 * @addr: dst addr to write into pe
591 * @count: number of page entries to update
592 * @incr: increase next addr by incr bytes
593 * @flags: hw access flags
594 *
595 * Traces the parameters and calls the DMA function to copy the PTEs.
596 */
597static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
598 uint64_t pe, uint64_t addr,
599 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800600 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200601{
Christian Königec2f05f2016-09-25 16:11:52 +0200602 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200603
Christian Königec2f05f2016-09-25 16:11:52 +0200604
605 trace_amdgpu_vm_copy_ptes(pe, src, count);
606
607 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200608}
609
610/**
Christian Königb07c9d22015-11-30 13:26:07 +0100611 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400612 *
Christian Königb07c9d22015-11-30 13:26:07 +0100613 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 * @addr: the unmapped addr
615 *
616 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100617 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200619static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620{
621 uint64_t result;
622
Christian Königde9ea7b2016-08-12 11:33:30 +0200623 /* page table offset */
624 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625
Christian Königde9ea7b2016-08-12 11:33:30 +0200626 /* in case cpu page size != gpu page size*/
627 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100628
629 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630
631 return result;
632}
633
Christian Königf8991ba2016-09-16 15:36:49 +0200634/*
635 * amdgpu_vm_update_pdes - make sure that page directory is valid
636 *
637 * @adev: amdgpu_device pointer
638 * @vm: requested vm
639 * @start: start of GPU address range
640 * @end: end of GPU address range
641 *
642 * Allocates new page tables if necessary
643 * and updates the page directory.
644 * Returns 0 for success, error for failure.
645 */
646int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
647 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648{
Christian Königf8991ba2016-09-16 15:36:49 +0200649 struct amdgpu_bo *shadow;
Christian König2d55e452016-02-08 17:37:38 +0100650 struct amdgpu_ring *ring;
Christian Königf8991ba2016-09-16 15:36:49 +0200651 uint64_t pd_addr, shadow_addr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
Christian Königf8991ba2016-09-16 15:36:49 +0200653 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100655 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200656 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +1000657 struct dma_fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800658
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 int r;
660
Christian König2d55e452016-02-08 17:37:38 +0100661 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian Königf8991ba2016-09-16 15:36:49 +0200662 shadow = vm->page_directory->shadow;
Christian König2d55e452016-02-08 17:37:38 +0100663
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 /* padding, etc. */
665 ndw = 64;
666
667 /* assume the worst case */
668 ndw += vm->max_pde_used * 6;
669
Christian Königf8991ba2016-09-16 15:36:49 +0200670 pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
671 if (shadow) {
672 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
673 if (r)
674 return r;
675 shadow_addr = amdgpu_bo_gpu_offset(shadow);
676 ndw *= 2;
677 } else {
678 shadow_addr = 0;
679 }
680
Christian Königd71518b2016-02-01 12:20:25 +0100681 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
682 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400683 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100684
Christian König27c5f362016-08-04 15:02:49 +0200685 memset(&params, 0, sizeof(params));
686 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200687 params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688
689 /* walk over the address space and update the page directory */
690 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian König914b4dc2016-09-28 12:27:37 +0200691 struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692 uint64_t pde, pt;
693
694 if (bo == NULL)
695 continue;
696
Christian König0fc86832016-09-16 11:46:23 +0200697 if (bo->shadow) {
Christian Königf8991ba2016-09-16 15:36:49 +0200698 struct amdgpu_bo *pt_shadow = bo->shadow;
Christian König0fc86832016-09-16 11:46:23 +0200699
Christian Königf8991ba2016-09-16 15:36:49 +0200700 r = amdgpu_ttm_bind(&pt_shadow->tbo,
701 &pt_shadow->tbo.mem);
Christian König0fc86832016-09-16 11:46:23 +0200702 if (r)
703 return r;
704 }
705
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 pt = amdgpu_bo_gpu_offset(bo);
Christian Königf8991ba2016-09-16 15:36:49 +0200707 if (vm->page_tables[pt_idx].addr == pt)
708 continue;
709
710 vm->page_tables[pt_idx].addr = pt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711
712 pde = pd_addr + pt_idx * 8;
713 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +0200714 ((last_pt + incr * count) != pt) ||
715 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716
717 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +0200718 if (shadow)
719 amdgpu_vm_do_set_ptes(&params,
720 last_shadow,
721 last_pt, count,
722 incr,
723 AMDGPU_PTE_VALID);
724
Christian Königafef8b82016-08-12 13:29:18 +0200725 amdgpu_vm_do_set_ptes(&params, last_pde,
726 last_pt, count, incr,
727 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728 }
729
730 count = 1;
731 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +0200732 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400733 last_pt = pt;
734 } else {
735 ++count;
736 }
737 }
738
Christian Königf8991ba2016-09-16 15:36:49 +0200739 if (count) {
740 if (vm->page_directory->shadow)
741 amdgpu_vm_do_set_ptes(&params, last_shadow, last_pt,
742 count, incr, AMDGPU_PTE_VALID);
743
Christian Königafef8b82016-08-12 13:29:18 +0200744 amdgpu_vm_do_set_ptes(&params, last_pde, last_pt,
745 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800746 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747
Christian Königf8991ba2016-09-16 15:36:49 +0200748 if (params.ib->length_dw == 0) {
749 amdgpu_job_free(job);
750 return 0;
751 }
752
753 amdgpu_ring_pad_ib(ring, params.ib);
754 amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
755 AMDGPU_FENCE_OWNER_VM);
756 if (shadow)
757 amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
758 AMDGPU_FENCE_OWNER_VM);
759
760 WARN_ON(params.ib->length_dw > ndw);
761 r = amdgpu_job_submit(job, ring, &vm->entity,
762 AMDGPU_FENCE_OWNER_VM, &fence);
763 if (r)
764 goto error_free;
765
766 amdgpu_bo_fence(vm->page_directory, fence, true);
Dave Airlie220196b2016-10-28 11:33:52 +1000767 dma_fence_put(vm->page_directory_fence);
768 vm->page_directory_fence = dma_fence_get(fence);
769 dma_fence_put(fence);
Christian Königf8991ba2016-09-16 15:36:49 +0200770
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400771 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800772
773error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100774 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800775 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776}
777
778/**
Christian König92696dd2016-08-05 13:56:35 +0200779 * amdgpu_vm_update_ptes - make sure that page tables are valid
780 *
781 * @params: see amdgpu_pte_update_params definition
782 * @vm: requested vm
783 * @start: start of GPU address range
784 * @end: end of GPU address range
785 * @dst: destination address to map to, the next dst inside the function
786 * @flags: mapping flags
787 *
788 * Update the page tables in the range @start - @end.
789 */
790static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
791 struct amdgpu_vm *vm,
792 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800793 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +0200794{
795 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
796
797 uint64_t cur_pe_start, cur_nptes, cur_dst;
798 uint64_t addr; /* next GPU address to be updated */
799 uint64_t pt_idx;
800 struct amdgpu_bo *pt;
801 unsigned nptes; /* next number of ptes to be updated */
802 uint64_t next_pe_start;
803
804 /* initialize the variables */
805 addr = start;
806 pt_idx = addr >> amdgpu_vm_block_size;
Christian König914b4dc2016-09-28 12:27:37 +0200807 pt = vm->page_tables[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800808 if (params->shadow) {
809 if (!pt->shadow)
810 return;
Christian König914b4dc2016-09-28 12:27:37 +0200811 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800812 }
Christian König92696dd2016-08-05 13:56:35 +0200813 if ((addr & ~mask) == (end & ~mask))
814 nptes = end - addr;
815 else
816 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
817
818 cur_pe_start = amdgpu_bo_gpu_offset(pt);
819 cur_pe_start += (addr & mask) * 8;
820 cur_nptes = nptes;
821 cur_dst = dst;
822
823 /* for next ptb*/
824 addr += nptes;
825 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
826
827 /* walk over the address space and update the page tables */
828 while (addr < end) {
829 pt_idx = addr >> amdgpu_vm_block_size;
Christian König914b4dc2016-09-28 12:27:37 +0200830 pt = vm->page_tables[pt_idx].bo;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800831 if (params->shadow) {
832 if (!pt->shadow)
833 return;
Christian König914b4dc2016-09-28 12:27:37 +0200834 pt = pt->shadow;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800835 }
Christian König92696dd2016-08-05 13:56:35 +0200836
837 if ((addr & ~mask) == (end & ~mask))
838 nptes = end - addr;
839 else
840 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
841
842 next_pe_start = amdgpu_bo_gpu_offset(pt);
843 next_pe_start += (addr & mask) * 8;
844
Christian König96105e52016-08-12 12:59:59 +0200845 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
846 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
Christian König92696dd2016-08-05 13:56:35 +0200847 /* The next ptb is consecutive to current ptb.
Christian Königafef8b82016-08-12 13:29:18 +0200848 * Don't call the update function now.
Christian König92696dd2016-08-05 13:56:35 +0200849 * Will update two ptbs together in future.
850 */
851 cur_nptes += nptes;
852 } else {
Christian Königafef8b82016-08-12 13:29:18 +0200853 params->func(params, cur_pe_start, cur_dst, cur_nptes,
854 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200855
856 cur_pe_start = next_pe_start;
857 cur_nptes = nptes;
858 cur_dst = dst;
859 }
860
861 /* for next ptb*/
862 addr += nptes;
863 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
864 }
865
Christian Königafef8b82016-08-12 13:29:18 +0200866 params->func(params, cur_pe_start, cur_dst, cur_nptes,
867 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200868}
869
870/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400871 * amdgpu_vm_frag_ptes - add fragment information to PTEs
872 *
Christian König29efc4f2016-08-04 14:52:50 +0200873 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +0200874 * @vm: requested vm
875 * @start: first PTE to handle
876 * @end: last PTE to handle
877 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400879 */
Christian König27c5f362016-08-04 15:02:49 +0200880static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200881 struct amdgpu_vm *vm,
882 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800883 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884{
885 /**
886 * The MC L1 TLB supports variable sized pages, based on a fragment
887 * field in the PTE. When this field is set to a non-zero value, page
888 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
889 * flags are considered valid for all PTEs within the fragment range
890 * and corresponding mappings are assumed to be physically contiguous.
891 *
892 * The L1 TLB can store a single PTE for the whole fragment,
893 * significantly increasing the space available for translation
894 * caching. This leads to large improvements in throughput when the
895 * TLB is under pressure.
896 *
897 * The L2 TLB distributes small and large fragments into two
898 * asymmetric partitions. The large fragment cache is significantly
899 * larger. Thus, we try to use large fragments wherever possible.
900 * Userspace can support this by aligning virtual base address and
901 * allocation size to the fragment size.
902 */
903
Christian König80366172016-10-04 13:39:43 +0200904 /* SI and newer are optimized for 64KB */
905 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
906 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400907
Christian König92696dd2016-08-05 13:56:35 +0200908 uint64_t frag_start = ALIGN(start, frag_align);
909 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +0100910
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400911 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +0200912 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Christian König92696dd2016-08-05 13:56:35 +0200913 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400914
Christian König92696dd2016-08-05 13:56:35 +0200915 amdgpu_vm_update_ptes(params, vm, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400916 return;
917 }
918
919 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +0200920 if (start != frag_start) {
921 amdgpu_vm_update_ptes(params, vm, start, frag_start,
922 dst, flags);
923 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400924 }
925
926 /* handle the area in the middle */
Christian König92696dd2016-08-05 13:56:35 +0200927 amdgpu_vm_update_ptes(params, vm, frag_start, frag_end, dst,
Christian König80366172016-10-04 13:39:43 +0200928 flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929
930 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +0200931 if (frag_end != end) {
932 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
933 amdgpu_vm_update_ptes(params, vm, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400934 }
935}
936
937/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
939 *
940 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +0200941 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +0100942 * @src: address where to copy page table entries from
943 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100944 * @vm: requested vm
945 * @start: start of mapped range
946 * @last: last mapped entry
947 * @flags: flags for the entries
948 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400949 * @fence: optional resulting fence
950 *
Christian Königa14faa62016-01-25 14:27:31 +0100951 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953 */
954static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100955 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100956 uint64_t src,
957 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400958 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100959 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +0800960 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100961 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962{
Christian König2d55e452016-02-08 17:37:38 +0100963 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100964 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100966 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200967 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100968 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969 int r;
970
Christian Königafef8b82016-08-12 13:29:18 +0200971 memset(&params, 0, sizeof(params));
972 params.adev = adev;
973 params.src = src;
974
Christian König2d55e452016-02-08 17:37:38 +0100975 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +0200976
Christian König29efc4f2016-08-04 14:52:50 +0200977 memset(&params, 0, sizeof(params));
Christian König27c5f362016-08-04 15:02:49 +0200978 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200979 params.src = src;
Christian König2d55e452016-02-08 17:37:38 +0100980
Christian Königa1e08d32016-01-26 11:40:46 +0100981 /* sync to everything on unmapping */
982 if (!(flags & AMDGPU_PTE_VALID))
983 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
984
Christian Königa14faa62016-01-25 14:27:31 +0100985 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986
987 /*
988 * reserve space for one command every (1 << BLOCK_SIZE)
989 * entries or 2k dwords (whatever is smaller)
990 */
991 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
992
993 /* padding, etc. */
994 ndw = 64;
995
Christian Königb0456f92016-08-11 14:06:54 +0200996 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400997 /* only copy commands needed */
998 ndw += ncmds * 7;
999
Christian Königafef8b82016-08-12 13:29:18 +02001000 params.func = amdgpu_vm_do_copy_ptes;
1001
Christian Königb0456f92016-08-11 14:06:54 +02001002 } else if (pages_addr) {
1003 /* copy commands needed */
1004 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005
Christian Königb0456f92016-08-11 14:06:54 +02001006 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007 ndw += nptes * 2;
1008
Christian Königafef8b82016-08-12 13:29:18 +02001009 params.func = amdgpu_vm_do_copy_ptes;
1010
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001011 } else {
1012 /* set page commands needed */
1013 ndw += ncmds * 10;
1014
1015 /* two extra commands for begin/end of fragment */
1016 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +02001017
1018 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001019 }
1020
Christian Königd71518b2016-02-01 12:20:25 +01001021 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1022 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001024
Christian König29efc4f2016-08-04 14:52:50 +02001025 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001026
Christian Königb0456f92016-08-11 14:06:54 +02001027 if (!src && pages_addr) {
1028 uint64_t *pte;
1029 unsigned i;
1030
1031 /* Put the PTEs at the end of the IB. */
1032 i = ndw - nptes * 2;
1033 pte= (uint64_t *)&(job->ibs->ptr[i]);
1034 params.src = job->ibs->gpu_addr + i * 4;
1035
1036 for (i = 0; i < nptes; ++i) {
1037 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1038 AMDGPU_GPU_PAGE_SIZE);
1039 pte[i] |= flags;
1040 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001041 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001042 }
1043
Christian König3cabaa52016-06-06 10:17:58 +02001044 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1045 if (r)
1046 goto error_free;
1047
Christian Könige86f9ce2016-02-08 12:13:05 +01001048 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001049 owner);
1050 if (r)
1051 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001052
Christian Königa1e08d32016-01-26 11:40:46 +01001053 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
1054 if (r)
1055 goto error_free;
1056
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001057 params.shadow = true;
1058 amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
1059 params.shadow = false;
Christian König92696dd2016-08-05 13:56:35 +02001060 amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061
Christian König29efc4f2016-08-04 14:52:50 +02001062 amdgpu_ring_pad_ib(ring, params.ib);
1063 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001064 r = amdgpu_job_submit(job, ring, &vm->entity,
1065 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001066 if (r)
1067 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068
Christian Königbf60efd2015-09-04 10:47:56 +02001069 amdgpu_bo_fence(vm->page_directory, f, true);
Christian König284710f2017-01-30 11:09:31 +01001070 dma_fence_put(*fence);
1071 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001073
1074error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001075 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001076 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001077}
1078
1079/**
Christian Königa14faa62016-01-25 14:27:31 +01001080 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1081 *
1082 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001083 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001084 * @gtt_flags: flags as they are used for GTT
1085 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001086 * @vm: requested vm
1087 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001088 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001089 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001090 * @fence: optional resulting fence
1091 *
1092 * Split the mapping into smaller chunks so that each update fits
1093 * into a SDMA IB.
1094 * Returns 0 for success, -EINVAL for failure.
1095 */
1096static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001097 struct dma_fence *exclusive,
Chunming Zhou6b777602016-09-21 16:19:19 +08001098 uint64_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +02001099 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001100 struct amdgpu_vm *vm,
1101 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001102 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001103 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001104 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001105{
Christian König63e0ba42016-08-16 17:38:37 +02001106 uint64_t pfn, src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +01001107 int r;
1108
1109 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1110 * but in case of something, we filter the flags in first place
1111 */
1112 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1113 flags &= ~AMDGPU_PTE_READABLE;
1114 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1115 flags &= ~AMDGPU_PTE_WRITEABLE;
1116
1117 trace_amdgpu_vm_bo_update(mapping);
1118
Christian König63e0ba42016-08-16 17:38:37 +02001119 pfn = mapping->offset >> PAGE_SHIFT;
1120 if (nodes) {
1121 while (pfn >= nodes->size) {
1122 pfn -= nodes->size;
1123 ++nodes;
1124 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001125 }
Christian Königa14faa62016-01-25 14:27:31 +01001126
Christian König63e0ba42016-08-16 17:38:37 +02001127 do {
1128 uint64_t max_entries;
1129 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001130
Christian König63e0ba42016-08-16 17:38:37 +02001131 if (nodes) {
1132 addr = nodes->start << PAGE_SHIFT;
1133 max_entries = (nodes->size - pfn) *
1134 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1135 } else {
1136 addr = 0;
1137 max_entries = S64_MAX;
1138 }
Christian Königa14faa62016-01-25 14:27:31 +01001139
Christian König63e0ba42016-08-16 17:38:37 +02001140 if (pages_addr) {
1141 if (flags == gtt_flags)
1142 src = adev->gart.table_addr +
1143 (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
1144 else
1145 max_entries = min(max_entries, 16ull * 1024ull);
1146 addr = 0;
1147 } else if (flags & AMDGPU_PTE_VALID) {
1148 addr += adev->vm_manager.vram_base_offset;
1149 }
1150 addr += pfn << PAGE_SHIFT;
1151
1152 last = min((uint64_t)mapping->it.last, start + max_entries - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001153 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1154 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001155 start, last, flags, addr,
1156 fence);
1157 if (r)
1158 return r;
1159
Christian König63e0ba42016-08-16 17:38:37 +02001160 pfn += last - start + 1;
1161 if (nodes && nodes->size == pfn) {
1162 pfn = 0;
1163 ++nodes;
1164 }
Christian Königa14faa62016-01-25 14:27:31 +01001165 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001166
1167 } while (unlikely(start != mapping->it.last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001168
1169 return 0;
1170}
1171
1172/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1174 *
1175 * @adev: amdgpu_device pointer
1176 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001177 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001178 *
1179 * Fill in the page table entries for @bo_va.
1180 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181 */
1182int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1183 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001184 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001185{
1186 struct amdgpu_vm *vm = bo_va->vm;
1187 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001188 dma_addr_t *pages_addr = NULL;
Chunming Zhou6b777602016-09-21 16:19:19 +08001189 uint64_t gtt_flags, flags;
Christian König99e124f2016-08-16 14:43:17 +02001190 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001191 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001192 struct dma_fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001193 int r;
1194
Christian Königa5f6b5b2017-01-30 11:01:38 +01001195 if (clear || !bo_va->bo) {
Christian König99e124f2016-08-16 14:43:17 +02001196 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001197 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001198 exclusive = NULL;
1199 } else {
Christian König8358dce2016-03-30 10:50:25 +02001200 struct ttm_dma_tt *ttm;
1201
Christian König99e124f2016-08-16 14:43:17 +02001202 mem = &bo_va->bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001203 nodes = mem->mm_node;
1204 if (mem->mem_type == TTM_PL_TT) {
Christian König8358dce2016-03-30 10:50:25 +02001205 ttm = container_of(bo_va->bo->tbo.ttm, struct
1206 ttm_dma_tt, ttm);
1207 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001208 }
Christian König3cabaa52016-06-06 10:17:58 +02001209 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210 }
1211
Christian Königa5f6b5b2017-01-30 11:01:38 +01001212 if (bo_va->bo) {
1213 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1214 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1215 adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
1216 flags : 0;
1217 } else {
1218 flags = 0x0;
1219 gtt_flags = ~0x0;
1220 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001221
Christian König7fc11952015-07-30 11:53:42 +02001222 spin_lock(&vm->status_lock);
1223 if (!list_empty(&bo_va->vm_status))
1224 list_splice_init(&bo_va->valids, &bo_va->invalids);
1225 spin_unlock(&vm->status_lock);
1226
1227 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001228 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1229 gtt_flags, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001230 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001231 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 if (r)
1233 return r;
1234 }
1235
Christian Königd6c10f62015-09-28 12:00:23 +02001236 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1237 list_for_each_entry(mapping, &bo_va->valids, list)
1238 trace_amdgpu_vm_bo_mapping(mapping);
1239
1240 list_for_each_entry(mapping, &bo_va->invalids, list)
1241 trace_amdgpu_vm_bo_mapping(mapping);
1242 }
1243
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001245 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246 list_del_init(&bo_va->vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001247 if (clear)
Christian König7fc11952015-07-30 11:53:42 +02001248 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001249 spin_unlock(&vm->status_lock);
1250
1251 return 0;
1252}
1253
1254/**
Christian König284710f2017-01-30 11:09:31 +01001255 * amdgpu_vm_update_prt_state - update the global PRT state
1256 */
1257static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1258{
1259 unsigned long flags;
1260 bool enable;
1261
1262 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001263 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001264 adev->gart.gart_funcs->set_prt(adev, enable);
1265 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1266}
1267
1268/**
Christian König4388fc22017-03-13 10:13:36 +01001269 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001270 */
1271static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1272{
Christian König4388fc22017-03-13 10:13:36 +01001273 if (!adev->gart.gart_funcs->set_prt)
1274 return;
1275
Christian König451bc8e2017-02-14 16:02:52 +01001276 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1277 amdgpu_vm_update_prt_state(adev);
1278}
1279
1280/**
Christian König0b15f2f2017-02-14 15:47:03 +01001281 * amdgpu_vm_prt_put - drop a PRT user
1282 */
1283static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1284{
Christian König451bc8e2017-02-14 16:02:52 +01001285 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001286 amdgpu_vm_update_prt_state(adev);
1287}
1288
1289/**
Christian König451bc8e2017-02-14 16:02:52 +01001290 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001291 */
1292static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1293{
1294 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1295
Christian König0b15f2f2017-02-14 15:47:03 +01001296 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001297 kfree(cb);
1298}
1299
1300/**
Christian König451bc8e2017-02-14 16:02:52 +01001301 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1302 */
1303static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1304 struct dma_fence *fence)
1305{
Christian König4388fc22017-03-13 10:13:36 +01001306 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001307
Christian König4388fc22017-03-13 10:13:36 +01001308 if (!adev->gart.gart_funcs->set_prt)
1309 return;
1310
1311 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001312 if (!cb) {
1313 /* Last resort when we are OOM */
1314 if (fence)
1315 dma_fence_wait(fence, false);
1316
1317 amdgpu_vm_prt_put(cb->adev);
1318 } else {
1319 cb->adev = adev;
1320 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1321 amdgpu_vm_prt_cb))
1322 amdgpu_vm_prt_cb(fence, &cb->cb);
1323 }
1324}
1325
1326/**
Christian König284710f2017-01-30 11:09:31 +01001327 * amdgpu_vm_free_mapping - free a mapping
1328 *
1329 * @adev: amdgpu_device pointer
1330 * @vm: requested vm
1331 * @mapping: mapping to be freed
1332 * @fence: fence of the unmap operation
1333 *
1334 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1335 */
1336static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1337 struct amdgpu_vm *vm,
1338 struct amdgpu_bo_va_mapping *mapping,
1339 struct dma_fence *fence)
1340{
Christian König451bc8e2017-02-14 16:02:52 +01001341 if (mapping->flags & AMDGPU_PTE_PRT)
1342 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001343 kfree(mapping);
1344}
1345
1346/**
Christian König451bc8e2017-02-14 16:02:52 +01001347 * amdgpu_vm_prt_fini - finish all prt mappings
1348 *
1349 * @adev: amdgpu_device pointer
1350 * @vm: requested vm
1351 *
1352 * Register a cleanup callback to disable PRT support after VM dies.
1353 */
1354static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1355{
1356 struct reservation_object *resv = vm->page_directory->tbo.resv;
1357 struct dma_fence *excl, **shared;
1358 unsigned i, shared_count;
1359 int r;
1360
1361 r = reservation_object_get_fences_rcu(resv, &excl,
1362 &shared_count, &shared);
1363 if (r) {
1364 /* Not enough memory to grab the fence list, as last resort
1365 * block for all the fences to complete.
1366 */
1367 reservation_object_wait_timeout_rcu(resv, true, false,
1368 MAX_SCHEDULE_TIMEOUT);
1369 return;
1370 }
1371
1372 /* Add a callback for each fence in the reservation object */
1373 amdgpu_vm_prt_get(adev);
1374 amdgpu_vm_add_prt_cb(adev, excl);
1375
1376 for (i = 0; i < shared_count; ++i) {
1377 amdgpu_vm_prt_get(adev);
1378 amdgpu_vm_add_prt_cb(adev, shared[i]);
1379 }
1380
1381 kfree(shared);
1382}
1383
1384/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1386 *
1387 * @adev: amdgpu_device pointer
1388 * @vm: requested vm
1389 *
1390 * Make sure all freed BOs are cleared in the PT.
1391 * Returns 0 for success.
1392 *
1393 * PTs have to be reserved and mutex must be locked!
1394 */
1395int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1396 struct amdgpu_vm *vm)
1397{
1398 struct amdgpu_bo_va_mapping *mapping;
Christian König284710f2017-01-30 11:09:31 +01001399 struct dma_fence *fence = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001400 int r;
1401
1402 while (!list_empty(&vm->freed)) {
1403 mapping = list_first_entry(&vm->freed,
1404 struct amdgpu_bo_va_mapping, list);
1405 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001406
Christian König3cabaa52016-06-06 10:17:58 +02001407 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
Christian König284710f2017-01-30 11:09:31 +01001408 0, 0, &fence);
1409 amdgpu_vm_free_mapping(adev, vm, mapping, fence);
1410 if (r) {
1411 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412 return r;
Christian König284710f2017-01-30 11:09:31 +01001413 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414
1415 }
Christian König284710f2017-01-30 11:09:31 +01001416 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417 return 0;
1418
1419}
1420
1421/**
1422 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1423 *
1424 * @adev: amdgpu_device pointer
1425 * @vm: requested vm
1426 *
1427 * Make sure all invalidated BOs are cleared in the PT.
1428 * Returns 0 for success.
1429 *
1430 * PTs have to be reserved and mutex must be locked!
1431 */
1432int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001433 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001434{
monk.liucfe2c972015-05-26 15:01:54 +08001435 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001436 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437
1438 spin_lock(&vm->status_lock);
1439 while (!list_empty(&vm->invalidated)) {
1440 bo_va = list_first_entry(&vm->invalidated,
1441 struct amdgpu_bo_va, vm_status);
1442 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001443
Christian König99e124f2016-08-16 14:43:17 +02001444 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001445 if (r)
1446 return r;
1447
1448 spin_lock(&vm->status_lock);
1449 }
1450 spin_unlock(&vm->status_lock);
1451
monk.liucfe2c972015-05-26 15:01:54 +08001452 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001453 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001454
1455 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001456}
1457
1458/**
1459 * amdgpu_vm_bo_add - add a bo to a specific vm
1460 *
1461 * @adev: amdgpu_device pointer
1462 * @vm: requested vm
1463 * @bo: amdgpu buffer object
1464 *
Christian König8843dbb2016-01-26 12:17:11 +01001465 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001466 * Add @bo to the list of bos associated with the vm
1467 * Returns newly added bo_va or NULL for failure
1468 *
1469 * Object has to be reserved!
1470 */
1471struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1472 struct amdgpu_vm *vm,
1473 struct amdgpu_bo *bo)
1474{
1475 struct amdgpu_bo_va *bo_va;
1476
1477 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1478 if (bo_va == NULL) {
1479 return NULL;
1480 }
1481 bo_va->vm = vm;
1482 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001483 bo_va->ref_count = 1;
1484 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001485 INIT_LIST_HEAD(&bo_va->valids);
1486 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001488
Christian Königa5f6b5b2017-01-30 11:01:38 +01001489 if (bo)
1490 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491
1492 return bo_va;
1493}
1494
1495/**
1496 * amdgpu_vm_bo_map - map bo inside a vm
1497 *
1498 * @adev: amdgpu_device pointer
1499 * @bo_va: bo_va to store the address
1500 * @saddr: where to map the BO
1501 * @offset: requested offset in the BO
1502 * @flags: attributes of pages (read/write/valid/etc.)
1503 *
1504 * Add a mapping of the BO at the specefied addr into the VM.
1505 * Returns 0 for success, error for failure.
1506 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001507 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001508 */
1509int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1510 struct amdgpu_bo_va *bo_va,
1511 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001512 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513{
1514 struct amdgpu_bo_va_mapping *mapping;
1515 struct amdgpu_vm *vm = bo_va->vm;
1516 struct interval_tree_node *it;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001518
Christian König0be52de2015-05-18 14:37:27 +02001519 /* validate the parameters */
1520 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001521 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001522 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001523
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001524 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001525 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001526 if (saddr >= eaddr ||
1527 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001528 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001529
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001530 saddr /= AMDGPU_GPU_PAGE_SIZE;
1531 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1532
Felix Kuehling005ae952015-11-23 17:43:48 -05001533 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001534 if (it) {
1535 struct amdgpu_bo_va_mapping *tmp;
1536 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1537 /* bo and tmp overlap, invalid addr */
1538 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1539 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1540 tmp->it.start, tmp->it.last + 1);
Christian König663e4572017-03-13 10:13:37 +01001541 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 }
1543
1544 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001545 if (!mapping)
1546 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001547
1548 INIT_LIST_HEAD(&mapping->list);
1549 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001550 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551 mapping->offset = offset;
1552 mapping->flags = flags;
1553
Christian König7fc11952015-07-30 11:53:42 +02001554 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 interval_tree_insert(&mapping->it, &vm->va);
1556
Christian König4388fc22017-03-13 10:13:36 +01001557 if (flags & AMDGPU_PTE_PRT)
1558 amdgpu_vm_prt_get(adev);
1559
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001561}
1562
1563/**
Christian König80f95c52017-03-13 10:13:39 +01001564 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1565 *
1566 * @adev: amdgpu_device pointer
1567 * @bo_va: bo_va to store the address
1568 * @saddr: where to map the BO
1569 * @offset: requested offset in the BO
1570 * @flags: attributes of pages (read/write/valid/etc.)
1571 *
1572 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1573 * mappings as we do so.
1574 * Returns 0 for success, error for failure.
1575 *
1576 * Object has to be reserved and unreserved outside!
1577 */
1578int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1579 struct amdgpu_bo_va *bo_va,
1580 uint64_t saddr, uint64_t offset,
1581 uint64_t size, uint64_t flags)
1582{
1583 struct amdgpu_bo_va_mapping *mapping;
1584 struct amdgpu_vm *vm = bo_va->vm;
1585 uint64_t eaddr;
1586 int r;
1587
1588 /* validate the parameters */
1589 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1590 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1591 return -EINVAL;
1592
1593 /* make sure object fit at this offset */
1594 eaddr = saddr + size - 1;
1595 if (saddr >= eaddr ||
1596 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
1597 return -EINVAL;
1598
1599 /* Allocate all the needed memory */
1600 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1601 if (!mapping)
1602 return -ENOMEM;
1603
1604 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
1605 if (r) {
1606 kfree(mapping);
1607 return r;
1608 }
1609
1610 saddr /= AMDGPU_GPU_PAGE_SIZE;
1611 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1612
1613 mapping->it.start = saddr;
1614 mapping->it.last = eaddr;
1615 mapping->offset = offset;
1616 mapping->flags = flags;
1617
1618 list_add(&mapping->list, &bo_va->invalids);
1619 interval_tree_insert(&mapping->it, &vm->va);
1620
1621 if (flags & AMDGPU_PTE_PRT)
1622 amdgpu_vm_prt_get(adev);
1623
1624 return 0;
1625}
1626
1627/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001628 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1629 *
1630 * @adev: amdgpu_device pointer
1631 * @bo_va: bo_va to remove the address from
1632 * @saddr: where to the BO is mapped
1633 *
1634 * Remove a mapping of the BO at the specefied addr from the VM.
1635 * Returns 0 for success, error for failure.
1636 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001637 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638 */
1639int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1640 struct amdgpu_bo_va *bo_va,
1641 uint64_t saddr)
1642{
1643 struct amdgpu_bo_va_mapping *mapping;
1644 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001645 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646
Christian König6c7fc502015-06-05 20:56:17 +02001647 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001648
Christian König7fc11952015-07-30 11:53:42 +02001649 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 if (mapping->it.start == saddr)
1651 break;
1652 }
1653
Christian König7fc11952015-07-30 11:53:42 +02001654 if (&mapping->list == &bo_va->valids) {
1655 valid = false;
1656
1657 list_for_each_entry(mapping, &bo_va->invalids, list) {
1658 if (mapping->it.start == saddr)
1659 break;
1660 }
1661
Christian König32b41ac2016-03-08 18:03:27 +01001662 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001663 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001664 }
Christian König32b41ac2016-03-08 18:03:27 +01001665
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001666 list_del(&mapping->list);
1667 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001668 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669
Christian Könige17841b2016-03-08 17:52:01 +01001670 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001672 else
Christian König284710f2017-01-30 11:09:31 +01001673 amdgpu_vm_free_mapping(adev, vm, mapping,
1674 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675
1676 return 0;
1677}
1678
1679/**
Christian Königdc54d3d2017-03-13 10:13:38 +01001680 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1681 *
1682 * @adev: amdgpu_device pointer
1683 * @vm: VM structure to use
1684 * @saddr: start of the range
1685 * @size: size of the range
1686 *
1687 * Remove all mappings in a range, split them as appropriate.
1688 * Returns 0 for success, error for failure.
1689 */
1690int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1691 struct amdgpu_vm *vm,
1692 uint64_t saddr, uint64_t size)
1693{
1694 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1695 struct interval_tree_node *it;
1696 LIST_HEAD(removed);
1697 uint64_t eaddr;
1698
1699 eaddr = saddr + size - 1;
1700 saddr /= AMDGPU_GPU_PAGE_SIZE;
1701 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1702
1703 /* Allocate all the needed memory */
1704 before = kzalloc(sizeof(*before), GFP_KERNEL);
1705 if (!before)
1706 return -ENOMEM;
1707
1708 after = kzalloc(sizeof(*after), GFP_KERNEL);
1709 if (!after) {
1710 kfree(before);
1711 return -ENOMEM;
1712 }
1713
1714 /* Now gather all removed mappings */
1715 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
1716 while (it) {
1717 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1718 it = interval_tree_iter_next(it, saddr, eaddr);
1719
1720 /* Remember mapping split at the start */
1721 if (tmp->it.start < saddr) {
1722 before->it.start = tmp->it.start;;
1723 before->it.last = saddr - 1;
1724 before->offset = tmp->offset;
1725 before->flags = tmp->flags;
1726 list_add(&before->list, &tmp->list);
1727 }
1728
1729 /* Remember mapping split at the end */
1730 if (tmp->it.last > eaddr) {
1731 after->it.start = eaddr + 1;
1732 after->it.last = tmp->it.last;
1733 after->offset = tmp->offset;
1734 after->offset += after->it.start - tmp->it.start;
1735 after->flags = tmp->flags;
1736 list_add(&after->list, &tmp->list);
1737 }
1738
1739 list_del(&tmp->list);
1740 list_add(&tmp->list, &removed);
1741 }
1742
1743 /* And free them up */
1744 list_for_each_entry_safe(tmp, next, &removed, list) {
1745 interval_tree_remove(&tmp->it, &vm->va);
1746 list_del(&tmp->list);
1747
1748 if (tmp->it.start < saddr)
1749 tmp->it.start = saddr;
1750 if (tmp->it.last > eaddr)
1751 tmp->it.last = eaddr;
1752
1753 list_add(&tmp->list, &vm->freed);
1754 trace_amdgpu_vm_bo_unmap(NULL, tmp);
1755 }
1756
1757 /* Insert partial mapping before the range*/
1758 if (before->it.start != before->it.last) {
1759 interval_tree_insert(&before->it, &vm->va);
1760 if (before->flags & AMDGPU_PTE_PRT)
1761 amdgpu_vm_prt_get(adev);
1762 } else {
1763 kfree(before);
1764 }
1765
1766 /* Insert partial mapping after the range */
1767 if (after->it.start != after->it.last) {
1768 interval_tree_insert(&after->it, &vm->va);
1769 if (after->flags & AMDGPU_PTE_PRT)
1770 amdgpu_vm_prt_get(adev);
1771 } else {
1772 kfree(after);
1773 }
1774
1775 return 0;
1776}
1777
1778/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1780 *
1781 * @adev: amdgpu_device pointer
1782 * @bo_va: requested bo_va
1783 *
Christian König8843dbb2016-01-26 12:17:11 +01001784 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785 *
1786 * Object have to be reserved!
1787 */
1788void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1789 struct amdgpu_bo_va *bo_va)
1790{
1791 struct amdgpu_bo_va_mapping *mapping, *next;
1792 struct amdgpu_vm *vm = bo_va->vm;
1793
1794 list_del(&bo_va->bo_list);
1795
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001796 spin_lock(&vm->status_lock);
1797 list_del(&bo_va->vm_status);
1798 spin_unlock(&vm->status_lock);
1799
Christian König7fc11952015-07-30 11:53:42 +02001800 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001801 list_del(&mapping->list);
1802 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001803 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001804 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001805 }
Christian König7fc11952015-07-30 11:53:42 +02001806 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1807 list_del(&mapping->list);
1808 interval_tree_remove(&mapping->it, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01001809 amdgpu_vm_free_mapping(adev, vm, mapping,
1810 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02001811 }
Christian König32b41ac2016-03-08 18:03:27 +01001812
Chris Wilsonf54d1862016-10-25 13:00:45 +01001813 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001814 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815}
1816
1817/**
1818 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1819 *
1820 * @adev: amdgpu_device pointer
1821 * @vm: requested vm
1822 * @bo: amdgpu buffer object
1823 *
Christian König8843dbb2016-01-26 12:17:11 +01001824 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001825 */
1826void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1827 struct amdgpu_bo *bo)
1828{
1829 struct amdgpu_bo_va *bo_va;
1830
1831 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001832 spin_lock(&bo_va->vm->status_lock);
1833 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001834 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001835 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001836 }
1837}
1838
1839/**
1840 * amdgpu_vm_init - initialize a vm instance
1841 *
1842 * @adev: amdgpu_device pointer
1843 * @vm: requested vm
1844 *
Christian König8843dbb2016-01-26 12:17:11 +01001845 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001846 */
1847int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1848{
1849 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1850 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001851 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001852 unsigned ring_instance;
1853 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001854 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855 int i, r;
1856
Christian Königbcb1ba32016-03-08 15:40:11 +01001857 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1858 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001859 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001860 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001861 spin_lock_init(&vm->status_lock);
1862 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001863 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001865
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001866 pd_size = amdgpu_vm_directory_size(adev);
1867 pd_entries = amdgpu_vm_num_pdes(adev);
1868
1869 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001870 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001871 if (vm->page_tables == NULL) {
1872 DRM_ERROR("Cannot allocate memory for page table array\n");
1873 return -ENOMEM;
1874 }
1875
Christian König2bd9ccf2016-02-01 12:53:58 +01001876 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001877
1878 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1879 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1880 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001881 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1882 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1883 rq, amdgpu_sched_jobs);
1884 if (r)
Chunming Zhou64827ad2016-07-28 17:20:32 +08001885 goto err;
Christian König2bd9ccf2016-02-01 12:53:58 +01001886
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001887 vm->page_directory_fence = NULL;
1888
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001890 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08001891 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
Christian König03f48dd2016-08-15 17:00:22 +02001892 AMDGPU_GEM_CREATE_SHADOW |
Christian König617859e2016-11-17 15:40:02 +01001893 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1894 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Christian König72d76682015-09-03 17:34:59 +02001895 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001897 goto error_free_sched_entity;
1898
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001899 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001900 if (r)
1901 goto error_free_page_directory;
1902
Christian König5a712a82016-06-21 16:28:15 +02001903 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König2a82ec212016-09-16 13:11:45 +02001904 amdgpu_bo_unreserve(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905
1906 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001907
1908error_free_page_directory:
Christian König2698f622016-09-16 13:06:09 +02001909 amdgpu_bo_unref(&vm->page_directory->shadow);
Christian König2bd9ccf2016-02-01 12:53:58 +01001910 amdgpu_bo_unref(&vm->page_directory);
1911 vm->page_directory = NULL;
1912
1913error_free_sched_entity:
1914 amd_sched_entity_fini(&ring->sched, &vm->entity);
1915
Chunming Zhou64827ad2016-07-28 17:20:32 +08001916err:
1917 drm_free_large(vm->page_tables);
1918
Christian König2bd9ccf2016-02-01 12:53:58 +01001919 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920}
1921
1922/**
1923 * amdgpu_vm_fini - tear down a vm instance
1924 *
1925 * @adev: amdgpu_device pointer
1926 * @vm: requested vm
1927 *
Christian König8843dbb2016-01-26 12:17:11 +01001928 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929 * Unbind the VM and remove all bos from the vm bo list
1930 */
1931void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1932{
1933 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01001934 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935 int i;
1936
Christian König2d55e452016-02-08 17:37:38 +01001937 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001938
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939 if (!RB_EMPTY_ROOT(&vm->va)) {
1940 dev_err(adev->dev, "still active bo inside vm\n");
1941 }
1942 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1943 list_del(&mapping->list);
1944 interval_tree_remove(&mapping->it, &vm->va);
1945 kfree(mapping);
1946 }
1947 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01001948 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01001949 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01001950 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01001951 }
Christian König284710f2017-01-30 11:09:31 +01001952
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001953 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01001954 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001955 }
1956
Chunming Zhou1baa4392016-08-04 13:59:32 +08001957 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) {
Christian König914b4dc2016-09-28 12:27:37 +02001958 struct amdgpu_bo *pt = vm->page_tables[i].bo;
Christian König2698f622016-09-16 13:06:09 +02001959
1960 if (!pt)
1961 continue;
1962
1963 amdgpu_bo_unref(&pt->shadow);
1964 amdgpu_bo_unref(&pt);
Chunming Zhou1baa4392016-08-04 13:59:32 +08001965 }
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001966 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001967
Christian König2698f622016-09-16 13:06:09 +02001968 amdgpu_bo_unref(&vm->page_directory->shadow);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969 amdgpu_bo_unref(&vm->page_directory);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001970 dma_fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001971}
Christian Königea89f8c2015-11-15 20:52:06 +01001972
1973/**
Christian Königa9a78b32016-01-21 10:19:11 +01001974 * amdgpu_vm_manager_init - init the VM manager
1975 *
1976 * @adev: amdgpu_device pointer
1977 *
1978 * Initialize the VM manager structures
1979 */
1980void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1981{
1982 unsigned i;
1983
1984 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1985
1986 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001987 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1988 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01001989 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01001990 list_add_tail(&adev->vm_manager.ids[i].list,
1991 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001992 }
Christian König2d55e452016-02-08 17:37:38 +01001993
Chris Wilsonf54d1862016-10-25 13:00:45 +01001994 adev->vm_manager.fence_context =
1995 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02001996 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1997 adev->vm_manager.seqno[i] = 0;
1998
Christian König2d55e452016-02-08 17:37:38 +01001999 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002000 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002001 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002002 atomic_set(&adev->vm_manager.num_prt_users, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01002003}
2004
2005/**
Christian Königea89f8c2015-11-15 20:52:06 +01002006 * amdgpu_vm_manager_fini - cleanup VM manager
2007 *
2008 * @adev: amdgpu_device pointer
2009 *
2010 * Cleanup the VM manager and free resources.
2011 */
2012void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2013{
2014 unsigned i;
2015
Christian Königbcb1ba32016-03-08 15:40:11 +01002016 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
2017 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
2018
Chris Wilsonf54d1862016-10-25 13:00:45 +01002019 dma_fence_put(adev->vm_manager.ids[i].first);
Christian König832a9022016-02-15 12:33:02 +01002020 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Chris Wilsonf54d1862016-10-25 13:00:45 +01002021 dma_fence_put(id->flushed_updates);
Dave Airlie7b624ad2016-11-07 09:37:09 +10002022 dma_fence_put(id->last_flush);
Christian Königbcb1ba32016-03-08 15:40:11 +01002023 }
Christian Königea89f8c2015-11-15 20:52:06 +01002024}