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