06e207fd007b501e9857726989143d33a49aa370
[deliverable/linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_vm.c
1 /*
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 */
60 static 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 */
72 static 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 */
86 struct 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
93 list = drm_malloc_ab(vm->max_pde_used + 2,
94 sizeof(struct amdgpu_bo_list_entry));
95 if (!list) {
96 return NULL;
97 }
98
99 /* add the vm page table to the list */
100 list[0].robj = vm->page_directory;
101 list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
102 list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
103 list[0].priority = 0;
104 list[0].tv.bo = &vm->page_directory->tbo;
105 list[0].tv.shared = true;
106 list_add(&list[0].tv.head, head);
107
108 for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
109 if (!vm->page_tables[i].bo)
110 continue;
111
112 list[idx].robj = vm->page_tables[i].bo;
113 list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
114 list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
115 list[idx].priority = 0;
116 list[idx].tv.bo = &list[idx].robj->tbo;
117 list[idx].tv.shared = true;
118 list_add(&list[idx++].tv.head, head);
119 }
120
121 return list;
122 }
123
124 /**
125 * amdgpu_vm_grab_id - allocate the next free VMID
126 *
127 * @vm: vm to allocate id for
128 * @ring: ring we want to submit job to
129 * @sync: sync object where we add dependencies
130 *
131 * Allocate an id for the vm, adding fences to the sync obj as necessary.
132 *
133 * Global mutex must be locked!
134 */
135 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
136 struct amdgpu_sync *sync)
137 {
138 struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {};
139 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
140 struct amdgpu_device *adev = ring->adev;
141
142 unsigned choices[2] = {};
143 unsigned i;
144
145 /* check if the id is still valid */
146 if (vm_id->id && vm_id->last_id_use &&
147 vm_id->last_id_use == adev->vm_manager.active[vm_id->id]) {
148 trace_amdgpu_vm_grab_id(vm_id->id, ring->idx);
149 return 0;
150 }
151
152 /* we definately need to flush */
153 vm_id->pd_gpu_addr = ~0ll;
154
155 /* skip over VMID 0, since it is the system VM */
156 for (i = 1; i < adev->vm_manager.nvm; ++i) {
157 struct amdgpu_fence *fence = adev->vm_manager.active[i];
158
159 if (fence == NULL) {
160 /* found a free one */
161 vm_id->id = i;
162 trace_amdgpu_vm_grab_id(i, ring->idx);
163 return 0;
164 }
165
166 if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) {
167 best[fence->ring->idx] = fence;
168 choices[fence->ring == ring ? 0 : 1] = i;
169 }
170 }
171
172 for (i = 0; i < 2; ++i) {
173 if (choices[i]) {
174 struct amdgpu_fence *fence;
175
176 fence = adev->vm_manager.active[choices[i]];
177 vm_id->id = choices[i];
178
179 trace_amdgpu_vm_grab_id(choices[i], ring->idx);
180 return amdgpu_sync_fence(ring->adev, sync, &fence->base);
181 }
182 }
183
184 /* should never happen */
185 BUG();
186 return -EINVAL;
187 }
188
189 /**
190 * amdgpu_vm_flush - hardware flush the vm
191 *
192 * @ring: ring to use for flush
193 * @vm: vm we want to flush
194 * @updates: last vm update that we waited for
195 *
196 * Flush the vm (cayman+).
197 *
198 * Global and local mutex must be locked!
199 */
200 void amdgpu_vm_flush(struct amdgpu_ring *ring,
201 struct amdgpu_vm *vm,
202 struct fence *updates)
203 {
204 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
205 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
206 struct fence *flushed_updates = vm_id->flushed_updates;
207 bool is_earlier = false;
208
209 if (flushed_updates && updates) {
210 BUG_ON(flushed_updates->context != updates->context);
211 is_earlier = (updates->seqno - flushed_updates->seqno <=
212 INT_MAX) ? true : false;
213 }
214
215 if (pd_addr != vm_id->pd_gpu_addr || !flushed_updates ||
216 is_earlier) {
217
218 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
219 if (is_earlier) {
220 vm_id->flushed_updates = fence_get(updates);
221 fence_put(flushed_updates);
222 }
223 if (!flushed_updates)
224 vm_id->flushed_updates = fence_get(updates);
225 vm_id->pd_gpu_addr = pd_addr;
226 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
227 }
228 }
229
230 /**
231 * amdgpu_vm_fence - remember fence for vm
232 *
233 * @adev: amdgpu_device pointer
234 * @vm: vm we want to fence
235 * @fence: fence to remember
236 *
237 * Fence the vm (cayman+).
238 * Set the fence used to protect page table and id.
239 *
240 * Global and local mutex must be locked!
241 */
242 void amdgpu_vm_fence(struct amdgpu_device *adev,
243 struct amdgpu_vm *vm,
244 struct amdgpu_fence *fence)
245 {
246 unsigned ridx = fence->ring->idx;
247 unsigned vm_id = vm->ids[ridx].id;
248
249 amdgpu_fence_unref(&adev->vm_manager.active[vm_id]);
250 adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence);
251
252 amdgpu_fence_unref(&vm->ids[ridx].last_id_use);
253 vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence);
254 }
255
256 /**
257 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
258 *
259 * @vm: requested vm
260 * @bo: requested buffer object
261 *
262 * Find @bo inside the requested vm (cayman+).
263 * Search inside the @bos vm list for the requested vm
264 * Returns the found bo_va or NULL if none is found
265 *
266 * Object has to be reserved!
267 */
268 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
269 struct amdgpu_bo *bo)
270 {
271 struct amdgpu_bo_va *bo_va;
272
273 list_for_each_entry(bo_va, &bo->va, bo_list) {
274 if (bo_va->vm == vm) {
275 return bo_va;
276 }
277 }
278 return NULL;
279 }
280
281 /**
282 * amdgpu_vm_update_pages - helper to call the right asic function
283 *
284 * @adev: amdgpu_device pointer
285 * @ib: indirect buffer to fill with commands
286 * @pe: addr of the page entry
287 * @addr: dst addr to write into pe
288 * @count: number of page entries to update
289 * @incr: increase next addr by incr bytes
290 * @flags: hw access flags
291 * @gtt_flags: GTT hw access flags
292 *
293 * Traces the parameters and calls the right asic functions
294 * to setup the page table using the DMA.
295 */
296 static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
297 struct amdgpu_ib *ib,
298 uint64_t pe, uint64_t addr,
299 unsigned count, uint32_t incr,
300 uint32_t flags, uint32_t gtt_flags)
301 {
302 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
303
304 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
305 uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
306 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
307
308 } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
309 amdgpu_vm_write_pte(adev, ib, pe, addr,
310 count, incr, flags);
311
312 } else {
313 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
314 count, incr, flags);
315 }
316 }
317
318 int amdgpu_vm_free_job(struct amdgpu_job *job)
319 {
320 int i;
321 for (i = 0; i < job->num_ibs; i++)
322 amdgpu_ib_free(job->adev, &job->ibs[i]);
323 kfree(job->ibs);
324 return 0;
325 }
326
327 /**
328 * amdgpu_vm_clear_bo - initially clear the page dir/table
329 *
330 * @adev: amdgpu_device pointer
331 * @bo: bo to clear
332 */
333 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
334 struct amdgpu_bo *bo)
335 {
336 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
337 struct fence *fence = NULL;
338 struct amdgpu_ib *ib;
339 unsigned entries;
340 uint64_t addr;
341 int r;
342
343 r = amdgpu_bo_reserve(bo, false);
344 if (r)
345 return r;
346
347 r = reservation_object_reserve_shared(bo->tbo.resv);
348 if (r)
349 return r;
350
351 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
352 if (r)
353 goto error_unreserve;
354
355 addr = amdgpu_bo_gpu_offset(bo);
356 entries = amdgpu_bo_size(bo) / 8;
357
358 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
359 if (!ib)
360 goto error_unreserve;
361
362 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
363 if (r)
364 goto error_free;
365
366 ib->length_dw = 0;
367
368 amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0);
369 amdgpu_vm_pad_ib(adev, ib);
370 WARN_ON(ib->length_dw > 64);
371 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
372 &amdgpu_vm_free_job,
373 AMDGPU_FENCE_OWNER_VM,
374 &fence);
375 if (!r)
376 amdgpu_bo_fence(bo, fence, true);
377 fence_put(fence);
378 if (amdgpu_enable_scheduler) {
379 amdgpu_bo_unreserve(bo);
380 return 0;
381 }
382 error_free:
383 amdgpu_ib_free(adev, ib);
384 kfree(ib);
385
386 error_unreserve:
387 amdgpu_bo_unreserve(bo);
388 return r;
389 }
390
391 /**
392 * amdgpu_vm_map_gart - get the physical address of a gart page
393 *
394 * @adev: amdgpu_device pointer
395 * @addr: the unmapped addr
396 *
397 * Look up the physical address of the page that the pte resolves
398 * to (cayman+).
399 * Returns the physical address of the page.
400 */
401 uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
402 {
403 uint64_t result;
404
405 /* page table offset */
406 result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
407
408 /* in case cpu page size != gpu page size*/
409 result |= addr & (~PAGE_MASK);
410
411 return result;
412 }
413
414 /**
415 * amdgpu_vm_update_pdes - make sure that page directory is valid
416 *
417 * @adev: amdgpu_device pointer
418 * @vm: requested vm
419 * @start: start of GPU address range
420 * @end: end of GPU address range
421 *
422 * Allocates new page tables if necessary
423 * and updates the page directory (cayman+).
424 * Returns 0 for success, error for failure.
425 *
426 * Global and local mutex must be locked!
427 */
428 int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
429 struct amdgpu_vm *vm)
430 {
431 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
432 struct amdgpu_bo *pd = vm->page_directory;
433 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
434 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
435 uint64_t last_pde = ~0, last_pt = ~0;
436 unsigned count = 0, pt_idx, ndw;
437 struct amdgpu_ib *ib;
438 struct fence *fence = NULL;
439
440 int r;
441
442 /* padding, etc. */
443 ndw = 64;
444
445 /* assume the worst case */
446 ndw += vm->max_pde_used * 6;
447
448 /* update too big for an IB */
449 if (ndw > 0xfffff)
450 return -ENOMEM;
451
452 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
453 if (!ib)
454 return -ENOMEM;
455
456 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
457 if (r)
458 return r;
459 ib->length_dw = 0;
460
461 /* walk over the address space and update the page directory */
462 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
463 struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
464 uint64_t pde, pt;
465
466 if (bo == NULL)
467 continue;
468
469 pt = amdgpu_bo_gpu_offset(bo);
470 if (vm->page_tables[pt_idx].addr == pt)
471 continue;
472 vm->page_tables[pt_idx].addr = pt;
473
474 pde = pd_addr + pt_idx * 8;
475 if (((last_pde + 8 * count) != pde) ||
476 ((last_pt + incr * count) != pt)) {
477
478 if (count) {
479 amdgpu_vm_update_pages(adev, ib, last_pde,
480 last_pt, count, incr,
481 AMDGPU_PTE_VALID, 0);
482 }
483
484 count = 1;
485 last_pde = pde;
486 last_pt = pt;
487 } else {
488 ++count;
489 }
490 }
491
492 if (count)
493 amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count,
494 incr, AMDGPU_PTE_VALID, 0);
495
496 if (ib->length_dw != 0) {
497 amdgpu_vm_pad_ib(adev, ib);
498 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
499 WARN_ON(ib->length_dw > ndw);
500 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
501 &amdgpu_vm_free_job,
502 AMDGPU_FENCE_OWNER_VM,
503 &fence);
504 if (r)
505 goto error_free;
506
507 amdgpu_bo_fence(pd, fence, true);
508 fence_put(vm->page_directory_fence);
509 vm->page_directory_fence = fence_get(fence);
510 fence_put(fence);
511 }
512
513 if (!amdgpu_enable_scheduler || ib->length_dw == 0) {
514 amdgpu_ib_free(adev, ib);
515 kfree(ib);
516 }
517
518 return 0;
519
520 error_free:
521 amdgpu_ib_free(adev, ib);
522 kfree(ib);
523 return r;
524 }
525
526 /**
527 * amdgpu_vm_frag_ptes - add fragment information to PTEs
528 *
529 * @adev: amdgpu_device pointer
530 * @ib: IB for the update
531 * @pe_start: first PTE to handle
532 * @pe_end: last PTE to handle
533 * @addr: addr those PTEs should point to
534 * @flags: hw mapping flags
535 * @gtt_flags: GTT hw mapping flags
536 *
537 * Global and local mutex must be locked!
538 */
539 static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
540 struct amdgpu_ib *ib,
541 uint64_t pe_start, uint64_t pe_end,
542 uint64_t addr, uint32_t flags,
543 uint32_t gtt_flags)
544 {
545 /**
546 * The MC L1 TLB supports variable sized pages, based on a fragment
547 * field in the PTE. When this field is set to a non-zero value, page
548 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
549 * flags are considered valid for all PTEs within the fragment range
550 * and corresponding mappings are assumed to be physically contiguous.
551 *
552 * The L1 TLB can store a single PTE for the whole fragment,
553 * significantly increasing the space available for translation
554 * caching. This leads to large improvements in throughput when the
555 * TLB is under pressure.
556 *
557 * The L2 TLB distributes small and large fragments into two
558 * asymmetric partitions. The large fragment cache is significantly
559 * larger. Thus, we try to use large fragments wherever possible.
560 * Userspace can support this by aligning virtual base address and
561 * allocation size to the fragment size.
562 */
563
564 /* SI and newer are optimized for 64KB */
565 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
566 uint64_t frag_align = 0x80;
567
568 uint64_t frag_start = ALIGN(pe_start, frag_align);
569 uint64_t frag_end = pe_end & ~(frag_align - 1);
570
571 unsigned count;
572
573 /* system pages are non continuously */
574 if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
575 (frag_start >= frag_end)) {
576
577 count = (pe_end - pe_start) / 8;
578 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
579 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
580 return;
581 }
582
583 /* handle the 4K area at the beginning */
584 if (pe_start != frag_start) {
585 count = (frag_start - pe_start) / 8;
586 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
587 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
588 addr += AMDGPU_GPU_PAGE_SIZE * count;
589 }
590
591 /* handle the area in the middle */
592 count = (frag_end - frag_start) / 8;
593 amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
594 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
595 gtt_flags);
596
597 /* handle the 4K area at the end */
598 if (frag_end != pe_end) {
599 addr += AMDGPU_GPU_PAGE_SIZE * count;
600 count = (pe_end - frag_end) / 8;
601 amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
602 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
603 }
604 }
605
606 /**
607 * amdgpu_vm_update_ptes - make sure that page tables are valid
608 *
609 * @adev: amdgpu_device pointer
610 * @vm: requested vm
611 * @start: start of GPU address range
612 * @end: end of GPU address range
613 * @dst: destination address to map to
614 * @flags: mapping flags
615 *
616 * Update the page tables in the range @start - @end (cayman+).
617 *
618 * Global and local mutex must be locked!
619 */
620 static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
621 struct amdgpu_vm *vm,
622 struct amdgpu_ib *ib,
623 uint64_t start, uint64_t end,
624 uint64_t dst, uint32_t flags,
625 uint32_t gtt_flags)
626 {
627 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
628 uint64_t last_pte = ~0, last_dst = ~0;
629 void *owner = AMDGPU_FENCE_OWNER_VM;
630 unsigned count = 0;
631 uint64_t addr;
632
633 /* sync to everything on unmapping */
634 if (!(flags & AMDGPU_PTE_VALID))
635 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
636
637 /* walk over the address space and update the page tables */
638 for (addr = start; addr < end; ) {
639 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
640 struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
641 unsigned nptes;
642 uint64_t pte;
643 int r;
644
645 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner);
646 r = reservation_object_reserve_shared(pt->tbo.resv);
647 if (r)
648 return r;
649
650 if ((addr & ~mask) == (end & ~mask))
651 nptes = end - addr;
652 else
653 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
654
655 pte = amdgpu_bo_gpu_offset(pt);
656 pte += (addr & mask) * 8;
657
658 if ((last_pte + 8 * count) != pte) {
659
660 if (count) {
661 amdgpu_vm_frag_ptes(adev, ib, last_pte,
662 last_pte + 8 * count,
663 last_dst, flags,
664 gtt_flags);
665 }
666
667 count = nptes;
668 last_pte = pte;
669 last_dst = dst;
670 } else {
671 count += nptes;
672 }
673
674 addr += nptes;
675 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
676 }
677
678 if (count) {
679 amdgpu_vm_frag_ptes(adev, ib, last_pte,
680 last_pte + 8 * count,
681 last_dst, flags, gtt_flags);
682 }
683
684 return 0;
685 }
686
687 /**
688 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
689 *
690 * @adev: amdgpu_device pointer
691 * @vm: requested vm
692 * @mapping: mapped range and flags to use for the update
693 * @addr: addr to set the area to
694 * @gtt_flags: flags as they are used for GTT
695 * @fence: optional resulting fence
696 *
697 * Fill in the page table entries for @mapping.
698 * Returns 0 for success, -EINVAL for failure.
699 *
700 * Object have to be reserved and mutex must be locked!
701 */
702 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
703 struct amdgpu_vm *vm,
704 struct amdgpu_bo_va_mapping *mapping,
705 uint64_t addr, uint32_t gtt_flags,
706 struct fence **fence)
707 {
708 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
709 unsigned nptes, ncmds, ndw;
710 uint32_t flags = gtt_flags;
711 struct amdgpu_ib *ib;
712 struct fence *f = NULL;
713 int r;
714
715 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
716 * but in case of something, we filter the flags in first place
717 */
718 if (!(mapping->flags & AMDGPU_PTE_READABLE))
719 flags &= ~AMDGPU_PTE_READABLE;
720 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
721 flags &= ~AMDGPU_PTE_WRITEABLE;
722
723 trace_amdgpu_vm_bo_update(mapping);
724
725 nptes = mapping->it.last - mapping->it.start + 1;
726
727 /*
728 * reserve space for one command every (1 << BLOCK_SIZE)
729 * entries or 2k dwords (whatever is smaller)
730 */
731 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
732
733 /* padding, etc. */
734 ndw = 64;
735
736 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
737 /* only copy commands needed */
738 ndw += ncmds * 7;
739
740 } else if (flags & AMDGPU_PTE_SYSTEM) {
741 /* header for write data commands */
742 ndw += ncmds * 4;
743
744 /* body of write data command */
745 ndw += nptes * 2;
746
747 } else {
748 /* set page commands needed */
749 ndw += ncmds * 10;
750
751 /* two extra commands for begin/end of fragment */
752 ndw += 2 * 10;
753 }
754
755 /* update too big for an IB */
756 if (ndw > 0xfffff)
757 return -ENOMEM;
758
759 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
760 if (!ib)
761 return -ENOMEM;
762
763 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
764 if (r) {
765 kfree(ib);
766 return r;
767 }
768
769 ib->length_dw = 0;
770
771 r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start,
772 mapping->it.last + 1, addr + mapping->offset,
773 flags, gtt_flags);
774
775 if (r) {
776 amdgpu_ib_free(adev, ib);
777 kfree(ib);
778 return r;
779 }
780
781 amdgpu_vm_pad_ib(adev, ib);
782 WARN_ON(ib->length_dw > ndw);
783 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
784 &amdgpu_vm_free_job,
785 AMDGPU_FENCE_OWNER_VM,
786 &f);
787 if (r)
788 goto error_free;
789
790 amdgpu_bo_fence(vm->page_directory, f, true);
791 if (fence) {
792 fence_put(*fence);
793 *fence = fence_get(f);
794 }
795 fence_put(f);
796 if (!amdgpu_enable_scheduler) {
797 amdgpu_ib_free(adev, ib);
798 kfree(ib);
799 }
800 return 0;
801
802 error_free:
803 amdgpu_ib_free(adev, ib);
804 kfree(ib);
805 return r;
806 }
807
808 /**
809 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
810 *
811 * @adev: amdgpu_device pointer
812 * @bo_va: requested BO and VM object
813 * @mem: ttm mem
814 *
815 * Fill in the page table entries for @bo_va.
816 * Returns 0 for success, -EINVAL for failure.
817 *
818 * Object have to be reserved and mutex must be locked!
819 */
820 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
821 struct amdgpu_bo_va *bo_va,
822 struct ttm_mem_reg *mem)
823 {
824 struct amdgpu_vm *vm = bo_va->vm;
825 struct amdgpu_bo_va_mapping *mapping;
826 uint32_t flags;
827 uint64_t addr;
828 int r;
829
830 if (mem) {
831 addr = (u64)mem->start << PAGE_SHIFT;
832 if (mem->mem_type != TTM_PL_TT)
833 addr += adev->vm_manager.vram_base_offset;
834 } else {
835 addr = 0;
836 }
837
838 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
839
840 spin_lock(&vm->status_lock);
841 if (!list_empty(&bo_va->vm_status))
842 list_splice_init(&bo_va->valids, &bo_va->invalids);
843 spin_unlock(&vm->status_lock);
844
845 list_for_each_entry(mapping, &bo_va->invalids, list) {
846 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
847 flags, &bo_va->last_pt_update);
848 if (r)
849 return r;
850 }
851
852 if (trace_amdgpu_vm_bo_mapping_enabled()) {
853 list_for_each_entry(mapping, &bo_va->valids, list)
854 trace_amdgpu_vm_bo_mapping(mapping);
855
856 list_for_each_entry(mapping, &bo_va->invalids, list)
857 trace_amdgpu_vm_bo_mapping(mapping);
858 }
859
860 spin_lock(&vm->status_lock);
861 list_splice_init(&bo_va->invalids, &bo_va->valids);
862 list_del_init(&bo_va->vm_status);
863 if (!mem)
864 list_add(&bo_va->vm_status, &vm->cleared);
865 spin_unlock(&vm->status_lock);
866
867 return 0;
868 }
869
870 /**
871 * amdgpu_vm_clear_freed - clear freed BOs in the PT
872 *
873 * @adev: amdgpu_device pointer
874 * @vm: requested vm
875 *
876 * Make sure all freed BOs are cleared in the PT.
877 * Returns 0 for success.
878 *
879 * PTs have to be reserved and mutex must be locked!
880 */
881 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
882 struct amdgpu_vm *vm)
883 {
884 struct amdgpu_bo_va_mapping *mapping;
885 int r;
886
887 while (!list_empty(&vm->freed)) {
888 mapping = list_first_entry(&vm->freed,
889 struct amdgpu_bo_va_mapping, list);
890 list_del(&mapping->list);
891
892 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
893 kfree(mapping);
894 if (r)
895 return r;
896
897 }
898 return 0;
899
900 }
901
902 /**
903 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
904 *
905 * @adev: amdgpu_device pointer
906 * @vm: requested vm
907 *
908 * Make sure all invalidated BOs are cleared in the PT.
909 * Returns 0 for success.
910 *
911 * PTs have to be reserved and mutex must be locked!
912 */
913 int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
914 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
915 {
916 struct amdgpu_bo_va *bo_va = NULL;
917 int r = 0;
918
919 spin_lock(&vm->status_lock);
920 while (!list_empty(&vm->invalidated)) {
921 bo_va = list_first_entry(&vm->invalidated,
922 struct amdgpu_bo_va, vm_status);
923 spin_unlock(&vm->status_lock);
924
925 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
926 if (r)
927 return r;
928
929 spin_lock(&vm->status_lock);
930 }
931 spin_unlock(&vm->status_lock);
932
933 if (bo_va)
934 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
935
936 return r;
937 }
938
939 /**
940 * amdgpu_vm_bo_add - add a bo to a specific vm
941 *
942 * @adev: amdgpu_device pointer
943 * @vm: requested vm
944 * @bo: amdgpu buffer object
945 *
946 * Add @bo into the requested vm (cayman+).
947 * Add @bo to the list of bos associated with the vm
948 * Returns newly added bo_va or NULL for failure
949 *
950 * Object has to be reserved!
951 */
952 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
953 struct amdgpu_vm *vm,
954 struct amdgpu_bo *bo)
955 {
956 struct amdgpu_bo_va *bo_va;
957
958 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
959 if (bo_va == NULL) {
960 return NULL;
961 }
962 bo_va->vm = vm;
963 bo_va->bo = bo;
964 bo_va->ref_count = 1;
965 INIT_LIST_HEAD(&bo_va->bo_list);
966 INIT_LIST_HEAD(&bo_va->valids);
967 INIT_LIST_HEAD(&bo_va->invalids);
968 INIT_LIST_HEAD(&bo_va->vm_status);
969
970 list_add_tail(&bo_va->bo_list, &bo->va);
971
972 return bo_va;
973 }
974
975 /**
976 * amdgpu_vm_bo_map - map bo inside a vm
977 *
978 * @adev: amdgpu_device pointer
979 * @bo_va: bo_va to store the address
980 * @saddr: where to map the BO
981 * @offset: requested offset in the BO
982 * @flags: attributes of pages (read/write/valid/etc.)
983 *
984 * Add a mapping of the BO at the specefied addr into the VM.
985 * Returns 0 for success, error for failure.
986 *
987 * Object has to be reserved and gets unreserved by this function!
988 */
989 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
990 struct amdgpu_bo_va *bo_va,
991 uint64_t saddr, uint64_t offset,
992 uint64_t size, uint32_t flags)
993 {
994 struct amdgpu_bo_va_mapping *mapping;
995 struct amdgpu_vm *vm = bo_va->vm;
996 struct interval_tree_node *it;
997 unsigned last_pfn, pt_idx;
998 uint64_t eaddr;
999 int r;
1000
1001 /* validate the parameters */
1002 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1003 size == 0 || size & AMDGPU_GPU_PAGE_MASK) {
1004 amdgpu_bo_unreserve(bo_va->bo);
1005 return -EINVAL;
1006 }
1007
1008 /* make sure object fit at this offset */
1009 eaddr = saddr + size;
1010 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) {
1011 amdgpu_bo_unreserve(bo_va->bo);
1012 return -EINVAL;
1013 }
1014
1015 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
1016 if (last_pfn > adev->vm_manager.max_pfn) {
1017 dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n",
1018 last_pfn, adev->vm_manager.max_pfn);
1019 amdgpu_bo_unreserve(bo_va->bo);
1020 return -EINVAL;
1021 }
1022
1023 saddr /= AMDGPU_GPU_PAGE_SIZE;
1024 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1025
1026 it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1);
1027 if (it) {
1028 struct amdgpu_bo_va_mapping *tmp;
1029 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1030 /* bo and tmp overlap, invalid addr */
1031 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1032 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1033 tmp->it.start, tmp->it.last + 1);
1034 amdgpu_bo_unreserve(bo_va->bo);
1035 r = -EINVAL;
1036 goto error;
1037 }
1038
1039 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1040 if (!mapping) {
1041 amdgpu_bo_unreserve(bo_va->bo);
1042 r = -ENOMEM;
1043 goto error;
1044 }
1045
1046 INIT_LIST_HEAD(&mapping->list);
1047 mapping->it.start = saddr;
1048 mapping->it.last = eaddr - 1;
1049 mapping->offset = offset;
1050 mapping->flags = flags;
1051
1052 list_add(&mapping->list, &bo_va->invalids);
1053 interval_tree_insert(&mapping->it, &vm->va);
1054 trace_amdgpu_vm_bo_map(bo_va, mapping);
1055
1056 /* Make sure the page tables are allocated */
1057 saddr >>= amdgpu_vm_block_size;
1058 eaddr >>= amdgpu_vm_block_size;
1059
1060 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1061
1062 if (eaddr > vm->max_pde_used)
1063 vm->max_pde_used = eaddr;
1064
1065 amdgpu_bo_unreserve(bo_va->bo);
1066
1067 /* walk over the address space and allocate the page tables */
1068 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
1069 struct reservation_object *resv = vm->page_directory->tbo.resv;
1070 struct amdgpu_bo *pt;
1071
1072 if (vm->page_tables[pt_idx].bo)
1073 continue;
1074
1075 ww_mutex_lock(&resv->lock, NULL);
1076 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1077 AMDGPU_GPU_PAGE_SIZE, true,
1078 AMDGPU_GEM_DOMAIN_VRAM,
1079 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
1080 NULL, resv, &pt);
1081 ww_mutex_unlock(&resv->lock);
1082 if (r)
1083 goto error_free;
1084
1085 r = amdgpu_vm_clear_bo(adev, pt);
1086 if (r) {
1087 amdgpu_bo_unref(&pt);
1088 goto error_free;
1089 }
1090
1091 vm->page_tables[pt_idx].addr = 0;
1092 vm->page_tables[pt_idx].bo = pt;
1093 }
1094
1095 return 0;
1096
1097 error_free:
1098 list_del(&mapping->list);
1099 interval_tree_remove(&mapping->it, &vm->va);
1100 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1101 kfree(mapping);
1102
1103 error:
1104 return r;
1105 }
1106
1107 /**
1108 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1109 *
1110 * @adev: amdgpu_device pointer
1111 * @bo_va: bo_va to remove the address from
1112 * @saddr: where to the BO is mapped
1113 *
1114 * Remove a mapping of the BO at the specefied addr from the VM.
1115 * Returns 0 for success, error for failure.
1116 *
1117 * Object has to be reserved and gets unreserved by this function!
1118 */
1119 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1120 struct amdgpu_bo_va *bo_va,
1121 uint64_t saddr)
1122 {
1123 struct amdgpu_bo_va_mapping *mapping;
1124 struct amdgpu_vm *vm = bo_va->vm;
1125 bool valid = true;
1126
1127 saddr /= AMDGPU_GPU_PAGE_SIZE;
1128
1129 list_for_each_entry(mapping, &bo_va->valids, list) {
1130 if (mapping->it.start == saddr)
1131 break;
1132 }
1133
1134 if (&mapping->list == &bo_va->valids) {
1135 valid = false;
1136
1137 list_for_each_entry(mapping, &bo_va->invalids, list) {
1138 if (mapping->it.start == saddr)
1139 break;
1140 }
1141
1142 if (&mapping->list == &bo_va->invalids) {
1143 amdgpu_bo_unreserve(bo_va->bo);
1144 return -ENOENT;
1145 }
1146 }
1147
1148 list_del(&mapping->list);
1149 interval_tree_remove(&mapping->it, &vm->va);
1150 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1151
1152 if (valid)
1153 list_add(&mapping->list, &vm->freed);
1154 else
1155 kfree(mapping);
1156 amdgpu_bo_unreserve(bo_va->bo);
1157
1158 return 0;
1159 }
1160
1161 /**
1162 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1163 *
1164 * @adev: amdgpu_device pointer
1165 * @bo_va: requested bo_va
1166 *
1167 * Remove @bo_va->bo from the requested vm (cayman+).
1168 *
1169 * Object have to be reserved!
1170 */
1171 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1172 struct amdgpu_bo_va *bo_va)
1173 {
1174 struct amdgpu_bo_va_mapping *mapping, *next;
1175 struct amdgpu_vm *vm = bo_va->vm;
1176
1177 list_del(&bo_va->bo_list);
1178
1179 spin_lock(&vm->status_lock);
1180 list_del(&bo_va->vm_status);
1181 spin_unlock(&vm->status_lock);
1182
1183 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
1184 list_del(&mapping->list);
1185 interval_tree_remove(&mapping->it, &vm->va);
1186 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1187 list_add(&mapping->list, &vm->freed);
1188 }
1189 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1190 list_del(&mapping->list);
1191 interval_tree_remove(&mapping->it, &vm->va);
1192 kfree(mapping);
1193 }
1194
1195 fence_put(bo_va->last_pt_update);
1196 kfree(bo_va);
1197 }
1198
1199 /**
1200 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1201 *
1202 * @adev: amdgpu_device pointer
1203 * @vm: requested vm
1204 * @bo: amdgpu buffer object
1205 *
1206 * Mark @bo as invalid (cayman+).
1207 */
1208 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1209 struct amdgpu_bo *bo)
1210 {
1211 struct amdgpu_bo_va *bo_va;
1212
1213 list_for_each_entry(bo_va, &bo->va, bo_list) {
1214 spin_lock(&bo_va->vm->status_lock);
1215 if (list_empty(&bo_va->vm_status))
1216 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
1217 spin_unlock(&bo_va->vm->status_lock);
1218 }
1219 }
1220
1221 /**
1222 * amdgpu_vm_init - initialize a vm instance
1223 *
1224 * @adev: amdgpu_device pointer
1225 * @vm: requested vm
1226 *
1227 * Init @vm fields (cayman+).
1228 */
1229 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1230 {
1231 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1232 AMDGPU_VM_PTE_COUNT * 8);
1233 unsigned pd_size, pd_entries, pts_size;
1234 int i, r;
1235
1236 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1237 vm->ids[i].id = 0;
1238 vm->ids[i].flushed_updates = NULL;
1239 vm->ids[i].last_id_use = NULL;
1240 }
1241 mutex_init(&vm->mutex);
1242 vm->va = RB_ROOT;
1243 spin_lock_init(&vm->status_lock);
1244 INIT_LIST_HEAD(&vm->invalidated);
1245 INIT_LIST_HEAD(&vm->cleared);
1246 INIT_LIST_HEAD(&vm->freed);
1247
1248 pd_size = amdgpu_vm_directory_size(adev);
1249 pd_entries = amdgpu_vm_num_pdes(adev);
1250
1251 /* allocate page table array */
1252 pts_size = pd_entries * sizeof(struct amdgpu_vm_pt);
1253 vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
1254 if (vm->page_tables == NULL) {
1255 DRM_ERROR("Cannot allocate memory for page table array\n");
1256 return -ENOMEM;
1257 }
1258
1259 vm->page_directory_fence = NULL;
1260
1261 r = amdgpu_bo_create(adev, pd_size, align, true,
1262 AMDGPU_GEM_DOMAIN_VRAM,
1263 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
1264 NULL, NULL, &vm->page_directory);
1265 if (r)
1266 return r;
1267
1268 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
1269 if (r) {
1270 amdgpu_bo_unref(&vm->page_directory);
1271 vm->page_directory = NULL;
1272 return r;
1273 }
1274
1275 return 0;
1276 }
1277
1278 /**
1279 * amdgpu_vm_fini - tear down a vm instance
1280 *
1281 * @adev: amdgpu_device pointer
1282 * @vm: requested vm
1283 *
1284 * Tear down @vm (cayman+).
1285 * Unbind the VM and remove all bos from the vm bo list
1286 */
1287 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1288 {
1289 struct amdgpu_bo_va_mapping *mapping, *tmp;
1290 int i;
1291
1292 if (!RB_EMPTY_ROOT(&vm->va)) {
1293 dev_err(adev->dev, "still active bo inside vm\n");
1294 }
1295 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1296 list_del(&mapping->list);
1297 interval_tree_remove(&mapping->it, &vm->va);
1298 kfree(mapping);
1299 }
1300 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1301 list_del(&mapping->list);
1302 kfree(mapping);
1303 }
1304
1305 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
1306 amdgpu_bo_unref(&vm->page_tables[i].bo);
1307 kfree(vm->page_tables);
1308
1309 amdgpu_bo_unref(&vm->page_directory);
1310 fence_put(vm->page_directory_fence);
1311
1312 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1313 fence_put(vm->ids[i].flushed_updates);
1314 amdgpu_fence_unref(&vm->ids[i].last_id_use);
1315 }
1316
1317 mutex_destroy(&vm->mutex);
1318 }
This page took 0.060728 seconds and 4 git commands to generate.