blob: d92db7007f62c44aed9b247c30b8f0f7a32372b6 [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
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200419/*
420 * Get a mapping information.
421 *
422 * \param inode device inode.
423 * \param file_priv DRM file private.
424 * \param cmd command.
425 * \param arg user argument, pointing to a drm_map structure.
426 *
427 * \return zero on success or a negative number on failure.
428 *
429 * Searches for the mapping with the specified offset and copies its information
430 * into userspace
431 */
432int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
433 struct drm_file *file_priv)
434{
435 struct drm_map *map = data;
436 struct drm_map_list *r_list = NULL;
437 struct list_head *list;
438 int idx;
439 int i;
440
441 idx = map->offset;
442 if (idx < 0)
443 return -EINVAL;
444
445 i = 0;
446 mutex_lock(&dev->struct_mutex);
447 list_for_each(list, &dev->maplist) {
448 if (i == idx) {
449 r_list = list_entry(list, struct drm_map_list, head);
450 break;
451 }
452 i++;
453 }
454 if (!r_list || !r_list->map) {
455 mutex_unlock(&dev->struct_mutex);
456 return -EINVAL;
457 }
458
459 map->offset = r_list->map->offset;
460 map->size = r_list->map->size;
461 map->type = r_list->map->type;
462 map->flags = r_list->map->flags;
463 map->handle = (void *)(unsigned long) r_list->user_token;
464 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
465
466 mutex_unlock(&dev->struct_mutex);
467
468 return 0;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/**
472 * Remove a map private from list and deallocate resources if the mapping
473 * isn't in use.
474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Searches the map on drm_device::maplist, removes it from the list, see if
476 * its being used, and free any associate resource (such as MTRR's) if it's not
477 * being on use.
478 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200479 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200481int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Dave Airlie55910512007-07-11 16:53:40 +1000483 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000484 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000485 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000486 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Dave Airlie836cf042005-07-10 19:27:04 +1000488 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000489 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000490 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000491 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000492 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100493 drm_ht_remove_key(&dev->map_hash,
494 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700495 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000496 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Dave Airlie836cf042005-07-10 19:27:04 +1000500
Dave Airliebd1b3312007-05-26 05:01:51 +1000501 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000502 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000503
504 switch (map->type) {
505 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100506 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000507 /* FALLTHROUGH */
508 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200509 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000510 break;
511 case _DRM_SHM:
512 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000513 if (master) {
514 if (dev->sigdata.lock == master->lock.hw_lock)
515 dev->sigdata.lock = NULL;
516 master->lock.hw_lock = NULL; /* SHM removed */
517 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100518 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000519 }
Dave Airlie836cf042005-07-10 19:27:04 +1000520 break;
521 case _DRM_AGP:
522 case _DRM_SCATTER_GATHER:
523 break;
524 case _DRM_CONSISTENT:
525 dmah.vaddr = map->handle;
526 dmah.busaddr = map->offset;
527 dmah.size = map->size;
Daniel Vetter1c96e842014-09-10 12:43:51 +0200528 __drm_legacy_pci_free(dev, &dmah);
Dave Airlie836cf042005-07-10 19:27:04 +1000529 break;
530 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700531 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
534}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200535EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000536
David Herrmann9fc5cde2014-08-29 12:12:28 +0200537int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000538{
539 int ret;
540
Dave Airlie30e2fb12006-02-02 19:37:46 +1100541 mutex_lock(&dev->struct_mutex);
David Herrmann9fc5cde2014-08-29 12:12:28 +0200542 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100543 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000544
545 return ret;
546}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200547EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000548
Dave Airlie836cf042005-07-10 19:27:04 +1000549/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
550 * the last close of the device, and this is necessary for cleanup when things
551 * exit uncleanly. Therefore, having userland manually remove mappings seems
552 * like a pointless exercise since they're going away anyway.
553 *
554 * One use case might be after addmap is allowed for normal users for SHM and
555 * gets used by drivers that the server doesn't need to care about. This seems
556 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100557 *
558 * \param inode device inode.
559 * \param file_priv DRM file private.
560 * \param cmd command.
561 * \param arg pointer to a struct drm_map structure.
562 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000563 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200564int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
565 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000566{
Eric Anholtc153f452007-09-03 12:06:45 +1000567 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100568 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000569 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000570 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000571
Dave Airlie30e2fb12006-02-02 19:37:46 +1100572 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000573 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000574 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000575 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000576 r_list->map->flags & _DRM_REMOVABLE) {
577 map = r_list->map;
578 break;
579 }
580 }
581
582 /* List has wrapped around to the head pointer, or its empty we didn't
583 * find anything.
584 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000585 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100586 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000587 return -EINVAL;
588 }
589
Dave Airlie836cf042005-07-10 19:27:04 +1000590 /* Register and framebuffer maps are permanent */
591 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100592 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000593 return 0;
594 }
595
David Herrmann9fc5cde2014-08-29 12:12:28 +0200596 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000597
Dave Airlie30e2fb12006-02-02 19:37:46 +1100598 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000599
600 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/**
604 * Cleanup after an error on one of the addbufs() functions.
605 *
Dave Airlie836cf042005-07-10 19:27:04 +1000606 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * \param entry buffer entry where the error occurred.
608 *
609 * Frees any pages and buffers associated with the given entry.
610 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000611static void drm_cleanup_buf_error(struct drm_device * dev,
612 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 int i;
615
616 if (entry->seg_count) {
617 for (i = 0; i < entry->seg_count; i++) {
618 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100619 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700622 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 entry->seg_count = 0;
625 }
626
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000627 if (entry->buf_count) {
628 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700629 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700631 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 entry->buf_count = 0;
634 }
635}
636
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200637#if IS_ENABLED(CONFIG_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/**
Dave Airlied59431b2005-07-10 15:00:06 +1000639 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000641 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000642 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * After some sanity checks creates a drm_buf structure for each buffer and
646 * reallocates the buffer list of the same size order to accommodate the new
647 * buffers.
648 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200649int drm_legacy_addbufs_agp(struct drm_device *dev,
650 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Dave Airliecdd55a22007-07-11 16:32:08 +1000652 struct drm_device_dma *dma = dev->dma;
653 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000654 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000655 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned long offset;
657 unsigned long agp_offset;
658 int count;
659 int order;
660 int size;
661 int alignment;
662 int page_order;
663 int total;
664 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100665 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000666 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668 if (!dma)
669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Dave Airlied59431b2005-07-10 15:00:06 +1000671 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200672 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 size = 1 << order;
674
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 alignment = (request->flags & _DRM_PAGE_ALIGN)
676 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
678 total = PAGE_SIZE << page_order;
679
680 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000681 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 DRM_DEBUG("count: %d\n", count);
684 DRM_DEBUG("order: %d\n", order);
685 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100686 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 DRM_DEBUG("alignment: %d\n", alignment);
688 DRM_DEBUG("page_order: %d\n", page_order);
689 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
692 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Dave Airlie54ba2f72007-02-10 12:07:47 +1100694 /* Make sure buffers are located in AGP memory that we own */
695 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000696 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100697 if ((agp_offset >= agp_entry->bound) &&
698 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
699 valid = 1;
700 break;
701 }
702 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000703 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100704 DRM_DEBUG("zone invalid\n");
705 return -EINVAL;
706 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100707 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100709 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EBUSY;
711 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100713 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dave Airlie30e2fb12006-02-02 19:37:46 +1100715 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100718 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 atomic_dec(&dev->buf_alloc);
720 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100724 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EINVAL;
727 }
728
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400729 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100731 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -ENOMEM;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 entry->buf_size = size;
737 entry->page_order = page_order;
738
739 offset = 0;
740
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 while (entry->buf_count < count) {
742 buf = &entry->buflist[entry->buf_count];
743 buf->idx = dma->buf_count + entry->buf_count;
744 buf->total = alignment;
745 buf->order = order;
746 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 buf->bus_address = agp_offset + offset;
750 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 buf->waiting = 0;
753 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000754 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400757 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* Set count correctly so we free the proper amount. */
760 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100762 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -ENOMEM;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 offset += alignment;
770 entry->buf_count++;
771 byte_count += PAGE_SIZE << page_order;
772 }
773
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Eric Anholt9a298b22009-03-24 12:23:04 -0700776 temp_buflist = krealloc(dma->buflist,
777 (dma->buf_count + entry->buf_count) *
778 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100782 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -ENOMEM;
785 }
786 dma->buflist = temp_buflist;
787
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
790 }
791
792 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100793 dma->seg_count += entry->seg_count;
794 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dma->byte_count += byte_count;
796
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
798 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Airlie30e2fb12006-02-02 19:37:46 +1100800 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Dave Airlied59431b2005-07-10 15:00:06 +1000802 request->count = entry->buf_count;
803 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 dma->flags = _DRM_DMA_USE_AGP;
806
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
809}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200810EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200811#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812
David Herrmann9fc5cde2014-08-29 12:12:28 +0200813int drm_legacy_addbufs_pci(struct drm_device *dev,
814 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Dave Airliecdd55a22007-07-11 16:32:08 +1000816 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int count;
818 int order;
819 int size;
820 int total;
821 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000822 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100823 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000824 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int alignment;
826 unsigned long offset;
827 int i;
828 int byte_count;
829 int page_count;
830 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000831 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
834 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100835
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 if (!dma)
837 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Dave Airlied985c102006-01-02 21:32:48 +1100839 if (!capable(CAP_SYS_ADMIN))
840 return -EPERM;
841
Dave Airlied59431b2005-07-10 15:00:06 +1000842 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200843 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 size = 1 << order;
845
Daniel Vettera344a7e2011-10-26 00:54:41 +0200846 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
847 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
850 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Airlied59431b2005-07-10 15:00:06 +1000852 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
855 total = PAGE_SIZE << page_order;
856
Daniel Vetter2177a212013-12-16 11:21:06 +0100857 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100859 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -EBUSY;
861 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100863 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlie30e2fb12006-02-02 19:37:46 +1100865 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100868 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return -ENOMEM; /* May only call once for each order */
871 }
872
873 if (count < 0 || count > 4096) {
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 -EINVAL;
877 }
878
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400879 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100881 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -ENOMEM;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400886 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700888 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100889 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -ENOMEM;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* Keep the original pagelist until we know all the allocations
895 * have succeeded
896 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700897 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
898 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700900 kfree(entry->buflist);
901 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100902 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -ENOMEM;
905 }
906 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
908 DRM_DEBUG("pagelist: %d entries\n",
909 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 entry->page_order = page_order;
913 byte_count = 0;
914 page_count = 0;
915
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000917
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800918 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000919
Dave Airlieddf19b92006-03-19 18:56:12 +1100920 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* Set count correctly so we free the proper amount. */
922 entry->buf_count = count;
923 entry->seg_count = count;
924 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700925 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100926 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -ENOMEM;
929 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100930 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 for (i = 0; i < (1 << page_order); i++) {
932 DRM_DEBUG("page %d @ 0x%08lx\n",
933 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100934 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100936 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 for (offset = 0;
939 offset + size <= total && entry->buf_count < count;
940 offset += alignment, ++entry->buf_count) {
941 buf = &entry->buflist[entry->buf_count];
942 buf->idx = dma->buf_count + entry->buf_count;
943 buf->total = alignment;
944 buf->order = order;
945 buf->used = 0;
946 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100947 buf->address = (void *)(dmah->vaddr + offset);
948 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 buf->waiting = 0;
951 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000952 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400955 buf->dev_private = kzalloc(buf->dev_priv_size,
956 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000957 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* Set count correctly so we free the proper amount. */
959 entry->buf_count = count;
960 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700962 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100963 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000964 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -ENOMEM;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 DRM_DEBUG("buffer %d @ %p\n",
969 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 byte_count += PAGE_SIZE << page_order;
972 }
973
Eric Anholt9a298b22009-03-24 12:23:04 -0700974 temp_buflist = krealloc(dma->buflist,
975 (dma->buf_count + entry->buf_count) *
976 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!temp_buflist) {
978 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700980 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100981 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -ENOMEM;
984 }
985 dma->buflist = temp_buflist;
986
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
989 }
990
Thomas Weber88393162010-03-16 11:47:56 +0100991 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * with the new one.
993 */
994 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700995 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 dma->pagelist = temp_pagelist;
998
999 dma->buf_count += entry->buf_count;
1000 dma->seg_count += entry->seg_count;
1001 dma->page_count += entry->seg_count << page_order;
1002 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1003
Dave Airlie30e2fb12006-02-02 19:37:46 +11001004 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Dave Airlied59431b2005-07-10 15:00:06 +10001006 request->count = entry->buf_count;
1007 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
George Sapountzis3417f332006-10-24 12:03:04 -07001009 if (request->flags & _DRM_PCI_BUFFER_RO)
1010 dma->flags = _DRM_DMA_USE_PCI_RO;
1011
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014
1015}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001016EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
David Herrmann9fc5cde2014-08-29 12:12:28 +02001018static int drm_legacy_addbufs_sg(struct drm_device *dev,
1019 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Dave Airliecdd55a22007-07-11 16:32:08 +10001021 struct drm_device_dma *dma = dev->dma;
1022 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001023 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 unsigned long offset;
1025 unsigned long agp_offset;
1026 int count;
1027 int order;
1028 int size;
1029 int alignment;
1030 int page_order;
1031 int total;
1032 int byte_count;
1033 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001034 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 if (!drm_core_check_feature(dev, DRIVER_SG))
1037 return -EINVAL;
1038
1039 if (!dma)
1040 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Dave Airlied985c102006-01-02 21:32:48 +11001042 if (!capable(CAP_SYS_ADMIN))
1043 return -EPERM;
1044
Dave Airlied59431b2005-07-10 15:00:06 +10001045 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001046 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 size = 1 << order;
1048
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 alignment = (request->flags & _DRM_PAGE_ALIGN)
1050 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1052 total = PAGE_SIZE << page_order;
1053
1054 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001055 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 DRM_DEBUG("count: %d\n", count);
1058 DRM_DEBUG("order: %d\n", order);
1059 DRM_DEBUG("size: %d\n", size);
1060 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1061 DRM_DEBUG("alignment: %d\n", alignment);
1062 DRM_DEBUG("page_order: %d\n", page_order);
1063 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1066 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Daniel Vetter2177a212013-12-16 11:21:06 +01001068 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001070 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -EBUSY;
1072 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001074 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Dave Airlie30e2fb12006-02-02 19:37:46 +11001076 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001079 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 atomic_dec(&dev->buf_alloc);
1081 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
1084 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001085 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -EINVAL;
1088 }
1089
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001090 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001091 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 if (!entry->buflist) {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 entry->buf_size = size;
1099 entry->page_order = page_order;
1100
1101 offset = 0;
1102
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 while (entry->buf_count < count) {
1104 buf = &entry->buflist[entry->buf_count];
1105 buf->idx = dma->buf_count + entry->buf_count;
1106 buf->total = alignment;
1107 buf->order = order;
1108 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001113 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 buf->waiting = 0;
1116 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001117 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001120 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Set count correctly so we free the proper amount. */
1123 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001125 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return -ENOMEM;
1128 }
1129
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 offset += alignment;
1133 entry->buf_count++;
1134 byte_count += PAGE_SIZE << page_order;
1135 }
1136
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001137 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Eric Anholt9a298b22009-03-24 12:23:04 -07001139 temp_buflist = krealloc(dma->buflist,
1140 (dma->buf_count + entry->buf_count) *
1141 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001144 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001145 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -ENOMEM;
1148 }
1149 dma->buflist = temp_buflist;
1150
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001151 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1153 }
1154
1155 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001156 dma->seg_count += entry->seg_count;
1157 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 dma->byte_count += byte_count;
1159
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001160 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1161 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Dave Airlie30e2fb12006-02-02 19:37:46 +11001163 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Dave Airlied59431b2005-07-10 15:00:06 +10001165 request->count = entry->buf_count;
1166 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 dma->flags = _DRM_DMA_USE_SG;
1169
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172}
1173
1174/**
1175 * Add buffers for DMA transfers (ioctl).
1176 *
1177 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001178 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001180 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * \return zero on success or a negative number on failure.
1182 *
1183 * According with the memory type specified in drm_buf_desc::flags and the
1184 * build options, it dispatches the call either to addbufs_agp(),
1185 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1186 * PCI memory respectively.
1187 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001188int drm_legacy_addbufs(struct drm_device *dev, void *data,
1189 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Eric Anholtc153f452007-09-03 12:06:45 +10001191 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001192 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001193
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001194 if (drm_core_check_feature(dev, DRIVER_MODESET))
1195 return -EINVAL;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1198 return -EINVAL;
1199
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001200#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001201 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001202 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 else
1204#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001205 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001206 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001207 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001208 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001210 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001211
Dave Airlied59431b2005-07-10 15:00:06 +10001212 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
1216 * Get information about the buffer mappings.
1217 *
1218 * This was originally mean for debugging purposes, or by a sophisticated
1219 * client library to determine how best to use the available buffers (e.g.,
1220 * large buffers can be used for image transfer).
1221 *
1222 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001223 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * \param cmd command.
1225 * \param arg pointer to a drm_buf_info structure.
1226 * \return zero on success or a negative number on failure.
1227 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001228 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 * lock, preventing of allocating more buffers after this call. Information
1230 * about each requested buffer is then copied into user space.
1231 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001232int drm_legacy_infobufs(struct drm_device *dev, void *data,
1233 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Dave Airliecdd55a22007-07-11 16:32:08 +10001235 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001236 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int i;
1238 int count;
1239
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001240 if (drm_core_check_feature(dev, DRIVER_MODESET))
1241 return -EINVAL;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1244 return -EINVAL;
1245
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001246 if (!dma)
1247 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Daniel Vetter2177a212013-12-16 11:21:06 +01001249 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001250 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001251 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -EBUSY;
1253 }
1254 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001255 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1258 if (dma->bufs[i].buf_count)
1259 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001262 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Eric Anholtc153f452007-09-03 12:06:45 +10001264 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001265 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1266 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001267 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001268 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001269 struct drm_buf_entry *from = &dma->bufs[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001270 if (copy_to_user(&to->count,
1271 &from->buf_count,
1272 sizeof(from->buf_count)) ||
1273 copy_to_user(&to->size,
1274 &from->buf_size,
1275 sizeof(from->buf_size)) ||
1276 copy_to_user(&to->low_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001277 &from->low_mark,
1278 sizeof(from->low_mark)) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001279 copy_to_user(&to->high_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001280 &from->high_mark,
1281 sizeof(from->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -EFAULT;
1283
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001284 DRM_DEBUG("%d %d %d %d %d\n",
1285 i,
1286 dma->bufs[i].buf_count,
1287 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001288 dma->bufs[i].low_mark,
1289 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 ++count;
1291 }
1292 }
1293 }
Eric Anholtc153f452007-09-03 12:06:45 +10001294 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 return 0;
1297}
1298
1299/**
1300 * Specifies a low and high water mark for buffer allocation
1301 *
1302 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001303 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * \param cmd command.
1305 * \param arg a pointer to a drm_buf_desc structure.
1306 * \return zero on success or a negative number on failure.
1307 *
1308 * Verifies that the size order is bounded between the admissible orders and
1309 * updates the respective drm_device_dma::bufs entry low and high water mark.
1310 *
1311 * \note This ioctl is deprecated and mostly never used.
1312 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001313int drm_legacy_markbufs(struct drm_device *dev, void *data,
1314 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Dave Airliecdd55a22007-07-11 16:32:08 +10001316 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001317 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001319 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001321 if (drm_core_check_feature(dev, DRIVER_MODESET))
1322 return -EINVAL;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1325 return -EINVAL;
1326
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001327 if (!dma)
1328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001331 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001332 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1334 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 entry = &dma->bufs[order];
1336
Eric Anholtc153f452007-09-03 12:06:45 +10001337 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001339 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return -EINVAL;
1341
David Herrmannb008c0f2014-07-23 17:26:36 +02001342 entry->low_mark = request->low_mark;
1343 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 return 0;
1346}
1347
1348/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001349 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
1351 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001352 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * \param cmd command.
1354 * \param arg pointer to a drm_buf_free structure.
1355 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * Calls free_buffer() for each used buffer.
1358 * This function is primarily used for debugging.
1359 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001360int drm_legacy_freebufs(struct drm_device *dev, void *data,
1361 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Dave Airliecdd55a22007-07-11 16:32:08 +10001363 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001364 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 int i;
1366 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001367 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001369 if (drm_core_check_feature(dev, DRIVER_MODESET))
1370 return -EINVAL;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1373 return -EINVAL;
1374
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001375 if (!dma)
1376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Eric Anholtc153f452007-09-03 12:06:45 +10001378 DRM_DEBUG("%d\n", request->count);
1379 for (i = 0; i < request->count; i++) {
1380 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 if (idx < 0 || idx >= dma->buf_count) {
1383 DRM_ERROR("Index %d (of %d max)\n",
1384 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -EINVAL;
1386 }
1387 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001388 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001390 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -EINVAL;
1392 }
Daniel Vettera2661622014-09-11 07:41:51 +02001393 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
1396 return 0;
1397}
1398
1399/**
1400 * Maps all of the DMA buffers into client-virtual space (ioctl).
1401 *
1402 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001403 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * \param cmd command.
1405 * \param arg pointer to a drm_buf_map structure.
1406 * \return zero on success or a negative number on failure.
1407 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001408 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1409 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001410 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1411 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001413int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1414 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Dave Airliecdd55a22007-07-11 16:32:08 +10001416 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int retcode = 0;
1418 const int zero = 0;
1419 unsigned long virtual;
1420 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001421 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int i;
1423
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001424 if (drm_core_check_feature(dev, DRIVER_MODESET))
1425 return -EINVAL;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1428 return -EINVAL;
1429
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 if (!dma)
1431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Daniel Vetter2177a212013-12-16 11:21:06 +01001433 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001435 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -EBUSY;
1437 }
1438 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001439 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Eric Anholtc153f452007-09-03 12:06:45 +10001441 if (request->count >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001442 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001444 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001445 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001446 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 retcode = -EINVAL;
1450 goto done;
1451 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001452 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001453 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001454 MAP_SHARED,
1455 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001457 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001458 PROT_READ | PROT_WRITE,
1459 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001461 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* Real error */
1463 retcode = (signed long)virtual;
1464 goto done;
1465 }
Eric Anholtc153f452007-09-03 12:06:45 +10001466 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001468 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001469 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001470 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001471 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 retcode = -EFAULT;
1473 goto done;
1474 }
Eric Anholtc153f452007-09-03 12:06:45 +10001475 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001476 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001477 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 retcode = -EFAULT;
1479 goto done;
1480 }
Eric Anholtc153f452007-09-03 12:06:45 +10001481 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001482 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 retcode = -EFAULT;
1484 goto done;
1485 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001487 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001488 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 retcode = -EFAULT;
1490 goto done;
1491 }
1492 }
1493 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001495 request->count = dma->buf_count;
1496 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 return retcode;
1499}
1500
David Herrmann9fc5cde2014-08-29 12:12:28 +02001501int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001502 struct drm_file *file_priv)
1503{
1504 if (drm_core_check_feature(dev, DRIVER_MODESET))
1505 return -EINVAL;
1506
1507 if (dev->driver->dma_ioctl)
1508 return dev->driver->dma_ioctl(dev, data, file_priv);
1509 else
1510 return -EINVAL;
1511}
1512
David Herrmann9fc5cde2014-08-29 12:12:28 +02001513struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001514{
1515 struct drm_map_list *entry;
1516
1517 list_for_each_entry(entry, &dev->maplist, head) {
1518 if (entry->map && entry->map->type == _DRM_SHM &&
1519 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1520 return entry->map;
1521 }
1522 }
1523 return NULL;
1524}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001525EXPORT_SYMBOL(drm_legacy_getsarea);