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