blob: 0411d912d82abb9a82b812eb5e1639029659c60a [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>
David Millerf1a2a9b2009-02-18 15:41:02 -080037#include <linux/log2.h>
38#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "drmP.h"
40
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110041resource_size_t drm_get_resource_start(struct drm_device *dev, unsigned int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Dave Airlie836cf042005-07-10 19:27:04 +100043 return pci_resource_start(dev->pdev, resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
Dave Airlie836cf042005-07-10 19:27:04 +100045EXPORT_SYMBOL(drm_get_resource_start);
46
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110047resource_size_t drm_get_resource_len(struct drm_device *dev, unsigned int resource)
Dave Airlie836cf042005-07-10 19:27:04 +100048{
49 return pci_resource_len(dev->pdev, resource);
50}
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051
Dave Airlie836cf042005-07-10 19:27:04 +100052EXPORT_SYMBOL(drm_get_resource_len);
53
Dave Airlie55910512007-07-11 16:53:40 +100054static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110055 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100056{
Dave Airlie55910512007-07-11 16:53:40 +100057 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100058 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110059 /*
60 * Because the kernel-userspace ABI is fixed at a 32-bit offset
61 * while PCI resources may live above that, we ignore the map
62 * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
63 * It is assumed that each driver will have only one resource of
64 * each type.
65 */
66 if (!entry->map ||
67 map->type != entry->map->type ||
68 entry->master != dev->primary->master)
69 continue;
70 switch (map->type) {
71 case _DRM_SHM:
72 if (map->flags != _DRM_CONTAINS_LOCK)
73 break;
74 case _DRM_REGISTERS:
75 case _DRM_FRAME_BUFFER:
Dave Airlie89625eb2005-09-05 21:23:23 +100076 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110077 default: /* Make gcc happy */
78 ;
Dave Airlie836cf042005-07-10 19:27:04 +100079 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110080 if (entry->map->offset == map->offset)
81 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100082 }
83
84 return NULL;
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Dave Airliee0be4282007-07-12 10:26:44 +100087static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080088 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100089{
David Millerf1a2a9b2009-02-18 15:41:02 -080090 int use_hashed_handle, shift;
91 unsigned long add;
92
Dave Airliec2604ce2006-08-12 16:03:26 +100093#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100094 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
95#elif (BITS_PER_LONG == 32)
96 use_hashed_handle = hashed_handle;
97#else
98#error Unsupported long size. Neither 64 nor 32 bits.
99#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000100
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000101 if (!use_hashed_handle) {
102 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100103 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000104 ret = drm_ht_insert_item(&dev->map_hash, hash);
105 if (ret != -EINVAL)
106 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000107 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800108
109 shift = 0;
110 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
111 if (shm && (SHMLBA > PAGE_SIZE)) {
112 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
113
114 /* For shared memory, we have to preserve the SHMLBA
115 * bits of the eventual vma->vm_pgoff value during
116 * mmap(). Otherwise we run into cache aliasing problems
117 * on some platforms. On these platforms, the pgoff of
118 * a mmap() request is used to pick a suitable virtual
119 * address for the mmap() region such that it will not
120 * cause cache aliasing problems.
121 *
122 * Therefore, make sure the SHMLBA relevant bits of the
123 * hash value we use are equal to those in the original
124 * kernel virtual address.
125 */
126 shift = bits;
127 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
128 }
129
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000130 return drm_ht_just_insert_please(&dev->map_hash, hash,
131 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800132 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000133}
Dave Airlie9a186642005-06-23 21:29:18 +1000134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100136 * Core function to create a range of memory available for mapping by a
137 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
139 * Adjusts the memory offset to its absolute value according to the mapping
140 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
141 * applicable and if supported by the kernel.
142 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100143static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000144 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000145 enum drm_map_flags flags,
146 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100148 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000149 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000150 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000151 unsigned long user_token;
152 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
155 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -ENOMEM;
157
Dave Airlie7ab98402005-07-10 16:56:52 +1000158 map->offset = offset;
159 map->size = size;
160 map->flags = flags;
161 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* Only allow shared memory to be removable since we only keep enough
164 * book keeping information about shared memory to allow for removal
165 * when processes fork.
166 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000167 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
168 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -EINVAL;
170 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100171 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
172 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000173
174 /* page-align _DRM_SHM maps. They are allocated here so there is no security
175 * hole created by that and it works around various broken drivers that use
176 * a non-aligned quantity to map the SAREA. --BenH
177 */
178 if (map->type == _DRM_SHM)
179 map->size = PAGE_ALIGN(map->size);
180
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100181 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EINVAL;
184 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 map->handle = NULL;
187
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 case _DRM_REGISTERS:
190 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000191#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100192 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 map->offset < virt_to_phys(high_memory)) {
194 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
196 }
197#endif
198#ifdef __alpha__
199 map->offset += dev->hose->mem_space->start;
200#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000201 /* Some drivers preinitialize some maps, without the X Server
202 * needing to be aware of it. Therefore, we just return success
203 * when the server tries to create a duplicate map.
204 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000205 list = drm_find_matching_map(dev, map);
206 if (list != NULL) {
207 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000208 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 "mismatched sizes, (%ld vs %ld)\n",
210 map->type, map->size,
211 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000212 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000213 }
214
215 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000216 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000217 return 0;
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 if (map->type == _DRM_FRAME_BUFFER ||
222 (map->flags & _DRM_WRITE_COMBINING)) {
223 map->mtrr = mtrr_add(map->offset, map->size,
224 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 }
Scott Thompson0769d392007-08-25 18:17:49 +1000227 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100228 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000229 if (!map->handle) {
230 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
231 return -ENOMEM;
232 }
233 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100237 list = drm_find_matching_map(dev, map);
238 if (list != NULL) {
239 if(list->map->size != map->size) {
240 DRM_DEBUG("Matching maps of type %d with "
241 "mismatched sizes, (%ld vs %ld)\n",
242 map->type, map->size, list->map->size);
243 list->map->size = map->size;
244 }
245
246 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
247 *maplist = list;
248 return 0;
249 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100250 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 DRM_DEBUG("%lu %d %p\n",
252 map->size, drm_order(map->size), map->handle);
253 if (!map->handle) {
254 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -ENOMEM;
256 }
257 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000260 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 vfree(map->handle);
262 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EBUSY;
264 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000265 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100268 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000269 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100270 int valid = 0;
271
272 if (!drm_core_has_AGP(dev)) {
273 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100276#ifdef __alpha__
277 map->offset += dev->hose->mem_space->start;
278#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000279 /* In some cases (i810 driver), user space may have already
280 * added the AGP base itself, because dev->agp->base previously
281 * only got set during AGP enable. So, only add the base
282 * address if the map's offset isn't already within the
283 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100284 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000285 if (map->offset < dev->agp->base ||
286 map->offset > dev->agp->base +
287 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
288 map->offset += dev->agp->base;
289 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100290 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
291
292 /* This assumes the DRM is in total control of AGP space.
293 * It's not always the case as AGP can be in the control
294 * of user space (i.e. i810 driver). So this loop will get
295 * skipped and we double check that dev->agp->memory is
296 * actually set as well as being invalid before EPERM'ing
297 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000298 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100299 if ((map->offset >= entry->bound) &&
300 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
301 valid = 1;
302 break;
303 }
304 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000305 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100306 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
307 return -EPERM;
308 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100309 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
310 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800313 case _DRM_GEM:
314 DRM_ERROR("tried to rmmap GEM object\n");
315 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 case _DRM_SCATTER_GATHER:
318 if (!dev->sg) {
319 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
320 return -EINVAL;
321 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000322 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000325 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000326 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000327 * casting it down to 32 bits is no problem, but we
328 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000329 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
330 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000331 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
332 return -ENOMEM;
333 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000334 map->handle = dmah->vaddr;
335 map->offset = (unsigned long)dmah->busaddr;
336 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EINVAL;
341 }
342
343 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700345 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100346 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
348 return -EINVAL;
349 }
350 memset(list, 0, sizeof(*list));
351 list->map = map;
352
Dave Airlie30e2fb12006-02-02 19:37:46 +1100353 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000354 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000355
Dave Airlied1f2b552005-08-05 22:11:22 +1000356 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100357 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000358 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
359 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800360 ret = drm_map_handle(dev, &list->hash, user_token, 0,
361 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000362 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700363 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100364 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000365 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
366 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
367 mutex_unlock(&dev->struct_mutex);
368 return ret;
369 }
370
Thomas Hellstrom15450852007-02-08 16:14:05 +1100371 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100372 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dave Airlie7c1c2872008-11-28 14:22:24 +1000374 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000375 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000376 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100377 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000378
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100379int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000380 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100381 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000382{
Dave Airlie55910512007-07-11 16:53:40 +1000383 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000384 int rc;
385
386 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
387 if (!rc)
388 *map_ptr = list->map;
389 return rc;
390}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000391
Dave Airlie7ab98402005-07-10 16:56:52 +1000392EXPORT_SYMBOL(drm_addmap);
393
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100394/**
395 * Ioctl to specify a range of memory that is available for mapping by a
396 * non-root process.
397 *
398 * \param inode device inode.
399 * \param file_priv DRM file private.
400 * \param cmd command.
401 * \param arg pointer to a drm_map structure.
402 * \return zero on success or a negative value on error.
403 *
404 */
Eric Anholtc153f452007-09-03 12:06:45 +1000405int drm_addmap_ioctl(struct drm_device *dev, void *data,
406 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000407{
Eric Anholtc153f452007-09-03 12:06:45 +1000408 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000409 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000410 int err;
411
Dave Airlie7c1c2872008-11-28 14:22:24 +1000412 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100413 return -EPERM;
414
Eric Anholtc153f452007-09-03 12:06:45 +1000415 err = drm_addmap_core(dev, map->offset, map->size, map->type,
416 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000417
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000419 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000420
Dave Airlie67e1a012005-10-24 18:41:39 +1000421 /* 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 +1000422 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000424}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/**
427 * Remove a map private from list and deallocate resources if the mapping
428 * isn't in use.
429 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * Searches the map on drm_device::maplist, removes it from the list, see if
431 * its being used, and free any associate resource (such as MTRR's) if it's not
432 * being on use.
433 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000434 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100436int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Dave Airlie55910512007-07-11 16:53:40 +1000438 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000439 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000440 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000441 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dave Airlie836cf042005-07-10 19:27:04 +1000443 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000444 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000445 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000446 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000447 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100448 drm_ht_remove_key(&dev->map_hash,
449 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000450 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
451 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Dave Airlie836cf042005-07-10 19:27:04 +1000455
Dave Airliebd1b3312007-05-26 05:01:51 +1000456 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000457 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000458
459 switch (map->type) {
460 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100461 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000462 /* FALLTHROUGH */
463 case _DRM_FRAME_BUFFER:
464 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
465 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 retcode = mtrr_del(map->mtrr, map->offset, map->size);
467 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000468 }
469 break;
470 case _DRM_SHM:
471 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000472 if (master) {
473 if (dev->sigdata.lock == master->lock.hw_lock)
474 dev->sigdata.lock = NULL;
475 master->lock.hw_lock = NULL; /* SHM removed */
476 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100477 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000478 }
Dave Airlie836cf042005-07-10 19:27:04 +1000479 break;
480 case _DRM_AGP:
481 case _DRM_SCATTER_GATHER:
482 break;
483 case _DRM_CONSISTENT:
484 dmah.vaddr = map->handle;
485 dmah.busaddr = map->offset;
486 dmah.size = map->size;
487 __drm_pci_free(dev, &dmah);
488 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800489 case _DRM_GEM:
490 DRM_ERROR("tried to rmmap GEM object\n");
491 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000492 }
493 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496}
Dave Airlie4e74f362008-12-19 10:23:14 +1100497EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000498
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100499int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000500{
501 int ret;
502
Dave Airlie30e2fb12006-02-02 19:37:46 +1100503 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000504 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100505 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000506
507 return ret;
508}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000509EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000510
Dave Airlie836cf042005-07-10 19:27:04 +1000511/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
512 * the last close of the device, and this is necessary for cleanup when things
513 * exit uncleanly. Therefore, having userland manually remove mappings seems
514 * like a pointless exercise since they're going away anyway.
515 *
516 * One use case might be after addmap is allowed for normal users for SHM and
517 * gets used by drivers that the server doesn't need to care about. This seems
518 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100519 *
520 * \param inode device inode.
521 * \param file_priv DRM file private.
522 * \param cmd command.
523 * \param arg pointer to a struct drm_map structure.
524 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000525 */
Eric Anholtc153f452007-09-03 12:06:45 +1000526int drm_rmmap_ioctl(struct drm_device *dev, void *data,
527 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000528{
Eric Anholtc153f452007-09-03 12:06:45 +1000529 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100530 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000531 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000532 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000533
Dave Airlie30e2fb12006-02-02 19:37:46 +1100534 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000535 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000536 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000537 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000538 r_list->map->flags & _DRM_REMOVABLE) {
539 map = r_list->map;
540 break;
541 }
542 }
543
544 /* List has wrapped around to the head pointer, or its empty we didn't
545 * find anything.
546 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000547 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100548 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000549 return -EINVAL;
550 }
551
Dave Airlie836cf042005-07-10 19:27:04 +1000552 /* Register and framebuffer maps are permanent */
553 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100554 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000555 return 0;
556 }
557
558 ret = drm_rmmap_locked(dev, map);
559
Dave Airlie30e2fb12006-02-02 19:37:46 +1100560 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000561
562 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565/**
566 * Cleanup after an error on one of the addbufs() functions.
567 *
Dave Airlie836cf042005-07-10 19:27:04 +1000568 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * \param entry buffer entry where the error occurred.
570 *
571 * Frees any pages and buffers associated with the given entry.
572 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000573static void drm_cleanup_buf_error(struct drm_device * dev,
574 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 int i;
577
578 if (entry->seg_count) {
579 for (i = 0; i < entry->seg_count; i++) {
580 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100581 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 }
584 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 entry->seg_count *
586 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 entry->seg_count = 0;
589 }
590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 if (entry->buf_count) {
592 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (entry->buflist[i].dev_private) {
594 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 entry->buflist[i].dev_priv_size,
596 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 }
599 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 entry->buf_count *
601 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 entry->buf_count = 0;
604 }
605}
606
607#if __OS_HAS_AGP
608/**
Dave Airlied59431b2005-07-10 15:00:06 +1000609 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000611 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000612 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * After some sanity checks creates a drm_buf structure for each buffer and
616 * reallocates the buffer list of the same size order to accommodate the new
617 * buffers.
618 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000619int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Dave Airliecdd55a22007-07-11 16:32:08 +1000621 struct drm_device_dma *dma = dev->dma;
622 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000623 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000624 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned long offset;
626 unsigned long agp_offset;
627 int count;
628 int order;
629 int size;
630 int alignment;
631 int page_order;
632 int total;
633 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100634 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000635 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 if (!dma)
638 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Dave Airlied59431b2005-07-10 15:00:06 +1000640 count = request->count;
641 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 size = 1 << order;
643
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 alignment = (request->flags & _DRM_PAGE_ALIGN)
645 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
647 total = PAGE_SIZE << page_order;
648
649 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000650 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 DRM_DEBUG("count: %d\n", count);
653 DRM_DEBUG("order: %d\n", order);
654 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100655 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 DRM_DEBUG("alignment: %d\n", alignment);
657 DRM_DEBUG("page_order: %d\n", page_order);
658 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
661 return -EINVAL;
662 if (dev->queue_count)
663 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dave Airlie54ba2f72007-02-10 12:07:47 +1100665 /* Make sure buffers are located in AGP memory that we own */
666 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000667 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100668 if ((agp_offset >= agp_entry->bound) &&
669 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
670 valid = 1;
671 break;
672 }
673 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000674 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100675 DRM_DEBUG("zone invalid\n");
676 return -EINVAL;
677 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 spin_lock(&dev->count_lock);
679 if (dev->buf_use) {
680 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -EBUSY;
682 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 atomic_inc(&dev->buf_alloc);
684 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dave Airlie30e2fb12006-02-02 19:37:46 +1100686 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100689 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 atomic_dec(&dev->buf_alloc);
691 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
694 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100695 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -EINVAL;
698 }
699
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
701 DRM_MEM_BUFS);
702 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100703 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
706 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 entry->buf_size = size;
710 entry->page_order = page_order;
711
712 offset = 0;
713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 while (entry->buf_count < count) {
715 buf = &entry->buflist[entry->buf_count];
716 buf->idx = dma->buf_count + entry->buf_count;
717 buf->total = alignment;
718 buf->order = order;
719 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 buf->bus_address = agp_offset + offset;
723 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 buf->waiting = 0;
726 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000728 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
732 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* Set count correctly so we free the proper amount. */
734 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100736 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -ENOMEM;
739 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 offset += alignment;
745 entry->buf_count++;
746 byte_count += PAGE_SIZE << page_order;
747 }
748
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000749 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 temp_buflist = drm_realloc(dma->buflist,
752 dma->buf_count * sizeof(*dma->buflist),
753 (dma->buf_count + entry->buf_count)
754 * sizeof(*dma->buflist), DRM_MEM_BUFS);
755 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100758 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -ENOMEM;
761 }
762 dma->buflist = temp_buflist;
763
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
766 }
767
768 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100769 dma->seg_count += entry->seg_count;
770 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dma->byte_count += byte_count;
772
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000773 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
774 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Dave Airlie30e2fb12006-02-02 19:37:46 +1100776 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Dave Airlied59431b2005-07-10 15:00:06 +1000778 request->count = entry->buf_count;
779 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 dma->flags = _DRM_DMA_USE_AGP;
782
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786EXPORT_SYMBOL(drm_addbufs_agp);
787#endif /* __OS_HAS_AGP */
788
Dave Airlie84b1fd12007-07-11 15:53:27 +1000789int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Dave Airliecdd55a22007-07-11 16:32:08 +1000791 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int count;
793 int order;
794 int size;
795 int total;
796 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000797 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100798 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000799 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int alignment;
801 unsigned long offset;
802 int i;
803 int byte_count;
804 int page_count;
805 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000806 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
809 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100810
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 if (!dma)
812 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Airlied985c102006-01-02 21:32:48 +1100814 if (!capable(CAP_SYS_ADMIN))
815 return -EPERM;
816
Dave Airlied59431b2005-07-10 15:00:06 +1000817 count = request->count;
818 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 size = 1 << order;
820
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
822 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
825 return -EINVAL;
826 if (dev->queue_count)
827 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Airlied59431b2005-07-10 15:00:06 +1000829 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
832 total = PAGE_SIZE << page_order;
833
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 spin_lock(&dev->count_lock);
835 if (dev->buf_use) {
836 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EBUSY;
838 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 atomic_inc(&dev->buf_alloc);
840 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Dave Airlie30e2fb12006-02-02 19:37:46 +1100842 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100845 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -ENOMEM; /* May only call once for each order */
848 }
849
850 if (count < 0 || count > 4096) {
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 -EINVAL;
854 }
855
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
857 DRM_MEM_BUFS);
858 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100859 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -ENOMEM;
862 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
866 DRM_MEM_SEGS);
867 if (!entry->seglist) {
868 drm_free(entry->buflist,
869 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100870 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -ENOMEM;
873 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* Keep the original pagelist until we know all the allocations
877 * have succeeded
878 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
880 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 drm_free(entry->buflist,
883 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
884 drm_free(entry->seglist,
885 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100886 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -ENOMEM;
889 }
890 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
892 DRM_DEBUG("pagelist: %d entries\n",
893 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 entry->page_order = page_order;
897 byte_count = 0;
898 page_count = 0;
899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000901
Dave Airlieddf19b92006-03-19 18:56:12 +1100902 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
Dave Airliebc5f4522007-11-05 12:50:58 +1000903
Dave Airlieddf19b92006-03-19 18:56:12 +1100904 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* Set count correctly so we free the proper amount. */
906 entry->buf_count = count;
907 entry->seg_count = count;
908 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 drm_free(temp_pagelist,
910 (dma->page_count + (count << page_order))
911 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100912 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOMEM;
915 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100916 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 for (i = 0; i < (1 << page_order); i++) {
918 DRM_DEBUG("page %d @ 0x%08lx\n",
919 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100920 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100922 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 for (offset = 0;
925 offset + size <= total && entry->buf_count < count;
926 offset += alignment, ++entry->buf_count) {
927 buf = &entry->buflist[entry->buf_count];
928 buf->idx = dma->buf_count + entry->buf_count;
929 buf->total = alignment;
930 buf->order = order;
931 buf->used = 0;
932 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100933 buf->address = (void *)(dmah->vaddr + offset);
934 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 buf->waiting = 0;
937 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000939 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 buf->dev_private = drm_alloc(buf->dev_priv_size,
943 DRM_MEM_BUFS);
944 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* Set count correctly so we free the proper amount. */
946 entry->buf_count = count;
947 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 drm_cleanup_buf_error(dev, entry);
949 drm_free(temp_pagelist,
950 (dma->page_count +
951 (count << page_order))
952 * sizeof(*dma->pagelist),
953 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100954 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -ENOMEM;
957 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960 DRM_DEBUG("buffer %d @ %p\n",
961 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 byte_count += PAGE_SIZE << page_order;
964 }
965
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000966 temp_buflist = drm_realloc(dma->buflist,
967 dma->buf_count * sizeof(*dma->buflist),
968 (dma->buf_count + entry->buf_count)
969 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (!temp_buflist) {
971 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 drm_cleanup_buf_error(dev, entry);
973 drm_free(temp_pagelist,
974 (dma->page_count + (count << page_order))
975 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100976 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -ENOMEM;
979 }
980 dma->buflist = temp_buflist;
981
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
984 }
985
986 /* No allocations failed, so now we can replace the orginal pagelist
987 * with the new one.
988 */
989 if (dma->page_count) {
990 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 dma->page_count * sizeof(*dma->pagelist),
992 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 dma->pagelist = temp_pagelist;
995
996 dma->buf_count += entry->buf_count;
997 dma->seg_count += entry->seg_count;
998 dma->page_count += entry->seg_count << page_order;
999 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1000
Dave Airlie30e2fb12006-02-02 19:37:46 +11001001 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dave Airlied59431b2005-07-10 15:00:06 +10001003 request->count = entry->buf_count;
1004 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
George Sapountzis3417f332006-10-24 12:03:04 -07001006 if (request->flags & _DRM_PCI_BUFFER_RO)
1007 dma->flags = _DRM_DMA_USE_PCI_RO;
1008
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return 0;
1011
1012}
Dave Airlied84f76d2005-07-10 17:04:22 +10001013EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Dave Airlie84b1fd12007-07-11 15:53:27 +10001015static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Dave Airliecdd55a22007-07-11 16:32:08 +10001017 struct drm_device_dma *dma = dev->dma;
1018 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001019 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 unsigned long offset;
1021 unsigned long agp_offset;
1022 int count;
1023 int order;
1024 int size;
1025 int alignment;
1026 int page_order;
1027 int total;
1028 int byte_count;
1029 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001030 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 if (!drm_core_check_feature(dev, DRIVER_SG))
1033 return -EINVAL;
1034
1035 if (!dma)
1036 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Dave Airlied985c102006-01-02 21:32:48 +11001038 if (!capable(CAP_SYS_ADMIN))
1039 return -EPERM;
1040
Dave Airlied59431b2005-07-10 15:00:06 +10001041 count = request->count;
1042 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 size = 1 << order;
1044
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 alignment = (request->flags & _DRM_PAGE_ALIGN)
1046 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1048 total = PAGE_SIZE << page_order;
1049
1050 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001051 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 DRM_DEBUG("count: %d\n", count);
1054 DRM_DEBUG("order: %d\n", order);
1055 DRM_DEBUG("size: %d\n", size);
1056 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1057 DRM_DEBUG("alignment: %d\n", alignment);
1058 DRM_DEBUG("page_order: %d\n", page_order);
1059 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1062 return -EINVAL;
1063 if (dev->queue_count)
1064 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 spin_lock(&dev->count_lock);
1067 if (dev->buf_use) {
1068 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -EBUSY;
1070 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 atomic_inc(&dev->buf_alloc);
1072 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dave Airlie30e2fb12006-02-02 19:37:46 +11001074 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001077 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 atomic_dec(&dev->buf_alloc);
1079 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001083 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EINVAL;
1086 }
1087
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001088 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1089 DRM_MEM_BUFS);
1090 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001091 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -ENOMEM;
1094 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 entry->buf_size = size;
1098 entry->page_order = page_order;
1099
1100 offset = 0;
1101
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 while (entry->buf_count < count) {
1103 buf = &entry->buflist[entry->buf_count];
1104 buf->idx = dma->buf_count + entry->buf_count;
1105 buf->total = alignment;
1106 buf->order = order;
1107 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001112 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001113 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 buf->waiting = 0;
1115 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001117 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1121 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Set count correctly so we free the proper amount. */
1123 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001125 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return -ENOMEM;
1128 }
1129
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 offset += alignment;
1135 entry->buf_count++;
1136 byte_count += PAGE_SIZE << page_order;
1137 }
1138
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 temp_buflist = drm_realloc(dma->buflist,
1142 dma->buf_count * sizeof(*dma->buflist),
1143 (dma->buf_count + entry->buf_count)
1144 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1145 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001147 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001148 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -ENOMEM;
1151 }
1152 dma->buflist = temp_buflist;
1153
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1156 }
1157
1158 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001159 dma->seg_count += entry->seg_count;
1160 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 dma->byte_count += byte_count;
1162
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1164 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Dave Airlie30e2fb12006-02-02 19:37:46 +11001166 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Dave Airlied59431b2005-07-10 15:00:06 +10001168 request->count = entry->buf_count;
1169 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 dma->flags = _DRM_DMA_USE_SG;
1172
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 0;
1175}
1176
Dave Airlie84b1fd12007-07-11 15:53:27 +10001177static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001178{
Dave Airliecdd55a22007-07-11 16:32:08 +10001179 struct drm_device_dma *dma = dev->dma;
1180 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001181 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001182 unsigned long offset;
1183 unsigned long agp_offset;
1184 int count;
1185 int order;
1186 int size;
1187 int alignment;
1188 int page_order;
1189 int total;
1190 int byte_count;
1191 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001192 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001193
1194 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1195 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196
Dave Airlieb84397d62005-07-10 14:46:12 +10001197 if (!dma)
1198 return -EINVAL;
1199
Dave Airlied985c102006-01-02 21:32:48 +11001200 if (!capable(CAP_SYS_ADMIN))
1201 return -EPERM;
1202
Dave Airlied59431b2005-07-10 15:00:06 +10001203 count = request->count;
1204 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001205 size = 1 << order;
1206
Dave Airlied59431b2005-07-10 15:00:06 +10001207 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001208 ? PAGE_ALIGN(size) : size;
1209 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1210 total = PAGE_SIZE << page_order;
1211
1212 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001213 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001214
1215 DRM_DEBUG("count: %d\n", count);
1216 DRM_DEBUG("order: %d\n", order);
1217 DRM_DEBUG("size: %d\n", size);
1218 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1219 DRM_DEBUG("alignment: %d\n", alignment);
1220 DRM_DEBUG("page_order: %d\n", page_order);
1221 DRM_DEBUG("total: %d\n", total);
1222
1223 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1224 return -EINVAL;
1225 if (dev->queue_count)
1226 return -EBUSY; /* Not while in use */
1227
1228 spin_lock(&dev->count_lock);
1229 if (dev->buf_use) {
1230 spin_unlock(&dev->count_lock);
1231 return -EBUSY;
1232 }
1233 atomic_inc(&dev->buf_alloc);
1234 spin_unlock(&dev->count_lock);
1235
Dave Airlie30e2fb12006-02-02 19:37:46 +11001236 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001237 entry = &dma->bufs[order];
1238 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001239 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001240 atomic_dec(&dev->buf_alloc);
1241 return -ENOMEM; /* May only call once for each order */
1242 }
1243
1244 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001245 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001246 atomic_dec(&dev->buf_alloc);
1247 return -EINVAL;
1248 }
1249
1250 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1251 DRM_MEM_BUFS);
1252 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001253 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001254 atomic_dec(&dev->buf_alloc);
1255 return -ENOMEM;
1256 }
1257 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1258
1259 entry->buf_size = size;
1260 entry->page_order = page_order;
1261
1262 offset = 0;
1263
1264 while (entry->buf_count < count) {
1265 buf = &entry->buflist[entry->buf_count];
1266 buf->idx = dma->buf_count + entry->buf_count;
1267 buf->total = alignment;
1268 buf->order = order;
1269 buf->used = 0;
1270
1271 buf->offset = (dma->byte_count + offset);
1272 buf->bus_address = agp_offset + offset;
1273 buf->address = (void *)(agp_offset + offset);
1274 buf->next = NULL;
1275 buf->waiting = 0;
1276 buf->pending = 0;
1277 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001278 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001279
1280 buf->dev_priv_size = dev->driver->dev_priv_size;
1281 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1282 if (!buf->dev_private) {
1283 /* Set count correctly so we free the proper amount. */
1284 entry->buf_count = count;
1285 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001286 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001287 atomic_dec(&dev->buf_alloc);
1288 return -ENOMEM;
1289 }
1290 memset(buf->dev_private, 0, buf->dev_priv_size);
1291
1292 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1293
1294 offset += alignment;
1295 entry->buf_count++;
1296 byte_count += PAGE_SIZE << page_order;
1297 }
1298
1299 DRM_DEBUG("byte_count: %d\n", byte_count);
1300
1301 temp_buflist = drm_realloc(dma->buflist,
1302 dma->buf_count * sizeof(*dma->buflist),
1303 (dma->buf_count + entry->buf_count)
1304 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1305 if (!temp_buflist) {
1306 /* Free the entry because it isn't valid */
1307 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001308 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001309 atomic_dec(&dev->buf_alloc);
1310 return -ENOMEM;
1311 }
1312 dma->buflist = temp_buflist;
1313
1314 for (i = 0; i < entry->buf_count; i++) {
1315 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1316 }
1317
1318 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001319 dma->seg_count += entry->seg_count;
1320 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001321 dma->byte_count += byte_count;
1322
1323 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1324 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1325
Dave Airlie30e2fb12006-02-02 19:37:46 +11001326 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001327
Dave Airlied59431b2005-07-10 15:00:06 +10001328 request->count = entry->buf_count;
1329 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001330
1331 dma->flags = _DRM_DMA_USE_FB;
1332
1333 atomic_dec(&dev->buf_alloc);
1334 return 0;
1335}
Dave Airlied985c102006-01-02 21:32:48 +11001336
Dave Airlieb84397d62005-07-10 14:46:12 +10001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338/**
1339 * Add buffers for DMA transfers (ioctl).
1340 *
1341 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001342 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001344 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * \return zero on success or a negative number on failure.
1346 *
1347 * According with the memory type specified in drm_buf_desc::flags and the
1348 * build options, it dispatches the call either to addbufs_agp(),
1349 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1350 * PCI memory respectively.
1351 */
Eric Anholtc153f452007-09-03 12:06:45 +10001352int drm_addbufs(struct drm_device *dev, void *data,
1353 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Eric Anholtc153f452007-09-03 12:06:45 +10001355 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001356 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1359 return -EINVAL;
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001362 if (request->flags & _DRM_AGP_BUFFER)
1363 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 else
1365#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001366 if (request->flags & _DRM_SG_BUFFER)
1367 ret = drm_addbufs_sg(dev, request);
1368 else if (request->flags & _DRM_FB_BUFFER)
1369 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 else
Eric Anholtc153f452007-09-03 12:06:45 +10001371 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001372
Dave Airlied59431b2005-07-10 15:00:06 +10001373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/**
1377 * Get information about the buffer mappings.
1378 *
1379 * This was originally mean for debugging purposes, or by a sophisticated
1380 * client library to determine how best to use the available buffers (e.g.,
1381 * large buffers can be used for image transfer).
1382 *
1383 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001384 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * \param cmd command.
1386 * \param arg pointer to a drm_buf_info structure.
1387 * \return zero on success or a negative number on failure.
1388 *
1389 * Increments drm_device::buf_use while holding the drm_device::count_lock
1390 * lock, preventing of allocating more buffers after this call. Information
1391 * about each requested buffer is then copied into user space.
1392 */
Eric Anholtc153f452007-09-03 12:06:45 +10001393int drm_infobufs(struct drm_device *dev, void *data,
1394 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Dave Airliecdd55a22007-07-11 16:32:08 +10001396 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001397 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int i;
1399 int count;
1400
1401 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1402 return -EINVAL;
1403
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404 if (!dma)
1405 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001407 spin_lock(&dev->count_lock);
1408 if (atomic_read(&dev->buf_alloc)) {
1409 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EBUSY;
1411 }
1412 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001413 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1416 if (dma->bufs[i].buf_count)
1417 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001420 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Eric Anholtc153f452007-09-03 12:06:45 +10001422 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001423 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1424 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001425 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001426 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001427 struct drm_buf_entry *from = &dma->bufs[i];
1428 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001429 if (copy_to_user(&to->count,
1430 &from->buf_count,
1431 sizeof(from->buf_count)) ||
1432 copy_to_user(&to->size,
1433 &from->buf_size,
1434 sizeof(from->buf_size)) ||
1435 copy_to_user(&to->low_mark,
1436 &list->low_mark,
1437 sizeof(list->low_mark)) ||
1438 copy_to_user(&to->high_mark,
1439 &list->high_mark,
1440 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return -EFAULT;
1442
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 DRM_DEBUG("%d %d %d %d %d\n",
1444 i,
1445 dma->bufs[i].buf_count,
1446 dma->bufs[i].buf_size,
1447 dma->bufs[i].freelist.low_mark,
1448 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 ++count;
1450 }
1451 }
1452 }
Eric Anholtc153f452007-09-03 12:06:45 +10001453 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 return 0;
1456}
1457
1458/**
1459 * Specifies a low and high water mark for buffer allocation
1460 *
1461 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001462 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * \param cmd command.
1464 * \param arg a pointer to a drm_buf_desc structure.
1465 * \return zero on success or a negative number on failure.
1466 *
1467 * Verifies that the size order is bounded between the admissible orders and
1468 * updates the respective drm_device_dma::bufs entry low and high water mark.
1469 *
1470 * \note This ioctl is deprecated and mostly never used.
1471 */
Eric Anholtc153f452007-09-03 12:06:45 +10001472int drm_markbufs(struct drm_device *dev, void *data,
1473 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Dave Airliecdd55a22007-07-11 16:32:08 +10001475 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001476 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001478 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1481 return -EINVAL;
1482
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001483 if (!dma)
1484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001487 request->size, request->low_mark, request->high_mark);
1488 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 entry = &dma->bufs[order];
1492
Eric Anholtc153f452007-09-03 12:06:45 +10001493 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001495 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return -EINVAL;
1497
Eric Anholtc153f452007-09-03 12:06:45 +10001498 entry->freelist.low_mark = request->low_mark;
1499 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 return 0;
1502}
1503
1504/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 *
1507 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001508 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * \param cmd command.
1510 * \param arg pointer to a drm_buf_free structure.
1511 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * Calls free_buffer() for each used buffer.
1514 * This function is primarily used for debugging.
1515 */
Eric Anholtc153f452007-09-03 12:06:45 +10001516int drm_freebufs(struct drm_device *dev, void *data,
1517 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Dave Airliecdd55a22007-07-11 16:32:08 +10001519 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001520 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 int i;
1522 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001523 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1526 return -EINVAL;
1527
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001528 if (!dma)
1529 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Eric Anholtc153f452007-09-03 12:06:45 +10001531 DRM_DEBUG("%d\n", request->count);
1532 for (i = 0; i < request->count; i++) {
1533 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001535 if (idx < 0 || idx >= dma->buf_count) {
1536 DRM_ERROR("Index %d (of %d max)\n",
1537 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return -EINVAL;
1539 }
1540 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001541 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001542 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001543 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return -EINVAL;
1545 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001546 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
1549 return 0;
1550}
1551
1552/**
1553 * Maps all of the DMA buffers into client-virtual space (ioctl).
1554 *
1555 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001556 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * \param cmd command.
1558 * \param arg pointer to a drm_buf_map structure.
1559 * \return zero on success or a negative number on failure.
1560 *
George Sapountzis3417f332006-10-24 12:03:04 -07001561 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1562 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1563 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1564 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
Eric Anholtc153f452007-09-03 12:06:45 +10001566int drm_mapbufs(struct drm_device *dev, void *data,
1567 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Dave Airliecdd55a22007-07-11 16:32:08 +10001569 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int retcode = 0;
1571 const int zero = 0;
1572 unsigned long virtual;
1573 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001574 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int i;
1576
1577 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1578 return -EINVAL;
1579
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001580 if (!dma)
1581 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 spin_lock(&dev->count_lock);
1584 if (atomic_read(&dev->buf_alloc)) {
1585 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return -EBUSY;
1587 }
1588 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Eric Anholtc153f452007-09-03 12:06:45 +10001591 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001592 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001593 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001594 && (dma->flags & _DRM_DMA_USE_SG))
1595 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1596 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001597 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001598 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001600 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 retcode = -EINVAL;
1602 goto done;
1603 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001604 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001605 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001606 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001607 MAP_SHARED,
1608 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001609 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001612 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001613 PROT_READ | PROT_WRITE,
1614 MAP_SHARED, 0);
1615 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001617 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /* Real error */
1619 retcode = (signed long)virtual;
1620 goto done;
1621 }
Eric Anholtc153f452007-09-03 12:06:45 +10001622 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001624 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001625 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001626 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001627 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 retcode = -EFAULT;
1629 goto done;
1630 }
Eric Anholtc153f452007-09-03 12:06:45 +10001631 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001632 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001633 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 retcode = -EFAULT;
1635 goto done;
1636 }
Eric Anholtc153f452007-09-03 12:06:45 +10001637 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001638 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 retcode = -EFAULT;
1640 goto done;
1641 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001642 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001643 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001644 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 retcode = -EFAULT;
1646 goto done;
1647 }
1648 }
1649 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001650 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001651 request->count = dma->buf_count;
1652 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 return retcode;
1655}
1656
Dave Airlie836cf042005-07-10 19:27:04 +10001657/**
1658 * Compute size order. Returns the exponent of the smaller power of two which
1659 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001660 *
Dave Airlie836cf042005-07-10 19:27:04 +10001661 * \param size size.
1662 * \return order.
1663 *
1664 * \todo Can be made faster.
1665 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001666int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001667{
1668 int order;
1669 unsigned long tmp;
1670
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001671 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001672
1673 if (size & (size - 1))
1674 ++order;
1675
1676 return order;
1677}
1678EXPORT_SYMBOL(drm_order);