blob: de882b0db3507acafdc01a7377d1e6f838365c8d [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/**
78 * amdgpu_vm_get_bos - add the vm BOs to a validation list
79 *
80 * @vm: vm providing the BOs
81 * @head: head of validation list
82 *
83 * Add the page directory to the list of BOs to
84 * validate for command submission (cayman+).
85 */
86struct amdgpu_bo_list_entry *amdgpu_vm_get_bos(struct amdgpu_device *adev,
87 struct amdgpu_vm *vm,
88 struct list_head *head)
89{
90 struct amdgpu_bo_list_entry *list;
91 unsigned i, idx;
92
monk.liu3d5a08c2015-05-26 10:22:41 +080093 mutex_lock(&vm->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 list = drm_malloc_ab(vm->max_pde_used + 2,
95 sizeof(struct amdgpu_bo_list_entry));
monk.liu3d5a08c2015-05-26 10:22:41 +080096 if (!list) {
97 mutex_unlock(&vm->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 return NULL;
monk.liu3d5a08c2015-05-26 10:22:41 +080099 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100
101 /* add the vm page table to the list */
102 list[0].robj = vm->page_directory;
103 list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
104 list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
105 list[0].priority = 0;
106 list[0].tv.bo = &vm->page_directory->tbo;
107 list[0].tv.shared = true;
108 list_add(&list[0].tv.head, head);
109
110 for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
111 if (!vm->page_tables[i].bo)
112 continue;
113
114 list[idx].robj = vm->page_tables[i].bo;
115 list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
116 list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
117 list[idx].priority = 0;
118 list[idx].tv.bo = &list[idx].robj->tbo;
119 list[idx].tv.shared = true;
120 list_add(&list[idx++].tv.head, head);
121 }
monk.liu3d5a08c2015-05-26 10:22:41 +0800122 mutex_unlock(&vm->mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123
124 return list;
125}
126
127/**
128 * amdgpu_vm_grab_id - allocate the next free VMID
129 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200131 * @ring: ring we want to submit job to
132 * @sync: sync object where we add dependencies
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 *
Christian König7f8a5292015-07-20 16:09:40 +0200134 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 *
Christian König7f8a5292015-07-20 16:09:40 +0200136 * Global mutex must be locked!
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 */
Christian König7f8a5292015-07-20 16:09:40 +0200138int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
139 struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140{
141 struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {};
142 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
143 struct amdgpu_device *adev = ring->adev;
144
145 unsigned choices[2] = {};
146 unsigned i;
147
148 /* check if the id is still valid */
149 if (vm_id->id && vm_id->last_id_use &&
150 vm_id->last_id_use == adev->vm_manager.active[vm_id->id])
Christian König7f8a5292015-07-20 16:09:40 +0200151 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152
153 /* we definately need to flush */
154 vm_id->pd_gpu_addr = ~0ll;
155
156 /* skip over VMID 0, since it is the system VM */
157 for (i = 1; i < adev->vm_manager.nvm; ++i) {
158 struct amdgpu_fence *fence = adev->vm_manager.active[i];
159
160 if (fence == NULL) {
161 /* found a free one */
162 vm_id->id = i;
163 trace_amdgpu_vm_grab_id(i, ring->idx);
Christian König7f8a5292015-07-20 16:09:40 +0200164 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 }
166
167 if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) {
168 best[fence->ring->idx] = fence;
169 choices[fence->ring == ring ? 0 : 1] = i;
170 }
171 }
172
173 for (i = 0; i < 2; ++i) {
174 if (choices[i]) {
Christian König7f8a5292015-07-20 16:09:40 +0200175 struct amdgpu_fence *fence;
176
177 fence = adev->vm_manager.active[choices[i]];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178 vm_id->id = choices[i];
Christian König7f8a5292015-07-20 16:09:40 +0200179
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180 trace_amdgpu_vm_grab_id(choices[i], ring->idx);
Christian König7f8a5292015-07-20 16:09:40 +0200181 return amdgpu_sync_fence(ring->adev, sync, &fence->base);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182 }
183 }
184
185 /* should never happen */
186 BUG();
Christian König7f8a5292015-07-20 16:09:40 +0200187 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188}
189
190/**
191 * amdgpu_vm_flush - hardware flush the vm
192 *
193 * @ring: ring to use for flush
194 * @vm: vm we want to flush
195 * @updates: last vm update that we waited for
196 *
197 * Flush the vm (cayman+).
198 *
199 * Global and local mutex must be locked!
200 */
201void amdgpu_vm_flush(struct amdgpu_ring *ring,
202 struct amdgpu_vm *vm,
203 struct amdgpu_fence *updates)
204{
205 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
206 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
Christian Königfc8fa5e2015-07-20 15:47:30 +0200207 struct amdgpu_fence *flushed_updates = vm_id->flushed_updates;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208
Christian Königfc8fa5e2015-07-20 15:47:30 +0200209 if (pd_addr != vm_id->pd_gpu_addr || !flushed_updates ||
210 (updates && amdgpu_fence_is_earlier(flushed_updates, updates))) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211
212 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
Christian Königfc8fa5e2015-07-20 15:47:30 +0200213 vm_id->flushed_updates = amdgpu_fence_ref(
214 amdgpu_fence_later(flushed_updates, updates));
215 amdgpu_fence_unref(&flushed_updates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216 vm_id->pd_gpu_addr = pd_addr;
217 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
218 }
219}
220
221/**
222 * amdgpu_vm_fence - remember fence for vm
223 *
224 * @adev: amdgpu_device pointer
225 * @vm: vm we want to fence
226 * @fence: fence to remember
227 *
228 * Fence the vm (cayman+).
229 * Set the fence used to protect page table and id.
230 *
231 * Global and local mutex must be locked!
232 */
233void amdgpu_vm_fence(struct amdgpu_device *adev,
234 struct amdgpu_vm *vm,
235 struct amdgpu_fence *fence)
236{
237 unsigned ridx = fence->ring->idx;
238 unsigned vm_id = vm->ids[ridx].id;
239
240 amdgpu_fence_unref(&adev->vm_manager.active[vm_id]);
241 adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence);
242
243 amdgpu_fence_unref(&vm->ids[ridx].last_id_use);
244 vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence);
245}
246
247/**
248 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
249 *
250 * @vm: requested vm
251 * @bo: requested buffer object
252 *
253 * Find @bo inside the requested vm (cayman+).
254 * Search inside the @bos vm list for the requested vm
255 * Returns the found bo_va or NULL if none is found
256 *
257 * Object has to be reserved!
258 */
259struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
260 struct amdgpu_bo *bo)
261{
262 struct amdgpu_bo_va *bo_va;
263
264 list_for_each_entry(bo_va, &bo->va, bo_list) {
265 if (bo_va->vm == vm) {
266 return bo_va;
267 }
268 }
269 return NULL;
270}
271
272/**
273 * amdgpu_vm_update_pages - helper to call the right asic function
274 *
275 * @adev: amdgpu_device pointer
276 * @ib: indirect buffer to fill with commands
277 * @pe: addr of the page entry
278 * @addr: dst addr to write into pe
279 * @count: number of page entries to update
280 * @incr: increase next addr by incr bytes
281 * @flags: hw access flags
282 * @gtt_flags: GTT hw access flags
283 *
284 * Traces the parameters and calls the right asic functions
285 * to setup the page table using the DMA.
286 */
287static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
288 struct amdgpu_ib *ib,
289 uint64_t pe, uint64_t addr,
290 unsigned count, uint32_t incr,
291 uint32_t flags, uint32_t gtt_flags)
292{
293 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
294
295 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
296 uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
297 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
298
299 } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
300 amdgpu_vm_write_pte(adev, ib, pe, addr,
301 count, incr, flags);
302
303 } else {
304 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
305 count, incr, flags);
306 }
307}
308
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800309static int amdgpu_vm_free_job(
310 struct amdgpu_cs_parser *sched_job)
311{
312 int i;
313 for (i = 0; i < sched_job->num_ibs; i++)
314 amdgpu_ib_free(sched_job->adev, &sched_job->ibs[i]);
315 kfree(sched_job->ibs);
316 return 0;
317}
318
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400319/**
320 * amdgpu_vm_clear_bo - initially clear the page dir/table
321 *
322 * @adev: amdgpu_device pointer
323 * @bo: bo to clear
324 */
325static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
326 struct amdgpu_bo *bo)
327{
328 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800329 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800330 struct amdgpu_ib *ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331 unsigned entries;
332 uint64_t addr;
333 int r;
334
335 r = amdgpu_bo_reserve(bo, false);
336 if (r)
337 return r;
338
monk.liuca952612015-05-25 14:44:05 +0800339 r = reservation_object_reserve_shared(bo->tbo.resv);
340 if (r)
341 return r;
342
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400343 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
344 if (r)
345 goto error_unreserve;
346
347 addr = amdgpu_bo_gpu_offset(bo);
348 entries = amdgpu_bo_size(bo) / 8;
349
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800350 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
351 if (!ib)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400352 goto error_unreserve;
353
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800354 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400355 if (r)
356 goto error_free;
357
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800358 ib->length_dw = 0;
359
360 amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0);
361 amdgpu_vm_pad_ib(adev, ib);
362 WARN_ON(ib->length_dw > 64);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800363 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
364 &amdgpu_vm_free_job,
365 AMDGPU_FENCE_OWNER_VM,
366 &fence);
367 if (!r)
368 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800369 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800370 if (amdgpu_enable_scheduler) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800371 amdgpu_bo_unreserve(bo);
372 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800373 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400374error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800375 amdgpu_ib_free(adev, ib);
376 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377
378error_unreserve:
379 amdgpu_bo_unreserve(bo);
380 return r;
381}
382
383/**
384 * amdgpu_vm_map_gart - get the physical address of a gart page
385 *
386 * @adev: amdgpu_device pointer
387 * @addr: the unmapped addr
388 *
389 * Look up the physical address of the page that the pte resolves
390 * to (cayman+).
391 * Returns the physical address of the page.
392 */
393uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
394{
395 uint64_t result;
396
397 /* page table offset */
398 result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
399
400 /* in case cpu page size != gpu page size*/
401 result |= addr & (~PAGE_MASK);
402
403 return result;
404}
405
406/**
407 * amdgpu_vm_update_pdes - make sure that page directory is valid
408 *
409 * @adev: amdgpu_device pointer
410 * @vm: requested vm
411 * @start: start of GPU address range
412 * @end: end of GPU address range
413 *
414 * Allocates new page tables if necessary
415 * and updates the page directory (cayman+).
416 * Returns 0 for success, error for failure.
417 *
418 * Global and local mutex must be locked!
419 */
420int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
421 struct amdgpu_vm *vm)
422{
423 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
424 struct amdgpu_bo *pd = vm->page_directory;
425 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
426 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
427 uint64_t last_pde = ~0, last_pt = ~0;
428 unsigned count = 0, pt_idx, ndw;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800429 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800430 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800431
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432 int r;
433
434 /* padding, etc. */
435 ndw = 64;
436
437 /* assume the worst case */
438 ndw += vm->max_pde_used * 6;
439
440 /* update too big for an IB */
441 if (ndw > 0xfffff)
442 return -ENOMEM;
443
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800444 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
445 if (!ib)
446 return -ENOMEM;
447
448 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449 if (r)
450 return r;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800451 ib->length_dw = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452
453 /* walk over the address space and update the page directory */
454 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
455 struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
456 uint64_t pde, pt;
457
458 if (bo == NULL)
459 continue;
460
461 pt = amdgpu_bo_gpu_offset(bo);
462 if (vm->page_tables[pt_idx].addr == pt)
463 continue;
464 vm->page_tables[pt_idx].addr = pt;
465
466 pde = pd_addr + pt_idx * 8;
467 if (((last_pde + 8 * count) != pde) ||
468 ((last_pt + incr * count) != pt)) {
469
470 if (count) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800471 amdgpu_vm_update_pages(adev, ib, last_pde,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472 last_pt, count, incr,
473 AMDGPU_PTE_VALID, 0);
474 }
475
476 count = 1;
477 last_pde = pde;
478 last_pt = pt;
479 } else {
480 ++count;
481 }
482 }
483
484 if (count)
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800485 amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 incr, AMDGPU_PTE_VALID, 0);
487
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800488 if (ib->length_dw != 0) {
489 amdgpu_vm_pad_ib(adev, ib);
490 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
491 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800492 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
493 &amdgpu_vm_free_job,
494 AMDGPU_FENCE_OWNER_VM,
495 &fence);
496 if (r)
497 goto error_free;
498 amdgpu_bo_fence(pd, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800499 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 }
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800501
502 if (!amdgpu_enable_scheduler || ib->length_dw == 0) {
503 amdgpu_ib_free(adev, ib);
504 kfree(ib);
505 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506
507 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800508
509error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800510 amdgpu_ib_free(adev, ib);
511 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800512 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513}
514
515/**
516 * amdgpu_vm_frag_ptes - add fragment information to PTEs
517 *
518 * @adev: amdgpu_device pointer
519 * @ib: IB for the update
520 * @pe_start: first PTE to handle
521 * @pe_end: last PTE to handle
522 * @addr: addr those PTEs should point to
523 * @flags: hw mapping flags
524 * @gtt_flags: GTT hw mapping flags
525 *
526 * Global and local mutex must be locked!
527 */
528static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
529 struct amdgpu_ib *ib,
530 uint64_t pe_start, uint64_t pe_end,
531 uint64_t addr, uint32_t flags,
532 uint32_t gtt_flags)
533{
534 /**
535 * The MC L1 TLB supports variable sized pages, based on a fragment
536 * field in the PTE. When this field is set to a non-zero value, page
537 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
538 * flags are considered valid for all PTEs within the fragment range
539 * and corresponding mappings are assumed to be physically contiguous.
540 *
541 * The L1 TLB can store a single PTE for the whole fragment,
542 * significantly increasing the space available for translation
543 * caching. This leads to large improvements in throughput when the
544 * TLB is under pressure.
545 *
546 * The L2 TLB distributes small and large fragments into two
547 * asymmetric partitions. The large fragment cache is significantly
548 * larger. Thus, we try to use large fragments wherever possible.
549 * Userspace can support this by aligning virtual base address and
550 * allocation size to the fragment size.
551 */
552
553 /* SI and newer are optimized for 64KB */
554 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
555 uint64_t frag_align = 0x80;
556
557 uint64_t frag_start = ALIGN(pe_start, frag_align);
558 uint64_t frag_end = pe_end & ~(frag_align - 1);
559
560 unsigned count;
561
562 /* system pages are non continuously */
563 if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
564 (frag_start >= frag_end)) {
565
566 count = (pe_end - pe_start) / 8;
567 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
568 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
569 return;
570 }
571
572 /* handle the 4K area at the beginning */
573 if (pe_start != frag_start) {
574 count = (frag_start - pe_start) / 8;
575 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
576 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
577 addr += AMDGPU_GPU_PAGE_SIZE * count;
578 }
579
580 /* handle the area in the middle */
581 count = (frag_end - frag_start) / 8;
582 amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
583 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
584 gtt_flags);
585
586 /* handle the 4K area at the end */
587 if (frag_end != pe_end) {
588 addr += AMDGPU_GPU_PAGE_SIZE * count;
589 count = (pe_end - frag_end) / 8;
590 amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
591 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
592 }
593}
594
595/**
596 * amdgpu_vm_update_ptes - make sure that page tables are valid
597 *
598 * @adev: amdgpu_device pointer
599 * @vm: requested vm
600 * @start: start of GPU address range
601 * @end: end of GPU address range
602 * @dst: destination address to map to
603 * @flags: mapping flags
604 *
605 * Update the page tables in the range @start - @end (cayman+).
606 *
607 * Global and local mutex must be locked!
608 */
609static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
610 struct amdgpu_vm *vm,
611 struct amdgpu_ib *ib,
612 uint64_t start, uint64_t end,
613 uint64_t dst, uint32_t flags,
614 uint32_t gtt_flags)
615{
616 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
617 uint64_t last_pte = ~0, last_dst = ~0;
618 unsigned count = 0;
619 uint64_t addr;
620
621 /* walk over the address space and update the page tables */
622 for (addr = start; addr < end; ) {
623 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
624 struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
625 unsigned nptes;
626 uint64_t pte;
627 int r;
628
629 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv,
630 AMDGPU_FENCE_OWNER_VM);
631 r = reservation_object_reserve_shared(pt->tbo.resv);
632 if (r)
633 return r;
634
635 if ((addr & ~mask) == (end & ~mask))
636 nptes = end - addr;
637 else
638 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
639
640 pte = amdgpu_bo_gpu_offset(pt);
641 pte += (addr & mask) * 8;
642
643 if ((last_pte + 8 * count) != pte) {
644
645 if (count) {
646 amdgpu_vm_frag_ptes(adev, ib, last_pte,
647 last_pte + 8 * count,
648 last_dst, flags,
649 gtt_flags);
650 }
651
652 count = nptes;
653 last_pte = pte;
654 last_dst = dst;
655 } else {
656 count += nptes;
657 }
658
659 addr += nptes;
660 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
661 }
662
663 if (count) {
664 amdgpu_vm_frag_ptes(adev, ib, last_pte,
665 last_pte + 8 * count,
666 last_dst, flags, gtt_flags);
667 }
668
669 return 0;
670}
671
672/**
673 * amdgpu_vm_fence_pts - fence page tables after an update
674 *
675 * @vm: requested vm
676 * @start: start of GPU address range
677 * @end: end of GPU address range
678 * @fence: fence to use
679 *
680 * Fence the page tables in the range @start - @end (cayman+).
681 *
682 * Global and local mutex must be locked!
683 */
684static void amdgpu_vm_fence_pts(struct amdgpu_vm *vm,
685 uint64_t start, uint64_t end,
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800686 struct fence *fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687{
688 unsigned i;
689
690 start >>= amdgpu_vm_block_size;
691 end >>= amdgpu_vm_block_size;
692
693 for (i = start; i <= end; ++i)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800694 amdgpu_bo_fence(vm->page_tables[i].bo, fence, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695}
696
697/**
698 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
699 *
700 * @adev: amdgpu_device pointer
701 * @vm: requested vm
702 * @mapping: mapped range and flags to use for the update
703 * @addr: addr to set the area to
704 * @gtt_flags: flags as they are used for GTT
705 * @fence: optional resulting fence
706 *
707 * Fill in the page table entries for @mapping.
708 * Returns 0 for success, -EINVAL for failure.
709 *
710 * Object have to be reserved and mutex must be locked!
711 */
712static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
713 struct amdgpu_vm *vm,
714 struct amdgpu_bo_va_mapping *mapping,
715 uint64_t addr, uint32_t gtt_flags,
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800716 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717{
718 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
719 unsigned nptes, ncmds, ndw;
720 uint32_t flags = gtt_flags;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800721 struct amdgpu_ib *ib;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800722 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 int r;
724
725 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
726 * but in case of something, we filter the flags in first place
727 */
728 if (!(mapping->flags & AMDGPU_PTE_READABLE))
729 flags &= ~AMDGPU_PTE_READABLE;
730 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
731 flags &= ~AMDGPU_PTE_WRITEABLE;
732
733 trace_amdgpu_vm_bo_update(mapping);
734
735 nptes = mapping->it.last - mapping->it.start + 1;
736
737 /*
738 * reserve space for one command every (1 << BLOCK_SIZE)
739 * entries or 2k dwords (whatever is smaller)
740 */
741 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
742
743 /* padding, etc. */
744 ndw = 64;
745
746 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
747 /* only copy commands needed */
748 ndw += ncmds * 7;
749
750 } else if (flags & AMDGPU_PTE_SYSTEM) {
751 /* header for write data commands */
752 ndw += ncmds * 4;
753
754 /* body of write data command */
755 ndw += nptes * 2;
756
757 } else {
758 /* set page commands needed */
759 ndw += ncmds * 10;
760
761 /* two extra commands for begin/end of fragment */
762 ndw += 2 * 10;
763 }
764
765 /* update too big for an IB */
766 if (ndw > 0xfffff)
767 return -ENOMEM;
768
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800769 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
770 if (!ib)
771 return -ENOMEM;
772
773 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
774 if (r) {
775 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776 return r;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800777 }
778
779 ib->length_dw = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780
781 if (!(flags & AMDGPU_PTE_VALID)) {
782 unsigned i;
783
784 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
785 struct amdgpu_fence *f = vm->ids[i].last_id_use;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800786 r = amdgpu_sync_fence(adev, &ib->sync, &f->base);
Christian König91e1a522015-07-06 22:06:40 +0200787 if (r)
788 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 }
790 }
791
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800792 r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400793 mapping->it.last + 1, addr + mapping->offset,
794 flags, gtt_flags);
795
796 if (r) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800797 amdgpu_ib_free(adev, ib);
798 kfree(ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400799 return r;
800 }
801
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800802 amdgpu_vm_pad_ib(adev, ib);
803 WARN_ON(ib->length_dw > ndw);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800804 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
805 &amdgpu_vm_free_job,
806 AMDGPU_FENCE_OWNER_VM,
807 &f);
808 if (r)
809 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400810
Chunming Zhou4af9f072015-08-03 12:57:31 +0800811 amdgpu_vm_fence_pts(vm, mapping->it.start,
812 mapping->it.last + 1, f);
813 if (fence) {
814 fence_put(*fence);
815 *fence = fence_get(f);
816 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800817 fence_put(f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800818 if (!amdgpu_enable_scheduler) {
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800819 amdgpu_ib_free(adev, ib);
820 kfree(ib);
821 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400822 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800823
824error_free:
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800825 amdgpu_ib_free(adev, ib);
826 kfree(ib);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800827 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400828}
829
830/**
831 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
832 *
833 * @adev: amdgpu_device pointer
834 * @bo_va: requested BO and VM object
835 * @mem: ttm mem
836 *
837 * Fill in the page table entries for @bo_va.
838 * Returns 0 for success, -EINVAL for failure.
839 *
840 * Object have to be reserved and mutex must be locked!
841 */
842int amdgpu_vm_bo_update(struct amdgpu_device *adev,
843 struct amdgpu_bo_va *bo_va,
844 struct ttm_mem_reg *mem)
845{
846 struct amdgpu_vm *vm = bo_va->vm;
847 struct amdgpu_bo_va_mapping *mapping;
848 uint32_t flags;
849 uint64_t addr;
850 int r;
851
852 if (mem) {
853 addr = mem->start << PAGE_SHIFT;
854 if (mem->mem_type != TTM_PL_TT)
855 addr += adev->vm_manager.vram_base_offset;
856 } else {
857 addr = 0;
858 }
859
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400860 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
861
Christian König7fc11952015-07-30 11:53:42 +0200862 spin_lock(&vm->status_lock);
863 if (!list_empty(&bo_va->vm_status))
864 list_splice_init(&bo_va->valids, &bo_va->invalids);
865 spin_unlock(&vm->status_lock);
866
867 list_for_each_entry(mapping, &bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400868 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
869 flags, &bo_va->last_pt_update);
870 if (r)
871 return r;
872 }
873
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 spin_lock(&vm->status_lock);
875 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +0200876 if (!mem)
877 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878 spin_unlock(&vm->status_lock);
879
880 return 0;
881}
882
883/**
884 * amdgpu_vm_clear_freed - clear freed BOs in the PT
885 *
886 * @adev: amdgpu_device pointer
887 * @vm: requested vm
888 *
889 * Make sure all freed BOs are cleared in the PT.
890 * Returns 0 for success.
891 *
892 * PTs have to be reserved and mutex must be locked!
893 */
894int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
895 struct amdgpu_vm *vm)
896{
897 struct amdgpu_bo_va_mapping *mapping;
898 int r;
899
900 while (!list_empty(&vm->freed)) {
901 mapping = list_first_entry(&vm->freed,
902 struct amdgpu_bo_va_mapping, list);
903 list_del(&mapping->list);
904
905 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
906 kfree(mapping);
907 if (r)
908 return r;
909
910 }
911 return 0;
912
913}
914
915/**
916 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
917 *
918 * @adev: amdgpu_device pointer
919 * @vm: requested vm
920 *
921 * Make sure all invalidated BOs are cleared in the PT.
922 * Returns 0 for success.
923 *
924 * PTs have to be reserved and mutex must be locked!
925 */
926int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +0800927 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400928{
monk.liucfe2c972015-05-26 15:01:54 +0800929 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +0200930 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931
932 spin_lock(&vm->status_lock);
933 while (!list_empty(&vm->invalidated)) {
934 bo_va = list_first_entry(&vm->invalidated,
935 struct amdgpu_bo_va, vm_status);
936 spin_unlock(&vm->status_lock);
937
938 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
939 if (r)
940 return r;
941
942 spin_lock(&vm->status_lock);
943 }
944 spin_unlock(&vm->status_lock);
945
monk.liucfe2c972015-05-26 15:01:54 +0800946 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800947 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +0200948
949 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950}
951
952/**
953 * amdgpu_vm_bo_add - add a bo to a specific vm
954 *
955 * @adev: amdgpu_device pointer
956 * @vm: requested vm
957 * @bo: amdgpu buffer object
958 *
959 * Add @bo into the requested vm (cayman+).
960 * Add @bo to the list of bos associated with the vm
961 * Returns newly added bo_va or NULL for failure
962 *
963 * Object has to be reserved!
964 */
965struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
966 struct amdgpu_vm *vm,
967 struct amdgpu_bo *bo)
968{
969 struct amdgpu_bo_va *bo_va;
970
971 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
972 if (bo_va == NULL) {
973 return NULL;
974 }
975 bo_va->vm = vm;
976 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400977 bo_va->ref_count = 1;
978 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +0200979 INIT_LIST_HEAD(&bo_va->valids);
980 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 INIT_LIST_HEAD(&bo_va->vm_status);
982
983 mutex_lock(&vm->mutex);
984 list_add_tail(&bo_va->bo_list, &bo->va);
985 mutex_unlock(&vm->mutex);
986
987 return bo_va;
988}
989
990/**
991 * amdgpu_vm_bo_map - map bo inside a vm
992 *
993 * @adev: amdgpu_device pointer
994 * @bo_va: bo_va to store the address
995 * @saddr: where to map the BO
996 * @offset: requested offset in the BO
997 * @flags: attributes of pages (read/write/valid/etc.)
998 *
999 * Add a mapping of the BO at the specefied addr into the VM.
1000 * Returns 0 for success, error for failure.
1001 *
1002 * Object has to be reserved and gets unreserved by this function!
1003 */
1004int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1005 struct amdgpu_bo_va *bo_va,
1006 uint64_t saddr, uint64_t offset,
1007 uint64_t size, uint32_t flags)
1008{
1009 struct amdgpu_bo_va_mapping *mapping;
1010 struct amdgpu_vm *vm = bo_va->vm;
1011 struct interval_tree_node *it;
1012 unsigned last_pfn, pt_idx;
1013 uint64_t eaddr;
1014 int r;
1015
Christian König0be52de2015-05-18 14:37:27 +02001016 /* validate the parameters */
1017 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1018 size == 0 || size & AMDGPU_GPU_PAGE_MASK) {
1019 amdgpu_bo_unreserve(bo_va->bo);
1020 return -EINVAL;
1021 }
1022
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023 /* make sure object fit at this offset */
1024 eaddr = saddr + size;
1025 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) {
1026 amdgpu_bo_unreserve(bo_va->bo);
1027 return -EINVAL;
1028 }
1029
1030 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
1031 if (last_pfn > adev->vm_manager.max_pfn) {
1032 dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n",
1033 last_pfn, adev->vm_manager.max_pfn);
1034 amdgpu_bo_unreserve(bo_va->bo);
1035 return -EINVAL;
1036 }
1037
1038 mutex_lock(&vm->mutex);
1039
1040 saddr /= AMDGPU_GPU_PAGE_SIZE;
1041 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1042
1043 it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1);
1044 if (it) {
1045 struct amdgpu_bo_va_mapping *tmp;
1046 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1047 /* bo and tmp overlap, invalid addr */
1048 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1049 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1050 tmp->it.start, tmp->it.last + 1);
1051 amdgpu_bo_unreserve(bo_va->bo);
1052 r = -EINVAL;
1053 goto error_unlock;
1054 }
1055
1056 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1057 if (!mapping) {
1058 amdgpu_bo_unreserve(bo_va->bo);
1059 r = -ENOMEM;
1060 goto error_unlock;
1061 }
1062
1063 INIT_LIST_HEAD(&mapping->list);
1064 mapping->it.start = saddr;
1065 mapping->it.last = eaddr - 1;
1066 mapping->offset = offset;
1067 mapping->flags = flags;
1068
Christian König7fc11952015-07-30 11:53:42 +02001069 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001070 interval_tree_insert(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001071 trace_amdgpu_vm_bo_map(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073 /* Make sure the page tables are allocated */
1074 saddr >>= amdgpu_vm_block_size;
1075 eaddr >>= amdgpu_vm_block_size;
1076
1077 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1078
1079 if (eaddr > vm->max_pde_used)
1080 vm->max_pde_used = eaddr;
1081
1082 amdgpu_bo_unreserve(bo_va->bo);
1083
1084 /* walk over the address space and allocate the page tables */
1085 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
1086 struct amdgpu_bo *pt;
1087
1088 if (vm->page_tables[pt_idx].bo)
1089 continue;
1090
1091 /* drop mutex to allocate and clear page table */
1092 mutex_unlock(&vm->mutex);
1093
1094 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1095 AMDGPU_GPU_PAGE_SIZE, true,
1096 AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &pt);
1097 if (r)
1098 goto error_free;
1099
1100 r = amdgpu_vm_clear_bo(adev, pt);
1101 if (r) {
1102 amdgpu_bo_unref(&pt);
1103 goto error_free;
1104 }
1105
1106 /* aquire mutex again */
1107 mutex_lock(&vm->mutex);
1108 if (vm->page_tables[pt_idx].bo) {
1109 /* someone else allocated the pt in the meantime */
1110 mutex_unlock(&vm->mutex);
1111 amdgpu_bo_unref(&pt);
1112 mutex_lock(&vm->mutex);
1113 continue;
1114 }
1115
1116 vm->page_tables[pt_idx].addr = 0;
1117 vm->page_tables[pt_idx].bo = pt;
1118 }
1119
1120 mutex_unlock(&vm->mutex);
1121 return 0;
1122
1123error_free:
1124 mutex_lock(&vm->mutex);
1125 list_del(&mapping->list);
1126 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001127 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001128 kfree(mapping);
1129
1130error_unlock:
1131 mutex_unlock(&vm->mutex);
1132 return r;
1133}
1134
1135/**
1136 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1137 *
1138 * @adev: amdgpu_device pointer
1139 * @bo_va: bo_va to remove the address from
1140 * @saddr: where to the BO is mapped
1141 *
1142 * Remove a mapping of the BO at the specefied addr from the VM.
1143 * Returns 0 for success, error for failure.
1144 *
1145 * Object has to be reserved and gets unreserved by this function!
1146 */
1147int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1148 struct amdgpu_bo_va *bo_va,
1149 uint64_t saddr)
1150{
1151 struct amdgpu_bo_va_mapping *mapping;
1152 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001153 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154
Christian König6c7fc502015-06-05 20:56:17 +02001155 saddr /= AMDGPU_GPU_PAGE_SIZE;
1156
Christian König7fc11952015-07-30 11:53:42 +02001157 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001158 if (mapping->it.start == saddr)
1159 break;
1160 }
1161
Christian König7fc11952015-07-30 11:53:42 +02001162 if (&mapping->list == &bo_va->valids) {
1163 valid = false;
1164
1165 list_for_each_entry(mapping, &bo_va->invalids, list) {
1166 if (mapping->it.start == saddr)
1167 break;
1168 }
1169
1170 if (&mapping->list == &bo_va->invalids) {
1171 amdgpu_bo_unreserve(bo_va->bo);
1172 return -ENOENT;
1173 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 }
1175
1176 mutex_lock(&vm->mutex);
1177 list_del(&mapping->list);
1178 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001179 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001180
Christian König7fc11952015-07-30 11:53:42 +02001181 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001182 list_add(&mapping->list, &vm->freed);
Christian König7fc11952015-07-30 11:53:42 +02001183 else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 kfree(mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001185 mutex_unlock(&vm->mutex);
1186 amdgpu_bo_unreserve(bo_va->bo);
1187
1188 return 0;
1189}
1190
1191/**
1192 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1193 *
1194 * @adev: amdgpu_device pointer
1195 * @bo_va: requested bo_va
1196 *
1197 * Remove @bo_va->bo from the requested vm (cayman+).
1198 *
1199 * Object have to be reserved!
1200 */
1201void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1202 struct amdgpu_bo_va *bo_va)
1203{
1204 struct amdgpu_bo_va_mapping *mapping, *next;
1205 struct amdgpu_vm *vm = bo_va->vm;
1206
1207 list_del(&bo_va->bo_list);
1208
1209 mutex_lock(&vm->mutex);
1210
1211 spin_lock(&vm->status_lock);
1212 list_del(&bo_va->vm_status);
1213 spin_unlock(&vm->status_lock);
1214
Christian König7fc11952015-07-30 11:53:42 +02001215 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001216 list_del(&mapping->list);
1217 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001218 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001219 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001220 }
Christian König7fc11952015-07-30 11:53:42 +02001221 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1222 list_del(&mapping->list);
1223 interval_tree_remove(&mapping->it, &vm->va);
1224 kfree(mapping);
1225 }
1226
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001227 fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001228 kfree(bo_va);
1229
1230 mutex_unlock(&vm->mutex);
1231}
1232
1233/**
1234 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1235 *
1236 * @adev: amdgpu_device pointer
1237 * @vm: requested vm
1238 * @bo: amdgpu buffer object
1239 *
1240 * Mark @bo as invalid (cayman+).
1241 */
1242void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1243 struct amdgpu_bo *bo)
1244{
1245 struct amdgpu_bo_va *bo_va;
1246
1247 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001248 spin_lock(&bo_va->vm->status_lock);
1249 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001251 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 }
1253}
1254
1255/**
1256 * amdgpu_vm_init - initialize a vm instance
1257 *
1258 * @adev: amdgpu_device pointer
1259 * @vm: requested vm
1260 *
1261 * Init @vm fields (cayman+).
1262 */
1263int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1264{
1265 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1266 AMDGPU_VM_PTE_COUNT * 8);
1267 unsigned pd_size, pd_entries, pts_size;
1268 int i, r;
1269
1270 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1271 vm->ids[i].id = 0;
1272 vm->ids[i].flushed_updates = NULL;
1273 vm->ids[i].last_id_use = NULL;
1274 }
1275 mutex_init(&vm->mutex);
1276 vm->va = RB_ROOT;
1277 spin_lock_init(&vm->status_lock);
1278 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001279 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 INIT_LIST_HEAD(&vm->freed);
1281
1282 pd_size = amdgpu_vm_directory_size(adev);
1283 pd_entries = amdgpu_vm_num_pdes(adev);
1284
1285 /* allocate page table array */
1286 pts_size = pd_entries * sizeof(struct amdgpu_vm_pt);
1287 vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
1288 if (vm->page_tables == NULL) {
1289 DRM_ERROR("Cannot allocate memory for page table array\n");
1290 return -ENOMEM;
1291 }
1292
1293 r = amdgpu_bo_create(adev, pd_size, align, true,
1294 AMDGPU_GEM_DOMAIN_VRAM, 0,
1295 NULL, &vm->page_directory);
1296 if (r)
1297 return r;
1298
1299 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
1300 if (r) {
1301 amdgpu_bo_unref(&vm->page_directory);
1302 vm->page_directory = NULL;
1303 return r;
1304 }
1305
1306 return 0;
1307}
1308
1309/**
1310 * amdgpu_vm_fini - tear down a vm instance
1311 *
1312 * @adev: amdgpu_device pointer
1313 * @vm: requested vm
1314 *
1315 * Tear down @vm (cayman+).
1316 * Unbind the VM and remove all bos from the vm bo list
1317 */
1318void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1319{
1320 struct amdgpu_bo_va_mapping *mapping, *tmp;
1321 int i;
1322
1323 if (!RB_EMPTY_ROOT(&vm->va)) {
1324 dev_err(adev->dev, "still active bo inside vm\n");
1325 }
1326 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1327 list_del(&mapping->list);
1328 interval_tree_remove(&mapping->it, &vm->va);
1329 kfree(mapping);
1330 }
1331 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1332 list_del(&mapping->list);
1333 kfree(mapping);
1334 }
1335
1336 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
1337 amdgpu_bo_unref(&vm->page_tables[i].bo);
1338 kfree(vm->page_tables);
1339
1340 amdgpu_bo_unref(&vm->page_directory);
1341
1342 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1343 amdgpu_fence_unref(&vm->ids[i].flushed_updates);
1344 amdgpu_fence_unref(&vm->ids[i].last_id_use);
1345 }
1346
1347 mutex_destroy(&vm->mutex);
1348}