intel_pm.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494
  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 <linux/cpufreq.h>
  28. #include "i915_drv.h"
  29. #include "intel_drv.h"
  30. /* FBC, or Frame Buffer Compression, is a technique employed to compress the
  31. * framebuffer contents in-memory, aiming at reducing the required bandwidth
  32. * during in-memory transfers and, therefore, reduce the power packet.
  33. *
  34. * The benefits of FBC are mostly visible with solid backgrounds and
  35. * variation-less patterns.
  36. *
  37. * FBC-related functionality can be enabled by the means of the
  38. * i915.i915_enable_fbc parameter
  39. */
  40. void i8xx_disable_fbc(struct drm_device *dev)
  41. {
  42. struct drm_i915_private *dev_priv = dev->dev_private;
  43. u32 fbc_ctl;
  44. /* Disable compression */
  45. fbc_ctl = I915_READ(FBC_CONTROL);
  46. if ((fbc_ctl & FBC_CTL_EN) == 0)
  47. return;
  48. fbc_ctl &= ~FBC_CTL_EN;
  49. I915_WRITE(FBC_CONTROL, fbc_ctl);
  50. /* Wait for compressing bit to clear */
  51. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  52. DRM_DEBUG_KMS("FBC idle timed out\n");
  53. return;
  54. }
  55. DRM_DEBUG_KMS("disabled FBC\n");
  56. }
  57. void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  58. {
  59. struct drm_device *dev = crtc->dev;
  60. struct drm_i915_private *dev_priv = dev->dev_private;
  61. struct drm_framebuffer *fb = crtc->fb;
  62. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  63. struct drm_i915_gem_object *obj = intel_fb->obj;
  64. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  65. int cfb_pitch;
  66. int plane, i;
  67. u32 fbc_ctl, fbc_ctl2;
  68. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  69. if (fb->pitches[0] < cfb_pitch)
  70. cfb_pitch = fb->pitches[0];
  71. /* FBC_CTL wants 64B units */
  72. cfb_pitch = (cfb_pitch / 64) - 1;
  73. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  74. /* Clear old tags */
  75. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  76. I915_WRITE(FBC_TAG + (i * 4), 0);
  77. /* Set it up... */
  78. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  79. fbc_ctl2 |= plane;
  80. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  81. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  82. /* enable it... */
  83. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  84. if (IS_I945GM(dev))
  85. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  86. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  87. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  88. fbc_ctl |= obj->fence_reg;
  89. I915_WRITE(FBC_CONTROL, fbc_ctl);
  90. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  91. cfb_pitch, crtc->y, intel_crtc->plane);
  92. }
  93. bool i8xx_fbc_enabled(struct drm_device *dev)
  94. {
  95. struct drm_i915_private *dev_priv = dev->dev_private;
  96. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  97. }
  98. void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  99. {
  100. struct drm_device *dev = crtc->dev;
  101. struct drm_i915_private *dev_priv = dev->dev_private;
  102. struct drm_framebuffer *fb = crtc->fb;
  103. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  104. struct drm_i915_gem_object *obj = intel_fb->obj;
  105. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  106. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  107. unsigned long stall_watermark = 200;
  108. u32 dpfc_ctl;
  109. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  110. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  111. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  112. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  113. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  114. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  115. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  116. /* enable it... */
  117. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  118. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  119. }
  120. void g4x_disable_fbc(struct drm_device *dev)
  121. {
  122. struct drm_i915_private *dev_priv = dev->dev_private;
  123. u32 dpfc_ctl;
  124. /* Disable compression */
  125. dpfc_ctl = I915_READ(DPFC_CONTROL);
  126. if (dpfc_ctl & DPFC_CTL_EN) {
  127. dpfc_ctl &= ~DPFC_CTL_EN;
  128. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  129. DRM_DEBUG_KMS("disabled FBC\n");
  130. }
  131. }
  132. bool g4x_fbc_enabled(struct drm_device *dev)
  133. {
  134. struct drm_i915_private *dev_priv = dev->dev_private;
  135. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  136. }
  137. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  138. {
  139. struct drm_i915_private *dev_priv = dev->dev_private;
  140. u32 blt_ecoskpd;
  141. /* Make sure blitter notifies FBC of writes */
  142. gen6_gt_force_wake_get(dev_priv);
  143. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  144. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  145. GEN6_BLITTER_LOCK_SHIFT;
  146. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  147. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  148. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  149. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  150. GEN6_BLITTER_LOCK_SHIFT);
  151. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  152. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  153. gen6_gt_force_wake_put(dev_priv);
  154. }
  155. void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  156. {
  157. struct drm_device *dev = crtc->dev;
  158. struct drm_i915_private *dev_priv = dev->dev_private;
  159. struct drm_framebuffer *fb = crtc->fb;
  160. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  161. struct drm_i915_gem_object *obj = intel_fb->obj;
  162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  163. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  164. unsigned long stall_watermark = 200;
  165. u32 dpfc_ctl;
  166. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  167. dpfc_ctl &= DPFC_RESERVED;
  168. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  169. /* Set persistent mode for front-buffer rendering, ala X. */
  170. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  171. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  172. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  173. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  174. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  175. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  176. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  177. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  178. /* enable it... */
  179. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  180. if (IS_GEN6(dev)) {
  181. I915_WRITE(SNB_DPFC_CTL_SA,
  182. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  183. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  184. sandybridge_blit_fbc_update(dev);
  185. }
  186. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  187. }
  188. void ironlake_disable_fbc(struct drm_device *dev)
  189. {
  190. struct drm_i915_private *dev_priv = dev->dev_private;
  191. u32 dpfc_ctl;
  192. /* Disable compression */
  193. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  194. if (dpfc_ctl & DPFC_CTL_EN) {
  195. dpfc_ctl &= ~DPFC_CTL_EN;
  196. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  197. DRM_DEBUG_KMS("disabled FBC\n");
  198. }
  199. }
  200. bool ironlake_fbc_enabled(struct drm_device *dev)
  201. {
  202. struct drm_i915_private *dev_priv = dev->dev_private;
  203. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  204. }
  205. bool intel_fbc_enabled(struct drm_device *dev)
  206. {
  207. struct drm_i915_private *dev_priv = dev->dev_private;
  208. if (!dev_priv->display.fbc_enabled)
  209. return false;
  210. return dev_priv->display.fbc_enabled(dev);
  211. }
  212. static void intel_fbc_work_fn(struct work_struct *__work)
  213. {
  214. struct intel_fbc_work *work =
  215. container_of(to_delayed_work(__work),
  216. struct intel_fbc_work, work);
  217. struct drm_device *dev = work->crtc->dev;
  218. struct drm_i915_private *dev_priv = dev->dev_private;
  219. mutex_lock(&dev->struct_mutex);
  220. if (work == dev_priv->fbc_work) {
  221. /* Double check that we haven't switched fb without cancelling
  222. * the prior work.
  223. */
  224. if (work->crtc->fb == work->fb) {
  225. dev_priv->display.enable_fbc(work->crtc,
  226. work->interval);
  227. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  228. dev_priv->cfb_fb = work->crtc->fb->base.id;
  229. dev_priv->cfb_y = work->crtc->y;
  230. }
  231. dev_priv->fbc_work = NULL;
  232. }
  233. mutex_unlock(&dev->struct_mutex);
  234. kfree(work);
  235. }
  236. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  237. {
  238. if (dev_priv->fbc_work == NULL)
  239. return;
  240. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  241. /* Synchronisation is provided by struct_mutex and checking of
  242. * dev_priv->fbc_work, so we can perform the cancellation
  243. * entirely asynchronously.
  244. */
  245. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  246. /* tasklet was killed before being run, clean up */
  247. kfree(dev_priv->fbc_work);
  248. /* Mark the work as no longer wanted so that if it does
  249. * wake-up (because the work was already running and waiting
  250. * for our mutex), it will discover that is no longer
  251. * necessary to run.
  252. */
  253. dev_priv->fbc_work = NULL;
  254. }
  255. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  256. {
  257. struct intel_fbc_work *work;
  258. struct drm_device *dev = crtc->dev;
  259. struct drm_i915_private *dev_priv = dev->dev_private;
  260. if (!dev_priv->display.enable_fbc)
  261. return;
  262. intel_cancel_fbc_work(dev_priv);
  263. work = kzalloc(sizeof *work, GFP_KERNEL);
  264. if (work == NULL) {
  265. dev_priv->display.enable_fbc(crtc, interval);
  266. return;
  267. }
  268. work->crtc = crtc;
  269. work->fb = crtc->fb;
  270. work->interval = interval;
  271. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  272. dev_priv->fbc_work = work;
  273. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  274. /* Delay the actual enabling to let pageflipping cease and the
  275. * display to settle before starting the compression. Note that
  276. * this delay also serves a second purpose: it allows for a
  277. * vblank to pass after disabling the FBC before we attempt
  278. * to modify the control registers.
  279. *
  280. * A more complicated solution would involve tracking vblanks
  281. * following the termination of the page-flipping sequence
  282. * and indeed performing the enable as a co-routine and not
  283. * waiting synchronously upon the vblank.
  284. */
  285. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  286. }
  287. void intel_disable_fbc(struct drm_device *dev)
  288. {
  289. struct drm_i915_private *dev_priv = dev->dev_private;
  290. intel_cancel_fbc_work(dev_priv);
  291. if (!dev_priv->display.disable_fbc)
  292. return;
  293. dev_priv->display.disable_fbc(dev);
  294. dev_priv->cfb_plane = -1;
  295. }
  296. /**
  297. * intel_update_fbc - enable/disable FBC as needed
  298. * @dev: the drm_device
  299. *
  300. * Set up the framebuffer compression hardware at mode set time. We
  301. * enable it if possible:
  302. * - plane A only (on pre-965)
  303. * - no pixel mulitply/line duplication
  304. * - no alpha buffer discard
  305. * - no dual wide
  306. * - framebuffer <= 2048 in width, 1536 in height
  307. *
  308. * We can't assume that any compression will take place (worst case),
  309. * so the compressed buffer has to be the same size as the uncompressed
  310. * one. It also must reside (along with the line length buffer) in
  311. * stolen memory.
  312. *
  313. * We need to enable/disable FBC on a global basis.
  314. */
  315. void intel_update_fbc(struct drm_device *dev)
  316. {
  317. struct drm_i915_private *dev_priv = dev->dev_private;
  318. struct drm_crtc *crtc = NULL, *tmp_crtc;
  319. struct intel_crtc *intel_crtc;
  320. struct drm_framebuffer *fb;
  321. struct intel_framebuffer *intel_fb;
  322. struct drm_i915_gem_object *obj;
  323. int enable_fbc;
  324. DRM_DEBUG_KMS("\n");
  325. if (!i915_powersave)
  326. return;
  327. if (!I915_HAS_FBC(dev))
  328. return;
  329. /*
  330. * If FBC is already on, we just have to verify that we can
  331. * keep it that way...
  332. * Need to disable if:
  333. * - more than one pipe is active
  334. * - changing FBC params (stride, fence, mode)
  335. * - new fb is too large to fit in compressed buffer
  336. * - going to an unsupported config (interlace, pixel multiply, etc.)
  337. */
  338. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  339. if (tmp_crtc->enabled && tmp_crtc->fb) {
  340. if (crtc) {
  341. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  342. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  343. goto out_disable;
  344. }
  345. crtc = tmp_crtc;
  346. }
  347. }
  348. if (!crtc || crtc->fb == NULL) {
  349. DRM_DEBUG_KMS("no output, disabling\n");
  350. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  351. goto out_disable;
  352. }
  353. intel_crtc = to_intel_crtc(crtc);
  354. fb = crtc->fb;
  355. intel_fb = to_intel_framebuffer(fb);
  356. obj = intel_fb->obj;
  357. enable_fbc = i915_enable_fbc;
  358. if (enable_fbc < 0) {
  359. DRM_DEBUG_KMS("fbc set to per-chip default\n");
  360. enable_fbc = 1;
  361. if (INTEL_INFO(dev)->gen <= 6)
  362. enable_fbc = 0;
  363. }
  364. if (!enable_fbc) {
  365. DRM_DEBUG_KMS("fbc disabled per module param\n");
  366. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  367. goto out_disable;
  368. }
  369. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  370. DRM_DEBUG_KMS("framebuffer too large, disabling "
  371. "compression\n");
  372. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  373. goto out_disable;
  374. }
  375. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  376. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  377. DRM_DEBUG_KMS("mode incompatible with compression, "
  378. "disabling\n");
  379. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  380. goto out_disable;
  381. }
  382. if ((crtc->mode.hdisplay > 2048) ||
  383. (crtc->mode.vdisplay > 1536)) {
  384. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  385. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  386. goto out_disable;
  387. }
  388. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  389. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  390. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  391. goto out_disable;
  392. }
  393. /* The use of a CPU fence is mandatory in order to detect writes
  394. * by the CPU to the scanout and trigger updates to the FBC.
  395. */
  396. if (obj->tiling_mode != I915_TILING_X ||
  397. obj->fence_reg == I915_FENCE_REG_NONE) {
  398. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  399. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  400. goto out_disable;
  401. }
  402. /* If the kernel debugger is active, always disable compression */
  403. if (in_dbg_master())
  404. goto out_disable;
  405. /* If the scanout has not changed, don't modify the FBC settings.
  406. * Note that we make the fundamental assumption that the fb->obj
  407. * cannot be unpinned (and have its GTT offset and fence revoked)
  408. * without first being decoupled from the scanout and FBC disabled.
  409. */
  410. if (dev_priv->cfb_plane == intel_crtc->plane &&
  411. dev_priv->cfb_fb == fb->base.id &&
  412. dev_priv->cfb_y == crtc->y)
  413. return;
  414. if (intel_fbc_enabled(dev)) {
  415. /* We update FBC along two paths, after changing fb/crtc
  416. * configuration (modeswitching) and after page-flipping
  417. * finishes. For the latter, we know that not only did
  418. * we disable the FBC at the start of the page-flip
  419. * sequence, but also more than one vblank has passed.
  420. *
  421. * For the former case of modeswitching, it is possible
  422. * to switch between two FBC valid configurations
  423. * instantaneously so we do need to disable the FBC
  424. * before we can modify its control registers. We also
  425. * have to wait for the next vblank for that to take
  426. * effect. However, since we delay enabling FBC we can
  427. * assume that a vblank has passed since disabling and
  428. * that we can safely alter the registers in the deferred
  429. * callback.
  430. *
  431. * In the scenario that we go from a valid to invalid
  432. * and then back to valid FBC configuration we have
  433. * no strict enforcement that a vblank occurred since
  434. * disabling the FBC. However, along all current pipe
  435. * disabling paths we do need to wait for a vblank at
  436. * some point. And we wait before enabling FBC anyway.
  437. */
  438. DRM_DEBUG_KMS("disabling active FBC for update\n");
  439. intel_disable_fbc(dev);
  440. }
  441. intel_enable_fbc(crtc, 500);
  442. return;
  443. out_disable:
  444. /* Multiple disables should be harmless */
  445. if (intel_fbc_enabled(dev)) {
  446. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  447. intel_disable_fbc(dev);
  448. }
  449. }
  450. static const struct cxsr_latency cxsr_latency_table[] = {
  451. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  452. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  453. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  454. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  455. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  456. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  457. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  458. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  459. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  460. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  461. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  462. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  463. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  464. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  465. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  466. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  467. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  468. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  469. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  470. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  471. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  472. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  473. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  474. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  475. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  476. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  477. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  478. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  479. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  480. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  481. };
  482. const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  483. int is_ddr3,
  484. int fsb,
  485. int mem)
  486. {
  487. const struct cxsr_latency *latency;
  488. int i;
  489. if (fsb == 0 || mem == 0)
  490. return NULL;
  491. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  492. latency = &cxsr_latency_table[i];
  493. if (is_desktop == latency->is_desktop &&
  494. is_ddr3 == latency->is_ddr3 &&
  495. fsb == latency->fsb_freq && mem == latency->mem_freq)
  496. return latency;
  497. }
  498. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  499. return NULL;
  500. }
  501. void pineview_disable_cxsr(struct drm_device *dev)
  502. {
  503. struct drm_i915_private *dev_priv = dev->dev_private;
  504. /* deactivate cxsr */
  505. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  506. }
  507. /*
  508. * Latency for FIFO fetches is dependent on several factors:
  509. * - memory configuration (speed, channels)
  510. * - chipset
  511. * - current MCH state
  512. * It can be fairly high in some situations, so here we assume a fairly
  513. * pessimal value. It's a tradeoff between extra memory fetches (if we
  514. * set this value too high, the FIFO will fetch frequently to stay full)
  515. * and power consumption (set it too low to save power and we might see
  516. * FIFO underruns and display "flicker").
  517. *
  518. * A value of 5us seems to be a good balance; safe for very low end
  519. * platforms but not overly aggressive on lower latency configs.
  520. */
  521. static const int latency_ns = 5000;
  522. int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  523. {
  524. struct drm_i915_private *dev_priv = dev->dev_private;
  525. uint32_t dsparb = I915_READ(DSPARB);
  526. int size;
  527. size = dsparb & 0x7f;
  528. if (plane)
  529. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  530. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  531. plane ? "B" : "A", size);
  532. return size;
  533. }
  534. int i85x_get_fifo_size(struct drm_device *dev, int plane)
  535. {
  536. struct drm_i915_private *dev_priv = dev->dev_private;
  537. uint32_t dsparb = I915_READ(DSPARB);
  538. int size;
  539. size = dsparb & 0x1ff;
  540. if (plane)
  541. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  542. size >>= 1; /* Convert to cachelines */
  543. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  544. plane ? "B" : "A", size);
  545. return size;
  546. }
  547. int i845_get_fifo_size(struct drm_device *dev, int plane)
  548. {
  549. struct drm_i915_private *dev_priv = dev->dev_private;
  550. uint32_t dsparb = I915_READ(DSPARB);
  551. int size;
  552. size = dsparb & 0x7f;
  553. size >>= 2; /* Convert to cachelines */
  554. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  555. plane ? "B" : "A",
  556. size);
  557. return size;
  558. }
  559. int i830_get_fifo_size(struct drm_device *dev, int plane)
  560. {
  561. struct drm_i915_private *dev_priv = dev->dev_private;
  562. uint32_t dsparb = I915_READ(DSPARB);
  563. int size;
  564. size = dsparb & 0x7f;
  565. size >>= 1; /* Convert to cachelines */
  566. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  567. plane ? "B" : "A", size);
  568. return size;
  569. }
  570. /* Pineview has different values for various configs */
  571. static const struct intel_watermark_params pineview_display_wm = {
  572. PINEVIEW_DISPLAY_FIFO,
  573. PINEVIEW_MAX_WM,
  574. PINEVIEW_DFT_WM,
  575. PINEVIEW_GUARD_WM,
  576. PINEVIEW_FIFO_LINE_SIZE
  577. };
  578. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  579. PINEVIEW_DISPLAY_FIFO,
  580. PINEVIEW_MAX_WM,
  581. PINEVIEW_DFT_HPLLOFF_WM,
  582. PINEVIEW_GUARD_WM,
  583. PINEVIEW_FIFO_LINE_SIZE
  584. };
  585. static const struct intel_watermark_params pineview_cursor_wm = {
  586. PINEVIEW_CURSOR_FIFO,
  587. PINEVIEW_CURSOR_MAX_WM,
  588. PINEVIEW_CURSOR_DFT_WM,
  589. PINEVIEW_CURSOR_GUARD_WM,
  590. PINEVIEW_FIFO_LINE_SIZE,
  591. };
  592. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  593. PINEVIEW_CURSOR_FIFO,
  594. PINEVIEW_CURSOR_MAX_WM,
  595. PINEVIEW_CURSOR_DFT_WM,
  596. PINEVIEW_CURSOR_GUARD_WM,
  597. PINEVIEW_FIFO_LINE_SIZE
  598. };
  599. static const struct intel_watermark_params g4x_wm_info = {
  600. G4X_FIFO_SIZE,
  601. G4X_MAX_WM,
  602. G4X_MAX_WM,
  603. 2,
  604. G4X_FIFO_LINE_SIZE,
  605. };
  606. static const struct intel_watermark_params g4x_cursor_wm_info = {
  607. I965_CURSOR_FIFO,
  608. I965_CURSOR_MAX_WM,
  609. I965_CURSOR_DFT_WM,
  610. 2,
  611. G4X_FIFO_LINE_SIZE,
  612. };
  613. static const struct intel_watermark_params valleyview_wm_info = {
  614. VALLEYVIEW_FIFO_SIZE,
  615. VALLEYVIEW_MAX_WM,
  616. VALLEYVIEW_MAX_WM,
  617. 2,
  618. G4X_FIFO_LINE_SIZE,
  619. };
  620. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  621. I965_CURSOR_FIFO,
  622. VALLEYVIEW_CURSOR_MAX_WM,
  623. I965_CURSOR_DFT_WM,
  624. 2,
  625. G4X_FIFO_LINE_SIZE,
  626. };
  627. static const struct intel_watermark_params i965_cursor_wm_info = {
  628. I965_CURSOR_FIFO,
  629. I965_CURSOR_MAX_WM,
  630. I965_CURSOR_DFT_WM,
  631. 2,
  632. I915_FIFO_LINE_SIZE,
  633. };
  634. static const struct intel_watermark_params i945_wm_info = {
  635. I945_FIFO_SIZE,
  636. I915_MAX_WM,
  637. 1,
  638. 2,
  639. I915_FIFO_LINE_SIZE
  640. };
  641. static const struct intel_watermark_params i915_wm_info = {
  642. I915_FIFO_SIZE,
  643. I915_MAX_WM,
  644. 1,
  645. 2,
  646. I915_FIFO_LINE_SIZE
  647. };
  648. static const struct intel_watermark_params i855_wm_info = {
  649. I855GM_FIFO_SIZE,
  650. I915_MAX_WM,
  651. 1,
  652. 2,
  653. I830_FIFO_LINE_SIZE
  654. };
  655. static const struct intel_watermark_params i830_wm_info = {
  656. I830_FIFO_SIZE,
  657. I915_MAX_WM,
  658. 1,
  659. 2,
  660. I830_FIFO_LINE_SIZE
  661. };
  662. static const struct intel_watermark_params ironlake_display_wm_info = {
  663. ILK_DISPLAY_FIFO,
  664. ILK_DISPLAY_MAXWM,
  665. ILK_DISPLAY_DFTWM,
  666. 2,
  667. ILK_FIFO_LINE_SIZE
  668. };
  669. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  670. ILK_CURSOR_FIFO,
  671. ILK_CURSOR_MAXWM,
  672. ILK_CURSOR_DFTWM,
  673. 2,
  674. ILK_FIFO_LINE_SIZE
  675. };
  676. static const struct intel_watermark_params ironlake_display_srwm_info = {
  677. ILK_DISPLAY_SR_FIFO,
  678. ILK_DISPLAY_MAX_SRWM,
  679. ILK_DISPLAY_DFT_SRWM,
  680. 2,
  681. ILK_FIFO_LINE_SIZE
  682. };
  683. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  684. ILK_CURSOR_SR_FIFO,
  685. ILK_CURSOR_MAX_SRWM,
  686. ILK_CURSOR_DFT_SRWM,
  687. 2,
  688. ILK_FIFO_LINE_SIZE
  689. };
  690. static const struct intel_watermark_params sandybridge_display_wm_info = {
  691. SNB_DISPLAY_FIFO,
  692. SNB_DISPLAY_MAXWM,
  693. SNB_DISPLAY_DFTWM,
  694. 2,
  695. SNB_FIFO_LINE_SIZE
  696. };
  697. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  698. SNB_CURSOR_FIFO,
  699. SNB_CURSOR_MAXWM,
  700. SNB_CURSOR_DFTWM,
  701. 2,
  702. SNB_FIFO_LINE_SIZE
  703. };
  704. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  705. SNB_DISPLAY_SR_FIFO,
  706. SNB_DISPLAY_MAX_SRWM,
  707. SNB_DISPLAY_DFT_SRWM,
  708. 2,
  709. SNB_FIFO_LINE_SIZE
  710. };
  711. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  712. SNB_CURSOR_SR_FIFO,
  713. SNB_CURSOR_MAX_SRWM,
  714. SNB_CURSOR_DFT_SRWM,
  715. 2,
  716. SNB_FIFO_LINE_SIZE
  717. };
  718. /**
  719. * intel_calculate_wm - calculate watermark level
  720. * @clock_in_khz: pixel clock
  721. * @wm: chip FIFO params
  722. * @pixel_size: display pixel size
  723. * @latency_ns: memory latency for the platform
  724. *
  725. * Calculate the watermark level (the level at which the display plane will
  726. * start fetching from memory again). Each chip has a different display
  727. * FIFO size and allocation, so the caller needs to figure that out and pass
  728. * in the correct intel_watermark_params structure.
  729. *
  730. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  731. * on the pixel size. When it reaches the watermark level, it'll start
  732. * fetching FIFO line sized based chunks from memory until the FIFO fills
  733. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  734. * will occur, and a display engine hang could result.
  735. */
  736. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  737. const struct intel_watermark_params *wm,
  738. int fifo_size,
  739. int pixel_size,
  740. unsigned long latency_ns)
  741. {
  742. long entries_required, wm_size;
  743. /*
  744. * Note: we need to make sure we don't overflow for various clock &
  745. * latency values.
  746. * clocks go from a few thousand to several hundred thousand.
  747. * latency is usually a few thousand
  748. */
  749. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  750. 1000;
  751. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  752. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  753. wm_size = fifo_size - (entries_required + wm->guard_size);
  754. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  755. /* Don't promote wm_size to unsigned... */
  756. if (wm_size > (long)wm->max_wm)
  757. wm_size = wm->max_wm;
  758. if (wm_size <= 0)
  759. wm_size = wm->default_wm;
  760. return wm_size;
  761. }
  762. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  763. {
  764. struct drm_crtc *crtc, *enabled = NULL;
  765. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  766. if (crtc->enabled && crtc->fb) {
  767. if (enabled)
  768. return NULL;
  769. enabled = crtc;
  770. }
  771. }
  772. return enabled;
  773. }
  774. void pineview_update_wm(struct drm_device *dev)
  775. {
  776. struct drm_i915_private *dev_priv = dev->dev_private;
  777. struct drm_crtc *crtc;
  778. const struct cxsr_latency *latency;
  779. u32 reg;
  780. unsigned long wm;
  781. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  782. dev_priv->fsb_freq, dev_priv->mem_freq);
  783. if (!latency) {
  784. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  785. pineview_disable_cxsr(dev);
  786. return;
  787. }
  788. crtc = single_enabled_crtc(dev);
  789. if (crtc) {
  790. int clock = crtc->mode.clock;
  791. int pixel_size = crtc->fb->bits_per_pixel / 8;
  792. /* Display SR */
  793. wm = intel_calculate_wm(clock, &pineview_display_wm,
  794. pineview_display_wm.fifo_size,
  795. pixel_size, latency->display_sr);
  796. reg = I915_READ(DSPFW1);
  797. reg &= ~DSPFW_SR_MASK;
  798. reg |= wm << DSPFW_SR_SHIFT;
  799. I915_WRITE(DSPFW1, reg);
  800. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  801. /* cursor SR */
  802. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  803. pineview_display_wm.fifo_size,
  804. pixel_size, latency->cursor_sr);
  805. reg = I915_READ(DSPFW3);
  806. reg &= ~DSPFW_CURSOR_SR_MASK;
  807. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  808. I915_WRITE(DSPFW3, reg);
  809. /* Display HPLL off SR */
  810. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  811. pineview_display_hplloff_wm.fifo_size,
  812. pixel_size, latency->display_hpll_disable);
  813. reg = I915_READ(DSPFW3);
  814. reg &= ~DSPFW_HPLL_SR_MASK;
  815. reg |= wm & DSPFW_HPLL_SR_MASK;
  816. I915_WRITE(DSPFW3, reg);
  817. /* cursor HPLL off SR */
  818. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  819. pineview_display_hplloff_wm.fifo_size,
  820. pixel_size, latency->cursor_hpll_disable);
  821. reg = I915_READ(DSPFW3);
  822. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  823. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  824. I915_WRITE(DSPFW3, reg);
  825. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  826. /* activate cxsr */
  827. I915_WRITE(DSPFW3,
  828. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  829. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  830. } else {
  831. pineview_disable_cxsr(dev);
  832. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  833. }
  834. }
  835. static bool g4x_compute_wm0(struct drm_device *dev,
  836. int plane,
  837. const struct intel_watermark_params *display,
  838. int display_latency_ns,
  839. const struct intel_watermark_params *cursor,
  840. int cursor_latency_ns,
  841. int *plane_wm,
  842. int *cursor_wm)
  843. {
  844. struct drm_crtc *crtc;
  845. int htotal, hdisplay, clock, pixel_size;
  846. int line_time_us, line_count;
  847. int entries, tlb_miss;
  848. crtc = intel_get_crtc_for_plane(dev, plane);
  849. if (crtc->fb == NULL || !crtc->enabled) {
  850. *cursor_wm = cursor->guard_size;
  851. *plane_wm = display->guard_size;
  852. return false;
  853. }
  854. htotal = crtc->mode.htotal;
  855. hdisplay = crtc->mode.hdisplay;
  856. clock = crtc->mode.clock;
  857. pixel_size = crtc->fb->bits_per_pixel / 8;
  858. /* Use the small buffer method to calculate plane watermark */
  859. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  860. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  861. if (tlb_miss > 0)
  862. entries += tlb_miss;
  863. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  864. *plane_wm = entries + display->guard_size;
  865. if (*plane_wm > (int)display->max_wm)
  866. *plane_wm = display->max_wm;
  867. /* Use the large buffer method to calculate cursor watermark */
  868. line_time_us = ((htotal * 1000) / clock);
  869. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  870. entries = line_count * 64 * pixel_size;
  871. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  872. if (tlb_miss > 0)
  873. entries += tlb_miss;
  874. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  875. *cursor_wm = entries + cursor->guard_size;
  876. if (*cursor_wm > (int)cursor->max_wm)
  877. *cursor_wm = (int)cursor->max_wm;
  878. return true;
  879. }
  880. /*
  881. * Check the wm result.
  882. *
  883. * If any calculated watermark values is larger than the maximum value that
  884. * can be programmed into the associated watermark register, that watermark
  885. * must be disabled.
  886. */
  887. static bool g4x_check_srwm(struct drm_device *dev,
  888. int display_wm, int cursor_wm,
  889. const struct intel_watermark_params *display,
  890. const struct intel_watermark_params *cursor)
  891. {
  892. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  893. display_wm, cursor_wm);
  894. if (display_wm > display->max_wm) {
  895. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  896. display_wm, display->max_wm);
  897. return false;
  898. }
  899. if (cursor_wm > cursor->max_wm) {
  900. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  901. cursor_wm, cursor->max_wm);
  902. return false;
  903. }
  904. if (!(display_wm || cursor_wm)) {
  905. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  906. return false;
  907. }
  908. return true;
  909. }
  910. static bool g4x_compute_srwm(struct drm_device *dev,
  911. int plane,
  912. int latency_ns,
  913. const struct intel_watermark_params *display,
  914. const struct intel_watermark_params *cursor,
  915. int *display_wm, int *cursor_wm)
  916. {
  917. struct drm_crtc *crtc;
  918. int hdisplay, htotal, pixel_size, clock;
  919. unsigned long line_time_us;
  920. int line_count, line_size;
  921. int small, large;
  922. int entries;
  923. if (!latency_ns) {
  924. *display_wm = *cursor_wm = 0;
  925. return false;
  926. }
  927. crtc = intel_get_crtc_for_plane(dev, plane);
  928. hdisplay = crtc->mode.hdisplay;
  929. htotal = crtc->mode.htotal;
  930. clock = crtc->mode.clock;
  931. pixel_size = crtc->fb->bits_per_pixel / 8;
  932. line_time_us = (htotal * 1000) / clock;
  933. line_count = (latency_ns / line_time_us + 1000) / 1000;
  934. line_size = hdisplay * pixel_size;
  935. /* Use the minimum of the small and large buffer method for primary */
  936. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  937. large = line_count * line_size;
  938. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  939. *display_wm = entries + display->guard_size;
  940. /* calculate the self-refresh watermark for display cursor */
  941. entries = line_count * pixel_size * 64;
  942. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  943. *cursor_wm = entries + cursor->guard_size;
  944. return g4x_check_srwm(dev,
  945. *display_wm, *cursor_wm,
  946. display, cursor);
  947. }
  948. static bool vlv_compute_drain_latency(struct drm_device *dev,
  949. int plane,
  950. int *plane_prec_mult,
  951. int *plane_dl,
  952. int *cursor_prec_mult,
  953. int *cursor_dl)
  954. {
  955. struct drm_crtc *crtc;
  956. int clock, pixel_size;
  957. int entries;
  958. crtc = intel_get_crtc_for_plane(dev, plane);
  959. if (crtc->fb == NULL || !crtc->enabled)
  960. return false;
  961. clock = crtc->mode.clock; /* VESA DOT Clock */
  962. pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
  963. entries = (clock / 1000) * pixel_size;
  964. *plane_prec_mult = (entries > 256) ?
  965. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  966. *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
  967. pixel_size);
  968. entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
  969. *cursor_prec_mult = (entries > 256) ?
  970. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  971. *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
  972. return true;
  973. }
  974. /*
  975. * Update drain latency registers of memory arbiter
  976. *
  977. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  978. * to be programmed. Each plane has a drain latency multiplier and a drain
  979. * latency value.
  980. */
  981. static void vlv_update_drain_latency(struct drm_device *dev)
  982. {
  983. struct drm_i915_private *dev_priv = dev->dev_private;
  984. int planea_prec, planea_dl, planeb_prec, planeb_dl;
  985. int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
  986. int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
  987. either 16 or 32 */
  988. /* For plane A, Cursor A */
  989. if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
  990. &cursor_prec_mult, &cursora_dl)) {
  991. cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  992. DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
  993. planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  994. DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
  995. I915_WRITE(VLV_DDL1, cursora_prec |
  996. (cursora_dl << DDL_CURSORA_SHIFT) |
  997. planea_prec | planea_dl);
  998. }
  999. /* For plane B, Cursor B */
  1000. if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
  1001. &cursor_prec_mult, &cursorb_dl)) {
  1002. cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1003. DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
  1004. planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1005. DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
  1006. I915_WRITE(VLV_DDL2, cursorb_prec |
  1007. (cursorb_dl << DDL_CURSORB_SHIFT) |
  1008. planeb_prec | planeb_dl);
  1009. }
  1010. }
  1011. #define single_plane_enabled(mask) is_power_of_2(mask)
  1012. void valleyview_update_wm(struct drm_device *dev)
  1013. {
  1014. static const int sr_latency_ns = 12000;
  1015. struct drm_i915_private *dev_priv = dev->dev_private;
  1016. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1017. int plane_sr, cursor_sr;
  1018. unsigned int enabled = 0;
  1019. vlv_update_drain_latency(dev);
  1020. if (g4x_compute_wm0(dev, 0,
  1021. &valleyview_wm_info, latency_ns,
  1022. &valleyview_cursor_wm_info, latency_ns,
  1023. &planea_wm, &cursora_wm))
  1024. enabled |= 1;
  1025. if (g4x_compute_wm0(dev, 1,
  1026. &valleyview_wm_info, latency_ns,
  1027. &valleyview_cursor_wm_info, latency_ns,
  1028. &planeb_wm, &cursorb_wm))
  1029. enabled |= 2;
  1030. plane_sr = cursor_sr = 0;
  1031. if (single_plane_enabled(enabled) &&
  1032. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1033. sr_latency_ns,
  1034. &valleyview_wm_info,
  1035. &valleyview_cursor_wm_info,
  1036. &plane_sr, &cursor_sr))
  1037. I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
  1038. else
  1039. I915_WRITE(FW_BLC_SELF_VLV,
  1040. I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
  1041. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1042. planea_wm, cursora_wm,
  1043. planeb_wm, cursorb_wm,
  1044. plane_sr, cursor_sr);
  1045. I915_WRITE(DSPFW1,
  1046. (plane_sr << DSPFW_SR_SHIFT) |
  1047. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1048. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1049. planea_wm);
  1050. I915_WRITE(DSPFW2,
  1051. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  1052. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1053. I915_WRITE(DSPFW3,
  1054. (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
  1055. }
  1056. void g4x_update_wm(struct drm_device *dev)
  1057. {
  1058. static const int sr_latency_ns = 12000;
  1059. struct drm_i915_private *dev_priv = dev->dev_private;
  1060. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1061. int plane_sr, cursor_sr;
  1062. unsigned int enabled = 0;
  1063. if (g4x_compute_wm0(dev, 0,
  1064. &g4x_wm_info, latency_ns,
  1065. &g4x_cursor_wm_info, latency_ns,
  1066. &planea_wm, &cursora_wm))
  1067. enabled |= 1;
  1068. if (g4x_compute_wm0(dev, 1,
  1069. &g4x_wm_info, latency_ns,
  1070. &g4x_cursor_wm_info, latency_ns,
  1071. &planeb_wm, &cursorb_wm))
  1072. enabled |= 2;
  1073. plane_sr = cursor_sr = 0;
  1074. if (single_plane_enabled(enabled) &&
  1075. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1076. sr_latency_ns,
  1077. &g4x_wm_info,
  1078. &g4x_cursor_wm_info,
  1079. &plane_sr, &cursor_sr))
  1080. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1081. else
  1082. I915_WRITE(FW_BLC_SELF,
  1083. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  1084. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1085. planea_wm, cursora_wm,
  1086. planeb_wm, cursorb_wm,
  1087. plane_sr, cursor_sr);
  1088. I915_WRITE(DSPFW1,
  1089. (plane_sr << DSPFW_SR_SHIFT) |
  1090. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1091. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1092. planea_wm);
  1093. I915_WRITE(DSPFW2,
  1094. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  1095. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1096. /* HPLL off in SR has some issues on G4x... disable it */
  1097. I915_WRITE(DSPFW3,
  1098. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  1099. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1100. }
  1101. void i965_update_wm(struct drm_device *dev)
  1102. {
  1103. struct drm_i915_private *dev_priv = dev->dev_private;
  1104. struct drm_crtc *crtc;
  1105. int srwm = 1;
  1106. int cursor_sr = 16;
  1107. /* Calc sr entries for one plane configs */
  1108. crtc = single_enabled_crtc(dev);
  1109. if (crtc) {
  1110. /* self-refresh has much higher latency */
  1111. static const int sr_latency_ns = 12000;
  1112. int clock = crtc->mode.clock;
  1113. int htotal = crtc->mode.htotal;
  1114. int hdisplay = crtc->mode.hdisplay;
  1115. int pixel_size = crtc->fb->bits_per_pixel / 8;
  1116. unsigned long line_time_us;
  1117. int entries;
  1118. line_time_us = ((htotal * 1000) / clock);
  1119. /* Use ns/us then divide to preserve precision */
  1120. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1121. pixel_size * hdisplay;
  1122. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1123. srwm = I965_FIFO_SIZE - entries;
  1124. if (srwm < 0)
  1125. srwm = 1;
  1126. srwm &= 0x1ff;
  1127. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1128. entries, srwm);
  1129. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1130. pixel_size * 64;
  1131. entries = DIV_ROUND_UP(entries,
  1132. i965_cursor_wm_info.cacheline_size);
  1133. cursor_sr = i965_cursor_wm_info.fifo_size -
  1134. (entries + i965_cursor_wm_info.guard_size);
  1135. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1136. cursor_sr = i965_cursor_wm_info.max_wm;
  1137. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1138. "cursor %d\n", srwm, cursor_sr);
  1139. if (IS_CRESTLINE(dev))
  1140. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1141. } else {
  1142. /* Turn off self refresh if both pipes are enabled */
  1143. if (IS_CRESTLINE(dev))
  1144. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  1145. & ~FW_BLC_SELF_EN);
  1146. }
  1147. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1148. srwm);
  1149. /* 965 has limitations... */
  1150. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  1151. (8 << 16) | (8 << 8) | (8 << 0));
  1152. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  1153. /* update cursor SR watermark */
  1154. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1155. }
  1156. void i9xx_update_wm(struct drm_device *dev)
  1157. {
  1158. struct drm_i915_private *dev_priv = dev->dev_private;
  1159. const struct intel_watermark_params *wm_info;
  1160. uint32_t fwater_lo;
  1161. uint32_t fwater_hi;
  1162. int cwm, srwm = 1;
  1163. int fifo_size;
  1164. int planea_wm, planeb_wm;
  1165. struct drm_crtc *crtc, *enabled = NULL;
  1166. if (IS_I945GM(dev))
  1167. wm_info = &i945_wm_info;
  1168. else if (!IS_GEN2(dev))
  1169. wm_info = &i915_wm_info;
  1170. else
  1171. wm_info = &i855_wm_info;
  1172. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1173. crtc = intel_get_crtc_for_plane(dev, 0);
  1174. if (crtc->enabled && crtc->fb) {
  1175. planea_wm = intel_calculate_wm(crtc->mode.clock,
  1176. wm_info, fifo_size,
  1177. crtc->fb->bits_per_pixel / 8,
  1178. latency_ns);
  1179. enabled = crtc;
  1180. } else
  1181. planea_wm = fifo_size - wm_info->guard_size;
  1182. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1183. crtc = intel_get_crtc_for_plane(dev, 1);
  1184. if (crtc->enabled && crtc->fb) {
  1185. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  1186. wm_info, fifo_size,
  1187. crtc->fb->bits_per_pixel / 8,
  1188. latency_ns);
  1189. if (enabled == NULL)
  1190. enabled = crtc;
  1191. else
  1192. enabled = NULL;
  1193. } else
  1194. planeb_wm = fifo_size - wm_info->guard_size;
  1195. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1196. /*
  1197. * Overlay gets an aggressive default since video jitter is bad.
  1198. */
  1199. cwm = 2;
  1200. /* Play safe and disable self-refresh before adjusting watermarks. */
  1201. if (IS_I945G(dev) || IS_I945GM(dev))
  1202. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  1203. else if (IS_I915GM(dev))
  1204. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  1205. /* Calc sr entries for one plane configs */
  1206. if (HAS_FW_BLC(dev) && enabled) {
  1207. /* self-refresh has much higher latency */
  1208. static const int sr_latency_ns = 6000;
  1209. int clock = enabled->mode.clock;
  1210. int htotal = enabled->mode.htotal;
  1211. int hdisplay = enabled->mode.hdisplay;
  1212. int pixel_size = enabled->fb->bits_per_pixel / 8;
  1213. unsigned long line_time_us;
  1214. int entries;
  1215. line_time_us = (htotal * 1000) / clock;
  1216. /* Use ns/us then divide to preserve precision */
  1217. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1218. pixel_size * hdisplay;
  1219. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1220. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1221. srwm = wm_info->fifo_size - entries;
  1222. if (srwm < 0)
  1223. srwm = 1;
  1224. if (IS_I945G(dev) || IS_I945GM(dev))
  1225. I915_WRITE(FW_BLC_SELF,
  1226. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1227. else if (IS_I915GM(dev))
  1228. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1229. }
  1230. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1231. planea_wm, planeb_wm, cwm, srwm);
  1232. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1233. fwater_hi = (cwm & 0x1f);
  1234. /* Set request length to 8 cachelines per fetch */
  1235. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1236. fwater_hi = fwater_hi | (1 << 8);
  1237. I915_WRITE(FW_BLC, fwater_lo);
  1238. I915_WRITE(FW_BLC2, fwater_hi);
  1239. if (HAS_FW_BLC(dev)) {
  1240. if (enabled) {
  1241. if (IS_I945G(dev) || IS_I945GM(dev))
  1242. I915_WRITE(FW_BLC_SELF,
  1243. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  1244. else if (IS_I915GM(dev))
  1245. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  1246. DRM_DEBUG_KMS("memory self refresh enabled\n");
  1247. } else
  1248. DRM_DEBUG_KMS("memory self refresh disabled\n");
  1249. }
  1250. }
  1251. void i830_update_wm(struct drm_device *dev)
  1252. {
  1253. struct drm_i915_private *dev_priv = dev->dev_private;
  1254. struct drm_crtc *crtc;
  1255. uint32_t fwater_lo;
  1256. int planea_wm;
  1257. crtc = single_enabled_crtc(dev);
  1258. if (crtc == NULL)
  1259. return;
  1260. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  1261. dev_priv->display.get_fifo_size(dev, 0),
  1262. crtc->fb->bits_per_pixel / 8,
  1263. latency_ns);
  1264. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1265. fwater_lo |= (3<<8) | planea_wm;
  1266. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1267. I915_WRITE(FW_BLC, fwater_lo);
  1268. }
  1269. #define ILK_LP0_PLANE_LATENCY 700
  1270. #define ILK_LP0_CURSOR_LATENCY 1300
  1271. /*
  1272. * Check the wm result.
  1273. *
  1274. * If any calculated watermark values is larger than the maximum value that
  1275. * can be programmed into the associated watermark register, that watermark
  1276. * must be disabled.
  1277. */
  1278. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  1279. int fbc_wm, int display_wm, int cursor_wm,
  1280. const struct intel_watermark_params *display,
  1281. const struct intel_watermark_params *cursor)
  1282. {
  1283. struct drm_i915_private *dev_priv = dev->dev_private;
  1284. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  1285. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  1286. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  1287. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  1288. fbc_wm, SNB_FBC_MAX_SRWM, level);
  1289. /* fbc has it's own way to disable FBC WM */
  1290. I915_WRITE(DISP_ARB_CTL,
  1291. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  1292. return false;
  1293. }
  1294. if (display_wm > display->max_wm) {
  1295. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  1296. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  1297. return false;
  1298. }
  1299. if (cursor_wm > cursor->max_wm) {
  1300. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  1301. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  1302. return false;
  1303. }
  1304. if (!(fbc_wm || display_wm || cursor_wm)) {
  1305. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  1306. return false;
  1307. }
  1308. return true;
  1309. }
  1310. /*
  1311. * Compute watermark values of WM[1-3],
  1312. */
  1313. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  1314. int latency_ns,
  1315. const struct intel_watermark_params *display,
  1316. const struct intel_watermark_params *cursor,
  1317. int *fbc_wm, int *display_wm, int *cursor_wm)
  1318. {
  1319. struct drm_crtc *crtc;
  1320. unsigned long line_time_us;
  1321. int hdisplay, htotal, pixel_size, clock;
  1322. int line_count, line_size;
  1323. int small, large;
  1324. int entries;
  1325. if (!latency_ns) {
  1326. *fbc_wm = *display_wm = *cursor_wm = 0;
  1327. return false;
  1328. }
  1329. crtc = intel_get_crtc_for_plane(dev, plane);
  1330. hdisplay = crtc->mode.hdisplay;
  1331. htotal = crtc->mode.htotal;
  1332. clock = crtc->mode.clock;
  1333. pixel_size = crtc->fb->bits_per_pixel / 8;
  1334. line_time_us = (htotal * 1000) / clock;
  1335. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1336. line_size = hdisplay * pixel_size;
  1337. /* Use the minimum of the small and large buffer method for primary */
  1338. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1339. large = line_count * line_size;
  1340. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1341. *display_wm = entries + display->guard_size;
  1342. /*
  1343. * Spec says:
  1344. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  1345. */
  1346. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  1347. /* calculate the self-refresh watermark for display cursor */
  1348. entries = line_count * pixel_size * 64;
  1349. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1350. *cursor_wm = entries + cursor->guard_size;
  1351. return ironlake_check_srwm(dev, level,
  1352. *fbc_wm, *display_wm, *cursor_wm,
  1353. display, cursor);
  1354. }
  1355. void ironlake_update_wm(struct drm_device *dev)
  1356. {
  1357. struct drm_i915_private *dev_priv = dev->dev_private;
  1358. int fbc_wm, plane_wm, cursor_wm;
  1359. unsigned int enabled;
  1360. enabled = 0;
  1361. if (g4x_compute_wm0(dev, 0,
  1362. &ironlake_display_wm_info,
  1363. ILK_LP0_PLANE_LATENCY,
  1364. &ironlake_cursor_wm_info,
  1365. ILK_LP0_CURSOR_LATENCY,
  1366. &plane_wm, &cursor_wm)) {
  1367. I915_WRITE(WM0_PIPEA_ILK,
  1368. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1369. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1370. " plane %d, " "cursor: %d\n",
  1371. plane_wm, cursor_wm);
  1372. enabled |= 1;
  1373. }
  1374. if (g4x_compute_wm0(dev, 1,
  1375. &ironlake_display_wm_info,
  1376. ILK_LP0_PLANE_LATENCY,
  1377. &ironlake_cursor_wm_info,
  1378. ILK_LP0_CURSOR_LATENCY,
  1379. &plane_wm, &cursor_wm)) {
  1380. I915_WRITE(WM0_PIPEB_ILK,
  1381. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1382. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1383. " plane %d, cursor: %d\n",
  1384. plane_wm, cursor_wm);
  1385. enabled |= 2;
  1386. }
  1387. /*
  1388. * Calculate and update the self-refresh watermark only when one
  1389. * display plane is used.
  1390. */
  1391. I915_WRITE(WM3_LP_ILK, 0);
  1392. I915_WRITE(WM2_LP_ILK, 0);
  1393. I915_WRITE(WM1_LP_ILK, 0);
  1394. if (!single_plane_enabled(enabled))
  1395. return;
  1396. enabled = ffs(enabled) - 1;
  1397. /* WM1 */
  1398. if (!ironlake_compute_srwm(dev, 1, enabled,
  1399. ILK_READ_WM1_LATENCY() * 500,
  1400. &ironlake_display_srwm_info,
  1401. &ironlake_cursor_srwm_info,
  1402. &fbc_wm, &plane_wm, &cursor_wm))
  1403. return;
  1404. I915_WRITE(WM1_LP_ILK,
  1405. WM1_LP_SR_EN |
  1406. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1407. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1408. (plane_wm << WM1_LP_SR_SHIFT) |
  1409. cursor_wm);
  1410. /* WM2 */
  1411. if (!ironlake_compute_srwm(dev, 2, enabled,
  1412. ILK_READ_WM2_LATENCY() * 500,
  1413. &ironlake_display_srwm_info,
  1414. &ironlake_cursor_srwm_info,
  1415. &fbc_wm, &plane_wm, &cursor_wm))
  1416. return;
  1417. I915_WRITE(WM2_LP_ILK,
  1418. WM2_LP_EN |
  1419. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1420. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1421. (plane_wm << WM1_LP_SR_SHIFT) |
  1422. cursor_wm);
  1423. /*
  1424. * WM3 is unsupported on ILK, probably because we don't have latency
  1425. * data for that power state
  1426. */
  1427. }
  1428. void sandybridge_update_wm(struct drm_device *dev)
  1429. {
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1432. u32 val;
  1433. int fbc_wm, plane_wm, cursor_wm;
  1434. unsigned int enabled;
  1435. enabled = 0;
  1436. if (g4x_compute_wm0(dev, 0,
  1437. &sandybridge_display_wm_info, latency,
  1438. &sandybridge_cursor_wm_info, latency,
  1439. &plane_wm, &cursor_wm)) {
  1440. val = I915_READ(WM0_PIPEA_ILK);
  1441. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1442. I915_WRITE(WM0_PIPEA_ILK, val |
  1443. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1444. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1445. " plane %d, " "cursor: %d\n",
  1446. plane_wm, cursor_wm);
  1447. enabled |= 1;
  1448. }
  1449. if (g4x_compute_wm0(dev, 1,
  1450. &sandybridge_display_wm_info, latency,
  1451. &sandybridge_cursor_wm_info, latency,
  1452. &plane_wm, &cursor_wm)) {
  1453. val = I915_READ(WM0_PIPEB_ILK);
  1454. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1455. I915_WRITE(WM0_PIPEB_ILK, val |
  1456. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1457. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1458. " plane %d, cursor: %d\n",
  1459. plane_wm, cursor_wm);
  1460. enabled |= 2;
  1461. }
  1462. /* IVB has 3 pipes */
  1463. if (IS_IVYBRIDGE(dev) &&
  1464. g4x_compute_wm0(dev, 2,
  1465. &sandybridge_display_wm_info, latency,
  1466. &sandybridge_cursor_wm_info, latency,
  1467. &plane_wm, &cursor_wm)) {
  1468. val = I915_READ(WM0_PIPEC_IVB);
  1469. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1470. I915_WRITE(WM0_PIPEC_IVB, val |
  1471. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1472. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  1473. " plane %d, cursor: %d\n",
  1474. plane_wm, cursor_wm);
  1475. enabled |= 3;
  1476. }
  1477. /*
  1478. * Calculate and update the self-refresh watermark only when one
  1479. * display plane is used.
  1480. *
  1481. * SNB support 3 levels of watermark.
  1482. *
  1483. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1484. * and disabled in the descending order
  1485. *
  1486. */
  1487. I915_WRITE(WM3_LP_ILK, 0);
  1488. I915_WRITE(WM2_LP_ILK, 0);
  1489. I915_WRITE(WM1_LP_ILK, 0);
  1490. if (!single_plane_enabled(enabled) ||
  1491. dev_priv->sprite_scaling_enabled)
  1492. return;
  1493. enabled = ffs(enabled) - 1;
  1494. /* WM1 */
  1495. if (!ironlake_compute_srwm(dev, 1, enabled,
  1496. SNB_READ_WM1_LATENCY() * 500,
  1497. &sandybridge_display_srwm_info,
  1498. &sandybridge_cursor_srwm_info,
  1499. &fbc_wm, &plane_wm, &cursor_wm))
  1500. return;
  1501. I915_WRITE(WM1_LP_ILK,
  1502. WM1_LP_SR_EN |
  1503. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1504. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1505. (plane_wm << WM1_LP_SR_SHIFT) |
  1506. cursor_wm);
  1507. /* WM2 */
  1508. if (!ironlake_compute_srwm(dev, 2, enabled,
  1509. SNB_READ_WM2_LATENCY() * 500,
  1510. &sandybridge_display_srwm_info,
  1511. &sandybridge_cursor_srwm_info,
  1512. &fbc_wm, &plane_wm, &cursor_wm))
  1513. return;
  1514. I915_WRITE(WM2_LP_ILK,
  1515. WM2_LP_EN |
  1516. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1517. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1518. (plane_wm << WM1_LP_SR_SHIFT) |
  1519. cursor_wm);
  1520. /* WM3 */
  1521. if (!ironlake_compute_srwm(dev, 3, enabled,
  1522. SNB_READ_WM3_LATENCY() * 500,
  1523. &sandybridge_display_srwm_info,
  1524. &sandybridge_cursor_srwm_info,
  1525. &fbc_wm, &plane_wm, &cursor_wm))
  1526. return;
  1527. I915_WRITE(WM3_LP_ILK,
  1528. WM3_LP_EN |
  1529. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1530. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1531. (plane_wm << WM1_LP_SR_SHIFT) |
  1532. cursor_wm);
  1533. }
  1534. static bool
  1535. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  1536. uint32_t sprite_width, int pixel_size,
  1537. const struct intel_watermark_params *display,
  1538. int display_latency_ns, int *sprite_wm)
  1539. {
  1540. struct drm_crtc *crtc;
  1541. int clock;
  1542. int entries, tlb_miss;
  1543. crtc = intel_get_crtc_for_plane(dev, plane);
  1544. if (crtc->fb == NULL || !crtc->enabled) {
  1545. *sprite_wm = display->guard_size;
  1546. return false;
  1547. }
  1548. clock = crtc->mode.clock;
  1549. /* Use the small buffer method to calculate the sprite watermark */
  1550. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  1551. tlb_miss = display->fifo_size*display->cacheline_size -
  1552. sprite_width * 8;
  1553. if (tlb_miss > 0)
  1554. entries += tlb_miss;
  1555. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  1556. *sprite_wm = entries + display->guard_size;
  1557. if (*sprite_wm > (int)display->max_wm)
  1558. *sprite_wm = display->max_wm;
  1559. return true;
  1560. }
  1561. static bool
  1562. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  1563. uint32_t sprite_width, int pixel_size,
  1564. const struct intel_watermark_params *display,
  1565. int latency_ns, int *sprite_wm)
  1566. {
  1567. struct drm_crtc *crtc;
  1568. unsigned long line_time_us;
  1569. int clock;
  1570. int line_count, line_size;
  1571. int small, large;
  1572. int entries;
  1573. if (!latency_ns) {
  1574. *sprite_wm = 0;
  1575. return false;
  1576. }
  1577. crtc = intel_get_crtc_for_plane(dev, plane);
  1578. clock = crtc->mode.clock;
  1579. if (!clock) {
  1580. *sprite_wm = 0;
  1581. return false;
  1582. }
  1583. line_time_us = (sprite_width * 1000) / clock;
  1584. if (!line_time_us) {
  1585. *sprite_wm = 0;
  1586. return false;
  1587. }
  1588. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1589. line_size = sprite_width * pixel_size;
  1590. /* Use the minimum of the small and large buffer method for primary */
  1591. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1592. large = line_count * line_size;
  1593. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1594. *sprite_wm = entries + display->guard_size;
  1595. return *sprite_wm > 0x3ff ? false : true;
  1596. }
  1597. void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  1598. uint32_t sprite_width, int pixel_size)
  1599. {
  1600. struct drm_i915_private *dev_priv = dev->dev_private;
  1601. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1602. u32 val;
  1603. int sprite_wm, reg;
  1604. int ret;
  1605. switch (pipe) {
  1606. case 0:
  1607. reg = WM0_PIPEA_ILK;
  1608. break;
  1609. case 1:
  1610. reg = WM0_PIPEB_ILK;
  1611. break;
  1612. case 2:
  1613. reg = WM0_PIPEC_IVB;
  1614. break;
  1615. default:
  1616. return; /* bad pipe */
  1617. }
  1618. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  1619. &sandybridge_display_wm_info,
  1620. latency, &sprite_wm);
  1621. if (!ret) {
  1622. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
  1623. pipe);
  1624. return;
  1625. }
  1626. val = I915_READ(reg);
  1627. val &= ~WM0_PIPE_SPRITE_MASK;
  1628. I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  1629. DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
  1630. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1631. pixel_size,
  1632. &sandybridge_display_srwm_info,
  1633. SNB_READ_WM1_LATENCY() * 500,
  1634. &sprite_wm);
  1635. if (!ret) {
  1636. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
  1637. pipe);
  1638. return;
  1639. }
  1640. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  1641. /* Only IVB has two more LP watermarks for sprite */
  1642. if (!IS_IVYBRIDGE(dev))
  1643. return;
  1644. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1645. pixel_size,
  1646. &sandybridge_display_srwm_info,
  1647. SNB_READ_WM2_LATENCY() * 500,
  1648. &sprite_wm);
  1649. if (!ret) {
  1650. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
  1651. pipe);
  1652. return;
  1653. }
  1654. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  1655. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1656. pixel_size,
  1657. &sandybridge_display_srwm_info,
  1658. SNB_READ_WM3_LATENCY() * 500,
  1659. &sprite_wm);
  1660. if (!ret) {
  1661. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
  1662. pipe);
  1663. return;
  1664. }
  1665. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  1666. }
  1667. /**
  1668. * intel_update_watermarks - update FIFO watermark values based on current modes
  1669. *
  1670. * Calculate watermark values for the various WM regs based on current mode
  1671. * and plane configuration.
  1672. *
  1673. * There are several cases to deal with here:
  1674. * - normal (i.e. non-self-refresh)
  1675. * - self-refresh (SR) mode
  1676. * - lines are large relative to FIFO size (buffer can hold up to 2)
  1677. * - lines are small relative to FIFO size (buffer can hold more than 2
  1678. * lines), so need to account for TLB latency
  1679. *
  1680. * The normal calculation is:
  1681. * watermark = dotclock * bytes per pixel * latency
  1682. * where latency is platform & configuration dependent (we assume pessimal
  1683. * values here).
  1684. *
  1685. * The SR calculation is:
  1686. * watermark = (trunc(latency/line time)+1) * surface width *
  1687. * bytes per pixel
  1688. * where
  1689. * line time = htotal / dotclock
  1690. * surface width = hdisplay for normal plane and 64 for cursor
  1691. * and latency is assumed to be high, as above.
  1692. *
  1693. * The final value programmed to the register should always be rounded up,
  1694. * and include an extra 2 entries to account for clock crossings.
  1695. *
  1696. * We don't use the sprite, so we can ignore that. And on Crestline we have
  1697. * to set the non-SR watermarks to 8.
  1698. */
  1699. void intel_update_watermarks(struct drm_device *dev)
  1700. {
  1701. struct drm_i915_private *dev_priv = dev->dev_private;
  1702. if (dev_priv->display.update_wm)
  1703. dev_priv->display.update_wm(dev);
  1704. }
  1705. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  1706. uint32_t sprite_width, int pixel_size)
  1707. {
  1708. struct drm_i915_private *dev_priv = dev->dev_private;
  1709. if (dev_priv->display.update_sprite_wm)
  1710. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  1711. pixel_size);
  1712. }
  1713. static struct drm_i915_gem_object *
  1714. intel_alloc_context_page(struct drm_device *dev)
  1715. {
  1716. struct drm_i915_gem_object *ctx;
  1717. int ret;
  1718. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1719. ctx = i915_gem_alloc_object(dev, 4096);
  1720. if (!ctx) {
  1721. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  1722. return NULL;
  1723. }
  1724. ret = i915_gem_object_pin(ctx, 4096, true);
  1725. if (ret) {
  1726. DRM_ERROR("failed to pin power context: %d\n", ret);
  1727. goto err_unref;
  1728. }
  1729. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  1730. if (ret) {
  1731. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  1732. goto err_unpin;
  1733. }
  1734. return ctx;
  1735. err_unpin:
  1736. i915_gem_object_unpin(ctx);
  1737. err_unref:
  1738. drm_gem_object_unreference(&ctx->base);
  1739. mutex_unlock(&dev->struct_mutex);
  1740. return NULL;
  1741. }
  1742. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  1743. {
  1744. struct drm_i915_private *dev_priv = dev->dev_private;
  1745. u16 rgvswctl;
  1746. rgvswctl = I915_READ16(MEMSWCTL);
  1747. if (rgvswctl & MEMCTL_CMD_STS) {
  1748. DRM_DEBUG("gpu busy, RCS change rejected\n");
  1749. return false; /* still busy with another command */
  1750. }
  1751. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  1752. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  1753. I915_WRITE16(MEMSWCTL, rgvswctl);
  1754. POSTING_READ16(MEMSWCTL);
  1755. rgvswctl |= MEMCTL_CMD_STS;
  1756. I915_WRITE16(MEMSWCTL, rgvswctl);
  1757. return true;
  1758. }
  1759. void ironlake_enable_drps(struct drm_device *dev)
  1760. {
  1761. struct drm_i915_private *dev_priv = dev->dev_private;
  1762. u32 rgvmodectl = I915_READ(MEMMODECTL);
  1763. u8 fmax, fmin, fstart, vstart;
  1764. /* Enable temp reporting */
  1765. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  1766. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  1767. /* 100ms RC evaluation intervals */
  1768. I915_WRITE(RCUPEI, 100000);
  1769. I915_WRITE(RCDNEI, 100000);
  1770. /* Set max/min thresholds to 90ms and 80ms respectively */
  1771. I915_WRITE(RCBMAXAVG, 90000);
  1772. I915_WRITE(RCBMINAVG, 80000);
  1773. I915_WRITE(MEMIHYST, 1);
  1774. /* Set up min, max, and cur for interrupt handling */
  1775. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  1776. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  1777. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  1778. MEMMODE_FSTART_SHIFT;
  1779. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  1780. PXVFREQ_PX_SHIFT;
  1781. dev_priv->fmax = fmax; /* IPS callback will increase this */
  1782. dev_priv->fstart = fstart;
  1783. dev_priv->max_delay = fstart;
  1784. dev_priv->min_delay = fmin;
  1785. dev_priv->cur_delay = fstart;
  1786. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  1787. fmax, fmin, fstart);
  1788. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  1789. /*
  1790. * Interrupts will be enabled in ironlake_irq_postinstall
  1791. */
  1792. I915_WRITE(VIDSTART, vstart);
  1793. POSTING_READ(VIDSTART);
  1794. rgvmodectl |= MEMMODE_SWMODE_EN;
  1795. I915_WRITE(MEMMODECTL, rgvmodectl);
  1796. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  1797. DRM_ERROR("stuck trying to change perf mode\n");
  1798. msleep(1);
  1799. ironlake_set_drps(dev, fstart);
  1800. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  1801. I915_READ(0x112e0);
  1802. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  1803. dev_priv->last_count2 = I915_READ(0x112f4);
  1804. getrawmonotonic(&dev_priv->last_time2);
  1805. }
  1806. void ironlake_disable_drps(struct drm_device *dev)
  1807. {
  1808. struct drm_i915_private *dev_priv = dev->dev_private;
  1809. u16 rgvswctl = I915_READ16(MEMSWCTL);
  1810. /* Ack interrupts, disable EFC interrupt */
  1811. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  1812. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  1813. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  1814. I915_WRITE(DEIIR, DE_PCU_EVENT);
  1815. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  1816. /* Go back to the starting frequency */
  1817. ironlake_set_drps(dev, dev_priv->fstart);
  1818. msleep(1);
  1819. rgvswctl |= MEMCTL_CMD_STS;
  1820. I915_WRITE(MEMSWCTL, rgvswctl);
  1821. msleep(1);
  1822. }
  1823. void gen6_set_rps(struct drm_device *dev, u8 val)
  1824. {
  1825. struct drm_i915_private *dev_priv = dev->dev_private;
  1826. u32 swreq;
  1827. swreq = (val & 0x3ff) << 25;
  1828. I915_WRITE(GEN6_RPNSWREQ, swreq);
  1829. }
  1830. void gen6_disable_rps(struct drm_device *dev)
  1831. {
  1832. struct drm_i915_private *dev_priv = dev->dev_private;
  1833. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  1834. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  1835. I915_WRITE(GEN6_PMIER, 0);
  1836. /* Complete PM interrupt masking here doesn't race with the rps work
  1837. * item again unmasking PM interrupts because that is using a different
  1838. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  1839. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  1840. spin_lock_irq(&dev_priv->rps_lock);
  1841. dev_priv->pm_iir = 0;
  1842. spin_unlock_irq(&dev_priv->rps_lock);
  1843. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  1844. }
  1845. int intel_enable_rc6(const struct drm_device *dev)
  1846. {
  1847. /*
  1848. * Respect the kernel parameter if it is set
  1849. */
  1850. if (i915_enable_rc6 >= 0)
  1851. return i915_enable_rc6;
  1852. /*
  1853. * Disable RC6 on Ironlake
  1854. */
  1855. if (INTEL_INFO(dev)->gen == 5)
  1856. return 0;
  1857. /* Sorry Haswell, no RC6 for you for now. */
  1858. if (IS_HASWELL(dev))
  1859. return 0;
  1860. /*
  1861. * Disable rc6 on Sandybridge
  1862. */
  1863. if (INTEL_INFO(dev)->gen == 6) {
  1864. DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
  1865. return INTEL_RC6_ENABLE;
  1866. }
  1867. DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
  1868. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  1869. }
  1870. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  1871. {
  1872. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  1873. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  1874. u32 pcu_mbox, rc6_mask = 0;
  1875. u32 gtfifodbg;
  1876. int cur_freq, min_freq, max_freq;
  1877. int rc6_mode;
  1878. int i;
  1879. /* Here begins a magic sequence of register writes to enable
  1880. * auto-downclocking.
  1881. *
  1882. * Perhaps there might be some value in exposing these to
  1883. * userspace...
  1884. */
  1885. I915_WRITE(GEN6_RC_STATE, 0);
  1886. mutex_lock(&dev_priv->dev->struct_mutex);
  1887. /* Clear the DBG now so we don't confuse earlier errors */
  1888. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  1889. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  1890. I915_WRITE(GTFIFODBG, gtfifodbg);
  1891. }
  1892. gen6_gt_force_wake_get(dev_priv);
  1893. /* disable the counters and set deterministic thresholds */
  1894. I915_WRITE(GEN6_RC_CONTROL, 0);
  1895. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  1896. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  1897. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  1898. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  1899. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  1900. for (i = 0; i < I915_NUM_RINGS; i++)
  1901. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  1902. I915_WRITE(GEN6_RC_SLEEP, 0);
  1903. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  1904. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  1905. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  1906. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  1907. rc6_mode = intel_enable_rc6(dev_priv->dev);
  1908. if (rc6_mode & INTEL_RC6_ENABLE)
  1909. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  1910. if (rc6_mode & INTEL_RC6p_ENABLE)
  1911. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  1912. if (rc6_mode & INTEL_RC6pp_ENABLE)
  1913. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  1914. DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
  1915. (rc6_mode & INTEL_RC6_ENABLE) ? "on" : "off",
  1916. (rc6_mode & INTEL_RC6p_ENABLE) ? "on" : "off",
  1917. (rc6_mode & INTEL_RC6pp_ENABLE) ? "on" : "off");
  1918. I915_WRITE(GEN6_RC_CONTROL,
  1919. rc6_mask |
  1920. GEN6_RC_CTL_EI_MODE(1) |
  1921. GEN6_RC_CTL_HW_ENABLE);
  1922. I915_WRITE(GEN6_RPNSWREQ,
  1923. GEN6_FREQUENCY(10) |
  1924. GEN6_OFFSET(0) |
  1925. GEN6_AGGRESSIVE_TURBO);
  1926. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  1927. GEN6_FREQUENCY(12));
  1928. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  1929. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  1930. 18 << 24 |
  1931. 6 << 16);
  1932. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  1933. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  1934. I915_WRITE(GEN6_RP_UP_EI, 100000);
  1935. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  1936. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  1937. I915_WRITE(GEN6_RP_CONTROL,
  1938. GEN6_RP_MEDIA_TURBO |
  1939. GEN6_RP_MEDIA_HW_MODE |
  1940. GEN6_RP_MEDIA_IS_GFX |
  1941. GEN6_RP_ENABLE |
  1942. GEN6_RP_UP_BUSY_AVG |
  1943. GEN6_RP_DOWN_IDLE_CONT);
  1944. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  1945. 500))
  1946. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  1947. I915_WRITE(GEN6_PCODE_DATA, 0);
  1948. I915_WRITE(GEN6_PCODE_MAILBOX,
  1949. GEN6_PCODE_READY |
  1950. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  1951. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  1952. 500))
  1953. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  1954. min_freq = (rp_state_cap & 0xff0000) >> 16;
  1955. max_freq = rp_state_cap & 0xff;
  1956. cur_freq = (gt_perf_status & 0xff00) >> 8;
  1957. /* Check for overclock support */
  1958. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  1959. 500))
  1960. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  1961. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  1962. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  1963. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  1964. 500))
  1965. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  1966. if (pcu_mbox & (1<<31)) { /* OC supported */
  1967. max_freq = pcu_mbox & 0xff;
  1968. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  1969. }
  1970. /* In units of 100MHz */
  1971. dev_priv->max_delay = max_freq;
  1972. dev_priv->min_delay = min_freq;
  1973. dev_priv->cur_delay = cur_freq;
  1974. /* requires MSI enabled */
  1975. I915_WRITE(GEN6_PMIER,
  1976. GEN6_PM_MBOX_EVENT |
  1977. GEN6_PM_THERMAL_EVENT |
  1978. GEN6_PM_RP_DOWN_TIMEOUT |
  1979. GEN6_PM_RP_UP_THRESHOLD |
  1980. GEN6_PM_RP_DOWN_THRESHOLD |
  1981. GEN6_PM_RP_UP_EI_EXPIRED |
  1982. GEN6_PM_RP_DOWN_EI_EXPIRED);
  1983. spin_lock_irq(&dev_priv->rps_lock);
  1984. WARN_ON(dev_priv->pm_iir != 0);
  1985. I915_WRITE(GEN6_PMIMR, 0);
  1986. spin_unlock_irq(&dev_priv->rps_lock);
  1987. /* enable all PM interrupts */
  1988. I915_WRITE(GEN6_PMINTRMSK, 0);
  1989. gen6_gt_force_wake_put(dev_priv);
  1990. mutex_unlock(&dev_priv->dev->struct_mutex);
  1991. }
  1992. void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  1993. {
  1994. int min_freq = 15;
  1995. int gpu_freq, ia_freq, max_ia_freq;
  1996. int scaling_factor = 180;
  1997. max_ia_freq = cpufreq_quick_get_max(0);
  1998. /*
  1999. * Default to measured freq if none found, PCU will ensure we don't go
  2000. * over
  2001. */
  2002. if (!max_ia_freq)
  2003. max_ia_freq = tsc_khz;
  2004. /* Convert from kHz to MHz */
  2005. max_ia_freq /= 1000;
  2006. mutex_lock(&dev_priv->dev->struct_mutex);
  2007. /*
  2008. * For each potential GPU frequency, load a ring frequency we'd like
  2009. * to use for memory access. We do this by specifying the IA frequency
  2010. * the PCU should use as a reference to determine the ring frequency.
  2011. */
  2012. for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
  2013. gpu_freq--) {
  2014. int diff = dev_priv->max_delay - gpu_freq;
  2015. /*
  2016. * For GPU frequencies less than 750MHz, just use the lowest
  2017. * ring freq.
  2018. */
  2019. if (gpu_freq < min_freq)
  2020. ia_freq = 800;
  2021. else
  2022. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  2023. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  2024. I915_WRITE(GEN6_PCODE_DATA,
  2025. (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
  2026. gpu_freq);
  2027. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
  2028. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  2029. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
  2030. GEN6_PCODE_READY) == 0, 10)) {
  2031. DRM_ERROR("pcode write of freq table timed out\n");
  2032. continue;
  2033. }
  2034. }
  2035. mutex_unlock(&dev_priv->dev->struct_mutex);
  2036. }
  2037. static void ironlake_teardown_rc6(struct drm_device *dev)
  2038. {
  2039. struct drm_i915_private *dev_priv = dev->dev_private;
  2040. if (dev_priv->renderctx) {
  2041. i915_gem_object_unpin(dev_priv->renderctx);
  2042. drm_gem_object_unreference(&dev_priv->renderctx->base);
  2043. dev_priv->renderctx = NULL;
  2044. }
  2045. if (dev_priv->pwrctx) {
  2046. i915_gem_object_unpin(dev_priv->pwrctx);
  2047. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  2048. dev_priv->pwrctx = NULL;
  2049. }
  2050. }
  2051. void ironlake_disable_rc6(struct drm_device *dev)
  2052. {
  2053. struct drm_i915_private *dev_priv = dev->dev_private;
  2054. if (I915_READ(PWRCTXA)) {
  2055. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  2056. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  2057. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  2058. 50);
  2059. I915_WRITE(PWRCTXA, 0);
  2060. POSTING_READ(PWRCTXA);
  2061. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  2062. POSTING_READ(RSTDBYCTL);
  2063. }
  2064. ironlake_teardown_rc6(dev);
  2065. }
  2066. static int ironlake_setup_rc6(struct drm_device *dev)
  2067. {
  2068. struct drm_i915_private *dev_priv = dev->dev_private;
  2069. if (dev_priv->renderctx == NULL)
  2070. dev_priv->renderctx = intel_alloc_context_page(dev);
  2071. if (!dev_priv->renderctx)
  2072. return -ENOMEM;
  2073. if (dev_priv->pwrctx == NULL)
  2074. dev_priv->pwrctx = intel_alloc_context_page(dev);
  2075. if (!dev_priv->pwrctx) {
  2076. ironlake_teardown_rc6(dev);
  2077. return -ENOMEM;
  2078. }
  2079. return 0;
  2080. }
  2081. void ironlake_enable_rc6(struct drm_device *dev)
  2082. {
  2083. struct drm_i915_private *dev_priv = dev->dev_private;
  2084. int ret;
  2085. /* rc6 disabled by default due to repeated reports of hanging during
  2086. * boot and resume.
  2087. */
  2088. if (!intel_enable_rc6(dev))
  2089. return;
  2090. mutex_lock(&dev->struct_mutex);
  2091. ret = ironlake_setup_rc6(dev);
  2092. if (ret) {
  2093. mutex_unlock(&dev->struct_mutex);
  2094. return;
  2095. }
  2096. /*
  2097. * GPU can automatically power down the render unit if given a page
  2098. * to save state.
  2099. */
  2100. ret = BEGIN_LP_RING(6);
  2101. if (ret) {
  2102. ironlake_teardown_rc6(dev);
  2103. mutex_unlock(&dev->struct_mutex);
  2104. return;
  2105. }
  2106. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  2107. OUT_RING(MI_SET_CONTEXT);
  2108. OUT_RING(dev_priv->renderctx->gtt_offset |
  2109. MI_MM_SPACE_GTT |
  2110. MI_SAVE_EXT_STATE_EN |
  2111. MI_RESTORE_EXT_STATE_EN |
  2112. MI_RESTORE_INHIBIT);
  2113. OUT_RING(MI_SUSPEND_FLUSH);
  2114. OUT_RING(MI_NOOP);
  2115. OUT_RING(MI_FLUSH);
  2116. ADVANCE_LP_RING();
  2117. /*
  2118. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  2119. * does an implicit flush, combined with MI_FLUSH above, it should be
  2120. * safe to assume that renderctx is valid
  2121. */
  2122. ret = intel_wait_ring_idle(LP_RING(dev_priv));
  2123. if (ret) {
  2124. DRM_ERROR("failed to enable ironlake power power savings\n");
  2125. ironlake_teardown_rc6(dev);
  2126. mutex_unlock(&dev->struct_mutex);
  2127. return;
  2128. }
  2129. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  2130. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  2131. mutex_unlock(&dev->struct_mutex);
  2132. }