intel_fbc.c 36 KB

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