drm_fops.c 20 KB

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