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>
38 * Support code for mmaping of DRM maps.
41 #include <dev/drm2/drmP.h>
42 #include <dev/drm2/drm.h>
46 drm_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
47 int prot, vm_memattr_t *memattr)
49 struct drm_device *dev = drm_get_device_from_kdev(kdev);
50 struct drm_file *file_priv = NULL;
52 enum drm_map_type type;
56 /* d_mmap gets called twice, we can only reference file_priv during
57 * the first call. We need to assume that if error is EBADF the
58 * call was successful and the client is authenticated.
60 error = devfs_get_cdevpriv((void **)&file_priv);
61 if (error == ENOENT) {
62 DRM_ERROR("Could not find authenticator!\n");
66 if (file_priv && !file_priv->authenticated)
69 DRM_DEBUG("called with offset %016jx\n", offset);
70 if (dev->dma && offset < ptoa(dev->dma->page_count)) {
71 drm_device_dma_t *dma = dev->dma;
73 DRM_SPINLOCK(&dev->dma_lock);
75 if (dma->pagelist != NULL) {
76 unsigned long page = offset >> PAGE_SHIFT;
77 unsigned long phys = dma->pagelist[page];
79 DRM_SPINUNLOCK(&dev->dma_lock);
83 DRM_SPINUNLOCK(&dev->dma_lock);
88 /* A sequential search of a linked list is
89 fine here because: 1) there will only be
90 about 5-10 entries in the list and, 2) a
91 DRI client only has to do this mapping
92 once, so it doesn't have to be optimized
93 for performance, even if the list was a
97 TAILQ_FOREACH(map, &dev->maplist, link) {
98 if (offset >> DRM_MAP_HANDLE_SHIFT ==
99 (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT)
104 DRM_DEBUG("Can't find map, request offset = %016jx\n", offset);
105 TAILQ_FOREACH(map, &dev->maplist, link) {
106 DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
107 map->offset, (unsigned long)map->handle);
112 if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
114 DRM_DEBUG("restricted map\n");
120 offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1);
123 case _DRM_FRAME_BUFFER:
125 *memattr = VM_MEMATTR_WRITE_COMBINING;
128 phys = map->offset + offset;
130 case _DRM_SCATTER_GATHER:
131 *memattr = VM_MEMATTR_WRITE_COMBINING;
133 case _DRM_CONSISTENT:
135 phys = vtophys((char *)map->virtual + offset);
138 DRM_ERROR("bad map type %d\n", type);
139 return -1; /* This should never happen. */
145 #endif /* FREEBSD_NOTYET */