blob: 21818e4c885d459da97f6f5fded4489c519447da [file] [log] [blame]
Martin Roth5c354b92019-04-22 14:55:16 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015-2016 Intel Corp.
Marshall Dawson34c30562019-07-16 15:18:00 -06005 * Copyright (C) 2017-2019 Advanced Micro Devices, Inc.
Martin Roth5c354b92019-04-22 14:55:16 -06006 *
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.
15 */
16
17#include <cpu/cpu.h>
18#include <cpu/x86/mp.h>
19#include <cpu/x86/mtrr.h>
20#include <cpu/x86/msr.h>
Kyösti Mälkkib2a5f0b2019-08-04 19:54:32 +030021#include <cpu/x86/smm.h>
Martin Roth5c354b92019-04-22 14:55:16 -060022#include <cpu/amd/msr.h>
23#include <cpu/x86/lapic.h>
24#include <device/device.h>
25#include <device/pci_ops.h>
26#include <soc/pci_devs.h>
27#include <soc/cpu.h>
28#include <soc/northbridge.h>
29#include <soc/smi.h>
30#include <soc/iomap.h>
31#include <console/console.h>
32
33/*
34 * MP and SMM loading initialization.
35 */
36struct smm_relocation_attrs {
37 uint32_t smbase;
38 uint32_t tseg_base;
39 uint32_t tseg_mask;
40};
41
42static struct smm_relocation_attrs relo_attrs;
43
44/*
45 * Do essential initialization tasks before APs can be fired up -
46 *
47 * 1. Prevent race condition in MTRR solution. Enable MTRRs on the BSP. This
48 * creates the MTRR solution that the APs will use. Otherwise APs will try to
49 * apply the incomplete solution as the BSP is calculating it.
50 */
51static void pre_mp_init(void)
52{
53 x86_setup_mtrrs_with_detect();
54 x86_mtrr_check();
55}
56
Marshall Dawson34c30562019-07-16 15:18:00 -060057int get_cpu_count(void)
Martin Roth5c354b92019-04-22 14:55:16 -060058{
Marshall Dawson34c30562019-07-16 15:18:00 -060059 return 1 + (cpuid_ecx(0x80000008) & 0xff);
Martin Roth5c354b92019-04-22 14:55:16 -060060}
61
62static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
63 size_t *smm_save_state_size)
64{
Kyösti Mälkki14222d82019-08-05 15:10:18 +030065 uintptr_t smm_base;
Martin Roth5c354b92019-04-22 14:55:16 -060066 size_t smm_size;
Kyösti Mälkki14222d82019-08-05 15:10:18 +030067 uintptr_t handler_base;
Martin Roth5c354b92019-04-22 14:55:16 -060068 size_t handler_size;
69
70 /* Initialize global tracking state. */
Kyösti Mälkki7db852a2019-08-04 20:26:53 +030071 smm_region(&smm_base, &smm_size);
Martin Roth5c354b92019-04-22 14:55:16 -060072 smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
73
Kyösti Mälkki14222d82019-08-05 15:10:18 +030074 relo_attrs.smbase = smm_base;
Martin Roth5c354b92019-04-22 14:55:16 -060075 relo_attrs.tseg_base = relo_attrs.smbase;
76 relo_attrs.tseg_mask = ALIGN_DOWN(~(smm_size - 1), 128 * KiB);
77 relo_attrs.tseg_mask |= SMM_TSEG_WB;
78
Kyösti Mälkki14222d82019-08-05 15:10:18 +030079 *perm_smbase = handler_base;
Martin Roth5c354b92019-04-22 14:55:16 -060080 *perm_smsize = handler_size;
81 *smm_save_state_size = sizeof(amd64_smm_state_save_area_t);
82}
83
84static void relocation_handler(int cpu, uintptr_t curr_smbase,
85 uintptr_t staggered_smbase)
86{
87 msr_t tseg_base, tseg_mask;
88 amd64_smm_state_save_area_t *smm_state;
89
90 tseg_base.lo = relo_attrs.tseg_base;
91 tseg_base.hi = 0;
92 wrmsr(SMM_ADDR_MSR, tseg_base);
93 tseg_mask.lo = relo_attrs.tseg_mask;
94 tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1);
95 wrmsr(SMM_MASK_MSR, tseg_mask);
96 smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase);
97 smm_state->smbase = staggered_smbase;
98}
99
100static const struct mp_ops mp_ops = {
101 .pre_mp_init = pre_mp_init,
102 .get_cpu_count = get_cpu_count,
103 .get_smm_info = get_smm_info,
104 .relocation_handler = relocation_handler,
105 .post_mp_init = enable_smi_generation,
106};
107
Marshall Dawsonbc4c9032019-06-11 12:18:20 -0600108void picasso_init_cpus(struct device *dev)
Martin Roth5c354b92019-04-22 14:55:16 -0600109{
110 /* Clear for take-off */
111 if (mp_init_with_smm(dev->link_list, &mp_ops) < 0)
112 printk(BIOS_ERR, "MP initialization failure.\n");
113
114 /* The flash is now no longer cacheable. Reset to WP for performance. */
115 mtrr_use_temp_range(FLASH_BASE_ADDR, CONFIG_ROM_SIZE, MTRR_TYPE_WRPROT);
116
117 set_warm_reset_flag();
118}
119
Marshall Dawson34c30562019-07-16 15:18:00 -0600120static void model_17_init(struct device *dev)
Martin Roth5c354b92019-04-22 14:55:16 -0600121{
122 check_mca();
123 setup_lapic();
Martin Roth5c354b92019-04-22 14:55:16 -0600124}
125
126static struct device_operations cpu_dev_ops = {
Marshall Dawson34c30562019-07-16 15:18:00 -0600127 .init = model_17_init,
Martin Roth5c354b92019-04-22 14:55:16 -0600128};
129
130static struct cpu_device_id cpu_table[] = {
Marshall Dawson34c30562019-07-16 15:18:00 -0600131 { X86_VENDOR_AMD, 0x810f81 },
Martin Roth5c354b92019-04-22 14:55:16 -0600132 { 0, 0 },
133};
134
Marshall Dawson34c30562019-07-16 15:18:00 -0600135static const struct cpu_driver model_17 __cpu_driver = {
Martin Roth5c354b92019-04-22 14:55:16 -0600136 .ops = &cpu_dev_ops,
137 .id_table = cpu_table,
138};