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