intel_fbc.c 38 KB

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