intel_fbc.c 37 KB

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