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.
38 #include <linux/efi.h>
41 static void drm_vm_open(struct vm_area_struct
*vma
);
42 static void drm_vm_close(struct vm_area_struct
*vma
);
44 static pgprot_t
drm_io_prot(uint32_t map_type
, struct vm_area_struct
*vma
)
46 pgprot_t tmp
= vm_get_page_prot(vma
->vm_flags
);
48 #if defined(__i386__) || defined(__x86_64__)
49 if (boot_cpu_data
.x86
> 3 && map_type
!= _DRM_AGP
) {
50 pgprot_val(tmp
) |= _PAGE_PCD
;
51 pgprot_val(tmp
) &= ~_PAGE_PWT
;
53 #elif defined(__powerpc__)
54 pgprot_val(tmp
) |= _PAGE_NO_CACHE
;
55 if (map_type
== _DRM_REGISTERS
)
56 pgprot_val(tmp
) |= _PAGE_GUARDED
;
59 if (efi_range_is_wc(vma
->vm_start
, vma
->vm_end
-
61 tmp
= pgprot_writecombine(tmp
);
63 tmp
= pgprot_noncached(tmp
);
69 * \c nopage method for AGP virtual memory.
71 * \param vma virtual memory area.
72 * \param address access address.
73 * \return pointer to the page structure.
75 * Find the right map and if it's AGP memory find the real physical page to
76 * map, get the page, increment the use count and return it.
79 static __inline__
struct page
*drm_do_vm_nopage(struct vm_area_struct
*vma
,
80 unsigned long address
)
82 drm_file_t
*priv
= vma
->vm_file
->private_data
;
83 drm_device_t
*dev
= priv
->head
->dev
;
84 drm_map_t
*map
= NULL
;
85 drm_map_list_t
*r_list
;
86 drm_hash_item_t
*hash
;
91 if (!drm_core_has_AGP(dev
))
94 if (!dev
->agp
|| !dev
->agp
->cant_use_aperture
)
97 if (drm_ht_find_item(&dev
->map_hash
, vma
->vm_pgoff
, &hash
))
100 r_list
= drm_hash_entry(hash
, drm_map_list_t
, hash
);
103 if (map
&& map
->type
== _DRM_AGP
) {
104 unsigned long offset
= address
- vma
->vm_start
;
105 unsigned long baddr
= map
->offset
+ offset
;
106 struct drm_agp_mem
*agpmem
;
111 * Adjust to a bus-relative address
113 baddr
-= dev
->hose
->mem_space
->start
;
117 * It's AGP memory - find the real physical page to map
119 for (agpmem
= dev
->agp
->memory
; agpmem
; agpmem
= agpmem
->next
) {
120 if (agpmem
->bound
<= baddr
&&
121 agpmem
->bound
+ agpmem
->pages
* PAGE_SIZE
> baddr
)
126 goto vm_nopage_error
;
129 * Get the page, inc the use count, and return it
131 offset
= (baddr
- agpmem
->bound
) >> PAGE_SHIFT
;
132 page
= virt_to_page(__va(agpmem
->memory
->memory
[offset
]));
136 ("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
137 baddr
, __va(agpmem
->memory
->memory
[offset
]), offset
,
143 return NOPAGE_SIGBUS
; /* Disallow mremap */
145 #else /* __OS_HAS_AGP */
146 static __inline__
struct page
*drm_do_vm_nopage(struct vm_area_struct
*vma
,
147 unsigned long address
)
149 return NOPAGE_SIGBUS
;
151 #endif /* __OS_HAS_AGP */
154 * \c nopage method for shared virtual memory.
156 * \param vma virtual memory area.
157 * \param address access address.
158 * \return pointer to the page structure.
160 * Get the the mapping, find the real physical page to map, get the page, and
163 static __inline__
struct page
*drm_do_vm_shm_nopage(struct vm_area_struct
*vma
,
164 unsigned long address
)
166 drm_map_t
*map
= (drm_map_t
*) vma
->vm_private_data
;
167 unsigned long offset
;
171 if (address
> vma
->vm_end
)
172 return NOPAGE_SIGBUS
; /* Disallow mremap */
174 return NOPAGE_SIGBUS
; /* Nothing allocated */
176 offset
= address
- vma
->vm_start
;
177 i
= (unsigned long)map
->handle
+ offset
;
178 page
= (map
->type
== _DRM_CONSISTENT
) ?
179 virt_to_page((void *)i
) : vmalloc_to_page((void *)i
);
181 return NOPAGE_SIGBUS
;
184 DRM_DEBUG("shm_nopage 0x%lx\n", address
);
189 * \c close method for shared virtual memory.
191 * \param vma virtual memory area.
193 * Deletes map information if we are the last
194 * person to close a mapping and it's not in the global maplist.
196 static void drm_vm_shm_close(struct vm_area_struct
*vma
)
198 drm_file_t
*priv
= vma
->vm_file
->private_data
;
199 drm_device_t
*dev
= priv
->head
->dev
;
200 drm_vma_entry_t
*pt
, *prev
, *next
;
202 drm_map_list_t
*r_list
;
203 struct list_head
*list
;
206 DRM_DEBUG("0x%08lx,0x%08lx\n",
207 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
208 atomic_dec(&dev
->vma_count
);
210 map
= vma
->vm_private_data
;
212 mutex_lock(&dev
->struct_mutex
);
213 for (pt
= dev
->vmalist
, prev
= NULL
; pt
; pt
= next
) {
215 if (pt
->vma
->vm_private_data
== map
)
217 if (pt
->vma
== vma
) {
219 prev
->next
= pt
->next
;
221 dev
->vmalist
= pt
->next
;
223 drm_free(pt
, sizeof(*pt
), DRM_MEM_VMAS
);
228 /* We were the only map that was found */
229 if (found_maps
== 1 && map
->flags
& _DRM_REMOVABLE
) {
230 /* Check to see if we are in the maplist, if we are not, then
231 * we delete this mappings information.
234 list
= &dev
->maplist
->head
;
235 list_for_each(list
, &dev
->maplist
->head
) {
236 r_list
= list_entry(list
, drm_map_list_t
, head
);
237 if (r_list
->map
== map
)
242 drm_dma_handle_t dmah
;
246 case _DRM_FRAME_BUFFER
:
247 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
249 retcode
= mtrr_del(map
->mtrr
,
252 DRM_DEBUG("mtrr_del = %d\n", retcode
);
254 iounmap(map
->handle
);
260 case _DRM_SCATTER_GATHER
:
262 case _DRM_CONSISTENT
:
263 dmah
.vaddr
= map
->handle
;
264 dmah
.busaddr
= map
->offset
;
265 dmah
.size
= map
->size
;
266 __drm_pci_free(dev
, &dmah
);
269 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
272 mutex_unlock(&dev
->struct_mutex
);
276 * \c nopage method for DMA virtual memory.
278 * \param vma virtual memory area.
279 * \param address access address.
280 * \return pointer to the page structure.
282 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
284 static __inline__
struct page
*drm_do_vm_dma_nopage(struct vm_area_struct
*vma
,
285 unsigned long address
)
287 drm_file_t
*priv
= vma
->vm_file
->private_data
;
288 drm_device_t
*dev
= priv
->head
->dev
;
289 drm_device_dma_t
*dma
= dev
->dma
;
290 unsigned long offset
;
291 unsigned long page_nr
;
295 return NOPAGE_SIGBUS
; /* Error */
296 if (address
> vma
->vm_end
)
297 return NOPAGE_SIGBUS
; /* Disallow mremap */
299 return NOPAGE_SIGBUS
; /* Nothing allocated */
301 offset
= address
- vma
->vm_start
; /* vm_[pg]off[set] should be 0 */
302 page_nr
= offset
>> PAGE_SHIFT
;
303 page
= virt_to_page((dma
->pagelist
[page_nr
] + (offset
& (~PAGE_MASK
))));
307 DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address
, page_nr
);
312 * \c nopage method for scatter-gather virtual memory.
314 * \param vma virtual memory area.
315 * \param address access address.
316 * \return pointer to the page structure.
318 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
320 static __inline__
struct page
*drm_do_vm_sg_nopage(struct vm_area_struct
*vma
,
321 unsigned long address
)
323 drm_map_t
*map
= (drm_map_t
*) vma
->vm_private_data
;
324 drm_file_t
*priv
= vma
->vm_file
->private_data
;
325 drm_device_t
*dev
= priv
->head
->dev
;
326 drm_sg_mem_t
*entry
= dev
->sg
;
327 unsigned long offset
;
328 unsigned long map_offset
;
329 unsigned long page_offset
;
333 return NOPAGE_SIGBUS
; /* Error */
334 if (address
> vma
->vm_end
)
335 return NOPAGE_SIGBUS
; /* Disallow mremap */
336 if (!entry
->pagelist
)
337 return NOPAGE_SIGBUS
; /* Nothing allocated */
339 offset
= address
- vma
->vm_start
;
340 map_offset
= map
->offset
- (unsigned long)dev
->sg
->virtual;
341 page_offset
= (offset
>> PAGE_SHIFT
) + (map_offset
>> PAGE_SHIFT
);
342 page
= entry
->pagelist
[page_offset
];
348 static struct page
*drm_vm_nopage(struct vm_area_struct
*vma
,
349 unsigned long address
, int *type
)
352 *type
= VM_FAULT_MINOR
;
353 return drm_do_vm_nopage(vma
, address
);
356 static struct page
*drm_vm_shm_nopage(struct vm_area_struct
*vma
,
357 unsigned long address
, int *type
)
360 *type
= VM_FAULT_MINOR
;
361 return drm_do_vm_shm_nopage(vma
, address
);
364 static struct page
*drm_vm_dma_nopage(struct vm_area_struct
*vma
,
365 unsigned long address
, int *type
)
368 *type
= VM_FAULT_MINOR
;
369 return drm_do_vm_dma_nopage(vma
, address
);
372 static struct page
*drm_vm_sg_nopage(struct vm_area_struct
*vma
,
373 unsigned long address
, int *type
)
376 *type
= VM_FAULT_MINOR
;
377 return drm_do_vm_sg_nopage(vma
, address
);
380 /** AGP virtual memory operations */
381 static struct vm_operations_struct drm_vm_ops
= {
382 .nopage
= drm_vm_nopage
,
384 .close
= drm_vm_close
,
387 /** Shared virtual memory operations */
388 static struct vm_operations_struct drm_vm_shm_ops
= {
389 .nopage
= drm_vm_shm_nopage
,
391 .close
= drm_vm_shm_close
,
394 /** DMA virtual memory operations */
395 static struct vm_operations_struct drm_vm_dma_ops
= {
396 .nopage
= drm_vm_dma_nopage
,
398 .close
= drm_vm_close
,
401 /** Scatter-gather virtual memory operations */
402 static struct vm_operations_struct drm_vm_sg_ops
= {
403 .nopage
= drm_vm_sg_nopage
,
405 .close
= drm_vm_close
,
409 * \c open method for shared virtual memory.
411 * \param vma virtual memory area.
413 * Create a new drm_vma_entry structure as the \p vma private data entry and
414 * add it to drm_device::vmalist.
416 static void drm_vm_open_locked(struct vm_area_struct
*vma
)
418 drm_file_t
*priv
= vma
->vm_file
->private_data
;
419 drm_device_t
*dev
= priv
->head
->dev
;
420 drm_vma_entry_t
*vma_entry
;
422 DRM_DEBUG("0x%08lx,0x%08lx\n",
423 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
424 atomic_inc(&dev
->vma_count
);
426 vma_entry
= drm_alloc(sizeof(*vma_entry
), DRM_MEM_VMAS
);
428 vma_entry
->vma
= vma
;
429 vma_entry
->next
= dev
->vmalist
;
430 vma_entry
->pid
= current
->pid
;
431 dev
->vmalist
= vma_entry
;
435 static void drm_vm_open(struct vm_area_struct
*vma
)
437 drm_file_t
*priv
= vma
->vm_file
->private_data
;
438 drm_device_t
*dev
= priv
->head
->dev
;
440 mutex_lock(&dev
->struct_mutex
);
441 drm_vm_open_locked(vma
);
442 mutex_unlock(&dev
->struct_mutex
);
446 * \c close method for all virtual memory types.
448 * \param vma virtual memory area.
450 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
453 static void drm_vm_close(struct vm_area_struct
*vma
)
455 drm_file_t
*priv
= vma
->vm_file
->private_data
;
456 drm_device_t
*dev
= priv
->head
->dev
;
457 drm_vma_entry_t
*pt
, *prev
;
459 DRM_DEBUG("0x%08lx,0x%08lx\n",
460 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
461 atomic_dec(&dev
->vma_count
);
463 mutex_lock(&dev
->struct_mutex
);
464 for (pt
= dev
->vmalist
, prev
= NULL
; pt
; prev
= pt
, pt
= pt
->next
) {
465 if (pt
->vma
== vma
) {
467 prev
->next
= pt
->next
;
469 dev
->vmalist
= pt
->next
;
471 drm_free(pt
, sizeof(*pt
), DRM_MEM_VMAS
);
475 mutex_unlock(&dev
->struct_mutex
);
481 * \param filp file pointer.
482 * \param vma virtual memory area.
483 * \return zero on success or a negative number on failure.
485 * Sets the virtual memory area operations structure to vm_dma_ops, the file
486 * pointer, and calls vm_open().
488 static int drm_mmap_dma(struct file
*filp
, struct vm_area_struct
*vma
)
490 drm_file_t
*priv
= filp
->private_data
;
492 drm_device_dma_t
*dma
;
493 unsigned long length
= vma
->vm_end
- vma
->vm_start
;
495 dev
= priv
->head
->dev
;
497 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
498 vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
);
500 /* Length must match exact page count */
501 if (!dma
|| (length
>> PAGE_SHIFT
) != dma
->page_count
) {
505 if (!capable(CAP_SYS_ADMIN
) &&
506 (dma
->flags
& _DRM_DMA_USE_PCI_RO
)) {
507 vma
->vm_flags
&= ~(VM_WRITE
| VM_MAYWRITE
);
508 #if defined(__i386__) || defined(__x86_64__)
509 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_RW
;
511 /* Ye gads this is ugly. With more thought
512 we could move this up higher and use
513 `protection_map' instead. */
517 (__pte(pgprot_val(vma
->vm_page_prot
)))));
521 vma
->vm_ops
= &drm_vm_dma_ops
;
523 vma
->vm_flags
|= VM_RESERVED
; /* Don't swap */
525 vma
->vm_file
= filp
; /* Needed for drm_vm_open() */
526 drm_vm_open_locked(vma
);
530 unsigned long drm_core_get_map_ofs(drm_map_t
* map
)
535 EXPORT_SYMBOL(drm_core_get_map_ofs
);
537 unsigned long drm_core_get_reg_ofs(struct drm_device
*dev
)
540 return dev
->hose
->dense_mem_base
- dev
->hose
->mem_space
->start
;
546 EXPORT_SYMBOL(drm_core_get_reg_ofs
);
551 * \param filp file pointer.
552 * \param vma virtual memory area.
553 * \return zero on success or a negative number on failure.
555 * If the virtual memory area has no offset associated with it then it's a DMA
556 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
557 * checks that the restricted flag is not set, sets the virtual memory operations
558 * according to the mapping type and remaps the pages. Finally sets the file
559 * pointer and calls vm_open().
561 static int drm_mmap_locked(struct file
*filp
, struct vm_area_struct
*vma
)
563 drm_file_t
*priv
= filp
->private_data
;
564 drm_device_t
*dev
= priv
->head
->dev
;
565 drm_map_t
*map
= NULL
;
566 unsigned long offset
= 0;
567 drm_hash_item_t
*hash
;
569 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
570 vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
);
572 if (!priv
->authenticated
)
575 /* We check for "dma". On Apple's UniNorth, it's valid to have
576 * the AGP mapped at physical address 0
582 || dev
->agp
->agp_info
.device
->vendor
!= PCI_VENDOR_ID_APPLE
)
585 return drm_mmap_dma(filp
, vma
);
587 if (drm_ht_find_item(&dev
->map_hash
, vma
->vm_pgoff
, &hash
)) {
588 DRM_ERROR("Could not find map\n");
592 map
= drm_hash_entry(hash
, drm_map_list_t
, hash
)->map
;
593 if (!map
|| ((map
->flags
& _DRM_RESTRICTED
) && !capable(CAP_SYS_ADMIN
)))
596 /* Check for valid size. */
597 if (map
->size
< vma
->vm_end
- vma
->vm_start
)
600 if (!capable(CAP_SYS_ADMIN
) && (map
->flags
& _DRM_READ_ONLY
)) {
601 vma
->vm_flags
&= ~(VM_WRITE
| VM_MAYWRITE
);
602 #if defined(__i386__) || defined(__x86_64__)
603 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_RW
;
605 /* Ye gads this is ugly. With more thought
606 we could move this up higher and use
607 `protection_map' instead. */
611 (__pte(pgprot_val(vma
->vm_page_prot
)))));
617 if (drm_core_has_AGP(dev
) && dev
->agp
->cant_use_aperture
) {
619 * On some platforms we can't talk to bus dma address from the CPU, so for
620 * memory of type DRM_AGP, we'll deal with sorting out the real physical
621 * pages and mappings in nopage()
623 #if defined(__powerpc__)
624 pgprot_val(vma
->vm_page_prot
) |= _PAGE_NO_CACHE
;
626 vma
->vm_ops
= &drm_vm_ops
;
629 /* fall through to _DRM_FRAME_BUFFER... */
630 case _DRM_FRAME_BUFFER
:
632 offset
= dev
->driver
->get_reg_ofs(dev
);
633 vma
->vm_flags
|= VM_IO
; /* not in core dump */
634 vma
->vm_page_prot
= drm_io_prot(map
->type
, vma
);
636 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
637 if (io_remap_pfn_range(vma
, vma
->vm_start
,
638 (map
->offset
+ offset
) >> PAGE_SHIFT
,
639 vma
->vm_end
- vma
->vm_start
,
642 if (io_remap_pfn_range(vma
, vma
->vm_start
,
643 (map
->offset
+ offset
) >> PAGE_SHIFT
,
644 vma
->vm_end
- vma
->vm_start
,
648 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
651 vma
->vm_start
, vma
->vm_end
, map
->offset
+ offset
);
652 vma
->vm_ops
= &drm_vm_ops
;
655 case _DRM_CONSISTENT
:
656 /* Consistent memory is really like shared memory. It's only
657 * allocate in a different way */
658 vma
->vm_ops
= &drm_vm_shm_ops
;
659 vma
->vm_private_data
= (void *)map
;
660 /* Don't let this area swap. Change when
661 DRM_KERNEL advisory is supported. */
662 vma
->vm_flags
|= VM_RESERVED
;
664 case _DRM_SCATTER_GATHER
:
665 vma
->vm_ops
= &drm_vm_sg_ops
;
666 vma
->vm_private_data
= (void *)map
;
667 vma
->vm_flags
|= VM_RESERVED
;
670 return -EINVAL
; /* This should never happen. */
672 vma
->vm_flags
|= VM_RESERVED
; /* Don't swap */
674 vma
->vm_file
= filp
; /* Needed for drm_vm_open() */
675 drm_vm_open_locked(vma
);
679 int drm_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
681 drm_file_t
*priv
= filp
->private_data
;
682 drm_device_t
*dev
= priv
->head
->dev
;
685 mutex_lock(&dev
->struct_mutex
);
686 ret
= drm_mmap_locked(filp
, vma
);
687 mutex_unlock(&dev
->struct_mutex
);
691 EXPORT_SYMBOL(drm_mmap
);