drm_fops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * \file drm_fops.h
  3. * File operations for DRM
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Daryll Strauss <daryll@valinux.com>
  7. * \author Gareth Hughes <gareth@valinux.com>
  8. */
  9. /*
  10. * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
  11. *
  12. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  13. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  14. * All Rights Reserved.
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files (the "Software"),
  18. * to deal in the Software without restriction, including without limitation
  19. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. * and/or sell copies of the Software, and to permit persons to whom the
  21. * Software is furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice (including the next
  24. * paragraph) shall be included in all copies or substantial portions of the
  25. * Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  31. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  32. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33. * OTHER DEALINGS IN THE SOFTWARE.
  34. */
  35. #include "drmP.h"
  36. #include <linux/poll.h>
  37. static int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev);
  38. static int drm_setup( drm_device_t *dev )
  39. {
  40. int i;
  41. int ret;
  42. if (dev->driver->presetup)
  43. {
  44. ret=dev->driver->presetup(dev);
  45. if (ret!=0)
  46. return ret;
  47. }
  48. atomic_set( &dev->ioctl_count, 0 );
  49. atomic_set( &dev->vma_count, 0 );
  50. dev->buf_use = 0;
  51. atomic_set( &dev->buf_alloc, 0 );
  52. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  53. {
  54. i = drm_dma_setup( dev );
  55. if ( i < 0 )
  56. return i;
  57. }
  58. for ( i = 0 ; i < DRM_ARRAY_SIZE(dev->counts) ; i++ )
  59. atomic_set( &dev->counts[i], 0 );
  60. for ( i = 0 ; i < DRM_HASH_SIZE ; i++ ) {
  61. dev->magiclist[i].head = NULL;
  62. dev->magiclist[i].tail = NULL;
  63. }
  64. dev->maplist = drm_alloc(sizeof(*dev->maplist),
  65. DRM_MEM_MAPS);
  66. if(dev->maplist == NULL) return -ENOMEM;
  67. memset(dev->maplist, 0, sizeof(*dev->maplist));
  68. INIT_LIST_HEAD(&dev->maplist->head);
  69. dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist),
  70. DRM_MEM_CTXLIST);
  71. if(dev->ctxlist == NULL) return -ENOMEM;
  72. memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
  73. INIT_LIST_HEAD(&dev->ctxlist->head);
  74. dev->vmalist = NULL;
  75. dev->sigdata.lock = dev->lock.hw_lock = NULL;
  76. init_waitqueue_head( &dev->lock.lock_queue );
  77. dev->queue_count = 0;
  78. dev->queue_reserved = 0;
  79. dev->queue_slots = 0;
  80. dev->queuelist = NULL;
  81. dev->irq_enabled = 0;
  82. dev->context_flag = 0;
  83. dev->interrupt_flag = 0;
  84. dev->dma_flag = 0;
  85. dev->last_context = 0;
  86. dev->last_switch = 0;
  87. dev->last_checked = 0;
  88. init_waitqueue_head( &dev->context_wait );
  89. dev->if_version = 0;
  90. dev->ctx_start = 0;
  91. dev->lck_start = 0;
  92. dev->buf_rp = dev->buf;
  93. dev->buf_wp = dev->buf;
  94. dev->buf_end = dev->buf + DRM_BSZ;
  95. dev->buf_async = NULL;
  96. init_waitqueue_head( &dev->buf_readers );
  97. init_waitqueue_head( &dev->buf_writers );
  98. DRM_DEBUG( "\n" );
  99. /*
  100. * The kernel's context could be created here, but is now created
  101. * in drm_dma_enqueue. This is more resource-efficient for
  102. * hardware that does not do DMA, but may mean that
  103. * drm_select_queue fails between the time the interrupt is
  104. * initialized and the time the queues are initialized.
  105. */
  106. if (dev->driver->postsetup)
  107. dev->driver->postsetup(dev);
  108. return 0;
  109. }
  110. /**
  111. * Open file.
  112. *
  113. * \param inode device inode
  114. * \param filp file pointer.
  115. * \return zero on success or a negative number on failure.
  116. *
  117. * Searches the DRM device with the same minor number, calls open_helper(), and
  118. * increments the device open count. If the open count was previous at zero,
  119. * i.e., it's the first that the device is open, then calls setup().
  120. */
  121. int drm_open( struct inode *inode, struct file *filp )
  122. {
  123. drm_device_t *dev = NULL;
  124. int minor = iminor(inode);
  125. int retcode = 0;
  126. if (!((minor >= 0) && (minor < drm_cards_limit)))
  127. return -ENODEV;
  128. if (!drm_heads[minor])
  129. return -ENODEV;
  130. if (!(dev = drm_heads[minor]->dev))
  131. return -ENODEV;
  132. retcode = drm_open_helper( inode, filp, dev );
  133. if ( !retcode ) {
  134. atomic_inc( &dev->counts[_DRM_STAT_OPENS] );
  135. spin_lock( &dev->count_lock );
  136. if ( !dev->open_count++ ) {
  137. spin_unlock( &dev->count_lock );
  138. return drm_setup( dev );
  139. }
  140. spin_unlock( &dev->count_lock );
  141. }
  142. return retcode;
  143. }
  144. EXPORT_SYMBOL(drm_open);
  145. /**
  146. * Release file.
  147. *
  148. * \param inode device inode
  149. * \param filp file pointer.
  150. * \return zero on success or a negative number on failure.
  151. *
  152. * If the hardware lock is held then free it, and take it again for the kernel
  153. * context since it's necessary to reclaim buffers. Unlink the file private
  154. * data from its list and free it. Decreases the open count and if it reaches
  155. * zero calls takedown().
  156. */
  157. int drm_release( struct inode *inode, struct file *filp )
  158. {
  159. drm_file_t *priv = filp->private_data;
  160. drm_device_t *dev;
  161. int retcode = 0;
  162. lock_kernel();
  163. dev = priv->head->dev;
  164. DRM_DEBUG( "open_count = %d\n", dev->open_count );
  165. if (dev->driver->prerelease)
  166. dev->driver->prerelease(dev, filp);
  167. /* ========================================================
  168. * Begin inline drm_release
  169. */
  170. DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
  171. current->pid, (long)old_encode_dev(priv->head->device), dev->open_count );
  172. if ( priv->lock_count && dev->lock.hw_lock &&
  173. _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
  174. dev->lock.filp == filp ) {
  175. DRM_DEBUG( "File %p released, freeing lock for context %d\n",
  176. filp,
  177. _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
  178. if (dev->driver->release)
  179. dev->driver->release(dev, filp);
  180. drm_lock_free( dev, &dev->lock.hw_lock->lock,
  181. _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
  182. /* FIXME: may require heavy-handed reset of
  183. hardware at this point, possibly
  184. processed via a callback to the X
  185. server. */
  186. }
  187. else if ( dev->driver->release && priv->lock_count && dev->lock.hw_lock ) {
  188. /* The lock is required to reclaim buffers */
  189. DECLARE_WAITQUEUE( entry, current );
  190. add_wait_queue( &dev->lock.lock_queue, &entry );
  191. for (;;) {
  192. __set_current_state(TASK_INTERRUPTIBLE);
  193. if ( !dev->lock.hw_lock ) {
  194. /* Device has been unregistered */
  195. retcode = -EINTR;
  196. break;
  197. }
  198. if ( drm_lock_take( &dev->lock.hw_lock->lock,
  199. DRM_KERNEL_CONTEXT ) ) {
  200. dev->lock.filp = filp;
  201. dev->lock.lock_time = jiffies;
  202. atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
  203. break; /* Got lock */
  204. }
  205. /* Contention */
  206. schedule();
  207. if ( signal_pending( current ) ) {
  208. retcode = -ERESTARTSYS;
  209. break;
  210. }
  211. }
  212. __set_current_state(TASK_RUNNING);
  213. remove_wait_queue( &dev->lock.lock_queue, &entry );
  214. if( !retcode ) {
  215. if (dev->driver->release)
  216. dev->driver->release(dev, filp);
  217. drm_lock_free( dev, &dev->lock.hw_lock->lock,
  218. DRM_KERNEL_CONTEXT );
  219. }
  220. }
  221. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && !dev->driver->release)
  222. {
  223. dev->driver->reclaim_buffers(dev, filp);
  224. }
  225. drm_fasync( -1, filp, 0 );
  226. down( &dev->ctxlist_sem );
  227. if ( dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
  228. drm_ctx_list_t *pos, *n;
  229. list_for_each_entry_safe( pos, n, &dev->ctxlist->head, head ) {
  230. if ( pos->tag == priv &&
  231. pos->handle != DRM_KERNEL_CONTEXT ) {
  232. if (dev->driver->context_dtor)
  233. dev->driver->context_dtor(dev, pos->handle);
  234. drm_ctxbitmap_free( dev, pos->handle );
  235. list_del( &pos->head );
  236. drm_free( pos, sizeof(*pos), DRM_MEM_CTXLIST );
  237. --dev->ctx_count;
  238. }
  239. }
  240. }
  241. up( &dev->ctxlist_sem );
  242. down( &dev->struct_sem );
  243. if ( priv->remove_auth_on_close == 1 ) {
  244. drm_file_t *temp = dev->file_first;
  245. while ( temp ) {
  246. temp->authenticated = 0;
  247. temp = temp->next;
  248. }
  249. }
  250. if ( priv->prev ) {
  251. priv->prev->next = priv->next;
  252. } else {
  253. dev->file_first = priv->next;
  254. }
  255. if ( priv->next ) {
  256. priv->next->prev = priv->prev;
  257. } else {
  258. dev->file_last = priv->prev;
  259. }
  260. up( &dev->struct_sem );
  261. if (dev->driver->free_filp_priv)
  262. dev->driver->free_filp_priv(dev, priv);
  263. drm_free( priv, sizeof(*priv), DRM_MEM_FILES );
  264. /* ========================================================
  265. * End inline drm_release
  266. */
  267. atomic_inc( &dev->counts[_DRM_STAT_CLOSES] );
  268. spin_lock( &dev->count_lock );
  269. if ( !--dev->open_count ) {
  270. if ( atomic_read( &dev->ioctl_count ) || dev->blocked ) {
  271. DRM_ERROR( "Device busy: %d %d\n",
  272. atomic_read( &dev->ioctl_count ),
  273. dev->blocked );
  274. spin_unlock( &dev->count_lock );
  275. unlock_kernel();
  276. return -EBUSY;
  277. }
  278. spin_unlock( &dev->count_lock );
  279. unlock_kernel();
  280. return drm_takedown( dev );
  281. }
  282. spin_unlock( &dev->count_lock );
  283. unlock_kernel();
  284. return retcode;
  285. }
  286. EXPORT_SYMBOL(drm_release);
  287. /**
  288. * Called whenever a process opens /dev/drm.
  289. *
  290. * \param inode device inode.
  291. * \param filp file pointer.
  292. * \param dev device.
  293. * \return zero on success or a negative number on failure.
  294. *
  295. * Creates and initializes a drm_file structure for the file private data in \p
  296. * filp and add it into the double linked list in \p dev.
  297. */
  298. static int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev)
  299. {
  300. int minor = iminor(inode);
  301. drm_file_t *priv;
  302. int ret;
  303. if (filp->f_flags & O_EXCL) return -EBUSY; /* No exclusive opens */
  304. if (!drm_cpu_valid()) return -EINVAL;
  305. DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
  306. priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
  307. if(!priv) return -ENOMEM;
  308. memset(priv, 0, sizeof(*priv));
  309. filp->private_data = priv;
  310. priv->uid = current->euid;
  311. priv->pid = current->pid;
  312. priv->minor = minor;
  313. priv->head = drm_heads[minor];
  314. priv->ioctl_count = 0;
  315. priv->authenticated = capable(CAP_SYS_ADMIN);
  316. priv->lock_count = 0;
  317. if (dev->driver->open_helper) {
  318. ret=dev->driver->open_helper(dev, priv);
  319. if (ret < 0)
  320. goto out_free;
  321. }
  322. down(&dev->struct_sem);
  323. if (!dev->file_last) {
  324. priv->next = NULL;
  325. priv->prev = NULL;
  326. dev->file_first = priv;
  327. dev->file_last = priv;
  328. } else {
  329. priv->next = NULL;
  330. priv->prev = dev->file_last;
  331. dev->file_last->next = priv;
  332. dev->file_last = priv;
  333. }
  334. up(&dev->struct_sem);
  335. #ifdef __alpha__
  336. /*
  337. * Default the hose
  338. */
  339. if (!dev->hose) {
  340. struct pci_dev *pci_dev;
  341. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  342. if (pci_dev) {
  343. dev->hose = pci_dev->sysdata;
  344. pci_dev_put(pci_dev);
  345. }
  346. if (!dev->hose) {
  347. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  348. if (b) dev->hose = b->sysdata;
  349. }
  350. }
  351. #endif
  352. return 0;
  353. out_free:
  354. drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
  355. filp->private_data=NULL;
  356. return ret;
  357. }
  358. /** No-op. */
  359. int drm_flush(struct file *filp)
  360. {
  361. drm_file_t *priv = filp->private_data;
  362. drm_device_t *dev = priv->head->dev;
  363. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  364. current->pid, (long)old_encode_dev(priv->head->device), dev->open_count);
  365. return 0;
  366. }
  367. EXPORT_SYMBOL(drm_flush);
  368. /** No-op. */
  369. int drm_fasync(int fd, struct file *filp, int on)
  370. {
  371. drm_file_t *priv = filp->private_data;
  372. drm_device_t *dev = priv->head->dev;
  373. int retcode;
  374. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)old_encode_dev(priv->head->device));
  375. retcode = fasync_helper(fd, filp, on, &dev->buf_async);
  376. if (retcode < 0) return retcode;
  377. return 0;
  378. }
  379. EXPORT_SYMBOL(drm_fasync);
  380. /** No-op. */
  381. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  382. {
  383. return 0;
  384. }
  385. EXPORT_SYMBOL(drm_poll);