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 | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 157 | if (map->offset + map->size < map->offset || |
| 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 | |
| 258 | down(&dev->struct_sem); |
| 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 | 9a18664 | 2005-06-23 21:29:18 +1000 | [diff] [blame] | 261 | /* We do it here so that dev->struct_sem 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 | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 265 | up(&dev->struct_sem); |
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 | EXPORT_SYMBOL(drm_rmmap_locked); |
| 390 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 391 | int drm_rmmap(drm_device_t *dev, drm_local_map_t *map) |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 392 | { |
| 393 | int ret; |
| 394 | |
| 395 | down(&dev->struct_sem); |
| 396 | ret = drm_rmmap_locked(dev, map); |
| 397 | up(&dev->struct_sem); |
| 398 | |
| 399 | return ret; |
| 400 | } |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 401 | EXPORT_SYMBOL(drm_rmmap); |
| 402 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 403 | /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on |
| 404 | * the last close of the device, and this is necessary for cleanup when things |
| 405 | * exit uncleanly. Therefore, having userland manually remove mappings seems |
| 406 | * like a pointless exercise since they're going away anyway. |
| 407 | * |
| 408 | * One use case might be after addmap is allowed for normal users for SHM and |
| 409 | * gets used by drivers that the server doesn't need to care about. This seems |
| 410 | * unlikely. |
| 411 | */ |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 412 | int drm_rmmap_ioctl(struct inode *inode, struct file *filp, |
| 413 | unsigned int cmd, unsigned long arg) |
| 414 | { |
| 415 | drm_file_t *priv = filp->private_data; |
| 416 | drm_device_t *dev = priv->head->dev; |
| 417 | drm_map_t request; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 418 | drm_local_map_t *map = NULL; |
| 419 | struct list_head *list; |
| 420 | int ret; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 421 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 422 | if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) { |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 423 | return -EFAULT; |
| 424 | } |
| 425 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 426 | down(&dev->struct_sem); |
| 427 | list_for_each(list, &dev->maplist->head) { |
| 428 | drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head); |
| 429 | |
| 430 | if (r_list->map && |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 431 | r_list->user_token == (unsigned long)request.handle && |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 432 | r_list->map->flags & _DRM_REMOVABLE) { |
| 433 | map = r_list->map; |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* List has wrapped around to the head pointer, or its empty we didn't |
| 439 | * find anything. |
| 440 | */ |
| 441 | if (list == (&dev->maplist->head)) { |
| 442 | up(&dev->struct_sem); |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
| 446 | if (!map) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | /* Register and framebuffer maps are permanent */ |
| 450 | if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) { |
| 451 | up(&dev->struct_sem); |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | ret = drm_rmmap_locked(dev, map); |
| 456 | |
| 457 | up(&dev->struct_sem); |
| 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]) { |
| 477 | drm_free_pages(entry->seglist[i], |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 478 | entry->page_order, DRM_MEM_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | drm_free(entry->seglist, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 482 | entry->seg_count * |
| 483 | sizeof(*entry->seglist), DRM_MEM_SEGS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | entry->seg_count = 0; |
| 486 | } |
| 487 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 488 | if (entry->buf_count) { |
| 489 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if (entry->buflist[i].dev_private) { |
| 491 | drm_free(entry->buflist[i].dev_private, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 492 | entry->buflist[i].dev_priv_size, |
| 493 | DRM_MEM_BUFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | drm_free(entry->buflist, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 497 | entry->buf_count * |
| 498 | sizeof(*entry->buflist), DRM_MEM_BUFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | entry->buf_count = 0; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | #if __OS_HAS_AGP |
| 505 | /** |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 506 | * Add AGP buffers for DMA transfers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | * |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 508 | * \param dev drm_device_t to which the buffers are to be added. |
| 509 | * \param request pointer to a drm_buf_desc_t describing the request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 511 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | * After some sanity checks creates a drm_buf structure for each buffer and |
| 513 | * reallocates the buffer list of the same size order to accommodate the new |
| 514 | * buffers. |
| 515 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 516 | 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] | 517 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | drm_device_dma_t *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | drm_buf_entry_t *entry; |
| 520 | drm_buf_t *buf; |
| 521 | unsigned long offset; |
| 522 | unsigned long agp_offset; |
| 523 | int count; |
| 524 | int order; |
| 525 | int size; |
| 526 | int alignment; |
| 527 | int page_order; |
| 528 | int total; |
| 529 | int byte_count; |
| 530 | int i; |
| 531 | drm_buf_t **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 533 | if (!dma) |
| 534 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 536 | count = request->count; |
| 537 | order = drm_order(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | size = 1 << order; |
| 539 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 540 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
| 541 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 543 | total = PAGE_SIZE << page_order; |
| 544 | |
| 545 | byte_count = 0; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 546 | agp_offset = dev->agp->base + request->agp_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 548 | DRM_DEBUG("count: %d\n", count); |
| 549 | DRM_DEBUG("order: %d\n", order); |
| 550 | DRM_DEBUG("size: %d\n", size); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 551 | DRM_DEBUG("agp_offset: %lx\n", agp_offset); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 552 | DRM_DEBUG("alignment: %d\n", alignment); |
| 553 | DRM_DEBUG("page_order: %d\n", page_order); |
| 554 | DRM_DEBUG("total: %d\n", total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 556 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 557 | return -EINVAL; |
| 558 | if (dev->queue_count) |
| 559 | return -EBUSY; /* Not while in use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 561 | spin_lock(&dev->count_lock); |
| 562 | if (dev->buf_use) { |
| 563 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return -EBUSY; |
| 565 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 566 | atomic_inc(&dev->buf_alloc); |
| 567 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 569 | down(&dev->struct_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 571 | if (entry->buf_count) { |
| 572 | up(&dev->struct_sem); |
| 573 | atomic_dec(&dev->buf_alloc); |
| 574 | return -ENOMEM; /* May only call once for each order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | if (count < 0 || count > 4096) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 578 | up(&dev->struct_sem); |
| 579 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | return -EINVAL; |
| 581 | } |
| 582 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 583 | entry->buflist = drm_alloc(count * sizeof(*entry->buflist), |
| 584 | DRM_MEM_BUFS); |
| 585 | if (!entry->buflist) { |
| 586 | up(&dev->struct_sem); |
| 587 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | return -ENOMEM; |
| 589 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 590 | memset(entry->buflist, 0, count * sizeof(*entry->buflist)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | entry->buf_size = size; |
| 593 | entry->page_order = page_order; |
| 594 | |
| 595 | offset = 0; |
| 596 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 597 | while (entry->buf_count < count) { |
| 598 | buf = &entry->buflist[entry->buf_count]; |
| 599 | buf->idx = dma->buf_count + entry->buf_count; |
| 600 | buf->total = alignment; |
| 601 | buf->order = order; |
| 602 | buf->used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 604 | buf->offset = (dma->byte_count + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | buf->bus_address = agp_offset + offset; |
| 606 | buf->address = (void *)(agp_offset + offset); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 607 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | buf->waiting = 0; |
| 609 | buf->pending = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 610 | init_waitqueue_head(&buf->dma_wait); |
| 611 | buf->filp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 614 | buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS); |
| 615 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | /* Set count correctly so we free the proper amount. */ |
| 617 | entry->buf_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 618 | drm_cleanup_buf_error(dev, entry); |
| 619 | up(&dev->struct_sem); |
| 620 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | return -ENOMEM; |
| 622 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 623 | memset(buf->dev_private, 0, buf->dev_priv_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 625 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
| 627 | offset += alignment; |
| 628 | entry->buf_count++; |
| 629 | byte_count += PAGE_SIZE << page_order; |
| 630 | } |
| 631 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 632 | DRM_DEBUG("byte_count: %d\n", byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 634 | temp_buflist = drm_realloc(dma->buflist, |
| 635 | dma->buf_count * sizeof(*dma->buflist), |
| 636 | (dma->buf_count + entry->buf_count) |
| 637 | * sizeof(*dma->buflist), DRM_MEM_BUFS); |
| 638 | if (!temp_buflist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 640 | drm_cleanup_buf_error(dev, entry); |
| 641 | up(&dev->struct_sem); |
| 642 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | return -ENOMEM; |
| 644 | } |
| 645 | dma->buflist = temp_buflist; |
| 646 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 647 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 649 | } |
| 650 | |
| 651 | dma->buf_count += entry->buf_count; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 652 | dma->seg_count += entry->seg_count; |
| 653 | dma->page_count += byte_count >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | dma->byte_count += byte_count; |
| 655 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 656 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
| 657 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 659 | up(&dev->struct_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 661 | request->count = entry->buf_count; |
| 662 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | dma->flags = _DRM_DMA_USE_AGP; |
| 665 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 666 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | return 0; |
| 668 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 669 | EXPORT_SYMBOL(drm_addbufs_agp); |
| 670 | #endif /* __OS_HAS_AGP */ |
| 671 | |
| 672 | 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] | 673 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | drm_device_dma_t *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | int count; |
| 676 | int order; |
| 677 | int size; |
| 678 | int total; |
| 679 | int page_order; |
| 680 | drm_buf_entry_t *entry; |
| 681 | unsigned long page; |
| 682 | drm_buf_t *buf; |
| 683 | int alignment; |
| 684 | unsigned long offset; |
| 685 | int i; |
| 686 | int byte_count; |
| 687 | int page_count; |
| 688 | unsigned long *temp_pagelist; |
| 689 | drm_buf_t **temp_buflist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 691 | if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) |
| 692 | return -EINVAL; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 693 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 694 | if (!dma) |
| 695 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 697 | if (!capable(CAP_SYS_ADMIN)) |
| 698 | return -EPERM; |
| 699 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 700 | count = request->count; |
| 701 | order = drm_order(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | size = 1 << order; |
| 703 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 704 | DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n", |
| 705 | request->count, request->size, size, order, dev->queue_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 707 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 708 | return -EINVAL; |
| 709 | if (dev->queue_count) |
| 710 | return -EBUSY; /* Not while in use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 712 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 713 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 715 | total = PAGE_SIZE << page_order; |
| 716 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 717 | spin_lock(&dev->count_lock); |
| 718 | if (dev->buf_use) { |
| 719 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | return -EBUSY; |
| 721 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 722 | atomic_inc(&dev->buf_alloc); |
| 723 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 725 | down(&dev->struct_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 727 | if (entry->buf_count) { |
| 728 | up(&dev->struct_sem); |
| 729 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | return -ENOMEM; /* May only call once for each order */ |
| 731 | } |
| 732 | |
| 733 | if (count < 0 || count > 4096) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 734 | up(&dev->struct_sem); |
| 735 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | return -EINVAL; |
| 737 | } |
| 738 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 739 | entry->buflist = drm_alloc(count * sizeof(*entry->buflist), |
| 740 | DRM_MEM_BUFS); |
| 741 | if (!entry->buflist) { |
| 742 | up(&dev->struct_sem); |
| 743 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return -ENOMEM; |
| 745 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 746 | memset(entry->buflist, 0, count * sizeof(*entry->buflist)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 748 | entry->seglist = drm_alloc(count * sizeof(*entry->seglist), |
| 749 | DRM_MEM_SEGS); |
| 750 | if (!entry->seglist) { |
| 751 | drm_free(entry->buflist, |
| 752 | count * sizeof(*entry->buflist), DRM_MEM_BUFS); |
| 753 | up(&dev->struct_sem); |
| 754 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | return -ENOMEM; |
| 756 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 757 | memset(entry->seglist, 0, count * sizeof(*entry->seglist)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | /* Keep the original pagelist until we know all the allocations |
| 760 | * have succeeded |
| 761 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 762 | temp_pagelist = drm_alloc((dma->page_count + (count << page_order)) |
| 763 | * sizeof(*dma->pagelist), DRM_MEM_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | if (!temp_pagelist) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 765 | drm_free(entry->buflist, |
| 766 | count * sizeof(*entry->buflist), DRM_MEM_BUFS); |
| 767 | drm_free(entry->seglist, |
| 768 | count * sizeof(*entry->seglist), DRM_MEM_SEGS); |
| 769 | up(&dev->struct_sem); |
| 770 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return -ENOMEM; |
| 772 | } |
| 773 | memcpy(temp_pagelist, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 774 | dma->pagelist, dma->page_count * sizeof(*dma->pagelist)); |
| 775 | DRM_DEBUG("pagelist: %d entries\n", |
| 776 | dma->page_count + (count << page_order)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 778 | entry->buf_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | entry->page_order = page_order; |
| 780 | byte_count = 0; |
| 781 | page_count = 0; |
| 782 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 783 | while (entry->buf_count < count) { |
| 784 | page = drm_alloc_pages(page_order, DRM_MEM_DMA); |
| 785 | if (!page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | /* Set count correctly so we free the proper amount. */ |
| 787 | entry->buf_count = count; |
| 788 | entry->seg_count = count; |
| 789 | drm_cleanup_buf_error(dev, entry); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 790 | drm_free(temp_pagelist, |
| 791 | (dma->page_count + (count << page_order)) |
| 792 | * sizeof(*dma->pagelist), DRM_MEM_PAGES); |
| 793 | up(&dev->struct_sem); |
| 794 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return -ENOMEM; |
| 796 | } |
| 797 | entry->seglist[entry->seg_count++] = page; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 798 | for (i = 0; i < (1 << page_order); i++) { |
| 799 | DRM_DEBUG("page %d @ 0x%08lx\n", |
| 800 | dma->page_count + page_count, |
| 801 | page + PAGE_SIZE * i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | temp_pagelist[dma->page_count + page_count++] |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 803 | = page + PAGE_SIZE * i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 805 | for (offset = 0; |
| 806 | offset + size <= total && entry->buf_count < count; |
| 807 | offset += alignment, ++entry->buf_count) { |
| 808 | buf = &entry->buflist[entry->buf_count]; |
| 809 | buf->idx = dma->buf_count + entry->buf_count; |
| 810 | buf->total = alignment; |
| 811 | buf->order = order; |
| 812 | buf->used = 0; |
| 813 | buf->offset = (dma->byte_count + byte_count + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | buf->address = (void *)(page + offset); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 815 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | buf->waiting = 0; |
| 817 | buf->pending = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 818 | init_waitqueue_head(&buf->dma_wait); |
| 819 | buf->filp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 822 | buf->dev_private = drm_alloc(buf->dev_priv_size, |
| 823 | DRM_MEM_BUFS); |
| 824 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | /* Set count correctly so we free the proper amount. */ |
| 826 | entry->buf_count = count; |
| 827 | entry->seg_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 828 | drm_cleanup_buf_error(dev, entry); |
| 829 | drm_free(temp_pagelist, |
| 830 | (dma->page_count + |
| 831 | (count << page_order)) |
| 832 | * sizeof(*dma->pagelist), |
| 833 | DRM_MEM_PAGES); |
| 834 | up(&dev->struct_sem); |
| 835 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | return -ENOMEM; |
| 837 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 838 | memset(buf->dev_private, 0, buf->dev_priv_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 840 | DRM_DEBUG("buffer %d @ %p\n", |
| 841 | entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | byte_count += PAGE_SIZE << page_order; |
| 844 | } |
| 845 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 846 | temp_buflist = drm_realloc(dma->buflist, |
| 847 | dma->buf_count * sizeof(*dma->buflist), |
| 848 | (dma->buf_count + entry->buf_count) |
| 849 | * sizeof(*dma->buflist), DRM_MEM_BUFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | if (!temp_buflist) { |
| 851 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 852 | drm_cleanup_buf_error(dev, entry); |
| 853 | drm_free(temp_pagelist, |
| 854 | (dma->page_count + (count << page_order)) |
| 855 | * sizeof(*dma->pagelist), DRM_MEM_PAGES); |
| 856 | up(&dev->struct_sem); |
| 857 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | return -ENOMEM; |
| 859 | } |
| 860 | dma->buflist = temp_buflist; |
| 861 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 862 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 864 | } |
| 865 | |
| 866 | /* No allocations failed, so now we can replace the orginal pagelist |
| 867 | * with the new one. |
| 868 | */ |
| 869 | if (dma->page_count) { |
| 870 | drm_free(dma->pagelist, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 871 | dma->page_count * sizeof(*dma->pagelist), |
| 872 | DRM_MEM_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | dma->pagelist = temp_pagelist; |
| 875 | |
| 876 | dma->buf_count += entry->buf_count; |
| 877 | dma->seg_count += entry->seg_count; |
| 878 | dma->page_count += entry->seg_count << page_order; |
| 879 | dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order); |
| 880 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 881 | up(&dev->struct_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 883 | request->count = entry->buf_count; |
| 884 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 886 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return 0; |
| 888 | |
| 889 | } |
Dave Airlie | d84f76d | 2005-07-10 17:04:22 +1000 | [diff] [blame] | 890 | EXPORT_SYMBOL(drm_addbufs_pci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 892 | 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] | 893 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | drm_device_dma_t *dma = dev->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | drm_buf_entry_t *entry; |
| 896 | drm_buf_t *buf; |
| 897 | unsigned long offset; |
| 898 | unsigned long agp_offset; |
| 899 | int count; |
| 900 | int order; |
| 901 | int size; |
| 902 | int alignment; |
| 903 | int page_order; |
| 904 | int total; |
| 905 | int byte_count; |
| 906 | int i; |
| 907 | drm_buf_t **temp_buflist; |
| 908 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 909 | if (!drm_core_check_feature(dev, DRIVER_SG)) |
| 910 | return -EINVAL; |
| 911 | |
| 912 | if (!dma) |
| 913 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 915 | if (!capable(CAP_SYS_ADMIN)) |
| 916 | return -EPERM; |
| 917 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 918 | count = request->count; |
| 919 | order = drm_order(request->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | size = 1 << order; |
| 921 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 922 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
| 923 | ? PAGE_ALIGN(size) : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 925 | total = PAGE_SIZE << page_order; |
| 926 | |
| 927 | byte_count = 0; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 928 | agp_offset = request->agp_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 930 | DRM_DEBUG("count: %d\n", count); |
| 931 | DRM_DEBUG("order: %d\n", order); |
| 932 | DRM_DEBUG("size: %d\n", size); |
| 933 | DRM_DEBUG("agp_offset: %lu\n", agp_offset); |
| 934 | DRM_DEBUG("alignment: %d\n", alignment); |
| 935 | DRM_DEBUG("page_order: %d\n", page_order); |
| 936 | DRM_DEBUG("total: %d\n", total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 938 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 939 | return -EINVAL; |
| 940 | if (dev->queue_count) |
| 941 | return -EBUSY; /* Not while in use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 943 | spin_lock(&dev->count_lock); |
| 944 | if (dev->buf_use) { |
| 945 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return -EBUSY; |
| 947 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 948 | atomic_inc(&dev->buf_alloc); |
| 949 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 951 | down(&dev->struct_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | entry = &dma->bufs[order]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 953 | if (entry->buf_count) { |
| 954 | up(&dev->struct_sem); |
| 955 | atomic_dec(&dev->buf_alloc); |
| 956 | return -ENOMEM; /* May only call once for each order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | if (count < 0 || count > 4096) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 960 | up(&dev->struct_sem); |
| 961 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | return -EINVAL; |
| 963 | } |
| 964 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 965 | entry->buflist = drm_alloc(count * sizeof(*entry->buflist), |
| 966 | DRM_MEM_BUFS); |
| 967 | if (!entry->buflist) { |
| 968 | up(&dev->struct_sem); |
| 969 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | return -ENOMEM; |
| 971 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 972 | memset(entry->buflist, 0, count * sizeof(*entry->buflist)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
| 974 | entry->buf_size = size; |
| 975 | entry->page_order = page_order; |
| 976 | |
| 977 | offset = 0; |
| 978 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 979 | while (entry->buf_count < count) { |
| 980 | buf = &entry->buflist[entry->buf_count]; |
| 981 | buf->idx = dma->buf_count + entry->buf_count; |
| 982 | buf->total = alignment; |
| 983 | buf->order = order; |
| 984 | buf->used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 986 | buf->offset = (dma->byte_count + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | buf->bus_address = agp_offset + offset; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 988 | buf->address = (void *)(agp_offset + offset |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 989 | + (unsigned long)dev->sg->virtual); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 990 | buf->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | buf->waiting = 0; |
| 992 | buf->pending = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 993 | init_waitqueue_head(&buf->dma_wait); |
| 994 | buf->filp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | buf->dev_priv_size = dev->driver->dev_priv_size; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 997 | buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS); |
| 998 | if (!buf->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | /* Set count correctly so we free the proper amount. */ |
| 1000 | entry->buf_count = count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1001 | drm_cleanup_buf_error(dev, entry); |
| 1002 | up(&dev->struct_sem); |
| 1003 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | return -ENOMEM; |
| 1005 | } |
| 1006 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1007 | memset(buf->dev_private, 0, buf->dev_priv_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1009 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | offset += alignment; |
| 1012 | entry->buf_count++; |
| 1013 | byte_count += PAGE_SIZE << page_order; |
| 1014 | } |
| 1015 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1016 | DRM_DEBUG("byte_count: %d\n", byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1018 | temp_buflist = drm_realloc(dma->buflist, |
| 1019 | dma->buf_count * sizeof(*dma->buflist), |
| 1020 | (dma->buf_count + entry->buf_count) |
| 1021 | * sizeof(*dma->buflist), DRM_MEM_BUFS); |
| 1022 | if (!temp_buflist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | /* Free the entry because it isn't valid */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1024 | drm_cleanup_buf_error(dev, entry); |
| 1025 | up(&dev->struct_sem); |
| 1026 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | return -ENOMEM; |
| 1028 | } |
| 1029 | dma->buflist = temp_buflist; |
| 1030 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1031 | for (i = 0; i < entry->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 1033 | } |
| 1034 | |
| 1035 | dma->buf_count += entry->buf_count; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 1036 | dma->seg_count += entry->seg_count; |
| 1037 | dma->page_count += byte_count >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | dma->byte_count += byte_count; |
| 1039 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1040 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
| 1041 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1043 | up(&dev->struct_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1045 | request->count = entry->buf_count; |
| 1046 | request->size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | dma->flags = _DRM_DMA_USE_SG; |
| 1049 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1050 | atomic_dec(&dev->buf_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | return 0; |
| 1052 | } |
| 1053 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 1054 | 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] | 1055 | { |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1056 | drm_device_dma_t *dma = dev->dma; |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1057 | drm_buf_entry_t *entry; |
| 1058 | drm_buf_t *buf; |
| 1059 | unsigned long offset; |
| 1060 | unsigned long agp_offset; |
| 1061 | int count; |
| 1062 | int order; |
| 1063 | int size; |
| 1064 | int alignment; |
| 1065 | int page_order; |
| 1066 | int total; |
| 1067 | int byte_count; |
| 1068 | int i; |
| 1069 | drm_buf_t **temp_buflist; |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1070 | |
| 1071 | if (!drm_core_check_feature(dev, DRIVER_FB_DMA)) |
| 1072 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1073 | |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1074 | if (!dma) |
| 1075 | return -EINVAL; |
| 1076 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 1077 | if (!capable(CAP_SYS_ADMIN)) |
| 1078 | return -EPERM; |
| 1079 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1080 | count = request->count; |
| 1081 | order = drm_order(request->size); |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1082 | size = 1 << order; |
| 1083 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1084 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1085 | ? PAGE_ALIGN(size) : size; |
| 1086 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
| 1087 | total = PAGE_SIZE << page_order; |
| 1088 | |
| 1089 | byte_count = 0; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1090 | agp_offset = request->agp_start; |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1091 | |
| 1092 | DRM_DEBUG("count: %d\n", count); |
| 1093 | DRM_DEBUG("order: %d\n", order); |
| 1094 | DRM_DEBUG("size: %d\n", size); |
| 1095 | DRM_DEBUG("agp_offset: %lu\n", agp_offset); |
| 1096 | DRM_DEBUG("alignment: %d\n", alignment); |
| 1097 | DRM_DEBUG("page_order: %d\n", page_order); |
| 1098 | DRM_DEBUG("total: %d\n", total); |
| 1099 | |
| 1100 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 1101 | return -EINVAL; |
| 1102 | if (dev->queue_count) |
| 1103 | return -EBUSY; /* Not while in use */ |
| 1104 | |
| 1105 | spin_lock(&dev->count_lock); |
| 1106 | if (dev->buf_use) { |
| 1107 | spin_unlock(&dev->count_lock); |
| 1108 | return -EBUSY; |
| 1109 | } |
| 1110 | atomic_inc(&dev->buf_alloc); |
| 1111 | spin_unlock(&dev->count_lock); |
| 1112 | |
| 1113 | down(&dev->struct_sem); |
| 1114 | entry = &dma->bufs[order]; |
| 1115 | if (entry->buf_count) { |
| 1116 | up(&dev->struct_sem); |
| 1117 | atomic_dec(&dev->buf_alloc); |
| 1118 | return -ENOMEM; /* May only call once for each order */ |
| 1119 | } |
| 1120 | |
| 1121 | if (count < 0 || count > 4096) { |
| 1122 | up(&dev->struct_sem); |
| 1123 | atomic_dec(&dev->buf_alloc); |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | |
| 1127 | entry->buflist = drm_alloc(count * sizeof(*entry->buflist), |
| 1128 | DRM_MEM_BUFS); |
| 1129 | if (!entry->buflist) { |
| 1130 | up(&dev->struct_sem); |
| 1131 | atomic_dec(&dev->buf_alloc); |
| 1132 | return -ENOMEM; |
| 1133 | } |
| 1134 | memset(entry->buflist, 0, count * sizeof(*entry->buflist)); |
| 1135 | |
| 1136 | entry->buf_size = size; |
| 1137 | entry->page_order = page_order; |
| 1138 | |
| 1139 | offset = 0; |
| 1140 | |
| 1141 | while (entry->buf_count < count) { |
| 1142 | buf = &entry->buflist[entry->buf_count]; |
| 1143 | buf->idx = dma->buf_count + entry->buf_count; |
| 1144 | buf->total = alignment; |
| 1145 | buf->order = order; |
| 1146 | buf->used = 0; |
| 1147 | |
| 1148 | buf->offset = (dma->byte_count + offset); |
| 1149 | buf->bus_address = agp_offset + offset; |
| 1150 | buf->address = (void *)(agp_offset + offset); |
| 1151 | buf->next = NULL; |
| 1152 | buf->waiting = 0; |
| 1153 | buf->pending = 0; |
| 1154 | init_waitqueue_head(&buf->dma_wait); |
| 1155 | buf->filp = NULL; |
| 1156 | |
| 1157 | buf->dev_priv_size = dev->driver->dev_priv_size; |
| 1158 | buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS); |
| 1159 | if (!buf->dev_private) { |
| 1160 | /* Set count correctly so we free the proper amount. */ |
| 1161 | entry->buf_count = count; |
| 1162 | drm_cleanup_buf_error(dev, entry); |
| 1163 | up(&dev->struct_sem); |
| 1164 | atomic_dec(&dev->buf_alloc); |
| 1165 | return -ENOMEM; |
| 1166 | } |
| 1167 | memset(buf->dev_private, 0, buf->dev_priv_size); |
| 1168 | |
| 1169 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
| 1170 | |
| 1171 | offset += alignment; |
| 1172 | entry->buf_count++; |
| 1173 | byte_count += PAGE_SIZE << page_order; |
| 1174 | } |
| 1175 | |
| 1176 | DRM_DEBUG("byte_count: %d\n", byte_count); |
| 1177 | |
| 1178 | temp_buflist = drm_realloc(dma->buflist, |
| 1179 | dma->buf_count * sizeof(*dma->buflist), |
| 1180 | (dma->buf_count + entry->buf_count) |
| 1181 | * sizeof(*dma->buflist), DRM_MEM_BUFS); |
| 1182 | if (!temp_buflist) { |
| 1183 | /* Free the entry because it isn't valid */ |
| 1184 | drm_cleanup_buf_error(dev, entry); |
| 1185 | up(&dev->struct_sem); |
| 1186 | atomic_dec(&dev->buf_alloc); |
| 1187 | return -ENOMEM; |
| 1188 | } |
| 1189 | dma->buflist = temp_buflist; |
| 1190 | |
| 1191 | for (i = 0; i < entry->buf_count; i++) { |
| 1192 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
| 1193 | } |
| 1194 | |
| 1195 | dma->buf_count += entry->buf_count; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 1196 | dma->seg_count += entry->seg_count; |
| 1197 | dma->page_count += byte_count >> PAGE_SHIFT; |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1198 | dma->byte_count += byte_count; |
| 1199 | |
| 1200 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
| 1201 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); |
| 1202 | |
| 1203 | up(&dev->struct_sem); |
| 1204 | |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1205 | request->count = entry->buf_count; |
| 1206 | request->size = size; |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1207 | |
| 1208 | dma->flags = _DRM_DMA_USE_FB; |
| 1209 | |
| 1210 | atomic_dec(&dev->buf_alloc); |
| 1211 | return 0; |
| 1212 | } |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 1213 | EXPORT_SYMBOL(drm_addbufs_fb); |
| 1214 | |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | /** |
| 1217 | * Add buffers for DMA transfers (ioctl). |
| 1218 | * |
| 1219 | * \param inode device inode. |
| 1220 | * \param filp file pointer. |
| 1221 | * \param cmd command. |
| 1222 | * \param arg pointer to a drm_buf_desc_t request. |
| 1223 | * \return zero on success or a negative number on failure. |
| 1224 | * |
| 1225 | * According with the memory type specified in drm_buf_desc::flags and the |
| 1226 | * build options, it dispatches the call either to addbufs_agp(), |
| 1227 | * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent |
| 1228 | * PCI memory respectively. |
| 1229 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1230 | int drm_addbufs(struct inode *inode, struct file *filp, |
| 1231 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | { |
| 1233 | drm_buf_desc_t request; |
| 1234 | drm_file_t *priv = filp->private_data; |
| 1235 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1236 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1239 | return -EINVAL; |
| 1240 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1241 | if (copy_from_user(&request, (drm_buf_desc_t __user *) arg, |
| 1242 | sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | return -EFAULT; |
| 1244 | |
| 1245 | #if __OS_HAS_AGP |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1246 | if (request.flags & _DRM_AGP_BUFFER) |
| 1247 | ret = drm_addbufs_agp(dev, &request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | else |
| 1249 | #endif |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1250 | if (request.flags & _DRM_SG_BUFFER) |
| 1251 | ret = drm_addbufs_sg(dev, &request); |
| 1252 | else if (request.flags & _DRM_FB_BUFFER) |
| 1253 | ret = drm_addbufs_fb(dev, &request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | else |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1255 | ret = drm_addbufs_pci(dev, &request); |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1256 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1257 | if (ret == 0) { |
| 1258 | if (copy_to_user((void __user *)arg, &request, sizeof(request))) { |
Dave Airlie | d59431b | 2005-07-10 15:00:06 +1000 | [diff] [blame] | 1259 | ret = -EFAULT; |
| 1260 | } |
| 1261 | } |
| 1262 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | /** |
| 1266 | * Get information about the buffer mappings. |
| 1267 | * |
| 1268 | * This was originally mean for debugging purposes, or by a sophisticated |
| 1269 | * client library to determine how best to use the available buffers (e.g., |
| 1270 | * large buffers can be used for image transfer). |
| 1271 | * |
| 1272 | * \param inode device inode. |
| 1273 | * \param filp file pointer. |
| 1274 | * \param cmd command. |
| 1275 | * \param arg pointer to a drm_buf_info structure. |
| 1276 | * \return zero on success or a negative number on failure. |
| 1277 | * |
| 1278 | * Increments drm_device::buf_use while holding the drm_device::count_lock |
| 1279 | * lock, preventing of allocating more buffers after this call. Information |
| 1280 | * about each requested buffer is then copied into user space. |
| 1281 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1282 | int drm_infobufs(struct inode *inode, struct file *filp, |
| 1283 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | { |
| 1285 | drm_file_t *priv = filp->private_data; |
| 1286 | drm_device_t *dev = priv->head->dev; |
| 1287 | drm_device_dma_t *dma = dev->dma; |
| 1288 | drm_buf_info_t request; |
| 1289 | drm_buf_info_t __user *argp = (void __user *)arg; |
| 1290 | int i; |
| 1291 | int count; |
| 1292 | |
| 1293 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1294 | return -EINVAL; |
| 1295 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1296 | if (!dma) |
| 1297 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1299 | spin_lock(&dev->count_lock); |
| 1300 | if (atomic_read(&dev->buf_alloc)) { |
| 1301 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | return -EBUSY; |
| 1303 | } |
| 1304 | ++dev->buf_use; /* Can't allocate more after this call */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1305 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1307 | if (copy_from_user(&request, argp, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | return -EFAULT; |
| 1309 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1310 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
| 1311 | if (dma->bufs[i].buf_count) |
| 1312 | ++count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1315 | DRM_DEBUG("count = %d\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1317 | if (request.count >= count) { |
| 1318 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
| 1319 | if (dma->bufs[i].buf_count) { |
| 1320 | drm_buf_desc_t __user *to = |
| 1321 | &request.list[count]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | drm_buf_entry_t *from = &dma->bufs[i]; |
| 1323 | drm_freelist_t *list = &dma->bufs[i].freelist; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1324 | if (copy_to_user(&to->count, |
| 1325 | &from->buf_count, |
| 1326 | sizeof(from->buf_count)) || |
| 1327 | copy_to_user(&to->size, |
| 1328 | &from->buf_size, |
| 1329 | sizeof(from->buf_size)) || |
| 1330 | copy_to_user(&to->low_mark, |
| 1331 | &list->low_mark, |
| 1332 | sizeof(list->low_mark)) || |
| 1333 | copy_to_user(&to->high_mark, |
| 1334 | &list->high_mark, |
| 1335 | sizeof(list->high_mark))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | return -EFAULT; |
| 1337 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1338 | DRM_DEBUG("%d %d %d %d %d\n", |
| 1339 | i, |
| 1340 | dma->bufs[i].buf_count, |
| 1341 | dma->bufs[i].buf_size, |
| 1342 | dma->bufs[i].freelist.low_mark, |
| 1343 | dma->bufs[i].freelist.high_mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | ++count; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | request.count = count; |
| 1349 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1350 | if (copy_to_user(argp, &request, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | return -EFAULT; |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * Specifies a low and high water mark for buffer allocation |
| 1358 | * |
| 1359 | * \param inode device inode. |
| 1360 | * \param filp file pointer. |
| 1361 | * \param cmd command. |
| 1362 | * \param arg a pointer to a drm_buf_desc structure. |
| 1363 | * \return zero on success or a negative number on failure. |
| 1364 | * |
| 1365 | * Verifies that the size order is bounded between the admissible orders and |
| 1366 | * updates the respective drm_device_dma::bufs entry low and high water mark. |
| 1367 | * |
| 1368 | * \note This ioctl is deprecated and mostly never used. |
| 1369 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1370 | int drm_markbufs(struct inode *inode, struct file *filp, |
| 1371 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | { |
| 1373 | drm_file_t *priv = filp->private_data; |
| 1374 | drm_device_t *dev = priv->head->dev; |
| 1375 | drm_device_dma_t *dma = dev->dma; |
| 1376 | drm_buf_desc_t request; |
| 1377 | int order; |
| 1378 | drm_buf_entry_t *entry; |
| 1379 | |
| 1380 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1381 | return -EINVAL; |
| 1382 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1383 | if (!dma) |
| 1384 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1386 | if (copy_from_user(&request, |
| 1387 | (drm_buf_desc_t __user *) arg, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | return -EFAULT; |
| 1389 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1390 | DRM_DEBUG("%d, %d, %d\n", |
| 1391 | request.size, request.low_mark, request.high_mark); |
| 1392 | order = drm_order(request.size); |
| 1393 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
| 1394 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | entry = &dma->bufs[order]; |
| 1396 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1397 | if (request.low_mark < 0 || request.low_mark > entry->buf_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1399 | if (request.high_mark < 0 || request.high_mark > entry->buf_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | return -EINVAL; |
| 1401 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1402 | entry->freelist.low_mark = request.low_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | entry->freelist.high_mark = request.high_mark; |
| 1404 | |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1409 | * Unreserve the buffers in list, previously reserved using drmDMA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | * |
| 1411 | * \param inode device inode. |
| 1412 | * \param filp file pointer. |
| 1413 | * \param cmd command. |
| 1414 | * \param arg pointer to a drm_buf_free structure. |
| 1415 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1416 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | * Calls free_buffer() for each used buffer. |
| 1418 | * This function is primarily used for debugging. |
| 1419 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1420 | int drm_freebufs(struct inode *inode, struct file *filp, |
| 1421 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | { |
| 1423 | drm_file_t *priv = filp->private_data; |
| 1424 | drm_device_t *dev = priv->head->dev; |
| 1425 | drm_device_dma_t *dma = dev->dma; |
| 1426 | drm_buf_free_t request; |
| 1427 | int i; |
| 1428 | int idx; |
| 1429 | drm_buf_t *buf; |
| 1430 | |
| 1431 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1432 | return -EINVAL; |
| 1433 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1434 | if (!dma) |
| 1435 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1437 | if (copy_from_user(&request, |
| 1438 | (drm_buf_free_t __user *) arg, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | return -EFAULT; |
| 1440 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1441 | DRM_DEBUG("%d\n", request.count); |
| 1442 | for (i = 0; i < request.count; i++) { |
| 1443 | if (copy_from_user(&idx, &request.list[i], sizeof(idx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | return -EFAULT; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1445 | if (idx < 0 || idx >= dma->buf_count) { |
| 1446 | DRM_ERROR("Index %d (of %d max)\n", |
| 1447 | idx, dma->buf_count - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | return -EINVAL; |
| 1449 | } |
| 1450 | buf = dma->buflist[idx]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1451 | if (buf->filp != filp) { |
| 1452 | DRM_ERROR("Process %d freeing buffer not owned\n", |
| 1453 | current->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | return -EINVAL; |
| 1455 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1456 | drm_free_buffer(dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * Maps all of the DMA buffers into client-virtual space (ioctl). |
| 1464 | * |
| 1465 | * \param inode device inode. |
| 1466 | * \param filp file pointer. |
| 1467 | * \param cmd command. |
| 1468 | * \param arg pointer to a drm_buf_map structure. |
| 1469 | * \return zero on success or a negative number on failure. |
| 1470 | * |
| 1471 | * Maps the AGP or SG buffer region with do_mmap(), and copies information |
| 1472 | * about each buffer into user space. The PCI buffers are already mapped on the |
| 1473 | * addbufs_pci() call. |
| 1474 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1475 | int drm_mapbufs(struct inode *inode, struct file *filp, |
| 1476 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | { |
| 1478 | drm_file_t *priv = filp->private_data; |
| 1479 | drm_device_t *dev = priv->head->dev; |
| 1480 | drm_device_dma_t *dma = dev->dma; |
| 1481 | drm_buf_map_t __user *argp = (void __user *)arg; |
| 1482 | int retcode = 0; |
| 1483 | const int zero = 0; |
| 1484 | unsigned long virtual; |
| 1485 | unsigned long address; |
| 1486 | drm_buf_map_t request; |
| 1487 | int i; |
| 1488 | |
| 1489 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
| 1490 | return -EINVAL; |
| 1491 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1492 | if (!dma) |
| 1493 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1495 | spin_lock(&dev->count_lock); |
| 1496 | if (atomic_read(&dev->buf_alloc)) { |
| 1497 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | return -EBUSY; |
| 1499 | } |
| 1500 | dev->buf_use++; /* Can't allocate more after this call */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1501 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1503 | if (copy_from_user(&request, argp, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | return -EFAULT; |
| 1505 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1506 | if (request.count >= dma->buf_count) { |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1507 | if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1508 | || (drm_core_check_feature(dev, DRIVER_SG) |
Dave Airlie | b84397d6 | 2005-07-10 14:46:12 +1000 | [diff] [blame] | 1509 | && (dma->flags & _DRM_DMA_USE_SG)) |
| 1510 | || (drm_core_check_feature(dev, DRIVER_FB_DMA) |
| 1511 | && (dma->flags & _DRM_DMA_USE_FB))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | drm_map_t *map = dev->agp_buffer_map; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 1513 | unsigned long token = dev->agp_buffer_token; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1515 | if (!map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | retcode = -EINVAL; |
| 1517 | goto done; |
| 1518 | } |
| 1519 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1520 | down_write(¤t->mm->mmap_sem); |
| 1521 | virtual = do_mmap(filp, 0, map->size, |
| 1522 | PROT_READ | PROT_WRITE, |
| 1523 | MAP_SHARED, token); |
| 1524 | up_write(¤t->mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } else { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1526 | down_write(¤t->mm->mmap_sem); |
| 1527 | virtual = do_mmap(filp, 0, dma->byte_count, |
| 1528 | PROT_READ | PROT_WRITE, |
| 1529 | MAP_SHARED, 0); |
| 1530 | up_write(¤t->mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1532 | if (virtual > -1024UL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | /* Real error */ |
| 1534 | retcode = (signed long)virtual; |
| 1535 | goto done; |
| 1536 | } |
| 1537 | request.virtual = (void __user *)virtual; |
| 1538 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1539 | for (i = 0; i < dma->buf_count; i++) { |
| 1540 | if (copy_to_user(&request.list[i].idx, |
| 1541 | &dma->buflist[i]->idx, |
| 1542 | sizeof(request.list[0].idx))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | retcode = -EFAULT; |
| 1544 | goto done; |
| 1545 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1546 | if (copy_to_user(&request.list[i].total, |
| 1547 | &dma->buflist[i]->total, |
| 1548 | sizeof(request.list[0].total))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | retcode = -EFAULT; |
| 1550 | goto done; |
| 1551 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1552 | if (copy_to_user(&request.list[i].used, |
| 1553 | &zero, sizeof(zero))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | retcode = -EFAULT; |
| 1555 | goto done; |
| 1556 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1557 | address = virtual + dma->buflist[i]->offset; /* *** */ |
| 1558 | if (copy_to_user(&request.list[i].address, |
| 1559 | &address, sizeof(address))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | retcode = -EFAULT; |
| 1561 | goto done; |
| 1562 | } |
| 1563 | } |
| 1564 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1565 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | request.count = dma->buf_count; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1567 | DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1569 | if (copy_to_user(argp, &request, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | return -EFAULT; |
| 1571 | |
| 1572 | return retcode; |
| 1573 | } |
| 1574 | |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 1575 | /** |
| 1576 | * Compute size order. Returns the exponent of the smaller power of two which |
| 1577 | * is greater or equal to given number. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1578 | * |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 1579 | * \param size size. |
| 1580 | * \return order. |
| 1581 | * |
| 1582 | * \todo Can be made faster. |
| 1583 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1584 | int drm_order(unsigned long size) |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 1585 | { |
| 1586 | int order; |
| 1587 | unsigned long tmp; |
| 1588 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1589 | for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ; |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 1590 | |
| 1591 | if (size & (size - 1)) |
| 1592 | ++order; |
| 1593 | |
| 1594 | return order; |
| 1595 | } |
| 1596 | EXPORT_SYMBOL(drm_order); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame^] | 1597 | |
| 1598 | |