]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/drm/drm_vm.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / drm / drm_vm.c
1 /*-
2  * Copyright 2003 Eric Anholt
3  * All Rights Reserved.
4  *
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:
11  * 
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
14  * Software.
15  * 
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.
22  */
23
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
26
27 /** @file drm_vm.c
28  * Support code for mmaping of DRM maps.
29  */
30
31 #include "dev/drm/drmP.h"
32 #include "dev/drm/drm.h"
33
34 int drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr,
35     int prot)
36 {
37         struct drm_device *dev = drm_get_device_from_kdev(kdev);
38         struct drm_file *file_priv = NULL;
39         drm_local_map_t *map;
40         enum drm_map_type type;
41         vm_paddr_t phys;
42         int error;
43
44         /* d_mmap gets called twice, we can only reference file_priv during
45          * the first call.  We need to assume that if error is EBADF the
46          * call was succesful and the client is authenticated.
47          */
48         error = devfs_get_cdevpriv((void **)&file_priv);
49         if (error == ENOENT) {
50                 DRM_ERROR("Could not find authenticator!\n");
51                 return EINVAL;
52         }
53
54         if (file_priv && !file_priv->authenticated)
55                 return EACCES;
56
57         if (dev->dma && offset < ptoa(dev->dma->page_count)) {
58                 drm_device_dma_t *dma = dev->dma;
59
60                 DRM_SPINLOCK(&dev->dma_lock);
61
62                 if (dma->pagelist != NULL) {
63                         unsigned long page = offset >> PAGE_SHIFT;
64                         unsigned long phys = dma->pagelist[page];
65
66                         DRM_SPINUNLOCK(&dev->dma_lock);
67                         *paddr = phys;
68                         return 0;
69                 } else {
70                         DRM_SPINUNLOCK(&dev->dma_lock);
71                         return -1;
72                 }
73         }
74
75                                 /* A sequential search of a linked list is
76                                    fine here because: 1) there will only be
77                                    about 5-10 entries in the list and, 2) a
78                                    DRI client only has to do this mapping
79                                    once, so it doesn't have to be optimized
80                                    for performance, even if the list was a
81                                    bit longer. */
82         DRM_LOCK();
83         TAILQ_FOREACH(map, &dev->maplist, link) {
84                 if (offset >= map->offset && offset < map->offset + map->size)
85                         break;
86         }
87
88         if (map == NULL) {
89                 DRM_DEBUG("Can't find map, requested offset = %016lx\n",
90                     (unsigned long)offset);
91                 TAILQ_FOREACH(map, &dev->maplist, link) {
92                         DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
93                             (unsigned long)map->offset,
94                             (unsigned long)map->handle);
95                 }
96                 DRM_UNLOCK();
97                 return -1;
98         }
99         if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
100                 DRM_UNLOCK();
101                 DRM_DEBUG("restricted map\n");
102                 return -1;
103         }
104         type = map->type;
105         DRM_UNLOCK();
106
107         switch (type) {
108         case _DRM_FRAME_BUFFER:
109         case _DRM_REGISTERS:
110         case _DRM_AGP:
111                 phys = offset;
112                 break;
113         case _DRM_CONSISTENT:
114                 phys = vtophys((char *)map->handle + (offset - map->offset));
115                 break;
116         case _DRM_SCATTER_GATHER:
117         case _DRM_SHM:
118                 phys = vtophys(offset);
119                 break;
120         default:
121                 DRM_ERROR("bad map type %d\n", type);
122                 return -1;      /* This should never happen. */
123         }
124
125         *paddr = phys;
126         return 0;
127 }
128