intel_fbc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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_work_fn(struct work_struct *__work)
  343. {
  344. struct drm_i915_private *dev_priv =
  345. container_of(__work, struct drm_i915_private, fbc.work.work);
  346. struct intel_fbc *fbc = &dev_priv->fbc;
  347. struct intel_fbc_work *work = &fbc->work;
  348. struct intel_crtc *crtc = fbc->crtc;
  349. struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[crtc->pipe];
  350. if (drm_crtc_vblank_get(&crtc->base)) {
  351. /* CRTC is now off, leave FBC deactivated */
  352. mutex_lock(&fbc->lock);
  353. work->scheduled = false;
  354. mutex_unlock(&fbc->lock);
  355. return;
  356. }
  357. retry:
  358. /* Delay the actual enabling to let pageflipping cease and the
  359. * display to settle before starting the compression. Note that
  360. * this delay also serves a second purpose: it allows for a
  361. * vblank to pass after disabling the FBC before we attempt
  362. * to modify the control registers.
  363. *
  364. * WaFbcWaitForVBlankBeforeEnable:ilk,snb
  365. *
  366. * It is also worth mentioning that since work->scheduled_vblank can be
  367. * updated multiple times by the other threads, hitting the timeout is
  368. * not an error condition. We'll just end up hitting the "goto retry"
  369. * case below.
  370. */
  371. wait_event_timeout(vblank->queue,
  372. drm_crtc_vblank_count(&crtc->base) != work->scheduled_vblank,
  373. msecs_to_jiffies(50));
  374. mutex_lock(&fbc->lock);
  375. /* Were we cancelled? */
  376. if (!work->scheduled)
  377. goto out;
  378. /* Were we delayed again while this function was sleeping? */
  379. if (drm_crtc_vblank_count(&crtc->base) == work->scheduled_vblank) {
  380. mutex_unlock(&fbc->lock);
  381. goto retry;
  382. }
  383. intel_fbc_hw_activate(dev_priv);
  384. work->scheduled = false;
  385. out:
  386. mutex_unlock(&fbc->lock);
  387. drm_crtc_vblank_put(&crtc->base);
  388. }
  389. static void intel_fbc_schedule_activation(struct intel_crtc *crtc)
  390. {
  391. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  392. struct intel_fbc *fbc = &dev_priv->fbc;
  393. struct intel_fbc_work *work = &fbc->work;
  394. WARN_ON(!mutex_is_locked(&fbc->lock));
  395. if (WARN_ON(!fbc->enabled))
  396. return;
  397. if (drm_crtc_vblank_get(&crtc->base)) {
  398. DRM_ERROR("vblank not available for FBC on pipe %c\n",
  399. pipe_name(crtc->pipe));
  400. return;
  401. }
  402. /* It is useless to call intel_fbc_cancel_work() or cancel_work() in
  403. * this function since we're not releasing fbc.lock, so it won't have an
  404. * opportunity to grab it to discover that it was cancelled. So we just
  405. * update the expected jiffy count. */
  406. work->scheduled = true;
  407. work->scheduled_vblank = drm_crtc_vblank_count(&crtc->base);
  408. drm_crtc_vblank_put(&crtc->base);
  409. schedule_work(&work->work);
  410. }
  411. static void intel_fbc_deactivate(struct drm_i915_private *dev_priv,
  412. const char *reason)
  413. {
  414. struct intel_fbc *fbc = &dev_priv->fbc;
  415. WARN_ON(!mutex_is_locked(&fbc->lock));
  416. /* Calling cancel_work() here won't help due to the fact that the work
  417. * function grabs fbc->lock. Just set scheduled to false so the work
  418. * function can know it was cancelled. */
  419. fbc->work.scheduled = false;
  420. if (fbc->active)
  421. intel_fbc_hw_deactivate(dev_priv);
  422. fbc->no_fbc_reason = reason;
  423. }
  424. static bool multiple_pipes_ok(struct intel_crtc *crtc,
  425. struct intel_plane_state *plane_state)
  426. {
  427. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  428. struct intel_fbc *fbc = &dev_priv->fbc;
  429. enum pipe pipe = crtc->pipe;
  430. /* Don't even bother tracking anything we don't need. */
  431. if (!no_fbc_on_multiple_pipes(dev_priv))
  432. return true;
  433. if (plane_state->base.visible)
  434. fbc->visible_pipes_mask |= (1 << pipe);
  435. else
  436. fbc->visible_pipes_mask &= ~(1 << pipe);
  437. return (fbc->visible_pipes_mask & ~(1 << pipe)) != 0;
  438. }
  439. static int find_compression_threshold(struct drm_i915_private *dev_priv,
  440. struct drm_mm_node *node,
  441. int size,
  442. int fb_cpp)
  443. {
  444. int compression_threshold = 1;
  445. int ret;
  446. u64 end;
  447. /* The FBC hardware for BDW/SKL doesn't have access to the stolen
  448. * reserved range size, so it always assumes the maximum (8mb) is used.
  449. * If we enable FBC using a CFB on that memory range we'll get FIFO
  450. * underruns, even if that range is not reserved by the BIOS. */
  451. if (IS_BROADWELL(dev_priv) || IS_GEN9_BC(dev_priv))
  452. end = resource_size(&dev_priv->dsm) - 8 * 1024 * 1024;
  453. else
  454. end = U64_MAX;
  455. /* HACK: This code depends on what we will do in *_enable_fbc. If that
  456. * code changes, this code needs to change as well.
  457. *
  458. * The enable_fbc code will attempt to use one of our 2 compression
  459. * thresholds, therefore, in that case, we only have 1 resort.
  460. */
  461. /* Try to over-allocate to reduce reallocations and fragmentation. */
  462. ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
  463. 4096, 0, end);
  464. if (ret == 0)
  465. return compression_threshold;
  466. again:
  467. /* HW's ability to limit the CFB is 1:4 */
  468. if (compression_threshold > 4 ||
  469. (fb_cpp == 2 && compression_threshold == 2))
  470. return 0;
  471. ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
  472. 4096, 0, end);
  473. if (ret && INTEL_GEN(dev_priv) <= 4) {
  474. return 0;
  475. } else if (ret) {
  476. compression_threshold <<= 1;
  477. goto again;
  478. } else {
  479. return compression_threshold;
  480. }
  481. }
  482. static int intel_fbc_alloc_cfb(struct intel_crtc *crtc)
  483. {
  484. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  485. struct intel_fbc *fbc = &dev_priv->fbc;
  486. struct drm_mm_node *uninitialized_var(compressed_llb);
  487. int size, fb_cpp, ret;
  488. WARN_ON(drm_mm_node_allocated(&fbc->compressed_fb));
  489. size = intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache);
  490. fb_cpp = fbc->state_cache.fb.format->cpp[0];
  491. ret = find_compression_threshold(dev_priv, &fbc->compressed_fb,
  492. size, fb_cpp);
  493. if (!ret)
  494. goto err_llb;
  495. else if (ret > 1) {
  496. 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");
  497. }
  498. fbc->threshold = ret;
  499. if (INTEL_GEN(dev_priv) >= 5)
  500. I915_WRITE(ILK_DPFC_CB_BASE, fbc->compressed_fb.start);
  501. else if (IS_GM45(dev_priv)) {
  502. I915_WRITE(DPFC_CB_BASE, fbc->compressed_fb.start);
  503. } else {
  504. compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
  505. if (!compressed_llb)
  506. goto err_fb;
  507. ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
  508. 4096, 4096);
  509. if (ret)
  510. goto err_fb;
  511. fbc->compressed_llb = compressed_llb;
  512. GEM_BUG_ON(range_overflows_t(u64, dev_priv->dsm.start,
  513. fbc->compressed_fb.start,
  514. U32_MAX));
  515. GEM_BUG_ON(range_overflows_t(u64, dev_priv->dsm.start,
  516. fbc->compressed_llb->start,
  517. U32_MAX));
  518. I915_WRITE(FBC_CFB_BASE,
  519. dev_priv->dsm.start + fbc->compressed_fb.start);
  520. I915_WRITE(FBC_LL_BASE,
  521. dev_priv->dsm.start + compressed_llb->start);
  522. }
  523. DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
  524. fbc->compressed_fb.size, fbc->threshold);
  525. return 0;
  526. err_fb:
  527. kfree(compressed_llb);
  528. i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
  529. err_llb:
  530. if (drm_mm_initialized(&dev_priv->mm.stolen))
  531. 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);
  532. return -ENOSPC;
  533. }
  534. static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
  535. {
  536. struct intel_fbc *fbc = &dev_priv->fbc;
  537. if (drm_mm_node_allocated(&fbc->compressed_fb))
  538. i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
  539. if (fbc->compressed_llb) {
  540. i915_gem_stolen_remove_node(dev_priv, fbc->compressed_llb);
  541. kfree(fbc->compressed_llb);
  542. }
  543. }
  544. void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
  545. {
  546. struct intel_fbc *fbc = &dev_priv->fbc;
  547. if (!fbc_supported(dev_priv))
  548. return;
  549. mutex_lock(&fbc->lock);
  550. __intel_fbc_cleanup_cfb(dev_priv);
  551. mutex_unlock(&fbc->lock);
  552. }
  553. static bool stride_is_valid(struct drm_i915_private *dev_priv,
  554. unsigned int stride)
  555. {
  556. /* This should have been caught earlier. */
  557. if (WARN_ON_ONCE((stride & (64 - 1)) != 0))
  558. return false;
  559. /* Below are the additional FBC restrictions. */
  560. if (stride < 512)
  561. return false;
  562. if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
  563. return stride == 4096 || stride == 8192;
  564. if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
  565. return false;
  566. if (stride > 16384)
  567. return false;
  568. return true;
  569. }
  570. static bool pixel_format_is_valid(struct drm_i915_private *dev_priv,
  571. uint32_t pixel_format)
  572. {
  573. switch (pixel_format) {
  574. case DRM_FORMAT_XRGB8888:
  575. case DRM_FORMAT_XBGR8888:
  576. return true;
  577. case DRM_FORMAT_XRGB1555:
  578. case DRM_FORMAT_RGB565:
  579. /* 16bpp not supported on gen2 */
  580. if (IS_GEN2(dev_priv))
  581. return false;
  582. /* WaFbcOnly1to1Ratio:ctg */
  583. if (IS_G4X(dev_priv))
  584. return false;
  585. return true;
  586. default:
  587. return false;
  588. }
  589. }
  590. /*
  591. * For some reason, the hardware tracking starts looking at whatever we
  592. * programmed as the display plane base address register. It does not look at
  593. * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
  594. * variables instead of just looking at the pipe/plane size.
  595. */
  596. static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
  597. {
  598. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  599. struct intel_fbc *fbc = &dev_priv->fbc;
  600. unsigned int effective_w, effective_h, max_w, max_h;
  601. if (INTEL_GEN(dev_priv) >= 8 || IS_HASWELL(dev_priv)) {
  602. max_w = 4096;
  603. max_h = 4096;
  604. } else if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
  605. max_w = 4096;
  606. max_h = 2048;
  607. } else {
  608. max_w = 2048;
  609. max_h = 1536;
  610. }
  611. intel_fbc_get_plane_source_size(&fbc->state_cache, &effective_w,
  612. &effective_h);
  613. effective_w += fbc->state_cache.plane.adjusted_x;
  614. effective_h += fbc->state_cache.plane.adjusted_y;
  615. return effective_w <= max_w && effective_h <= max_h;
  616. }
  617. static void intel_fbc_update_state_cache(struct intel_crtc *crtc,
  618. struct intel_crtc_state *crtc_state,
  619. struct intel_plane_state *plane_state)
  620. {
  621. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  622. struct intel_fbc *fbc = &dev_priv->fbc;
  623. struct intel_fbc_state_cache *cache = &fbc->state_cache;
  624. struct drm_framebuffer *fb = plane_state->base.fb;
  625. cache->vma = NULL;
  626. cache->flags = 0;
  627. cache->crtc.mode_flags = crtc_state->base.adjusted_mode.flags;
  628. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  629. cache->crtc.hsw_bdw_pixel_rate = crtc_state->pixel_rate;
  630. cache->plane.rotation = plane_state->base.rotation;
  631. /*
  632. * Src coordinates are already rotated by 270 degrees for
  633. * the 90/270 degree plane rotation cases (to match the
  634. * GTT mapping), hence no need to account for rotation here.
  635. */
  636. cache->plane.src_w = drm_rect_width(&plane_state->base.src) >> 16;
  637. cache->plane.src_h = drm_rect_height(&plane_state->base.src) >> 16;
  638. cache->plane.visible = plane_state->base.visible;
  639. cache->plane.adjusted_x = plane_state->main.x;
  640. cache->plane.adjusted_y = plane_state->main.y;
  641. cache->plane.y = plane_state->base.src.y1 >> 16;
  642. if (!cache->plane.visible)
  643. return;
  644. cache->fb.format = fb->format;
  645. cache->fb.stride = fb->pitches[0];
  646. cache->vma = plane_state->vma;
  647. cache->flags = plane_state->flags;
  648. if (WARN_ON(cache->flags & PLANE_HAS_FENCE && !cache->vma->fence))
  649. cache->flags &= ~PLANE_HAS_FENCE;
  650. }
  651. static bool intel_fbc_can_activate(struct intel_crtc *crtc)
  652. {
  653. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  654. struct intel_fbc *fbc = &dev_priv->fbc;
  655. struct intel_fbc_state_cache *cache = &fbc->state_cache;
  656. /* We don't need to use a state cache here since this information is
  657. * global for all CRTC.
  658. */
  659. if (fbc->underrun_detected) {
  660. fbc->no_fbc_reason = "underrun detected";
  661. return false;
  662. }
  663. if (!cache->vma) {
  664. fbc->no_fbc_reason = "primary plane not visible";
  665. return false;
  666. }
  667. if (cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) {
  668. fbc->no_fbc_reason = "incompatible mode";
  669. return false;
  670. }
  671. if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
  672. fbc->no_fbc_reason = "mode too large for compression";
  673. return false;
  674. }
  675. /* The use of a CPU fence is mandatory in order to detect writes
  676. * by the CPU to the scanout and trigger updates to the FBC.
  677. *
  678. * Note that is possible for a tiled surface to be unmappable (and
  679. * so have no fence associated with it) due to aperture constaints
  680. * at the time of pinning.
  681. *
  682. * FIXME with 90/270 degree rotation we should use the fence on
  683. * the normal GTT view (the rotated view doesn't even have a
  684. * fence). Would need changes to the FBC fence Y offset as well.
  685. * For now this will effecively disable FBC with 90/270 degree
  686. * rotation.
  687. */
  688. if (!(cache->flags & PLANE_HAS_FENCE)) {
  689. fbc->no_fbc_reason = "framebuffer not tiled or fenced";
  690. return false;
  691. }
  692. if (INTEL_GEN(dev_priv) <= 4 && !IS_G4X(dev_priv) &&
  693. cache->plane.rotation != DRM_MODE_ROTATE_0) {
  694. fbc->no_fbc_reason = "rotation unsupported";
  695. return false;
  696. }
  697. if (!stride_is_valid(dev_priv, cache->fb.stride)) {
  698. fbc->no_fbc_reason = "framebuffer stride not supported";
  699. return false;
  700. }
  701. if (!pixel_format_is_valid(dev_priv, cache->fb.format->format)) {
  702. fbc->no_fbc_reason = "pixel format is invalid";
  703. return false;
  704. }
  705. /* WaFbcExceedCdClockThreshold:hsw,bdw */
  706. if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
  707. cache->crtc.hsw_bdw_pixel_rate >= dev_priv->cdclk.hw.cdclk * 95 / 100) {
  708. fbc->no_fbc_reason = "pixel rate is too big";
  709. return false;
  710. }
  711. /* It is possible for the required CFB size change without a
  712. * crtc->disable + crtc->enable since it is possible to change the
  713. * stride without triggering a full modeset. Since we try to
  714. * over-allocate the CFB, there's a chance we may keep FBC enabled even
  715. * if this happens, but if we exceed the current CFB size we'll have to
  716. * disable FBC. Notice that it would be possible to disable FBC, wait
  717. * for a frame, free the stolen node, then try to reenable FBC in case
  718. * we didn't get any invalidate/deactivate calls, but this would require
  719. * a lot of tracking just for a specific case. If we conclude it's an
  720. * important case, we can implement it later. */
  721. if (intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache) >
  722. fbc->compressed_fb.size * fbc->threshold) {
  723. fbc->no_fbc_reason = "CFB requirements changed";
  724. return false;
  725. }
  726. /*
  727. * Work around a problem on GEN9+ HW, where enabling FBC on a plane
  728. * having a Y offset that isn't divisible by 4 causes FIFO underrun
  729. * and screen flicker.
  730. */
  731. if (IS_GEN(dev_priv, 9, 10) &&
  732. (fbc->state_cache.plane.adjusted_y & 3)) {
  733. fbc->no_fbc_reason = "plane Y offset is misaligned";
  734. return false;
  735. }
  736. return true;
  737. }
  738. static bool intel_fbc_can_enable(struct drm_i915_private *dev_priv)
  739. {
  740. struct intel_fbc *fbc = &dev_priv->fbc;
  741. if (intel_vgpu_active(dev_priv)) {
  742. fbc->no_fbc_reason = "VGPU is active";
  743. return false;
  744. }
  745. if (!i915_modparams.enable_fbc) {
  746. fbc->no_fbc_reason = "disabled per module param or by default";
  747. return false;
  748. }
  749. if (fbc->underrun_detected) {
  750. fbc->no_fbc_reason = "underrun detected";
  751. return false;
  752. }
  753. return true;
  754. }
  755. static void intel_fbc_get_reg_params(struct intel_crtc *crtc,
  756. struct intel_fbc_reg_params *params)
  757. {
  758. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  759. struct intel_fbc *fbc = &dev_priv->fbc;
  760. struct intel_fbc_state_cache *cache = &fbc->state_cache;
  761. /* Since all our fields are integer types, use memset here so the
  762. * comparison function can rely on memcmp because the padding will be
  763. * zero. */
  764. memset(params, 0, sizeof(*params));
  765. params->vma = cache->vma;
  766. params->flags = cache->flags;
  767. params->crtc.pipe = crtc->pipe;
  768. params->crtc.i9xx_plane = to_intel_plane(crtc->base.primary)->i9xx_plane;
  769. params->crtc.fence_y_offset = get_crtc_fence_y_offset(fbc);
  770. params->fb.format = cache->fb.format;
  771. params->fb.stride = cache->fb.stride;
  772. params->cfb_size = intel_fbc_calculate_cfb_size(dev_priv, cache);
  773. if (IS_GEN9(dev_priv) && !IS_GEMINILAKE(dev_priv))
  774. params->gen9_wa_cfb_stride = DIV_ROUND_UP(cache->plane.src_w,
  775. 32 * fbc->threshold) * 8;
  776. }
  777. static bool intel_fbc_reg_params_equal(struct intel_fbc_reg_params *params1,
  778. struct intel_fbc_reg_params *params2)
  779. {
  780. /* We can use this since intel_fbc_get_reg_params() does a memset. */
  781. return memcmp(params1, params2, sizeof(*params1)) == 0;
  782. }
  783. void intel_fbc_pre_update(struct intel_crtc *crtc,
  784. struct intel_crtc_state *crtc_state,
  785. struct intel_plane_state *plane_state)
  786. {
  787. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  788. struct intel_fbc *fbc = &dev_priv->fbc;
  789. const char *reason = "update pending";
  790. if (!fbc_supported(dev_priv))
  791. return;
  792. mutex_lock(&fbc->lock);
  793. if (!multiple_pipes_ok(crtc, plane_state)) {
  794. reason = "more than one pipe active";
  795. goto deactivate;
  796. }
  797. if (!fbc->enabled || fbc->crtc != crtc)
  798. goto unlock;
  799. intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
  800. deactivate:
  801. intel_fbc_deactivate(dev_priv, reason);
  802. unlock:
  803. mutex_unlock(&fbc->lock);
  804. }
  805. /**
  806. * __intel_fbc_disable - disable FBC
  807. * @dev_priv: i915 device instance
  808. *
  809. * This is the low level function that actually disables FBC. Callers should
  810. * grab the FBC lock.
  811. */
  812. static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
  813. {
  814. struct intel_fbc *fbc = &dev_priv->fbc;
  815. struct intel_crtc *crtc = fbc->crtc;
  816. WARN_ON(!mutex_is_locked(&fbc->lock));
  817. WARN_ON(!fbc->enabled);
  818. WARN_ON(fbc->active);
  819. DRM_DEBUG_KMS("Disabling FBC on pipe %c\n", pipe_name(crtc->pipe));
  820. __intel_fbc_cleanup_cfb(dev_priv);
  821. fbc->enabled = false;
  822. fbc->crtc = NULL;
  823. }
  824. static void __intel_fbc_post_update(struct intel_crtc *crtc)
  825. {
  826. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  827. struct intel_fbc *fbc = &dev_priv->fbc;
  828. struct intel_fbc_reg_params old_params;
  829. WARN_ON(!mutex_is_locked(&fbc->lock));
  830. if (!fbc->enabled || fbc->crtc != crtc)
  831. return;
  832. if (!i915_modparams.enable_fbc) {
  833. intel_fbc_deactivate(dev_priv, "disabled at runtime per module param");
  834. __intel_fbc_disable(dev_priv);
  835. return;
  836. }
  837. if (!intel_fbc_can_activate(crtc)) {
  838. WARN_ON(fbc->active);
  839. return;
  840. }
  841. old_params = fbc->params;
  842. intel_fbc_get_reg_params(crtc, &fbc->params);
  843. /* If the scanout has not changed, don't modify the FBC settings.
  844. * Note that we make the fundamental assumption that the fb->obj
  845. * cannot be unpinned (and have its GTT offset and fence revoked)
  846. * without first being decoupled from the scanout and FBC disabled.
  847. */
  848. if (fbc->active &&
  849. intel_fbc_reg_params_equal(&old_params, &fbc->params))
  850. return;
  851. intel_fbc_deactivate(dev_priv, "FBC enabled (active or scheduled)");
  852. intel_fbc_schedule_activation(crtc);
  853. }
  854. void intel_fbc_post_update(struct intel_crtc *crtc)
  855. {
  856. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  857. struct intel_fbc *fbc = &dev_priv->fbc;
  858. if (!fbc_supported(dev_priv))
  859. return;
  860. mutex_lock(&fbc->lock);
  861. __intel_fbc_post_update(crtc);
  862. mutex_unlock(&fbc->lock);
  863. }
  864. static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
  865. {
  866. if (fbc->enabled)
  867. return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit;
  868. else
  869. return fbc->possible_framebuffer_bits;
  870. }
  871. void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
  872. unsigned int frontbuffer_bits,
  873. enum fb_op_origin origin)
  874. {
  875. struct intel_fbc *fbc = &dev_priv->fbc;
  876. if (!fbc_supported(dev_priv))
  877. return;
  878. if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
  879. return;
  880. mutex_lock(&fbc->lock);
  881. fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits;
  882. if (fbc->enabled && fbc->busy_bits)
  883. intel_fbc_deactivate(dev_priv, "frontbuffer write");
  884. mutex_unlock(&fbc->lock);
  885. }
  886. void intel_fbc_flush(struct drm_i915_private *dev_priv,
  887. unsigned int frontbuffer_bits, enum fb_op_origin origin)
  888. {
  889. struct intel_fbc *fbc = &dev_priv->fbc;
  890. if (!fbc_supported(dev_priv))
  891. return;
  892. mutex_lock(&fbc->lock);
  893. fbc->busy_bits &= ~frontbuffer_bits;
  894. if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
  895. goto out;
  896. if (!fbc->busy_bits && fbc->enabled &&
  897. (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) {
  898. if (fbc->active)
  899. intel_fbc_recompress(dev_priv);
  900. else
  901. __intel_fbc_post_update(fbc->crtc);
  902. }
  903. out:
  904. mutex_unlock(&fbc->lock);
  905. }
  906. /**
  907. * intel_fbc_choose_crtc - select a CRTC to enable FBC on
  908. * @dev_priv: i915 device instance
  909. * @state: the atomic state structure
  910. *
  911. * This function looks at the proposed state for CRTCs and planes, then chooses
  912. * which pipe is going to have FBC by setting intel_crtc_state->enable_fbc to
  913. * true.
  914. *
  915. * Later, intel_fbc_enable is going to look for state->enable_fbc and then maybe
  916. * enable FBC for the chosen CRTC. If it does, it will set dev_priv->fbc.crtc.
  917. */
  918. void intel_fbc_choose_crtc(struct drm_i915_private *dev_priv,
  919. struct intel_atomic_state *state)
  920. {
  921. struct intel_fbc *fbc = &dev_priv->fbc;
  922. struct intel_plane *plane;
  923. struct intel_plane_state *plane_state;
  924. bool crtc_chosen = false;
  925. int i;
  926. mutex_lock(&fbc->lock);
  927. /* Does this atomic commit involve the CRTC currently tied to FBC? */
  928. if (fbc->crtc &&
  929. !intel_atomic_get_new_crtc_state(state, fbc->crtc))
  930. goto out;
  931. if (!intel_fbc_can_enable(dev_priv))
  932. goto out;
  933. /* Simply choose the first CRTC that is compatible and has a visible
  934. * plane. We could go for fancier schemes such as checking the plane
  935. * size, but this would just affect the few platforms that don't tie FBC
  936. * to pipe or plane A. */
  937. for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
  938. struct intel_crtc_state *crtc_state;
  939. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  940. if (!plane->has_fbc)
  941. continue;
  942. if (!plane_state->base.visible)
  943. continue;
  944. crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
  945. crtc_state->enable_fbc = true;
  946. crtc_chosen = true;
  947. break;
  948. }
  949. if (!crtc_chosen)
  950. fbc->no_fbc_reason = "no suitable CRTC for FBC";
  951. out:
  952. mutex_unlock(&fbc->lock);
  953. }
  954. /**
  955. * intel_fbc_enable: tries to enable FBC on the CRTC
  956. * @crtc: the CRTC
  957. * @crtc_state: corresponding &drm_crtc_state for @crtc
  958. * @plane_state: corresponding &drm_plane_state for the primary plane of @crtc
  959. *
  960. * This function checks if the given CRTC was chosen for FBC, then enables it if
  961. * possible. Notice that it doesn't activate FBC. It is valid to call
  962. * intel_fbc_enable multiple times for the same pipe without an
  963. * intel_fbc_disable in the middle, as long as it is deactivated.
  964. */
  965. void intel_fbc_enable(struct intel_crtc *crtc,
  966. struct intel_crtc_state *crtc_state,
  967. struct intel_plane_state *plane_state)
  968. {
  969. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  970. struct intel_fbc *fbc = &dev_priv->fbc;
  971. if (!fbc_supported(dev_priv))
  972. return;
  973. mutex_lock(&fbc->lock);
  974. if (fbc->enabled) {
  975. WARN_ON(fbc->crtc == NULL);
  976. if (fbc->crtc == crtc) {
  977. WARN_ON(!crtc_state->enable_fbc);
  978. WARN_ON(fbc->active);
  979. }
  980. goto out;
  981. }
  982. if (!crtc_state->enable_fbc)
  983. goto out;
  984. WARN_ON(fbc->active);
  985. WARN_ON(fbc->crtc != NULL);
  986. intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
  987. if (intel_fbc_alloc_cfb(crtc)) {
  988. fbc->no_fbc_reason = "not enough stolen memory";
  989. goto out;
  990. }
  991. DRM_DEBUG_KMS("Enabling FBC on pipe %c\n", pipe_name(crtc->pipe));
  992. fbc->no_fbc_reason = "FBC enabled but not active yet\n";
  993. fbc->enabled = true;
  994. fbc->crtc = crtc;
  995. out:
  996. mutex_unlock(&fbc->lock);
  997. }
  998. /**
  999. * intel_fbc_disable - disable FBC if it's associated with crtc
  1000. * @crtc: the CRTC
  1001. *
  1002. * This function disables FBC if it's associated with the provided CRTC.
  1003. */
  1004. void intel_fbc_disable(struct intel_crtc *crtc)
  1005. {
  1006. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1007. struct intel_fbc *fbc = &dev_priv->fbc;
  1008. if (!fbc_supported(dev_priv))
  1009. return;
  1010. WARN_ON(crtc->active);
  1011. mutex_lock(&fbc->lock);
  1012. if (fbc->crtc == crtc)
  1013. __intel_fbc_disable(dev_priv);
  1014. mutex_unlock(&fbc->lock);
  1015. cancel_work_sync(&fbc->work.work);
  1016. }
  1017. /**
  1018. * intel_fbc_global_disable - globally disable FBC
  1019. * @dev_priv: i915 device instance
  1020. *
  1021. * This function disables FBC regardless of which CRTC is associated with it.
  1022. */
  1023. void intel_fbc_global_disable(struct drm_i915_private *dev_priv)
  1024. {
  1025. struct intel_fbc *fbc = &dev_priv->fbc;
  1026. if (!fbc_supported(dev_priv))
  1027. return;
  1028. mutex_lock(&fbc->lock);
  1029. if (fbc->enabled) {
  1030. WARN_ON(fbc->crtc->active);
  1031. __intel_fbc_disable(dev_priv);
  1032. }
  1033. mutex_unlock(&fbc->lock);
  1034. cancel_work_sync(&fbc->work.work);
  1035. }
  1036. static void intel_fbc_underrun_work_fn(struct work_struct *work)
  1037. {
  1038. struct drm_i915_private *dev_priv =
  1039. container_of(work, struct drm_i915_private, fbc.underrun_work);
  1040. struct intel_fbc *fbc = &dev_priv->fbc;
  1041. mutex_lock(&fbc->lock);
  1042. /* Maybe we were scheduled twice. */
  1043. if (fbc->underrun_detected || !fbc->enabled)
  1044. goto out;
  1045. DRM_DEBUG_KMS("Disabling FBC due to FIFO underrun.\n");
  1046. fbc->underrun_detected = true;
  1047. intel_fbc_deactivate(dev_priv, "FIFO underrun");
  1048. out:
  1049. mutex_unlock(&fbc->lock);
  1050. }
  1051. /*
  1052. * intel_fbc_reset_underrun - reset FBC fifo underrun status.
  1053. * @dev_priv: i915 device instance
  1054. *
  1055. * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we
  1056. * want to re-enable FBC after an underrun to increase test coverage.
  1057. */
  1058. int intel_fbc_reset_underrun(struct drm_i915_private *dev_priv)
  1059. {
  1060. int ret;
  1061. cancel_work_sync(&dev_priv->fbc.underrun_work);
  1062. ret = mutex_lock_interruptible(&dev_priv->fbc.lock);
  1063. if (ret)
  1064. return ret;
  1065. if (dev_priv->fbc.underrun_detected) {
  1066. DRM_DEBUG_KMS("Re-allowing FBC after fifo underrun\n");
  1067. dev_priv->fbc.no_fbc_reason = "FIFO underrun cleared";
  1068. }
  1069. dev_priv->fbc.underrun_detected = false;
  1070. mutex_unlock(&dev_priv->fbc.lock);
  1071. return 0;
  1072. }
  1073. /**
  1074. * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
  1075. * @dev_priv: i915 device instance
  1076. *
  1077. * Without FBC, most underruns are harmless and don't really cause too many
  1078. * problems, except for an annoying message on dmesg. With FBC, underruns can
  1079. * become black screens or even worse, especially when paired with bad
  1080. * watermarks. So in order for us to be on the safe side, completely disable FBC
  1081. * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
  1082. * already suggests that watermarks may be bad, so try to be as safe as
  1083. * possible.
  1084. *
  1085. * This function is called from the IRQ handler.
  1086. */
  1087. void intel_fbc_handle_fifo_underrun_irq(struct drm_i915_private *dev_priv)
  1088. {
  1089. struct intel_fbc *fbc = &dev_priv->fbc;
  1090. if (!fbc_supported(dev_priv))
  1091. return;
  1092. /* There's no guarantee that underrun_detected won't be set to true
  1093. * right after this check and before the work is scheduled, but that's
  1094. * not a problem since we'll check it again under the work function
  1095. * while FBC is locked. This check here is just to prevent us from
  1096. * unnecessarily scheduling the work, and it relies on the fact that we
  1097. * never switch underrun_detect back to false after it's true. */
  1098. if (READ_ONCE(fbc->underrun_detected))
  1099. return;
  1100. schedule_work(&fbc->underrun_work);
  1101. }
  1102. /**
  1103. * intel_fbc_init_pipe_state - initialize FBC's CRTC visibility tracking
  1104. * @dev_priv: i915 device instance
  1105. *
  1106. * The FBC code needs to track CRTC visibility since the older platforms can't
  1107. * have FBC enabled while multiple pipes are used. This function does the
  1108. * initial setup at driver load to make sure FBC is matching the real hardware.
  1109. */
  1110. void intel_fbc_init_pipe_state(struct drm_i915_private *dev_priv)
  1111. {
  1112. struct intel_crtc *crtc;
  1113. /* Don't even bother tracking anything if we don't need. */
  1114. if (!no_fbc_on_multiple_pipes(dev_priv))
  1115. return;
  1116. for_each_intel_crtc(&dev_priv->drm, crtc)
  1117. if (intel_crtc_active(crtc) &&
  1118. crtc->base.primary->state->visible)
  1119. dev_priv->fbc.visible_pipes_mask |= (1 << crtc->pipe);
  1120. }
  1121. /*
  1122. * The DDX driver changes its behavior depending on the value it reads from
  1123. * i915.enable_fbc, so sanitize it by translating the default value into either
  1124. * 0 or 1 in order to allow it to know what's going on.
  1125. *
  1126. * Notice that this is done at driver initialization and we still allow user
  1127. * space to change the value during runtime without sanitizing it again. IGT
  1128. * relies on being able to change i915.enable_fbc at runtime.
  1129. */
  1130. static int intel_sanitize_fbc_option(struct drm_i915_private *dev_priv)
  1131. {
  1132. if (i915_modparams.enable_fbc >= 0)
  1133. return !!i915_modparams.enable_fbc;
  1134. if (!HAS_FBC(dev_priv))
  1135. return 0;
  1136. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9)
  1137. return 1;
  1138. return 0;
  1139. }
  1140. static bool need_fbc_vtd_wa(struct drm_i915_private *dev_priv)
  1141. {
  1142. /* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
  1143. if (intel_vtd_active() &&
  1144. (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))) {
  1145. DRM_INFO("Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
  1146. return true;
  1147. }
  1148. return false;
  1149. }
  1150. /**
  1151. * intel_fbc_init - Initialize FBC
  1152. * @dev_priv: the i915 device
  1153. *
  1154. * This function might be called during PM init process.
  1155. */
  1156. void intel_fbc_init(struct drm_i915_private *dev_priv)
  1157. {
  1158. struct intel_fbc *fbc = &dev_priv->fbc;
  1159. INIT_WORK(&fbc->work.work, intel_fbc_work_fn);
  1160. INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
  1161. mutex_init(&fbc->lock);
  1162. fbc->enabled = false;
  1163. fbc->active = false;
  1164. fbc->work.scheduled = false;
  1165. if (need_fbc_vtd_wa(dev_priv))
  1166. mkwrite_device_info(dev_priv)->has_fbc = false;
  1167. i915_modparams.enable_fbc = intel_sanitize_fbc_option(dev_priv);
  1168. DRM_DEBUG_KMS("Sanitized enable_fbc value: %d\n",
  1169. i915_modparams.enable_fbc);
  1170. if (!HAS_FBC(dev_priv)) {
  1171. fbc->no_fbc_reason = "unsupported by this chipset";
  1172. return;
  1173. }
  1174. /* This value was pulled out of someone's hat */
  1175. if (INTEL_GEN(dev_priv) <= 4 && !IS_GM45(dev_priv))
  1176. I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
  1177. /* We still don't have any sort of hardware state readout for FBC, so
  1178. * deactivate it in case the BIOS activated it to make sure software
  1179. * matches the hardware state. */
  1180. if (intel_fbc_hw_is_active(dev_priv))
  1181. intel_fbc_hw_deactivate(dev_priv);
  1182. }