blob: 15dd38147ce79ba50bf55da6b9cb0030e6a75aa7 [file] [log] [blame]
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015-2016 Intel Corp.
5 * Copyright (C) 2017 Advanced Micro Devices, Inc.
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.
15 */
16
Marshall Dawsonb6172112017-09-13 17:47:31 -060017#include <cpu/cpu.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060018#include <cpu/x86/mp.h>
19#include <cpu/x86/mtrr.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060020#include <cpu/x86/msr.h>
Marshall Dawson178e65d2017-10-20 13:20:25 -060021#include <cpu/x86/lapic.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060022#include <cpu/amd/amdfam15.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060023#include <device/device.h>
24#include <soc/pci_devs.h>
25#include <soc/cpu.h>
26#include <soc/northbridge.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060027#include <soc/smi.h>
Marshall Dawson0814b122018-01-10 11:35:24 -070028#include <soc/iomap.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060029#include <console/console.h>
30
31/*
Marshall Dawsonb6172112017-09-13 17:47:31 -060032 * MP and SMM loading initialization.
33 */
34struct smm_relocation_attrs {
35 uint32_t smbase;
36 uint32_t tseg_base;
37 uint32_t tseg_mask;
38};
39
40static struct smm_relocation_attrs relo_attrs;
41
42/*
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060043 * Do essential initialization tasks before APs can be fired up -
44 *
45 * 1. Prevent race condition in MTRR solution. Enable MTRRs on the BSP. This
46 * creates the MTRR solution that the APs will use. Otherwise APs will try to
47 * apply the incomplete solution as the BSP is calculating it.
48 */
49static void pre_mp_init(void)
50{
51 x86_setup_mtrrs_with_detect();
52 x86_mtrr_check();
53}
54
55static int get_cpu_count(void)
56{
57 device_t nb = dev_find_slot(0, HT_DEVFN);
58 return (pci_read_config16(nb, D18F0_CPU_CNT) & CPU_CNT_MASK) + 1;
59}
60
Marshall Dawsonb6172112017-09-13 17:47:31 -060061static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
62 size_t *smm_save_state_size)
63{
64 void *smm_base;
65 size_t smm_size;
66 void *handler_base;
67 size_t handler_size;
68
69 /* Initialize global tracking state. */
70 smm_region_info(&smm_base, &smm_size);
71 smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
72
73 relo_attrs.smbase = (uint32_t)smm_base;
74 relo_attrs.tseg_base = relo_attrs.smbase;
75 relo_attrs.tseg_mask = ALIGN_DOWN(~(smm_size - 1), 128 * KiB);
Marshall Dawson2a5e15c2018-01-24 12:07:11 -070076 relo_attrs.tseg_mask |= SMM_TSEG_WB;
Marshall Dawsonb6172112017-09-13 17:47:31 -060077
78 *perm_smbase = (uintptr_t)handler_base;
79 *perm_smsize = handler_size;
80 *smm_save_state_size = sizeof(amd64_smm_state_save_area_t);
81}
82
83static void relocation_handler(int cpu, uintptr_t curr_smbase,
84 uintptr_t staggered_smbase)
85{
86 msr_t tseg_base, tseg_mask;
87 amd64_smm_state_save_area_t *smm_state;
88
89 tseg_base.lo = relo_attrs.tseg_base;
90 tseg_base.hi = 0;
91 wrmsr(MSR_TSEG_BASE, tseg_base);
92 tseg_mask.lo = relo_attrs.tseg_mask;
93 tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1);
94 wrmsr(MSR_SMM_MASK, tseg_mask);
95 smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase);
96 smm_state->smbase = staggered_smbase;
97}
98
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060099static const struct mp_ops mp_ops = {
100 .pre_mp_init = pre_mp_init,
101 .get_cpu_count = get_cpu_count,
Marshall Dawsonb6172112017-09-13 17:47:31 -0600102 .get_smm_info = get_smm_info,
103 .relocation_handler = relocation_handler,
104 .post_mp_init = enable_smi_generation,
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600105};
106
107void stoney_init_cpus(struct device *dev)
108{
109 /* Clear for take-off */
110 if (mp_init_with_smm(dev->link_list, &mp_ops) < 0)
111 printk(BIOS_ERR, "MP initialization failure.\n");
Marshall Dawson8f031d82018-04-09 22:15:06 -0600112
113 /* The flash is now no longer cacheable. Reset to WP for performance. */
114 mtrr_use_temp_range(FLASH_BASE_ADDR, CONFIG_ROM_SIZE, MTRR_TYPE_WRPROT);
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600115}
Marshall Dawson178e65d2017-10-20 13:20:25 -0600116
117static void model_15_init(device_t dev)
118{
119 printk(BIOS_DEBUG, "Model 15 Init.\n");
120
121 int i;
122 msr_t msr;
123
124 /* zero the machine check error status registers */
125 msr.lo = 0;
126 msr.hi = 0;
127 for (i = 0 ; i < 6 ; i++)
128 wrmsr(MCI_STATUS + (i * 4), msr);
129
130 setup_lapic();
131}
132
133static struct device_operations cpu_dev_ops = {
134 .init = model_15_init,
135};
136
137static struct cpu_device_id cpu_table[] = {
138 { X86_VENDOR_AMD, 0x670f00 },
139 { 0, 0 },
140};
141
142static const struct cpu_driver model_15 __cpu_driver = {
143 .ops = &cpu_dev_ops,
144 .id_table = cpu_table,
145};