blob: 5a4dbb410b71591f6f7b61a7d89f6f7609438578 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
David Millerf1a2a9b2009-02-18 15:41:02 -080038#include <linux/log2.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040039#include <linux/export.h>
David Millerf1a2a9b2009-02-18 15:41:02 -080040#include <asm/shmparam.h>
David Howells760285e2012-10-02 18:01:07 +010041#include <drm/drmP.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Dave Airlie55910512007-07-11 16:53:40 +100043static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110044 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100045{
Dave Airlie55910512007-07-11 16:53:40 +100046 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100047 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110048 /*
49 * Because the kernel-userspace ABI is fixed at a 32-bit offset
Tormod Volden66aa6962011-05-30 19:45:43 +000050 * while PCI resources may live above that, we only compare the
51 * lower 32 bits of the map offset for maps of type
52 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
53 * It is assumed that if a driver have more than one resource
54 * of each type, the lower 32 bits are different.
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110055 */
56 if (!entry->map ||
57 map->type != entry->map->type ||
58 entry->master != dev->primary->master)
59 continue;
60 switch (map->type) {
61 case _DRM_SHM:
62 if (map->flags != _DRM_CONTAINS_LOCK)
63 break;
Tormod Volden66aa6962011-05-30 19:45:43 +000064 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110065 case _DRM_REGISTERS:
66 case _DRM_FRAME_BUFFER:
Tormod Volden66aa6962011-05-30 19:45:43 +000067 if ((entry->map->offset & 0xffffffff) ==
68 (map->offset & 0xffffffff))
69 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110070 default: /* Make gcc happy */
71 ;
Dave Airlie836cf042005-07-10 19:27:04 +100072 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110073 if (entry->map->offset == map->offset)
74 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100075 }
76
77 return NULL;
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Dave Airliee0be4282007-07-12 10:26:44 +100080static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080081 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100082{
David Millerf1a2a9b2009-02-18 15:41:02 -080083 int use_hashed_handle, shift;
84 unsigned long add;
85
Dave Airliec2604ce2006-08-12 16:03:26 +100086#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100087 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
88#elif (BITS_PER_LONG == 32)
89 use_hashed_handle = hashed_handle;
90#else
91#error Unsupported long size. Neither 64 nor 32 bits.
92#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100093
Thomas Hellstrome08870c2006-09-22 04:18:37 +100094 if (!use_hashed_handle) {
95 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110096 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100097 ret = drm_ht_insert_item(&dev->map_hash, hash);
98 if (ret != -EINVAL)
99 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000100 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800101
102 shift = 0;
103 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
104 if (shm && (SHMLBA > PAGE_SIZE)) {
105 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
106
107 /* For shared memory, we have to preserve the SHMLBA
108 * bits of the eventual vma->vm_pgoff value during
109 * mmap(). Otherwise we run into cache aliasing problems
110 * on some platforms. On these platforms, the pgoff of
111 * a mmap() request is used to pick a suitable virtual
112 * address for the mmap() region such that it will not
113 * cause cache aliasing problems.
114 *
115 * Therefore, make sure the SHMLBA relevant bits of the
116 * hash value we use are equal to those in the original
117 * kernel virtual address.
118 */
119 shift = bits;
120 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
121 }
122
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000123 return drm_ht_just_insert_please(&dev->map_hash, hash,
124 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800125 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000126}
Dave Airlie9a186642005-06-23 21:29:18 +1000127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100129 * Core function to create a range of memory available for mapping by a
130 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
132 * Adjusts the memory offset to its absolute value according to the mapping
133 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
134 * applicable and if supported by the kernel.
135 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100136static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000137 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000138 enum drm_map_flags flags,
139 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100141 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000142 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000143 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000144 unsigned long user_token;
145 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Eric Anholt9a298b22009-03-24 12:23:04 -0700147 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -ENOMEM;
150
Dave Airlie7ab98402005-07-10 16:56:52 +1000151 map->offset = offset;
152 map->size = size;
153 map->flags = flags;
154 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* Only allow shared memory to be removable since we only keep enough
157 * book keeping information about shared memory to allow for removal
158 * when processes fork.
159 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000160 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700161 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EINVAL;
163 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100164 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
165 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000166
167 /* page-align _DRM_SHM maps. They are allocated here so there is no security
168 * hole created by that and it works around various broken drivers that use
169 * a non-aligned quantity to map the SAREA. --BenH
170 */
171 if (map->type == _DRM_SHM)
172 map->size = PAGE_ALIGN(map->size);
173
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100174 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700175 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -EINVAL;
177 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 map->handle = NULL;
180
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case _DRM_REGISTERS:
183 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600184#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100185 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700187 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189 }
190#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000191 /* Some drivers preinitialize some maps, without the X Server
192 * needing to be aware of it. Therefore, we just return success
193 * when the server tries to create a duplicate map.
194 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000195 list = drm_find_matching_map(dev, map);
196 if (list != NULL) {
197 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000198 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 "mismatched sizes, (%ld vs %ld)\n",
200 map->type, map->size,
201 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000202 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000203 }
204
Eric Anholt9a298b22009-03-24 12:23:04 -0700205 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000206 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000207 return 0;
208 }
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 if (map->type == _DRM_FRAME_BUFFER ||
212 (map->flags & _DRM_WRITE_COMBINING)) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000213 map->mtrr =
214 arch_phys_wc_add(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 }
Scott Thompson0769d392007-08-25 18:17:49 +1000217 if (map->type == _DRM_REGISTERS) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000218 if (map->flags & _DRM_WRITE_COMBINING)
219 map->handle = ioremap_wc(map->offset,
220 map->size);
221 else
222 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000223 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700224 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000225 return -ENOMEM;
226 }
227 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100231 list = drm_find_matching_map(dev, map);
232 if (list != NULL) {
233 if(list->map->size != map->size) {
234 DRM_DEBUG("Matching maps of type %d with "
235 "mismatched sizes, (%ld vs %ld)\n",
236 map->type, map->size, list->map->size);
237 list->map->size = map->size;
238 }
239
Eric Anholt9a298b22009-03-24 12:23:04 -0700240 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100241 *maplist = list;
242 return 0;
243 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100244 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245 DRM_DEBUG("%lu %d %p\n",
246 map->size, drm_order(map->size), map->handle);
247 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700248 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ENOMEM;
250 }
251 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000254 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000255 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700256 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -EBUSY;
258 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000259 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100262 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000263 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100264 int valid = 0;
265
266 if (!drm_core_has_AGP(dev)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700267 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100270#ifdef __alpha__
271 map->offset += dev->hose->mem_space->start;
272#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000273 /* In some cases (i810 driver), user space may have already
274 * added the AGP base itself, because dev->agp->base previously
275 * only got set during AGP enable. So, only add the base
276 * address if the map's offset isn't already within the
277 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100278 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000279 if (map->offset < dev->agp->base ||
280 map->offset > dev->agp->base +
281 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
282 map->offset += dev->agp->base;
283 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100284 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
285
286 /* This assumes the DRM is in total control of AGP space.
287 * It's not always the case as AGP can be in the control
288 * of user space (i.e. i810 driver). So this loop will get
289 * skipped and we double check that dev->agp->memory is
290 * actually set as well as being invalid before EPERM'ing
291 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000292 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100293 if ((map->offset >= entry->bound) &&
294 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
295 valid = 1;
296 break;
297 }
298 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000299 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700300 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100301 return -EPERM;
302 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100303 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
304 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100307 }
Pekka Paalanen812c3692009-09-17 22:59:54 +0300308 case _DRM_GEM:
309 DRM_ERROR("tried to addmap GEM object\n");
310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 case _DRM_SCATTER_GATHER:
312 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700313 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -EINVAL;
315 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000316 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000319 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000320 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000321 * casting it down to 32 bits is no problem, but we
322 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800323 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000324 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700325 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000326 return -ENOMEM;
327 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000328 map->handle = dmah->vaddr;
329 map->offset = (unsigned long)dmah->busaddr;
330 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700333 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EINVAL;
335 }
336
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400337 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700339 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100340 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700341 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return -EINVAL;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 list->map = map;
345
Dave Airlie30e2fb12006-02-02 19:37:46 +1100346 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000347 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000348
Dave Airlied1f2b552005-08-05 22:11:22 +1000349 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100350 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000351 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
352 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800353 ret = drm_map_handle(dev, &list->hash, user_token, 0,
354 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000355 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700356 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100357 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700358 kfree(map);
359 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000360 mutex_unlock(&dev->struct_mutex);
361 return ret;
362 }
363
Thomas Hellstrom15450852007-02-08 16:14:05 +1100364 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100365 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000367 if (!(map->flags & _DRM_DRIVER))
368 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000369 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000370 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100371 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000372
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100373int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000374 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100375 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000376{
Dave Airlie55910512007-07-11 16:53:40 +1000377 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000378 int rc;
379
380 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
381 if (!rc)
382 *map_ptr = list->map;
383 return rc;
384}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385
Dave Airlie7ab98402005-07-10 16:56:52 +1000386EXPORT_SYMBOL(drm_addmap);
387
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100388/**
389 * Ioctl to specify a range of memory that is available for mapping by a
390 * non-root process.
391 *
392 * \param inode device inode.
393 * \param file_priv DRM file private.
394 * \param cmd command.
395 * \param arg pointer to a drm_map structure.
396 * \return zero on success or a negative value on error.
397 *
398 */
Eric Anholtc153f452007-09-03 12:06:45 +1000399int drm_addmap_ioctl(struct drm_device *dev, void *data,
400 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000401{
Eric Anholtc153f452007-09-03 12:06:45 +1000402 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000403 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000404 int err;
405
Dave Airlie7c1c2872008-11-28 14:22:24 +1000406 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100407 return -EPERM;
408
Eric Anholtc153f452007-09-03 12:06:45 +1000409 err = drm_addmap_core(dev, map->offset, map->size, map->type,
410 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000411
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000413 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000414
Dave Airlie67e1a012005-10-24 18:41:39 +1000415 /* 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 +1000416 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000417
418 /*
419 * It appears that there are no users of this value whatsoever --
420 * drmAddMap just discards it. Let's not encourage its use.
421 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
422 * it's not a real mtrr index anymore.)
423 */
424 map->mtrr = -1;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/**
430 * Remove a map private from list and deallocate resources if the mapping
431 * isn't in use.
432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * Searches the map on drm_device::maplist, removes it from the list, see if
434 * its being used, and free any associate resource (such as MTRR's) if it's not
435 * being on use.
436 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000437 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100439int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Dave Airlie55910512007-07-11 16:53:40 +1000441 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000442 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000443 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000444 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dave Airlie836cf042005-07-10 19:27:04 +1000446 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000447 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000448 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000449 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000450 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100451 drm_ht_remove_key(&dev->map_hash,
452 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700453 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000454 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Dave Airlie836cf042005-07-10 19:27:04 +1000458
Dave Airliebd1b3312007-05-26 05:01:51 +1000459 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000460 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000461
462 switch (map->type) {
463 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100464 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000465 /* FALLTHROUGH */
466 case _DRM_FRAME_BUFFER:
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000467 if (drm_core_has_MTRR(dev))
468 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000469 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 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700493 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000494
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 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700584 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 entry->seg_count = 0;
587 }
588
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 if (entry->buf_count) {
590 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700591 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700593 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 entry->buf_count = 0;
596 }
597}
598
599#if __OS_HAS_AGP
600/**
Dave Airlied59431b2005-07-10 15:00:06 +1000601 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000603 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000604 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * After some sanity checks creates a drm_buf structure for each buffer and
608 * reallocates the buffer list of the same size order to accommodate the new
609 * buffers.
610 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000611int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Dave Airliecdd55a22007-07-11 16:32:08 +1000613 struct drm_device_dma *dma = dev->dma;
614 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000615 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000616 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 unsigned long offset;
618 unsigned long agp_offset;
619 int count;
620 int order;
621 int size;
622 int alignment;
623 int page_order;
624 int total;
625 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100626 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000627 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 if (!dma)
630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Dave Airlied59431b2005-07-10 15:00:06 +1000632 count = request->count;
633 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 size = 1 << order;
635
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 alignment = (request->flags & _DRM_PAGE_ALIGN)
637 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
639 total = PAGE_SIZE << page_order;
640
641 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000642 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 DRM_DEBUG("count: %d\n", count);
645 DRM_DEBUG("order: %d\n", order);
646 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100647 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 DRM_DEBUG("alignment: %d\n", alignment);
649 DRM_DEBUG("page_order: %d\n", page_order);
650 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
653 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dave Airlie54ba2f72007-02-10 12:07:47 +1100655 /* Make sure buffers are located in AGP memory that we own */
656 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000657 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100658 if ((agp_offset >= agp_entry->bound) &&
659 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
660 valid = 1;
661 break;
662 }
663 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000664 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100665 DRM_DEBUG("zone invalid\n");
666 return -EINVAL;
667 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668 spin_lock(&dev->count_lock);
669 if (dev->buf_use) {
670 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -EBUSY;
672 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 atomic_inc(&dev->buf_alloc);
674 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dave Airlie30e2fb12006-02-02 19:37:46 +1100676 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100679 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 atomic_dec(&dev->buf_alloc);
681 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100685 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EINVAL;
688 }
689
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400690 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100692 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -ENOMEM;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 entry->buf_size = size;
698 entry->page_order = page_order;
699
700 offset = 0;
701
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702 while (entry->buf_count < count) {
703 buf = &entry->buflist[entry->buf_count];
704 buf->idx = dma->buf_count + entry->buf_count;
705 buf->total = alignment;
706 buf->order = order;
707 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 buf->bus_address = agp_offset + offset;
711 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 buf->waiting = 0;
714 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000715 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400718 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* Set count correctly so we free the proper amount. */
721 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100723 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -ENOMEM;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 offset += alignment;
731 entry->buf_count++;
732 byte_count += PAGE_SIZE << page_order;
733 }
734
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Eric Anholt9a298b22009-03-24 12:23:04 -0700737 temp_buflist = krealloc(dma->buflist,
738 (dma->buf_count + entry->buf_count) *
739 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100743 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -ENOMEM;
746 }
747 dma->buflist = temp_buflist;
748
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000749 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
751 }
752
753 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100754 dma->seg_count += entry->seg_count;
755 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dma->byte_count += byte_count;
757
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
759 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Dave Airlie30e2fb12006-02-02 19:37:46 +1100761 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Dave Airlied59431b2005-07-10 15:00:06 +1000763 request->count = entry->buf_count;
764 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 dma->flags = _DRM_DMA_USE_AGP;
767
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
770}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771EXPORT_SYMBOL(drm_addbufs_agp);
772#endif /* __OS_HAS_AGP */
773
Dave Airlie84b1fd12007-07-11 15:53:27 +1000774int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Dave Airliecdd55a22007-07-11 16:32:08 +1000776 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int count;
778 int order;
779 int size;
780 int total;
781 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000782 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100783 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000784 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int alignment;
786 unsigned long offset;
787 int i;
788 int byte_count;
789 int page_count;
790 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000791 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
794 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100795
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 if (!dma)
797 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Dave Airlied985c102006-01-02 21:32:48 +1100799 if (!capable(CAP_SYS_ADMIN))
800 return -EPERM;
801
Dave Airlied59431b2005-07-10 15:00:06 +1000802 count = request->count;
803 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 size = 1 << order;
805
Daniel Vettera344a7e2011-10-26 00:54:41 +0200806 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
807 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
810 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Dave Airlied59431b2005-07-10 15:00:06 +1000812 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
815 total = PAGE_SIZE << page_order;
816
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 spin_lock(&dev->count_lock);
818 if (dev->buf_use) {
819 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -EBUSY;
821 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 atomic_inc(&dev->buf_alloc);
823 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Dave Airlie30e2fb12006-02-02 19:37:46 +1100825 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100828 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -ENOMEM; /* May only call once for each order */
831 }
832
833 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100834 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -EINVAL;
837 }
838
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400839 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100841 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -ENOMEM;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400846 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700848 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100849 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -ENOMEM;
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* Keep the original pagelist until we know all the allocations
855 * have succeeded
856 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700857 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
858 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700860 kfree(entry->buflist);
861 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100862 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -ENOMEM;
865 }
866 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
868 DRM_DEBUG("pagelist: %d entries\n",
869 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 entry->page_order = page_order;
873 byte_count = 0;
874 page_count = 0;
875
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000877
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800878 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000879
Dave Airlieddf19b92006-03-19 18:56:12 +1100880 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Set count correctly so we free the proper amount. */
882 entry->buf_count = count;
883 entry->seg_count = count;
884 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700885 kfree(temp_pagelist);
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 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100890 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 for (i = 0; i < (1 << page_order); i++) {
892 DRM_DEBUG("page %d @ 0x%08lx\n",
893 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100894 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100896 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 for (offset = 0;
899 offset + size <= total && entry->buf_count < count;
900 offset += alignment, ++entry->buf_count) {
901 buf = &entry->buflist[entry->buf_count];
902 buf->idx = dma->buf_count + entry->buf_count;
903 buf->total = alignment;
904 buf->order = order;
905 buf->used = 0;
906 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100907 buf->address = (void *)(dmah->vaddr + offset);
908 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 buf->waiting = 0;
911 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000912 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400915 buf->dev_private = kzalloc(buf->dev_priv_size,
916 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* Set count correctly so we free the proper amount. */
919 entry->buf_count = count;
920 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700922 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100923 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -ENOMEM;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 DRM_DEBUG("buffer %d @ %p\n",
929 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 byte_count += PAGE_SIZE << page_order;
932 }
933
Eric Anholt9a298b22009-03-24 12:23:04 -0700934 temp_buflist = krealloc(dma->buflist,
935 (dma->buf_count + entry->buf_count) *
936 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!temp_buflist) {
938 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700940 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100941 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -ENOMEM;
944 }
945 dma->buflist = temp_buflist;
946
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
949 }
950
Thomas Weber88393162010-03-16 11:47:56 +0100951 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * with the new one.
953 */
954 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700955 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 dma->pagelist = temp_pagelist;
958
959 dma->buf_count += entry->buf_count;
960 dma->seg_count += entry->seg_count;
961 dma->page_count += entry->seg_count << page_order;
962 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
963
Dave Airlie30e2fb12006-02-02 19:37:46 +1100964 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Airlied59431b2005-07-10 15:00:06 +1000966 request->count = entry->buf_count;
967 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
George Sapountzis3417f332006-10-24 12:03:04 -0700969 if (request->flags & _DRM_PCI_BUFFER_RO)
970 dma->flags = _DRM_DMA_USE_PCI_RO;
971
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
974
975}
Dave Airlied84f76d2005-07-10 17:04:22 +1000976EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Dave Airlie84b1fd12007-07-11 15:53:27 +1000978static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Dave Airliecdd55a22007-07-11 16:32:08 +1000980 struct drm_device_dma *dma = dev->dma;
981 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000982 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 unsigned long offset;
984 unsigned long agp_offset;
985 int count;
986 int order;
987 int size;
988 int alignment;
989 int page_order;
990 int total;
991 int byte_count;
992 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000993 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 if (!drm_core_check_feature(dev, DRIVER_SG))
996 return -EINVAL;
997
998 if (!dma)
999 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Dave Airlied985c102006-01-02 21:32:48 +11001001 if (!capable(CAP_SYS_ADMIN))
1002 return -EPERM;
1003
Dave Airlied59431b2005-07-10 15:00:06 +10001004 count = request->count;
1005 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 size = 1 << order;
1007
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 alignment = (request->flags & _DRM_PAGE_ALIGN)
1009 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1011 total = PAGE_SIZE << page_order;
1012
1013 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001014 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 DRM_DEBUG("count: %d\n", count);
1017 DRM_DEBUG("order: %d\n", order);
1018 DRM_DEBUG("size: %d\n", size);
1019 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1020 DRM_DEBUG("alignment: %d\n", alignment);
1021 DRM_DEBUG("page_order: %d\n", page_order);
1022 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1025 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 spin_lock(&dev->count_lock);
1028 if (dev->buf_use) {
1029 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -EBUSY;
1031 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 atomic_inc(&dev->buf_alloc);
1033 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Dave Airlie30e2fb12006-02-02 19:37:46 +11001035 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001038 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 atomic_dec(&dev->buf_alloc);
1040 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001044 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return -EINVAL;
1047 }
1048
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001049 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001050 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001052 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -ENOMEM;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 entry->buf_size = size;
1058 entry->page_order = page_order;
1059
1060 offset = 0;
1061
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 while (entry->buf_count < count) {
1063 buf = &entry->buflist[entry->buf_count];
1064 buf->idx = dma->buf_count + entry->buf_count;
1065 buf->total = alignment;
1066 buf->order = order;
1067 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001072 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 buf->waiting = 0;
1075 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001076 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001079 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /* Set count correctly so we free the proper amount. */
1082 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 drm_cleanup_buf_error(dev, entry);
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 -ENOMEM;
1087 }
1088
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 offset += alignment;
1092 entry->buf_count++;
1093 byte_count += PAGE_SIZE << page_order;
1094 }
1095
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Eric Anholt9a298b22009-03-24 12:23:04 -07001098 temp_buflist = krealloc(dma->buflist,
1099 (dma->buf_count + entry->buf_count) *
1100 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001104 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -ENOMEM;
1107 }
1108 dma->buflist = temp_buflist;
1109
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1112 }
1113
1114 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001115 dma->seg_count += entry->seg_count;
1116 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 dma->byte_count += byte_count;
1118
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001119 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1120 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dave Airlie30e2fb12006-02-02 19:37:46 +11001122 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Dave Airlied59431b2005-07-10 15:00:06 +10001124 request->count = entry->buf_count;
1125 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 dma->flags = _DRM_DMA_USE_SG;
1128
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return 0;
1131}
1132
Dave Airlie84b1fd12007-07-11 15:53:27 +10001133static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001134{
Dave Airliecdd55a22007-07-11 16:32:08 +10001135 struct drm_device_dma *dma = dev->dma;
1136 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001137 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001138 unsigned long offset;
1139 unsigned long agp_offset;
1140 int count;
1141 int order;
1142 int size;
1143 int alignment;
1144 int page_order;
1145 int total;
1146 int byte_count;
1147 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001148 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001149
1150 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1151 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001152
Dave Airlieb84397d62005-07-10 14:46:12 +10001153 if (!dma)
1154 return -EINVAL;
1155
Dave Airlied985c102006-01-02 21:32:48 +11001156 if (!capable(CAP_SYS_ADMIN))
1157 return -EPERM;
1158
Dave Airlied59431b2005-07-10 15:00:06 +10001159 count = request->count;
1160 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001161 size = 1 << order;
1162
Dave Airlied59431b2005-07-10 15:00:06 +10001163 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001164 ? PAGE_ALIGN(size) : size;
1165 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1166 total = PAGE_SIZE << page_order;
1167
1168 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001169 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001170
1171 DRM_DEBUG("count: %d\n", count);
1172 DRM_DEBUG("order: %d\n", order);
1173 DRM_DEBUG("size: %d\n", size);
1174 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1175 DRM_DEBUG("alignment: %d\n", alignment);
1176 DRM_DEBUG("page_order: %d\n", page_order);
1177 DRM_DEBUG("total: %d\n", total);
1178
1179 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1180 return -EINVAL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001181
1182 spin_lock(&dev->count_lock);
1183 if (dev->buf_use) {
1184 spin_unlock(&dev->count_lock);
1185 return -EBUSY;
1186 }
1187 atomic_inc(&dev->buf_alloc);
1188 spin_unlock(&dev->count_lock);
1189
Dave Airlie30e2fb12006-02-02 19:37:46 +11001190 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001191 entry = &dma->bufs[order];
1192 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001193 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001194 atomic_dec(&dev->buf_alloc);
1195 return -ENOMEM; /* May only call once for each order */
1196 }
1197
1198 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001199 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001200 atomic_dec(&dev->buf_alloc);
1201 return -EINVAL;
1202 }
1203
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001204 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001205 GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001206 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001207 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001208 atomic_dec(&dev->buf_alloc);
1209 return -ENOMEM;
1210 }
Dave Airlieb84397d62005-07-10 14:46:12 +10001211
1212 entry->buf_size = size;
1213 entry->page_order = page_order;
1214
1215 offset = 0;
1216
1217 while (entry->buf_count < count) {
1218 buf = &entry->buflist[entry->buf_count];
1219 buf->idx = dma->buf_count + entry->buf_count;
1220 buf->total = alignment;
1221 buf->order = order;
1222 buf->used = 0;
1223
1224 buf->offset = (dma->byte_count + offset);
1225 buf->bus_address = agp_offset + offset;
1226 buf->address = (void *)(agp_offset + offset);
1227 buf->next = NULL;
1228 buf->waiting = 0;
1229 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001230 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001231
1232 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001233 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001234 if (!buf->dev_private) {
1235 /* Set count correctly so we free the proper amount. */
1236 entry->buf_count = count;
1237 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001238 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001239 atomic_dec(&dev->buf_alloc);
1240 return -ENOMEM;
1241 }
Dave Airlieb84397d62005-07-10 14:46:12 +10001242
1243 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1244
1245 offset += alignment;
1246 entry->buf_count++;
1247 byte_count += PAGE_SIZE << page_order;
1248 }
1249
1250 DRM_DEBUG("byte_count: %d\n", byte_count);
1251
Eric Anholt9a298b22009-03-24 12:23:04 -07001252 temp_buflist = krealloc(dma->buflist,
1253 (dma->buf_count + entry->buf_count) *
1254 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001255 if (!temp_buflist) {
1256 /* Free the entry because it isn't valid */
1257 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001258 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001259 atomic_dec(&dev->buf_alloc);
1260 return -ENOMEM;
1261 }
1262 dma->buflist = temp_buflist;
1263
1264 for (i = 0; i < entry->buf_count; i++) {
1265 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1266 }
1267
1268 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001269 dma->seg_count += entry->seg_count;
1270 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001271 dma->byte_count += byte_count;
1272
1273 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1274 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1275
Dave Airlie30e2fb12006-02-02 19:37:46 +11001276 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001277
Dave Airlied59431b2005-07-10 15:00:06 +10001278 request->count = entry->buf_count;
1279 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001280
1281 dma->flags = _DRM_DMA_USE_FB;
1282
1283 atomic_dec(&dev->buf_alloc);
1284 return 0;
1285}
Dave Airlied985c102006-01-02 21:32:48 +11001286
Dave Airlieb84397d62005-07-10 14:46:12 +10001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/**
1289 * Add buffers for DMA transfers (ioctl).
1290 *
1291 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001292 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001294 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * \return zero on success or a negative number on failure.
1296 *
1297 * According with the memory type specified in drm_buf_desc::flags and the
1298 * build options, it dispatches the call either to addbufs_agp(),
1299 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1300 * PCI memory respectively.
1301 */
Eric Anholtc153f452007-09-03 12:06:45 +10001302int drm_addbufs(struct drm_device *dev, void *data,
1303 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Eric Anholtc153f452007-09-03 12:06:45 +10001305 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001306 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1309 return -EINVAL;
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001312 if (request->flags & _DRM_AGP_BUFFER)
1313 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 else
1315#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001316 if (request->flags & _DRM_SG_BUFFER)
1317 ret = drm_addbufs_sg(dev, request);
1318 else if (request->flags & _DRM_FB_BUFFER)
1319 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 else
Eric Anholtc153f452007-09-03 12:06:45 +10001321 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001322
Dave Airlied59431b2005-07-10 15:00:06 +10001323 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/**
1327 * Get information about the buffer mappings.
1328 *
1329 * This was originally mean for debugging purposes, or by a sophisticated
1330 * client library to determine how best to use the available buffers (e.g.,
1331 * large buffers can be used for image transfer).
1332 *
1333 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001334 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * \param cmd command.
1336 * \param arg pointer to a drm_buf_info structure.
1337 * \return zero on success or a negative number on failure.
1338 *
1339 * Increments drm_device::buf_use while holding the drm_device::count_lock
1340 * lock, preventing of allocating more buffers after this call. Information
1341 * about each requested buffer is then copied into user space.
1342 */
Eric Anholtc153f452007-09-03 12:06:45 +10001343int drm_infobufs(struct drm_device *dev, void *data,
1344 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Dave Airliecdd55a22007-07-11 16:32:08 +10001346 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001347 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int i;
1349 int count;
1350
1351 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1352 return -EINVAL;
1353
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354 if (!dma)
1355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001357 spin_lock(&dev->count_lock);
1358 if (atomic_read(&dev->buf_alloc)) {
1359 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EBUSY;
1361 }
1362 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001363 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1366 if (dma->bufs[i].buf_count)
1367 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001370 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Eric Anholtc153f452007-09-03 12:06:45 +10001372 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001373 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1374 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001375 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001376 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001377 struct drm_buf_entry *from = &dma->bufs[i];
1378 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001379 if (copy_to_user(&to->count,
1380 &from->buf_count,
1381 sizeof(from->buf_count)) ||
1382 copy_to_user(&to->size,
1383 &from->buf_size,
1384 sizeof(from->buf_size)) ||
1385 copy_to_user(&to->low_mark,
1386 &list->low_mark,
1387 sizeof(list->low_mark)) ||
1388 copy_to_user(&to->high_mark,
1389 &list->high_mark,
1390 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -EFAULT;
1392
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001393 DRM_DEBUG("%d %d %d %d %d\n",
1394 i,
1395 dma->bufs[i].buf_count,
1396 dma->bufs[i].buf_size,
1397 dma->bufs[i].freelist.low_mark,
1398 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ++count;
1400 }
1401 }
1402 }
Eric Anholtc153f452007-09-03 12:06:45 +10001403 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return 0;
1406}
1407
1408/**
1409 * Specifies a low and high water mark for buffer allocation
1410 *
1411 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001412 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * \param cmd command.
1414 * \param arg a pointer to a drm_buf_desc structure.
1415 * \return zero on success or a negative number on failure.
1416 *
1417 * Verifies that the size order is bounded between the admissible orders and
1418 * updates the respective drm_device_dma::bufs entry low and high water mark.
1419 *
1420 * \note This ioctl is deprecated and mostly never used.
1421 */
Eric Anholtc153f452007-09-03 12:06:45 +10001422int drm_markbufs(struct drm_device *dev, void *data,
1423 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Dave Airliecdd55a22007-07-11 16:32:08 +10001425 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001426 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001428 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1431 return -EINVAL;
1432
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 if (!dma)
1434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001437 request->size, request->low_mark, request->high_mark);
1438 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1440 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 entry = &dma->bufs[order];
1442
Eric Anholtc153f452007-09-03 12:06:45 +10001443 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001445 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EINVAL;
1447
Eric Anholtc153f452007-09-03 12:06:45 +10001448 entry->freelist.low_mark = request->low_mark;
1449 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 return 0;
1452}
1453
1454/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 *
1457 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001458 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * \param cmd command.
1460 * \param arg pointer to a drm_buf_free structure.
1461 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001462 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * Calls free_buffer() for each used buffer.
1464 * This function is primarily used for debugging.
1465 */
Eric Anholtc153f452007-09-03 12:06:45 +10001466int drm_freebufs(struct drm_device *dev, void *data,
1467 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Dave Airliecdd55a22007-07-11 16:32:08 +10001469 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001470 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 int i;
1472 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001473 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1476 return -EINVAL;
1477
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001478 if (!dma)
1479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Eric Anholtc153f452007-09-03 12:06:45 +10001481 DRM_DEBUG("%d\n", request->count);
1482 for (i = 0; i < request->count; i++) {
1483 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001485 if (idx < 0 || idx >= dma->buf_count) {
1486 DRM_ERROR("Index %d (of %d max)\n",
1487 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return -EINVAL;
1489 }
1490 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001491 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001492 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001493 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EINVAL;
1495 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001496 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
1499 return 0;
1500}
1501
1502/**
1503 * Maps all of the DMA buffers into client-virtual space (ioctl).
1504 *
1505 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001506 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * \param cmd command.
1508 * \param arg pointer to a drm_buf_map structure.
1509 * \return zero on success or a negative number on failure.
1510 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001511 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1512 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001513 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1514 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
Eric Anholtc153f452007-09-03 12:06:45 +10001516int drm_mapbufs(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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int retcode = 0;
1521 const int zero = 0;
1522 unsigned long virtual;
1523 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001524 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 int i;
1526
1527 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1528 return -EINVAL;
1529
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001530 if (!dma)
1531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 spin_lock(&dev->count_lock);
1534 if (atomic_read(&dev->buf_alloc)) {
1535 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return -EBUSY;
1537 }
1538 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001539 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Eric Anholtc153f452007-09-03 12:06:45 +10001541 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001542 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001543 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001544 && (dma->flags & _DRM_DMA_USE_SG))
1545 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1546 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001547 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001548 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001550 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 retcode = -EINVAL;
1552 goto done;
1553 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001554 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001555 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001556 MAP_SHARED,
1557 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001559 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 PROT_READ | PROT_WRITE,
1561 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Real error */
1565 retcode = (signed long)virtual;
1566 goto done;
1567 }
Eric Anholtc153f452007-09-03 12:06:45 +10001568 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001570 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001571 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001572 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001573 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 retcode = -EFAULT;
1575 goto done;
1576 }
Eric Anholtc153f452007-09-03 12:06:45 +10001577 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001579 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 retcode = -EFAULT;
1581 goto done;
1582 }
Eric Anholtc153f452007-09-03 12:06:45 +10001583 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 retcode = -EFAULT;
1586 goto done;
1587 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001588 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001589 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001590 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 retcode = -EFAULT;
1592 goto done;
1593 }
1594 }
1595 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001597 request->count = dma->buf_count;
1598 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 return retcode;
1601}
1602
Dave Airlie836cf042005-07-10 19:27:04 +10001603/**
1604 * Compute size order. Returns the exponent of the smaller power of two which
1605 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001606 *
Dave Airlie836cf042005-07-10 19:27:04 +10001607 * \param size size.
1608 * \return order.
1609 *
1610 * \todo Can be made faster.
1611 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001612int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001613{
1614 int order;
1615 unsigned long tmp;
1616
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001617 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001618
1619 if (size & (size - 1))
1620 ++order;
1621
1622 return order;
1623}
1624EXPORT_SYMBOL(drm_order);