drm_fops.c 20 KB

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