drm_context.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 filp file pointer.
  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 inode *inode, struct file *filp,
  131. unsigned int cmd, unsigned long arg)
  132. {
  133. struct drm_file *priv = filp->private_data;
  134. struct drm_device *dev = priv->head->dev;
  135. struct drm_ctx_priv_map __user *argp = (void __user *)arg;
  136. struct drm_ctx_priv_map request;
  137. struct drm_map *map;
  138. struct drm_map_list *_entry;
  139. if (copy_from_user(&request, argp, sizeof(request)))
  140. return -EFAULT;
  141. mutex_lock(&dev->struct_mutex);
  142. map = idr_find(&dev->ctx_idr, request.ctx_id);
  143. if (!map) {
  144. mutex_unlock(&dev->struct_mutex);
  145. return -EINVAL;
  146. }
  147. mutex_unlock(&dev->struct_mutex);
  148. request.handle = NULL;
  149. list_for_each_entry(_entry, &dev->maplist, head) {
  150. if (_entry->map == map) {
  151. request.handle =
  152. (void *)(unsigned long)_entry->user_token;
  153. break;
  154. }
  155. }
  156. if (request.handle == NULL)
  157. return -EINVAL;
  158. if (copy_to_user(argp, &request, sizeof(request)))
  159. return -EFAULT;
  160. return 0;
  161. }
  162. /**
  163. * Set per-context SAREA.
  164. *
  165. * \param inode device inode.
  166. * \param filp file pointer.
  167. * \param cmd command.
  168. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  169. * \return zero on success or a negative number on failure.
  170. *
  171. * Searches the mapping specified in \p arg and update the entry in
  172. * drm_device::ctx_idr with it.
  173. */
  174. int drm_setsareactx(struct inode *inode, struct file *filp,
  175. unsigned int cmd, unsigned long arg)
  176. {
  177. struct drm_file *priv = filp->private_data;
  178. struct drm_device *dev = priv->head->dev;
  179. struct drm_ctx_priv_map request;
  180. struct drm_map *map = NULL;
  181. struct drm_map_list *r_list = NULL;
  182. if (copy_from_user(&request,
  183. (struct drm_ctx_priv_map __user *) arg,
  184. sizeof(request)))
  185. return -EFAULT;
  186. mutex_lock(&dev->struct_mutex);
  187. list_for_each_entry(r_list, &dev->maplist, head) {
  188. if (r_list->map
  189. && r_list->user_token == (unsigned long)request.handle)
  190. goto found;
  191. }
  192. bad:
  193. mutex_unlock(&dev->struct_mutex);
  194. return -EINVAL;
  195. found:
  196. map = r_list->map;
  197. if (!map)
  198. goto bad;
  199. if (IS_ERR(idr_replace(&dev->ctx_idr, map, request.ctx_id)))
  200. goto bad;
  201. mutex_unlock(&dev->struct_mutex);
  202. return 0;
  203. }
  204. /*@}*/
  205. /******************************************************************/
  206. /** \name The actual DRM context handling routines */
  207. /*@{*/
  208. /**
  209. * Switch context.
  210. *
  211. * \param dev DRM device.
  212. * \param old old context handle.
  213. * \param new new context handle.
  214. * \return zero on success or a negative number on failure.
  215. *
  216. * Attempt to set drm_device::context_flag.
  217. */
  218. static int drm_context_switch(struct drm_device * dev, int old, int new)
  219. {
  220. if (test_and_set_bit(0, &dev->context_flag)) {
  221. DRM_ERROR("Reentering -- FIXME\n");
  222. return -EBUSY;
  223. }
  224. DRM_DEBUG("Context switch from %d to %d\n", old, new);
  225. if (new == dev->last_context) {
  226. clear_bit(0, &dev->context_flag);
  227. return 0;
  228. }
  229. return 0;
  230. }
  231. /**
  232. * Complete context switch.
  233. *
  234. * \param dev DRM device.
  235. * \param new new context handle.
  236. * \return zero on success or a negative number on failure.
  237. *
  238. * Updates drm_device::last_context and drm_device::last_switch. Verifies the
  239. * hardware lock is held, clears the drm_device::context_flag and wakes up
  240. * drm_device::context_wait.
  241. */
  242. static int drm_context_switch_complete(struct drm_device * dev, int new)
  243. {
  244. dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
  245. dev->last_switch = jiffies;
  246. if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
  247. DRM_ERROR("Lock isn't held after context switch\n");
  248. }
  249. /* If a context switch is ever initiated
  250. when the kernel holds the lock, release
  251. that lock here. */
  252. clear_bit(0, &dev->context_flag);
  253. wake_up(&dev->context_wait);
  254. return 0;
  255. }
  256. /**
  257. * Reserve contexts.
  258. *
  259. * \param inode device inode.
  260. * \param filp file pointer.
  261. * \param cmd command.
  262. * \param arg user argument pointing to a drm_ctx_res structure.
  263. * \return zero on success or a negative number on failure.
  264. */
  265. int drm_resctx(struct inode *inode, struct file *filp,
  266. unsigned int cmd, unsigned long arg)
  267. {
  268. struct drm_ctx_res res;
  269. struct drm_ctx_res __user *argp = (void __user *)arg;
  270. struct drm_ctx ctx;
  271. int i;
  272. if (copy_from_user(&res, argp, sizeof(res)))
  273. return -EFAULT;
  274. if (res.count >= DRM_RESERVED_CONTEXTS) {
  275. memset(&ctx, 0, sizeof(ctx));
  276. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  277. ctx.handle = i;
  278. if (copy_to_user(&res.contexts[i], &ctx, sizeof(ctx)))
  279. return -EFAULT;
  280. }
  281. }
  282. res.count = DRM_RESERVED_CONTEXTS;
  283. if (copy_to_user(argp, &res, sizeof(res)))
  284. return -EFAULT;
  285. return 0;
  286. }
  287. /**
  288. * Add context.
  289. *
  290. * \param inode device inode.
  291. * \param filp file pointer.
  292. * \param cmd command.
  293. * \param arg user argument pointing to a drm_ctx structure.
  294. * \return zero on success or a negative number on failure.
  295. *
  296. * Get a new handle for the context and copy to userspace.
  297. */
  298. int drm_addctx(struct inode *inode, struct file *filp,
  299. unsigned int cmd, unsigned long arg)
  300. {
  301. struct drm_file *priv = filp->private_data;
  302. struct drm_device *dev = priv->head->dev;
  303. struct drm_ctx_list *ctx_entry;
  304. struct drm_ctx __user *argp = (void __user *)arg;
  305. struct drm_ctx ctx;
  306. if (copy_from_user(&ctx, argp, sizeof(ctx)))
  307. return -EFAULT;
  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. if (ctx.handle != DRM_KERNEL_CONTEXT) {
  320. if (dev->driver->context_ctor)
  321. if (!dev->driver->context_ctor(dev, ctx.handle)) {
  322. DRM_DEBUG("Running out of ctxs or memory.\n");
  323. return -ENOMEM;
  324. }
  325. }
  326. ctx_entry = drm_alloc(sizeof(*ctx_entry), DRM_MEM_CTXLIST);
  327. if (!ctx_entry) {
  328. DRM_DEBUG("out of memory\n");
  329. return -ENOMEM;
  330. }
  331. INIT_LIST_HEAD(&ctx_entry->head);
  332. ctx_entry->handle = ctx.handle;
  333. ctx_entry->tag = priv;
  334. mutex_lock(&dev->ctxlist_mutex);
  335. list_add(&ctx_entry->head, &dev->ctxlist);
  336. ++dev->ctx_count;
  337. mutex_unlock(&dev->ctxlist_mutex);
  338. if (copy_to_user(argp, &ctx, sizeof(ctx)))
  339. return -EFAULT;
  340. return 0;
  341. }
  342. int drm_modctx(struct inode *inode, struct file *filp,
  343. unsigned int cmd, unsigned long arg)
  344. {
  345. /* This does nothing */
  346. return 0;
  347. }
  348. /**
  349. * Get context.
  350. *
  351. * \param inode device inode.
  352. * \param filp file pointer.
  353. * \param cmd command.
  354. * \param arg user argument pointing to a drm_ctx structure.
  355. * \return zero on success or a negative number on failure.
  356. */
  357. int drm_getctx(struct inode *inode, struct file *filp,
  358. unsigned int cmd, unsigned long arg)
  359. {
  360. struct drm_ctx __user *argp = (void __user *)arg;
  361. struct drm_ctx ctx;
  362. if (copy_from_user(&ctx, argp, sizeof(ctx)))
  363. return -EFAULT;
  364. /* This is 0, because we don't handle any context flags */
  365. ctx.flags = 0;
  366. if (copy_to_user(argp, &ctx, sizeof(ctx)))
  367. return -EFAULT;
  368. return 0;
  369. }
  370. /**
  371. * Switch context.
  372. *
  373. * \param inode device inode.
  374. * \param filp file pointer.
  375. * \param cmd command.
  376. * \param arg user argument pointing to a drm_ctx structure.
  377. * \return zero on success or a negative number on failure.
  378. *
  379. * Calls context_switch().
  380. */
  381. int drm_switchctx(struct inode *inode, struct file *filp,
  382. unsigned int cmd, unsigned long arg)
  383. {
  384. struct drm_file *priv = filp->private_data;
  385. struct drm_device *dev = priv->head->dev;
  386. struct drm_ctx ctx;
  387. if (copy_from_user(&ctx, (struct drm_ctx __user *) arg, sizeof(ctx)))
  388. return -EFAULT;
  389. DRM_DEBUG("%d\n", ctx.handle);
  390. return drm_context_switch(dev, dev->last_context, ctx.handle);
  391. }
  392. /**
  393. * New context.
  394. *
  395. * \param inode device inode.
  396. * \param filp file pointer.
  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. * Calls context_switch_complete().
  402. */
  403. int drm_newctx(struct inode *inode, struct file *filp,
  404. unsigned int cmd, unsigned long arg)
  405. {
  406. struct drm_file *priv = filp->private_data;
  407. struct drm_device *dev = priv->head->dev;
  408. struct drm_ctx ctx;
  409. if (copy_from_user(&ctx, (struct drm_ctx __user *) arg, sizeof(ctx)))
  410. return -EFAULT;
  411. DRM_DEBUG("%d\n", ctx.handle);
  412. drm_context_switch_complete(dev, ctx.handle);
  413. return 0;
  414. }
  415. /**
  416. * Remove context.
  417. *
  418. * \param inode device inode.
  419. * \param filp file pointer.
  420. * \param cmd command.
  421. * \param arg user argument pointing to a drm_ctx structure.
  422. * \return zero on success or a negative number on failure.
  423. *
  424. * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
  425. */
  426. int drm_rmctx(struct inode *inode, struct file *filp,
  427. unsigned int cmd, unsigned long arg)
  428. {
  429. struct drm_file *priv = filp->private_data;
  430. struct drm_device *dev = priv->head->dev;
  431. struct drm_ctx ctx;
  432. if (copy_from_user(&ctx, (struct drm_ctx __user *) arg, sizeof(ctx)))
  433. return -EFAULT;
  434. DRM_DEBUG("%d\n", ctx.handle);
  435. if (ctx.handle == DRM_KERNEL_CONTEXT + 1) {
  436. priv->remove_auth_on_close = 1;
  437. }
  438. if (ctx.handle != DRM_KERNEL_CONTEXT) {
  439. if (dev->driver->context_dtor)
  440. dev->driver->context_dtor(dev, ctx.handle);
  441. drm_ctxbitmap_free(dev, ctx.handle);
  442. }
  443. mutex_lock(&dev->ctxlist_mutex);
  444. if (!list_empty(&dev->ctxlist)) {
  445. struct drm_ctx_list *pos, *n;
  446. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  447. if (pos->handle == ctx.handle) {
  448. list_del(&pos->head);
  449. drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
  450. --dev->ctx_count;
  451. }
  452. }
  453. }
  454. mutex_unlock(&dev->ctxlist_mutex);
  455. return 0;
  456. }
  457. /*@}*/