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/radeon_drm.h>
30 #include "radeon.h"
31 #include "radeon_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 * radeon_vm_num_pde - return the number of page directory entries
55 *
56 * @rdev: radeon_device pointer
57 *
58 * Calculate the number of page directory entries (cayman+).
59 */
radeon_vm_num_pdes(struct radeon_device * rdev)60 static unsigned radeon_vm_num_pdes(struct radeon_device *rdev)
61 {
62 return rdev->vm_manager.max_pfn >> radeon_vm_block_size;
63 }
64
65 /**
66 * radeon_vm_directory_size - returns the size of the page directory in bytes
67 *
68 * @rdev: radeon_device pointer
69 *
70 * Calculate the size of the page directory in bytes (cayman+).
71 */
radeon_vm_directory_size(struct radeon_device * rdev)72 static unsigned radeon_vm_directory_size(struct radeon_device *rdev)
73 {
74 return RADEON_GPU_PAGE_ALIGN(radeon_vm_num_pdes(rdev) * 8);
75 }
76
77 /**
78 * radeon_vm_manager_init - init the vm manager
79 *
80 * @rdev: radeon_device pointer
81 *
82 * Init the vm manager (cayman+).
83 * Returns 0 for success, error for failure.
84 */
radeon_vm_manager_init(struct radeon_device * rdev)85 int radeon_vm_manager_init(struct radeon_device *rdev)
86 {
87 int r;
88
89 if (!rdev->vm_manager.enabled) {
90 r = radeon_asic_vm_init(rdev);
91 if (r)
92 return r;
93
94 rdev->vm_manager.enabled = true;
95 }
96 return 0;
97 }
98
99 /**
100 * radeon_vm_manager_fini - tear down the vm manager
101 *
102 * @rdev: radeon_device pointer
103 *
104 * Tear down the VM manager (cayman+).
105 */
radeon_vm_manager_fini(struct radeon_device * rdev)106 void radeon_vm_manager_fini(struct radeon_device *rdev)
107 {
108 int i;
109
110 if (!rdev->vm_manager.enabled)
111 return;
112
113 for (i = 0; i < RADEON_NUM_VM; ++i)
114 radeon_fence_unref(&rdev->vm_manager.active[i]);
115 radeon_asic_vm_fini(rdev);
116 rdev->vm_manager.enabled = false;
117 }
118
119 /**
120 * radeon_vm_get_bos - add the vm BOs to a validation list
121 *
122 * @vm: vm providing the BOs
123 * @head: head of validation list
124 *
125 * Add the page directory to the list of BOs to
126 * validate for command submission (cayman+).
127 */
radeon_vm_get_bos(struct radeon_device * rdev,struct radeon_vm * vm,struct list_head * head)128 struct radeon_cs_reloc *radeon_vm_get_bos(struct radeon_device *rdev,
129 struct radeon_vm *vm,
130 struct list_head *head)
131 {
132 struct radeon_cs_reloc *list;
133 unsigned i, idx;
134
135 list = drm_malloc_ab(vm->max_pde_used + 2,
136 sizeof(struct radeon_cs_reloc));
137 if (!list)
138 return NULL;
139
140 /* add the vm page table to the list */
141 list[0].gobj = NULL;
142 list[0].robj = vm->page_directory;
143 list[0].prefered_domains = RADEON_GEM_DOMAIN_VRAM;
144 list[0].allowed_domains = RADEON_GEM_DOMAIN_VRAM;
145 list[0].tv.bo = &vm->page_directory->tbo;
146 list[0].tv.shared = true;
147 list[0].tiling_flags = 0;
148 list[0].handle = 0;
149 list_add(&list[0].tv.head, head);
150
151 for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
152 if (!vm->page_tables[i].bo)
153 continue;
154
155 list[idx].gobj = NULL;
156 list[idx].robj = vm->page_tables[i].bo;
157 list[idx].prefered_domains = RADEON_GEM_DOMAIN_VRAM;
158 list[idx].allowed_domains = RADEON_GEM_DOMAIN_VRAM;
159 list[idx].tv.bo = &list[idx].robj->tbo;
160 list[idx].tv.shared = true;
161 list[idx].tiling_flags = 0;
162 list[idx].handle = 0;
163 list_add(&list[idx++].tv.head, head);
164 }
165
166 return list;
167 }
168
169 /**
170 * radeon_vm_grab_id - allocate the next free VMID
171 *
172 * @rdev: radeon_device pointer
173 * @vm: vm to allocate id for
174 * @ring: ring we want to submit job to
175 *
176 * Allocate an id for the vm (cayman+).
177 * Returns the fence we need to sync to (if any).
178 *
179 * Global and local mutex must be locked!
180 */
radeon_vm_grab_id(struct radeon_device * rdev,struct radeon_vm * vm,int ring)181 struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
182 struct radeon_vm *vm, int ring)
183 {
184 struct radeon_fence *best[RADEON_NUM_RINGS] = {};
185 unsigned choices[2] = {};
186 unsigned i;
187
188 /* check if the id is still valid */
189 if (vm->last_id_use && vm->last_id_use == rdev->vm_manager.active[vm->id])
190 return NULL;
191
192 /* we definately need to flush */
193 radeon_fence_unref(&vm->last_flush);
194
195 /* skip over VMID 0, since it is the system VM */
196 for (i = 1; i < rdev->vm_manager.nvm; ++i) {
197 struct radeon_fence *fence = rdev->vm_manager.active[i];
198
199 if (fence == NULL) {
200 /* found a free one */
201 vm->id = i;
202 trace_radeon_vm_grab_id(vm->id, ring);
203 return NULL;
204 }
205
206 if (radeon_fence_is_earlier(fence, best[fence->ring])) {
207 best[fence->ring] = fence;
208 choices[fence->ring == ring ? 0 : 1] = i;
209 }
210 }
211
212 for (i = 0; i < 2; ++i) {
213 if (choices[i]) {
214 vm->id = choices[i];
215 trace_radeon_vm_grab_id(vm->id, ring);
216 return rdev->vm_manager.active[choices[i]];
217 }
218 }
219
220 /* should never happen */
221 BUG();
222 return NULL;
223 }
224
225 /**
226 * radeon_vm_flush - hardware flush the vm
227 *
228 * @rdev: radeon_device pointer
229 * @vm: vm we want to flush
230 * @ring: ring to use for flush
231 *
232 * Flush the vm (cayman+).
233 *
234 * Global and local mutex must be locked!
235 */
radeon_vm_flush(struct radeon_device * rdev,struct radeon_vm * vm,int ring)236 void radeon_vm_flush(struct radeon_device *rdev,
237 struct radeon_vm *vm,
238 int ring)
239 {
240 uint64_t pd_addr = radeon_bo_gpu_offset(vm->page_directory);
241
242 /* if we can't remember our last VM flush then flush now! */
243 if (!vm->last_flush || pd_addr != vm->pd_gpu_addr) {
244 trace_radeon_vm_flush(pd_addr, ring, vm->id);
245 vm->pd_gpu_addr = pd_addr;
246 radeon_ring_vm_flush(rdev, ring, vm);
247 }
248 }
249
250 /**
251 * radeon_vm_fence - remember fence for vm
252 *
253 * @rdev: radeon_device pointer
254 * @vm: vm we want to fence
255 * @fence: fence to remember
256 *
257 * Fence the vm (cayman+).
258 * Set the fence used to protect page table and id.
259 *
260 * Global and local mutex must be locked!
261 */
radeon_vm_fence(struct radeon_device * rdev,struct radeon_vm * vm,struct radeon_fence * fence)262 void radeon_vm_fence(struct radeon_device *rdev,
263 struct radeon_vm *vm,
264 struct radeon_fence *fence)
265 {
266 radeon_fence_unref(&vm->fence);
267 vm->fence = radeon_fence_ref(fence);
268
269 radeon_fence_unref(&rdev->vm_manager.active[vm->id]);
270 rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence);
271
272 radeon_fence_unref(&vm->last_id_use);
273 vm->last_id_use = radeon_fence_ref(fence);
274
275 /* we just flushed the VM, remember that */
276 if (!vm->last_flush)
277 vm->last_flush = radeon_fence_ref(fence);
278 }
279
280 /**
281 * radeon_vm_bo_find - find the bo_va for a specific vm & bo
282 *
283 * @vm: requested vm
284 * @bo: requested buffer object
285 *
286 * Find @bo inside the requested vm (cayman+).
287 * Search inside the @bos vm list for the requested vm
288 * Returns the found bo_va or NULL if none is found
289 *
290 * Object has to be reserved!
291 */
radeon_vm_bo_find(struct radeon_vm * vm,struct radeon_bo * bo)292 struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
293 struct radeon_bo *bo)
294 {
295 struct radeon_bo_va *bo_va;
296
297 list_for_each_entry(bo_va, &bo->va, bo_list) {
298 if (bo_va->vm == vm) {
299 return bo_va;
300 }
301 }
302 return NULL;
303 }
304
305 /**
306 * radeon_vm_bo_add - add a bo to a specific vm
307 *
308 * @rdev: radeon_device pointer
309 * @vm: requested vm
310 * @bo: radeon buffer object
311 *
312 * Add @bo into the requested vm (cayman+).
313 * Add @bo to the list of bos associated with the vm
314 * Returns newly added bo_va or NULL for failure
315 *
316 * Object has to be reserved!
317 */
radeon_vm_bo_add(struct radeon_device * rdev,struct radeon_vm * vm,struct radeon_bo * bo)318 struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
319 struct radeon_vm *vm,
320 struct radeon_bo *bo)
321 {
322 struct radeon_bo_va *bo_va;
323
324 bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
325 if (bo_va == NULL) {
326 return NULL;
327 }
328 bo_va->vm = vm;
329 bo_va->bo = bo;
330 bo_va->it.start = 0;
331 bo_va->it.last = 0;
332 bo_va->flags = 0;
333 bo_va->addr = 0;
334 bo_va->ref_count = 1;
335 INIT_LIST_HEAD(&bo_va->bo_list);
336 INIT_LIST_HEAD(&bo_va->vm_status);
337
338 mutex_lock(&vm->mutex);
339 list_add_tail(&bo_va->bo_list, &bo->va);
340 mutex_unlock(&vm->mutex);
341
342 return bo_va;
343 }
344
345 /**
346 * radeon_vm_set_pages - helper to call the right asic function
347 *
348 * @rdev: radeon_device pointer
349 * @ib: indirect buffer to fill with commands
350 * @pe: addr of the page entry
351 * @addr: dst addr to write into pe
352 * @count: number of page entries to update
353 * @incr: increase next addr by incr bytes
354 * @flags: hw access flags
355 *
356 * Traces the parameters and calls the right asic functions
357 * to setup the page table using the DMA.
358 */
radeon_vm_set_pages(struct radeon_device * rdev,struct radeon_ib * ib,uint64_t pe,uint64_t addr,unsigned count,uint32_t incr,uint32_t flags)359 static void radeon_vm_set_pages(struct radeon_device *rdev,
360 struct radeon_ib *ib,
361 uint64_t pe,
362 uint64_t addr, unsigned count,
363 uint32_t incr, uint32_t flags)
364 {
365 trace_radeon_vm_set_page(pe, addr, count, incr, flags);
366
367 if ((flags & R600_PTE_GART_MASK) == R600_PTE_GART_MASK) {
368 uint64_t src = rdev->gart.table_addr + (addr >> 12) * 8;
369 radeon_asic_vm_copy_pages(rdev, ib, pe, src, count);
370
371 } else if ((flags & R600_PTE_SYSTEM) || (count < 3)) {
372 radeon_asic_vm_write_pages(rdev, ib, pe, addr,
373 count, incr, flags);
374
375 } else {
376 radeon_asic_vm_set_pages(rdev, ib, pe, addr,
377 count, incr, flags);
378 }
379 }
380
381 /**
382 * radeon_vm_clear_bo - initially clear the page dir/table
383 *
384 * @rdev: radeon_device pointer
385 * @bo: bo to clear
386 */
radeon_vm_clear_bo(struct radeon_device * rdev,struct radeon_bo * bo)387 static int radeon_vm_clear_bo(struct radeon_device *rdev,
388 struct radeon_bo *bo)
389 {
390 struct radeon_ib ib;
391 unsigned entries;
392 uint64_t addr;
393 int r;
394
395 r = radeon_bo_reserve(bo, false);
396 if (r)
397 return r;
398
399 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
400 if (r)
401 goto error_unreserve;
402
403 addr = radeon_bo_gpu_offset(bo);
404 entries = radeon_bo_size(bo) / 8;
405
406 r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, 256);
407 if (r)
408 goto error_unreserve;
409
410 ib.length_dw = 0;
411
412 radeon_vm_set_pages(rdev, &ib, addr, 0, entries, 0, 0);
413 radeon_asic_vm_pad_ib(rdev, &ib);
414 WARN_ON(ib.length_dw > 64);
415
416 r = radeon_ib_schedule(rdev, &ib, NULL, false);
417 if (r)
418 goto error_free;
419
420 radeon_bo_fence(bo, ib.fence, false);
421
422 error_free:
423 radeon_ib_free(rdev, &ib);
424
425 error_unreserve:
426 radeon_bo_unreserve(bo);
427 return r;
428 }
429
430 /**
431 * radeon_vm_bo_set_addr - set bos virtual address inside a vm
432 *
433 * @rdev: radeon_device pointer
434 * @bo_va: bo_va to store the address
435 * @soffset: requested offset of the buffer in the VM address space
436 * @flags: attributes of pages (read/write/valid/etc.)
437 *
438 * Set offset of @bo_va (cayman+).
439 * Validate and set the offset requested within the vm address space.
440 * Returns 0 for success, error for failure.
441 *
442 * Object has to be reserved!
443 */
radeon_vm_bo_set_addr(struct radeon_device * rdev,struct radeon_bo_va * bo_va,uint64_t soffset,uint32_t flags)444 int radeon_vm_bo_set_addr(struct radeon_device *rdev,
445 struct radeon_bo_va *bo_va,
446 uint64_t soffset,
447 uint32_t flags)
448 {
449 uint64_t size = radeon_bo_size(bo_va->bo);
450 struct radeon_vm *vm = bo_va->vm;
451 unsigned last_pfn, pt_idx;
452 uint64_t eoffset;
453 int r;
454
455 if (soffset) {
456 /* make sure object fit at this offset */
457 eoffset = soffset + size - 1;
458 if (soffset >= eoffset) {
459 return -EINVAL;
460 }
461
462 last_pfn = eoffset / RADEON_GPU_PAGE_SIZE;
463 if (last_pfn >= rdev->vm_manager.max_pfn) {
464 dev_err(rdev->dev, "va above limit (0x%08X >= 0x%08X)\n",
465 last_pfn, rdev->vm_manager.max_pfn);
466 return -EINVAL;
467 }
468
469 } else {
470 eoffset = last_pfn = 0;
471 }
472
473 mutex_lock(&vm->mutex);
474 soffset /= RADEON_GPU_PAGE_SIZE;
475 eoffset /= RADEON_GPU_PAGE_SIZE;
476 if (soffset || eoffset) {
477 struct interval_tree_node *it;
478 it = interval_tree_iter_first(&vm->va, soffset, eoffset);
479 if (it && it != &bo_va->it) {
480 struct radeon_bo_va *tmp;
481 tmp = container_of(it, struct radeon_bo_va, it);
482 /* bo and tmp overlap, invalid offset */
483 dev_err(rdev->dev, "bo %p va 0x%010Lx conflict with "
484 "(bo %p 0x%010lx 0x%010lx)\n", bo_va->bo,
485 soffset, tmp->bo, tmp->it.start, tmp->it.last);
486 mutex_unlock(&vm->mutex);
487 return -EINVAL;
488 }
489 }
490
491 if (bo_va->it.start || bo_va->it.last) {
492 if (bo_va->addr) {
493 /* add a clone of the bo_va to clear the old address */
494 struct radeon_bo_va *tmp;
495 tmp = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
496 if (!tmp) {
497 mutex_unlock(&vm->mutex);
498 return -ENOMEM;
499 }
500 tmp->it.start = bo_va->it.start;
501 tmp->it.last = bo_va->it.last;
502 tmp->vm = vm;
503 tmp->addr = bo_va->addr;
504 tmp->bo = radeon_bo_ref(bo_va->bo);
505 list_add(&tmp->vm_status, &vm->freed);
506
507 bo_va->addr = 0;
508 }
509
510 interval_tree_remove(&bo_va->it, &vm->va);
511 bo_va->it.start = 0;
512 bo_va->it.last = 0;
513 }
514
515 if (soffset || eoffset) {
516 bo_va->it.start = soffset;
517 bo_va->it.last = eoffset;
518 interval_tree_insert(&bo_va->it, &vm->va);
519 }
520
521 bo_va->flags = flags;
522 bo_va->addr = 0;
523
524 soffset >>= radeon_vm_block_size;
525 eoffset >>= radeon_vm_block_size;
526
527 BUG_ON(eoffset >= radeon_vm_num_pdes(rdev));
528
529 if (eoffset > vm->max_pde_used)
530 vm->max_pde_used = eoffset;
531
532 radeon_bo_unreserve(bo_va->bo);
533
534 /* walk over the address space and allocate the page tables */
535 for (pt_idx = soffset; pt_idx <= eoffset; ++pt_idx) {
536 struct radeon_bo *pt;
537
538 if (vm->page_tables[pt_idx].bo)
539 continue;
540
541 /* drop mutex to allocate and clear page table */
542 mutex_unlock(&vm->mutex);
543
544 r = radeon_bo_create(rdev, RADEON_VM_PTE_COUNT * 8,
545 RADEON_GPU_PAGE_SIZE, true,
546 RADEON_GEM_DOMAIN_VRAM, 0,
547 NULL, NULL, &pt);
548 if (r)
549 return r;
550
551 r = radeon_vm_clear_bo(rdev, pt);
552 if (r) {
553 radeon_bo_unref(&pt);
554 radeon_bo_reserve(bo_va->bo, false);
555 return r;
556 }
557
558 /* aquire mutex again */
559 mutex_lock(&vm->mutex);
560 if (vm->page_tables[pt_idx].bo) {
561 /* someone else allocated the pt in the meantime */
562 mutex_unlock(&vm->mutex);
563 radeon_bo_unref(&pt);
564 mutex_lock(&vm->mutex);
565 continue;
566 }
567
568 vm->page_tables[pt_idx].addr = 0;
569 vm->page_tables[pt_idx].bo = pt;
570 }
571
572 mutex_unlock(&vm->mutex);
573 return radeon_bo_reserve(bo_va->bo, false);
574 }
575
576 /**
577 * radeon_vm_map_gart - get the physical address of a gart page
578 *
579 * @rdev: radeon_device pointer
580 * @addr: the unmapped addr
581 *
582 * Look up the physical address of the page that the pte resolves
583 * to (cayman+).
584 * Returns the physical address of the page.
585 */
radeon_vm_map_gart(struct radeon_device * rdev,uint64_t addr)586 uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr)
587 {
588 uint64_t result;
589
590 /* page table offset */
591 result = rdev->gart.pages_addr[addr >> PAGE_SHIFT];
592
593 /* in case cpu page size != gpu page size*/
594 result |= addr & (~PAGE_MASK);
595
596 return result;
597 }
598
599 /**
600 * radeon_vm_page_flags - translate page flags to what the hw uses
601 *
602 * @flags: flags comming from userspace
603 *
604 * Translate the flags the userspace ABI uses to hw flags.
605 */
radeon_vm_page_flags(uint32_t flags)606 static uint32_t radeon_vm_page_flags(uint32_t flags)
607 {
608 uint32_t hw_flags = 0;
609 hw_flags |= (flags & RADEON_VM_PAGE_VALID) ? R600_PTE_VALID : 0;
610 hw_flags |= (flags & RADEON_VM_PAGE_READABLE) ? R600_PTE_READABLE : 0;
611 hw_flags |= (flags & RADEON_VM_PAGE_WRITEABLE) ? R600_PTE_WRITEABLE : 0;
612 if (flags & RADEON_VM_PAGE_SYSTEM) {
613 hw_flags |= R600_PTE_SYSTEM;
614 hw_flags |= (flags & RADEON_VM_PAGE_SNOOPED) ? R600_PTE_SNOOPED : 0;
615 }
616 return hw_flags;
617 }
618
619 /**
620 * radeon_vm_update_pdes - make sure that page directory is valid
621 *
622 * @rdev: radeon_device pointer
623 * @vm: requested vm
624 * @start: start of GPU address range
625 * @end: end of GPU address range
626 *
627 * Allocates new page tables if necessary
628 * and updates the page directory (cayman+).
629 * Returns 0 for success, error for failure.
630 *
631 * Global and local mutex must be locked!
632 */
radeon_vm_update_page_directory(struct radeon_device * rdev,struct radeon_vm * vm)633 int radeon_vm_update_page_directory(struct radeon_device *rdev,
634 struct radeon_vm *vm)
635 {
636 struct radeon_bo *pd = vm->page_directory;
637 uint64_t pd_addr = radeon_bo_gpu_offset(pd);
638 uint32_t incr = RADEON_VM_PTE_COUNT * 8;
639 uint64_t last_pde = ~0, last_pt = ~0;
640 unsigned count = 0, pt_idx, ndw;
641 struct radeon_ib ib;
642 int r;
643
644 /* padding, etc. */
645 ndw = 64;
646
647 /* assume the worst case */
648 ndw += vm->max_pde_used * 6;
649
650 /* update too big for an IB */
651 if (ndw > 0xfffff)
652 return -ENOMEM;
653
654 r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, ndw * 4);
655 if (r)
656 return r;
657 ib.length_dw = 0;
658
659 /* walk over the address space and update the page directory */
660 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
661 struct radeon_bo *bo = vm->page_tables[pt_idx].bo;
662 uint64_t pde, pt;
663
664 if (bo == NULL)
665 continue;
666
667 pt = radeon_bo_gpu_offset(bo);
668 if (vm->page_tables[pt_idx].addr == pt)
669 continue;
670 vm->page_tables[pt_idx].addr = pt;
671
672 pde = pd_addr + pt_idx * 8;
673 if (((last_pde + 8 * count) != pde) ||
674 ((last_pt + incr * count) != pt)) {
675
676 if (count) {
677 radeon_vm_set_pages(rdev, &ib, last_pde,
678 last_pt, count, incr,
679 R600_PTE_VALID);
680 }
681
682 count = 1;
683 last_pde = pde;
684 last_pt = pt;
685 } else {
686 ++count;
687 }
688 }
689
690 if (count)
691 radeon_vm_set_pages(rdev, &ib, last_pde, last_pt, count,
692 incr, R600_PTE_VALID);
693
694 if (ib.length_dw != 0) {
695 radeon_asic_vm_pad_ib(rdev, &ib);
696
697 radeon_semaphore_sync_resv(rdev, ib.semaphore, pd->tbo.resv, false);
698 radeon_semaphore_sync_fence(ib.semaphore, vm->last_id_use);
699 WARN_ON(ib.length_dw > ndw);
700 r = radeon_ib_schedule(rdev, &ib, NULL, false);
701 if (r) {
702 radeon_ib_free(rdev, &ib);
703 return r;
704 }
705 radeon_bo_fence(pd, ib.fence, false);
706 radeon_fence_unref(&vm->fence);
707 vm->fence = radeon_fence_ref(ib.fence);
708 radeon_fence_unref(&vm->last_flush);
709 }
710 radeon_ib_free(rdev, &ib);
711
712 return 0;
713 }
714
715 /**
716 * radeon_vm_frag_ptes - add fragment information to PTEs
717 *
718 * @rdev: radeon_device pointer
719 * @ib: IB for the update
720 * @pe_start: first PTE to handle
721 * @pe_end: last PTE to handle
722 * @addr: addr those PTEs should point to
723 * @flags: hw mapping flags
724 *
725 * Global and local mutex must be locked!
726 */
radeon_vm_frag_ptes(struct radeon_device * rdev,struct radeon_ib * ib,uint64_t pe_start,uint64_t pe_end,uint64_t addr,uint32_t flags)727 static void radeon_vm_frag_ptes(struct radeon_device *rdev,
728 struct radeon_ib *ib,
729 uint64_t pe_start, uint64_t pe_end,
730 uint64_t addr, uint32_t flags)
731 {
732 /**
733 * The MC L1 TLB supports variable sized pages, based on a fragment
734 * field in the PTE. When this field is set to a non-zero value, page
735 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
736 * flags are considered valid for all PTEs within the fragment range
737 * and corresponding mappings are assumed to be physically contiguous.
738 *
739 * The L1 TLB can store a single PTE for the whole fragment,
740 * significantly increasing the space available for translation
741 * caching. This leads to large improvements in throughput when the
742 * TLB is under pressure.
743 *
744 * The L2 TLB distributes small and large fragments into two
745 * asymmetric partitions. The large fragment cache is significantly
746 * larger. Thus, we try to use large fragments wherever possible.
747 * Userspace can support this by aligning virtual base address and
748 * allocation size to the fragment size.
749 */
750
751 /* NI is optimized for 256KB fragments, SI and newer for 64KB */
752 uint64_t frag_flags = ((rdev->family == CHIP_CAYMAN) ||
753 (rdev->family == CHIP_ARUBA)) ?
754 R600_PTE_FRAG_256KB : R600_PTE_FRAG_64KB;
755 uint64_t frag_align = ((rdev->family == CHIP_CAYMAN) ||
756 (rdev->family == CHIP_ARUBA)) ? 0x200 : 0x80;
757
758 uint64_t frag_start = ALIGN(pe_start, frag_align);
759 uint64_t frag_end = pe_end & ~(frag_align - 1);
760
761 unsigned count;
762
763 /* system pages are non continuously */
764 if ((flags & R600_PTE_SYSTEM) || !(flags & R600_PTE_VALID) ||
765 (frag_start >= frag_end)) {
766
767 count = (pe_end - pe_start) / 8;
768 radeon_vm_set_pages(rdev, ib, pe_start, addr, count,
769 RADEON_GPU_PAGE_SIZE, flags);
770 return;
771 }
772
773 /* handle the 4K area at the beginning */
774 if (pe_start != frag_start) {
775 count = (frag_start - pe_start) / 8;
776 radeon_vm_set_pages(rdev, ib, pe_start, addr, count,
777 RADEON_GPU_PAGE_SIZE, flags);
778 addr += RADEON_GPU_PAGE_SIZE * count;
779 }
780
781 /* handle the area in the middle */
782 count = (frag_end - frag_start) / 8;
783 radeon_vm_set_pages(rdev, ib, frag_start, addr, count,
784 RADEON_GPU_PAGE_SIZE, flags | frag_flags);
785
786 /* handle the 4K area at the end */
787 if (frag_end != pe_end) {
788 addr += RADEON_GPU_PAGE_SIZE * count;
789 count = (pe_end - frag_end) / 8;
790 radeon_vm_set_pages(rdev, ib, frag_end, addr, count,
791 RADEON_GPU_PAGE_SIZE, flags);
792 }
793 }
794
795 /**
796 * radeon_vm_update_ptes - make sure that page tables are valid
797 *
798 * @rdev: radeon_device pointer
799 * @vm: requested vm
800 * @start: start of GPU address range
801 * @end: end of GPU address range
802 * @dst: destination address to map to
803 * @flags: mapping flags
804 *
805 * Update the page tables in the range @start - @end (cayman+).
806 *
807 * Global and local mutex must be locked!
808 */
radeon_vm_update_ptes(struct radeon_device * rdev,struct radeon_vm * vm,struct radeon_ib * ib,uint64_t start,uint64_t end,uint64_t dst,uint32_t flags)809 static void radeon_vm_update_ptes(struct radeon_device *rdev,
810 struct radeon_vm *vm,
811 struct radeon_ib *ib,
812 uint64_t start, uint64_t end,
813 uint64_t dst, uint32_t flags)
814 {
815 uint64_t mask = RADEON_VM_PTE_COUNT - 1;
816 uint64_t last_pte = ~0, last_dst = ~0;
817 unsigned count = 0;
818 uint64_t addr;
819
820 /* walk over the address space and update the page tables */
821 for (addr = start; addr < end; ) {
822 uint64_t pt_idx = addr >> radeon_vm_block_size;
823 struct radeon_bo *pt = vm->page_tables[pt_idx].bo;
824 unsigned nptes;
825 uint64_t pte;
826
827 radeon_semaphore_sync_resv(rdev, ib->semaphore, pt->tbo.resv, false);
828
829 if ((addr & ~mask) == (end & ~mask))
830 nptes = end - addr;
831 else
832 nptes = RADEON_VM_PTE_COUNT - (addr & mask);
833
834 pte = radeon_bo_gpu_offset(pt);
835 pte += (addr & mask) * 8;
836
837 if ((last_pte + 8 * count) != pte) {
838
839 if (count) {
840 radeon_vm_frag_ptes(rdev, ib, last_pte,
841 last_pte + 8 * count,
842 last_dst, flags);
843 }
844
845 count = nptes;
846 last_pte = pte;
847 last_dst = dst;
848 } else {
849 count += nptes;
850 }
851
852 addr += nptes;
853 dst += nptes * RADEON_GPU_PAGE_SIZE;
854 }
855
856 if (count) {
857 radeon_vm_frag_ptes(rdev, ib, last_pte,
858 last_pte + 8 * count,
859 last_dst, flags);
860 }
861 }
862
863 /**
864 * radeon_vm_fence_pts - fence page tables after an update
865 *
866 * @vm: requested vm
867 * @start: start of GPU address range
868 * @end: end of GPU address range
869 * @fence: fence to use
870 *
871 * Fence the page tables in the range @start - @end (cayman+).
872 *
873 * Global and local mutex must be locked!
874 */
radeon_vm_fence_pts(struct radeon_vm * vm,uint64_t start,uint64_t end,struct radeon_fence * fence)875 static void radeon_vm_fence_pts(struct radeon_vm *vm,
876 uint64_t start, uint64_t end,
877 struct radeon_fence *fence)
878 {
879 unsigned i;
880
881 start >>= radeon_vm_block_size;
882 end = (end - 1) >> radeon_vm_block_size;
883
884 for (i = start; i <= end; ++i)
885 radeon_bo_fence(vm->page_tables[i].bo, fence, false);
886 }
887
888 /**
889 * radeon_vm_bo_update - map a bo into the vm page table
890 *
891 * @rdev: radeon_device pointer
892 * @vm: requested vm
893 * @bo: radeon buffer object
894 * @mem: ttm mem
895 *
896 * Fill in the page table entries for @bo (cayman+).
897 * Returns 0 for success, -EINVAL for failure.
898 *
899 * Object have to be reserved and mutex must be locked!
900 */
radeon_vm_bo_update(struct radeon_device * rdev,struct radeon_bo_va * bo_va,struct ttm_mem_reg * mem)901 int radeon_vm_bo_update(struct radeon_device *rdev,
902 struct radeon_bo_va *bo_va,
903 struct ttm_mem_reg *mem)
904 {
905 struct radeon_vm *vm = bo_va->vm;
906 struct radeon_ib ib;
907 unsigned nptes, ncmds, ndw;
908 uint64_t addr;
909 uint32_t flags;
910 int r;
911
912 if (!bo_va->it.start) {
913 dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n",
914 bo_va->bo, vm);
915 return -EINVAL;
916 }
917
918 list_del_init(&bo_va->vm_status);
919
920 bo_va->flags &= ~RADEON_VM_PAGE_VALID;
921 bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
922 bo_va->flags &= ~RADEON_VM_PAGE_SNOOPED;
923 if (bo_va->bo && radeon_ttm_tt_is_readonly(bo_va->bo->tbo.ttm))
924 bo_va->flags &= ~RADEON_VM_PAGE_WRITEABLE;
925
926 if (mem) {
927 addr = mem->start << PAGE_SHIFT;
928 if (mem->mem_type != TTM_PL_SYSTEM) {
929 bo_va->flags |= RADEON_VM_PAGE_VALID;
930 }
931 if (mem->mem_type == TTM_PL_TT) {
932 bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
933 if (!(bo_va->bo->flags & (RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC)))
934 bo_va->flags |= RADEON_VM_PAGE_SNOOPED;
935
936 } else {
937 addr += rdev->vm_manager.vram_base_offset;
938 }
939 } else {
940 addr = 0;
941 }
942
943 if (addr == bo_va->addr)
944 return 0;
945 bo_va->addr = addr;
946
947 trace_radeon_vm_bo_update(bo_va);
948
949 nptes = bo_va->it.last - bo_va->it.start + 1;
950
951 /* reserve space for one command every (1 << BLOCK_SIZE) entries
952 or 2k dwords (whatever is smaller) */
953 ncmds = (nptes >> min(radeon_vm_block_size, 11)) + 1;
954
955 /* padding, etc. */
956 ndw = 64;
957
958 flags = radeon_vm_page_flags(bo_va->flags);
959 if ((flags & R600_PTE_GART_MASK) == R600_PTE_GART_MASK) {
960 /* only copy commands needed */
961 ndw += ncmds * 7;
962
963 } else if (flags & R600_PTE_SYSTEM) {
964 /* header for write data commands */
965 ndw += ncmds * 4;
966
967 /* body of write data command */
968 ndw += nptes * 2;
969
970 } else {
971 /* set page commands needed */
972 ndw += ncmds * 10;
973
974 /* two extra commands for begin/end of fragment */
975 ndw += 2 * 10;
976 }
977
978 /* update too big for an IB */
979 if (ndw > 0xfffff)
980 return -ENOMEM;
981
982 r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, ndw * 4);
983 if (r)
984 return r;
985 ib.length_dw = 0;
986
987 radeon_vm_update_ptes(rdev, vm, &ib, bo_va->it.start,
988 bo_va->it.last + 1, addr,
989 radeon_vm_page_flags(bo_va->flags));
990
991 radeon_asic_vm_pad_ib(rdev, &ib);
992 WARN_ON(ib.length_dw > ndw);
993
994 radeon_semaphore_sync_fence(ib.semaphore, vm->fence);
995 r = radeon_ib_schedule(rdev, &ib, NULL, false);
996 if (r) {
997 radeon_ib_free(rdev, &ib);
998 return r;
999 }
1000 radeon_vm_fence_pts(vm, bo_va->it.start, bo_va->it.last + 1, ib.fence);
1001 radeon_fence_unref(&vm->fence);
1002 vm->fence = radeon_fence_ref(ib.fence);
1003 radeon_ib_free(rdev, &ib);
1004 radeon_fence_unref(&vm->last_flush);
1005
1006 return 0;
1007 }
1008
1009 /**
1010 * radeon_vm_clear_freed - clear freed BOs in the PT
1011 *
1012 * @rdev: radeon_device pointer
1013 * @vm: requested vm
1014 *
1015 * Make sure all freed BOs are cleared in the PT.
1016 * Returns 0 for success.
1017 *
1018 * PTs have to be reserved and mutex must be locked!
1019 */
radeon_vm_clear_freed(struct radeon_device * rdev,struct radeon_vm * vm)1020 int radeon_vm_clear_freed(struct radeon_device *rdev,
1021 struct radeon_vm *vm)
1022 {
1023 struct radeon_bo_va *bo_va, *tmp;
1024 int r;
1025
1026 list_for_each_entry_safe(bo_va, tmp, &vm->freed, vm_status) {
1027 r = radeon_vm_bo_update(rdev, bo_va, NULL);
1028 radeon_bo_unref(&bo_va->bo);
1029 kfree(bo_va);
1030 if (r)
1031 return r;
1032 }
1033 return 0;
1034
1035 }
1036
1037 /**
1038 * radeon_vm_clear_invalids - clear invalidated BOs in the PT
1039 *
1040 * @rdev: radeon_device pointer
1041 * @vm: requested vm
1042 *
1043 * Make sure all invalidated BOs are cleared in the PT.
1044 * Returns 0 for success.
1045 *
1046 * PTs have to be reserved and mutex must be locked!
1047 */
radeon_vm_clear_invalids(struct radeon_device * rdev,struct radeon_vm * vm)1048 int radeon_vm_clear_invalids(struct radeon_device *rdev,
1049 struct radeon_vm *vm)
1050 {
1051 struct radeon_bo_va *bo_va, *tmp;
1052 int r;
1053
1054 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, vm_status) {
1055 r = radeon_vm_bo_update(rdev, bo_va, NULL);
1056 if (r)
1057 return r;
1058 }
1059 return 0;
1060 }
1061
1062 /**
1063 * radeon_vm_bo_rmv - remove a bo to a specific vm
1064 *
1065 * @rdev: radeon_device pointer
1066 * @bo_va: requested bo_va
1067 *
1068 * Remove @bo_va->bo from the requested vm (cayman+).
1069 *
1070 * Object have to be reserved!
1071 */
radeon_vm_bo_rmv(struct radeon_device * rdev,struct radeon_bo_va * bo_va)1072 void radeon_vm_bo_rmv(struct radeon_device *rdev,
1073 struct radeon_bo_va *bo_va)
1074 {
1075 struct radeon_vm *vm = bo_va->vm;
1076
1077 list_del(&bo_va->bo_list);
1078
1079 mutex_lock(&vm->mutex);
1080 if (bo_va->it.start || bo_va->it.last)
1081 interval_tree_remove(&bo_va->it, &vm->va);
1082 list_del(&bo_va->vm_status);
1083
1084 if (bo_va->addr) {
1085 bo_va->bo = radeon_bo_ref(bo_va->bo);
1086 list_add(&bo_va->vm_status, &vm->freed);
1087 } else {
1088 kfree(bo_va);
1089 }
1090
1091 mutex_unlock(&vm->mutex);
1092 }
1093
1094 /**
1095 * radeon_vm_bo_invalidate - mark the bo as invalid
1096 *
1097 * @rdev: radeon_device pointer
1098 * @vm: requested vm
1099 * @bo: radeon buffer object
1100 *
1101 * Mark @bo as invalid (cayman+).
1102 */
radeon_vm_bo_invalidate(struct radeon_device * rdev,struct radeon_bo * bo)1103 void radeon_vm_bo_invalidate(struct radeon_device *rdev,
1104 struct radeon_bo *bo)
1105 {
1106 struct radeon_bo_va *bo_va;
1107
1108 list_for_each_entry(bo_va, &bo->va, bo_list) {
1109 if (bo_va->addr) {
1110 mutex_lock(&bo_va->vm->mutex);
1111 list_del(&bo_va->vm_status);
1112 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
1113 mutex_unlock(&bo_va->vm->mutex);
1114 }
1115 }
1116 }
1117
1118 /**
1119 * radeon_vm_init - initialize a vm instance
1120 *
1121 * @rdev: radeon_device pointer
1122 * @vm: requested vm
1123 *
1124 * Init @vm fields (cayman+).
1125 */
radeon_vm_init(struct radeon_device * rdev,struct radeon_vm * vm)1126 int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
1127 {
1128 const unsigned align = min(RADEON_VM_PTB_ALIGN_SIZE,
1129 RADEON_VM_PTE_COUNT * 8);
1130 unsigned pd_size, pd_entries, pts_size;
1131 int r;
1132
1133 vm->id = 0;
1134 vm->ib_bo_va = NULL;
1135 vm->fence = NULL;
1136 vm->last_flush = NULL;
1137 vm->last_id_use = NULL;
1138 mutex_init(&vm->mutex);
1139 vm->va = RB_ROOT;
1140 INIT_LIST_HEAD(&vm->invalidated);
1141 INIT_LIST_HEAD(&vm->freed);
1142
1143 pd_size = radeon_vm_directory_size(rdev);
1144 pd_entries = radeon_vm_num_pdes(rdev);
1145
1146 /* allocate page table array */
1147 pts_size = pd_entries * sizeof(struct radeon_vm_pt);
1148 vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
1149 if (vm->page_tables == NULL) {
1150 DRM_ERROR("Cannot allocate memory for page table array\n");
1151 return -ENOMEM;
1152 }
1153
1154 r = radeon_bo_create(rdev, pd_size, align, true,
1155 RADEON_GEM_DOMAIN_VRAM, 0, NULL,
1156 NULL, &vm->page_directory);
1157 if (r)
1158 return r;
1159
1160 r = radeon_vm_clear_bo(rdev, vm->page_directory);
1161 if (r) {
1162 radeon_bo_unref(&vm->page_directory);
1163 vm->page_directory = NULL;
1164 return r;
1165 }
1166
1167 return 0;
1168 }
1169
1170 /**
1171 * radeon_vm_fini - tear down a vm instance
1172 *
1173 * @rdev: radeon_device pointer
1174 * @vm: requested vm
1175 *
1176 * Tear down @vm (cayman+).
1177 * Unbind the VM and remove all bos from the vm bo list
1178 */
radeon_vm_fini(struct radeon_device * rdev,struct radeon_vm * vm)1179 void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
1180 {
1181 struct radeon_bo_va *bo_va, *tmp;
1182 int i, r;
1183
1184 if (!RB_EMPTY_ROOT(&vm->va)) {
1185 dev_err(rdev->dev, "still active bo inside vm\n");
1186 }
1187 rbtree_postorder_for_each_entry_safe(bo_va, tmp, &vm->va, it.rb) {
1188 interval_tree_remove(&bo_va->it, &vm->va);
1189 r = radeon_bo_reserve(bo_va->bo, false);
1190 if (!r) {
1191 list_del_init(&bo_va->bo_list);
1192 radeon_bo_unreserve(bo_va->bo);
1193 kfree(bo_va);
1194 }
1195 }
1196 list_for_each_entry_safe(bo_va, tmp, &vm->freed, vm_status) {
1197 radeon_bo_unref(&bo_va->bo);
1198 kfree(bo_va);
1199 }
1200
1201 for (i = 0; i < radeon_vm_num_pdes(rdev); i++)
1202 radeon_bo_unref(&vm->page_tables[i].bo);
1203 kfree(vm->page_tables);
1204
1205 radeon_bo_unref(&vm->page_directory);
1206
1207 radeon_fence_unref(&vm->fence);
1208 radeon_fence_unref(&vm->last_flush);
1209 radeon_fence_unref(&vm->last_id_use);
1210
1211 mutex_destroy(&vm->mutex);
1212 }
1213