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_context.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * IOCTLs for generic contexts |
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: Fri Nov 24 18:31:37 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 | /* |
| 37 | * ChangeLog: |
| 38 | * 2001-11-16 Torsten Duwe <duwe@caldera.de> |
| 39 | * added context constructor/destructor hooks, |
| 40 | * needed by SiS driver's memory management. |
| 41 | */ |
| 42 | |
| 43 | #include "drmP.h" |
| 44 | |
| 45 | /******************************************************************/ |
| 46 | /** \name Context bitmap support */ |
| 47 | /*@{*/ |
| 48 | |
| 49 | /** |
| 50 | * Free a handle from the context bitmap. |
| 51 | * |
| 52 | * \param dev DRM device. |
| 53 | * \param ctx_handle context handle. |
| 54 | * |
| 55 | * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 56 | * in drm_device::context_sareas, while holding the drm_device::struct_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * lock. |
| 58 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 59 | void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 61 | if (ctx_handle < 0) |
| 62 | goto failed; |
| 63 | if (!dev->ctx_bitmap) |
| 64 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 66 | if (ctx_handle < DRM_MAX_CTXBITMAP) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 67 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 68 | clear_bit(ctx_handle, dev->ctx_bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | dev->context_sareas[ctx_handle] = NULL; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 70 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return; |
| 72 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 73 | failed: |
| 74 | DRM_ERROR("Attempt to free invalid context handle: %d\n", ctx_handle); |
| 75 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 78 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * Context bitmap allocation. |
| 80 | * |
| 81 | * \param dev DRM device. |
| 82 | * \return (non-negative) context handle on success or a negative number on failure. |
| 83 | * |
| 84 | * Find the first zero bit in drm_device::ctx_bitmap and (re)allocates |
| 85 | * drm_device::context_sareas to accommodate the new entry while holding the |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 86 | * drm_device::struct_mutex lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 88 | static int drm_ctxbitmap_next(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | int bit; |
| 91 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 92 | if (!dev->ctx_bitmap) |
| 93 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 95 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 96 | bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP); |
| 97 | if (bit < DRM_MAX_CTXBITMAP) { |
| 98 | set_bit(bit, dev->ctx_bitmap); |
| 99 | DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit); |
| 100 | if ((bit + 1) > dev->max_context) { |
| 101 | dev->max_context = (bit + 1); |
| 102 | if (dev->context_sareas) { |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 103 | struct drm_map **ctx_sareas; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | ctx_sareas = drm_realloc(dev->context_sareas, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 106 | (dev->max_context - |
| 107 | 1) * |
| 108 | sizeof(*dev-> |
| 109 | context_sareas), |
| 110 | dev->max_context * |
| 111 | sizeof(*dev-> |
| 112 | context_sareas), |
| 113 | DRM_MEM_MAPS); |
| 114 | if (!ctx_sareas) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | clear_bit(bit, dev->ctx_bitmap); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 116 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return -1; |
| 118 | } |
| 119 | dev->context_sareas = ctx_sareas; |
| 120 | dev->context_sareas[bit] = NULL; |
| 121 | } else { |
| 122 | /* max_context == 1 at this point */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 123 | dev->context_sareas = |
| 124 | drm_alloc(dev->max_context * |
| 125 | sizeof(*dev->context_sareas), |
| 126 | DRM_MEM_MAPS); |
| 127 | if (!dev->context_sareas) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | clear_bit(bit, dev->ctx_bitmap); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 129 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | dev->context_sareas[bit] = NULL; |
| 133 | } |
| 134 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 135 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return bit; |
| 137 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 138 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Context bitmap initialization. |
| 144 | * |
| 145 | * \param dev DRM device. |
| 146 | * |
| 147 | * Allocates and initialize drm_device::ctx_bitmap and drm_device::context_sareas, while holding |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 148 | * the drm_device::struct_mutex lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 150 | int drm_ctxbitmap_init(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | int i; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 153 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 155 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 156 | dev->ctx_bitmap = (unsigned long *)drm_alloc(PAGE_SIZE, |
| 157 | DRM_MEM_CTXBITMAP); |
| 158 | if (dev->ctx_bitmap == NULL) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 159 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return -ENOMEM; |
| 161 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 162 | memset((void *)dev->ctx_bitmap, 0, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | dev->context_sareas = NULL; |
| 164 | dev->max_context = -1; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 165 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 167 | for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { |
| 168 | temp = drm_ctxbitmap_next(dev); |
| 169 | DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Context bitmap cleanup. |
| 177 | * |
| 178 | * \param dev DRM device. |
| 179 | * |
| 180 | * Frees drm_device::ctx_bitmap and drm_device::context_sareas, while holding |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 181 | * the drm_device::struct_mutex lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 183 | void drm_ctxbitmap_cleanup(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 185 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 186 | if (dev->context_sareas) |
| 187 | drm_free(dev->context_sareas, |
| 188 | sizeof(*dev->context_sareas) * |
| 189 | dev->max_context, DRM_MEM_MAPS); |
| 190 | drm_free((void *)dev->ctx_bitmap, PAGE_SIZE, DRM_MEM_CTXBITMAP); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 191 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /*@}*/ |
| 195 | |
| 196 | /******************************************************************/ |
| 197 | /** \name Per Context SAREA Support */ |
| 198 | /*@{*/ |
| 199 | |
| 200 | /** |
| 201 | * Get per-context SAREA. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 202 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * \param inode device inode. |
| 204 | * \param filp file pointer. |
| 205 | * \param cmd command. |
| 206 | * \param arg user argument pointing to a drm_ctx_priv_map structure. |
| 207 | * \return zero on success or a negative number on failure. |
| 208 | * |
| 209 | * Gets the map from drm_device::context_sareas with the handle specified and |
| 210 | * returns its handle. |
| 211 | */ |
| 212 | int drm_getsareactx(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 213 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 215 | drm_file_t *priv = filp->private_data; |
| 216 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 217 | struct drm_ctx_priv_map __user *argp = (void __user *)arg; |
| 218 | struct drm_ctx_priv_map request; |
| 219 | struct drm_map *map; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 220 | drm_map_list_t *_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | if (copy_from_user(&request, argp, sizeof(request))) |
| 223 | return -EFAULT; |
| 224 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 225 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 226 | if (dev->max_context < 0 |
| 227 | || request.ctx_id >= (unsigned)dev->max_context) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 228 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | map = dev->context_sareas[request.ctx_id]; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 233 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Dave Airlie | b3a8363 | 2005-09-30 18:37:36 +1000 | [diff] [blame] | 235 | request.handle = NULL; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 236 | list_for_each_entry(_entry, &dev->maplist, head) { |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 237 | if (_entry->map == map) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 238 | request.handle = |
| 239 | (void *)(unsigned long)_entry->user_token; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | } |
Dave Airlie | b3a8363 | 2005-09-30 18:37:36 +1000 | [diff] [blame] | 243 | if (request.handle == NULL) |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 244 | return -EINVAL; |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (copy_to_user(argp, &request, sizeof(request))) |
| 247 | return -EFAULT; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Set per-context SAREA. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 253 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | * \param inode device inode. |
| 255 | * \param filp file pointer. |
| 256 | * \param cmd command. |
| 257 | * \param arg user argument pointing to a drm_ctx_priv_map structure. |
| 258 | * \return zero on success or a negative number on failure. |
| 259 | * |
| 260 | * Searches the mapping specified in \p arg and update the entry in |
| 261 | * drm_device::context_sareas with it. |
| 262 | */ |
| 263 | int drm_setsareactx(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 264 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 266 | drm_file_t *priv = filp->private_data; |
| 267 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 268 | struct drm_ctx_priv_map request; |
| 269 | struct drm_map *map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | drm_map_list_t *r_list = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (copy_from_user(&request, |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 273 | (struct drm_ctx_priv_map __user *) arg, |
| 274 | sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | return -EFAULT; |
| 276 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 277 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 278 | list_for_each_entry(r_list, &dev->maplist, head) { |
Dave Airlie | 9a18664 | 2005-06-23 21:29:18 +1000 | [diff] [blame] | 279 | if (r_list->map |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 280 | && r_list->user_token == (unsigned long)request.handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | goto found; |
| 282 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 283 | bad: |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 284 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return -EINVAL; |
| 286 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 287 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | map = r_list->map; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 289 | if (!map) |
| 290 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (dev->max_context < 0) |
| 292 | goto bad; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 293 | if (request.ctx_id >= (unsigned)dev->max_context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | goto bad; |
| 295 | dev->context_sareas[request.ctx_id] = map; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 296 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /*@}*/ |
| 301 | |
| 302 | /******************************************************************/ |
| 303 | /** \name The actual DRM context handling routines */ |
| 304 | /*@{*/ |
| 305 | |
| 306 | /** |
| 307 | * Switch context. |
| 308 | * |
| 309 | * \param dev DRM device. |
| 310 | * \param old old context handle. |
| 311 | * \param new new context handle. |
| 312 | * \return zero on success or a negative number on failure. |
| 313 | * |
| 314 | * Attempt to set drm_device::context_flag. |
| 315 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 316 | static int drm_context_switch(drm_device_t * dev, int old, int new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 318 | if (test_and_set_bit(0, &dev->context_flag)) { |
| 319 | DRM_ERROR("Reentering -- FIXME\n"); |
| 320 | return -EBUSY; |
| 321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 323 | DRM_DEBUG("Context switch from %d to %d\n", old, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 325 | if (new == dev->last_context) { |
| 326 | clear_bit(0, &dev->context_flag); |
| 327 | return 0; |
| 328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 330 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Complete context switch. |
| 335 | * |
| 336 | * \param dev DRM device. |
| 337 | * \param new new context handle. |
| 338 | * \return zero on success or a negative number on failure. |
| 339 | * |
| 340 | * Updates drm_device::last_context and drm_device::last_switch. Verifies the |
| 341 | * hardware lock is held, clears the drm_device::context_flag and wakes up |
| 342 | * drm_device::context_wait. |
| 343 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 344 | static int drm_context_switch_complete(drm_device_t * dev, int new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 346 | dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ |
| 347 | dev->last_switch = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 349 | if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) { |
| 350 | DRM_ERROR("Lock isn't held after context switch\n"); |
| 351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 353 | /* If a context switch is ever initiated |
| 354 | when the kernel holds the lock, release |
| 355 | that lock here. */ |
| 356 | clear_bit(0, &dev->context_flag); |
| 357 | wake_up(&dev->context_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 359 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Reserve contexts. |
| 364 | * |
| 365 | * \param inode device inode. |
| 366 | * \param filp file pointer. |
| 367 | * \param cmd command. |
| 368 | * \param arg user argument pointing to a drm_ctx_res structure. |
| 369 | * \return zero on success or a negative number on failure. |
| 370 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 371 | int drm_resctx(struct inode *inode, struct file *filp, |
| 372 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 374 | struct drm_ctx_res res; |
| 375 | struct drm_ctx_res __user *argp = (void __user *)arg; |
| 376 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | int i; |
| 378 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 379 | if (copy_from_user(&res, argp, sizeof(res))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return -EFAULT; |
| 381 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 382 | if (res.count >= DRM_RESERVED_CONTEXTS) { |
| 383 | memset(&ctx, 0, sizeof(ctx)); |
| 384 | for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | ctx.handle = i; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 386 | if (copy_to_user(&res.contexts[i], &ctx, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return -EFAULT; |
| 388 | } |
| 389 | } |
| 390 | res.count = DRM_RESERVED_CONTEXTS; |
| 391 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 392 | if (copy_to_user(argp, &res, sizeof(res))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return -EFAULT; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Add context. |
| 399 | * |
| 400 | * \param inode device inode. |
| 401 | * \param filp file pointer. |
| 402 | * \param cmd command. |
| 403 | * \param arg user argument pointing to a drm_ctx structure. |
| 404 | * \return zero on success or a negative number on failure. |
| 405 | * |
| 406 | * Get a new handle for the context and copy to userspace. |
| 407 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 408 | int drm_addctx(struct inode *inode, struct file *filp, |
| 409 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
| 411 | drm_file_t *priv = filp->private_data; |
| 412 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 413 | drm_ctx_list_t *ctx_entry; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 414 | struct drm_ctx __user *argp = (void __user *)arg; |
| 415 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 417 | if (copy_from_user(&ctx, argp, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return -EFAULT; |
| 419 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 420 | ctx.handle = drm_ctxbitmap_next(dev); |
| 421 | if (ctx.handle == DRM_KERNEL_CONTEXT) { |
| 422 | /* Skip kernel's context and get a new one. */ |
| 423 | ctx.handle = drm_ctxbitmap_next(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 425 | DRM_DEBUG("%d\n", ctx.handle); |
| 426 | if (ctx.handle == -1) { |
| 427 | DRM_DEBUG("Not enough free contexts.\n"); |
| 428 | /* Should this return -EBUSY instead? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | return -ENOMEM; |
| 430 | } |
| 431 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 432 | if (ctx.handle != DRM_KERNEL_CONTEXT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | if (dev->driver->context_ctor) |
Dave Airlie | 1e7d519 | 2006-01-02 19:25:35 +1100 | [diff] [blame] | 434 | if (!dev->driver->context_ctor(dev, ctx.handle)) { |
Dave Airlie | b5e9fc1 | 2006-01-02 19:23:44 +1100 | [diff] [blame] | 435 | DRM_DEBUG("Running out of ctxs or memory.\n"); |
| 436 | return -ENOMEM; |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 440 | ctx_entry = drm_alloc(sizeof(*ctx_entry), DRM_MEM_CTXLIST); |
| 441 | if (!ctx_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | DRM_DEBUG("out of memory\n"); |
| 443 | return -ENOMEM; |
| 444 | } |
| 445 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 446 | INIT_LIST_HEAD(&ctx_entry->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | ctx_entry->handle = ctx.handle; |
| 448 | ctx_entry->tag = priv; |
| 449 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 450 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 451 | list_add(&ctx_entry->head, &dev->ctxlist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | ++dev->ctx_count; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 453 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 455 | if (copy_to_user(argp, &ctx, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return -EFAULT; |
| 457 | return 0; |
| 458 | } |
| 459 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 460 | int drm_modctx(struct inode *inode, struct file *filp, |
| 461 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
| 463 | /* This does nothing */ |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Get context. |
| 469 | * |
| 470 | * \param inode device inode. |
| 471 | * \param filp file pointer. |
| 472 | * \param cmd command. |
| 473 | * \param arg user argument pointing to a drm_ctx structure. |
| 474 | * \return zero on success or a negative number on failure. |
| 475 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 476 | int drm_getctx(struct inode *inode, struct file *filp, |
| 477 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 479 | struct drm_ctx __user *argp = (void __user *)arg; |
| 480 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 482 | if (copy_from_user(&ctx, argp, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return -EFAULT; |
| 484 | |
| 485 | /* This is 0, because we don't handle any context flags */ |
| 486 | ctx.flags = 0; |
| 487 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 488 | if (copy_to_user(argp, &ctx, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return -EFAULT; |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Switch context. |
| 495 | * |
| 496 | * \param inode device inode. |
| 497 | * \param filp file pointer. |
| 498 | * \param cmd command. |
| 499 | * \param arg user argument pointing to a drm_ctx structure. |
| 500 | * \return zero on success or a negative number on failure. |
| 501 | * |
| 502 | * Calls context_switch(). |
| 503 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 504 | int drm_switchctx(struct inode *inode, struct file *filp, |
| 505 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
| 507 | drm_file_t *priv = filp->private_data; |
| 508 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 509 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 511 | if (copy_from_user(&ctx, (struct drm_ctx __user *) arg, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | return -EFAULT; |
| 513 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 514 | DRM_DEBUG("%d\n", ctx.handle); |
| 515 | return drm_context_switch(dev, dev->last_context, ctx.handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
| 519 | * New context. |
| 520 | * |
| 521 | * \param inode device inode. |
| 522 | * \param filp file pointer. |
| 523 | * \param cmd command. |
| 524 | * \param arg user argument pointing to a drm_ctx structure. |
| 525 | * \return zero on success or a negative number on failure. |
| 526 | * |
| 527 | * Calls context_switch_complete(). |
| 528 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 529 | int drm_newctx(struct inode *inode, struct file *filp, |
| 530 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
| 532 | drm_file_t *priv = filp->private_data; |
| 533 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 534 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 536 | if (copy_from_user(&ctx, (struct drm_ctx __user *) arg, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return -EFAULT; |
| 538 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 539 | DRM_DEBUG("%d\n", ctx.handle); |
| 540 | drm_context_switch_complete(dev, ctx.handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Remove context. |
| 547 | * |
| 548 | * \param inode device inode. |
| 549 | * \param filp file pointer. |
| 550 | * \param cmd command. |
| 551 | * \param arg user argument pointing to a drm_ctx structure. |
| 552 | * \return zero on success or a negative number on failure. |
| 553 | * |
| 554 | * If not the special kernel context, calls ctxbitmap_free() to free the specified context. |
| 555 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 556 | int drm_rmctx(struct inode *inode, struct file *filp, |
| 557 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | drm_file_t *priv = filp->private_data; |
| 560 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 561 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame^] | 563 | if (copy_from_user(&ctx, (struct drm_ctx __user *) arg, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return -EFAULT; |
| 565 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 566 | DRM_DEBUG("%d\n", ctx.handle); |
| 567 | if (ctx.handle == DRM_KERNEL_CONTEXT + 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | priv->remove_auth_on_close = 1; |
| 569 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 570 | if (ctx.handle != DRM_KERNEL_CONTEXT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (dev->driver->context_dtor) |
| 572 | dev->driver->context_dtor(dev, ctx.handle); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 573 | drm_ctxbitmap_free(dev, ctx.handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 576 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 577 | if (!list_empty(&dev->ctxlist)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | drm_ctx_list_t *pos, *n; |
| 579 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 580 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 581 | if (pos->handle == ctx.handle) { |
| 582 | list_del(&pos->head); |
| 583 | drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | --dev->ctx_count; |
| 585 | } |
| 586 | } |
| 587 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 588 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | /*@}*/ |