drm_fops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 = kmalloc(sizeof(*priv), GFP_KERNEL);
  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. mutex_unlock(&dev->struct_mutex);
  240. ret = -ENOMEM;
  241. goto out_free;
  242. }
  243. priv->is_master = 1;
  244. /* take another reference for the copy in the local file priv */
  245. priv->master = drm_master_get(priv->minor->master);
  246. priv->authenticated = 1;
  247. mutex_unlock(&dev->struct_mutex);
  248. if (dev->driver->master_create) {
  249. ret = dev->driver->master_create(dev, priv->master);
  250. if (ret) {
  251. mutex_lock(&dev->struct_mutex);
  252. /* drop both references if this fails */
  253. drm_master_put(&priv->minor->master);
  254. drm_master_put(&priv->master);
  255. mutex_unlock(&dev->struct_mutex);
  256. goto out_free;
  257. }
  258. }
  259. } else {
  260. /* get a reference to the master */
  261. priv->master = drm_master_get(priv->minor->master);
  262. mutex_unlock(&dev->struct_mutex);
  263. }
  264. mutex_lock(&dev->struct_mutex);
  265. list_add(&priv->lhead, &dev->filelist);
  266. mutex_unlock(&dev->struct_mutex);
  267. #ifdef __alpha__
  268. /*
  269. * Default the hose
  270. */
  271. if (!dev->hose) {
  272. struct pci_dev *pci_dev;
  273. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  274. if (pci_dev) {
  275. dev->hose = pci_dev->sysdata;
  276. pci_dev_put(pci_dev);
  277. }
  278. if (!dev->hose) {
  279. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  280. if (b)
  281. dev->hose = b->sysdata;
  282. }
  283. }
  284. #endif
  285. return 0;
  286. out_free:
  287. kfree(priv);
  288. filp->private_data = NULL;
  289. return ret;
  290. }
  291. /** No-op. */
  292. int drm_fasync(int fd, struct file *filp, int on)
  293. {
  294. struct drm_file *priv = filp->private_data;
  295. struct drm_device *dev = priv->minor->dev;
  296. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
  297. (long)old_encode_dev(priv->minor->device));
  298. return fasync_helper(fd, filp, on, &dev->buf_async);
  299. }
  300. EXPORT_SYMBOL(drm_fasync);
  301. /*
  302. * Reclaim locked buffers; note that this may be a bad idea if the current
  303. * context doesn't have the hw lock...
  304. */
  305. static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
  306. {
  307. struct drm_file *file_priv = f->private_data;
  308. if (drm_i_have_hw_lock(dev, file_priv)) {
  309. dev->driver->reclaim_buffers_locked(dev, file_priv);
  310. } else {
  311. unsigned long _end = jiffies + 3 * DRM_HZ;
  312. int locked = 0;
  313. drm_idlelock_take(&file_priv->master->lock);
  314. /*
  315. * Wait for a while.
  316. */
  317. do {
  318. spin_lock_bh(&file_priv->master->lock.spinlock);
  319. locked = file_priv->master->lock.idle_has_lock;
  320. spin_unlock_bh(&file_priv->master->lock.spinlock);
  321. if (locked)
  322. break;
  323. schedule();
  324. } while (!time_after_eq(jiffies, _end));
  325. if (!locked) {
  326. DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
  327. "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
  328. "\tI will go on reclaiming the buffers anyway.\n");
  329. }
  330. dev->driver->reclaim_buffers_locked(dev, file_priv);
  331. drm_idlelock_release(&file_priv->master->lock);
  332. }
  333. }
  334. static void drm_master_release(struct drm_device *dev, struct file *filp)
  335. {
  336. struct drm_file *file_priv = filp->private_data;
  337. if (dev->driver->reclaim_buffers_locked &&
  338. file_priv->master->lock.hw_lock)
  339. drm_reclaim_locked_buffers(dev, filp);
  340. if (dev->driver->reclaim_buffers_idlelocked &&
  341. file_priv->master->lock.hw_lock) {
  342. drm_idlelock_take(&file_priv->master->lock);
  343. dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
  344. drm_idlelock_release(&file_priv->master->lock);
  345. }
  346. if (drm_i_have_hw_lock(dev, file_priv)) {
  347. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  348. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  349. drm_lock_free(&file_priv->master->lock,
  350. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  351. }
  352. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  353. !dev->driver->reclaim_buffers_locked) {
  354. dev->driver->reclaim_buffers(dev, file_priv);
  355. }
  356. }
  357. /**
  358. * Release file.
  359. *
  360. * \param inode device inode
  361. * \param file_priv DRM file private.
  362. * \return zero on success or a negative number on failure.
  363. *
  364. * If the hardware lock is held then free it, and take it again for the kernel
  365. * context since it's necessary to reclaim buffers. Unlink the file private
  366. * data from its list and free it. Decreases the open count and if it reaches
  367. * zero calls drm_lastclose().
  368. */
  369. int drm_release(struct inode *inode, struct file *filp)
  370. {
  371. struct drm_file *file_priv = filp->private_data;
  372. struct drm_device *dev = file_priv->minor->dev;
  373. int retcode = 0;
  374. lock_kernel();
  375. DRM_DEBUG("open_count = %d\n", dev->open_count);
  376. if (dev->driver->preclose)
  377. dev->driver->preclose(dev, file_priv);
  378. /* ========================================================
  379. * Begin inline drm_release
  380. */
  381. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  382. task_pid_nr(current),
  383. (long)old_encode_dev(file_priv->minor->device),
  384. dev->open_count);
  385. /* if the master has gone away we can't do anything with the lock */
  386. if (file_priv->minor->master)
  387. drm_master_release(dev, filp);
  388. if (dev->driver->driver_features & DRIVER_GEM)
  389. drm_gem_release(dev, file_priv);
  390. if (dev->driver->driver_features & DRIVER_MODESET)
  391. drm_fb_release(file_priv);
  392. mutex_lock(&dev->ctxlist_mutex);
  393. if (!list_empty(&dev->ctxlist)) {
  394. struct drm_ctx_list *pos, *n;
  395. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  396. if (pos->tag == file_priv &&
  397. pos->handle != DRM_KERNEL_CONTEXT) {
  398. if (dev->driver->context_dtor)
  399. dev->driver->context_dtor(dev,
  400. pos->handle);
  401. drm_ctxbitmap_free(dev, pos->handle);
  402. list_del(&pos->head);
  403. kfree(pos);
  404. --dev->ctx_count;
  405. }
  406. }
  407. }
  408. mutex_unlock(&dev->ctxlist_mutex);
  409. mutex_lock(&dev->struct_mutex);
  410. if (file_priv->is_master) {
  411. struct drm_master *master = file_priv->master;
  412. struct drm_file *temp;
  413. list_for_each_entry(temp, &dev->filelist, lhead) {
  414. if ((temp->master == file_priv->master) &&
  415. (temp != file_priv))
  416. temp->authenticated = 0;
  417. }
  418. /**
  419. * Since the master is disappearing, so is the
  420. * possibility to lock.
  421. */
  422. if (master->lock.hw_lock) {
  423. if (dev->sigdata.lock == master->lock.hw_lock)
  424. dev->sigdata.lock = NULL;
  425. master->lock.hw_lock = NULL;
  426. master->lock.file_priv = NULL;
  427. wake_up_interruptible_all(&master->lock.lock_queue);
  428. }
  429. if (file_priv->minor->master == file_priv->master) {
  430. /* drop the reference held my the minor */
  431. drm_master_put(&file_priv->minor->master);
  432. }
  433. }
  434. /* drop the reference held my the file priv */
  435. drm_master_put(&file_priv->master);
  436. file_priv->is_master = 0;
  437. list_del(&file_priv->lhead);
  438. mutex_unlock(&dev->struct_mutex);
  439. if (dev->driver->postclose)
  440. dev->driver->postclose(dev, file_priv);
  441. kfree(file_priv);
  442. /* ========================================================
  443. * End inline drm_release
  444. */
  445. atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
  446. spin_lock(&dev->count_lock);
  447. if (!--dev->open_count) {
  448. if (atomic_read(&dev->ioctl_count)) {
  449. DRM_ERROR("Device busy: %d\n",
  450. atomic_read(&dev->ioctl_count));
  451. spin_unlock(&dev->count_lock);
  452. unlock_kernel();
  453. return -EBUSY;
  454. }
  455. spin_unlock(&dev->count_lock);
  456. unlock_kernel();
  457. return drm_lastclose(dev);
  458. }
  459. spin_unlock(&dev->count_lock);
  460. unlock_kernel();
  461. return retcode;
  462. }
  463. EXPORT_SYMBOL(drm_release);
  464. /** No-op. */
  465. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  466. {
  467. return 0;
  468. }
  469. EXPORT_SYMBOL(drm_poll);