drm_file.c 21 KB

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