blob: c115b39b85178cc4d84fd9d9b55f5521ccb6d7c5 [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 }
Scott Thompson0769d392007-08-25 18:17:49 +1000180 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100181 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000182 if (!map->handle) {
183 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
184 return -ENOMEM;
185 }
186 }
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100190 list = drm_find_matching_map(dev, map);
191 if (list != NULL) {
192 if(list->map->size != map->size) {
193 DRM_DEBUG("Matching maps of type %d with "
194 "mismatched sizes, (%ld vs %ld)\n",
195 map->type, map->size, list->map->size);
196 list->map->size = map->size;
197 }
198
199 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
200 *maplist = list;
201 return 0;
202 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100203 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204 DRM_DEBUG("%lu %d %p\n",
205 map->size, drm_order(map->size), map->handle);
206 if (!map->handle) {
207 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENOMEM;
209 }
210 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Prevent a 2nd X Server from creating a 2nd lock */
213 if (dev->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 vfree(map->handle);
215 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -EBUSY;
217 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100221 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000222 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100223 int valid = 0;
224
225 if (!drm_core_has_AGP(dev)) {
226 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
227 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100229#ifdef __alpha__
230 map->offset += dev->hose->mem_space->start;
231#endif
232 /* Note: dev->agp->base may actually be 0 when the DRM
233 * is not in control of AGP space. But if user space is
234 * it should already have added the AGP base itself.
235 */
236 map->offset += dev->agp->base;
237 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
238
239 /* This assumes the DRM is in total control of AGP space.
240 * It's not always the case as AGP can be in the control
241 * of user space (i.e. i810 driver). So this loop will get
242 * skipped and we double check that dev->agp->memory is
243 * actually set as well as being invalid before EPERM'ing
244 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000245 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100246 if ((map->offset >= entry->bound) &&
247 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
248 valid = 1;
249 break;
250 }
251 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000252 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100253 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
254 return -EPERM;
255 }
256 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 case _DRM_SCATTER_GATHER:
261 if (!dev->sg) {
262 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
263 return -EINVAL;
264 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000265 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000268 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000269 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000270 * casting it down to 32 bits is no problem, but we
271 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000272 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
273 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000274 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
275 return -ENOMEM;
276 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000277 map->handle = dmah->vaddr;
278 map->offset = (unsigned long)dmah->busaddr;
279 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000280 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return -EINVAL;
284 }
285
286 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000287 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700288 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100289 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
291 return -EINVAL;
292 }
293 memset(list, 0, sizeof(*list));
294 list->map = map;
295
Dave Airlie30e2fb12006-02-02 19:37:46 +1100296 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000297 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000298
Dave Airlied1f2b552005-08-05 22:11:22 +1000299 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100300 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000301 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
302 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000303 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000304 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700305 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100306 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000307 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
308 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
309 mutex_unlock(&dev->struct_mutex);
310 return ret;
311 }
312
Thomas Hellstrom15450852007-02-08 16:14:05 +1100313 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100314 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dave Airlie89625eb2005-09-05 21:23:23 +1000316 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000317 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100318 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000319
Dave Airlie84b1fd12007-07-11 15:53:27 +1000320int drm_addmap(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000321 unsigned int size, enum drm_map_type type,
322 enum drm_map_flags flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000323{
Dave Airlie55910512007-07-11 16:53:40 +1000324 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000325 int rc;
326
327 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
328 if (!rc)
329 *map_ptr = list->map;
330 return rc;
331}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332
Dave Airlie7ab98402005-07-10 16:56:52 +1000333EXPORT_SYMBOL(drm_addmap);
334
335int drm_addmap_ioctl(struct inode *inode, struct file *filp,
336 unsigned int cmd, unsigned long arg)
337{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000338 struct drm_file *priv = filp->private_data;
339 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000340 struct drm_map map;
Dave Airlie55910512007-07-11 16:53:40 +1000341 struct drm_map_list *maplist;
Dave Airliec60ce622007-07-11 15:27:12 +1000342 struct drm_map __user *argp = (void __user *)arg;
Dave Airlie7ab98402005-07-10 16:56:52 +1000343 int err;
344
345 if (!(filp->f_mode & 3))
346 return -EACCES; /* Require read/write */
347
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000348 if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EFAULT;
Dave Airlie7ab98402005-07-10 16:56:52 +1000350 }
351
Dave Airlied985c102006-01-02 21:32:48 +1100352 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
353 return -EPERM;
354
Dave Airlie89625eb2005-09-05 21:23:23 +1000355 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
356 &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000359 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000360
Dave Airliec60ce622007-07-11 15:27:12 +1000361 if (copy_to_user(argp, maplist->map, sizeof(struct drm_map)))
Dave Airlied1f2b552005-08-05 22:11:22 +1000362 return -EFAULT;
Dave Airlie67e1a012005-10-24 18:41:39 +1000363
364 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
365 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
Dave Airlied1f2b552005-08-05 22:11:22 +1000366 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/**
371 * Remove a map private from list and deallocate resources if the mapping
372 * isn't in use.
373 *
374 * \param inode device inode.
375 * \param filp file pointer.
376 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +1000377 * \param arg pointer to a struct drm_map structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * \return zero on success or a negative value on error.
379 *
380 * Searches the map on drm_device::maplist, removes it from the list, see if
381 * its being used, and free any associate resource (such as MTRR's) if it's not
382 * being on use.
383 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000384 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000386int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Dave Airlie55910512007-07-11 16:53:40 +1000388 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000389 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000390 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Dave Airlie836cf042005-07-10 19:27:04 +1000392 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000393 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000394 if (r_list->map == map) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000395 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100396 drm_ht_remove_key(&dev->map_hash,
397 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000398 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
399 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Dave Airlie836cf042005-07-10 19:27:04 +1000403
Dave Airliebd1b3312007-05-26 05:01:51 +1000404 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000405 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000406
407 switch (map->type) {
408 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100409 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000410 /* FALLTHROUGH */
411 case _DRM_FRAME_BUFFER:
412 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
413 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 retcode = mtrr_del(map->mtrr, map->offset, map->size);
415 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000416 }
417 break;
418 case _DRM_SHM:
419 vfree(map->handle);
420 break;
421 case _DRM_AGP:
422 case _DRM_SCATTER_GATHER:
423 break;
424 case _DRM_CONSISTENT:
425 dmah.vaddr = map->handle;
426 dmah.busaddr = map->offset;
427 dmah.size = map->size;
428 __drm_pci_free(dev, &dmah);
429 break;
430 }
431 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
Dave Airlie836cf042005-07-10 19:27:04 +1000435
Dave Airlie84b1fd12007-07-11 15:53:27 +1000436int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000437{
438 int ret;
439
Dave Airlie30e2fb12006-02-02 19:37:46 +1100440 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000441 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100442 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000443
444 return ret;
445}
Dave Airlie7ab98402005-07-10 16:56:52 +1000446
Dave Airlie836cf042005-07-10 19:27:04 +1000447/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
448 * the last close of the device, and this is necessary for cleanup when things
449 * exit uncleanly. Therefore, having userland manually remove mappings seems
450 * like a pointless exercise since they're going away anyway.
451 *
452 * One use case might be after addmap is allowed for normal users for SHM and
453 * gets used by drivers that the server doesn't need to care about. This seems
454 * unlikely.
455 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000456int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
457 unsigned int cmd, unsigned long arg)
458{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000459 struct drm_file *priv = filp->private_data;
460 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000461 struct drm_map request;
Dave Airlie836cf042005-07-10 19:27:04 +1000462 drm_local_map_t *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000463 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000464 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000465
Dave Airliec60ce622007-07-11 15:27:12 +1000466 if (copy_from_user(&request, (struct drm_map __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000467 return -EFAULT;
468 }
469
Dave Airlie30e2fb12006-02-02 19:37:46 +1100470 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000471 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000472 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000474 r_list->map->flags & _DRM_REMOVABLE) {
475 map = r_list->map;
476 break;
477 }
478 }
479
480 /* List has wrapped around to the head pointer, or its empty we didn't
481 * find anything.
482 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000483 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100484 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000485 return -EINVAL;
486 }
487
Dave Airlie836cf042005-07-10 19:27:04 +1000488 /* Register and framebuffer maps are permanent */
489 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100490 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000491 return 0;
492 }
493
494 ret = drm_rmmap_locked(dev, map);
495
Dave Airlie30e2fb12006-02-02 19:37:46 +1100496 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000497
498 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501/**
502 * Cleanup after an error on one of the addbufs() functions.
503 *
Dave Airlie836cf042005-07-10 19:27:04 +1000504 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * \param entry buffer entry where the error occurred.
506 *
507 * Frees any pages and buffers associated with the given entry.
508 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000509static void drm_cleanup_buf_error(struct drm_device * dev,
510 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 int i;
513
514 if (entry->seg_count) {
515 for (i = 0; i < entry->seg_count; i++) {
516 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100517 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 }
520 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000521 entry->seg_count *
522 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 entry->seg_count = 0;
525 }
526
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000527 if (entry->buf_count) {
528 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (entry->buflist[i].dev_private) {
530 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 entry->buflist[i].dev_priv_size,
532 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 }
535 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000536 entry->buf_count *
537 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 entry->buf_count = 0;
540 }
541}
542
543#if __OS_HAS_AGP
544/**
Dave Airlied59431b2005-07-10 15:00:06 +1000545 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000547 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000548 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * After some sanity checks creates a drm_buf structure for each buffer and
552 * reallocates the buffer list of the same size order to accommodate the new
553 * buffers.
554 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000555int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Dave Airliecdd55a22007-07-11 16:32:08 +1000557 struct drm_device_dma *dma = dev->dma;
558 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000559 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000560 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unsigned long offset;
562 unsigned long agp_offset;
563 int count;
564 int order;
565 int size;
566 int alignment;
567 int page_order;
568 int total;
569 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100570 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000571 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 if (!dma)
574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dave Airlied59431b2005-07-10 15:00:06 +1000576 count = request->count;
577 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 size = 1 << order;
579
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 alignment = (request->flags & _DRM_PAGE_ALIGN)
581 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
583 total = PAGE_SIZE << page_order;
584
585 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000586 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 DRM_DEBUG("count: %d\n", count);
589 DRM_DEBUG("order: %d\n", order);
590 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100591 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 DRM_DEBUG("alignment: %d\n", alignment);
593 DRM_DEBUG("page_order: %d\n", page_order);
594 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
597 return -EINVAL;
598 if (dev->queue_count)
599 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Dave Airlie54ba2f72007-02-10 12:07:47 +1100601 /* Make sure buffers are located in AGP memory that we own */
602 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000603 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100604 if ((agp_offset >= agp_entry->bound) &&
605 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
606 valid = 1;
607 break;
608 }
609 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000610 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100611 DRM_DEBUG("zone invalid\n");
612 return -EINVAL;
613 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 spin_lock(&dev->count_lock);
615 if (dev->buf_use) {
616 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EBUSY;
618 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 atomic_inc(&dev->buf_alloc);
620 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Dave Airlie30e2fb12006-02-02 19:37:46 +1100622 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100625 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000626 atomic_dec(&dev->buf_alloc);
627 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100631 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000632 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return -EINVAL;
634 }
635
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
637 DRM_MEM_BUFS);
638 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100639 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -ENOMEM;
642 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 entry->buf_size = size;
646 entry->page_order = page_order;
647
648 offset = 0;
649
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 while (entry->buf_count < count) {
651 buf = &entry->buflist[entry->buf_count];
652 buf->idx = dma->buf_count + entry->buf_count;
653 buf->total = alignment;
654 buf->order = order;
655 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 buf->bus_address = agp_offset + offset;
659 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 buf->waiting = 0;
662 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 init_waitqueue_head(&buf->dma_wait);
664 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
668 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* Set count correctly so we free the proper amount. */
670 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100672 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -ENOMEM;
675 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 offset += alignment;
681 entry->buf_count++;
682 byte_count += PAGE_SIZE << page_order;
683 }
684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 temp_buflist = drm_realloc(dma->buflist,
688 dma->buf_count * sizeof(*dma->buflist),
689 (dma->buf_count + entry->buf_count)
690 * sizeof(*dma->buflist), DRM_MEM_BUFS);
691 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100694 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -ENOMEM;
697 }
698 dma->buflist = temp_buflist;
699
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
702 }
703
704 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100705 dma->seg_count += entry->seg_count;
706 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dma->byte_count += byte_count;
708
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
710 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Dave Airlie30e2fb12006-02-02 19:37:46 +1100712 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Airlied59431b2005-07-10 15:00:06 +1000714 request->count = entry->buf_count;
715 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 dma->flags = _DRM_DMA_USE_AGP;
718
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 0;
721}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722EXPORT_SYMBOL(drm_addbufs_agp);
723#endif /* __OS_HAS_AGP */
724
Dave Airlie84b1fd12007-07-11 15:53:27 +1000725int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Dave Airliecdd55a22007-07-11 16:32:08 +1000727 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int count;
729 int order;
730 int size;
731 int total;
732 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000733 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100734 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000735 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int alignment;
737 unsigned long offset;
738 int i;
739 int byte_count;
740 int page_count;
741 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000742 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
745 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100746
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 if (!dma)
748 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Airlied985c102006-01-02 21:32:48 +1100750 if (!capable(CAP_SYS_ADMIN))
751 return -EPERM;
752
Dave Airlied59431b2005-07-10 15:00:06 +1000753 count = request->count;
754 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 size = 1 << order;
756
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
758 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
761 return -EINVAL;
762 if (dev->queue_count)
763 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Dave Airlied59431b2005-07-10 15:00:06 +1000765 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000766 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
768 total = PAGE_SIZE << page_order;
769
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 spin_lock(&dev->count_lock);
771 if (dev->buf_use) {
772 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EBUSY;
774 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 atomic_inc(&dev->buf_alloc);
776 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Dave Airlie30e2fb12006-02-02 19:37:46 +1100778 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100781 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return -ENOMEM; /* May only call once for each order */
784 }
785
786 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100787 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EINVAL;
790 }
791
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
793 DRM_MEM_BUFS);
794 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100795 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -ENOMEM;
798 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
802 DRM_MEM_SEGS);
803 if (!entry->seglist) {
804 drm_free(entry->buflist,
805 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100806 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -ENOMEM;
809 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* Keep the original pagelist until we know all the allocations
813 * have succeeded
814 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
816 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 drm_free(entry->buflist,
819 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
820 drm_free(entry->seglist,
821 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100822 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -ENOMEM;
825 }
826 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
828 DRM_DEBUG("pagelist: %d entries\n",
829 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 entry->page_order = page_order;
833 byte_count = 0;
834 page_count = 0;
835
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100837
838 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
839
840 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Set count correctly so we free the proper amount. */
842 entry->buf_count = count;
843 entry->seg_count = count;
844 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 drm_free(temp_pagelist,
846 (dma->page_count + (count << page_order))
847 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100848 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return -ENOMEM;
851 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100852 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 for (i = 0; i < (1 << page_order); i++) {
854 DRM_DEBUG("page %d @ 0x%08lx\n",
855 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 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100858 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 for (offset = 0;
861 offset + size <= total && entry->buf_count < count;
862 offset += alignment, ++entry->buf_count) {
863 buf = &entry->buflist[entry->buf_count];
864 buf->idx = dma->buf_count + entry->buf_count;
865 buf->total = alignment;
866 buf->order = order;
867 buf->used = 0;
868 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100869 buf->address = (void *)(dmah->vaddr + offset);
870 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 buf->waiting = 0;
873 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 init_waitqueue_head(&buf->dma_wait);
875 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 buf->dev_private = drm_alloc(buf->dev_priv_size,
879 DRM_MEM_BUFS);
880 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Set count correctly so we free the proper amount. */
882 entry->buf_count = count;
883 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 drm_cleanup_buf_error(dev, entry);
885 drm_free(temp_pagelist,
886 (dma->page_count +
887 (count << page_order))
888 * sizeof(*dma->pagelist),
889 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100890 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -ENOMEM;
893 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 DRM_DEBUG("buffer %d @ %p\n",
897 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 byte_count += PAGE_SIZE << page_order;
900 }
901
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 temp_buflist = drm_realloc(dma->buflist,
903 dma->buf_count * sizeof(*dma->buflist),
904 (dma->buf_count + entry->buf_count)
905 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!temp_buflist) {
907 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 drm_cleanup_buf_error(dev, entry);
909 drm_free(temp_pagelist,
910 (dma->page_count + (count << page_order))
911 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100912 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOMEM;
915 }
916 dma->buflist = temp_buflist;
917
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
920 }
921
922 /* No allocations failed, so now we can replace the orginal pagelist
923 * with the new one.
924 */
925 if (dma->page_count) {
926 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 dma->page_count * sizeof(*dma->pagelist),
928 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 dma->pagelist = temp_pagelist;
931
932 dma->buf_count += entry->buf_count;
933 dma->seg_count += entry->seg_count;
934 dma->page_count += entry->seg_count << page_order;
935 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
936
Dave Airlie30e2fb12006-02-02 19:37:46 +1100937 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dave Airlied59431b2005-07-10 15:00:06 +1000939 request->count = entry->buf_count;
940 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
George Sapountzis3417f332006-10-24 12:03:04 -0700942 if (request->flags & _DRM_PCI_BUFFER_RO)
943 dma->flags = _DRM_DMA_USE_PCI_RO;
944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947
948}
Dave Airlied84f76d2005-07-10 17:04:22 +1000949EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dave Airlie84b1fd12007-07-11 15:53:27 +1000951static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Dave Airliecdd55a22007-07-11 16:32:08 +1000953 struct drm_device_dma *dma = dev->dma;
954 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000955 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned long offset;
957 unsigned long agp_offset;
958 int count;
959 int order;
960 int size;
961 int alignment;
962 int page_order;
963 int total;
964 int byte_count;
965 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000966 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 if (!drm_core_check_feature(dev, DRIVER_SG))
969 return -EINVAL;
970
971 if (!dma)
972 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Airlied985c102006-01-02 21:32:48 +1100974 if (!capable(CAP_SYS_ADMIN))
975 return -EPERM;
976
Dave Airlied59431b2005-07-10 15:00:06 +1000977 count = request->count;
978 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 size = 1 << order;
980
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000981 alignment = (request->flags & _DRM_PAGE_ALIGN)
982 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
984 total = PAGE_SIZE << page_order;
985
986 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000987 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 DRM_DEBUG("count: %d\n", count);
990 DRM_DEBUG("order: %d\n", order);
991 DRM_DEBUG("size: %d\n", size);
992 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
993 DRM_DEBUG("alignment: %d\n", alignment);
994 DRM_DEBUG("page_order: %d\n", page_order);
995 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
998 return -EINVAL;
999 if (dev->queue_count)
1000 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001002 spin_lock(&dev->count_lock);
1003 if (dev->buf_use) {
1004 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EBUSY;
1006 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 atomic_inc(&dev->buf_alloc);
1008 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Dave Airlie30e2fb12006-02-02 19:37:46 +11001010 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001013 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 atomic_dec(&dev->buf_alloc);
1015 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
1018 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001019 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EINVAL;
1022 }
1023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1025 DRM_MEM_BUFS);
1026 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001027 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -ENOMEM;
1030 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 entry->buf_size = size;
1034 entry->page_order = page_order;
1035
1036 offset = 0;
1037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 while (entry->buf_count < count) {
1039 buf = &entry->buflist[entry->buf_count];
1040 buf->idx = dma->buf_count + entry->buf_count;
1041 buf->total = alignment;
1042 buf->order = order;
1043 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001048 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 buf->waiting = 0;
1051 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 init_waitqueue_head(&buf->dma_wait);
1053 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1057 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Set count correctly so we free the proper amount. */
1059 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001061 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return -ENOMEM;
1064 }
1065
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 offset += alignment;
1071 entry->buf_count++;
1072 byte_count += PAGE_SIZE << page_order;
1073 }
1074
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 temp_buflist = drm_realloc(dma->buflist,
1078 dma->buf_count * sizeof(*dma->buflist),
1079 (dma->buf_count + entry->buf_count)
1080 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1081 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001084 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -ENOMEM;
1087 }
1088 dma->buflist = temp_buflist;
1089
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1092 }
1093
1094 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001095 dma->seg_count += entry->seg_count;
1096 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 dma->byte_count += byte_count;
1098
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1100 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Dave Airlie30e2fb12006-02-02 19:37:46 +11001102 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Dave Airlied59431b2005-07-10 15:00:06 +10001104 request->count = entry->buf_count;
1105 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 dma->flags = _DRM_DMA_USE_SG;
1108
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111}
1112
Dave Airlie84b1fd12007-07-11 15:53:27 +10001113static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001114{
Dave Airliecdd55a22007-07-11 16:32:08 +10001115 struct drm_device_dma *dma = dev->dma;
1116 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001117 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001118 unsigned long offset;
1119 unsigned long agp_offset;
1120 int count;
1121 int order;
1122 int size;
1123 int alignment;
1124 int page_order;
1125 int total;
1126 int byte_count;
1127 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001128 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001129
1130 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1131 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132
Dave Airlieb84397d62005-07-10 14:46:12 +10001133 if (!dma)
1134 return -EINVAL;
1135
Dave Airlied985c102006-01-02 21:32:48 +11001136 if (!capable(CAP_SYS_ADMIN))
1137 return -EPERM;
1138
Dave Airlied59431b2005-07-10 15:00:06 +10001139 count = request->count;
1140 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001141 size = 1 << order;
1142
Dave Airlied59431b2005-07-10 15:00:06 +10001143 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001144 ? PAGE_ALIGN(size) : size;
1145 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1146 total = PAGE_SIZE << page_order;
1147
1148 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001149 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001150
1151 DRM_DEBUG("count: %d\n", count);
1152 DRM_DEBUG("order: %d\n", order);
1153 DRM_DEBUG("size: %d\n", size);
1154 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1155 DRM_DEBUG("alignment: %d\n", alignment);
1156 DRM_DEBUG("page_order: %d\n", page_order);
1157 DRM_DEBUG("total: %d\n", total);
1158
1159 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1160 return -EINVAL;
1161 if (dev->queue_count)
1162 return -EBUSY; /* Not while in use */
1163
1164 spin_lock(&dev->count_lock);
1165 if (dev->buf_use) {
1166 spin_unlock(&dev->count_lock);
1167 return -EBUSY;
1168 }
1169 atomic_inc(&dev->buf_alloc);
1170 spin_unlock(&dev->count_lock);
1171
Dave Airlie30e2fb12006-02-02 19:37:46 +11001172 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001173 entry = &dma->bufs[order];
1174 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001175 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001176 atomic_dec(&dev->buf_alloc);
1177 return -ENOMEM; /* May only call once for each order */
1178 }
1179
1180 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001181 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001182 atomic_dec(&dev->buf_alloc);
1183 return -EINVAL;
1184 }
1185
1186 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1187 DRM_MEM_BUFS);
1188 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001189 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001190 atomic_dec(&dev->buf_alloc);
1191 return -ENOMEM;
1192 }
1193 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1194
1195 entry->buf_size = size;
1196 entry->page_order = page_order;
1197
1198 offset = 0;
1199
1200 while (entry->buf_count < count) {
1201 buf = &entry->buflist[entry->buf_count];
1202 buf->idx = dma->buf_count + entry->buf_count;
1203 buf->total = alignment;
1204 buf->order = order;
1205 buf->used = 0;
1206
1207 buf->offset = (dma->byte_count + offset);
1208 buf->bus_address = agp_offset + offset;
1209 buf->address = (void *)(agp_offset + offset);
1210 buf->next = NULL;
1211 buf->waiting = 0;
1212 buf->pending = 0;
1213 init_waitqueue_head(&buf->dma_wait);
1214 buf->filp = NULL;
1215
1216 buf->dev_priv_size = dev->driver->dev_priv_size;
1217 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1218 if (!buf->dev_private) {
1219 /* Set count correctly so we free the proper amount. */
1220 entry->buf_count = count;
1221 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001222 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001223 atomic_dec(&dev->buf_alloc);
1224 return -ENOMEM;
1225 }
1226 memset(buf->dev_private, 0, buf->dev_priv_size);
1227
1228 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1229
1230 offset += alignment;
1231 entry->buf_count++;
1232 byte_count += PAGE_SIZE << page_order;
1233 }
1234
1235 DRM_DEBUG("byte_count: %d\n", byte_count);
1236
1237 temp_buflist = drm_realloc(dma->buflist,
1238 dma->buf_count * sizeof(*dma->buflist),
1239 (dma->buf_count + entry->buf_count)
1240 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1241 if (!temp_buflist) {
1242 /* Free the entry because it isn't valid */
1243 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001244 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001245 atomic_dec(&dev->buf_alloc);
1246 return -ENOMEM;
1247 }
1248 dma->buflist = temp_buflist;
1249
1250 for (i = 0; i < entry->buf_count; i++) {
1251 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1252 }
1253
1254 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001255 dma->seg_count += entry->seg_count;
1256 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001257 dma->byte_count += byte_count;
1258
1259 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1260 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1261
Dave Airlie30e2fb12006-02-02 19:37:46 +11001262 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001263
Dave Airlied59431b2005-07-10 15:00:06 +10001264 request->count = entry->buf_count;
1265 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001266
1267 dma->flags = _DRM_DMA_USE_FB;
1268
1269 atomic_dec(&dev->buf_alloc);
1270 return 0;
1271}
Dave Airlied985c102006-01-02 21:32:48 +11001272
Dave Airlieb84397d62005-07-10 14:46:12 +10001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/**
1275 * Add buffers for DMA transfers (ioctl).
1276 *
1277 * \param inode device inode.
1278 * \param filp file pointer.
1279 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001280 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 * \return zero on success or a negative number on failure.
1282 *
1283 * According with the memory type specified in drm_buf_desc::flags and the
1284 * build options, it dispatches the call either to addbufs_agp(),
1285 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1286 * PCI memory respectively.
1287 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001288int drm_addbufs(struct inode *inode, struct file *filp,
1289 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Dave Airliec60ce622007-07-11 15:27:12 +10001291 struct drm_buf_desc request;
Dave Airlie84b1fd12007-07-11 15:53:27 +10001292 struct drm_file *priv = filp->private_data;
1293 struct drm_device *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001294 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1297 return -EINVAL;
1298
Dave Airliec60ce622007-07-11 15:27:12 +10001299 if (copy_from_user(&request, (struct drm_buf_desc __user *) arg,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001300 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return -EFAULT;
1302
1303#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304 if (request.flags & _DRM_AGP_BUFFER)
1305 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 else
1307#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001308 if (request.flags & _DRM_SG_BUFFER)
1309 ret = drm_addbufs_sg(dev, &request);
1310 else if (request.flags & _DRM_FB_BUFFER)
1311 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001313 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001314
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001315 if (ret == 0) {
1316 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001317 ret = -EFAULT;
1318 }
1319 }
1320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323/**
1324 * Get information about the buffer mappings.
1325 *
1326 * This was originally mean for debugging purposes, or by a sophisticated
1327 * client library to determine how best to use the available buffers (e.g.,
1328 * large buffers can be used for image transfer).
1329 *
1330 * \param inode device inode.
1331 * \param filp file pointer.
1332 * \param cmd command.
1333 * \param arg pointer to a drm_buf_info structure.
1334 * \return zero on success or a negative number on failure.
1335 *
1336 * Increments drm_device::buf_use while holding the drm_device::count_lock
1337 * lock, preventing of allocating more buffers after this call. Information
1338 * about each requested buffer is then copied into user space.
1339 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001340int drm_infobufs(struct inode *inode, struct file *filp,
1341 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001343 struct drm_file *priv = filp->private_data;
1344 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001345 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001346 struct drm_buf_info request;
1347 struct drm_buf_info __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int i;
1349 int count;
1350
1351 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1352 return -EINVAL;
1353
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354 if (!dma)
1355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001357 spin_lock(&dev->count_lock);
1358 if (atomic_read(&dev->buf_alloc)) {
1359 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EBUSY;
1361 }
1362 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001363 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EFAULT;
1367
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1369 if (dma->bufs[i].buf_count)
1370 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001373 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001375 if (request.count >= count) {
1376 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1377 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001378 struct drm_buf_desc __user *to =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001379 &request.list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001380 struct drm_buf_entry *from = &dma->bufs[i];
1381 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 if (copy_to_user(&to->count,
1383 &from->buf_count,
1384 sizeof(from->buf_count)) ||
1385 copy_to_user(&to->size,
1386 &from->buf_size,
1387 sizeof(from->buf_size)) ||
1388 copy_to_user(&to->low_mark,
1389 &list->low_mark,
1390 sizeof(list->low_mark)) ||
1391 copy_to_user(&to->high_mark,
1392 &list->high_mark,
1393 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -EFAULT;
1395
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 DRM_DEBUG("%d %d %d %d %d\n",
1397 i,
1398 dma->bufs[i].buf_count,
1399 dma->bufs[i].buf_size,
1400 dma->bufs[i].freelist.low_mark,
1401 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 ++count;
1403 }
1404 }
1405 }
1406 request.count = count;
1407
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EFAULT;
1410
1411 return 0;
1412}
1413
1414/**
1415 * Specifies a low and high water mark for buffer allocation
1416 *
1417 * \param inode device inode.
1418 * \param filp file pointer.
1419 * \param cmd command.
1420 * \param arg a pointer to a drm_buf_desc structure.
1421 * \return zero on success or a negative number on failure.
1422 *
1423 * Verifies that the size order is bounded between the admissible orders and
1424 * updates the respective drm_device_dma::bufs entry low and high water mark.
1425 *
1426 * \note This ioctl is deprecated and mostly never used.
1427 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001428int drm_markbufs(struct inode *inode, struct file *filp,
1429 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001431 struct drm_file *priv = filp->private_data;
1432 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001433 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001434 struct drm_buf_desc request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001436 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1439 return -EINVAL;
1440
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001441 if (!dma)
1442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001444 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001445 (struct drm_buf_desc __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EFAULT;
1447
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 DRM_DEBUG("%d, %d, %d\n",
1449 request.size, request.low_mark, request.high_mark);
1450 order = drm_order(request.size);
1451 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 entry = &dma->bufs[order];
1454
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001457 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -EINVAL;
1459
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001460 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 entry->freelist.high_mark = request.high_mark;
1462
1463 return 0;
1464}
1465
1466/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001467 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 *
1469 * \param inode device inode.
1470 * \param filp file pointer.
1471 * \param cmd command.
1472 * \param arg pointer to a drm_buf_free structure.
1473 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * Calls free_buffer() for each used buffer.
1476 * This function is primarily used for debugging.
1477 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001478int drm_freebufs(struct inode *inode, struct file *filp,
1479 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001481 struct drm_file *priv = filp->private_data;
1482 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001483 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001484 struct drm_buf_free request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 int i;
1486 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001487 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1490 return -EINVAL;
1491
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001492 if (!dma)
1493 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001495 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001496 (struct drm_buf_free __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -EFAULT;
1498
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001499 DRM_DEBUG("%d\n", request.count);
1500 for (i = 0; i < request.count; i++) {
1501 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 if (idx < 0 || idx >= dma->buf_count) {
1504 DRM_ERROR("Index %d (of %d max)\n",
1505 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EINVAL;
1507 }
1508 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001509 if (buf->filp != filp) {
1510 DRM_ERROR("Process %d freeing buffer not owned\n",
1511 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EINVAL;
1513 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001514 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
1517 return 0;
1518}
1519
1520/**
1521 * Maps all of the DMA buffers into client-virtual space (ioctl).
1522 *
1523 * \param inode device inode.
1524 * \param filp file pointer.
1525 * \param cmd command.
1526 * \param arg pointer to a drm_buf_map structure.
1527 * \return zero on success or a negative number on failure.
1528 *
George Sapountzis3417f332006-10-24 12:03:04 -07001529 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1530 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1531 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1532 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534int drm_mapbufs(struct inode *inode, struct file *filp,
1535 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001537 struct drm_file *priv = filp->private_data;
1538 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001539 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001540 struct drm_buf_map __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 int retcode = 0;
1542 const int zero = 0;
1543 unsigned long virtual;
1544 unsigned long address;
Dave Airliec60ce622007-07-11 15:27:12 +10001545 struct drm_buf_map request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 int i;
1547
1548 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1549 return -EINVAL;
1550
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 if (!dma)
1552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001554 spin_lock(&dev->count_lock);
1555 if (atomic_read(&dev->buf_alloc)) {
1556 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return -EBUSY;
1558 }
1559 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001562 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return -EFAULT;
1564
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001566 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001567 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001568 && (dma->flags & _DRM_DMA_USE_SG))
1569 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1570 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001571 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001572 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001574 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 retcode = -EINVAL;
1576 goto done;
1577 }
1578
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001579 down_write(&current->mm->mmap_sem);
1580 virtual = do_mmap(filp, 0, map->size,
1581 PROT_READ | PROT_WRITE,
1582 MAP_SHARED, token);
1583 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 down_write(&current->mm->mmap_sem);
1586 virtual = do_mmap(filp, 0, dma->byte_count,
1587 PROT_READ | PROT_WRITE,
1588 MAP_SHARED, 0);
1589 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Real error */
1593 retcode = (signed long)virtual;
1594 goto done;
1595 }
1596 request.virtual = (void __user *)virtual;
1597
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001598 for (i = 0; i < dma->buf_count; i++) {
1599 if (copy_to_user(&request.list[i].idx,
1600 &dma->buflist[i]->idx,
1601 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 retcode = -EFAULT;
1603 goto done;
1604 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001605 if (copy_to_user(&request.list[i].total,
1606 &dma->buflist[i]->total,
1607 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 retcode = -EFAULT;
1609 goto done;
1610 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 if (copy_to_user(&request.list[i].used,
1612 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 retcode = -EFAULT;
1614 goto done;
1615 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001616 address = virtual + dma->buflist[i]->offset; /* *** */
1617 if (copy_to_user(&request.list[i].address,
1618 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 retcode = -EFAULT;
1620 goto done;
1621 }
1622 }
1623 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001624 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001626 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001628 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return -EFAULT;
1630
1631 return retcode;
1632}
1633
Dave Airlie836cf042005-07-10 19:27:04 +10001634/**
1635 * Compute size order. Returns the exponent of the smaller power of two which
1636 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001637 *
Dave Airlie836cf042005-07-10 19:27:04 +10001638 * \param size size.
1639 * \return order.
1640 *
1641 * \todo Can be made faster.
1642 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001643int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001644{
1645 int order;
1646 unsigned long tmp;
1647
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001648 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001649
1650 if (size & (size - 1))
1651 ++order;
1652
1653 return order;
1654}
1655EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001656
1657