intel_pm.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. /*
  2. * Copyright © 2012 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 DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eugeni Dodonov <eugeni.dodonov@intel.com>
  25. *
  26. */
  27. #include "i915_drv.h"
  28. #include "intel_drv.h"
  29. /* FBC, or Frame Buffer Compression, is a technique employed to compress the
  30. * framebuffer contents in-memory, aiming at reducing the required bandwidth
  31. * during in-memory transfers and, therefore, reduce the power packet.
  32. *
  33. * The benefits of FBC are mostly visible with solid backgrounds and
  34. * variation-less patterns.
  35. *
  36. * FBC-related functionality can be enabled by the means of the
  37. * i915.i915_enable_fbc parameter
  38. */
  39. void i8xx_disable_fbc(struct drm_device *dev)
  40. {
  41. struct drm_i915_private *dev_priv = dev->dev_private;
  42. u32 fbc_ctl;
  43. /* Disable compression */
  44. fbc_ctl = I915_READ(FBC_CONTROL);
  45. if ((fbc_ctl & FBC_CTL_EN) == 0)
  46. return;
  47. fbc_ctl &= ~FBC_CTL_EN;
  48. I915_WRITE(FBC_CONTROL, fbc_ctl);
  49. /* Wait for compressing bit to clear */
  50. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  51. DRM_DEBUG_KMS("FBC idle timed out\n");
  52. return;
  53. }
  54. DRM_DEBUG_KMS("disabled FBC\n");
  55. }
  56. void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  57. {
  58. struct drm_device *dev = crtc->dev;
  59. struct drm_i915_private *dev_priv = dev->dev_private;
  60. struct drm_framebuffer *fb = crtc->fb;
  61. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  62. struct drm_i915_gem_object *obj = intel_fb->obj;
  63. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  64. int cfb_pitch;
  65. int plane, i;
  66. u32 fbc_ctl, fbc_ctl2;
  67. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  68. if (fb->pitches[0] < cfb_pitch)
  69. cfb_pitch = fb->pitches[0];
  70. /* FBC_CTL wants 64B units */
  71. cfb_pitch = (cfb_pitch / 64) - 1;
  72. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  73. /* Clear old tags */
  74. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  75. I915_WRITE(FBC_TAG + (i * 4), 0);
  76. /* Set it up... */
  77. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  78. fbc_ctl2 |= plane;
  79. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  80. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  81. /* enable it... */
  82. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  83. if (IS_I945GM(dev))
  84. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  85. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  86. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  87. fbc_ctl |= obj->fence_reg;
  88. I915_WRITE(FBC_CONTROL, fbc_ctl);
  89. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  90. cfb_pitch, crtc->y, intel_crtc->plane);
  91. }
  92. bool i8xx_fbc_enabled(struct drm_device *dev)
  93. {
  94. struct drm_i915_private *dev_priv = dev->dev_private;
  95. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  96. }
  97. void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  98. {
  99. struct drm_device *dev = crtc->dev;
  100. struct drm_i915_private *dev_priv = dev->dev_private;
  101. struct drm_framebuffer *fb = crtc->fb;
  102. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  103. struct drm_i915_gem_object *obj = intel_fb->obj;
  104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  105. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  106. unsigned long stall_watermark = 200;
  107. u32 dpfc_ctl;
  108. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  109. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  110. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  111. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  112. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  113. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  114. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  115. /* enable it... */
  116. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  117. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  118. }
  119. void g4x_disable_fbc(struct drm_device *dev)
  120. {
  121. struct drm_i915_private *dev_priv = dev->dev_private;
  122. u32 dpfc_ctl;
  123. /* Disable compression */
  124. dpfc_ctl = I915_READ(DPFC_CONTROL);
  125. if (dpfc_ctl & DPFC_CTL_EN) {
  126. dpfc_ctl &= ~DPFC_CTL_EN;
  127. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  128. DRM_DEBUG_KMS("disabled FBC\n");
  129. }
  130. }
  131. bool g4x_fbc_enabled(struct drm_device *dev)
  132. {
  133. struct drm_i915_private *dev_priv = dev->dev_private;
  134. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  135. }
  136. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  137. {
  138. struct drm_i915_private *dev_priv = dev->dev_private;
  139. u32 blt_ecoskpd;
  140. /* Make sure blitter notifies FBC of writes */
  141. gen6_gt_force_wake_get(dev_priv);
  142. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  143. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  144. GEN6_BLITTER_LOCK_SHIFT;
  145. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  146. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  147. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  148. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  149. GEN6_BLITTER_LOCK_SHIFT);
  150. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  151. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  152. gen6_gt_force_wake_put(dev_priv);
  153. }
  154. void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  155. {
  156. struct drm_device *dev = crtc->dev;
  157. struct drm_i915_private *dev_priv = dev->dev_private;
  158. struct drm_framebuffer *fb = crtc->fb;
  159. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  160. struct drm_i915_gem_object *obj = intel_fb->obj;
  161. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  162. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  163. unsigned long stall_watermark = 200;
  164. u32 dpfc_ctl;
  165. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  166. dpfc_ctl &= DPFC_RESERVED;
  167. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  168. /* Set persistent mode for front-buffer rendering, ala X. */
  169. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  170. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  171. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  172. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  173. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  174. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  175. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  176. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  177. /* enable it... */
  178. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  179. if (IS_GEN6(dev)) {
  180. I915_WRITE(SNB_DPFC_CTL_SA,
  181. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  182. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  183. sandybridge_blit_fbc_update(dev);
  184. }
  185. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  186. }
  187. void ironlake_disable_fbc(struct drm_device *dev)
  188. {
  189. struct drm_i915_private *dev_priv = dev->dev_private;
  190. u32 dpfc_ctl;
  191. /* Disable compression */
  192. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  193. if (dpfc_ctl & DPFC_CTL_EN) {
  194. dpfc_ctl &= ~DPFC_CTL_EN;
  195. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  196. DRM_DEBUG_KMS("disabled FBC\n");
  197. }
  198. }
  199. bool ironlake_fbc_enabled(struct drm_device *dev)
  200. {
  201. struct drm_i915_private *dev_priv = dev->dev_private;
  202. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  203. }
  204. bool intel_fbc_enabled(struct drm_device *dev)
  205. {
  206. struct drm_i915_private *dev_priv = dev->dev_private;
  207. if (!dev_priv->display.fbc_enabled)
  208. return false;
  209. return dev_priv->display.fbc_enabled(dev);
  210. }
  211. static void intel_fbc_work_fn(struct work_struct *__work)
  212. {
  213. struct intel_fbc_work *work =
  214. container_of(to_delayed_work(__work),
  215. struct intel_fbc_work, work);
  216. struct drm_device *dev = work->crtc->dev;
  217. struct drm_i915_private *dev_priv = dev->dev_private;
  218. mutex_lock(&dev->struct_mutex);
  219. if (work == dev_priv->fbc_work) {
  220. /* Double check that we haven't switched fb without cancelling
  221. * the prior work.
  222. */
  223. if (work->crtc->fb == work->fb) {
  224. dev_priv->display.enable_fbc(work->crtc,
  225. work->interval);
  226. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  227. dev_priv->cfb_fb = work->crtc->fb->base.id;
  228. dev_priv->cfb_y = work->crtc->y;
  229. }
  230. dev_priv->fbc_work = NULL;
  231. }
  232. mutex_unlock(&dev->struct_mutex);
  233. kfree(work);
  234. }
  235. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  236. {
  237. if (dev_priv->fbc_work == NULL)
  238. return;
  239. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  240. /* Synchronisation is provided by struct_mutex and checking of
  241. * dev_priv->fbc_work, so we can perform the cancellation
  242. * entirely asynchronously.
  243. */
  244. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  245. /* tasklet was killed before being run, clean up */
  246. kfree(dev_priv->fbc_work);
  247. /* Mark the work as no longer wanted so that if it does
  248. * wake-up (because the work was already running and waiting
  249. * for our mutex), it will discover that is no longer
  250. * necessary to run.
  251. */
  252. dev_priv->fbc_work = NULL;
  253. }
  254. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  255. {
  256. struct intel_fbc_work *work;
  257. struct drm_device *dev = crtc->dev;
  258. struct drm_i915_private *dev_priv = dev->dev_private;
  259. if (!dev_priv->display.enable_fbc)
  260. return;
  261. intel_cancel_fbc_work(dev_priv);
  262. work = kzalloc(sizeof *work, GFP_KERNEL);
  263. if (work == NULL) {
  264. dev_priv->display.enable_fbc(crtc, interval);
  265. return;
  266. }
  267. work->crtc = crtc;
  268. work->fb = crtc->fb;
  269. work->interval = interval;
  270. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  271. dev_priv->fbc_work = work;
  272. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  273. /* Delay the actual enabling to let pageflipping cease and the
  274. * display to settle before starting the compression. Note that
  275. * this delay also serves a second purpose: it allows for a
  276. * vblank to pass after disabling the FBC before we attempt
  277. * to modify the control registers.
  278. *
  279. * A more complicated solution would involve tracking vblanks
  280. * following the termination of the page-flipping sequence
  281. * and indeed performing the enable as a co-routine and not
  282. * waiting synchronously upon the vblank.
  283. */
  284. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  285. }
  286. void intel_disable_fbc(struct drm_device *dev)
  287. {
  288. struct drm_i915_private *dev_priv = dev->dev_private;
  289. intel_cancel_fbc_work(dev_priv);
  290. if (!dev_priv->display.disable_fbc)
  291. return;
  292. dev_priv->display.disable_fbc(dev);
  293. dev_priv->cfb_plane = -1;
  294. }
  295. /**
  296. * intel_update_fbc - enable/disable FBC as needed
  297. * @dev: the drm_device
  298. *
  299. * Set up the framebuffer compression hardware at mode set time. We
  300. * enable it if possible:
  301. * - plane A only (on pre-965)
  302. * - no pixel mulitply/line duplication
  303. * - no alpha buffer discard
  304. * - no dual wide
  305. * - framebuffer <= 2048 in width, 1536 in height
  306. *
  307. * We can't assume that any compression will take place (worst case),
  308. * so the compressed buffer has to be the same size as the uncompressed
  309. * one. It also must reside (along with the line length buffer) in
  310. * stolen memory.
  311. *
  312. * We need to enable/disable FBC on a global basis.
  313. */
  314. void intel_update_fbc(struct drm_device *dev)
  315. {
  316. struct drm_i915_private *dev_priv = dev->dev_private;
  317. struct drm_crtc *crtc = NULL, *tmp_crtc;
  318. struct intel_crtc *intel_crtc;
  319. struct drm_framebuffer *fb;
  320. struct intel_framebuffer *intel_fb;
  321. struct drm_i915_gem_object *obj;
  322. int enable_fbc;
  323. DRM_DEBUG_KMS("\n");
  324. if (!i915_powersave)
  325. return;
  326. if (!I915_HAS_FBC(dev))
  327. return;
  328. /*
  329. * If FBC is already on, we just have to verify that we can
  330. * keep it that way...
  331. * Need to disable if:
  332. * - more than one pipe is active
  333. * - changing FBC params (stride, fence, mode)
  334. * - new fb is too large to fit in compressed buffer
  335. * - going to an unsupported config (interlace, pixel multiply, etc.)
  336. */
  337. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  338. if (tmp_crtc->enabled && tmp_crtc->fb) {
  339. if (crtc) {
  340. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  341. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  342. goto out_disable;
  343. }
  344. crtc = tmp_crtc;
  345. }
  346. }
  347. if (!crtc || crtc->fb == NULL) {
  348. DRM_DEBUG_KMS("no output, disabling\n");
  349. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  350. goto out_disable;
  351. }
  352. intel_crtc = to_intel_crtc(crtc);
  353. fb = crtc->fb;
  354. intel_fb = to_intel_framebuffer(fb);
  355. obj = intel_fb->obj;
  356. enable_fbc = i915_enable_fbc;
  357. if (enable_fbc < 0) {
  358. DRM_DEBUG_KMS("fbc set to per-chip default\n");
  359. enable_fbc = 1;
  360. if (INTEL_INFO(dev)->gen <= 6)
  361. enable_fbc = 0;
  362. }
  363. if (!enable_fbc) {
  364. DRM_DEBUG_KMS("fbc disabled per module param\n");
  365. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  366. goto out_disable;
  367. }
  368. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  369. DRM_DEBUG_KMS("framebuffer too large, disabling "
  370. "compression\n");
  371. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  372. goto out_disable;
  373. }
  374. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  375. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  376. DRM_DEBUG_KMS("mode incompatible with compression, "
  377. "disabling\n");
  378. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  379. goto out_disable;
  380. }
  381. if ((crtc->mode.hdisplay > 2048) ||
  382. (crtc->mode.vdisplay > 1536)) {
  383. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  384. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  385. goto out_disable;
  386. }
  387. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  388. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  389. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  390. goto out_disable;
  391. }
  392. /* The use of a CPU fence is mandatory in order to detect writes
  393. * by the CPU to the scanout and trigger updates to the FBC.
  394. */
  395. if (obj->tiling_mode != I915_TILING_X ||
  396. obj->fence_reg == I915_FENCE_REG_NONE) {
  397. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  398. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  399. goto out_disable;
  400. }
  401. /* If the kernel debugger is active, always disable compression */
  402. if (in_dbg_master())
  403. goto out_disable;
  404. /* If the scanout has not changed, don't modify the FBC settings.
  405. * Note that we make the fundamental assumption that the fb->obj
  406. * cannot be unpinned (and have its GTT offset and fence revoked)
  407. * without first being decoupled from the scanout and FBC disabled.
  408. */
  409. if (dev_priv->cfb_plane == intel_crtc->plane &&
  410. dev_priv->cfb_fb == fb->base.id &&
  411. dev_priv->cfb_y == crtc->y)
  412. return;
  413. if (intel_fbc_enabled(dev)) {
  414. /* We update FBC along two paths, after changing fb/crtc
  415. * configuration (modeswitching) and after page-flipping
  416. * finishes. For the latter, we know that not only did
  417. * we disable the FBC at the start of the page-flip
  418. * sequence, but also more than one vblank has passed.
  419. *
  420. * For the former case of modeswitching, it is possible
  421. * to switch between two FBC valid configurations
  422. * instantaneously so we do need to disable the FBC
  423. * before we can modify its control registers. We also
  424. * have to wait for the next vblank for that to take
  425. * effect. However, since we delay enabling FBC we can
  426. * assume that a vblank has passed since disabling and
  427. * that we can safely alter the registers in the deferred
  428. * callback.
  429. *
  430. * In the scenario that we go from a valid to invalid
  431. * and then back to valid FBC configuration we have
  432. * no strict enforcement that a vblank occurred since
  433. * disabling the FBC. However, along all current pipe
  434. * disabling paths we do need to wait for a vblank at
  435. * some point. And we wait before enabling FBC anyway.
  436. */
  437. DRM_DEBUG_KMS("disabling active FBC for update\n");
  438. intel_disable_fbc(dev);
  439. }
  440. intel_enable_fbc(crtc, 500);
  441. return;
  442. out_disable:
  443. /* Multiple disables should be harmless */
  444. if (intel_fbc_enabled(dev)) {
  445. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  446. intel_disable_fbc(dev);
  447. }
  448. }
  449. static const struct cxsr_latency cxsr_latency_table[] = {
  450. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  451. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  452. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  453. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  454. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  455. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  456. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  457. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  458. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  459. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  460. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  461. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  462. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  463. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  464. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  465. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  466. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  467. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  468. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  469. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  470. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  471. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  472. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  473. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  474. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  475. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  476. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  477. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  478. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  479. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  480. };
  481. const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  482. int is_ddr3,
  483. int fsb,
  484. int mem)
  485. {
  486. const struct cxsr_latency *latency;
  487. int i;
  488. if (fsb == 0 || mem == 0)
  489. return NULL;
  490. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  491. latency = &cxsr_latency_table[i];
  492. if (is_desktop == latency->is_desktop &&
  493. is_ddr3 == latency->is_ddr3 &&
  494. fsb == latency->fsb_freq && mem == latency->mem_freq)
  495. return latency;
  496. }
  497. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  498. return NULL;
  499. }
  500. void pineview_disable_cxsr(struct drm_device *dev)
  501. {
  502. struct drm_i915_private *dev_priv = dev->dev_private;
  503. /* deactivate cxsr */
  504. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  505. }
  506. /*
  507. * Latency for FIFO fetches is dependent on several factors:
  508. * - memory configuration (speed, channels)
  509. * - chipset
  510. * - current MCH state
  511. * It can be fairly high in some situations, so here we assume a fairly
  512. * pessimal value. It's a tradeoff between extra memory fetches (if we
  513. * set this value too high, the FIFO will fetch frequently to stay full)
  514. * and power consumption (set it too low to save power and we might see
  515. * FIFO underruns and display "flicker").
  516. *
  517. * A value of 5us seems to be a good balance; safe for very low end
  518. * platforms but not overly aggressive on lower latency configs.
  519. */
  520. static const int latency_ns = 5000;
  521. int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  522. {
  523. struct drm_i915_private *dev_priv = dev->dev_private;
  524. uint32_t dsparb = I915_READ(DSPARB);
  525. int size;
  526. size = dsparb & 0x7f;
  527. if (plane)
  528. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  529. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  530. plane ? "B" : "A", size);
  531. return size;
  532. }
  533. int i85x_get_fifo_size(struct drm_device *dev, int plane)
  534. {
  535. struct drm_i915_private *dev_priv = dev->dev_private;
  536. uint32_t dsparb = I915_READ(DSPARB);
  537. int size;
  538. size = dsparb & 0x1ff;
  539. if (plane)
  540. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  541. size >>= 1; /* Convert to cachelines */
  542. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  543. plane ? "B" : "A", size);
  544. return size;
  545. }
  546. int i845_get_fifo_size(struct drm_device *dev, int plane)
  547. {
  548. struct drm_i915_private *dev_priv = dev->dev_private;
  549. uint32_t dsparb = I915_READ(DSPARB);
  550. int size;
  551. size = dsparb & 0x7f;
  552. size >>= 2; /* Convert to cachelines */
  553. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  554. plane ? "B" : "A",
  555. size);
  556. return size;
  557. }
  558. int i830_get_fifo_size(struct drm_device *dev, int plane)
  559. {
  560. struct drm_i915_private *dev_priv = dev->dev_private;
  561. uint32_t dsparb = I915_READ(DSPARB);
  562. int size;
  563. size = dsparb & 0x7f;
  564. size >>= 1; /* Convert to cachelines */
  565. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  566. plane ? "B" : "A", size);
  567. return size;
  568. }
  569. /* Pineview has different values for various configs */
  570. static const struct intel_watermark_params pineview_display_wm = {
  571. PINEVIEW_DISPLAY_FIFO,
  572. PINEVIEW_MAX_WM,
  573. PINEVIEW_DFT_WM,
  574. PINEVIEW_GUARD_WM,
  575. PINEVIEW_FIFO_LINE_SIZE
  576. };
  577. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  578. PINEVIEW_DISPLAY_FIFO,
  579. PINEVIEW_MAX_WM,
  580. PINEVIEW_DFT_HPLLOFF_WM,
  581. PINEVIEW_GUARD_WM,
  582. PINEVIEW_FIFO_LINE_SIZE
  583. };
  584. static const struct intel_watermark_params pineview_cursor_wm = {
  585. PINEVIEW_CURSOR_FIFO,
  586. PINEVIEW_CURSOR_MAX_WM,
  587. PINEVIEW_CURSOR_DFT_WM,
  588. PINEVIEW_CURSOR_GUARD_WM,
  589. PINEVIEW_FIFO_LINE_SIZE,
  590. };
  591. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  592. PINEVIEW_CURSOR_FIFO,
  593. PINEVIEW_CURSOR_MAX_WM,
  594. PINEVIEW_CURSOR_DFT_WM,
  595. PINEVIEW_CURSOR_GUARD_WM,
  596. PINEVIEW_FIFO_LINE_SIZE
  597. };
  598. static const struct intel_watermark_params g4x_wm_info = {
  599. G4X_FIFO_SIZE,
  600. G4X_MAX_WM,
  601. G4X_MAX_WM,
  602. 2,
  603. G4X_FIFO_LINE_SIZE,
  604. };
  605. static const struct intel_watermark_params g4x_cursor_wm_info = {
  606. I965_CURSOR_FIFO,
  607. I965_CURSOR_MAX_WM,
  608. I965_CURSOR_DFT_WM,
  609. 2,
  610. G4X_FIFO_LINE_SIZE,
  611. };
  612. static const struct intel_watermark_params valleyview_wm_info = {
  613. VALLEYVIEW_FIFO_SIZE,
  614. VALLEYVIEW_MAX_WM,
  615. VALLEYVIEW_MAX_WM,
  616. 2,
  617. G4X_FIFO_LINE_SIZE,
  618. };
  619. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  620. I965_CURSOR_FIFO,
  621. VALLEYVIEW_CURSOR_MAX_WM,
  622. I965_CURSOR_DFT_WM,
  623. 2,
  624. G4X_FIFO_LINE_SIZE,
  625. };
  626. static const struct intel_watermark_params i965_cursor_wm_info = {
  627. I965_CURSOR_FIFO,
  628. I965_CURSOR_MAX_WM,
  629. I965_CURSOR_DFT_WM,
  630. 2,
  631. I915_FIFO_LINE_SIZE,
  632. };
  633. static const struct intel_watermark_params i945_wm_info = {
  634. I945_FIFO_SIZE,
  635. I915_MAX_WM,
  636. 1,
  637. 2,
  638. I915_FIFO_LINE_SIZE
  639. };
  640. static const struct intel_watermark_params i915_wm_info = {
  641. I915_FIFO_SIZE,
  642. I915_MAX_WM,
  643. 1,
  644. 2,
  645. I915_FIFO_LINE_SIZE
  646. };
  647. static const struct intel_watermark_params i855_wm_info = {
  648. I855GM_FIFO_SIZE,
  649. I915_MAX_WM,
  650. 1,
  651. 2,
  652. I830_FIFO_LINE_SIZE
  653. };
  654. static const struct intel_watermark_params i830_wm_info = {
  655. I830_FIFO_SIZE,
  656. I915_MAX_WM,
  657. 1,
  658. 2,
  659. I830_FIFO_LINE_SIZE
  660. };
  661. static const struct intel_watermark_params ironlake_display_wm_info = {
  662. ILK_DISPLAY_FIFO,
  663. ILK_DISPLAY_MAXWM,
  664. ILK_DISPLAY_DFTWM,
  665. 2,
  666. ILK_FIFO_LINE_SIZE
  667. };
  668. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  669. ILK_CURSOR_FIFO,
  670. ILK_CURSOR_MAXWM,
  671. ILK_CURSOR_DFTWM,
  672. 2,
  673. ILK_FIFO_LINE_SIZE
  674. };
  675. static const struct intel_watermark_params ironlake_display_srwm_info = {
  676. ILK_DISPLAY_SR_FIFO,
  677. ILK_DISPLAY_MAX_SRWM,
  678. ILK_DISPLAY_DFT_SRWM,
  679. 2,
  680. ILK_FIFO_LINE_SIZE
  681. };
  682. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  683. ILK_CURSOR_SR_FIFO,
  684. ILK_CURSOR_MAX_SRWM,
  685. ILK_CURSOR_DFT_SRWM,
  686. 2,
  687. ILK_FIFO_LINE_SIZE
  688. };
  689. static const struct intel_watermark_params sandybridge_display_wm_info = {
  690. SNB_DISPLAY_FIFO,
  691. SNB_DISPLAY_MAXWM,
  692. SNB_DISPLAY_DFTWM,
  693. 2,
  694. SNB_FIFO_LINE_SIZE
  695. };
  696. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  697. SNB_CURSOR_FIFO,
  698. SNB_CURSOR_MAXWM,
  699. SNB_CURSOR_DFTWM,
  700. 2,
  701. SNB_FIFO_LINE_SIZE
  702. };
  703. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  704. SNB_DISPLAY_SR_FIFO,
  705. SNB_DISPLAY_MAX_SRWM,
  706. SNB_DISPLAY_DFT_SRWM,
  707. 2,
  708. SNB_FIFO_LINE_SIZE
  709. };
  710. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  711. SNB_CURSOR_SR_FIFO,
  712. SNB_CURSOR_MAX_SRWM,
  713. SNB_CURSOR_DFT_SRWM,
  714. 2,
  715. SNB_FIFO_LINE_SIZE
  716. };
  717. /**
  718. * intel_calculate_wm - calculate watermark level
  719. * @clock_in_khz: pixel clock
  720. * @wm: chip FIFO params
  721. * @pixel_size: display pixel size
  722. * @latency_ns: memory latency for the platform
  723. *
  724. * Calculate the watermark level (the level at which the display plane will
  725. * start fetching from memory again). Each chip has a different display
  726. * FIFO size and allocation, so the caller needs to figure that out and pass
  727. * in the correct intel_watermark_params structure.
  728. *
  729. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  730. * on the pixel size. When it reaches the watermark level, it'll start
  731. * fetching FIFO line sized based chunks from memory until the FIFO fills
  732. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  733. * will occur, and a display engine hang could result.
  734. */
  735. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  736. const struct intel_watermark_params *wm,
  737. int fifo_size,
  738. int pixel_size,
  739. unsigned long latency_ns)
  740. {
  741. long entries_required, wm_size;
  742. /*
  743. * Note: we need to make sure we don't overflow for various clock &
  744. * latency values.
  745. * clocks go from a few thousand to several hundred thousand.
  746. * latency is usually a few thousand
  747. */
  748. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  749. 1000;
  750. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  751. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  752. wm_size = fifo_size - (entries_required + wm->guard_size);
  753. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  754. /* Don't promote wm_size to unsigned... */
  755. if (wm_size > (long)wm->max_wm)
  756. wm_size = wm->max_wm;
  757. if (wm_size <= 0)
  758. wm_size = wm->default_wm;
  759. return wm_size;
  760. }
  761. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  762. {
  763. struct drm_crtc *crtc, *enabled = NULL;
  764. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  765. if (crtc->enabled && crtc->fb) {
  766. if (enabled)
  767. return NULL;
  768. enabled = crtc;
  769. }
  770. }
  771. return enabled;
  772. }
  773. void pineview_update_wm(struct drm_device *dev)
  774. {
  775. struct drm_i915_private *dev_priv = dev->dev_private;
  776. struct drm_crtc *crtc;
  777. const struct cxsr_latency *latency;
  778. u32 reg;
  779. unsigned long wm;
  780. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  781. dev_priv->fsb_freq, dev_priv->mem_freq);
  782. if (!latency) {
  783. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  784. pineview_disable_cxsr(dev);
  785. return;
  786. }
  787. crtc = single_enabled_crtc(dev);
  788. if (crtc) {
  789. int clock = crtc->mode.clock;
  790. int pixel_size = crtc->fb->bits_per_pixel / 8;
  791. /* Display SR */
  792. wm = intel_calculate_wm(clock, &pineview_display_wm,
  793. pineview_display_wm.fifo_size,
  794. pixel_size, latency->display_sr);
  795. reg = I915_READ(DSPFW1);
  796. reg &= ~DSPFW_SR_MASK;
  797. reg |= wm << DSPFW_SR_SHIFT;
  798. I915_WRITE(DSPFW1, reg);
  799. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  800. /* cursor SR */
  801. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  802. pineview_display_wm.fifo_size,
  803. pixel_size, latency->cursor_sr);
  804. reg = I915_READ(DSPFW3);
  805. reg &= ~DSPFW_CURSOR_SR_MASK;
  806. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  807. I915_WRITE(DSPFW3, reg);
  808. /* Display HPLL off SR */
  809. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  810. pineview_display_hplloff_wm.fifo_size,
  811. pixel_size, latency->display_hpll_disable);
  812. reg = I915_READ(DSPFW3);
  813. reg &= ~DSPFW_HPLL_SR_MASK;
  814. reg |= wm & DSPFW_HPLL_SR_MASK;
  815. I915_WRITE(DSPFW3, reg);
  816. /* cursor HPLL off SR */
  817. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  818. pineview_display_hplloff_wm.fifo_size,
  819. pixel_size, latency->cursor_hpll_disable);
  820. reg = I915_READ(DSPFW3);
  821. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  822. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  823. I915_WRITE(DSPFW3, reg);
  824. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  825. /* activate cxsr */
  826. I915_WRITE(DSPFW3,
  827. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  828. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  829. } else {
  830. pineview_disable_cxsr(dev);
  831. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  832. }
  833. }
  834. static bool g4x_compute_wm0(struct drm_device *dev,
  835. int plane,
  836. const struct intel_watermark_params *display,
  837. int display_latency_ns,
  838. const struct intel_watermark_params *cursor,
  839. int cursor_latency_ns,
  840. int *plane_wm,
  841. int *cursor_wm)
  842. {
  843. struct drm_crtc *crtc;
  844. int htotal, hdisplay, clock, pixel_size;
  845. int line_time_us, line_count;
  846. int entries, tlb_miss;
  847. crtc = intel_get_crtc_for_plane(dev, plane);
  848. if (crtc->fb == NULL || !crtc->enabled) {
  849. *cursor_wm = cursor->guard_size;
  850. *plane_wm = display->guard_size;
  851. return false;
  852. }
  853. htotal = crtc->mode.htotal;
  854. hdisplay = crtc->mode.hdisplay;
  855. clock = crtc->mode.clock;
  856. pixel_size = crtc->fb->bits_per_pixel / 8;
  857. /* Use the small buffer method to calculate plane watermark */
  858. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  859. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  860. if (tlb_miss > 0)
  861. entries += tlb_miss;
  862. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  863. *plane_wm = entries + display->guard_size;
  864. if (*plane_wm > (int)display->max_wm)
  865. *plane_wm = display->max_wm;
  866. /* Use the large buffer method to calculate cursor watermark */
  867. line_time_us = ((htotal * 1000) / clock);
  868. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  869. entries = line_count * 64 * pixel_size;
  870. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  871. if (tlb_miss > 0)
  872. entries += tlb_miss;
  873. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  874. *cursor_wm = entries + cursor->guard_size;
  875. if (*cursor_wm > (int)cursor->max_wm)
  876. *cursor_wm = (int)cursor->max_wm;
  877. return true;
  878. }
  879. /*
  880. * Check the wm result.
  881. *
  882. * If any calculated watermark values is larger than the maximum value that
  883. * can be programmed into the associated watermark register, that watermark
  884. * must be disabled.
  885. */
  886. static bool g4x_check_srwm(struct drm_device *dev,
  887. int display_wm, int cursor_wm,
  888. const struct intel_watermark_params *display,
  889. const struct intel_watermark_params *cursor)
  890. {
  891. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  892. display_wm, cursor_wm);
  893. if (display_wm > display->max_wm) {
  894. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  895. display_wm, display->max_wm);
  896. return false;
  897. }
  898. if (cursor_wm > cursor->max_wm) {
  899. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  900. cursor_wm, cursor->max_wm);
  901. return false;
  902. }
  903. if (!(display_wm || cursor_wm)) {
  904. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  905. return false;
  906. }
  907. return true;
  908. }
  909. static bool g4x_compute_srwm(struct drm_device *dev,
  910. int plane,
  911. int latency_ns,
  912. const struct intel_watermark_params *display,
  913. const struct intel_watermark_params *cursor,
  914. int *display_wm, int *cursor_wm)
  915. {
  916. struct drm_crtc *crtc;
  917. int hdisplay, htotal, pixel_size, clock;
  918. unsigned long line_time_us;
  919. int line_count, line_size;
  920. int small, large;
  921. int entries;
  922. if (!latency_ns) {
  923. *display_wm = *cursor_wm = 0;
  924. return false;
  925. }
  926. crtc = intel_get_crtc_for_plane(dev, plane);
  927. hdisplay = crtc->mode.hdisplay;
  928. htotal = crtc->mode.htotal;
  929. clock = crtc->mode.clock;
  930. pixel_size = crtc->fb->bits_per_pixel / 8;
  931. line_time_us = (htotal * 1000) / clock;
  932. line_count = (latency_ns / line_time_us + 1000) / 1000;
  933. line_size = hdisplay * pixel_size;
  934. /* Use the minimum of the small and large buffer method for primary */
  935. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  936. large = line_count * line_size;
  937. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  938. *display_wm = entries + display->guard_size;
  939. /* calculate the self-refresh watermark for display cursor */
  940. entries = line_count * pixel_size * 64;
  941. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  942. *cursor_wm = entries + cursor->guard_size;
  943. return g4x_check_srwm(dev,
  944. *display_wm, *cursor_wm,
  945. display, cursor);
  946. }
  947. static bool vlv_compute_drain_latency(struct drm_device *dev,
  948. int plane,
  949. int *plane_prec_mult,
  950. int *plane_dl,
  951. int *cursor_prec_mult,
  952. int *cursor_dl)
  953. {
  954. struct drm_crtc *crtc;
  955. int clock, pixel_size;
  956. int entries;
  957. crtc = intel_get_crtc_for_plane(dev, plane);
  958. if (crtc->fb == NULL || !crtc->enabled)
  959. return false;
  960. clock = crtc->mode.clock; /* VESA DOT Clock */
  961. pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
  962. entries = (clock / 1000) * pixel_size;
  963. *plane_prec_mult = (entries > 256) ?
  964. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  965. *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
  966. pixel_size);
  967. entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
  968. *cursor_prec_mult = (entries > 256) ?
  969. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  970. *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
  971. return true;
  972. }
  973. /*
  974. * Update drain latency registers of memory arbiter
  975. *
  976. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  977. * to be programmed. Each plane has a drain latency multiplier and a drain
  978. * latency value.
  979. */
  980. static void vlv_update_drain_latency(struct drm_device *dev)
  981. {
  982. struct drm_i915_private *dev_priv = dev->dev_private;
  983. int planea_prec, planea_dl, planeb_prec, planeb_dl;
  984. int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
  985. int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
  986. either 16 or 32 */
  987. /* For plane A, Cursor A */
  988. if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
  989. &cursor_prec_mult, &cursora_dl)) {
  990. cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  991. DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
  992. planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  993. DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
  994. I915_WRITE(VLV_DDL1, cursora_prec |
  995. (cursora_dl << DDL_CURSORA_SHIFT) |
  996. planea_prec | planea_dl);
  997. }
  998. /* For plane B, Cursor B */
  999. if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
  1000. &cursor_prec_mult, &cursorb_dl)) {
  1001. cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1002. DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
  1003. planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1004. DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
  1005. I915_WRITE(VLV_DDL2, cursorb_prec |
  1006. (cursorb_dl << DDL_CURSORB_SHIFT) |
  1007. planeb_prec | planeb_dl);
  1008. }
  1009. }
  1010. #define single_plane_enabled(mask) is_power_of_2(mask)
  1011. void valleyview_update_wm(struct drm_device *dev)
  1012. {
  1013. static const int sr_latency_ns = 12000;
  1014. struct drm_i915_private *dev_priv = dev->dev_private;
  1015. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1016. int plane_sr, cursor_sr;
  1017. unsigned int enabled = 0;
  1018. vlv_update_drain_latency(dev);
  1019. if (g4x_compute_wm0(dev, 0,
  1020. &valleyview_wm_info, latency_ns,
  1021. &valleyview_cursor_wm_info, latency_ns,
  1022. &planea_wm, &cursora_wm))
  1023. enabled |= 1;
  1024. if (g4x_compute_wm0(dev, 1,
  1025. &valleyview_wm_info, latency_ns,
  1026. &valleyview_cursor_wm_info, latency_ns,
  1027. &planeb_wm, &cursorb_wm))
  1028. enabled |= 2;
  1029. plane_sr = cursor_sr = 0;
  1030. if (single_plane_enabled(enabled) &&
  1031. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1032. sr_latency_ns,
  1033. &valleyview_wm_info,
  1034. &valleyview_cursor_wm_info,
  1035. &plane_sr, &cursor_sr))
  1036. I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
  1037. else
  1038. I915_WRITE(FW_BLC_SELF_VLV,
  1039. I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
  1040. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1041. planea_wm, cursora_wm,
  1042. planeb_wm, cursorb_wm,
  1043. plane_sr, cursor_sr);
  1044. I915_WRITE(DSPFW1,
  1045. (plane_sr << DSPFW_SR_SHIFT) |
  1046. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1047. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1048. planea_wm);
  1049. I915_WRITE(DSPFW2,
  1050. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  1051. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1052. I915_WRITE(DSPFW3,
  1053. (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
  1054. }
  1055. void g4x_update_wm(struct drm_device *dev)
  1056. {
  1057. static const int sr_latency_ns = 12000;
  1058. struct drm_i915_private *dev_priv = dev->dev_private;
  1059. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1060. int plane_sr, cursor_sr;
  1061. unsigned int enabled = 0;
  1062. if (g4x_compute_wm0(dev, 0,
  1063. &g4x_wm_info, latency_ns,
  1064. &g4x_cursor_wm_info, latency_ns,
  1065. &planea_wm, &cursora_wm))
  1066. enabled |= 1;
  1067. if (g4x_compute_wm0(dev, 1,
  1068. &g4x_wm_info, latency_ns,
  1069. &g4x_cursor_wm_info, latency_ns,
  1070. &planeb_wm, &cursorb_wm))
  1071. enabled |= 2;
  1072. plane_sr = cursor_sr = 0;
  1073. if (single_plane_enabled(enabled) &&
  1074. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1075. sr_latency_ns,
  1076. &g4x_wm_info,
  1077. &g4x_cursor_wm_info,
  1078. &plane_sr, &cursor_sr))
  1079. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1080. else
  1081. I915_WRITE(FW_BLC_SELF,
  1082. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  1083. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1084. planea_wm, cursora_wm,
  1085. planeb_wm, cursorb_wm,
  1086. plane_sr, cursor_sr);
  1087. I915_WRITE(DSPFW1,
  1088. (plane_sr << DSPFW_SR_SHIFT) |
  1089. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1090. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1091. planea_wm);
  1092. I915_WRITE(DSPFW2,
  1093. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  1094. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1095. /* HPLL off in SR has some issues on G4x... disable it */
  1096. I915_WRITE(DSPFW3,
  1097. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  1098. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1099. }
  1100. void i965_update_wm(struct drm_device *dev)
  1101. {
  1102. struct drm_i915_private *dev_priv = dev->dev_private;
  1103. struct drm_crtc *crtc;
  1104. int srwm = 1;
  1105. int cursor_sr = 16;
  1106. /* Calc sr entries for one plane configs */
  1107. crtc = single_enabled_crtc(dev);
  1108. if (crtc) {
  1109. /* self-refresh has much higher latency */
  1110. static const int sr_latency_ns = 12000;
  1111. int clock = crtc->mode.clock;
  1112. int htotal = crtc->mode.htotal;
  1113. int hdisplay = crtc->mode.hdisplay;
  1114. int pixel_size = crtc->fb->bits_per_pixel / 8;
  1115. unsigned long line_time_us;
  1116. int entries;
  1117. line_time_us = ((htotal * 1000) / clock);
  1118. /* Use ns/us then divide to preserve precision */
  1119. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1120. pixel_size * hdisplay;
  1121. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1122. srwm = I965_FIFO_SIZE - entries;
  1123. if (srwm < 0)
  1124. srwm = 1;
  1125. srwm &= 0x1ff;
  1126. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1127. entries, srwm);
  1128. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1129. pixel_size * 64;
  1130. entries = DIV_ROUND_UP(entries,
  1131. i965_cursor_wm_info.cacheline_size);
  1132. cursor_sr = i965_cursor_wm_info.fifo_size -
  1133. (entries + i965_cursor_wm_info.guard_size);
  1134. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1135. cursor_sr = i965_cursor_wm_info.max_wm;
  1136. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1137. "cursor %d\n", srwm, cursor_sr);
  1138. if (IS_CRESTLINE(dev))
  1139. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1140. } else {
  1141. /* Turn off self refresh if both pipes are enabled */
  1142. if (IS_CRESTLINE(dev))
  1143. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  1144. & ~FW_BLC_SELF_EN);
  1145. }
  1146. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1147. srwm);
  1148. /* 965 has limitations... */
  1149. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  1150. (8 << 16) | (8 << 8) | (8 << 0));
  1151. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  1152. /* update cursor SR watermark */
  1153. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1154. }
  1155. void i9xx_update_wm(struct drm_device *dev)
  1156. {
  1157. struct drm_i915_private *dev_priv = dev->dev_private;
  1158. const struct intel_watermark_params *wm_info;
  1159. uint32_t fwater_lo;
  1160. uint32_t fwater_hi;
  1161. int cwm, srwm = 1;
  1162. int fifo_size;
  1163. int planea_wm, planeb_wm;
  1164. struct drm_crtc *crtc, *enabled = NULL;
  1165. if (IS_I945GM(dev))
  1166. wm_info = &i945_wm_info;
  1167. else if (!IS_GEN2(dev))
  1168. wm_info = &i915_wm_info;
  1169. else
  1170. wm_info = &i855_wm_info;
  1171. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1172. crtc = intel_get_crtc_for_plane(dev, 0);
  1173. if (crtc->enabled && crtc->fb) {
  1174. planea_wm = intel_calculate_wm(crtc->mode.clock,
  1175. wm_info, fifo_size,
  1176. crtc->fb->bits_per_pixel / 8,
  1177. latency_ns);
  1178. enabled = crtc;
  1179. } else
  1180. planea_wm = fifo_size - wm_info->guard_size;
  1181. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1182. crtc = intel_get_crtc_for_plane(dev, 1);
  1183. if (crtc->enabled && crtc->fb) {
  1184. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  1185. wm_info, fifo_size,
  1186. crtc->fb->bits_per_pixel / 8,
  1187. latency_ns);
  1188. if (enabled == NULL)
  1189. enabled = crtc;
  1190. else
  1191. enabled = NULL;
  1192. } else
  1193. planeb_wm = fifo_size - wm_info->guard_size;
  1194. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1195. /*
  1196. * Overlay gets an aggressive default since video jitter is bad.
  1197. */
  1198. cwm = 2;
  1199. /* Play safe and disable self-refresh before adjusting watermarks. */
  1200. if (IS_I945G(dev) || IS_I945GM(dev))
  1201. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  1202. else if (IS_I915GM(dev))
  1203. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  1204. /* Calc sr entries for one plane configs */
  1205. if (HAS_FW_BLC(dev) && enabled) {
  1206. /* self-refresh has much higher latency */
  1207. static const int sr_latency_ns = 6000;
  1208. int clock = enabled->mode.clock;
  1209. int htotal = enabled->mode.htotal;
  1210. int hdisplay = enabled->mode.hdisplay;
  1211. int pixel_size = enabled->fb->bits_per_pixel / 8;
  1212. unsigned long line_time_us;
  1213. int entries;
  1214. line_time_us = (htotal * 1000) / clock;
  1215. /* Use ns/us then divide to preserve precision */
  1216. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1217. pixel_size * hdisplay;
  1218. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1219. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1220. srwm = wm_info->fifo_size - entries;
  1221. if (srwm < 0)
  1222. srwm = 1;
  1223. if (IS_I945G(dev) || IS_I945GM(dev))
  1224. I915_WRITE(FW_BLC_SELF,
  1225. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1226. else if (IS_I915GM(dev))
  1227. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1228. }
  1229. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1230. planea_wm, planeb_wm, cwm, srwm);
  1231. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1232. fwater_hi = (cwm & 0x1f);
  1233. /* Set request length to 8 cachelines per fetch */
  1234. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1235. fwater_hi = fwater_hi | (1 << 8);
  1236. I915_WRITE(FW_BLC, fwater_lo);
  1237. I915_WRITE(FW_BLC2, fwater_hi);
  1238. if (HAS_FW_BLC(dev)) {
  1239. if (enabled) {
  1240. if (IS_I945G(dev) || IS_I945GM(dev))
  1241. I915_WRITE(FW_BLC_SELF,
  1242. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  1243. else if (IS_I915GM(dev))
  1244. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  1245. DRM_DEBUG_KMS("memory self refresh enabled\n");
  1246. } else
  1247. DRM_DEBUG_KMS("memory self refresh disabled\n");
  1248. }
  1249. }
  1250. void i830_update_wm(struct drm_device *dev)
  1251. {
  1252. struct drm_i915_private *dev_priv = dev->dev_private;
  1253. struct drm_crtc *crtc;
  1254. uint32_t fwater_lo;
  1255. int planea_wm;
  1256. crtc = single_enabled_crtc(dev);
  1257. if (crtc == NULL)
  1258. return;
  1259. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  1260. dev_priv->display.get_fifo_size(dev, 0),
  1261. crtc->fb->bits_per_pixel / 8,
  1262. latency_ns);
  1263. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1264. fwater_lo |= (3<<8) | planea_wm;
  1265. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1266. I915_WRITE(FW_BLC, fwater_lo);
  1267. }
  1268. #define ILK_LP0_PLANE_LATENCY 700
  1269. #define ILK_LP0_CURSOR_LATENCY 1300
  1270. /*
  1271. * Check the wm result.
  1272. *
  1273. * If any calculated watermark values is larger than the maximum value that
  1274. * can be programmed into the associated watermark register, that watermark
  1275. * must be disabled.
  1276. */
  1277. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  1278. int fbc_wm, int display_wm, int cursor_wm,
  1279. const struct intel_watermark_params *display,
  1280. const struct intel_watermark_params *cursor)
  1281. {
  1282. struct drm_i915_private *dev_priv = dev->dev_private;
  1283. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  1284. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  1285. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  1286. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  1287. fbc_wm, SNB_FBC_MAX_SRWM, level);
  1288. /* fbc has it's own way to disable FBC WM */
  1289. I915_WRITE(DISP_ARB_CTL,
  1290. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  1291. return false;
  1292. }
  1293. if (display_wm > display->max_wm) {
  1294. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  1295. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  1296. return false;
  1297. }
  1298. if (cursor_wm > cursor->max_wm) {
  1299. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  1300. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  1301. return false;
  1302. }
  1303. if (!(fbc_wm || display_wm || cursor_wm)) {
  1304. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  1305. return false;
  1306. }
  1307. return true;
  1308. }
  1309. /*
  1310. * Compute watermark values of WM[1-3],
  1311. */
  1312. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  1313. int latency_ns,
  1314. const struct intel_watermark_params *display,
  1315. const struct intel_watermark_params *cursor,
  1316. int *fbc_wm, int *display_wm, int *cursor_wm)
  1317. {
  1318. struct drm_crtc *crtc;
  1319. unsigned long line_time_us;
  1320. int hdisplay, htotal, pixel_size, clock;
  1321. int line_count, line_size;
  1322. int small, large;
  1323. int entries;
  1324. if (!latency_ns) {
  1325. *fbc_wm = *display_wm = *cursor_wm = 0;
  1326. return false;
  1327. }
  1328. crtc = intel_get_crtc_for_plane(dev, plane);
  1329. hdisplay = crtc->mode.hdisplay;
  1330. htotal = crtc->mode.htotal;
  1331. clock = crtc->mode.clock;
  1332. pixel_size = crtc->fb->bits_per_pixel / 8;
  1333. line_time_us = (htotal * 1000) / clock;
  1334. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1335. line_size = hdisplay * pixel_size;
  1336. /* Use the minimum of the small and large buffer method for primary */
  1337. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1338. large = line_count * line_size;
  1339. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1340. *display_wm = entries + display->guard_size;
  1341. /*
  1342. * Spec says:
  1343. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  1344. */
  1345. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  1346. /* calculate the self-refresh watermark for display cursor */
  1347. entries = line_count * pixel_size * 64;
  1348. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1349. *cursor_wm = entries + cursor->guard_size;
  1350. return ironlake_check_srwm(dev, level,
  1351. *fbc_wm, *display_wm, *cursor_wm,
  1352. display, cursor);
  1353. }
  1354. void ironlake_update_wm(struct drm_device *dev)
  1355. {
  1356. struct drm_i915_private *dev_priv = dev->dev_private;
  1357. int fbc_wm, plane_wm, cursor_wm;
  1358. unsigned int enabled;
  1359. enabled = 0;
  1360. if (g4x_compute_wm0(dev, 0,
  1361. &ironlake_display_wm_info,
  1362. ILK_LP0_PLANE_LATENCY,
  1363. &ironlake_cursor_wm_info,
  1364. ILK_LP0_CURSOR_LATENCY,
  1365. &plane_wm, &cursor_wm)) {
  1366. I915_WRITE(WM0_PIPEA_ILK,
  1367. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1368. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1369. " plane %d, " "cursor: %d\n",
  1370. plane_wm, cursor_wm);
  1371. enabled |= 1;
  1372. }
  1373. if (g4x_compute_wm0(dev, 1,
  1374. &ironlake_display_wm_info,
  1375. ILK_LP0_PLANE_LATENCY,
  1376. &ironlake_cursor_wm_info,
  1377. ILK_LP0_CURSOR_LATENCY,
  1378. &plane_wm, &cursor_wm)) {
  1379. I915_WRITE(WM0_PIPEB_ILK,
  1380. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1381. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1382. " plane %d, cursor: %d\n",
  1383. plane_wm, cursor_wm);
  1384. enabled |= 2;
  1385. }
  1386. /*
  1387. * Calculate and update the self-refresh watermark only when one
  1388. * display plane is used.
  1389. */
  1390. I915_WRITE(WM3_LP_ILK, 0);
  1391. I915_WRITE(WM2_LP_ILK, 0);
  1392. I915_WRITE(WM1_LP_ILK, 0);
  1393. if (!single_plane_enabled(enabled))
  1394. return;
  1395. enabled = ffs(enabled) - 1;
  1396. /* WM1 */
  1397. if (!ironlake_compute_srwm(dev, 1, enabled,
  1398. ILK_READ_WM1_LATENCY() * 500,
  1399. &ironlake_display_srwm_info,
  1400. &ironlake_cursor_srwm_info,
  1401. &fbc_wm, &plane_wm, &cursor_wm))
  1402. return;
  1403. I915_WRITE(WM1_LP_ILK,
  1404. WM1_LP_SR_EN |
  1405. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1406. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1407. (plane_wm << WM1_LP_SR_SHIFT) |
  1408. cursor_wm);
  1409. /* WM2 */
  1410. if (!ironlake_compute_srwm(dev, 2, enabled,
  1411. ILK_READ_WM2_LATENCY() * 500,
  1412. &ironlake_display_srwm_info,
  1413. &ironlake_cursor_srwm_info,
  1414. &fbc_wm, &plane_wm, &cursor_wm))
  1415. return;
  1416. I915_WRITE(WM2_LP_ILK,
  1417. WM2_LP_EN |
  1418. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1419. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1420. (plane_wm << WM1_LP_SR_SHIFT) |
  1421. cursor_wm);
  1422. /*
  1423. * WM3 is unsupported on ILK, probably because we don't have latency
  1424. * data for that power state
  1425. */
  1426. }
  1427. void sandybridge_update_wm(struct drm_device *dev)
  1428. {
  1429. struct drm_i915_private *dev_priv = dev->dev_private;
  1430. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1431. u32 val;
  1432. int fbc_wm, plane_wm, cursor_wm;
  1433. unsigned int enabled;
  1434. enabled = 0;
  1435. if (g4x_compute_wm0(dev, 0,
  1436. &sandybridge_display_wm_info, latency,
  1437. &sandybridge_cursor_wm_info, latency,
  1438. &plane_wm, &cursor_wm)) {
  1439. val = I915_READ(WM0_PIPEA_ILK);
  1440. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1441. I915_WRITE(WM0_PIPEA_ILK, val |
  1442. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1443. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1444. " plane %d, " "cursor: %d\n",
  1445. plane_wm, cursor_wm);
  1446. enabled |= 1;
  1447. }
  1448. if (g4x_compute_wm0(dev, 1,
  1449. &sandybridge_display_wm_info, latency,
  1450. &sandybridge_cursor_wm_info, latency,
  1451. &plane_wm, &cursor_wm)) {
  1452. val = I915_READ(WM0_PIPEB_ILK);
  1453. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1454. I915_WRITE(WM0_PIPEB_ILK, val |
  1455. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1456. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1457. " plane %d, cursor: %d\n",
  1458. plane_wm, cursor_wm);
  1459. enabled |= 2;
  1460. }
  1461. /* IVB has 3 pipes */
  1462. if (IS_IVYBRIDGE(dev) &&
  1463. g4x_compute_wm0(dev, 2,
  1464. &sandybridge_display_wm_info, latency,
  1465. &sandybridge_cursor_wm_info, latency,
  1466. &plane_wm, &cursor_wm)) {
  1467. val = I915_READ(WM0_PIPEC_IVB);
  1468. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1469. I915_WRITE(WM0_PIPEC_IVB, val |
  1470. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1471. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  1472. " plane %d, cursor: %d\n",
  1473. plane_wm, cursor_wm);
  1474. enabled |= 3;
  1475. }
  1476. /*
  1477. * Calculate and update the self-refresh watermark only when one
  1478. * display plane is used.
  1479. *
  1480. * SNB support 3 levels of watermark.
  1481. *
  1482. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1483. * and disabled in the descending order
  1484. *
  1485. */
  1486. I915_WRITE(WM3_LP_ILK, 0);
  1487. I915_WRITE(WM2_LP_ILK, 0);
  1488. I915_WRITE(WM1_LP_ILK, 0);
  1489. if (!single_plane_enabled(enabled) ||
  1490. dev_priv->sprite_scaling_enabled)
  1491. return;
  1492. enabled = ffs(enabled) - 1;
  1493. /* WM1 */
  1494. if (!ironlake_compute_srwm(dev, 1, enabled,
  1495. SNB_READ_WM1_LATENCY() * 500,
  1496. &sandybridge_display_srwm_info,
  1497. &sandybridge_cursor_srwm_info,
  1498. &fbc_wm, &plane_wm, &cursor_wm))
  1499. return;
  1500. I915_WRITE(WM1_LP_ILK,
  1501. WM1_LP_SR_EN |
  1502. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1503. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1504. (plane_wm << WM1_LP_SR_SHIFT) |
  1505. cursor_wm);
  1506. /* WM2 */
  1507. if (!ironlake_compute_srwm(dev, 2, enabled,
  1508. SNB_READ_WM2_LATENCY() * 500,
  1509. &sandybridge_display_srwm_info,
  1510. &sandybridge_cursor_srwm_info,
  1511. &fbc_wm, &plane_wm, &cursor_wm))
  1512. return;
  1513. I915_WRITE(WM2_LP_ILK,
  1514. WM2_LP_EN |
  1515. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1516. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1517. (plane_wm << WM1_LP_SR_SHIFT) |
  1518. cursor_wm);
  1519. /* WM3 */
  1520. if (!ironlake_compute_srwm(dev, 3, enabled,
  1521. SNB_READ_WM3_LATENCY() * 500,
  1522. &sandybridge_display_srwm_info,
  1523. &sandybridge_cursor_srwm_info,
  1524. &fbc_wm, &plane_wm, &cursor_wm))
  1525. return;
  1526. I915_WRITE(WM3_LP_ILK,
  1527. WM3_LP_EN |
  1528. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1529. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1530. (plane_wm << WM1_LP_SR_SHIFT) |
  1531. cursor_wm);
  1532. }
  1533. static bool
  1534. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  1535. uint32_t sprite_width, int pixel_size,
  1536. const struct intel_watermark_params *display,
  1537. int display_latency_ns, int *sprite_wm)
  1538. {
  1539. struct drm_crtc *crtc;
  1540. int clock;
  1541. int entries, tlb_miss;
  1542. crtc = intel_get_crtc_for_plane(dev, plane);
  1543. if (crtc->fb == NULL || !crtc->enabled) {
  1544. *sprite_wm = display->guard_size;
  1545. return false;
  1546. }
  1547. clock = crtc->mode.clock;
  1548. /* Use the small buffer method to calculate the sprite watermark */
  1549. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  1550. tlb_miss = display->fifo_size*display->cacheline_size -
  1551. sprite_width * 8;
  1552. if (tlb_miss > 0)
  1553. entries += tlb_miss;
  1554. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  1555. *sprite_wm = entries + display->guard_size;
  1556. if (*sprite_wm > (int)display->max_wm)
  1557. *sprite_wm = display->max_wm;
  1558. return true;
  1559. }
  1560. static bool
  1561. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  1562. uint32_t sprite_width, int pixel_size,
  1563. const struct intel_watermark_params *display,
  1564. int latency_ns, int *sprite_wm)
  1565. {
  1566. struct drm_crtc *crtc;
  1567. unsigned long line_time_us;
  1568. int clock;
  1569. int line_count, line_size;
  1570. int small, large;
  1571. int entries;
  1572. if (!latency_ns) {
  1573. *sprite_wm = 0;
  1574. return false;
  1575. }
  1576. crtc = intel_get_crtc_for_plane(dev, plane);
  1577. clock = crtc->mode.clock;
  1578. if (!clock) {
  1579. *sprite_wm = 0;
  1580. return false;
  1581. }
  1582. line_time_us = (sprite_width * 1000) / clock;
  1583. if (!line_time_us) {
  1584. *sprite_wm = 0;
  1585. return false;
  1586. }
  1587. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1588. line_size = sprite_width * pixel_size;
  1589. /* Use the minimum of the small and large buffer method for primary */
  1590. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1591. large = line_count * line_size;
  1592. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1593. *sprite_wm = entries + display->guard_size;
  1594. return *sprite_wm > 0x3ff ? false : true;
  1595. }
  1596. void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  1597. uint32_t sprite_width, int pixel_size)
  1598. {
  1599. struct drm_i915_private *dev_priv = dev->dev_private;
  1600. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1601. u32 val;
  1602. int sprite_wm, reg;
  1603. int ret;
  1604. switch (pipe) {
  1605. case 0:
  1606. reg = WM0_PIPEA_ILK;
  1607. break;
  1608. case 1:
  1609. reg = WM0_PIPEB_ILK;
  1610. break;
  1611. case 2:
  1612. reg = WM0_PIPEC_IVB;
  1613. break;
  1614. default:
  1615. return; /* bad pipe */
  1616. }
  1617. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  1618. &sandybridge_display_wm_info,
  1619. latency, &sprite_wm);
  1620. if (!ret) {
  1621. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
  1622. pipe);
  1623. return;
  1624. }
  1625. val = I915_READ(reg);
  1626. val &= ~WM0_PIPE_SPRITE_MASK;
  1627. I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  1628. DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
  1629. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1630. pixel_size,
  1631. &sandybridge_display_srwm_info,
  1632. SNB_READ_WM1_LATENCY() * 500,
  1633. &sprite_wm);
  1634. if (!ret) {
  1635. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
  1636. pipe);
  1637. return;
  1638. }
  1639. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  1640. /* Only IVB has two more LP watermarks for sprite */
  1641. if (!IS_IVYBRIDGE(dev))
  1642. return;
  1643. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1644. pixel_size,
  1645. &sandybridge_display_srwm_info,
  1646. SNB_READ_WM2_LATENCY() * 500,
  1647. &sprite_wm);
  1648. if (!ret) {
  1649. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
  1650. pipe);
  1651. return;
  1652. }
  1653. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  1654. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1655. pixel_size,
  1656. &sandybridge_display_srwm_info,
  1657. SNB_READ_WM3_LATENCY() * 500,
  1658. &sprite_wm);
  1659. if (!ret) {
  1660. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
  1661. pipe);
  1662. return;
  1663. }
  1664. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  1665. }
  1666. /**
  1667. * intel_update_watermarks - update FIFO watermark values based on current modes
  1668. *
  1669. * Calculate watermark values for the various WM regs based on current mode
  1670. * and plane configuration.
  1671. *
  1672. * There are several cases to deal with here:
  1673. * - normal (i.e. non-self-refresh)
  1674. * - self-refresh (SR) mode
  1675. * - lines are large relative to FIFO size (buffer can hold up to 2)
  1676. * - lines are small relative to FIFO size (buffer can hold more than 2
  1677. * lines), so need to account for TLB latency
  1678. *
  1679. * The normal calculation is:
  1680. * watermark = dotclock * bytes per pixel * latency
  1681. * where latency is platform & configuration dependent (we assume pessimal
  1682. * values here).
  1683. *
  1684. * The SR calculation is:
  1685. * watermark = (trunc(latency/line time)+1) * surface width *
  1686. * bytes per pixel
  1687. * where
  1688. * line time = htotal / dotclock
  1689. * surface width = hdisplay for normal plane and 64 for cursor
  1690. * and latency is assumed to be high, as above.
  1691. *
  1692. * The final value programmed to the register should always be rounded up,
  1693. * and include an extra 2 entries to account for clock crossings.
  1694. *
  1695. * We don't use the sprite, so we can ignore that. And on Crestline we have
  1696. * to set the non-SR watermarks to 8.
  1697. */
  1698. void intel_update_watermarks(struct drm_device *dev)
  1699. {
  1700. struct drm_i915_private *dev_priv = dev->dev_private;
  1701. if (dev_priv->display.update_wm)
  1702. dev_priv->display.update_wm(dev);
  1703. }
  1704. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  1705. uint32_t sprite_width, int pixel_size)
  1706. {
  1707. struct drm_i915_private *dev_priv = dev->dev_private;
  1708. if (dev_priv->display.update_sprite_wm)
  1709. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  1710. pixel_size);
  1711. }