drm_context.c 15 KB

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