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