intel_rc6.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #include <linux/pm_runtime.h>
  6. #include <linux/string_helpers.h>
  7. #include <drm/drm_print.h>
  8. #include "display/vlv_clock.h"
  9. #include "gem/i915_gem_region.h"
  10. #include "i915_drv.h"
  11. #include "i915_reg.h"
  12. #include "i915_vgpu.h"
  13. #include "intel_engine_regs.h"
  14. #include "intel_gt.h"
  15. #include "intel_gt_pm.h"
  16. #include "intel_gt_regs.h"
  17. #include "intel_pcode.h"
  18. #include "intel_rc6.h"
  19. /**
  20. * DOC: RC6
  21. *
  22. * RC6 is a special power stage which allows the GPU to enter an very
  23. * low-voltage mode when idle, using down to 0V while at this stage. This
  24. * stage is entered automatically when the GPU is idle when RC6 support is
  25. * enabled, and as soon as new workload arises GPU wakes up automatically as
  26. * well.
  27. *
  28. * There are different RC6 modes available in Intel GPU, which differentiate
  29. * among each other with the latency required to enter and leave RC6 and
  30. * voltage consumed by the GPU in different states.
  31. *
  32. * The combination of the following flags define which states GPU is allowed
  33. * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
  34. * RC6pp is deepest RC6. Their support by hardware varies according to the
  35. * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
  36. * which brings the most power savings; deeper states save more power, but
  37. * require higher latency to switch to and wake up.
  38. */
  39. static struct intel_gt *rc6_to_gt(struct intel_rc6 *rc6)
  40. {
  41. return container_of(rc6, struct intel_gt, rc6);
  42. }
  43. static struct intel_uncore *rc6_to_uncore(struct intel_rc6 *rc)
  44. {
  45. return rc6_to_gt(rc)->uncore;
  46. }
  47. static struct drm_i915_private *rc6_to_i915(struct intel_rc6 *rc)
  48. {
  49. return rc6_to_gt(rc)->i915;
  50. }
  51. static void gen11_rc6_enable(struct intel_rc6 *rc6)
  52. {
  53. struct intel_gt *gt = rc6_to_gt(rc6);
  54. struct intel_uncore *uncore = gt->uncore;
  55. struct intel_engine_cs *engine;
  56. enum intel_engine_id id;
  57. u32 pg_enable;
  58. int i;
  59. /*
  60. * With GuCRC, these parameters are set by GuC
  61. */
  62. if (!intel_uc_uses_guc_rc(&gt->uc)) {
  63. /* 2b: Program RC6 thresholds.*/
  64. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
  65. intel_uncore_write_fw(uncore, GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
  66. intel_uncore_write_fw(uncore, GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  67. intel_uncore_write_fw(uncore, GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  68. for_each_engine(engine, rc6_to_gt(rc6), id)
  69. intel_uncore_write_fw(uncore, RING_MAX_IDLE(engine->mmio_base), 10);
  70. intel_uncore_write_fw(uncore, GUC_MAX_IDLE_COUNT, 0xA);
  71. intel_uncore_write_fw(uncore, GEN6_RC_SLEEP, 0);
  72. intel_uncore_write_fw(uncore, GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
  73. }
  74. /*
  75. * 2c: Program Coarse Power Gating Policies.
  76. *
  77. * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
  78. * use instead is a more conservative estimate for the maximum time
  79. * it takes us to service a CS interrupt and submit a new ELSP - that
  80. * is the time which the GPU is idle waiting for the CPU to select the
  81. * next request to execute. If the idle hysteresis is less than that
  82. * interrupt service latency, the hardware will automatically gate
  83. * the power well and we will then incur the wake up cost on top of
  84. * the service latency. A similar guide from plane_state is that we
  85. * do not want the enable hysteresis to less than the wakeup latency.
  86. *
  87. * igt/gem_exec_nop/sequential provides a rough estimate for the
  88. * service latency, and puts it under 10us for Icelake, similar to
  89. * Broadwell+, To be conservative, we want to factor in a context
  90. * switch on top (due to ksoftirqd).
  91. */
  92. intel_uncore_write_fw(uncore, GEN9_MEDIA_PG_IDLE_HYSTERESIS, 60);
  93. intel_uncore_write_fw(uncore, GEN9_RENDER_PG_IDLE_HYSTERESIS, 60);
  94. /* 3a: Enable RC6
  95. *
  96. * With GuCRC, we do not enable bit 31 of RC_CTL,
  97. * thus allowing GuC to control RC6 entry/exit fully instead.
  98. * We will not set the HW ENABLE and EI bits
  99. */
  100. if (!intel_guc_rc_enable(gt_to_guc(gt)))
  101. rc6->ctl_enable = GEN6_RC_CTL_RC6_ENABLE;
  102. else
  103. rc6->ctl_enable =
  104. GEN6_RC_CTL_HW_ENABLE |
  105. GEN6_RC_CTL_RC6_ENABLE |
  106. GEN6_RC_CTL_EI_MODE(1);
  107. pg_enable =
  108. GEN9_RENDER_PG_ENABLE |
  109. GEN9_MEDIA_PG_ENABLE |
  110. GEN11_MEDIA_SAMPLER_PG_ENABLE;
  111. if (GRAPHICS_VER(gt->i915) >= 12 && !IS_DG1(gt->i915)) {
  112. for (i = 0; i < I915_MAX_VCS; i++)
  113. if (HAS_ENGINE(gt, _VCS(i)))
  114. pg_enable |= (VDN_HCP_POWERGATE_ENABLE(i) |
  115. VDN_MFX_POWERGATE_ENABLE(i));
  116. }
  117. intel_uncore_write_fw(uncore, GEN9_PG_ENABLE, pg_enable);
  118. }
  119. static void gen9_rc6_enable(struct intel_rc6 *rc6)
  120. {
  121. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  122. struct intel_engine_cs *engine;
  123. enum intel_engine_id id;
  124. /* 2b: Program RC6 thresholds.*/
  125. if (GRAPHICS_VER(rc6_to_i915(rc6)) >= 11) {
  126. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
  127. intel_uncore_write_fw(uncore, GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
  128. } else if (IS_SKYLAKE(rc6_to_i915(rc6))) {
  129. /*
  130. * WaRsDoubleRc6WrlWithCoarsePowerGating:skl Doubling WRL only
  131. * when CPG is enabled
  132. */
  133. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
  134. } else {
  135. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
  136. }
  137. intel_uncore_write_fw(uncore, GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  138. intel_uncore_write_fw(uncore, GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  139. for_each_engine(engine, rc6_to_gt(rc6), id)
  140. intel_uncore_write_fw(uncore, RING_MAX_IDLE(engine->mmio_base), 10);
  141. intel_uncore_write_fw(uncore, GUC_MAX_IDLE_COUNT, 0xA);
  142. intel_uncore_write_fw(uncore, GEN6_RC_SLEEP, 0);
  143. /*
  144. * 2c: Program Coarse Power Gating Policies.
  145. *
  146. * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
  147. * use instead is a more conservative estimate for the maximum time
  148. * it takes us to service a CS interrupt and submit a new ELSP - that
  149. * is the time which the GPU is idle waiting for the CPU to select the
  150. * next request to execute. If the idle hysteresis is less than that
  151. * interrupt service latency, the hardware will automatically gate
  152. * the power well and we will then incur the wake up cost on top of
  153. * the service latency. A similar guide from plane_state is that we
  154. * do not want the enable hysteresis to less than the wakeup latency.
  155. *
  156. * igt/gem_exec_nop/sequential provides a rough estimate for the
  157. * service latency, and puts it around 10us for Broadwell (and other
  158. * big core) and around 40us for Broxton (and other low power cores).
  159. * [Note that for legacy ringbuffer submission, this is less than 1us!]
  160. * However, the wakeup latency on Broxton is closer to 100us. To be
  161. * conservative, we have to factor in a context switch on top (due
  162. * to ksoftirqd).
  163. */
  164. intel_uncore_write_fw(uncore, GEN9_MEDIA_PG_IDLE_HYSTERESIS, 250);
  165. intel_uncore_write_fw(uncore, GEN9_RENDER_PG_IDLE_HYSTERESIS, 250);
  166. /* 3a: Enable RC6 */
  167. intel_uncore_write_fw(uncore, GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
  168. rc6->ctl_enable =
  169. GEN6_RC_CTL_HW_ENABLE |
  170. GEN6_RC_CTL_RC6_ENABLE |
  171. GEN6_RC_CTL_EI_MODE(1);
  172. /*
  173. * WaRsDisableCoarsePowerGating:skl,cnl
  174. * - Render/Media PG need to be disabled with RC6.
  175. */
  176. if (!NEEDS_WaRsDisableCoarsePowerGating(rc6_to_i915(rc6)))
  177. intel_uncore_write_fw(uncore, GEN9_PG_ENABLE,
  178. GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE);
  179. }
  180. static void gen8_rc6_enable(struct intel_rc6 *rc6)
  181. {
  182. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  183. struct intel_engine_cs *engine;
  184. enum intel_engine_id id;
  185. /* 2b: Program RC6 thresholds.*/
  186. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  187. intel_uncore_write_fw(uncore, GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  188. intel_uncore_write_fw(uncore, GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  189. for_each_engine(engine, rc6_to_gt(rc6), id)
  190. intel_uncore_write_fw(uncore, RING_MAX_IDLE(engine->mmio_base), 10);
  191. intel_uncore_write_fw(uncore, GEN6_RC_SLEEP, 0);
  192. intel_uncore_write_fw(uncore, GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
  193. /* 3: Enable RC6 */
  194. rc6->ctl_enable =
  195. GEN6_RC_CTL_HW_ENABLE |
  196. GEN7_RC_CTL_TO_MODE |
  197. GEN6_RC_CTL_RC6_ENABLE;
  198. }
  199. static void gen6_rc6_enable(struct intel_rc6 *rc6)
  200. {
  201. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  202. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  203. struct intel_engine_cs *engine;
  204. enum intel_engine_id id;
  205. u32 rc6vids, rc6_mask;
  206. int ret;
  207. intel_uncore_write_fw(uncore, GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  208. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  209. intel_uncore_write_fw(uncore, GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  210. intel_uncore_write_fw(uncore, GEN6_RC_EVALUATION_INTERVAL, 125000);
  211. intel_uncore_write_fw(uncore, GEN6_RC_IDLE_HYSTERSIS, 25);
  212. for_each_engine(engine, rc6_to_gt(rc6), id)
  213. intel_uncore_write_fw(uncore, RING_MAX_IDLE(engine->mmio_base), 10);
  214. intel_uncore_write_fw(uncore, GEN6_RC_SLEEP, 0);
  215. intel_uncore_write_fw(uncore, GEN6_RC1e_THRESHOLD, 1000);
  216. intel_uncore_write_fw(uncore, GEN6_RC6_THRESHOLD, 50000);
  217. intel_uncore_write_fw(uncore, GEN6_RC6p_THRESHOLD, 150000);
  218. intel_uncore_write_fw(uncore, GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  219. /* We don't use those on Haswell */
  220. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  221. if (HAS_RC6p(i915))
  222. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  223. if (HAS_RC6pp(i915))
  224. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  225. rc6->ctl_enable =
  226. rc6_mask |
  227. GEN6_RC_CTL_EI_MODE(1) |
  228. GEN6_RC_CTL_HW_ENABLE;
  229. rc6vids = 0;
  230. ret = snb_pcode_read(rc6_to_gt(rc6)->uncore, GEN6_PCODE_READ_RC6VIDS, &rc6vids, NULL);
  231. if (GRAPHICS_VER(i915) == 6 && ret) {
  232. drm_dbg(&i915->drm, "Couldn't check for BIOS workaround\n");
  233. } else if (GRAPHICS_VER(i915) == 6 &&
  234. (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  235. drm_dbg(&i915->drm,
  236. "You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  237. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  238. rc6vids &= 0xffff00;
  239. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  240. ret = snb_pcode_write(rc6_to_gt(rc6)->uncore, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  241. if (ret)
  242. drm_err(&i915->drm,
  243. "Couldn't fix incorrect rc6 voltage\n");
  244. }
  245. }
  246. /* Check that the pcbr address is not empty. */
  247. static int chv_rc6_init(struct intel_rc6 *rc6)
  248. {
  249. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  250. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  251. resource_size_t pctx_paddr, paddr;
  252. resource_size_t pctx_size = 32 * SZ_1K;
  253. u32 pcbr;
  254. pcbr = intel_uncore_read(uncore, VLV_PCBR);
  255. if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
  256. drm_dbg(&i915->drm, "BIOS didn't set up PCBR, fixing up\n");
  257. paddr = i915->dsm.stolen.end + 1 - pctx_size;
  258. GEM_BUG_ON(paddr > U32_MAX);
  259. pctx_paddr = (paddr & ~4095);
  260. intel_uncore_write(uncore, VLV_PCBR, pctx_paddr);
  261. }
  262. return 0;
  263. }
  264. static int vlv_rc6_init(struct intel_rc6 *rc6)
  265. {
  266. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  267. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  268. struct drm_i915_gem_object *pctx;
  269. resource_size_t pctx_paddr;
  270. resource_size_t pctx_size = 24 * SZ_1K;
  271. u32 pcbr;
  272. pcbr = intel_uncore_read(uncore, VLV_PCBR);
  273. if (pcbr) {
  274. /* BIOS set it up already, grab the pre-alloc'd space */
  275. resource_size_t pcbr_offset;
  276. pcbr_offset = (pcbr & ~4095) - i915->dsm.stolen.start;
  277. pctx = i915_gem_object_create_region_at(i915->mm.stolen_region,
  278. pcbr_offset,
  279. pctx_size,
  280. 0);
  281. if (IS_ERR(pctx))
  282. return PTR_ERR(pctx);
  283. goto out;
  284. }
  285. drm_dbg(&i915->drm, "BIOS didn't set up PCBR, fixing up\n");
  286. /*
  287. * From the Gunit register HAS:
  288. * The Gfx driver is expected to program this register and ensure
  289. * proper allocation within Gfx stolen memory. For example, this
  290. * register should be programmed such than the PCBR range does not
  291. * overlap with other ranges, such as the frame buffer, protected
  292. * memory, or any other relevant ranges.
  293. */
  294. pctx = i915_gem_object_create_stolen(i915, pctx_size);
  295. if (IS_ERR(pctx)) {
  296. drm_dbg(&i915->drm,
  297. "not enough stolen space for PCTX, disabling\n");
  298. return PTR_ERR(pctx);
  299. }
  300. GEM_BUG_ON(range_end_overflows_t(u64,
  301. i915->dsm.stolen.start,
  302. pctx->stolen->start,
  303. U32_MAX));
  304. pctx_paddr = i915->dsm.stolen.start + pctx->stolen->start;
  305. intel_uncore_write(uncore, VLV_PCBR, pctx_paddr);
  306. out:
  307. rc6->pctx = pctx;
  308. return 0;
  309. }
  310. static void chv_rc6_enable(struct intel_rc6 *rc6)
  311. {
  312. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  313. struct intel_engine_cs *engine;
  314. enum intel_engine_id id;
  315. /* 2a: Program RC6 thresholds.*/
  316. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  317. intel_uncore_write_fw(uncore, GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  318. intel_uncore_write_fw(uncore, GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  319. for_each_engine(engine, rc6_to_gt(rc6), id)
  320. intel_uncore_write_fw(uncore, RING_MAX_IDLE(engine->mmio_base), 10);
  321. intel_uncore_write_fw(uncore, GEN6_RC_SLEEP, 0);
  322. /* TO threshold set to 500 us (0x186 * 1.28 us) */
  323. intel_uncore_write_fw(uncore, GEN6_RC6_THRESHOLD, 0x186);
  324. /* Allows RC6 residency counter to work */
  325. intel_uncore_write_fw(uncore, VLV_COUNTER_CONTROL,
  326. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  327. VLV_MEDIA_RC6_COUNT_EN |
  328. VLV_RENDER_RC6_COUNT_EN));
  329. /* 3: Enable RC6 */
  330. rc6->ctl_enable = GEN7_RC_CTL_TO_MODE;
  331. }
  332. static void vlv_rc6_enable(struct intel_rc6 *rc6)
  333. {
  334. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  335. struct intel_engine_cs *engine;
  336. enum intel_engine_id id;
  337. intel_uncore_write_fw(uncore, GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  338. intel_uncore_write_fw(uncore, GEN6_RC_EVALUATION_INTERVAL, 125000);
  339. intel_uncore_write_fw(uncore, GEN6_RC_IDLE_HYSTERSIS, 25);
  340. for_each_engine(engine, rc6_to_gt(rc6), id)
  341. intel_uncore_write_fw(uncore, RING_MAX_IDLE(engine->mmio_base), 10);
  342. intel_uncore_write_fw(uncore, GEN6_RC6_THRESHOLD, 0x557);
  343. /* Allows RC6 residency counter to work */
  344. intel_uncore_write_fw(uncore, VLV_COUNTER_CONTROL,
  345. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  346. VLV_MEDIA_RC0_COUNT_EN |
  347. VLV_RENDER_RC0_COUNT_EN |
  348. VLV_MEDIA_RC6_COUNT_EN |
  349. VLV_RENDER_RC6_COUNT_EN));
  350. rc6->ctl_enable =
  351. GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
  352. }
  353. bool intel_check_bios_c6_setup(struct intel_rc6 *rc6)
  354. {
  355. if (!rc6->bios_state_captured) {
  356. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  357. intel_wakeref_t wakeref;
  358. with_intel_runtime_pm(uncore->rpm, wakeref)
  359. rc6->bios_rc_state = intel_uncore_read(uncore, GEN6_RC_STATE);
  360. rc6->bios_state_captured = true;
  361. }
  362. return rc6->bios_rc_state & RC_SW_TARGET_STATE_MASK;
  363. }
  364. static bool bxt_check_bios_rc6_setup(struct intel_rc6 *rc6)
  365. {
  366. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  367. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  368. u32 rc6_ctx_base, rc_ctl, rc_sw_target;
  369. bool enable_rc6 = true;
  370. rc_ctl = intel_uncore_read(uncore, GEN6_RC_CONTROL);
  371. rc_sw_target = intel_uncore_read(uncore, GEN6_RC_STATE);
  372. rc_sw_target &= RC_SW_TARGET_STATE_MASK;
  373. rc_sw_target >>= RC_SW_TARGET_STATE_SHIFT;
  374. drm_dbg(&i915->drm, "BIOS enabled RC states: "
  375. "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
  376. str_on_off(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
  377. str_on_off(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
  378. rc_sw_target);
  379. if (!(intel_uncore_read(uncore, RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
  380. drm_dbg(&i915->drm, "RC6 Base location not set properly.\n");
  381. enable_rc6 = false;
  382. }
  383. /*
  384. * The exact context size is not known for BXT, so assume a page size
  385. * for this check.
  386. */
  387. rc6_ctx_base =
  388. intel_uncore_read(uncore, RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
  389. if (!(rc6_ctx_base >= i915->dsm.reserved.start &&
  390. rc6_ctx_base + PAGE_SIZE < i915->dsm.reserved.end)) {
  391. drm_dbg(&i915->drm, "RC6 Base address not as expected.\n");
  392. enable_rc6 = false;
  393. }
  394. if (!((intel_uncore_read(uncore, PWRCTX_MAXCNT(RENDER_RING_BASE)) & IDLE_TIME_MASK) > 1 &&
  395. (intel_uncore_read(uncore, PWRCTX_MAXCNT(GEN6_BSD_RING_BASE)) & IDLE_TIME_MASK) > 1 &&
  396. (intel_uncore_read(uncore, PWRCTX_MAXCNT(BLT_RING_BASE)) & IDLE_TIME_MASK) > 1 &&
  397. (intel_uncore_read(uncore, PWRCTX_MAXCNT(VEBOX_RING_BASE)) & IDLE_TIME_MASK) > 1)) {
  398. drm_dbg(&i915->drm,
  399. "Engine Idle wait time not set properly.\n");
  400. enable_rc6 = false;
  401. }
  402. if (!intel_uncore_read(uncore, GEN8_PUSHBUS_CONTROL) ||
  403. !intel_uncore_read(uncore, GEN8_PUSHBUS_ENABLE) ||
  404. !intel_uncore_read(uncore, GEN8_PUSHBUS_SHIFT)) {
  405. drm_dbg(&i915->drm, "Pushbus not setup properly.\n");
  406. enable_rc6 = false;
  407. }
  408. if (!intel_uncore_read(uncore, GEN6_GFXPAUSE)) {
  409. drm_dbg(&i915->drm, "GFX pause not setup properly.\n");
  410. enable_rc6 = false;
  411. }
  412. if (!intel_uncore_read(uncore, GEN8_MISC_CTRL0)) {
  413. drm_dbg(&i915->drm, "GPM control not setup properly.\n");
  414. enable_rc6 = false;
  415. }
  416. return enable_rc6;
  417. }
  418. static bool rc6_supported(struct intel_rc6 *rc6)
  419. {
  420. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  421. struct intel_gt *gt = rc6_to_gt(rc6);
  422. if (!HAS_RC6(i915))
  423. return false;
  424. if (intel_vgpu_active(i915))
  425. return false;
  426. if (is_mock_gt(rc6_to_gt(rc6)))
  427. return false;
  428. if (IS_GEN9_LP(i915) && !bxt_check_bios_rc6_setup(rc6)) {
  429. drm_notice(&i915->drm,
  430. "RC6 and powersaving disabled by BIOS\n");
  431. return false;
  432. }
  433. if (IS_METEORLAKE(gt->i915) &&
  434. !intel_check_bios_c6_setup(rc6)) {
  435. drm_notice(&i915->drm,
  436. "C6 disabled by BIOS\n");
  437. return false;
  438. }
  439. if (IS_MEDIA_GT_IP_STEP(gt, IP_VER(13, 0), STEP_A0, STEP_B0)) {
  440. drm_notice(&i915->drm,
  441. "Media RC6 disabled on A step\n");
  442. return false;
  443. }
  444. return true;
  445. }
  446. static void rpm_get(struct intel_rc6 *rc6)
  447. {
  448. GEM_BUG_ON(rc6->wakeref);
  449. pm_runtime_get_sync(rc6_to_i915(rc6)->drm.dev);
  450. rc6->wakeref = true;
  451. }
  452. static void rpm_put(struct intel_rc6 *rc6)
  453. {
  454. GEM_BUG_ON(!rc6->wakeref);
  455. pm_runtime_put(rc6_to_i915(rc6)->drm.dev);
  456. rc6->wakeref = false;
  457. }
  458. static bool pctx_corrupted(struct intel_rc6 *rc6)
  459. {
  460. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  461. if (!NEEDS_RC6_CTX_CORRUPTION_WA(i915))
  462. return false;
  463. if (intel_uncore_read(rc6_to_uncore(rc6), GEN8_RC6_CTX_INFO))
  464. return false;
  465. drm_notice(&i915->drm,
  466. "RC6 context corruption, disabling runtime power management\n");
  467. return true;
  468. }
  469. static void __intel_rc6_disable(struct intel_rc6 *rc6)
  470. {
  471. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  472. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  473. struct intel_gt *gt = rc6_to_gt(rc6);
  474. /* Take control of RC6 back from GuC */
  475. intel_guc_rc_disable(gt_to_guc(gt));
  476. intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
  477. if (GRAPHICS_VER(i915) >= 9)
  478. intel_uncore_write_fw(uncore, GEN9_PG_ENABLE, 0);
  479. intel_uncore_write_fw(uncore, GEN6_RC_CONTROL, 0);
  480. intel_uncore_write_fw(uncore, GEN6_RC_STATE, 0);
  481. intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
  482. }
  483. static void rc6_res_reg_init(struct intel_rc6 *rc6)
  484. {
  485. i915_reg_t res_reg[INTEL_RC6_RES_MAX] = {
  486. [0 ... INTEL_RC6_RES_MAX - 1] = INVALID_MMIO_REG,
  487. };
  488. switch (rc6_to_gt(rc6)->type) {
  489. case GT_MEDIA:
  490. res_reg[INTEL_RC6_RES_RC6] = MTL_MEDIA_MC6;
  491. break;
  492. default:
  493. res_reg[INTEL_RC6_RES_RC6_LOCKED] = GEN6_GT_GFX_RC6_LOCKED;
  494. res_reg[INTEL_RC6_RES_RC6] = GEN6_GT_GFX_RC6;
  495. res_reg[INTEL_RC6_RES_RC6p] = GEN6_GT_GFX_RC6p;
  496. res_reg[INTEL_RC6_RES_RC6pp] = GEN6_GT_GFX_RC6pp;
  497. break;
  498. }
  499. memcpy(rc6->res_reg, res_reg, sizeof(res_reg));
  500. }
  501. void intel_rc6_init(struct intel_rc6 *rc6)
  502. {
  503. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  504. int err;
  505. /* Disable runtime-pm until we can save the GPU state with rc6 pctx */
  506. rpm_get(rc6);
  507. if (!rc6_supported(rc6))
  508. return;
  509. rc6_res_reg_init(rc6);
  510. if (IS_CHERRYVIEW(i915))
  511. err = chv_rc6_init(rc6);
  512. else if (IS_VALLEYVIEW(i915))
  513. err = vlv_rc6_init(rc6);
  514. else
  515. err = 0;
  516. /* Sanitize rc6, ensure it is disabled before we are ready. */
  517. __intel_rc6_disable(rc6);
  518. rc6->supported = err == 0;
  519. }
  520. void intel_rc6_sanitize(struct intel_rc6 *rc6)
  521. {
  522. memset(rc6->prev_hw_residency, 0, sizeof(rc6->prev_hw_residency));
  523. if (rc6->enabled) { /* unbalanced suspend/resume */
  524. rpm_get(rc6);
  525. rc6->enabled = false;
  526. }
  527. if (rc6->supported)
  528. __intel_rc6_disable(rc6);
  529. }
  530. void intel_rc6_enable(struct intel_rc6 *rc6)
  531. {
  532. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  533. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  534. if (!rc6->supported)
  535. return;
  536. GEM_BUG_ON(rc6->enabled);
  537. intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
  538. if (IS_CHERRYVIEW(i915))
  539. chv_rc6_enable(rc6);
  540. else if (IS_VALLEYVIEW(i915))
  541. vlv_rc6_enable(rc6);
  542. else if (GRAPHICS_VER(i915) >= 11)
  543. gen11_rc6_enable(rc6);
  544. else if (GRAPHICS_VER(i915) >= 9)
  545. gen9_rc6_enable(rc6);
  546. else if (IS_BROADWELL(i915))
  547. gen8_rc6_enable(rc6);
  548. else if (GRAPHICS_VER(i915) >= 6)
  549. gen6_rc6_enable(rc6);
  550. rc6->manual = rc6->ctl_enable & GEN6_RC_CTL_RC6_ENABLE;
  551. if (NEEDS_RC6_CTX_CORRUPTION_WA(i915))
  552. rc6->ctl_enable = 0;
  553. intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
  554. if (unlikely(pctx_corrupted(rc6)))
  555. return;
  556. /* rc6 is ready, runtime-pm is go! */
  557. rpm_put(rc6);
  558. rc6->enabled = true;
  559. }
  560. void intel_rc6_unpark(struct intel_rc6 *rc6)
  561. {
  562. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  563. if (!rc6->enabled)
  564. return;
  565. /* Restore HW timers for automatic RC6 entry while busy */
  566. intel_uncore_write_fw(uncore, GEN6_RC_CONTROL, rc6->ctl_enable);
  567. }
  568. void intel_rc6_park(struct intel_rc6 *rc6)
  569. {
  570. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  571. unsigned int target;
  572. if (!rc6->enabled)
  573. return;
  574. if (unlikely(pctx_corrupted(rc6))) {
  575. intel_rc6_disable(rc6);
  576. return;
  577. }
  578. if (!rc6->manual)
  579. return;
  580. /* Turn off the HW timers and go directly to rc6 */
  581. intel_uncore_write_fw(uncore, GEN6_RC_CONTROL, GEN6_RC_CTL_RC6_ENABLE);
  582. if (HAS_RC6pp(rc6_to_i915(rc6)))
  583. target = 0x6; /* deepest rc6 */
  584. else if (HAS_RC6p(rc6_to_i915(rc6)))
  585. target = 0x5; /* deep rc6 */
  586. else
  587. target = 0x4; /* normal rc6 */
  588. intel_uncore_write_fw(uncore, GEN6_RC_STATE, target << RC_SW_TARGET_STATE_SHIFT);
  589. }
  590. void intel_rc6_disable(struct intel_rc6 *rc6)
  591. {
  592. if (!rc6->enabled)
  593. return;
  594. rpm_get(rc6);
  595. rc6->enabled = false;
  596. __intel_rc6_disable(rc6);
  597. }
  598. void intel_rc6_fini(struct intel_rc6 *rc6)
  599. {
  600. struct drm_i915_gem_object *pctx;
  601. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  602. intel_rc6_disable(rc6);
  603. /* We want the BIOS C6 state preserved across loads for MTL */
  604. if (IS_METEORLAKE(rc6_to_i915(rc6)) && rc6->bios_state_captured)
  605. intel_uncore_write_fw(uncore, GEN6_RC_STATE, rc6->bios_rc_state);
  606. pctx = fetch_and_zero(&rc6->pctx);
  607. if (pctx)
  608. i915_gem_object_put(pctx);
  609. if (rc6->wakeref)
  610. rpm_put(rc6);
  611. }
  612. static u64 vlv_residency_raw(struct intel_uncore *uncore, const i915_reg_t reg)
  613. {
  614. u32 lower, upper, tmp;
  615. int loop = 2;
  616. /*
  617. * The register accessed do not need forcewake. We borrow
  618. * uncore lock to prevent concurrent access to range reg.
  619. */
  620. lockdep_assert_held(&uncore->lock);
  621. /*
  622. * vlv and chv residency counters are 40 bits in width.
  623. * With a control bit, we can choose between upper or lower
  624. * 32bit window into this counter.
  625. *
  626. * Although we always use the counter in high-range mode elsewhere,
  627. * userspace may attempt to read the value before rc6 is initialised,
  628. * before we have set the default VLV_COUNTER_CONTROL value. So always
  629. * set the high bit to be safe.
  630. */
  631. intel_uncore_write_fw(uncore, VLV_COUNTER_CONTROL,
  632. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  633. upper = intel_uncore_read_fw(uncore, reg);
  634. do {
  635. tmp = upper;
  636. intel_uncore_write_fw(uncore, VLV_COUNTER_CONTROL,
  637. _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
  638. lower = intel_uncore_read_fw(uncore, reg);
  639. intel_uncore_write_fw(uncore, VLV_COUNTER_CONTROL,
  640. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  641. upper = intel_uncore_read_fw(uncore, reg);
  642. } while (upper != tmp && --loop);
  643. /*
  644. * Everywhere else we always use VLV_COUNTER_CONTROL with the
  645. * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
  646. * now.
  647. */
  648. return lower | (u64)upper << 8;
  649. }
  650. u64 intel_rc6_residency_ns(struct intel_rc6 *rc6, enum intel_rc6_res_type id)
  651. {
  652. struct drm_i915_private *i915 = rc6_to_i915(rc6);
  653. struct intel_uncore *uncore = rc6_to_uncore(rc6);
  654. u64 time_hw, prev_hw, overflow_hw;
  655. i915_reg_t reg = rc6->res_reg[id];
  656. unsigned int fw_domains;
  657. unsigned long flags;
  658. u32 mul, div;
  659. if (!rc6->supported)
  660. return 0;
  661. fw_domains = intel_uncore_forcewake_for_reg(uncore, reg, FW_REG_READ);
  662. spin_lock_irqsave(&uncore->lock, flags);
  663. intel_uncore_forcewake_get__locked(uncore, fw_domains);
  664. /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
  665. if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
  666. mul = 1000000;
  667. div = vlv_clock_get_czclk(&i915->drm);
  668. overflow_hw = BIT_ULL(40);
  669. time_hw = vlv_residency_raw(uncore, reg);
  670. } else {
  671. /* 833.33ns units on Gen9LP, 1.28us elsewhere. */
  672. if (IS_GEN9_LP(i915)) {
  673. mul = 10000;
  674. div = 12;
  675. } else {
  676. mul = 1280;
  677. div = 1;
  678. }
  679. overflow_hw = BIT_ULL(32);
  680. time_hw = intel_uncore_read_fw(uncore, reg);
  681. }
  682. /*
  683. * Counter wrap handling.
  684. *
  685. * Store previous hw counter values for counter wrap-around handling. But
  686. * relying on a sufficient frequency of queries otherwise counters can still wrap.
  687. */
  688. prev_hw = rc6->prev_hw_residency[id];
  689. rc6->prev_hw_residency[id] = time_hw;
  690. /* RC6 delta from last sample. */
  691. if (time_hw >= prev_hw)
  692. time_hw -= prev_hw;
  693. else
  694. time_hw += overflow_hw - prev_hw;
  695. /* Add delta to RC6 extended raw driver copy. */
  696. time_hw += rc6->cur_residency[id];
  697. rc6->cur_residency[id] = time_hw;
  698. intel_uncore_forcewake_put__locked(uncore, fw_domains);
  699. spin_unlock_irqrestore(&uncore->lock, flags);
  700. return mul_u64_u32_div(time_hw, mul, div);
  701. }
  702. u64 intel_rc6_residency_us(struct intel_rc6 *rc6, enum intel_rc6_res_type id)
  703. {
  704. return DIV_ROUND_UP_ULL(intel_rc6_residency_ns(rc6, id), 1000);
  705. }
  706. void intel_rc6_print_residency(struct seq_file *m, const char *title,
  707. enum intel_rc6_res_type id)
  708. {
  709. struct intel_gt *gt = m->private;
  710. i915_reg_t reg = gt->rc6.res_reg[id];
  711. intel_wakeref_t wakeref;
  712. with_intel_runtime_pm(gt->uncore->rpm, wakeref)
  713. seq_printf(m, "%s %u (%llu us)\n", title,
  714. intel_uncore_read(gt->uncore, reg),
  715. intel_rc6_residency_us(&gt->rc6, id));
  716. }
  717. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  718. #include "selftest_rc6.c"
  719. #endif