blob: a0735fbc144b2aa26386230a0a74ba800afb7e4d [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;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110080 default: /* Make gcc happy */
81 ;
Dave Airlie836cf042005-07-10 19:27:04 +100082 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110083 if (entry->map->offset == map->offset)
84 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100085 }
86
87 return NULL;
88}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Dave Airliee0be4282007-07-12 10:26:44 +100090static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080091 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100092{
David Millerf1a2a9b2009-02-18 15:41:02 -080093 int use_hashed_handle, shift;
94 unsigned long add;
95
Dave Airliec2604ce2006-08-12 16:03:26 +100096#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100097 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
98#elif (BITS_PER_LONG == 32)
99 use_hashed_handle = hashed_handle;
100#else
101#error Unsupported long size. Neither 64 nor 32 bits.
102#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000103
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000104 if (!use_hashed_handle) {
105 int ret;
Suraj Upadhyay948de8422020-07-02 18:53:32 +0530106
Thomas Hellstrom15450852007-02-08 16:14:05 +1100107 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000108 ret = drm_ht_insert_item(&dev->map_hash, hash);
109 if (ret != -EINVAL)
110 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000111 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800112
113 shift = 0;
114 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
115 if (shm && (SHMLBA > PAGE_SIZE)) {
116 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
117
118 /* For shared memory, we have to preserve the SHMLBA
119 * bits of the eventual vma->vm_pgoff value during
120 * mmap(). Otherwise we run into cache aliasing problems
121 * on some platforms. On these platforms, the pgoff of
122 * a mmap() request is used to pick a suitable virtual
123 * address for the mmap() region such that it will not
124 * cause cache aliasing problems.
125 *
126 * Therefore, make sure the SHMLBA relevant bits of the
127 * hash value we use are equal to those in the original
128 * kernel virtual address.
129 */
130 shift = bits;
131 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
132 }
133
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000134 return drm_ht_just_insert_please(&dev->map_hash, hash,
135 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800136 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000137}
Dave Airlie9a186642005-06-23 21:29:18 +1000138
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100139/*
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100140 * Core function to create a range of memory available for mapping by a
141 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
143 * Adjusts the memory offset to its absolute value according to the mapping
144 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
145 * applicable and if supported by the kernel.
146 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000147static int drm_addmap_core(struct drm_device *dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000148 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000149 enum drm_map_flags flags,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000150 struct drm_map_list **maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100152 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000153 struct drm_map_list *list;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000154 unsigned long user_token;
155 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Eric Anholt9a298b22009-03-24 12:23:04 -0700157 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -ENOMEM;
160
Dave Airlie7ab98402005-07-10 16:56:52 +1000161 map->offset = offset;
162 map->size = size;
163 map->flags = flags;
164 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /* Only allow shared memory to be removable since we only keep enough
167 * book keeping information about shared memory to allow for removal
168 * when processes fork.
169 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700171 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -EINVAL;
173 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100174 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
175 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000176
177 /* page-align _DRM_SHM maps. They are allocated here so there is no security
178 * hole created by that and it works around various broken drivers that use
179 * a non-aligned quantity to map the SAREA. --BenH
180 */
181 if (map->type == _DRM_SHM)
182 map->size = PAGE_ALIGN(map->size);
183
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100184 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700185 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -EINVAL;
187 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 map->handle = NULL;
190
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 case _DRM_REGISTERS:
193 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600194#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100195 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700197 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
199 }
200#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000201 /* Some drivers preinitialize some maps, without the X Server
202 * needing to be aware of it. Therefore, we just return success
203 * when the server tries to create a duplicate map.
204 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000205 list = drm_find_matching_map(dev, map);
206 if (list != NULL) {
207 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000208 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 "mismatched sizes, (%ld vs %ld)\n",
210 map->type, map->size,
211 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000212 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000213 }
214
Eric Anholt9a298b22009-03-24 12:23:04 -0700215 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000216 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000217 return 0;
218 }
219
Daniel Vetter28185642013-08-08 15:41:27 +0200220 if (map->type == _DRM_FRAME_BUFFER ||
221 (map->flags & _DRM_WRITE_COMBINING)) {
222 map->mtrr =
223 arch_phys_wc_add(map->offset, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Scott Thompson0769d392007-08-25 18:17:49 +1000225 if (map->type == _DRM_REGISTERS) {
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000226 if (map->flags & _DRM_WRITE_COMBINING)
227 map->handle = ioremap_wc(map->offset,
228 map->size);
229 else
230 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000231 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700232 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000233 return -ENOMEM;
234 }
235 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100239 list = drm_find_matching_map(dev, map);
240 if (list != NULL) {
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000241 if (list->map->size != map->size) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100242 DRM_DEBUG("Matching maps of type %d with "
243 "mismatched sizes, (%ld vs %ld)\n",
244 map->type, map->size, list->map->size);
245 list->map->size = map->size;
246 }
247
Eric Anholt9a298b22009-03-24 12:23:04 -0700248 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100249 *maplist = list;
250 return 0;
251 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100252 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 DRM_DEBUG("%lu %d %p\n",
Daniel Vetter04420c92013-07-10 14:11:57 +0200254 map->size, order_base_2(map->size), map->handle);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000255 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700256 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -ENOMEM;
258 }
259 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Prevent a 2nd X Server from creating a 2nd lock */
Daniel Vetter95c081c2016-06-21 10:54:12 +0200262 if (dev->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700264 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EBUSY;
266 }
Daniel Vetter95c081c2016-06-21 10:54:12 +0200267 dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100270 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000271 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100272 int valid = 0;
273
Daniel Vetterd9906752013-12-11 11:34:35 +0100274 if (!dev->agp) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700275 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100278#ifdef __alpha__
279 map->offset += dev->hose->mem_space->start;
280#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000281 /* In some cases (i810 driver), user space may have already
282 * added the AGP base itself, because dev->agp->base previously
283 * only got set during AGP enable. So, only add the base
284 * address if the map's offset isn't already within the
285 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100286 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000287 if (map->offset < dev->agp->base ||
288 map->offset > dev->agp->base +
289 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
290 map->offset += dev->agp->base;
291 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100292 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
293
294 /* This assumes the DRM is in total control of AGP space.
295 * It's not always the case as AGP can be in the control
296 * of user space (i.e. i810 driver). So this loop will get
297 * skipped and we double check that dev->agp->memory is
298 * actually set as well as being invalid before EPERM'ing
299 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000300 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100301 if ((map->offset >= entry->bound) &&
302 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
303 valid = 1;
304 break;
305 }
306 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000307 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700308 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100309 return -EPERM;
310 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100311 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
312 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 case _DRM_SCATTER_GATHER:
317 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700318 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -EINVAL;
320 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000321 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000324 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000325 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000326 * casting it down to 32 bits is no problem, but we
327 * need to point to a 64bit variable first. */
Chris Wilson8e4ff9b2020-02-02 17:16:32 +0000328 map->handle = dma_alloc_coherent(&dev->pdev->dev,
329 map->size,
330 &map->offset,
331 GFP_KERNEL);
332 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700333 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000334 return -ENOMEM;
335 }
336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700338 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EINVAL;
340 }
341
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400342 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700344 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100345 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700346 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -EINVAL;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 list->map = map;
350
Dave Airlie30e2fb12006-02-02 19:37:46 +1100351 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000352 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000353
Dave Airlied1f2b552005-08-05 22:11:22 +1000354 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100355 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000356 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
357 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800358 ret = drm_map_handle(dev, &list->hash, user_token, 0,
359 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000360 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700361 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100362 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700363 kfree(map);
364 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000365 mutex_unlock(&dev->struct_mutex);
366 return ret;
367 }
368
Thomas Hellstrom15450852007-02-08 16:14:05 +1100369 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100370 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Ben Skeggs2ff2e8a32009-05-26 10:35:52 +1000372 if (!(map->flags & _DRM_DRIVER))
Daniel Vetter95c081c2016-06-21 10:54:12 +0200373 list->master = dev->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000374 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000375 return 0;
Thierry Redingafe0f692014-04-29 11:44:38 +0200376}
Dave Airlie89625eb2005-09-05 21:23:23 +1000377
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000378int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset,
David Herrmann9fc5cde2014-08-29 12:12:28 +0200379 unsigned int size, enum drm_map_type type,
380 enum drm_map_flags flags, struct drm_local_map **map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000381{
Dave Airlie55910512007-07-11 16:53:40 +1000382 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000383 int rc;
384
385 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
386 if (!rc)
387 *map_ptr = list->map;
388 return rc;
389}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200390EXPORT_SYMBOL(drm_legacy_addmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000391
Jani Nikulac7642682018-12-28 15:04:46 +0200392struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
393 unsigned int token)
394{
395 struct drm_map_list *_entry;
Suraj Upadhyay948de8422020-07-02 18:53:32 +0530396
Jani Nikulac7642682018-12-28 15:04:46 +0200397 list_for_each_entry(_entry, &dev->maplist, head)
398 if (_entry->user_token == token)
399 return _entry->map;
400 return NULL;
401}
402EXPORT_SYMBOL(drm_legacy_findmap);
403
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100404/*
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100405 * Ioctl to specify a range of memory that is available for mapping by a
406 * non-root process.
407 *
408 * \param inode device inode.
409 * \param file_priv DRM file private.
410 * \param cmd command.
411 * \param arg pointer to a drm_map structure.
412 * \return zero on success or a negative value on error.
413 *
414 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200415int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
416 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000417{
Eric Anholtc153f452007-09-03 12:06:45 +1000418 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000419 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000420 int err;
421
Dave Airlie7c1c2872008-11-28 14:22:24 +1000422 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100423 return -EPERM;
424
Daniel Vettere975eef2016-04-26 19:29:37 +0200425 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200426 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100427 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200428
Eric Anholtc153f452007-09-03 12:06:45 +1000429 err = drm_addmap_core(dev, map->offset, map->size, map->type,
430 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000431
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000433 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000434
Dave Airlie67e1a012005-10-24 18:41:39 +1000435 /* 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 +1000436 map->handle = (void *)(unsigned long)maplist->user_token;
Andy Lutomirski0dd99f12013-05-13 23:58:48 +0000437
438 /*
439 * It appears that there are no users of this value whatsoever --
440 * drmAddMap just discards it. Let's not encourage its use.
441 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
442 * it's not a real mtrr index anymore.)
443 */
444 map->mtrr = -1;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000447}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200449/*
450 * Get a mapping information.
451 *
452 * \param inode device inode.
453 * \param file_priv DRM file private.
454 * \param cmd command.
455 * \param arg user argument, pointing to a drm_map structure.
456 *
457 * \return zero on success or a negative number on failure.
458 *
459 * Searches for the mapping with the specified offset and copies its information
460 * into userspace
461 */
462int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
463 struct drm_file *file_priv)
464{
465 struct drm_map *map = data;
466 struct drm_map_list *r_list = NULL;
467 struct list_head *list;
468 int idx;
469 int i;
470
Daniel Vettere975eef2016-04-26 19:29:37 +0200471 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200472 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100473 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200474
Daniel Vetterec1f52e2016-04-26 19:29:36 +0200475 idx = map->offset;
476 if (idx < 0)
477 return -EINVAL;
478
479 i = 0;
480 mutex_lock(&dev->struct_mutex);
481 list_for_each(list, &dev->maplist) {
482 if (i == idx) {
483 r_list = list_entry(list, struct drm_map_list, head);
484 break;
485 }
486 i++;
487 }
488 if (!r_list || !r_list->map) {
489 mutex_unlock(&dev->struct_mutex);
490 return -EINVAL;
491 }
492
493 map->offset = r_list->map->offset;
494 map->size = r_list->map->size;
495 map->type = r_list->map->type;
496 map->flags = r_list->map->flags;
497 map->handle = (void *)(unsigned long) r_list->user_token;
498 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
499
500 mutex_unlock(&dev->struct_mutex);
501
502 return 0;
503}
504
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100505/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * Remove a map private from list and deallocate resources if the mapping
507 * isn't in use.
508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * Searches the map on drm_device::maplist, removes it from the list, see if
Matt Roper1e55a532019-02-01 17:23:26 -0800510 * it's being used, and free any associated resource (such as MTRR's) if it's not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * being on use.
512 *
David Herrmann9fc5cde2014-08-29 12:12:28 +0200513 * \sa drm_legacy_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200515int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Dave Airlie55910512007-07-11 16:53:40 +1000517 struct drm_map_list *r_list = NULL, *list_t;
Dave Airliebd1b3312007-05-26 05:01:51 +1000518 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000519 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dave Airlie836cf042005-07-10 19:27:04 +1000521 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000522 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000523 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000524 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000525 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100526 drm_ht_remove_key(&dev->map_hash,
527 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700528 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000529 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Dave Airlie836cf042005-07-10 19:27:04 +1000533
Dave Airliebd1b3312007-05-26 05:01:51 +1000534 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000535 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000536
537 switch (map->type) {
538 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100539 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000540 /* FALLTHROUGH */
541 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200542 arch_phys_wc_del(map->mtrr);
Dave Airlie836cf042005-07-10 19:27:04 +1000543 break;
544 case _DRM_SHM:
545 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000546 if (master) {
547 if (dev->sigdata.lock == master->lock.hw_lock)
548 dev->sigdata.lock = NULL;
549 master->lock.hw_lock = NULL; /* SHM removed */
550 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100551 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000552 }
Dave Airlie836cf042005-07-10 19:27:04 +1000553 break;
554 case _DRM_AGP:
555 case _DRM_SCATTER_GATHER:
556 break;
557 case _DRM_CONSISTENT:
Chris Wilson8e4ff9b2020-02-02 17:16:32 +0000558 dma_free_coherent(&dev->pdev->dev,
559 map->size,
560 map->handle,
561 map->offset);
Dave Airlie836cf042005-07-10 19:27:04 +1000562 break;
563 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700564 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
567}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200568EXPORT_SYMBOL(drm_legacy_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000569
Daniel Vetter40647e42016-04-27 09:20:18 +0200570void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000571{
Daniel Vetter40647e42016-04-27 09:20:18 +0200572 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200573 !drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200574 return;
Dave Airlie836cf042005-07-10 19:27:04 +1000575
Dave Airlie30e2fb12006-02-02 19:37:46 +1100576 mutex_lock(&dev->struct_mutex);
Daniel Vetter40647e42016-04-27 09:20:18 +0200577 drm_legacy_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100578 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000579}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200580EXPORT_SYMBOL(drm_legacy_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000581
Daniel Vetter40647e42016-04-27 09:20:18 +0200582void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
583{
584 struct drm_map_list *r_list, *list_temp;
585
Daniel Vetterfa538642016-08-03 21:11:10 +0200586 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter40647e42016-04-27 09:20:18 +0200587 return;
588
589 mutex_lock(&dev->struct_mutex);
590 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
591 if (r_list->master == master) {
592 drm_legacy_rmmap_locked(dev, r_list->map);
593 r_list = NULL;
594 }
595 }
596 mutex_unlock(&dev->struct_mutex);
597}
598
Dave Airlie35a28022019-04-23 08:45:12 +1000599void drm_legacy_rmmaps(struct drm_device *dev)
600{
601 struct drm_map_list *r_list, *list_temp;
602
603 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
604 drm_legacy_rmmap(dev, r_list->map);
605}
606
Dave Airlie836cf042005-07-10 19:27:04 +1000607/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
608 * the last close of the device, and this is necessary for cleanup when things
609 * exit uncleanly. Therefore, having userland manually remove mappings seems
610 * like a pointless exercise since they're going away anyway.
611 *
612 * One use case might be after addmap is allowed for normal users for SHM and
613 * gets used by drivers that the server doesn't need to care about. This seems
614 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100615 *
616 * \param inode device inode.
617 * \param file_priv DRM file private.
618 * \param cmd command.
619 * \param arg pointer to a struct drm_map structure.
620 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000621 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200622int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
623 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000624{
Eric Anholtc153f452007-09-03 12:06:45 +1000625 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100626 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000627 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000628 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000629
Daniel Vettere975eef2016-04-26 19:29:37 +0200630 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200631 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100632 return -EOPNOTSUPP;
Daniel Vettere975eef2016-04-26 19:29:37 +0200633
Dave Airlie30e2fb12006-02-02 19:37:46 +1100634 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000635 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000636 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000637 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000638 r_list->map->flags & _DRM_REMOVABLE) {
639 map = r_list->map;
640 break;
641 }
642 }
643
Matt Roper1e55a532019-02-01 17:23:26 -0800644 /* List has wrapped around to the head pointer, or it's empty we didn't
Dave Airlie836cf042005-07-10 19:27:04 +1000645 * find anything.
646 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000647 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100648 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000649 return -EINVAL;
650 }
651
Dave Airlie836cf042005-07-10 19:27:04 +1000652 /* Register and framebuffer maps are permanent */
653 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100654 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000655 return 0;
656 }
657
David Herrmann9fc5cde2014-08-29 12:12:28 +0200658 ret = drm_legacy_rmmap_locked(dev, map);
Dave Airlie836cf042005-07-10 19:27:04 +1000659
Dave Airlie30e2fb12006-02-02 19:37:46 +1100660 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000661
662 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000663}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * Cleanup after an error on one of the addbufs() functions.
667 *
Dave Airlie836cf042005-07-10 19:27:04 +1000668 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * \param entry buffer entry where the error occurred.
670 *
671 * Frees any pages and buffers associated with the given entry.
672 */
Paul McQuade2bcfcbf2018-03-19 00:52:22 +0000673static void drm_cleanup_buf_error(struct drm_device *dev,
674 struct drm_buf_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 int i;
677
678 if (entry->seg_count) {
679 for (i = 0; i < entry->seg_count; i++) {
680 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100681 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700684 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 entry->seg_count = 0;
687 }
688
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 if (entry->buf_count) {
690 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700691 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700693 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 entry->buf_count = 0;
696 }
697}
698
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200699#if IS_ENABLED(CONFIG_AGP)
Benjamin Gaignardabee5492020-03-06 11:29:36 +0100700/*
Dave Airlied59431b2005-07-10 15:00:06 +1000701 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000703 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000704 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * After some sanity checks creates a drm_buf structure for each buffer and
708 * reallocates the buffer list of the same size order to accommodate the new
709 * buffers.
710 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200711int drm_legacy_addbufs_agp(struct drm_device *dev,
712 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Dave Airliecdd55a22007-07-11 16:32:08 +1000714 struct drm_device_dma *dma = dev->dma;
715 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000716 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000717 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned long offset;
719 unsigned long agp_offset;
720 int count;
721 int order;
722 int size;
723 int alignment;
724 int page_order;
725 int total;
726 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100727 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000728 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 if (!dma)
731 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dave Airlied59431b2005-07-10 15:00:06 +1000733 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200734 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 size = 1 << order;
736
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 alignment = (request->flags & _DRM_PAGE_ALIGN)
738 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
740 total = PAGE_SIZE << page_order;
741
742 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000743 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 DRM_DEBUG("count: %d\n", count);
746 DRM_DEBUG("order: %d\n", order);
747 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100748 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000749 DRM_DEBUG("alignment: %d\n", alignment);
750 DRM_DEBUG("page_order: %d\n", page_order);
751 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
754 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Dave Airlie54ba2f72007-02-10 12:07:47 +1100756 /* Make sure buffers are located in AGP memory that we own */
757 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000758 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100759 if ((agp_offset >= agp_entry->bound) &&
760 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
761 valid = 1;
762 break;
763 }
764 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000765 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100766 DRM_DEBUG("zone invalid\n");
767 return -EINVAL;
768 }
Daniel Vetter2177a212013-12-16 11:21:06 +0100769 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100771 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EBUSY;
773 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100775 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dave Airlie30e2fb12006-02-02 19:37:46 +1100777 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100780 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 atomic_dec(&dev->buf_alloc);
782 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100786 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EINVAL;
789 }
790
Markus Elfring81a44132016-09-19 17:24:20 +0200791 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100793 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -ENOMEM;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 entry->buf_size = size;
799 entry->page_order = page_order;
800
801 offset = 0;
802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 while (entry->buf_count < count) {
804 buf = &entry->buflist[entry->buf_count];
805 buf->idx = dma->buf_count + entry->buf_count;
806 buf->total = alignment;
807 buf->order = order;
808 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 buf->bus_address = agp_offset + offset;
812 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 buf->waiting = 0;
815 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000816 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400819 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* Set count correctly so we free the proper amount. */
822 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100824 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -ENOMEM;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 offset += alignment;
832 entry->buf_count++;
833 byte_count += PAGE_SIZE << page_order;
834 }
835
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Eric Anholt9a298b22009-03-24 12:23:04 -0700838 temp_buflist = krealloc(dma->buflist,
839 (dma->buf_count + entry->buf_count) *
840 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100844 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return -ENOMEM;
847 }
848 dma->buflist = temp_buflist;
849
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
852 }
853
854 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100855 dma->seg_count += entry->seg_count;
856 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 dma->byte_count += byte_count;
858
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
860 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Dave Airlie30e2fb12006-02-02 19:37:46 +1100862 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dave Airlied59431b2005-07-10 15:00:06 +1000864 request->count = entry->buf_count;
865 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 dma->flags = _DRM_DMA_USE_AGP;
868
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
871}
David Herrmann9fc5cde2014-08-29 12:12:28 +0200872EXPORT_SYMBOL(drm_legacy_addbufs_agp);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200873#endif /* CONFIG_AGP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874
David Herrmann9fc5cde2014-08-29 12:12:28 +0200875int drm_legacy_addbufs_pci(struct drm_device *dev,
876 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Dave Airliecdd55a22007-07-11 16:32:08 +1000878 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int count;
880 int order;
881 int size;
882 int total;
883 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000884 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100885 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000886 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int alignment;
888 unsigned long offset;
889 int i;
890 int byte_count;
891 int page_count;
892 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000893 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +0100896 return -EOPNOTSUPP;
Dave Airlied985c102006-01-02 21:32:48 +1100897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 if (!dma)
899 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Dave Airlied985c102006-01-02 21:32:48 +1100901 if (!capable(CAP_SYS_ADMIN))
902 return -EPERM;
903
Dave Airlied59431b2005-07-10 15:00:06 +1000904 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +0200905 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 size = 1 << order;
907
Daniel Vettera344a7e2011-10-26 00:54:41 +0200908 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
909 request->count, request->size, size, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
912 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Dave Airlied59431b2005-07-10 15:00:06 +1000914 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
917 total = PAGE_SIZE << page_order;
918
Daniel Vetter2177a212013-12-16 11:21:06 +0100919 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +0100921 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -EBUSY;
923 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +0100925 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Dave Airlie30e2fb12006-02-02 19:37:46 +1100927 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100930 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return -ENOMEM; /* May only call once for each order */
933 }
934
935 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100936 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000937 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -EINVAL;
939 }
940
Markus Elfringed6dee42016-09-19 17:17:34 +0200941 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100943 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -ENOMEM;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Markus Elfringed6dee42016-09-19 17:17:34 +0200948 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700950 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100951 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -ENOMEM;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* Keep the original pagelist until we know all the allocations
957 * have succeeded
958 */
Markus Elfring20274002016-09-19 17:07:06 +0200959 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
960 sizeof(*dma->pagelist),
961 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700963 kfree(entry->buflist);
964 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100965 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000966 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -ENOMEM;
968 }
969 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
971 DRM_DEBUG("pagelist: %d entries\n",
972 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 entry->page_order = page_order;
976 byte_count = 0;
977 page_count = 0;
978
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000980
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800981 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000982
Dave Airlieddf19b92006-03-19 18:56:12 +1100983 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* Set count correctly so we free the proper amount. */
985 entry->buf_count = count;
986 entry->seg_count = count;
987 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700988 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100989 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return -ENOMEM;
992 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100993 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 for (i = 0; i < (1 << page_order); i++) {
995 DRM_DEBUG("page %d @ 0x%08lx\n",
996 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100997 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 temp_pagelist[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 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 for (offset = 0;
1002 offset + size <= total && entry->buf_count < count;
1003 offset += alignment, ++entry->buf_count) {
1004 buf = &entry->buflist[entry->buf_count];
1005 buf->idx = dma->buf_count + entry->buf_count;
1006 buf->total = alignment;
1007 buf->order = order;
1008 buf->used = 0;
1009 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +11001010 buf->address = (void *)(dmah->vaddr + offset);
1011 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 buf->waiting = 0;
1014 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001015 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001018 buf->dev_private = kzalloc(buf->dev_priv_size,
1019 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* Set count correctly so we free the proper amount. */
1022 entry->buf_count = count;
1023 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001025 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001026 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return -ENOMEM;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 DRM_DEBUG("buffer %d @ %p\n",
1032 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 byte_count += PAGE_SIZE << page_order;
1035 }
1036
Eric Anholt9a298b22009-03-24 12:23:04 -07001037 temp_buflist = krealloc(dma->buflist,
1038 (dma->buf_count + entry->buf_count) *
1039 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!temp_buflist) {
1041 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -07001043 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001044 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return -ENOMEM;
1047 }
1048 dma->buflist = temp_buflist;
1049
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1052 }
1053
Thomas Weber88393162010-03-16 11:47:56 +01001054 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * with the new one.
1056 */
1057 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001058 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 dma->pagelist = temp_pagelist;
1061
1062 dma->buf_count += entry->buf_count;
1063 dma->seg_count += entry->seg_count;
1064 dma->page_count += entry->seg_count << page_order;
1065 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1066
Dave Airlie30e2fb12006-02-02 19:37:46 +11001067 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Dave Airlied59431b2005-07-10 15:00:06 +10001069 request->count = entry->buf_count;
1070 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
George Sapountzis3417f332006-10-24 12:03:04 -07001072 if (request->flags & _DRM_PCI_BUFFER_RO)
1073 dma->flags = _DRM_DMA_USE_PCI_RO;
1074
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0;
1077
1078}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001079EXPORT_SYMBOL(drm_legacy_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
David Herrmann9fc5cde2014-08-29 12:12:28 +02001081static int drm_legacy_addbufs_sg(struct drm_device *dev,
1082 struct drm_buf_desc *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Dave Airliecdd55a22007-07-11 16:32:08 +10001084 struct drm_device_dma *dma = dev->dma;
1085 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001086 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 unsigned long offset;
1088 unsigned long agp_offset;
1089 int count;
1090 int order;
1091 int size;
1092 int alignment;
1093 int page_order;
1094 int total;
1095 int byte_count;
1096 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001097 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 if (!drm_core_check_feature(dev, DRIVER_SG))
Chris Wilson69fdf422018-09-13 20:20:50 +01001100 return -EOPNOTSUPP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101
1102 if (!dma)
1103 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Dave Airlied985c102006-01-02 21:32:48 +11001105 if (!capable(CAP_SYS_ADMIN))
1106 return -EPERM;
1107
Dave Airlied59431b2005-07-10 15:00:06 +10001108 count = request->count;
Daniel Vetter04420c92013-07-10 14:11:57 +02001109 order = order_base_2(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 size = 1 << order;
1111
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 alignment = (request->flags & _DRM_PAGE_ALIGN)
1113 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1115 total = PAGE_SIZE << page_order;
1116
1117 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001118 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 DRM_DEBUG("count: %d\n", count);
1121 DRM_DEBUG("order: %d\n", order);
1122 DRM_DEBUG("size: %d\n", size);
1123 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1124 DRM_DEBUG("alignment: %d\n", alignment);
1125 DRM_DEBUG("page_order: %d\n", page_order);
1126 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Daniel Vetter2177a212013-12-16 11:21:06 +01001131 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 if (dev->buf_use) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001133 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -EBUSY;
1135 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001136 atomic_inc(&dev->buf_alloc);
Daniel Vetter2177a212013-12-16 11:21:06 +01001137 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Dave Airlie30e2fb12006-02-02 19:37:46 +11001139 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001142 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 atomic_dec(&dev->buf_alloc);
1144 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001148 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EINVAL;
1151 }
1152
Markus Elfringb5a2ecd2016-09-19 17:30:31 +02001153 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001155 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -ENOMEM;
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 entry->buf_size = size;
1161 entry->page_order = page_order;
1162
1163 offset = 0;
1164
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001165 while (entry->buf_count < count) {
1166 buf = &entry->buflist[entry->buf_count];
1167 buf->idx = dma->buf_count + entry->buf_count;
1168 buf->total = alignment;
1169 buf->order = order;
1170 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001172 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001174 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001175 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001176 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 buf->waiting = 0;
1178 buf->pending = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001179 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001182 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001183 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* Set count correctly so we free the proper amount. */
1185 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001186 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001187 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001188 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return -ENOMEM;
1190 }
1191
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 offset += alignment;
1195 entry->buf_count++;
1196 byte_count += PAGE_SIZE << page_order;
1197 }
1198
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001199 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Eric Anholt9a298b22009-03-24 12:23:04 -07001201 temp_buflist = krealloc(dma->buflist,
1202 (dma->buf_count + entry->buf_count) *
1203 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001204 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001207 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001208 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -ENOMEM;
1210 }
1211 dma->buflist = temp_buflist;
1212
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001213 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1215 }
1216
1217 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001218 dma->seg_count += entry->seg_count;
1219 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 dma->byte_count += byte_count;
1221
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001222 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1223 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Dave Airlie30e2fb12006-02-02 19:37:46 +11001225 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Dave Airlied59431b2005-07-10 15:00:06 +10001227 request->count = entry->buf_count;
1228 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 dma->flags = _DRM_DMA_USE_SG;
1231
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001232 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234}
1235
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * Add buffers for DMA transfers (ioctl).
1238 *
1239 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001240 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001242 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 * \return zero on success or a negative number on failure.
1244 *
1245 * According with the memory type specified in drm_buf_desc::flags and the
1246 * build options, it dispatches the call either to addbufs_agp(),
1247 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1248 * PCI memory respectively.
1249 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001250int drm_legacy_addbufs(struct drm_device *dev, void *data,
1251 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Eric Anholtc153f452007-09-03 12:06:45 +10001253 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001254 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001255
Daniel Vetterfa538642016-08-03 21:11:10 +02001256 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001257 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001260 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001262#if IS_ENABLED(CONFIG_AGP)
Eric Anholtc153f452007-09-03 12:06:45 +10001263 if (request->flags & _DRM_AGP_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001264 ret = drm_legacy_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 else
1266#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001267 if (request->flags & _DRM_SG_BUFFER)
David Herrmann9fc5cde2014-08-29 12:12:28 +02001268 ret = drm_legacy_addbufs_sg(dev, request);
Eric Anholtc153f452007-09-03 12:06:45 +10001269 else if (request->flags & _DRM_FB_BUFFER)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001270 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 else
David Herrmann9fc5cde2014-08-29 12:12:28 +02001272 ret = drm_legacy_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001273
Dave Airlied59431b2005-07-10 15:00:06 +10001274 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001277/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * Get information about the buffer mappings.
1279 *
1280 * This was originally mean for debugging purposes, or by a sophisticated
1281 * client library to determine how best to use the available buffers (e.g.,
1282 * large buffers can be used for image transfer).
1283 *
1284 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001285 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 * \param cmd command.
1287 * \param arg pointer to a drm_buf_info structure.
1288 * \return zero on success or a negative number on failure.
1289 *
Daniel Vetter2177a212013-12-16 11:21:06 +01001290 * Increments drm_device::buf_use while holding the drm_device::buf_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * lock, preventing of allocating more buffers after this call. Information
1292 * about each requested buffer is then copied into user space.
1293 */
Al Viro5c7640a2017-05-24 17:54:09 -04001294int __drm_legacy_infobufs(struct drm_device *dev,
1295 void *data, int *p,
1296 int (*f)(void *, int, struct drm_buf_entry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Dave Airliecdd55a22007-07-11 16:32:08 +10001298 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 int i;
1300 int count;
1301
Daniel Vetterfa538642016-08-03 21:11:10 +02001302 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001303 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001306 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001308 if (!dma)
1309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Daniel Vetter2177a212013-12-16 11:21:06 +01001311 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001313 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EBUSY;
1315 }
1316 ++dev->buf_use; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001317 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001319 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1320 if (dma->bufs[i].buf_count)
1321 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001324 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Al Viro5c7640a2017-05-24 17:54:09 -04001326 if (*p >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001327 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
Al Viro5c7640a2017-05-24 17:54:09 -04001328 struct drm_buf_entry *from = &dma->bufs[i];
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301329
Al Viro5c7640a2017-05-24 17:54:09 -04001330 if (from->buf_count) {
1331 if (f(data, count, from) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 DRM_DEBUG("%d %d %d %d %d\n",
1334 i,
1335 dma->bufs[i].buf_count,
1336 dma->bufs[i].buf_size,
David Herrmannb008c0f2014-07-23 17:26:36 +02001337 dma->bufs[i].low_mark,
1338 dma->bufs[i].high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 ++count;
1340 }
1341 }
1342 }
Al Viro5c7640a2017-05-24 17:54:09 -04001343 *p = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 return 0;
1346}
1347
Al Viro5c7640a2017-05-24 17:54:09 -04001348static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1349{
1350 struct drm_buf_info *request = data;
1351 struct drm_buf_desc __user *to = &request->list[count];
1352 struct drm_buf_desc v = {.count = from->buf_count,
1353 .size = from->buf_size,
1354 .low_mark = from->low_mark,
1355 .high_mark = from->high_mark};
Dan Carpenter74b67ef2019-06-18 16:18:43 +03001356
1357 if (copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags)))
1358 return -EFAULT;
1359 return 0;
Al Viro5c7640a2017-05-24 17:54:09 -04001360}
1361
1362int drm_legacy_infobufs(struct drm_device *dev, void *data,
1363 struct drm_file *file_priv)
1364{
1365 struct drm_buf_info *request = data;
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301366
Al Viro5c7640a2017-05-24 17:54:09 -04001367 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1368}
1369
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * Specifies a low and high water mark for buffer allocation
1372 *
1373 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001374 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * \param cmd command.
1376 * \param arg a pointer to a drm_buf_desc structure.
1377 * \return zero on success or a negative number on failure.
1378 *
1379 * Verifies that the size order is bounded between the admissible orders and
1380 * updates the respective drm_device_dma::bufs entry low and high water mark.
1381 *
1382 * \note This ioctl is deprecated and mostly never used.
1383 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001384int drm_legacy_markbufs(struct drm_device *dev, void *data,
1385 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Dave Airliecdd55a22007-07-11 16:32:08 +10001387 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001388 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001390 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Daniel Vetterfa538642016-08-03 21:11:10 +02001392 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001393 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001396 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001398 if (!dma)
1399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001402 request->size, request->low_mark, request->high_mark);
Daniel Vetter04420c92013-07-10 14:11:57 +02001403 order = order_base_2(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1405 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 entry = &dma->bufs[order];
1407
Eric Anholtc153f452007-09-03 12:06:45 +10001408 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001410 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -EINVAL;
1412
David Herrmannb008c0f2014-07-23 17:26:36 +02001413 entry->low_mark = request->low_mark;
1414 entry->high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 return 0;
1417}
1418
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001419/*
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001420 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 *
1422 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001423 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * \param cmd command.
1425 * \param arg pointer to a drm_buf_free structure.
1426 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001427 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 * Calls free_buffer() for each used buffer.
1429 * This function is primarily used for debugging.
1430 */
David Herrmann9fc5cde2014-08-29 12:12:28 +02001431int drm_legacy_freebufs(struct drm_device *dev, void *data,
1432 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Dave Airliecdd55a22007-07-11 16:32:08 +10001434 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001435 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int i;
1437 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001438 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Daniel Vetterfa538642016-08-03 21:11:10 +02001440 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001441 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001444 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001446 if (!dma)
1447 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Eric Anholtc153f452007-09-03 12:06:45 +10001449 DRM_DEBUG("%d\n", request->count);
1450 for (i = 0; i < request->count; i++) {
1451 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001453 if (idx < 0 || idx >= dma->buf_count) {
1454 DRM_ERROR("Index %d (of %d max)\n",
1455 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -EINVAL;
1457 }
Gustavo A. R. Silvaa3780502018-10-16 11:55:49 +02001458 idx = array_index_nospec(idx, dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001460 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001461 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001462 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -EINVAL;
1464 }
Daniel Vettera2661622014-09-11 07:41:51 +02001465 drm_legacy_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
1468 return 0;
1469}
1470
Benjamin Gaignardabee5492020-03-06 11:29:36 +01001471/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * Maps all of the DMA buffers into client-virtual space (ioctl).
1473 *
1474 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001475 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * \param cmd command.
1477 * \param arg pointer to a drm_buf_map structure.
1478 * \return zero on success or a negative number on failure.
1479 *
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001480 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1481 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
George Sapountzis3417f332006-10-24 12:03:04 -07001482 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1483 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 */
Al Viro87d3ce12017-05-25 16:24:20 -04001485int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1486 void __user **v,
1487 int (*f)(void *, int, unsigned long,
Paul McQuade2bcfcbf2018-03-19 00:52:22 +00001488 struct drm_buf *),
1489 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Dave Airliecdd55a22007-07-11 16:32:08 +10001491 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 unsigned long virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int i;
1495
Daniel Vetterfa538642016-08-03 21:11:10 +02001496 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001497 return -EOPNOTSUPP;
Daniel Vetter8d38c4b2013-08-08 15:41:20 +02001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Chris Wilson69fdf422018-09-13 20:20:50 +01001500 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001502 if (!dma)
1503 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Daniel Vetter2177a212013-12-16 11:21:06 +01001505 spin_lock(&dev->buf_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001506 if (atomic_read(&dev->buf_alloc)) {
Daniel Vetter2177a212013-12-16 11:21:06 +01001507 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EBUSY;
1509 }
1510 dev->buf_use++; /* Can't allocate more after this call */
Daniel Vetter2177a212013-12-16 11:21:06 +01001511 spin_unlock(&dev->buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Al Viro87d3ce12017-05-25 16:24:20 -04001513 if (*p >= dma->buf_count) {
Daniel Vetterd9906752013-12-11 11:34:35 +01001514 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 || (drm_core_check_feature(dev, DRIVER_SG)
Daniel Vetter687fbb22013-08-08 15:41:24 +02001516 && (dma->flags & _DRM_DMA_USE_SG))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001517 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001518 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 retcode = -EINVAL;
1522 goto done;
1523 }
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001524 virtual = vm_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001525 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001526 MAP_SHARED,
1527 token);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 } else {
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001529 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001530 PROT_READ | PROT_WRITE,
1531 MAP_SHARED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* Real error */
1535 retcode = (signed long)virtual;
1536 goto done;
1537 }
Al Viro87d3ce12017-05-25 16:24:20 -04001538 *v = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 for (i = 0; i < dma->buf_count; i++) {
Al Viro87d3ce12017-05-25 16:24:20 -04001541 if (f(data, i, virtual, dma->buflist[i]) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 retcode = -EFAULT;
1543 goto done;
1544 }
1545 }
1546 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001547 done:
Al Viro87d3ce12017-05-25 16:24:20 -04001548 *p = dma->buf_count;
1549 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 return retcode;
1552}
1553
Al Viro87d3ce12017-05-25 16:24:20 -04001554static int map_one_buf(void *data, int idx, unsigned long virtual,
1555 struct drm_buf *buf)
1556{
1557 struct drm_buf_map *request = data;
1558 unsigned long address = virtual + buf->offset; /* *** */
1559
1560 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1561 sizeof(request->list[0].idx)))
1562 return -EFAULT;
1563 if (copy_to_user(&request->list[idx].total, &buf->total,
1564 sizeof(request->list[0].total)))
1565 return -EFAULT;
1566 if (clear_user(&request->list[idx].used, sizeof(int)))
1567 return -EFAULT;
1568 if (copy_to_user(&request->list[idx].address, &address,
1569 sizeof(address)))
1570 return -EFAULT;
1571 return 0;
1572}
1573
1574int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1575 struct drm_file *file_priv)
1576{
1577 struct drm_buf_map *request = data;
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301578
Al Viro87d3ce12017-05-25 16:24:20 -04001579 return __drm_legacy_mapbufs(dev, data, &request->count,
1580 &request->virtual, map_one_buf,
1581 file_priv);
1582}
1583
David Herrmann9fc5cde2014-08-29 12:12:28 +02001584int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
Daniel Vetter6eb92782013-08-08 15:41:29 +02001585 struct drm_file *file_priv)
1586{
Daniel Vetterfa538642016-08-03 21:11:10 +02001587 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +01001588 return -EOPNOTSUPP;
Daniel Vetter6eb92782013-08-08 15:41:29 +02001589
1590 if (dev->driver->dma_ioctl)
1591 return dev->driver->dma_ioctl(dev, data, file_priv);
1592 else
1593 return -EINVAL;
1594}
1595
David Herrmann9fc5cde2014-08-29 12:12:28 +02001596struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
Daniel Vetterbd0c0ce2013-07-10 14:11:56 +02001597{
1598 struct drm_map_list *entry;
1599
1600 list_for_each_entry(entry, &dev->maplist, head) {
1601 if (entry->map && entry->map->type == _DRM_SHM &&
1602 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1603 return entry->map;
1604 }
1605 }
1606 return NULL;
1607}
David Herrmann9fc5cde2014-08-29 12:12:28 +02001608EXPORT_SYMBOL(drm_legacy_getsarea);