drm_vblank.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /*
  2. * drm_irq.c IRQ and vblank support
  3. *
  4. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  5. * \author Gareth Hughes <gareth@valinux.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <drm/drm_vblank.h>
  27. #include <drm/drmP.h>
  28. #include <linux/export.h>
  29. #include "drm_trace.h"
  30. #include "drm_internal.h"
  31. /**
  32. * DOC: vblank handling
  33. *
  34. * Vertical blanking plays a major role in graphics rendering. To achieve
  35. * tear-free display, users must synchronize page flips and/or rendering to
  36. * vertical blanking. The DRM API offers ioctls to perform page flips
  37. * synchronized to vertical blanking and wait for vertical blanking.
  38. *
  39. * The DRM core handles most of the vertical blanking management logic, which
  40. * involves filtering out spurious interrupts, keeping race-free blanking
  41. * counters, coping with counter wrap-around and resets and keeping use counts.
  42. * It relies on the driver to generate vertical blanking interrupts and
  43. * optionally provide a hardware vertical blanking counter.
  44. *
  45. * Drivers must initialize the vertical blanking handling core with a call to
  46. * drm_vblank_init(). Minimally, a driver needs to implement
  47. * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
  48. * drm_crtc_handle_vblank() in it's vblank interrupt handler for working vblank
  49. * support.
  50. *
  51. * Vertical blanking interrupts can be enabled by the DRM core or by drivers
  52. * themselves (for instance to handle page flipping operations). The DRM core
  53. * maintains a vertical blanking use count to ensure that the interrupts are not
  54. * disabled while a user still needs them. To increment the use count, drivers
  55. * call drm_crtc_vblank_get() and release the vblank reference again with
  56. * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
  57. * guaranteed to be enabled.
  58. *
  59. * On many hardware disabling the vblank interrupt cannot be done in a race-free
  60. * manner, see &drm_driver.vblank_disable_immediate and
  61. * &drm_driver.max_vblank_count. In that case the vblank core only disables the
  62. * vblanks after a timer has expired, which can be configured through the
  63. * ``vblankoffdelay`` module parameter.
  64. */
  65. /* Retry timestamp calculation up to 3 times to satisfy
  66. * drm_timestamp_precision before giving up.
  67. */
  68. #define DRM_TIMESTAMP_MAXRETRIES 3
  69. /* Threshold in nanoseconds for detection of redundant
  70. * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  71. */
  72. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  73. static bool
  74. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  75. struct timeval *tvblank, bool in_vblank_irq);
  76. static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
  77. /*
  78. * Default to use monotonic timestamps for wait-for-vblank and page-flip
  79. * complete events.
  80. */
  81. unsigned int drm_timestamp_monotonic = 1;
  82. static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
  83. module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
  84. module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
  85. module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
  86. MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
  87. MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
  88. MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
  89. static void store_vblank(struct drm_device *dev, unsigned int pipe,
  90. u32 vblank_count_inc,
  91. struct timeval *t_vblank, u32 last)
  92. {
  93. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  94. assert_spin_locked(&dev->vblank_time_lock);
  95. vblank->last = last;
  96. write_seqlock(&vblank->seqlock);
  97. vblank->time = *t_vblank;
  98. vblank->count += vblank_count_inc;
  99. write_sequnlock(&vblank->seqlock);
  100. }
  101. /*
  102. * "No hw counter" fallback implementation of .get_vblank_counter() hook,
  103. * if there is no useable hardware frame counter available.
  104. */
  105. static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
  106. {
  107. WARN_ON_ONCE(dev->max_vblank_count != 0);
  108. return 0;
  109. }
  110. static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  111. {
  112. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  113. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  114. if (crtc->funcs->get_vblank_counter)
  115. return crtc->funcs->get_vblank_counter(crtc);
  116. }
  117. if (dev->driver->get_vblank_counter)
  118. return dev->driver->get_vblank_counter(dev, pipe);
  119. return drm_vblank_no_hw_counter(dev, pipe);
  120. }
  121. /*
  122. * Reset the stored timestamp for the current vblank count to correspond
  123. * to the last vblank occurred.
  124. *
  125. * Only to be called from drm_crtc_vblank_on().
  126. *
  127. * Note: caller must hold &drm_device.vbl_lock since this reads & writes
  128. * device vblank fields.
  129. */
  130. static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
  131. {
  132. u32 cur_vblank;
  133. bool rc;
  134. struct timeval t_vblank;
  135. int count = DRM_TIMESTAMP_MAXRETRIES;
  136. spin_lock(&dev->vblank_time_lock);
  137. /*
  138. * sample the current counter to avoid random jumps
  139. * when drm_vblank_enable() applies the diff
  140. */
  141. do {
  142. cur_vblank = __get_vblank_counter(dev, pipe);
  143. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
  144. } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
  145. /*
  146. * Only reinitialize corresponding vblank timestamp if high-precision query
  147. * available and didn't fail. Otherwise reinitialize delayed at next vblank
  148. * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
  149. */
  150. if (!rc)
  151. t_vblank = (struct timeval) {0, 0};
  152. /*
  153. * +1 to make sure user will never see the same
  154. * vblank counter value before and after a modeset
  155. */
  156. store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
  157. spin_unlock(&dev->vblank_time_lock);
  158. }
  159. /*
  160. * Call back into the driver to update the appropriate vblank counter
  161. * (specified by @pipe). Deal with wraparound, if it occurred, and
  162. * update the last read value so we can deal with wraparound on the next
  163. * call if necessary.
  164. *
  165. * Only necessary when going from off->on, to account for frames we
  166. * didn't get an interrupt for.
  167. *
  168. * Note: caller must hold &drm_device.vbl_lock since this reads & writes
  169. * device vblank fields.
  170. */
  171. static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
  172. bool in_vblank_irq)
  173. {
  174. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  175. u32 cur_vblank, diff;
  176. bool rc;
  177. struct timeval t_vblank;
  178. int count = DRM_TIMESTAMP_MAXRETRIES;
  179. int framedur_ns = vblank->framedur_ns;
  180. /*
  181. * Interrupts were disabled prior to this call, so deal with counter
  182. * wrap if needed.
  183. * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
  184. * here if the register is small or we had vblank interrupts off for
  185. * a long time.
  186. *
  187. * We repeat the hardware vblank counter & timestamp query until
  188. * we get consistent results. This to prevent races between gpu
  189. * updating its hardware counter while we are retrieving the
  190. * corresponding vblank timestamp.
  191. */
  192. do {
  193. cur_vblank = __get_vblank_counter(dev, pipe);
  194. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
  195. } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
  196. if (dev->max_vblank_count != 0) {
  197. /* trust the hw counter when it's around */
  198. diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
  199. } else if (rc && framedur_ns) {
  200. const struct timeval *t_old;
  201. u64 diff_ns;
  202. t_old = &vblank->time;
  203. diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
  204. /*
  205. * Figure out how many vblanks we've missed based
  206. * on the difference in the timestamps and the
  207. * frame/field duration.
  208. */
  209. diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
  210. if (diff == 0 && in_vblank_irq)
  211. DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
  212. " diff_ns = %lld, framedur_ns = %d)\n",
  213. pipe, (long long) diff_ns, framedur_ns);
  214. } else {
  215. /* some kind of default for drivers w/o accurate vbl timestamping */
  216. diff = in_vblank_irq ? 1 : 0;
  217. }
  218. /*
  219. * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
  220. * interval? If so then vblank irqs keep running and it will likely
  221. * happen that the hardware vblank counter is not trustworthy as it
  222. * might reset at some point in that interval and vblank timestamps
  223. * are not trustworthy either in that interval. Iow. this can result
  224. * in a bogus diff >> 1 which must be avoided as it would cause
  225. * random large forward jumps of the software vblank counter.
  226. */
  227. if (diff > 1 && (vblank->inmodeset & 0x2)) {
  228. DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
  229. " due to pre-modeset.\n", pipe, diff);
  230. diff = 1;
  231. }
  232. DRM_DEBUG_VBL("updating vblank count on crtc %u:"
  233. " current=%u, diff=%u, hw=%u hw_last=%u\n",
  234. pipe, vblank->count, diff, cur_vblank, vblank->last);
  235. if (diff == 0) {
  236. WARN_ON_ONCE(cur_vblank != vblank->last);
  237. return;
  238. }
  239. /*
  240. * Only reinitialize corresponding vblank timestamp if high-precision query
  241. * available and didn't fail, or we were called from the vblank interrupt.
  242. * Otherwise reinitialize delayed at next vblank interrupt and assign 0
  243. * for now, to mark the vblanktimestamp as invalid.
  244. */
  245. if (!rc && !in_vblank_irq)
  246. t_vblank = (struct timeval) {0, 0};
  247. store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
  248. }
  249. static u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
  250. {
  251. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  252. if (WARN_ON(pipe >= dev->num_crtcs))
  253. return 0;
  254. return vblank->count;
  255. }
  256. /**
  257. * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
  258. * @crtc: which counter to retrieve
  259. *
  260. * This function is similar to drm_crtc_vblank_count() but this function
  261. * interpolates to handle a race with vblank interrupts using the high precision
  262. * timestamping support.
  263. *
  264. * This is mostly useful for hardware that can obtain the scanout position, but
  265. * doesn't have a hardware frame counter.
  266. */
  267. u32 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
  268. {
  269. struct drm_device *dev = crtc->dev;
  270. unsigned int pipe = drm_crtc_index(crtc);
  271. u32 vblank;
  272. unsigned long flags;
  273. WARN(!dev->driver->get_vblank_timestamp,
  274. "This function requires support for accurate vblank timestamps.");
  275. spin_lock_irqsave(&dev->vblank_time_lock, flags);
  276. drm_update_vblank_count(dev, pipe, false);
  277. vblank = drm_vblank_count(dev, pipe);
  278. spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
  279. return vblank;
  280. }
  281. EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
  282. static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
  283. {
  284. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  285. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  286. if (crtc->funcs->disable_vblank) {
  287. crtc->funcs->disable_vblank(crtc);
  288. return;
  289. }
  290. }
  291. dev->driver->disable_vblank(dev, pipe);
  292. }
  293. /*
  294. * Disable vblank irq's on crtc, make sure that last vblank count
  295. * of hardware and corresponding consistent software vblank counter
  296. * are preserved, even if there are any spurious vblank irq's after
  297. * disable.
  298. */
  299. void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
  300. {
  301. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  302. unsigned long irqflags;
  303. assert_spin_locked(&dev->vbl_lock);
  304. /* Prevent vblank irq processing while disabling vblank irqs,
  305. * so no updates of timestamps or count can happen after we've
  306. * disabled. Needed to prevent races in case of delayed irq's.
  307. */
  308. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  309. /*
  310. * Only disable vblank interrupts if they're enabled. This avoids
  311. * calling the ->disable_vblank() operation in atomic context with the
  312. * hardware potentially runtime suspended.
  313. */
  314. if (vblank->enabled) {
  315. __disable_vblank(dev, pipe);
  316. vblank->enabled = false;
  317. }
  318. /*
  319. * Always update the count and timestamp to maintain the
  320. * appearance that the counter has been ticking all along until
  321. * this time. This makes the count account for the entire time
  322. * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
  323. */
  324. drm_update_vblank_count(dev, pipe, false);
  325. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  326. }
  327. static void vblank_disable_fn(unsigned long arg)
  328. {
  329. struct drm_vblank_crtc *vblank = (void *)arg;
  330. struct drm_device *dev = vblank->dev;
  331. unsigned int pipe = vblank->pipe;
  332. unsigned long irqflags;
  333. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  334. if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  335. DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
  336. drm_vblank_disable_and_save(dev, pipe);
  337. }
  338. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  339. }
  340. void drm_vblank_cleanup(struct drm_device *dev)
  341. {
  342. unsigned int pipe;
  343. /* Bail if the driver didn't call drm_vblank_init() */
  344. if (dev->num_crtcs == 0)
  345. return;
  346. for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
  347. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  348. WARN_ON(READ_ONCE(vblank->enabled) &&
  349. drm_core_check_feature(dev, DRIVER_MODESET));
  350. del_timer_sync(&vblank->disable_timer);
  351. }
  352. kfree(dev->vblank);
  353. dev->num_crtcs = 0;
  354. }
  355. /**
  356. * drm_vblank_init - initialize vblank support
  357. * @dev: DRM device
  358. * @num_crtcs: number of CRTCs supported by @dev
  359. *
  360. * This function initializes vblank support for @num_crtcs display pipelines.
  361. * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
  362. * drivers with a &drm_driver.release callback.
  363. *
  364. * Returns:
  365. * Zero on success or a negative error code on failure.
  366. */
  367. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
  368. {
  369. int ret = -ENOMEM;
  370. unsigned int i;
  371. spin_lock_init(&dev->vbl_lock);
  372. spin_lock_init(&dev->vblank_time_lock);
  373. dev->num_crtcs = num_crtcs;
  374. dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  375. if (!dev->vblank)
  376. goto err;
  377. for (i = 0; i < num_crtcs; i++) {
  378. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  379. vblank->dev = dev;
  380. vblank->pipe = i;
  381. init_waitqueue_head(&vblank->queue);
  382. setup_timer(&vblank->disable_timer, vblank_disable_fn,
  383. (unsigned long)vblank);
  384. seqlock_init(&vblank->seqlock);
  385. }
  386. DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
  387. /* Driver specific high-precision vblank timestamping supported? */
  388. if (dev->driver->get_vblank_timestamp)
  389. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  390. else
  391. DRM_INFO("No driver support for vblank timestamp query.\n");
  392. /* Must have precise timestamping for reliable vblank instant disable */
  393. if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
  394. dev->vblank_disable_immediate = false;
  395. DRM_INFO("Setting vblank_disable_immediate to false because "
  396. "get_vblank_timestamp == NULL\n");
  397. }
  398. return 0;
  399. err:
  400. dev->num_crtcs = 0;
  401. return ret;
  402. }
  403. EXPORT_SYMBOL(drm_vblank_init);
  404. /**
  405. * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
  406. * @crtc: which CRTC's vblank waitqueue to retrieve
  407. *
  408. * This function returns a pointer to the vblank waitqueue for the CRTC.
  409. * Drivers can use this to implement vblank waits using wait_event() and related
  410. * functions.
  411. */
  412. wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
  413. {
  414. return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
  415. }
  416. EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
  417. /**
  418. * drm_calc_timestamping_constants - calculate vblank timestamp constants
  419. * @crtc: drm_crtc whose timestamp constants should be updated.
  420. * @mode: display mode containing the scanout timings
  421. *
  422. * Calculate and store various constants which are later needed by vblank and
  423. * swap-completion timestamping, e.g, by
  424. * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true
  425. * scanout timing, so they take things like panel scaling or other adjustments
  426. * into account.
  427. */
  428. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  429. const struct drm_display_mode *mode)
  430. {
  431. struct drm_device *dev = crtc->dev;
  432. unsigned int pipe = drm_crtc_index(crtc);
  433. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  434. int linedur_ns = 0, framedur_ns = 0;
  435. int dotclock = mode->crtc_clock;
  436. if (!dev->num_crtcs)
  437. return;
  438. if (WARN_ON(pipe >= dev->num_crtcs))
  439. return;
  440. /* Valid dotclock? */
  441. if (dotclock > 0) {
  442. int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
  443. /*
  444. * Convert scanline length in pixels and video
  445. * dot clock to line duration and frame duration
  446. * in nanoseconds:
  447. */
  448. linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
  449. framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
  450. /*
  451. * Fields of interlaced scanout modes are only half a frame duration.
  452. */
  453. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  454. framedur_ns /= 2;
  455. } else
  456. DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
  457. crtc->base.id);
  458. vblank->linedur_ns = linedur_ns;
  459. vblank->framedur_ns = framedur_ns;
  460. vblank->hwmode = *mode;
  461. DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  462. crtc->base.id, mode->crtc_htotal,
  463. mode->crtc_vtotal, mode->crtc_vdisplay);
  464. DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
  465. crtc->base.id, dotclock, framedur_ns, linedur_ns);
  466. }
  467. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  468. /**
  469. * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  470. * @dev: DRM device
  471. * @pipe: index of CRTC whose vblank timestamp to retrieve
  472. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  473. * On return contains true maximum error of timestamp
  474. * @vblank_time: Pointer to struct timeval which should receive the timestamp
  475. * @in_vblank_irq:
  476. * True when called from drm_crtc_handle_vblank(). Some drivers
  477. * need to apply some workarounds for gpu-specific vblank irq quirks
  478. * if flag is set.
  479. *
  480. * Implements calculation of exact vblank timestamps from given drm_display_mode
  481. * timings and current video scanout position of a CRTC. This can be directly
  482. * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver
  483. * if &drm_driver.get_scanout_position is implemented.
  484. *
  485. * The current implementation only handles standard video modes. For double scan
  486. * and interlaced modes the driver is supposed to adjust the hardware mode
  487. * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
  488. * match the scanout position reported.
  489. *
  490. * Note that atomic drivers must call drm_calc_timestamping_constants() before
  491. * enabling a CRTC. The atomic helpers already take care of that in
  492. * drm_atomic_helper_update_legacy_modeset_state().
  493. *
  494. * Returns:
  495. *
  496. * Returns true on success, and false on failure, i.e. when no accurate
  497. * timestamp could be acquired.
  498. */
  499. bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  500. unsigned int pipe,
  501. int *max_error,
  502. struct timeval *vblank_time,
  503. bool in_vblank_irq)
  504. {
  505. struct timeval tv_etime;
  506. ktime_t stime, etime;
  507. bool vbl_status;
  508. struct drm_crtc *crtc;
  509. const struct drm_display_mode *mode;
  510. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  511. int vpos, hpos, i;
  512. int delta_ns, duration_ns;
  513. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  514. return false;
  515. crtc = drm_crtc_from_index(dev, pipe);
  516. if (pipe >= dev->num_crtcs || !crtc) {
  517. DRM_ERROR("Invalid crtc %u\n", pipe);
  518. return false;
  519. }
  520. /* Scanout position query not supported? Should not happen. */
  521. if (!dev->driver->get_scanout_position) {
  522. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  523. return false;
  524. }
  525. if (drm_drv_uses_atomic_modeset(dev))
  526. mode = &vblank->hwmode;
  527. else
  528. mode = &crtc->hwmode;
  529. /* If mode timing undefined, just return as no-op:
  530. * Happens during initial modesetting of a crtc.
  531. */
  532. if (mode->crtc_clock == 0) {
  533. DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
  534. WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev));
  535. return false;
  536. }
  537. /* Get current scanout position with system timestamp.
  538. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  539. * if single query takes longer than max_error nanoseconds.
  540. *
  541. * This guarantees a tight bound on maximum error if
  542. * code gets preempted or delayed for some reason.
  543. */
  544. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  545. /*
  546. * Get vertical and horizontal scanout position vpos, hpos,
  547. * and bounding timestamps stime, etime, pre/post query.
  548. */
  549. vbl_status = dev->driver->get_scanout_position(dev, pipe,
  550. in_vblank_irq,
  551. &vpos, &hpos,
  552. &stime, &etime,
  553. mode);
  554. /* Return as no-op if scanout query unsupported or failed. */
  555. if (!vbl_status) {
  556. DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
  557. pipe);
  558. return false;
  559. }
  560. /* Compute uncertainty in timestamp of scanout position query. */
  561. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  562. /* Accept result with < max_error nsecs timing uncertainty. */
  563. if (duration_ns <= *max_error)
  564. break;
  565. }
  566. /* Noisy system timing? */
  567. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  568. DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
  569. pipe, duration_ns/1000, *max_error/1000, i);
  570. }
  571. /* Return upper bound of timestamp precision error. */
  572. *max_error = duration_ns;
  573. /* Convert scanout position into elapsed time at raw_time query
  574. * since start of scanout at first display scanline. delta_ns
  575. * can be negative if start of scanout hasn't happened yet.
  576. */
  577. delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  578. mode->crtc_clock);
  579. if (!drm_timestamp_monotonic)
  580. etime = ktime_mono_to_real(etime);
  581. /* save this only for debugging purposes */
  582. tv_etime = ktime_to_timeval(etime);
  583. /* Subtract time delta from raw timestamp to get final
  584. * vblank_time timestamp for end of vblank.
  585. */
  586. etime = ktime_sub_ns(etime, delta_ns);
  587. *vblank_time = ktime_to_timeval(etime);
  588. DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  589. pipe, hpos, vpos,
  590. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  591. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  592. duration_ns/1000, i);
  593. return true;
  594. }
  595. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  596. static struct timeval get_drm_timestamp(void)
  597. {
  598. ktime_t now;
  599. now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
  600. return ktime_to_timeval(now);
  601. }
  602. /**
  603. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  604. * vblank interval
  605. * @dev: DRM device
  606. * @pipe: index of CRTC whose vblank timestamp to retrieve
  607. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  608. * @in_vblank_irq:
  609. * True when called from drm_crtc_handle_vblank(). Some drivers
  610. * need to apply some workarounds for gpu-specific vblank irq quirks
  611. * if flag is set.
  612. *
  613. * Fetches the system timestamp corresponding to the time of the most recent
  614. * vblank interval on specified CRTC. May call into kms-driver to
  615. * compute the timestamp with a high-precision GPU specific method.
  616. *
  617. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  618. * call, i.e., it isn't very precisely locked to the true vblank.
  619. *
  620. * Returns:
  621. * True if timestamp is considered to be very precise, false otherwise.
  622. */
  623. static bool
  624. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  625. struct timeval *tvblank, bool in_vblank_irq)
  626. {
  627. bool ret = false;
  628. /* Define requested maximum error on timestamps (nanoseconds). */
  629. int max_error = (int) drm_timestamp_precision * 1000;
  630. /* Query driver if possible and precision timestamping enabled. */
  631. if (dev->driver->get_vblank_timestamp && (max_error > 0))
  632. ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
  633. tvblank, in_vblank_irq);
  634. /* GPU high precision timestamp query unsupported or failed.
  635. * Return current monotonic/gettimeofday timestamp as best estimate.
  636. */
  637. if (!ret)
  638. *tvblank = get_drm_timestamp();
  639. return ret;
  640. }
  641. /**
  642. * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  643. * @crtc: which counter to retrieve
  644. *
  645. * Fetches the "cooked" vblank count value that represents the number of
  646. * vblank events since the system was booted, including lost events due to
  647. * modesetting activity. Note that this timer isn't correct against a racing
  648. * vblank interrupt (since it only reports the software vblank counter), see
  649. * drm_crtc_accurate_vblank_count() for such use-cases.
  650. *
  651. * Returns:
  652. * The software vblank counter.
  653. */
  654. u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
  655. {
  656. return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  657. }
  658. EXPORT_SYMBOL(drm_crtc_vblank_count);
  659. static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  660. struct timeval *vblanktime)
  661. {
  662. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  663. u32 vblank_count;
  664. unsigned int seq;
  665. if (WARN_ON(pipe >= dev->num_crtcs)) {
  666. *vblanktime = (struct timeval) { 0 };
  667. return 0;
  668. }
  669. do {
  670. seq = read_seqbegin(&vblank->seqlock);
  671. vblank_count = vblank->count;
  672. *vblanktime = vblank->time;
  673. } while (read_seqretry(&vblank->seqlock, seq));
  674. return vblank_count;
  675. }
  676. /**
  677. * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  678. * and the system timestamp corresponding to that vblank counter value
  679. * @crtc: which counter to retrieve
  680. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  681. *
  682. * Fetches the "cooked" vblank count value that represents the number of
  683. * vblank events since the system was booted, including lost events due to
  684. * modesetting activity. Returns corresponding system timestamp of the time
  685. * of the vblank interval that corresponds to the current vblank counter value.
  686. */
  687. u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  688. struct timeval *vblanktime)
  689. {
  690. return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  691. vblanktime);
  692. }
  693. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  694. static void send_vblank_event(struct drm_device *dev,
  695. struct drm_pending_vblank_event *e,
  696. unsigned long seq, struct timeval *now)
  697. {
  698. e->event.sequence = seq;
  699. e->event.tv_sec = now->tv_sec;
  700. e->event.tv_usec = now->tv_usec;
  701. trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe,
  702. e->event.sequence);
  703. drm_send_event_locked(dev, &e->base);
  704. }
  705. /**
  706. * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  707. * @crtc: the source CRTC of the vblank event
  708. * @e: the event to send
  709. *
  710. * A lot of drivers need to generate vblank events for the very next vblank
  711. * interrupt. For example when the page flip interrupt happens when the page
  712. * flip gets armed, but not when it actually executes within the next vblank
  713. * period. This helper function implements exactly the required vblank arming
  714. * behaviour.
  715. *
  716. * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
  717. * atomic commit must ensure that the next vblank happens at exactly the same
  718. * time as the atomic commit is committed to the hardware. This function itself
  719. * does **not** protect against the next vblank interrupt racing with either this
  720. * function call or the atomic commit operation. A possible sequence could be:
  721. *
  722. * 1. Driver commits new hardware state into vblank-synchronized registers.
  723. * 2. A vblank happens, committing the hardware state. Also the corresponding
  724. * vblank interrupt is fired off and fully processed by the interrupt
  725. * handler.
  726. * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
  727. * 4. The event is only send out for the next vblank, which is wrong.
  728. *
  729. * An equivalent race can happen when the driver calls
  730. * drm_crtc_arm_vblank_event() before writing out the new hardware state.
  731. *
  732. * The only way to make this work safely is to prevent the vblank from firing
  733. * (and the hardware from committing anything else) until the entire atomic
  734. * commit sequence has run to completion. If the hardware does not have such a
  735. * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
  736. * Instead drivers need to manually send out the event from their interrupt
  737. * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
  738. * possible race with the hardware committing the atomic update.
  739. *
  740. * Caller must hold a vblank reference for the event @e, which will be dropped
  741. * when the next vblank arrives.
  742. */
  743. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  744. struct drm_pending_vblank_event *e)
  745. {
  746. struct drm_device *dev = crtc->dev;
  747. unsigned int pipe = drm_crtc_index(crtc);
  748. assert_spin_locked(&dev->event_lock);
  749. e->pipe = pipe;
  750. e->event.sequence = drm_vblank_count(dev, pipe);
  751. e->event.crtc_id = crtc->base.id;
  752. list_add_tail(&e->base.link, &dev->vblank_event_list);
  753. }
  754. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  755. /**
  756. * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  757. * @crtc: the source CRTC of the vblank event
  758. * @e: the event to send
  759. *
  760. * Updates sequence # and timestamp on event for the most recently processed
  761. * vblank, and sends it to userspace. Caller must hold event lock.
  762. *
  763. * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
  764. * situation, especially to send out events for atomic commit operations.
  765. */
  766. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  767. struct drm_pending_vblank_event *e)
  768. {
  769. struct drm_device *dev = crtc->dev;
  770. unsigned int seq, pipe = drm_crtc_index(crtc);
  771. struct timeval now;
  772. if (dev->num_crtcs > 0) {
  773. seq = drm_vblank_count_and_time(dev, pipe, &now);
  774. } else {
  775. seq = 0;
  776. now = get_drm_timestamp();
  777. }
  778. e->pipe = pipe;
  779. e->event.crtc_id = crtc->base.id;
  780. send_vblank_event(dev, e, seq, &now);
  781. }
  782. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  783. static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
  784. {
  785. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  786. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  787. if (crtc->funcs->enable_vblank)
  788. return crtc->funcs->enable_vblank(crtc);
  789. }
  790. return dev->driver->enable_vblank(dev, pipe);
  791. }
  792. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  793. {
  794. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  795. int ret = 0;
  796. assert_spin_locked(&dev->vbl_lock);
  797. spin_lock(&dev->vblank_time_lock);
  798. if (!vblank->enabled) {
  799. /*
  800. * Enable vblank irqs under vblank_time_lock protection.
  801. * All vblank count & timestamp updates are held off
  802. * until we are done reinitializing master counter and
  803. * timestamps. Filtercode in drm_handle_vblank() will
  804. * prevent double-accounting of same vblank interval.
  805. */
  806. ret = __enable_vblank(dev, pipe);
  807. DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
  808. if (ret) {
  809. atomic_dec(&vblank->refcount);
  810. } else {
  811. drm_update_vblank_count(dev, pipe, 0);
  812. /* drm_update_vblank_count() includes a wmb so we just
  813. * need to ensure that the compiler emits the write
  814. * to mark the vblank as enabled after the call
  815. * to drm_update_vblank_count().
  816. */
  817. WRITE_ONCE(vblank->enabled, true);
  818. }
  819. }
  820. spin_unlock(&dev->vblank_time_lock);
  821. return ret;
  822. }
  823. static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  824. {
  825. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  826. unsigned long irqflags;
  827. int ret = 0;
  828. if (!dev->num_crtcs)
  829. return -EINVAL;
  830. if (WARN_ON(pipe >= dev->num_crtcs))
  831. return -EINVAL;
  832. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  833. /* Going from 0->1 means we have to enable interrupts again */
  834. if (atomic_add_return(1, &vblank->refcount) == 1) {
  835. ret = drm_vblank_enable(dev, pipe);
  836. } else {
  837. if (!vblank->enabled) {
  838. atomic_dec(&vblank->refcount);
  839. ret = -EINVAL;
  840. }
  841. }
  842. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  843. return ret;
  844. }
  845. /**
  846. * drm_crtc_vblank_get - get a reference count on vblank events
  847. * @crtc: which CRTC to own
  848. *
  849. * Acquire a reference count on vblank events to avoid having them disabled
  850. * while in use.
  851. *
  852. * Returns:
  853. * Zero on success or a negative error code on failure.
  854. */
  855. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  856. {
  857. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  858. }
  859. EXPORT_SYMBOL(drm_crtc_vblank_get);
  860. static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  861. {
  862. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  863. if (WARN_ON(pipe >= dev->num_crtcs))
  864. return;
  865. if (WARN_ON(atomic_read(&vblank->refcount) == 0))
  866. return;
  867. /* Last user schedules interrupt disable */
  868. if (atomic_dec_and_test(&vblank->refcount)) {
  869. if (drm_vblank_offdelay == 0)
  870. return;
  871. else if (drm_vblank_offdelay < 0)
  872. vblank_disable_fn((unsigned long)vblank);
  873. else if (!dev->vblank_disable_immediate)
  874. mod_timer(&vblank->disable_timer,
  875. jiffies + ((drm_vblank_offdelay * HZ)/1000));
  876. }
  877. }
  878. /**
  879. * drm_crtc_vblank_put - give up ownership of vblank events
  880. * @crtc: which counter to give up
  881. *
  882. * Release ownership of a given vblank counter, turning off interrupts
  883. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  884. */
  885. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  886. {
  887. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  888. }
  889. EXPORT_SYMBOL(drm_crtc_vblank_put);
  890. /**
  891. * drm_wait_one_vblank - wait for one vblank
  892. * @dev: DRM device
  893. * @pipe: CRTC index
  894. *
  895. * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
  896. * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  897. * due to lack of driver support or because the crtc is off.
  898. *
  899. * This is the legacy version of drm_crtc_wait_one_vblank().
  900. */
  901. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
  902. {
  903. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  904. int ret;
  905. u32 last;
  906. if (WARN_ON(pipe >= dev->num_crtcs))
  907. return;
  908. ret = drm_vblank_get(dev, pipe);
  909. if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
  910. return;
  911. last = drm_vblank_count(dev, pipe);
  912. ret = wait_event_timeout(vblank->queue,
  913. last != drm_vblank_count(dev, pipe),
  914. msecs_to_jiffies(100));
  915. WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  916. drm_vblank_put(dev, pipe);
  917. }
  918. EXPORT_SYMBOL(drm_wait_one_vblank);
  919. /**
  920. * drm_crtc_wait_one_vblank - wait for one vblank
  921. * @crtc: DRM crtc
  922. *
  923. * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  924. * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  925. * due to lack of driver support or because the crtc is off.
  926. */
  927. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  928. {
  929. drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
  930. }
  931. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  932. /**
  933. * drm_crtc_vblank_off - disable vblank events on a CRTC
  934. * @crtc: CRTC in question
  935. *
  936. * Drivers can use this function to shut down the vblank interrupt handling when
  937. * disabling a crtc. This function ensures that the latest vblank frame count is
  938. * stored so that drm_vblank_on can restore it again.
  939. *
  940. * Drivers must use this function when the hardware vblank counter can get
  941. * reset, e.g. when suspending or disabling the @crtc in general.
  942. */
  943. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  944. {
  945. struct drm_device *dev = crtc->dev;
  946. unsigned int pipe = drm_crtc_index(crtc);
  947. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  948. struct drm_pending_vblank_event *e, *t;
  949. struct timeval now;
  950. unsigned long irqflags;
  951. unsigned int seq;
  952. if (WARN_ON(pipe >= dev->num_crtcs))
  953. return;
  954. spin_lock_irqsave(&dev->event_lock, irqflags);
  955. spin_lock(&dev->vbl_lock);
  956. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  957. pipe, vblank->enabled, vblank->inmodeset);
  958. /* Avoid redundant vblank disables without previous
  959. * drm_crtc_vblank_on(). */
  960. if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
  961. drm_vblank_disable_and_save(dev, pipe);
  962. wake_up(&vblank->queue);
  963. /*
  964. * Prevent subsequent drm_vblank_get() from re-enabling
  965. * the vblank interrupt by bumping the refcount.
  966. */
  967. if (!vblank->inmodeset) {
  968. atomic_inc(&vblank->refcount);
  969. vblank->inmodeset = 1;
  970. }
  971. spin_unlock(&dev->vbl_lock);
  972. /* Send any queued vblank events, lest the natives grow disquiet */
  973. seq = drm_vblank_count_and_time(dev, pipe, &now);
  974. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  975. if (e->pipe != pipe)
  976. continue;
  977. DRM_DEBUG("Sending premature vblank event on disable: "
  978. "wanted %u, current %u\n",
  979. e->event.sequence, seq);
  980. list_del(&e->base.link);
  981. drm_vblank_put(dev, pipe);
  982. send_vblank_event(dev, e, seq, &now);
  983. }
  984. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  985. /* Will be reset by the modeset helpers when re-enabling the crtc by
  986. * calling drm_calc_timestamping_constants(). */
  987. vblank->hwmode.crtc_clock = 0;
  988. }
  989. EXPORT_SYMBOL(drm_crtc_vblank_off);
  990. /**
  991. * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  992. * @crtc: CRTC in question
  993. *
  994. * Drivers can use this function to reset the vblank state to off at load time.
  995. * Drivers should use this together with the drm_crtc_vblank_off() and
  996. * drm_crtc_vblank_on() functions. The difference compared to
  997. * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  998. * and hence doesn't need to call any driver hooks.
  999. *
  1000. * This is useful for recovering driver state e.g. on driver load, or on resume.
  1001. */
  1002. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1003. {
  1004. struct drm_device *dev = crtc->dev;
  1005. unsigned long irqflags;
  1006. unsigned int pipe = drm_crtc_index(crtc);
  1007. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1008. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1009. /*
  1010. * Prevent subsequent drm_vblank_get() from enabling the vblank
  1011. * interrupt by bumping the refcount.
  1012. */
  1013. if (!vblank->inmodeset) {
  1014. atomic_inc(&vblank->refcount);
  1015. vblank->inmodeset = 1;
  1016. }
  1017. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1018. WARN_ON(!list_empty(&dev->vblank_event_list));
  1019. }
  1020. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1021. /**
  1022. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1023. * @crtc: CRTC in question
  1024. *
  1025. * This functions restores the vblank interrupt state captured with
  1026. * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
  1027. * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
  1028. * unbalanced and so can also be unconditionally called in driver load code to
  1029. * reflect the current hardware state of the crtc.
  1030. */
  1031. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1032. {
  1033. struct drm_device *dev = crtc->dev;
  1034. unsigned int pipe = drm_crtc_index(crtc);
  1035. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1036. unsigned long irqflags;
  1037. if (WARN_ON(pipe >= dev->num_crtcs))
  1038. return;
  1039. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1040. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1041. pipe, vblank->enabled, vblank->inmodeset);
  1042. /* Drop our private "prevent drm_vblank_get" refcount */
  1043. if (vblank->inmodeset) {
  1044. atomic_dec(&vblank->refcount);
  1045. vblank->inmodeset = 0;
  1046. }
  1047. drm_reset_vblank_timestamp(dev, pipe);
  1048. /*
  1049. * re-enable interrupts if there are users left, or the
  1050. * user wishes vblank interrupts to be enabled all the time.
  1051. */
  1052. if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
  1053. WARN_ON(drm_vblank_enable(dev, pipe));
  1054. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1055. }
  1056. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1057. static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
  1058. unsigned int pipe)
  1059. {
  1060. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1061. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1062. if (!dev->num_crtcs)
  1063. return;
  1064. if (WARN_ON(pipe >= dev->num_crtcs))
  1065. return;
  1066. /*
  1067. * To avoid all the problems that might happen if interrupts
  1068. * were enabled/disabled around or between these calls, we just
  1069. * have the kernel take a reference on the CRTC (just once though
  1070. * to avoid corrupting the count if multiple, mismatch calls occur),
  1071. * so that interrupts remain enabled in the interim.
  1072. */
  1073. if (!vblank->inmodeset) {
  1074. vblank->inmodeset = 0x1;
  1075. if (drm_vblank_get(dev, pipe) == 0)
  1076. vblank->inmodeset |= 0x2;
  1077. }
  1078. }
  1079. static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
  1080. unsigned int pipe)
  1081. {
  1082. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1083. unsigned long irqflags;
  1084. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1085. if (!dev->num_crtcs)
  1086. return;
  1087. if (WARN_ON(pipe >= dev->num_crtcs))
  1088. return;
  1089. if (vblank->inmodeset) {
  1090. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1091. drm_reset_vblank_timestamp(dev, pipe);
  1092. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1093. if (vblank->inmodeset & 0x2)
  1094. drm_vblank_put(dev, pipe);
  1095. vblank->inmodeset = 0;
  1096. }
  1097. }
  1098. int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
  1099. struct drm_file *file_priv)
  1100. {
  1101. struct drm_modeset_ctl *modeset = data;
  1102. unsigned int pipe;
  1103. /* If drm_vblank_init() hasn't been called yet, just no-op */
  1104. if (!dev->num_crtcs)
  1105. return 0;
  1106. /* KMS drivers handle this internally */
  1107. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1108. return 0;
  1109. pipe = modeset->crtc;
  1110. if (pipe >= dev->num_crtcs)
  1111. return -EINVAL;
  1112. switch (modeset->cmd) {
  1113. case _DRM_PRE_MODESET:
  1114. drm_legacy_vblank_pre_modeset(dev, pipe);
  1115. break;
  1116. case _DRM_POST_MODESET:
  1117. drm_legacy_vblank_post_modeset(dev, pipe);
  1118. break;
  1119. default:
  1120. return -EINVAL;
  1121. }
  1122. return 0;
  1123. }
  1124. static inline bool vblank_passed(u32 seq, u32 ref)
  1125. {
  1126. return (seq - ref) <= (1 << 23);
  1127. }
  1128. static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
  1129. union drm_wait_vblank *vblwait,
  1130. struct drm_file *file_priv)
  1131. {
  1132. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1133. struct drm_pending_vblank_event *e;
  1134. struct timeval now;
  1135. unsigned long flags;
  1136. unsigned int seq;
  1137. int ret;
  1138. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1139. if (e == NULL) {
  1140. ret = -ENOMEM;
  1141. goto err_put;
  1142. }
  1143. e->pipe = pipe;
  1144. e->event.base.type = DRM_EVENT_VBLANK;
  1145. e->event.base.length = sizeof(e->event);
  1146. e->event.user_data = vblwait->request.signal;
  1147. spin_lock_irqsave(&dev->event_lock, flags);
  1148. /*
  1149. * drm_crtc_vblank_off() might have been called after we called
  1150. * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
  1151. * vblank disable, so no need for further locking. The reference from
  1152. * drm_vblank_get() protects against vblank disable from another source.
  1153. */
  1154. if (!READ_ONCE(vblank->enabled)) {
  1155. ret = -EINVAL;
  1156. goto err_unlock;
  1157. }
  1158. ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
  1159. &e->event.base);
  1160. if (ret)
  1161. goto err_unlock;
  1162. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1163. DRM_DEBUG("event on vblank count %u, current %u, crtc %u\n",
  1164. vblwait->request.sequence, seq, pipe);
  1165. trace_drm_vblank_event_queued(file_priv, pipe,
  1166. vblwait->request.sequence);
  1167. e->event.sequence = vblwait->request.sequence;
  1168. if (vblank_passed(seq, vblwait->request.sequence)) {
  1169. drm_vblank_put(dev, pipe);
  1170. send_vblank_event(dev, e, seq, &now);
  1171. vblwait->reply.sequence = seq;
  1172. } else {
  1173. /* drm_handle_vblank_events will call drm_vblank_put */
  1174. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1175. vblwait->reply.sequence = vblwait->request.sequence;
  1176. }
  1177. spin_unlock_irqrestore(&dev->event_lock, flags);
  1178. return 0;
  1179. err_unlock:
  1180. spin_unlock_irqrestore(&dev->event_lock, flags);
  1181. kfree(e);
  1182. err_put:
  1183. drm_vblank_put(dev, pipe);
  1184. return ret;
  1185. }
  1186. static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
  1187. {
  1188. if (vblwait->request.sequence)
  1189. return false;
  1190. return _DRM_VBLANK_RELATIVE ==
  1191. (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
  1192. _DRM_VBLANK_EVENT |
  1193. _DRM_VBLANK_NEXTONMISS));
  1194. }
  1195. int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
  1196. struct drm_file *file_priv)
  1197. {
  1198. struct drm_vblank_crtc *vblank;
  1199. union drm_wait_vblank *vblwait = data;
  1200. int ret;
  1201. unsigned int flags, seq, pipe, high_pipe;
  1202. if (!dev->irq_enabled)
  1203. return -EINVAL;
  1204. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1205. return -EINVAL;
  1206. if (vblwait->request.type &
  1207. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1208. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1209. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1210. vblwait->request.type,
  1211. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1212. _DRM_VBLANK_HIGH_CRTC_MASK));
  1213. return -EINVAL;
  1214. }
  1215. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1216. high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1217. if (high_pipe)
  1218. pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1219. else
  1220. pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1221. if (pipe >= dev->num_crtcs)
  1222. return -EINVAL;
  1223. vblank = &dev->vblank[pipe];
  1224. /* If the counter is currently enabled and accurate, short-circuit
  1225. * queries to return the cached timestamp of the last vblank.
  1226. */
  1227. if (dev->vblank_disable_immediate &&
  1228. drm_wait_vblank_is_query(vblwait) &&
  1229. READ_ONCE(vblank->enabled)) {
  1230. struct timeval now;
  1231. vblwait->reply.sequence =
  1232. drm_vblank_count_and_time(dev, pipe, &now);
  1233. vblwait->reply.tval_sec = now.tv_sec;
  1234. vblwait->reply.tval_usec = now.tv_usec;
  1235. return 0;
  1236. }
  1237. ret = drm_vblank_get(dev, pipe);
  1238. if (ret) {
  1239. DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
  1240. return ret;
  1241. }
  1242. seq = drm_vblank_count(dev, pipe);
  1243. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1244. case _DRM_VBLANK_RELATIVE:
  1245. vblwait->request.sequence += seq;
  1246. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1247. case _DRM_VBLANK_ABSOLUTE:
  1248. break;
  1249. default:
  1250. ret = -EINVAL;
  1251. goto done;
  1252. }
  1253. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1254. vblank_passed(seq, vblwait->request.sequence))
  1255. vblwait->request.sequence = seq + 1;
  1256. if (flags & _DRM_VBLANK_EVENT) {
  1257. /* must hold on to the vblank ref until the event fires
  1258. * drm_vblank_put will be called asynchronously
  1259. */
  1260. return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
  1261. }
  1262. if (vblwait->request.sequence != seq) {
  1263. DRM_DEBUG("waiting on vblank count %u, crtc %u\n",
  1264. vblwait->request.sequence, pipe);
  1265. DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
  1266. vblank_passed(drm_vblank_count(dev, pipe),
  1267. vblwait->request.sequence) ||
  1268. !READ_ONCE(vblank->enabled));
  1269. }
  1270. if (ret != -EINTR) {
  1271. struct timeval now;
  1272. vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1273. vblwait->reply.tval_sec = now.tv_sec;
  1274. vblwait->reply.tval_usec = now.tv_usec;
  1275. DRM_DEBUG("crtc %d returning %u to client\n",
  1276. pipe, vblwait->reply.sequence);
  1277. } else {
  1278. DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
  1279. }
  1280. done:
  1281. drm_vblank_put(dev, pipe);
  1282. return ret;
  1283. }
  1284. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1285. {
  1286. struct drm_pending_vblank_event *e, *t;
  1287. struct timeval now;
  1288. unsigned int seq;
  1289. assert_spin_locked(&dev->event_lock);
  1290. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1291. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1292. if (e->pipe != pipe)
  1293. continue;
  1294. if (!vblank_passed(seq, e->event.sequence))
  1295. continue;
  1296. DRM_DEBUG("vblank event on %u, current %u\n",
  1297. e->event.sequence, seq);
  1298. list_del(&e->base.link);
  1299. drm_vblank_put(dev, pipe);
  1300. send_vblank_event(dev, e, seq, &now);
  1301. }
  1302. trace_drm_vblank_event(pipe, seq);
  1303. }
  1304. /**
  1305. * drm_handle_vblank - handle a vblank event
  1306. * @dev: DRM device
  1307. * @pipe: index of CRTC where this event occurred
  1308. *
  1309. * Drivers should call this routine in their vblank interrupt handlers to
  1310. * update the vblank counter and send any signals that may be pending.
  1311. *
  1312. * This is the legacy version of drm_crtc_handle_vblank().
  1313. */
  1314. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1315. {
  1316. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1317. unsigned long irqflags;
  1318. bool disable_irq;
  1319. if (WARN_ON_ONCE(!dev->num_crtcs))
  1320. return false;
  1321. if (WARN_ON(pipe >= dev->num_crtcs))
  1322. return false;
  1323. spin_lock_irqsave(&dev->event_lock, irqflags);
  1324. /* Need timestamp lock to prevent concurrent execution with
  1325. * vblank enable/disable, as this would cause inconsistent
  1326. * or corrupted timestamps and vblank counts.
  1327. */
  1328. spin_lock(&dev->vblank_time_lock);
  1329. /* Vblank irq handling disabled. Nothing to do. */
  1330. if (!vblank->enabled) {
  1331. spin_unlock(&dev->vblank_time_lock);
  1332. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1333. return false;
  1334. }
  1335. drm_update_vblank_count(dev, pipe, true);
  1336. spin_unlock(&dev->vblank_time_lock);
  1337. wake_up(&vblank->queue);
  1338. /* With instant-off, we defer disabling the interrupt until after
  1339. * we finish processing the following vblank after all events have
  1340. * been signaled. The disable has to be last (after
  1341. * drm_handle_vblank_events) so that the timestamp is always accurate.
  1342. */
  1343. disable_irq = (dev->vblank_disable_immediate &&
  1344. drm_vblank_offdelay > 0 &&
  1345. !atomic_read(&vblank->refcount));
  1346. drm_handle_vblank_events(dev, pipe);
  1347. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1348. if (disable_irq)
  1349. vblank_disable_fn((unsigned long)vblank);
  1350. return true;
  1351. }
  1352. EXPORT_SYMBOL(drm_handle_vblank);
  1353. /**
  1354. * drm_crtc_handle_vblank - handle a vblank event
  1355. * @crtc: where this event occurred
  1356. *
  1357. * Drivers should call this routine in their vblank interrupt handlers to
  1358. * update the vblank counter and send any signals that may be pending.
  1359. *
  1360. * This is the native KMS version of drm_handle_vblank().
  1361. *
  1362. * Returns:
  1363. * True if the event was successfully handled, false on failure.
  1364. */
  1365. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1366. {
  1367. return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1368. }
  1369. EXPORT_SYMBOL(drm_crtc_handle_vblank);