drm_vm.c revision 1.1.2.4 1 /* $NetBSD: drm_vm.c,v 1.1.2.4 2014/01/22 16:40:44 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_vm.c,v 1.1.2.4 2014/01/22 16:40:44 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/conf.h>
37
38 #include <uvm/uvm_extern.h>
39 #include <uvm/uvm_device.h>
40
41 #include <drm/drmP.h>
42
43 static paddr_t drm_mmap_paddr_locked(struct drm_device *, off_t, int);
44 static paddr_t drm_mmap_dma_paddr(struct drm_device *, off_t, int);
45 static paddr_t drm_mmap_map_paddr(struct drm_device *, struct drm_local_map *,
46 off_t, int);
47
48 extern struct cdevsw drm_cdevsw; /* XXX */
49
50 int
51 drm_mmap_object(struct drm_device *dev, off_t offset, size_t size, int prot,
52 struct uvm_object **uobjp)
53 {
54 dev_t devno = cdevsw_lookup_major(&drm_cdevsw);
55 struct uvm_object *uobj;
56
57 uobj = udv_attach(&devno, prot, offset, size);
58 if (uobj == NULL)
59 return -EINVAL;
60
61 *uobjp = uobj;
62 return 0;
63 }
64
65 paddr_t
66 drm_mmap_paddr(struct drm_device *dev, off_t byte_offset, int prot)
67 {
68 paddr_t paddr;
69
70 mutex_lock(&dev->struct_mutex);
71 paddr = drm_mmap_paddr_locked(dev, byte_offset, prot);
72 mutex_unlock(&dev->struct_mutex);
73
74 return paddr;
75 }
76
77 static paddr_t
78 drm_mmap_paddr_locked(struct drm_device *dev, off_t byte_offset, int prot)
79 {
80 const off_t page_offset = (byte_offset >> PAGE_SHIFT);
81 struct drm_hash_item *hash;
82
83 KASSERT(mutex_is_locked(&dev->struct_mutex));
84
85 if (byte_offset != (byte_offset & ~(PAGE_SIZE-1)))
86 return -1;
87
88 if ((dev->dma != NULL) &&
89 (0 <= byte_offset) &&
90 (byte_offset <= (dev->dma->page_count << PAGE_SHIFT)))
91 return drm_mmap_dma_paddr(dev, byte_offset, prot);
92
93 if (drm_ht_find_item(&dev->map_hash, page_offset, &hash))
94 return -1;
95
96 struct drm_local_map *const map = drm_hash_entry(hash,
97 struct drm_map_list, hash)->map;
98 if (map == NULL)
99 return -1;
100
101 /*
102 * XXX FreeBSD drops the mutex at this point, which would be
103 * nice, to allow sleeping in bus_dma cruft, but I don't know
104 * what guarantees the map will continue to exist.
105 */
106
107 if (ISSET(map->flags, _DRM_RESTRICTED) && !DRM_SUSER())
108 return -1;
109
110 if (byte_offset < map->offset)
111 return -1;
112
113 return drm_mmap_map_paddr(dev, map, (map->offset - byte_offset),
114 prot);
115 }
116
117 static paddr_t
118 drm_mmap_dma_paddr(struct drm_device *dev, off_t byte_offset, int prot)
119 {
120 const off_t page_offset = (byte_offset >> PAGE_SHIFT);
121
122 KASSERT(mutex_is_locked(&dev->struct_mutex));
123
124 if (dev->dma->pagelist == NULL)
125 return (paddr_t)-1;
126
127 if (page_offset >= dev->dma->page_count)
128 return (paddr_t)-1;
129
130 return dev->dma->pagelist[page_offset];
131 }
132
133 static paddr_t
134 drm_mmap_map_paddr(struct drm_device *dev, struct drm_local_map *map,
135 off_t byte_offset, int prot)
136 {
137 int flags = 0;
138
139 switch (map->type) {
140 case _DRM_FRAME_BUFFER:
141 case _DRM_AGP:
142 flags |= BUS_SPACE_MAP_PREFETCHABLE;
143 /* Fall through. */
144
145 case _DRM_REGISTERS:
146 flags |= BUS_SPACE_MAP_LINEAR; /* XXX Why? */
147
148 return bus_space_mmap(dev->bst, map->offset, byte_offset, prot,
149 flags);
150
151 case _DRM_CONSISTENT: {
152 struct drm_dma_handle *const dmah = map->lm_data.dmah;
153
154 return bus_dmamem_mmap(dev->dmat, &dmah->dmah_seg, 1,
155 byte_offset, prot,
156 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */
157 /* XXX What else? BUS_DMA_COHERENT? */
158 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
159 }
160
161 case _DRM_SCATTER_GATHER: {
162 struct drm_sg_mem *const sg = dev->sg;
163
164 #if 0 /* XXX */
165 KASSERT(sg == map->lm_data.sg);
166 #endif
167
168 return bus_dmamem_mmap(dev->dmat, sg->sg_segs, sg->sg_nsegs,
169 byte_offset, prot,
170 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */
171 /* XXX What else? BUS_DMA_COHERENT? */
172 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
173 }
174
175 case _DRM_SHM:
176 case _DRM_GEM:
177 default:
178 return (paddr_t)-1;
179 }
180 }
181