intel_fbc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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->main.x;
  567. cache->plane.adjusted_y = plane_state->main.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. /* https://bugs.freedesktop.org/show_bug.cgi?id=108085 */
  1050. if (IS_GEMINILAKE(dev_priv))
  1051. return 0;
  1052. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9)
  1053. return 1;
  1054. return 0;
  1055. }
  1056. static bool need_fbc_vtd_wa(struct drm_i915_private *dev_priv)
  1057. {
  1058. /* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
  1059. if (intel_vtd_active() &&
  1060. (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))) {
  1061. DRM_INFO("Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
  1062. return true;
  1063. }
  1064. return false;
  1065. }
  1066. /**
  1067. * intel_fbc_init - Initialize FBC
  1068. * @dev_priv: the i915 device
  1069. *
  1070. * This function might be called during PM init process.
  1071. */
  1072. void intel_fbc_init(struct drm_i915_private *dev_priv)
  1073. {
  1074. struct intel_fbc *fbc = &dev_priv->fbc;
  1075. INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
  1076. mutex_init(&fbc->lock);
  1077. fbc->enabled = false;
  1078. fbc->active = false;
  1079. if (need_fbc_vtd_wa(dev_priv))
  1080. mkwrite_device_info(dev_priv)->has_fbc = false;
  1081. i915_modparams.enable_fbc = intel_sanitize_fbc_option(dev_priv);
  1082. DRM_DEBUG_KMS("Sanitized enable_fbc value: %d\n",
  1083. i915_modparams.enable_fbc);
  1084. if (!HAS_FBC(dev_priv)) {
  1085. fbc->no_fbc_reason = "unsupported by this chipset";
  1086. return;
  1087. }
  1088. /* This value was pulled out of someone's hat */
  1089. if (INTEL_GEN(dev_priv) <= 4 && !IS_GM45(dev_priv))
  1090. I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
  1091. /* We still don't have any sort of hardware state readout for FBC, so
  1092. * deactivate it in case the BIOS activated it to make sure software
  1093. * matches the hardware state. */
  1094. if (intel_fbc_hw_is_active(dev_priv))
  1095. intel_fbc_hw_deactivate(dev_priv);
  1096. }