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