drm_context.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. wake_up(&dev->context_wait);
  232. return 0;
  233. }
  234. /**
  235. * Reserve contexts.
  236. *
  237. * \param inode device inode.
  238. * \param file_priv DRM file private.
  239. * \param cmd command.
  240. * \param arg user argument pointing to a drm_ctx_res structure.
  241. * \return zero on success or a negative number on failure.
  242. */
  243. int drm_resctx(struct drm_device *dev, void *data,
  244. struct drm_file *file_priv)
  245. {
  246. struct drm_ctx_res *res = data;
  247. struct drm_ctx ctx;
  248. int i;
  249. if (res->count >= DRM_RESERVED_CONTEXTS) {
  250. memset(&ctx, 0, sizeof(ctx));
  251. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  252. ctx.handle = i;
  253. if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
  254. return -EFAULT;
  255. }
  256. }
  257. res->count = DRM_RESERVED_CONTEXTS;
  258. return 0;
  259. }
  260. /**
  261. * Add context.
  262. *
  263. * \param inode device inode.
  264. * \param file_priv DRM file private.
  265. * \param cmd command.
  266. * \param arg user argument pointing to a drm_ctx structure.
  267. * \return zero on success or a negative number on failure.
  268. *
  269. * Get a new handle for the context and copy to userspace.
  270. */
  271. int drm_addctx(struct drm_device *dev, void *data,
  272. struct drm_file *file_priv)
  273. {
  274. struct drm_ctx_list *ctx_entry;
  275. struct drm_ctx *ctx = data;
  276. ctx->handle = drm_ctxbitmap_next(dev);
  277. if (ctx->handle == DRM_KERNEL_CONTEXT) {
  278. /* Skip kernel's context and get a new one. */
  279. ctx->handle = drm_ctxbitmap_next(dev);
  280. }
  281. DRM_DEBUG("%d\n", ctx->handle);
  282. if (ctx->handle == -1) {
  283. DRM_DEBUG("Not enough free contexts.\n");
  284. /* Should this return -EBUSY instead? */
  285. return -ENOMEM;
  286. }
  287. ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
  288. if (!ctx_entry) {
  289. DRM_DEBUG("out of memory\n");
  290. return -ENOMEM;
  291. }
  292. INIT_LIST_HEAD(&ctx_entry->head);
  293. ctx_entry->handle = ctx->handle;
  294. ctx_entry->tag = file_priv;
  295. mutex_lock(&dev->ctxlist_mutex);
  296. list_add(&ctx_entry->head, &dev->ctxlist);
  297. ++dev->ctx_count;
  298. mutex_unlock(&dev->ctxlist_mutex);
  299. return 0;
  300. }
  301. int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
  302. {
  303. /* This does nothing */
  304. return 0;
  305. }
  306. /**
  307. * Get context.
  308. *
  309. * \param inode device inode.
  310. * \param file_priv DRM file private.
  311. * \param cmd command.
  312. * \param arg user argument pointing to a drm_ctx structure.
  313. * \return zero on success or a negative number on failure.
  314. */
  315. int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
  316. {
  317. struct drm_ctx *ctx = data;
  318. /* This is 0, because we don't handle any context flags */
  319. ctx->flags = 0;
  320. return 0;
  321. }
  322. /**
  323. * Switch context.
  324. *
  325. * \param inode device inode.
  326. * \param file_priv DRM file private.
  327. * \param cmd command.
  328. * \param arg user argument pointing to a drm_ctx structure.
  329. * \return zero on success or a negative number on failure.
  330. *
  331. * Calls context_switch().
  332. */
  333. int drm_switchctx(struct drm_device *dev, void *data,
  334. struct drm_file *file_priv)
  335. {
  336. struct drm_ctx *ctx = data;
  337. DRM_DEBUG("%d\n", ctx->handle);
  338. return drm_context_switch(dev, dev->last_context, ctx->handle);
  339. }
  340. /**
  341. * New context.
  342. *
  343. * \param inode device inode.
  344. * \param file_priv DRM file private.
  345. * \param cmd command.
  346. * \param arg user argument pointing to a drm_ctx structure.
  347. * \return zero on success or a negative number on failure.
  348. *
  349. * Calls context_switch_complete().
  350. */
  351. int drm_newctx(struct drm_device *dev, void *data,
  352. struct drm_file *file_priv)
  353. {
  354. struct drm_ctx *ctx = data;
  355. DRM_DEBUG("%d\n", ctx->handle);
  356. drm_context_switch_complete(dev, file_priv, ctx->handle);
  357. return 0;
  358. }
  359. /**
  360. * Remove context.
  361. *
  362. * \param inode device inode.
  363. * \param file_priv DRM file private.
  364. * \param cmd command.
  365. * \param arg user argument pointing to a drm_ctx structure.
  366. * \return zero on success or a negative number on failure.
  367. *
  368. * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
  369. */
  370. int drm_rmctx(struct drm_device *dev, void *data,
  371. struct drm_file *file_priv)
  372. {
  373. struct drm_ctx *ctx = data;
  374. DRM_DEBUG("%d\n", ctx->handle);
  375. if (ctx->handle != DRM_KERNEL_CONTEXT) {
  376. if (dev->driver->context_dtor)
  377. dev->driver->context_dtor(dev, ctx->handle);
  378. drm_ctxbitmap_free(dev, ctx->handle);
  379. }
  380. mutex_lock(&dev->ctxlist_mutex);
  381. if (!list_empty(&dev->ctxlist)) {
  382. struct drm_ctx_list *pos, *n;
  383. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  384. if (pos->handle == ctx->handle) {
  385. list_del(&pos->head);
  386. kfree(pos);
  387. --dev->ctx_count;
  388. }
  389. }
  390. }
  391. mutex_unlock(&dev->ctxlist_mutex);
  392. return 0;
  393. }
  394. /*@}*/