drm_fops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 <drm/drmP.h>
  36. #include <linux/poll.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include "drm_legacy.h"
  40. #include "drm_internal.h"
  41. /* from BKL pushdown */
  42. DEFINE_MUTEX(drm_global_mutex);
  43. static int drm_open_helper(struct file *filp, struct drm_minor *minor);
  44. static int drm_setup(struct drm_device * dev)
  45. {
  46. int ret;
  47. if (dev->driver->firstopen &&
  48. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  49. ret = dev->driver->firstopen(dev);
  50. if (ret != 0)
  51. return ret;
  52. }
  53. ret = drm_legacy_dma_setup(dev);
  54. if (ret < 0)
  55. return ret;
  56. DRM_DEBUG("\n");
  57. return 0;
  58. }
  59. /**
  60. * Open file.
  61. *
  62. * \param inode device inode
  63. * \param filp file pointer.
  64. * \return zero on success or a negative number on failure.
  65. *
  66. * Searches the DRM device with the same minor number, calls open_helper(), and
  67. * increments the device open count. If the open count was previous at zero,
  68. * i.e., it's the first that the device is open, then calls setup().
  69. */
  70. int drm_open(struct inode *inode, struct file *filp)
  71. {
  72. struct drm_device *dev;
  73. struct drm_minor *minor;
  74. int retcode;
  75. int need_setup = 0;
  76. minor = drm_minor_acquire(iminor(inode));
  77. if (IS_ERR(minor))
  78. return PTR_ERR(minor);
  79. dev = minor->dev;
  80. if (!dev->open_count++)
  81. need_setup = 1;
  82. /* share address_space across all char-devs of a single device */
  83. filp->f_mapping = dev->anon_inode->i_mapping;
  84. retcode = drm_open_helper(filp, minor);
  85. if (retcode)
  86. goto err_undo;
  87. if (need_setup) {
  88. retcode = drm_setup(dev);
  89. if (retcode)
  90. goto err_undo;
  91. }
  92. return 0;
  93. err_undo:
  94. dev->open_count--;
  95. drm_minor_release(minor);
  96. return retcode;
  97. }
  98. EXPORT_SYMBOL(drm_open);
  99. /**
  100. * Check whether DRI will run on this CPU.
  101. *
  102. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  103. */
  104. static int drm_cpu_valid(void)
  105. {
  106. #if defined(__sparc__) && !defined(__sparc_v9__)
  107. return 0; /* No cmpxchg before v9 sparc. */
  108. #endif
  109. return 1;
  110. }
  111. /**
  112. * Called whenever a process opens /dev/drm.
  113. *
  114. * \param filp file pointer.
  115. * \param minor acquired minor-object.
  116. * \return zero on success or a negative number on failure.
  117. *
  118. * Creates and initializes a drm_file structure for the file private data in \p
  119. * filp and add it into the double linked list in \p dev.
  120. */
  121. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  122. {
  123. struct drm_device *dev = minor->dev;
  124. struct drm_file *priv;
  125. int ret;
  126. if (filp->f_flags & O_EXCL)
  127. return -EBUSY; /* No exclusive opens */
  128. if (!drm_cpu_valid())
  129. return -EINVAL;
  130. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  131. return -EINVAL;
  132. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
  133. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  134. if (!priv)
  135. return -ENOMEM;
  136. filp->private_data = priv;
  137. priv->filp = filp;
  138. priv->uid = current_euid();
  139. priv->pid = get_pid(task_pid(current));
  140. priv->minor = minor;
  141. /* for compatibility root is always authenticated */
  142. priv->authenticated = capable(CAP_SYS_ADMIN);
  143. priv->lock_count = 0;
  144. INIT_LIST_HEAD(&priv->lhead);
  145. INIT_LIST_HEAD(&priv->fbs);
  146. mutex_init(&priv->fbs_lock);
  147. INIT_LIST_HEAD(&priv->event_list);
  148. init_waitqueue_head(&priv->event_wait);
  149. priv->event_space = 4096; /* set aside 4k for event buffer */
  150. if (drm_core_check_feature(dev, DRIVER_GEM))
  151. drm_gem_open(dev, priv);
  152. if (drm_core_check_feature(dev, DRIVER_PRIME))
  153. drm_prime_init_file_private(&priv->prime);
  154. if (dev->driver->open) {
  155. ret = dev->driver->open(dev, priv);
  156. if (ret < 0)
  157. goto out_prime_destroy;
  158. }
  159. /* if there is no current master make this fd it, but do not create
  160. * any master object for render clients */
  161. mutex_lock(&dev->master_mutex);
  162. if (drm_is_primary_client(priv) && !priv->minor->master) {
  163. /* create a new master */
  164. priv->minor->master = drm_master_create(priv->minor);
  165. if (!priv->minor->master) {
  166. ret = -ENOMEM;
  167. goto out_close;
  168. }
  169. priv->is_master = 1;
  170. /* take another reference for the copy in the local file priv */
  171. priv->master = drm_master_get(priv->minor->master);
  172. priv->authenticated = 1;
  173. if (dev->driver->master_create) {
  174. ret = dev->driver->master_create(dev, priv->master);
  175. if (ret) {
  176. /* drop both references if this fails */
  177. drm_master_put(&priv->minor->master);
  178. drm_master_put(&priv->master);
  179. goto out_close;
  180. }
  181. }
  182. if (dev->driver->master_set) {
  183. ret = dev->driver->master_set(dev, priv, true);
  184. if (ret) {
  185. /* drop both references if this fails */
  186. drm_master_put(&priv->minor->master);
  187. drm_master_put(&priv->master);
  188. goto out_close;
  189. }
  190. }
  191. } else if (drm_is_primary_client(priv)) {
  192. /* get a reference to the master */
  193. priv->master = drm_master_get(priv->minor->master);
  194. }
  195. mutex_unlock(&dev->master_mutex);
  196. mutex_lock(&dev->struct_mutex);
  197. list_add(&priv->lhead, &dev->filelist);
  198. mutex_unlock(&dev->struct_mutex);
  199. #ifdef __alpha__
  200. /*
  201. * Default the hose
  202. */
  203. if (!dev->hose) {
  204. struct pci_dev *pci_dev;
  205. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  206. if (pci_dev) {
  207. dev->hose = pci_dev->sysdata;
  208. pci_dev_put(pci_dev);
  209. }
  210. if (!dev->hose) {
  211. struct pci_bus *b = list_entry(pci_root_buses.next,
  212. struct pci_bus, node);
  213. if (b)
  214. dev->hose = b->sysdata;
  215. }
  216. }
  217. #endif
  218. return 0;
  219. out_close:
  220. mutex_unlock(&dev->master_mutex);
  221. if (dev->driver->postclose)
  222. dev->driver->postclose(dev, priv);
  223. out_prime_destroy:
  224. if (drm_core_check_feature(dev, DRIVER_PRIME))
  225. drm_prime_destroy_file_private(&priv->prime);
  226. if (drm_core_check_feature(dev, DRIVER_GEM))
  227. drm_gem_release(dev, priv);
  228. put_pid(priv->pid);
  229. kfree(priv);
  230. filp->private_data = NULL;
  231. return ret;
  232. }
  233. static void drm_master_release(struct drm_device *dev, struct file *filp)
  234. {
  235. struct drm_file *file_priv = filp->private_data;
  236. if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
  237. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  238. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  239. drm_legacy_lock_free(&file_priv->master->lock,
  240. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  241. }
  242. }
  243. static void drm_events_release(struct drm_file *file_priv)
  244. {
  245. struct drm_device *dev = file_priv->minor->dev;
  246. struct drm_pending_event *e, *et;
  247. struct drm_pending_vblank_event *v, *vt;
  248. unsigned long flags;
  249. spin_lock_irqsave(&dev->event_lock, flags);
  250. /* Remove pending flips */
  251. list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
  252. if (v->base.file_priv == file_priv) {
  253. list_del(&v->base.link);
  254. drm_vblank_put(dev, v->pipe);
  255. v->base.destroy(&v->base);
  256. }
  257. /* Remove unconsumed events */
  258. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  259. list_del(&e->link);
  260. e->destroy(e);
  261. }
  262. spin_unlock_irqrestore(&dev->event_lock, flags);
  263. }
  264. /**
  265. * drm_legacy_dev_reinit
  266. *
  267. * Reinitializes a legacy/ums drm device in it's lastclose function.
  268. */
  269. static void drm_legacy_dev_reinit(struct drm_device *dev)
  270. {
  271. if (drm_core_check_feature(dev, DRIVER_MODESET))
  272. return;
  273. dev->sigdata.lock = NULL;
  274. dev->context_flag = 0;
  275. dev->last_context = 0;
  276. dev->if_version = 0;
  277. }
  278. /**
  279. * Take down the DRM device.
  280. *
  281. * \param dev DRM device structure.
  282. *
  283. * Frees every resource in \p dev.
  284. *
  285. * \sa drm_device
  286. */
  287. int drm_lastclose(struct drm_device * dev)
  288. {
  289. DRM_DEBUG("\n");
  290. if (dev->driver->lastclose)
  291. dev->driver->lastclose(dev);
  292. DRM_DEBUG("driver lastclose completed\n");
  293. if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
  294. drm_irq_uninstall(dev);
  295. mutex_lock(&dev->struct_mutex);
  296. drm_agp_clear(dev);
  297. drm_legacy_sg_cleanup(dev);
  298. drm_legacy_vma_flush(dev);
  299. drm_legacy_dma_takedown(dev);
  300. mutex_unlock(&dev->struct_mutex);
  301. drm_legacy_dev_reinit(dev);
  302. DRM_DEBUG("lastclose completed\n");
  303. return 0;
  304. }
  305. /**
  306. * Release file.
  307. *
  308. * \param inode device inode
  309. * \param file_priv DRM file private.
  310. * \return zero on success or a negative number on failure.
  311. *
  312. * If the hardware lock is held then free it, and take it again for the kernel
  313. * context since it's necessary to reclaim buffers. Unlink the file private
  314. * data from its list and free it. Decreases the open count and if it reaches
  315. * zero calls drm_lastclose().
  316. */
  317. int drm_release(struct inode *inode, struct file *filp)
  318. {
  319. struct drm_file *file_priv = filp->private_data;
  320. struct drm_minor *minor = file_priv->minor;
  321. struct drm_device *dev = minor->dev;
  322. int retcode = 0;
  323. mutex_lock(&drm_global_mutex);
  324. DRM_DEBUG("open_count = %d\n", dev->open_count);
  325. mutex_lock(&dev->struct_mutex);
  326. list_del(&file_priv->lhead);
  327. mutex_unlock(&dev->struct_mutex);
  328. if (dev->driver->preclose)
  329. dev->driver->preclose(dev, file_priv);
  330. /* ========================================================
  331. * Begin inline drm_release
  332. */
  333. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  334. task_pid_nr(current),
  335. (long)old_encode_dev(file_priv->minor->kdev->devt),
  336. dev->open_count);
  337. /* Release any auth tokens that might point to this file_priv,
  338. (do that under the drm_global_mutex) */
  339. if (file_priv->magic)
  340. (void) drm_remove_magic(file_priv->master, file_priv->magic);
  341. /* if the master has gone away we can't do anything with the lock */
  342. if (file_priv->minor->master)
  343. drm_master_release(dev, filp);
  344. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  345. drm_legacy_reclaim_buffers(dev, file_priv);
  346. drm_events_release(file_priv);
  347. if (drm_core_check_feature(dev, DRIVER_MODESET))
  348. drm_fb_release(file_priv);
  349. if (drm_core_check_feature(dev, DRIVER_GEM))
  350. drm_gem_release(dev, file_priv);
  351. drm_legacy_ctxbitmap_flush(dev, file_priv);
  352. mutex_lock(&dev->master_mutex);
  353. if (file_priv->is_master) {
  354. struct drm_master *master = file_priv->master;
  355. /**
  356. * Since the master is disappearing, so is the
  357. * possibility to lock.
  358. */
  359. mutex_lock(&dev->struct_mutex);
  360. if (master->lock.hw_lock) {
  361. if (dev->sigdata.lock == master->lock.hw_lock)
  362. dev->sigdata.lock = NULL;
  363. master->lock.hw_lock = NULL;
  364. master->lock.file_priv = NULL;
  365. wake_up_interruptible_all(&master->lock.lock_queue);
  366. }
  367. mutex_unlock(&dev->struct_mutex);
  368. if (file_priv->minor->master == file_priv->master) {
  369. /* drop the reference held my the minor */
  370. if (dev->driver->master_drop)
  371. dev->driver->master_drop(dev, file_priv, true);
  372. drm_master_put(&file_priv->minor->master);
  373. }
  374. }
  375. /* drop the master reference held by the file priv */
  376. if (file_priv->master)
  377. drm_master_put(&file_priv->master);
  378. file_priv->is_master = 0;
  379. mutex_unlock(&dev->master_mutex);
  380. if (dev->driver->postclose)
  381. dev->driver->postclose(dev, file_priv);
  382. if (drm_core_check_feature(dev, DRIVER_PRIME))
  383. drm_prime_destroy_file_private(&file_priv->prime);
  384. WARN_ON(!list_empty(&file_priv->event_list));
  385. put_pid(file_priv->pid);
  386. kfree(file_priv);
  387. /* ========================================================
  388. * End inline drm_release
  389. */
  390. if (!--dev->open_count) {
  391. retcode = drm_lastclose(dev);
  392. if (drm_device_is_unplugged(dev))
  393. drm_put_dev(dev);
  394. }
  395. mutex_unlock(&drm_global_mutex);
  396. drm_minor_release(minor);
  397. return retcode;
  398. }
  399. EXPORT_SYMBOL(drm_release);
  400. ssize_t drm_read(struct file *filp, char __user *buffer,
  401. size_t count, loff_t *offset)
  402. {
  403. struct drm_file *file_priv = filp->private_data;
  404. struct drm_device *dev = file_priv->minor->dev;
  405. ssize_t ret = 0;
  406. if (!access_ok(VERIFY_WRITE, buffer, count))
  407. return -EFAULT;
  408. spin_lock_irq(&dev->event_lock);
  409. for (;;) {
  410. if (list_empty(&file_priv->event_list)) {
  411. if (ret)
  412. break;
  413. if (filp->f_flags & O_NONBLOCK) {
  414. ret = -EAGAIN;
  415. break;
  416. }
  417. spin_unlock_irq(&dev->event_lock);
  418. ret = wait_event_interruptible(file_priv->event_wait,
  419. !list_empty(&file_priv->event_list));
  420. spin_lock_irq(&dev->event_lock);
  421. if (ret < 0)
  422. break;
  423. ret = 0;
  424. } else {
  425. struct drm_pending_event *e;
  426. e = list_first_entry(&file_priv->event_list,
  427. struct drm_pending_event, link);
  428. if (e->event->length + ret > count)
  429. break;
  430. if (__copy_to_user_inatomic(buffer + ret,
  431. e->event, e->event->length)) {
  432. if (ret == 0)
  433. ret = -EFAULT;
  434. break;
  435. }
  436. file_priv->event_space += e->event->length;
  437. ret += e->event->length;
  438. list_del(&e->link);
  439. e->destroy(e);
  440. }
  441. }
  442. spin_unlock_irq(&dev->event_lock);
  443. return ret;
  444. }
  445. EXPORT_SYMBOL(drm_read);
  446. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  447. {
  448. struct drm_file *file_priv = filp->private_data;
  449. unsigned int mask = 0;
  450. poll_wait(filp, &file_priv->event_wait, wait);
  451. if (!list_empty(&file_priv->event_list))
  452. mask |= POLLIN | POLLRDNORM;
  453. return mask;
  454. }
  455. EXPORT_SYMBOL(drm_poll);