blob: d7d10cabb9bbfd15540f8fe04edb0bdd9fc6558d [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
Gustavo A. R. Silvaa3780502018-10-16 11:55:49 +020039#include <linux/nospec.h>
40
Dave Airlie55910512007-07-11 16:53:40 +100041static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110042 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100043{
Dave Airlie55910512007-07-11 16:53:40 +100044 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100045 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110046 /*
47 * Because the kernel-userspace ABI is fixed at a 32-bit offset
Tormod Volden66aa6962011-05-30 19:45:43 +000048 * while PCI resources may live above that, we only compare the
49 * lower 32 bits of the map offset for maps of type
50 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
51 * It is assumed that if a driver have more than one resource
52 * of each type, the lower 32 bits are different.
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110053 */
54 if (!entry->map ||
55 map->type != entry->map->type ||
Daniel Vetter95c081c2016-06-21 10:54:12 +020056 entry->master != dev->master)
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110057 continue;
58 switch (map->type) {
59 case _DRM_SHM:
60 if (map->flags != _DRM_CONTAINS_LOCK)
61 break;
Tormod Volden66aa6962011-05-30 19:45:43 +000062 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110063 case _DRM_REGISTERS:
64 case _DRM_FRAME_BUFFER:
Tormod Volden66aa6962011-05-30 19:45:43 +000065 if ((entry->map->offset & 0xffffffff) ==
66 (map->offset & 0xffffffff))
67 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110068 default: /* Make gcc happy */
69 ;
Dave Airlie836cf042005-07-10 19:27:04 +100070 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110071 if (entry->map->offset == map->offset)
72 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100073 }
74
75 return NULL;
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Dave Airliee0be4282007-07-12 10:26:44 +100078static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080079 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100080{
David Millerf1a2a9b2009-02-18 15:41:02 -080081 int use_hashed_handle, shift;
82 unsigned long add;
83
Dave Airliec2604ce2006-08-12 16:03:26 +100084#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100085 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
86#elif (BITS_PER_LONG == 32)
87 use_hashed_handle = hashed_handle;
88#else
89#error Unsupported long size. Neither 64 nor 32 bits.
90#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100091
Thomas Hellstrome08870c2006-09-22 04:18:37 +100092 if (!use_hashed_handle) {
93 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110094 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100095 ret = drm_ht_insert_item(&dev->map_hash, hash);
96 if (ret != -EINVAL)
97 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +100098 }
David Millerf1a2a9b2009-02-18 15:41:02 -080099
100 shift = 0;
101 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
102 if (shm && (SHMLBA > PAGE_SIZE)) {
103 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
104
105 /* For shared memory, we have to preserve the SHMLBA
106 * bits of the eventual vma->vm_pgoff value during
107 * mmap(). Otherwise we run into cache aliasing problems
108 * on some platforms. On these platforms, the pgoff of
109 * a mmap() request is used to pick a suitable virtual
110 * address for the mmap() region such that it will not
111 * cause cache aliasing problems.
112 *
113 * Therefore, make sure the SHMLBA relevant bits of the
114 * hash value we use are equal to those in the original
115 * kernel virtual address.
116 */
117 shift = bits;
118 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
119 }
120
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000121 return drm_ht_just_insert_please(&dev->map_hash, hash,
122 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800123 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000124}
Dave Airlie9a186642005-06-23 21:29:18 +1000125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100127 * Core function to create a range of memory available for mapping by a
128 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
130 * Adjusts the memory offset to its absolute value according to the mapping
131 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
132 * applicable and if supported by the kernel.
133 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000134static int drm_addmap_core(struct drm_device *dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000135 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000136 enum drm_map_flags flags,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000137 struct drm_map_list **maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100139 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000140 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000141 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000142 unsigned long user_token;
143 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Eric Anholt9a298b22009-03-24 12:23:04 -0700145 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -ENOMEM;
148
Dave Airlie7ab98402005-07-10 16:56:52 +1000149 map->offset = offset;
150 map->size = size;
151 map->flags = flags;
152 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* Only allow shared memory to be removable since we only keep enough
155 * book keeping information about shared memory to allow for removal
156 * when processes fork.
157 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700159 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EINVAL;
161 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100162 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
163 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000164
165 /* page-align _DRM_SHM maps. They are allocated here so there is no security
166 * hole created by that and it works around various broken drivers that use
167 * a non-aligned quantity to map the SAREA. --BenH
168 */
169 if (map->type == _DRM_SHM)
170 map->size = PAGE_ALIGN(map->size);
171
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100172 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700173 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -EINVAL;
175 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 map->handle = NULL;
178
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 case _DRM_REGISTERS:
181 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600182#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100183 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700185 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -EINVAL;
187 }
188#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000189 /* Some drivers preinitialize some maps, without the X Server
190 * needing to be aware of it. Therefore, we just return success
191 * when the server tries to create a duplicate map.
192 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000193 list = drm_find_matching_map(dev, map);
194 if (list != NULL) {
195 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000196 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 "mismatched sizes, (%ld vs %ld)\n",
198 map->type, map->size,
199 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000200 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000201 }
202
Eric Anholt9a298b22009-03-24 12:23:04 -0700203 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000204 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000205 return 0;
206 }
207
Daniel Vetter28185642013-08-08 15:41:27 +0200208 if (map->type == _DRM_FRAME_BUFFER ||
209 (map->flags & _DRM_WRITE_COMBINING)) {
210 map->mtrr =
211 arch_phys_wc_add(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Scott Thompson0769d392007-08-25 18:17:49 +1000213 if (map->type == _DRM_REGISTERS) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000214 if (map->flags & _DRM_WRITE_COMBINING)
215 map->handle = ioremap_wc(map->offset,
216 map->size);
217 else
218 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000219 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700220 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000221 return -ENOMEM;
222 }
223 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100227 list = drm_find_matching_map(dev, map);
228 if (list != NULL) {
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000229 if (list->map->size != map->size) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100230 DRM_DEBUG("Matching maps of type %d with "
231 "mismatched sizes, (%ld vs %ld)\n",
232 map->type, map->size, list->map->size);
233 list->map->size = map->size;
234 }
235
Eric Anholt9a298b22009-03-24 12:23:04 -0700236 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100237 *maplist = list;
238 return 0;
239 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100240 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 DRM_DEBUG("%lu %d %p\n",
Daniel Vetter04420c92013-07-10 14:11:57 +0200242 map->size, order_base_2(map->size), map->handle);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700244 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -ENOMEM;
246 }
247 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000248 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* Prevent a 2nd X Server from creating a 2nd lock */
Daniel Vetter95c081c2016-06-21 10:54:12 +0200250 if (dev->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700252 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EBUSY;
254 }
Daniel Vetter95c081c2016-06-21 10:54:12 +0200255 dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100258 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000259 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100260 int valid = 0;
261
Daniel Vetterd9906752013-12-11 11:34:35 +0100262 if (!dev->agp) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700263 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100264 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100266#ifdef __alpha__
267 map->offset += dev->hose->mem_space->start;
268#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000269 /* In some cases (i810 driver), user space may have already
270 * added the AGP base itself, because dev->agp->base previously
271 * only got set during AGP enable. So, only add the base
272 * address if the map's offset isn't already within the
273 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100274 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000275 if (map->offset < dev->agp->base ||
276 map->offset > dev->agp->base +
277 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
278 map->offset += dev->agp->base;
279 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100280 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
281
282 /* This assumes the DRM is in total control of AGP space.
283 * It's not always the case as AGP can be in the control
284 * of user space (i.e. i810 driver). So this loop will get
285 * skipped and we double check that dev->agp->memory is
286 * actually set as well as being invalid before EPERM'ing
287 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000288 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100289 if ((map->offset >= entry->bound) &&
290 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
291 valid = 1;
292 break;
293 }
294 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000295 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700296 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100297 return -EPERM;
298 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100299 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
300 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case _DRM_SCATTER_GATHER:
305 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700306 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -EINVAL;
308 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000309 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000311 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000312 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000313 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000314 * casting it down to 32 bits is no problem, but we
315 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800316 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000317 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700318 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000319 return -ENOMEM;
320 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000321 map->handle = dmah->vaddr;
322 map->offset = (unsigned long)dmah->busaddr;
323 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000324 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700326 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -EINVAL;
328 }
329
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400330 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700332 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100333 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700334 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -EINVAL;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 list->map = map;
338
Dave Airlie30e2fb12006-02-02 19:37:46 +1100339 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000340 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000341
Dave Airlied1f2b552005-08-05 22:11:22 +1000342 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100343 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000344 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
345 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800346 ret = drm_map_handle(dev, &list->hash, user_token, 0,
347 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000348 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700349 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100350 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700351 kfree(map);
352 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000353 mutex_unlock(&dev->struct_mutex);
354 return ret;
355 }
356
Thomas Hellstrom15450852007-02-08 16:14:05 +1100357 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100358 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Ben Skeggs2ff2e8a32009-05-26 10:35:52 +1000360 if (!(map->flags & _DRM_DRIVER))
Daniel Vetter95c081c2016-06-21 10:54:12 +0200361 list->master = dev->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000362 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000363 return 0;
Thierry Redingafe0f692014-04-29 11:44:38 +0200364}
Dave Airlie89625eb2005-09-05 21:23:23 +1000365
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000366int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset,
David Herrmann9fc5cde2014-08-29 12:12:28 +0200367 unsigned int size, enum drm_map_type type,
368 enum drm_map_flags flags, struct drm_local_map **map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000369{
Dave Airlie55910512007-07-11 16:53:40 +1000370 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000371 int rc;
372
373 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
374 if (!rc)
375 *map_ptr = list->map;
376 return rc;
377}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200378EXPORT_SYMBOL(drm_legacy_addmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000379
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100380/**
381 * Ioctl to specify a range of memory that is available for mapping by a
382 * non-root process.
383 *
384 * \param inode device inode.
385 * \param file_priv DRM file private.
386 * \param cmd command.
387 * \param arg pointer to a drm_map structure.
388 * \return zero on success or a negative value on error.
389 *
390 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200391int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
392 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000393{
Eric Anholtc153f452007-09-03 12:06:45 +1000394 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000395 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000396 int err;
397
Dave Airlie7c1c2872008-11-28 14:22:24 +1000398 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100399 return -EPERM;
400
Daniel Vettere975eef2016-04-26 19:29:37 +0200401 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200402 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100403 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200404
Eric Anholtc153f452007-09-03 12:06:45 +1000405 err = drm_addmap_core(dev, map->offset, map->size, map->type,
406 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000407
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000409 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000410
Dave Airlie67e1a012005-10-24 18:41:39 +1000411 /* 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 +1000412 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000413
414 /*
415 * It appears that there are no users of this value whatsoever --
416 * drmAddMap just discards it. Let's not encourage its use.
417 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
418 * it's not a real mtrr index anymore.)
419 */
420 map->mtrr = -1;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200425/*
426 * Get a mapping information.
427 *
428 * \param inode device inode.
429 * \param file_priv DRM file private.
430 * \param cmd command.
431 * \param arg user argument, pointing to a drm_map structure.
432 *
433 * \return zero on success or a negative number on failure.
434 *
435 * Searches for the mapping with the specified offset and copies its information
436 * into userspace
437 */
438int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
439 struct drm_file *file_priv)
440{
441 struct drm_map *map = data;
442 struct drm_map_list *r_list = NULL;
443 struct list_head *list;
444 int idx;
445 int i;
446
Daniel Vettere975eef2016-04-26 19:29:37 +0200447 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200448 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100449 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200450
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200451 idx = map->offset;
452 if (idx < 0)
453 return -EINVAL;
454
455 i = 0;
456 mutex_lock(&dev->struct_mutex);
457 list_for_each(list, &dev->maplist) {
458 if (i == idx) {
459 r_list = list_entry(list, struct drm_map_list, head);
460 break;
461 }
462 i++;
463 }
464 if (!r_list || !r_list->map) {
465 mutex_unlock(&dev->struct_mutex);
466 return -EINVAL;
467 }
468
469 map->offset = r_list->map->offset;
470 map->size = r_list->map->size;
471 map->type = r_list->map->type;
472 map->flags = r_list->map->flags;
473 map->handle = (void *)(unsigned long) r_list->user_token;
474 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
475
476 mutex_unlock(&dev->struct_mutex);
477
478 return 0;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/**
482 * Remove a map private from list and deallocate resources if the mapping
483 * isn't in use.
484 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * Searches the map on drm_device::maplist, removes it from the list, see if
486 * its being used, and free any associate resource (such as MTRR's) if it's not
487 * being on use.
488 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200489 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200491int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Dave Airlie55910512007-07-11 16:53:40 +1000493 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000494 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000495 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000496 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Dave Airlie836cf042005-07-10 19:27:04 +1000498 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000499 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000500 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000501 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000502 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100503 drm_ht_remove_key(&dev->map_hash,
504 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700505 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000506 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Dave Airlie836cf042005-07-10 19:27:04 +1000510
Dave Airliebd1b3312007-05-26 05:01:51 +1000511 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000512 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000513
514 switch (map->type) {
515 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100516 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000517 /* FALLTHROUGH */
518 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200519 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000520 break;
521 case _DRM_SHM:
522 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000523 if (master) {
524 if (dev->sigdata.lock == master->lock.hw_lock)
525 dev->sigdata.lock = NULL;
526 master->lock.hw_lock = NULL; /* SHM removed */
527 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100528 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000529 }
Dave Airlie836cf042005-07-10 19:27:04 +1000530 break;
531 case _DRM_AGP:
532 case _DRM_SCATTER_GATHER:
533 break;
534 case _DRM_CONSISTENT:
535 dmah.vaddr = map->handle;
536 dmah.busaddr = map->offset;
537 dmah.size = map->size;
Daniel Vetter1c96e842014-09-10 12:43:51 +0200538 __drm_legacy_pci_free(dev, &dmah);
Dave Airlie836cf042005-07-10 19:27:04 +1000539 break;
540 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700541 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 0;
544}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200545EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000546
Daniel Vetter40647e42016-04-27 09:20:18 +0200547void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000548{
Daniel Vetter40647e42016-04-27 09:20:18 +0200549 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200550 !drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200551 return;
Dave Airlie836cf042005-07-10 19:27:04 +1000552
Dave Airlie30e2fb12006-02-02 19:37:46 +1100553 mutex_lock(&dev->struct_mutex);
Daniel Vetter40647e42016-04-27 09:20:18 +0200554 drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100555 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000556}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200557EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000558
Daniel Vetter40647e42016-04-27 09:20:18 +0200559void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
560{
561 struct drm_map_list *r_list, *list_temp;
562
Daniel Vetterfa538642016-08-03 21:11:10 +0200563 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200564 return;
565
566 mutex_lock(&dev->struct_mutex);
567 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
568 if (r_list->master == master) {
569 drm_legacy_rmmap_locked(dev, r_list->map);
570 r_list = NULL;
571 }
572 }
573 mutex_unlock(&dev->struct_mutex);
574}
575
Dave Airlie836cf042005-07-10 19:27:04 +1000576/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
577 * the last close of the device, and this is necessary for cleanup when things
578 * exit uncleanly. Therefore, having userland manually remove mappings seems
579 * like a pointless exercise since they're going away anyway.
580 *
581 * One use case might be after addmap is allowed for normal users for SHM and
582 * gets used by drivers that the server doesn't need to care about. This seems
583 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100584 *
585 * \param inode device inode.
586 * \param file_priv DRM file private.
587 * \param cmd command.
588 * \param arg pointer to a struct drm_map structure.
589 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000590 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200591int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
592 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000593{
Eric Anholtc153f452007-09-03 12:06:45 +1000594 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100595 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000596 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000597 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000598
Daniel Vettere975eef2016-04-26 19:29:37 +0200599 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200600 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100601 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200602
Dave Airlie30e2fb12006-02-02 19:37:46 +1100603 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000604 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000605 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000606 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000607 r_list->map->flags & _DRM_REMOVABLE) {
608 map = r_list->map;
609 break;
610 }
611 }
612
613 /* List has wrapped around to the head pointer, or its empty we didn't
614 * find anything.
615 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000616 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100617 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000618 return -EINVAL;
619 }
620
Dave Airlie836cf042005-07-10 19:27:04 +1000621 /* Register and framebuffer maps are permanent */
622 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100623 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000624 return 0;
625 }
626
David Herrmann9fc5cde2014-08-29 12:12:28 +0200627 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000628
Dave Airlie30e2fb12006-02-02 19:37:46 +1100629 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000630
631 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/**
635 * Cleanup after an error on one of the addbufs() functions.
636 *
Dave Airlie836cf042005-07-10 19:27:04 +1000637 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * \param entry buffer entry where the error occurred.
639 *
640 * Frees any pages and buffers associated with the given entry.
641 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000642static void drm_cleanup_buf_error(struct drm_device *dev,
643 struct drm_buf_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int i;
646
647 if (entry->seg_count) {
648 for (i = 0; i < entry->seg_count; i++) {
649 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100650 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700653 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 entry->seg_count = 0;
656 }
657
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 if (entry->buf_count) {
659 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700660 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700662 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 entry->buf_count = 0;
665 }
666}
667
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200668#if IS_ENABLED(CONFIG_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/**
Dave Airlied59431b2005-07-10 15:00:06 +1000670 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000672 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000673 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * After some sanity checks creates a drm_buf structure for each buffer and
677 * reallocates the buffer list of the same size order to accommodate the new
678 * buffers.
679 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200680int drm_legacy_addbufs_agp(struct drm_device *dev,
681 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Dave Airliecdd55a22007-07-11 16:32:08 +1000683 struct drm_device_dma *dma = dev->dma;
684 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000685 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000686 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned long offset;
688 unsigned long agp_offset;
689 int count;
690 int order;
691 int size;
692 int alignment;
693 int page_order;
694 int total;
695 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100696 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000697 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 if (!dma)
700 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Dave Airlied59431b2005-07-10 15:00:06 +1000702 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200703 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 size = 1 << order;
705
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 alignment = (request->flags & _DRM_PAGE_ALIGN)
707 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
709 total = PAGE_SIZE << page_order;
710
711 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000712 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 DRM_DEBUG("count: %d\n", count);
715 DRM_DEBUG("order: %d\n", order);
716 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100717 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 DRM_DEBUG("alignment: %d\n", alignment);
719 DRM_DEBUG("page_order: %d\n", page_order);
720 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
723 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Dave Airlie54ba2f72007-02-10 12:07:47 +1100725 /* Make sure buffers are located in AGP memory that we own */
726 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000727 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100728 if ((agp_offset >= agp_entry->bound) &&
729 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
730 valid = 1;
731 break;
732 }
733 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000734 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100735 DRM_DEBUG("zone invalid\n");
736 return -EINVAL;
737 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100738 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100740 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EBUSY;
742 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100744 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Dave Airlie30e2fb12006-02-02 19:37:46 +1100746 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100749 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 atomic_dec(&dev->buf_alloc);
751 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
754 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100755 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -EINVAL;
758 }
759
Markus Elfring81a44132016-09-19 17:24:20 +0200760 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100762 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -ENOMEM;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 entry->buf_size = size;
768 entry->page_order = page_order;
769
770 offset = 0;
771
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 while (entry->buf_count < count) {
773 buf = &entry->buflist[entry->buf_count];
774 buf->idx = dma->buf_count + entry->buf_count;
775 buf->total = alignment;
776 buf->order = order;
777 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 buf->bus_address = agp_offset + offset;
781 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 buf->waiting = 0;
784 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000785 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400788 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* Set count correctly so we free the proper amount. */
791 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100793 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -ENOMEM;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 offset += alignment;
801 entry->buf_count++;
802 byte_count += PAGE_SIZE << page_order;
803 }
804
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Eric Anholt9a298b22009-03-24 12:23:04 -0700807 temp_buflist = krealloc(dma->buflist,
808 (dma->buf_count + entry->buf_count) *
809 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100813 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -ENOMEM;
816 }
817 dma->buflist = temp_buflist;
818
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
821 }
822
823 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100824 dma->seg_count += entry->seg_count;
825 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 dma->byte_count += byte_count;
827
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
829 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Dave Airlie30e2fb12006-02-02 19:37:46 +1100831 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dave Airlied59431b2005-07-10 15:00:06 +1000833 request->count = entry->buf_count;
834 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 dma->flags = _DRM_DMA_USE_AGP;
837
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200841EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200842#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843
David Herrmann9fc5cde2014-08-29 12:12:28 +0200844int drm_legacy_addbufs_pci(struct drm_device *dev,
845 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Dave Airliecdd55a22007-07-11 16:32:08 +1000847 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int count;
849 int order;
850 int size;
851 int total;
852 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000853 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100854 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000855 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int alignment;
857 unsigned long offset;
858 int i;
859 int byte_count;
860 int page_count;
861 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000862 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +0100865 return -EOPNOTSUPP;
Dave Airlied985c102006-01-02 21:32:48 +1100866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 if (!dma)
868 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Dave Airlied985c102006-01-02 21:32:48 +1100870 if (!capable(CAP_SYS_ADMIN))
871 return -EPERM;
872
Dave Airlied59431b2005-07-10 15:00:06 +1000873 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200874 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 size = 1 << order;
876
Daniel Vettera344a7e2011-10-26 00:54:41 +0200877 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
878 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
881 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Airlied59431b2005-07-10 15:00:06 +1000883 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
886 total = PAGE_SIZE << page_order;
887
Daniel Vetter2177a212013-12-16 11:21:06 +0100888 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100890 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EBUSY;
892 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100894 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Dave Airlie30e2fb12006-02-02 19:37:46 +1100896 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100899 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -ENOMEM; /* May only call once for each order */
902 }
903
904 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100905 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return -EINVAL;
908 }
909
Markus Elfringed6dee42016-09-19 17:17:34 +0200910 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100912 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOMEM;
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Markus Elfringed6dee42016-09-19 17:17:34 +0200917 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700919 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100920 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ENOMEM;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /* Keep the original pagelist until we know all the allocations
926 * have succeeded
927 */
Markus Elfring20274002016-09-19 17:07:06 +0200928 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
929 sizeof(*dma->pagelist),
930 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700932 kfree(entry->buflist);
933 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100934 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -ENOMEM;
937 }
938 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
940 DRM_DEBUG("pagelist: %d entries\n",
941 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 entry->page_order = page_order;
945 byte_count = 0;
946 page_count = 0;
947
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000949
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800950 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000951
Dave Airlieddf19b92006-03-19 18:56:12 +1100952 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /* Set count correctly so we free the proper amount. */
954 entry->buf_count = count;
955 entry->seg_count = count;
956 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700957 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100958 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000959 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -ENOMEM;
961 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100962 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 for (i = 0; i < (1 << page_order); i++) {
964 DRM_DEBUG("page %d @ 0x%08lx\n",
965 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100966 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100968 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 for (offset = 0;
971 offset + size <= total && entry->buf_count < count;
972 offset += alignment, ++entry->buf_count) {
973 buf = &entry->buflist[entry->buf_count];
974 buf->idx = dma->buf_count + entry->buf_count;
975 buf->total = alignment;
976 buf->order = order;
977 buf->used = 0;
978 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100979 buf->address = (void *)(dmah->vaddr + offset);
980 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000981 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 buf->waiting = 0;
983 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000984 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400987 buf->dev_private = kzalloc(buf->dev_priv_size,
988 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* Set count correctly so we free the proper amount. */
991 entry->buf_count = count;
992 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700994 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100995 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -ENOMEM;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 DRM_DEBUG("buffer %d @ %p\n",
1001 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 byte_count += PAGE_SIZE << page_order;
1004 }
1005
Eric Anholt9a298b22009-03-24 12:23:04 -07001006 temp_buflist = krealloc(dma->buflist,
1007 (dma->buf_count + entry->buf_count) *
1008 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (!temp_buflist) {
1010 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001012 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001013 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return -ENOMEM;
1016 }
1017 dma->buflist = temp_buflist;
1018
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1021 }
1022
Thomas Weber88393162010-03-16 11:47:56 +01001023 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * with the new one.
1025 */
1026 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001027 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 dma->pagelist = temp_pagelist;
1030
1031 dma->buf_count += entry->buf_count;
1032 dma->seg_count += entry->seg_count;
1033 dma->page_count += entry->seg_count << page_order;
1034 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1035
Dave Airlie30e2fb12006-02-02 19:37:46 +11001036 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Dave Airlied59431b2005-07-10 15:00:06 +10001038 request->count = entry->buf_count;
1039 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
George Sapountzis3417f332006-10-24 12:03:04 -07001041 if (request->flags & _DRM_PCI_BUFFER_RO)
1042 dma->flags = _DRM_DMA_USE_PCI_RO;
1043
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046
1047}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001048EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
David Herrmann9fc5cde2014-08-29 12:12:28 +02001050static int drm_legacy_addbufs_sg(struct drm_device *dev,
1051 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Dave Airliecdd55a22007-07-11 16:32:08 +10001053 struct drm_device_dma *dma = dev->dma;
1054 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001055 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 unsigned long offset;
1057 unsigned long agp_offset;
1058 int count;
1059 int order;
1060 int size;
1061 int alignment;
1062 int page_order;
1063 int total;
1064 int byte_count;
1065 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001066 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 if (!drm_core_check_feature(dev, DRIVER_SG))
Chris Wilson69fdf422018-09-13 20:20:50 +01001069 return -EOPNOTSUPP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070
1071 if (!dma)
1072 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dave Airlied985c102006-01-02 21:32:48 +11001074 if (!capable(CAP_SYS_ADMIN))
1075 return -EPERM;
1076
Dave Airlied59431b2005-07-10 15:00:06 +10001077 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001078 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 size = 1 << order;
1080
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 alignment = (request->flags & _DRM_PAGE_ALIGN)
1082 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1084 total = PAGE_SIZE << page_order;
1085
1086 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001087 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 DRM_DEBUG("count: %d\n", count);
1090 DRM_DEBUG("order: %d\n", order);
1091 DRM_DEBUG("size: %d\n", size);
1092 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1093 DRM_DEBUG("alignment: %d\n", alignment);
1094 DRM_DEBUG("page_order: %d\n", page_order);
1095 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1098 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Daniel Vetter2177a212013-12-16 11:21:06 +01001100 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001102 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -EBUSY;
1104 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001106 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Dave Airlie30e2fb12006-02-02 19:37:46 +11001108 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001111 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 atomic_dec(&dev->buf_alloc);
1113 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115
1116 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001117 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -EINVAL;
1120 }
1121
Markus Elfringb5a2ecd2016-09-19 17:30:31 +02001122 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001124 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return -ENOMEM;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 entry->buf_size = size;
1130 entry->page_order = page_order;
1131
1132 offset = 0;
1133
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001134 while (entry->buf_count < count) {
1135 buf = &entry->buflist[entry->buf_count];
1136 buf->idx = dma->buf_count + entry->buf_count;
1137 buf->total = alignment;
1138 buf->order = order;
1139 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001144 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001145 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 buf->waiting = 0;
1147 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001148 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001151 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001152 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* Set count correctly so we free the proper amount. */
1154 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001156 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001157 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return -ENOMEM;
1159 }
1160
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001161 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 offset += alignment;
1164 entry->buf_count++;
1165 byte_count += PAGE_SIZE << page_order;
1166 }
1167
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001168 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Eric Anholt9a298b22009-03-24 12:23:04 -07001170 temp_buflist = krealloc(dma->buflist,
1171 (dma->buf_count + entry->buf_count) *
1172 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001176 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001177 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -ENOMEM;
1179 }
1180 dma->buflist = temp_buflist;
1181
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001182 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1184 }
1185
1186 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001187 dma->seg_count += entry->seg_count;
1188 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 dma->byte_count += byte_count;
1190
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001191 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1192 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dave Airlie30e2fb12006-02-02 19:37:46 +11001194 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Dave Airlied59431b2005-07-10 15:00:06 +10001196 request->count = entry->buf_count;
1197 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 dma->flags = _DRM_DMA_USE_SG;
1200
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
1205/**
1206 * Add buffers for DMA transfers (ioctl).
1207 *
1208 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001209 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001211 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * \return zero on success or a negative number on failure.
1213 *
1214 * According with the memory type specified in drm_buf_desc::flags and the
1215 * build options, it dispatches the call either to addbufs_agp(),
1216 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1217 * PCI memory respectively.
1218 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001219int drm_legacy_addbufs(struct drm_device *dev, void *data,
1220 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Eric Anholtc153f452007-09-03 12:06:45 +10001222 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001223 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001224
Daniel Vetterfa538642016-08-03 21:11:10 +02001225 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001226 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001229 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001231#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001232 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001233 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 else
1235#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001236 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001237 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001238 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001239 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001241 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001242
Dave Airlied59431b2005-07-10 15:00:06 +10001243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/**
1247 * Get information about the buffer mappings.
1248 *
1249 * This was originally mean for debugging purposes, or by a sophisticated
1250 * client library to determine how best to use the available buffers (e.g.,
1251 * large buffers can be used for image transfer).
1252 *
1253 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001254 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * \param cmd command.
1256 * \param arg pointer to a drm_buf_info structure.
1257 * \return zero on success or a negative number on failure.
1258 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001259 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * lock, preventing of allocating more buffers after this call. Information
1261 * about each requested buffer is then copied into user space.
1262 */
Al Viro5c7640a2017-05-24 17:54:09 -04001263int __drm_legacy_infobufs(struct drm_device *dev,
1264 void *data, int *p,
1265 int (*f)(void *, int, struct drm_buf_entry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Dave Airliecdd55a22007-07-11 16:32:08 +10001267 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int i;
1269 int count;
1270
Daniel Vetterfa538642016-08-03 21:11:10 +02001271 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001272 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001275 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001277 if (!dma)
1278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Daniel Vetter2177a212013-12-16 11:21:06 +01001280 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001281 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001282 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EBUSY;
1284 }
1285 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001286 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001288 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1289 if (dma->bufs[i].buf_count)
1290 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001293 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Al Viro5c7640a2017-05-24 17:54:09 -04001295 if (*p >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
Al Viro5c7640a2017-05-24 17:54:09 -04001297 struct drm_buf_entry *from = &dma->bufs[i];
1298 if (from->buf_count) {
1299 if (f(data, count, from) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301 DRM_DEBUG("%d %d %d %d %d\n",
1302 i,
1303 dma->bufs[i].buf_count,
1304 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001305 dma->bufs[i].low_mark,
1306 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ++count;
1308 }
1309 }
1310 }
Al Viro5c7640a2017-05-24 17:54:09 -04001311 *p = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 return 0;
1314}
1315
Al Viro5c7640a2017-05-24 17:54:09 -04001316static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1317{
1318 struct drm_buf_info *request = data;
1319 struct drm_buf_desc __user *to = &request->list[count];
1320 struct drm_buf_desc v = {.count = from->buf_count,
1321 .size = from->buf_size,
1322 .low_mark = from->low_mark,
1323 .high_mark = from->high_mark};
1324 return copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags));
1325}
1326
1327int drm_legacy_infobufs(struct drm_device *dev, void *data,
1328 struct drm_file *file_priv)
1329{
1330 struct drm_buf_info *request = data;
1331 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/**
1335 * Specifies a low and high water mark for buffer allocation
1336 *
1337 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001338 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * \param cmd command.
1340 * \param arg a pointer to a drm_buf_desc structure.
1341 * \return zero on success or a negative number on failure.
1342 *
1343 * Verifies that the size order is bounded between the admissible orders and
1344 * updates the respective drm_device_dma::bufs entry low and high water mark.
1345 *
1346 * \note This ioctl is deprecated and mostly never used.
1347 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001348int drm_legacy_markbufs(struct drm_device *dev, void *data,
1349 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Dave Airliecdd55a22007-07-11 16:32:08 +10001351 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001352 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001354 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Daniel Vetterfa538642016-08-03 21:11:10 +02001356 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001357 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001360 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 if (!dma)
1363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001366 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001367 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1369 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 entry = &dma->bufs[order];
1371
Eric Anholtc153f452007-09-03 12:06:45 +10001372 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001374 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return -EINVAL;
1376
David Herrmannb008c0f2014-07-23 17:26:36 +02001377 entry->low_mark = request->low_mark;
1378 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 return 0;
1381}
1382
1383/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001384 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 *
1386 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001387 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 * \param cmd command.
1389 * \param arg pointer to a drm_buf_free structure.
1390 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001391 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * Calls free_buffer() for each used buffer.
1393 * This function is primarily used for debugging.
1394 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001395int drm_legacy_freebufs(struct drm_device *dev, void *data,
1396 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Dave Airliecdd55a22007-07-11 16:32:08 +10001398 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001399 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int i;
1401 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001402 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Daniel Vetterfa538642016-08-03 21:11:10 +02001404 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001405 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001408 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001410 if (!dma)
1411 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Eric Anholtc153f452007-09-03 12:06:45 +10001413 DRM_DEBUG("%d\n", request->count);
1414 for (i = 0; i < request->count; i++) {
1415 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001417 if (idx < 0 || idx >= dma->buf_count) {
1418 DRM_ERROR("Index %d (of %d max)\n",
1419 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EINVAL;
1421 }
Gustavo A. R. Silvaa3780502018-10-16 11:55:49 +02001422 idx = array_index_nospec(idx, dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001424 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001425 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001426 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return -EINVAL;
1428 }
Daniel Vettera2661622014-09-11 07:41:51 +02001429 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
1432 return 0;
1433}
1434
1435/**
1436 * Maps all of the DMA buffers into client-virtual space (ioctl).
1437 *
1438 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001439 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * \param cmd command.
1441 * \param arg pointer to a drm_buf_map structure.
1442 * \return zero on success or a negative number on failure.
1443 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001444 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1445 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001446 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1447 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Al Viro87d3ce12017-05-25 16:24:20 -04001449int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1450 void __user **v,
1451 int (*f)(void *, int, unsigned long,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +00001452 struct drm_buf *),
1453 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Dave Airliecdd55a22007-07-11 16:32:08 +10001455 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 unsigned long virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int i;
1459
Daniel Vetterfa538642016-08-03 21:11:10 +02001460 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001461 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001464 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 if (!dma)
1467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Daniel Vetter2177a212013-12-16 11:21:06 +01001469 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001470 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001471 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EBUSY;
1473 }
1474 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001475 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Al Viro87d3ce12017-05-25 16:24:20 -04001477 if (*p >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001478 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001479 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001480 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001481 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001482 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 retcode = -EINVAL;
1486 goto done;
1487 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001488 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001490 MAP_SHARED,
1491 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001493 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 PROT_READ | PROT_WRITE,
1495 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /* Real error */
1499 retcode = (signed long)virtual;
1500 goto done;
1501 }
Al Viro87d3ce12017-05-25 16:24:20 -04001502 *v = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001504 for (i = 0; i < dma->buf_count; i++) {
Al Viro87d3ce12017-05-25 16:24:20 -04001505 if (f(data, i, virtual, dma->buflist[i]) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 retcode = -EFAULT;
1507 goto done;
1508 }
1509 }
1510 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001511 done:
Al Viro87d3ce12017-05-25 16:24:20 -04001512 *p = dma->buf_count;
1513 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 return retcode;
1516}
1517
Al Viro87d3ce12017-05-25 16:24:20 -04001518static int map_one_buf(void *data, int idx, unsigned long virtual,
1519 struct drm_buf *buf)
1520{
1521 struct drm_buf_map *request = data;
1522 unsigned long address = virtual + buf->offset; /* *** */
1523
1524 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1525 sizeof(request->list[0].idx)))
1526 return -EFAULT;
1527 if (copy_to_user(&request->list[idx].total, &buf->total,
1528 sizeof(request->list[0].total)))
1529 return -EFAULT;
1530 if (clear_user(&request->list[idx].used, sizeof(int)))
1531 return -EFAULT;
1532 if (copy_to_user(&request->list[idx].address, &address,
1533 sizeof(address)))
1534 return -EFAULT;
1535 return 0;
1536}
1537
1538int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1539 struct drm_file *file_priv)
1540{
1541 struct drm_buf_map *request = data;
1542 return __drm_legacy_mapbufs(dev, data, &request->count,
1543 &request->virtual, map_one_buf,
1544 file_priv);
1545}
1546
David Herrmann9fc5cde2014-08-29 12:12:28 +02001547int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001548 struct drm_file *file_priv)
1549{
Daniel Vetterfa538642016-08-03 21:11:10 +02001550 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001551 return -EOPNOTSUPP;
Daniel Vetter6eb92782013-08-08 15:41:29 +02001552
1553 if (dev->driver->dma_ioctl)
1554 return dev->driver->dma_ioctl(dev, data, file_priv);
1555 else
1556 return -EINVAL;
1557}
1558
David Herrmann9fc5cde2014-08-29 12:12:28 +02001559struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001560{
1561 struct drm_map_list *entry;
1562
1563 list_for_each_entry(entry, &dev->maplist, head) {
1564 if (entry->map && entry->map->type == _DRM_SHM &&
1565 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1566 return entry->map;
1567 }
1568 }
1569 return NULL;
1570}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001571EXPORT_SYMBOL(drm_legacy_getsarea);