2d21d6ab5788edbf44b784c1dff83eb83edc7b73
[dragonfly.git] / sys / dev / drm2 / 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  * $FreeBSD: src/sys/dev/drm2/drm_vm.c,v 1.1 2012/05/22 11:07:44 kib Exp $
24  */
25
26 /** @file drm_vm.c
27  * Support code for mmaping of DRM maps.
28  */
29
30 #include <sys/conf.h>
31 #include <dev/drm2/drmP.h>
32 #include <dev/drm2/drm.h>
33
34 int
35 drm_mmap(struct dev_mmap_args *ap)
36 /*
37                 struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
38     int prot, vm_memattr_t *memattr)*/
39 {
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;
44         drm_local_map_t *map;
45         enum drm_map_type type;
46         vm_paddr_t phys;
47         int error;
48
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.
52          */
53         error = devfs_get_cdevpriv((void **)&file_priv);
54         if (error == ENOENT) {
55                 DRM_ERROR("Could not find authenticator!\n");
56                 return EINVAL;
57         }
58
59         if (file_priv && !file_priv->authenticated)
60                 return EACCES;
61
62         DRM_DEBUG("called with offset %016jx\n", offset);
63         if (dev->dma && offset < ptoa(dev->dma->page_count)) {
64                 drm_device_dma_t *dma = dev->dma;
65
66                 DRM_SPINLOCK(&dev->dma_lock);
67
68                 if (dma->pagelist != NULL) {
69                         unsigned long page = offset >> PAGE_SHIFT;
70                         phys = dma->pagelist[page];
71
72                         DRM_SPINUNLOCK(&dev->dma_lock);
73                         return 0;
74                 } else {
75                         DRM_SPINUNLOCK(&dev->dma_lock);
76                         return -1;
77                 }
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         */
88         DRM_LOCK(dev);
89         TAILQ_FOREACH(map, &dev->maplist, link) {
90                 if (offset >> DRM_MAP_HANDLE_SHIFT ==
91                     (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT)
92                         break;
93         }
94
95         if (map == NULL) {
96                 DRM_DEBUG("Can't find map, request offset = %016jx\n", offset);
97                 TAILQ_FOREACH(map, &dev->maplist, link) {
98                         DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
99                             map->offset, (unsigned long)map->handle);
100                 }
101                 DRM_UNLOCK(dev);
102                 return -1;
103         }
104         if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
105                 DRM_UNLOCK(dev);
106                 DRM_DEBUG("restricted map\n");
107                 return -1;
108         }
109         type = map->type;
110         DRM_UNLOCK(dev);
111
112         offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1);
113
114         switch (type) {
115         case _DRM_FRAME_BUFFER:
116         case _DRM_AGP:
117 #if 0
118                 /* FIXME */
119                 *memattr = VM_MEMATTR_WRITE_COMBINING;
120 #endif
121                 /* FALLTHROUGH */
122         case _DRM_REGISTERS:
123                 phys = map->offset + offset;
124                 break;
125         case _DRM_SCATTER_GATHER:
126 #if 0
127                 /* FIXME */
128                 *memattr = VM_MEMATTR_WRITE_COMBINING;
129 #endif
130                 /* FALLTHROUGH */
131         case _DRM_CONSISTENT:
132         case _DRM_SHM:
133                 phys = vtophys((char *)map->virtual + offset);
134                 break;
135         default:
136                 DRM_ERROR("bad map type %d\n", type);
137                 return -1;      /* This should never happen. */
138         }
139
140         ap->a_result = atop(phys);
141         return 0;
142 }
143