blob: bdd301519769fee93ffcc94fc11260f74cd4532a [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 Airlie84b1fd12007-07-11 15:53:27 +100039unsigned long drm_get_resource_start(struct drm_device *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 Airlie84b1fd12007-07-11 15:53:27 +100045unsigned long drm_get_resource_len(struct drm_device *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 Airlie84b1fd12007-07-11 15:53:27 +100052static drm_map_list_t *drm_find_matching_map(struct drm_device *dev,
Dave Airlied985c102006-01-02 21:32:48 +110053 drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +100054{
Dave Airliebd1b3312007-05-26 05:01:51 +100055 drm_map_list_t *entry;
56 list_for_each_entry(entry, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +100057 if (entry->map && map->type == entry->map->type &&
Dave Airlie54ba2f72007-02-10 12:07:47 +110058 ((entry->map->offset == map->offset) ||
59 (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) {
Dave Airlie89625eb2005-09-05 21:23:23 +100060 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100061 }
62 }
63
64 return NULL;
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Airlie84b1fd12007-07-11 15:53:27 +100067static int drm_map_handle(struct drm_device *dev, drm_hash_item_t *hash,
Adrian Bunkfb41e542006-08-16 11:55:18 +100068 unsigned long user_token, int hashed_handle)
Dave Airlied1f2b552005-08-05 22:11:22 +100069{
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100070 int use_hashed_handle;
Dave Airliec2604ce2006-08-12 16:03:26 +100071#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100072 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
73#elif (BITS_PER_LONG == 32)
74 use_hashed_handle = hashed_handle;
75#else
76#error Unsupported long size. Neither 64 nor 32 bits.
77#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100078
Thomas Hellstrome08870c2006-09-22 04:18:37 +100079 if (!use_hashed_handle) {
80 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110081 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100082 ret = drm_ht_insert_item(&dev->map_hash, hash);
83 if (ret != -EINVAL)
84 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +100085 }
Thomas Hellstrome08870c2006-09-22 04:18:37 +100086 return drm_ht_just_insert_please(&dev->map_hash, hash,
87 user_token, 32 - PAGE_SHIFT - 3,
Thomas Hellstrom15450852007-02-08 16:14:05 +110088 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
Dave Airlied1f2b552005-08-05 22:11:22 +100089}
Dave Airlie9a186642005-06-23 21:29:18 +100090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/**
92 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
93 *
94 * \param inode device inode.
95 * \param filp file pointer.
96 * \param cmd command.
97 * \param arg pointer to a drm_map structure.
98 * \return zero on success or a negative value on error.
99 *
100 * Adjusts the memory offset to its absolute value according to the mapping
101 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
102 * applicable and if supported by the kernel.
103 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000104static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000105 unsigned int size, enum drm_map_type type,
106 enum drm_map_flags flags, drm_map_list_t ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Dave Airliec60ce622007-07-11 15:27:12 +1000108 struct drm_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 drm_map_list_t *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000110 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000111 unsigned long user_token;
112 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
115 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return -ENOMEM;
117
Dave Airlie7ab98402005-07-10 16:56:52 +1000118 map->offset = offset;
119 map->size = size;
120 map->flags = flags;
121 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* Only allow shared memory to be removable since we only keep enough
124 * book keeping information about shared memory to allow for removal
125 * when processes fork.
126 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
128 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -EINVAL;
130 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
132 map->offset, map->size, map->type);
133 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
134 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -EINVAL;
136 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000137 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 map->handle = NULL;
139
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 case _DRM_REGISTERS:
142 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000143#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100144 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000145 map->offset < virt_to_phys(high_memory)) {
146 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -EINVAL;
148 }
149#endif
150#ifdef __alpha__
151 map->offset += dev->hose->mem_space->start;
152#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000153 /* Some drivers preinitialize some maps, without the X Server
154 * needing to be aware of it. Therefore, we just return success
155 * when the server tries to create a duplicate map.
156 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000157 list = drm_find_matching_map(dev, map);
158 if (list != NULL) {
159 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000160 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000161 "mismatched sizes, (%ld vs %ld)\n",
162 map->type, map->size,
163 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000164 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000165 }
166
167 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000168 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000169 return 0;
170 }
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 if (map->type == _DRM_FRAME_BUFFER ||
174 (map->flags & _DRM_WRITE_COMBINING)) {
175 map->mtrr = mtrr_add(map->offset, map->size,
176 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 }
179 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100180 map->handle = ioremap(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100183 list = drm_find_matching_map(dev, map);
184 if (list != NULL) {
185 if(list->map->size != map->size) {
186 DRM_DEBUG("Matching maps of type %d with "
187 "mismatched sizes, (%ld vs %ld)\n",
188 map->type, map->size, list->map->size);
189 list->map->size = map->size;
190 }
191
192 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
193 *maplist = list;
194 return 0;
195 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100196 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 DRM_DEBUG("%lu %d %p\n",
198 map->size, drm_order(map->size), map->handle);
199 if (!map->handle) {
200 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -ENOMEM;
202 }
203 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Prevent a 2nd X Server from creating a 2nd lock */
206 if (dev->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 vfree(map->handle);
208 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return -EBUSY;
210 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100214 case _DRM_AGP: {
215 drm_agp_mem_t *entry;
216 int valid = 0;
217
218 if (!drm_core_has_AGP(dev)) {
219 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
220 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100222#ifdef __alpha__
223 map->offset += dev->hose->mem_space->start;
224#endif
225 /* Note: dev->agp->base may actually be 0 when the DRM
226 * is not in control of AGP space. But if user space is
227 * it should already have added the AGP base itself.
228 */
229 map->offset += dev->agp->base;
230 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
231
232 /* This assumes the DRM is in total control of AGP space.
233 * It's not always the case as AGP can be in the control
234 * of user space (i.e. i810 driver). So this loop will get
235 * skipped and we double check that dev->agp->memory is
236 * actually set as well as being invalid before EPERM'ing
237 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000238 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100239 if ((map->offset >= entry->bound) &&
240 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
241 valid = 1;
242 break;
243 }
244 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000245 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100246 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
247 return -EPERM;
248 }
249 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 case _DRM_SCATTER_GATHER:
254 if (!dev->sg) {
255 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
256 return -EINVAL;
257 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000258 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000261 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000262 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000263 * casting it down to 32 bits is no problem, but we
264 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000265 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
266 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000267 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
268 return -ENOMEM;
269 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000270 map->handle = dmah->vaddr;
271 map->offset = (unsigned long)dmah->busaddr;
272 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
277 }
278
279 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700281 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100282 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
284 return -EINVAL;
285 }
286 memset(list, 0, sizeof(*list));
287 list->map = map;
288
Dave Airlie30e2fb12006-02-02 19:37:46 +1100289 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000290 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000291
Dave Airlied1f2b552005-08-05 22:11:22 +1000292 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100293 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000294 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
295 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000296 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000297 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700298 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100299 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000300 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
301 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
302 mutex_unlock(&dev->struct_mutex);
303 return ret;
304 }
305
Thomas Hellstrom15450852007-02-08 16:14:05 +1100306 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100307 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Dave Airlie89625eb2005-09-05 21:23:23 +1000309 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000310 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100311 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000312
Dave Airlie84b1fd12007-07-11 15:53:27 +1000313int drm_addmap(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000314 unsigned int size, enum drm_map_type type,
315 enum drm_map_flags flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000316{
317 drm_map_list_t *list;
318 int rc;
319
320 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
321 if (!rc)
322 *map_ptr = list->map;
323 return rc;
324}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325
Dave Airlie7ab98402005-07-10 16:56:52 +1000326EXPORT_SYMBOL(drm_addmap);
327
328int drm_addmap_ioctl(struct inode *inode, struct file *filp,
329 unsigned int cmd, unsigned long arg)
330{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000331 struct drm_file *priv = filp->private_data;
332 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000333 struct drm_map map;
Dave Airlie89625eb2005-09-05 21:23:23 +1000334 drm_map_list_t *maplist;
Dave Airliec60ce622007-07-11 15:27:12 +1000335 struct drm_map __user *argp = (void __user *)arg;
Dave Airlie7ab98402005-07-10 16:56:52 +1000336 int err;
337
338 if (!(filp->f_mode & 3))
339 return -EACCES; /* Require read/write */
340
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return -EFAULT;
Dave Airlie7ab98402005-07-10 16:56:52 +1000343 }
344
Dave Airlied985c102006-01-02 21:32:48 +1100345 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
346 return -EPERM;
347
Dave Airlie89625eb2005-09-05 21:23:23 +1000348 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
349 &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000350
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000351 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000352 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000353
Dave Airliec60ce622007-07-11 15:27:12 +1000354 if (copy_to_user(argp, maplist->map, sizeof(struct drm_map)))
Dave Airlied1f2b552005-08-05 22:11:22 +1000355 return -EFAULT;
Dave Airlie67e1a012005-10-24 18:41:39 +1000356
357 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
358 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
Dave Airlied1f2b552005-08-05 22:11:22 +1000359 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000361}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/**
364 * Remove a map private from list and deallocate resources if the mapping
365 * isn't in use.
366 *
367 * \param inode device inode.
368 * \param filp file pointer.
369 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +1000370 * \param arg pointer to a struct drm_map structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * \return zero on success or a negative value on error.
372 *
373 * Searches the map on drm_device::maplist, removes it from the list, see if
374 * its being used, and free any associate resource (such as MTRR's) if it's not
375 * being on use.
376 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000377 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000379int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Dave Airliebd1b3312007-05-26 05:01:51 +1000381 drm_map_list_t *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000382 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000383 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Dave Airlie836cf042005-07-10 19:27:04 +1000385 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000386 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000387 if (r_list->map == map) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000388 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100389 drm_ht_remove_key(&dev->map_hash,
390 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000391 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
392 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Dave Airlie836cf042005-07-10 19:27:04 +1000396
Dave Airliebd1b3312007-05-26 05:01:51 +1000397 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000398 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000399
400 switch (map->type) {
401 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100402 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000403 /* FALLTHROUGH */
404 case _DRM_FRAME_BUFFER:
405 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
406 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 retcode = mtrr_del(map->mtrr, map->offset, map->size);
408 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000409 }
410 break;
411 case _DRM_SHM:
412 vfree(map->handle);
413 break;
414 case _DRM_AGP:
415 case _DRM_SCATTER_GATHER:
416 break;
417 case _DRM_CONSISTENT:
418 dmah.vaddr = map->handle;
419 dmah.busaddr = map->offset;
420 dmah.size = map->size;
421 __drm_pci_free(dev, &dmah);
422 break;
423 }
424 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
Dave Airlie836cf042005-07-10 19:27:04 +1000428
Dave Airlie84b1fd12007-07-11 15:53:27 +1000429int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000430{
431 int ret;
432
Dave Airlie30e2fb12006-02-02 19:37:46 +1100433 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000434 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100435 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000436
437 return ret;
438}
Dave Airlie7ab98402005-07-10 16:56:52 +1000439
Dave Airlie836cf042005-07-10 19:27:04 +1000440/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
441 * the last close of the device, and this is necessary for cleanup when things
442 * exit uncleanly. Therefore, having userland manually remove mappings seems
443 * like a pointless exercise since they're going away anyway.
444 *
445 * One use case might be after addmap is allowed for normal users for SHM and
446 * gets used by drivers that the server doesn't need to care about. This seems
447 * unlikely.
448 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000449int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
450 unsigned int cmd, unsigned long arg)
451{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000452 struct drm_file *priv = filp->private_data;
453 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000454 struct drm_map request;
Dave Airlie836cf042005-07-10 19:27:04 +1000455 drm_local_map_t *map = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000456 drm_map_list_t *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000457 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000458
Dave Airliec60ce622007-07-11 15:27:12 +1000459 if (copy_from_user(&request, (struct drm_map __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000460 return -EFAULT;
461 }
462
Dave Airlie30e2fb12006-02-02 19:37:46 +1100463 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000464 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000465 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000467 r_list->map->flags & _DRM_REMOVABLE) {
468 map = r_list->map;
469 break;
470 }
471 }
472
473 /* List has wrapped around to the head pointer, or its empty we didn't
474 * find anything.
475 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000476 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100477 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000478 return -EINVAL;
479 }
480
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000481 if (!map) {
482 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000483 return -EINVAL;
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000484 }
Dave Airlie836cf042005-07-10 19:27:04 +1000485
486 /* Register and framebuffer maps are permanent */
487 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100488 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000489 return 0;
490 }
491
492 ret = drm_rmmap_locked(dev, map);
493
Dave Airlie30e2fb12006-02-02 19:37:46 +1100494 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000495
496 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000497}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499/**
500 * Cleanup after an error on one of the addbufs() functions.
501 *
Dave Airlie836cf042005-07-10 19:27:04 +1000502 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * \param entry buffer entry where the error occurred.
504 *
505 * Frees any pages and buffers associated with the given entry.
506 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000507static void drm_cleanup_buf_error(struct drm_device * dev, drm_buf_entry_t * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int i;
510
511 if (entry->seg_count) {
512 for (i = 0; i < entry->seg_count; i++) {
513 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100514 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 }
517 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 entry->seg_count *
519 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 entry->seg_count = 0;
522 }
523
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 if (entry->buf_count) {
525 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (entry->buflist[i].dev_private) {
527 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528 entry->buflist[i].dev_priv_size,
529 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
532 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 entry->buf_count *
534 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 entry->buf_count = 0;
537 }
538}
539
540#if __OS_HAS_AGP
541/**
Dave Airlied59431b2005-07-10 15:00:06 +1000542 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000544 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000545 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * After some sanity checks creates a drm_buf structure for each buffer and
549 * reallocates the buffer list of the same size order to accommodate the new
550 * buffers.
551 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000552int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 drm_buf_entry_t *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100556 drm_agp_mem_t *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000557 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned long offset;
559 unsigned long agp_offset;
560 int count;
561 int order;
562 int size;
563 int alignment;
564 int page_order;
565 int total;
566 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100567 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000568 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 if (!dma)
571 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dave Airlied59431b2005-07-10 15:00:06 +1000573 count = request->count;
574 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 size = 1 << order;
576
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 alignment = (request->flags & _DRM_PAGE_ALIGN)
578 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
580 total = PAGE_SIZE << page_order;
581
582 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000583 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 DRM_DEBUG("count: %d\n", count);
586 DRM_DEBUG("order: %d\n", order);
587 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100588 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 DRM_DEBUG("alignment: %d\n", alignment);
590 DRM_DEBUG("page_order: %d\n", page_order);
591 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000593 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
594 return -EINVAL;
595 if (dev->queue_count)
596 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Airlie54ba2f72007-02-10 12:07:47 +1100598 /* Make sure buffers are located in AGP memory that we own */
599 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000600 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100601 if ((agp_offset >= agp_entry->bound) &&
602 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
603 valid = 1;
604 break;
605 }
606 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000607 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100608 DRM_DEBUG("zone invalid\n");
609 return -EINVAL;
610 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 spin_lock(&dev->count_lock);
612 if (dev->buf_use) {
613 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EBUSY;
615 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000616 atomic_inc(&dev->buf_alloc);
617 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dave Airlie30e2fb12006-02-02 19:37:46 +1100619 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100622 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 atomic_dec(&dev->buf_alloc);
624 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100628 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -EINVAL;
631 }
632
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
634 DRM_MEM_BUFS);
635 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100636 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ENOMEM;
639 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 entry->buf_size = size;
643 entry->page_order = page_order;
644
645 offset = 0;
646
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 while (entry->buf_count < count) {
648 buf = &entry->buflist[entry->buf_count];
649 buf->idx = dma->buf_count + entry->buf_count;
650 buf->total = alignment;
651 buf->order = order;
652 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 buf->bus_address = agp_offset + offset;
656 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 buf->waiting = 0;
659 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 init_waitqueue_head(&buf->dma_wait);
661 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000664 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
665 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* Set count correctly so we free the proper amount. */
667 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100669 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -ENOMEM;
672 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 offset += alignment;
678 entry->buf_count++;
679 byte_count += PAGE_SIZE << page_order;
680 }
681
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 temp_buflist = drm_realloc(dma->buflist,
685 dma->buf_count * sizeof(*dma->buflist),
686 (dma->buf_count + entry->buf_count)
687 * sizeof(*dma->buflist), DRM_MEM_BUFS);
688 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100691 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -ENOMEM;
694 }
695 dma->buflist = temp_buflist;
696
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
699 }
700
701 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100702 dma->seg_count += entry->seg_count;
703 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dma->byte_count += byte_count;
705
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
707 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Dave Airlie30e2fb12006-02-02 19:37:46 +1100709 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Airlied59431b2005-07-10 15:00:06 +1000711 request->count = entry->buf_count;
712 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 dma->flags = _DRM_DMA_USE_AGP;
715
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719EXPORT_SYMBOL(drm_addbufs_agp);
720#endif /* __OS_HAS_AGP */
721
Dave Airlie84b1fd12007-07-11 15:53:27 +1000722int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int count;
726 int order;
727 int size;
728 int total;
729 int page_order;
730 drm_buf_entry_t *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100731 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000732 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int alignment;
734 unsigned long offset;
735 int i;
736 int byte_count;
737 int page_count;
738 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000739 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
742 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100743
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 if (!dma)
745 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dave Airlied985c102006-01-02 21:32:48 +1100747 if (!capable(CAP_SYS_ADMIN))
748 return -EPERM;
749
Dave Airlied59431b2005-07-10 15:00:06 +1000750 count = request->count;
751 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 size = 1 << order;
753
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000754 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
755 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
758 return -EINVAL;
759 if (dev->queue_count)
760 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dave Airlied59431b2005-07-10 15:00:06 +1000762 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
765 total = PAGE_SIZE << page_order;
766
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 spin_lock(&dev->count_lock);
768 if (dev->buf_use) {
769 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -EBUSY;
771 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 atomic_inc(&dev->buf_alloc);
773 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dave Airlie30e2fb12006-02-02 19:37:46 +1100775 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100778 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -ENOMEM; /* May only call once for each order */
781 }
782
783 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100784 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000785 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -EINVAL;
787 }
788
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
790 DRM_MEM_BUFS);
791 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100792 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -ENOMEM;
795 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
799 DRM_MEM_SEGS);
800 if (!entry->seglist) {
801 drm_free(entry->buflist,
802 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100803 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -ENOMEM;
806 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* Keep the original pagelist until we know all the allocations
810 * have succeeded
811 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
813 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 drm_free(entry->buflist,
816 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
817 drm_free(entry->seglist,
818 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100819 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return -ENOMEM;
822 }
823 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
825 DRM_DEBUG("pagelist: %d entries\n",
826 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 entry->page_order = page_order;
830 byte_count = 0;
831 page_count = 0;
832
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100834
835 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
836
837 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Set count correctly so we free the proper amount. */
839 entry->buf_count = count;
840 entry->seg_count = count;
841 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 drm_free(temp_pagelist,
843 (dma->page_count + (count << page_order))
844 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100845 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -ENOMEM;
848 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100849 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 for (i = 0; i < (1 << page_order); i++) {
851 DRM_DEBUG("page %d @ 0x%08lx\n",
852 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100853 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100855 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 for (offset = 0;
858 offset + size <= total && entry->buf_count < count;
859 offset += alignment, ++entry->buf_count) {
860 buf = &entry->buflist[entry->buf_count];
861 buf->idx = dma->buf_count + entry->buf_count;
862 buf->total = alignment;
863 buf->order = order;
864 buf->used = 0;
865 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100866 buf->address = (void *)(dmah->vaddr + offset);
867 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 buf->waiting = 0;
870 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 init_waitqueue_head(&buf->dma_wait);
872 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 buf->dev_private = drm_alloc(buf->dev_priv_size,
876 DRM_MEM_BUFS);
877 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* Set count correctly so we free the proper amount. */
879 entry->buf_count = count;
880 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 drm_cleanup_buf_error(dev, entry);
882 drm_free(temp_pagelist,
883 (dma->page_count +
884 (count << page_order))
885 * sizeof(*dma->pagelist),
886 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100887 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENOMEM;
890 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 DRM_DEBUG("buffer %d @ %p\n",
894 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 byte_count += PAGE_SIZE << page_order;
897 }
898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 temp_buflist = drm_realloc(dma->buflist,
900 dma->buf_count * sizeof(*dma->buflist),
901 (dma->buf_count + entry->buf_count)
902 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (!temp_buflist) {
904 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 drm_cleanup_buf_error(dev, entry);
906 drm_free(temp_pagelist,
907 (dma->page_count + (count << page_order))
908 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100909 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENOMEM;
912 }
913 dma->buflist = temp_buflist;
914
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
917 }
918
919 /* No allocations failed, so now we can replace the orginal pagelist
920 * with the new one.
921 */
922 if (dma->page_count) {
923 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 dma->page_count * sizeof(*dma->pagelist),
925 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 dma->pagelist = temp_pagelist;
928
929 dma->buf_count += entry->buf_count;
930 dma->seg_count += entry->seg_count;
931 dma->page_count += entry->seg_count << page_order;
932 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
933
Dave Airlie30e2fb12006-02-02 19:37:46 +1100934 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Dave Airlied59431b2005-07-10 15:00:06 +1000936 request->count = entry->buf_count;
937 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
George Sapountzis3417f332006-10-24 12:03:04 -0700939 if (request->flags & _DRM_PCI_BUFFER_RO)
940 dma->flags = _DRM_DMA_USE_PCI_RO;
941
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944
945}
Dave Airlied84f76d2005-07-10 17:04:22 +1000946EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dave Airlie84b1fd12007-07-11 15:53:27 +1000948static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 drm_buf_entry_t *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000952 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 unsigned long offset;
954 unsigned long agp_offset;
955 int count;
956 int order;
957 int size;
958 int alignment;
959 int page_order;
960 int total;
961 int byte_count;
962 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000963 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 if (!drm_core_check_feature(dev, DRIVER_SG))
966 return -EINVAL;
967
968 if (!dma)
969 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dave Airlied985c102006-01-02 21:32:48 +1100971 if (!capable(CAP_SYS_ADMIN))
972 return -EPERM;
973
Dave Airlied59431b2005-07-10 15:00:06 +1000974 count = request->count;
975 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 size = 1 << order;
977
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000978 alignment = (request->flags & _DRM_PAGE_ALIGN)
979 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
981 total = PAGE_SIZE << page_order;
982
983 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000984 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 DRM_DEBUG("count: %d\n", count);
987 DRM_DEBUG("order: %d\n", order);
988 DRM_DEBUG("size: %d\n", size);
989 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
990 DRM_DEBUG("alignment: %d\n", alignment);
991 DRM_DEBUG("page_order: %d\n", page_order);
992 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
995 return -EINVAL;
996 if (dev->queue_count)
997 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 spin_lock(&dev->count_lock);
1000 if (dev->buf_use) {
1001 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return -EBUSY;
1003 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 atomic_inc(&dev->buf_alloc);
1005 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dave Airlie30e2fb12006-02-02 19:37:46 +11001007 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001010 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 atomic_dec(&dev->buf_alloc);
1012 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
1015 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001016 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EINVAL;
1019 }
1020
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1022 DRM_MEM_BUFS);
1023 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001024 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -ENOMEM;
1027 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 entry->buf_size = size;
1031 entry->page_order = page_order;
1032
1033 offset = 0;
1034
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001035 while (entry->buf_count < count) {
1036 buf = &entry->buflist[entry->buf_count];
1037 buf->idx = dma->buf_count + entry->buf_count;
1038 buf->total = alignment;
1039 buf->order = order;
1040 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001045 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 buf->waiting = 0;
1048 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 init_waitqueue_head(&buf->dma_wait);
1050 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1054 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Set count correctly so we free the proper amount. */
1056 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001058 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -ENOMEM;
1061 }
1062
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 offset += alignment;
1068 entry->buf_count++;
1069 byte_count += PAGE_SIZE << page_order;
1070 }
1071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 temp_buflist = drm_realloc(dma->buflist,
1075 dma->buf_count * sizeof(*dma->buflist),
1076 (dma->buf_count + entry->buf_count)
1077 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1078 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001081 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -ENOMEM;
1084 }
1085 dma->buflist = temp_buflist;
1086
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1089 }
1090
1091 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001092 dma->seg_count += entry->seg_count;
1093 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dma->byte_count += byte_count;
1095
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1097 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Dave Airlie30e2fb12006-02-02 19:37:46 +11001099 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Dave Airlied59431b2005-07-10 15:00:06 +10001101 request->count = entry->buf_count;
1102 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 dma->flags = _DRM_DMA_USE_SG;
1105
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
1108}
1109
Dave Airlie84b1fd12007-07-11 15:53:27 +10001110static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001111{
Dave Airlieb84397d62005-07-10 14:46:12 +10001112 drm_device_dma_t *dma = dev->dma;
Dave Airlieb84397d62005-07-10 14:46:12 +10001113 drm_buf_entry_t *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001114 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001115 unsigned long offset;
1116 unsigned long agp_offset;
1117 int count;
1118 int order;
1119 int size;
1120 int alignment;
1121 int page_order;
1122 int total;
1123 int byte_count;
1124 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001125 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001126
1127 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1128 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129
Dave Airlieb84397d62005-07-10 14:46:12 +10001130 if (!dma)
1131 return -EINVAL;
1132
Dave Airlied985c102006-01-02 21:32:48 +11001133 if (!capable(CAP_SYS_ADMIN))
1134 return -EPERM;
1135
Dave Airlied59431b2005-07-10 15:00:06 +10001136 count = request->count;
1137 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001138 size = 1 << order;
1139
Dave Airlied59431b2005-07-10 15:00:06 +10001140 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001141 ? PAGE_ALIGN(size) : size;
1142 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1143 total = PAGE_SIZE << page_order;
1144
1145 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001146 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001147
1148 DRM_DEBUG("count: %d\n", count);
1149 DRM_DEBUG("order: %d\n", order);
1150 DRM_DEBUG("size: %d\n", size);
1151 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1152 DRM_DEBUG("alignment: %d\n", alignment);
1153 DRM_DEBUG("page_order: %d\n", page_order);
1154 DRM_DEBUG("total: %d\n", total);
1155
1156 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1157 return -EINVAL;
1158 if (dev->queue_count)
1159 return -EBUSY; /* Not while in use */
1160
1161 spin_lock(&dev->count_lock);
1162 if (dev->buf_use) {
1163 spin_unlock(&dev->count_lock);
1164 return -EBUSY;
1165 }
1166 atomic_inc(&dev->buf_alloc);
1167 spin_unlock(&dev->count_lock);
1168
Dave Airlie30e2fb12006-02-02 19:37:46 +11001169 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001170 entry = &dma->bufs[order];
1171 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001172 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001173 atomic_dec(&dev->buf_alloc);
1174 return -ENOMEM; /* May only call once for each order */
1175 }
1176
1177 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001178 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001179 atomic_dec(&dev->buf_alloc);
1180 return -EINVAL;
1181 }
1182
1183 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1184 DRM_MEM_BUFS);
1185 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001186 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001187 atomic_dec(&dev->buf_alloc);
1188 return -ENOMEM;
1189 }
1190 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1191
1192 entry->buf_size = size;
1193 entry->page_order = page_order;
1194
1195 offset = 0;
1196
1197 while (entry->buf_count < count) {
1198 buf = &entry->buflist[entry->buf_count];
1199 buf->idx = dma->buf_count + entry->buf_count;
1200 buf->total = alignment;
1201 buf->order = order;
1202 buf->used = 0;
1203
1204 buf->offset = (dma->byte_count + offset);
1205 buf->bus_address = agp_offset + offset;
1206 buf->address = (void *)(agp_offset + offset);
1207 buf->next = NULL;
1208 buf->waiting = 0;
1209 buf->pending = 0;
1210 init_waitqueue_head(&buf->dma_wait);
1211 buf->filp = NULL;
1212
1213 buf->dev_priv_size = dev->driver->dev_priv_size;
1214 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1215 if (!buf->dev_private) {
1216 /* Set count correctly so we free the proper amount. */
1217 entry->buf_count = count;
1218 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001219 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001220 atomic_dec(&dev->buf_alloc);
1221 return -ENOMEM;
1222 }
1223 memset(buf->dev_private, 0, buf->dev_priv_size);
1224
1225 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1226
1227 offset += alignment;
1228 entry->buf_count++;
1229 byte_count += PAGE_SIZE << page_order;
1230 }
1231
1232 DRM_DEBUG("byte_count: %d\n", byte_count);
1233
1234 temp_buflist = drm_realloc(dma->buflist,
1235 dma->buf_count * sizeof(*dma->buflist),
1236 (dma->buf_count + entry->buf_count)
1237 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1238 if (!temp_buflist) {
1239 /* Free the entry because it isn't valid */
1240 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001241 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001242 atomic_dec(&dev->buf_alloc);
1243 return -ENOMEM;
1244 }
1245 dma->buflist = temp_buflist;
1246
1247 for (i = 0; i < entry->buf_count; i++) {
1248 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1249 }
1250
1251 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001252 dma->seg_count += entry->seg_count;
1253 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001254 dma->byte_count += byte_count;
1255
1256 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1257 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1258
Dave Airlie30e2fb12006-02-02 19:37:46 +11001259 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001260
Dave Airlied59431b2005-07-10 15:00:06 +10001261 request->count = entry->buf_count;
1262 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001263
1264 dma->flags = _DRM_DMA_USE_FB;
1265
1266 atomic_dec(&dev->buf_alloc);
1267 return 0;
1268}
Dave Airlied985c102006-01-02 21:32:48 +11001269
Dave Airlieb84397d62005-07-10 14:46:12 +10001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/**
1272 * Add buffers for DMA transfers (ioctl).
1273 *
1274 * \param inode device inode.
1275 * \param filp file pointer.
1276 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001277 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * \return zero on success or a negative number on failure.
1279 *
1280 * According with the memory type specified in drm_buf_desc::flags and the
1281 * build options, it dispatches the call either to addbufs_agp(),
1282 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1283 * PCI memory respectively.
1284 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001285int drm_addbufs(struct inode *inode, struct file *filp,
1286 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Dave Airliec60ce622007-07-11 15:27:12 +10001288 struct drm_buf_desc request;
Dave Airlie84b1fd12007-07-11 15:53:27 +10001289 struct drm_file *priv = filp->private_data;
1290 struct drm_device *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001291 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1294 return -EINVAL;
1295
Dave Airliec60ce622007-07-11 15:27:12 +10001296 if (copy_from_user(&request, (struct drm_buf_desc __user *) arg,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001297 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EFAULT;
1299
1300#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301 if (request.flags & _DRM_AGP_BUFFER)
1302 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 else
1304#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 if (request.flags & _DRM_SG_BUFFER)
1306 ret = drm_addbufs_sg(dev, &request);
1307 else if (request.flags & _DRM_FB_BUFFER)
1308 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001310 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001311
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 if (ret == 0) {
1313 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001314 ret = -EFAULT;
1315 }
1316 }
1317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320/**
1321 * Get information about the buffer mappings.
1322 *
1323 * This was originally mean for debugging purposes, or by a sophisticated
1324 * client library to determine how best to use the available buffers (e.g.,
1325 * large buffers can be used for image transfer).
1326 *
1327 * \param inode device inode.
1328 * \param filp file pointer.
1329 * \param cmd command.
1330 * \param arg pointer to a drm_buf_info structure.
1331 * \return zero on success or a negative number on failure.
1332 *
1333 * Increments drm_device::buf_use while holding the drm_device::count_lock
1334 * lock, preventing of allocating more buffers after this call. Information
1335 * about each requested buffer is then copied into user space.
1336 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001337int drm_infobufs(struct inode *inode, struct file *filp,
1338 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001340 struct drm_file *priv = filp->private_data;
1341 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 drm_device_dma_t *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001343 struct drm_buf_info request;
1344 struct drm_buf_info __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int i;
1346 int count;
1347
1348 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1349 return -EINVAL;
1350
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001351 if (!dma)
1352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354 spin_lock(&dev->count_lock);
1355 if (atomic_read(&dev->buf_alloc)) {
1356 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EBUSY;
1358 }
1359 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001360 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EFAULT;
1364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1366 if (dma->bufs[i].buf_count)
1367 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001370 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001372 if (request.count >= count) {
1373 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1374 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001375 struct drm_buf_desc __user *to =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001376 &request.list[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 drm_buf_entry_t *from = &dma->bufs[i];
1378 drm_freelist_t *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001379 if (copy_to_user(&to->count,
1380 &from->buf_count,
1381 sizeof(from->buf_count)) ||
1382 copy_to_user(&to->size,
1383 &from->buf_size,
1384 sizeof(from->buf_size)) ||
1385 copy_to_user(&to->low_mark,
1386 &list->low_mark,
1387 sizeof(list->low_mark)) ||
1388 copy_to_user(&to->high_mark,
1389 &list->high_mark,
1390 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -EFAULT;
1392
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001393 DRM_DEBUG("%d %d %d %d %d\n",
1394 i,
1395 dma->bufs[i].buf_count,
1396 dma->bufs[i].buf_size,
1397 dma->bufs[i].freelist.low_mark,
1398 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ++count;
1400 }
1401 }
1402 }
1403 request.count = count;
1404
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return -EFAULT;
1407
1408 return 0;
1409}
1410
1411/**
1412 * Specifies a low and high water mark for buffer allocation
1413 *
1414 * \param inode device inode.
1415 * \param filp file pointer.
1416 * \param cmd command.
1417 * \param arg a pointer to a drm_buf_desc structure.
1418 * \return zero on success or a negative number on failure.
1419 *
1420 * Verifies that the size order is bounded between the admissible orders and
1421 * updates the respective drm_device_dma::bufs entry low and high water mark.
1422 *
1423 * \note This ioctl is deprecated and mostly never used.
1424 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001425int drm_markbufs(struct inode *inode, struct file *filp,
1426 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001428 struct drm_file *priv = filp->private_data;
1429 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 drm_device_dma_t *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001431 struct drm_buf_desc request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 int order;
1433 drm_buf_entry_t *entry;
1434
1435 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1436 return -EINVAL;
1437
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001438 if (!dma)
1439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001441 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001442 (struct drm_buf_desc __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EFAULT;
1444
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001445 DRM_DEBUG("%d, %d, %d\n",
1446 request.size, request.low_mark, request.high_mark);
1447 order = drm_order(request.size);
1448 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1449 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 entry = &dma->bufs[order];
1451
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001452 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -EINVAL;
1456
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001457 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 entry->freelist.high_mark = request.high_mark;
1459
1460 return 0;
1461}
1462
1463/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001464 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
1466 * \param inode device inode.
1467 * \param filp file pointer.
1468 * \param cmd command.
1469 * \param arg pointer to a drm_buf_free structure.
1470 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001471 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * Calls free_buffer() for each used buffer.
1473 * This function is primarily used for debugging.
1474 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001475int drm_freebufs(struct inode *inode, struct file *filp,
1476 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001478 struct drm_file *priv = filp->private_data;
1479 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 drm_device_dma_t *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001481 struct drm_buf_free request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int i;
1483 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001484 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1487 return -EINVAL;
1488
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 if (!dma)
1490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001492 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001493 (struct drm_buf_free __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EFAULT;
1495
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001496 DRM_DEBUG("%d\n", request.count);
1497 for (i = 0; i < request.count; i++) {
1498 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001500 if (idx < 0 || idx >= dma->buf_count) {
1501 DRM_ERROR("Index %d (of %d max)\n",
1502 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EINVAL;
1504 }
1505 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001506 if (buf->filp != filp) {
1507 DRM_ERROR("Process %d freeing buffer not owned\n",
1508 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return -EINVAL;
1510 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001511 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
1514 return 0;
1515}
1516
1517/**
1518 * Maps all of the DMA buffers into client-virtual space (ioctl).
1519 *
1520 * \param inode device inode.
1521 * \param filp file pointer.
1522 * \param cmd command.
1523 * \param arg pointer to a drm_buf_map structure.
1524 * \return zero on success or a negative number on failure.
1525 *
George Sapountzis3417f332006-10-24 12:03:04 -07001526 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1527 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1528 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1529 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001531int drm_mapbufs(struct inode *inode, struct file *filp,
1532 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001534 struct drm_file *priv = filp->private_data;
1535 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 drm_device_dma_t *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001537 struct drm_buf_map __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int retcode = 0;
1539 const int zero = 0;
1540 unsigned long virtual;
1541 unsigned long address;
Dave Airliec60ce622007-07-11 15:27:12 +10001542 struct drm_buf_map request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int i;
1544
1545 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1546 return -EINVAL;
1547
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001548 if (!dma)
1549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 spin_lock(&dev->count_lock);
1552 if (atomic_read(&dev->buf_alloc)) {
1553 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return -EBUSY;
1555 }
1556 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001559 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -EFAULT;
1561
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001562 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001563 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001565 && (dma->flags & _DRM_DMA_USE_SG))
1566 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1567 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001568 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001569 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 retcode = -EINVAL;
1573 goto done;
1574 }
1575
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001576 down_write(&current->mm->mmap_sem);
1577 virtual = do_mmap(filp, 0, map->size,
1578 PROT_READ | PROT_WRITE,
1579 MAP_SHARED, token);
1580 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001582 down_write(&current->mm->mmap_sem);
1583 virtual = do_mmap(filp, 0, dma->byte_count,
1584 PROT_READ | PROT_WRITE,
1585 MAP_SHARED, 0);
1586 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001588 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* Real error */
1590 retcode = (signed long)virtual;
1591 goto done;
1592 }
1593 request.virtual = (void __user *)virtual;
1594
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001595 for (i = 0; i < dma->buf_count; i++) {
1596 if (copy_to_user(&request.list[i].idx,
1597 &dma->buflist[i]->idx,
1598 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 retcode = -EFAULT;
1600 goto done;
1601 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001602 if (copy_to_user(&request.list[i].total,
1603 &dma->buflist[i]->total,
1604 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 retcode = -EFAULT;
1606 goto done;
1607 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001608 if (copy_to_user(&request.list[i].used,
1609 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 retcode = -EFAULT;
1611 goto done;
1612 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001613 address = virtual + dma->buflist[i]->offset; /* *** */
1614 if (copy_to_user(&request.list[i].address,
1615 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 retcode = -EFAULT;
1617 goto done;
1618 }
1619 }
1620 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001621 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001623 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001625 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -EFAULT;
1627
1628 return retcode;
1629}
1630
Dave Airlie836cf042005-07-10 19:27:04 +10001631/**
1632 * Compute size order. Returns the exponent of the smaller power of two which
1633 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001634 *
Dave Airlie836cf042005-07-10 19:27:04 +10001635 * \param size size.
1636 * \return order.
1637 *
1638 * \todo Can be made faster.
1639 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001640int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001641{
1642 int order;
1643 unsigned long tmp;
1644
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001645 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001646
1647 if (size & (size - 1))
1648 ++order;
1649
1650 return order;
1651}
1652EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001653
1654