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