drm_bufs.c revision 1.1.1.1.4.3 1 1.1.1.1.4.2 rmind /**
2 1.1.1.1.4.2 rmind * \file drm_bufs.c
3 1.1.1.1.4.2 rmind * Generic buffer template
4 1.1.1.1.4.2 rmind *
5 1.1.1.1.4.2 rmind * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
6 1.1.1.1.4.2 rmind * \author Gareth Hughes <gareth (at) valinux.com>
7 1.1.1.1.4.2 rmind */
8 1.1.1.1.4.2 rmind
9 1.1.1.1.4.2 rmind /*
10 1.1.1.1.4.2 rmind * Created: Thu Nov 23 03:10:50 2000 by gareth (at) valinux.com
11 1.1.1.1.4.2 rmind *
12 1.1.1.1.4.2 rmind * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 1.1.1.1.4.2 rmind * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 1.1.1.1.4.2 rmind * All Rights Reserved.
15 1.1.1.1.4.2 rmind *
16 1.1.1.1.4.2 rmind * Permission is hereby granted, free of charge, to any person obtaining a
17 1.1.1.1.4.2 rmind * copy of this software and associated documentation files (the "Software"),
18 1.1.1.1.4.2 rmind * to deal in the Software without restriction, including without limitation
19 1.1.1.1.4.2 rmind * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 1.1.1.1.4.2 rmind * and/or sell copies of the Software, and to permit persons to whom the
21 1.1.1.1.4.2 rmind * Software is furnished to do so, subject to the following conditions:
22 1.1.1.1.4.2 rmind *
23 1.1.1.1.4.2 rmind * The above copyright notice and this permission notice (including the next
24 1.1.1.1.4.2 rmind * paragraph) shall be included in all copies or substantial portions of the
25 1.1.1.1.4.2 rmind * Software.
26 1.1.1.1.4.2 rmind *
27 1.1.1.1.4.2 rmind * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 1.1.1.1.4.2 rmind * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 1.1.1.1.4.2 rmind * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 1.1.1.1.4.2 rmind * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 1.1.1.1.4.2 rmind * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 1.1.1.1.4.2 rmind * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 1.1.1.1.4.2 rmind * OTHER DEALINGS IN THE SOFTWARE.
34 1.1.1.1.4.2 rmind */
35 1.1.1.1.4.2 rmind
36 1.1.1.1.4.2 rmind #include <linux/vmalloc.h>
37 1.1.1.1.4.2 rmind #include <linux/slab.h>
38 1.1.1.1.4.3 rmind #include <linux/sched.h>
39 1.1.1.1.4.2 rmind #include <linux/log2.h>
40 1.1.1.1.4.2 rmind #include <linux/export.h>
41 1.1.1.1.4.3 rmind #include <linux/mm.h>
42 1.1.1.1.4.3 rmind #include <asm/bug.h>
43 1.1.1.1.4.2 rmind #include <asm/shmparam.h>
44 1.1.1.1.4.2 rmind #include <drm/drmP.h>
45 1.1.1.1.4.2 rmind
46 1.1.1.1.4.2 rmind static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
47 1.1.1.1.4.2 rmind struct drm_local_map *map)
48 1.1.1.1.4.2 rmind {
49 1.1.1.1.4.2 rmind struct drm_map_list *entry;
50 1.1.1.1.4.2 rmind list_for_each_entry(entry, &dev->maplist, head) {
51 1.1.1.1.4.2 rmind /*
52 1.1.1.1.4.2 rmind * Because the kernel-userspace ABI is fixed at a 32-bit offset
53 1.1.1.1.4.2 rmind * while PCI resources may live above that, we only compare the
54 1.1.1.1.4.2 rmind * lower 32 bits of the map offset for maps of type
55 1.1.1.1.4.2 rmind * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
56 1.1.1.1.4.2 rmind * It is assumed that if a driver have more than one resource
57 1.1.1.1.4.2 rmind * of each type, the lower 32 bits are different.
58 1.1.1.1.4.2 rmind */
59 1.1.1.1.4.2 rmind if (!entry->map ||
60 1.1.1.1.4.2 rmind map->type != entry->map->type ||
61 1.1.1.1.4.2 rmind entry->master != dev->primary->master)
62 1.1.1.1.4.2 rmind continue;
63 1.1.1.1.4.2 rmind switch (map->type) {
64 1.1.1.1.4.2 rmind case _DRM_SHM:
65 1.1.1.1.4.2 rmind if (map->flags != _DRM_CONTAINS_LOCK)
66 1.1.1.1.4.2 rmind break;
67 1.1.1.1.4.2 rmind return entry;
68 1.1.1.1.4.2 rmind case _DRM_REGISTERS:
69 1.1.1.1.4.2 rmind case _DRM_FRAME_BUFFER:
70 1.1.1.1.4.2 rmind if ((entry->map->offset & 0xffffffff) ==
71 1.1.1.1.4.2 rmind (map->offset & 0xffffffff))
72 1.1.1.1.4.2 rmind return entry;
73 1.1.1.1.4.2 rmind default: /* Make gcc happy */
74 1.1.1.1.4.2 rmind ;
75 1.1.1.1.4.2 rmind }
76 1.1.1.1.4.2 rmind if (entry->map->offset == map->offset)
77 1.1.1.1.4.2 rmind return entry;
78 1.1.1.1.4.2 rmind }
79 1.1.1.1.4.2 rmind
80 1.1.1.1.4.2 rmind return NULL;
81 1.1.1.1.4.2 rmind }
82 1.1.1.1.4.2 rmind
83 1.1.1.1.4.2 rmind static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
84 1.1.1.1.4.2 rmind unsigned long user_token, int hashed_handle, int shm)
85 1.1.1.1.4.2 rmind {
86 1.1.1.1.4.2 rmind int use_hashed_handle, shift;
87 1.1.1.1.4.2 rmind unsigned long add;
88 1.1.1.1.4.2 rmind
89 1.1.1.1.4.3 rmind use_hashed_handle = (user_token &~ 0xffffffffUL) || hashed_handle;
90 1.1.1.1.4.2 rmind if (!use_hashed_handle) {
91 1.1.1.1.4.2 rmind int ret;
92 1.1.1.1.4.2 rmind hash->key = user_token >> PAGE_SHIFT;
93 1.1.1.1.4.2 rmind ret = drm_ht_insert_item(&dev->map_hash, hash);
94 1.1.1.1.4.2 rmind if (ret != -EINVAL)
95 1.1.1.1.4.2 rmind return ret;
96 1.1.1.1.4.2 rmind }
97 1.1.1.1.4.2 rmind
98 1.1.1.1.4.2 rmind shift = 0;
99 1.1.1.1.4.2 rmind add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
100 1.1.1.1.4.2 rmind if (shm && (SHMLBA > PAGE_SIZE)) {
101 1.1.1.1.4.2 rmind int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
102 1.1.1.1.4.2 rmind
103 1.1.1.1.4.2 rmind /* For shared memory, we have to preserve the SHMLBA
104 1.1.1.1.4.2 rmind * bits of the eventual vma->vm_pgoff value during
105 1.1.1.1.4.2 rmind * mmap(). Otherwise we run into cache aliasing problems
106 1.1.1.1.4.2 rmind * on some platforms. On these platforms, the pgoff of
107 1.1.1.1.4.2 rmind * a mmap() request is used to pick a suitable virtual
108 1.1.1.1.4.2 rmind * address for the mmap() region such that it will not
109 1.1.1.1.4.2 rmind * cause cache aliasing problems.
110 1.1.1.1.4.2 rmind *
111 1.1.1.1.4.2 rmind * Therefore, make sure the SHMLBA relevant bits of the
112 1.1.1.1.4.2 rmind * hash value we use are equal to those in the original
113 1.1.1.1.4.2 rmind * kernel virtual address.
114 1.1.1.1.4.2 rmind */
115 1.1.1.1.4.2 rmind shift = bits;
116 1.1.1.1.4.2 rmind add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
117 1.1.1.1.4.2 rmind }
118 1.1.1.1.4.2 rmind
119 1.1.1.1.4.2 rmind return drm_ht_just_insert_please(&dev->map_hash, hash,
120 1.1.1.1.4.2 rmind user_token, 32 - PAGE_SHIFT - 3,
121 1.1.1.1.4.2 rmind shift, add);
122 1.1.1.1.4.2 rmind }
123 1.1.1.1.4.2 rmind
124 1.1.1.1.4.2 rmind /**
125 1.1.1.1.4.2 rmind * Core function to create a range of memory available for mapping by a
126 1.1.1.1.4.2 rmind * non-root process.
127 1.1.1.1.4.2 rmind *
128 1.1.1.1.4.2 rmind * Adjusts the memory offset to its absolute value according to the mapping
129 1.1.1.1.4.2 rmind * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
130 1.1.1.1.4.2 rmind * applicable and if supported by the kernel.
131 1.1.1.1.4.2 rmind */
132 1.1.1.1.4.2 rmind static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
133 1.1.1.1.4.2 rmind unsigned int size, enum drm_map_type type,
134 1.1.1.1.4.2 rmind enum drm_map_flags flags,
135 1.1.1.1.4.2 rmind struct drm_map_list ** maplist)
136 1.1.1.1.4.2 rmind {
137 1.1.1.1.4.2 rmind struct drm_local_map *map;
138 1.1.1.1.4.2 rmind struct drm_map_list *list;
139 1.1.1.1.4.2 rmind drm_dma_handle_t *dmah;
140 1.1.1.1.4.2 rmind unsigned long user_token;
141 1.1.1.1.4.2 rmind int ret;
142 1.1.1.1.4.2 rmind
143 1.1.1.1.4.2 rmind map = kmalloc(sizeof(*map), GFP_KERNEL);
144 1.1.1.1.4.2 rmind if (!map)
145 1.1.1.1.4.2 rmind return -ENOMEM;
146 1.1.1.1.4.2 rmind
147 1.1.1.1.4.2 rmind map->offset = offset;
148 1.1.1.1.4.2 rmind map->size = size;
149 1.1.1.1.4.2 rmind map->flags = flags;
150 1.1.1.1.4.2 rmind map->type = type;
151 1.1.1.1.4.2 rmind
152 1.1.1.1.4.2 rmind /* Only allow shared memory to be removable since we only keep enough
153 1.1.1.1.4.2 rmind * book keeping information about shared memory to allow for removal
154 1.1.1.1.4.2 rmind * when processes fork.
155 1.1.1.1.4.2 rmind */
156 1.1.1.1.4.2 rmind if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
157 1.1.1.1.4.2 rmind kfree(map);
158 1.1.1.1.4.2 rmind return -EINVAL;
159 1.1.1.1.4.2 rmind }
160 1.1.1.1.4.2 rmind DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
161 1.1.1.1.4.2 rmind (unsigned long long)map->offset, map->size, map->type);
162 1.1.1.1.4.2 rmind
163 1.1.1.1.4.2 rmind /* page-align _DRM_SHM maps. They are allocated here so there is no security
164 1.1.1.1.4.2 rmind * hole created by that and it works around various broken drivers that use
165 1.1.1.1.4.2 rmind * a non-aligned quantity to map the SAREA. --BenH
166 1.1.1.1.4.2 rmind */
167 1.1.1.1.4.2 rmind if (map->type == _DRM_SHM)
168 1.1.1.1.4.2 rmind map->size = PAGE_ALIGN(map->size);
169 1.1.1.1.4.2 rmind
170 1.1.1.1.4.2 rmind if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
171 1.1.1.1.4.2 rmind kfree(map);
172 1.1.1.1.4.2 rmind return -EINVAL;
173 1.1.1.1.4.2 rmind }
174 1.1.1.1.4.2 rmind map->mtrr = -1;
175 1.1.1.1.4.2 rmind map->handle = NULL;
176 1.1.1.1.4.2 rmind
177 1.1.1.1.4.2 rmind switch (map->type) {
178 1.1.1.1.4.2 rmind case _DRM_REGISTERS:
179 1.1.1.1.4.2 rmind case _DRM_FRAME_BUFFER:
180 1.1.1.1.4.3 rmind #ifndef __NetBSD__ /* XXX No idea what this is for... */
181 1.1.1.1.4.2 rmind #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
182 1.1.1.1.4.2 rmind if (map->offset + (map->size-1) < map->offset ||
183 1.1.1.1.4.2 rmind map->offset < virt_to_phys(high_memory)) {
184 1.1.1.1.4.2 rmind kfree(map);
185 1.1.1.1.4.2 rmind return -EINVAL;
186 1.1.1.1.4.2 rmind }
187 1.1.1.1.4.2 rmind #endif
188 1.1.1.1.4.3 rmind #endif
189 1.1.1.1.4.2 rmind /* Some drivers preinitialize some maps, without the X Server
190 1.1.1.1.4.2 rmind * needing to be aware of it. Therefore, we just return success
191 1.1.1.1.4.2 rmind * when the server tries to create a duplicate map.
192 1.1.1.1.4.2 rmind */
193 1.1.1.1.4.2 rmind list = drm_find_matching_map(dev, map);
194 1.1.1.1.4.2 rmind if (list != NULL) {
195 1.1.1.1.4.2 rmind if (list->map->size != map->size) {
196 1.1.1.1.4.2 rmind DRM_DEBUG("Matching maps of type %d with "
197 1.1.1.1.4.2 rmind "mismatched sizes, (%ld vs %ld)\n",
198 1.1.1.1.4.2 rmind map->type, map->size,
199 1.1.1.1.4.2 rmind list->map->size);
200 1.1.1.1.4.2 rmind list->map->size = map->size;
201 1.1.1.1.4.2 rmind }
202 1.1.1.1.4.2 rmind
203 1.1.1.1.4.2 rmind kfree(map);
204 1.1.1.1.4.2 rmind *maplist = list;
205 1.1.1.1.4.2 rmind return 0;
206 1.1.1.1.4.2 rmind }
207 1.1.1.1.4.2 rmind
208 1.1.1.1.4.2 rmind if (drm_core_has_MTRR(dev)) {
209 1.1.1.1.4.2 rmind if (map->type == _DRM_FRAME_BUFFER ||
210 1.1.1.1.4.2 rmind (map->flags & _DRM_WRITE_COMBINING)) {
211 1.1.1.1.4.2 rmind map->mtrr = mtrr_add(map->offset, map->size,
212 1.1.1.1.4.2 rmind MTRR_TYPE_WRCOMB, 1);
213 1.1.1.1.4.2 rmind }
214 1.1.1.1.4.2 rmind }
215 1.1.1.1.4.2 rmind if (map->type == _DRM_REGISTERS) {
216 1.1.1.1.4.3 rmind #ifdef __NetBSD__
217 1.1.1.1.4.3 rmind map->handle = drm_ioremap(dev, map);
218 1.1.1.1.4.3 rmind #else
219 1.1.1.1.4.2 rmind map->handle = ioremap(map->offset, map->size);
220 1.1.1.1.4.3 rmind #endif
221 1.1.1.1.4.2 rmind if (!map->handle) {
222 1.1.1.1.4.2 rmind kfree(map);
223 1.1.1.1.4.2 rmind return -ENOMEM;
224 1.1.1.1.4.2 rmind }
225 1.1.1.1.4.2 rmind }
226 1.1.1.1.4.2 rmind
227 1.1.1.1.4.2 rmind break;
228 1.1.1.1.4.2 rmind case _DRM_SHM:
229 1.1.1.1.4.2 rmind list = drm_find_matching_map(dev, map);
230 1.1.1.1.4.2 rmind if (list != NULL) {
231 1.1.1.1.4.2 rmind if(list->map->size != map->size) {
232 1.1.1.1.4.2 rmind DRM_DEBUG("Matching maps of type %d with "
233 1.1.1.1.4.2 rmind "mismatched sizes, (%ld vs %ld)\n",
234 1.1.1.1.4.2 rmind map->type, map->size, list->map->size);
235 1.1.1.1.4.2 rmind list->map->size = map->size;
236 1.1.1.1.4.2 rmind }
237 1.1.1.1.4.2 rmind
238 1.1.1.1.4.2 rmind kfree(map);
239 1.1.1.1.4.2 rmind *maplist = list;
240 1.1.1.1.4.2 rmind return 0;
241 1.1.1.1.4.2 rmind }
242 1.1.1.1.4.2 rmind map->handle = vmalloc_user(map->size);
243 1.1.1.1.4.2 rmind DRM_DEBUG("%lu %d %p\n",
244 1.1.1.1.4.2 rmind map->size, drm_order(map->size), map->handle);
245 1.1.1.1.4.2 rmind if (!map->handle) {
246 1.1.1.1.4.2 rmind kfree(map);
247 1.1.1.1.4.2 rmind return -ENOMEM;
248 1.1.1.1.4.2 rmind }
249 1.1.1.1.4.2 rmind map->offset = (unsigned long)map->handle;
250 1.1.1.1.4.2 rmind if (map->flags & _DRM_CONTAINS_LOCK) {
251 1.1.1.1.4.2 rmind /* Prevent a 2nd X Server from creating a 2nd lock */
252 1.1.1.1.4.3 rmind spin_lock(&dev->primary->master->lock.spinlock);
253 1.1.1.1.4.2 rmind if (dev->primary->master->lock.hw_lock != NULL) {
254 1.1.1.1.4.2 rmind vfree(map->handle);
255 1.1.1.1.4.2 rmind kfree(map);
256 1.1.1.1.4.3 rmind spin_unlock(&dev->primary->master->lock.spinlock);
257 1.1.1.1.4.2 rmind return -EBUSY;
258 1.1.1.1.4.2 rmind }
259 1.1.1.1.4.2 rmind dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
260 1.1.1.1.4.3 rmind spin_unlock(&dev->primary->master->lock.spinlock);
261 1.1.1.1.4.2 rmind }
262 1.1.1.1.4.2 rmind break;
263 1.1.1.1.4.2 rmind case _DRM_AGP: {
264 1.1.1.1.4.2 rmind struct drm_agp_mem *entry;
265 1.1.1.1.4.2 rmind int valid = 0;
266 1.1.1.1.4.2 rmind
267 1.1.1.1.4.2 rmind if (!drm_core_has_AGP(dev)) {
268 1.1.1.1.4.2 rmind kfree(map);
269 1.1.1.1.4.2 rmind return -EINVAL;
270 1.1.1.1.4.2 rmind }
271 1.1.1.1.4.2 rmind #ifdef __alpha__
272 1.1.1.1.4.2 rmind map->offset += dev->hose->mem_space->start;
273 1.1.1.1.4.2 rmind #endif
274 1.1.1.1.4.2 rmind /* In some cases (i810 driver), user space may have already
275 1.1.1.1.4.2 rmind * added the AGP base itself, because dev->agp->base previously
276 1.1.1.1.4.2 rmind * only got set during AGP enable. So, only add the base
277 1.1.1.1.4.2 rmind * address if the map's offset isn't already within the
278 1.1.1.1.4.2 rmind * aperture.
279 1.1.1.1.4.2 rmind */
280 1.1.1.1.4.3 rmind #ifdef __NetBSD__
281 1.1.1.1.4.3 rmind if (map->offset < dev->agp->base ||
282 1.1.1.1.4.3 rmind map->offset > dev->agp->base +
283 1.1.1.1.4.3 rmind dev->agp->agp_info.ai_aperture_size - 1) {
284 1.1.1.1.4.3 rmind map->offset += dev->agp->base;
285 1.1.1.1.4.3 rmind }
286 1.1.1.1.4.3 rmind #else
287 1.1.1.1.4.2 rmind if (map->offset < dev->agp->base ||
288 1.1.1.1.4.2 rmind map->offset > dev->agp->base +
289 1.1.1.1.4.2 rmind dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
290 1.1.1.1.4.2 rmind map->offset += dev->agp->base;
291 1.1.1.1.4.2 rmind }
292 1.1.1.1.4.3 rmind #endif
293 1.1.1.1.4.2 rmind map->mtrr = dev->agp->agp_mtrr; /* for getmap */
294 1.1.1.1.4.2 rmind
295 1.1.1.1.4.2 rmind /* This assumes the DRM is in total control of AGP space.
296 1.1.1.1.4.2 rmind * It's not always the case as AGP can be in the control
297 1.1.1.1.4.2 rmind * of user space (i.e. i810 driver). So this loop will get
298 1.1.1.1.4.2 rmind * skipped and we double check that dev->agp->memory is
299 1.1.1.1.4.2 rmind * actually set as well as being invalid before EPERM'ing
300 1.1.1.1.4.2 rmind */
301 1.1.1.1.4.2 rmind list_for_each_entry(entry, &dev->agp->memory, head) {
302 1.1.1.1.4.2 rmind if ((map->offset >= entry->bound) &&
303 1.1.1.1.4.2 rmind (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
304 1.1.1.1.4.2 rmind valid = 1;
305 1.1.1.1.4.2 rmind break;
306 1.1.1.1.4.2 rmind }
307 1.1.1.1.4.2 rmind }
308 1.1.1.1.4.2 rmind if (!list_empty(&dev->agp->memory) && !valid) {
309 1.1.1.1.4.2 rmind kfree(map);
310 1.1.1.1.4.2 rmind return -EPERM;
311 1.1.1.1.4.2 rmind }
312 1.1.1.1.4.2 rmind DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
313 1.1.1.1.4.2 rmind (unsigned long long)map->offset, map->size);
314 1.1.1.1.4.2 rmind
315 1.1.1.1.4.2 rmind break;
316 1.1.1.1.4.2 rmind }
317 1.1.1.1.4.2 rmind case _DRM_GEM:
318 1.1.1.1.4.2 rmind DRM_ERROR("tried to addmap GEM object\n");
319 1.1.1.1.4.2 rmind break;
320 1.1.1.1.4.2 rmind case _DRM_SCATTER_GATHER:
321 1.1.1.1.4.2 rmind if (!dev->sg) {
322 1.1.1.1.4.2 rmind kfree(map);
323 1.1.1.1.4.2 rmind return -EINVAL;
324 1.1.1.1.4.2 rmind }
325 1.1.1.1.4.2 rmind map->offset += (unsigned long)dev->sg->virtual;
326 1.1.1.1.4.2 rmind break;
327 1.1.1.1.4.2 rmind case _DRM_CONSISTENT:
328 1.1.1.1.4.2 rmind /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
329 1.1.1.1.4.2 rmind * As we're limiting the address to 2^32-1 (or less),
330 1.1.1.1.4.2 rmind * casting it down to 32 bits is no problem, but we
331 1.1.1.1.4.2 rmind * need to point to a 64bit variable first. */
332 1.1.1.1.4.2 rmind dmah = drm_pci_alloc(dev, map->size, map->size);
333 1.1.1.1.4.2 rmind if (!dmah) {
334 1.1.1.1.4.2 rmind kfree(map);
335 1.1.1.1.4.2 rmind return -ENOMEM;
336 1.1.1.1.4.2 rmind }
337 1.1.1.1.4.2 rmind map->handle = dmah->vaddr;
338 1.1.1.1.4.2 rmind map->offset = (unsigned long)dmah->busaddr;
339 1.1.1.1.4.3 rmind #ifdef __NetBSD__
340 1.1.1.1.4.3 rmind map->lm_data.dmah = dmah;
341 1.1.1.1.4.3 rmind #else
342 1.1.1.1.4.2 rmind kfree(dmah);
343 1.1.1.1.4.3 rmind #endif
344 1.1.1.1.4.2 rmind break;
345 1.1.1.1.4.2 rmind default:
346 1.1.1.1.4.2 rmind kfree(map);
347 1.1.1.1.4.2 rmind return -EINVAL;
348 1.1.1.1.4.2 rmind }
349 1.1.1.1.4.2 rmind
350 1.1.1.1.4.2 rmind list = kzalloc(sizeof(*list), GFP_KERNEL);
351 1.1.1.1.4.2 rmind if (!list) {
352 1.1.1.1.4.2 rmind if (map->type == _DRM_REGISTERS)
353 1.1.1.1.4.3 rmind #ifdef __NetBSD__
354 1.1.1.1.4.3 rmind drm_iounmap(dev, map);
355 1.1.1.1.4.3 rmind #else
356 1.1.1.1.4.2 rmind iounmap(map->handle);
357 1.1.1.1.4.3 rmind #endif
358 1.1.1.1.4.2 rmind kfree(map);
359 1.1.1.1.4.2 rmind return -EINVAL;
360 1.1.1.1.4.2 rmind }
361 1.1.1.1.4.2 rmind list->map = map;
362 1.1.1.1.4.2 rmind
363 1.1.1.1.4.2 rmind mutex_lock(&dev->struct_mutex);
364 1.1.1.1.4.2 rmind list_add(&list->head, &dev->maplist);
365 1.1.1.1.4.2 rmind
366 1.1.1.1.4.2 rmind /* Assign a 32-bit handle */
367 1.1.1.1.4.2 rmind /* We do it here so that dev->struct_mutex protects the increment */
368 1.1.1.1.4.2 rmind user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
369 1.1.1.1.4.2 rmind map->offset;
370 1.1.1.1.4.2 rmind ret = drm_map_handle(dev, &list->hash, user_token, 0,
371 1.1.1.1.4.2 rmind (map->type == _DRM_SHM));
372 1.1.1.1.4.2 rmind if (ret) {
373 1.1.1.1.4.2 rmind if (map->type == _DRM_REGISTERS)
374 1.1.1.1.4.3 rmind #ifdef __NetBSD__ /* XXX What about other map types...? */
375 1.1.1.1.4.3 rmind drm_iounmap(dev, map);
376 1.1.1.1.4.3 rmind #else
377 1.1.1.1.4.2 rmind iounmap(map->handle);
378 1.1.1.1.4.3 rmind #endif
379 1.1.1.1.4.2 rmind kfree(map);
380 1.1.1.1.4.2 rmind kfree(list);
381 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
382 1.1.1.1.4.2 rmind return ret;
383 1.1.1.1.4.2 rmind }
384 1.1.1.1.4.2 rmind
385 1.1.1.1.4.2 rmind list->user_token = list->hash.key << PAGE_SHIFT;
386 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
387 1.1.1.1.4.2 rmind
388 1.1.1.1.4.2 rmind if (!(map->flags & _DRM_DRIVER))
389 1.1.1.1.4.2 rmind list->master = dev->primary->master;
390 1.1.1.1.4.2 rmind *maplist = list;
391 1.1.1.1.4.2 rmind return 0;
392 1.1.1.1.4.2 rmind }
393 1.1.1.1.4.2 rmind
394 1.1.1.1.4.2 rmind int drm_addmap(struct drm_device * dev, resource_size_t offset,
395 1.1.1.1.4.2 rmind unsigned int size, enum drm_map_type type,
396 1.1.1.1.4.2 rmind enum drm_map_flags flags, struct drm_local_map ** map_ptr)
397 1.1.1.1.4.2 rmind {
398 1.1.1.1.4.2 rmind struct drm_map_list *list;
399 1.1.1.1.4.2 rmind int rc;
400 1.1.1.1.4.2 rmind
401 1.1.1.1.4.2 rmind rc = drm_addmap_core(dev, offset, size, type, flags, &list);
402 1.1.1.1.4.2 rmind if (!rc)
403 1.1.1.1.4.2 rmind *map_ptr = list->map;
404 1.1.1.1.4.2 rmind return rc;
405 1.1.1.1.4.2 rmind }
406 1.1.1.1.4.2 rmind
407 1.1.1.1.4.2 rmind EXPORT_SYMBOL(drm_addmap);
408 1.1.1.1.4.2 rmind
409 1.1.1.1.4.2 rmind /**
410 1.1.1.1.4.2 rmind * Ioctl to specify a range of memory that is available for mapping by a
411 1.1.1.1.4.2 rmind * non-root process.
412 1.1.1.1.4.2 rmind *
413 1.1.1.1.4.2 rmind * \param inode device inode.
414 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
415 1.1.1.1.4.2 rmind * \param cmd command.
416 1.1.1.1.4.2 rmind * \param arg pointer to a drm_map structure.
417 1.1.1.1.4.2 rmind * \return zero on success or a negative value on error.
418 1.1.1.1.4.2 rmind *
419 1.1.1.1.4.2 rmind */
420 1.1.1.1.4.2 rmind int drm_addmap_ioctl(struct drm_device *dev, void *data,
421 1.1.1.1.4.2 rmind struct drm_file *file_priv)
422 1.1.1.1.4.2 rmind {
423 1.1.1.1.4.2 rmind struct drm_map *map = data;
424 1.1.1.1.4.2 rmind struct drm_map_list *maplist;
425 1.1.1.1.4.2 rmind int err;
426 1.1.1.1.4.2 rmind
427 1.1.1.1.4.3 rmind #ifdef __NetBSD__
428 1.1.1.1.4.3 rmind # if 0 /* XXX Old drm did this. */
429 1.1.1.1.4.3 rmind if (!(dev->flags & (FREAD | FWRITE)))
430 1.1.1.1.4.3 rmind return -EACCES;
431 1.1.1.1.4.3 rmind # endif
432 1.1.1.1.4.3 rmind if (!(DRM_SUSER() || map->type == _DRM_AGP || map->type == _DRM_SHM))
433 1.1.1.1.4.3 rmind return -EACCES; /* XXX */
434 1.1.1.1.4.3 rmind #else
435 1.1.1.1.4.2 rmind if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
436 1.1.1.1.4.2 rmind return -EPERM;
437 1.1.1.1.4.3 rmind #endif
438 1.1.1.1.4.2 rmind
439 1.1.1.1.4.2 rmind err = drm_addmap_core(dev, map->offset, map->size, map->type,
440 1.1.1.1.4.2 rmind map->flags, &maplist);
441 1.1.1.1.4.2 rmind
442 1.1.1.1.4.2 rmind if (err)
443 1.1.1.1.4.2 rmind return err;
444 1.1.1.1.4.2 rmind
445 1.1.1.1.4.2 rmind /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
446 1.1.1.1.4.2 rmind map->handle = (void *)(unsigned long)maplist->user_token;
447 1.1.1.1.4.2 rmind return 0;
448 1.1.1.1.4.2 rmind }
449 1.1.1.1.4.2 rmind
450 1.1.1.1.4.2 rmind /**
451 1.1.1.1.4.2 rmind * Remove a map private from list and deallocate resources if the mapping
452 1.1.1.1.4.2 rmind * isn't in use.
453 1.1.1.1.4.2 rmind *
454 1.1.1.1.4.2 rmind * Searches the map on drm_device::maplist, removes it from the list, see if
455 1.1.1.1.4.2 rmind * its being used, and free any associate resource (such as MTRR's) if it's not
456 1.1.1.1.4.2 rmind * being on use.
457 1.1.1.1.4.2 rmind *
458 1.1.1.1.4.2 rmind * \sa drm_addmap
459 1.1.1.1.4.2 rmind */
460 1.1.1.1.4.2 rmind int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
461 1.1.1.1.4.2 rmind {
462 1.1.1.1.4.2 rmind struct drm_map_list *r_list = NULL, *list_t;
463 1.1.1.1.4.3 rmind #ifndef __NetBSD__
464 1.1.1.1.4.2 rmind drm_dma_handle_t dmah;
465 1.1.1.1.4.3 rmind #endif
466 1.1.1.1.4.2 rmind int found = 0;
467 1.1.1.1.4.2 rmind struct drm_master *master;
468 1.1.1.1.4.2 rmind
469 1.1.1.1.4.2 rmind /* Find the list entry for the map and remove it */
470 1.1.1.1.4.2 rmind list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
471 1.1.1.1.4.2 rmind if (r_list->map == map) {
472 1.1.1.1.4.2 rmind master = r_list->master;
473 1.1.1.1.4.2 rmind list_del(&r_list->head);
474 1.1.1.1.4.2 rmind drm_ht_remove_key(&dev->map_hash,
475 1.1.1.1.4.2 rmind r_list->user_token >> PAGE_SHIFT);
476 1.1.1.1.4.2 rmind kfree(r_list);
477 1.1.1.1.4.2 rmind found = 1;
478 1.1.1.1.4.2 rmind break;
479 1.1.1.1.4.2 rmind }
480 1.1.1.1.4.2 rmind }
481 1.1.1.1.4.2 rmind
482 1.1.1.1.4.2 rmind if (!found)
483 1.1.1.1.4.2 rmind return -EINVAL;
484 1.1.1.1.4.2 rmind
485 1.1.1.1.4.2 rmind switch (map->type) {
486 1.1.1.1.4.2 rmind case _DRM_REGISTERS:
487 1.1.1.1.4.3 rmind #ifdef __NetBSD__
488 1.1.1.1.4.3 rmind drm_iounmap(dev, map);
489 1.1.1.1.4.3 rmind #else
490 1.1.1.1.4.2 rmind iounmap(map->handle);
491 1.1.1.1.4.3 rmind #endif
492 1.1.1.1.4.2 rmind /* FALLTHROUGH */
493 1.1.1.1.4.2 rmind case _DRM_FRAME_BUFFER:
494 1.1.1.1.4.2 rmind if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
495 1.1.1.1.4.2 rmind int retcode;
496 1.1.1.1.4.2 rmind retcode = mtrr_del(map->mtrr, map->offset, map->size);
497 1.1.1.1.4.2 rmind DRM_DEBUG("mtrr_del=%d\n", retcode);
498 1.1.1.1.4.2 rmind }
499 1.1.1.1.4.2 rmind break;
500 1.1.1.1.4.2 rmind case _DRM_SHM:
501 1.1.1.1.4.3 rmind if (master && (map->flags & _DRM_CONTAINS_LOCK)) {
502 1.1.1.1.4.3 rmind spin_lock(&master->lock.spinlock);
503 1.1.1.1.4.3 rmind /*
504 1.1.1.1.4.3 rmind * If we successfully removed this mapping,
505 1.1.1.1.4.3 rmind * then the mapping must have been there in the
506 1.1.1.1.4.3 rmind * first place, and we must have had a
507 1.1.1.1.4.3 rmind * heavyweight lock, so we assert here instead
508 1.1.1.1.4.3 rmind * of just checking and failing.
509 1.1.1.1.4.3 rmind *
510 1.1.1.1.4.3 rmind * XXX What about the _DRM_CONTAINS_LOCK flag?
511 1.1.1.1.4.3 rmind * Where is that supposed to be set? Is it
512 1.1.1.1.4.3 rmind * equivalent to having a master set?
513 1.1.1.1.4.3 rmind *
514 1.1.1.1.4.3 rmind * XXX There is copypasta of this in
515 1.1.1.1.4.3 rmind * drm_fops.c.
516 1.1.1.1.4.3 rmind */
517 1.1.1.1.4.3 rmind BUG_ON(master->lock.hw_lock == NULL);
518 1.1.1.1.4.2 rmind if (dev->sigdata.lock == master->lock.hw_lock)
519 1.1.1.1.4.2 rmind dev->sigdata.lock = NULL;
520 1.1.1.1.4.2 rmind master->lock.hw_lock = NULL; /* SHM removed */
521 1.1.1.1.4.2 rmind master->lock.file_priv = NULL;
522 1.1.1.1.4.3 rmind #ifdef __NetBSD__
523 1.1.1.1.4.3 rmind DRM_SPIN_WAKEUP_ALL(&master->lock.lock_queue,
524 1.1.1.1.4.3 rmind &master->lock.spinlock);
525 1.1.1.1.4.3 rmind #else
526 1.1.1.1.4.2 rmind wake_up_interruptible_all(&master->lock.lock_queue);
527 1.1.1.1.4.3 rmind #endif
528 1.1.1.1.4.3 rmind spin_unlock(&master->lock.spinlock);
529 1.1.1.1.4.2 rmind }
530 1.1.1.1.4.3 rmind vfree(map->handle);
531 1.1.1.1.4.2 rmind break;
532 1.1.1.1.4.2 rmind case _DRM_AGP:
533 1.1.1.1.4.2 rmind case _DRM_SCATTER_GATHER:
534 1.1.1.1.4.2 rmind break;
535 1.1.1.1.4.2 rmind case _DRM_CONSISTENT:
536 1.1.1.1.4.3 rmind #ifdef __NetBSD__
537 1.1.1.1.4.3 rmind drm_pci_free(dev, map->lm_data.dmah);
538 1.1.1.1.4.3 rmind #else
539 1.1.1.1.4.2 rmind dmah.vaddr = map->handle;
540 1.1.1.1.4.2 rmind dmah.busaddr = map->offset;
541 1.1.1.1.4.2 rmind dmah.size = map->size;
542 1.1.1.1.4.2 rmind __drm_pci_free(dev, &dmah);
543 1.1.1.1.4.3 rmind #endif
544 1.1.1.1.4.2 rmind break;
545 1.1.1.1.4.2 rmind case _DRM_GEM:
546 1.1.1.1.4.2 rmind DRM_ERROR("tried to rmmap GEM object\n");
547 1.1.1.1.4.2 rmind break;
548 1.1.1.1.4.2 rmind }
549 1.1.1.1.4.2 rmind kfree(map);
550 1.1.1.1.4.2 rmind
551 1.1.1.1.4.2 rmind return 0;
552 1.1.1.1.4.2 rmind }
553 1.1.1.1.4.2 rmind EXPORT_SYMBOL(drm_rmmap_locked);
554 1.1.1.1.4.2 rmind
555 1.1.1.1.4.2 rmind int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
556 1.1.1.1.4.2 rmind {
557 1.1.1.1.4.2 rmind int ret;
558 1.1.1.1.4.2 rmind
559 1.1.1.1.4.2 rmind mutex_lock(&dev->struct_mutex);
560 1.1.1.1.4.2 rmind ret = drm_rmmap_locked(dev, map);
561 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
562 1.1.1.1.4.2 rmind
563 1.1.1.1.4.2 rmind return ret;
564 1.1.1.1.4.2 rmind }
565 1.1.1.1.4.2 rmind EXPORT_SYMBOL(drm_rmmap);
566 1.1.1.1.4.2 rmind
567 1.1.1.1.4.2 rmind /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
568 1.1.1.1.4.2 rmind * the last close of the device, and this is necessary for cleanup when things
569 1.1.1.1.4.2 rmind * exit uncleanly. Therefore, having userland manually remove mappings seems
570 1.1.1.1.4.2 rmind * like a pointless exercise since they're going away anyway.
571 1.1.1.1.4.2 rmind *
572 1.1.1.1.4.2 rmind * One use case might be after addmap is allowed for normal users for SHM and
573 1.1.1.1.4.2 rmind * gets used by drivers that the server doesn't need to care about. This seems
574 1.1.1.1.4.2 rmind * unlikely.
575 1.1.1.1.4.2 rmind *
576 1.1.1.1.4.2 rmind * \param inode device inode.
577 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
578 1.1.1.1.4.2 rmind * \param cmd command.
579 1.1.1.1.4.2 rmind * \param arg pointer to a struct drm_map structure.
580 1.1.1.1.4.2 rmind * \return zero on success or a negative value on error.
581 1.1.1.1.4.2 rmind */
582 1.1.1.1.4.2 rmind int drm_rmmap_ioctl(struct drm_device *dev, void *data,
583 1.1.1.1.4.2 rmind struct drm_file *file_priv)
584 1.1.1.1.4.2 rmind {
585 1.1.1.1.4.2 rmind struct drm_map *request = data;
586 1.1.1.1.4.2 rmind struct drm_local_map *map = NULL;
587 1.1.1.1.4.2 rmind struct drm_map_list *r_list;
588 1.1.1.1.4.2 rmind int ret;
589 1.1.1.1.4.2 rmind
590 1.1.1.1.4.2 rmind mutex_lock(&dev->struct_mutex);
591 1.1.1.1.4.2 rmind list_for_each_entry(r_list, &dev->maplist, head) {
592 1.1.1.1.4.2 rmind if (r_list->map &&
593 1.1.1.1.4.2 rmind r_list->user_token == (unsigned long)request->handle &&
594 1.1.1.1.4.2 rmind r_list->map->flags & _DRM_REMOVABLE) {
595 1.1.1.1.4.2 rmind map = r_list->map;
596 1.1.1.1.4.2 rmind break;
597 1.1.1.1.4.2 rmind }
598 1.1.1.1.4.2 rmind }
599 1.1.1.1.4.2 rmind
600 1.1.1.1.4.2 rmind /* List has wrapped around to the head pointer, or its empty we didn't
601 1.1.1.1.4.2 rmind * find anything.
602 1.1.1.1.4.2 rmind */
603 1.1.1.1.4.2 rmind if (list_empty(&dev->maplist) || !map) {
604 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
605 1.1.1.1.4.2 rmind return -EINVAL;
606 1.1.1.1.4.2 rmind }
607 1.1.1.1.4.2 rmind
608 1.1.1.1.4.2 rmind /* Register and framebuffer maps are permanent */
609 1.1.1.1.4.2 rmind if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
610 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
611 1.1.1.1.4.2 rmind return 0;
612 1.1.1.1.4.2 rmind }
613 1.1.1.1.4.2 rmind
614 1.1.1.1.4.2 rmind ret = drm_rmmap_locked(dev, map);
615 1.1.1.1.4.2 rmind
616 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
617 1.1.1.1.4.2 rmind
618 1.1.1.1.4.2 rmind return ret;
619 1.1.1.1.4.2 rmind }
620 1.1.1.1.4.2 rmind
621 1.1.1.1.4.2 rmind /**
622 1.1.1.1.4.2 rmind * Cleanup after an error on one of the addbufs() functions.
623 1.1.1.1.4.2 rmind *
624 1.1.1.1.4.2 rmind * \param dev DRM device.
625 1.1.1.1.4.2 rmind * \param entry buffer entry where the error occurred.
626 1.1.1.1.4.2 rmind *
627 1.1.1.1.4.2 rmind * Frees any pages and buffers associated with the given entry.
628 1.1.1.1.4.2 rmind */
629 1.1.1.1.4.2 rmind static void drm_cleanup_buf_error(struct drm_device * dev,
630 1.1.1.1.4.2 rmind struct drm_buf_entry * entry)
631 1.1.1.1.4.2 rmind {
632 1.1.1.1.4.2 rmind int i;
633 1.1.1.1.4.2 rmind
634 1.1.1.1.4.2 rmind if (entry->seg_count) {
635 1.1.1.1.4.2 rmind for (i = 0; i < entry->seg_count; i++) {
636 1.1.1.1.4.2 rmind if (entry->seglist[i]) {
637 1.1.1.1.4.2 rmind drm_pci_free(dev, entry->seglist[i]);
638 1.1.1.1.4.2 rmind }
639 1.1.1.1.4.2 rmind }
640 1.1.1.1.4.2 rmind kfree(entry->seglist);
641 1.1.1.1.4.2 rmind
642 1.1.1.1.4.2 rmind entry->seg_count = 0;
643 1.1.1.1.4.2 rmind }
644 1.1.1.1.4.2 rmind
645 1.1.1.1.4.2 rmind if (entry->buf_count) {
646 1.1.1.1.4.2 rmind for (i = 0; i < entry->buf_count; i++) {
647 1.1.1.1.4.2 rmind kfree(entry->buflist[i].dev_private);
648 1.1.1.1.4.2 rmind }
649 1.1.1.1.4.2 rmind kfree(entry->buflist);
650 1.1.1.1.4.2 rmind
651 1.1.1.1.4.2 rmind entry->buf_count = 0;
652 1.1.1.1.4.2 rmind }
653 1.1.1.1.4.2 rmind }
654 1.1.1.1.4.2 rmind
655 1.1.1.1.4.2 rmind #if __OS_HAS_AGP
656 1.1.1.1.4.2 rmind /**
657 1.1.1.1.4.2 rmind * Add AGP buffers for DMA transfers.
658 1.1.1.1.4.2 rmind *
659 1.1.1.1.4.2 rmind * \param dev struct drm_device to which the buffers are to be added.
660 1.1.1.1.4.2 rmind * \param request pointer to a struct drm_buf_desc describing the request.
661 1.1.1.1.4.2 rmind * \return zero on success or a negative number on failure.
662 1.1.1.1.4.2 rmind *
663 1.1.1.1.4.2 rmind * After some sanity checks creates a drm_buf structure for each buffer and
664 1.1.1.1.4.2 rmind * reallocates the buffer list of the same size order to accommodate the new
665 1.1.1.1.4.2 rmind * buffers.
666 1.1.1.1.4.2 rmind */
667 1.1.1.1.4.2 rmind int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
668 1.1.1.1.4.2 rmind {
669 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
670 1.1.1.1.4.2 rmind struct drm_buf_entry *entry;
671 1.1.1.1.4.2 rmind struct drm_agp_mem *agp_entry;
672 1.1.1.1.4.2 rmind struct drm_buf *buf;
673 1.1.1.1.4.2 rmind unsigned long offset;
674 1.1.1.1.4.2 rmind unsigned long agp_offset;
675 1.1.1.1.4.2 rmind int count;
676 1.1.1.1.4.2 rmind int order;
677 1.1.1.1.4.2 rmind int size;
678 1.1.1.1.4.2 rmind int alignment;
679 1.1.1.1.4.2 rmind int page_order;
680 1.1.1.1.4.2 rmind int total;
681 1.1.1.1.4.2 rmind int byte_count;
682 1.1.1.1.4.2 rmind int i, valid;
683 1.1.1.1.4.2 rmind struct drm_buf **temp_buflist;
684 1.1.1.1.4.2 rmind
685 1.1.1.1.4.2 rmind if (!dma)
686 1.1.1.1.4.2 rmind return -EINVAL;
687 1.1.1.1.4.2 rmind
688 1.1.1.1.4.2 rmind count = request->count;
689 1.1.1.1.4.2 rmind order = drm_order(request->size);
690 1.1.1.1.4.2 rmind size = 1 << order;
691 1.1.1.1.4.2 rmind
692 1.1.1.1.4.2 rmind alignment = (request->flags & _DRM_PAGE_ALIGN)
693 1.1.1.1.4.2 rmind ? PAGE_ALIGN(size) : size;
694 1.1.1.1.4.2 rmind page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
695 1.1.1.1.4.2 rmind total = PAGE_SIZE << page_order;
696 1.1.1.1.4.2 rmind
697 1.1.1.1.4.2 rmind byte_count = 0;
698 1.1.1.1.4.2 rmind agp_offset = dev->agp->base + request->agp_start;
699 1.1.1.1.4.2 rmind
700 1.1.1.1.4.2 rmind DRM_DEBUG("count: %d\n", count);
701 1.1.1.1.4.2 rmind DRM_DEBUG("order: %d\n", order);
702 1.1.1.1.4.2 rmind DRM_DEBUG("size: %d\n", size);
703 1.1.1.1.4.2 rmind DRM_DEBUG("agp_offset: %lx\n", agp_offset);
704 1.1.1.1.4.2 rmind DRM_DEBUG("alignment: %d\n", alignment);
705 1.1.1.1.4.2 rmind DRM_DEBUG("page_order: %d\n", page_order);
706 1.1.1.1.4.2 rmind DRM_DEBUG("total: %d\n", total);
707 1.1.1.1.4.2 rmind
708 1.1.1.1.4.2 rmind if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
709 1.1.1.1.4.2 rmind return -EINVAL;
710 1.1.1.1.4.2 rmind
711 1.1.1.1.4.2 rmind /* Make sure buffers are located in AGP memory that we own */
712 1.1.1.1.4.2 rmind valid = 0;
713 1.1.1.1.4.2 rmind list_for_each_entry(agp_entry, &dev->agp->memory, head) {
714 1.1.1.1.4.2 rmind if ((agp_offset >= agp_entry->bound) &&
715 1.1.1.1.4.2 rmind (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
716 1.1.1.1.4.2 rmind valid = 1;
717 1.1.1.1.4.2 rmind break;
718 1.1.1.1.4.2 rmind }
719 1.1.1.1.4.2 rmind }
720 1.1.1.1.4.2 rmind if (!list_empty(&dev->agp->memory) && !valid) {
721 1.1.1.1.4.2 rmind DRM_DEBUG("zone invalid\n");
722 1.1.1.1.4.2 rmind return -EINVAL;
723 1.1.1.1.4.2 rmind }
724 1.1.1.1.4.2 rmind spin_lock(&dev->count_lock);
725 1.1.1.1.4.2 rmind if (dev->buf_use) {
726 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
727 1.1.1.1.4.2 rmind return -EBUSY;
728 1.1.1.1.4.2 rmind }
729 1.1.1.1.4.2 rmind atomic_inc(&dev->buf_alloc);
730 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
731 1.1.1.1.4.2 rmind
732 1.1.1.1.4.2 rmind mutex_lock(&dev->struct_mutex);
733 1.1.1.1.4.2 rmind entry = &dma->bufs[order];
734 1.1.1.1.4.2 rmind if (entry->buf_count) {
735 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
736 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
737 1.1.1.1.4.2 rmind return -ENOMEM; /* May only call once for each order */
738 1.1.1.1.4.2 rmind }
739 1.1.1.1.4.2 rmind
740 1.1.1.1.4.2 rmind if (count < 0 || count > 4096) {
741 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
742 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
743 1.1.1.1.4.2 rmind return -EINVAL;
744 1.1.1.1.4.2 rmind }
745 1.1.1.1.4.2 rmind
746 1.1.1.1.4.2 rmind entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
747 1.1.1.1.4.2 rmind if (!entry->buflist) {
748 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
749 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
750 1.1.1.1.4.2 rmind return -ENOMEM;
751 1.1.1.1.4.2 rmind }
752 1.1.1.1.4.2 rmind
753 1.1.1.1.4.2 rmind entry->buf_size = size;
754 1.1.1.1.4.2 rmind entry->page_order = page_order;
755 1.1.1.1.4.2 rmind
756 1.1.1.1.4.2 rmind offset = 0;
757 1.1.1.1.4.2 rmind
758 1.1.1.1.4.2 rmind while (entry->buf_count < count) {
759 1.1.1.1.4.2 rmind buf = &entry->buflist[entry->buf_count];
760 1.1.1.1.4.2 rmind buf->idx = dma->buf_count + entry->buf_count;
761 1.1.1.1.4.2 rmind buf->total = alignment;
762 1.1.1.1.4.2 rmind buf->order = order;
763 1.1.1.1.4.2 rmind buf->used = 0;
764 1.1.1.1.4.2 rmind
765 1.1.1.1.4.2 rmind buf->offset = (dma->byte_count + offset);
766 1.1.1.1.4.2 rmind buf->bus_address = agp_offset + offset;
767 1.1.1.1.4.2 rmind buf->address = (void *)(agp_offset + offset);
768 1.1.1.1.4.2 rmind buf->next = NULL;
769 1.1.1.1.4.2 rmind buf->waiting = 0;
770 1.1.1.1.4.2 rmind buf->pending = 0;
771 1.1.1.1.4.2 rmind buf->file_priv = NULL;
772 1.1.1.1.4.2 rmind
773 1.1.1.1.4.2 rmind buf->dev_priv_size = dev->driver->dev_priv_size;
774 1.1.1.1.4.2 rmind buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
775 1.1.1.1.4.2 rmind if (!buf->dev_private) {
776 1.1.1.1.4.2 rmind /* Set count correctly so we free the proper amount. */
777 1.1.1.1.4.2 rmind entry->buf_count = count;
778 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
779 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
780 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
781 1.1.1.1.4.2 rmind return -ENOMEM;
782 1.1.1.1.4.2 rmind }
783 1.1.1.1.4.2 rmind
784 1.1.1.1.4.2 rmind DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
785 1.1.1.1.4.2 rmind
786 1.1.1.1.4.2 rmind offset += alignment;
787 1.1.1.1.4.2 rmind entry->buf_count++;
788 1.1.1.1.4.2 rmind byte_count += PAGE_SIZE << page_order;
789 1.1.1.1.4.2 rmind }
790 1.1.1.1.4.2 rmind
791 1.1.1.1.4.2 rmind DRM_DEBUG("byte_count: %d\n", byte_count);
792 1.1.1.1.4.2 rmind
793 1.1.1.1.4.2 rmind temp_buflist = krealloc(dma->buflist,
794 1.1.1.1.4.2 rmind (dma->buf_count + entry->buf_count) *
795 1.1.1.1.4.2 rmind sizeof(*dma->buflist), GFP_KERNEL);
796 1.1.1.1.4.2 rmind if (!temp_buflist) {
797 1.1.1.1.4.2 rmind /* Free the entry because it isn't valid */
798 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
799 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
800 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
801 1.1.1.1.4.2 rmind return -ENOMEM;
802 1.1.1.1.4.2 rmind }
803 1.1.1.1.4.2 rmind dma->buflist = temp_buflist;
804 1.1.1.1.4.2 rmind
805 1.1.1.1.4.2 rmind for (i = 0; i < entry->buf_count; i++) {
806 1.1.1.1.4.2 rmind dma->buflist[i + dma->buf_count] = &entry->buflist[i];
807 1.1.1.1.4.2 rmind }
808 1.1.1.1.4.2 rmind
809 1.1.1.1.4.2 rmind dma->buf_count += entry->buf_count;
810 1.1.1.1.4.2 rmind dma->seg_count += entry->seg_count;
811 1.1.1.1.4.2 rmind dma->page_count += byte_count >> PAGE_SHIFT;
812 1.1.1.1.4.2 rmind dma->byte_count += byte_count;
813 1.1.1.1.4.2 rmind
814 1.1.1.1.4.2 rmind DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
815 1.1.1.1.4.2 rmind DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
816 1.1.1.1.4.2 rmind
817 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
818 1.1.1.1.4.2 rmind
819 1.1.1.1.4.2 rmind request->count = entry->buf_count;
820 1.1.1.1.4.2 rmind request->size = size;
821 1.1.1.1.4.2 rmind
822 1.1.1.1.4.2 rmind dma->flags = _DRM_DMA_USE_AGP;
823 1.1.1.1.4.2 rmind
824 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
825 1.1.1.1.4.2 rmind return 0;
826 1.1.1.1.4.2 rmind }
827 1.1.1.1.4.2 rmind EXPORT_SYMBOL(drm_addbufs_agp);
828 1.1.1.1.4.2 rmind #endif /* __OS_HAS_AGP */
829 1.1.1.1.4.2 rmind
830 1.1.1.1.4.2 rmind int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
831 1.1.1.1.4.2 rmind {
832 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
833 1.1.1.1.4.2 rmind int count;
834 1.1.1.1.4.2 rmind int order;
835 1.1.1.1.4.2 rmind int size;
836 1.1.1.1.4.2 rmind int total;
837 1.1.1.1.4.2 rmind int page_order;
838 1.1.1.1.4.2 rmind struct drm_buf_entry *entry;
839 1.1.1.1.4.2 rmind drm_dma_handle_t *dmah;
840 1.1.1.1.4.2 rmind struct drm_buf *buf;
841 1.1.1.1.4.2 rmind int alignment;
842 1.1.1.1.4.2 rmind unsigned long offset;
843 1.1.1.1.4.2 rmind int i;
844 1.1.1.1.4.2 rmind int byte_count;
845 1.1.1.1.4.2 rmind int page_count;
846 1.1.1.1.4.2 rmind unsigned long *temp_pagelist;
847 1.1.1.1.4.2 rmind struct drm_buf **temp_buflist;
848 1.1.1.1.4.2 rmind
849 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
850 1.1.1.1.4.2 rmind return -EINVAL;
851 1.1.1.1.4.2 rmind
852 1.1.1.1.4.2 rmind if (!dma)
853 1.1.1.1.4.2 rmind return -EINVAL;
854 1.1.1.1.4.2 rmind
855 1.1.1.1.4.3 rmind #ifdef __NetBSD__
856 1.1.1.1.4.3 rmind if (!DRM_SUSER())
857 1.1.1.1.4.3 rmind return -EACCES; /* XXX */
858 1.1.1.1.4.3 rmind #else
859 1.1.1.1.4.2 rmind if (!capable(CAP_SYS_ADMIN))
860 1.1.1.1.4.2 rmind return -EPERM;
861 1.1.1.1.4.3 rmind #endif
862 1.1.1.1.4.2 rmind
863 1.1.1.1.4.2 rmind count = request->count;
864 1.1.1.1.4.2 rmind order = drm_order(request->size);
865 1.1.1.1.4.2 rmind size = 1 << order;
866 1.1.1.1.4.2 rmind
867 1.1.1.1.4.2 rmind DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
868 1.1.1.1.4.2 rmind request->count, request->size, size, order);
869 1.1.1.1.4.2 rmind
870 1.1.1.1.4.2 rmind if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
871 1.1.1.1.4.2 rmind return -EINVAL;
872 1.1.1.1.4.2 rmind
873 1.1.1.1.4.2 rmind alignment = (request->flags & _DRM_PAGE_ALIGN)
874 1.1.1.1.4.2 rmind ? PAGE_ALIGN(size) : size;
875 1.1.1.1.4.2 rmind page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
876 1.1.1.1.4.2 rmind total = PAGE_SIZE << page_order;
877 1.1.1.1.4.2 rmind
878 1.1.1.1.4.2 rmind spin_lock(&dev->count_lock);
879 1.1.1.1.4.2 rmind if (dev->buf_use) {
880 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
881 1.1.1.1.4.2 rmind return -EBUSY;
882 1.1.1.1.4.2 rmind }
883 1.1.1.1.4.2 rmind atomic_inc(&dev->buf_alloc);
884 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
885 1.1.1.1.4.2 rmind
886 1.1.1.1.4.2 rmind mutex_lock(&dev->struct_mutex);
887 1.1.1.1.4.2 rmind entry = &dma->bufs[order];
888 1.1.1.1.4.2 rmind if (entry->buf_count) {
889 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
890 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
891 1.1.1.1.4.2 rmind return -ENOMEM; /* May only call once for each order */
892 1.1.1.1.4.2 rmind }
893 1.1.1.1.4.2 rmind
894 1.1.1.1.4.2 rmind if (count < 0 || count > 4096) {
895 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
896 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
897 1.1.1.1.4.2 rmind return -EINVAL;
898 1.1.1.1.4.2 rmind }
899 1.1.1.1.4.2 rmind
900 1.1.1.1.4.2 rmind entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
901 1.1.1.1.4.2 rmind if (!entry->buflist) {
902 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
903 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
904 1.1.1.1.4.2 rmind return -ENOMEM;
905 1.1.1.1.4.2 rmind }
906 1.1.1.1.4.2 rmind
907 1.1.1.1.4.2 rmind entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
908 1.1.1.1.4.2 rmind if (!entry->seglist) {
909 1.1.1.1.4.2 rmind kfree(entry->buflist);
910 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
911 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
912 1.1.1.1.4.2 rmind return -ENOMEM;
913 1.1.1.1.4.2 rmind }
914 1.1.1.1.4.2 rmind
915 1.1.1.1.4.2 rmind /* Keep the original pagelist until we know all the allocations
916 1.1.1.1.4.2 rmind * have succeeded
917 1.1.1.1.4.2 rmind */
918 1.1.1.1.4.2 rmind temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
919 1.1.1.1.4.2 rmind sizeof(*dma->pagelist), GFP_KERNEL);
920 1.1.1.1.4.2 rmind if (!temp_pagelist) {
921 1.1.1.1.4.2 rmind kfree(entry->buflist);
922 1.1.1.1.4.2 rmind kfree(entry->seglist);
923 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
924 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
925 1.1.1.1.4.2 rmind return -ENOMEM;
926 1.1.1.1.4.2 rmind }
927 1.1.1.1.4.2 rmind memcpy(temp_pagelist,
928 1.1.1.1.4.2 rmind dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
929 1.1.1.1.4.2 rmind DRM_DEBUG("pagelist: %d entries\n",
930 1.1.1.1.4.2 rmind dma->page_count + (count << page_order));
931 1.1.1.1.4.2 rmind
932 1.1.1.1.4.2 rmind entry->buf_size = size;
933 1.1.1.1.4.2 rmind entry->page_order = page_order;
934 1.1.1.1.4.2 rmind byte_count = 0;
935 1.1.1.1.4.2 rmind page_count = 0;
936 1.1.1.1.4.2 rmind
937 1.1.1.1.4.2 rmind while (entry->buf_count < count) {
938 1.1.1.1.4.2 rmind
939 1.1.1.1.4.2 rmind dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
940 1.1.1.1.4.2 rmind
941 1.1.1.1.4.2 rmind if (!dmah) {
942 1.1.1.1.4.2 rmind /* Set count correctly so we free the proper amount. */
943 1.1.1.1.4.2 rmind entry->buf_count = count;
944 1.1.1.1.4.2 rmind entry->seg_count = count;
945 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
946 1.1.1.1.4.2 rmind kfree(temp_pagelist);
947 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
948 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
949 1.1.1.1.4.2 rmind return -ENOMEM;
950 1.1.1.1.4.2 rmind }
951 1.1.1.1.4.2 rmind entry->seglist[entry->seg_count++] = dmah;
952 1.1.1.1.4.2 rmind for (i = 0; i < (1 << page_order); i++) {
953 1.1.1.1.4.2 rmind DRM_DEBUG("page %d @ 0x%08lx\n",
954 1.1.1.1.4.2 rmind dma->page_count + page_count,
955 1.1.1.1.4.2 rmind (unsigned long)dmah->vaddr + PAGE_SIZE * i);
956 1.1.1.1.4.2 rmind temp_pagelist[dma->page_count + page_count++]
957 1.1.1.1.4.2 rmind = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
958 1.1.1.1.4.2 rmind }
959 1.1.1.1.4.2 rmind for (offset = 0;
960 1.1.1.1.4.2 rmind offset + size <= total && entry->buf_count < count;
961 1.1.1.1.4.2 rmind offset += alignment, ++entry->buf_count) {
962 1.1.1.1.4.2 rmind buf = &entry->buflist[entry->buf_count];
963 1.1.1.1.4.2 rmind buf->idx = dma->buf_count + entry->buf_count;
964 1.1.1.1.4.2 rmind buf->total = alignment;
965 1.1.1.1.4.2 rmind buf->order = order;
966 1.1.1.1.4.2 rmind buf->used = 0;
967 1.1.1.1.4.2 rmind buf->offset = (dma->byte_count + byte_count + offset);
968 1.1.1.1.4.3 rmind buf->address = (void *)((char *)dmah->vaddr + offset);
969 1.1.1.1.4.2 rmind buf->bus_address = dmah->busaddr + offset;
970 1.1.1.1.4.2 rmind buf->next = NULL;
971 1.1.1.1.4.2 rmind buf->waiting = 0;
972 1.1.1.1.4.2 rmind buf->pending = 0;
973 1.1.1.1.4.2 rmind buf->file_priv = NULL;
974 1.1.1.1.4.2 rmind
975 1.1.1.1.4.2 rmind buf->dev_priv_size = dev->driver->dev_priv_size;
976 1.1.1.1.4.2 rmind buf->dev_private = kzalloc(buf->dev_priv_size,
977 1.1.1.1.4.2 rmind GFP_KERNEL);
978 1.1.1.1.4.2 rmind if (!buf->dev_private) {
979 1.1.1.1.4.2 rmind /* Set count correctly so we free the proper amount. */
980 1.1.1.1.4.2 rmind entry->buf_count = count;
981 1.1.1.1.4.2 rmind entry->seg_count = count;
982 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
983 1.1.1.1.4.2 rmind kfree(temp_pagelist);
984 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
985 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
986 1.1.1.1.4.2 rmind return -ENOMEM;
987 1.1.1.1.4.2 rmind }
988 1.1.1.1.4.2 rmind
989 1.1.1.1.4.2 rmind DRM_DEBUG("buffer %d @ %p\n",
990 1.1.1.1.4.2 rmind entry->buf_count, buf->address);
991 1.1.1.1.4.2 rmind }
992 1.1.1.1.4.2 rmind byte_count += PAGE_SIZE << page_order;
993 1.1.1.1.4.2 rmind }
994 1.1.1.1.4.2 rmind
995 1.1.1.1.4.2 rmind temp_buflist = krealloc(dma->buflist,
996 1.1.1.1.4.2 rmind (dma->buf_count + entry->buf_count) *
997 1.1.1.1.4.2 rmind sizeof(*dma->buflist), GFP_KERNEL);
998 1.1.1.1.4.2 rmind if (!temp_buflist) {
999 1.1.1.1.4.2 rmind /* Free the entry because it isn't valid */
1000 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
1001 1.1.1.1.4.2 rmind kfree(temp_pagelist);
1002 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1003 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1004 1.1.1.1.4.2 rmind return -ENOMEM;
1005 1.1.1.1.4.2 rmind }
1006 1.1.1.1.4.2 rmind dma->buflist = temp_buflist;
1007 1.1.1.1.4.2 rmind
1008 1.1.1.1.4.2 rmind for (i = 0; i < entry->buf_count; i++) {
1009 1.1.1.1.4.2 rmind dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1010 1.1.1.1.4.2 rmind }
1011 1.1.1.1.4.2 rmind
1012 1.1.1.1.4.2 rmind /* No allocations failed, so now we can replace the original pagelist
1013 1.1.1.1.4.2 rmind * with the new one.
1014 1.1.1.1.4.2 rmind */
1015 1.1.1.1.4.2 rmind if (dma->page_count) {
1016 1.1.1.1.4.2 rmind kfree(dma->pagelist);
1017 1.1.1.1.4.2 rmind }
1018 1.1.1.1.4.2 rmind dma->pagelist = temp_pagelist;
1019 1.1.1.1.4.2 rmind
1020 1.1.1.1.4.2 rmind dma->buf_count += entry->buf_count;
1021 1.1.1.1.4.2 rmind dma->seg_count += entry->seg_count;
1022 1.1.1.1.4.2 rmind dma->page_count += entry->seg_count << page_order;
1023 1.1.1.1.4.2 rmind dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1024 1.1.1.1.4.2 rmind
1025 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1026 1.1.1.1.4.2 rmind
1027 1.1.1.1.4.2 rmind request->count = entry->buf_count;
1028 1.1.1.1.4.2 rmind request->size = size;
1029 1.1.1.1.4.2 rmind
1030 1.1.1.1.4.2 rmind if (request->flags & _DRM_PCI_BUFFER_RO)
1031 1.1.1.1.4.2 rmind dma->flags = _DRM_DMA_USE_PCI_RO;
1032 1.1.1.1.4.2 rmind
1033 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1034 1.1.1.1.4.2 rmind return 0;
1035 1.1.1.1.4.2 rmind
1036 1.1.1.1.4.2 rmind }
1037 1.1.1.1.4.2 rmind EXPORT_SYMBOL(drm_addbufs_pci);
1038 1.1.1.1.4.2 rmind
1039 1.1.1.1.4.2 rmind static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
1040 1.1.1.1.4.2 rmind {
1041 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
1042 1.1.1.1.4.2 rmind struct drm_buf_entry *entry;
1043 1.1.1.1.4.2 rmind struct drm_buf *buf;
1044 1.1.1.1.4.2 rmind unsigned long offset;
1045 1.1.1.1.4.2 rmind unsigned long agp_offset;
1046 1.1.1.1.4.2 rmind int count;
1047 1.1.1.1.4.2 rmind int order;
1048 1.1.1.1.4.2 rmind int size;
1049 1.1.1.1.4.2 rmind int alignment;
1050 1.1.1.1.4.2 rmind int page_order;
1051 1.1.1.1.4.2 rmind int total;
1052 1.1.1.1.4.2 rmind int byte_count;
1053 1.1.1.1.4.2 rmind int i;
1054 1.1.1.1.4.2 rmind struct drm_buf **temp_buflist;
1055 1.1.1.1.4.2 rmind
1056 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_SG))
1057 1.1.1.1.4.2 rmind return -EINVAL;
1058 1.1.1.1.4.2 rmind
1059 1.1.1.1.4.2 rmind if (!dma)
1060 1.1.1.1.4.2 rmind return -EINVAL;
1061 1.1.1.1.4.2 rmind
1062 1.1.1.1.4.3 rmind #ifdef __NetBSD__
1063 1.1.1.1.4.3 rmind if (!DRM_SUSER())
1064 1.1.1.1.4.3 rmind return -EACCES; /* XXX */
1065 1.1.1.1.4.3 rmind #else
1066 1.1.1.1.4.2 rmind if (!capable(CAP_SYS_ADMIN))
1067 1.1.1.1.4.2 rmind return -EPERM;
1068 1.1.1.1.4.3 rmind #endif
1069 1.1.1.1.4.2 rmind
1070 1.1.1.1.4.2 rmind count = request->count;
1071 1.1.1.1.4.2 rmind order = drm_order(request->size);
1072 1.1.1.1.4.2 rmind size = 1 << order;
1073 1.1.1.1.4.2 rmind
1074 1.1.1.1.4.2 rmind alignment = (request->flags & _DRM_PAGE_ALIGN)
1075 1.1.1.1.4.2 rmind ? PAGE_ALIGN(size) : size;
1076 1.1.1.1.4.2 rmind page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1077 1.1.1.1.4.2 rmind total = PAGE_SIZE << page_order;
1078 1.1.1.1.4.2 rmind
1079 1.1.1.1.4.2 rmind byte_count = 0;
1080 1.1.1.1.4.2 rmind agp_offset = request->agp_start;
1081 1.1.1.1.4.2 rmind
1082 1.1.1.1.4.2 rmind DRM_DEBUG("count: %d\n", count);
1083 1.1.1.1.4.2 rmind DRM_DEBUG("order: %d\n", order);
1084 1.1.1.1.4.2 rmind DRM_DEBUG("size: %d\n", size);
1085 1.1.1.1.4.2 rmind DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1086 1.1.1.1.4.2 rmind DRM_DEBUG("alignment: %d\n", alignment);
1087 1.1.1.1.4.2 rmind DRM_DEBUG("page_order: %d\n", page_order);
1088 1.1.1.1.4.2 rmind DRM_DEBUG("total: %d\n", total);
1089 1.1.1.1.4.2 rmind
1090 1.1.1.1.4.2 rmind if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1091 1.1.1.1.4.2 rmind return -EINVAL;
1092 1.1.1.1.4.2 rmind
1093 1.1.1.1.4.2 rmind spin_lock(&dev->count_lock);
1094 1.1.1.1.4.2 rmind if (dev->buf_use) {
1095 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1096 1.1.1.1.4.2 rmind return -EBUSY;
1097 1.1.1.1.4.2 rmind }
1098 1.1.1.1.4.2 rmind atomic_inc(&dev->buf_alloc);
1099 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1100 1.1.1.1.4.2 rmind
1101 1.1.1.1.4.2 rmind mutex_lock(&dev->struct_mutex);
1102 1.1.1.1.4.2 rmind entry = &dma->bufs[order];
1103 1.1.1.1.4.2 rmind if (entry->buf_count) {
1104 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1105 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1106 1.1.1.1.4.2 rmind return -ENOMEM; /* May only call once for each order */
1107 1.1.1.1.4.2 rmind }
1108 1.1.1.1.4.2 rmind
1109 1.1.1.1.4.2 rmind if (count < 0 || count > 4096) {
1110 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1111 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1112 1.1.1.1.4.2 rmind return -EINVAL;
1113 1.1.1.1.4.2 rmind }
1114 1.1.1.1.4.2 rmind
1115 1.1.1.1.4.2 rmind entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1116 1.1.1.1.4.2 rmind GFP_KERNEL);
1117 1.1.1.1.4.2 rmind if (!entry->buflist) {
1118 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1119 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1120 1.1.1.1.4.2 rmind return -ENOMEM;
1121 1.1.1.1.4.2 rmind }
1122 1.1.1.1.4.2 rmind
1123 1.1.1.1.4.2 rmind entry->buf_size = size;
1124 1.1.1.1.4.2 rmind entry->page_order = page_order;
1125 1.1.1.1.4.2 rmind
1126 1.1.1.1.4.2 rmind offset = 0;
1127 1.1.1.1.4.2 rmind
1128 1.1.1.1.4.2 rmind while (entry->buf_count < count) {
1129 1.1.1.1.4.2 rmind buf = &entry->buflist[entry->buf_count];
1130 1.1.1.1.4.2 rmind buf->idx = dma->buf_count + entry->buf_count;
1131 1.1.1.1.4.2 rmind buf->total = alignment;
1132 1.1.1.1.4.2 rmind buf->order = order;
1133 1.1.1.1.4.2 rmind buf->used = 0;
1134 1.1.1.1.4.2 rmind
1135 1.1.1.1.4.2 rmind buf->offset = (dma->byte_count + offset);
1136 1.1.1.1.4.2 rmind buf->bus_address = agp_offset + offset;
1137 1.1.1.1.4.2 rmind buf->address = (void *)(agp_offset + offset
1138 1.1.1.1.4.2 rmind + (unsigned long)dev->sg->virtual);
1139 1.1.1.1.4.2 rmind buf->next = NULL;
1140 1.1.1.1.4.2 rmind buf->waiting = 0;
1141 1.1.1.1.4.2 rmind buf->pending = 0;
1142 1.1.1.1.4.2 rmind buf->file_priv = NULL;
1143 1.1.1.1.4.2 rmind
1144 1.1.1.1.4.2 rmind buf->dev_priv_size = dev->driver->dev_priv_size;
1145 1.1.1.1.4.2 rmind buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1146 1.1.1.1.4.2 rmind if (!buf->dev_private) {
1147 1.1.1.1.4.2 rmind /* Set count correctly so we free the proper amount. */
1148 1.1.1.1.4.2 rmind entry->buf_count = count;
1149 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
1150 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1151 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1152 1.1.1.1.4.2 rmind return -ENOMEM;
1153 1.1.1.1.4.2 rmind }
1154 1.1.1.1.4.2 rmind
1155 1.1.1.1.4.2 rmind DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1156 1.1.1.1.4.2 rmind
1157 1.1.1.1.4.2 rmind offset += alignment;
1158 1.1.1.1.4.2 rmind entry->buf_count++;
1159 1.1.1.1.4.2 rmind byte_count += PAGE_SIZE << page_order;
1160 1.1.1.1.4.2 rmind }
1161 1.1.1.1.4.2 rmind
1162 1.1.1.1.4.2 rmind DRM_DEBUG("byte_count: %d\n", byte_count);
1163 1.1.1.1.4.2 rmind
1164 1.1.1.1.4.2 rmind temp_buflist = krealloc(dma->buflist,
1165 1.1.1.1.4.2 rmind (dma->buf_count + entry->buf_count) *
1166 1.1.1.1.4.2 rmind sizeof(*dma->buflist), GFP_KERNEL);
1167 1.1.1.1.4.2 rmind if (!temp_buflist) {
1168 1.1.1.1.4.2 rmind /* Free the entry because it isn't valid */
1169 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
1170 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1171 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1172 1.1.1.1.4.2 rmind return -ENOMEM;
1173 1.1.1.1.4.2 rmind }
1174 1.1.1.1.4.2 rmind dma->buflist = temp_buflist;
1175 1.1.1.1.4.2 rmind
1176 1.1.1.1.4.2 rmind for (i = 0; i < entry->buf_count; i++) {
1177 1.1.1.1.4.2 rmind dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1178 1.1.1.1.4.2 rmind }
1179 1.1.1.1.4.2 rmind
1180 1.1.1.1.4.2 rmind dma->buf_count += entry->buf_count;
1181 1.1.1.1.4.2 rmind dma->seg_count += entry->seg_count;
1182 1.1.1.1.4.2 rmind dma->page_count += byte_count >> PAGE_SHIFT;
1183 1.1.1.1.4.2 rmind dma->byte_count += byte_count;
1184 1.1.1.1.4.2 rmind
1185 1.1.1.1.4.2 rmind DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1186 1.1.1.1.4.2 rmind DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1187 1.1.1.1.4.2 rmind
1188 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1189 1.1.1.1.4.2 rmind
1190 1.1.1.1.4.2 rmind request->count = entry->buf_count;
1191 1.1.1.1.4.2 rmind request->size = size;
1192 1.1.1.1.4.2 rmind
1193 1.1.1.1.4.2 rmind dma->flags = _DRM_DMA_USE_SG;
1194 1.1.1.1.4.2 rmind
1195 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1196 1.1.1.1.4.2 rmind return 0;
1197 1.1.1.1.4.2 rmind }
1198 1.1.1.1.4.2 rmind
1199 1.1.1.1.4.2 rmind static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
1200 1.1.1.1.4.2 rmind {
1201 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
1202 1.1.1.1.4.2 rmind struct drm_buf_entry *entry;
1203 1.1.1.1.4.2 rmind struct drm_buf *buf;
1204 1.1.1.1.4.2 rmind unsigned long offset;
1205 1.1.1.1.4.2 rmind unsigned long agp_offset;
1206 1.1.1.1.4.2 rmind int count;
1207 1.1.1.1.4.2 rmind int order;
1208 1.1.1.1.4.2 rmind int size;
1209 1.1.1.1.4.2 rmind int alignment;
1210 1.1.1.1.4.2 rmind int page_order;
1211 1.1.1.1.4.2 rmind int total;
1212 1.1.1.1.4.2 rmind int byte_count;
1213 1.1.1.1.4.2 rmind int i;
1214 1.1.1.1.4.2 rmind struct drm_buf **temp_buflist;
1215 1.1.1.1.4.2 rmind
1216 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1217 1.1.1.1.4.2 rmind return -EINVAL;
1218 1.1.1.1.4.2 rmind
1219 1.1.1.1.4.2 rmind if (!dma)
1220 1.1.1.1.4.2 rmind return -EINVAL;
1221 1.1.1.1.4.2 rmind
1222 1.1.1.1.4.3 rmind #ifdef __NetBSD__
1223 1.1.1.1.4.3 rmind if (!DRM_SUSER())
1224 1.1.1.1.4.3 rmind return -EACCES; /* XXX */
1225 1.1.1.1.4.3 rmind #else
1226 1.1.1.1.4.2 rmind if (!capable(CAP_SYS_ADMIN))
1227 1.1.1.1.4.2 rmind return -EPERM;
1228 1.1.1.1.4.3 rmind #endif
1229 1.1.1.1.4.2 rmind
1230 1.1.1.1.4.2 rmind count = request->count;
1231 1.1.1.1.4.2 rmind order = drm_order(request->size);
1232 1.1.1.1.4.2 rmind size = 1 << order;
1233 1.1.1.1.4.2 rmind
1234 1.1.1.1.4.2 rmind alignment = (request->flags & _DRM_PAGE_ALIGN)
1235 1.1.1.1.4.2 rmind ? PAGE_ALIGN(size) : size;
1236 1.1.1.1.4.2 rmind page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1237 1.1.1.1.4.2 rmind total = PAGE_SIZE << page_order;
1238 1.1.1.1.4.2 rmind
1239 1.1.1.1.4.2 rmind byte_count = 0;
1240 1.1.1.1.4.2 rmind agp_offset = request->agp_start;
1241 1.1.1.1.4.2 rmind
1242 1.1.1.1.4.2 rmind DRM_DEBUG("count: %d\n", count);
1243 1.1.1.1.4.2 rmind DRM_DEBUG("order: %d\n", order);
1244 1.1.1.1.4.2 rmind DRM_DEBUG("size: %d\n", size);
1245 1.1.1.1.4.2 rmind DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1246 1.1.1.1.4.2 rmind DRM_DEBUG("alignment: %d\n", alignment);
1247 1.1.1.1.4.2 rmind DRM_DEBUG("page_order: %d\n", page_order);
1248 1.1.1.1.4.2 rmind DRM_DEBUG("total: %d\n", total);
1249 1.1.1.1.4.2 rmind
1250 1.1.1.1.4.2 rmind if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1251 1.1.1.1.4.2 rmind return -EINVAL;
1252 1.1.1.1.4.2 rmind
1253 1.1.1.1.4.2 rmind spin_lock(&dev->count_lock);
1254 1.1.1.1.4.2 rmind if (dev->buf_use) {
1255 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1256 1.1.1.1.4.2 rmind return -EBUSY;
1257 1.1.1.1.4.2 rmind }
1258 1.1.1.1.4.2 rmind atomic_inc(&dev->buf_alloc);
1259 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1260 1.1.1.1.4.2 rmind
1261 1.1.1.1.4.2 rmind mutex_lock(&dev->struct_mutex);
1262 1.1.1.1.4.2 rmind entry = &dma->bufs[order];
1263 1.1.1.1.4.2 rmind if (entry->buf_count) {
1264 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1265 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1266 1.1.1.1.4.2 rmind return -ENOMEM; /* May only call once for each order */
1267 1.1.1.1.4.2 rmind }
1268 1.1.1.1.4.2 rmind
1269 1.1.1.1.4.2 rmind if (count < 0 || count > 4096) {
1270 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1271 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1272 1.1.1.1.4.2 rmind return -EINVAL;
1273 1.1.1.1.4.2 rmind }
1274 1.1.1.1.4.2 rmind
1275 1.1.1.1.4.2 rmind entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1276 1.1.1.1.4.2 rmind GFP_KERNEL);
1277 1.1.1.1.4.2 rmind if (!entry->buflist) {
1278 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1279 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1280 1.1.1.1.4.2 rmind return -ENOMEM;
1281 1.1.1.1.4.2 rmind }
1282 1.1.1.1.4.2 rmind
1283 1.1.1.1.4.2 rmind entry->buf_size = size;
1284 1.1.1.1.4.2 rmind entry->page_order = page_order;
1285 1.1.1.1.4.2 rmind
1286 1.1.1.1.4.2 rmind offset = 0;
1287 1.1.1.1.4.2 rmind
1288 1.1.1.1.4.2 rmind while (entry->buf_count < count) {
1289 1.1.1.1.4.2 rmind buf = &entry->buflist[entry->buf_count];
1290 1.1.1.1.4.2 rmind buf->idx = dma->buf_count + entry->buf_count;
1291 1.1.1.1.4.2 rmind buf->total = alignment;
1292 1.1.1.1.4.2 rmind buf->order = order;
1293 1.1.1.1.4.2 rmind buf->used = 0;
1294 1.1.1.1.4.2 rmind
1295 1.1.1.1.4.2 rmind buf->offset = (dma->byte_count + offset);
1296 1.1.1.1.4.2 rmind buf->bus_address = agp_offset + offset;
1297 1.1.1.1.4.2 rmind buf->address = (void *)(agp_offset + offset);
1298 1.1.1.1.4.2 rmind buf->next = NULL;
1299 1.1.1.1.4.2 rmind buf->waiting = 0;
1300 1.1.1.1.4.2 rmind buf->pending = 0;
1301 1.1.1.1.4.2 rmind buf->file_priv = NULL;
1302 1.1.1.1.4.2 rmind
1303 1.1.1.1.4.2 rmind buf->dev_priv_size = dev->driver->dev_priv_size;
1304 1.1.1.1.4.2 rmind buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1305 1.1.1.1.4.2 rmind if (!buf->dev_private) {
1306 1.1.1.1.4.2 rmind /* Set count correctly so we free the proper amount. */
1307 1.1.1.1.4.2 rmind entry->buf_count = count;
1308 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
1309 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1310 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1311 1.1.1.1.4.2 rmind return -ENOMEM;
1312 1.1.1.1.4.2 rmind }
1313 1.1.1.1.4.2 rmind
1314 1.1.1.1.4.2 rmind DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1315 1.1.1.1.4.2 rmind
1316 1.1.1.1.4.2 rmind offset += alignment;
1317 1.1.1.1.4.2 rmind entry->buf_count++;
1318 1.1.1.1.4.2 rmind byte_count += PAGE_SIZE << page_order;
1319 1.1.1.1.4.2 rmind }
1320 1.1.1.1.4.2 rmind
1321 1.1.1.1.4.2 rmind DRM_DEBUG("byte_count: %d\n", byte_count);
1322 1.1.1.1.4.2 rmind
1323 1.1.1.1.4.2 rmind temp_buflist = krealloc(dma->buflist,
1324 1.1.1.1.4.2 rmind (dma->buf_count + entry->buf_count) *
1325 1.1.1.1.4.2 rmind sizeof(*dma->buflist), GFP_KERNEL);
1326 1.1.1.1.4.2 rmind if (!temp_buflist) {
1327 1.1.1.1.4.2 rmind /* Free the entry because it isn't valid */
1328 1.1.1.1.4.2 rmind drm_cleanup_buf_error(dev, entry);
1329 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1330 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1331 1.1.1.1.4.2 rmind return -ENOMEM;
1332 1.1.1.1.4.2 rmind }
1333 1.1.1.1.4.2 rmind dma->buflist = temp_buflist;
1334 1.1.1.1.4.2 rmind
1335 1.1.1.1.4.2 rmind for (i = 0; i < entry->buf_count; i++) {
1336 1.1.1.1.4.2 rmind dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1337 1.1.1.1.4.2 rmind }
1338 1.1.1.1.4.2 rmind
1339 1.1.1.1.4.2 rmind dma->buf_count += entry->buf_count;
1340 1.1.1.1.4.2 rmind dma->seg_count += entry->seg_count;
1341 1.1.1.1.4.2 rmind dma->page_count += byte_count >> PAGE_SHIFT;
1342 1.1.1.1.4.2 rmind dma->byte_count += byte_count;
1343 1.1.1.1.4.2 rmind
1344 1.1.1.1.4.2 rmind DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1345 1.1.1.1.4.2 rmind DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1346 1.1.1.1.4.2 rmind
1347 1.1.1.1.4.2 rmind mutex_unlock(&dev->struct_mutex);
1348 1.1.1.1.4.2 rmind
1349 1.1.1.1.4.2 rmind request->count = entry->buf_count;
1350 1.1.1.1.4.2 rmind request->size = size;
1351 1.1.1.1.4.2 rmind
1352 1.1.1.1.4.2 rmind dma->flags = _DRM_DMA_USE_FB;
1353 1.1.1.1.4.2 rmind
1354 1.1.1.1.4.2 rmind atomic_dec(&dev->buf_alloc);
1355 1.1.1.1.4.2 rmind return 0;
1356 1.1.1.1.4.2 rmind }
1357 1.1.1.1.4.2 rmind
1358 1.1.1.1.4.2 rmind
1359 1.1.1.1.4.2 rmind /**
1360 1.1.1.1.4.2 rmind * Add buffers for DMA transfers (ioctl).
1361 1.1.1.1.4.2 rmind *
1362 1.1.1.1.4.2 rmind * \param inode device inode.
1363 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
1364 1.1.1.1.4.2 rmind * \param cmd command.
1365 1.1.1.1.4.2 rmind * \param arg pointer to a struct drm_buf_desc request.
1366 1.1.1.1.4.2 rmind * \return zero on success or a negative number on failure.
1367 1.1.1.1.4.2 rmind *
1368 1.1.1.1.4.2 rmind * According with the memory type specified in drm_buf_desc::flags and the
1369 1.1.1.1.4.2 rmind * build options, it dispatches the call either to addbufs_agp(),
1370 1.1.1.1.4.2 rmind * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1371 1.1.1.1.4.2 rmind * PCI memory respectively.
1372 1.1.1.1.4.2 rmind */
1373 1.1.1.1.4.2 rmind int drm_addbufs(struct drm_device *dev, void *data,
1374 1.1.1.1.4.2 rmind struct drm_file *file_priv)
1375 1.1.1.1.4.2 rmind {
1376 1.1.1.1.4.2 rmind struct drm_buf_desc *request = data;
1377 1.1.1.1.4.2 rmind int ret;
1378 1.1.1.1.4.2 rmind
1379 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1380 1.1.1.1.4.2 rmind return -EINVAL;
1381 1.1.1.1.4.2 rmind
1382 1.1.1.1.4.2 rmind #if __OS_HAS_AGP
1383 1.1.1.1.4.2 rmind if (request->flags & _DRM_AGP_BUFFER)
1384 1.1.1.1.4.2 rmind ret = drm_addbufs_agp(dev, request);
1385 1.1.1.1.4.2 rmind else
1386 1.1.1.1.4.2 rmind #endif
1387 1.1.1.1.4.2 rmind if (request->flags & _DRM_SG_BUFFER)
1388 1.1.1.1.4.2 rmind ret = drm_addbufs_sg(dev, request);
1389 1.1.1.1.4.2 rmind else if (request->flags & _DRM_FB_BUFFER)
1390 1.1.1.1.4.2 rmind ret = drm_addbufs_fb(dev, request);
1391 1.1.1.1.4.2 rmind else
1392 1.1.1.1.4.2 rmind ret = drm_addbufs_pci(dev, request);
1393 1.1.1.1.4.2 rmind
1394 1.1.1.1.4.2 rmind return ret;
1395 1.1.1.1.4.2 rmind }
1396 1.1.1.1.4.2 rmind
1397 1.1.1.1.4.2 rmind /**
1398 1.1.1.1.4.2 rmind * Get information about the buffer mappings.
1399 1.1.1.1.4.2 rmind *
1400 1.1.1.1.4.2 rmind * This was originally mean for debugging purposes, or by a sophisticated
1401 1.1.1.1.4.2 rmind * client library to determine how best to use the available buffers (e.g.,
1402 1.1.1.1.4.2 rmind * large buffers can be used for image transfer).
1403 1.1.1.1.4.2 rmind *
1404 1.1.1.1.4.2 rmind * \param inode device inode.
1405 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
1406 1.1.1.1.4.2 rmind * \param cmd command.
1407 1.1.1.1.4.2 rmind * \param arg pointer to a drm_buf_info structure.
1408 1.1.1.1.4.2 rmind * \return zero on success or a negative number on failure.
1409 1.1.1.1.4.2 rmind *
1410 1.1.1.1.4.2 rmind * Increments drm_device::buf_use while holding the drm_device::count_lock
1411 1.1.1.1.4.2 rmind * lock, preventing of allocating more buffers after this call. Information
1412 1.1.1.1.4.2 rmind * about each requested buffer is then copied into user space.
1413 1.1.1.1.4.2 rmind */
1414 1.1.1.1.4.2 rmind int drm_infobufs(struct drm_device *dev, void *data,
1415 1.1.1.1.4.2 rmind struct drm_file *file_priv)
1416 1.1.1.1.4.2 rmind {
1417 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
1418 1.1.1.1.4.2 rmind struct drm_buf_info *request = data;
1419 1.1.1.1.4.2 rmind int i;
1420 1.1.1.1.4.2 rmind int count;
1421 1.1.1.1.4.2 rmind
1422 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1423 1.1.1.1.4.2 rmind return -EINVAL;
1424 1.1.1.1.4.2 rmind
1425 1.1.1.1.4.2 rmind if (!dma)
1426 1.1.1.1.4.2 rmind return -EINVAL;
1427 1.1.1.1.4.2 rmind
1428 1.1.1.1.4.2 rmind spin_lock(&dev->count_lock);
1429 1.1.1.1.4.2 rmind if (atomic_read(&dev->buf_alloc)) {
1430 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1431 1.1.1.1.4.2 rmind return -EBUSY;
1432 1.1.1.1.4.2 rmind }
1433 1.1.1.1.4.2 rmind ++dev->buf_use; /* Can't allocate more after this call */
1434 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1435 1.1.1.1.4.2 rmind
1436 1.1.1.1.4.2 rmind for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1437 1.1.1.1.4.2 rmind if (dma->bufs[i].buf_count)
1438 1.1.1.1.4.2 rmind ++count;
1439 1.1.1.1.4.2 rmind }
1440 1.1.1.1.4.2 rmind
1441 1.1.1.1.4.2 rmind DRM_DEBUG("count = %d\n", count);
1442 1.1.1.1.4.2 rmind
1443 1.1.1.1.4.2 rmind if (request->count >= count) {
1444 1.1.1.1.4.2 rmind for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1445 1.1.1.1.4.2 rmind if (dma->bufs[i].buf_count) {
1446 1.1.1.1.4.2 rmind struct drm_buf_desc __user *to =
1447 1.1.1.1.4.2 rmind &request->list[count];
1448 1.1.1.1.4.2 rmind struct drm_buf_entry *from = &dma->bufs[i];
1449 1.1.1.1.4.2 rmind struct drm_freelist *list = &dma->bufs[i].freelist;
1450 1.1.1.1.4.2 rmind if (copy_to_user(&to->count,
1451 1.1.1.1.4.2 rmind &from->buf_count,
1452 1.1.1.1.4.2 rmind sizeof(from->buf_count)) ||
1453 1.1.1.1.4.2 rmind copy_to_user(&to->size,
1454 1.1.1.1.4.2 rmind &from->buf_size,
1455 1.1.1.1.4.2 rmind sizeof(from->buf_size)) ||
1456 1.1.1.1.4.2 rmind copy_to_user(&to->low_mark,
1457 1.1.1.1.4.2 rmind &list->low_mark,
1458 1.1.1.1.4.2 rmind sizeof(list->low_mark)) ||
1459 1.1.1.1.4.2 rmind copy_to_user(&to->high_mark,
1460 1.1.1.1.4.2 rmind &list->high_mark,
1461 1.1.1.1.4.2 rmind sizeof(list->high_mark)))
1462 1.1.1.1.4.2 rmind return -EFAULT;
1463 1.1.1.1.4.2 rmind
1464 1.1.1.1.4.2 rmind DRM_DEBUG("%d %d %d %d %d\n",
1465 1.1.1.1.4.2 rmind i,
1466 1.1.1.1.4.2 rmind dma->bufs[i].buf_count,
1467 1.1.1.1.4.2 rmind dma->bufs[i].buf_size,
1468 1.1.1.1.4.2 rmind dma->bufs[i].freelist.low_mark,
1469 1.1.1.1.4.2 rmind dma->bufs[i].freelist.high_mark);
1470 1.1.1.1.4.2 rmind ++count;
1471 1.1.1.1.4.2 rmind }
1472 1.1.1.1.4.2 rmind }
1473 1.1.1.1.4.2 rmind }
1474 1.1.1.1.4.2 rmind request->count = count;
1475 1.1.1.1.4.2 rmind
1476 1.1.1.1.4.2 rmind return 0;
1477 1.1.1.1.4.2 rmind }
1478 1.1.1.1.4.2 rmind
1479 1.1.1.1.4.2 rmind /**
1480 1.1.1.1.4.2 rmind * Specifies a low and high water mark for buffer allocation
1481 1.1.1.1.4.2 rmind *
1482 1.1.1.1.4.2 rmind * \param inode device inode.
1483 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
1484 1.1.1.1.4.2 rmind * \param cmd command.
1485 1.1.1.1.4.2 rmind * \param arg a pointer to a drm_buf_desc structure.
1486 1.1.1.1.4.2 rmind * \return zero on success or a negative number on failure.
1487 1.1.1.1.4.2 rmind *
1488 1.1.1.1.4.2 rmind * Verifies that the size order is bounded between the admissible orders and
1489 1.1.1.1.4.2 rmind * updates the respective drm_device_dma::bufs entry low and high water mark.
1490 1.1.1.1.4.2 rmind *
1491 1.1.1.1.4.2 rmind * \note This ioctl is deprecated and mostly never used.
1492 1.1.1.1.4.2 rmind */
1493 1.1.1.1.4.2 rmind int drm_markbufs(struct drm_device *dev, void *data,
1494 1.1.1.1.4.2 rmind struct drm_file *file_priv)
1495 1.1.1.1.4.2 rmind {
1496 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
1497 1.1.1.1.4.2 rmind struct drm_buf_desc *request = data;
1498 1.1.1.1.4.2 rmind int order;
1499 1.1.1.1.4.2 rmind struct drm_buf_entry *entry;
1500 1.1.1.1.4.2 rmind
1501 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1502 1.1.1.1.4.2 rmind return -EINVAL;
1503 1.1.1.1.4.2 rmind
1504 1.1.1.1.4.2 rmind if (!dma)
1505 1.1.1.1.4.2 rmind return -EINVAL;
1506 1.1.1.1.4.2 rmind
1507 1.1.1.1.4.2 rmind DRM_DEBUG("%d, %d, %d\n",
1508 1.1.1.1.4.2 rmind request->size, request->low_mark, request->high_mark);
1509 1.1.1.1.4.2 rmind order = drm_order(request->size);
1510 1.1.1.1.4.2 rmind if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1511 1.1.1.1.4.2 rmind return -EINVAL;
1512 1.1.1.1.4.2 rmind entry = &dma->bufs[order];
1513 1.1.1.1.4.2 rmind
1514 1.1.1.1.4.2 rmind if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1515 1.1.1.1.4.2 rmind return -EINVAL;
1516 1.1.1.1.4.2 rmind if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1517 1.1.1.1.4.2 rmind return -EINVAL;
1518 1.1.1.1.4.2 rmind
1519 1.1.1.1.4.2 rmind entry->freelist.low_mark = request->low_mark;
1520 1.1.1.1.4.2 rmind entry->freelist.high_mark = request->high_mark;
1521 1.1.1.1.4.2 rmind
1522 1.1.1.1.4.2 rmind return 0;
1523 1.1.1.1.4.2 rmind }
1524 1.1.1.1.4.2 rmind
1525 1.1.1.1.4.2 rmind /**
1526 1.1.1.1.4.2 rmind * Unreserve the buffers in list, previously reserved using drmDMA.
1527 1.1.1.1.4.2 rmind *
1528 1.1.1.1.4.2 rmind * \param inode device inode.
1529 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
1530 1.1.1.1.4.2 rmind * \param cmd command.
1531 1.1.1.1.4.2 rmind * \param arg pointer to a drm_buf_free structure.
1532 1.1.1.1.4.2 rmind * \return zero on success or a negative number on failure.
1533 1.1.1.1.4.2 rmind *
1534 1.1.1.1.4.2 rmind * Calls free_buffer() for each used buffer.
1535 1.1.1.1.4.2 rmind * This function is primarily used for debugging.
1536 1.1.1.1.4.2 rmind */
1537 1.1.1.1.4.2 rmind int drm_freebufs(struct drm_device *dev, void *data,
1538 1.1.1.1.4.2 rmind struct drm_file *file_priv)
1539 1.1.1.1.4.2 rmind {
1540 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
1541 1.1.1.1.4.2 rmind struct drm_buf_free *request = data;
1542 1.1.1.1.4.2 rmind int i;
1543 1.1.1.1.4.2 rmind int idx;
1544 1.1.1.1.4.2 rmind struct drm_buf *buf;
1545 1.1.1.1.4.2 rmind
1546 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1547 1.1.1.1.4.2 rmind return -EINVAL;
1548 1.1.1.1.4.2 rmind
1549 1.1.1.1.4.2 rmind if (!dma)
1550 1.1.1.1.4.2 rmind return -EINVAL;
1551 1.1.1.1.4.2 rmind
1552 1.1.1.1.4.2 rmind DRM_DEBUG("%d\n", request->count);
1553 1.1.1.1.4.2 rmind for (i = 0; i < request->count; i++) {
1554 1.1.1.1.4.2 rmind if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1555 1.1.1.1.4.2 rmind return -EFAULT;
1556 1.1.1.1.4.2 rmind if (idx < 0 || idx >= dma->buf_count) {
1557 1.1.1.1.4.2 rmind DRM_ERROR("Index %d (of %d max)\n",
1558 1.1.1.1.4.2 rmind idx, dma->buf_count - 1);
1559 1.1.1.1.4.2 rmind return -EINVAL;
1560 1.1.1.1.4.2 rmind }
1561 1.1.1.1.4.2 rmind buf = dma->buflist[idx];
1562 1.1.1.1.4.2 rmind if (buf->file_priv != file_priv) {
1563 1.1.1.1.4.2 rmind DRM_ERROR("Process %d freeing buffer not owned\n",
1564 1.1.1.1.4.2 rmind task_pid_nr(current));
1565 1.1.1.1.4.2 rmind return -EINVAL;
1566 1.1.1.1.4.2 rmind }
1567 1.1.1.1.4.2 rmind drm_free_buffer(dev, buf);
1568 1.1.1.1.4.2 rmind }
1569 1.1.1.1.4.2 rmind
1570 1.1.1.1.4.2 rmind return 0;
1571 1.1.1.1.4.2 rmind }
1572 1.1.1.1.4.2 rmind
1573 1.1.1.1.4.2 rmind /**
1574 1.1.1.1.4.2 rmind * Maps all of the DMA buffers into client-virtual space (ioctl).
1575 1.1.1.1.4.2 rmind *
1576 1.1.1.1.4.2 rmind * \param inode device inode.
1577 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
1578 1.1.1.1.4.2 rmind * \param cmd command.
1579 1.1.1.1.4.2 rmind * \param arg pointer to a drm_buf_map structure.
1580 1.1.1.1.4.2 rmind * \return zero on success or a negative number on failure.
1581 1.1.1.1.4.2 rmind *
1582 1.1.1.1.4.2 rmind * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1583 1.1.1.1.4.2 rmind * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1584 1.1.1.1.4.2 rmind * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1585 1.1.1.1.4.2 rmind * drm_mmap_dma().
1586 1.1.1.1.4.2 rmind */
1587 1.1.1.1.4.2 rmind int drm_mapbufs(struct drm_device *dev, void *data,
1588 1.1.1.1.4.2 rmind struct drm_file *file_priv)
1589 1.1.1.1.4.2 rmind {
1590 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
1591 1.1.1.1.4.2 rmind int retcode = 0;
1592 1.1.1.1.4.2 rmind const int zero = 0;
1593 1.1.1.1.4.2 rmind unsigned long virtual;
1594 1.1.1.1.4.2 rmind unsigned long address;
1595 1.1.1.1.4.2 rmind struct drm_buf_map *request = data;
1596 1.1.1.1.4.2 rmind int i;
1597 1.1.1.1.4.2 rmind
1598 1.1.1.1.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1599 1.1.1.1.4.2 rmind return -EINVAL;
1600 1.1.1.1.4.2 rmind
1601 1.1.1.1.4.2 rmind if (!dma)
1602 1.1.1.1.4.2 rmind return -EINVAL;
1603 1.1.1.1.4.2 rmind
1604 1.1.1.1.4.2 rmind spin_lock(&dev->count_lock);
1605 1.1.1.1.4.2 rmind if (atomic_read(&dev->buf_alloc)) {
1606 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1607 1.1.1.1.4.2 rmind return -EBUSY;
1608 1.1.1.1.4.2 rmind }
1609 1.1.1.1.4.2 rmind dev->buf_use++; /* Can't allocate more after this call */
1610 1.1.1.1.4.2 rmind spin_unlock(&dev->count_lock);
1611 1.1.1.1.4.2 rmind
1612 1.1.1.1.4.2 rmind if (request->count >= dma->buf_count) {
1613 1.1.1.1.4.2 rmind if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1614 1.1.1.1.4.2 rmind || (drm_core_check_feature(dev, DRIVER_SG)
1615 1.1.1.1.4.2 rmind && (dma->flags & _DRM_DMA_USE_SG))
1616 1.1.1.1.4.2 rmind || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1617 1.1.1.1.4.2 rmind && (dma->flags & _DRM_DMA_USE_FB))) {
1618 1.1.1.1.4.2 rmind struct drm_local_map *map = dev->agp_buffer_map;
1619 1.1.1.1.4.2 rmind unsigned long token = dev->agp_buffer_token;
1620 1.1.1.1.4.2 rmind
1621 1.1.1.1.4.2 rmind if (!map) {
1622 1.1.1.1.4.2 rmind retcode = -EINVAL;
1623 1.1.1.1.4.2 rmind goto done;
1624 1.1.1.1.4.2 rmind }
1625 1.1.1.1.4.2 rmind virtual = vm_mmap(file_priv->filp, 0, map->size,
1626 1.1.1.1.4.2 rmind PROT_READ | PROT_WRITE,
1627 1.1.1.1.4.2 rmind MAP_SHARED,
1628 1.1.1.1.4.2 rmind token);
1629 1.1.1.1.4.2 rmind } else {
1630 1.1.1.1.4.2 rmind virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
1631 1.1.1.1.4.2 rmind PROT_READ | PROT_WRITE,
1632 1.1.1.1.4.2 rmind MAP_SHARED, 0);
1633 1.1.1.1.4.2 rmind }
1634 1.1.1.1.4.2 rmind if (virtual > -1024UL) {
1635 1.1.1.1.4.2 rmind /* Real error */
1636 1.1.1.1.4.2 rmind retcode = (signed long)virtual;
1637 1.1.1.1.4.2 rmind goto done;
1638 1.1.1.1.4.2 rmind }
1639 1.1.1.1.4.2 rmind request->virtual = (void __user *)virtual;
1640 1.1.1.1.4.2 rmind
1641 1.1.1.1.4.2 rmind for (i = 0; i < dma->buf_count; i++) {
1642 1.1.1.1.4.2 rmind if (copy_to_user(&request->list[i].idx,
1643 1.1.1.1.4.2 rmind &dma->buflist[i]->idx,
1644 1.1.1.1.4.2 rmind sizeof(request->list[0].idx))) {
1645 1.1.1.1.4.2 rmind retcode = -EFAULT;
1646 1.1.1.1.4.2 rmind goto done;
1647 1.1.1.1.4.2 rmind }
1648 1.1.1.1.4.2 rmind if (copy_to_user(&request->list[i].total,
1649 1.1.1.1.4.2 rmind &dma->buflist[i]->total,
1650 1.1.1.1.4.2 rmind sizeof(request->list[0].total))) {
1651 1.1.1.1.4.2 rmind retcode = -EFAULT;
1652 1.1.1.1.4.2 rmind goto done;
1653 1.1.1.1.4.2 rmind }
1654 1.1.1.1.4.2 rmind if (copy_to_user(&request->list[i].used,
1655 1.1.1.1.4.2 rmind &zero, sizeof(zero))) {
1656 1.1.1.1.4.2 rmind retcode = -EFAULT;
1657 1.1.1.1.4.2 rmind goto done;
1658 1.1.1.1.4.2 rmind }
1659 1.1.1.1.4.2 rmind address = virtual + dma->buflist[i]->offset; /* *** */
1660 1.1.1.1.4.2 rmind if (copy_to_user(&request->list[i].address,
1661 1.1.1.1.4.2 rmind &address, sizeof(address))) {
1662 1.1.1.1.4.2 rmind retcode = -EFAULT;
1663 1.1.1.1.4.2 rmind goto done;
1664 1.1.1.1.4.2 rmind }
1665 1.1.1.1.4.2 rmind }
1666 1.1.1.1.4.2 rmind }
1667 1.1.1.1.4.2 rmind done:
1668 1.1.1.1.4.2 rmind request->count = dma->buf_count;
1669 1.1.1.1.4.2 rmind DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1670 1.1.1.1.4.2 rmind
1671 1.1.1.1.4.2 rmind return retcode;
1672 1.1.1.1.4.2 rmind }
1673 1.1.1.1.4.2 rmind
1674 1.1.1.1.4.2 rmind /**
1675 1.1.1.1.4.2 rmind * Compute size order. Returns the exponent of the smaller power of two which
1676 1.1.1.1.4.2 rmind * is greater or equal to given number.
1677 1.1.1.1.4.2 rmind *
1678 1.1.1.1.4.2 rmind * \param size size.
1679 1.1.1.1.4.2 rmind * \return order.
1680 1.1.1.1.4.2 rmind *
1681 1.1.1.1.4.2 rmind * \todo Can be made faster.
1682 1.1.1.1.4.2 rmind */
1683 1.1.1.1.4.2 rmind int drm_order(unsigned long size)
1684 1.1.1.1.4.2 rmind {
1685 1.1.1.1.4.2 rmind int order;
1686 1.1.1.1.4.2 rmind unsigned long tmp;
1687 1.1.1.1.4.2 rmind
1688 1.1.1.1.4.2 rmind for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1689 1.1.1.1.4.2 rmind
1690 1.1.1.1.4.2 rmind if (size & (size - 1))
1691 1.1.1.1.4.2 rmind ++order;
1692 1.1.1.1.4.2 rmind
1693 1.1.1.1.4.2 rmind return order;
1694 1.1.1.1.4.2 rmind }
1695 1.1.1.1.4.2 rmind EXPORT_SYMBOL(drm_order);
1696