drm_context.c 15 KB

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