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