OSDN Git Service

nouveau/nv50: hack up initial channel context from current state
[android-x86/external-libdrm.git] / bsd-core / 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 "drmP.h"
25 #include "drm.h"
26
27 #if defined(__FreeBSD__) && __FreeBSD_version >= 500102
28 int drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr,
29     int prot)
30 #elif defined(__FreeBSD__)
31 int drm_mmap(dev_t kdev, vm_offset_t offset, int prot)
32 #elif defined(__NetBSD__) || defined(__OpenBSD__)
33 paddr_t drm_mmap(dev_t kdev, off_t offset, int prot)
34 #endif
35 {
36         drm_device_t *dev = drm_get_device_from_kdev(kdev);
37         drm_local_map_t *map;
38         drm_file_t *priv;
39         drm_map_type_t type;
40 #ifdef __FreeBSD__
41         vm_paddr_t phys;
42 #else
43         paddr_t phys;
44 #endif
45
46         DRM_LOCK();
47         priv = drm_find_file_by_proc(dev, DRM_CURPROC);
48         DRM_UNLOCK();
49         if (priv == NULL) {
50                 DRM_ERROR("can't find authenticator\n");
51                 return EINVAL;
52         }
53
54         if (!priv->authenticated)
55                 return EACCES;
56
57         if (dev->dma && offset >= 0 && 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 #if defined(__FreeBSD__) && __FreeBSD_version >= 500102
67                         *paddr = phys;
68                         DRM_SPINUNLOCK(&dev->dma_lock);
69                         return 0;
70 #else
71                         return atop(phys);
72 #endif
73                 } else {
74                         DRM_SPINUNLOCK(&dev->dma_lock);
75                         return -1;
76                 }
77                 DRM_SPINUNLOCK(&dev->dma_lock);
78         }
79
80                                 /* A sequential search of a linked list is
81                                    fine here because: 1) there will only be
82                                    about 5-10 entries in the list and, 2) a
83                                    DRI client only has to do this mapping
84                                    once, so it doesn't have to be optimized
85                                    for performance, even if the list was a
86                                    bit longer. */
87         DRM_LOCK();
88         TAILQ_FOREACH(map, &dev->maplist, link) {
89                 if (offset >= map->offset && offset < map->offset + map->size)
90                         break;
91         }
92
93         if (map == NULL) {
94                 DRM_UNLOCK();
95                 DRM_DEBUG("can't find map\n");
96                 return -1;
97         }
98         if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
99                 DRM_UNLOCK();
100                 DRM_DEBUG("restricted map\n");
101                 return -1;
102         }
103         type = map->type;
104         DRM_UNLOCK();
105
106         switch (type) {
107         case _DRM_FRAME_BUFFER:
108         case _DRM_REGISTERS:
109         case _DRM_AGP:
110                 phys = offset;
111                 break;
112         case _DRM_CONSISTENT:
113                 phys = vtophys((char *)map->handle + (offset - map->offset));
114                 break;
115         case _DRM_SCATTER_GATHER:
116         case _DRM_SHM:
117                 phys = vtophys(offset);
118                 break;
119         default:
120                 DRM_ERROR("bad map type %d\n", type);
121                 return -1;      /* This should never happen. */
122         }
123
124 #if defined(__FreeBSD__) && __FreeBSD_version >= 500102
125         *paddr = phys;
126         return 0;
127 #else
128         return atop(phys);
129 #endif
130 }
131