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 || |
| 54 | entry->master != dev->primary->master) |
| 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 */ |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 248 | if (dev->primary->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 | } |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 253 | dev->sigdata.lock = dev->primary->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)) |
| 359 | list->master = dev->primary->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 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 399 | err = drm_addmap_core(dev, map->offset, map->size, map->type, |
| 400 | map->flags, &maplist); |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 401 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 402 | if (err) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 403 | return err; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 404 | |
Dave Airlie | 67e1a01 | 2005-10-24 18:41:39 +1000 | [diff] [blame] | 405 | /* 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] | 406 | map->handle = (void *)(unsigned long)maplist->user_token; |
Andy Lutomirski | 0dd99f1 | 2013-05-13 23:58:48 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * It appears that there are no users of this value whatsoever -- |
| 410 | * drmAddMap just discards it. Let's not encourage its use. |
| 411 | * (Keeping drm_addmap_core's returned mtrr value would be wrong -- |
| 412 | * it's not a real mtrr index anymore.) |
| 413 | */ |
| 414 | map->mtrr = -1; |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | return 0; |
Dave Airlie | 88f399c | 2005-08-20 17:43:33 +1000 | [diff] [blame] | 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | /** |
| 420 | * Remove a map private from list and deallocate resources if the mapping |
| 421 | * isn't in use. |
| 422 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | * Searches the map on drm_device::maplist, removes it from the list, see if |
| 424 | * its being used, and free any associate resource (such as MTRR's) if it's not |
| 425 | * being on use. |
| 426 | * |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 427 | * \sa drm_legacy_addmap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 429 | 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] | 430 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 431 | struct drm_map_list *r_list = NULL, *list_t; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 432 | drm_dma_handle_t dmah; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 433 | int found = 0; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 434 | struct drm_master *master; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 436 | /* Find the list entry for the map and remove it */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 437 | list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) { |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 438 | if (r_list->map == map) { |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 439 | master = r_list->master; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 440 | list_del(&r_list->head); |
Thomas Hellstrom | 1545085 | 2007-02-08 16:14:05 +1100 | [diff] [blame] | 441 | drm_ht_remove_key(&dev->map_hash, |
| 442 | r_list->user_token >> PAGE_SHIFT); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 443 | kfree(r_list); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 444 | found = 1; |
Dave Airlie | 2d0f9ea | 2005-07-10 14:34:13 +1000 | [diff] [blame] | 445 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 448 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 449 | if (!found) |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 450 | return -EINVAL; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 451 | |
| 452 | switch (map->type) { |
| 453 | case _DRM_REGISTERS: |
Christoph Hellwig | 004a772 | 2007-01-08 21:56:59 +1100 | [diff] [blame] | 454 | iounmap(map->handle); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 455 | /* FALLTHROUGH */ |
| 456 | case _DRM_FRAME_BUFFER: |
Daniel Vetter | 2818564 | 2013-08-08 15:41:27 +0200 | [diff] [blame] | 457 | arch_phys_wc_del(map->mtrr); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 458 | break; |
| 459 | case _DRM_SHM: |
| 460 | vfree(map->handle); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 461 | if (master) { |
| 462 | if (dev->sigdata.lock == master->lock.hw_lock) |
| 463 | dev->sigdata.lock = NULL; |
| 464 | master->lock.hw_lock = NULL; /* SHM removed */ |
| 465 | master->lock.file_priv = NULL; |
Thomas Hellstrom | 171901d | 2009-03-02 11:10:55 +0100 | [diff] [blame] | 466 | wake_up_interruptible_all(&master->lock.lock_queue); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 467 | } |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 468 | break; |
| 469 | case _DRM_AGP: |
| 470 | case _DRM_SCATTER_GATHER: |
| 471 | break; |
| 472 | case _DRM_CONSISTENT: |
| 473 | dmah.vaddr = map->handle; |
| 474 | dmah.busaddr = map->offset; |
| 475 | dmah.size = map->size; |
Daniel Vetter | 1c96e84 | 2014-09-10 12:43:51 +0200 | [diff] [blame] | 476 | __drm_legacy_pci_free(dev, &dmah); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 477 | break; |
| 478 | } |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 479 | kfree(map); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | return 0; |
| 482 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 483 | EXPORT_SYMBOL(drm_legacy_rmmap_locked); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 484 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 485 | int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map) |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 486 | { |
| 487 | int ret; |
| 488 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 489 | mutex_lock(&dev->struct_mutex); |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 490 | ret = drm_legacy_rmmap_locked(dev, map); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 491 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 492 | |
| 493 | return ret; |
| 494 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 495 | EXPORT_SYMBOL(drm_legacy_rmmap); |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 496 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 497 | /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on |
| 498 | * the last close of the device, and this is necessary for cleanup when things |
| 499 | * exit uncleanly. Therefore, having userland manually remove mappings seems |
| 500 | * like a pointless exercise since they're going away anyway. |
| 501 | * |
| 502 | * One use case might be after addmap is allowed for normal users for SHM and |
| 503 | * gets used by drivers that the server doesn't need to care about. This seems |
| 504 | * unlikely. |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 505 | * |
| 506 | * \param inode device inode. |
| 507 | * \param file_priv DRM file private. |
| 508 | * \param cmd command. |
| 509 | * \param arg pointer to a struct drm_map structure. |
| 510 | * \return zero on success or a negative value on error. |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 511 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 512 | int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data, |
| 513 | struct drm_file *file_priv) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 514 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 515 | struct drm_map *request = data; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 516 | struct drm_local_map *map = NULL; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 517 | struct drm_map_list *r_list; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 518 | int ret; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 519 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 520 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 521 | list_for_each_entry(r_list, &dev->maplist, head) { |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 522 | if (r_list->map && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 523 | r_list->user_token == (unsigned long)request->handle && |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 524 | r_list->map->flags & _DRM_REMOVABLE) { |
| 525 | map = r_list->map; |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* List has wrapped around to the head pointer, or its empty we didn't |
| 531 | * find anything. |
| 532 | */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 533 | if (list_empty(&dev->maplist) || !map) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 534 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 535 | return -EINVAL; |
| 536 | } |
| 537 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 538 | /* Register and framebuffer maps are permanent */ |
| 539 | if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 540 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 544 | ret = drm_legacy_rmmap_locked(dev, map); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 545 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 546 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 547 | |
| 548 | return ret; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | /** |
| 552 | * Cleanup after an error on one of the addbufs() functions. |
| 553 | * |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 554 | * \param dev DRM device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | * \param entry buffer entry where the error occurred. |
| 556 | * |
| 557 | * Frees any pages and buffers associated with the given entry. |
| 558 | */ |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 559 | static void drm_cleanup_buf_error(struct drm_device * dev, |
| 560 | struct drm_buf_entry * entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
| 562 | int i; |
| 563 | |
| 564 | if (entry->seg_count) { |
| 565 | for (i = 0; i < entry->seg_count; i++) { |
| 566 | if (entry->seglist[i]) { |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 567 | drm_pci_free(dev, entry->seglist[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 570 | kfree(entry->seglist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | entry->seg_count = 0; |
| 573 | } |
| 574 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 575 | if (entry->buf_count) { |
| 576 | for (i = 0; i < entry->buf_count; i++) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 577 | kfree(entry->buflist[i].dev_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 579 | kfree(entry->buflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | entry->buf_count = 0; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | #if __OS_HAS_AGP |
| 586 | /** |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 587 | * Add AGP buffers for DMA transfers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | * |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 589 | * \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] | 590 | * \param request pointer to a struct drm_buf_desc describing the request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 592 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | * After some sanity checks creates a drm_buf structure for each buffer and |
| 594 | * reallocates the buffer list of the same size order to accommodate the new |
| 595 | * buffers. |
| 596 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 597 | int drm_legacy_addbufs_agp(struct drm_device *dev, |
| 598 | struct drm_buf_desc *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 600 | struct drm_device_dma *dma = dev->dma; |
| 601 | struct drm_buf_entry *entry; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 602 | struct drm_agp_mem *agp_entry; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 603 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | unsigned long offset; |
| 605 | unsigned long agp_offset; |
| 606 | int count; |
| 607 | int order; |
| 608 | int size; |
| 609 | int alignment; |
| 610 | int page_order; |
| 611 | int total; |
| 612 | int byte_count; |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 613 | int i, valid; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 614 | struct drm_buf **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 616 | if (!dma) |
| 617 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 619 | count = request->count; |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 620 | order = order_base_2(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | size = 1 << order; |
| 622 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 623 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
| 624 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 626 | total = PAGE_SIZE << page_order; |
| 627 | |
| 628 | byte_count = 0; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 629 | agp_offset = dev->agp->base + request->agp_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 631 | DRM_DEBUG("count: %d\n", count); |
| 632 | DRM_DEBUG("order: %d\n", order); |
| 633 | DRM_DEBUG("size: %d\n", size); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 634 | DRM_DEBUG("agp_offset: %lx\n", agp_offset); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 635 | DRM_DEBUG("alignment: %d\n", alignment); |
| 636 | DRM_DEBUG("page_order: %d\n", page_order); |
| 637 | DRM_DEBUG("total: %d\n", total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 639 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 640 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 642 | /* Make sure buffers are located in AGP memory that we own */ |
| 643 | valid = 0; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 644 | list_for_each_entry(agp_entry, &dev->agp->memory, head) { |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 645 | if ((agp_offset >= agp_entry->bound) && |
| 646 | (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) { |
| 647 | valid = 1; |
| 648 | break; |
| 649 | } |
| 650 | } |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 651 | if (!list_empty(&dev->agp->memory) && !valid) { |
Dave Airlie | 54ba2f7 | 2007-02-10 12:07:47 +1100 | [diff] [blame] | 652 | DRM_DEBUG("zone invalid\n"); |
| 653 | return -EINVAL; |
| 654 | } |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 655 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 656 | if (dev->buf_use) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 657 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return -EBUSY; |
| 659 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 660 | atomic_inc(&dev->buf_alloc); |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 661 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 663 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 665 | if (entry->buf_count) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 666 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 667 | atomic_dec(&dev->buf_alloc); |
| 668 | return -ENOMEM; /* May only call once for each order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | if (count < 0 || count > 4096) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 672 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 673 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return -EINVAL; |
| 675 | } |
| 676 | |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 677 | entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 678 | if (!entry->buflist) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 679 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 680 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return -ENOMEM; |
| 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | entry->buf_size = size; |
| 685 | entry->page_order = page_order; |
| 686 | |
| 687 | offset = 0; |
| 688 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 689 | while (entry->buf_count < count) { |
| 690 | buf = &entry->buflist[entry->buf_count]; |
| 691 | buf->idx = dma->buf_count + entry->buf_count; |
| 692 | buf->total = alignment; |
| 693 | buf->order = order; |
| 694 | buf->used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 696 | buf->offset = (dma->byte_count + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | buf->bus_address = agp_offset + offset; |
| 698 | buf->address = (void *)(agp_offset + offset); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 699 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | buf->waiting = 0; |
| 701 | buf->pending = 0; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 702 | buf->file_priv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 705 | buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 706 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | /* Set count correctly so we free the proper amount. */ |
| 708 | entry->buf_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 709 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 710 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 711 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return -ENOMEM; |
| 713 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 715 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
| 717 | offset += alignment; |
| 718 | entry->buf_count++; |
| 719 | byte_count += PAGE_SIZE << page_order; |
| 720 | } |
| 721 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 722 | DRM_DEBUG("byte_count: %d\n", byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 724 | temp_buflist = krealloc(dma->buflist, |
| 725 | (dma->buf_count + entry->buf_count) * |
| 726 | sizeof(*dma->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 727 | if (!temp_buflist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 729 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 730 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 731 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | return -ENOMEM; |
| 733 | } |
| 734 | dma->buflist = temp_buflist; |
| 735 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 736 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 738 | } |
| 739 | |
| 740 | dma->buf_count += entry->buf_count; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 741 | dma->seg_count += entry->seg_count; |
| 742 | dma->page_count += byte_count >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | dma->byte_count += byte_count; |
| 744 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 745 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
| 746 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 748 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 750 | request->count = entry->buf_count; |
| 751 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
| 753 | dma->flags = _DRM_DMA_USE_AGP; |
| 754 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 755 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | return 0; |
| 757 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 758 | EXPORT_SYMBOL(drm_legacy_addbufs_agp); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 759 | #endif /* __OS_HAS_AGP */ |
| 760 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 761 | int drm_legacy_addbufs_pci(struct drm_device *dev, |
| 762 | struct drm_buf_desc *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 764 | struct drm_device_dma *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | int count; |
| 766 | int order; |
| 767 | int size; |
| 768 | int total; |
| 769 | int page_order; |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 770 | struct drm_buf_entry *entry; |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 771 | drm_dma_handle_t *dmah; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 772 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | int alignment; |
| 774 | unsigned long offset; |
| 775 | int i; |
| 776 | int byte_count; |
| 777 | int page_count; |
| 778 | unsigned long *temp_pagelist; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 779 | struct drm_buf **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 781 | if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) |
| 782 | return -EINVAL; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 783 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 784 | if (!dma) |
| 785 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 787 | if (!capable(CAP_SYS_ADMIN)) |
| 788 | return -EPERM; |
| 789 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 790 | count = request->count; |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 791 | order = order_base_2(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | size = 1 << order; |
| 793 | |
Daniel Vetter | a344a7e | 2011-10-26 00:54:41 +0200 | [diff] [blame] | 794 | DRM_DEBUG("count=%d, size=%d (%d), order=%d\n", |
| 795 | request->count, request->size, size, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 797 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 798 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 800 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 801 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 803 | total = PAGE_SIZE << page_order; |
| 804 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 805 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 806 | if (dev->buf_use) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 807 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return -EBUSY; |
| 809 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 810 | atomic_inc(&dev->buf_alloc); |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 811 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 813 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 815 | if (entry->buf_count) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 816 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 817 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | return -ENOMEM; /* May only call once for each order */ |
| 819 | } |
| 820 | |
| 821 | if (count < 0 || count > 4096) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 822 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 823 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | return -EINVAL; |
| 825 | } |
| 826 | |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 827 | entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 828 | if (!entry->buflist) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 829 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 830 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | return -ENOMEM; |
| 832 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 834 | entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 835 | if (!entry->seglist) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 836 | kfree(entry->buflist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 837 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 838 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return -ENOMEM; |
| 840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
| 842 | /* Keep the original pagelist until we know all the allocations |
| 843 | * have succeeded |
| 844 | */ |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 845 | temp_pagelist = kmalloc((dma->page_count + (count << page_order)) * |
| 846 | sizeof(*dma->pagelist), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | if (!temp_pagelist) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 848 | kfree(entry->buflist); |
| 849 | kfree(entry->seglist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 850 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 851 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return -ENOMEM; |
| 853 | } |
| 854 | memcpy(temp_pagelist, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 855 | dma->pagelist, dma->page_count * sizeof(*dma->pagelist)); |
| 856 | DRM_DEBUG("pagelist: %d entries\n", |
| 857 | dma->page_count + (count << page_order)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 859 | entry->buf_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | entry->page_order = page_order; |
| 861 | byte_count = 0; |
| 862 | page_count = 0; |
| 863 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 864 | while (entry->buf_count < count) { |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 865 | |
Zhenyu Wang | e6be8d9 | 2010-01-05 11:25:05 +0800 | [diff] [blame] | 866 | dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000); |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 867 | |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 868 | if (!dmah) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | /* Set count correctly so we free the proper amount. */ |
| 870 | entry->buf_count = count; |
| 871 | entry->seg_count = count; |
| 872 | drm_cleanup_buf_error(dev, entry); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 873 | kfree(temp_pagelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 874 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 875 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | return -ENOMEM; |
| 877 | } |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 878 | entry->seglist[entry->seg_count++] = dmah; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 879 | for (i = 0; i < (1 << page_order); i++) { |
| 880 | DRM_DEBUG("page %d @ 0x%08lx\n", |
| 881 | dma->page_count + page_count, |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 882 | (unsigned long)dmah->vaddr + PAGE_SIZE * i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | temp_pagelist[dma->page_count + page_count++] |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 884 | = (unsigned long)dmah->vaddr + PAGE_SIZE * i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 886 | for (offset = 0; |
| 887 | offset + size <= total && entry->buf_count < count; |
| 888 | offset += alignment, ++entry->buf_count) { |
| 889 | buf = &entry->buflist[entry->buf_count]; |
| 890 | buf->idx = dma->buf_count + entry->buf_count; |
| 891 | buf->total = alignment; |
| 892 | buf->order = order; |
| 893 | buf->used = 0; |
| 894 | buf->offset = (dma->byte_count + byte_count + offset); |
Dave Airlie | ddf19b9 | 2006-03-19 18:56:12 +1100 | [diff] [blame] | 895 | buf->address = (void *)(dmah->vaddr + offset); |
| 896 | buf->bus_address = dmah->busaddr + offset; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 897 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | buf->waiting = 0; |
| 899 | buf->pending = 0; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 900 | buf->file_priv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
| 902 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 903 | buf->dev_private = kzalloc(buf->dev_priv_size, |
| 904 | GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 905 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | /* Set count correctly so we free the proper amount. */ |
| 907 | entry->buf_count = count; |
| 908 | entry->seg_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 909 | drm_cleanup_buf_error(dev, entry); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 910 | kfree(temp_pagelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 911 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 912 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | return -ENOMEM; |
| 914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 916 | DRM_DEBUG("buffer %d @ %p\n", |
| 917 | entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
| 919 | byte_count += PAGE_SIZE << page_order; |
| 920 | } |
| 921 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 922 | temp_buflist = krealloc(dma->buflist, |
| 923 | (dma->buf_count + entry->buf_count) * |
| 924 | sizeof(*dma->buflist), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (!temp_buflist) { |
| 926 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 927 | drm_cleanup_buf_error(dev, entry); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 928 | kfree(temp_pagelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 929 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 930 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | return -ENOMEM; |
| 932 | } |
| 933 | dma->buflist = temp_buflist; |
| 934 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 935 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 937 | } |
| 938 | |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 939 | /* No allocations failed, so now we can replace the original pagelist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * with the new one. |
| 941 | */ |
| 942 | if (dma->page_count) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 943 | kfree(dma->pagelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
| 945 | dma->pagelist = temp_pagelist; |
| 946 | |
| 947 | dma->buf_count += entry->buf_count; |
| 948 | dma->seg_count += entry->seg_count; |
| 949 | dma->page_count += entry->seg_count << page_order; |
| 950 | dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order); |
| 951 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 952 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 954 | request->count = entry->buf_count; |
| 955 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
George Sapountzis | 3417f33 | 2006-10-24 12:03:04 -0700 | [diff] [blame] | 957 | if (request->flags & _DRM_PCI_BUFFER_RO) |
| 958 | dma->flags = _DRM_DMA_USE_PCI_RO; |
| 959 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 960 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return 0; |
| 962 | |
| 963 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 964 | EXPORT_SYMBOL(drm_legacy_addbufs_pci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 966 | static int drm_legacy_addbufs_sg(struct drm_device *dev, |
| 967 | struct drm_buf_desc *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 969 | struct drm_device_dma *dma = dev->dma; |
| 970 | struct drm_buf_entry *entry; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 971 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | unsigned long offset; |
| 973 | unsigned long agp_offset; |
| 974 | int count; |
| 975 | int order; |
| 976 | int size; |
| 977 | int alignment; |
| 978 | int page_order; |
| 979 | int total; |
| 980 | int byte_count; |
| 981 | int i; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 982 | struct drm_buf **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 984 | if (!drm_core_check_feature(dev, DRIVER_SG)) |
| 985 | return -EINVAL; |
| 986 | |
| 987 | if (!dma) |
| 988 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 990 | if (!capable(CAP_SYS_ADMIN)) |
| 991 | return -EPERM; |
| 992 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 993 | count = request->count; |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 994 | order = order_base_2(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | size = 1 << order; |
| 996 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 997 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
| 998 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 1000 | total = PAGE_SIZE << page_order; |
| 1001 | |
| 1002 | byte_count = 0; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1003 | agp_offset = request->agp_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1005 | DRM_DEBUG("count: %d\n", count); |
| 1006 | DRM_DEBUG("order: %d\n", order); |
| 1007 | DRM_DEBUG("size: %d\n", size); |
| 1008 | DRM_DEBUG("agp_offset: %lu\n", agp_offset); |
| 1009 | DRM_DEBUG("alignment: %d\n", alignment); |
| 1010 | DRM_DEBUG("page_order: %d\n", page_order); |
| 1011 | DRM_DEBUG("total: %d\n", total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1013 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 1014 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1016 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1017 | if (dev->buf_use) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1018 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | return -EBUSY; |
| 1020 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1021 | atomic_inc(&dev->buf_alloc); |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1022 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1024 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1026 | if (entry->buf_count) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1027 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1028 | atomic_dec(&dev->buf_alloc); |
| 1029 | return -ENOMEM; /* May only call once for each order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | if (count < 0 || count > 4096) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1033 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1034 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | return -EINVAL; |
| 1036 | } |
| 1037 | |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 1038 | entry->buflist = kzalloc(count * sizeof(*entry->buflist), |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1039 | GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1040 | if (!entry->buflist) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1041 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1042 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | return -ENOMEM; |
| 1044 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
| 1046 | entry->buf_size = size; |
| 1047 | entry->page_order = page_order; |
| 1048 | |
| 1049 | offset = 0; |
| 1050 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1051 | while (entry->buf_count < count) { |
| 1052 | buf = &entry->buflist[entry->buf_count]; |
| 1053 | buf->idx = dma->buf_count + entry->buf_count; |
| 1054 | buf->total = alignment; |
| 1055 | buf->order = order; |
| 1056 | buf->used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1058 | buf->offset = (dma->byte_count + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | buf->bus_address = agp_offset + offset; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1060 | buf->address = (void *)(agp_offset + offset |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 1061 | + (unsigned long)dev->sg->virtual); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1062 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | buf->waiting = 0; |
| 1064 | buf->pending = 0; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1065 | buf->file_priv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Davidlohr Bueso | 94e3370 | 2010-08-11 09:18:52 -0400 | [diff] [blame] | 1068 | buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1069 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | /* Set count correctly so we free the proper amount. */ |
| 1071 | entry->buf_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1072 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1073 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1074 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | return -ENOMEM; |
| 1076 | } |
| 1077 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1078 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
| 1080 | offset += alignment; |
| 1081 | entry->buf_count++; |
| 1082 | byte_count += PAGE_SIZE << page_order; |
| 1083 | } |
| 1084 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1085 | DRM_DEBUG("byte_count: %d\n", byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1087 | temp_buflist = krealloc(dma->buflist, |
| 1088 | (dma->buf_count + entry->buf_count) * |
| 1089 | sizeof(*dma->buflist), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1090 | if (!temp_buflist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1092 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1093 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1094 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | return -ENOMEM; |
| 1096 | } |
| 1097 | dma->buflist = temp_buflist; |
| 1098 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1099 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 1101 | } |
| 1102 | |
| 1103 | dma->buf_count += entry->buf_count; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 1104 | dma->seg_count += entry->seg_count; |
| 1105 | dma->page_count += byte_count >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | dma->byte_count += byte_count; |
| 1107 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1108 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
| 1109 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 1111 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1113 | request->count = entry->buf_count; |
| 1114 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | dma->flags = _DRM_DMA_USE_SG; |
| 1117 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1118 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * Add buffers for DMA transfers (ioctl). |
| 1124 | * |
| 1125 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1126 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | * \param cmd command. |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 1128 | * \param arg pointer to a struct drm_buf_desc request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | * \return zero on success or a negative number on failure. |
| 1130 | * |
| 1131 | * According with the memory type specified in drm_buf_desc::flags and the |
| 1132 | * build options, it dispatches the call either to addbufs_agp(), |
| 1133 | * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent |
| 1134 | * PCI memory respectively. |
| 1135 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1136 | int drm_legacy_addbufs(struct drm_device *dev, void *data, |
| 1137 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1139 | struct drm_buf_desc *request = data; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1140 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1141 | |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1142 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1143 | return -EINVAL; |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1146 | return -EINVAL; |
| 1147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | #if __OS_HAS_AGP |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1149 | if (request->flags & _DRM_AGP_BUFFER) |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1150 | ret = drm_legacy_addbufs_agp(dev, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | else |
| 1152 | #endif |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1153 | if (request->flags & _DRM_SG_BUFFER) |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1154 | ret = drm_legacy_addbufs_sg(dev, request); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1155 | else if (request->flags & _DRM_FB_BUFFER) |
Daniel Vetter | 687fbb2 | 2013-08-08 15:41:24 +0200 | [diff] [blame] | 1156 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | else |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1158 | ret = drm_legacy_addbufs_pci(dev, request); |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1159 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1160 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | /** |
| 1164 | * Get information about the buffer mappings. |
| 1165 | * |
| 1166 | * This was originally mean for debugging purposes, or by a sophisticated |
| 1167 | * client library to determine how best to use the available buffers (e.g., |
| 1168 | * large buffers can be used for image transfer). |
| 1169 | * |
| 1170 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1171 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | * \param cmd command. |
| 1173 | * \param arg pointer to a drm_buf_info structure. |
| 1174 | * \return zero on success or a negative number on failure. |
| 1175 | * |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1176 | * Increments drm_device::buf_use while holding the drm_device::buf_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | * lock, preventing of allocating more buffers after this call. Information |
| 1178 | * about each requested buffer is then copied into user space. |
| 1179 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1180 | int drm_legacy_infobufs(struct drm_device *dev, void *data, |
| 1181 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1183 | struct drm_device_dma *dma = dev->dma; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1184 | struct drm_buf_info *request = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | int i; |
| 1186 | int count; |
| 1187 | |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1188 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1189 | return -EINVAL; |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1192 | return -EINVAL; |
| 1193 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1194 | if (!dma) |
| 1195 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1197 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1198 | if (atomic_read(&dev->buf_alloc)) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1199 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | return -EBUSY; |
| 1201 | } |
| 1202 | ++dev->buf_use; /* Can't allocate more after this call */ |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1203 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1205 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
| 1206 | if (dma->bufs[i].buf_count) |
| 1207 | ++count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1210 | DRM_DEBUG("count = %d\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1212 | if (request->count >= count) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1213 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
| 1214 | if (dma->bufs[i].buf_count) { |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 1215 | struct drm_buf_desc __user *to = |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1216 | &request->list[count]; |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1217 | struct drm_buf_entry *from = &dma->bufs[i]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1218 | if (copy_to_user(&to->count, |
| 1219 | &from->buf_count, |
| 1220 | sizeof(from->buf_count)) || |
| 1221 | copy_to_user(&to->size, |
| 1222 | &from->buf_size, |
| 1223 | sizeof(from->buf_size)) || |
| 1224 | copy_to_user(&to->low_mark, |
David Herrmann | b008c0f | 2014-07-23 17:26:36 +0200 | [diff] [blame] | 1225 | &from->low_mark, |
| 1226 | sizeof(from->low_mark)) || |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1227 | copy_to_user(&to->high_mark, |
David Herrmann | b008c0f | 2014-07-23 17:26:36 +0200 | [diff] [blame] | 1228 | &from->high_mark, |
| 1229 | sizeof(from->high_mark))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | return -EFAULT; |
| 1231 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1232 | DRM_DEBUG("%d %d %d %d %d\n", |
| 1233 | i, |
| 1234 | dma->bufs[i].buf_count, |
| 1235 | dma->bufs[i].buf_size, |
David Herrmann | b008c0f | 2014-07-23 17:26:36 +0200 | [diff] [blame] | 1236 | dma->bufs[i].low_mark, |
| 1237 | dma->bufs[i].high_mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | ++count; |
| 1239 | } |
| 1240 | } |
| 1241 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1242 | request->count = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * Specifies a low and high water mark for buffer allocation |
| 1249 | * |
| 1250 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1251 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | * \param cmd command. |
| 1253 | * \param arg a pointer to a drm_buf_desc structure. |
| 1254 | * \return zero on success or a negative number on failure. |
| 1255 | * |
| 1256 | * Verifies that the size order is bounded between the admissible orders and |
| 1257 | * updates the respective drm_device_dma::bufs entry low and high water mark. |
| 1258 | * |
| 1259 | * \note This ioctl is deprecated and mostly never used. |
| 1260 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1261 | int drm_legacy_markbufs(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_desc *request = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | int order; |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1267 | struct drm_buf_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1278 | DRM_DEBUG("%d, %d, %d\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1279 | request->size, request->low_mark, request->high_mark); |
Daniel Vetter | 04420c9 | 2013-07-10 14:11:57 +0200 | [diff] [blame] | 1280 | order = order_base_2(request->size); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1281 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 1282 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | entry = &dma->bufs[order]; |
| 1284 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1285 | if (request->low_mark < 0 || request->low_mark > entry->buf_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | return -EINVAL; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1287 | if (request->high_mark < 0 || request->high_mark > entry->buf_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | return -EINVAL; |
| 1289 | |
David Herrmann | b008c0f | 2014-07-23 17:26:36 +0200 | [diff] [blame] | 1290 | entry->low_mark = request->low_mark; |
| 1291 | entry->high_mark = request->high_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1297 | * Unreserve the buffers in list, previously reserved using drmDMA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | * |
| 1299 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1300 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | * \param cmd command. |
| 1302 | * \param arg pointer to a drm_buf_free structure. |
| 1303 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1304 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | * Calls free_buffer() for each used buffer. |
| 1306 | * This function is primarily used for debugging. |
| 1307 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1308 | int drm_legacy_freebufs(struct drm_device *dev, void *data, |
| 1309 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1311 | struct drm_device_dma *dma = dev->dma; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1312 | struct drm_buf_free *request = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | int i; |
| 1314 | int idx; |
Dave Airlie | 056219e | 2007-07-11 16:17:42 +1000 | [diff] [blame] | 1315 | struct drm_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1317 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1318 | return -EINVAL; |
| 1319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1321 | return -EINVAL; |
| 1322 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1323 | if (!dma) |
| 1324 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1326 | DRM_DEBUG("%d\n", request->count); |
| 1327 | for (i = 0; i < request->count; i++) { |
| 1328 | if (copy_from_user(&idx, &request->list[i], sizeof(idx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | return -EFAULT; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1330 | if (idx < 0 || idx >= dma->buf_count) { |
| 1331 | DRM_ERROR("Index %d (of %d max)\n", |
| 1332 | idx, dma->buf_count - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | return -EINVAL; |
| 1334 | } |
| 1335 | buf = dma->buflist[idx]; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1336 | if (buf->file_priv != file_priv) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1337 | DRM_ERROR("Process %d freeing buffer not owned\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1338 | task_pid_nr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | return -EINVAL; |
| 1340 | } |
Daniel Vetter | a266162 | 2014-09-11 07:41:51 +0200 | [diff] [blame] | 1341 | drm_legacy_free_buffer(dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | /** |
| 1348 | * Maps all of the DMA buffers into client-virtual space (ioctl). |
| 1349 | * |
| 1350 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1351 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | * \param cmd command. |
| 1353 | * \param arg pointer to a drm_buf_map structure. |
| 1354 | * \return zero on success or a negative number on failure. |
| 1355 | * |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1356 | * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information |
| 1357 | * 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] | 1358 | * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls |
| 1359 | * drm_mmap_dma(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | */ |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1361 | int drm_legacy_mapbufs(struct drm_device *dev, void *data, |
| 1362 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
Dave Airlie | cdd55a2 | 2007-07-11 16:32:08 +1000 | [diff] [blame] | 1364 | struct drm_device_dma *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | int retcode = 0; |
| 1366 | const int zero = 0; |
| 1367 | unsigned long virtual; |
| 1368 | unsigned long address; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1369 | struct drm_buf_map *request = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | int i; |
| 1371 | |
Daniel Vetter | 8d38c4b | 2013-08-08 15:41:20 +0200 | [diff] [blame] | 1372 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1373 | return -EINVAL; |
| 1374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1376 | return -EINVAL; |
| 1377 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1378 | if (!dma) |
| 1379 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1381 | spin_lock(&dev->buf_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1382 | if (atomic_read(&dev->buf_alloc)) { |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1383 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | return -EBUSY; |
| 1385 | } |
| 1386 | dev->buf_use++; /* Can't allocate more after this call */ |
Daniel Vetter | 2177a21 | 2013-12-16 11:21:06 +0100 | [diff] [blame] | 1387 | spin_unlock(&dev->buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1389 | if (request->count >= dma->buf_count) { |
Daniel Vetter | d990675 | 2013-12-11 11:34:35 +0100 | [diff] [blame] | 1390 | if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP)) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1391 | || (drm_core_check_feature(dev, DRIVER_SG) |
Daniel Vetter | 687fbb2 | 2013-08-08 15:41:24 +0200 | [diff] [blame] | 1392 | && (dma->flags & _DRM_DMA_USE_SG))) { |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 1393 | struct drm_local_map *map = dev->agp_buffer_map; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 1394 | unsigned long token = dev->agp_buffer_token; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1396 | if (!map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | retcode = -EINVAL; |
| 1398 | goto done; |
| 1399 | } |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1400 | virtual = vm_mmap(file_priv->filp, 0, map->size, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1401 | PROT_READ | PROT_WRITE, |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1402 | MAP_SHARED, |
| 1403 | token); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } else { |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1405 | virtual = vm_mmap(file_priv->filp, 0, dma->byte_count, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1406 | PROT_READ | PROT_WRITE, |
| 1407 | MAP_SHARED, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1409 | if (virtual > -1024UL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | /* Real error */ |
| 1411 | retcode = (signed long)virtual; |
| 1412 | goto done; |
| 1413 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1414 | request->virtual = (void __user *)virtual; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1416 | for (i = 0; i < dma->buf_count; i++) { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1417 | if (copy_to_user(&request->list[i].idx, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1418 | &dma->buflist[i]->idx, |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1419 | sizeof(request->list[0].idx))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | retcode = -EFAULT; |
| 1421 | goto done; |
| 1422 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1423 | if (copy_to_user(&request->list[i].total, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1424 | &dma->buflist[i]->total, |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1425 | sizeof(request->list[0].total))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | retcode = -EFAULT; |
| 1427 | goto done; |
| 1428 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1429 | if (copy_to_user(&request->list[i].used, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1430 | &zero, sizeof(zero))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | retcode = -EFAULT; |
| 1432 | goto done; |
| 1433 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1434 | address = virtual + dma->buflist[i]->offset; /* *** */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1435 | if (copy_to_user(&request->list[i].address, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1436 | &address, sizeof(address))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | retcode = -EFAULT; |
| 1438 | goto done; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1442 | done: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1443 | request->count = dma->buf_count; |
| 1444 | DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | return retcode; |
| 1447 | } |
| 1448 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1449 | int drm_legacy_dma_ioctl(struct drm_device *dev, void *data, |
Daniel Vetter | 6eb9278 | 2013-08-08 15:41:29 +0200 | [diff] [blame] | 1450 | struct drm_file *file_priv) |
| 1451 | { |
| 1452 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1453 | return -EINVAL; |
| 1454 | |
| 1455 | if (dev->driver->dma_ioctl) |
| 1456 | return dev->driver->dma_ioctl(dev, data, file_priv); |
| 1457 | else |
| 1458 | return -EINVAL; |
| 1459 | } |
| 1460 | |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1461 | struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev) |
Daniel Vetter | bd0c0ce | 2013-07-10 14:11:56 +0200 | [diff] [blame] | 1462 | { |
| 1463 | struct drm_map_list *entry; |
| 1464 | |
| 1465 | list_for_each_entry(entry, &dev->maplist, head) { |
| 1466 | if (entry->map && entry->map->type == _DRM_SHM && |
| 1467 | (entry->map->flags & _DRM_CONTAINS_LOCK)) { |
| 1468 | return entry->map; |
| 1469 | } |
| 1470 | } |
| 1471 | return NULL; |
| 1472 | } |
David Herrmann | 9fc5cde | 2014-08-29 12:12:28 +0200 | [diff] [blame] | 1473 | EXPORT_SYMBOL(drm_legacy_getsarea); |