drm_context.c 11 KB

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