intel_fbc.c 35 KB

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