intel_fbc.c 37 KB

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