drm_irq.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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. if (!dev->vblank_disable_allowed)
  310. return;
  311. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  312. if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  313. DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
  314. vblank_disable_and_save(dev, pipe);
  315. }
  316. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  317. }
  318. /**
  319. * drm_vblank_cleanup - cleanup vblank support
  320. * @dev: DRM device
  321. *
  322. * This function cleans up any resources allocated in drm_vblank_init.
  323. */
  324. void drm_vblank_cleanup(struct drm_device *dev)
  325. {
  326. unsigned int pipe;
  327. /* Bail if the driver didn't call drm_vblank_init() */
  328. if (dev->num_crtcs == 0)
  329. return;
  330. for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
  331. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  332. WARN_ON(vblank->enabled &&
  333. drm_core_check_feature(dev, DRIVER_MODESET));
  334. del_timer_sync(&vblank->disable_timer);
  335. }
  336. kfree(dev->vblank);
  337. dev->num_crtcs = 0;
  338. }
  339. EXPORT_SYMBOL(drm_vblank_cleanup);
  340. /**
  341. * drm_vblank_init - initialize vblank support
  342. * @dev: DRM device
  343. * @num_crtcs: number of CRTCs supported by @dev
  344. *
  345. * This function initializes vblank support for @num_crtcs display pipelines.
  346. *
  347. * Returns:
  348. * Zero on success or a negative error code on failure.
  349. */
  350. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
  351. {
  352. int ret = -ENOMEM;
  353. unsigned int i;
  354. spin_lock_init(&dev->vbl_lock);
  355. spin_lock_init(&dev->vblank_time_lock);
  356. dev->num_crtcs = num_crtcs;
  357. dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  358. if (!dev->vblank)
  359. goto err;
  360. for (i = 0; i < num_crtcs; i++) {
  361. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  362. vblank->dev = dev;
  363. vblank->pipe = i;
  364. init_waitqueue_head(&vblank->queue);
  365. setup_timer(&vblank->disable_timer, vblank_disable_fn,
  366. (unsigned long)vblank);
  367. }
  368. DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
  369. /* Driver specific high-precision vblank timestamping supported? */
  370. if (dev->driver->get_vblank_timestamp)
  371. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  372. else
  373. DRM_INFO("No driver support for vblank timestamp query.\n");
  374. /* Must have precise timestamping for reliable vblank instant disable */
  375. if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
  376. dev->vblank_disable_immediate = false;
  377. DRM_INFO("Setting vblank_disable_immediate to false because "
  378. "get_vblank_timestamp == NULL\n");
  379. }
  380. dev->vblank_disable_allowed = false;
  381. return 0;
  382. err:
  383. dev->num_crtcs = 0;
  384. return ret;
  385. }
  386. EXPORT_SYMBOL(drm_vblank_init);
  387. static void drm_irq_vgaarb_nokms(void *cookie, bool state)
  388. {
  389. struct drm_device *dev = cookie;
  390. if (dev->driver->vgaarb_irq) {
  391. dev->driver->vgaarb_irq(dev, state);
  392. return;
  393. }
  394. if (!dev->irq_enabled)
  395. return;
  396. if (state) {
  397. if (dev->driver->irq_uninstall)
  398. dev->driver->irq_uninstall(dev);
  399. } else {
  400. if (dev->driver->irq_preinstall)
  401. dev->driver->irq_preinstall(dev);
  402. if (dev->driver->irq_postinstall)
  403. dev->driver->irq_postinstall(dev);
  404. }
  405. }
  406. /**
  407. * drm_irq_install - install IRQ handler
  408. * @dev: DRM device
  409. * @irq: IRQ number to install the handler for
  410. *
  411. * Initializes the IRQ related data. Installs the handler, calling the driver
  412. * irq_preinstall() and irq_postinstall() functions before and after the
  413. * installation.
  414. *
  415. * This is the simplified helper interface provided for drivers with no special
  416. * needs. Drivers which need to install interrupt handlers for multiple
  417. * interrupts must instead set drm_device->irq_enabled to signal the DRM core
  418. * that vblank interrupts are available.
  419. *
  420. * Returns:
  421. * Zero on success or a negative error code on failure.
  422. */
  423. int drm_irq_install(struct drm_device *dev, int irq)
  424. {
  425. int ret;
  426. unsigned long sh_flags = 0;
  427. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  428. return -EINVAL;
  429. if (irq == 0)
  430. return -EINVAL;
  431. /* Driver must have been initialized */
  432. if (!dev->dev_private)
  433. return -EINVAL;
  434. if (dev->irq_enabled)
  435. return -EBUSY;
  436. dev->irq_enabled = true;
  437. DRM_DEBUG("irq=%d\n", irq);
  438. /* Before installing handler */
  439. if (dev->driver->irq_preinstall)
  440. dev->driver->irq_preinstall(dev);
  441. /* Install handler */
  442. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  443. sh_flags = IRQF_SHARED;
  444. ret = request_irq(irq, dev->driver->irq_handler,
  445. sh_flags, dev->driver->name, dev);
  446. if (ret < 0) {
  447. dev->irq_enabled = false;
  448. return ret;
  449. }
  450. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  451. vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
  452. /* After installing handler */
  453. if (dev->driver->irq_postinstall)
  454. ret = dev->driver->irq_postinstall(dev);
  455. if (ret < 0) {
  456. dev->irq_enabled = false;
  457. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  458. vga_client_register(dev->pdev, NULL, NULL, NULL);
  459. free_irq(irq, dev);
  460. } else {
  461. dev->irq = irq;
  462. }
  463. return ret;
  464. }
  465. EXPORT_SYMBOL(drm_irq_install);
  466. /**
  467. * drm_irq_uninstall - uninstall the IRQ handler
  468. * @dev: DRM device
  469. *
  470. * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
  471. * This should only be called by drivers which used drm_irq_install() to set up
  472. * their interrupt handler. Other drivers must only reset
  473. * drm_device->irq_enabled to false.
  474. *
  475. * Note that for kernel modesetting drivers it is a bug if this function fails.
  476. * The sanity checks are only to catch buggy user modesetting drivers which call
  477. * the same function through an ioctl.
  478. *
  479. * Returns:
  480. * Zero on success or a negative error code on failure.
  481. */
  482. int drm_irq_uninstall(struct drm_device *dev)
  483. {
  484. unsigned long irqflags;
  485. bool irq_enabled;
  486. int i;
  487. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  488. return -EINVAL;
  489. irq_enabled = dev->irq_enabled;
  490. dev->irq_enabled = false;
  491. /*
  492. * Wake up any waiters so they don't hang. This is just to paper over
  493. * isssues for UMS drivers which aren't in full control of their
  494. * vblank/irq handling. KMS drivers must ensure that vblanks are all
  495. * disabled when uninstalling the irq handler.
  496. */
  497. if (dev->num_crtcs) {
  498. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  499. for (i = 0; i < dev->num_crtcs; i++) {
  500. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  501. if (!vblank->enabled)
  502. continue;
  503. WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
  504. vblank_disable_and_save(dev, i);
  505. wake_up(&vblank->queue);
  506. }
  507. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  508. }
  509. if (!irq_enabled)
  510. return -EINVAL;
  511. DRM_DEBUG("irq=%d\n", dev->irq);
  512. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  513. vga_client_register(dev->pdev, NULL, NULL, NULL);
  514. if (dev->driver->irq_uninstall)
  515. dev->driver->irq_uninstall(dev);
  516. free_irq(dev->irq, dev);
  517. return 0;
  518. }
  519. EXPORT_SYMBOL(drm_irq_uninstall);
  520. /*
  521. * IRQ control ioctl.
  522. *
  523. * \param inode device inode.
  524. * \param file_priv DRM file private.
  525. * \param cmd command.
  526. * \param arg user argument, pointing to a drm_control structure.
  527. * \return zero on success or a negative number on failure.
  528. *
  529. * Calls irq_install() or irq_uninstall() according to \p arg.
  530. */
  531. int drm_control(struct drm_device *dev, void *data,
  532. struct drm_file *file_priv)
  533. {
  534. struct drm_control *ctl = data;
  535. int ret = 0, irq;
  536. /* if we haven't irq we fallback for compatibility reasons -
  537. * this used to be a separate function in drm_dma.h
  538. */
  539. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  540. return 0;
  541. if (drm_core_check_feature(dev, DRIVER_MODESET))
  542. return 0;
  543. /* UMS was only ever support on pci devices. */
  544. if (WARN_ON(!dev->pdev))
  545. return -EINVAL;
  546. switch (ctl->func) {
  547. case DRM_INST_HANDLER:
  548. irq = dev->pdev->irq;
  549. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  550. ctl->irq != irq)
  551. return -EINVAL;
  552. mutex_lock(&dev->struct_mutex);
  553. ret = drm_irq_install(dev, irq);
  554. mutex_unlock(&dev->struct_mutex);
  555. return ret;
  556. case DRM_UNINST_HANDLER:
  557. mutex_lock(&dev->struct_mutex);
  558. ret = drm_irq_uninstall(dev);
  559. mutex_unlock(&dev->struct_mutex);
  560. return ret;
  561. default:
  562. return -EINVAL;
  563. }
  564. }
  565. /**
  566. * drm_calc_timestamping_constants - calculate vblank timestamp constants
  567. * @crtc: drm_crtc whose timestamp constants should be updated.
  568. * @mode: display mode containing the scanout timings
  569. *
  570. * Calculate and store various constants which are later
  571. * needed by vblank and swap-completion timestamping, e.g,
  572. * by drm_calc_vbltimestamp_from_scanoutpos(). They are
  573. * derived from CRTC's true scanout timing, so they take
  574. * things like panel scaling or other adjustments into account.
  575. */
  576. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  577. const struct drm_display_mode *mode)
  578. {
  579. struct drm_device *dev = crtc->dev;
  580. unsigned int pipe = drm_crtc_index(crtc);
  581. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  582. int linedur_ns = 0, framedur_ns = 0;
  583. int dotclock = mode->crtc_clock;
  584. if (!dev->num_crtcs)
  585. return;
  586. if (WARN_ON(pipe >= dev->num_crtcs))
  587. return;
  588. /* Valid dotclock? */
  589. if (dotclock > 0) {
  590. int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
  591. /*
  592. * Convert scanline length in pixels and video
  593. * dot clock to line duration and frame duration
  594. * in nanoseconds:
  595. */
  596. linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
  597. framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
  598. /*
  599. * Fields of interlaced scanout modes are only half a frame duration.
  600. */
  601. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  602. framedur_ns /= 2;
  603. } else
  604. DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
  605. crtc->base.id);
  606. vblank->linedur_ns = linedur_ns;
  607. vblank->framedur_ns = framedur_ns;
  608. DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  609. crtc->base.id, mode->crtc_htotal,
  610. mode->crtc_vtotal, mode->crtc_vdisplay);
  611. DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
  612. crtc->base.id, dotclock, framedur_ns, linedur_ns);
  613. }
  614. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  615. /**
  616. * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  617. * @dev: DRM device
  618. * @pipe: index of CRTC whose vblank timestamp to retrieve
  619. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  620. * On return contains true maximum error of timestamp
  621. * @vblank_time: Pointer to struct timeval which should receive the timestamp
  622. * @flags: Flags to pass to driver:
  623. * 0 = Default,
  624. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  625. * @mode: mode which defines the scanout timings
  626. *
  627. * Implements calculation of exact vblank timestamps from given drm_display_mode
  628. * timings and current video scanout position of a CRTC. This can be called from
  629. * within get_vblank_timestamp() implementation of a kms driver to implement the
  630. * actual timestamping.
  631. *
  632. * Should return timestamps conforming to the OML_sync_control OpenML
  633. * extension specification. The timestamp corresponds to the end of
  634. * the vblank interval, aka start of scanout of topmost-leftmost display
  635. * pixel in the following video frame.
  636. *
  637. * Requires support for optional dev->driver->get_scanout_position()
  638. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  639. * that corresponds to the true scanout timing.
  640. *
  641. * The current implementation only handles standard video modes. It
  642. * returns as no operation if a doublescan or interlaced video mode is
  643. * active. Higher level code is expected to handle this.
  644. *
  645. * Returns:
  646. * Negative value on error, failure or if not supported in current
  647. * video mode:
  648. *
  649. * -EINVAL - Invalid CRTC.
  650. * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
  651. * -ENOTSUPP - Function not supported in current display mode.
  652. * -EIO - Failed, e.g., due to failed scanout position query.
  653. *
  654. * Returns or'ed positive status flags on success:
  655. *
  656. * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  657. * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  658. *
  659. */
  660. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  661. unsigned int pipe,
  662. int *max_error,
  663. struct timeval *vblank_time,
  664. unsigned flags,
  665. const struct drm_display_mode *mode)
  666. {
  667. struct timeval tv_etime;
  668. ktime_t stime, etime;
  669. unsigned int vbl_status;
  670. int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  671. int vpos, hpos, i;
  672. int delta_ns, duration_ns;
  673. if (pipe >= dev->num_crtcs) {
  674. DRM_ERROR("Invalid crtc %u\n", pipe);
  675. return -EINVAL;
  676. }
  677. /* Scanout position query not supported? Should not happen. */
  678. if (!dev->driver->get_scanout_position) {
  679. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  680. return -EIO;
  681. }
  682. /* If mode timing undefined, just return as no-op:
  683. * Happens during initial modesetting of a crtc.
  684. */
  685. if (mode->crtc_clock == 0) {
  686. DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
  687. return -EAGAIN;
  688. }
  689. /* Get current scanout position with system timestamp.
  690. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  691. * if single query takes longer than max_error nanoseconds.
  692. *
  693. * This guarantees a tight bound on maximum error if
  694. * code gets preempted or delayed for some reason.
  695. */
  696. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  697. /*
  698. * Get vertical and horizontal scanout position vpos, hpos,
  699. * and bounding timestamps stime, etime, pre/post query.
  700. */
  701. vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
  702. &vpos, &hpos,
  703. &stime, &etime,
  704. mode);
  705. /* Return as no-op if scanout query unsupported or failed. */
  706. if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  707. DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
  708. pipe, vbl_status);
  709. return -EIO;
  710. }
  711. /* Compute uncertainty in timestamp of scanout position query. */
  712. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  713. /* Accept result with < max_error nsecs timing uncertainty. */
  714. if (duration_ns <= *max_error)
  715. break;
  716. }
  717. /* Noisy system timing? */
  718. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  719. DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
  720. pipe, duration_ns/1000, *max_error/1000, i);
  721. }
  722. /* Return upper bound of timestamp precision error. */
  723. *max_error = duration_ns;
  724. /* Check if in vblank area:
  725. * vpos is >=0 in video scanout area, but negative
  726. * within vblank area, counting down the number of lines until
  727. * start of scanout.
  728. */
  729. if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
  730. ret |= DRM_VBLANKTIME_IN_VBLANK;
  731. /* Convert scanout position into elapsed time at raw_time query
  732. * since start of scanout at first display scanline. delta_ns
  733. * can be negative if start of scanout hasn't happened yet.
  734. */
  735. delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  736. mode->crtc_clock);
  737. if (!drm_timestamp_monotonic)
  738. etime = ktime_mono_to_real(etime);
  739. /* save this only for debugging purposes */
  740. tv_etime = ktime_to_timeval(etime);
  741. /* Subtract time delta from raw timestamp to get final
  742. * vblank_time timestamp for end of vblank.
  743. */
  744. if (delta_ns < 0)
  745. etime = ktime_add_ns(etime, -delta_ns);
  746. else
  747. etime = ktime_sub_ns(etime, delta_ns);
  748. *vblank_time = ktime_to_timeval(etime);
  749. DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  750. pipe, vbl_status, hpos, vpos,
  751. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  752. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  753. duration_ns/1000, i);
  754. return ret;
  755. }
  756. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  757. static struct timeval get_drm_timestamp(void)
  758. {
  759. ktime_t now;
  760. now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
  761. return ktime_to_timeval(now);
  762. }
  763. /**
  764. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  765. * vblank interval
  766. * @dev: DRM device
  767. * @pipe: index of CRTC whose vblank timestamp to retrieve
  768. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  769. * @flags: Flags to pass to driver:
  770. * 0 = Default,
  771. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  772. *
  773. * Fetches the system timestamp corresponding to the time of the most recent
  774. * vblank interval on specified CRTC. May call into kms-driver to
  775. * compute the timestamp with a high-precision GPU specific method.
  776. *
  777. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  778. * call, i.e., it isn't very precisely locked to the true vblank.
  779. *
  780. * Returns:
  781. * True if timestamp is considered to be very precise, false otherwise.
  782. */
  783. static bool
  784. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  785. struct timeval *tvblank, unsigned flags)
  786. {
  787. int ret;
  788. /* Define requested maximum error on timestamps (nanoseconds). */
  789. int max_error = (int) drm_timestamp_precision * 1000;
  790. /* Query driver if possible and precision timestamping enabled. */
  791. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  792. ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
  793. tvblank, flags);
  794. if (ret > 0)
  795. return true;
  796. }
  797. /* GPU high precision timestamp query unsupported or failed.
  798. * Return current monotonic/gettimeofday timestamp as best estimate.
  799. */
  800. *tvblank = get_drm_timestamp();
  801. return false;
  802. }
  803. /**
  804. * drm_vblank_count - retrieve "cooked" vblank counter value
  805. * @dev: DRM device
  806. * @pipe: index of CRTC for which to retrieve the counter
  807. *
  808. * Fetches the "cooked" vblank count value that represents the number of
  809. * vblank events since the system was booted, including lost events due to
  810. * modesetting activity.
  811. *
  812. * This is the legacy version of drm_crtc_vblank_count().
  813. *
  814. * Returns:
  815. * The software vblank counter.
  816. */
  817. u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
  818. {
  819. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  820. if (WARN_ON(pipe >= dev->num_crtcs))
  821. return 0;
  822. return vblank->count;
  823. }
  824. EXPORT_SYMBOL(drm_vblank_count);
  825. /**
  826. * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  827. * @crtc: which counter to retrieve
  828. *
  829. * Fetches the "cooked" vblank count value that represents the number of
  830. * vblank events since the system was booted, including lost events due to
  831. * modesetting activity.
  832. *
  833. * This is the native KMS version of drm_vblank_count().
  834. *
  835. * Returns:
  836. * The software vblank counter.
  837. */
  838. u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
  839. {
  840. return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  841. }
  842. EXPORT_SYMBOL(drm_crtc_vblank_count);
  843. /**
  844. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
  845. * system timestamp corresponding to that vblank counter value.
  846. * @dev: DRM device
  847. * @pipe: index of CRTC whose counter to retrieve
  848. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  849. *
  850. * Fetches the "cooked" vblank count value that represents the number of
  851. * vblank events since the system was booted, including lost events due to
  852. * modesetting activity. Returns corresponding system timestamp of the time
  853. * of the vblank interval that corresponds to the current vblank counter value.
  854. *
  855. * This is the legacy version of drm_crtc_vblank_count_and_time().
  856. */
  857. u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  858. struct timeval *vblanktime)
  859. {
  860. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  861. int count = DRM_TIMESTAMP_MAXRETRIES;
  862. u32 cur_vblank;
  863. if (WARN_ON(pipe >= dev->num_crtcs))
  864. return 0;
  865. /*
  866. * Vblank timestamps are read lockless. To ensure consistency the vblank
  867. * counter is rechecked and ordering is ensured using memory barriers.
  868. * This works like a seqlock. The write-side barriers are in store_vblank.
  869. */
  870. do {
  871. cur_vblank = vblank->count;
  872. smp_rmb();
  873. *vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
  874. smp_rmb();
  875. } while (cur_vblank != vblank->count && --count > 0);
  876. return cur_vblank;
  877. }
  878. EXPORT_SYMBOL(drm_vblank_count_and_time);
  879. /**
  880. * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  881. * and the system timestamp corresponding to that vblank counter value
  882. * @crtc: which counter to retrieve
  883. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  884. *
  885. * Fetches the "cooked" vblank count value that represents the number of
  886. * vblank events since the system was booted, including lost events due to
  887. * modesetting activity. Returns corresponding system timestamp of the time
  888. * of the vblank interval that corresponds to the current vblank counter value.
  889. *
  890. * This is the native KMS version of drm_vblank_count_and_time().
  891. */
  892. u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  893. struct timeval *vblanktime)
  894. {
  895. return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  896. vblanktime);
  897. }
  898. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  899. static void send_vblank_event(struct drm_device *dev,
  900. struct drm_pending_vblank_event *e,
  901. unsigned long seq, struct timeval *now)
  902. {
  903. assert_spin_locked(&dev->event_lock);
  904. e->event.sequence = seq;
  905. e->event.tv_sec = now->tv_sec;
  906. e->event.tv_usec = now->tv_usec;
  907. list_add_tail(&e->base.link,
  908. &e->base.file_priv->event_list);
  909. wake_up_interruptible(&e->base.file_priv->event_wait);
  910. trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
  911. e->event.sequence);
  912. }
  913. /**
  914. * drm_arm_vblank_event - arm vblank event after pageflip
  915. * @dev: DRM device
  916. * @pipe: CRTC index
  917. * @e: the event to prepare to send
  918. *
  919. * A lot of drivers need to generate vblank events for the very next vblank
  920. * interrupt. For example when the page flip interrupt happens when the page
  921. * flip gets armed, but not when it actually executes within the next vblank
  922. * period. This helper function implements exactly the required vblank arming
  923. * behaviour.
  924. *
  925. * Caller must hold event lock. Caller must also hold a vblank reference for
  926. * the event @e, which will be dropped when the next vblank arrives.
  927. *
  928. * This is the legacy version of drm_crtc_arm_vblank_event().
  929. */
  930. void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
  931. struct drm_pending_vblank_event *e)
  932. {
  933. assert_spin_locked(&dev->event_lock);
  934. e->pipe = pipe;
  935. e->event.sequence = drm_vblank_count(dev, pipe);
  936. list_add_tail(&e->base.link, &dev->vblank_event_list);
  937. }
  938. EXPORT_SYMBOL(drm_arm_vblank_event);
  939. /**
  940. * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  941. * @crtc: the source CRTC of the vblank event
  942. * @e: the event to send
  943. *
  944. * A lot of drivers need to generate vblank events for the very next vblank
  945. * interrupt. For example when the page flip interrupt happens when the page
  946. * flip gets armed, but not when it actually executes within the next vblank
  947. * period. This helper function implements exactly the required vblank arming
  948. * behaviour.
  949. *
  950. * Caller must hold event lock. Caller must also hold a vblank reference for
  951. * the event @e, which will be dropped when the next vblank arrives.
  952. *
  953. * This is the native KMS version of drm_arm_vblank_event().
  954. */
  955. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  956. struct drm_pending_vblank_event *e)
  957. {
  958. drm_arm_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
  959. }
  960. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  961. /**
  962. * drm_send_vblank_event - helper to send vblank event after pageflip
  963. * @dev: DRM device
  964. * @pipe: CRTC index
  965. * @e: the event to send
  966. *
  967. * Updates sequence # and timestamp on event, and sends it to userspace.
  968. * Caller must hold event lock.
  969. *
  970. * This is the legacy version of drm_crtc_send_vblank_event().
  971. */
  972. void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
  973. struct drm_pending_vblank_event *e)
  974. {
  975. struct timeval now;
  976. unsigned int seq;
  977. if (dev->num_crtcs > 0) {
  978. seq = drm_vblank_count_and_time(dev, pipe, &now);
  979. } else {
  980. seq = 0;
  981. now = get_drm_timestamp();
  982. }
  983. e->pipe = pipe;
  984. send_vblank_event(dev, e, seq, &now);
  985. }
  986. EXPORT_SYMBOL(drm_send_vblank_event);
  987. /**
  988. * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  989. * @crtc: the source CRTC of the vblank event
  990. * @e: the event to send
  991. *
  992. * Updates sequence # and timestamp on event, and sends it to userspace.
  993. * Caller must hold event lock.
  994. *
  995. * This is the native KMS version of drm_send_vblank_event().
  996. */
  997. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  998. struct drm_pending_vblank_event *e)
  999. {
  1000. drm_send_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
  1001. }
  1002. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  1003. /**
  1004. * drm_vblank_enable - enable the vblank interrupt on a CRTC
  1005. * @dev: DRM device
  1006. * @pipe: CRTC index
  1007. *
  1008. * Returns:
  1009. * Zero on success or a negative error code on failure.
  1010. */
  1011. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  1012. {
  1013. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1014. int ret = 0;
  1015. assert_spin_locked(&dev->vbl_lock);
  1016. spin_lock(&dev->vblank_time_lock);
  1017. if (!vblank->enabled) {
  1018. /*
  1019. * Enable vblank irqs under vblank_time_lock protection.
  1020. * All vblank count & timestamp updates are held off
  1021. * until we are done reinitializing master counter and
  1022. * timestamps. Filtercode in drm_handle_vblank() will
  1023. * prevent double-accounting of same vblank interval.
  1024. */
  1025. ret = dev->driver->enable_vblank(dev, pipe);
  1026. DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
  1027. if (ret)
  1028. atomic_dec(&vblank->refcount);
  1029. else {
  1030. vblank->enabled = true;
  1031. drm_update_vblank_count(dev, pipe, 0);
  1032. }
  1033. }
  1034. spin_unlock(&dev->vblank_time_lock);
  1035. return ret;
  1036. }
  1037. /**
  1038. * drm_vblank_get - get a reference count on vblank events
  1039. * @dev: DRM device
  1040. * @pipe: index of CRTC to own
  1041. *
  1042. * Acquire a reference count on vblank events to avoid having them disabled
  1043. * while in use.
  1044. *
  1045. * This is the legacy version of drm_crtc_vblank_get().
  1046. *
  1047. * Returns:
  1048. * Zero on success or a negative error code on failure.
  1049. */
  1050. int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  1051. {
  1052. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1053. unsigned long irqflags;
  1054. int ret = 0;
  1055. if (!dev->num_crtcs)
  1056. return -EINVAL;
  1057. if (WARN_ON(pipe >= dev->num_crtcs))
  1058. return -EINVAL;
  1059. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1060. /* Going from 0->1 means we have to enable interrupts again */
  1061. if (atomic_add_return(1, &vblank->refcount) == 1) {
  1062. ret = drm_vblank_enable(dev, pipe);
  1063. } else {
  1064. if (!vblank->enabled) {
  1065. atomic_dec(&vblank->refcount);
  1066. ret = -EINVAL;
  1067. }
  1068. }
  1069. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1070. return ret;
  1071. }
  1072. EXPORT_SYMBOL(drm_vblank_get);
  1073. /**
  1074. * drm_crtc_vblank_get - get a reference count on vblank events
  1075. * @crtc: which CRTC to own
  1076. *
  1077. * Acquire a reference count on vblank events to avoid having them disabled
  1078. * while in use.
  1079. *
  1080. * This is the native kms version of drm_vblank_get().
  1081. *
  1082. * Returns:
  1083. * Zero on success or a negative error code on failure.
  1084. */
  1085. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  1086. {
  1087. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  1088. }
  1089. EXPORT_SYMBOL(drm_crtc_vblank_get);
  1090. /**
  1091. * drm_vblank_put - release ownership of vblank events
  1092. * @dev: DRM device
  1093. * @pipe: index of CRTC to release
  1094. *
  1095. * Release ownership of a given vblank counter, turning off interrupts
  1096. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1097. *
  1098. * This is the legacy version of drm_crtc_vblank_put().
  1099. */
  1100. void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  1101. {
  1102. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1103. if (WARN_ON(pipe >= dev->num_crtcs))
  1104. return;
  1105. if (WARN_ON(atomic_read(&vblank->refcount) == 0))
  1106. return;
  1107. /* Last user schedules interrupt disable */
  1108. if (atomic_dec_and_test(&vblank->refcount)) {
  1109. if (drm_vblank_offdelay == 0)
  1110. return;
  1111. else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
  1112. vblank_disable_fn((unsigned long)vblank);
  1113. else
  1114. mod_timer(&vblank->disable_timer,
  1115. jiffies + ((drm_vblank_offdelay * HZ)/1000));
  1116. }
  1117. }
  1118. EXPORT_SYMBOL(drm_vblank_put);
  1119. /**
  1120. * drm_crtc_vblank_put - give up ownership of vblank events
  1121. * @crtc: which counter to give up
  1122. *
  1123. * Release ownership of a given vblank counter, turning off interrupts
  1124. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1125. *
  1126. * This is the native kms version of drm_vblank_put().
  1127. */
  1128. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  1129. {
  1130. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  1131. }
  1132. EXPORT_SYMBOL(drm_crtc_vblank_put);
  1133. /**
  1134. * drm_wait_one_vblank - wait for one vblank
  1135. * @dev: DRM device
  1136. * @pipe: CRTC index
  1137. *
  1138. * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
  1139. * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  1140. * due to lack of driver support or because the crtc is off.
  1141. */
  1142. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
  1143. {
  1144. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1145. int ret;
  1146. u32 last;
  1147. if (WARN_ON(pipe >= dev->num_crtcs))
  1148. return;
  1149. ret = drm_vblank_get(dev, pipe);
  1150. if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
  1151. return;
  1152. last = drm_vblank_count(dev, pipe);
  1153. ret = wait_event_timeout(vblank->queue,
  1154. last != drm_vblank_count(dev, pipe),
  1155. msecs_to_jiffies(100));
  1156. WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  1157. drm_vblank_put(dev, pipe);
  1158. }
  1159. EXPORT_SYMBOL(drm_wait_one_vblank);
  1160. /**
  1161. * drm_crtc_wait_one_vblank - wait for one vblank
  1162. * @crtc: DRM crtc
  1163. *
  1164. * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  1165. * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  1166. * due to lack of driver support or because the crtc is off.
  1167. */
  1168. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  1169. {
  1170. drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
  1171. }
  1172. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  1173. /**
  1174. * drm_vblank_off - disable vblank events on a CRTC
  1175. * @dev: DRM device
  1176. * @pipe: CRTC index
  1177. *
  1178. * Drivers can use this function to shut down the vblank interrupt handling when
  1179. * disabling a crtc. This function ensures that the latest vblank frame count is
  1180. * stored so that drm_vblank_on() can restore it again.
  1181. *
  1182. * Drivers must use this function when the hardware vblank counter can get
  1183. * reset, e.g. when suspending.
  1184. *
  1185. * This is the legacy version of drm_crtc_vblank_off().
  1186. */
  1187. void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
  1188. {
  1189. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1190. struct drm_pending_vblank_event *e, *t;
  1191. struct timeval now;
  1192. unsigned long irqflags;
  1193. unsigned int seq;
  1194. if (WARN_ON(pipe >= dev->num_crtcs))
  1195. return;
  1196. spin_lock_irqsave(&dev->event_lock, irqflags);
  1197. spin_lock(&dev->vbl_lock);
  1198. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1199. pipe, vblank->enabled, vblank->inmodeset);
  1200. /* Avoid redundant vblank disables without previous drm_vblank_on(). */
  1201. if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
  1202. vblank_disable_and_save(dev, pipe);
  1203. wake_up(&vblank->queue);
  1204. /*
  1205. * Prevent subsequent drm_vblank_get() from re-enabling
  1206. * the vblank interrupt by bumping the refcount.
  1207. */
  1208. if (!vblank->inmodeset) {
  1209. atomic_inc(&vblank->refcount);
  1210. vblank->inmodeset = 1;
  1211. }
  1212. spin_unlock(&dev->vbl_lock);
  1213. /* Send any queued vblank events, lest the natives grow disquiet */
  1214. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1215. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1216. if (e->pipe != pipe)
  1217. continue;
  1218. DRM_DEBUG("Sending premature vblank event on disable: "
  1219. "wanted %d, current %d\n",
  1220. e->event.sequence, seq);
  1221. list_del(&e->base.link);
  1222. drm_vblank_put(dev, pipe);
  1223. send_vblank_event(dev, e, seq, &now);
  1224. }
  1225. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1226. }
  1227. EXPORT_SYMBOL(drm_vblank_off);
  1228. /**
  1229. * drm_crtc_vblank_off - disable vblank events on a CRTC
  1230. * @crtc: CRTC in question
  1231. *
  1232. * Drivers can use this function to shut down the vblank interrupt handling when
  1233. * disabling a crtc. This function ensures that the latest vblank frame count is
  1234. * stored so that drm_vblank_on can restore it again.
  1235. *
  1236. * Drivers must use this function when the hardware vblank counter can get
  1237. * reset, e.g. when suspending.
  1238. *
  1239. * This is the native kms version of drm_vblank_off().
  1240. */
  1241. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  1242. {
  1243. drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
  1244. }
  1245. EXPORT_SYMBOL(drm_crtc_vblank_off);
  1246. /**
  1247. * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  1248. * @crtc: CRTC in question
  1249. *
  1250. * Drivers can use this function to reset the vblank state to off at load time.
  1251. * Drivers should use this together with the drm_crtc_vblank_off() and
  1252. * drm_crtc_vblank_on() functions. The difference compared to
  1253. * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  1254. * and hence doesn't need to call any driver hooks.
  1255. */
  1256. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1257. {
  1258. struct drm_device *dev = crtc->dev;
  1259. unsigned long irqflags;
  1260. unsigned int pipe = drm_crtc_index(crtc);
  1261. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1262. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1263. /*
  1264. * Prevent subsequent drm_vblank_get() from enabling the vblank
  1265. * interrupt by bumping the refcount.
  1266. */
  1267. if (!vblank->inmodeset) {
  1268. atomic_inc(&vblank->refcount);
  1269. vblank->inmodeset = 1;
  1270. }
  1271. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1272. WARN_ON(!list_empty(&dev->vblank_event_list));
  1273. }
  1274. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1275. /**
  1276. * drm_vblank_on - enable vblank events on a CRTC
  1277. * @dev: DRM device
  1278. * @pipe: CRTC index
  1279. *
  1280. * This functions restores the vblank interrupt state captured with
  1281. * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  1282. * drm_vblank_off() can be unbalanced and so can also be unconditionally called
  1283. * in driver load code to reflect the current hardware state of the crtc.
  1284. *
  1285. * This is the legacy version of drm_crtc_vblank_on().
  1286. */
  1287. void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
  1288. {
  1289. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1290. unsigned long irqflags;
  1291. if (WARN_ON(pipe >= dev->num_crtcs))
  1292. return;
  1293. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1294. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1295. pipe, vblank->enabled, vblank->inmodeset);
  1296. /* Drop our private "prevent drm_vblank_get" refcount */
  1297. if (vblank->inmodeset) {
  1298. atomic_dec(&vblank->refcount);
  1299. vblank->inmodeset = 0;
  1300. }
  1301. drm_reset_vblank_timestamp(dev, pipe);
  1302. /*
  1303. * re-enable interrupts if there are users left, or the
  1304. * user wishes vblank interrupts to be enabled all the time.
  1305. */
  1306. if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
  1307. WARN_ON(drm_vblank_enable(dev, pipe));
  1308. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1309. }
  1310. EXPORT_SYMBOL(drm_vblank_on);
  1311. /**
  1312. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1313. * @crtc: CRTC in question
  1314. *
  1315. * This functions restores the vblank interrupt state captured with
  1316. * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  1317. * drm_vblank_off() can be unbalanced and so can also be unconditionally called
  1318. * in driver load code to reflect the current hardware state of the crtc.
  1319. *
  1320. * This is the native kms version of drm_vblank_on().
  1321. */
  1322. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1323. {
  1324. drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
  1325. }
  1326. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1327. /**
  1328. * drm_vblank_pre_modeset - account for vblanks across mode sets
  1329. * @dev: DRM device
  1330. * @pipe: CRTC index
  1331. *
  1332. * Account for vblank events across mode setting events, which will likely
  1333. * reset the hardware frame counter.
  1334. *
  1335. * This is done by grabbing a temporary vblank reference to ensure that the
  1336. * vblank interrupt keeps running across the modeset sequence. With this the
  1337. * software-side vblank frame counting will ensure that there are no jumps or
  1338. * discontinuities.
  1339. *
  1340. * Unfortunately this approach is racy and also doesn't work when the vblank
  1341. * interrupt stops running, e.g. across system suspend resume. It is therefore
  1342. * highly recommended that drivers use the newer drm_vblank_off() and
  1343. * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
  1344. * using "cooked" software vblank frame counters and not relying on any hardware
  1345. * counters.
  1346. *
  1347. * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
  1348. * again.
  1349. */
  1350. void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
  1351. {
  1352. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1353. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1354. if (!dev->num_crtcs)
  1355. return;
  1356. if (WARN_ON(pipe >= dev->num_crtcs))
  1357. return;
  1358. /*
  1359. * To avoid all the problems that might happen if interrupts
  1360. * were enabled/disabled around or between these calls, we just
  1361. * have the kernel take a reference on the CRTC (just once though
  1362. * to avoid corrupting the count if multiple, mismatch calls occur),
  1363. * so that interrupts remain enabled in the interim.
  1364. */
  1365. if (!vblank->inmodeset) {
  1366. vblank->inmodeset = 0x1;
  1367. if (drm_vblank_get(dev, pipe) == 0)
  1368. vblank->inmodeset |= 0x2;
  1369. }
  1370. }
  1371. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  1372. /**
  1373. * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
  1374. * @dev: DRM device
  1375. * @pipe: CRTC index
  1376. *
  1377. * This function again drops the temporary vblank reference acquired in
  1378. * drm_vblank_pre_modeset.
  1379. */
  1380. void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
  1381. {
  1382. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1383. unsigned long irqflags;
  1384. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1385. if (!dev->num_crtcs)
  1386. return;
  1387. if (WARN_ON(pipe >= dev->num_crtcs))
  1388. return;
  1389. if (vblank->inmodeset) {
  1390. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1391. dev->vblank_disable_allowed = true;
  1392. drm_reset_vblank_timestamp(dev, pipe);
  1393. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1394. if (vblank->inmodeset & 0x2)
  1395. drm_vblank_put(dev, pipe);
  1396. vblank->inmodeset = 0;
  1397. }
  1398. }
  1399. EXPORT_SYMBOL(drm_vblank_post_modeset);
  1400. /*
  1401. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  1402. * @DRM_IOCTL_ARGS: standard ioctl arguments
  1403. *
  1404. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  1405. * ioctls around modesetting so that any lost vblank events are accounted for.
  1406. *
  1407. * Generally the counter will reset across mode sets. If interrupts are
  1408. * enabled around this call, we don't have to do anything since the counter
  1409. * will have already been incremented.
  1410. */
  1411. int drm_modeset_ctl(struct drm_device *dev, void *data,
  1412. struct drm_file *file_priv)
  1413. {
  1414. struct drm_modeset_ctl *modeset = data;
  1415. unsigned int pipe;
  1416. /* If drm_vblank_init() hasn't been called yet, just no-op */
  1417. if (!dev->num_crtcs)
  1418. return 0;
  1419. /* KMS drivers handle this internally */
  1420. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1421. return 0;
  1422. pipe = modeset->crtc;
  1423. if (pipe >= dev->num_crtcs)
  1424. return -EINVAL;
  1425. switch (modeset->cmd) {
  1426. case _DRM_PRE_MODESET:
  1427. drm_vblank_pre_modeset(dev, pipe);
  1428. break;
  1429. case _DRM_POST_MODESET:
  1430. drm_vblank_post_modeset(dev, pipe);
  1431. break;
  1432. default:
  1433. return -EINVAL;
  1434. }
  1435. return 0;
  1436. }
  1437. static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
  1438. union drm_wait_vblank *vblwait,
  1439. struct drm_file *file_priv)
  1440. {
  1441. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1442. struct drm_pending_vblank_event *e;
  1443. struct timeval now;
  1444. unsigned long flags;
  1445. unsigned int seq;
  1446. int ret;
  1447. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1448. if (e == NULL) {
  1449. ret = -ENOMEM;
  1450. goto err_put;
  1451. }
  1452. e->pipe = pipe;
  1453. e->base.pid = current->pid;
  1454. e->event.base.type = DRM_EVENT_VBLANK;
  1455. e->event.base.length = sizeof(e->event);
  1456. e->event.user_data = vblwait->request.signal;
  1457. e->base.event = &e->event.base;
  1458. e->base.file_priv = file_priv;
  1459. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  1460. spin_lock_irqsave(&dev->event_lock, flags);
  1461. /*
  1462. * drm_vblank_off() might have been called after we called
  1463. * drm_vblank_get(). drm_vblank_off() holds event_lock
  1464. * around the vblank disable, so no need for further locking.
  1465. * The reference from drm_vblank_get() protects against
  1466. * vblank disable from another source.
  1467. */
  1468. if (!vblank->enabled) {
  1469. ret = -EINVAL;
  1470. goto err_unlock;
  1471. }
  1472. if (file_priv->event_space < sizeof(e->event)) {
  1473. ret = -EBUSY;
  1474. goto err_unlock;
  1475. }
  1476. file_priv->event_space -= sizeof(e->event);
  1477. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1478. if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
  1479. (seq - vblwait->request.sequence) <= (1 << 23)) {
  1480. vblwait->request.sequence = seq + 1;
  1481. vblwait->reply.sequence = vblwait->request.sequence;
  1482. }
  1483. DRM_DEBUG("event on vblank count %d, current %d, crtc %u\n",
  1484. vblwait->request.sequence, seq, pipe);
  1485. trace_drm_vblank_event_queued(current->pid, pipe,
  1486. vblwait->request.sequence);
  1487. e->event.sequence = vblwait->request.sequence;
  1488. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  1489. drm_vblank_put(dev, pipe);
  1490. send_vblank_event(dev, e, seq, &now);
  1491. vblwait->reply.sequence = seq;
  1492. } else {
  1493. /* drm_handle_vblank_events will call drm_vblank_put */
  1494. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1495. vblwait->reply.sequence = vblwait->request.sequence;
  1496. }
  1497. spin_unlock_irqrestore(&dev->event_lock, flags);
  1498. return 0;
  1499. err_unlock:
  1500. spin_unlock_irqrestore(&dev->event_lock, flags);
  1501. kfree(e);
  1502. err_put:
  1503. drm_vblank_put(dev, pipe);
  1504. return ret;
  1505. }
  1506. /*
  1507. * Wait for VBLANK.
  1508. *
  1509. * \param inode device inode.
  1510. * \param file_priv DRM file private.
  1511. * \param cmd command.
  1512. * \param data user argument, pointing to a drm_wait_vblank structure.
  1513. * \return zero on success or a negative number on failure.
  1514. *
  1515. * This function enables the vblank interrupt on the pipe requested, then
  1516. * sleeps waiting for the requested sequence number to occur, and drops
  1517. * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
  1518. * after a timeout with no further vblank waits scheduled).
  1519. */
  1520. int drm_wait_vblank(struct drm_device *dev, void *data,
  1521. struct drm_file *file_priv)
  1522. {
  1523. struct drm_vblank_crtc *vblank;
  1524. union drm_wait_vblank *vblwait = data;
  1525. int ret;
  1526. unsigned int flags, seq, pipe, high_pipe;
  1527. if (!dev->irq_enabled)
  1528. return -EINVAL;
  1529. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1530. return -EINVAL;
  1531. if (vblwait->request.type &
  1532. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1533. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1534. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1535. vblwait->request.type,
  1536. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1537. _DRM_VBLANK_HIGH_CRTC_MASK));
  1538. return -EINVAL;
  1539. }
  1540. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1541. high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1542. if (high_pipe)
  1543. pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1544. else
  1545. pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1546. if (pipe >= dev->num_crtcs)
  1547. return -EINVAL;
  1548. vblank = &dev->vblank[pipe];
  1549. ret = drm_vblank_get(dev, pipe);
  1550. if (ret) {
  1551. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  1552. return ret;
  1553. }
  1554. seq = drm_vblank_count(dev, pipe);
  1555. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1556. case _DRM_VBLANK_RELATIVE:
  1557. vblwait->request.sequence += seq;
  1558. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1559. case _DRM_VBLANK_ABSOLUTE:
  1560. break;
  1561. default:
  1562. ret = -EINVAL;
  1563. goto done;
  1564. }
  1565. if (flags & _DRM_VBLANK_EVENT) {
  1566. /* must hold on to the vblank ref until the event fires
  1567. * drm_vblank_put will be called asynchronously
  1568. */
  1569. return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
  1570. }
  1571. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1572. (seq - vblwait->request.sequence) <= (1<<23)) {
  1573. vblwait->request.sequence = seq + 1;
  1574. }
  1575. DRM_DEBUG("waiting on vblank count %d, crtc %u\n",
  1576. vblwait->request.sequence, pipe);
  1577. vblank->last_wait = vblwait->request.sequence;
  1578. DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
  1579. (((drm_vblank_count(dev, pipe) -
  1580. vblwait->request.sequence) <= (1 << 23)) ||
  1581. !vblank->enabled ||
  1582. !dev->irq_enabled));
  1583. if (ret != -EINTR) {
  1584. struct timeval now;
  1585. vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1586. vblwait->reply.tval_sec = now.tv_sec;
  1587. vblwait->reply.tval_usec = now.tv_usec;
  1588. DRM_DEBUG("returning %d to client\n",
  1589. vblwait->reply.sequence);
  1590. } else {
  1591. DRM_DEBUG("vblank wait interrupted by signal\n");
  1592. }
  1593. done:
  1594. drm_vblank_put(dev, pipe);
  1595. return ret;
  1596. }
  1597. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1598. {
  1599. struct drm_pending_vblank_event *e, *t;
  1600. struct timeval now;
  1601. unsigned int seq;
  1602. assert_spin_locked(&dev->event_lock);
  1603. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1604. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1605. if (e->pipe != pipe)
  1606. continue;
  1607. if ((seq - e->event.sequence) > (1<<23))
  1608. continue;
  1609. DRM_DEBUG("vblank event on %d, current %d\n",
  1610. e->event.sequence, seq);
  1611. list_del(&e->base.link);
  1612. drm_vblank_put(dev, pipe);
  1613. send_vblank_event(dev, e, seq, &now);
  1614. }
  1615. trace_drm_vblank_event(pipe, seq);
  1616. }
  1617. /**
  1618. * drm_handle_vblank - handle a vblank event
  1619. * @dev: DRM device
  1620. * @pipe: index of CRTC where this event occurred
  1621. *
  1622. * Drivers should call this routine in their vblank interrupt handlers to
  1623. * update the vblank counter and send any signals that may be pending.
  1624. *
  1625. * This is the legacy version of drm_crtc_handle_vblank().
  1626. */
  1627. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1628. {
  1629. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1630. unsigned long irqflags;
  1631. if (WARN_ON_ONCE(!dev->num_crtcs))
  1632. return false;
  1633. if (WARN_ON(pipe >= dev->num_crtcs))
  1634. return false;
  1635. spin_lock_irqsave(&dev->event_lock, irqflags);
  1636. /* Need timestamp lock to prevent concurrent execution with
  1637. * vblank enable/disable, as this would cause inconsistent
  1638. * or corrupted timestamps and vblank counts.
  1639. */
  1640. spin_lock(&dev->vblank_time_lock);
  1641. /* Vblank irq handling disabled. Nothing to do. */
  1642. if (!vblank->enabled) {
  1643. spin_unlock(&dev->vblank_time_lock);
  1644. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1645. return false;
  1646. }
  1647. drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
  1648. spin_unlock(&dev->vblank_time_lock);
  1649. wake_up(&vblank->queue);
  1650. drm_handle_vblank_events(dev, pipe);
  1651. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1652. return true;
  1653. }
  1654. EXPORT_SYMBOL(drm_handle_vblank);
  1655. /**
  1656. * drm_crtc_handle_vblank - handle a vblank event
  1657. * @crtc: where this event occurred
  1658. *
  1659. * Drivers should call this routine in their vblank interrupt handlers to
  1660. * update the vblank counter and send any signals that may be pending.
  1661. *
  1662. * This is the native KMS version of drm_handle_vblank().
  1663. *
  1664. * Returns:
  1665. * True if the event was successfully handled, false on failure.
  1666. */
  1667. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1668. {
  1669. return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1670. }
  1671. EXPORT_SYMBOL(drm_crtc_handle_vblank);
  1672. /**
  1673. * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
  1674. * @dev: DRM device
  1675. * @pipe: CRTC for which to read the counter
  1676. *
  1677. * Drivers can plug this into the .get_vblank_counter() function if
  1678. * there is no useable hardware frame counter available.
  1679. *
  1680. * Returns:
  1681. * 0
  1682. */
  1683. u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
  1684. {
  1685. return 0;
  1686. }
  1687. EXPORT_SYMBOL(drm_vblank_no_hw_counter);