blob: 80a257554b307a4bc0346e5f38e3c154ec22f4c5 [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
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000374 if (!(map->flags & _DRM_DRIVER))
375 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000376 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000377 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100378 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000379
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100380int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000381 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100382 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000383{
Dave Airlie55910512007-07-11 16:53:40 +1000384 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000385 int rc;
386
387 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
388 if (!rc)
389 *map_ptr = list->map;
390 return rc;
391}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392
Dave Airlie7ab98402005-07-10 16:56:52 +1000393EXPORT_SYMBOL(drm_addmap);
394
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100395/**
396 * Ioctl to specify a range of memory that is available for mapping by a
397 * non-root process.
398 *
399 * \param inode device inode.
400 * \param file_priv DRM file private.
401 * \param cmd command.
402 * \param arg pointer to a drm_map structure.
403 * \return zero on success or a negative value on error.
404 *
405 */
Eric Anholtc153f452007-09-03 12:06:45 +1000406int drm_addmap_ioctl(struct drm_device *dev, void *data,
407 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000408{
Eric Anholtc153f452007-09-03 12:06:45 +1000409 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000410 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000411 int err;
412
Dave Airlie7c1c2872008-11-28 14:22:24 +1000413 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100414 return -EPERM;
415
Eric Anholtc153f452007-09-03 12:06:45 +1000416 err = drm_addmap_core(dev, map->offset, map->size, map->type,
417 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000418
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000420 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000421
Dave Airlie67e1a012005-10-24 18:41:39 +1000422 /* 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 +1000423 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * Remove a map private from list and deallocate resources if the mapping
429 * isn't in use.
430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * Searches the map on drm_device::maplist, removes it from the list, see if
432 * its being used, and free any associate resource (such as MTRR's) if it's not
433 * being on use.
434 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000435 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100437int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Dave Airlie55910512007-07-11 16:53:40 +1000439 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000440 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000441 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000442 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Dave Airlie836cf042005-07-10 19:27:04 +1000444 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000445 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000446 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000447 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000448 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100449 drm_ht_remove_key(&dev->map_hash,
450 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000451 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
452 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Dave Airlie836cf042005-07-10 19:27:04 +1000456
Dave Airliebd1b3312007-05-26 05:01:51 +1000457 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000458 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000459
460 switch (map->type) {
461 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100462 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000463 /* FALLTHROUGH */
464 case _DRM_FRAME_BUFFER:
465 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
466 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 retcode = mtrr_del(map->mtrr, map->offset, map->size);
468 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000469 }
470 break;
471 case _DRM_SHM:
472 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000473 if (master) {
474 if (dev->sigdata.lock == master->lock.hw_lock)
475 dev->sigdata.lock = NULL;
476 master->lock.hw_lock = NULL; /* SHM removed */
477 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100478 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000479 }
Dave Airlie836cf042005-07-10 19:27:04 +1000480 break;
481 case _DRM_AGP:
482 case _DRM_SCATTER_GATHER:
483 break;
484 case _DRM_CONSISTENT:
485 dmah.vaddr = map->handle;
486 dmah.busaddr = map->offset;
487 dmah.size = map->size;
488 __drm_pci_free(dev, &dmah);
489 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800490 case _DRM_GEM:
491 DRM_ERROR("tried to rmmap GEM object\n");
492 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000493 }
494 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
497}
Dave Airlie4e74f362008-12-19 10:23:14 +1100498EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000499
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100500int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000501{
502 int ret;
503
Dave Airlie30e2fb12006-02-02 19:37:46 +1100504 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000505 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100506 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000507
508 return ret;
509}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000510EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000511
Dave Airlie836cf042005-07-10 19:27:04 +1000512/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
513 * the last close of the device, and this is necessary for cleanup when things
514 * exit uncleanly. Therefore, having userland manually remove mappings seems
515 * like a pointless exercise since they're going away anyway.
516 *
517 * One use case might be after addmap is allowed for normal users for SHM and
518 * gets used by drivers that the server doesn't need to care about. This seems
519 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100520 *
521 * \param inode device inode.
522 * \param file_priv DRM file private.
523 * \param cmd command.
524 * \param arg pointer to a struct drm_map structure.
525 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000526 */
Eric Anholtc153f452007-09-03 12:06:45 +1000527int drm_rmmap_ioctl(struct drm_device *dev, void *data,
528 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000529{
Eric Anholtc153f452007-09-03 12:06:45 +1000530 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100531 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000532 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000533 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000534
Dave Airlie30e2fb12006-02-02 19:37:46 +1100535 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000536 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000537 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000538 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000539 r_list->map->flags & _DRM_REMOVABLE) {
540 map = r_list->map;
541 break;
542 }
543 }
544
545 /* List has wrapped around to the head pointer, or its empty we didn't
546 * find anything.
547 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000548 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100549 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000550 return -EINVAL;
551 }
552
Dave Airlie836cf042005-07-10 19:27:04 +1000553 /* Register and framebuffer maps are permanent */
554 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100555 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000556 return 0;
557 }
558
559 ret = drm_rmmap_locked(dev, map);
560
Dave Airlie30e2fb12006-02-02 19:37:46 +1100561 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000562
563 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000564}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566/**
567 * Cleanup after an error on one of the addbufs() functions.
568 *
Dave Airlie836cf042005-07-10 19:27:04 +1000569 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * \param entry buffer entry where the error occurred.
571 *
572 * Frees any pages and buffers associated with the given entry.
573 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000574static void drm_cleanup_buf_error(struct drm_device * dev,
575 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 int i;
578
579 if (entry->seg_count) {
580 for (i = 0; i < entry->seg_count; i++) {
581 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100582 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 }
585 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 entry->seg_count *
587 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 entry->seg_count = 0;
590 }
591
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 if (entry->buf_count) {
593 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (entry->buflist[i].dev_private) {
595 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 entry->buflist[i].dev_priv_size,
597 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 }
600 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 entry->buf_count *
602 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 entry->buf_count = 0;
605 }
606}
607
608#if __OS_HAS_AGP
609/**
Dave Airlied59431b2005-07-10 15:00:06 +1000610 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000612 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000613 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * After some sanity checks creates a drm_buf structure for each buffer and
617 * reallocates the buffer list of the same size order to accommodate the new
618 * buffers.
619 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000620int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Dave Airliecdd55a22007-07-11 16:32:08 +1000622 struct drm_device_dma *dma = dev->dma;
623 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000624 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000625 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned long offset;
627 unsigned long agp_offset;
628 int count;
629 int order;
630 int size;
631 int alignment;
632 int page_order;
633 int total;
634 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100635 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000636 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 if (!dma)
639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dave Airlied59431b2005-07-10 15:00:06 +1000641 count = request->count;
642 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 size = 1 << order;
644
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 alignment = (request->flags & _DRM_PAGE_ALIGN)
646 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
648 total = PAGE_SIZE << page_order;
649
650 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000651 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 DRM_DEBUG("count: %d\n", count);
654 DRM_DEBUG("order: %d\n", order);
655 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100656 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 DRM_DEBUG("alignment: %d\n", alignment);
658 DRM_DEBUG("page_order: %d\n", page_order);
659 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
662 return -EINVAL;
663 if (dev->queue_count)
664 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dave Airlie54ba2f72007-02-10 12:07:47 +1100666 /* Make sure buffers are located in AGP memory that we own */
667 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000668 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100669 if ((agp_offset >= agp_entry->bound) &&
670 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
671 valid = 1;
672 break;
673 }
674 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000675 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100676 DRM_DEBUG("zone invalid\n");
677 return -EINVAL;
678 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 spin_lock(&dev->count_lock);
680 if (dev->buf_use) {
681 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EBUSY;
683 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 atomic_inc(&dev->buf_alloc);
685 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dave Airlie30e2fb12006-02-02 19:37:46 +1100687 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100690 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 atomic_dec(&dev->buf_alloc);
692 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
695 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100696 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -EINVAL;
699 }
700
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
702 DRM_MEM_BUFS);
703 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100704 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -ENOMEM;
707 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 entry->buf_size = size;
711 entry->page_order = page_order;
712
713 offset = 0;
714
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 while (entry->buf_count < count) {
716 buf = &entry->buflist[entry->buf_count];
717 buf->idx = dma->buf_count + entry->buf_count;
718 buf->total = alignment;
719 buf->order = order;
720 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 buf->bus_address = agp_offset + offset;
724 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 buf->waiting = 0;
727 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000729 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
733 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* Set count correctly so we free the proper amount. */
735 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100737 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -ENOMEM;
740 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 offset += alignment;
746 entry->buf_count++;
747 byte_count += PAGE_SIZE << page_order;
748 }
749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000752 temp_buflist = drm_realloc(dma->buflist,
753 dma->buf_count * sizeof(*dma->buflist),
754 (dma->buf_count + entry->buf_count)
755 * sizeof(*dma->buflist), DRM_MEM_BUFS);
756 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100759 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -ENOMEM;
762 }
763 dma->buflist = temp_buflist;
764
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
767 }
768
769 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100770 dma->seg_count += entry->seg_count;
771 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 dma->byte_count += byte_count;
773
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
775 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dave Airlie30e2fb12006-02-02 19:37:46 +1100777 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Airlied59431b2005-07-10 15:00:06 +1000779 request->count = entry->buf_count;
780 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 dma->flags = _DRM_DMA_USE_AGP;
783
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787EXPORT_SYMBOL(drm_addbufs_agp);
788#endif /* __OS_HAS_AGP */
789
Dave Airlie84b1fd12007-07-11 15:53:27 +1000790int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Dave Airliecdd55a22007-07-11 16:32:08 +1000792 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 int count;
794 int order;
795 int size;
796 int total;
797 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000798 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100799 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000800 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int alignment;
802 unsigned long offset;
803 int i;
804 int byte_count;
805 int page_count;
806 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000807 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
810 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100811
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 if (!dma)
813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Dave Airlied985c102006-01-02 21:32:48 +1100815 if (!capable(CAP_SYS_ADMIN))
816 return -EPERM;
817
Dave Airlied59431b2005-07-10 15:00:06 +1000818 count = request->count;
819 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 size = 1 << order;
821
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
823 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
826 return -EINVAL;
827 if (dev->queue_count)
828 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Dave Airlied59431b2005-07-10 15:00:06 +1000830 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
833 total = PAGE_SIZE << page_order;
834
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 spin_lock(&dev->count_lock);
836 if (dev->buf_use) {
837 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return -EBUSY;
839 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 atomic_inc(&dev->buf_alloc);
841 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Dave Airlie30e2fb12006-02-02 19:37:46 +1100843 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100846 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -ENOMEM; /* May only call once for each order */
849 }
850
851 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100852 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -EINVAL;
855 }
856
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
858 DRM_MEM_BUFS);
859 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100860 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -ENOMEM;
863 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
867 DRM_MEM_SEGS);
868 if (!entry->seglist) {
869 drm_free(entry->buflist,
870 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100871 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -ENOMEM;
874 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /* Keep the original pagelist until we know all the allocations
878 * have succeeded
879 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
881 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000883 drm_free(entry->buflist,
884 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
885 drm_free(entry->seglist,
886 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100887 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENOMEM;
890 }
891 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
893 DRM_DEBUG("pagelist: %d entries\n",
894 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 entry->page_order = page_order;
898 byte_count = 0;
899 page_count = 0;
900
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000902
Dave Airlieddf19b92006-03-19 18:56:12 +1100903 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
Dave Airliebc5f4522007-11-05 12:50:58 +1000904
Dave Airlieddf19b92006-03-19 18:56:12 +1100905 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /* Set count correctly so we free the proper amount. */
907 entry->buf_count = count;
908 entry->seg_count = count;
909 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 drm_free(temp_pagelist,
911 (dma->page_count + (count << page_order))
912 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100913 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -ENOMEM;
916 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100917 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 for (i = 0; i < (1 << page_order); i++) {
919 DRM_DEBUG("page %d @ 0x%08lx\n",
920 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100921 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100923 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 for (offset = 0;
926 offset + size <= total && entry->buf_count < count;
927 offset += alignment, ++entry->buf_count) {
928 buf = &entry->buflist[entry->buf_count];
929 buf->idx = dma->buf_count + entry->buf_count;
930 buf->total = alignment;
931 buf->order = order;
932 buf->used = 0;
933 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100934 buf->address = (void *)(dmah->vaddr + offset);
935 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 buf->waiting = 0;
938 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000940 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 buf->dev_private = drm_alloc(buf->dev_priv_size,
944 DRM_MEM_BUFS);
945 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* Set count correctly so we free the proper amount. */
947 entry->buf_count = count;
948 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 drm_cleanup_buf_error(dev, entry);
950 drm_free(temp_pagelist,
951 (dma->page_count +
952 (count << page_order))
953 * sizeof(*dma->pagelist),
954 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100955 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -ENOMEM;
958 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000959 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 DRM_DEBUG("buffer %d @ %p\n",
962 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 byte_count += PAGE_SIZE << page_order;
965 }
966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 temp_buflist = drm_realloc(dma->buflist,
968 dma->buf_count * sizeof(*dma->buflist),
969 (dma->buf_count + entry->buf_count)
970 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!temp_buflist) {
972 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000973 drm_cleanup_buf_error(dev, entry);
974 drm_free(temp_pagelist,
975 (dma->page_count + (count << page_order))
976 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100977 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000978 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -ENOMEM;
980 }
981 dma->buflist = temp_buflist;
982
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000983 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
985 }
986
987 /* No allocations failed, so now we can replace the orginal pagelist
988 * with the new one.
989 */
990 if (dma->page_count) {
991 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 dma->page_count * sizeof(*dma->pagelist),
993 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 dma->pagelist = temp_pagelist;
996
997 dma->buf_count += entry->buf_count;
998 dma->seg_count += entry->seg_count;
999 dma->page_count += entry->seg_count << page_order;
1000 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1001
Dave Airlie30e2fb12006-02-02 19:37:46 +11001002 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Dave Airlied59431b2005-07-10 15:00:06 +10001004 request->count = entry->buf_count;
1005 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
George Sapountzis3417f332006-10-24 12:03:04 -07001007 if (request->flags & _DRM_PCI_BUFFER_RO)
1008 dma->flags = _DRM_DMA_USE_PCI_RO;
1009
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return 0;
1012
1013}
Dave Airlied84f76d2005-07-10 17:04:22 +10001014EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dave Airlie84b1fd12007-07-11 15:53:27 +10001016static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Dave Airliecdd55a22007-07-11 16:32:08 +10001018 struct drm_device_dma *dma = dev->dma;
1019 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001020 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 unsigned long offset;
1022 unsigned long agp_offset;
1023 int count;
1024 int order;
1025 int size;
1026 int alignment;
1027 int page_order;
1028 int total;
1029 int byte_count;
1030 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001031 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 if (!drm_core_check_feature(dev, DRIVER_SG))
1034 return -EINVAL;
1035
1036 if (!dma)
1037 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Dave Airlied985c102006-01-02 21:32:48 +11001039 if (!capable(CAP_SYS_ADMIN))
1040 return -EPERM;
1041
Dave Airlied59431b2005-07-10 15:00:06 +10001042 count = request->count;
1043 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 size = 1 << order;
1045
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 alignment = (request->flags & _DRM_PAGE_ALIGN)
1047 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1049 total = PAGE_SIZE << page_order;
1050
1051 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001052 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001054 DRM_DEBUG("count: %d\n", count);
1055 DRM_DEBUG("order: %d\n", order);
1056 DRM_DEBUG("size: %d\n", size);
1057 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1058 DRM_DEBUG("alignment: %d\n", alignment);
1059 DRM_DEBUG("page_order: %d\n", page_order);
1060 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1063 return -EINVAL;
1064 if (dev->queue_count)
1065 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 spin_lock(&dev->count_lock);
1068 if (dev->buf_use) {
1069 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return -EBUSY;
1071 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 atomic_inc(&dev->buf_alloc);
1073 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Dave Airlie30e2fb12006-02-02 19:37:46 +11001075 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001078 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 atomic_dec(&dev->buf_alloc);
1080 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
1083 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001084 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -EINVAL;
1087 }
1088
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1090 DRM_MEM_BUFS);
1091 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001092 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return -ENOMEM;
1095 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 entry->buf_size = size;
1099 entry->page_order = page_order;
1100
1101 offset = 0;
1102
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 while (entry->buf_count < count) {
1104 buf = &entry->buflist[entry->buf_count];
1105 buf->idx = dma->buf_count + entry->buf_count;
1106 buf->total = alignment;
1107 buf->order = order;
1108 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001113 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 buf->waiting = 0;
1116 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001118 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1122 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Set count correctly so we free the proper amount. */
1124 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001126 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -ENOMEM;
1129 }
1130
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 offset += alignment;
1136 entry->buf_count++;
1137 byte_count += PAGE_SIZE << page_order;
1138 }
1139
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 temp_buflist = drm_realloc(dma->buflist,
1143 dma->buf_count * sizeof(*dma->buflist),
1144 (dma->buf_count + entry->buf_count)
1145 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1146 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001148 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001149 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return -ENOMEM;
1152 }
1153 dma->buflist = temp_buflist;
1154
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1157 }
1158
1159 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001160 dma->seg_count += entry->seg_count;
1161 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 dma->byte_count += byte_count;
1163
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001164 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1165 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Dave Airlie30e2fb12006-02-02 19:37:46 +11001167 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Dave Airlied59431b2005-07-10 15:00:06 +10001169 request->count = entry->buf_count;
1170 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 dma->flags = _DRM_DMA_USE_SG;
1173
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001174 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return 0;
1176}
1177
Dave Airlie84b1fd12007-07-11 15:53:27 +10001178static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001179{
Dave Airliecdd55a22007-07-11 16:32:08 +10001180 struct drm_device_dma *dma = dev->dma;
1181 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001182 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001183 unsigned long offset;
1184 unsigned long agp_offset;
1185 int count;
1186 int order;
1187 int size;
1188 int alignment;
1189 int page_order;
1190 int total;
1191 int byte_count;
1192 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001193 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001194
1195 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1196 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001197
Dave Airlieb84397d62005-07-10 14:46:12 +10001198 if (!dma)
1199 return -EINVAL;
1200
Dave Airlied985c102006-01-02 21:32:48 +11001201 if (!capable(CAP_SYS_ADMIN))
1202 return -EPERM;
1203
Dave Airlied59431b2005-07-10 15:00:06 +10001204 count = request->count;
1205 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001206 size = 1 << order;
1207
Dave Airlied59431b2005-07-10 15:00:06 +10001208 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001209 ? PAGE_ALIGN(size) : size;
1210 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1211 total = PAGE_SIZE << page_order;
1212
1213 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001214 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001215
1216 DRM_DEBUG("count: %d\n", count);
1217 DRM_DEBUG("order: %d\n", order);
1218 DRM_DEBUG("size: %d\n", size);
1219 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1220 DRM_DEBUG("alignment: %d\n", alignment);
1221 DRM_DEBUG("page_order: %d\n", page_order);
1222 DRM_DEBUG("total: %d\n", total);
1223
1224 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1225 return -EINVAL;
1226 if (dev->queue_count)
1227 return -EBUSY; /* Not while in use */
1228
1229 spin_lock(&dev->count_lock);
1230 if (dev->buf_use) {
1231 spin_unlock(&dev->count_lock);
1232 return -EBUSY;
1233 }
1234 atomic_inc(&dev->buf_alloc);
1235 spin_unlock(&dev->count_lock);
1236
Dave Airlie30e2fb12006-02-02 19:37:46 +11001237 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001238 entry = &dma->bufs[order];
1239 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001240 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001241 atomic_dec(&dev->buf_alloc);
1242 return -ENOMEM; /* May only call once for each order */
1243 }
1244
1245 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001246 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001247 atomic_dec(&dev->buf_alloc);
1248 return -EINVAL;
1249 }
1250
1251 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1252 DRM_MEM_BUFS);
1253 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001254 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001255 atomic_dec(&dev->buf_alloc);
1256 return -ENOMEM;
1257 }
1258 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1259
1260 entry->buf_size = size;
1261 entry->page_order = page_order;
1262
1263 offset = 0;
1264
1265 while (entry->buf_count < count) {
1266 buf = &entry->buflist[entry->buf_count];
1267 buf->idx = dma->buf_count + entry->buf_count;
1268 buf->total = alignment;
1269 buf->order = order;
1270 buf->used = 0;
1271
1272 buf->offset = (dma->byte_count + offset);
1273 buf->bus_address = agp_offset + offset;
1274 buf->address = (void *)(agp_offset + offset);
1275 buf->next = NULL;
1276 buf->waiting = 0;
1277 buf->pending = 0;
1278 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001279 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001280
1281 buf->dev_priv_size = dev->driver->dev_priv_size;
1282 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1283 if (!buf->dev_private) {
1284 /* Set count correctly so we free the proper amount. */
1285 entry->buf_count = count;
1286 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001287 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001288 atomic_dec(&dev->buf_alloc);
1289 return -ENOMEM;
1290 }
1291 memset(buf->dev_private, 0, buf->dev_priv_size);
1292
1293 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1294
1295 offset += alignment;
1296 entry->buf_count++;
1297 byte_count += PAGE_SIZE << page_order;
1298 }
1299
1300 DRM_DEBUG("byte_count: %d\n", byte_count);
1301
1302 temp_buflist = drm_realloc(dma->buflist,
1303 dma->buf_count * sizeof(*dma->buflist),
1304 (dma->buf_count + entry->buf_count)
1305 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1306 if (!temp_buflist) {
1307 /* Free the entry because it isn't valid */
1308 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001309 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001310 atomic_dec(&dev->buf_alloc);
1311 return -ENOMEM;
1312 }
1313 dma->buflist = temp_buflist;
1314
1315 for (i = 0; i < entry->buf_count; i++) {
1316 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1317 }
1318
1319 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001320 dma->seg_count += entry->seg_count;
1321 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001322 dma->byte_count += byte_count;
1323
1324 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1325 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1326
Dave Airlie30e2fb12006-02-02 19:37:46 +11001327 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001328
Dave Airlied59431b2005-07-10 15:00:06 +10001329 request->count = entry->buf_count;
1330 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001331
1332 dma->flags = _DRM_DMA_USE_FB;
1333
1334 atomic_dec(&dev->buf_alloc);
1335 return 0;
1336}
Dave Airlied985c102006-01-02 21:32:48 +11001337
Dave Airlieb84397d62005-07-10 14:46:12 +10001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/**
1340 * Add buffers for DMA transfers (ioctl).
1341 *
1342 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001343 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001345 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * \return zero on success or a negative number on failure.
1347 *
1348 * According with the memory type specified in drm_buf_desc::flags and the
1349 * build options, it dispatches the call either to addbufs_agp(),
1350 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1351 * PCI memory respectively.
1352 */
Eric Anholtc153f452007-09-03 12:06:45 +10001353int drm_addbufs(struct drm_device *dev, void *data,
1354 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Eric Anholtc153f452007-09-03 12:06:45 +10001356 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001357 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1360 return -EINVAL;
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001363 if (request->flags & _DRM_AGP_BUFFER)
1364 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 else
1366#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001367 if (request->flags & _DRM_SG_BUFFER)
1368 ret = drm_addbufs_sg(dev, request);
1369 else if (request->flags & _DRM_FB_BUFFER)
1370 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 else
Eric Anholtc153f452007-09-03 12:06:45 +10001372 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001373
Dave Airlied59431b2005-07-10 15:00:06 +10001374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/**
1378 * Get information about the buffer mappings.
1379 *
1380 * This was originally mean for debugging purposes, or by a sophisticated
1381 * client library to determine how best to use the available buffers (e.g.,
1382 * large buffers can be used for image transfer).
1383 *
1384 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001385 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * \param cmd command.
1387 * \param arg pointer to a drm_buf_info structure.
1388 * \return zero on success or a negative number on failure.
1389 *
1390 * Increments drm_device::buf_use while holding the drm_device::count_lock
1391 * lock, preventing of allocating more buffers after this call. Information
1392 * about each requested buffer is then copied into user space.
1393 */
Eric Anholtc153f452007-09-03 12:06:45 +10001394int drm_infobufs(struct drm_device *dev, void *data,
1395 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Dave Airliecdd55a22007-07-11 16:32:08 +10001397 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001398 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 int i;
1400 int count;
1401
1402 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1403 return -EINVAL;
1404
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 if (!dma)
1406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 spin_lock(&dev->count_lock);
1409 if (atomic_read(&dev->buf_alloc)) {
1410 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -EBUSY;
1412 }
1413 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001414 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001416 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1417 if (dma->bufs[i].buf_count)
1418 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001421 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Eric Anholtc153f452007-09-03 12:06:45 +10001423 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001424 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1425 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001426 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001427 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001428 struct drm_buf_entry *from = &dma->bufs[i];
1429 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 if (copy_to_user(&to->count,
1431 &from->buf_count,
1432 sizeof(from->buf_count)) ||
1433 copy_to_user(&to->size,
1434 &from->buf_size,
1435 sizeof(from->buf_size)) ||
1436 copy_to_user(&to->low_mark,
1437 &list->low_mark,
1438 sizeof(list->low_mark)) ||
1439 copy_to_user(&to->high_mark,
1440 &list->high_mark,
1441 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EFAULT;
1443
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001444 DRM_DEBUG("%d %d %d %d %d\n",
1445 i,
1446 dma->bufs[i].buf_count,
1447 dma->bufs[i].buf_size,
1448 dma->bufs[i].freelist.low_mark,
1449 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 ++count;
1451 }
1452 }
1453 }
Eric Anholtc153f452007-09-03 12:06:45 +10001454 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 return 0;
1457}
1458
1459/**
1460 * Specifies a low and high water mark for buffer allocation
1461 *
1462 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001463 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * \param cmd command.
1465 * \param arg a pointer to a drm_buf_desc structure.
1466 * \return zero on success or a negative number on failure.
1467 *
1468 * Verifies that the size order is bounded between the admissible orders and
1469 * updates the respective drm_device_dma::bufs entry low and high water mark.
1470 *
1471 * \note This ioctl is deprecated and mostly never used.
1472 */
Eric Anholtc153f452007-09-03 12:06:45 +10001473int drm_markbufs(struct drm_device *dev, void *data,
1474 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Dave Airliecdd55a22007-07-11 16:32:08 +10001476 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001477 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001479 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1482 return -EINVAL;
1483
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484 if (!dma)
1485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001487 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001488 request->size, request->low_mark, request->high_mark);
1489 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001490 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 entry = &dma->bufs[order];
1493
Eric Anholtc153f452007-09-03 12:06:45 +10001494 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001496 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -EINVAL;
1498
Eric Anholtc153f452007-09-03 12:06:45 +10001499 entry->freelist.low_mark = request->low_mark;
1500 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 return 0;
1503}
1504
1505/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001506 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 *
1508 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001509 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * \param cmd command.
1511 * \param arg pointer to a drm_buf_free structure.
1512 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001513 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 * Calls free_buffer() for each used buffer.
1515 * This function is primarily used for debugging.
1516 */
Eric Anholtc153f452007-09-03 12:06:45 +10001517int drm_freebufs(struct drm_device *dev, void *data,
1518 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Dave Airliecdd55a22007-07-11 16:32:08 +10001520 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001521 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int i;
1523 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001524 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1527 return -EINVAL;
1528
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001529 if (!dma)
1530 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Eric Anholtc153f452007-09-03 12:06:45 +10001532 DRM_DEBUG("%d\n", request->count);
1533 for (i = 0; i < request->count; i++) {
1534 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001536 if (idx < 0 || idx >= dma->buf_count) {
1537 DRM_ERROR("Index %d (of %d max)\n",
1538 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return -EINVAL;
1540 }
1541 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001542 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001543 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001544 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return -EINVAL;
1546 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001547 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
1550 return 0;
1551}
1552
1553/**
1554 * Maps all of the DMA buffers into client-virtual space (ioctl).
1555 *
1556 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001557 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * \param cmd command.
1559 * \param arg pointer to a drm_buf_map structure.
1560 * \return zero on success or a negative number on failure.
1561 *
George Sapountzis3417f332006-10-24 12:03:04 -07001562 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1563 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1564 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1565 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 */
Eric Anholtc153f452007-09-03 12:06:45 +10001567int drm_mapbufs(struct drm_device *dev, void *data,
1568 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Dave Airliecdd55a22007-07-11 16:32:08 +10001570 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int retcode = 0;
1572 const int zero = 0;
1573 unsigned long virtual;
1574 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001575 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 int i;
1577
1578 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1579 return -EINVAL;
1580
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001581 if (!dma)
1582 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584 spin_lock(&dev->count_lock);
1585 if (atomic_read(&dev->buf_alloc)) {
1586 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -EBUSY;
1588 }
1589 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001590 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Eric Anholtc153f452007-09-03 12:06:45 +10001592 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001593 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001594 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001595 && (dma->flags & _DRM_DMA_USE_SG))
1596 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1597 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001598 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001599 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001601 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 retcode = -EINVAL;
1603 goto done;
1604 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001605 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001606 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001607 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001608 MAP_SHARED,
1609 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001610 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001612 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001613 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001614 PROT_READ | PROT_WRITE,
1615 MAP_SHARED, 0);
1616 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001618 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /* Real error */
1620 retcode = (signed long)virtual;
1621 goto done;
1622 }
Eric Anholtc153f452007-09-03 12:06:45 +10001623 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001625 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001626 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001627 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001628 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 retcode = -EFAULT;
1630 goto done;
1631 }
Eric Anholtc153f452007-09-03 12:06:45 +10001632 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001633 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001634 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 retcode = -EFAULT;
1636 goto done;
1637 }
Eric Anholtc153f452007-09-03 12:06:45 +10001638 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001639 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 retcode = -EFAULT;
1641 goto done;
1642 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001643 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001644 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001645 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 retcode = -EFAULT;
1647 goto done;
1648 }
1649 }
1650 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001651 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001652 request->count = dma->buf_count;
1653 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 return retcode;
1656}
1657
Dave Airlie836cf042005-07-10 19:27:04 +10001658/**
1659 * Compute size order. Returns the exponent of the smaller power of two which
1660 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001661 *
Dave Airlie836cf042005-07-10 19:27:04 +10001662 * \param size size.
1663 * \return order.
1664 *
1665 * \todo Can be made faster.
1666 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001667int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001668{
1669 int order;
1670 unsigned long tmp;
1671
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001672 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001673
1674 if (size & (size - 1))
1675 ++order;
1676
1677 return order;
1678}
1679EXPORT_SYMBOL(drm_order);