drm_context.c 12 KB

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