blob: c11345856ffe1dd5db26763b45c9881e8ea259b5 [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 &&
Dave Airlie54ba2f72007-02-10 12:07:47 +110060 ((entry->map->offset == map->offset) ||
61 (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) {
Dave Airlie89625eb2005-09-05 21:23:23 +100062 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100063 }
64 }
65
66 return NULL;
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Adrian Bunkfb41e542006-08-16 11:55:18 +100069static int drm_map_handle(drm_device_t *dev, drm_hash_item_t *hash,
70 unsigned long user_token, int hashed_handle)
Dave Airlied1f2b552005-08-05 22:11:22 +100071{
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100072 int use_hashed_handle;
Dave Airliec2604ce2006-08-12 16:03:26 +100073#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100074 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
75#elif (BITS_PER_LONG == 32)
76 use_hashed_handle = hashed_handle;
77#else
78#error Unsupported long size. Neither 64 nor 32 bits.
79#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100080
Thomas Hellstrome08870c2006-09-22 04:18:37 +100081 if (!use_hashed_handle) {
82 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110083 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100084 ret = drm_ht_insert_item(&dev->map_hash, hash);
85 if (ret != -EINVAL)
86 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +100087 }
Thomas Hellstrome08870c2006-09-22 04:18:37 +100088 return drm_ht_just_insert_please(&dev->map_hash, hash,
89 user_token, 32 - PAGE_SHIFT - 3,
Thomas Hellstrom15450852007-02-08 16:14:05 +110090 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
Dave Airlied1f2b552005-08-05 22:11:22 +100091}
Dave Airlie9a186642005-06-23 21:29:18 +100092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/**
94 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
95 *
96 * \param inode device inode.
97 * \param filp file pointer.
98 * \param cmd command.
99 * \param arg pointer to a drm_map structure.
100 * \return zero on success or a negative value on error.
101 *
102 * Adjusts the memory offset to its absolute value according to the mapping
103 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
104 * applicable and if supported by the kernel.
105 */
Dave Airlieb3a83632005-09-30 18:37:36 +1000106static int drm_addmap_core(drm_device_t * dev, unsigned int offset,
107 unsigned int size, drm_map_type_t type,
108 drm_map_flags_t flags, drm_map_list_t ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 drm_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 drm_map_list_t *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000112 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000113 unsigned long user_token;
114 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
117 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return -ENOMEM;
119
Dave Airlie7ab98402005-07-10 16:56:52 +1000120 map->offset = offset;
121 map->size = size;
122 map->flags = flags;
123 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /* Only allow shared memory to be removable since we only keep enough
126 * book keeping information about shared memory to allow for removal
127 * when processes fork.
128 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
130 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -EINVAL;
132 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
134 map->offset, map->size, map->type);
135 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
136 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return -EINVAL;
138 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 map->handle = NULL;
141
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 case _DRM_REGISTERS:
144 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000145#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100146 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 map->offset < virt_to_phys(high_memory)) {
148 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -EINVAL;
150 }
151#endif
152#ifdef __alpha__
153 map->offset += dev->hose->mem_space->start;
154#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000155 /* Some drivers preinitialize some maps, without the X Server
156 * needing to be aware of it. Therefore, we just return success
157 * when the server tries to create a duplicate map.
158 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000159 list = drm_find_matching_map(dev, map);
160 if (list != NULL) {
161 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000162 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 "mismatched sizes, (%ld vs %ld)\n",
164 map->type, map->size,
165 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000166 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000167 }
168
169 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000170 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000171 return 0;
172 }
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000175 if (map->type == _DRM_FRAME_BUFFER ||
176 (map->flags & _DRM_WRITE_COMBINING)) {
177 map->mtrr = mtrr_add(map->offset, map->size,
178 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 }
181 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100182 map->handle = ioremap(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100185 list = drm_find_matching_map(dev, map);
186 if (list != NULL) {
187 if(list->map->size != map->size) {
188 DRM_DEBUG("Matching maps of type %d with "
189 "mismatched sizes, (%ld vs %ld)\n",
190 map->type, map->size, list->map->size);
191 list->map->size = map->size;
192 }
193
194 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
195 *maplist = list;
196 return 0;
197 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100198 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 DRM_DEBUG("%lu %d %p\n",
200 map->size, drm_order(map->size), map->handle);
201 if (!map->handle) {
202 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -ENOMEM;
204 }
205 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Prevent a 2nd X Server from creating a 2nd lock */
208 if (dev->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 vfree(map->handle);
210 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -EBUSY;
212 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100216 case _DRM_AGP: {
217 drm_agp_mem_t *entry;
218 int valid = 0;
219
220 if (!drm_core_has_AGP(dev)) {
221 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
222 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100224#ifdef __alpha__
225 map->offset += dev->hose->mem_space->start;
226#endif
227 /* Note: dev->agp->base may actually be 0 when the DRM
228 * is not in control of AGP space. But if user space is
229 * it should already have added the AGP base itself.
230 */
231 map->offset += dev->agp->base;
232 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
233
234 /* This assumes the DRM is in total control of AGP space.
235 * It's not always the case as AGP can be in the control
236 * of user space (i.e. i810 driver). So this loop will get
237 * skipped and we double check that dev->agp->memory is
238 * actually set as well as being invalid before EPERM'ing
239 */
240 for (entry = dev->agp->memory; entry; entry = entry->next) {
241 if ((map->offset >= entry->bound) &&
242 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
243 valid = 1;
244 break;
245 }
246 }
247 if (dev->agp->memory && !valid) {
248 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
249 return -EPERM;
250 }
251 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 case _DRM_SCATTER_GATHER:
256 if (!dev->sg) {
257 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
258 return -EINVAL;
259 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000260 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000262 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000263 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000264 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000265 * casting it down to 32 bits is no problem, but we
266 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000267 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
268 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000269 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
270 return -ENOMEM;
271 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000272 map->handle = dmah->vaddr;
273 map->offset = (unsigned long)dmah->busaddr;
274 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -EINVAL;
279 }
280
281 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700283 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100284 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
286 return -EINVAL;
287 }
288 memset(list, 0, sizeof(*list));
289 list->map = map;
290
Dave Airlie30e2fb12006-02-02 19:37:46 +1100291 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 list_add(&list->head, &dev->maplist->head);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000293
Dave Airlied1f2b552005-08-05 22:11:22 +1000294 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100295 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000296 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
297 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000298 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000299 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700300 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100301 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000302 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
303 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
304 mutex_unlock(&dev->struct_mutex);
305 return ret;
306 }
307
Thomas Hellstrom15450852007-02-08 16:14:05 +1100308 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100309 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Dave Airlie89625eb2005-09-05 21:23:23 +1000311 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000312 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100313 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000314
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315int drm_addmap(drm_device_t * dev, unsigned int offset,
Dave Airlie89625eb2005-09-05 21:23:23 +1000316 unsigned int size, drm_map_type_t type,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317 drm_map_flags_t flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000318{
319 drm_map_list_t *list;
320 int rc;
321
322 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
323 if (!rc)
324 *map_ptr = list->map;
325 return rc;
326}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327
Dave Airlie7ab98402005-07-10 16:56:52 +1000328EXPORT_SYMBOL(drm_addmap);
329
330int drm_addmap_ioctl(struct inode *inode, struct file *filp,
331 unsigned int cmd, unsigned long arg)
332{
333 drm_file_t *priv = filp->private_data;
334 drm_device_t *dev = priv->head->dev;
335 drm_map_t map;
Dave Airlie89625eb2005-09-05 21:23:23 +1000336 drm_map_list_t *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000337 drm_map_t __user *argp = (void __user *)arg;
338 int err;
339
340 if (!(filp->f_mode & 3))
341 return -EACCES; /* Require read/write */
342
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EFAULT;
Dave Airlie7ab98402005-07-10 16:56:52 +1000345 }
346
Dave Airlied985c102006-01-02 21:32:48 +1100347 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
348 return -EPERM;
349
Dave Airlie89625eb2005-09-05 21:23:23 +1000350 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
351 &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000352
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000353 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000354 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000355
Dave Airlie89625eb2005-09-05 21:23:23 +1000356 if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
Dave Airlied1f2b552005-08-05 22:11:22 +1000357 return -EFAULT;
Dave Airlie67e1a012005-10-24 18:41:39 +1000358
359 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
360 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
Dave Airlied1f2b552005-08-05 22:11:22 +1000361 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/**
366 * Remove a map private from list and deallocate resources if the mapping
367 * isn't in use.
368 *
369 * \param inode device inode.
370 * \param filp file pointer.
371 * \param cmd command.
372 * \param arg pointer to a drm_map_t structure.
373 * \return zero on success or a negative value on error.
374 *
375 * Searches the map on drm_device::maplist, removes it from the list, see if
376 * its being used, and free any associate resource (such as MTRR's) if it's not
377 * being on use.
378 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000379 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Dave Airlied985c102006-01-02 21:32:48 +1100381int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct list_head *list;
384 drm_map_list_t *r_list = NULL;
Dave Airlie836cf042005-07-10 19:27:04 +1000385 drm_dma_handle_t dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dave Airlie836cf042005-07-10 19:27:04 +1000387 /* Find the list entry for the map and remove it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 list_for_each(list, &dev->maplist->head) {
389 r_list = list_entry(list, drm_map_list_t, head);
390
Dave Airlie836cf042005-07-10 19:27:04 +1000391 if (r_list->map == map) {
392 list_del(list);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100393 drm_ht_remove_key(&dev->map_hash,
394 r_list->user_token >> PAGE_SHIFT);
Dave Airlie836cf042005-07-10 19:27:04 +1000395 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Dave Airlie836cf042005-07-10 19:27:04 +1000399
400 /* List has wrapped around to the head pointer, or it's empty and we
401 * didn't find anything.
402 */
403 if (list == (&dev->maplist->head)) {
404 return -EINVAL;
405 }
406
407 switch (map->type) {
408 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100409 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000410 /* FALLTHROUGH */
411 case _DRM_FRAME_BUFFER:
412 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
413 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 retcode = mtrr_del(map->mtrr, map->offset, map->size);
415 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000416 }
417 break;
418 case _DRM_SHM:
419 vfree(map->handle);
420 break;
421 case _DRM_AGP:
422 case _DRM_SCATTER_GATHER:
423 break;
424 case _DRM_CONSISTENT:
425 dmah.vaddr = map->handle;
426 dmah.busaddr = map->offset;
427 dmah.size = map->size;
428 __drm_pci_free(dev, &dmah);
429 break;
430 }
431 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
Dave Airlie836cf042005-07-10 19:27:04 +1000435
Dave Airlied985c102006-01-02 21:32:48 +1100436int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000437{
438 int ret;
439
Dave Airlie30e2fb12006-02-02 19:37:46 +1100440 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000441 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100442 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000443
444 return ret;
445}
Dave Airlie7ab98402005-07-10 16:56:52 +1000446
Dave Airlie836cf042005-07-10 19:27:04 +1000447/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
448 * the last close of the device, and this is necessary for cleanup when things
449 * exit uncleanly. Therefore, having userland manually remove mappings seems
450 * like a pointless exercise since they're going away anyway.
451 *
452 * One use case might be after addmap is allowed for normal users for SHM and
453 * gets used by drivers that the server doesn't need to care about. This seems
454 * unlikely.
455 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000456int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
457 unsigned int cmd, unsigned long arg)
458{
459 drm_file_t *priv = filp->private_data;
460 drm_device_t *dev = priv->head->dev;
461 drm_map_t request;
Dave Airlie836cf042005-07-10 19:27:04 +1000462 drm_local_map_t *map = NULL;
463 struct list_head *list;
464 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000465
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000467 return -EFAULT;
468 }
469
Dave Airlie30e2fb12006-02-02 19:37:46 +1100470 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000471 list_for_each(list, &dev->maplist->head) {
472 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
473
474 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000476 r_list->map->flags & _DRM_REMOVABLE) {
477 map = r_list->map;
478 break;
479 }
480 }
481
482 /* List has wrapped around to the head pointer, or its empty we didn't
483 * find anything.
484 */
485 if (list == (&dev->maplist->head)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100486 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000487 return -EINVAL;
488 }
489
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000490 if (!map) {
491 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000492 return -EINVAL;
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000493 }
Dave Airlie836cf042005-07-10 19:27:04 +1000494
495 /* Register and framebuffer maps are permanent */
496 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100497 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000498 return 0;
499 }
500
501 ret = drm_rmmap_locked(dev, map);
502
Dave Airlie30e2fb12006-02-02 19:37:46 +1100503 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000504
505 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508/**
509 * Cleanup after an error on one of the addbufs() functions.
510 *
Dave Airlie836cf042005-07-10 19:27:04 +1000511 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * \param entry buffer entry where the error occurred.
513 *
514 * Frees any pages and buffers associated with the given entry.
515 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 int i;
519
520 if (entry->seg_count) {
521 for (i = 0; i < entry->seg_count; i++) {
522 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100523 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 }
526 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000527 entry->seg_count *
528 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 entry->seg_count = 0;
531 }
532
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 if (entry->buf_count) {
534 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (entry->buflist[i].dev_private) {
536 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 entry->buflist[i].dev_priv_size,
538 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 }
541 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000542 entry->buf_count *
543 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 entry->buf_count = 0;
546 }
547}
548
549#if __OS_HAS_AGP
550/**
Dave Airlied59431b2005-07-10 15:00:06 +1000551 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
Dave Airlied59431b2005-07-10 15:00:06 +1000553 * \param dev drm_device_t to which the buffers are to be added.
554 * \param request pointer to a drm_buf_desc_t describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * After some sanity checks creates a drm_buf structure for each buffer and
558 * reallocates the buffer list of the same size order to accommodate the new
559 * buffers.
560 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 drm_buf_entry_t *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100565 drm_agp_mem_t *agp_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 drm_buf_t *buf;
567 unsigned long offset;
568 unsigned long agp_offset;
569 int count;
570 int order;
571 int size;
572 int alignment;
573 int page_order;
574 int total;
575 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100576 int i, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 if (!dma)
580 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Dave Airlied59431b2005-07-10 15:00:06 +1000582 count = request->count;
583 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 size = 1 << order;
585
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 alignment = (request->flags & _DRM_PAGE_ALIGN)
587 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
589 total = PAGE_SIZE << page_order;
590
591 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000592 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 DRM_DEBUG("count: %d\n", count);
595 DRM_DEBUG("order: %d\n", order);
596 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100597 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 DRM_DEBUG("alignment: %d\n", alignment);
599 DRM_DEBUG("page_order: %d\n", page_order);
600 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
603 return -EINVAL;
604 if (dev->queue_count)
605 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dave Airlie54ba2f72007-02-10 12:07:47 +1100607 /* Make sure buffers are located in AGP memory that we own */
608 valid = 0;
609 for (agp_entry = dev->agp->memory; agp_entry; agp_entry = agp_entry->next) {
610 if ((agp_offset >= agp_entry->bound) &&
611 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
612 valid = 1;
613 break;
614 }
615 }
616 if (dev->agp->memory && !valid) {
617 DRM_DEBUG("zone invalid\n");
618 return -EINVAL;
619 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 spin_lock(&dev->count_lock);
621 if (dev->buf_use) {
622 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EBUSY;
624 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 atomic_inc(&dev->buf_alloc);
626 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dave Airlie30e2fb12006-02-02 19:37:46 +1100628 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100631 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000632 atomic_dec(&dev->buf_alloc);
633 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100637 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -EINVAL;
640 }
641
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
643 DRM_MEM_BUFS);
644 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100645 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -ENOMEM;
648 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 entry->buf_size = size;
652 entry->page_order = page_order;
653
654 offset = 0;
655
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 while (entry->buf_count < count) {
657 buf = &entry->buflist[entry->buf_count];
658 buf->idx = dma->buf_count + entry->buf_count;
659 buf->total = alignment;
660 buf->order = order;
661 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 buf->bus_address = agp_offset + offset;
665 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 buf->waiting = 0;
668 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 init_waitqueue_head(&buf->dma_wait);
670 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
674 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* Set count correctly so we free the proper amount. */
676 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100678 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return -ENOMEM;
681 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 offset += alignment;
687 entry->buf_count++;
688 byte_count += PAGE_SIZE << page_order;
689 }
690
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 temp_buflist = drm_realloc(dma->buflist,
694 dma->buf_count * sizeof(*dma->buflist),
695 (dma->buf_count + entry->buf_count)
696 * sizeof(*dma->buflist), DRM_MEM_BUFS);
697 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100700 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -ENOMEM;
703 }
704 dma->buflist = temp_buflist;
705
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
708 }
709
710 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100711 dma->seg_count += entry->seg_count;
712 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 dma->byte_count += byte_count;
714
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
716 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Dave Airlie30e2fb12006-02-02 19:37:46 +1100718 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dave Airlied59431b2005-07-10 15:00:06 +1000720 request->count = entry->buf_count;
721 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 dma->flags = _DRM_DMA_USE_AGP;
724
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728EXPORT_SYMBOL(drm_addbufs_agp);
729#endif /* __OS_HAS_AGP */
730
731int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int count;
735 int order;
736 int size;
737 int total;
738 int page_order;
739 drm_buf_entry_t *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100740 drm_dma_handle_t *dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 drm_buf_t *buf;
742 int alignment;
743 unsigned long offset;
744 int i;
745 int byte_count;
746 int page_count;
747 unsigned long *temp_pagelist;
748 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
751 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100752
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 if (!dma)
754 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Dave Airlied985c102006-01-02 21:32:48 +1100756 if (!capable(CAP_SYS_ADMIN))
757 return -EPERM;
758
Dave Airlied59431b2005-07-10 15:00:06 +1000759 count = request->count;
760 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 size = 1 << order;
762
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
764 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000766 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
767 return -EINVAL;
768 if (dev->queue_count)
769 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Dave Airlied59431b2005-07-10 15:00:06 +1000771 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
774 total = PAGE_SIZE << page_order;
775
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 spin_lock(&dev->count_lock);
777 if (dev->buf_use) {
778 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EBUSY;
780 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 atomic_inc(&dev->buf_alloc);
782 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Dave Airlie30e2fb12006-02-02 19:37:46 +1100784 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100787 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -ENOMEM; /* May only call once for each order */
790 }
791
792 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100793 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -EINVAL;
796 }
797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
799 DRM_MEM_BUFS);
800 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100801 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -ENOMEM;
804 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
808 DRM_MEM_SEGS);
809 if (!entry->seglist) {
810 drm_free(entry->buflist,
811 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100812 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -ENOMEM;
815 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* Keep the original pagelist until we know all the allocations
819 * have succeeded
820 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
822 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 drm_free(entry->buflist,
825 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
826 drm_free(entry->seglist,
827 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100828 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -ENOMEM;
831 }
832 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
834 DRM_DEBUG("pagelist: %d entries\n",
835 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 entry->page_order = page_order;
839 byte_count = 0;
840 page_count = 0;
841
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100843
844 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
845
846 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* Set count correctly so we free the proper amount. */
848 entry->buf_count = count;
849 entry->seg_count = count;
850 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 drm_free(temp_pagelist,
852 (dma->page_count + (count << page_order))
853 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100854 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return -ENOMEM;
857 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100858 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 for (i = 0; i < (1 << page_order); i++) {
860 DRM_DEBUG("page %d @ 0x%08lx\n",
861 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100862 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100864 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 for (offset = 0;
867 offset + size <= total && entry->buf_count < count;
868 offset += alignment, ++entry->buf_count) {
869 buf = &entry->buflist[entry->buf_count];
870 buf->idx = dma->buf_count + entry->buf_count;
871 buf->total = alignment;
872 buf->order = order;
873 buf->used = 0;
874 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100875 buf->address = (void *)(dmah->vaddr + offset);
876 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 buf->waiting = 0;
879 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 init_waitqueue_head(&buf->dma_wait);
881 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 buf->dev_private = drm_alloc(buf->dev_priv_size,
885 DRM_MEM_BUFS);
886 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* Set count correctly so we free the proper amount. */
888 entry->buf_count = count;
889 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 drm_cleanup_buf_error(dev, entry);
891 drm_free(temp_pagelist,
892 (dma->page_count +
893 (count << page_order))
894 * sizeof(*dma->pagelist),
895 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100896 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -ENOMEM;
899 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 DRM_DEBUG("buffer %d @ %p\n",
903 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 byte_count += PAGE_SIZE << page_order;
906 }
907
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 temp_buflist = drm_realloc(dma->buflist,
909 dma->buf_count * sizeof(*dma->buflist),
910 (dma->buf_count + entry->buf_count)
911 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (!temp_buflist) {
913 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 drm_cleanup_buf_error(dev, entry);
915 drm_free(temp_pagelist,
916 (dma->page_count + (count << page_order))
917 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100918 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -ENOMEM;
921 }
922 dma->buflist = temp_buflist;
923
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
926 }
927
928 /* No allocations failed, so now we can replace the orginal pagelist
929 * with the new one.
930 */
931 if (dma->page_count) {
932 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 dma->page_count * sizeof(*dma->pagelist),
934 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 dma->pagelist = temp_pagelist;
937
938 dma->buf_count += entry->buf_count;
939 dma->seg_count += entry->seg_count;
940 dma->page_count += entry->seg_count << page_order;
941 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
942
Dave Airlie30e2fb12006-02-02 19:37:46 +1100943 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Dave Airlied59431b2005-07-10 15:00:06 +1000945 request->count = entry->buf_count;
946 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
George Sapountzis3417f332006-10-24 12:03:04 -0700948 if (request->flags & _DRM_PCI_BUFFER_RO)
949 dma->flags = _DRM_DMA_USE_PCI_RO;
950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953
954}
Dave Airlied84f76d2005-07-10 17:04:22 +1000955EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000957static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 drm_buf_entry_t *entry;
961 drm_buf_t *buf;
962 unsigned long offset;
963 unsigned long agp_offset;
964 int count;
965 int order;
966 int size;
967 int alignment;
968 int page_order;
969 int total;
970 int byte_count;
971 int i;
972 drm_buf_t **temp_buflist;
973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 if (!drm_core_check_feature(dev, DRIVER_SG))
975 return -EINVAL;
976
977 if (!dma)
978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Dave Airlied985c102006-01-02 21:32:48 +1100980 if (!capable(CAP_SYS_ADMIN))
981 return -EPERM;
982
Dave Airlied59431b2005-07-10 15:00:06 +1000983 count = request->count;
984 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 size = 1 << order;
986
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 alignment = (request->flags & _DRM_PAGE_ALIGN)
988 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
990 total = PAGE_SIZE << page_order;
991
992 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000993 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 DRM_DEBUG("count: %d\n", count);
996 DRM_DEBUG("order: %d\n", order);
997 DRM_DEBUG("size: %d\n", size);
998 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
999 DRM_DEBUG("alignment: %d\n", alignment);
1000 DRM_DEBUG("page_order: %d\n", page_order);
1001 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1004 return -EINVAL;
1005 if (dev->queue_count)
1006 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 spin_lock(&dev->count_lock);
1009 if (dev->buf_use) {
1010 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return -EBUSY;
1012 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 atomic_inc(&dev->buf_alloc);
1014 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dave Airlie30e2fb12006-02-02 19:37:46 +11001016 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001019 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 atomic_dec(&dev->buf_alloc);
1021 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
1024 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001025 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EINVAL;
1028 }
1029
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1031 DRM_MEM_BUFS);
1032 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001033 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return -ENOMEM;
1036 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 entry->buf_size = size;
1040 entry->page_order = page_order;
1041
1042 offset = 0;
1043
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 while (entry->buf_count < count) {
1045 buf = &entry->buflist[entry->buf_count];
1046 buf->idx = dma->buf_count + entry->buf_count;
1047 buf->total = alignment;
1048 buf->order = order;
1049 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001054 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 buf->waiting = 0;
1057 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 init_waitqueue_head(&buf->dma_wait);
1059 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1063 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* Set count correctly so we free the proper amount. */
1065 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001067 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -ENOMEM;
1070 }
1071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 offset += alignment;
1077 entry->buf_count++;
1078 byte_count += PAGE_SIZE << page_order;
1079 }
1080
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 temp_buflist = drm_realloc(dma->buflist,
1084 dma->buf_count * sizeof(*dma->buflist),
1085 (dma->buf_count + entry->buf_count)
1086 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1087 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001090 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -ENOMEM;
1093 }
1094 dma->buflist = temp_buflist;
1095
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1098 }
1099
1100 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001101 dma->seg_count += entry->seg_count;
1102 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 dma->byte_count += byte_count;
1104
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1106 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Dave Airlie30e2fb12006-02-02 19:37:46 +11001108 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Dave Airlied59431b2005-07-10 15:00:06 +10001110 request->count = entry->buf_count;
1111 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 dma->flags = _DRM_DMA_USE_SG;
1114
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117}
1118
Dave Airlie5d23faf2006-04-23 18:26:40 +10001119static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001120{
Dave Airlieb84397d62005-07-10 14:46:12 +10001121 drm_device_dma_t *dma = dev->dma;
Dave Airlieb84397d62005-07-10 14:46:12 +10001122 drm_buf_entry_t *entry;
1123 drm_buf_t *buf;
1124 unsigned long offset;
1125 unsigned long agp_offset;
1126 int count;
1127 int order;
1128 int size;
1129 int alignment;
1130 int page_order;
1131 int total;
1132 int byte_count;
1133 int i;
1134 drm_buf_t **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001135
1136 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1137 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138
Dave Airlieb84397d62005-07-10 14:46:12 +10001139 if (!dma)
1140 return -EINVAL;
1141
Dave Airlied985c102006-01-02 21:32:48 +11001142 if (!capable(CAP_SYS_ADMIN))
1143 return -EPERM;
1144
Dave Airlied59431b2005-07-10 15:00:06 +10001145 count = request->count;
1146 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001147 size = 1 << order;
1148
Dave Airlied59431b2005-07-10 15:00:06 +10001149 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001150 ? PAGE_ALIGN(size) : size;
1151 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1152 total = PAGE_SIZE << page_order;
1153
1154 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001155 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001156
1157 DRM_DEBUG("count: %d\n", count);
1158 DRM_DEBUG("order: %d\n", order);
1159 DRM_DEBUG("size: %d\n", size);
1160 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1161 DRM_DEBUG("alignment: %d\n", alignment);
1162 DRM_DEBUG("page_order: %d\n", page_order);
1163 DRM_DEBUG("total: %d\n", total);
1164
1165 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1166 return -EINVAL;
1167 if (dev->queue_count)
1168 return -EBUSY; /* Not while in use */
1169
1170 spin_lock(&dev->count_lock);
1171 if (dev->buf_use) {
1172 spin_unlock(&dev->count_lock);
1173 return -EBUSY;
1174 }
1175 atomic_inc(&dev->buf_alloc);
1176 spin_unlock(&dev->count_lock);
1177
Dave Airlie30e2fb12006-02-02 19:37:46 +11001178 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001179 entry = &dma->bufs[order];
1180 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001181 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001182 atomic_dec(&dev->buf_alloc);
1183 return -ENOMEM; /* May only call once for each order */
1184 }
1185
1186 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001187 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001188 atomic_dec(&dev->buf_alloc);
1189 return -EINVAL;
1190 }
1191
1192 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1193 DRM_MEM_BUFS);
1194 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001195 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001196 atomic_dec(&dev->buf_alloc);
1197 return -ENOMEM;
1198 }
1199 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1200
1201 entry->buf_size = size;
1202 entry->page_order = page_order;
1203
1204 offset = 0;
1205
1206 while (entry->buf_count < count) {
1207 buf = &entry->buflist[entry->buf_count];
1208 buf->idx = dma->buf_count + entry->buf_count;
1209 buf->total = alignment;
1210 buf->order = order;
1211 buf->used = 0;
1212
1213 buf->offset = (dma->byte_count + offset);
1214 buf->bus_address = agp_offset + offset;
1215 buf->address = (void *)(agp_offset + offset);
1216 buf->next = NULL;
1217 buf->waiting = 0;
1218 buf->pending = 0;
1219 init_waitqueue_head(&buf->dma_wait);
1220 buf->filp = NULL;
1221
1222 buf->dev_priv_size = dev->driver->dev_priv_size;
1223 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1224 if (!buf->dev_private) {
1225 /* Set count correctly so we free the proper amount. */
1226 entry->buf_count = count;
1227 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001228 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001229 atomic_dec(&dev->buf_alloc);
1230 return -ENOMEM;
1231 }
1232 memset(buf->dev_private, 0, buf->dev_priv_size);
1233
1234 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1235
1236 offset += alignment;
1237 entry->buf_count++;
1238 byte_count += PAGE_SIZE << page_order;
1239 }
1240
1241 DRM_DEBUG("byte_count: %d\n", byte_count);
1242
1243 temp_buflist = drm_realloc(dma->buflist,
1244 dma->buf_count * sizeof(*dma->buflist),
1245 (dma->buf_count + entry->buf_count)
1246 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1247 if (!temp_buflist) {
1248 /* Free the entry because it isn't valid */
1249 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001250 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001251 atomic_dec(&dev->buf_alloc);
1252 return -ENOMEM;
1253 }
1254 dma->buflist = temp_buflist;
1255
1256 for (i = 0; i < entry->buf_count; i++) {
1257 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1258 }
1259
1260 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001261 dma->seg_count += entry->seg_count;
1262 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001263 dma->byte_count += byte_count;
1264
1265 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1266 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1267
Dave Airlie30e2fb12006-02-02 19:37:46 +11001268 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001269
Dave Airlied59431b2005-07-10 15:00:06 +10001270 request->count = entry->buf_count;
1271 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001272
1273 dma->flags = _DRM_DMA_USE_FB;
1274
1275 atomic_dec(&dev->buf_alloc);
1276 return 0;
1277}
Dave Airlied985c102006-01-02 21:32:48 +11001278
Dave Airlieb84397d62005-07-10 14:46:12 +10001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/**
1281 * Add buffers for DMA transfers (ioctl).
1282 *
1283 * \param inode device inode.
1284 * \param filp file pointer.
1285 * \param cmd command.
1286 * \param arg pointer to a drm_buf_desc_t request.
1287 * \return zero on success or a negative number on failure.
1288 *
1289 * According with the memory type specified in drm_buf_desc::flags and the
1290 * build options, it dispatches the call either to addbufs_agp(),
1291 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1292 * PCI memory respectively.
1293 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294int drm_addbufs(struct inode *inode, struct file *filp,
1295 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 drm_buf_desc_t request;
1298 drm_file_t *priv = filp->private_data;
1299 drm_device_t *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001300 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1303 return -EINVAL;
1304
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
1306 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EFAULT;
1308
1309#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001310 if (request.flags & _DRM_AGP_BUFFER)
1311 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 else
1313#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 if (request.flags & _DRM_SG_BUFFER)
1315 ret = drm_addbufs_sg(dev, &request);
1316 else if (request.flags & _DRM_FB_BUFFER)
1317 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001319 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001320
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 if (ret == 0) {
1322 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001323 ret = -EFAULT;
1324 }
1325 }
1326 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/**
1330 * Get information about the buffer mappings.
1331 *
1332 * This was originally mean for debugging purposes, or by a sophisticated
1333 * client library to determine how best to use the available buffers (e.g.,
1334 * large buffers can be used for image transfer).
1335 *
1336 * \param inode device inode.
1337 * \param filp file pointer.
1338 * \param cmd command.
1339 * \param arg pointer to a drm_buf_info structure.
1340 * \return zero on success or a negative number on failure.
1341 *
1342 * Increments drm_device::buf_use while holding the drm_device::count_lock
1343 * lock, preventing of allocating more buffers after this call. Information
1344 * about each requested buffer is then copied into user space.
1345 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001346int drm_infobufs(struct inode *inode, struct file *filp,
1347 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 drm_file_t *priv = filp->private_data;
1350 drm_device_t *dev = priv->head->dev;
1351 drm_device_dma_t *dma = dev->dma;
1352 drm_buf_info_t request;
1353 drm_buf_info_t __user *argp = (void __user *)arg;
1354 int i;
1355 int count;
1356
1357 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1358 return -EINVAL;
1359
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001360 if (!dma)
1361 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001363 spin_lock(&dev->count_lock);
1364 if (atomic_read(&dev->buf_alloc)) {
1365 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EBUSY;
1367 }
1368 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001369 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001371 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EFAULT;
1373
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001374 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1375 if (dma->bufs[i].buf_count)
1376 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001379 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 if (request.count >= count) {
1382 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1383 if (dma->bufs[i].buf_count) {
1384 drm_buf_desc_t __user *to =
1385 &request.list[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 drm_buf_entry_t *from = &dma->bufs[i];
1387 drm_freelist_t *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001388 if (copy_to_user(&to->count,
1389 &from->buf_count,
1390 sizeof(from->buf_count)) ||
1391 copy_to_user(&to->size,
1392 &from->buf_size,
1393 sizeof(from->buf_size)) ||
1394 copy_to_user(&to->low_mark,
1395 &list->low_mark,
1396 sizeof(list->low_mark)) ||
1397 copy_to_user(&to->high_mark,
1398 &list->high_mark,
1399 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return -EFAULT;
1401
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001402 DRM_DEBUG("%d %d %d %d %d\n",
1403 i,
1404 dma->bufs[i].buf_count,
1405 dma->bufs[i].buf_size,
1406 dma->bufs[i].freelist.low_mark,
1407 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 ++count;
1409 }
1410 }
1411 }
1412 request.count = count;
1413
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001414 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -EFAULT;
1416
1417 return 0;
1418}
1419
1420/**
1421 * Specifies a low and high water mark for buffer allocation
1422 *
1423 * \param inode device inode.
1424 * \param filp file pointer.
1425 * \param cmd command.
1426 * \param arg a pointer to a drm_buf_desc structure.
1427 * \return zero on success or a negative number on failure.
1428 *
1429 * Verifies that the size order is bounded between the admissible orders and
1430 * updates the respective drm_device_dma::bufs entry low and high water mark.
1431 *
1432 * \note This ioctl is deprecated and mostly never used.
1433 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434int drm_markbufs(struct inode *inode, struct file *filp,
1435 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 drm_file_t *priv = filp->private_data;
1438 drm_device_t *dev = priv->head->dev;
1439 drm_device_dma_t *dma = dev->dma;
1440 drm_buf_desc_t request;
1441 int order;
1442 drm_buf_entry_t *entry;
1443
1444 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1445 return -EINVAL;
1446
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 if (!dma)
1448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001450 if (copy_from_user(&request,
1451 (drm_buf_desc_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EFAULT;
1453
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 DRM_DEBUG("%d, %d, %d\n",
1455 request.size, request.low_mark, request.high_mark);
1456 order = drm_order(request.size);
1457 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 entry = &dma->bufs[order];
1460
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001461 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001463 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EINVAL;
1465
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 entry->freelist.high_mark = request.high_mark;
1468
1469 return 0;
1470}
1471
1472/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001473 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 *
1475 * \param inode device inode.
1476 * \param filp file pointer.
1477 * \param cmd command.
1478 * \param arg pointer to a drm_buf_free structure.
1479 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001480 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * Calls free_buffer() for each used buffer.
1482 * This function is primarily used for debugging.
1483 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484int drm_freebufs(struct inode *inode, struct file *filp,
1485 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 drm_file_t *priv = filp->private_data;
1488 drm_device_t *dev = priv->head->dev;
1489 drm_device_dma_t *dma = dev->dma;
1490 drm_buf_free_t request;
1491 int i;
1492 int idx;
1493 drm_buf_t *buf;
1494
1495 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1496 return -EINVAL;
1497
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001498 if (!dma)
1499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501 if (copy_from_user(&request,
1502 (drm_buf_free_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EFAULT;
1504
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 DRM_DEBUG("%d\n", request.count);
1506 for (i = 0; i < request.count; i++) {
1507 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001509 if (idx < 0 || idx >= dma->buf_count) {
1510 DRM_ERROR("Index %d (of %d max)\n",
1511 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EINVAL;
1513 }
1514 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 if (buf->filp != filp) {
1516 DRM_ERROR("Process %d freeing buffer not owned\n",
1517 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return -EINVAL;
1519 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
1523 return 0;
1524}
1525
1526/**
1527 * Maps all of the DMA buffers into client-virtual space (ioctl).
1528 *
1529 * \param inode device inode.
1530 * \param filp file pointer.
1531 * \param cmd command.
1532 * \param arg pointer to a drm_buf_map structure.
1533 * \return zero on success or a negative number on failure.
1534 *
George Sapountzis3417f332006-10-24 12:03:04 -07001535 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1536 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1537 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1538 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540int drm_mapbufs(struct inode *inode, struct file *filp,
1541 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 drm_file_t *priv = filp->private_data;
1544 drm_device_t *dev = priv->head->dev;
1545 drm_device_dma_t *dma = dev->dma;
1546 drm_buf_map_t __user *argp = (void __user *)arg;
1547 int retcode = 0;
1548 const int zero = 0;
1549 unsigned long virtual;
1550 unsigned long address;
1551 drm_buf_map_t request;
1552 int i;
1553
1554 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1555 return -EINVAL;
1556
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 if (!dma)
1558 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 spin_lock(&dev->count_lock);
1561 if (atomic_read(&dev->buf_alloc)) {
1562 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return -EBUSY;
1564 }
1565 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001566 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001568 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -EFAULT;
1570
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001572 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001573 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001574 && (dma->flags & _DRM_DMA_USE_SG))
1575 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1576 && (dma->flags & _DRM_DMA_USE_FB))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 drm_map_t *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001578 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001580 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 retcode = -EINVAL;
1582 goto done;
1583 }
1584
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 down_write(&current->mm->mmap_sem);
1586 virtual = do_mmap(filp, 0, map->size,
1587 PROT_READ | PROT_WRITE,
1588 MAP_SHARED, token);
1589 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591 down_write(&current->mm->mmap_sem);
1592 virtual = do_mmap(filp, 0, dma->byte_count,
1593 PROT_READ | PROT_WRITE,
1594 MAP_SHARED, 0);
1595 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001597 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /* Real error */
1599 retcode = (signed long)virtual;
1600 goto done;
1601 }
1602 request.virtual = (void __user *)virtual;
1603
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001604 for (i = 0; i < dma->buf_count; i++) {
1605 if (copy_to_user(&request.list[i].idx,
1606 &dma->buflist[i]->idx,
1607 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 retcode = -EFAULT;
1609 goto done;
1610 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 if (copy_to_user(&request.list[i].total,
1612 &dma->buflist[i]->total,
1613 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 retcode = -EFAULT;
1615 goto done;
1616 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001617 if (copy_to_user(&request.list[i].used,
1618 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 retcode = -EFAULT;
1620 goto done;
1621 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001622 address = virtual + dma->buflist[i]->offset; /* *** */
1623 if (copy_to_user(&request.list[i].address,
1624 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 retcode = -EFAULT;
1626 goto done;
1627 }
1628 }
1629 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001630 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001632 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001634 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return -EFAULT;
1636
1637 return retcode;
1638}
1639
Dave Airlie836cf042005-07-10 19:27:04 +10001640/**
1641 * Compute size order. Returns the exponent of the smaller power of two which
1642 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001643 *
Dave Airlie836cf042005-07-10 19:27:04 +10001644 * \param size size.
1645 * \return order.
1646 *
1647 * \todo Can be made faster.
1648 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001649int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001650{
1651 int order;
1652 unsigned long tmp;
1653
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001654 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001655
1656 if (size & (size - 1))
1657 ++order;
1658
1659 return order;
1660}
1661EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001662
1663