1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 * Copyright (c) 2013 The FreeBSD Foundation
32 * All rights reserved.
34 * Portions of this software were developed by Konstantin Belousov
35 * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
37 *$FreeBSD: head/sys/dev/drm2/ttm/ttm_bo_vm.c 253710 2013-07-27 16:44:37Z kib $
42 #define pr_fmt(fmt) "[TTM] " fmt
44 #include <drm/ttm/ttm_module.h>
45 #include <drm/ttm/ttm_bo_driver.h>
46 #include <drm/ttm/ttm_placement.h>
48 #include <vm/vm_page.h>
49 #include <linux/export.h>
51 RB_GENERATE(ttm_bo_device_buffer_objects, ttm_buffer_object, vm_rb,
52 ttm_bo_cmp_rb_tree_items);
55 #define TTM_BO_VM_NUM_PREFAULT 16
58 ttm_bo_cmp_rb_tree_items(struct ttm_buffer_object *a,
59 struct ttm_buffer_object *b)
61 if (a->vm_node->start < b->vm_node->start) {
63 } else if (a->vm_node->start > b->vm_node->start) {
71 static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
72 unsigned long page_start,
73 unsigned long num_pages)
75 unsigned long cur_offset;
76 struct ttm_buffer_object *bo;
77 struct ttm_buffer_object *best_bo = NULL;
79 bo = RB_ROOT(&bdev->addr_space_rb);
81 cur_offset = bo->vm_node->start;
82 if (page_start >= cur_offset) {
84 if (page_start == cur_offset)
86 bo = RB_RIGHT(bo, vm_rb);
88 bo = RB_LEFT(bo, vm_rb);
91 if (unlikely(best_bo == NULL))
94 if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
95 (page_start + num_pages)))
102 ttm_bo_vm_fault(vm_object_t vm_obj, vm_ooffset_t offset,
103 int prot, vm_page_t *mres)
105 struct ttm_buffer_object *bo = vm_obj->handle;
106 struct ttm_bo_device *bdev = bo->bdev;
107 struct ttm_tt *ttm = NULL;
108 vm_page_t m, m1, oldm;
110 int retval = VM_PAGER_OK;
111 struct ttm_mem_type_manager *man =
112 &bdev->man[bo->mem.mem_type];
114 vm_object_pip_add(vm_obj, 1);
117 vm_page_remove(oldm);
122 VM_OBJECT_WUNLOCK(vm_obj);
126 ret = ttm_bo_reserve(bo, false, false, false, 0);
127 if (unlikely(ret != 0)) {
134 if (bdev->driver->fault_reserve_notify) {
135 ret = bdev->driver->fault_reserve_notify(bo);
145 retval = VM_PAGER_ERROR;
151 * Wait for buffer data in transit, due to a pipelined
155 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
156 if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
158 * Here, the behavior differs between Linux and FreeBSD.
160 * On Linux, the wait is interruptible (3rd argument to
161 * ttm_bo_wait). There must be some mechanism to resume
162 * page fault handling, once the signal is processed.
164 * On FreeBSD, the wait is uninteruptible. This is not a
165 * problem as we can't end up with an unkillable process
166 * here, because the wait will eventually time out.
168 * An example of this situation is the Xorg process
169 * which uses SIGALRM internally. The signal could
170 * interrupt the wait, causing the page fault to fail
171 * and the process to receive SIGSEGV.
173 ret = ttm_bo_wait(bo, false, false, false);
174 lockmgr(&bdev->fence_lock, LK_RELEASE);
175 if (unlikely(ret != 0)) {
176 retval = VM_PAGER_ERROR;
180 lockmgr(&bdev->fence_lock, LK_RELEASE);
182 ret = ttm_mem_io_lock(man, true);
183 if (unlikely(ret != 0)) {
184 retval = VM_PAGER_ERROR;
187 ret = ttm_mem_io_reserve_vm(bo);
188 if (unlikely(ret != 0)) {
189 retval = VM_PAGER_ERROR;
194 * Strictly, we're not allowed to modify vma->vm_page_prot here,
195 * since the mmap_sem is only held in read mode. However, we
196 * modify only the caching bits of vma->vm_page_prot and
197 * consider those bits protected by
198 * the bo->mutex, as we should be the only writers.
199 * There shouldn't really be any readers of these bits except
200 * within vm_insert_mixed()? fork?
202 * TODO: Add a list of vmas to the bo, and change the
203 * vma->vm_page_prot when the object changes caching policy, with
204 * the correct locks held.
206 if (!bo->mem.bus.is_iomem) {
207 /* Allocate all page at once, most common usage */
209 if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
210 retval = VM_PAGER_ERROR;
215 if (bo->mem.bus.is_iomem) {
216 m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base +
217 bo->mem.bus.offset + offset);
218 pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement));
221 m = ttm->pages[OFF_TO_IDX(offset)];
223 retval = VM_PAGER_ERROR;
226 pmap_page_set_memattr(m,
227 (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
228 VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement));
231 VM_OBJECT_WLOCK(vm_obj);
232 if ((m->flags & PG_BUSY) != 0) {
234 vm_page_sleep(m, "ttmpbs");
236 ttm_mem_io_unlock(man);
237 ttm_bo_unreserve(bo);
240 m->valid = VM_PAGE_BITS_ALL;
242 m1 = vm_page_lookup(vm_obj, OFF_TO_IDX(offset));
244 vm_page_insert(m, vm_obj, OFF_TO_IDX(offset));
247 ("inconsistent insert bo %p m %p m1 %p offset %jx",
248 bo, m, m1, (uintmax_t)offset));
250 vm_page_busy_try(m, FALSE);
257 ttm_mem_io_unlock(man);
259 ttm_bo_unreserve(bo);
260 vm_object_pip_wakeup(vm_obj);
264 VM_OBJECT_WLOCK(vm_obj);
268 VM_OBJECT_WLOCK(vm_obj);
273 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
274 vm_ooffset_t foff, struct ucred *cred, u_short *color)
278 * On Linux, a reference to the buffer object is acquired here.
279 * The reason is that this function is not called when the
280 * mmap() is initialized, but only when a process forks for
281 * instance. Therefore on Linux, the reference on the bo is
282 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's
283 * then released in ttm_bo_vm_close().
285 * Here, this function is called during mmap() intialization.
286 * Thus, the reference acquired in ttm_bo_mmap_single() is
294 ttm_bo_vm_dtor(void *handle)
296 struct ttm_buffer_object *bo = handle;
301 static struct cdev_pager_ops ttm_pager_ops = {
302 .cdev_pg_fault = ttm_bo_vm_fault,
303 .cdev_pg_ctor = ttm_bo_vm_ctor,
304 .cdev_pg_dtor = ttm_bo_vm_dtor
308 ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset, vm_size_t size,
309 struct vm_object **obj_res, int nprot)
311 struct ttm_bo_driver *driver;
312 struct ttm_buffer_object *bo;
313 struct vm_object *vm_obj;
318 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
319 bo = ttm_bo_vm_lookup_rb(bdev, OFF_TO_IDX(*offset), OFF_TO_IDX(size));
320 if (likely(bo != NULL))
322 lockmgr(&bdev->vm_lock, LK_RELEASE);
324 if (unlikely(bo == NULL)) {
325 kprintf("[TTM] Could not find buffer object to map\n");
329 driver = bo->bdev->driver;
330 if (unlikely(!driver->verify_access)) {
334 ret = -driver->verify_access(bo);
335 if (unlikely(ret != 0))
338 vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, &ttm_pager_ops,
339 size, nprot, 0, curthread->td_ucred);
341 if (vm_obj == NULL) {
346 * Note: We're transferring the bo reference to vm_obj->handle here.
355 EXPORT_SYMBOL(ttm_bo_mmap);
358 ttm_bo_release_mmap(struct ttm_buffer_object *bo)
364 vm_obj = cdev_pager_lookup(bo);
368 VM_OBJECT_WLOCK(vm_obj);
369 for (i = 0; i < bo->num_pages; i++) {
370 m = vm_page_lookup_busy_wait(vm_obj, i, TRUE, "ttm_unm");
373 cdev_pager_free_page(vm_obj, m);
375 VM_OBJECT_WUNLOCK(vm_obj);
377 vm_object_deallocate(vm_obj);
381 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
383 if (vma->vm_pgoff != 0)
386 vma->vm_ops = &ttm_bo_vm_ops;
387 vma->vm_private_data = ttm_bo_reference(bo);
388 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
391 EXPORT_SYMBOL(ttm_fbdev_mmap);
394 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
395 const char __user *wbuf, char __user *rbuf, size_t count,
396 loff_t *f_pos, bool write)
398 struct ttm_buffer_object *bo;
399 struct ttm_bo_driver *driver;
400 struct ttm_bo_kmap_obj map;
401 unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
402 unsigned long kmap_offset;
403 unsigned long kmap_end;
404 unsigned long kmap_num;
406 unsigned int page_offset;
409 bool no_wait = false;
412 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
413 bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
414 if (likely(bo != NULL))
415 ttm_bo_reference(bo);
416 lockmgr(&bdev->vm_lock, LK_RELEASE);
418 if (unlikely(bo == NULL))
421 driver = bo->bdev->driver;
422 if (unlikely(!driver->verify_access)) {
427 ret = driver->verify_access(bo, filp);
428 if (unlikely(ret != 0))
431 kmap_offset = dev_offset - bo->vm_node->start;
432 if (unlikely(kmap_offset >= bo->num_pages)) {
437 page_offset = *f_pos & ~PAGE_MASK;
438 io_size = bo->num_pages - kmap_offset;
439 io_size = (io_size << PAGE_SHIFT) - page_offset;
443 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
444 kmap_num = kmap_end - kmap_offset + 1;
446 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
458 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
459 if (unlikely(ret != 0)) {
460 ttm_bo_unreserve(bo);
464 virtual = ttm_kmap_obj_virtual(&map, &dummy);
465 virtual += page_offset;
468 ret = copy_from_user(virtual, wbuf, io_size);
470 ret = copy_to_user(rbuf, virtual, io_size);
473 ttm_bo_unreserve(bo);
476 if (unlikely(ret != 0))
487 ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
488 char __user *rbuf, size_t count, loff_t *f_pos,
491 struct ttm_bo_kmap_obj map;
492 unsigned long kmap_offset;
493 unsigned long kmap_end;
494 unsigned long kmap_num;
496 unsigned int page_offset;
499 bool no_wait = false;
502 kmap_offset = (*f_pos >> PAGE_SHIFT);
503 if (unlikely(kmap_offset >= bo->num_pages))
506 page_offset = *f_pos & ~PAGE_MASK;
507 io_size = bo->num_pages - kmap_offset;
508 io_size = (io_size << PAGE_SHIFT) - page_offset;
512 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
513 kmap_num = kmap_end - kmap_offset + 1;
515 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
526 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
527 if (unlikely(ret != 0)) {
528 ttm_bo_unreserve(bo);
532 virtual = ttm_kmap_obj_virtual(&map, &dummy);
533 virtual += page_offset;
536 ret = copy_from_user(virtual, wbuf, io_size);
538 ret = copy_to_user(rbuf, virtual, io_size);
541 ttm_bo_unreserve(bo);
544 if (unlikely(ret != 0))