Angel Pons | f23ae0b | 2020-04-02 23:48:12 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 2 | |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 3 | #include <assert.h> |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 4 | #include <console/console.h> |
| 5 | #include <device/device.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 6 | #include <acpi/acpi.h> |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 7 | #include <cpu/cpu.h> |
| 8 | #include <cpu/x86/mtrr.h> |
| 9 | #include <cpu/x86/msr.h> |
| 10 | #include <cpu/x86/lapic.h> |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 11 | #include <cpu/x86/mp.h> |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 12 | #include <cpu/intel/microcode.h> |
| 13 | #include <cpu/intel/speedstep.h> |
| 14 | #include <cpu/intel/turbo.h> |
| 15 | #include <cpu/x86/cache.h> |
| 16 | #include <cpu/x86/name.h> |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 17 | #include "model_2065x.h" |
| 18 | #include "chip.h" |
Kyösti Mälkki | f091f4d | 2019-08-14 03:49:21 +0300 | [diff] [blame] | 19 | #include <cpu/intel/smm_reloc.h> |
Matt DeVillier | ed6fe2f | 2016-12-14 16:12:43 -0600 | [diff] [blame] | 20 | #include <cpu/intel/common/common.h> |
Elyes HAOUAS | dda17fa | 2019-10-27 13:09:37 +0100 | [diff] [blame] | 21 | #include <smp/node.h> |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 22 | |
| 23 | /* |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 24 | * List of supported C-states in this processor |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 25 | * |
| 26 | * Latencies are typical worst-case package exit time in uS |
| 27 | * taken from the SandyBridge BIOS specification. |
| 28 | */ |
| 29 | static acpi_cstate_t cstate_map[] = { |
| 30 | { /* 0: C0 */ |
Lee Leahy | 9d62e7e | 2017-03-15 17:40:50 -0700 | [diff] [blame] | 31 | }, { /* 1: C1 */ |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 32 | .latency = 1, |
| 33 | .power = 1000, |
| 34 | .resource = { |
| 35 | .addrl = 0x00, /* MWAIT State 0 */ |
| 36 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 37 | .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, |
| 38 | .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 39 | .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 40 | } |
| 41 | }, |
| 42 | { /* 2: C1E */ |
| 43 | .latency = 1, |
| 44 | .power = 1000, |
| 45 | .resource = { |
| 46 | .addrl = 0x01, /* MWAIT State 0 Sub-state 1 */ |
| 47 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 48 | .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, |
| 49 | .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 50 | .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 51 | } |
| 52 | }, |
| 53 | { /* 3: C3 */ |
| 54 | .latency = 63, |
| 55 | .power = 500, |
| 56 | .resource = { |
| 57 | .addrl = 0x10, /* MWAIT State 1 */ |
| 58 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 59 | .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, |
| 60 | .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 61 | .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 62 | } |
| 63 | }, |
| 64 | { /* 4: C6 */ |
| 65 | .latency = 87, |
| 66 | .power = 350, |
| 67 | .resource = { |
| 68 | .addrl = 0x20, /* MWAIT State 2 */ |
| 69 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 70 | .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, |
| 71 | .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 72 | .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 73 | } |
| 74 | }, |
| 75 | { /* 5: C7 */ |
| 76 | .latency = 90, |
| 77 | .power = 200, |
| 78 | .resource = { |
| 79 | .addrl = 0x30, /* MWAIT State 3 */ |
| 80 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 81 | .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, |
| 82 | .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 83 | .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 84 | } |
| 85 | }, |
| 86 | { /* 6: C7S */ |
| 87 | .latency = 90, |
| 88 | .power = 200, |
| 89 | .resource = { |
| 90 | .addrl = 0x31, /* MWAIT State 3 Sub-state 1 */ |
| 91 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 92 | .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, |
| 93 | .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 94 | .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 95 | } |
| 96 | }, |
| 97 | { 0 } |
| 98 | }; |
| 99 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 100 | int cpu_config_tdp_levels(void) |
| 101 | { |
| 102 | msr_t platform_info; |
| 103 | |
| 104 | /* Minimum CPU revision */ |
| 105 | if (cpuid_eax(1) < IVB_CONFIG_TDP_MIN_CPUID) |
| 106 | return 0; |
| 107 | |
| 108 | /* Bits 34:33 indicate how many levels supported */ |
| 109 | platform_info = rdmsr(MSR_PLATFORM_INFO); |
| 110 | return (platform_info.hi >> 1) & 3; |
| 111 | } |
| 112 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 113 | static void configure_thermal_target(void) |
| 114 | { |
| 115 | struct cpu_intel_model_2065x_config *conf; |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 116 | struct device *lapic; |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 117 | msr_t msr; |
| 118 | |
| 119 | /* Find pointer to CPU configuration */ |
| 120 | lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC); |
| 121 | if (!lapic || !lapic->chip_info) |
| 122 | return; |
| 123 | conf = lapic->chip_info; |
| 124 | |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 125 | /* Set TCC activation offset if supported */ |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 126 | msr = rdmsr(MSR_PLATFORM_INFO); |
| 127 | if ((msr.lo & (1 << 30)) && conf->tcc_offset) { |
| 128 | msr = rdmsr(MSR_TEMPERATURE_TARGET); |
| 129 | msr.lo &= ~(0xf << 24); /* Bits 27:24 */ |
| 130 | msr.lo |= (conf->tcc_offset & 0xf) << 24; |
| 131 | wrmsr(MSR_TEMPERATURE_TARGET, msr); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void configure_misc(void) |
| 136 | { |
| 137 | msr_t msr; |
| 138 | |
| 139 | msr = rdmsr(IA32_MISC_ENABLE); |
| 140 | msr.lo |= (1 << 0); /* Fast String enable */ |
Lee Leahy | 7b5f12b9 | 2017-03-15 17:16:59 -0700 | [diff] [blame] | 141 | msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */ |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 142 | msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */ |
| 143 | wrmsr(IA32_MISC_ENABLE, msr); |
| 144 | |
| 145 | /* Disable Thermal interrupts */ |
| 146 | msr.lo = 0; |
| 147 | msr.hi = 0; |
| 148 | wrmsr(IA32_THERM_INTERRUPT, msr); |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 151 | static void set_max_ratio(void) |
| 152 | { |
| 153 | msr_t msr, perf_ctl; |
| 154 | |
| 155 | perf_ctl.hi = 0; |
| 156 | |
| 157 | /* Check for configurable TDP option */ |
| 158 | if (cpu_config_tdp_levels()) { |
| 159 | /* Set to nominal TDP ratio */ |
| 160 | msr = rdmsr(MSR_CONFIG_TDP_NOMINAL); |
| 161 | perf_ctl.lo = (msr.lo & 0xff) << 8; |
| 162 | } else { |
| 163 | /* Platform Info bits 15:8 give max ratio */ |
| 164 | msr = rdmsr(MSR_PLATFORM_INFO); |
| 165 | perf_ctl.lo = msr.lo & 0xff00; |
| 166 | } |
| 167 | wrmsr(IA32_PERF_CTL, perf_ctl); |
| 168 | |
| 169 | printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n", |
Angel Pons | 95de231 | 2020-02-17 13:08:53 +0100 | [diff] [blame] | 170 | ((perf_ctl.lo >> 8) & 0xff) * IRONLAKE_BCLK); |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 173 | static void configure_mca(void) |
| 174 | { |
| 175 | msr_t msr; |
| 176 | int i; |
| 177 | |
| 178 | msr.lo = msr.hi = 0; |
| 179 | /* This should only be done on a cold boot */ |
| 180 | for (i = 0; i < 7; i++) |
| 181 | wrmsr(IA32_MC0_STATUS + (i * 4), msr); |
| 182 | } |
| 183 | |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 184 | static void model_2065x_init(struct device *cpu) |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 185 | { |
| 186 | char processor_name[49]; |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 187 | |
| 188 | /* Turn on caching if we haven't already */ |
| 189 | x86_enable_cache(); |
| 190 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 191 | /* Clear out pending MCEs */ |
| 192 | configure_mca(); |
| 193 | |
| 194 | /* Print processor name */ |
| 195 | fill_processor_name(processor_name); |
| 196 | printk(BIOS_INFO, "CPU: %s.\n", processor_name); |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame] | 197 | printk(BIOS_INFO, "CPU:lapic=%d, boot_cpu=%d\n", lapicid(), |
Lee Leahy | 9d62e7e | 2017-03-15 17:40:50 -0700 | [diff] [blame] | 198 | boot_cpu()); |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 199 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 200 | /* Setup Page Attribute Tables (PAT) */ |
| 201 | // TODO set up PAT |
| 202 | |
Elyes HAOUAS | d6e9686 | 2016-08-21 10:12:15 +0200 | [diff] [blame] | 203 | /* Enable the local CPU APICs */ |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 204 | enable_lapic_tpr(); |
| 205 | setup_lapic(); |
| 206 | |
Matt DeVillier | ed6fe2f | 2016-12-14 16:12:43 -0600 | [diff] [blame] | 207 | /* Set virtualization based on Kconfig option */ |
Matt DeVillier | f9aed65 | 2018-12-15 15:57:33 -0600 | [diff] [blame] | 208 | set_vmx_and_lock(); |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 209 | |
Michael Niewöhner | 6303243 | 2020-10-11 17:34:54 +0200 | [diff] [blame] | 210 | set_aesni_lock(); |
Michael Niewöhner | 7f8767d | 2020-10-18 00:45:38 +0200 | [diff] [blame] | 211 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 212 | /* Configure Enhanced SpeedStep and Thermal Sensors */ |
| 213 | configure_misc(); |
| 214 | |
| 215 | /* Thermal throttle activation offset */ |
| 216 | configure_thermal_target(); |
| 217 | |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 218 | /* Set Max Ratio */ |
| 219 | set_max_ratio(); |
| 220 | |
| 221 | /* Enable Turbo */ |
| 222 | enable_turbo(); |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 223 | } |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 224 | |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 225 | /* MP initialization support. */ |
| 226 | static const void *microcode_patch; |
| 227 | |
| 228 | static void pre_mp_init(void) |
| 229 | { |
| 230 | /* Setup MTRRs based on physical address size. */ |
| 231 | x86_setup_mtrrs_with_detect(); |
| 232 | x86_mtrr_check(); |
| 233 | } |
| 234 | |
| 235 | static int get_cpu_count(void) |
| 236 | { |
| 237 | msr_t msr; |
| 238 | int num_threads; |
| 239 | int num_cores; |
| 240 | |
Elyes HAOUAS | a6a396d | 2019-05-26 13:25:30 +0200 | [diff] [blame] | 241 | msr = rdmsr(MSR_CORE_THREAD_COUNT); |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 242 | num_threads = (msr.lo >> 0) & 0xffff; |
| 243 | num_cores = (msr.lo >> 16) & 0xffff; |
| 244 | printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n", |
| 245 | num_cores, num_threads); |
| 246 | |
| 247 | return num_threads; |
| 248 | } |
| 249 | |
| 250 | static void get_microcode_info(const void **microcode, int *parallel) |
| 251 | { |
| 252 | microcode_patch = intel_microcode_find(); |
| 253 | *microcode = microcode_patch; |
| 254 | *parallel = 1; |
| 255 | } |
| 256 | |
| 257 | static void per_cpu_smm_trigger(void) |
| 258 | { |
| 259 | /* Relocate the SMM handler. */ |
| 260 | smm_relocate(); |
| 261 | |
| 262 | /* After SMM relocation a 2nd microcode load is required. */ |
| 263 | intel_microcode_load_unlocked(microcode_patch); |
| 264 | } |
| 265 | |
| 266 | static void post_mp_init(void) |
| 267 | { |
| 268 | /* Now that all APs have been relocated as well as the BSP let SMIs |
| 269 | * start flowing. */ |
Kyösti Mälkki | 0778c86 | 2020-06-10 12:44:03 +0300 | [diff] [blame] | 270 | global_smi_enable(); |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 271 | |
| 272 | /* Lock down the SMRAM space. */ |
| 273 | smm_lock(); |
| 274 | } |
| 275 | |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 276 | static const struct mp_ops mp_ops = { |
| 277 | .pre_mp_init = pre_mp_init, |
| 278 | .get_cpu_count = get_cpu_count, |
| 279 | .get_smm_info = smm_info, |
| 280 | .get_microcode_info = get_microcode_info, |
| 281 | .pre_mp_smm_init = smm_initialize, |
| 282 | .per_cpu_smm_trigger = per_cpu_smm_trigger, |
| 283 | .relocation_handler = smm_relocation_handler, |
| 284 | .post_mp_init = post_mp_init, |
| 285 | }; |
| 286 | |
Kyösti Mälkki | b3267e0 | 2019-08-13 16:44:04 +0300 | [diff] [blame] | 287 | void mp_init_cpus(struct bus *cpu_bus) |
Arthur Heymans | b66ee55 | 2018-05-15 16:35:45 +0200 | [diff] [blame] | 288 | { |
| 289 | if (mp_init_with_smm(cpu_bus, &mp_ops)) |
| 290 | printk(BIOS_ERR, "MP initialization failure.\n"); |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static struct device_operations cpu_dev_ops = { |
| 294 | .init = model_2065x_init, |
| 295 | }; |
| 296 | |
Angel Pons | a8305e7 | 2020-02-17 14:24:04 +0100 | [diff] [blame] | 297 | /* Arrandale / Clarkdale CPU IDs */ |
Jonathan Neuschäfer | 8f06ce3 | 2017-11-20 01:56:44 +0100 | [diff] [blame] | 298 | static const struct cpu_device_id cpu_table[] = { |
Angel Pons | a8305e7 | 2020-02-17 14:24:04 +0100 | [diff] [blame] | 299 | { X86_VENDOR_INTEL, 0x20650 }, |
| 300 | { X86_VENDOR_INTEL, 0x20651 }, |
| 301 | { X86_VENDOR_INTEL, 0x20652 }, |
| 302 | { X86_VENDOR_INTEL, 0x20654 }, |
| 303 | { X86_VENDOR_INTEL, 0x20655 }, |
Vladimir Serbinenko | 22dcdd9 | 2013-06-06 22:10:45 +0200 | [diff] [blame] | 304 | { 0, 0 }, |
| 305 | }; |
| 306 | |
| 307 | static const struct cpu_driver driver __cpu_driver = { |
| 308 | .ops = &cpu_dev_ops, |
| 309 | .id_table = cpu_table, |
| 310 | .cstates = cstate_map, |
| 311 | }; |