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