blob: 3528453c8ece9e4552c3ee2663c5f1b651df6c8f [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 Airliecdd55a22007-07-11 16:32:08 +1000507static void drm_cleanup_buf_error(struct drm_device * dev,
508 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int i;
511
512 if (entry->seg_count) {
513 for (i = 0; i < entry->seg_count; i++) {
514 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100515 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 }
518 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 entry->seg_count *
520 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 entry->seg_count = 0;
523 }
524
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000525 if (entry->buf_count) {
526 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (entry->buflist[i].dev_private) {
528 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529 entry->buflist[i].dev_priv_size,
530 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 entry->buf_count *
535 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 entry->buf_count = 0;
538 }
539}
540
541#if __OS_HAS_AGP
542/**
Dave Airlied59431b2005-07-10 15:00:06 +1000543 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000545 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000546 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * After some sanity checks creates a drm_buf structure for each buffer and
550 * reallocates the buffer list of the same size order to accommodate the new
551 * buffers.
552 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000553int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Dave Airliecdd55a22007-07-11 16:32:08 +1000555 struct drm_device_dma *dma = dev->dma;
556 struct drm_buf_entry *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100557 drm_agp_mem_t *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000558 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 unsigned long offset;
560 unsigned long agp_offset;
561 int count;
562 int order;
563 int size;
564 int alignment;
565 int page_order;
566 int total;
567 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100568 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000569 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000571 if (!dma)
572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dave Airlied59431b2005-07-10 15:00:06 +1000574 count = request->count;
575 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 size = 1 << order;
577
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 alignment = (request->flags & _DRM_PAGE_ALIGN)
579 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
581 total = PAGE_SIZE << page_order;
582
583 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000584 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 DRM_DEBUG("count: %d\n", count);
587 DRM_DEBUG("order: %d\n", order);
588 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100589 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 DRM_DEBUG("alignment: %d\n", alignment);
591 DRM_DEBUG("page_order: %d\n", page_order);
592 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
595 return -EINVAL;
596 if (dev->queue_count)
597 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Dave Airlie54ba2f72007-02-10 12:07:47 +1100599 /* Make sure buffers are located in AGP memory that we own */
600 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000601 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100602 if ((agp_offset >= agp_entry->bound) &&
603 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
604 valid = 1;
605 break;
606 }
607 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000608 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100609 DRM_DEBUG("zone invalid\n");
610 return -EINVAL;
611 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 spin_lock(&dev->count_lock);
613 if (dev->buf_use) {
614 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EBUSY;
616 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 atomic_inc(&dev->buf_alloc);
618 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Dave Airlie30e2fb12006-02-02 19:37:46 +1100620 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100623 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 atomic_dec(&dev->buf_alloc);
625 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100629 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -EINVAL;
632 }
633
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
635 DRM_MEM_BUFS);
636 if (!entry->buflist) {
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 -ENOMEM;
640 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 entry->buf_size = size;
644 entry->page_order = page_order;
645
646 offset = 0;
647
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 while (entry->buf_count < count) {
649 buf = &entry->buflist[entry->buf_count];
650 buf->idx = dma->buf_count + entry->buf_count;
651 buf->total = alignment;
652 buf->order = order;
653 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 buf->bus_address = agp_offset + offset;
657 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 buf->waiting = 0;
660 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 init_waitqueue_head(&buf->dma_wait);
662 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
666 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Set count correctly so we free the proper amount. */
668 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100670 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -ENOMEM;
673 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 offset += alignment;
679 entry->buf_count++;
680 byte_count += PAGE_SIZE << page_order;
681 }
682
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 temp_buflist = drm_realloc(dma->buflist,
686 dma->buf_count * sizeof(*dma->buflist),
687 (dma->buf_count + entry->buf_count)
688 * sizeof(*dma->buflist), DRM_MEM_BUFS);
689 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100692 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -ENOMEM;
695 }
696 dma->buflist = temp_buflist;
697
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
700 }
701
702 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100703 dma->seg_count += entry->seg_count;
704 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 dma->byte_count += byte_count;
706
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
708 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Dave Airlie30e2fb12006-02-02 19:37:46 +1100710 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Dave Airlied59431b2005-07-10 15:00:06 +1000712 request->count = entry->buf_count;
713 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 dma->flags = _DRM_DMA_USE_AGP;
716
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720EXPORT_SYMBOL(drm_addbufs_agp);
721#endif /* __OS_HAS_AGP */
722
Dave Airlie84b1fd12007-07-11 15:53:27 +1000723int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Dave Airliecdd55a22007-07-11 16:32:08 +1000725 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int count;
727 int order;
728 int size;
729 int total;
730 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000731 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100732 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000733 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int alignment;
735 unsigned long offset;
736 int i;
737 int byte_count;
738 int page_count;
739 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000740 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
743 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100744
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 if (!dma)
746 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Dave Airlied985c102006-01-02 21:32:48 +1100748 if (!capable(CAP_SYS_ADMIN))
749 return -EPERM;
750
Dave Airlied59431b2005-07-10 15:00:06 +1000751 count = request->count;
752 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 size = 1 << order;
754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
756 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
759 return -EINVAL;
760 if (dev->queue_count)
761 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Dave Airlied59431b2005-07-10 15:00:06 +1000763 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
766 total = PAGE_SIZE << page_order;
767
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768 spin_lock(&dev->count_lock);
769 if (dev->buf_use) {
770 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -EBUSY;
772 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000773 atomic_inc(&dev->buf_alloc);
774 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Dave Airlie30e2fb12006-02-02 19:37:46 +1100776 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100779 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -ENOMEM; /* May only call once for each order */
782 }
783
784 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100785 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EINVAL;
788 }
789
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
791 DRM_MEM_BUFS);
792 if (!entry->buflist) {
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 -ENOMEM;
796 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
800 DRM_MEM_SEGS);
801 if (!entry->seglist) {
802 drm_free(entry->buflist,
803 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100804 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return -ENOMEM;
807 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* Keep the original pagelist until we know all the allocations
811 * have succeeded
812 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
814 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 drm_free(entry->buflist,
817 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
818 drm_free(entry->seglist,
819 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100820 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -ENOMEM;
823 }
824 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
826 DRM_DEBUG("pagelist: %d entries\n",
827 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 entry->page_order = page_order;
831 byte_count = 0;
832 page_count = 0;
833
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100835
836 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
837
838 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* Set count correctly so we free the proper amount. */
840 entry->buf_count = count;
841 entry->seg_count = count;
842 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 drm_free(temp_pagelist,
844 (dma->page_count + (count << page_order))
845 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100846 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -ENOMEM;
849 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100850 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 for (i = 0; i < (1 << page_order); i++) {
852 DRM_DEBUG("page %d @ 0x%08lx\n",
853 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100854 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100856 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 for (offset = 0;
859 offset + size <= total && entry->buf_count < count;
860 offset += alignment, ++entry->buf_count) {
861 buf = &entry->buflist[entry->buf_count];
862 buf->idx = dma->buf_count + entry->buf_count;
863 buf->total = alignment;
864 buf->order = order;
865 buf->used = 0;
866 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100867 buf->address = (void *)(dmah->vaddr + offset);
868 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 buf->waiting = 0;
871 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 init_waitqueue_head(&buf->dma_wait);
873 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 buf->dev_private = drm_alloc(buf->dev_priv_size,
877 DRM_MEM_BUFS);
878 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Set count correctly so we free the proper amount. */
880 entry->buf_count = count;
881 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 drm_cleanup_buf_error(dev, entry);
883 drm_free(temp_pagelist,
884 (dma->page_count +
885 (count << page_order))
886 * sizeof(*dma->pagelist),
887 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100888 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -ENOMEM;
891 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 DRM_DEBUG("buffer %d @ %p\n",
895 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 byte_count += PAGE_SIZE << page_order;
898 }
899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 temp_buflist = drm_realloc(dma->buflist,
901 dma->buf_count * sizeof(*dma->buflist),
902 (dma->buf_count + entry->buf_count)
903 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (!temp_buflist) {
905 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 drm_cleanup_buf_error(dev, entry);
907 drm_free(temp_pagelist,
908 (dma->page_count + (count << page_order))
909 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100910 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -ENOMEM;
913 }
914 dma->buflist = temp_buflist;
915
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
918 }
919
920 /* No allocations failed, so now we can replace the orginal pagelist
921 * with the new one.
922 */
923 if (dma->page_count) {
924 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 dma->page_count * sizeof(*dma->pagelist),
926 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 dma->pagelist = temp_pagelist;
929
930 dma->buf_count += entry->buf_count;
931 dma->seg_count += entry->seg_count;
932 dma->page_count += entry->seg_count << page_order;
933 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
934
Dave Airlie30e2fb12006-02-02 19:37:46 +1100935 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Dave Airlied59431b2005-07-10 15:00:06 +1000937 request->count = entry->buf_count;
938 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
George Sapountzis3417f332006-10-24 12:03:04 -0700940 if (request->flags & _DRM_PCI_BUFFER_RO)
941 dma->flags = _DRM_DMA_USE_PCI_RO;
942
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945
946}
Dave Airlied84f76d2005-07-10 17:04:22 +1000947EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dave Airlie84b1fd12007-07-11 15:53:27 +1000949static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Dave Airliecdd55a22007-07-11 16:32:08 +1000951 struct drm_device_dma *dma = dev->dma;
952 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000953 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 unsigned long offset;
955 unsigned long agp_offset;
956 int count;
957 int order;
958 int size;
959 int alignment;
960 int page_order;
961 int total;
962 int byte_count;
963 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000964 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000966 if (!drm_core_check_feature(dev, DRIVER_SG))
967 return -EINVAL;
968
969 if (!dma)
970 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dave Airlied985c102006-01-02 21:32:48 +1100972 if (!capable(CAP_SYS_ADMIN))
973 return -EPERM;
974
Dave Airlied59431b2005-07-10 15:00:06 +1000975 count = request->count;
976 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 size = 1 << order;
978
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 alignment = (request->flags & _DRM_PAGE_ALIGN)
980 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
982 total = PAGE_SIZE << page_order;
983
984 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000985 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 DRM_DEBUG("count: %d\n", count);
988 DRM_DEBUG("order: %d\n", order);
989 DRM_DEBUG("size: %d\n", size);
990 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
991 DRM_DEBUG("alignment: %d\n", alignment);
992 DRM_DEBUG("page_order: %d\n", page_order);
993 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
996 return -EINVAL;
997 if (dev->queue_count)
998 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 spin_lock(&dev->count_lock);
1001 if (dev->buf_use) {
1002 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return -EBUSY;
1004 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 atomic_inc(&dev->buf_alloc);
1006 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Dave Airlie30e2fb12006-02-02 19:37:46 +11001008 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001011 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 atomic_dec(&dev->buf_alloc);
1013 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
1016 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001017 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return -EINVAL;
1020 }
1021
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1023 DRM_MEM_BUFS);
1024 if (!entry->buflist) {
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 -ENOMEM;
1028 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 entry->buf_size = size;
1032 entry->page_order = page_order;
1033
1034 offset = 0;
1035
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 while (entry->buf_count < count) {
1037 buf = &entry->buflist[entry->buf_count];
1038 buf->idx = dma->buf_count + entry->buf_count;
1039 buf->total = alignment;
1040 buf->order = order;
1041 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001046 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 buf->waiting = 0;
1049 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 init_waitqueue_head(&buf->dma_wait);
1051 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001054 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1055 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* Set count correctly so we free the proper amount. */
1057 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001059 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -ENOMEM;
1062 }
1063
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 offset += alignment;
1069 entry->buf_count++;
1070 byte_count += PAGE_SIZE << page_order;
1071 }
1072
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 temp_buflist = drm_realloc(dma->buflist,
1076 dma->buf_count * sizeof(*dma->buflist),
1077 (dma->buf_count + entry->buf_count)
1078 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1079 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001082 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -ENOMEM;
1085 }
1086 dma->buflist = temp_buflist;
1087
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001088 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1090 }
1091
1092 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001093 dma->seg_count += entry->seg_count;
1094 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 dma->byte_count += byte_count;
1096
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1098 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Dave Airlie30e2fb12006-02-02 19:37:46 +11001100 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Dave Airlied59431b2005-07-10 15:00:06 +10001102 request->count = entry->buf_count;
1103 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 dma->flags = _DRM_DMA_USE_SG;
1106
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001107 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return 0;
1109}
1110
Dave Airlie84b1fd12007-07-11 15:53:27 +10001111static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001112{
Dave Airliecdd55a22007-07-11 16:32:08 +10001113 struct drm_device_dma *dma = dev->dma;
1114 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001115 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001116 unsigned long offset;
1117 unsigned long agp_offset;
1118 int count;
1119 int order;
1120 int size;
1121 int alignment;
1122 int page_order;
1123 int total;
1124 int byte_count;
1125 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001126 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001127
1128 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1129 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130
Dave Airlieb84397d62005-07-10 14:46:12 +10001131 if (!dma)
1132 return -EINVAL;
1133
Dave Airlied985c102006-01-02 21:32:48 +11001134 if (!capable(CAP_SYS_ADMIN))
1135 return -EPERM;
1136
Dave Airlied59431b2005-07-10 15:00:06 +10001137 count = request->count;
1138 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001139 size = 1 << order;
1140
Dave Airlied59431b2005-07-10 15:00:06 +10001141 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001142 ? PAGE_ALIGN(size) : size;
1143 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1144 total = PAGE_SIZE << page_order;
1145
1146 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001147 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001148
1149 DRM_DEBUG("count: %d\n", count);
1150 DRM_DEBUG("order: %d\n", order);
1151 DRM_DEBUG("size: %d\n", size);
1152 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1153 DRM_DEBUG("alignment: %d\n", alignment);
1154 DRM_DEBUG("page_order: %d\n", page_order);
1155 DRM_DEBUG("total: %d\n", total);
1156
1157 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1158 return -EINVAL;
1159 if (dev->queue_count)
1160 return -EBUSY; /* Not while in use */
1161
1162 spin_lock(&dev->count_lock);
1163 if (dev->buf_use) {
1164 spin_unlock(&dev->count_lock);
1165 return -EBUSY;
1166 }
1167 atomic_inc(&dev->buf_alloc);
1168 spin_unlock(&dev->count_lock);
1169
Dave Airlie30e2fb12006-02-02 19:37:46 +11001170 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001171 entry = &dma->bufs[order];
1172 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001173 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001174 atomic_dec(&dev->buf_alloc);
1175 return -ENOMEM; /* May only call once for each order */
1176 }
1177
1178 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001179 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001180 atomic_dec(&dev->buf_alloc);
1181 return -EINVAL;
1182 }
1183
1184 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1185 DRM_MEM_BUFS);
1186 if (!entry->buflist) {
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 -ENOMEM;
1190 }
1191 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1192
1193 entry->buf_size = size;
1194 entry->page_order = page_order;
1195
1196 offset = 0;
1197
1198 while (entry->buf_count < count) {
1199 buf = &entry->buflist[entry->buf_count];
1200 buf->idx = dma->buf_count + entry->buf_count;
1201 buf->total = alignment;
1202 buf->order = order;
1203 buf->used = 0;
1204
1205 buf->offset = (dma->byte_count + offset);
1206 buf->bus_address = agp_offset + offset;
1207 buf->address = (void *)(agp_offset + offset);
1208 buf->next = NULL;
1209 buf->waiting = 0;
1210 buf->pending = 0;
1211 init_waitqueue_head(&buf->dma_wait);
1212 buf->filp = NULL;
1213
1214 buf->dev_priv_size = dev->driver->dev_priv_size;
1215 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1216 if (!buf->dev_private) {
1217 /* Set count correctly so we free the proper amount. */
1218 entry->buf_count = count;
1219 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001220 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001221 atomic_dec(&dev->buf_alloc);
1222 return -ENOMEM;
1223 }
1224 memset(buf->dev_private, 0, buf->dev_priv_size);
1225
1226 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1227
1228 offset += alignment;
1229 entry->buf_count++;
1230 byte_count += PAGE_SIZE << page_order;
1231 }
1232
1233 DRM_DEBUG("byte_count: %d\n", byte_count);
1234
1235 temp_buflist = drm_realloc(dma->buflist,
1236 dma->buf_count * sizeof(*dma->buflist),
1237 (dma->buf_count + entry->buf_count)
1238 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1239 if (!temp_buflist) {
1240 /* Free the entry because it isn't valid */
1241 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001242 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001243 atomic_dec(&dev->buf_alloc);
1244 return -ENOMEM;
1245 }
1246 dma->buflist = temp_buflist;
1247
1248 for (i = 0; i < entry->buf_count; i++) {
1249 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1250 }
1251
1252 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001253 dma->seg_count += entry->seg_count;
1254 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001255 dma->byte_count += byte_count;
1256
1257 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1258 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1259
Dave Airlie30e2fb12006-02-02 19:37:46 +11001260 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001261
Dave Airlied59431b2005-07-10 15:00:06 +10001262 request->count = entry->buf_count;
1263 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001264
1265 dma->flags = _DRM_DMA_USE_FB;
1266
1267 atomic_dec(&dev->buf_alloc);
1268 return 0;
1269}
Dave Airlied985c102006-01-02 21:32:48 +11001270
Dave Airlieb84397d62005-07-10 14:46:12 +10001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/**
1273 * Add buffers for DMA transfers (ioctl).
1274 *
1275 * \param inode device inode.
1276 * \param filp file pointer.
1277 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001278 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 * \return zero on success or a negative number on failure.
1280 *
1281 * According with the memory type specified in drm_buf_desc::flags and the
1282 * build options, it dispatches the call either to addbufs_agp(),
1283 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1284 * PCI memory respectively.
1285 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001286int drm_addbufs(struct inode *inode, struct file *filp,
1287 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Dave Airliec60ce622007-07-11 15:27:12 +10001289 struct drm_buf_desc request;
Dave Airlie84b1fd12007-07-11 15:53:27 +10001290 struct drm_file *priv = filp->private_data;
1291 struct drm_device *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001292 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1295 return -EINVAL;
1296
Dave Airliec60ce622007-07-11 15:27:12 +10001297 if (copy_from_user(&request, (struct drm_buf_desc __user *) arg,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EFAULT;
1300
1301#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001302 if (request.flags & _DRM_AGP_BUFFER)
1303 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 else
1305#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001306 if (request.flags & _DRM_SG_BUFFER)
1307 ret = drm_addbufs_sg(dev, &request);
1308 else if (request.flags & _DRM_FB_BUFFER)
1309 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001311 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001312
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001313 if (ret == 0) {
1314 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001315 ret = -EFAULT;
1316 }
1317 }
1318 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321/**
1322 * Get information about the buffer mappings.
1323 *
1324 * This was originally mean for debugging purposes, or by a sophisticated
1325 * client library to determine how best to use the available buffers (e.g.,
1326 * large buffers can be used for image transfer).
1327 *
1328 * \param inode device inode.
1329 * \param filp file pointer.
1330 * \param cmd command.
1331 * \param arg pointer to a drm_buf_info structure.
1332 * \return zero on success or a negative number on failure.
1333 *
1334 * Increments drm_device::buf_use while holding the drm_device::count_lock
1335 * lock, preventing of allocating more buffers after this call. Information
1336 * about each requested buffer is then copied into user space.
1337 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001338int drm_infobufs(struct inode *inode, struct file *filp,
1339 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001341 struct drm_file *priv = filp->private_data;
1342 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001343 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001344 struct drm_buf_info request;
1345 struct drm_buf_info __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int i;
1347 int count;
1348
1349 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1350 return -EINVAL;
1351
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 if (!dma)
1353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001355 spin_lock(&dev->count_lock);
1356 if (atomic_read(&dev->buf_alloc)) {
1357 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EBUSY;
1359 }
1360 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001361 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001363 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EFAULT;
1365
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1367 if (dma->bufs[i].buf_count)
1368 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001371 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001373 if (request.count >= count) {
1374 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1375 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001376 struct drm_buf_desc __user *to =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001377 &request.list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001378 struct drm_buf_entry *from = &dma->bufs[i];
1379 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001380 if (copy_to_user(&to->count,
1381 &from->buf_count,
1382 sizeof(from->buf_count)) ||
1383 copy_to_user(&to->size,
1384 &from->buf_size,
1385 sizeof(from->buf_size)) ||
1386 copy_to_user(&to->low_mark,
1387 &list->low_mark,
1388 sizeof(list->low_mark)) ||
1389 copy_to_user(&to->high_mark,
1390 &list->high_mark,
1391 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EFAULT;
1393
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 DRM_DEBUG("%d %d %d %d %d\n",
1395 i,
1396 dma->bufs[i].buf_count,
1397 dma->bufs[i].buf_size,
1398 dma->bufs[i].freelist.low_mark,
1399 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ++count;
1401 }
1402 }
1403 }
1404 request.count = count;
1405
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return -EFAULT;
1408
1409 return 0;
1410}
1411
1412/**
1413 * Specifies a low and high water mark for buffer allocation
1414 *
1415 * \param inode device inode.
1416 * \param filp file pointer.
1417 * \param cmd command.
1418 * \param arg a pointer to a drm_buf_desc structure.
1419 * \return zero on success or a negative number on failure.
1420 *
1421 * Verifies that the size order is bounded between the admissible orders and
1422 * updates the respective drm_device_dma::bufs entry low and high water mark.
1423 *
1424 * \note This ioctl is deprecated and mostly never used.
1425 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001426int drm_markbufs(struct inode *inode, struct file *filp,
1427 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001429 struct drm_file *priv = filp->private_data;
1430 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001431 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001432 struct drm_buf_desc request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001434 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1437 return -EINVAL;
1438
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 if (!dma)
1440 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001442 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001443 (struct drm_buf_desc __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EFAULT;
1445
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001446 DRM_DEBUG("%d, %d, %d\n",
1447 request.size, request.low_mark, request.high_mark);
1448 order = drm_order(request.size);
1449 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1450 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 entry = &dma->bufs[order];
1452
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001453 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -EINVAL;
1457
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001458 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 entry->freelist.high_mark = request.high_mark;
1460
1461 return 0;
1462}
1463
1464/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001465 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
1467 * \param inode device inode.
1468 * \param filp file pointer.
1469 * \param cmd command.
1470 * \param arg pointer to a drm_buf_free structure.
1471 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * Calls free_buffer() for each used buffer.
1474 * This function is primarily used for debugging.
1475 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001476int drm_freebufs(struct inode *inode, struct file *filp,
1477 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001479 struct drm_file *priv = filp->private_data;
1480 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001481 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001482 struct drm_buf_free request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 int i;
1484 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001485 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1488 return -EINVAL;
1489
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001490 if (!dma)
1491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001494 (struct drm_buf_free __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EFAULT;
1496
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 DRM_DEBUG("%d\n", request.count);
1498 for (i = 0; i < request.count; i++) {
1499 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501 if (idx < 0 || idx >= dma->buf_count) {
1502 DRM_ERROR("Index %d (of %d max)\n",
1503 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return -EINVAL;
1505 }
1506 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001507 if (buf->filp != filp) {
1508 DRM_ERROR("Process %d freeing buffer not owned\n",
1509 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return -EINVAL;
1511 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001512 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514
1515 return 0;
1516}
1517
1518/**
1519 * Maps all of the DMA buffers into client-virtual space (ioctl).
1520 *
1521 * \param inode device inode.
1522 * \param filp file pointer.
1523 * \param cmd command.
1524 * \param arg pointer to a drm_buf_map structure.
1525 * \return zero on success or a negative number on failure.
1526 *
George Sapountzis3417f332006-10-24 12:03:04 -07001527 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1528 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1529 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1530 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001532int drm_mapbufs(struct inode *inode, struct file *filp,
1533 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001535 struct drm_file *priv = filp->private_data;
1536 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001537 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001538 struct drm_buf_map __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int retcode = 0;
1540 const int zero = 0;
1541 unsigned long virtual;
1542 unsigned long address;
Dave Airliec60ce622007-07-11 15:27:12 +10001543 struct drm_buf_map request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int i;
1545
1546 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1547 return -EINVAL;
1548
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001549 if (!dma)
1550 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001552 spin_lock(&dev->count_lock);
1553 if (atomic_read(&dev->buf_alloc)) {
1554 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return -EBUSY;
1556 }
1557 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001558 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return -EFAULT;
1562
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001564 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001566 && (dma->flags & _DRM_DMA_USE_SG))
1567 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1568 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001569 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001570 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001572 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 retcode = -EINVAL;
1574 goto done;
1575 }
1576
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001577 down_write(&current->mm->mmap_sem);
1578 virtual = do_mmap(filp, 0, map->size,
1579 PROT_READ | PROT_WRITE,
1580 MAP_SHARED, token);
1581 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 down_write(&current->mm->mmap_sem);
1584 virtual = do_mmap(filp, 0, dma->byte_count,
1585 PROT_READ | PROT_WRITE,
1586 MAP_SHARED, 0);
1587 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /* Real error */
1591 retcode = (signed long)virtual;
1592 goto done;
1593 }
1594 request.virtual = (void __user *)virtual;
1595
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 for (i = 0; i < dma->buf_count; i++) {
1597 if (copy_to_user(&request.list[i].idx,
1598 &dma->buflist[i]->idx,
1599 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 retcode = -EFAULT;
1601 goto done;
1602 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001603 if (copy_to_user(&request.list[i].total,
1604 &dma->buflist[i]->total,
1605 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 retcode = -EFAULT;
1607 goto done;
1608 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001609 if (copy_to_user(&request.list[i].used,
1610 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 retcode = -EFAULT;
1612 goto done;
1613 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001614 address = virtual + dma->buflist[i]->offset; /* *** */
1615 if (copy_to_user(&request.list[i].address,
1616 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 retcode = -EFAULT;
1618 goto done;
1619 }
1620 }
1621 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001622 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001624 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001626 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return -EFAULT;
1628
1629 return retcode;
1630}
1631
Dave Airlie836cf042005-07-10 19:27:04 +10001632/**
1633 * Compute size order. Returns the exponent of the smaller power of two which
1634 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001635 *
Dave Airlie836cf042005-07-10 19:27:04 +10001636 * \param size size.
1637 * \return order.
1638 *
1639 * \todo Can be made faster.
1640 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001641int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001642{
1643 int order;
1644 unsigned long tmp;
1645
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001646 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001647
1648 if (size & (size - 1))
1649 ++order;
1650
1651 return order;
1652}
1653EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001654
1655