blob: 9961153b0bd69b10bee4631a52bd04d4ffbfc060 [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>
Kyösti Mälkkib2a5f0b2019-08-04 19:54:32 +030021#include <cpu/x86/smm.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020022#include <cpu/amd/msr.h>
Marshall Dawson178e65d2017-10-20 13:20:25 -060023#include <cpu/x86/lapic.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060024#include <device/device.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020025#include <device/pci_ops.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060026#include <soc/pci_devs.h>
27#include <soc/cpu.h>
28#include <soc/northbridge.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060029#include <soc/smi.h>
Marshall Dawson0814b122018-01-10 11:35:24 -070030#include <soc/iomap.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060031#include <console/console.h>
32
33/*
Marshall Dawsonb6172112017-09-13 17:47:31 -060034 * 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/*
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060045 * 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
57static int get_cpu_count(void)
58{
Martin Roth1956a002018-10-30 22:31:40 -060059 return (pci_read_config16(SOC_HT_DEV, D18F0_CPU_CNT) & CPU_CNT_MASK)
Richard Spiegel41baf0c2018-10-22 13:57:18 -070060 + 1;
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060061}
62
Marshall Dawsonb6172112017-09-13 17:47:31 -060063static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
64 size_t *smm_save_state_size)
65{
66 void *smm_base;
67 size_t smm_size;
68 void *handler_base;
69 size_t handler_size;
70
71 /* Initialize global tracking state. */
72 smm_region_info(&smm_base, &smm_size);
73 smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
74
75 relo_attrs.smbase = (uint32_t)smm_base;
76 relo_attrs.tseg_base = relo_attrs.smbase;
77 relo_attrs.tseg_mask = ALIGN_DOWN(~(smm_size - 1), 128 * KiB);
Marshall Dawson2a5e15c2018-01-24 12:07:11 -070078 relo_attrs.tseg_mask |= SMM_TSEG_WB;
Marshall Dawsonb6172112017-09-13 17:47:31 -060079
80 *perm_smbase = (uintptr_t)handler_base;
81 *perm_smsize = handler_size;
82 *smm_save_state_size = sizeof(amd64_smm_state_save_area_t);
83}
84
85static void relocation_handler(int cpu, uintptr_t curr_smbase,
86 uintptr_t staggered_smbase)
87{
88 msr_t tseg_base, tseg_mask;
89 amd64_smm_state_save_area_t *smm_state;
90
91 tseg_base.lo = relo_attrs.tseg_base;
92 tseg_base.hi = 0;
Elyes HAOUAS400ce552018-10-12 10:54:30 +020093 wrmsr(SMM_ADDR_MSR, tseg_base);
Marshall Dawsonb6172112017-09-13 17:47:31 -060094 tseg_mask.lo = relo_attrs.tseg_mask;
95 tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1);
Elyes HAOUAS400ce552018-10-12 10:54:30 +020096 wrmsr(SMM_MASK_MSR, tseg_mask);
Marshall Dawsonb6172112017-09-13 17:47:31 -060097 smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase);
98 smm_state->smbase = staggered_smbase;
99}
100
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600101static const struct mp_ops mp_ops = {
102 .pre_mp_init = pre_mp_init,
103 .get_cpu_count = get_cpu_count,
Marshall Dawsonb6172112017-09-13 17:47:31 -0600104 .get_smm_info = get_smm_info,
105 .relocation_handler = relocation_handler,
106 .post_mp_init = enable_smi_generation,
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600107};
108
109void stoney_init_cpus(struct device *dev)
110{
111 /* Clear for take-off */
112 if (mp_init_with_smm(dev->link_list, &mp_ops) < 0)
113 printk(BIOS_ERR, "MP initialization failure.\n");
Marshall Dawson8f031d82018-04-09 22:15:06 -0600114
115 /* The flash is now no longer cacheable. Reset to WP for performance. */
116 mtrr_use_temp_range(FLASH_BASE_ADDR, CONFIG_ROM_SIZE, MTRR_TYPE_WRPROT);
Marshall Dawson2e49cf122018-08-03 17:05:22 -0600117
118 set_warm_reset_flag();
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600119}
Marshall Dawson178e65d2017-10-20 13:20:25 -0600120
Marshall Dawson74473ec2018-08-05 10:42:17 -0600121static void model_15_init(struct device *dev)
122{
123 check_mca();
Marshall Dawson178e65d2017-10-20 13:20:25 -0600124 setup_lapic();
Marshall Dawson638bd132018-09-14 10:16:40 -0600125
126 /*
127 * Per AMD, sync an undocumented MSR with the PSP base address.
128 * Experiments showed that if you write to the MSR after it has
129 * been previously programmed, it causes a general protection fault.
130 * Also, the MSR survives warm reset and S3 cycles, so we need to
131 * test if it was previously written before writing to it.
132 */
133 msr_t psp_msr;
134 uint32_t psp_bar; /* Note: NDA BKDG names this 32-bit register BAR3 */
135 psp_bar = pci_read_config32(SOC_PSP_DEV, PCI_BASE_ADDRESS_4);
136 psp_bar &= ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
137 psp_msr = rdmsr(0xc00110a2);
138 if (psp_msr.lo == 0) {
139 psp_msr.lo = psp_bar;
140 wrmsr(0xc00110a2, psp_msr);
141 }
Marshall Dawson178e65d2017-10-20 13:20:25 -0600142}
143
144static struct device_operations cpu_dev_ops = {
145 .init = model_15_init,
146};
147
148static struct cpu_device_id cpu_table[] = {
Richard Spiegel9247e862019-06-28 09:18:47 -0700149 { X86_VENDOR_AMD, 0x660f01 },
Marshall Dawson178e65d2017-10-20 13:20:25 -0600150 { X86_VENDOR_AMD, 0x670f00 },
151 { 0, 0 },
152};
153
154static const struct cpu_driver model_15 __cpu_driver = {
155 .ops = &cpu_dev_ops,
156 .id_table = cpu_table,
157};