Angel Pons | ae59387 | 2020-04-04 18:50:57 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 2 | |
| 3 | #include <cpu/cpu.h> |
| 4 | #include <cpu/x86/mp.h> |
| 5 | #include <cpu/x86/mtrr.h> |
| 6 | #include <cpu/x86/msr.h> |
Kyösti Mälkki | b2a5f0b | 2019-08-04 19:54:32 +0300 | [diff] [blame] | 7 | #include <cpu/x86/smm.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 8 | #include <cpu/amd/msr.h> |
Kyösti Mälkki | e31ec29 | 2019-08-10 17:27:01 +0300 | [diff] [blame] | 9 | #include <cpu/amd/amd64_save_state.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 10 | #include <cpu/x86/lapic.h> |
| 11 | #include <device/device.h> |
| 12 | #include <device/pci_ops.h> |
| 13 | #include <soc/pci_devs.h> |
| 14 | #include <soc/cpu.h> |
Raul E Rangel | cd39a41 | 2020-05-07 15:16:15 -0600 | [diff] [blame^] | 15 | #include <soc/reset.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 16 | #include <soc/smi.h> |
| 17 | #include <soc/iomap.h> |
| 18 | #include <console/console.h> |
| 19 | |
| 20 | /* |
| 21 | * MP and SMM loading initialization. |
| 22 | */ |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 23 | struct smm_relocation_params { |
| 24 | msr_t tseg_base; |
| 25 | msr_t tseg_mask; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 26 | }; |
| 27 | |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 28 | static struct smm_relocation_params smm_reloc_params; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Do essential initialization tasks before APs can be fired up - |
| 32 | * |
| 33 | * 1. Prevent race condition in MTRR solution. Enable MTRRs on the BSP. This |
| 34 | * creates the MTRR solution that the APs will use. Otherwise APs will try to |
| 35 | * apply the incomplete solution as the BSP is calculating it. |
| 36 | */ |
| 37 | static void pre_mp_init(void) |
| 38 | { |
| 39 | x86_setup_mtrrs_with_detect(); |
| 40 | x86_mtrr_check(); |
| 41 | } |
| 42 | |
Marshall Dawson | 34c3056 | 2019-07-16 15:18:00 -0600 | [diff] [blame] | 43 | int get_cpu_count(void) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 44 | { |
Marshall Dawson | 34c3056 | 2019-07-16 15:18:00 -0600 | [diff] [blame] | 45 | return 1 + (cpuid_ecx(0x80000008) & 0xff); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 46 | } |
| 47 | |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 48 | static void fill_in_relocation_params(struct smm_relocation_params *params) |
| 49 | { |
| 50 | uintptr_t tseg_base; |
| 51 | size_t tseg_size; |
| 52 | |
| 53 | smm_region(&tseg_base, &tseg_size); |
| 54 | |
| 55 | params->tseg_base.lo = ALIGN_DOWN(tseg_base, 128 * KiB); |
| 56 | params->tseg_base.hi = 0; |
| 57 | params->tseg_mask.lo = ALIGN_DOWN(~(tseg_size - 1), 128 * KiB); |
| 58 | params->tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1); |
| 59 | |
| 60 | params->tseg_mask.lo |= SMM_TSEG_WB; |
| 61 | } |
| 62 | |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 63 | static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize, |
| 64 | size_t *smm_save_state_size) |
| 65 | { |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 66 | printk(BIOS_DEBUG, "Setting up SMI for CPU\n"); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 67 | |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 68 | fill_in_relocation_params(&smm_reloc_params); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 69 | |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 70 | smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 71 | *smm_save_state_size = sizeof(amd64_smm_state_save_area_t); |
| 72 | } |
| 73 | |
| 74 | static void relocation_handler(int cpu, uintptr_t curr_smbase, |
| 75 | uintptr_t staggered_smbase) |
| 76 | { |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 77 | struct smm_relocation_params *relo_params = &smm_reloc_params; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 78 | amd64_smm_state_save_area_t *smm_state; |
| 79 | |
Kyösti Mälkki | 8699724 | 2019-08-06 01:44:58 +0300 | [diff] [blame] | 80 | wrmsr(SMM_ADDR_MSR, relo_params->tseg_base); |
| 81 | wrmsr(SMM_MASK_MSR, relo_params->tseg_mask); |
| 82 | |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 83 | smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase); |
| 84 | smm_state->smbase = staggered_smbase; |
| 85 | } |
| 86 | |
| 87 | static const struct mp_ops mp_ops = { |
| 88 | .pre_mp_init = pre_mp_init, |
| 89 | .get_cpu_count = get_cpu_count, |
| 90 | .get_smm_info = get_smm_info, |
| 91 | .relocation_handler = relocation_handler, |
| 92 | .post_mp_init = enable_smi_generation, |
| 93 | }; |
| 94 | |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 95 | void picasso_init_cpus(struct device *dev) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 96 | { |
| 97 | /* Clear for take-off */ |
| 98 | if (mp_init_with_smm(dev->link_list, &mp_ops) < 0) |
| 99 | printk(BIOS_ERR, "MP initialization failure.\n"); |
| 100 | |
| 101 | /* The flash is now no longer cacheable. Reset to WP for performance. */ |
| 102 | mtrr_use_temp_range(FLASH_BASE_ADDR, CONFIG_ROM_SIZE, MTRR_TYPE_WRPROT); |
| 103 | |
| 104 | set_warm_reset_flag(); |
| 105 | } |
| 106 | |
Marshall Dawson | 34c3056 | 2019-07-16 15:18:00 -0600 | [diff] [blame] | 107 | static void model_17_init(struct device *dev) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 108 | { |
| 109 | check_mca(); |
| 110 | setup_lapic(); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static struct device_operations cpu_dev_ops = { |
Marshall Dawson | 34c3056 | 2019-07-16 15:18:00 -0600 | [diff] [blame] | 114 | .init = model_17_init, |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static struct cpu_device_id cpu_table[] = { |
Marshall Dawson | 04b4177 | 2019-09-04 09:40:50 -0600 | [diff] [blame] | 118 | { X86_VENDOR_AMD, 0x810f80 }, |
Martin Roth | eb30e1a | 2019-12-10 21:50:10 -0700 | [diff] [blame] | 119 | { X86_VENDOR_AMD, PICASSO_CPUID }, |
| 120 | { X86_VENDOR_AMD, RAVEN2_CPUID }, |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 121 | { 0, 0 }, |
| 122 | }; |
| 123 | |
Marshall Dawson | 34c3056 | 2019-07-16 15:18:00 -0600 | [diff] [blame] | 124 | static const struct cpu_driver model_17 __cpu_driver = { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 125 | .ops = &cpu_dev_ops, |
| 126 | .id_table = cpu_table, |
| 127 | }; |