drm_file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. /**
  97. * drm_file_alloc - allocate file context
  98. * @minor: minor to allocate on
  99. *
  100. * This allocates a new DRM file context. It is not linked into any context and
  101. * can be used by the caller freely. Note that the context keeps a pointer to
  102. * @minor, so it must be freed before @minor is.
  103. *
  104. * RETURNS:
  105. * Pointer to newly allocated context, ERR_PTR on failure.
  106. */
  107. struct drm_file *drm_file_alloc(struct drm_minor *minor)
  108. {
  109. struct drm_device *dev = minor->dev;
  110. struct drm_file *file;
  111. int ret;
  112. file = kzalloc(sizeof(*file), GFP_KERNEL);
  113. if (!file)
  114. return ERR_PTR(-ENOMEM);
  115. file->pid = get_pid(task_pid(current));
  116. file->minor = minor;
  117. /* for compatibility root is always authenticated */
  118. file->authenticated = capable(CAP_SYS_ADMIN);
  119. file->lock_count = 0;
  120. INIT_LIST_HEAD(&file->lhead);
  121. INIT_LIST_HEAD(&file->fbs);
  122. mutex_init(&file->fbs_lock);
  123. INIT_LIST_HEAD(&file->blobs);
  124. INIT_LIST_HEAD(&file->pending_event_list);
  125. INIT_LIST_HEAD(&file->event_list);
  126. init_waitqueue_head(&file->event_wait);
  127. file->event_space = 4096; /* set aside 4k for event buffer */
  128. mutex_init(&file->event_read_lock);
  129. if (drm_core_check_feature(dev, DRIVER_GEM))
  130. drm_gem_open(dev, file);
  131. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  132. drm_syncobj_open(file);
  133. if (drm_core_check_feature(dev, DRIVER_PRIME))
  134. drm_prime_init_file_private(&file->prime);
  135. if (dev->driver->open) {
  136. ret = dev->driver->open(dev, file);
  137. if (ret < 0)
  138. goto out_prime_destroy;
  139. }
  140. return file;
  141. out_prime_destroy:
  142. if (drm_core_check_feature(dev, DRIVER_PRIME))
  143. drm_prime_destroy_file_private(&file->prime);
  144. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  145. drm_syncobj_release(file);
  146. if (drm_core_check_feature(dev, DRIVER_GEM))
  147. drm_gem_release(dev, file);
  148. put_pid(file->pid);
  149. kfree(file);
  150. return ERR_PTR(ret);
  151. }
  152. static void drm_events_release(struct drm_file *file_priv)
  153. {
  154. struct drm_device *dev = file_priv->minor->dev;
  155. struct drm_pending_event *e, *et;
  156. unsigned long flags;
  157. spin_lock_irqsave(&dev->event_lock, flags);
  158. /* Unlink pending events */
  159. list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
  160. pending_link) {
  161. list_del(&e->pending_link);
  162. e->file_priv = NULL;
  163. }
  164. /* Remove unconsumed events */
  165. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  166. list_del(&e->link);
  167. kfree(e);
  168. }
  169. spin_unlock_irqrestore(&dev->event_lock, flags);
  170. }
  171. /**
  172. * drm_file_free - free file context
  173. * @file: context to free, or NULL
  174. *
  175. * This destroys and deallocates a DRM file context previously allocated via
  176. * drm_file_alloc(). The caller must make sure to unlink it from any contexts
  177. * before calling this.
  178. *
  179. * If NULL is passed, this is a no-op.
  180. *
  181. * RETURNS:
  182. * 0 on success, or error code on failure.
  183. */
  184. void drm_file_free(struct drm_file *file)
  185. {
  186. struct drm_device *dev;
  187. if (!file)
  188. return;
  189. dev = file->minor->dev;
  190. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  191. task_pid_nr(current),
  192. (long)old_encode_dev(file->minor->kdev->devt),
  193. dev->open_count);
  194. if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
  195. dev->driver->preclose)
  196. dev->driver->preclose(dev, file);
  197. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  198. drm_legacy_lock_release(dev, file->filp);
  199. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  200. drm_legacy_reclaim_buffers(dev, file);
  201. drm_events_release(file);
  202. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  203. drm_fb_release(file);
  204. drm_property_destroy_user_blobs(dev, file);
  205. }
  206. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  207. drm_syncobj_release(file);
  208. if (drm_core_check_feature(dev, DRIVER_GEM))
  209. drm_gem_release(dev, file);
  210. drm_legacy_ctxbitmap_flush(dev, file);
  211. if (drm_is_primary_client(file))
  212. drm_master_release(file);
  213. if (dev->driver->postclose)
  214. dev->driver->postclose(dev, file);
  215. if (drm_core_check_feature(dev, DRIVER_PRIME))
  216. drm_prime_destroy_file_private(&file->prime);
  217. WARN_ON(!list_empty(&file->event_list));
  218. put_pid(file->pid);
  219. kfree(file);
  220. }
  221. static int drm_setup(struct drm_device * dev)
  222. {
  223. int ret;
  224. if (dev->driver->firstopen &&
  225. drm_core_check_feature(dev, DRIVER_LEGACY)) {
  226. ret = dev->driver->firstopen(dev);
  227. if (ret != 0)
  228. return ret;
  229. }
  230. ret = drm_legacy_dma_setup(dev);
  231. if (ret < 0)
  232. return ret;
  233. DRM_DEBUG("\n");
  234. return 0;
  235. }
  236. /**
  237. * drm_open - open method for DRM file
  238. * @inode: device inode
  239. * @filp: file pointer.
  240. *
  241. * This function must be used by drivers as their &file_operations.open method.
  242. * It looks up the correct DRM device and instantiates all the per-file
  243. * resources for it. It also calls the &drm_driver.open driver callback.
  244. *
  245. * RETURNS:
  246. *
  247. * 0 on success or negative errno value on falure.
  248. */
  249. int drm_open(struct inode *inode, struct file *filp)
  250. {
  251. struct drm_device *dev;
  252. struct drm_minor *minor;
  253. int retcode;
  254. int need_setup = 0;
  255. minor = drm_minor_acquire(iminor(inode));
  256. if (IS_ERR(minor))
  257. return PTR_ERR(minor);
  258. dev = minor->dev;
  259. if (!dev->open_count++)
  260. need_setup = 1;
  261. /* share address_space across all char-devs of a single device */
  262. filp->f_mapping = dev->anon_inode->i_mapping;
  263. retcode = drm_open_helper(filp, minor);
  264. if (retcode)
  265. goto err_undo;
  266. if (need_setup) {
  267. retcode = drm_setup(dev);
  268. if (retcode)
  269. goto err_undo;
  270. }
  271. return 0;
  272. err_undo:
  273. dev->open_count--;
  274. drm_minor_release(minor);
  275. return retcode;
  276. }
  277. EXPORT_SYMBOL(drm_open);
  278. /*
  279. * Check whether DRI will run on this CPU.
  280. *
  281. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  282. */
  283. static int drm_cpu_valid(void)
  284. {
  285. #if defined(__sparc__) && !defined(__sparc_v9__)
  286. return 0; /* No cmpxchg before v9 sparc. */
  287. #endif
  288. return 1;
  289. }
  290. /*
  291. * Called whenever a process opens /dev/drm.
  292. *
  293. * \param filp file pointer.
  294. * \param minor acquired minor-object.
  295. * \return zero on success or a negative number on failure.
  296. *
  297. * Creates and initializes a drm_file structure for the file private data in \p
  298. * filp and add it into the double linked list in \p dev.
  299. */
  300. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  301. {
  302. struct drm_device *dev = minor->dev;
  303. struct drm_file *priv;
  304. int ret;
  305. if (filp->f_flags & O_EXCL)
  306. return -EBUSY; /* No exclusive opens */
  307. if (!drm_cpu_valid())
  308. return -EINVAL;
  309. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  310. return -EINVAL;
  311. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
  312. priv = drm_file_alloc(minor);
  313. if (IS_ERR(priv))
  314. return PTR_ERR(priv);
  315. if (drm_is_primary_client(priv)) {
  316. ret = drm_master_open(priv);
  317. if (ret) {
  318. drm_file_free(priv);
  319. return ret;
  320. }
  321. }
  322. filp->private_data = priv;
  323. filp->f_mode |= FMODE_UNSIGNED_OFFSET;
  324. priv->filp = filp;
  325. mutex_lock(&dev->filelist_mutex);
  326. list_add(&priv->lhead, &dev->filelist);
  327. mutex_unlock(&dev->filelist_mutex);
  328. #ifdef __alpha__
  329. /*
  330. * Default the hose
  331. */
  332. if (!dev->hose) {
  333. struct pci_dev *pci_dev;
  334. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  335. if (pci_dev) {
  336. dev->hose = pci_dev->sysdata;
  337. pci_dev_put(pci_dev);
  338. }
  339. if (!dev->hose) {
  340. struct pci_bus *b = list_entry(pci_root_buses.next,
  341. struct pci_bus, node);
  342. if (b)
  343. dev->hose = b->sysdata;
  344. }
  345. }
  346. #endif
  347. return 0;
  348. }
  349. static void drm_legacy_dev_reinit(struct drm_device *dev)
  350. {
  351. if (dev->irq_enabled)
  352. drm_irq_uninstall(dev);
  353. mutex_lock(&dev->struct_mutex);
  354. drm_legacy_agp_clear(dev);
  355. drm_legacy_sg_cleanup(dev);
  356. drm_legacy_vma_flush(dev);
  357. drm_legacy_dma_takedown(dev);
  358. mutex_unlock(&dev->struct_mutex);
  359. dev->sigdata.lock = NULL;
  360. dev->context_flag = 0;
  361. dev->last_context = 0;
  362. dev->if_version = 0;
  363. DRM_DEBUG("lastclose completed\n");
  364. }
  365. void drm_lastclose(struct drm_device * dev)
  366. {
  367. DRM_DEBUG("\n");
  368. if (dev->driver->lastclose)
  369. dev->driver->lastclose(dev);
  370. DRM_DEBUG("driver lastclose completed\n");
  371. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  372. drm_legacy_dev_reinit(dev);
  373. }
  374. /**
  375. * drm_release - release method for DRM file
  376. * @inode: device inode
  377. * @filp: file pointer.
  378. *
  379. * This function must be used by drivers as their &file_operations.release
  380. * method. It frees any resources associated with the open file, and calls the
  381. * &drm_driver.postclose driver callback. If this is the last open file for the
  382. * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
  383. *
  384. * RETURNS:
  385. *
  386. * Always succeeds and returns 0.
  387. */
  388. int drm_release(struct inode *inode, struct file *filp)
  389. {
  390. struct drm_file *file_priv = filp->private_data;
  391. struct drm_minor *minor = file_priv->minor;
  392. struct drm_device *dev = minor->dev;
  393. mutex_lock(&drm_global_mutex);
  394. DRM_DEBUG("open_count = %d\n", dev->open_count);
  395. mutex_lock(&dev->filelist_mutex);
  396. list_del(&file_priv->lhead);
  397. mutex_unlock(&dev->filelist_mutex);
  398. drm_file_free(file_priv);
  399. if (!--dev->open_count) {
  400. drm_lastclose(dev);
  401. if (drm_dev_is_unplugged(dev))
  402. drm_put_dev(dev);
  403. }
  404. mutex_unlock(&drm_global_mutex);
  405. drm_minor_release(minor);
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(drm_release);
  409. /**
  410. * drm_read - read method for DRM file
  411. * @filp: file pointer
  412. * @buffer: userspace destination pointer for the read
  413. * @count: count in bytes to read
  414. * @offset: offset to read
  415. *
  416. * This function must be used by drivers as their &file_operations.read
  417. * method iff they use DRM events for asynchronous signalling to userspace.
  418. * Since events are used by the KMS API for vblank and page flip completion this
  419. * means all modern display drivers must use it.
  420. *
  421. * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
  422. * must set the &file_operation.llseek to no_llseek(). Polling support is
  423. * provided by drm_poll().
  424. *
  425. * This function will only ever read a full event. Therefore userspace must
  426. * supply a big enough buffer to fit any event to ensure forward progress. Since
  427. * the maximum event space is currently 4K it's recommended to just use that for
  428. * safety.
  429. *
  430. * RETURNS:
  431. *
  432. * Number of bytes read (always aligned to full events, and can be 0) or a
  433. * negative error code on failure.
  434. */
  435. ssize_t drm_read(struct file *filp, char __user *buffer,
  436. size_t count, loff_t *offset)
  437. {
  438. struct drm_file *file_priv = filp->private_data;
  439. struct drm_device *dev = file_priv->minor->dev;
  440. ssize_t ret;
  441. if (!access_ok(VERIFY_WRITE, buffer, count))
  442. return -EFAULT;
  443. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  444. if (ret)
  445. return ret;
  446. for (;;) {
  447. struct drm_pending_event *e = NULL;
  448. spin_lock_irq(&dev->event_lock);
  449. if (!list_empty(&file_priv->event_list)) {
  450. e = list_first_entry(&file_priv->event_list,
  451. struct drm_pending_event, link);
  452. file_priv->event_space += e->event->length;
  453. list_del(&e->link);
  454. }
  455. spin_unlock_irq(&dev->event_lock);
  456. if (e == NULL) {
  457. if (ret)
  458. break;
  459. if (filp->f_flags & O_NONBLOCK) {
  460. ret = -EAGAIN;
  461. break;
  462. }
  463. mutex_unlock(&file_priv->event_read_lock);
  464. ret = wait_event_interruptible(file_priv->event_wait,
  465. !list_empty(&file_priv->event_list));
  466. if (ret >= 0)
  467. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  468. if (ret)
  469. return ret;
  470. } else {
  471. unsigned length = e->event->length;
  472. if (length > count - ret) {
  473. put_back_event:
  474. spin_lock_irq(&dev->event_lock);
  475. file_priv->event_space -= length;
  476. list_add(&e->link, &file_priv->event_list);
  477. spin_unlock_irq(&dev->event_lock);
  478. break;
  479. }
  480. if (copy_to_user(buffer + ret, e->event, length)) {
  481. if (ret == 0)
  482. ret = -EFAULT;
  483. goto put_back_event;
  484. }
  485. ret += length;
  486. kfree(e);
  487. }
  488. }
  489. mutex_unlock(&file_priv->event_read_lock);
  490. return ret;
  491. }
  492. EXPORT_SYMBOL(drm_read);
  493. /**
  494. * drm_poll - poll method for DRM file
  495. * @filp: file pointer
  496. * @wait: poll waiter table
  497. *
  498. * This function must be used by drivers as their &file_operations.read method
  499. * iff they use DRM events for asynchronous signalling to userspace. Since
  500. * events are used by the KMS API for vblank and page flip completion this means
  501. * all modern display drivers must use it.
  502. *
  503. * See also drm_read().
  504. *
  505. * RETURNS:
  506. *
  507. * Mask of POLL flags indicating the current status of the file.
  508. */
  509. __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
  510. {
  511. struct drm_file *file_priv = filp->private_data;
  512. __poll_t mask = 0;
  513. poll_wait(filp, &file_priv->event_wait, wait);
  514. if (!list_empty(&file_priv->event_list))
  515. mask |= EPOLLIN | EPOLLRDNORM;
  516. return mask;
  517. }
  518. EXPORT_SYMBOL(drm_poll);
  519. /**
  520. * drm_event_reserve_init_locked - init a DRM event and reserve space for it
  521. * @dev: DRM device
  522. * @file_priv: DRM file private data
  523. * @p: tracking structure for the pending event
  524. * @e: actual event data to deliver to userspace
  525. *
  526. * This function prepares the passed in event for eventual delivery. If the event
  527. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  528. * anything) then the even must be cancelled and freed using
  529. * drm_event_cancel_free(). Successfully initialized events should be sent out
  530. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  531. * asynchronous event to userspace.
  532. *
  533. * If callers embedded @p into a larger structure it must be allocated with
  534. * kmalloc and @p must be the first member element.
  535. *
  536. * This is the locked version of drm_event_reserve_init() for callers which
  537. * already hold &drm_device.event_lock.
  538. *
  539. * RETURNS:
  540. *
  541. * 0 on success or a negative error code on failure.
  542. */
  543. int drm_event_reserve_init_locked(struct drm_device *dev,
  544. struct drm_file *file_priv,
  545. struct drm_pending_event *p,
  546. struct drm_event *e)
  547. {
  548. if (file_priv->event_space < e->length)
  549. return -ENOMEM;
  550. file_priv->event_space -= e->length;
  551. p->event = e;
  552. list_add(&p->pending_link, &file_priv->pending_event_list);
  553. p->file_priv = file_priv;
  554. return 0;
  555. }
  556. EXPORT_SYMBOL(drm_event_reserve_init_locked);
  557. /**
  558. * drm_event_reserve_init - init a DRM event and reserve space for it
  559. * @dev: DRM device
  560. * @file_priv: DRM file private data
  561. * @p: tracking structure for the pending event
  562. * @e: actual event data to deliver to userspace
  563. *
  564. * This function prepares the passed in event for eventual delivery. If the event
  565. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  566. * anything) then the even must be cancelled and freed using
  567. * drm_event_cancel_free(). Successfully initialized events should be sent out
  568. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  569. * asynchronous event to userspace.
  570. *
  571. * If callers embedded @p into a larger structure it must be allocated with
  572. * kmalloc and @p must be the first member element.
  573. *
  574. * Callers which already hold &drm_device.event_lock should use
  575. * drm_event_reserve_init_locked() instead.
  576. *
  577. * RETURNS:
  578. *
  579. * 0 on success or a negative error code on failure.
  580. */
  581. int drm_event_reserve_init(struct drm_device *dev,
  582. struct drm_file *file_priv,
  583. struct drm_pending_event *p,
  584. struct drm_event *e)
  585. {
  586. unsigned long flags;
  587. int ret;
  588. spin_lock_irqsave(&dev->event_lock, flags);
  589. ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
  590. spin_unlock_irqrestore(&dev->event_lock, flags);
  591. return ret;
  592. }
  593. EXPORT_SYMBOL(drm_event_reserve_init);
  594. /**
  595. * drm_event_cancel_free - free a DRM event and release it's space
  596. * @dev: DRM device
  597. * @p: tracking structure for the pending event
  598. *
  599. * This function frees the event @p initialized with drm_event_reserve_init()
  600. * and releases any allocated space. It is used to cancel an event when the
  601. * nonblocking operation could not be submitted and needed to be aborted.
  602. */
  603. void drm_event_cancel_free(struct drm_device *dev,
  604. struct drm_pending_event *p)
  605. {
  606. unsigned long flags;
  607. spin_lock_irqsave(&dev->event_lock, flags);
  608. if (p->file_priv) {
  609. p->file_priv->event_space += p->event->length;
  610. list_del(&p->pending_link);
  611. }
  612. spin_unlock_irqrestore(&dev->event_lock, flags);
  613. if (p->fence)
  614. dma_fence_put(p->fence);
  615. kfree(p);
  616. }
  617. EXPORT_SYMBOL(drm_event_cancel_free);
  618. /**
  619. * drm_send_event_locked - send DRM event to file descriptor
  620. * @dev: DRM device
  621. * @e: DRM event to deliver
  622. *
  623. * This function sends the event @e, initialized with drm_event_reserve_init(),
  624. * to its associated userspace DRM file. Callers must already hold
  625. * &drm_device.event_lock, see drm_send_event() for the unlocked version.
  626. *
  627. * Note that the core will take care of unlinking and disarming events when the
  628. * corresponding DRM file is closed. Drivers need not worry about whether the
  629. * DRM file for this event still exists and can call this function upon
  630. * completion of the asynchronous work unconditionally.
  631. */
  632. void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
  633. {
  634. assert_spin_locked(&dev->event_lock);
  635. if (e->completion) {
  636. complete_all(e->completion);
  637. e->completion_release(e->completion);
  638. e->completion = NULL;
  639. }
  640. if (e->fence) {
  641. dma_fence_signal(e->fence);
  642. dma_fence_put(e->fence);
  643. }
  644. if (!e->file_priv) {
  645. kfree(e);
  646. return;
  647. }
  648. list_del(&e->pending_link);
  649. list_add_tail(&e->link,
  650. &e->file_priv->event_list);
  651. wake_up_interruptible(&e->file_priv->event_wait);
  652. }
  653. EXPORT_SYMBOL(drm_send_event_locked);
  654. /**
  655. * drm_send_event - send DRM event to file descriptor
  656. * @dev: DRM device
  657. * @e: DRM event to deliver
  658. *
  659. * This function sends the event @e, initialized with drm_event_reserve_init(),
  660. * to its associated userspace DRM file. This function acquires
  661. * &drm_device.event_lock, see drm_send_event_locked() for callers which already
  662. * hold this lock.
  663. *
  664. * Note that the core will take care of unlinking and disarming events when the
  665. * corresponding DRM file is closed. Drivers need not worry about whether the
  666. * DRM file for this event still exists and can call this function upon
  667. * completion of the asynchronous work unconditionally.
  668. */
  669. void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  670. {
  671. unsigned long irqflags;
  672. spin_lock_irqsave(&dev->event_lock, irqflags);
  673. drm_send_event_locked(dev, e);
  674. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  675. }
  676. EXPORT_SYMBOL(drm_send_event);