drm_fops.c 11 KB

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