drm_fops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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->driver_features & DRIVER_GEM)
  224. drm_gem_open(dev, priv);
  225. if (dev->driver->open) {
  226. ret = dev->driver->open(dev, priv);
  227. if (ret < 0)
  228. goto out_free;
  229. }
  230. mutex_lock(&dev->struct_mutex);
  231. if (list_empty(&dev->filelist))
  232. priv->master = 1;
  233. list_add(&priv->lhead, &dev->filelist);
  234. mutex_unlock(&dev->struct_mutex);
  235. #ifdef __alpha__
  236. /*
  237. * Default the hose
  238. */
  239. if (!dev->hose) {
  240. struct pci_dev *pci_dev;
  241. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  242. if (pci_dev) {
  243. dev->hose = pci_dev->sysdata;
  244. pci_dev_put(pci_dev);
  245. }
  246. if (!dev->hose) {
  247. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  248. if (b)
  249. dev->hose = b->sysdata;
  250. }
  251. }
  252. #endif
  253. return 0;
  254. out_free:
  255. drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
  256. filp->private_data = NULL;
  257. return ret;
  258. }
  259. /** No-op. */
  260. int drm_fasync(int fd, struct file *filp, int on)
  261. {
  262. struct drm_file *priv = filp->private_data;
  263. struct drm_device *dev = priv->minor->dev;
  264. int retcode;
  265. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
  266. (long)old_encode_dev(priv->minor->device));
  267. retcode = fasync_helper(fd, filp, on, &dev->buf_async);
  268. if (retcode < 0)
  269. return retcode;
  270. return 0;
  271. }
  272. EXPORT_SYMBOL(drm_fasync);
  273. /**
  274. * Release file.
  275. *
  276. * \param inode device inode
  277. * \param file_priv DRM file private.
  278. * \return zero on success or a negative number on failure.
  279. *
  280. * If the hardware lock is held then free it, and take it again for the kernel
  281. * context since it's necessary to reclaim buffers. Unlink the file private
  282. * data from its list and free it. Decreases the open count and if it reaches
  283. * zero calls drm_lastclose().
  284. */
  285. int drm_release(struct inode *inode, struct file *filp)
  286. {
  287. struct drm_file *file_priv = filp->private_data;
  288. struct drm_device *dev = file_priv->minor->dev;
  289. int retcode = 0;
  290. lock_kernel();
  291. DRM_DEBUG("open_count = %d\n", dev->open_count);
  292. if (dev->driver->preclose)
  293. dev->driver->preclose(dev, file_priv);
  294. /* ========================================================
  295. * Begin inline drm_release
  296. */
  297. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  298. task_pid_nr(current),
  299. (long)old_encode_dev(file_priv->minor->device),
  300. dev->open_count);
  301. if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
  302. if (drm_i_have_hw_lock(dev, file_priv)) {
  303. dev->driver->reclaim_buffers_locked(dev, file_priv);
  304. } else {
  305. unsigned long endtime = jiffies + 3 * DRM_HZ;
  306. int locked = 0;
  307. drm_idlelock_take(&dev->lock);
  308. /*
  309. * Wait for a while.
  310. */
  311. do{
  312. spin_lock_bh(&dev->lock.spinlock);
  313. locked = dev->lock.idle_has_lock;
  314. spin_unlock_bh(&dev->lock.spinlock);
  315. if (locked)
  316. break;
  317. schedule();
  318. } while (!time_after_eq(jiffies, endtime));
  319. if (!locked) {
  320. DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
  321. "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
  322. "\tI will go on reclaiming the buffers anyway.\n");
  323. }
  324. dev->driver->reclaim_buffers_locked(dev, file_priv);
  325. drm_idlelock_release(&dev->lock);
  326. }
  327. }
  328. if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
  329. drm_idlelock_take(&dev->lock);
  330. dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
  331. drm_idlelock_release(&dev->lock);
  332. }
  333. if (drm_i_have_hw_lock(dev, file_priv)) {
  334. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  335. filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
  336. drm_lock_free(&dev->lock,
  337. _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
  338. }
  339. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  340. !dev->driver->reclaim_buffers_locked) {
  341. dev->driver->reclaim_buffers(dev, file_priv);
  342. }
  343. if (dev->driver->driver_features & DRIVER_GEM)
  344. drm_gem_release(dev, file_priv);
  345. mutex_lock(&dev->ctxlist_mutex);
  346. if (!list_empty(&dev->ctxlist)) {
  347. struct drm_ctx_list *pos, *n;
  348. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  349. if (pos->tag == file_priv &&
  350. pos->handle != DRM_KERNEL_CONTEXT) {
  351. if (dev->driver->context_dtor)
  352. dev->driver->context_dtor(dev,
  353. pos->handle);
  354. drm_ctxbitmap_free(dev, pos->handle);
  355. list_del(&pos->head);
  356. drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
  357. --dev->ctx_count;
  358. }
  359. }
  360. }
  361. mutex_unlock(&dev->ctxlist_mutex);
  362. mutex_lock(&dev->struct_mutex);
  363. if (file_priv->remove_auth_on_close == 1) {
  364. struct drm_file *temp;
  365. list_for_each_entry(temp, &dev->filelist, lhead)
  366. temp->authenticated = 0;
  367. }
  368. list_del(&file_priv->lhead);
  369. mutex_unlock(&dev->struct_mutex);
  370. if (dev->driver->postclose)
  371. dev->driver->postclose(dev, file_priv);
  372. drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
  373. /* ========================================================
  374. * End inline drm_release
  375. */
  376. atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
  377. spin_lock(&dev->count_lock);
  378. if (!--dev->open_count) {
  379. if (atomic_read(&dev->ioctl_count) || dev->blocked) {
  380. DRM_ERROR("Device busy: %d %d\n",
  381. atomic_read(&dev->ioctl_count), dev->blocked);
  382. spin_unlock(&dev->count_lock);
  383. unlock_kernel();
  384. return -EBUSY;
  385. }
  386. spin_unlock(&dev->count_lock);
  387. unlock_kernel();
  388. return drm_lastclose(dev);
  389. }
  390. spin_unlock(&dev->count_lock);
  391. unlock_kernel();
  392. return retcode;
  393. }
  394. EXPORT_SYMBOL(drm_release);
  395. /** No-op. */
  396. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  397. {
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(drm_poll);