blob: 410623fc30e01473d4123986ecf0459a93b929b4 [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;
Suraj Upadhyay948de8422020-07-02 18:53:32 +053056
Dave Airliebd1b3312007-05-26 05:01:51 +100057 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110058 /*
59 * Because the kernel-userspace ABI is fixed at a 32-bit offset
Tormod Volden66aa6962011-05-30 19:45:43 +000060 * while PCI resources may live above that, we only compare the
61 * lower 32 bits of the map offset for maps of type
62 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
63 * It is assumed that if a driver have more than one resource
64 * of each type, the lower 32 bits are different.
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110065 */
66 if (!entry->map ||
67 map->type != entry->map->type ||
Daniel Vetter95c081c2016-06-21 10:54:12 +020068 entry->master != dev->master)
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110069 continue;
70 switch (map->type) {
71 case _DRM_SHM:
72 if (map->flags != _DRM_CONTAINS_LOCK)
73 break;
Tormod Volden66aa6962011-05-30 19:45:43 +000074 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110075 case _DRM_REGISTERS:
76 case _DRM_FRAME_BUFFER:
Tormod Volden66aa6962011-05-30 19:45:43 +000077 if ((entry->map->offset & 0xffffffff) ==
78 (map->offset & 0xffffffff))
79 return entry;
Gustavo A. R. Silva8ce9daf2020-11-20 12:35:17 -060080 break;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110081 default: /* Make gcc happy */
Fabio M. De Francesco42be7ca2021-04-17 18:15:52 +020082 break;
Dave Airlie836cf042005-07-10 19:27:04 +100083 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110084 if (entry->map->offset == map->offset)
85 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100086 }
87
88 return NULL;
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Dave Airliee0be4282007-07-12 10:26:44 +100091static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080092 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100093{
David Millerf1a2a9b2009-02-18 15:41:02 -080094 int use_hashed_handle, shift;
95 unsigned long add;
96
Dave Airliec2604ce2006-08-12 16:03:26 +100097#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100098 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
99#elif (BITS_PER_LONG == 32)
100 use_hashed_handle = hashed_handle;
101#else
102#error Unsupported long size. Neither 64 nor 32 bits.
103#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000104
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000105 if (!use_hashed_handle) {
106 int ret;
Suraj Upadhyay948de8422020-07-02 18:53:32 +0530107
Thomas Hellstrom15450852007-02-08 16:14:05 +1100108 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000109 ret = drm_ht_insert_item(&dev->map_hash, hash);
110 if (ret != -EINVAL)
111 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000112 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800113
114 shift = 0;
115 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
116 if (shm && (SHMLBA > PAGE_SIZE)) {
117 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
118
119 /* For shared memory, we have to preserve the SHMLBA
120 * bits of the eventual vma->vm_pgoff value during
121 * mmap(). Otherwise we run into cache aliasing problems
122 * on some platforms. On these platforms, the pgoff of
123 * a mmap() request is used to pick a suitable virtual
124 * address for the mmap() region such that it will not
125 * cause cache aliasing problems.
126 *
127 * Therefore, make sure the SHMLBA relevant bits of the
128 * hash value we use are equal to those in the original
129 * kernel virtual address.
130 */
131 shift = bits;
132 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
133 }
134
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000135 return drm_ht_just_insert_please(&dev->map_hash, hash,
136 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800137 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000138}
Dave Airlie9a186642005-06-23 21:29:18 +1000139
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100140/*
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100141 * Core function to create a range of memory available for mapping by a
142 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 *
144 * Adjusts the memory offset to its absolute value according to the mapping
145 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
146 * applicable and if supported by the kernel.
147 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000148static int drm_addmap_core(struct drm_device *dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000149 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000150 enum drm_map_flags flags,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000151 struct drm_map_list **maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100153 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000154 struct drm_map_list *list;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000155 unsigned long user_token;
156 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Eric Anholt9a298b22009-03-24 12:23:04 -0700158 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -ENOMEM;
161
Dave Airlie7ab98402005-07-10 16:56:52 +1000162 map->offset = offset;
163 map->size = size;
164 map->flags = flags;
165 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* Only allow shared memory to be removable since we only keep enough
168 * book keeping information about shared memory to allow for removal
169 * when processes fork.
170 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700172 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -EINVAL;
174 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100175 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
176 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000177
178 /* page-align _DRM_SHM maps. They are allocated here so there is no security
179 * hole created by that and it works around various broken drivers that use
180 * a non-aligned quantity to map the SAREA. --BenH
181 */
182 if (map->type == _DRM_SHM)
183 map->size = PAGE_ALIGN(map->size);
184
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100185 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700186 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 map->handle = NULL;
191
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 case _DRM_REGISTERS:
194 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600195#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100196 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700198 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EINVAL;
200 }
201#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000202 /* Some drivers preinitialize some maps, without the X Server
203 * needing to be aware of it. Therefore, we just return success
204 * when the server tries to create a duplicate map.
205 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000206 list = drm_find_matching_map(dev, map);
207 if (list != NULL) {
208 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000209 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 "mismatched sizes, (%ld vs %ld)\n",
211 map->type, map->size,
212 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000213 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000214 }
215
Eric Anholt9a298b22009-03-24 12:23:04 -0700216 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000217 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000218 return 0;
219 }
220
Daniel Vetter28185642013-08-08 15:41:27 +0200221 if (map->type == _DRM_FRAME_BUFFER ||
222 (map->flags & _DRM_WRITE_COMBINING)) {
223 map->mtrr =
224 arch_phys_wc_add(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Scott Thompson0769d392007-08-25 18:17:49 +1000226 if (map->type == _DRM_REGISTERS) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000227 if (map->flags & _DRM_WRITE_COMBINING)
228 map->handle = ioremap_wc(map->offset,
229 map->size);
230 else
231 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000232 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700233 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000234 return -ENOMEM;
235 }
236 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100240 list = drm_find_matching_map(dev, map);
241 if (list != NULL) {
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000242 if (list->map->size != map->size) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100243 DRM_DEBUG("Matching maps of type %d with "
244 "mismatched sizes, (%ld vs %ld)\n",
245 map->type, map->size, list->map->size);
246 list->map->size = map->size;
247 }
248
Eric Anholt9a298b22009-03-24 12:23:04 -0700249 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100250 *maplist = list;
251 return 0;
252 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100253 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 DRM_DEBUG("%lu %d %p\n",
Daniel Vetter04420c92013-07-10 14:11:57 +0200255 map->size, order_base_2(map->size), map->handle);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700257 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -ENOMEM;
259 }
260 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /* Prevent a 2nd X Server from creating a 2nd lock */
Daniel Vetter95c081c2016-06-21 10:54:12 +0200263 if (dev->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000264 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700265 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -EBUSY;
267 }
Daniel Vetter95c081c2016-06-21 10:54:12 +0200268 dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100271 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000272 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100273 int valid = 0;
274
Daniel Vetterd9906752013-12-11 11:34:35 +0100275 if (!dev->agp) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700276 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100277 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100279#ifdef __alpha__
280 map->offset += dev->hose->mem_space->start;
281#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000282 /* In some cases (i810 driver), user space may have already
283 * added the AGP base itself, because dev->agp->base previously
284 * only got set during AGP enable. So, only add the base
285 * address if the map's offset isn't already within the
286 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100287 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000288 if (map->offset < dev->agp->base ||
289 map->offset > dev->agp->base +
290 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
291 map->offset += dev->agp->base;
292 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100293 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
294
295 /* This assumes the DRM is in total control of AGP space.
296 * It's not always the case as AGP can be in the control
297 * of user space (i.e. i810 driver). So this loop will get
298 * skipped and we double check that dev->agp->memory is
299 * actually set as well as being invalid before EPERM'ing
300 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000301 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100302 if ((map->offset >= entry->bound) &&
303 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
304 valid = 1;
305 break;
306 }
307 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000308 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700309 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100310 return -EPERM;
311 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100312 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
313 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 case _DRM_SCATTER_GATHER:
318 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700319 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -EINVAL;
321 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000322 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000325 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000326 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000327 * casting it down to 32 bits is no problem, but we
Beatriz Martins de Carvalhoad6ce322021-04-18 15:48:23 +0100328 * need to point to a 64bit variable first.
329 */
Thomas Zimmermann36b73b02021-01-18 14:14:15 +0100330 map->handle = dma_alloc_coherent(dev->dev,
Chris Wilson8e4ff9b2020-02-02 17:16:32 +0000331 map->size,
332 &map->offset,
333 GFP_KERNEL);
334 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700335 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000336 return -ENOMEM;
337 }
338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700340 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EINVAL;
342 }
343
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400344 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700346 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100347 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700348 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EINVAL;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list->map = map;
352
Dave Airlie30e2fb12006-02-02 19:37:46 +1100353 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000354 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000355
Dave Airlied1f2b552005-08-05 22:11:22 +1000356 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100357 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000358 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
359 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800360 ret = drm_map_handle(dev, &list->hash, user_token, 0,
361 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000362 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700363 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100364 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700365 kfree(map);
366 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000367 mutex_unlock(&dev->struct_mutex);
368 return ret;
369 }
370
Thomas Hellstrom15450852007-02-08 16:14:05 +1100371 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100372 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Ben Skeggs2ff2e8a32009-05-26 10:35:52 +1000374 if (!(map->flags & _DRM_DRIVER))
Daniel Vetter95c081c2016-06-21 10:54:12 +0200375 list->master = dev->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000376 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000377 return 0;
Thierry Redingafe0f692014-04-29 11:44:38 +0200378}
Dave Airlie89625eb2005-09-05 21:23:23 +1000379
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000380int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset,
David Herrmann9fc5cde2014-08-29 12:12:28 +0200381 unsigned int size, enum drm_map_type type,
382 enum drm_map_flags flags, struct drm_local_map **map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000383{
Dave Airlie55910512007-07-11 16:53:40 +1000384 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000385 int rc;
386
387 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
388 if (!rc)
389 *map_ptr = list->map;
390 return rc;
391}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200392EXPORT_SYMBOL(drm_legacy_addmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000393
Jani Nikulac7642682018-12-28 15:04:46 +0200394struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
395 unsigned int token)
396{
397 struct drm_map_list *_entry;
Suraj Upadhyay948de8422020-07-02 18:53:32 +0530398
Jani Nikulac7642682018-12-28 15:04:46 +0200399 list_for_each_entry(_entry, &dev->maplist, head)
400 if (_entry->user_token == token)
401 return _entry->map;
402 return NULL;
403}
404EXPORT_SYMBOL(drm_legacy_findmap);
405
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100406/*
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100407 * Ioctl to specify a range of memory that is available for mapping by a
408 * non-root process.
409 *
410 * \param inode device inode.
411 * \param file_priv DRM file private.
412 * \param cmd command.
413 * \param arg pointer to a drm_map structure.
414 * \return zero on success or a negative value on error.
415 *
416 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200417int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
418 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000419{
Eric Anholtc153f452007-09-03 12:06:45 +1000420 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000421 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000422 int err;
423
Dave Airlie7c1c2872008-11-28 14:22:24 +1000424 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100425 return -EPERM;
426
Daniel Vettere975eef2016-04-26 19:29:37 +0200427 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200428 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100429 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200430
Eric Anholtc153f452007-09-03 12:06:45 +1000431 err = drm_addmap_core(dev, map->offset, map->size, map->type,
432 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000433
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000435 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000436
Dave Airlie67e1a012005-10-24 18:41:39 +1000437 /* 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 +1000438 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000439
440 /*
441 * It appears that there are no users of this value whatsoever --
442 * drmAddMap just discards it. Let's not encourage its use.
443 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
444 * it's not a real mtrr index anymore.)
445 */
446 map->mtrr = -1;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000449}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200451/*
452 * Get a mapping information.
453 *
454 * \param inode device inode.
455 * \param file_priv DRM file private.
456 * \param cmd command.
457 * \param arg user argument, pointing to a drm_map structure.
458 *
459 * \return zero on success or a negative number on failure.
460 *
461 * Searches for the mapping with the specified offset and copies its information
462 * into userspace
463 */
464int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
465 struct drm_file *file_priv)
466{
467 struct drm_map *map = data;
468 struct drm_map_list *r_list = NULL;
469 struct list_head *list;
470 int idx;
471 int i;
472
Daniel Vettere975eef2016-04-26 19:29:37 +0200473 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200474 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100475 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200476
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200477 idx = map->offset;
478 if (idx < 0)
479 return -EINVAL;
480
481 i = 0;
482 mutex_lock(&dev->struct_mutex);
483 list_for_each(list, &dev->maplist) {
484 if (i == idx) {
485 r_list = list_entry(list, struct drm_map_list, head);
486 break;
487 }
488 i++;
489 }
490 if (!r_list || !r_list->map) {
491 mutex_unlock(&dev->struct_mutex);
492 return -EINVAL;
493 }
494
495 map->offset = r_list->map->offset;
496 map->size = r_list->map->size;
497 map->type = r_list->map->type;
498 map->flags = r_list->map->flags;
499 map->handle = (void *)(unsigned long) r_list->user_token;
500 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
501
502 mutex_unlock(&dev->struct_mutex);
503
504 return 0;
505}
506
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100507/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * Remove a map private from list and deallocate resources if the mapping
509 * isn't in use.
510 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * Searches the map on drm_device::maplist, removes it from the list, see if
Matt Roper1e55a532019-02-01 17:23:26 -0800512 * it's being used, and free any associated resource (such as MTRR's) if it's not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * being on use.
514 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200515 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200517int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Dave Airlie55910512007-07-11 16:53:40 +1000519 struct drm_map_list *r_list = NULL, *list_t;
Dave Airliebd1b3312007-05-26 05:01:51 +1000520 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000521 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Dave Airlie836cf042005-07-10 19:27:04 +1000523 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000524 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000525 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000526 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000527 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100528 drm_ht_remove_key(&dev->map_hash,
529 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700530 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000531 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Dave Airlie836cf042005-07-10 19:27:04 +1000535
Dave Airliebd1b3312007-05-26 05:01:51 +1000536 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000537 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000538
539 switch (map->type) {
540 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100541 iounmap(map->handle);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500542 fallthrough;
Dave Airlie836cf042005-07-10 19:27:04 +1000543 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200544 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000545 break;
546 case _DRM_SHM:
547 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000548 if (master) {
549 if (dev->sigdata.lock == master->lock.hw_lock)
550 dev->sigdata.lock = NULL;
551 master->lock.hw_lock = NULL; /* SHM removed */
552 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100553 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000554 }
Dave Airlie836cf042005-07-10 19:27:04 +1000555 break;
556 case _DRM_AGP:
557 case _DRM_SCATTER_GATHER:
558 break;
559 case _DRM_CONSISTENT:
Thomas Zimmermann36b73b02021-01-18 14:14:15 +0100560 dma_free_coherent(dev->dev,
Chris Wilson8e4ff9b2020-02-02 17:16:32 +0000561 map->size,
562 map->handle,
563 map->offset);
Dave Airlie836cf042005-07-10 19:27:04 +1000564 break;
565 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700566 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
569}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200570EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000571
Daniel Vetter40647e42016-04-27 09:20:18 +0200572void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000573{
Daniel Vetter40647e42016-04-27 09:20:18 +0200574 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200575 !drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200576 return;
Dave Airlie836cf042005-07-10 19:27:04 +1000577
Dave Airlie30e2fb12006-02-02 19:37:46 +1100578 mutex_lock(&dev->struct_mutex);
Daniel Vetter40647e42016-04-27 09:20:18 +0200579 drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100580 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000581}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200582EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000583
Daniel Vetter40647e42016-04-27 09:20:18 +0200584void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
585{
586 struct drm_map_list *r_list, *list_temp;
587
Daniel Vetterfa538642016-08-03 21:11:10 +0200588 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200589 return;
590
591 mutex_lock(&dev->struct_mutex);
592 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
593 if (r_list->master == master) {
594 drm_legacy_rmmap_locked(dev, r_list->map);
595 r_list = NULL;
596 }
597 }
598 mutex_unlock(&dev->struct_mutex);
599}
600
Dave Airlie35a28022019-04-23 08:45:12 +1000601void drm_legacy_rmmaps(struct drm_device *dev)
602{
603 struct drm_map_list *r_list, *list_temp;
604
605 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
606 drm_legacy_rmmap(dev, r_list->map);
607}
608
Dave Airlie836cf042005-07-10 19:27:04 +1000609/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
610 * the last close of the device, and this is necessary for cleanup when things
611 * exit uncleanly. Therefore, having userland manually remove mappings seems
612 * like a pointless exercise since they're going away anyway.
613 *
614 * One use case might be after addmap is allowed for normal users for SHM and
615 * gets used by drivers that the server doesn't need to care about. This seems
616 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100617 *
618 * \param inode device inode.
619 * \param file_priv DRM file private.
620 * \param cmd command.
621 * \param arg pointer to a struct drm_map structure.
622 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000623 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200624int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
625 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000626{
Eric Anholtc153f452007-09-03 12:06:45 +1000627 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100628 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000629 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000630 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000631
Daniel Vettere975eef2016-04-26 19:29:37 +0200632 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200633 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100634 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200635
Dave Airlie30e2fb12006-02-02 19:37:46 +1100636 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000637 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000638 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000639 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000640 r_list->map->flags & _DRM_REMOVABLE) {
641 map = r_list->map;
642 break;
643 }
644 }
645
Matt Roper1e55a532019-02-01 17:23:26 -0800646 /* List has wrapped around to the head pointer, or it's empty we didn't
Dave Airlie836cf042005-07-10 19:27:04 +1000647 * find anything.
648 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000649 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100650 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000651 return -EINVAL;
652 }
653
Dave Airlie836cf042005-07-10 19:27:04 +1000654 /* Register and framebuffer maps are permanent */
655 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100656 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000657 return 0;
658 }
659
David Herrmann9fc5cde2014-08-29 12:12:28 +0200660 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000661
Dave Airlie30e2fb12006-02-02 19:37:46 +1100662 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000663
664 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100667/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * Cleanup after an error on one of the addbufs() functions.
669 *
Dave Airlie836cf042005-07-10 19:27:04 +1000670 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * \param entry buffer entry where the error occurred.
672 *
673 * Frees any pages and buffers associated with the given entry.
674 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000675static void drm_cleanup_buf_error(struct drm_device *dev,
676 struct drm_buf_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 int i;
679
680 if (entry->seg_count) {
681 for (i = 0; i < entry->seg_count; i++) {
682 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100683 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700686 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 entry->seg_count = 0;
689 }
690
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 if (entry->buf_count) {
692 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700693 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700695 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 entry->buf_count = 0;
698 }
699}
700
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200701#if IS_ENABLED(CONFIG_AGP)
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100702/*
Dave Airlied59431b2005-07-10 15:00:06 +1000703 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000705 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000706 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * After some sanity checks creates a drm_buf structure for each buffer and
710 * reallocates the buffer list of the same size order to accommodate the new
711 * buffers.
712 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200713int drm_legacy_addbufs_agp(struct drm_device *dev,
714 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Dave Airliecdd55a22007-07-11 16:32:08 +1000716 struct drm_device_dma *dma = dev->dma;
717 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000718 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000719 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unsigned long offset;
721 unsigned long agp_offset;
722 int count;
723 int order;
724 int size;
725 int alignment;
726 int page_order;
727 int total;
728 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100729 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000730 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 if (!dma)
733 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Dave Airlied59431b2005-07-10 15:00:06 +1000735 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200736 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 size = 1 << order;
738
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 alignment = (request->flags & _DRM_PAGE_ALIGN)
740 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
742 total = PAGE_SIZE << page_order;
743
744 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000745 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 DRM_DEBUG("count: %d\n", count);
748 DRM_DEBUG("order: %d\n", order);
749 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100750 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 DRM_DEBUG("alignment: %d\n", alignment);
752 DRM_DEBUG("page_order: %d\n", page_order);
753 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dave Airlie54ba2f72007-02-10 12:07:47 +1100758 /* Make sure buffers are located in AGP memory that we own */
759 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000760 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100761 if ((agp_offset >= agp_entry->bound) &&
762 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
763 valid = 1;
764 break;
765 }
766 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000767 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100768 DRM_DEBUG("zone invalid\n");
769 return -EINVAL;
770 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100771 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100773 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -EBUSY;
775 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100777 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Airlie30e2fb12006-02-02 19:37:46 +1100779 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100782 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 atomic_dec(&dev->buf_alloc);
784 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
787 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100788 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EINVAL;
791 }
792
Markus Elfring81a44132016-09-19 17:24:20 +0200793 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100795 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -ENOMEM;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 entry->buf_size = size;
801 entry->page_order = page_order;
802
803 offset = 0;
804
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 while (entry->buf_count < count) {
806 buf = &entry->buflist[entry->buf_count];
807 buf->idx = dma->buf_count + entry->buf_count;
808 buf->total = alignment;
809 buf->order = order;
810 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 buf->bus_address = agp_offset + offset;
814 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 buf->waiting = 0;
817 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000818 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400821 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Set count correctly so we free the proper amount. */
824 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100826 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -ENOMEM;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 offset += alignment;
834 entry->buf_count++;
835 byte_count += PAGE_SIZE << page_order;
836 }
837
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Eric Anholt9a298b22009-03-24 12:23:04 -0700840 temp_buflist = krealloc(dma->buflist,
841 (dma->buf_count + entry->buf_count) *
842 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100846 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -ENOMEM;
849 }
850 dma->buflist = temp_buflist;
851
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
854 }
855
856 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100857 dma->seg_count += entry->seg_count;
858 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 dma->byte_count += byte_count;
860
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
862 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dave Airlie30e2fb12006-02-02 19:37:46 +1100864 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dave Airlied59431b2005-07-10 15:00:06 +1000866 request->count = entry->buf_count;
867 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 dma->flags = _DRM_DMA_USE_AGP;
870
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
873}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200874EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200875#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876
David Herrmann9fc5cde2014-08-29 12:12:28 +0200877int drm_legacy_addbufs_pci(struct drm_device *dev,
878 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Dave Airliecdd55a22007-07-11 16:32:08 +1000880 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int count;
882 int order;
883 int size;
884 int total;
885 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000886 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100887 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000888 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int alignment;
890 unsigned long offset;
891 int i;
892 int byte_count;
893 int page_count;
894 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000895 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +0100898 return -EOPNOTSUPP;
Dave Airlied985c102006-01-02 21:32:48 +1100899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 if (!dma)
901 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Dave Airlied985c102006-01-02 21:32:48 +1100903 if (!capable(CAP_SYS_ADMIN))
904 return -EPERM;
905
Dave Airlied59431b2005-07-10 15:00:06 +1000906 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200907 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 size = 1 << order;
909
Daniel Vettera344a7e2011-10-26 00:54:41 +0200910 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
911 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
914 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Dave Airlied59431b2005-07-10 15:00:06 +1000916 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
919 total = PAGE_SIZE << page_order;
920
Daniel Vetter2177a212013-12-16 11:21:06 +0100921 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100923 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -EBUSY;
925 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100927 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dave Airlie30e2fb12006-02-02 19:37:46 +1100929 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100932 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return -ENOMEM; /* May only call once for each order */
935 }
936
937 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100938 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EINVAL;
941 }
942
Markus Elfringed6dee42016-09-19 17:17:34 +0200943 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100945 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000946 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -ENOMEM;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Markus Elfringed6dee42016-09-19 17:17:34 +0200950 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700952 kfree(entry->buflist);
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* Keep the original pagelist until we know all the allocations
959 * have succeeded
960 */
Markus Elfring20274002016-09-19 17:07:06 +0200961 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
962 sizeof(*dma->pagelist),
963 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700965 kfree(entry->buflist);
966 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100967 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -ENOMEM;
970 }
971 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
973 DRM_DEBUG("pagelist: %d entries\n",
974 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 entry->page_order = page_order;
978 byte_count = 0;
979 page_count = 0;
980
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000981 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000982
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800983 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000984
Dave Airlieddf19b92006-03-19 18:56:12 +1100985 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* Set count correctly so we free the proper amount. */
987 entry->buf_count = count;
988 entry->seg_count = count;
989 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700990 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100991 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return -ENOMEM;
994 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100995 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 for (i = 0; i < (1 << page_order); i++) {
997 DRM_DEBUG("page %d @ 0x%08lx\n",
998 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100999 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +11001001 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 for (offset = 0;
1004 offset + size <= total && entry->buf_count < count;
1005 offset += alignment, ++entry->buf_count) {
1006 buf = &entry->buflist[entry->buf_count];
1007 buf->idx = dma->buf_count + entry->buf_count;
1008 buf->total = alignment;
1009 buf->order = order;
1010 buf->used = 0;
1011 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +11001012 buf->address = (void *)(dmah->vaddr + offset);
1013 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 buf->waiting = 0;
1016 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001017 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001020 buf->dev_private = kzalloc(buf->dev_priv_size,
1021 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* Set count correctly so we free the proper amount. */
1024 entry->buf_count = count;
1025 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001027 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001028 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -ENOMEM;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 DRM_DEBUG("buffer %d @ %p\n",
1034 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036 byte_count += PAGE_SIZE << page_order;
1037 }
1038
Eric Anholt9a298b22009-03-24 12:23:04 -07001039 temp_buflist = krealloc(dma->buflist,
1040 (dma->buf_count + entry->buf_count) *
1041 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!temp_buflist) {
1043 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001045 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001046 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -ENOMEM;
1049 }
1050 dma->buflist = temp_buflist;
1051
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1054 }
1055
Thomas Weber88393162010-03-16 11:47:56 +01001056 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 * with the new one.
1058 */
1059 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001060 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 dma->pagelist = temp_pagelist;
1063
1064 dma->buf_count += entry->buf_count;
1065 dma->seg_count += entry->seg_count;
1066 dma->page_count += entry->seg_count << page_order;
1067 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1068
Dave Airlie30e2fb12006-02-02 19:37:46 +11001069 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Dave Airlied59431b2005-07-10 15:00:06 +10001071 request->count = entry->buf_count;
1072 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
George Sapountzis3417f332006-10-24 12:03:04 -07001074 if (request->flags & _DRM_PCI_BUFFER_RO)
1075 dma->flags = _DRM_DMA_USE_PCI_RO;
1076
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return 0;
1079
1080}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001081EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
David Herrmann9fc5cde2014-08-29 12:12:28 +02001083static int drm_legacy_addbufs_sg(struct drm_device *dev,
1084 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Dave Airliecdd55a22007-07-11 16:32:08 +10001086 struct drm_device_dma *dma = dev->dma;
1087 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001088 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 unsigned long offset;
1090 unsigned long agp_offset;
1091 int count;
1092 int order;
1093 int size;
1094 int alignment;
1095 int page_order;
1096 int total;
1097 int byte_count;
1098 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001099 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 if (!drm_core_check_feature(dev, DRIVER_SG))
Chris Wilson69fdf422018-09-13 20:20:50 +01001102 return -EOPNOTSUPP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103
1104 if (!dma)
1105 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Dave Airlied985c102006-01-02 21:32:48 +11001107 if (!capable(CAP_SYS_ADMIN))
1108 return -EPERM;
1109
Dave Airlied59431b2005-07-10 15:00:06 +10001110 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001111 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 size = 1 << order;
1113
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 alignment = (request->flags & _DRM_PAGE_ALIGN)
1115 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1117 total = PAGE_SIZE << page_order;
1118
1119 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001120 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 DRM_DEBUG("count: %d\n", count);
1123 DRM_DEBUG("order: %d\n", order);
1124 DRM_DEBUG("size: %d\n", size);
1125 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1126 DRM_DEBUG("alignment: %d\n", alignment);
1127 DRM_DEBUG("page_order: %d\n", page_order);
1128 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1131 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Daniel Vetter2177a212013-12-16 11:21:06 +01001133 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001134 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001135 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -EBUSY;
1137 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001139 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Dave Airlie30e2fb12006-02-02 19:37:46 +11001141 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001144 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001145 atomic_dec(&dev->buf_alloc);
1146 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
1149 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001150 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001151 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return -EINVAL;
1153 }
1154
Markus Elfringb5a2ecd2016-09-19 17:30:31 +02001155 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001157 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -ENOMEM;
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 entry->buf_size = size;
1163 entry->page_order = page_order;
1164
1165 offset = 0;
1166
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167 while (entry->buf_count < count) {
1168 buf = &entry->buflist[entry->buf_count];
1169 buf->idx = dma->buf_count + entry->buf_count;
1170 buf->total = alignment;
1171 buf->order = order;
1172 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001174 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001176 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001177 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001178 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 buf->waiting = 0;
1180 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001181 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001184 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001185 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* Set count correctly so we free the proper amount. */
1187 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001188 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001189 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return -ENOMEM;
1192 }
1193
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001194 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 offset += alignment;
1197 entry->buf_count++;
1198 byte_count += PAGE_SIZE << page_order;
1199 }
1200
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Eric Anholt9a298b22009-03-24 12:23:04 -07001203 temp_buflist = krealloc(dma->buflist,
1204 (dma->buf_count + entry->buf_count) *
1205 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001208 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001209 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001210 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return -ENOMEM;
1212 }
1213 dma->buflist = temp_buflist;
1214
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001215 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1217 }
1218
1219 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001220 dma->seg_count += entry->seg_count;
1221 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 dma->byte_count += byte_count;
1223
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001224 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1225 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Dave Airlie30e2fb12006-02-02 19:37:46 +11001227 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Dave Airlied59431b2005-07-10 15:00:06 +10001229 request->count = entry->buf_count;
1230 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 dma->flags = _DRM_DMA_USE_SG;
1233
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001234 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return 0;
1236}
1237
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * Add buffers for DMA transfers (ioctl).
1240 *
1241 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001242 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001244 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 * \return zero on success or a negative number on failure.
1246 *
1247 * According with the memory type specified in drm_buf_desc::flags and the
1248 * build options, it dispatches the call either to addbufs_agp(),
1249 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1250 * PCI memory respectively.
1251 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001252int drm_legacy_addbufs(struct drm_device *dev, void *data,
1253 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Eric Anholtc153f452007-09-03 12:06:45 +10001255 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001256 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257
Daniel Vetterfa538642016-08-03 21:11:10 +02001258 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001259 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001262 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001264#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001265 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001266 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
1268#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001269 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001270 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001271 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001272 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001274 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001275
Dave Airlied59431b2005-07-10 15:00:06 +10001276 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001279/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * Get information about the buffer mappings.
1281 *
1282 * This was originally mean for debugging purposes, or by a sophisticated
1283 * client library to determine how best to use the available buffers (e.g.,
1284 * large buffers can be used for image transfer).
1285 *
1286 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001287 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * \param cmd command.
1289 * \param arg pointer to a drm_buf_info structure.
1290 * \return zero on success or a negative number on failure.
1291 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001292 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * lock, preventing of allocating more buffers after this call. Information
1294 * about each requested buffer is then copied into user space.
1295 */
Al Viro5c7640a2017-05-24 17:54:09 -04001296int __drm_legacy_infobufs(struct drm_device *dev,
1297 void *data, int *p,
1298 int (*f)(void *, int, struct drm_buf_entry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Dave Airliecdd55a22007-07-11 16:32:08 +10001300 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int i;
1302 int count;
1303
Daniel Vetterfa538642016-08-03 21:11:10 +02001304 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001305 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001308 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001310 if (!dma)
1311 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Daniel Vetter2177a212013-12-16 11:21:06 +01001313 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001315 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return -EBUSY;
1317 }
1318 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001319 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1322 if (dma->bufs[i].buf_count)
1323 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001326 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Al Viro5c7640a2017-05-24 17:54:09 -04001328 if (*p >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001329 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
Al Viro5c7640a2017-05-24 17:54:09 -04001330 struct drm_buf_entry *from = &dma->bufs[i];
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301331
Al Viro5c7640a2017-05-24 17:54:09 -04001332 if (from->buf_count) {
1333 if (f(data, count, from) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001335 DRM_DEBUG("%d %d %d %d %d\n",
1336 i,
1337 dma->bufs[i].buf_count,
1338 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001339 dma->bufs[i].low_mark,
1340 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 ++count;
1342 }
1343 }
1344 }
Al Viro5c7640a2017-05-24 17:54:09 -04001345 *p = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 return 0;
1348}
1349
Al Viro5c7640a2017-05-24 17:54:09 -04001350static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1351{
1352 struct drm_buf_info *request = data;
1353 struct drm_buf_desc __user *to = &request->list[count];
1354 struct drm_buf_desc v = {.count = from->buf_count,
1355 .size = from->buf_size,
1356 .low_mark = from->low_mark,
1357 .high_mark = from->high_mark};
Dan Carpenter74b67ef2019-06-18 16:18:43 +03001358
1359 if (copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags)))
1360 return -EFAULT;
1361 return 0;
Al Viro5c7640a2017-05-24 17:54:09 -04001362}
1363
1364int drm_legacy_infobufs(struct drm_device *dev, void *data,
1365 struct drm_file *file_priv)
1366{
1367 struct drm_buf_info *request = data;
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301368
Al Viro5c7640a2017-05-24 17:54:09 -04001369 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1370}
1371
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001372/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * Specifies a low and high water mark for buffer allocation
1374 *
1375 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001376 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * \param cmd command.
1378 * \param arg a pointer to a drm_buf_desc structure.
1379 * \return zero on success or a negative number on failure.
1380 *
1381 * Verifies that the size order is bounded between the admissible orders and
1382 * updates the respective drm_device_dma::bufs entry low and high water mark.
1383 *
1384 * \note This ioctl is deprecated and mostly never used.
1385 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001386int drm_legacy_markbufs(struct drm_device *dev, void *data,
1387 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Dave Airliecdd55a22007-07-11 16:32:08 +10001389 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001390 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001392 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Daniel Vetterfa538642016-08-03 21:11:10 +02001394 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001395 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001398 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001400 if (!dma)
1401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001403 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001404 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001405 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1407 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 entry = &dma->bufs[order];
1409
Eric Anholtc153f452007-09-03 12:06:45 +10001410 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001412 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EINVAL;
1414
David Herrmannb008c0f2014-07-23 17:26:36 +02001415 entry->low_mark = request->low_mark;
1416 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 return 0;
1419}
1420
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001421/*
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *
1424 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001425 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * \param cmd command.
1427 * \param arg pointer to a drm_buf_free structure.
1428 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001429 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 * Calls free_buffer() for each used buffer.
1431 * This function is primarily used for debugging.
1432 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001433int drm_legacy_freebufs(struct drm_device *dev, void *data,
1434 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Dave Airliecdd55a22007-07-11 16:32:08 +10001436 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001437 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int i;
1439 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001440 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Daniel Vetterfa538642016-08-03 21:11:10 +02001442 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001443 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001446 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 if (!dma)
1449 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Eric Anholtc153f452007-09-03 12:06:45 +10001451 DRM_DEBUG("%d\n", request->count);
1452 for (i = 0; i < request->count; i++) {
1453 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455 if (idx < 0 || idx >= dma->buf_count) {
1456 DRM_ERROR("Index %d (of %d max)\n",
1457 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -EINVAL;
1459 }
Gustavo A. R. Silvaa3780502018-10-16 11:55:49 +02001460 idx = array_index_nospec(idx, dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001462 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001463 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001464 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -EINVAL;
1466 }
Daniel Vettera2661622014-09-11 07:41:51 +02001467 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
1470 return 0;
1471}
1472
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001473/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * Maps all of the DMA buffers into client-virtual space (ioctl).
1475 *
1476 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001477 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 * \param cmd command.
1479 * \param arg pointer to a drm_buf_map structure.
1480 * \return zero on success or a negative number on failure.
1481 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001482 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1483 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001484 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1485 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
Al Viro87d3ce12017-05-25 16:24:20 -04001487int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1488 void __user **v,
1489 int (*f)(void *, int, unsigned long,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +00001490 struct drm_buf *),
1491 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Dave Airliecdd55a22007-07-11 16:32:08 +10001493 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 unsigned long virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 int i;
1497
Daniel Vetterfa538642016-08-03 21:11:10 +02001498 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001499 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001502 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001504 if (!dma)
1505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Daniel Vetter2177a212013-12-16 11:21:06 +01001507 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001509 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return -EBUSY;
1511 }
1512 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001513 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Al Viro87d3ce12017-05-25 16:24:20 -04001515 if (*p >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001516 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001517 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001518 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001519 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001520 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 retcode = -EINVAL;
1524 goto done;
1525 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001526 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001527 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001528 MAP_SHARED,
1529 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001531 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001532 PROT_READ | PROT_WRITE,
1533 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001535 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* Real error */
1537 retcode = (signed long)virtual;
1538 goto done;
1539 }
Al Viro87d3ce12017-05-25 16:24:20 -04001540 *v = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001542 for (i = 0; i < dma->buf_count; i++) {
Al Viro87d3ce12017-05-25 16:24:20 -04001543 if (f(data, i, virtual, dma->buflist[i]) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 retcode = -EFAULT;
1545 goto done;
1546 }
1547 }
1548 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001549 done:
Al Viro87d3ce12017-05-25 16:24:20 -04001550 *p = dma->buf_count;
1551 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 return retcode;
1554}
1555
Al Viro87d3ce12017-05-25 16:24:20 -04001556static int map_one_buf(void *data, int idx, unsigned long virtual,
1557 struct drm_buf *buf)
1558{
1559 struct drm_buf_map *request = data;
1560 unsigned long address = virtual + buf->offset; /* *** */
1561
1562 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1563 sizeof(request->list[0].idx)))
1564 return -EFAULT;
1565 if (copy_to_user(&request->list[idx].total, &buf->total,
1566 sizeof(request->list[0].total)))
1567 return -EFAULT;
1568 if (clear_user(&request->list[idx].used, sizeof(int)))
1569 return -EFAULT;
1570 if (copy_to_user(&request->list[idx].address, &address,
1571 sizeof(address)))
1572 return -EFAULT;
1573 return 0;
1574}
1575
1576int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1577 struct drm_file *file_priv)
1578{
1579 struct drm_buf_map *request = data;
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301580
Al Viro87d3ce12017-05-25 16:24:20 -04001581 return __drm_legacy_mapbufs(dev, data, &request->count,
1582 &request->virtual, map_one_buf,
1583 file_priv);
1584}
1585
David Herrmann9fc5cde2014-08-29 12:12:28 +02001586int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001587 struct drm_file *file_priv)
1588{
Daniel Vetterfa538642016-08-03 21:11:10 +02001589 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001590 return -EOPNOTSUPP;
Daniel Vetter6eb92782013-08-08 15:41:29 +02001591
1592 if (dev->driver->dma_ioctl)
1593 return dev->driver->dma_ioctl(dev, data, file_priv);
1594 else
1595 return -EINVAL;
1596}
1597
David Herrmann9fc5cde2014-08-29 12:12:28 +02001598struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001599{
1600 struct drm_map_list *entry;
1601
1602 list_for_each_entry(entry, &dev->maplist, head) {
1603 if (entry->map && entry->map->type == _DRM_SHM &&
1604 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1605 return entry->map;
1606 }
1607 }
1608 return NULL;
1609}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001610EXPORT_SYMBOL(drm_legacy_getsarea);