drm_fops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. * drm_new_set_master - Allocate a new master object and become master for the
  113. * associated master realm.
  114. *
  115. * @dev: The associated device.
  116. * @fpriv: File private identifying the client.
  117. *
  118. * This function must be called with dev::struct_mutex held.
  119. * Returns negative error code on failure. Zero on success.
  120. */
  121. int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
  122. {
  123. struct drm_master *old_master;
  124. int ret;
  125. lockdep_assert_held_once(&dev->master_mutex);
  126. /* create a new master */
  127. fpriv->minor->master = drm_master_create(fpriv->minor);
  128. if (!fpriv->minor->master)
  129. return -ENOMEM;
  130. /* take another reference for the copy in the local file priv */
  131. old_master = fpriv->master;
  132. fpriv->master = drm_master_get(fpriv->minor->master);
  133. if (dev->driver->master_create) {
  134. ret = dev->driver->master_create(dev, fpriv->master);
  135. if (ret)
  136. goto out_err;
  137. }
  138. if (dev->driver->master_set) {
  139. ret = dev->driver->master_set(dev, fpriv, true);
  140. if (ret)
  141. goto out_err;
  142. }
  143. fpriv->is_master = 1;
  144. fpriv->allowed_master = 1;
  145. fpriv->authenticated = 1;
  146. if (old_master)
  147. drm_master_put(&old_master);
  148. return 0;
  149. out_err:
  150. /* drop both references and restore old master on failure */
  151. drm_master_put(&fpriv->minor->master);
  152. drm_master_put(&fpriv->master);
  153. fpriv->master = old_master;
  154. return ret;
  155. }
  156. /**
  157. * Called whenever a process opens /dev/drm.
  158. *
  159. * \param filp file pointer.
  160. * \param minor acquired minor-object.
  161. * \return zero on success or a negative number on failure.
  162. *
  163. * Creates and initializes a drm_file structure for the file private data in \p
  164. * filp and add it into the double linked list in \p dev.
  165. */
  166. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  167. {
  168. struct drm_device *dev = minor->dev;
  169. struct drm_file *priv;
  170. int ret;
  171. if (filp->f_flags & O_EXCL)
  172. return -EBUSY; /* No exclusive opens */
  173. if (!drm_cpu_valid())
  174. return -EINVAL;
  175. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  176. return -EINVAL;
  177. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
  178. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  179. if (!priv)
  180. return -ENOMEM;
  181. filp->private_data = priv;
  182. filp->f_mode |= FMODE_UNSIGNED_OFFSET;
  183. priv->filp = filp;
  184. priv->uid = current_euid();
  185. priv->pid = get_pid(task_pid(current));
  186. priv->minor = minor;
  187. /* for compatibility root is always authenticated */
  188. priv->authenticated = capable(CAP_SYS_ADMIN);
  189. priv->lock_count = 0;
  190. INIT_LIST_HEAD(&priv->lhead);
  191. INIT_LIST_HEAD(&priv->fbs);
  192. mutex_init(&priv->fbs_lock);
  193. INIT_LIST_HEAD(&priv->blobs);
  194. INIT_LIST_HEAD(&priv->event_list);
  195. init_waitqueue_head(&priv->event_wait);
  196. priv->event_space = 4096; /* set aside 4k for event buffer */
  197. if (drm_core_check_feature(dev, DRIVER_GEM))
  198. drm_gem_open(dev, priv);
  199. if (drm_core_check_feature(dev, DRIVER_PRIME))
  200. drm_prime_init_file_private(&priv->prime);
  201. if (dev->driver->open) {
  202. ret = dev->driver->open(dev, priv);
  203. if (ret < 0)
  204. goto out_prime_destroy;
  205. }
  206. /* if there is no current master make this fd it, but do not create
  207. * any master object for render clients */
  208. mutex_lock(&dev->master_mutex);
  209. if (drm_is_primary_client(priv) && !priv->minor->master) {
  210. /* create a new master */
  211. ret = drm_new_set_master(dev, priv);
  212. if (ret)
  213. goto out_close;
  214. } else if (drm_is_primary_client(priv)) {
  215. /* get a reference to the master */
  216. priv->master = drm_master_get(priv->minor->master);
  217. }
  218. mutex_unlock(&dev->master_mutex);
  219. mutex_lock(&dev->struct_mutex);
  220. list_add(&priv->lhead, &dev->filelist);
  221. mutex_unlock(&dev->struct_mutex);
  222. #ifdef __alpha__
  223. /*
  224. * Default the hose
  225. */
  226. if (!dev->hose) {
  227. struct pci_dev *pci_dev;
  228. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  229. if (pci_dev) {
  230. dev->hose = pci_dev->sysdata;
  231. pci_dev_put(pci_dev);
  232. }
  233. if (!dev->hose) {
  234. struct pci_bus *b = list_entry(pci_root_buses.next,
  235. struct pci_bus, node);
  236. if (b)
  237. dev->hose = b->sysdata;
  238. }
  239. }
  240. #endif
  241. return 0;
  242. out_close:
  243. mutex_unlock(&dev->master_mutex);
  244. if (dev->driver->postclose)
  245. dev->driver->postclose(dev, priv);
  246. out_prime_destroy:
  247. if (drm_core_check_feature(dev, DRIVER_PRIME))
  248. drm_prime_destroy_file_private(&priv->prime);
  249. if (drm_core_check_feature(dev, DRIVER_GEM))
  250. drm_gem_release(dev, priv);
  251. put_pid(priv->pid);
  252. kfree(priv);
  253. filp->private_data = NULL;
  254. return ret;
  255. }
  256. static void drm_master_release(struct drm_device *dev, struct file *filp)
  257. {
  258. struct drm_file *file_priv = filp->private_data;
  259. if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
  260. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  261. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  262. drm_legacy_lock_free(&file_priv->master->lock,
  263. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  264. }
  265. }
  266. static void drm_events_release(struct drm_file *file_priv)
  267. {
  268. struct drm_device *dev = file_priv->minor->dev;
  269. struct drm_pending_event *e, *et;
  270. struct drm_pending_vblank_event *v, *vt;
  271. unsigned long flags;
  272. spin_lock_irqsave(&dev->event_lock, flags);
  273. /* Remove pending flips */
  274. list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
  275. if (v->base.file_priv == file_priv) {
  276. list_del(&v->base.link);
  277. drm_vblank_put(dev, v->pipe);
  278. v->base.destroy(&v->base);
  279. }
  280. /* Remove unconsumed events */
  281. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  282. list_del(&e->link);
  283. e->destroy(e);
  284. }
  285. spin_unlock_irqrestore(&dev->event_lock, flags);
  286. }
  287. /**
  288. * drm_legacy_dev_reinit
  289. *
  290. * Reinitializes a legacy/ums drm device in it's lastclose function.
  291. */
  292. static void drm_legacy_dev_reinit(struct drm_device *dev)
  293. {
  294. if (drm_core_check_feature(dev, DRIVER_MODESET))
  295. return;
  296. dev->sigdata.lock = NULL;
  297. dev->context_flag = 0;
  298. dev->last_context = 0;
  299. dev->if_version = 0;
  300. }
  301. /**
  302. * Take down the DRM device.
  303. *
  304. * \param dev DRM device structure.
  305. *
  306. * Frees every resource in \p dev.
  307. *
  308. * \sa drm_device
  309. */
  310. int drm_lastclose(struct drm_device * dev)
  311. {
  312. DRM_DEBUG("\n");
  313. if (dev->driver->lastclose)
  314. dev->driver->lastclose(dev);
  315. DRM_DEBUG("driver lastclose completed\n");
  316. if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
  317. drm_irq_uninstall(dev);
  318. mutex_lock(&dev->struct_mutex);
  319. drm_agp_clear(dev);
  320. drm_legacy_sg_cleanup(dev);
  321. drm_legacy_vma_flush(dev);
  322. drm_legacy_dma_takedown(dev);
  323. mutex_unlock(&dev->struct_mutex);
  324. drm_legacy_dev_reinit(dev);
  325. DRM_DEBUG("lastclose completed\n");
  326. return 0;
  327. }
  328. /**
  329. * Release file.
  330. *
  331. * \param inode device inode
  332. * \param file_priv DRM file private.
  333. * \return zero on success or a negative number on failure.
  334. *
  335. * If the hardware lock is held then free it, and take it again for the kernel
  336. * context since it's necessary to reclaim buffers. Unlink the file private
  337. * data from its list and free it. Decreases the open count and if it reaches
  338. * zero calls drm_lastclose().
  339. */
  340. int drm_release(struct inode *inode, struct file *filp)
  341. {
  342. struct drm_file *file_priv = filp->private_data;
  343. struct drm_minor *minor = file_priv->minor;
  344. struct drm_device *dev = minor->dev;
  345. int retcode = 0;
  346. mutex_lock(&drm_global_mutex);
  347. DRM_DEBUG("open_count = %d\n", dev->open_count);
  348. mutex_lock(&dev->struct_mutex);
  349. list_del(&file_priv->lhead);
  350. if (file_priv->magic)
  351. idr_remove(&file_priv->master->magic_map, file_priv->magic);
  352. mutex_unlock(&dev->struct_mutex);
  353. if (dev->driver->preclose)
  354. dev->driver->preclose(dev, file_priv);
  355. /* ========================================================
  356. * Begin inline drm_release
  357. */
  358. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  359. task_pid_nr(current),
  360. (long)old_encode_dev(file_priv->minor->kdev->devt),
  361. dev->open_count);
  362. /* if the master has gone away we can't do anything with the lock */
  363. if (file_priv->minor->master)
  364. drm_master_release(dev, filp);
  365. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  366. drm_legacy_reclaim_buffers(dev, file_priv);
  367. drm_events_release(file_priv);
  368. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  369. drm_fb_release(file_priv);
  370. drm_property_destroy_user_blobs(dev, file_priv);
  371. }
  372. if (drm_core_check_feature(dev, DRIVER_GEM))
  373. drm_gem_release(dev, file_priv);
  374. drm_legacy_ctxbitmap_flush(dev, file_priv);
  375. mutex_lock(&dev->master_mutex);
  376. if (file_priv->is_master) {
  377. struct drm_master *master = file_priv->master;
  378. /**
  379. * Since the master is disappearing, so is the
  380. * possibility to lock.
  381. */
  382. mutex_lock(&dev->struct_mutex);
  383. if (master->lock.hw_lock) {
  384. if (dev->sigdata.lock == master->lock.hw_lock)
  385. dev->sigdata.lock = NULL;
  386. master->lock.hw_lock = NULL;
  387. master->lock.file_priv = NULL;
  388. wake_up_interruptible_all(&master->lock.lock_queue);
  389. }
  390. mutex_unlock(&dev->struct_mutex);
  391. if (file_priv->minor->master == file_priv->master) {
  392. /* drop the reference held my the minor */
  393. if (dev->driver->master_drop)
  394. dev->driver->master_drop(dev, file_priv, true);
  395. drm_master_put(&file_priv->minor->master);
  396. }
  397. }
  398. /* drop the master reference held by the file priv */
  399. if (file_priv->master)
  400. drm_master_put(&file_priv->master);
  401. file_priv->is_master = 0;
  402. mutex_unlock(&dev->master_mutex);
  403. if (dev->driver->postclose)
  404. dev->driver->postclose(dev, file_priv);
  405. if (drm_core_check_feature(dev, DRIVER_PRIME))
  406. drm_prime_destroy_file_private(&file_priv->prime);
  407. WARN_ON(!list_empty(&file_priv->event_list));
  408. put_pid(file_priv->pid);
  409. kfree(file_priv);
  410. /* ========================================================
  411. * End inline drm_release
  412. */
  413. if (!--dev->open_count) {
  414. retcode = drm_lastclose(dev);
  415. if (drm_device_is_unplugged(dev))
  416. drm_put_dev(dev);
  417. }
  418. mutex_unlock(&drm_global_mutex);
  419. drm_minor_release(minor);
  420. return retcode;
  421. }
  422. EXPORT_SYMBOL(drm_release);
  423. ssize_t drm_read(struct file *filp, char __user *buffer,
  424. size_t count, loff_t *offset)
  425. {
  426. struct drm_file *file_priv = filp->private_data;
  427. struct drm_device *dev = file_priv->minor->dev;
  428. ssize_t ret = 0;
  429. if (!access_ok(VERIFY_WRITE, buffer, count))
  430. return -EFAULT;
  431. spin_lock_irq(&dev->event_lock);
  432. for (;;) {
  433. if (list_empty(&file_priv->event_list)) {
  434. if (ret)
  435. break;
  436. if (filp->f_flags & O_NONBLOCK) {
  437. ret = -EAGAIN;
  438. break;
  439. }
  440. spin_unlock_irq(&dev->event_lock);
  441. ret = wait_event_interruptible(file_priv->event_wait,
  442. !list_empty(&file_priv->event_list));
  443. spin_lock_irq(&dev->event_lock);
  444. if (ret < 0)
  445. break;
  446. ret = 0;
  447. } else {
  448. struct drm_pending_event *e;
  449. e = list_first_entry(&file_priv->event_list,
  450. struct drm_pending_event, link);
  451. if (e->event->length + ret > count)
  452. break;
  453. if (__copy_to_user_inatomic(buffer + ret,
  454. e->event, e->event->length)) {
  455. if (ret == 0)
  456. ret = -EFAULT;
  457. break;
  458. }
  459. file_priv->event_space += e->event->length;
  460. ret += e->event->length;
  461. list_del(&e->link);
  462. e->destroy(e);
  463. }
  464. }
  465. spin_unlock_irq(&dev->event_lock);
  466. return ret;
  467. }
  468. EXPORT_SYMBOL(drm_read);
  469. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  470. {
  471. struct drm_file *file_priv = filp->private_data;
  472. unsigned int mask = 0;
  473. poll_wait(filp, &file_priv->event_wait, wait);
  474. if (!list_empty(&file_priv->event_list))
  475. mask |= POLLIN | POLLRDNORM;
  476. return mask;
  477. }
  478. EXPORT_SYMBOL(drm_poll);