2 * Copyright 2003 Eric Anholt
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * $FreeBSD: src/sys/dev/drm2/drm_vm.c,v 1.1 2012/05/22 11:07:44 kib Exp $
27 * Support code for mmaping of DRM maps.
31 #include <dev/drm2/drmP.h>
32 #include <dev/drm2/drm.h>
35 drm_mmap(struct dev_mmap_args *ap)
37 struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
38 int prot, vm_memattr_t *memattr)*/
40 struct cdev *kdev = ap->a_head.a_dev;
41 vm_offset_t offset = ap->a_offset;
42 struct drm_device *dev = drm_get_device_from_kdev(kdev);
43 struct drm_file *file_priv = NULL;
45 enum drm_map_type type;
49 /* d_mmap gets called twice, we can only reference file_priv during
50 * the first call. We need to assume that if error is EBADF the
51 * call was succesful and the client is authenticated.
54 file_priv = drm_find_file_by_proc(dev, curthread);
58 DRM_ERROR("Could not find authenticator!\n");
62 if (!file_priv->authenticated)
65 DRM_DEBUG("called with offset %016jx\n", offset);
66 if (dev->dma && offset < ptoa(dev->dma->page_count)) {
67 drm_device_dma_t *dma = dev->dma;
69 DRM_SPINLOCK(&dev->dma_lock);
71 if (dma->pagelist != NULL) {
72 unsigned long page = offset >> PAGE_SHIFT;
73 phys = dma->pagelist[page];
75 DRM_SPINUNLOCK(&dev->dma_lock);
78 DRM_SPINUNLOCK(&dev->dma_lock);
83 /* A sequential search of a linked list is
84 fine here because: 1) there will only be
85 about 5-10 entries in the list and, 2) a
86 DRI client only has to do this mapping
87 once, so it doesn't have to be optimized
88 for performance, even if the list was a
92 TAILQ_FOREACH(map, &dev->maplist, link) {
93 if (offset >> DRM_MAP_HANDLE_SHIFT ==
94 (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT)
99 DRM_DEBUG("Can't find map, request offset = %016jx\n", offset);
100 TAILQ_FOREACH(map, &dev->maplist, link) {
101 DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
102 map->offset, (unsigned long)map->handle);
107 if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
109 DRM_DEBUG("restricted map\n");
115 offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1);
118 case _DRM_FRAME_BUFFER:
122 *memattr = VM_MEMATTR_WRITE_COMBINING;
126 phys = map->offset + offset;
128 case _DRM_SCATTER_GATHER:
131 *memattr = VM_MEMATTR_WRITE_COMBINING;
134 case _DRM_CONSISTENT:
136 phys = vtophys((char *)map->virtual + offset);
139 DRM_ERROR("bad map type %d\n", type);
140 return -1; /* This should never happen. */
143 ap->a_result = atop(phys);
148 drm_mmap_single(struct dev_mmap_single_args *ap)
150 struct cdev *kdev = ap->a_head.a_dev;
152 return drm_gem_mmap_single(kdev, ap->a_offset, ap->a_size,
153 ap->a_object, ap->a_nprot);