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