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