blob: 52b1c9cbb165053f2641b4e52819f1881c4a793a [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>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020024#include <device/pci_ops.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060025#include <soc/pci_devs.h>
26#include <soc/cpu.h>
27#include <soc/northbridge.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060028#include <soc/smi.h>
Marshall Dawson0814b122018-01-10 11:35:24 -070029#include <soc/iomap.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060030#include <console/console.h>
31
32/*
Marshall Dawsonb6172112017-09-13 17:47:31 -060033 * MP and SMM loading initialization.
34 */
35struct smm_relocation_attrs {
36 uint32_t smbase;
37 uint32_t tseg_base;
38 uint32_t tseg_mask;
39};
40
41static struct smm_relocation_attrs relo_attrs;
42
43/*
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060044 * Do essential initialization tasks before APs can be fired up -
45 *
46 * 1. Prevent race condition in MTRR solution. Enable MTRRs on the BSP. This
47 * creates the MTRR solution that the APs will use. Otherwise APs will try to
48 * apply the incomplete solution as the BSP is calculating it.
49 */
50static void pre_mp_init(void)
51{
52 x86_setup_mtrrs_with_detect();
53 x86_mtrr_check();
54}
55
56static int get_cpu_count(void)
57{
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020058 struct device *nb = dev_find_slot(0, HT_DEVFN);
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060059 return (pci_read_config16(nb, D18F0_CPU_CNT) & CPU_CNT_MASK) + 1;
60}
61
Marshall Dawsonb6172112017-09-13 17:47:31 -060062static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
63 size_t *smm_save_state_size)
64{
65 void *smm_base;
66 size_t smm_size;
67 void *handler_base;
68 size_t handler_size;
69
70 /* Initialize global tracking state. */
71 smm_region_info(&smm_base, &smm_size);
72 smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
73
74 relo_attrs.smbase = (uint32_t)smm_base;
75 relo_attrs.tseg_base = relo_attrs.smbase;
76 relo_attrs.tseg_mask = ALIGN_DOWN(~(smm_size - 1), 128 * KiB);
Marshall Dawson2a5e15c2018-01-24 12:07:11 -070077 relo_attrs.tseg_mask |= SMM_TSEG_WB;
Marshall Dawsonb6172112017-09-13 17:47:31 -060078
79 *perm_smbase = (uintptr_t)handler_base;
80 *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(MSR_TSEG_BASE, tseg_base);
93 tseg_mask.lo = relo_attrs.tseg_mask;
94 tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1);
95 wrmsr(MSR_SMM_MASK, tseg_mask);
96 smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase);
97 smm_state->smbase = staggered_smbase;
98}
99
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600100static const struct mp_ops mp_ops = {
101 .pre_mp_init = pre_mp_init,
102 .get_cpu_count = get_cpu_count,
Marshall Dawsonb6172112017-09-13 17:47:31 -0600103 .get_smm_info = get_smm_info,
104 .relocation_handler = relocation_handler,
105 .post_mp_init = enable_smi_generation,
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600106};
107
108void stoney_init_cpus(struct device *dev)
109{
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");
Marshall Dawson8f031d82018-04-09 22:15:06 -0600113
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);
Marshall Dawson2e49cf122018-08-03 17:05:22 -0600116
117 set_warm_reset_flag();
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600118}
Marshall Dawson178e65d2017-10-20 13:20:25 -0600119
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200120static void model_15_init(struct device *dev)
Marshall Dawson178e65d2017-10-20 13:20:25 -0600121{
122 printk(BIOS_DEBUG, "Model 15 Init.\n");
123
124 int i;
125 msr_t msr;
Marshall Dawsonbd4a3f82018-08-07 07:27:57 -0600126 int num_banks;
Marshall Dawson178e65d2017-10-20 13:20:25 -0600127
128 /* zero the machine check error status registers */
Marshall Dawsonbd4a3f82018-08-07 07:27:57 -0600129 msr = rdmsr(MCG_CAP);
130 num_banks = msr.lo & MCA_BANKS_MASK;
Marshall Dawson178e65d2017-10-20 13:20:25 -0600131 msr.lo = 0;
132 msr.hi = 0;
Marshall Dawsonbd4a3f82018-08-07 07:27:57 -0600133 for (i = 0 ; i < num_banks ; i++)
Marshall Dawsonbddd1572018-08-07 07:17:41 -0600134 wrmsr(MC0_STATUS + (i * 4), msr);
Marshall Dawson178e65d2017-10-20 13:20:25 -0600135
136 setup_lapic();
137}
138
139static struct device_operations cpu_dev_ops = {
140 .init = model_15_init,
141};
142
143static struct cpu_device_id cpu_table[] = {
144 { X86_VENDOR_AMD, 0x670f00 },
145 { 0, 0 },
146};
147
148static const struct cpu_driver model_15 __cpu_driver = {
149 .ops = &cpu_dev_ops,
150 .id_table = cpu_table,
151};