blob: 7fb690bcd492dafd60a4fe722d43543c6c150839 [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 Airlie7c1c2872008-11-28 14:22:24 +100057 if (entry->map && (entry->master == dev->primary->master) && (map->type == entry->map->type) &&
Dave Airlie54ba2f72007-02-10 12:07:47 +110058 ((entry->map->offset == map->offset) ||
Dave Airlie7c1c2872008-11-28 14:22:24 +100059 ((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 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000213 if (dev->primary->master->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 Airlie7c1c2872008-11-28 14:22:24 +1000218 dev->sigdata.lock = dev->primary->master->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;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800265 case _DRM_GEM:
266 DRM_ERROR("tried to rmmap GEM object\n");
267 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 case _DRM_SCATTER_GATHER:
270 if (!dev->sg) {
271 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
272 return -EINVAL;
273 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000274 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000277 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000278 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000279 * casting it down to 32 bits is no problem, but we
280 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000281 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
282 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000283 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
284 return -ENOMEM;
285 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000286 map->handle = dmah->vaddr;
287 map->offset = (unsigned long)dmah->busaddr;
288 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return -EINVAL;
293 }
294
295 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700297 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100298 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
300 return -EINVAL;
301 }
302 memset(list, 0, sizeof(*list));
303 list->map = map;
304
Dave Airlie30e2fb12006-02-02 19:37:46 +1100305 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000306 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000307
Dave Airlied1f2b552005-08-05 22:11:22 +1000308 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100309 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000310 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
311 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000312 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000313 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700314 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100315 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000316 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
317 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
318 mutex_unlock(&dev->struct_mutex);
319 return ret;
320 }
321
Thomas Hellstrom15450852007-02-08 16:14:05 +1100322 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100323 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Dave Airlie7c1c2872008-11-28 14:22:24 +1000325 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000326 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000327 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100328 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000329
Dave Airlie84b1fd12007-07-11 15:53:27 +1000330int drm_addmap(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000331 unsigned int size, enum drm_map_type type,
332 enum drm_map_flags flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000333{
Dave Airlie55910512007-07-11 16:53:40 +1000334 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000335 int rc;
336
337 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
338 if (!rc)
339 *map_ptr = list->map;
340 return rc;
341}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342
Dave Airlie7ab98402005-07-10 16:56:52 +1000343EXPORT_SYMBOL(drm_addmap);
344
Eric Anholtc153f452007-09-03 12:06:45 +1000345int drm_addmap_ioctl(struct drm_device *dev, void *data,
346 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000347{
Eric Anholtc153f452007-09-03 12:06:45 +1000348 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000349 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000350 int err;
351
Dave Airlie7c1c2872008-11-28 14:22:24 +1000352 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100353 return -EPERM;
354
Eric Anholtc153f452007-09-03 12:06:45 +1000355 err = drm_addmap_core(dev, map->offset, map->size, map->type,
356 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000359 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000360
Dave Airlie67e1a012005-10-24 18:41:39 +1000361 /* 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 +1000362 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/**
367 * Remove a map private from list and deallocate resources if the mapping
368 * isn't in use.
369 *
370 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000371 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +1000373 * \param arg pointer to a struct drm_map structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * \return zero on success or a negative value on error.
375 *
376 * Searches the map on drm_device::maplist, removes it from the list, see if
377 * its being used, and free any associate resource (such as MTRR's) if it's not
378 * being on use.
379 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000380 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000382int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Dave Airlie55910512007-07-11 16:53:40 +1000384 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000385 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000386 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000387 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Dave Airlie836cf042005-07-10 19:27:04 +1000389 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000390 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000391 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000392 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000393 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100394 drm_ht_remove_key(&dev->map_hash,
395 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000396 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
397 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Dave Airlie836cf042005-07-10 19:27:04 +1000401
Dave Airliebd1b3312007-05-26 05:01:51 +1000402 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000403 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000404
405 switch (map->type) {
406 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100407 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000408 /* FALLTHROUGH */
409 case _DRM_FRAME_BUFFER:
410 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
411 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 retcode = mtrr_del(map->mtrr, map->offset, map->size);
413 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000414 }
415 break;
416 case _DRM_SHM:
417 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000418 if (master) {
419 if (dev->sigdata.lock == master->lock.hw_lock)
420 dev->sigdata.lock = NULL;
421 master->lock.hw_lock = NULL; /* SHM removed */
422 master->lock.file_priv = NULL;
423 wake_up_interruptible(&master->lock.lock_queue);
424 }
Dave Airlie836cf042005-07-10 19:27:04 +1000425 break;
426 case _DRM_AGP:
427 case _DRM_SCATTER_GATHER:
428 break;
429 case _DRM_CONSISTENT:
430 dmah.vaddr = map->handle;
431 dmah.busaddr = map->offset;
432 dmah.size = map->size;
433 __drm_pci_free(dev, &dmah);
434 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800435 case _DRM_GEM:
436 DRM_ERROR("tried to rmmap GEM object\n");
437 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000438 }
439 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442}
Dave Airlie836cf042005-07-10 19:27:04 +1000443
Dave Airlie84b1fd12007-07-11 15:53:27 +1000444int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000445{
446 int ret;
447
Dave Airlie30e2fb12006-02-02 19:37:46 +1100448 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000449 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100450 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000451
452 return ret;
453}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000454EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000455
Dave Airlie836cf042005-07-10 19:27:04 +1000456/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
457 * the last close of the device, and this is necessary for cleanup when things
458 * exit uncleanly. Therefore, having userland manually remove mappings seems
459 * like a pointless exercise since they're going away anyway.
460 *
461 * One use case might be after addmap is allowed for normal users for SHM and
462 * gets used by drivers that the server doesn't need to care about. This seems
463 * unlikely.
464 */
Eric Anholtc153f452007-09-03 12:06:45 +1000465int drm_rmmap_ioctl(struct drm_device *dev, void *data,
466 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000467{
Eric Anholtc153f452007-09-03 12:06:45 +1000468 struct drm_map *request = data;
Dave Airlie836cf042005-07-10 19:27:04 +1000469 drm_local_map_t *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000470 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000471 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000472
Dave Airlie30e2fb12006-02-02 19:37:46 +1100473 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000474 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000475 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000476 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000477 r_list->map->flags & _DRM_REMOVABLE) {
478 map = r_list->map;
479 break;
480 }
481 }
482
483 /* List has wrapped around to the head pointer, or its empty we didn't
484 * find anything.
485 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000486 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100487 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000488 return -EINVAL;
489 }
490
Dave Airlie836cf042005-07-10 19:27:04 +1000491 /* Register and framebuffer maps are permanent */
492 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100493 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000494 return 0;
495 }
496
497 ret = drm_rmmap_locked(dev, map);
498
Dave Airlie30e2fb12006-02-02 19:37:46 +1100499 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000500
501 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000502}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504/**
505 * Cleanup after an error on one of the addbufs() functions.
506 *
Dave Airlie836cf042005-07-10 19:27:04 +1000507 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * \param entry buffer entry where the error occurred.
509 *
510 * Frees any pages and buffers associated with the given entry.
511 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000512static void drm_cleanup_buf_error(struct drm_device * dev,
513 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 int i;
516
517 if (entry->seg_count) {
518 for (i = 0; i < entry->seg_count; i++) {
519 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100520 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 }
523 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 entry->seg_count *
525 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 entry->seg_count = 0;
528 }
529
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 if (entry->buf_count) {
531 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (entry->buflist[i].dev_private) {
533 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 entry->buflist[i].dev_priv_size,
535 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 }
538 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 entry->buf_count *
540 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 entry->buf_count = 0;
543 }
544}
545
546#if __OS_HAS_AGP
547/**
Dave Airlied59431b2005-07-10 15:00:06 +1000548 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000550 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000551 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * After some sanity checks creates a drm_buf structure for each buffer and
555 * reallocates the buffer list of the same size order to accommodate the new
556 * buffers.
557 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000558int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Dave Airliecdd55a22007-07-11 16:32:08 +1000560 struct drm_device_dma *dma = dev->dma;
561 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000562 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000563 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 unsigned long offset;
565 unsigned long agp_offset;
566 int count;
567 int order;
568 int size;
569 int alignment;
570 int page_order;
571 int total;
572 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100573 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000574 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 if (!dma)
577 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Airlied59431b2005-07-10 15:00:06 +1000579 count = request->count;
580 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 size = 1 << order;
582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 alignment = (request->flags & _DRM_PAGE_ALIGN)
584 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
586 total = PAGE_SIZE << page_order;
587
588 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000589 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 DRM_DEBUG("count: %d\n", count);
592 DRM_DEBUG("order: %d\n", order);
593 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100594 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 DRM_DEBUG("alignment: %d\n", alignment);
596 DRM_DEBUG("page_order: %d\n", page_order);
597 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
600 return -EINVAL;
601 if (dev->queue_count)
602 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Dave Airlie54ba2f72007-02-10 12:07:47 +1100604 /* Make sure buffers are located in AGP memory that we own */
605 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000606 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100607 if ((agp_offset >= agp_entry->bound) &&
608 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
609 valid = 1;
610 break;
611 }
612 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000613 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100614 DRM_DEBUG("zone invalid\n");
615 return -EINVAL;
616 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 spin_lock(&dev->count_lock);
618 if (dev->buf_use) {
619 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EBUSY;
621 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 atomic_inc(&dev->buf_alloc);
623 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dave Airlie30e2fb12006-02-02 19:37:46 +1100625 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000627 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100628 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 atomic_dec(&dev->buf_alloc);
630 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
633 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100634 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EINVAL;
637 }
638
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
640 DRM_MEM_BUFS);
641 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100642 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -ENOMEM;
645 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 entry->buf_size = size;
649 entry->page_order = page_order;
650
651 offset = 0;
652
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 while (entry->buf_count < count) {
654 buf = &entry->buflist[entry->buf_count];
655 buf->idx = dma->buf_count + entry->buf_count;
656 buf->total = alignment;
657 buf->order = order;
658 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 buf->bus_address = agp_offset + offset;
662 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 buf->waiting = 0;
665 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000667 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
671 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* Set count correctly so we free the proper amount. */
673 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100675 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ENOMEM;
678 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 offset += alignment;
684 entry->buf_count++;
685 byte_count += PAGE_SIZE << page_order;
686 }
687
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 temp_buflist = drm_realloc(dma->buflist,
691 dma->buf_count * sizeof(*dma->buflist),
692 (dma->buf_count + entry->buf_count)
693 * sizeof(*dma->buflist), DRM_MEM_BUFS);
694 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100697 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -ENOMEM;
700 }
701 dma->buflist = temp_buflist;
702
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
705 }
706
707 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100708 dma->seg_count += entry->seg_count;
709 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 dma->byte_count += byte_count;
711
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
713 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dave Airlie30e2fb12006-02-02 19:37:46 +1100715 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Dave Airlied59431b2005-07-10 15:00:06 +1000717 request->count = entry->buf_count;
718 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 dma->flags = _DRM_DMA_USE_AGP;
721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 0;
724}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725EXPORT_SYMBOL(drm_addbufs_agp);
726#endif /* __OS_HAS_AGP */
727
Dave Airlie84b1fd12007-07-11 15:53:27 +1000728int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Dave Airliecdd55a22007-07-11 16:32:08 +1000730 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int count;
732 int order;
733 int size;
734 int total;
735 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000736 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100737 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000738 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int alignment;
740 unsigned long offset;
741 int i;
742 int byte_count;
743 int page_count;
744 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000745 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
748 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 if (!dma)
751 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Dave Airlied985c102006-01-02 21:32:48 +1100753 if (!capable(CAP_SYS_ADMIN))
754 return -EPERM;
755
Dave Airlied59431b2005-07-10 15:00:06 +1000756 count = request->count;
757 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 size = 1 << order;
759
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
761 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
764 return -EINVAL;
765 if (dev->queue_count)
766 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Dave Airlied59431b2005-07-10 15:00:06 +1000768 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
771 total = PAGE_SIZE << page_order;
772
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000773 spin_lock(&dev->count_lock);
774 if (dev->buf_use) {
775 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EBUSY;
777 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 atomic_inc(&dev->buf_alloc);
779 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Airlie30e2fb12006-02-02 19:37:46 +1100781 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100784 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000785 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -ENOMEM; /* May only call once for each order */
787 }
788
789 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100790 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -EINVAL;
793 }
794
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
796 DRM_MEM_BUFS);
797 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100798 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -ENOMEM;
801 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
805 DRM_MEM_SEGS);
806 if (!entry->seglist) {
807 drm_free(entry->buflist,
808 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
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 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Keep the original pagelist until we know all the allocations
816 * have succeeded
817 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
819 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 drm_free(entry->buflist,
822 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
823 drm_free(entry->seglist,
824 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100825 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -ENOMEM;
828 }
829 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
831 DRM_DEBUG("pagelist: %d entries\n",
832 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 entry->page_order = page_order;
836 byte_count = 0;
837 page_count = 0;
838
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000840
Dave Airlieddf19b92006-03-19 18:56:12 +1100841 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
Dave Airliebc5f4522007-11-05 12:50:58 +1000842
Dave Airlieddf19b92006-03-19 18:56:12 +1100843 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Set count correctly so we free the proper amount. */
845 entry->buf_count = count;
846 entry->seg_count = count;
847 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 drm_free(temp_pagelist,
849 (dma->page_count + (count << page_order))
850 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100851 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return -ENOMEM;
854 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100855 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 for (i = 0; i < (1 << page_order); i++) {
857 DRM_DEBUG("page %d @ 0x%08lx\n",
858 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100859 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100861 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 for (offset = 0;
864 offset + size <= total && entry->buf_count < count;
865 offset += alignment, ++entry->buf_count) {
866 buf = &entry->buflist[entry->buf_count];
867 buf->idx = dma->buf_count + entry->buf_count;
868 buf->total = alignment;
869 buf->order = order;
870 buf->used = 0;
871 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100872 buf->address = (void *)(dmah->vaddr + offset);
873 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 buf->waiting = 0;
876 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000878 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 buf->dev_private = drm_alloc(buf->dev_priv_size,
882 DRM_MEM_BUFS);
883 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Set count correctly so we free the proper amount. */
885 entry->buf_count = count;
886 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 drm_cleanup_buf_error(dev, entry);
888 drm_free(temp_pagelist,
889 (dma->page_count +
890 (count << page_order))
891 * sizeof(*dma->pagelist),
892 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100893 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -ENOMEM;
896 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 DRM_DEBUG("buffer %d @ %p\n",
900 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 byte_count += PAGE_SIZE << page_order;
903 }
904
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 temp_buflist = drm_realloc(dma->buflist,
906 dma->buf_count * sizeof(*dma->buflist),
907 (dma->buf_count + entry->buf_count)
908 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!temp_buflist) {
910 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 drm_cleanup_buf_error(dev, entry);
912 drm_free(temp_pagelist,
913 (dma->page_count + (count << page_order))
914 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100915 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -ENOMEM;
918 }
919 dma->buflist = temp_buflist;
920
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
923 }
924
925 /* No allocations failed, so now we can replace the orginal pagelist
926 * with the new one.
927 */
928 if (dma->page_count) {
929 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 dma->page_count * sizeof(*dma->pagelist),
931 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 dma->pagelist = temp_pagelist;
934
935 dma->buf_count += entry->buf_count;
936 dma->seg_count += entry->seg_count;
937 dma->page_count += entry->seg_count << page_order;
938 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
939
Dave Airlie30e2fb12006-02-02 19:37:46 +1100940 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dave Airlied59431b2005-07-10 15:00:06 +1000942 request->count = entry->buf_count;
943 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
George Sapountzis3417f332006-10-24 12:03:04 -0700945 if (request->flags & _DRM_PCI_BUFFER_RO)
946 dma->flags = _DRM_DMA_USE_PCI_RO;
947
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950
951}
Dave Airlied84f76d2005-07-10 17:04:22 +1000952EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dave Airlie84b1fd12007-07-11 15:53:27 +1000954static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Dave Airliecdd55a22007-07-11 16:32:08 +1000956 struct drm_device_dma *dma = dev->dma;
957 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000958 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 unsigned long offset;
960 unsigned long agp_offset;
961 int count;
962 int order;
963 int size;
964 int alignment;
965 int page_order;
966 int total;
967 int byte_count;
968 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000969 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 if (!drm_core_check_feature(dev, DRIVER_SG))
972 return -EINVAL;
973
974 if (!dma)
975 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dave Airlied985c102006-01-02 21:32:48 +1100977 if (!capable(CAP_SYS_ADMIN))
978 return -EPERM;
979
Dave Airlied59431b2005-07-10 15:00:06 +1000980 count = request->count;
981 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 size = 1 << order;
983
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 alignment = (request->flags & _DRM_PAGE_ALIGN)
985 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
987 total = PAGE_SIZE << page_order;
988
989 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000990 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 DRM_DEBUG("count: %d\n", count);
993 DRM_DEBUG("order: %d\n", order);
994 DRM_DEBUG("size: %d\n", size);
995 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
996 DRM_DEBUG("alignment: %d\n", alignment);
997 DRM_DEBUG("page_order: %d\n", page_order);
998 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1001 return -EINVAL;
1002 if (dev->queue_count)
1003 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 spin_lock(&dev->count_lock);
1006 if (dev->buf_use) {
1007 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EBUSY;
1009 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 atomic_inc(&dev->buf_alloc);
1011 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dave Airlie30e2fb12006-02-02 19:37:46 +11001013 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001016 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 atomic_dec(&dev->buf_alloc);
1018 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
1021 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001022 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -EINVAL;
1025 }
1026
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1028 DRM_MEM_BUFS);
1029 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001030 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -ENOMEM;
1033 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 entry->buf_size = size;
1037 entry->page_order = page_order;
1038
1039 offset = 0;
1040
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 while (entry->buf_count < count) {
1042 buf = &entry->buflist[entry->buf_count];
1043 buf->idx = dma->buf_count + entry->buf_count;
1044 buf->total = alignment;
1045 buf->order = order;
1046 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001051 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 buf->waiting = 0;
1054 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001056 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1060 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Set count correctly so we free the proper amount. */
1062 entry->buf_count = count;
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
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 offset += alignment;
1074 entry->buf_count++;
1075 byte_count += PAGE_SIZE << page_order;
1076 }
1077
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 temp_buflist = drm_realloc(dma->buflist,
1081 dma->buf_count * sizeof(*dma->buflist),
1082 (dma->buf_count + entry->buf_count)
1083 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1084 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001087 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001088 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return -ENOMEM;
1090 }
1091 dma->buflist = temp_buflist;
1092
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1095 }
1096
1097 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001098 dma->seg_count += entry->seg_count;
1099 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dma->byte_count += byte_count;
1101
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1103 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Dave Airlie30e2fb12006-02-02 19:37:46 +11001105 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Dave Airlied59431b2005-07-10 15:00:06 +10001107 request->count = entry->buf_count;
1108 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 dma->flags = _DRM_DMA_USE_SG;
1111
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return 0;
1114}
1115
Dave Airlie84b1fd12007-07-11 15:53:27 +10001116static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001117{
Dave Airliecdd55a22007-07-11 16:32:08 +10001118 struct drm_device_dma *dma = dev->dma;
1119 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001120 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001121 unsigned long offset;
1122 unsigned long agp_offset;
1123 int count;
1124 int order;
1125 int size;
1126 int alignment;
1127 int page_order;
1128 int total;
1129 int byte_count;
1130 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001131 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001132
1133 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1134 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001135
Dave Airlieb84397d62005-07-10 14:46:12 +10001136 if (!dma)
1137 return -EINVAL;
1138
Dave Airlied985c102006-01-02 21:32:48 +11001139 if (!capable(CAP_SYS_ADMIN))
1140 return -EPERM;
1141
Dave Airlied59431b2005-07-10 15:00:06 +10001142 count = request->count;
1143 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001144 size = 1 << order;
1145
Dave Airlied59431b2005-07-10 15:00:06 +10001146 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001147 ? PAGE_ALIGN(size) : size;
1148 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1149 total = PAGE_SIZE << page_order;
1150
1151 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001152 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001153
1154 DRM_DEBUG("count: %d\n", count);
1155 DRM_DEBUG("order: %d\n", order);
1156 DRM_DEBUG("size: %d\n", size);
1157 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1158 DRM_DEBUG("alignment: %d\n", alignment);
1159 DRM_DEBUG("page_order: %d\n", page_order);
1160 DRM_DEBUG("total: %d\n", total);
1161
1162 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1163 return -EINVAL;
1164 if (dev->queue_count)
1165 return -EBUSY; /* Not while in use */
1166
1167 spin_lock(&dev->count_lock);
1168 if (dev->buf_use) {
1169 spin_unlock(&dev->count_lock);
1170 return -EBUSY;
1171 }
1172 atomic_inc(&dev->buf_alloc);
1173 spin_unlock(&dev->count_lock);
1174
Dave Airlie30e2fb12006-02-02 19:37:46 +11001175 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001176 entry = &dma->bufs[order];
1177 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001178 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001179 atomic_dec(&dev->buf_alloc);
1180 return -ENOMEM; /* May only call once for each order */
1181 }
1182
1183 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001184 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001185 atomic_dec(&dev->buf_alloc);
1186 return -EINVAL;
1187 }
1188
1189 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1190 DRM_MEM_BUFS);
1191 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001192 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001193 atomic_dec(&dev->buf_alloc);
1194 return -ENOMEM;
1195 }
1196 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1197
1198 entry->buf_size = size;
1199 entry->page_order = page_order;
1200
1201 offset = 0;
1202
1203 while (entry->buf_count < count) {
1204 buf = &entry->buflist[entry->buf_count];
1205 buf->idx = dma->buf_count + entry->buf_count;
1206 buf->total = alignment;
1207 buf->order = order;
1208 buf->used = 0;
1209
1210 buf->offset = (dma->byte_count + offset);
1211 buf->bus_address = agp_offset + offset;
1212 buf->address = (void *)(agp_offset + offset);
1213 buf->next = NULL;
1214 buf->waiting = 0;
1215 buf->pending = 0;
1216 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001217 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001218
1219 buf->dev_priv_size = dev->driver->dev_priv_size;
1220 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1221 if (!buf->dev_private) {
1222 /* Set count correctly so we free the proper amount. */
1223 entry->buf_count = count;
1224 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001225 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001226 atomic_dec(&dev->buf_alloc);
1227 return -ENOMEM;
1228 }
1229 memset(buf->dev_private, 0, buf->dev_priv_size);
1230
1231 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1232
1233 offset += alignment;
1234 entry->buf_count++;
1235 byte_count += PAGE_SIZE << page_order;
1236 }
1237
1238 DRM_DEBUG("byte_count: %d\n", byte_count);
1239
1240 temp_buflist = drm_realloc(dma->buflist,
1241 dma->buf_count * sizeof(*dma->buflist),
1242 (dma->buf_count + entry->buf_count)
1243 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1244 if (!temp_buflist) {
1245 /* Free the entry because it isn't valid */
1246 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001247 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001248 atomic_dec(&dev->buf_alloc);
1249 return -ENOMEM;
1250 }
1251 dma->buflist = temp_buflist;
1252
1253 for (i = 0; i < entry->buf_count; i++) {
1254 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1255 }
1256
1257 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001258 dma->seg_count += entry->seg_count;
1259 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001260 dma->byte_count += byte_count;
1261
1262 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1263 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1264
Dave Airlie30e2fb12006-02-02 19:37:46 +11001265 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001266
Dave Airlied59431b2005-07-10 15:00:06 +10001267 request->count = entry->buf_count;
1268 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001269
1270 dma->flags = _DRM_DMA_USE_FB;
1271
1272 atomic_dec(&dev->buf_alloc);
1273 return 0;
1274}
Dave Airlied985c102006-01-02 21:32:48 +11001275
Dave Airlieb84397d62005-07-10 14:46:12 +10001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277/**
1278 * Add buffers for DMA transfers (ioctl).
1279 *
1280 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001281 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001283 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 * \return zero on success or a negative number on failure.
1285 *
1286 * According with the memory type specified in drm_buf_desc::flags and the
1287 * build options, it dispatches the call either to addbufs_agp(),
1288 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1289 * PCI memory respectively.
1290 */
Eric Anholtc153f452007-09-03 12:06:45 +10001291int drm_addbufs(struct drm_device *dev, void *data,
1292 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Eric Anholtc153f452007-09-03 12:06:45 +10001294 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001295 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1298 return -EINVAL;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001301 if (request->flags & _DRM_AGP_BUFFER)
1302 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 else
1304#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001305 if (request->flags & _DRM_SG_BUFFER)
1306 ret = drm_addbufs_sg(dev, request);
1307 else if (request->flags & _DRM_FB_BUFFER)
1308 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 else
Eric Anholtc153f452007-09-03 12:06:45 +10001310 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001311
Dave Airlied59431b2005-07-10 15:00:06 +10001312 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/**
1316 * Get information about the buffer mappings.
1317 *
1318 * This was originally mean for debugging purposes, or by a sophisticated
1319 * client library to determine how best to use the available buffers (e.g.,
1320 * large buffers can be used for image transfer).
1321 *
1322 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001323 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * \param cmd command.
1325 * \param arg pointer to a drm_buf_info structure.
1326 * \return zero on success or a negative number on failure.
1327 *
1328 * Increments drm_device::buf_use while holding the drm_device::count_lock
1329 * lock, preventing of allocating more buffers after this call. Information
1330 * about each requested buffer is then copied into user space.
1331 */
Eric Anholtc153f452007-09-03 12:06:45 +10001332int drm_infobufs(struct drm_device *dev, void *data,
1333 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Dave Airliecdd55a22007-07-11 16:32:08 +10001335 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001336 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 int i;
1338 int count;
1339
1340 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1341 return -EINVAL;
1342
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001343 if (!dma)
1344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001346 spin_lock(&dev->count_lock);
1347 if (atomic_read(&dev->buf_alloc)) {
1348 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -EBUSY;
1350 }
1351 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1355 if (dma->bufs[i].buf_count)
1356 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001359 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Eric Anholtc153f452007-09-03 12:06:45 +10001361 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1363 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001364 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001365 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001366 struct drm_buf_entry *from = &dma->bufs[i];
1367 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 if (copy_to_user(&to->count,
1369 &from->buf_count,
1370 sizeof(from->buf_count)) ||
1371 copy_to_user(&to->size,
1372 &from->buf_size,
1373 sizeof(from->buf_size)) ||
1374 copy_to_user(&to->low_mark,
1375 &list->low_mark,
1376 sizeof(list->low_mark)) ||
1377 copy_to_user(&to->high_mark,
1378 &list->high_mark,
1379 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return -EFAULT;
1381
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 DRM_DEBUG("%d %d %d %d %d\n",
1383 i,
1384 dma->bufs[i].buf_count,
1385 dma->bufs[i].buf_size,
1386 dma->bufs[i].freelist.low_mark,
1387 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ++count;
1389 }
1390 }
1391 }
Eric Anholtc153f452007-09-03 12:06:45 +10001392 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 return 0;
1395}
1396
1397/**
1398 * Specifies a low and high water mark for buffer allocation
1399 *
1400 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001401 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * \param cmd command.
1403 * \param arg a pointer to a drm_buf_desc structure.
1404 * \return zero on success or a negative number on failure.
1405 *
1406 * Verifies that the size order is bounded between the admissible orders and
1407 * updates the respective drm_device_dma::bufs entry low and high water mark.
1408 *
1409 * \note This ioctl is deprecated and mostly never used.
1410 */
Eric Anholtc153f452007-09-03 12:06:45 +10001411int drm_markbufs(struct drm_device *dev, void *data,
1412 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Dave Airliecdd55a22007-07-11 16:32:08 +10001414 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001415 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001417 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1420 return -EINVAL;
1421
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 if (!dma)
1423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001425 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001426 request->size, request->low_mark, request->high_mark);
1427 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001428 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 entry = &dma->bufs[order];
1431
Eric Anholtc153f452007-09-03 12:06:45 +10001432 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001434 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -EINVAL;
1436
Eric Anholtc153f452007-09-03 12:06:45 +10001437 entry->freelist.low_mark = request->low_mark;
1438 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 return 0;
1441}
1442
1443/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001444 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 *
1446 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001447 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 * \param cmd command.
1449 * \param arg pointer to a drm_buf_free structure.
1450 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * Calls free_buffer() for each used buffer.
1453 * This function is primarily used for debugging.
1454 */
Eric Anholtc153f452007-09-03 12:06:45 +10001455int drm_freebufs(struct drm_device *dev, void *data,
1456 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Dave Airliecdd55a22007-07-11 16:32:08 +10001458 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001459 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int i;
1461 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001462 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1465 return -EINVAL;
1466
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001467 if (!dma)
1468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Eric Anholtc153f452007-09-03 12:06:45 +10001470 DRM_DEBUG("%d\n", request->count);
1471 for (i = 0; i < request->count; i++) {
1472 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001474 if (idx < 0 || idx >= dma->buf_count) {
1475 DRM_ERROR("Index %d (of %d max)\n",
1476 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -EINVAL;
1478 }
1479 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001480 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001481 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001482 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EINVAL;
1484 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001485 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487
1488 return 0;
1489}
1490
1491/**
1492 * Maps all of the DMA buffers into client-virtual space (ioctl).
1493 *
1494 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001495 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * \param cmd command.
1497 * \param arg pointer to a drm_buf_map structure.
1498 * \return zero on success or a negative number on failure.
1499 *
George Sapountzis3417f332006-10-24 12:03:04 -07001500 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1501 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1502 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1503 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
Eric Anholtc153f452007-09-03 12:06:45 +10001505int drm_mapbufs(struct drm_device *dev, void *data,
1506 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Dave Airliecdd55a22007-07-11 16:32:08 +10001508 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 int retcode = 0;
1510 const int zero = 0;
1511 unsigned long virtual;
1512 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001513 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 int i;
1515
1516 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1517 return -EINVAL;
1518
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001519 if (!dma)
1520 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 spin_lock(&dev->count_lock);
1523 if (atomic_read(&dev->buf_alloc)) {
1524 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return -EBUSY;
1526 }
1527 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001528 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Eric Anholtc153f452007-09-03 12:06:45 +10001530 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001531 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001532 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001533 && (dma->flags & _DRM_DMA_USE_SG))
1534 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1535 && (dma->flags & _DRM_DMA_USE_FB))) {
Dave Airliec60ce622007-07-11 15:27:12 +10001536 struct drm_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001537 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001539 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 retcode = -EINVAL;
1541 goto done;
1542 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001543 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001544 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001545 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001546 MAP_SHARED,
1547 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001548 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001550 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001551 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001552 PROT_READ | PROT_WRITE,
1553 MAP_SHARED, 0);
1554 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001556 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* Real error */
1558 retcode = (signed long)virtual;
1559 goto done;
1560 }
Eric Anholtc153f452007-09-03 12:06:45 +10001561 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001564 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001566 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 retcode = -EFAULT;
1568 goto done;
1569 }
Eric Anholtc153f452007-09-03 12:06:45 +10001570 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001572 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 retcode = -EFAULT;
1574 goto done;
1575 }
Eric Anholtc153f452007-09-03 12:06:45 +10001576 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001577 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 retcode = -EFAULT;
1579 goto done;
1580 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001581 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001582 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 retcode = -EFAULT;
1585 goto done;
1586 }
1587 }
1588 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001590 request->count = dma->buf_count;
1591 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 return retcode;
1594}
1595
Dave Airlie836cf042005-07-10 19:27:04 +10001596/**
1597 * Compute size order. Returns the exponent of the smaller power of two which
1598 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001599 *
Dave Airlie836cf042005-07-10 19:27:04 +10001600 * \param size size.
1601 * \return order.
1602 *
1603 * \todo Can be made faster.
1604 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001605int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001606{
1607 int order;
1608 unsigned long tmp;
1609
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001610 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001611
1612 if (size & (size - 1))
1613 ++order;
1614
1615 return order;
1616}
1617EXPORT_SYMBOL(drm_order);