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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 43 | #include <drm/drmP.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Free a handle from the context bitmap. |
| 47 | * |
| 48 | * \param dev DRM device. |
| 49 | * \param ctx_handle context handle. |
| 50 | * |
| 51 | * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 52 | * in drm_device::ctx_idr, while holding the drm_device::struct_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | * lock. |
| 54 | */ |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 55 | static void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 57 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 58 | return; |
| 59 | |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 60 | mutex_lock(&dev->struct_mutex); |
| 61 | idr_remove(&dev->ctx_idr, ctx_handle); |
| 62 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 65 | /******************************************************************/ |
| 66 | /** \name Context bitmap support */ |
| 67 | /*@{*/ |
| 68 | |
| 69 | void drm_legacy_ctxbitmap_release(struct drm_device *dev, |
| 70 | struct drm_file *file_priv) |
| 71 | { |
| 72 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 73 | return; |
| 74 | |
| 75 | mutex_lock(&dev->ctxlist_mutex); |
| 76 | if (!list_empty(&dev->ctxlist)) { |
| 77 | struct drm_ctx_list *pos, *n; |
| 78 | |
| 79 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
| 80 | if (pos->tag == file_priv && |
| 81 | pos->handle != DRM_KERNEL_CONTEXT) { |
| 82 | if (dev->driver->context_dtor) |
| 83 | dev->driver->context_dtor(dev, |
| 84 | pos->handle); |
| 85 | |
| 86 | drm_ctxbitmap_free(dev, pos->handle); |
| 87 | |
| 88 | list_del(&pos->head); |
| 89 | kfree(pos); |
| 90 | --dev->ctx_count; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | mutex_unlock(&dev->ctxlist_mutex); |
| 95 | } |
| 96 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 97 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * Context bitmap allocation. |
| 99 | * |
| 100 | * \param dev DRM device. |
| 101 | * \return (non-negative) context handle on success or a negative number on failure. |
| 102 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 103 | * Allocate a new idr from drm_device::ctx_idr while holding the |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 104 | * drm_device::struct_mutex lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 106 | static int drm_ctxbitmap_next(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 108 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 110 | mutex_lock(&dev->struct_mutex); |
Tejun Heo | 2e92881 | 2013-02-27 17:04:08 -0800 | [diff] [blame] | 111 | ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0, |
| 112 | GFP_KERNEL); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 113 | mutex_unlock(&dev->struct_mutex); |
Tejun Heo | 2e92881 | 2013-02-27 17:04:08 -0800 | [diff] [blame] | 114 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Context bitmap initialization. |
| 119 | * |
| 120 | * \param dev DRM device. |
| 121 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 122 | * Initialise the drm_device::ctx_idr |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | */ |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 124 | void drm_legacy_ctxbitmap_init(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 126 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 127 | return; |
| 128 | |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 129 | idr_init(&dev->ctx_idr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Context bitmap cleanup. |
| 134 | * |
| 135 | * \param dev DRM device. |
| 136 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 137 | * Free all idr members using drm_ctx_sarea_free helper function |
| 138 | * while holding the drm_device::struct_mutex lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | */ |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 140 | void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 142 | mutex_lock(&dev->struct_mutex); |
Tejun Heo | 4d53233 | 2013-02-27 17:03:39 -0800 | [diff] [blame] | 143 | idr_destroy(&dev->ctx_idr); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 144 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /*@}*/ |
| 148 | |
| 149 | /******************************************************************/ |
| 150 | /** \name Per Context SAREA Support */ |
| 151 | /*@{*/ |
| 152 | |
| 153 | /** |
| 154 | * Get per-context SAREA. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 155 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 157 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | * \param cmd command. |
| 159 | * \param arg user argument pointing to a drm_ctx_priv_map structure. |
| 160 | * \return zero on success or a negative number on failure. |
| 161 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 162 | * Gets the map from drm_device::ctx_idr with the handle specified and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | * returns its handle. |
| 164 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 165 | int drm_getsareactx(struct drm_device *dev, void *data, |
| 166 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 168 | struct drm_ctx_priv_map *request = data; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 169 | struct drm_local_map *map; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 170 | struct drm_map_list *_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 172 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 173 | return -EINVAL; |
| 174 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 175 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 176 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 177 | map = idr_find(&dev->ctx_idr, request->ctx_id); |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 178 | if (!map) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 179 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return -EINVAL; |
| 181 | } |
| 182 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 183 | request->handle = NULL; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 184 | list_for_each_entry(_entry, &dev->maplist, head) { |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 185 | if (_entry->map == map) { |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 186 | request->handle = |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 187 | (void *)(unsigned long)_entry->user_token; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | } |
Ilija Hadzic | 09b4ea4 | 2011-10-28 17:43:28 -0400 | [diff] [blame] | 191 | |
| 192 | mutex_unlock(&dev->struct_mutex); |
| 193 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 194 | if (request->handle == NULL) |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 195 | return -EINVAL; |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Set 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. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 204 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * Searches the mapping specified in \p arg and update the entry in |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 210 | * drm_device::ctx_idr with it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 212 | int drm_setsareactx(struct drm_device *dev, void *data, |
| 213 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 215 | struct drm_ctx_priv_map *request = data; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 216 | struct drm_local_map *map = NULL; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 217 | struct drm_map_list *r_list = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 219 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 220 | return -EINVAL; |
| 221 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 222 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 223 | list_for_each_entry(r_list, &dev->maplist, head) { |
Dave Airlie | 9a18664 | 2005-06-23 21:29:18 +1000 | [diff] [blame] | 224 | if (r_list->map |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 225 | && r_list->user_token == (unsigned long) request->handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | goto found; |
| 227 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 228 | bad: |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 229 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return -EINVAL; |
| 231 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 232 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | map = r_list->map; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 234 | if (!map) |
| 235 | goto bad; |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 236 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 237 | if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | goto bad; |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 239 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 240 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /*@}*/ |
| 246 | |
| 247 | /******************************************************************/ |
| 248 | /** \name The actual DRM context handling routines */ |
| 249 | /*@{*/ |
| 250 | |
| 251 | /** |
| 252 | * Switch context. |
| 253 | * |
| 254 | * \param dev DRM device. |
| 255 | * \param old old context handle. |
| 256 | * \param new new context handle. |
| 257 | * \return zero on success or a negative number on failure. |
| 258 | * |
| 259 | * Attempt to set drm_device::context_flag. |
| 260 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 261 | static int drm_context_switch(struct drm_device * dev, int old, int new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 263 | if (test_and_set_bit(0, &dev->context_flag)) { |
| 264 | DRM_ERROR("Reentering -- FIXME\n"); |
| 265 | return -EBUSY; |
| 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 268 | DRM_DEBUG("Context switch from %d to %d\n", old, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 270 | if (new == dev->last_context) { |
| 271 | clear_bit(0, &dev->context_flag); |
| 272 | return 0; |
| 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 275 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Complete context switch. |
| 280 | * |
| 281 | * \param dev DRM device. |
| 282 | * \param new new context handle. |
| 283 | * \return zero on success or a negative number on failure. |
| 284 | * |
| 285 | * Updates drm_device::last_context and drm_device::last_switch. Verifies the |
| 286 | * hardware lock is held, clears the drm_device::context_flag and wakes up |
| 287 | * drm_device::context_wait. |
| 288 | */ |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 289 | static int drm_context_switch_complete(struct drm_device *dev, |
| 290 | struct drm_file *file_priv, int new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 292 | dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 294 | if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 295 | DRM_ERROR("Lock isn't held after context switch\n"); |
| 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 298 | /* If a context switch is ever initiated |
| 299 | when the kernel holds the lock, release |
| 300 | that lock here. */ |
| 301 | clear_bit(0, &dev->context_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 303 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Reserve contexts. |
| 308 | * |
| 309 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 310 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | * \param cmd command. |
| 312 | * \param arg user argument pointing to a drm_ctx_res structure. |
| 313 | * \return zero on success or a negative number on failure. |
| 314 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 315 | int drm_resctx(struct drm_device *dev, void *data, |
| 316 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 318 | struct drm_ctx_res *res = data; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 319 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | int i; |
| 321 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 322 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 323 | return -EINVAL; |
| 324 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 325 | if (res->count >= DRM_RESERVED_CONTEXTS) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 326 | memset(&ctx, 0, sizeof(ctx)); |
| 327 | for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | ctx.handle = i; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 329 | if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return -EFAULT; |
| 331 | } |
| 332 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 333 | res->count = DRM_RESERVED_CONTEXTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Add context. |
| 340 | * |
| 341 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 342 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | * \param cmd command. |
| 344 | * \param arg user argument pointing to a drm_ctx structure. |
| 345 | * \return zero on success or a negative number on failure. |
| 346 | * |
| 347 | * Get a new handle for the context and copy to userspace. |
| 348 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 349 | int drm_addctx(struct drm_device *dev, void *data, |
| 350 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 352 | struct drm_ctx_list *ctx_entry; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 353 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 355 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 356 | return -EINVAL; |
| 357 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 358 | ctx->handle = drm_ctxbitmap_next(dev); |
| 359 | if (ctx->handle == DRM_KERNEL_CONTEXT) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 360 | /* Skip kernel's context and get a new one. */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 361 | ctx->handle = drm_ctxbitmap_next(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 363 | DRM_DEBUG("%d\n", ctx->handle); |
| 364 | if (ctx->handle == -1) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 365 | DRM_DEBUG("Not enough free contexts.\n"); |
| 366 | /* Should this return -EBUSY instead? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return -ENOMEM; |
| 368 | } |
| 369 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 370 | ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 371 | if (!ctx_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | DRM_DEBUG("out of memory\n"); |
| 373 | return -ENOMEM; |
| 374 | } |
| 375 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 376 | INIT_LIST_HEAD(&ctx_entry->head); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 377 | ctx_entry->handle = ctx->handle; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 378 | ctx_entry->tag = file_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 380 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 381 | list_add(&ctx_entry->head, &dev->ctxlist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | ++dev->ctx_count; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 383 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | /** |
| 389 | * Get context. |
| 390 | * |
| 391 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 392 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | * \param cmd command. |
| 394 | * \param arg user argument pointing to a drm_ctx structure. |
| 395 | * \return zero on success or a negative number on failure. |
| 396 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 397 | int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 399 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 401 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 402 | return -EINVAL; |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | /* This is 0, because we don't handle any context flags */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 405 | ctx->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Switch context. |
| 412 | * |
| 413 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 414 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | * \param cmd command. |
| 416 | * \param arg user argument pointing to a drm_ctx structure. |
| 417 | * \return zero on success or a negative number on failure. |
| 418 | * |
| 419 | * Calls context_switch(). |
| 420 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 421 | int drm_switchctx(struct drm_device *dev, void *data, |
| 422 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 424 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 426 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 427 | return -EINVAL; |
| 428 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 429 | DRM_DEBUG("%d\n", ctx->handle); |
| 430 | return drm_context_switch(dev, dev->last_context, ctx->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * New context. |
| 435 | * |
| 436 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 437 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | * \param cmd command. |
| 439 | * \param arg user argument pointing to a drm_ctx structure. |
| 440 | * \return zero on success or a negative number on failure. |
| 441 | * |
| 442 | * Calls context_switch_complete(). |
| 443 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 444 | int drm_newctx(struct drm_device *dev, void *data, |
| 445 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 447 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 449 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 450 | return -EINVAL; |
| 451 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 452 | DRM_DEBUG("%d\n", ctx->handle); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 453 | drm_context_switch_complete(dev, file_priv, ctx->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Remove context. |
| 460 | * |
| 461 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 462 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * \param cmd command. |
| 464 | * \param arg user argument pointing to a drm_ctx structure. |
| 465 | * \return zero on success or a negative number on failure. |
| 466 | * |
| 467 | * If not the special kernel context, calls ctxbitmap_free() to free the specified context. |
| 468 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 469 | int drm_rmctx(struct drm_device *dev, void *data, |
| 470 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 472 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Daniel Vetter | 7c51013 | 2013-08-08 15:41:21 +0200 | [diff] [blame] | 474 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 475 | return -EINVAL; |
| 476 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 477 | DRM_DEBUG("%d\n", ctx->handle); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 478 | if (ctx->handle != DRM_KERNEL_CONTEXT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (dev->driver->context_dtor) |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 480 | dev->driver->context_dtor(dev, ctx->handle); |
| 481 | drm_ctxbitmap_free(dev, ctx->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 484 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 485 | if (!list_empty(&dev->ctxlist)) { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 486 | struct drm_ctx_list *pos, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 488 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 489 | if (pos->handle == ctx->handle) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 490 | list_del(&pos->head); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 491 | kfree(pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | --dev->ctx_count; |
| 493 | } |
| 494 | } |
| 495 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 496 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | /*@}*/ |