drm_irq.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 = true;
  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 = false;
  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 = false;
  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. bool irq_enabled;
  295. int i;
  296. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  297. return -EINVAL;
  298. mutex_lock(&dev->struct_mutex);
  299. irq_enabled = dev->irq_enabled;
  300. dev->irq_enabled = false;
  301. mutex_unlock(&dev->struct_mutex);
  302. /*
  303. * Wake up any waiters so they don't hang.
  304. */
  305. if (dev->num_crtcs) {
  306. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  307. for (i = 0; i < dev->num_crtcs; i++) {
  308. DRM_WAKEUP(&dev->vblank[i].queue);
  309. dev->vblank[i].enabled = false;
  310. dev->vblank[i].last =
  311. dev->driver->get_vblank_counter(dev, i);
  312. }
  313. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  314. }
  315. if (!irq_enabled)
  316. return -EINVAL;
  317. DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
  318. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  319. vga_client_register(dev->pdev, NULL, NULL, NULL);
  320. if (dev->driver->irq_uninstall)
  321. dev->driver->irq_uninstall(dev);
  322. free_irq(drm_dev_to_irq(dev), dev);
  323. return 0;
  324. }
  325. EXPORT_SYMBOL(drm_irq_uninstall);
  326. /**
  327. * IRQ control ioctl.
  328. *
  329. * \param inode device inode.
  330. * \param file_priv DRM file private.
  331. * \param cmd command.
  332. * \param arg user argument, pointing to a drm_control structure.
  333. * \return zero on success or a negative number on failure.
  334. *
  335. * Calls irq_install() or irq_uninstall() according to \p arg.
  336. */
  337. int drm_control(struct drm_device *dev, void *data,
  338. struct drm_file *file_priv)
  339. {
  340. struct drm_control *ctl = data;
  341. /* if we haven't irq we fallback for compatibility reasons -
  342. * this used to be a separate function in drm_dma.h
  343. */
  344. switch (ctl->func) {
  345. case DRM_INST_HANDLER:
  346. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  347. return 0;
  348. if (drm_core_check_feature(dev, DRIVER_MODESET))
  349. return 0;
  350. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  351. ctl->irq != drm_dev_to_irq(dev))
  352. return -EINVAL;
  353. return drm_irq_install(dev);
  354. case DRM_UNINST_HANDLER:
  355. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  356. return 0;
  357. if (drm_core_check_feature(dev, DRIVER_MODESET))
  358. return 0;
  359. return drm_irq_uninstall(dev);
  360. default:
  361. return -EINVAL;
  362. }
  363. }
  364. /**
  365. * drm_calc_timestamping_constants - Calculate and
  366. * store various constants which are later needed by
  367. * vblank and swap-completion timestamping, e.g, by
  368. * drm_calc_vbltimestamp_from_scanoutpos().
  369. * They are derived from crtc's true scanout timing,
  370. * so they take things like panel scaling or other
  371. * adjustments into account.
  372. *
  373. * @crtc drm_crtc whose timestamp constants should be updated.
  374. *
  375. */
  376. void drm_calc_timestamping_constants(struct drm_crtc *crtc)
  377. {
  378. s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
  379. u64 dotclock;
  380. /* Dot clock in Hz: */
  381. dotclock = (u64) crtc->hwmode.clock * 1000;
  382. /* Fields of interlaced scanout modes are only halve a frame duration.
  383. * Double the dotclock to get halve the frame-/line-/pixelduration.
  384. */
  385. if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
  386. dotclock *= 2;
  387. /* Valid dotclock? */
  388. if (dotclock > 0) {
  389. int frame_size;
  390. /* Convert scanline length in pixels and video dot clock to
  391. * line duration, frame duration and pixel duration in
  392. * nanoseconds:
  393. */
  394. pixeldur_ns = (s64) div64_u64(1000000000, dotclock);
  395. linedur_ns = (s64) div64_u64(((u64) crtc->hwmode.crtc_htotal *
  396. 1000000000), dotclock);
  397. frame_size = crtc->hwmode.crtc_htotal *
  398. crtc->hwmode.crtc_vtotal;
  399. framedur_ns = (s64) div64_u64((u64) frame_size * 1000000000,
  400. dotclock);
  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, crtc->hwmode.crtc_htotal,
  409. crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
  410. DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
  411. crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
  412. (int) linedur_ns, (int) pixeldur_ns);
  413. }
  414. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  415. /**
  416. * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
  417. * drivers. Implements calculation of exact vblank timestamps from
  418. * given drm_display_mode timings and current video scanout position
  419. * of a crtc. This can be called from within get_vblank_timestamp()
  420. * implementation of a kms driver to implement the actual timestamping.
  421. *
  422. * Should return timestamps conforming to the OML_sync_control OpenML
  423. * extension specification. The timestamp corresponds to the end of
  424. * the vblank interval, aka start of scanout of topmost-leftmost display
  425. * pixel in the following video frame.
  426. *
  427. * Requires support for optional dev->driver->get_scanout_position()
  428. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  429. * that corresponds to the true scanout timing.
  430. *
  431. * The current implementation only handles standard video modes. It
  432. * returns as no operation if a doublescan or interlaced video mode is
  433. * active. Higher level code is expected to handle this.
  434. *
  435. * @dev: DRM device.
  436. * @crtc: Which crtc's vblank timestamp to retrieve.
  437. * @max_error: Desired maximum allowable error in timestamps (nanosecs).
  438. * On return contains true maximum error of timestamp.
  439. * @vblank_time: Pointer to struct timeval which should receive the timestamp.
  440. * @flags: Flags to pass to driver:
  441. * 0 = Default.
  442. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
  443. * @refcrtc: drm_crtc* of crtc which defines scanout timing.
  444. *
  445. * Returns negative value on error, failure or if not supported in current
  446. * video mode:
  447. *
  448. * -EINVAL - Invalid crtc.
  449. * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
  450. * -ENOTSUPP - Function not supported in current display mode.
  451. * -EIO - Failed, e.g., due to failed scanout position query.
  452. *
  453. * Returns or'ed positive status flags on success:
  454. *
  455. * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  456. * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  457. *
  458. */
  459. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
  460. int *max_error,
  461. struct timeval *vblank_time,
  462. unsigned flags,
  463. struct drm_crtc *refcrtc)
  464. {
  465. ktime_t stime, etime, mono_time_offset;
  466. struct timeval tv_etime;
  467. struct drm_display_mode *mode;
  468. int vbl_status, vtotal, vdisplay;
  469. int vpos, hpos, i;
  470. s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
  471. bool invbl;
  472. if (crtc < 0 || crtc >= dev->num_crtcs) {
  473. DRM_ERROR("Invalid crtc %d\n", crtc);
  474. return -EINVAL;
  475. }
  476. /* Scanout position query not supported? Should not happen. */
  477. if (!dev->driver->get_scanout_position) {
  478. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  479. return -EIO;
  480. }
  481. mode = &refcrtc->hwmode;
  482. vtotal = mode->crtc_vtotal;
  483. vdisplay = mode->crtc_vdisplay;
  484. /* Durations of frames, lines, pixels in nanoseconds. */
  485. framedur_ns = refcrtc->framedur_ns;
  486. linedur_ns = refcrtc->linedur_ns;
  487. pixeldur_ns = refcrtc->pixeldur_ns;
  488. /* If mode timing undefined, just return as no-op:
  489. * Happens during initial modesetting of a crtc.
  490. */
  491. if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
  492. DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
  493. return -EAGAIN;
  494. }
  495. /* Get current scanout position with system timestamp.
  496. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  497. * if single query takes longer than max_error nanoseconds.
  498. *
  499. * This guarantees a tight bound on maximum error if
  500. * code gets preempted or delayed for some reason.
  501. */
  502. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  503. /* Get system timestamp before query. */
  504. stime = ktime_get();
  505. /* Get vertical and horizontal scanout pos. vpos, hpos. */
  506. vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
  507. /* Get system timestamp after query. */
  508. etime = ktime_get();
  509. if (!drm_timestamp_monotonic)
  510. mono_time_offset = ktime_get_monotonic_offset();
  511. /* Return as no-op if scanout query unsupported or failed. */
  512. if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  513. DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
  514. crtc, vbl_status);
  515. return -EIO;
  516. }
  517. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  518. /* Accept result with < max_error nsecs timing uncertainty. */
  519. if (duration_ns <= (s64) *max_error)
  520. break;
  521. }
  522. /* Noisy system timing? */
  523. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  524. DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
  525. crtc, (int) duration_ns/1000, *max_error/1000, i);
  526. }
  527. /* Return upper bound of timestamp precision error. */
  528. *max_error = (int) duration_ns;
  529. /* Check if in vblank area:
  530. * vpos is >=0 in video scanout area, but negative
  531. * within vblank area, counting down the number of lines until
  532. * start of scanout.
  533. */
  534. invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
  535. /* Convert scanout position into elapsed time at raw_time query
  536. * since start of scanout at first display scanline. delta_ns
  537. * can be negative if start of scanout hasn't happened yet.
  538. */
  539. delta_ns = (s64) vpos * linedur_ns + (s64) hpos * pixeldur_ns;
  540. /* Is vpos outside nominal vblank area, but less than
  541. * 1/100 of a frame height away from start of vblank?
  542. * If so, assume this isn't a massively delayed vblank
  543. * interrupt, but a vblank interrupt that fired a few
  544. * microseconds before true start of vblank. Compensate
  545. * by adding a full frame duration to the final timestamp.
  546. * Happens, e.g., on ATI R500, R600.
  547. *
  548. * We only do this if DRM_CALLED_FROM_VBLIRQ.
  549. */
  550. if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
  551. ((vdisplay - vpos) < vtotal / 100)) {
  552. delta_ns = delta_ns - framedur_ns;
  553. /* Signal this correction as "applied". */
  554. vbl_status |= 0x8;
  555. }
  556. if (!drm_timestamp_monotonic)
  557. etime = ktime_sub(etime, mono_time_offset);
  558. /* save this only for debugging purposes */
  559. tv_etime = ktime_to_timeval(etime);
  560. /* Subtract time delta from raw timestamp to get final
  561. * vblank_time timestamp for end of vblank.
  562. */
  563. if (delta_ns < 0)
  564. etime = ktime_add_ns(etime, -delta_ns);
  565. else
  566. etime = ktime_sub_ns(etime, delta_ns);
  567. *vblank_time = ktime_to_timeval(etime);
  568. DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  569. crtc, (int)vbl_status, hpos, vpos,
  570. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  571. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  572. (int)duration_ns/1000, i);
  573. vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  574. if (invbl)
  575. vbl_status |= DRM_VBLANKTIME_INVBL;
  576. return vbl_status;
  577. }
  578. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  579. static struct timeval get_drm_timestamp(void)
  580. {
  581. ktime_t now;
  582. now = ktime_get();
  583. if (!drm_timestamp_monotonic)
  584. now = ktime_sub(now, ktime_get_monotonic_offset());
  585. return ktime_to_timeval(now);
  586. }
  587. /**
  588. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  589. * vblank interval.
  590. *
  591. * @dev: DRM device
  592. * @crtc: which crtc's vblank timestamp to retrieve
  593. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  594. * @flags: Flags to pass to driver:
  595. * 0 = Default.
  596. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
  597. *
  598. * Fetches the system timestamp corresponding to the time of the most recent
  599. * vblank interval on specified crtc. May call into kms-driver to
  600. * compute the timestamp with a high-precision GPU specific method.
  601. *
  602. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  603. * call, i.e., it isn't very precisely locked to the true vblank.
  604. *
  605. * Returns non-zero if timestamp is considered to be very precise.
  606. */
  607. u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
  608. struct timeval *tvblank, unsigned flags)
  609. {
  610. int ret;
  611. /* Define requested maximum error on timestamps (nanoseconds). */
  612. int max_error = (int) drm_timestamp_precision * 1000;
  613. /* Query driver if possible and precision timestamping enabled. */
  614. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  615. ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
  616. tvblank, flags);
  617. if (ret > 0)
  618. return (u32) ret;
  619. }
  620. /* GPU high precision timestamp query unsupported or failed.
  621. * Return current monotonic/gettimeofday timestamp as best estimate.
  622. */
  623. *tvblank = get_drm_timestamp();
  624. return 0;
  625. }
  626. EXPORT_SYMBOL(drm_get_last_vbltimestamp);
  627. /**
  628. * drm_vblank_count - retrieve "cooked" vblank counter value
  629. * @dev: DRM device
  630. * @crtc: which counter to retrieve
  631. *
  632. * Fetches the "cooked" vblank count value that represents the number of
  633. * vblank events since the system was booted, including lost events due to
  634. * modesetting activity.
  635. */
  636. u32 drm_vblank_count(struct drm_device *dev, int crtc)
  637. {
  638. return atomic_read(&dev->vblank[crtc].count);
  639. }
  640. EXPORT_SYMBOL(drm_vblank_count);
  641. /**
  642. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
  643. * and the system timestamp corresponding to that vblank counter value.
  644. *
  645. * @dev: DRM device
  646. * @crtc: which counter to retrieve
  647. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  648. *
  649. * Fetches the "cooked" vblank count value that represents the number of
  650. * vblank events since the system was booted, including lost events due to
  651. * modesetting activity. Returns corresponding system timestamp of the time
  652. * of the vblank interval that corresponds to the current value vblank counter
  653. * value.
  654. */
  655. u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
  656. struct timeval *vblanktime)
  657. {
  658. u32 cur_vblank;
  659. /* Read timestamp from slot of _vblank_time ringbuffer
  660. * that corresponds to current vblank count. Retry if
  661. * count has incremented during readout. This works like
  662. * a seqlock.
  663. */
  664. do {
  665. cur_vblank = atomic_read(&dev->vblank[crtc].count);
  666. *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
  667. smp_rmb();
  668. } while (cur_vblank != atomic_read(&dev->vblank[crtc].count));
  669. return cur_vblank;
  670. }
  671. EXPORT_SYMBOL(drm_vblank_count_and_time);
  672. static void send_vblank_event(struct drm_device *dev,
  673. struct drm_pending_vblank_event *e,
  674. unsigned long seq, struct timeval *now)
  675. {
  676. WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
  677. e->event.sequence = seq;
  678. e->event.tv_sec = now->tv_sec;
  679. e->event.tv_usec = now->tv_usec;
  680. list_add_tail(&e->base.link,
  681. &e->base.file_priv->event_list);
  682. wake_up_interruptible(&e->base.file_priv->event_wait);
  683. trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
  684. e->event.sequence);
  685. }
  686. /**
  687. * drm_send_vblank_event - helper to send vblank event after pageflip
  688. * @dev: DRM device
  689. * @crtc: CRTC in question
  690. * @e: the event to send
  691. *
  692. * Updates sequence # and timestamp on event, and sends it to userspace.
  693. * Caller must hold event lock.
  694. */
  695. void drm_send_vblank_event(struct drm_device *dev, int crtc,
  696. struct drm_pending_vblank_event *e)
  697. {
  698. struct timeval now;
  699. unsigned int seq;
  700. if (crtc >= 0) {
  701. seq = drm_vblank_count_and_time(dev, crtc, &now);
  702. } else {
  703. seq = 0;
  704. now = get_drm_timestamp();
  705. }
  706. e->pipe = crtc;
  707. send_vblank_event(dev, e, seq, &now);
  708. }
  709. EXPORT_SYMBOL(drm_send_vblank_event);
  710. /**
  711. * drm_update_vblank_count - update the master vblank counter
  712. * @dev: DRM device
  713. * @crtc: counter to update
  714. *
  715. * Call back into the driver to update the appropriate vblank counter
  716. * (specified by @crtc). Deal with wraparound, if it occurred, and
  717. * update the last read value so we can deal with wraparound on the next
  718. * call if necessary.
  719. *
  720. * Only necessary when going from off->on, to account for frames we
  721. * didn't get an interrupt for.
  722. *
  723. * Note: caller must hold dev->vbl_lock since this reads & writes
  724. * device vblank fields.
  725. */
  726. static void drm_update_vblank_count(struct drm_device *dev, int crtc)
  727. {
  728. u32 cur_vblank, diff, tslot, rc;
  729. struct timeval t_vblank;
  730. /*
  731. * Interrupts were disabled prior to this call, so deal with counter
  732. * wrap if needed.
  733. * NOTE! It's possible we lost a full dev->max_vblank_count events
  734. * here if the register is small or we had vblank interrupts off for
  735. * a long time.
  736. *
  737. * We repeat the hardware vblank counter & timestamp query until
  738. * we get consistent results. This to prevent races between gpu
  739. * updating its hardware counter while we are retrieving the
  740. * corresponding vblank timestamp.
  741. */
  742. do {
  743. cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
  744. rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
  745. } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
  746. /* Deal with counter wrap */
  747. diff = cur_vblank - dev->vblank[crtc].last;
  748. if (cur_vblank < dev->vblank[crtc].last) {
  749. diff += dev->max_vblank_count;
  750. DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
  751. crtc, dev->vblank[crtc].last, cur_vblank, diff);
  752. }
  753. DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
  754. crtc, diff);
  755. /* Reinitialize corresponding vblank timestamp if high-precision query
  756. * available. Skip this step if query unsupported or failed. Will
  757. * reinitialize delayed at next vblank interrupt in that case.
  758. */
  759. if (rc) {
  760. tslot = atomic_read(&dev->vblank[crtc].count) + diff;
  761. vblanktimestamp(dev, crtc, tslot) = t_vblank;
  762. }
  763. smp_mb__before_atomic_inc();
  764. atomic_add(diff, &dev->vblank[crtc].count);
  765. smp_mb__after_atomic_inc();
  766. }
  767. /**
  768. * drm_vblank_get - get a reference count on vblank events
  769. * @dev: DRM device
  770. * @crtc: which CRTC to own
  771. *
  772. * Acquire a reference count on vblank events to avoid having them disabled
  773. * while in use.
  774. *
  775. * RETURNS
  776. * Zero on success, nonzero on failure.
  777. */
  778. int drm_vblank_get(struct drm_device *dev, int crtc)
  779. {
  780. unsigned long irqflags, irqflags2;
  781. int ret = 0;
  782. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  783. /* Going from 0->1 means we have to enable interrupts again */
  784. if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) {
  785. spin_lock_irqsave(&dev->vblank_time_lock, irqflags2);
  786. if (!dev->vblank[crtc].enabled) {
  787. /* Enable vblank irqs under vblank_time_lock protection.
  788. * All vblank count & timestamp updates are held off
  789. * until we are done reinitializing master counter and
  790. * timestamps. Filtercode in drm_handle_vblank() will
  791. * prevent double-accounting of same vblank interval.
  792. */
  793. ret = dev->driver->enable_vblank(dev, crtc);
  794. DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
  795. crtc, ret);
  796. if (ret)
  797. atomic_dec(&dev->vblank[crtc].refcount);
  798. else {
  799. dev->vblank[crtc].enabled = true;
  800. drm_update_vblank_count(dev, crtc);
  801. }
  802. }
  803. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
  804. } else {
  805. if (!dev->vblank[crtc].enabled) {
  806. atomic_dec(&dev->vblank[crtc].refcount);
  807. ret = -EINVAL;
  808. }
  809. }
  810. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  811. return ret;
  812. }
  813. EXPORT_SYMBOL(drm_vblank_get);
  814. /**
  815. * drm_vblank_put - give up ownership of vblank events
  816. * @dev: DRM device
  817. * @crtc: which counter to give up
  818. *
  819. * Release ownership of a given vblank counter, turning off interrupts
  820. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  821. */
  822. void drm_vblank_put(struct drm_device *dev, int crtc)
  823. {
  824. BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0);
  825. /* Last user schedules interrupt disable */
  826. if (atomic_dec_and_test(&dev->vblank[crtc].refcount) &&
  827. (drm_vblank_offdelay > 0))
  828. mod_timer(&dev->vblank_disable_timer,
  829. jiffies + ((drm_vblank_offdelay * DRM_HZ)/1000));
  830. }
  831. EXPORT_SYMBOL(drm_vblank_put);
  832. /**
  833. * drm_vblank_off - disable vblank events on a CRTC
  834. * @dev: DRM device
  835. * @crtc: CRTC in question
  836. *
  837. * Caller must hold event lock.
  838. */
  839. void drm_vblank_off(struct drm_device *dev, int crtc)
  840. {
  841. struct drm_pending_vblank_event *e, *t;
  842. struct timeval now;
  843. unsigned long irqflags;
  844. unsigned int seq;
  845. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  846. vblank_disable_and_save(dev, crtc);
  847. DRM_WAKEUP(&dev->vblank[crtc].queue);
  848. /* Send any queued vblank events, lest the natives grow disquiet */
  849. seq = drm_vblank_count_and_time(dev, crtc, &now);
  850. spin_lock(&dev->event_lock);
  851. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  852. if (e->pipe != crtc)
  853. continue;
  854. DRM_DEBUG("Sending premature vblank event on disable: \
  855. wanted %d, current %d\n",
  856. e->event.sequence, seq);
  857. list_del(&e->base.link);
  858. drm_vblank_put(dev, e->pipe);
  859. send_vblank_event(dev, e, seq, &now);
  860. }
  861. spin_unlock(&dev->event_lock);
  862. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  863. }
  864. EXPORT_SYMBOL(drm_vblank_off);
  865. /**
  866. * drm_vblank_pre_modeset - account for vblanks across mode sets
  867. * @dev: DRM device
  868. * @crtc: CRTC in question
  869. *
  870. * Account for vblank events across mode setting events, which will likely
  871. * reset the hardware frame counter.
  872. */
  873. void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
  874. {
  875. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  876. if (!dev->num_crtcs)
  877. return;
  878. /*
  879. * To avoid all the problems that might happen if interrupts
  880. * were enabled/disabled around or between these calls, we just
  881. * have the kernel take a reference on the CRTC (just once though
  882. * to avoid corrupting the count if multiple, mismatch calls occur),
  883. * so that interrupts remain enabled in the interim.
  884. */
  885. if (!dev->vblank[crtc].inmodeset) {
  886. dev->vblank[crtc].inmodeset = 0x1;
  887. if (drm_vblank_get(dev, crtc) == 0)
  888. dev->vblank[crtc].inmodeset |= 0x2;
  889. }
  890. }
  891. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  892. void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
  893. {
  894. unsigned long irqflags;
  895. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  896. if (!dev->num_crtcs)
  897. return;
  898. if (dev->vblank[crtc].inmodeset) {
  899. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  900. dev->vblank_disable_allowed = true;
  901. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  902. if (dev->vblank[crtc].inmodeset & 0x2)
  903. drm_vblank_put(dev, crtc);
  904. dev->vblank[crtc].inmodeset = 0;
  905. }
  906. }
  907. EXPORT_SYMBOL(drm_vblank_post_modeset);
  908. /**
  909. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  910. * @DRM_IOCTL_ARGS: standard ioctl arguments
  911. *
  912. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  913. * ioctls around modesetting so that any lost vblank events are accounted for.
  914. *
  915. * Generally the counter will reset across mode sets. If interrupts are
  916. * enabled around this call, we don't have to do anything since the counter
  917. * will have already been incremented.
  918. */
  919. int drm_modeset_ctl(struct drm_device *dev, void *data,
  920. struct drm_file *file_priv)
  921. {
  922. struct drm_modeset_ctl *modeset = data;
  923. unsigned int crtc;
  924. /* If drm_vblank_init() hasn't been called yet, just no-op */
  925. if (!dev->num_crtcs)
  926. return 0;
  927. /* KMS drivers handle this internally */
  928. if (drm_core_check_feature(dev, DRIVER_MODESET))
  929. return 0;
  930. crtc = modeset->crtc;
  931. if (crtc >= dev->num_crtcs)
  932. return -EINVAL;
  933. switch (modeset->cmd) {
  934. case _DRM_PRE_MODESET:
  935. drm_vblank_pre_modeset(dev, crtc);
  936. break;
  937. case _DRM_POST_MODESET:
  938. drm_vblank_post_modeset(dev, crtc);
  939. break;
  940. default:
  941. return -EINVAL;
  942. }
  943. return 0;
  944. }
  945. static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
  946. union drm_wait_vblank *vblwait,
  947. struct drm_file *file_priv)
  948. {
  949. struct drm_pending_vblank_event *e;
  950. struct timeval now;
  951. unsigned long flags;
  952. unsigned int seq;
  953. int ret;
  954. e = kzalloc(sizeof *e, GFP_KERNEL);
  955. if (e == NULL) {
  956. ret = -ENOMEM;
  957. goto err_put;
  958. }
  959. e->pipe = pipe;
  960. e->base.pid = current->pid;
  961. e->event.base.type = DRM_EVENT_VBLANK;
  962. e->event.base.length = sizeof e->event;
  963. e->event.user_data = vblwait->request.signal;
  964. e->base.event = &e->event.base;
  965. e->base.file_priv = file_priv;
  966. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  967. spin_lock_irqsave(&dev->event_lock, flags);
  968. if (file_priv->event_space < sizeof e->event) {
  969. ret = -EBUSY;
  970. goto err_unlock;
  971. }
  972. file_priv->event_space -= sizeof e->event;
  973. seq = drm_vblank_count_and_time(dev, pipe, &now);
  974. if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
  975. (seq - vblwait->request.sequence) <= (1 << 23)) {
  976. vblwait->request.sequence = seq + 1;
  977. vblwait->reply.sequence = vblwait->request.sequence;
  978. }
  979. DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
  980. vblwait->request.sequence, seq, pipe);
  981. trace_drm_vblank_event_queued(current->pid, pipe,
  982. vblwait->request.sequence);
  983. e->event.sequence = vblwait->request.sequence;
  984. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  985. drm_vblank_put(dev, pipe);
  986. send_vblank_event(dev, e, seq, &now);
  987. vblwait->reply.sequence = seq;
  988. } else {
  989. /* drm_handle_vblank_events will call drm_vblank_put */
  990. list_add_tail(&e->base.link, &dev->vblank_event_list);
  991. vblwait->reply.sequence = vblwait->request.sequence;
  992. }
  993. spin_unlock_irqrestore(&dev->event_lock, flags);
  994. return 0;
  995. err_unlock:
  996. spin_unlock_irqrestore(&dev->event_lock, flags);
  997. kfree(e);
  998. err_put:
  999. drm_vblank_put(dev, pipe);
  1000. return ret;
  1001. }
  1002. /**
  1003. * Wait for VBLANK.
  1004. *
  1005. * \param inode device inode.
  1006. * \param file_priv DRM file private.
  1007. * \param cmd command.
  1008. * \param data user argument, pointing to a drm_wait_vblank structure.
  1009. * \return zero on success or a negative number on failure.
  1010. *
  1011. * This function enables the vblank interrupt on the pipe requested, then
  1012. * sleeps waiting for the requested sequence number to occur, and drops
  1013. * the vblank interrupt refcount afterwards. (vblank irq disable follows that
  1014. * after a timeout with no further vblank waits scheduled).
  1015. */
  1016. int drm_wait_vblank(struct drm_device *dev, void *data,
  1017. struct drm_file *file_priv)
  1018. {
  1019. union drm_wait_vblank *vblwait = data;
  1020. int ret;
  1021. unsigned int flags, seq, crtc, high_crtc;
  1022. if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  1023. if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled))
  1024. return -EINVAL;
  1025. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1026. return -EINVAL;
  1027. if (vblwait->request.type &
  1028. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1029. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1030. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1031. vblwait->request.type,
  1032. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1033. _DRM_VBLANK_HIGH_CRTC_MASK));
  1034. return -EINVAL;
  1035. }
  1036. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1037. high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1038. if (high_crtc)
  1039. crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1040. else
  1041. crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1042. if (crtc >= dev->num_crtcs)
  1043. return -EINVAL;
  1044. ret = drm_vblank_get(dev, crtc);
  1045. if (ret) {
  1046. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  1047. return ret;
  1048. }
  1049. seq = drm_vblank_count(dev, crtc);
  1050. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1051. case _DRM_VBLANK_RELATIVE:
  1052. vblwait->request.sequence += seq;
  1053. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1054. case _DRM_VBLANK_ABSOLUTE:
  1055. break;
  1056. default:
  1057. ret = -EINVAL;
  1058. goto done;
  1059. }
  1060. if (flags & _DRM_VBLANK_EVENT) {
  1061. /* must hold on to the vblank ref until the event fires
  1062. * drm_vblank_put will be called asynchronously
  1063. */
  1064. return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
  1065. }
  1066. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1067. (seq - vblwait->request.sequence) <= (1<<23)) {
  1068. vblwait->request.sequence = seq + 1;
  1069. }
  1070. DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
  1071. vblwait->request.sequence, crtc);
  1072. dev->vblank[crtc].last_wait = vblwait->request.sequence;
  1073. DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * DRM_HZ,
  1074. (((drm_vblank_count(dev, crtc) -
  1075. vblwait->request.sequence) <= (1 << 23)) ||
  1076. !dev->irq_enabled));
  1077. if (ret != -EINTR) {
  1078. struct timeval now;
  1079. vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
  1080. vblwait->reply.tval_sec = now.tv_sec;
  1081. vblwait->reply.tval_usec = now.tv_usec;
  1082. DRM_DEBUG("returning %d to client\n",
  1083. vblwait->reply.sequence);
  1084. } else {
  1085. DRM_DEBUG("vblank wait interrupted by signal\n");
  1086. }
  1087. done:
  1088. drm_vblank_put(dev, crtc);
  1089. return ret;
  1090. }
  1091. static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
  1092. {
  1093. struct drm_pending_vblank_event *e, *t;
  1094. struct timeval now;
  1095. unsigned long flags;
  1096. unsigned int seq;
  1097. seq = drm_vblank_count_and_time(dev, crtc, &now);
  1098. spin_lock_irqsave(&dev->event_lock, flags);
  1099. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1100. if (e->pipe != crtc)
  1101. continue;
  1102. if ((seq - e->event.sequence) > (1<<23))
  1103. continue;
  1104. DRM_DEBUG("vblank event on %d, current %d\n",
  1105. e->event.sequence, seq);
  1106. list_del(&e->base.link);
  1107. drm_vblank_put(dev, e->pipe);
  1108. send_vblank_event(dev, e, seq, &now);
  1109. }
  1110. spin_unlock_irqrestore(&dev->event_lock, flags);
  1111. trace_drm_vblank_event(crtc, seq);
  1112. }
  1113. /**
  1114. * drm_handle_vblank - handle a vblank event
  1115. * @dev: DRM device
  1116. * @crtc: where this event occurred
  1117. *
  1118. * Drivers should call this routine in their vblank interrupt handlers to
  1119. * update the vblank counter and send any signals that may be pending.
  1120. */
  1121. bool drm_handle_vblank(struct drm_device *dev, int crtc)
  1122. {
  1123. u32 vblcount;
  1124. s64 diff_ns;
  1125. struct timeval tvblank;
  1126. unsigned long irqflags;
  1127. if (!dev->num_crtcs)
  1128. return false;
  1129. /* Need timestamp lock to prevent concurrent execution with
  1130. * vblank enable/disable, as this would cause inconsistent
  1131. * or corrupted timestamps and vblank counts.
  1132. */
  1133. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  1134. /* Vblank irq handling disabled. Nothing to do. */
  1135. if (!dev->vblank[crtc].enabled) {
  1136. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  1137. return false;
  1138. }
  1139. /* Fetch corresponding timestamp for this vblank interval from
  1140. * driver and store it in proper slot of timestamp ringbuffer.
  1141. */
  1142. /* Get current timestamp and count. */
  1143. vblcount = atomic_read(&dev->vblank[crtc].count);
  1144. drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
  1145. /* Compute time difference to timestamp of last vblank */
  1146. diff_ns = timeval_to_ns(&tvblank) -
  1147. timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
  1148. /* Update vblank timestamp and count if at least
  1149. * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
  1150. * difference between last stored timestamp and current
  1151. * timestamp. A smaller difference means basically
  1152. * identical timestamps. Happens if this vblank has
  1153. * been already processed and this is a redundant call,
  1154. * e.g., due to spurious vblank interrupts. We need to
  1155. * ignore those for accounting.
  1156. */
  1157. if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
  1158. /* Store new timestamp in ringbuffer. */
  1159. vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
  1160. /* Increment cooked vblank count. This also atomically commits
  1161. * the timestamp computed above.
  1162. */
  1163. smp_mb__before_atomic_inc();
  1164. atomic_inc(&dev->vblank[crtc].count);
  1165. smp_mb__after_atomic_inc();
  1166. } else {
  1167. DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
  1168. crtc, (int) diff_ns);
  1169. }
  1170. DRM_WAKEUP(&dev->vblank[crtc].queue);
  1171. drm_handle_vblank_events(dev, crtc);
  1172. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  1173. return true;
  1174. }
  1175. EXPORT_SYMBOL(drm_handle_vblank);