drm_irq.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  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. /*
  8. * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  9. *
  10. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  11. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  12. * All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a
  15. * copy of this software and associated documentation files (the "Software"),
  16. * to deal in the Software without restriction, including without limitation
  17. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  18. * and/or sell copies of the Software, and to permit persons to whom the
  19. * Software is furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice (including the next
  22. * paragraph) shall be included in all copies or substantial portions of the
  23. * Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  28. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  29. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  30. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  31. * OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. #include <drm/drmP.h>
  34. #include "drm_trace.h"
  35. #include "drm_internal.h"
  36. #include <linux/interrupt.h> /* For task queue support */
  37. #include <linux/slab.h>
  38. #include <linux/vgaarb.h>
  39. #include <linux/export.h>
  40. /* Access macro for slots in vblank timestamp ringbuffer. */
  41. #define vblanktimestamp(dev, pipe, count) \
  42. ((dev)->vblank[pipe].time[(count) % DRM_VBLANKTIME_RBSIZE])
  43. /* Retry timestamp calculation up to 3 times to satisfy
  44. * drm_timestamp_precision before giving up.
  45. */
  46. #define DRM_TIMESTAMP_MAXRETRIES 3
  47. /* Threshold in nanoseconds for detection of redundant
  48. * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  49. */
  50. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  51. static bool
  52. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  53. struct timeval *tvblank, unsigned flags);
  54. static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
  55. /*
  56. * Default to use monotonic timestamps for wait-for-vblank and page-flip
  57. * complete events.
  58. */
  59. unsigned int drm_timestamp_monotonic = 1;
  60. static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
  61. module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
  62. module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
  63. module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
  64. MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
  65. MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
  66. MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
  67. static void store_vblank(struct drm_device *dev, unsigned int pipe,
  68. u32 vblank_count_inc,
  69. struct timeval *t_vblank, u32 last)
  70. {
  71. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  72. u32 tslot;
  73. assert_spin_locked(&dev->vblank_time_lock);
  74. vblank->last = last;
  75. /* All writers hold the spinlock, but readers are serialized by
  76. * the latching of vblank->count below.
  77. */
  78. tslot = vblank->count + vblank_count_inc;
  79. vblanktimestamp(dev, pipe, tslot) = *t_vblank;
  80. /*
  81. * vblank timestamp updates are protected on the write side with
  82. * vblank_time_lock, but on the read side done locklessly using a
  83. * sequence-lock on the vblank counter. Ensure correct ordering using
  84. * memory barrriers. We need the barrier both before and also after the
  85. * counter update to synchronize with the next timestamp write.
  86. * The read-side barriers for this are in drm_vblank_count_and_time.
  87. */
  88. smp_wmb();
  89. vblank->count += vblank_count_inc;
  90. smp_wmb();
  91. }
  92. /**
  93. * drm_reset_vblank_timestamp - reset the last timestamp to the last vblank
  94. * @dev: DRM device
  95. * @pipe: index of CRTC for which to reset the timestamp
  96. *
  97. * Reset the stored timestamp for the current vblank count to correspond
  98. * to the last vblank occurred.
  99. *
  100. * Only to be called from drm_vblank_on().
  101. *
  102. * Note: caller must hold dev->vbl_lock since this reads & writes
  103. * device vblank fields.
  104. */
  105. static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
  106. {
  107. u32 cur_vblank;
  108. bool rc;
  109. struct timeval t_vblank;
  110. int count = DRM_TIMESTAMP_MAXRETRIES;
  111. spin_lock(&dev->vblank_time_lock);
  112. /*
  113. * sample the current counter to avoid random jumps
  114. * when drm_vblank_enable() applies the diff
  115. */
  116. do {
  117. cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
  118. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
  119. } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
  120. /*
  121. * Only reinitialize corresponding vblank timestamp if high-precision query
  122. * available and didn't fail. Otherwise reinitialize delayed at next vblank
  123. * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
  124. */
  125. if (!rc)
  126. t_vblank = (struct timeval) {0, 0};
  127. /*
  128. * +1 to make sure user will never see the same
  129. * vblank counter value before and after a modeset
  130. */
  131. store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
  132. spin_unlock(&dev->vblank_time_lock);
  133. }
  134. /**
  135. * drm_update_vblank_count - update the master vblank counter
  136. * @dev: DRM device
  137. * @pipe: counter to update
  138. *
  139. * Call back into the driver to update the appropriate vblank counter
  140. * (specified by @pipe). Deal with wraparound, if it occurred, and
  141. * update the last read value so we can deal with wraparound on the next
  142. * call if necessary.
  143. *
  144. * Only necessary when going from off->on, to account for frames we
  145. * didn't get an interrupt for.
  146. *
  147. * Note: caller must hold dev->vbl_lock since this reads & writes
  148. * device vblank fields.
  149. */
  150. static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
  151. unsigned long flags)
  152. {
  153. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  154. u32 cur_vblank, diff;
  155. bool rc;
  156. struct timeval t_vblank;
  157. int count = DRM_TIMESTAMP_MAXRETRIES;
  158. int framedur_ns = vblank->framedur_ns;
  159. /*
  160. * Interrupts were disabled prior to this call, so deal with counter
  161. * wrap if needed.
  162. * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
  163. * here if the register is small or we had vblank interrupts off for
  164. * a long time.
  165. *
  166. * We repeat the hardware vblank counter & timestamp query until
  167. * we get consistent results. This to prevent races between gpu
  168. * updating its hardware counter while we are retrieving the
  169. * corresponding vblank timestamp.
  170. */
  171. do {
  172. cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
  173. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
  174. } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
  175. if (dev->max_vblank_count != 0) {
  176. /* trust the hw counter when it's around */
  177. diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
  178. } else if (rc && framedur_ns) {
  179. const struct timeval *t_old;
  180. u64 diff_ns;
  181. t_old = &vblanktimestamp(dev, pipe, vblank->count);
  182. diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
  183. /*
  184. * Figure out how many vblanks we've missed based
  185. * on the difference in the timestamps and the
  186. * frame/field duration.
  187. */
  188. diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
  189. if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
  190. DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
  191. " diff_ns = %lld, framedur_ns = %d)\n",
  192. pipe, (long long) diff_ns, framedur_ns);
  193. } else {
  194. /* some kind of default for drivers w/o accurate vbl timestamping */
  195. diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
  196. }
  197. /*
  198. * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
  199. * interval? If so then vblank irqs keep running and it will likely
  200. * happen that the hardware vblank counter is not trustworthy as it
  201. * might reset at some point in that interval and vblank timestamps
  202. * are not trustworthy either in that interval. Iow. this can result
  203. * in a bogus diff >> 1 which must be avoided as it would cause
  204. * random large forward jumps of the software vblank counter.
  205. */
  206. if (diff > 1 && (vblank->inmodeset & 0x2)) {
  207. DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
  208. " due to pre-modeset.\n", pipe, diff);
  209. diff = 1;
  210. }
  211. /*
  212. * FIMXE: Need to replace this hack with proper seqlocks.
  213. *
  214. * Restrict the bump of the software vblank counter to a safe maximum
  215. * value of +1 whenever there is the possibility that concurrent readers
  216. * of vblank timestamps could be active at the moment, as the current
  217. * implementation of the timestamp caching and updating is not safe
  218. * against concurrent readers for calls to store_vblank() with a bump
  219. * of anything but +1. A bump != 1 would very likely return corrupted
  220. * timestamps to userspace, because the same slot in the cache could
  221. * be concurrently written by store_vblank() and read by one of those
  222. * readers without the read-retry logic detecting the collision.
  223. *
  224. * Concurrent readers can exist when we are called from the
  225. * drm_vblank_off() or drm_vblank_on() functions and other non-vblank-
  226. * irq callers. However, all those calls to us are happening with the
  227. * vbl_lock locked to prevent drm_vblank_get(), so the vblank refcount
  228. * can't increase while we are executing. Therefore a zero refcount at
  229. * this point is safe for arbitrary counter bumps if we are called
  230. * outside vblank irq, a non-zero count is not 100% safe. Unfortunately
  231. * we must also accept a refcount of 1, as whenever we are called from
  232. * drm_vblank_get() -> drm_vblank_enable() the refcount will be 1 and
  233. * we must let that one pass through in order to not lose vblank counts
  234. * during vblank irq off - which would completely defeat the whole
  235. * point of this routine.
  236. *
  237. * Whenever we are called from vblank irq, we have to assume concurrent
  238. * readers exist or can show up any time during our execution, even if
  239. * the refcount is currently zero, as vblank irqs are usually only
  240. * enabled due to the presence of readers, and because when we are called
  241. * from vblank irq we can't hold the vbl_lock to protect us from sudden
  242. * bumps in vblank refcount. Therefore also restrict bumps to +1 when
  243. * called from vblank irq.
  244. */
  245. if ((diff > 1) && (atomic_read(&vblank->refcount) > 1 ||
  246. (flags & DRM_CALLED_FROM_VBLIRQ))) {
  247. DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u "
  248. "refcount %u, vblirq %u\n", pipe, diff,
  249. atomic_read(&vblank->refcount),
  250. (flags & DRM_CALLED_FROM_VBLIRQ) != 0);
  251. diff = 1;
  252. }
  253. DRM_DEBUG_VBL("updating vblank count on crtc %u:"
  254. " current=%u, diff=%u, hw=%u hw_last=%u\n",
  255. pipe, vblank->count, diff, cur_vblank, vblank->last);
  256. if (diff == 0) {
  257. WARN_ON_ONCE(cur_vblank != vblank->last);
  258. return;
  259. }
  260. /*
  261. * Only reinitialize corresponding vblank timestamp if high-precision query
  262. * available and didn't fail, or we were called from the vblank interrupt.
  263. * Otherwise reinitialize delayed at next vblank interrupt and assign 0
  264. * for now, to mark the vblanktimestamp as invalid.
  265. */
  266. if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
  267. t_vblank = (struct timeval) {0, 0};
  268. store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
  269. }
  270. /*
  271. * Disable vblank irq's on crtc, make sure that last vblank count
  272. * of hardware and corresponding consistent software vblank counter
  273. * are preserved, even if there are any spurious vblank irq's after
  274. * disable.
  275. */
  276. static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
  277. {
  278. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  279. unsigned long irqflags;
  280. /* Prevent vblank irq processing while disabling vblank irqs,
  281. * so no updates of timestamps or count can happen after we've
  282. * disabled. Needed to prevent races in case of delayed irq's.
  283. */
  284. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  285. /*
  286. * Only disable vblank interrupts if they're enabled. This avoids
  287. * calling the ->disable_vblank() operation in atomic context with the
  288. * hardware potentially runtime suspended.
  289. */
  290. if (vblank->enabled) {
  291. dev->driver->disable_vblank(dev, pipe);
  292. vblank->enabled = false;
  293. }
  294. /*
  295. * Always update the count and timestamp to maintain the
  296. * appearance that the counter has been ticking all along until
  297. * this time. This makes the count account for the entire time
  298. * between drm_vblank_on() and drm_vblank_off().
  299. */
  300. drm_update_vblank_count(dev, pipe, 0);
  301. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  302. }
  303. static void vblank_disable_fn(unsigned long arg)
  304. {
  305. struct drm_vblank_crtc *vblank = (void *)arg;
  306. struct drm_device *dev = vblank->dev;
  307. unsigned int pipe = vblank->pipe;
  308. unsigned long irqflags;
  309. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  310. if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  311. DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
  312. vblank_disable_and_save(dev, pipe);
  313. }
  314. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  315. }
  316. /**
  317. * drm_vblank_cleanup - cleanup vblank support
  318. * @dev: DRM device
  319. *
  320. * This function cleans up any resources allocated in drm_vblank_init.
  321. */
  322. void drm_vblank_cleanup(struct drm_device *dev)
  323. {
  324. unsigned int pipe;
  325. /* Bail if the driver didn't call drm_vblank_init() */
  326. if (dev->num_crtcs == 0)
  327. return;
  328. for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
  329. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  330. WARN_ON(vblank->enabled &&
  331. drm_core_check_feature(dev, DRIVER_MODESET));
  332. del_timer_sync(&vblank->disable_timer);
  333. }
  334. kfree(dev->vblank);
  335. dev->num_crtcs = 0;
  336. }
  337. EXPORT_SYMBOL(drm_vblank_cleanup);
  338. /**
  339. * drm_vblank_init - initialize vblank support
  340. * @dev: DRM device
  341. * @num_crtcs: number of CRTCs supported by @dev
  342. *
  343. * This function initializes vblank support for @num_crtcs display pipelines.
  344. *
  345. * Returns:
  346. * Zero on success or a negative error code on failure.
  347. */
  348. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
  349. {
  350. int ret = -ENOMEM;
  351. unsigned int i;
  352. spin_lock_init(&dev->vbl_lock);
  353. spin_lock_init(&dev->vblank_time_lock);
  354. dev->num_crtcs = num_crtcs;
  355. dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  356. if (!dev->vblank)
  357. goto err;
  358. for (i = 0; i < num_crtcs; i++) {
  359. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  360. vblank->dev = dev;
  361. vblank->pipe = i;
  362. init_waitqueue_head(&vblank->queue);
  363. setup_timer(&vblank->disable_timer, vblank_disable_fn,
  364. (unsigned long)vblank);
  365. }
  366. DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
  367. /* Driver specific high-precision vblank timestamping supported? */
  368. if (dev->driver->get_vblank_timestamp)
  369. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  370. else
  371. DRM_INFO("No driver support for vblank timestamp query.\n");
  372. /* Must have precise timestamping for reliable vblank instant disable */
  373. if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
  374. dev->vblank_disable_immediate = false;
  375. DRM_INFO("Setting vblank_disable_immediate to false because "
  376. "get_vblank_timestamp == NULL\n");
  377. }
  378. return 0;
  379. err:
  380. dev->num_crtcs = 0;
  381. return ret;
  382. }
  383. EXPORT_SYMBOL(drm_vblank_init);
  384. static void drm_irq_vgaarb_nokms(void *cookie, bool state)
  385. {
  386. struct drm_device *dev = cookie;
  387. if (dev->driver->vgaarb_irq) {
  388. dev->driver->vgaarb_irq(dev, state);
  389. return;
  390. }
  391. if (!dev->irq_enabled)
  392. return;
  393. if (state) {
  394. if (dev->driver->irq_uninstall)
  395. dev->driver->irq_uninstall(dev);
  396. } else {
  397. if (dev->driver->irq_preinstall)
  398. dev->driver->irq_preinstall(dev);
  399. if (dev->driver->irq_postinstall)
  400. dev->driver->irq_postinstall(dev);
  401. }
  402. }
  403. /**
  404. * drm_irq_install - install IRQ handler
  405. * @dev: DRM device
  406. * @irq: IRQ number to install the handler for
  407. *
  408. * Initializes the IRQ related data. Installs the handler, calling the driver
  409. * irq_preinstall() and irq_postinstall() functions before and after the
  410. * installation.
  411. *
  412. * This is the simplified helper interface provided for drivers with no special
  413. * needs. Drivers which need to install interrupt handlers for multiple
  414. * interrupts must instead set drm_device->irq_enabled to signal the DRM core
  415. * that vblank interrupts are available.
  416. *
  417. * Returns:
  418. * Zero on success or a negative error code on failure.
  419. */
  420. int drm_irq_install(struct drm_device *dev, int irq)
  421. {
  422. int ret;
  423. unsigned long sh_flags = 0;
  424. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  425. return -EINVAL;
  426. if (irq == 0)
  427. return -EINVAL;
  428. /* Driver must have been initialized */
  429. if (!dev->dev_private)
  430. return -EINVAL;
  431. if (dev->irq_enabled)
  432. return -EBUSY;
  433. dev->irq_enabled = true;
  434. DRM_DEBUG("irq=%d\n", irq);
  435. /* Before installing handler */
  436. if (dev->driver->irq_preinstall)
  437. dev->driver->irq_preinstall(dev);
  438. /* Install handler */
  439. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  440. sh_flags = IRQF_SHARED;
  441. ret = request_irq(irq, dev->driver->irq_handler,
  442. sh_flags, dev->driver->name, dev);
  443. if (ret < 0) {
  444. dev->irq_enabled = false;
  445. return ret;
  446. }
  447. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  448. vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
  449. /* After installing handler */
  450. if (dev->driver->irq_postinstall)
  451. ret = dev->driver->irq_postinstall(dev);
  452. if (ret < 0) {
  453. dev->irq_enabled = false;
  454. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  455. vga_client_register(dev->pdev, NULL, NULL, NULL);
  456. free_irq(irq, dev);
  457. } else {
  458. dev->irq = irq;
  459. }
  460. return ret;
  461. }
  462. EXPORT_SYMBOL(drm_irq_install);
  463. /**
  464. * drm_irq_uninstall - uninstall the IRQ handler
  465. * @dev: DRM device
  466. *
  467. * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
  468. * This should only be called by drivers which used drm_irq_install() to set up
  469. * their interrupt handler. Other drivers must only reset
  470. * drm_device->irq_enabled to false.
  471. *
  472. * Note that for kernel modesetting drivers it is a bug if this function fails.
  473. * The sanity checks are only to catch buggy user modesetting drivers which call
  474. * the same function through an ioctl.
  475. *
  476. * Returns:
  477. * Zero on success or a negative error code on failure.
  478. */
  479. int drm_irq_uninstall(struct drm_device *dev)
  480. {
  481. unsigned long irqflags;
  482. bool irq_enabled;
  483. int i;
  484. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  485. return -EINVAL;
  486. irq_enabled = dev->irq_enabled;
  487. dev->irq_enabled = false;
  488. /*
  489. * Wake up any waiters so they don't hang. This is just to paper over
  490. * isssues for UMS drivers which aren't in full control of their
  491. * vblank/irq handling. KMS drivers must ensure that vblanks are all
  492. * disabled when uninstalling the irq handler.
  493. */
  494. if (dev->num_crtcs) {
  495. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  496. for (i = 0; i < dev->num_crtcs; i++) {
  497. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  498. if (!vblank->enabled)
  499. continue;
  500. WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
  501. vblank_disable_and_save(dev, i);
  502. wake_up(&vblank->queue);
  503. }
  504. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  505. }
  506. if (!irq_enabled)
  507. return -EINVAL;
  508. DRM_DEBUG("irq=%d\n", dev->irq);
  509. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  510. vga_client_register(dev->pdev, NULL, NULL, NULL);
  511. if (dev->driver->irq_uninstall)
  512. dev->driver->irq_uninstall(dev);
  513. free_irq(dev->irq, dev);
  514. return 0;
  515. }
  516. EXPORT_SYMBOL(drm_irq_uninstall);
  517. /*
  518. * IRQ control ioctl.
  519. *
  520. * \param inode device inode.
  521. * \param file_priv DRM file private.
  522. * \param cmd command.
  523. * \param arg user argument, pointing to a drm_control structure.
  524. * \return zero on success or a negative number on failure.
  525. *
  526. * Calls irq_install() or irq_uninstall() according to \p arg.
  527. */
  528. int drm_control(struct drm_device *dev, void *data,
  529. struct drm_file *file_priv)
  530. {
  531. struct drm_control *ctl = data;
  532. int ret = 0, irq;
  533. /* if we haven't irq we fallback for compatibility reasons -
  534. * this used to be a separate function in drm_dma.h
  535. */
  536. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  537. return 0;
  538. if (drm_core_check_feature(dev, DRIVER_MODESET))
  539. return 0;
  540. /* UMS was only ever support on pci devices. */
  541. if (WARN_ON(!dev->pdev))
  542. return -EINVAL;
  543. switch (ctl->func) {
  544. case DRM_INST_HANDLER:
  545. irq = dev->pdev->irq;
  546. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  547. ctl->irq != irq)
  548. return -EINVAL;
  549. mutex_lock(&dev->struct_mutex);
  550. ret = drm_irq_install(dev, irq);
  551. mutex_unlock(&dev->struct_mutex);
  552. return ret;
  553. case DRM_UNINST_HANDLER:
  554. mutex_lock(&dev->struct_mutex);
  555. ret = drm_irq_uninstall(dev);
  556. mutex_unlock(&dev->struct_mutex);
  557. return ret;
  558. default:
  559. return -EINVAL;
  560. }
  561. }
  562. /**
  563. * drm_calc_timestamping_constants - calculate vblank timestamp constants
  564. * @crtc: drm_crtc whose timestamp constants should be updated.
  565. * @mode: display mode containing the scanout timings
  566. *
  567. * Calculate and store various constants which are later
  568. * needed by vblank and swap-completion timestamping, e.g,
  569. * by drm_calc_vbltimestamp_from_scanoutpos(). They are
  570. * derived from CRTC's true scanout timing, so they take
  571. * things like panel scaling or other adjustments into account.
  572. */
  573. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  574. const struct drm_display_mode *mode)
  575. {
  576. struct drm_device *dev = crtc->dev;
  577. unsigned int pipe = drm_crtc_index(crtc);
  578. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  579. int linedur_ns = 0, framedur_ns = 0;
  580. int dotclock = mode->crtc_clock;
  581. if (!dev->num_crtcs)
  582. return;
  583. if (WARN_ON(pipe >= dev->num_crtcs))
  584. return;
  585. /* Valid dotclock? */
  586. if (dotclock > 0) {
  587. int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
  588. /*
  589. * Convert scanline length in pixels and video
  590. * dot clock to line duration and frame duration
  591. * in nanoseconds:
  592. */
  593. linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
  594. framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
  595. /*
  596. * Fields of interlaced scanout modes are only half a frame duration.
  597. */
  598. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  599. framedur_ns /= 2;
  600. } else
  601. DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
  602. crtc->base.id);
  603. vblank->linedur_ns = linedur_ns;
  604. vblank->framedur_ns = framedur_ns;
  605. DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  606. crtc->base.id, mode->crtc_htotal,
  607. mode->crtc_vtotal, mode->crtc_vdisplay);
  608. DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
  609. crtc->base.id, dotclock, framedur_ns, linedur_ns);
  610. }
  611. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  612. /**
  613. * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  614. * @dev: DRM device
  615. * @pipe: index of CRTC whose vblank timestamp to retrieve
  616. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  617. * On return contains true maximum error of timestamp
  618. * @vblank_time: Pointer to struct timeval which should receive the timestamp
  619. * @flags: Flags to pass to driver:
  620. * 0 = Default,
  621. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  622. * @mode: mode which defines the scanout timings
  623. *
  624. * Implements calculation of exact vblank timestamps from given drm_display_mode
  625. * timings and current video scanout position of a CRTC. This can be called from
  626. * within get_vblank_timestamp() implementation of a kms driver to implement the
  627. * actual timestamping.
  628. *
  629. * Should return timestamps conforming to the OML_sync_control OpenML
  630. * extension specification. The timestamp corresponds to the end of
  631. * the vblank interval, aka start of scanout of topmost-leftmost display
  632. * pixel in the following video frame.
  633. *
  634. * Requires support for optional dev->driver->get_scanout_position()
  635. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  636. * that corresponds to the true scanout timing.
  637. *
  638. * The current implementation only handles standard video modes. It
  639. * returns as no operation if a doublescan or interlaced video mode is
  640. * active. Higher level code is expected to handle this.
  641. *
  642. * Returns:
  643. * Negative value on error, failure or if not supported in current
  644. * video mode:
  645. *
  646. * -EINVAL - Invalid CRTC.
  647. * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
  648. * -ENOTSUPP - Function not supported in current display mode.
  649. * -EIO - Failed, e.g., due to failed scanout position query.
  650. *
  651. * Returns or'ed positive status flags on success:
  652. *
  653. * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  654. * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  655. *
  656. */
  657. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  658. unsigned int pipe,
  659. int *max_error,
  660. struct timeval *vblank_time,
  661. unsigned flags,
  662. const struct drm_display_mode *mode)
  663. {
  664. struct timeval tv_etime;
  665. ktime_t stime, etime;
  666. unsigned int vbl_status;
  667. int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  668. int vpos, hpos, i;
  669. int delta_ns, duration_ns;
  670. if (pipe >= dev->num_crtcs) {
  671. DRM_ERROR("Invalid crtc %u\n", pipe);
  672. return -EINVAL;
  673. }
  674. /* Scanout position query not supported? Should not happen. */
  675. if (!dev->driver->get_scanout_position) {
  676. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  677. return -EIO;
  678. }
  679. /* If mode timing undefined, just return as no-op:
  680. * Happens during initial modesetting of a crtc.
  681. */
  682. if (mode->crtc_clock == 0) {
  683. DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
  684. return -EAGAIN;
  685. }
  686. /* Get current scanout position with system timestamp.
  687. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  688. * if single query takes longer than max_error nanoseconds.
  689. *
  690. * This guarantees a tight bound on maximum error if
  691. * code gets preempted or delayed for some reason.
  692. */
  693. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  694. /*
  695. * Get vertical and horizontal scanout position vpos, hpos,
  696. * and bounding timestamps stime, etime, pre/post query.
  697. */
  698. vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
  699. &vpos, &hpos,
  700. &stime, &etime,
  701. mode);
  702. /* Return as no-op if scanout query unsupported or failed. */
  703. if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  704. DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
  705. pipe, vbl_status);
  706. return -EIO;
  707. }
  708. /* Compute uncertainty in timestamp of scanout position query. */
  709. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  710. /* Accept result with < max_error nsecs timing uncertainty. */
  711. if (duration_ns <= *max_error)
  712. break;
  713. }
  714. /* Noisy system timing? */
  715. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  716. DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
  717. pipe, duration_ns/1000, *max_error/1000, i);
  718. }
  719. /* Return upper bound of timestamp precision error. */
  720. *max_error = duration_ns;
  721. /* Check if in vblank area:
  722. * vpos is >=0 in video scanout area, but negative
  723. * within vblank area, counting down the number of lines until
  724. * start of scanout.
  725. */
  726. if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
  727. ret |= DRM_VBLANKTIME_IN_VBLANK;
  728. /* Convert scanout position into elapsed time at raw_time query
  729. * since start of scanout at first display scanline. delta_ns
  730. * can be negative if start of scanout hasn't happened yet.
  731. */
  732. delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  733. mode->crtc_clock);
  734. if (!drm_timestamp_monotonic)
  735. etime = ktime_mono_to_real(etime);
  736. /* save this only for debugging purposes */
  737. tv_etime = ktime_to_timeval(etime);
  738. /* Subtract time delta from raw timestamp to get final
  739. * vblank_time timestamp for end of vblank.
  740. */
  741. etime = ktime_sub_ns(etime, delta_ns);
  742. *vblank_time = ktime_to_timeval(etime);
  743. DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  744. pipe, vbl_status, hpos, vpos,
  745. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  746. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  747. duration_ns/1000, i);
  748. return ret;
  749. }
  750. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  751. static struct timeval get_drm_timestamp(void)
  752. {
  753. ktime_t now;
  754. now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
  755. return ktime_to_timeval(now);
  756. }
  757. /**
  758. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  759. * vblank interval
  760. * @dev: DRM device
  761. * @pipe: index of CRTC whose vblank timestamp to retrieve
  762. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  763. * @flags: Flags to pass to driver:
  764. * 0 = Default,
  765. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  766. *
  767. * Fetches the system timestamp corresponding to the time of the most recent
  768. * vblank interval on specified CRTC. May call into kms-driver to
  769. * compute the timestamp with a high-precision GPU specific method.
  770. *
  771. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  772. * call, i.e., it isn't very precisely locked to the true vblank.
  773. *
  774. * Returns:
  775. * True if timestamp is considered to be very precise, false otherwise.
  776. */
  777. static bool
  778. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  779. struct timeval *tvblank, unsigned flags)
  780. {
  781. int ret;
  782. /* Define requested maximum error on timestamps (nanoseconds). */
  783. int max_error = (int) drm_timestamp_precision * 1000;
  784. /* Query driver if possible and precision timestamping enabled. */
  785. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  786. ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
  787. tvblank, flags);
  788. if (ret > 0)
  789. return true;
  790. }
  791. /* GPU high precision timestamp query unsupported or failed.
  792. * Return current monotonic/gettimeofday timestamp as best estimate.
  793. */
  794. *tvblank = get_drm_timestamp();
  795. return false;
  796. }
  797. /**
  798. * drm_vblank_count - retrieve "cooked" vblank counter value
  799. * @dev: DRM device
  800. * @pipe: index of CRTC for which to retrieve the counter
  801. *
  802. * Fetches the "cooked" vblank count value that represents the number of
  803. * vblank events since the system was booted, including lost events due to
  804. * modesetting activity.
  805. *
  806. * This is the legacy version of drm_crtc_vblank_count().
  807. *
  808. * Returns:
  809. * The software vblank counter.
  810. */
  811. u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
  812. {
  813. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  814. if (WARN_ON(pipe >= dev->num_crtcs))
  815. return 0;
  816. return vblank->count;
  817. }
  818. EXPORT_SYMBOL(drm_vblank_count);
  819. /**
  820. * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  821. * @crtc: which counter to retrieve
  822. *
  823. * Fetches the "cooked" vblank count value that represents the number of
  824. * vblank events since the system was booted, including lost events due to
  825. * modesetting activity.
  826. *
  827. * This is the native KMS version of drm_vblank_count().
  828. *
  829. * Returns:
  830. * The software vblank counter.
  831. */
  832. u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
  833. {
  834. return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  835. }
  836. EXPORT_SYMBOL(drm_crtc_vblank_count);
  837. /**
  838. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
  839. * system timestamp corresponding to that vblank counter value.
  840. * @dev: DRM device
  841. * @pipe: index of CRTC whose counter to retrieve
  842. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  843. *
  844. * Fetches the "cooked" vblank count value that represents the number of
  845. * vblank events since the system was booted, including lost events due to
  846. * modesetting activity. Returns corresponding system timestamp of the time
  847. * of the vblank interval that corresponds to the current vblank counter value.
  848. *
  849. * This is the legacy version of drm_crtc_vblank_count_and_time().
  850. */
  851. u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  852. struct timeval *vblanktime)
  853. {
  854. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  855. int count = DRM_TIMESTAMP_MAXRETRIES;
  856. u32 cur_vblank;
  857. if (WARN_ON(pipe >= dev->num_crtcs))
  858. return 0;
  859. /*
  860. * Vblank timestamps are read lockless. To ensure consistency the vblank
  861. * counter is rechecked and ordering is ensured using memory barriers.
  862. * This works like a seqlock. The write-side barriers are in store_vblank.
  863. */
  864. do {
  865. cur_vblank = vblank->count;
  866. smp_rmb();
  867. *vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
  868. smp_rmb();
  869. } while (cur_vblank != vblank->count && --count > 0);
  870. return cur_vblank;
  871. }
  872. EXPORT_SYMBOL(drm_vblank_count_and_time);
  873. /**
  874. * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  875. * and the system timestamp corresponding to that vblank counter value
  876. * @crtc: which counter to retrieve
  877. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  878. *
  879. * Fetches the "cooked" vblank count value that represents the number of
  880. * vblank events since the system was booted, including lost events due to
  881. * modesetting activity. Returns corresponding system timestamp of the time
  882. * of the vblank interval that corresponds to the current vblank counter value.
  883. *
  884. * This is the native KMS version of drm_vblank_count_and_time().
  885. */
  886. u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  887. struct timeval *vblanktime)
  888. {
  889. return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  890. vblanktime);
  891. }
  892. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  893. static void send_vblank_event(struct drm_device *dev,
  894. struct drm_pending_vblank_event *e,
  895. unsigned long seq, struct timeval *now)
  896. {
  897. e->event.sequence = seq;
  898. e->event.tv_sec = now->tv_sec;
  899. e->event.tv_usec = now->tv_usec;
  900. drm_send_event_locked(dev, &e->base);
  901. trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
  902. e->event.sequence);
  903. }
  904. /**
  905. * drm_arm_vblank_event - arm vblank event after pageflip
  906. * @dev: DRM device
  907. * @pipe: CRTC index
  908. * @e: the event to prepare to send
  909. *
  910. * A lot of drivers need to generate vblank events for the very next vblank
  911. * interrupt. For example when the page flip interrupt happens when the page
  912. * flip gets armed, but not when it actually executes within the next vblank
  913. * period. This helper function implements exactly the required vblank arming
  914. * behaviour.
  915. *
  916. * Caller must hold event lock. Caller must also hold a vblank reference for
  917. * the event @e, which will be dropped when the next vblank arrives.
  918. *
  919. * This is the legacy version of drm_crtc_arm_vblank_event().
  920. */
  921. void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
  922. struct drm_pending_vblank_event *e)
  923. {
  924. assert_spin_locked(&dev->event_lock);
  925. e->pipe = pipe;
  926. e->event.sequence = drm_vblank_count(dev, pipe);
  927. list_add_tail(&e->base.link, &dev->vblank_event_list);
  928. }
  929. EXPORT_SYMBOL(drm_arm_vblank_event);
  930. /**
  931. * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  932. * @crtc: the source CRTC of the vblank event
  933. * @e: the event to send
  934. *
  935. * A lot of drivers need to generate vblank events for the very next vblank
  936. * interrupt. For example when the page flip interrupt happens when the page
  937. * flip gets armed, but not when it actually executes within the next vblank
  938. * period. This helper function implements exactly the required vblank arming
  939. * behaviour.
  940. *
  941. * Caller must hold event lock. Caller must also hold a vblank reference for
  942. * the event @e, which will be dropped when the next vblank arrives.
  943. *
  944. * This is the native KMS version of drm_arm_vblank_event().
  945. */
  946. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  947. struct drm_pending_vblank_event *e)
  948. {
  949. drm_arm_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
  950. }
  951. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  952. /**
  953. * drm_send_vblank_event - helper to send vblank event after pageflip
  954. * @dev: DRM device
  955. * @pipe: CRTC index
  956. * @e: the event to send
  957. *
  958. * Updates sequence # and timestamp on event, and sends it to userspace.
  959. * Caller must hold event lock.
  960. *
  961. * This is the legacy version of drm_crtc_send_vblank_event().
  962. */
  963. void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
  964. struct drm_pending_vblank_event *e)
  965. {
  966. struct timeval now;
  967. unsigned int seq;
  968. if (dev->num_crtcs > 0) {
  969. seq = drm_vblank_count_and_time(dev, pipe, &now);
  970. } else {
  971. seq = 0;
  972. now = get_drm_timestamp();
  973. }
  974. e->pipe = pipe;
  975. send_vblank_event(dev, e, seq, &now);
  976. }
  977. EXPORT_SYMBOL(drm_send_vblank_event);
  978. /**
  979. * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  980. * @crtc: the source CRTC of the vblank event
  981. * @e: the event to send
  982. *
  983. * Updates sequence # and timestamp on event, and sends it to userspace.
  984. * Caller must hold event lock.
  985. *
  986. * This is the native KMS version of drm_send_vblank_event().
  987. */
  988. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  989. struct drm_pending_vblank_event *e)
  990. {
  991. drm_send_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
  992. }
  993. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  994. /**
  995. * drm_vblank_enable - enable the vblank interrupt on a CRTC
  996. * @dev: DRM device
  997. * @pipe: CRTC index
  998. *
  999. * Returns:
  1000. * Zero on success or a negative error code on failure.
  1001. */
  1002. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  1003. {
  1004. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1005. int ret = 0;
  1006. assert_spin_locked(&dev->vbl_lock);
  1007. spin_lock(&dev->vblank_time_lock);
  1008. if (!vblank->enabled) {
  1009. /*
  1010. * Enable vblank irqs under vblank_time_lock protection.
  1011. * All vblank count & timestamp updates are held off
  1012. * until we are done reinitializing master counter and
  1013. * timestamps. Filtercode in drm_handle_vblank() will
  1014. * prevent double-accounting of same vblank interval.
  1015. */
  1016. ret = dev->driver->enable_vblank(dev, pipe);
  1017. DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
  1018. if (ret)
  1019. atomic_dec(&vblank->refcount);
  1020. else {
  1021. vblank->enabled = true;
  1022. drm_update_vblank_count(dev, pipe, 0);
  1023. }
  1024. }
  1025. spin_unlock(&dev->vblank_time_lock);
  1026. return ret;
  1027. }
  1028. /**
  1029. * drm_vblank_get - get a reference count on vblank events
  1030. * @dev: DRM device
  1031. * @pipe: index of CRTC to own
  1032. *
  1033. * Acquire a reference count on vblank events to avoid having them disabled
  1034. * while in use.
  1035. *
  1036. * This is the legacy version of drm_crtc_vblank_get().
  1037. *
  1038. * Returns:
  1039. * Zero on success or a negative error code on failure.
  1040. */
  1041. int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  1042. {
  1043. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1044. unsigned long irqflags;
  1045. int ret = 0;
  1046. if (!dev->num_crtcs)
  1047. return -EINVAL;
  1048. if (WARN_ON(pipe >= dev->num_crtcs))
  1049. return -EINVAL;
  1050. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1051. /* Going from 0->1 means we have to enable interrupts again */
  1052. if (atomic_add_return(1, &vblank->refcount) == 1) {
  1053. ret = drm_vblank_enable(dev, pipe);
  1054. } else {
  1055. if (!vblank->enabled) {
  1056. atomic_dec(&vblank->refcount);
  1057. ret = -EINVAL;
  1058. }
  1059. }
  1060. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1061. return ret;
  1062. }
  1063. EXPORT_SYMBOL(drm_vblank_get);
  1064. /**
  1065. * drm_crtc_vblank_get - get a reference count on vblank events
  1066. * @crtc: which CRTC to own
  1067. *
  1068. * Acquire a reference count on vblank events to avoid having them disabled
  1069. * while in use.
  1070. *
  1071. * This is the native kms version of drm_vblank_get().
  1072. *
  1073. * Returns:
  1074. * Zero on success or a negative error code on failure.
  1075. */
  1076. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  1077. {
  1078. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  1079. }
  1080. EXPORT_SYMBOL(drm_crtc_vblank_get);
  1081. /**
  1082. * drm_vblank_put - release ownership of vblank events
  1083. * @dev: DRM device
  1084. * @pipe: index of CRTC to release
  1085. *
  1086. * Release ownership of a given vblank counter, turning off interrupts
  1087. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1088. *
  1089. * This is the legacy version of drm_crtc_vblank_put().
  1090. */
  1091. void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  1092. {
  1093. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1094. if (WARN_ON(pipe >= dev->num_crtcs))
  1095. return;
  1096. if (WARN_ON(atomic_read(&vblank->refcount) == 0))
  1097. return;
  1098. /* Last user schedules interrupt disable */
  1099. if (atomic_dec_and_test(&vblank->refcount)) {
  1100. if (drm_vblank_offdelay == 0)
  1101. return;
  1102. else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
  1103. vblank_disable_fn((unsigned long)vblank);
  1104. else
  1105. mod_timer(&vblank->disable_timer,
  1106. jiffies + ((drm_vblank_offdelay * HZ)/1000));
  1107. }
  1108. }
  1109. EXPORT_SYMBOL(drm_vblank_put);
  1110. /**
  1111. * drm_crtc_vblank_put - give up ownership of vblank events
  1112. * @crtc: which counter to give up
  1113. *
  1114. * Release ownership of a given vblank counter, turning off interrupts
  1115. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1116. *
  1117. * This is the native kms version of drm_vblank_put().
  1118. */
  1119. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  1120. {
  1121. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  1122. }
  1123. EXPORT_SYMBOL(drm_crtc_vblank_put);
  1124. /**
  1125. * drm_wait_one_vblank - wait for one vblank
  1126. * @dev: DRM device
  1127. * @pipe: CRTC index
  1128. *
  1129. * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
  1130. * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  1131. * due to lack of driver support or because the crtc is off.
  1132. */
  1133. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
  1134. {
  1135. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1136. int ret;
  1137. u32 last;
  1138. if (WARN_ON(pipe >= dev->num_crtcs))
  1139. return;
  1140. ret = drm_vblank_get(dev, pipe);
  1141. if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
  1142. return;
  1143. last = drm_vblank_count(dev, pipe);
  1144. ret = wait_event_timeout(vblank->queue,
  1145. last != drm_vblank_count(dev, pipe),
  1146. msecs_to_jiffies(100));
  1147. WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  1148. drm_vblank_put(dev, pipe);
  1149. }
  1150. EXPORT_SYMBOL(drm_wait_one_vblank);
  1151. /**
  1152. * drm_crtc_wait_one_vblank - wait for one vblank
  1153. * @crtc: DRM crtc
  1154. *
  1155. * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  1156. * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  1157. * due to lack of driver support or because the crtc is off.
  1158. */
  1159. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  1160. {
  1161. drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
  1162. }
  1163. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  1164. /**
  1165. * drm_vblank_off - disable vblank events on a CRTC
  1166. * @dev: DRM device
  1167. * @pipe: CRTC index
  1168. *
  1169. * Drivers can use this function to shut down the vblank interrupt handling when
  1170. * disabling a crtc. This function ensures that the latest vblank frame count is
  1171. * stored so that drm_vblank_on() can restore it again.
  1172. *
  1173. * Drivers must use this function when the hardware vblank counter can get
  1174. * reset, e.g. when suspending.
  1175. *
  1176. * This is the legacy version of drm_crtc_vblank_off().
  1177. */
  1178. void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
  1179. {
  1180. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1181. struct drm_pending_vblank_event *e, *t;
  1182. struct timeval now;
  1183. unsigned long irqflags;
  1184. unsigned int seq;
  1185. if (WARN_ON(pipe >= dev->num_crtcs))
  1186. return;
  1187. spin_lock_irqsave(&dev->event_lock, irqflags);
  1188. spin_lock(&dev->vbl_lock);
  1189. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1190. pipe, vblank->enabled, vblank->inmodeset);
  1191. /* Avoid redundant vblank disables without previous drm_vblank_on(). */
  1192. if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
  1193. vblank_disable_and_save(dev, pipe);
  1194. wake_up(&vblank->queue);
  1195. /*
  1196. * Prevent subsequent drm_vblank_get() from re-enabling
  1197. * the vblank interrupt by bumping the refcount.
  1198. */
  1199. if (!vblank->inmodeset) {
  1200. atomic_inc(&vblank->refcount);
  1201. vblank->inmodeset = 1;
  1202. }
  1203. spin_unlock(&dev->vbl_lock);
  1204. /* Send any queued vblank events, lest the natives grow disquiet */
  1205. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1206. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1207. if (e->pipe != pipe)
  1208. continue;
  1209. DRM_DEBUG("Sending premature vblank event on disable: "
  1210. "wanted %d, current %d\n",
  1211. e->event.sequence, seq);
  1212. list_del(&e->base.link);
  1213. drm_vblank_put(dev, pipe);
  1214. send_vblank_event(dev, e, seq, &now);
  1215. }
  1216. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1217. }
  1218. EXPORT_SYMBOL(drm_vblank_off);
  1219. /**
  1220. * drm_crtc_vblank_off - disable vblank events on a CRTC
  1221. * @crtc: CRTC in question
  1222. *
  1223. * Drivers can use this function to shut down the vblank interrupt handling when
  1224. * disabling a crtc. This function ensures that the latest vblank frame count is
  1225. * stored so that drm_vblank_on can restore it again.
  1226. *
  1227. * Drivers must use this function when the hardware vblank counter can get
  1228. * reset, e.g. when suspending.
  1229. *
  1230. * This is the native kms version of drm_vblank_off().
  1231. */
  1232. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  1233. {
  1234. drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
  1235. }
  1236. EXPORT_SYMBOL(drm_crtc_vblank_off);
  1237. /**
  1238. * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  1239. * @crtc: CRTC in question
  1240. *
  1241. * Drivers can use this function to reset the vblank state to off at load time.
  1242. * Drivers should use this together with the drm_crtc_vblank_off() and
  1243. * drm_crtc_vblank_on() functions. The difference compared to
  1244. * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  1245. * and hence doesn't need to call any driver hooks.
  1246. */
  1247. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1248. {
  1249. struct drm_device *dev = crtc->dev;
  1250. unsigned long irqflags;
  1251. unsigned int pipe = drm_crtc_index(crtc);
  1252. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1253. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1254. /*
  1255. * Prevent subsequent drm_vblank_get() from enabling the vblank
  1256. * interrupt by bumping the refcount.
  1257. */
  1258. if (!vblank->inmodeset) {
  1259. atomic_inc(&vblank->refcount);
  1260. vblank->inmodeset = 1;
  1261. }
  1262. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1263. WARN_ON(!list_empty(&dev->vblank_event_list));
  1264. }
  1265. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1266. /**
  1267. * drm_vblank_on - enable vblank events on a CRTC
  1268. * @dev: DRM device
  1269. * @pipe: CRTC index
  1270. *
  1271. * This functions restores the vblank interrupt state captured with
  1272. * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  1273. * drm_vblank_off() can be unbalanced and so can also be unconditionally called
  1274. * in driver load code to reflect the current hardware state of the crtc.
  1275. *
  1276. * This is the legacy version of drm_crtc_vblank_on().
  1277. */
  1278. void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
  1279. {
  1280. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1281. unsigned long irqflags;
  1282. if (WARN_ON(pipe >= dev->num_crtcs))
  1283. return;
  1284. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1285. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1286. pipe, vblank->enabled, vblank->inmodeset);
  1287. /* Drop our private "prevent drm_vblank_get" refcount */
  1288. if (vblank->inmodeset) {
  1289. atomic_dec(&vblank->refcount);
  1290. vblank->inmodeset = 0;
  1291. }
  1292. drm_reset_vblank_timestamp(dev, pipe);
  1293. /*
  1294. * re-enable interrupts if there are users left, or the
  1295. * user wishes vblank interrupts to be enabled all the time.
  1296. */
  1297. if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
  1298. WARN_ON(drm_vblank_enable(dev, pipe));
  1299. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1300. }
  1301. EXPORT_SYMBOL(drm_vblank_on);
  1302. /**
  1303. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1304. * @crtc: CRTC in question
  1305. *
  1306. * This functions restores the vblank interrupt state captured with
  1307. * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  1308. * drm_vblank_off() can be unbalanced and so can also be unconditionally called
  1309. * in driver load code to reflect the current hardware state of the crtc.
  1310. *
  1311. * This is the native kms version of drm_vblank_on().
  1312. */
  1313. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1314. {
  1315. drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
  1316. }
  1317. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1318. /**
  1319. * drm_vblank_pre_modeset - account for vblanks across mode sets
  1320. * @dev: DRM device
  1321. * @pipe: CRTC index
  1322. *
  1323. * Account for vblank events across mode setting events, which will likely
  1324. * reset the hardware frame counter.
  1325. *
  1326. * This is done by grabbing a temporary vblank reference to ensure that the
  1327. * vblank interrupt keeps running across the modeset sequence. With this the
  1328. * software-side vblank frame counting will ensure that there are no jumps or
  1329. * discontinuities.
  1330. *
  1331. * Unfortunately this approach is racy and also doesn't work when the vblank
  1332. * interrupt stops running, e.g. across system suspend resume. It is therefore
  1333. * highly recommended that drivers use the newer drm_vblank_off() and
  1334. * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
  1335. * using "cooked" software vblank frame counters and not relying on any hardware
  1336. * counters.
  1337. *
  1338. * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
  1339. * again.
  1340. */
  1341. void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
  1342. {
  1343. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1344. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1345. if (!dev->num_crtcs)
  1346. return;
  1347. if (WARN_ON(pipe >= dev->num_crtcs))
  1348. return;
  1349. /*
  1350. * To avoid all the problems that might happen if interrupts
  1351. * were enabled/disabled around or between these calls, we just
  1352. * have the kernel take a reference on the CRTC (just once though
  1353. * to avoid corrupting the count if multiple, mismatch calls occur),
  1354. * so that interrupts remain enabled in the interim.
  1355. */
  1356. if (!vblank->inmodeset) {
  1357. vblank->inmodeset = 0x1;
  1358. if (drm_vblank_get(dev, pipe) == 0)
  1359. vblank->inmodeset |= 0x2;
  1360. }
  1361. }
  1362. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  1363. /**
  1364. * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
  1365. * @dev: DRM device
  1366. * @pipe: CRTC index
  1367. *
  1368. * This function again drops the temporary vblank reference acquired in
  1369. * drm_vblank_pre_modeset.
  1370. */
  1371. void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
  1372. {
  1373. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1374. unsigned long irqflags;
  1375. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1376. if (!dev->num_crtcs)
  1377. return;
  1378. if (WARN_ON(pipe >= dev->num_crtcs))
  1379. return;
  1380. if (vblank->inmodeset) {
  1381. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1382. drm_reset_vblank_timestamp(dev, pipe);
  1383. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1384. if (vblank->inmodeset & 0x2)
  1385. drm_vblank_put(dev, pipe);
  1386. vblank->inmodeset = 0;
  1387. }
  1388. }
  1389. EXPORT_SYMBOL(drm_vblank_post_modeset);
  1390. /*
  1391. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  1392. * @DRM_IOCTL_ARGS: standard ioctl arguments
  1393. *
  1394. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  1395. * ioctls around modesetting so that any lost vblank events are accounted for.
  1396. *
  1397. * Generally the counter will reset across mode sets. If interrupts are
  1398. * enabled around this call, we don't have to do anything since the counter
  1399. * will have already been incremented.
  1400. */
  1401. int drm_modeset_ctl(struct drm_device *dev, void *data,
  1402. struct drm_file *file_priv)
  1403. {
  1404. struct drm_modeset_ctl *modeset = data;
  1405. unsigned int pipe;
  1406. /* If drm_vblank_init() hasn't been called yet, just no-op */
  1407. if (!dev->num_crtcs)
  1408. return 0;
  1409. /* KMS drivers handle this internally */
  1410. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1411. return 0;
  1412. pipe = modeset->crtc;
  1413. if (pipe >= dev->num_crtcs)
  1414. return -EINVAL;
  1415. switch (modeset->cmd) {
  1416. case _DRM_PRE_MODESET:
  1417. drm_vblank_pre_modeset(dev, pipe);
  1418. break;
  1419. case _DRM_POST_MODESET:
  1420. drm_vblank_post_modeset(dev, pipe);
  1421. break;
  1422. default:
  1423. return -EINVAL;
  1424. }
  1425. return 0;
  1426. }
  1427. static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
  1428. union drm_wait_vblank *vblwait,
  1429. struct drm_file *file_priv)
  1430. {
  1431. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1432. struct drm_pending_vblank_event *e;
  1433. struct timeval now;
  1434. unsigned long flags;
  1435. unsigned int seq;
  1436. int ret;
  1437. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1438. if (e == NULL) {
  1439. ret = -ENOMEM;
  1440. goto err_put;
  1441. }
  1442. e->pipe = pipe;
  1443. e->base.pid = current->pid;
  1444. e->event.base.type = DRM_EVENT_VBLANK;
  1445. e->event.base.length = sizeof(e->event);
  1446. e->event.user_data = vblwait->request.signal;
  1447. spin_lock_irqsave(&dev->event_lock, flags);
  1448. /*
  1449. * drm_vblank_off() might have been called after we called
  1450. * drm_vblank_get(). drm_vblank_off() holds event_lock
  1451. * around the vblank disable, so no need for further locking.
  1452. * The reference from drm_vblank_get() protects against
  1453. * vblank disable from another source.
  1454. */
  1455. if (!vblank->enabled) {
  1456. ret = -EINVAL;
  1457. goto err_unlock;
  1458. }
  1459. ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
  1460. &e->event.base);
  1461. if (ret)
  1462. goto err_unlock;
  1463. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1464. if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
  1465. (seq - vblwait->request.sequence) <= (1 << 23)) {
  1466. vblwait->request.sequence = seq + 1;
  1467. vblwait->reply.sequence = vblwait->request.sequence;
  1468. }
  1469. DRM_DEBUG("event on vblank count %d, current %d, crtc %u\n",
  1470. vblwait->request.sequence, seq, pipe);
  1471. trace_drm_vblank_event_queued(current->pid, pipe,
  1472. vblwait->request.sequence);
  1473. e->event.sequence = vblwait->request.sequence;
  1474. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  1475. drm_vblank_put(dev, pipe);
  1476. send_vblank_event(dev, e, seq, &now);
  1477. vblwait->reply.sequence = seq;
  1478. } else {
  1479. /* drm_handle_vblank_events will call drm_vblank_put */
  1480. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1481. vblwait->reply.sequence = vblwait->request.sequence;
  1482. }
  1483. spin_unlock_irqrestore(&dev->event_lock, flags);
  1484. return 0;
  1485. err_unlock:
  1486. spin_unlock_irqrestore(&dev->event_lock, flags);
  1487. kfree(e);
  1488. err_put:
  1489. drm_vblank_put(dev, pipe);
  1490. return ret;
  1491. }
  1492. /*
  1493. * Wait for VBLANK.
  1494. *
  1495. * \param inode device inode.
  1496. * \param file_priv DRM file private.
  1497. * \param cmd command.
  1498. * \param data user argument, pointing to a drm_wait_vblank structure.
  1499. * \return zero on success or a negative number on failure.
  1500. *
  1501. * This function enables the vblank interrupt on the pipe requested, then
  1502. * sleeps waiting for the requested sequence number to occur, and drops
  1503. * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
  1504. * after a timeout with no further vblank waits scheduled).
  1505. */
  1506. int drm_wait_vblank(struct drm_device *dev, void *data,
  1507. struct drm_file *file_priv)
  1508. {
  1509. struct drm_vblank_crtc *vblank;
  1510. union drm_wait_vblank *vblwait = data;
  1511. int ret;
  1512. unsigned int flags, seq, pipe, high_pipe;
  1513. if (!dev->irq_enabled)
  1514. return -EINVAL;
  1515. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1516. return -EINVAL;
  1517. if (vblwait->request.type &
  1518. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1519. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1520. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1521. vblwait->request.type,
  1522. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1523. _DRM_VBLANK_HIGH_CRTC_MASK));
  1524. return -EINVAL;
  1525. }
  1526. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1527. high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1528. if (high_pipe)
  1529. pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1530. else
  1531. pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1532. if (pipe >= dev->num_crtcs)
  1533. return -EINVAL;
  1534. vblank = &dev->vblank[pipe];
  1535. ret = drm_vblank_get(dev, pipe);
  1536. if (ret) {
  1537. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  1538. return ret;
  1539. }
  1540. seq = drm_vblank_count(dev, pipe);
  1541. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1542. case _DRM_VBLANK_RELATIVE:
  1543. vblwait->request.sequence += seq;
  1544. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1545. case _DRM_VBLANK_ABSOLUTE:
  1546. break;
  1547. default:
  1548. ret = -EINVAL;
  1549. goto done;
  1550. }
  1551. if (flags & _DRM_VBLANK_EVENT) {
  1552. /* must hold on to the vblank ref until the event fires
  1553. * drm_vblank_put will be called asynchronously
  1554. */
  1555. return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
  1556. }
  1557. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1558. (seq - vblwait->request.sequence) <= (1<<23)) {
  1559. vblwait->request.sequence = seq + 1;
  1560. }
  1561. DRM_DEBUG("waiting on vblank count %d, crtc %u\n",
  1562. vblwait->request.sequence, pipe);
  1563. vblank->last_wait = vblwait->request.sequence;
  1564. DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
  1565. (((drm_vblank_count(dev, pipe) -
  1566. vblwait->request.sequence) <= (1 << 23)) ||
  1567. !vblank->enabled ||
  1568. !dev->irq_enabled));
  1569. if (ret != -EINTR) {
  1570. struct timeval now;
  1571. vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1572. vblwait->reply.tval_sec = now.tv_sec;
  1573. vblwait->reply.tval_usec = now.tv_usec;
  1574. DRM_DEBUG("returning %d to client\n",
  1575. vblwait->reply.sequence);
  1576. } else {
  1577. DRM_DEBUG("vblank wait interrupted by signal\n");
  1578. }
  1579. done:
  1580. drm_vblank_put(dev, pipe);
  1581. return ret;
  1582. }
  1583. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1584. {
  1585. struct drm_pending_vblank_event *e, *t;
  1586. struct timeval now;
  1587. unsigned int seq;
  1588. assert_spin_locked(&dev->event_lock);
  1589. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1590. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1591. if (e->pipe != pipe)
  1592. continue;
  1593. if ((seq - e->event.sequence) > (1<<23))
  1594. continue;
  1595. DRM_DEBUG("vblank event on %d, current %d\n",
  1596. e->event.sequence, seq);
  1597. list_del(&e->base.link);
  1598. drm_vblank_put(dev, pipe);
  1599. send_vblank_event(dev, e, seq, &now);
  1600. }
  1601. trace_drm_vblank_event(pipe, seq);
  1602. }
  1603. /**
  1604. * drm_handle_vblank - handle a vblank event
  1605. * @dev: DRM device
  1606. * @pipe: index of CRTC where this event occurred
  1607. *
  1608. * Drivers should call this routine in their vblank interrupt handlers to
  1609. * update the vblank counter and send any signals that may be pending.
  1610. *
  1611. * This is the legacy version of drm_crtc_handle_vblank().
  1612. */
  1613. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1614. {
  1615. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1616. unsigned long irqflags;
  1617. if (WARN_ON_ONCE(!dev->num_crtcs))
  1618. return false;
  1619. if (WARN_ON(pipe >= dev->num_crtcs))
  1620. return false;
  1621. spin_lock_irqsave(&dev->event_lock, irqflags);
  1622. /* Need timestamp lock to prevent concurrent execution with
  1623. * vblank enable/disable, as this would cause inconsistent
  1624. * or corrupted timestamps and vblank counts.
  1625. */
  1626. spin_lock(&dev->vblank_time_lock);
  1627. /* Vblank irq handling disabled. Nothing to do. */
  1628. if (!vblank->enabled) {
  1629. spin_unlock(&dev->vblank_time_lock);
  1630. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1631. return false;
  1632. }
  1633. drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
  1634. spin_unlock(&dev->vblank_time_lock);
  1635. wake_up(&vblank->queue);
  1636. drm_handle_vblank_events(dev, pipe);
  1637. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1638. return true;
  1639. }
  1640. EXPORT_SYMBOL(drm_handle_vblank);
  1641. /**
  1642. * drm_crtc_handle_vblank - handle a vblank event
  1643. * @crtc: where this event occurred
  1644. *
  1645. * Drivers should call this routine in their vblank interrupt handlers to
  1646. * update the vblank counter and send any signals that may be pending.
  1647. *
  1648. * This is the native KMS version of drm_handle_vblank().
  1649. *
  1650. * Returns:
  1651. * True if the event was successfully handled, false on failure.
  1652. */
  1653. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1654. {
  1655. return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1656. }
  1657. EXPORT_SYMBOL(drm_crtc_handle_vblank);
  1658. /**
  1659. * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
  1660. * @dev: DRM device
  1661. * @pipe: CRTC for which to read the counter
  1662. *
  1663. * Drivers can plug this into the .get_vblank_counter() function if
  1664. * there is no useable hardware frame counter available.
  1665. *
  1666. * Returns:
  1667. * 0
  1668. */
  1669. u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
  1670. {
  1671. return 0;
  1672. }
  1673. EXPORT_SYMBOL(drm_vblank_no_hw_counter);