drm_context.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Legacy: Generic DRM Contexts
  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. *
  8. * Author: Rickard E. (Rik) Faith <[email protected]>
  9. * Author: Gareth Hughes <[email protected]>
  10. *
  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. #include <linux/slab.h>
  31. #include <linux/uaccess.h>
  32. #include <drm/drm_drv.h>
  33. #include <drm/drm_file.h>
  34. #include <drm/drm_print.h>
  35. #include "drm_legacy.h"
  36. struct drm_ctx_list {
  37. struct list_head head;
  38. drm_context_t handle;
  39. struct drm_file *tag;
  40. };
  41. /******************************************************************/
  42. /** \name Context bitmap support */
  43. /*@{*/
  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
  51. * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
  52. * lock.
  53. */
  54. void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
  55. {
  56. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  57. !drm_core_check_feature(dev, DRIVER_LEGACY))
  58. return;
  59. mutex_lock(&dev->struct_mutex);
  60. idr_remove(&dev->ctx_idr, ctx_handle);
  61. mutex_unlock(&dev->struct_mutex);
  62. }
  63. /*
  64. * Context bitmap allocation.
  65. *
  66. * \param dev DRM device.
  67. * \return (non-negative) context handle on success or a negative number on failure.
  68. *
  69. * Allocate a new idr from drm_device::ctx_idr while holding the
  70. * drm_device::struct_mutex lock.
  71. */
  72. static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
  73. {
  74. int ret;
  75. mutex_lock(&dev->struct_mutex);
  76. ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
  77. GFP_KERNEL);
  78. mutex_unlock(&dev->struct_mutex);
  79. return ret;
  80. }
  81. /*
  82. * Context bitmap initialization.
  83. *
  84. * \param dev DRM device.
  85. *
  86. * Initialise the drm_device::ctx_idr
  87. */
  88. void drm_legacy_ctxbitmap_init(struct drm_device * dev)
  89. {
  90. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  91. !drm_core_check_feature(dev, DRIVER_LEGACY))
  92. return;
  93. idr_init(&dev->ctx_idr);
  94. }
  95. /*
  96. * Context bitmap cleanup.
  97. *
  98. * \param dev DRM device.
  99. *
  100. * Free all idr members using drm_ctx_sarea_free helper function
  101. * while holding the drm_device::struct_mutex lock.
  102. */
  103. void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
  104. {
  105. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  106. !drm_core_check_feature(dev, DRIVER_LEGACY))
  107. return;
  108. mutex_lock(&dev->struct_mutex);
  109. idr_destroy(&dev->ctx_idr);
  110. mutex_unlock(&dev->struct_mutex);
  111. }
  112. /**
  113. * drm_legacy_ctxbitmap_flush() - Flush all contexts owned by a file
  114. * @dev: DRM device to operate on
  115. * @file: Open file to flush contexts for
  116. *
  117. * This iterates over all contexts on @dev and drops them if they're owned by
  118. * @file. Note that after this call returns, new contexts might be added if
  119. * the file is still alive.
  120. */
  121. void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
  122. {
  123. struct drm_ctx_list *pos, *tmp;
  124. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  125. !drm_core_check_feature(dev, DRIVER_LEGACY))
  126. return;
  127. mutex_lock(&dev->ctxlist_mutex);
  128. list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
  129. if (pos->tag == file &&
  130. pos->handle != DRM_KERNEL_CONTEXT) {
  131. if (dev->driver->context_dtor)
  132. dev->driver->context_dtor(dev, pos->handle);
  133. drm_legacy_ctxbitmap_free(dev, pos->handle);
  134. list_del(&pos->head);
  135. kfree(pos);
  136. }
  137. }
  138. mutex_unlock(&dev->ctxlist_mutex);
  139. }
  140. /*@}*/
  141. /******************************************************************/
  142. /** \name Per Context SAREA Support */
  143. /*@{*/
  144. /*
  145. * Get per-context SAREA.
  146. *
  147. * \param inode device inode.
  148. * \param file_priv DRM file private.
  149. * \param cmd command.
  150. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  151. * \return zero on success or a negative number on failure.
  152. *
  153. * Gets the map from drm_device::ctx_idr with the handle specified and
  154. * returns its handle.
  155. */
  156. int drm_legacy_getsareactx(struct drm_device *dev, void *data,
  157. struct drm_file *file_priv)
  158. {
  159. struct drm_ctx_priv_map *request = data;
  160. struct drm_local_map *map;
  161. struct drm_map_list *_entry;
  162. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  163. !drm_core_check_feature(dev, DRIVER_LEGACY))
  164. return -EOPNOTSUPP;
  165. mutex_lock(&dev->struct_mutex);
  166. map = idr_find(&dev->ctx_idr, request->ctx_id);
  167. if (!map) {
  168. mutex_unlock(&dev->struct_mutex);
  169. return -EINVAL;
  170. }
  171. request->handle = NULL;
  172. list_for_each_entry(_entry, &dev->maplist, head) {
  173. if (_entry->map == map) {
  174. request->handle =
  175. (void *)(unsigned long)_entry->user_token;
  176. break;
  177. }
  178. }
  179. mutex_unlock(&dev->struct_mutex);
  180. if (request->handle == NULL)
  181. return -EINVAL;
  182. return 0;
  183. }
  184. /*
  185. * Set per-context SAREA.
  186. *
  187. * \param inode device inode.
  188. * \param file_priv DRM file private.
  189. * \param cmd command.
  190. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  191. * \return zero on success or a negative number on failure.
  192. *
  193. * Searches the mapping specified in \p arg and update the entry in
  194. * drm_device::ctx_idr with it.
  195. */
  196. int drm_legacy_setsareactx(struct drm_device *dev, void *data,
  197. struct drm_file *file_priv)
  198. {
  199. struct drm_ctx_priv_map *request = data;
  200. struct drm_local_map *map = NULL;
  201. struct drm_map_list *r_list = NULL;
  202. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  203. !drm_core_check_feature(dev, DRIVER_LEGACY))
  204. return -EOPNOTSUPP;
  205. mutex_lock(&dev->struct_mutex);
  206. list_for_each_entry(r_list, &dev->maplist, head) {
  207. if (r_list->map
  208. && r_list->user_token == (unsigned long) request->handle)
  209. goto found;
  210. }
  211. bad:
  212. mutex_unlock(&dev->struct_mutex);
  213. return -EINVAL;
  214. found:
  215. map = r_list->map;
  216. if (!map)
  217. goto bad;
  218. if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
  219. goto bad;
  220. mutex_unlock(&dev->struct_mutex);
  221. return 0;
  222. }
  223. /*@}*/
  224. /******************************************************************/
  225. /** \name The actual DRM context handling routines */
  226. /*@{*/
  227. /*
  228. * Switch context.
  229. *
  230. * \param dev DRM device.
  231. * \param old old context handle.
  232. * \param new new context handle.
  233. * \return zero on success or a negative number on failure.
  234. *
  235. * Attempt to set drm_device::context_flag.
  236. */
  237. static int drm_context_switch(struct drm_device * dev, int old, int new)
  238. {
  239. if (test_and_set_bit(0, &dev->context_flag)) {
  240. DRM_ERROR("Reentering -- FIXME\n");
  241. return -EBUSY;
  242. }
  243. DRM_DEBUG("Context switch from %d to %d\n", old, new);
  244. if (new == dev->last_context) {
  245. clear_bit(0, &dev->context_flag);
  246. return 0;
  247. }
  248. return 0;
  249. }
  250. /*
  251. * Complete context switch.
  252. *
  253. * \param dev DRM device.
  254. * \param new new context handle.
  255. * \return zero on success or a negative number on failure.
  256. *
  257. * Updates drm_device::last_context and drm_device::last_switch. Verifies the
  258. * hardware lock is held, clears the drm_device::context_flag and wakes up
  259. * drm_device::context_wait.
  260. */
  261. static int drm_context_switch_complete(struct drm_device *dev,
  262. struct drm_file *file_priv, int new)
  263. {
  264. dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
  265. if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
  266. DRM_ERROR("Lock isn't held after context switch\n");
  267. }
  268. /* If a context switch is ever initiated
  269. when the kernel holds the lock, release
  270. that lock here.
  271. */
  272. clear_bit(0, &dev->context_flag);
  273. return 0;
  274. }
  275. /*
  276. * Reserve contexts.
  277. *
  278. * \param inode device inode.
  279. * \param file_priv DRM file private.
  280. * \param cmd command.
  281. * \param arg user argument pointing to a drm_ctx_res structure.
  282. * \return zero on success or a negative number on failure.
  283. */
  284. int drm_legacy_resctx(struct drm_device *dev, void *data,
  285. struct drm_file *file_priv)
  286. {
  287. struct drm_ctx_res *res = data;
  288. struct drm_ctx ctx;
  289. int i;
  290. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  291. !drm_core_check_feature(dev, DRIVER_LEGACY))
  292. return -EOPNOTSUPP;
  293. if (res->count >= DRM_RESERVED_CONTEXTS) {
  294. memset(&ctx, 0, sizeof(ctx));
  295. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  296. ctx.handle = i;
  297. if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
  298. return -EFAULT;
  299. }
  300. }
  301. res->count = DRM_RESERVED_CONTEXTS;
  302. return 0;
  303. }
  304. /*
  305. * Add context.
  306. *
  307. * \param inode device inode.
  308. * \param file_priv DRM file private.
  309. * \param cmd command.
  310. * \param arg user argument pointing to a drm_ctx structure.
  311. * \return zero on success or a negative number on failure.
  312. *
  313. * Get a new handle for the context and copy to userspace.
  314. */
  315. int drm_legacy_addctx(struct drm_device *dev, void *data,
  316. struct drm_file *file_priv)
  317. {
  318. struct drm_ctx_list *ctx_entry;
  319. struct drm_ctx *ctx = data;
  320. int tmp_handle;
  321. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  322. !drm_core_check_feature(dev, DRIVER_LEGACY))
  323. return -EOPNOTSUPP;
  324. tmp_handle = drm_legacy_ctxbitmap_next(dev);
  325. if (tmp_handle == DRM_KERNEL_CONTEXT) {
  326. /* Skip kernel's context and get a new one. */
  327. tmp_handle = drm_legacy_ctxbitmap_next(dev);
  328. }
  329. DRM_DEBUG("%d\n", tmp_handle);
  330. if (tmp_handle < 0) {
  331. DRM_DEBUG("Not enough free contexts.\n");
  332. /* Should this return -EBUSY instead? */
  333. return tmp_handle;
  334. }
  335. ctx->handle = tmp_handle;
  336. ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
  337. if (!ctx_entry) {
  338. DRM_DEBUG("out of memory\n");
  339. return -ENOMEM;
  340. }
  341. INIT_LIST_HEAD(&ctx_entry->head);
  342. ctx_entry->handle = ctx->handle;
  343. ctx_entry->tag = file_priv;
  344. mutex_lock(&dev->ctxlist_mutex);
  345. list_add(&ctx_entry->head, &dev->ctxlist);
  346. mutex_unlock(&dev->ctxlist_mutex);
  347. return 0;
  348. }
  349. /*
  350. * Get context.
  351. *
  352. * \param inode device inode.
  353. * \param file_priv DRM file private.
  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. int drm_legacy_getctx(struct drm_device *dev, void *data,
  359. struct drm_file *file_priv)
  360. {
  361. struct drm_ctx *ctx = data;
  362. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  363. !drm_core_check_feature(dev, DRIVER_LEGACY))
  364. return -EOPNOTSUPP;
  365. /* This is 0, because we don't handle any context flags */
  366. ctx->flags = 0;
  367. return 0;
  368. }
  369. /*
  370. * Switch context.
  371. *
  372. * \param inode device inode.
  373. * \param file_priv DRM file private.
  374. * \param cmd command.
  375. * \param arg user argument pointing to a drm_ctx structure.
  376. * \return zero on success or a negative number on failure.
  377. *
  378. * Calls context_switch().
  379. */
  380. int drm_legacy_switchctx(struct drm_device *dev, void *data,
  381. struct drm_file *file_priv)
  382. {
  383. struct drm_ctx *ctx = data;
  384. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  385. !drm_core_check_feature(dev, DRIVER_LEGACY))
  386. return -EOPNOTSUPP;
  387. DRM_DEBUG("%d\n", ctx->handle);
  388. return drm_context_switch(dev, dev->last_context, ctx->handle);
  389. }
  390. /*
  391. * New context.
  392. *
  393. * \param inode device inode.
  394. * \param file_priv DRM file private.
  395. * \param cmd command.
  396. * \param arg user argument pointing to a drm_ctx structure.
  397. * \return zero on success or a negative number on failure.
  398. *
  399. * Calls context_switch_complete().
  400. */
  401. int drm_legacy_newctx(struct drm_device *dev, void *data,
  402. struct drm_file *file_priv)
  403. {
  404. struct drm_ctx *ctx = data;
  405. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  406. !drm_core_check_feature(dev, DRIVER_LEGACY))
  407. return -EOPNOTSUPP;
  408. DRM_DEBUG("%d\n", ctx->handle);
  409. drm_context_switch_complete(dev, file_priv, ctx->handle);
  410. return 0;
  411. }
  412. /*
  413. * Remove context.
  414. *
  415. * \param inode device inode.
  416. * \param file_priv DRM file private.
  417. * \param cmd command.
  418. * \param arg user argument pointing to a drm_ctx structure.
  419. * \return zero on success or a negative number on failure.
  420. *
  421. * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
  422. */
  423. int drm_legacy_rmctx(struct drm_device *dev, void *data,
  424. struct drm_file *file_priv)
  425. {
  426. struct drm_ctx *ctx = data;
  427. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  428. !drm_core_check_feature(dev, DRIVER_LEGACY))
  429. return -EOPNOTSUPP;
  430. DRM_DEBUG("%d\n", ctx->handle);
  431. if (ctx->handle != DRM_KERNEL_CONTEXT) {
  432. if (dev->driver->context_dtor)
  433. dev->driver->context_dtor(dev, ctx->handle);
  434. drm_legacy_ctxbitmap_free(dev, ctx->handle);
  435. }
  436. mutex_lock(&dev->ctxlist_mutex);
  437. if (!list_empty(&dev->ctxlist)) {
  438. struct drm_ctx_list *pos, *n;
  439. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  440. if (pos->handle == ctx->handle) {
  441. list_del(&pos->head);
  442. kfree(pos);
  443. }
  444. }
  445. }
  446. mutex_unlock(&dev->ctxlist_mutex);
  447. return 0;
  448. }
  449. /*@}*/