blob: a8b21406ac06cc47ea6249691ba849cec7ec2527 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +00002/*
Graeme Russdbf71152011-04-13 19:43:26 +10003 * (C) Copyright 2008-2011
4 * Graeme Russ, <graeme.russ@gmail.com>
5 *
wdenk2262cfe2002-11-18 00:14:45 +00006 * (C) Copyright 2002
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02007 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk8bde7f72003-06-27 21:31:46 +00008 *
wdenk2262cfe2002-11-18 00:14:45 +00009 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Marius Groeger <mgroeger@sysgo.de>
12 *
13 * (C) Copyright 2002
14 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
15 * Alex Zuepke <azu@sysgo.de>
16 *
Bin Meng52f952b2014-11-09 22:18:56 +080017 * Part of this file is adapted from coreboot
18 * src/arch/x86/lib/cpu.c
wdenk2262cfe2002-11-18 00:14:45 +000019 */
20
Simon Glass38e498c2020-11-04 09:57:18 -070021#define LOG_CATEGORY UCLASS_CPU
22
Simon Glass52f24232020-05-10 11:40:00 -060023#include <bootstage.h>
wdenk2262cfe2002-11-18 00:14:45 +000024#include <command.h>
Simon Glass9edefc22019-11-14 12:57:37 -070025#include <cpu_func.h>
Bin Meng6e6f4ce2015-06-17 11:15:36 +080026#include <dm.h>
Simon Glass200182a2014-10-10 08:21:55 -060027#include <errno.h>
Simon Glass91caa3b2023-08-21 21:17:01 -060028#include <event.h>
Simon Glass35a3f872019-12-28 10:44:56 -070029#include <init.h>
Simon Glassb95611f2020-07-16 21:22:30 -060030#include <irq.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060031#include <log.h>
Simon Glass200182a2014-10-10 08:21:55 -060032#include <malloc.h>
Bin Mengd8906c12016-06-08 05:07:38 -070033#include <syscon.h>
Simon Glass3cabcf92020-04-08 16:57:35 -060034#include <acpi/acpi_s3.h>
Simon Glass776cc202020-04-08 16:57:36 -060035#include <acpi/acpi_table.h>
Bin Menga0609a82018-07-18 21:42:15 -070036#include <asm/acpi.h>
Stefan Reinauer095593c2012-12-02 04:49:50 +000037#include <asm/control_regs.h>
Bin Mengd19c9072016-05-11 07:45:01 -070038#include <asm/coreboot_tables.h>
Simon Glass200182a2014-10-10 08:21:55 -060039#include <asm/cpu.h>
Simon Glass401d1c42020-10-30 21:38:53 -060040#include <asm/global_data.h>
Bin Meng6e6f4ce2015-06-17 11:15:36 +080041#include <asm/lapic.h>
Simon Glasse77b62e2016-03-11 22:07:11 -070042#include <asm/microcode.h>
Bin Meng6e6f4ce2015-06-17 11:15:36 +080043#include <asm/mp.h>
Bin Meng0c2b7ee2016-05-11 07:45:00 -070044#include <asm/mrccache.h>
Bin Meng43dd22f2015-07-06 16:31:30 +080045#include <asm/msr.h>
46#include <asm/mtrr.h>
Simon Glassa49e3c72014-11-12 22:42:26 -070047#include <asm/post.h>
Graeme Russc53fd2b2011-02-12 15:11:30 +110048#include <asm/processor.h>
Graeme Russ0c24c9c2011-02-12 15:11:32 +110049#include <asm/processor-flags.h>
Graeme Russ3f5f18d2008-12-07 10:29:02 +110050#include <asm/interrupt.h>
Bin Meng5e2400e2015-04-24 18:10:04 +080051#include <asm/tables.h>
Gabe Black60a9b6b2011-11-16 23:32:50 +000052#include <linux/compiler.h>
wdenk2262cfe2002-11-18 00:14:45 +000053
Bin Meng52f952b2014-11-09 22:18:56 +080054DECLARE_GLOBAL_DATA_PTR;
55
Simon Glasscaca13f2019-12-06 21:41:51 -070056#ifndef CONFIG_TPL_BUILD
Bin Meng52f952b2014-11-09 22:18:56 +080057static const char *const x86_vendor_name[] = {
58 [X86_VENDOR_INTEL] = "Intel",
59 [X86_VENDOR_CYRIX] = "Cyrix",
60 [X86_VENDOR_AMD] = "AMD",
61 [X86_VENDOR_UMC] = "UMC",
62 [X86_VENDOR_NEXGEN] = "NexGen",
63 [X86_VENDOR_CENTAUR] = "Centaur",
64 [X86_VENDOR_RISE] = "Rise",
65 [X86_VENDOR_TRANSMETA] = "Transmeta",
66 [X86_VENDOR_NSC] = "NSC",
67 [X86_VENDOR_SIS] = "SiS",
68};
Simon Glasscaca13f2019-12-06 21:41:51 -070069#endif
Bin Meng52f952b2014-11-09 22:18:56 +080070
Gabe Blackf30fc4d2012-10-20 12:33:10 +000071int __weak x86_cleanup_before_linux(void)
72{
Simon Glass99a573f2020-07-17 08:48:20 -060073 int ret;
74
75 ret = mp_park_aps();
76 if (ret)
77 return log_msg_ret("park", ret);
Tom Rinib81e31a2024-07-15 12:42:00 +010078 bootstage_stash_default();
Simon Glass79497032013-04-17 16:13:35 +000079
Gabe Blackf30fc4d2012-10-20 12:33:10 +000080 return 0;
81}
82
Graeme Russd6532442011-12-27 22:46:43 +110083int x86_init_cache(void)
84{
85 enable_caches();
86
wdenk2262cfe2002-11-18 00:14:45 +000087 return 0;
88}
Graeme Russd6532442011-12-27 22:46:43 +110089int init_cache(void) __attribute__((weak, alias("x86_init_cache")));
wdenk2262cfe2002-11-18 00:14:45 +000090
Graeme Russ717979f2011-11-08 02:33:13 +000091void flush_cache(unsigned long dummy1, unsigned long dummy2)
wdenk2262cfe2002-11-18 00:14:45 +000092{
93 asm("wbinvd\n");
wdenk2262cfe2002-11-18 00:14:45 +000094}
Graeme Russ3f5f18d2008-12-07 10:29:02 +110095
Stefan Reinauer095593c2012-12-02 04:49:50 +000096/* Define these functions to allow ehch-hcd to function */
97void flush_dcache_range(unsigned long start, unsigned long stop)
98{
99}
100
101void invalidate_dcache_range(unsigned long start, unsigned long stop)
102{
103}
Simon Glass89371402013-02-28 19:26:11 +0000104
105void dcache_enable(void)
106{
107 enable_caches();
108}
109
110void dcache_disable(void)
111{
112 disable_caches();
113}
114
115void icache_enable(void)
116{
117}
118
119void icache_disable(void)
120{
121}
122
123int icache_status(void)
124{
125 return 1;
126}
Simon Glass7bddac92014-10-10 08:21:52 -0600127
Simon Glasscaca13f2019-12-06 21:41:51 -0700128#ifndef CONFIG_TPL_BUILD
Bin Meng52f952b2014-11-09 22:18:56 +0800129const char *cpu_vendor_name(int vendor)
130{
131 const char *name;
132 name = "<invalid cpu vendor>";
Heinrich Schuchardt39670c32017-11-20 19:45:56 +0100133 if (vendor < ARRAY_SIZE(x86_vendor_name) &&
134 x86_vendor_name[vendor])
Bin Meng52f952b2014-11-09 22:18:56 +0800135 name = x86_vendor_name[vendor];
136
137 return name;
138}
Simon Glasscaca13f2019-12-06 21:41:51 -0700139#endif
Bin Meng52f952b2014-11-09 22:18:56 +0800140
Simon Glass727c1a92014-11-10 18:00:26 -0700141char *cpu_get_name(char *name)
Bin Meng52f952b2014-11-09 22:18:56 +0800142{
Simon Glass727c1a92014-11-10 18:00:26 -0700143 unsigned int *name_as_ints = (unsigned int *)name;
Bin Meng52f952b2014-11-09 22:18:56 +0800144 struct cpuid_result regs;
Simon Glass727c1a92014-11-10 18:00:26 -0700145 char *ptr;
Bin Meng52f952b2014-11-09 22:18:56 +0800146 int i;
147
Simon Glass727c1a92014-11-10 18:00:26 -0700148 /* This bit adds up to 48 bytes */
Bin Meng52f952b2014-11-09 22:18:56 +0800149 for (i = 0; i < 3; i++) {
150 regs = cpuid(0x80000002 + i);
151 name_as_ints[i * 4 + 0] = regs.eax;
152 name_as_ints[i * 4 + 1] = regs.ebx;
153 name_as_ints[i * 4 + 2] = regs.ecx;
154 name_as_ints[i * 4 + 3] = regs.edx;
155 }
Simon Glass727c1a92014-11-10 18:00:26 -0700156 name[CPU_MAX_NAME_LEN - 1] = '\0';
Bin Meng52f952b2014-11-09 22:18:56 +0800157
158 /* Skip leading spaces. */
Simon Glass727c1a92014-11-10 18:00:26 -0700159 ptr = name;
160 while (*ptr == ' ')
161 ptr++;
Bin Meng52f952b2014-11-09 22:18:56 +0800162
Simon Glass727c1a92014-11-10 18:00:26 -0700163 return ptr;
Bin Meng52f952b2014-11-09 22:18:56 +0800164}
165
Andy Shevchenkoc222dda2024-10-05 22:11:57 +0300166#if !CONFIG_IS_ENABLED(CPU)
167int print_cpuinfo(void)
Simon Glass92cc94a2014-10-10 08:21:54 -0600168{
Andy Shevchenkoc222dda2024-10-05 22:11:57 +0300169 post_code(POST_CPU_INFO);
170
Bin Meng52f952b2014-11-09 22:18:56 +0800171 printf("CPU: %s, vendor %s, device %xh\n",
172 cpu_has_64bit() ? "x86_64" : "x86",
173 cpu_vendor_name(gd->arch.x86_vendor), gd->arch.x86_device);
Simon Glass92cc94a2014-10-10 08:21:54 -0600174
Simon Glassef5f5f62020-07-09 18:43:16 -0600175 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
176 debug("ACPI previous sleep state: %s\n",
177 acpi_ss_string(gd->arch.prev_sleep_state));
178 }
Bin Mengb7279612017-04-21 07:24:32 -0700179
Simon Glass92cc94a2014-10-10 08:21:54 -0600180 return 0;
181}
Andy Shevchenkoc222dda2024-10-05 22:11:57 +0300182#endif
Simon Glass200182a2014-10-10 08:21:55 -0600183
Marek Vasutb55881d2021-10-23 03:06:03 +0200184#if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
Simon Glassa49e3c72014-11-12 22:42:26 -0700185void show_boot_progress(int val)
186{
Simon Glassa49e3c72014-11-12 22:42:26 -0700187 outb(val, POST_PORT);
188}
Tom Rinicb80ff22021-05-03 16:48:58 -0400189#endif
Bin Meng5e2400e2015-04-24 18:10:04 +0800190
Simon Glass91caa3b2023-08-21 21:17:01 -0600191#if !defined(CONFIG_SYS_COREBOOT) && !defined(CONFIG_EFI_STUB) && \
Simon Glassbef9fdb2024-09-29 19:49:46 -0600192 !defined(CONFIG_XPL_BUILD)
Bin Meng1e2f7b92016-05-11 07:44:56 -0700193/*
Simon Glass4021ee62020-07-16 21:22:38 -0600194 * Implement a weak default function for boards that need to do some final init
195 * before the system is ready.
Bin Meng1e2f7b92016-05-11 07:44:56 -0700196 */
Simon Glass4021ee62020-07-16 21:22:38 -0600197__weak void board_final_init(void)
Bin Meng1e2f7b92016-05-11 07:44:56 -0700198{
199}
200
Simon Glass7c73cea2020-09-22 12:45:28 -0600201/*
202 * Implement a weak default function for boards that need to do some final
203 * processing before booting the OS.
204 */
205__weak void board_final_cleanup(void)
206{
207}
208
Simon Glass91caa3b2023-08-21 21:17:01 -0600209static int last_stage_init(void)
Bin Meng5e2400e2015-04-24 18:10:04 +0800210{
Bin Meng474a62b2018-07-18 21:42:16 -0700211 struct acpi_fadt __maybe_unused *fadt;
Simon Glass38e498c2020-11-04 09:57:18 -0700212 int ret;
Bin Meng474a62b2018-07-18 21:42:16 -0700213
Simon Glass4021ee62020-07-16 21:22:38 -0600214 board_final_init();
Bin Mengbffd7982017-04-21 07:24:41 -0700215
Simon Glassef5f5f62020-07-09 18:43:16 -0600216 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
217 fadt = acpi_find_fadt();
Bin Meng3a34cae2017-04-21 07:24:37 -0700218
Simon Glassef5f5f62020-07-09 18:43:16 -0600219 if (fadt && gd->arch.prev_sleep_state == ACPI_S3)
220 acpi_resume(fadt);
221 }
Bin Meng3a34cae2017-04-21 07:24:37 -0700222
Simon Glass38e498c2020-11-04 09:57:18 -0700223 ret = write_tables();
224 if (ret) {
225 log_err("Failed to write tables\n");
226 return log_msg_ret("table", ret);
227 }
Bin Meng5e2400e2015-04-24 18:10:04 +0800228
Simon Glass8bccbc52020-07-17 08:48:15 -0600229 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
230 fadt = acpi_find_fadt();
Bin Meng474a62b2018-07-18 21:42:16 -0700231
Simon Glass8bccbc52020-07-17 08:48:15 -0600232 /* Don't touch ACPI hardware on HW reduced platforms */
233 if (fadt && !(fadt->flags & ACPI_FADT_HW_REDUCED_ACPI)) {
234 /*
235 * Other than waiting for OSPM to request us to switch
236 * to ACPI * mode, do it by ourselves, since SMI will
237 * not be triggered.
238 */
239 enter_acpi_mode(fadt->pm1a_cnt_blk);
240 }
Bin Meng474a62b2018-07-18 21:42:16 -0700241 }
Bin Meng474a62b2018-07-18 21:42:16 -0700242
Simon Glass7c73cea2020-09-22 12:45:28 -0600243 /*
244 * TODO(sjg@chromium.org): Move this to bootm_announce_and_cleanup()
245 * once APL FSP-S at 0x200000 does not overlap with the bzimage at
246 * 0x100000.
247 */
248 board_final_cleanup();
249
Bin Meng5e2400e2015-04-24 18:10:04 +0800250 return 0;
251}
Simon Glass91caa3b2023-08-21 21:17:01 -0600252EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
253
Simon Glassbef9fdb2024-09-29 19:49:46 -0600254#endif /* !SYS_COREBOOT && !EFI_STUB && !XPL_BUILD */
Simon Glassbcb0c612015-04-29 22:26:01 -0600255
Simon Glassafd5d502016-01-17 16:11:28 -0700256static int x86_init_cpus(void)
Simon Glassbcb0c612015-04-29 22:26:01 -0600257{
Simon Glass8bccbc52020-07-17 08:48:15 -0600258 if (IS_ENABLED(CONFIG_SMP)) {
259 debug("Init additional CPUs\n");
260 x86_mp_init();
261 } else {
262 struct udevice *dev;
Bin Mengc77b8912015-07-22 01:21:12 -0700263
Simon Glass8bccbc52020-07-17 08:48:15 -0600264 /*
265 * This causes the cpu-x86 driver to be probed.
266 * We don't check return value here as we want to allow boards
267 * which have not been converted to use cpu uclass driver to
268 * boot.
269 */
270 uclass_first_device(UCLASS_CPU, &dev);
271 }
Bin Meng6e6f4ce2015-06-17 11:15:36 +0800272
Simon Glassbcb0c612015-04-29 22:26:01 -0600273 return 0;
274}
275
276int cpu_init_r(void)
277{
Simon Glassac643e02016-01-17 16:11:30 -0700278 struct udevice *dev;
279 int ret;
280
Simon Glass526aabe2020-04-26 09:12:55 -0600281 if (!ll_boot_init()) {
282 uclass_first_device(UCLASS_PCI, &dev);
Simon Glassac643e02016-01-17 16:11:30 -0700283 return 0;
Simon Glass526aabe2020-04-26 09:12:55 -0600284 }
Simon Glassac643e02016-01-17 16:11:30 -0700285
286 ret = x86_init_cpus();
287 if (ret)
288 return ret;
289
290 /*
291 * Set up the northbridge, PCH and LPC if available. Note that these
292 * may have had some limited pre-relocation init if they were probed
293 * before relocation, but this is post relocation.
294 */
295 uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
296 uclass_first_device(UCLASS_PCH, &dev);
297 uclass_first_device(UCLASS_LPC, &dev);
Simon Glasse49ccea2015-08-04 12:34:00 -0600298
Bin Mengd8906c12016-06-08 05:07:38 -0700299 /* Set up pin control if available */
300 ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &dev);
301 debug("%s, pinctrl=%p, ret=%d\n", __func__, dev, ret);
302
Simon Glasse49ccea2015-08-04 12:34:00 -0600303 return 0;
Simon Glassbcb0c612015-04-29 22:26:01 -0600304}
Bin Meng0c2b7ee2016-05-11 07:45:00 -0700305
306#ifndef CONFIG_EFI_STUB
307int reserve_arch(void)
308{
Simon Glassb95611f2020-07-16 21:22:30 -0600309 struct udevice *itss;
310 int ret;
311
312 if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE))
313 mrccache_reserve();
Bin Mengd19c9072016-05-11 07:45:01 -0700314
Simon Glass8bccbc52020-07-17 08:48:15 -0600315 if (IS_ENABLED(CONFIG_SEABIOS))
316 high_table_reserve();
Bin Mengd19c9072016-05-11 07:45:01 -0700317
Simon Glassef5f5f62020-07-09 18:43:16 -0600318 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
319 acpi_s3_reserve();
Bin Meng5ae5aa92017-04-21 07:24:47 -0700320
Simon Glassef5f5f62020-07-09 18:43:16 -0600321 if (IS_ENABLED(CONFIG_HAVE_FSP)) {
322 /*
323 * Save stack address to CMOS so that at next S3 boot,
Bin Meng58e2a352022-11-24 11:39:23 +0800324 * we can use it as the stack address for fsp_continue()
Simon Glassef5f5f62020-07-09 18:43:16 -0600325 */
326 fsp_save_s3_stack();
327 }
328 }
Simon Glassb95611f2020-07-16 21:22:30 -0600329 ret = irq_first_device_type(X86_IRQT_ITSS, &itss);
330 if (!ret) {
331 /*
332 * Snapshot the current GPIO IRQ polarities. FSP-S is about to
333 * run and will set a default policy that doesn't honour boards'
334 * requirements
335 */
336 irq_snapshot_polarities(itss);
337 }
Bin Mengba658082017-04-21 07:24:39 -0700338
Bin Mengd19c9072016-05-11 07:45:01 -0700339 return 0;
Bin Meng0c2b7ee2016-05-11 07:45:00 -0700340}
341#endif
Simon Glass7ec0e7b2020-04-30 21:21:39 -0600342
Andy Shevchenko2fb5cc22024-10-05 22:11:58 +0300343static long detect_coreboot_table_at(ulong start, ulong size)
Simon Glass7ec0e7b2020-04-30 21:21:39 -0600344{
345 u32 *ptr, *end;
346
347 size /= 4;
348 for (ptr = (void *)start, end = ptr + size; ptr < end; ptr += 4) {
349 if (*ptr == 0x4f49424c) /* "LBIO" */
350 return (long)ptr;
351 }
352
353 return -ENOENT;
354}
355
356long locate_coreboot_table(void)
357{
358 long addr;
359
Simon Glass7a187a82023-05-04 16:54:55 -0600360 /* We look for LBIO from addresses 1K-4K and again at 960KB */
361 addr = detect_coreboot_table_at(0x400, 0xc00);
Simon Glass7ec0e7b2020-04-30 21:21:39 -0600362 if (addr < 0)
363 addr = detect_coreboot_table_at(0xf0000, 0x1000);
364
365 return addr;
366}