drm_context.c 12 KB

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