drm_context.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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::context_sareas, while holding the drm_device::struct_mutex
  52. * lock.
  53. */
  54. void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle)
  55. {
  56. if (ctx_handle < 0)
  57. goto failed;
  58. if (!dev->ctx_bitmap)
  59. goto failed;
  60. if (ctx_handle < DRM_MAX_CTXBITMAP) {
  61. mutex_lock(&dev->struct_mutex);
  62. clear_bit(ctx_handle, dev->ctx_bitmap);
  63. dev->context_sareas[ctx_handle] = NULL;
  64. mutex_unlock(&dev->struct_mutex);
  65. return;
  66. }
  67. failed:
  68. DRM_ERROR("Attempt to free invalid context handle: %d\n", ctx_handle);
  69. return;
  70. }
  71. /**
  72. * Context bitmap allocation.
  73. *
  74. * \param dev DRM device.
  75. * \return (non-negative) context handle on success or a negative number on failure.
  76. *
  77. * Find the first zero bit in drm_device::ctx_bitmap and (re)allocates
  78. * drm_device::context_sareas to accommodate the new entry while holding the
  79. * drm_device::struct_mutex lock.
  80. */
  81. static int drm_ctxbitmap_next(drm_device_t * dev)
  82. {
  83. int bit;
  84. if (!dev->ctx_bitmap)
  85. return -1;
  86. mutex_lock(&dev->struct_mutex);
  87. bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
  88. if (bit < DRM_MAX_CTXBITMAP) {
  89. set_bit(bit, dev->ctx_bitmap);
  90. DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit);
  91. if ((bit + 1) > dev->max_context) {
  92. dev->max_context = (bit + 1);
  93. if (dev->context_sareas) {
  94. drm_map_t **ctx_sareas;
  95. ctx_sareas = drm_realloc(dev->context_sareas,
  96. (dev->max_context -
  97. 1) *
  98. sizeof(*dev->
  99. context_sareas),
  100. dev->max_context *
  101. sizeof(*dev->
  102. context_sareas),
  103. DRM_MEM_MAPS);
  104. if (!ctx_sareas) {
  105. clear_bit(bit, dev->ctx_bitmap);
  106. mutex_unlock(&dev->struct_mutex);
  107. return -1;
  108. }
  109. dev->context_sareas = ctx_sareas;
  110. dev->context_sareas[bit] = NULL;
  111. } else {
  112. /* max_context == 1 at this point */
  113. dev->context_sareas =
  114. drm_alloc(dev->max_context *
  115. sizeof(*dev->context_sareas),
  116. DRM_MEM_MAPS);
  117. if (!dev->context_sareas) {
  118. clear_bit(bit, dev->ctx_bitmap);
  119. mutex_unlock(&dev->struct_mutex);
  120. return -1;
  121. }
  122. dev->context_sareas[bit] = NULL;
  123. }
  124. }
  125. mutex_unlock(&dev->struct_mutex);
  126. return bit;
  127. }
  128. mutex_unlock(&dev->struct_mutex);
  129. return -1;
  130. }
  131. /**
  132. * Context bitmap initialization.
  133. *
  134. * \param dev DRM device.
  135. *
  136. * Allocates and initialize drm_device::ctx_bitmap and drm_device::context_sareas, while holding
  137. * the drm_device::struct_mutex lock.
  138. */
  139. int drm_ctxbitmap_init(drm_device_t * dev)
  140. {
  141. int i;
  142. int temp;
  143. mutex_lock(&dev->struct_mutex);
  144. dev->ctx_bitmap = (unsigned long *)drm_alloc(PAGE_SIZE,
  145. DRM_MEM_CTXBITMAP);
  146. if (dev->ctx_bitmap == NULL) {
  147. mutex_unlock(&dev->struct_mutex);
  148. return -ENOMEM;
  149. }
  150. memset((void *)dev->ctx_bitmap, 0, PAGE_SIZE);
  151. dev->context_sareas = NULL;
  152. dev->max_context = -1;
  153. mutex_unlock(&dev->struct_mutex);
  154. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  155. temp = drm_ctxbitmap_next(dev);
  156. DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
  157. }
  158. return 0;
  159. }
  160. /**
  161. * Context bitmap cleanup.
  162. *
  163. * \param dev DRM device.
  164. *
  165. * Frees drm_device::ctx_bitmap and drm_device::context_sareas, while holding
  166. * the drm_device::struct_mutex lock.
  167. */
  168. void drm_ctxbitmap_cleanup(drm_device_t * dev)
  169. {
  170. mutex_lock(&dev->struct_mutex);
  171. if (dev->context_sareas)
  172. drm_free(dev->context_sareas,
  173. sizeof(*dev->context_sareas) *
  174. dev->max_context, DRM_MEM_MAPS);
  175. drm_free((void *)dev->ctx_bitmap, PAGE_SIZE, DRM_MEM_CTXBITMAP);
  176. mutex_unlock(&dev->struct_mutex);
  177. }
  178. /*@}*/
  179. /******************************************************************/
  180. /** \name Per Context SAREA Support */
  181. /*@{*/
  182. /**
  183. * Get per-context SAREA.
  184. *
  185. * \param inode device inode.
  186. * \param filp file pointer.
  187. * \param cmd command.
  188. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  189. * \return zero on success or a negative number on failure.
  190. *
  191. * Gets the map from drm_device::context_sareas with the handle specified and
  192. * returns its handle.
  193. */
  194. int drm_getsareactx(struct inode *inode, struct file *filp,
  195. unsigned int cmd, unsigned long arg)
  196. {
  197. drm_file_t *priv = filp->private_data;
  198. drm_device_t *dev = priv->head->dev;
  199. drm_ctx_priv_map_t __user *argp = (void __user *)arg;
  200. drm_ctx_priv_map_t request;
  201. drm_map_t *map;
  202. drm_map_list_t *_entry;
  203. if (copy_from_user(&request, argp, sizeof(request)))
  204. return -EFAULT;
  205. mutex_lock(&dev->struct_mutex);
  206. if (dev->max_context < 0
  207. || request.ctx_id >= (unsigned)dev->max_context) {
  208. mutex_unlock(&dev->struct_mutex);
  209. return -EINVAL;
  210. }
  211. map = dev->context_sareas[request.ctx_id];
  212. mutex_unlock(&dev->struct_mutex);
  213. request.handle = NULL;
  214. list_for_each_entry(_entry, &dev->maplist->head, head) {
  215. if (_entry->map == map) {
  216. request.handle =
  217. (void *)(unsigned long)_entry->user_token;
  218. break;
  219. }
  220. }
  221. if (request.handle == NULL)
  222. return -EINVAL;
  223. if (copy_to_user(argp, &request, sizeof(request)))
  224. return -EFAULT;
  225. return 0;
  226. }
  227. /**
  228. * Set per-context SAREA.
  229. *
  230. * \param inode device inode.
  231. * \param filp file pointer.
  232. * \param cmd command.
  233. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  234. * \return zero on success or a negative number on failure.
  235. *
  236. * Searches the mapping specified in \p arg and update the entry in
  237. * drm_device::context_sareas with it.
  238. */
  239. int drm_setsareactx(struct inode *inode, struct file *filp,
  240. unsigned int cmd, unsigned long arg)
  241. {
  242. drm_file_t *priv = filp->private_data;
  243. drm_device_t *dev = priv->head->dev;
  244. drm_ctx_priv_map_t request;
  245. drm_map_t *map = NULL;
  246. drm_map_list_t *r_list = NULL;
  247. struct list_head *list;
  248. if (copy_from_user(&request,
  249. (drm_ctx_priv_map_t __user *) arg, sizeof(request)))
  250. return -EFAULT;
  251. mutex_lock(&dev->struct_mutex);
  252. list_for_each(list, &dev->maplist->head) {
  253. r_list = list_entry(list, drm_map_list_t, head);
  254. if (r_list->map
  255. && r_list->user_token == (unsigned long)request.handle)
  256. goto found;
  257. }
  258. bad:
  259. mutex_unlock(&dev->struct_mutex);
  260. return -EINVAL;
  261. found:
  262. map = r_list->map;
  263. if (!map)
  264. goto bad;
  265. if (dev->max_context < 0)
  266. goto bad;
  267. if (request.ctx_id >= (unsigned)dev->max_context)
  268. goto bad;
  269. dev->context_sareas[request.ctx_id] = map;
  270. mutex_unlock(&dev->struct_mutex);
  271. return 0;
  272. }
  273. /*@}*/
  274. /******************************************************************/
  275. /** \name The actual DRM context handling routines */
  276. /*@{*/
  277. /**
  278. * Switch context.
  279. *
  280. * \param dev DRM device.
  281. * \param old old context handle.
  282. * \param new new context handle.
  283. * \return zero on success or a negative number on failure.
  284. *
  285. * Attempt to set drm_device::context_flag.
  286. */
  287. static int drm_context_switch(drm_device_t * dev, int old, int new)
  288. {
  289. if (test_and_set_bit(0, &dev->context_flag)) {
  290. DRM_ERROR("Reentering -- FIXME\n");
  291. return -EBUSY;
  292. }
  293. DRM_DEBUG("Context switch from %d to %d\n", old, new);
  294. if (new == dev->last_context) {
  295. clear_bit(0, &dev->context_flag);
  296. return 0;
  297. }
  298. return 0;
  299. }
  300. /**
  301. * Complete context switch.
  302. *
  303. * \param dev DRM device.
  304. * \param new new context handle.
  305. * \return zero on success or a negative number on failure.
  306. *
  307. * Updates drm_device::last_context and drm_device::last_switch. Verifies the
  308. * hardware lock is held, clears the drm_device::context_flag and wakes up
  309. * drm_device::context_wait.
  310. */
  311. static int drm_context_switch_complete(drm_device_t * dev, int new)
  312. {
  313. dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
  314. dev->last_switch = jiffies;
  315. if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
  316. DRM_ERROR("Lock isn't held after context switch\n");
  317. }
  318. /* If a context switch is ever initiated
  319. when the kernel holds the lock, release
  320. that lock here. */
  321. clear_bit(0, &dev->context_flag);
  322. wake_up(&dev->context_wait);
  323. return 0;
  324. }
  325. /**
  326. * Reserve contexts.
  327. *
  328. * \param inode device inode.
  329. * \param filp file pointer.
  330. * \param cmd command.
  331. * \param arg user argument pointing to a drm_ctx_res structure.
  332. * \return zero on success or a negative number on failure.
  333. */
  334. int drm_resctx(struct inode *inode, struct file *filp,
  335. unsigned int cmd, unsigned long arg)
  336. {
  337. drm_ctx_res_t res;
  338. drm_ctx_t __user *argp = (void __user *)arg;
  339. drm_ctx_t ctx;
  340. int i;
  341. if (copy_from_user(&res, argp, sizeof(res)))
  342. return -EFAULT;
  343. if (res.count >= DRM_RESERVED_CONTEXTS) {
  344. memset(&ctx, 0, sizeof(ctx));
  345. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  346. ctx.handle = i;
  347. if (copy_to_user(&res.contexts[i], &ctx, sizeof(ctx)))
  348. return -EFAULT;
  349. }
  350. }
  351. res.count = DRM_RESERVED_CONTEXTS;
  352. if (copy_to_user(argp, &res, sizeof(res)))
  353. return -EFAULT;
  354. return 0;
  355. }
  356. /**
  357. * Add context.
  358. *
  359. * \param inode device inode.
  360. * \param filp file pointer.
  361. * \param cmd command.
  362. * \param arg user argument pointing to a drm_ctx structure.
  363. * \return zero on success or a negative number on failure.
  364. *
  365. * Get a new handle for the context and copy to userspace.
  366. */
  367. int drm_addctx(struct inode *inode, struct file *filp,
  368. unsigned int cmd, unsigned long arg)
  369. {
  370. drm_file_t *priv = filp->private_data;
  371. drm_device_t *dev = priv->head->dev;
  372. drm_ctx_list_t *ctx_entry;
  373. drm_ctx_t __user *argp = (void __user *)arg;
  374. drm_ctx_t ctx;
  375. if (copy_from_user(&ctx, argp, sizeof(ctx)))
  376. return -EFAULT;
  377. ctx.handle = drm_ctxbitmap_next(dev);
  378. if (ctx.handle == DRM_KERNEL_CONTEXT) {
  379. /* Skip kernel's context and get a new one. */
  380. ctx.handle = drm_ctxbitmap_next(dev);
  381. }
  382. DRM_DEBUG("%d\n", ctx.handle);
  383. if (ctx.handle == -1) {
  384. DRM_DEBUG("Not enough free contexts.\n");
  385. /* Should this return -EBUSY instead? */
  386. return -ENOMEM;
  387. }
  388. if (ctx.handle != DRM_KERNEL_CONTEXT) {
  389. if (dev->driver->context_ctor)
  390. if (!dev->driver->context_ctor(dev, ctx.handle)) {
  391. DRM_DEBUG("Running out of ctxs or memory.\n");
  392. return -ENOMEM;
  393. }
  394. }
  395. ctx_entry = drm_alloc(sizeof(*ctx_entry), DRM_MEM_CTXLIST);
  396. if (!ctx_entry) {
  397. DRM_DEBUG("out of memory\n");
  398. return -ENOMEM;
  399. }
  400. INIT_LIST_HEAD(&ctx_entry->head);
  401. ctx_entry->handle = ctx.handle;
  402. ctx_entry->tag = priv;
  403. mutex_lock(&dev->ctxlist_mutex);
  404. list_add(&ctx_entry->head, &dev->ctxlist->head);
  405. ++dev->ctx_count;
  406. mutex_unlock(&dev->ctxlist_mutex);
  407. if (copy_to_user(argp, &ctx, sizeof(ctx)))
  408. return -EFAULT;
  409. return 0;
  410. }
  411. int drm_modctx(struct inode *inode, struct file *filp,
  412. unsigned int cmd, unsigned long arg)
  413. {
  414. /* This does nothing */
  415. return 0;
  416. }
  417. /**
  418. * Get context.
  419. *
  420. * \param inode device inode.
  421. * \param filp file pointer.
  422. * \param cmd command.
  423. * \param arg user argument pointing to a drm_ctx structure.
  424. * \return zero on success or a negative number on failure.
  425. */
  426. int drm_getctx(struct inode *inode, struct file *filp,
  427. unsigned int cmd, unsigned long arg)
  428. {
  429. drm_ctx_t __user *argp = (void __user *)arg;
  430. drm_ctx_t ctx;
  431. if (copy_from_user(&ctx, argp, sizeof(ctx)))
  432. return -EFAULT;
  433. /* This is 0, because we don't handle any context flags */
  434. ctx.flags = 0;
  435. if (copy_to_user(argp, &ctx, sizeof(ctx)))
  436. return -EFAULT;
  437. return 0;
  438. }
  439. /**
  440. * Switch context.
  441. *
  442. * \param inode device inode.
  443. * \param filp file pointer.
  444. * \param cmd command.
  445. * \param arg user argument pointing to a drm_ctx structure.
  446. * \return zero on success or a negative number on failure.
  447. *
  448. * Calls context_switch().
  449. */
  450. int drm_switchctx(struct inode *inode, struct file *filp,
  451. unsigned int cmd, unsigned long arg)
  452. {
  453. drm_file_t *priv = filp->private_data;
  454. drm_device_t *dev = priv->head->dev;
  455. drm_ctx_t ctx;
  456. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  457. return -EFAULT;
  458. DRM_DEBUG("%d\n", ctx.handle);
  459. return drm_context_switch(dev, dev->last_context, ctx.handle);
  460. }
  461. /**
  462. * New context.
  463. *
  464. * \param inode device inode.
  465. * \param filp file pointer.
  466. * \param cmd command.
  467. * \param arg user argument pointing to a drm_ctx structure.
  468. * \return zero on success or a negative number on failure.
  469. *
  470. * Calls context_switch_complete().
  471. */
  472. int drm_newctx(struct inode *inode, struct file *filp,
  473. unsigned int cmd, unsigned long arg)
  474. {
  475. drm_file_t *priv = filp->private_data;
  476. drm_device_t *dev = priv->head->dev;
  477. drm_ctx_t ctx;
  478. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  479. return -EFAULT;
  480. DRM_DEBUG("%d\n", ctx.handle);
  481. drm_context_switch_complete(dev, ctx.handle);
  482. return 0;
  483. }
  484. /**
  485. * Remove context.
  486. *
  487. * \param inode device inode.
  488. * \param filp file pointer.
  489. * \param cmd command.
  490. * \param arg user argument pointing to a drm_ctx structure.
  491. * \return zero on success or a negative number on failure.
  492. *
  493. * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
  494. */
  495. int drm_rmctx(struct inode *inode, struct file *filp,
  496. unsigned int cmd, unsigned long arg)
  497. {
  498. drm_file_t *priv = filp->private_data;
  499. drm_device_t *dev = priv->head->dev;
  500. drm_ctx_t ctx;
  501. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  502. return -EFAULT;
  503. DRM_DEBUG("%d\n", ctx.handle);
  504. if (ctx.handle == DRM_KERNEL_CONTEXT + 1) {
  505. priv->remove_auth_on_close = 1;
  506. }
  507. if (ctx.handle != DRM_KERNEL_CONTEXT) {
  508. if (dev->driver->context_dtor)
  509. dev->driver->context_dtor(dev, ctx.handle);
  510. drm_ctxbitmap_free(dev, ctx.handle);
  511. }
  512. mutex_lock(&dev->ctxlist_mutex);
  513. if (!list_empty(&dev->ctxlist->head)) {
  514. drm_ctx_list_t *pos, *n;
  515. list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
  516. if (pos->handle == ctx.handle) {
  517. list_del(&pos->head);
  518. drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
  519. --dev->ctx_count;
  520. }
  521. }
  522. }
  523. mutex_unlock(&dev->ctxlist_mutex);
  524. return 0;
  525. }
  526. /*@}*/