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