ttm_bo_util.c revision 1.1.1.1.6.1 1 /**************************************************************************
2 *
3 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
31 #include <drm/ttm/ttm_bo_driver.h>
32 #include <drm/ttm/ttm_placement.h>
33 #include <drm/drm_vma_manager.h>
34 #include <linux/io.h>
35 #include <linux/highmem.h>
36 #include <linux/wait.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
39 #include <linux/module.h>
40 #include <linux/export.h>
41
42 #ifdef __NetBSD__ /* PMAP_* caching flags for ttm_io_prot */
43 #include <uvm/uvm_pmap.h>
44 #endif
45
46 void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
47 {
48 ttm_bo_mem_put(bo, &bo->mem);
49 }
50
51 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
52 bool evict,
53 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
54 {
55 struct ttm_tt *ttm = bo->ttm;
56 struct ttm_mem_reg *old_mem = &bo->mem;
57 int ret;
58
59 if (old_mem->mem_type != TTM_PL_SYSTEM) {
60 ttm_tt_unbind(ttm);
61 ttm_bo_free_old_node(bo);
62 ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
63 TTM_PL_MASK_MEM);
64 old_mem->mem_type = TTM_PL_SYSTEM;
65 }
66
67 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
68 if (unlikely(ret != 0))
69 return ret;
70
71 if (new_mem->mem_type != TTM_PL_SYSTEM) {
72 ret = ttm_tt_bind(ttm, new_mem);
73 if (unlikely(ret != 0))
74 return ret;
75 }
76
77 *old_mem = *new_mem;
78 new_mem->mm_node = NULL;
79
80 return 0;
81 }
82 EXPORT_SYMBOL(ttm_bo_move_ttm);
83
84 int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
85 {
86 if (likely(man->io_reserve_fastpath))
87 return 0;
88
89 if (interruptible)
90 return mutex_lock_interruptible(&man->io_reserve_mutex);
91
92 mutex_lock(&man->io_reserve_mutex);
93 return 0;
94 }
95 EXPORT_SYMBOL(ttm_mem_io_lock);
96
97 void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
98 {
99 if (likely(man->io_reserve_fastpath))
100 return;
101
102 mutex_unlock(&man->io_reserve_mutex);
103 }
104 EXPORT_SYMBOL(ttm_mem_io_unlock);
105
106 static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
107 {
108 struct ttm_buffer_object *bo;
109
110 if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
111 return -EAGAIN;
112
113 bo = list_first_entry(&man->io_reserve_lru,
114 struct ttm_buffer_object,
115 io_reserve_lru);
116 list_del_init(&bo->io_reserve_lru);
117 ttm_bo_unmap_virtual_locked(bo);
118
119 return 0;
120 }
121
122
123 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
124 struct ttm_mem_reg *mem)
125 {
126 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
127 int ret = 0;
128
129 if (!bdev->driver->io_mem_reserve)
130 return 0;
131 if (likely(man->io_reserve_fastpath))
132 return bdev->driver->io_mem_reserve(bdev, mem);
133
134 if (bdev->driver->io_mem_reserve &&
135 mem->bus.io_reserved_count++ == 0) {
136 retry:
137 ret = bdev->driver->io_mem_reserve(bdev, mem);
138 if (ret == -EAGAIN) {
139 ret = ttm_mem_io_evict(man);
140 if (ret == 0)
141 goto retry;
142 }
143 }
144 return ret;
145 }
146 EXPORT_SYMBOL(ttm_mem_io_reserve);
147
148 void ttm_mem_io_free(struct ttm_bo_device *bdev,
149 struct ttm_mem_reg *mem)
150 {
151 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
152
153 if (likely(man->io_reserve_fastpath))
154 return;
155
156 if (bdev->driver->io_mem_reserve &&
157 --mem->bus.io_reserved_count == 0 &&
158 bdev->driver->io_mem_free)
159 bdev->driver->io_mem_free(bdev, mem);
160
161 }
162 EXPORT_SYMBOL(ttm_mem_io_free);
163
164 int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
165 {
166 struct ttm_mem_reg *mem = &bo->mem;
167 int ret;
168
169 if (!mem->bus.io_reserved_vm) {
170 struct ttm_mem_type_manager *man =
171 &bo->bdev->man[mem->mem_type];
172
173 ret = ttm_mem_io_reserve(bo->bdev, mem);
174 if (unlikely(ret != 0))
175 return ret;
176 mem->bus.io_reserved_vm = true;
177 if (man->use_io_reserve_lru)
178 list_add_tail(&bo->io_reserve_lru,
179 &man->io_reserve_lru);
180 }
181 return 0;
182 }
183
184 void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
185 {
186 struct ttm_mem_reg *mem = &bo->mem;
187
188 if (mem->bus.io_reserved_vm) {
189 mem->bus.io_reserved_vm = false;
190 list_del_init(&bo->io_reserve_lru);
191 ttm_mem_io_free(bo->bdev, mem);
192 }
193 }
194
195 static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
196 void **virtual)
197 {
198 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
199 int ret;
200 void *addr;
201
202 *virtual = NULL;
203 (void) ttm_mem_io_lock(man, false);
204 ret = ttm_mem_io_reserve(bdev, mem);
205 ttm_mem_io_unlock(man);
206 if (ret || !mem->bus.is_iomem)
207 return ret;
208
209 if (mem->bus.addr) {
210 addr = mem->bus.addr;
211 } else {
212 #ifdef __NetBSD__
213 const bus_addr_t bus_addr = (mem->bus.base + mem->bus.offset);
214 int flags = BUS_SPACE_MAP_LINEAR;
215
216 if (ISSET(mem->placement, TTM_PL_FLAG_WC))
217 flags |= BUS_SPACE_MAP_PREFETCHABLE;
218 /* XXX errno NetBSD->Linux */
219 ret = -bus_space_map(bdev->memt, bus_addr, mem->bus.size,
220 flags, &mem->bus.memh);
221 if (ret) {
222 (void) ttm_mem_io_lock(man, false);
223 ttm_mem_io_free(bdev, mem);
224 ttm_mem_io_unlock(man);
225 return ret;
226 }
227 addr = bus_space_vaddr(bdev->memt, mem->bus.memh);
228 #else
229 if (mem->placement & TTM_PL_FLAG_WC)
230 addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
231 else
232 addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size);
233 if (!addr) {
234 (void) ttm_mem_io_lock(man, false);
235 ttm_mem_io_free(bdev, mem);
236 ttm_mem_io_unlock(man);
237 return -ENOMEM;
238 }
239 #endif
240 }
241 *virtual = addr;
242 return 0;
243 }
244
245 static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
246 void *virtual)
247 {
248 struct ttm_mem_type_manager *man;
249
250 man = &bdev->man[mem->mem_type];
251
252 if (virtual && mem->bus.addr == NULL)
253 #ifdef __NetBSD__
254 bus_space_unmap(bdev->memt, mem->bus.memh, mem->bus.size);
255 #else
256 iounmap(virtual);
257 #endif
258 (void) ttm_mem_io_lock(man, false);
259 ttm_mem_io_free(bdev, mem);
260 ttm_mem_io_unlock(man);
261 }
262
263 #ifdef __NetBSD__
264 # define ioread32 fake_ioread32
265 # define iowrite32 fake_iowrite32
266
267 static inline uint32_t
268 fake_ioread32(const volatile uint32_t *p)
269 {
270 uint32_t v;
271
272 v = *p;
273 __insn_barrier(); /* XXX */
274
275 return v;
276 }
277
278 static inline void
279 iowrite32(uint32_t v, volatile uint32_t *p)
280 {
281
282 __insn_barrier(); /* XXX */
283 *p = v;
284 }
285 #endif
286
287 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
288 {
289 uint32_t *dstP =
290 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
291 uint32_t *srcP =
292 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
293
294 int i;
295 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
296 iowrite32(ioread32(srcP++), dstP++);
297 return 0;
298 }
299
300 #ifdef __NetBSD__
301 # undef ioread32
302 # undef iowrite32
303 #endif
304
305 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
306 unsigned long page,
307 pgprot_t prot)
308 {
309 struct page *d = ttm->pages[page];
310 void *dst;
311
312 if (!d)
313 return -ENOMEM;
314
315 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
316
317 #ifdef CONFIG_X86
318 dst = kmap_atomic_prot(d, prot);
319 #else
320 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
321 dst = vmap(&d, 1, 0, prot);
322 else
323 dst = kmap(d);
324 #endif
325 if (!dst)
326 return -ENOMEM;
327
328 memcpy_fromio(dst, src, PAGE_SIZE);
329
330 #ifdef CONFIG_X86
331 kunmap_atomic(dst);
332 #else
333 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
334 #ifdef __NetBSD__
335 vunmap(dst, 1);
336 #else
337 vunmap(dst);
338 #endif
339 else
340 kunmap(d);
341 #endif
342
343 return 0;
344 }
345
346 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
347 unsigned long page,
348 pgprot_t prot)
349 {
350 struct page *s = ttm->pages[page];
351 void *src;
352
353 if (!s)
354 return -ENOMEM;
355
356 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
357 #ifdef CONFIG_X86
358 src = kmap_atomic_prot(s, prot);
359 #else
360 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
361 src = vmap(&s, 1, 0, prot);
362 else
363 src = kmap(s);
364 #endif
365 if (!src)
366 return -ENOMEM;
367
368 memcpy_toio(dst, src, PAGE_SIZE);
369
370 #ifdef CONFIG_X86
371 kunmap_atomic(src);
372 #else
373 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
374 vunmap(src);
375 else
376 kunmap(s);
377 #endif
378
379 return 0;
380 }
381
382 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
383 bool evict, bool no_wait_gpu,
384 struct ttm_mem_reg *new_mem)
385 {
386 struct ttm_bo_device *bdev = bo->bdev;
387 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
388 struct ttm_tt *ttm = bo->ttm;
389 struct ttm_mem_reg *old_mem = &bo->mem;
390 struct ttm_mem_reg old_copy = *old_mem;
391 void *old_iomap;
392 void *new_iomap;
393 int ret;
394 unsigned long i;
395 unsigned long page;
396 unsigned long add = 0;
397 int dir;
398
399 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
400 if (ret)
401 return ret;
402 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
403 if (ret)
404 goto out;
405
406 /*
407 * Single TTM move. NOP.
408 */
409 if (old_iomap == NULL && new_iomap == NULL)
410 goto out2;
411
412 /*
413 * Don't move nonexistent data. Clear destination instead.
414 */
415 if (old_iomap == NULL &&
416 (ttm == NULL || (ttm->state == tt_unpopulated &&
417 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
418 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
419 goto out2;
420 }
421
422 /*
423 * TTM might be null for moves within the same region.
424 */
425 if (ttm && ttm->state == tt_unpopulated) {
426 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
427 if (ret)
428 goto out1;
429 }
430
431 add = 0;
432 dir = 1;
433
434 if ((old_mem->mem_type == new_mem->mem_type) &&
435 (new_mem->start < old_mem->start + old_mem->size)) {
436 dir = -1;
437 add = new_mem->num_pages - 1;
438 }
439
440 for (i = 0; i < new_mem->num_pages; ++i) {
441 page = i * dir + add;
442 if (old_iomap == NULL) {
443 pgprot_t prot = ttm_io_prot(old_mem->placement,
444 PAGE_KERNEL);
445 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
446 prot);
447 } else if (new_iomap == NULL) {
448 pgprot_t prot = ttm_io_prot(new_mem->placement,
449 PAGE_KERNEL);
450 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
451 prot);
452 } else
453 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
454 if (ret)
455 goto out1;
456 }
457 mb();
458 out2:
459 old_copy = *old_mem;
460 *old_mem = *new_mem;
461 new_mem->mm_node = NULL;
462
463 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
464 ttm_tt_unbind(ttm);
465 ttm_tt_destroy(ttm);
466 bo->ttm = NULL;
467 }
468
469 out1:
470 ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
471 out:
472 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
473
474 /*
475 * On error, keep the mm node!
476 */
477 if (!ret)
478 ttm_bo_mem_put(bo, &old_copy);
479 return ret;
480 }
481 EXPORT_SYMBOL(ttm_bo_move_memcpy);
482
483 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
484 {
485 kfree(bo);
486 }
487
488 /**
489 * ttm_buffer_object_transfer
490 *
491 * @bo: A pointer to a struct ttm_buffer_object.
492 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
493 * holding the data of @bo with the old placement.
494 *
495 * This is a utility function that may be called after an accelerated move
496 * has been scheduled. A new buffer object is created as a placeholder for
497 * the old data while it's being copied. When that buffer object is idle,
498 * it can be destroyed, releasing the space of the old placement.
499 * Returns:
500 * !0: Failure.
501 */
502
503 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
504 struct ttm_buffer_object **new_obj)
505 {
506 struct ttm_buffer_object *fbo;
507 struct ttm_bo_device *bdev = bo->bdev;
508 struct ttm_bo_driver *driver = bdev->driver;
509 int ret;
510
511 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
512 if (!fbo)
513 return -ENOMEM;
514
515 *fbo = *bo;
516
517 /**
518 * Fix up members that we shouldn't copy directly:
519 * TODO: Explicit member copy would probably be better here.
520 */
521
522 INIT_LIST_HEAD(&fbo->ddestroy);
523 INIT_LIST_HEAD(&fbo->lru);
524 INIT_LIST_HEAD(&fbo->swap);
525 INIT_LIST_HEAD(&fbo->io_reserve_lru);
526 #ifdef __NetBSD__
527 drm_vma_node_init(&fbo->vma_node);
528 #else
529 drm_vma_node_reset(&fbo->vma_node);
530 #endif
531 atomic_set(&fbo->cpu_writers, 0);
532
533 spin_lock(&bdev->fence_lock);
534 if (bo->sync_obj)
535 fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
536 else
537 fbo->sync_obj = NULL;
538 spin_unlock(&bdev->fence_lock);
539 kref_init(&fbo->list_kref);
540 kref_init(&fbo->kref);
541 fbo->destroy = &ttm_transfered_destroy;
542 fbo->acc_size = 0;
543 fbo->resv = &fbo->ttm_resv;
544 reservation_object_init(fbo->resv);
545 ret = ww_mutex_trylock(&fbo->resv->lock);
546 WARN_ON(!ret);
547
548 *new_obj = fbo;
549 return 0;
550 }
551
552 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
553 {
554 #ifdef __NetBSD__
555 switch (caching_flags & TTM_PL_MASK_CACHING) {
556 case TTM_PL_FLAG_CACHED:
557 return (tmp | PMAP_WRITE_BACK);
558 case TTM_PL_FLAG_WC:
559 return (tmp | PMAP_WRITE_COMBINE);
560 case TTM_PL_FLAG_UNCACHED:
561 return (tmp | PMAP_NOCACHE);
562 default:
563 panic("invalid caching flags: %"PRIx32"\n",
564 (caching_flags & TTM_PL_MASK_CACHING));
565 }
566 #else
567 #if defined(__i386__) || defined(__x86_64__)
568 if (caching_flags & TTM_PL_FLAG_WC)
569 tmp = pgprot_writecombine(tmp);
570 else if (boot_cpu_data.x86 > 3)
571 tmp = pgprot_noncached(tmp);
572
573 #elif defined(__powerpc__)
574 if (!(caching_flags & TTM_PL_FLAG_CACHED)) {
575 pgprot_val(tmp) |= _PAGE_NO_CACHE;
576 if (caching_flags & TTM_PL_FLAG_UNCACHED)
577 pgprot_val(tmp) |= _PAGE_GUARDED;
578 }
579 #endif
580 #if defined(__ia64__)
581 if (caching_flags & TTM_PL_FLAG_WC)
582 tmp = pgprot_writecombine(tmp);
583 else
584 tmp = pgprot_noncached(tmp);
585 #endif
586 #if defined(__sparc__) || defined(__mips__)
587 if (!(caching_flags & TTM_PL_FLAG_CACHED))
588 tmp = pgprot_noncached(tmp);
589 #endif
590 return tmp;
591 #endif
592 }
593 EXPORT_SYMBOL(ttm_io_prot);
594
595 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
596 unsigned long offset,
597 unsigned long size,
598 struct ttm_bo_kmap_obj *map)
599 {
600 struct ttm_mem_reg *mem = &bo->mem;
601
602 if (bo->mem.bus.addr) {
603 map->bo_kmap_type = ttm_bo_map_premapped;
604 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
605 } else {
606 map->bo_kmap_type = ttm_bo_map_iomap;
607 #ifdef __NetBSD__
608 {
609 bus_addr_t addr;
610 int flags = BUS_SPACE_MAP_LINEAR;
611 int ret;
612
613 addr = (bo->mem.bus.base + bo->mem.bus.offset + offset);
614 if (ISSET(mem->placement, TTM_PL_FLAG_WC))
615 flags |= BUS_SPACE_MAP_PREFETCHABLE;
616 /* XXX errno NetBSD->Linux */
617 ret = -bus_space_map(bo->bdev->memt, addr, size, flags,
618 &map->u.io.memh);
619 if (ret)
620 return ret;
621 map->u.io.size = size;
622 map->virtual = bus_space_vaddr(bo->bdev->memt, map->u.io.memh);
623 }
624 #else
625 if (mem->placement & TTM_PL_FLAG_WC)
626 map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
627 size);
628 else
629 map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
630 size);
631 #endif
632 }
633 return (!map->virtual) ? -ENOMEM : 0;
634 }
635
636 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
637 unsigned long start_page,
638 unsigned long num_pages,
639 struct ttm_bo_kmap_obj *map)
640 {
641 struct ttm_mem_reg *mem = &bo->mem;
642 pgprot_t prot;
643 struct ttm_tt *ttm = bo->ttm;
644 #ifdef __NetBSD__
645 unsigned i;
646 vaddr_t vaddr;
647 #endif
648 int ret;
649
650 BUG_ON(!ttm);
651
652 if (ttm->state == tt_unpopulated) {
653 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
654 if (ret)
655 return ret;
656 }
657
658 #ifdef __NetBSD__
659 /*
660 * Can't use uvm_map here because it provides no way to pass
661 * along the cacheability flags. So we'll uvm_km_alloc
662 * ourselves some KVA and then pmap_kenter_pa directly.
663 */
664
665 KASSERT(num_pages <= ttm->num_pages);
666 KASSERT(start_page <= (ttm->num_pages - num_pages));
667 prot = ttm_io_prot(mem->placement, (VM_PROT_READ | VM_PROT_WRITE));
668 vaddr = uvm_km_alloc(kernel_map, (num_pages << PAGE_SHIFT), PAGE_SIZE,
669 UVM_KMF_VAONLY | UVM_KMF_CANFAIL | UVM_KMF_WAITVA);
670 if (vaddr == 0)
671 return -ENOMEM;
672 for (i = 0; i < num_pages; i++)
673 pmap_kenter_pa(vaddr + i*PAGE_SIZE,
674 page_to_phys(ttm->pages[start_page + i]),
675 (VM_PROT_READ | VM_PROT_WRITE), prot);
676 pmap_update(pmap_kernel());
677 map->bo_kmap_type = ttm_bo_map_vmap;
678 map->u.uvm.vsize = (num_pages << PAGE_SHIFT);
679 map->virtual = (void *)vaddr;
680 return 0;
681 #else
682 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
683 /*
684 * We're mapping a single page, and the desired
685 * page protection is consistent with the bo.
686 */
687
688 map->bo_kmap_type = ttm_bo_map_kmap;
689 map->page = ttm->pages[start_page];
690 map->virtual = kmap(map->page);
691 } else {
692 /*
693 * We need to use vmap to get the desired page protection
694 * or to make the buffer object look contiguous.
695 */
696 prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
697 PAGE_KERNEL :
698 ttm_io_prot(mem->placement, PAGE_KERNEL);
699 map->bo_kmap_type = ttm_bo_map_vmap;
700 map->virtual = vmap(ttm->pages + start_page, num_pages,
701 0, prot);
702 }
703 return (!map->virtual) ? -ENOMEM : 0;
704 #endif
705 }
706
707 int ttm_bo_kmap(struct ttm_buffer_object *bo,
708 unsigned long start_page, unsigned long num_pages,
709 struct ttm_bo_kmap_obj *map)
710 {
711 struct ttm_mem_type_manager *man =
712 &bo->bdev->man[bo->mem.mem_type];
713 unsigned long offset, size;
714 int ret;
715
716 BUG_ON(!list_empty(&bo->swap));
717 map->virtual = NULL;
718 map->bo = bo;
719 if (num_pages > bo->num_pages)
720 return -EINVAL;
721 if (start_page > bo->num_pages)
722 return -EINVAL;
723 #if 0
724 if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
725 return -EPERM;
726 #endif
727 (void) ttm_mem_io_lock(man, false);
728 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
729 ttm_mem_io_unlock(man);
730 if (ret)
731 return ret;
732 if (!bo->mem.bus.is_iomem) {
733 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
734 } else {
735 offset = start_page << PAGE_SHIFT;
736 size = num_pages << PAGE_SHIFT;
737 return ttm_bo_ioremap(bo, offset, size, map);
738 }
739 }
740 EXPORT_SYMBOL(ttm_bo_kmap);
741
742 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
743 {
744 struct ttm_buffer_object *bo = map->bo;
745 struct ttm_mem_type_manager *man =
746 &bo->bdev->man[bo->mem.mem_type];
747
748 if (!map->virtual)
749 return;
750 switch (map->bo_kmap_type) {
751 case ttm_bo_map_iomap:
752 #ifdef __NetBSD__
753 bus_space_unmap(bo->bdev->memt, map->u.io.memh,
754 map->u.io.size);
755 #else
756 iounmap(map->virtual);
757 #endif
758 break;
759 case ttm_bo_map_vmap:
760 #ifdef __NetBSD__
761 pmap_kremove((vaddr_t)map->virtual, map->u.uvm.vsize);
762 pmap_update(pmap_kernel());
763 uvm_km_free(kernel_map, (vaddr_t)map->virtual,
764 map->u.uvm.vsize, UVM_KMF_VAONLY);
765 #else
766 vunmap(map->virtual);
767 #endif
768 break;
769 case ttm_bo_map_kmap:
770 #ifdef __NetBSD__
771 panic("ttm_bo_map_kmap does not exist in NetBSD");
772 #else
773 kunmap(map->page);
774 #endif
775 break;
776 case ttm_bo_map_premapped:
777 break;
778 default:
779 BUG();
780 }
781 (void) ttm_mem_io_lock(man, false);
782 ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
783 ttm_mem_io_unlock(man);
784 map->virtual = NULL;
785 #ifndef __NetBSD__
786 map->page = NULL;
787 #endif
788 }
789 EXPORT_SYMBOL(ttm_bo_kunmap);
790
791 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
792 void *sync_obj,
793 bool evict,
794 bool no_wait_gpu,
795 struct ttm_mem_reg *new_mem)
796 {
797 struct ttm_bo_device *bdev = bo->bdev;
798 struct ttm_bo_driver *driver = bdev->driver;
799 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
800 struct ttm_mem_reg *old_mem = &bo->mem;
801 int ret;
802 struct ttm_buffer_object *ghost_obj;
803 void *tmp_obj = NULL;
804
805 spin_lock(&bdev->fence_lock);
806 if (bo->sync_obj) {
807 tmp_obj = bo->sync_obj;
808 bo->sync_obj = NULL;
809 }
810 bo->sync_obj = driver->sync_obj_ref(sync_obj);
811 if (evict) {
812 ret = ttm_bo_wait(bo, false, false, false);
813 spin_unlock(&bdev->fence_lock);
814 if (tmp_obj)
815 driver->sync_obj_unref(&tmp_obj);
816 if (ret)
817 return ret;
818
819 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
820 (bo->ttm != NULL)) {
821 ttm_tt_unbind(bo->ttm);
822 ttm_tt_destroy(bo->ttm);
823 bo->ttm = NULL;
824 }
825 ttm_bo_free_old_node(bo);
826 } else {
827 /**
828 * This should help pipeline ordinary buffer moves.
829 *
830 * Hang old buffer memory on a new buffer object,
831 * and leave it to be released when the GPU
832 * operation has completed.
833 */
834
835 set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
836 spin_unlock(&bdev->fence_lock);
837 if (tmp_obj)
838 driver->sync_obj_unref(&tmp_obj);
839
840 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
841 if (ret)
842 return ret;
843
844 /**
845 * If we're not moving to fixed memory, the TTM object
846 * needs to stay alive. Otherwhise hang it on the ghost
847 * bo to be unbound and destroyed.
848 */
849
850 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
851 ghost_obj->ttm = NULL;
852 else
853 bo->ttm = NULL;
854
855 ttm_bo_unreserve(ghost_obj);
856 ttm_bo_unref(&ghost_obj);
857 }
858
859 *old_mem = *new_mem;
860 new_mem->mm_node = NULL;
861
862 return 0;
863 }
864 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
865