blob: 62a4c127620f070911fc39a601684bd42d0b519c [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 */
28#include <drm/drmP.h>
29#include <drm/amdgpu_drm.h>
30#include "amdgpu.h"
31#include "amdgpu_trace.h"
32
33/*
34 * GPUVM
35 * GPUVM is similar to the legacy gart on older asics, however
36 * rather than there being a single global gart table
37 * for the entire GPU, there are multiple VM page tables active
38 * at any given time. The VM page tables can contain a mix
39 * vram pages and system memory pages and system memory pages
40 * can be mapped as snooped (cached system pages) or unsnooped
41 * (uncached system pages).
42 * Each VM has an ID associated with it and there is a page table
43 * associated with each VMID. When execting a command buffer,
44 * the kernel tells the the ring what VMID to use for that command
45 * buffer. VMIDs are allocated dynamically as commands are submitted.
46 * The userspace drivers maintain their own address space and the kernel
47 * sets up their pages tables accordingly when they submit their
48 * command buffers and a VMID is assigned.
49 * Cayman/Trinity support up to 8 active VMs at any given time;
50 * SI supports 16.
51 */
52
Christian König4ff37a82016-02-26 16:18:26 +010053/* Special value that no flush is necessary */
54#define AMDGPU_VM_NO_FLUSH (~0ll)
55
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040056/* Local structure. Encapsulate some VM table update parameters to reduce
57 * the number of function parameters
58 */
59struct amdgpu_vm_update_params {
60 /* address where to copy page table entries from */
61 uint64_t src;
62 /* DMA addresses to use for mapping */
63 dma_addr_t *pages_addr;
64 /* indirect buffer to fill with commands */
65 struct amdgpu_ib *ib;
66};
67
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068/**
69 * amdgpu_vm_num_pde - return the number of page directory entries
70 *
71 * @adev: amdgpu_device pointer
72 *
Christian König8843dbb2016-01-26 12:17:11 +010073 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 */
75static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
76{
77 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
78}
79
80/**
81 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
82 *
83 * @adev: amdgpu_device pointer
84 *
Christian König8843dbb2016-01-26 12:17:11 +010085 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 */
87static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
88{
89 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
90}
91
92/**
Christian König56467eb2015-12-11 15:16:32 +010093 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 *
95 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +010096 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +010097 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 *
99 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100100 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 */
Christian König56467eb2015-12-11 15:16:32 +0100102void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
103 struct list_head *validated,
104 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105{
Christian König56467eb2015-12-11 15:16:32 +0100106 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +0100107 entry->priority = 0;
108 entry->tv.bo = &vm->page_directory->tbo;
109 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100110 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100111 list_add(&entry->tv.head, validated);
112}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113
Christian König56467eb2015-12-11 15:16:32 +0100114/**
Christian Königee1782c2015-12-11 21:01:23 +0100115 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100116 *
117 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100118 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 *
Christian Königee1782c2015-12-11 21:01:23 +0100120 * Add the page directory to the BO duplicates list
121 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 */
Christian Königee1782c2015-12-11 21:01:23 +0100123void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124{
Christian Königee1782c2015-12-11 21:01:23 +0100125 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126
127 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100128 for (i = 0; i <= vm->max_pde_used; ++i) {
129 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130
Christian Königee1782c2015-12-11 21:01:23 +0100131 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132 continue;
133
Christian Königee1782c2015-12-11 21:01:23 +0100134 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 }
Christian Königeceb8a12016-01-11 15:35:21 +0100136
137}
138
139/**
140 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
141 *
142 * @adev: amdgpu device instance
143 * @vm: vm providing the BOs
144 *
145 * Move the PT BOs to the tail of the LRU.
146 */
147void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
148 struct amdgpu_vm *vm)
149{
150 struct ttm_bo_global *glob = adev->mman.bdev.glob;
151 unsigned i;
152
153 spin_lock(&glob->lru_lock);
154 for (i = 0; i <= vm->max_pde_used; ++i) {
155 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
156
157 if (!entry->robj)
158 continue;
159
160 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
161 }
162 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163}
164
165/**
166 * amdgpu_vm_grab_id - allocate the next free VMID
167 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200169 * @ring: ring we want to submit job to
170 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100171 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172 *
Christian König7f8a5292015-07-20 16:09:40 +0200173 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174 */
Christian König7f8a5292015-07-20 16:09:40 +0200175int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100176 struct amdgpu_sync *sync, struct fence *fence,
177 unsigned *vm_id, uint64_t *vm_pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178{
Christian König4ff37a82016-02-26 16:18:26 +0100179 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180 struct amdgpu_device *adev = ring->adev;
Christian König4ff37a82016-02-26 16:18:26 +0100181 struct fence *updates = sync->last_vm_update;
Christian König794f50b2016-03-09 22:11:53 +0100182 struct amdgpu_vm_id *id;
183 unsigned i = ring->idx;
Christian Königa9a78b32016-01-21 10:19:11 +0100184 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185
Christian König94dd0a42016-01-18 17:01:42 +0100186 mutex_lock(&adev->vm_manager.lock);
187
Christian König794f50b2016-03-09 22:11:53 +0100188 /* Check if we can use a VMID already assigned to this VM */
189 do {
190 struct fence *flushed;
Christian König1c16c0a2015-11-14 21:31:40 +0100191
Christian König794f50b2016-03-09 22:11:53 +0100192 id = vm->ids[i++];
193 if (i == AMDGPU_MAX_RINGS)
194 i = 0;
Christian Königa9a78b32016-01-21 10:19:11 +0100195
Christian König794f50b2016-03-09 22:11:53 +0100196 /* Check all the prerequisites to using this VMID */
197 if (!id)
198 continue;
Christian König4ff37a82016-02-26 16:18:26 +0100199
Christian König0ea54b92016-05-04 10:20:01 +0200200 if (atomic64_read(&id->owner) != vm->client_id)
Christian König794f50b2016-03-09 22:11:53 +0100201 continue;
202
203 if (pd_addr != id->pd_gpu_addr)
204 continue;
205
Chunming Zhou178d7cb2016-04-14 15:53:55 +0800206 if (id->last_user != ring &&
Christian König794f50b2016-03-09 22:11:53 +0100207 (!id->last_flush || !fence_is_signaled(id->last_flush)))
208 continue;
209
210 flushed = id->flushed_updates;
211 if (updates && (!flushed || fence_is_later(updates, flushed)))
212 continue;
213
214 /* Good we can use this VMID */
Chunming Zhou178d7cb2016-04-14 15:53:55 +0800215 if (id->last_user == ring) {
Christian König794f50b2016-03-09 22:11:53 +0100216 r = amdgpu_sync_fence(ring->adev, sync,
217 id->first);
Christian König832a9022016-02-15 12:33:02 +0100218 if (r)
219 goto error;
Christian König1c16c0a2015-11-14 21:31:40 +0100220 }
Christian König794f50b2016-03-09 22:11:53 +0100221
222 /* And remember this submission as user of the VMID */
223 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
224 if (r)
225 goto error;
226
227 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
228 vm->ids[ring->idx] = id;
229
230 *vm_id = id - adev->vm_manager.ids;
231 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
232 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
233
234 mutex_unlock(&adev->vm_manager.lock);
235 return 0;
236
237 } while (i != ring->idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238
Christian Königbcb1ba32016-03-08 15:40:11 +0100239 id = list_first_entry(&adev->vm_manager.ids_lru,
240 struct amdgpu_vm_id,
241 list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242
Christian König832a9022016-02-15 12:33:02 +0100243 if (!amdgpu_sync_is_idle(&id->active)) {
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800244 struct list_head *head = &adev->vm_manager.ids_lru;
Christian König832a9022016-02-15 12:33:02 +0100245 struct amdgpu_vm_id *tmp;
Christian Königbcb1ba32016-03-08 15:40:11 +0100246
247 list_for_each_entry_safe(id, tmp, &adev->vm_manager.ids_lru,
248 list) {
Christian König832a9022016-02-15 12:33:02 +0100249 if (amdgpu_sync_is_idle(&id->active)) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100250 list_move(&id->list, head);
251 head = &id->list;
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800252 }
253 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100254 id = list_first_entry(&adev->vm_manager.ids_lru,
255 struct amdgpu_vm_id,
256 list);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800257 }
258
Christian König832a9022016-02-15 12:33:02 +0100259 r = amdgpu_sync_cycle_fences(sync, &id->active, fence);
260 if (r)
261 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100262
Christian König832a9022016-02-15 12:33:02 +0100263 fence_put(id->first);
264 id->first = fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100265
Christian König41d9eb22016-03-01 16:46:18 +0100266 fence_put(id->last_flush);
267 id->last_flush = NULL;
268
Christian König832a9022016-02-15 12:33:02 +0100269 fence_put(id->flushed_updates);
270 id->flushed_updates = fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100271
Christian König832a9022016-02-15 12:33:02 +0100272 id->pd_gpu_addr = pd_addr;
Christian König4ff37a82016-02-26 16:18:26 +0100273
Christian König832a9022016-02-15 12:33:02 +0100274 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Chunming Zhou68befeb2016-04-14 13:42:32 +0800275 id->last_user = ring;
Christian König0ea54b92016-05-04 10:20:01 +0200276 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100277 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278
Christian König832a9022016-02-15 12:33:02 +0100279 *vm_id = id - adev->vm_manager.ids;
280 *vm_pd_addr = pd_addr;
281 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
282
283error:
Christian König94dd0a42016-01-18 17:01:42 +0100284 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100285 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286}
287
288/**
289 * amdgpu_vm_flush - hardware flush the vm
290 *
291 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100292 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100293 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294 *
Christian König4ff37a82016-02-26 16:18:26 +0100295 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 */
Christian König41d9eb22016-03-01 16:46:18 +0100297int amdgpu_vm_flush(struct amdgpu_ring *ring,
298 unsigned vm_id, uint64_t pd_addr,
299 uint32_t gds_base, uint32_t gds_size,
300 uint32_t gws_base, uint32_t gws_size,
Chunming Zhou2ba272d2016-04-27 18:07:41 +0800301 uint32_t oa_base, uint32_t oa_size,
302 bool vmid_switch)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303{
Christian König971fe9a92016-03-01 15:09:25 +0100304 struct amdgpu_device *adev = ring->adev;
Christian Königbcb1ba32016-03-08 15:40:11 +0100305 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100306 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Christian Königbcb1ba32016-03-08 15:40:11 +0100307 id->gds_base != gds_base ||
308 id->gds_size != gds_size ||
309 id->gws_base != gws_base ||
310 id->gws_size != gws_size ||
311 id->oa_base != oa_base ||
312 id->oa_size != oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100313 int r;
Christian Königd564a062016-03-01 15:51:53 +0100314
315 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhou2ba272d2016-04-27 18:07:41 +0800316 pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed || vmid_switch))
Christian Königd564a062016-03-01 15:51:53 +0100317 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100318
Monk Liuc5637832016-04-19 20:11:32 +0800319 if (ring->funcs->emit_vm_flush &&
320 pd_addr != AMDGPU_VM_NO_FLUSH) {
Christian König41d9eb22016-03-01 16:46:18 +0100321 struct fence *fence;
322
Christian Königcffadc82016-03-01 13:34:49 +0100323 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
324 amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100325
326 mutex_lock(&adev->vm_manager.lock);
Chunming Zhou68befeb2016-04-14 13:42:32 +0800327 if ((id->pd_gpu_addr == pd_addr) && (id->last_user == ring)) {
328 r = amdgpu_fence_emit(ring, &fence);
329 if (r) {
330 mutex_unlock(&adev->vm_manager.lock);
331 return r;
332 }
333 fence_put(id->last_flush);
334 id->last_flush = fence;
335 }
Christian König41d9eb22016-03-01 16:46:18 +0100336 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400337 }
Christian Königcffadc82016-03-01 13:34:49 +0100338
Christian Königd564a062016-03-01 15:51:53 +0100339 if (gds_switch_needed) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100340 id->gds_base = gds_base;
341 id->gds_size = gds_size;
342 id->gws_base = gws_base;
343 id->gws_size = gws_size;
344 id->oa_base = oa_base;
345 id->oa_size = oa_size;
Christian Königcffadc82016-03-01 13:34:49 +0100346 amdgpu_ring_emit_gds_switch(ring, vm_id,
347 gds_base, gds_size,
348 gws_base, gws_size,
349 oa_base, oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100350 }
Christian König41d9eb22016-03-01 16:46:18 +0100351
352 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100353}
354
355/**
356 * amdgpu_vm_reset_id - reset VMID to zero
357 *
358 * @adev: amdgpu device structure
359 * @vm_id: vmid number to use
360 *
361 * Reset saved GDW, GWS and OA to force switch on next flush.
362 */
363void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
364{
Christian Königbcb1ba32016-03-08 15:40:11 +0100365 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100366
Christian Königbcb1ba32016-03-08 15:40:11 +0100367 id->gds_base = 0;
368 id->gds_size = 0;
369 id->gws_base = 0;
370 id->gws_size = 0;
371 id->oa_base = 0;
372 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400373}
374
375/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
377 *
378 * @vm: requested vm
379 * @bo: requested buffer object
380 *
Christian König8843dbb2016-01-26 12:17:11 +0100381 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382 * Search inside the @bos vm list for the requested vm
383 * Returns the found bo_va or NULL if none is found
384 *
385 * Object has to be reserved!
386 */
387struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
388 struct amdgpu_bo *bo)
389{
390 struct amdgpu_bo_va *bo_va;
391
392 list_for_each_entry(bo_va, &bo->va, bo_list) {
393 if (bo_va->vm == vm) {
394 return bo_va;
395 }
396 }
397 return NULL;
398}
399
400/**
401 * amdgpu_vm_update_pages - helper to call the right asic function
402 *
403 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400404 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405 * @pe: addr of the page entry
406 * @addr: dst addr to write into pe
407 * @count: number of page entries to update
408 * @incr: increase next addr by incr bytes
409 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400410 *
411 * Traces the parameters and calls the right asic functions
412 * to setup the page table using the DMA.
413 */
414static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400415 struct amdgpu_vm_update_params
416 *vm_update_params,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417 uint64_t pe, uint64_t addr,
418 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100419 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400420{
421 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
422
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400423 if (vm_update_params->src) {
424 amdgpu_vm_copy_pte(adev, vm_update_params->ib,
425 pe, (vm_update_params->src + (addr >> 12) * 8), count);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400426
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400427 } else if (vm_update_params->pages_addr) {
428 amdgpu_vm_write_pte(adev, vm_update_params->ib,
429 vm_update_params->pages_addr,
430 pe, addr, count, incr, flags);
Christian Königb07c9d22015-11-30 13:26:07 +0100431
432 } else if (count < 3) {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400433 amdgpu_vm_write_pte(adev, vm_update_params->ib, NULL, pe, addr,
Christian Königb07c9d22015-11-30 13:26:07 +0100434 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400435
436 } else {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400437 amdgpu_vm_set_pte_pde(adev, vm_update_params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400438 count, incr, flags);
439 }
440}
441
442/**
443 * amdgpu_vm_clear_bo - initially clear the page dir/table
444 *
445 * @adev: amdgpu_device pointer
446 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800447 *
448 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449 */
450static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100451 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452 struct amdgpu_bo *bo)
453{
Christian König2d55e452016-02-08 17:37:38 +0100454 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800455 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100456 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400457 struct amdgpu_vm_update_params vm_update_params;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400458 unsigned entries;
459 uint64_t addr;
460 int r;
461
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400462 memset(&vm_update_params, 0, sizeof(vm_update_params));
Christian König2d55e452016-02-08 17:37:38 +0100463 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
464
monk.liuca952612015-05-25 14:44:05 +0800465 r = reservation_object_reserve_shared(bo->tbo.resv);
466 if (r)
467 return r;
468
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
470 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800471 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472
473 addr = amdgpu_bo_gpu_offset(bo);
474 entries = amdgpu_bo_size(bo) / 8;
475
Christian Königd71518b2016-02-01 12:20:25 +0100476 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
477 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800478 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400480 vm_update_params.ib = &job->ibs[0];
481 amdgpu_vm_update_pages(adev, &vm_update_params, addr, 0, entries,
Christian Königd71518b2016-02-01 12:20:25 +0100482 0, 0);
483 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
484
485 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100486 r = amdgpu_job_submit(job, ring, &vm->entity,
487 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400488 if (r)
489 goto error_free;
490
Christian Königd71518b2016-02-01 12:20:25 +0100491 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800492 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800493 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800494
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400495error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100496 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800498error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499 return r;
500}
501
502/**
Christian Königb07c9d22015-11-30 13:26:07 +0100503 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 *
Christian Königb07c9d22015-11-30 13:26:07 +0100505 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506 * @addr: the unmapped addr
507 *
508 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100509 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400510 */
Christian Königb07c9d22015-11-30 13:26:07 +0100511uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400512{
513 uint64_t result;
514
Christian Königb07c9d22015-11-30 13:26:07 +0100515 if (pages_addr) {
516 /* page table offset */
517 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518
Christian Königb07c9d22015-11-30 13:26:07 +0100519 /* in case cpu page size != gpu page size*/
520 result |= addr & (~PAGE_MASK);
521
522 } else {
523 /* No mapping required */
524 result = addr;
525 }
526
527 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528
529 return result;
530}
531
532/**
533 * amdgpu_vm_update_pdes - make sure that page directory is valid
534 *
535 * @adev: amdgpu_device pointer
536 * @vm: requested vm
537 * @start: start of GPU address range
538 * @end: end of GPU address range
539 *
540 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100541 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 */
544int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
545 struct amdgpu_vm *vm)
546{
Christian König2d55e452016-02-08 17:37:38 +0100547 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548 struct amdgpu_bo *pd = vm->page_directory;
549 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
550 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
551 uint64_t last_pde = ~0, last_pt = ~0;
552 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100553 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400554 struct amdgpu_vm_update_params vm_update_params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800555 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800556
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557 int r;
558
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400559 memset(&vm_update_params, 0, sizeof(vm_update_params));
Christian König2d55e452016-02-08 17:37:38 +0100560 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
561
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 /* padding, etc. */
563 ndw = 64;
564
565 /* assume the worst case */
566 ndw += vm->max_pde_used * 6;
567
Christian Königd71518b2016-02-01 12:20:25 +0100568 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
569 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100571
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400572 vm_update_params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573
574 /* walk over the address space and update the page directory */
575 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100576 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 uint64_t pde, pt;
578
579 if (bo == NULL)
580 continue;
581
582 pt = amdgpu_bo_gpu_offset(bo);
583 if (vm->page_tables[pt_idx].addr == pt)
584 continue;
585 vm->page_tables[pt_idx].addr = pt;
586
587 pde = pd_addr + pt_idx * 8;
588 if (((last_pde + 8 * count) != pde) ||
589 ((last_pt + incr * count) != pt)) {
590
591 if (count) {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400592 amdgpu_vm_update_pages(adev, &vm_update_params,
Christian König9ab21462015-11-30 14:19:26 +0100593 last_pde, last_pt,
594 count, incr,
595 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400596 }
597
598 count = 1;
599 last_pde = pde;
600 last_pt = pt;
601 } else {
602 ++count;
603 }
604 }
605
606 if (count)
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400607 amdgpu_vm_update_pages(adev, &vm_update_params,
608 last_pde, last_pt,
609 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400611 if (vm_update_params.ib->length_dw != 0) {
612 amdgpu_ring_pad_ib(ring, vm_update_params.ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100613 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
614 AMDGPU_FENCE_OWNER_VM);
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400615 WARN_ON(vm_update_params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100616 r = amdgpu_job_submit(job, ring, &vm->entity,
617 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800618 if (r)
619 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200620
Chunming Zhou4af9f072015-08-03 12:57:31 +0800621 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200622 fence_put(vm->page_directory_fence);
623 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800624 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800625
Christian Königd71518b2016-02-01 12:20:25 +0100626 } else {
627 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800628 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629
630 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800631
632error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100633 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800634 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635}
636
637/**
638 * amdgpu_vm_frag_ptes - add fragment information to PTEs
639 *
640 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400641 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 * @pe_start: first PTE to handle
643 * @pe_end: last PTE to handle
644 * @addr: addr those PTEs should point to
645 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646 */
647static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400648 struct amdgpu_vm_update_params
649 *vm_update_params,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100651 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652{
653 /**
654 * The MC L1 TLB supports variable sized pages, based on a fragment
655 * field in the PTE. When this field is set to a non-zero value, page
656 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
657 * flags are considered valid for all PTEs within the fragment range
658 * and corresponding mappings are assumed to be physically contiguous.
659 *
660 * The L1 TLB can store a single PTE for the whole fragment,
661 * significantly increasing the space available for translation
662 * caching. This leads to large improvements in throughput when the
663 * TLB is under pressure.
664 *
665 * The L2 TLB distributes small and large fragments into two
666 * asymmetric partitions. The large fragment cache is significantly
667 * larger. Thus, we try to use large fragments wherever possible.
668 * Userspace can support this by aligning virtual base address and
669 * allocation size to the fragment size.
670 */
671
672 /* SI and newer are optimized for 64KB */
673 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
674 uint64_t frag_align = 0x80;
675
676 uint64_t frag_start = ALIGN(pe_start, frag_align);
677 uint64_t frag_end = pe_end & ~(frag_align - 1);
678
679 unsigned count;
680
Christian König31f6c1f2016-01-26 12:37:49 +0100681 /* Abort early if there isn't anything to do */
682 if (pe_start == pe_end)
683 return;
684
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 /* system pages are non continuously */
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400686 if (vm_update_params->src || vm_update_params->pages_addr ||
687 !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688
689 count = (pe_end - pe_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400690 amdgpu_vm_update_pages(adev, vm_update_params, pe_start,
Christian König9ab21462015-11-30 14:19:26 +0100691 addr, count, AMDGPU_GPU_PAGE_SIZE,
692 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 return;
694 }
695
696 /* handle the 4K area at the beginning */
697 if (pe_start != frag_start) {
698 count = (frag_start - pe_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400699 amdgpu_vm_update_pages(adev, vm_update_params, pe_start, addr,
Christian König9ab21462015-11-30 14:19:26 +0100700 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 addr += AMDGPU_GPU_PAGE_SIZE * count;
702 }
703
704 /* handle the area in the middle */
705 count = (frag_end - frag_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400706 amdgpu_vm_update_pages(adev, vm_update_params, frag_start, addr, count,
Christian König9ab21462015-11-30 14:19:26 +0100707 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708
709 /* handle the 4K area at the end */
710 if (frag_end != pe_end) {
711 addr += AMDGPU_GPU_PAGE_SIZE * count;
712 count = (pe_end - frag_end) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400713 amdgpu_vm_update_pages(adev, vm_update_params, frag_end, addr,
Christian König9ab21462015-11-30 14:19:26 +0100714 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 }
716}
717
718/**
719 * amdgpu_vm_update_ptes - make sure that page tables are valid
720 *
721 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400722 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 * @vm: requested vm
724 * @start: start of GPU address range
725 * @end: end of GPU address range
726 * @dst: destination address to map to
727 * @flags: mapping flags
728 *
Christian König8843dbb2016-01-26 12:17:11 +0100729 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 */
Christian Königa1e08d32016-01-26 11:40:46 +0100731static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400732 struct amdgpu_vm_update_params
733 *vm_update_params,
Christian Königa1e08d32016-01-26 11:40:46 +0100734 struct amdgpu_vm *vm,
Christian Königa1e08d32016-01-26 11:40:46 +0100735 uint64_t start, uint64_t end,
736 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737{
Christian König31f6c1f2016-01-26 12:37:49 +0100738 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
739
740 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 uint64_t addr;
742
743 /* walk over the address space and update the page tables */
744 for (addr = start; addr < end; ) {
745 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100746 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100748 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749
750 if ((addr & ~mask) == (end & ~mask))
751 nptes = end - addr;
752 else
753 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
754
Christian König31f6c1f2016-01-26 12:37:49 +0100755 pe_start = amdgpu_bo_gpu_offset(pt);
756 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757
Christian König31f6c1f2016-01-26 12:37:49 +0100758 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400760 amdgpu_vm_frag_ptes(adev, vm_update_params,
Christian König31f6c1f2016-01-26 12:37:49 +0100761 last_pe_start, last_pe_end,
762 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763
Christian König31f6c1f2016-01-26 12:37:49 +0100764 last_pe_start = pe_start;
765 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766 last_dst = dst;
767 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100768 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769 }
770
771 addr += nptes;
772 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
773 }
774
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400775 amdgpu_vm_frag_ptes(adev, vm_update_params, last_pe_start,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100776 last_pe_end, last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400777}
778
779/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
781 *
782 * @adev: amdgpu_device pointer
Christian Königfa3ab3c72016-03-18 21:00:35 +0100783 * @src: address where to copy page table entries from
784 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100785 * @vm: requested vm
786 * @start: start of mapped range
787 * @last: last mapped entry
788 * @flags: flags for the entries
789 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400790 * @fence: optional resulting fence
791 *
Christian Königa14faa62016-01-25 14:27:31 +0100792 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400793 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794 */
795static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100796 uint64_t src,
797 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100799 uint64_t start, uint64_t last,
800 uint32_t flags, uint64_t addr,
801 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400802{
Christian König2d55e452016-02-08 17:37:38 +0100803 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100804 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100806 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400807 struct amdgpu_vm_update_params vm_update_params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800808 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400809 int r;
810
Christian König2d55e452016-02-08 17:37:38 +0100811 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400812 memset(&vm_update_params, 0, sizeof(vm_update_params));
813 vm_update_params.src = src;
814 vm_update_params.pages_addr = pages_addr;
Christian König2d55e452016-02-08 17:37:38 +0100815
Christian Königa1e08d32016-01-26 11:40:46 +0100816 /* sync to everything on unmapping */
817 if (!(flags & AMDGPU_PTE_VALID))
818 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
819
Christian Königa14faa62016-01-25 14:27:31 +0100820 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400821
822 /*
823 * reserve space for one command every (1 << BLOCK_SIZE)
824 * entries or 2k dwords (whatever is smaller)
825 */
826 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
827
828 /* padding, etc. */
829 ndw = 64;
830
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400831 if (vm_update_params.src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400832 /* only copy commands needed */
833 ndw += ncmds * 7;
834
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400835 } else if (vm_update_params.pages_addr) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400836 /* header for write data commands */
837 ndw += ncmds * 4;
838
839 /* body of write data command */
840 ndw += nptes * 2;
841
842 } else {
843 /* set page commands needed */
844 ndw += ncmds * 10;
845
846 /* two extra commands for begin/end of fragment */
847 ndw += 2 * 10;
848 }
849
Christian Königd71518b2016-02-01 12:20:25 +0100850 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
851 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400852 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100853
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400854 vm_update_params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800855
Christian Könige86f9ce2016-02-08 12:13:05 +0100856 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100857 owner);
858 if (r)
859 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400860
Christian Königa1e08d32016-01-26 11:40:46 +0100861 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
862 if (r)
863 goto error_free;
864
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400865 amdgpu_vm_update_ptes(adev, &vm_update_params, vm, start,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100866 last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400867
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400868 amdgpu_ring_pad_ib(ring, vm_update_params.ib);
869 WARN_ON(vm_update_params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100870 r = amdgpu_job_submit(job, ring, &vm->entity,
871 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800872 if (r)
873 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874
Christian Königbf60efd2015-09-04 10:47:56 +0200875 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800876 if (fence) {
877 fence_put(*fence);
878 *fence = fence_get(f);
879 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800880 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400881 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800882
883error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100884 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800885 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400886}
887
888/**
Christian Königa14faa62016-01-25 14:27:31 +0100889 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
890 *
891 * @adev: amdgpu_device pointer
Christian König8358dce2016-03-30 10:50:25 +0200892 * @gtt_flags: flags as they are used for GTT
893 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100894 * @vm: requested vm
895 * @mapping: mapped range and flags to use for the update
896 * @addr: addr to set the area to
Christian König8358dce2016-03-30 10:50:25 +0200897 * @flags: HW flags for the mapping
Christian Königa14faa62016-01-25 14:27:31 +0100898 * @fence: optional resulting fence
899 *
900 * Split the mapping into smaller chunks so that each update fits
901 * into a SDMA IB.
902 * Returns 0 for success, -EINVAL for failure.
903 */
904static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Christian Königa14faa62016-01-25 14:27:31 +0100905 uint32_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +0200906 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +0100907 struct amdgpu_vm *vm,
908 struct amdgpu_bo_va_mapping *mapping,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100909 uint32_t flags, uint64_t addr,
910 struct fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +0100911{
912 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
913
Christian Königfa3ab3c72016-03-18 21:00:35 +0100914 uint64_t src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +0100915 int r;
916
917 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
918 * but in case of something, we filter the flags in first place
919 */
920 if (!(mapping->flags & AMDGPU_PTE_READABLE))
921 flags &= ~AMDGPU_PTE_READABLE;
922 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
923 flags &= ~AMDGPU_PTE_WRITEABLE;
924
925 trace_amdgpu_vm_bo_update(mapping);
926
Christian König8358dce2016-03-30 10:50:25 +0200927 if (pages_addr) {
Christian Königfa3ab3c72016-03-18 21:00:35 +0100928 if (flags == gtt_flags)
929 src = adev->gart.table_addr + (addr >> 12) * 8;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100930 addr = 0;
931 }
Christian Königa14faa62016-01-25 14:27:31 +0100932 addr += mapping->offset;
933
Christian König8358dce2016-03-30 10:50:25 +0200934 if (!pages_addr || src)
Christian Königfa3ab3c72016-03-18 21:00:35 +0100935 return amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100936 start, mapping->it.last,
937 flags, addr, fence);
938
939 while (start != mapping->it.last + 1) {
940 uint64_t last;
941
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500942 last = min((uint64_t)mapping->it.last, start + max_size - 1);
Christian Königfa3ab3c72016-03-18 21:00:35 +0100943 r = amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100944 start, last, flags, addr,
945 fence);
946 if (r)
947 return r;
948
949 start = last + 1;
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500950 addr += max_size * AMDGPU_GPU_PAGE_SIZE;
Christian Königa14faa62016-01-25 14:27:31 +0100951 }
952
953 return 0;
954}
955
956/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400957 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
958 *
959 * @adev: amdgpu_device pointer
960 * @bo_va: requested BO and VM object
961 * @mem: ttm mem
962 *
963 * Fill in the page table entries for @bo_va.
964 * Returns 0 for success, -EINVAL for failure.
965 *
966 * Object have to be reserved and mutex must be locked!
967 */
968int amdgpu_vm_bo_update(struct amdgpu_device *adev,
969 struct amdgpu_bo_va *bo_va,
970 struct ttm_mem_reg *mem)
971{
972 struct amdgpu_vm *vm = bo_va->vm;
973 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +0200974 dma_addr_t *pages_addr = NULL;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100975 uint32_t gtt_flags, flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976 uint64_t addr;
977 int r;
978
979 if (mem) {
Christian König8358dce2016-03-30 10:50:25 +0200980 struct ttm_dma_tt *ttm;
981
Christian Königb7d698d2015-09-07 12:32:09 +0200982 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100983 switch (mem->mem_type) {
984 case TTM_PL_TT:
Christian König8358dce2016-03-30 10:50:25 +0200985 ttm = container_of(bo_va->bo->tbo.ttm, struct
986 ttm_dma_tt, ttm);
987 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +0100988 break;
989
990 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400991 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100992 break;
993
994 default:
995 break;
996 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400997 } else {
998 addr = 0;
999 }
1000
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
Christian Königfa3ab3c72016-03-18 21:00:35 +01001002 gtt_flags = (adev == bo_va->bo->adev) ? flags : 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001003
Christian König7fc11952015-07-30 11:53:42 +02001004 spin_lock(&vm->status_lock);
1005 if (!list_empty(&bo_va->vm_status))
1006 list_splice_init(&bo_va->valids, &bo_va->invalids);
1007 spin_unlock(&vm->status_lock);
1008
1009 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König8358dce2016-03-30 10:50:25 +02001010 r = amdgpu_vm_bo_split_mapping(adev, gtt_flags, pages_addr, vm,
1011 mapping, flags, addr,
1012 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001013 if (r)
1014 return r;
1015 }
1016
Christian Königd6c10f62015-09-28 12:00:23 +02001017 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1018 list_for_each_entry(mapping, &bo_va->valids, list)
1019 trace_amdgpu_vm_bo_mapping(mapping);
1020
1021 list_for_each_entry(mapping, &bo_va->invalids, list)
1022 trace_amdgpu_vm_bo_mapping(mapping);
1023 }
1024
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001025 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001026 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001027 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +02001028 if (!mem)
1029 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001030 spin_unlock(&vm->status_lock);
1031
1032 return 0;
1033}
1034
1035/**
1036 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1037 *
1038 * @adev: amdgpu_device pointer
1039 * @vm: requested vm
1040 *
1041 * Make sure all freed BOs are cleared in the PT.
1042 * Returns 0 for success.
1043 *
1044 * PTs have to be reserved and mutex must be locked!
1045 */
1046int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1047 struct amdgpu_vm *vm)
1048{
1049 struct amdgpu_bo_va_mapping *mapping;
1050 int r;
1051
1052 while (!list_empty(&vm->freed)) {
1053 mapping = list_first_entry(&vm->freed,
1054 struct amdgpu_bo_va_mapping, list);
1055 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001056
Christian König8358dce2016-03-30 10:50:25 +02001057 r = amdgpu_vm_bo_split_mapping(adev, 0, NULL, vm, mapping,
Christian Königfa3ab3c72016-03-18 21:00:35 +01001058 0, 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001059 kfree(mapping);
1060 if (r)
1061 return r;
1062
1063 }
1064 return 0;
1065
1066}
1067
1068/**
1069 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1070 *
1071 * @adev: amdgpu_device pointer
1072 * @vm: requested vm
1073 *
1074 * Make sure all invalidated BOs are cleared in the PT.
1075 * Returns 0 for success.
1076 *
1077 * PTs have to be reserved and mutex must be locked!
1078 */
1079int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001080 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081{
monk.liucfe2c972015-05-26 15:01:54 +08001082 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001083 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001084
1085 spin_lock(&vm->status_lock);
1086 while (!list_empty(&vm->invalidated)) {
1087 bo_va = list_first_entry(&vm->invalidated,
1088 struct amdgpu_bo_va, vm_status);
1089 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001090
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
1092 if (r)
1093 return r;
1094
1095 spin_lock(&vm->status_lock);
1096 }
1097 spin_unlock(&vm->status_lock);
1098
monk.liucfe2c972015-05-26 15:01:54 +08001099 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001100 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001101
1102 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001103}
1104
1105/**
1106 * amdgpu_vm_bo_add - add a bo to a specific vm
1107 *
1108 * @adev: amdgpu_device pointer
1109 * @vm: requested vm
1110 * @bo: amdgpu buffer object
1111 *
Christian König8843dbb2016-01-26 12:17:11 +01001112 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001113 * Add @bo to the list of bos associated with the vm
1114 * Returns newly added bo_va or NULL for failure
1115 *
1116 * Object has to be reserved!
1117 */
1118struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1119 struct amdgpu_vm *vm,
1120 struct amdgpu_bo *bo)
1121{
1122 struct amdgpu_bo_va *bo_va;
1123
1124 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1125 if (bo_va == NULL) {
1126 return NULL;
1127 }
1128 bo_va->vm = vm;
1129 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 bo_va->ref_count = 1;
1131 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001132 INIT_LIST_HEAD(&bo_va->valids);
1133 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001135
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001137
1138 return bo_va;
1139}
1140
1141/**
1142 * amdgpu_vm_bo_map - map bo inside a vm
1143 *
1144 * @adev: amdgpu_device pointer
1145 * @bo_va: bo_va to store the address
1146 * @saddr: where to map the BO
1147 * @offset: requested offset in the BO
1148 * @flags: attributes of pages (read/write/valid/etc.)
1149 *
1150 * Add a mapping of the BO at the specefied addr into the VM.
1151 * Returns 0 for success, error for failure.
1152 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001153 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 */
1155int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1156 struct amdgpu_bo_va *bo_va,
1157 uint64_t saddr, uint64_t offset,
1158 uint64_t size, uint32_t flags)
1159{
1160 struct amdgpu_bo_va_mapping *mapping;
1161 struct amdgpu_vm *vm = bo_va->vm;
1162 struct interval_tree_node *it;
1163 unsigned last_pfn, pt_idx;
1164 uint64_t eaddr;
1165 int r;
1166
Christian König0be52de2015-05-18 14:37:27 +02001167 /* validate the parameters */
1168 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001169 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001170 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001171
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001173 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001174 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001175 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001176
1177 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001178 if (last_pfn >= adev->vm_manager.max_pfn) {
1179 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001180 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181 return -EINVAL;
1182 }
1183
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 saddr /= AMDGPU_GPU_PAGE_SIZE;
1185 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1186
Felix Kuehling005ae952015-11-23 17:43:48 -05001187 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 if (it) {
1189 struct amdgpu_bo_va_mapping *tmp;
1190 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1191 /* bo and tmp overlap, invalid addr */
1192 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1193 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1194 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001196 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 }
1198
1199 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1200 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001202 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001203 }
1204
1205 INIT_LIST_HEAD(&mapping->list);
1206 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001207 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208 mapping->offset = offset;
1209 mapping->flags = flags;
1210
Christian König7fc11952015-07-30 11:53:42 +02001211 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 interval_tree_insert(&mapping->it, &vm->va);
1213
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214 /* Make sure the page tables are allocated */
1215 saddr >>= amdgpu_vm_block_size;
1216 eaddr >>= amdgpu_vm_block_size;
1217
1218 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1219
1220 if (eaddr > vm->max_pde_used)
1221 vm->max_pde_used = eaddr;
1222
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 /* walk over the address space and allocate the page tables */
1224 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001225 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001226 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001227 struct amdgpu_bo *pt;
1228
Christian Königee1782c2015-12-11 21:01:23 +01001229 entry = &vm->page_tables[pt_idx].entry;
1230 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231 continue;
1232
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1234 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001235 AMDGPU_GEM_DOMAIN_VRAM,
1236 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001237 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001238 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001240
Christian König82b9c552015-11-27 16:49:00 +01001241 /* Keep a reference to the page table to avoid freeing
1242 * them up in the wrong order.
1243 */
1244 pt->parent = amdgpu_bo_ref(vm->page_directory);
1245
Christian König2bd9ccf2016-02-01 12:53:58 +01001246 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247 if (r) {
1248 amdgpu_bo_unref(&pt);
1249 goto error_free;
1250 }
1251
Christian Königee1782c2015-12-11 21:01:23 +01001252 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001253 entry->priority = 0;
1254 entry->tv.bo = &entry->robj->tbo;
1255 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +01001256 entry->user_pages = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001258 }
1259
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260 return 0;
1261
1262error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263 list_del(&mapping->list);
1264 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001265 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266 kfree(mapping);
1267
Chunming Zhouf48b2652015-10-16 14:06:19 +08001268error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001269 return r;
1270}
1271
1272/**
1273 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1274 *
1275 * @adev: amdgpu_device pointer
1276 * @bo_va: bo_va to remove the address from
1277 * @saddr: where to the BO is mapped
1278 *
1279 * Remove a mapping of the BO at the specefied addr from the VM.
1280 * Returns 0 for success, error for failure.
1281 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001282 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 */
1284int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1285 struct amdgpu_bo_va *bo_va,
1286 uint64_t saddr)
1287{
1288 struct amdgpu_bo_va_mapping *mapping;
1289 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001290 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001291
Christian König6c7fc502015-06-05 20:56:17 +02001292 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001293
Christian König7fc11952015-07-30 11:53:42 +02001294 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 if (mapping->it.start == saddr)
1296 break;
1297 }
1298
Christian König7fc11952015-07-30 11:53:42 +02001299 if (&mapping->list == &bo_va->valids) {
1300 valid = false;
1301
1302 list_for_each_entry(mapping, &bo_va->invalids, list) {
1303 if (mapping->it.start == saddr)
1304 break;
1305 }
1306
Christian König32b41ac2016-03-08 18:03:27 +01001307 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001308 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001309 }
Christian König32b41ac2016-03-08 18:03:27 +01001310
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001311 list_del(&mapping->list);
1312 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001313 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314
Christian Könige17841b2016-03-08 17:52:01 +01001315 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001316 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001317 else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 kfree(mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001319
1320 return 0;
1321}
1322
1323/**
1324 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1325 *
1326 * @adev: amdgpu_device pointer
1327 * @bo_va: requested bo_va
1328 *
Christian König8843dbb2016-01-26 12:17:11 +01001329 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001330 *
1331 * Object have to be reserved!
1332 */
1333void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1334 struct amdgpu_bo_va *bo_va)
1335{
1336 struct amdgpu_bo_va_mapping *mapping, *next;
1337 struct amdgpu_vm *vm = bo_va->vm;
1338
1339 list_del(&bo_va->bo_list);
1340
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 spin_lock(&vm->status_lock);
1342 list_del(&bo_va->vm_status);
1343 spin_unlock(&vm->status_lock);
1344
Christian König7fc11952015-07-30 11:53:42 +02001345 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001346 list_del(&mapping->list);
1347 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001348 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001349 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001350 }
Christian König7fc11952015-07-30 11:53:42 +02001351 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1352 list_del(&mapping->list);
1353 interval_tree_remove(&mapping->it, &vm->va);
1354 kfree(mapping);
1355 }
Christian König32b41ac2016-03-08 18:03:27 +01001356
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001357 fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001359}
1360
1361/**
1362 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1363 *
1364 * @adev: amdgpu_device pointer
1365 * @vm: requested vm
1366 * @bo: amdgpu buffer object
1367 *
Christian König8843dbb2016-01-26 12:17:11 +01001368 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001369 */
1370void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1371 struct amdgpu_bo *bo)
1372{
1373 struct amdgpu_bo_va *bo_va;
1374
1375 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001376 spin_lock(&bo_va->vm->status_lock);
1377 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001379 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001380 }
1381}
1382
1383/**
1384 * amdgpu_vm_init - initialize a vm instance
1385 *
1386 * @adev: amdgpu_device pointer
1387 * @vm: requested vm
1388 *
Christian König8843dbb2016-01-26 12:17:11 +01001389 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001390 */
1391int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1392{
1393 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1394 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001395 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001396 unsigned ring_instance;
1397 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001398 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001399 int i, r;
1400
Christian Königbcb1ba32016-03-08 15:40:11 +01001401 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1402 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001404 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405 spin_lock_init(&vm->status_lock);
1406 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001407 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001408 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001409
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410 pd_size = amdgpu_vm_directory_size(adev);
1411 pd_entries = amdgpu_vm_num_pdes(adev);
1412
1413 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001414 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 if (vm->page_tables == NULL) {
1416 DRM_ERROR("Cannot allocate memory for page table array\n");
1417 return -ENOMEM;
1418 }
1419
Christian König2bd9ccf2016-02-01 12:53:58 +01001420 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001421
1422 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1423 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1424 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001425 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1426 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1427 rq, amdgpu_sched_jobs);
1428 if (r)
1429 return r;
1430
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001431 vm->page_directory_fence = NULL;
1432
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001433 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001434 AMDGPU_GEM_DOMAIN_VRAM,
1435 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001436 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001438 goto error_free_sched_entity;
1439
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001440 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001441 if (r)
1442 goto error_free_page_directory;
1443
1444 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001445 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001446 if (r)
1447 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001448
1449 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001450
1451error_free_page_directory:
1452 amdgpu_bo_unref(&vm->page_directory);
1453 vm->page_directory = NULL;
1454
1455error_free_sched_entity:
1456 amd_sched_entity_fini(&ring->sched, &vm->entity);
1457
1458 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001459}
1460
1461/**
1462 * amdgpu_vm_fini - tear down a vm instance
1463 *
1464 * @adev: amdgpu_device pointer
1465 * @vm: requested vm
1466 *
Christian König8843dbb2016-01-26 12:17:11 +01001467 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001468 * Unbind the VM and remove all bos from the vm bo list
1469 */
1470void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1471{
1472 struct amdgpu_bo_va_mapping *mapping, *tmp;
1473 int i;
1474
Christian König2d55e452016-02-08 17:37:38 +01001475 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001476
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001477 if (!RB_EMPTY_ROOT(&vm->va)) {
1478 dev_err(adev->dev, "still active bo inside vm\n");
1479 }
1480 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1481 list_del(&mapping->list);
1482 interval_tree_remove(&mapping->it, &vm->va);
1483 kfree(mapping);
1484 }
1485 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1486 list_del(&mapping->list);
1487 kfree(mapping);
1488 }
1489
1490 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001491 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001492 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493
1494 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001495 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001496}
Christian Königea89f8c2015-11-15 20:52:06 +01001497
1498/**
Christian Königa9a78b32016-01-21 10:19:11 +01001499 * amdgpu_vm_manager_init - init the VM manager
1500 *
1501 * @adev: amdgpu_device pointer
1502 *
1503 * Initialize the VM manager structures
1504 */
1505void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1506{
1507 unsigned i;
1508
1509 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1510
1511 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001512 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1513 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01001514 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01001515 list_add_tail(&adev->vm_manager.ids[i].list,
1516 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001517 }
Christian König2d55e452016-02-08 17:37:38 +01001518
1519 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02001520 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001521}
1522
1523/**
Christian Königea89f8c2015-11-15 20:52:06 +01001524 * amdgpu_vm_manager_fini - cleanup VM manager
1525 *
1526 * @adev: amdgpu_device pointer
1527 *
1528 * Cleanup the VM manager and free resources.
1529 */
1530void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1531{
1532 unsigned i;
1533
Christian Königbcb1ba32016-03-08 15:40:11 +01001534 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
1535 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
1536
Christian König832a9022016-02-15 12:33:02 +01001537 fence_put(adev->vm_manager.ids[i].first);
1538 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Christian Königbcb1ba32016-03-08 15:40:11 +01001539 fence_put(id->flushed_updates);
1540 }
Christian Königea89f8c2015-11-15 20:52:06 +01001541}