drm_irq.c 45 KB

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