drm_vblank.c 55 KB

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