drm_fops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. #include "drm_crtc_internal.h"
  42. /* from BKL pushdown */
  43. DEFINE_MUTEX(drm_global_mutex);
  44. /**
  45. * DOC: file operations
  46. *
  47. * Drivers must define the file operations structure that forms the DRM
  48. * userspace API entry point, even though most of those operations are
  49. * implemented in the DRM core. The mandatory functions are drm_open(),
  50. * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
  51. * (note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n). Drivers which
  52. * implement private ioctls that require 32/64 bit compatibility support must
  53. * provide their own .compat_ioctl() handler that processes private ioctls and
  54. * calls 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. * The memory mapping implementation will vary depending on how the driver
  63. * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
  64. * function, modern drivers should use one of the provided memory-manager
  65. * specific implementations. For GEM-based drivers this is drm_gem_mmap().
  66. *
  67. * No other file operations are supported by the DRM userspace API. Overall the
  68. * following is an example #file_operations structure::
  69. *
  70. * static const example_drm_fops = {
  71. * .owner = THIS_MODULE,
  72. * .open = drm_open,
  73. * .release = drm_release,
  74. * .unlocked_ioctl = drm_ioctl,
  75. * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
  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_LEGACY)) {
  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. * Called whenever a process opens /dev/drm.
  154. *
  155. * \param filp file pointer.
  156. * \param minor acquired minor-object.
  157. * \return zero on success or a negative number on failure.
  158. *
  159. * Creates and initializes a drm_file structure for the file private data in \p
  160. * filp and add it into the double linked list in \p dev.
  161. */
  162. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  163. {
  164. struct drm_device *dev = minor->dev;
  165. struct drm_file *priv;
  166. int ret;
  167. if (filp->f_flags & O_EXCL)
  168. return -EBUSY; /* No exclusive opens */
  169. if (!drm_cpu_valid())
  170. return -EINVAL;
  171. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  172. return -EINVAL;
  173. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
  174. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  175. if (!priv)
  176. return -ENOMEM;
  177. filp->private_data = priv;
  178. priv->filp = filp;
  179. priv->pid = get_pid(task_pid(current));
  180. priv->minor = minor;
  181. /* for compatibility root is always authenticated */
  182. priv->authenticated = capable(CAP_SYS_ADMIN);
  183. priv->lock_count = 0;
  184. INIT_LIST_HEAD(&priv->lhead);
  185. INIT_LIST_HEAD(&priv->fbs);
  186. mutex_init(&priv->fbs_lock);
  187. INIT_LIST_HEAD(&priv->blobs);
  188. INIT_LIST_HEAD(&priv->pending_event_list);
  189. INIT_LIST_HEAD(&priv->event_list);
  190. init_waitqueue_head(&priv->event_wait);
  191. priv->event_space = 4096; /* set aside 4k for event buffer */
  192. mutex_init(&priv->event_read_lock);
  193. if (drm_core_check_feature(dev, DRIVER_GEM))
  194. drm_gem_open(dev, priv);
  195. if (drm_core_check_feature(dev, DRIVER_PRIME))
  196. drm_prime_init_file_private(&priv->prime);
  197. if (dev->driver->open) {
  198. ret = dev->driver->open(dev, priv);
  199. if (ret < 0)
  200. goto out_prime_destroy;
  201. }
  202. if (drm_is_primary_client(priv)) {
  203. ret = drm_master_open(priv);
  204. if (ret)
  205. goto out_close;
  206. }
  207. mutex_lock(&dev->filelist_mutex);
  208. list_add(&priv->lhead, &dev->filelist);
  209. mutex_unlock(&dev->filelist_mutex);
  210. #ifdef __alpha__
  211. /*
  212. * Default the hose
  213. */
  214. if (!dev->hose) {
  215. struct pci_dev *pci_dev;
  216. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  217. if (pci_dev) {
  218. dev->hose = pci_dev->sysdata;
  219. pci_dev_put(pci_dev);
  220. }
  221. if (!dev->hose) {
  222. struct pci_bus *b = list_entry(pci_root_buses.next,
  223. struct pci_bus, node);
  224. if (b)
  225. dev->hose = b->sysdata;
  226. }
  227. }
  228. #endif
  229. return 0;
  230. out_close:
  231. if (dev->driver->postclose)
  232. dev->driver->postclose(dev, priv);
  233. out_prime_destroy:
  234. if (drm_core_check_feature(dev, DRIVER_PRIME))
  235. drm_prime_destroy_file_private(&priv->prime);
  236. if (drm_core_check_feature(dev, DRIVER_GEM))
  237. drm_gem_release(dev, priv);
  238. put_pid(priv->pid);
  239. kfree(priv);
  240. filp->private_data = NULL;
  241. return ret;
  242. }
  243. static void drm_events_release(struct drm_file *file_priv)
  244. {
  245. struct drm_device *dev = file_priv->minor->dev;
  246. struct drm_pending_event *e, *et;
  247. unsigned long flags;
  248. spin_lock_irqsave(&dev->event_lock, flags);
  249. /* Unlink pending events */
  250. list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
  251. pending_link) {
  252. list_del(&e->pending_link);
  253. e->file_priv = NULL;
  254. }
  255. /* Remove unconsumed events */
  256. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  257. list_del(&e->link);
  258. kfree(e);
  259. }
  260. spin_unlock_irqrestore(&dev->event_lock, flags);
  261. }
  262. /*
  263. * drm_legacy_dev_reinit
  264. *
  265. * Reinitializes a legacy/ums drm device in it's lastclose function.
  266. */
  267. static void drm_legacy_dev_reinit(struct drm_device *dev)
  268. {
  269. if (dev->irq_enabled)
  270. drm_irq_uninstall(dev);
  271. mutex_lock(&dev->struct_mutex);
  272. drm_legacy_agp_clear(dev);
  273. drm_legacy_sg_cleanup(dev);
  274. drm_legacy_vma_flush(dev);
  275. drm_legacy_dma_takedown(dev);
  276. mutex_unlock(&dev->struct_mutex);
  277. dev->sigdata.lock = NULL;
  278. dev->context_flag = 0;
  279. dev->last_context = 0;
  280. dev->if_version = 0;
  281. DRM_DEBUG("lastclose completed\n");
  282. }
  283. /*
  284. * Take down the DRM device.
  285. *
  286. * \param dev DRM device structure.
  287. *
  288. * Frees every resource in \p dev.
  289. *
  290. * \sa drm_device
  291. */
  292. void drm_lastclose(struct drm_device * dev)
  293. {
  294. DRM_DEBUG("\n");
  295. if (dev->driver->lastclose)
  296. dev->driver->lastclose(dev);
  297. DRM_DEBUG("driver lastclose completed\n");
  298. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  299. drm_legacy_dev_reinit(dev);
  300. }
  301. /**
  302. * drm_release - release method for DRM file
  303. * @inode: device inode
  304. * @filp: file pointer.
  305. *
  306. * This function must be used by drivers as their .release() #file_operations
  307. * method. It frees any resources associated with the open file, and if this is
  308. * the last open file for the DRM device also proceeds to call drm_lastclose().
  309. *
  310. * RETURNS:
  311. *
  312. * Always succeeds and returns 0.
  313. */
  314. int drm_release(struct inode *inode, struct file *filp)
  315. {
  316. struct drm_file *file_priv = filp->private_data;
  317. struct drm_minor *minor = file_priv->minor;
  318. struct drm_device *dev = minor->dev;
  319. mutex_lock(&drm_global_mutex);
  320. DRM_DEBUG("open_count = %d\n", dev->open_count);
  321. mutex_lock(&dev->filelist_mutex);
  322. list_del(&file_priv->lhead);
  323. mutex_unlock(&dev->filelist_mutex);
  324. if (dev->driver->preclose)
  325. dev->driver->preclose(dev, file_priv);
  326. /* ========================================================
  327. * Begin inline drm_release
  328. */
  329. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  330. task_pid_nr(current),
  331. (long)old_encode_dev(file_priv->minor->kdev->devt),
  332. dev->open_count);
  333. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  334. drm_legacy_lock_release(dev, filp);
  335. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  336. drm_legacy_reclaim_buffers(dev, file_priv);
  337. drm_events_release(file_priv);
  338. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  339. drm_fb_release(file_priv);
  340. drm_property_destroy_user_blobs(dev, file_priv);
  341. }
  342. if (drm_core_check_feature(dev, DRIVER_GEM))
  343. drm_gem_release(dev, file_priv);
  344. drm_legacy_ctxbitmap_flush(dev, file_priv);
  345. if (drm_is_primary_client(file_priv))
  346. drm_master_release(file_priv);
  347. if (dev->driver->postclose)
  348. dev->driver->postclose(dev, file_priv);
  349. if (drm_core_check_feature(dev, DRIVER_PRIME))
  350. drm_prime_destroy_file_private(&file_priv->prime);
  351. WARN_ON(!list_empty(&file_priv->event_list));
  352. put_pid(file_priv->pid);
  353. kfree(file_priv);
  354. /* ========================================================
  355. * End inline drm_release
  356. */
  357. if (!--dev->open_count) {
  358. drm_lastclose(dev);
  359. if (drm_device_is_unplugged(dev))
  360. drm_put_dev(dev);
  361. }
  362. mutex_unlock(&drm_global_mutex);
  363. drm_minor_release(minor);
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(drm_release);
  367. /**
  368. * drm_read - read method for DRM file
  369. * @filp: file pointer
  370. * @buffer: userspace destination pointer for the read
  371. * @count: count in bytes to read
  372. * @offset: offset to read
  373. *
  374. * This function must be used by drivers as their .read() #file_operations
  375. * method iff they use DRM events for asynchronous signalling to userspace.
  376. * Since events are used by the KMS API for vblank and page flip completion this
  377. * means all modern display drivers must use it.
  378. *
  379. * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
  380. * must set the .llseek() #file_operation to no_llseek(). Polling support is
  381. * provided by drm_poll().
  382. *
  383. * This function will only ever read a full event. Therefore userspace must
  384. * supply a big enough buffer to fit any event to ensure forward progress. Since
  385. * the maximum event space is currently 4K it's recommended to just use that for
  386. * safety.
  387. *
  388. * RETURNS:
  389. *
  390. * Number of bytes read (always aligned to full events, and can be 0) or a
  391. * negative error code on failure.
  392. */
  393. ssize_t drm_read(struct file *filp, char __user *buffer,
  394. size_t count, loff_t *offset)
  395. {
  396. struct drm_file *file_priv = filp->private_data;
  397. struct drm_device *dev = file_priv->minor->dev;
  398. ssize_t ret;
  399. if (!access_ok(VERIFY_WRITE, buffer, count))
  400. return -EFAULT;
  401. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  402. if (ret)
  403. return ret;
  404. for (;;) {
  405. struct drm_pending_event *e = NULL;
  406. spin_lock_irq(&dev->event_lock);
  407. if (!list_empty(&file_priv->event_list)) {
  408. e = list_first_entry(&file_priv->event_list,
  409. struct drm_pending_event, link);
  410. file_priv->event_space += e->event->length;
  411. list_del(&e->link);
  412. }
  413. spin_unlock_irq(&dev->event_lock);
  414. if (e == NULL) {
  415. if (ret)
  416. break;
  417. if (filp->f_flags & O_NONBLOCK) {
  418. ret = -EAGAIN;
  419. break;
  420. }
  421. mutex_unlock(&file_priv->event_read_lock);
  422. ret = wait_event_interruptible(file_priv->event_wait,
  423. !list_empty(&file_priv->event_list));
  424. if (ret >= 0)
  425. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  426. if (ret)
  427. return ret;
  428. } else {
  429. unsigned length = e->event->length;
  430. if (length > count - ret) {
  431. put_back_event:
  432. spin_lock_irq(&dev->event_lock);
  433. file_priv->event_space -= length;
  434. list_add(&e->link, &file_priv->event_list);
  435. spin_unlock_irq(&dev->event_lock);
  436. break;
  437. }
  438. if (copy_to_user(buffer + ret, e->event, length)) {
  439. if (ret == 0)
  440. ret = -EFAULT;
  441. goto put_back_event;
  442. }
  443. ret += length;
  444. kfree(e);
  445. }
  446. }
  447. mutex_unlock(&file_priv->event_read_lock);
  448. return ret;
  449. }
  450. EXPORT_SYMBOL(drm_read);
  451. /**
  452. * drm_poll - poll method for DRM file
  453. * @filp: file pointer
  454. * @wait: poll waiter table
  455. *
  456. * This function must be used by drivers as their .read() #file_operations
  457. * method iff they use DRM events for asynchronous signalling to userspace.
  458. * Since events are used by the KMS API for vblank and page flip completion this
  459. * means all modern display drivers must use it.
  460. *
  461. * See also drm_read().
  462. *
  463. * RETURNS:
  464. *
  465. * Mask of POLL flags indicating the current status of the file.
  466. */
  467. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  468. {
  469. struct drm_file *file_priv = filp->private_data;
  470. unsigned int mask = 0;
  471. poll_wait(filp, &file_priv->event_wait, wait);
  472. if (!list_empty(&file_priv->event_list))
  473. mask |= POLLIN | POLLRDNORM;
  474. return mask;
  475. }
  476. EXPORT_SYMBOL(drm_poll);
  477. /**
  478. * drm_event_reserve_init_locked - init a DRM event and reserve space for it
  479. * @dev: DRM device
  480. * @file_priv: DRM file private data
  481. * @p: tracking structure for the pending event
  482. * @e: actual event data to deliver to userspace
  483. *
  484. * This function prepares the passed in event for eventual delivery. If the event
  485. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  486. * anything) then the even must be cancelled and freed using
  487. * drm_event_cancel_free(). Successfully initialized events should be sent out
  488. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  489. * asynchronous event to userspace.
  490. *
  491. * If callers embedded @p into a larger structure it must be allocated with
  492. * kmalloc and @p must be the first member element.
  493. *
  494. * This is the locked version of drm_event_reserve_init() for callers which
  495. * already hold &drm_device.event_lock.
  496. *
  497. * RETURNS:
  498. *
  499. * 0 on success or a negative error code on failure.
  500. */
  501. int drm_event_reserve_init_locked(struct drm_device *dev,
  502. struct drm_file *file_priv,
  503. struct drm_pending_event *p,
  504. struct drm_event *e)
  505. {
  506. if (file_priv->event_space < e->length)
  507. return -ENOMEM;
  508. file_priv->event_space -= e->length;
  509. p->event = e;
  510. list_add(&p->pending_link, &file_priv->pending_event_list);
  511. p->file_priv = file_priv;
  512. return 0;
  513. }
  514. EXPORT_SYMBOL(drm_event_reserve_init_locked);
  515. /**
  516. * drm_event_reserve_init - init a DRM event and reserve space for it
  517. * @dev: DRM device
  518. * @file_priv: DRM file private data
  519. * @p: tracking structure for the pending event
  520. * @e: actual event data to deliver to userspace
  521. *
  522. * This function prepares the passed in event for eventual delivery. If the event
  523. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  524. * anything) then the even must be cancelled and freed using
  525. * drm_event_cancel_free(). Successfully initialized events should be sent out
  526. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  527. * asynchronous event to userspace.
  528. *
  529. * If callers embedded @p into a larger structure it must be allocated with
  530. * kmalloc and @p must be the first member element.
  531. *
  532. * Callers which already hold &drm_device.event_lock should use
  533. * drm_event_reserve_init_locked() instead.
  534. *
  535. * RETURNS:
  536. *
  537. * 0 on success or a negative error code on failure.
  538. */
  539. int drm_event_reserve_init(struct drm_device *dev,
  540. struct drm_file *file_priv,
  541. struct drm_pending_event *p,
  542. struct drm_event *e)
  543. {
  544. unsigned long flags;
  545. int ret;
  546. spin_lock_irqsave(&dev->event_lock, flags);
  547. ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
  548. spin_unlock_irqrestore(&dev->event_lock, flags);
  549. return ret;
  550. }
  551. EXPORT_SYMBOL(drm_event_reserve_init);
  552. /**
  553. * drm_event_cancel_free - free a DRM event and release it's space
  554. * @dev: DRM device
  555. * @p: tracking structure for the pending event
  556. *
  557. * This function frees the event @p initialized with drm_event_reserve_init()
  558. * and releases any allocated space.
  559. */
  560. void drm_event_cancel_free(struct drm_device *dev,
  561. struct drm_pending_event *p)
  562. {
  563. unsigned long flags;
  564. spin_lock_irqsave(&dev->event_lock, flags);
  565. if (p->file_priv) {
  566. p->file_priv->event_space += p->event->length;
  567. list_del(&p->pending_link);
  568. }
  569. spin_unlock_irqrestore(&dev->event_lock, flags);
  570. if (p->fence)
  571. dma_fence_put(p->fence);
  572. kfree(p);
  573. }
  574. EXPORT_SYMBOL(drm_event_cancel_free);
  575. /**
  576. * drm_send_event_locked - send DRM event to file descriptor
  577. * @dev: DRM device
  578. * @e: DRM event to deliver
  579. *
  580. * This function sends the event @e, initialized with drm_event_reserve_init(),
  581. * to its associated userspace DRM file. Callers must already hold
  582. * &drm_device.event_lock, see drm_send_event() for the unlocked version.
  583. *
  584. * Note that the core will take care of unlinking and disarming events when the
  585. * corresponding DRM file is closed. Drivers need not worry about whether the
  586. * DRM file for this event still exists and can call this function upon
  587. * completion of the asynchronous work unconditionally.
  588. */
  589. void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
  590. {
  591. assert_spin_locked(&dev->event_lock);
  592. if (e->completion) {
  593. complete_all(e->completion);
  594. e->completion_release(e->completion);
  595. e->completion = NULL;
  596. }
  597. if (e->fence) {
  598. dma_fence_signal(e->fence);
  599. dma_fence_put(e->fence);
  600. }
  601. if (!e->file_priv) {
  602. kfree(e);
  603. return;
  604. }
  605. list_del(&e->pending_link);
  606. list_add_tail(&e->link,
  607. &e->file_priv->event_list);
  608. wake_up_interruptible(&e->file_priv->event_wait);
  609. }
  610. EXPORT_SYMBOL(drm_send_event_locked);
  611. /**
  612. * drm_send_event - send DRM event to file descriptor
  613. * @dev: DRM device
  614. * @e: DRM event to deliver
  615. *
  616. * This function sends the event @e, initialized with drm_event_reserve_init(),
  617. * to its associated userspace DRM file. This function acquires
  618. * &drm_device.event_lock, see drm_send_event_locked() for callers which already
  619. * hold this lock.
  620. *
  621. * Note that the core will take care of unlinking and disarming events when the
  622. * corresponding DRM file is closed. Drivers need not worry about whether the
  623. * DRM file for this event still exists and can call this function upon
  624. * completion of the asynchronous work unconditionally.
  625. */
  626. void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  627. {
  628. unsigned long irqflags;
  629. spin_lock_irqsave(&dev->event_lock, irqflags);
  630. drm_send_event_locked(dev, e);
  631. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  632. }
  633. EXPORT_SYMBOL(drm_send_event);