blob: ceca79b1468d8a0ed630e07a4e1fe0f9fb9ac12e [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 Skeggs2ff2e8a2009-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
Jani Nikulac7642682018-12-28 15:04:46 +0200380struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
381 unsigned int token)
382{
383 struct drm_map_list *_entry;
384 list_for_each_entry(_entry, &dev->maplist, head)
385 if (_entry->user_token == token)
386 return _entry->map;
387 return NULL;
388}
389EXPORT_SYMBOL(drm_legacy_findmap);
390
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100391/**
392 * Ioctl to specify a range of memory that is available for mapping by a
393 * non-root process.
394 *
395 * \param inode device inode.
396 * \param file_priv DRM file private.
397 * \param cmd command.
398 * \param arg pointer to a drm_map structure.
399 * \return zero on success or a negative value on error.
400 *
401 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200402int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
403 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000404{
Eric Anholtc153f452007-09-03 12:06:45 +1000405 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000406 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000407 int err;
408
Dave Airlie7c1c2872008-11-28 14:22:24 +1000409 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100410 return -EPERM;
411
Daniel Vettere975eef2016-04-26 19:29:37 +0200412 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200413 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100414 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200415
Eric Anholtc153f452007-09-03 12:06:45 +1000416 err = drm_addmap_core(dev, map->offset, map->size, map->type,
417 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000418
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000420 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000421
Dave Airlie67e1a012005-10-24 18:41:39 +1000422 /* 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 +1000423 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000424
425 /*
426 * It appears that there are no users of this value whatsoever --
427 * drmAddMap just discards it. Let's not encourage its use.
428 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
429 * it's not a real mtrr index anymore.)
430 */
431 map->mtrr = -1;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200436/*
437 * Get a mapping information.
438 *
439 * \param inode device inode.
440 * \param file_priv DRM file private.
441 * \param cmd command.
442 * \param arg user argument, pointing to a drm_map structure.
443 *
444 * \return zero on success or a negative number on failure.
445 *
446 * Searches for the mapping with the specified offset and copies its information
447 * into userspace
448 */
449int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
451{
452 struct drm_map *map = data;
453 struct drm_map_list *r_list = NULL;
454 struct list_head *list;
455 int idx;
456 int i;
457
Daniel Vettere975eef2016-04-26 19:29:37 +0200458 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200459 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100460 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200461
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200462 idx = map->offset;
463 if (idx < 0)
464 return -EINVAL;
465
466 i = 0;
467 mutex_lock(&dev->struct_mutex);
468 list_for_each(list, &dev->maplist) {
469 if (i == idx) {
470 r_list = list_entry(list, struct drm_map_list, head);
471 break;
472 }
473 i++;
474 }
475 if (!r_list || !r_list->map) {
476 mutex_unlock(&dev->struct_mutex);
477 return -EINVAL;
478 }
479
480 map->offset = r_list->map->offset;
481 map->size = r_list->map->size;
482 map->type = r_list->map->type;
483 map->flags = r_list->map->flags;
484 map->handle = (void *)(unsigned long) r_list->user_token;
485 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
486
487 mutex_unlock(&dev->struct_mutex);
488
489 return 0;
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/**
493 * Remove a map private from list and deallocate resources if the mapping
494 * isn't in use.
495 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * Searches the map on drm_device::maplist, removes it from the list, see if
Matt Roper1e55a532019-02-01 17:23:26 -0800497 * it's being used, and free any associated resource (such as MTRR's) if it's not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * being on use.
499 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200500 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200502int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Dave Airlie55910512007-07-11 16:53:40 +1000504 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000505 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000506 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000507 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Dave Airlie836cf042005-07-10 19:27:04 +1000509 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000510 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000511 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000512 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000513 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100514 drm_ht_remove_key(&dev->map_hash,
515 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700516 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000517 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Dave Airlie836cf042005-07-10 19:27:04 +1000521
Dave Airliebd1b3312007-05-26 05:01:51 +1000522 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000523 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000524
525 switch (map->type) {
526 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100527 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000528 /* FALLTHROUGH */
529 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200530 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000531 break;
532 case _DRM_SHM:
533 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000534 if (master) {
535 if (dev->sigdata.lock == master->lock.hw_lock)
536 dev->sigdata.lock = NULL;
537 master->lock.hw_lock = NULL; /* SHM removed */
538 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100539 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000540 }
Dave Airlie836cf042005-07-10 19:27:04 +1000541 break;
542 case _DRM_AGP:
543 case _DRM_SCATTER_GATHER:
544 break;
545 case _DRM_CONSISTENT:
546 dmah.vaddr = map->handle;
547 dmah.busaddr = map->offset;
548 dmah.size = map->size;
Daniel Vetter1c96e842014-09-10 12:43:51 +0200549 __drm_legacy_pci_free(dev, &dmah);
Dave Airlie836cf042005-07-10 19:27:04 +1000550 break;
551 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700552 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200556EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000557
Daniel Vetter40647e42016-04-27 09:20:18 +0200558void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000559{
Daniel Vetter40647e42016-04-27 09:20:18 +0200560 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200561 !drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200562 return;
Dave Airlie836cf042005-07-10 19:27:04 +1000563
Dave Airlie30e2fb12006-02-02 19:37:46 +1100564 mutex_lock(&dev->struct_mutex);
Daniel Vetter40647e42016-04-27 09:20:18 +0200565 drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100566 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000567}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200568EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000569
Daniel Vetter40647e42016-04-27 09:20:18 +0200570void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
571{
572 struct drm_map_list *r_list, *list_temp;
573
Daniel Vetterfa538642016-08-03 21:11:10 +0200574 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200575 return;
576
577 mutex_lock(&dev->struct_mutex);
578 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
579 if (r_list->master == master) {
580 drm_legacy_rmmap_locked(dev, r_list->map);
581 r_list = NULL;
582 }
583 }
584 mutex_unlock(&dev->struct_mutex);
585}
586
Dave Airlie35a28022019-04-23 08:45:12 +1000587void drm_legacy_rmmaps(struct drm_device *dev)
588{
589 struct drm_map_list *r_list, *list_temp;
590
591 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
592 drm_legacy_rmmap(dev, r_list->map);
593}
594
Dave Airlie836cf042005-07-10 19:27:04 +1000595/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
596 * the last close of the device, and this is necessary for cleanup when things
597 * exit uncleanly. Therefore, having userland manually remove mappings seems
598 * like a pointless exercise since they're going away anyway.
599 *
600 * One use case might be after addmap is allowed for normal users for SHM and
601 * gets used by drivers that the server doesn't need to care about. This seems
602 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100603 *
604 * \param inode device inode.
605 * \param file_priv DRM file private.
606 * \param cmd command.
607 * \param arg pointer to a struct drm_map structure.
608 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000609 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200610int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
611 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000612{
Eric Anholtc153f452007-09-03 12:06:45 +1000613 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100614 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000615 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000616 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000617
Daniel Vettere975eef2016-04-26 19:29:37 +0200618 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200619 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100620 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200621
Dave Airlie30e2fb12006-02-02 19:37:46 +1100622 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000623 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000624 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000625 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000626 r_list->map->flags & _DRM_REMOVABLE) {
627 map = r_list->map;
628 break;
629 }
630 }
631
Matt Roper1e55a532019-02-01 17:23:26 -0800632 /* List has wrapped around to the head pointer, or it's empty we didn't
Dave Airlie836cf042005-07-10 19:27:04 +1000633 * find anything.
634 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000635 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100636 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000637 return -EINVAL;
638 }
639
Dave Airlie836cf042005-07-10 19:27:04 +1000640 /* Register and framebuffer maps are permanent */
641 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100642 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000643 return 0;
644 }
645
David Herrmann9fc5cde2014-08-29 12:12:28 +0200646 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000647
Dave Airlie30e2fb12006-02-02 19:37:46 +1100648 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000649
650 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653/**
654 * Cleanup after an error on one of the addbufs() functions.
655 *
Dave Airlie836cf042005-07-10 19:27:04 +1000656 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * \param entry buffer entry where the error occurred.
658 *
659 * Frees any pages and buffers associated with the given entry.
660 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000661static void drm_cleanup_buf_error(struct drm_device *dev,
662 struct drm_buf_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 int i;
665
666 if (entry->seg_count) {
667 for (i = 0; i < entry->seg_count; i++) {
668 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100669 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700672 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 entry->seg_count = 0;
675 }
676
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 if (entry->buf_count) {
678 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700679 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700681 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 entry->buf_count = 0;
684 }
685}
686
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200687#if IS_ENABLED(CONFIG_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/**
Dave Airlied59431b2005-07-10 15:00:06 +1000689 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000691 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000692 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * After some sanity checks creates a drm_buf structure for each buffer and
696 * reallocates the buffer list of the same size order to accommodate the new
697 * buffers.
698 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200699int drm_legacy_addbufs_agp(struct drm_device *dev,
700 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Dave Airliecdd55a22007-07-11 16:32:08 +1000702 struct drm_device_dma *dma = dev->dma;
703 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000704 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000705 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 unsigned long offset;
707 unsigned long agp_offset;
708 int count;
709 int order;
710 int size;
711 int alignment;
712 int page_order;
713 int total;
714 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100715 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000716 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 if (!dma)
719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Dave Airlied59431b2005-07-10 15:00:06 +1000721 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200722 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 size = 1 << order;
724
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 alignment = (request->flags & _DRM_PAGE_ALIGN)
726 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
728 total = PAGE_SIZE << page_order;
729
730 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000731 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 DRM_DEBUG("count: %d\n", count);
734 DRM_DEBUG("order: %d\n", order);
735 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100736 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 DRM_DEBUG("alignment: %d\n", alignment);
738 DRM_DEBUG("page_order: %d\n", page_order);
739 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Dave Airlie54ba2f72007-02-10 12:07:47 +1100744 /* Make sure buffers are located in AGP memory that we own */
745 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000746 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100747 if ((agp_offset >= agp_entry->bound) &&
748 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
749 valid = 1;
750 break;
751 }
752 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000753 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100754 DRM_DEBUG("zone invalid\n");
755 return -EINVAL;
756 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100757 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100759 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EBUSY;
761 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100763 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Dave Airlie30e2fb12006-02-02 19:37:46 +1100765 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100768 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 atomic_dec(&dev->buf_alloc);
770 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100774 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EINVAL;
777 }
778
Markus Elfring81a44132016-09-19 17:24:20 +0200779 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100781 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return -ENOMEM;
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 entry->buf_size = size;
787 entry->page_order = page_order;
788
789 offset = 0;
790
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 while (entry->buf_count < count) {
792 buf = &entry->buflist[entry->buf_count];
793 buf->idx = dma->buf_count + entry->buf_count;
794 buf->total = alignment;
795 buf->order = order;
796 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 buf->bus_address = agp_offset + offset;
800 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 buf->waiting = 0;
803 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000804 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400807 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /* Set count correctly so we free the proper amount. */
810 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100812 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -ENOMEM;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 offset += alignment;
820 entry->buf_count++;
821 byte_count += PAGE_SIZE << page_order;
822 }
823
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Eric Anholt9a298b22009-03-24 12:23:04 -0700826 temp_buflist = krealloc(dma->buflist,
827 (dma->buf_count + entry->buf_count) *
828 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100832 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -ENOMEM;
835 }
836 dma->buflist = temp_buflist;
837
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
840 }
841
842 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100843 dma->seg_count += entry->seg_count;
844 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 dma->byte_count += byte_count;
846
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
848 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Airlie30e2fb12006-02-02 19:37:46 +1100850 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Airlied59431b2005-07-10 15:00:06 +1000852 request->count = entry->buf_count;
853 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 dma->flags = _DRM_DMA_USE_AGP;
856
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200860EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200861#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862
David Herrmann9fc5cde2014-08-29 12:12:28 +0200863int drm_legacy_addbufs_pci(struct drm_device *dev,
864 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Dave Airliecdd55a22007-07-11 16:32:08 +1000866 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int count;
868 int order;
869 int size;
870 int total;
871 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000872 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100873 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000874 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int alignment;
876 unsigned long offset;
877 int i;
878 int byte_count;
879 int page_count;
880 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000881 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000883 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +0100884 return -EOPNOTSUPP;
Dave Airlied985c102006-01-02 21:32:48 +1100885
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 if (!dma)
887 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Dave Airlied985c102006-01-02 21:32:48 +1100889 if (!capable(CAP_SYS_ADMIN))
890 return -EPERM;
891
Dave Airlied59431b2005-07-10 15:00:06 +1000892 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200893 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 size = 1 << order;
895
Daniel Vettera344a7e2011-10-26 00:54:41 +0200896 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
897 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
900 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Dave Airlied59431b2005-07-10 15:00:06 +1000902 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
905 total = PAGE_SIZE << page_order;
906
Daniel Vetter2177a212013-12-16 11:21:06 +0100907 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100909 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -EBUSY;
911 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100913 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Dave Airlie30e2fb12006-02-02 19:37:46 +1100915 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 if (entry->buf_count) {
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; /* May only call once for each order */
921 }
922
923 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100924 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EINVAL;
927 }
928
Markus Elfringed6dee42016-09-19 17:17:34 +0200929 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 if (!entry->buflist) {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Markus Elfringed6dee42016-09-19 17:17:34 +0200936 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000937 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700938 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100939 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -ENOMEM;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* Keep the original pagelist until we know all the allocations
945 * have succeeded
946 */
Markus Elfring20274002016-09-19 17:07:06 +0200947 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
948 sizeof(*dma->pagelist),
949 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700951 kfree(entry->buflist);
952 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100953 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -ENOMEM;
956 }
957 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
959 DRM_DEBUG("pagelist: %d entries\n",
960 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000962 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 entry->page_order = page_order;
964 byte_count = 0;
965 page_count = 0;
966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000968
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800969 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000970
Dave Airlieddf19b92006-03-19 18:56:12 +1100971 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Set count correctly so we free the proper amount. */
973 entry->buf_count = count;
974 entry->seg_count = count;
975 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700976 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100977 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000978 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -ENOMEM;
980 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100981 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 for (i = 0; i < (1 << page_order); i++) {
983 DRM_DEBUG("page %d @ 0x%08lx\n",
984 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100985 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100987 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 for (offset = 0;
990 offset + size <= total && entry->buf_count < count;
991 offset += alignment, ++entry->buf_count) {
992 buf = &entry->buflist[entry->buf_count];
993 buf->idx = dma->buf_count + entry->buf_count;
994 buf->total = alignment;
995 buf->order = order;
996 buf->used = 0;
997 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100998 buf->address = (void *)(dmah->vaddr + offset);
999 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 buf->waiting = 0;
1002 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001003 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001006 buf->dev_private = kzalloc(buf->dev_priv_size,
1007 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* Set count correctly so we free the proper amount. */
1010 entry->buf_count = count;
1011 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001013 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001014 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -ENOMEM;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 DRM_DEBUG("buffer %d @ %p\n",
1020 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 byte_count += PAGE_SIZE << page_order;
1023 }
1024
Eric Anholt9a298b22009-03-24 12:23:04 -07001025 temp_buflist = krealloc(dma->buflist,
1026 (dma->buf_count + entry->buf_count) *
1027 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (!temp_buflist) {
1029 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001031 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001032 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -ENOMEM;
1035 }
1036 dma->buflist = temp_buflist;
1037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1040 }
1041
Thomas Weber88393162010-03-16 11:47:56 +01001042 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * with the new one.
1044 */
1045 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001046 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 dma->pagelist = temp_pagelist;
1049
1050 dma->buf_count += entry->buf_count;
1051 dma->seg_count += entry->seg_count;
1052 dma->page_count += entry->seg_count << page_order;
1053 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1054
Dave Airlie30e2fb12006-02-02 19:37:46 +11001055 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Airlied59431b2005-07-10 15:00:06 +10001057 request->count = entry->buf_count;
1058 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
George Sapountzis3417f332006-10-24 12:03:04 -07001060 if (request->flags & _DRM_PCI_BUFFER_RO)
1061 dma->flags = _DRM_DMA_USE_PCI_RO;
1062
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065
1066}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001067EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
David Herrmann9fc5cde2014-08-29 12:12:28 +02001069static int drm_legacy_addbufs_sg(struct drm_device *dev,
1070 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Dave Airliecdd55a22007-07-11 16:32:08 +10001072 struct drm_device_dma *dma = dev->dma;
1073 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001074 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 unsigned long offset;
1076 unsigned long agp_offset;
1077 int count;
1078 int order;
1079 int size;
1080 int alignment;
1081 int page_order;
1082 int total;
1083 int byte_count;
1084 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001085 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 if (!drm_core_check_feature(dev, DRIVER_SG))
Chris Wilson69fdf422018-09-13 20:20:50 +01001088 return -EOPNOTSUPP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089
1090 if (!dma)
1091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Dave Airlied985c102006-01-02 21:32:48 +11001093 if (!capable(CAP_SYS_ADMIN))
1094 return -EPERM;
1095
Dave Airlied59431b2005-07-10 15:00:06 +10001096 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001097 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 size = 1 << order;
1099
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 alignment = (request->flags & _DRM_PAGE_ALIGN)
1101 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1103 total = PAGE_SIZE << page_order;
1104
1105 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001106 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 DRM_DEBUG("count: %d\n", count);
1109 DRM_DEBUG("order: %d\n", order);
1110 DRM_DEBUG("size: %d\n", size);
1111 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1112 DRM_DEBUG("alignment: %d\n", alignment);
1113 DRM_DEBUG("page_order: %d\n", page_order);
1114 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Daniel Vetter2177a212013-12-16 11:21:06 +01001119 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001121 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return -EBUSY;
1123 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001125 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Dave Airlie30e2fb12006-02-02 19:37:46 +11001127 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001130 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 atomic_dec(&dev->buf_alloc);
1132 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
1135 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001136 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001137 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return -EINVAL;
1139 }
1140
Markus Elfringb5a2ecd2016-09-19 17:30:31 +02001141 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001143 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001144 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return -ENOMEM;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 entry->buf_size = size;
1149 entry->page_order = page_order;
1150
1151 offset = 0;
1152
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001153 while (entry->buf_count < count) {
1154 buf = &entry->buflist[entry->buf_count];
1155 buf->idx = dma->buf_count + entry->buf_count;
1156 buf->total = alignment;
1157 buf->order = order;
1158 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001160 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001162 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001163 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001164 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 buf->waiting = 0;
1166 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001167 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001170 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Set count correctly so we free the proper amount. */
1173 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001174 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001175 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001176 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return -ENOMEM;
1178 }
1179
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 offset += alignment;
1183 entry->buf_count++;
1184 byte_count += PAGE_SIZE << page_order;
1185 }
1186
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001187 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Eric Anholt9a298b22009-03-24 12:23:04 -07001189 temp_buflist = krealloc(dma->buflist,
1190 (dma->buf_count + entry->buf_count) *
1191 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001194 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001195 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return -ENOMEM;
1198 }
1199 dma->buflist = temp_buflist;
1200
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1203 }
1204
1205 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001206 dma->seg_count += entry->seg_count;
1207 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 dma->byte_count += byte_count;
1209
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001210 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1211 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Dave Airlie30e2fb12006-02-02 19:37:46 +11001213 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dave Airlied59431b2005-07-10 15:00:06 +10001215 request->count = entry->buf_count;
1216 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 dma->flags = _DRM_DMA_USE_SG;
1219
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001220 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return 0;
1222}
1223
1224/**
1225 * Add buffers for DMA transfers (ioctl).
1226 *
1227 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001228 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001230 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * \return zero on success or a negative number on failure.
1232 *
1233 * According with the memory type specified in drm_buf_desc::flags and the
1234 * build options, it dispatches the call either to addbufs_agp(),
1235 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1236 * PCI memory respectively.
1237 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001238int drm_legacy_addbufs(struct drm_device *dev, void *data,
1239 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Eric Anholtc153f452007-09-03 12:06:45 +10001241 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001242 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001243
Daniel Vetterfa538642016-08-03 21:11:10 +02001244 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001245 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001248 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001250#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001251 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001252 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 else
1254#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001255 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001256 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001257 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001258 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001260 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001261
Dave Airlied59431b2005-07-10 15:00:06 +10001262 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265/**
1266 * Get information about the buffer mappings.
1267 *
1268 * This was originally mean for debugging purposes, or by a sophisticated
1269 * client library to determine how best to use the available buffers (e.g.,
1270 * large buffers can be used for image transfer).
1271 *
1272 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001273 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * \param cmd command.
1275 * \param arg pointer to a drm_buf_info structure.
1276 * \return zero on success or a negative number on failure.
1277 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001278 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 * lock, preventing of allocating more buffers after this call. Information
1280 * about each requested buffer is then copied into user space.
1281 */
Al Viro5c7640a2017-05-24 17:54:09 -04001282int __drm_legacy_infobufs(struct drm_device *dev,
1283 void *data, int *p,
1284 int (*f)(void *, int, struct drm_buf_entry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Dave Airliecdd55a22007-07-11 16:32:08 +10001286 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int i;
1288 int count;
1289
Daniel Vetterfa538642016-08-03 21:11:10 +02001290 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001291 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001294 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296 if (!dma)
1297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Daniel Vetter2177a212013-12-16 11:21:06 +01001299 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001300 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001301 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -EBUSY;
1303 }
1304 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001305 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1308 if (dma->bufs[i].buf_count)
1309 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Al Viro5c7640a2017-05-24 17:54:09 -04001314 if (*p >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001315 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
Al Viro5c7640a2017-05-24 17:54:09 -04001316 struct drm_buf_entry *from = &dma->bufs[i];
1317 if (from->buf_count) {
1318 if (f(data, count, from) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 DRM_DEBUG("%d %d %d %d %d\n",
1321 i,
1322 dma->bufs[i].buf_count,
1323 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001324 dma->bufs[i].low_mark,
1325 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ++count;
1327 }
1328 }
1329 }
Al Viro5c7640a2017-05-24 17:54:09 -04001330 *p = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 return 0;
1333}
1334
Al Viro5c7640a2017-05-24 17:54:09 -04001335static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1336{
1337 struct drm_buf_info *request = data;
1338 struct drm_buf_desc __user *to = &request->list[count];
1339 struct drm_buf_desc v = {.count = from->buf_count,
1340 .size = from->buf_size,
1341 .low_mark = from->low_mark,
1342 .high_mark = from->high_mark};
Dan Carpenter74b67ef2019-06-18 16:18:43 +03001343
1344 if (copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags)))
1345 return -EFAULT;
1346 return 0;
Al Viro5c7640a2017-05-24 17:54:09 -04001347}
1348
1349int drm_legacy_infobufs(struct drm_device *dev, void *data,
1350 struct drm_file *file_priv)
1351{
1352 struct drm_buf_info *request = data;
1353 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/**
1357 * Specifies a low and high water mark for buffer allocation
1358 *
1359 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001360 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * \param cmd command.
1362 * \param arg a pointer to a drm_buf_desc structure.
1363 * \return zero on success or a negative number on failure.
1364 *
1365 * Verifies that the size order is bounded between the admissible orders and
1366 * updates the respective drm_device_dma::bufs entry low and high water mark.
1367 *
1368 * \note This ioctl is deprecated and mostly never used.
1369 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001370int drm_legacy_markbufs(struct drm_device *dev, void *data,
1371 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Dave Airliecdd55a22007-07-11 16:32:08 +10001373 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001374 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001376 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Daniel Vetterfa538642016-08-03 21:11:10 +02001378 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001379 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001382 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001384 if (!dma)
1385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001388 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001389 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1391 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 entry = &dma->bufs[order];
1393
Eric Anholtc153f452007-09-03 12:06:45 +10001394 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001396 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EINVAL;
1398
David Herrmannb008c0f2014-07-23 17:26:36 +02001399 entry->low_mark = request->low_mark;
1400 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 return 0;
1403}
1404
1405/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 *
1408 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001409 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 * \param cmd command.
1411 * \param arg pointer to a drm_buf_free structure.
1412 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * Calls free_buffer() for each used buffer.
1415 * This function is primarily used for debugging.
1416 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001417int drm_legacy_freebufs(struct drm_device *dev, void *data,
1418 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Dave Airliecdd55a22007-07-11 16:32:08 +10001420 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001421 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int i;
1423 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001424 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Daniel Vetterfa538642016-08-03 21:11:10 +02001426 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001427 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001430 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001432 if (!dma)
1433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Eric Anholtc153f452007-09-03 12:06:45 +10001435 DRM_DEBUG("%d\n", request->count);
1436 for (i = 0; i < request->count; i++) {
1437 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 if (idx < 0 || idx >= dma->buf_count) {
1440 DRM_ERROR("Index %d (of %d max)\n",
1441 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EINVAL;
1443 }
Gustavo A. R. Silvaa3780502018-10-16 11:55:49 +02001444 idx = array_index_nospec(idx, dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001446 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001448 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -EINVAL;
1450 }
Daniel Vettera2661622014-09-11 07:41:51 +02001451 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
1454 return 0;
1455}
1456
1457/**
1458 * Maps all of the DMA buffers into client-virtual space (ioctl).
1459 *
1460 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001461 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 * \param cmd command.
1463 * \param arg pointer to a drm_buf_map structure.
1464 * \return zero on success or a negative number on failure.
1465 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001466 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1467 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001468 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1469 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Al Viro87d3ce12017-05-25 16:24:20 -04001471int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1472 void __user **v,
1473 int (*f)(void *, int, unsigned long,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +00001474 struct drm_buf *),
1475 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Dave Airliecdd55a22007-07-11 16:32:08 +10001477 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 unsigned long virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int i;
1481
Daniel Vetterfa538642016-08-03 21:11:10 +02001482 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001483 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001486 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001488 if (!dma)
1489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Daniel Vetter2177a212013-12-16 11:21:06 +01001491 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001492 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001493 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EBUSY;
1495 }
1496 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001497 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Al Viro87d3ce12017-05-25 16:24:20 -04001499 if (*p >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001500 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001502 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001503 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001504 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001506 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 retcode = -EINVAL;
1508 goto done;
1509 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001510 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001511 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001512 MAP_SHARED,
1513 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001515 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001516 PROT_READ | PROT_WRITE,
1517 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001519 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* Real error */
1521 retcode = (signed long)virtual;
1522 goto done;
1523 }
Al Viro87d3ce12017-05-25 16:24:20 -04001524 *v = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526 for (i = 0; i < dma->buf_count; i++) {
Al Viro87d3ce12017-05-25 16:24:20 -04001527 if (f(data, i, virtual, dma->buflist[i]) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 retcode = -EFAULT;
1529 goto done;
1530 }
1531 }
1532 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 done:
Al Viro87d3ce12017-05-25 16:24:20 -04001534 *p = dma->buf_count;
1535 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 return retcode;
1538}
1539
Al Viro87d3ce12017-05-25 16:24:20 -04001540static int map_one_buf(void *data, int idx, unsigned long virtual,
1541 struct drm_buf *buf)
1542{
1543 struct drm_buf_map *request = data;
1544 unsigned long address = virtual + buf->offset; /* *** */
1545
1546 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1547 sizeof(request->list[0].idx)))
1548 return -EFAULT;
1549 if (copy_to_user(&request->list[idx].total, &buf->total,
1550 sizeof(request->list[0].total)))
1551 return -EFAULT;
1552 if (clear_user(&request->list[idx].used, sizeof(int)))
1553 return -EFAULT;
1554 if (copy_to_user(&request->list[idx].address, &address,
1555 sizeof(address)))
1556 return -EFAULT;
1557 return 0;
1558}
1559
1560int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1561 struct drm_file *file_priv)
1562{
1563 struct drm_buf_map *request = data;
1564 return __drm_legacy_mapbufs(dev, data, &request->count,
1565 &request->virtual, map_one_buf,
1566 file_priv);
1567}
1568
David Herrmann9fc5cde2014-08-29 12:12:28 +02001569int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001570 struct drm_file *file_priv)
1571{
Daniel Vetterfa538642016-08-03 21:11:10 +02001572 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001573 return -EOPNOTSUPP;
Daniel Vetter6eb92782013-08-08 15:41:29 +02001574
1575 if (dev->driver->dma_ioctl)
1576 return dev->driver->dma_ioctl(dev, data, file_priv);
1577 else
1578 return -EINVAL;
1579}
1580
David Herrmann9fc5cde2014-08-29 12:12:28 +02001581struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001582{
1583 struct drm_map_list *entry;
1584
1585 list_for_each_entry(entry, &dev->maplist, head) {
1586 if (entry->map && entry->map->type == _DRM_SHM &&
1587 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1588 return entry->map;
1589 }
1590 }
1591 return NULL;
1592}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001593EXPORT_SYMBOL(drm_legacy_getsarea);