blob: 668dbe8b8dd3c9e486ebd483942c827dcd8d40a6 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
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:
13 *
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.
17 *
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.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
31#include <ttm/ttm_module.h>
32#include <ttm/ttm_bo_driver.h>
33#include <ttm/ttm_placement.h>
34#include <linux/mm.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020035#include <linux/rbtree.h>
36#include <linux/module.h>
37#include <linux/uaccess.h>
38
39#define TTM_BO_VM_NUM_PREFAULT 16
40
41static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
42 unsigned long page_start,
43 unsigned long num_pages)
44{
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;
49
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;
60 }
61
62 if (unlikely(best_bo == NULL))
63 return NULL;
64
65 if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
66 (page_start + num_pages)))
67 return NULL;
68
69 return best_bo;
70}
71
72static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
73{
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;
90
91 /*
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.
95 */
96
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;
102 }
103
Dave Airliee024e112009-06-24 09:48:08 +1000104 if (bdev->driver->fault_reserve_notify)
105 bdev->driver->fault_reserve_notify(bo);
106
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200107 /*
108 * Wait for buffer data in transit, due to a pipelined
109 * move.
110 */
111
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)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100117 retval = (ret != -ERESTARTSYS) ?
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200118 VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
119 goto out_unlock;
120 }
121 } else
122 spin_unlock(&bo->lock);
123
124
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;
130 }
131
132 is_iomem = (bus_size != 0);
133
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;
138
139 if (unlikely(page_offset >= bo->num_pages)) {
140 retval = VM_FAULT_SIGBUS;
141 goto out_unlock;
142 }
143
144 /*
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?
152 *
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.
156 */
157
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);
166 }
167
168 /*
169 * Speculatively prefault a number of pages. Only error on
170 * first page.
171 */
172
173 for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
174
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;
185 }
186 pfn = page_to_pfn(page);
187 }
188
189 ret = vm_insert_mixed(vma, address, pfn);
190 /*
191 * Somebody beat us to this PTE or prefaulting to
192 * an already populated PTE, or prefaulting error.
193 */
194
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;
201
202 }
203
204 address += PAGE_SIZE;
205 if (unlikely(++page_offset >= page_last))
206 break;
207 }
208
209out_unlock:
210 ttm_bo_unreserve(bo);
211 return retval;
212}
213
214static void ttm_bo_vm_open(struct vm_area_struct *vma)
215{
216 struct ttm_buffer_object *bo =
217 (struct ttm_buffer_object *)vma->vm_private_data;
218
219 (void)ttm_bo_reference(bo);
220}
221
222static void ttm_bo_vm_close(struct vm_area_struct *vma)
223{
224 struct ttm_buffer_object *bo =
225 (struct ttm_buffer_object *)vma->vm_private_data;
226
227 ttm_bo_unref(&bo);
228 vma->vm_private_data = NULL;
229}
230
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400231static const struct vm_operations_struct ttm_bo_vm_ops = {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200232 .fault = ttm_bo_vm_fault,
233 .open = ttm_bo_vm_open,
234 .close = ttm_bo_vm_close
235};
236
237int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
238 struct ttm_bo_device *bdev)
239{
240 struct ttm_bo_driver *driver;
241 struct ttm_buffer_object *bo;
242 int ret;
243
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);
250
251 if (unlikely(bo == NULL)) {
252 printk(KERN_ERR TTM_PFX
253 "Could not find buffer object to map.\n");
254 return -EINVAL;
255 }
256
257 driver = bo->bdev->driver;
258 if (unlikely(!driver->verify_access)) {
259 ret = -EPERM;
260 goto out_unref;
261 }
262 ret = driver->verify_access(bo, filp);
263 if (unlikely(ret != 0))
264 goto out_unref;
265
266 vma->vm_ops = &ttm_bo_vm_ops;
267
268 /*
269 * Note: We're transferring the bo reference to
270 * vma->vm_private_data here.
271 */
272
273 vma->vm_private_data = bo;
274 vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
275 return 0;
276out_unref:
277 ttm_bo_unref(&bo);
278 return ret;
279}
280EXPORT_SYMBOL(ttm_bo_mmap);
281
282int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
283{
284 if (vma->vm_pgoff != 0)
285 return -EACCES;
286
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;
291}
292EXPORT_SYMBOL(ttm_fbdev_mmap);
293
294
295ssize_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)
298{
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;
312
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);
318
319 if (unlikely(bo == NULL))
320 return -EFAULT;
321
322 driver = bo->bdev->driver;
Dave Airlieb8ff7352009-12-15 08:07:12 +1000323 if (unlikely(!driver->verify_access)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200324 ret = -EPERM;
325 goto out_unref;
326 }
327
328 ret = driver->verify_access(bo, filp);
329 if (unlikely(ret != 0))
330 goto out_unref;
331
332 kmap_offset = dev_offset - bo->vm_node->start;
Roel Kluin916635b2009-07-15 16:00:37 +1000333 if (unlikely(kmap_offset >= bo->num_pages)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200334 ret = -EFBIG;
335 goto out_unref;
336 }
337
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;
343
344 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
345 kmap_num = kmap_end - kmap_offset + 1;
346
347 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
348
349 switch (ret) {
350 case 0:
351 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200352 case -EBUSY:
353 ret = -EAGAIN;
354 goto out_unref;
355 default:
356 goto out_unref;
357 }
358
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;
363 }
364
365 virtual = ttm_kmap_obj_virtual(&map, &dummy);
366 virtual += page_offset;
367
368 if (write)
369 ret = copy_from_user(virtual, wbuf, io_size);
370 else
371 ret = copy_to_user(rbuf, virtual, io_size);
372
373 ttm_bo_kunmap(&map);
374 ttm_bo_unreserve(bo);
375 ttm_bo_unref(&bo);
376
377 if (unlikely(ret != 0))
378 return -EFBIG;
379
380 *f_pos += io_size;
381
382 return io_size;
383out_unref:
384 ttm_bo_unref(&bo);
385 return ret;
386}
387
388ssize_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)
391{
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;
402
403 kmap_offset = (*f_pos >> PAGE_SHIFT);
Roel Kluin916635b2009-07-15 16:00:37 +1000404 if (unlikely(kmap_offset >= bo->num_pages))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200405 return -EFBIG;
406
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;
412
413 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
414 kmap_num = kmap_end - kmap_offset + 1;
415
416 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
417
418 switch (ret) {
419 case 0:
420 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200421 case -EBUSY:
422 return -EAGAIN;
423 default:
424 return ret;
425 }
426
427 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
428 if (unlikely(ret != 0)) {
429 ttm_bo_unreserve(bo);
430 return ret;
431 }
432
433 virtual = ttm_kmap_obj_virtual(&map, &dummy);
434 virtual += page_offset;
435
436 if (write)
437 ret = copy_from_user(virtual, wbuf, io_size);
438 else
439 ret = copy_to_user(rbuf, virtual, io_size);
440
441 ttm_bo_kunmap(&map);
442 ttm_bo_unreserve(bo);
443 ttm_bo_unref(&bo);
444
445 if (unlikely(ret != 0))
446 return ret;
447
448 *f_pos += io_size;
449
450 return io_size;
451}