drm_fops.c 11 KB

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