blob: 1b8dbd5961bc006716c9c8e9c308bc1aeda1f1c2 [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
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110039resource_size_t 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
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110045resource_size_t 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,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110053 struct drm_local_map *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/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110092 * Core function to create a range of memory available for mapping by a
93 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
95 * Adjusts the memory offset to its absolute value according to the mapping
96 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
97 * applicable and if supported by the kernel.
98 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100099static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000100 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000101 enum drm_map_flags flags,
102 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100104 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000105 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000106 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000107 unsigned long user_token;
108 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
111 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOMEM;
113
Dave Airlie7ab98402005-07-10 16:56:52 +1000114 map->offset = offset;
115 map->size = size;
116 map->flags = flags;
117 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* Only allow shared memory to be removable since we only keep enough
120 * book keeping information about shared memory to allow for removal
121 * when processes fork.
122 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
124 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -EINVAL;
126 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
128 map->offset, map->size, map->type);
129 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
130 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -EINVAL;
132 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 map->handle = NULL;
135
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000136 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 case _DRM_REGISTERS:
138 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000139#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100140 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 map->offset < virt_to_phys(high_memory)) {
142 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -EINVAL;
144 }
145#endif
146#ifdef __alpha__
147 map->offset += dev->hose->mem_space->start;
148#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000149 /* Some drivers preinitialize some maps, without the X Server
150 * needing to be aware of it. Therefore, we just return success
151 * when the server tries to create a duplicate map.
152 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000153 list = drm_find_matching_map(dev, map);
154 if (list != NULL) {
155 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000156 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157 "mismatched sizes, (%ld vs %ld)\n",
158 map->type, map->size,
159 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000160 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000161 }
162
163 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000164 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000165 return 0;
166 }
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 if (map->type == _DRM_FRAME_BUFFER ||
170 (map->flags & _DRM_WRITE_COMBINING)) {
171 map->mtrr = mtrr_add(map->offset, map->size,
172 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 }
Scott Thompson0769d392007-08-25 18:17:49 +1000175 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100176 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000177 if (!map->handle) {
178 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
179 return -ENOMEM;
180 }
181 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100185 list = drm_find_matching_map(dev, map);
186 if (list != NULL) {
187 if(list->map->size != map->size) {
188 DRM_DEBUG("Matching maps of type %d with "
189 "mismatched sizes, (%ld vs %ld)\n",
190 map->type, map->size, list->map->size);
191 list->map->size = map->size;
192 }
193
194 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
195 *maplist = list;
196 return 0;
197 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100198 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 DRM_DEBUG("%lu %d %p\n",
200 map->size, drm_order(map->size), map->handle);
201 if (!map->handle) {
202 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -ENOMEM;
204 }
205 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000208 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 vfree(map->handle);
210 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -EBUSY;
212 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000213 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100216 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000217 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100218 int valid = 0;
219
220 if (!drm_core_has_AGP(dev)) {
221 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
222 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100224#ifdef __alpha__
225 map->offset += dev->hose->mem_space->start;
226#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000227 /* In some cases (i810 driver), user space may have already
228 * added the AGP base itself, because dev->agp->base previously
229 * only got set during AGP enable. So, only add the base
230 * address if the map's offset isn't already within the
231 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100232 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000233 if (map->offset < dev->agp->base ||
234 map->offset > dev->agp->base +
235 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
236 map->offset += dev->agp->base;
237 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100238 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
239
240 /* This assumes the DRM is in total control of AGP space.
241 * It's not always the case as AGP can be in the control
242 * of user space (i.e. i810 driver). So this loop will get
243 * skipped and we double check that dev->agp->memory is
244 * actually set as well as being invalid before EPERM'ing
245 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000246 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100247 if ((map->offset >= entry->bound) &&
248 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
249 valid = 1;
250 break;
251 }
252 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000253 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100254 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
255 return -EPERM;
256 }
257 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800260 case _DRM_GEM:
261 DRM_ERROR("tried to rmmap GEM object\n");
262 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 case _DRM_SCATTER_GATHER:
265 if (!dev->sg) {
266 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
267 return -EINVAL;
268 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000269 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000272 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000273 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000274 * casting it down to 32 bits is no problem, but we
275 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000276 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
277 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000278 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
279 return -ENOMEM;
280 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000281 map->handle = dmah->vaddr;
282 map->offset = (unsigned long)dmah->busaddr;
283 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EINVAL;
288 }
289
290 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700292 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100293 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
295 return -EINVAL;
296 }
297 memset(list, 0, sizeof(*list));
298 list->map = map;
299
Dave Airlie30e2fb12006-02-02 19:37:46 +1100300 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000301 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000302
Dave Airlied1f2b552005-08-05 22:11:22 +1000303 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100304 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000305 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
306 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000307 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000308 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700309 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100310 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000311 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
312 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
313 mutex_unlock(&dev->struct_mutex);
314 return ret;
315 }
316
Thomas Hellstrom15450852007-02-08 16:14:05 +1100317 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100318 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Dave Airlie7c1c2872008-11-28 14:22:24 +1000320 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000321 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000322 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100323 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000324
Dave Airlie84b1fd12007-07-11 15:53:27 +1000325int drm_addmap(struct drm_device * dev, unsigned int offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000326 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100327 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000328{
Dave Airlie55910512007-07-11 16:53:40 +1000329 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000330 int rc;
331
332 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
333 if (!rc)
334 *map_ptr = list->map;
335 return rc;
336}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337
Dave Airlie7ab98402005-07-10 16:56:52 +1000338EXPORT_SYMBOL(drm_addmap);
339
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100340/**
341 * Ioctl to specify a range of memory that is available for mapping by a
342 * non-root process.
343 *
344 * \param inode device inode.
345 * \param file_priv DRM file private.
346 * \param cmd command.
347 * \param arg pointer to a drm_map structure.
348 * \return zero on success or a negative value on error.
349 *
350 */
Eric Anholtc153f452007-09-03 12:06:45 +1000351int drm_addmap_ioctl(struct drm_device *dev, void *data,
352 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000353{
Eric Anholtc153f452007-09-03 12:06:45 +1000354 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000355 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000356 int err;
357
Dave Airlie7c1c2872008-11-28 14:22:24 +1000358 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100359 return -EPERM;
360
Eric Anholtc153f452007-09-03 12:06:45 +1000361 err = drm_addmap_core(dev, map->offset, map->size, map->type,
362 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000363
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000365 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000366
Dave Airlie67e1a012005-10-24 18:41:39 +1000367 /* 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 +1000368 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/**
373 * Remove a map private from list and deallocate resources if the mapping
374 * isn't in use.
375 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * 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 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100382int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *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;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100423 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000424 }
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 Airlie4e74f362008-12-19 10:23:14 +1100443EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000444
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100445int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000446{
447 int ret;
448
Dave Airlie30e2fb12006-02-02 19:37:46 +1100449 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000450 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100451 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000452
453 return ret;
454}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000455EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000456
Dave Airlie836cf042005-07-10 19:27:04 +1000457/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
458 * the last close of the device, and this is necessary for cleanup when things
459 * exit uncleanly. Therefore, having userland manually remove mappings seems
460 * like a pointless exercise since they're going away anyway.
461 *
462 * One use case might be after addmap is allowed for normal users for SHM and
463 * gets used by drivers that the server doesn't need to care about. This seems
464 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100465 *
466 * \param inode device inode.
467 * \param file_priv DRM file private.
468 * \param cmd command.
469 * \param arg pointer to a struct drm_map structure.
470 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000471 */
Eric Anholtc153f452007-09-03 12:06:45 +1000472int drm_rmmap_ioctl(struct drm_device *dev, void *data,
473 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000474{
Eric Anholtc153f452007-09-03 12:06:45 +1000475 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100476 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000477 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000478 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000479
Dave Airlie30e2fb12006-02-02 19:37:46 +1100480 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000481 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000482 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000483 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000484 r_list->map->flags & _DRM_REMOVABLE) {
485 map = r_list->map;
486 break;
487 }
488 }
489
490 /* List has wrapped around to the head pointer, or its empty we didn't
491 * find anything.
492 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000493 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100494 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000495 return -EINVAL;
496 }
497
Dave Airlie836cf042005-07-10 19:27:04 +1000498 /* Register and framebuffer maps are permanent */
499 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100500 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000501 return 0;
502 }
503
504 ret = drm_rmmap_locked(dev, map);
505
Dave Airlie30e2fb12006-02-02 19:37:46 +1100506 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000507
508 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511/**
512 * Cleanup after an error on one of the addbufs() functions.
513 *
Dave Airlie836cf042005-07-10 19:27:04 +1000514 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * \param entry buffer entry where the error occurred.
516 *
517 * Frees any pages and buffers associated with the given entry.
518 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000519static void drm_cleanup_buf_error(struct drm_device * dev,
520 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 int i;
523
524 if (entry->seg_count) {
525 for (i = 0; i < entry->seg_count; i++) {
526 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100527 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 }
530 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 entry->seg_count *
532 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 entry->seg_count = 0;
535 }
536
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 if (entry->buf_count) {
538 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (entry->buflist[i].dev_private) {
540 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 entry->buflist[i].dev_priv_size,
542 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 }
545 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 entry->buf_count *
547 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 entry->buf_count = 0;
550 }
551}
552
553#if __OS_HAS_AGP
554/**
Dave Airlied59431b2005-07-10 15:00:06 +1000555 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000557 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000558 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * After some sanity checks creates a drm_buf structure for each buffer and
562 * reallocates the buffer list of the same size order to accommodate the new
563 * buffers.
564 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000565int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Dave Airliecdd55a22007-07-11 16:32:08 +1000567 struct drm_device_dma *dma = dev->dma;
568 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000569 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000570 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned long offset;
572 unsigned long agp_offset;
573 int count;
574 int order;
575 int size;
576 int alignment;
577 int page_order;
578 int total;
579 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100580 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000581 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 if (!dma)
584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dave Airlied59431b2005-07-10 15:00:06 +1000586 count = request->count;
587 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 size = 1 << order;
589
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 alignment = (request->flags & _DRM_PAGE_ALIGN)
591 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
593 total = PAGE_SIZE << page_order;
594
595 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000596 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 DRM_DEBUG("count: %d\n", count);
599 DRM_DEBUG("order: %d\n", order);
600 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100601 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 DRM_DEBUG("alignment: %d\n", alignment);
603 DRM_DEBUG("page_order: %d\n", page_order);
604 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
607 return -EINVAL;
608 if (dev->queue_count)
609 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Dave Airlie54ba2f72007-02-10 12:07:47 +1100611 /* Make sure buffers are located in AGP memory that we own */
612 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000613 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100614 if ((agp_offset >= agp_entry->bound) &&
615 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
616 valid = 1;
617 break;
618 }
619 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000620 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100621 DRM_DEBUG("zone invalid\n");
622 return -EINVAL;
623 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 spin_lock(&dev->count_lock);
625 if (dev->buf_use) {
626 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -EBUSY;
628 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 atomic_inc(&dev->buf_alloc);
630 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Dave Airlie30e2fb12006-02-02 19:37:46 +1100632 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100635 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 atomic_dec(&dev->buf_alloc);
637 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100641 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EINVAL;
644 }
645
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
647 DRM_MEM_BUFS);
648 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100649 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -ENOMEM;
652 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 entry->buf_size = size;
656 entry->page_order = page_order;
657
658 offset = 0;
659
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 while (entry->buf_count < count) {
661 buf = &entry->buflist[entry->buf_count];
662 buf->idx = dma->buf_count + entry->buf_count;
663 buf->total = alignment;
664 buf->order = order;
665 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 buf->bus_address = agp_offset + offset;
669 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 buf->waiting = 0;
672 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000674 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
678 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* Set count correctly so we free the proper amount. */
680 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100682 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -ENOMEM;
685 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 offset += alignment;
691 entry->buf_count++;
692 byte_count += PAGE_SIZE << page_order;
693 }
694
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 temp_buflist = drm_realloc(dma->buflist,
698 dma->buf_count * sizeof(*dma->buflist),
699 (dma->buf_count + entry->buf_count)
700 * sizeof(*dma->buflist), DRM_MEM_BUFS);
701 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100704 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -ENOMEM;
707 }
708 dma->buflist = temp_buflist;
709
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
712 }
713
714 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100715 dma->seg_count += entry->seg_count;
716 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 dma->byte_count += byte_count;
718
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
720 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Airlie30e2fb12006-02-02 19:37:46 +1100722 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Dave Airlied59431b2005-07-10 15:00:06 +1000724 request->count = entry->buf_count;
725 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 dma->flags = _DRM_DMA_USE_AGP;
728
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 0;
731}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732EXPORT_SYMBOL(drm_addbufs_agp);
733#endif /* __OS_HAS_AGP */
734
Dave Airlie84b1fd12007-07-11 15:53:27 +1000735int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Dave Airliecdd55a22007-07-11 16:32:08 +1000737 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int count;
739 int order;
740 int size;
741 int total;
742 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000743 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100744 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000745 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int alignment;
747 unsigned long offset;
748 int i;
749 int byte_count;
750 int page_count;
751 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000752 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000754 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
755 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100756
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 if (!dma)
758 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Airlied985c102006-01-02 21:32:48 +1100760 if (!capable(CAP_SYS_ADMIN))
761 return -EPERM;
762
Dave Airlied59431b2005-07-10 15:00:06 +1000763 count = request->count;
764 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 size = 1 << order;
766
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
768 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
771 return -EINVAL;
772 if (dev->queue_count)
773 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dave Airlied59431b2005-07-10 15:00:06 +1000775 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
778 total = PAGE_SIZE << page_order;
779
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 spin_lock(&dev->count_lock);
781 if (dev->buf_use) {
782 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return -EBUSY;
784 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000785 atomic_inc(&dev->buf_alloc);
786 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Dave Airlie30e2fb12006-02-02 19:37:46 +1100788 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100791 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -ENOMEM; /* May only call once for each order */
794 }
795
796 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100797 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -EINVAL;
800 }
801
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
803 DRM_MEM_BUFS);
804 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100805 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -ENOMEM;
808 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
812 DRM_MEM_SEGS);
813 if (!entry->seglist) {
814 drm_free(entry->buflist,
815 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100816 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -ENOMEM;
819 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* Keep the original pagelist until we know all the allocations
823 * have succeeded
824 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
826 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 drm_free(entry->buflist,
829 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
830 drm_free(entry->seglist,
831 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100832 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -ENOMEM;
835 }
836 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
838 DRM_DEBUG("pagelist: %d entries\n",
839 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 entry->page_order = page_order;
843 byte_count = 0;
844 page_count = 0;
845
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000847
Dave Airlieddf19b92006-03-19 18:56:12 +1100848 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
Dave Airliebc5f4522007-11-05 12:50:58 +1000849
Dave Airlieddf19b92006-03-19 18:56:12 +1100850 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* Set count correctly so we free the proper amount. */
852 entry->buf_count = count;
853 entry->seg_count = count;
854 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 drm_free(temp_pagelist,
856 (dma->page_count + (count << page_order))
857 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100858 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -ENOMEM;
861 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100862 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 for (i = 0; i < (1 << page_order); i++) {
864 DRM_DEBUG("page %d @ 0x%08lx\n",
865 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100866 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100868 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 for (offset = 0;
871 offset + size <= total && entry->buf_count < count;
872 offset += alignment, ++entry->buf_count) {
873 buf = &entry->buflist[entry->buf_count];
874 buf->idx = dma->buf_count + entry->buf_count;
875 buf->total = alignment;
876 buf->order = order;
877 buf->used = 0;
878 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100879 buf->address = (void *)(dmah->vaddr + offset);
880 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 buf->waiting = 0;
883 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000885 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 buf->dev_private = drm_alloc(buf->dev_priv_size,
889 DRM_MEM_BUFS);
890 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Set count correctly so we free the proper amount. */
892 entry->buf_count = count;
893 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 drm_cleanup_buf_error(dev, entry);
895 drm_free(temp_pagelist,
896 (dma->page_count +
897 (count << page_order))
898 * sizeof(*dma->pagelist),
899 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100900 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -ENOMEM;
903 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 DRM_DEBUG("buffer %d @ %p\n",
907 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 byte_count += PAGE_SIZE << page_order;
910 }
911
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 temp_buflist = drm_realloc(dma->buflist,
913 dma->buf_count * sizeof(*dma->buflist),
914 (dma->buf_count + entry->buf_count)
915 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!temp_buflist) {
917 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 drm_cleanup_buf_error(dev, entry);
919 drm_free(temp_pagelist,
920 (dma->page_count + (count << page_order))
921 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100922 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -ENOMEM;
925 }
926 dma->buflist = temp_buflist;
927
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
930 }
931
932 /* No allocations failed, so now we can replace the orginal pagelist
933 * with the new one.
934 */
935 if (dma->page_count) {
936 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000937 dma->page_count * sizeof(*dma->pagelist),
938 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 dma->pagelist = temp_pagelist;
941
942 dma->buf_count += entry->buf_count;
943 dma->seg_count += entry->seg_count;
944 dma->page_count += entry->seg_count << page_order;
945 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
946
Dave Airlie30e2fb12006-02-02 19:37:46 +1100947 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dave Airlied59431b2005-07-10 15:00:06 +1000949 request->count = entry->buf_count;
950 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
George Sapountzis3417f332006-10-24 12:03:04 -0700952 if (request->flags & _DRM_PCI_BUFFER_RO)
953 dma->flags = _DRM_DMA_USE_PCI_RO;
954
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957
958}
Dave Airlied84f76d2005-07-10 17:04:22 +1000959EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dave Airlie84b1fd12007-07-11 15:53:27 +1000961static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Dave Airliecdd55a22007-07-11 16:32:08 +1000963 struct drm_device_dma *dma = dev->dma;
964 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000965 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned long offset;
967 unsigned long agp_offset;
968 int count;
969 int order;
970 int size;
971 int alignment;
972 int page_order;
973 int total;
974 int byte_count;
975 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000976 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000978 if (!drm_core_check_feature(dev, DRIVER_SG))
979 return -EINVAL;
980
981 if (!dma)
982 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Airlied985c102006-01-02 21:32:48 +1100984 if (!capable(CAP_SYS_ADMIN))
985 return -EPERM;
986
Dave Airlied59431b2005-07-10 15:00:06 +1000987 count = request->count;
988 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 size = 1 << order;
990
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 alignment = (request->flags & _DRM_PAGE_ALIGN)
992 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
994 total = PAGE_SIZE << page_order;
995
996 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000997 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 DRM_DEBUG("count: %d\n", count);
1000 DRM_DEBUG("order: %d\n", order);
1001 DRM_DEBUG("size: %d\n", size);
1002 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1003 DRM_DEBUG("alignment: %d\n", alignment);
1004 DRM_DEBUG("page_order: %d\n", page_order);
1005 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1008 return -EINVAL;
1009 if (dev->queue_count)
1010 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 spin_lock(&dev->count_lock);
1013 if (dev->buf_use) {
1014 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return -EBUSY;
1016 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 atomic_inc(&dev->buf_alloc);
1018 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Dave Airlie30e2fb12006-02-02 19:37:46 +11001020 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001023 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 atomic_dec(&dev->buf_alloc);
1025 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
1028 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001029 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -EINVAL;
1032 }
1033
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1035 DRM_MEM_BUFS);
1036 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001037 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -ENOMEM;
1040 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 entry->buf_size = size;
1044 entry->page_order = page_order;
1045
1046 offset = 0;
1047
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 while (entry->buf_count < count) {
1049 buf = &entry->buflist[entry->buf_count];
1050 buf->idx = dma->buf_count + entry->buf_count;
1051 buf->total = alignment;
1052 buf->order = order;
1053 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001058 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 buf->waiting = 0;
1061 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001063 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1067 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* Set count correctly so we free the proper amount. */
1069 entry->buf_count = count;
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
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 offset += alignment;
1081 entry->buf_count++;
1082 byte_count += PAGE_SIZE << page_order;
1083 }
1084
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 temp_buflist = drm_realloc(dma->buflist,
1088 dma->buf_count * sizeof(*dma->buflist),
1089 (dma->buf_count + entry->buf_count)
1090 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1091 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001094 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return -ENOMEM;
1097 }
1098 dma->buflist = temp_buflist;
1099
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1102 }
1103
1104 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001105 dma->seg_count += entry->seg_count;
1106 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 dma->byte_count += byte_count;
1108
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1110 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Dave Airlie30e2fb12006-02-02 19:37:46 +11001112 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Dave Airlied59431b2005-07-10 15:00:06 +10001114 request->count = entry->buf_count;
1115 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 dma->flags = _DRM_DMA_USE_SG;
1118
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001119 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return 0;
1121}
1122
Dave Airlie84b1fd12007-07-11 15:53:27 +10001123static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001124{
Dave Airliecdd55a22007-07-11 16:32:08 +10001125 struct drm_device_dma *dma = dev->dma;
1126 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001127 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001128 unsigned long offset;
1129 unsigned long agp_offset;
1130 int count;
1131 int order;
1132 int size;
1133 int alignment;
1134 int page_order;
1135 int total;
1136 int byte_count;
1137 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001138 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001139
1140 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1141 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142
Dave Airlieb84397d62005-07-10 14:46:12 +10001143 if (!dma)
1144 return -EINVAL;
1145
Dave Airlied985c102006-01-02 21:32:48 +11001146 if (!capable(CAP_SYS_ADMIN))
1147 return -EPERM;
1148
Dave Airlied59431b2005-07-10 15:00:06 +10001149 count = request->count;
1150 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001151 size = 1 << order;
1152
Dave Airlied59431b2005-07-10 15:00:06 +10001153 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001154 ? PAGE_ALIGN(size) : size;
1155 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1156 total = PAGE_SIZE << page_order;
1157
1158 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001159 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001160
1161 DRM_DEBUG("count: %d\n", count);
1162 DRM_DEBUG("order: %d\n", order);
1163 DRM_DEBUG("size: %d\n", size);
1164 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1165 DRM_DEBUG("alignment: %d\n", alignment);
1166 DRM_DEBUG("page_order: %d\n", page_order);
1167 DRM_DEBUG("total: %d\n", total);
1168
1169 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1170 return -EINVAL;
1171 if (dev->queue_count)
1172 return -EBUSY; /* Not while in use */
1173
1174 spin_lock(&dev->count_lock);
1175 if (dev->buf_use) {
1176 spin_unlock(&dev->count_lock);
1177 return -EBUSY;
1178 }
1179 atomic_inc(&dev->buf_alloc);
1180 spin_unlock(&dev->count_lock);
1181
Dave Airlie30e2fb12006-02-02 19:37:46 +11001182 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001183 entry = &dma->bufs[order];
1184 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001185 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001186 atomic_dec(&dev->buf_alloc);
1187 return -ENOMEM; /* May only call once for each order */
1188 }
1189
1190 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001191 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001192 atomic_dec(&dev->buf_alloc);
1193 return -EINVAL;
1194 }
1195
1196 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1197 DRM_MEM_BUFS);
1198 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001199 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001200 atomic_dec(&dev->buf_alloc);
1201 return -ENOMEM;
1202 }
1203 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1204
1205 entry->buf_size = size;
1206 entry->page_order = page_order;
1207
1208 offset = 0;
1209
1210 while (entry->buf_count < count) {
1211 buf = &entry->buflist[entry->buf_count];
1212 buf->idx = dma->buf_count + entry->buf_count;
1213 buf->total = alignment;
1214 buf->order = order;
1215 buf->used = 0;
1216
1217 buf->offset = (dma->byte_count + offset);
1218 buf->bus_address = agp_offset + offset;
1219 buf->address = (void *)(agp_offset + offset);
1220 buf->next = NULL;
1221 buf->waiting = 0;
1222 buf->pending = 0;
1223 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001224 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001225
1226 buf->dev_priv_size = dev->driver->dev_priv_size;
1227 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1228 if (!buf->dev_private) {
1229 /* Set count correctly so we free the proper amount. */
1230 entry->buf_count = count;
1231 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001232 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001233 atomic_dec(&dev->buf_alloc);
1234 return -ENOMEM;
1235 }
1236 memset(buf->dev_private, 0, buf->dev_priv_size);
1237
1238 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1239
1240 offset += alignment;
1241 entry->buf_count++;
1242 byte_count += PAGE_SIZE << page_order;
1243 }
1244
1245 DRM_DEBUG("byte_count: %d\n", byte_count);
1246
1247 temp_buflist = drm_realloc(dma->buflist,
1248 dma->buf_count * sizeof(*dma->buflist),
1249 (dma->buf_count + entry->buf_count)
1250 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1251 if (!temp_buflist) {
1252 /* Free the entry because it isn't valid */
1253 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001254 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001255 atomic_dec(&dev->buf_alloc);
1256 return -ENOMEM;
1257 }
1258 dma->buflist = temp_buflist;
1259
1260 for (i = 0; i < entry->buf_count; i++) {
1261 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1262 }
1263
1264 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001265 dma->seg_count += entry->seg_count;
1266 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001267 dma->byte_count += byte_count;
1268
1269 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1270 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1271
Dave Airlie30e2fb12006-02-02 19:37:46 +11001272 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001273
Dave Airlied59431b2005-07-10 15:00:06 +10001274 request->count = entry->buf_count;
1275 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001276
1277 dma->flags = _DRM_DMA_USE_FB;
1278
1279 atomic_dec(&dev->buf_alloc);
1280 return 0;
1281}
Dave Airlied985c102006-01-02 21:32:48 +11001282
Dave Airlieb84397d62005-07-10 14:46:12 +10001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284/**
1285 * Add buffers for DMA transfers (ioctl).
1286 *
1287 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001288 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001290 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * \return zero on success or a negative number on failure.
1292 *
1293 * According with the memory type specified in drm_buf_desc::flags and the
1294 * build options, it dispatches the call either to addbufs_agp(),
1295 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1296 * PCI memory respectively.
1297 */
Eric Anholtc153f452007-09-03 12:06:45 +10001298int drm_addbufs(struct drm_device *dev, void *data,
1299 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Eric Anholtc153f452007-09-03 12:06:45 +10001301 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001302 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1305 return -EINVAL;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001308 if (request->flags & _DRM_AGP_BUFFER)
1309 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 else
1311#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001312 if (request->flags & _DRM_SG_BUFFER)
1313 ret = drm_addbufs_sg(dev, request);
1314 else if (request->flags & _DRM_FB_BUFFER)
1315 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 else
Eric Anholtc153f452007-09-03 12:06:45 +10001317 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001318
Dave Airlied59431b2005-07-10 15:00:06 +10001319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322/**
1323 * Get information about the buffer mappings.
1324 *
1325 * This was originally mean for debugging purposes, or by a sophisticated
1326 * client library to determine how best to use the available buffers (e.g.,
1327 * large buffers can be used for image transfer).
1328 *
1329 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001330 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * \param cmd command.
1332 * \param arg pointer to a drm_buf_info structure.
1333 * \return zero on success or a negative number on failure.
1334 *
1335 * Increments drm_device::buf_use while holding the drm_device::count_lock
1336 * lock, preventing of allocating more buffers after this call. Information
1337 * about each requested buffer is then copied into user space.
1338 */
Eric Anholtc153f452007-09-03 12:06:45 +10001339int drm_infobufs(struct drm_device *dev, void *data,
1340 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Dave Airliecdd55a22007-07-11 16:32:08 +10001342 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001343 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int i;
1345 int count;
1346
1347 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1348 return -EINVAL;
1349
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001350 if (!dma)
1351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001353 spin_lock(&dev->count_lock);
1354 if (atomic_read(&dev->buf_alloc)) {
1355 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EBUSY;
1357 }
1358 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001359 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001361 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1362 if (dma->bufs[i].buf_count)
1363 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Eric Anholtc153f452007-09-03 12:06:45 +10001368 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001369 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1370 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001371 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001372 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001373 struct drm_buf_entry *from = &dma->bufs[i];
1374 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001375 if (copy_to_user(&to->count,
1376 &from->buf_count,
1377 sizeof(from->buf_count)) ||
1378 copy_to_user(&to->size,
1379 &from->buf_size,
1380 sizeof(from->buf_size)) ||
1381 copy_to_user(&to->low_mark,
1382 &list->low_mark,
1383 sizeof(list->low_mark)) ||
1384 copy_to_user(&to->high_mark,
1385 &list->high_mark,
1386 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
1388
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 DRM_DEBUG("%d %d %d %d %d\n",
1390 i,
1391 dma->bufs[i].buf_count,
1392 dma->bufs[i].buf_size,
1393 dma->bufs[i].freelist.low_mark,
1394 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 ++count;
1396 }
1397 }
1398 }
Eric Anholtc153f452007-09-03 12:06:45 +10001399 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return 0;
1402}
1403
1404/**
1405 * Specifies a low and high water mark for buffer allocation
1406 *
1407 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001408 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * \param cmd command.
1410 * \param arg a pointer to a drm_buf_desc structure.
1411 * \return zero on success or a negative number on failure.
1412 *
1413 * Verifies that the size order is bounded between the admissible orders and
1414 * updates the respective drm_device_dma::bufs entry low and high water mark.
1415 *
1416 * \note This ioctl is deprecated and mostly never used.
1417 */
Eric Anholtc153f452007-09-03 12:06:45 +10001418int drm_markbufs(struct drm_device *dev, void *data,
1419 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Dave Airliecdd55a22007-07-11 16:32:08 +10001421 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001422 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001424 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1427 return -EINVAL;
1428
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001429 if (!dma)
1430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001432 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001433 request->size, request->low_mark, request->high_mark);
1434 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001435 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 entry = &dma->bufs[order];
1438
Eric Anholtc153f452007-09-03 12:06:45 +10001439 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001441 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EINVAL;
1443
Eric Anholtc153f452007-09-03 12:06:45 +10001444 entry->freelist.low_mark = request->low_mark;
1445 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 return 0;
1448}
1449
1450/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *
1453 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001454 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * \param cmd command.
1456 * \param arg pointer to a drm_buf_free structure.
1457 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001458 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * Calls free_buffer() for each used buffer.
1460 * This function is primarily used for debugging.
1461 */
Eric Anholtc153f452007-09-03 12:06:45 +10001462int drm_freebufs(struct drm_device *dev, void *data,
1463 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Dave Airliecdd55a22007-07-11 16:32:08 +10001465 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001466 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int i;
1468 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001469 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1472 return -EINVAL;
1473
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001474 if (!dma)
1475 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Eric Anholtc153f452007-09-03 12:06:45 +10001477 DRM_DEBUG("%d\n", request->count);
1478 for (i = 0; i < request->count; i++) {
1479 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001481 if (idx < 0 || idx >= dma->buf_count) {
1482 DRM_ERROR("Index %d (of %d max)\n",
1483 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -EINVAL;
1485 }
1486 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001487 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001488 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001489 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return -EINVAL;
1491 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001492 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
1495 return 0;
1496}
1497
1498/**
1499 * Maps all of the DMA buffers into client-virtual space (ioctl).
1500 *
1501 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001502 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 * \param cmd command.
1504 * \param arg pointer to a drm_buf_map structure.
1505 * \return zero on success or a negative number on failure.
1506 *
George Sapountzis3417f332006-10-24 12:03:04 -07001507 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1508 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1509 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1510 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
Eric Anholtc153f452007-09-03 12:06:45 +10001512int drm_mapbufs(struct drm_device *dev, void *data,
1513 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Dave Airliecdd55a22007-07-11 16:32:08 +10001515 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int retcode = 0;
1517 const int zero = 0;
1518 unsigned long virtual;
1519 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001520 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 int i;
1522
1523 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1524 return -EINVAL;
1525
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526 if (!dma)
1527 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001529 spin_lock(&dev->count_lock);
1530 if (atomic_read(&dev->buf_alloc)) {
1531 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return -EBUSY;
1533 }
1534 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001535 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Eric Anholtc153f452007-09-03 12:06:45 +10001537 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001538 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001539 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001540 && (dma->flags & _DRM_DMA_USE_SG))
1541 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1542 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001543 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001544 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001546 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 retcode = -EINVAL;
1548 goto done;
1549 }
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, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001552 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001553 MAP_SHARED,
1554 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001555 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001558 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001559 PROT_READ | PROT_WRITE,
1560 MAP_SHARED, 0);
1561 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Real error */
1565 retcode = (signed long)virtual;
1566 goto done;
1567 }
Eric Anholtc153f452007-09-03 12:06:45 +10001568 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001570 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001571 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001572 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001573 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 retcode = -EFAULT;
1575 goto done;
1576 }
Eric Anholtc153f452007-09-03 12:06:45 +10001577 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001579 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 retcode = -EFAULT;
1581 goto done;
1582 }
Eric Anholtc153f452007-09-03 12:06:45 +10001583 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 retcode = -EFAULT;
1586 goto done;
1587 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001588 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001589 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001590 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 retcode = -EFAULT;
1592 goto done;
1593 }
1594 }
1595 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001597 request->count = dma->buf_count;
1598 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 return retcode;
1601}
1602
Dave Airlie836cf042005-07-10 19:27:04 +10001603/**
1604 * Compute size order. Returns the exponent of the smaller power of two which
1605 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001606 *
Dave Airlie836cf042005-07-10 19:27:04 +10001607 * \param size size.
1608 * \return order.
1609 *
1610 * \todo Can be made faster.
1611 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001612int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001613{
1614 int order;
1615 unsigned long tmp;
1616
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001617 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001618
1619 if (size & (size - 1))
1620 ++order;
1621
1622 return order;
1623}
1624EXPORT_SYMBOL(drm_order);