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 $
43 #include <dev/drm/ttm/ttm_module.h>
44 #include <dev/drm/ttm/ttm_bo_driver.h>
45 #include <dev/drm/ttm/ttm_placement.h>
48 #include <vm/vm_page.h>
50 #define TTM_BO_VM_NUM_PREFAULT 16
52 RB_GENERATE(ttm_bo_device_buffer_objects, ttm_buffer_object, vm_rb,
53 ttm_bo_cmp_rb_tree_items);
56 ttm_bo_cmp_rb_tree_items(struct ttm_buffer_object *a,
57 struct ttm_buffer_object *b)
60 if (a->vm_node->start < b->vm_node->start) {
62 } else if (a->vm_node->start > b->vm_node->start) {
69 static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
70 unsigned long page_start,
71 unsigned long num_pages)
73 unsigned long cur_offset;
74 struct ttm_buffer_object *bo;
75 struct ttm_buffer_object *best_bo = NULL;
77 RB_FOREACH(bo, ttm_bo_device_buffer_objects, &bdev->addr_space_rb) {
78 cur_offset = bo->vm_node->start;
79 if (page_start >= cur_offset) {
81 if (page_start == cur_offset)
86 if (unlikely(best_bo == NULL))
89 if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
90 (page_start + num_pages)))
97 ttm_bo_vm_fault(vm_object_t vm_obj, vm_ooffset_t offset,
98 int prot, vm_page_t *mres)
101 struct ttm_buffer_object *bo = vm_obj->handle;
102 struct ttm_bo_device *bdev = bo->bdev;
103 struct ttm_tt *ttm = NULL;
104 vm_page_t m, m1, oldm;
106 int retval = VM_PAGER_OK;
107 struct ttm_mem_type_manager *man =
108 &bdev->man[bo->mem.mem_type];
110 vm_object_pip_add(vm_obj, 1);
113 vm_page_remove(oldm);
118 VM_OBJECT_WUNLOCK(vm_obj);
122 ret = ttm_bo_reserve(bo, false, false, false, 0);
123 if (unlikely(ret != 0)) {
130 if (bdev->driver->fault_reserve_notify) {
131 ret = bdev->driver->fault_reserve_notify(bo);
141 retval = VM_PAGER_ERROR;
147 * Wait for buffer data in transit, due to a pipelined
151 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
152 if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
154 * Here, the behavior differs between Linux and FreeBSD.
156 * On Linux, the wait is interruptible (3rd argument to
157 * ttm_bo_wait). There must be some mechanism to resume
158 * page fault handling, once the signal is processed.
160 * On FreeBSD, the wait is uninteruptible. This is not a
161 * problem as we can't end up with an unkillable process
162 * here, because the wait will eventually time out.
164 * An example of this situation is the Xorg process
165 * which uses SIGALRM internally. The signal could
166 * interrupt the wait, causing the page fault to fail
167 * and the process to receive SIGSEGV.
169 ret = ttm_bo_wait(bo, false, false, false);
170 lockmgr(&bdev->fence_lock, LK_RELEASE);
171 if (unlikely(ret != 0)) {
172 retval = VM_PAGER_ERROR;
176 lockmgr(&bdev->fence_lock, LK_RELEASE);
178 ret = ttm_mem_io_lock(man, true);
179 if (unlikely(ret != 0)) {
180 retval = VM_PAGER_ERROR;
183 ret = ttm_mem_io_reserve_vm(bo);
184 if (unlikely(ret != 0)) {
185 retval = VM_PAGER_ERROR;
190 * Strictly, we're not allowed to modify vma->vm_page_prot here,
191 * since the mmap_sem is only held in read mode. However, we
192 * modify only the caching bits of vma->vm_page_prot and
193 * consider those bits protected by
194 * the bo->mutex, as we should be the only writers.
195 * There shouldn't really be any readers of these bits except
196 * within vm_insert_mixed()? fork?
198 * TODO: Add a list of vmas to the bo, and change the
199 * vma->vm_page_prot when the object changes caching policy, with
200 * the correct locks held.
202 if (!bo->mem.bus.is_iomem) {
203 /* Allocate all page at once, most common usage */
205 if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
206 retval = VM_PAGER_ERROR;
211 if (bo->mem.bus.is_iomem) {
212 m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base +
213 bo->mem.bus.offset + offset);
214 pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement));
217 m = ttm->pages[OFF_TO_IDX(offset)];
219 retval = VM_PAGER_ERROR;
222 pmap_page_set_memattr(m,
223 (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
224 VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement));
227 VM_OBJECT_WLOCK(vm_obj);
228 if ((m->flags & PG_BUSY) != 0) {
230 vm_page_sleep(m, "ttmpbs");
232 ttm_mem_io_unlock(man);
233 ttm_bo_unreserve(bo);
236 m->valid = VM_PAGE_BITS_ALL;
238 m1 = vm_page_lookup(vm_obj, OFF_TO_IDX(offset));
240 vm_page_insert(m, vm_obj, OFF_TO_IDX(offset));
243 ("inconsistent insert bo %p m %p m1 %p offset %jx",
244 bo, m, m1, (uintmax_t)offset));
246 vm_page_busy_try(m, FALSE);
253 ttm_mem_io_unlock(man);
255 ttm_bo_unreserve(bo);
256 vm_object_pip_wakeup(vm_obj);
260 VM_OBJECT_WLOCK(vm_obj);
264 VM_OBJECT_WLOCK(vm_obj);
269 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
270 vm_ooffset_t foff, struct ucred *cred, u_short *color)
274 * On Linux, a reference to the buffer object is acquired here.
275 * The reason is that this function is not called when the
276 * mmap() is initialized, but only when a process forks for
277 * instance. Therefore on Linux, the reference on the bo is
278 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's
279 * then released in ttm_bo_vm_close().
281 * Here, this function is called during mmap() intialization.
282 * Thus, the reference acquired in ttm_bo_mmap_single() is
291 ttm_bo_vm_dtor(void *handle)
293 struct ttm_buffer_object *bo = handle;
298 static struct cdev_pager_ops ttm_pager_ops = {
299 .cdev_pg_fault = ttm_bo_vm_fault,
300 .cdev_pg_ctor = ttm_bo_vm_ctor,
301 .cdev_pg_dtor = ttm_bo_vm_dtor
305 ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset, vm_size_t size,
306 struct vm_object **obj_res, int nprot)
308 struct ttm_bo_driver *driver;
309 struct ttm_buffer_object *bo;
310 struct vm_object *vm_obj;
313 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
314 bo = ttm_bo_vm_lookup_rb(bdev, OFF_TO_IDX(*offset), OFF_TO_IDX(size));
315 if (likely(bo != NULL))
317 lockmgr(&bdev->vm_lock, LK_RELEASE);
319 if (unlikely(bo == NULL)) {
320 kprintf("[TTM] Could not find buffer object to map\n");
324 driver = bo->bdev->driver;
325 if (unlikely(!driver->verify_access)) {
329 ret = -driver->verify_access(bo);
330 if (unlikely(ret != 0))
333 vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, &ttm_pager_ops,
334 size, nprot, 0, curthread->td_ucred);
335 if (vm_obj == NULL) {
340 * Note: We're transferring the bo reference to vm_obj->handle here.
351 ttm_bo_release_mmap(struct ttm_buffer_object *bo)
357 vm_obj = cdev_pager_lookup(bo);
361 VM_OBJECT_WLOCK(vm_obj);
362 for (i = 0; i < bo->num_pages; i++) {
363 m = vm_page_lookup_busy_wait(vm_obj, i, TRUE, "ttm_unm");
366 cdev_pager_free_page(vm_obj, m);
368 VM_OBJECT_WUNLOCK(vm_obj);
370 vm_object_deallocate(vm_obj);
374 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
376 if (vma->vm_pgoff != 0)
379 vma->vm_ops = &ttm_bo_vm_ops;
380 vma->vm_private_data = ttm_bo_reference(bo);
381 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
385 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
386 const char __user *wbuf, char __user *rbuf, size_t count,
387 loff_t *f_pos, bool write)
389 struct ttm_buffer_object *bo;
390 struct ttm_bo_driver *driver;
391 struct ttm_bo_kmap_obj map;
392 unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
393 unsigned long kmap_offset;
394 unsigned long kmap_end;
395 unsigned long kmap_num;
397 unsigned int page_offset;
400 bool no_wait = false;
403 read_lock(&bdev->vm_lock);
404 bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
405 if (likely(bo != NULL))
406 ttm_bo_reference(bo);
407 read_unlock(&bdev->vm_lock);
409 if (unlikely(bo == NULL))
412 driver = bo->bdev->driver;
413 if (unlikely(!driver->verify_access)) {
418 ret = driver->verify_access(bo, filp);
419 if (unlikely(ret != 0))
422 kmap_offset = dev_offset - bo->vm_node->start;
423 if (unlikely(kmap_offset >= bo->num_pages)) {
428 page_offset = *f_pos & ~PAGE_MASK;
429 io_size = bo->num_pages - kmap_offset;
430 io_size = (io_size << PAGE_SHIFT) - page_offset;
434 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
435 kmap_num = kmap_end - kmap_offset + 1;
437 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
449 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
450 if (unlikely(ret != 0)) {
451 ttm_bo_unreserve(bo);
455 virtual = ttm_kmap_obj_virtual(&map, &dummy);
456 virtual += page_offset;
459 ret = copy_from_user(virtual, wbuf, io_size);
461 ret = copy_to_user(rbuf, virtual, io_size);
464 ttm_bo_unreserve(bo);
467 if (unlikely(ret != 0))
478 ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
479 char __user *rbuf, size_t count, loff_t *f_pos,
482 struct ttm_bo_kmap_obj map;
483 unsigned long kmap_offset;
484 unsigned long kmap_end;
485 unsigned long kmap_num;
487 unsigned int page_offset;
490 bool no_wait = false;
493 kmap_offset = (*f_pos >> PAGE_SHIFT);
494 if (unlikely(kmap_offset >= bo->num_pages))
497 page_offset = *f_pos & ~PAGE_MASK;
498 io_size = bo->num_pages - kmap_offset;
499 io_size = (io_size << PAGE_SHIFT) - page_offset;
503 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
504 kmap_num = kmap_end - kmap_offset + 1;
506 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
517 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
518 if (unlikely(ret != 0)) {
519 ttm_bo_unreserve(bo);
523 virtual = ttm_kmap_obj_virtual(&map, &dummy);
524 virtual += page_offset;
527 ret = copy_from_user(virtual, wbuf, io_size);
529 ret = copy_to_user(rbuf, virtual, io_size);
532 ttm_bo_unreserve(bo);
535 if (unlikely(ret != 0))