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.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Support code for mmaping of DRM maps.
43 #include <dev/drm2/drmP.h>
44 #include <dev/drm2/drm.h>
48 drm_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
49 int prot, vm_memattr_t *memattr)
51 struct drm_device *dev = drm_get_device_from_kdev(kdev);
52 struct drm_file *file_priv = NULL;
54 enum drm_map_type type;
58 /* d_mmap gets called twice, we can only reference file_priv during
59 * the first call. We need to assume that if error is EBADF the
60 * call was succesful and the client is authenticated.
62 error = devfs_get_cdevpriv((void **)&file_priv);
63 if (error == ENOENT) {
64 DRM_ERROR("Could not find authenticator!\n");
68 if (file_priv && !file_priv->authenticated)
71 DRM_DEBUG("called with offset %016jx\n", offset);
72 if (dev->dma && offset < ptoa(dev->dma->page_count)) {
73 drm_device_dma_t *dma = dev->dma;
75 DRM_SPINLOCK(&dev->dma_lock);
77 if (dma->pagelist != NULL) {
78 unsigned long page = offset >> PAGE_SHIFT;
79 unsigned long phys = dma->pagelist[page];
81 DRM_SPINUNLOCK(&dev->dma_lock);
85 DRM_SPINUNLOCK(&dev->dma_lock);
90 /* A sequential search of a linked list is
91 fine here because: 1) there will only be
92 about 5-10 entries in the list and, 2) a
93 DRI client only has to do this mapping
94 once, so it doesn't have to be optimized
95 for performance, even if the list was a
99 TAILQ_FOREACH(map, &dev->maplist, link) {
100 if (offset >> DRM_MAP_HANDLE_SHIFT ==
101 (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT)
106 DRM_DEBUG("Can't find map, request offset = %016jx\n", offset);
107 TAILQ_FOREACH(map, &dev->maplist, link) {
108 DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
109 map->offset, (unsigned long)map->handle);
114 if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
116 DRM_DEBUG("restricted map\n");
122 offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1);
125 case _DRM_FRAME_BUFFER:
127 *memattr = VM_MEMATTR_WRITE_COMBINING;
130 phys = map->offset + offset;
132 case _DRM_SCATTER_GATHER:
133 *memattr = VM_MEMATTR_WRITE_COMBINING;
135 case _DRM_CONSISTENT:
137 phys = vtophys((char *)map->virtual + offset);
140 DRM_ERROR("bad map type %d\n", type);
141 return -1; /* This should never happen. */
147 #endif /* FREEBSD_NOTYET */