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> |
Ben Gardner | fa6014a | 2015-12-08 21:20:25 -0600 | [diff] [blame] | 33 | #include <soc/smm.h> |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 34 | |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 35 | /* Core level MSRs */ |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 36 | static const struct reg_script core_msr_script[] = { |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 37 | /* Dynamic L2 shrink enable and threshold */ |
| 38 | REG_MSR_RMW(MSR_PMG_CST_CONFIG_CONTROL, ~0x3f000f, 0xe0008), |
| 39 | /* Disable C1E */ |
| 40 | REG_MSR_RMW(MSR_POWER_CTL, ~0x2, 0), |
| 41 | REG_MSR_OR(MSR_POWER_MISC, 0x44), |
| 42 | REG_SCRIPT_END |
| 43 | }; |
| 44 | |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 45 | static void baytrail_core_init(device_t cpu) |
| 46 | { |
| 47 | printk(BIOS_DEBUG, "Init BayTrail core.\n"); |
| 48 | |
| 49 | /* On bay trail the turbo disable bit is actually scoped at building |
| 50 | * block level -- not package. For non-bsp cores that are within a |
| 51 | * building block enable turbo. The cores within the BSP's building |
| 52 | * block will just see it already enabled and move on. */ |
| 53 | if (lapicid()) |
| 54 | enable_turbo(); |
| 55 | |
| 56 | /* Set core MSRs */ |
| 57 | reg_script_run(core_msr_script); |
| 58 | |
| 59 | /* Set this core to max frequency ratio */ |
| 60 | set_max_freq(); |
| 61 | } |
| 62 | |
| 63 | static struct device_operations cpu_dev_ops = { |
| 64 | .init = baytrail_core_init, |
| 65 | }; |
| 66 | |
| 67 | static struct cpu_device_id cpu_table[] = { |
| 68 | { X86_VENDOR_INTEL, 0x30671 }, |
| 69 | { X86_VENDOR_INTEL, 0x30672 }, |
| 70 | { X86_VENDOR_INTEL, 0x30673 }, |
| 71 | { X86_VENDOR_INTEL, 0x30678 }, |
Herve ELter | c7e6cae | 2014-11-19 16:05:28 +0100 | [diff] [blame] | 72 | { X86_VENDOR_INTEL, 0x30679 }, |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 73 | { 0, 0 }, |
| 74 | }; |
| 75 | |
| 76 | static const struct cpu_driver driver __cpu_driver = { |
| 77 | .ops = &cpu_dev_ops, |
| 78 | .id_table = cpu_table, |
| 79 | }; |
| 80 | |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 81 | /* |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 82 | * MP and SMM loading initialization. |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [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 | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 93 | static void pre_mp_init(void) |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 94 | { |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 95 | x86_mtrr_check(); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 96 | |
Elyes HAOUAS | 038e724 | 2016-07-29 18:31:16 +0200 | [diff] [blame] | 97 | /* Enable the local CPU apics */ |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 98 | setup_lapic(); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 99 | } |
| 100 | |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 101 | static int get_cpu_count(void) |
| 102 | { |
| 103 | const struct pattrs *pattrs = pattrs_get(); |
| 104 | |
| 105 | return pattrs->num_cpus; |
| 106 | } |
| 107 | |
| 108 | static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize, |
| 109 | size_t *smm_save_state_size) |
| 110 | { |
| 111 | /* All range registers are aligned to 4KiB */ |
| 112 | const uint32_t rmask = ~((1 << 12) - 1); |
| 113 | |
| 114 | /* Initialize global tracking state. */ |
| 115 | relo_attrs.smbase = (uint32_t)smm_region_start(); |
| 116 | relo_attrs.smrr_base = relo_attrs.smbase | MTRR_TYPE_WRBACK; |
| 117 | relo_attrs.smrr_mask = ~(smm_region_size() - 1) & rmask; |
| 118 | relo_attrs.smrr_mask |= MTRR_PHYS_MASK_VALID; |
| 119 | |
| 120 | *perm_smbase = relo_attrs.smbase; |
| 121 | *perm_smsize = smm_region_size() - CONFIG_SMM_RESERVED_SIZE; |
| 122 | *smm_save_state_size = sizeof(em64t100_smm_state_save_area_t); |
| 123 | } |
| 124 | |
| 125 | /* The APIC id space on Bay Trail is sparse. Each id is separated by 2. */ |
| 126 | static int adjust_apic_id(int index, int apic_id) |
| 127 | { |
| 128 | return 2 * index; |
| 129 | } |
| 130 | |
| 131 | static void get_microcode_info(const void **microcode, int *parallel) |
| 132 | { |
| 133 | const struct pattrs *pattrs = pattrs_get(); |
| 134 | |
| 135 | *microcode = pattrs->microcode_patch; |
| 136 | *parallel = 1; |
| 137 | } |
| 138 | |
| 139 | static void relocation_handler(int cpu, uintptr_t curr_smbase, |
| 140 | uintptr_t staggered_smbase) |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 141 | { |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 142 | msr_t smrr; |
| 143 | em64t100_smm_state_save_area_t *smm_state; |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 144 | |
| 145 | /* Set up SMRR. */ |
| 146 | smrr.lo = relo_attrs.smrr_base; |
| 147 | smrr.hi = 0; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 148 | wrmsr(SMRR_PHYS_BASE, smrr); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 149 | smrr.lo = relo_attrs.smrr_mask; |
| 150 | smrr.hi = 0; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 151 | wrmsr(SMRR_PHYS_MASK, smrr); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 152 | |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 153 | smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + curr_smbase); |
| 154 | smm_state->smbase = staggered_smbase; |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Aaron Durbin | 0e55632 | 2016-04-29 23:15:12 -0500 | [diff] [blame] | 157 | static void enable_smis(void) |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 158 | { |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 159 | if (IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)) |
| 160 | southcluster_smm_enable_smi(); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 161 | } |
Aaron Durbin | 2a07a4d | 2016-05-03 11:31:32 -0500 | [diff] [blame] | 162 | |
| 163 | static const struct mp_ops mp_ops = { |
| 164 | .pre_mp_init = pre_mp_init, |
| 165 | .get_cpu_count = get_cpu_count, |
| 166 | .get_smm_info = get_smm_info, |
| 167 | .get_microcode_info = get_microcode_info, |
| 168 | .adjust_cpu_apic_entry = adjust_apic_id, |
| 169 | .pre_mp_smm_init = southcluster_smm_clear_state, |
| 170 | .relocation_handler = relocation_handler, |
| 171 | .post_mp_init = enable_smis, |
| 172 | }; |
| 173 | |
| 174 | void baytrail_init_cpus(device_t dev) |
| 175 | { |
| 176 | struct bus *cpu_bus = dev->link_list; |
| 177 | |
| 178 | if (mp_init_with_smm(cpu_bus, &mp_ops)) { |
| 179 | printk(BIOS_ERR, "MP initialization failure.\n"); |
| 180 | } |
| 181 | } |