drm_context.c 11 KB

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