blob: bde64b84166e1d783a2c5a8ece1816c483e76f0b [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 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000187
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
Eric Anholt47a184a2007-11-22 16:55:15 +1000232 /* In some cases (i810 driver), user space may have already
233 * added the AGP base itself, because dev->agp->base previously
234 * only got set during AGP enable. So, only add the base
235 * address if the map's offset isn't already within the
236 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100237 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000238 if (map->offset < dev->agp->base ||
239 map->offset > dev->agp->base +
240 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
241 map->offset += dev->agp->base;
242 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100243 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
244
245 /* This assumes the DRM is in total control of AGP space.
246 * It's not always the case as AGP can be in the control
247 * of user space (i.e. i810 driver). So this loop will get
248 * skipped and we double check that dev->agp->memory is
249 * actually set as well as being invalid before EPERM'ing
250 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000251 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100252 if ((map->offset >= entry->bound) &&
253 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
254 valid = 1;
255 break;
256 }
257 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000258 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100259 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
260 return -EPERM;
261 }
262 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 case _DRM_SCATTER_GATHER:
267 if (!dev->sg) {
268 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
269 return -EINVAL;
270 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000271 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000274 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000275 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000276 * casting it down to 32 bits is no problem, but we
277 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000278 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
279 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000280 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
281 return -ENOMEM;
282 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000283 map->handle = dmah->vaddr;
284 map->offset = (unsigned long)dmah->busaddr;
285 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -EINVAL;
290 }
291
292 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700294 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100295 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
297 return -EINVAL;
298 }
299 memset(list, 0, sizeof(*list));
300 list->map = map;
301
Dave Airlie30e2fb12006-02-02 19:37:46 +1100302 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000303 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000304
Dave Airlied1f2b552005-08-05 22:11:22 +1000305 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100306 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000307 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
308 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000309 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000310 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700311 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100312 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000313 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
314 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
315 mutex_unlock(&dev->struct_mutex);
316 return ret;
317 }
318
Thomas Hellstrom15450852007-02-08 16:14:05 +1100319 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100320 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dave Airlie89625eb2005-09-05 21:23:23 +1000322 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000323 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100324 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000325
Dave Airlie84b1fd12007-07-11 15:53:27 +1000326int drm_addmap(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000327 unsigned int size, enum drm_map_type type,
328 enum drm_map_flags flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000329{
Dave Airlie55910512007-07-11 16:53:40 +1000330 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000331 int rc;
332
333 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
334 if (!rc)
335 *map_ptr = list->map;
336 return rc;
337}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338
Dave Airlie7ab98402005-07-10 16:56:52 +1000339EXPORT_SYMBOL(drm_addmap);
340
Eric Anholtc153f452007-09-03 12:06:45 +1000341int drm_addmap_ioctl(struct drm_device *dev, void *data,
342 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000343{
Eric Anholtc153f452007-09-03 12:06:45 +1000344 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000345 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000346 int err;
347
Eric Anholtc153f452007-09-03 12:06:45 +1000348 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP))
Dave Airlied985c102006-01-02 21:32:48 +1100349 return -EPERM;
350
Eric Anholtc153f452007-09-03 12:06:45 +1000351 err = drm_addmap_core(dev, map->offset, map->size, map->type,
352 map->flags, &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 Airlie67e1a012005-10-24 18:41:39 +1000357 /* 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 +1000358 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
363 * Remove a map private from list and deallocate resources if the mapping
364 * isn't in use.
365 *
366 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000367 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +1000369 * \param arg pointer to a struct drm_map structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * \return zero on success or a negative value on error.
371 *
372 * Searches the map on drm_device::maplist, removes it from the list, see if
373 * its being used, and free any associate resource (such as MTRR's) if it's not
374 * being on use.
375 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000376 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000378int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Dave Airlie55910512007-07-11 16:53:40 +1000380 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000381 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000382 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dave Airlie836cf042005-07-10 19:27:04 +1000384 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000385 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000386 if (r_list->map == map) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000387 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100388 drm_ht_remove_key(&dev->map_hash,
389 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000390 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
391 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Dave Airlie836cf042005-07-10 19:27:04 +1000395
Dave Airliebd1b3312007-05-26 05:01:51 +1000396 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000397 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000398
399 switch (map->type) {
400 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100401 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000402 /* FALLTHROUGH */
403 case _DRM_FRAME_BUFFER:
404 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
405 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000406 retcode = mtrr_del(map->mtrr, map->offset, map->size);
407 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000408 }
409 break;
410 case _DRM_SHM:
411 vfree(map->handle);
412 break;
413 case _DRM_AGP:
414 case _DRM_SCATTER_GATHER:
415 break;
416 case _DRM_CONSISTENT:
417 dmah.vaddr = map->handle;
418 dmah.busaddr = map->offset;
419 dmah.size = map->size;
420 __drm_pci_free(dev, &dmah);
421 break;
422 }
423 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
426}
Dave Airlie836cf042005-07-10 19:27:04 +1000427
Dave Airlie84b1fd12007-07-11 15:53:27 +1000428int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000429{
430 int ret;
431
Dave Airlie30e2fb12006-02-02 19:37:46 +1100432 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000433 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100434 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000435
436 return ret;
437}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000438EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000439
Dave Airlie836cf042005-07-10 19:27:04 +1000440/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
441 * the last close of the device, and this is necessary for cleanup when things
442 * exit uncleanly. Therefore, having userland manually remove mappings seems
443 * like a pointless exercise since they're going away anyway.
444 *
445 * One use case might be after addmap is allowed for normal users for SHM and
446 * gets used by drivers that the server doesn't need to care about. This seems
447 * unlikely.
448 */
Eric Anholtc153f452007-09-03 12:06:45 +1000449int drm_rmmap_ioctl(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000451{
Eric Anholtc153f452007-09-03 12:06:45 +1000452 struct drm_map *request = data;
Dave Airlie836cf042005-07-10 19:27:04 +1000453 drm_local_map_t *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000454 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000455 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000456
Dave Airlie30e2fb12006-02-02 19:37:46 +1100457 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000458 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000459 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000460 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000461 r_list->map->flags & _DRM_REMOVABLE) {
462 map = r_list->map;
463 break;
464 }
465 }
466
467 /* List has wrapped around to the head pointer, or its empty we didn't
468 * find anything.
469 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000470 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100471 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000472 return -EINVAL;
473 }
474
Dave Airlie836cf042005-07-10 19:27:04 +1000475 /* Register and framebuffer maps are permanent */
476 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100477 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000478 return 0;
479 }
480
481 ret = drm_rmmap_locked(dev, map);
482
Dave Airlie30e2fb12006-02-02 19:37:46 +1100483 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000484
485 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000486}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/**
489 * Cleanup after an error on one of the addbufs() functions.
490 *
Dave Airlie836cf042005-07-10 19:27:04 +1000491 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * \param entry buffer entry where the error occurred.
493 *
494 * Frees any pages and buffers associated with the given entry.
495 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000496static void drm_cleanup_buf_error(struct drm_device * dev,
497 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 int i;
500
501 if (entry->seg_count) {
502 for (i = 0; i < entry->seg_count; i++) {
503 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100504 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 }
507 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508 entry->seg_count *
509 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 entry->seg_count = 0;
512 }
513
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514 if (entry->buf_count) {
515 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (entry->buflist[i].dev_private) {
517 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 entry->buflist[i].dev_priv_size,
519 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 }
522 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000523 entry->buf_count *
524 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 entry->buf_count = 0;
527 }
528}
529
530#if __OS_HAS_AGP
531/**
Dave Airlied59431b2005-07-10 15:00:06 +1000532 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000534 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000535 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * After some sanity checks creates a drm_buf structure for each buffer and
539 * reallocates the buffer list of the same size order to accommodate the new
540 * buffers.
541 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000542int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Dave Airliecdd55a22007-07-11 16:32:08 +1000544 struct drm_device_dma *dma = dev->dma;
545 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000546 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000547 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 unsigned long offset;
549 unsigned long agp_offset;
550 int count;
551 int order;
552 int size;
553 int alignment;
554 int page_order;
555 int total;
556 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100557 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000558 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 if (!dma)
561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Dave Airlied59431b2005-07-10 15:00:06 +1000563 count = request->count;
564 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 size = 1 << order;
566
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 alignment = (request->flags & _DRM_PAGE_ALIGN)
568 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
570 total = PAGE_SIZE << page_order;
571
572 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000573 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 DRM_DEBUG("count: %d\n", count);
576 DRM_DEBUG("order: %d\n", order);
577 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100578 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 DRM_DEBUG("alignment: %d\n", alignment);
580 DRM_DEBUG("page_order: %d\n", page_order);
581 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
584 return -EINVAL;
585 if (dev->queue_count)
586 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dave Airlie54ba2f72007-02-10 12:07:47 +1100588 /* Make sure buffers are located in AGP memory that we own */
589 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000590 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100591 if ((agp_offset >= agp_entry->bound) &&
592 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
593 valid = 1;
594 break;
595 }
596 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000597 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100598 DRM_DEBUG("zone invalid\n");
599 return -EINVAL;
600 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 spin_lock(&dev->count_lock);
602 if (dev->buf_use) {
603 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -EBUSY;
605 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 atomic_inc(&dev->buf_alloc);
607 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Dave Airlie30e2fb12006-02-02 19:37:46 +1100609 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100612 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 atomic_dec(&dev->buf_alloc);
614 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
617 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100618 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EINVAL;
621 }
622
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
624 DRM_MEM_BUFS);
625 if (!entry->buflist) {
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 -ENOMEM;
629 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 entry->buf_size = size;
633 entry->page_order = page_order;
634
635 offset = 0;
636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 while (entry->buf_count < count) {
638 buf = &entry->buflist[entry->buf_count];
639 buf->idx = dma->buf_count + entry->buf_count;
640 buf->total = alignment;
641 buf->order = order;
642 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 buf->bus_address = agp_offset + offset;
646 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 buf->waiting = 0;
649 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000651 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
655 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Set count correctly so we free the proper amount. */
657 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100659 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return -ENOMEM;
662 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 offset += alignment;
668 entry->buf_count++;
669 byte_count += PAGE_SIZE << page_order;
670 }
671
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000672 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 temp_buflist = drm_realloc(dma->buflist,
675 dma->buf_count * sizeof(*dma->buflist),
676 (dma->buf_count + entry->buf_count)
677 * sizeof(*dma->buflist), DRM_MEM_BUFS);
678 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100681 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -ENOMEM;
684 }
685 dma->buflist = temp_buflist;
686
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
689 }
690
691 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100692 dma->seg_count += entry->seg_count;
693 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 dma->byte_count += byte_count;
695
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
697 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dave Airlie30e2fb12006-02-02 19:37:46 +1100699 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Dave Airlied59431b2005-07-10 15:00:06 +1000701 request->count = entry->buf_count;
702 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 dma->flags = _DRM_DMA_USE_AGP;
705
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709EXPORT_SYMBOL(drm_addbufs_agp);
710#endif /* __OS_HAS_AGP */
711
Dave Airlie84b1fd12007-07-11 15:53:27 +1000712int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Dave Airliecdd55a22007-07-11 16:32:08 +1000714 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int count;
716 int order;
717 int size;
718 int total;
719 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000720 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100721 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000722 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int alignment;
724 unsigned long offset;
725 int i;
726 int byte_count;
727 int page_count;
728 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000729 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
732 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100733
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 if (!dma)
735 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Dave Airlied985c102006-01-02 21:32:48 +1100737 if (!capable(CAP_SYS_ADMIN))
738 return -EPERM;
739
Dave Airlied59431b2005-07-10 15:00:06 +1000740 count = request->count;
741 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 size = 1 << order;
743
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
745 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
748 return -EINVAL;
749 if (dev->queue_count)
750 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Dave Airlied59431b2005-07-10 15:00:06 +1000752 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
755 total = PAGE_SIZE << page_order;
756
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 spin_lock(&dev->count_lock);
758 if (dev->buf_use) {
759 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EBUSY;
761 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 atomic_inc(&dev->buf_alloc);
763 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Dave Airlie30e2fb12006-02-02 19:37:46 +1100765 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100768 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -ENOMEM; /* May only call once for each order */
771 }
772
773 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100774 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EINVAL;
777 }
778
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
780 DRM_MEM_BUFS);
781 if (!entry->buflist) {
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 -ENOMEM;
785 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
789 DRM_MEM_SEGS);
790 if (!entry->seglist) {
791 drm_free(entry->buflist,
792 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100793 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -ENOMEM;
796 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* Keep the original pagelist until we know all the allocations
800 * have succeeded
801 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
803 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 drm_free(entry->buflist,
806 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
807 drm_free(entry->seglist,
808 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100809 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return -ENOMEM;
812 }
813 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
815 DRM_DEBUG("pagelist: %d entries\n",
816 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 entry->page_order = page_order;
820 byte_count = 0;
821 page_count = 0;
822
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000824
Dave Airlieddf19b92006-03-19 18:56:12 +1100825 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
Dave Airliebc5f4522007-11-05 12:50:58 +1000826
Dave Airlieddf19b92006-03-19 18:56:12 +1100827 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Set count correctly so we free the proper amount. */
829 entry->buf_count = count;
830 entry->seg_count = count;
831 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 drm_free(temp_pagelist,
833 (dma->page_count + (count << page_order))
834 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100835 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -ENOMEM;
838 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100839 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 for (i = 0; i < (1 << page_order); i++) {
841 DRM_DEBUG("page %d @ 0x%08lx\n",
842 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100843 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100845 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 for (offset = 0;
848 offset + size <= total && entry->buf_count < count;
849 offset += alignment, ++entry->buf_count) {
850 buf = &entry->buflist[entry->buf_count];
851 buf->idx = dma->buf_count + entry->buf_count;
852 buf->total = alignment;
853 buf->order = order;
854 buf->used = 0;
855 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100856 buf->address = (void *)(dmah->vaddr + offset);
857 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 buf->waiting = 0;
860 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000862 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 buf->dev_private = drm_alloc(buf->dev_priv_size,
866 DRM_MEM_BUFS);
867 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Set count correctly so we free the proper amount. */
869 entry->buf_count = count;
870 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 drm_cleanup_buf_error(dev, entry);
872 drm_free(temp_pagelist,
873 (dma->page_count +
874 (count << page_order))
875 * sizeof(*dma->pagelist),
876 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100877 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -ENOMEM;
880 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000883 DRM_DEBUG("buffer %d @ %p\n",
884 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 byte_count += PAGE_SIZE << page_order;
887 }
888
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 temp_buflist = drm_realloc(dma->buflist,
890 dma->buf_count * sizeof(*dma->buflist),
891 (dma->buf_count + entry->buf_count)
892 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!temp_buflist) {
894 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 drm_cleanup_buf_error(dev, entry);
896 drm_free(temp_pagelist,
897 (dma->page_count + (count << page_order))
898 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100899 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -ENOMEM;
902 }
903 dma->buflist = temp_buflist;
904
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
907 }
908
909 /* No allocations failed, so now we can replace the orginal pagelist
910 * with the new one.
911 */
912 if (dma->page_count) {
913 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 dma->page_count * sizeof(*dma->pagelist),
915 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 dma->pagelist = temp_pagelist;
918
919 dma->buf_count += entry->buf_count;
920 dma->seg_count += entry->seg_count;
921 dma->page_count += entry->seg_count << page_order;
922 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
923
Dave Airlie30e2fb12006-02-02 19:37:46 +1100924 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Dave Airlied59431b2005-07-10 15:00:06 +1000926 request->count = entry->buf_count;
927 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
George Sapountzis3417f332006-10-24 12:03:04 -0700929 if (request->flags & _DRM_PCI_BUFFER_RO)
930 dma->flags = _DRM_DMA_USE_PCI_RO;
931
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000932 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
934
935}
Dave Airlied84f76d2005-07-10 17:04:22 +1000936EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dave Airlie84b1fd12007-07-11 15:53:27 +1000938static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Dave Airliecdd55a22007-07-11 16:32:08 +1000940 struct drm_device_dma *dma = dev->dma;
941 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000942 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 unsigned long offset;
944 unsigned long agp_offset;
945 int count;
946 int order;
947 int size;
948 int alignment;
949 int page_order;
950 int total;
951 int byte_count;
952 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000953 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 if (!drm_core_check_feature(dev, DRIVER_SG))
956 return -EINVAL;
957
958 if (!dma)
959 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dave Airlied985c102006-01-02 21:32:48 +1100961 if (!capable(CAP_SYS_ADMIN))
962 return -EPERM;
963
Dave Airlied59431b2005-07-10 15:00:06 +1000964 count = request->count;
965 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 size = 1 << order;
967
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 alignment = (request->flags & _DRM_PAGE_ALIGN)
969 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
971 total = PAGE_SIZE << page_order;
972
973 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000974 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 DRM_DEBUG("count: %d\n", count);
977 DRM_DEBUG("order: %d\n", order);
978 DRM_DEBUG("size: %d\n", size);
979 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
980 DRM_DEBUG("alignment: %d\n", alignment);
981 DRM_DEBUG("page_order: %d\n", page_order);
982 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
985 return -EINVAL;
986 if (dev->queue_count)
987 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 spin_lock(&dev->count_lock);
990 if (dev->buf_use) {
991 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -EBUSY;
993 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 atomic_inc(&dev->buf_alloc);
995 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Airlie30e2fb12006-02-02 19:37:46 +1100997 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001000 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 atomic_dec(&dev->buf_alloc);
1002 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
1005 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001006 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EINVAL;
1009 }
1010
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1012 DRM_MEM_BUFS);
1013 if (!entry->buflist) {
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 -ENOMEM;
1017 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 entry->buf_size = size;
1021 entry->page_order = page_order;
1022
1023 offset = 0;
1024
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 while (entry->buf_count < count) {
1026 buf = &entry->buflist[entry->buf_count];
1027 buf->idx = dma->buf_count + entry->buf_count;
1028 buf->total = alignment;
1029 buf->order = order;
1030 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001035 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 buf->waiting = 0;
1038 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001040 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1044 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* Set count correctly so we free the proper amount. */
1046 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001048 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return -ENOMEM;
1051 }
1052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 offset += alignment;
1058 entry->buf_count++;
1059 byte_count += PAGE_SIZE << page_order;
1060 }
1061
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 temp_buflist = drm_realloc(dma->buflist,
1065 dma->buf_count * sizeof(*dma->buflist),
1066 (dma->buf_count + entry->buf_count)
1067 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1068 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001071 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return -ENOMEM;
1074 }
1075 dma->buflist = temp_buflist;
1076
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1079 }
1080
1081 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001082 dma->seg_count += entry->seg_count;
1083 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 dma->byte_count += byte_count;
1085
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1087 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Dave Airlie30e2fb12006-02-02 19:37:46 +11001089 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dave Airlied59431b2005-07-10 15:00:06 +10001091 request->count = entry->buf_count;
1092 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 dma->flags = _DRM_DMA_USE_SG;
1095
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
Dave Airlie84b1fd12007-07-11 15:53:27 +10001100static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001101{
Dave Airliecdd55a22007-07-11 16:32:08 +10001102 struct drm_device_dma *dma = dev->dma;
1103 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001104 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001105 unsigned long offset;
1106 unsigned long agp_offset;
1107 int count;
1108 int order;
1109 int size;
1110 int alignment;
1111 int page_order;
1112 int total;
1113 int byte_count;
1114 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001115 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001116
1117 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1118 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001119
Dave Airlieb84397d62005-07-10 14:46:12 +10001120 if (!dma)
1121 return -EINVAL;
1122
Dave Airlied985c102006-01-02 21:32:48 +11001123 if (!capable(CAP_SYS_ADMIN))
1124 return -EPERM;
1125
Dave Airlied59431b2005-07-10 15:00:06 +10001126 count = request->count;
1127 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001128 size = 1 << order;
1129
Dave Airlied59431b2005-07-10 15:00:06 +10001130 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001131 ? PAGE_ALIGN(size) : size;
1132 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1133 total = PAGE_SIZE << page_order;
1134
1135 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001136 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001137
1138 DRM_DEBUG("count: %d\n", count);
1139 DRM_DEBUG("order: %d\n", order);
1140 DRM_DEBUG("size: %d\n", size);
1141 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1142 DRM_DEBUG("alignment: %d\n", alignment);
1143 DRM_DEBUG("page_order: %d\n", page_order);
1144 DRM_DEBUG("total: %d\n", total);
1145
1146 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1147 return -EINVAL;
1148 if (dev->queue_count)
1149 return -EBUSY; /* Not while in use */
1150
1151 spin_lock(&dev->count_lock);
1152 if (dev->buf_use) {
1153 spin_unlock(&dev->count_lock);
1154 return -EBUSY;
1155 }
1156 atomic_inc(&dev->buf_alloc);
1157 spin_unlock(&dev->count_lock);
1158
Dave Airlie30e2fb12006-02-02 19:37:46 +11001159 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001160 entry = &dma->bufs[order];
1161 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001162 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001163 atomic_dec(&dev->buf_alloc);
1164 return -ENOMEM; /* May only call once for each order */
1165 }
1166
1167 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001168 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001169 atomic_dec(&dev->buf_alloc);
1170 return -EINVAL;
1171 }
1172
1173 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1174 DRM_MEM_BUFS);
1175 if (!entry->buflist) {
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 -ENOMEM;
1179 }
1180 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1181
1182 entry->buf_size = size;
1183 entry->page_order = page_order;
1184
1185 offset = 0;
1186
1187 while (entry->buf_count < count) {
1188 buf = &entry->buflist[entry->buf_count];
1189 buf->idx = dma->buf_count + entry->buf_count;
1190 buf->total = alignment;
1191 buf->order = order;
1192 buf->used = 0;
1193
1194 buf->offset = (dma->byte_count + offset);
1195 buf->bus_address = agp_offset + offset;
1196 buf->address = (void *)(agp_offset + offset);
1197 buf->next = NULL;
1198 buf->waiting = 0;
1199 buf->pending = 0;
1200 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001201 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001202
1203 buf->dev_priv_size = dev->driver->dev_priv_size;
1204 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1205 if (!buf->dev_private) {
1206 /* Set count correctly so we free the proper amount. */
1207 entry->buf_count = count;
1208 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001209 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001210 atomic_dec(&dev->buf_alloc);
1211 return -ENOMEM;
1212 }
1213 memset(buf->dev_private, 0, buf->dev_priv_size);
1214
1215 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1216
1217 offset += alignment;
1218 entry->buf_count++;
1219 byte_count += PAGE_SIZE << page_order;
1220 }
1221
1222 DRM_DEBUG("byte_count: %d\n", byte_count);
1223
1224 temp_buflist = drm_realloc(dma->buflist,
1225 dma->buf_count * sizeof(*dma->buflist),
1226 (dma->buf_count + entry->buf_count)
1227 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1228 if (!temp_buflist) {
1229 /* Free the entry because it isn't valid */
1230 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001231 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001232 atomic_dec(&dev->buf_alloc);
1233 return -ENOMEM;
1234 }
1235 dma->buflist = temp_buflist;
1236
1237 for (i = 0; i < entry->buf_count; i++) {
1238 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1239 }
1240
1241 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001242 dma->seg_count += entry->seg_count;
1243 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001244 dma->byte_count += byte_count;
1245
1246 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1247 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1248
Dave Airlie30e2fb12006-02-02 19:37:46 +11001249 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001250
Dave Airlied59431b2005-07-10 15:00:06 +10001251 request->count = entry->buf_count;
1252 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001253
1254 dma->flags = _DRM_DMA_USE_FB;
1255
1256 atomic_dec(&dev->buf_alloc);
1257 return 0;
1258}
Dave Airlied985c102006-01-02 21:32:48 +11001259
Dave Airlieb84397d62005-07-10 14:46:12 +10001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261/**
1262 * Add buffers for DMA transfers (ioctl).
1263 *
1264 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001265 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001267 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * \return zero on success or a negative number on failure.
1269 *
1270 * According with the memory type specified in drm_buf_desc::flags and the
1271 * build options, it dispatches the call either to addbufs_agp(),
1272 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1273 * PCI memory respectively.
1274 */
Eric Anholtc153f452007-09-03 12:06:45 +10001275int drm_addbufs(struct drm_device *dev, void *data,
1276 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Eric Anholtc153f452007-09-03 12:06:45 +10001278 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001279 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1282 return -EINVAL;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001285 if (request->flags & _DRM_AGP_BUFFER)
1286 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 else
1288#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001289 if (request->flags & _DRM_SG_BUFFER)
1290 ret = drm_addbufs_sg(dev, request);
1291 else if (request->flags & _DRM_FB_BUFFER)
1292 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 else
Eric Anholtc153f452007-09-03 12:06:45 +10001294 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001295
Dave Airlied59431b2005-07-10 15:00:06 +10001296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299/**
1300 * Get information about the buffer mappings.
1301 *
1302 * This was originally mean for debugging purposes, or by a sophisticated
1303 * client library to determine how best to use the available buffers (e.g.,
1304 * large buffers can be used for image transfer).
1305 *
1306 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001307 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * \param cmd command.
1309 * \param arg pointer to a drm_buf_info structure.
1310 * \return zero on success or a negative number on failure.
1311 *
1312 * Increments drm_device::buf_use while holding the drm_device::count_lock
1313 * lock, preventing of allocating more buffers after this call. Information
1314 * about each requested buffer is then copied into user space.
1315 */
Eric Anholtc153f452007-09-03 12:06:45 +10001316int drm_infobufs(struct drm_device *dev, void *data,
1317 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Dave Airliecdd55a22007-07-11 16:32:08 +10001319 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001320 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int i;
1322 int count;
1323
1324 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1325 return -EINVAL;
1326
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001327 if (!dma)
1328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330 spin_lock(&dev->count_lock);
1331 if (atomic_read(&dev->buf_alloc)) {
1332 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return -EBUSY;
1334 }
1335 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001336 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001338 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1339 if (dma->bufs[i].buf_count)
1340 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001343 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Eric Anholtc153f452007-09-03 12:06:45 +10001345 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001346 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1347 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001348 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001349 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001350 struct drm_buf_entry *from = &dma->bufs[i];
1351 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 if (copy_to_user(&to->count,
1353 &from->buf_count,
1354 sizeof(from->buf_count)) ||
1355 copy_to_user(&to->size,
1356 &from->buf_size,
1357 sizeof(from->buf_size)) ||
1358 copy_to_user(&to->low_mark,
1359 &list->low_mark,
1360 sizeof(list->low_mark)) ||
1361 copy_to_user(&to->high_mark,
1362 &list->high_mark,
1363 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EFAULT;
1365
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 DRM_DEBUG("%d %d %d %d %d\n",
1367 i,
1368 dma->bufs[i].buf_count,
1369 dma->bufs[i].buf_size,
1370 dma->bufs[i].freelist.low_mark,
1371 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 ++count;
1373 }
1374 }
1375 }
Eric Anholtc153f452007-09-03 12:06:45 +10001376 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 return 0;
1379}
1380
1381/**
1382 * Specifies a low and high water mark for buffer allocation
1383 *
1384 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001385 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * \param cmd command.
1387 * \param arg a pointer to a drm_buf_desc structure.
1388 * \return zero on success or a negative number on failure.
1389 *
1390 * Verifies that the size order is bounded between the admissible orders and
1391 * updates the respective drm_device_dma::bufs entry low and high water mark.
1392 *
1393 * \note This ioctl is deprecated and mostly never used.
1394 */
Eric Anholtc153f452007-09-03 12:06:45 +10001395int drm_markbufs(struct drm_device *dev, void *data,
1396 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Dave Airliecdd55a22007-07-11 16:32:08 +10001398 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001399 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001401 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1404 return -EINVAL;
1405
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 if (!dma)
1407 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001409 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001410 request->size, request->low_mark, request->high_mark);
1411 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1413 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 entry = &dma->bufs[order];
1415
Eric Anholtc153f452007-09-03 12:06:45 +10001416 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001418 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return -EINVAL;
1420
Eric Anholtc153f452007-09-03 12:06:45 +10001421 entry->freelist.low_mark = request->low_mark;
1422 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 return 0;
1425}
1426
1427/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001428 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 *
1430 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001431 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * \param cmd command.
1433 * \param arg pointer to a drm_buf_free structure.
1434 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001435 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 * Calls free_buffer() for each used buffer.
1437 * This function is primarily used for debugging.
1438 */
Eric Anholtc153f452007-09-03 12:06:45 +10001439int drm_freebufs(struct drm_device *dev, void *data,
1440 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Dave Airliecdd55a22007-07-11 16:32:08 +10001442 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001443 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int i;
1445 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001446 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1449 return -EINVAL;
1450
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 if (!dma)
1452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Eric Anholtc153f452007-09-03 12:06:45 +10001454 DRM_DEBUG("%d\n", request->count);
1455 for (i = 0; i < request->count; i++) {
1456 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001458 if (idx < 0 || idx >= dma->buf_count) {
1459 DRM_ERROR("Index %d (of %d max)\n",
1460 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EINVAL;
1462 }
1463 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001464 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001465 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001466 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EINVAL;
1468 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001469 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
1472 return 0;
1473}
1474
1475/**
1476 * Maps all of the DMA buffers into client-virtual space (ioctl).
1477 *
1478 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001479 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * \param cmd command.
1481 * \param arg pointer to a drm_buf_map structure.
1482 * \return zero on success or a negative number on failure.
1483 *
George Sapountzis3417f332006-10-24 12:03:04 -07001484 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1485 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1486 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1487 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Eric Anholtc153f452007-09-03 12:06:45 +10001489int drm_mapbufs(struct drm_device *dev, void *data,
1490 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Dave Airliecdd55a22007-07-11 16:32:08 +10001492 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int retcode = 0;
1494 const int zero = 0;
1495 unsigned long virtual;
1496 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001497 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 int i;
1499
1500 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1501 return -EINVAL;
1502
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 if (!dma)
1504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001506 spin_lock(&dev->count_lock);
1507 if (atomic_read(&dev->buf_alloc)) {
1508 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return -EBUSY;
1510 }
1511 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001512 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Eric Anholtc153f452007-09-03 12:06:45 +10001514 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001515 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001516 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001517 && (dma->flags & _DRM_DMA_USE_SG))
1518 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1519 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001520 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001521 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001523 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 retcode = -EINVAL;
1525 goto done;
1526 }
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, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001529 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001530 MAP_SHARED,
1531 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001532 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001535 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001536 PROT_READ | PROT_WRITE,
1537 MAP_SHARED, 0);
1538 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /* Real error */
1542 retcode = (signed long)virtual;
1543 goto done;
1544 }
Eric Anholtc153f452007-09-03 12:06:45 +10001545 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001547 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001548 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001549 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001550 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 retcode = -EFAULT;
1552 goto done;
1553 }
Eric Anholtc153f452007-09-03 12:06:45 +10001554 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001555 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001556 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 retcode = -EFAULT;
1558 goto done;
1559 }
Eric Anholtc153f452007-09-03 12:06:45 +10001560 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001561 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 retcode = -EFAULT;
1563 goto done;
1564 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001566 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001567 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 retcode = -EFAULT;
1569 goto done;
1570 }
1571 }
1572 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001573 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001574 request->count = dma->buf_count;
1575 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 return retcode;
1578}
1579
Dave Airlie836cf042005-07-10 19:27:04 +10001580/**
1581 * Compute size order. Returns the exponent of the smaller power of two which
1582 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 *
Dave Airlie836cf042005-07-10 19:27:04 +10001584 * \param size size.
1585 * \return order.
1586 *
1587 * \todo Can be made faster.
1588 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001590{
1591 int order;
1592 unsigned long tmp;
1593
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001594 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001595
1596 if (size & (size - 1))
1597 ++order;
1598
1599 return order;
1600}
1601EXPORT_SYMBOL(drm_order);