blob: 923174c54a1cd3150d30cd06f298d130beb20e57 [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 Airlie55910512007-07-11 16:53:40 +100052static struct drm_map_list *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 Airlie55910512007-07-11 16:53:40 +100055 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100056 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 Airliee0be4282007-07-12 10:26:44 +100067static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *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,
Dave Airlie55910512007-07-11 16:53:40 +1000106 enum drm_map_flags flags,
107 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Dave Airliec60ce622007-07-11 15:27:12 +1000109 struct drm_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000110 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000111 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000112 unsigned long user_token;
113 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000115 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
116 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -ENOMEM;
118
Dave Airlie7ab98402005-07-10 16:56:52 +1000119 map->offset = offset;
120 map->size = size;
121 map->flags = flags;
122 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* Only allow shared memory to be removable since we only keep enough
125 * book keeping information about shared memory to allow for removal
126 * when processes fork.
127 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
129 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EINVAL;
131 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
133 map->offset, map->size, map->type);
134 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
135 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EINVAL;
137 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 map->handle = NULL;
140
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 case _DRM_REGISTERS:
143 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000144#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100145 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146 map->offset < virt_to_phys(high_memory)) {
147 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EINVAL;
149 }
150#endif
151#ifdef __alpha__
152 map->offset += dev->hose->mem_space->start;
153#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000154 /* Some drivers preinitialize some maps, without the X Server
155 * needing to be aware of it. Therefore, we just return success
156 * when the server tries to create a duplicate map.
157 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000158 list = drm_find_matching_map(dev, map);
159 if (list != NULL) {
160 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000161 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 "mismatched sizes, (%ld vs %ld)\n",
163 map->type, map->size,
164 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000165 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000166 }
167
168 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000169 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000170 return 0;
171 }
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 if (map->type == _DRM_FRAME_BUFFER ||
175 (map->flags & _DRM_WRITE_COMBINING)) {
176 map->mtrr = mtrr_add(map->offset, map->size,
177 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 }
180 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100181 map->handle = ioremap(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100184 list = drm_find_matching_map(dev, map);
185 if (list != NULL) {
186 if(list->map->size != map->size) {
187 DRM_DEBUG("Matching maps of type %d with "
188 "mismatched sizes, (%ld vs %ld)\n",
189 map->type, map->size, list->map->size);
190 list->map->size = map->size;
191 }
192
193 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
194 *maplist = list;
195 return 0;
196 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100197 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 DRM_DEBUG("%lu %d %p\n",
199 map->size, drm_order(map->size), map->handle);
200 if (!map->handle) {
201 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -ENOMEM;
203 }
204 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000205 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Prevent a 2nd X Server from creating a 2nd lock */
207 if (dev->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 vfree(map->handle);
209 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -EBUSY;
211 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100215 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000216 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100217 int valid = 0;
218
219 if (!drm_core_has_AGP(dev)) {
220 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100223#ifdef __alpha__
224 map->offset += dev->hose->mem_space->start;
225#endif
226 /* Note: dev->agp->base may actually be 0 when the DRM
227 * is not in control of AGP space. But if user space is
228 * it should already have added the AGP base itself.
229 */
230 map->offset += dev->agp->base;
231 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
232
233 /* This assumes the DRM is in total control of AGP space.
234 * It's not always the case as AGP can be in the control
235 * of user space (i.e. i810 driver). So this loop will get
236 * skipped and we double check that dev->agp->memory is
237 * actually set as well as being invalid before EPERM'ing
238 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000239 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100240 if ((map->offset >= entry->bound) &&
241 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
242 valid = 1;
243 break;
244 }
245 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000246 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100247 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
248 return -EPERM;
249 }
250 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 case _DRM_SCATTER_GATHER:
255 if (!dev->sg) {
256 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
257 return -EINVAL;
258 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000259 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000262 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000263 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000264 * casting it down to 32 bits is no problem, but we
265 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000266 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
267 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000268 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
269 return -ENOMEM;
270 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000271 map->handle = dmah->vaddr;
272 map->offset = (unsigned long)dmah->busaddr;
273 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -EINVAL;
278 }
279
280 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700282 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100283 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
285 return -EINVAL;
286 }
287 memset(list, 0, sizeof(*list));
288 list->map = map;
289
Dave Airlie30e2fb12006-02-02 19:37:46 +1100290 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000291 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000292
Dave Airlied1f2b552005-08-05 22:11:22 +1000293 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100294 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000295 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
296 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000297 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000298 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700299 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100300 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000301 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
302 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
303 mutex_unlock(&dev->struct_mutex);
304 return ret;
305 }
306
Thomas Hellstrom15450852007-02-08 16:14:05 +1100307 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100308 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Dave Airlie89625eb2005-09-05 21:23:23 +1000310 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000311 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100312 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000313
Dave Airlie84b1fd12007-07-11 15:53:27 +1000314int drm_addmap(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000315 unsigned int size, enum drm_map_type type,
316 enum drm_map_flags flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000317{
Dave Airlie55910512007-07-11 16:53:40 +1000318 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000319 int rc;
320
321 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
322 if (!rc)
323 *map_ptr = list->map;
324 return rc;
325}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326
Dave Airlie7ab98402005-07-10 16:56:52 +1000327EXPORT_SYMBOL(drm_addmap);
328
329int drm_addmap_ioctl(struct inode *inode, struct file *filp,
330 unsigned int cmd, unsigned long arg)
331{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000332 struct drm_file *priv = filp->private_data;
333 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000334 struct drm_map map;
Dave Airlie55910512007-07-11 16:53:40 +1000335 struct drm_map_list *maplist;
Dave Airliec60ce622007-07-11 15:27:12 +1000336 struct drm_map __user *argp = (void __user *)arg;
Dave Airlie7ab98402005-07-10 16:56:52 +1000337 int err;
338
339 if (!(filp->f_mode & 3))
340 return -EACCES; /* Require read/write */
341
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -EFAULT;
Dave Airlie7ab98402005-07-10 16:56:52 +1000344 }
345
Dave Airlied985c102006-01-02 21:32:48 +1100346 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
347 return -EPERM;
348
Dave Airlie89625eb2005-09-05 21:23:23 +1000349 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
350 &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000351
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000353 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000354
Dave Airliec60ce622007-07-11 15:27:12 +1000355 if (copy_to_user(argp, maplist->map, sizeof(struct drm_map)))
Dave Airlied1f2b552005-08-05 22:11:22 +1000356 return -EFAULT;
Dave Airlie67e1a012005-10-24 18:41:39 +1000357
358 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
359 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
Dave Airlied1f2b552005-08-05 22:11:22 +1000360 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/**
365 * Remove a map private from list and deallocate resources if the mapping
366 * isn't in use.
367 *
368 * \param inode device inode.
369 * \param filp file pointer.
370 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +1000371 * \param arg pointer to a struct drm_map structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * \return zero on success or a negative value on error.
373 *
374 * Searches the map on drm_device::maplist, removes it from the list, see if
375 * its being used, and free any associate resource (such as MTRR's) if it's not
376 * being on use.
377 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000378 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000380int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Dave Airlie55910512007-07-11 16:53:40 +1000382 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000383 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000384 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dave Airlie836cf042005-07-10 19:27:04 +1000386 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000387 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000388 if (r_list->map == map) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000389 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100390 drm_ht_remove_key(&dev->map_hash,
391 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000392 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
393 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Dave Airlie836cf042005-07-10 19:27:04 +1000397
Dave Airliebd1b3312007-05-26 05:01:51 +1000398 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000399 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000400
401 switch (map->type) {
402 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100403 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000404 /* FALLTHROUGH */
405 case _DRM_FRAME_BUFFER:
406 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
407 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 retcode = mtrr_del(map->mtrr, map->offset, map->size);
409 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000410 }
411 break;
412 case _DRM_SHM:
413 vfree(map->handle);
414 break;
415 case _DRM_AGP:
416 case _DRM_SCATTER_GATHER:
417 break;
418 case _DRM_CONSISTENT:
419 dmah.vaddr = map->handle;
420 dmah.busaddr = map->offset;
421 dmah.size = map->size;
422 __drm_pci_free(dev, &dmah);
423 break;
424 }
425 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
Dave Airlie836cf042005-07-10 19:27:04 +1000429
Dave Airlie84b1fd12007-07-11 15:53:27 +1000430int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000431{
432 int ret;
433
Dave Airlie30e2fb12006-02-02 19:37:46 +1100434 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000435 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100436 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000437
438 return ret;
439}
Dave Airlie7ab98402005-07-10 16:56:52 +1000440
Dave Airlie836cf042005-07-10 19:27:04 +1000441/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
442 * the last close of the device, and this is necessary for cleanup when things
443 * exit uncleanly. Therefore, having userland manually remove mappings seems
444 * like a pointless exercise since they're going away anyway.
445 *
446 * One use case might be after addmap is allowed for normal users for SHM and
447 * gets used by drivers that the server doesn't need to care about. This seems
448 * unlikely.
449 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000450int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
451 unsigned int cmd, unsigned long arg)
452{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000453 struct drm_file *priv = filp->private_data;
454 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000455 struct drm_map request;
Dave Airlie836cf042005-07-10 19:27:04 +1000456 drm_local_map_t *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000457 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000458 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000459
Dave Airliec60ce622007-07-11 15:27:12 +1000460 if (copy_from_user(&request, (struct drm_map __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000461 return -EFAULT;
462 }
463
Dave Airlie30e2fb12006-02-02 19:37:46 +1100464 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000465 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000466 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000468 r_list->map->flags & _DRM_REMOVABLE) {
469 map = r_list->map;
470 break;
471 }
472 }
473
474 /* List has wrapped around to the head pointer, or its empty we didn't
475 * find anything.
476 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000477 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100478 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000479 return -EINVAL;
480 }
481
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000482 if (!map) {
483 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000484 return -EINVAL;
Thomas Hellstrom7a3f1f22006-08-07 20:28:29 +1000485 }
Dave Airlie836cf042005-07-10 19:27:04 +1000486
487 /* Register and framebuffer maps are permanent */
488 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100489 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000490 return 0;
491 }
492
493 ret = drm_rmmap_locked(dev, map);
494
Dave Airlie30e2fb12006-02-02 19:37:46 +1100495 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000496
497 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000498}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500/**
501 * Cleanup after an error on one of the addbufs() functions.
502 *
Dave Airlie836cf042005-07-10 19:27:04 +1000503 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * \param entry buffer entry where the error occurred.
505 *
506 * Frees any pages and buffers associated with the given entry.
507 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000508static void drm_cleanup_buf_error(struct drm_device * dev,
509 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int i;
512
513 if (entry->seg_count) {
514 for (i = 0; i < entry->seg_count; i++) {
515 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100516 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 }
519 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 entry->seg_count *
521 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 entry->seg_count = 0;
524 }
525
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000526 if (entry->buf_count) {
527 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (entry->buflist[i].dev_private) {
529 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 entry->buflist[i].dev_priv_size,
531 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 }
534 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000535 entry->buf_count *
536 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 entry->buf_count = 0;
539 }
540}
541
542#if __OS_HAS_AGP
543/**
Dave Airlied59431b2005-07-10 15:00:06 +1000544 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000546 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000547 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * After some sanity checks creates a drm_buf structure for each buffer and
551 * reallocates the buffer list of the same size order to accommodate the new
552 * buffers.
553 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000554int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Dave Airliecdd55a22007-07-11 16:32:08 +1000556 struct drm_device_dma *dma = dev->dma;
557 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000558 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000559 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 unsigned long offset;
561 unsigned long agp_offset;
562 int count;
563 int order;
564 int size;
565 int alignment;
566 int page_order;
567 int total;
568 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100569 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000570 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 if (!dma)
573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Dave Airlied59431b2005-07-10 15:00:06 +1000575 count = request->count;
576 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 size = 1 << order;
578
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 alignment = (request->flags & _DRM_PAGE_ALIGN)
580 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
582 total = PAGE_SIZE << page_order;
583
584 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000585 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 DRM_DEBUG("count: %d\n", count);
588 DRM_DEBUG("order: %d\n", order);
589 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100590 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 DRM_DEBUG("alignment: %d\n", alignment);
592 DRM_DEBUG("page_order: %d\n", page_order);
593 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
596 return -EINVAL;
597 if (dev->queue_count)
598 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Dave Airlie54ba2f72007-02-10 12:07:47 +1100600 /* Make sure buffers are located in AGP memory that we own */
601 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000602 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100603 if ((agp_offset >= agp_entry->bound) &&
604 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
605 valid = 1;
606 break;
607 }
608 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000609 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100610 DRM_DEBUG("zone invalid\n");
611 return -EINVAL;
612 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 spin_lock(&dev->count_lock);
614 if (dev->buf_use) {
615 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -EBUSY;
617 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 atomic_inc(&dev->buf_alloc);
619 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dave Airlie30e2fb12006-02-02 19:37:46 +1100621 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100624 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 atomic_dec(&dev->buf_alloc);
626 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
629 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100630 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -EINVAL;
633 }
634
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
636 DRM_MEM_BUFS);
637 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100638 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return -ENOMEM;
641 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 entry->buf_size = size;
645 entry->page_order = page_order;
646
647 offset = 0;
648
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 while (entry->buf_count < count) {
650 buf = &entry->buflist[entry->buf_count];
651 buf->idx = dma->buf_count + entry->buf_count;
652 buf->total = alignment;
653 buf->order = order;
654 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 buf->bus_address = agp_offset + offset;
658 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 buf->waiting = 0;
661 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 init_waitqueue_head(&buf->dma_wait);
663 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
667 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* Set count correctly so we free the proper amount. */
669 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100671 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000672 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -ENOMEM;
674 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 offset += alignment;
680 entry->buf_count++;
681 byte_count += PAGE_SIZE << page_order;
682 }
683
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 temp_buflist = drm_realloc(dma->buflist,
687 dma->buf_count * sizeof(*dma->buflist),
688 (dma->buf_count + entry->buf_count)
689 * sizeof(*dma->buflist), DRM_MEM_BUFS);
690 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100693 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -ENOMEM;
696 }
697 dma->buflist = temp_buflist;
698
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
701 }
702
703 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100704 dma->seg_count += entry->seg_count;
705 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 dma->byte_count += byte_count;
707
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
709 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Airlie30e2fb12006-02-02 19:37:46 +1100711 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Dave Airlied59431b2005-07-10 15:00:06 +1000713 request->count = entry->buf_count;
714 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 dma->flags = _DRM_DMA_USE_AGP;
717
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721EXPORT_SYMBOL(drm_addbufs_agp);
722#endif /* __OS_HAS_AGP */
723
Dave Airlie84b1fd12007-07-11 15:53:27 +1000724int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Dave Airliecdd55a22007-07-11 16:32:08 +1000726 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int count;
728 int order;
729 int size;
730 int total;
731 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000732 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100733 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000734 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int alignment;
736 unsigned long offset;
737 int i;
738 int byte_count;
739 int page_count;
740 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000741 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
744 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100745
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 if (!dma)
747 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Dave Airlied985c102006-01-02 21:32:48 +1100749 if (!capable(CAP_SYS_ADMIN))
750 return -EPERM;
751
Dave Airlied59431b2005-07-10 15:00:06 +1000752 count = request->count;
753 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 size = 1 << order;
755
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
757 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
760 return -EINVAL;
761 if (dev->queue_count)
762 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Dave Airlied59431b2005-07-10 15:00:06 +1000764 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
767 total = PAGE_SIZE << page_order;
768
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 spin_lock(&dev->count_lock);
770 if (dev->buf_use) {
771 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EBUSY;
773 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 atomic_inc(&dev->buf_alloc);
775 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dave Airlie30e2fb12006-02-02 19:37:46 +1100777 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100780 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -ENOMEM; /* May only call once for each order */
783 }
784
785 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100786 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EINVAL;
789 }
790
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
792 DRM_MEM_BUFS);
793 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100794 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -ENOMEM;
797 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
801 DRM_MEM_SEGS);
802 if (!entry->seglist) {
803 drm_free(entry->buflist,
804 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100805 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -ENOMEM;
808 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Keep the original pagelist until we know all the allocations
812 * have succeeded
813 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
815 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 drm_free(entry->buflist,
818 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
819 drm_free(entry->seglist,
820 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100821 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -ENOMEM;
824 }
825 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
827 DRM_DEBUG("pagelist: %d entries\n",
828 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 entry->page_order = page_order;
832 byte_count = 0;
833 page_count = 0;
834
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100836
837 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
838
839 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* Set count correctly so we free the proper amount. */
841 entry->buf_count = count;
842 entry->seg_count = count;
843 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 drm_free(temp_pagelist,
845 (dma->page_count + (count << page_order))
846 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100847 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -ENOMEM;
850 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100851 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 for (i = 0; i < (1 << page_order); i++) {
853 DRM_DEBUG("page %d @ 0x%08lx\n",
854 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 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100857 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 for (offset = 0;
860 offset + size <= total && entry->buf_count < count;
861 offset += alignment, ++entry->buf_count) {
862 buf = &entry->buflist[entry->buf_count];
863 buf->idx = dma->buf_count + entry->buf_count;
864 buf->total = alignment;
865 buf->order = order;
866 buf->used = 0;
867 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100868 buf->address = (void *)(dmah->vaddr + offset);
869 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 buf->waiting = 0;
872 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000873 init_waitqueue_head(&buf->dma_wait);
874 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 buf->dev_private = drm_alloc(buf->dev_priv_size,
878 DRM_MEM_BUFS);
879 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Set count correctly so we free the proper amount. */
881 entry->buf_count = count;
882 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000883 drm_cleanup_buf_error(dev, entry);
884 drm_free(temp_pagelist,
885 (dma->page_count +
886 (count << page_order))
887 * sizeof(*dma->pagelist),
888 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100889 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -ENOMEM;
892 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 DRM_DEBUG("buffer %d @ %p\n",
896 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 byte_count += PAGE_SIZE << page_order;
899 }
900
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 temp_buflist = drm_realloc(dma->buflist,
902 dma->buf_count * sizeof(*dma->buflist),
903 (dma->buf_count + entry->buf_count)
904 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!temp_buflist) {
906 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 drm_cleanup_buf_error(dev, entry);
908 drm_free(temp_pagelist,
909 (dma->page_count + (count << page_order))
910 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100911 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -ENOMEM;
914 }
915 dma->buflist = temp_buflist;
916
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
919 }
920
921 /* No allocations failed, so now we can replace the orginal pagelist
922 * with the new one.
923 */
924 if (dma->page_count) {
925 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 dma->page_count * sizeof(*dma->pagelist),
927 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 dma->pagelist = temp_pagelist;
930
931 dma->buf_count += entry->buf_count;
932 dma->seg_count += entry->seg_count;
933 dma->page_count += entry->seg_count << page_order;
934 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
935
Dave Airlie30e2fb12006-02-02 19:37:46 +1100936 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dave Airlied59431b2005-07-10 15:00:06 +1000938 request->count = entry->buf_count;
939 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
George Sapountzis3417f332006-10-24 12:03:04 -0700941 if (request->flags & _DRM_PCI_BUFFER_RO)
942 dma->flags = _DRM_DMA_USE_PCI_RO;
943
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946
947}
Dave Airlied84f76d2005-07-10 17:04:22 +1000948EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Dave Airlie84b1fd12007-07-11 15:53:27 +1000950static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Dave Airliecdd55a22007-07-11 16:32:08 +1000952 struct drm_device_dma *dma = dev->dma;
953 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000954 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 unsigned long offset;
956 unsigned long agp_offset;
957 int count;
958 int order;
959 int size;
960 int alignment;
961 int page_order;
962 int total;
963 int byte_count;
964 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000965 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 if (!drm_core_check_feature(dev, DRIVER_SG))
968 return -EINVAL;
969
970 if (!dma)
971 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Dave Airlied985c102006-01-02 21:32:48 +1100973 if (!capable(CAP_SYS_ADMIN))
974 return -EPERM;
975
Dave Airlied59431b2005-07-10 15:00:06 +1000976 count = request->count;
977 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 size = 1 << order;
979
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000980 alignment = (request->flags & _DRM_PAGE_ALIGN)
981 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
983 total = PAGE_SIZE << page_order;
984
985 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000986 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000988 DRM_DEBUG("count: %d\n", count);
989 DRM_DEBUG("order: %d\n", order);
990 DRM_DEBUG("size: %d\n", size);
991 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
992 DRM_DEBUG("alignment: %d\n", alignment);
993 DRM_DEBUG("page_order: %d\n", page_order);
994 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
997 return -EINVAL;
998 if (dev->queue_count)
999 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 spin_lock(&dev->count_lock);
1002 if (dev->buf_use) {
1003 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -EBUSY;
1005 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001006 atomic_inc(&dev->buf_alloc);
1007 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Dave Airlie30e2fb12006-02-02 19:37:46 +11001009 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001012 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 atomic_dec(&dev->buf_alloc);
1014 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
1017 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001018 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -EINVAL;
1021 }
1022
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1024 DRM_MEM_BUFS);
1025 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001026 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return -ENOMEM;
1029 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 entry->buf_size = size;
1033 entry->page_order = page_order;
1034
1035 offset = 0;
1036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 while (entry->buf_count < count) {
1038 buf = &entry->buflist[entry->buf_count];
1039 buf->idx = dma->buf_count + entry->buf_count;
1040 buf->total = alignment;
1041 buf->order = order;
1042 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001047 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 buf->waiting = 0;
1050 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 init_waitqueue_head(&buf->dma_wait);
1052 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1056 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* Set count correctly so we free the proper amount. */
1058 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001060 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -ENOMEM;
1063 }
1064
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 offset += alignment;
1070 entry->buf_count++;
1071 byte_count += PAGE_SIZE << page_order;
1072 }
1073
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 temp_buflist = drm_realloc(dma->buflist,
1077 dma->buf_count * sizeof(*dma->buflist),
1078 (dma->buf_count + entry->buf_count)
1079 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1080 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001083 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -ENOMEM;
1086 }
1087 dma->buflist = temp_buflist;
1088
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1091 }
1092
1093 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001094 dma->seg_count += entry->seg_count;
1095 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 dma->byte_count += byte_count;
1097
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1099 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Dave Airlie30e2fb12006-02-02 19:37:46 +11001101 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dave Airlied59431b2005-07-10 15:00:06 +10001103 request->count = entry->buf_count;
1104 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 dma->flags = _DRM_DMA_USE_SG;
1107
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110}
1111
Dave Airlie84b1fd12007-07-11 15:53:27 +10001112static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001113{
Dave Airliecdd55a22007-07-11 16:32:08 +10001114 struct drm_device_dma *dma = dev->dma;
1115 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001116 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001117 unsigned long offset;
1118 unsigned long agp_offset;
1119 int count;
1120 int order;
1121 int size;
1122 int alignment;
1123 int page_order;
1124 int total;
1125 int byte_count;
1126 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001127 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001128
1129 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1130 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131
Dave Airlieb84397d62005-07-10 14:46:12 +10001132 if (!dma)
1133 return -EINVAL;
1134
Dave Airlied985c102006-01-02 21:32:48 +11001135 if (!capable(CAP_SYS_ADMIN))
1136 return -EPERM;
1137
Dave Airlied59431b2005-07-10 15:00:06 +10001138 count = request->count;
1139 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001140 size = 1 << order;
1141
Dave Airlied59431b2005-07-10 15:00:06 +10001142 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001143 ? PAGE_ALIGN(size) : size;
1144 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1145 total = PAGE_SIZE << page_order;
1146
1147 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001148 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001149
1150 DRM_DEBUG("count: %d\n", count);
1151 DRM_DEBUG("order: %d\n", order);
1152 DRM_DEBUG("size: %d\n", size);
1153 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1154 DRM_DEBUG("alignment: %d\n", alignment);
1155 DRM_DEBUG("page_order: %d\n", page_order);
1156 DRM_DEBUG("total: %d\n", total);
1157
1158 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1159 return -EINVAL;
1160 if (dev->queue_count)
1161 return -EBUSY; /* Not while in use */
1162
1163 spin_lock(&dev->count_lock);
1164 if (dev->buf_use) {
1165 spin_unlock(&dev->count_lock);
1166 return -EBUSY;
1167 }
1168 atomic_inc(&dev->buf_alloc);
1169 spin_unlock(&dev->count_lock);
1170
Dave Airlie30e2fb12006-02-02 19:37:46 +11001171 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001172 entry = &dma->bufs[order];
1173 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001174 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001175 atomic_dec(&dev->buf_alloc);
1176 return -ENOMEM; /* May only call once for each order */
1177 }
1178
1179 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001180 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001181 atomic_dec(&dev->buf_alloc);
1182 return -EINVAL;
1183 }
1184
1185 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1186 DRM_MEM_BUFS);
1187 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001188 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001189 atomic_dec(&dev->buf_alloc);
1190 return -ENOMEM;
1191 }
1192 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1193
1194 entry->buf_size = size;
1195 entry->page_order = page_order;
1196
1197 offset = 0;
1198
1199 while (entry->buf_count < count) {
1200 buf = &entry->buflist[entry->buf_count];
1201 buf->idx = dma->buf_count + entry->buf_count;
1202 buf->total = alignment;
1203 buf->order = order;
1204 buf->used = 0;
1205
1206 buf->offset = (dma->byte_count + offset);
1207 buf->bus_address = agp_offset + offset;
1208 buf->address = (void *)(agp_offset + offset);
1209 buf->next = NULL;
1210 buf->waiting = 0;
1211 buf->pending = 0;
1212 init_waitqueue_head(&buf->dma_wait);
1213 buf->filp = NULL;
1214
1215 buf->dev_priv_size = dev->driver->dev_priv_size;
1216 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1217 if (!buf->dev_private) {
1218 /* Set count correctly so we free the proper amount. */
1219 entry->buf_count = count;
1220 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001221 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001222 atomic_dec(&dev->buf_alloc);
1223 return -ENOMEM;
1224 }
1225 memset(buf->dev_private, 0, buf->dev_priv_size);
1226
1227 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1228
1229 offset += alignment;
1230 entry->buf_count++;
1231 byte_count += PAGE_SIZE << page_order;
1232 }
1233
1234 DRM_DEBUG("byte_count: %d\n", byte_count);
1235
1236 temp_buflist = drm_realloc(dma->buflist,
1237 dma->buf_count * sizeof(*dma->buflist),
1238 (dma->buf_count + entry->buf_count)
1239 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1240 if (!temp_buflist) {
1241 /* Free the entry because it isn't valid */
1242 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001243 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001244 atomic_dec(&dev->buf_alloc);
1245 return -ENOMEM;
1246 }
1247 dma->buflist = temp_buflist;
1248
1249 for (i = 0; i < entry->buf_count; i++) {
1250 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1251 }
1252
1253 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001254 dma->seg_count += entry->seg_count;
1255 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001256 dma->byte_count += byte_count;
1257
1258 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1259 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1260
Dave Airlie30e2fb12006-02-02 19:37:46 +11001261 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001262
Dave Airlied59431b2005-07-10 15:00:06 +10001263 request->count = entry->buf_count;
1264 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001265
1266 dma->flags = _DRM_DMA_USE_FB;
1267
1268 atomic_dec(&dev->buf_alloc);
1269 return 0;
1270}
Dave Airlied985c102006-01-02 21:32:48 +11001271
Dave Airlieb84397d62005-07-10 14:46:12 +10001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/**
1274 * Add buffers for DMA transfers (ioctl).
1275 *
1276 * \param inode device inode.
1277 * \param filp file pointer.
1278 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001279 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * \return zero on success or a negative number on failure.
1281 *
1282 * According with the memory type specified in drm_buf_desc::flags and the
1283 * build options, it dispatches the call either to addbufs_agp(),
1284 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1285 * PCI memory respectively.
1286 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001287int drm_addbufs(struct inode *inode, struct file *filp,
1288 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Dave Airliec60ce622007-07-11 15:27:12 +10001290 struct drm_buf_desc request;
Dave Airlie84b1fd12007-07-11 15:53:27 +10001291 struct drm_file *priv = filp->private_data;
1292 struct drm_device *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001293 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1296 return -EINVAL;
1297
Dave Airliec60ce622007-07-11 15:27:12 +10001298 if (copy_from_user(&request, (struct drm_buf_desc __user *) arg,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001299 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EFAULT;
1301
1302#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001303 if (request.flags & _DRM_AGP_BUFFER)
1304 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 else
1306#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 if (request.flags & _DRM_SG_BUFFER)
1308 ret = drm_addbufs_sg(dev, &request);
1309 else if (request.flags & _DRM_FB_BUFFER)
1310 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001313
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 if (ret == 0) {
1315 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001316 ret = -EFAULT;
1317 }
1318 }
1319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322/**
1323 * Get information about the buffer mappings.
1324 *
1325 * This was originally mean for debugging purposes, or by a sophisticated
1326 * client library to determine how best to use the available buffers (e.g.,
1327 * large buffers can be used for image transfer).
1328 *
1329 * \param inode device inode.
1330 * \param filp file pointer.
1331 * \param cmd command.
1332 * \param arg pointer to a drm_buf_info structure.
1333 * \return zero on success or a negative number on failure.
1334 *
1335 * Increments drm_device::buf_use while holding the drm_device::count_lock
1336 * lock, preventing of allocating more buffers after this call. Information
1337 * about each requested buffer is then copied into user space.
1338 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001339int drm_infobufs(struct inode *inode, struct file *filp,
1340 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001342 struct drm_file *priv = filp->private_data;
1343 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001344 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001345 struct drm_buf_info request;
1346 struct drm_buf_info __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int i;
1348 int count;
1349
1350 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1351 return -EINVAL;
1352
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001353 if (!dma)
1354 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356 spin_lock(&dev->count_lock);
1357 if (atomic_read(&dev->buf_alloc)) {
1358 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EBUSY;
1360 }
1361 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001364 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EFAULT;
1366
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001367 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1368 if (dma->bufs[i].buf_count)
1369 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001372 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001374 if (request.count >= count) {
1375 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1376 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001377 struct drm_buf_desc __user *to =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 &request.list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001379 struct drm_buf_entry *from = &dma->bufs[i];
1380 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 if (copy_to_user(&to->count,
1382 &from->buf_count,
1383 sizeof(from->buf_count)) ||
1384 copy_to_user(&to->size,
1385 &from->buf_size,
1386 sizeof(from->buf_size)) ||
1387 copy_to_user(&to->low_mark,
1388 &list->low_mark,
1389 sizeof(list->low_mark)) ||
1390 copy_to_user(&to->high_mark,
1391 &list->high_mark,
1392 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EFAULT;
1394
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001395 DRM_DEBUG("%d %d %d %d %d\n",
1396 i,
1397 dma->bufs[i].buf_count,
1398 dma->bufs[i].buf_size,
1399 dma->bufs[i].freelist.low_mark,
1400 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ++count;
1402 }
1403 }
1404 }
1405 request.count = count;
1406
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001407 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return -EFAULT;
1409
1410 return 0;
1411}
1412
1413/**
1414 * Specifies a low and high water mark for buffer allocation
1415 *
1416 * \param inode device inode.
1417 * \param filp file pointer.
1418 * \param cmd command.
1419 * \param arg a pointer to a drm_buf_desc structure.
1420 * \return zero on success or a negative number on failure.
1421 *
1422 * Verifies that the size order is bounded between the admissible orders and
1423 * updates the respective drm_device_dma::bufs entry low and high water mark.
1424 *
1425 * \note This ioctl is deprecated and mostly never used.
1426 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001427int drm_markbufs(struct inode *inode, struct file *filp,
1428 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001430 struct drm_file *priv = filp->private_data;
1431 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001432 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001433 struct drm_buf_desc request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001435 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1438 return -EINVAL;
1439
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001440 if (!dma)
1441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001444 (struct drm_buf_desc __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EFAULT;
1446
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 DRM_DEBUG("%d, %d, %d\n",
1448 request.size, request.low_mark, request.high_mark);
1449 order = drm_order(request.size);
1450 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 entry = &dma->bufs[order];
1453
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001456 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return -EINVAL;
1458
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 entry->freelist.high_mark = request.high_mark;
1461
1462 return 0;
1463}
1464
1465/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 *
1468 * \param inode device inode.
1469 * \param filp file pointer.
1470 * \param cmd command.
1471 * \param arg pointer to a drm_buf_free structure.
1472 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001473 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * Calls free_buffer() for each used buffer.
1475 * This function is primarily used for debugging.
1476 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001477int drm_freebufs(struct inode *inode, struct file *filp,
1478 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001480 struct drm_file *priv = filp->private_data;
1481 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001482 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001483 struct drm_buf_free request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 int i;
1485 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001486 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1489 return -EINVAL;
1490
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001491 if (!dma)
1492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001495 (struct drm_buf_free __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return -EFAULT;
1497
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001498 DRM_DEBUG("%d\n", request.count);
1499 for (i = 0; i < request.count; i++) {
1500 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001502 if (idx < 0 || idx >= dma->buf_count) {
1503 DRM_ERROR("Index %d (of %d max)\n",
1504 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EINVAL;
1506 }
1507 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 if (buf->filp != filp) {
1509 DRM_ERROR("Process %d freeing buffer not owned\n",
1510 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EINVAL;
1512 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001513 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515
1516 return 0;
1517}
1518
1519/**
1520 * Maps all of the DMA buffers into client-virtual space (ioctl).
1521 *
1522 * \param inode device inode.
1523 * \param filp file pointer.
1524 * \param cmd command.
1525 * \param arg pointer to a drm_buf_map structure.
1526 * \return zero on success or a negative number on failure.
1527 *
George Sapountzis3417f332006-10-24 12:03:04 -07001528 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1529 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1530 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1531 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533int drm_mapbufs(struct inode *inode, struct file *filp,
1534 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001536 struct drm_file *priv = filp->private_data;
1537 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001538 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001539 struct drm_buf_map __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int retcode = 0;
1541 const int zero = 0;
1542 unsigned long virtual;
1543 unsigned long address;
Dave Airliec60ce622007-07-11 15:27:12 +10001544 struct drm_buf_map request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int i;
1546
1547 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1548 return -EINVAL;
1549
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001550 if (!dma)
1551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001553 spin_lock(&dev->count_lock);
1554 if (atomic_read(&dev->buf_alloc)) {
1555 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return -EBUSY;
1557 }
1558 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001559 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001561 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return -EFAULT;
1563
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001565 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001566 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001567 && (dma->flags & _DRM_DMA_USE_SG))
1568 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1569 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001570 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001571 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001573 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 retcode = -EINVAL;
1575 goto done;
1576 }
1577
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 down_write(&current->mm->mmap_sem);
1579 virtual = do_mmap(filp, 0, map->size,
1580 PROT_READ | PROT_WRITE,
1581 MAP_SHARED, token);
1582 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584 down_write(&current->mm->mmap_sem);
1585 virtual = do_mmap(filp, 0, dma->byte_count,
1586 PROT_READ | PROT_WRITE,
1587 MAP_SHARED, 0);
1588 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001590 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /* Real error */
1592 retcode = (signed long)virtual;
1593 goto done;
1594 }
1595 request.virtual = (void __user *)virtual;
1596
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001597 for (i = 0; i < dma->buf_count; i++) {
1598 if (copy_to_user(&request.list[i].idx,
1599 &dma->buflist[i]->idx,
1600 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 retcode = -EFAULT;
1602 goto done;
1603 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001604 if (copy_to_user(&request.list[i].total,
1605 &dma->buflist[i]->total,
1606 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 retcode = -EFAULT;
1608 goto done;
1609 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001610 if (copy_to_user(&request.list[i].used,
1611 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 retcode = -EFAULT;
1613 goto done;
1614 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001615 address = virtual + dma->buflist[i]->offset; /* *** */
1616 if (copy_to_user(&request.list[i].address,
1617 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 retcode = -EFAULT;
1619 goto done;
1620 }
1621 }
1622 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001623 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001625 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001627 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return -EFAULT;
1629
1630 return retcode;
1631}
1632
Dave Airlie836cf042005-07-10 19:27:04 +10001633/**
1634 * Compute size order. Returns the exponent of the smaller power of two which
1635 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001636 *
Dave Airlie836cf042005-07-10 19:27:04 +10001637 * \param size size.
1638 * \return order.
1639 *
1640 * \todo Can be made faster.
1641 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001642int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001643{
1644 int order;
1645 unsigned long tmp;
1646
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001647 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001648
1649 if (size & (size - 1))
1650 ++order;
1651
1652 return order;
1653}
1654EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001655
1656