drm_fops.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. /**
  44. * DOC: file operations
  45. *
  46. * Drivers must define the file operations structure that forms the DRM
  47. * userspace API entry point, even though most of those operations are
  48. * implemented in the DRM core. The mandatory functions are drm_open(),
  49. * drm_read(), drm_ioctl() and drm_compat_ioctl if CONFIG_COMPAT is enabled.
  50. * Drivers which implement private ioctls that require 32/64 bit compatibility
  51. * support must provided their onw .compat_ioctl() handler that processes
  52. * private ioctls and calls drm_compat_ioctl() for core ioctls.
  53. *
  54. * In addition drm_read() and drm_poll() provide support for DRM events. DRM
  55. * events are a generic and extensible means to send asynchronous events to
  56. * userspace through the file descriptor. They are used to send vblank event and
  57. * page flip completions by the KMS API. But drivers can also use it for their
  58. * own needs, e.g. to signal completion of rendering.
  59. *
  60. * The memory mapping implementation will vary depending on how the driver
  61. * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
  62. * function, modern drivers should use one of the provided memory-manager
  63. * specific implementations. For GEM-based drivers this is drm_gem_mmap().
  64. *
  65. * No other file operations are supported by the DRM userspace API. Overall the
  66. * following is an example #file_operations structure:
  67. *
  68. * static const example_drm_fops = {
  69. * .owner = THIS_MODULE,
  70. * .open = drm_open,
  71. * .release = drm_release,
  72. * .unlocked_ioctl = drm_ioctl,
  73. * #ifdef CONFIG_COMPAT
  74. * .compat_ioctl = drm_compat_ioctl,
  75. * #endif
  76. * .poll = drm_poll,
  77. * .read = drm_read,
  78. * .llseek = no_llseek,
  79. * .mmap = drm_gem_mmap,
  80. * };
  81. */
  82. static int drm_open_helper(struct file *filp, struct drm_minor *minor);
  83. static int drm_setup(struct drm_device * dev)
  84. {
  85. int ret;
  86. if (dev->driver->firstopen &&
  87. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  88. ret = dev->driver->firstopen(dev);
  89. if (ret != 0)
  90. return ret;
  91. }
  92. ret = drm_legacy_dma_setup(dev);
  93. if (ret < 0)
  94. return ret;
  95. DRM_DEBUG("\n");
  96. return 0;
  97. }
  98. /**
  99. * drm_open - open method for DRM file
  100. * @inode: device inode
  101. * @filp: file pointer.
  102. *
  103. * This function must be used by drivers as their .open() #file_operations
  104. * method. It looks up the correct DRM device and instantiates all the per-file
  105. * resources for it.
  106. *
  107. * RETURNS:
  108. *
  109. * 0 on success or negative errno value on falure.
  110. */
  111. int drm_open(struct inode *inode, struct file *filp)
  112. {
  113. struct drm_device *dev;
  114. struct drm_minor *minor;
  115. int retcode;
  116. int need_setup = 0;
  117. minor = drm_minor_acquire(iminor(inode));
  118. if (IS_ERR(minor))
  119. return PTR_ERR(minor);
  120. dev = minor->dev;
  121. if (!dev->open_count++)
  122. need_setup = 1;
  123. /* share address_space across all char-devs of a single device */
  124. filp->f_mapping = dev->anon_inode->i_mapping;
  125. retcode = drm_open_helper(filp, minor);
  126. if (retcode)
  127. goto err_undo;
  128. if (need_setup) {
  129. retcode = drm_setup(dev);
  130. if (retcode)
  131. goto err_undo;
  132. }
  133. return 0;
  134. err_undo:
  135. dev->open_count--;
  136. drm_minor_release(minor);
  137. return retcode;
  138. }
  139. EXPORT_SYMBOL(drm_open);
  140. /*
  141. * Check whether DRI will run on this CPU.
  142. *
  143. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  144. */
  145. static int drm_cpu_valid(void)
  146. {
  147. #if defined(__sparc__) && !defined(__sparc_v9__)
  148. return 0; /* No cmpxchg before v9 sparc. */
  149. #endif
  150. return 1;
  151. }
  152. /*
  153. * drm_new_set_master - Allocate a new master object and become master for the
  154. * associated master realm.
  155. *
  156. * @dev: The associated device.
  157. * @fpriv: File private identifying the client.
  158. *
  159. * This function must be called with dev::struct_mutex held.
  160. * Returns negative error code on failure. Zero on success.
  161. */
  162. int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
  163. {
  164. struct drm_master *old_master;
  165. int ret;
  166. lockdep_assert_held_once(&dev->master_mutex);
  167. /* create a new master */
  168. fpriv->minor->master = drm_master_create(fpriv->minor);
  169. if (!fpriv->minor->master)
  170. return -ENOMEM;
  171. /* take another reference for the copy in the local file priv */
  172. old_master = fpriv->master;
  173. fpriv->master = drm_master_get(fpriv->minor->master);
  174. if (dev->driver->master_create) {
  175. ret = dev->driver->master_create(dev, fpriv->master);
  176. if (ret)
  177. goto out_err;
  178. }
  179. if (dev->driver->master_set) {
  180. ret = dev->driver->master_set(dev, fpriv, true);
  181. if (ret)
  182. goto out_err;
  183. }
  184. fpriv->is_master = 1;
  185. fpriv->allowed_master = 1;
  186. fpriv->authenticated = 1;
  187. if (old_master)
  188. drm_master_put(&old_master);
  189. return 0;
  190. out_err:
  191. /* drop both references and restore old master on failure */
  192. drm_master_put(&fpriv->minor->master);
  193. drm_master_put(&fpriv->master);
  194. fpriv->master = old_master;
  195. return ret;
  196. }
  197. /*
  198. * Called whenever a process opens /dev/drm.
  199. *
  200. * \param filp file pointer.
  201. * \param minor acquired minor-object.
  202. * \return zero on success or a negative number on failure.
  203. *
  204. * Creates and initializes a drm_file structure for the file private data in \p
  205. * filp and add it into the double linked list in \p dev.
  206. */
  207. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  208. {
  209. struct drm_device *dev = minor->dev;
  210. struct drm_file *priv;
  211. int ret;
  212. if (filp->f_flags & O_EXCL)
  213. return -EBUSY; /* No exclusive opens */
  214. if (!drm_cpu_valid())
  215. return -EINVAL;
  216. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  217. return -EINVAL;
  218. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
  219. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  220. if (!priv)
  221. return -ENOMEM;
  222. filp->private_data = priv;
  223. priv->filp = filp;
  224. priv->uid = current_euid();
  225. priv->pid = get_pid(task_pid(current));
  226. priv->minor = minor;
  227. /* for compatibility root is always authenticated */
  228. priv->authenticated = capable(CAP_SYS_ADMIN);
  229. priv->lock_count = 0;
  230. INIT_LIST_HEAD(&priv->lhead);
  231. INIT_LIST_HEAD(&priv->fbs);
  232. mutex_init(&priv->fbs_lock);
  233. INIT_LIST_HEAD(&priv->blobs);
  234. INIT_LIST_HEAD(&priv->pending_event_list);
  235. INIT_LIST_HEAD(&priv->event_list);
  236. init_waitqueue_head(&priv->event_wait);
  237. priv->event_space = 4096; /* set aside 4k for event buffer */
  238. mutex_init(&priv->event_read_lock);
  239. if (drm_core_check_feature(dev, DRIVER_GEM))
  240. drm_gem_open(dev, priv);
  241. if (drm_core_check_feature(dev, DRIVER_PRIME))
  242. drm_prime_init_file_private(&priv->prime);
  243. if (dev->driver->open) {
  244. ret = dev->driver->open(dev, priv);
  245. if (ret < 0)
  246. goto out_prime_destroy;
  247. }
  248. /* if there is no current master make this fd it, but do not create
  249. * any master object for render clients */
  250. mutex_lock(&dev->master_mutex);
  251. if (drm_is_primary_client(priv) && !priv->minor->master) {
  252. /* create a new master */
  253. ret = drm_new_set_master(dev, priv);
  254. if (ret)
  255. goto out_close;
  256. } else if (drm_is_primary_client(priv)) {
  257. /* get a reference to the master */
  258. priv->master = drm_master_get(priv->minor->master);
  259. }
  260. mutex_unlock(&dev->master_mutex);
  261. mutex_lock(&dev->filelist_mutex);
  262. list_add(&priv->lhead, &dev->filelist);
  263. mutex_unlock(&dev->filelist_mutex);
  264. #ifdef __alpha__
  265. /*
  266. * Default the hose
  267. */
  268. if (!dev->hose) {
  269. struct pci_dev *pci_dev;
  270. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  271. if (pci_dev) {
  272. dev->hose = pci_dev->sysdata;
  273. pci_dev_put(pci_dev);
  274. }
  275. if (!dev->hose) {
  276. struct pci_bus *b = list_entry(pci_root_buses.next,
  277. struct pci_bus, node);
  278. if (b)
  279. dev->hose = b->sysdata;
  280. }
  281. }
  282. #endif
  283. return 0;
  284. out_close:
  285. mutex_unlock(&dev->master_mutex);
  286. if (dev->driver->postclose)
  287. dev->driver->postclose(dev, priv);
  288. out_prime_destroy:
  289. if (drm_core_check_feature(dev, DRIVER_PRIME))
  290. drm_prime_destroy_file_private(&priv->prime);
  291. if (drm_core_check_feature(dev, DRIVER_GEM))
  292. drm_gem_release(dev, priv);
  293. put_pid(priv->pid);
  294. kfree(priv);
  295. filp->private_data = NULL;
  296. return ret;
  297. }
  298. static void drm_master_release(struct drm_device *dev, struct file *filp)
  299. {
  300. struct drm_file *file_priv = filp->private_data;
  301. if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
  302. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  303. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  304. drm_legacy_lock_free(&file_priv->master->lock,
  305. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  306. }
  307. }
  308. static void drm_events_release(struct drm_file *file_priv)
  309. {
  310. struct drm_device *dev = file_priv->minor->dev;
  311. struct drm_pending_event *e, *et;
  312. unsigned long flags;
  313. spin_lock_irqsave(&dev->event_lock, flags);
  314. /* Unlink pending events */
  315. list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
  316. pending_link) {
  317. list_del(&e->pending_link);
  318. e->file_priv = NULL;
  319. }
  320. /* Remove unconsumed events */
  321. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  322. list_del(&e->link);
  323. e->destroy(e);
  324. }
  325. spin_unlock_irqrestore(&dev->event_lock, flags);
  326. }
  327. /*
  328. * drm_legacy_dev_reinit
  329. *
  330. * Reinitializes a legacy/ums drm device in it's lastclose function.
  331. */
  332. static void drm_legacy_dev_reinit(struct drm_device *dev)
  333. {
  334. if (dev->irq_enabled)
  335. drm_irq_uninstall(dev);
  336. mutex_lock(&dev->struct_mutex);
  337. drm_legacy_agp_clear(dev);
  338. drm_legacy_sg_cleanup(dev);
  339. drm_legacy_vma_flush(dev);
  340. drm_legacy_dma_takedown(dev);
  341. mutex_unlock(&dev->struct_mutex);
  342. dev->sigdata.lock = NULL;
  343. dev->context_flag = 0;
  344. dev->last_context = 0;
  345. dev->if_version = 0;
  346. DRM_DEBUG("lastclose completed\n");
  347. }
  348. /*
  349. * Take down the DRM device.
  350. *
  351. * \param dev DRM device structure.
  352. *
  353. * Frees every resource in \p dev.
  354. *
  355. * \sa drm_device
  356. */
  357. void drm_lastclose(struct drm_device * dev)
  358. {
  359. DRM_DEBUG("\n");
  360. if (dev->driver->lastclose)
  361. dev->driver->lastclose(dev);
  362. DRM_DEBUG("driver lastclose completed\n");
  363. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  364. drm_legacy_dev_reinit(dev);
  365. }
  366. /**
  367. * drm_release - release method for DRM file
  368. * @inode: device inode
  369. * @filp: file pointer.
  370. *
  371. * This function must be used by drivers as their .release() #file_operations
  372. * method. It frees any resources associated with the open file, and if this is
  373. * the last open file for the DRM device also proceeds to call drm_lastclose().
  374. *
  375. * RETURNS:
  376. *
  377. * Always succeeds and returns 0.
  378. */
  379. int drm_release(struct inode *inode, struct file *filp)
  380. {
  381. struct drm_file *file_priv = filp->private_data;
  382. struct drm_minor *minor = file_priv->minor;
  383. struct drm_device *dev = minor->dev;
  384. mutex_lock(&drm_global_mutex);
  385. DRM_DEBUG("open_count = %d\n", dev->open_count);
  386. mutex_lock(&dev->filelist_mutex);
  387. list_del(&file_priv->lhead);
  388. mutex_unlock(&dev->filelist_mutex);
  389. mutex_lock(&dev->struct_mutex);
  390. if (file_priv->magic)
  391. idr_remove(&file_priv->master->magic_map, file_priv->magic);
  392. mutex_unlock(&dev->struct_mutex);
  393. if (dev->driver->preclose)
  394. dev->driver->preclose(dev, file_priv);
  395. /* ========================================================
  396. * Begin inline drm_release
  397. */
  398. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  399. task_pid_nr(current),
  400. (long)old_encode_dev(file_priv->minor->kdev->devt),
  401. dev->open_count);
  402. /* if the master has gone away we can't do anything with the lock */
  403. if (file_priv->minor->master)
  404. drm_master_release(dev, filp);
  405. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  406. drm_legacy_reclaim_buffers(dev, file_priv);
  407. drm_events_release(file_priv);
  408. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  409. drm_fb_release(file_priv);
  410. drm_property_destroy_user_blobs(dev, file_priv);
  411. }
  412. if (drm_core_check_feature(dev, DRIVER_GEM))
  413. drm_gem_release(dev, file_priv);
  414. drm_legacy_ctxbitmap_flush(dev, file_priv);
  415. mutex_lock(&dev->master_mutex);
  416. if (file_priv->is_master) {
  417. struct drm_master *master = file_priv->master;
  418. /*
  419. * Since the master is disappearing, so is the
  420. * possibility to lock.
  421. */
  422. mutex_lock(&dev->struct_mutex);
  423. if (master->lock.hw_lock) {
  424. if (dev->sigdata.lock == master->lock.hw_lock)
  425. dev->sigdata.lock = NULL;
  426. master->lock.hw_lock = NULL;
  427. master->lock.file_priv = NULL;
  428. wake_up_interruptible_all(&master->lock.lock_queue);
  429. }
  430. mutex_unlock(&dev->struct_mutex);
  431. if (file_priv->minor->master == file_priv->master) {
  432. /* drop the reference held my the minor */
  433. if (dev->driver->master_drop)
  434. dev->driver->master_drop(dev, file_priv, true);
  435. drm_master_put(&file_priv->minor->master);
  436. }
  437. }
  438. /* drop the master reference held by the file priv */
  439. if (file_priv->master)
  440. drm_master_put(&file_priv->master);
  441. file_priv->is_master = 0;
  442. mutex_unlock(&dev->master_mutex);
  443. if (dev->driver->postclose)
  444. dev->driver->postclose(dev, file_priv);
  445. if (drm_core_check_feature(dev, DRIVER_PRIME))
  446. drm_prime_destroy_file_private(&file_priv->prime);
  447. WARN_ON(!list_empty(&file_priv->event_list));
  448. put_pid(file_priv->pid);
  449. kfree(file_priv);
  450. /* ========================================================
  451. * End inline drm_release
  452. */
  453. if (!--dev->open_count) {
  454. drm_lastclose(dev);
  455. if (drm_device_is_unplugged(dev))
  456. drm_put_dev(dev);
  457. }
  458. mutex_unlock(&drm_global_mutex);
  459. drm_minor_release(minor);
  460. return 0;
  461. }
  462. EXPORT_SYMBOL(drm_release);
  463. /**
  464. * drm_read - read method for DRM file
  465. * @filp: file pointer
  466. * @buffer: userspace destination pointer for the read
  467. * @count: count in bytes to read
  468. * @offset: offset to read
  469. *
  470. * This function must be used by drivers as their .read() #file_operations
  471. * method iff they use DRM events for asynchronous signalling to userspace.
  472. * Since events are used by the KMS API for vblank and page flip completion this
  473. * means all modern display drivers must use it.
  474. *
  475. * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
  476. * must set the .llseek() #file_operation to no_llseek(). Polling support is
  477. * provided by drm_poll().
  478. *
  479. * This function will only ever read a full event. Therefore userspace must
  480. * supply a big enough buffer to fit any event to ensure forward progress. Since
  481. * the maximum event space is currently 4K it's recommended to just use that for
  482. * safety.
  483. *
  484. * RETURNS:
  485. *
  486. * Number of bytes read (always aligned to full events, and can be 0) or a
  487. * negative error code on failure.
  488. */
  489. ssize_t drm_read(struct file *filp, char __user *buffer,
  490. size_t count, loff_t *offset)
  491. {
  492. struct drm_file *file_priv = filp->private_data;
  493. struct drm_device *dev = file_priv->minor->dev;
  494. ssize_t ret;
  495. if (!access_ok(VERIFY_WRITE, buffer, count))
  496. return -EFAULT;
  497. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  498. if (ret)
  499. return ret;
  500. for (;;) {
  501. struct drm_pending_event *e = NULL;
  502. spin_lock_irq(&dev->event_lock);
  503. if (!list_empty(&file_priv->event_list)) {
  504. e = list_first_entry(&file_priv->event_list,
  505. struct drm_pending_event, link);
  506. file_priv->event_space += e->event->length;
  507. list_del(&e->link);
  508. }
  509. spin_unlock_irq(&dev->event_lock);
  510. if (e == NULL) {
  511. if (ret)
  512. break;
  513. if (filp->f_flags & O_NONBLOCK) {
  514. ret = -EAGAIN;
  515. break;
  516. }
  517. mutex_unlock(&file_priv->event_read_lock);
  518. ret = wait_event_interruptible(file_priv->event_wait,
  519. !list_empty(&file_priv->event_list));
  520. if (ret >= 0)
  521. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  522. if (ret)
  523. return ret;
  524. } else {
  525. unsigned length = e->event->length;
  526. if (length > count - ret) {
  527. put_back_event:
  528. spin_lock_irq(&dev->event_lock);
  529. file_priv->event_space -= length;
  530. list_add(&e->link, &file_priv->event_list);
  531. spin_unlock_irq(&dev->event_lock);
  532. break;
  533. }
  534. if (copy_to_user(buffer + ret, e->event, length)) {
  535. if (ret == 0)
  536. ret = -EFAULT;
  537. goto put_back_event;
  538. }
  539. ret += length;
  540. e->destroy(e);
  541. }
  542. }
  543. mutex_unlock(&file_priv->event_read_lock);
  544. return ret;
  545. }
  546. EXPORT_SYMBOL(drm_read);
  547. /**
  548. * drm_poll - poll method for DRM file
  549. * @filp: file pointer
  550. * @wait: poll waiter table
  551. *
  552. * This function must be used by drivers as their .read() #file_operations
  553. * method iff they use DRM events for asynchronous signalling to userspace.
  554. * Since events are used by the KMS API for vblank and page flip completion this
  555. * means all modern display drivers must use it.
  556. *
  557. * See also drm_read().
  558. *
  559. * RETURNS:
  560. *
  561. * Mask of POLL flags indicating the current status of the file.
  562. */
  563. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  564. {
  565. struct drm_file *file_priv = filp->private_data;
  566. unsigned int mask = 0;
  567. poll_wait(filp, &file_priv->event_wait, wait);
  568. if (!list_empty(&file_priv->event_list))
  569. mask |= POLLIN | POLLRDNORM;
  570. return mask;
  571. }
  572. EXPORT_SYMBOL(drm_poll);
  573. /**
  574. * drm_event_reserve_init_locked - init a DRM event and reserve space for it
  575. * @dev: DRM device
  576. * @file_priv: DRM file private data
  577. * @p: tracking structure for the pending event
  578. * @e: actual event data to deliver to userspace
  579. *
  580. * This function prepares the passed in event for eventual delivery. If the event
  581. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  582. * anything) then the even must be cancelled and freed using
  583. * drm_event_cancel_free(). Successfully initialized events should be sent out
  584. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  585. * asynchronous event to userspace.
  586. *
  587. * If callers embedded @p into a larger structure it must be allocated with
  588. * kmalloc and @p must be the first member element.
  589. *
  590. * This is the locked version of drm_event_reserve_init() for callers which
  591. * already hold dev->event_lock.
  592. *
  593. * RETURNS:
  594. *
  595. * 0 on success or a negative error code on failure.
  596. */
  597. int drm_event_reserve_init_locked(struct drm_device *dev,
  598. struct drm_file *file_priv,
  599. struct drm_pending_event *p,
  600. struct drm_event *e)
  601. {
  602. if (file_priv->event_space < e->length)
  603. return -ENOMEM;
  604. file_priv->event_space -= e->length;
  605. p->event = e;
  606. list_add(&p->pending_link, &file_priv->pending_event_list);
  607. p->file_priv = file_priv;
  608. /* we *could* pass this in as arg, but everyone uses kfree: */
  609. p->destroy = (void (*) (struct drm_pending_event *)) kfree;
  610. return 0;
  611. }
  612. EXPORT_SYMBOL(drm_event_reserve_init_locked);
  613. /**
  614. * drm_event_reserve_init - init a DRM event and reserve space for it
  615. * @dev: DRM device
  616. * @file_priv: DRM file private data
  617. * @p: tracking structure for the pending event
  618. * @e: actual event data to deliver to userspace
  619. *
  620. * This function prepares the passed in event for eventual delivery. If the event
  621. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  622. * anything) then the even must be cancelled and freed using
  623. * drm_event_cancel_free(). Successfully initialized events should be sent out
  624. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  625. * asynchronous event to userspace.
  626. *
  627. * If callers embedded @p into a larger structure it must be allocated with
  628. * kmalloc and @p must be the first member element.
  629. *
  630. * Callers which already hold dev->event_lock should use
  631. * drm_event_reserve_init() instead.
  632. *
  633. * RETURNS:
  634. *
  635. * 0 on success or a negative error code on failure.
  636. */
  637. int drm_event_reserve_init(struct drm_device *dev,
  638. struct drm_file *file_priv,
  639. struct drm_pending_event *p,
  640. struct drm_event *e)
  641. {
  642. unsigned long flags;
  643. int ret;
  644. spin_lock_irqsave(&dev->event_lock, flags);
  645. ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
  646. spin_unlock_irqrestore(&dev->event_lock, flags);
  647. return ret;
  648. }
  649. EXPORT_SYMBOL(drm_event_reserve_init);
  650. /**
  651. * drm_event_cancel_free - free a DRM event and release it's space
  652. * @dev: DRM device
  653. * @p: tracking structure for the pending event
  654. *
  655. * This function frees the event @p initialized with drm_event_reserve_init()
  656. * and releases any allocated space.
  657. */
  658. void drm_event_cancel_free(struct drm_device *dev,
  659. struct drm_pending_event *p)
  660. {
  661. unsigned long flags;
  662. spin_lock_irqsave(&dev->event_lock, flags);
  663. if (p->file_priv) {
  664. p->file_priv->event_space += p->event->length;
  665. list_del(&p->pending_link);
  666. }
  667. spin_unlock_irqrestore(&dev->event_lock, flags);
  668. p->destroy(p);
  669. }
  670. EXPORT_SYMBOL(drm_event_cancel_free);
  671. /**
  672. * drm_send_event_locked - send DRM event to file descriptor
  673. * @dev: DRM device
  674. * @e: DRM event to deliver
  675. *
  676. * This function sends the event @e, initialized with drm_event_reserve_init(),
  677. * to its associated userspace DRM file. Callers must already hold
  678. * dev->event_lock, see drm_send_event() for the unlocked version.
  679. *
  680. * Note that the core will take care of unlinking and disarming events when the
  681. * corresponding DRM file is closed. Drivers need not worry about whether the
  682. * DRM file for this event still exists and can call this function upon
  683. * completion of the asynchronous work unconditionally.
  684. */
  685. void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
  686. {
  687. assert_spin_locked(&dev->event_lock);
  688. if (!e->file_priv) {
  689. e->destroy(e);
  690. return;
  691. }
  692. list_del(&e->pending_link);
  693. list_add_tail(&e->link,
  694. &e->file_priv->event_list);
  695. wake_up_interruptible(&e->file_priv->event_wait);
  696. }
  697. EXPORT_SYMBOL(drm_send_event_locked);
  698. /**
  699. * drm_send_event - send DRM event to file descriptor
  700. * @dev: DRM device
  701. * @e: DRM event to deliver
  702. *
  703. * This function sends the event @e, initialized with drm_event_reserve_init(),
  704. * to its associated userspace DRM file. This function acquires dev->event_lock,
  705. * see drm_send_event_locked() for callers which already hold this lock.
  706. *
  707. * Note that the core will take care of unlinking and disarming events when the
  708. * corresponding DRM file is closed. Drivers need not worry about whether the
  709. * DRM file for this event still exists and can call this function upon
  710. * completion of the asynchronous work unconditionally.
  711. */
  712. void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  713. {
  714. unsigned long irqflags;
  715. spin_lock_irqsave(&dev->event_lock, irqflags);
  716. drm_send_event_locked(dev, e);
  717. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  718. }
  719. EXPORT_SYMBOL(drm_send_event);