1 /** 2 * \file drm_vm.c 3 * Memory mapping for DRM 4 * 5 * \author Rickard E. (Rik) Faith <faith@valinux.com> 6 * \author Gareth Hughes <gareth@valinux.com> 7 */ 8 9 /* 10 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com 11 * 12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 14 * All Rights Reserved. 15 * 16 * Permission is hereby granted, free of charge, to any person obtaining a 17 * copy of this software and associated documentation files (the "Software"), 18 * to deal in the Software without restriction, including without limitation 19 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 20 * and/or sell copies of the Software, and to permit persons to whom the 21 * Software is furnished to do so, subject to the following conditions: 22 * 23 * The above copyright notice and this permission notice (including the next 24 * paragraph) shall be included in all copies or substantial portions of the 25 * Software. 26 * 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 33 * OTHER DEALINGS IN THE SOFTWARE. 34 */ 35 36 #include <linux/export.h> 37 #include <linux/pci.h> 38 #include <linux/seq_file.h> 39 #include <linux/vmalloc.h> 40 41 #if defined(__ia64__) 42 #include <linux/efi.h> 43 #include <linux/slab.h> 44 #endif 45 #include <linux/mem_encrypt.h> 46 47 #include <asm/pgtable.h> 48 49 #include <drm/drm_agpsupport.h> 50 #include <drm/drm_device.h> 51 #include <drm/drm_drv.h> 52 #include <drm/drm_file.h> 53 #include <drm/drm_framebuffer.h> 54 #include <drm/drm_gem.h> 55 #include <drm/drm_print.h> 56 57 #include "drm_internal.h" 58 #include "drm_legacy.h" 59 60 struct drm_vma_entry { 61 struct list_head head; 62 struct vm_area_struct *vma; 63 pid_t pid; 64 }; 65 66 static void drm_vm_open(struct vm_area_struct *vma); 67 static void drm_vm_close(struct vm_area_struct *vma); 68 69 static pgprot_t drm_io_prot(struct drm_local_map *map, 70 struct vm_area_struct *vma) 71 { 72 pgprot_t tmp = vm_get_page_prot(vma->vm_flags); 73 74 /* We don't want graphics memory to be mapped encrypted */ 75 tmp = pgprot_decrypted(tmp); 76 77 #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) 78 if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING)) 79 tmp = pgprot_noncached(tmp); 80 else 81 tmp = pgprot_writecombine(tmp); 82 #elif defined(__ia64__) 83 if (efi_range_is_wc(vma->vm_start, vma->vm_end - 84 vma->vm_start)) 85 tmp = pgprot_writecombine(tmp); 86 else 87 tmp = pgprot_noncached(tmp); 88 #elif defined(__sparc__) || defined(__arm__) || defined(__mips__) 89 tmp = pgprot_noncached(tmp); 90 #endif 91 return tmp; 92 } 93 94 static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma) 95 { 96 pgprot_t tmp = vm_get_page_prot(vma->vm_flags); 97 98 #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE) 99 tmp = pgprot_noncached_wc(tmp); 100 #endif 101 return tmp; 102 } 103 104 /** 105 * \c fault method for AGP virtual memory. 106 * 107 * \param vma virtual memory area. 108 * \param address access address. 109 * \return pointer to the page structure. 110 * 111 * Find the right map and if it's AGP memory find the real physical page to 112 * map, get the page, increment the use count and return it. 113 */ 114 #if IS_ENABLED(CONFIG_AGP) 115 static vm_fault_t drm_vm_fault(struct vm_fault *vmf) 116 { 117 struct vm_area_struct *vma = vmf->vma; 118 struct drm_file *priv = vma->vm_file->private_data; 119 struct drm_device *dev = priv->minor->dev; 120 struct drm_local_map *map = NULL; 121 struct drm_map_list *r_list; 122 struct drm_hash_item *hash; 123 124 /* 125 * Find the right map 126 */ 127 if (!dev->agp) 128 goto vm_fault_error; 129 130 if (!dev->agp || !dev->agp->cant_use_aperture) 131 goto vm_fault_error; 132 133 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) 134 goto vm_fault_error; 135 136 r_list = drm_hash_entry(hash, struct drm_map_list, hash); 137 map = r_list->map; 138 139 if (map && map->type == _DRM_AGP) { 140 /* 141 * Using vm_pgoff as a selector forces us to use this unusual 142 * addressing scheme. 143 */ 144 resource_size_t offset = vmf->address - vma->vm_start; 145 resource_size_t baddr = map->offset + offset; 146 struct drm_agp_mem *agpmem; 147 struct page *page; 148 149 #ifdef __alpha__ 150 /* 151 * Adjust to a bus-relative address 152 */ 153 baddr -= dev->hose->mem_space->start; 154 #endif 155 156 /* 157 * It's AGP memory - find the real physical page to map 158 */ 159 list_for_each_entry(agpmem, &dev->agp->memory, head) { 160 if (agpmem->bound <= baddr && 161 agpmem->bound + agpmem->pages * PAGE_SIZE > baddr) 162 break; 163 } 164 165 if (&agpmem->head == &dev->agp->memory) 166 goto vm_fault_error; 167 168 /* 169 * Get the page, inc the use count, and return it 170 */ 171 offset = (baddr - agpmem->bound) >> PAGE_SHIFT; 172 page = agpmem->memory->pages[offset]; 173 get_page(page); 174 vmf->page = page; 175 176 DRM_DEBUG 177 ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n", 178 (unsigned long long)baddr, 179 agpmem->memory->pages[offset], 180 (unsigned long long)offset, 181 page_count(page)); 182 return 0; 183 } 184 vm_fault_error: 185 return VM_FAULT_SIGBUS; /* Disallow mremap */ 186 } 187 #else 188 static vm_fault_t drm_vm_fault(struct vm_fault *vmf) 189 { 190 return VM_FAULT_SIGBUS; 191 } 192 #endif 193 194 /** 195 * \c nopage method for shared virtual memory. 196 * 197 * \param vma virtual memory area. 198 * \param address access address. 199 * \return pointer to the page structure. 200 * 201 * Get the mapping, find the real physical page to map, get the page, and 202 * return it. 203 */ 204 static vm_fault_t drm_vm_shm_fault(struct vm_fault *vmf) 205 { 206 struct vm_area_struct *vma = vmf->vma; 207 struct drm_local_map *map = vma->vm_private_data; 208 unsigned long offset; 209 unsigned long i; 210 struct page *page; 211 212 if (!map) 213 return VM_FAULT_SIGBUS; /* Nothing allocated */ 214 215 offset = vmf->address - vma->vm_start; 216 i = (unsigned long)map->handle + offset; 217 page = vmalloc_to_page((void *)i); 218 if (!page) 219 return VM_FAULT_SIGBUS; 220 get_page(page); 221 vmf->page = page; 222 223 DRM_DEBUG("shm_fault 0x%lx\n", offset); 224 return 0; 225 } 226 227 /** 228 * \c close method for shared virtual memory. 229 * 230 * \param vma virtual memory area. 231 * 232 * Deletes map information if we are the last 233 * person to close a mapping and it's not in the global maplist. 234 */ 235 static void drm_vm_shm_close(struct vm_area_struct *vma) 236 { 237 struct drm_file *priv = vma->vm_file->private_data; 238 struct drm_device *dev = priv->minor->dev; 239 struct drm_vma_entry *pt, *temp; 240 struct drm_local_map *map; 241 struct drm_map_list *r_list; 242 int found_maps = 0; 243 244 DRM_DEBUG("0x%08lx,0x%08lx\n", 245 vma->vm_start, vma->vm_end - vma->vm_start); 246 247 map = vma->vm_private_data; 248 249 mutex_lock(&dev->struct_mutex); 250 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { 251 if (pt->vma->vm_private_data == map) 252 found_maps++; 253 if (pt->vma == vma) { 254 list_del(&pt->head); 255 kfree(pt); 256 } 257 } 258 259 /* We were the only map that was found */ 260 if (found_maps == 1 && map->flags & _DRM_REMOVABLE) { 261 /* Check to see if we are in the maplist, if we are not, then 262 * we delete this mappings information. 263 */ 264 found_maps = 0; 265 list_for_each_entry(r_list, &dev->maplist, head) { 266 if (r_list->map == map) 267 found_maps++; 268 } 269 270 if (!found_maps) { 271 drm_dma_handle_t dmah; 272 273 switch (map->type) { 274 case _DRM_REGISTERS: 275 case _DRM_FRAME_BUFFER: 276 arch_phys_wc_del(map->mtrr); 277 iounmap(map->handle); 278 break; 279 case _DRM_SHM: 280 vfree(map->handle); 281 break; 282 case _DRM_AGP: 283 case _DRM_SCATTER_GATHER: 284 break; 285 case _DRM_CONSISTENT: 286 dmah.vaddr = map->handle; 287 dmah.busaddr = map->offset; 288 dmah.size = map->size; 289 __drm_legacy_pci_free(dev, &dmah); 290 break; 291 } 292 kfree(map); 293 } 294 } 295 mutex_unlock(&dev->struct_mutex); 296 } 297 298 /** 299 * \c fault method for DMA virtual memory. 300 * 301 * \param address access address. 302 * \return pointer to the page structure. 303 * 304 * Determine the page number from the page offset and get it from drm_device_dma::pagelist. 305 */ 306 static vm_fault_t drm_vm_dma_fault(struct vm_fault *vmf) 307 { 308 struct vm_area_struct *vma = vmf->vma; 309 struct drm_file *priv = vma->vm_file->private_data; 310 struct drm_device *dev = priv->minor->dev; 311 struct drm_device_dma *dma = dev->dma; 312 unsigned long offset; 313 unsigned long page_nr; 314 struct page *page; 315 316 if (!dma) 317 return VM_FAULT_SIGBUS; /* Error */ 318 if (!dma->pagelist) 319 return VM_FAULT_SIGBUS; /* Nothing allocated */ 320 321 offset = vmf->address - vma->vm_start; 322 /* vm_[pg]off[set] should be 0 */ 323 page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */ 324 page = virt_to_page((void *)dma->pagelist[page_nr]); 325 326 get_page(page); 327 vmf->page = page; 328 329 DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr); 330 return 0; 331 } 332 333 /** 334 * \c fault method for scatter-gather virtual memory. 335 * 336 * \param address access address. 337 * \return pointer to the page structure. 338 * 339 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist. 340 */ 341 static vm_fault_t drm_vm_sg_fault(struct vm_fault *vmf) 342 { 343 struct vm_area_struct *vma = vmf->vma; 344 struct drm_local_map *map = vma->vm_private_data; 345 struct drm_file *priv = vma->vm_file->private_data; 346 struct drm_device *dev = priv->minor->dev; 347 struct drm_sg_mem *entry = dev->sg; 348 unsigned long offset; 349 unsigned long map_offset; 350 unsigned long page_offset; 351 struct page *page; 352 353 if (!entry) 354 return VM_FAULT_SIGBUS; /* Error */ 355 if (!entry->pagelist) 356 return VM_FAULT_SIGBUS; /* Nothing allocated */ 357 358 offset = vmf->address - vma->vm_start; 359 map_offset = map->offset - (unsigned long)dev->sg->virtual; 360 page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT); 361 page = entry->pagelist[page_offset]; 362 get_page(page); 363 vmf->page = page; 364 365 return 0; 366 } 367 368 /** AGP virtual memory operations */ 369 static const struct vm_operations_struct drm_vm_ops = { 370 .fault = drm_vm_fault, 371 .open = drm_vm_open, 372 .close = drm_vm_close, 373 }; 374 375 /** Shared virtual memory operations */ 376 static const struct vm_operations_struct drm_vm_shm_ops = { 377 .fault = drm_vm_shm_fault, 378 .open = drm_vm_open, 379 .close = drm_vm_shm_close, 380 }; 381 382 /** DMA virtual memory operations */ 383 static const struct vm_operations_struct drm_vm_dma_ops = { 384 .fault = drm_vm_dma_fault, 385 .open = drm_vm_open, 386 .close = drm_vm_close, 387 }; 388 389 /** Scatter-gather virtual memory operations */ 390 static const struct vm_operations_struct drm_vm_sg_ops = { 391 .fault = drm_vm_sg_fault, 392 .open = drm_vm_open, 393 .close = drm_vm_close, 394 }; 395 396 static void drm_vm_open_locked(struct drm_device *dev, 397 struct vm_area_struct *vma) 398 { 399 struct drm_vma_entry *vma_entry; 400 401 DRM_DEBUG("0x%08lx,0x%08lx\n", 402 vma->vm_start, vma->vm_end - vma->vm_start); 403 404 vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL); 405 if (vma_entry) { 406 vma_entry->vma = vma; 407 vma_entry->pid = current->pid; 408 list_add(&vma_entry->head, &dev->vmalist); 409 } 410 } 411 412 static void drm_vm_open(struct vm_area_struct *vma) 413 { 414 struct drm_file *priv = vma->vm_file->private_data; 415 struct drm_device *dev = priv->minor->dev; 416 417 mutex_lock(&dev->struct_mutex); 418 drm_vm_open_locked(dev, vma); 419 mutex_unlock(&dev->struct_mutex); 420 } 421 422 static void drm_vm_close_locked(struct drm_device *dev, 423 struct vm_area_struct *vma) 424 { 425 struct drm_vma_entry *pt, *temp; 426 427 DRM_DEBUG("0x%08lx,0x%08lx\n", 428 vma->vm_start, vma->vm_end - vma->vm_start); 429 430 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { 431 if (pt->vma == vma) { 432 list_del(&pt->head); 433 kfree(pt); 434 break; 435 } 436 } 437 } 438 439 /** 440 * \c close method for all virtual memory types. 441 * 442 * \param vma virtual memory area. 443 * 444 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and 445 * free it. 446 */ 447 static void drm_vm_close(struct vm_area_struct *vma) 448 { 449 struct drm_file *priv = vma->vm_file->private_data; 450 struct drm_device *dev = priv->minor->dev; 451 452 mutex_lock(&dev->struct_mutex); 453 drm_vm_close_locked(dev, vma); 454 mutex_unlock(&dev->struct_mutex); 455 } 456 457 /** 458 * mmap DMA memory. 459 * 460 * \param file_priv DRM file private. 461 * \param vma virtual memory area. 462 * \return zero on success or a negative number on failure. 463 * 464 * Sets the virtual memory area operations structure to vm_dma_ops, the file 465 * pointer, and calls vm_open(). 466 */ 467 static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma) 468 { 469 struct drm_file *priv = filp->private_data; 470 struct drm_device *dev; 471 struct drm_device_dma *dma; 472 unsigned long length = vma->vm_end - vma->vm_start; 473 474 dev = priv->minor->dev; 475 dma = dev->dma; 476 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", 477 vma->vm_start, vma->vm_end, vma->vm_pgoff); 478 479 /* Length must match exact page count */ 480 if (!dma || (length >> PAGE_SHIFT) != dma->page_count) { 481 return -EINVAL; 482 } 483 484 if (!capable(CAP_SYS_ADMIN) && 485 (dma->flags & _DRM_DMA_USE_PCI_RO)) { 486 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); 487 #if defined(__i386__) || defined(__x86_64__) 488 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; 489 #else 490 /* Ye gads this is ugly. With more thought 491 we could move this up higher and use 492 `protection_map' instead. */ 493 vma->vm_page_prot = 494 __pgprot(pte_val 495 (pte_wrprotect 496 (__pte(pgprot_val(vma->vm_page_prot))))); 497 #endif 498 } 499 500 vma->vm_ops = &drm_vm_dma_ops; 501 502 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 503 504 drm_vm_open_locked(dev, vma); 505 return 0; 506 } 507 508 static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev) 509 { 510 #ifdef __alpha__ 511 return dev->hose->dense_mem_base; 512 #else 513 return 0; 514 #endif 515 } 516 517 /** 518 * mmap DMA memory. 519 * 520 * \param file_priv DRM file private. 521 * \param vma virtual memory area. 522 * \return zero on success or a negative number on failure. 523 * 524 * If the virtual memory area has no offset associated with it then it's a DMA 525 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist, 526 * checks that the restricted flag is not set, sets the virtual memory operations 527 * according to the mapping type and remaps the pages. Finally sets the file 528 * pointer and calls vm_open(). 529 */ 530 static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma) 531 { 532 struct drm_file *priv = filp->private_data; 533 struct drm_device *dev = priv->minor->dev; 534 struct drm_local_map *map = NULL; 535 resource_size_t offset = 0; 536 struct drm_hash_item *hash; 537 538 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", 539 vma->vm_start, vma->vm_end, vma->vm_pgoff); 540 541 if (!priv->authenticated) 542 return -EACCES; 543 544 /* We check for "dma". On Apple's UniNorth, it's valid to have 545 * the AGP mapped at physical address 0 546 * --BenH. 547 */ 548 if (!vma->vm_pgoff 549 #if IS_ENABLED(CONFIG_AGP) 550 && (!dev->agp 551 || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE) 552 #endif 553 ) 554 return drm_mmap_dma(filp, vma); 555 556 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) { 557 DRM_ERROR("Could not find map\n"); 558 return -EINVAL; 559 } 560 561 map = drm_hash_entry(hash, struct drm_map_list, hash)->map; 562 if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) 563 return -EPERM; 564 565 /* Check for valid size. */ 566 if (map->size < vma->vm_end - vma->vm_start) 567 return -EINVAL; 568 569 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) { 570 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); 571 #if defined(__i386__) || defined(__x86_64__) 572 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; 573 #else 574 /* Ye gads this is ugly. With more thought 575 we could move this up higher and use 576 `protection_map' instead. */ 577 vma->vm_page_prot = 578 __pgprot(pte_val 579 (pte_wrprotect 580 (__pte(pgprot_val(vma->vm_page_prot))))); 581 #endif 582 } 583 584 switch (map->type) { 585 #if !defined(__arm__) 586 case _DRM_AGP: 587 if (dev->agp && dev->agp->cant_use_aperture) { 588 /* 589 * On some platforms we can't talk to bus dma address from the CPU, so for 590 * memory of type DRM_AGP, we'll deal with sorting out the real physical 591 * pages and mappings in fault() 592 */ 593 #if defined(__powerpc__) 594 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 595 #endif 596 vma->vm_ops = &drm_vm_ops; 597 break; 598 } 599 #endif 600 /* fall through - to _DRM_FRAME_BUFFER... */ 601 case _DRM_FRAME_BUFFER: 602 case _DRM_REGISTERS: 603 offset = drm_core_get_reg_ofs(dev); 604 vma->vm_page_prot = drm_io_prot(map, vma); 605 if (io_remap_pfn_range(vma, vma->vm_start, 606 (map->offset + offset) >> PAGE_SHIFT, 607 vma->vm_end - vma->vm_start, 608 vma->vm_page_prot)) 609 return -EAGAIN; 610 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx," 611 " offset = 0x%llx\n", 612 map->type, 613 vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset)); 614 615 vma->vm_ops = &drm_vm_ops; 616 break; 617 case _DRM_CONSISTENT: 618 /* Consistent memory is really like shared memory. But 619 * it's allocated in a different way, so avoid fault */ 620 if (remap_pfn_range(vma, vma->vm_start, 621 page_to_pfn(virt_to_page(map->handle)), 622 vma->vm_end - vma->vm_start, vma->vm_page_prot)) 623 return -EAGAIN; 624 vma->vm_page_prot = drm_dma_prot(map->type, vma); 625 /* fall through - to _DRM_SHM */ 626 case _DRM_SHM: 627 vma->vm_ops = &drm_vm_shm_ops; 628 vma->vm_private_data = (void *)map; 629 break; 630 case _DRM_SCATTER_GATHER: 631 vma->vm_ops = &drm_vm_sg_ops; 632 vma->vm_private_data = (void *)map; 633 vma->vm_page_prot = drm_dma_prot(map->type, vma); 634 break; 635 default: 636 return -EINVAL; /* This should never happen. */ 637 } 638 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 639 640 drm_vm_open_locked(dev, vma); 641 return 0; 642 } 643 644 int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma) 645 { 646 struct drm_file *priv = filp->private_data; 647 struct drm_device *dev = priv->minor->dev; 648 int ret; 649 650 if (drm_dev_is_unplugged(dev)) 651 return -ENODEV; 652 653 mutex_lock(&dev->struct_mutex); 654 ret = drm_mmap_locked(filp, vma); 655 mutex_unlock(&dev->struct_mutex); 656 657 return ret; 658 } 659 EXPORT_SYMBOL(drm_legacy_mmap); 660 661 #if IS_ENABLED(CONFIG_DRM_LEGACY) 662 void drm_legacy_vma_flush(struct drm_device *dev) 663 { 664 struct drm_vma_entry *vma, *vma_temp; 665 666 /* Clear vma list (only needed for legacy drivers) */ 667 list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) { 668 list_del(&vma->head); 669 kfree(vma); 670 } 671 } 672 #endif 673