drm_fops.c 12 KB

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