blob: ef26ac57f0394c118ee1cc6bf953e79ee8cb831d [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
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040031#include <linux/export.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020032#include <linux/log2.h>
33#include <linux/mm.h>
34#include <linux/mman.h>
35#include <linux/nospec.h>
Daniel Vetter625c18d2020-04-03 13:06:10 +020036#include <linux/pci.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020037#include <linux/slab.h>
38#include <linux/uaccess.h>
39#include <linux/vmalloc.h>
40
David Millerf1a2a9b2009-02-18 15:41:02 -080041#include <asm/shmparam.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020042
43#include <drm/drm_agpsupport.h>
44#include <drm/drm_device.h>
45#include <drm/drm_drv.h>
46#include <drm/drm_file.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020047#include <drm/drm_print.h>
48
David Herrmann9fc5cde2014-08-29 12:12:28 +020049#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Gustavo A. R. Silvaa3780502018-10-16 11:55:49 +020051
Dave Airlie55910512007-07-11 16:53:40 +100052static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110053 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100054{
Dave Airlie55910512007-07-11 16:53:40 +100055 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100056 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110057 /*
58 * Because the kernel-userspace ABI is fixed at a 32-bit offset
Tormod Volden66aa6962011-05-30 19:45:43 +000059 * while PCI resources may live above that, we only compare the
60 * lower 32 bits of the map offset for maps of type
61 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
62 * It is assumed that if a driver have more than one resource
63 * of each type, the lower 32 bits are different.
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110064 */
65 if (!entry->map ||
66 map->type != entry->map->type ||
Daniel Vetter95c081c2016-06-21 10:54:12 +020067 entry->master != dev->master)
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110068 continue;
69 switch (map->type) {
70 case _DRM_SHM:
71 if (map->flags != _DRM_CONTAINS_LOCK)
72 break;
Tormod Volden66aa6962011-05-30 19:45:43 +000073 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110074 case _DRM_REGISTERS:
75 case _DRM_FRAME_BUFFER:
Tormod Volden66aa6962011-05-30 19:45:43 +000076 if ((entry->map->offset & 0xffffffff) ==
77 (map->offset & 0xffffffff))
78 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110079 default: /* Make gcc happy */
80 ;
Dave Airlie836cf042005-07-10 19:27:04 +100081 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110082 if (entry->map->offset == map->offset)
83 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100084 }
85
86 return NULL;
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Dave Airliee0be4282007-07-12 10:26:44 +100089static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080090 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100091{
David Millerf1a2a9b2009-02-18 15:41:02 -080092 int use_hashed_handle, shift;
93 unsigned long add;
94
Dave Airliec2604ce2006-08-12 16:03:26 +100095#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100096 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
97#elif (BITS_PER_LONG == 32)
98 use_hashed_handle = hashed_handle;
99#else
100#error Unsupported long size. Neither 64 nor 32 bits.
101#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000102
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000103 if (!use_hashed_handle) {
104 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100105 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000106 ret = drm_ht_insert_item(&dev->map_hash, hash);
107 if (ret != -EINVAL)
108 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000109 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800110
111 shift = 0;
112 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
113 if (shm && (SHMLBA > PAGE_SIZE)) {
114 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
115
116 /* For shared memory, we have to preserve the SHMLBA
117 * bits of the eventual vma->vm_pgoff value during
118 * mmap(). Otherwise we run into cache aliasing problems
119 * on some platforms. On these platforms, the pgoff of
120 * a mmap() request is used to pick a suitable virtual
121 * address for the mmap() region such that it will not
122 * cause cache aliasing problems.
123 *
124 * Therefore, make sure the SHMLBA relevant bits of the
125 * hash value we use are equal to those in the original
126 * kernel virtual address.
127 */
128 shift = bits;
129 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
130 }
131
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000132 return drm_ht_just_insert_please(&dev->map_hash, hash,
133 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800134 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000135}
Dave Airlie9a186642005-06-23 21:29:18 +1000136
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100137/*
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100138 * Core function to create a range of memory available for mapping by a
139 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *
141 * Adjusts the memory offset to its absolute value according to the mapping
142 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
143 * applicable and if supported by the kernel.
144 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000145static int drm_addmap_core(struct drm_device *dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000146 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000147 enum drm_map_flags flags,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000148 struct drm_map_list **maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100150 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000151 struct drm_map_list *list;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000152 unsigned long user_token;
153 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Eric Anholt9a298b22009-03-24 12:23:04 -0700155 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -ENOMEM;
158
Dave Airlie7ab98402005-07-10 16:56:52 +1000159 map->offset = offset;
160 map->size = size;
161 map->flags = flags;
162 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /* Only allow shared memory to be removable since we only keep enough
165 * book keeping information about shared memory to allow for removal
166 * when processes fork.
167 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700169 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EINVAL;
171 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100172 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
173 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000174
175 /* page-align _DRM_SHM maps. They are allocated here so there is no security
176 * hole created by that and it works around various broken drivers that use
177 * a non-aligned quantity to map the SAREA. --BenH
178 */
179 if (map->type == _DRM_SHM)
180 map->size = PAGE_ALIGN(map->size);
181
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100182 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700183 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EINVAL;
185 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 map->handle = NULL;
188
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 case _DRM_REGISTERS:
191 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600192#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100193 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700195 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
197 }
198#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000199 /* Some drivers preinitialize some maps, without the X Server
200 * needing to be aware of it. Therefore, we just return success
201 * when the server tries to create a duplicate map.
202 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000203 list = drm_find_matching_map(dev, map);
204 if (list != NULL) {
205 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000206 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 "mismatched sizes, (%ld vs %ld)\n",
208 map->type, map->size,
209 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000210 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000211 }
212
Eric Anholt9a298b22009-03-24 12:23:04 -0700213 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000214 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000215 return 0;
216 }
217
Daniel Vetter28185642013-08-08 15:41:27 +0200218 if (map->type == _DRM_FRAME_BUFFER ||
219 (map->flags & _DRM_WRITE_COMBINING)) {
220 map->mtrr =
221 arch_phys_wc_add(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Scott Thompson0769d392007-08-25 18:17:49 +1000223 if (map->type == _DRM_REGISTERS) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000224 if (map->flags & _DRM_WRITE_COMBINING)
225 map->handle = ioremap_wc(map->offset,
226 map->size);
227 else
228 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000229 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700230 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000231 return -ENOMEM;
232 }
233 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100237 list = drm_find_matching_map(dev, map);
238 if (list != NULL) {
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000239 if (list->map->size != map->size) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100240 DRM_DEBUG("Matching maps of type %d with "
241 "mismatched sizes, (%ld vs %ld)\n",
242 map->type, map->size, list->map->size);
243 list->map->size = map->size;
244 }
245
Eric Anholt9a298b22009-03-24 12:23:04 -0700246 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100247 *maplist = list;
248 return 0;
249 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100250 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 DRM_DEBUG("%lu %d %p\n",
Daniel Vetter04420c92013-07-10 14:11:57 +0200252 map->size, order_base_2(map->size), map->handle);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700254 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -ENOMEM;
256 }
257 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* Prevent a 2nd X Server from creating a 2nd lock */
Daniel Vetter95c081c2016-06-21 10:54:12 +0200260 if (dev->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700262 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EBUSY;
264 }
Daniel Vetter95c081c2016-06-21 10:54:12 +0200265 dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100268 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000269 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100270 int valid = 0;
271
Daniel Vetterd9906752013-12-11 11:34:35 +0100272 if (!dev->agp) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700273 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100276#ifdef __alpha__
277 map->offset += dev->hose->mem_space->start;
278#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000279 /* In some cases (i810 driver), user space may have already
280 * added the AGP base itself, because dev->agp->base previously
281 * only got set during AGP enable. So, only add the base
282 * address if the map's offset isn't already within the
283 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100284 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000285 if (map->offset < dev->agp->base ||
286 map->offset > dev->agp->base +
287 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
288 map->offset += dev->agp->base;
289 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100290 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
291
292 /* This assumes the DRM is in total control of AGP space.
293 * It's not always the case as AGP can be in the control
294 * of user space (i.e. i810 driver). So this loop will get
295 * skipped and we double check that dev->agp->memory is
296 * actually set as well as being invalid before EPERM'ing
297 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000298 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100299 if ((map->offset >= entry->bound) &&
300 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
301 valid = 1;
302 break;
303 }
304 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000305 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700306 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100307 return -EPERM;
308 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100309 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
310 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case _DRM_SCATTER_GATHER:
315 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700316 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -EINVAL;
318 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000319 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000322 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000323 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000324 * casting it down to 32 bits is no problem, but we
325 * need to point to a 64bit variable first. */
Chris Wilson8e4ff9b2020-02-02 17:16:32 +0000326 map->handle = dma_alloc_coherent(&dev->pdev->dev,
327 map->size,
328 &map->offset,
329 GFP_KERNEL);
330 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700331 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000332 return -ENOMEM;
333 }
334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700336 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -EINVAL;
338 }
339
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400340 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700342 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100343 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700344 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -EINVAL;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 list->map = map;
348
Dave Airlie30e2fb12006-02-02 19:37:46 +1100349 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000350 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000351
Dave Airlied1f2b552005-08-05 22:11:22 +1000352 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100353 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000354 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
355 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800356 ret = drm_map_handle(dev, &list->hash, user_token, 0,
357 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000358 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700359 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100360 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700361 kfree(map);
362 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000363 mutex_unlock(&dev->struct_mutex);
364 return ret;
365 }
366
Thomas Hellstrom15450852007-02-08 16:14:05 +1100367 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100368 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000370 if (!(map->flags & _DRM_DRIVER))
Daniel Vetter95c081c2016-06-21 10:54:12 +0200371 list->master = dev->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000372 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000373 return 0;
Thierry Redingafe0f692014-04-29 11:44:38 +0200374}
Dave Airlie89625eb2005-09-05 21:23:23 +1000375
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000376int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset,
David Herrmann9fc5cde2014-08-29 12:12:28 +0200377 unsigned int size, enum drm_map_type type,
378 enum drm_map_flags flags, struct drm_local_map **map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000379{
Dave Airlie55910512007-07-11 16:53:40 +1000380 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000381 int rc;
382
383 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
384 if (!rc)
385 *map_ptr = list->map;
386 return rc;
387}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200388EXPORT_SYMBOL(drm_legacy_addmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000389
Jani Nikulac7642682018-12-28 15:04:46 +0200390struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
391 unsigned int token)
392{
393 struct drm_map_list *_entry;
394 list_for_each_entry(_entry, &dev->maplist, head)
395 if (_entry->user_token == token)
396 return _entry->map;
397 return NULL;
398}
399EXPORT_SYMBOL(drm_legacy_findmap);
400
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100401/*
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100402 * Ioctl to specify a range of memory that is available for mapping by a
403 * non-root process.
404 *
405 * \param inode device inode.
406 * \param file_priv DRM file private.
407 * \param cmd command.
408 * \param arg pointer to a drm_map structure.
409 * \return zero on success or a negative value on error.
410 *
411 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200412int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
413 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000414{
Eric Anholtc153f452007-09-03 12:06:45 +1000415 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000416 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000417 int err;
418
Dave Airlie7c1c2872008-11-28 14:22:24 +1000419 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100420 return -EPERM;
421
Daniel Vettere975eef2016-04-26 19:29:37 +0200422 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200423 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100424 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200425
Eric Anholtc153f452007-09-03 12:06:45 +1000426 err = drm_addmap_core(dev, map->offset, map->size, map->type,
427 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000428
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000430 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000431
Dave Airlie67e1a012005-10-24 18:41:39 +1000432 /* 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 +1000433 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000434
435 /*
436 * It appears that there are no users of this value whatsoever --
437 * drmAddMap just discards it. Let's not encourage its use.
438 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
439 * it's not a real mtrr index anymore.)
440 */
441 map->mtrr = -1;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200446/*
447 * Get a mapping information.
448 *
449 * \param inode device inode.
450 * \param file_priv DRM file private.
451 * \param cmd command.
452 * \param arg user argument, pointing to a drm_map structure.
453 *
454 * \return zero on success or a negative number on failure.
455 *
456 * Searches for the mapping with the specified offset and copies its information
457 * into userspace
458 */
459int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
460 struct drm_file *file_priv)
461{
462 struct drm_map *map = data;
463 struct drm_map_list *r_list = NULL;
464 struct list_head *list;
465 int idx;
466 int i;
467
Daniel Vettere975eef2016-04-26 19:29:37 +0200468 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200469 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100470 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200471
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200472 idx = map->offset;
473 if (idx < 0)
474 return -EINVAL;
475
476 i = 0;
477 mutex_lock(&dev->struct_mutex);
478 list_for_each(list, &dev->maplist) {
479 if (i == idx) {
480 r_list = list_entry(list, struct drm_map_list, head);
481 break;
482 }
483 i++;
484 }
485 if (!r_list || !r_list->map) {
486 mutex_unlock(&dev->struct_mutex);
487 return -EINVAL;
488 }
489
490 map->offset = r_list->map->offset;
491 map->size = r_list->map->size;
492 map->type = r_list->map->type;
493 map->flags = r_list->map->flags;
494 map->handle = (void *)(unsigned long) r_list->user_token;
495 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
496
497 mutex_unlock(&dev->struct_mutex);
498
499 return 0;
500}
501
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100502/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * Remove a map private from list and deallocate resources if the mapping
504 * isn't in use.
505 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * Searches the map on drm_device::maplist, removes it from the list, see if
Matt Roper1e55a532019-02-01 17:23:26 -0800507 * it's being used, and free any associated resource (such as MTRR's) if it's not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * being on use.
509 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200510 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200512int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Dave Airlie55910512007-07-11 16:53:40 +1000514 struct drm_map_list *r_list = NULL, *list_t;
Dave Airliebd1b3312007-05-26 05:01:51 +1000515 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000516 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Dave Airlie836cf042005-07-10 19:27:04 +1000518 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000519 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000520 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000521 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000522 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100523 drm_ht_remove_key(&dev->map_hash,
524 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700525 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000526 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Dave Airlie836cf042005-07-10 19:27:04 +1000530
Dave Airliebd1b3312007-05-26 05:01:51 +1000531 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000532 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000533
534 switch (map->type) {
535 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100536 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000537 /* FALLTHROUGH */
538 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200539 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000540 break;
541 case _DRM_SHM:
542 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000543 if (master) {
544 if (dev->sigdata.lock == master->lock.hw_lock)
545 dev->sigdata.lock = NULL;
546 master->lock.hw_lock = NULL; /* SHM removed */
547 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100548 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000549 }
Dave Airlie836cf042005-07-10 19:27:04 +1000550 break;
551 case _DRM_AGP:
552 case _DRM_SCATTER_GATHER:
553 break;
554 case _DRM_CONSISTENT:
Chris Wilson8e4ff9b2020-02-02 17:16:32 +0000555 dma_free_coherent(&dev->pdev->dev,
556 map->size,
557 map->handle,
558 map->offset);
Dave Airlie836cf042005-07-10 19:27:04 +1000559 break;
560 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700561 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200565EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000566
Daniel Vetter40647e42016-04-27 09:20:18 +0200567void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000568{
Daniel Vetter40647e42016-04-27 09:20:18 +0200569 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200570 !drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200571 return;
Dave Airlie836cf042005-07-10 19:27:04 +1000572
Dave Airlie30e2fb12006-02-02 19:37:46 +1100573 mutex_lock(&dev->struct_mutex);
Daniel Vetter40647e42016-04-27 09:20:18 +0200574 drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100575 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000576}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200577EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000578
Daniel Vetter40647e42016-04-27 09:20:18 +0200579void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
580{
581 struct drm_map_list *r_list, *list_temp;
582
Daniel Vetterfa538642016-08-03 21:11:10 +0200583 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200584 return;
585
586 mutex_lock(&dev->struct_mutex);
587 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
588 if (r_list->master == master) {
589 drm_legacy_rmmap_locked(dev, r_list->map);
590 r_list = NULL;
591 }
592 }
593 mutex_unlock(&dev->struct_mutex);
594}
595
Dave Airlie35a28022019-04-23 08:45:12 +1000596void drm_legacy_rmmaps(struct drm_device *dev)
597{
598 struct drm_map_list *r_list, *list_temp;
599
600 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
601 drm_legacy_rmmap(dev, r_list->map);
602}
603
Dave Airlie836cf042005-07-10 19:27:04 +1000604/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
605 * the last close of the device, and this is necessary for cleanup when things
606 * exit uncleanly. Therefore, having userland manually remove mappings seems
607 * like a pointless exercise since they're going away anyway.
608 *
609 * One use case might be after addmap is allowed for normal users for SHM and
610 * gets used by drivers that the server doesn't need to care about. This seems
611 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100612 *
613 * \param inode device inode.
614 * \param file_priv DRM file private.
615 * \param cmd command.
616 * \param arg pointer to a struct drm_map structure.
617 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000618 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200619int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
620 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000621{
Eric Anholtc153f452007-09-03 12:06:45 +1000622 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100623 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000624 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000625 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000626
Daniel Vettere975eef2016-04-26 19:29:37 +0200627 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200628 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100629 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200630
Dave Airlie30e2fb12006-02-02 19:37:46 +1100631 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000632 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000633 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000634 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000635 r_list->map->flags & _DRM_REMOVABLE) {
636 map = r_list->map;
637 break;
638 }
639 }
640
Matt Roper1e55a532019-02-01 17:23:26 -0800641 /* List has wrapped around to the head pointer, or it's empty we didn't
Dave Airlie836cf042005-07-10 19:27:04 +1000642 * find anything.
643 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000644 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100645 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000646 return -EINVAL;
647 }
648
Dave Airlie836cf042005-07-10 19:27:04 +1000649 /* Register and framebuffer maps are permanent */
650 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100651 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000652 return 0;
653 }
654
David Herrmann9fc5cde2014-08-29 12:12:28 +0200655 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000656
Dave Airlie30e2fb12006-02-02 19:37:46 +1100657 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000658
659 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100662/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * Cleanup after an error on one of the addbufs() functions.
664 *
Dave Airlie836cf042005-07-10 19:27:04 +1000665 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * \param entry buffer entry where the error occurred.
667 *
668 * Frees any pages and buffers associated with the given entry.
669 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000670static void drm_cleanup_buf_error(struct drm_device *dev,
671 struct drm_buf_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int i;
674
675 if (entry->seg_count) {
676 for (i = 0; i < entry->seg_count; i++) {
677 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100678 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700681 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 entry->seg_count = 0;
684 }
685
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 if (entry->buf_count) {
687 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700688 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700690 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 entry->buf_count = 0;
693 }
694}
695
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200696#if IS_ENABLED(CONFIG_AGP)
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100697/*
Dave Airlied59431b2005-07-10 15:00:06 +1000698 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000700 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000701 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * After some sanity checks creates a drm_buf structure for each buffer and
705 * reallocates the buffer list of the same size order to accommodate the new
706 * buffers.
707 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200708int drm_legacy_addbufs_agp(struct drm_device *dev,
709 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Dave Airliecdd55a22007-07-11 16:32:08 +1000711 struct drm_device_dma *dma = dev->dma;
712 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000713 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000714 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned long offset;
716 unsigned long agp_offset;
717 int count;
718 int order;
719 int size;
720 int alignment;
721 int page_order;
722 int total;
723 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100724 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000725 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 if (!dma)
728 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dave Airlied59431b2005-07-10 15:00:06 +1000730 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200731 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 size = 1 << order;
733
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 alignment = (request->flags & _DRM_PAGE_ALIGN)
735 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
737 total = PAGE_SIZE << page_order;
738
739 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000740 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 DRM_DEBUG("count: %d\n", count);
743 DRM_DEBUG("order: %d\n", order);
744 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100745 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 DRM_DEBUG("alignment: %d\n", alignment);
747 DRM_DEBUG("page_order: %d\n", page_order);
748 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
751 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Dave Airlie54ba2f72007-02-10 12:07:47 +1100753 /* Make sure buffers are located in AGP memory that we own */
754 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000755 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100756 if ((agp_offset >= agp_entry->bound) &&
757 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
758 valid = 1;
759 break;
760 }
761 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000762 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100763 DRM_DEBUG("zone invalid\n");
764 return -EINVAL;
765 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100766 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100768 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return -EBUSY;
770 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100772 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Dave Airlie30e2fb12006-02-02 19:37:46 +1100774 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100777 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 atomic_dec(&dev->buf_alloc);
779 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
782 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100783 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -EINVAL;
786 }
787
Markus Elfring81a44132016-09-19 17:24:20 +0200788 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100790 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -ENOMEM;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 entry->buf_size = size;
796 entry->page_order = page_order;
797
798 offset = 0;
799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 while (entry->buf_count < count) {
801 buf = &entry->buflist[entry->buf_count];
802 buf->idx = dma->buf_count + entry->buf_count;
803 buf->total = alignment;
804 buf->order = order;
805 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 buf->bus_address = agp_offset + offset;
809 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 buf->waiting = 0;
812 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000813 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400816 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* Set count correctly so we free the proper amount. */
819 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100821 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -ENOMEM;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 offset += alignment;
829 entry->buf_count++;
830 byte_count += PAGE_SIZE << page_order;
831 }
832
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Eric Anholt9a298b22009-03-24 12:23:04 -0700835 temp_buflist = krealloc(dma->buflist,
836 (dma->buf_count + entry->buf_count) *
837 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100841 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -ENOMEM;
844 }
845 dma->buflist = temp_buflist;
846
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
849 }
850
851 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100852 dma->seg_count += entry->seg_count;
853 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 dma->byte_count += byte_count;
855
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
857 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Dave Airlie30e2fb12006-02-02 19:37:46 +1100859 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dave Airlied59431b2005-07-10 15:00:06 +1000861 request->count = entry->buf_count;
862 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 dma->flags = _DRM_DMA_USE_AGP;
865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
868}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200869EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200870#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871
David Herrmann9fc5cde2014-08-29 12:12:28 +0200872int drm_legacy_addbufs_pci(struct drm_device *dev,
873 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Dave Airliecdd55a22007-07-11 16:32:08 +1000875 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int count;
877 int order;
878 int size;
879 int total;
880 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000881 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100882 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000883 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 int alignment;
885 unsigned long offset;
886 int i;
887 int byte_count;
888 int page_count;
889 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000890 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +0100893 return -EOPNOTSUPP;
Dave Airlied985c102006-01-02 21:32:48 +1100894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 if (!dma)
896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Dave Airlied985c102006-01-02 21:32:48 +1100898 if (!capable(CAP_SYS_ADMIN))
899 return -EPERM;
900
Dave Airlied59431b2005-07-10 15:00:06 +1000901 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200902 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 size = 1 << order;
904
Daniel Vettera344a7e2011-10-26 00:54:41 +0200905 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
906 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
909 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Dave Airlied59431b2005-07-10 15:00:06 +1000911 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
914 total = PAGE_SIZE << page_order;
915
Daniel Vetter2177a212013-12-16 11:21:06 +0100916 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100918 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EBUSY;
920 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100922 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dave Airlie30e2fb12006-02-02 19:37:46 +1100924 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100927 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return -ENOMEM; /* May only call once for each order */
930 }
931
932 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100933 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000934 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -EINVAL;
936 }
937
Markus Elfringed6dee42016-09-19 17:17:34 +0200938 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100940 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000941 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -ENOMEM;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Markus Elfringed6dee42016-09-19 17:17:34 +0200945 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000946 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700947 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100948 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -ENOMEM;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* Keep the original pagelist until we know all the allocations
954 * have succeeded
955 */
Markus Elfring20274002016-09-19 17:07:06 +0200956 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
957 sizeof(*dma->pagelist),
958 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700960 kfree(entry->buflist);
961 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100962 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -ENOMEM;
965 }
966 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
968 DRM_DEBUG("pagelist: %d entries\n",
969 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 entry->page_order = page_order;
973 byte_count = 0;
974 page_count = 0;
975
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000977
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800978 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000979
Dave Airlieddf19b92006-03-19 18:56:12 +1100980 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* Set count correctly so we free the proper amount. */
982 entry->buf_count = count;
983 entry->seg_count = count;
984 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700985 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100986 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return -ENOMEM;
989 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100990 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 for (i = 0; i < (1 << page_order); i++) {
992 DRM_DEBUG("page %d @ 0x%08lx\n",
993 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100994 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100996 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 for (offset = 0;
999 offset + size <= total && entry->buf_count < count;
1000 offset += alignment, ++entry->buf_count) {
1001 buf = &entry->buflist[entry->buf_count];
1002 buf->idx = dma->buf_count + entry->buf_count;
1003 buf->total = alignment;
1004 buf->order = order;
1005 buf->used = 0;
1006 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +11001007 buf->address = (void *)(dmah->vaddr + offset);
1008 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 buf->waiting = 0;
1011 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001012 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001015 buf->dev_private = kzalloc(buf->dev_priv_size,
1016 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* Set count correctly so we free the proper amount. */
1019 entry->buf_count = count;
1020 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001022 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001023 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return -ENOMEM;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 DRM_DEBUG("buffer %d @ %p\n",
1029 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 byte_count += PAGE_SIZE << page_order;
1032 }
1033
Eric Anholt9a298b22009-03-24 12:23:04 -07001034 temp_buflist = krealloc(dma->buflist,
1035 (dma->buf_count + entry->buf_count) *
1036 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!temp_buflist) {
1038 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001040 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001041 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -ENOMEM;
1044 }
1045 dma->buflist = temp_buflist;
1046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1049 }
1050
Thomas Weber88393162010-03-16 11:47:56 +01001051 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * with the new one.
1053 */
1054 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001055 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 dma->pagelist = temp_pagelist;
1058
1059 dma->buf_count += entry->buf_count;
1060 dma->seg_count += entry->seg_count;
1061 dma->page_count += entry->seg_count << page_order;
1062 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1063
Dave Airlie30e2fb12006-02-02 19:37:46 +11001064 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dave Airlied59431b2005-07-10 15:00:06 +10001066 request->count = entry->buf_count;
1067 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
George Sapountzis3417f332006-10-24 12:03:04 -07001069 if (request->flags & _DRM_PCI_BUFFER_RO)
1070 dma->flags = _DRM_DMA_USE_PCI_RO;
1071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0;
1074
1075}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001076EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
David Herrmann9fc5cde2014-08-29 12:12:28 +02001078static int drm_legacy_addbufs_sg(struct drm_device *dev,
1079 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Dave Airliecdd55a22007-07-11 16:32:08 +10001081 struct drm_device_dma *dma = dev->dma;
1082 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001083 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 unsigned long offset;
1085 unsigned long agp_offset;
1086 int count;
1087 int order;
1088 int size;
1089 int alignment;
1090 int page_order;
1091 int total;
1092 int byte_count;
1093 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001094 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 if (!drm_core_check_feature(dev, DRIVER_SG))
Chris Wilson69fdf422018-09-13 20:20:50 +01001097 return -EOPNOTSUPP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098
1099 if (!dma)
1100 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Dave Airlied985c102006-01-02 21:32:48 +11001102 if (!capable(CAP_SYS_ADMIN))
1103 return -EPERM;
1104
Dave Airlied59431b2005-07-10 15:00:06 +10001105 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001106 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 size = 1 << order;
1108
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 alignment = (request->flags & _DRM_PAGE_ALIGN)
1110 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1112 total = PAGE_SIZE << page_order;
1113
1114 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001115 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 DRM_DEBUG("count: %d\n", count);
1118 DRM_DEBUG("order: %d\n", order);
1119 DRM_DEBUG("size: %d\n", size);
1120 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1121 DRM_DEBUG("alignment: %d\n", alignment);
1122 DRM_DEBUG("page_order: %d\n", page_order);
1123 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1126 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Daniel Vetter2177a212013-12-16 11:21:06 +01001128 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001130 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -EBUSY;
1132 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001134 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Dave Airlie30e2fb12006-02-02 19:37:46 +11001136 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001139 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140 atomic_dec(&dev->buf_alloc);
1141 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001145 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -EINVAL;
1148 }
1149
Markus Elfringb5a2ecd2016-09-19 17:30:31 +02001150 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001151 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001152 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001153 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return -ENOMEM;
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 entry->buf_size = size;
1158 entry->page_order = page_order;
1159
1160 offset = 0;
1161
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001162 while (entry->buf_count < count) {
1163 buf = &entry->buflist[entry->buf_count];
1164 buf->idx = dma->buf_count + entry->buf_count;
1165 buf->total = alignment;
1166 buf->order = order;
1167 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001169 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001172 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 buf->waiting = 0;
1175 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001176 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001179 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Set count correctly so we free the proper amount. */
1182 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001183 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001184 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001185 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -ENOMEM;
1187 }
1188
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001189 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 offset += alignment;
1192 entry->buf_count++;
1193 byte_count += PAGE_SIZE << page_order;
1194 }
1195
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Eric Anholt9a298b22009-03-24 12:23:04 -07001198 temp_buflist = krealloc(dma->buflist,
1199 (dma->buf_count + entry->buf_count) *
1200 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001203 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001204 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001205 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -ENOMEM;
1207 }
1208 dma->buflist = temp_buflist;
1209
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001210 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1212 }
1213
1214 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001215 dma->seg_count += entry->seg_count;
1216 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 dma->byte_count += byte_count;
1218
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1220 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Dave Airlie30e2fb12006-02-02 19:37:46 +11001222 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Dave Airlied59431b2005-07-10 15:00:06 +10001224 request->count = entry->buf_count;
1225 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 dma->flags = _DRM_DMA_USE_SG;
1228
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001229 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return 0;
1231}
1232
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * Add buffers for DMA transfers (ioctl).
1235 *
1236 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001237 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001239 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * \return zero on success or a negative number on failure.
1241 *
1242 * According with the memory type specified in drm_buf_desc::flags and the
1243 * build options, it dispatches the call either to addbufs_agp(),
1244 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1245 * PCI memory respectively.
1246 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001247int drm_legacy_addbufs(struct drm_device *dev, void *data,
1248 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Eric Anholtc153f452007-09-03 12:06:45 +10001250 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001251 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001252
Daniel Vetterfa538642016-08-03 21:11:10 +02001253 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001254 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001257 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001259#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001260 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001261 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 else
1263#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001264 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001265 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001266 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001267 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001269 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001270
Dave Airlied59431b2005-07-10 15:00:06 +10001271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * Get information about the buffer mappings.
1276 *
1277 * This was originally mean for debugging purposes, or by a sophisticated
1278 * client library to determine how best to use the available buffers (e.g.,
1279 * large buffers can be used for image transfer).
1280 *
1281 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001282 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 * \param cmd command.
1284 * \param arg pointer to a drm_buf_info structure.
1285 * \return zero on success or a negative number on failure.
1286 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001287 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * lock, preventing of allocating more buffers after this call. Information
1289 * about each requested buffer is then copied into user space.
1290 */
Al Viro5c7640a2017-05-24 17:54:09 -04001291int __drm_legacy_infobufs(struct drm_device *dev,
1292 void *data, int *p,
1293 int (*f)(void *, int, struct drm_buf_entry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Dave Airliecdd55a22007-07-11 16:32:08 +10001295 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int i;
1297 int count;
1298
Daniel Vetterfa538642016-08-03 21:11:10 +02001299 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001300 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001303 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 if (!dma)
1306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Daniel Vetter2177a212013-12-16 11:21:06 +01001308 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001309 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001310 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EBUSY;
1312 }
1313 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001314 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001316 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1317 if (dma->bufs[i].buf_count)
1318 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Al Viro5c7640a2017-05-24 17:54:09 -04001323 if (*p >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001324 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
Al Viro5c7640a2017-05-24 17:54:09 -04001325 struct drm_buf_entry *from = &dma->bufs[i];
1326 if (from->buf_count) {
1327 if (f(data, count, from) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001329 DRM_DEBUG("%d %d %d %d %d\n",
1330 i,
1331 dma->bufs[i].buf_count,
1332 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001333 dma->bufs[i].low_mark,
1334 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 ++count;
1336 }
1337 }
1338 }
Al Viro5c7640a2017-05-24 17:54:09 -04001339 *p = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 return 0;
1342}
1343
Al Viro5c7640a2017-05-24 17:54:09 -04001344static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1345{
1346 struct drm_buf_info *request = data;
1347 struct drm_buf_desc __user *to = &request->list[count];
1348 struct drm_buf_desc v = {.count = from->buf_count,
1349 .size = from->buf_size,
1350 .low_mark = from->low_mark,
1351 .high_mark = from->high_mark};
Dan Carpenter74b67ef2019-06-18 16:18:43 +03001352
1353 if (copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags)))
1354 return -EFAULT;
1355 return 0;
Al Viro5c7640a2017-05-24 17:54:09 -04001356}
1357
1358int drm_legacy_infobufs(struct drm_device *dev, void *data,
1359 struct drm_file *file_priv)
1360{
1361 struct drm_buf_info *request = data;
1362 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1363}
1364
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * Specifies a low and high water mark for buffer allocation
1367 *
1368 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001369 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 * \param cmd command.
1371 * \param arg a pointer to a drm_buf_desc structure.
1372 * \return zero on success or a negative number on failure.
1373 *
1374 * Verifies that the size order is bounded between the admissible orders and
1375 * updates the respective drm_device_dma::bufs entry low and high water mark.
1376 *
1377 * \note This ioctl is deprecated and mostly never used.
1378 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001379int drm_legacy_markbufs(struct drm_device *dev, void *data,
1380 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Dave Airliecdd55a22007-07-11 16:32:08 +10001382 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001383 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001385 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Daniel Vetterfa538642016-08-03 21:11:10 +02001387 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001388 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001391 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001393 if (!dma)
1394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001397 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001398 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001399 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 entry = &dma->bufs[order];
1402
Eric Anholtc153f452007-09-03 12:06:45 +10001403 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001405 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return -EINVAL;
1407
David Herrmannb008c0f2014-07-23 17:26:36 +02001408 entry->low_mark = request->low_mark;
1409 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return 0;
1412}
1413
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001414/*
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
1417 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001418 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * \param cmd command.
1420 * \param arg pointer to a drm_buf_free structure.
1421 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * Calls free_buffer() for each used buffer.
1424 * This function is primarily used for debugging.
1425 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001426int drm_legacy_freebufs(struct drm_device *dev, void *data,
1427 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Dave Airliecdd55a22007-07-11 16:32:08 +10001429 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001430 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int i;
1432 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001433 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Daniel Vetterfa538642016-08-03 21:11:10 +02001435 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001436 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001439 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001441 if (!dma)
1442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Eric Anholtc153f452007-09-03 12:06:45 +10001444 DRM_DEBUG("%d\n", request->count);
1445 for (i = 0; i < request->count; i++) {
1446 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 if (idx < 0 || idx >= dma->buf_count) {
1449 DRM_ERROR("Index %d (of %d max)\n",
1450 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -EINVAL;
1452 }
Gustavo A. R. Silvaa3780502018-10-16 11:55:49 +02001453 idx = array_index_nospec(idx, dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001455 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001456 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001457 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -EINVAL;
1459 }
Daniel Vettera2661622014-09-11 07:41:51 +02001460 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
1463 return 0;
1464}
1465
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * Maps all of the DMA buffers into client-virtual space (ioctl).
1468 *
1469 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001470 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * \param cmd command.
1472 * \param arg pointer to a drm_buf_map structure.
1473 * \return zero on success or a negative number on failure.
1474 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001475 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1476 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001477 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1478 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
Al Viro87d3ce12017-05-25 16:24:20 -04001480int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1481 void __user **v,
1482 int (*f)(void *, int, unsigned long,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +00001483 struct drm_buf *),
1484 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Dave Airliecdd55a22007-07-11 16:32:08 +10001486 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 unsigned long virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 int i;
1490
Daniel Vetterfa538642016-08-03 21:11:10 +02001491 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001492 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001495 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 if (!dma)
1498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Daniel Vetter2177a212013-12-16 11:21:06 +01001500 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001502 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EBUSY;
1504 }
1505 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001506 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Al Viro87d3ce12017-05-25 16:24:20 -04001508 if (*p >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001509 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001510 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001511 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001512 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001513 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 retcode = -EINVAL;
1517 goto done;
1518 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001519 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001521 MAP_SHARED,
1522 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001524 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001525 PROT_READ | PROT_WRITE,
1526 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001528 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* Real error */
1530 retcode = (signed long)virtual;
1531 goto done;
1532 }
Al Viro87d3ce12017-05-25 16:24:20 -04001533 *v = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001535 for (i = 0; i < dma->buf_count; i++) {
Al Viro87d3ce12017-05-25 16:24:20 -04001536 if (f(data, i, virtual, dma->buflist[i]) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 retcode = -EFAULT;
1538 goto done;
1539 }
1540 }
1541 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001542 done:
Al Viro87d3ce12017-05-25 16:24:20 -04001543 *p = dma->buf_count;
1544 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 return retcode;
1547}
1548
Al Viro87d3ce12017-05-25 16:24:20 -04001549static int map_one_buf(void *data, int idx, unsigned long virtual,
1550 struct drm_buf *buf)
1551{
1552 struct drm_buf_map *request = data;
1553 unsigned long address = virtual + buf->offset; /* *** */
1554
1555 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1556 sizeof(request->list[0].idx)))
1557 return -EFAULT;
1558 if (copy_to_user(&request->list[idx].total, &buf->total,
1559 sizeof(request->list[0].total)))
1560 return -EFAULT;
1561 if (clear_user(&request->list[idx].used, sizeof(int)))
1562 return -EFAULT;
1563 if (copy_to_user(&request->list[idx].address, &address,
1564 sizeof(address)))
1565 return -EFAULT;
1566 return 0;
1567}
1568
1569int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1570 struct drm_file *file_priv)
1571{
1572 struct drm_buf_map *request = data;
1573 return __drm_legacy_mapbufs(dev, data, &request->count,
1574 &request->virtual, map_one_buf,
1575 file_priv);
1576}
1577
David Herrmann9fc5cde2014-08-29 12:12:28 +02001578int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001579 struct drm_file *file_priv)
1580{
Daniel Vetterfa538642016-08-03 21:11:10 +02001581 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001582 return -EOPNOTSUPP;
Daniel Vetter6eb92782013-08-08 15:41:29 +02001583
1584 if (dev->driver->dma_ioctl)
1585 return dev->driver->dma_ioctl(dev, data, file_priv);
1586 else
1587 return -EINVAL;
1588}
1589
David Herrmann9fc5cde2014-08-29 12:12:28 +02001590struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001591{
1592 struct drm_map_list *entry;
1593
1594 list_for_each_entry(entry, &dev->maplist, head) {
1595 if (entry->map && entry->map->type == _DRM_SHM &&
1596 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1597 return entry->map;
1598 }
1599 }
1600 return NULL;
1601}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001602EXPORT_SYMBOL(drm_legacy_getsarea);