Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 5 | * Copyright (C) 2014 Sage Electronic Engineering, LLC. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <console/console.h> |
| 19 | #include <cpu/cpu.h> |
| 20 | #include <cpu/intel/microcode.h> |
| 21 | #include <cpu/intel/turbo.h> |
| 22 | #include <cpu/x86/cache.h> |
| 23 | #include <cpu/x86/lapic.h> |
| 24 | #include <cpu/x86/mp.h> |
| 25 | #include <cpu/x86/msr.h> |
| 26 | #include <cpu/x86/mtrr.h> |
| 27 | #include <cpu/x86/smm.h> |
| 28 | #include <reg_script.h> |
| 29 | |
Ben Gardner | fa6014a | 2015-12-08 21:20:25 -0600 | [diff] [blame] | 30 | #include <soc/msr.h> |
| 31 | #include <soc/pattrs.h> |
| 32 | #include <soc/ramstage.h> |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 33 | #if IS_ENABLED(CONFIG_HAVE_SMI_HANDLER) |
Ben Gardner | fa6014a | 2015-12-08 21:20:25 -0600 | [diff] [blame] | 34 | #include <soc/smm.h> |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 35 | |
| 36 | static void smm_relocate(void *unused); |
| 37 | static void enable_smis(void *unused); |
| 38 | |
| 39 | static struct mp_flight_record mp_steps[] = { |
| 40 | MP_FR_BLOCK_APS(smm_relocate, NULL, smm_relocate, NULL), |
| 41 | MP_FR_BLOCK_APS(mp_initialize_cpu, NULL, mp_initialize_cpu, NULL), |
| 42 | /* Wait for APs to finish initialization before proceeding. */ |
| 43 | MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL), |
| 44 | }; |
| 45 | #else /* CONFIG_HAVE_SMI_HANDLER */ |
| 46 | static struct mp_flight_record mp_steps[] = { |
| 47 | MP_FR_BLOCK_APS(mp_initialize_cpu, NULL, mp_initialize_cpu, NULL), |
| 48 | }; |
| 49 | #endif |
| 50 | |
| 51 | /* The APIC id space on Bay Trail is sparse. Each id is separated by 2. */ |
| 52 | static int adjust_apic_id(int index, int apic_id) |
| 53 | { |
| 54 | return 2 * index; |
| 55 | } |
| 56 | |
| 57 | /* Core level MSRs */ |
| 58 | const struct reg_script core_msr_script[] = { |
| 59 | /* Dynamic L2 shrink enable and threshold */ |
| 60 | REG_MSR_RMW(MSR_PMG_CST_CONFIG_CONTROL, ~0x3f000f, 0xe0008), |
| 61 | /* Disable C1E */ |
| 62 | REG_MSR_RMW(MSR_POWER_CTL, ~0x2, 0), |
| 63 | REG_MSR_OR(MSR_POWER_MISC, 0x44), |
| 64 | REG_SCRIPT_END |
| 65 | }; |
| 66 | |
| 67 | void baytrail_init_cpus(device_t dev) |
| 68 | { |
| 69 | struct bus *cpu_bus = dev->link_list; |
| 70 | const struct pattrs *pattrs = pattrs_get(); |
| 71 | struct mp_params mp_params; |
| 72 | |
| 73 | x86_mtrr_check(); |
| 74 | |
| 75 | /* Enable the local cpu apics */ |
| 76 | setup_lapic(); |
| 77 | |
| 78 | mp_params.num_cpus = pattrs->num_cpus, |
York Yang | 72e33a7 | 2015-10-19 13:35:21 -0700 | [diff] [blame] | 79 | mp_params.parallel_microcode_load = 1, |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 80 | mp_params.adjust_apic_id = adjust_apic_id; |
| 81 | mp_params.flight_plan = &mp_steps[0]; |
| 82 | mp_params.num_records = ARRAY_SIZE(mp_steps); |
York Yang | 72e33a7 | 2015-10-19 13:35:21 -0700 | [diff] [blame] | 83 | mp_params.microcode_pointer = pattrs->microcode_patch; |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 84 | |
| 85 | if (mp_init(cpu_bus, &mp_params)) { |
| 86 | printk(BIOS_ERR, "MP initialization failure.\n"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void baytrail_core_init(device_t cpu) |
| 91 | { |
| 92 | printk(BIOS_DEBUG, "Init BayTrail core.\n"); |
| 93 | |
| 94 | /* On bay trail the turbo disable bit is actually scoped at building |
| 95 | * block level -- not package. For non-bsp cores that are within a |
| 96 | * building block enable turbo. The cores within the BSP's building |
| 97 | * block will just see it already enabled and move on. */ |
| 98 | if (lapicid()) |
| 99 | enable_turbo(); |
| 100 | |
| 101 | /* Set core MSRs */ |
| 102 | reg_script_run(core_msr_script); |
| 103 | |
| 104 | /* Set this core to max frequency ratio */ |
| 105 | set_max_freq(); |
| 106 | } |
| 107 | |
| 108 | static struct device_operations cpu_dev_ops = { |
| 109 | .init = baytrail_core_init, |
| 110 | }; |
| 111 | |
| 112 | static struct cpu_device_id cpu_table[] = { |
| 113 | { X86_VENDOR_INTEL, 0x30671 }, |
| 114 | { X86_VENDOR_INTEL, 0x30672 }, |
| 115 | { X86_VENDOR_INTEL, 0x30673 }, |
| 116 | { X86_VENDOR_INTEL, 0x30678 }, |
Herve ELter | c7e6cae | 2014-11-19 16:05:28 +0100 | [diff] [blame] | 117 | { X86_VENDOR_INTEL, 0x30679 }, |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 118 | { 0, 0 }, |
| 119 | }; |
| 120 | |
| 121 | static const struct cpu_driver driver __cpu_driver = { |
| 122 | .ops = &cpu_dev_ops, |
| 123 | .id_table = cpu_table, |
| 124 | }; |
| 125 | |
| 126 | #if IS_ENABLED(CONFIG_HAVE_SMI_HANDLER) |
| 127 | /* |
| 128 | * SMM loading and initialization. |
| 129 | */ |
| 130 | |
| 131 | struct smm_relocation_attrs { |
| 132 | uint32_t smbase; |
| 133 | uint32_t smrr_base; |
| 134 | uint32_t smrr_mask; |
| 135 | }; |
| 136 | |
| 137 | static struct smm_relocation_attrs relo_attrs; |
| 138 | |
| 139 | static void adjust_apic_id_map(struct smm_loader_params *smm_params) |
| 140 | { |
| 141 | int i; |
| 142 | struct smm_runtime *runtime = smm_params->runtime; |
| 143 | |
| 144 | for (i = 0; i < CONFIG_MAX_CPUS; i++) |
| 145 | runtime->apic_id_to_cpu[i] = mp_get_apic_id(i); |
| 146 | } |
| 147 | |
Kayalvizhi Dhandapani | 454625c | 2014-10-07 14:34:01 -0400 | [diff] [blame] | 148 | static void asmlinkage cpu_smm_do_relocation(void *arg) |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 149 | { |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 150 | msr_t smrr; |
| 151 | em64t100_smm_state_save_area_t *smm_state; |
Ben Gardner | 6fc544d | 2015-12-09 11:33:39 -0600 | [diff] [blame] | 152 | const struct smm_module_params *p; |
| 153 | const struct smm_runtime *runtime; |
| 154 | int cpu; |
Kayalvizhi Dhandapani | 454625c | 2014-10-07 14:34:01 -0400 | [diff] [blame] | 155 | |
Ben Gardner | 6fc544d | 2015-12-09 11:33:39 -0600 | [diff] [blame] | 156 | p = arg; |
| 157 | runtime = p->runtime; |
| 158 | cpu = p->cpu; |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 159 | |
| 160 | if (cpu >= CONFIG_MAX_CPUS) { |
| 161 | printk(BIOS_CRIT, |
| 162 | "Invalid CPU number assigned in SMM stub: %d\n", cpu); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | /* Set up SMRR. */ |
| 167 | smrr.lo = relo_attrs.smrr_base; |
| 168 | smrr.hi = 0; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 169 | wrmsr(SMRR_PHYS_BASE, smrr); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 170 | smrr.lo = relo_attrs.smrr_mask; |
| 171 | smrr.hi = 0; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 172 | wrmsr(SMRR_PHYS_MASK, smrr); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 173 | |
| 174 | /* The relocated handler runs with all CPUs concurrently. Therefore |
| 175 | * stagger the entry points adjusting SMBASE downwards by save state |
| 176 | * size * CPU num. */ |
| 177 | smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + runtime->smbase); |
| 178 | smm_state->smbase = relo_attrs.smbase - cpu * runtime->save_state_size; |
| 179 | printk(BIOS_DEBUG, "New SMBASE 0x%08x\n", smm_state->smbase); |
| 180 | } |
| 181 | |
| 182 | static int install_relocation_handler(int num_cpus) |
| 183 | { |
| 184 | const int save_state_size = sizeof(em64t100_smm_state_save_area_t); |
| 185 | |
| 186 | struct smm_loader_params smm_params = { |
| 187 | .per_cpu_stack_size = save_state_size, |
| 188 | .num_concurrent_stacks = num_cpus, |
| 189 | .per_cpu_save_state_size = save_state_size, |
| 190 | .num_concurrent_save_states = 1, |
| 191 | .handler = (smm_handler_t)&cpu_smm_do_relocation, |
| 192 | }; |
| 193 | |
| 194 | if (smm_setup_relocation_handler(&smm_params)) |
| 195 | return -1; |
| 196 | |
| 197 | adjust_apic_id_map(&smm_params); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int install_permanent_handler(int num_cpus) |
| 203 | { |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 204 | /* There are num_cpus concurrent stacks and num_cpus concurrent save |
| 205 | * state areas. Lastly, set the stack size to the save state size. */ |
| 206 | int save_state_size = sizeof(em64t100_smm_state_save_area_t); |
| 207 | struct smm_loader_params smm_params = { |
| 208 | .per_cpu_stack_size = save_state_size, |
| 209 | .num_concurrent_stacks = num_cpus, |
| 210 | .per_cpu_save_state_size = save_state_size, |
| 211 | .num_concurrent_save_states = num_cpus, |
| 212 | }; |
| 213 | const int tseg_size = smm_region_size() - CONFIG_SMM_RESERVED_SIZE; |
| 214 | |
| 215 | printk(BIOS_DEBUG, "Installing SMM handler to 0x%08x\n", |
| 216 | relo_attrs.smbase); |
| 217 | |
| 218 | if (smm_load_module((void *)relo_attrs.smbase, tseg_size, &smm_params)) |
| 219 | return -1; |
| 220 | |
| 221 | adjust_apic_id_map(&smm_params); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int smm_load_handlers(void) |
| 227 | { |
| 228 | /* All range registers are aligned to 4KiB */ |
| 229 | const uint32_t rmask = ~((1 << 12) - 1); |
| 230 | const struct pattrs *pattrs = pattrs_get(); |
| 231 | |
| 232 | /* Initialize global tracking state. */ |
| 233 | relo_attrs.smbase = (uint32_t)smm_region_start(); |
| 234 | relo_attrs.smrr_base = relo_attrs.smbase | MTRR_TYPE_WRBACK; |
| 235 | relo_attrs.smrr_mask = ~(smm_region_size() - 1) & rmask; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 236 | relo_attrs.smrr_mask |= MTRR_PHYS_MASK_VALID; |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 237 | |
| 238 | /* Install handlers. */ |
| 239 | if (install_relocation_handler(pattrs->num_cpus) < 0) { |
| 240 | printk(BIOS_ERR, "Unable to install SMM relocation handler.\n"); |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | if (install_permanent_handler(pattrs->num_cpus) < 0) { |
| 245 | printk(BIOS_ERR, "Unable to install SMM permanent handler.\n"); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | /* Ensure the SMM handlers hit DRAM before performing first SMI. */ |
| 250 | wbinvd(); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static void smm_relocate(void *unused) |
| 256 | { |
| 257 | |
| 258 | /* Load relocation and permanent handler. */ |
| 259 | if (boot_cpu()) { |
| 260 | if (smm_load_handlers() < 0) { |
| 261 | printk(BIOS_ERR, "Error loading SMM handlers.\n"); |
| 262 | return; |
| 263 | } |
| 264 | southcluster_smm_clear_state(); |
| 265 | } |
| 266 | |
| 267 | /* Relocate SMM space. */ |
| 268 | smm_initiate_relocation(); |
| 269 | } |
| 270 | |
| 271 | static void enable_smis(void *unused) |
| 272 | { |
| 273 | southcluster_smm_enable_smi(); |
| 274 | } |
| 275 | #endif |