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