drm_context.c 13 KB

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