drm_irq.c 39 KB

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