1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include "intel_context.h"
7 #include "intel_engine_pm.h"
8 #include "intel_gpu_commands.h"
9 #include "intel_gt_requests.h"
10 #include "intel_ring.h"
11 #include "selftest_rc6.h"
13 #include "selftests/i915_random.h"
14 #include "selftests/librapl.h"
16 static u64 rc6_residency(struct intel_rc6 *rc6)
20 /* XXX VLV_GT_MEDIA_RC6? */
22 result = intel_rc6_residency_ns(rc6, INTEL_RC6_RES_RC6);
23 if (HAS_RC6p(rc6_to_i915(rc6)))
24 result += intel_rc6_residency_ns(rc6, INTEL_RC6_RES_RC6p);
25 if (HAS_RC6pp(rc6_to_i915(rc6)))
26 result += intel_rc6_residency_ns(rc6, INTEL_RC6_RES_RC6pp);
31 int live_rc6_manual(void *arg)
33 struct intel_gt *gt = arg;
34 struct intel_rc6 *rc6 = >->rc6;
35 u64 rc0_power, rc6_power;
36 intel_wakeref_t wakeref;
43 * Our claim is that we can "encourage" the GPU to enter rc6 at will.
50 /* bsw/byt use a PCU and decouple RC6 from our manual control */
51 if (IS_VALLEYVIEW(gt->i915) || IS_CHERRYVIEW(gt->i915))
54 has_power = librapl_supported(gt->i915);
55 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
57 /* Force RC6 off for starters */
58 __intel_rc6_disable(rc6);
59 msleep(1); /* wakeup is not immediate, takes about 100us on icl */
61 res[0] = rc6_residency(rc6);
64 rc0_power = librapl_energy_uJ();
66 rc0_power = librapl_energy_uJ() - rc0_power;
67 dt = ktime_sub(ktime_get(), dt);
68 res[1] = rc6_residency(rc6);
69 if ((res[1] - res[0]) >> 10) {
70 pr_err("RC6 residency increased by %lldus while disabled for 250ms!\n",
71 (res[1] - res[0]) >> 10);
77 rc0_power = div64_u64(NSEC_PER_SEC * rc0_power,
80 pr_err("No power measured while in RC0\n");
86 /* Manually enter RC6 */
89 res[0] = rc6_residency(rc6);
90 intel_uncore_forcewake_flush(rc6_to_uncore(rc6), FORCEWAKE_ALL);
92 rc6_power = librapl_energy_uJ();
94 rc6_power = librapl_energy_uJ() - rc6_power;
95 dt = ktime_sub(ktime_get(), dt);
96 res[1] = rc6_residency(rc6);
97 if (res[1] == res[0]) {
98 pr_err("Did not enter RC6! RC6_STATE=%08x, RC6_CONTROL=%08x, residency=%lld\n",
99 intel_uncore_read_fw(gt->uncore, GEN6_RC_STATE),
100 intel_uncore_read_fw(gt->uncore, GEN6_RC_CONTROL),
106 rc6_power = div64_u64(NSEC_PER_SEC * rc6_power,
108 pr_info("GPU consumed %llduW in RC0 and %llduW in RC6\n",
109 rc0_power, rc6_power);
110 if (2 * rc6_power > rc0_power) {
111 pr_err("GPU leaked energy while in RC6!\n");
117 /* Restore what should have been the original state! */
118 intel_rc6_unpark(rc6);
121 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
125 static const u32 *__live_rc6_ctx(struct intel_context *ce)
127 struct i915_request *rq;
132 rq = intel_context_create_request(ce);
136 cs = intel_ring_begin(rq, 4);
138 i915_request_add(rq);
142 cmd = MI_STORE_REGISTER_MEM | MI_USE_GGTT;
143 if (GRAPHICS_VER(rq->engine->i915) >= 8)
147 *cs++ = i915_mmio_reg_offset(GEN8_RC6_CTX_INFO);
148 *cs++ = ce->timeline->hwsp_offset + 8;
150 intel_ring_advance(rq, cs);
152 result = rq->hwsp_seqno + 2;
153 i915_request_add(rq);
158 static struct intel_engine_cs **
159 randomised_engines(struct intel_gt *gt,
160 struct rnd_state *prng,
163 struct intel_engine_cs *engine, **engines;
164 enum intel_engine_id id;
168 for_each_engine(engine, gt, id)
173 engines = kmalloc_array(n, sizeof(*engines), GFP_KERNEL);
178 for_each_engine(engine, gt, id)
179 engines[n++] = engine;
181 i915_prandom_shuffle(engines, sizeof(*engines), n, prng);
187 int live_rc6_ctx_wa(void *arg)
189 struct intel_gt *gt = arg;
190 struct intel_engine_cs **engines;
191 unsigned int n, count;
192 I915_RND_STATE(prng);
195 /* A read of CTX_INFO upsets rc6. Poke the bear! */
196 if (GRAPHICS_VER(gt->i915) < 8)
199 engines = randomised_engines(gt, &prng, &count);
203 for (n = 0; n < count; n++) {
204 struct intel_engine_cs *engine = engines[n];
207 for (pass = 0; pass < 2; pass++) {
208 struct i915_gpu_error *error = >->i915->gpu_error;
209 struct intel_context *ce;
210 unsigned int resets =
211 i915_reset_engine_count(error, engine);
214 /* Use a sacrifical context */
215 ce = intel_context_create(engine);
221 intel_engine_pm_get(engine);
222 res = __live_rc6_ctx(ce);
223 intel_engine_pm_put(engine);
224 intel_context_put(ce);
230 if (intel_gt_wait_for_idle(gt, HZ / 5) == -ETIME) {
231 intel_gt_set_wedged(gt);
236 intel_gt_pm_wait_for_idle(gt);
237 pr_debug("%s: CTX_INFO=%0x\n",
238 engine->name, READ_ONCE(*res));
241 i915_reset_engine_count(error, engine)) {
242 pr_err("%s: GPU reset required\n",
244 add_taint_for_CI(gt->i915, TAINT_WARN);