drm_fops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 "drm_sarea.h"
  37. #include <linux/poll.h>
  38. #include <linux/smp_lock.h>
  39. static int drm_open_helper(struct inode *inode, struct file *filp,
  40. struct drm_device * dev);
  41. static int drm_setup(struct drm_device * dev)
  42. {
  43. drm_local_map_t *map;
  44. int i;
  45. int ret;
  46. u32 sareapage;
  47. if (dev->driver->firstopen) {
  48. ret = dev->driver->firstopen(dev);
  49. if (ret != 0)
  50. return ret;
  51. }
  52. dev->magicfree.next = NULL;
  53. /* prebuild the SAREA */
  54. sareapage = max_t(unsigned, SAREA_MAX, PAGE_SIZE);
  55. i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
  56. if (i != 0)
  57. return i;
  58. atomic_set(&dev->ioctl_count, 0);
  59. atomic_set(&dev->vma_count, 0);
  60. dev->buf_use = 0;
  61. atomic_set(&dev->buf_alloc, 0);
  62. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
  63. i = drm_dma_setup(dev);
  64. if (i < 0)
  65. return i;
  66. }
  67. for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
  68. atomic_set(&dev->counts[i], 0);
  69. drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
  70. INIT_LIST_HEAD(&dev->magicfree);
  71. dev->sigdata.lock = NULL;
  72. init_waitqueue_head(&dev->lock.lock_queue);
  73. dev->queue_count = 0;
  74. dev->queue_reserved = 0;
  75. dev->queue_slots = 0;
  76. dev->queuelist = NULL;
  77. dev->irq_enabled = 0;
  78. dev->context_flag = 0;
  79. dev->interrupt_flag = 0;
  80. dev->dma_flag = 0;
  81. dev->last_context = 0;
  82. dev->last_switch = 0;
  83. dev->last_checked = 0;
  84. init_waitqueue_head(&dev->context_wait);
  85. dev->if_version = 0;
  86. dev->ctx_start = 0;
  87. dev->lck_start = 0;
  88. dev->buf_async = NULL;
  89. init_waitqueue_head(&dev->buf_readers);
  90. init_waitqueue_head(&dev->buf_writers);
  91. DRM_DEBUG("\n");
  92. /*
  93. * The kernel's context could be created here, but is now created
  94. * in drm_dma_enqueue. This is more resource-efficient for
  95. * hardware that does not do DMA, but may mean that
  96. * drm_select_queue fails between the time the interrupt is
  97. * initialized and the time the queues are initialized.
  98. */
  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. struct drm_device *dev = NULL;
  115. int minor_id = iminor(inode);
  116. struct drm_minor *minor;
  117. int retcode = 0;
  118. minor = idr_find(&drm_minors_idr, minor_id);
  119. if (!minor)
  120. return -ENODEV;
  121. if (!(dev = 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. * File \c open operation.
  138. *
  139. * \param inode device inode.
  140. * \param filp file pointer.
  141. *
  142. * Puts the dev->fops corresponding to the device minor number into
  143. * \p filp, call the \c open method, and restore the file operations.
  144. */
  145. int drm_stub_open(struct inode *inode, struct file *filp)
  146. {
  147. struct drm_device *dev = NULL;
  148. struct drm_minor *minor;
  149. int minor_id = iminor(inode);
  150. int err = -ENODEV;
  151. const struct file_operations *old_fops;
  152. DRM_DEBUG("\n");
  153. /* BKL pushdown: note that nothing else serializes idr_find() */
  154. lock_kernel();
  155. minor = idr_find(&drm_minors_idr, minor_id);
  156. if (!minor)
  157. goto out;
  158. if (!(dev = minor->dev))
  159. goto out;
  160. old_fops = filp->f_op;
  161. filp->f_op = fops_get(&dev->driver->fops);
  162. if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
  163. fops_put(filp->f_op);
  164. filp->f_op = fops_get(old_fops);
  165. }
  166. fops_put(old_fops);
  167. out:
  168. unlock_kernel();
  169. return err;
  170. }
  171. /**
  172. * Check whether DRI will run on this CPU.
  173. *
  174. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  175. */
  176. static int drm_cpu_valid(void)
  177. {
  178. #if defined(__i386__)
  179. if (boot_cpu_data.x86 == 3)
  180. return 0; /* No cmpxchg on a 386 */
  181. #endif
  182. #if defined(__sparc__) && !defined(__sparc_v9__)
  183. return 0; /* No cmpxchg before v9 sparc. */
  184. #endif
  185. return 1;
  186. }
  187. /**
  188. * Called whenever a process opens /dev/drm.
  189. *
  190. * \param inode device inode.
  191. * \param filp file pointer.
  192. * \param dev device.
  193. * \return zero on success or a negative number on failure.
  194. *
  195. * Creates and initializes a drm_file structure for the file private data in \p
  196. * filp and add it into the double linked list in \p dev.
  197. */
  198. static int drm_open_helper(struct inode *inode, struct file *filp,
  199. struct drm_device * dev)
  200. {
  201. int minor_id = iminor(inode);
  202. struct drm_file *priv;
  203. int ret;
  204. if (filp->f_flags & O_EXCL)
  205. return -EBUSY; /* No exclusive opens */
  206. if (!drm_cpu_valid())
  207. return -EINVAL;
  208. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
  209. priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
  210. if (!priv)
  211. return -ENOMEM;
  212. memset(priv, 0, sizeof(*priv));
  213. filp->private_data = priv;
  214. priv->filp = filp;
  215. priv->uid = current->euid;
  216. priv->pid = task_pid_nr(current);
  217. priv->minor = idr_find(&drm_minors_idr, minor_id);
  218. priv->ioctl_count = 0;
  219. /* for compatibility root is always authenticated */
  220. priv->authenticated = capable(CAP_SYS_ADMIN);
  221. priv->lock_count = 0;
  222. INIT_LIST_HEAD(&priv->lhead);
  223. if (dev->driver->open) {
  224. ret = dev->driver->open(dev, priv);
  225. if (ret < 0)
  226. goto out_free;
  227. }
  228. mutex_lock(&dev->struct_mutex);
  229. if (list_empty(&dev->filelist))
  230. priv->master = 1;
  231. list_add(&priv->lhead, &dev->filelist);
  232. mutex_unlock(&dev->struct_mutex);
  233. #ifdef __alpha__
  234. /*
  235. * Default the hose
  236. */
  237. if (!dev->hose) {
  238. struct pci_dev *pci_dev;
  239. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  240. if (pci_dev) {
  241. dev->hose = pci_dev->sysdata;
  242. pci_dev_put(pci_dev);
  243. }
  244. if (!dev->hose) {
  245. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  246. if (b)
  247. dev->hose = b->sysdata;
  248. }
  249. }
  250. #endif
  251. return 0;
  252. out_free:
  253. drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
  254. filp->private_data = NULL;
  255. return ret;
  256. }
  257. /** No-op. */
  258. int drm_fasync(int fd, struct file *filp, int on)
  259. {
  260. struct drm_file *priv = filp->private_data;
  261. struct drm_device *dev = priv->minor->dev;
  262. int retcode;
  263. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
  264. (long)old_encode_dev(priv->minor->device));
  265. retcode = fasync_helper(fd, filp, on, &dev->buf_async);
  266. if (retcode < 0)
  267. return retcode;
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(drm_fasync);
  271. /**
  272. * Release file.
  273. *
  274. * \param inode device inode
  275. * \param file_priv DRM file private.
  276. * \return zero on success or a negative number on failure.
  277. *
  278. * If the hardware lock is held then free it, and take it again for the kernel
  279. * context since it's necessary to reclaim buffers. Unlink the file private
  280. * data from its list and free it. Decreases the open count and if it reaches
  281. * zero calls drm_lastclose().
  282. */
  283. int drm_release(struct inode *inode, struct file *filp)
  284. {
  285. struct drm_file *file_priv = filp->private_data;
  286. struct drm_device *dev = file_priv->minor->dev;
  287. int retcode = 0;
  288. lock_kernel();
  289. DRM_DEBUG("open_count = %d\n", dev->open_count);
  290. if (dev->driver->preclose)
  291. dev->driver->preclose(dev, file_priv);
  292. /* ========================================================
  293. * Begin inline drm_release
  294. */
  295. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  296. task_pid_nr(current),
  297. (long)old_encode_dev(file_priv->minor->device),
  298. dev->open_count);
  299. if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
  300. if (drm_i_have_hw_lock(dev, file_priv)) {
  301. dev->driver->reclaim_buffers_locked(dev, file_priv);
  302. } else {
  303. unsigned long endtime = jiffies + 3 * DRM_HZ;
  304. int locked = 0;
  305. drm_idlelock_take(&dev->lock);
  306. /*
  307. * Wait for a while.
  308. */
  309. do{
  310. spin_lock_bh(&dev->lock.spinlock);
  311. locked = dev->lock.idle_has_lock;
  312. spin_unlock_bh(&dev->lock.spinlock);
  313. if (locked)
  314. break;
  315. schedule();
  316. } while (!time_after_eq(jiffies, endtime));
  317. if (!locked) {
  318. DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
  319. "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
  320. "\tI will go on reclaiming the buffers anyway.\n");
  321. }
  322. dev->driver->reclaim_buffers_locked(dev, file_priv);
  323. drm_idlelock_release(&dev->lock);
  324. }
  325. }
  326. if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
  327. drm_idlelock_take(&dev->lock);
  328. dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
  329. drm_idlelock_release(&dev->lock);
  330. }
  331. if (drm_i_have_hw_lock(dev, file_priv)) {
  332. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  333. filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
  334. drm_lock_free(&dev->lock,
  335. _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
  336. }
  337. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  338. !dev->driver->reclaim_buffers_locked) {
  339. dev->driver->reclaim_buffers(dev, file_priv);
  340. }
  341. drm_fasync(-1, filp, 0);
  342. mutex_lock(&dev->ctxlist_mutex);
  343. if (!list_empty(&dev->ctxlist)) {
  344. struct drm_ctx_list *pos, *n;
  345. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  346. if (pos->tag == file_priv &&
  347. pos->handle != DRM_KERNEL_CONTEXT) {
  348. if (dev->driver->context_dtor)
  349. dev->driver->context_dtor(dev,
  350. pos->handle);
  351. drm_ctxbitmap_free(dev, pos->handle);
  352. list_del(&pos->head);
  353. drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
  354. --dev->ctx_count;
  355. }
  356. }
  357. }
  358. mutex_unlock(&dev->ctxlist_mutex);
  359. mutex_lock(&dev->struct_mutex);
  360. if (file_priv->remove_auth_on_close == 1) {
  361. struct drm_file *temp;
  362. list_for_each_entry(temp, &dev->filelist, lhead)
  363. temp->authenticated = 0;
  364. }
  365. list_del(&file_priv->lhead);
  366. mutex_unlock(&dev->struct_mutex);
  367. if (dev->driver->postclose)
  368. dev->driver->postclose(dev, file_priv);
  369. drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
  370. /* ========================================================
  371. * End inline drm_release
  372. */
  373. atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
  374. spin_lock(&dev->count_lock);
  375. if (!--dev->open_count) {
  376. if (atomic_read(&dev->ioctl_count) || dev->blocked) {
  377. DRM_ERROR("Device busy: %d %d\n",
  378. atomic_read(&dev->ioctl_count), dev->blocked);
  379. spin_unlock(&dev->count_lock);
  380. unlock_kernel();
  381. return -EBUSY;
  382. }
  383. spin_unlock(&dev->count_lock);
  384. unlock_kernel();
  385. return drm_lastclose(dev);
  386. }
  387. spin_unlock(&dev->count_lock);
  388. unlock_kernel();
  389. return retcode;
  390. }
  391. EXPORT_SYMBOL(drm_release);
  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);