blob: f38978d8834c0f53ee98cb85e07a737f798ae8b6 [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önig8d76001e2016-05-23 16:00:32 +0200182 struct amdgpu_vm_id *id, *idle;
Christian König794f50b2016-03-09 22:11:53 +0100183 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önig36fd7c52016-05-23 15:30:08 +0200188 /* Check if we have an idle VMID */
Christian König8d76001e2016-05-23 16:00:32 +0200189 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
190 if (amdgpu_sync_is_idle(&idle->active, ring))
Christian König36fd7c52016-05-23 15:30:08 +0200191 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192
Christian König36fd7c52016-05-23 15:30:08 +0200193 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100194
Christian König36fd7c52016-05-23 15:30:08 +0200195 /* If we can't find a idle VMID to use, just wait for the oldest */
Christian König8d76001e2016-05-23 16:00:32 +0200196 if (&idle->list == &adev->vm_manager.ids_lru) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100197 id = list_first_entry(&adev->vm_manager.ids_lru,
198 struct amdgpu_vm_id,
199 list);
Christian König8d76001e2016-05-23 16:00:32 +0200200 } else {
201 /* Check if we can use a VMID already assigned to this VM */
202 do {
203 struct fence *flushed;
204
205 id = vm->ids[i++];
206 if (i == AMDGPU_MAX_RINGS)
207 i = 0;
208
209 /* Check all the prerequisites to using this VMID */
210 if (!id)
211 continue;
212
213 if (atomic64_read(&id->owner) != vm->client_id)
214 continue;
215
216 if (pd_addr != id->pd_gpu_addr)
217 continue;
218
219 if (id->last_user != ring && (!id->last_flush ||
220 !fence_is_signaled(id->last_flush)))
221 continue;
222
223 flushed = id->flushed_updates;
224 if (updates && (!flushed ||
225 fence_is_later(updates, flushed)))
226 continue;
227
228 /* Good we can use this VMID */
229 if (id->last_user == ring) {
230 r = amdgpu_sync_fence(ring->adev, sync,
231 id->first);
232 if (r)
233 goto error;
234 }
235
236 /* And remember this submission as user of the VMID */
237 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
238 if (r)
239 goto error;
240
241 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
242 vm->ids[ring->idx] = id;
243
244 *vm_id = id - adev->vm_manager.ids;
245 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
246 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id,
247 *vm_pd_addr);
248
249 mutex_unlock(&adev->vm_manager.lock);
250 return 0;
251
252 } while (i != ring->idx);
253
254 /* Still no ID to use? Then use the idle one found earlier */
255 id = idle;
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800256 }
257
Christian König832a9022016-02-15 12:33:02 +0100258 r = amdgpu_sync_cycle_fences(sync, &id->active, fence);
259 if (r)
260 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100261
Christian König832a9022016-02-15 12:33:02 +0100262 fence_put(id->first);
263 id->first = fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100264
Christian König41d9eb22016-03-01 16:46:18 +0100265 fence_put(id->last_flush);
266 id->last_flush = NULL;
267
Christian König832a9022016-02-15 12:33:02 +0100268 fence_put(id->flushed_updates);
269 id->flushed_updates = fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100270
Christian König832a9022016-02-15 12:33:02 +0100271 id->pd_gpu_addr = pd_addr;
Christian König4ff37a82016-02-26 16:18:26 +0100272
Christian König832a9022016-02-15 12:33:02 +0100273 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Chunming Zhou68befeb2016-04-14 13:42:32 +0800274 id->last_user = ring;
Christian König0ea54b92016-05-04 10:20:01 +0200275 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100276 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400277
Christian König832a9022016-02-15 12:33:02 +0100278 *vm_id = id - adev->vm_manager.ids;
279 *vm_pd_addr = pd_addr;
280 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
281
282error:
Christian König94dd0a42016-01-18 17:01:42 +0100283 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100284 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285}
286
287/**
288 * amdgpu_vm_flush - hardware flush the vm
289 *
290 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100291 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100292 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400293 *
Christian König4ff37a82016-02-26 16:18:26 +0100294 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400295 */
Christian König41d9eb22016-03-01 16:46:18 +0100296int amdgpu_vm_flush(struct amdgpu_ring *ring,
297 unsigned vm_id, uint64_t pd_addr,
298 uint32_t gds_base, uint32_t gds_size,
299 uint32_t gws_base, uint32_t gws_size,
300 uint32_t oa_base, uint32_t oa_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400301{
Christian König971fe9a92016-03-01 15:09:25 +0100302 struct amdgpu_device *adev = ring->adev;
Christian Königbcb1ba32016-03-08 15:40:11 +0100303 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100304 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Christian Königbcb1ba32016-03-08 15:40:11 +0100305 id->gds_base != gds_base ||
306 id->gds_size != gds_size ||
307 id->gws_base != gws_base ||
308 id->gws_size != gws_size ||
309 id->oa_base != oa_base ||
310 id->oa_size != oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100311 int r;
Christian Königd564a062016-03-01 15:51:53 +0100312
313 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufe707662016-04-27 18:07:41 +0800314 pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed ||
315 ring->type == AMDGPU_RING_TYPE_COMPUTE))
Christian Königd564a062016-03-01 15:51:53 +0100316 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100317
Monk Liuc5637832016-04-19 20:11:32 +0800318 if (ring->funcs->emit_vm_flush &&
319 pd_addr != AMDGPU_VM_NO_FLUSH) {
Christian König41d9eb22016-03-01 16:46:18 +0100320 struct fence *fence;
321
Christian Königcffadc82016-03-01 13:34:49 +0100322 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
323 amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100324
325 mutex_lock(&adev->vm_manager.lock);
Chunming Zhou68befeb2016-04-14 13:42:32 +0800326 if ((id->pd_gpu_addr == pd_addr) && (id->last_user == ring)) {
327 r = amdgpu_fence_emit(ring, &fence);
328 if (r) {
329 mutex_unlock(&adev->vm_manager.lock);
330 return r;
331 }
332 fence_put(id->last_flush);
333 id->last_flush = fence;
334 }
Christian König41d9eb22016-03-01 16:46:18 +0100335 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400336 }
Christian Königcffadc82016-03-01 13:34:49 +0100337
Christian Königd564a062016-03-01 15:51:53 +0100338 if (gds_switch_needed) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100339 id->gds_base = gds_base;
340 id->gds_size = gds_size;
341 id->gws_base = gws_base;
342 id->gws_size = gws_size;
343 id->oa_base = oa_base;
344 id->oa_size = oa_size;
Christian Königcffadc82016-03-01 13:34:49 +0100345 amdgpu_ring_emit_gds_switch(ring, vm_id,
346 gds_base, gds_size,
347 gws_base, gws_size,
348 oa_base, oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100349 }
Christian König41d9eb22016-03-01 16:46:18 +0100350
351 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100352}
353
354/**
355 * amdgpu_vm_reset_id - reset VMID to zero
356 *
357 * @adev: amdgpu device structure
358 * @vm_id: vmid number to use
359 *
360 * Reset saved GDW, GWS and OA to force switch on next flush.
361 */
362void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
363{
Christian Königbcb1ba32016-03-08 15:40:11 +0100364 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100365
Christian Königbcb1ba32016-03-08 15:40:11 +0100366 id->gds_base = 0;
367 id->gds_size = 0;
368 id->gws_base = 0;
369 id->gws_size = 0;
370 id->oa_base = 0;
371 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400372}
373
374/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400375 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
376 *
377 * @vm: requested vm
378 * @bo: requested buffer object
379 *
Christian König8843dbb2016-01-26 12:17:11 +0100380 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381 * Search inside the @bos vm list for the requested vm
382 * Returns the found bo_va or NULL if none is found
383 *
384 * Object has to be reserved!
385 */
386struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
387 struct amdgpu_bo *bo)
388{
389 struct amdgpu_bo_va *bo_va;
390
391 list_for_each_entry(bo_va, &bo->va, bo_list) {
392 if (bo_va->vm == vm) {
393 return bo_va;
394 }
395 }
396 return NULL;
397}
398
399/**
400 * amdgpu_vm_update_pages - helper to call the right asic function
401 *
402 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400403 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400404 * @pe: addr of the page entry
405 * @addr: dst addr to write into pe
406 * @count: number of page entries to update
407 * @incr: increase next addr by incr bytes
408 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409 *
410 * Traces the parameters and calls the right asic functions
411 * to setup the page table using the DMA.
412 */
413static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400414 struct amdgpu_vm_update_params
415 *vm_update_params,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400416 uint64_t pe, uint64_t addr,
417 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100418 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419{
420 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
421
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400422 if (vm_update_params->src) {
423 amdgpu_vm_copy_pte(adev, vm_update_params->ib,
424 pe, (vm_update_params->src + (addr >> 12) * 8), count);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400426 } else if (vm_update_params->pages_addr) {
427 amdgpu_vm_write_pte(adev, vm_update_params->ib,
428 vm_update_params->pages_addr,
429 pe, addr, count, incr, flags);
Christian Königb07c9d22015-11-30 13:26:07 +0100430
431 } else if (count < 3) {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400432 amdgpu_vm_write_pte(adev, vm_update_params->ib, NULL, pe, addr,
Christian Königb07c9d22015-11-30 13:26:07 +0100433 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434
435 } else {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400436 amdgpu_vm_set_pte_pde(adev, vm_update_params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437 count, incr, flags);
438 }
439}
440
441/**
442 * amdgpu_vm_clear_bo - initially clear the page dir/table
443 *
444 * @adev: amdgpu_device pointer
445 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800446 *
447 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448 */
449static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100450 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 struct amdgpu_bo *bo)
452{
Christian König2d55e452016-02-08 17:37:38 +0100453 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800454 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100455 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400456 struct amdgpu_vm_update_params vm_update_params;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457 unsigned entries;
458 uint64_t addr;
459 int r;
460
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400461 memset(&vm_update_params, 0, sizeof(vm_update_params));
Christian König2d55e452016-02-08 17:37:38 +0100462 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
463
monk.liuca952612015-05-25 14:44:05 +0800464 r = reservation_object_reserve_shared(bo->tbo.resv);
465 if (r)
466 return r;
467
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400468 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
469 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800470 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471
472 addr = amdgpu_bo_gpu_offset(bo);
473 entries = amdgpu_bo_size(bo) / 8;
474
Christian Königd71518b2016-02-01 12:20:25 +0100475 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
476 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800477 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400478
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400479 vm_update_params.ib = &job->ibs[0];
480 amdgpu_vm_update_pages(adev, &vm_update_params, addr, 0, entries,
Christian Königd71518b2016-02-01 12:20:25 +0100481 0, 0);
482 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
483
484 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100485 r = amdgpu_job_submit(job, ring, &vm->entity,
486 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400487 if (r)
488 goto error_free;
489
Christian Königd71518b2016-02-01 12:20:25 +0100490 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800491 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800492 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800493
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400494error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100495 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800497error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400498 return r;
499}
500
501/**
Christian Königb07c9d22015-11-30 13:26:07 +0100502 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 *
Christian Königb07c9d22015-11-30 13:26:07 +0100504 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 * @addr: the unmapped addr
506 *
507 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100508 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509 */
Christian Königb07c9d22015-11-30 13:26:07 +0100510uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511{
512 uint64_t result;
513
Christian Königb07c9d22015-11-30 13:26:07 +0100514 if (pages_addr) {
515 /* page table offset */
516 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517
Christian Königb07c9d22015-11-30 13:26:07 +0100518 /* in case cpu page size != gpu page size*/
519 result |= addr & (~PAGE_MASK);
520
521 } else {
522 /* No mapping required */
523 result = addr;
524 }
525
526 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527
528 return result;
529}
530
531/**
532 * amdgpu_vm_update_pdes - make sure that page directory is valid
533 *
534 * @adev: amdgpu_device pointer
535 * @vm: requested vm
536 * @start: start of GPU address range
537 * @end: end of GPU address range
538 *
539 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100540 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 */
543int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
544 struct amdgpu_vm *vm)
545{
Christian König2d55e452016-02-08 17:37:38 +0100546 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 struct amdgpu_bo *pd = vm->page_directory;
548 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
549 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
550 uint64_t last_pde = ~0, last_pt = ~0;
551 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100552 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400553 struct amdgpu_vm_update_params vm_update_params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800554 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800555
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 int r;
557
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400558 memset(&vm_update_params, 0, sizeof(vm_update_params));
Christian König2d55e452016-02-08 17:37:38 +0100559 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
560
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 /* padding, etc. */
562 ndw = 64;
563
564 /* assume the worst case */
565 ndw += vm->max_pde_used * 6;
566
Christian Königd71518b2016-02-01 12:20:25 +0100567 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
568 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100570
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400571 vm_update_params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572
573 /* walk over the address space and update the page directory */
574 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100575 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576 uint64_t pde, pt;
577
578 if (bo == NULL)
579 continue;
580
581 pt = amdgpu_bo_gpu_offset(bo);
582 if (vm->page_tables[pt_idx].addr == pt)
583 continue;
584 vm->page_tables[pt_idx].addr = pt;
585
586 pde = pd_addr + pt_idx * 8;
587 if (((last_pde + 8 * count) != pde) ||
588 ((last_pt + incr * count) != pt)) {
589
590 if (count) {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400591 amdgpu_vm_update_pages(adev, &vm_update_params,
Christian König9ab21462015-11-30 14:19:26 +0100592 last_pde, last_pt,
593 count, incr,
594 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 }
596
597 count = 1;
598 last_pde = pde;
599 last_pt = pt;
600 } else {
601 ++count;
602 }
603 }
604
605 if (count)
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400606 amdgpu_vm_update_pages(adev, &vm_update_params,
607 last_pde, last_pt,
608 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400609
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400610 if (vm_update_params.ib->length_dw != 0) {
611 amdgpu_ring_pad_ib(ring, vm_update_params.ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100612 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
613 AMDGPU_FENCE_OWNER_VM);
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400614 WARN_ON(vm_update_params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100615 r = amdgpu_job_submit(job, ring, &vm->entity,
616 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800617 if (r)
618 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200619
Chunming Zhou4af9f072015-08-03 12:57:31 +0800620 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200621 fence_put(vm->page_directory_fence);
622 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800623 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800624
Christian Königd71518b2016-02-01 12:20:25 +0100625 } else {
626 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800627 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628
629 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800630
631error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100632 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800633 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634}
635
636/**
637 * amdgpu_vm_frag_ptes - add fragment information to PTEs
638 *
639 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400640 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641 * @pe_start: first PTE to handle
642 * @pe_end: last PTE to handle
643 * @addr: addr those PTEs should point to
644 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 */
646static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400647 struct amdgpu_vm_update_params
648 *vm_update_params,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100650 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651{
652 /**
653 * The MC L1 TLB supports variable sized pages, based on a fragment
654 * field in the PTE. When this field is set to a non-zero value, page
655 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
656 * flags are considered valid for all PTEs within the fragment range
657 * and corresponding mappings are assumed to be physically contiguous.
658 *
659 * The L1 TLB can store a single PTE for the whole fragment,
660 * significantly increasing the space available for translation
661 * caching. This leads to large improvements in throughput when the
662 * TLB is under pressure.
663 *
664 * The L2 TLB distributes small and large fragments into two
665 * asymmetric partitions. The large fragment cache is significantly
666 * larger. Thus, we try to use large fragments wherever possible.
667 * Userspace can support this by aligning virtual base address and
668 * allocation size to the fragment size.
669 */
670
671 /* SI and newer are optimized for 64KB */
672 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
673 uint64_t frag_align = 0x80;
674
675 uint64_t frag_start = ALIGN(pe_start, frag_align);
676 uint64_t frag_end = pe_end & ~(frag_align - 1);
677
678 unsigned count;
679
Christian König31f6c1f2016-01-26 12:37:49 +0100680 /* Abort early if there isn't anything to do */
681 if (pe_start == pe_end)
682 return;
683
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 /* system pages are non continuously */
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400685 if (vm_update_params->src || vm_update_params->pages_addr ||
686 !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687
688 count = (pe_end - pe_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400689 amdgpu_vm_update_pages(adev, vm_update_params, pe_start,
Christian König9ab21462015-11-30 14:19:26 +0100690 addr, count, AMDGPU_GPU_PAGE_SIZE,
691 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692 return;
693 }
694
695 /* handle the 4K area at the beginning */
696 if (pe_start != frag_start) {
697 count = (frag_start - pe_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400698 amdgpu_vm_update_pages(adev, vm_update_params, pe_start, addr,
Christian König9ab21462015-11-30 14:19:26 +0100699 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 addr += AMDGPU_GPU_PAGE_SIZE * count;
701 }
702
703 /* handle the area in the middle */
704 count = (frag_end - frag_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400705 amdgpu_vm_update_pages(adev, vm_update_params, frag_start, addr, count,
Christian König9ab21462015-11-30 14:19:26 +0100706 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707
708 /* handle the 4K area at the end */
709 if (frag_end != pe_end) {
710 addr += AMDGPU_GPU_PAGE_SIZE * count;
711 count = (pe_end - frag_end) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400712 amdgpu_vm_update_pages(adev, vm_update_params, frag_end, addr,
Christian König9ab21462015-11-30 14:19:26 +0100713 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400714 }
715}
716
717/**
718 * amdgpu_vm_update_ptes - make sure that page tables are valid
719 *
720 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400721 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722 * @vm: requested vm
723 * @start: start of GPU address range
724 * @end: end of GPU address range
725 * @dst: destination address to map to
726 * @flags: mapping flags
727 *
Christian König8843dbb2016-01-26 12:17:11 +0100728 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 */
Christian Königa1e08d32016-01-26 11:40:46 +0100730static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400731 struct amdgpu_vm_update_params
732 *vm_update_params,
Christian Königa1e08d32016-01-26 11:40:46 +0100733 struct amdgpu_vm *vm,
Christian Königa1e08d32016-01-26 11:40:46 +0100734 uint64_t start, uint64_t end,
735 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400736{
Christian König31f6c1f2016-01-26 12:37:49 +0100737 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
738
739 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400740 uint64_t addr;
741
742 /* walk over the address space and update the page tables */
743 for (addr = start; addr < end; ) {
744 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100745 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100747 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748
749 if ((addr & ~mask) == (end & ~mask))
750 nptes = end - addr;
751 else
752 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
753
Christian König31f6c1f2016-01-26 12:37:49 +0100754 pe_start = amdgpu_bo_gpu_offset(pt);
755 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756
Christian König31f6c1f2016-01-26 12:37:49 +0100757 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400759 amdgpu_vm_frag_ptes(adev, vm_update_params,
Christian König31f6c1f2016-01-26 12:37:49 +0100760 last_pe_start, last_pe_end,
761 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762
Christian König31f6c1f2016-01-26 12:37:49 +0100763 last_pe_start = pe_start;
764 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765 last_dst = dst;
766 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100767 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400768 }
769
770 addr += nptes;
771 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
772 }
773
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400774 amdgpu_vm_frag_ptes(adev, vm_update_params, last_pe_start,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100775 last_pe_end, last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776}
777
778/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
780 *
781 * @adev: amdgpu_device pointer
Christian Königfa3ab3c2016-03-18 21:00:35 +0100782 * @src: address where to copy page table entries from
783 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100784 * @vm: requested vm
785 * @start: start of mapped range
786 * @last: last mapped entry
787 * @flags: flags for the entries
788 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 * @fence: optional resulting fence
790 *
Christian Königa14faa62016-01-25 14:27:31 +0100791 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400792 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400793 */
794static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100795 uint64_t src,
796 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100798 uint64_t start, uint64_t last,
799 uint32_t flags, uint64_t addr,
800 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801{
Christian König2d55e452016-02-08 17:37:38 +0100802 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100803 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100805 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400806 struct amdgpu_vm_update_params vm_update_params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800807 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808 int r;
809
Christian König2d55e452016-02-08 17:37:38 +0100810 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400811 memset(&vm_update_params, 0, sizeof(vm_update_params));
812 vm_update_params.src = src;
813 vm_update_params.pages_addr = pages_addr;
Christian König2d55e452016-02-08 17:37:38 +0100814
Christian Königa1e08d32016-01-26 11:40:46 +0100815 /* sync to everything on unmapping */
816 if (!(flags & AMDGPU_PTE_VALID))
817 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
818
Christian Königa14faa62016-01-25 14:27:31 +0100819 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820
821 /*
822 * reserve space for one command every (1 << BLOCK_SIZE)
823 * entries or 2k dwords (whatever is smaller)
824 */
825 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
826
827 /* padding, etc. */
828 ndw = 64;
829
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400830 if (vm_update_params.src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400831 /* only copy commands needed */
832 ndw += ncmds * 7;
833
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400834 } else if (vm_update_params.pages_addr) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400835 /* header for write data commands */
836 ndw += ncmds * 4;
837
838 /* body of write data command */
839 ndw += nptes * 2;
840
841 } else {
842 /* set page commands needed */
843 ndw += ncmds * 10;
844
845 /* two extra commands for begin/end of fragment */
846 ndw += 2 * 10;
847 }
848
Christian Königd71518b2016-02-01 12:20:25 +0100849 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
850 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400851 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100852
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400853 vm_update_params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800854
Christian Könige86f9ce2016-02-08 12:13:05 +0100855 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100856 owner);
857 if (r)
858 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400859
Christian Königa1e08d32016-01-26 11:40:46 +0100860 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
861 if (r)
862 goto error_free;
863
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400864 amdgpu_vm_update_ptes(adev, &vm_update_params, vm, start,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100865 last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400866
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400867 amdgpu_ring_pad_ib(ring, vm_update_params.ib);
868 WARN_ON(vm_update_params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100869 r = amdgpu_job_submit(job, ring, &vm->entity,
870 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800871 if (r)
872 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873
Christian Königbf60efd2015-09-04 10:47:56 +0200874 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800875 if (fence) {
876 fence_put(*fence);
877 *fence = fence_get(f);
878 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800879 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800881
882error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100883 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800884 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885}
886
887/**
Christian Königa14faa62016-01-25 14:27:31 +0100888 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
889 *
890 * @adev: amdgpu_device pointer
Christian König8358dce2016-03-30 10:50:25 +0200891 * @gtt_flags: flags as they are used for GTT
892 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100893 * @vm: requested vm
894 * @mapping: mapped range and flags to use for the update
895 * @addr: addr to set the area to
Christian König8358dce2016-03-30 10:50:25 +0200896 * @flags: HW flags for the mapping
Christian Königa14faa62016-01-25 14:27:31 +0100897 * @fence: optional resulting fence
898 *
899 * Split the mapping into smaller chunks so that each update fits
900 * into a SDMA IB.
901 * Returns 0 for success, -EINVAL for failure.
902 */
903static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Christian Königa14faa62016-01-25 14:27:31 +0100904 uint32_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +0200905 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +0100906 struct amdgpu_vm *vm,
907 struct amdgpu_bo_va_mapping *mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100908 uint32_t flags, uint64_t addr,
909 struct fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +0100910{
911 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
912
Christian Königfa3ab3c2016-03-18 21:00:35 +0100913 uint64_t src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +0100914 int r;
915
916 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
917 * but in case of something, we filter the flags in first place
918 */
919 if (!(mapping->flags & AMDGPU_PTE_READABLE))
920 flags &= ~AMDGPU_PTE_READABLE;
921 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
922 flags &= ~AMDGPU_PTE_WRITEABLE;
923
924 trace_amdgpu_vm_bo_update(mapping);
925
Christian König8358dce2016-03-30 10:50:25 +0200926 if (pages_addr) {
Christian Königfa3ab3c2016-03-18 21:00:35 +0100927 if (flags == gtt_flags)
928 src = adev->gart.table_addr + (addr >> 12) * 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100929 addr = 0;
930 }
Christian Königa14faa62016-01-25 14:27:31 +0100931 addr += mapping->offset;
932
Christian König8358dce2016-03-30 10:50:25 +0200933 if (!pages_addr || src)
Christian Königfa3ab3c2016-03-18 21:00:35 +0100934 return amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100935 start, mapping->it.last,
936 flags, addr, fence);
937
938 while (start != mapping->it.last + 1) {
939 uint64_t last;
940
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500941 last = min((uint64_t)mapping->it.last, start + max_size - 1);
Christian Königfa3ab3c2016-03-18 21:00:35 +0100942 r = amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100943 start, last, flags, addr,
944 fence);
945 if (r)
946 return r;
947
948 start = last + 1;
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500949 addr += max_size * AMDGPU_GPU_PAGE_SIZE;
Christian Königa14faa62016-01-25 14:27:31 +0100950 }
951
952 return 0;
953}
954
955/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
957 *
958 * @adev: amdgpu_device pointer
959 * @bo_va: requested BO and VM object
960 * @mem: ttm mem
961 *
962 * Fill in the page table entries for @bo_va.
963 * Returns 0 for success, -EINVAL for failure.
964 *
965 * Object have to be reserved and mutex must be locked!
966 */
967int amdgpu_vm_bo_update(struct amdgpu_device *adev,
968 struct amdgpu_bo_va *bo_va,
969 struct ttm_mem_reg *mem)
970{
971 struct amdgpu_vm *vm = bo_va->vm;
972 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +0200973 dma_addr_t *pages_addr = NULL;
Christian Königfa3ab3c2016-03-18 21:00:35 +0100974 uint32_t gtt_flags, flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 uint64_t addr;
976 int r;
977
978 if (mem) {
Christian König8358dce2016-03-30 10:50:25 +0200979 struct ttm_dma_tt *ttm;
980
Christian Königb7d698d2015-09-07 12:32:09 +0200981 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100982 switch (mem->mem_type) {
983 case TTM_PL_TT:
Christian König8358dce2016-03-30 10:50:25 +0200984 ttm = container_of(bo_va->bo->tbo.ttm, struct
985 ttm_dma_tt, ttm);
986 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +0100987 break;
988
989 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100991 break;
992
993 default:
994 break;
995 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 } else {
997 addr = 0;
998 }
999
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
Christian Königfa3ab3c2016-03-18 21:00:35 +01001001 gtt_flags = (adev == bo_va->bo->adev) ? flags : 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001002
Christian König7fc11952015-07-30 11:53:42 +02001003 spin_lock(&vm->status_lock);
1004 if (!list_empty(&bo_va->vm_status))
1005 list_splice_init(&bo_va->valids, &bo_va->invalids);
1006 spin_unlock(&vm->status_lock);
1007
1008 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König8358dce2016-03-30 10:50:25 +02001009 r = amdgpu_vm_bo_split_mapping(adev, gtt_flags, pages_addr, vm,
1010 mapping, flags, addr,
1011 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001012 if (r)
1013 return r;
1014 }
1015
Christian Königd6c10f62015-09-28 12:00:23 +02001016 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1017 list_for_each_entry(mapping, &bo_va->valids, list)
1018 trace_amdgpu_vm_bo_mapping(mapping);
1019
1020 list_for_each_entry(mapping, &bo_va->invalids, list)
1021 trace_amdgpu_vm_bo_mapping(mapping);
1022 }
1023
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001025 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001026 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +02001027 if (!mem)
1028 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029 spin_unlock(&vm->status_lock);
1030
1031 return 0;
1032}
1033
1034/**
1035 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1036 *
1037 * @adev: amdgpu_device pointer
1038 * @vm: requested vm
1039 *
1040 * Make sure all freed BOs are cleared in the PT.
1041 * Returns 0 for success.
1042 *
1043 * PTs have to be reserved and mutex must be locked!
1044 */
1045int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1046 struct amdgpu_vm *vm)
1047{
1048 struct amdgpu_bo_va_mapping *mapping;
1049 int r;
1050
1051 while (!list_empty(&vm->freed)) {
1052 mapping = list_first_entry(&vm->freed,
1053 struct amdgpu_bo_va_mapping, list);
1054 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001055
Christian König8358dce2016-03-30 10:50:25 +02001056 r = amdgpu_vm_bo_split_mapping(adev, 0, NULL, vm, mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001057 0, 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001058 kfree(mapping);
1059 if (r)
1060 return r;
1061
1062 }
1063 return 0;
1064
1065}
1066
1067/**
1068 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1069 *
1070 * @adev: amdgpu_device pointer
1071 * @vm: requested vm
1072 *
1073 * Make sure all invalidated BOs are cleared in the PT.
1074 * Returns 0 for success.
1075 *
1076 * PTs have to be reserved and mutex must be locked!
1077 */
1078int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001079 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080{
monk.liucfe2c972015-05-26 15:01:54 +08001081 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001082 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083
1084 spin_lock(&vm->status_lock);
1085 while (!list_empty(&vm->invalidated)) {
1086 bo_va = list_first_entry(&vm->invalidated,
1087 struct amdgpu_bo_va, vm_status);
1088 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001089
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
1091 if (r)
1092 return r;
1093
1094 spin_lock(&vm->status_lock);
1095 }
1096 spin_unlock(&vm->status_lock);
1097
monk.liucfe2c972015-05-26 15:01:54 +08001098 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001099 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001100
1101 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001102}
1103
1104/**
1105 * amdgpu_vm_bo_add - add a bo to a specific vm
1106 *
1107 * @adev: amdgpu_device pointer
1108 * @vm: requested vm
1109 * @bo: amdgpu buffer object
1110 *
Christian König8843dbb2016-01-26 12:17:11 +01001111 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001112 * Add @bo to the list of bos associated with the vm
1113 * Returns newly added bo_va or NULL for failure
1114 *
1115 * Object has to be reserved!
1116 */
1117struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1118 struct amdgpu_vm *vm,
1119 struct amdgpu_bo *bo)
1120{
1121 struct amdgpu_bo_va *bo_va;
1122
1123 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1124 if (bo_va == NULL) {
1125 return NULL;
1126 }
1127 bo_va->vm = vm;
1128 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129 bo_va->ref_count = 1;
1130 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001131 INIT_LIST_HEAD(&bo_va->valids);
1132 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001134
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001135 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136
1137 return bo_va;
1138}
1139
1140/**
1141 * amdgpu_vm_bo_map - map bo inside a vm
1142 *
1143 * @adev: amdgpu_device pointer
1144 * @bo_va: bo_va to store the address
1145 * @saddr: where to map the BO
1146 * @offset: requested offset in the BO
1147 * @flags: attributes of pages (read/write/valid/etc.)
1148 *
1149 * Add a mapping of the BO at the specefied addr into the VM.
1150 * Returns 0 for success, error for failure.
1151 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001152 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153 */
1154int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1155 struct amdgpu_bo_va *bo_va,
1156 uint64_t saddr, uint64_t offset,
1157 uint64_t size, uint32_t flags)
1158{
1159 struct amdgpu_bo_va_mapping *mapping;
1160 struct amdgpu_vm *vm = bo_va->vm;
1161 struct interval_tree_node *it;
1162 unsigned last_pfn, pt_idx;
1163 uint64_t eaddr;
1164 int r;
1165
Christian König0be52de2015-05-18 14:37:27 +02001166 /* validate the parameters */
1167 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001168 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001169 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001170
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001172 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001173 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001175
1176 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001177 if (last_pfn >= adev->vm_manager.max_pfn) {
1178 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001179 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001180 return -EINVAL;
1181 }
1182
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001183 saddr /= AMDGPU_GPU_PAGE_SIZE;
1184 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1185
Felix Kuehling005ae952015-11-23 17:43:48 -05001186 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187 if (it) {
1188 struct amdgpu_bo_va_mapping *tmp;
1189 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1190 /* bo and tmp overlap, invalid addr */
1191 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1192 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1193 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001194 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001195 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196 }
1197
1198 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1199 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001200 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001201 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001202 }
1203
1204 INIT_LIST_HEAD(&mapping->list);
1205 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001206 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 mapping->offset = offset;
1208 mapping->flags = flags;
1209
Christian König7fc11952015-07-30 11:53:42 +02001210 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211 interval_tree_insert(&mapping->it, &vm->va);
1212
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213 /* Make sure the page tables are allocated */
1214 saddr >>= amdgpu_vm_block_size;
1215 eaddr >>= amdgpu_vm_block_size;
1216
1217 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1218
1219 if (eaddr > vm->max_pde_used)
1220 vm->max_pde_used = eaddr;
1221
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 /* walk over the address space and allocate the page tables */
1223 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001224 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001225 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226 struct amdgpu_bo *pt;
1227
Christian Königee1782c2015-12-11 21:01:23 +01001228 entry = &vm->page_tables[pt_idx].entry;
1229 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230 continue;
1231
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1233 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001234 AMDGPU_GEM_DOMAIN_VRAM,
1235 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001236 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001237 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001238 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001239
Christian König82b9c552015-11-27 16:49:00 +01001240 /* Keep a reference to the page table to avoid freeing
1241 * them up in the wrong order.
1242 */
1243 pt->parent = amdgpu_bo_ref(vm->page_directory);
1244
Christian König2bd9ccf2016-02-01 12:53:58 +01001245 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246 if (r) {
1247 amdgpu_bo_unref(&pt);
1248 goto error_free;
1249 }
1250
Christian Königee1782c2015-12-11 21:01:23 +01001251 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001252 entry->priority = 0;
1253 entry->tv.bo = &entry->robj->tbo;
1254 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +01001255 entry->user_pages = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257 }
1258
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 return 0;
1260
1261error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262 list_del(&mapping->list);
1263 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001264 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001265 kfree(mapping);
1266
Chunming Zhouf48b2652015-10-16 14:06:19 +08001267error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001268 return r;
1269}
1270
1271/**
1272 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1273 *
1274 * @adev: amdgpu_device pointer
1275 * @bo_va: bo_va to remove the address from
1276 * @saddr: where to the BO is mapped
1277 *
1278 * Remove a mapping of the BO at the specefied addr from the VM.
1279 * Returns 0 for success, error for failure.
1280 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001281 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 */
1283int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1284 struct amdgpu_bo_va *bo_va,
1285 uint64_t saddr)
1286{
1287 struct amdgpu_bo_va_mapping *mapping;
1288 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001289 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290
Christian König6c7fc502015-06-05 20:56:17 +02001291 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001292
Christian König7fc11952015-07-30 11:53:42 +02001293 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001294 if (mapping->it.start == saddr)
1295 break;
1296 }
1297
Christian König7fc11952015-07-30 11:53:42 +02001298 if (&mapping->list == &bo_va->valids) {
1299 valid = false;
1300
1301 list_for_each_entry(mapping, &bo_va->invalids, list) {
1302 if (mapping->it.start == saddr)
1303 break;
1304 }
1305
Christian König32b41ac2016-03-08 18:03:27 +01001306 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001307 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308 }
Christian König32b41ac2016-03-08 18:03:27 +01001309
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001310 list_del(&mapping->list);
1311 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001312 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001313
Christian Könige17841b2016-03-08 17:52:01 +01001314 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001316 else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001317 kfree(mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318
1319 return 0;
1320}
1321
1322/**
1323 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1324 *
1325 * @adev: amdgpu_device pointer
1326 * @bo_va: requested bo_va
1327 *
Christian König8843dbb2016-01-26 12:17:11 +01001328 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001329 *
1330 * Object have to be reserved!
1331 */
1332void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1333 struct amdgpu_bo_va *bo_va)
1334{
1335 struct amdgpu_bo_va_mapping *mapping, *next;
1336 struct amdgpu_vm *vm = bo_va->vm;
1337
1338 list_del(&bo_va->bo_list);
1339
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001340 spin_lock(&vm->status_lock);
1341 list_del(&bo_va->vm_status);
1342 spin_unlock(&vm->status_lock);
1343
Christian König7fc11952015-07-30 11:53:42 +02001344 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001345 list_del(&mapping->list);
1346 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001347 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001348 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 }
Christian König7fc11952015-07-30 11:53:42 +02001350 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1351 list_del(&mapping->list);
1352 interval_tree_remove(&mapping->it, &vm->va);
1353 kfree(mapping);
1354 }
Christian König32b41ac2016-03-08 18:03:27 +01001355
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001356 fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001357 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358}
1359
1360/**
1361 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1362 *
1363 * @adev: amdgpu_device pointer
1364 * @vm: requested vm
1365 * @bo: amdgpu buffer object
1366 *
Christian König8843dbb2016-01-26 12:17:11 +01001367 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001368 */
1369void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1370 struct amdgpu_bo *bo)
1371{
1372 struct amdgpu_bo_va *bo_va;
1373
1374 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001375 spin_lock(&bo_va->vm->status_lock);
1376 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001378 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 }
1380}
1381
1382/**
1383 * amdgpu_vm_init - initialize a vm instance
1384 *
1385 * @adev: amdgpu_device pointer
1386 * @vm: requested vm
1387 *
Christian König8843dbb2016-01-26 12:17:11 +01001388 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001389 */
1390int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1391{
1392 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1393 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001394 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001395 unsigned ring_instance;
1396 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001397 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 int i, r;
1399
Christian Königbcb1ba32016-03-08 15:40:11 +01001400 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1401 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001402 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001403 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001404 spin_lock_init(&vm->status_lock);
1405 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001406 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001407 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001408
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001409 pd_size = amdgpu_vm_directory_size(adev);
1410 pd_entries = amdgpu_vm_num_pdes(adev);
1411
1412 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001413 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414 if (vm->page_tables == NULL) {
1415 DRM_ERROR("Cannot allocate memory for page table array\n");
1416 return -ENOMEM;
1417 }
1418
Christian König2bd9ccf2016-02-01 12:53:58 +01001419 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001420
1421 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1422 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1423 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001424 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1425 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1426 rq, amdgpu_sched_jobs);
1427 if (r)
1428 return r;
1429
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001430 vm->page_directory_fence = NULL;
1431
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001433 AMDGPU_GEM_DOMAIN_VRAM,
1434 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001435 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001436 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001437 goto error_free_sched_entity;
1438
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001439 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001440 if (r)
1441 goto error_free_page_directory;
1442
1443 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001444 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001445 if (r)
1446 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447
1448 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001449
1450error_free_page_directory:
1451 amdgpu_bo_unref(&vm->page_directory);
1452 vm->page_directory = NULL;
1453
1454error_free_sched_entity:
1455 amd_sched_entity_fini(&ring->sched, &vm->entity);
1456
1457 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458}
1459
1460/**
1461 * amdgpu_vm_fini - tear down a vm instance
1462 *
1463 * @adev: amdgpu_device pointer
1464 * @vm: requested vm
1465 *
Christian König8843dbb2016-01-26 12:17:11 +01001466 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001467 * Unbind the VM and remove all bos from the vm bo list
1468 */
1469void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1470{
1471 struct amdgpu_bo_va_mapping *mapping, *tmp;
1472 int i;
1473
Christian König2d55e452016-02-08 17:37:38 +01001474 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001475
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476 if (!RB_EMPTY_ROOT(&vm->va)) {
1477 dev_err(adev->dev, "still active bo inside vm\n");
1478 }
1479 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1480 list_del(&mapping->list);
1481 interval_tree_remove(&mapping->it, &vm->va);
1482 kfree(mapping);
1483 }
1484 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1485 list_del(&mapping->list);
1486 kfree(mapping);
1487 }
1488
1489 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001490 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001491 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492
1493 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001494 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001495}
Christian Königea89f8c2015-11-15 20:52:06 +01001496
1497/**
Christian Königa9a78b32016-01-21 10:19:11 +01001498 * amdgpu_vm_manager_init - init the VM manager
1499 *
1500 * @adev: amdgpu_device pointer
1501 *
1502 * Initialize the VM manager structures
1503 */
1504void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1505{
1506 unsigned i;
1507
1508 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1509
1510 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001511 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1512 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01001513 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01001514 list_add_tail(&adev->vm_manager.ids[i].list,
1515 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001516 }
Christian König2d55e452016-02-08 17:37:38 +01001517
1518 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02001519 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001520}
1521
1522/**
Christian Königea89f8c2015-11-15 20:52:06 +01001523 * amdgpu_vm_manager_fini - cleanup VM manager
1524 *
1525 * @adev: amdgpu_device pointer
1526 *
1527 * Cleanup the VM manager and free resources.
1528 */
1529void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1530{
1531 unsigned i;
1532
Christian Königbcb1ba32016-03-08 15:40:11 +01001533 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
1534 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
1535
Christian König832a9022016-02-15 12:33:02 +01001536 fence_put(adev->vm_manager.ids[i].first);
1537 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Christian Königbcb1ba32016-03-08 15:40:11 +01001538 fence_put(id->flushed_updates);
1539 }
Christian Königea89f8c2015-11-15 20:52:06 +01001540}