blob: f1a204d253cce0bfb2616cd4a9b0ebb400e5718c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Herrmann9fc5cde2014-08-29 12:12:28 +02002 * Legacy: Generic DRM Buffer Management
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
David Herrmann9fc5cde2014-08-29 12:12:28 +02008 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author: Gareth Hughes <gareth@valinux.com>
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
20 * Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
David Millerf1a2a9b2009-02-18 15:41:02 -080033#include <linux/log2.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
David Millerf1a2a9b2009-02-18 15:41:02 -080035#include <asm/shmparam.h>
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
David Herrmann9fc5cde2014-08-29 12:12:28 +020037#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Dave Airlie55910512007-07-11 16:53:40 +100039static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110040 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100041{
Dave Airlie55910512007-07-11 16:53:40 +100042 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100043 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110044 /*
45 * Because the kernel-userspace ABI is fixed at a 32-bit offset
Tormod Volden66aa6962011-05-30 19:45:43 +000046 * while PCI resources may live above that, we only compare the
47 * lower 32 bits of the map offset for maps of type
48 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
49 * It is assumed that if a driver have more than one resource
50 * of each type, the lower 32 bits are different.
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110051 */
52 if (!entry->map ||
53 map->type != entry->map->type ||
54 entry->master != dev->primary->master)
55 continue;
56 switch (map->type) {
57 case _DRM_SHM:
58 if (map->flags != _DRM_CONTAINS_LOCK)
59 break;
Tormod Volden66aa6962011-05-30 19:45:43 +000060 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110061 case _DRM_REGISTERS:
62 case _DRM_FRAME_BUFFER:
Tormod Volden66aa6962011-05-30 19:45:43 +000063 if ((entry->map->offset & 0xffffffff) ==
64 (map->offset & 0xffffffff))
65 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110066 default: /* Make gcc happy */
67 ;
Dave Airlie836cf042005-07-10 19:27:04 +100068 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110069 if (entry->map->offset == map->offset)
70 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100071 }
72
73 return NULL;
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Dave Airliee0be4282007-07-12 10:26:44 +100076static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080077 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100078{
David Millerf1a2a9b2009-02-18 15:41:02 -080079 int use_hashed_handle, shift;
80 unsigned long add;
81
Dave Airliec2604ce2006-08-12 16:03:26 +100082#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100083 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
84#elif (BITS_PER_LONG == 32)
85 use_hashed_handle = hashed_handle;
86#else
87#error Unsupported long size. Neither 64 nor 32 bits.
88#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100089
Thomas Hellstrome08870c2006-09-22 04:18:37 +100090 if (!use_hashed_handle) {
91 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110092 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100093 ret = drm_ht_insert_item(&dev->map_hash, hash);
94 if (ret != -EINVAL)
95 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +100096 }
David Millerf1a2a9b2009-02-18 15:41:02 -080097
98 shift = 0;
99 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
100 if (shm && (SHMLBA > PAGE_SIZE)) {
101 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
102
103 /* For shared memory, we have to preserve the SHMLBA
104 * bits of the eventual vma->vm_pgoff value during
105 * mmap(). Otherwise we run into cache aliasing problems
106 * on some platforms. On these platforms, the pgoff of
107 * a mmap() request is used to pick a suitable virtual
108 * address for the mmap() region such that it will not
109 * cause cache aliasing problems.
110 *
111 * Therefore, make sure the SHMLBA relevant bits of the
112 * hash value we use are equal to those in the original
113 * kernel virtual address.
114 */
115 shift = bits;
116 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
117 }
118
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000119 return drm_ht_just_insert_please(&dev->map_hash, hash,
120 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800121 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000122}
Dave Airlie9a186642005-06-23 21:29:18 +1000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100125 * Core function to create a range of memory available for mapping by a
126 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * Adjusts the memory offset to its absolute value according to the mapping
129 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
130 * applicable and if supported by the kernel.
131 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100132static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000133 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000134 enum drm_map_flags flags,
135 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100137 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000138 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000139 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000140 unsigned long user_token;
141 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Eric Anholt9a298b22009-03-24 12:23:04 -0700143 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000144 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -ENOMEM;
146
Dave Airlie7ab98402005-07-10 16:56:52 +1000147 map->offset = offset;
148 map->size = size;
149 map->flags = flags;
150 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* Only allow shared memory to be removable since we only keep enough
153 * book keeping information about shared memory to allow for removal
154 * when processes fork.
155 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700157 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EINVAL;
159 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100160 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
161 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000162
163 /* page-align _DRM_SHM maps. They are allocated here so there is no security
164 * hole created by that and it works around various broken drivers that use
165 * a non-aligned quantity to map the SAREA. --BenH
166 */
167 if (map->type == _DRM_SHM)
168 map->size = PAGE_ALIGN(map->size);
169
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100170 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700171 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -EINVAL;
173 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 map->handle = NULL;
176
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 case _DRM_REGISTERS:
179 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600180#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100181 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700183 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EINVAL;
185 }
186#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000187 /* Some drivers preinitialize some maps, without the X Server
188 * needing to be aware of it. Therefore, we just return success
189 * when the server tries to create a duplicate map.
190 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000191 list = drm_find_matching_map(dev, map);
192 if (list != NULL) {
193 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000194 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000195 "mismatched sizes, (%ld vs %ld)\n",
196 map->type, map->size,
197 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000198 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000199 }
200
Eric Anholt9a298b22009-03-24 12:23:04 -0700201 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000202 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000203 return 0;
204 }
205
Daniel Vetter28185642013-08-08 15:41:27 +0200206 if (map->type == _DRM_FRAME_BUFFER ||
207 (map->flags & _DRM_WRITE_COMBINING)) {
208 map->mtrr =
209 arch_phys_wc_add(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Scott Thompson0769d392007-08-25 18:17:49 +1000211 if (map->type == _DRM_REGISTERS) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000212 if (map->flags & _DRM_WRITE_COMBINING)
213 map->handle = ioremap_wc(map->offset,
214 map->size);
215 else
216 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000217 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700218 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000219 return -ENOMEM;
220 }
221 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100225 list = drm_find_matching_map(dev, map);
226 if (list != NULL) {
227 if(list->map->size != map->size) {
228 DRM_DEBUG("Matching maps of type %d with "
229 "mismatched sizes, (%ld vs %ld)\n",
230 map->type, map->size, list->map->size);
231 list->map->size = map->size;
232 }
233
Eric Anholt9a298b22009-03-24 12:23:04 -0700234 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100235 *maplist = list;
236 return 0;
237 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100238 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 DRM_DEBUG("%lu %d %p\n",
Daniel Vetter04420c92013-07-10 14:11:57 +0200240 map->size, order_base_2(map->size), map->handle);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700242 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -ENOMEM;
244 }
245 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000248 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700250 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EBUSY;
252 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000253 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100256 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000257 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100258 int valid = 0;
259
Daniel Vetterd9906752013-12-11 11:34:35 +0100260 if (!dev->agp) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700261 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100264#ifdef __alpha__
265 map->offset += dev->hose->mem_space->start;
266#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000267 /* In some cases (i810 driver), user space may have already
268 * added the AGP base itself, because dev->agp->base previously
269 * only got set during AGP enable. So, only add the base
270 * address if the map's offset isn't already within the
271 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100272 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000273 if (map->offset < dev->agp->base ||
274 map->offset > dev->agp->base +
275 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
276 map->offset += dev->agp->base;
277 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100278 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
279
280 /* This assumes the DRM is in total control of AGP space.
281 * It's not always the case as AGP can be in the control
282 * of user space (i.e. i810 driver). So this loop will get
283 * skipped and we double check that dev->agp->memory is
284 * actually set as well as being invalid before EPERM'ing
285 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000286 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100287 if ((map->offset >= entry->bound) &&
288 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
289 valid = 1;
290 break;
291 }
292 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000293 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700294 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100295 return -EPERM;
296 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100297 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
298 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case _DRM_SCATTER_GATHER:
303 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700304 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -EINVAL;
306 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000307 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000309 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000310 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000311 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000312 * casting it down to 32 bits is no problem, but we
313 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800314 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000315 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700316 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000317 return -ENOMEM;
318 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000319 map->handle = dmah->vaddr;
320 map->offset = (unsigned long)dmah->busaddr;
321 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700324 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -EINVAL;
326 }
327
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400328 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700330 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100331 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700332 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EINVAL;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 list->map = map;
336
Dave Airlie30e2fb12006-02-02 19:37:46 +1100337 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000338 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000339
Dave Airlied1f2b552005-08-05 22:11:22 +1000340 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100341 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000342 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
343 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800344 ret = drm_map_handle(dev, &list->hash, user_token, 0,
345 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000346 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700347 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100348 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700349 kfree(map);
350 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000351 mutex_unlock(&dev->struct_mutex);
352 return ret;
353 }
354
Thomas Hellstrom15450852007-02-08 16:14:05 +1100355 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100356 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000358 if (!(map->flags & _DRM_DRIVER))
359 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000360 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000361 return 0;
Thierry Redingafe0f692014-04-29 11:44:38 +0200362}
Dave Airlie89625eb2005-09-05 21:23:23 +1000363
David Herrmann9fc5cde2014-08-29 12:12:28 +0200364int drm_legacy_addmap(struct drm_device * dev, resource_size_t offset,
365 unsigned int size, enum drm_map_type type,
366 enum drm_map_flags flags, struct drm_local_map **map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000367{
Dave Airlie55910512007-07-11 16:53:40 +1000368 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000369 int rc;
370
371 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
372 if (!rc)
373 *map_ptr = list->map;
374 return rc;
375}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200376EXPORT_SYMBOL(drm_legacy_addmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000377
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100378/**
379 * Ioctl to specify a range of memory that is available for mapping by a
380 * non-root process.
381 *
382 * \param inode device inode.
383 * \param file_priv DRM file private.
384 * \param cmd command.
385 * \param arg pointer to a drm_map structure.
386 * \return zero on success or a negative value on error.
387 *
388 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200389int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
390 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000391{
Eric Anholtc153f452007-09-03 12:06:45 +1000392 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000393 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000394 int err;
395
Dave Airlie7c1c2872008-11-28 14:22:24 +1000396 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100397 return -EPERM;
398
Eric Anholtc153f452007-09-03 12:06:45 +1000399 err = drm_addmap_core(dev, map->offset, map->size, map->type,
400 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000401
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000403 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000404
Dave Airlie67e1a012005-10-24 18:41:39 +1000405 /* 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 +1000406 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000407
408 /*
409 * It appears that there are no users of this value whatsoever --
410 * drmAddMap just discards it. Let's not encourage its use.
411 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
412 * it's not a real mtrr index anymore.)
413 */
414 map->mtrr = -1;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
420 * Remove a map private from list and deallocate resources if the mapping
421 * isn't in use.
422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * Searches the map on drm_device::maplist, removes it from the list, see if
424 * its being used, and free any associate resource (such as MTRR's) if it's not
425 * being on use.
426 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200427 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200429int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Dave Airlie55910512007-07-11 16:53:40 +1000431 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000432 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000433 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000434 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Dave Airlie836cf042005-07-10 19:27:04 +1000436 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000437 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000438 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000439 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000440 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100441 drm_ht_remove_key(&dev->map_hash,
442 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700443 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000444 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Dave Airlie836cf042005-07-10 19:27:04 +1000448
Dave Airliebd1b3312007-05-26 05:01:51 +1000449 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000450 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000451
452 switch (map->type) {
453 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100454 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000455 /* FALLTHROUGH */
456 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200457 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000458 break;
459 case _DRM_SHM:
460 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000461 if (master) {
462 if (dev->sigdata.lock == master->lock.hw_lock)
463 dev->sigdata.lock = NULL;
464 master->lock.hw_lock = NULL; /* SHM removed */
465 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100466 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000467 }
Dave Airlie836cf042005-07-10 19:27:04 +1000468 break;
469 case _DRM_AGP:
470 case _DRM_SCATTER_GATHER:
471 break;
472 case _DRM_CONSISTENT:
473 dmah.vaddr = map->handle;
474 dmah.busaddr = map->offset;
475 dmah.size = map->size;
Daniel Vetter1c96e842014-09-10 12:43:51 +0200476 __drm_legacy_pci_free(dev, &dmah);
Dave Airlie836cf042005-07-10 19:27:04 +1000477 break;
478 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700479 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
482}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200483EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000484
David Herrmann9fc5cde2014-08-29 12:12:28 +0200485int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000486{
487 int ret;
488
Dave Airlie30e2fb12006-02-02 19:37:46 +1100489 mutex_lock(&dev->struct_mutex);
David Herrmann9fc5cde2014-08-29 12:12:28 +0200490 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100491 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000492
493 return ret;
494}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200495EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000496
Dave Airlie836cf042005-07-10 19:27:04 +1000497/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
498 * the last close of the device, and this is necessary for cleanup when things
499 * exit uncleanly. Therefore, having userland manually remove mappings seems
500 * like a pointless exercise since they're going away anyway.
501 *
502 * One use case might be after addmap is allowed for normal users for SHM and
503 * gets used by drivers that the server doesn't need to care about. This seems
504 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100505 *
506 * \param inode device inode.
507 * \param file_priv DRM file private.
508 * \param cmd command.
509 * \param arg pointer to a struct drm_map structure.
510 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000511 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200512int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
513 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000514{
Eric Anholtc153f452007-09-03 12:06:45 +1000515 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100516 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000517 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000518 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000519
Dave Airlie30e2fb12006-02-02 19:37:46 +1100520 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000521 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000522 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000523 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000524 r_list->map->flags & _DRM_REMOVABLE) {
525 map = r_list->map;
526 break;
527 }
528 }
529
530 /* List has wrapped around to the head pointer, or its empty we didn't
531 * find anything.
532 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000533 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100534 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000535 return -EINVAL;
536 }
537
Dave Airlie836cf042005-07-10 19:27:04 +1000538 /* Register and framebuffer maps are permanent */
539 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100540 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000541 return 0;
542 }
543
David Herrmann9fc5cde2014-08-29 12:12:28 +0200544 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000545
Dave Airlie30e2fb12006-02-02 19:37:46 +1100546 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000547
548 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551/**
552 * Cleanup after an error on one of the addbufs() functions.
553 *
Dave Airlie836cf042005-07-10 19:27:04 +1000554 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * \param entry buffer entry where the error occurred.
556 *
557 * Frees any pages and buffers associated with the given entry.
558 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000559static void drm_cleanup_buf_error(struct drm_device * dev,
560 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 int i;
563
564 if (entry->seg_count) {
565 for (i = 0; i < entry->seg_count; i++) {
566 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100567 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700570 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 entry->seg_count = 0;
573 }
574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 if (entry->buf_count) {
576 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700577 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700579 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 entry->buf_count = 0;
582 }
583}
584
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200585#if IS_ENABLED(CONFIG_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/**
Dave Airlied59431b2005-07-10 15:00:06 +1000587 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000589 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000590 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * After some sanity checks creates a drm_buf structure for each buffer and
594 * reallocates the buffer list of the same size order to accommodate the new
595 * buffers.
596 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200597int drm_legacy_addbufs_agp(struct drm_device *dev,
598 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Dave Airliecdd55a22007-07-11 16:32:08 +1000600 struct drm_device_dma *dma = dev->dma;
601 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000602 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000603 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unsigned long offset;
605 unsigned long agp_offset;
606 int count;
607 int order;
608 int size;
609 int alignment;
610 int page_order;
611 int total;
612 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100613 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000614 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000616 if (!dma)
617 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dave Airlied59431b2005-07-10 15:00:06 +1000619 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200620 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 size = 1 << order;
622
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 alignment = (request->flags & _DRM_PAGE_ALIGN)
624 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
626 total = PAGE_SIZE << page_order;
627
628 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000629 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 DRM_DEBUG("count: %d\n", count);
632 DRM_DEBUG("order: %d\n", order);
633 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100634 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 DRM_DEBUG("alignment: %d\n", alignment);
636 DRM_DEBUG("page_order: %d\n", page_order);
637 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Dave Airlie54ba2f72007-02-10 12:07:47 +1100642 /* Make sure buffers are located in AGP memory that we own */
643 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000644 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100645 if ((agp_offset >= agp_entry->bound) &&
646 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
647 valid = 1;
648 break;
649 }
650 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000651 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100652 DRM_DEBUG("zone invalid\n");
653 return -EINVAL;
654 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100655 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100657 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EBUSY;
659 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100661 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Dave Airlie30e2fb12006-02-02 19:37:46 +1100663 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100666 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 atomic_dec(&dev->buf_alloc);
668 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
671 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100672 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINVAL;
675 }
676
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400677 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 if (!entry->buflist) {
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -ENOMEM;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 entry->buf_size = size;
685 entry->page_order = page_order;
686
687 offset = 0;
688
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 while (entry->buf_count < count) {
690 buf = &entry->buflist[entry->buf_count];
691 buf->idx = dma->buf_count + entry->buf_count;
692 buf->total = alignment;
693 buf->order = order;
694 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 buf->bus_address = agp_offset + offset;
698 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 buf->waiting = 0;
701 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000702 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400705 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Set count correctly so we free the proper amount. */
708 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100710 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -ENOMEM;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 offset += alignment;
718 entry->buf_count++;
719 byte_count += PAGE_SIZE << page_order;
720 }
721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Eric Anholt9a298b22009-03-24 12:23:04 -0700724 temp_buflist = krealloc(dma->buflist,
725 (dma->buf_count + entry->buf_count) *
726 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100730 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -ENOMEM;
733 }
734 dma->buflist = temp_buflist;
735
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
738 }
739
740 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100741 dma->seg_count += entry->seg_count;
742 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 dma->byte_count += byte_count;
744
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
746 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Dave Airlie30e2fb12006-02-02 19:37:46 +1100748 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Airlied59431b2005-07-10 15:00:06 +1000750 request->count = entry->buf_count;
751 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 dma->flags = _DRM_DMA_USE_AGP;
754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return 0;
757}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200758EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200759#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760
David Herrmann9fc5cde2014-08-29 12:12:28 +0200761int drm_legacy_addbufs_pci(struct drm_device *dev,
762 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Dave Airliecdd55a22007-07-11 16:32:08 +1000764 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int count;
766 int order;
767 int size;
768 int total;
769 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000770 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100771 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000772 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int alignment;
774 unsigned long offset;
775 int i;
776 int byte_count;
777 int page_count;
778 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000779 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
782 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100783
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 if (!dma)
785 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Dave Airlied985c102006-01-02 21:32:48 +1100787 if (!capable(CAP_SYS_ADMIN))
788 return -EPERM;
789
Dave Airlied59431b2005-07-10 15:00:06 +1000790 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200791 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 size = 1 << order;
793
Daniel Vettera344a7e2011-10-26 00:54:41 +0200794 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
795 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
798 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Airlied59431b2005-07-10 15:00:06 +1000800 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
803 total = PAGE_SIZE << page_order;
804
Daniel Vetter2177a212013-12-16 11:21:06 +0100805 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100807 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -EBUSY;
809 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100811 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Dave Airlie30e2fb12006-02-02 19:37:46 +1100813 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100816 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -ENOMEM; /* May only call once for each order */
819 }
820
821 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100822 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -EINVAL;
825 }
826
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400827 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100829 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -ENOMEM;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400834 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700836 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100837 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -ENOMEM;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* Keep the original pagelist until we know all the allocations
843 * have succeeded
844 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700845 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
846 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700848 kfree(entry->buflist);
849 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100850 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return -ENOMEM;
853 }
854 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
856 DRM_DEBUG("pagelist: %d entries\n",
857 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 entry->page_order = page_order;
861 byte_count = 0;
862 page_count = 0;
863
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000865
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800866 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000867
Dave Airlieddf19b92006-03-19 18:56:12 +1100868 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /* Set count correctly so we free the proper amount. */
870 entry->buf_count = count;
871 entry->seg_count = count;
872 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700873 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100874 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -ENOMEM;
877 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100878 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 for (i = 0; i < (1 << page_order); i++) {
880 DRM_DEBUG("page %d @ 0x%08lx\n",
881 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100882 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100884 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 for (offset = 0;
887 offset + size <= total && entry->buf_count < count;
888 offset += alignment, ++entry->buf_count) {
889 buf = &entry->buflist[entry->buf_count];
890 buf->idx = dma->buf_count + entry->buf_count;
891 buf->total = alignment;
892 buf->order = order;
893 buf->used = 0;
894 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100895 buf->address = (void *)(dmah->vaddr + offset);
896 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 buf->waiting = 0;
899 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000900 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400903 buf->dev_private = kzalloc(buf->dev_priv_size,
904 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /* Set count correctly so we free the proper amount. */
907 entry->buf_count = count;
908 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700910 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100911 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -ENOMEM;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 DRM_DEBUG("buffer %d @ %p\n",
917 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 byte_count += PAGE_SIZE << page_order;
920 }
921
Eric Anholt9a298b22009-03-24 12:23:04 -0700922 temp_buflist = krealloc(dma->buflist,
923 (dma->buf_count + entry->buf_count) *
924 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!temp_buflist) {
926 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700928 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100929 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -ENOMEM;
932 }
933 dma->buflist = temp_buflist;
934
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
937 }
938
Thomas Weber88393162010-03-16 11:47:56 +0100939 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * with the new one.
941 */
942 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700943 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 dma->pagelist = temp_pagelist;
946
947 dma->buf_count += entry->buf_count;
948 dma->seg_count += entry->seg_count;
949 dma->page_count += entry->seg_count << page_order;
950 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
951
Dave Airlie30e2fb12006-02-02 19:37:46 +1100952 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dave Airlied59431b2005-07-10 15:00:06 +1000954 request->count = entry->buf_count;
955 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
George Sapountzis3417f332006-10-24 12:03:04 -0700957 if (request->flags & _DRM_PCI_BUFFER_RO)
958 dma->flags = _DRM_DMA_USE_PCI_RO;
959
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962
963}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200964EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
David Herrmann9fc5cde2014-08-29 12:12:28 +0200966static int drm_legacy_addbufs_sg(struct drm_device *dev,
967 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Dave Airliecdd55a22007-07-11 16:32:08 +1000969 struct drm_device_dma *dma = dev->dma;
970 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000971 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 unsigned long offset;
973 unsigned long agp_offset;
974 int count;
975 int order;
976 int size;
977 int alignment;
978 int page_order;
979 int total;
980 int byte_count;
981 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000982 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 if (!drm_core_check_feature(dev, DRIVER_SG))
985 return -EINVAL;
986
987 if (!dma)
988 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Dave Airlied985c102006-01-02 21:32:48 +1100990 if (!capable(CAP_SYS_ADMIN))
991 return -EPERM;
992
Dave Airlied59431b2005-07-10 15:00:06 +1000993 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200994 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 size = 1 << order;
996
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 alignment = (request->flags & _DRM_PAGE_ALIGN)
998 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1000 total = PAGE_SIZE << page_order;
1001
1002 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001003 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 DRM_DEBUG("count: %d\n", count);
1006 DRM_DEBUG("order: %d\n", order);
1007 DRM_DEBUG("size: %d\n", size);
1008 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1009 DRM_DEBUG("alignment: %d\n", alignment);
1010 DRM_DEBUG("page_order: %d\n", page_order);
1011 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1014 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Daniel Vetter2177a212013-12-16 11:21:06 +01001016 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001018 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return -EBUSY;
1020 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001022 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlie30e2fb12006-02-02 19:37:46 +11001024 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001027 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 atomic_dec(&dev->buf_alloc);
1029 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001033 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return -EINVAL;
1036 }
1037
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001038 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001039 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 if (!entry->buflist) {
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 -ENOMEM;
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 entry->buf_size = size;
1047 entry->page_order = page_order;
1048
1049 offset = 0;
1050
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 while (entry->buf_count < count) {
1052 buf = &entry->buflist[entry->buf_count];
1053 buf->idx = dma->buf_count + entry->buf_count;
1054 buf->total = alignment;
1055 buf->order = order;
1056 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001061 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 buf->waiting = 0;
1064 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001065 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001068 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* Set count correctly so we free the proper amount. */
1071 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001073 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return -ENOMEM;
1076 }
1077
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 offset += alignment;
1081 entry->buf_count++;
1082 byte_count += PAGE_SIZE << page_order;
1083 }
1084
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Eric Anholt9a298b22009-03-24 12:23:04 -07001087 temp_buflist = krealloc(dma->buflist,
1088 (dma->buf_count + entry->buf_count) *
1089 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001093 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -ENOMEM;
1096 }
1097 dma->buflist = temp_buflist;
1098
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1101 }
1102
1103 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001104 dma->seg_count += entry->seg_count;
1105 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 dma->byte_count += byte_count;
1107
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1109 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Dave Airlie30e2fb12006-02-02 19:37:46 +11001111 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Dave Airlied59431b2005-07-10 15:00:06 +10001113 request->count = entry->buf_count;
1114 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 dma->flags = _DRM_DMA_USE_SG;
1117
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
1120}
1121
1122/**
1123 * Add buffers for DMA transfers (ioctl).
1124 *
1125 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001126 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001128 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 * \return zero on success or a negative number on failure.
1130 *
1131 * According with the memory type specified in drm_buf_desc::flags and the
1132 * build options, it dispatches the call either to addbufs_agp(),
1133 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1134 * PCI memory respectively.
1135 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001136int drm_legacy_addbufs(struct drm_device *dev, void *data,
1137 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Eric Anholtc153f452007-09-03 12:06:45 +10001139 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001140 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001142 if (drm_core_check_feature(dev, DRIVER_MODESET))
1143 return -EINVAL;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1146 return -EINVAL;
1147
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001148#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001149 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001150 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 else
1152#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001153 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001154 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001155 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001156 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001158 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001159
Dave Airlied59431b2005-07-10 15:00:06 +10001160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/**
1164 * Get information about the buffer mappings.
1165 *
1166 * This was originally mean for debugging purposes, or by a sophisticated
1167 * client library to determine how best to use the available buffers (e.g.,
1168 * large buffers can be used for image transfer).
1169 *
1170 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001171 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * \param cmd command.
1173 * \param arg pointer to a drm_buf_info structure.
1174 * \return zero on success or a negative number on failure.
1175 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001176 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 * lock, preventing of allocating more buffers after this call. Information
1178 * about each requested buffer is then copied into user space.
1179 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001180int drm_legacy_infobufs(struct drm_device *dev, void *data,
1181 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Dave Airliecdd55a22007-07-11 16:32:08 +10001183 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001184 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int i;
1186 int count;
1187
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001188 if (drm_core_check_feature(dev, DRIVER_MODESET))
1189 return -EINVAL;
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1192 return -EINVAL;
1193
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001194 if (!dma)
1195 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Daniel Vetter2177a212013-12-16 11:21:06 +01001197 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001198 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001199 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return -EBUSY;
1201 }
1202 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001203 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001205 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1206 if (dma->bufs[i].buf_count)
1207 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001210 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Eric Anholtc153f452007-09-03 12:06:45 +10001212 if (request->count >= count) {
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) {
Dave Airliec60ce622007-07-11 15:27:12 +10001215 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001216 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001217 struct drm_buf_entry *from = &dma->bufs[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001218 if (copy_to_user(&to->count,
1219 &from->buf_count,
1220 sizeof(from->buf_count)) ||
1221 copy_to_user(&to->size,
1222 &from->buf_size,
1223 sizeof(from->buf_size)) ||
1224 copy_to_user(&to->low_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001225 &from->low_mark,
1226 sizeof(from->low_mark)) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001227 copy_to_user(&to->high_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001228 &from->high_mark,
1229 sizeof(from->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -EFAULT;
1231
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001232 DRM_DEBUG("%d %d %d %d %d\n",
1233 i,
1234 dma->bufs[i].buf_count,
1235 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001236 dma->bufs[i].low_mark,
1237 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ++count;
1239 }
1240 }
1241 }
Eric Anholtc153f452007-09-03 12:06:45 +10001242 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 return 0;
1245}
1246
1247/**
1248 * Specifies a low and high water mark for buffer allocation
1249 *
1250 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001251 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 * \param cmd command.
1253 * \param arg a pointer to a drm_buf_desc structure.
1254 * \return zero on success or a negative number on failure.
1255 *
1256 * Verifies that the size order is bounded between the admissible orders and
1257 * updates the respective drm_device_dma::bufs entry low and high water mark.
1258 *
1259 * \note This ioctl is deprecated and mostly never used.
1260 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001261int drm_legacy_markbufs(struct drm_device *dev, void *data,
1262 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Dave Airliecdd55a22007-07-11 16:32:08 +10001264 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001265 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001267 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001269 if (drm_core_check_feature(dev, DRIVER_MODESET))
1270 return -EINVAL;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1273 return -EINVAL;
1274
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 if (!dma)
1276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001278 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001279 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001280 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001281 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1282 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 entry = &dma->bufs[order];
1284
Eric Anholtc153f452007-09-03 12:06:45 +10001285 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001287 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -EINVAL;
1289
David Herrmannb008c0f2014-07-23 17:26:36 +02001290 entry->low_mark = request->low_mark;
1291 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 return 0;
1294}
1295
1296/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001297 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 *
1299 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001300 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * \param cmd command.
1302 * \param arg pointer to a drm_buf_free structure.
1303 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 * Calls free_buffer() for each used buffer.
1306 * This function is primarily used for debugging.
1307 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001308int drm_legacy_freebufs(struct drm_device *dev, void *data,
1309 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Dave Airliecdd55a22007-07-11 16:32:08 +10001311 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001312 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int i;
1314 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001315 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001317 if (drm_core_check_feature(dev, DRIVER_MODESET))
1318 return -EINVAL;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1321 return -EINVAL;
1322
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001323 if (!dma)
1324 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Eric Anholtc153f452007-09-03 12:06:45 +10001326 DRM_DEBUG("%d\n", request->count);
1327 for (i = 0; i < request->count; i++) {
1328 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330 if (idx < 0 || idx >= dma->buf_count) {
1331 DRM_ERROR("Index %d (of %d max)\n",
1332 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return -EINVAL;
1334 }
1335 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001336 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001337 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001338 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return -EINVAL;
1340 }
Daniel Vettera2661622014-09-11 07:41:51 +02001341 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
1344 return 0;
1345}
1346
1347/**
1348 * Maps all of the DMA buffers into client-virtual space (ioctl).
1349 *
1350 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001351 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * \param cmd command.
1353 * \param arg pointer to a drm_buf_map structure.
1354 * \return zero on success or a negative number on failure.
1355 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001356 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1357 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001358 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1359 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001361int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1362 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Dave Airliecdd55a22007-07-11 16:32:08 +10001364 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 int retcode = 0;
1366 const int zero = 0;
1367 unsigned long virtual;
1368 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001369 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 int i;
1371
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001372 if (drm_core_check_feature(dev, DRIVER_MODESET))
1373 return -EINVAL;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1376 return -EINVAL;
1377
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 if (!dma)
1379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Daniel Vetter2177a212013-12-16 11:21:06 +01001381 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001383 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -EBUSY;
1385 }
1386 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001387 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Eric Anholtc153f452007-09-03 12:06:45 +10001389 if (request->count >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001390 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001391 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001392 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001393 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001394 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 retcode = -EINVAL;
1398 goto done;
1399 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001400 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001402 MAP_SHARED,
1403 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001405 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 PROT_READ | PROT_WRITE,
1407 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001409 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /* Real error */
1411 retcode = (signed long)virtual;
1412 goto done;
1413 }
Eric Anholtc153f452007-09-03 12:06:45 +10001414 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001416 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001417 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001418 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001419 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 retcode = -EFAULT;
1421 goto done;
1422 }
Eric Anholtc153f452007-09-03 12:06:45 +10001423 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001424 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001425 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 retcode = -EFAULT;
1427 goto done;
1428 }
Eric Anholtc153f452007-09-03 12:06:45 +10001429 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 retcode = -EFAULT;
1432 goto done;
1433 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001435 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 retcode = -EFAULT;
1438 goto done;
1439 }
1440 }
1441 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001442 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001443 request->count = dma->buf_count;
1444 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 return retcode;
1447}
1448
David Herrmann9fc5cde2014-08-29 12:12:28 +02001449int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001450 struct drm_file *file_priv)
1451{
1452 if (drm_core_check_feature(dev, DRIVER_MODESET))
1453 return -EINVAL;
1454
1455 if (dev->driver->dma_ioctl)
1456 return dev->driver->dma_ioctl(dev, data, file_priv);
1457 else
1458 return -EINVAL;
1459}
1460
David Herrmann9fc5cde2014-08-29 12:12:28 +02001461struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001462{
1463 struct drm_map_list *entry;
1464
1465 list_for_each_entry(entry, &dev->maplist, head) {
1466 if (entry->map && entry->map->type == _DRM_SHM &&
1467 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1468 return entry->map;
1469 }
1470 }
1471 return NULL;
1472}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001473EXPORT_SYMBOL(drm_legacy_getsarea);