drm_irq.c 39 KB

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