Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 2 | * Legacy: Generic DRM Contexts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. |
| 5 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 6 | * All Rights Reserved. |
| 7 | * |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 8 | * Author: Rickard E. (Rik) Faith <faith@valinux.com> |
| 9 | * Author: Gareth Hughes <gareth@valinux.com> |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 12 | * copy of this software and associated documentation files (the "Software"), |
| 13 | * to deal in the Software without restriction, including without limitation |
| 14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 15 | * and/or sell copies of the Software, and to permit persons to whom the |
| 16 | * Software is furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice (including the next |
| 19 | * paragraph) shall be included in all copies or substantial portions of the |
| 20 | * Software. |
| 21 | * |
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 25 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 26 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 27 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | * OTHER DEALINGS IN THE SOFTWARE. |
| 29 | */ |
| 30 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 31 | #include <drm/drmP.h> |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 32 | #include "drm_legacy.h" |
| 33 | |
| 34 | struct drm_ctx_list { |
| 35 | struct list_head head; |
| 36 | drm_context_t handle; |
| 37 | struct drm_file *tag; |
| 38 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Dave Airlie | c21eb21 | 2013-09-20 08:32:59 +1000 | [diff] [blame] | 40 | /******************************************************************/ |
| 41 | /** \name Context bitmap support */ |
| 42 | /*@{*/ |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Free a handle from the context bitmap. |
| 46 | * |
| 47 | * \param dev DRM device. |
| 48 | * \param ctx_handle context handle. |
| 49 | * |
| 50 | * 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] | 51 | * in drm_device::ctx_idr, while holding the drm_device::struct_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * lock. |
| 53 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 54 | void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 56 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 57 | 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 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 65 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * Context bitmap allocation. |
| 67 | * |
| 68 | * \param dev DRM device. |
| 69 | * \return (non-negative) context handle on success or a negative number on failure. |
| 70 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 71 | * Allocate a new idr from drm_device::ctx_idr while holding the |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 72 | * drm_device::struct_mutex lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 74 | static int drm_legacy_ctxbitmap_next(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 76 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 78 | mutex_lock(&dev->struct_mutex); |
Tejun Heo | 2e92881 | 2013-02-27 17:04:08 -0800 | [diff] [blame] | 79 | ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0, |
| 80 | GFP_KERNEL); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 81 | mutex_unlock(&dev->struct_mutex); |
Tejun Heo | 2e92881 | 2013-02-27 17:04:08 -0800 | [diff] [blame] | 82 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Context bitmap initialization. |
| 87 | * |
| 88 | * \param dev DRM device. |
| 89 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 90 | * Initialise the drm_device::ctx_idr |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 92 | int drm_legacy_ctxbitmap_init(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 94 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 95 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 96 | return -EINVAL; |
| 97 | |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 98 | idr_init(&dev->ctx_idr); |
Dave Airlie | c21eb21 | 2013-09-20 08:32:59 +1000 | [diff] [blame] | 99 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Context bitmap cleanup. |
| 104 | * |
| 105 | * \param dev DRM device. |
| 106 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 107 | * Free all idr members using drm_ctx_sarea_free helper function |
| 108 | * while holding the drm_device::struct_mutex lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 110 | void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 112 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 113 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 114 | return; |
| 115 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 116 | mutex_lock(&dev->struct_mutex); |
Tejun Heo | 4d53233 | 2013-02-27 17:03:39 -0800 | [diff] [blame] | 117 | idr_destroy(&dev->ctx_idr); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 118 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
David Herrmann | 9f8d21e | 2014-07-23 09:01:12 +0200 | [diff] [blame] | 121 | /** |
| 122 | * drm_ctxbitmap_flush() - Flush all contexts owned by a file |
| 123 | * @dev: DRM device to operate on |
| 124 | * @file: Open file to flush contexts for |
| 125 | * |
| 126 | * This iterates over all contexts on @dev and drops them if they're owned by |
| 127 | * @file. Note that after this call returns, new contexts might be added if |
| 128 | * the file is still alive. |
| 129 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 130 | void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file) |
David Herrmann | 9f8d21e | 2014-07-23 09:01:12 +0200 | [diff] [blame] | 131 | { |
| 132 | struct drm_ctx_list *pos, *tmp; |
| 133 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 134 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 135 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 136 | return; |
| 137 | |
David Herrmann | 9f8d21e | 2014-07-23 09:01:12 +0200 | [diff] [blame] | 138 | mutex_lock(&dev->ctxlist_mutex); |
| 139 | |
| 140 | list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) { |
| 141 | if (pos->tag == file && |
| 142 | pos->handle != DRM_KERNEL_CONTEXT) { |
| 143 | if (dev->driver->context_dtor) |
| 144 | dev->driver->context_dtor(dev, pos->handle); |
| 145 | |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 146 | drm_legacy_ctxbitmap_free(dev, pos->handle); |
David Herrmann | 9f8d21e | 2014-07-23 09:01:12 +0200 | [diff] [blame] | 147 | list_del(&pos->head); |
| 148 | kfree(pos); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | mutex_unlock(&dev->ctxlist_mutex); |
| 153 | } |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /*@}*/ |
| 156 | |
| 157 | /******************************************************************/ |
| 158 | /** \name Per Context SAREA Support */ |
| 159 | /*@{*/ |
| 160 | |
| 161 | /** |
| 162 | * Get per-context SAREA. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 163 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 165 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | * \param cmd command. |
| 167 | * \param arg user argument pointing to a drm_ctx_priv_map structure. |
| 168 | * \return zero on success or a negative number on failure. |
| 169 | * |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 170 | * 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] | 171 | * returns its handle. |
| 172 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 173 | int drm_legacy_getsareactx(struct drm_device *dev, void *data, |
| 174 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 176 | struct drm_ctx_priv_map *request = data; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 177 | struct drm_local_map *map; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 178 | struct drm_map_list *_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 180 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 181 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 182 | return -EINVAL; |
| 183 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 184 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 185 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 186 | map = idr_find(&dev->ctx_idr, request->ctx_id); |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 187 | if (!map) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 188 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return -EINVAL; |
| 190 | } |
| 191 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 192 | request->handle = NULL; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 193 | list_for_each_entry(_entry, &dev->maplist, head) { |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 194 | if (_entry->map == map) { |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 195 | request->handle = |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 196 | (void *)(unsigned long)_entry->user_token; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | } |
Ilija Hadzic | 09b4ea4 | 2011-10-28 17:43:28 -0400 | [diff] [blame] | 200 | |
| 201 | mutex_unlock(&dev->struct_mutex); |
| 202 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 203 | if (request->handle == NULL) |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Set per-context SAREA. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 211 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 213 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * \param cmd command. |
| 215 | * \param arg user argument pointing to a drm_ctx_priv_map structure. |
| 216 | * \return zero on success or a negative number on failure. |
| 217 | * |
| 218 | * Searches the mapping specified in \p arg and update the entry in |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 219 | * drm_device::ctx_idr with it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 221 | int drm_legacy_setsareactx(struct drm_device *dev, void *data, |
| 222 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 224 | struct drm_ctx_priv_map *request = data; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 225 | struct drm_local_map *map = NULL; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 226 | struct drm_map_list *r_list = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 228 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 229 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 230 | return -EINVAL; |
| 231 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 232 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 233 | list_for_each_entry(r_list, &dev->maplist, head) { |
Dave Airlie | 9a18664 | 2005-06-23 21:29:18 +1000 | [diff] [blame] | 234 | if (r_list->map |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 235 | && r_list->user_token == (unsigned long) request->handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | goto found; |
| 237 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 238 | bad: |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 239 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return -EINVAL; |
| 241 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 242 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | map = r_list->map; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 244 | if (!map) |
| 245 | goto bad; |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 246 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 247 | if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | goto bad; |
Dave Airlie | 6296814 | 2007-07-17 10:46:52 +1000 | [diff] [blame] | 249 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 250 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /*@}*/ |
| 256 | |
| 257 | /******************************************************************/ |
| 258 | /** \name The actual DRM context handling routines */ |
| 259 | /*@{*/ |
| 260 | |
| 261 | /** |
| 262 | * Switch context. |
| 263 | * |
| 264 | * \param dev DRM device. |
| 265 | * \param old old context handle. |
| 266 | * \param new new context handle. |
| 267 | * \return zero on success or a negative number on failure. |
| 268 | * |
| 269 | * Attempt to set drm_device::context_flag. |
| 270 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 271 | 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] | 272 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 273 | if (test_and_set_bit(0, &dev->context_flag)) { |
| 274 | DRM_ERROR("Reentering -- FIXME\n"); |
| 275 | return -EBUSY; |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 278 | DRM_DEBUG("Context switch from %d to %d\n", old, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 280 | if (new == dev->last_context) { |
| 281 | clear_bit(0, &dev->context_flag); |
| 282 | return 0; |
| 283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 285 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Complete context switch. |
| 290 | * |
| 291 | * \param dev DRM device. |
| 292 | * \param new new context handle. |
| 293 | * \return zero on success or a negative number on failure. |
| 294 | * |
| 295 | * Updates drm_device::last_context and drm_device::last_switch. Verifies the |
| 296 | * hardware lock is held, clears the drm_device::context_flag and wakes up |
| 297 | * drm_device::context_wait. |
| 298 | */ |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 299 | static int drm_context_switch_complete(struct drm_device *dev, |
| 300 | struct drm_file *file_priv, int new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 302 | dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 304 | if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 305 | DRM_ERROR("Lock isn't held after context switch\n"); |
| 306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 308 | /* If a context switch is ever initiated |
| 309 | when the kernel holds the lock, release |
| 310 | that lock here. */ |
| 311 | clear_bit(0, &dev->context_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 313 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Reserve contexts. |
| 318 | * |
| 319 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 320 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | * \param cmd command. |
| 322 | * \param arg user argument pointing to a drm_ctx_res structure. |
| 323 | * \return zero on success or a negative number on failure. |
| 324 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 325 | int drm_legacy_resctx(struct drm_device *dev, void *data, |
| 326 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 328 | struct drm_ctx_res *res = data; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 329 | struct drm_ctx ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | int i; |
| 331 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 332 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 333 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 334 | return -EINVAL; |
| 335 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 336 | if (res->count >= DRM_RESERVED_CONTEXTS) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 337 | memset(&ctx, 0, sizeof(ctx)); |
| 338 | for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | ctx.handle = i; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 340 | if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return -EFAULT; |
| 342 | } |
| 343 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 344 | res->count = DRM_RESERVED_CONTEXTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Add context. |
| 351 | * |
| 352 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 353 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | * \param cmd command. |
| 355 | * \param arg user argument pointing to a drm_ctx structure. |
| 356 | * \return zero on success or a negative number on failure. |
| 357 | * |
| 358 | * Get a new handle for the context and copy to userspace. |
| 359 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 360 | int drm_legacy_addctx(struct drm_device *dev, void *data, |
| 361 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 363 | struct drm_ctx_list *ctx_entry; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 364 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 366 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 367 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 368 | return -EINVAL; |
| 369 | |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 370 | ctx->handle = drm_legacy_ctxbitmap_next(dev); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 371 | if (ctx->handle == DRM_KERNEL_CONTEXT) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 372 | /* Skip kernel's context and get a new one. */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 373 | ctx->handle = drm_legacy_ctxbitmap_next(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 375 | DRM_DEBUG("%d\n", ctx->handle); |
| 376 | if (ctx->handle == -1) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 377 | DRM_DEBUG("Not enough free contexts.\n"); |
| 378 | /* Should this return -EBUSY instead? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | return -ENOMEM; |
| 380 | } |
| 381 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 382 | ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 383 | if (!ctx_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | DRM_DEBUG("out of memory\n"); |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 388 | INIT_LIST_HEAD(&ctx_entry->head); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 389 | ctx_entry->handle = ctx->handle; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 390 | ctx_entry->tag = file_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 392 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 393 | list_add(&ctx_entry->head, &dev->ctxlist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 394 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | /** |
| 400 | * Get context. |
| 401 | * |
| 402 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 403 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | * \param cmd command. |
| 405 | * \param arg user argument pointing to a drm_ctx structure. |
| 406 | * \return zero on success or a negative number on failure. |
| 407 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 408 | int drm_legacy_getctx(struct drm_device *dev, void *data, |
| 409 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 411 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 413 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 414 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 415 | return -EINVAL; |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /* This is 0, because we don't handle any context flags */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 418 | ctx->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Switch context. |
| 425 | * |
| 426 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 427 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * \param cmd command. |
| 429 | * \param arg user argument pointing to a drm_ctx structure. |
| 430 | * \return zero on success or a negative number on failure. |
| 431 | * |
| 432 | * Calls context_switch(). |
| 433 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 434 | int drm_legacy_switchctx(struct drm_device *dev, void *data, |
| 435 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 437 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 439 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 440 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 441 | return -EINVAL; |
| 442 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 443 | DRM_DEBUG("%d\n", ctx->handle); |
| 444 | return drm_context_switch(dev, dev->last_context, ctx->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
| 448 | * New context. |
| 449 | * |
| 450 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 451 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | * \param cmd command. |
| 453 | * \param arg user argument pointing to a drm_ctx structure. |
| 454 | * \return zero on success or a negative number on failure. |
| 455 | * |
| 456 | * Calls context_switch_complete(). |
| 457 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 458 | int drm_legacy_newctx(struct drm_device *dev, void *data, |
| 459 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 461 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 463 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 464 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 465 | return -EINVAL; |
| 466 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 467 | DRM_DEBUG("%d\n", ctx->handle); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 468 | drm_context_switch_complete(dev, file_priv, ctx->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Remove context. |
| 475 | * |
| 476 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 477 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | * \param cmd command. |
| 479 | * \param arg user argument pointing to a drm_ctx structure. |
| 480 | * \return zero on success or a negative number on failure. |
| 481 | * |
| 482 | * If not the special kernel context, calls ctxbitmap_free() to free the specified context. |
| 483 | */ |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 484 | int drm_legacy_rmctx(struct drm_device *dev, void *data, |
| 485 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 487 | struct drm_ctx *ctx = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Peter Antoine | 0e97598 | 2015-06-23 08:18:49 +0100 | [diff] [blame^] | 489 | if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) && |
| 490 | drm_core_check_feature(dev, DRIVER_MODESET)) |
| 491 | return -EINVAL; |
| 492 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 493 | DRM_DEBUG("%d\n", ctx->handle); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 494 | if (ctx->handle != DRM_KERNEL_CONTEXT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (dev->driver->context_dtor) |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 496 | dev->driver->context_dtor(dev, ctx->handle); |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 497 | drm_legacy_ctxbitmap_free(dev, ctx->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 500 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 501 | if (!list_empty(&dev->ctxlist)) { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 502 | struct drm_ctx_list *pos, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 504 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 505 | if (pos->handle == ctx->handle) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 506 | list_del(&pos->head); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 507 | kfree(pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 511 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /*@}*/ |