drm_context.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. static 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. drm_map_list_t *_entry;
  198. if (copy_from_user(&request, argp, sizeof(request)))
  199. return -EFAULT;
  200. down(&dev->struct_sem);
  201. if (dev->max_context < 0 || request.ctx_id >= (unsigned) dev->max_context) {
  202. up(&dev->struct_sem);
  203. return -EINVAL;
  204. }
  205. map = dev->context_sareas[request.ctx_id];
  206. up(&dev->struct_sem);
  207. request.handle = 0;
  208. list_for_each_entry(_entry, &dev->maplist->head,head) {
  209. if (_entry->map == map) {
  210. request.handle = (void *)(unsigned long)_entry->user_token;
  211. break;
  212. }
  213. }
  214. if (request.handle == 0)
  215. return -EINVAL;
  216. if (copy_to_user(argp, &request, sizeof(request)))
  217. return -EFAULT;
  218. return 0;
  219. }
  220. /**
  221. * Set per-context SAREA.
  222. *
  223. * \param inode device inode.
  224. * \param filp file pointer.
  225. * \param cmd command.
  226. * \param arg user argument pointing to a drm_ctx_priv_map structure.
  227. * \return zero on success or a negative number on failure.
  228. *
  229. * Searches the mapping specified in \p arg and update the entry in
  230. * drm_device::context_sareas with it.
  231. */
  232. int drm_setsareactx(struct inode *inode, struct file *filp,
  233. unsigned int cmd, unsigned long arg)
  234. {
  235. drm_file_t *priv = filp->private_data;
  236. drm_device_t *dev = priv->head->dev;
  237. drm_ctx_priv_map_t request;
  238. drm_map_t *map = NULL;
  239. drm_map_list_t *r_list = NULL;
  240. struct list_head *list;
  241. if (copy_from_user(&request,
  242. (drm_ctx_priv_map_t __user *)arg,
  243. sizeof(request)))
  244. return -EFAULT;
  245. down(&dev->struct_sem);
  246. list_for_each(list, &dev->maplist->head) {
  247. r_list = list_entry(list, drm_map_list_t, head);
  248. if (r_list->map
  249. && r_list->user_token == (unsigned long) request.handle)
  250. goto found;
  251. }
  252. bad:
  253. up(&dev->struct_sem);
  254. return -EINVAL;
  255. found:
  256. map = r_list->map;
  257. if (!map) goto bad;
  258. if (dev->max_context < 0)
  259. goto bad;
  260. if (request.ctx_id >= (unsigned) dev->max_context)
  261. goto bad;
  262. dev->context_sareas[request.ctx_id] = map;
  263. up(&dev->struct_sem);
  264. return 0;
  265. }
  266. /*@}*/
  267. /******************************************************************/
  268. /** \name The actual DRM context handling routines */
  269. /*@{*/
  270. /**
  271. * Switch context.
  272. *
  273. * \param dev DRM device.
  274. * \param old old context handle.
  275. * \param new new context handle.
  276. * \return zero on success or a negative number on failure.
  277. *
  278. * Attempt to set drm_device::context_flag.
  279. */
  280. static int drm_context_switch( drm_device_t *dev, int old, int new )
  281. {
  282. if ( test_and_set_bit( 0, &dev->context_flag ) ) {
  283. DRM_ERROR( "Reentering -- FIXME\n" );
  284. return -EBUSY;
  285. }
  286. DRM_DEBUG( "Context switch from %d to %d\n", old, new );
  287. if ( new == dev->last_context ) {
  288. clear_bit( 0, &dev->context_flag );
  289. return 0;
  290. }
  291. return 0;
  292. }
  293. /**
  294. * Complete context switch.
  295. *
  296. * \param dev DRM device.
  297. * \param new new context handle.
  298. * \return zero on success or a negative number on failure.
  299. *
  300. * Updates drm_device::last_context and drm_device::last_switch. Verifies the
  301. * hardware lock is held, clears the drm_device::context_flag and wakes up
  302. * drm_device::context_wait.
  303. */
  304. static int drm_context_switch_complete( drm_device_t *dev, int new )
  305. {
  306. dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
  307. dev->last_switch = jiffies;
  308. if ( !_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ) {
  309. DRM_ERROR( "Lock isn't held after context switch\n" );
  310. }
  311. /* If a context switch is ever initiated
  312. when the kernel holds the lock, release
  313. that lock here. */
  314. clear_bit( 0, &dev->context_flag );
  315. wake_up( &dev->context_wait );
  316. return 0;
  317. }
  318. /**
  319. * Reserve contexts.
  320. *
  321. * \param inode device inode.
  322. * \param filp file pointer.
  323. * \param cmd command.
  324. * \param arg user argument pointing to a drm_ctx_res structure.
  325. * \return zero on success or a negative number on failure.
  326. */
  327. int drm_resctx( struct inode *inode, struct file *filp,
  328. unsigned int cmd, unsigned long arg )
  329. {
  330. drm_ctx_res_t res;
  331. drm_ctx_t __user *argp = (void __user *)arg;
  332. drm_ctx_t ctx;
  333. int i;
  334. if ( copy_from_user( &res, argp, sizeof(res) ) )
  335. return -EFAULT;
  336. if ( res.count >= DRM_RESERVED_CONTEXTS ) {
  337. memset( &ctx, 0, sizeof(ctx) );
  338. for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
  339. ctx.handle = i;
  340. if ( copy_to_user( &res.contexts[i],
  341. &ctx, sizeof(ctx) ) )
  342. return -EFAULT;
  343. }
  344. }
  345. res.count = DRM_RESERVED_CONTEXTS;
  346. if ( copy_to_user( argp, &res, sizeof(res) ) )
  347. return -EFAULT;
  348. return 0;
  349. }
  350. /**
  351. * Add context.
  352. *
  353. * \param inode device inode.
  354. * \param filp file pointer.
  355. * \param cmd command.
  356. * \param arg user argument pointing to a drm_ctx structure.
  357. * \return zero on success or a negative number on failure.
  358. *
  359. * Get a new handle for the context and copy to userspace.
  360. */
  361. int drm_addctx( struct inode *inode, struct file *filp,
  362. unsigned int cmd, unsigned long arg )
  363. {
  364. drm_file_t *priv = filp->private_data;
  365. drm_device_t *dev = priv->head->dev;
  366. drm_ctx_list_t * ctx_entry;
  367. drm_ctx_t __user *argp = (void __user *)arg;
  368. drm_ctx_t ctx;
  369. if ( copy_from_user( &ctx, argp, sizeof(ctx) ) )
  370. return -EFAULT;
  371. ctx.handle = drm_ctxbitmap_next( dev );
  372. if ( ctx.handle == DRM_KERNEL_CONTEXT ) {
  373. /* Skip kernel's context and get a new one. */
  374. ctx.handle = drm_ctxbitmap_next( dev );
  375. }
  376. DRM_DEBUG( "%d\n", ctx.handle );
  377. if ( ctx.handle == -1 ) {
  378. DRM_DEBUG( "Not enough free contexts.\n" );
  379. /* Should this return -EBUSY instead? */
  380. return -ENOMEM;
  381. }
  382. if ( ctx.handle != DRM_KERNEL_CONTEXT )
  383. {
  384. if (dev->driver->context_ctor)
  385. dev->driver->context_ctor(dev, ctx.handle);
  386. }
  387. ctx_entry = drm_alloc( sizeof(*ctx_entry), DRM_MEM_CTXLIST );
  388. if ( !ctx_entry ) {
  389. DRM_DEBUG("out of memory\n");
  390. return -ENOMEM;
  391. }
  392. INIT_LIST_HEAD( &ctx_entry->head );
  393. ctx_entry->handle = ctx.handle;
  394. ctx_entry->tag = priv;
  395. down( &dev->ctxlist_sem );
  396. list_add( &ctx_entry->head, &dev->ctxlist->head );
  397. ++dev->ctx_count;
  398. up( &dev->ctxlist_sem );
  399. if ( copy_to_user( argp, &ctx, sizeof(ctx) ) )
  400. return -EFAULT;
  401. return 0;
  402. }
  403. int drm_modctx( struct inode *inode, struct file *filp,
  404. unsigned int cmd, unsigned long arg )
  405. {
  406. /* This does nothing */
  407. return 0;
  408. }
  409. /**
  410. * Get context.
  411. *
  412. * \param inode device inode.
  413. * \param filp file pointer.
  414. * \param cmd command.
  415. * \param arg user argument pointing to a drm_ctx structure.
  416. * \return zero on success or a negative number on failure.
  417. */
  418. int drm_getctx( struct inode *inode, struct file *filp,
  419. unsigned int cmd, unsigned long arg )
  420. {
  421. drm_ctx_t __user *argp = (void __user *)arg;
  422. drm_ctx_t ctx;
  423. if ( copy_from_user( &ctx, argp, sizeof(ctx) ) )
  424. return -EFAULT;
  425. /* This is 0, because we don't handle any context flags */
  426. ctx.flags = 0;
  427. if ( copy_to_user( argp, &ctx, sizeof(ctx) ) )
  428. return -EFAULT;
  429. return 0;
  430. }
  431. /**
  432. * Switch context.
  433. *
  434. * \param inode device inode.
  435. * \param filp file pointer.
  436. * \param cmd command.
  437. * \param arg user argument pointing to a drm_ctx structure.
  438. * \return zero on success or a negative number on failure.
  439. *
  440. * Calls context_switch().
  441. */
  442. int drm_switchctx( struct inode *inode, struct file *filp,
  443. unsigned int cmd, unsigned long arg )
  444. {
  445. drm_file_t *priv = filp->private_data;
  446. drm_device_t *dev = priv->head->dev;
  447. drm_ctx_t ctx;
  448. if ( copy_from_user( &ctx, (drm_ctx_t __user *)arg, sizeof(ctx) ) )
  449. return -EFAULT;
  450. DRM_DEBUG( "%d\n", ctx.handle );
  451. return drm_context_switch( dev, dev->last_context, ctx.handle );
  452. }
  453. /**
  454. * New context.
  455. *
  456. * \param inode device inode.
  457. * \param filp file pointer.
  458. * \param cmd command.
  459. * \param arg user argument pointing to a drm_ctx structure.
  460. * \return zero on success or a negative number on failure.
  461. *
  462. * Calls context_switch_complete().
  463. */
  464. int drm_newctx( struct inode *inode, struct file *filp,
  465. unsigned int cmd, unsigned long arg )
  466. {
  467. drm_file_t *priv = filp->private_data;
  468. drm_device_t *dev = priv->head->dev;
  469. drm_ctx_t ctx;
  470. if ( copy_from_user( &ctx, (drm_ctx_t __user *)arg, sizeof(ctx) ) )
  471. return -EFAULT;
  472. DRM_DEBUG( "%d\n", ctx.handle );
  473. drm_context_switch_complete( dev, ctx.handle );
  474. return 0;
  475. }
  476. /**
  477. * Remove context.
  478. *
  479. * \param inode device inode.
  480. * \param filp file pointer.
  481. * \param cmd command.
  482. * \param arg user argument pointing to a drm_ctx structure.
  483. * \return zero on success or a negative number on failure.
  484. *
  485. * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
  486. */
  487. int drm_rmctx( struct inode *inode, struct file *filp,
  488. unsigned int cmd, unsigned long arg )
  489. {
  490. drm_file_t *priv = filp->private_data;
  491. drm_device_t *dev = priv->head->dev;
  492. drm_ctx_t ctx;
  493. if ( copy_from_user( &ctx, (drm_ctx_t __user *)arg, sizeof(ctx) ) )
  494. return -EFAULT;
  495. DRM_DEBUG( "%d\n", ctx.handle );
  496. if ( ctx.handle == DRM_KERNEL_CONTEXT + 1 ) {
  497. priv->remove_auth_on_close = 1;
  498. }
  499. if ( ctx.handle != DRM_KERNEL_CONTEXT ) {
  500. if (dev->driver->context_dtor)
  501. dev->driver->context_dtor(dev, ctx.handle);
  502. drm_ctxbitmap_free( dev, ctx.handle );
  503. }
  504. down( &dev->ctxlist_sem );
  505. if ( !list_empty( &dev->ctxlist->head ) ) {
  506. drm_ctx_list_t *pos, *n;
  507. list_for_each_entry_safe( pos, n, &dev->ctxlist->head, head ) {
  508. if ( pos->handle == ctx.handle ) {
  509. list_del( &pos->head );
  510. drm_free( pos, sizeof(*pos), DRM_MEM_CTXLIST );
  511. --dev->ctx_count;
  512. }
  513. }
  514. }
  515. up( &dev->ctxlist_sem );
  516. return 0;
  517. }
  518. /*@}*/