drm_fops.c 14 KB

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