intel_fbc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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 dev_priv->fbc.enable_fbc != NULL;
  44. }
  45. /*
  46. * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the
  47. * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's
  48. * origin so the x and y offsets can actually fit the registers. As a
  49. * consequence, the fence doesn't really start exactly at the display plane
  50. * address we program because it starts at the real start of the buffer, so we
  51. * have to take this into consideration here.
  52. */
  53. static unsigned int get_crtc_fence_y_offset(struct intel_crtc *crtc)
  54. {
  55. return crtc->base.y - crtc->adjusted_y;
  56. }
  57. static void i8xx_fbc_disable(struct drm_i915_private *dev_priv)
  58. {
  59. u32 fbc_ctl;
  60. dev_priv->fbc.enabled = false;
  61. /* Disable compression */
  62. fbc_ctl = I915_READ(FBC_CONTROL);
  63. if ((fbc_ctl & FBC_CTL_EN) == 0)
  64. return;
  65. fbc_ctl &= ~FBC_CTL_EN;
  66. I915_WRITE(FBC_CONTROL, fbc_ctl);
  67. /* Wait for compressing bit to clear */
  68. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  69. DRM_DEBUG_KMS("FBC idle timed out\n");
  70. return;
  71. }
  72. DRM_DEBUG_KMS("disabled FBC\n");
  73. }
  74. static void i8xx_fbc_enable(struct intel_crtc *crtc)
  75. {
  76. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  77. struct drm_framebuffer *fb = crtc->base.primary->fb;
  78. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  79. int cfb_pitch;
  80. int i;
  81. u32 fbc_ctl;
  82. dev_priv->fbc.enabled = true;
  83. /* Note: fbc.threshold == 1 for i8xx */
  84. cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
  85. if (fb->pitches[0] < cfb_pitch)
  86. cfb_pitch = fb->pitches[0];
  87. /* FBC_CTL wants 32B or 64B units */
  88. if (IS_GEN2(dev_priv))
  89. cfb_pitch = (cfb_pitch / 32) - 1;
  90. else
  91. cfb_pitch = (cfb_pitch / 64) - 1;
  92. /* Clear old tags */
  93. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  94. I915_WRITE(FBC_TAG(i), 0);
  95. if (IS_GEN4(dev_priv)) {
  96. u32 fbc_ctl2;
  97. /* Set it up... */
  98. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  99. fbc_ctl2 |= FBC_CTL_PLANE(crtc->plane);
  100. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  101. I915_WRITE(FBC_FENCE_OFF, get_crtc_fence_y_offset(crtc));
  102. }
  103. /* enable it... */
  104. fbc_ctl = I915_READ(FBC_CONTROL);
  105. fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
  106. fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
  107. if (IS_I945GM(dev_priv))
  108. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  109. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  110. fbc_ctl |= obj->fence_reg;
  111. I915_WRITE(FBC_CONTROL, fbc_ctl);
  112. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
  113. cfb_pitch, crtc->base.y, plane_name(crtc->plane));
  114. }
  115. static bool i8xx_fbc_enabled(struct drm_i915_private *dev_priv)
  116. {
  117. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  118. }
  119. static void g4x_fbc_enable(struct intel_crtc *crtc)
  120. {
  121. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  122. struct drm_framebuffer *fb = crtc->base.primary->fb;
  123. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  124. u32 dpfc_ctl;
  125. dev_priv->fbc.enabled = true;
  126. dpfc_ctl = DPFC_CTL_PLANE(crtc->plane) | DPFC_SR_EN;
  127. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  128. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  129. else
  130. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  131. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  132. I915_WRITE(DPFC_FENCE_YOFF, get_crtc_fence_y_offset(crtc));
  133. /* enable it... */
  134. I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  135. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
  136. }
  137. static void g4x_fbc_disable(struct drm_i915_private *dev_priv)
  138. {
  139. u32 dpfc_ctl;
  140. dev_priv->fbc.enabled = false;
  141. /* Disable compression */
  142. dpfc_ctl = I915_READ(DPFC_CONTROL);
  143. if (dpfc_ctl & DPFC_CTL_EN) {
  144. dpfc_ctl &= ~DPFC_CTL_EN;
  145. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  146. DRM_DEBUG_KMS("disabled FBC\n");
  147. }
  148. }
  149. static bool g4x_fbc_enabled(struct drm_i915_private *dev_priv)
  150. {
  151. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  152. }
  153. static void intel_fbc_nuke(struct drm_i915_private *dev_priv)
  154. {
  155. I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
  156. POSTING_READ(MSG_FBC_REND_STATE);
  157. }
  158. static void ilk_fbc_enable(struct intel_crtc *crtc)
  159. {
  160. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  161. struct drm_framebuffer *fb = crtc->base.primary->fb;
  162. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  163. u32 dpfc_ctl;
  164. int threshold = dev_priv->fbc.threshold;
  165. unsigned int y_offset;
  166. dev_priv->fbc.enabled = true;
  167. dpfc_ctl = DPFC_CTL_PLANE(crtc->plane);
  168. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  169. threshold++;
  170. switch (threshold) {
  171. case 4:
  172. case 3:
  173. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  174. break;
  175. case 2:
  176. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  177. break;
  178. case 1:
  179. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  180. break;
  181. }
  182. dpfc_ctl |= DPFC_CTL_FENCE_EN;
  183. if (IS_GEN5(dev_priv))
  184. dpfc_ctl |= obj->fence_reg;
  185. y_offset = get_crtc_fence_y_offset(crtc);
  186. I915_WRITE(ILK_DPFC_FENCE_YOFF, y_offset);
  187. I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
  188. /* enable it... */
  189. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  190. if (IS_GEN6(dev_priv)) {
  191. I915_WRITE(SNB_DPFC_CTL_SA,
  192. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  193. I915_WRITE(DPFC_CPU_FENCE_OFFSET, y_offset);
  194. }
  195. intel_fbc_nuke(dev_priv);
  196. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
  197. }
  198. static void ilk_fbc_disable(struct drm_i915_private *dev_priv)
  199. {
  200. u32 dpfc_ctl;
  201. dev_priv->fbc.enabled = false;
  202. /* Disable compression */
  203. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  204. if (dpfc_ctl & DPFC_CTL_EN) {
  205. dpfc_ctl &= ~DPFC_CTL_EN;
  206. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  207. DRM_DEBUG_KMS("disabled FBC\n");
  208. }
  209. }
  210. static bool ilk_fbc_enabled(struct drm_i915_private *dev_priv)
  211. {
  212. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  213. }
  214. static void gen7_fbc_enable(struct intel_crtc *crtc)
  215. {
  216. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  217. struct drm_framebuffer *fb = crtc->base.primary->fb;
  218. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  219. u32 dpfc_ctl;
  220. int threshold = dev_priv->fbc.threshold;
  221. dev_priv->fbc.enabled = true;
  222. dpfc_ctl = 0;
  223. if (IS_IVYBRIDGE(dev_priv))
  224. dpfc_ctl |= IVB_DPFC_CTL_PLANE(crtc->plane);
  225. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  226. threshold++;
  227. switch (threshold) {
  228. case 4:
  229. case 3:
  230. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  231. break;
  232. case 2:
  233. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  234. break;
  235. case 1:
  236. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  237. break;
  238. }
  239. dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
  240. if (dev_priv->fbc.false_color)
  241. dpfc_ctl |= FBC_CTL_FALSE_COLOR;
  242. if (IS_IVYBRIDGE(dev_priv)) {
  243. /* WaFbcAsynchFlipDisableFbcQueue:ivb */
  244. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  245. I915_READ(ILK_DISPLAY_CHICKEN1) |
  246. ILK_FBCQ_DIS);
  247. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  248. /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
  249. I915_WRITE(CHICKEN_PIPESL_1(crtc->pipe),
  250. I915_READ(CHICKEN_PIPESL_1(crtc->pipe)) |
  251. HSW_FBCQ_DIS);
  252. }
  253. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  254. I915_WRITE(SNB_DPFC_CTL_SA,
  255. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  256. I915_WRITE(DPFC_CPU_FENCE_OFFSET, get_crtc_fence_y_offset(crtc));
  257. intel_fbc_nuke(dev_priv);
  258. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
  259. }
  260. /**
  261. * intel_fbc_enabled - Is FBC enabled?
  262. * @dev_priv: i915 device instance
  263. *
  264. * This function is used to verify the current state of FBC.
  265. * FIXME: This should be tracked in the plane config eventually
  266. * instead of queried at runtime for most callers.
  267. */
  268. bool intel_fbc_enabled(struct drm_i915_private *dev_priv)
  269. {
  270. return dev_priv->fbc.enabled;
  271. }
  272. static void intel_fbc_enable(struct intel_crtc *crtc,
  273. const struct drm_framebuffer *fb)
  274. {
  275. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  276. dev_priv->fbc.enable_fbc(crtc);
  277. dev_priv->fbc.crtc = crtc;
  278. dev_priv->fbc.fb_id = fb->base.id;
  279. dev_priv->fbc.y = crtc->base.y;
  280. }
  281. static void intel_fbc_work_fn(struct work_struct *__work)
  282. {
  283. struct intel_fbc_work *work =
  284. container_of(to_delayed_work(__work),
  285. struct intel_fbc_work, work);
  286. struct drm_i915_private *dev_priv = work->crtc->base.dev->dev_private;
  287. struct drm_framebuffer *crtc_fb = work->crtc->base.primary->fb;
  288. mutex_lock(&dev_priv->fbc.lock);
  289. if (work == dev_priv->fbc.fbc_work) {
  290. /* Double check that we haven't switched fb without cancelling
  291. * the prior work.
  292. */
  293. if (crtc_fb == work->fb)
  294. intel_fbc_enable(work->crtc, work->fb);
  295. dev_priv->fbc.fbc_work = NULL;
  296. }
  297. mutex_unlock(&dev_priv->fbc.lock);
  298. kfree(work);
  299. }
  300. static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
  301. {
  302. WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
  303. if (dev_priv->fbc.fbc_work == NULL)
  304. return;
  305. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  306. /* Synchronisation is provided by struct_mutex and checking of
  307. * dev_priv->fbc.fbc_work, so we can perform the cancellation
  308. * entirely asynchronously.
  309. */
  310. if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
  311. /* tasklet was killed before being run, clean up */
  312. kfree(dev_priv->fbc.fbc_work);
  313. /* Mark the work as no longer wanted so that if it does
  314. * wake-up (because the work was already running and waiting
  315. * for our mutex), it will discover that is no longer
  316. * necessary to run.
  317. */
  318. dev_priv->fbc.fbc_work = NULL;
  319. }
  320. static void intel_fbc_schedule_enable(struct intel_crtc *crtc)
  321. {
  322. struct intel_fbc_work *work;
  323. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  324. WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
  325. intel_fbc_cancel_work(dev_priv);
  326. work = kzalloc(sizeof(*work), GFP_KERNEL);
  327. if (work == NULL) {
  328. DRM_ERROR("Failed to allocate FBC work structure\n");
  329. intel_fbc_enable(crtc, crtc->base.primary->fb);
  330. return;
  331. }
  332. work->crtc = crtc;
  333. work->fb = crtc->base.primary->fb;
  334. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  335. dev_priv->fbc.fbc_work = work;
  336. /* Delay the actual enabling to let pageflipping cease and the
  337. * display to settle before starting the compression. Note that
  338. * this delay also serves a second purpose: it allows for a
  339. * vblank to pass after disabling the FBC before we attempt
  340. * to modify the control registers.
  341. *
  342. * A more complicated solution would involve tracking vblanks
  343. * following the termination of the page-flipping sequence
  344. * and indeed performing the enable as a co-routine and not
  345. * waiting synchronously upon the vblank.
  346. *
  347. * WaFbcWaitForVBlankBeforeEnable:ilk,snb
  348. */
  349. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  350. }
  351. static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
  352. {
  353. WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
  354. intel_fbc_cancel_work(dev_priv);
  355. dev_priv->fbc.disable_fbc(dev_priv);
  356. dev_priv->fbc.crtc = NULL;
  357. }
  358. /**
  359. * intel_fbc_disable - disable FBC
  360. * @dev_priv: i915 device instance
  361. *
  362. * This function disables FBC.
  363. */
  364. void intel_fbc_disable(struct drm_i915_private *dev_priv)
  365. {
  366. if (!fbc_supported(dev_priv))
  367. return;
  368. mutex_lock(&dev_priv->fbc.lock);
  369. __intel_fbc_disable(dev_priv);
  370. mutex_unlock(&dev_priv->fbc.lock);
  371. }
  372. /*
  373. * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
  374. * @crtc: the CRTC
  375. *
  376. * This function disables FBC if it's associated with the provided CRTC.
  377. */
  378. void intel_fbc_disable_crtc(struct intel_crtc *crtc)
  379. {
  380. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  381. if (!fbc_supported(dev_priv))
  382. return;
  383. mutex_lock(&dev_priv->fbc.lock);
  384. if (dev_priv->fbc.crtc == crtc)
  385. __intel_fbc_disable(dev_priv);
  386. mutex_unlock(&dev_priv->fbc.lock);
  387. }
  388. const char *intel_no_fbc_reason_str(enum no_fbc_reason reason)
  389. {
  390. switch (reason) {
  391. case FBC_OK:
  392. return "FBC enabled but currently disabled in hardware";
  393. case FBC_UNSUPPORTED:
  394. return "unsupported by this chipset";
  395. case FBC_NO_OUTPUT:
  396. return "no output";
  397. case FBC_STOLEN_TOO_SMALL:
  398. return "not enough stolen memory";
  399. case FBC_UNSUPPORTED_MODE:
  400. return "mode incompatible with compression";
  401. case FBC_MODE_TOO_LARGE:
  402. return "mode too large for compression";
  403. case FBC_BAD_PLANE:
  404. return "FBC unsupported on plane";
  405. case FBC_NOT_TILED:
  406. return "framebuffer not tiled or fenced";
  407. case FBC_MULTIPLE_PIPES:
  408. return "more than one pipe active";
  409. case FBC_MODULE_PARAM:
  410. return "disabled per module param";
  411. case FBC_CHIP_DEFAULT:
  412. return "disabled per chip default";
  413. case FBC_ROTATION:
  414. return "rotation unsupported";
  415. case FBC_IN_DBG_MASTER:
  416. return "Kernel debugger is active";
  417. case FBC_BAD_STRIDE:
  418. return "framebuffer stride not supported";
  419. case FBC_PIXEL_RATE:
  420. return "pixel rate is too big";
  421. case FBC_PIXEL_FORMAT:
  422. return "pixel format is invalid";
  423. default:
  424. MISSING_CASE(reason);
  425. return "unknown reason";
  426. }
  427. }
  428. static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
  429. enum no_fbc_reason reason)
  430. {
  431. if (dev_priv->fbc.no_fbc_reason == reason)
  432. return;
  433. dev_priv->fbc.no_fbc_reason = reason;
  434. DRM_DEBUG_KMS("Disabling FBC: %s\n", intel_no_fbc_reason_str(reason));
  435. }
  436. static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
  437. {
  438. struct drm_crtc *crtc = NULL, *tmp_crtc;
  439. enum pipe pipe;
  440. bool pipe_a_only = false;
  441. if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
  442. pipe_a_only = true;
  443. for_each_pipe(dev_priv, pipe) {
  444. tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  445. if (intel_crtc_active(tmp_crtc) &&
  446. to_intel_plane_state(tmp_crtc->primary->state)->visible)
  447. crtc = tmp_crtc;
  448. if (pipe_a_only)
  449. break;
  450. }
  451. if (!crtc || crtc->primary->fb == NULL)
  452. return NULL;
  453. return crtc;
  454. }
  455. static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
  456. {
  457. enum pipe pipe;
  458. int n_pipes = 0;
  459. struct drm_crtc *crtc;
  460. if (INTEL_INFO(dev_priv)->gen > 4)
  461. return true;
  462. for_each_pipe(dev_priv, pipe) {
  463. crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  464. if (intel_crtc_active(crtc) &&
  465. to_intel_plane_state(crtc->primary->state)->visible)
  466. n_pipes++;
  467. }
  468. return (n_pipes < 2);
  469. }
  470. static int find_compression_threshold(struct drm_i915_private *dev_priv,
  471. struct drm_mm_node *node,
  472. int size,
  473. int fb_cpp)
  474. {
  475. int compression_threshold = 1;
  476. int ret;
  477. u64 end;
  478. /* The FBC hardware for BDW/SKL doesn't have access to the stolen
  479. * reserved range size, so it always assumes the maximum (8mb) is used.
  480. * If we enable FBC using a CFB on that memory range we'll get FIFO
  481. * underruns, even if that range is not reserved by the BIOS. */
  482. if (IS_BROADWELL(dev_priv) || IS_SKYLAKE(dev_priv))
  483. end = dev_priv->gtt.stolen_size - 8 * 1024 * 1024;
  484. else
  485. end = dev_priv->gtt.stolen_usable_size;
  486. /* HACK: This code depends on what we will do in *_enable_fbc. If that
  487. * code changes, this code needs to change as well.
  488. *
  489. * The enable_fbc code will attempt to use one of our 2 compression
  490. * thresholds, therefore, in that case, we only have 1 resort.
  491. */
  492. /* Try to over-allocate to reduce reallocations and fragmentation. */
  493. ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
  494. 4096, 0, end);
  495. if (ret == 0)
  496. return compression_threshold;
  497. again:
  498. /* HW's ability to limit the CFB is 1:4 */
  499. if (compression_threshold > 4 ||
  500. (fb_cpp == 2 && compression_threshold == 2))
  501. return 0;
  502. ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
  503. 4096, 0, end);
  504. if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
  505. return 0;
  506. } else if (ret) {
  507. compression_threshold <<= 1;
  508. goto again;
  509. } else {
  510. return compression_threshold;
  511. }
  512. }
  513. static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv, int size,
  514. int fb_cpp)
  515. {
  516. struct drm_mm_node *uninitialized_var(compressed_llb);
  517. int ret;
  518. ret = find_compression_threshold(dev_priv, &dev_priv->fbc.compressed_fb,
  519. size, fb_cpp);
  520. if (!ret)
  521. goto err_llb;
  522. else if (ret > 1) {
  523. 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");
  524. }
  525. dev_priv->fbc.threshold = ret;
  526. if (INTEL_INFO(dev_priv)->gen >= 5)
  527. I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
  528. else if (IS_GM45(dev_priv)) {
  529. I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
  530. } else {
  531. compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
  532. if (!compressed_llb)
  533. goto err_fb;
  534. ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
  535. 4096, 4096);
  536. if (ret)
  537. goto err_fb;
  538. dev_priv->fbc.compressed_llb = compressed_llb;
  539. I915_WRITE(FBC_CFB_BASE,
  540. dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
  541. I915_WRITE(FBC_LL_BASE,
  542. dev_priv->mm.stolen_base + compressed_llb->start);
  543. }
  544. dev_priv->fbc.uncompressed_size = size;
  545. DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
  546. dev_priv->fbc.compressed_fb.size,
  547. dev_priv->fbc.threshold);
  548. return 0;
  549. err_fb:
  550. kfree(compressed_llb);
  551. i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
  552. err_llb:
  553. 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);
  554. return -ENOSPC;
  555. }
  556. static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
  557. {
  558. if (dev_priv->fbc.uncompressed_size == 0)
  559. return;
  560. i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
  561. if (dev_priv->fbc.compressed_llb) {
  562. i915_gem_stolen_remove_node(dev_priv,
  563. dev_priv->fbc.compressed_llb);
  564. kfree(dev_priv->fbc.compressed_llb);
  565. }
  566. dev_priv->fbc.uncompressed_size = 0;
  567. }
  568. void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
  569. {
  570. if (!fbc_supported(dev_priv))
  571. return;
  572. mutex_lock(&dev_priv->fbc.lock);
  573. __intel_fbc_cleanup_cfb(dev_priv);
  574. mutex_unlock(&dev_priv->fbc.lock);
  575. }
  576. /*
  577. * For SKL+, the plane source size used by the hardware is based on the value we
  578. * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
  579. * we wrote to PIPESRC.
  580. */
  581. static void intel_fbc_get_plane_source_size(struct intel_crtc *crtc,
  582. int *width, int *height)
  583. {
  584. struct intel_plane_state *plane_state =
  585. to_intel_plane_state(crtc->base.primary->state);
  586. int w, h;
  587. if (intel_rotation_90_or_270(plane_state->base.rotation)) {
  588. w = drm_rect_height(&plane_state->src) >> 16;
  589. h = drm_rect_width(&plane_state->src) >> 16;
  590. } else {
  591. w = drm_rect_width(&plane_state->src) >> 16;
  592. h = drm_rect_height(&plane_state->src) >> 16;
  593. }
  594. if (width)
  595. *width = w;
  596. if (height)
  597. *height = h;
  598. }
  599. static int intel_fbc_calculate_cfb_size(struct intel_crtc *crtc)
  600. {
  601. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  602. struct drm_framebuffer *fb = crtc->base.primary->fb;
  603. int lines;
  604. intel_fbc_get_plane_source_size(crtc, NULL, &lines);
  605. if (INTEL_INFO(dev_priv)->gen >= 7)
  606. lines = min(lines, 2048);
  607. return lines * fb->pitches[0];
  608. }
  609. static int intel_fbc_setup_cfb(struct intel_crtc *crtc)
  610. {
  611. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  612. struct drm_framebuffer *fb = crtc->base.primary->fb;
  613. int size, cpp;
  614. size = intel_fbc_calculate_cfb_size(crtc);
  615. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  616. if (size <= dev_priv->fbc.uncompressed_size)
  617. return 0;
  618. /* Release any current block */
  619. __intel_fbc_cleanup_cfb(dev_priv);
  620. return intel_fbc_alloc_cfb(dev_priv, size, cpp);
  621. }
  622. static bool stride_is_valid(struct drm_i915_private *dev_priv,
  623. unsigned int stride)
  624. {
  625. /* These should have been caught earlier. */
  626. WARN_ON(stride < 512);
  627. WARN_ON((stride & (64 - 1)) != 0);
  628. /* Below are the additional FBC restrictions. */
  629. if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
  630. return stride == 4096 || stride == 8192;
  631. if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
  632. return false;
  633. if (stride > 16384)
  634. return false;
  635. return true;
  636. }
  637. static bool pixel_format_is_valid(struct drm_framebuffer *fb)
  638. {
  639. struct drm_device *dev = fb->dev;
  640. struct drm_i915_private *dev_priv = dev->dev_private;
  641. switch (fb->pixel_format) {
  642. case DRM_FORMAT_XRGB8888:
  643. case DRM_FORMAT_XBGR8888:
  644. return true;
  645. case DRM_FORMAT_XRGB1555:
  646. case DRM_FORMAT_RGB565:
  647. /* 16bpp not supported on gen2 */
  648. if (IS_GEN2(dev))
  649. return false;
  650. /* WaFbcOnly1to1Ratio:ctg */
  651. if (IS_G4X(dev_priv))
  652. return false;
  653. return true;
  654. default:
  655. return false;
  656. }
  657. }
  658. /*
  659. * For some reason, the hardware tracking starts looking at whatever we
  660. * programmed as the display plane base address register. It does not look at
  661. * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
  662. * variables instead of just looking at the pipe/plane size.
  663. */
  664. static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
  665. {
  666. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  667. unsigned int effective_w, effective_h, max_w, max_h;
  668. if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
  669. max_w = 4096;
  670. max_h = 4096;
  671. } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
  672. max_w = 4096;
  673. max_h = 2048;
  674. } else {
  675. max_w = 2048;
  676. max_h = 1536;
  677. }
  678. intel_fbc_get_plane_source_size(crtc, &effective_w, &effective_h);
  679. effective_w += crtc->adjusted_x;
  680. effective_h += crtc->adjusted_y;
  681. return effective_w <= max_w && effective_h <= max_h;
  682. }
  683. /**
  684. * __intel_fbc_update - enable/disable FBC as needed, unlocked
  685. * @dev_priv: i915 device instance
  686. *
  687. * Set up the framebuffer compression hardware at mode set time. We
  688. * enable it if possible:
  689. * - plane A only (on pre-965)
  690. * - no pixel mulitply/line duplication
  691. * - no alpha buffer discard
  692. * - no dual wide
  693. * - framebuffer <= max_hdisplay in width, max_vdisplay in height
  694. *
  695. * We can't assume that any compression will take place (worst case),
  696. * so the compressed buffer has to be the same size as the uncompressed
  697. * one. It also must reside (along with the line length buffer) in
  698. * stolen memory.
  699. *
  700. * We need to enable/disable FBC on a global basis.
  701. */
  702. static void __intel_fbc_update(struct drm_i915_private *dev_priv)
  703. {
  704. struct drm_crtc *crtc = NULL;
  705. struct intel_crtc *intel_crtc;
  706. struct drm_framebuffer *fb;
  707. struct drm_i915_gem_object *obj;
  708. const struct drm_display_mode *adjusted_mode;
  709. WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
  710. /* disable framebuffer compression in vGPU */
  711. if (intel_vgpu_active(dev_priv->dev))
  712. i915.enable_fbc = 0;
  713. if (i915.enable_fbc < 0) {
  714. set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT);
  715. goto out_disable;
  716. }
  717. if (!i915.enable_fbc) {
  718. set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM);
  719. goto out_disable;
  720. }
  721. /*
  722. * If FBC is already on, we just have to verify that we can
  723. * keep it that way...
  724. * Need to disable if:
  725. * - more than one pipe is active
  726. * - changing FBC params (stride, fence, mode)
  727. * - new fb is too large to fit in compressed buffer
  728. * - going to an unsupported config (interlace, pixel multiply, etc.)
  729. */
  730. crtc = intel_fbc_find_crtc(dev_priv);
  731. if (!crtc) {
  732. set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT);
  733. goto out_disable;
  734. }
  735. if (!multiple_pipes_ok(dev_priv)) {
  736. set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES);
  737. goto out_disable;
  738. }
  739. intel_crtc = to_intel_crtc(crtc);
  740. fb = crtc->primary->fb;
  741. obj = intel_fb_obj(fb);
  742. adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  743. if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  744. (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
  745. set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE);
  746. goto out_disable;
  747. }
  748. if (!intel_fbc_hw_tracking_covers_screen(intel_crtc)) {
  749. set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE);
  750. goto out_disable;
  751. }
  752. if ((INTEL_INFO(dev_priv)->gen < 4 || HAS_DDI(dev_priv)) &&
  753. intel_crtc->plane != PLANE_A) {
  754. set_no_fbc_reason(dev_priv, FBC_BAD_PLANE);
  755. goto out_disable;
  756. }
  757. /* The use of a CPU fence is mandatory in order to detect writes
  758. * by the CPU to the scanout and trigger updates to the FBC.
  759. */
  760. if (obj->tiling_mode != I915_TILING_X ||
  761. obj->fence_reg == I915_FENCE_REG_NONE) {
  762. set_no_fbc_reason(dev_priv, FBC_NOT_TILED);
  763. goto out_disable;
  764. }
  765. if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
  766. crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
  767. set_no_fbc_reason(dev_priv, FBC_ROTATION);
  768. goto out_disable;
  769. }
  770. if (!stride_is_valid(dev_priv, fb->pitches[0])) {
  771. set_no_fbc_reason(dev_priv, FBC_BAD_STRIDE);
  772. goto out_disable;
  773. }
  774. if (!pixel_format_is_valid(fb)) {
  775. set_no_fbc_reason(dev_priv, FBC_PIXEL_FORMAT);
  776. goto out_disable;
  777. }
  778. /* If the kernel debugger is active, always disable compression */
  779. if (in_dbg_master()) {
  780. set_no_fbc_reason(dev_priv, FBC_IN_DBG_MASTER);
  781. goto out_disable;
  782. }
  783. /* WaFbcExceedCdClockThreshold:hsw,bdw */
  784. if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
  785. ilk_pipe_pixel_rate(intel_crtc->config) >=
  786. dev_priv->cdclk_freq * 95 / 100) {
  787. set_no_fbc_reason(dev_priv, FBC_PIXEL_RATE);
  788. goto out_disable;
  789. }
  790. if (intel_fbc_setup_cfb(intel_crtc)) {
  791. set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL);
  792. goto out_disable;
  793. }
  794. /* If the scanout has not changed, don't modify the FBC settings.
  795. * Note that we make the fundamental assumption that the fb->obj
  796. * cannot be unpinned (and have its GTT offset and fence revoked)
  797. * without first being decoupled from the scanout and FBC disabled.
  798. */
  799. if (dev_priv->fbc.crtc == intel_crtc &&
  800. dev_priv->fbc.fb_id == fb->base.id &&
  801. dev_priv->fbc.y == crtc->y)
  802. return;
  803. if (intel_fbc_enabled(dev_priv)) {
  804. /* We update FBC along two paths, after changing fb/crtc
  805. * configuration (modeswitching) and after page-flipping
  806. * finishes. For the latter, we know that not only did
  807. * we disable the FBC at the start of the page-flip
  808. * sequence, but also more than one vblank has passed.
  809. *
  810. * For the former case of modeswitching, it is possible
  811. * to switch between two FBC valid configurations
  812. * instantaneously so we do need to disable the FBC
  813. * before we can modify its control registers. We also
  814. * have to wait for the next vblank for that to take
  815. * effect. However, since we delay enabling FBC we can
  816. * assume that a vblank has passed since disabling and
  817. * that we can safely alter the registers in the deferred
  818. * callback.
  819. *
  820. * In the scenario that we go from a valid to invalid
  821. * and then back to valid FBC configuration we have
  822. * no strict enforcement that a vblank occurred since
  823. * disabling the FBC. However, along all current pipe
  824. * disabling paths we do need to wait for a vblank at
  825. * some point. And we wait before enabling FBC anyway.
  826. */
  827. DRM_DEBUG_KMS("disabling active FBC for update\n");
  828. __intel_fbc_disable(dev_priv);
  829. }
  830. intel_fbc_schedule_enable(intel_crtc);
  831. dev_priv->fbc.no_fbc_reason = FBC_OK;
  832. return;
  833. out_disable:
  834. /* Multiple disables should be harmless */
  835. if (intel_fbc_enabled(dev_priv)) {
  836. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  837. __intel_fbc_disable(dev_priv);
  838. }
  839. __intel_fbc_cleanup_cfb(dev_priv);
  840. }
  841. /*
  842. * intel_fbc_update - enable/disable FBC as needed
  843. * @dev_priv: i915 device instance
  844. *
  845. * This function reevaluates the overall state and enables or disables FBC.
  846. */
  847. void intel_fbc_update(struct drm_i915_private *dev_priv)
  848. {
  849. if (!fbc_supported(dev_priv))
  850. return;
  851. mutex_lock(&dev_priv->fbc.lock);
  852. __intel_fbc_update(dev_priv);
  853. mutex_unlock(&dev_priv->fbc.lock);
  854. }
  855. void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
  856. unsigned int frontbuffer_bits,
  857. enum fb_op_origin origin)
  858. {
  859. unsigned int fbc_bits;
  860. if (!fbc_supported(dev_priv))
  861. return;
  862. if (origin == ORIGIN_GTT)
  863. return;
  864. mutex_lock(&dev_priv->fbc.lock);
  865. if (dev_priv->fbc.enabled)
  866. fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
  867. else if (dev_priv->fbc.fbc_work)
  868. fbc_bits = INTEL_FRONTBUFFER_PRIMARY(
  869. dev_priv->fbc.fbc_work->crtc->pipe);
  870. else
  871. fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
  872. dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
  873. if (dev_priv->fbc.busy_bits)
  874. __intel_fbc_disable(dev_priv);
  875. mutex_unlock(&dev_priv->fbc.lock);
  876. }
  877. void intel_fbc_flush(struct drm_i915_private *dev_priv,
  878. unsigned int frontbuffer_bits, enum fb_op_origin origin)
  879. {
  880. if (!fbc_supported(dev_priv))
  881. return;
  882. if (origin == ORIGIN_GTT)
  883. return;
  884. mutex_lock(&dev_priv->fbc.lock);
  885. dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
  886. if (!dev_priv->fbc.busy_bits) {
  887. __intel_fbc_disable(dev_priv);
  888. __intel_fbc_update(dev_priv);
  889. }
  890. mutex_unlock(&dev_priv->fbc.lock);
  891. }
  892. /**
  893. * intel_fbc_init - Initialize FBC
  894. * @dev_priv: the i915 device
  895. *
  896. * This function might be called during PM init process.
  897. */
  898. void intel_fbc_init(struct drm_i915_private *dev_priv)
  899. {
  900. enum pipe pipe;
  901. mutex_init(&dev_priv->fbc.lock);
  902. if (!HAS_FBC(dev_priv)) {
  903. dev_priv->fbc.enabled = false;
  904. dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
  905. return;
  906. }
  907. for_each_pipe(dev_priv, pipe) {
  908. dev_priv->fbc.possible_framebuffer_bits |=
  909. INTEL_FRONTBUFFER_PRIMARY(pipe);
  910. if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
  911. break;
  912. }
  913. if (INTEL_INFO(dev_priv)->gen >= 7) {
  914. dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
  915. dev_priv->fbc.enable_fbc = gen7_fbc_enable;
  916. dev_priv->fbc.disable_fbc = ilk_fbc_disable;
  917. } else if (INTEL_INFO(dev_priv)->gen >= 5) {
  918. dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
  919. dev_priv->fbc.enable_fbc = ilk_fbc_enable;
  920. dev_priv->fbc.disable_fbc = ilk_fbc_disable;
  921. } else if (IS_GM45(dev_priv)) {
  922. dev_priv->fbc.fbc_enabled = g4x_fbc_enabled;
  923. dev_priv->fbc.enable_fbc = g4x_fbc_enable;
  924. dev_priv->fbc.disable_fbc = g4x_fbc_disable;
  925. } else {
  926. dev_priv->fbc.fbc_enabled = i8xx_fbc_enabled;
  927. dev_priv->fbc.enable_fbc = i8xx_fbc_enable;
  928. dev_priv->fbc.disable_fbc = i8xx_fbc_disable;
  929. /* This value was pulled out of someone's hat */
  930. I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
  931. }
  932. dev_priv->fbc.enabled = dev_priv->fbc.fbc_enabled(dev_priv);
  933. }