drm_fops.c 12 KB

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