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