blob: cc28bdc02078a1b39700616e2455307cf8608817 [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
53/**
54 * amdgpu_vm_num_pde - return the number of page directory entries
55 *
56 * @adev: amdgpu_device pointer
57 *
58 * Calculate the number of page directory entries (cayman+).
59 */
60static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
61{
62 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
63}
64
65/**
66 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
67 *
68 * @adev: amdgpu_device pointer
69 *
70 * Calculate the size of the page directory in bytes (cayman+).
71 */
72static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
73{
74 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
75}
76
77/**
Christian König56467eb2015-12-11 15:16:32 +010078 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079 *
80 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +010081 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +010082 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 *
84 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +010085 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 */
Christian König56467eb2015-12-11 15:16:32 +010087void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
88 struct list_head *validated,
89 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090{
Christian König56467eb2015-12-11 15:16:32 +010091 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +010092 entry->priority = 0;
93 entry->tv.bo = &vm->page_directory->tbo;
94 entry->tv.shared = true;
95 list_add(&entry->tv.head, validated);
96}
Alex Deucherd38ceaf2015-04-20 16:55:21 -040097
Christian König56467eb2015-12-11 15:16:32 +010098/**
Christian Königee1782c2015-12-11 21:01:23 +010099 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100100 *
101 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100102 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103 *
Christian Königee1782c2015-12-11 21:01:23 +0100104 * Add the page directory to the BO duplicates list
105 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 */
Christian Königee1782c2015-12-11 21:01:23 +0100107void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108{
Christian Königee1782c2015-12-11 21:01:23 +0100109 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110
111 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100112 for (i = 0; i <= vm->max_pde_used; ++i) {
113 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114
Christian Königee1782c2015-12-11 21:01:23 +0100115 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 continue;
117
Christian Königee1782c2015-12-11 21:01:23 +0100118 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 }
Christian Königeceb8a12016-01-11 15:35:21 +0100120
121}
122
123/**
124 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
125 *
126 * @adev: amdgpu device instance
127 * @vm: vm providing the BOs
128 *
129 * Move the PT BOs to the tail of the LRU.
130 */
131void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
132 struct amdgpu_vm *vm)
133{
134 struct ttm_bo_global *glob = adev->mman.bdev.glob;
135 unsigned i;
136
137 spin_lock(&glob->lru_lock);
138 for (i = 0; i <= vm->max_pde_used; ++i) {
139 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
140
141 if (!entry->robj)
142 continue;
143
144 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
145 }
146 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147}
148
149/**
150 * amdgpu_vm_grab_id - allocate the next free VMID
151 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200153 * @ring: ring we want to submit job to
154 * @sync: sync object where we add dependencies
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155 *
Christian König7f8a5292015-07-20 16:09:40 +0200156 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157 *
Christian König7f8a5292015-07-20 16:09:40 +0200158 * Global mutex must be locked!
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 */
Christian König7f8a5292015-07-20 16:09:40 +0200160int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
161 struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162{
Christian Königd5283292015-10-22 11:55:58 +0200163 struct fence *best[AMDGPU_MAX_RINGS] = {};
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
165 struct amdgpu_device *adev = ring->adev;
166
167 unsigned choices[2] = {};
168 unsigned i;
169
170 /* check if the id is still valid */
Christian König1c16c0a2015-11-14 21:31:40 +0100171 if (vm_id->id) {
172 unsigned id = vm_id->id;
173 long owner;
174
175 owner = atomic_long_read(&adev->vm_manager.ids[id].owner);
176 if (owner == (long)vm) {
177 trace_amdgpu_vm_grab_id(vm_id->id, ring->idx);
178 return 0;
179 }
Christian König39ff8442015-09-28 12:01:20 +0200180 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181
182 /* we definately need to flush */
183 vm_id->pd_gpu_addr = ~0ll;
184
185 /* skip over VMID 0, since it is the system VM */
186 for (i = 1; i < adev->vm_manager.nvm; ++i) {
Christian König1c16c0a2015-11-14 21:31:40 +0100187 struct fence *fence = adev->vm_manager.ids[i].active;
Christian Königd5283292015-10-22 11:55:58 +0200188 struct amdgpu_ring *fring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400189
190 if (fence == NULL) {
191 /* found a free one */
192 vm_id->id = i;
193 trace_amdgpu_vm_grab_id(i, ring->idx);
Christian König7f8a5292015-07-20 16:09:40 +0200194 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195 }
196
Christian Königd5283292015-10-22 11:55:58 +0200197 fring = amdgpu_ring_from_fence(fence);
198 if (best[fring->idx] == NULL ||
199 fence_is_later(best[fring->idx], fence)) {
200 best[fring->idx] = fence;
201 choices[fring == ring ? 0 : 1] = i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202 }
203 }
204
205 for (i = 0; i < 2; ++i) {
206 if (choices[i]) {
Christian Königd5283292015-10-22 11:55:58 +0200207 struct fence *fence;
Christian König7f8a5292015-07-20 16:09:40 +0200208
Christian König1c16c0a2015-11-14 21:31:40 +0100209 fence = adev->vm_manager.ids[choices[i]].active;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210 vm_id->id = choices[i];
Christian König7f8a5292015-07-20 16:09:40 +0200211
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 trace_amdgpu_vm_grab_id(choices[i], ring->idx);
Christian Königd5283292015-10-22 11:55:58 +0200213 return amdgpu_sync_fence(ring->adev, sync, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214 }
215 }
216
217 /* should never happen */
218 BUG();
Christian König7f8a5292015-07-20 16:09:40 +0200219 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220}
221
222/**
223 * amdgpu_vm_flush - hardware flush the vm
224 *
225 * @ring: ring to use for flush
226 * @vm: vm we want to flush
227 * @updates: last vm update that we waited for
228 *
229 * Flush the vm (cayman+).
230 *
231 * Global and local mutex must be locked!
232 */
233void amdgpu_vm_flush(struct amdgpu_ring *ring,
234 struct amdgpu_vm *vm,
Chunming Zhou3c623382015-08-20 18:33:59 +0800235 struct fence *updates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236{
237 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
238 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
Chunming Zhou3c623382015-08-20 18:33:59 +0800239 struct fence *flushed_updates = vm_id->flushed_updates;
Christian Königb56c2282015-10-29 17:01:19 +0100240 bool is_later;
Chunming Zhou3c623382015-08-20 18:33:59 +0800241
Christian Königb56c2282015-10-29 17:01:19 +0100242 if (!flushed_updates)
243 is_later = true;
244 else if (!updates)
245 is_later = false;
246 else
247 is_later = fence_is_later(updates, flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400248
Christian Königb56c2282015-10-29 17:01:19 +0100249 if (pd_addr != vm_id->pd_gpu_addr || is_later) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
Christian Königb56c2282015-10-29 17:01:19 +0100251 if (is_later) {
Chunming Zhou3c623382015-08-20 18:33:59 +0800252 vm_id->flushed_updates = fence_get(updates);
253 fence_put(flushed_updates);
254 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255 vm_id->pd_gpu_addr = pd_addr;
256 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
257 }
258}
259
260/**
261 * amdgpu_vm_fence - remember fence for vm
262 *
263 * @adev: amdgpu_device pointer
264 * @vm: vm we want to fence
265 * @fence: fence to remember
266 *
267 * Fence the vm (cayman+).
268 * Set the fence used to protect page table and id.
269 *
270 * Global and local mutex must be locked!
271 */
272void amdgpu_vm_fence(struct amdgpu_device *adev,
273 struct amdgpu_vm *vm,
Christian König16ae42f2015-11-03 14:53:28 +0100274 struct fence *fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400275{
Christian König16ae42f2015-11-03 14:53:28 +0100276 struct amdgpu_ring *ring = amdgpu_ring_from_fence(fence);
277 unsigned vm_id = vm->ids[ring->idx].id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278
Christian König1c16c0a2015-11-14 21:31:40 +0100279 fence_put(adev->vm_manager.ids[vm_id].active);
280 adev->vm_manager.ids[vm_id].active = fence_get(fence);
281 atomic_long_set(&adev->vm_manager.ids[vm_id].owner, (long)vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282}
283
284/**
285 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
286 *
287 * @vm: requested vm
288 * @bo: requested buffer object
289 *
290 * Find @bo inside the requested vm (cayman+).
291 * Search inside the @bos vm list for the requested vm
292 * Returns the found bo_va or NULL if none is found
293 *
294 * Object has to be reserved!
295 */
296struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
297 struct amdgpu_bo *bo)
298{
299 struct amdgpu_bo_va *bo_va;
300
301 list_for_each_entry(bo_va, &bo->va, bo_list) {
302 if (bo_va->vm == vm) {
303 return bo_va;
304 }
305 }
306 return NULL;
307}
308
309/**
310 * amdgpu_vm_update_pages - helper to call the right asic function
311 *
312 * @adev: amdgpu_device pointer
313 * @ib: indirect buffer to fill with commands
314 * @pe: addr of the page entry
315 * @addr: dst addr to write into pe
316 * @count: number of page entries to update
317 * @incr: increase next addr by incr bytes
318 * @flags: hw access flags
319 * @gtt_flags: GTT hw access flags
320 *
321 * Traces the parameters and calls the right asic functions
322 * to setup the page table using the DMA.
323 */
324static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
325 struct amdgpu_ib *ib,
326 uint64_t pe, uint64_t addr,
327 unsigned count, uint32_t incr,
328 uint32_t flags, uint32_t gtt_flags)
329{
330 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
331
332 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
333 uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
334 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
335
336 } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
337 amdgpu_vm_write_pte(adev, ib, pe, addr,
338 count, incr, flags);
339
340 } else {
341 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
342 count, incr, flags);
343 }
344}
345
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800346int amdgpu_vm_free_job(struct amdgpu_job *job)
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800347{
348 int i;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800349 for (i = 0; i < job->num_ibs; i++)
350 amdgpu_ib_free(job->adev, &job->ibs[i]);
351 kfree(job->ibs);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800352 return 0;
353}
354
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400355/**
356 * amdgpu_vm_clear_bo - initially clear the page dir/table
357 *
358 * @adev: amdgpu_device pointer
359 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800360 *
361 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400362 */
363static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
364 struct amdgpu_bo *bo)
365{
366 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800367 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800368 struct amdgpu_ib *ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400369 unsigned entries;
370 uint64_t addr;
371 int r;
372
monk.liuca952612015-05-25 14:44:05 +0800373 r = reservation_object_reserve_shared(bo->tbo.resv);
374 if (r)
375 return r;
376
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
378 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800379 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380
381 addr = amdgpu_bo_gpu_offset(bo);
382 entries = amdgpu_bo_size(bo) / 8;
383
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800384 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
385 if (!ib)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800386 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400387
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800388 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389 if (r)
390 goto error_free;
391
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800392 ib->length_dw = 0;
393
394 amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0);
395 amdgpu_vm_pad_ib(adev, ib);
396 WARN_ON(ib->length_dw > 64);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800397 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
398 &amdgpu_vm_free_job,
399 AMDGPU_FENCE_OWNER_VM,
400 &fence);
401 if (!r)
402 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800403 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800404 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800405
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400406error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800407 amdgpu_ib_free(adev, ib);
408 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800410error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400411 return r;
412}
413
414/**
415 * amdgpu_vm_map_gart - get the physical address of a gart page
416 *
417 * @adev: amdgpu_device pointer
418 * @addr: the unmapped addr
419 *
420 * Look up the physical address of the page that the pte resolves
421 * to (cayman+).
422 * Returns the physical address of the page.
423 */
424uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
425{
426 uint64_t result;
427
428 /* page table offset */
429 result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
430
431 /* in case cpu page size != gpu page size*/
432 result |= addr & (~PAGE_MASK);
433
434 return result;
435}
436
437/**
438 * amdgpu_vm_update_pdes - make sure that page directory is valid
439 *
440 * @adev: amdgpu_device pointer
441 * @vm: requested vm
442 * @start: start of GPU address range
443 * @end: end of GPU address range
444 *
445 * Allocates new page tables if necessary
446 * and updates the page directory (cayman+).
447 * Returns 0 for success, error for failure.
448 *
449 * Global and local mutex must be locked!
450 */
451int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
452 struct amdgpu_vm *vm)
453{
454 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
455 struct amdgpu_bo *pd = vm->page_directory;
456 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
457 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
458 uint64_t last_pde = ~0, last_pt = ~0;
459 unsigned count = 0, pt_idx, ndw;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800460 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800461 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800462
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 int r;
464
465 /* padding, etc. */
466 ndw = 64;
467
468 /* assume the worst case */
469 ndw += vm->max_pde_used * 6;
470
471 /* update too big for an IB */
472 if (ndw > 0xfffff)
473 return -ENOMEM;
474
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800475 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
476 if (!ib)
477 return -ENOMEM;
478
479 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
Sudip Mukherjee7a574552015-10-08 19:28:01 +0530480 if (r) {
481 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400482 return r;
Sudip Mukherjee7a574552015-10-08 19:28:01 +0530483 }
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800484 ib->length_dw = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400485
486 /* walk over the address space and update the page directory */
487 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100488 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400489 uint64_t pde, pt;
490
491 if (bo == NULL)
492 continue;
493
494 pt = amdgpu_bo_gpu_offset(bo);
495 if (vm->page_tables[pt_idx].addr == pt)
496 continue;
497 vm->page_tables[pt_idx].addr = pt;
498
499 pde = pd_addr + pt_idx * 8;
500 if (((last_pde + 8 * count) != pde) ||
501 ((last_pt + incr * count) != pt)) {
502
503 if (count) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800504 amdgpu_vm_update_pages(adev, ib, last_pde,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 last_pt, count, incr,
506 AMDGPU_PTE_VALID, 0);
507 }
508
509 count = 1;
510 last_pde = pde;
511 last_pt = pt;
512 } else {
513 ++count;
514 }
515 }
516
517 if (count)
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800518 amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400519 incr, AMDGPU_PTE_VALID, 0);
520
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800521 if (ib->length_dw != 0) {
522 amdgpu_vm_pad_ib(adev, ib);
523 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
524 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800525 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
526 &amdgpu_vm_free_job,
527 AMDGPU_FENCE_OWNER_VM,
528 &fence);
529 if (r)
530 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200531
Chunming Zhou4af9f072015-08-03 12:57:31 +0800532 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200533 fence_put(vm->page_directory_fence);
534 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800535 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 }
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800537
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800538 if (ib->length_dw == 0) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800539 amdgpu_ib_free(adev, ib);
540 kfree(ib);
541 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542
543 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800544
545error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800546 amdgpu_ib_free(adev, ib);
547 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800548 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400549}
550
551/**
552 * amdgpu_vm_frag_ptes - add fragment information to PTEs
553 *
554 * @adev: amdgpu_device pointer
555 * @ib: IB for the update
556 * @pe_start: first PTE to handle
557 * @pe_end: last PTE to handle
558 * @addr: addr those PTEs should point to
559 * @flags: hw mapping flags
560 * @gtt_flags: GTT hw mapping flags
561 *
562 * Global and local mutex must be locked!
563 */
564static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
565 struct amdgpu_ib *ib,
566 uint64_t pe_start, uint64_t pe_end,
567 uint64_t addr, uint32_t flags,
568 uint32_t gtt_flags)
569{
570 /**
571 * The MC L1 TLB supports variable sized pages, based on a fragment
572 * field in the PTE. When this field is set to a non-zero value, page
573 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
574 * flags are considered valid for all PTEs within the fragment range
575 * and corresponding mappings are assumed to be physically contiguous.
576 *
577 * The L1 TLB can store a single PTE for the whole fragment,
578 * significantly increasing the space available for translation
579 * caching. This leads to large improvements in throughput when the
580 * TLB is under pressure.
581 *
582 * The L2 TLB distributes small and large fragments into two
583 * asymmetric partitions. The large fragment cache is significantly
584 * larger. Thus, we try to use large fragments wherever possible.
585 * Userspace can support this by aligning virtual base address and
586 * allocation size to the fragment size.
587 */
588
589 /* SI and newer are optimized for 64KB */
590 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
591 uint64_t frag_align = 0x80;
592
593 uint64_t frag_start = ALIGN(pe_start, frag_align);
594 uint64_t frag_end = pe_end & ~(frag_align - 1);
595
596 unsigned count;
597
598 /* system pages are non continuously */
599 if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
600 (frag_start >= frag_end)) {
601
602 count = (pe_end - pe_start) / 8;
603 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
604 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
605 return;
606 }
607
608 /* handle the 4K area at the beginning */
609 if (pe_start != frag_start) {
610 count = (frag_start - pe_start) / 8;
611 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
612 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
613 addr += AMDGPU_GPU_PAGE_SIZE * count;
614 }
615
616 /* handle the area in the middle */
617 count = (frag_end - frag_start) / 8;
618 amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
619 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
620 gtt_flags);
621
622 /* handle the 4K area at the end */
623 if (frag_end != pe_end) {
624 addr += AMDGPU_GPU_PAGE_SIZE * count;
625 count = (pe_end - frag_end) / 8;
626 amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
627 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
628 }
629}
630
631/**
632 * amdgpu_vm_update_ptes - make sure that page tables are valid
633 *
634 * @adev: amdgpu_device pointer
635 * @vm: requested vm
636 * @start: start of GPU address range
637 * @end: end of GPU address range
638 * @dst: destination address to map to
639 * @flags: mapping flags
640 *
641 * Update the page tables in the range @start - @end (cayman+).
642 *
643 * Global and local mutex must be locked!
644 */
645static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
646 struct amdgpu_vm *vm,
647 struct amdgpu_ib *ib,
648 uint64_t start, uint64_t end,
649 uint64_t dst, uint32_t flags,
650 uint32_t gtt_flags)
651{
652 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
653 uint64_t last_pte = ~0, last_dst = ~0;
Christian Königa60c4232015-09-01 15:33:25 +0200654 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655 unsigned count = 0;
656 uint64_t addr;
657
Christian Königa60c4232015-09-01 15:33:25 +0200658 /* sync to everything on unmapping */
659 if (!(flags & AMDGPU_PTE_VALID))
660 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
661
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662 /* walk over the address space and update the page tables */
663 for (addr = start; addr < end; ) {
664 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
Christian Königee1782c2015-12-11 21:01:23 +0100665 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666 unsigned nptes;
667 uint64_t pte;
668 int r;
669
Christian Königa60c4232015-09-01 15:33:25 +0200670 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 r = reservation_object_reserve_shared(pt->tbo.resv);
672 if (r)
673 return r;
674
675 if ((addr & ~mask) == (end & ~mask))
676 nptes = end - addr;
677 else
678 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
679
680 pte = amdgpu_bo_gpu_offset(pt);
681 pte += (addr & mask) * 8;
682
683 if ((last_pte + 8 * count) != pte) {
684
685 if (count) {
686 amdgpu_vm_frag_ptes(adev, ib, last_pte,
687 last_pte + 8 * count,
688 last_dst, flags,
689 gtt_flags);
690 }
691
692 count = nptes;
693 last_pte = pte;
694 last_dst = dst;
695 } else {
696 count += nptes;
697 }
698
699 addr += nptes;
700 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
701 }
702
703 if (count) {
704 amdgpu_vm_frag_ptes(adev, ib, last_pte,
705 last_pte + 8 * count,
706 last_dst, flags, gtt_flags);
707 }
708
709 return 0;
710}
711
712/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
714 *
715 * @adev: amdgpu_device pointer
716 * @vm: requested vm
717 * @mapping: mapped range and flags to use for the update
718 * @addr: addr to set the area to
719 * @gtt_flags: flags as they are used for GTT
720 * @fence: optional resulting fence
721 *
722 * Fill in the page table entries for @mapping.
723 * Returns 0 for success, -EINVAL for failure.
724 *
725 * Object have to be reserved and mutex must be locked!
726 */
727static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
728 struct amdgpu_vm *vm,
729 struct amdgpu_bo_va_mapping *mapping,
730 uint64_t addr, uint32_t gtt_flags,
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800731 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732{
733 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
734 unsigned nptes, ncmds, ndw;
735 uint32_t flags = gtt_flags;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800736 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800737 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738 int r;
739
740 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
741 * but in case of something, we filter the flags in first place
742 */
743 if (!(mapping->flags & AMDGPU_PTE_READABLE))
744 flags &= ~AMDGPU_PTE_READABLE;
745 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
746 flags &= ~AMDGPU_PTE_WRITEABLE;
747
748 trace_amdgpu_vm_bo_update(mapping);
749
750 nptes = mapping->it.last - mapping->it.start + 1;
751
752 /*
753 * reserve space for one command every (1 << BLOCK_SIZE)
754 * entries or 2k dwords (whatever is smaller)
755 */
756 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
757
758 /* padding, etc. */
759 ndw = 64;
760
761 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
762 /* only copy commands needed */
763 ndw += ncmds * 7;
764
765 } else if (flags & AMDGPU_PTE_SYSTEM) {
766 /* header for write data commands */
767 ndw += ncmds * 4;
768
769 /* body of write data command */
770 ndw += nptes * 2;
771
772 } else {
773 /* set page commands needed */
774 ndw += ncmds * 10;
775
776 /* two extra commands for begin/end of fragment */
777 ndw += 2 * 10;
778 }
779
780 /* update too big for an IB */
781 if (ndw > 0xfffff)
782 return -ENOMEM;
783
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800784 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
785 if (!ib)
786 return -ENOMEM;
787
788 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
789 if (r) {
790 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400791 return r;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800792 }
793
794 ib->length_dw = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400795
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800796 r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797 mapping->it.last + 1, addr + mapping->offset,
798 flags, gtt_flags);
799
800 if (r) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800801 amdgpu_ib_free(adev, ib);
802 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400803 return r;
804 }
805
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800806 amdgpu_vm_pad_ib(adev, ib);
807 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800808 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
809 &amdgpu_vm_free_job,
810 AMDGPU_FENCE_OWNER_VM,
811 &f);
812 if (r)
813 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814
Christian Königbf60efd2015-09-04 10:47:56 +0200815 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800816 if (fence) {
817 fence_put(*fence);
818 *fence = fence_get(f);
819 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800820 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400821 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800822
823error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800824 amdgpu_ib_free(adev, ib);
825 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800826 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400827}
828
829/**
830 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
831 *
832 * @adev: amdgpu_device pointer
833 * @bo_va: requested BO and VM object
834 * @mem: ttm mem
835 *
836 * Fill in the page table entries for @bo_va.
837 * Returns 0 for success, -EINVAL for failure.
838 *
839 * Object have to be reserved and mutex must be locked!
840 */
841int amdgpu_vm_bo_update(struct amdgpu_device *adev,
842 struct amdgpu_bo_va *bo_va,
843 struct ttm_mem_reg *mem)
844{
845 struct amdgpu_vm *vm = bo_va->vm;
846 struct amdgpu_bo_va_mapping *mapping;
847 uint32_t flags;
848 uint64_t addr;
849 int r;
850
851 if (mem) {
Christian Königb7d698d2015-09-07 12:32:09 +0200852 addr = (u64)mem->start << PAGE_SHIFT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400853 if (mem->mem_type != TTM_PL_TT)
854 addr += adev->vm_manager.vram_base_offset;
855 } else {
856 addr = 0;
857 }
858
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400859 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
860
Christian König7fc11952015-07-30 11:53:42 +0200861 spin_lock(&vm->status_lock);
862 if (!list_empty(&bo_va->vm_status))
863 list_splice_init(&bo_va->valids, &bo_va->invalids);
864 spin_unlock(&vm->status_lock);
865
866 list_for_each_entry(mapping, &bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400867 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
868 flags, &bo_va->last_pt_update);
869 if (r)
870 return r;
871 }
872
Christian Königd6c10f62015-09-28 12:00:23 +0200873 if (trace_amdgpu_vm_bo_mapping_enabled()) {
874 list_for_each_entry(mapping, &bo_va->valids, list)
875 trace_amdgpu_vm_bo_mapping(mapping);
876
877 list_for_each_entry(mapping, &bo_va->invalids, list)
878 trace_amdgpu_vm_bo_mapping(mapping);
879 }
880
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400881 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +0800882 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400883 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200884 if (!mem)
885 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400886 spin_unlock(&vm->status_lock);
887
888 return 0;
889}
890
891/**
892 * amdgpu_vm_clear_freed - clear freed BOs in the PT
893 *
894 * @adev: amdgpu_device pointer
895 * @vm: requested vm
896 *
897 * Make sure all freed BOs are cleared in the PT.
898 * Returns 0 for success.
899 *
900 * PTs have to be reserved and mutex must be locked!
901 */
902int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
903 struct amdgpu_vm *vm)
904{
905 struct amdgpu_bo_va_mapping *mapping;
906 int r;
907
jimqu81d75a32015-12-04 17:17:00 +0800908 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400909 while (!list_empty(&vm->freed)) {
910 mapping = list_first_entry(&vm->freed,
911 struct amdgpu_bo_va_mapping, list);
912 list_del(&mapping->list);
jimqu81d75a32015-12-04 17:17:00 +0800913 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400914 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
915 kfree(mapping);
916 if (r)
917 return r;
918
jimqu81d75a32015-12-04 17:17:00 +0800919 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400920 }
jimqu81d75a32015-12-04 17:17:00 +0800921 spin_unlock(&vm->freed_lock);
922
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400923 return 0;
924
925}
926
927/**
928 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
929 *
930 * @adev: amdgpu_device pointer
931 * @vm: requested vm
932 *
933 * Make sure all invalidated BOs are cleared in the PT.
934 * Returns 0 for success.
935 *
936 * PTs have to be reserved and mutex must be locked!
937 */
938int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800939 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940{
monk.liucfe2c972015-05-26 15:01:54 +0800941 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200942 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943
944 spin_lock(&vm->status_lock);
945 while (!list_empty(&vm->invalidated)) {
946 bo_va = list_first_entry(&vm->invalidated,
947 struct amdgpu_bo_va, vm_status);
948 spin_unlock(&vm->status_lock);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800949 mutex_lock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800951 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952 if (r)
953 return r;
954
955 spin_lock(&vm->status_lock);
956 }
957 spin_unlock(&vm->status_lock);
958
monk.liucfe2c972015-05-26 15:01:54 +0800959 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800960 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200961
962 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963}
964
965/**
966 * amdgpu_vm_bo_add - add a bo to a specific vm
967 *
968 * @adev: amdgpu_device pointer
969 * @vm: requested vm
970 * @bo: amdgpu buffer object
971 *
972 * Add @bo into the requested vm (cayman+).
973 * Add @bo to the list of bos associated with the vm
974 * Returns newly added bo_va or NULL for failure
975 *
976 * Object has to be reserved!
977 */
978struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
979 struct amdgpu_vm *vm,
980 struct amdgpu_bo *bo)
981{
982 struct amdgpu_bo_va *bo_va;
983
984 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
985 if (bo_va == NULL) {
986 return NULL;
987 }
988 bo_va->vm = vm;
989 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990 bo_va->ref_count = 1;
991 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +0200992 INIT_LIST_HEAD(&bo_va->valids);
993 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400994 INIT_LIST_HEAD(&bo_va->vm_status);
Chunming Zhou69b576a2015-11-18 11:17:39 +0800995 mutex_init(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400997
998 return bo_va;
999}
1000
1001/**
1002 * amdgpu_vm_bo_map - map bo inside a vm
1003 *
1004 * @adev: amdgpu_device pointer
1005 * @bo_va: bo_va to store the address
1006 * @saddr: where to map the BO
1007 * @offset: requested offset in the BO
1008 * @flags: attributes of pages (read/write/valid/etc.)
1009 *
1010 * Add a mapping of the BO at the specefied addr into the VM.
1011 * Returns 0 for success, error for failure.
1012 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001013 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001014 */
1015int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1016 struct amdgpu_bo_va *bo_va,
1017 uint64_t saddr, uint64_t offset,
1018 uint64_t size, uint32_t flags)
1019{
1020 struct amdgpu_bo_va_mapping *mapping;
1021 struct amdgpu_vm *vm = bo_va->vm;
1022 struct interval_tree_node *it;
1023 unsigned last_pfn, pt_idx;
1024 uint64_t eaddr;
1025 int r;
1026
Christian König0be52de2015-05-18 14:37:27 +02001027 /* validate the parameters */
1028 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001029 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001030 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001031
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001032 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001033 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001034 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001035 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001036
1037 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001038 if (last_pfn >= adev->vm_manager.max_pfn) {
1039 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001040 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041 return -EINVAL;
1042 }
1043
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001044 saddr /= AMDGPU_GPU_PAGE_SIZE;
1045 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1046
Chunming Zhouc25867d2015-11-13 13:32:01 +08001047 spin_lock(&vm->it_lock);
Felix Kuehling005ae952015-11-23 17:43:48 -05001048 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001049 spin_unlock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050 if (it) {
1051 struct amdgpu_bo_va_mapping *tmp;
1052 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1053 /* bo and tmp overlap, invalid addr */
1054 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1055 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1056 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001057 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001058 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001059 }
1060
1061 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1062 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001063 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001064 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001065 }
1066
1067 INIT_LIST_HEAD(&mapping->list);
1068 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001069 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001070 mapping->offset = offset;
1071 mapping->flags = flags;
1072
Chunming Zhou69b576a2015-11-18 11:17:39 +08001073 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001074 list_add(&mapping->list, &bo_va->invalids);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001075 mutex_unlock(&bo_va->mutex);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001076 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001077 interval_tree_insert(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001078 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001079 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081 /* Make sure the page tables are allocated */
1082 saddr >>= amdgpu_vm_block_size;
1083 eaddr >>= amdgpu_vm_block_size;
1084
1085 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1086
1087 if (eaddr > vm->max_pde_used)
1088 vm->max_pde_used = eaddr;
1089
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090 /* walk over the address space and allocate the page tables */
1091 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001092 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001093 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001094 struct amdgpu_bo *pt;
1095
Christian Königee1782c2015-12-11 21:01:23 +01001096 entry = &vm->page_tables[pt_idx].entry;
1097 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098 continue;
1099
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1101 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001102 AMDGPU_GEM_DOMAIN_VRAM,
1103 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001104 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001105 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001107
Christian König82b9c552015-11-27 16:49:00 +01001108 /* Keep a reference to the page table to avoid freeing
1109 * them up in the wrong order.
1110 */
1111 pt->parent = amdgpu_bo_ref(vm->page_directory);
1112
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001113 r = amdgpu_vm_clear_bo(adev, pt);
1114 if (r) {
1115 amdgpu_bo_unref(&pt);
1116 goto error_free;
1117 }
1118
Christian Königee1782c2015-12-11 21:01:23 +01001119 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001120 entry->priority = 0;
1121 entry->tv.bo = &entry->robj->tbo;
1122 entry->tv.shared = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001123 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124 }
1125
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001126 return 0;
1127
1128error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001130 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001131 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001132 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001133 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134 kfree(mapping);
1135
Chunming Zhouf48b2652015-10-16 14:06:19 +08001136error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001137 return r;
1138}
1139
1140/**
1141 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1142 *
1143 * @adev: amdgpu_device pointer
1144 * @bo_va: bo_va to remove the address from
1145 * @saddr: where to the BO is mapped
1146 *
1147 * Remove a mapping of the BO at the specefied addr from the VM.
1148 * Returns 0 for success, error for failure.
1149 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001150 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151 */
1152int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1153 struct amdgpu_bo_va *bo_va,
1154 uint64_t saddr)
1155{
1156 struct amdgpu_bo_va_mapping *mapping;
1157 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001158 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159
Christian König6c7fc502015-06-05 20:56:17 +02001160 saddr /= AMDGPU_GPU_PAGE_SIZE;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001161 mutex_lock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001162 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001163 if (mapping->it.start == saddr)
1164 break;
1165 }
1166
Christian König7fc11952015-07-30 11:53:42 +02001167 if (&mapping->list == &bo_va->valids) {
1168 valid = false;
1169
1170 list_for_each_entry(mapping, &bo_va->invalids, list) {
1171 if (mapping->it.start == saddr)
1172 break;
1173 }
1174
Chunming Zhou69b576a2015-11-18 11:17:39 +08001175 if (&mapping->list == &bo_va->invalids) {
1176 mutex_unlock(&bo_va->mutex);
Christian König7fc11952015-07-30 11:53:42 +02001177 return -ENOENT;
Chunming Zhou69b576a2015-11-18 11:17:39 +08001178 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001179 }
Chunming Zhou69b576a2015-11-18 11:17:39 +08001180 mutex_unlock(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001182 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001183 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001184 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001185 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001186
jimqu81d75a32015-12-04 17:17:00 +08001187 if (valid) {
1188 spin_lock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001189 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001190 spin_unlock(&vm->freed_lock);
1191 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 kfree(mapping);
jimqu81d75a32015-12-04 17:17:00 +08001193 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001194
1195 return 0;
1196}
1197
1198/**
1199 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1200 *
1201 * @adev: amdgpu_device pointer
1202 * @bo_va: requested bo_va
1203 *
1204 * Remove @bo_va->bo from the requested vm (cayman+).
1205 *
1206 * Object have to be reserved!
1207 */
1208void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1209 struct amdgpu_bo_va *bo_va)
1210{
1211 struct amdgpu_bo_va_mapping *mapping, *next;
1212 struct amdgpu_vm *vm = bo_va->vm;
1213
1214 list_del(&bo_va->bo_list);
1215
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001216 spin_lock(&vm->status_lock);
1217 list_del(&bo_va->vm_status);
1218 spin_unlock(&vm->status_lock);
1219
Christian König7fc11952015-07-30 11:53:42 +02001220 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001221 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001222 spin_lock(&vm->it_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001224 spin_unlock(&vm->it_lock);
Christian König93e3e432015-06-09 16:58:33 +02001225 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
jimqu81d75a32015-12-04 17:17:00 +08001226 spin_lock(&vm->freed_lock);
Christian König7fc11952015-07-30 11:53:42 +02001227 list_add(&mapping->list, &vm->freed);
jimqu81d75a32015-12-04 17:17:00 +08001228 spin_unlock(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001229 }
Christian König7fc11952015-07-30 11:53:42 +02001230 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1231 list_del(&mapping->list);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001232 spin_lock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001233 interval_tree_remove(&mapping->it, &vm->va);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001234 spin_unlock(&vm->it_lock);
Christian König7fc11952015-07-30 11:53:42 +02001235 kfree(mapping);
1236 }
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001237 fence_put(bo_va->last_pt_update);
Chunming Zhou69b576a2015-11-18 11:17:39 +08001238 mutex_destroy(&bo_va->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001240}
1241
1242/**
1243 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1244 *
1245 * @adev: amdgpu_device pointer
1246 * @vm: requested vm
1247 * @bo: amdgpu buffer object
1248 *
1249 * Mark @bo as invalid (cayman+).
1250 */
1251void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1252 struct amdgpu_bo *bo)
1253{
1254 struct amdgpu_bo_va *bo_va;
1255
1256 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001257 spin_lock(&bo_va->vm->status_lock);
1258 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001260 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 }
1262}
1263
1264/**
1265 * amdgpu_vm_init - initialize a vm instance
1266 *
1267 * @adev: amdgpu_device pointer
1268 * @vm: requested vm
1269 *
1270 * Init @vm fields (cayman+).
1271 */
1272int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1273{
1274 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1275 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001276 unsigned pd_size, pd_entries;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001277 int i, r;
1278
1279 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1280 vm->ids[i].id = 0;
1281 vm->ids[i].flushed_updates = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 vm->va = RB_ROOT;
1284 spin_lock_init(&vm->status_lock);
1285 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001286 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001287 INIT_LIST_HEAD(&vm->freed);
Chunming Zhouc25867d2015-11-13 13:32:01 +08001288 spin_lock_init(&vm->it_lock);
jimqu81d75a32015-12-04 17:17:00 +08001289 spin_lock_init(&vm->freed_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 pd_size = amdgpu_vm_directory_size(adev);
1291 pd_entries = amdgpu_vm_num_pdes(adev);
1292
1293 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001294 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 if (vm->page_tables == NULL) {
1296 DRM_ERROR("Cannot allocate memory for page table array\n");
1297 return -ENOMEM;
1298 }
1299
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001300 vm->page_directory_fence = NULL;
1301
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001302 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001303 AMDGPU_GEM_DOMAIN_VRAM,
1304 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001305 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306 if (r)
1307 return r;
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001308 r = amdgpu_bo_reserve(vm->page_directory, false);
1309 if (r) {
1310 amdgpu_bo_unref(&vm->page_directory);
1311 vm->page_directory = NULL;
1312 return r;
1313 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001315 amdgpu_bo_unreserve(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001316 if (r) {
1317 amdgpu_bo_unref(&vm->page_directory);
1318 vm->page_directory = NULL;
1319 return r;
1320 }
1321
1322 return 0;
1323}
1324
1325/**
1326 * amdgpu_vm_fini - tear down a vm instance
1327 *
1328 * @adev: amdgpu_device pointer
1329 * @vm: requested vm
1330 *
1331 * Tear down @vm (cayman+).
1332 * Unbind the VM and remove all bos from the vm bo list
1333 */
1334void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1335{
1336 struct amdgpu_bo_va_mapping *mapping, *tmp;
1337 int i;
1338
1339 if (!RB_EMPTY_ROOT(&vm->va)) {
1340 dev_err(adev->dev, "still active bo inside vm\n");
1341 }
1342 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1343 list_del(&mapping->list);
1344 interval_tree_remove(&mapping->it, &vm->va);
1345 kfree(mapping);
1346 }
1347 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1348 list_del(&mapping->list);
1349 kfree(mapping);
1350 }
1351
1352 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001353 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001354 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001355
1356 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001357 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Christian König1c16c0a2015-11-14 21:31:40 +01001359 unsigned id = vm->ids[i].id;
1360
1361 atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner,
1362 (long)vm, 0);
Chunming Zhou3c623382015-08-20 18:33:59 +08001363 fence_put(vm->ids[i].flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001364 }
1365
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366}
Christian Königea89f8c2015-11-15 20:52:06 +01001367
1368/**
1369 * amdgpu_vm_manager_fini - cleanup VM manager
1370 *
1371 * @adev: amdgpu_device pointer
1372 *
1373 * Cleanup the VM manager and free resources.
1374 */
1375void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1376{
1377 unsigned i;
1378
1379 for (i = 0; i < AMDGPU_NUM_VM; ++i)
Christian König1c16c0a2015-11-14 21:31:40 +01001380 fence_put(adev->vm_manager.ids[i].active);
Christian Königea89f8c2015-11-15 20:52:06 +01001381}