vlv_suspend.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2020 Intel Corporation
  4. */
  5. #include <linux/string_helpers.h>
  6. #include <linux/kernel.h>
  7. #include <drm/drm_print.h>
  8. #include "gt/intel_gt_regs.h"
  9. #include "i915_drv.h"
  10. #include "i915_reg.h"
  11. #include "i915_trace.h"
  12. #include "i915_utils.h"
  13. #include "i915_wait_util.h"
  14. #include "intel_clock_gating.h"
  15. #include "intel_uncore_trace.h"
  16. #include "vlv_suspend.h"
  17. struct vlv_s0ix_state {
  18. /* GAM */
  19. u32 wr_watermark;
  20. u32 gfx_prio_ctrl;
  21. u32 arb_mode;
  22. u32 gfx_pend_tlb0;
  23. u32 gfx_pend_tlb1;
  24. u32 lra_limits[GEN7_LRA_LIMITS_REG_NUM];
  25. u32 media_max_req_count;
  26. u32 gfx_max_req_count;
  27. u32 render_hwsp;
  28. u32 ecochk;
  29. u32 bsd_hwsp;
  30. u32 blt_hwsp;
  31. u32 tlb_rd_addr;
  32. /* MBC */
  33. u32 g3dctl;
  34. u32 gsckgctl;
  35. u32 mbctl;
  36. /* GCP */
  37. u32 ucgctl1;
  38. u32 ucgctl3;
  39. u32 rcgctl1;
  40. u32 rcgctl2;
  41. u32 rstctl;
  42. u32 misccpctl;
  43. /* GPM */
  44. u32 gfxpause;
  45. u32 rpdeuhwtc;
  46. u32 rpdeuc;
  47. u32 ecobus;
  48. u32 pwrdwnupctl;
  49. u32 rp_down_timeout;
  50. u32 rp_deucsw;
  51. u32 rcubmabdtmr;
  52. u32 rcedata;
  53. u32 spare2gh;
  54. /* Display 1 CZ domain */
  55. u32 gt_imr;
  56. u32 gt_ier;
  57. u32 pm_imr;
  58. u32 pm_ier;
  59. u32 gt_scratch[GEN7_GT_SCRATCH_REG_NUM];
  60. /* GT SA CZ domain */
  61. u32 tilectl;
  62. u32 gt_fifoctl;
  63. u32 gtlc_wake_ctrl;
  64. u32 gtlc_survive;
  65. u32 pmwgicz;
  66. /* Display 2 CZ domain */
  67. u32 gu_ctl0;
  68. u32 gu_ctl1;
  69. u32 pcbr;
  70. u32 clock_gate_dis2;
  71. };
  72. /*
  73. * Save all Gunit registers that may be lost after a D3 and a subsequent
  74. * S0i[R123] transition. The list of registers needing a save/restore is
  75. * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
  76. * registers in the following way:
  77. * - Driver: saved/restored by the driver
  78. * - Punit : saved/restored by the Punit firmware
  79. * - No, w/o marking: no need to save/restore, since the register is R/O or
  80. * used internally by the HW in a way that doesn't depend
  81. * keeping the content across a suspend/resume.
  82. * - Debug : used for debugging
  83. *
  84. * We save/restore all registers marked with 'Driver', with the following
  85. * exceptions:
  86. * - Registers out of use, including also registers marked with 'Debug'.
  87. * These have no effect on the driver's operation, so we don't save/restore
  88. * them to reduce the overhead.
  89. * - Registers that are fully setup by an initialization function called from
  90. * the resume path. For example many clock gating and RPS/RC6 registers.
  91. * - Registers that provide the right functionality with their reset defaults.
  92. *
  93. * TODO: Except for registers that based on the above 3 criteria can be safely
  94. * ignored, we save/restore all others, practically treating the HW context as
  95. * a black-box for the driver. Further investigation is needed to reduce the
  96. * saved/restored registers even further, by following the same 3 criteria.
  97. */
  98. static void vlv_save_gunit_s0ix_state(struct drm_i915_private *i915)
  99. {
  100. struct vlv_s0ix_state *s = i915->vlv_s0ix_state;
  101. struct intel_uncore *uncore = &i915->uncore;
  102. int i;
  103. if (!s)
  104. return;
  105. /* GAM 0x4000-0x4770 */
  106. s->wr_watermark = intel_uncore_read(uncore, GEN7_WR_WATERMARK);
  107. s->gfx_prio_ctrl = intel_uncore_read(uncore, GEN7_GFX_PRIO_CTRL);
  108. s->arb_mode = intel_uncore_read(uncore, ARB_MODE);
  109. s->gfx_pend_tlb0 = intel_uncore_read(uncore, GEN7_GFX_PEND_TLB0);
  110. s->gfx_pend_tlb1 = intel_uncore_read(uncore, GEN7_GFX_PEND_TLB1);
  111. for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
  112. s->lra_limits[i] = intel_uncore_read(uncore, GEN7_LRA_LIMITS(i));
  113. s->media_max_req_count = intel_uncore_read(uncore, GEN7_MEDIA_MAX_REQ_COUNT);
  114. s->gfx_max_req_count = intel_uncore_read(uncore, GEN7_GFX_MAX_REQ_COUNT);
  115. s->render_hwsp = intel_uncore_read(uncore, RENDER_HWS_PGA_GEN7);
  116. s->ecochk = intel_uncore_read(uncore, GAM_ECOCHK);
  117. s->bsd_hwsp = intel_uncore_read(uncore, BSD_HWS_PGA_GEN7);
  118. s->blt_hwsp = intel_uncore_read(uncore, BLT_HWS_PGA_GEN7);
  119. s->tlb_rd_addr = intel_uncore_read(uncore, GEN7_TLB_RD_ADDR);
  120. /* MBC 0x9024-0x91D0, 0x8500 */
  121. s->g3dctl = intel_uncore_read(uncore, VLV_G3DCTL);
  122. s->gsckgctl = intel_uncore_read(uncore, VLV_GSCKGCTL);
  123. s->mbctl = intel_uncore_read(uncore, GEN6_MBCTL);
  124. /* GCP 0x9400-0x9424, 0x8100-0x810C */
  125. s->ucgctl1 = intel_uncore_read(uncore, GEN6_UCGCTL1);
  126. s->ucgctl3 = intel_uncore_read(uncore, GEN6_UCGCTL3);
  127. s->rcgctl1 = intel_uncore_read(uncore, GEN6_RCGCTL1);
  128. s->rcgctl2 = intel_uncore_read(uncore, GEN6_RCGCTL2);
  129. s->rstctl = intel_uncore_read(uncore, GEN6_RSTCTL);
  130. s->misccpctl = intel_uncore_read(uncore, GEN7_MISCCPCTL);
  131. /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
  132. s->gfxpause = intel_uncore_read(uncore, GEN6_GFXPAUSE);
  133. s->rpdeuhwtc = intel_uncore_read(uncore, GEN6_RPDEUHWTC);
  134. s->rpdeuc = intel_uncore_read(uncore, GEN6_RPDEUC);
  135. s->ecobus = intel_uncore_read(uncore, ECOBUS);
  136. s->pwrdwnupctl = intel_uncore_read(uncore, VLV_PWRDWNUPCTL);
  137. s->rp_down_timeout = intel_uncore_read(uncore, GEN6_RP_DOWN_TIMEOUT);
  138. s->rp_deucsw = intel_uncore_read(uncore, GEN6_RPDEUCSW);
  139. s->rcubmabdtmr = intel_uncore_read(uncore, GEN6_RCUBMABDTMR);
  140. s->rcedata = intel_uncore_read(uncore, VLV_RCEDATA);
  141. s->spare2gh = intel_uncore_read(uncore, VLV_SPAREG2H);
  142. /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
  143. s->gt_imr = intel_uncore_read(uncore, GTIMR);
  144. s->gt_ier = intel_uncore_read(uncore, GTIER);
  145. s->pm_imr = intel_uncore_read(uncore, GEN6_PMIMR);
  146. s->pm_ier = intel_uncore_read(uncore, GEN6_PMIER);
  147. for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
  148. s->gt_scratch[i] = intel_uncore_read(uncore, GEN7_GT_SCRATCH(i));
  149. /* GT SA CZ domain, 0x100000-0x138124 */
  150. s->tilectl = intel_uncore_read(uncore, TILECTL);
  151. s->gt_fifoctl = intel_uncore_read(uncore, GTFIFOCTL);
  152. s->gtlc_wake_ctrl = intel_uncore_read(uncore, VLV_GTLC_WAKE_CTRL);
  153. s->gtlc_survive = intel_uncore_read(uncore, VLV_GTLC_SURVIVABILITY_REG);
  154. s->pmwgicz = intel_uncore_read(uncore, VLV_PMWGICZ);
  155. /* Gunit-Display CZ domain, 0x182028-0x1821CF */
  156. s->gu_ctl0 = intel_uncore_read(uncore, VLV_GU_CTL0);
  157. s->gu_ctl1 = intel_uncore_read(uncore, VLV_GU_CTL1);
  158. s->pcbr = intel_uncore_read(uncore, VLV_PCBR);
  159. s->clock_gate_dis2 = intel_uncore_read(uncore, VLV_GUNIT_CLOCK_GATE2);
  160. /*
  161. * Not saving any of:
  162. * DFT, 0x9800-0x9EC0
  163. * SARB, 0xB000-0xB1FC
  164. * GAC, 0x5208-0x524C, 0x14000-0x14C000
  165. * PCI CFG
  166. */
  167. }
  168. static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *i915)
  169. {
  170. struct vlv_s0ix_state *s = i915->vlv_s0ix_state;
  171. struct intel_uncore *uncore = &i915->uncore;
  172. int i;
  173. if (!s)
  174. return;
  175. /* GAM 0x4000-0x4770 */
  176. intel_uncore_write(uncore, GEN7_WR_WATERMARK, s->wr_watermark);
  177. intel_uncore_write(uncore, GEN7_GFX_PRIO_CTRL, s->gfx_prio_ctrl);
  178. intel_uncore_write(uncore, ARB_MODE, s->arb_mode | (0xffff << 16));
  179. intel_uncore_write(uncore, GEN7_GFX_PEND_TLB0, s->gfx_pend_tlb0);
  180. intel_uncore_write(uncore, GEN7_GFX_PEND_TLB1, s->gfx_pend_tlb1);
  181. for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
  182. intel_uncore_write(uncore, GEN7_LRA_LIMITS(i), s->lra_limits[i]);
  183. intel_uncore_write(uncore, GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
  184. intel_uncore_write(uncore, GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
  185. intel_uncore_write(uncore, RENDER_HWS_PGA_GEN7, s->render_hwsp);
  186. intel_uncore_write(uncore, GAM_ECOCHK, s->ecochk);
  187. intel_uncore_write(uncore, BSD_HWS_PGA_GEN7, s->bsd_hwsp);
  188. intel_uncore_write(uncore, BLT_HWS_PGA_GEN7, s->blt_hwsp);
  189. intel_uncore_write(uncore, GEN7_TLB_RD_ADDR, s->tlb_rd_addr);
  190. /* MBC 0x9024-0x91D0, 0x8500 */
  191. intel_uncore_write(uncore, VLV_G3DCTL, s->g3dctl);
  192. intel_uncore_write(uncore, VLV_GSCKGCTL, s->gsckgctl);
  193. intel_uncore_write(uncore, GEN6_MBCTL, s->mbctl);
  194. /* GCP 0x9400-0x9424, 0x8100-0x810C */
  195. intel_uncore_write(uncore, GEN6_UCGCTL1, s->ucgctl1);
  196. intel_uncore_write(uncore, GEN6_UCGCTL3, s->ucgctl3);
  197. intel_uncore_write(uncore, GEN6_RCGCTL1, s->rcgctl1);
  198. intel_uncore_write(uncore, GEN6_RCGCTL2, s->rcgctl2);
  199. intel_uncore_write(uncore, GEN6_RSTCTL, s->rstctl);
  200. intel_uncore_write(uncore, GEN7_MISCCPCTL, s->misccpctl);
  201. /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
  202. intel_uncore_write(uncore, GEN6_GFXPAUSE, s->gfxpause);
  203. intel_uncore_write(uncore, GEN6_RPDEUHWTC, s->rpdeuhwtc);
  204. intel_uncore_write(uncore, GEN6_RPDEUC, s->rpdeuc);
  205. intel_uncore_write(uncore, ECOBUS, s->ecobus);
  206. intel_uncore_write(uncore, VLV_PWRDWNUPCTL, s->pwrdwnupctl);
  207. intel_uncore_write(uncore, GEN6_RP_DOWN_TIMEOUT, s->rp_down_timeout);
  208. intel_uncore_write(uncore, GEN6_RPDEUCSW, s->rp_deucsw);
  209. intel_uncore_write(uncore, GEN6_RCUBMABDTMR, s->rcubmabdtmr);
  210. intel_uncore_write(uncore, VLV_RCEDATA, s->rcedata);
  211. intel_uncore_write(uncore, VLV_SPAREG2H, s->spare2gh);
  212. /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
  213. intel_uncore_write(uncore, GTIMR, s->gt_imr);
  214. intel_uncore_write(uncore, GTIER, s->gt_ier);
  215. intel_uncore_write(uncore, GEN6_PMIMR, s->pm_imr);
  216. intel_uncore_write(uncore, GEN6_PMIER, s->pm_ier);
  217. for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
  218. intel_uncore_write(uncore, GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
  219. /* GT SA CZ domain, 0x100000-0x138124 */
  220. intel_uncore_write(uncore, TILECTL, s->tilectl);
  221. intel_uncore_write(uncore, GTFIFOCTL, s->gt_fifoctl);
  222. /*
  223. * Preserve the GT allow wake and GFX force clock bit, they are not
  224. * be restored, as they are used to control the s0ix suspend/resume
  225. * sequence by the caller.
  226. */
  227. intel_uncore_rmw(uncore, VLV_GTLC_WAKE_CTRL, ~VLV_GTLC_ALLOWWAKEREQ,
  228. s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ);
  229. intel_uncore_rmw(uncore, VLV_GTLC_SURVIVABILITY_REG, ~VLV_GFX_CLK_FORCE_ON_BIT,
  230. s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT);
  231. intel_uncore_write(uncore, VLV_PMWGICZ, s->pmwgicz);
  232. /* Gunit-Display CZ domain, 0x182028-0x1821CF */
  233. intel_uncore_write(uncore, VLV_GU_CTL0, s->gu_ctl0);
  234. intel_uncore_write(uncore, VLV_GU_CTL1, s->gu_ctl1);
  235. intel_uncore_write(uncore, VLV_PCBR, s->pcbr);
  236. intel_uncore_write(uncore, VLV_GUNIT_CLOCK_GATE2, s->clock_gate_dis2);
  237. }
  238. static int vlv_wait_for_pw_status(struct drm_i915_private *i915,
  239. u32 mask, u32 val)
  240. {
  241. i915_reg_t reg = VLV_GTLC_PW_STATUS;
  242. u32 reg_value;
  243. int ret;
  244. /* The HW does not like us polling for PW_STATUS frequently, so
  245. * use the sleeping loop rather than risk the busy spin within
  246. * intel_wait_for_register().
  247. *
  248. * Transitioning between RC6 states should be at most 2ms (see
  249. * valleyview_enable_rps) so use a 3ms timeout.
  250. */
  251. ret = wait_for(((reg_value =
  252. intel_uncore_read_notrace(&i915->uncore, reg)) & mask)
  253. == val, 3);
  254. /* just trace the final value */
  255. trace_i915_reg_rw(false, reg, reg_value, sizeof(reg_value), true);
  256. return ret;
  257. }
  258. static int vlv_force_gfx_clock(struct drm_i915_private *i915, bool force_on)
  259. {
  260. struct intel_uncore *uncore = &i915->uncore;
  261. int err;
  262. intel_uncore_rmw(uncore, VLV_GTLC_SURVIVABILITY_REG, VLV_GFX_CLK_FORCE_ON_BIT,
  263. force_on ? VLV_GFX_CLK_FORCE_ON_BIT : 0);
  264. if (!force_on)
  265. return 0;
  266. err = intel_wait_for_register(uncore,
  267. VLV_GTLC_SURVIVABILITY_REG,
  268. VLV_GFX_CLK_STATUS_BIT,
  269. VLV_GFX_CLK_STATUS_BIT,
  270. 20);
  271. if (err)
  272. drm_err(&i915->drm,
  273. "timeout waiting for GFX clock force-on (%08x)\n",
  274. intel_uncore_read(uncore, VLV_GTLC_SURVIVABILITY_REG));
  275. return err;
  276. }
  277. static int vlv_allow_gt_wake(struct drm_i915_private *i915, bool allow)
  278. {
  279. struct intel_uncore *uncore = &i915->uncore;
  280. u32 mask;
  281. u32 val;
  282. int err;
  283. intel_uncore_rmw(uncore, VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ,
  284. allow ? VLV_GTLC_ALLOWWAKEREQ : 0);
  285. intel_uncore_posting_read(uncore, VLV_GTLC_WAKE_CTRL);
  286. mask = VLV_GTLC_ALLOWWAKEACK;
  287. val = allow ? mask : 0;
  288. err = vlv_wait_for_pw_status(i915, mask, val);
  289. if (err)
  290. drm_err(&i915->drm, "timeout disabling GT waking\n");
  291. return err;
  292. }
  293. static void vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
  294. bool wait_for_on)
  295. {
  296. u32 mask;
  297. u32 val;
  298. mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
  299. val = wait_for_on ? mask : 0;
  300. /*
  301. * RC6 transitioning can be delayed up to 2 msec (see
  302. * valleyview_enable_rps), use 3 msec for safety.
  303. *
  304. * This can fail to turn off the rc6 if the GPU is stuck after a failed
  305. * reset and we are trying to force the machine to sleep.
  306. */
  307. if (vlv_wait_for_pw_status(dev_priv, mask, val))
  308. drm_dbg(&dev_priv->drm,
  309. "timeout waiting for GT wells to go %s\n",
  310. str_on_off(wait_for_on));
  311. }
  312. static void vlv_check_no_gt_access(struct drm_i915_private *i915)
  313. {
  314. struct intel_uncore *uncore = &i915->uncore;
  315. if (!(intel_uncore_read(uncore, VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
  316. return;
  317. drm_dbg(&i915->drm, "GT register access while GT waking disabled\n");
  318. intel_uncore_write(uncore, VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
  319. }
  320. int vlv_suspend_complete(struct drm_i915_private *dev_priv)
  321. {
  322. u32 mask;
  323. int err;
  324. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
  325. return 0;
  326. /*
  327. * Bspec defines the following GT well on flags as debug only, so
  328. * don't treat them as hard failures.
  329. */
  330. vlv_wait_for_gt_wells(dev_priv, false);
  331. mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
  332. drm_WARN_ON(&dev_priv->drm,
  333. (intel_uncore_read(&dev_priv->uncore, VLV_GTLC_WAKE_CTRL) & mask) != mask);
  334. vlv_check_no_gt_access(dev_priv);
  335. err = vlv_force_gfx_clock(dev_priv, true);
  336. if (err)
  337. goto err1;
  338. err = vlv_allow_gt_wake(dev_priv, false);
  339. if (err)
  340. goto err2;
  341. vlv_save_gunit_s0ix_state(dev_priv);
  342. err = vlv_force_gfx_clock(dev_priv, false);
  343. if (err)
  344. goto err2;
  345. return 0;
  346. err2:
  347. /* For safety always re-enable waking and disable gfx clock forcing */
  348. vlv_allow_gt_wake(dev_priv, true);
  349. err1:
  350. vlv_force_gfx_clock(dev_priv, false);
  351. return err;
  352. }
  353. int vlv_resume_prepare(struct drm_i915_private *dev_priv, bool rpm_resume)
  354. {
  355. int err;
  356. int ret;
  357. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
  358. return 0;
  359. /*
  360. * If any of the steps fail just try to continue, that's the best we
  361. * can do at this point. Return the first error code (which will also
  362. * leave RPM permanently disabled).
  363. */
  364. ret = vlv_force_gfx_clock(dev_priv, true);
  365. vlv_restore_gunit_s0ix_state(dev_priv);
  366. err = vlv_allow_gt_wake(dev_priv, true);
  367. if (!ret)
  368. ret = err;
  369. err = vlv_force_gfx_clock(dev_priv, false);
  370. if (!ret)
  371. ret = err;
  372. vlv_check_no_gt_access(dev_priv);
  373. if (rpm_resume)
  374. intel_clock_gating_init(&dev_priv->drm);
  375. return ret;
  376. }
  377. int vlv_suspend_init(struct drm_i915_private *i915)
  378. {
  379. if (!IS_VALLEYVIEW(i915))
  380. return 0;
  381. /* we write all the values in the struct, so no need to zero it out */
  382. i915->vlv_s0ix_state = kmalloc_obj(*i915->vlv_s0ix_state);
  383. if (!i915->vlv_s0ix_state)
  384. return -ENOMEM;
  385. return 0;
  386. }
  387. void vlv_suspend_cleanup(struct drm_i915_private *i915)
  388. {
  389. if (!i915->vlv_s0ix_state)
  390. return;
  391. kfree(i915->vlv_s0ix_state);
  392. i915->vlv_s0ix_state = NULL;
  393. }