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
);
45 * \c nopage method for AGP virtual memory.
47 * \param vma virtual memory area.
48 * \param address access address.
49 * \return pointer to the page structure.
51 * Find the right map and if it's AGP memory find the real physical page to
52 * map, get the page, increment the use count and return it.
55 static __inline__
struct page
*drm_do_vm_nopage(struct vm_area_struct
*vma
,
56 unsigned long address
)
58 drm_file_t
*priv
= vma
->vm_file
->private_data
;
59 drm_device_t
*dev
= priv
->head
->dev
;
60 drm_map_t
*map
= NULL
;
61 drm_map_list_t
*r_list
;
62 struct list_head
*list
;
67 if (!drm_core_has_AGP(dev
))
70 if(!dev
->agp
|| !dev
->agp
->cant_use_aperture
) goto vm_nopage_error
;
72 list_for_each(list
, &dev
->maplist
->head
) {
73 r_list
= list_entry(list
, drm_map_list_t
, head
);
76 if (map
->offset
== VM_OFFSET(vma
)) break;
79 if (map
&& map
->type
== _DRM_AGP
) {
80 unsigned long offset
= address
- vma
->vm_start
;
81 unsigned long baddr
= VM_OFFSET(vma
) + offset
;
82 struct drm_agp_mem
*agpmem
;
87 * Adjust to a bus-relative address
89 baddr
-= dev
->hose
->mem_space
->start
;
93 * It's AGP memory - find the real physical page to map
95 for(agpmem
= dev
->agp
->memory
; agpmem
; agpmem
= agpmem
->next
) {
96 if (agpmem
->bound
<= baddr
&&
97 agpmem
->bound
+ agpmem
->pages
* PAGE_SIZE
> baddr
)
101 if (!agpmem
) goto vm_nopage_error
;
104 * Get the page, inc the use count, and return it
106 offset
= (baddr
- agpmem
->bound
) >> PAGE_SHIFT
;
107 page
= virt_to_page(__va(agpmem
->memory
->memory
[offset
]));
110 DRM_DEBUG("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
111 baddr
, __va(agpmem
->memory
->memory
[offset
]), offset
,
117 return NOPAGE_SIGBUS
; /* Disallow mremap */
119 #else /* __OS_HAS_AGP */
120 static __inline__
struct page
*drm_do_vm_nopage(struct vm_area_struct
*vma
,
121 unsigned long address
)
123 return NOPAGE_SIGBUS
;
125 #endif /* __OS_HAS_AGP */
128 * \c nopage method for shared virtual memory.
130 * \param vma virtual memory area.
131 * \param address access address.
132 * \return pointer to the page structure.
134 * Get the the mapping, find the real physical page to map, get the page, and
137 static __inline__
struct page
*drm_do_vm_shm_nopage(struct vm_area_struct
*vma
,
138 unsigned long address
)
140 drm_map_t
*map
= (drm_map_t
*)vma
->vm_private_data
;
141 unsigned long offset
;
145 if (address
> vma
->vm_end
) return NOPAGE_SIGBUS
; /* Disallow mremap */
146 if (!map
) return NOPAGE_OOM
; /* Nothing allocated */
148 offset
= address
- vma
->vm_start
;
149 i
= (unsigned long)map
->handle
+ offset
;
150 page
= vmalloc_to_page((void *)i
);
155 DRM_DEBUG("shm_nopage 0x%lx\n", address
);
161 * \c close method for shared virtual memory.
163 * \param vma virtual memory area.
165 * Deletes map information if we are the last
166 * person to close a mapping and it's not in the global maplist.
168 static void drm_vm_shm_close(struct vm_area_struct
*vma
)
170 drm_file_t
*priv
= vma
->vm_file
->private_data
;
171 drm_device_t
*dev
= priv
->head
->dev
;
172 drm_vma_entry_t
*pt
, *prev
, *next
;
174 drm_map_list_t
*r_list
;
175 struct list_head
*list
;
178 DRM_DEBUG("0x%08lx,0x%08lx\n",
179 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
180 atomic_dec(&dev
->vma_count
);
182 map
= vma
->vm_private_data
;
184 down(&dev
->struct_sem
);
185 for (pt
= dev
->vmalist
, prev
= NULL
; pt
; pt
= next
) {
187 if (pt
->vma
->vm_private_data
== map
) found_maps
++;
188 if (pt
->vma
== vma
) {
190 prev
->next
= pt
->next
;
192 dev
->vmalist
= pt
->next
;
194 drm_free(pt
, sizeof(*pt
), DRM_MEM_VMAS
);
199 /* We were the only map that was found */
200 if(found_maps
== 1 &&
201 map
->flags
& _DRM_REMOVABLE
) {
202 /* Check to see if we are in the maplist, if we are not, then
203 * we delete this mappings information.
206 list
= &dev
->maplist
->head
;
207 list_for_each(list
, &dev
->maplist
->head
) {
208 r_list
= list_entry(list
, drm_map_list_t
, head
);
209 if (r_list
->map
== map
) found_maps
++;
215 case _DRM_FRAME_BUFFER
:
216 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
218 retcode
= mtrr_del(map
->mtrr
,
221 DRM_DEBUG("mtrr_del = %d\n", retcode
);
223 drm_ioremapfree(map
->handle
, map
->size
, dev
);
229 case _DRM_SCATTER_GATHER
:
232 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
235 up(&dev
->struct_sem
);
239 * \c nopage method for DMA virtual memory.
241 * \param vma virtual memory area.
242 * \param address access address.
243 * \return pointer to the page structure.
245 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
247 static __inline__
struct page
*drm_do_vm_dma_nopage(struct vm_area_struct
*vma
,
248 unsigned long address
)
250 drm_file_t
*priv
= vma
->vm_file
->private_data
;
251 drm_device_t
*dev
= priv
->head
->dev
;
252 drm_device_dma_t
*dma
= dev
->dma
;
253 unsigned long offset
;
254 unsigned long page_nr
;
257 if (!dma
) return NOPAGE_SIGBUS
; /* Error */
258 if (address
> vma
->vm_end
) return NOPAGE_SIGBUS
; /* Disallow mremap */
259 if (!dma
->pagelist
) return NOPAGE_OOM
; /* Nothing allocated */
261 offset
= address
- vma
->vm_start
; /* vm_[pg]off[set] should be 0 */
262 page_nr
= offset
>> PAGE_SHIFT
;
263 page
= virt_to_page((dma
->pagelist
[page_nr
] +
264 (offset
& (~PAGE_MASK
))));
268 DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address
, page_nr
);
273 * \c nopage method for scatter-gather virtual memory.
275 * \param vma virtual memory area.
276 * \param address access address.
277 * \return pointer to the page structure.
279 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
281 static __inline__
struct page
*drm_do_vm_sg_nopage(struct vm_area_struct
*vma
,
282 unsigned long address
)
284 drm_map_t
*map
= (drm_map_t
*)vma
->vm_private_data
;
285 drm_file_t
*priv
= vma
->vm_file
->private_data
;
286 drm_device_t
*dev
= priv
->head
->dev
;
287 drm_sg_mem_t
*entry
= dev
->sg
;
288 unsigned long offset
;
289 unsigned long map_offset
;
290 unsigned long page_offset
;
293 if (!entry
) return NOPAGE_SIGBUS
; /* Error */
294 if (address
> vma
->vm_end
) return NOPAGE_SIGBUS
; /* Disallow mremap */
295 if (!entry
->pagelist
) return NOPAGE_OOM
; /* Nothing allocated */
298 offset
= address
- vma
->vm_start
;
299 map_offset
= map
->offset
- dev
->sg
->handle
;
300 page_offset
= (offset
>> PAGE_SHIFT
) + (map_offset
>> PAGE_SHIFT
);
301 page
= entry
->pagelist
[page_offset
];
308 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
310 static struct page
*drm_vm_nopage(struct vm_area_struct
*vma
,
311 unsigned long address
,
313 if (type
) *type
= VM_FAULT_MINOR
;
314 return drm_do_vm_nopage(vma
, address
);
317 static struct page
*drm_vm_shm_nopage(struct vm_area_struct
*vma
,
318 unsigned long address
,
320 if (type
) *type
= VM_FAULT_MINOR
;
321 return drm_do_vm_shm_nopage(vma
, address
);
324 static struct page
*drm_vm_dma_nopage(struct vm_area_struct
*vma
,
325 unsigned long address
,
327 if (type
) *type
= VM_FAULT_MINOR
;
328 return drm_do_vm_dma_nopage(vma
, address
);
331 static struct page
*drm_vm_sg_nopage(struct vm_area_struct
*vma
,
332 unsigned long address
,
334 if (type
) *type
= VM_FAULT_MINOR
;
335 return drm_do_vm_sg_nopage(vma
, address
);
338 #else /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
340 static struct page
*drm_vm_nopage(struct vm_area_struct
*vma
,
341 unsigned long address
,
343 return drm_do_vm_nopage(vma
, address
);
346 static struct page
*drm_vm_shm_nopage(struct vm_area_struct
*vma
,
347 unsigned long address
,
349 return drm_do_vm_shm_nopage(vma
, address
);
352 static struct page
*drm_vm_dma_nopage(struct vm_area_struct
*vma
,
353 unsigned long address
,
355 return drm_do_vm_dma_nopage(vma
, address
);
358 static struct page
*drm_vm_sg_nopage(struct vm_area_struct
*vma
,
359 unsigned long address
,
361 return drm_do_vm_sg_nopage(vma
, address
);
367 /** AGP virtual memory operations */
368 static struct vm_operations_struct drm_vm_ops
= {
369 .nopage
= drm_vm_nopage
,
371 .close
= drm_vm_close
,
374 /** Shared virtual memory operations */
375 static struct vm_operations_struct drm_vm_shm_ops
= {
376 .nopage
= drm_vm_shm_nopage
,
378 .close
= drm_vm_shm_close
,
381 /** DMA virtual memory operations */
382 static struct vm_operations_struct drm_vm_dma_ops
= {
383 .nopage
= drm_vm_dma_nopage
,
385 .close
= drm_vm_close
,
388 /** Scatter-gather virtual memory operations */
389 static struct vm_operations_struct drm_vm_sg_ops
= {
390 .nopage
= drm_vm_sg_nopage
,
392 .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(struct vm_area_struct
*vma
)
406 drm_file_t
*priv
= vma
->vm_file
->private_data
;
407 drm_device_t
*dev
= priv
->head
->dev
;
408 drm_vma_entry_t
*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 down(&dev
->struct_sem
);
417 vma_entry
->vma
= vma
;
418 vma_entry
->next
= dev
->vmalist
;
419 vma_entry
->pid
= current
->pid
;
420 dev
->vmalist
= vma_entry
;
421 up(&dev
->struct_sem
);
426 * \c close method for all virtual memory types.
428 * \param vma virtual memory area.
430 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
433 static void drm_vm_close(struct vm_area_struct
*vma
)
435 drm_file_t
*priv
= vma
->vm_file
->private_data
;
436 drm_device_t
*dev
= priv
->head
->dev
;
437 drm_vma_entry_t
*pt
, *prev
;
439 DRM_DEBUG("0x%08lx,0x%08lx\n",
440 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
441 atomic_dec(&dev
->vma_count
);
443 down(&dev
->struct_sem
);
444 for (pt
= dev
->vmalist
, prev
= NULL
; pt
; prev
= pt
, pt
= pt
->next
) {
445 if (pt
->vma
== vma
) {
447 prev
->next
= pt
->next
;
449 dev
->vmalist
= pt
->next
;
451 drm_free(pt
, sizeof(*pt
), DRM_MEM_VMAS
);
455 up(&dev
->struct_sem
);
461 * \param filp file pointer.
462 * \param vma virtual memory area.
463 * \return zero on success or a negative number on failure.
465 * Sets the virtual memory area operations structure to vm_dma_ops, the file
466 * pointer, and calls vm_open().
468 static int drm_mmap_dma(struct file
*filp
, struct vm_area_struct
*vma
)
470 drm_file_t
*priv
= filp
->private_data
;
472 drm_device_dma_t
*dma
;
473 unsigned long length
= vma
->vm_end
- vma
->vm_start
;
476 dev
= priv
->head
->dev
;
478 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
479 vma
->vm_start
, vma
->vm_end
, VM_OFFSET(vma
));
481 /* Length must match exact page count */
482 if (!dma
|| (length
>> PAGE_SHIFT
) != dma
->page_count
) {
488 vma
->vm_ops
= &drm_vm_dma_ops
;
490 #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
491 vma
->vm_flags
|= VM_LOCKED
| VM_SHM
; /* Don't swap */
493 vma
->vm_flags
|= VM_RESERVED
; /* Don't swap */
496 vma
->vm_file
= filp
; /* Needed for drm_vm_open() */
501 unsigned long drm_core_get_map_ofs(drm_map_t
*map
)
505 EXPORT_SYMBOL(drm_core_get_map_ofs
);
507 unsigned long drm_core_get_reg_ofs(struct drm_device
*dev
)
510 return dev
->hose
->dense_mem_base
- dev
->hose
->mem_space
->start
;
515 EXPORT_SYMBOL(drm_core_get_reg_ofs
);
520 * \param filp file pointer.
521 * \param vma virtual memory area.
522 * \return zero on success or a negative number on failure.
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().
530 int drm_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
532 drm_file_t
*priv
= filp
->private_data
;
533 drm_device_t
*dev
= priv
->head
->dev
;
534 drm_map_t
*map
= NULL
;
535 drm_map_list_t
*r_list
;
536 unsigned long offset
= 0;
537 struct list_head
*list
;
539 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
540 vma
->vm_start
, vma
->vm_end
, VM_OFFSET(vma
));
542 if ( !priv
->authenticated
) return -EACCES
;
544 /* We check for "dma". On Apple's UniNorth, it's valid to have
545 * the AGP mapped at physical address 0
550 && (!dev
->agp
|| dev
->agp
->agp_info
.device
->vendor
!= PCI_VENDOR_ID_APPLE
)
553 return drm_mmap_dma(filp
, vma
);
555 /* A sequential search of a linked list is
556 fine here because: 1) there will only be
557 about 5-10 entries in the list and, 2) a
558 DRI client only has to do this mapping
559 once, so it doesn't have to be optimized
560 for performance, even if the list was a
562 list_for_each(list
, &dev
->maplist
->head
) {
565 r_list
= list_entry(list
, drm_map_list_t
, head
);
568 off
= dev
->driver
->get_map_ofs(map
);
569 if (off
== VM_OFFSET(vma
)) break;
572 if (!map
|| ((map
->flags
&_DRM_RESTRICTED
) && !capable(CAP_SYS_ADMIN
)))
575 /* Check for valid size. */
576 if (map
->size
!= vma
->vm_end
- vma
->vm_start
) return -EINVAL
;
578 if (!capable(CAP_SYS_ADMIN
) && (map
->flags
& _DRM_READ_ONLY
)) {
579 vma
->vm_flags
&= ~(VM_WRITE
| VM_MAYWRITE
);
580 #if defined(__i386__) || defined(__x86_64__)
581 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_RW
;
583 /* Ye gads this is ugly. With more thought
584 we could move this up higher and use
585 `protection_map' instead. */
586 vma
->vm_page_prot
= __pgprot(pte_val(pte_wrprotect(
587 __pte(pgprot_val(vma
->vm_page_prot
)))));
593 if (drm_core_has_AGP(dev
) && dev
->agp
->cant_use_aperture
) {
595 * On some platforms we can't talk to bus dma address from the CPU, so for
596 * memory of type DRM_AGP, we'll deal with sorting out the real physical
597 * pages and mappings in nopage()
599 #if defined(__powerpc__)
600 pgprot_val(vma
->vm_page_prot
) |= _PAGE_NO_CACHE
;
602 vma
->vm_ops
= &drm_vm_ops
;
605 /* fall through to _DRM_FRAME_BUFFER... */
606 case _DRM_FRAME_BUFFER
:
608 if (VM_OFFSET(vma
) >= __pa(high_memory
)) {
609 #if defined(__i386__) || defined(__x86_64__)
610 if (boot_cpu_data
.x86
> 3 && map
->type
!= _DRM_AGP
) {
611 pgprot_val(vma
->vm_page_prot
) |= _PAGE_PCD
;
612 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_PWT
;
614 #elif defined(__powerpc__)
615 pgprot_val(vma
->vm_page_prot
) |= _PAGE_NO_CACHE
| _PAGE_GUARDED
;
617 vma
->vm_flags
|= VM_IO
; /* not in core dump */
619 #if defined(__ia64__)
620 if (efi_range_is_wc(vma
->vm_start
, vma
->vm_end
-
623 pgprot_writecombine(vma
->vm_page_prot
);
626 pgprot_noncached(vma
->vm_page_prot
);
628 offset
= dev
->driver
->get_reg_ofs(dev
);
630 if (io_remap_pfn_range(DRM_RPR_ARG(vma
) vma
->vm_start
,
631 (VM_OFFSET(vma
) + offset
) >> PAGE_SHIFT
,
632 vma
->vm_end
- vma
->vm_start
,
635 if (io_remap_pfn_range(vma
, vma
->vm_start
,
636 (VM_OFFSET(vma
) + offset
) >> PAGE_SHIFT
,
637 vma
->vm_end
- vma
->vm_start
,
641 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
644 vma
->vm_start
, vma
->vm_end
, VM_OFFSET(vma
) + offset
);
645 vma
->vm_ops
= &drm_vm_ops
;
648 vma
->vm_ops
= &drm_vm_shm_ops
;
649 vma
->vm_private_data
= (void *)map
;
650 /* Don't let this area swap. Change when
651 DRM_KERNEL advisory is supported. */
652 #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
653 vma
->vm_flags
|= VM_LOCKED
;
655 vma
->vm_flags
|= VM_RESERVED
;
658 case _DRM_SCATTER_GATHER
:
659 vma
->vm_ops
= &drm_vm_sg_ops
;
660 vma
->vm_private_data
= (void *)map
;
661 #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
662 vma
->vm_flags
|= VM_LOCKED
;
664 vma
->vm_flags
|= VM_RESERVED
;
668 return -EINVAL
; /* This should never happen. */
670 #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
671 vma
->vm_flags
|= VM_LOCKED
| VM_SHM
; /* Don't swap */
673 vma
->vm_flags
|= VM_RESERVED
; /* Don't swap */
676 vma
->vm_file
= filp
; /* Needed for drm_vm_open() */
680 EXPORT_SYMBOL(drm_mmap
);