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
)
73 list_for_each(list
, &dev
->maplist
->head
) {
74 r_list
= list_entry(list
, drm_map_list_t
, head
);
78 if (r_list
->user_token
== (vma
->vm_pgoff
<< PAGE_SHIFT
))
82 if (map
&& map
->type
== _DRM_AGP
) {
83 unsigned long offset
= address
- vma
->vm_start
;
84 unsigned long baddr
= map
->offset
+ offset
;
85 struct drm_agp_mem
*agpmem
;
90 * Adjust to a bus-relative address
92 baddr
-= dev
->hose
->mem_space
->start
;
96 * It's AGP memory - find the real physical page to map
98 for (agpmem
= dev
->agp
->memory
; agpmem
; agpmem
= agpmem
->next
) {
99 if (agpmem
->bound
<= baddr
&&
100 agpmem
->bound
+ agpmem
->pages
* PAGE_SIZE
> baddr
)
105 goto vm_nopage_error
;
108 * Get the page, inc the use count, and return it
110 offset
= (baddr
- agpmem
->bound
) >> PAGE_SHIFT
;
111 page
= virt_to_page(__va(agpmem
->memory
->memory
[offset
]));
115 ("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
116 baddr
, __va(agpmem
->memory
->memory
[offset
]), offset
,
122 return NOPAGE_SIGBUS
; /* Disallow mremap */
124 #else /* __OS_HAS_AGP */
125 static __inline__
struct page
*drm_do_vm_nopage(struct vm_area_struct
*vma
,
126 unsigned long address
)
128 return NOPAGE_SIGBUS
;
130 #endif /* __OS_HAS_AGP */
133 * \c nopage method for shared virtual memory.
135 * \param vma virtual memory area.
136 * \param address access address.
137 * \return pointer to the page structure.
139 * Get the the mapping, find the real physical page to map, get the page, and
142 static __inline__
struct page
*drm_do_vm_shm_nopage(struct vm_area_struct
*vma
,
143 unsigned long address
)
145 drm_map_t
*map
= (drm_map_t
*) vma
->vm_private_data
;
146 unsigned long offset
;
150 if (address
> vma
->vm_end
)
151 return NOPAGE_SIGBUS
; /* Disallow mremap */
153 return NOPAGE_OOM
; /* Nothing allocated */
155 offset
= address
- vma
->vm_start
;
156 i
= (unsigned long)map
->handle
+ offset
;
157 page
= (map
->type
== _DRM_CONSISTENT
) ?
158 virt_to_page((void *)i
) : vmalloc_to_page((void *)i
);
163 DRM_DEBUG("shm_nopage 0x%lx\n", address
);
168 * \c close method for shared virtual memory.
170 * \param vma virtual memory area.
172 * Deletes map information if we are the last
173 * person to close a mapping and it's not in the global maplist.
175 static void drm_vm_shm_close(struct vm_area_struct
*vma
)
177 drm_file_t
*priv
= vma
->vm_file
->private_data
;
178 drm_device_t
*dev
= priv
->head
->dev
;
179 drm_vma_entry_t
*pt
, *prev
, *next
;
181 drm_map_list_t
*r_list
;
182 struct list_head
*list
;
185 DRM_DEBUG("0x%08lx,0x%08lx\n",
186 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
187 atomic_dec(&dev
->vma_count
);
189 map
= vma
->vm_private_data
;
191 mutex_lock(&dev
->struct_mutex
);
192 for (pt
= dev
->vmalist
, prev
= NULL
; pt
; pt
= next
) {
194 if (pt
->vma
->vm_private_data
== map
)
196 if (pt
->vma
== vma
) {
198 prev
->next
= pt
->next
;
200 dev
->vmalist
= pt
->next
;
202 drm_free(pt
, sizeof(*pt
), DRM_MEM_VMAS
);
207 /* We were the only map that was found */
208 if (found_maps
== 1 && map
->flags
& _DRM_REMOVABLE
) {
209 /* Check to see if we are in the maplist, if we are not, then
210 * we delete this mappings information.
213 list
= &dev
->maplist
->head
;
214 list_for_each(list
, &dev
->maplist
->head
) {
215 r_list
= list_entry(list
, drm_map_list_t
, head
);
216 if (r_list
->map
== map
)
221 drm_dma_handle_t dmah
;
225 case _DRM_FRAME_BUFFER
:
226 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
228 retcode
= mtrr_del(map
->mtrr
,
231 DRM_DEBUG("mtrr_del = %d\n", retcode
);
233 drm_ioremapfree(map
->handle
, map
->size
, dev
);
239 case _DRM_SCATTER_GATHER
:
241 case _DRM_CONSISTENT
:
242 dmah
.vaddr
= map
->handle
;
243 dmah
.busaddr
= map
->offset
;
244 dmah
.size
= map
->size
;
245 __drm_pci_free(dev
, &dmah
);
248 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
251 mutex_unlock(&dev
->struct_mutex
);
255 * \c nopage method for DMA virtual memory.
257 * \param vma virtual memory area.
258 * \param address access address.
259 * \return pointer to the page structure.
261 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
263 static __inline__
struct page
*drm_do_vm_dma_nopage(struct vm_area_struct
*vma
,
264 unsigned long address
)
266 drm_file_t
*priv
= vma
->vm_file
->private_data
;
267 drm_device_t
*dev
= priv
->head
->dev
;
268 drm_device_dma_t
*dma
= dev
->dma
;
269 unsigned long offset
;
270 unsigned long page_nr
;
274 return NOPAGE_SIGBUS
; /* Error */
275 if (address
> vma
->vm_end
)
276 return NOPAGE_SIGBUS
; /* Disallow mremap */
278 return NOPAGE_OOM
; /* Nothing allocated */
280 offset
= address
- vma
->vm_start
; /* vm_[pg]off[set] should be 0 */
281 page_nr
= offset
>> PAGE_SHIFT
;
282 page
= virt_to_page((dma
->pagelist
[page_nr
] + (offset
& (~PAGE_MASK
))));
286 DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address
, page_nr
);
291 * \c nopage method for scatter-gather virtual memory.
293 * \param vma virtual memory area.
294 * \param address access address.
295 * \return pointer to the page structure.
297 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
299 static __inline__
struct page
*drm_do_vm_sg_nopage(struct vm_area_struct
*vma
,
300 unsigned long address
)
302 drm_map_t
*map
= (drm_map_t
*) vma
->vm_private_data
;
303 drm_file_t
*priv
= vma
->vm_file
->private_data
;
304 drm_device_t
*dev
= priv
->head
->dev
;
305 drm_sg_mem_t
*entry
= dev
->sg
;
306 unsigned long offset
;
307 unsigned long map_offset
;
308 unsigned long page_offset
;
312 return NOPAGE_SIGBUS
; /* Error */
313 if (address
> vma
->vm_end
)
314 return NOPAGE_SIGBUS
; /* Disallow mremap */
315 if (!entry
->pagelist
)
316 return NOPAGE_OOM
; /* Nothing allocated */
318 offset
= address
- vma
->vm_start
;
319 map_offset
= map
->offset
- (unsigned long)dev
->sg
->virtual;
320 page_offset
= (offset
>> PAGE_SHIFT
) + (map_offset
>> PAGE_SHIFT
);
321 page
= entry
->pagelist
[page_offset
];
327 static struct page
*drm_vm_nopage(struct vm_area_struct
*vma
,
328 unsigned long address
, int *type
)
331 *type
= VM_FAULT_MINOR
;
332 return drm_do_vm_nopage(vma
, address
);
335 static struct page
*drm_vm_shm_nopage(struct vm_area_struct
*vma
,
336 unsigned long address
, int *type
)
339 *type
= VM_FAULT_MINOR
;
340 return drm_do_vm_shm_nopage(vma
, address
);
343 static struct page
*drm_vm_dma_nopage(struct vm_area_struct
*vma
,
344 unsigned long address
, int *type
)
347 *type
= VM_FAULT_MINOR
;
348 return drm_do_vm_dma_nopage(vma
, address
);
351 static struct page
*drm_vm_sg_nopage(struct vm_area_struct
*vma
,
352 unsigned long address
, int *type
)
355 *type
= VM_FAULT_MINOR
;
356 return drm_do_vm_sg_nopage(vma
, address
);
359 /** AGP virtual memory operations */
360 static struct vm_operations_struct drm_vm_ops
= {
361 .nopage
= drm_vm_nopage
,
363 .close
= drm_vm_close
,
366 /** Shared virtual memory operations */
367 static struct vm_operations_struct drm_vm_shm_ops
= {
368 .nopage
= drm_vm_shm_nopage
,
370 .close
= drm_vm_shm_close
,
373 /** DMA virtual memory operations */
374 static struct vm_operations_struct drm_vm_dma_ops
= {
375 .nopage
= drm_vm_dma_nopage
,
377 .close
= drm_vm_close
,
380 /** Scatter-gather virtual memory operations */
381 static struct vm_operations_struct drm_vm_sg_ops
= {
382 .nopage
= drm_vm_sg_nopage
,
384 .close
= drm_vm_close
,
388 * \c open method for shared virtual memory.
390 * \param vma virtual memory area.
392 * Create a new drm_vma_entry structure as the \p vma private data entry and
393 * add it to drm_device::vmalist.
395 static void drm_vm_open(struct vm_area_struct
*vma
)
397 drm_file_t
*priv
= vma
->vm_file
->private_data
;
398 drm_device_t
*dev
= priv
->head
->dev
;
399 drm_vma_entry_t
*vma_entry
;
401 DRM_DEBUG("0x%08lx,0x%08lx\n",
402 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
403 atomic_inc(&dev
->vma_count
);
405 vma_entry
= drm_alloc(sizeof(*vma_entry
), DRM_MEM_VMAS
);
407 mutex_lock(&dev
->struct_mutex
);
408 vma_entry
->vma
= vma
;
409 vma_entry
->next
= dev
->vmalist
;
410 vma_entry
->pid
= current
->pid
;
411 dev
->vmalist
= vma_entry
;
412 mutex_unlock(&dev
->struct_mutex
);
417 * \c close method for all virtual memory types.
419 * \param vma virtual memory area.
421 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
424 static void drm_vm_close(struct vm_area_struct
*vma
)
426 drm_file_t
*priv
= vma
->vm_file
->private_data
;
427 drm_device_t
*dev
= priv
->head
->dev
;
428 drm_vma_entry_t
*pt
, *prev
;
430 DRM_DEBUG("0x%08lx,0x%08lx\n",
431 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
432 atomic_dec(&dev
->vma_count
);
434 mutex_lock(&dev
->struct_mutex
);
435 for (pt
= dev
->vmalist
, prev
= NULL
; pt
; prev
= pt
, pt
= pt
->next
) {
436 if (pt
->vma
== vma
) {
438 prev
->next
= pt
->next
;
440 dev
->vmalist
= pt
->next
;
442 drm_free(pt
, sizeof(*pt
), DRM_MEM_VMAS
);
446 mutex_unlock(&dev
->struct_mutex
);
452 * \param filp file pointer.
453 * \param vma virtual memory area.
454 * \return zero on success or a negative number on failure.
456 * Sets the virtual memory area operations structure to vm_dma_ops, the file
457 * pointer, and calls vm_open().
459 static int drm_mmap_dma(struct file
*filp
, struct vm_area_struct
*vma
)
461 drm_file_t
*priv
= filp
->private_data
;
463 drm_device_dma_t
*dma
;
464 unsigned long length
= vma
->vm_end
- vma
->vm_start
;
467 dev
= priv
->head
->dev
;
469 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
470 vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
<< PAGE_SHIFT
);
472 /* Length must match exact page count */
473 if (!dma
|| (length
>> PAGE_SHIFT
) != dma
->page_count
) {
479 vma
->vm_ops
= &drm_vm_dma_ops
;
481 vma
->vm_flags
|= VM_RESERVED
; /* Don't swap */
483 vma
->vm_file
= filp
; /* Needed for drm_vm_open() */
488 unsigned long drm_core_get_map_ofs(drm_map_t
* map
)
493 EXPORT_SYMBOL(drm_core_get_map_ofs
);
495 unsigned long drm_core_get_reg_ofs(struct drm_device
*dev
)
498 return dev
->hose
->dense_mem_base
- dev
->hose
->mem_space
->start
;
504 EXPORT_SYMBOL(drm_core_get_reg_ofs
);
509 * \param filp file pointer.
510 * \param vma virtual memory area.
511 * \return zero on success or a negative number on failure.
513 * If the virtual memory area has no offset associated with it then it's a DMA
514 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
515 * checks that the restricted flag is not set, sets the virtual memory operations
516 * according to the mapping type and remaps the pages. Finally sets the file
517 * pointer and calls vm_open().
519 int drm_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
521 drm_file_t
*priv
= filp
->private_data
;
522 drm_device_t
*dev
= priv
->head
->dev
;
523 drm_map_t
*map
= NULL
;
524 drm_map_list_t
*r_list
;
525 unsigned long offset
= 0;
526 struct list_head
*list
;
528 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
529 vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
<< PAGE_SHIFT
);
531 if (!priv
->authenticated
)
534 /* We check for "dma". On Apple's UniNorth, it's valid to have
535 * the AGP mapped at physical address 0
538 if (!(vma
->vm_pgoff
<< PAGE_SHIFT
)
541 || dev
->agp
->agp_info
.device
->vendor
!= PCI_VENDOR_ID_APPLE
)
544 return drm_mmap_dma(filp
, vma
);
546 /* A sequential search of a linked list is
547 fine here because: 1) there will only be
548 about 5-10 entries in the list and, 2) a
549 DRI client only has to do this mapping
550 once, so it doesn't have to be optimized
551 for performance, even if the list was a
553 list_for_each(list
, &dev
->maplist
->head
) {
555 r_list
= list_entry(list
, drm_map_list_t
, head
);
559 if (r_list
->user_token
== vma
->vm_pgoff
<< PAGE_SHIFT
)
563 if (!map
|| ((map
->flags
& _DRM_RESTRICTED
) && !capable(CAP_SYS_ADMIN
)))
566 /* Check for valid size. */
567 if (map
->size
!= vma
->vm_end
- vma
->vm_start
)
570 if (!capable(CAP_SYS_ADMIN
) && (map
->flags
& _DRM_READ_ONLY
)) {
571 vma
->vm_flags
&= ~(VM_WRITE
| VM_MAYWRITE
);
572 #if defined(__i386__) || defined(__x86_64__)
573 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_RW
;
575 /* Ye gads this is ugly. With more thought
576 we could move this up higher and use
577 `protection_map' instead. */
581 (__pte(pgprot_val(vma
->vm_page_prot
)))));
587 if (drm_core_has_AGP(dev
) && dev
->agp
->cant_use_aperture
) {
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 nopage()
593 #if defined(__powerpc__)
594 pgprot_val(vma
->vm_page_prot
) |= _PAGE_NO_CACHE
;
596 vma
->vm_ops
= &drm_vm_ops
;
599 /* fall through to _DRM_FRAME_BUFFER... */
600 case _DRM_FRAME_BUFFER
:
602 #if defined(__i386__) || defined(__x86_64__)
603 if (boot_cpu_data
.x86
> 3 && map
->type
!= _DRM_AGP
) {
604 pgprot_val(vma
->vm_page_prot
) |= _PAGE_PCD
;
605 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_PWT
;
607 #elif defined(__powerpc__)
608 pgprot_val(vma
->vm_page_prot
) |= _PAGE_NO_CACHE
;
609 if (map
->type
== _DRM_REGISTERS
)
610 pgprot_val(vma
->vm_page_prot
) |= _PAGE_GUARDED
;
612 vma
->vm_flags
|= VM_IO
; /* not in core dump */
613 #if defined(__ia64__)
614 if (efi_range_is_wc(vma
->vm_start
, vma
->vm_end
- vma
->vm_start
))
616 pgprot_writecombine(vma
->vm_page_prot
);
618 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
620 offset
= dev
->driver
->get_reg_ofs(dev
);
622 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
623 if (io_remap_pfn_range(vma
, vma
->vm_start
,
624 (map
->offset
+ offset
) >> PAGE_SHIFT
,
625 vma
->vm_end
- vma
->vm_start
,
628 if (io_remap_pfn_range(vma
, vma
->vm_start
,
629 (map
->offset
+ offset
) >> PAGE_SHIFT
,
630 vma
->vm_end
- vma
->vm_start
,
634 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
637 vma
->vm_start
, vma
->vm_end
, map
->offset
+ offset
);
638 vma
->vm_ops
= &drm_vm_ops
;
641 case _DRM_CONSISTENT
:
642 /* Consistent memory is really like shared memory. It's only
643 * allocate in a different way */
644 vma
->vm_ops
= &drm_vm_shm_ops
;
645 vma
->vm_private_data
= (void *)map
;
646 /* Don't let this area swap. Change when
647 DRM_KERNEL advisory is supported. */
648 vma
->vm_flags
|= VM_RESERVED
;
650 case _DRM_SCATTER_GATHER
:
651 vma
->vm_ops
= &drm_vm_sg_ops
;
652 vma
->vm_private_data
= (void *)map
;
653 vma
->vm_flags
|= VM_RESERVED
;
656 return -EINVAL
; /* This should never happen. */
658 vma
->vm_flags
|= VM_RESERVED
; /* Don't swap */
660 vma
->vm_file
= filp
; /* Needed for drm_vm_open() */
665 EXPORT_SYMBOL(drm_mmap
);