blob: 1f74fb49fa78167d5c856f927444bf685468b40d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_bufs.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Generic buffer template
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36#include <linux/vmalloc.h>
37#include "drmP.h"
38
Dave Airlied985c102006-01-02 21:32:48 +110039unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Dave Airlie836cf042005-07-10 19:27:04 +100041 return pci_resource_start(dev->pdev, resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
Dave Airlie836cf042005-07-10 19:27:04 +100043EXPORT_SYMBOL(drm_get_resource_start);
44
Dave Airlied985c102006-01-02 21:32:48 +110045unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource)
Dave Airlie836cf042005-07-10 19:27:04 +100046{
47 return pci_resource_len(dev->pdev, resource);
48}
Dave Airlieb5e89ed2005-09-25 14:28:13 +100049
Dave Airlie836cf042005-07-10 19:27:04 +100050EXPORT_SYMBOL(drm_get_resource_len);
51
Dave Airlied985c102006-01-02 21:32:48 +110052static drm_map_list_t *drm_find_matching_map(drm_device_t *dev,
53 drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +100054{
55 struct list_head *list;
56
57 list_for_each(list, &dev->maplist->head) {
58 drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);
59 if (entry->map && map->type == entry->map->type &&
60 entry->map->offset == map->offset) {
Dave Airlie89625eb2005-09-05 21:23:23 +100061 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100062 }
63 }
64
65 return NULL;
66}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Adrian Bunkfb41e542006-08-16 11:55:18 +100068static int drm_map_handle(drm_device_t *dev, drm_hash_item_t *hash,
69 unsigned long user_token, int hashed_handle)
Dave Airlied1f2b552005-08-05 22:11:22 +100070{
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100071 int use_hashed_handle;
Dave Airliec2604ce2006-08-12 16:03:26 +100072#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100073 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
74#elif (BITS_PER_LONG == 32)
75 use_hashed_handle = hashed_handle;
76#else
77#error Unsupported long size. Neither 64 nor 32 bits.
78#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100079
Thomas Hellstrome08870c2006-09-22 04:18:37 +100080 if (!use_hashed_handle) {
81 int ret;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100082 hash->key = user_token;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100083 ret = drm_ht_insert_item(&dev->map_hash, hash);
84 if (ret != -EINVAL)
85 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +100086 }
Thomas Hellstrome08870c2006-09-22 04:18:37 +100087 return drm_ht_just_insert_please(&dev->map_hash, hash,
88 user_token, 32 - PAGE_SHIFT - 3,
89 PAGE_SHIFT, DRM_MAP_HASH_OFFSET);
Dave Airlied1f2b552005-08-05 22:11:22 +100090}
Dave Airlie9a186642005-06-23 21:29:18 +100091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/**
93 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
94 *
95 * \param inode device inode.
96 * \param filp file pointer.
97 * \param cmd command.
98 * \param arg pointer to a drm_map structure.
99 * \return zero on success or a negative value on error.
100 *
101 * Adjusts the memory offset to its absolute value according to the mapping
102 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
103 * applicable and if supported by the kernel.
104 */
Dave Airlieb3a83632005-09-30 18:37:36 +1000105static int drm_addmap_core(drm_device_t * dev, unsigned int offset,
106 unsigned int size, drm_map_type_t type,
107 drm_map_flags_t flags, drm_map_list_t ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 drm_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 drm_map_list_t *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000111 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000112 unsigned long user_token;
113 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000115 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
116 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -ENOMEM;
118
Dave Airlie7ab98402005-07-10 16:56:52 +1000119 map->offset = offset;
120 map->size = size;
121 map->flags = flags;
122 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* Only allow shared memory to be removable since we only keep enough
125 * book keeping information about shared memory to allow for removal
126 * when processes fork.
127 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
129 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EINVAL;
131 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
133 map->offset, map->size, map->type);
134 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
135 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EINVAL;
137 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 map->handle = NULL;
140
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 case _DRM_REGISTERS:
143 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000144#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100145 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146 map->offset < virt_to_phys(high_memory)) {
147 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EINVAL;
149 }
150#endif
151#ifdef __alpha__
152 map->offset += dev->hose->mem_space->start;
153#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000154 /* Some drivers preinitialize some maps, without the X Server
155 * needing to be aware of it. Therefore, we just return success
156 * when the server tries to create a duplicate map.
157 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000158 list = drm_find_matching_map(dev, map);
159 if (list != NULL) {
160 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000161 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 "mismatched sizes, (%ld vs %ld)\n",
163 map->type, map->size,
164 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000165 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000166 }
167
168 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000169 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000170 return 0;
171 }
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 if (map->type == _DRM_FRAME_BUFFER ||
175 (map->flags & _DRM_WRITE_COMBINING)) {
176 map->mtrr = mtrr_add(map->offset, map->size,
177 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 }
180 if (map->type == _DRM_REGISTERS)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 map->handle = drm_ioremap(map->offset, map->size, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183
184 case _DRM_SHM:
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100185 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 DRM_DEBUG("%lu %d %p\n",
187 map->size, drm_order(map->size), map->handle);
188 if (!map->handle) {
189 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -ENOMEM;
191 }
192 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Prevent a 2nd X Server from creating a 2nd lock */
195 if (dev->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 vfree(map->handle);
197 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EBUSY;
199 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 break;
203 case _DRM_AGP:
204 if (drm_core_has_AGP(dev)) {
205#ifdef __alpha__
206 map->offset += dev->hose->mem_space->start;
207#endif
208 map->offset += dev->agp->base;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 break;
212 case _DRM_SCATTER_GATHER:
213 if (!dev->sg) {
214 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
215 return -EINVAL;
216 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000217 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000220 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000221 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000222 * casting it down to 32 bits is no problem, but we
223 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000224 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
225 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000226 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
227 return -ENOMEM;
228 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000229 map->handle = dmah->vaddr;
230 map->offset = (unsigned long)dmah->busaddr;
231 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
236 }
237
238 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700240 if (map->type == _DRM_REGISTERS)
241 drm_ioremapfree(map->handle, map->size, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
243 return -EINVAL;
244 }
245 memset(list, 0, sizeof(*list));
246 list->map = map;
247
Dave Airlie30e2fb12006-02-02 19:37:46 +1100248 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 list_add(&list->head, &dev->maplist->head);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000250
Dave Airlied1f2b552005-08-05 22:11:22 +1000251 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100252 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000253 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
254 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000255 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000256 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700257 if (map->type == _DRM_REGISTERS)
258 drm_ioremapfree(map->handle, map->size, dev);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000259 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
260 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
261 mutex_unlock(&dev->struct_mutex);
262 return ret;
263 }
264
265 list->user_token = list->hash.key;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100266 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dave Airlie89625eb2005-09-05 21:23:23 +1000268 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000269 return 0;
270}
Dave Airlie89625eb2005-09-05 21:23:23 +1000271
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272int drm_addmap(drm_device_t * dev, unsigned int offset,
Dave Airlie89625eb2005-09-05 21:23:23 +1000273 unsigned int size, drm_map_type_t type,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 drm_map_flags_t flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000275{
276 drm_map_list_t *list;
277 int rc;
278
279 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
280 if (!rc)
281 *map_ptr = list->map;
282 return rc;
283}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284
Dave Airlie7ab98402005-07-10 16:56:52 +1000285EXPORT_SYMBOL(drm_addmap);
286
287int drm_addmap_ioctl(struct inode *inode, struct file *filp,
288 unsigned int cmd, unsigned long arg)
289{
290 drm_file_t *priv = filp->private_data;
291 drm_device_t *dev = priv->head->dev;
292 drm_map_t map;
Dave Airlie89625eb2005-09-05 21:23:23 +1000293 drm_map_list_t *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000294 drm_map_t __user *argp = (void __user *)arg;
295 int err;
296
297 if (!(filp->f_mode & 3))
298 return -EACCES; /* Require read/write */
299
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -EFAULT;
Dave Airlie7ab98402005-07-10 16:56:52 +1000302 }
303
Dave Airlied985c102006-01-02 21:32:48 +1100304 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
305 return -EPERM;
306
Dave Airlie89625eb2005-09-05 21:23:23 +1000307 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
308 &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000309
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000311 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000312
Dave Airlie89625eb2005-09-05 21:23:23 +1000313 if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
Dave Airlied1f2b552005-08-05 22:11:22 +1000314 return -EFAULT;
Dave Airlie67e1a012005-10-24 18:41:39 +1000315
316 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
317 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
Dave Airlied1f2b552005-08-05 22:11:22 +1000318 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/**
323 * Remove a map private from list and deallocate resources if the mapping
324 * isn't in use.
325 *
326 * \param inode device inode.
327 * \param filp file pointer.
328 * \param cmd command.
329 * \param arg pointer to a drm_map_t structure.
330 * \return zero on success or a negative value on error.
331 *
332 * Searches the map on drm_device::maplist, removes it from the list, see if
333 * its being used, and free any associate resource (such as MTRR's) if it's not
334 * being on use.
335 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000336 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Dave Airlied985c102006-01-02 21:32:48 +1100338int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct list_head *list;
341 drm_map_list_t *r_list = NULL;
Dave Airlie836cf042005-07-10 19:27:04 +1000342 drm_dma_handle_t dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Dave Airlie836cf042005-07-10 19:27:04 +1000344 /* Find the list entry for the map and remove it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 list_for_each(list, &dev->maplist->head) {
346 r_list = list_entry(list, drm_map_list_t, head);
347
Dave Airlie836cf042005-07-10 19:27:04 +1000348 if (r_list->map == map) {
349 list_del(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000350 drm_ht_remove_key(&dev->map_hash, r_list->user_token);
Dave Airlie836cf042005-07-10 19:27:04 +1000351 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Dave Airlie836cf042005-07-10 19:27:04 +1000355
356 /* List has wrapped around to the head pointer, or it's empty and we
357 * didn't find anything.
358 */
359 if (list == (&dev->maplist->head)) {
360 return -EINVAL;
361 }
362
363 switch (map->type) {
364 case _DRM_REGISTERS:
365 drm_ioremapfree(map->handle, map->size, dev);
366 /* FALLTHROUGH */
367 case _DRM_FRAME_BUFFER:
368 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
369 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 retcode = mtrr_del(map->mtrr, map->offset, map->size);
371 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000372 }
373 break;
374 case _DRM_SHM:
375 vfree(map->handle);
376 break;
377 case _DRM_AGP:
378 case _DRM_SCATTER_GATHER:
379 break;
380 case _DRM_CONSISTENT:
381 dmah.vaddr = map->handle;
382 dmah.busaddr = map->offset;
383 dmah.size = map->size;
384 __drm_pci_free(dev, &dmah);
385 break;
386 }
387 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390}
Dave Airlie836cf042005-07-10 19:27:04 +1000391
Dave Airlied985c102006-01-02 21:32:48 +1100392int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000393{
394 int ret;
395
Dave Airlie30e2fb12006-02-02 19:37:46 +1100396 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000397 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100398 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000399
400 return ret;
401}
Dave Airlie7ab98402005-07-10 16:56:52 +1000402
Dave Airlie836cf042005-07-10 19:27:04 +1000403/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
404 * the last close of the device, and this is necessary for cleanup when things
405 * exit uncleanly. Therefore, having userland manually remove mappings seems
406 * like a pointless exercise since they're going away anyway.
407 *
408 * One use case might be after addmap is allowed for normal users for SHM and
409 * gets used by drivers that the server doesn't need to care about. This seems
410 * unlikely.
411 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000412int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
413 unsigned int cmd, unsigned long arg)
414{
415 drm_file_t *priv = filp->private_data;
416 drm_device_t *dev = priv->head->dev;
417 drm_map_t request;
Dave Airlie836cf042005-07-10 19:27:04 +1000418 drm_local_map_t *map = NULL;
419 struct list_head *list;
420 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000423 return -EFAULT;
424 }
425
Dave Airlie30e2fb12006-02-02 19:37:46 +1100426 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000427 list_for_each(list, &dev->maplist->head) {
428 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
429
430 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000432 r_list->map->flags & _DRM_REMOVABLE) {
433 map = r_list->map;
434 break;
435 }
436 }
437
438 /* List has wrapped around to the head pointer, or its empty we didn't
439 * find anything.
440 */
441 if (list == (&dev->maplist->head)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100442 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000443 return -EINVAL;
444 }
445
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000446 if (!map) {
447 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000448 return -EINVAL;
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000449 }
Dave Airlie836cf042005-07-10 19:27:04 +1000450
451 /* Register and framebuffer maps are permanent */
452 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100453 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000454 return 0;
455 }
456
457 ret = drm_rmmap_locked(dev, map);
458
Dave Airlie30e2fb12006-02-02 19:37:46 +1100459 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000460
461 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000462}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/**
465 * Cleanup after an error on one of the addbufs() functions.
466 *
Dave Airlie836cf042005-07-10 19:27:04 +1000467 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * \param entry buffer entry where the error occurred.
469 *
470 * Frees any pages and buffers associated with the given entry.
471 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 int i;
475
476 if (entry->seg_count) {
477 for (i = 0; i < entry->seg_count; i++) {
478 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100479 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 }
482 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483 entry->seg_count *
484 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 entry->seg_count = 0;
487 }
488
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 if (entry->buf_count) {
490 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (entry->buflist[i].dev_private) {
492 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 entry->buflist[i].dev_priv_size,
494 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 }
497 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000498 entry->buf_count *
499 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 entry->buf_count = 0;
502 }
503}
504
505#if __OS_HAS_AGP
506/**
Dave Airlied59431b2005-07-10 15:00:06 +1000507 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 *
Dave Airlied59431b2005-07-10 15:00:06 +1000509 * \param dev drm_device_t to which the buffers are to be added.
510 * \param request pointer to a drm_buf_desc_t describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * After some sanity checks creates a drm_buf structure for each buffer and
514 * reallocates the buffer list of the same size order to accommodate the new
515 * buffers.
516 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 drm_buf_entry_t *entry;
521 drm_buf_t *buf;
522 unsigned long offset;
523 unsigned long agp_offset;
524 int count;
525 int order;
526 int size;
527 int alignment;
528 int page_order;
529 int total;
530 int byte_count;
531 int i;
532 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 if (!dma)
535 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Dave Airlied59431b2005-07-10 15:00:06 +1000537 count = request->count;
538 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 size = 1 << order;
540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 alignment = (request->flags & _DRM_PAGE_ALIGN)
542 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
544 total = PAGE_SIZE << page_order;
545
546 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000547 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 DRM_DEBUG("count: %d\n", count);
550 DRM_DEBUG("order: %d\n", order);
551 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100552 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 DRM_DEBUG("alignment: %d\n", alignment);
554 DRM_DEBUG("page_order: %d\n", page_order);
555 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000557 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
558 return -EINVAL;
559 if (dev->queue_count)
560 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000562 spin_lock(&dev->count_lock);
563 if (dev->buf_use) {
564 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EBUSY;
566 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 atomic_inc(&dev->buf_alloc);
568 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Airlie30e2fb12006-02-02 19:37:46 +1100570 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100573 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 atomic_dec(&dev->buf_alloc);
575 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100579 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -EINVAL;
582 }
583
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
585 DRM_MEM_BUFS);
586 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100587 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return -ENOMEM;
590 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 entry->buf_size = size;
594 entry->page_order = page_order;
595
596 offset = 0;
597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 while (entry->buf_count < count) {
599 buf = &entry->buflist[entry->buf_count];
600 buf->idx = dma->buf_count + entry->buf_count;
601 buf->total = alignment;
602 buf->order = order;
603 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 buf->bus_address = agp_offset + offset;
607 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 buf->waiting = 0;
610 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 init_waitqueue_head(&buf->dma_wait);
612 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
616 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Set count correctly so we free the proper amount. */
618 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100620 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -ENOMEM;
623 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000626 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 offset += alignment;
629 entry->buf_count++;
630 byte_count += PAGE_SIZE << page_order;
631 }
632
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 temp_buflist = drm_realloc(dma->buflist,
636 dma->buf_count * sizeof(*dma->buflist),
637 (dma->buf_count + entry->buf_count)
638 * sizeof(*dma->buflist), DRM_MEM_BUFS);
639 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100642 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -ENOMEM;
645 }
646 dma->buflist = temp_buflist;
647
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
650 }
651
652 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100653 dma->seg_count += entry->seg_count;
654 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 dma->byte_count += byte_count;
656
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
658 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dave Airlie30e2fb12006-02-02 19:37:46 +1100660 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Dave Airlied59431b2005-07-10 15:00:06 +1000662 request->count = entry->buf_count;
663 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 dma->flags = _DRM_DMA_USE_AGP;
666
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670EXPORT_SYMBOL(drm_addbufs_agp);
671#endif /* __OS_HAS_AGP */
672
673int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int count;
677 int order;
678 int size;
679 int total;
680 int page_order;
681 drm_buf_entry_t *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100682 drm_dma_handle_t *dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 drm_buf_t *buf;
684 int alignment;
685 unsigned long offset;
686 int i;
687 int byte_count;
688 int page_count;
689 unsigned long *temp_pagelist;
690 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
693 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100694
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 if (!dma)
696 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Dave Airlied985c102006-01-02 21:32:48 +1100698 if (!capable(CAP_SYS_ADMIN))
699 return -EPERM;
700
Dave Airlied59431b2005-07-10 15:00:06 +1000701 count = request->count;
702 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 size = 1 << order;
704
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
706 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
709 return -EINVAL;
710 if (dev->queue_count)
711 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Dave Airlied59431b2005-07-10 15:00:06 +1000713 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
716 total = PAGE_SIZE << page_order;
717
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 spin_lock(&dev->count_lock);
719 if (dev->buf_use) {
720 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -EBUSY;
722 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 atomic_inc(&dev->buf_alloc);
724 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Airlie30e2fb12006-02-02 19:37:46 +1100726 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100729 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -ENOMEM; /* May only call once for each order */
732 }
733
734 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100735 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EINVAL;
738 }
739
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
741 DRM_MEM_BUFS);
742 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100743 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -ENOMEM;
746 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000749 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
750 DRM_MEM_SEGS);
751 if (!entry->seglist) {
752 drm_free(entry->buflist,
753 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100754 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -ENOMEM;
757 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /* Keep the original pagelist until we know all the allocations
761 * have succeeded
762 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
764 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000766 drm_free(entry->buflist,
767 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
768 drm_free(entry->seglist,
769 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100770 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -ENOMEM;
773 }
774 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
776 DRM_DEBUG("pagelist: %d entries\n",
777 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 entry->page_order = page_order;
781 byte_count = 0;
782 page_count = 0;
783
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100785
786 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
787
788 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* Set count correctly so we free the proper amount. */
790 entry->buf_count = count;
791 entry->seg_count = count;
792 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 drm_free(temp_pagelist,
794 (dma->page_count + (count << page_order))
795 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100796 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -ENOMEM;
799 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100800 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 for (i = 0; i < (1 << page_order); i++) {
802 DRM_DEBUG("page %d @ 0x%08lx\n",
803 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100804 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100806 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 for (offset = 0;
809 offset + size <= total && entry->buf_count < count;
810 offset += alignment, ++entry->buf_count) {
811 buf = &entry->buflist[entry->buf_count];
812 buf->idx = dma->buf_count + entry->buf_count;
813 buf->total = alignment;
814 buf->order = order;
815 buf->used = 0;
816 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100817 buf->address = (void *)(dmah->vaddr + offset);
818 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 buf->waiting = 0;
821 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 init_waitqueue_head(&buf->dma_wait);
823 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 buf->dev_private = drm_alloc(buf->dev_priv_size,
827 DRM_MEM_BUFS);
828 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Set count correctly so we free the proper amount. */
830 entry->buf_count = count;
831 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 drm_cleanup_buf_error(dev, entry);
833 drm_free(temp_pagelist,
834 (dma->page_count +
835 (count << page_order))
836 * sizeof(*dma->pagelist),
837 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100838 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -ENOMEM;
841 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 DRM_DEBUG("buffer %d @ %p\n",
845 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 byte_count += PAGE_SIZE << page_order;
848 }
849
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 temp_buflist = drm_realloc(dma->buflist,
851 dma->buf_count * sizeof(*dma->buflist),
852 (dma->buf_count + entry->buf_count)
853 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!temp_buflist) {
855 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 drm_cleanup_buf_error(dev, entry);
857 drm_free(temp_pagelist,
858 (dma->page_count + (count << page_order))
859 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100860 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -ENOMEM;
863 }
864 dma->buflist = temp_buflist;
865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
868 }
869
870 /* No allocations failed, so now we can replace the orginal pagelist
871 * with the new one.
872 */
873 if (dma->page_count) {
874 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 dma->page_count * sizeof(*dma->pagelist),
876 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 dma->pagelist = temp_pagelist;
879
880 dma->buf_count += entry->buf_count;
881 dma->seg_count += entry->seg_count;
882 dma->page_count += entry->seg_count << page_order;
883 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
884
Dave Airlie30e2fb12006-02-02 19:37:46 +1100885 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Dave Airlied59431b2005-07-10 15:00:06 +1000887 request->count = entry->buf_count;
888 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
George Sapountzis3417f332006-10-24 12:03:04 -0700890 if (request->flags & _DRM_PCI_BUFFER_RO)
891 dma->flags = _DRM_DMA_USE_PCI_RO;
892
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return 0;
895
896}
Dave Airlied84f76d2005-07-10 17:04:22 +1000897EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 drm_buf_entry_t *entry;
903 drm_buf_t *buf;
904 unsigned long offset;
905 unsigned long agp_offset;
906 int count;
907 int order;
908 int size;
909 int alignment;
910 int page_order;
911 int total;
912 int byte_count;
913 int i;
914 drm_buf_t **temp_buflist;
915
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 if (!drm_core_check_feature(dev, DRIVER_SG))
917 return -EINVAL;
918
919 if (!dma)
920 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Dave Airlied985c102006-01-02 21:32:48 +1100922 if (!capable(CAP_SYS_ADMIN))
923 return -EPERM;
924
Dave Airlied59431b2005-07-10 15:00:06 +1000925 count = request->count;
926 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 size = 1 << order;
928
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 alignment = (request->flags & _DRM_PAGE_ALIGN)
930 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
932 total = PAGE_SIZE << page_order;
933
934 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000935 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000937 DRM_DEBUG("count: %d\n", count);
938 DRM_DEBUG("order: %d\n", order);
939 DRM_DEBUG("size: %d\n", size);
940 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
941 DRM_DEBUG("alignment: %d\n", alignment);
942 DRM_DEBUG("page_order: %d\n", page_order);
943 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
946 return -EINVAL;
947 if (dev->queue_count)
948 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 spin_lock(&dev->count_lock);
951 if (dev->buf_use) {
952 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -EBUSY;
954 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 atomic_inc(&dev->buf_alloc);
956 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Dave Airlie30e2fb12006-02-02 19:37:46 +1100958 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100961 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000962 atomic_dec(&dev->buf_alloc);
963 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
966 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100967 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -EINVAL;
970 }
971
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
973 DRM_MEM_BUFS);
974 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100975 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ENOMEM;
978 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 entry->buf_size = size;
982 entry->page_order = page_order;
983
984 offset = 0;
985
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 while (entry->buf_count < count) {
987 buf = &entry->buflist[entry->buf_count];
988 buf->idx = dma->buf_count + entry->buf_count;
989 buf->total = alignment;
990 buf->order = order;
991 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +1000996 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 buf->waiting = 0;
999 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 init_waitqueue_head(&buf->dma_wait);
1001 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1005 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* Set count correctly so we free the proper amount. */
1007 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001009 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return -ENOMEM;
1012 }
1013
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 offset += alignment;
1019 entry->buf_count++;
1020 byte_count += PAGE_SIZE << page_order;
1021 }
1022
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 temp_buflist = drm_realloc(dma->buflist,
1026 dma->buf_count * sizeof(*dma->buflist),
1027 (dma->buf_count + entry->buf_count)
1028 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1029 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001032 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -ENOMEM;
1035 }
1036 dma->buflist = temp_buflist;
1037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1040 }
1041
1042 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001043 dma->seg_count += entry->seg_count;
1044 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 dma->byte_count += byte_count;
1046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1048 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Dave Airlie30e2fb12006-02-02 19:37:46 +11001050 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Dave Airlied59431b2005-07-10 15:00:06 +10001052 request->count = entry->buf_count;
1053 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 dma->flags = _DRM_DMA_USE_SG;
1056
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 0;
1059}
1060
Dave Airlie5d23faf2006-04-23 18:26:40 +10001061static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001062{
Dave Airlieb84397d62005-07-10 14:46:12 +10001063 drm_device_dma_t *dma = dev->dma;
Dave Airlieb84397d62005-07-10 14:46:12 +10001064 drm_buf_entry_t *entry;
1065 drm_buf_t *buf;
1066 unsigned long offset;
1067 unsigned long agp_offset;
1068 int count;
1069 int order;
1070 int size;
1071 int alignment;
1072 int page_order;
1073 int total;
1074 int byte_count;
1075 int i;
1076 drm_buf_t **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001077
1078 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1079 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080
Dave Airlieb84397d62005-07-10 14:46:12 +10001081 if (!dma)
1082 return -EINVAL;
1083
Dave Airlied985c102006-01-02 21:32:48 +11001084 if (!capable(CAP_SYS_ADMIN))
1085 return -EPERM;
1086
Dave Airlied59431b2005-07-10 15:00:06 +10001087 count = request->count;
1088 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001089 size = 1 << order;
1090
Dave Airlied59431b2005-07-10 15:00:06 +10001091 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001092 ? PAGE_ALIGN(size) : size;
1093 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1094 total = PAGE_SIZE << page_order;
1095
1096 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001097 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001098
1099 DRM_DEBUG("count: %d\n", count);
1100 DRM_DEBUG("order: %d\n", order);
1101 DRM_DEBUG("size: %d\n", size);
1102 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1103 DRM_DEBUG("alignment: %d\n", alignment);
1104 DRM_DEBUG("page_order: %d\n", page_order);
1105 DRM_DEBUG("total: %d\n", total);
1106
1107 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1108 return -EINVAL;
1109 if (dev->queue_count)
1110 return -EBUSY; /* Not while in use */
1111
1112 spin_lock(&dev->count_lock);
1113 if (dev->buf_use) {
1114 spin_unlock(&dev->count_lock);
1115 return -EBUSY;
1116 }
1117 atomic_inc(&dev->buf_alloc);
1118 spin_unlock(&dev->count_lock);
1119
Dave Airlie30e2fb12006-02-02 19:37:46 +11001120 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001121 entry = &dma->bufs[order];
1122 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001123 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001124 atomic_dec(&dev->buf_alloc);
1125 return -ENOMEM; /* May only call once for each order */
1126 }
1127
1128 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001129 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001130 atomic_dec(&dev->buf_alloc);
1131 return -EINVAL;
1132 }
1133
1134 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1135 DRM_MEM_BUFS);
1136 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001137 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001138 atomic_dec(&dev->buf_alloc);
1139 return -ENOMEM;
1140 }
1141 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1142
1143 entry->buf_size = size;
1144 entry->page_order = page_order;
1145
1146 offset = 0;
1147
1148 while (entry->buf_count < count) {
1149 buf = &entry->buflist[entry->buf_count];
1150 buf->idx = dma->buf_count + entry->buf_count;
1151 buf->total = alignment;
1152 buf->order = order;
1153 buf->used = 0;
1154
1155 buf->offset = (dma->byte_count + offset);
1156 buf->bus_address = agp_offset + offset;
1157 buf->address = (void *)(agp_offset + offset);
1158 buf->next = NULL;
1159 buf->waiting = 0;
1160 buf->pending = 0;
1161 init_waitqueue_head(&buf->dma_wait);
1162 buf->filp = NULL;
1163
1164 buf->dev_priv_size = dev->driver->dev_priv_size;
1165 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1166 if (!buf->dev_private) {
1167 /* Set count correctly so we free the proper amount. */
1168 entry->buf_count = count;
1169 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001170 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001171 atomic_dec(&dev->buf_alloc);
1172 return -ENOMEM;
1173 }
1174 memset(buf->dev_private, 0, buf->dev_priv_size);
1175
1176 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1177
1178 offset += alignment;
1179 entry->buf_count++;
1180 byte_count += PAGE_SIZE << page_order;
1181 }
1182
1183 DRM_DEBUG("byte_count: %d\n", byte_count);
1184
1185 temp_buflist = drm_realloc(dma->buflist,
1186 dma->buf_count * sizeof(*dma->buflist),
1187 (dma->buf_count + entry->buf_count)
1188 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1189 if (!temp_buflist) {
1190 /* Free the entry because it isn't valid */
1191 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001192 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001193 atomic_dec(&dev->buf_alloc);
1194 return -ENOMEM;
1195 }
1196 dma->buflist = temp_buflist;
1197
1198 for (i = 0; i < entry->buf_count; i++) {
1199 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1200 }
1201
1202 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001203 dma->seg_count += entry->seg_count;
1204 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001205 dma->byte_count += byte_count;
1206
1207 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1208 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1209
Dave Airlie30e2fb12006-02-02 19:37:46 +11001210 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001211
Dave Airlied59431b2005-07-10 15:00:06 +10001212 request->count = entry->buf_count;
1213 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001214
1215 dma->flags = _DRM_DMA_USE_FB;
1216
1217 atomic_dec(&dev->buf_alloc);
1218 return 0;
1219}
Dave Airlied985c102006-01-02 21:32:48 +11001220
Dave Airlieb84397d62005-07-10 14:46:12 +10001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/**
1223 * Add buffers for DMA transfers (ioctl).
1224 *
1225 * \param inode device inode.
1226 * \param filp file pointer.
1227 * \param cmd command.
1228 * \param arg pointer to a drm_buf_desc_t request.
1229 * \return zero on success or a negative number on failure.
1230 *
1231 * According with the memory type specified in drm_buf_desc::flags and the
1232 * build options, it dispatches the call either to addbufs_agp(),
1233 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1234 * PCI memory respectively.
1235 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001236int drm_addbufs(struct inode *inode, struct file *filp,
1237 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 drm_buf_desc_t request;
1240 drm_file_t *priv = filp->private_data;
1241 drm_device_t *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001242 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1245 return -EINVAL;
1246
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001247 if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
1248 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -EFAULT;
1250
1251#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001252 if (request.flags & _DRM_AGP_BUFFER)
1253 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 else
1255#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001256 if (request.flags & _DRM_SG_BUFFER)
1257 ret = drm_addbufs_sg(dev, &request);
1258 else if (request.flags & _DRM_FB_BUFFER)
1259 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001261 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001262
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001263 if (ret == 0) {
1264 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001265 ret = -EFAULT;
1266 }
1267 }
1268 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/**
1272 * Get information about the buffer mappings.
1273 *
1274 * This was originally mean for debugging purposes, or by a sophisticated
1275 * client library to determine how best to use the available buffers (e.g.,
1276 * large buffers can be used for image transfer).
1277 *
1278 * \param inode device inode.
1279 * \param filp file pointer.
1280 * \param cmd command.
1281 * \param arg pointer to a drm_buf_info structure.
1282 * \return zero on success or a negative number on failure.
1283 *
1284 * Increments drm_device::buf_use while holding the drm_device::count_lock
1285 * lock, preventing of allocating more buffers after this call. Information
1286 * about each requested buffer is then copied into user space.
1287 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001288int drm_infobufs(struct inode *inode, struct file *filp,
1289 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 drm_file_t *priv = filp->private_data;
1292 drm_device_t *dev = priv->head->dev;
1293 drm_device_dma_t *dma = dev->dma;
1294 drm_buf_info_t request;
1295 drm_buf_info_t __user *argp = (void __user *)arg;
1296 int i;
1297 int count;
1298
1299 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1300 return -EINVAL;
1301
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001302 if (!dma)
1303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 spin_lock(&dev->count_lock);
1306 if (atomic_read(&dev->buf_alloc)) {
1307 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EBUSY;
1309 }
1310 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001311 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001313 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EFAULT;
1315
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001316 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1317 if (dma->bufs[i].buf_count)
1318 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001323 if (request.count >= count) {
1324 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1325 if (dma->bufs[i].buf_count) {
1326 drm_buf_desc_t __user *to =
1327 &request.list[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 drm_buf_entry_t *from = &dma->bufs[i];
1329 drm_freelist_t *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330 if (copy_to_user(&to->count,
1331 &from->buf_count,
1332 sizeof(from->buf_count)) ||
1333 copy_to_user(&to->size,
1334 &from->buf_size,
1335 sizeof(from->buf_size)) ||
1336 copy_to_user(&to->low_mark,
1337 &list->low_mark,
1338 sizeof(list->low_mark)) ||
1339 copy_to_user(&to->high_mark,
1340 &list->high_mark,
1341 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -EFAULT;
1343
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001344 DRM_DEBUG("%d %d %d %d %d\n",
1345 i,
1346 dma->bufs[i].buf_count,
1347 dma->bufs[i].buf_size,
1348 dma->bufs[i].freelist.low_mark,
1349 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 ++count;
1351 }
1352 }
1353 }
1354 request.count = count;
1355
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EFAULT;
1358
1359 return 0;
1360}
1361
1362/**
1363 * Specifies a low and high water mark for buffer allocation
1364 *
1365 * \param inode device inode.
1366 * \param filp file pointer.
1367 * \param cmd command.
1368 * \param arg a pointer to a drm_buf_desc structure.
1369 * \return zero on success or a negative number on failure.
1370 *
1371 * Verifies that the size order is bounded between the admissible orders and
1372 * updates the respective drm_device_dma::bufs entry low and high water mark.
1373 *
1374 * \note This ioctl is deprecated and mostly never used.
1375 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001376int drm_markbufs(struct inode *inode, struct file *filp,
1377 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 drm_file_t *priv = filp->private_data;
1380 drm_device_t *dev = priv->head->dev;
1381 drm_device_dma_t *dma = dev->dma;
1382 drm_buf_desc_t request;
1383 int order;
1384 drm_buf_entry_t *entry;
1385
1386 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1387 return -EINVAL;
1388
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 if (!dma)
1390 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001392 if (copy_from_user(&request,
1393 (drm_buf_desc_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -EFAULT;
1395
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 DRM_DEBUG("%d, %d, %d\n",
1397 request.size, request.low_mark, request.high_mark);
1398 order = drm_order(request.size);
1399 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 entry = &dma->bufs[order];
1402
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001403 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return -EINVAL;
1407
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 entry->freelist.high_mark = request.high_mark;
1410
1411 return 0;
1412}
1413
1414/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
1417 * \param inode device inode.
1418 * \param filp file pointer.
1419 * \param cmd command.
1420 * \param arg pointer to a drm_buf_free structure.
1421 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * Calls free_buffer() for each used buffer.
1424 * This function is primarily used for debugging.
1425 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001426int drm_freebufs(struct inode *inode, struct file *filp,
1427 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 drm_file_t *priv = filp->private_data;
1430 drm_device_t *dev = priv->head->dev;
1431 drm_device_dma_t *dma = dev->dma;
1432 drm_buf_free_t request;
1433 int i;
1434 int idx;
1435 drm_buf_t *buf;
1436
1437 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1438 return -EINVAL;
1439
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001440 if (!dma)
1441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 if (copy_from_user(&request,
1444 (drm_buf_free_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EFAULT;
1446
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 DRM_DEBUG("%d\n", request.count);
1448 for (i = 0; i < request.count; i++) {
1449 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 if (idx < 0 || idx >= dma->buf_count) {
1452 DRM_ERROR("Index %d (of %d max)\n",
1453 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EINVAL;
1455 }
1456 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001457 if (buf->filp != filp) {
1458 DRM_ERROR("Process %d freeing buffer not owned\n",
1459 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -EINVAL;
1461 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001462 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
1465 return 0;
1466}
1467
1468/**
1469 * Maps all of the DMA buffers into client-virtual space (ioctl).
1470 *
1471 * \param inode device inode.
1472 * \param filp file pointer.
1473 * \param cmd command.
1474 * \param arg pointer to a drm_buf_map structure.
1475 * \return zero on success or a negative number on failure.
1476 *
George Sapountzis3417f332006-10-24 12:03:04 -07001477 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1478 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1479 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1480 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001482int drm_mapbufs(struct inode *inode, struct file *filp,
1483 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 drm_file_t *priv = filp->private_data;
1486 drm_device_t *dev = priv->head->dev;
1487 drm_device_dma_t *dma = dev->dma;
1488 drm_buf_map_t __user *argp = (void __user *)arg;
1489 int retcode = 0;
1490 const int zero = 0;
1491 unsigned long virtual;
1492 unsigned long address;
1493 drm_buf_map_t request;
1494 int i;
1495
1496 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1497 return -EINVAL;
1498
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001499 if (!dma)
1500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001502 spin_lock(&dev->count_lock);
1503 if (atomic_read(&dev->buf_alloc)) {
1504 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EBUSY;
1506 }
1507 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001510 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EFAULT;
1512
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001513 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001514 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001516 && (dma->flags & _DRM_DMA_USE_SG))
1517 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1518 && (dma->flags & _DRM_DMA_USE_FB))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 drm_map_t *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001520 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 retcode = -EINVAL;
1524 goto done;
1525 }
1526
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001527 down_write(&current->mm->mmap_sem);
1528 virtual = do_mmap(filp, 0, map->size,
1529 PROT_READ | PROT_WRITE,
1530 MAP_SHARED, token);
1531 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 down_write(&current->mm->mmap_sem);
1534 virtual = do_mmap(filp, 0, dma->byte_count,
1535 PROT_READ | PROT_WRITE,
1536 MAP_SHARED, 0);
1537 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001539 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /* Real error */
1541 retcode = (signed long)virtual;
1542 goto done;
1543 }
1544 request.virtual = (void __user *)virtual;
1545
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001546 for (i = 0; i < dma->buf_count; i++) {
1547 if (copy_to_user(&request.list[i].idx,
1548 &dma->buflist[i]->idx,
1549 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 retcode = -EFAULT;
1551 goto done;
1552 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001553 if (copy_to_user(&request.list[i].total,
1554 &dma->buflist[i]->total,
1555 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 retcode = -EFAULT;
1557 goto done;
1558 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001559 if (copy_to_user(&request.list[i].used,
1560 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 retcode = -EFAULT;
1562 goto done;
1563 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 address = virtual + dma->buflist[i]->offset; /* *** */
1565 if (copy_to_user(&request.list[i].address,
1566 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 retcode = -EFAULT;
1568 goto done;
1569 }
1570 }
1571 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001572 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001574 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001576 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return -EFAULT;
1578
1579 return retcode;
1580}
1581
Dave Airlie836cf042005-07-10 19:27:04 +10001582/**
1583 * Compute size order. Returns the exponent of the smaller power of two which
1584 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 *
Dave Airlie836cf042005-07-10 19:27:04 +10001586 * \param size size.
1587 * \return order.
1588 *
1589 * \todo Can be made faster.
1590 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001592{
1593 int order;
1594 unsigned long tmp;
1595
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001597
1598 if (size & (size - 1))
1599 ++order;
1600
1601 return order;
1602}
1603EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001604
1605