drm_context.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /**
  2. * \file drm_context.c
  3. * IOCTLs for generic contexts
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
  10. *
  11. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. /*
  35. * ChangeLog:
  36. * 2001-11-16 Torsten Duwe <duwe@caldera.de>
  37. * added context constructor/destructor hooks,
  38. * needed by SiS driver's memory management.
  39. */
  40. #include <drm/drmP.h>
  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_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
  55. {
  56. mutex_lock(&dev->struct_mutex);
  57. idr_remove(&dev->ctx_idr, ctx_handle);
  58. mutex_unlock(&dev->struct_mutex);
  59. }
  60. /**
  61. * Context bitmap allocation.
  62. *
  63. * \param dev DRM device.
  64. * \return (non-negative) context handle on success or a negative number on failure.
  65. *
  66. * Allocate a new idr from drm_device::ctx_idr while holding the
  67. * drm_device::struct_mutex lock.
  68. */
  69. static int drm_ctxbitmap_next(struct drm_device * dev)
  70. {
  71. int ret;
  72. mutex_lock(&dev->struct_mutex);
  73. ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
  74. GFP_KERNEL);
  75. mutex_unlock(&dev->struct_mutex);
  76. return ret;
  77. }
  78. /**
  79. * Context bitmap initialization.
  80. *
  81. * \param dev DRM device.
  82. *
  83. * Initialise the drm_device::ctx_idr
  84. */
  85. int drm_ctxbitmap_init(struct drm_device * dev)
  86. {
  87. idr_init(&dev->ctx_idr);
  88. return 0;
  89. }
  90. /**
  91. * Context bitmap cleanup.
  92. *
  93. * \param dev DRM device.
  94. *
  95. * Free all idr members using drm_ctx_sarea_free helper function
  96. * while holding the drm_device::struct_mutex lock.
  97. */
  98. void drm_ctxbitmap_cleanup(struct drm_device * dev)
  99. {
  100. mutex_lock(&dev->struct_mutex);
  101. idr_destroy(&dev->ctx_idr);
  102. mutex_unlock(&dev->struct_mutex);
  103. }
  104. /**
  105. * drm_ctxbitmap_flush() - Flush all contexts owned by a file
  106. * @dev: DRM device to operate on
  107. * @file: Open file to flush contexts for
  108. *
  109. * This iterates over all contexts on @dev and drops them if they're owned by
  110. * @file. Note that after this call returns, new contexts might be added if
  111. * the file is still alive.
  112. */
  113. void drm_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
  114. {
  115. struct drm_ctx_list *pos, *tmp;
  116. mutex_lock(&dev->ctxlist_mutex);
  117. list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
  118. if (pos->tag == file &&
  119. pos->handle != DRM_KERNEL_CONTEXT) {
  120. if (dev->driver->context_dtor)
  121. dev->driver->context_dtor(dev, pos->handle);
  122. drm_ctxbitmap_free(dev, pos->handle);
  123. list_del(&pos->head);
  124. kfree(pos);
  125. }
  126. }
  127. mutex_unlock(&dev->ctxlist_mutex);
  128. }
  129. /*@}*/
  130. /******************************************************************/
  131. /** \name Per Context SAREA Support */
  132. /*@{*/
  133. /**
  134. * Get per-context SAREA.
  135. *
  136. * \param inode device inode.
  137. * \param file_priv DRM file private.
  138. * \param cmd command.
  139. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  140. * \return zero on success or a negative number on failure.
  141. *
  142. * Gets the map from drm_device::ctx_idr with the handle specified and
  143. * returns its handle.
  144. */
  145. int drm_getsareactx(struct drm_device *dev, void *data,
  146. struct drm_file *file_priv)
  147. {
  148. struct drm_ctx_priv_map *request = data;
  149. struct drm_local_map *map;
  150. struct drm_map_list *_entry;
  151. mutex_lock(&dev->struct_mutex);
  152. map = idr_find(&dev->ctx_idr, request->ctx_id);
  153. if (!map) {
  154. mutex_unlock(&dev->struct_mutex);
  155. return -EINVAL;
  156. }
  157. request->handle = NULL;
  158. list_for_each_entry(_entry, &dev->maplist, head) {
  159. if (_entry->map == map) {
  160. request->handle =
  161. (void *)(unsigned long)_entry->user_token;
  162. break;
  163. }
  164. }
  165. mutex_unlock(&dev->struct_mutex);
  166. if (request->handle == NULL)
  167. return -EINVAL;
  168. return 0;
  169. }
  170. /**
  171. * Set per-context SAREA.
  172. *
  173. * \param inode device inode.
  174. * \param file_priv DRM file private.
  175. * \param cmd command.
  176. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  177. * \return zero on success or a negative number on failure.
  178. *
  179. * Searches the mapping specified in \p arg and update the entry in
  180. * drm_device::ctx_idr with it.
  181. */
  182. int drm_setsareactx(struct drm_device *dev, void *data,
  183. struct drm_file *file_priv)
  184. {
  185. struct drm_ctx_priv_map *request = data;
  186. struct drm_local_map *map = NULL;
  187. struct drm_map_list *r_list = NULL;
  188. mutex_lock(&dev->struct_mutex);
  189. list_for_each_entry(r_list, &dev->maplist, head) {
  190. if (r_list->map
  191. && r_list->user_token == (unsigned long) request->handle)
  192. goto found;
  193. }
  194. bad:
  195. mutex_unlock(&dev->struct_mutex);
  196. return -EINVAL;
  197. found:
  198. map = r_list->map;
  199. if (!map)
  200. goto bad;
  201. if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
  202. goto bad;
  203. mutex_unlock(&dev->struct_mutex);
  204. return 0;
  205. }
  206. /*@}*/
  207. /******************************************************************/
  208. /** \name The actual DRM context handling routines */
  209. /*@{*/
  210. /**
  211. * Switch context.
  212. *
  213. * \param dev DRM device.
  214. * \param old old context handle.
  215. * \param new new context handle.
  216. * \return zero on success or a negative number on failure.
  217. *
  218. * Attempt to set drm_device::context_flag.
  219. */
  220. static int drm_context_switch(struct drm_device * dev, int old, int new)
  221. {
  222. if (test_and_set_bit(0, &dev->context_flag)) {
  223. DRM_ERROR("Reentering -- FIXME\n");
  224. return -EBUSY;
  225. }
  226. DRM_DEBUG("Context switch from %d to %d\n", old, new);
  227. if (new == dev->last_context) {
  228. clear_bit(0, &dev->context_flag);
  229. return 0;
  230. }
  231. return 0;
  232. }
  233. /**
  234. * Complete context switch.
  235. *
  236. * \param dev DRM device.
  237. * \param new new context handle.
  238. * \return zero on success or a negative number on failure.
  239. *
  240. * Updates drm_device::last_context and drm_device::last_switch. Verifies the
  241. * hardware lock is held, clears the drm_device::context_flag and wakes up
  242. * drm_device::context_wait.
  243. */
  244. static int drm_context_switch_complete(struct drm_device *dev,
  245. struct drm_file *file_priv, int new)
  246. {
  247. dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
  248. if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
  249. DRM_ERROR("Lock isn't held after context switch\n");
  250. }
  251. /* If a context switch is ever initiated
  252. when the kernel holds the lock, release
  253. that lock here. */
  254. clear_bit(0, &dev->context_flag);
  255. return 0;
  256. }
  257. /**
  258. * Reserve contexts.
  259. *
  260. * \param inode device inode.
  261. * \param file_priv DRM file private.
  262. * \param cmd command.
  263. * \param arg user argument pointing to a drm_ctx_res structure.
  264. * \return zero on success or a negative number on failure.
  265. */
  266. int drm_resctx(struct drm_device *dev, void *data,
  267. struct drm_file *file_priv)
  268. {
  269. struct drm_ctx_res *res = data;
  270. struct drm_ctx ctx;
  271. int i;
  272. if (res->count >= DRM_RESERVED_CONTEXTS) {
  273. memset(&ctx, 0, sizeof(ctx));
  274. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  275. ctx.handle = i;
  276. if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
  277. return -EFAULT;
  278. }
  279. }
  280. res->count = DRM_RESERVED_CONTEXTS;
  281. return 0;
  282. }
  283. /**
  284. * Add context.
  285. *
  286. * \param inode device inode.
  287. * \param file_priv DRM file private.
  288. * \param cmd command.
  289. * \param arg user argument pointing to a drm_ctx structure.
  290. * \return zero on success or a negative number on failure.
  291. *
  292. * Get a new handle for the context and copy to userspace.
  293. */
  294. int drm_addctx(struct drm_device *dev, void *data,
  295. struct drm_file *file_priv)
  296. {
  297. struct drm_ctx_list *ctx_entry;
  298. struct drm_ctx *ctx = data;
  299. ctx->handle = drm_ctxbitmap_next(dev);
  300. if (ctx->handle == DRM_KERNEL_CONTEXT) {
  301. /* Skip kernel's context and get a new one. */
  302. ctx->handle = drm_ctxbitmap_next(dev);
  303. }
  304. DRM_DEBUG("%d\n", ctx->handle);
  305. if (ctx->handle == -1) {
  306. DRM_DEBUG("Not enough free contexts.\n");
  307. /* Should this return -EBUSY instead? */
  308. return -ENOMEM;
  309. }
  310. ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
  311. if (!ctx_entry) {
  312. DRM_DEBUG("out of memory\n");
  313. return -ENOMEM;
  314. }
  315. INIT_LIST_HEAD(&ctx_entry->head);
  316. ctx_entry->handle = ctx->handle;
  317. ctx_entry->tag = file_priv;
  318. mutex_lock(&dev->ctxlist_mutex);
  319. list_add(&ctx_entry->head, &dev->ctxlist);
  320. mutex_unlock(&dev->ctxlist_mutex);
  321. return 0;
  322. }
  323. /**
  324. * Get context.
  325. *
  326. * \param inode device inode.
  327. * \param file_priv DRM file private.
  328. * \param cmd command.
  329. * \param arg user argument pointing to a drm_ctx structure.
  330. * \return zero on success or a negative number on failure.
  331. */
  332. int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
  333. {
  334. struct drm_ctx *ctx = data;
  335. /* This is 0, because we don't handle any context flags */
  336. ctx->flags = 0;
  337. return 0;
  338. }
  339. /**
  340. * Switch context.
  341. *
  342. * \param inode device inode.
  343. * \param file_priv DRM file private.
  344. * \param cmd command.
  345. * \param arg user argument pointing to a drm_ctx structure.
  346. * \return zero on success or a negative number on failure.
  347. *
  348. * Calls context_switch().
  349. */
  350. int drm_switchctx(struct drm_device *dev, void *data,
  351. struct drm_file *file_priv)
  352. {
  353. struct drm_ctx *ctx = data;
  354. DRM_DEBUG("%d\n", ctx->handle);
  355. return drm_context_switch(dev, dev->last_context, ctx->handle);
  356. }
  357. /**
  358. * New context.
  359. *
  360. * \param inode device inode.
  361. * \param file_priv DRM file private.
  362. * \param cmd command.
  363. * \param arg user argument pointing to a drm_ctx structure.
  364. * \return zero on success or a negative number on failure.
  365. *
  366. * Calls context_switch_complete().
  367. */
  368. int drm_newctx(struct drm_device *dev, void *data,
  369. struct drm_file *file_priv)
  370. {
  371. struct drm_ctx *ctx = data;
  372. DRM_DEBUG("%d\n", ctx->handle);
  373. drm_context_switch_complete(dev, file_priv, ctx->handle);
  374. return 0;
  375. }
  376. /**
  377. * Remove context.
  378. *
  379. * \param inode device inode.
  380. * \param file_priv DRM file private.
  381. * \param cmd command.
  382. * \param arg user argument pointing to a drm_ctx structure.
  383. * \return zero on success or a negative number on failure.
  384. *
  385. * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
  386. */
  387. int drm_rmctx(struct drm_device *dev, void *data,
  388. struct drm_file *file_priv)
  389. {
  390. struct drm_ctx *ctx = data;
  391. DRM_DEBUG("%d\n", ctx->handle);
  392. if (ctx->handle != DRM_KERNEL_CONTEXT) {
  393. if (dev->driver->context_dtor)
  394. dev->driver->context_dtor(dev, ctx->handle);
  395. drm_ctxbitmap_free(dev, ctx->handle);
  396. }
  397. mutex_lock(&dev->ctxlist_mutex);
  398. if (!list_empty(&dev->ctxlist)) {
  399. struct drm_ctx_list *pos, *n;
  400. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  401. if (pos->handle == ctx->handle) {
  402. list_del(&pos->head);
  403. kfree(pos);
  404. }
  405. }
  406. }
  407. mutex_unlock(&dev->ctxlist_mutex);
  408. return 0;
  409. }
  410. /*@}*/