blob: ea708cb94862fba8bd9a4f8256b7cb16427c2cb3 [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
Alex Deucherd38ceaf2015-04-20 16:55:21 -040056/**
57 * amdgpu_vm_num_pde - return the number of page directory entries
58 *
59 * @adev: amdgpu_device pointer
60 *
Christian König8843dbb2016-01-26 12:17:11 +010061 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062 */
63static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
64{
65 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
66}
67
68/**
69 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
70 *
71 * @adev: amdgpu_device pointer
72 *
Christian König8843dbb2016-01-26 12:17:11 +010073 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 */
75static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
76{
77 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
78}
79
80/**
Christian König56467eb2015-12-11 15:16:32 +010081 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082 *
83 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +010084 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +010085 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 *
87 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +010088 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 */
Christian König56467eb2015-12-11 15:16:32 +010090void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
91 struct list_head *validated,
92 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093{
Christian König56467eb2015-12-11 15:16:32 +010094 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +010095 entry->priority = 0;
96 entry->tv.bo = &vm->page_directory->tbo;
97 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +010098 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +010099 list_add(&entry->tv.head, validated);
100}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101
Christian König56467eb2015-12-11 15:16:32 +0100102/**
Christian Königee1782c2015-12-11 21:01:23 +0100103 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100104 *
105 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100106 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107 *
Christian Königee1782c2015-12-11 21:01:23 +0100108 * Add the page directory to the BO duplicates list
109 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110 */
Christian Königee1782c2015-12-11 21:01:23 +0100111void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112{
Christian Königee1782c2015-12-11 21:01:23 +0100113 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114
115 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100116 for (i = 0; i <= vm->max_pde_used; ++i) {
117 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118
Christian Königee1782c2015-12-11 21:01:23 +0100119 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 continue;
121
Christian Königee1782c2015-12-11 21:01:23 +0100122 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 }
Christian Königeceb8a12016-01-11 15:35:21 +0100124
125}
126
127/**
128 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
129 *
130 * @adev: amdgpu device instance
131 * @vm: vm providing the BOs
132 *
133 * Move the PT BOs to the tail of the LRU.
134 */
135void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
136 struct amdgpu_vm *vm)
137{
138 struct ttm_bo_global *glob = adev->mman.bdev.glob;
139 unsigned i;
140
141 spin_lock(&glob->lru_lock);
142 for (i = 0; i <= vm->max_pde_used; ++i) {
143 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
144
145 if (!entry->robj)
146 continue;
147
148 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
149 }
150 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151}
152
153/**
154 * amdgpu_vm_grab_id - allocate the next free VMID
155 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200157 * @ring: ring we want to submit job to
158 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100159 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 *
Christian König7f8a5292015-07-20 16:09:40 +0200161 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162 */
Christian König7f8a5292015-07-20 16:09:40 +0200163int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100164 struct amdgpu_sync *sync, struct fence *fence,
165 unsigned *vm_id, uint64_t *vm_pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166{
Christian König4ff37a82016-02-26 16:18:26 +0100167 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 struct amdgpu_device *adev = ring->adev;
Christian König4ff37a82016-02-26 16:18:26 +0100169 struct fence *updates = sync->last_vm_update;
Christian König794f50b2016-03-09 22:11:53 +0100170 struct amdgpu_vm_id *id;
171 unsigned i = ring->idx;
Christian Königa9a78b32016-01-21 10:19:11 +0100172 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173
Christian König94dd0a42016-01-18 17:01:42 +0100174 mutex_lock(&adev->vm_manager.lock);
175
Christian König794f50b2016-03-09 22:11:53 +0100176 /* Check if we can use a VMID already assigned to this VM */
177 do {
178 struct fence *flushed;
Christian König1c16c0a2015-11-14 21:31:40 +0100179
Christian König794f50b2016-03-09 22:11:53 +0100180 id = vm->ids[i++];
181 if (i == AMDGPU_MAX_RINGS)
182 i = 0;
Christian Königa9a78b32016-01-21 10:19:11 +0100183
Christian König794f50b2016-03-09 22:11:53 +0100184 /* Check all the prerequisites to using this VMID */
185 if (!id)
186 continue;
Christian König4ff37a82016-02-26 16:18:26 +0100187
Christian König0ea54b92016-05-04 10:20:01 +0200188 if (atomic64_read(&id->owner) != vm->client_id)
Christian König794f50b2016-03-09 22:11:53 +0100189 continue;
190
191 if (pd_addr != id->pd_gpu_addr)
192 continue;
193
Chunming Zhou178d7cb2016-04-14 15:53:55 +0800194 if (id->last_user != ring &&
Christian König794f50b2016-03-09 22:11:53 +0100195 (!id->last_flush || !fence_is_signaled(id->last_flush)))
196 continue;
197
198 flushed = id->flushed_updates;
199 if (updates && (!flushed || fence_is_later(updates, flushed)))
200 continue;
201
202 /* Good we can use this VMID */
Chunming Zhou178d7cb2016-04-14 15:53:55 +0800203 if (id->last_user == ring) {
Christian König794f50b2016-03-09 22:11:53 +0100204 r = amdgpu_sync_fence(ring->adev, sync,
205 id->first);
Christian König832a9022016-02-15 12:33:02 +0100206 if (r)
207 goto error;
Christian König1c16c0a2015-11-14 21:31:40 +0100208 }
Christian König794f50b2016-03-09 22:11:53 +0100209
210 /* And remember this submission as user of the VMID */
211 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
212 if (r)
213 goto error;
214
215 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
216 vm->ids[ring->idx] = id;
217
218 *vm_id = id - adev->vm_manager.ids;
219 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
220 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
221
222 mutex_unlock(&adev->vm_manager.lock);
223 return 0;
224
225 } while (i != ring->idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226
Christian Königbcb1ba32016-03-08 15:40:11 +0100227 id = list_first_entry(&adev->vm_manager.ids_lru,
228 struct amdgpu_vm_id,
229 list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400230
Christian König832a9022016-02-15 12:33:02 +0100231 if (!amdgpu_sync_is_idle(&id->active)) {
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800232 struct list_head *head = &adev->vm_manager.ids_lru;
Christian König832a9022016-02-15 12:33:02 +0100233 struct amdgpu_vm_id *tmp;
Christian Königbcb1ba32016-03-08 15:40:11 +0100234
235 list_for_each_entry_safe(id, tmp, &adev->vm_manager.ids_lru,
236 list) {
Christian König832a9022016-02-15 12:33:02 +0100237 if (amdgpu_sync_is_idle(&id->active)) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100238 list_move(&id->list, head);
239 head = &id->list;
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800240 }
241 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100242 id = list_first_entry(&adev->vm_manager.ids_lru,
243 struct amdgpu_vm_id,
244 list);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800245 }
246
Christian König832a9022016-02-15 12:33:02 +0100247 r = amdgpu_sync_cycle_fences(sync, &id->active, fence);
248 if (r)
249 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100250
Christian König832a9022016-02-15 12:33:02 +0100251 fence_put(id->first);
252 id->first = fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100253
Christian König41d9eb22016-03-01 16:46:18 +0100254 fence_put(id->last_flush);
255 id->last_flush = NULL;
256
Christian König832a9022016-02-15 12:33:02 +0100257 fence_put(id->flushed_updates);
258 id->flushed_updates = fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100259
Christian König832a9022016-02-15 12:33:02 +0100260 id->pd_gpu_addr = pd_addr;
Christian König4ff37a82016-02-26 16:18:26 +0100261
Christian König832a9022016-02-15 12:33:02 +0100262 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Chunming Zhou68befeb2016-04-14 13:42:32 +0800263 id->last_user = ring;
Christian König0ea54b92016-05-04 10:20:01 +0200264 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100265 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266
Christian König832a9022016-02-15 12:33:02 +0100267 *vm_id = id - adev->vm_manager.ids;
268 *vm_pd_addr = pd_addr;
269 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
270
271error:
Christian König94dd0a42016-01-18 17:01:42 +0100272 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100273 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400274}
275
276/**
277 * amdgpu_vm_flush - hardware flush the vm
278 *
279 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100280 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100281 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282 *
Christian König4ff37a82016-02-26 16:18:26 +0100283 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284 */
Christian König41d9eb22016-03-01 16:46:18 +0100285int amdgpu_vm_flush(struct amdgpu_ring *ring,
286 unsigned vm_id, uint64_t pd_addr,
287 uint32_t gds_base, uint32_t gds_size,
288 uint32_t gws_base, uint32_t gws_size,
289 uint32_t oa_base, uint32_t oa_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290{
Christian König971fe9a92016-03-01 15:09:25 +0100291 struct amdgpu_device *adev = ring->adev;
Christian Königbcb1ba32016-03-08 15:40:11 +0100292 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100293 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Christian Königbcb1ba32016-03-08 15:40:11 +0100294 id->gds_base != gds_base ||
295 id->gds_size != gds_size ||
296 id->gws_base != gws_base ||
297 id->gws_size != gws_size ||
298 id->oa_base != oa_base ||
299 id->oa_size != oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100300 int r;
Christian Königd564a062016-03-01 15:51:53 +0100301
302 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufe707662016-04-27 18:07:41 +0800303 pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed ||
304 ring->type == AMDGPU_RING_TYPE_COMPUTE))
Christian Königd564a062016-03-01 15:51:53 +0100305 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100306
Monk Liuc5637832016-04-19 20:11:32 +0800307 if (ring->funcs->emit_vm_flush &&
308 pd_addr != AMDGPU_VM_NO_FLUSH) {
Christian König41d9eb22016-03-01 16:46:18 +0100309 struct fence *fence;
310
Christian Königcffadc82016-03-01 13:34:49 +0100311 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
312 amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100313
314 mutex_lock(&adev->vm_manager.lock);
Chunming Zhou68befeb2016-04-14 13:42:32 +0800315 if ((id->pd_gpu_addr == pd_addr) && (id->last_user == ring)) {
316 r = amdgpu_fence_emit(ring, &fence);
317 if (r) {
318 mutex_unlock(&adev->vm_manager.lock);
319 return r;
320 }
321 fence_put(id->last_flush);
322 id->last_flush = fence;
323 }
Christian König41d9eb22016-03-01 16:46:18 +0100324 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400325 }
Christian Königcffadc82016-03-01 13:34:49 +0100326
Christian Königd564a062016-03-01 15:51:53 +0100327 if (gds_switch_needed) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100328 id->gds_base = gds_base;
329 id->gds_size = gds_size;
330 id->gws_base = gws_base;
331 id->gws_size = gws_size;
332 id->oa_base = oa_base;
333 id->oa_size = oa_size;
Christian Königcffadc82016-03-01 13:34:49 +0100334 amdgpu_ring_emit_gds_switch(ring, vm_id,
335 gds_base, gds_size,
336 gws_base, gws_size,
337 oa_base, oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100338 }
Christian König41d9eb22016-03-01 16:46:18 +0100339
340 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100341}
342
343/**
344 * amdgpu_vm_reset_id - reset VMID to zero
345 *
346 * @adev: amdgpu device structure
347 * @vm_id: vmid number to use
348 *
349 * Reset saved GDW, GWS and OA to force switch on next flush.
350 */
351void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
352{
Christian Königbcb1ba32016-03-08 15:40:11 +0100353 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100354
Christian Königbcb1ba32016-03-08 15:40:11 +0100355 id->gds_base = 0;
356 id->gds_size = 0;
357 id->gws_base = 0;
358 id->gws_size = 0;
359 id->oa_base = 0;
360 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400361}
362
363/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400364 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
365 *
366 * @vm: requested vm
367 * @bo: requested buffer object
368 *
Christian König8843dbb2016-01-26 12:17:11 +0100369 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400370 * Search inside the @bos vm list for the requested vm
371 * Returns the found bo_va or NULL if none is found
372 *
373 * Object has to be reserved!
374 */
375struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
376 struct amdgpu_bo *bo)
377{
378 struct amdgpu_bo_va *bo_va;
379
380 list_for_each_entry(bo_va, &bo->va, bo_list) {
381 if (bo_va->vm == vm) {
382 return bo_va;
383 }
384 }
385 return NULL;
386}
387
388/**
389 * amdgpu_vm_update_pages - helper to call the right asic function
390 *
391 * @adev: amdgpu_device pointer
Christian Königfa3ab3c72016-03-18 21:00:35 +0100392 * @src: address where to copy page table entries from
393 * @pages_addr: DMA addresses to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400394 * @ib: indirect buffer to fill with commands
395 * @pe: addr of the page entry
396 * @addr: dst addr to write into pe
397 * @count: number of page entries to update
398 * @incr: increase next addr by incr bytes
399 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400400 *
401 * Traces the parameters and calls the right asic functions
402 * to setup the page table using the DMA.
403 */
404static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100405 uint64_t src,
406 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400407 struct amdgpu_ib *ib,
408 uint64_t pe, uint64_t addr,
409 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100410 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400411{
412 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
413
Christian Königfa3ab3c72016-03-18 21:00:35 +0100414 if (src) {
415 src += (addr >> 12) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400416 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
417
Christian Königfa3ab3c72016-03-18 21:00:35 +0100418 } else if (pages_addr) {
Christian Königb07c9d22015-11-30 13:26:07 +0100419 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
420 count, incr, flags);
421
422 } else if (count < 3) {
423 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
424 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425
426 } else {
427 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
428 count, incr, flags);
429 }
430}
431
432/**
433 * amdgpu_vm_clear_bo - initially clear the page dir/table
434 *
435 * @adev: amdgpu_device pointer
436 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800437 *
438 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400439 */
440static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100441 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400442 struct amdgpu_bo *bo)
443{
Christian König2d55e452016-02-08 17:37:38 +0100444 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800445 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100446 struct amdgpu_job *job;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400447 unsigned entries;
448 uint64_t addr;
449 int r;
450
Christian König2d55e452016-02-08 17:37:38 +0100451 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
452
monk.liuca952612015-05-25 14:44:05 +0800453 r = reservation_object_reserve_shared(bo->tbo.resv);
454 if (r)
455 return r;
456
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
458 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800459 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460
461 addr = amdgpu_bo_gpu_offset(bo);
462 entries = amdgpu_bo_size(bo) / 8;
463
Christian Königd71518b2016-02-01 12:20:25 +0100464 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
465 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800466 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400467
Christian Königfa3ab3c72016-03-18 21:00:35 +0100468 amdgpu_vm_update_pages(adev, 0, NULL, &job->ibs[0], addr, 0, entries,
Christian Königd71518b2016-02-01 12:20:25 +0100469 0, 0);
470 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
471
472 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100473 r = amdgpu_job_submit(job, ring, &vm->entity,
474 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475 if (r)
476 goto error_free;
477
Christian Königd71518b2016-02-01 12:20:25 +0100478 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800479 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800480 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800481
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400482error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100483 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400484
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800485error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 return r;
487}
488
489/**
Christian Königb07c9d22015-11-30 13:26:07 +0100490 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491 *
Christian Königb07c9d22015-11-30 13:26:07 +0100492 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493 * @addr: the unmapped addr
494 *
495 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100496 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497 */
Christian Königb07c9d22015-11-30 13:26:07 +0100498uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499{
500 uint64_t result;
501
Christian Königb07c9d22015-11-30 13:26:07 +0100502 if (pages_addr) {
503 /* page table offset */
504 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505
Christian Königb07c9d22015-11-30 13:26:07 +0100506 /* in case cpu page size != gpu page size*/
507 result |= addr & (~PAGE_MASK);
508
509 } else {
510 /* No mapping required */
511 result = addr;
512 }
513
514 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515
516 return result;
517}
518
519/**
520 * amdgpu_vm_update_pdes - make sure that page directory is valid
521 *
522 * @adev: amdgpu_device pointer
523 * @vm: requested vm
524 * @start: start of GPU address range
525 * @end: end of GPU address range
526 *
527 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100528 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 */
531int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
532 struct amdgpu_vm *vm)
533{
Christian König2d55e452016-02-08 17:37:38 +0100534 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535 struct amdgpu_bo *pd = vm->page_directory;
536 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
537 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
538 uint64_t last_pde = ~0, last_pt = ~0;
539 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100540 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800541 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800542 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800543
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 int r;
545
Christian König2d55e452016-02-08 17:37:38 +0100546 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
547
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548 /* padding, etc. */
549 ndw = 64;
550
551 /* assume the worst case */
552 ndw += vm->max_pde_used * 6;
553
Christian Königd71518b2016-02-01 12:20:25 +0100554 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
555 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100557
558 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559
560 /* walk over the address space and update the page directory */
561 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100562 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 uint64_t pde, pt;
564
565 if (bo == NULL)
566 continue;
567
568 pt = amdgpu_bo_gpu_offset(bo);
569 if (vm->page_tables[pt_idx].addr == pt)
570 continue;
571 vm->page_tables[pt_idx].addr = pt;
572
573 pde = pd_addr + pt_idx * 8;
574 if (((last_pde + 8 * count) != pde) ||
575 ((last_pt + incr * count) != pt)) {
576
577 if (count) {
Christian Königfa3ab3c72016-03-18 21:00:35 +0100578 amdgpu_vm_update_pages(adev, 0, NULL, ib,
Christian König9ab21462015-11-30 14:19:26 +0100579 last_pde, last_pt,
580 count, incr,
581 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 }
583
584 count = 1;
585 last_pde = pde;
586 last_pt = pt;
587 } else {
588 ++count;
589 }
590 }
591
592 if (count)
Christian Königfa3ab3c72016-03-18 21:00:35 +0100593 amdgpu_vm_update_pages(adev, 0, NULL, ib, last_pde, last_pt,
Christian König9ab21462015-11-30 14:19:26 +0100594 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800596 if (ib->length_dw != 0) {
Christian König9e5d53092016-01-31 12:20:55 +0100597 amdgpu_ring_pad_ib(ring, ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100598 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
599 AMDGPU_FENCE_OWNER_VM);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800600 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100601 r = amdgpu_job_submit(job, ring, &vm->entity,
602 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800603 if (r)
604 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200605
Chunming Zhou4af9f072015-08-03 12:57:31 +0800606 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200607 fence_put(vm->page_directory_fence);
608 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800609 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800610
Christian Königd71518b2016-02-01 12:20:25 +0100611 } else {
612 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800613 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614
615 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800616
617error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100618 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800619 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620}
621
622/**
623 * amdgpu_vm_frag_ptes - add fragment information to PTEs
624 *
625 * @adev: amdgpu_device pointer
Christian Königfa3ab3c72016-03-18 21:00:35 +0100626 * @src: address where to copy page table entries from
627 * @pages_addr: DMA addresses to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628 * @ib: IB for the update
629 * @pe_start: first PTE to handle
630 * @pe_end: last PTE to handle
631 * @addr: addr those PTEs should point to
632 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633 */
634static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100635 uint64_t src,
636 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 struct amdgpu_ib *ib,
638 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100639 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640{
641 /**
642 * The MC L1 TLB supports variable sized pages, based on a fragment
643 * field in the PTE. When this field is set to a non-zero value, page
644 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
645 * flags are considered valid for all PTEs within the fragment range
646 * and corresponding mappings are assumed to be physically contiguous.
647 *
648 * The L1 TLB can store a single PTE for the whole fragment,
649 * significantly increasing the space available for translation
650 * caching. This leads to large improvements in throughput when the
651 * TLB is under pressure.
652 *
653 * The L2 TLB distributes small and large fragments into two
654 * asymmetric partitions. The large fragment cache is significantly
655 * larger. Thus, we try to use large fragments wherever possible.
656 * Userspace can support this by aligning virtual base address and
657 * allocation size to the fragment size.
658 */
659
660 /* SI and newer are optimized for 64KB */
661 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
662 uint64_t frag_align = 0x80;
663
664 uint64_t frag_start = ALIGN(pe_start, frag_align);
665 uint64_t frag_end = pe_end & ~(frag_align - 1);
666
667 unsigned count;
668
Christian König31f6c1f2016-01-26 12:37:49 +0100669 /* Abort early if there isn't anything to do */
670 if (pe_start == pe_end)
671 return;
672
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 /* system pages are non continuously */
Christian Königfa3ab3c72016-03-18 21:00:35 +0100674 if (src || pages_addr || !(flags & AMDGPU_PTE_VALID) ||
675 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676
677 count = (pe_end - pe_start) / 8;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100678 amdgpu_vm_update_pages(adev, src, pages_addr, ib, pe_start,
Christian König9ab21462015-11-30 14:19:26 +0100679 addr, count, AMDGPU_GPU_PAGE_SIZE,
680 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 return;
682 }
683
684 /* handle the 4K area at the beginning */
685 if (pe_start != frag_start) {
686 count = (frag_start - pe_start) / 8;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100687 amdgpu_vm_update_pages(adev, 0, NULL, ib, pe_start, addr,
Christian König9ab21462015-11-30 14:19:26 +0100688 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689 addr += AMDGPU_GPU_PAGE_SIZE * count;
690 }
691
692 /* handle the area in the middle */
693 count = (frag_end - frag_start) / 8;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100694 amdgpu_vm_update_pages(adev, 0, NULL, ib, frag_start, addr, count,
Christian König9ab21462015-11-30 14:19:26 +0100695 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696
697 /* handle the 4K area at the end */
698 if (frag_end != pe_end) {
699 addr += AMDGPU_GPU_PAGE_SIZE * count;
700 count = (pe_end - frag_end) / 8;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100701 amdgpu_vm_update_pages(adev, 0, NULL, ib, frag_end, addr,
Christian König9ab21462015-11-30 14:19:26 +0100702 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 }
704}
705
706/**
707 * amdgpu_vm_update_ptes - make sure that page tables are valid
708 *
709 * @adev: amdgpu_device pointer
Christian Königfa3ab3c72016-03-18 21:00:35 +0100710 * @src: address where to copy page table entries from
711 * @pages_addr: DMA addresses to use for mapping
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712 * @vm: requested vm
713 * @start: start of GPU address range
714 * @end: end of GPU address range
715 * @dst: destination address to map to
716 * @flags: mapping flags
717 *
Christian König8843dbb2016-01-26 12:17:11 +0100718 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719 */
Christian Königa1e08d32016-01-26 11:40:46 +0100720static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100721 uint64_t src,
722 dma_addr_t *pages_addr,
Christian Königa1e08d32016-01-26 11:40:46 +0100723 struct amdgpu_vm *vm,
724 struct amdgpu_ib *ib,
725 uint64_t start, uint64_t end,
726 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727{
Christian König31f6c1f2016-01-26 12:37:49 +0100728 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
729
730 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731 uint64_t addr;
732
733 /* walk over the address space and update the page tables */
734 for (addr = start; addr < end; ) {
735 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100736 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 unsigned nptes;
Christian König31f6c1f2016-01-26 12:37:49 +0100738 uint64_t pe_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739
740 if ((addr & ~mask) == (end & ~mask))
741 nptes = end - addr;
742 else
743 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
744
Christian König31f6c1f2016-01-26 12:37:49 +0100745 pe_start = amdgpu_bo_gpu_offset(pt);
746 pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747
Christian König31f6c1f2016-01-26 12:37:49 +0100748 if (last_pe_end != pe_start) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749
Christian Königfa3ab3c72016-03-18 21:00:35 +0100750 amdgpu_vm_frag_ptes(adev, src, pages_addr, ib,
Christian König31f6c1f2016-01-26 12:37:49 +0100751 last_pe_start, last_pe_end,
752 last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753
Christian König31f6c1f2016-01-26 12:37:49 +0100754 last_pe_start = pe_start;
755 last_pe_end = pe_start + 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756 last_dst = dst;
757 } else {
Christian König31f6c1f2016-01-26 12:37:49 +0100758 last_pe_end += 8 * nptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759 }
760
761 addr += nptes;
762 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
763 }
764
Christian Königfa3ab3c72016-03-18 21:00:35 +0100765 amdgpu_vm_frag_ptes(adev, src, pages_addr, ib, last_pe_start,
766 last_pe_end, last_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400767}
768
769/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
771 *
772 * @adev: amdgpu_device pointer
Christian Königfa3ab3c72016-03-18 21:00:35 +0100773 * @src: address where to copy page table entries from
774 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100775 * @vm: requested vm
776 * @start: start of mapped range
777 * @last: last mapped entry
778 * @flags: flags for the entries
779 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780 * @fence: optional resulting fence
781 *
Christian Königa14faa62016-01-25 14:27:31 +0100782 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400783 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784 */
785static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100786 uint64_t src,
787 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400788 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100789 uint64_t start, uint64_t last,
790 uint32_t flags, uint64_t addr,
791 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400792{
Christian König2d55e452016-02-08 17:37:38 +0100793 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100794 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400795 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100796 struct amdgpu_job *job;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800797 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800798 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400799 int r;
800
Christian König2d55e452016-02-08 17:37:38 +0100801 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
802
Christian Königa1e08d32016-01-26 11:40:46 +0100803 /* sync to everything on unmapping */
804 if (!(flags & AMDGPU_PTE_VALID))
805 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
806
Christian Königa14faa62016-01-25 14:27:31 +0100807 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808
809 /*
810 * reserve space for one command every (1 << BLOCK_SIZE)
811 * entries or 2k dwords (whatever is smaller)
812 */
813 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
814
815 /* padding, etc. */
816 ndw = 64;
817
Christian Königfa3ab3c72016-03-18 21:00:35 +0100818 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819 /* only copy commands needed */
820 ndw += ncmds * 7;
821
Christian Königfa3ab3c72016-03-18 21:00:35 +0100822 } else if (pages_addr) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823 /* header for write data commands */
824 ndw += ncmds * 4;
825
826 /* body of write data command */
827 ndw += nptes * 2;
828
829 } else {
830 /* set page commands needed */
831 ndw += ncmds * 10;
832
833 /* two extra commands for begin/end of fragment */
834 ndw += 2 * 10;
835 }
836
Christian Königd71518b2016-02-01 12:20:25 +0100837 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
838 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400839 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100840
841 ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800842
Christian Könige86f9ce2016-02-08 12:13:05 +0100843 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100844 owner);
845 if (r)
846 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400847
Christian Königa1e08d32016-01-26 11:40:46 +0100848 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
849 if (r)
850 goto error_free;
851
Christian Königfa3ab3c72016-03-18 21:00:35 +0100852 amdgpu_vm_update_ptes(adev, src, pages_addr, vm, ib, start,
853 last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854
Christian König9e5d53092016-01-31 12:20:55 +0100855 amdgpu_ring_pad_ib(ring, ib);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800856 WARN_ON(ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100857 r = amdgpu_job_submit(job, ring, &vm->entity,
858 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800859 if (r)
860 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861
Christian Königbf60efd2015-09-04 10:47:56 +0200862 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800863 if (fence) {
864 fence_put(*fence);
865 *fence = fence_get(f);
866 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800867 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400868 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800869
870error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100871 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800872 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873}
874
875/**
Christian Königa14faa62016-01-25 14:27:31 +0100876 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
877 *
878 * @adev: amdgpu_device pointer
Christian König8358dce2016-03-30 10:50:25 +0200879 * @gtt_flags: flags as they are used for GTT
880 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100881 * @vm: requested vm
882 * @mapping: mapped range and flags to use for the update
883 * @addr: addr to set the area to
Christian König8358dce2016-03-30 10:50:25 +0200884 * @flags: HW flags for the mapping
Christian Königa14faa62016-01-25 14:27:31 +0100885 * @fence: optional resulting fence
886 *
887 * Split the mapping into smaller chunks so that each update fits
888 * into a SDMA IB.
889 * Returns 0 for success, -EINVAL for failure.
890 */
891static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Christian Königa14faa62016-01-25 14:27:31 +0100892 uint32_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +0200893 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +0100894 struct amdgpu_vm *vm,
895 struct amdgpu_bo_va_mapping *mapping,
Christian Königfa3ab3c72016-03-18 21:00:35 +0100896 uint32_t flags, uint64_t addr,
897 struct fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +0100898{
899 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
900
Christian Königfa3ab3c72016-03-18 21:00:35 +0100901 uint64_t src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +0100902 int r;
903
904 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
905 * but in case of something, we filter the flags in first place
906 */
907 if (!(mapping->flags & AMDGPU_PTE_READABLE))
908 flags &= ~AMDGPU_PTE_READABLE;
909 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
910 flags &= ~AMDGPU_PTE_WRITEABLE;
911
912 trace_amdgpu_vm_bo_update(mapping);
913
Christian König8358dce2016-03-30 10:50:25 +0200914 if (pages_addr) {
Christian Königfa3ab3c72016-03-18 21:00:35 +0100915 if (flags == gtt_flags)
916 src = adev->gart.table_addr + (addr >> 12) * 8;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100917 addr = 0;
918 }
Christian Königa14faa62016-01-25 14:27:31 +0100919 addr += mapping->offset;
920
Christian König8358dce2016-03-30 10:50:25 +0200921 if (!pages_addr || src)
Christian Königfa3ab3c72016-03-18 21:00:35 +0100922 return amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100923 start, mapping->it.last,
924 flags, addr, fence);
925
926 while (start != mapping->it.last + 1) {
927 uint64_t last;
928
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500929 last = min((uint64_t)mapping->it.last, start + max_size - 1);
Christian Königfa3ab3c72016-03-18 21:00:35 +0100930 r = amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +0100931 start, last, flags, addr,
932 fence);
933 if (r)
934 return r;
935
936 start = last + 1;
Felix Kuehlingfb29b572016-03-03 19:13:20 -0500937 addr += max_size * AMDGPU_GPU_PAGE_SIZE;
Christian Königa14faa62016-01-25 14:27:31 +0100938 }
939
940 return 0;
941}
942
943/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400944 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
945 *
946 * @adev: amdgpu_device pointer
947 * @bo_va: requested BO and VM object
948 * @mem: ttm mem
949 *
950 * Fill in the page table entries for @bo_va.
951 * Returns 0 for success, -EINVAL for failure.
952 *
953 * Object have to be reserved and mutex must be locked!
954 */
955int amdgpu_vm_bo_update(struct amdgpu_device *adev,
956 struct amdgpu_bo_va *bo_va,
957 struct ttm_mem_reg *mem)
958{
959 struct amdgpu_vm *vm = bo_va->vm;
960 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +0200961 dma_addr_t *pages_addr = NULL;
Christian Königfa3ab3c72016-03-18 21:00:35 +0100962 uint32_t gtt_flags, flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963 uint64_t addr;
964 int r;
965
966 if (mem) {
Christian König8358dce2016-03-30 10:50:25 +0200967 struct ttm_dma_tt *ttm;
968
Christian Königb7d698d2015-09-07 12:32:09 +0200969 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +0100970 switch (mem->mem_type) {
971 case TTM_PL_TT:
Christian König8358dce2016-03-30 10:50:25 +0200972 ttm = container_of(bo_va->bo->tbo.ttm, struct
973 ttm_dma_tt, ttm);
974 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +0100975 break;
976
977 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +0100979 break;
980
981 default:
982 break;
983 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984 } else {
985 addr = 0;
986 }
987
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400988 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
Christian Königfa3ab3c72016-03-18 21:00:35 +0100989 gtt_flags = (adev == bo_va->bo->adev) ? flags : 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990
Christian König7fc11952015-07-30 11:53:42 +0200991 spin_lock(&vm->status_lock);
992 if (!list_empty(&bo_va->vm_status))
993 list_splice_init(&bo_va->valids, &bo_va->invalids);
994 spin_unlock(&vm->status_lock);
995
996 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König8358dce2016-03-30 10:50:25 +0200997 r = amdgpu_vm_bo_split_mapping(adev, gtt_flags, pages_addr, vm,
998 mapping, flags, addr,
999 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000 if (r)
1001 return r;
1002 }
1003
Christian Königd6c10f62015-09-28 12:00:23 +02001004 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1005 list_for_each_entry(mapping, &bo_va->valids, list)
1006 trace_amdgpu_vm_bo_mapping(mapping);
1007
1008 list_for_each_entry(mapping, &bo_va->invalids, list)
1009 trace_amdgpu_vm_bo_mapping(mapping);
1010 }
1011
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001012 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001013 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001014 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +02001015 if (!mem)
1016 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001017 spin_unlock(&vm->status_lock);
1018
1019 return 0;
1020}
1021
1022/**
1023 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1024 *
1025 * @adev: amdgpu_device pointer
1026 * @vm: requested vm
1027 *
1028 * Make sure all freed BOs are cleared in the PT.
1029 * Returns 0 for success.
1030 *
1031 * PTs have to be reserved and mutex must be locked!
1032 */
1033int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1034 struct amdgpu_vm *vm)
1035{
1036 struct amdgpu_bo_va_mapping *mapping;
1037 int r;
1038
1039 while (!list_empty(&vm->freed)) {
1040 mapping = list_first_entry(&vm->freed,
1041 struct amdgpu_bo_va_mapping, list);
1042 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001043
Christian König8358dce2016-03-30 10:50:25 +02001044 r = amdgpu_vm_bo_split_mapping(adev, 0, NULL, vm, mapping,
Christian Königfa3ab3c72016-03-18 21:00:35 +01001045 0, 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046 kfree(mapping);
1047 if (r)
1048 return r;
1049
1050 }
1051 return 0;
1052
1053}
1054
1055/**
1056 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1057 *
1058 * @adev: amdgpu_device pointer
1059 * @vm: requested vm
1060 *
1061 * Make sure all invalidated BOs are cleared in the PT.
1062 * Returns 0 for success.
1063 *
1064 * PTs have to be reserved and mutex must be locked!
1065 */
1066int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001067 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068{
monk.liucfe2c972015-05-26 15:01:54 +08001069 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001070 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001071
1072 spin_lock(&vm->status_lock);
1073 while (!list_empty(&vm->invalidated)) {
1074 bo_va = list_first_entry(&vm->invalidated,
1075 struct amdgpu_bo_va, vm_status);
1076 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001077
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001078 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
1079 if (r)
1080 return r;
1081
1082 spin_lock(&vm->status_lock);
1083 }
1084 spin_unlock(&vm->status_lock);
1085
monk.liucfe2c972015-05-26 15:01:54 +08001086 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001087 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001088
1089 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090}
1091
1092/**
1093 * amdgpu_vm_bo_add - add a bo to a specific vm
1094 *
1095 * @adev: amdgpu_device pointer
1096 * @vm: requested vm
1097 * @bo: amdgpu buffer object
1098 *
Christian König8843dbb2016-01-26 12:17:11 +01001099 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 * Add @bo to the list of bos associated with the vm
1101 * Returns newly added bo_va or NULL for failure
1102 *
1103 * Object has to be reserved!
1104 */
1105struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1106 struct amdgpu_vm *vm,
1107 struct amdgpu_bo *bo)
1108{
1109 struct amdgpu_bo_va *bo_va;
1110
1111 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1112 if (bo_va == NULL) {
1113 return NULL;
1114 }
1115 bo_va->vm = vm;
1116 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117 bo_va->ref_count = 1;
1118 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001119 INIT_LIST_HEAD(&bo_va->valids);
1120 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001121 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001122
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001123 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124
1125 return bo_va;
1126}
1127
1128/**
1129 * amdgpu_vm_bo_map - map bo inside a vm
1130 *
1131 * @adev: amdgpu_device pointer
1132 * @bo_va: bo_va to store the address
1133 * @saddr: where to map the BO
1134 * @offset: requested offset in the BO
1135 * @flags: attributes of pages (read/write/valid/etc.)
1136 *
1137 * Add a mapping of the BO at the specefied addr into the VM.
1138 * Returns 0 for success, error for failure.
1139 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001140 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001141 */
1142int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1143 struct amdgpu_bo_va *bo_va,
1144 uint64_t saddr, uint64_t offset,
1145 uint64_t size, uint32_t flags)
1146{
1147 struct amdgpu_bo_va_mapping *mapping;
1148 struct amdgpu_vm *vm = bo_va->vm;
1149 struct interval_tree_node *it;
1150 unsigned last_pfn, pt_idx;
1151 uint64_t eaddr;
1152 int r;
1153
Christian König0be52de2015-05-18 14:37:27 +02001154 /* validate the parameters */
1155 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001156 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001157 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001158
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001160 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001161 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001163
1164 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001165 if (last_pfn >= adev->vm_manager.max_pfn) {
1166 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001167 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168 return -EINVAL;
1169 }
1170
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171 saddr /= AMDGPU_GPU_PAGE_SIZE;
1172 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1173
Felix Kuehling005ae952015-11-23 17:43:48 -05001174 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001175 if (it) {
1176 struct amdgpu_bo_va_mapping *tmp;
1177 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1178 /* bo and tmp overlap, invalid addr */
1179 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1180 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1181 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001182 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001183 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 }
1185
1186 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1187 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001189 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001190 }
1191
1192 INIT_LIST_HEAD(&mapping->list);
1193 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001194 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 mapping->offset = offset;
1196 mapping->flags = flags;
1197
Christian König7fc11952015-07-30 11:53:42 +02001198 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 interval_tree_insert(&mapping->it, &vm->va);
1200
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201 /* Make sure the page tables are allocated */
1202 saddr >>= amdgpu_vm_block_size;
1203 eaddr >>= amdgpu_vm_block_size;
1204
1205 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1206
1207 if (eaddr > vm->max_pde_used)
1208 vm->max_pde_used = eaddr;
1209
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210 /* walk over the address space and allocate the page tables */
1211 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001212 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001213 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214 struct amdgpu_bo *pt;
1215
Christian Königee1782c2015-12-11 21:01:23 +01001216 entry = &vm->page_tables[pt_idx].entry;
1217 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001218 continue;
1219
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001220 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1221 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001222 AMDGPU_GEM_DOMAIN_VRAM,
1223 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001224 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001225 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001227
Christian König82b9c552015-11-27 16:49:00 +01001228 /* Keep a reference to the page table to avoid freeing
1229 * them up in the wrong order.
1230 */
1231 pt->parent = amdgpu_bo_ref(vm->page_directory);
1232
Christian König2bd9ccf2016-02-01 12:53:58 +01001233 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001234 if (r) {
1235 amdgpu_bo_unref(&pt);
1236 goto error_free;
1237 }
1238
Christian Königee1782c2015-12-11 21:01:23 +01001239 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001240 entry->priority = 0;
1241 entry->tv.bo = &entry->robj->tbo;
1242 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +01001243 entry->user_pages = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001245 }
1246
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247 return 0;
1248
1249error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250 list_del(&mapping->list);
1251 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001252 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001253 kfree(mapping);
1254
Chunming Zhouf48b2652015-10-16 14:06:19 +08001255error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256 return r;
1257}
1258
1259/**
1260 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1261 *
1262 * @adev: amdgpu_device pointer
1263 * @bo_va: bo_va to remove the address from
1264 * @saddr: where to the BO is mapped
1265 *
1266 * Remove a mapping of the BO at the specefied addr from the VM.
1267 * Returns 0 for success, error for failure.
1268 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001269 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270 */
1271int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1272 struct amdgpu_bo_va *bo_va,
1273 uint64_t saddr)
1274{
1275 struct amdgpu_bo_va_mapping *mapping;
1276 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001277 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278
Christian König6c7fc502015-06-05 20:56:17 +02001279 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001280
Christian König7fc11952015-07-30 11:53:42 +02001281 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 if (mapping->it.start == saddr)
1283 break;
1284 }
1285
Christian König7fc11952015-07-30 11:53:42 +02001286 if (&mapping->list == &bo_va->valids) {
1287 valid = false;
1288
1289 list_for_each_entry(mapping, &bo_va->invalids, list) {
1290 if (mapping->it.start == saddr)
1291 break;
1292 }
1293
Christian König32b41ac2016-03-08 18:03:27 +01001294 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001295 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 }
Christian König32b41ac2016-03-08 18:03:27 +01001297
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298 list_del(&mapping->list);
1299 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001300 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301
Christian Könige17841b2016-03-08 17:52:01 +01001302 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001304 else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001305 kfree(mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306
1307 return 0;
1308}
1309
1310/**
1311 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1312 *
1313 * @adev: amdgpu_device pointer
1314 * @bo_va: requested bo_va
1315 *
Christian König8843dbb2016-01-26 12:17:11 +01001316 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001317 *
1318 * Object have to be reserved!
1319 */
1320void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1321 struct amdgpu_bo_va *bo_va)
1322{
1323 struct amdgpu_bo_va_mapping *mapping, *next;
1324 struct amdgpu_vm *vm = bo_va->vm;
1325
1326 list_del(&bo_va->bo_list);
1327
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001328 spin_lock(&vm->status_lock);
1329 list_del(&bo_va->vm_status);
1330 spin_unlock(&vm->status_lock);
1331
Christian König7fc11952015-07-30 11:53:42 +02001332 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001333 list_del(&mapping->list);
1334 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001335 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001336 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001337 }
Christian König7fc11952015-07-30 11:53:42 +02001338 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1339 list_del(&mapping->list);
1340 interval_tree_remove(&mapping->it, &vm->va);
1341 kfree(mapping);
1342 }
Christian König32b41ac2016-03-08 18:03:27 +01001343
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001344 fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001345 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001346}
1347
1348/**
1349 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1350 *
1351 * @adev: amdgpu_device pointer
1352 * @vm: requested vm
1353 * @bo: amdgpu buffer object
1354 *
Christian König8843dbb2016-01-26 12:17:11 +01001355 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356 */
1357void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1358 struct amdgpu_bo *bo)
1359{
1360 struct amdgpu_bo_va *bo_va;
1361
1362 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001363 spin_lock(&bo_va->vm->status_lock);
1364 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001365 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001366 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001367 }
1368}
1369
1370/**
1371 * amdgpu_vm_init - initialize a vm instance
1372 *
1373 * @adev: amdgpu_device pointer
1374 * @vm: requested vm
1375 *
Christian König8843dbb2016-01-26 12:17:11 +01001376 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 */
1378int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1379{
1380 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1381 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001382 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001383 unsigned ring_instance;
1384 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001385 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 int i, r;
1387
Christian Königbcb1ba32016-03-08 15:40:11 +01001388 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1389 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001390 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001391 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392 spin_lock_init(&vm->status_lock);
1393 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001394 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001395 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001396
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397 pd_size = amdgpu_vm_directory_size(adev);
1398 pd_entries = amdgpu_vm_num_pdes(adev);
1399
1400 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001401 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001402 if (vm->page_tables == NULL) {
1403 DRM_ERROR("Cannot allocate memory for page table array\n");
1404 return -ENOMEM;
1405 }
1406
Christian König2bd9ccf2016-02-01 12:53:58 +01001407 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001408
1409 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1410 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1411 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001412 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1413 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1414 rq, amdgpu_sched_jobs);
1415 if (r)
1416 return r;
1417
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001418 vm->page_directory_fence = NULL;
1419
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001421 AMDGPU_GEM_DOMAIN_VRAM,
1422 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001423 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001424 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001425 goto error_free_sched_entity;
1426
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001427 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001428 if (r)
1429 goto error_free_page_directory;
1430
1431 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001432 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001433 if (r)
1434 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001435
1436 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001437
1438error_free_page_directory:
1439 amdgpu_bo_unref(&vm->page_directory);
1440 vm->page_directory = NULL;
1441
1442error_free_sched_entity:
1443 amd_sched_entity_fini(&ring->sched, &vm->entity);
1444
1445 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001446}
1447
1448/**
1449 * amdgpu_vm_fini - tear down a vm instance
1450 *
1451 * @adev: amdgpu_device pointer
1452 * @vm: requested vm
1453 *
Christian König8843dbb2016-01-26 12:17:11 +01001454 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001455 * Unbind the VM and remove all bos from the vm bo list
1456 */
1457void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1458{
1459 struct amdgpu_bo_va_mapping *mapping, *tmp;
1460 int i;
1461
Christian König2d55e452016-02-08 17:37:38 +01001462 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001463
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001464 if (!RB_EMPTY_ROOT(&vm->va)) {
1465 dev_err(adev->dev, "still active bo inside vm\n");
1466 }
1467 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1468 list_del(&mapping->list);
1469 interval_tree_remove(&mapping->it, &vm->va);
1470 kfree(mapping);
1471 }
1472 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1473 list_del(&mapping->list);
1474 kfree(mapping);
1475 }
1476
1477 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001478 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001479 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480
1481 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001482 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001483}
Christian Königea89f8c2015-11-15 20:52:06 +01001484
1485/**
Christian Königa9a78b32016-01-21 10:19:11 +01001486 * amdgpu_vm_manager_init - init the VM manager
1487 *
1488 * @adev: amdgpu_device pointer
1489 *
1490 * Initialize the VM manager structures
1491 */
1492void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1493{
1494 unsigned i;
1495
1496 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1497
1498 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001499 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1500 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01001501 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01001502 list_add_tail(&adev->vm_manager.ids[i].list,
1503 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001504 }
Christian König2d55e452016-02-08 17:37:38 +01001505
1506 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02001507 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001508}
1509
1510/**
Christian Königea89f8c2015-11-15 20:52:06 +01001511 * amdgpu_vm_manager_fini - cleanup VM manager
1512 *
1513 * @adev: amdgpu_device pointer
1514 *
1515 * Cleanup the VM manager and free resources.
1516 */
1517void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1518{
1519 unsigned i;
1520
Christian Königbcb1ba32016-03-08 15:40:11 +01001521 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
1522 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
1523
Christian König832a9022016-02-15 12:33:02 +01001524 fence_put(adev->vm_manager.ids[i].first);
1525 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Christian Königbcb1ba32016-03-08 15:40:11 +01001526 fence_put(id->flushed_updates);
1527 }
Christian Königea89f8c2015-11-15 20:52:06 +01001528}