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