intel_runtime_pm.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. /*
  2. * Copyright © 2012-2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eugeni Dodonov <eugeni.dodonov@intel.com>
  25. * Daniel Vetter <daniel.vetter@ffwll.ch>
  26. *
  27. */
  28. #include <linux/pm_runtime.h>
  29. #include <linux/vgaarb.h>
  30. #include "i915_drv.h"
  31. #include "intel_drv.h"
  32. /**
  33. * DOC: runtime pm
  34. *
  35. * The i915 driver supports dynamic enabling and disabling of entire hardware
  36. * blocks at runtime. This is especially important on the display side where
  37. * software is supposed to control many power gates manually on recent hardware,
  38. * since on the GT side a lot of the power management is done by the hardware.
  39. * But even there some manual control at the device level is required.
  40. *
  41. * Since i915 supports a diverse set of platforms with a unified codebase and
  42. * hardware engineers just love to shuffle functionality around between power
  43. * domains there's a sizeable amount of indirection required. This file provides
  44. * generic functions to the driver for grabbing and releasing references for
  45. * abstract power domains. It then maps those to the actual power wells
  46. * present for a given platform.
  47. */
  48. #define GEN9_ENABLE_DC5(dev) 0
  49. #define SKL_ENABLE_DC6(dev) IS_SKYLAKE(dev)
  50. #define for_each_power_well(i, power_well, domain_mask, power_domains) \
  51. for (i = 0; \
  52. i < (power_domains)->power_well_count && \
  53. ((power_well) = &(power_domains)->power_wells[i]); \
  54. i++) \
  55. if ((power_well)->domains & (domain_mask))
  56. #define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
  57. for (i = (power_domains)->power_well_count - 1; \
  58. i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
  59. i--) \
  60. if ((power_well)->domains & (domain_mask))
  61. bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
  62. int power_well_id);
  63. /*
  64. * We should only use the power well if we explicitly asked the hardware to
  65. * enable it, so check if it's enabled and also check if we've requested it to
  66. * be enabled.
  67. */
  68. static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
  69. struct i915_power_well *power_well)
  70. {
  71. return I915_READ(HSW_PWR_WELL_DRIVER) ==
  72. (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
  73. }
  74. /**
  75. * __intel_display_power_is_enabled - unlocked check for a power domain
  76. * @dev_priv: i915 device instance
  77. * @domain: power domain to check
  78. *
  79. * This is the unlocked version of intel_display_power_is_enabled() and should
  80. * only be used from error capture and recovery code where deadlocks are
  81. * possible.
  82. *
  83. * Returns:
  84. * True when the power domain is enabled, false otherwise.
  85. */
  86. bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
  87. enum intel_display_power_domain domain)
  88. {
  89. struct i915_power_domains *power_domains;
  90. struct i915_power_well *power_well;
  91. bool is_enabled;
  92. int i;
  93. if (dev_priv->pm.suspended)
  94. return false;
  95. power_domains = &dev_priv->power_domains;
  96. is_enabled = true;
  97. for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
  98. if (power_well->always_on)
  99. continue;
  100. if (!power_well->hw_enabled) {
  101. is_enabled = false;
  102. break;
  103. }
  104. }
  105. return is_enabled;
  106. }
  107. /**
  108. * intel_display_power_is_enabled - check for a power domain
  109. * @dev_priv: i915 device instance
  110. * @domain: power domain to check
  111. *
  112. * This function can be used to check the hw power domain state. It is mostly
  113. * used in hardware state readout functions. Everywhere else code should rely
  114. * upon explicit power domain reference counting to ensure that the hardware
  115. * block is powered up before accessing it.
  116. *
  117. * Callers must hold the relevant modesetting locks to ensure that concurrent
  118. * threads can't disable the power well while the caller tries to read a few
  119. * registers.
  120. *
  121. * Returns:
  122. * True when the power domain is enabled, false otherwise.
  123. */
  124. bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
  125. enum intel_display_power_domain domain)
  126. {
  127. struct i915_power_domains *power_domains;
  128. bool ret;
  129. power_domains = &dev_priv->power_domains;
  130. mutex_lock(&power_domains->lock);
  131. ret = __intel_display_power_is_enabled(dev_priv, domain);
  132. mutex_unlock(&power_domains->lock);
  133. return ret;
  134. }
  135. /**
  136. * intel_display_set_init_power - set the initial power domain state
  137. * @dev_priv: i915 device instance
  138. * @enable: whether to enable or disable the initial power domain state
  139. *
  140. * For simplicity our driver load/unload and system suspend/resume code assumes
  141. * that all power domains are always enabled. This functions controls the state
  142. * of this little hack. While the initial power domain state is enabled runtime
  143. * pm is effectively disabled.
  144. */
  145. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  146. bool enable)
  147. {
  148. if (dev_priv->power_domains.init_power_on == enable)
  149. return;
  150. if (enable)
  151. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  152. else
  153. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  154. dev_priv->power_domains.init_power_on = enable;
  155. }
  156. /*
  157. * Starting with Haswell, we have a "Power Down Well" that can be turned off
  158. * when not needed anymore. We have 4 registers that can request the power well
  159. * to be enabled, and it will only be disabled if none of the registers is
  160. * requesting it to be enabled.
  161. */
  162. static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
  163. {
  164. struct drm_device *dev = dev_priv->dev;
  165. /*
  166. * After we re-enable the power well, if we touch VGA register 0x3d5
  167. * we'll get unclaimed register interrupts. This stops after we write
  168. * anything to the VGA MSR register. The vgacon module uses this
  169. * register all the time, so if we unbind our driver and, as a
  170. * consequence, bind vgacon, we'll get stuck in an infinite loop at
  171. * console_unlock(). So make here we touch the VGA MSR register, making
  172. * sure vgacon can keep working normally without triggering interrupts
  173. * and error messages.
  174. */
  175. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  176. outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
  177. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  178. if (IS_BROADWELL(dev))
  179. gen8_irq_power_well_post_enable(dev_priv,
  180. 1 << PIPE_C | 1 << PIPE_B);
  181. }
  182. static void skl_power_well_post_enable(struct drm_i915_private *dev_priv,
  183. struct i915_power_well *power_well)
  184. {
  185. struct drm_device *dev = dev_priv->dev;
  186. /*
  187. * After we re-enable the power well, if we touch VGA register 0x3d5
  188. * we'll get unclaimed register interrupts. This stops after we write
  189. * anything to the VGA MSR register. The vgacon module uses this
  190. * register all the time, so if we unbind our driver and, as a
  191. * consequence, bind vgacon, we'll get stuck in an infinite loop at
  192. * console_unlock(). So make here we touch the VGA MSR register, making
  193. * sure vgacon can keep working normally without triggering interrupts
  194. * and error messages.
  195. */
  196. if (power_well->data == SKL_DISP_PW_2) {
  197. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  198. outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
  199. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  200. gen8_irq_power_well_post_enable(dev_priv,
  201. 1 << PIPE_C | 1 << PIPE_B);
  202. }
  203. if (power_well->data == SKL_DISP_PW_1) {
  204. intel_prepare_ddi(dev);
  205. gen8_irq_power_well_post_enable(dev_priv, 1 << PIPE_A);
  206. }
  207. }
  208. static void hsw_set_power_well(struct drm_i915_private *dev_priv,
  209. struct i915_power_well *power_well, bool enable)
  210. {
  211. bool is_enabled, enable_requested;
  212. uint32_t tmp;
  213. tmp = I915_READ(HSW_PWR_WELL_DRIVER);
  214. is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
  215. enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
  216. if (enable) {
  217. if (!enable_requested)
  218. I915_WRITE(HSW_PWR_WELL_DRIVER,
  219. HSW_PWR_WELL_ENABLE_REQUEST);
  220. if (!is_enabled) {
  221. DRM_DEBUG_KMS("Enabling power well\n");
  222. if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
  223. HSW_PWR_WELL_STATE_ENABLED), 20))
  224. DRM_ERROR("Timeout enabling power well\n");
  225. hsw_power_well_post_enable(dev_priv);
  226. }
  227. } else {
  228. if (enable_requested) {
  229. I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
  230. POSTING_READ(HSW_PWR_WELL_DRIVER);
  231. DRM_DEBUG_KMS("Requesting to disable the power well\n");
  232. }
  233. }
  234. }
  235. #define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
  236. BIT(POWER_DOMAIN_TRANSCODER_A) | \
  237. BIT(POWER_DOMAIN_PIPE_B) | \
  238. BIT(POWER_DOMAIN_TRANSCODER_B) | \
  239. BIT(POWER_DOMAIN_PIPE_C) | \
  240. BIT(POWER_DOMAIN_TRANSCODER_C) | \
  241. BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
  242. BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
  243. BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
  244. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  245. BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
  246. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  247. BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
  248. BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
  249. BIT(POWER_DOMAIN_AUX_B) | \
  250. BIT(POWER_DOMAIN_AUX_C) | \
  251. BIT(POWER_DOMAIN_AUX_D) | \
  252. BIT(POWER_DOMAIN_AUDIO) | \
  253. BIT(POWER_DOMAIN_VGA) | \
  254. BIT(POWER_DOMAIN_INIT))
  255. #define SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS ( \
  256. SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
  257. BIT(POWER_DOMAIN_PLLS) | \
  258. BIT(POWER_DOMAIN_PIPE_A) | \
  259. BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
  260. BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
  261. BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
  262. BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
  263. BIT(POWER_DOMAIN_AUX_A) | \
  264. BIT(POWER_DOMAIN_INIT))
  265. #define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS ( \
  266. BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
  267. BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
  268. BIT(POWER_DOMAIN_INIT))
  269. #define SKL_DISPLAY_DDI_B_POWER_DOMAINS ( \
  270. BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
  271. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  272. BIT(POWER_DOMAIN_INIT))
  273. #define SKL_DISPLAY_DDI_C_POWER_DOMAINS ( \
  274. BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
  275. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  276. BIT(POWER_DOMAIN_INIT))
  277. #define SKL_DISPLAY_DDI_D_POWER_DOMAINS ( \
  278. BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
  279. BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
  280. BIT(POWER_DOMAIN_INIT))
  281. #define SKL_DISPLAY_MISC_IO_POWER_DOMAINS ( \
  282. SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
  283. BIT(POWER_DOMAIN_PLLS) | \
  284. BIT(POWER_DOMAIN_INIT))
  285. #define SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
  286. (POWER_DOMAIN_MASK & ~(SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
  287. SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
  288. SKL_DISPLAY_DDI_A_E_POWER_DOMAINS | \
  289. SKL_DISPLAY_DDI_B_POWER_DOMAINS | \
  290. SKL_DISPLAY_DDI_C_POWER_DOMAINS | \
  291. SKL_DISPLAY_DDI_D_POWER_DOMAINS | \
  292. SKL_DISPLAY_MISC_IO_POWER_DOMAINS)) | \
  293. BIT(POWER_DOMAIN_INIT))
  294. #define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
  295. BIT(POWER_DOMAIN_TRANSCODER_A) | \
  296. BIT(POWER_DOMAIN_PIPE_B) | \
  297. BIT(POWER_DOMAIN_TRANSCODER_B) | \
  298. BIT(POWER_DOMAIN_PIPE_C) | \
  299. BIT(POWER_DOMAIN_TRANSCODER_C) | \
  300. BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
  301. BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
  302. BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
  303. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  304. BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
  305. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  306. BIT(POWER_DOMAIN_AUX_B) | \
  307. BIT(POWER_DOMAIN_AUX_C) | \
  308. BIT(POWER_DOMAIN_AUDIO) | \
  309. BIT(POWER_DOMAIN_VGA) | \
  310. BIT(POWER_DOMAIN_INIT))
  311. #define BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS ( \
  312. BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
  313. BIT(POWER_DOMAIN_PIPE_A) | \
  314. BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
  315. BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
  316. BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
  317. BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
  318. BIT(POWER_DOMAIN_AUX_A) | \
  319. BIT(POWER_DOMAIN_PLLS) | \
  320. BIT(POWER_DOMAIN_INIT))
  321. #define BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
  322. (POWER_DOMAIN_MASK & ~(BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
  323. BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS)) | \
  324. BIT(POWER_DOMAIN_INIT))
  325. static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
  326. {
  327. struct drm_device *dev = dev_priv->dev;
  328. WARN(!IS_BROXTON(dev), "Platform doesn't support DC9.\n");
  329. WARN((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
  330. "DC9 already programmed to be enabled.\n");
  331. WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
  332. "DC5 still not disabled to enable DC9.\n");
  333. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on.\n");
  334. WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
  335. /*
  336. * TODO: check for the following to verify the conditions to enter DC9
  337. * state are satisfied:
  338. * 1] Check relevant display engine registers to verify if mode set
  339. * disable sequence was followed.
  340. * 2] Check if display uninitialize sequence is initialized.
  341. */
  342. }
  343. static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
  344. {
  345. WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
  346. WARN(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
  347. "DC9 already programmed to be disabled.\n");
  348. WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
  349. "DC5 still not disabled.\n");
  350. /*
  351. * TODO: check for the following to verify DC9 state was indeed
  352. * entered before programming to disable it:
  353. * 1] Check relevant display engine registers to verify if mode
  354. * set disable sequence was followed.
  355. * 2] Check if display uninitialize sequence is initialized.
  356. */
  357. }
  358. void bxt_enable_dc9(struct drm_i915_private *dev_priv)
  359. {
  360. uint32_t val;
  361. assert_can_enable_dc9(dev_priv);
  362. DRM_DEBUG_KMS("Enabling DC9\n");
  363. val = I915_READ(DC_STATE_EN);
  364. val |= DC_STATE_EN_DC9;
  365. I915_WRITE(DC_STATE_EN, val);
  366. POSTING_READ(DC_STATE_EN);
  367. }
  368. void bxt_disable_dc9(struct drm_i915_private *dev_priv)
  369. {
  370. uint32_t val;
  371. assert_can_disable_dc9(dev_priv);
  372. DRM_DEBUG_KMS("Disabling DC9\n");
  373. val = I915_READ(DC_STATE_EN);
  374. val &= ~DC_STATE_EN_DC9;
  375. I915_WRITE(DC_STATE_EN, val);
  376. POSTING_READ(DC_STATE_EN);
  377. }
  378. static void gen9_set_dc_state_debugmask_memory_up(
  379. struct drm_i915_private *dev_priv)
  380. {
  381. uint32_t val;
  382. /* The below bit doesn't need to be cleared ever afterwards */
  383. val = I915_READ(DC_STATE_DEBUG);
  384. if (!(val & DC_STATE_DEBUG_MASK_MEMORY_UP)) {
  385. val |= DC_STATE_DEBUG_MASK_MEMORY_UP;
  386. I915_WRITE(DC_STATE_DEBUG, val);
  387. POSTING_READ(DC_STATE_DEBUG);
  388. }
  389. }
  390. static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
  391. {
  392. struct drm_device *dev = dev_priv->dev;
  393. bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
  394. SKL_DISP_PW_2);
  395. WARN(!IS_SKYLAKE(dev), "Platform doesn't support DC5.\n");
  396. WARN(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
  397. WARN(pg2_enabled, "PG2 not disabled to enable DC5.\n");
  398. WARN((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
  399. "DC5 already programmed to be enabled.\n");
  400. WARN(dev_priv->pm.suspended,
  401. "DC5 cannot be enabled, if platform is runtime-suspended.\n");
  402. assert_csr_loaded(dev_priv);
  403. }
  404. static void assert_can_disable_dc5(struct drm_i915_private *dev_priv)
  405. {
  406. bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
  407. SKL_DISP_PW_2);
  408. /*
  409. * During initialization, the firmware may not be loaded yet.
  410. * We still want to make sure that the DC enabling flag is cleared.
  411. */
  412. if (dev_priv->power_domains.initializing)
  413. return;
  414. WARN(!pg2_enabled, "PG2 not enabled to disable DC5.\n");
  415. WARN(dev_priv->pm.suspended,
  416. "Disabling of DC5 while platform is runtime-suspended should never happen.\n");
  417. }
  418. static void gen9_enable_dc5(struct drm_i915_private *dev_priv)
  419. {
  420. uint32_t val;
  421. assert_can_enable_dc5(dev_priv);
  422. DRM_DEBUG_KMS("Enabling DC5\n");
  423. gen9_set_dc_state_debugmask_memory_up(dev_priv);
  424. val = I915_READ(DC_STATE_EN);
  425. val &= ~DC_STATE_EN_UPTO_DC5_DC6_MASK;
  426. val |= DC_STATE_EN_UPTO_DC5;
  427. I915_WRITE(DC_STATE_EN, val);
  428. POSTING_READ(DC_STATE_EN);
  429. }
  430. static void gen9_disable_dc5(struct drm_i915_private *dev_priv)
  431. {
  432. uint32_t val;
  433. assert_can_disable_dc5(dev_priv);
  434. DRM_DEBUG_KMS("Disabling DC5\n");
  435. val = I915_READ(DC_STATE_EN);
  436. val &= ~DC_STATE_EN_UPTO_DC5;
  437. I915_WRITE(DC_STATE_EN, val);
  438. POSTING_READ(DC_STATE_EN);
  439. }
  440. static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
  441. {
  442. struct drm_device *dev = dev_priv->dev;
  443. WARN(!IS_SKYLAKE(dev), "Platform doesn't support DC6.\n");
  444. WARN(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
  445. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  446. "Backlight is not disabled.\n");
  447. WARN((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
  448. "DC6 already programmed to be enabled.\n");
  449. assert_csr_loaded(dev_priv);
  450. }
  451. static void assert_can_disable_dc6(struct drm_i915_private *dev_priv)
  452. {
  453. /*
  454. * During initialization, the firmware may not be loaded yet.
  455. * We still want to make sure that the DC enabling flag is cleared.
  456. */
  457. if (dev_priv->power_domains.initializing)
  458. return;
  459. assert_csr_loaded(dev_priv);
  460. WARN(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
  461. "DC6 already programmed to be disabled.\n");
  462. }
  463. static void skl_enable_dc6(struct drm_i915_private *dev_priv)
  464. {
  465. uint32_t val;
  466. assert_can_enable_dc6(dev_priv);
  467. DRM_DEBUG_KMS("Enabling DC6\n");
  468. gen9_set_dc_state_debugmask_memory_up(dev_priv);
  469. val = I915_READ(DC_STATE_EN);
  470. val &= ~DC_STATE_EN_UPTO_DC5_DC6_MASK;
  471. val |= DC_STATE_EN_UPTO_DC6;
  472. I915_WRITE(DC_STATE_EN, val);
  473. POSTING_READ(DC_STATE_EN);
  474. }
  475. static void skl_disable_dc6(struct drm_i915_private *dev_priv)
  476. {
  477. uint32_t val;
  478. assert_can_disable_dc6(dev_priv);
  479. DRM_DEBUG_KMS("Disabling DC6\n");
  480. val = I915_READ(DC_STATE_EN);
  481. val &= ~DC_STATE_EN_UPTO_DC6;
  482. I915_WRITE(DC_STATE_EN, val);
  483. POSTING_READ(DC_STATE_EN);
  484. }
  485. static void skl_set_power_well(struct drm_i915_private *dev_priv,
  486. struct i915_power_well *power_well, bool enable)
  487. {
  488. struct drm_device *dev = dev_priv->dev;
  489. uint32_t tmp, fuse_status;
  490. uint32_t req_mask, state_mask;
  491. bool is_enabled, enable_requested, check_fuse_status = false;
  492. tmp = I915_READ(HSW_PWR_WELL_DRIVER);
  493. fuse_status = I915_READ(SKL_FUSE_STATUS);
  494. switch (power_well->data) {
  495. case SKL_DISP_PW_1:
  496. if (wait_for((I915_READ(SKL_FUSE_STATUS) &
  497. SKL_FUSE_PG0_DIST_STATUS), 1)) {
  498. DRM_ERROR("PG0 not enabled\n");
  499. return;
  500. }
  501. break;
  502. case SKL_DISP_PW_2:
  503. if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
  504. DRM_ERROR("PG1 in disabled state\n");
  505. return;
  506. }
  507. break;
  508. case SKL_DISP_PW_DDI_A_E:
  509. case SKL_DISP_PW_DDI_B:
  510. case SKL_DISP_PW_DDI_C:
  511. case SKL_DISP_PW_DDI_D:
  512. case SKL_DISP_PW_MISC_IO:
  513. break;
  514. default:
  515. WARN(1, "Unknown power well %lu\n", power_well->data);
  516. return;
  517. }
  518. req_mask = SKL_POWER_WELL_REQ(power_well->data);
  519. enable_requested = tmp & req_mask;
  520. state_mask = SKL_POWER_WELL_STATE(power_well->data);
  521. is_enabled = tmp & state_mask;
  522. if (enable) {
  523. if (!enable_requested) {
  524. WARN((tmp & state_mask) &&
  525. !I915_READ(HSW_PWR_WELL_BIOS),
  526. "Invalid for power well status to be enabled, unless done by the BIOS, \
  527. when request is to disable!\n");
  528. if ((GEN9_ENABLE_DC5(dev) || SKL_ENABLE_DC6(dev)) &&
  529. power_well->data == SKL_DISP_PW_2) {
  530. if (SKL_ENABLE_DC6(dev)) {
  531. skl_disable_dc6(dev_priv);
  532. /*
  533. * DDI buffer programming unnecessary during driver-load/resume
  534. * as it's already done during modeset initialization then.
  535. * It's also invalid here as encoder list is still uninitialized.
  536. */
  537. if (!dev_priv->power_domains.initializing)
  538. intel_prepare_ddi(dev);
  539. } else {
  540. gen9_disable_dc5(dev_priv);
  541. }
  542. }
  543. I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
  544. }
  545. if (!is_enabled) {
  546. DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
  547. if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
  548. state_mask), 1))
  549. DRM_ERROR("%s enable timeout\n",
  550. power_well->name);
  551. check_fuse_status = true;
  552. }
  553. } else {
  554. if (enable_requested) {
  555. I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
  556. POSTING_READ(HSW_PWR_WELL_DRIVER);
  557. DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
  558. if ((GEN9_ENABLE_DC5(dev) || SKL_ENABLE_DC6(dev)) &&
  559. power_well->data == SKL_DISP_PW_2) {
  560. enum csr_state state;
  561. /* TODO: wait for a completion event or
  562. * similar here instead of busy
  563. * waiting using wait_for function.
  564. */
  565. wait_for((state = intel_csr_load_status_get(dev_priv)) !=
  566. FW_UNINITIALIZED, 1000);
  567. if (state != FW_LOADED)
  568. DRM_ERROR("CSR firmware not ready (%d)\n",
  569. state);
  570. else
  571. if (SKL_ENABLE_DC6(dev))
  572. skl_enable_dc6(dev_priv);
  573. else
  574. gen9_enable_dc5(dev_priv);
  575. }
  576. }
  577. }
  578. if (check_fuse_status) {
  579. if (power_well->data == SKL_DISP_PW_1) {
  580. if (wait_for((I915_READ(SKL_FUSE_STATUS) &
  581. SKL_FUSE_PG1_DIST_STATUS), 1))
  582. DRM_ERROR("PG1 distributing status timeout\n");
  583. } else if (power_well->data == SKL_DISP_PW_2) {
  584. if (wait_for((I915_READ(SKL_FUSE_STATUS) &
  585. SKL_FUSE_PG2_DIST_STATUS), 1))
  586. DRM_ERROR("PG2 distributing status timeout\n");
  587. }
  588. }
  589. if (enable && !is_enabled)
  590. skl_power_well_post_enable(dev_priv, power_well);
  591. }
  592. static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
  593. struct i915_power_well *power_well)
  594. {
  595. hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
  596. /*
  597. * We're taking over the BIOS, so clear any requests made by it since
  598. * the driver is in charge now.
  599. */
  600. if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
  601. I915_WRITE(HSW_PWR_WELL_BIOS, 0);
  602. }
  603. static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
  604. struct i915_power_well *power_well)
  605. {
  606. hsw_set_power_well(dev_priv, power_well, true);
  607. }
  608. static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
  609. struct i915_power_well *power_well)
  610. {
  611. hsw_set_power_well(dev_priv, power_well, false);
  612. }
  613. static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
  614. struct i915_power_well *power_well)
  615. {
  616. uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
  617. SKL_POWER_WELL_STATE(power_well->data);
  618. return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
  619. }
  620. static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
  621. struct i915_power_well *power_well)
  622. {
  623. skl_set_power_well(dev_priv, power_well, power_well->count > 0);
  624. /* Clear any request made by BIOS as driver is taking over */
  625. I915_WRITE(HSW_PWR_WELL_BIOS, 0);
  626. }
  627. static void skl_power_well_enable(struct drm_i915_private *dev_priv,
  628. struct i915_power_well *power_well)
  629. {
  630. skl_set_power_well(dev_priv, power_well, true);
  631. }
  632. static void skl_power_well_disable(struct drm_i915_private *dev_priv,
  633. struct i915_power_well *power_well)
  634. {
  635. skl_set_power_well(dev_priv, power_well, false);
  636. }
  637. static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
  638. struct i915_power_well *power_well)
  639. {
  640. }
  641. static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
  642. struct i915_power_well *power_well)
  643. {
  644. return true;
  645. }
  646. static void vlv_set_power_well(struct drm_i915_private *dev_priv,
  647. struct i915_power_well *power_well, bool enable)
  648. {
  649. enum punit_power_well power_well_id = power_well->data;
  650. u32 mask;
  651. u32 state;
  652. u32 ctrl;
  653. mask = PUNIT_PWRGT_MASK(power_well_id);
  654. state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
  655. PUNIT_PWRGT_PWR_GATE(power_well_id);
  656. mutex_lock(&dev_priv->rps.hw_lock);
  657. #define COND \
  658. ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
  659. if (COND)
  660. goto out;
  661. ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
  662. ctrl &= ~mask;
  663. ctrl |= state;
  664. vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
  665. if (wait_for(COND, 100))
  666. DRM_ERROR("timeout setting power well state %08x (%08x)\n",
  667. state,
  668. vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
  669. #undef COND
  670. out:
  671. mutex_unlock(&dev_priv->rps.hw_lock);
  672. }
  673. static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
  674. struct i915_power_well *power_well)
  675. {
  676. vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
  677. }
  678. static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
  679. struct i915_power_well *power_well)
  680. {
  681. vlv_set_power_well(dev_priv, power_well, true);
  682. }
  683. static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
  684. struct i915_power_well *power_well)
  685. {
  686. vlv_set_power_well(dev_priv, power_well, false);
  687. }
  688. static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
  689. struct i915_power_well *power_well)
  690. {
  691. int power_well_id = power_well->data;
  692. bool enabled = false;
  693. u32 mask;
  694. u32 state;
  695. u32 ctrl;
  696. mask = PUNIT_PWRGT_MASK(power_well_id);
  697. ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
  698. mutex_lock(&dev_priv->rps.hw_lock);
  699. state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
  700. /*
  701. * We only ever set the power-on and power-gate states, anything
  702. * else is unexpected.
  703. */
  704. WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
  705. state != PUNIT_PWRGT_PWR_GATE(power_well_id));
  706. if (state == ctrl)
  707. enabled = true;
  708. /*
  709. * A transient state at this point would mean some unexpected party
  710. * is poking at the power controls too.
  711. */
  712. ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
  713. WARN_ON(ctrl != state);
  714. mutex_unlock(&dev_priv->rps.hw_lock);
  715. return enabled;
  716. }
  717. static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
  718. {
  719. spin_lock_irq(&dev_priv->irq_lock);
  720. valleyview_enable_display_irqs(dev_priv);
  721. spin_unlock_irq(&dev_priv->irq_lock);
  722. /*
  723. * During driver initialization/resume we can avoid restoring the
  724. * part of the HW/SW state that will be inited anyway explicitly.
  725. */
  726. if (dev_priv->power_domains.initializing)
  727. return;
  728. intel_hpd_init(dev_priv);
  729. i915_redisable_vga_power_on(dev_priv->dev);
  730. }
  731. static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
  732. {
  733. spin_lock_irq(&dev_priv->irq_lock);
  734. valleyview_disable_display_irqs(dev_priv);
  735. spin_unlock_irq(&dev_priv->irq_lock);
  736. vlv_power_sequencer_reset(dev_priv);
  737. }
  738. static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
  739. struct i915_power_well *power_well)
  740. {
  741. WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
  742. vlv_set_power_well(dev_priv, power_well, true);
  743. vlv_display_power_well_init(dev_priv);
  744. }
  745. static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
  746. struct i915_power_well *power_well)
  747. {
  748. WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
  749. vlv_display_power_well_deinit(dev_priv);
  750. vlv_set_power_well(dev_priv, power_well, false);
  751. }
  752. static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
  753. struct i915_power_well *power_well)
  754. {
  755. WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
  756. /*
  757. * Enable the CRI clock source so we can get at the
  758. * display and the reference clock for VGA
  759. * hotplug / manual detection.
  760. */
  761. I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) | DPLL_VGA_MODE_DIS |
  762. DPLL_REF_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
  763. udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
  764. vlv_set_power_well(dev_priv, power_well, true);
  765. /*
  766. * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
  767. * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
  768. * a. GUnit 0x2110 bit[0] set to 1 (def 0)
  769. * b. The other bits such as sfr settings / modesel may all
  770. * be set to 0.
  771. *
  772. * This should only be done on init and resume from S3 with
  773. * both PLLs disabled, or we risk losing DPIO and PLL
  774. * synchronization.
  775. */
  776. I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
  777. }
  778. static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
  779. struct i915_power_well *power_well)
  780. {
  781. enum pipe pipe;
  782. WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
  783. for_each_pipe(dev_priv, pipe)
  784. assert_pll_disabled(dev_priv, pipe);
  785. /* Assert common reset */
  786. I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
  787. vlv_set_power_well(dev_priv, power_well, false);
  788. }
  789. static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
  790. struct i915_power_well *power_well)
  791. {
  792. enum dpio_phy phy;
  793. WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
  794. power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
  795. /*
  796. * Enable the CRI clock source so we can get at the
  797. * display and the reference clock for VGA
  798. * hotplug / manual detection.
  799. */
  800. if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
  801. phy = DPIO_PHY0;
  802. I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) | DPLL_VGA_MODE_DIS |
  803. DPLL_REF_CLK_ENABLE_VLV);
  804. I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) | DPLL_VGA_MODE_DIS |
  805. DPLL_REF_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
  806. } else {
  807. phy = DPIO_PHY1;
  808. I915_WRITE(DPLL(PIPE_C), I915_READ(DPLL(PIPE_C)) | DPLL_VGA_MODE_DIS |
  809. DPLL_REF_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
  810. }
  811. udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
  812. vlv_set_power_well(dev_priv, power_well, true);
  813. /* Poll for phypwrgood signal */
  814. if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
  815. DRM_ERROR("Display PHY %d is not power up\n", phy);
  816. dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
  817. I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
  818. }
  819. static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
  820. struct i915_power_well *power_well)
  821. {
  822. enum dpio_phy phy;
  823. WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
  824. power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
  825. if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
  826. phy = DPIO_PHY0;
  827. assert_pll_disabled(dev_priv, PIPE_A);
  828. assert_pll_disabled(dev_priv, PIPE_B);
  829. } else {
  830. phy = DPIO_PHY1;
  831. assert_pll_disabled(dev_priv, PIPE_C);
  832. }
  833. dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
  834. I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
  835. vlv_set_power_well(dev_priv, power_well, false);
  836. }
  837. static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
  838. struct i915_power_well *power_well)
  839. {
  840. enum pipe pipe = power_well->data;
  841. bool enabled;
  842. u32 state, ctrl;
  843. mutex_lock(&dev_priv->rps.hw_lock);
  844. state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
  845. /*
  846. * We only ever set the power-on and power-gate states, anything
  847. * else is unexpected.
  848. */
  849. WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
  850. enabled = state == DP_SSS_PWR_ON(pipe);
  851. /*
  852. * A transient state at this point would mean some unexpected party
  853. * is poking at the power controls too.
  854. */
  855. ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
  856. WARN_ON(ctrl << 16 != state);
  857. mutex_unlock(&dev_priv->rps.hw_lock);
  858. return enabled;
  859. }
  860. static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
  861. struct i915_power_well *power_well,
  862. bool enable)
  863. {
  864. enum pipe pipe = power_well->data;
  865. u32 state;
  866. u32 ctrl;
  867. state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
  868. mutex_lock(&dev_priv->rps.hw_lock);
  869. #define COND \
  870. ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
  871. if (COND)
  872. goto out;
  873. ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  874. ctrl &= ~DP_SSC_MASK(pipe);
  875. ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
  876. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
  877. if (wait_for(COND, 100))
  878. DRM_ERROR("timeout setting power well state %08x (%08x)\n",
  879. state,
  880. vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
  881. #undef COND
  882. out:
  883. mutex_unlock(&dev_priv->rps.hw_lock);
  884. }
  885. static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
  886. struct i915_power_well *power_well)
  887. {
  888. WARN_ON_ONCE(power_well->data != PIPE_A);
  889. chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
  890. }
  891. static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
  892. struct i915_power_well *power_well)
  893. {
  894. WARN_ON_ONCE(power_well->data != PIPE_A);
  895. chv_set_pipe_power_well(dev_priv, power_well, true);
  896. vlv_display_power_well_init(dev_priv);
  897. }
  898. static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
  899. struct i915_power_well *power_well)
  900. {
  901. WARN_ON_ONCE(power_well->data != PIPE_A);
  902. vlv_display_power_well_deinit(dev_priv);
  903. chv_set_pipe_power_well(dev_priv, power_well, false);
  904. }
  905. /**
  906. * intel_display_power_get - grab a power domain reference
  907. * @dev_priv: i915 device instance
  908. * @domain: power domain to reference
  909. *
  910. * This function grabs a power domain reference for @domain and ensures that the
  911. * power domain and all its parents are powered up. Therefore users should only
  912. * grab a reference to the innermost power domain they need.
  913. *
  914. * Any power domain reference obtained by this function must have a symmetric
  915. * call to intel_display_power_put() to release the reference again.
  916. */
  917. void intel_display_power_get(struct drm_i915_private *dev_priv,
  918. enum intel_display_power_domain domain)
  919. {
  920. struct i915_power_domains *power_domains;
  921. struct i915_power_well *power_well;
  922. int i;
  923. intel_runtime_pm_get(dev_priv);
  924. power_domains = &dev_priv->power_domains;
  925. mutex_lock(&power_domains->lock);
  926. for_each_power_well(i, power_well, BIT(domain), power_domains) {
  927. if (!power_well->count++) {
  928. DRM_DEBUG_KMS("enabling %s\n", power_well->name);
  929. power_well->ops->enable(dev_priv, power_well);
  930. power_well->hw_enabled = true;
  931. }
  932. }
  933. power_domains->domain_use_count[domain]++;
  934. mutex_unlock(&power_domains->lock);
  935. }
  936. /**
  937. * intel_display_power_put - release a power domain reference
  938. * @dev_priv: i915 device instance
  939. * @domain: power domain to reference
  940. *
  941. * This function drops the power domain reference obtained by
  942. * intel_display_power_get() and might power down the corresponding hardware
  943. * block right away if this is the last reference.
  944. */
  945. void intel_display_power_put(struct drm_i915_private *dev_priv,
  946. enum intel_display_power_domain domain)
  947. {
  948. struct i915_power_domains *power_domains;
  949. struct i915_power_well *power_well;
  950. int i;
  951. power_domains = &dev_priv->power_domains;
  952. mutex_lock(&power_domains->lock);
  953. WARN_ON(!power_domains->domain_use_count[domain]);
  954. power_domains->domain_use_count[domain]--;
  955. for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
  956. WARN_ON(!power_well->count);
  957. if (!--power_well->count && i915.disable_power_well) {
  958. DRM_DEBUG_KMS("disabling %s\n", power_well->name);
  959. power_well->hw_enabled = false;
  960. power_well->ops->disable(dev_priv, power_well);
  961. }
  962. }
  963. mutex_unlock(&power_domains->lock);
  964. intel_runtime_pm_put(dev_priv);
  965. }
  966. #define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
  967. #define HSW_ALWAYS_ON_POWER_DOMAINS ( \
  968. BIT(POWER_DOMAIN_PIPE_A) | \
  969. BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
  970. BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
  971. BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
  972. BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
  973. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  974. BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
  975. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  976. BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
  977. BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
  978. BIT(POWER_DOMAIN_PORT_CRT) | \
  979. BIT(POWER_DOMAIN_PLLS) | \
  980. BIT(POWER_DOMAIN_AUX_A) | \
  981. BIT(POWER_DOMAIN_AUX_B) | \
  982. BIT(POWER_DOMAIN_AUX_C) | \
  983. BIT(POWER_DOMAIN_AUX_D) | \
  984. BIT(POWER_DOMAIN_INIT))
  985. #define HSW_DISPLAY_POWER_DOMAINS ( \
  986. (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
  987. BIT(POWER_DOMAIN_INIT))
  988. #define BDW_ALWAYS_ON_POWER_DOMAINS ( \
  989. HSW_ALWAYS_ON_POWER_DOMAINS | \
  990. BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
  991. #define BDW_DISPLAY_POWER_DOMAINS ( \
  992. (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
  993. BIT(POWER_DOMAIN_INIT))
  994. #define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
  995. #define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
  996. #define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
  997. BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
  998. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  999. BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
  1000. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  1001. BIT(POWER_DOMAIN_PORT_CRT) | \
  1002. BIT(POWER_DOMAIN_AUX_B) | \
  1003. BIT(POWER_DOMAIN_AUX_C) | \
  1004. BIT(POWER_DOMAIN_INIT))
  1005. #define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
  1006. BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
  1007. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  1008. BIT(POWER_DOMAIN_AUX_B) | \
  1009. BIT(POWER_DOMAIN_INIT))
  1010. #define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
  1011. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  1012. BIT(POWER_DOMAIN_AUX_B) | \
  1013. BIT(POWER_DOMAIN_INIT))
  1014. #define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
  1015. BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
  1016. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  1017. BIT(POWER_DOMAIN_AUX_C) | \
  1018. BIT(POWER_DOMAIN_INIT))
  1019. #define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
  1020. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  1021. BIT(POWER_DOMAIN_AUX_C) | \
  1022. BIT(POWER_DOMAIN_INIT))
  1023. #define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
  1024. BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
  1025. BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
  1026. BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
  1027. BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
  1028. BIT(POWER_DOMAIN_AUX_B) | \
  1029. BIT(POWER_DOMAIN_AUX_C) | \
  1030. BIT(POWER_DOMAIN_INIT))
  1031. #define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
  1032. BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
  1033. BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
  1034. BIT(POWER_DOMAIN_AUX_D) | \
  1035. BIT(POWER_DOMAIN_INIT))
  1036. static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
  1037. .sync_hw = i9xx_always_on_power_well_noop,
  1038. .enable = i9xx_always_on_power_well_noop,
  1039. .disable = i9xx_always_on_power_well_noop,
  1040. .is_enabled = i9xx_always_on_power_well_enabled,
  1041. };
  1042. static const struct i915_power_well_ops chv_pipe_power_well_ops = {
  1043. .sync_hw = chv_pipe_power_well_sync_hw,
  1044. .enable = chv_pipe_power_well_enable,
  1045. .disable = chv_pipe_power_well_disable,
  1046. .is_enabled = chv_pipe_power_well_enabled,
  1047. };
  1048. static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
  1049. .sync_hw = vlv_power_well_sync_hw,
  1050. .enable = chv_dpio_cmn_power_well_enable,
  1051. .disable = chv_dpio_cmn_power_well_disable,
  1052. .is_enabled = vlv_power_well_enabled,
  1053. };
  1054. static struct i915_power_well i9xx_always_on_power_well[] = {
  1055. {
  1056. .name = "always-on",
  1057. .always_on = 1,
  1058. .domains = POWER_DOMAIN_MASK,
  1059. .ops = &i9xx_always_on_power_well_ops,
  1060. },
  1061. };
  1062. static const struct i915_power_well_ops hsw_power_well_ops = {
  1063. .sync_hw = hsw_power_well_sync_hw,
  1064. .enable = hsw_power_well_enable,
  1065. .disable = hsw_power_well_disable,
  1066. .is_enabled = hsw_power_well_enabled,
  1067. };
  1068. static const struct i915_power_well_ops skl_power_well_ops = {
  1069. .sync_hw = skl_power_well_sync_hw,
  1070. .enable = skl_power_well_enable,
  1071. .disable = skl_power_well_disable,
  1072. .is_enabled = skl_power_well_enabled,
  1073. };
  1074. static struct i915_power_well hsw_power_wells[] = {
  1075. {
  1076. .name = "always-on",
  1077. .always_on = 1,
  1078. .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
  1079. .ops = &i9xx_always_on_power_well_ops,
  1080. },
  1081. {
  1082. .name = "display",
  1083. .domains = HSW_DISPLAY_POWER_DOMAINS,
  1084. .ops = &hsw_power_well_ops,
  1085. },
  1086. };
  1087. static struct i915_power_well bdw_power_wells[] = {
  1088. {
  1089. .name = "always-on",
  1090. .always_on = 1,
  1091. .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
  1092. .ops = &i9xx_always_on_power_well_ops,
  1093. },
  1094. {
  1095. .name = "display",
  1096. .domains = BDW_DISPLAY_POWER_DOMAINS,
  1097. .ops = &hsw_power_well_ops,
  1098. },
  1099. };
  1100. static const struct i915_power_well_ops vlv_display_power_well_ops = {
  1101. .sync_hw = vlv_power_well_sync_hw,
  1102. .enable = vlv_display_power_well_enable,
  1103. .disable = vlv_display_power_well_disable,
  1104. .is_enabled = vlv_power_well_enabled,
  1105. };
  1106. static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
  1107. .sync_hw = vlv_power_well_sync_hw,
  1108. .enable = vlv_dpio_cmn_power_well_enable,
  1109. .disable = vlv_dpio_cmn_power_well_disable,
  1110. .is_enabled = vlv_power_well_enabled,
  1111. };
  1112. static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
  1113. .sync_hw = vlv_power_well_sync_hw,
  1114. .enable = vlv_power_well_enable,
  1115. .disable = vlv_power_well_disable,
  1116. .is_enabled = vlv_power_well_enabled,
  1117. };
  1118. static struct i915_power_well vlv_power_wells[] = {
  1119. {
  1120. .name = "always-on",
  1121. .always_on = 1,
  1122. .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
  1123. .ops = &i9xx_always_on_power_well_ops,
  1124. },
  1125. {
  1126. .name = "display",
  1127. .domains = VLV_DISPLAY_POWER_DOMAINS,
  1128. .data = PUNIT_POWER_WELL_DISP2D,
  1129. .ops = &vlv_display_power_well_ops,
  1130. },
  1131. {
  1132. .name = "dpio-tx-b-01",
  1133. .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
  1134. VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
  1135. VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
  1136. VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
  1137. .ops = &vlv_dpio_power_well_ops,
  1138. .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
  1139. },
  1140. {
  1141. .name = "dpio-tx-b-23",
  1142. .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
  1143. VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
  1144. VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
  1145. VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
  1146. .ops = &vlv_dpio_power_well_ops,
  1147. .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
  1148. },
  1149. {
  1150. .name = "dpio-tx-c-01",
  1151. .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
  1152. VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
  1153. VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
  1154. VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
  1155. .ops = &vlv_dpio_power_well_ops,
  1156. .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
  1157. },
  1158. {
  1159. .name = "dpio-tx-c-23",
  1160. .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
  1161. VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
  1162. VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
  1163. VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
  1164. .ops = &vlv_dpio_power_well_ops,
  1165. .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
  1166. },
  1167. {
  1168. .name = "dpio-common",
  1169. .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
  1170. .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
  1171. .ops = &vlv_dpio_cmn_power_well_ops,
  1172. },
  1173. };
  1174. static struct i915_power_well chv_power_wells[] = {
  1175. {
  1176. .name = "always-on",
  1177. .always_on = 1,
  1178. .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
  1179. .ops = &i9xx_always_on_power_well_ops,
  1180. },
  1181. {
  1182. .name = "display",
  1183. /*
  1184. * Pipe A power well is the new disp2d well. Pipe B and C
  1185. * power wells don't actually exist. Pipe A power well is
  1186. * required for any pipe to work.
  1187. */
  1188. .domains = VLV_DISPLAY_POWER_DOMAINS,
  1189. .data = PIPE_A,
  1190. .ops = &chv_pipe_power_well_ops,
  1191. },
  1192. {
  1193. .name = "dpio-common-bc",
  1194. .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
  1195. .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
  1196. .ops = &chv_dpio_cmn_power_well_ops,
  1197. },
  1198. {
  1199. .name = "dpio-common-d",
  1200. .domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
  1201. .data = PUNIT_POWER_WELL_DPIO_CMN_D,
  1202. .ops = &chv_dpio_cmn_power_well_ops,
  1203. },
  1204. };
  1205. static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
  1206. int power_well_id)
  1207. {
  1208. struct i915_power_domains *power_domains = &dev_priv->power_domains;
  1209. struct i915_power_well *power_well;
  1210. int i;
  1211. for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
  1212. if (power_well->data == power_well_id)
  1213. return power_well;
  1214. }
  1215. return NULL;
  1216. }
  1217. bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
  1218. int power_well_id)
  1219. {
  1220. struct i915_power_well *power_well;
  1221. bool ret;
  1222. power_well = lookup_power_well(dev_priv, power_well_id);
  1223. ret = power_well->ops->is_enabled(dev_priv, power_well);
  1224. return ret;
  1225. }
  1226. static struct i915_power_well skl_power_wells[] = {
  1227. {
  1228. .name = "always-on",
  1229. .always_on = 1,
  1230. .domains = SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
  1231. .ops = &i9xx_always_on_power_well_ops,
  1232. },
  1233. {
  1234. .name = "power well 1",
  1235. .domains = SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS,
  1236. .ops = &skl_power_well_ops,
  1237. .data = SKL_DISP_PW_1,
  1238. },
  1239. {
  1240. .name = "MISC IO power well",
  1241. .domains = SKL_DISPLAY_MISC_IO_POWER_DOMAINS,
  1242. .ops = &skl_power_well_ops,
  1243. .data = SKL_DISP_PW_MISC_IO,
  1244. },
  1245. {
  1246. .name = "power well 2",
  1247. .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
  1248. .ops = &skl_power_well_ops,
  1249. .data = SKL_DISP_PW_2,
  1250. },
  1251. {
  1252. .name = "DDI A/E power well",
  1253. .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
  1254. .ops = &skl_power_well_ops,
  1255. .data = SKL_DISP_PW_DDI_A_E,
  1256. },
  1257. {
  1258. .name = "DDI B power well",
  1259. .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
  1260. .ops = &skl_power_well_ops,
  1261. .data = SKL_DISP_PW_DDI_B,
  1262. },
  1263. {
  1264. .name = "DDI C power well",
  1265. .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
  1266. .ops = &skl_power_well_ops,
  1267. .data = SKL_DISP_PW_DDI_C,
  1268. },
  1269. {
  1270. .name = "DDI D power well",
  1271. .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
  1272. .ops = &skl_power_well_ops,
  1273. .data = SKL_DISP_PW_DDI_D,
  1274. },
  1275. };
  1276. static struct i915_power_well bxt_power_wells[] = {
  1277. {
  1278. .name = "always-on",
  1279. .always_on = 1,
  1280. .domains = BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
  1281. .ops = &i9xx_always_on_power_well_ops,
  1282. },
  1283. {
  1284. .name = "power well 1",
  1285. .domains = BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS,
  1286. .ops = &skl_power_well_ops,
  1287. .data = SKL_DISP_PW_1,
  1288. },
  1289. {
  1290. .name = "power well 2",
  1291. .domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
  1292. .ops = &skl_power_well_ops,
  1293. .data = SKL_DISP_PW_2,
  1294. }
  1295. };
  1296. #define set_power_wells(power_domains, __power_wells) ({ \
  1297. (power_domains)->power_wells = (__power_wells); \
  1298. (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
  1299. })
  1300. /**
  1301. * intel_power_domains_init - initializes the power domain structures
  1302. * @dev_priv: i915 device instance
  1303. *
  1304. * Initializes the power domain structures for @dev_priv depending upon the
  1305. * supported platform.
  1306. */
  1307. int intel_power_domains_init(struct drm_i915_private *dev_priv)
  1308. {
  1309. struct i915_power_domains *power_domains = &dev_priv->power_domains;
  1310. mutex_init(&power_domains->lock);
  1311. /*
  1312. * The enabling order will be from lower to higher indexed wells,
  1313. * the disabling order is reversed.
  1314. */
  1315. if (IS_HASWELL(dev_priv->dev)) {
  1316. set_power_wells(power_domains, hsw_power_wells);
  1317. } else if (IS_BROADWELL(dev_priv->dev)) {
  1318. set_power_wells(power_domains, bdw_power_wells);
  1319. } else if (IS_SKYLAKE(dev_priv->dev)) {
  1320. set_power_wells(power_domains, skl_power_wells);
  1321. } else if (IS_BROXTON(dev_priv->dev)) {
  1322. set_power_wells(power_domains, bxt_power_wells);
  1323. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1324. set_power_wells(power_domains, chv_power_wells);
  1325. } else if (IS_VALLEYVIEW(dev_priv->dev)) {
  1326. set_power_wells(power_domains, vlv_power_wells);
  1327. } else {
  1328. set_power_wells(power_domains, i9xx_always_on_power_well);
  1329. }
  1330. return 0;
  1331. }
  1332. static void intel_runtime_pm_disable(struct drm_i915_private *dev_priv)
  1333. {
  1334. struct drm_device *dev = dev_priv->dev;
  1335. struct device *device = &dev->pdev->dev;
  1336. if (!HAS_RUNTIME_PM(dev))
  1337. return;
  1338. if (!intel_enable_rc6(dev))
  1339. return;
  1340. /* Make sure we're not suspended first. */
  1341. pm_runtime_get_sync(device);
  1342. pm_runtime_disable(device);
  1343. }
  1344. /**
  1345. * intel_power_domains_fini - finalizes the power domain structures
  1346. * @dev_priv: i915 device instance
  1347. *
  1348. * Finalizes the power domain structures for @dev_priv depending upon the
  1349. * supported platform. This function also disables runtime pm and ensures that
  1350. * the device stays powered up so that the driver can be reloaded.
  1351. */
  1352. void intel_power_domains_fini(struct drm_i915_private *dev_priv)
  1353. {
  1354. intel_runtime_pm_disable(dev_priv);
  1355. /* The i915.ko module is still not prepared to be loaded when
  1356. * the power well is not enabled, so just enable it in case
  1357. * we're going to unload/reload. */
  1358. intel_display_set_init_power(dev_priv, true);
  1359. }
  1360. static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
  1361. {
  1362. struct i915_power_domains *power_domains = &dev_priv->power_domains;
  1363. struct i915_power_well *power_well;
  1364. int i;
  1365. mutex_lock(&power_domains->lock);
  1366. for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
  1367. power_well->ops->sync_hw(dev_priv, power_well);
  1368. power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
  1369. power_well);
  1370. }
  1371. mutex_unlock(&power_domains->lock);
  1372. }
  1373. static void chv_phy_control_init(struct drm_i915_private *dev_priv)
  1374. {
  1375. struct i915_power_well *cmn_bc =
  1376. lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
  1377. struct i915_power_well *cmn_d =
  1378. lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
  1379. /*
  1380. * DISPLAY_PHY_CONTROL can get corrupted if read. As a
  1381. * workaround never ever read DISPLAY_PHY_CONTROL, and
  1382. * instead maintain a shadow copy ourselves. Use the actual
  1383. * power well state to reconstruct the expected initial
  1384. * value.
  1385. */
  1386. dev_priv->chv_phy_control =
  1387. PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
  1388. PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
  1389. PHY_CH_POWER_MODE(PHY_CH_SU_PSR, DPIO_PHY0, DPIO_CH0) |
  1390. PHY_CH_POWER_MODE(PHY_CH_SU_PSR, DPIO_PHY0, DPIO_CH1) |
  1391. PHY_CH_POWER_MODE(PHY_CH_SU_PSR, DPIO_PHY1, DPIO_CH0);
  1392. if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc))
  1393. dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
  1394. if (cmn_d->ops->is_enabled(dev_priv, cmn_d))
  1395. dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
  1396. }
  1397. static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
  1398. {
  1399. struct i915_power_well *cmn =
  1400. lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
  1401. struct i915_power_well *disp2d =
  1402. lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
  1403. /* If the display might be already active skip this */
  1404. if (cmn->ops->is_enabled(dev_priv, cmn) &&
  1405. disp2d->ops->is_enabled(dev_priv, disp2d) &&
  1406. I915_READ(DPIO_CTL) & DPIO_CMNRST)
  1407. return;
  1408. DRM_DEBUG_KMS("toggling display PHY side reset\n");
  1409. /* cmnlane needs DPLL registers */
  1410. disp2d->ops->enable(dev_priv, disp2d);
  1411. /*
  1412. * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
  1413. * Need to assert and de-assert PHY SB reset by gating the
  1414. * common lane power, then un-gating it.
  1415. * Simply ungating isn't enough to reset the PHY enough to get
  1416. * ports and lanes running.
  1417. */
  1418. cmn->ops->disable(dev_priv, cmn);
  1419. }
  1420. /**
  1421. * intel_power_domains_init_hw - initialize hardware power domain state
  1422. * @dev_priv: i915 device instance
  1423. *
  1424. * This function initializes the hardware power domain state and enables all
  1425. * power domains using intel_display_set_init_power().
  1426. */
  1427. void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
  1428. {
  1429. struct drm_device *dev = dev_priv->dev;
  1430. struct i915_power_domains *power_domains = &dev_priv->power_domains;
  1431. power_domains->initializing = true;
  1432. if (IS_CHERRYVIEW(dev)) {
  1433. chv_phy_control_init(dev_priv);
  1434. } else if (IS_VALLEYVIEW(dev)) {
  1435. mutex_lock(&power_domains->lock);
  1436. vlv_cmnlane_wa(dev_priv);
  1437. mutex_unlock(&power_domains->lock);
  1438. }
  1439. /* For now, we need the power well to be always enabled. */
  1440. intel_display_set_init_power(dev_priv, true);
  1441. intel_power_domains_resume(dev_priv);
  1442. power_domains->initializing = false;
  1443. }
  1444. /**
  1445. * intel_aux_display_runtime_get - grab an auxiliary power domain reference
  1446. * @dev_priv: i915 device instance
  1447. *
  1448. * This function grabs a power domain reference for the auxiliary power domain
  1449. * (for access to the GMBUS and DP AUX blocks) and ensures that it and all its
  1450. * parents are powered up. Therefore users should only grab a reference to the
  1451. * innermost power domain they need.
  1452. *
  1453. * Any power domain reference obtained by this function must have a symmetric
  1454. * call to intel_aux_display_runtime_put() to release the reference again.
  1455. */
  1456. void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
  1457. {
  1458. intel_runtime_pm_get(dev_priv);
  1459. }
  1460. /**
  1461. * intel_aux_display_runtime_put - release an auxiliary power domain reference
  1462. * @dev_priv: i915 device instance
  1463. *
  1464. * This function drops the auxiliary power domain reference obtained by
  1465. * intel_aux_display_runtime_get() and might power down the corresponding
  1466. * hardware block right away if this is the last reference.
  1467. */
  1468. void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
  1469. {
  1470. intel_runtime_pm_put(dev_priv);
  1471. }
  1472. /**
  1473. * intel_runtime_pm_get - grab a runtime pm reference
  1474. * @dev_priv: i915 device instance
  1475. *
  1476. * This function grabs a device-level runtime pm reference (mostly used for GEM
  1477. * code to ensure the GTT or GT is on) and ensures that it is powered up.
  1478. *
  1479. * Any runtime pm reference obtained by this function must have a symmetric
  1480. * call to intel_runtime_pm_put() to release the reference again.
  1481. */
  1482. void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
  1483. {
  1484. struct drm_device *dev = dev_priv->dev;
  1485. struct device *device = &dev->pdev->dev;
  1486. if (!HAS_RUNTIME_PM(dev))
  1487. return;
  1488. pm_runtime_get_sync(device);
  1489. WARN(dev_priv->pm.suspended, "Device still suspended.\n");
  1490. }
  1491. /**
  1492. * intel_runtime_pm_get_noresume - grab a runtime pm reference
  1493. * @dev_priv: i915 device instance
  1494. *
  1495. * This function grabs a device-level runtime pm reference (mostly used for GEM
  1496. * code to ensure the GTT or GT is on).
  1497. *
  1498. * It will _not_ power up the device but instead only check that it's powered
  1499. * on. Therefore it is only valid to call this functions from contexts where
  1500. * the device is known to be powered up and where trying to power it up would
  1501. * result in hilarity and deadlocks. That pretty much means only the system
  1502. * suspend/resume code where this is used to grab runtime pm references for
  1503. * delayed setup down in work items.
  1504. *
  1505. * Any runtime pm reference obtained by this function must have a symmetric
  1506. * call to intel_runtime_pm_put() to release the reference again.
  1507. */
  1508. void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
  1509. {
  1510. struct drm_device *dev = dev_priv->dev;
  1511. struct device *device = &dev->pdev->dev;
  1512. if (!HAS_RUNTIME_PM(dev))
  1513. return;
  1514. WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
  1515. pm_runtime_get_noresume(device);
  1516. }
  1517. /**
  1518. * intel_runtime_pm_put - release a runtime pm reference
  1519. * @dev_priv: i915 device instance
  1520. *
  1521. * This function drops the device-level runtime pm reference obtained by
  1522. * intel_runtime_pm_get() and might power down the corresponding
  1523. * hardware block right away if this is the last reference.
  1524. */
  1525. void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
  1526. {
  1527. struct drm_device *dev = dev_priv->dev;
  1528. struct device *device = &dev->pdev->dev;
  1529. if (!HAS_RUNTIME_PM(dev))
  1530. return;
  1531. pm_runtime_mark_last_busy(device);
  1532. pm_runtime_put_autosuspend(device);
  1533. }
  1534. /**
  1535. * intel_runtime_pm_enable - enable runtime pm
  1536. * @dev_priv: i915 device instance
  1537. *
  1538. * This function enables runtime pm at the end of the driver load sequence.
  1539. *
  1540. * Note that this function does currently not enable runtime pm for the
  1541. * subordinate display power domains. That is only done on the first modeset
  1542. * using intel_display_set_init_power().
  1543. */
  1544. void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
  1545. {
  1546. struct drm_device *dev = dev_priv->dev;
  1547. struct device *device = &dev->pdev->dev;
  1548. if (!HAS_RUNTIME_PM(dev))
  1549. return;
  1550. pm_runtime_set_active(device);
  1551. /*
  1552. * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
  1553. * requirement.
  1554. */
  1555. if (!intel_enable_rc6(dev)) {
  1556. DRM_INFO("RC6 disabled, disabling runtime PM support\n");
  1557. return;
  1558. }
  1559. pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
  1560. pm_runtime_mark_last_busy(device);
  1561. pm_runtime_use_autosuspend(device);
  1562. pm_runtime_put_autosuspend(device);
  1563. }