1 /**************************************************************************
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 * Copyright (c) 2013 The FreeBSD Foundation
32 * All rights reserved.
33 *
34 * Portions of this software were developed by Konstantin Belousov
35 * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
36 */
37
38 #include <sys/param.h>
39 #include <sys/pctrie.h>
40 #include "opt_vm.h"
41
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>
46
47 #include <vm/vm.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_pageout.h>
50 #include <vm/vm_radix.h>
51
52 #define TTM_BO_VM_NUM_PREFAULT 16
53
54 RB_GENERATE(ttm_bo_device_buffer_objects, ttm_buffer_object, vm_rb,
55 ttm_bo_cmp_rb_tree_items);
56
57 int
ttm_bo_cmp_rb_tree_items(struct ttm_buffer_object * a,struct ttm_buffer_object * b)58 ttm_bo_cmp_rb_tree_items(struct ttm_buffer_object *a,
59 struct ttm_buffer_object *b)
60 {
61
62 if (a->vm_node->start < b->vm_node->start) {
63 return (-1);
64 } else if (a->vm_node->start > b->vm_node->start) {
65 return (1);
66 } else {
67 return (0);
68 }
69 }
70
ttm_bo_vm_lookup_rb(struct ttm_bo_device * bdev,unsigned long page_start,unsigned long num_pages)71 static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
72 unsigned long page_start,
73 unsigned long num_pages)
74 {
75 unsigned long cur_offset;
76 struct ttm_buffer_object *bo;
77 struct ttm_buffer_object *best_bo = NULL;
78
79 bo = RB_ROOT(&bdev->addr_space_rb);
80 while (bo != NULL) {
81 cur_offset = bo->vm_node->start;
82 if (page_start >= cur_offset) {
83 best_bo = bo;
84 if (page_start == cur_offset)
85 break;
86 bo = RB_RIGHT(bo, vm_rb);
87 } else
88 bo = RB_LEFT(bo, vm_rb);
89 }
90
91 if (unlikely(best_bo == NULL))
92 return NULL;
93
94 if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
95 (page_start + num_pages)))
96 return NULL;
97
98 return best_bo;
99 }
100
101 static int
ttm_bo_vm_fault(vm_object_t vm_obj,vm_ooffset_t offset,int prot,vm_page_t * mres)102 ttm_bo_vm_fault(vm_object_t vm_obj, vm_ooffset_t offset,
103 int prot, vm_page_t *mres)
104 {
105 struct pctrie_iter pages;
106 struct ttm_buffer_object *bo = vm_obj->handle;
107 struct ttm_bo_device *bdev = bo->bdev;
108 struct ttm_tt *ttm = NULL;
109 vm_page_t m, m1;
110 int ret;
111 int retval = VM_PAGER_OK;
112 struct ttm_mem_type_manager *man =
113 &bdev->man[bo->mem.mem_type];
114
115 vm_object_pip_add(vm_obj, 1);
116 if (*mres != NULL) {
117 (void)vm_page_remove(*mres);
118 }
119 vm_page_iter_init(&pages, vm_obj);
120 retry:
121 VM_OBJECT_WUNLOCK(vm_obj);
122 m = NULL;
123
124 reserve:
125 ret = ttm_bo_reserve(bo, false, false, false, 0);
126 if (unlikely(ret != 0)) {
127 if (ret == -EBUSY) {
128 kern_yield(PRI_USER);
129 goto reserve;
130 }
131 }
132
133 if (bdev->driver->fault_reserve_notify) {
134 ret = bdev->driver->fault_reserve_notify(bo);
135 switch (ret) {
136 case 0:
137 break;
138 case -EBUSY:
139 case -ERESTARTSYS:
140 case -EINTR:
141 kern_yield(PRI_USER);
142 goto reserve;
143 default:
144 retval = VM_PAGER_ERROR;
145 goto out_unlock;
146 }
147 }
148
149 /*
150 * Wait for buffer data in transit, due to a pipelined
151 * move.
152 */
153
154 mtx_lock(&bdev->fence_lock);
155 if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
156 /*
157 * Here, the behavior differs between Linux and FreeBSD.
158 *
159 * On Linux, the wait is interruptible (3rd argument to
160 * ttm_bo_wait). There must be some mechanism to resume
161 * page fault handling, once the signal is processed.
162 *
163 * On FreeBSD, the wait is uninteruptible. This is not a
164 * problem as we can't end up with an unkillable process
165 * here, because the wait will eventually time out.
166 *
167 * An example of this situation is the Xorg process
168 * which uses SIGALRM internally. The signal could
169 * interrupt the wait, causing the page fault to fail
170 * and the process to receive SIGSEGV.
171 */
172 ret = ttm_bo_wait(bo, false, false, false);
173 mtx_unlock(&bdev->fence_lock);
174 if (unlikely(ret != 0)) {
175 retval = VM_PAGER_ERROR;
176 goto out_unlock;
177 }
178 } else
179 mtx_unlock(&bdev->fence_lock);
180
181 ret = ttm_mem_io_lock(man, true);
182 if (unlikely(ret != 0)) {
183 retval = VM_PAGER_ERROR;
184 goto out_unlock;
185 }
186 ret = ttm_mem_io_reserve_vm(bo);
187 if (unlikely(ret != 0)) {
188 retval = VM_PAGER_ERROR;
189 goto out_io_unlock;
190 }
191
192 /*
193 * Strictly, we're not allowed to modify vma->vm_page_prot here,
194 * since the mmap_sem is only held in read mode. However, we
195 * modify only the caching bits of vma->vm_page_prot and
196 * consider those bits protected by
197 * the bo->mutex, as we should be the only writers.
198 * There shouldn't really be any readers of these bits except
199 * within vm_insert_mixed()? fork?
200 *
201 * TODO: Add a list of vmas to the bo, and change the
202 * vma->vm_page_prot when the object changes caching policy, with
203 * the correct locks held.
204 */
205 if (!bo->mem.bus.is_iomem) {
206 /* Allocate all page at once, most common usage */
207 ttm = bo->ttm;
208 if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
209 retval = VM_PAGER_ERROR;
210 goto out_io_unlock;
211 }
212 }
213
214 if (bo->mem.bus.is_iomem) {
215 m = PHYS_TO_VM_PAGE(bo->mem.bus.base + bo->mem.bus.offset +
216 offset);
217 KASSERT((m->flags & PG_FICTITIOUS) != 0,
218 ("physical address %#jx not fictitious",
219 (uintmax_t)(bo->mem.bus.base + bo->mem.bus.offset
220 + offset)));
221 pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement));
222 } else {
223 ttm = bo->ttm;
224 m = ttm->pages[OFF_TO_IDX(offset)];
225 if (unlikely(!m)) {
226 retval = VM_PAGER_ERROR;
227 goto out_io_unlock;
228 }
229 pmap_page_set_memattr(m,
230 (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
231 VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement));
232 }
233
234 VM_OBJECT_WLOCK(vm_obj);
235 if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0) {
236 ttm_mem_io_unlock(man);
237 ttm_bo_unreserve(bo);
238 goto retry;
239 }
240 pctrie_iter_reset(&pages);
241 m1 = vm_radix_iter_lookup(&pages, OFF_TO_IDX(offset));
242 /* XXX This looks like it should just be vm_page_replace? */
243 if (m1 == NULL) {
244 if (vm_page_iter_insert(
245 m, vm_obj, OFF_TO_IDX(offset), &pages) != 0) {
246 vm_page_xunbusy(m);
247 VM_OBJECT_WUNLOCK(vm_obj);
248 vm_wait(vm_obj);
249 VM_OBJECT_WLOCK(vm_obj);
250 ttm_mem_io_unlock(man);
251 ttm_bo_unreserve(bo);
252 goto retry;
253 }
254 } else {
255 KASSERT(m == m1,
256 ("inconsistent insert bo %p m %p m1 %p offset %jx",
257 bo, m, m1, (uintmax_t)offset));
258 }
259 vm_page_valid(m);
260 if (*mres != NULL) {
261 KASSERT(*mres != m, ("losing %p %p", *mres, m));
262 vm_page_xunbusy(*mres);
263 vm_page_free(*mres);
264 }
265 *mres = m;
266
267 out_io_unlock1:
268 ttm_mem_io_unlock(man);
269 out_unlock1:
270 ttm_bo_unreserve(bo);
271 vm_object_pip_wakeup(vm_obj);
272 return (retval);
273
274 out_io_unlock:
275 VM_OBJECT_WLOCK(vm_obj);
276 goto out_io_unlock1;
277
278 out_unlock:
279 VM_OBJECT_WLOCK(vm_obj);
280 goto out_unlock1;
281 }
282
283 static int
ttm_bo_vm_ctor(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred,u_short * color)284 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
285 vm_ooffset_t foff, struct ucred *cred, u_short *color)
286 {
287
288 /*
289 * On Linux, a reference to the buffer object is acquired here.
290 * The reason is that this function is not called when the
291 * mmap() is initialized, but only when a process forks for
292 * instance. Therefore on Linux, the reference on the bo is
293 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's
294 * then released in ttm_bo_vm_close().
295 *
296 * Here, this function is called during mmap() initialization.
297 * Thus, the reference acquired in ttm_bo_mmap_single() is
298 * sufficient.
299 */
300
301 *color = 0;
302 return (0);
303 }
304
305 static void
ttm_bo_vm_dtor(void * handle)306 ttm_bo_vm_dtor(void *handle)
307 {
308 struct ttm_buffer_object *bo = handle;
309
310 ttm_bo_unref(&bo);
311 }
312
313 static struct cdev_pager_ops ttm_pager_ops = {
314 .cdev_pg_fault = ttm_bo_vm_fault,
315 .cdev_pg_ctor = ttm_bo_vm_ctor,
316 .cdev_pg_dtor = ttm_bo_vm_dtor
317 };
318
319 int
ttm_bo_mmap_single(struct ttm_bo_device * bdev,vm_ooffset_t * offset,vm_size_t size,struct vm_object ** obj_res,int nprot)320 ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset, vm_size_t size,
321 struct vm_object **obj_res, int nprot)
322 {
323 struct ttm_bo_driver *driver;
324 struct ttm_buffer_object *bo;
325 struct vm_object *vm_obj;
326 int ret;
327
328 rw_wlock(&bdev->vm_lock);
329 bo = ttm_bo_vm_lookup_rb(bdev, OFF_TO_IDX(*offset), OFF_TO_IDX(size));
330 if (likely(bo != NULL))
331 refcount_acquire(&bo->kref);
332 rw_wunlock(&bdev->vm_lock);
333
334 if (unlikely(bo == NULL)) {
335 printf("[TTM] Could not find buffer object to map\n");
336 return (-EINVAL);
337 }
338
339 driver = bo->bdev->driver;
340 if (unlikely(!driver->verify_access)) {
341 ret = -EPERM;
342 goto out_unref;
343 }
344 ret = driver->verify_access(bo);
345 if (unlikely(ret != 0))
346 goto out_unref;
347
348 vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, &ttm_pager_ops,
349 size, nprot, 0, curthread->td_ucred);
350 if (vm_obj == NULL) {
351 ret = -EINVAL;
352 goto out_unref;
353 }
354 /*
355 * Note: We're transferring the bo reference to vm_obj->handle here.
356 */
357 *offset = 0;
358 *obj_res = vm_obj;
359 return 0;
360 out_unref:
361 ttm_bo_unref(&bo);
362 return ret;
363 }
364
365 void
ttm_bo_release_mmap(struct ttm_buffer_object * bo)366 ttm_bo_release_mmap(struct ttm_buffer_object *bo)
367 {
368 vm_object_t vm_obj;
369
370 vm_obj = cdev_pager_lookup(bo);
371 if (vm_obj != NULL) {
372 cdev_mgtdev_pager_free_pages(vm_obj);
373 vm_object_deallocate(vm_obj);
374 }
375 }
376
377 #if 0
378 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
379 {
380 if (vma->vm_pgoff != 0)
381 return -EACCES;
382
383 vma->vm_ops = &ttm_bo_vm_ops;
384 vma->vm_private_data = ttm_bo_reference(bo);
385 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
386 return 0;
387 }
388
389 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
390 const char __user *wbuf, char __user *rbuf, size_t count,
391 loff_t *f_pos, bool write)
392 {
393 struct ttm_buffer_object *bo;
394 struct ttm_bo_driver *driver;
395 struct ttm_bo_kmap_obj map;
396 unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
397 unsigned long kmap_offset;
398 unsigned long kmap_end;
399 unsigned long kmap_num;
400 size_t io_size;
401 unsigned int page_offset;
402 char *virtual;
403 int ret;
404 bool no_wait = false;
405 bool dummy;
406
407 read_lock(&bdev->vm_lock);
408 bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
409 if (likely(bo != NULL))
410 ttm_bo_reference(bo);
411 read_unlock(&bdev->vm_lock);
412
413 if (unlikely(bo == NULL))
414 return -EFAULT;
415
416 driver = bo->bdev->driver;
417 if (unlikely(!driver->verify_access)) {
418 ret = -EPERM;
419 goto out_unref;
420 }
421
422 ret = driver->verify_access(bo, filp);
423 if (unlikely(ret != 0))
424 goto out_unref;
425
426 kmap_offset = dev_offset - bo->vm_node->start;
427 if (unlikely(kmap_offset >= bo->num_pages)) {
428 ret = -EFBIG;
429 goto out_unref;
430 }
431
432 page_offset = *f_pos & ~PAGE_MASK;
433 io_size = bo->num_pages - kmap_offset;
434 io_size = (io_size << PAGE_SHIFT) - page_offset;
435 if (count < io_size)
436 io_size = count;
437
438 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
439 kmap_num = kmap_end - kmap_offset + 1;
440
441 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
442
443 switch (ret) {
444 case 0:
445 break;
446 case -EBUSY:
447 ret = -EAGAIN;
448 goto out_unref;
449 default:
450 goto out_unref;
451 }
452
453 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
454 if (unlikely(ret != 0)) {
455 ttm_bo_unreserve(bo);
456 goto out_unref;
457 }
458
459 virtual = ttm_kmap_obj_virtual(&map, &dummy);
460 virtual += page_offset;
461
462 if (write)
463 ret = copy_from_user(virtual, wbuf, io_size);
464 else
465 ret = copy_to_user(rbuf, virtual, io_size);
466
467 ttm_bo_kunmap(&map);
468 ttm_bo_unreserve(bo);
469 ttm_bo_unref(&bo);
470
471 if (unlikely(ret != 0))
472 return -EFBIG;
473
474 *f_pos += io_size;
475
476 return io_size;
477 out_unref:
478 ttm_bo_unref(&bo);
479 return ret;
480 }
481
482 ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
483 char __user *rbuf, size_t count, loff_t *f_pos,
484 bool write)
485 {
486 struct ttm_bo_kmap_obj map;
487 unsigned long kmap_offset;
488 unsigned long kmap_end;
489 unsigned long kmap_num;
490 size_t io_size;
491 unsigned int page_offset;
492 char *virtual;
493 int ret;
494 bool no_wait = false;
495 bool dummy;
496
497 kmap_offset = (*f_pos >> PAGE_SHIFT);
498 if (unlikely(kmap_offset >= bo->num_pages))
499 return -EFBIG;
500
501 page_offset = *f_pos & ~PAGE_MASK;
502 io_size = bo->num_pages - kmap_offset;
503 io_size = (io_size << PAGE_SHIFT) - page_offset;
504 if (count < io_size)
505 io_size = count;
506
507 kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
508 kmap_num = kmap_end - kmap_offset + 1;
509
510 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
511
512 switch (ret) {
513 case 0:
514 break;
515 case -EBUSY:
516 return -EAGAIN;
517 default:
518 return ret;
519 }
520
521 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
522 if (unlikely(ret != 0)) {
523 ttm_bo_unreserve(bo);
524 return ret;
525 }
526
527 virtual = ttm_kmap_obj_virtual(&map, &dummy);
528 virtual += page_offset;
529
530 if (write)
531 ret = copy_from_user(virtual, wbuf, io_size);
532 else
533 ret = copy_to_user(rbuf, virtual, io_size);
534
535 ttm_bo_kunmap(&map);
536 ttm_bo_unreserve(bo);
537 ttm_bo_unref(&bo);
538
539 if (unlikely(ret != 0))
540 return ret;
541
542 *f_pos += io_size;
543
544 return io_size;
545 }
546 #endif
547