drm_fops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_rp = dev->buf;
  87. dev->buf_wp = dev->buf;
  88. dev->buf_end = dev->buf + DRM_BSZ;
  89. dev->buf_async = NULL;
  90. init_waitqueue_head(&dev->buf_readers);
  91. init_waitqueue_head(&dev->buf_writers);
  92. DRM_DEBUG("\n");
  93. /*
  94. * The kernel's context could be created here, but is now created
  95. * in drm_dma_enqueue. This is more resource-efficient for
  96. * hardware that does not do DMA, but may mean that
  97. * drm_select_queue fails between the time the interrupt is
  98. * initialized and the time the queues are initialized.
  99. */
  100. if (dev->driver->postsetup)
  101. dev->driver->postsetup(dev);
  102. return 0;
  103. }
  104. /**
  105. * Open file.
  106. *
  107. * \param inode device inode
  108. * \param filp file pointer.
  109. * \return zero on success or a negative number on failure.
  110. *
  111. * Searches the DRM device with the same minor number, calls open_helper(), and
  112. * increments the device open count. If the open count was previous at zero,
  113. * i.e., it's the first that the device is open, then calls setup().
  114. */
  115. int drm_open(struct inode *inode, struct file *filp)
  116. {
  117. drm_device_t *dev = NULL;
  118. int minor = iminor(inode);
  119. int retcode = 0;
  120. if (!((minor >= 0) && (minor < drm_cards_limit)))
  121. return -ENODEV;
  122. if (!drm_heads[minor])
  123. return -ENODEV;
  124. if (!(dev = drm_heads[minor]->dev))
  125. return -ENODEV;
  126. retcode = drm_open_helper(inode, filp, dev);
  127. if (!retcode) {
  128. atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
  129. spin_lock(&dev->count_lock);
  130. if (!dev->open_count++) {
  131. spin_unlock(&dev->count_lock);
  132. return drm_setup(dev);
  133. }
  134. spin_unlock(&dev->count_lock);
  135. }
  136. return retcode;
  137. }
  138. EXPORT_SYMBOL(drm_open);
  139. /**
  140. * Release file.
  141. *
  142. * \param inode device inode
  143. * \param filp file pointer.
  144. * \return zero on success or a negative number on failure.
  145. *
  146. * If the hardware lock is held then free it, and take it again for the kernel
  147. * context since it's necessary to reclaim buffers. Unlink the file private
  148. * data from its list and free it. Decreases the open count and if it reaches
  149. * zero calls takedown().
  150. */
  151. int drm_release(struct inode *inode, struct file *filp)
  152. {
  153. drm_file_t *priv = filp->private_data;
  154. drm_device_t *dev;
  155. int retcode = 0;
  156. lock_kernel();
  157. dev = priv->head->dev;
  158. DRM_DEBUG("open_count = %d\n", dev->open_count);
  159. if (dev->driver->prerelease)
  160. dev->driver->prerelease(dev, filp);
  161. /* ========================================================
  162. * Begin inline drm_release
  163. */
  164. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  165. current->pid, (long)old_encode_dev(priv->head->device),
  166. 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, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
  172. if (dev->driver->release)
  173. dev->driver->release(dev, filp);
  174. drm_lock_free(dev, &dev->lock.hw_lock->lock,
  175. _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
  176. /* FIXME: may require heavy-handed reset of
  177. hardware at this point, possibly
  178. processed via a callback to the X
  179. server. */
  180. } else if (dev->driver->release && priv->lock_count
  181. && dev->lock.hw_lock) {
  182. /* The lock is required to reclaim buffers */
  183. DECLARE_WAITQUEUE(entry, current);
  184. add_wait_queue(&dev->lock.lock_queue, &entry);
  185. for (;;) {
  186. __set_current_state(TASK_INTERRUPTIBLE);
  187. if (!dev->lock.hw_lock) {
  188. /* Device has been unregistered */
  189. retcode = -EINTR;
  190. break;
  191. }
  192. if (drm_lock_take(&dev->lock.hw_lock->lock,
  193. DRM_KERNEL_CONTEXT)) {
  194. dev->lock.filp = filp;
  195. dev->lock.lock_time = jiffies;
  196. atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
  197. break; /* Got lock */
  198. }
  199. /* Contention */
  200. schedule();
  201. if (signal_pending(current)) {
  202. retcode = -ERESTARTSYS;
  203. break;
  204. }
  205. }
  206. __set_current_state(TASK_RUNNING);
  207. remove_wait_queue(&dev->lock.lock_queue, &entry);
  208. if (!retcode) {
  209. if (dev->driver->release)
  210. dev->driver->release(dev, filp);
  211. drm_lock_free(dev, &dev->lock.hw_lock->lock,
  212. DRM_KERNEL_CONTEXT);
  213. }
  214. }
  215. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)
  216. && !dev->driver->release) {
  217. dev->driver->reclaim_buffers(dev, filp);
  218. }
  219. drm_fasync(-1, filp, 0);
  220. down(&dev->ctxlist_sem);
  221. if (dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
  222. drm_ctx_list_t *pos, *n;
  223. list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
  224. if (pos->tag == priv &&
  225. pos->handle != DRM_KERNEL_CONTEXT) {
  226. if (dev->driver->context_dtor)
  227. dev->driver->context_dtor(dev,
  228. 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), dev->blocked);
  268. spin_unlock(&dev->count_lock);
  269. unlock_kernel();
  270. return -EBUSY;
  271. }
  272. spin_unlock(&dev->count_lock);
  273. unlock_kernel();
  274. return drm_takedown(dev);
  275. }
  276. spin_unlock(&dev->count_lock);
  277. unlock_kernel();
  278. return retcode;
  279. }
  280. EXPORT_SYMBOL(drm_release);
  281. /**
  282. * Called whenever a process opens /dev/drm.
  283. *
  284. * \param inode device inode.
  285. * \param filp file pointer.
  286. * \param dev device.
  287. * \return zero on success or a negative number on failure.
  288. *
  289. * Creates and initializes a drm_file structure for the file private data in \p
  290. * filp and add it into the double linked list in \p dev.
  291. */
  292. static int drm_open_helper(struct inode *inode, struct file *filp,
  293. 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)
  299. return -EBUSY; /* No exclusive opens */
  300. if (!drm_cpu_valid())
  301. return -EINVAL;
  302. DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
  303. priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
  304. if (!priv)
  305. return -ENOMEM;
  306. memset(priv, 0, sizeof(*priv));
  307. filp->private_data = priv;
  308. priv->uid = current->euid;
  309. priv->pid = current->pid;
  310. priv->minor = minor;
  311. priv->head = drm_heads[minor];
  312. priv->ioctl_count = 0;
  313. priv->authenticated = capable(CAP_SYS_ADMIN);
  314. priv->lock_count = 0;
  315. if (dev->driver->open_helper) {
  316. ret = dev->driver->open_helper(dev, priv);
  317. if (ret < 0)
  318. goto out_free;
  319. }
  320. down(&dev->struct_sem);
  321. if (!dev->file_last) {
  322. priv->next = NULL;
  323. priv->prev = NULL;
  324. dev->file_first = priv;
  325. dev->file_last = priv;
  326. } else {
  327. priv->next = NULL;
  328. priv->prev = dev->file_last;
  329. dev->file_last->next = priv;
  330. dev->file_last = priv;
  331. }
  332. up(&dev->struct_sem);
  333. #ifdef __alpha__
  334. /*
  335. * Default the hose
  336. */
  337. if (!dev->hose) {
  338. struct pci_dev *pci_dev;
  339. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  340. if (pci_dev) {
  341. dev->hose = pci_dev->sysdata;
  342. pci_dev_put(pci_dev);
  343. }
  344. if (!dev->hose) {
  345. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  346. if (b)
  347. 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),
  364. 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,
  375. (long)old_encode_dev(priv->head->device));
  376. retcode = fasync_helper(fd, filp, on, &dev->buf_async);
  377. if (retcode < 0)
  378. return retcode;
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(drm_fasync);
  382. /** No-op. */
  383. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  384. {
  385. return 0;
  386. }
  387. EXPORT_SYMBOL(drm_poll);