blob: d24a6c2c2c24e10f33d7498fccd30e16a8574ec0 [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 Anholtc153f452007-09-03 12:06:45 +1000335int drm_addmap_ioctl(struct drm_device *dev, void *data,
336 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000337{
Eric Anholtc153f452007-09-03 12:06:45 +1000338 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000339 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000340 int err;
341
Eric Anholtc153f452007-09-03 12:06:45 +1000342 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP))
Dave Airlied985c102006-01-02 21:32:48 +1100343 return -EPERM;
344
Eric Anholtc153f452007-09-03 12:06:45 +1000345 err = drm_addmap_core(dev, map->offset, map->size, map->type,
346 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000347
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000348 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000349 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000350
Dave Airlie67e1a012005-10-24 18:41:39 +1000351 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
Eric Anholtc153f452007-09-03 12:06:45 +1000352 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000354}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/**
357 * Remove a map private from list and deallocate resources if the mapping
358 * isn't in use.
359 *
360 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000361 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +1000363 * \param arg pointer to a struct drm_map structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * \return zero on success or a negative value on error.
365 *
366 * Searches the map on drm_device::maplist, removes it from the list, see if
367 * its being used, and free any associate resource (such as MTRR's) if it's not
368 * being on use.
369 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000370 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000372int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Dave Airlie55910512007-07-11 16:53:40 +1000374 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000375 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000376 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dave Airlie836cf042005-07-10 19:27:04 +1000378 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000379 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000380 if (r_list->map == map) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000381 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100382 drm_ht_remove_key(&dev->map_hash,
383 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000384 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
385 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Dave Airlie836cf042005-07-10 19:27:04 +1000389
Dave Airliebd1b3312007-05-26 05:01:51 +1000390 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000391 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000392
393 switch (map->type) {
394 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100395 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000396 /* FALLTHROUGH */
397 case _DRM_FRAME_BUFFER:
398 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
399 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000400 retcode = mtrr_del(map->mtrr, map->offset, map->size);
401 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000402 }
403 break;
404 case _DRM_SHM:
405 vfree(map->handle);
406 break;
407 case _DRM_AGP:
408 case _DRM_SCATTER_GATHER:
409 break;
410 case _DRM_CONSISTENT:
411 dmah.vaddr = map->handle;
412 dmah.busaddr = map->offset;
413 dmah.size = map->size;
414 __drm_pci_free(dev, &dmah);
415 break;
416 }
417 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return 0;
420}
Dave Airlie836cf042005-07-10 19:27:04 +1000421
Dave Airlie84b1fd12007-07-11 15:53:27 +1000422int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000423{
424 int ret;
425
Dave Airlie30e2fb12006-02-02 19:37:46 +1100426 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000427 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100428 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000429
430 return ret;
431}
Dave Airlie7ab98402005-07-10 16:56:52 +1000432
Dave Airlie836cf042005-07-10 19:27:04 +1000433/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
434 * the last close of the device, and this is necessary for cleanup when things
435 * exit uncleanly. Therefore, having userland manually remove mappings seems
436 * like a pointless exercise since they're going away anyway.
437 *
438 * One use case might be after addmap is allowed for normal users for SHM and
439 * gets used by drivers that the server doesn't need to care about. This seems
440 * unlikely.
441 */
Eric Anholtc153f452007-09-03 12:06:45 +1000442int drm_rmmap_ioctl(struct drm_device *dev, void *data,
443 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000444{
Eric Anholtc153f452007-09-03 12:06:45 +1000445 struct drm_map *request = data;
Dave Airlie836cf042005-07-10 19:27:04 +1000446 drm_local_map_t *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000447 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000448 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000449
Dave Airlie30e2fb12006-02-02 19:37:46 +1100450 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000451 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000452 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000453 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000454 r_list->map->flags & _DRM_REMOVABLE) {
455 map = r_list->map;
456 break;
457 }
458 }
459
460 /* List has wrapped around to the head pointer, or its empty we didn't
461 * find anything.
462 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000463 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100464 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000465 return -EINVAL;
466 }
467
Dave Airlie836cf042005-07-10 19:27:04 +1000468 /* Register and framebuffer maps are permanent */
469 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100470 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000471 return 0;
472 }
473
474 ret = drm_rmmap_locked(dev, map);
475
Dave Airlie30e2fb12006-02-02 19:37:46 +1100476 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000477
478 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000479}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481/**
482 * Cleanup after an error on one of the addbufs() functions.
483 *
Dave Airlie836cf042005-07-10 19:27:04 +1000484 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * \param entry buffer entry where the error occurred.
486 *
487 * Frees any pages and buffers associated with the given entry.
488 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000489static void drm_cleanup_buf_error(struct drm_device * dev,
490 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 int i;
493
494 if (entry->seg_count) {
495 for (i = 0; i < entry->seg_count; i++) {
496 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100497 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 }
500 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501 entry->seg_count *
502 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 entry->seg_count = 0;
505 }
506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 if (entry->buf_count) {
508 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (entry->buflist[i].dev_private) {
510 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000511 entry->buflist[i].dev_priv_size,
512 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 }
515 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 entry->buf_count *
517 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 entry->buf_count = 0;
520 }
521}
522
523#if __OS_HAS_AGP
524/**
Dave Airlied59431b2005-07-10 15:00:06 +1000525 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000527 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000528 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * After some sanity checks creates a drm_buf structure for each buffer and
532 * reallocates the buffer list of the same size order to accommodate the new
533 * buffers.
534 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000535int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Dave Airliecdd55a22007-07-11 16:32:08 +1000537 struct drm_device_dma *dma = dev->dma;
538 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000539 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000540 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unsigned long offset;
542 unsigned long agp_offset;
543 int count;
544 int order;
545 int size;
546 int alignment;
547 int page_order;
548 int total;
549 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100550 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000551 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 if (!dma)
554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dave Airlied59431b2005-07-10 15:00:06 +1000556 count = request->count;
557 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 size = 1 << order;
559
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 alignment = (request->flags & _DRM_PAGE_ALIGN)
561 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
563 total = PAGE_SIZE << page_order;
564
565 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000566 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 DRM_DEBUG("count: %d\n", count);
569 DRM_DEBUG("order: %d\n", order);
570 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100571 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 DRM_DEBUG("alignment: %d\n", alignment);
573 DRM_DEBUG("page_order: %d\n", page_order);
574 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
577 return -EINVAL;
578 if (dev->queue_count)
579 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Dave Airlie54ba2f72007-02-10 12:07:47 +1100581 /* Make sure buffers are located in AGP memory that we own */
582 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000583 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100584 if ((agp_offset >= agp_entry->bound) &&
585 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
586 valid = 1;
587 break;
588 }
589 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000590 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100591 DRM_DEBUG("zone invalid\n");
592 return -EINVAL;
593 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 spin_lock(&dev->count_lock);
595 if (dev->buf_use) {
596 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EBUSY;
598 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 atomic_inc(&dev->buf_alloc);
600 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Dave Airlie30e2fb12006-02-02 19:37:46 +1100602 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100605 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 atomic_dec(&dev->buf_alloc);
607 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100611 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EINVAL;
614 }
615
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000616 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
617 DRM_MEM_BUFS);
618 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100619 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -ENOMEM;
622 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 entry->buf_size = size;
626 entry->page_order = page_order;
627
628 offset = 0;
629
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 while (entry->buf_count < count) {
631 buf = &entry->buflist[entry->buf_count];
632 buf->idx = dma->buf_count + entry->buf_count;
633 buf->total = alignment;
634 buf->order = order;
635 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 buf->bus_address = agp_offset + offset;
639 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 buf->waiting = 0;
642 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000644 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
648 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Set count correctly so we free the proper amount. */
650 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100652 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -ENOMEM;
655 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 offset += alignment;
661 entry->buf_count++;
662 byte_count += PAGE_SIZE << page_order;
663 }
664
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 temp_buflist = drm_realloc(dma->buflist,
668 dma->buf_count * sizeof(*dma->buflist),
669 (dma->buf_count + entry->buf_count)
670 * sizeof(*dma->buflist), DRM_MEM_BUFS);
671 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100674 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENOMEM;
677 }
678 dma->buflist = temp_buflist;
679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
682 }
683
684 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100685 dma->seg_count += entry->seg_count;
686 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 dma->byte_count += byte_count;
688
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
690 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Dave Airlie30e2fb12006-02-02 19:37:46 +1100692 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Dave Airlied59431b2005-07-10 15:00:06 +1000694 request->count = entry->buf_count;
695 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 dma->flags = _DRM_DMA_USE_AGP;
698
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702EXPORT_SYMBOL(drm_addbufs_agp);
703#endif /* __OS_HAS_AGP */
704
Dave Airlie84b1fd12007-07-11 15:53:27 +1000705int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Dave Airliecdd55a22007-07-11 16:32:08 +1000707 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int count;
709 int order;
710 int size;
711 int total;
712 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000713 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100714 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000715 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int alignment;
717 unsigned long offset;
718 int i;
719 int byte_count;
720 int page_count;
721 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000722 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
725 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100726
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 if (!dma)
728 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dave Airlied985c102006-01-02 21:32:48 +1100730 if (!capable(CAP_SYS_ADMIN))
731 return -EPERM;
732
Dave Airlied59431b2005-07-10 15:00:06 +1000733 count = request->count;
734 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 size = 1 << order;
736
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
738 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
741 return -EINVAL;
742 if (dev->queue_count)
743 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Airlied59431b2005-07-10 15:00:06 +1000745 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
748 total = PAGE_SIZE << page_order;
749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 spin_lock(&dev->count_lock);
751 if (dev->buf_use) {
752 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return -EBUSY;
754 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 atomic_inc(&dev->buf_alloc);
756 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dave Airlie30e2fb12006-02-02 19:37:46 +1100758 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100761 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -ENOMEM; /* May only call once for each order */
764 }
765
766 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100767 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return -EINVAL;
770 }
771
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
773 DRM_MEM_BUFS);
774 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100775 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -ENOMEM;
778 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
782 DRM_MEM_SEGS);
783 if (!entry->seglist) {
784 drm_free(entry->buflist,
785 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100786 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -ENOMEM;
789 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* Keep the original pagelist until we know all the allocations
793 * have succeeded
794 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
796 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 drm_free(entry->buflist,
799 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
800 drm_free(entry->seglist,
801 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100802 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -ENOMEM;
805 }
806 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
808 DRM_DEBUG("pagelist: %d entries\n",
809 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 entry->page_order = page_order;
813 byte_count = 0;
814 page_count = 0;
815
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100817
818 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
819
820 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* Set count correctly so we free the proper amount. */
822 entry->buf_count = count;
823 entry->seg_count = count;
824 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 drm_free(temp_pagelist,
826 (dma->page_count + (count << page_order))
827 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100828 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -ENOMEM;
831 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100832 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 for (i = 0; i < (1 << page_order); i++) {
834 DRM_DEBUG("page %d @ 0x%08lx\n",
835 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100836 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100838 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 for (offset = 0;
841 offset + size <= total && entry->buf_count < count;
842 offset += alignment, ++entry->buf_count) {
843 buf = &entry->buflist[entry->buf_count];
844 buf->idx = dma->buf_count + entry->buf_count;
845 buf->total = alignment;
846 buf->order = order;
847 buf->used = 0;
848 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100849 buf->address = (void *)(dmah->vaddr + offset);
850 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 buf->waiting = 0;
853 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000855 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 buf->dev_private = drm_alloc(buf->dev_priv_size,
859 DRM_MEM_BUFS);
860 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* Set count correctly so we free the proper amount. */
862 entry->buf_count = count;
863 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 drm_cleanup_buf_error(dev, entry);
865 drm_free(temp_pagelist,
866 (dma->page_count +
867 (count << page_order))
868 * sizeof(*dma->pagelist),
869 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100870 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -ENOMEM;
873 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 DRM_DEBUG("buffer %d @ %p\n",
877 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 byte_count += PAGE_SIZE << page_order;
880 }
881
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 temp_buflist = drm_realloc(dma->buflist,
883 dma->buf_count * sizeof(*dma->buflist),
884 (dma->buf_count + entry->buf_count)
885 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!temp_buflist) {
887 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 drm_cleanup_buf_error(dev, entry);
889 drm_free(temp_pagelist,
890 (dma->page_count + (count << page_order))
891 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100892 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -ENOMEM;
895 }
896 dma->buflist = temp_buflist;
897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
900 }
901
902 /* No allocations failed, so now we can replace the orginal pagelist
903 * with the new one.
904 */
905 if (dma->page_count) {
906 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 dma->page_count * sizeof(*dma->pagelist),
908 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 dma->pagelist = temp_pagelist;
911
912 dma->buf_count += entry->buf_count;
913 dma->seg_count += entry->seg_count;
914 dma->page_count += entry->seg_count << page_order;
915 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
916
Dave Airlie30e2fb12006-02-02 19:37:46 +1100917 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Dave Airlied59431b2005-07-10 15:00:06 +1000919 request->count = entry->buf_count;
920 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
George Sapountzis3417f332006-10-24 12:03:04 -0700922 if (request->flags & _DRM_PCI_BUFFER_RO)
923 dma->flags = _DRM_DMA_USE_PCI_RO;
924
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
927
928}
Dave Airlied84f76d2005-07-10 17:04:22 +1000929EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dave Airlie84b1fd12007-07-11 15:53:27 +1000931static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Dave Airliecdd55a22007-07-11 16:32:08 +1000933 struct drm_device_dma *dma = dev->dma;
934 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000935 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 unsigned long offset;
937 unsigned long agp_offset;
938 int count;
939 int order;
940 int size;
941 int alignment;
942 int page_order;
943 int total;
944 int byte_count;
945 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000946 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 if (!drm_core_check_feature(dev, DRIVER_SG))
949 return -EINVAL;
950
951 if (!dma)
952 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dave Airlied985c102006-01-02 21:32:48 +1100954 if (!capable(CAP_SYS_ADMIN))
955 return -EPERM;
956
Dave Airlied59431b2005-07-10 15:00:06 +1000957 count = request->count;
958 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 size = 1 << order;
960
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 alignment = (request->flags & _DRM_PAGE_ALIGN)
962 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
964 total = PAGE_SIZE << page_order;
965
966 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000967 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000969 DRM_DEBUG("count: %d\n", count);
970 DRM_DEBUG("order: %d\n", order);
971 DRM_DEBUG("size: %d\n", size);
972 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
973 DRM_DEBUG("alignment: %d\n", alignment);
974 DRM_DEBUG("page_order: %d\n", page_order);
975 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
978 return -EINVAL;
979 if (dev->queue_count)
980 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 spin_lock(&dev->count_lock);
983 if (dev->buf_use) {
984 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -EBUSY;
986 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 atomic_inc(&dev->buf_alloc);
988 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Dave Airlie30e2fb12006-02-02 19:37:46 +1100990 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100993 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 atomic_dec(&dev->buf_alloc);
995 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
998 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100999 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return -EINVAL;
1002 }
1003
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1005 DRM_MEM_BUFS);
1006 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001007 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return -ENOMEM;
1010 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 entry->buf_size = size;
1014 entry->page_order = page_order;
1015
1016 offset = 0;
1017
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 while (entry->buf_count < count) {
1019 buf = &entry->buflist[entry->buf_count];
1020 buf->idx = dma->buf_count + entry->buf_count;
1021 buf->total = alignment;
1022 buf->order = order;
1023 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001028 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 buf->waiting = 0;
1031 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001033 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1037 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Set count correctly so we free the proper amount. */
1039 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001041 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -ENOMEM;
1044 }
1045
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 offset += alignment;
1051 entry->buf_count++;
1052 byte_count += PAGE_SIZE << page_order;
1053 }
1054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 temp_buflist = drm_realloc(dma->buflist,
1058 dma->buf_count * sizeof(*dma->buflist),
1059 (dma->buf_count + entry->buf_count)
1060 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1061 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001064 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return -ENOMEM;
1067 }
1068 dma->buflist = temp_buflist;
1069
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1072 }
1073
1074 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001075 dma->seg_count += entry->seg_count;
1076 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 dma->byte_count += byte_count;
1078
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1080 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Dave Airlie30e2fb12006-02-02 19:37:46 +11001082 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Dave Airlied59431b2005-07-10 15:00:06 +10001084 request->count = entry->buf_count;
1085 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 dma->flags = _DRM_DMA_USE_SG;
1088
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return 0;
1091}
1092
Dave Airlie84b1fd12007-07-11 15:53:27 +10001093static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001094{
Dave Airliecdd55a22007-07-11 16:32:08 +10001095 struct drm_device_dma *dma = dev->dma;
1096 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001097 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001098 unsigned long offset;
1099 unsigned long agp_offset;
1100 int count;
1101 int order;
1102 int size;
1103 int alignment;
1104 int page_order;
1105 int total;
1106 int byte_count;
1107 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001108 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001109
1110 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1111 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112
Dave Airlieb84397d62005-07-10 14:46:12 +10001113 if (!dma)
1114 return -EINVAL;
1115
Dave Airlied985c102006-01-02 21:32:48 +11001116 if (!capable(CAP_SYS_ADMIN))
1117 return -EPERM;
1118
Dave Airlied59431b2005-07-10 15:00:06 +10001119 count = request->count;
1120 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001121 size = 1 << order;
1122
Dave Airlied59431b2005-07-10 15:00:06 +10001123 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001124 ? PAGE_ALIGN(size) : size;
1125 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1126 total = PAGE_SIZE << page_order;
1127
1128 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001129 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001130
1131 DRM_DEBUG("count: %d\n", count);
1132 DRM_DEBUG("order: %d\n", order);
1133 DRM_DEBUG("size: %d\n", size);
1134 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1135 DRM_DEBUG("alignment: %d\n", alignment);
1136 DRM_DEBUG("page_order: %d\n", page_order);
1137 DRM_DEBUG("total: %d\n", total);
1138
1139 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1140 return -EINVAL;
1141 if (dev->queue_count)
1142 return -EBUSY; /* Not while in use */
1143
1144 spin_lock(&dev->count_lock);
1145 if (dev->buf_use) {
1146 spin_unlock(&dev->count_lock);
1147 return -EBUSY;
1148 }
1149 atomic_inc(&dev->buf_alloc);
1150 spin_unlock(&dev->count_lock);
1151
Dave Airlie30e2fb12006-02-02 19:37:46 +11001152 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001153 entry = &dma->bufs[order];
1154 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001155 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001156 atomic_dec(&dev->buf_alloc);
1157 return -ENOMEM; /* May only call once for each order */
1158 }
1159
1160 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001161 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001162 atomic_dec(&dev->buf_alloc);
1163 return -EINVAL;
1164 }
1165
1166 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1167 DRM_MEM_BUFS);
1168 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001169 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001170 atomic_dec(&dev->buf_alloc);
1171 return -ENOMEM;
1172 }
1173 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1174
1175 entry->buf_size = size;
1176 entry->page_order = page_order;
1177
1178 offset = 0;
1179
1180 while (entry->buf_count < count) {
1181 buf = &entry->buflist[entry->buf_count];
1182 buf->idx = dma->buf_count + entry->buf_count;
1183 buf->total = alignment;
1184 buf->order = order;
1185 buf->used = 0;
1186
1187 buf->offset = (dma->byte_count + offset);
1188 buf->bus_address = agp_offset + offset;
1189 buf->address = (void *)(agp_offset + offset);
1190 buf->next = NULL;
1191 buf->waiting = 0;
1192 buf->pending = 0;
1193 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001194 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001195
1196 buf->dev_priv_size = dev->driver->dev_priv_size;
1197 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1198 if (!buf->dev_private) {
1199 /* Set count correctly so we free the proper amount. */
1200 entry->buf_count = count;
1201 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001202 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001203 atomic_dec(&dev->buf_alloc);
1204 return -ENOMEM;
1205 }
1206 memset(buf->dev_private, 0, buf->dev_priv_size);
1207
1208 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1209
1210 offset += alignment;
1211 entry->buf_count++;
1212 byte_count += PAGE_SIZE << page_order;
1213 }
1214
1215 DRM_DEBUG("byte_count: %d\n", byte_count);
1216
1217 temp_buflist = drm_realloc(dma->buflist,
1218 dma->buf_count * sizeof(*dma->buflist),
1219 (dma->buf_count + entry->buf_count)
1220 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1221 if (!temp_buflist) {
1222 /* Free the entry because it isn't valid */
1223 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001224 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001225 atomic_dec(&dev->buf_alloc);
1226 return -ENOMEM;
1227 }
1228 dma->buflist = temp_buflist;
1229
1230 for (i = 0; i < entry->buf_count; i++) {
1231 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1232 }
1233
1234 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001235 dma->seg_count += entry->seg_count;
1236 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001237 dma->byte_count += byte_count;
1238
1239 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1240 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1241
Dave Airlie30e2fb12006-02-02 19:37:46 +11001242 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001243
Dave Airlied59431b2005-07-10 15:00:06 +10001244 request->count = entry->buf_count;
1245 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001246
1247 dma->flags = _DRM_DMA_USE_FB;
1248
1249 atomic_dec(&dev->buf_alloc);
1250 return 0;
1251}
Dave Airlied985c102006-01-02 21:32:48 +11001252
Dave Airlieb84397d62005-07-10 14:46:12 +10001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/**
1255 * Add buffers for DMA transfers (ioctl).
1256 *
1257 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001258 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001260 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * \return zero on success or a negative number on failure.
1262 *
1263 * According with the memory type specified in drm_buf_desc::flags and the
1264 * build options, it dispatches the call either to addbufs_agp(),
1265 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1266 * PCI memory respectively.
1267 */
Eric Anholtc153f452007-09-03 12:06:45 +10001268int drm_addbufs(struct drm_device *dev, void *data,
1269 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Eric Anholtc153f452007-09-03 12:06:45 +10001271 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001272 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1275 return -EINVAL;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001278 if (request->flags & _DRM_AGP_BUFFER)
1279 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 else
1281#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001282 if (request->flags & _DRM_SG_BUFFER)
1283 ret = drm_addbufs_sg(dev, request);
1284 else if (request->flags & _DRM_FB_BUFFER)
1285 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 else
Eric Anholtc153f452007-09-03 12:06:45 +10001287 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001288
Dave Airlied59431b2005-07-10 15:00:06 +10001289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/**
1293 * Get information about the buffer mappings.
1294 *
1295 * This was originally mean for debugging purposes, or by a sophisticated
1296 * client library to determine how best to use the available buffers (e.g.,
1297 * large buffers can be used for image transfer).
1298 *
1299 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001300 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * \param cmd command.
1302 * \param arg pointer to a drm_buf_info structure.
1303 * \return zero on success or a negative number on failure.
1304 *
1305 * Increments drm_device::buf_use while holding the drm_device::count_lock
1306 * lock, preventing of allocating more buffers after this call. Information
1307 * about each requested buffer is then copied into user space.
1308 */
Eric Anholtc153f452007-09-03 12:06:45 +10001309int drm_infobufs(struct drm_device *dev, void *data,
1310 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Dave Airliecdd55a22007-07-11 16:32:08 +10001312 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001313 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 int i;
1315 int count;
1316
1317 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1318 return -EINVAL;
1319
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 if (!dma)
1321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001323 spin_lock(&dev->count_lock);
1324 if (atomic_read(&dev->buf_alloc)) {
1325 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -EBUSY;
1327 }
1328 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001329 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001331 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1332 if (dma->bufs[i].buf_count)
1333 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001336 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Eric Anholtc153f452007-09-03 12:06:45 +10001338 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001339 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1340 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001341 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001342 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001343 struct drm_buf_entry *from = &dma->bufs[i];
1344 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001345 if (copy_to_user(&to->count,
1346 &from->buf_count,
1347 sizeof(from->buf_count)) ||
1348 copy_to_user(&to->size,
1349 &from->buf_size,
1350 sizeof(from->buf_size)) ||
1351 copy_to_user(&to->low_mark,
1352 &list->low_mark,
1353 sizeof(list->low_mark)) ||
1354 copy_to_user(&to->high_mark,
1355 &list->high_mark,
1356 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EFAULT;
1358
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001359 DRM_DEBUG("%d %d %d %d %d\n",
1360 i,
1361 dma->bufs[i].buf_count,
1362 dma->bufs[i].buf_size,
1363 dma->bufs[i].freelist.low_mark,
1364 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ++count;
1366 }
1367 }
1368 }
Eric Anholtc153f452007-09-03 12:06:45 +10001369 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 return 0;
1372}
1373
1374/**
1375 * Specifies a low and high water mark for buffer allocation
1376 *
1377 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001378 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * \param cmd command.
1380 * \param arg a pointer to a drm_buf_desc structure.
1381 * \return zero on success or a negative number on failure.
1382 *
1383 * Verifies that the size order is bounded between the admissible orders and
1384 * updates the respective drm_device_dma::bufs entry low and high water mark.
1385 *
1386 * \note This ioctl is deprecated and mostly never used.
1387 */
Eric Anholtc153f452007-09-03 12:06:45 +10001388int drm_markbufs(struct drm_device *dev, void *data,
1389 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Dave Airliecdd55a22007-07-11 16:32:08 +10001391 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001392 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001394 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1397 return -EINVAL;
1398
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001399 if (!dma)
1400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001402 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001403 request->size, request->low_mark, request->high_mark);
1404 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 entry = &dma->bufs[order];
1408
Eric Anholtc153f452007-09-03 12:06:45 +10001409 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001411 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -EINVAL;
1413
Eric Anholtc153f452007-09-03 12:06:45 +10001414 entry->freelist.low_mark = request->low_mark;
1415 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 return 0;
1418}
1419
1420/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001421 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 *
1423 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001424 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 * \param cmd command.
1426 * \param arg pointer to a drm_buf_free structure.
1427 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * Calls free_buffer() for each used buffer.
1430 * This function is primarily used for debugging.
1431 */
Eric Anholtc153f452007-09-03 12:06:45 +10001432int drm_freebufs(struct drm_device *dev, void *data,
1433 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Dave Airliecdd55a22007-07-11 16:32:08 +10001435 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001436 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int i;
1438 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001439 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1442 return -EINVAL;
1443
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001444 if (!dma)
1445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Eric Anholtc153f452007-09-03 12:06:45 +10001447 DRM_DEBUG("%d\n", request->count);
1448 for (i = 0; i < request->count; i++) {
1449 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 if (idx < 0 || idx >= dma->buf_count) {
1452 DRM_ERROR("Index %d (of %d max)\n",
1453 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EINVAL;
1455 }
1456 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001457 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001458 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001459 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -EINVAL;
1461 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001462 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
1465 return 0;
1466}
1467
1468/**
1469 * Maps all of the DMA buffers into client-virtual space (ioctl).
1470 *
1471 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001472 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * \param cmd command.
1474 * \param arg pointer to a drm_buf_map structure.
1475 * \return zero on success or a negative number on failure.
1476 *
George Sapountzis3417f332006-10-24 12:03:04 -07001477 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1478 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1479 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1480 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
Eric Anholtc153f452007-09-03 12:06:45 +10001482int drm_mapbufs(struct drm_device *dev, void *data,
1483 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Dave Airliecdd55a22007-07-11 16:32:08 +10001485 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int retcode = 0;
1487 const int zero = 0;
1488 unsigned long virtual;
1489 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001490 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int i;
1492
1493 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1494 return -EINVAL;
1495
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001496 if (!dma)
1497 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001499 spin_lock(&dev->count_lock);
1500 if (atomic_read(&dev->buf_alloc)) {
1501 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -EBUSY;
1503 }
1504 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Eric Anholtc153f452007-09-03 12:06:45 +10001507 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001508 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001509 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001510 && (dma->flags & _DRM_DMA_USE_SG))
1511 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1512 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001513 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001514 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001516 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 retcode = -EINVAL;
1518 goto done;
1519 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001521 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001523 MAP_SHARED,
1524 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001525 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001527 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001528 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001529 PROT_READ | PROT_WRITE,
1530 MAP_SHARED, 0);
1531 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* Real error */
1535 retcode = (signed long)virtual;
1536 goto done;
1537 }
Eric Anholtc153f452007-09-03 12:06:45 +10001538 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001541 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001542 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001543 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 retcode = -EFAULT;
1545 goto done;
1546 }
Eric Anholtc153f452007-09-03 12:06:45 +10001547 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001548 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001549 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 retcode = -EFAULT;
1551 goto done;
1552 }
Eric Anholtc153f452007-09-03 12:06:45 +10001553 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001554 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 retcode = -EFAULT;
1556 goto done;
1557 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001558 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001559 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 retcode = -EFAULT;
1562 goto done;
1563 }
1564 }
1565 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001566 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001567 request->count = dma->buf_count;
1568 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 return retcode;
1571}
1572
Dave Airlie836cf042005-07-10 19:27:04 +10001573/**
1574 * Compute size order. Returns the exponent of the smaller power of two which
1575 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001576 *
Dave Airlie836cf042005-07-10 19:27:04 +10001577 * \param size size.
1578 * \return order.
1579 *
1580 * \todo Can be made faster.
1581 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001582int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001583{
1584 int order;
1585 unsigned long tmp;
1586
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001587 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001588
1589 if (size & (size - 1))
1590 ++order;
1591
1592 return order;
1593}
1594EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001595
1596