drm_fops.c 12 KB

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