31 #define pr_fmt(fmt) "[TTM] " fmt
37 #include <linux/rbtree.h>
38 #include <linux/module.h>
41 #define TTM_BO_VM_NUM_PREFAULT 16
44 unsigned long page_start,
48 unsigned long cur_offset;
54 cur_offset = bo->
vm_node->start;
55 if (page_start >= cur_offset) {
58 if (page_start == cur_offset)
68 (page_start + num_pages)))
74 static int ttm_bo_vm_fault(
struct vm_area_struct *vma,
struct vm_fault *vmf)
80 unsigned long page_last;
86 unsigned long address = (
unsigned long)vmf->virtual_address;
87 int retval = VM_FAULT_NOPAGE;
101 return VM_FAULT_NOPAGE;
104 if (
bdev->driver->fault_reserve_notify) {
105 ret =
bdev->driver->fault_reserve_notify(bo);
112 retval = VM_FAULT_NOPAGE;
115 retval = VM_FAULT_SIGBUS;
125 spin_lock(&
bdev->fence_lock);
128 spin_unlock(&
bdev->fence_lock);
131 VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
135 spin_unlock(&
bdev->fence_lock);
139 retval = VM_FAULT_NOPAGE;
144 retval = VM_FAULT_SIGBUS;
154 retval = VM_FAULT_SIGBUS;
171 if (bo->
mem.bus.is_iomem) {
181 if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
182 retval = VM_FAULT_OOM;
192 if (bo->
mem.bus.is_iomem)
197 retval = VM_FAULT_OOM;
215 (
ret == -
ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
235 (
void)ttm_bo_reference(bo);
246 static const struct vm_operations_struct ttm_bo_vm_ops = {
247 .fault = ttm_bo_vm_fault,
248 .open = ttm_bo_vm_open,
249 .close = ttm_bo_vm_close
260 bo = ttm_bo_vm_lookup_rb(bdev, vma->
vm_pgoff,
263 ttm_bo_reference(bo);
267 pr_err(
"Could not find buffer object to map\n");
271 driver = bo->
bdev->driver;
280 vma->
vm_ops = &ttm_bo_vm_ops;
288 vma->
vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
301 vma->
vm_ops = &ttm_bo_vm_ops;
303 vma->
vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
310 const char __user *wbuf,
char __user *rbuf,
size_t count,
311 loff_t *f_pos,
bool write)
316 unsigned long dev_offset = (*f_pos >>
PAGE_SHIFT);
317 unsigned long kmap_offset;
318 unsigned long kmap_end;
319 unsigned long kmap_num;
324 bool no_wait =
false;
328 bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
330 ttm_bo_reference(bo);
336 driver = bo->
bdev->driver;
346 kmap_offset = dev_offset - bo->
vm_node->start;
354 io_size = (io_size <<
PAGE_SHIFT) - page_offset;
358 kmap_end = (*f_pos + count - 1) >>
PAGE_SHIFT;
359 kmap_num = kmap_end - kmap_offset + 1;
373 ret =
ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
379 virtual = ttm_kmap_obj_virtual(&map, &dummy);
403 char __user *rbuf,
size_t count, loff_t *f_pos,
407 unsigned long kmap_offset;
408 unsigned long kmap_end;
409 unsigned long kmap_num;
414 bool no_wait =
false;
423 io_size = (io_size <<
PAGE_SHIFT) - page_offset;
427 kmap_end = (*f_pos + count - 1) >>
PAGE_SHIFT;
428 kmap_num = kmap_end - kmap_offset + 1;
441 ret =
ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
447 virtual = ttm_kmap_obj_virtual(&map, &dummy);