blob: cddea1a2472c8abd41370b38f0759a8a3d86a0cf [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) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110057 /*
58 * Because the kernel-userspace ABI is fixed at a 32-bit offset
59 * while PCI resources may live above that, we ignore the map
60 * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
61 * It is assumed that each driver will have only one resource of
62 * each type.
63 */
64 if (!entry->map ||
65 map->type != entry->map->type ||
66 entry->master != dev->primary->master)
67 continue;
68 switch (map->type) {
69 case _DRM_SHM:
70 if (map->flags != _DRM_CONTAINS_LOCK)
71 break;
72 case _DRM_REGISTERS:
73 case _DRM_FRAME_BUFFER:
Dave Airlie89625eb2005-09-05 21:23:23 +100074 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110075 default: /* Make gcc happy */
76 ;
Dave Airlie836cf042005-07-10 19:27:04 +100077 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110078 if (entry->map->offset == map->offset)
79 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100080 }
81
82 return NULL;
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dave Airliee0be4282007-07-12 10:26:44 +100085static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
Adrian Bunkfb41e542006-08-16 11:55:18 +100086 unsigned long user_token, int hashed_handle)
Dave Airlied1f2b552005-08-05 22:11:22 +100087{
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100088 int use_hashed_handle;
Dave Airliec2604ce2006-08-12 16:03:26 +100089#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100090 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
91#elif (BITS_PER_LONG == 32)
92 use_hashed_handle = hashed_handle;
93#else
94#error Unsupported long size. Neither 64 nor 32 bits.
95#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100096
Thomas Hellstrome08870c2006-09-22 04:18:37 +100097 if (!use_hashed_handle) {
98 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110099 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000100 ret = drm_ht_insert_item(&dev->map_hash, hash);
101 if (ret != -EINVAL)
102 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000103 }
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000104 return drm_ht_just_insert_please(&dev->map_hash, hash,
105 user_token, 32 - PAGE_SHIFT - 3,
Thomas Hellstrom15450852007-02-08 16:14:05 +1100106 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
Dave Airlied1f2b552005-08-05 22:11:22 +1000107}
Dave Airlie9a186642005-06-23 21:29:18 +1000108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100110 * Core function to create a range of memory available for mapping by a
111 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
113 * Adjusts the memory offset to its absolute value according to the mapping
114 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
115 * applicable and if supported by the kernel.
116 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100117static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000118 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000119 enum drm_map_flags flags,
120 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100122 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000123 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000124 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000125 unsigned long user_token;
126 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
129 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -ENOMEM;
131
Dave Airlie7ab98402005-07-10 16:56:52 +1000132 map->offset = offset;
133 map->size = size;
134 map->flags = flags;
135 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* Only allow shared memory to be removable since we only keep enough
138 * book keeping information about shared memory to allow for removal
139 * when processes fork.
140 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
142 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -EINVAL;
144 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100145 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
146 (unsigned long long)map->offset, map->size, map->type);
147 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -EINVAL;
150 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 map->handle = NULL;
153
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 case _DRM_REGISTERS:
156 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000157#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100158 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 map->offset < virt_to_phys(high_memory)) {
160 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EINVAL;
162 }
163#endif
164#ifdef __alpha__
165 map->offset += dev->hose->mem_space->start;
166#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000167 /* Some drivers preinitialize some maps, without the X Server
168 * needing to be aware of it. Therefore, we just return success
169 * when the server tries to create a duplicate map.
170 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000171 list = drm_find_matching_map(dev, map);
172 if (list != NULL) {
173 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000174 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000175 "mismatched sizes, (%ld vs %ld)\n",
176 map->type, map->size,
177 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000178 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000179 }
180
181 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000182 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000183 return 0;
184 }
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000187 if (map->type == _DRM_FRAME_BUFFER ||
188 (map->flags & _DRM_WRITE_COMBINING)) {
189 map->mtrr = mtrr_add(map->offset, map->size,
190 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 }
Scott Thompson0769d392007-08-25 18:17:49 +1000193 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100194 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000195 if (!map->handle) {
196 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
197 return -ENOMEM;
198 }
199 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100203 list = drm_find_matching_map(dev, map);
204 if (list != NULL) {
205 if(list->map->size != map->size) {
206 DRM_DEBUG("Matching maps of type %d with "
207 "mismatched sizes, (%ld vs %ld)\n",
208 map->type, map->size, list->map->size);
209 list->map->size = map->size;
210 }
211
212 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
213 *maplist = list;
214 return 0;
215 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100216 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 DRM_DEBUG("%lu %d %p\n",
218 map->size, drm_order(map->size), map->handle);
219 if (!map->handle) {
220 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -ENOMEM;
222 }
223 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000226 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 vfree(map->handle);
228 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EBUSY;
230 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000231 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100234 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000235 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100236 int valid = 0;
237
238 if (!drm_core_has_AGP(dev)) {
239 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100242#ifdef __alpha__
243 map->offset += dev->hose->mem_space->start;
244#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000245 /* In some cases (i810 driver), user space may have already
246 * added the AGP base itself, because dev->agp->base previously
247 * only got set during AGP enable. So, only add the base
248 * address if the map's offset isn't already within the
249 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100250 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000251 if (map->offset < dev->agp->base ||
252 map->offset > dev->agp->base +
253 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
254 map->offset += dev->agp->base;
255 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100256 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
257
258 /* This assumes the DRM is in total control of AGP space.
259 * It's not always the case as AGP can be in the control
260 * of user space (i.e. i810 driver). So this loop will get
261 * skipped and we double check that dev->agp->memory is
262 * actually set as well as being invalid before EPERM'ing
263 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000264 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100265 if ((map->offset >= entry->bound) &&
266 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
267 valid = 1;
268 break;
269 }
270 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000271 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100272 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
273 return -EPERM;
274 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100275 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
276 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800279 case _DRM_GEM:
280 DRM_ERROR("tried to rmmap GEM object\n");
281 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 case _DRM_SCATTER_GATHER:
284 if (!dev->sg) {
285 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
286 return -EINVAL;
287 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000288 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000291 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000292 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000293 * casting it down to 32 bits is no problem, but we
294 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000295 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
296 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000297 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
298 return -ENOMEM;
299 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000300 map->handle = dmah->vaddr;
301 map->offset = (unsigned long)dmah->busaddr;
302 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return -EINVAL;
307 }
308
309 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700311 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100312 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
314 return -EINVAL;
315 }
316 memset(list, 0, sizeof(*list));
317 list->map = map;
318
Dave Airlie30e2fb12006-02-02 19:37:46 +1100319 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000320 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000321
Dave Airlied1f2b552005-08-05 22:11:22 +1000322 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100323 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000324 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
325 map->offset;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000326 ret = drm_map_handle(dev, &list->hash, user_token, 0);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000327 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700328 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100329 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000330 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
331 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
332 mutex_unlock(&dev->struct_mutex);
333 return ret;
334 }
335
Thomas Hellstrom15450852007-02-08 16:14:05 +1100336 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100337 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Airlie7c1c2872008-11-28 14:22:24 +1000339 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000340 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000341 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100342 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000343
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100344int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000345 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100346 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000347{
Dave Airlie55910512007-07-11 16:53:40 +1000348 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000349 int rc;
350
351 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
352 if (!rc)
353 *map_ptr = list->map;
354 return rc;
355}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356
Dave Airlie7ab98402005-07-10 16:56:52 +1000357EXPORT_SYMBOL(drm_addmap);
358
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100359/**
360 * Ioctl to specify a range of memory that is available for mapping by a
361 * non-root process.
362 *
363 * \param inode device inode.
364 * \param file_priv DRM file private.
365 * \param cmd command.
366 * \param arg pointer to a drm_map structure.
367 * \return zero on success or a negative value on error.
368 *
369 */
Eric Anholtc153f452007-09-03 12:06:45 +1000370int drm_addmap_ioctl(struct drm_device *dev, void *data,
371 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000372{
Eric Anholtc153f452007-09-03 12:06:45 +1000373 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000374 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000375 int err;
376
Dave Airlie7c1c2872008-11-28 14:22:24 +1000377 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100378 return -EPERM;
379
Eric Anholtc153f452007-09-03 12:06:45 +1000380 err = drm_addmap_core(dev, map->offset, map->size, map->type,
381 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000382
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000384 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000385
Dave Airlie67e1a012005-10-24 18:41:39 +1000386 /* 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 +1000387 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/**
392 * Remove a map private from list and deallocate resources if the mapping
393 * isn't in use.
394 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * Searches the map on drm_device::maplist, removes it from the list, see if
396 * its being used, and free any associate resource (such as MTRR's) if it's not
397 * being on use.
398 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000399 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100401int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Dave Airlie55910512007-07-11 16:53:40 +1000403 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000404 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000405 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000406 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dave Airlie836cf042005-07-10 19:27:04 +1000408 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000409 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000410 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000411 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000412 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100413 drm_ht_remove_key(&dev->map_hash,
414 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000415 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
416 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Dave Airlie836cf042005-07-10 19:27:04 +1000420
Dave Airliebd1b3312007-05-26 05:01:51 +1000421 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000422 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000423
424 switch (map->type) {
425 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100426 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000427 /* FALLTHROUGH */
428 case _DRM_FRAME_BUFFER:
429 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
430 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 retcode = mtrr_del(map->mtrr, map->offset, map->size);
432 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000433 }
434 break;
435 case _DRM_SHM:
436 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000437 if (master) {
438 if (dev->sigdata.lock == master->lock.hw_lock)
439 dev->sigdata.lock = NULL;
440 master->lock.hw_lock = NULL; /* SHM removed */
441 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100442 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000443 }
Dave Airlie836cf042005-07-10 19:27:04 +1000444 break;
445 case _DRM_AGP:
446 case _DRM_SCATTER_GATHER:
447 break;
448 case _DRM_CONSISTENT:
449 dmah.vaddr = map->handle;
450 dmah.busaddr = map->offset;
451 dmah.size = map->size;
452 __drm_pci_free(dev, &dmah);
453 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800454 case _DRM_GEM:
455 DRM_ERROR("tried to rmmap GEM object\n");
456 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000457 }
458 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461}
Dave Airlie4e74f362008-12-19 10:23:14 +1100462EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000463
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100464int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000465{
466 int ret;
467
Dave Airlie30e2fb12006-02-02 19:37:46 +1100468 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000469 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100470 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000471
472 return ret;
473}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000474EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000475
Dave Airlie836cf042005-07-10 19:27:04 +1000476/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
477 * the last close of the device, and this is necessary for cleanup when things
478 * exit uncleanly. Therefore, having userland manually remove mappings seems
479 * like a pointless exercise since they're going away anyway.
480 *
481 * One use case might be after addmap is allowed for normal users for SHM and
482 * gets used by drivers that the server doesn't need to care about. This seems
483 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100484 *
485 * \param inode device inode.
486 * \param file_priv DRM file private.
487 * \param cmd command.
488 * \param arg pointer to a struct drm_map structure.
489 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000490 */
Eric Anholtc153f452007-09-03 12:06:45 +1000491int drm_rmmap_ioctl(struct drm_device *dev, void *data,
492 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000493{
Eric Anholtc153f452007-09-03 12:06:45 +1000494 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100495 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000496 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000497 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000498
Dave Airlie30e2fb12006-02-02 19:37:46 +1100499 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000500 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000501 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000502 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000503 r_list->map->flags & _DRM_REMOVABLE) {
504 map = r_list->map;
505 break;
506 }
507 }
508
509 /* List has wrapped around to the head pointer, or its empty we didn't
510 * find anything.
511 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000512 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100513 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000514 return -EINVAL;
515 }
516
Dave Airlie836cf042005-07-10 19:27:04 +1000517 /* Register and framebuffer maps are permanent */
518 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100519 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000520 return 0;
521 }
522
523 ret = drm_rmmap_locked(dev, map);
524
Dave Airlie30e2fb12006-02-02 19:37:46 +1100525 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000526
527 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530/**
531 * Cleanup after an error on one of the addbufs() functions.
532 *
Dave Airlie836cf042005-07-10 19:27:04 +1000533 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * \param entry buffer entry where the error occurred.
535 *
536 * Frees any pages and buffers associated with the given entry.
537 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000538static void drm_cleanup_buf_error(struct drm_device * dev,
539 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 int i;
542
543 if (entry->seg_count) {
544 for (i = 0; i < entry->seg_count; i++) {
545 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100546 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 }
549 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 entry->seg_count *
551 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 entry->seg_count = 0;
554 }
555
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 if (entry->buf_count) {
557 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (entry->buflist[i].dev_private) {
559 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 entry->buflist[i].dev_priv_size,
561 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 }
564 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 entry->buf_count *
566 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 entry->buf_count = 0;
569 }
570}
571
572#if __OS_HAS_AGP
573/**
Dave Airlied59431b2005-07-10 15:00:06 +1000574 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000576 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000577 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * After some sanity checks creates a drm_buf structure for each buffer and
581 * reallocates the buffer list of the same size order to accommodate the new
582 * buffers.
583 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000584int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Dave Airliecdd55a22007-07-11 16:32:08 +1000586 struct drm_device_dma *dma = dev->dma;
587 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000588 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000589 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 unsigned long offset;
591 unsigned long agp_offset;
592 int count;
593 int order;
594 int size;
595 int alignment;
596 int page_order;
597 int total;
598 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100599 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000600 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 if (!dma)
603 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dave Airlied59431b2005-07-10 15:00:06 +1000605 count = request->count;
606 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 size = 1 << order;
608
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 alignment = (request->flags & _DRM_PAGE_ALIGN)
610 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
612 total = PAGE_SIZE << page_order;
613
614 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000615 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 DRM_DEBUG("count: %d\n", count);
618 DRM_DEBUG("order: %d\n", order);
619 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100620 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 DRM_DEBUG("alignment: %d\n", alignment);
622 DRM_DEBUG("page_order: %d\n", page_order);
623 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
626 return -EINVAL;
627 if (dev->queue_count)
628 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Dave Airlie54ba2f72007-02-10 12:07:47 +1100630 /* Make sure buffers are located in AGP memory that we own */
631 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000632 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100633 if ((agp_offset >= agp_entry->bound) &&
634 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
635 valid = 1;
636 break;
637 }
638 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000639 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100640 DRM_DEBUG("zone invalid\n");
641 return -EINVAL;
642 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 spin_lock(&dev->count_lock);
644 if (dev->buf_use) {
645 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -EBUSY;
647 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 atomic_inc(&dev->buf_alloc);
649 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Dave Airlie30e2fb12006-02-02 19:37:46 +1100651 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100654 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 atomic_dec(&dev->buf_alloc);
656 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
659 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100660 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -EINVAL;
663 }
664
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
666 DRM_MEM_BUFS);
667 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100668 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -ENOMEM;
671 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000672 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 entry->buf_size = size;
675 entry->page_order = page_order;
676
677 offset = 0;
678
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 while (entry->buf_count < count) {
680 buf = &entry->buflist[entry->buf_count];
681 buf->idx = dma->buf_count + entry->buf_count;
682 buf->total = alignment;
683 buf->order = order;
684 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 buf->bus_address = agp_offset + offset;
688 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 buf->waiting = 0;
691 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000693 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
697 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* Set count correctly so we free the proper amount. */
699 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100701 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -ENOMEM;
704 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 offset += alignment;
710 entry->buf_count++;
711 byte_count += PAGE_SIZE << page_order;
712 }
713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 temp_buflist = drm_realloc(dma->buflist,
717 dma->buf_count * sizeof(*dma->buflist),
718 (dma->buf_count + entry->buf_count)
719 * sizeof(*dma->buflist), DRM_MEM_BUFS);
720 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100723 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -ENOMEM;
726 }
727 dma->buflist = temp_buflist;
728
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
731 }
732
733 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100734 dma->seg_count += entry->seg_count;
735 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 dma->byte_count += byte_count;
737
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
739 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Dave Airlie30e2fb12006-02-02 19:37:46 +1100741 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dave Airlied59431b2005-07-10 15:00:06 +1000743 request->count = entry->buf_count;
744 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 dma->flags = _DRM_DMA_USE_AGP;
747
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751EXPORT_SYMBOL(drm_addbufs_agp);
752#endif /* __OS_HAS_AGP */
753
Dave Airlie84b1fd12007-07-11 15:53:27 +1000754int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Dave Airliecdd55a22007-07-11 16:32:08 +1000756 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int count;
758 int order;
759 int size;
760 int total;
761 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000762 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100763 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000764 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int alignment;
766 unsigned long offset;
767 int i;
768 int byte_count;
769 int page_count;
770 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000771 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000773 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
774 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100775
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 if (!dma)
777 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Airlied985c102006-01-02 21:32:48 +1100779 if (!capable(CAP_SYS_ADMIN))
780 return -EPERM;
781
Dave Airlied59431b2005-07-10 15:00:06 +1000782 count = request->count;
783 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 size = 1 << order;
785
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
787 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
790 return -EINVAL;
791 if (dev->queue_count)
792 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Dave Airlied59431b2005-07-10 15:00:06 +1000794 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
797 total = PAGE_SIZE << page_order;
798
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 spin_lock(&dev->count_lock);
800 if (dev->buf_use) {
801 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EBUSY;
803 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 atomic_inc(&dev->buf_alloc);
805 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dave Airlie30e2fb12006-02-02 19:37:46 +1100807 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100810 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -ENOMEM; /* May only call once for each order */
813 }
814
815 if (count < 0 || count > 4096) {
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 -EINVAL;
819 }
820
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
822 DRM_MEM_BUFS);
823 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100824 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -ENOMEM;
827 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
831 DRM_MEM_SEGS);
832 if (!entry->seglist) {
833 drm_free(entry->buflist,
834 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100835 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -ENOMEM;
838 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Keep the original pagelist until we know all the allocations
842 * have succeeded
843 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
845 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 drm_free(entry->buflist,
848 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
849 drm_free(entry->seglist,
850 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
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 }
855 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
857 DRM_DEBUG("pagelist: %d entries\n",
858 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 entry->page_order = page_order;
862 byte_count = 0;
863 page_count = 0;
864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000866
Dave Airlieddf19b92006-03-19 18:56:12 +1100867 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
Dave Airliebc5f4522007-11-05 12:50:58 +1000868
Dave Airlieddf19b92006-03-19 18:56:12 +1100869 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* Set count correctly so we free the proper amount. */
871 entry->buf_count = count;
872 entry->seg_count = count;
873 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 drm_free(temp_pagelist,
875 (dma->page_count + (count << page_order))
876 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100877 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -ENOMEM;
880 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100881 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 for (i = 0; i < (1 << page_order); i++) {
883 DRM_DEBUG("page %d @ 0x%08lx\n",
884 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100885 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100887 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 for (offset = 0;
890 offset + size <= total && entry->buf_count < count;
891 offset += alignment, ++entry->buf_count) {
892 buf = &entry->buflist[entry->buf_count];
893 buf->idx = dma->buf_count + entry->buf_count;
894 buf->total = alignment;
895 buf->order = order;
896 buf->used = 0;
897 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100898 buf->address = (void *)(dmah->vaddr + offset);
899 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 buf->waiting = 0;
902 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000904 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 buf->dev_private = drm_alloc(buf->dev_priv_size,
908 DRM_MEM_BUFS);
909 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* Set count correctly so we free the proper amount. */
911 entry->buf_count = count;
912 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 drm_cleanup_buf_error(dev, entry);
914 drm_free(temp_pagelist,
915 (dma->page_count +
916 (count << page_order))
917 * sizeof(*dma->pagelist),
918 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100919 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -ENOMEM;
922 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 DRM_DEBUG("buffer %d @ %p\n",
926 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 byte_count += PAGE_SIZE << page_order;
929 }
930
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 temp_buflist = drm_realloc(dma->buflist,
932 dma->buf_count * sizeof(*dma->buflist),
933 (dma->buf_count + entry->buf_count)
934 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (!temp_buflist) {
936 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000937 drm_cleanup_buf_error(dev, entry);
938 drm_free(temp_pagelist,
939 (dma->page_count + (count << page_order))
940 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100941 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -ENOMEM;
944 }
945 dma->buflist = temp_buflist;
946
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
949 }
950
951 /* No allocations failed, so now we can replace the orginal pagelist
952 * with the new one.
953 */
954 if (dma->page_count) {
955 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 dma->page_count * sizeof(*dma->pagelist),
957 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 dma->pagelist = temp_pagelist;
960
961 dma->buf_count += entry->buf_count;
962 dma->seg_count += entry->seg_count;
963 dma->page_count += entry->seg_count << page_order;
964 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
965
Dave Airlie30e2fb12006-02-02 19:37:46 +1100966 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Airlied59431b2005-07-10 15:00:06 +1000968 request->count = entry->buf_count;
969 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
George Sapountzis3417f332006-10-24 12:03:04 -0700971 if (request->flags & _DRM_PCI_BUFFER_RO)
972 dma->flags = _DRM_DMA_USE_PCI_RO;
973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976
977}
Dave Airlied84f76d2005-07-10 17:04:22 +1000978EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Dave Airlie84b1fd12007-07-11 15:53:27 +1000980static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Dave Airliecdd55a22007-07-11 16:32:08 +1000982 struct drm_device_dma *dma = dev->dma;
983 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000984 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 unsigned long offset;
986 unsigned long agp_offset;
987 int count;
988 int order;
989 int size;
990 int alignment;
991 int page_order;
992 int total;
993 int byte_count;
994 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000995 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 if (!drm_core_check_feature(dev, DRIVER_SG))
998 return -EINVAL;
999
1000 if (!dma)
1001 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dave Airlied985c102006-01-02 21:32:48 +11001003 if (!capable(CAP_SYS_ADMIN))
1004 return -EPERM;
1005
Dave Airlied59431b2005-07-10 15:00:06 +10001006 count = request->count;
1007 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 size = 1 << order;
1009
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 alignment = (request->flags & _DRM_PAGE_ALIGN)
1011 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1013 total = PAGE_SIZE << page_order;
1014
1015 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001016 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 DRM_DEBUG("count: %d\n", count);
1019 DRM_DEBUG("order: %d\n", order);
1020 DRM_DEBUG("size: %d\n", size);
1021 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1022 DRM_DEBUG("alignment: %d\n", alignment);
1023 DRM_DEBUG("page_order: %d\n", page_order);
1024 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1027 return -EINVAL;
1028 if (dev->queue_count)
1029 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 spin_lock(&dev->count_lock);
1032 if (dev->buf_use) {
1033 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -EBUSY;
1035 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 atomic_inc(&dev->buf_alloc);
1037 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Dave Airlie30e2fb12006-02-02 19:37:46 +11001039 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001042 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 atomic_dec(&dev->buf_alloc);
1044 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
1047 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001048 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return -EINVAL;
1051 }
1052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1054 DRM_MEM_BUFS);
1055 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001056 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -ENOMEM;
1059 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 entry->buf_size = size;
1063 entry->page_order = page_order;
1064
1065 offset = 0;
1066
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 while (entry->buf_count < count) {
1068 buf = &entry->buflist[entry->buf_count];
1069 buf->idx = dma->buf_count + entry->buf_count;
1070 buf->total = alignment;
1071 buf->order = order;
1072 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001077 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 buf->waiting = 0;
1080 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001082 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1086 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /* Set count correctly so we free the proper amount. */
1088 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001090 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -ENOMEM;
1093 }
1094
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 offset += alignment;
1100 entry->buf_count++;
1101 byte_count += PAGE_SIZE << page_order;
1102 }
1103
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 temp_buflist = drm_realloc(dma->buflist,
1107 dma->buf_count * sizeof(*dma->buflist),
1108 (dma->buf_count + entry->buf_count)
1109 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1110 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001113 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return -ENOMEM;
1116 }
1117 dma->buflist = temp_buflist;
1118
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001119 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1121 }
1122
1123 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001124 dma->seg_count += entry->seg_count;
1125 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 dma->byte_count += byte_count;
1127
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1129 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Dave Airlie30e2fb12006-02-02 19:37:46 +11001131 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dave Airlied59431b2005-07-10 15:00:06 +10001133 request->count = entry->buf_count;
1134 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 dma->flags = _DRM_DMA_USE_SG;
1137
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
1141
Dave Airlie84b1fd12007-07-11 15:53:27 +10001142static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001143{
Dave Airliecdd55a22007-07-11 16:32:08 +10001144 struct drm_device_dma *dma = dev->dma;
1145 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001146 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001147 unsigned long offset;
1148 unsigned long agp_offset;
1149 int count;
1150 int order;
1151 int size;
1152 int alignment;
1153 int page_order;
1154 int total;
1155 int byte_count;
1156 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001157 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001158
1159 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1160 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001161
Dave Airlieb84397d62005-07-10 14:46:12 +10001162 if (!dma)
1163 return -EINVAL;
1164
Dave Airlied985c102006-01-02 21:32:48 +11001165 if (!capable(CAP_SYS_ADMIN))
1166 return -EPERM;
1167
Dave Airlied59431b2005-07-10 15:00:06 +10001168 count = request->count;
1169 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001170 size = 1 << order;
1171
Dave Airlied59431b2005-07-10 15:00:06 +10001172 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001173 ? PAGE_ALIGN(size) : size;
1174 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1175 total = PAGE_SIZE << page_order;
1176
1177 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001178 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001179
1180 DRM_DEBUG("count: %d\n", count);
1181 DRM_DEBUG("order: %d\n", order);
1182 DRM_DEBUG("size: %d\n", size);
1183 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1184 DRM_DEBUG("alignment: %d\n", alignment);
1185 DRM_DEBUG("page_order: %d\n", page_order);
1186 DRM_DEBUG("total: %d\n", total);
1187
1188 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1189 return -EINVAL;
1190 if (dev->queue_count)
1191 return -EBUSY; /* Not while in use */
1192
1193 spin_lock(&dev->count_lock);
1194 if (dev->buf_use) {
1195 spin_unlock(&dev->count_lock);
1196 return -EBUSY;
1197 }
1198 atomic_inc(&dev->buf_alloc);
1199 spin_unlock(&dev->count_lock);
1200
Dave Airlie30e2fb12006-02-02 19:37:46 +11001201 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001202 entry = &dma->bufs[order];
1203 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001204 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001205 atomic_dec(&dev->buf_alloc);
1206 return -ENOMEM; /* May only call once for each order */
1207 }
1208
1209 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001210 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001211 atomic_dec(&dev->buf_alloc);
1212 return -EINVAL;
1213 }
1214
1215 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1216 DRM_MEM_BUFS);
1217 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001218 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001219 atomic_dec(&dev->buf_alloc);
1220 return -ENOMEM;
1221 }
1222 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1223
1224 entry->buf_size = size;
1225 entry->page_order = page_order;
1226
1227 offset = 0;
1228
1229 while (entry->buf_count < count) {
1230 buf = &entry->buflist[entry->buf_count];
1231 buf->idx = dma->buf_count + entry->buf_count;
1232 buf->total = alignment;
1233 buf->order = order;
1234 buf->used = 0;
1235
1236 buf->offset = (dma->byte_count + offset);
1237 buf->bus_address = agp_offset + offset;
1238 buf->address = (void *)(agp_offset + offset);
1239 buf->next = NULL;
1240 buf->waiting = 0;
1241 buf->pending = 0;
1242 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001243 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001244
1245 buf->dev_priv_size = dev->driver->dev_priv_size;
1246 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1247 if (!buf->dev_private) {
1248 /* Set count correctly so we free the proper amount. */
1249 entry->buf_count = count;
1250 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001251 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001252 atomic_dec(&dev->buf_alloc);
1253 return -ENOMEM;
1254 }
1255 memset(buf->dev_private, 0, buf->dev_priv_size);
1256
1257 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1258
1259 offset += alignment;
1260 entry->buf_count++;
1261 byte_count += PAGE_SIZE << page_order;
1262 }
1263
1264 DRM_DEBUG("byte_count: %d\n", byte_count);
1265
1266 temp_buflist = drm_realloc(dma->buflist,
1267 dma->buf_count * sizeof(*dma->buflist),
1268 (dma->buf_count + entry->buf_count)
1269 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1270 if (!temp_buflist) {
1271 /* Free the entry because it isn't valid */
1272 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001273 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001274 atomic_dec(&dev->buf_alloc);
1275 return -ENOMEM;
1276 }
1277 dma->buflist = temp_buflist;
1278
1279 for (i = 0; i < entry->buf_count; i++) {
1280 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1281 }
1282
1283 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001284 dma->seg_count += entry->seg_count;
1285 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001286 dma->byte_count += byte_count;
1287
1288 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1289 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1290
Dave Airlie30e2fb12006-02-02 19:37:46 +11001291 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001292
Dave Airlied59431b2005-07-10 15:00:06 +10001293 request->count = entry->buf_count;
1294 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001295
1296 dma->flags = _DRM_DMA_USE_FB;
1297
1298 atomic_dec(&dev->buf_alloc);
1299 return 0;
1300}
Dave Airlied985c102006-01-02 21:32:48 +11001301
Dave Airlieb84397d62005-07-10 14:46:12 +10001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/**
1304 * Add buffers for DMA transfers (ioctl).
1305 *
1306 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001307 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001309 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * \return zero on success or a negative number on failure.
1311 *
1312 * According with the memory type specified in drm_buf_desc::flags and the
1313 * build options, it dispatches the call either to addbufs_agp(),
1314 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1315 * PCI memory respectively.
1316 */
Eric Anholtc153f452007-09-03 12:06:45 +10001317int drm_addbufs(struct drm_device *dev, void *data,
1318 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Eric Anholtc153f452007-09-03 12:06:45 +10001320 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001321 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1324 return -EINVAL;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001327 if (request->flags & _DRM_AGP_BUFFER)
1328 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 else
1330#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001331 if (request->flags & _DRM_SG_BUFFER)
1332 ret = drm_addbufs_sg(dev, request);
1333 else if (request->flags & _DRM_FB_BUFFER)
1334 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 else
Eric Anholtc153f452007-09-03 12:06:45 +10001336 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001337
Dave Airlied59431b2005-07-10 15:00:06 +10001338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/**
1342 * Get information about the buffer mappings.
1343 *
1344 * This was originally mean for debugging purposes, or by a sophisticated
1345 * client library to determine how best to use the available buffers (e.g.,
1346 * large buffers can be used for image transfer).
1347 *
1348 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001349 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * \param cmd command.
1351 * \param arg pointer to a drm_buf_info structure.
1352 * \return zero on success or a negative number on failure.
1353 *
1354 * Increments drm_device::buf_use while holding the drm_device::count_lock
1355 * lock, preventing of allocating more buffers after this call. Information
1356 * about each requested buffer is then copied into user space.
1357 */
Eric Anholtc153f452007-09-03 12:06:45 +10001358int drm_infobufs(struct drm_device *dev, void *data,
1359 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Dave Airliecdd55a22007-07-11 16:32:08 +10001361 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001362 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 int i;
1364 int count;
1365
1366 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1367 return -EINVAL;
1368
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001369 if (!dma)
1370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001372 spin_lock(&dev->count_lock);
1373 if (atomic_read(&dev->buf_alloc)) {
1374 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return -EBUSY;
1376 }
1377 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001380 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1381 if (dma->bufs[i].buf_count)
1382 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001385 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Eric Anholtc153f452007-09-03 12:06:45 +10001387 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001388 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1389 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001390 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001391 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001392 struct drm_buf_entry *from = &dma->bufs[i];
1393 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 if (copy_to_user(&to->count,
1395 &from->buf_count,
1396 sizeof(from->buf_count)) ||
1397 copy_to_user(&to->size,
1398 &from->buf_size,
1399 sizeof(from->buf_size)) ||
1400 copy_to_user(&to->low_mark,
1401 &list->low_mark,
1402 sizeof(list->low_mark)) ||
1403 copy_to_user(&to->high_mark,
1404 &list->high_mark,
1405 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return -EFAULT;
1407
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 DRM_DEBUG("%d %d %d %d %d\n",
1409 i,
1410 dma->bufs[i].buf_count,
1411 dma->bufs[i].buf_size,
1412 dma->bufs[i].freelist.low_mark,
1413 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 ++count;
1415 }
1416 }
1417 }
Eric Anholtc153f452007-09-03 12:06:45 +10001418 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 return 0;
1421}
1422
1423/**
1424 * Specifies a low and high water mark for buffer allocation
1425 *
1426 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001427 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 * \param cmd command.
1429 * \param arg a pointer to a drm_buf_desc structure.
1430 * \return zero on success or a negative number on failure.
1431 *
1432 * Verifies that the size order is bounded between the admissible orders and
1433 * updates the respective drm_device_dma::bufs entry low and high water mark.
1434 *
1435 * \note This ioctl is deprecated and mostly never used.
1436 */
Eric Anholtc153f452007-09-03 12:06:45 +10001437int drm_markbufs(struct drm_device *dev, void *data,
1438 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Dave Airliecdd55a22007-07-11 16:32:08 +10001440 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001441 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001443 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1446 return -EINVAL;
1447
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 if (!dma)
1449 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001452 request->size, request->low_mark, request->high_mark);
1453 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 entry = &dma->bufs[order];
1457
Eric Anholtc153f452007-09-03 12:06:45 +10001458 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001460 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EINVAL;
1462
Eric Anholtc153f452007-09-03 12:06:45 +10001463 entry->freelist.low_mark = request->low_mark;
1464 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 return 0;
1467}
1468
1469/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001470 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 *
1472 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001473 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * \param cmd command.
1475 * \param arg pointer to a drm_buf_free structure.
1476 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001477 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 * Calls free_buffer() for each used buffer.
1479 * This function is primarily used for debugging.
1480 */
Eric Anholtc153f452007-09-03 12:06:45 +10001481int drm_freebufs(struct drm_device *dev, void *data,
1482 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Dave Airliecdd55a22007-07-11 16:32:08 +10001484 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001485 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int i;
1487 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001488 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1491 return -EINVAL;
1492
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 if (!dma)
1494 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Eric Anholtc153f452007-09-03 12:06:45 +10001496 DRM_DEBUG("%d\n", request->count);
1497 for (i = 0; i < request->count; i++) {
1498 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001500 if (idx < 0 || idx >= dma->buf_count) {
1501 DRM_ERROR("Index %d (of %d max)\n",
1502 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EINVAL;
1504 }
1505 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001506 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001507 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001508 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return -EINVAL;
1510 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001511 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
1514 return 0;
1515}
1516
1517/**
1518 * Maps all of the DMA buffers into client-virtual space (ioctl).
1519 *
1520 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001521 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 * \param cmd command.
1523 * \param arg pointer to a drm_buf_map structure.
1524 * \return zero on success or a negative number on failure.
1525 *
George Sapountzis3417f332006-10-24 12:03:04 -07001526 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1527 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1528 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1529 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
Eric Anholtc153f452007-09-03 12:06:45 +10001531int drm_mapbufs(struct drm_device *dev, void *data,
1532 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Dave Airliecdd55a22007-07-11 16:32:08 +10001534 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int retcode = 0;
1536 const int zero = 0;
1537 unsigned long virtual;
1538 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001539 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int i;
1541
1542 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1543 return -EINVAL;
1544
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001545 if (!dma)
1546 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001548 spin_lock(&dev->count_lock);
1549 if (atomic_read(&dev->buf_alloc)) {
1550 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return -EBUSY;
1552 }
1553 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001554 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Eric Anholtc153f452007-09-03 12:06:45 +10001556 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001557 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001558 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001559 && (dma->flags & _DRM_DMA_USE_SG))
1560 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1561 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001562 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001563 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 retcode = -EINVAL;
1567 goto done;
1568 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001569 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001570 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001572 MAP_SHARED,
1573 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001574 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001576 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001577 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 PROT_READ | PROT_WRITE,
1579 MAP_SHARED, 0);
1580 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001582 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /* Real error */
1584 retcode = (signed long)virtual;
1585 goto done;
1586 }
Eric Anholtc153f452007-09-03 12:06:45 +10001587 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001590 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001592 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 retcode = -EFAULT;
1594 goto done;
1595 }
Eric Anholtc153f452007-09-03 12:06:45 +10001596 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001597 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001598 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 retcode = -EFAULT;
1600 goto done;
1601 }
Eric Anholtc153f452007-09-03 12:06:45 +10001602 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001603 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 retcode = -EFAULT;
1605 goto done;
1606 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001607 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001608 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001609 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 retcode = -EFAULT;
1611 goto done;
1612 }
1613 }
1614 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001615 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001616 request->count = dma->buf_count;
1617 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 return retcode;
1620}
1621
Dave Airlie836cf042005-07-10 19:27:04 +10001622/**
1623 * Compute size order. Returns the exponent of the smaller power of two which
1624 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001625 *
Dave Airlie836cf042005-07-10 19:27:04 +10001626 * \param size size.
1627 * \return order.
1628 *
1629 * \todo Can be made faster.
1630 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001631int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001632{
1633 int order;
1634 unsigned long tmp;
1635
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001636 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001637
1638 if (size & (size - 1))
1639 ++order;
1640
1641 return order;
1642}
1643EXPORT_SYMBOL(drm_order);