intel_fbc.c 37 KB

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