drm_file.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  3. * \author Daryll Strauss <daryll@valinux.com>
  4. * \author Gareth Hughes <gareth@valinux.com>
  5. */
  6. /*
  7. * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
  8. *
  9. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  10. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  11. * All Rights Reserved.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. * OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. #include <linux/poll.h>
  33. #include <linux/slab.h>
  34. #include <linux/module.h>
  35. #include <drm/drm_file.h>
  36. #include <drm/drmP.h>
  37. #include "drm_legacy.h"
  38. #include "drm_internal.h"
  39. #include "drm_crtc_internal.h"
  40. /* from BKL pushdown */
  41. DEFINE_MUTEX(drm_global_mutex);
  42. /**
  43. * DOC: file operations
  44. *
  45. * Drivers must define the file operations structure that forms the DRM
  46. * userspace API entry point, even though most of those operations are
  47. * implemented in the DRM core. The resulting &struct file_operations must be
  48. * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
  49. * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
  50. * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
  51. * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
  52. * that require 32/64 bit compatibility support must provide their own
  53. * &file_operations.compat_ioctl handler that processes private ioctls and calls
  54. * drm_compat_ioctl() for core ioctls.
  55. *
  56. * In addition drm_read() and drm_poll() provide support for DRM events. DRM
  57. * events are a generic and extensible means to send asynchronous events to
  58. * userspace through the file descriptor. They are used to send vblank event and
  59. * page flip completions by the KMS API. But drivers can also use it for their
  60. * own needs, e.g. to signal completion of rendering.
  61. *
  62. * For the driver-side event interface see drm_event_reserve_init() and
  63. * drm_send_event() as the main starting points.
  64. *
  65. * The memory mapping implementation will vary depending on how the driver
  66. * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
  67. * function, modern drivers should use one of the provided memory-manager
  68. * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
  69. * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
  70. *
  71. * No other file operations are supported by the DRM userspace API. Overall the
  72. * following is an example &file_operations structure::
  73. *
  74. * static const example_drm_fops = {
  75. * .owner = THIS_MODULE,
  76. * .open = drm_open,
  77. * .release = drm_release,
  78. * .unlocked_ioctl = drm_ioctl,
  79. * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
  80. * .poll = drm_poll,
  81. * .read = drm_read,
  82. * .llseek = no_llseek,
  83. * .mmap = drm_gem_mmap,
  84. * };
  85. *
  86. * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
  87. * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
  88. * simpler.
  89. *
  90. * The driver's &file_operations must be stored in &drm_driver.fops.
  91. *
  92. * For driver-private IOCTL handling see the more detailed discussion in
  93. * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
  94. */
  95. static int drm_open_helper(struct file *filp, struct drm_minor *minor);
  96. static int drm_setup(struct drm_device * dev)
  97. {
  98. int ret;
  99. if (dev->driver->firstopen &&
  100. drm_core_check_feature(dev, DRIVER_LEGACY)) {
  101. ret = dev->driver->firstopen(dev);
  102. if (ret != 0)
  103. return ret;
  104. }
  105. ret = drm_legacy_dma_setup(dev);
  106. if (ret < 0)
  107. return ret;
  108. DRM_DEBUG("\n");
  109. return 0;
  110. }
  111. /**
  112. * drm_open - open method for DRM file
  113. * @inode: device inode
  114. * @filp: file pointer.
  115. *
  116. * This function must be used by drivers as their &file_operations.open method.
  117. * It looks up the correct DRM device and instantiates all the per-file
  118. * resources for it. It also calls the &drm_driver.open driver callback.
  119. *
  120. * RETURNS:
  121. *
  122. * 0 on success or negative errno value on falure.
  123. */
  124. int drm_open(struct inode *inode, struct file *filp)
  125. {
  126. struct drm_device *dev;
  127. struct drm_minor *minor;
  128. int retcode;
  129. int need_setup = 0;
  130. minor = drm_minor_acquire(iminor(inode));
  131. if (IS_ERR(minor))
  132. return PTR_ERR(minor);
  133. dev = minor->dev;
  134. if (!dev->open_count++)
  135. need_setup = 1;
  136. /* share address_space across all char-devs of a single device */
  137. filp->f_mapping = dev->anon_inode->i_mapping;
  138. retcode = drm_open_helper(filp, minor);
  139. if (retcode)
  140. goto err_undo;
  141. if (need_setup) {
  142. retcode = drm_setup(dev);
  143. if (retcode)
  144. goto err_undo;
  145. }
  146. return 0;
  147. err_undo:
  148. dev->open_count--;
  149. drm_minor_release(minor);
  150. return retcode;
  151. }
  152. EXPORT_SYMBOL(drm_open);
  153. /*
  154. * Check whether DRI will run on this CPU.
  155. *
  156. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  157. */
  158. static int drm_cpu_valid(void)
  159. {
  160. #if defined(__sparc__) && !defined(__sparc_v9__)
  161. return 0; /* No cmpxchg before v9 sparc. */
  162. #endif
  163. return 1;
  164. }
  165. /*
  166. * Called whenever a process opens /dev/drm.
  167. *
  168. * \param filp file pointer.
  169. * \param minor acquired minor-object.
  170. * \return zero on success or a negative number on failure.
  171. *
  172. * Creates and initializes a drm_file structure for the file private data in \p
  173. * filp and add it into the double linked list in \p dev.
  174. */
  175. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  176. {
  177. struct drm_device *dev = minor->dev;
  178. struct drm_file *priv;
  179. int ret;
  180. if (filp->f_flags & O_EXCL)
  181. return -EBUSY; /* No exclusive opens */
  182. if (!drm_cpu_valid())
  183. return -EINVAL;
  184. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  185. return -EINVAL;
  186. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
  187. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  188. if (!priv)
  189. return -ENOMEM;
  190. filp->private_data = priv;
  191. filp->f_mode |= FMODE_UNSIGNED_OFFSET;
  192. priv->filp = filp;
  193. priv->pid = get_pid(task_pid(current));
  194. priv->minor = minor;
  195. /* for compatibility root is always authenticated */
  196. priv->authenticated = capable(CAP_SYS_ADMIN);
  197. priv->lock_count = 0;
  198. INIT_LIST_HEAD(&priv->lhead);
  199. INIT_LIST_HEAD(&priv->fbs);
  200. mutex_init(&priv->fbs_lock);
  201. INIT_LIST_HEAD(&priv->blobs);
  202. INIT_LIST_HEAD(&priv->pending_event_list);
  203. INIT_LIST_HEAD(&priv->event_list);
  204. init_waitqueue_head(&priv->event_wait);
  205. priv->event_space = 4096; /* set aside 4k for event buffer */
  206. mutex_init(&priv->event_read_lock);
  207. if (drm_core_check_feature(dev, DRIVER_GEM))
  208. drm_gem_open(dev, priv);
  209. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  210. drm_syncobj_open(priv);
  211. if (drm_core_check_feature(dev, DRIVER_PRIME))
  212. drm_prime_init_file_private(&priv->prime);
  213. if (dev->driver->open) {
  214. ret = dev->driver->open(dev, priv);
  215. if (ret < 0)
  216. goto out_prime_destroy;
  217. }
  218. if (drm_is_primary_client(priv)) {
  219. ret = drm_master_open(priv);
  220. if (ret)
  221. goto out_close;
  222. }
  223. mutex_lock(&dev->filelist_mutex);
  224. list_add(&priv->lhead, &dev->filelist);
  225. mutex_unlock(&dev->filelist_mutex);
  226. #ifdef __alpha__
  227. /*
  228. * Default the hose
  229. */
  230. if (!dev->hose) {
  231. struct pci_dev *pci_dev;
  232. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  233. if (pci_dev) {
  234. dev->hose = pci_dev->sysdata;
  235. pci_dev_put(pci_dev);
  236. }
  237. if (!dev->hose) {
  238. struct pci_bus *b = list_entry(pci_root_buses.next,
  239. struct pci_bus, node);
  240. if (b)
  241. dev->hose = b->sysdata;
  242. }
  243. }
  244. #endif
  245. return 0;
  246. out_close:
  247. if (dev->driver->postclose)
  248. dev->driver->postclose(dev, priv);
  249. out_prime_destroy:
  250. if (drm_core_check_feature(dev, DRIVER_PRIME))
  251. drm_prime_destroy_file_private(&priv->prime);
  252. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  253. drm_syncobj_release(priv);
  254. if (drm_core_check_feature(dev, DRIVER_GEM))
  255. drm_gem_release(dev, priv);
  256. put_pid(priv->pid);
  257. kfree(priv);
  258. filp->private_data = NULL;
  259. return ret;
  260. }
  261. static void drm_events_release(struct drm_file *file_priv)
  262. {
  263. struct drm_device *dev = file_priv->minor->dev;
  264. struct drm_pending_event *e, *et;
  265. unsigned long flags;
  266. spin_lock_irqsave(&dev->event_lock, flags);
  267. /* Unlink pending events */
  268. list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
  269. pending_link) {
  270. list_del(&e->pending_link);
  271. e->file_priv = NULL;
  272. }
  273. /* Remove unconsumed events */
  274. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  275. list_del(&e->link);
  276. kfree(e);
  277. }
  278. spin_unlock_irqrestore(&dev->event_lock, flags);
  279. }
  280. static void drm_legacy_dev_reinit(struct drm_device *dev)
  281. {
  282. if (dev->irq_enabled)
  283. drm_irq_uninstall(dev);
  284. mutex_lock(&dev->struct_mutex);
  285. drm_legacy_agp_clear(dev);
  286. drm_legacy_sg_cleanup(dev);
  287. drm_legacy_vma_flush(dev);
  288. drm_legacy_dma_takedown(dev);
  289. mutex_unlock(&dev->struct_mutex);
  290. dev->sigdata.lock = NULL;
  291. dev->context_flag = 0;
  292. dev->last_context = 0;
  293. dev->if_version = 0;
  294. DRM_DEBUG("lastclose completed\n");
  295. }
  296. void drm_lastclose(struct drm_device * dev)
  297. {
  298. DRM_DEBUG("\n");
  299. if (dev->driver->lastclose)
  300. dev->driver->lastclose(dev);
  301. DRM_DEBUG("driver lastclose completed\n");
  302. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  303. drm_legacy_dev_reinit(dev);
  304. }
  305. /**
  306. * drm_release - release method for DRM file
  307. * @inode: device inode
  308. * @filp: file pointer.
  309. *
  310. * This function must be used by drivers as their &file_operations.release
  311. * method. It frees any resources associated with the open file, and calls the
  312. * &drm_driver.postclose driver callback. If this is the last open file for the
  313. * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
  314. *
  315. * RETURNS:
  316. *
  317. * Always succeeds and returns 0.
  318. */
  319. int drm_release(struct inode *inode, struct file *filp)
  320. {
  321. struct drm_file *file_priv = filp->private_data;
  322. struct drm_minor *minor = file_priv->minor;
  323. struct drm_device *dev = minor->dev;
  324. mutex_lock(&drm_global_mutex);
  325. DRM_DEBUG("open_count = %d\n", dev->open_count);
  326. mutex_lock(&dev->filelist_mutex);
  327. list_del(&file_priv->lhead);
  328. mutex_unlock(&dev->filelist_mutex);
  329. if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
  330. dev->driver->preclose)
  331. dev->driver->preclose(dev, file_priv);
  332. /* ========================================================
  333. * Begin inline drm_release
  334. */
  335. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  336. task_pid_nr(current),
  337. (long)old_encode_dev(file_priv->minor->kdev->devt),
  338. dev->open_count);
  339. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  340. drm_legacy_lock_release(dev, filp);
  341. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  342. drm_legacy_reclaim_buffers(dev, file_priv);
  343. drm_events_release(file_priv);
  344. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  345. drm_fb_release(file_priv);
  346. drm_property_destroy_user_blobs(dev, file_priv);
  347. }
  348. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  349. drm_syncobj_release(file_priv);
  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. if (drm_is_primary_client(file_priv))
  354. drm_master_release(file_priv);
  355. if (dev->driver->postclose)
  356. dev->driver->postclose(dev, file_priv);
  357. if (drm_core_check_feature(dev, DRIVER_PRIME))
  358. drm_prime_destroy_file_private(&file_priv->prime);
  359. WARN_ON(!list_empty(&file_priv->event_list));
  360. put_pid(file_priv->pid);
  361. kfree(file_priv);
  362. /* ========================================================
  363. * End inline drm_release
  364. */
  365. if (!--dev->open_count) {
  366. drm_lastclose(dev);
  367. if (drm_dev_is_unplugged(dev))
  368. drm_put_dev(dev);
  369. }
  370. mutex_unlock(&drm_global_mutex);
  371. drm_minor_release(minor);
  372. return 0;
  373. }
  374. EXPORT_SYMBOL(drm_release);
  375. /**
  376. * drm_read - read method for DRM file
  377. * @filp: file pointer
  378. * @buffer: userspace destination pointer for the read
  379. * @count: count in bytes to read
  380. * @offset: offset to read
  381. *
  382. * This function must be used by drivers as their &file_operations.read
  383. * method iff they use DRM events for asynchronous signalling to userspace.
  384. * Since events are used by the KMS API for vblank and page flip completion this
  385. * means all modern display drivers must use it.
  386. *
  387. * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
  388. * must set the &file_operation.llseek to no_llseek(). Polling support is
  389. * provided by drm_poll().
  390. *
  391. * This function will only ever read a full event. Therefore userspace must
  392. * supply a big enough buffer to fit any event to ensure forward progress. Since
  393. * the maximum event space is currently 4K it's recommended to just use that for
  394. * safety.
  395. *
  396. * RETURNS:
  397. *
  398. * Number of bytes read (always aligned to full events, and can be 0) or a
  399. * negative error code on failure.
  400. */
  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;
  407. if (!access_ok(VERIFY_WRITE, buffer, count))
  408. return -EFAULT;
  409. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  410. if (ret)
  411. return ret;
  412. for (;;) {
  413. struct drm_pending_event *e = NULL;
  414. spin_lock_irq(&dev->event_lock);
  415. if (!list_empty(&file_priv->event_list)) {
  416. e = list_first_entry(&file_priv->event_list,
  417. struct drm_pending_event, link);
  418. file_priv->event_space += e->event->length;
  419. list_del(&e->link);
  420. }
  421. spin_unlock_irq(&dev->event_lock);
  422. if (e == NULL) {
  423. if (ret)
  424. break;
  425. if (filp->f_flags & O_NONBLOCK) {
  426. ret = -EAGAIN;
  427. break;
  428. }
  429. mutex_unlock(&file_priv->event_read_lock);
  430. ret = wait_event_interruptible(file_priv->event_wait,
  431. !list_empty(&file_priv->event_list));
  432. if (ret >= 0)
  433. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  434. if (ret)
  435. return ret;
  436. } else {
  437. unsigned length = e->event->length;
  438. if (length > count - ret) {
  439. put_back_event:
  440. spin_lock_irq(&dev->event_lock);
  441. file_priv->event_space -= length;
  442. list_add(&e->link, &file_priv->event_list);
  443. spin_unlock_irq(&dev->event_lock);
  444. break;
  445. }
  446. if (copy_to_user(buffer + ret, e->event, length)) {
  447. if (ret == 0)
  448. ret = -EFAULT;
  449. goto put_back_event;
  450. }
  451. ret += length;
  452. kfree(e);
  453. }
  454. }
  455. mutex_unlock(&file_priv->event_read_lock);
  456. return ret;
  457. }
  458. EXPORT_SYMBOL(drm_read);
  459. /**
  460. * drm_poll - poll method for DRM file
  461. * @filp: file pointer
  462. * @wait: poll waiter table
  463. *
  464. * This function must be used by drivers as their &file_operations.read method
  465. * iff they use DRM events for asynchronous signalling to userspace. Since
  466. * events are used by the KMS API for vblank and page flip completion this means
  467. * all modern display drivers must use it.
  468. *
  469. * See also drm_read().
  470. *
  471. * RETURNS:
  472. *
  473. * Mask of POLL flags indicating the current status of the file.
  474. */
  475. __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
  476. {
  477. struct drm_file *file_priv = filp->private_data;
  478. __poll_t mask = 0;
  479. poll_wait(filp, &file_priv->event_wait, wait);
  480. if (!list_empty(&file_priv->event_list))
  481. mask |= EPOLLIN | EPOLLRDNORM;
  482. return mask;
  483. }
  484. EXPORT_SYMBOL(drm_poll);
  485. /**
  486. * drm_event_reserve_init_locked - init a DRM event and reserve space for it
  487. * @dev: DRM device
  488. * @file_priv: DRM file private data
  489. * @p: tracking structure for the pending event
  490. * @e: actual event data to deliver to userspace
  491. *
  492. * This function prepares the passed in event for eventual delivery. If the event
  493. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  494. * anything) then the even must be cancelled and freed using
  495. * drm_event_cancel_free(). Successfully initialized events should be sent out
  496. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  497. * asynchronous event to userspace.
  498. *
  499. * If callers embedded @p into a larger structure it must be allocated with
  500. * kmalloc and @p must be the first member element.
  501. *
  502. * This is the locked version of drm_event_reserve_init() for callers which
  503. * already hold &drm_device.event_lock.
  504. *
  505. * RETURNS:
  506. *
  507. * 0 on success or a negative error code on failure.
  508. */
  509. int drm_event_reserve_init_locked(struct drm_device *dev,
  510. struct drm_file *file_priv,
  511. struct drm_pending_event *p,
  512. struct drm_event *e)
  513. {
  514. if (file_priv->event_space < e->length)
  515. return -ENOMEM;
  516. file_priv->event_space -= e->length;
  517. p->event = e;
  518. list_add(&p->pending_link, &file_priv->pending_event_list);
  519. p->file_priv = file_priv;
  520. return 0;
  521. }
  522. EXPORT_SYMBOL(drm_event_reserve_init_locked);
  523. /**
  524. * drm_event_reserve_init - init a DRM event and reserve space for it
  525. * @dev: DRM device
  526. * @file_priv: DRM file private data
  527. * @p: tracking structure for the pending event
  528. * @e: actual event data to deliver to userspace
  529. *
  530. * This function prepares the passed in event for eventual delivery. If the event
  531. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  532. * anything) then the even must be cancelled and freed using
  533. * drm_event_cancel_free(). Successfully initialized events should be sent out
  534. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  535. * asynchronous event to userspace.
  536. *
  537. * If callers embedded @p into a larger structure it must be allocated with
  538. * kmalloc and @p must be the first member element.
  539. *
  540. * Callers which already hold &drm_device.event_lock should use
  541. * drm_event_reserve_init_locked() instead.
  542. *
  543. * RETURNS:
  544. *
  545. * 0 on success or a negative error code on failure.
  546. */
  547. int drm_event_reserve_init(struct drm_device *dev,
  548. struct drm_file *file_priv,
  549. struct drm_pending_event *p,
  550. struct drm_event *e)
  551. {
  552. unsigned long flags;
  553. int ret;
  554. spin_lock_irqsave(&dev->event_lock, flags);
  555. ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
  556. spin_unlock_irqrestore(&dev->event_lock, flags);
  557. return ret;
  558. }
  559. EXPORT_SYMBOL(drm_event_reserve_init);
  560. /**
  561. * drm_event_cancel_free - free a DRM event and release it's space
  562. * @dev: DRM device
  563. * @p: tracking structure for the pending event
  564. *
  565. * This function frees the event @p initialized with drm_event_reserve_init()
  566. * and releases any allocated space. It is used to cancel an event when the
  567. * nonblocking operation could not be submitted and needed to be aborted.
  568. */
  569. void drm_event_cancel_free(struct drm_device *dev,
  570. struct drm_pending_event *p)
  571. {
  572. unsigned long flags;
  573. spin_lock_irqsave(&dev->event_lock, flags);
  574. if (p->file_priv) {
  575. p->file_priv->event_space += p->event->length;
  576. list_del(&p->pending_link);
  577. }
  578. spin_unlock_irqrestore(&dev->event_lock, flags);
  579. if (p->fence)
  580. dma_fence_put(p->fence);
  581. kfree(p);
  582. }
  583. EXPORT_SYMBOL(drm_event_cancel_free);
  584. /**
  585. * drm_send_event_locked - send DRM event to file descriptor
  586. * @dev: DRM device
  587. * @e: DRM event to deliver
  588. *
  589. * This function sends the event @e, initialized with drm_event_reserve_init(),
  590. * to its associated userspace DRM file. Callers must already hold
  591. * &drm_device.event_lock, see drm_send_event() for the unlocked version.
  592. *
  593. * Note that the core will take care of unlinking and disarming events when the
  594. * corresponding DRM file is closed. Drivers need not worry about whether the
  595. * DRM file for this event still exists and can call this function upon
  596. * completion of the asynchronous work unconditionally.
  597. */
  598. void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
  599. {
  600. assert_spin_locked(&dev->event_lock);
  601. if (e->completion) {
  602. complete_all(e->completion);
  603. e->completion_release(e->completion);
  604. e->completion = NULL;
  605. }
  606. if (e->fence) {
  607. dma_fence_signal(e->fence);
  608. dma_fence_put(e->fence);
  609. }
  610. if (!e->file_priv) {
  611. kfree(e);
  612. return;
  613. }
  614. list_del(&e->pending_link);
  615. list_add_tail(&e->link,
  616. &e->file_priv->event_list);
  617. wake_up_interruptible(&e->file_priv->event_wait);
  618. }
  619. EXPORT_SYMBOL(drm_send_event_locked);
  620. /**
  621. * drm_send_event - send DRM event to file descriptor
  622. * @dev: DRM device
  623. * @e: DRM event to deliver
  624. *
  625. * This function sends the event @e, initialized with drm_event_reserve_init(),
  626. * to its associated userspace DRM file. This function acquires
  627. * &drm_device.event_lock, see drm_send_event_locked() for callers which already
  628. * hold this lock.
  629. *
  630. * Note that the core will take care of unlinking and disarming events when the
  631. * corresponding DRM file is closed. Drivers need not worry about whether the
  632. * DRM file for this event still exists and can call this function upon
  633. * completion of the asynchronous work unconditionally.
  634. */
  635. void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  636. {
  637. unsigned long irqflags;
  638. spin_lock_irqsave(&dev->event_lock, irqflags);
  639. drm_send_event_locked(dev, e);
  640. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  641. }
  642. EXPORT_SYMBOL(drm_send_event);