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