intel_fbc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 void i8xx_fbc_disable(struct drm_device *dev)
  42. {
  43. struct drm_i915_private *dev_priv = dev->dev_private;
  44. u32 fbc_ctl;
  45. dev_priv->fbc.enabled = false;
  46. /* Disable compression */
  47. fbc_ctl = I915_READ(FBC_CONTROL);
  48. if ((fbc_ctl & FBC_CTL_EN) == 0)
  49. return;
  50. fbc_ctl &= ~FBC_CTL_EN;
  51. I915_WRITE(FBC_CONTROL, fbc_ctl);
  52. /* Wait for compressing bit to clear */
  53. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  54. DRM_DEBUG_KMS("FBC idle timed out\n");
  55. return;
  56. }
  57. DRM_DEBUG_KMS("disabled FBC\n");
  58. }
  59. static void i8xx_fbc_enable(struct drm_crtc *crtc)
  60. {
  61. struct drm_device *dev = crtc->dev;
  62. struct drm_i915_private *dev_priv = dev->dev_private;
  63. struct drm_framebuffer *fb = crtc->primary->fb;
  64. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  65. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  66. int cfb_pitch;
  67. int i;
  68. u32 fbc_ctl;
  69. dev_priv->fbc.enabled = true;
  70. /* Note: fbc.threshold == 1 for i8xx */
  71. cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
  72. if (fb->pitches[0] < cfb_pitch)
  73. cfb_pitch = fb->pitches[0];
  74. /* FBC_CTL wants 32B or 64B units */
  75. if (IS_GEN2(dev))
  76. cfb_pitch = (cfb_pitch / 32) - 1;
  77. else
  78. cfb_pitch = (cfb_pitch / 64) - 1;
  79. /* Clear old tags */
  80. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  81. I915_WRITE(FBC_TAG + (i * 4), 0);
  82. if (IS_GEN4(dev)) {
  83. u32 fbc_ctl2;
  84. /* Set it up... */
  85. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  86. fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
  87. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  88. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  89. }
  90. /* enable it... */
  91. fbc_ctl = I915_READ(FBC_CONTROL);
  92. fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
  93. fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
  94. if (IS_I945GM(dev))
  95. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  96. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  97. fbc_ctl |= obj->fence_reg;
  98. I915_WRITE(FBC_CONTROL, fbc_ctl);
  99. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
  100. cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
  101. }
  102. static bool i8xx_fbc_enabled(struct drm_device *dev)
  103. {
  104. struct drm_i915_private *dev_priv = dev->dev_private;
  105. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  106. }
  107. static void g4x_fbc_enable(struct drm_crtc *crtc)
  108. {
  109. struct drm_device *dev = crtc->dev;
  110. struct drm_i915_private *dev_priv = dev->dev_private;
  111. struct drm_framebuffer *fb = crtc->primary->fb;
  112. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  114. u32 dpfc_ctl;
  115. dev_priv->fbc.enabled = true;
  116. dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
  117. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  118. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  119. else
  120. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  121. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  122. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  123. /* enable it... */
  124. I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  125. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  126. }
  127. static void g4x_fbc_disable(struct drm_device *dev)
  128. {
  129. struct drm_i915_private *dev_priv = dev->dev_private;
  130. u32 dpfc_ctl;
  131. dev_priv->fbc.enabled = false;
  132. /* Disable compression */
  133. dpfc_ctl = I915_READ(DPFC_CONTROL);
  134. if (dpfc_ctl & DPFC_CTL_EN) {
  135. dpfc_ctl &= ~DPFC_CTL_EN;
  136. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  137. DRM_DEBUG_KMS("disabled FBC\n");
  138. }
  139. }
  140. static bool g4x_fbc_enabled(struct drm_device *dev)
  141. {
  142. struct drm_i915_private *dev_priv = dev->dev_private;
  143. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  144. }
  145. static void intel_fbc_nuke(struct drm_i915_private *dev_priv)
  146. {
  147. I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
  148. POSTING_READ(MSG_FBC_REND_STATE);
  149. }
  150. static void ilk_fbc_enable(struct drm_crtc *crtc)
  151. {
  152. struct drm_device *dev = crtc->dev;
  153. struct drm_i915_private *dev_priv = dev->dev_private;
  154. struct drm_framebuffer *fb = crtc->primary->fb;
  155. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  157. u32 dpfc_ctl;
  158. dev_priv->fbc.enabled = true;
  159. dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
  160. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  161. dev_priv->fbc.threshold++;
  162. switch (dev_priv->fbc.threshold) {
  163. case 4:
  164. case 3:
  165. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  166. break;
  167. case 2:
  168. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  169. break;
  170. case 1:
  171. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  172. break;
  173. }
  174. dpfc_ctl |= DPFC_CTL_FENCE_EN;
  175. if (IS_GEN5(dev))
  176. dpfc_ctl |= obj->fence_reg;
  177. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  178. I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
  179. /* enable it... */
  180. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  181. if (IS_GEN6(dev)) {
  182. I915_WRITE(SNB_DPFC_CTL_SA,
  183. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  184. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  185. }
  186. intel_fbc_nuke(dev_priv);
  187. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  188. }
  189. static void ilk_fbc_disable(struct drm_device *dev)
  190. {
  191. struct drm_i915_private *dev_priv = dev->dev_private;
  192. u32 dpfc_ctl;
  193. dev_priv->fbc.enabled = false;
  194. /* Disable compression */
  195. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  196. if (dpfc_ctl & DPFC_CTL_EN) {
  197. dpfc_ctl &= ~DPFC_CTL_EN;
  198. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  199. DRM_DEBUG_KMS("disabled FBC\n");
  200. }
  201. }
  202. static bool ilk_fbc_enabled(struct drm_device *dev)
  203. {
  204. struct drm_i915_private *dev_priv = dev->dev_private;
  205. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  206. }
  207. static void gen7_fbc_enable(struct drm_crtc *crtc)
  208. {
  209. struct drm_device *dev = crtc->dev;
  210. struct drm_i915_private *dev_priv = dev->dev_private;
  211. struct drm_framebuffer *fb = crtc->primary->fb;
  212. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  214. u32 dpfc_ctl;
  215. dev_priv->fbc.enabled = true;
  216. dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
  217. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  218. dev_priv->fbc.threshold++;
  219. switch (dev_priv->fbc.threshold) {
  220. case 4:
  221. case 3:
  222. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  223. break;
  224. case 2:
  225. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  226. break;
  227. case 1:
  228. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  229. break;
  230. }
  231. dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
  232. if (dev_priv->fbc.false_color)
  233. dpfc_ctl |= FBC_CTL_FALSE_COLOR;
  234. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  235. if (IS_IVYBRIDGE(dev)) {
  236. /* WaFbcAsynchFlipDisableFbcQueue:ivb */
  237. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  238. I915_READ(ILK_DISPLAY_CHICKEN1) |
  239. ILK_FBCQ_DIS);
  240. } else {
  241. /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
  242. I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
  243. I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
  244. HSW_FBCQ_DIS);
  245. }
  246. I915_WRITE(SNB_DPFC_CTL_SA,
  247. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  248. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  249. intel_fbc_nuke(dev_priv);
  250. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  251. }
  252. /**
  253. * intel_fbc_enabled - Is FBC enabled?
  254. * @dev: the drm_device
  255. *
  256. * This function is used to verify the current state of FBC.
  257. * FIXME: This should be tracked in the plane config eventually
  258. * instead of queried at runtime for most callers.
  259. */
  260. bool intel_fbc_enabled(struct drm_device *dev)
  261. {
  262. struct drm_i915_private *dev_priv = dev->dev_private;
  263. return dev_priv->fbc.enabled;
  264. }
  265. static void intel_fbc_work_fn(struct work_struct *__work)
  266. {
  267. struct intel_fbc_work *work =
  268. container_of(to_delayed_work(__work),
  269. struct intel_fbc_work, work);
  270. struct drm_device *dev = work->crtc->dev;
  271. struct drm_i915_private *dev_priv = dev->dev_private;
  272. mutex_lock(&dev->struct_mutex);
  273. if (work == dev_priv->fbc.fbc_work) {
  274. /* Double check that we haven't switched fb without cancelling
  275. * the prior work.
  276. */
  277. if (work->crtc->primary->fb == work->fb) {
  278. dev_priv->display.enable_fbc(work->crtc);
  279. dev_priv->fbc.crtc = to_intel_crtc(work->crtc);
  280. dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
  281. dev_priv->fbc.y = work->crtc->y;
  282. }
  283. dev_priv->fbc.fbc_work = NULL;
  284. }
  285. mutex_unlock(&dev->struct_mutex);
  286. kfree(work);
  287. }
  288. static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
  289. {
  290. if (dev_priv->fbc.fbc_work == NULL)
  291. return;
  292. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  293. /* Synchronisation is provided by struct_mutex and checking of
  294. * dev_priv->fbc.fbc_work, so we can perform the cancellation
  295. * entirely asynchronously.
  296. */
  297. if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
  298. /* tasklet was killed before being run, clean up */
  299. kfree(dev_priv->fbc.fbc_work);
  300. /* Mark the work as no longer wanted so that if it does
  301. * wake-up (because the work was already running and waiting
  302. * for our mutex), it will discover that is no longer
  303. * necessary to run.
  304. */
  305. dev_priv->fbc.fbc_work = NULL;
  306. }
  307. static void intel_fbc_enable(struct drm_crtc *crtc)
  308. {
  309. struct intel_fbc_work *work;
  310. struct drm_device *dev = crtc->dev;
  311. struct drm_i915_private *dev_priv = dev->dev_private;
  312. if (!dev_priv->display.enable_fbc)
  313. return;
  314. intel_fbc_cancel_work(dev_priv);
  315. work = kzalloc(sizeof(*work), GFP_KERNEL);
  316. if (work == NULL) {
  317. DRM_ERROR("Failed to allocate FBC work structure\n");
  318. dev_priv->display.enable_fbc(crtc);
  319. return;
  320. }
  321. work->crtc = crtc;
  322. work->fb = crtc->primary->fb;
  323. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  324. dev_priv->fbc.fbc_work = work;
  325. /* Delay the actual enabling to let pageflipping cease and the
  326. * display to settle before starting the compression. Note that
  327. * this delay also serves a second purpose: it allows for a
  328. * vblank to pass after disabling the FBC before we attempt
  329. * to modify the control registers.
  330. *
  331. * A more complicated solution would involve tracking vblanks
  332. * following the termination of the page-flipping sequence
  333. * and indeed performing the enable as a co-routine and not
  334. * waiting synchronously upon the vblank.
  335. *
  336. * WaFbcWaitForVBlankBeforeEnable:ilk,snb
  337. */
  338. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  339. }
  340. /**
  341. * intel_fbc_disable - disable FBC
  342. * @dev: the drm_device
  343. *
  344. * This function disables FBC.
  345. */
  346. void intel_fbc_disable(struct drm_device *dev)
  347. {
  348. struct drm_i915_private *dev_priv = dev->dev_private;
  349. intel_fbc_cancel_work(dev_priv);
  350. if (!dev_priv->display.disable_fbc)
  351. return;
  352. dev_priv->display.disable_fbc(dev);
  353. dev_priv->fbc.crtc = NULL;
  354. }
  355. static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
  356. enum no_fbc_reason reason)
  357. {
  358. if (dev_priv->fbc.no_fbc_reason == reason)
  359. return false;
  360. dev_priv->fbc.no_fbc_reason = reason;
  361. return true;
  362. }
  363. static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
  364. {
  365. struct drm_crtc *crtc = NULL, *tmp_crtc;
  366. enum pipe pipe;
  367. bool pipe_a_only = false, one_pipe_only = false;
  368. if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
  369. pipe_a_only = true;
  370. else if (INTEL_INFO(dev_priv)->gen <= 4)
  371. one_pipe_only = true;
  372. for_each_pipe(dev_priv, pipe) {
  373. tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  374. if (intel_crtc_active(tmp_crtc) &&
  375. to_intel_crtc(tmp_crtc)->primary_enabled) {
  376. if (one_pipe_only && crtc) {
  377. if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
  378. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  379. return NULL;
  380. }
  381. crtc = tmp_crtc;
  382. }
  383. if (pipe_a_only)
  384. break;
  385. }
  386. if (!crtc || crtc->primary->fb == NULL) {
  387. if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
  388. DRM_DEBUG_KMS("no output, disabling\n");
  389. return NULL;
  390. }
  391. return crtc;
  392. }
  393. /**
  394. * intel_fbc_update - enable/disable FBC as needed
  395. * @dev: the drm_device
  396. *
  397. * Set up the framebuffer compression hardware at mode set time. We
  398. * enable it if possible:
  399. * - plane A only (on pre-965)
  400. * - no pixel mulitply/line duplication
  401. * - no alpha buffer discard
  402. * - no dual wide
  403. * - framebuffer <= max_hdisplay in width, max_vdisplay in height
  404. *
  405. * We can't assume that any compression will take place (worst case),
  406. * so the compressed buffer has to be the same size as the uncompressed
  407. * one. It also must reside (along with the line length buffer) in
  408. * stolen memory.
  409. *
  410. * We need to enable/disable FBC on a global basis.
  411. */
  412. void intel_fbc_update(struct drm_device *dev)
  413. {
  414. struct drm_i915_private *dev_priv = dev->dev_private;
  415. struct drm_crtc *crtc = NULL;
  416. struct intel_crtc *intel_crtc;
  417. struct drm_framebuffer *fb;
  418. struct drm_i915_gem_object *obj;
  419. const struct drm_display_mode *adjusted_mode;
  420. unsigned int max_width, max_height;
  421. if (!HAS_FBC(dev))
  422. return;
  423. /* disable framebuffer compression in vGPU */
  424. if (intel_vgpu_active(dev))
  425. i915.enable_fbc = 0;
  426. if (i915.enable_fbc < 0) {
  427. if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
  428. DRM_DEBUG_KMS("disabled per chip default\n");
  429. goto out_disable;
  430. }
  431. if (!i915.enable_fbc) {
  432. if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
  433. DRM_DEBUG_KMS("fbc disabled per module param\n");
  434. goto out_disable;
  435. }
  436. /*
  437. * If FBC is already on, we just have to verify that we can
  438. * keep it that way...
  439. * Need to disable if:
  440. * - more than one pipe is active
  441. * - changing FBC params (stride, fence, mode)
  442. * - new fb is too large to fit in compressed buffer
  443. * - going to an unsupported config (interlace, pixel multiply, etc.)
  444. */
  445. crtc = intel_fbc_find_crtc(dev_priv);
  446. if (!crtc)
  447. goto out_disable;
  448. intel_crtc = to_intel_crtc(crtc);
  449. fb = crtc->primary->fb;
  450. obj = intel_fb_obj(fb);
  451. adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  452. if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  453. (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
  454. if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
  455. DRM_DEBUG_KMS("mode incompatible with compression, "
  456. "disabling\n");
  457. goto out_disable;
  458. }
  459. if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
  460. max_width = 4096;
  461. max_height = 4096;
  462. } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  463. max_width = 4096;
  464. max_height = 2048;
  465. } else {
  466. max_width = 2048;
  467. max_height = 1536;
  468. }
  469. if (intel_crtc->config->pipe_src_w > max_width ||
  470. intel_crtc->config->pipe_src_h > max_height) {
  471. if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
  472. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  473. goto out_disable;
  474. }
  475. if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
  476. intel_crtc->plane != PLANE_A) {
  477. if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
  478. DRM_DEBUG_KMS("plane not A, disabling compression\n");
  479. goto out_disable;
  480. }
  481. /* The use of a CPU fence is mandatory in order to detect writes
  482. * by the CPU to the scanout and trigger updates to the FBC.
  483. */
  484. if (obj->tiling_mode != I915_TILING_X ||
  485. obj->fence_reg == I915_FENCE_REG_NONE) {
  486. if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
  487. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  488. goto out_disable;
  489. }
  490. if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  491. crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
  492. if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
  493. DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
  494. goto out_disable;
  495. }
  496. /* If the kernel debugger is active, always disable compression */
  497. if (in_dbg_master())
  498. goto out_disable;
  499. if (i915_gem_stolen_setup_compression(dev, obj->base.size,
  500. drm_format_plane_cpp(fb->pixel_format, 0))) {
  501. if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
  502. DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
  503. goto out_disable;
  504. }
  505. /* If the scanout has not changed, don't modify the FBC settings.
  506. * Note that we make the fundamental assumption that the fb->obj
  507. * cannot be unpinned (and have its GTT offset and fence revoked)
  508. * without first being decoupled from the scanout and FBC disabled.
  509. */
  510. if (dev_priv->fbc.crtc == intel_crtc &&
  511. dev_priv->fbc.fb_id == fb->base.id &&
  512. dev_priv->fbc.y == crtc->y)
  513. return;
  514. if (intel_fbc_enabled(dev)) {
  515. /* We update FBC along two paths, after changing fb/crtc
  516. * configuration (modeswitching) and after page-flipping
  517. * finishes. For the latter, we know that not only did
  518. * we disable the FBC at the start of the page-flip
  519. * sequence, but also more than one vblank has passed.
  520. *
  521. * For the former case of modeswitching, it is possible
  522. * to switch between two FBC valid configurations
  523. * instantaneously so we do need to disable the FBC
  524. * before we can modify its control registers. We also
  525. * have to wait for the next vblank for that to take
  526. * effect. However, since we delay enabling FBC we can
  527. * assume that a vblank has passed since disabling and
  528. * that we can safely alter the registers in the deferred
  529. * callback.
  530. *
  531. * In the scenario that we go from a valid to invalid
  532. * and then back to valid FBC configuration we have
  533. * no strict enforcement that a vblank occurred since
  534. * disabling the FBC. However, along all current pipe
  535. * disabling paths we do need to wait for a vblank at
  536. * some point. And we wait before enabling FBC anyway.
  537. */
  538. DRM_DEBUG_KMS("disabling active FBC for update\n");
  539. intel_fbc_disable(dev);
  540. }
  541. intel_fbc_enable(crtc);
  542. dev_priv->fbc.no_fbc_reason = FBC_OK;
  543. return;
  544. out_disable:
  545. /* Multiple disables should be harmless */
  546. if (intel_fbc_enabled(dev)) {
  547. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  548. intel_fbc_disable(dev);
  549. }
  550. i915_gem_stolen_cleanup_compression(dev);
  551. }
  552. void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
  553. unsigned int frontbuffer_bits,
  554. enum fb_op_origin origin)
  555. {
  556. struct drm_device *dev = dev_priv->dev;
  557. unsigned int fbc_bits;
  558. if (origin == ORIGIN_GTT)
  559. return;
  560. if (dev_priv->fbc.enabled)
  561. fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
  562. else if (dev_priv->fbc.fbc_work)
  563. fbc_bits = INTEL_FRONTBUFFER_PRIMARY(
  564. to_intel_crtc(dev_priv->fbc.fbc_work->crtc)->pipe);
  565. else
  566. fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
  567. dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
  568. if (dev_priv->fbc.busy_bits)
  569. intel_fbc_disable(dev);
  570. }
  571. void intel_fbc_flush(struct drm_i915_private *dev_priv,
  572. unsigned int frontbuffer_bits)
  573. {
  574. struct drm_device *dev = dev_priv->dev;
  575. if (!dev_priv->fbc.busy_bits)
  576. return;
  577. dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
  578. if (!dev_priv->fbc.busy_bits)
  579. intel_fbc_update(dev);
  580. }
  581. /**
  582. * intel_fbc_init - Initialize FBC
  583. * @dev_priv: the i915 device
  584. *
  585. * This function might be called during PM init process.
  586. */
  587. void intel_fbc_init(struct drm_i915_private *dev_priv)
  588. {
  589. enum pipe pipe;
  590. if (!HAS_FBC(dev_priv)) {
  591. dev_priv->fbc.enabled = false;
  592. dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
  593. return;
  594. }
  595. for_each_pipe(dev_priv, pipe) {
  596. dev_priv->fbc.possible_framebuffer_bits |=
  597. INTEL_FRONTBUFFER_PRIMARY(pipe);
  598. if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
  599. break;
  600. }
  601. if (INTEL_INFO(dev_priv)->gen >= 7) {
  602. dev_priv->display.fbc_enabled = ilk_fbc_enabled;
  603. dev_priv->display.enable_fbc = gen7_fbc_enable;
  604. dev_priv->display.disable_fbc = ilk_fbc_disable;
  605. } else if (INTEL_INFO(dev_priv)->gen >= 5) {
  606. dev_priv->display.fbc_enabled = ilk_fbc_enabled;
  607. dev_priv->display.enable_fbc = ilk_fbc_enable;
  608. dev_priv->display.disable_fbc = ilk_fbc_disable;
  609. } else if (IS_GM45(dev_priv)) {
  610. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  611. dev_priv->display.enable_fbc = g4x_fbc_enable;
  612. dev_priv->display.disable_fbc = g4x_fbc_disable;
  613. } else {
  614. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  615. dev_priv->display.enable_fbc = i8xx_fbc_enable;
  616. dev_priv->display.disable_fbc = i8xx_fbc_disable;
  617. /* This value was pulled out of someone's hat */
  618. I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
  619. }
  620. dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
  621. }