drm_file.c 26 KB

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