1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 /************************************************************************** 3 * 4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 /* 29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 30 */ 31 32 #define pr_fmt(fmt) "[TTM] " fmt 33 34 #include <drm/ttm/ttm_module.h> 35 #include <drm/ttm/ttm_bo_driver.h> 36 #include <drm/ttm/ttm_placement.h> 37 #include <drm/drm_vma_manager.h> 38 #include <linux/mm.h> 39 #include <linux/pfn_t.h> 40 #include <linux/rbtree.h> 41 #include <linux/module.h> 42 #include <linux/uaccess.h> 43 #include <linux/mem_encrypt.h> 44 45 static vm_fault_t ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo, 46 struct vm_fault *vmf) 47 { 48 vm_fault_t ret = 0; 49 int err = 0; 50 51 if (likely(!bo->moving)) 52 goto out_unlock; 53 54 /* 55 * Quick non-stalling check for idle. 56 */ 57 if (dma_fence_is_signaled(bo->moving)) 58 goto out_clear; 59 60 /* 61 * If possible, avoid waiting for GPU with mmap_sem 62 * held. 63 */ 64 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) { 65 ret = VM_FAULT_RETRY; 66 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) 67 goto out_unlock; 68 69 ttm_bo_get(bo); 70 up_read(&vmf->vma->vm_mm->mmap_sem); 71 (void) dma_fence_wait(bo->moving, true); 72 reservation_object_unlock(bo->resv); 73 ttm_bo_put(bo); 74 goto out_unlock; 75 } 76 77 /* 78 * Ordinary wait. 79 */ 80 err = dma_fence_wait(bo->moving, true); 81 if (unlikely(err != 0)) { 82 ret = (err != -ERESTARTSYS) ? VM_FAULT_SIGBUS : 83 VM_FAULT_NOPAGE; 84 goto out_unlock; 85 } 86 87 out_clear: 88 dma_fence_put(bo->moving); 89 bo->moving = NULL; 90 91 out_unlock: 92 return ret; 93 } 94 95 static unsigned long ttm_bo_io_mem_pfn(struct ttm_buffer_object *bo, 96 unsigned long page_offset) 97 { 98 struct ttm_bo_device *bdev = bo->bdev; 99 100 if (bdev->driver->io_mem_pfn) 101 return bdev->driver->io_mem_pfn(bo, page_offset); 102 103 return ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) 104 + page_offset; 105 } 106 107 /** 108 * ttm_bo_vm_reserve - Reserve a buffer object in a retryable vm callback 109 * @bo: The buffer object 110 * @vmf: The fault structure handed to the callback 111 * 112 * vm callbacks like fault() and *_mkwrite() allow for the mm_sem to be dropped 113 * during long waits, and after the wait the callback will be restarted. This 114 * is to allow other threads using the same virtual memory space concurrent 115 * access to map(), unmap() completely unrelated buffer objects. TTM buffer 116 * object reservations sometimes wait for GPU and should therefore be 117 * considered long waits. This function reserves the buffer object interruptibly 118 * taking this into account. Starvation is avoided by the vm system not 119 * allowing too many repeated restarts. 120 * This function is intended to be used in customized fault() and _mkwrite() 121 * handlers. 122 * 123 * Return: 124 * 0 on success and the bo was reserved. 125 * VM_FAULT_RETRY if blocking wait. 126 * VM_FAULT_NOPAGE if blocking wait and retrying was not allowed. 127 */ 128 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo, 129 struct vm_fault *vmf) 130 { 131 /* 132 * Work around locking order reversal in fault / nopfn 133 * between mmap_sem and bo_reserve: Perform a trylock operation 134 * for reserve, and if it fails, retry the fault after waiting 135 * for the buffer to become unreserved. 136 */ 137 if (unlikely(!reservation_object_trylock(bo->resv))) { 138 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) { 139 if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 140 ttm_bo_get(bo); 141 up_read(&vmf->vma->vm_mm->mmap_sem); 142 (void) ttm_bo_wait_unreserved(bo); 143 ttm_bo_put(bo); 144 } 145 146 return VM_FAULT_RETRY; 147 } 148 149 /* 150 * If we'd want to change locking order to 151 * mmap_sem -> bo::reserve, we'd use a blocking reserve here 152 * instead of retrying the fault... 153 */ 154 return VM_FAULT_NOPAGE; 155 } 156 157 return 0; 158 } 159 EXPORT_SYMBOL(ttm_bo_vm_reserve); 160 161 /** 162 * ttm_bo_vm_fault_reserved - TTM fault helper 163 * @vmf: The struct vm_fault given as argument to the fault callback 164 * @prot: The page protection to be used for this memory area. 165 * @num_prefault: Maximum number of prefault pages. The caller may want to 166 * specify this based on madvice settings and the size of the GPU object 167 * backed by the memory. 168 * 169 * This function inserts one or more page table entries pointing to the 170 * memory backing the buffer object, and then returns a return code 171 * instructing the caller to retry the page access. 172 * 173 * Return: 174 * VM_FAULT_NOPAGE on success or pending signal 175 * VM_FAULT_SIGBUS on unspecified error 176 * VM_FAULT_OOM on out-of-memory 177 * VM_FAULT_RETRY if retryable wait 178 */ 179 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, 180 pgprot_t prot, 181 pgoff_t num_prefault) 182 { 183 struct vm_area_struct *vma = vmf->vma; 184 struct vm_area_struct cvma = *vma; 185 struct ttm_buffer_object *bo = (struct ttm_buffer_object *) 186 vma->vm_private_data; 187 struct ttm_bo_device *bdev = bo->bdev; 188 unsigned long page_offset; 189 unsigned long page_last; 190 unsigned long pfn; 191 struct ttm_tt *ttm = NULL; 192 struct page *page; 193 int err; 194 pgoff_t i; 195 vm_fault_t ret = VM_FAULT_NOPAGE; 196 unsigned long address = vmf->address; 197 struct ttm_mem_type_manager *man = 198 &bdev->man[bo->mem.mem_type]; 199 200 /* 201 * Refuse to fault imported pages. This should be handled 202 * (if at all) by redirecting mmap to the exporter. 203 */ 204 if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) 205 return VM_FAULT_SIGBUS; 206 207 if (bdev->driver->fault_reserve_notify) { 208 struct dma_fence *moving = dma_fence_get(bo->moving); 209 210 err = bdev->driver->fault_reserve_notify(bo); 211 switch (err) { 212 case 0: 213 break; 214 case -EBUSY: 215 case -ERESTARTSYS: 216 return VM_FAULT_NOPAGE; 217 default: 218 return VM_FAULT_SIGBUS; 219 } 220 221 if (bo->moving != moving) { 222 spin_lock(&bdev->glob->lru_lock); 223 ttm_bo_move_to_lru_tail(bo, NULL); 224 spin_unlock(&bdev->glob->lru_lock); 225 } 226 dma_fence_put(moving); 227 } 228 229 /* 230 * Wait for buffer data in transit, due to a pipelined 231 * move. 232 */ 233 ret = ttm_bo_vm_fault_idle(bo, vmf); 234 if (unlikely(ret != 0)) 235 return ret; 236 237 err = ttm_mem_io_lock(man, true); 238 if (unlikely(err != 0)) 239 return VM_FAULT_NOPAGE; 240 err = ttm_mem_io_reserve_vm(bo); 241 if (unlikely(err != 0)) { 242 ret = VM_FAULT_SIGBUS; 243 goto out_io_unlock; 244 } 245 246 page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) + 247 vma->vm_pgoff - drm_vma_node_start(&bo->vma_node); 248 page_last = vma_pages(vma) + vma->vm_pgoff - 249 drm_vma_node_start(&bo->vma_node); 250 251 if (unlikely(page_offset >= bo->num_pages)) { 252 ret = VM_FAULT_SIGBUS; 253 goto out_io_unlock; 254 } 255 256 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, prot); 257 if (!bo->mem.bus.is_iomem) { 258 struct ttm_operation_ctx ctx = { 259 .interruptible = false, 260 .no_wait_gpu = false, 261 .flags = TTM_OPT_FLAG_FORCE_ALLOC 262 263 }; 264 265 ttm = bo->ttm; 266 if (ttm_tt_populate(bo->ttm, &ctx)) { 267 ret = VM_FAULT_OOM; 268 goto out_io_unlock; 269 } 270 } else { 271 /* Iomem should not be marked encrypted */ 272 cvma.vm_page_prot = pgprot_decrypted(cvma.vm_page_prot); 273 } 274 275 /* 276 * Speculatively prefault a number of pages. Only error on 277 * first page. 278 */ 279 for (i = 0; i < num_prefault; ++i) { 280 if (bo->mem.bus.is_iomem) { 281 pfn = ttm_bo_io_mem_pfn(bo, page_offset); 282 } else { 283 page = ttm->pages[page_offset]; 284 if (unlikely(!page && i == 0)) { 285 ret = VM_FAULT_OOM; 286 goto out_io_unlock; 287 } else if (unlikely(!page)) { 288 break; 289 } 290 page->index = drm_vma_node_start(&bo->vma_node) + 291 page_offset; 292 pfn = page_to_pfn(page); 293 } 294 295 if (vma->vm_flags & VM_MIXEDMAP) 296 ret = vmf_insert_mixed(&cvma, address, 297 __pfn_to_pfn_t(pfn, PFN_DEV)); 298 else 299 ret = vmf_insert_pfn(&cvma, address, pfn); 300 301 /* 302 * Somebody beat us to this PTE or prefaulting to 303 * an already populated PTE, or prefaulting error. 304 */ 305 306 if (unlikely((ret == VM_FAULT_NOPAGE && i > 0))) 307 break; 308 else if (unlikely(ret & VM_FAULT_ERROR)) 309 goto out_io_unlock; 310 311 address += PAGE_SIZE; 312 if (unlikely(++page_offset >= page_last)) 313 break; 314 } 315 ret = VM_FAULT_NOPAGE; 316 out_io_unlock: 317 ttm_mem_io_unlock(man); 318 return ret; 319 } 320 EXPORT_SYMBOL(ttm_bo_vm_fault_reserved); 321 322 static vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf) 323 { 324 struct vm_area_struct *vma = vmf->vma; 325 pgprot_t prot; 326 struct ttm_buffer_object *bo = vma->vm_private_data; 327 vm_fault_t ret; 328 329 ret = ttm_bo_vm_reserve(bo, vmf); 330 if (ret) 331 return ret; 332 333 prot = vm_get_page_prot(vma->vm_flags); 334 ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT); 335 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) 336 return ret; 337 338 reservation_object_unlock(bo->resv); 339 return ret; 340 } 341 342 static void ttm_bo_vm_open(struct vm_area_struct *vma) 343 { 344 struct ttm_buffer_object *bo = 345 (struct ttm_buffer_object *)vma->vm_private_data; 346 347 WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping); 348 349 ttm_bo_get(bo); 350 } 351 352 static void ttm_bo_vm_close(struct vm_area_struct *vma) 353 { 354 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data; 355 356 ttm_bo_put(bo); 357 vma->vm_private_data = NULL; 358 } 359 360 static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo, 361 unsigned long offset, 362 uint8_t *buf, int len, int write) 363 { 364 unsigned long page = offset >> PAGE_SHIFT; 365 unsigned long bytes_left = len; 366 int ret; 367 368 /* Copy a page at a time, that way no extra virtual address 369 * mapping is needed 370 */ 371 offset -= page << PAGE_SHIFT; 372 do { 373 unsigned long bytes = min(bytes_left, PAGE_SIZE - offset); 374 struct ttm_bo_kmap_obj map; 375 void *ptr; 376 bool is_iomem; 377 378 ret = ttm_bo_kmap(bo, page, 1, &map); 379 if (ret) 380 return ret; 381 382 ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset; 383 WARN_ON_ONCE(is_iomem); 384 if (write) 385 memcpy(ptr, buf, bytes); 386 else 387 memcpy(buf, ptr, bytes); 388 ttm_bo_kunmap(&map); 389 390 page++; 391 buf += bytes; 392 bytes_left -= bytes; 393 offset = 0; 394 } while (bytes_left); 395 396 return len; 397 } 398 399 static int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, 400 void *buf, int len, int write) 401 { 402 unsigned long offset = (addr) - vma->vm_start; 403 struct ttm_buffer_object *bo = vma->vm_private_data; 404 int ret; 405 406 if (len < 1 || (offset + len) >> PAGE_SHIFT > bo->num_pages) 407 return -EIO; 408 409 ret = ttm_bo_reserve(bo, true, false, NULL); 410 if (ret) 411 return ret; 412 413 switch (bo->mem.mem_type) { 414 case TTM_PL_SYSTEM: 415 if (unlikely(bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) { 416 ret = ttm_tt_swapin(bo->ttm); 417 if (unlikely(ret != 0)) 418 return ret; 419 } 420 /* fall through */ 421 case TTM_PL_TT: 422 ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write); 423 break; 424 default: 425 if (bo->bdev->driver->access_memory) 426 ret = bo->bdev->driver->access_memory( 427 bo, offset, buf, len, write); 428 else 429 ret = -EIO; 430 } 431 432 ttm_bo_unreserve(bo); 433 434 return ret; 435 } 436 437 const struct vm_operations_struct ttm_bo_vm_ops = { 438 .fault = ttm_bo_vm_fault, 439 .open = ttm_bo_vm_open, 440 .close = ttm_bo_vm_close, 441 .access = ttm_bo_vm_access 442 }; 443 444 static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev, 445 unsigned long offset, 446 unsigned long pages) 447 { 448 struct drm_vma_offset_node *node; 449 struct ttm_buffer_object *bo = NULL; 450 451 drm_vma_offset_lock_lookup(&bdev->vma_manager); 452 453 node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages); 454 if (likely(node)) { 455 bo = container_of(node, struct ttm_buffer_object, vma_node); 456 bo = ttm_bo_get_unless_zero(bo); 457 } 458 459 drm_vma_offset_unlock_lookup(&bdev->vma_manager); 460 461 if (!bo) 462 pr_err("Could not find buffer object to map\n"); 463 464 return bo; 465 } 466 467 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, 468 struct ttm_bo_device *bdev) 469 { 470 struct ttm_bo_driver *driver; 471 struct ttm_buffer_object *bo; 472 int ret; 473 474 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET_START)) 475 return -EINVAL; 476 477 bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma)); 478 if (unlikely(!bo)) 479 return -EINVAL; 480 481 driver = bo->bdev->driver; 482 if (unlikely(!driver->verify_access)) { 483 ret = -EPERM; 484 goto out_unref; 485 } 486 ret = driver->verify_access(bo, filp); 487 if (unlikely(ret != 0)) 488 goto out_unref; 489 490 vma->vm_ops = bdev->vm_ops; 491 492 /* 493 * Note: We're transferring the bo reference to 494 * vma->vm_private_data here. 495 */ 496 497 vma->vm_private_data = bo; 498 499 /* 500 * We'd like to use VM_PFNMAP on shared mappings, where 501 * (vma->vm_flags & VM_SHARED) != 0, for performance reasons, 502 * but for some reason VM_PFNMAP + x86 PAT + write-combine is very 503 * bad for performance. Until that has been sorted out, use 504 * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719 505 */ 506 vma->vm_flags |= VM_MIXEDMAP; 507 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; 508 return 0; 509 out_unref: 510 ttm_bo_put(bo); 511 return ret; 512 } 513 EXPORT_SYMBOL(ttm_bo_mmap); 514 515 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo) 516 { 517 if (vma->vm_pgoff != 0) 518 return -EACCES; 519 520 ttm_bo_get(bo); 521 522 vma->vm_ops = bo->bdev->vm_ops; 523 vma->vm_private_data = bo; 524 vma->vm_flags |= VM_MIXEDMAP; 525 vma->vm_flags |= VM_IO | VM_DONTEXPAND; 526 return 0; 527 } 528 EXPORT_SYMBOL(ttm_fbdev_mmap); 529