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.
37 #include <linux/export.h>
38 #include <linux/seq_file.h>
40 #include <linux/efi.h>
41 #include <linux/slab.h>
43 #include <asm/pgtable.h>
44 #include "drm_internal.h"
45 #include "drm_legacy.h"
47 struct drm_vma_entry
{
48 struct list_head head
;
49 struct vm_area_struct
*vma
;
53 static void drm_vm_open(struct vm_area_struct
*vma
);
54 static void drm_vm_close(struct vm_area_struct
*vma
);
56 static pgprot_t
drm_io_prot(struct drm_local_map
*map
,
57 struct vm_area_struct
*vma
)
59 pgprot_t tmp
= vm_get_page_prot(vma
->vm_flags
);
61 #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
62 if (map
->type
== _DRM_REGISTERS
&& !(map
->flags
& _DRM_WRITE_COMBINING
))
63 tmp
= pgprot_noncached(tmp
);
65 tmp
= pgprot_writecombine(tmp
);
66 #elif defined(__ia64__)
67 if (efi_range_is_wc(vma
->vm_start
, vma
->vm_end
-
69 tmp
= pgprot_writecombine(tmp
);
71 tmp
= pgprot_noncached(tmp
);
72 #elif defined(__sparc__) || defined(__arm__) || defined(__mips__)
73 tmp
= pgprot_noncached(tmp
);
78 static pgprot_t
drm_dma_prot(uint32_t map_type
, struct vm_area_struct
*vma
)
80 pgprot_t tmp
= vm_get_page_prot(vma
->vm_flags
);
82 #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
83 tmp
= pgprot_noncached_wc(tmp
);
89 * \c fault method for AGP virtual memory.
91 * \param vma virtual memory area.
92 * \param address access address.
93 * \return pointer to the page structure.
95 * Find the right map and if it's AGP memory find the real physical page to
96 * map, get the page, increment the use count and return it.
98 #if IS_ENABLED(CONFIG_AGP)
99 static int drm_vm_fault(struct vm_fault
*vmf
)
101 struct vm_area_struct
*vma
= vmf
->vma
;
102 struct drm_file
*priv
= vma
->vm_file
->private_data
;
103 struct drm_device
*dev
= priv
->minor
->dev
;
104 struct drm_local_map
*map
= NULL
;
105 struct drm_map_list
*r_list
;
106 struct drm_hash_item
*hash
;
114 if (!dev
->agp
|| !dev
->agp
->cant_use_aperture
)
117 if (drm_ht_find_item(&dev
->map_hash
, vma
->vm_pgoff
, &hash
))
120 r_list
= drm_hash_entry(hash
, struct drm_map_list
, hash
);
123 if (map
&& map
->type
== _DRM_AGP
) {
125 * Using vm_pgoff as a selector forces us to use this unusual
128 resource_size_t offset
= vmf
->address
- vma
->vm_start
;
129 resource_size_t baddr
= map
->offset
+ offset
;
130 struct drm_agp_mem
*agpmem
;
135 * Adjust to a bus-relative address
137 baddr
-= dev
->hose
->mem_space
->start
;
141 * It's AGP memory - find the real physical page to map
143 list_for_each_entry(agpmem
, &dev
->agp
->memory
, head
) {
144 if (agpmem
->bound
<= baddr
&&
145 agpmem
->bound
+ agpmem
->pages
* PAGE_SIZE
> baddr
)
149 if (&agpmem
->head
== &dev
->agp
->memory
)
153 * Get the page, inc the use count, and return it
155 offset
= (baddr
- agpmem
->bound
) >> PAGE_SHIFT
;
156 page
= agpmem
->memory
->pages
[offset
];
161 ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
162 (unsigned long long)baddr
,
163 agpmem
->memory
->pages
[offset
],
164 (unsigned long long)offset
,
169 return VM_FAULT_SIGBUS
; /* Disallow mremap */
172 static int drm_vm_fault(struct vm_fault
*vmf
)
174 return VM_FAULT_SIGBUS
;
179 * \c nopage method for shared virtual memory.
181 * \param vma virtual memory area.
182 * \param address access address.
183 * \return pointer to the page structure.
185 * Get the mapping, find the real physical page to map, get the page, and
188 static int drm_vm_shm_fault(struct vm_fault
*vmf
)
190 struct vm_area_struct
*vma
= vmf
->vma
;
191 struct drm_local_map
*map
= vma
->vm_private_data
;
192 unsigned long offset
;
197 return VM_FAULT_SIGBUS
; /* Nothing allocated */
199 offset
= vmf
->address
- vma
->vm_start
;
200 i
= (unsigned long)map
->handle
+ offset
;
201 page
= vmalloc_to_page((void *)i
);
203 return VM_FAULT_SIGBUS
;
207 DRM_DEBUG("shm_fault 0x%lx\n", offset
);
212 * \c close method for shared virtual memory.
214 * \param vma virtual memory area.
216 * Deletes map information if we are the last
217 * person to close a mapping and it's not in the global maplist.
219 static void drm_vm_shm_close(struct vm_area_struct
*vma
)
221 struct drm_file
*priv
= vma
->vm_file
->private_data
;
222 struct drm_device
*dev
= priv
->minor
->dev
;
223 struct drm_vma_entry
*pt
, *temp
;
224 struct drm_local_map
*map
;
225 struct drm_map_list
*r_list
;
228 DRM_DEBUG("0x%08lx,0x%08lx\n",
229 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
231 map
= vma
->vm_private_data
;
233 mutex_lock(&dev
->struct_mutex
);
234 list_for_each_entry_safe(pt
, temp
, &dev
->vmalist
, head
) {
235 if (pt
->vma
->vm_private_data
== map
)
237 if (pt
->vma
== vma
) {
243 /* We were the only map that was found */
244 if (found_maps
== 1 && map
->flags
& _DRM_REMOVABLE
) {
245 /* Check to see if we are in the maplist, if we are not, then
246 * we delete this mappings information.
249 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
250 if (r_list
->map
== map
)
255 drm_dma_handle_t dmah
;
259 case _DRM_FRAME_BUFFER
:
260 arch_phys_wc_del(map
->mtrr
);
261 iounmap(map
->handle
);
267 case _DRM_SCATTER_GATHER
:
269 case _DRM_CONSISTENT
:
270 dmah
.vaddr
= map
->handle
;
271 dmah
.busaddr
= map
->offset
;
272 dmah
.size
= map
->size
;
273 __drm_legacy_pci_free(dev
, &dmah
);
279 mutex_unlock(&dev
->struct_mutex
);
283 * \c fault method for DMA virtual memory.
285 * \param address access address.
286 * \return pointer to the page structure.
288 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
290 static int drm_vm_dma_fault(struct vm_fault
*vmf
)
292 struct vm_area_struct
*vma
= vmf
->vma
;
293 struct drm_file
*priv
= vma
->vm_file
->private_data
;
294 struct drm_device
*dev
= priv
->minor
->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
= vmf
->address
- vma
->vm_start
;
306 /* vm_[pg]off[set] should be 0 */
307 page_nr
= offset
>> PAGE_SHIFT
; /* page_nr could just be vmf->pgoff */
308 page
= virt_to_page((void *)dma
->pagelist
[page_nr
]);
313 DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset
, page_nr
);
318 * \c fault method for scatter-gather virtual memory.
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_vm_sg_fault(struct vm_fault
*vmf
)
327 struct vm_area_struct
*vma
= vmf
->vma
;
328 struct drm_local_map
*map
= vma
->vm_private_data
;
329 struct drm_file
*priv
= vma
->vm_file
->private_data
;
330 struct drm_device
*dev
= priv
->minor
->dev
;
331 struct drm_sg_mem
*entry
= dev
->sg
;
332 unsigned long offset
;
333 unsigned long map_offset
;
334 unsigned long page_offset
;
338 return VM_FAULT_SIGBUS
; /* Error */
339 if (!entry
->pagelist
)
340 return VM_FAULT_SIGBUS
; /* Nothing allocated */
342 offset
= vmf
->address
- vma
->vm_start
;
343 map_offset
= map
->offset
- (unsigned long)dev
->sg
->virtual;
344 page_offset
= (offset
>> PAGE_SHIFT
) + (map_offset
>> PAGE_SHIFT
);
345 page
= entry
->pagelist
[page_offset
];
352 /** AGP virtual memory operations */
353 static const struct vm_operations_struct drm_vm_ops
= {
354 .fault
= drm_vm_fault
,
356 .close
= drm_vm_close
,
359 /** Shared virtual memory operations */
360 static const struct vm_operations_struct drm_vm_shm_ops
= {
361 .fault
= drm_vm_shm_fault
,
363 .close
= drm_vm_shm_close
,
366 /** DMA virtual memory operations */
367 static const struct vm_operations_struct drm_vm_dma_ops
= {
368 .fault
= drm_vm_dma_fault
,
370 .close
= drm_vm_close
,
373 /** Scatter-gather virtual memory operations */
374 static const struct vm_operations_struct drm_vm_sg_ops
= {
375 .fault
= drm_vm_sg_fault
,
377 .close
= drm_vm_close
,
380 static void drm_vm_open_locked(struct drm_device
*dev
,
381 struct vm_area_struct
*vma
)
383 struct drm_vma_entry
*vma_entry
;
385 DRM_DEBUG("0x%08lx,0x%08lx\n",
386 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
388 vma_entry
= kmalloc(sizeof(*vma_entry
), GFP_KERNEL
);
390 vma_entry
->vma
= vma
;
391 vma_entry
->pid
= current
->pid
;
392 list_add(&vma_entry
->head
, &dev
->vmalist
);
396 static void drm_vm_open(struct vm_area_struct
*vma
)
398 struct drm_file
*priv
= vma
->vm_file
->private_data
;
399 struct drm_device
*dev
= priv
->minor
->dev
;
401 mutex_lock(&dev
->struct_mutex
);
402 drm_vm_open_locked(dev
, vma
);
403 mutex_unlock(&dev
->struct_mutex
);
406 static void drm_vm_close_locked(struct drm_device
*dev
,
407 struct vm_area_struct
*vma
)
409 struct drm_vma_entry
*pt
, *temp
;
411 DRM_DEBUG("0x%08lx,0x%08lx\n",
412 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
414 list_for_each_entry_safe(pt
, temp
, &dev
->vmalist
, head
) {
415 if (pt
->vma
== vma
) {
424 * \c close method for all virtual memory types.
426 * \param vma virtual memory area.
428 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
431 static void drm_vm_close(struct vm_area_struct
*vma
)
433 struct drm_file
*priv
= vma
->vm_file
->private_data
;
434 struct drm_device
*dev
= priv
->minor
->dev
;
436 mutex_lock(&dev
->struct_mutex
);
437 drm_vm_close_locked(dev
, vma
);
438 mutex_unlock(&dev
->struct_mutex
);
444 * \param file_priv DRM file private.
445 * \param vma virtual memory area.
446 * \return zero on success or a negative number on failure.
448 * Sets the virtual memory area operations structure to vm_dma_ops, the file
449 * pointer, and calls vm_open().
451 static int drm_mmap_dma(struct file
*filp
, struct vm_area_struct
*vma
)
453 struct drm_file
*priv
= filp
->private_data
;
454 struct drm_device
*dev
;
455 struct drm_device_dma
*dma
;
456 unsigned long length
= vma
->vm_end
- vma
->vm_start
;
458 dev
= priv
->minor
->dev
;
460 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
461 vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
);
463 /* Length must match exact page count */
464 if (!dma
|| (length
>> PAGE_SHIFT
) != dma
->page_count
) {
468 if (!capable(CAP_SYS_ADMIN
) &&
469 (dma
->flags
& _DRM_DMA_USE_PCI_RO
)) {
470 vma
->vm_flags
&= ~(VM_WRITE
| VM_MAYWRITE
);
471 #if defined(__i386__) || defined(__x86_64__)
472 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_RW
;
474 /* Ye gads this is ugly. With more thought
475 we could move this up higher and use
476 `protection_map' instead. */
480 (__pte(pgprot_val(vma
->vm_page_prot
)))));
484 vma
->vm_ops
= &drm_vm_dma_ops
;
486 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
488 drm_vm_open_locked(dev
, vma
);
492 static resource_size_t
drm_core_get_reg_ofs(struct drm_device
*dev
)
495 return dev
->hose
->dense_mem_base
;
504 * \param file_priv DRM file private.
505 * \param vma virtual memory area.
506 * \return zero on success or a negative number on failure.
508 * If the virtual memory area has no offset associated with it then it's a DMA
509 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
510 * checks that the restricted flag is not set, sets the virtual memory operations
511 * according to the mapping type and remaps the pages. Finally sets the file
512 * pointer and calls vm_open().
514 static int drm_mmap_locked(struct file
*filp
, struct vm_area_struct
*vma
)
516 struct drm_file
*priv
= filp
->private_data
;
517 struct drm_device
*dev
= priv
->minor
->dev
;
518 struct drm_local_map
*map
= NULL
;
519 resource_size_t offset
= 0;
520 struct drm_hash_item
*hash
;
522 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
523 vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
);
525 if (!priv
->authenticated
)
528 /* We check for "dma". On Apple's UniNorth, it's valid to have
529 * the AGP mapped at physical address 0
533 #if IS_ENABLED(CONFIG_AGP)
535 || dev
->agp
->agp_info
.device
->vendor
!= PCI_VENDOR_ID_APPLE
)
538 return drm_mmap_dma(filp
, vma
);
540 if (drm_ht_find_item(&dev
->map_hash
, vma
->vm_pgoff
, &hash
)) {
541 DRM_ERROR("Could not find map\n");
545 map
= drm_hash_entry(hash
, struct drm_map_list
, hash
)->map
;
546 if (!map
|| ((map
->flags
& _DRM_RESTRICTED
) && !capable(CAP_SYS_ADMIN
)))
549 /* Check for valid size. */
550 if (map
->size
< vma
->vm_end
- vma
->vm_start
)
553 if (!capable(CAP_SYS_ADMIN
) && (map
->flags
& _DRM_READ_ONLY
)) {
554 vma
->vm_flags
&= ~(VM_WRITE
| VM_MAYWRITE
);
555 #if defined(__i386__) || defined(__x86_64__)
556 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_RW
;
558 /* Ye gads this is ugly. With more thought
559 we could move this up higher and use
560 `protection_map' instead. */
564 (__pte(pgprot_val(vma
->vm_page_prot
)))));
569 #if !defined(__arm__)
571 if (dev
->agp
&& dev
->agp
->cant_use_aperture
) {
573 * On some platforms we can't talk to bus dma address from the CPU, so for
574 * memory of type DRM_AGP, we'll deal with sorting out the real physical
575 * pages and mappings in fault()
577 #if defined(__powerpc__)
578 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
580 vma
->vm_ops
= &drm_vm_ops
;
583 /* fall through to _DRM_FRAME_BUFFER... */
585 case _DRM_FRAME_BUFFER
:
587 offset
= drm_core_get_reg_ofs(dev
);
588 vma
->vm_page_prot
= drm_io_prot(map
, vma
);
589 if (io_remap_pfn_range(vma
, vma
->vm_start
,
590 (map
->offset
+ offset
) >> PAGE_SHIFT
,
591 vma
->vm_end
- vma
->vm_start
,
594 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
595 " offset = 0x%llx\n",
597 vma
->vm_start
, vma
->vm_end
, (unsigned long long)(map
->offset
+ offset
));
599 vma
->vm_ops
= &drm_vm_ops
;
601 case _DRM_CONSISTENT
:
602 /* Consistent memory is really like shared memory. But
603 * it's allocated in a different way, so avoid fault */
604 if (remap_pfn_range(vma
, vma
->vm_start
,
605 page_to_pfn(virt_to_page(map
->handle
)),
606 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
608 vma
->vm_page_prot
= drm_dma_prot(map
->type
, vma
);
609 /* fall through to _DRM_SHM */
611 vma
->vm_ops
= &drm_vm_shm_ops
;
612 vma
->vm_private_data
= (void *)map
;
614 case _DRM_SCATTER_GATHER
:
615 vma
->vm_ops
= &drm_vm_sg_ops
;
616 vma
->vm_private_data
= (void *)map
;
617 vma
->vm_page_prot
= drm_dma_prot(map
->type
, vma
);
620 return -EINVAL
; /* This should never happen. */
622 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
624 drm_vm_open_locked(dev
, vma
);
628 int drm_legacy_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
630 struct drm_file
*priv
= filp
->private_data
;
631 struct drm_device
*dev
= priv
->minor
->dev
;
634 if (drm_device_is_unplugged(dev
))
637 mutex_lock(&dev
->struct_mutex
);
638 ret
= drm_mmap_locked(filp
, vma
);
639 mutex_unlock(&dev
->struct_mutex
);
643 EXPORT_SYMBOL(drm_legacy_mmap
);
645 void drm_legacy_vma_flush(struct drm_device
*dev
)
647 struct drm_vma_entry
*vma
, *vma_temp
;
649 /* Clear vma list (only needed for legacy drivers) */
650 list_for_each_entry_safe(vma
, vma_temp
, &dev
->vmalist
, head
) {
651 list_del(&vma
->head
);