Staging: netwave: delete the driver
[linux/fpc-iii.git] / drivers / gpu / drm / ttm / ttm_bo_vm.c
blob668dbe8b8dd3c9e486ebd483942c827dcd8d40a6
1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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
16 * of the Software.
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 #include <ttm/ttm_module.h>
32 #include <ttm/ttm_bo_driver.h>
33 #include <ttm/ttm_placement.h>
34 #include <linux/mm.h>
35 #include <linux/rbtree.h>
36 #include <linux/module.h>
37 #include <linux/uaccess.h>
39 #define TTM_BO_VM_NUM_PREFAULT 16
41 static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
42 unsigned long page_start,
43 unsigned long num_pages)
45 struct rb_node *cur = bdev->addr_space_rb.rb_node;
46 unsigned long cur_offset;
47 struct ttm_buffer_object *bo;
48 struct ttm_buffer_object *best_bo = NULL;
50 while (likely(cur != NULL)) {
51 bo = rb_entry(cur, struct ttm_buffer_object, vm_rb);
52 cur_offset = bo->vm_node->start;
53 if (page_start >= cur_offset) {
54 cur = cur->rb_right;
55 best_bo = bo;
56 if (page_start == cur_offset)
57 break;
58 } else
59 cur = cur->rb_left;
62 if (unlikely(best_bo == NULL))
63 return NULL;
65 if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
66 (page_start + num_pages)))
67 return NULL;
69 return best_bo;
72 static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
74 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
75 vma->vm_private_data;
76 struct ttm_bo_device *bdev = bo->bdev;
77 unsigned long bus_base;
78 unsigned long bus_offset;
79 unsigned long bus_size;
80 unsigned long page_offset;
81 unsigned long page_last;
82 unsigned long pfn;
83 struct ttm_tt *ttm = NULL;
84 struct page *page;
85 int ret;
86 int i;
87 bool is_iomem;
88 unsigned long address = (unsigned long)vmf->virtual_address;
89 int retval = VM_FAULT_NOPAGE;
92 * Work around locking order reversal in fault / nopfn
93 * between mmap_sem and bo_reserve: Perform a trylock operation
94 * for reserve, and if it fails, retry the fault after scheduling.
97 ret = ttm_bo_reserve(bo, true, true, false, 0);
98 if (unlikely(ret != 0)) {
99 if (ret == -EBUSY)
100 set_need_resched();
101 return VM_FAULT_NOPAGE;
104 if (bdev->driver->fault_reserve_notify)
105 bdev->driver->fault_reserve_notify(bo);
108 * Wait for buffer data in transit, due to a pipelined
109 * move.
112 spin_lock(&bo->lock);
113 if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
114 ret = ttm_bo_wait(bo, false, true, false);
115 spin_unlock(&bo->lock);
116 if (unlikely(ret != 0)) {
117 retval = (ret != -ERESTARTSYS) ?
118 VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
119 goto out_unlock;
121 } else
122 spin_unlock(&bo->lock);
125 ret = ttm_bo_pci_offset(bdev, &bo->mem, &bus_base, &bus_offset,
126 &bus_size);
127 if (unlikely(ret != 0)) {
128 retval = VM_FAULT_SIGBUS;
129 goto out_unlock;
132 is_iomem = (bus_size != 0);
134 page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
135 bo->vm_node->start - vma->vm_pgoff;
136 page_last = ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) +
137 bo->vm_node->start - vma->vm_pgoff;
139 if (unlikely(page_offset >= bo->num_pages)) {
140 retval = VM_FAULT_SIGBUS;
141 goto out_unlock;
145 * Strictly, we're not allowed to modify vma->vm_page_prot here,
146 * since the mmap_sem is only held in read mode. However, we
147 * modify only the caching bits of vma->vm_page_prot and
148 * consider those bits protected by
149 * the bo->mutex, as we should be the only writers.
150 * There shouldn't really be any readers of these bits except
151 * within vm_insert_mixed()? fork?
153 * TODO: Add a list of vmas to the bo, and change the
154 * vma->vm_page_prot when the object changes caching policy, with
155 * the correct locks held.
158 if (is_iomem) {
159 vma->vm_page_prot = ttm_io_prot(bo->mem.placement,
160 vma->vm_page_prot);
161 } else {
162 ttm = bo->ttm;
163 vma->vm_page_prot = (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
164 vm_get_page_prot(vma->vm_flags) :
165 ttm_io_prot(bo->mem.placement, vma->vm_page_prot);
169 * Speculatively prefault a number of pages. Only error on
170 * first page.
173 for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
175 if (is_iomem)
176 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) +
177 page_offset;
178 else {
179 page = ttm_tt_get_page(ttm, page_offset);
180 if (unlikely(!page && i == 0)) {
181 retval = VM_FAULT_OOM;
182 goto out_unlock;
183 } else if (unlikely(!page)) {
184 break;
186 pfn = page_to_pfn(page);
189 ret = vm_insert_mixed(vma, address, pfn);
191 * Somebody beat us to this PTE or prefaulting to
192 * an already populated PTE, or prefaulting error.
195 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
196 break;
197 else if (unlikely(ret != 0)) {
198 retval =
199 (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
200 goto out_unlock;
204 address += PAGE_SIZE;
205 if (unlikely(++page_offset >= page_last))
206 break;
209 out_unlock:
210 ttm_bo_unreserve(bo);
211 return retval;
214 static void ttm_bo_vm_open(struct vm_area_struct *vma)
216 struct ttm_buffer_object *bo =
217 (struct ttm_buffer_object *)vma->vm_private_data;
219 (void)ttm_bo_reference(bo);
222 static void ttm_bo_vm_close(struct vm_area_struct *vma)
224 struct ttm_buffer_object *bo =
225 (struct ttm_buffer_object *)vma->vm_private_data;
227 ttm_bo_unref(&bo);
228 vma->vm_private_data = NULL;
231 static const struct vm_operations_struct ttm_bo_vm_ops = {
232 .fault = ttm_bo_vm_fault,
233 .open = ttm_bo_vm_open,
234 .close = ttm_bo_vm_close
237 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
238 struct ttm_bo_device *bdev)
240 struct ttm_bo_driver *driver;
241 struct ttm_buffer_object *bo;
242 int ret;
244 read_lock(&bdev->vm_lock);
245 bo = ttm_bo_vm_lookup_rb(bdev, vma->vm_pgoff,
246 (vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
247 if (likely(bo != NULL))
248 ttm_bo_reference(bo);
249 read_unlock(&bdev->vm_lock);
251 if (unlikely(bo == NULL)) {
252 printk(KERN_ERR TTM_PFX
253 "Could not find buffer object to map.\n");
254 return -EINVAL;
257 driver = bo->bdev->driver;
258 if (unlikely(!driver->verify_access)) {
259 ret = -EPERM;
260 goto out_unref;
262 ret = driver->verify_access(bo, filp);
263 if (unlikely(ret != 0))
264 goto out_unref;
266 vma->vm_ops = &ttm_bo_vm_ops;
269 * Note: We're transferring the bo reference to
270 * vma->vm_private_data here.
273 vma->vm_private_data = bo;
274 vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
275 return 0;
276 out_unref:
277 ttm_bo_unref(&bo);
278 return ret;
280 EXPORT_SYMBOL(ttm_bo_mmap);
282 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
284 if (vma->vm_pgoff != 0)
285 return -EACCES;
287 vma->vm_ops = &ttm_bo_vm_ops;
288 vma->vm_private_data = ttm_bo_reference(bo);
289 vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
290 return 0;
292 EXPORT_SYMBOL(ttm_fbdev_mmap);
295 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
296 const char __user *wbuf, char __user *rbuf, size_t count,
297 loff_t *f_pos, bool write)
299 struct ttm_buffer_object *bo;
300 struct ttm_bo_driver *driver;
301 struct ttm_bo_kmap_obj map;
302 unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
303 unsigned long kmap_offset;
304 unsigned long kmap_end;
305 unsigned long kmap_num;
306 size_t io_size;
307 unsigned int page_offset;
308 char *virtual;
309 int ret;
310 bool no_wait = false;
311 bool dummy;
313 read_lock(&bdev->vm_lock);
314 bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
315 if (likely(bo != NULL))
316 ttm_bo_reference(bo);
317 read_unlock(&bdev->vm_lock);
319 if (unlikely(bo == NULL))
320 return -EFAULT;
322 driver = bo->bdev->driver;
323 if (unlikely(!driver->verify_access)) {
324 ret = -EPERM;
325 goto out_unref;
328 ret = driver->verify_access(bo, filp);
329 if (unlikely(ret != 0))
330 goto out_unref;
332 kmap_offset = dev_offset - bo->vm_node->start;
333 if (unlikely(kmap_offset >= bo->num_pages)) {
334 ret = -EFBIG;
335 goto out_unref;
338 page_offset = *f_pos & ~PAGE_MASK;
339 io_size = bo->num_pages - kmap_offset;
340 io_size = (io_size << PAGE_SHIFT) - page_offset;
341 if (count < io_size)
342 io_size = count;
344 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
345 kmap_num = kmap_end - kmap_offset + 1;
347 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
349 switch (ret) {
350 case 0:
351 break;
352 case -EBUSY:
353 ret = -EAGAIN;
354 goto out_unref;
355 default:
356 goto out_unref;
359 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
360 if (unlikely(ret != 0)) {
361 ttm_bo_unreserve(bo);
362 goto out_unref;
365 virtual = ttm_kmap_obj_virtual(&map, &dummy);
366 virtual += page_offset;
368 if (write)
369 ret = copy_from_user(virtual, wbuf, io_size);
370 else
371 ret = copy_to_user(rbuf, virtual, io_size);
373 ttm_bo_kunmap(&map);
374 ttm_bo_unreserve(bo);
375 ttm_bo_unref(&bo);
377 if (unlikely(ret != 0))
378 return -EFBIG;
380 *f_pos += io_size;
382 return io_size;
383 out_unref:
384 ttm_bo_unref(&bo);
385 return ret;
388 ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
389 char __user *rbuf, size_t count, loff_t *f_pos,
390 bool write)
392 struct ttm_bo_kmap_obj map;
393 unsigned long kmap_offset;
394 unsigned long kmap_end;
395 unsigned long kmap_num;
396 size_t io_size;
397 unsigned int page_offset;
398 char *virtual;
399 int ret;
400 bool no_wait = false;
401 bool dummy;
403 kmap_offset = (*f_pos >> PAGE_SHIFT);
404 if (unlikely(kmap_offset >= bo->num_pages))
405 return -EFBIG;
407 page_offset = *f_pos & ~PAGE_MASK;
408 io_size = bo->num_pages - kmap_offset;
409 io_size = (io_size << PAGE_SHIFT) - page_offset;
410 if (count < io_size)
411 io_size = count;
413 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
414 kmap_num = kmap_end - kmap_offset + 1;
416 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
418 switch (ret) {
419 case 0:
420 break;
421 case -EBUSY:
422 return -EAGAIN;
423 default:
424 return ret;
427 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
428 if (unlikely(ret != 0)) {
429 ttm_bo_unreserve(bo);
430 return ret;
433 virtual = ttm_kmap_obj_virtual(&map, &dummy);
434 virtual += page_offset;
436 if (write)
437 ret = copy_from_user(virtual, wbuf, io_size);
438 else
439 ret = copy_to_user(rbuf, virtual, io_size);
441 ttm_bo_kunmap(&map);
442 ttm_bo_unreserve(bo);
443 ttm_bo_unref(&bo);
445 if (unlikely(ret != 0))
446 return ret;
448 *f_pos += io_size;
450 return io_size;