drm_fops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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->ctxlist = drm_alloc(sizeof(*dev->ctxlist),
  65. DRM_MEM_CTXLIST);
  66. if(dev->ctxlist == NULL) return -ENOMEM;
  67. memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
  68. INIT_LIST_HEAD(&dev->ctxlist->head);
  69. dev->vmalist = NULL;
  70. dev->sigdata.lock = dev->lock.hw_lock = NULL;
  71. init_waitqueue_head( &dev->lock.lock_queue );
  72. dev->queue_count = 0;
  73. dev->queue_reserved = 0;
  74. dev->queue_slots = 0;
  75. dev->queuelist = NULL;
  76. dev->irq_enabled = 0;
  77. dev->context_flag = 0;
  78. dev->interrupt_flag = 0;
  79. dev->dma_flag = 0;
  80. dev->last_context = 0;
  81. dev->last_switch = 0;
  82. dev->last_checked = 0;
  83. init_waitqueue_head( &dev->context_wait );
  84. dev->if_version = 0;
  85. dev->ctx_start = 0;
  86. dev->lck_start = 0;
  87. dev->buf_rp = dev->buf;
  88. dev->buf_wp = dev->buf;
  89. dev->buf_end = dev->buf + DRM_BSZ;
  90. dev->buf_async = NULL;
  91. init_waitqueue_head( &dev->buf_readers );
  92. init_waitqueue_head( &dev->buf_writers );
  93. DRM_DEBUG( "\n" );
  94. /*
  95. * The kernel's context could be created here, but is now created
  96. * in drm_dma_enqueue. This is more resource-efficient for
  97. * hardware that does not do DMA, but may mean that
  98. * drm_select_queue fails between the time the interrupt is
  99. * initialized and the time the queues are initialized.
  100. */
  101. if (dev->driver->postsetup)
  102. dev->driver->postsetup(dev);
  103. return 0;
  104. }
  105. /**
  106. * Open file.
  107. *
  108. * \param inode device inode
  109. * \param filp file pointer.
  110. * \return zero on success or a negative number on failure.
  111. *
  112. * Searches the DRM device with the same minor number, calls open_helper(), and
  113. * increments the device open count. If the open count was previous at zero,
  114. * i.e., it's the first that the device is open, then calls setup().
  115. */
  116. int drm_open( struct inode *inode, struct file *filp )
  117. {
  118. drm_device_t *dev = NULL;
  119. int minor = iminor(inode);
  120. int retcode = 0;
  121. if (!((minor >= 0) && (minor < drm_cards_limit)))
  122. return -ENODEV;
  123. if (!drm_heads[minor])
  124. return -ENODEV;
  125. if (!(dev = drm_heads[minor]->dev))
  126. return -ENODEV;
  127. retcode = drm_open_helper( inode, filp, dev );
  128. if ( !retcode ) {
  129. atomic_inc( &dev->counts[_DRM_STAT_OPENS] );
  130. spin_lock( &dev->count_lock );
  131. if ( !dev->open_count++ ) {
  132. spin_unlock( &dev->count_lock );
  133. return drm_setup( dev );
  134. }
  135. spin_unlock( &dev->count_lock );
  136. }
  137. return retcode;
  138. }
  139. EXPORT_SYMBOL(drm_open);
  140. /**
  141. * Release file.
  142. *
  143. * \param inode device inode
  144. * \param filp file pointer.
  145. * \return zero on success or a negative number on failure.
  146. *
  147. * If the hardware lock is held then free it, and take it again for the kernel
  148. * context since it's necessary to reclaim buffers. Unlink the file private
  149. * data from its list and free it. Decreases the open count and if it reaches
  150. * zero calls takedown().
  151. */
  152. int drm_release( struct inode *inode, struct file *filp )
  153. {
  154. drm_file_t *priv = filp->private_data;
  155. drm_device_t *dev;
  156. int retcode = 0;
  157. lock_kernel();
  158. dev = priv->head->dev;
  159. DRM_DEBUG( "open_count = %d\n", dev->open_count );
  160. if (dev->driver->prerelease)
  161. dev->driver->prerelease(dev, filp);
  162. /* ========================================================
  163. * Begin inline drm_release
  164. */
  165. DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
  166. current->pid, (long)old_encode_dev(priv->head->device), dev->open_count );
  167. if ( priv->lock_count && dev->lock.hw_lock &&
  168. _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
  169. dev->lock.filp == filp ) {
  170. DRM_DEBUG( "File %p released, freeing lock for context %d\n",
  171. filp,
  172. _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
  173. if (dev->driver->release)
  174. dev->driver->release(dev, filp);
  175. drm_lock_free( dev, &dev->lock.hw_lock->lock,
  176. _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
  177. /* FIXME: may require heavy-handed reset of
  178. hardware at this point, possibly
  179. processed via a callback to the X
  180. server. */
  181. }
  182. else if ( dev->driver->release && priv->lock_count && dev->lock.hw_lock ) {
  183. /* The lock is required to reclaim buffers */
  184. DECLARE_WAITQUEUE( entry, current );
  185. add_wait_queue( &dev->lock.lock_queue, &entry );
  186. for (;;) {
  187. __set_current_state(TASK_INTERRUPTIBLE);
  188. if ( !dev->lock.hw_lock ) {
  189. /* Device has been unregistered */
  190. retcode = -EINTR;
  191. break;
  192. }
  193. if ( drm_lock_take( &dev->lock.hw_lock->lock,
  194. DRM_KERNEL_CONTEXT ) ) {
  195. dev->lock.filp = filp;
  196. dev->lock.lock_time = jiffies;
  197. atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
  198. break; /* Got lock */
  199. }
  200. /* Contention */
  201. schedule();
  202. if ( signal_pending( current ) ) {
  203. retcode = -ERESTARTSYS;
  204. break;
  205. }
  206. }
  207. __set_current_state(TASK_RUNNING);
  208. remove_wait_queue( &dev->lock.lock_queue, &entry );
  209. if( !retcode ) {
  210. if (dev->driver->release)
  211. dev->driver->release(dev, filp);
  212. drm_lock_free( dev, &dev->lock.hw_lock->lock,
  213. DRM_KERNEL_CONTEXT );
  214. }
  215. }
  216. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && !dev->driver->release)
  217. {
  218. dev->driver->reclaim_buffers(dev, filp);
  219. }
  220. drm_fasync( -1, filp, 0 );
  221. down( &dev->ctxlist_sem );
  222. if ( dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
  223. drm_ctx_list_t *pos, *n;
  224. list_for_each_entry_safe( pos, n, &dev->ctxlist->head, head ) {
  225. if ( pos->tag == priv &&
  226. pos->handle != DRM_KERNEL_CONTEXT ) {
  227. if (dev->driver->context_dtor)
  228. dev->driver->context_dtor(dev, pos->handle);
  229. drm_ctxbitmap_free( dev, pos->handle );
  230. list_del( &pos->head );
  231. drm_free( pos, sizeof(*pos), DRM_MEM_CTXLIST );
  232. --dev->ctx_count;
  233. }
  234. }
  235. }
  236. up( &dev->ctxlist_sem );
  237. down( &dev->struct_sem );
  238. if ( priv->remove_auth_on_close == 1 ) {
  239. drm_file_t *temp = dev->file_first;
  240. while ( temp ) {
  241. temp->authenticated = 0;
  242. temp = temp->next;
  243. }
  244. }
  245. if ( priv->prev ) {
  246. priv->prev->next = priv->next;
  247. } else {
  248. dev->file_first = priv->next;
  249. }
  250. if ( priv->next ) {
  251. priv->next->prev = priv->prev;
  252. } else {
  253. dev->file_last = priv->prev;
  254. }
  255. up( &dev->struct_sem );
  256. if (dev->driver->free_filp_priv)
  257. dev->driver->free_filp_priv(dev, priv);
  258. drm_free( priv, sizeof(*priv), DRM_MEM_FILES );
  259. /* ========================================================
  260. * End inline drm_release
  261. */
  262. atomic_inc( &dev->counts[_DRM_STAT_CLOSES] );
  263. spin_lock( &dev->count_lock );
  264. if ( !--dev->open_count ) {
  265. if ( atomic_read( &dev->ioctl_count ) || dev->blocked ) {
  266. DRM_ERROR( "Device busy: %d %d\n",
  267. atomic_read( &dev->ioctl_count ),
  268. dev->blocked );
  269. spin_unlock( &dev->count_lock );
  270. unlock_kernel();
  271. return -EBUSY;
  272. }
  273. spin_unlock( &dev->count_lock );
  274. unlock_kernel();
  275. return drm_takedown( dev );
  276. }
  277. spin_unlock( &dev->count_lock );
  278. unlock_kernel();
  279. return retcode;
  280. }
  281. EXPORT_SYMBOL(drm_release);
  282. /**
  283. * Called whenever a process opens /dev/drm.
  284. *
  285. * \param inode device inode.
  286. * \param filp file pointer.
  287. * \param dev device.
  288. * \return zero on success or a negative number on failure.
  289. *
  290. * Creates and initializes a drm_file structure for the file private data in \p
  291. * filp and add it into the double linked list in \p dev.
  292. */
  293. static int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev)
  294. {
  295. int minor = iminor(inode);
  296. drm_file_t *priv;
  297. int ret;
  298. if (filp->f_flags & O_EXCL) return -EBUSY; /* No exclusive opens */
  299. if (!drm_cpu_valid()) return -EINVAL;
  300. DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
  301. priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
  302. if(!priv) return -ENOMEM;
  303. memset(priv, 0, sizeof(*priv));
  304. filp->private_data = priv;
  305. priv->uid = current->euid;
  306. priv->pid = current->pid;
  307. priv->minor = minor;
  308. priv->head = drm_heads[minor];
  309. priv->ioctl_count = 0;
  310. priv->authenticated = capable(CAP_SYS_ADMIN);
  311. priv->lock_count = 0;
  312. if (dev->driver->open_helper) {
  313. ret=dev->driver->open_helper(dev, priv);
  314. if (ret < 0)
  315. goto out_free;
  316. }
  317. down(&dev->struct_sem);
  318. if (!dev->file_last) {
  319. priv->next = NULL;
  320. priv->prev = NULL;
  321. dev->file_first = priv;
  322. dev->file_last = priv;
  323. } else {
  324. priv->next = NULL;
  325. priv->prev = dev->file_last;
  326. dev->file_last->next = priv;
  327. dev->file_last = priv;
  328. }
  329. up(&dev->struct_sem);
  330. #ifdef __alpha__
  331. /*
  332. * Default the hose
  333. */
  334. if (!dev->hose) {
  335. struct pci_dev *pci_dev;
  336. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  337. if (pci_dev) {
  338. dev->hose = pci_dev->sysdata;
  339. pci_dev_put(pci_dev);
  340. }
  341. if (!dev->hose) {
  342. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  343. if (b) dev->hose = b->sysdata;
  344. }
  345. }
  346. #endif
  347. return 0;
  348. out_free:
  349. drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
  350. filp->private_data=NULL;
  351. return ret;
  352. }
  353. /** No-op. */
  354. int drm_flush(struct file *filp)
  355. {
  356. drm_file_t *priv = filp->private_data;
  357. drm_device_t *dev = priv->head->dev;
  358. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  359. current->pid, (long)old_encode_dev(priv->head->device), dev->open_count);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(drm_flush);
  363. /** No-op. */
  364. int drm_fasync(int fd, struct file *filp, int on)
  365. {
  366. drm_file_t *priv = filp->private_data;
  367. drm_device_t *dev = priv->head->dev;
  368. int retcode;
  369. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)old_encode_dev(priv->head->device));
  370. retcode = fasync_helper(fd, filp, on, &dev->buf_async);
  371. if (retcode < 0) return retcode;
  372. return 0;
  373. }
  374. EXPORT_SYMBOL(drm_fasync);
  375. /** No-op. */
  376. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  377. {
  378. return 0;
  379. }
  380. EXPORT_SYMBOL(drm_poll);