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 #include <dev/drm2/drmP.h>
43 #include <dev/drm2/ttm/ttm_module.h>
44 #include <dev/drm2/ttm/ttm_bo_driver.h>
45 #include <dev/drm2/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)) {
153 ret = ttm_bo_wait(bo, false, true, false);
154 lockmgr(&bdev->fence_lock, LK_RELEASE);
155 if (unlikely(ret != 0)) {
156 retval = VM_PAGER_ERROR;
160 lockmgr(&bdev->fence_lock, LK_RELEASE);
162 ret = ttm_mem_io_lock(man, true);
163 if (unlikely(ret != 0)) {
164 retval = VM_PAGER_ERROR;
167 ret = ttm_mem_io_reserve_vm(bo);
168 if (unlikely(ret != 0)) {
169 retval = VM_PAGER_ERROR;
174 * Strictly, we're not allowed to modify vma->vm_page_prot here,
175 * since the mmap_sem is only held in read mode. However, we
176 * modify only the caching bits of vma->vm_page_prot and
177 * consider those bits protected by
178 * the bo->mutex, as we should be the only writers.
179 * There shouldn't really be any readers of these bits except
180 * within vm_insert_mixed()? fork?
182 * TODO: Add a list of vmas to the bo, and change the
183 * vma->vm_page_prot when the object changes caching policy, with
184 * the correct locks held.
186 if (!bo->mem.bus.is_iomem) {
187 /* Allocate all page at once, most common usage */
189 if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
190 retval = VM_PAGER_ERROR;
195 if (bo->mem.bus.is_iomem) {
196 m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base +
197 bo->mem.bus.offset + offset);
198 pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement));
201 m = ttm->pages[OFF_TO_IDX(offset)];
203 retval = VM_PAGER_ERROR;
206 pmap_page_set_memattr(m,
207 (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
208 VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement));
211 VM_OBJECT_WLOCK(vm_obj);
212 if ((m->flags & PG_BUSY) != 0) {
214 vm_page_sleep(m, "ttmpbs");
216 ttm_mem_io_unlock(man);
217 ttm_bo_unreserve(bo);
220 m->valid = VM_PAGE_BITS_ALL;
222 m1 = vm_page_lookup(vm_obj, OFF_TO_IDX(offset));
224 vm_page_insert(m, vm_obj, OFF_TO_IDX(offset));
227 ("inconsistent insert bo %p m %p m1 %p offset %jx",
228 bo, m, m1, (uintmax_t)offset));
230 vm_page_busy_try(m, FALSE);
237 ttm_mem_io_unlock(man);
239 ttm_bo_unreserve(bo);
240 vm_object_pip_wakeup(vm_obj);
244 VM_OBJECT_WLOCK(vm_obj);
248 VM_OBJECT_WLOCK(vm_obj);
253 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
254 vm_ooffset_t foff, struct ucred *cred, u_short *color)
258 * We don't acquire a reference on bo->kref here, because it was
259 * already done in ttm_bo_mmap_single().
267 ttm_bo_vm_dtor(void *handle)
269 struct ttm_buffer_object *bo = handle;
274 static struct cdev_pager_ops ttm_pager_ops = {
275 .cdev_pg_fault = ttm_bo_vm_fault,
276 .cdev_pg_ctor = ttm_bo_vm_ctor,
277 .cdev_pg_dtor = ttm_bo_vm_dtor
281 ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset, vm_size_t size,
282 struct vm_object **obj_res, int nprot)
284 struct ttm_bo_driver *driver;
285 struct ttm_buffer_object *bo;
286 struct vm_object *vm_obj;
289 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
290 bo = ttm_bo_vm_lookup_rb(bdev, OFF_TO_IDX(*offset), OFF_TO_IDX(size));
291 if (likely(bo != NULL))
292 refcount_acquire(&bo->kref);
293 lockmgr(&bdev->vm_lock, LK_RELEASE);
295 if (unlikely(bo == NULL)) {
296 kprintf("[TTM] Could not find buffer object to map\n");
300 driver = bo->bdev->driver;
301 if (unlikely(!driver->verify_access)) {
305 ret = -driver->verify_access(bo);
306 if (unlikely(ret != 0))
309 vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, &ttm_pager_ops,
310 size, nprot, 0, curthread->td_ucred);
311 if (vm_obj == NULL) {
316 * Note: We're transferring the bo reference to vm_obj->handle here.
327 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
329 if (vma->vm_pgoff != 0)
332 vma->vm_ops = &ttm_bo_vm_ops;
333 vma->vm_private_data = ttm_bo_reference(bo);
334 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
338 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
339 const char __user *wbuf, char __user *rbuf, size_t count,
340 loff_t *f_pos, bool write)
342 struct ttm_buffer_object *bo;
343 struct ttm_bo_driver *driver;
344 struct ttm_bo_kmap_obj map;
345 unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
346 unsigned long kmap_offset;
347 unsigned long kmap_end;
348 unsigned long kmap_num;
350 unsigned int page_offset;
353 bool no_wait = false;
356 read_lock(&bdev->vm_lock);
357 bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
358 if (likely(bo != NULL))
359 ttm_bo_reference(bo);
360 read_unlock(&bdev->vm_lock);
362 if (unlikely(bo == NULL))
365 driver = bo->bdev->driver;
366 if (unlikely(!driver->verify_access)) {
371 ret = driver->verify_access(bo, filp);
372 if (unlikely(ret != 0))
375 kmap_offset = dev_offset - bo->vm_node->start;
376 if (unlikely(kmap_offset >= bo->num_pages)) {
381 page_offset = *f_pos & ~PAGE_MASK;
382 io_size = bo->num_pages - kmap_offset;
383 io_size = (io_size << PAGE_SHIFT) - page_offset;
387 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
388 kmap_num = kmap_end - kmap_offset + 1;
390 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
402 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
403 if (unlikely(ret != 0)) {
404 ttm_bo_unreserve(bo);
408 virtual = ttm_kmap_obj_virtual(&map, &dummy);
409 virtual += page_offset;
412 ret = copy_from_user(virtual, wbuf, io_size);
414 ret = copy_to_user(rbuf, virtual, io_size);
417 ttm_bo_unreserve(bo);
420 if (unlikely(ret != 0))
431 ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
432 char __user *rbuf, size_t count, loff_t *f_pos,
435 struct ttm_bo_kmap_obj map;
436 unsigned long kmap_offset;
437 unsigned long kmap_end;
438 unsigned long kmap_num;
440 unsigned int page_offset;
443 bool no_wait = false;
446 kmap_offset = (*f_pos >> PAGE_SHIFT);
447 if (unlikely(kmap_offset >= bo->num_pages))
450 page_offset = *f_pos & ~PAGE_MASK;
451 io_size = bo->num_pages - kmap_offset;
452 io_size = (io_size << PAGE_SHIFT) - page_offset;
456 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
457 kmap_num = kmap_end - kmap_offset + 1;
459 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
470 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
471 if (unlikely(ret != 0)) {
472 ttm_bo_unreserve(bo);
476 virtual = ttm_kmap_obj_virtual(&map, &dummy);
477 virtual += page_offset;
480 ret = copy_from_user(virtual, wbuf, io_size);
482 ret = copy_to_user(rbuf, virtual, io_size);
485 ttm_bo_unreserve(bo);
488 if (unlikely(ret != 0))