drm_file.c 22 KB

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