intel_fbc.c 26 KB

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