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