3 * Memory mapping for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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
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.
39 #include <linux/efi.h>
42 static void drm_vm_open(struct vm_area_struct *vma);
43 static void drm_vm_close(struct vm_area_struct *vma);
44 static int drm_bo_mmap_locked(struct vm_area_struct *vma,
46 drm_local_map_t *map);
48 pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
50 pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
52 #if defined(__i386__) || defined(__x86_64__)
55 if (drm_use_pat() && map_type == _DRM_TTM) {
56 pgprot_val(tmp) |= _PAGE_PAT;
60 if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
61 pgprot_val(tmp) |= _PAGE_PCD;
62 pgprot_val(tmp) &= ~_PAGE_PWT;
64 #elif defined(__powerpc__)
65 pgprot_val(tmp) |= _PAGE_NO_CACHE;
66 if (map_type == _DRM_REGISTERS)
67 pgprot_val(tmp) |= _PAGE_GUARDED;
70 if (efi_range_is_wc(vma->vm_start, vma->vm_end -
72 tmp = pgprot_writecombine(tmp);
74 tmp = pgprot_noncached(tmp);
81 * \c fault method for AGP virtual memory.
83 * \param vma virtual memory area.
84 * \param address access address.
85 * \return pointer to the page structure.
87 * Find the right map and if it's AGP memory find the real physical page to
88 * map, get the page, increment the use count and return it.
91 static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
93 struct drm_file *priv = vma->vm_file->private_data;
94 struct drm_device *dev = priv->head->dev;
95 struct drm_map *map = NULL;
96 struct drm_map_list *r_list;
97 struct drm_hash_item *hash;
102 if (!drm_core_has_AGP(dev))
105 if (!dev->agp || !dev->agp->cant_use_aperture)
108 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
111 r_list = drm_hash_entry(hash, struct drm_map_list, hash);
114 if (map && map->type == _DRM_AGP) {
116 * Using vm_pgoff as a selector forces us to use this unusual
119 unsigned long offset = (unsigned long)vmf->virtual_address -
121 unsigned long baddr = map->offset + offset;
122 struct drm_agp_mem *agpmem;
127 * Adjust to a bus-relative address
129 baddr -= dev->hose->mem_space->start;
133 * It's AGP memory - find the real physical page to map
135 list_for_each_entry(agpmem, &dev->agp->memory, head) {
136 if (agpmem->bound <= baddr &&
137 agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
145 * Get the page, inc the use count, and return it
147 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
148 page = virt_to_page(__va(agpmem->memory->memory[offset]));
153 ("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
154 baddr, __va(agpmem->memory->memory[offset]), offset,
159 return VM_FAULT_SIGBUS; /* Disallow mremap */
161 #else /* __OS_HAS_AGP */
162 static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
164 return VM_FAULT_SIGBUS;
166 #endif /* __OS_HAS_AGP */
169 * \c nopage method for shared virtual memory.
171 * \param vma virtual memory area.
172 * \param address access address.
173 * \return pointer to the page structure.
175 * Get the mapping, find the real physical page to map, get the page, and
178 static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
180 struct drm_map *map = (struct drm_map *) vma->vm_private_data;
181 unsigned long offset;
186 return VM_FAULT_SIGBUS; /* Nothing allocated */
188 offset = (unsigned long)vmf->virtual_address - vma->vm_start;
189 i = (unsigned long)map->handle + offset;
190 page = vmalloc_to_page((void *)i);
192 return VM_FAULT_SIGBUS;
196 DRM_DEBUG("shm_fault 0x%lx\n", offset);
202 * \c close method for shared virtual memory.
204 * \param vma virtual memory area.
206 * Deletes map information if we are the last
207 * person to close a mapping and it's not in the global maplist.
209 static void drm_vm_shm_close(struct vm_area_struct *vma)
211 struct drm_file *priv = vma->vm_file->private_data;
212 struct drm_device *dev = priv->head->dev;
213 struct drm_vma_entry *pt, *temp;
215 struct drm_map_list *r_list;
218 DRM_DEBUG("0x%08lx,0x%08lx\n",
219 vma->vm_start, vma->vm_end - vma->vm_start);
220 atomic_dec(&dev->vma_count);
222 map = vma->vm_private_data;
224 mutex_lock(&dev->struct_mutex);
225 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
226 if (pt->vma->vm_private_data == map)
228 if (pt->vma == vma) {
230 drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
233 /* We were the only map that was found */
234 if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
235 /* Check to see if we are in the maplist, if we are not, then
236 * we delete this mappings information.
239 list_for_each_entry(r_list, &dev->maplist, head) {
240 if (r_list->map == map)
245 drm_dma_handle_t dmah;
249 case _DRM_FRAME_BUFFER:
250 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
252 retcode = mtrr_del(map->mtrr,
255 DRM_DEBUG("mtrr_del = %d\n", retcode);
257 iounmap(map->handle);
263 case _DRM_SCATTER_GATHER:
265 case _DRM_CONSISTENT:
266 dmah.vaddr = map->handle;
267 dmah.busaddr = map->offset;
268 dmah.size = map->size;
269 __drm_pci_free(dev, &dmah);
275 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
278 mutex_unlock(&dev->struct_mutex);
281 #ifndef DRM_VM_NOPAGE
283 * \c fault method for DMA virtual memory.
285 * \param vma virtual memory area.
286 * \param address access address.
287 * \return pointer to the page structure.
289 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
291 static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
293 struct drm_file *priv = vma->vm_file->private_data;
294 struct drm_device *dev = priv->head->dev;
295 struct drm_device_dma *dma = dev->dma;
296 unsigned long offset;
297 unsigned long page_nr;
301 return VM_FAULT_SIGBUS; /* Error */
303 return VM_FAULT_SIGBUS; /* Nothing allocated */
305 offset = (unsigned long)vmf->virtual_address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
306 page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
307 page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
312 DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
317 * \c fault method for scatter-gather virtual memory.
319 * \param vma virtual memory area.
320 * \param address access address.
321 * \return pointer to the page structure.
323 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
325 static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
327 struct drm_map *map = (struct drm_map *) vma->vm_private_data;
328 struct drm_file *priv = vma->vm_file->private_data;
329 struct drm_device *dev = priv->head->dev;
330 struct drm_sg_mem *entry = dev->sg;
331 unsigned long offset;
332 unsigned long map_offset;
333 unsigned long page_offset;
337 return VM_FAULT_SIGBUS; /* Error */
338 if (!entry->pagelist)
339 return VM_FAULT_SIGBUS; /* Nothing allocated */
341 offset = (unsigned long)vmf->virtual_address - vma->vm_start;
342 map_offset = map->offset - (unsigned long)dev->sg->virtual;
343 page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
344 page = entry->pagelist[page_offset];
352 /** AGP virtual memory operations */
353 static struct vm_operations_struct drm_vm_ops = {
355 .nopage = drm_vm_nopage,
357 .fault = drm_do_vm_fault,
360 .close = drm_vm_close,
363 /** Shared virtual memory operations */
364 static struct vm_operations_struct drm_vm_shm_ops = {
366 .nopage = drm_vm_shm_nopage,
368 .fault = drm_do_vm_shm_fault,
371 .close = drm_vm_shm_close,
374 /** DMA virtual memory operations */
375 static struct vm_operations_struct drm_vm_dma_ops = {
377 .nopage = drm_vm_dma_nopage,
379 .fault = drm_do_vm_dma_fault,
382 .close = drm_vm_close,
385 /** Scatter-gather virtual memory operations */
386 static struct vm_operations_struct drm_vm_sg_ops = {
388 .nopage = drm_vm_sg_nopage,
390 .fault = drm_do_vm_sg_fault,
393 .close = drm_vm_close,
397 * \c open method for shared virtual memory.
399 * \param vma virtual memory area.
401 * Create a new drm_vma_entry structure as the \p vma private data entry and
402 * add it to drm_device::vmalist.
404 static void drm_vm_open_locked(struct vm_area_struct *vma)
406 struct drm_file *priv = vma->vm_file->private_data;
407 struct drm_device *dev = priv->head->dev;
408 struct drm_vma_entry *vma_entry;
410 DRM_DEBUG("0x%08lx,0x%08lx\n",
411 vma->vm_start, vma->vm_end - vma->vm_start);
412 atomic_inc(&dev->vma_count);
414 vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
416 vma_entry->vma = vma;
417 vma_entry->pid = current->pid;
418 list_add(&vma_entry->head, &dev->vmalist);
422 static void drm_vm_open(struct vm_area_struct *vma)
424 struct drm_file *priv = vma->vm_file->private_data;
425 struct drm_device *dev = priv->head->dev;
427 mutex_lock(&dev->struct_mutex);
428 drm_vm_open_locked(vma);
429 mutex_unlock(&dev->struct_mutex);
433 * \c close method for all virtual memory types.
435 * \param vma virtual memory area.
437 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
440 static void drm_vm_close(struct vm_area_struct *vma)
442 struct drm_file *priv = vma->vm_file->private_data;
443 struct drm_device *dev = priv->head->dev;
444 struct drm_vma_entry *pt, *temp;
446 DRM_DEBUG("0x%08lx,0x%08lx\n",
447 vma->vm_start, vma->vm_end - vma->vm_start);
448 atomic_dec(&dev->vma_count);
450 mutex_lock(&dev->struct_mutex);
451 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
452 if (pt->vma == vma) {
454 drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
458 mutex_unlock(&dev->struct_mutex);
465 * \param file_priv DRM file private.
466 * \param vma virtual memory area.
467 * \return zero on success or a negative number on failure.
469 * Sets the virtual memory area operations structure to vm_dma_ops, the file
470 * pointer, and calls vm_open().
472 static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
474 struct drm_file *priv = filp->private_data;
475 struct drm_device *dev;
476 struct drm_device_dma *dma;
477 unsigned long length = vma->vm_end - vma->vm_start;
479 dev = priv->head->dev;
481 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
482 vma->vm_start, vma->vm_end, vma->vm_pgoff);
484 /* Length must match exact page count */
485 if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
489 if (!capable(CAP_SYS_ADMIN) && (dma->flags & _DRM_DMA_USE_PCI_RO)) {
490 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
491 #if defined(__i386__) || defined(__x86_64__)
492 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
494 /* Ye gads this is ugly. With more thought
495 we could move this up higher and use
496 `protection_map' instead. */
500 (__pte(pgprot_val(vma->vm_page_prot)))));
504 vma->vm_ops = &drm_vm_dma_ops;
505 vma->vm_flags |= VM_RESERVED; /* Don't swap */
507 vma->vm_file = filp; /* Needed for drm_vm_open() */
508 drm_vm_open_locked(vma);
512 unsigned long drm_core_get_map_ofs(struct drm_map * map)
516 EXPORT_SYMBOL(drm_core_get_map_ofs);
518 unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
521 return dev->hose->dense_mem_base - dev->hose->mem_space->start;
526 EXPORT_SYMBOL(drm_core_get_reg_ofs);
531 * \param file_priv DRM file private.
532 * \param vma virtual memory area.
533 * \return zero on success or a negative number on failure.
535 * If the virtual memory area has no offset associated with it then it's a DMA
536 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
537 * checks that the restricted flag is not set, sets the virtual memory operations
538 * according to the mapping type and remaps the pages. Finally sets the file
539 * pointer and calls vm_open().
541 static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
543 struct drm_file *priv = filp->private_data;
544 struct drm_device *dev = priv->head->dev;
545 struct drm_map *map = NULL;
546 unsigned long offset = 0;
547 struct drm_hash_item *hash;
549 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
550 vma->vm_start, vma->vm_end, vma->vm_pgoff);
552 if (!priv->authenticated)
555 /* We check for "dma". On Apple's UniNorth, it's valid to have
556 * the AGP mapped at physical address 0
563 || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
566 return drm_mmap_dma(filp, vma);
568 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
569 DRM_ERROR("Could not find map\n");
573 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
574 if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
577 /* Check for valid size. */
578 if (map->size < vma->vm_end - vma->vm_start)
581 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
582 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
583 #if defined(__i386__) || defined(__x86_64__)
584 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
586 /* Ye gads this is ugly. With more thought
587 we could move this up higher and use
588 `protection_map' instead. */
592 (__pte(pgprot_val(vma->vm_page_prot)))));
598 if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
600 * On some platforms we can't talk to bus dma address from the CPU, so for
601 * memory of type DRM_AGP, we'll deal with sorting out the real physical
602 * pages and mappings in nopage()
604 #if defined(__powerpc__)
605 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
607 vma->vm_ops = &drm_vm_ops;
610 /* fall through to _DRM_FRAME_BUFFER... */
611 case _DRM_FRAME_BUFFER:
613 offset = dev->driver->get_reg_ofs(dev);
614 vma->vm_flags |= VM_IO; /* not in core dump */
615 vma->vm_page_prot = drm_io_prot(map->type, vma);
617 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
619 if (io_remap_pfn_range(vma, vma->vm_start,
620 (map->offset + offset) >> PAGE_SHIFT,
621 vma->vm_end - vma->vm_start,
624 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
627 vma->vm_start, vma->vm_end, map->offset + offset);
628 vma->vm_ops = &drm_vm_ops;
630 case _DRM_CONSISTENT:
631 /* Consistent memory is really like shared memory. But
632 * it's allocated in a different way, so avoid nopage */
633 if (remap_pfn_range(vma, vma->vm_start,
634 page_to_pfn(virt_to_page(map->handle)),
635 vma->vm_end - vma->vm_start, vma->vm_page_prot))
637 /* fall through to _DRM_SHM */
639 vma->vm_ops = &drm_vm_shm_ops;
640 vma->vm_private_data = (void *)map;
641 /* Don't let this area swap. Change when
642 DRM_KERNEL advisory is supported. */
643 vma->vm_flags |= VM_RESERVED;
645 case _DRM_SCATTER_GATHER:
646 vma->vm_ops = &drm_vm_sg_ops;
647 vma->vm_private_data = (void *)map;
648 vma->vm_flags |= VM_RESERVED;
651 return drm_bo_mmap_locked(vma, filp, map);
653 return -EINVAL; /* This should never happen. */
655 vma->vm_flags |= VM_RESERVED; /* Don't swap */
657 vma->vm_file = filp; /* Needed for drm_vm_open() */
658 drm_vm_open_locked(vma);
662 int drm_mmap(struct file *filp, struct vm_area_struct *vma)
664 struct drm_file *priv = filp->private_data;
665 struct drm_device *dev = priv->head->dev;
668 mutex_lock(&dev->struct_mutex);
669 ret = drm_mmap_locked(filp, vma);
670 mutex_unlock(&dev->struct_mutex);
674 EXPORT_SYMBOL(drm_mmap);
677 * buffer object vm functions.
681 * \c Pagefault method for buffer objects.
683 * \param vma Virtual memory area.
684 * \param address File offset.
685 * \return Error or refault. The pfn is manually inserted.
687 * It's important that pfns are inserted while holding the bo->mutex lock.
688 * otherwise we might race with unmap_mapping_range() which is always
689 * called with the bo->mutex lock held.
691 * We're modifying the page attribute bits of the vma->vm_page_prot field,
692 * without holding the mmap_sem in write mode. Only in read mode.
693 * These bits are not used by the mm subsystem code, and we consider them
694 * protected by the bo->mutex lock.
696 #ifdef DRM_FULL_MM_COMPAT
697 #define DRM_NOPFN_EXTRA 15 /* Fault 16 pages at a time in */
699 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
700 int drm_bo_vm_fault(struct vm_area_struct *vma,
701 struct vm_fault *vmf)
703 struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
704 unsigned long page_offset;
705 struct page *page = NULL;
706 struct drm_ttm *ttm = NULL;
707 struct drm_device *dev;
710 unsigned long bus_base;
711 unsigned long bus_offset;
712 unsigned long bus_size;
715 unsigned long ret = VM_FAULT_NOPAGE;
717 unsigned long address = (unsigned long)vmf->virtual_address;
719 if (address > vma->vm_end)
720 return VM_FAULT_SIGBUS;
723 err = drm_bo_read_lock(&dev->bm.bm_lock, 1);
725 return VM_FAULT_NOPAGE;
727 err = mutex_lock_interruptible(&bo->mutex);
729 drm_bo_read_unlock(&dev->bm.bm_lock);
730 return VM_FAULT_NOPAGE;
733 err = drm_bo_wait(bo, 0, 0, 0);
735 ret = (err != -EAGAIN) ? VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
740 * If buffer happens to be in a non-mappable location,
741 * move it to a mappable.
744 if (!(bo->mem.flags & DRM_BO_FLAG_MAPPABLE)) {
745 uint32_t new_mask = bo->mem.mask |
746 DRM_BO_FLAG_MAPPABLE |
747 DRM_BO_FLAG_FORCE_MAPPABLE;
748 err = drm_bo_move_buffer(bo, new_mask, 0, 0);
750 ret = (err != -EAGAIN) ? VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
755 err = drm_bo_pci_offset(dev, &bo->mem, &bus_base, &bus_offset,
759 ret = VM_FAULT_SIGBUS;
763 page_offset = (address - vma->vm_start) >> PAGE_SHIFT;
766 struct drm_mem_type_manager *man = &dev->bm.man[bo->mem.mem_type];
768 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) + page_offset;
769 vma->vm_page_prot = drm_io_prot(man->drm_bus_maptype, vma);
773 drm_ttm_fixup_caching(ttm);
774 page = drm_ttm_get_page(ttm, page_offset);
779 pfn = page_to_pfn(page);
780 vma->vm_page_prot = (bo->mem.flags & DRM_BO_FLAG_CACHED) ?
781 vm_get_page_prot(vma->vm_flags) :
782 drm_io_prot(_DRM_TTM, vma);
785 err = vm_insert_pfn(vma, address, pfn);
787 ret = (err != -EAGAIN) ? VM_FAULT_OOM : VM_FAULT_NOPAGE;
791 for (i=0; i<DRM_NOPFN_EXTRA; ++i) {
793 if (++page_offset == bo->mem.num_pages)
795 address = vma->vm_start + (page_offset << PAGE_SHIFT);
796 if (address >= vma->vm_end)
799 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT)
802 page = drm_ttm_get_page(ttm, page_offset);
805 pfn = page_to_pfn(page);
807 if (vm_insert_pfn(vma, address, pfn))
811 mutex_unlock(&bo->mutex);
812 drm_bo_read_unlock(&dev->bm.bm_lock);
816 EXPORT_SYMBOL(drm_bo_vm_fault);
818 int drm_bo_vm_nopfn(struct vm_area_struct *vma,
819 struct vm_fault *vmf )
821 struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
822 unsigned long page_offset;
823 struct page *page = NULL;
824 struct drm_ttm *ttm = NULL;
825 struct drm_device *dev;
828 unsigned long bus_base;
829 unsigned long bus_offset;
830 unsigned long bus_size;
832 unsigned long ret = VM_FAULT_NOPAGE;
834 unsigned long address = (unsigned long)vmf->virtual_address;
836 if (address > vma->vm_end)
837 return VM_FAULT_SIGBUS;
840 err = drm_bo_read_lock(&dev->bm.bm_lock, 1);
842 return VM_FAULT_NOPAGE;
844 err = mutex_lock_interruptible(&bo->mutex);
846 drm_bo_read_unlock(&dev->bm.bm_lock);
847 return VM_FAULT_NOPAGE;
850 err = drm_bo_wait(bo, 0, 0, 0);
852 ret = (err != -EAGAIN) ? VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
857 * If buffer happens to be in a non-mappable location,
858 * move it to a mappable.
861 if (!(bo->mem.flags & DRM_BO_FLAG_MAPPABLE)) {
862 uint32_t new_mask = bo->mem.mask |
863 DRM_BO_FLAG_MAPPABLE |
864 DRM_BO_FLAG_FORCE_MAPPABLE;
865 err = drm_bo_move_buffer(bo, new_mask, 0, 0);
867 ret = (err != -EAGAIN) ? VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
872 err = drm_bo_pci_offset(dev, &bo->mem, &bus_base, &bus_offset,
876 ret = VM_FAULT_SIGBUS;
880 page_offset = (address - vma->vm_start) >> PAGE_SHIFT;
883 struct drm_mem_type_manager *man = &dev->bm.man[bo->mem.mem_type];
885 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) + page_offset;
886 vma->vm_page_prot = drm_io_prot(man->drm_bus_maptype, vma);
890 drm_ttm_fixup_caching(ttm);
891 page = drm_ttm_get_page(ttm, page_offset);
896 pfn = page_to_pfn(page);
897 vma->vm_page_prot = (bo->mem.flags & DRM_BO_FLAG_CACHED) ?
898 vm_get_page_prot(vma->vm_flags) :
899 drm_io_prot(_DRM_TTM, vma);
902 err = vm_insert_pfn(vma, address, pfn);
904 ret = (err != -EAGAIN) ? VM_FAULT_OOM : VM_FAULT_NOPAGE;
908 for (i=0; i<DRM_NOPFN_EXTRA; ++i) {
910 if (++page_offset == bo->mem.num_pages)
912 address = vma->vm_start + (page_offset << PAGE_SHIFT);
913 if (address >= vma->vm_end)
916 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT)
919 page = drm_ttm_get_page(ttm, page_offset);
922 pfn = page_to_pfn(page);
924 if (vm_insert_pfn(vma, address, pfn))
928 mutex_unlock(&bo->mutex);
929 drm_bo_read_unlock(&dev->bm.bm_lock);
934 unsigned long drm_bo_vm_nopfn(struct vm_area_struct *vma,
935 unsigned long address)
937 struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
938 unsigned long page_offset;
939 struct page *page = NULL;
940 struct drm_ttm *ttm = NULL;
941 struct drm_device *dev;
944 unsigned long bus_base;
945 unsigned long bus_offset;
946 unsigned long bus_size;
948 unsigned long ret = NOPFN_REFAULT;
950 if (address > vma->vm_end)
954 err = drm_bo_read_lock(&dev->bm.bm_lock, 1);
956 return NOPFN_REFAULT;
958 err = mutex_lock_interruptible(&bo->mutex);
960 drm_bo_read_unlock(&dev->bm.bm_lock);
961 return NOPFN_REFAULT;
964 err = drm_bo_wait(bo, 0, 0, 0);
966 ret = (err != -EAGAIN) ? NOPFN_SIGBUS : NOPFN_REFAULT;
971 * If buffer happens to be in a non-mappable location,
972 * move it to a mappable.
975 if (!(bo->mem.flags & DRM_BO_FLAG_MAPPABLE)) {
976 uint32_t new_mask = bo->mem.mask |
977 DRM_BO_FLAG_MAPPABLE |
978 DRM_BO_FLAG_FORCE_MAPPABLE;
979 err = drm_bo_move_buffer(bo, new_mask, 0, 0);
981 ret = (err != -EAGAIN) ? NOPFN_SIGBUS : NOPFN_REFAULT;
986 err = drm_bo_pci_offset(dev, &bo->mem, &bus_base, &bus_offset,
994 page_offset = (address - vma->vm_start) >> PAGE_SHIFT;
997 struct drm_mem_type_manager *man = &dev->bm.man[bo->mem.mem_type];
999 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) + page_offset;
1000 vma->vm_page_prot = drm_io_prot(man->drm_bus_maptype, vma);
1004 drm_ttm_fixup_caching(ttm);
1005 page = drm_ttm_get_page(ttm, page_offset);
1010 pfn = page_to_pfn(page);
1011 vma->vm_page_prot = (bo->mem.flags & DRM_BO_FLAG_CACHED) ?
1012 vm_get_page_prot(vma->vm_flags) :
1013 drm_io_prot(_DRM_TTM, vma);
1016 err = vm_insert_pfn(vma, address, pfn);
1018 ret = (err != -EAGAIN) ? NOPFN_OOM : NOPFN_REFAULT;
1022 for (i=0; i<DRM_NOPFN_EXTRA; ++i) {
1024 if (++page_offset == bo->mem.num_pages)
1026 address = vma->vm_start + (page_offset << PAGE_SHIFT);
1027 if (address >= vma->vm_end)
1030 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT)
1033 page = drm_ttm_get_page(ttm, page_offset);
1036 pfn = page_to_pfn(page);
1038 if (vm_insert_pfn(vma, address, pfn))
1042 mutex_unlock(&bo->mutex);
1043 drm_bo_read_unlock(&dev->bm.bm_lock);
1048 EXPORT_SYMBOL(drm_bo_vm_nopfn);
1051 static void drm_bo_vm_open_locked(struct vm_area_struct *vma)
1053 struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
1055 drm_vm_open_locked(vma);
1056 atomic_inc(&bo->usage);
1057 #ifdef DRM_ODD_MM_COMPAT
1058 drm_bo_add_vma(bo, vma);
1063 * \c vma open method for buffer objects.
1065 * \param vma virtual memory area.
1068 static void drm_bo_vm_open(struct vm_area_struct *vma)
1070 struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
1071 struct drm_device *dev = bo->dev;
1073 mutex_lock(&dev->struct_mutex);
1074 drm_bo_vm_open_locked(vma);
1075 mutex_unlock(&dev->struct_mutex);
1078 EXPORT_SYMBOL(drm_bo_vm_open);
1080 * \c vma close method for buffer objects.
1082 * \param vma virtual memory area.
1085 static void drm_bo_vm_close(struct vm_area_struct *vma)
1087 struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
1088 struct drm_device *dev = bo->dev;
1092 mutex_lock(&dev->struct_mutex);
1093 #ifdef DRM_ODD_MM_COMPAT
1094 drm_bo_delete_vma(bo, vma);
1096 drm_bo_usage_deref_locked((struct drm_buffer_object **)
1097 &vma->vm_private_data);
1098 mutex_unlock(&dev->struct_mutex);
1103 EXPORT_SYMBOL(drm_bo_vm_close);
1104 static struct vm_operations_struct drm_bo_vm_ops = {
1105 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
1106 .fault = drm_bo_vm_fault,
1108 #ifdef DRM_FULL_MM_COMPAT
1109 .nopfn = drm_bo_vm_nopfn,
1111 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
1112 .nopfn = drm_bo_vm_nopfn,
1114 .nopage = drm_bo_vm_nopage,
1118 .open = drm_bo_vm_open,
1119 .close = drm_bo_vm_close,
1123 * mmap buffer object memory.
1125 * \param vma virtual memory area.
1126 * \param file_priv DRM file private.
1127 * \param map The buffer object drm map.
1128 * \return zero on success or a negative number on failure.
1131 int drm_bo_mmap_locked(struct vm_area_struct *vma,
1133 drm_local_map_t *map)
1135 vma->vm_ops = &drm_bo_vm_ops;
1136 vma->vm_private_data = map->handle;
1137 vma->vm_file = filp;
1138 vma->vm_flags |= VM_RESERVED | VM_IO;
1139 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
1140 vma->vm_flags |= VM_PFNMAP;
1142 drm_bo_vm_open_locked(vma);
1143 #ifdef DRM_ODD_MM_COMPAT
1144 drm_bo_map_bound(vma);