intel_fbc.c 38 KB

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