Aaron Durbin | 302cbd6 | 2013-10-21 12:36:17 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Aaron Durbin | 302cbd6 | 2013-10-21 12:36:17 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdlib.h> |
| 17 | #include <console/console.h> |
| 18 | #include <cpu/cpu.h> |
| 19 | #include <cpu/intel/microcode.h> |
Duncan Laurie | 05a3393 | 2013-11-05 12:59:50 -0800 | [diff] [blame] | 20 | #include <cpu/intel/turbo.h> |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 21 | #include <cpu/x86/cache.h> |
| 22 | #include <cpu/x86/lapic.h> |
Aaron Durbin | 302cbd6 | 2013-10-21 12:36:17 -0500 | [diff] [blame] | 23 | #include <cpu/x86/mp.h> |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 24 | #include <cpu/x86/msr.h> |
| 25 | #include <cpu/x86/mtrr.h> |
| 26 | #include <cpu/x86/smm.h> |
Duncan Laurie | 05a3393 | 2013-11-05 12:59:50 -0800 | [diff] [blame] | 27 | #include <reg_script.h> |
Aaron Durbin | 302cbd6 | 2013-10-21 12:36:17 -0500 | [diff] [blame] | 28 | |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 29 | #include <soc/iosf.h> |
| 30 | #include <soc/msr.h> |
| 31 | #include <soc/pattrs.h> |
| 32 | #include <soc/ramstage.h> |
| 33 | #include <soc/smm.h> |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 34 | |
Duncan Laurie | 05a3393 | 2013-11-05 12:59:50 -0800 | [diff] [blame] | 35 | /* Core level MSRs */ |
| 36 | const struct reg_script core_msr_script[] = { |
Duncan Laurie | 31ac9e3 | 2014-03-28 10:52:13 -0700 | [diff] [blame] | 37 | /* Dynamic L2 shrink enable and threshold, clear SINGLE_PCTL bit 11 */ |
| 38 | REG_MSR_RMW(MSR_PMG_CST_CONFIG_CONTROL, ~0x3f080f, 0xe0008), |
| 39 | REG_MSR_RMW(MSR_POWER_MISC, |
| 40 | ~(ENABLE_ULFM_AUTOCM_MASK | ENABLE_INDP_AUTOCM_MASK), 0), |
Duncan Laurie | 05a3393 | 2013-11-05 12:59:50 -0800 | [diff] [blame] | 41 | /* Disable C1E */ |
| 42 | REG_MSR_RMW(MSR_POWER_CTL, ~0x2, 0), |
| 43 | REG_MSR_OR(MSR_POWER_MISC, 0x44), |
| 44 | REG_SCRIPT_END |
| 45 | }; |
| 46 | |
Aaron Durbin | 302cbd6 | 2013-10-21 12:36:17 -0500 | [diff] [blame] | 47 | static void baytrail_core_init(device_t cpu) |
| 48 | { |
| 49 | printk(BIOS_DEBUG, "Init BayTrail core.\n"); |
Duncan Laurie | 05a3393 | 2013-11-05 12:59:50 -0800 | [diff] [blame] | 50 | |
Aaron Durbin | 59d1d87 | 2014-01-14 17:34:10 -0600 | [diff] [blame] | 51 | /* On bay trail the turbo disable bit is actually scoped at building |
| 52 | * block level -- not package. For non-bsp cores that are within a |
| 53 | * building block enable turbo. The cores within the BSP's building |
| 54 | * block will just see it already enabled and move on. */ |
| 55 | if (lapicid()) |
| 56 | enable_turbo(); |
| 57 | |
Duncan Laurie | 05a3393 | 2013-11-05 12:59:50 -0800 | [diff] [blame] | 58 | /* Set core MSRs */ |
| 59 | reg_script_run(core_msr_script); |
| 60 | |
| 61 | /* Set this core to max frequency ratio */ |
| 62 | set_max_freq(); |
Aaron Durbin | 302cbd6 | 2013-10-21 12:36:17 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static struct device_operations cpu_dev_ops = { |
| 66 | .init = baytrail_core_init, |
| 67 | }; |
| 68 | |
| 69 | static struct cpu_device_id cpu_table[] = { |
| 70 | { X86_VENDOR_INTEL, 0x30673 }, |
Aaron Durbin | 1ea9bde | 2014-01-08 17:33:05 -0600 | [diff] [blame] | 71 | { X86_VENDOR_INTEL, 0x30678 }, |
Aaron Durbin | 302cbd6 | 2013-10-21 12:36:17 -0500 | [diff] [blame] | 72 | { 0, 0 }, |
| 73 | }; |
| 74 | |
| 75 | static const struct cpu_driver driver __cpu_driver = { |
| 76 | .ops = &cpu_dev_ops, |
| 77 | .id_table = cpu_table, |
| 78 | }; |
| 79 | |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 80 | |
| 81 | /* |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 82 | * MP and SMM loading initialization. |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 83 | */ |
| 84 | |
| 85 | struct smm_relocation_attrs { |
| 86 | uint32_t smbase; |
| 87 | uint32_t smrr_base; |
| 88 | uint32_t smrr_mask; |
| 89 | }; |
| 90 | |
| 91 | static struct smm_relocation_attrs relo_attrs; |
| 92 | |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 93 | /* Package level MSRs */ |
| 94 | static const struct reg_script package_msr_script[] = { |
| 95 | /* Set Package TDP to ~7W */ |
| 96 | REG_MSR_WRITE(MSR_PKG_POWER_LIMIT, 0x3880fa), |
| 97 | REG_MSR_RMW(MSR_PP1_POWER_LIMIT, ~(0x7f << 17), 0), |
| 98 | REG_MSR_WRITE(MSR_PKG_TURBO_CFG1, 0x702), |
| 99 | REG_MSR_WRITE(MSR_CPU_TURBO_WKLD_CFG1, 0x200b), |
| 100 | REG_MSR_WRITE(MSR_CPU_TURBO_WKLD_CFG2, 0), |
| 101 | REG_MSR_WRITE(MSR_CPU_THERM_CFG1, 0x00000305), |
| 102 | REG_MSR_WRITE(MSR_CPU_THERM_CFG2, 0x0405500d), |
| 103 | REG_MSR_WRITE(MSR_CPU_THERM_SENS_CFG, 0x27), |
| 104 | REG_SCRIPT_END |
| 105 | }; |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 106 | |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 107 | static void pre_mp_init(void) |
| 108 | { |
| 109 | uint32_t bsmrwac; |
| 110 | |
| 111 | /* Set up MTRRs based on physical address size. */ |
| 112 | x86_setup_mtrrs_with_detect(); |
| 113 | x86_mtrr_check(); |
| 114 | |
| 115 | /* |
| 116 | * Configure the BUNIT to allow dirty cache line evictions in non-SMM |
| 117 | * mode for the lines that were dirtied while in SMM mode. Otherwise |
| 118 | * the writes would be silently dropped. |
| 119 | */ |
| 120 | bsmrwac = iosf_bunit_read(BUNIT_SMRWAC) | SAI_IA_UNTRUSTED; |
| 121 | iosf_bunit_write(BUNIT_SMRWAC, bsmrwac); |
| 122 | |
| 123 | /* Set package MSRs */ |
| 124 | reg_script_run(package_msr_script); |
| 125 | |
| 126 | /* Enable Turbo Mode on BSP and siblings of the BSP's building block. */ |
| 127 | enable_turbo(); |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 130 | static int get_cpu_count(void) |
| 131 | { |
| 132 | const struct pattrs *pattrs = pattrs_get(); |
| 133 | |
| 134 | return pattrs->num_cpus; |
| 135 | } |
| 136 | |
| 137 | static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize, |
| 138 | size_t *smm_save_state_size) |
| 139 | { |
| 140 | /* All range registers are aligned to 4KiB */ |
| 141 | const uint32_t rmask = ~((1 << 12) - 1); |
| 142 | |
| 143 | /* Initialize global tracking state. */ |
| 144 | relo_attrs.smbase = (uint32_t)smm_region_start(); |
| 145 | relo_attrs.smrr_base = relo_attrs.smbase | MTRR_TYPE_WRBACK; |
| 146 | relo_attrs.smrr_mask = ~(smm_region_size() - 1) & rmask; |
| 147 | relo_attrs.smrr_mask |= MTRR_PHYS_MASK_VALID; |
| 148 | |
| 149 | *perm_smbase = relo_attrs.smbase; |
| 150 | *perm_smsize = smm_region_size() - CONFIG_SMM_RESERVED_SIZE; |
| 151 | *smm_save_state_size = sizeof(em64t100_smm_state_save_area_t); |
| 152 | } |
| 153 | |
| 154 | /* The APIC id space on Bay Trail is sparse. Each id is separated by 2. */ |
| 155 | static int adjust_apic_id(int index, int apic_id) |
| 156 | { |
| 157 | return 2 * index; |
| 158 | } |
| 159 | |
| 160 | static void get_microcode_info(const void **microcode, int *parallel) |
| 161 | { |
| 162 | const struct pattrs *pattrs = pattrs_get(); |
| 163 | |
| 164 | *microcode = pattrs->microcode_patch; |
| 165 | *parallel = 1; |
| 166 | } |
| 167 | |
| 168 | static void per_cpu_smm_trigger(void) |
| 169 | { |
| 170 | const struct pattrs *pattrs = pattrs_get(); |
| 171 | |
| 172 | /* Relocate SMM space. */ |
| 173 | smm_initiate_relocation(); |
| 174 | |
| 175 | /* Load microcode after SMM relocation. */ |
| 176 | intel_microcode_load_unlocked(pattrs->microcode_patch); |
| 177 | } |
| 178 | |
| 179 | static void relocation_handler(int cpu, uintptr_t curr_smbase, |
| 180 | uintptr_t staggered_smbase) |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 181 | { |
| 182 | msr_t smrr; |
| 183 | em64t100_smm_state_save_area_t *smm_state; |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 184 | |
| 185 | /* Set up SMRR. */ |
| 186 | smrr.lo = relo_attrs.smrr_base; |
| 187 | smrr.hi = 0; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 188 | wrmsr(SMRR_PHYS_BASE, smrr); |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 189 | smrr.lo = relo_attrs.smrr_mask; |
| 190 | smrr.hi = 0; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 191 | wrmsr(SMRR_PHYS_MASK, smrr); |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 192 | |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 193 | smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + curr_smbase); |
| 194 | smm_state->smbase = staggered_smbase; |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 197 | static const struct mp_ops mp_ops = { |
| 198 | .pre_mp_init = pre_mp_init, |
| 199 | .get_cpu_count = get_cpu_count, |
| 200 | .get_smm_info = get_smm_info, |
| 201 | .get_microcode_info = get_microcode_info, |
| 202 | .adjust_cpu_apic_entry = adjust_apic_id, |
| 203 | .pre_mp_smm_init = southcluster_smm_clear_state, |
| 204 | .per_cpu_smm_trigger = per_cpu_smm_trigger, |
| 205 | .relocation_handler = relocation_handler, |
| 206 | .post_mp_init = southcluster_smm_enable_smi, |
| 207 | }; |
| 208 | |
| 209 | void baytrail_init_cpus(device_t dev) |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 210 | { |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 211 | struct bus *cpu_bus = dev->link_list; |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 212 | |
Aaron Durbin | b04bb65 | 2016-05-03 11:12:52 -0500 | [diff] [blame] | 213 | if (mp_init_with_smm(cpu_bus, &mp_ops)) { |
| 214 | printk(BIOS_ERR, "MP initialization failure.\n"); |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 215 | } |
Aaron Durbin | 7837be6 | 2013-10-21 22:32:00 -0500 | [diff] [blame] | 216 | } |