drm_irq.c 49 KB

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