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