1 1.1 riastrad /* $NetBSD: intel_llc.c,v 1.2 2021/12/18 23:45:30 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.1 riastrad __KERNEL_RCSID(0, "$NetBSD: intel_llc.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $"); 11 1.1 riastrad 12 1.1 riastrad #include <linux/cpufreq.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_llc.h" 17 1.1 riastrad #include "intel_sideband.h" 18 1.1 riastrad 19 1.1 riastrad struct ia_constants { 20 1.1 riastrad unsigned int min_gpu_freq; 21 1.1 riastrad unsigned int max_gpu_freq; 22 1.1 riastrad 23 1.1 riastrad unsigned int min_ring_freq; 24 1.1 riastrad unsigned int max_ia_freq; 25 1.1 riastrad }; 26 1.1 riastrad 27 1.1 riastrad static struct intel_gt *llc_to_gt(struct intel_llc *llc) 28 1.1 riastrad { 29 1.1 riastrad return container_of(llc, struct intel_gt, llc); 30 1.1 riastrad } 31 1.1 riastrad 32 1.1 riastrad static unsigned int cpu_max_MHz(void) 33 1.1 riastrad { 34 1.1 riastrad struct cpufreq_policy *policy; 35 1.1 riastrad unsigned int max_khz; 36 1.1 riastrad 37 1.1 riastrad policy = cpufreq_cpu_get(0); 38 1.1 riastrad if (policy) { 39 1.1 riastrad max_khz = policy->cpuinfo.max_freq; 40 1.1 riastrad cpufreq_cpu_put(policy); 41 1.1 riastrad } else { 42 1.1 riastrad /* 43 1.1 riastrad * Default to measured freq if none found, PCU will ensure we 44 1.1 riastrad * don't go over 45 1.1 riastrad */ 46 1.1 riastrad max_khz = tsc_khz; 47 1.1 riastrad } 48 1.1 riastrad 49 1.1 riastrad return max_khz / 1000; 50 1.1 riastrad } 51 1.1 riastrad 52 1.1 riastrad static bool get_ia_constants(struct intel_llc *llc, 53 1.1 riastrad struct ia_constants *consts) 54 1.1 riastrad { 55 1.1 riastrad struct drm_i915_private *i915 = llc_to_gt(llc)->i915; 56 1.1 riastrad struct intel_rps *rps = &llc_to_gt(llc)->rps; 57 1.1 riastrad 58 1.1 riastrad if (rps->max_freq <= rps->min_freq) 59 1.1 riastrad return false; 60 1.1 riastrad 61 1.1 riastrad consts->max_ia_freq = cpu_max_MHz(); 62 1.1 riastrad 63 1.1 riastrad consts->min_ring_freq = 64 1.1 riastrad intel_uncore_read(llc_to_gt(llc)->uncore, DCLK) & 0xf; 65 1.1 riastrad /* convert DDR frequency from units of 266.6MHz to bandwidth */ 66 1.1 riastrad consts->min_ring_freq = mult_frac(consts->min_ring_freq, 8, 3); 67 1.1 riastrad 68 1.1 riastrad consts->min_gpu_freq = rps->min_freq; 69 1.1 riastrad consts->max_gpu_freq = rps->max_freq; 70 1.1 riastrad if (INTEL_GEN(i915) >= 9) { 71 1.1 riastrad /* Convert GT frequency to 50 HZ units */ 72 1.1 riastrad consts->min_gpu_freq /= GEN9_FREQ_SCALER; 73 1.1 riastrad consts->max_gpu_freq /= GEN9_FREQ_SCALER; 74 1.1 riastrad } 75 1.1 riastrad 76 1.1 riastrad return true; 77 1.1 riastrad } 78 1.1 riastrad 79 1.1 riastrad static void calc_ia_freq(struct intel_llc *llc, 80 1.1 riastrad unsigned int gpu_freq, 81 1.1 riastrad const struct ia_constants *consts, 82 1.1 riastrad unsigned int *out_ia_freq, 83 1.1 riastrad unsigned int *out_ring_freq) 84 1.1 riastrad { 85 1.1 riastrad struct drm_i915_private *i915 = llc_to_gt(llc)->i915; 86 1.1 riastrad const int diff = consts->max_gpu_freq - gpu_freq; 87 1.1 riastrad unsigned int ia_freq = 0, ring_freq = 0; 88 1.1 riastrad 89 1.1 riastrad if (INTEL_GEN(i915) >= 9) { 90 1.1 riastrad /* 91 1.1 riastrad * ring_freq = 2 * GT. ring_freq is in 100MHz units 92 1.1 riastrad * No floor required for ring frequency on SKL. 93 1.1 riastrad */ 94 1.1 riastrad ring_freq = gpu_freq; 95 1.1 riastrad } else if (INTEL_GEN(i915) >= 8) { 96 1.1 riastrad /* max(2 * GT, DDR). NB: GT is 50MHz units */ 97 1.1 riastrad ring_freq = max(consts->min_ring_freq, gpu_freq); 98 1.1 riastrad } else if (IS_HASWELL(i915)) { 99 1.1 riastrad ring_freq = mult_frac(gpu_freq, 5, 4); 100 1.1 riastrad ring_freq = max(consts->min_ring_freq, ring_freq); 101 1.1 riastrad /* leave ia_freq as the default, chosen by cpufreq */ 102 1.1 riastrad } else { 103 1.1 riastrad const int min_freq = 15; 104 1.1 riastrad const int scale = 180; 105 1.1 riastrad 106 1.1 riastrad /* 107 1.1 riastrad * On older processors, there is no separate ring 108 1.1 riastrad * clock domain, so in order to boost the bandwidth 109 1.1 riastrad * of the ring, we need to upclock the CPU (ia_freq). 110 1.1 riastrad * 111 1.1 riastrad * For GPU frequencies less than 750MHz, 112 1.1 riastrad * just use the lowest ring freq. 113 1.1 riastrad */ 114 1.1 riastrad if (gpu_freq < min_freq) 115 1.1 riastrad ia_freq = 800; 116 1.1 riastrad else 117 1.1 riastrad ia_freq = consts->max_ia_freq - diff * scale / 2; 118 1.1 riastrad ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100); 119 1.1 riastrad } 120 1.1 riastrad 121 1.1 riastrad *out_ia_freq = ia_freq; 122 1.1 riastrad *out_ring_freq = ring_freq; 123 1.1 riastrad } 124 1.1 riastrad 125 1.1 riastrad static void gen6_update_ring_freq(struct intel_llc *llc) 126 1.1 riastrad { 127 1.1 riastrad struct drm_i915_private *i915 = llc_to_gt(llc)->i915; 128 1.1 riastrad struct ia_constants consts; 129 1.1 riastrad unsigned int gpu_freq; 130 1.1 riastrad 131 1.1 riastrad if (!get_ia_constants(llc, &consts)) 132 1.1 riastrad return; 133 1.1 riastrad 134 1.1 riastrad /* 135 1.1 riastrad * For each potential GPU frequency, load a ring frequency we'd like 136 1.1 riastrad * to use for memory access. We do this by specifying the IA frequency 137 1.1 riastrad * the PCU should use as a reference to determine the ring frequency. 138 1.1 riastrad */ 139 1.1 riastrad for (gpu_freq = consts.max_gpu_freq; 140 1.1 riastrad gpu_freq >= consts.min_gpu_freq; 141 1.1 riastrad gpu_freq--) { 142 1.1 riastrad unsigned int ia_freq, ring_freq; 143 1.1 riastrad 144 1.1 riastrad calc_ia_freq(llc, gpu_freq, &consts, &ia_freq, &ring_freq); 145 1.1 riastrad sandybridge_pcode_write(i915, 146 1.1 riastrad GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 147 1.1 riastrad ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT | 148 1.1 riastrad ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT | 149 1.1 riastrad gpu_freq); 150 1.1 riastrad } 151 1.1 riastrad } 152 1.1 riastrad 153 1.1 riastrad void intel_llc_enable(struct intel_llc *llc) 154 1.1 riastrad { 155 1.1 riastrad if (HAS_LLC(llc_to_gt(llc)->i915)) 156 1.1 riastrad gen6_update_ring_freq(llc); 157 1.1 riastrad } 158 1.1 riastrad 159 1.1 riastrad void intel_llc_disable(struct intel_llc *llc) 160 1.1 riastrad { 161 1.1 riastrad /* Currently there is no HW configuration to be done to disable. */ 162 1.1 riastrad } 163 1.1 riastrad 164 1.1 riastrad #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 165 1.1 riastrad #include "selftest_llc.c" 166 1.1 riastrad #endif 167