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