drm_irq.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /**
  2. * \file drm_irq.c
  3. * IRQ support
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <drm/drmP.h>
  35. #include "drm_trace.h"
  36. #include <linux/interrupt.h> /* For task queue support */
  37. #include <linux/slab.h>
  38. #include <linux/vgaarb.h>
  39. #include <linux/export.h>
  40. /* Access macro for slots in vblank timestamp ringbuffer. */
  41. #define vblanktimestamp(dev, crtc, count) \
  42. ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
  43. /* Retry timestamp calculation up to 3 times to satisfy
  44. * drm_timestamp_precision before giving up.
  45. */
  46. #define DRM_TIMESTAMP_MAXRETRIES 3
  47. /* Threshold in nanoseconds for detection of redundant
  48. * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  49. */
  50. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  51. /**
  52. * Get interrupt from bus id.
  53. *
  54. * \param inode device inode.
  55. * \param file_priv DRM file private.
  56. * \param cmd command.
  57. * \param arg user argument, pointing to a drm_irq_busid structure.
  58. * \return zero on success or a negative number on failure.
  59. *
  60. * Finds the PCI device with the specified bus id and gets its IRQ number.
  61. * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
  62. * to that of the device that this DRM instance attached to.
  63. */
  64. int drm_irq_by_busid(struct drm_device *dev, void *data,
  65. struct drm_file *file_priv)
  66. {
  67. struct drm_irq_busid *p = data;
  68. if (!dev->driver->bus->irq_by_busid)
  69. return -EINVAL;
  70. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  71. return -EINVAL;
  72. return dev->driver->bus->irq_by_busid(dev, p);
  73. }
  74. /*
  75. * Clear vblank timestamp buffer for a crtc.
  76. */
  77. static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
  78. {
  79. memset(dev->vblank[crtc].time, 0, sizeof(dev->vblank[crtc].time));
  80. }
  81. /*
  82. * Disable vblank irq's on crtc, make sure that last vblank count
  83. * of hardware and corresponding consistent software vblank counter
  84. * are preserved, even if there are any spurious vblank irq's after
  85. * disable.
  86. */
  87. static void vblank_disable_and_save(struct drm_device *dev, int crtc)
  88. {
  89. unsigned long irqflags;
  90. u32 vblcount;
  91. s64 diff_ns;
  92. int vblrc;
  93. struct timeval tvblank;
  94. int count = DRM_TIMESTAMP_MAXRETRIES;
  95. /* Prevent vblank irq processing while disabling vblank irqs,
  96. * so no updates of timestamps or count can happen after we've
  97. * disabled. Needed to prevent races in case of delayed irq's.
  98. */
  99. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  100. dev->driver->disable_vblank(dev, crtc);
  101. dev->vblank[crtc].enabled = false;
  102. /* No further vblank irq's will be processed after
  103. * this point. Get current hardware vblank count and
  104. * vblank timestamp, repeat until they are consistent.
  105. *
  106. * FIXME: There is still a race condition here and in
  107. * drm_update_vblank_count() which can cause off-by-one
  108. * reinitialization of software vblank counter. If gpu
  109. * vblank counter doesn't increment exactly at the leading
  110. * edge of a vblank interval, then we can lose 1 count if
  111. * we happen to execute between start of vblank and the
  112. * delayed gpu counter increment.
  113. */
  114. do {
  115. dev->vblank[crtc].last = dev->driver->get_vblank_counter(dev, crtc);
  116. vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
  117. } while (dev->vblank[crtc].last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
  118. if (!count)
  119. vblrc = 0;
  120. /* Compute time difference to stored timestamp of last vblank
  121. * as updated by last invocation of drm_handle_vblank() in vblank irq.
  122. */
  123. vblcount = atomic_read(&dev->vblank[crtc].count);
  124. diff_ns = timeval_to_ns(&tvblank) -
  125. timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
  126. /* If there is at least 1 msec difference between the last stored
  127. * timestamp and tvblank, then we are currently executing our
  128. * disable inside a new vblank interval, the tvblank timestamp
  129. * corresponds to this new vblank interval and the irq handler
  130. * for this vblank didn't run yet and won't run due to our disable.
  131. * Therefore we need to do the job of drm_handle_vblank() and
  132. * increment the vblank counter by one to account for this vblank.
  133. *
  134. * Skip this step if there isn't any high precision timestamp
  135. * available. In that case we can't account for this and just
  136. * hope for the best.
  137. */
  138. if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
  139. atomic_inc(&dev->vblank[crtc].count);
  140. smp_mb__after_atomic_inc();
  141. }
  142. /* Invalidate all timestamps while vblank irq's are off. */
  143. clear_vblank_timestamps(dev, crtc);
  144. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  145. }
  146. static void vblank_disable_fn(unsigned long arg)
  147. {
  148. struct drm_device *dev = (struct drm_device *)arg;
  149. unsigned long irqflags;
  150. int i;
  151. if (!dev->vblank_disable_allowed)
  152. return;
  153. for (i = 0; i < dev->num_crtcs; i++) {
  154. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  155. if (atomic_read(&dev->vblank[i].refcount) == 0 &&
  156. dev->vblank[i].enabled) {
  157. DRM_DEBUG("disabling vblank on crtc %d\n", i);
  158. vblank_disable_and_save(dev, i);
  159. }
  160. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  161. }
  162. }
  163. void drm_vblank_cleanup(struct drm_device *dev)
  164. {
  165. /* Bail if the driver didn't call drm_vblank_init() */
  166. if (dev->num_crtcs == 0)
  167. return;
  168. del_timer_sync(&dev->vblank_disable_timer);
  169. vblank_disable_fn((unsigned long)dev);
  170. kfree(dev->vblank);
  171. dev->num_crtcs = 0;
  172. }
  173. EXPORT_SYMBOL(drm_vblank_cleanup);
  174. int drm_vblank_init(struct drm_device *dev, int num_crtcs)
  175. {
  176. int i, ret = -ENOMEM;
  177. setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
  178. (unsigned long)dev);
  179. spin_lock_init(&dev->vbl_lock);
  180. spin_lock_init(&dev->vblank_time_lock);
  181. dev->num_crtcs = num_crtcs;
  182. dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  183. if (!dev->vblank)
  184. goto err;
  185. for (i = 0; i < num_crtcs; i++)
  186. init_waitqueue_head(&dev->vblank[i].queue);
  187. DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
  188. /* Driver specific high-precision vblank timestamping supported? */
  189. if (dev->driver->get_vblank_timestamp)
  190. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  191. else
  192. DRM_INFO("No driver support for vblank timestamp query.\n");
  193. dev->vblank_disable_allowed = false;
  194. return 0;
  195. err:
  196. drm_vblank_cleanup(dev);
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(drm_vblank_init);
  200. static void drm_irq_vgaarb_nokms(void *cookie, bool state)
  201. {
  202. struct drm_device *dev = cookie;
  203. if (dev->driver->vgaarb_irq) {
  204. dev->driver->vgaarb_irq(dev, state);
  205. return;
  206. }
  207. if (!dev->irq_enabled)
  208. return;
  209. if (state) {
  210. if (dev->driver->irq_uninstall)
  211. dev->driver->irq_uninstall(dev);
  212. } else {
  213. if (dev->driver->irq_preinstall)
  214. dev->driver->irq_preinstall(dev);
  215. if (dev->driver->irq_postinstall)
  216. dev->driver->irq_postinstall(dev);
  217. }
  218. }
  219. /**
  220. * Install IRQ handler.
  221. *
  222. * \param dev DRM device.
  223. *
  224. * Initializes the IRQ related data. Installs the handler, calling the driver
  225. * \c irq_preinstall() and \c irq_postinstall() functions
  226. * before and after the installation.
  227. */
  228. int drm_irq_install(struct drm_device *dev)
  229. {
  230. int ret;
  231. unsigned long sh_flags = 0;
  232. char *irqname;
  233. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  234. return -EINVAL;
  235. if (drm_dev_to_irq(dev) == 0)
  236. return -EINVAL;
  237. mutex_lock(&dev->struct_mutex);
  238. /* Driver must have been initialized */
  239. if (!dev->dev_private) {
  240. mutex_unlock(&dev->struct_mutex);
  241. return -EINVAL;
  242. }
  243. if (dev->irq_enabled) {
  244. mutex_unlock(&dev->struct_mutex);
  245. return -EBUSY;
  246. }
  247. dev->irq_enabled = 1;
  248. mutex_unlock(&dev->struct_mutex);
  249. DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
  250. /* Before installing handler */
  251. if (dev->driver->irq_preinstall)
  252. dev->driver->irq_preinstall(dev);
  253. /* Install handler */
  254. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  255. sh_flags = IRQF_SHARED;
  256. if (dev->devname)
  257. irqname = dev->devname;
  258. else
  259. irqname = dev->driver->name;
  260. ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
  261. sh_flags, irqname, dev);
  262. if (ret < 0) {
  263. mutex_lock(&dev->struct_mutex);
  264. dev->irq_enabled = 0;
  265. mutex_unlock(&dev->struct_mutex);
  266. return ret;
  267. }
  268. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  269. vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
  270. /* After installing handler */
  271. if (dev->driver->irq_postinstall)
  272. ret = dev->driver->irq_postinstall(dev);
  273. if (ret < 0) {
  274. mutex_lock(&dev->struct_mutex);
  275. dev->irq_enabled = 0;
  276. mutex_unlock(&dev->struct_mutex);
  277. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  278. vga_client_register(dev->pdev, NULL, NULL, NULL);
  279. free_irq(drm_dev_to_irq(dev), dev);
  280. }
  281. return ret;
  282. }
  283. EXPORT_SYMBOL(drm_irq_install);
  284. /**
  285. * Uninstall the IRQ handler.
  286. *
  287. * \param dev DRM device.
  288. *
  289. * Calls the driver's \c irq_uninstall() function, and stops the irq.
  290. */
  291. int drm_irq_uninstall(struct drm_device *dev)
  292. {
  293. unsigned long irqflags;
  294. int irq_enabled, i;
  295. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  296. return -EINVAL;
  297. mutex_lock(&dev->struct_mutex);
  298. irq_enabled = dev->irq_enabled;
  299. dev->irq_enabled = 0;
  300. mutex_unlock(&dev->struct_mutex);
  301. /*
  302. * Wake up any waiters so they don't hang.
  303. */
  304. if (dev->num_crtcs) {
  305. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  306. for (i = 0; i < dev->num_crtcs; i++) {
  307. DRM_WAKEUP(&dev->vblank[i].queue);
  308. dev->vblank[i].enabled = false;
  309. dev->vblank[i].last =
  310. dev->driver->get_vblank_counter(dev, i);
  311. }
  312. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  313. }
  314. if (!irq_enabled)
  315. return -EINVAL;
  316. DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
  317. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  318. vga_client_register(dev->pdev, NULL, NULL, NULL);
  319. if (dev->driver->irq_uninstall)
  320. dev->driver->irq_uninstall(dev);
  321. free_irq(drm_dev_to_irq(dev), dev);
  322. return 0;
  323. }
  324. EXPORT_SYMBOL(drm_irq_uninstall);
  325. /**
  326. * IRQ control ioctl.
  327. *
  328. * \param inode device inode.
  329. * \param file_priv DRM file private.
  330. * \param cmd command.
  331. * \param arg user argument, pointing to a drm_control structure.
  332. * \return zero on success or a negative number on failure.
  333. *
  334. * Calls irq_install() or irq_uninstall() according to \p arg.
  335. */
  336. int drm_control(struct drm_device *dev, void *data,
  337. struct drm_file *file_priv)
  338. {
  339. struct drm_control *ctl = data;
  340. /* if we haven't irq we fallback for compatibility reasons -
  341. * this used to be a separate function in drm_dma.h
  342. */
  343. switch (ctl->func) {
  344. case DRM_INST_HANDLER:
  345. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  346. return 0;
  347. if (drm_core_check_feature(dev, DRIVER_MODESET))
  348. return 0;
  349. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  350. ctl->irq != drm_dev_to_irq(dev))
  351. return -EINVAL;
  352. return drm_irq_install(dev);
  353. case DRM_UNINST_HANDLER:
  354. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  355. return 0;
  356. if (drm_core_check_feature(dev, DRIVER_MODESET))
  357. return 0;
  358. return drm_irq_uninstall(dev);
  359. default:
  360. return -EINVAL;
  361. }
  362. }
  363. /**
  364. * drm_calc_timestamping_constants - Calculate and
  365. * store various constants which are later needed by
  366. * vblank and swap-completion timestamping, e.g, by
  367. * drm_calc_vbltimestamp_from_scanoutpos().
  368. * They are derived from crtc's true scanout timing,
  369. * so they take things like panel scaling or other
  370. * adjustments into account.
  371. *
  372. * @crtc drm_crtc whose timestamp constants should be updated.
  373. *
  374. */
  375. void drm_calc_timestamping_constants(struct drm_crtc *crtc)
  376. {
  377. s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
  378. u64 dotclock;
  379. /* Dot clock in Hz: */
  380. dotclock = (u64) crtc->hwmode.clock * 1000;
  381. /* Fields of interlaced scanout modes are only halve a frame duration.
  382. * Double the dotclock to get halve the frame-/line-/pixelduration.
  383. */
  384. if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
  385. dotclock *= 2;
  386. /* Valid dotclock? */
  387. if (dotclock > 0) {
  388. int frame_size;
  389. /* Convert scanline length in pixels and video dot clock to
  390. * line duration, frame duration and pixel duration in
  391. * nanoseconds:
  392. */
  393. pixeldur_ns = (s64) div64_u64(1000000000, dotclock);
  394. linedur_ns = (s64) div64_u64(((u64) crtc->hwmode.crtc_htotal *
  395. 1000000000), dotclock);
  396. frame_size = crtc->hwmode.crtc_htotal *
  397. crtc->hwmode.crtc_vtotal;
  398. framedur_ns = (s64) div64_u64((u64) frame_size * 1000000000,
  399. dotclock);
  400. } else
  401. DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
  402. crtc->base.id);
  403. crtc->pixeldur_ns = pixeldur_ns;
  404. crtc->linedur_ns = linedur_ns;
  405. crtc->framedur_ns = framedur_ns;
  406. DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  407. crtc->base.id, crtc->hwmode.crtc_htotal,
  408. crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
  409. DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
  410. crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
  411. (int) linedur_ns, (int) pixeldur_ns);
  412. }
  413. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  414. /**
  415. * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
  416. * drivers. Implements calculation of exact vblank timestamps from
  417. * given drm_display_mode timings and current video scanout position
  418. * of a crtc. This can be called from within get_vblank_timestamp()
  419. * implementation of a kms driver to implement the actual timestamping.
  420. *
  421. * Should return timestamps conforming to the OML_sync_control OpenML
  422. * extension specification. The timestamp corresponds to the end of
  423. * the vblank interval, aka start of scanout of topmost-leftmost display
  424. * pixel in the following video frame.
  425. *
  426. * Requires support for optional dev->driver->get_scanout_position()
  427. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  428. * that corresponds to the true scanout timing.
  429. *
  430. * The current implementation only handles standard video modes. It
  431. * returns as no operation if a doublescan or interlaced video mode is
  432. * active. Higher level code is expected to handle this.
  433. *
  434. * @dev: DRM device.
  435. * @crtc: Which crtc's vblank timestamp to retrieve.
  436. * @max_error: Desired maximum allowable error in timestamps (nanosecs).
  437. * On return contains true maximum error of timestamp.
  438. * @vblank_time: Pointer to struct timeval which should receive the timestamp.
  439. * @flags: Flags to pass to driver:
  440. * 0 = Default.
  441. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
  442. * @refcrtc: drm_crtc* of crtc which defines scanout timing.
  443. *
  444. * Returns negative value on error, failure or if not supported in current
  445. * video mode:
  446. *
  447. * -EINVAL - Invalid crtc.
  448. * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
  449. * -ENOTSUPP - Function not supported in current display mode.
  450. * -EIO - Failed, e.g., due to failed scanout position query.
  451. *
  452. * Returns or'ed positive status flags on success:
  453. *
  454. * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  455. * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  456. *
  457. */
  458. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
  459. int *max_error,
  460. struct timeval *vblank_time,
  461. unsigned flags,
  462. struct drm_crtc *refcrtc)
  463. {
  464. ktime_t stime, etime, mono_time_offset;
  465. struct timeval tv_etime;
  466. struct drm_display_mode *mode;
  467. int vbl_status, vtotal, vdisplay;
  468. int vpos, hpos, i;
  469. s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
  470. bool invbl;
  471. if (crtc < 0 || crtc >= dev->num_crtcs) {
  472. DRM_ERROR("Invalid crtc %d\n", crtc);
  473. return -EINVAL;
  474. }
  475. /* Scanout position query not supported? Should not happen. */
  476. if (!dev->driver->get_scanout_position) {
  477. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  478. return -EIO;
  479. }
  480. mode = &refcrtc->hwmode;
  481. vtotal = mode->crtc_vtotal;
  482. vdisplay = mode->crtc_vdisplay;
  483. /* Durations of frames, lines, pixels in nanoseconds. */
  484. framedur_ns = refcrtc->framedur_ns;
  485. linedur_ns = refcrtc->linedur_ns;
  486. pixeldur_ns = refcrtc->pixeldur_ns;
  487. /* If mode timing undefined, just return as no-op:
  488. * Happens during initial modesetting of a crtc.
  489. */
  490. if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
  491. DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
  492. return -EAGAIN;
  493. }
  494. /* Get current scanout position with system timestamp.
  495. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  496. * if single query takes longer than max_error nanoseconds.
  497. *
  498. * This guarantees a tight bound on maximum error if
  499. * code gets preempted or delayed for some reason.
  500. */
  501. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  502. /* Disable preemption to make it very likely to
  503. * succeed in the first iteration even on PREEMPT_RT kernel.
  504. */
  505. preempt_disable();
  506. /* Get system timestamp before query. */
  507. stime = ktime_get();
  508. /* Get vertical and horizontal scanout pos. vpos, hpos. */
  509. vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
  510. /* Get system timestamp after query. */
  511. etime = ktime_get();
  512. if (!drm_timestamp_monotonic)
  513. mono_time_offset = ktime_get_monotonic_offset();
  514. preempt_enable();
  515. /* Return as no-op if scanout query unsupported or failed. */
  516. if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  517. DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
  518. crtc, vbl_status);
  519. return -EIO;
  520. }
  521. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  522. /* Accept result with < max_error nsecs timing uncertainty. */
  523. if (duration_ns <= (s64) *max_error)
  524. break;
  525. }
  526. /* Noisy system timing? */
  527. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  528. DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
  529. crtc, (int) duration_ns/1000, *max_error/1000, i);
  530. }
  531. /* Return upper bound of timestamp precision error. */
  532. *max_error = (int) duration_ns;
  533. /* Check if in vblank area:
  534. * vpos is >=0 in video scanout area, but negative
  535. * within vblank area, counting down the number of lines until
  536. * start of scanout.
  537. */
  538. invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
  539. /* Convert scanout position into elapsed time at raw_time query
  540. * since start of scanout at first display scanline. delta_ns
  541. * can be negative if start of scanout hasn't happened yet.
  542. */
  543. delta_ns = (s64) vpos * linedur_ns + (s64) hpos * pixeldur_ns;
  544. /* Is vpos outside nominal vblank area, but less than
  545. * 1/100 of a frame height away from start of vblank?
  546. * If so, assume this isn't a massively delayed vblank
  547. * interrupt, but a vblank interrupt that fired a few
  548. * microseconds before true start of vblank. Compensate
  549. * by adding a full frame duration to the final timestamp.
  550. * Happens, e.g., on ATI R500, R600.
  551. *
  552. * We only do this if DRM_CALLED_FROM_VBLIRQ.
  553. */
  554. if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
  555. ((vdisplay - vpos) < vtotal / 100)) {
  556. delta_ns = delta_ns - framedur_ns;
  557. /* Signal this correction as "applied". */
  558. vbl_status |= 0x8;
  559. }
  560. if (!drm_timestamp_monotonic)
  561. etime = ktime_sub(etime, mono_time_offset);
  562. /* save this only for debugging purposes */
  563. tv_etime = ktime_to_timeval(etime);
  564. /* Subtract time delta from raw timestamp to get final
  565. * vblank_time timestamp for end of vblank.
  566. */
  567. if (delta_ns < 0)
  568. etime = ktime_add_ns(etime, -delta_ns);
  569. else
  570. etime = ktime_sub_ns(etime, delta_ns);
  571. *vblank_time = ktime_to_timeval(etime);
  572. DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  573. crtc, (int)vbl_status, hpos, vpos,
  574. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  575. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  576. (int)duration_ns/1000, i);
  577. vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  578. if (invbl)
  579. vbl_status |= DRM_VBLANKTIME_INVBL;
  580. return vbl_status;
  581. }
  582. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  583. static struct timeval get_drm_timestamp(void)
  584. {
  585. ktime_t now;
  586. now = ktime_get();
  587. if (!drm_timestamp_monotonic)
  588. now = ktime_sub(now, ktime_get_monotonic_offset());
  589. return ktime_to_timeval(now);
  590. }
  591. /**
  592. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  593. * vblank interval.
  594. *
  595. * @dev: DRM device
  596. * @crtc: which crtc's vblank timestamp to retrieve
  597. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  598. * @flags: Flags to pass to driver:
  599. * 0 = Default.
  600. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
  601. *
  602. * Fetches the system timestamp corresponding to the time of the most recent
  603. * vblank interval on specified crtc. May call into kms-driver to
  604. * compute the timestamp with a high-precision GPU specific method.
  605. *
  606. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  607. * call, i.e., it isn't very precisely locked to the true vblank.
  608. *
  609. * Returns non-zero if timestamp is considered to be very precise.
  610. */
  611. u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
  612. struct timeval *tvblank, unsigned flags)
  613. {
  614. int ret;
  615. /* Define requested maximum error on timestamps (nanoseconds). */
  616. int max_error = (int) drm_timestamp_precision * 1000;
  617. /* Query driver if possible and precision timestamping enabled. */
  618. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  619. ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
  620. tvblank, flags);
  621. if (ret > 0)
  622. return (u32) ret;
  623. }
  624. /* GPU high precision timestamp query unsupported or failed.
  625. * Return current monotonic/gettimeofday timestamp as best estimate.
  626. */
  627. *tvblank = get_drm_timestamp();
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(drm_get_last_vbltimestamp);
  631. /**
  632. * drm_vblank_count - retrieve "cooked" vblank counter value
  633. * @dev: DRM device
  634. * @crtc: which counter to retrieve
  635. *
  636. * Fetches the "cooked" vblank count value that represents the number of
  637. * vblank events since the system was booted, including lost events due to
  638. * modesetting activity.
  639. */
  640. u32 drm_vblank_count(struct drm_device *dev, int crtc)
  641. {
  642. return atomic_read(&dev->vblank[crtc].count);
  643. }
  644. EXPORT_SYMBOL(drm_vblank_count);
  645. /**
  646. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
  647. * and the system timestamp corresponding to that vblank counter value.
  648. *
  649. * @dev: DRM device
  650. * @crtc: which counter to retrieve
  651. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  652. *
  653. * Fetches the "cooked" vblank count value that represents the number of
  654. * vblank events since the system was booted, including lost events due to
  655. * modesetting activity. Returns corresponding system timestamp of the time
  656. * of the vblank interval that corresponds to the current value vblank counter
  657. * value.
  658. */
  659. u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
  660. struct timeval *vblanktime)
  661. {
  662. u32 cur_vblank;
  663. /* Read timestamp from slot of _vblank_time ringbuffer
  664. * that corresponds to current vblank count. Retry if
  665. * count has incremented during readout. This works like
  666. * a seqlock.
  667. */
  668. do {
  669. cur_vblank = atomic_read(&dev->vblank[crtc].count);
  670. *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
  671. smp_rmb();
  672. } while (cur_vblank != atomic_read(&dev->vblank[crtc].count));
  673. return cur_vblank;
  674. }
  675. EXPORT_SYMBOL(drm_vblank_count_and_time);
  676. static void send_vblank_event(struct drm_device *dev,
  677. struct drm_pending_vblank_event *e,
  678. unsigned long seq, struct timeval *now)
  679. {
  680. WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
  681. e->event.sequence = seq;
  682. e->event.tv_sec = now->tv_sec;
  683. e->event.tv_usec = now->tv_usec;
  684. list_add_tail(&e->base.link,
  685. &e->base.file_priv->event_list);
  686. wake_up_interruptible(&e->base.file_priv->event_wait);
  687. trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
  688. e->event.sequence);
  689. }
  690. /**
  691. * drm_send_vblank_event - helper to send vblank event after pageflip
  692. * @dev: DRM device
  693. * @crtc: CRTC in question
  694. * @e: the event to send
  695. *
  696. * Updates sequence # and timestamp on event, and sends it to userspace.
  697. * Caller must hold event lock.
  698. */
  699. void drm_send_vblank_event(struct drm_device *dev, int crtc,
  700. struct drm_pending_vblank_event *e)
  701. {
  702. struct timeval now;
  703. unsigned int seq;
  704. if (crtc >= 0) {
  705. seq = drm_vblank_count_and_time(dev, crtc, &now);
  706. } else {
  707. seq = 0;
  708. now = get_drm_timestamp();
  709. }
  710. e->pipe = crtc;
  711. send_vblank_event(dev, e, seq, &now);
  712. }
  713. EXPORT_SYMBOL(drm_send_vblank_event);
  714. /**
  715. * drm_update_vblank_count - update the master vblank counter
  716. * @dev: DRM device
  717. * @crtc: counter to update
  718. *
  719. * Call back into the driver to update the appropriate vblank counter
  720. * (specified by @crtc). Deal with wraparound, if it occurred, and
  721. * update the last read value so we can deal with wraparound on the next
  722. * call if necessary.
  723. *
  724. * Only necessary when going from off->on, to account for frames we
  725. * didn't get an interrupt for.
  726. *
  727. * Note: caller must hold dev->vbl_lock since this reads & writes
  728. * device vblank fields.
  729. */
  730. static void drm_update_vblank_count(struct drm_device *dev, int crtc)
  731. {
  732. u32 cur_vblank, diff, tslot, rc;
  733. struct timeval t_vblank;
  734. /*
  735. * Interrupts were disabled prior to this call, so deal with counter
  736. * wrap if needed.
  737. * NOTE! It's possible we lost a full dev->max_vblank_count events
  738. * here if the register is small or we had vblank interrupts off for
  739. * a long time.
  740. *
  741. * We repeat the hardware vblank counter & timestamp query until
  742. * we get consistent results. This to prevent races between gpu
  743. * updating its hardware counter while we are retrieving the
  744. * corresponding vblank timestamp.
  745. */
  746. do {
  747. cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
  748. rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
  749. } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
  750. /* Deal with counter wrap */
  751. diff = cur_vblank - dev->vblank[crtc].last;
  752. if (cur_vblank < dev->vblank[crtc].last) {
  753. diff += dev->max_vblank_count;
  754. DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
  755. crtc, dev->vblank[crtc].last, cur_vblank, diff);
  756. }
  757. DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
  758. crtc, diff);
  759. /* Reinitialize corresponding vblank timestamp if high-precision query
  760. * available. Skip this step if query unsupported or failed. Will
  761. * reinitialize delayed at next vblank interrupt in that case.
  762. */
  763. if (rc) {
  764. tslot = atomic_read(&dev->vblank[crtc].count) + diff;
  765. vblanktimestamp(dev, crtc, tslot) = t_vblank;
  766. }
  767. smp_mb__before_atomic_inc();
  768. atomic_add(diff, &dev->vblank[crtc].count);
  769. smp_mb__after_atomic_inc();
  770. }
  771. /**
  772. * drm_vblank_get - get a reference count on vblank events
  773. * @dev: DRM device
  774. * @crtc: which CRTC to own
  775. *
  776. * Acquire a reference count on vblank events to avoid having them disabled
  777. * while in use.
  778. *
  779. * RETURNS
  780. * Zero on success, nonzero on failure.
  781. */
  782. int drm_vblank_get(struct drm_device *dev, int crtc)
  783. {
  784. unsigned long irqflags, irqflags2;
  785. int ret = 0;
  786. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  787. /* Going from 0->1 means we have to enable interrupts again */
  788. if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) {
  789. spin_lock_irqsave(&dev->vblank_time_lock, irqflags2);
  790. if (!dev->vblank[crtc].enabled) {
  791. /* Enable vblank irqs under vblank_time_lock protection.
  792. * All vblank count & timestamp updates are held off
  793. * until we are done reinitializing master counter and
  794. * timestamps. Filtercode in drm_handle_vblank() will
  795. * prevent double-accounting of same vblank interval.
  796. */
  797. ret = dev->driver->enable_vblank(dev, crtc);
  798. DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
  799. crtc, ret);
  800. if (ret)
  801. atomic_dec(&dev->vblank[crtc].refcount);
  802. else {
  803. dev->vblank[crtc].enabled = true;
  804. drm_update_vblank_count(dev, crtc);
  805. }
  806. }
  807. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
  808. } else {
  809. if (!dev->vblank[crtc].enabled) {
  810. atomic_dec(&dev->vblank[crtc].refcount);
  811. ret = -EINVAL;
  812. }
  813. }
  814. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  815. return ret;
  816. }
  817. EXPORT_SYMBOL(drm_vblank_get);
  818. /**
  819. * drm_vblank_put - give up ownership of vblank events
  820. * @dev: DRM device
  821. * @crtc: which counter to give up
  822. *
  823. * Release ownership of a given vblank counter, turning off interrupts
  824. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  825. */
  826. void drm_vblank_put(struct drm_device *dev, int crtc)
  827. {
  828. BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0);
  829. /* Last user schedules interrupt disable */
  830. if (atomic_dec_and_test(&dev->vblank[crtc].refcount) &&
  831. (drm_vblank_offdelay > 0))
  832. mod_timer(&dev->vblank_disable_timer,
  833. jiffies + ((drm_vblank_offdelay * DRM_HZ)/1000));
  834. }
  835. EXPORT_SYMBOL(drm_vblank_put);
  836. /**
  837. * drm_vblank_off - disable vblank events on a CRTC
  838. * @dev: DRM device
  839. * @crtc: CRTC in question
  840. *
  841. * Caller must hold event lock.
  842. */
  843. void drm_vblank_off(struct drm_device *dev, int crtc)
  844. {
  845. struct drm_pending_vblank_event *e, *t;
  846. struct timeval now;
  847. unsigned long irqflags;
  848. unsigned int seq;
  849. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  850. vblank_disable_and_save(dev, crtc);
  851. DRM_WAKEUP(&dev->vblank[crtc].queue);
  852. /* Send any queued vblank events, lest the natives grow disquiet */
  853. seq = drm_vblank_count_and_time(dev, crtc, &now);
  854. spin_lock(&dev->event_lock);
  855. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  856. if (e->pipe != crtc)
  857. continue;
  858. DRM_DEBUG("Sending premature vblank event on disable: \
  859. wanted %d, current %d\n",
  860. e->event.sequence, seq);
  861. list_del(&e->base.link);
  862. drm_vblank_put(dev, e->pipe);
  863. send_vblank_event(dev, e, seq, &now);
  864. }
  865. spin_unlock(&dev->event_lock);
  866. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  867. }
  868. EXPORT_SYMBOL(drm_vblank_off);
  869. /**
  870. * drm_vblank_pre_modeset - account for vblanks across mode sets
  871. * @dev: DRM device
  872. * @crtc: CRTC in question
  873. *
  874. * Account for vblank events across mode setting events, which will likely
  875. * reset the hardware frame counter.
  876. */
  877. void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
  878. {
  879. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  880. if (!dev->num_crtcs)
  881. return;
  882. /*
  883. * To avoid all the problems that might happen if interrupts
  884. * were enabled/disabled around or between these calls, we just
  885. * have the kernel take a reference on the CRTC (just once though
  886. * to avoid corrupting the count if multiple, mismatch calls occur),
  887. * so that interrupts remain enabled in the interim.
  888. */
  889. if (!dev->vblank[crtc].inmodeset) {
  890. dev->vblank[crtc].inmodeset = 0x1;
  891. if (drm_vblank_get(dev, crtc) == 0)
  892. dev->vblank[crtc].inmodeset |= 0x2;
  893. }
  894. }
  895. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  896. void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
  897. {
  898. unsigned long irqflags;
  899. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  900. if (!dev->num_crtcs)
  901. return;
  902. if (dev->vblank[crtc].inmodeset) {
  903. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  904. dev->vblank_disable_allowed = true;
  905. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  906. if (dev->vblank[crtc].inmodeset & 0x2)
  907. drm_vblank_put(dev, crtc);
  908. dev->vblank[crtc].inmodeset = 0;
  909. }
  910. }
  911. EXPORT_SYMBOL(drm_vblank_post_modeset);
  912. /**
  913. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  914. * @DRM_IOCTL_ARGS: standard ioctl arguments
  915. *
  916. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  917. * ioctls around modesetting so that any lost vblank events are accounted for.
  918. *
  919. * Generally the counter will reset across mode sets. If interrupts are
  920. * enabled around this call, we don't have to do anything since the counter
  921. * will have already been incremented.
  922. */
  923. int drm_modeset_ctl(struct drm_device *dev, void *data,
  924. struct drm_file *file_priv)
  925. {
  926. struct drm_modeset_ctl *modeset = data;
  927. unsigned int crtc;
  928. /* If drm_vblank_init() hasn't been called yet, just no-op */
  929. if (!dev->num_crtcs)
  930. return 0;
  931. /* KMS drivers handle this internally */
  932. if (drm_core_check_feature(dev, DRIVER_MODESET))
  933. return 0;
  934. crtc = modeset->crtc;
  935. if (crtc >= dev->num_crtcs)
  936. return -EINVAL;
  937. switch (modeset->cmd) {
  938. case _DRM_PRE_MODESET:
  939. drm_vblank_pre_modeset(dev, crtc);
  940. break;
  941. case _DRM_POST_MODESET:
  942. drm_vblank_post_modeset(dev, crtc);
  943. break;
  944. default:
  945. return -EINVAL;
  946. }
  947. return 0;
  948. }
  949. static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
  950. union drm_wait_vblank *vblwait,
  951. struct drm_file *file_priv)
  952. {
  953. struct drm_pending_vblank_event *e;
  954. struct timeval now;
  955. unsigned long flags;
  956. unsigned int seq;
  957. int ret;
  958. e = kzalloc(sizeof *e, GFP_KERNEL);
  959. if (e == NULL) {
  960. ret = -ENOMEM;
  961. goto err_put;
  962. }
  963. e->pipe = pipe;
  964. e->base.pid = current->pid;
  965. e->event.base.type = DRM_EVENT_VBLANK;
  966. e->event.base.length = sizeof e->event;
  967. e->event.user_data = vblwait->request.signal;
  968. e->base.event = &e->event.base;
  969. e->base.file_priv = file_priv;
  970. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  971. spin_lock_irqsave(&dev->event_lock, flags);
  972. if (file_priv->event_space < sizeof e->event) {
  973. ret = -EBUSY;
  974. goto err_unlock;
  975. }
  976. file_priv->event_space -= sizeof e->event;
  977. seq = drm_vblank_count_and_time(dev, pipe, &now);
  978. if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
  979. (seq - vblwait->request.sequence) <= (1 << 23)) {
  980. vblwait->request.sequence = seq + 1;
  981. vblwait->reply.sequence = vblwait->request.sequence;
  982. }
  983. DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
  984. vblwait->request.sequence, seq, pipe);
  985. trace_drm_vblank_event_queued(current->pid, pipe,
  986. vblwait->request.sequence);
  987. e->event.sequence = vblwait->request.sequence;
  988. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  989. drm_vblank_put(dev, pipe);
  990. send_vblank_event(dev, e, seq, &now);
  991. vblwait->reply.sequence = seq;
  992. } else {
  993. /* drm_handle_vblank_events will call drm_vblank_put */
  994. list_add_tail(&e->base.link, &dev->vblank_event_list);
  995. vblwait->reply.sequence = vblwait->request.sequence;
  996. }
  997. spin_unlock_irqrestore(&dev->event_lock, flags);
  998. return 0;
  999. err_unlock:
  1000. spin_unlock_irqrestore(&dev->event_lock, flags);
  1001. kfree(e);
  1002. err_put:
  1003. drm_vblank_put(dev, pipe);
  1004. return ret;
  1005. }
  1006. /**
  1007. * Wait for VBLANK.
  1008. *
  1009. * \param inode device inode.
  1010. * \param file_priv DRM file private.
  1011. * \param cmd command.
  1012. * \param data user argument, pointing to a drm_wait_vblank structure.
  1013. * \return zero on success or a negative number on failure.
  1014. *
  1015. * This function enables the vblank interrupt on the pipe requested, then
  1016. * sleeps waiting for the requested sequence number to occur, and drops
  1017. * the vblank interrupt refcount afterwards. (vblank irq disable follows that
  1018. * after a timeout with no further vblank waits scheduled).
  1019. */
  1020. int drm_wait_vblank(struct drm_device *dev, void *data,
  1021. struct drm_file *file_priv)
  1022. {
  1023. union drm_wait_vblank *vblwait = data;
  1024. int ret;
  1025. unsigned int flags, seq, crtc, high_crtc;
  1026. if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  1027. if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled))
  1028. return -EINVAL;
  1029. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1030. return -EINVAL;
  1031. if (vblwait->request.type &
  1032. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1033. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1034. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1035. vblwait->request.type,
  1036. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1037. _DRM_VBLANK_HIGH_CRTC_MASK));
  1038. return -EINVAL;
  1039. }
  1040. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1041. high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1042. if (high_crtc)
  1043. crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1044. else
  1045. crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1046. if (crtc >= dev->num_crtcs)
  1047. return -EINVAL;
  1048. ret = drm_vblank_get(dev, crtc);
  1049. if (ret) {
  1050. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  1051. return ret;
  1052. }
  1053. seq = drm_vblank_count(dev, crtc);
  1054. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1055. case _DRM_VBLANK_RELATIVE:
  1056. vblwait->request.sequence += seq;
  1057. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1058. case _DRM_VBLANK_ABSOLUTE:
  1059. break;
  1060. default:
  1061. ret = -EINVAL;
  1062. goto done;
  1063. }
  1064. if (flags & _DRM_VBLANK_EVENT) {
  1065. /* must hold on to the vblank ref until the event fires
  1066. * drm_vblank_put will be called asynchronously
  1067. */
  1068. return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
  1069. }
  1070. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1071. (seq - vblwait->request.sequence) <= (1<<23)) {
  1072. vblwait->request.sequence = seq + 1;
  1073. }
  1074. DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
  1075. vblwait->request.sequence, crtc);
  1076. dev->vblank[crtc].last_wait = vblwait->request.sequence;
  1077. DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * DRM_HZ,
  1078. (((drm_vblank_count(dev, crtc) -
  1079. vblwait->request.sequence) <= (1 << 23)) ||
  1080. !dev->irq_enabled));
  1081. if (ret != -EINTR) {
  1082. struct timeval now;
  1083. vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
  1084. vblwait->reply.tval_sec = now.tv_sec;
  1085. vblwait->reply.tval_usec = now.tv_usec;
  1086. DRM_DEBUG("returning %d to client\n",
  1087. vblwait->reply.sequence);
  1088. } else {
  1089. DRM_DEBUG("vblank wait interrupted by signal\n");
  1090. }
  1091. done:
  1092. drm_vblank_put(dev, crtc);
  1093. return ret;
  1094. }
  1095. static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
  1096. {
  1097. struct drm_pending_vblank_event *e, *t;
  1098. struct timeval now;
  1099. unsigned long flags;
  1100. unsigned int seq;
  1101. seq = drm_vblank_count_and_time(dev, crtc, &now);
  1102. spin_lock_irqsave(&dev->event_lock, flags);
  1103. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1104. if (e->pipe != crtc)
  1105. continue;
  1106. if ((seq - e->event.sequence) > (1<<23))
  1107. continue;
  1108. DRM_DEBUG("vblank event on %d, current %d\n",
  1109. e->event.sequence, seq);
  1110. list_del(&e->base.link);
  1111. drm_vblank_put(dev, e->pipe);
  1112. send_vblank_event(dev, e, seq, &now);
  1113. }
  1114. spin_unlock_irqrestore(&dev->event_lock, flags);
  1115. trace_drm_vblank_event(crtc, seq);
  1116. }
  1117. /**
  1118. * drm_handle_vblank - handle a vblank event
  1119. * @dev: DRM device
  1120. * @crtc: where this event occurred
  1121. *
  1122. * Drivers should call this routine in their vblank interrupt handlers to
  1123. * update the vblank counter and send any signals that may be pending.
  1124. */
  1125. bool drm_handle_vblank(struct drm_device *dev, int crtc)
  1126. {
  1127. u32 vblcount;
  1128. s64 diff_ns;
  1129. struct timeval tvblank;
  1130. unsigned long irqflags;
  1131. if (!dev->num_crtcs)
  1132. return false;
  1133. /* Need timestamp lock to prevent concurrent execution with
  1134. * vblank enable/disable, as this would cause inconsistent
  1135. * or corrupted timestamps and vblank counts.
  1136. */
  1137. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  1138. /* Vblank irq handling disabled. Nothing to do. */
  1139. if (!dev->vblank[crtc].enabled) {
  1140. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  1141. return false;
  1142. }
  1143. /* Fetch corresponding timestamp for this vblank interval from
  1144. * driver and store it in proper slot of timestamp ringbuffer.
  1145. */
  1146. /* Get current timestamp and count. */
  1147. vblcount = atomic_read(&dev->vblank[crtc].count);
  1148. drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
  1149. /* Compute time difference to timestamp of last vblank */
  1150. diff_ns = timeval_to_ns(&tvblank) -
  1151. timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
  1152. /* Update vblank timestamp and count if at least
  1153. * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
  1154. * difference between last stored timestamp and current
  1155. * timestamp. A smaller difference means basically
  1156. * identical timestamps. Happens if this vblank has
  1157. * been already processed and this is a redundant call,
  1158. * e.g., due to spurious vblank interrupts. We need to
  1159. * ignore those for accounting.
  1160. */
  1161. if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
  1162. /* Store new timestamp in ringbuffer. */
  1163. vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
  1164. /* Increment cooked vblank count. This also atomically commits
  1165. * the timestamp computed above.
  1166. */
  1167. smp_mb__before_atomic_inc();
  1168. atomic_inc(&dev->vblank[crtc].count);
  1169. smp_mb__after_atomic_inc();
  1170. } else {
  1171. DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
  1172. crtc, (int) diff_ns);
  1173. }
  1174. DRM_WAKEUP(&dev->vblank[crtc].queue);
  1175. drm_handle_vblank_events(dev, crtc);
  1176. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  1177. return true;
  1178. }
  1179. EXPORT_SYMBOL(drm_handle_vblank);