intel_fbc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * DOC: Frame Buffer Compression (FBC)
  25. *
  26. * FBC tries to save memory bandwidth (and so power consumption) by
  27. * compressing the amount of memory used by the display. It is total
  28. * transparent to user space and completely handled in the kernel.
  29. *
  30. * The benefits of FBC are mostly visible with solid backgrounds and
  31. * variation-less patterns. It comes from keeping the memory footprint small
  32. * and having fewer memory pages opened and accessed for refreshing the display.
  33. *
  34. * i915 is responsible to reserve stolen memory for FBC and configure its
  35. * offset on proper registers. The hardware takes care of all
  36. * compress/decompress. However there are many known cases where we have to
  37. * forcibly disable it to allow proper screen updates.
  38. */
  39. #include "intel_drv.h"
  40. #include "i915_drv.h"
  41. static inline bool fbc_supported(struct drm_i915_private *dev_priv)
  42. {
  43. return HAS_FBC(dev_priv);
  44. }
  45. static inline bool fbc_on_pipe_a_only(struct drm_i915_private *dev_priv)
  46. {
  47. return IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8;
  48. }
  49. static inline bool fbc_on_plane_a_only(struct drm_i915_private *dev_priv)
  50. {
  51. return INTEL_GEN(dev_priv) < 4;
  52. }
  53. static inline bool no_fbc_on_multiple_pipes(struct drm_i915_private *dev_priv)
  54. {
  55. return INTEL_GEN(dev_priv) <= 3;
  56. }
  57. /*
  58. * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the
  59. * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's
  60. * origin so the x and y offsets can actually fit the registers. As a
  61. * consequence, the fence doesn't really start exactly at the display plane
  62. * address we program because it starts at the real start of the buffer, so we
  63. * have to take this into consideration here.
  64. */
  65. static unsigned int get_crtc_fence_y_offset(struct intel_crtc *crtc)
  66. {
  67. return crtc->base.y - crtc->adjusted_y;
  68. }
  69. /*
  70. * For SKL+, the plane source size used by the hardware is based on the value we
  71. * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
  72. * we wrote to PIPESRC.
  73. */
  74. static void intel_fbc_get_plane_source_size(struct intel_fbc_state_cache *cache,
  75. int *width, int *height)
  76. {
  77. if (width)
  78. *width = cache->plane.src_w;
  79. if (height)
  80. *height = cache->plane.src_h;
  81. }
  82. static int intel_fbc_calculate_cfb_size(struct drm_i915_private *dev_priv,
  83. struct intel_fbc_state_cache *cache)
  84. {
  85. int lines;
  86. intel_fbc_get_plane_source_size(cache, NULL, &lines);
  87. if (INTEL_GEN(dev_priv) == 7)
  88. lines = min(lines, 2048);
  89. else if (INTEL_GEN(dev_priv) >= 8)
  90. lines = min(lines, 2560);
  91. /* Hardware needs the full buffer stride, not just the active area. */
  92. return lines * cache->fb.stride;
  93. }
  94. static void i8xx_fbc_deactivate(struct drm_i915_private *dev_priv)
  95. {
  96. u32 fbc_ctl;
  97. /* Disable compression */
  98. fbc_ctl = I915_READ(FBC_CONTROL);
  99. if ((fbc_ctl & FBC_CTL_EN) == 0)
  100. return;
  101. fbc_ctl &= ~FBC_CTL_EN;
  102. I915_WRITE(FBC_CONTROL, fbc_ctl);
  103. /* Wait for compressing bit to clear */
  104. if (intel_wait_for_register(dev_priv,
  105. FBC_STATUS, FBC_STAT_COMPRESSING, 0,
  106. 10)) {
  107. DRM_DEBUG_KMS("FBC idle timed out\n");
  108. return;
  109. }
  110. }
  111. static void i8xx_fbc_activate(struct drm_i915_private *dev_priv)
  112. {
  113. struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
  114. int cfb_pitch;
  115. int i;
  116. u32 fbc_ctl;
  117. /* Note: fbc.threshold == 1 for i8xx */
  118. cfb_pitch = params->cfb_size / FBC_LL_SIZE;
  119. if (params->fb.stride < cfb_pitch)
  120. cfb_pitch = params->fb.stride;
  121. /* FBC_CTL wants 32B or 64B units */
  122. if (IS_GEN2(dev_priv))
  123. cfb_pitch = (cfb_pitch / 32) - 1;
  124. else
  125. cfb_pitch = (cfb_pitch / 64) - 1;
  126. /* Clear old tags */
  127. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  128. I915_WRITE(FBC_TAG(i), 0);
  129. if (IS_GEN4(dev_priv)) {
  130. u32 fbc_ctl2;
  131. /* Set it up... */
  132. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  133. fbc_ctl2 |= FBC_CTL_PLANE(params->crtc.plane);
  134. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  135. I915_WRITE(FBC_FENCE_OFF, params->crtc.fence_y_offset);
  136. }
  137. /* enable it... */
  138. fbc_ctl = I915_READ(FBC_CONTROL);
  139. fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
  140. fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
  141. if (IS_I945GM(dev_priv))
  142. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  143. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  144. fbc_ctl |= params->vma->fence->id;
  145. I915_WRITE(FBC_CONTROL, fbc_ctl);
  146. }
  147. static bool i8xx_fbc_is_active(struct drm_i915_private *dev_priv)
  148. {
  149. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  150. }
  151. static void g4x_fbc_activate(struct drm_i915_private *dev_priv)
  152. {
  153. struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
  154. u32 dpfc_ctl;
  155. dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane) | DPFC_SR_EN;
  156. if (params->fb.format->cpp[0] == 2)
  157. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  158. else
  159. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  160. if (params->vma->fence) {
  161. dpfc_ctl |= DPFC_CTL_FENCE_EN | params->vma->fence->id;
  162. I915_WRITE(DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
  163. } else {
  164. I915_WRITE(DPFC_FENCE_YOFF, 0);
  165. }
  166. /* enable it... */
  167. I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  168. }
  169. static void g4x_fbc_deactivate(struct drm_i915_private *dev_priv)
  170. {
  171. u32 dpfc_ctl;
  172. /* Disable compression */
  173. dpfc_ctl = I915_READ(DPFC_CONTROL);
  174. if (dpfc_ctl & DPFC_CTL_EN) {
  175. dpfc_ctl &= ~DPFC_CTL_EN;
  176. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  177. }
  178. }
  179. static bool g4x_fbc_is_active(struct drm_i915_private *dev_priv)
  180. {
  181. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  182. }
  183. /* This function forces a CFB recompression through the nuke operation. */
  184. static void intel_fbc_recompress(struct drm_i915_private *dev_priv)
  185. {
  186. I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
  187. POSTING_READ(MSG_FBC_REND_STATE);
  188. }
  189. static void ilk_fbc_activate(struct drm_i915_private *dev_priv)
  190. {
  191. struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
  192. u32 dpfc_ctl;
  193. int threshold = dev_priv->fbc.threshold;
  194. dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane);
  195. if (params->fb.format->cpp[0] == 2)
  196. threshold++;
  197. switch (threshold) {
  198. case 4:
  199. case 3:
  200. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  201. break;
  202. case 2:
  203. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  204. break;
  205. case 1:
  206. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  207. break;
  208. }
  209. if (params->vma->fence) {
  210. dpfc_ctl |= DPFC_CTL_FENCE_EN;
  211. if (IS_GEN5(dev_priv))
  212. dpfc_ctl |= params->vma->fence->id;
  213. if (IS_GEN6(dev_priv)) {
  214. I915_WRITE(SNB_DPFC_CTL_SA,
  215. SNB_CPU_FENCE_ENABLE |
  216. params->vma->fence->id);
  217. I915_WRITE(DPFC_CPU_FENCE_OFFSET,
  218. params->crtc.fence_y_offset);
  219. }
  220. } else {
  221. if (IS_GEN6(dev_priv)) {
  222. I915_WRITE(SNB_DPFC_CTL_SA, 0);
  223. I915_WRITE(DPFC_CPU_FENCE_OFFSET, 0);
  224. }
  225. }
  226. I915_WRITE(ILK_DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
  227. I915_WRITE(ILK_FBC_RT_BASE,
  228. i915_ggtt_offset(params->vma) | ILK_FBC_RT_VALID);
  229. /* enable it... */
  230. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  231. intel_fbc_recompress(dev_priv);
  232. }
  233. static void ilk_fbc_deactivate(struct drm_i915_private *dev_priv)
  234. {
  235. u32 dpfc_ctl;
  236. /* Disable compression */
  237. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  238. if (dpfc_ctl & DPFC_CTL_EN) {
  239. dpfc_ctl &= ~DPFC_CTL_EN;
  240. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  241. }
  242. }
  243. static bool ilk_fbc_is_active(struct drm_i915_private *dev_priv)
  244. {
  245. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  246. }
  247. static void gen7_fbc_activate(struct drm_i915_private *dev_priv)
  248. {
  249. struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
  250. u32 dpfc_ctl;
  251. int threshold = dev_priv->fbc.threshold;
  252. dpfc_ctl = 0;
  253. if (IS_IVYBRIDGE(dev_priv))
  254. dpfc_ctl |= IVB_DPFC_CTL_PLANE(params->crtc.plane);
  255. if (params->fb.format->cpp[0] == 2)
  256. threshold++;
  257. switch (threshold) {
  258. case 4:
  259. case 3:
  260. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  261. break;
  262. case 2:
  263. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  264. break;
  265. case 1:
  266. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  267. break;
  268. }
  269. if (params->vma->fence) {
  270. dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
  271. I915_WRITE(SNB_DPFC_CTL_SA,
  272. SNB_CPU_FENCE_ENABLE |
  273. params->vma->fence->id);
  274. I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset);
  275. } else {
  276. I915_WRITE(SNB_DPFC_CTL_SA,0);
  277. I915_WRITE(DPFC_CPU_FENCE_OFFSET, 0);
  278. }
  279. if (dev_priv->fbc.false_color)
  280. dpfc_ctl |= FBC_CTL_FALSE_COLOR;
  281. if (IS_IVYBRIDGE(dev_priv)) {
  282. /* WaFbcAsynchFlipDisableFbcQueue:ivb */
  283. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  284. I915_READ(ILK_DISPLAY_CHICKEN1) |
  285. ILK_FBCQ_DIS);
  286. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  287. /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
  288. I915_WRITE(CHICKEN_PIPESL_1(params->crtc.pipe),
  289. I915_READ(CHICKEN_PIPESL_1(params->crtc.pipe)) |
  290. HSW_FBCQ_DIS);
  291. }
  292. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  293. intel_fbc_recompress(dev_priv);
  294. }
  295. static bool intel_fbc_hw_is_active(struct drm_i915_private *dev_priv)
  296. {
  297. if (INTEL_GEN(dev_priv) >= 5)
  298. return ilk_fbc_is_active(dev_priv);
  299. else if (IS_GM45(dev_priv))
  300. return g4x_fbc_is_active(dev_priv);
  301. else
  302. return i8xx_fbc_is_active(dev_priv);
  303. }
  304. static void intel_fbc_hw_activate(struct drm_i915_private *dev_priv)
  305. {
  306. struct intel_fbc *fbc = &dev_priv->fbc;
  307. fbc->active = true;
  308. if (INTEL_GEN(dev_priv) >= 7)
  309. gen7_fbc_activate(dev_priv);
  310. else if (INTEL_GEN(dev_priv) >= 5)
  311. ilk_fbc_activate(dev_priv);
  312. else if (IS_GM45(dev_priv))
  313. g4x_fbc_activate(dev_priv);
  314. else
  315. i8xx_fbc_activate(dev_priv);
  316. }
  317. static void intel_fbc_hw_deactivate(struct drm_i915_private *dev_priv)
  318. {
  319. struct intel_fbc *fbc = &dev_priv->fbc;
  320. fbc->active = false;
  321. if (INTEL_GEN(dev_priv) >= 5)
  322. ilk_fbc_deactivate(dev_priv);
  323. else if (IS_GM45(dev_priv))
  324. g4x_fbc_deactivate(dev_priv);
  325. else
  326. i8xx_fbc_deactivate(dev_priv);
  327. }
  328. /**
  329. * intel_fbc_is_active - Is FBC active?
  330. * @dev_priv: i915 device instance
  331. *
  332. * This function is used to verify the current state of FBC.
  333. *
  334. * FIXME: This should be tracked in the plane config eventually
  335. * instead of queried at runtime for most callers.
  336. */
  337. bool intel_fbc_is_active(struct drm_i915_private *dev_priv)
  338. {
  339. return dev_priv->fbc.active;
  340. }
  341. static void intel_fbc_work_fn(struct work_struct *__work)
  342. {
  343. struct drm_i915_private *dev_priv =
  344. container_of(__work, struct drm_i915_private, fbc.work.work);
  345. struct intel_fbc *fbc = &dev_priv->fbc;
  346. struct intel_fbc_work *work = &fbc->work;
  347. struct intel_crtc *crtc = fbc->crtc;
  348. struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[crtc->pipe];
  349. if (drm_crtc_vblank_get(&crtc->base)) {
  350. DRM_ERROR("vblank not available for FBC on pipe %c\n",
  351. pipe_name(crtc->pipe));
  352. mutex_lock(&fbc->lock);
  353. work->scheduled = false;
  354. mutex_unlock(&fbc->lock);
  355. return;
  356. }
  357. retry:
  358. /* Delay the actual enabling to let pageflipping cease and the
  359. * display to settle before starting the compression. Note that
  360. * this delay also serves a second purpose: it allows for a
  361. * vblank to pass after disabling the FBC before we attempt
  362. * to modify the control registers.
  363. *
  364. * WaFbcWaitForVBlankBeforeEnable:ilk,snb
  365. *
  366. * It is also worth mentioning that since work->scheduled_vblank can be
  367. * updated multiple times by the other threads, hitting the timeout is
  368. * not an error condition. We'll just end up hitting the "goto retry"
  369. * case below.
  370. */
  371. wait_event_timeout(vblank->queue,
  372. drm_crtc_vblank_count(&crtc->base) != work->scheduled_vblank,
  373. msecs_to_jiffies(50));
  374. mutex_lock(&fbc->lock);
  375. /* Were we cancelled? */
  376. if (!work->scheduled)
  377. goto out;
  378. /* Were we delayed again while this function was sleeping? */
  379. if (drm_crtc_vblank_count(&crtc->base) == work->scheduled_vblank) {
  380. mutex_unlock(&fbc->lock);
  381. goto retry;
  382. }
  383. intel_fbc_hw_activate(dev_priv);
  384. work->scheduled = false;
  385. out:
  386. mutex_unlock(&fbc->lock);
  387. drm_crtc_vblank_put(&crtc->base);
  388. }
  389. static void intel_fbc_schedule_activation(struct intel_crtc *crtc)
  390. {
  391. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  392. struct intel_fbc *fbc = &dev_priv->fbc;
  393. struct intel_fbc_work *work = &fbc->work;
  394. WARN_ON(!mutex_is_locked(&fbc->lock));
  395. if (WARN_ON(!fbc->enabled))
  396. return;
  397. if (drm_crtc_vblank_get(&crtc->base)) {
  398. DRM_ERROR("vblank not available for FBC on pipe %c\n",
  399. pipe_name(crtc->pipe));
  400. return;
  401. }
  402. /* It is useless to call intel_fbc_cancel_work() or cancel_work() in
  403. * this function since we're not releasing fbc.lock, so it won't have an
  404. * opportunity to grab it to discover that it was cancelled. So we just
  405. * update the expected jiffy count. */
  406. work->scheduled = true;
  407. work->scheduled_vblank = drm_crtc_vblank_count(&crtc->base);
  408. drm_crtc_vblank_put(&crtc->base);
  409. schedule_work(&work->work);
  410. }
  411. static void intel_fbc_deactivate(struct drm_i915_private *dev_priv)
  412. {
  413. struct intel_fbc *fbc = &dev_priv->fbc;
  414. WARN_ON(!mutex_is_locked(&fbc->lock));
  415. /* Calling cancel_work() here won't help due to the fact that the work
  416. * function grabs fbc->lock. Just set scheduled to false so the work
  417. * function can know it was cancelled. */
  418. fbc->work.scheduled = false;
  419. if (fbc->active)
  420. intel_fbc_hw_deactivate(dev_priv);
  421. }
  422. static bool multiple_pipes_ok(struct intel_crtc *crtc,
  423. struct intel_plane_state *plane_state)
  424. {
  425. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  426. struct intel_fbc *fbc = &dev_priv->fbc;
  427. enum pipe pipe = crtc->pipe;
  428. /* Don't even bother tracking anything we don't need. */
  429. if (!no_fbc_on_multiple_pipes(dev_priv))
  430. return true;
  431. if (plane_state->base.visible)
  432. fbc->visible_pipes_mask |= (1 << pipe);
  433. else
  434. fbc->visible_pipes_mask &= ~(1 << pipe);
  435. return (fbc->visible_pipes_mask & ~(1 << pipe)) != 0;
  436. }
  437. static int find_compression_threshold(struct drm_i915_private *dev_priv,
  438. struct drm_mm_node *node,
  439. int size,
  440. int fb_cpp)
  441. {
  442. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  443. int compression_threshold = 1;
  444. int ret;
  445. u64 end;
  446. /* The FBC hardware for BDW/SKL doesn't have access to the stolen
  447. * reserved range size, so it always assumes the maximum (8mb) is used.
  448. * If we enable FBC using a CFB on that memory range we'll get FIFO
  449. * underruns, even if that range is not reserved by the BIOS. */
  450. if (IS_BROADWELL(dev_priv) || IS_GEN9_BC(dev_priv))
  451. end = ggtt->stolen_size - 8 * 1024 * 1024;
  452. else
  453. end = U64_MAX;
  454. /* HACK: This code depends on what we will do in *_enable_fbc. If that
  455. * code changes, this code needs to change as well.
  456. *
  457. * The enable_fbc code will attempt to use one of our 2 compression
  458. * thresholds, therefore, in that case, we only have 1 resort.
  459. */
  460. /* Try to over-allocate to reduce reallocations and fragmentation. */
  461. ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
  462. 4096, 0, end);
  463. if (ret == 0)
  464. return compression_threshold;
  465. again:
  466. /* HW's ability to limit the CFB is 1:4 */
  467. if (compression_threshold > 4 ||
  468. (fb_cpp == 2 && compression_threshold == 2))
  469. return 0;
  470. ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
  471. 4096, 0, end);
  472. if (ret && INTEL_GEN(dev_priv) <= 4) {
  473. return 0;
  474. } else if (ret) {
  475. compression_threshold <<= 1;
  476. goto again;
  477. } else {
  478. return compression_threshold;
  479. }
  480. }
  481. static int intel_fbc_alloc_cfb(struct intel_crtc *crtc)
  482. {
  483. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  484. struct intel_fbc *fbc = &dev_priv->fbc;
  485. struct drm_mm_node *uninitialized_var(compressed_llb);
  486. int size, fb_cpp, ret;
  487. WARN_ON(drm_mm_node_allocated(&fbc->compressed_fb));
  488. size = intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache);
  489. fb_cpp = fbc->state_cache.fb.format->cpp[0];
  490. ret = find_compression_threshold(dev_priv, &fbc->compressed_fb,
  491. size, fb_cpp);
  492. if (!ret)
  493. goto err_llb;
  494. else if (ret > 1) {
  495. DRM_INFO("Reducing the compressed framebuffer size. This may lead to less power savings than a non-reduced-size. Try to increase stolen memory size if available in BIOS.\n");
  496. }
  497. fbc->threshold = ret;
  498. if (INTEL_GEN(dev_priv) >= 5)
  499. I915_WRITE(ILK_DPFC_CB_BASE, fbc->compressed_fb.start);
  500. else if (IS_GM45(dev_priv)) {
  501. I915_WRITE(DPFC_CB_BASE, fbc->compressed_fb.start);
  502. } else {
  503. compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
  504. if (!compressed_llb)
  505. goto err_fb;
  506. ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
  507. 4096, 4096);
  508. if (ret)
  509. goto err_fb;
  510. fbc->compressed_llb = compressed_llb;
  511. I915_WRITE(FBC_CFB_BASE,
  512. dev_priv->mm.stolen_base + fbc->compressed_fb.start);
  513. I915_WRITE(FBC_LL_BASE,
  514. dev_priv->mm.stolen_base + compressed_llb->start);
  515. }
  516. DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
  517. fbc->compressed_fb.size, fbc->threshold);
  518. return 0;
  519. err_fb:
  520. kfree(compressed_llb);
  521. i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
  522. err_llb:
  523. if (drm_mm_initialized(&dev_priv->mm.stolen))
  524. pr_info_once("drm: not enough stolen space for compressed buffer (need %d more bytes), disabling. Hint: you may be able to increase stolen memory size in the BIOS to avoid this.\n", size);
  525. return -ENOSPC;
  526. }
  527. static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
  528. {
  529. struct intel_fbc *fbc = &dev_priv->fbc;
  530. if (drm_mm_node_allocated(&fbc->compressed_fb))
  531. i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
  532. if (fbc->compressed_llb) {
  533. i915_gem_stolen_remove_node(dev_priv, fbc->compressed_llb);
  534. kfree(fbc->compressed_llb);
  535. }
  536. }
  537. void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
  538. {
  539. struct intel_fbc *fbc = &dev_priv->fbc;
  540. if (!fbc_supported(dev_priv))
  541. return;
  542. mutex_lock(&fbc->lock);
  543. __intel_fbc_cleanup_cfb(dev_priv);
  544. mutex_unlock(&fbc->lock);
  545. }
  546. static bool stride_is_valid(struct drm_i915_private *dev_priv,
  547. unsigned int stride)
  548. {
  549. /* These should have been caught earlier. */
  550. WARN_ON(stride < 512);
  551. WARN_ON((stride & (64 - 1)) != 0);
  552. /* Below are the additional FBC restrictions. */
  553. if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
  554. return stride == 4096 || stride == 8192;
  555. if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
  556. return false;
  557. if (stride > 16384)
  558. return false;
  559. return true;
  560. }
  561. static bool pixel_format_is_valid(struct drm_i915_private *dev_priv,
  562. uint32_t pixel_format)
  563. {
  564. switch (pixel_format) {
  565. case DRM_FORMAT_XRGB8888:
  566. case DRM_FORMAT_XBGR8888:
  567. return true;
  568. case DRM_FORMAT_XRGB1555:
  569. case DRM_FORMAT_RGB565:
  570. /* 16bpp not supported on gen2 */
  571. if (IS_GEN2(dev_priv))
  572. return false;
  573. /* WaFbcOnly1to1Ratio:ctg */
  574. if (IS_G4X(dev_priv))
  575. return false;
  576. return true;
  577. default:
  578. return false;
  579. }
  580. }
  581. /*
  582. * For some reason, the hardware tracking starts looking at whatever we
  583. * programmed as the display plane base address register. It does not look at
  584. * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
  585. * variables instead of just looking at the pipe/plane size.
  586. */
  587. static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
  588. {
  589. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  590. struct intel_fbc *fbc = &dev_priv->fbc;
  591. unsigned int effective_w, effective_h, max_w, max_h;
  592. if (INTEL_GEN(dev_priv) >= 8 || IS_HASWELL(dev_priv)) {
  593. max_w = 4096;
  594. max_h = 4096;
  595. } else if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
  596. max_w = 4096;
  597. max_h = 2048;
  598. } else {
  599. max_w = 2048;
  600. max_h = 1536;
  601. }
  602. intel_fbc_get_plane_source_size(&fbc->state_cache, &effective_w,
  603. &effective_h);
  604. effective_w += crtc->adjusted_x;
  605. effective_h += crtc->adjusted_y;
  606. return effective_w <= max_w && effective_h <= max_h;
  607. }
  608. static void intel_fbc_update_state_cache(struct intel_crtc *crtc,
  609. struct intel_crtc_state *crtc_state,
  610. struct intel_plane_state *plane_state)
  611. {
  612. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  613. struct intel_fbc *fbc = &dev_priv->fbc;
  614. struct intel_fbc_state_cache *cache = &fbc->state_cache;
  615. struct drm_framebuffer *fb = plane_state->base.fb;
  616. cache->vma = NULL;
  617. cache->crtc.mode_flags = crtc_state->base.adjusted_mode.flags;
  618. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  619. cache->crtc.hsw_bdw_pixel_rate = crtc_state->pixel_rate;
  620. cache->plane.rotation = plane_state->base.rotation;
  621. /*
  622. * Src coordinates are already rotated by 270 degrees for
  623. * the 90/270 degree plane rotation cases (to match the
  624. * GTT mapping), hence no need to account for rotation here.
  625. */
  626. cache->plane.src_w = drm_rect_width(&plane_state->base.src) >> 16;
  627. cache->plane.src_h = drm_rect_height(&plane_state->base.src) >> 16;
  628. cache->plane.visible = plane_state->base.visible;
  629. if (!cache->plane.visible)
  630. return;
  631. cache->fb.format = fb->format;
  632. cache->fb.stride = fb->pitches[0];
  633. cache->vma = plane_state->vma;
  634. }
  635. static bool intel_fbc_can_activate(struct intel_crtc *crtc)
  636. {
  637. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  638. struct intel_fbc *fbc = &dev_priv->fbc;
  639. struct intel_fbc_state_cache *cache = &fbc->state_cache;
  640. /* We don't need to use a state cache here since this information is
  641. * global for all CRTC.
  642. */
  643. if (fbc->underrun_detected) {
  644. fbc->no_fbc_reason = "underrun detected";
  645. return false;
  646. }
  647. if (!cache->vma) {
  648. fbc->no_fbc_reason = "primary plane not visible";
  649. return false;
  650. }
  651. if ((cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) ||
  652. (cache->crtc.mode_flags & DRM_MODE_FLAG_DBLSCAN)) {
  653. fbc->no_fbc_reason = "incompatible mode";
  654. return false;
  655. }
  656. if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
  657. fbc->no_fbc_reason = "mode too large for compression";
  658. return false;
  659. }
  660. /* The use of a CPU fence is mandatory in order to detect writes
  661. * by the CPU to the scanout and trigger updates to the FBC.
  662. *
  663. * Note that is possible for a tiled surface to be unmappable (and
  664. * so have no fence associated with it) due to aperture constaints
  665. * at the time of pinning.
  666. */
  667. if (!cache->vma->fence) {
  668. fbc->no_fbc_reason = "framebuffer not tiled or fenced";
  669. return false;
  670. }
  671. if (INTEL_GEN(dev_priv) <= 4 && !IS_G4X(dev_priv) &&
  672. cache->plane.rotation != DRM_MODE_ROTATE_0) {
  673. fbc->no_fbc_reason = "rotation unsupported";
  674. return false;
  675. }
  676. if (!stride_is_valid(dev_priv, cache->fb.stride)) {
  677. fbc->no_fbc_reason = "framebuffer stride not supported";
  678. return false;
  679. }
  680. if (!pixel_format_is_valid(dev_priv, cache->fb.format->format)) {
  681. fbc->no_fbc_reason = "pixel format is invalid";
  682. return false;
  683. }
  684. /* WaFbcExceedCdClockThreshold:hsw,bdw */
  685. if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
  686. cache->crtc.hsw_bdw_pixel_rate >= dev_priv->cdclk.hw.cdclk * 95 / 100) {
  687. fbc->no_fbc_reason = "pixel rate is too big";
  688. return false;
  689. }
  690. /* It is possible for the required CFB size change without a
  691. * crtc->disable + crtc->enable since it is possible to change the
  692. * stride without triggering a full modeset. Since we try to
  693. * over-allocate the CFB, there's a chance we may keep FBC enabled even
  694. * if this happens, but if we exceed the current CFB size we'll have to
  695. * disable FBC. Notice that it would be possible to disable FBC, wait
  696. * for a frame, free the stolen node, then try to reenable FBC in case
  697. * we didn't get any invalidate/deactivate calls, but this would require
  698. * a lot of tracking just for a specific case. If we conclude it's an
  699. * important case, we can implement it later. */
  700. if (intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache) >
  701. fbc->compressed_fb.size * fbc->threshold) {
  702. fbc->no_fbc_reason = "CFB requirements changed";
  703. return false;
  704. }
  705. return true;
  706. }
  707. static bool intel_fbc_can_enable(struct drm_i915_private *dev_priv)
  708. {
  709. struct intel_fbc *fbc = &dev_priv->fbc;
  710. if (intel_vgpu_active(dev_priv)) {
  711. fbc->no_fbc_reason = "VGPU is active";
  712. return false;
  713. }
  714. if (!i915.enable_fbc) {
  715. fbc->no_fbc_reason = "disabled per module param or by default";
  716. return false;
  717. }
  718. if (fbc->underrun_detected) {
  719. fbc->no_fbc_reason = "underrun detected";
  720. return false;
  721. }
  722. return true;
  723. }
  724. static void intel_fbc_get_reg_params(struct intel_crtc *crtc,
  725. struct intel_fbc_reg_params *params)
  726. {
  727. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  728. struct intel_fbc *fbc = &dev_priv->fbc;
  729. struct intel_fbc_state_cache *cache = &fbc->state_cache;
  730. /* Since all our fields are integer types, use memset here so the
  731. * comparison function can rely on memcmp because the padding will be
  732. * zero. */
  733. memset(params, 0, sizeof(*params));
  734. params->vma = cache->vma;
  735. params->crtc.pipe = crtc->pipe;
  736. params->crtc.plane = crtc->plane;
  737. params->crtc.fence_y_offset = get_crtc_fence_y_offset(crtc);
  738. params->fb.format = cache->fb.format;
  739. params->fb.stride = cache->fb.stride;
  740. params->cfb_size = intel_fbc_calculate_cfb_size(dev_priv, cache);
  741. }
  742. static bool intel_fbc_reg_params_equal(struct intel_fbc_reg_params *params1,
  743. struct intel_fbc_reg_params *params2)
  744. {
  745. /* We can use this since intel_fbc_get_reg_params() does a memset. */
  746. return memcmp(params1, params2, sizeof(*params1)) == 0;
  747. }
  748. void intel_fbc_pre_update(struct intel_crtc *crtc,
  749. struct intel_crtc_state *crtc_state,
  750. struct intel_plane_state *plane_state)
  751. {
  752. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  753. struct intel_fbc *fbc = &dev_priv->fbc;
  754. if (!fbc_supported(dev_priv))
  755. return;
  756. mutex_lock(&fbc->lock);
  757. if (!multiple_pipes_ok(crtc, plane_state)) {
  758. fbc->no_fbc_reason = "more than one pipe active";
  759. goto deactivate;
  760. }
  761. if (!fbc->enabled || fbc->crtc != crtc)
  762. goto unlock;
  763. intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
  764. deactivate:
  765. intel_fbc_deactivate(dev_priv);
  766. unlock:
  767. mutex_unlock(&fbc->lock);
  768. }
  769. static void __intel_fbc_post_update(struct intel_crtc *crtc)
  770. {
  771. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  772. struct intel_fbc *fbc = &dev_priv->fbc;
  773. struct intel_fbc_reg_params old_params;
  774. WARN_ON(!mutex_is_locked(&fbc->lock));
  775. if (!fbc->enabled || fbc->crtc != crtc)
  776. return;
  777. if (!intel_fbc_can_activate(crtc)) {
  778. WARN_ON(fbc->active);
  779. return;
  780. }
  781. old_params = fbc->params;
  782. intel_fbc_get_reg_params(crtc, &fbc->params);
  783. /* If the scanout has not changed, don't modify the FBC settings.
  784. * Note that we make the fundamental assumption that the fb->obj
  785. * cannot be unpinned (and have its GTT offset and fence revoked)
  786. * without first being decoupled from the scanout and FBC disabled.
  787. */
  788. if (fbc->active &&
  789. intel_fbc_reg_params_equal(&old_params, &fbc->params))
  790. return;
  791. intel_fbc_deactivate(dev_priv);
  792. intel_fbc_schedule_activation(crtc);
  793. fbc->no_fbc_reason = "FBC enabled (active or scheduled)";
  794. }
  795. void intel_fbc_post_update(struct intel_crtc *crtc)
  796. {
  797. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  798. struct intel_fbc *fbc = &dev_priv->fbc;
  799. if (!fbc_supported(dev_priv))
  800. return;
  801. mutex_lock(&fbc->lock);
  802. __intel_fbc_post_update(crtc);
  803. mutex_unlock(&fbc->lock);
  804. }
  805. static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
  806. {
  807. if (fbc->enabled)
  808. return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit;
  809. else
  810. return fbc->possible_framebuffer_bits;
  811. }
  812. void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
  813. unsigned int frontbuffer_bits,
  814. enum fb_op_origin origin)
  815. {
  816. struct intel_fbc *fbc = &dev_priv->fbc;
  817. if (!fbc_supported(dev_priv))
  818. return;
  819. if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
  820. return;
  821. mutex_lock(&fbc->lock);
  822. fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits;
  823. if (fbc->enabled && fbc->busy_bits)
  824. intel_fbc_deactivate(dev_priv);
  825. mutex_unlock(&fbc->lock);
  826. }
  827. void intel_fbc_flush(struct drm_i915_private *dev_priv,
  828. unsigned int frontbuffer_bits, enum fb_op_origin origin)
  829. {
  830. struct intel_fbc *fbc = &dev_priv->fbc;
  831. if (!fbc_supported(dev_priv))
  832. return;
  833. mutex_lock(&fbc->lock);
  834. fbc->busy_bits &= ~frontbuffer_bits;
  835. if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
  836. goto out;
  837. if (!fbc->busy_bits && fbc->enabled &&
  838. (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) {
  839. if (fbc->active)
  840. intel_fbc_recompress(dev_priv);
  841. else
  842. __intel_fbc_post_update(fbc->crtc);
  843. }
  844. out:
  845. mutex_unlock(&fbc->lock);
  846. }
  847. /**
  848. * intel_fbc_choose_crtc - select a CRTC to enable FBC on
  849. * @dev_priv: i915 device instance
  850. * @state: the atomic state structure
  851. *
  852. * This function looks at the proposed state for CRTCs and planes, then chooses
  853. * which pipe is going to have FBC by setting intel_crtc_state->enable_fbc to
  854. * true.
  855. *
  856. * Later, intel_fbc_enable is going to look for state->enable_fbc and then maybe
  857. * enable FBC for the chosen CRTC. If it does, it will set dev_priv->fbc.crtc.
  858. */
  859. void intel_fbc_choose_crtc(struct drm_i915_private *dev_priv,
  860. struct drm_atomic_state *state)
  861. {
  862. struct intel_fbc *fbc = &dev_priv->fbc;
  863. struct drm_plane *plane;
  864. struct drm_plane_state *plane_state;
  865. bool crtc_chosen = false;
  866. int i;
  867. mutex_lock(&fbc->lock);
  868. /* Does this atomic commit involve the CRTC currently tied to FBC? */
  869. if (fbc->crtc &&
  870. !drm_atomic_get_existing_crtc_state(state, &fbc->crtc->base))
  871. goto out;
  872. if (!intel_fbc_can_enable(dev_priv))
  873. goto out;
  874. /* Simply choose the first CRTC that is compatible and has a visible
  875. * plane. We could go for fancier schemes such as checking the plane
  876. * size, but this would just affect the few platforms that don't tie FBC
  877. * to pipe or plane A. */
  878. for_each_new_plane_in_state(state, plane, plane_state, i) {
  879. struct intel_plane_state *intel_plane_state =
  880. to_intel_plane_state(plane_state);
  881. struct intel_crtc_state *intel_crtc_state;
  882. struct intel_crtc *crtc = to_intel_crtc(plane_state->crtc);
  883. if (!intel_plane_state->base.visible)
  884. continue;
  885. if (fbc_on_pipe_a_only(dev_priv) && crtc->pipe != PIPE_A)
  886. continue;
  887. if (fbc_on_plane_a_only(dev_priv) && crtc->plane != PLANE_A)
  888. continue;
  889. intel_crtc_state = to_intel_crtc_state(
  890. drm_atomic_get_existing_crtc_state(state, &crtc->base));
  891. intel_crtc_state->enable_fbc = true;
  892. crtc_chosen = true;
  893. break;
  894. }
  895. if (!crtc_chosen)
  896. fbc->no_fbc_reason = "no suitable CRTC for FBC";
  897. out:
  898. mutex_unlock(&fbc->lock);
  899. }
  900. /**
  901. * intel_fbc_enable: tries to enable FBC on the CRTC
  902. * @crtc: the CRTC
  903. * @crtc_state: corresponding &drm_crtc_state for @crtc
  904. * @plane_state: corresponding &drm_plane_state for the primary plane of @crtc
  905. *
  906. * This function checks if the given CRTC was chosen for FBC, then enables it if
  907. * possible. Notice that it doesn't activate FBC. It is valid to call
  908. * intel_fbc_enable multiple times for the same pipe without an
  909. * intel_fbc_disable in the middle, as long as it is deactivated.
  910. */
  911. void intel_fbc_enable(struct intel_crtc *crtc,
  912. struct intel_crtc_state *crtc_state,
  913. struct intel_plane_state *plane_state)
  914. {
  915. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  916. struct intel_fbc *fbc = &dev_priv->fbc;
  917. if (!fbc_supported(dev_priv))
  918. return;
  919. mutex_lock(&fbc->lock);
  920. if (fbc->enabled) {
  921. WARN_ON(fbc->crtc == NULL);
  922. if (fbc->crtc == crtc) {
  923. WARN_ON(!crtc_state->enable_fbc);
  924. WARN_ON(fbc->active);
  925. }
  926. goto out;
  927. }
  928. if (!crtc_state->enable_fbc)
  929. goto out;
  930. WARN_ON(fbc->active);
  931. WARN_ON(fbc->crtc != NULL);
  932. intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
  933. if (intel_fbc_alloc_cfb(crtc)) {
  934. fbc->no_fbc_reason = "not enough stolen memory";
  935. goto out;
  936. }
  937. DRM_DEBUG_KMS("Enabling FBC on pipe %c\n", pipe_name(crtc->pipe));
  938. fbc->no_fbc_reason = "FBC enabled but not active yet\n";
  939. fbc->enabled = true;
  940. fbc->crtc = crtc;
  941. out:
  942. mutex_unlock(&fbc->lock);
  943. }
  944. /**
  945. * __intel_fbc_disable - disable FBC
  946. * @dev_priv: i915 device instance
  947. *
  948. * This is the low level function that actually disables FBC. Callers should
  949. * grab the FBC lock.
  950. */
  951. static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
  952. {
  953. struct intel_fbc *fbc = &dev_priv->fbc;
  954. struct intel_crtc *crtc = fbc->crtc;
  955. WARN_ON(!mutex_is_locked(&fbc->lock));
  956. WARN_ON(!fbc->enabled);
  957. WARN_ON(fbc->active);
  958. WARN_ON(crtc->active);
  959. DRM_DEBUG_KMS("Disabling FBC on pipe %c\n", pipe_name(crtc->pipe));
  960. __intel_fbc_cleanup_cfb(dev_priv);
  961. fbc->enabled = false;
  962. fbc->crtc = NULL;
  963. }
  964. /**
  965. * intel_fbc_disable - disable FBC if it's associated with crtc
  966. * @crtc: the CRTC
  967. *
  968. * This function disables FBC if it's associated with the provided CRTC.
  969. */
  970. void intel_fbc_disable(struct intel_crtc *crtc)
  971. {
  972. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  973. struct intel_fbc *fbc = &dev_priv->fbc;
  974. if (!fbc_supported(dev_priv))
  975. return;
  976. mutex_lock(&fbc->lock);
  977. if (fbc->crtc == crtc)
  978. __intel_fbc_disable(dev_priv);
  979. mutex_unlock(&fbc->lock);
  980. cancel_work_sync(&fbc->work.work);
  981. }
  982. /**
  983. * intel_fbc_global_disable - globally disable FBC
  984. * @dev_priv: i915 device instance
  985. *
  986. * This function disables FBC regardless of which CRTC is associated with it.
  987. */
  988. void intel_fbc_global_disable(struct drm_i915_private *dev_priv)
  989. {
  990. struct intel_fbc *fbc = &dev_priv->fbc;
  991. if (!fbc_supported(dev_priv))
  992. return;
  993. mutex_lock(&fbc->lock);
  994. if (fbc->enabled)
  995. __intel_fbc_disable(dev_priv);
  996. mutex_unlock(&fbc->lock);
  997. cancel_work_sync(&fbc->work.work);
  998. }
  999. static void intel_fbc_underrun_work_fn(struct work_struct *work)
  1000. {
  1001. struct drm_i915_private *dev_priv =
  1002. container_of(work, struct drm_i915_private, fbc.underrun_work);
  1003. struct intel_fbc *fbc = &dev_priv->fbc;
  1004. mutex_lock(&fbc->lock);
  1005. /* Maybe we were scheduled twice. */
  1006. if (fbc->underrun_detected || !fbc->enabled)
  1007. goto out;
  1008. DRM_DEBUG_KMS("Disabling FBC due to FIFO underrun.\n");
  1009. fbc->underrun_detected = true;
  1010. intel_fbc_deactivate(dev_priv);
  1011. out:
  1012. mutex_unlock(&fbc->lock);
  1013. }
  1014. /**
  1015. * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
  1016. * @dev_priv: i915 device instance
  1017. *
  1018. * Without FBC, most underruns are harmless and don't really cause too many
  1019. * problems, except for an annoying message on dmesg. With FBC, underruns can
  1020. * become black screens or even worse, especially when paired with bad
  1021. * watermarks. So in order for us to be on the safe side, completely disable FBC
  1022. * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
  1023. * already suggests that watermarks may be bad, so try to be as safe as
  1024. * possible.
  1025. *
  1026. * This function is called from the IRQ handler.
  1027. */
  1028. void intel_fbc_handle_fifo_underrun_irq(struct drm_i915_private *dev_priv)
  1029. {
  1030. struct intel_fbc *fbc = &dev_priv->fbc;
  1031. if (!fbc_supported(dev_priv))
  1032. return;
  1033. /* There's no guarantee that underrun_detected won't be set to true
  1034. * right after this check and before the work is scheduled, but that's
  1035. * not a problem since we'll check it again under the work function
  1036. * while FBC is locked. This check here is just to prevent us from
  1037. * unnecessarily scheduling the work, and it relies on the fact that we
  1038. * never switch underrun_detect back to false after it's true. */
  1039. if (READ_ONCE(fbc->underrun_detected))
  1040. return;
  1041. schedule_work(&fbc->underrun_work);
  1042. }
  1043. /**
  1044. * intel_fbc_init_pipe_state - initialize FBC's CRTC visibility tracking
  1045. * @dev_priv: i915 device instance
  1046. *
  1047. * The FBC code needs to track CRTC visibility since the older platforms can't
  1048. * have FBC enabled while multiple pipes are used. This function does the
  1049. * initial setup at driver load to make sure FBC is matching the real hardware.
  1050. */
  1051. void intel_fbc_init_pipe_state(struct drm_i915_private *dev_priv)
  1052. {
  1053. struct intel_crtc *crtc;
  1054. /* Don't even bother tracking anything if we don't need. */
  1055. if (!no_fbc_on_multiple_pipes(dev_priv))
  1056. return;
  1057. for_each_intel_crtc(&dev_priv->drm, crtc)
  1058. if (intel_crtc_active(crtc) &&
  1059. crtc->base.primary->state->visible)
  1060. dev_priv->fbc.visible_pipes_mask |= (1 << crtc->pipe);
  1061. }
  1062. /*
  1063. * The DDX driver changes its behavior depending on the value it reads from
  1064. * i915.enable_fbc, so sanitize it by translating the default value into either
  1065. * 0 or 1 in order to allow it to know what's going on.
  1066. *
  1067. * Notice that this is done at driver initialization and we still allow user
  1068. * space to change the value during runtime without sanitizing it again. IGT
  1069. * relies on being able to change i915.enable_fbc at runtime.
  1070. */
  1071. static int intel_sanitize_fbc_option(struct drm_i915_private *dev_priv)
  1072. {
  1073. if (i915.enable_fbc >= 0)
  1074. return !!i915.enable_fbc;
  1075. if (!HAS_FBC(dev_priv))
  1076. return 0;
  1077. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9)
  1078. return 1;
  1079. return 0;
  1080. }
  1081. static bool need_fbc_vtd_wa(struct drm_i915_private *dev_priv)
  1082. {
  1083. /* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
  1084. if (intel_vtd_active() &&
  1085. (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))) {
  1086. DRM_INFO("Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
  1087. return true;
  1088. }
  1089. return false;
  1090. }
  1091. /**
  1092. * intel_fbc_init - Initialize FBC
  1093. * @dev_priv: the i915 device
  1094. *
  1095. * This function might be called during PM init process.
  1096. */
  1097. void intel_fbc_init(struct drm_i915_private *dev_priv)
  1098. {
  1099. struct intel_fbc *fbc = &dev_priv->fbc;
  1100. enum pipe pipe;
  1101. INIT_WORK(&fbc->work.work, intel_fbc_work_fn);
  1102. INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
  1103. mutex_init(&fbc->lock);
  1104. fbc->enabled = false;
  1105. fbc->active = false;
  1106. fbc->work.scheduled = false;
  1107. if (need_fbc_vtd_wa(dev_priv))
  1108. mkwrite_device_info(dev_priv)->has_fbc = false;
  1109. i915.enable_fbc = intel_sanitize_fbc_option(dev_priv);
  1110. DRM_DEBUG_KMS("Sanitized enable_fbc value: %d\n", i915.enable_fbc);
  1111. if (!HAS_FBC(dev_priv)) {
  1112. fbc->no_fbc_reason = "unsupported by this chipset";
  1113. return;
  1114. }
  1115. for_each_pipe(dev_priv, pipe) {
  1116. fbc->possible_framebuffer_bits |=
  1117. INTEL_FRONTBUFFER_PRIMARY(pipe);
  1118. if (fbc_on_pipe_a_only(dev_priv))
  1119. break;
  1120. }
  1121. /* This value was pulled out of someone's hat */
  1122. if (INTEL_GEN(dev_priv) <= 4 && !IS_GM45(dev_priv))
  1123. I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
  1124. /* We still don't have any sort of hardware state readout for FBC, so
  1125. * deactivate it in case the BIOS activated it to make sure software
  1126. * matches the hardware state. */
  1127. if (intel_fbc_hw_is_active(dev_priv))
  1128. intel_fbc_hw_deactivate(dev_priv);
  1129. }