Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 2 | * Legacy: Generic DRM Buffer Management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 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 Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 8 | * Author: Rickard E. (Rik) Faith <faith@valinux.com> |
| 9 | * Author: Gareth Hughes <gareth@valinux.com> |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * 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 Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 31 | #include <linux/export.h> |
Sam Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 32 | #include <linux/log2.h> |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/mman.h> |
| 35 | #include <linux/nospec.h> |
Daniel Vetter | 625c18d | 2020-04-03 13:06:10 +0200 | [diff] [blame] | 36 | #include <linux/pci.h> |
Sam Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 37 | #include <linux/slab.h> |
| 38 | #include <linux/uaccess.h> |
| 39 | #include <linux/vmalloc.h> |
| 40 | |
David Miller | f1a2a9b | 2009-02-18 15:41:02 -0800 | [diff] [blame] | 41 | #include <asm/shmparam.h> |
Sam Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 42 | |
| 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 Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 47 | #include <drm/drm_print.h> |
| 48 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 49 | #include "drm_legacy.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Gustavo A. R. Silva | a378050 | 2018-10-16 11:55:49 +0200 | [diff] [blame] | 51 | |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 52 | static struct drm_map_list *drm_find_matching_map(struct drm_device *dev, |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 53 | struct drm_local_map *map) |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 54 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 55 | struct drm_map_list *entry; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 56 | list_for_each_entry(entry, &dev->maplist, head) { |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 57 | /* |
| 58 | * Because the kernel-userspace ABI is fixed at a 32-bit offset |
Tormod Volden | 66aa696 | 2011-05-30 19:45:43 +0000 | [diff] [blame] | 59 | * while PCI resources may live above that, we only compare the |
| 60 | * lower 32 bits of the map offset for maps of type |
| 61 | * _DRM_FRAMEBUFFER or _DRM_REGISTERS. |
| 62 | * It is assumed that if a driver have more than one resource |
| 63 | * of each type, the lower 32 bits are different. |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 64 | */ |
| 65 | if (!entry->map || |
| 66 | map->type != entry->map->type || |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame] | 67 | entry->master != dev->master) |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 68 | continue; |
| 69 | switch (map->type) { |
| 70 | case _DRM_SHM: |
| 71 | if (map->flags != _DRM_CONTAINS_LOCK) |
| 72 | break; |
Tormod Volden | 66aa696 | 2011-05-30 19:45:43 +0000 | [diff] [blame] | 73 | return entry; |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 74 | case _DRM_REGISTERS: |
| 75 | case _DRM_FRAME_BUFFER: |
Tormod Volden | 66aa696 | 2011-05-30 19:45:43 +0000 | [diff] [blame] | 76 | if ((entry->map->offset & 0xffffffff) == |
| 77 | (map->offset & 0xffffffff)) |
| 78 | return entry; |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 79 | default: /* Make gcc happy */ |
| 80 | ; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 81 | } |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 82 | if (entry->map->offset == map->offset) |
| 83 | return entry; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return NULL; |
| 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Dave Airlie | e0be428 | 2007-07-12 10:26:44 +1000 | [diff] [blame] | 89 | static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash, |
David Miller | f1a2a9b | 2009-02-18 15:41:02 -0800 | [diff] [blame] | 90 | unsigned long user_token, int hashed_handle, int shm) |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 91 | { |
David Miller | f1a2a9b | 2009-02-18 15:41:02 -0800 | [diff] [blame] | 92 | int use_hashed_handle, shift; |
| 93 | unsigned long add; |
| 94 | |
Dave Airlie | c2604ce | 2006-08-12 16:03:26 +1000 | [diff] [blame] | 95 | #if (BITS_PER_LONG == 64) |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 96 | use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle); |
| 97 | #elif (BITS_PER_LONG == 32) |
| 98 | use_hashed_handle = hashed_handle; |
| 99 | #else |
| 100 | #error Unsupported long size. Neither 64 nor 32 bits. |
| 101 | #endif |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 102 | |
Thomas Hellstrom | e08870c | 2006-09-22 04:18:37 +1000 | [diff] [blame] | 103 | if (!use_hashed_handle) { |
| 104 | int ret; |
Thomas Hellstrom | 1545085 | 2007-02-08 16:14:05 +1100 | [diff] [blame] | 105 | hash->key = user_token >> PAGE_SHIFT; |
Thomas Hellstrom | e08870c | 2006-09-22 04:18:37 +1000 | [diff] [blame] | 106 | ret = drm_ht_insert_item(&dev->map_hash, hash); |
| 107 | if (ret != -EINVAL) |
| 108 | return ret; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 109 | } |
David Miller | f1a2a9b | 2009-02-18 15:41:02 -0800 | [diff] [blame] | 110 | |
| 111 | shift = 0; |
| 112 | add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT; |
| 113 | if (shm && (SHMLBA > PAGE_SIZE)) { |
| 114 | int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1; |
| 115 | |
| 116 | /* For shared memory, we have to preserve the SHMLBA |
| 117 | * bits of the eventual vma->vm_pgoff value during |
| 118 | * mmap(). Otherwise we run into cache aliasing problems |
| 119 | * on some platforms. On these platforms, the pgoff of |
| 120 | * a mmap() request is used to pick a suitable virtual |
| 121 | * address for the mmap() region such that it will not |
| 122 | * cause cache aliasing problems. |
| 123 | * |
| 124 | * Therefore, make sure the SHMLBA relevant bits of the |
| 125 | * hash value we use are equal to those in the original |
| 126 | * kernel virtual address. |
| 127 | */ |
| 128 | shift = bits; |
| 129 | add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL)); |
| 130 | } |
| 131 | |
Thomas Hellstrom | e08870c | 2006-09-22 04:18:37 +1000 | [diff] [blame] | 132 | return drm_ht_just_insert_please(&dev->map_hash, hash, |
| 133 | user_token, 32 - PAGE_SHIFT - 3, |
David Miller | f1a2a9b | 2009-02-18 15:41:02 -0800 | [diff] [blame] | 134 | shift, add); |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 135 | } |
Dave Airlie | 9a18664 | 2005-06-23 21:29:18 +1000 | [diff] [blame] | 136 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 137 | /* |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 138 | * Core function to create a range of memory available for mapping by a |
| 139 | * non-root process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * |
| 141 | * Adjusts the memory offset to its absolute value according to the mapping |
| 142 | * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where |
| 143 | * applicable and if supported by the kernel. |
| 144 | */ |
Paul McQuade | 2bcfcbf | 2018-03-19 00:52:22 +0000 | [diff] [blame] | 145 | static int drm_addmap_core(struct drm_device *dev, resource_size_t offset, |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 146 | unsigned int size, enum drm_map_type type, |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 147 | enum drm_map_flags flags, |
Paul McQuade | 2bcfcbf | 2018-03-19 00:52:22 +0000 | [diff] [blame] | 148 | struct drm_map_list **maplist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 150 | struct drm_local_map *map; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 151 | struct drm_map_list *list; |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 152 | unsigned long user_token; |
| 153 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 155 | map = kmalloc(sizeof(*map), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 156 | if (!map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return -ENOMEM; |
| 158 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 159 | map->offset = offset; |
| 160 | map->size = size; |
| 161 | map->flags = flags; |
| 162 | map->type = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | /* Only allow shared memory to be removable since we only keep enough |
| 165 | * book keeping information about shared memory to allow for removal |
| 166 | * when processes fork. |
| 167 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 168 | if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 169 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return -EINVAL; |
| 171 | } |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 172 | DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n", |
| 173 | (unsigned long long)map->offset, map->size, map->type); |
Benjamin Herrenschmidt | b674137 | 2009-05-18 11:56:16 +1000 | [diff] [blame] | 174 | |
| 175 | /* page-align _DRM_SHM maps. They are allocated here so there is no security |
| 176 | * hole created by that and it works around various broken drivers that use |
| 177 | * a non-aligned quantity to map the SAREA. --BenH |
| 178 | */ |
| 179 | if (map->type == _DRM_SHM) |
| 180 | map->size = PAGE_ALIGN(map->size); |
| 181 | |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 182 | if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 183 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return -EINVAL; |
| 185 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 186 | map->mtrr = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | map->handle = NULL; |
| 188 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 189 | switch (map->type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | case _DRM_REGISTERS: |
| 191 | case _DRM_FRAME_BUFFER: |
Jordan Crouse | 4b7fb9b | 2010-05-27 13:40:26 -0600 | [diff] [blame] | 192 | #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__) |
Dave Airlie | 8d2ea62 | 2006-01-11 20:48:09 +1100 | [diff] [blame] | 193 | if (map->offset + (map->size-1) < map->offset || |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 194 | map->offset < virt_to_phys(high_memory)) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 195 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | } |
| 198 | #endif |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 199 | /* Some drivers preinitialize some maps, without the X Server |
| 200 | * needing to be aware of it. Therefore, we just return success |
| 201 | * when the server tries to create a duplicate map. |
| 202 | */ |
Dave Airlie | 89625eb | 2005-09-05 21:23:23 +1000 | [diff] [blame] | 203 | list = drm_find_matching_map(dev, map); |
| 204 | if (list != NULL) { |
| 205 | if (list->map->size != map->size) { |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 206 | DRM_DEBUG("Matching maps of type %d with " |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 207 | "mismatched sizes, (%ld vs %ld)\n", |
| 208 | map->type, map->size, |
| 209 | list->map->size); |
Dave Airlie | 89625eb | 2005-09-05 21:23:23 +1000 | [diff] [blame] | 210 | list->map->size = map->size; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 211 | } |
| 212 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 213 | kfree(map); |
Dave Airlie | 89625eb | 2005-09-05 21:23:23 +1000 | [diff] [blame] | 214 | *maplist = list; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Daniel Vetter | 2818564 | 2013-08-08 15:41:27 +0200 | [diff] [blame] | 218 | if (map->type == _DRM_FRAME_BUFFER || |
| 219 | (map->flags & _DRM_WRITE_COMBINING)) { |
| 220 | map->mtrr = |
| 221 | arch_phys_wc_add(map->offset, map->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Scott Thompson | 0769d39 | 2007-08-25 18:17:49 +1000 | [diff] [blame] | 223 | if (map->type == _DRM_REGISTERS) { |
Andy Lutomirski | ff47eaf | 2013-05-13 23:58:42 +0000 | [diff] [blame] | 224 | if (map->flags & _DRM_WRITE_COMBINING) |
| 225 | map->handle = ioremap_wc(map->offset, |
| 226 | map->size); |
| 227 | else |
| 228 | map->handle = ioremap(map->offset, map->size); |
Scott Thompson | 0769d39 | 2007-08-25 18:17:49 +1000 | [diff] [blame] | 229 | if (!map->handle) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 230 | kfree(map); |
Scott Thompson | 0769d39 | 2007-08-25 18:17:49 +1000 | [diff] [blame] | 231 | return -ENOMEM; |
| 232 | } |
| 233 | } |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | case _DRM_SHM: |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 237 | list = drm_find_matching_map(dev, map); |
| 238 | if (list != NULL) { |
Paul McQuade | 2bcfcbf | 2018-03-19 00:52:22 +0000 | [diff] [blame] | 239 | if (list->map->size != map->size) { |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 240 | DRM_DEBUG("Matching maps of type %d with " |
| 241 | "mismatched sizes, (%ld vs %ld)\n", |
| 242 | map->type, map->size, list->map->size); |
| 243 | list->map->size = map->size; |
| 244 | } |
| 245 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 246 | kfree(map); |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 247 | *maplist = list; |
| 248 | return 0; |
| 249 | } |
Thomas Hellstrom | f239b7b | 2007-01-08 21:22:50 +1100 | [diff] [blame] | 250 | map->handle = vmalloc_user(map->size); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 251 | DRM_DEBUG("%lu %d %p\n", |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 252 | map->size, order_base_2(map->size), map->handle); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 253 | if (!map->handle) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 254 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return -ENOMEM; |
| 256 | } |
| 257 | map->offset = (unsigned long)map->handle; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 258 | if (map->flags & _DRM_CONTAINS_LOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /* Prevent a 2nd X Server from creating a 2nd lock */ |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame] | 260 | if (dev->master->lock.hw_lock != NULL) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 261 | vfree(map->handle); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 262 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return -EBUSY; |
| 264 | } |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame] | 265 | dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | break; |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 268 | case _DRM_AGP: { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 269 | struct drm_agp_mem *entry; |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 270 | int valid = 0; |
| 271 | |
Daniel Vetter | d990675 | 2013-12-11 11:34:35 +0100 | [diff] [blame] | 272 | if (!dev->agp) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 273 | kfree(map); |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 274 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 276 | #ifdef __alpha__ |
| 277 | map->offset += dev->hose->mem_space->start; |
| 278 | #endif |
Eric Anholt | 47a184a | 2007-11-22 16:55:15 +1000 | [diff] [blame] | 279 | /* In some cases (i810 driver), user space may have already |
| 280 | * added the AGP base itself, because dev->agp->base previously |
| 281 | * only got set during AGP enable. So, only add the base |
| 282 | * address if the map's offset isn't already within the |
| 283 | * aperture. |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 284 | */ |
Eric Anholt | 47a184a | 2007-11-22 16:55:15 +1000 | [diff] [blame] | 285 | if (map->offset < dev->agp->base || |
| 286 | map->offset > dev->agp->base + |
| 287 | dev->agp->agp_info.aper_size * 1024 * 1024 - 1) { |
| 288 | map->offset += dev->agp->base; |
| 289 | } |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 290 | map->mtrr = dev->agp->agp_mtrr; /* for getmap */ |
| 291 | |
| 292 | /* This assumes the DRM is in total control of AGP space. |
| 293 | * It's not always the case as AGP can be in the control |
| 294 | * of user space (i.e. i810 driver). So this loop will get |
| 295 | * skipped and we double check that dev->agp->memory is |
| 296 | * actually set as well as being invalid before EPERM'ing |
| 297 | */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 298 | list_for_each_entry(entry, &dev->agp->memory, head) { |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 299 | if ((map->offset >= entry->bound) && |
| 300 | (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) { |
| 301 | valid = 1; |
| 302 | break; |
| 303 | } |
| 304 | } |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 305 | if (!list_empty(&dev->agp->memory) && !valid) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 306 | kfree(map); |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 307 | return -EPERM; |
| 308 | } |
Benjamin Herrenschmidt | 41c2e75 | 2009-02-02 16:55:47 +1100 | [diff] [blame] | 309 | DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n", |
| 310 | (unsigned long long)map->offset, map->size); |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | break; |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | case _DRM_SCATTER_GATHER: |
| 315 | if (!dev->sg) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 316 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return -EINVAL; |
| 318 | } |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 319 | map->offset += (unsigned long)dev->sg->virtual; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | break; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 321 | case _DRM_CONSISTENT: |
Dave Airlie | 2d0f9ea | 2005-07-10 14:34:13 +1000 | [diff] [blame] | 322 | /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G, |
Dave Airlie | 9c8da5e | 2005-07-10 15:38:56 +1000 | [diff] [blame] | 323 | * As we're limiting the address to 2^32-1 (or less), |
Dave Airlie | 2d0f9ea | 2005-07-10 14:34:13 +1000 | [diff] [blame] | 324 | * casting it down to 32 bits is no problem, but we |
| 325 | * need to point to a 64bit variable first. */ |
Chris Wilson | 8e4ff9b | 2020-02-02 17:16:32 +0000 | [diff] [blame] | 326 | map->handle = dma_alloc_coherent(&dev->pdev->dev, |
| 327 | map->size, |
| 328 | &map->offset, |
| 329 | GFP_KERNEL); |
| 330 | if (!map->handle) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 331 | kfree(map); |
Dave Airlie | 2d0f9ea | 2005-07-10 14:34:13 +1000 | [diff] [blame] | 332 | return -ENOMEM; |
| 333 | } |
| 334 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | default: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 336 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return -EINVAL; |
| 338 | } |
| 339 | |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 340 | list = kzalloc(sizeof(*list), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 341 | if (!list) { |
Amol Lad | 85abb3f | 2006-10-25 09:55:34 -0700 | [diff] [blame] | 342 | if (map->type == _DRM_REGISTERS) |
Christoph Hellwig | 004a772 | 2007-01-08 21:56:59 +1100 | [diff] [blame] | 343 | iounmap(map->handle); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 344 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return -EINVAL; |
| 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | list->map = map; |
| 348 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 349 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 350 | list_add(&list->head, &dev->maplist); |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 351 | |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 352 | /* Assign a 32-bit handle */ |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 353 | /* We do it here so that dev->struct_mutex protects the increment */ |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 354 | user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle : |
| 355 | map->offset; |
David Miller | f1a2a9b | 2009-02-18 15:41:02 -0800 | [diff] [blame] | 356 | ret = drm_map_handle(dev, &list->hash, user_token, 0, |
| 357 | (map->type == _DRM_SHM)); |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 358 | if (ret) { |
Amol Lad | 85abb3f | 2006-10-25 09:55:34 -0700 | [diff] [blame] | 359 | if (map->type == _DRM_REGISTERS) |
Christoph Hellwig | 004a772 | 2007-01-08 21:56:59 +1100 | [diff] [blame] | 360 | iounmap(map->handle); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 361 | kfree(map); |
| 362 | kfree(list); |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 363 | mutex_unlock(&dev->struct_mutex); |
| 364 | return ret; |
| 365 | } |
| 366 | |
Thomas Hellstrom | 1545085 | 2007-02-08 16:14:05 +1100 | [diff] [blame] | 367 | list->user_token = list->hash.key << PAGE_SHIFT; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 368 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Ben Skeggs | 2ff2e8a | 2009-05-26 10:35:52 +1000 | [diff] [blame] | 370 | if (!(map->flags & _DRM_DRIVER)) |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame] | 371 | list->master = dev->master; |
Dave Airlie | 89625eb | 2005-09-05 21:23:23 +1000 | [diff] [blame] | 372 | *maplist = list; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 373 | return 0; |
Thierry Reding | afe0f69 | 2014-04-29 11:44:38 +0200 | [diff] [blame] | 374 | } |
Dave Airlie | 89625eb | 2005-09-05 21:23:23 +1000 | [diff] [blame] | 375 | |
Paul McQuade | 2bcfcbf | 2018-03-19 00:52:22 +0000 | [diff] [blame] | 376 | int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset, |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 377 | unsigned int size, enum drm_map_type type, |
| 378 | enum drm_map_flags flags, struct drm_local_map **map_ptr) |
Dave Airlie | 89625eb | 2005-09-05 21:23:23 +1000 | [diff] [blame] | 379 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 380 | struct drm_map_list *list; |
Dave Airlie | 89625eb | 2005-09-05 21:23:23 +1000 | [diff] [blame] | 381 | int rc; |
| 382 | |
| 383 | rc = drm_addmap_core(dev, offset, size, type, flags, &list); |
| 384 | if (!rc) |
| 385 | *map_ptr = list->map; |
| 386 | return rc; |
| 387 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 388 | EXPORT_SYMBOL(drm_legacy_addmap); |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 389 | |
Jani Nikula | c764268 | 2018-12-28 15:04:46 +0200 | [diff] [blame] | 390 | struct drm_local_map *drm_legacy_findmap(struct drm_device *dev, |
| 391 | unsigned int token) |
| 392 | { |
| 393 | struct drm_map_list *_entry; |
| 394 | list_for_each_entry(_entry, &dev->maplist, head) |
| 395 | if (_entry->user_token == token) |
| 396 | return _entry->map; |
| 397 | return NULL; |
| 398 | } |
| 399 | EXPORT_SYMBOL(drm_legacy_findmap); |
| 400 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 401 | /* |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 402 | * Ioctl to specify a range of memory that is available for mapping by a |
| 403 | * non-root process. |
| 404 | * |
| 405 | * \param inode device inode. |
| 406 | * \param file_priv DRM file private. |
| 407 | * \param cmd command. |
| 408 | * \param arg pointer to a drm_map structure. |
| 409 | * \return zero on success or a negative value on error. |
| 410 | * |
| 411 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 412 | int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data, |
| 413 | struct drm_file *file_priv) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 414 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 415 | struct drm_map *map = data; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 416 | struct drm_map_list *maplist; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 417 | int err; |
| 418 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 419 | if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM)) |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 420 | return -EPERM; |
| 421 | |
Daniel Vetter | e975eef | 2016-04-26 19:29:37 +0200 | [diff] [blame] | 422 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 423 | !drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 424 | return -EOPNOTSUPP; |
Daniel Vetter | e975eef | 2016-04-26 19:29:37 +0200 | [diff] [blame] | 425 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 426 | err = drm_addmap_core(dev, map->offset, map->size, map->type, |
| 427 | map->flags, &maplist); |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 428 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 429 | if (err) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 430 | return err; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 431 | |
Dave Airlie | 67e1a01 | 2005-10-24 18:41:39 +1000 | [diff] [blame] | 432 | /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 433 | map->handle = (void *)(unsigned long)maplist->user_token; |
Andy Lutomirski | 0dd99f1 | 2013-05-13 23:58:48 +0000 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * It appears that there are no users of this value whatsoever -- |
| 437 | * drmAddMap just discards it. Let's not encourage its use. |
| 438 | * (Keeping drm_addmap_core's returned mtrr value would be wrong -- |
| 439 | * it's not a real mtrr index anymore.) |
| 440 | */ |
| 441 | map->mtrr = -1; |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return 0; |
Dave Airlie | 88f399c | 2005-08-20 17:43:33 +1000 | [diff] [blame] | 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Daniel Vetter | ec1f52e | 2016-04-26 19:29:36 +0200 | [diff] [blame] | 446 | /* |
| 447 | * Get a mapping information. |
| 448 | * |
| 449 | * \param inode device inode. |
| 450 | * \param file_priv DRM file private. |
| 451 | * \param cmd command. |
| 452 | * \param arg user argument, pointing to a drm_map structure. |
| 453 | * |
| 454 | * \return zero on success or a negative number on failure. |
| 455 | * |
| 456 | * Searches for the mapping with the specified offset and copies its information |
| 457 | * into userspace |
| 458 | */ |
| 459 | int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data, |
| 460 | struct drm_file *file_priv) |
| 461 | { |
| 462 | struct drm_map *map = data; |
| 463 | struct drm_map_list *r_list = NULL; |
| 464 | struct list_head *list; |
| 465 | int idx; |
| 466 | int i; |
| 467 | |
Daniel Vetter | e975eef | 2016-04-26 19:29:37 +0200 | [diff] [blame] | 468 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 469 | !drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 470 | return -EOPNOTSUPP; |
Daniel Vetter | e975eef | 2016-04-26 19:29:37 +0200 | [diff] [blame] | 471 | |
Daniel Vetter | ec1f52e | 2016-04-26 19:29:36 +0200 | [diff] [blame] | 472 | idx = map->offset; |
| 473 | if (idx < 0) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | i = 0; |
| 477 | mutex_lock(&dev->struct_mutex); |
| 478 | list_for_each(list, &dev->maplist) { |
| 479 | if (i == idx) { |
| 480 | r_list = list_entry(list, struct drm_map_list, head); |
| 481 | break; |
| 482 | } |
| 483 | i++; |
| 484 | } |
| 485 | if (!r_list || !r_list->map) { |
| 486 | mutex_unlock(&dev->struct_mutex); |
| 487 | return -EINVAL; |
| 488 | } |
| 489 | |
| 490 | map->offset = r_list->map->offset; |
| 491 | map->size = r_list->map->size; |
| 492 | map->type = r_list->map->type; |
| 493 | map->flags = r_list->map->flags; |
| 494 | map->handle = (void *)(unsigned long) r_list->user_token; |
| 495 | map->mtrr = arch_phys_wc_index(r_list->map->mtrr); |
| 496 | |
| 497 | mutex_unlock(&dev->struct_mutex); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 502 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | * Remove a map private from list and deallocate resources if the mapping |
| 504 | * isn't in use. |
| 505 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | * Searches the map on drm_device::maplist, removes it from the list, see if |
Matt Roper | 1e55a53 | 2019-02-01 17:23:26 -0800 | [diff] [blame] | 507 | * it's being used, and free any associated resource (such as MTRR's) if it's not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | * being on use. |
| 509 | * |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 510 | * \sa drm_legacy_addmap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 512 | int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 514 | struct drm_map_list *r_list = NULL, *list_t; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 515 | int found = 0; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 516 | struct drm_master *master; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 518 | /* Find the list entry for the map and remove it */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 519 | list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) { |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 520 | if (r_list->map == map) { |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 521 | master = r_list->master; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 522 | list_del(&r_list->head); |
Thomas Hellstrom | 1545085 | 2007-02-08 16:14:05 +1100 | [diff] [blame] | 523 | drm_ht_remove_key(&dev->map_hash, |
| 524 | r_list->user_token >> PAGE_SHIFT); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 525 | kfree(r_list); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 526 | found = 1; |
Dave Airlie | 2d0f9ea | 2005-07-10 14:34:13 +1000 | [diff] [blame] | 527 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 530 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 531 | if (!found) |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 532 | return -EINVAL; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 533 | |
| 534 | switch (map->type) { |
| 535 | case _DRM_REGISTERS: |
Christoph Hellwig | 004a772 | 2007-01-08 21:56:59 +1100 | [diff] [blame] | 536 | iounmap(map->handle); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 537 | /* FALLTHROUGH */ |
| 538 | case _DRM_FRAME_BUFFER: |
Daniel Vetter | 2818564 | 2013-08-08 15:41:27 +0200 | [diff] [blame] | 539 | arch_phys_wc_del(map->mtrr); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 540 | break; |
| 541 | case _DRM_SHM: |
| 542 | vfree(map->handle); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 543 | if (master) { |
| 544 | if (dev->sigdata.lock == master->lock.hw_lock) |
| 545 | dev->sigdata.lock = NULL; |
| 546 | master->lock.hw_lock = NULL; /* SHM removed */ |
| 547 | master->lock.file_priv = NULL; |
Thomas Hellstrom | 171901d | 2009-03-02 11:10:55 +0100 | [diff] [blame] | 548 | wake_up_interruptible_all(&master->lock.lock_queue); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 549 | } |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 550 | break; |
| 551 | case _DRM_AGP: |
| 552 | case _DRM_SCATTER_GATHER: |
| 553 | break; |
| 554 | case _DRM_CONSISTENT: |
Chris Wilson | 8e4ff9b | 2020-02-02 17:16:32 +0000 | [diff] [blame] | 555 | dma_free_coherent(&dev->pdev->dev, |
| 556 | map->size, |
| 557 | map->handle, |
| 558 | map->offset); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 559 | break; |
| 560 | } |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 561 | kfree(map); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return 0; |
| 564 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 565 | EXPORT_SYMBOL(drm_legacy_rmmap_locked); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 566 | |
Daniel Vetter | 40647e4 | 2016-04-27 09:20:18 +0200 | [diff] [blame] | 567 | void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map) |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 568 | { |
Daniel Vetter | 40647e4 | 2016-04-27 09:20:18 +0200 | [diff] [blame] | 569 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 570 | !drm_core_check_feature(dev, DRIVER_LEGACY)) |
Daniel Vetter | 40647e4 | 2016-04-27 09:20:18 +0200 | [diff] [blame] | 571 | return; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 572 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 573 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | 40647e4 | 2016-04-27 09:20:18 +0200 | [diff] [blame] | 574 | drm_legacy_rmmap_locked(dev, map); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 575 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 576 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 577 | EXPORT_SYMBOL(drm_legacy_rmmap); |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 578 | |
Daniel Vetter | 40647e4 | 2016-04-27 09:20:18 +0200 | [diff] [blame] | 579 | void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master) |
| 580 | { |
| 581 | struct drm_map_list *r_list, *list_temp; |
| 582 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 583 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
Daniel Vetter | 40647e4 | 2016-04-27 09:20:18 +0200 | [diff] [blame] | 584 | return; |
| 585 | |
| 586 | mutex_lock(&dev->struct_mutex); |
| 587 | list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) { |
| 588 | if (r_list->master == master) { |
| 589 | drm_legacy_rmmap_locked(dev, r_list->map); |
| 590 | r_list = NULL; |
| 591 | } |
| 592 | } |
| 593 | mutex_unlock(&dev->struct_mutex); |
| 594 | } |
| 595 | |
Dave Airlie | 35a2802 | 2019-04-23 08:45:12 +1000 | [diff] [blame] | 596 | void drm_legacy_rmmaps(struct drm_device *dev) |
| 597 | { |
| 598 | struct drm_map_list *r_list, *list_temp; |
| 599 | |
| 600 | list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) |
| 601 | drm_legacy_rmmap(dev, r_list->map); |
| 602 | } |
| 603 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 604 | /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on |
| 605 | * the last close of the device, and this is necessary for cleanup when things |
| 606 | * exit uncleanly. Therefore, having userland manually remove mappings seems |
| 607 | * like a pointless exercise since they're going away anyway. |
| 608 | * |
| 609 | * One use case might be after addmap is allowed for normal users for SHM and |
| 610 | * gets used by drivers that the server doesn't need to care about. This seems |
| 611 | * unlikely. |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 612 | * |
| 613 | * \param inode device inode. |
| 614 | * \param file_priv DRM file private. |
| 615 | * \param cmd command. |
| 616 | * \param arg pointer to a struct drm_map structure. |
| 617 | * \return zero on success or a negative value on error. |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 618 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 619 | int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data, |
| 620 | struct drm_file *file_priv) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 621 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 622 | struct drm_map *request = data; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 623 | struct drm_local_map *map = NULL; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 624 | struct drm_map_list *r_list; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 625 | int ret; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 626 | |
Daniel Vetter | e975eef | 2016-04-26 19:29:37 +0200 | [diff] [blame] | 627 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 628 | !drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 629 | return -EOPNOTSUPP; |
Daniel Vetter | e975eef | 2016-04-26 19:29:37 +0200 | [diff] [blame] | 630 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 631 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 632 | list_for_each_entry(r_list, &dev->maplist, head) { |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 633 | if (r_list->map && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 634 | r_list->user_token == (unsigned long)request->handle && |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 635 | r_list->map->flags & _DRM_REMOVABLE) { |
| 636 | map = r_list->map; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
Matt Roper | 1e55a53 | 2019-02-01 17:23:26 -0800 | [diff] [blame] | 641 | /* List has wrapped around to the head pointer, or it's empty we didn't |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 642 | * find anything. |
| 643 | */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 644 | if (list_empty(&dev->maplist) || !map) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 645 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 646 | return -EINVAL; |
| 647 | } |
| 648 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 649 | /* Register and framebuffer maps are permanent */ |
| 650 | if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 651 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 655 | ret = drm_legacy_rmmap_locked(dev, map); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 656 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 657 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 658 | |
| 659 | return ret; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 662 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | * Cleanup after an error on one of the addbufs() functions. |
| 664 | * |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 665 | * \param dev DRM device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | * \param entry buffer entry where the error occurred. |
| 667 | * |
| 668 | * Frees any pages and buffers associated with the given entry. |
| 669 | */ |
Paul McQuade | 2bcfcbf | 2018-03-19 00:52:22 +0000 | [diff] [blame] | 670 | static void drm_cleanup_buf_error(struct drm_device *dev, |
| 671 | struct drm_buf_entry *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
| 673 | int i; |
| 674 | |
| 675 | if (entry->seg_count) { |
| 676 | for (i = 0; i < entry->seg_count; i++) { |
| 677 | if (entry->seglist[i]) { |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 678 | drm_pci_free(dev, entry->seglist[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 681 | kfree(entry->seglist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | entry->seg_count = 0; |
| 684 | } |
| 685 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 686 | if (entry->buf_count) { |
| 687 | for (i = 0; i < entry->buf_count; i++) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 688 | kfree(entry->buflist[i].dev_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 690 | kfree(entry->buflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | entry->buf_count = 0; |
| 693 | } |
| 694 | } |
| 695 | |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 696 | #if IS_ENABLED(CONFIG_AGP) |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 697 | /* |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 698 | * Add AGP buffers for DMA transfers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | * |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 700 | * \param dev struct drm_device to which the buffers are to be added. |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 701 | * \param request pointer to a struct drm_buf_desc describing the request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 703 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | * After some sanity checks creates a drm_buf structure for each buffer and |
| 705 | * reallocates the buffer list of the same size order to accommodate the new |
| 706 | * buffers. |
| 707 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 708 | int drm_legacy_addbufs_agp(struct drm_device *dev, |
| 709 | struct drm_buf_desc *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 711 | struct drm_device_dma *dma = dev->dma; |
| 712 | struct drm_buf_entry *entry; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 713 | struct drm_agp_mem *agp_entry; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 714 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | unsigned long offset; |
| 716 | unsigned long agp_offset; |
| 717 | int count; |
| 718 | int order; |
| 719 | int size; |
| 720 | int alignment; |
| 721 | int page_order; |
| 722 | int total; |
| 723 | int byte_count; |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 724 | int i, valid; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 725 | struct drm_buf **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 727 | if (!dma) |
| 728 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 730 | count = request->count; |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 731 | order = order_base_2(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | size = 1 << order; |
| 733 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 734 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
| 735 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 737 | total = PAGE_SIZE << page_order; |
| 738 | |
| 739 | byte_count = 0; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 740 | agp_offset = dev->agp->base + request->agp_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 742 | DRM_DEBUG("count: %d\n", count); |
| 743 | DRM_DEBUG("order: %d\n", order); |
| 744 | DRM_DEBUG("size: %d\n", size); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 745 | DRM_DEBUG("agp_offset: %lx\n", agp_offset); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 746 | DRM_DEBUG("alignment: %d\n", alignment); |
| 747 | DRM_DEBUG("page_order: %d\n", page_order); |
| 748 | DRM_DEBUG("total: %d\n", total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 750 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 751 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 753 | /* Make sure buffers are located in AGP memory that we own */ |
| 754 | valid = 0; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 755 | list_for_each_entry(agp_entry, &dev->agp->memory, head) { |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 756 | if ((agp_offset >= agp_entry->bound) && |
| 757 | (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) { |
| 758 | valid = 1; |
| 759 | break; |
| 760 | } |
| 761 | } |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 762 | if (!list_empty(&dev->agp->memory) && !valid) { |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 763 | DRM_DEBUG("zone invalid\n"); |
| 764 | return -EINVAL; |
| 765 | } |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 766 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 767 | if (dev->buf_use) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 768 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return -EBUSY; |
| 770 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 771 | atomic_inc(&dev->buf_alloc); |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 772 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 774 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 776 | if (entry->buf_count) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 777 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 778 | atomic_dec(&dev->buf_alloc); |
| 779 | return -ENOMEM; /* May only call once for each order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | if (count < 0 || count > 4096) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 783 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 784 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | return -EINVAL; |
| 786 | } |
| 787 | |
Markus Elfring | 81a4413 | 2016-09-19 17:24:20 +0200 | [diff] [blame] | 788 | entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 789 | if (!entry->buflist) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 790 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 791 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | return -ENOMEM; |
| 793 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | entry->buf_size = size; |
| 796 | entry->page_order = page_order; |
| 797 | |
| 798 | offset = 0; |
| 799 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 800 | while (entry->buf_count < count) { |
| 801 | buf = &entry->buflist[entry->buf_count]; |
| 802 | buf->idx = dma->buf_count + entry->buf_count; |
| 803 | buf->total = alignment; |
| 804 | buf->order = order; |
| 805 | buf->used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 807 | buf->offset = (dma->byte_count + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | buf->bus_address = agp_offset + offset; |
| 809 | buf->address = (void *)(agp_offset + offset); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 810 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | buf->waiting = 0; |
| 812 | buf->pending = 0; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 813 | buf->file_priv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 816 | buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 817 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /* Set count correctly so we free the proper amount. */ |
| 819 | entry->buf_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 820 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 821 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 822 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | return -ENOMEM; |
| 824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 826 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
| 828 | offset += alignment; |
| 829 | entry->buf_count++; |
| 830 | byte_count += PAGE_SIZE << page_order; |
| 831 | } |
| 832 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 833 | DRM_DEBUG("byte_count: %d\n", byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 835 | temp_buflist = krealloc(dma->buflist, |
| 836 | (dma->buf_count + entry->buf_count) * |
| 837 | sizeof(*dma->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 838 | if (!temp_buflist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 840 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 841 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 842 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return -ENOMEM; |
| 844 | } |
| 845 | dma->buflist = temp_buflist; |
| 846 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 847 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 849 | } |
| 850 | |
| 851 | dma->buf_count += entry->buf_count; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 852 | dma->seg_count += entry->seg_count; |
| 853 | dma->page_count += byte_count >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | dma->byte_count += byte_count; |
| 855 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 856 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
| 857 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 859 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 861 | request->count = entry->buf_count; |
| 862 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
| 864 | dma->flags = _DRM_DMA_USE_AGP; |
| 865 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 866 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | return 0; |
| 868 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 869 | EXPORT_SYMBOL(drm_legacy_addbufs_agp); |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 870 | #endif /* CONFIG_AGP */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 871 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 872 | int drm_legacy_addbufs_pci(struct drm_device *dev, |
| 873 | struct drm_buf_desc *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 875 | struct drm_device_dma *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | int count; |
| 877 | int order; |
| 878 | int size; |
| 879 | int total; |
| 880 | int page_order; |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 881 | struct drm_buf_entry *entry; |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 882 | drm_dma_handle_t *dmah; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 883 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | int alignment; |
| 885 | unsigned long offset; |
| 886 | int i; |
| 887 | int byte_count; |
| 888 | int page_count; |
| 889 | unsigned long *temp_pagelist; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 890 | struct drm_buf **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 892 | if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 893 | return -EOPNOTSUPP; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 894 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 895 | if (!dma) |
| 896 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 898 | if (!capable(CAP_SYS_ADMIN)) |
| 899 | return -EPERM; |
| 900 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 901 | count = request->count; |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 902 | order = order_base_2(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | size = 1 << order; |
| 904 | |
Daniel Vetter | a344a7e | 2011-10-26 00:54:41 +0200 | [diff] [blame] | 905 | DRM_DEBUG("count=%d, size=%d (%d), order=%d\n", |
| 906 | request->count, request->size, size, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 908 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 909 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 911 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 912 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 914 | total = PAGE_SIZE << page_order; |
| 915 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 916 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 917 | if (dev->buf_use) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 918 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return -EBUSY; |
| 920 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 921 | atomic_inc(&dev->buf_alloc); |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 922 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 924 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 926 | if (entry->buf_count) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 927 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 928 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | return -ENOMEM; /* May only call once for each order */ |
| 930 | } |
| 931 | |
| 932 | if (count < 0 || count > 4096) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 933 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 934 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | return -EINVAL; |
| 936 | } |
| 937 | |
Markus Elfring | ed6dee4 | 2016-09-19 17:17:34 +0200 | [diff] [blame] | 938 | entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 939 | if (!entry->buflist) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 940 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 941 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | return -ENOMEM; |
| 943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
Markus Elfring | ed6dee4 | 2016-09-19 17:17:34 +0200 | [diff] [blame] | 945 | entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 946 | if (!entry->seglist) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 947 | kfree(entry->buflist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 948 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 949 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | return -ENOMEM; |
| 951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
| 953 | /* Keep the original pagelist until we know all the allocations |
| 954 | * have succeeded |
| 955 | */ |
Markus Elfring | 2027400 | 2016-09-19 17:07:06 +0200 | [diff] [blame] | 956 | temp_pagelist = kmalloc_array(dma->page_count + (count << page_order), |
| 957 | sizeof(*dma->pagelist), |
| 958 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | if (!temp_pagelist) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 960 | kfree(entry->buflist); |
| 961 | kfree(entry->seglist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 962 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 963 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | return -ENOMEM; |
| 965 | } |
| 966 | memcpy(temp_pagelist, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 967 | dma->pagelist, dma->page_count * sizeof(*dma->pagelist)); |
| 968 | DRM_DEBUG("pagelist: %d entries\n", |
| 969 | dma->page_count + (count << page_order)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 971 | entry->buf_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | entry->page_order = page_order; |
| 973 | byte_count = 0; |
| 974 | page_count = 0; |
| 975 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 976 | while (entry->buf_count < count) { |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 977 | |
Zhenyu Wang | e6be8d9 | 2010-01-05 11:25:05 +0800 | [diff] [blame] | 978 | dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000); |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 979 | |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 980 | if (!dmah) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | /* Set count correctly so we free the proper amount. */ |
| 982 | entry->buf_count = count; |
| 983 | entry->seg_count = count; |
| 984 | drm_cleanup_buf_error(dev, entry); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 985 | kfree(temp_pagelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 986 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 987 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | return -ENOMEM; |
| 989 | } |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 990 | entry->seglist[entry->seg_count++] = dmah; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 991 | for (i = 0; i < (1 << page_order); i++) { |
| 992 | DRM_DEBUG("page %d @ 0x%08lx\n", |
| 993 | dma->page_count + page_count, |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 994 | (unsigned long)dmah->vaddr + PAGE_SIZE * i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | temp_pagelist[dma->page_count + page_count++] |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 996 | = (unsigned long)dmah->vaddr + PAGE_SIZE * i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 998 | for (offset = 0; |
| 999 | offset + size <= total && entry->buf_count < count; |
| 1000 | offset += alignment, ++entry->buf_count) { |
| 1001 | buf = &entry->buflist[entry->buf_count]; |
| 1002 | buf->idx = dma->buf_count + entry->buf_count; |
| 1003 | buf->total = alignment; |
| 1004 | buf->order = order; |
| 1005 | buf->used = 0; |
| 1006 | buf->offset = (dma->byte_count + byte_count + offset); |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 1007 | buf->address = (void *)(dmah->vaddr + offset); |
| 1008 | buf->bus_address = dmah->busaddr + offset; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1009 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | buf->waiting = 0; |
| 1011 | buf->pending = 0; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1012 | buf->file_priv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
| 1014 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 1015 | buf->dev_private = kzalloc(buf->dev_priv_size, |
| 1016 | GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1017 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | /* Set count correctly so we free the proper amount. */ |
| 1019 | entry->buf_count = count; |
| 1020 | entry->seg_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1021 | drm_cleanup_buf_error(dev, entry); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1022 | kfree(temp_pagelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1023 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1024 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return -ENOMEM; |
| 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1028 | DRM_DEBUG("buffer %d @ %p\n", |
| 1029 | entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | byte_count += PAGE_SIZE << page_order; |
| 1032 | } |
| 1033 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1034 | temp_buflist = krealloc(dma->buflist, |
| 1035 | (dma->buf_count + entry->buf_count) * |
| 1036 | sizeof(*dma->buflist), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | if (!temp_buflist) { |
| 1038 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1039 | drm_cleanup_buf_error(dev, entry); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1040 | kfree(temp_pagelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1041 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1042 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | return -ENOMEM; |
| 1044 | } |
| 1045 | dma->buflist = temp_buflist; |
| 1046 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1047 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 1049 | } |
| 1050 | |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 1051 | /* No allocations failed, so now we can replace the original pagelist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | * with the new one. |
| 1053 | */ |
| 1054 | if (dma->page_count) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1055 | kfree(dma->pagelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | dma->pagelist = temp_pagelist; |
| 1058 | |
| 1059 | dma->buf_count += entry->buf_count; |
| 1060 | dma->seg_count += entry->seg_count; |
| 1061 | dma->page_count += entry->seg_count << page_order; |
| 1062 | dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order); |
| 1063 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1064 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1066 | request->count = entry->buf_count; |
| 1067 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
George Sapountzis | 3417f33 | 2006-10-24 12:03:04 -0700 | [diff] [blame] | 1069 | if (request->flags & _DRM_PCI_BUFFER_RO) |
| 1070 | dma->flags = _DRM_DMA_USE_PCI_RO; |
| 1071 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1072 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | return 0; |
| 1074 | |
| 1075 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1076 | EXPORT_SYMBOL(drm_legacy_addbufs_pci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1078 | static int drm_legacy_addbufs_sg(struct drm_device *dev, |
| 1079 | struct drm_buf_desc *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1081 | struct drm_device_dma *dma = dev->dma; |
| 1082 | struct drm_buf_entry *entry; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 1083 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | unsigned long offset; |
| 1085 | unsigned long agp_offset; |
| 1086 | int count; |
| 1087 | int order; |
| 1088 | int size; |
| 1089 | int alignment; |
| 1090 | int page_order; |
| 1091 | int total; |
| 1092 | int byte_count; |
| 1093 | int i; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 1094 | struct drm_buf **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1096 | if (!drm_core_check_feature(dev, DRIVER_SG)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1097 | return -EOPNOTSUPP; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1098 | |
| 1099 | if (!dma) |
| 1100 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 1102 | if (!capable(CAP_SYS_ADMIN)) |
| 1103 | return -EPERM; |
| 1104 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1105 | count = request->count; |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 1106 | order = order_base_2(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | size = 1 << order; |
| 1108 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1109 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
| 1110 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 1112 | total = PAGE_SIZE << page_order; |
| 1113 | |
| 1114 | byte_count = 0; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1115 | agp_offset = request->agp_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1117 | DRM_DEBUG("count: %d\n", count); |
| 1118 | DRM_DEBUG("order: %d\n", order); |
| 1119 | DRM_DEBUG("size: %d\n", size); |
| 1120 | DRM_DEBUG("agp_offset: %lu\n", agp_offset); |
| 1121 | DRM_DEBUG("alignment: %d\n", alignment); |
| 1122 | DRM_DEBUG("page_order: %d\n", page_order); |
| 1123 | DRM_DEBUG("total: %d\n", total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1125 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 1126 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1128 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1129 | if (dev->buf_use) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1130 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | return -EBUSY; |
| 1132 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1133 | atomic_inc(&dev->buf_alloc); |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1134 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1136 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1138 | if (entry->buf_count) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1139 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1140 | atomic_dec(&dev->buf_alloc); |
| 1141 | return -ENOMEM; /* May only call once for each order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | if (count < 0 || count > 4096) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1145 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1146 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | return -EINVAL; |
| 1148 | } |
| 1149 | |
Markus Elfring | b5a2ecd | 2016-09-19 17:30:31 +0200 | [diff] [blame] | 1150 | entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1151 | if (!entry->buflist) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1152 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1153 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | return -ENOMEM; |
| 1155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
| 1157 | entry->buf_size = size; |
| 1158 | entry->page_order = page_order; |
| 1159 | |
| 1160 | offset = 0; |
| 1161 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1162 | while (entry->buf_count < count) { |
| 1163 | buf = &entry->buflist[entry->buf_count]; |
| 1164 | buf->idx = dma->buf_count + entry->buf_count; |
| 1165 | buf->total = alignment; |
| 1166 | buf->order = order; |
| 1167 | buf->used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1169 | buf->offset = (dma->byte_count + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | buf->bus_address = agp_offset + offset; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1171 | buf->address = (void *)(agp_offset + offset |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 1172 | + (unsigned long)dev->sg->virtual); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1173 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | buf->waiting = 0; |
| 1175 | buf->pending = 0; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1176 | buf->file_priv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
| 1178 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 1179 | buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1180 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | /* Set count correctly so we free the proper amount. */ |
| 1182 | entry->buf_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1183 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1184 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1185 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return -ENOMEM; |
| 1187 | } |
| 1188 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1189 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | offset += alignment; |
| 1192 | entry->buf_count++; |
| 1193 | byte_count += PAGE_SIZE << page_order; |
| 1194 | } |
| 1195 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1196 | DRM_DEBUG("byte_count: %d\n", byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1198 | temp_buflist = krealloc(dma->buflist, |
| 1199 | (dma->buf_count + entry->buf_count) * |
| 1200 | sizeof(*dma->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1201 | if (!temp_buflist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1203 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1204 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1205 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | return -ENOMEM; |
| 1207 | } |
| 1208 | dma->buflist = temp_buflist; |
| 1209 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1210 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 1212 | } |
| 1213 | |
| 1214 | dma->buf_count += entry->buf_count; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 1215 | dma->seg_count += entry->seg_count; |
| 1216 | dma->page_count += byte_count >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | dma->byte_count += byte_count; |
| 1218 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1219 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
| 1220 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1222 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1224 | request->count = entry->buf_count; |
| 1225 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
| 1227 | dma->flags = _DRM_DMA_USE_SG; |
| 1228 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1229 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | return 0; |
| 1231 | } |
| 1232 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 1233 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | * Add buffers for DMA transfers (ioctl). |
| 1235 | * |
| 1236 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1237 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | * \param cmd command. |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 1239 | * \param arg pointer to a struct drm_buf_desc request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | * \return zero on success or a negative number on failure. |
| 1241 | * |
| 1242 | * According with the memory type specified in drm_buf_desc::flags and the |
| 1243 | * build options, it dispatches the call either to addbufs_agp(), |
| 1244 | * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent |
| 1245 | * PCI memory respectively. |
| 1246 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1247 | int drm_legacy_addbufs(struct drm_device *dev, void *data, |
| 1248 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1250 | struct drm_buf_desc *request = data; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1251 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1252 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 1253 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1254 | return -EOPNOTSUPP; |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1257 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 1259 | #if IS_ENABLED(CONFIG_AGP) |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1260 | if (request->flags & _DRM_AGP_BUFFER) |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1261 | ret = drm_legacy_addbufs_agp(dev, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | else |
| 1263 | #endif |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1264 | if (request->flags & _DRM_SG_BUFFER) |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1265 | ret = drm_legacy_addbufs_sg(dev, request); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1266 | else if (request->flags & _DRM_FB_BUFFER) |
Daniel Vetter | 687fbb2 | 2013-08-08 15:41:24 +0200 | [diff] [blame] | 1267 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | else |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1269 | ret = drm_legacy_addbufs_pci(dev, request); |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1270 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1271 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 1274 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | * Get information about the buffer mappings. |
| 1276 | * |
| 1277 | * This was originally mean for debugging purposes, or by a sophisticated |
| 1278 | * client library to determine how best to use the available buffers (e.g., |
| 1279 | * large buffers can be used for image transfer). |
| 1280 | * |
| 1281 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1282 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | * \param cmd command. |
| 1284 | * \param arg pointer to a drm_buf_info structure. |
| 1285 | * \return zero on success or a negative number on failure. |
| 1286 | * |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1287 | * Increments drm_device::buf_use while holding the drm_device::buf_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | * lock, preventing of allocating more buffers after this call. Information |
| 1289 | * about each requested buffer is then copied into user space. |
| 1290 | */ |
Al Viro | 5c7640a | 2017-05-24 17:54:09 -0400 | [diff] [blame] | 1291 | int __drm_legacy_infobufs(struct drm_device *dev, |
| 1292 | void *data, int *p, |
| 1293 | int (*f)(void *, int, struct drm_buf_entry *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1295 | struct drm_device_dma *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | int i; |
| 1297 | int count; |
| 1298 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 1299 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1300 | return -EOPNOTSUPP; |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1303 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1305 | if (!dma) |
| 1306 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1308 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1309 | if (atomic_read(&dev->buf_alloc)) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1310 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | return -EBUSY; |
| 1312 | } |
| 1313 | ++dev->buf_use; /* Can't allocate more after this call */ |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1314 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1316 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
| 1317 | if (dma->bufs[i].buf_count) |
| 1318 | ++count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1321 | DRM_DEBUG("count = %d\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
Al Viro | 5c7640a | 2017-05-24 17:54:09 -0400 | [diff] [blame] | 1323 | if (*p >= count) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1324 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
Al Viro | 5c7640a | 2017-05-24 17:54:09 -0400 | [diff] [blame] | 1325 | struct drm_buf_entry *from = &dma->bufs[i]; |
| 1326 | if (from->buf_count) { |
| 1327 | if (f(data, count, from) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | return -EFAULT; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1329 | DRM_DEBUG("%d %d %d %d %d\n", |
| 1330 | i, |
| 1331 | dma->bufs[i].buf_count, |
| 1332 | dma->bufs[i].buf_size, |
David Herrmann | b008c0f | 2014-07-23 17:26:36 +0200 | [diff] [blame] | 1333 | dma->bufs[i].low_mark, |
| 1334 | dma->bufs[i].high_mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | ++count; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
Al Viro | 5c7640a | 2017-05-24 17:54:09 -0400 | [diff] [blame] | 1339 | *p = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Al Viro | 5c7640a | 2017-05-24 17:54:09 -0400 | [diff] [blame] | 1344 | static int copy_one_buf(void *data, int count, struct drm_buf_entry *from) |
| 1345 | { |
| 1346 | struct drm_buf_info *request = data; |
| 1347 | struct drm_buf_desc __user *to = &request->list[count]; |
| 1348 | struct drm_buf_desc v = {.count = from->buf_count, |
| 1349 | .size = from->buf_size, |
| 1350 | .low_mark = from->low_mark, |
| 1351 | .high_mark = from->high_mark}; |
Dan Carpenter | 74b67ef | 2019-06-18 16:18:43 +0300 | [diff] [blame] | 1352 | |
| 1353 | if (copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags))) |
| 1354 | return -EFAULT; |
| 1355 | return 0; |
Al Viro | 5c7640a | 2017-05-24 17:54:09 -0400 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | int drm_legacy_infobufs(struct drm_device *dev, void *data, |
| 1359 | struct drm_file *file_priv) |
| 1360 | { |
| 1361 | struct drm_buf_info *request = data; |
| 1362 | return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf); |
| 1363 | } |
| 1364 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 1365 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | * Specifies a low and high water mark for buffer allocation |
| 1367 | * |
| 1368 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1369 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | * \param cmd command. |
| 1371 | * \param arg a pointer to a drm_buf_desc structure. |
| 1372 | * \return zero on success or a negative number on failure. |
| 1373 | * |
| 1374 | * Verifies that the size order is bounded between the admissible orders and |
| 1375 | * updates the respective drm_device_dma::bufs entry low and high water mark. |
| 1376 | * |
| 1377 | * \note This ioctl is deprecated and mostly never used. |
| 1378 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1379 | int drm_legacy_markbufs(struct drm_device *dev, void *data, |
| 1380 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1382 | struct drm_device_dma *dma = dev->dma; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1383 | struct drm_buf_desc *request = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | int order; |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1385 | struct drm_buf_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 1387 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1388 | return -EOPNOTSUPP; |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1391 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1393 | if (!dma) |
| 1394 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1396 | DRM_DEBUG("%d, %d, %d\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1397 | request->size, request->low_mark, request->high_mark); |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 1398 | order = order_base_2(request->size); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1399 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 1400 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | entry = &dma->bufs[order]; |
| 1402 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1403 | if (request->low_mark < 0 || request->low_mark > entry->buf_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | return -EINVAL; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1405 | if (request->high_mark < 0 || request->high_mark > entry->buf_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | return -EINVAL; |
| 1407 | |
David Herrmann | b008c0f | 2014-07-23 17:26:36 +0200 | [diff] [blame] | 1408 | entry->low_mark = request->low_mark; |
| 1409 | entry->high_mark = request->high_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 1414 | /* |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1415 | * Unreserve the buffers in list, previously reserved using drmDMA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | * |
| 1417 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1418 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | * \param cmd command. |
| 1420 | * \param arg pointer to a drm_buf_free structure. |
| 1421 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1422 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | * Calls free_buffer() for each used buffer. |
| 1424 | * This function is primarily used for debugging. |
| 1425 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1426 | int drm_legacy_freebufs(struct drm_device *dev, void *data, |
| 1427 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1429 | struct drm_device_dma *dma = dev->dma; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1430 | struct drm_buf_free *request = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | int i; |
| 1432 | int idx; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 1433 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 1435 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1436 | return -EOPNOTSUPP; |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1439 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1441 | if (!dma) |
| 1442 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1444 | DRM_DEBUG("%d\n", request->count); |
| 1445 | for (i = 0; i < request->count; i++) { |
| 1446 | if (copy_from_user(&idx, &request->list[i], sizeof(idx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | return -EFAULT; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1448 | if (idx < 0 || idx >= dma->buf_count) { |
| 1449 | DRM_ERROR("Index %d (of %d max)\n", |
| 1450 | idx, dma->buf_count - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | return -EINVAL; |
| 1452 | } |
Gustavo A. R. Silva | a378050 | 2018-10-16 11:55:49 +0200 | [diff] [blame] | 1453 | idx = array_index_nospec(idx, dma->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | buf = dma->buflist[idx]; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1455 | if (buf->file_priv != file_priv) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1456 | DRM_ERROR("Process %d freeing buffer not owned\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1457 | task_pid_nr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | return -EINVAL; |
| 1459 | } |
Daniel Vetter | a266162 | 2014-09-11 07:41:51 +0200 | [diff] [blame] | 1460 | drm_legacy_free_buffer(dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
Benjamin Gaignard | abee549 | 2020-03-06 11:29:36 +0100 | [diff] [blame] | 1466 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | * Maps all of the DMA buffers into client-virtual space (ioctl). |
| 1468 | * |
| 1469 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1470 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | * \param cmd command. |
| 1472 | * \param arg pointer to a drm_buf_map structure. |
| 1473 | * \return zero on success or a negative number on failure. |
| 1474 | * |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1475 | * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information |
| 1476 | * about each buffer into user space. For PCI buffers, it calls vm_mmap() with |
George Sapountzis | 3417f33 | 2006-10-24 12:03:04 -0700 | [diff] [blame] | 1477 | * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls |
| 1478 | * drm_mmap_dma(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | */ |
Al Viro | 87d3ce1 | 2017-05-25 16:24:20 -0400 | [diff] [blame] | 1480 | int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p, |
| 1481 | void __user **v, |
| 1482 | int (*f)(void *, int, unsigned long, |
Paul McQuade | 2bcfcbf | 2018-03-19 00:52:22 +0000 | [diff] [blame] | 1483 | struct drm_buf *), |
| 1484 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1486 | struct drm_device_dma *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | int retcode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | unsigned long virtual; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | int i; |
| 1490 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 1491 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1492 | return -EOPNOTSUPP; |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1495 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1497 | if (!dma) |
| 1498 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1500 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1501 | if (atomic_read(&dev->buf_alloc)) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1502 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | return -EBUSY; |
| 1504 | } |
| 1505 | dev->buf_use++; /* Can't allocate more after this call */ |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1506 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Al Viro | 87d3ce1 | 2017-05-25 16:24:20 -0400 | [diff] [blame] | 1508 | if (*p >= dma->buf_count) { |
Daniel Vetter | d990675 | 2013-12-11 11:34:35 +0100 | [diff] [blame] | 1509 | if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP)) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1510 | || (drm_core_check_feature(dev, DRIVER_SG) |
Daniel Vetter | 687fbb2 | 2013-08-08 15:41:24 +0200 | [diff] [blame] | 1511 | && (dma->flags & _DRM_DMA_USE_SG))) { |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 1512 | struct drm_local_map *map = dev->agp_buffer_map; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 1513 | unsigned long token = dev->agp_buffer_token; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1515 | if (!map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | retcode = -EINVAL; |
| 1517 | goto done; |
| 1518 | } |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1519 | virtual = vm_mmap(file_priv->filp, 0, map->size, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1520 | PROT_READ | PROT_WRITE, |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1521 | MAP_SHARED, |
| 1522 | token); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | } else { |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1524 | virtual = vm_mmap(file_priv->filp, 0, dma->byte_count, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1525 | PROT_READ | PROT_WRITE, |
| 1526 | MAP_SHARED, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1528 | if (virtual > -1024UL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | /* Real error */ |
| 1530 | retcode = (signed long)virtual; |
| 1531 | goto done; |
| 1532 | } |
Al Viro | 87d3ce1 | 2017-05-25 16:24:20 -0400 | [diff] [blame] | 1533 | *v = (void __user *)virtual; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1535 | for (i = 0; i < dma->buf_count; i++) { |
Al Viro | 87d3ce1 | 2017-05-25 16:24:20 -0400 | [diff] [blame] | 1536 | if (f(data, i, virtual, dma->buflist[i]) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | retcode = -EFAULT; |
| 1538 | goto done; |
| 1539 | } |
| 1540 | } |
| 1541 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1542 | done: |
Al Viro | 87d3ce1 | 2017-05-25 16:24:20 -0400 | [diff] [blame] | 1543 | *p = dma->buf_count; |
| 1544 | DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | |
| 1546 | return retcode; |
| 1547 | } |
| 1548 | |
Al Viro | 87d3ce1 | 2017-05-25 16:24:20 -0400 | [diff] [blame] | 1549 | static int map_one_buf(void *data, int idx, unsigned long virtual, |
| 1550 | struct drm_buf *buf) |
| 1551 | { |
| 1552 | struct drm_buf_map *request = data; |
| 1553 | unsigned long address = virtual + buf->offset; /* *** */ |
| 1554 | |
| 1555 | if (copy_to_user(&request->list[idx].idx, &buf->idx, |
| 1556 | sizeof(request->list[0].idx))) |
| 1557 | return -EFAULT; |
| 1558 | if (copy_to_user(&request->list[idx].total, &buf->total, |
| 1559 | sizeof(request->list[0].total))) |
| 1560 | return -EFAULT; |
| 1561 | if (clear_user(&request->list[idx].used, sizeof(int))) |
| 1562 | return -EFAULT; |
| 1563 | if (copy_to_user(&request->list[idx].address, &address, |
| 1564 | sizeof(address))) |
| 1565 | return -EFAULT; |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | int drm_legacy_mapbufs(struct drm_device *dev, void *data, |
| 1570 | struct drm_file *file_priv) |
| 1571 | { |
| 1572 | struct drm_buf_map *request = data; |
| 1573 | return __drm_legacy_mapbufs(dev, data, &request->count, |
| 1574 | &request->virtual, map_one_buf, |
| 1575 | file_priv); |
| 1576 | } |
| 1577 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1578 | int drm_legacy_dma_ioctl(struct drm_device *dev, void *data, |
Daniel Vetter | 6eb9278 | 2013-08-08 15:41:29 +0200 | [diff] [blame] | 1579 | struct drm_file *file_priv) |
| 1580 | { |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 1581 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 1582 | return -EOPNOTSUPP; |
Daniel Vetter | 6eb9278 | 2013-08-08 15:41:29 +0200 | [diff] [blame] | 1583 | |
| 1584 | if (dev->driver->dma_ioctl) |
| 1585 | return dev->driver->dma_ioctl(dev, data, file_priv); |
| 1586 | else |
| 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1590 | struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev) |
Daniel Vetter | bd0c0ce | 2013-07-10 14:11:56 +0200 | [diff] [blame] | 1591 | { |
| 1592 | struct drm_map_list *entry; |
| 1593 | |
| 1594 | list_for_each_entry(entry, &dev->maplist, head) { |
| 1595 | if (entry->map && entry->map->type == _DRM_SHM && |
| 1596 | (entry->map->flags & _DRM_CONTAINS_LOCK)) { |
| 1597 | return entry->map; |
| 1598 | } |
| 1599 | } |
| 1600 | return NULL; |
| 1601 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1602 | EXPORT_SYMBOL(drm_legacy_getsarea); |