drm_fops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 <linux/poll.h>
  37. #include <linux/smp_lock.h>
  38. static int drm_open_helper(struct inode *inode, struct file *filp,
  39. struct drm_device * dev);
  40. static int drm_setup(struct drm_device * dev)
  41. {
  42. int i;
  43. int ret;
  44. if (dev->driver->firstopen) {
  45. ret = dev->driver->firstopen(dev);
  46. if (ret != 0)
  47. return ret;
  48. }
  49. atomic_set(&dev->ioctl_count, 0);
  50. atomic_set(&dev->vma_count, 0);
  51. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  52. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  53. dev->buf_use = 0;
  54. atomic_set(&dev->buf_alloc, 0);
  55. i = drm_dma_setup(dev);
  56. if (i < 0)
  57. return i;
  58. }
  59. for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
  60. atomic_set(&dev->counts[i], 0);
  61. dev->sigdata.lock = NULL;
  62. dev->queue_count = 0;
  63. dev->queue_reserved = 0;
  64. dev->queue_slots = 0;
  65. dev->queuelist = NULL;
  66. dev->context_flag = 0;
  67. dev->interrupt_flag = 0;
  68. dev->dma_flag = 0;
  69. dev->last_context = 0;
  70. dev->last_switch = 0;
  71. dev->last_checked = 0;
  72. init_waitqueue_head(&dev->context_wait);
  73. dev->if_version = 0;
  74. dev->ctx_start = 0;
  75. dev->lck_start = 0;
  76. dev->buf_async = NULL;
  77. init_waitqueue_head(&dev->buf_readers);
  78. init_waitqueue_head(&dev->buf_writers);
  79. DRM_DEBUG("\n");
  80. /*
  81. * The kernel's context could be created here, but is now created
  82. * in drm_dma_enqueue. This is more resource-efficient for
  83. * hardware that does not do DMA, but may mean that
  84. * drm_select_queue fails between the time the interrupt is
  85. * initialized and the time the queues are initialized.
  86. */
  87. return 0;
  88. }
  89. /**
  90. * Open file.
  91. *
  92. * \param inode device inode
  93. * \param filp file pointer.
  94. * \return zero on success or a negative number on failure.
  95. *
  96. * Searches the DRM device with the same minor number, calls open_helper(), and
  97. * increments the device open count. If the open count was previous at zero,
  98. * i.e., it's the first that the device is open, then calls setup().
  99. */
  100. int drm_open(struct inode *inode, struct file *filp)
  101. {
  102. struct drm_device *dev = NULL;
  103. int minor_id = iminor(inode);
  104. struct drm_minor *minor;
  105. int retcode = 0;
  106. minor = idr_find(&drm_minors_idr, minor_id);
  107. if (!minor)
  108. return -ENODEV;
  109. if (!(dev = minor->dev))
  110. return -ENODEV;
  111. retcode = drm_open_helper(inode, filp, dev);
  112. if (!retcode) {
  113. atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
  114. spin_lock(&dev->count_lock);
  115. if (!dev->open_count++) {
  116. spin_unlock(&dev->count_lock);
  117. retcode = drm_setup(dev);
  118. goto out;
  119. }
  120. spin_unlock(&dev->count_lock);
  121. }
  122. out:
  123. mutex_lock(&dev->struct_mutex);
  124. if (minor->type == DRM_MINOR_LEGACY) {
  125. BUG_ON((dev->dev_mapping != NULL) &&
  126. (dev->dev_mapping != inode->i_mapping));
  127. if (dev->dev_mapping == NULL)
  128. dev->dev_mapping = inode->i_mapping;
  129. }
  130. mutex_unlock(&dev->struct_mutex);
  131. return retcode;
  132. }
  133. EXPORT_SYMBOL(drm_open);
  134. /**
  135. * File \c open operation.
  136. *
  137. * \param inode device inode.
  138. * \param filp file pointer.
  139. *
  140. * Puts the dev->fops corresponding to the device minor number into
  141. * \p filp, call the \c open method, and restore the file operations.
  142. */
  143. int drm_stub_open(struct inode *inode, struct file *filp)
  144. {
  145. struct drm_device *dev = NULL;
  146. struct drm_minor *minor;
  147. int minor_id = iminor(inode);
  148. int err = -ENODEV;
  149. const struct file_operations *old_fops;
  150. DRM_DEBUG("\n");
  151. /* BKL pushdown: note that nothing else serializes idr_find() */
  152. lock_kernel();
  153. minor = idr_find(&drm_minors_idr, minor_id);
  154. if (!minor)
  155. goto out;
  156. if (!(dev = minor->dev))
  157. goto out;
  158. old_fops = filp->f_op;
  159. filp->f_op = fops_get(&dev->driver->fops);
  160. if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
  161. fops_put(filp->f_op);
  162. filp->f_op = fops_get(old_fops);
  163. }
  164. fops_put(old_fops);
  165. out:
  166. unlock_kernel();
  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. struct drm_device * dev)
  198. {
  199. int minor_id = iminor(inode);
  200. struct drm_file *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", task_pid_nr(current), minor_id);
  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->filp = filp;
  213. priv->uid = current_euid();
  214. priv->pid = task_pid_nr(current);
  215. priv->minor = idr_find(&drm_minors_idr, minor_id);
  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. INIT_LIST_HEAD(&priv->lhead);
  221. INIT_LIST_HEAD(&priv->fbs);
  222. if (dev->driver->driver_features & DRIVER_GEM)
  223. drm_gem_open(dev, priv);
  224. if (dev->driver->open) {
  225. ret = dev->driver->open(dev, priv);
  226. if (ret < 0)
  227. goto out_free;
  228. }
  229. /* if there is no current master make this fd it */
  230. mutex_lock(&dev->struct_mutex);
  231. if (!priv->minor->master) {
  232. /* create a new master */
  233. priv->minor->master = drm_master_create(priv->minor);
  234. if (!priv->minor->master) {
  235. ret = -ENOMEM;
  236. goto out_free;
  237. }
  238. priv->is_master = 1;
  239. /* take another reference for the copy in the local file priv */
  240. priv->master = drm_master_get(priv->minor->master);
  241. priv->authenticated = 1;
  242. mutex_unlock(&dev->struct_mutex);
  243. if (dev->driver->master_create) {
  244. ret = dev->driver->master_create(dev, priv->master);
  245. if (ret) {
  246. mutex_lock(&dev->struct_mutex);
  247. /* drop both references if this fails */
  248. drm_master_put(&priv->minor->master);
  249. drm_master_put(&priv->master);
  250. mutex_unlock(&dev->struct_mutex);
  251. goto out_free;
  252. }
  253. }
  254. } else {
  255. /* get a reference to the master */
  256. priv->master = drm_master_get(priv->minor->master);
  257. mutex_unlock(&dev->struct_mutex);
  258. }
  259. mutex_lock(&dev->struct_mutex);
  260. list_add(&priv->lhead, &dev->filelist);
  261. mutex_unlock(&dev->struct_mutex);
  262. #ifdef __alpha__
  263. /*
  264. * Default the hose
  265. */
  266. if (!dev->hose) {
  267. struct pci_dev *pci_dev;
  268. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  269. if (pci_dev) {
  270. dev->hose = pci_dev->sysdata;
  271. pci_dev_put(pci_dev);
  272. }
  273. if (!dev->hose) {
  274. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  275. if (b)
  276. dev->hose = b->sysdata;
  277. }
  278. }
  279. #endif
  280. return 0;
  281. out_free:
  282. drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
  283. filp->private_data = NULL;
  284. return ret;
  285. }
  286. /** No-op. */
  287. int drm_fasync(int fd, struct file *filp, int on)
  288. {
  289. struct drm_file *priv = filp->private_data;
  290. struct drm_device *dev = priv->minor->dev;
  291. int retcode;
  292. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
  293. (long)old_encode_dev(priv->minor->device));
  294. retcode = fasync_helper(fd, filp, on, &dev->buf_async);
  295. if (retcode < 0)
  296. return retcode;
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(drm_fasync);
  300. /*
  301. * Reclaim locked buffers; note that this may be a bad idea if the current
  302. * context doesn't have the hw lock...
  303. */
  304. static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
  305. {
  306. struct drm_file *file_priv = f->private_data;
  307. if (drm_i_have_hw_lock(dev, file_priv)) {
  308. dev->driver->reclaim_buffers_locked(dev, file_priv);
  309. } else {
  310. unsigned long _end = jiffies + 3 * DRM_HZ;
  311. int locked = 0;
  312. drm_idlelock_take(&file_priv->master->lock);
  313. /*
  314. * Wait for a while.
  315. */
  316. do {
  317. spin_lock_bh(&file_priv->master->lock.spinlock);
  318. locked = file_priv->master->lock.idle_has_lock;
  319. spin_unlock_bh(&file_priv->master->lock.spinlock);
  320. if (locked)
  321. break;
  322. schedule();
  323. } while (!time_after_eq(jiffies, _end));
  324. if (!locked) {
  325. DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
  326. "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
  327. "\tI will go on reclaiming the buffers anyway.\n");
  328. }
  329. dev->driver->reclaim_buffers_locked(dev, file_priv);
  330. drm_idlelock_release(&file_priv->master->lock);
  331. }
  332. }
  333. static void drm_master_release(struct drm_device *dev, struct file *filp)
  334. {
  335. struct drm_file *file_priv = filp->private_data;
  336. if (dev->driver->reclaim_buffers_locked &&
  337. file_priv->master->lock.hw_lock)
  338. drm_reclaim_locked_buffers(dev, filp);
  339. if (dev->driver->reclaim_buffers_idlelocked &&
  340. file_priv->master->lock.hw_lock) {
  341. drm_idlelock_take(&file_priv->master->lock);
  342. dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
  343. drm_idlelock_release(&file_priv->master->lock);
  344. }
  345. if (drm_i_have_hw_lock(dev, file_priv)) {
  346. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  347. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  348. drm_lock_free(&file_priv->master->lock,
  349. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  350. }
  351. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  352. !dev->driver->reclaim_buffers_locked) {
  353. dev->driver->reclaim_buffers(dev, file_priv);
  354. }
  355. }
  356. /**
  357. * Release file.
  358. *
  359. * \param inode device inode
  360. * \param file_priv DRM file private.
  361. * \return zero on success or a negative number on failure.
  362. *
  363. * If the hardware lock is held then free it, and take it again for the kernel
  364. * context since it's necessary to reclaim buffers. Unlink the file private
  365. * data from its list and free it. Decreases the open count and if it reaches
  366. * zero calls drm_lastclose().
  367. */
  368. int drm_release(struct inode *inode, struct file *filp)
  369. {
  370. struct drm_file *file_priv = filp->private_data;
  371. struct drm_device *dev = file_priv->minor->dev;
  372. int retcode = 0;
  373. lock_kernel();
  374. DRM_DEBUG("open_count = %d\n", dev->open_count);
  375. if (dev->driver->preclose)
  376. dev->driver->preclose(dev, file_priv);
  377. /* ========================================================
  378. * Begin inline drm_release
  379. */
  380. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  381. task_pid_nr(current),
  382. (long)old_encode_dev(file_priv->minor->device),
  383. dev->open_count);
  384. /* if the master has gone away we can't do anything with the lock */
  385. if (file_priv->minor->master)
  386. drm_master_release(dev, filp);
  387. if (dev->driver->driver_features & DRIVER_GEM)
  388. drm_gem_release(dev, file_priv);
  389. mutex_lock(&dev->ctxlist_mutex);
  390. if (!list_empty(&dev->ctxlist)) {
  391. struct drm_ctx_list *pos, *n;
  392. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  393. if (pos->tag == file_priv &&
  394. pos->handle != DRM_KERNEL_CONTEXT) {
  395. if (dev->driver->context_dtor)
  396. dev->driver->context_dtor(dev,
  397. pos->handle);
  398. drm_ctxbitmap_free(dev, pos->handle);
  399. list_del(&pos->head);
  400. drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
  401. --dev->ctx_count;
  402. }
  403. }
  404. }
  405. mutex_unlock(&dev->ctxlist_mutex);
  406. mutex_lock(&dev->struct_mutex);
  407. if (file_priv->is_master) {
  408. struct drm_file *temp;
  409. list_for_each_entry(temp, &dev->filelist, lhead) {
  410. if ((temp->master == file_priv->master) &&
  411. (temp != file_priv))
  412. temp->authenticated = 0;
  413. }
  414. if (file_priv->minor->master == file_priv->master) {
  415. /* drop the reference held my the minor */
  416. drm_master_put(&file_priv->minor->master);
  417. }
  418. }
  419. /* drop the reference held my the file priv */
  420. drm_master_put(&file_priv->master);
  421. file_priv->is_master = 0;
  422. list_del(&file_priv->lhead);
  423. mutex_unlock(&dev->struct_mutex);
  424. if (dev->driver->postclose)
  425. dev->driver->postclose(dev, file_priv);
  426. drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
  427. /* ========================================================
  428. * End inline drm_release
  429. */
  430. atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
  431. spin_lock(&dev->count_lock);
  432. if (!--dev->open_count) {
  433. if (atomic_read(&dev->ioctl_count)) {
  434. DRM_ERROR("Device busy: %d\n",
  435. atomic_read(&dev->ioctl_count));
  436. spin_unlock(&dev->count_lock);
  437. unlock_kernel();
  438. return -EBUSY;
  439. }
  440. spin_unlock(&dev->count_lock);
  441. unlock_kernel();
  442. return drm_lastclose(dev);
  443. }
  444. spin_unlock(&dev->count_lock);
  445. unlock_kernel();
  446. return retcode;
  447. }
  448. EXPORT_SYMBOL(drm_release);
  449. /** No-op. */
  450. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  451. {
  452. return 0;
  453. }
  454. EXPORT_SYMBOL(drm_poll);