blob: c3a12cd8bd0de963e410d921c25e0bb7eeba46c9 [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 ||
Daniel Vetter95c081c2016-06-21 10:54:12 +020054 entry->master != dev->master)
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110055 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 */
Daniel Vetter95c081c2016-06-21 10:54:12 +0200248 if (dev->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 }
Daniel Vetter95c081c2016-06-21 10:54:12 +0200253 dev->sigdata.lock = dev->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))
Daniel Vetter95c081c2016-06-21 10:54:12 +0200359 list->master = dev->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
Daniel Vetter40647e42016-04-27 09:20:18 +0200545void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000546{
Daniel Vetter40647e42016-04-27 09:20:18 +0200547 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
548 drm_core_check_feature(dev, DRIVER_MODESET))
549 return;
Dave Airlie836cf042005-07-10 19:27:04 +1000550
Dave Airlie30e2fb12006-02-02 19:37:46 +1100551 mutex_lock(&dev->struct_mutex);
Daniel Vetter40647e42016-04-27 09:20:18 +0200552 drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100553 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000554}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200555EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000556
Daniel Vetter40647e42016-04-27 09:20:18 +0200557void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
558{
559 struct drm_map_list *r_list, *list_temp;
560
561 if (drm_core_check_feature(dev, DRIVER_MODESET))
562 return;
563
564 mutex_lock(&dev->struct_mutex);
565 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
566 if (r_list->master == master) {
567 drm_legacy_rmmap_locked(dev, r_list->map);
568 r_list = NULL;
569 }
570 }
571 mutex_unlock(&dev->struct_mutex);
572}
573
Dave Airlie836cf042005-07-10 19:27:04 +1000574/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
575 * the last close of the device, and this is necessary for cleanup when things
576 * exit uncleanly. Therefore, having userland manually remove mappings seems
577 * like a pointless exercise since they're going away anyway.
578 *
579 * One use case might be after addmap is allowed for normal users for SHM and
580 * gets used by drivers that the server doesn't need to care about. This seems
581 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100582 *
583 * \param inode device inode.
584 * \param file_priv DRM file private.
585 * \param cmd command.
586 * \param arg pointer to a struct drm_map structure.
587 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000588 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200589int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
590 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000591{
Eric Anholtc153f452007-09-03 12:06:45 +1000592 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100593 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000594 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000595 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000596
Daniel Vettere975eef2016-04-26 19:29:37 +0200597 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
598 drm_core_check_feature(dev, DRIVER_MODESET))
599 return -EINVAL;
600
Dave Airlie30e2fb12006-02-02 19:37:46 +1100601 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000602 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000603 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000604 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000605 r_list->map->flags & _DRM_REMOVABLE) {
606 map = r_list->map;
607 break;
608 }
609 }
610
611 /* List has wrapped around to the head pointer, or its empty we didn't
612 * find anything.
613 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000614 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100615 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000616 return -EINVAL;
617 }
618
Dave Airlie836cf042005-07-10 19:27:04 +1000619 /* Register and framebuffer maps are permanent */
620 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100621 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000622 return 0;
623 }
624
David Herrmann9fc5cde2014-08-29 12:12:28 +0200625 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000626
Dave Airlie30e2fb12006-02-02 19:37:46 +1100627 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000628
629 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/**
633 * Cleanup after an error on one of the addbufs() functions.
634 *
Dave Airlie836cf042005-07-10 19:27:04 +1000635 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * \param entry buffer entry where the error occurred.
637 *
638 * Frees any pages and buffers associated with the given entry.
639 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000640static void drm_cleanup_buf_error(struct drm_device * dev,
641 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int i;
644
645 if (entry->seg_count) {
646 for (i = 0; i < entry->seg_count; i++) {
647 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100648 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700651 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 entry->seg_count = 0;
654 }
655
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 if (entry->buf_count) {
657 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700658 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700660 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 entry->buf_count = 0;
663 }
664}
665
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200666#if IS_ENABLED(CONFIG_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
Dave Airlied59431b2005-07-10 15:00:06 +1000668 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000670 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000671 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * After some sanity checks creates a drm_buf structure for each buffer and
675 * reallocates the buffer list of the same size order to accommodate the new
676 * buffers.
677 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200678int drm_legacy_addbufs_agp(struct drm_device *dev,
679 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Dave Airliecdd55a22007-07-11 16:32:08 +1000681 struct drm_device_dma *dma = dev->dma;
682 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000683 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000684 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 unsigned long offset;
686 unsigned long agp_offset;
687 int count;
688 int order;
689 int size;
690 int alignment;
691 int page_order;
692 int total;
693 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100694 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000695 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 if (!dma)
698 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Dave Airlied59431b2005-07-10 15:00:06 +1000700 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200701 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 size = 1 << order;
703
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 alignment = (request->flags & _DRM_PAGE_ALIGN)
705 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
707 total = PAGE_SIZE << page_order;
708
709 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000710 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 DRM_DEBUG("count: %d\n", count);
713 DRM_DEBUG("order: %d\n", order);
714 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100715 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 DRM_DEBUG("alignment: %d\n", alignment);
717 DRM_DEBUG("page_order: %d\n", page_order);
718 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Dave Airlie54ba2f72007-02-10 12:07:47 +1100723 /* Make sure buffers are located in AGP memory that we own */
724 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000725 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100726 if ((agp_offset >= agp_entry->bound) &&
727 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
728 valid = 1;
729 break;
730 }
731 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000732 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100733 DRM_DEBUG("zone invalid\n");
734 return -EINVAL;
735 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100736 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100738 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EBUSY;
740 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100742 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Dave Airlie30e2fb12006-02-02 19:37:46 +1100744 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100747 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 atomic_dec(&dev->buf_alloc);
749 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
752 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100753 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000754 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EINVAL;
756 }
757
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400758 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100760 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -ENOMEM;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 entry->buf_size = size;
766 entry->page_order = page_order;
767
768 offset = 0;
769
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 while (entry->buf_count < count) {
771 buf = &entry->buflist[entry->buf_count];
772 buf->idx = dma->buf_count + entry->buf_count;
773 buf->total = alignment;
774 buf->order = order;
775 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 buf->bus_address = agp_offset + offset;
779 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 buf->waiting = 0;
782 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000783 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400786 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Set count correctly so we free the proper amount. */
789 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100791 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -ENOMEM;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 offset += alignment;
799 entry->buf_count++;
800 byte_count += PAGE_SIZE << page_order;
801 }
802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Eric Anholt9a298b22009-03-24 12:23:04 -0700805 temp_buflist = krealloc(dma->buflist,
806 (dma->buf_count + entry->buf_count) *
807 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100811 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -ENOMEM;
814 }
815 dma->buflist = temp_buflist;
816
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
819 }
820
821 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100822 dma->seg_count += entry->seg_count;
823 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 dma->byte_count += byte_count;
825
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
827 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Airlie30e2fb12006-02-02 19:37:46 +1100829 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Dave Airlied59431b2005-07-10 15:00:06 +1000831 request->count = entry->buf_count;
832 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 dma->flags = _DRM_DMA_USE_AGP;
835
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
838}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200839EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200840#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841
David Herrmann9fc5cde2014-08-29 12:12:28 +0200842int drm_legacy_addbufs_pci(struct drm_device *dev,
843 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Dave Airliecdd55a22007-07-11 16:32:08 +1000845 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int count;
847 int order;
848 int size;
849 int total;
850 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000851 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100852 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000853 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int alignment;
855 unsigned long offset;
856 int i;
857 int byte_count;
858 int page_count;
859 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000860 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
863 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 if (!dma)
866 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Dave Airlied985c102006-01-02 21:32:48 +1100868 if (!capable(CAP_SYS_ADMIN))
869 return -EPERM;
870
Dave Airlied59431b2005-07-10 15:00:06 +1000871 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200872 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 size = 1 << order;
874
Daniel Vettera344a7e2011-10-26 00:54:41 +0200875 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
876 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Dave Airlied59431b2005-07-10 15:00:06 +1000881 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
884 total = PAGE_SIZE << page_order;
885
Daniel Vetter2177a212013-12-16 11:21:06 +0100886 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100888 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -EBUSY;
890 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100892 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Dave Airlie30e2fb12006-02-02 19:37:46 +1100894 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100897 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -ENOMEM; /* May only call once for each order */
900 }
901
902 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100903 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906 }
907
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400908 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100910 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -ENOMEM;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400915 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700917 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100918 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -ENOMEM;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Keep the original pagelist until we know all the allocations
924 * have succeeded
925 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700926 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
927 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700929 kfree(entry->buflist);
930 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100931 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000932 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return -ENOMEM;
934 }
935 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
937 DRM_DEBUG("pagelist: %d entries\n",
938 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 entry->page_order = page_order;
942 byte_count = 0;
943 page_count = 0;
944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000946
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800947 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000948
Dave Airlieddf19b92006-03-19 18:56:12 +1100949 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* Set count correctly so we free the proper amount. */
951 entry->buf_count = count;
952 entry->seg_count = count;
953 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700954 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100955 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -ENOMEM;
958 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100959 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960 for (i = 0; i < (1 << page_order); i++) {
961 DRM_DEBUG("page %d @ 0x%08lx\n",
962 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100963 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100965 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 for (offset = 0;
968 offset + size <= total && entry->buf_count < count;
969 offset += alignment, ++entry->buf_count) {
970 buf = &entry->buflist[entry->buf_count];
971 buf->idx = dma->buf_count + entry->buf_count;
972 buf->total = alignment;
973 buf->order = order;
974 buf->used = 0;
975 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100976 buf->address = (void *)(dmah->vaddr + offset);
977 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000978 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 buf->waiting = 0;
980 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000981 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400984 buf->dev_private = kzalloc(buf->dev_priv_size,
985 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* Set count correctly so we free the proper amount. */
988 entry->buf_count = count;
989 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700991 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100992 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -ENOMEM;
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 DRM_DEBUG("buffer %d @ %p\n",
998 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 byte_count += PAGE_SIZE << page_order;
1001 }
1002
Eric Anholt9a298b22009-03-24 12:23:04 -07001003 temp_buflist = krealloc(dma->buflist,
1004 (dma->buf_count + entry->buf_count) *
1005 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!temp_buflist) {
1007 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001009 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001010 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return -ENOMEM;
1013 }
1014 dma->buflist = temp_buflist;
1015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1018 }
1019
Thomas Weber88393162010-03-16 11:47:56 +01001020 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * with the new one.
1022 */
1023 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001024 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 dma->pagelist = temp_pagelist;
1027
1028 dma->buf_count += entry->buf_count;
1029 dma->seg_count += entry->seg_count;
1030 dma->page_count += entry->seg_count << page_order;
1031 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1032
Dave Airlie30e2fb12006-02-02 19:37:46 +11001033 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Dave Airlied59431b2005-07-10 15:00:06 +10001035 request->count = entry->buf_count;
1036 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
George Sapountzis3417f332006-10-24 12:03:04 -07001038 if (request->flags & _DRM_PCI_BUFFER_RO)
1039 dma->flags = _DRM_DMA_USE_PCI_RO;
1040
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 0;
1043
1044}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001045EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
David Herrmann9fc5cde2014-08-29 12:12:28 +02001047static int drm_legacy_addbufs_sg(struct drm_device *dev,
1048 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Dave Airliecdd55a22007-07-11 16:32:08 +10001050 struct drm_device_dma *dma = dev->dma;
1051 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001052 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 unsigned long offset;
1054 unsigned long agp_offset;
1055 int count;
1056 int order;
1057 int size;
1058 int alignment;
1059 int page_order;
1060 int total;
1061 int byte_count;
1062 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001063 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 if (!drm_core_check_feature(dev, DRIVER_SG))
1066 return -EINVAL;
1067
1068 if (!dma)
1069 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Dave Airlied985c102006-01-02 21:32:48 +11001071 if (!capable(CAP_SYS_ADMIN))
1072 return -EPERM;
1073
Dave Airlied59431b2005-07-10 15:00:06 +10001074 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001075 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 size = 1 << order;
1077
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 alignment = (request->flags & _DRM_PAGE_ALIGN)
1079 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1081 total = PAGE_SIZE << page_order;
1082
1083 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001084 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 DRM_DEBUG("count: %d\n", count);
1087 DRM_DEBUG("order: %d\n", order);
1088 DRM_DEBUG("size: %d\n", size);
1089 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1090 DRM_DEBUG("alignment: %d\n", alignment);
1091 DRM_DEBUG("page_order: %d\n", page_order);
1092 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1095 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Daniel Vetter2177a212013-12-16 11:21:06 +01001097 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001099 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -EBUSY;
1101 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001103 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Dave Airlie30e2fb12006-02-02 19:37:46 +11001105 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001107 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001108 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 atomic_dec(&dev->buf_alloc);
1110 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
1113 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001114 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return -EINVAL;
1117 }
1118
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001119 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001120 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001122 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -ENOMEM;
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 entry->buf_size = size;
1128 entry->page_order = page_order;
1129
1130 offset = 0;
1131
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 while (entry->buf_count < count) {
1133 buf = &entry->buflist[entry->buf_count];
1134 buf->idx = dma->buf_count + entry->buf_count;
1135 buf->total = alignment;
1136 buf->order = order;
1137 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001142 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 buf->waiting = 0;
1145 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001146 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001149 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* Set count correctly so we free the proper amount. */
1152 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001153 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001154 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -ENOMEM;
1157 }
1158
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 offset += alignment;
1162 entry->buf_count++;
1163 byte_count += PAGE_SIZE << page_order;
1164 }
1165
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001166 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Eric Anholt9a298b22009-03-24 12:23:04 -07001168 temp_buflist = krealloc(dma->buflist,
1169 (dma->buf_count + entry->buf_count) *
1170 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001174 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -ENOMEM;
1177 }
1178 dma->buflist = temp_buflist;
1179
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1182 }
1183
1184 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001185 dma->seg_count += entry->seg_count;
1186 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dma->byte_count += byte_count;
1188
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001189 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1190 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dave Airlie30e2fb12006-02-02 19:37:46 +11001192 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dave Airlied59431b2005-07-10 15:00:06 +10001194 request->count = entry->buf_count;
1195 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 dma->flags = _DRM_DMA_USE_SG;
1198
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001199 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203/**
1204 * Add buffers for DMA transfers (ioctl).
1205 *
1206 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001207 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001209 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * \return zero on success or a negative number on failure.
1211 *
1212 * According with the memory type specified in drm_buf_desc::flags and the
1213 * build options, it dispatches the call either to addbufs_agp(),
1214 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1215 * PCI memory respectively.
1216 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001217int drm_legacy_addbufs(struct drm_device *dev, void *data,
1218 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Eric Anholtc153f452007-09-03 12:06:45 +10001220 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001221 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001222
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001223 if (drm_core_check_feature(dev, DRIVER_MODESET))
1224 return -EINVAL;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1227 return -EINVAL;
1228
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001229#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001230 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001231 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 else
1233#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001234 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001235 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001236 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001237 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001239 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001240
Dave Airlied59431b2005-07-10 15:00:06 +10001241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244/**
1245 * Get information about the buffer mappings.
1246 *
1247 * This was originally mean for debugging purposes, or by a sophisticated
1248 * client library to determine how best to use the available buffers (e.g.,
1249 * large buffers can be used for image transfer).
1250 *
1251 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001252 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * \param cmd command.
1254 * \param arg pointer to a drm_buf_info structure.
1255 * \return zero on success or a negative number on failure.
1256 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001257 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * lock, preventing of allocating more buffers after this call. Information
1259 * about each requested buffer is then copied into user space.
1260 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001261int drm_legacy_infobufs(struct drm_device *dev, void *data,
1262 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Dave Airliecdd55a22007-07-11 16:32:08 +10001264 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001265 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int i;
1267 int count;
1268
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001269 if (drm_core_check_feature(dev, DRIVER_MODESET))
1270 return -EINVAL;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1273 return -EINVAL;
1274
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 if (!dma)
1276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Daniel Vetter2177a212013-12-16 11:21:06 +01001278 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001279 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001280 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EBUSY;
1282 }
1283 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001284 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001286 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1287 if (dma->bufs[i].buf_count)
1288 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001291 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Eric Anholtc153f452007-09-03 12:06:45 +10001293 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1295 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001296 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001297 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001298 struct drm_buf_entry *from = &dma->bufs[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001299 if (copy_to_user(&to->count,
1300 &from->buf_count,
1301 sizeof(from->buf_count)) ||
1302 copy_to_user(&to->size,
1303 &from->buf_size,
1304 sizeof(from->buf_size)) ||
1305 copy_to_user(&to->low_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001306 &from->low_mark,
1307 sizeof(from->low_mark)) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001308 copy_to_user(&to->high_mark,
David Herrmannb008c0f2014-07-23 17:26:36 +02001309 &from->high_mark,
1310 sizeof(from->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EFAULT;
1312
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001313 DRM_DEBUG("%d %d %d %d %d\n",
1314 i,
1315 dma->bufs[i].buf_count,
1316 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001317 dma->bufs[i].low_mark,
1318 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ++count;
1320 }
1321 }
1322 }
Eric Anholtc153f452007-09-03 12:06:45 +10001323 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 return 0;
1326}
1327
1328/**
1329 * Specifies a low and high water mark for buffer allocation
1330 *
1331 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001332 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * \param cmd command.
1334 * \param arg a pointer to a drm_buf_desc structure.
1335 * \return zero on success or a negative number on failure.
1336 *
1337 * Verifies that the size order is bounded between the admissible orders and
1338 * updates the respective drm_device_dma::bufs entry low and high water mark.
1339 *
1340 * \note This ioctl is deprecated and mostly never used.
1341 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001342int drm_legacy_markbufs(struct drm_device *dev, void *data,
1343 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Dave Airliecdd55a22007-07-11 16:32:08 +10001345 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001346 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001348 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001350 if (drm_core_check_feature(dev, DRIVER_MODESET))
1351 return -EINVAL;
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1354 return -EINVAL;
1355
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356 if (!dma)
1357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001359 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001360 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001361 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 entry = &dma->bufs[order];
1365
Eric Anholtc153f452007-09-03 12:06:45 +10001366 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001368 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EINVAL;
1370
David Herrmannb008c0f2014-07-23 17:26:36 +02001371 entry->low_mark = request->low_mark;
1372 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 return 0;
1375}
1376
1377/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 *
1380 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001381 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * \param cmd command.
1383 * \param arg pointer to a drm_buf_free structure.
1384 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001385 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * Calls free_buffer() for each used buffer.
1387 * This function is primarily used for debugging.
1388 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001389int drm_legacy_freebufs(struct drm_device *dev, void *data,
1390 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Dave Airliecdd55a22007-07-11 16:32:08 +10001392 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001393 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int i;
1395 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001396 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001398 if (drm_core_check_feature(dev, DRIVER_MODESET))
1399 return -EINVAL;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1402 return -EINVAL;
1403
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404 if (!dma)
1405 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Eric Anholtc153f452007-09-03 12:06:45 +10001407 DRM_DEBUG("%d\n", request->count);
1408 for (i = 0; i < request->count; i++) {
1409 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001411 if (idx < 0 || idx >= dma->buf_count) {
1412 DRM_ERROR("Index %d (of %d max)\n",
1413 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EINVAL;
1415 }
1416 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001417 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001418 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001419 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EINVAL;
1421 }
Daniel Vettera2661622014-09-11 07:41:51 +02001422 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
1425 return 0;
1426}
1427
1428/**
1429 * Maps all of the DMA buffers into client-virtual space (ioctl).
1430 *
1431 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001432 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * \param cmd command.
1434 * \param arg pointer to a drm_buf_map structure.
1435 * \return zero on success or a negative number on failure.
1436 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001437 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1438 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001439 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1440 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001442int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1443 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Dave Airliecdd55a22007-07-11 16:32:08 +10001445 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int retcode = 0;
1447 const int zero = 0;
1448 unsigned long virtual;
1449 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001450 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int i;
1452
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001453 if (drm_core_check_feature(dev, DRIVER_MODESET))
1454 return -EINVAL;
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1457 return -EINVAL;
1458
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459 if (!dma)
1460 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Daniel Vetter2177a212013-12-16 11:21:06 +01001462 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001463 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001464 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -EBUSY;
1466 }
1467 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001468 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Eric Anholtc153f452007-09-03 12:06:45 +10001470 if (request->count >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001471 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001472 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001473 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001474 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001475 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001477 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 retcode = -EINVAL;
1479 goto done;
1480 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001481 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001482 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001483 MAP_SHARED,
1484 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001486 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001487 PROT_READ | PROT_WRITE,
1488 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001490 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /* Real error */
1492 retcode = (signed long)virtual;
1493 goto done;
1494 }
Eric Anholtc153f452007-09-03 12:06:45 +10001495 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001498 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001499 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001500 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 retcode = -EFAULT;
1502 goto done;
1503 }
Eric Anholtc153f452007-09-03 12:06:45 +10001504 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001506 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 retcode = -EFAULT;
1508 goto done;
1509 }
Eric Anholtc153f452007-09-03 12:06:45 +10001510 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001511 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 retcode = -EFAULT;
1513 goto done;
1514 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001516 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001517 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 retcode = -EFAULT;
1519 goto done;
1520 }
1521 }
1522 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001523 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001524 request->count = dma->buf_count;
1525 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 return retcode;
1528}
1529
David Herrmann9fc5cde2014-08-29 12:12:28 +02001530int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001531 struct drm_file *file_priv)
1532{
1533 if (drm_core_check_feature(dev, DRIVER_MODESET))
1534 return -EINVAL;
1535
1536 if (dev->driver->dma_ioctl)
1537 return dev->driver->dma_ioctl(dev, data, file_priv);
1538 else
1539 return -EINVAL;
1540}
1541
David Herrmann9fc5cde2014-08-29 12:12:28 +02001542struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001543{
1544 struct drm_map_list *entry;
1545
1546 list_for_each_entry(entry, &dev->maplist, head) {
1547 if (entry->map && entry->map->type == _DRM_SHM &&
1548 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1549 return entry->map;
1550 }
1551 }
1552 return NULL;
1553}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001554EXPORT_SYMBOL(drm_legacy_getsarea);