blob: e8a12a4fd40070dfddb6e9a5264c7a67b275c1aa [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
Daniel Vettere975eef2016-04-26 19:29:37 +0200399 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
400 drm_core_check_feature(dev, DRIVER_MODESET))
401 return -EINVAL;
402
Eric Anholtc153f452007-09-03 12:06:45 +1000403 err = drm_addmap_core(dev, map->offset, map->size, map->type,
404 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000405
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000406 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000407 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000408
Dave Airlie67e1a012005-10-24 18:41:39 +1000409 /* 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 +1000410 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000411
412 /*
413 * It appears that there are no users of this value whatsoever --
414 * drmAddMap just discards it. Let's not encourage its use.
415 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
416 * it's not a real mtrr index anymore.)
417 */
418 map->mtrr = -1;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200423/*
424 * Get a mapping information.
425 *
426 * \param inode device inode.
427 * \param file_priv DRM file private.
428 * \param cmd command.
429 * \param arg user argument, pointing to a drm_map structure.
430 *
431 * \return zero on success or a negative number on failure.
432 *
433 * Searches for the mapping with the specified offset and copies its information
434 * into userspace
435 */
436int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
437 struct drm_file *file_priv)
438{
439 struct drm_map *map = data;
440 struct drm_map_list *r_list = NULL;
441 struct list_head *list;
442 int idx;
443 int i;
444
Daniel Vettere975eef2016-04-26 19:29:37 +0200445 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
446 drm_core_check_feature(dev, DRIVER_MODESET))
447 return -EINVAL;
448
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200449 idx = map->offset;
450 if (idx < 0)
451 return -EINVAL;
452
453 i = 0;
454 mutex_lock(&dev->struct_mutex);
455 list_for_each(list, &dev->maplist) {
456 if (i == idx) {
457 r_list = list_entry(list, struct drm_map_list, head);
458 break;
459 }
460 i++;
461 }
462 if (!r_list || !r_list->map) {
463 mutex_unlock(&dev->struct_mutex);
464 return -EINVAL;
465 }
466
467 map->offset = r_list->map->offset;
468 map->size = r_list->map->size;
469 map->type = r_list->map->type;
470 map->flags = r_list->map->flags;
471 map->handle = (void *)(unsigned long) r_list->user_token;
472 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
473
474 mutex_unlock(&dev->struct_mutex);
475
476 return 0;
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/**
480 * Remove a map private from list and deallocate resources if the mapping
481 * isn't in use.
482 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * Searches the map on drm_device::maplist, removes it from the list, see if
484 * its being used, and free any associate resource (such as MTRR's) if it's not
485 * being on use.
486 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200487 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200489int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Dave Airlie55910512007-07-11 16:53:40 +1000491 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000492 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000493 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000494 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Dave Airlie836cf042005-07-10 19:27:04 +1000496 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000497 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000498 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000499 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000500 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100501 drm_ht_remove_key(&dev->map_hash,
502 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700503 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000504 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Dave Airlie836cf042005-07-10 19:27:04 +1000508
Dave Airliebd1b3312007-05-26 05:01:51 +1000509 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000510 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000511
512 switch (map->type) {
513 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100514 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000515 /* FALLTHROUGH */
516 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200517 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000518 break;
519 case _DRM_SHM:
520 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000521 if (master) {
522 if (dev->sigdata.lock == master->lock.hw_lock)
523 dev->sigdata.lock = NULL;
524 master->lock.hw_lock = NULL; /* SHM removed */
525 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100526 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000527 }
Dave Airlie836cf042005-07-10 19:27:04 +1000528 break;
529 case _DRM_AGP:
530 case _DRM_SCATTER_GATHER:
531 break;
532 case _DRM_CONSISTENT:
533 dmah.vaddr = map->handle;
534 dmah.busaddr = map->offset;
535 dmah.size = map->size;
Daniel Vetter1c96e842014-09-10 12:43:51 +0200536 __drm_legacy_pci_free(dev, &dmah);
Dave Airlie836cf042005-07-10 19:27:04 +1000537 break;
538 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700539 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
542}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200543EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000544
David Herrmann9fc5cde2014-08-29 12:12:28 +0200545int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000546{
547 int ret;
548
Dave Airlie30e2fb12006-02-02 19:37:46 +1100549 mutex_lock(&dev->struct_mutex);
David Herrmann9fc5cde2014-08-29 12:12:28 +0200550 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100551 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000552
553 return ret;
554}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200555EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000556
Dave Airlie836cf042005-07-10 19:27:04 +1000557/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
558 * the last close of the device, and this is necessary for cleanup when things
559 * exit uncleanly. Therefore, having userland manually remove mappings seems
560 * like a pointless exercise since they're going away anyway.
561 *
562 * One use case might be after addmap is allowed for normal users for SHM and
563 * gets used by drivers that the server doesn't need to care about. This seems
564 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100565 *
566 * \param inode device inode.
567 * \param file_priv DRM file private.
568 * \param cmd command.
569 * \param arg pointer to a struct drm_map structure.
570 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000571 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200572int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
573 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000574{
Eric Anholtc153f452007-09-03 12:06:45 +1000575 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100576 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000577 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000578 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000579
Daniel Vettere975eef2016-04-26 19:29:37 +0200580 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
581 drm_core_check_feature(dev, DRIVER_MODESET))
582 return -EINVAL;
583
Dave Airlie30e2fb12006-02-02 19:37:46 +1100584 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000585 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000586 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000587 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000588 r_list->map->flags & _DRM_REMOVABLE) {
589 map = r_list->map;
590 break;
591 }
592 }
593
594 /* List has wrapped around to the head pointer, or its empty we didn't
595 * find anything.
596 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000597 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100598 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000599 return -EINVAL;
600 }
601
Dave Airlie836cf042005-07-10 19:27:04 +1000602 /* Register and framebuffer maps are permanent */
603 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100604 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000605 return 0;
606 }
607
David Herrmann9fc5cde2014-08-29 12:12:28 +0200608 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000609
Dave Airlie30e2fb12006-02-02 19:37:46 +1100610 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000611
612 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/**
616 * Cleanup after an error on one of the addbufs() functions.
617 *
Dave Airlie836cf042005-07-10 19:27:04 +1000618 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * \param entry buffer entry where the error occurred.
620 *
621 * Frees any pages and buffers associated with the given entry.
622 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000623static void drm_cleanup_buf_error(struct drm_device * dev,
624 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 int i;
627
628 if (entry->seg_count) {
629 for (i = 0; i < entry->seg_count; i++) {
630 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100631 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700634 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 entry->seg_count = 0;
637 }
638
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 if (entry->buf_count) {
640 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700641 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700643 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 entry->buf_count = 0;
646 }
647}
648
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200649#if IS_ENABLED(CONFIG_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/**
Dave Airlied59431b2005-07-10 15:00:06 +1000651 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000653 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000654 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * After some sanity checks creates a drm_buf structure for each buffer and
658 * reallocates the buffer list of the same size order to accommodate the new
659 * buffers.
660 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200661int drm_legacy_addbufs_agp(struct drm_device *dev,
662 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Dave Airliecdd55a22007-07-11 16:32:08 +1000664 struct drm_device_dma *dma = dev->dma;
665 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000666 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000667 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 unsigned long offset;
669 unsigned long agp_offset;
670 int count;
671 int order;
672 int size;
673 int alignment;
674 int page_order;
675 int total;
676 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100677 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000678 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 if (!dma)
681 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dave Airlied59431b2005-07-10 15:00:06 +1000683 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200684 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 size = 1 << order;
686
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 alignment = (request->flags & _DRM_PAGE_ALIGN)
688 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
690 total = PAGE_SIZE << page_order;
691
692 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000693 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 DRM_DEBUG("count: %d\n", count);
696 DRM_DEBUG("order: %d\n", order);
697 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100698 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 DRM_DEBUG("alignment: %d\n", alignment);
700 DRM_DEBUG("page_order: %d\n", page_order);
701 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Dave Airlie54ba2f72007-02-10 12:07:47 +1100706 /* Make sure buffers are located in AGP memory that we own */
707 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000708 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100709 if ((agp_offset >= agp_entry->bound) &&
710 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
711 valid = 1;
712 break;
713 }
714 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000715 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100716 DRM_DEBUG("zone invalid\n");
717 return -EINVAL;
718 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100719 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100721 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EBUSY;
723 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100725 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Dave Airlie30e2fb12006-02-02 19:37:46 +1100727 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 if (entry->buf_count) {
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);
732 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100736 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EINVAL;
739 }
740
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400741 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100743 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -ENOMEM;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 entry->buf_size = size;
749 entry->page_order = page_order;
750
751 offset = 0;
752
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 while (entry->buf_count < count) {
754 buf = &entry->buflist[entry->buf_count];
755 buf->idx = dma->buf_count + entry->buf_count;
756 buf->total = alignment;
757 buf->order = order;
758 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 buf->bus_address = agp_offset + offset;
762 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 buf->waiting = 0;
765 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000766 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400769 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Set count correctly so we free the proper amount. */
772 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000773 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100774 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -ENOMEM;
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 offset += alignment;
782 entry->buf_count++;
783 byte_count += PAGE_SIZE << page_order;
784 }
785
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Eric Anholt9a298b22009-03-24 12:23:04 -0700788 temp_buflist = krealloc(dma->buflist,
789 (dma->buf_count + entry->buf_count) *
790 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100794 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -ENOMEM;
797 }
798 dma->buflist = temp_buflist;
799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
802 }
803
804 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100805 dma->seg_count += entry->seg_count;
806 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 dma->byte_count += byte_count;
808
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
810 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Dave Airlie30e2fb12006-02-02 19:37:46 +1100812 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Airlied59431b2005-07-10 15:00:06 +1000814 request->count = entry->buf_count;
815 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 dma->flags = _DRM_DMA_USE_AGP;
818
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return 0;
821}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200822EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200823#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824
David Herrmann9fc5cde2014-08-29 12:12:28 +0200825int drm_legacy_addbufs_pci(struct drm_device *dev,
826 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Dave Airliecdd55a22007-07-11 16:32:08 +1000828 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int count;
830 int order;
831 int size;
832 int total;
833 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000834 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100835 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000836 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int alignment;
838 unsigned long offset;
839 int i;
840 int byte_count;
841 int page_count;
842 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000843 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
846 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100847
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 if (!dma)
849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dave Airlied985c102006-01-02 21:32:48 +1100851 if (!capable(CAP_SYS_ADMIN))
852 return -EPERM;
853
Dave Airlied59431b2005-07-10 15:00:06 +1000854 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200855 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 size = 1 << order;
857
Daniel Vettera344a7e2011-10-26 00:54:41 +0200858 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
859 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
862 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dave Airlied59431b2005-07-10 15:00:06 +1000864 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
867 total = PAGE_SIZE << page_order;
868
Daniel Vetter2177a212013-12-16 11:21:06 +0100869 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100871 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -EBUSY;
873 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100875 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Dave Airlie30e2fb12006-02-02 19:37:46 +1100877 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100880 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -ENOMEM; /* May only call once for each order */
883 }
884
885 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100886 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EINVAL;
889 }
890
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400891 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100893 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -ENOMEM;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400898 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700900 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100901 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -ENOMEM;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* Keep the original pagelist until we know all the allocations
907 * have succeeded
908 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700909 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
910 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700912 kfree(entry->buflist);
913 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100914 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -ENOMEM;
917 }
918 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
920 DRM_DEBUG("pagelist: %d entries\n",
921 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 entry->page_order = page_order;
925 byte_count = 0;
926 page_count = 0;
927
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000929
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800930 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000931
Dave Airlieddf19b92006-03-19 18:56:12 +1100932 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* Set count correctly so we free the proper amount. */
934 entry->buf_count = count;
935 entry->seg_count = count;
936 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700937 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100938 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -ENOMEM;
941 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100942 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 for (i = 0; i < (1 << page_order); i++) {
944 DRM_DEBUG("page %d @ 0x%08lx\n",
945 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100946 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100948 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 for (offset = 0;
951 offset + size <= total && entry->buf_count < count;
952 offset += alignment, ++entry->buf_count) {
953 buf = &entry->buflist[entry->buf_count];
954 buf->idx = dma->buf_count + entry->buf_count;
955 buf->total = alignment;
956 buf->order = order;
957 buf->used = 0;
958 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100959 buf->address = (void *)(dmah->vaddr + offset);
960 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 buf->waiting = 0;
963 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000964 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400967 buf->dev_private = kzalloc(buf->dev_priv_size,
968 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000969 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Set count correctly so we free the proper amount. */
971 entry->buf_count = count;
972 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000973 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700974 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100975 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ENOMEM;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000980 DRM_DEBUG("buffer %d @ %p\n",
981 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 byte_count += PAGE_SIZE << page_order;
984 }
985
Eric Anholt9a298b22009-03-24 12:23:04 -0700986 temp_buflist = krealloc(dma->buflist,
987 (dma->buf_count + entry->buf_count) *
988 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!temp_buflist) {
990 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700992 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100993 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return -ENOMEM;
996 }
997 dma->buflist = temp_buflist;
998
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1001 }
1002
Thomas Weber88393162010-03-16 11:47:56 +01001003 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * with the new one.
1005 */
1006 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001007 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 dma->pagelist = temp_pagelist;
1010
1011 dma->buf_count += entry->buf_count;
1012 dma->seg_count += entry->seg_count;
1013 dma->page_count += entry->seg_count << page_order;
1014 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1015
Dave Airlie30e2fb12006-02-02 19:37:46 +11001016 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Dave Airlied59431b2005-07-10 15:00:06 +10001018 request->count = entry->buf_count;
1019 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
George Sapountzis3417f332006-10-24 12:03:04 -07001021 if (request->flags & _DRM_PCI_BUFFER_RO)
1022 dma->flags = _DRM_DMA_USE_PCI_RO;
1023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return 0;
1026
1027}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001028EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
David Herrmann9fc5cde2014-08-29 12:12:28 +02001030static int drm_legacy_addbufs_sg(struct drm_device *dev,
1031 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Dave Airliecdd55a22007-07-11 16:32:08 +10001033 struct drm_device_dma *dma = dev->dma;
1034 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001035 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned long offset;
1037 unsigned long agp_offset;
1038 int count;
1039 int order;
1040 int size;
1041 int alignment;
1042 int page_order;
1043 int total;
1044 int byte_count;
1045 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001046 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 if (!drm_core_check_feature(dev, DRIVER_SG))
1049 return -EINVAL;
1050
1051 if (!dma)
1052 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Dave Airlied985c102006-01-02 21:32:48 +11001054 if (!capable(CAP_SYS_ADMIN))
1055 return -EPERM;
1056
Dave Airlied59431b2005-07-10 15:00:06 +10001057 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001058 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 size = 1 << order;
1060
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 alignment = (request->flags & _DRM_PAGE_ALIGN)
1062 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1064 total = PAGE_SIZE << page_order;
1065
1066 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001067 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 DRM_DEBUG("count: %d\n", count);
1070 DRM_DEBUG("order: %d\n", order);
1071 DRM_DEBUG("size: %d\n", size);
1072 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1073 DRM_DEBUG("alignment: %d\n", alignment);
1074 DRM_DEBUG("page_order: %d\n", page_order);
1075 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1078 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Daniel Vetter2177a212013-12-16 11:21:06 +01001080 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001082 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -EBUSY;
1084 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001086 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Dave Airlie30e2fb12006-02-02 19:37:46 +11001088 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001091 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 atomic_dec(&dev->buf_alloc);
1093 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
1096 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001097 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return -EINVAL;
1100 }
1101
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001102 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001103 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001105 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -ENOMEM;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 entry->buf_size = size;
1111 entry->page_order = page_order;
1112
1113 offset = 0;
1114
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 while (entry->buf_count < count) {
1116 buf = &entry->buflist[entry->buf_count];
1117 buf->idx = dma->buf_count + entry->buf_count;
1118 buf->total = alignment;
1119 buf->order = order;
1120 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001125 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 buf->waiting = 0;
1128 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001129 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001132 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* Set count correctly so we free the proper amount. */
1135 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001136 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001137 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return -ENOMEM;
1140 }
1141
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 offset += alignment;
1145 entry->buf_count++;
1146 byte_count += PAGE_SIZE << page_order;
1147 }
1148
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Eric Anholt9a298b22009-03-24 12:23:04 -07001151 temp_buflist = krealloc(dma->buflist,
1152 (dma->buf_count + entry->buf_count) *
1153 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001157 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -ENOMEM;
1160 }
1161 dma->buflist = temp_buflist;
1162
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1165 }
1166
1167 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001168 dma->seg_count += entry->seg_count;
1169 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 dma->byte_count += byte_count;
1171
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001172 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1173 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Dave Airlie30e2fb12006-02-02 19:37:46 +11001175 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Dave Airlied59431b2005-07-10 15:00:06 +10001177 request->count = entry->buf_count;
1178 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 dma->flags = _DRM_DMA_USE_SG;
1181
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001182 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
1184}
1185
1186/**
1187 * Add buffers for DMA transfers (ioctl).
1188 *
1189 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001190 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001192 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * \return zero on success or a negative number on failure.
1194 *
1195 * According with the memory type specified in drm_buf_desc::flags and the
1196 * build options, it dispatches the call either to addbufs_agp(),
1197 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1198 * PCI memory respectively.
1199 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001200int drm_legacy_addbufs(struct drm_device *dev, void *data,
1201 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Eric Anholtc153f452007-09-03 12:06:45 +10001203 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001204 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001205
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001206 if (drm_core_check_feature(dev, DRIVER_MODESET))
1207 return -EINVAL;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1210 return -EINVAL;
1211
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001212#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001213 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001214 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 else
1216#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001217 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001218 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001219 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001220 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001222 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001223
Dave Airlied59431b2005-07-10 15:00:06 +10001224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/**
1228 * Get information about the buffer mappings.
1229 *
1230 * This was originally mean for debugging purposes, or by a sophisticated
1231 * client library to determine how best to use the available buffers (e.g.,
1232 * large buffers can be used for image transfer).
1233 *
1234 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001235 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * \param cmd command.
1237 * \param arg pointer to a drm_buf_info structure.
1238 * \return zero on success or a negative number on failure.
1239 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001240 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * lock, preventing of allocating more buffers after this call. Information
1242 * about each requested buffer is then copied into user space.
1243 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001244int drm_legacy_infobufs(struct drm_device *dev, void *data,
1245 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Dave Airliecdd55a22007-07-11 16:32:08 +10001247 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001248 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int i;
1250 int count;
1251
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001252 if (drm_core_check_feature(dev, DRIVER_MODESET))
1253 return -EINVAL;
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1256 return -EINVAL;
1257
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001258 if (!dma)
1259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Daniel Vetter2177a212013-12-16 11:21:06 +01001261 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001262 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001263 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EBUSY;
1265 }
1266 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001267 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001269 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1270 if (dma->bufs[i].buf_count)
1271 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001274 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Eric Anholtc153f452007-09-03 12:06:45 +10001276 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001277 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1278 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001279 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001280 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001281 struct drm_buf_entry *from = &dma->bufs[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001282 if (copy_to_user(&to->count,
1283 &from->buf_count,
1284 sizeof(from->buf_count)) ||
1285 copy_to_user(&to->size,
1286 &from->buf_size,
1287 sizeof(from->buf_size)) ||
1288 copy_to_user(&to->low_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001289 &from->low_mark,
1290 sizeof(from->low_mark)) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001291 copy_to_user(&to->high_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001292 &from->high_mark,
1293 sizeof(from->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EFAULT;
1295
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296 DRM_DEBUG("%d %d %d %d %d\n",
1297 i,
1298 dma->bufs[i].buf_count,
1299 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001300 dma->bufs[i].low_mark,
1301 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ++count;
1303 }
1304 }
1305 }
Eric Anholtc153f452007-09-03 12:06:45 +10001306 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 return 0;
1309}
1310
1311/**
1312 * Specifies a low and high water mark for buffer allocation
1313 *
1314 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001315 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * \param cmd command.
1317 * \param arg a pointer to a drm_buf_desc structure.
1318 * \return zero on success or a negative number on failure.
1319 *
1320 * Verifies that the size order is bounded between the admissible orders and
1321 * updates the respective drm_device_dma::bufs entry low and high water mark.
1322 *
1323 * \note This ioctl is deprecated and mostly never used.
1324 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001325int drm_legacy_markbufs(struct drm_device *dev, void *data,
1326 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Dave Airliecdd55a22007-07-11 16:32:08 +10001328 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001329 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001331 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001333 if (drm_core_check_feature(dev, DRIVER_MODESET))
1334 return -EINVAL;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1337 return -EINVAL;
1338
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001339 if (!dma)
1340 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001342 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001343 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001344 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001345 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 entry = &dma->bufs[order];
1348
Eric Anholtc153f452007-09-03 12:06:45 +10001349 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001351 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EINVAL;
1353
David Herrmannb008c0f2014-07-23 17:26:36 +02001354 entry->low_mark = request->low_mark;
1355 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 return 0;
1358}
1359
1360/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001361 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 *
1363 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001364 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * \param cmd command.
1366 * \param arg pointer to a drm_buf_free structure.
1367 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * Calls free_buffer() for each used buffer.
1370 * This function is primarily used for debugging.
1371 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001372int drm_legacy_freebufs(struct drm_device *dev, void *data,
1373 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Dave Airliecdd55a22007-07-11 16:32:08 +10001375 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001376 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int i;
1378 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001379 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001381 if (drm_core_check_feature(dev, DRIVER_MODESET))
1382 return -EINVAL;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1385 return -EINVAL;
1386
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 if (!dma)
1388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Eric Anholtc153f452007-09-03 12:06:45 +10001390 DRM_DEBUG("%d\n", request->count);
1391 for (i = 0; i < request->count; i++) {
1392 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 if (idx < 0 || idx >= dma->buf_count) {
1395 DRM_ERROR("Index %d (of %d max)\n",
1396 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EINVAL;
1398 }
1399 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001400 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001402 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -EINVAL;
1404 }
Daniel Vettera2661622014-09-11 07:41:51 +02001405 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
1408 return 0;
1409}
1410
1411/**
1412 * Maps all of the DMA buffers into client-virtual space (ioctl).
1413 *
1414 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001415 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * \param cmd command.
1417 * \param arg pointer to a drm_buf_map structure.
1418 * \return zero on success or a negative number on failure.
1419 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001420 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1421 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001422 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1423 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001425int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1426 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Dave Airliecdd55a22007-07-11 16:32:08 +10001428 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int retcode = 0;
1430 const int zero = 0;
1431 unsigned long virtual;
1432 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001433 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int i;
1435
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001436 if (drm_core_check_feature(dev, DRIVER_MODESET))
1437 return -EINVAL;
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1440 return -EINVAL;
1441
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001442 if (!dma)
1443 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Daniel Vetter2177a212013-12-16 11:21:06 +01001445 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001446 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001447 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EBUSY;
1449 }
1450 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001451 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Eric Anholtc153f452007-09-03 12:06:45 +10001453 if (request->count >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001454 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001456 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001457 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001458 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001460 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 retcode = -EINVAL;
1462 goto done;
1463 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001464 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001465 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001466 MAP_SHARED,
1467 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001469 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001470 PROT_READ | PROT_WRITE,
1471 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001473 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 /* Real error */
1475 retcode = (signed long)virtual;
1476 goto done;
1477 }
Eric Anholtc153f452007-09-03 12:06:45 +10001478 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001480 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001481 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001482 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001483 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 retcode = -EFAULT;
1485 goto done;
1486 }
Eric Anholtc153f452007-09-03 12:06:45 +10001487 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001488 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001489 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 retcode = -EFAULT;
1491 goto done;
1492 }
Eric Anholtc153f452007-09-03 12:06:45 +10001493 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 retcode = -EFAULT;
1496 goto done;
1497 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001498 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001499 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001500 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 retcode = -EFAULT;
1502 goto done;
1503 }
1504 }
1505 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001506 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001507 request->count = dma->buf_count;
1508 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 return retcode;
1511}
1512
David Herrmann9fc5cde2014-08-29 12:12:28 +02001513int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001514 struct drm_file *file_priv)
1515{
1516 if (drm_core_check_feature(dev, DRIVER_MODESET))
1517 return -EINVAL;
1518
1519 if (dev->driver->dma_ioctl)
1520 return dev->driver->dma_ioctl(dev, data, file_priv);
1521 else
1522 return -EINVAL;
1523}
1524
David Herrmann9fc5cde2014-08-29 12:12:28 +02001525struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001526{
1527 struct drm_map_list *entry;
1528
1529 list_for_each_entry(entry, &dev->maplist, head) {
1530 if (entry->map && entry->map->type == _DRM_SHM &&
1531 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1532 return entry->map;
1533 }
1534 }
1535 return NULL;
1536}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001537EXPORT_SYMBOL(drm_legacy_getsarea);