drm_fops.c 12 KB

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