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/errno.h>
50 #include <linux/export.h>
51 #include <linux/rbtree.h>
53 #define TTM_BO_VM_NUM_PREFAULT 16
55 static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
56 unsigned long page_start,
57 unsigned long num_pages)
59 struct rb_node *cur = bdev->addr_space_rb.rb_node;
60 unsigned long cur_offset;
61 struct ttm_buffer_object *bo;
62 struct ttm_buffer_object *best_bo = NULL;
64 while (likely(cur != NULL)) {
65 bo = rb_entry(cur, struct ttm_buffer_object, vm_rb);
66 cur_offset = bo->vm_node->start;
67 if (page_start >= cur_offset) {
70 if (page_start == cur_offset)
76 if (unlikely(best_bo == NULL))
79 if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
80 (page_start + num_pages)))
87 ttm_bo_vm_fault(vm_object_t vm_obj, vm_ooffset_t offset,
88 int prot, vm_page_t *mres)
90 struct ttm_buffer_object *bo = vm_obj->handle;
91 struct ttm_bo_device *bdev = bo->bdev;
92 struct ttm_tt *ttm = NULL;
93 vm_page_t m, m1, oldm;
95 int retval = VM_PAGER_OK;
96 struct ttm_mem_type_manager *man =
97 &bdev->man[bo->mem.mem_type];
99 vm_object_pip_add(vm_obj, 1);
102 vm_page_remove(oldm);
107 VM_OBJECT_WUNLOCK(vm_obj);
111 ret = ttm_bo_reserve(bo, false, false, false, 0);
112 if (unlikely(ret != 0)) {
119 if (bdev->driver->fault_reserve_notify) {
120 ret = bdev->driver->fault_reserve_notify(bo);
130 retval = VM_PAGER_ERROR;
136 * Wait for buffer data in transit, due to a pipelined
140 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
141 if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
143 * Here, the behavior differs between Linux and FreeBSD.
145 * On Linux, the wait is interruptible (3rd argument to
146 * ttm_bo_wait). There must be some mechanism to resume
147 * page fault handling, once the signal is processed.
149 * On FreeBSD, the wait is uninteruptible. This is not a
150 * problem as we can't end up with an unkillable process
151 * here, because the wait will eventually time out.
153 * An example of this situation is the Xorg process
154 * which uses SIGALRM internally. The signal could
155 * interrupt the wait, causing the page fault to fail
156 * and the process to receive SIGSEGV.
158 ret = ttm_bo_wait(bo, false, false, false);
159 lockmgr(&bdev->fence_lock, LK_RELEASE);
160 if (unlikely(ret != 0)) {
161 retval = VM_PAGER_ERROR;
165 lockmgr(&bdev->fence_lock, LK_RELEASE);
167 ret = ttm_mem_io_lock(man, true);
168 if (unlikely(ret != 0)) {
169 retval = VM_PAGER_ERROR;
172 ret = ttm_mem_io_reserve_vm(bo);
173 if (unlikely(ret != 0)) {
174 retval = VM_PAGER_ERROR;
179 * Strictly, we're not allowed to modify vma->vm_page_prot here,
180 * since the mmap_sem is only held in read mode. However, we
181 * modify only the caching bits of vma->vm_page_prot and
182 * consider those bits protected by
183 * the bo->mutex, as we should be the only writers.
184 * There shouldn't really be any readers of these bits except
185 * within vm_insert_mixed()? fork?
187 * TODO: Add a list of vmas to the bo, and change the
188 * vma->vm_page_prot when the object changes caching policy, with
189 * the correct locks held.
191 if (!bo->mem.bus.is_iomem) {
192 /* Allocate all page at once, most common usage */
194 if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
195 retval = VM_PAGER_ERROR;
200 if (bo->mem.bus.is_iomem) {
201 m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base +
202 bo->mem.bus.offset + offset);
203 pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement));
206 m = ttm->pages[OFF_TO_IDX(offset)];
208 retval = VM_PAGER_ERROR;
211 pmap_page_set_memattr(m,
212 (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
213 VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement));
216 VM_OBJECT_WLOCK(vm_obj);
217 if ((m->flags & PG_BUSY) != 0) {
219 vm_page_sleep(m, "ttmpbs");
221 ttm_mem_io_unlock(man);
222 ttm_bo_unreserve(bo);
225 m->valid = VM_PAGE_BITS_ALL;
227 m1 = vm_page_lookup(vm_obj, OFF_TO_IDX(offset));
229 vm_page_insert(m, vm_obj, OFF_TO_IDX(offset));
232 ("inconsistent insert bo %p m %p m1 %p offset %jx",
233 bo, m, m1, (uintmax_t)offset));
235 vm_page_busy_try(m, FALSE);
242 ttm_mem_io_unlock(man);
244 ttm_bo_unreserve(bo);
245 vm_object_pip_wakeup(vm_obj);
249 VM_OBJECT_WLOCK(vm_obj);
253 VM_OBJECT_WLOCK(vm_obj);
258 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
259 vm_ooffset_t foff, struct ucred *cred, u_short *color)
263 * On Linux, a reference to the buffer object is acquired here.
264 * The reason is that this function is not called when the
265 * mmap() is initialized, but only when a process forks for
266 * instance. Therefore on Linux, the reference on the bo is
267 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's
268 * then released in ttm_bo_vm_close().
270 * Here, this function is called during mmap() intialization.
271 * Thus, the reference acquired in ttm_bo_mmap_single() is
279 ttm_bo_vm_dtor(void *handle)
281 struct ttm_buffer_object *bo = handle;
286 static struct cdev_pager_ops ttm_pager_ops = {
287 .cdev_pg_fault = ttm_bo_vm_fault,
288 .cdev_pg_ctor = ttm_bo_vm_ctor,
289 .cdev_pg_dtor = ttm_bo_vm_dtor
293 ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset, vm_size_t size,
294 struct vm_object **obj_res, int nprot)
296 struct ttm_bo_driver *driver;
297 struct ttm_buffer_object *bo;
298 struct vm_object *vm_obj;
303 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
304 bo = ttm_bo_vm_lookup_rb(bdev, OFF_TO_IDX(*offset), OFF_TO_IDX(size));
305 if (likely(bo != NULL))
307 lockmgr(&bdev->vm_lock, LK_RELEASE);
309 if (unlikely(bo == NULL)) {
310 kprintf("[TTM] Could not find buffer object to map\n");
314 driver = bo->bdev->driver;
315 if (unlikely(!driver->verify_access)) {
319 ret = -driver->verify_access(bo);
320 if (unlikely(ret != 0))
323 vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, &ttm_pager_ops,
324 size, nprot, 0, curthread->td_ucred);
326 if (vm_obj == NULL) {
331 * Note: We're transferring the bo reference to vm_obj->handle here.
340 EXPORT_SYMBOL(ttm_bo_mmap);
343 ttm_bo_release_mmap(struct ttm_buffer_object *bo)
349 vm_obj = cdev_pager_lookup(bo);
353 VM_OBJECT_WLOCK(vm_obj);
354 for (i = 0; i < bo->num_pages; i++) {
355 m = vm_page_lookup_busy_wait(vm_obj, i, TRUE, "ttm_unm");
358 cdev_pager_free_page(vm_obj, m);
360 VM_OBJECT_WUNLOCK(vm_obj);
362 vm_object_deallocate(vm_obj);
366 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
368 if (vma->vm_pgoff != 0)
371 vma->vm_ops = &ttm_bo_vm_ops;
372 vma->vm_private_data = ttm_bo_reference(bo);
373 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
376 EXPORT_SYMBOL(ttm_fbdev_mmap);
379 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
380 const char __user *wbuf, char __user *rbuf, size_t count,
381 loff_t *f_pos, bool write)
383 struct ttm_buffer_object *bo;
384 struct ttm_bo_driver *driver;
385 struct ttm_bo_kmap_obj map;
386 unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
387 unsigned long kmap_offset;
388 unsigned long kmap_end;
389 unsigned long kmap_num;
391 unsigned int page_offset;
394 bool no_wait = false;
397 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
398 bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
399 if (likely(bo != NULL))
400 ttm_bo_reference(bo);
401 lockmgr(&bdev->vm_lock, LK_RELEASE);
403 if (unlikely(bo == NULL))
406 driver = bo->bdev->driver;
407 if (unlikely(!driver->verify_access)) {
412 ret = driver->verify_access(bo, filp);
413 if (unlikely(ret != 0))
416 kmap_offset = dev_offset - bo->vm_node->start;
417 if (unlikely(kmap_offset >= bo->num_pages)) {
422 page_offset = *f_pos & ~PAGE_MASK;
423 io_size = bo->num_pages - kmap_offset;
424 io_size = (io_size << PAGE_SHIFT) - page_offset;
428 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
429 kmap_num = kmap_end - kmap_offset + 1;
431 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
443 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
444 if (unlikely(ret != 0)) {
445 ttm_bo_unreserve(bo);
449 virtual = ttm_kmap_obj_virtual(&map, &dummy);
450 virtual += page_offset;
453 ret = copy_from_user(virtual, wbuf, io_size);
455 ret = copy_to_user(rbuf, virtual, io_size);
458 ttm_bo_unreserve(bo);
461 if (unlikely(ret != 0))
472 ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
473 char __user *rbuf, size_t count, loff_t *f_pos,
476 struct ttm_bo_kmap_obj map;
477 unsigned long kmap_offset;
478 unsigned long kmap_end;
479 unsigned long kmap_num;
481 unsigned int page_offset;
484 bool no_wait = false;
487 kmap_offset = (*f_pos >> PAGE_SHIFT);
488 if (unlikely(kmap_offset >= bo->num_pages))
491 page_offset = *f_pos & ~PAGE_MASK;
492 io_size = bo->num_pages - kmap_offset;
493 io_size = (io_size << PAGE_SHIFT) - page_offset;
497 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
498 kmap_num = kmap_end - kmap_offset + 1;
500 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
511 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
512 if (unlikely(ret != 0)) {
513 ttm_bo_unreserve(bo);
517 virtual = ttm_kmap_obj_virtual(&map, &dummy);
518 virtual += page_offset;
521 ret = copy_from_user(virtual, wbuf, io_size);
523 ret = copy_to_user(rbuf, virtual, io_size);
526 ttm_bo_unreserve(bo);
529 if (unlikely(ret != 0))