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