blob: 471e051d295e383b61ebb2a2a19c0e0d67d8827a [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
Daniel Vetter28185642013-08-08 15:41:27 +0200210 if (map->type == _DRM_FRAME_BUFFER ||
211 (map->flags & _DRM_WRITE_COMBINING)) {
212 map->mtrr =
213 arch_phys_wc_add(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Scott Thompson0769d392007-08-25 18:17:49 +1000215 if (map->type == _DRM_REGISTERS) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000216 if (map->flags & _DRM_WRITE_COMBINING)
217 map->handle = ioremap_wc(map->offset,
218 map->size);
219 else
220 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000221 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700222 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000223 return -ENOMEM;
224 }
225 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100229 list = drm_find_matching_map(dev, map);
230 if (list != NULL) {
231 if(list->map->size != map->size) {
232 DRM_DEBUG("Matching maps of type %d with "
233 "mismatched sizes, (%ld vs %ld)\n",
234 map->type, map->size, list->map->size);
235 list->map->size = map->size;
236 }
237
Eric Anholt9a298b22009-03-24 12:23:04 -0700238 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100239 *maplist = list;
240 return 0;
241 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100242 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 DRM_DEBUG("%lu %d %p\n",
Daniel Vetter04420c92013-07-10 14:11:57 +0200244 map->size, order_base_2(map->size), map->handle);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700246 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -ENOMEM;
248 }
249 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000252 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700254 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EBUSY;
256 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000257 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100260 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000261 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100262 int valid = 0;
263
264 if (!drm_core_has_AGP(dev)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700265 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100266 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100268#ifdef __alpha__
269 map->offset += dev->hose->mem_space->start;
270#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000271 /* In some cases (i810 driver), user space may have already
272 * added the AGP base itself, because dev->agp->base previously
273 * only got set during AGP enable. So, only add the base
274 * address if the map's offset isn't already within the
275 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100276 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000277 if (map->offset < dev->agp->base ||
278 map->offset > dev->agp->base +
279 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
280 map->offset += dev->agp->base;
281 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100282 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
283
284 /* This assumes the DRM is in total control of AGP space.
285 * It's not always the case as AGP can be in the control
286 * of user space (i.e. i810 driver). So this loop will get
287 * skipped and we double check that dev->agp->memory is
288 * actually set as well as being invalid before EPERM'ing
289 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000290 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100291 if ((map->offset >= entry->bound) &&
292 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
293 valid = 1;
294 break;
295 }
296 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000297 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700298 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100299 return -EPERM;
300 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100301 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
302 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100305 }
Pekka Paalanen812c3692009-09-17 22:59:54 +0300306 case _DRM_GEM:
307 DRM_ERROR("tried to addmap GEM object\n");
308 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case _DRM_SCATTER_GATHER:
310 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700311 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -EINVAL;
313 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000314 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000317 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000318 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000319 * casting it down to 32 bits is no problem, but we
320 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800321 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000322 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700323 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000324 return -ENOMEM;
325 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000326 map->handle = dmah->vaddr;
327 map->offset = (unsigned long)dmah->busaddr;
328 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700331 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -EINVAL;
333 }
334
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400335 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700337 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100338 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700339 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EINVAL;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 list->map = map;
343
Dave Airlie30e2fb12006-02-02 19:37:46 +1100344 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000345 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000346
Dave Airlied1f2b552005-08-05 22:11:22 +1000347 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100348 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000349 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
350 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800351 ret = drm_map_handle(dev, &list->hash, user_token, 0,
352 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000353 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700354 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100355 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700356 kfree(map);
357 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000358 mutex_unlock(&dev->struct_mutex);
359 return ret;
360 }
361
Thomas Hellstrom15450852007-02-08 16:14:05 +1100362 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100363 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000365 if (!(map->flags & _DRM_DRIVER))
366 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000367 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000368 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100369 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000370
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100371int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000372 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100373 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000374{
Dave Airlie55910512007-07-11 16:53:40 +1000375 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000376 int rc;
377
378 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
379 if (!rc)
380 *map_ptr = list->map;
381 return rc;
382}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383
Dave Airlie7ab98402005-07-10 16:56:52 +1000384EXPORT_SYMBOL(drm_addmap);
385
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100386/**
387 * Ioctl to specify a range of memory that is available for mapping by a
388 * non-root process.
389 *
390 * \param inode device inode.
391 * \param file_priv DRM file private.
392 * \param cmd command.
393 * \param arg pointer to a drm_map structure.
394 * \return zero on success or a negative value on error.
395 *
396 */
Eric Anholtc153f452007-09-03 12:06:45 +1000397int drm_addmap_ioctl(struct drm_device *dev, void *data,
398 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000399{
Eric Anholtc153f452007-09-03 12:06:45 +1000400 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000401 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000402 int err;
403
Dave Airlie7c1c2872008-11-28 14:22:24 +1000404 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100405 return -EPERM;
406
Eric Anholtc153f452007-09-03 12:06:45 +1000407 err = drm_addmap_core(dev, map->offset, map->size, map->type,
408 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000409
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000411 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000412
Dave Airlie67e1a012005-10-24 18:41:39 +1000413 /* 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 +1000414 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000415
416 /*
417 * It appears that there are no users of this value whatsoever --
418 * drmAddMap just discards it. Let's not encourage its use.
419 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
420 * it's not a real mtrr index anymore.)
421 */
422 map->mtrr = -1;
423
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);
Eric Anholt9a298b22009-03-24 12:23:04 -0700451 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000452 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:
Daniel Vetter28185642013-08-08 15:41:27 +0200465 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000466 break;
467 case _DRM_SHM:
468 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000469 if (master) {
470 if (dev->sigdata.lock == master->lock.hw_lock)
471 dev->sigdata.lock = NULL;
472 master->lock.hw_lock = NULL; /* SHM removed */
473 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100474 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000475 }
Dave Airlie836cf042005-07-10 19:27:04 +1000476 break;
477 case _DRM_AGP:
478 case _DRM_SCATTER_GATHER:
479 break;
480 case _DRM_CONSISTENT:
481 dmah.vaddr = map->handle;
482 dmah.busaddr = map->offset;
483 dmah.size = map->size;
484 __drm_pci_free(dev, &dmah);
485 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800486 case _DRM_GEM:
487 DRM_ERROR("tried to rmmap GEM object\n");
488 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000489 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700490 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
Dave Airlie4e74f362008-12-19 10:23:14 +1100494EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000495
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100496int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000497{
498 int ret;
499
Dave Airlie30e2fb12006-02-02 19:37:46 +1100500 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000501 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100502 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000503
504 return ret;
505}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000506EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000507
Dave Airlie836cf042005-07-10 19:27:04 +1000508/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
509 * the last close of the device, and this is necessary for cleanup when things
510 * exit uncleanly. Therefore, having userland manually remove mappings seems
511 * like a pointless exercise since they're going away anyway.
512 *
513 * One use case might be after addmap is allowed for normal users for SHM and
514 * gets used by drivers that the server doesn't need to care about. This seems
515 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100516 *
517 * \param inode device inode.
518 * \param file_priv DRM file private.
519 * \param cmd command.
520 * \param arg pointer to a struct drm_map structure.
521 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000522 */
Eric Anholtc153f452007-09-03 12:06:45 +1000523int drm_rmmap_ioctl(struct drm_device *dev, void *data,
524 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000525{
Eric Anholtc153f452007-09-03 12:06:45 +1000526 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100527 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000528 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000529 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000530
Dave Airlie30e2fb12006-02-02 19:37:46 +1100531 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000532 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000533 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000534 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000535 r_list->map->flags & _DRM_REMOVABLE) {
536 map = r_list->map;
537 break;
538 }
539 }
540
541 /* List has wrapped around to the head pointer, or its empty we didn't
542 * find anything.
543 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000544 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100545 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000546 return -EINVAL;
547 }
548
Dave Airlie836cf042005-07-10 19:27:04 +1000549 /* Register and framebuffer maps are permanent */
550 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100551 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000552 return 0;
553 }
554
555 ret = drm_rmmap_locked(dev, map);
556
Dave Airlie30e2fb12006-02-02 19:37:46 +1100557 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000558
559 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000560}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562/**
563 * Cleanup after an error on one of the addbufs() functions.
564 *
Dave Airlie836cf042005-07-10 19:27:04 +1000565 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * \param entry buffer entry where the error occurred.
567 *
568 * Frees any pages and buffers associated with the given entry.
569 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000570static void drm_cleanup_buf_error(struct drm_device * dev,
571 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 int i;
574
575 if (entry->seg_count) {
576 for (i = 0; i < entry->seg_count; i++) {
577 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100578 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700581 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 entry->seg_count = 0;
584 }
585
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 if (entry->buf_count) {
587 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700588 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700590 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 entry->buf_count = 0;
593 }
594}
595
596#if __OS_HAS_AGP
597/**
Dave Airlied59431b2005-07-10 15:00:06 +1000598 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000600 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000601 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000603 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * After some sanity checks creates a drm_buf structure for each buffer and
605 * reallocates the buffer list of the same size order to accommodate the new
606 * buffers.
607 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000608int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Dave Airliecdd55a22007-07-11 16:32:08 +1000610 struct drm_device_dma *dma = dev->dma;
611 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000612 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000613 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 unsigned long offset;
615 unsigned long agp_offset;
616 int count;
617 int order;
618 int size;
619 int alignment;
620 int page_order;
621 int total;
622 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100623 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000624 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000626 if (!dma)
627 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Dave Airlied59431b2005-07-10 15:00:06 +1000629 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200630 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 size = 1 << order;
632
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 alignment = (request->flags & _DRM_PAGE_ALIGN)
634 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
636 total = PAGE_SIZE << page_order;
637
638 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000639 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 DRM_DEBUG("count: %d\n", count);
642 DRM_DEBUG("order: %d\n", order);
643 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100644 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 DRM_DEBUG("alignment: %d\n", alignment);
646 DRM_DEBUG("page_order: %d\n", page_order);
647 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Airlie54ba2f72007-02-10 12:07:47 +1100652 /* Make sure buffers are located in AGP memory that we own */
653 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000654 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100655 if ((agp_offset >= agp_entry->bound) &&
656 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
657 valid = 1;
658 break;
659 }
660 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000661 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100662 DRM_DEBUG("zone invalid\n");
663 return -EINVAL;
664 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 spin_lock(&dev->count_lock);
666 if (dev->buf_use) {
667 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EBUSY;
669 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 atomic_inc(&dev->buf_alloc);
671 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Dave Airlie30e2fb12006-02-02 19:37:46 +1100673 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100676 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 atomic_dec(&dev->buf_alloc);
678 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100682 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -EINVAL;
685 }
686
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400687 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100689 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -ENOMEM;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 entry->buf_size = size;
695 entry->page_order = page_order;
696
697 offset = 0;
698
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 while (entry->buf_count < count) {
700 buf = &entry->buflist[entry->buf_count];
701 buf->idx = dma->buf_count + entry->buf_count;
702 buf->total = alignment;
703 buf->order = order;
704 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 buf->bus_address = agp_offset + offset;
708 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 buf->waiting = 0;
711 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000712 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400715 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Set count correctly so we free the proper amount. */
718 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100720 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -ENOMEM;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 offset += alignment;
728 entry->buf_count++;
729 byte_count += PAGE_SIZE << page_order;
730 }
731
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Eric Anholt9a298b22009-03-24 12:23:04 -0700734 temp_buflist = krealloc(dma->buflist,
735 (dma->buf_count + entry->buf_count) *
736 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100740 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -ENOMEM;
743 }
744 dma->buflist = temp_buflist;
745
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
748 }
749
750 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100751 dma->seg_count += entry->seg_count;
752 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 dma->byte_count += byte_count;
754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
756 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dave Airlie30e2fb12006-02-02 19:37:46 +1100758 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Airlied59431b2005-07-10 15:00:06 +1000760 request->count = entry->buf_count;
761 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 dma->flags = _DRM_DMA_USE_AGP;
764
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return 0;
767}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768EXPORT_SYMBOL(drm_addbufs_agp);
769#endif /* __OS_HAS_AGP */
770
Dave Airlie84b1fd12007-07-11 15:53:27 +1000771int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Dave Airliecdd55a22007-07-11 16:32:08 +1000773 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int count;
775 int order;
776 int size;
777 int total;
778 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000779 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100780 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000781 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int alignment;
783 unsigned long offset;
784 int i;
785 int byte_count;
786 int page_count;
787 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000788 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
791 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100792
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 if (!dma)
794 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Dave Airlied985c102006-01-02 21:32:48 +1100796 if (!capable(CAP_SYS_ADMIN))
797 return -EPERM;
798
Dave Airlied59431b2005-07-10 15:00:06 +1000799 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200800 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 size = 1 << order;
802
Daniel Vettera344a7e2011-10-26 00:54:41 +0200803 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
804 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
807 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Airlied59431b2005-07-10 15:00:06 +1000809 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
812 total = PAGE_SIZE << page_order;
813
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 spin_lock(&dev->count_lock);
815 if (dev->buf_use) {
816 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return -EBUSY;
818 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 atomic_inc(&dev->buf_alloc);
820 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dave Airlie30e2fb12006-02-02 19:37:46 +1100822 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100825 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -ENOMEM; /* May only call once for each order */
828 }
829
830 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100831 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return -EINVAL;
834 }
835
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400836 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100838 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -ENOMEM;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400843 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700845 kfree(entry->buflist);
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;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* Keep the original pagelist until we know all the allocations
852 * have succeeded
853 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700854 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
855 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700857 kfree(entry->buflist);
858 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100859 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -ENOMEM;
862 }
863 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
865 DRM_DEBUG("pagelist: %d entries\n",
866 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 entry->page_order = page_order;
870 byte_count = 0;
871 page_count = 0;
872
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000873 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000874
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800875 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000876
Dave Airlieddf19b92006-03-19 18:56:12 +1100877 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* Set count correctly so we free the proper amount. */
879 entry->buf_count = count;
880 entry->seg_count = count;
881 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700882 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100883 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -ENOMEM;
886 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100887 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 for (i = 0; i < (1 << page_order); i++) {
889 DRM_DEBUG("page %d @ 0x%08lx\n",
890 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100891 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100893 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 for (offset = 0;
896 offset + size <= total && entry->buf_count < count;
897 offset += alignment, ++entry->buf_count) {
898 buf = &entry->buflist[entry->buf_count];
899 buf->idx = dma->buf_count + entry->buf_count;
900 buf->total = alignment;
901 buf->order = order;
902 buf->used = 0;
903 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100904 buf->address = (void *)(dmah->vaddr + offset);
905 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 buf->waiting = 0;
908 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000909 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400912 buf->dev_private = kzalloc(buf->dev_priv_size,
913 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* Set count correctly so we free the proper amount. */
916 entry->buf_count = count;
917 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700919 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100920 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ENOMEM;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 DRM_DEBUG("buffer %d @ %p\n",
926 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 byte_count += PAGE_SIZE << page_order;
929 }
930
Eric Anholt9a298b22009-03-24 12:23:04 -0700931 temp_buflist = krealloc(dma->buflist,
932 (dma->buf_count + entry->buf_count) *
933 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!temp_buflist) {
935 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700937 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100938 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -ENOMEM;
941 }
942 dma->buflist = temp_buflist;
943
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
946 }
947
Thomas Weber88393162010-03-16 11:47:56 +0100948 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * with the new one.
950 */
951 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700952 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 dma->pagelist = temp_pagelist;
955
956 dma->buf_count += entry->buf_count;
957 dma->seg_count += entry->seg_count;
958 dma->page_count += entry->seg_count << page_order;
959 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
960
Dave Airlie30e2fb12006-02-02 19:37:46 +1100961 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Dave Airlied59431b2005-07-10 15:00:06 +1000963 request->count = entry->buf_count;
964 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
George Sapountzis3417f332006-10-24 12:03:04 -0700966 if (request->flags & _DRM_PCI_BUFFER_RO)
967 dma->flags = _DRM_DMA_USE_PCI_RO;
968
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000969 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return 0;
971
972}
Dave Airlied84f76d2005-07-10 17:04:22 +1000973EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Dave Airlie84b1fd12007-07-11 15:53:27 +1000975static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Dave Airliecdd55a22007-07-11 16:32:08 +1000977 struct drm_device_dma *dma = dev->dma;
978 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000979 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 unsigned long offset;
981 unsigned long agp_offset;
982 int count;
983 int order;
984 int size;
985 int alignment;
986 int page_order;
987 int total;
988 int byte_count;
989 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000990 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 if (!drm_core_check_feature(dev, DRIVER_SG))
993 return -EINVAL;
994
995 if (!dma)
996 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Dave Airlied985c102006-01-02 21:32:48 +1100998 if (!capable(CAP_SYS_ADMIN))
999 return -EPERM;
1000
Dave Airlied59431b2005-07-10 15:00:06 +10001001 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001002 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 size = 1 << order;
1004
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 alignment = (request->flags & _DRM_PAGE_ALIGN)
1006 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1008 total = PAGE_SIZE << page_order;
1009
1010 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001011 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 DRM_DEBUG("count: %d\n", count);
1014 DRM_DEBUG("order: %d\n", order);
1015 DRM_DEBUG("size: %d\n", size);
1016 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1017 DRM_DEBUG("alignment: %d\n", alignment);
1018 DRM_DEBUG("page_order: %d\n", page_order);
1019 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1022 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 spin_lock(&dev->count_lock);
1025 if (dev->buf_use) {
1026 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EBUSY;
1028 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 atomic_inc(&dev->buf_alloc);
1030 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Dave Airlie30e2fb12006-02-02 19:37:46 +11001032 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001035 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 atomic_dec(&dev->buf_alloc);
1037 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
1040 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001041 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -EINVAL;
1044 }
1045
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001046 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001047 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001049 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return -ENOMEM;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 entry->buf_size = size;
1055 entry->page_order = page_order;
1056
1057 offset = 0;
1058
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 while (entry->buf_count < count) {
1060 buf = &entry->buflist[entry->buf_count];
1061 buf->idx = dma->buf_count + entry->buf_count;
1062 buf->total = alignment;
1063 buf->order = order;
1064 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001069 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 buf->waiting = 0;
1072 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001073 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001076 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* Set count correctly so we free the proper amount. */
1079 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001081 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -ENOMEM;
1084 }
1085
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 offset += alignment;
1089 entry->buf_count++;
1090 byte_count += PAGE_SIZE << page_order;
1091 }
1092
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Eric Anholt9a298b22009-03-24 12:23:04 -07001095 temp_buflist = krealloc(dma->buflist,
1096 (dma->buf_count + entry->buf_count) *
1097 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001101 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -ENOMEM;
1104 }
1105 dma->buflist = temp_buflist;
1106
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001107 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1109 }
1110
1111 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001112 dma->seg_count += entry->seg_count;
1113 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 dma->byte_count += byte_count;
1115
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1117 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Dave Airlie30e2fb12006-02-02 19:37:46 +11001119 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Dave Airlied59431b2005-07-10 15:00:06 +10001121 request->count = entry->buf_count;
1122 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 dma->flags = _DRM_DMA_USE_SG;
1125
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 0;
1128}
1129
1130/**
1131 * Add buffers for DMA transfers (ioctl).
1132 *
1133 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001134 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001136 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * \return zero on success or a negative number on failure.
1138 *
1139 * According with the memory type specified in drm_buf_desc::flags and the
1140 * build options, it dispatches the call either to addbufs_agp(),
1141 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1142 * PCI memory respectively.
1143 */
Eric Anholtc153f452007-09-03 12:06:45 +10001144int drm_addbufs(struct drm_device *dev, void *data,
1145 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Eric Anholtc153f452007-09-03 12:06:45 +10001147 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001148 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001150 if (drm_core_check_feature(dev, DRIVER_MODESET))
1151 return -EINVAL;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1154 return -EINVAL;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001157 if (request->flags & _DRM_AGP_BUFFER)
1158 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 else
1160#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001161 if (request->flags & _DRM_SG_BUFFER)
1162 ret = drm_addbufs_sg(dev, request);
1163 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001164 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 else
Eric Anholtc153f452007-09-03 12:06:45 +10001166 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001167
Dave Airlied59431b2005-07-10 15:00:06 +10001168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/**
1172 * Get information about the buffer mappings.
1173 *
1174 * This was originally mean for debugging purposes, or by a sophisticated
1175 * client library to determine how best to use the available buffers (e.g.,
1176 * large buffers can be used for image transfer).
1177 *
1178 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001179 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * \param cmd command.
1181 * \param arg pointer to a drm_buf_info structure.
1182 * \return zero on success or a negative number on failure.
1183 *
1184 * Increments drm_device::buf_use while holding the drm_device::count_lock
1185 * lock, preventing of allocating more buffers after this call. Information
1186 * about each requested buffer is then copied into user space.
1187 */
Eric Anholtc153f452007-09-03 12:06:45 +10001188int drm_infobufs(struct drm_device *dev, void *data,
1189 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Dave Airliecdd55a22007-07-11 16:32:08 +10001191 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001192 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int i;
1194 int count;
1195
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001196 if (drm_core_check_feature(dev, DRIVER_MODESET))
1197 return -EINVAL;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1200 return -EINVAL;
1201
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001202 if (!dma)
1203 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001205 spin_lock(&dev->count_lock);
1206 if (atomic_read(&dev->buf_alloc)) {
1207 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EBUSY;
1209 }
1210 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001211 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001213 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1214 if (dma->bufs[i].buf_count)
1215 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001218 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Eric Anholtc153f452007-09-03 12:06:45 +10001220 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001221 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1222 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001223 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001224 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001225 struct drm_buf_entry *from = &dma->bufs[i];
1226 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001227 if (copy_to_user(&to->count,
1228 &from->buf_count,
1229 sizeof(from->buf_count)) ||
1230 copy_to_user(&to->size,
1231 &from->buf_size,
1232 sizeof(from->buf_size)) ||
1233 copy_to_user(&to->low_mark,
1234 &list->low_mark,
1235 sizeof(list->low_mark)) ||
1236 copy_to_user(&to->high_mark,
1237 &list->high_mark,
1238 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EFAULT;
1240
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001241 DRM_DEBUG("%d %d %d %d %d\n",
1242 i,
1243 dma->bufs[i].buf_count,
1244 dma->bufs[i].buf_size,
1245 dma->bufs[i].freelist.low_mark,
1246 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 ++count;
1248 }
1249 }
1250 }
Eric Anholtc153f452007-09-03 12:06:45 +10001251 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 return 0;
1254}
1255
1256/**
1257 * Specifies a low and high water mark for buffer allocation
1258 *
1259 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001260 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * \param cmd command.
1262 * \param arg a pointer to a drm_buf_desc structure.
1263 * \return zero on success or a negative number on failure.
1264 *
1265 * Verifies that the size order is bounded between the admissible orders and
1266 * updates the respective drm_device_dma::bufs entry low and high water mark.
1267 *
1268 * \note This ioctl is deprecated and mostly never used.
1269 */
Eric Anholtc153f452007-09-03 12:06:45 +10001270int drm_markbufs(struct drm_device *dev, void *data,
1271 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Dave Airliecdd55a22007-07-11 16:32:08 +10001273 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001274 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001276 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001278 if (drm_core_check_feature(dev, DRIVER_MODESET))
1279 return -EINVAL;
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1282 return -EINVAL;
1283
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001284 if (!dma)
1285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001287 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001288 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001289 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001290 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 entry = &dma->bufs[order];
1293
Eric Anholtc153f452007-09-03 12:06:45 +10001294 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001296 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EINVAL;
1298
Eric Anholtc153f452007-09-03 12:06:45 +10001299 entry->freelist.low_mark = request->low_mark;
1300 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 return 0;
1303}
1304
1305/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001306 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 *
1308 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001309 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * \param cmd command.
1311 * \param arg pointer to a drm_buf_free structure.
1312 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001313 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * Calls free_buffer() for each used buffer.
1315 * This function is primarily used for debugging.
1316 */
Eric Anholtc153f452007-09-03 12:06:45 +10001317int drm_freebufs(struct drm_device *dev, void *data,
1318 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Dave Airliecdd55a22007-07-11 16:32:08 +10001320 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001321 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int i;
1323 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001324 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001326 if (drm_core_check_feature(dev, DRIVER_MODESET))
1327 return -EINVAL;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1330 return -EINVAL;
1331
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001332 if (!dma)
1333 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Eric Anholtc153f452007-09-03 12:06:45 +10001335 DRM_DEBUG("%d\n", request->count);
1336 for (i = 0; i < request->count; i++) {
1337 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001339 if (idx < 0 || idx >= dma->buf_count) {
1340 DRM_ERROR("Index %d (of %d max)\n",
1341 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -EINVAL;
1343 }
1344 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001345 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001346 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001347 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -EINVAL;
1349 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001350 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
1353 return 0;
1354}
1355
1356/**
1357 * Maps all of the DMA buffers into client-virtual space (ioctl).
1358 *
1359 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001360 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * \param cmd command.
1362 * \param arg pointer to a drm_buf_map structure.
1363 * \return zero on success or a negative number on failure.
1364 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001365 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1366 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001367 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1368 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Eric Anholtc153f452007-09-03 12:06:45 +10001370int drm_mapbufs(struct drm_device *dev, void *data,
1371 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Dave Airliecdd55a22007-07-11 16:32:08 +10001373 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int retcode = 0;
1375 const int zero = 0;
1376 unsigned long virtual;
1377 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001378 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int i;
1380
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001381 if (drm_core_check_feature(dev, DRIVER_MODESET))
1382 return -EINVAL;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1385 return -EINVAL;
1386
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 if (!dma)
1388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 spin_lock(&dev->count_lock);
1391 if (atomic_read(&dev->buf_alloc)) {
1392 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EBUSY;
1394 }
1395 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Eric Anholtc153f452007-09-03 12:06:45 +10001398 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001399 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001400 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001401 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001402 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001403 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 retcode = -EINVAL;
1407 goto done;
1408 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001409 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001410 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001411 MAP_SHARED,
1412 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001414 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 PROT_READ | PROT_WRITE,
1416 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001418 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /* Real error */
1420 retcode = (signed long)virtual;
1421 goto done;
1422 }
Eric Anholtc153f452007-09-03 12:06:45 +10001423 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001425 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001426 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001427 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001428 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 retcode = -EFAULT;
1430 goto done;
1431 }
Eric Anholtc153f452007-09-03 12:06:45 +10001432 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001434 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 retcode = -EFAULT;
1436 goto done;
1437 }
Eric Anholtc153f452007-09-03 12:06:45 +10001438 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 retcode = -EFAULT;
1441 goto done;
1442 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001444 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001445 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 retcode = -EFAULT;
1447 goto done;
1448 }
1449 }
1450 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001452 request->count = dma->buf_count;
1453 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 return retcode;
1456}
1457
Daniel Vetter6eb92782013-08-08 15:41:29 +02001458int drm_dma_ioctl(struct drm_device *dev, void *data,
1459 struct drm_file *file_priv)
1460{
1461 if (drm_core_check_feature(dev, DRIVER_MODESET))
1462 return -EINVAL;
1463
1464 if (dev->driver->dma_ioctl)
1465 return dev->driver->dma_ioctl(dev, data, file_priv);
1466 else
1467 return -EINVAL;
1468}
1469
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001470struct drm_local_map *drm_getsarea(struct drm_device *dev)
1471{
1472 struct drm_map_list *entry;
1473
1474 list_for_each_entry(entry, &dev->maplist, head) {
1475 if (entry->map && entry->map->type == _DRM_SHM &&
1476 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1477 return entry->map;
1478 }
1479 }
1480 return NULL;
1481}
1482EXPORT_SYMBOL(drm_getsarea);