intel_fbc.c 39 KB

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