drm_fops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 == NULL) {
  161. filp->f_op = old_fops;
  162. goto out;
  163. }
  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. out:
  170. unlock_kernel();
  171. return err;
  172. }
  173. /**
  174. * Check whether DRI will run on this CPU.
  175. *
  176. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  177. */
  178. static int drm_cpu_valid(void)
  179. {
  180. #if defined(__i386__)
  181. if (boot_cpu_data.x86 == 3)
  182. return 0; /* No cmpxchg on a 386 */
  183. #endif
  184. #if defined(__sparc__) && !defined(__sparc_v9__)
  185. return 0; /* No cmpxchg before v9 sparc. */
  186. #endif
  187. return 1;
  188. }
  189. /**
  190. * Called whenever a process opens /dev/drm.
  191. *
  192. * \param inode device inode.
  193. * \param filp file pointer.
  194. * \param dev device.
  195. * \return zero on success or a negative number on failure.
  196. *
  197. * Creates and initializes a drm_file structure for the file private data in \p
  198. * filp and add it into the double linked list in \p dev.
  199. */
  200. static int drm_open_helper(struct inode *inode, struct file *filp,
  201. struct drm_device * dev)
  202. {
  203. int minor_id = iminor(inode);
  204. struct drm_file *priv;
  205. int ret;
  206. if (filp->f_flags & O_EXCL)
  207. return -EBUSY; /* No exclusive opens */
  208. if (!drm_cpu_valid())
  209. return -EINVAL;
  210. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
  211. priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
  212. if (!priv)
  213. return -ENOMEM;
  214. memset(priv, 0, sizeof(*priv));
  215. filp->private_data = priv;
  216. priv->filp = filp;
  217. priv->uid = current_euid();
  218. priv->pid = task_pid_nr(current);
  219. priv->minor = idr_find(&drm_minors_idr, minor_id);
  220. priv->ioctl_count = 0;
  221. /* for compatibility root is always authenticated */
  222. priv->authenticated = capable(CAP_SYS_ADMIN);
  223. priv->lock_count = 0;
  224. INIT_LIST_HEAD(&priv->lhead);
  225. INIT_LIST_HEAD(&priv->fbs);
  226. if (dev->driver->driver_features & DRIVER_GEM)
  227. drm_gem_open(dev, priv);
  228. if (dev->driver->open) {
  229. ret = dev->driver->open(dev, priv);
  230. if (ret < 0)
  231. goto out_free;
  232. }
  233. /* if there is no current master make this fd it */
  234. mutex_lock(&dev->struct_mutex);
  235. if (!priv->minor->master) {
  236. /* create a new master */
  237. priv->minor->master = drm_master_create(priv->minor);
  238. if (!priv->minor->master) {
  239. ret = -ENOMEM;
  240. goto out_free;
  241. }
  242. priv->is_master = 1;
  243. /* take another reference for the copy in the local file priv */
  244. priv->master = drm_master_get(priv->minor->master);
  245. priv->authenticated = 1;
  246. mutex_unlock(&dev->struct_mutex);
  247. if (dev->driver->master_create) {
  248. ret = dev->driver->master_create(dev, priv->master);
  249. if (ret) {
  250. mutex_lock(&dev->struct_mutex);
  251. /* drop both references if this fails */
  252. drm_master_put(&priv->minor->master);
  253. drm_master_put(&priv->master);
  254. mutex_unlock(&dev->struct_mutex);
  255. goto out_free;
  256. }
  257. }
  258. } else {
  259. /* get a reference to the master */
  260. priv->master = drm_master_get(priv->minor->master);
  261. mutex_unlock(&dev->struct_mutex);
  262. }
  263. mutex_lock(&dev->struct_mutex);
  264. list_add(&priv->lhead, &dev->filelist);
  265. mutex_unlock(&dev->struct_mutex);
  266. #ifdef __alpha__
  267. /*
  268. * Default the hose
  269. */
  270. if (!dev->hose) {
  271. struct pci_dev *pci_dev;
  272. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  273. if (pci_dev) {
  274. dev->hose = pci_dev->sysdata;
  275. pci_dev_put(pci_dev);
  276. }
  277. if (!dev->hose) {
  278. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  279. if (b)
  280. dev->hose = b->sysdata;
  281. }
  282. }
  283. #endif
  284. return 0;
  285. out_free:
  286. drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
  287. filp->private_data = NULL;
  288. return ret;
  289. }
  290. /** No-op. */
  291. int drm_fasync(int fd, struct file *filp, int on)
  292. {
  293. struct drm_file *priv = filp->private_data;
  294. struct drm_device *dev = priv->minor->dev;
  295. int retcode;
  296. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
  297. (long)old_encode_dev(priv->minor->device));
  298. retcode = fasync_helper(fd, filp, on, &dev->buf_async);
  299. if (retcode < 0)
  300. return retcode;
  301. return 0;
  302. }
  303. EXPORT_SYMBOL(drm_fasync);
  304. /*
  305. * Reclaim locked buffers; note that this may be a bad idea if the current
  306. * context doesn't have the hw lock...
  307. */
  308. static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
  309. {
  310. struct drm_file *file_priv = f->private_data;
  311. if (drm_i_have_hw_lock(dev, file_priv)) {
  312. dev->driver->reclaim_buffers_locked(dev, file_priv);
  313. } else {
  314. unsigned long _end = jiffies + 3 * DRM_HZ;
  315. int locked = 0;
  316. drm_idlelock_take(&file_priv->master->lock);
  317. /*
  318. * Wait for a while.
  319. */
  320. do {
  321. spin_lock_bh(&file_priv->master->lock.spinlock);
  322. locked = file_priv->master->lock.idle_has_lock;
  323. spin_unlock_bh(&file_priv->master->lock.spinlock);
  324. if (locked)
  325. break;
  326. schedule();
  327. } while (!time_after_eq(jiffies, _end));
  328. if (!locked) {
  329. DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
  330. "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
  331. "\tI will go on reclaiming the buffers anyway.\n");
  332. }
  333. dev->driver->reclaim_buffers_locked(dev, file_priv);
  334. drm_idlelock_release(&file_priv->master->lock);
  335. }
  336. }
  337. static void drm_master_release(struct drm_device *dev, struct file *filp)
  338. {
  339. struct drm_file *file_priv = filp->private_data;
  340. if (dev->driver->reclaim_buffers_locked &&
  341. file_priv->master->lock.hw_lock)
  342. drm_reclaim_locked_buffers(dev, filp);
  343. if (dev->driver->reclaim_buffers_idlelocked &&
  344. file_priv->master->lock.hw_lock) {
  345. drm_idlelock_take(&file_priv->master->lock);
  346. dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
  347. drm_idlelock_release(&file_priv->master->lock);
  348. }
  349. if (drm_i_have_hw_lock(dev, file_priv)) {
  350. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  351. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  352. drm_lock_free(&file_priv->master->lock,
  353. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  354. }
  355. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  356. !dev->driver->reclaim_buffers_locked) {
  357. dev->driver->reclaim_buffers(dev, file_priv);
  358. }
  359. }
  360. /**
  361. * Release file.
  362. *
  363. * \param inode device inode
  364. * \param file_priv DRM file private.
  365. * \return zero on success or a negative number on failure.
  366. *
  367. * If the hardware lock is held then free it, and take it again for the kernel
  368. * context since it's necessary to reclaim buffers. Unlink the file private
  369. * data from its list and free it. Decreases the open count and if it reaches
  370. * zero calls drm_lastclose().
  371. */
  372. int drm_release(struct inode *inode, struct file *filp)
  373. {
  374. struct drm_file *file_priv = filp->private_data;
  375. struct drm_device *dev = file_priv->minor->dev;
  376. int retcode = 0;
  377. lock_kernel();
  378. DRM_DEBUG("open_count = %d\n", dev->open_count);
  379. if (dev->driver->preclose)
  380. dev->driver->preclose(dev, file_priv);
  381. /* ========================================================
  382. * Begin inline drm_release
  383. */
  384. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  385. task_pid_nr(current),
  386. (long)old_encode_dev(file_priv->minor->device),
  387. dev->open_count);
  388. /* if the master has gone away we can't do anything with the lock */
  389. if (file_priv->minor->master)
  390. drm_master_release(dev, filp);
  391. if (dev->driver->driver_features & DRIVER_GEM)
  392. drm_gem_release(dev, file_priv);
  393. if (dev->driver->driver_features & DRIVER_MODESET)
  394. drm_fb_release(file_priv);
  395. mutex_lock(&dev->ctxlist_mutex);
  396. if (!list_empty(&dev->ctxlist)) {
  397. struct drm_ctx_list *pos, *n;
  398. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  399. if (pos->tag == file_priv &&
  400. pos->handle != DRM_KERNEL_CONTEXT) {
  401. if (dev->driver->context_dtor)
  402. dev->driver->context_dtor(dev,
  403. pos->handle);
  404. drm_ctxbitmap_free(dev, pos->handle);
  405. list_del(&pos->head);
  406. drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
  407. --dev->ctx_count;
  408. }
  409. }
  410. }
  411. mutex_unlock(&dev->ctxlist_mutex);
  412. mutex_lock(&dev->struct_mutex);
  413. if (file_priv->is_master) {
  414. struct drm_master *master = file_priv->master;
  415. struct drm_file *temp;
  416. list_for_each_entry(temp, &dev->filelist, lhead) {
  417. if ((temp->master == file_priv->master) &&
  418. (temp != file_priv))
  419. temp->authenticated = 0;
  420. }
  421. /**
  422. * Since the master is disappearing, so is the
  423. * possibility to lock.
  424. */
  425. if (master->lock.hw_lock) {
  426. if (dev->sigdata.lock == master->lock.hw_lock)
  427. dev->sigdata.lock = NULL;
  428. master->lock.hw_lock = NULL;
  429. master->lock.file_priv = NULL;
  430. wake_up_interruptible_all(&master->lock.lock_queue);
  431. }
  432. if (file_priv->minor->master == file_priv->master) {
  433. /* drop the reference held my the minor */
  434. drm_master_put(&file_priv->minor->master);
  435. }
  436. }
  437. /* drop the reference held my the file priv */
  438. drm_master_put(&file_priv->master);
  439. file_priv->is_master = 0;
  440. list_del(&file_priv->lhead);
  441. mutex_unlock(&dev->struct_mutex);
  442. if (dev->driver->postclose)
  443. dev->driver->postclose(dev, file_priv);
  444. drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
  445. /* ========================================================
  446. * End inline drm_release
  447. */
  448. atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
  449. spin_lock(&dev->count_lock);
  450. if (!--dev->open_count) {
  451. if (atomic_read(&dev->ioctl_count)) {
  452. DRM_ERROR("Device busy: %d\n",
  453. atomic_read(&dev->ioctl_count));
  454. spin_unlock(&dev->count_lock);
  455. unlock_kernel();
  456. return -EBUSY;
  457. }
  458. spin_unlock(&dev->count_lock);
  459. unlock_kernel();
  460. return drm_lastclose(dev);
  461. }
  462. spin_unlock(&dev->count_lock);
  463. unlock_kernel();
  464. return retcode;
  465. }
  466. EXPORT_SYMBOL(drm_release);
  467. /** No-op. */
  468. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  469. {
  470. return 0;
  471. }
  472. EXPORT_SYMBOL(drm_poll);