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