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