blob: e7253874fa8fd4771d90dbec2b6eac2965baa7c1 [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.
Eric Anholt6c340ea2007-08-25 20:23:09 +100095 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * \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
Eric Anholt6c340ea2007-08-25 20:23:09 +1000335int drm_addmap_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlie7ab98402005-07-10 16:56:52 +1000336 unsigned int cmd, unsigned long arg)
337{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000338 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000339 struct drm_map map;
Dave Airlie55910512007-07-11 16:53:40 +1000340 struct drm_map_list *maplist;
Dave Airliec60ce622007-07-11 15:27:12 +1000341 struct drm_map __user *argp = (void __user *)arg;
Dave Airlie7ab98402005-07-10 16:56:52 +1000342 int err;
343
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -EFAULT;
Dave Airlie7ab98402005-07-10 16:56:52 +1000346 }
347
Dave Airlied985c102006-01-02 21:32:48 +1100348 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
349 return -EPERM;
350
Dave Airlie89625eb2005-09-05 21:23:23 +1000351 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
352 &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000353
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000355 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000356
Dave Airliec60ce622007-07-11 15:27:12 +1000357 if (copy_to_user(argp, maplist->map, sizeof(struct drm_map)))
Dave Airlied1f2b552005-08-05 22:11:22 +1000358 return -EFAULT;
Dave Airlie67e1a012005-10-24 18:41:39 +1000359
360 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
361 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
Dave Airlied1f2b552005-08-05 22:11:22 +1000362 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/**
367 * Remove a map private from list and deallocate resources if the mapping
368 * isn't in use.
369 *
370 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000371 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +1000373 * \param arg pointer to a struct drm_map structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * \return zero on success or a negative value on error.
375 *
376 * Searches the map on drm_device::maplist, removes it from the list, see if
377 * its being used, and free any associate resource (such as MTRR's) if it's not
378 * being on use.
379 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000380 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000382int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Dave Airlie55910512007-07-11 16:53:40 +1000384 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000385 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000386 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Dave Airlie836cf042005-07-10 19:27:04 +1000388 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000389 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000390 if (r_list->map == map) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000391 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100392 drm_ht_remove_key(&dev->map_hash,
393 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000394 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
395 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Dave Airlie836cf042005-07-10 19:27:04 +1000399
Dave Airliebd1b3312007-05-26 05:01:51 +1000400 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000401 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000402
403 switch (map->type) {
404 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100405 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000406 /* FALLTHROUGH */
407 case _DRM_FRAME_BUFFER:
408 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
409 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 retcode = mtrr_del(map->mtrr, map->offset, map->size);
411 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000412 }
413 break;
414 case _DRM_SHM:
415 vfree(map->handle);
416 break;
417 case _DRM_AGP:
418 case _DRM_SCATTER_GATHER:
419 break;
420 case _DRM_CONSISTENT:
421 dmah.vaddr = map->handle;
422 dmah.busaddr = map->offset;
423 dmah.size = map->size;
424 __drm_pci_free(dev, &dmah);
425 break;
426 }
427 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
Dave Airlie836cf042005-07-10 19:27:04 +1000431
Dave Airlie84b1fd12007-07-11 15:53:27 +1000432int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000433{
434 int ret;
435
Dave Airlie30e2fb12006-02-02 19:37:46 +1100436 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000437 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100438 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000439
440 return ret;
441}
Dave Airlie7ab98402005-07-10 16:56:52 +1000442
Dave Airlie836cf042005-07-10 19:27:04 +1000443/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
444 * the last close of the device, and this is necessary for cleanup when things
445 * exit uncleanly. Therefore, having userland manually remove mappings seems
446 * like a pointless exercise since they're going away anyway.
447 *
448 * One use case might be after addmap is allowed for normal users for SHM and
449 * gets used by drivers that the server doesn't need to care about. This seems
450 * unlikely.
451 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000452int drm_rmmap_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlie7ab98402005-07-10 16:56:52 +1000453 unsigned int cmd, unsigned long arg)
454{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000455 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000456 struct drm_map request;
Dave Airlie836cf042005-07-10 19:27:04 +1000457 drm_local_map_t *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000458 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000459 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000460
Dave Airliec60ce622007-07-11 15:27:12 +1000461 if (copy_from_user(&request, (struct drm_map __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000462 return -EFAULT;
463 }
464
Dave Airlie30e2fb12006-02-02 19:37:46 +1100465 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000466 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000467 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000469 r_list->map->flags & _DRM_REMOVABLE) {
470 map = r_list->map;
471 break;
472 }
473 }
474
475 /* List has wrapped around to the head pointer, or its empty we didn't
476 * find anything.
477 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000478 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100479 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000480 return -EINVAL;
481 }
482
Dave Airlie836cf042005-07-10 19:27:04 +1000483 /* Register and framebuffer maps are permanent */
484 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100485 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000486 return 0;
487 }
488
489 ret = drm_rmmap_locked(dev, map);
490
Dave Airlie30e2fb12006-02-02 19:37:46 +1100491 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000492
493 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496/**
497 * Cleanup after an error on one of the addbufs() functions.
498 *
Dave Airlie836cf042005-07-10 19:27:04 +1000499 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * \param entry buffer entry where the error occurred.
501 *
502 * Frees any pages and buffers associated with the given entry.
503 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000504static void drm_cleanup_buf_error(struct drm_device * dev,
505 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int i;
508
509 if (entry->seg_count) {
510 for (i = 0; i < entry->seg_count; i++) {
511 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100512 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 }
515 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 entry->seg_count *
517 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 entry->seg_count = 0;
520 }
521
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000522 if (entry->buf_count) {
523 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (entry->buflist[i].dev_private) {
525 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000526 entry->buflist[i].dev_priv_size,
527 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 }
530 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 entry->buf_count *
532 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 entry->buf_count = 0;
535 }
536}
537
538#if __OS_HAS_AGP
539/**
Dave Airlied59431b2005-07-10 15:00:06 +1000540 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000542 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000543 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * After some sanity checks creates a drm_buf structure for each buffer and
547 * reallocates the buffer list of the same size order to accommodate the new
548 * buffers.
549 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000550int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Dave Airliecdd55a22007-07-11 16:32:08 +1000552 struct drm_device_dma *dma = dev->dma;
553 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000554 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000555 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 unsigned long offset;
557 unsigned long agp_offset;
558 int count;
559 int order;
560 int size;
561 int alignment;
562 int page_order;
563 int total;
564 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100565 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000566 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 if (!dma)
569 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dave Airlied59431b2005-07-10 15:00:06 +1000571 count = request->count;
572 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 size = 1 << order;
574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 alignment = (request->flags & _DRM_PAGE_ALIGN)
576 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
578 total = PAGE_SIZE << page_order;
579
580 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000581 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 DRM_DEBUG("count: %d\n", count);
584 DRM_DEBUG("order: %d\n", order);
585 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100586 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 DRM_DEBUG("alignment: %d\n", alignment);
588 DRM_DEBUG("page_order: %d\n", page_order);
589 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
592 return -EINVAL;
593 if (dev->queue_count)
594 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dave Airlie54ba2f72007-02-10 12:07:47 +1100596 /* Make sure buffers are located in AGP memory that we own */
597 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000598 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100599 if ((agp_offset >= agp_entry->bound) &&
600 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
601 valid = 1;
602 break;
603 }
604 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000605 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100606 DRM_DEBUG("zone invalid\n");
607 return -EINVAL;
608 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 spin_lock(&dev->count_lock);
610 if (dev->buf_use) {
611 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -EBUSY;
613 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 atomic_inc(&dev->buf_alloc);
615 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Airlie30e2fb12006-02-02 19:37:46 +1100617 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100620 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 atomic_dec(&dev->buf_alloc);
622 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100626 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000627 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EINVAL;
629 }
630
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
632 DRM_MEM_BUFS);
633 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100634 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -ENOMEM;
637 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 entry->buf_size = size;
641 entry->page_order = page_order;
642
643 offset = 0;
644
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 while (entry->buf_count < count) {
646 buf = &entry->buflist[entry->buf_count];
647 buf->idx = dma->buf_count + entry->buf_count;
648 buf->total = alignment;
649 buf->order = order;
650 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 buf->bus_address = agp_offset + offset;
654 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 buf->waiting = 0;
657 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000659 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
663 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* Set count correctly so we free the proper amount. */
665 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100667 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -ENOMEM;
670 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 offset += alignment;
676 entry->buf_count++;
677 byte_count += PAGE_SIZE << page_order;
678 }
679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 temp_buflist = drm_realloc(dma->buflist,
683 dma->buf_count * sizeof(*dma->buflist),
684 (dma->buf_count + entry->buf_count)
685 * sizeof(*dma->buflist), DRM_MEM_BUFS);
686 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100689 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -ENOMEM;
692 }
693 dma->buflist = temp_buflist;
694
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
697 }
698
699 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100700 dma->seg_count += entry->seg_count;
701 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 dma->byte_count += byte_count;
703
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
705 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Dave Airlie30e2fb12006-02-02 19:37:46 +1100707 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Dave Airlied59431b2005-07-10 15:00:06 +1000709 request->count = entry->buf_count;
710 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 dma->flags = _DRM_DMA_USE_AGP;
713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 0;
716}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717EXPORT_SYMBOL(drm_addbufs_agp);
718#endif /* __OS_HAS_AGP */
719
Dave Airlie84b1fd12007-07-11 15:53:27 +1000720int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Dave Airliecdd55a22007-07-11 16:32:08 +1000722 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int count;
724 int order;
725 int size;
726 int total;
727 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000728 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100729 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000730 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int alignment;
732 unsigned long offset;
733 int i;
734 int byte_count;
735 int page_count;
736 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000737 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
740 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100741
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 if (!dma)
743 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Airlied985c102006-01-02 21:32:48 +1100745 if (!capable(CAP_SYS_ADMIN))
746 return -EPERM;
747
Dave Airlied59431b2005-07-10 15:00:06 +1000748 count = request->count;
749 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 size = 1 << order;
751
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000752 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
753 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
756 return -EINVAL;
757 if (dev->queue_count)
758 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Airlied59431b2005-07-10 15:00:06 +1000760 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
763 total = PAGE_SIZE << page_order;
764
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 spin_lock(&dev->count_lock);
766 if (dev->buf_use) {
767 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -EBUSY;
769 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 atomic_inc(&dev->buf_alloc);
771 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Dave Airlie30e2fb12006-02-02 19:37:46 +1100773 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100776 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -ENOMEM; /* May only call once for each order */
779 }
780
781 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100782 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EINVAL;
785 }
786
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
788 DRM_MEM_BUFS);
789 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100790 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -ENOMEM;
793 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
797 DRM_MEM_SEGS);
798 if (!entry->seglist) {
799 drm_free(entry->buflist,
800 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100801 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -ENOMEM;
804 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /* Keep the original pagelist until we know all the allocations
808 * have succeeded
809 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
811 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 drm_free(entry->buflist,
814 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
815 drm_free(entry->seglist,
816 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100817 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -ENOMEM;
820 }
821 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
823 DRM_DEBUG("pagelist: %d entries\n",
824 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 entry->page_order = page_order;
828 byte_count = 0;
829 page_count = 0;
830
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100832
833 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
834
835 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Set count correctly so we free the proper amount. */
837 entry->buf_count = count;
838 entry->seg_count = count;
839 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 drm_free(temp_pagelist,
841 (dma->page_count + (count << page_order))
842 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100843 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -ENOMEM;
846 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100847 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 for (i = 0; i < (1 << page_order); i++) {
849 DRM_DEBUG("page %d @ 0x%08lx\n",
850 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100851 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100853 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 for (offset = 0;
856 offset + size <= total && entry->buf_count < count;
857 offset += alignment, ++entry->buf_count) {
858 buf = &entry->buflist[entry->buf_count];
859 buf->idx = dma->buf_count + entry->buf_count;
860 buf->total = alignment;
861 buf->order = order;
862 buf->used = 0;
863 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100864 buf->address = (void *)(dmah->vaddr + offset);
865 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 buf->waiting = 0;
868 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000870 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000873 buf->dev_private = drm_alloc(buf->dev_priv_size,
874 DRM_MEM_BUFS);
875 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* Set count correctly so we free the proper amount. */
877 entry->buf_count = count;
878 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 drm_cleanup_buf_error(dev, entry);
880 drm_free(temp_pagelist,
881 (dma->page_count +
882 (count << page_order))
883 * sizeof(*dma->pagelist),
884 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100885 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -ENOMEM;
888 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 DRM_DEBUG("buffer %d @ %p\n",
892 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 byte_count += PAGE_SIZE << page_order;
895 }
896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 temp_buflist = drm_realloc(dma->buflist,
898 dma->buf_count * sizeof(*dma->buflist),
899 (dma->buf_count + entry->buf_count)
900 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (!temp_buflist) {
902 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 drm_cleanup_buf_error(dev, entry);
904 drm_free(temp_pagelist,
905 (dma->page_count + (count << page_order))
906 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100907 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -ENOMEM;
910 }
911 dma->buflist = temp_buflist;
912
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
915 }
916
917 /* No allocations failed, so now we can replace the orginal pagelist
918 * with the new one.
919 */
920 if (dma->page_count) {
921 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 dma->page_count * sizeof(*dma->pagelist),
923 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925 dma->pagelist = temp_pagelist;
926
927 dma->buf_count += entry->buf_count;
928 dma->seg_count += entry->seg_count;
929 dma->page_count += entry->seg_count << page_order;
930 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
931
Dave Airlie30e2fb12006-02-02 19:37:46 +1100932 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Dave Airlied59431b2005-07-10 15:00:06 +1000934 request->count = entry->buf_count;
935 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
George Sapountzis3417f332006-10-24 12:03:04 -0700937 if (request->flags & _DRM_PCI_BUFFER_RO)
938 dma->flags = _DRM_DMA_USE_PCI_RO;
939
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return 0;
942
943}
Dave Airlied84f76d2005-07-10 17:04:22 +1000944EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dave Airlie84b1fd12007-07-11 15:53:27 +1000946static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Dave Airliecdd55a22007-07-11 16:32:08 +1000948 struct drm_device_dma *dma = dev->dma;
949 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000950 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned long offset;
952 unsigned long agp_offset;
953 int count;
954 int order;
955 int size;
956 int alignment;
957 int page_order;
958 int total;
959 int byte_count;
960 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000961 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 if (!drm_core_check_feature(dev, DRIVER_SG))
964 return -EINVAL;
965
966 if (!dma)
967 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dave Airlied985c102006-01-02 21:32:48 +1100969 if (!capable(CAP_SYS_ADMIN))
970 return -EPERM;
971
Dave Airlied59431b2005-07-10 15:00:06 +1000972 count = request->count;
973 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 size = 1 << order;
975
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 alignment = (request->flags & _DRM_PAGE_ALIGN)
977 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
979 total = PAGE_SIZE << page_order;
980
981 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000982 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 DRM_DEBUG("count: %d\n", count);
985 DRM_DEBUG("order: %d\n", order);
986 DRM_DEBUG("size: %d\n", size);
987 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
988 DRM_DEBUG("alignment: %d\n", alignment);
989 DRM_DEBUG("page_order: %d\n", page_order);
990 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
993 return -EINVAL;
994 if (dev->queue_count)
995 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 spin_lock(&dev->count_lock);
998 if (dev->buf_use) {
999 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return -EBUSY;
1001 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001002 atomic_inc(&dev->buf_alloc);
1003 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Dave Airlie30e2fb12006-02-02 19:37:46 +11001005 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001008 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 atomic_dec(&dev->buf_alloc);
1010 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
1013 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001014 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -EINVAL;
1017 }
1018
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1020 DRM_MEM_BUFS);
1021 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001022 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -ENOMEM;
1025 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 entry->buf_size = size;
1029 entry->page_order = page_order;
1030
1031 offset = 0;
1032
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 while (entry->buf_count < count) {
1034 buf = &entry->buflist[entry->buf_count];
1035 buf->idx = dma->buf_count + entry->buf_count;
1036 buf->total = alignment;
1037 buf->order = order;
1038 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001043 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 buf->waiting = 0;
1046 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001048 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1052 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Set count correctly so we free the proper amount. */
1054 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001056 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -ENOMEM;
1059 }
1060
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 offset += alignment;
1066 entry->buf_count++;
1067 byte_count += PAGE_SIZE << page_order;
1068 }
1069
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 temp_buflist = drm_realloc(dma->buflist,
1073 dma->buf_count * sizeof(*dma->buflist),
1074 (dma->buf_count + entry->buf_count)
1075 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1076 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001079 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -ENOMEM;
1082 }
1083 dma->buflist = temp_buflist;
1084
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1087 }
1088
1089 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001090 dma->seg_count += entry->seg_count;
1091 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dma->byte_count += byte_count;
1093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1095 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Dave Airlie30e2fb12006-02-02 19:37:46 +11001097 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Dave Airlied59431b2005-07-10 15:00:06 +10001099 request->count = entry->buf_count;
1100 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 dma->flags = _DRM_DMA_USE_SG;
1103
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return 0;
1106}
1107
Dave Airlie84b1fd12007-07-11 15:53:27 +10001108static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001109{
Dave Airliecdd55a22007-07-11 16:32:08 +10001110 struct drm_device_dma *dma = dev->dma;
1111 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001112 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001113 unsigned long offset;
1114 unsigned long agp_offset;
1115 int count;
1116 int order;
1117 int size;
1118 int alignment;
1119 int page_order;
1120 int total;
1121 int byte_count;
1122 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001123 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001124
1125 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1126 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127
Dave Airlieb84397d62005-07-10 14:46:12 +10001128 if (!dma)
1129 return -EINVAL;
1130
Dave Airlied985c102006-01-02 21:32:48 +11001131 if (!capable(CAP_SYS_ADMIN))
1132 return -EPERM;
1133
Dave Airlied59431b2005-07-10 15:00:06 +10001134 count = request->count;
1135 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001136 size = 1 << order;
1137
Dave Airlied59431b2005-07-10 15:00:06 +10001138 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001139 ? PAGE_ALIGN(size) : size;
1140 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1141 total = PAGE_SIZE << page_order;
1142
1143 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001144 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001145
1146 DRM_DEBUG("count: %d\n", count);
1147 DRM_DEBUG("order: %d\n", order);
1148 DRM_DEBUG("size: %d\n", size);
1149 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1150 DRM_DEBUG("alignment: %d\n", alignment);
1151 DRM_DEBUG("page_order: %d\n", page_order);
1152 DRM_DEBUG("total: %d\n", total);
1153
1154 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1155 return -EINVAL;
1156 if (dev->queue_count)
1157 return -EBUSY; /* Not while in use */
1158
1159 spin_lock(&dev->count_lock);
1160 if (dev->buf_use) {
1161 spin_unlock(&dev->count_lock);
1162 return -EBUSY;
1163 }
1164 atomic_inc(&dev->buf_alloc);
1165 spin_unlock(&dev->count_lock);
1166
Dave Airlie30e2fb12006-02-02 19:37:46 +11001167 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001168 entry = &dma->bufs[order];
1169 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001170 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001171 atomic_dec(&dev->buf_alloc);
1172 return -ENOMEM; /* May only call once for each order */
1173 }
1174
1175 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001176 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001177 atomic_dec(&dev->buf_alloc);
1178 return -EINVAL;
1179 }
1180
1181 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1182 DRM_MEM_BUFS);
1183 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001184 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001185 atomic_dec(&dev->buf_alloc);
1186 return -ENOMEM;
1187 }
1188 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1189
1190 entry->buf_size = size;
1191 entry->page_order = page_order;
1192
1193 offset = 0;
1194
1195 while (entry->buf_count < count) {
1196 buf = &entry->buflist[entry->buf_count];
1197 buf->idx = dma->buf_count + entry->buf_count;
1198 buf->total = alignment;
1199 buf->order = order;
1200 buf->used = 0;
1201
1202 buf->offset = (dma->byte_count + offset);
1203 buf->bus_address = agp_offset + offset;
1204 buf->address = (void *)(agp_offset + offset);
1205 buf->next = NULL;
1206 buf->waiting = 0;
1207 buf->pending = 0;
1208 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001209 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001210
1211 buf->dev_priv_size = dev->driver->dev_priv_size;
1212 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1213 if (!buf->dev_private) {
1214 /* Set count correctly so we free the proper amount. */
1215 entry->buf_count = count;
1216 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001217 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001218 atomic_dec(&dev->buf_alloc);
1219 return -ENOMEM;
1220 }
1221 memset(buf->dev_private, 0, buf->dev_priv_size);
1222
1223 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1224
1225 offset += alignment;
1226 entry->buf_count++;
1227 byte_count += PAGE_SIZE << page_order;
1228 }
1229
1230 DRM_DEBUG("byte_count: %d\n", byte_count);
1231
1232 temp_buflist = drm_realloc(dma->buflist,
1233 dma->buf_count * sizeof(*dma->buflist),
1234 (dma->buf_count + entry->buf_count)
1235 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1236 if (!temp_buflist) {
1237 /* Free the entry because it isn't valid */
1238 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001239 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001240 atomic_dec(&dev->buf_alloc);
1241 return -ENOMEM;
1242 }
1243 dma->buflist = temp_buflist;
1244
1245 for (i = 0; i < entry->buf_count; i++) {
1246 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1247 }
1248
1249 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001250 dma->seg_count += entry->seg_count;
1251 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001252 dma->byte_count += byte_count;
1253
1254 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1255 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1256
Dave Airlie30e2fb12006-02-02 19:37:46 +11001257 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001258
Dave Airlied59431b2005-07-10 15:00:06 +10001259 request->count = entry->buf_count;
1260 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001261
1262 dma->flags = _DRM_DMA_USE_FB;
1263
1264 atomic_dec(&dev->buf_alloc);
1265 return 0;
1266}
Dave Airlied985c102006-01-02 21:32:48 +11001267
Dave Airlieb84397d62005-07-10 14:46:12 +10001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/**
1270 * Add buffers for DMA transfers (ioctl).
1271 *
1272 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001273 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001275 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * \return zero on success or a negative number on failure.
1277 *
1278 * According with the memory type specified in drm_buf_desc::flags and the
1279 * build options, it dispatches the call either to addbufs_agp(),
1280 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1281 * PCI memory respectively.
1282 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10001283int drm_addbufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001284 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Dave Airliec60ce622007-07-11 15:27:12 +10001286 struct drm_buf_desc request;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001287 struct drm_device *dev = file_priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001288 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1291 return -EINVAL;
1292
Dave Airliec60ce622007-07-11 15:27:12 +10001293 if (copy_from_user(&request, (struct drm_buf_desc __user *) arg,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -EFAULT;
1296
1297#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 if (request.flags & _DRM_AGP_BUFFER)
1299 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 else
1301#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001302 if (request.flags & _DRM_SG_BUFFER)
1303 ret = drm_addbufs_sg(dev, &request);
1304 else if (request.flags & _DRM_FB_BUFFER)
1305 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001308
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001309 if (ret == 0) {
1310 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001311 ret = -EFAULT;
1312 }
1313 }
1314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317/**
1318 * Get information about the buffer mappings.
1319 *
1320 * This was originally mean for debugging purposes, or by a sophisticated
1321 * client library to determine how best to use the available buffers (e.g.,
1322 * large buffers can be used for image transfer).
1323 *
1324 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001325 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 * \param cmd command.
1327 * \param arg pointer to a drm_buf_info structure.
1328 * \return zero on success or a negative number on failure.
1329 *
1330 * Increments drm_device::buf_use while holding the drm_device::count_lock
1331 * lock, preventing of allocating more buffers after this call. Information
1332 * about each requested buffer is then copied into user space.
1333 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10001334int drm_infobufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001335 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001337 struct drm_device *dev = file_priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001338 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001339 struct drm_buf_info request;
1340 struct drm_buf_info __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int i;
1342 int count;
1343
1344 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1345 return -EINVAL;
1346
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001347 if (!dma)
1348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001350 spin_lock(&dev->count_lock);
1351 if (atomic_read(&dev->buf_alloc)) {
1352 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EBUSY;
1354 }
1355 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001358 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EFAULT;
1360
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001361 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1362 if (dma->bufs[i].buf_count)
1363 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 if (request.count >= count) {
1369 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1370 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001371 struct drm_buf_desc __user *to =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001372 &request.list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001373 struct drm_buf_entry *from = &dma->bufs[i];
1374 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001375 if (copy_to_user(&to->count,
1376 &from->buf_count,
1377 sizeof(from->buf_count)) ||
1378 copy_to_user(&to->size,
1379 &from->buf_size,
1380 sizeof(from->buf_size)) ||
1381 copy_to_user(&to->low_mark,
1382 &list->low_mark,
1383 sizeof(list->low_mark)) ||
1384 copy_to_user(&to->high_mark,
1385 &list->high_mark,
1386 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
1388
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 DRM_DEBUG("%d %d %d %d %d\n",
1390 i,
1391 dma->bufs[i].buf_count,
1392 dma->bufs[i].buf_size,
1393 dma->bufs[i].freelist.low_mark,
1394 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 ++count;
1396 }
1397 }
1398 }
1399 request.count = count;
1400
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -EFAULT;
1403
1404 return 0;
1405}
1406
1407/**
1408 * Specifies a low and high water mark for buffer allocation
1409 *
1410 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001411 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * \param cmd command.
1413 * \param arg a pointer to a drm_buf_desc structure.
1414 * \return zero on success or a negative number on failure.
1415 *
1416 * Verifies that the size order is bounded between the admissible orders and
1417 * updates the respective drm_device_dma::bufs entry low and high water mark.
1418 *
1419 * \note This ioctl is deprecated and mostly never used.
1420 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10001421int drm_markbufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001424 struct drm_device *dev = file_priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001425 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001426 struct drm_buf_desc request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001428 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1431 return -EINVAL;
1432
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 if (!dma)
1434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001437 (struct drm_buf_desc __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return -EFAULT;
1439
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001440 DRM_DEBUG("%d, %d, %d\n",
1441 request.size, request.low_mark, request.high_mark);
1442 order = drm_order(request.size);
1443 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1444 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 entry = &dma->bufs[order];
1446
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001449 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EINVAL;
1451
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001452 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 entry->freelist.high_mark = request.high_mark;
1454
1455 return 0;
1456}
1457
1458/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
1461 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001462 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * \param cmd command.
1464 * \param arg pointer to a drm_buf_free structure.
1465 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * Calls free_buffer() for each used buffer.
1468 * This function is primarily used for debugging.
1469 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10001470int drm_freebufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001471 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001473 struct drm_device *dev = file_priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001474 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001475 struct drm_buf_free request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int i;
1477 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001478 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1481 return -EINVAL;
1482
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001483 if (!dma)
1484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 if (copy_from_user(&request,
Dave Airliec60ce622007-07-11 15:27:12 +10001487 (struct drm_buf_free __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return -EFAULT;
1489
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001490 DRM_DEBUG("%d\n", request.count);
1491 for (i = 0; i < request.count; i++) {
1492 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 if (idx < 0 || idx >= dma->buf_count) {
1495 DRM_ERROR("Index %d (of %d max)\n",
1496 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -EINVAL;
1498 }
1499 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001500 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501 DRM_ERROR("Process %d freeing buffer not owned\n",
1502 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EINVAL;
1504 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
1508 return 0;
1509}
1510
1511/**
1512 * Maps all of the DMA buffers into client-virtual space (ioctl).
1513 *
1514 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001515 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 * \param cmd command.
1517 * \param arg pointer to a drm_buf_map structure.
1518 * \return zero on success or a negative number on failure.
1519 *
George Sapountzis3417f332006-10-24 12:03:04 -07001520 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1521 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1522 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1523 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10001525int drm_mapbufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001528 struct drm_device *dev = file_priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001529 struct drm_device_dma *dma = dev->dma;
Dave Airliec60ce622007-07-11 15:27:12 +10001530 struct drm_buf_map __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int retcode = 0;
1532 const int zero = 0;
1533 unsigned long virtual;
1534 unsigned long address;
Dave Airliec60ce622007-07-11 15:27:12 +10001535 struct drm_buf_map request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int i;
1537
1538 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1539 return -EINVAL;
1540
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001541 if (!dma)
1542 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001544 spin_lock(&dev->count_lock);
1545 if (atomic_read(&dev->buf_alloc)) {
1546 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return -EBUSY;
1548 }
1549 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001550 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001552 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return -EFAULT;
1554
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001555 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001556 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001558 && (dma->flags & _DRM_DMA_USE_SG))
1559 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1560 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001561 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001562 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 retcode = -EINVAL;
1566 goto done;
1567 }
1568
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001569 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001570 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 PROT_READ | PROT_WRITE,
1572 MAP_SHARED, token);
1573 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001575 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001576 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001577 PROT_READ | PROT_WRITE,
1578 MAP_SHARED, 0);
1579 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001581 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /* Real error */
1583 retcode = (signed long)virtual;
1584 goto done;
1585 }
1586 request.virtual = (void __user *)virtual;
1587
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001588 for (i = 0; i < dma->buf_count; i++) {
1589 if (copy_to_user(&request.list[i].idx,
1590 &dma->buflist[i]->idx,
1591 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 retcode = -EFAULT;
1593 goto done;
1594 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001595 if (copy_to_user(&request.list[i].total,
1596 &dma->buflist[i]->total,
1597 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 retcode = -EFAULT;
1599 goto done;
1600 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001601 if (copy_to_user(&request.list[i].used,
1602 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 retcode = -EFAULT;
1604 goto done;
1605 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001606 address = virtual + dma->buflist[i]->offset; /* *** */
1607 if (copy_to_user(&request.list[i].address,
1608 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 retcode = -EFAULT;
1610 goto done;
1611 }
1612 }
1613 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001614 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001616 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001618 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return -EFAULT;
1620
1621 return retcode;
1622}
1623
Dave Airlie836cf042005-07-10 19:27:04 +10001624/**
1625 * Compute size order. Returns the exponent of the smaller power of two which
1626 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001627 *
Dave Airlie836cf042005-07-10 19:27:04 +10001628 * \param size size.
1629 * \return order.
1630 *
1631 * \todo Can be made faster.
1632 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001633int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001634{
1635 int order;
1636 unsigned long tmp;
1637
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001638 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001639
1640 if (size & (size - 1))
1641 ++order;
1642
1643 return order;
1644}
1645EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001646
1647