Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / x86 / cpu / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008-2011
4  * Graeme Russ, <graeme.russ@gmail.com>
5  *
6  * (C) Copyright 2002
7  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
8  *
9  * (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  *
17  * Part of this file is adapted from coreboot
18  * src/arch/x86/lib/cpu.c
19  */
20
21 #include <common.h>
22 #include <bootstage.h>
23 #include <command.h>
24 #include <cpu_func.h>
25 #include <dm.h>
26 #include <errno.h>
27 #include <init.h>
28 #include <log.h>
29 #include <malloc.h>
30 #include <syscon.h>
31 #include <acpi/acpi_s3.h>
32 #include <acpi/acpi_table.h>
33 #include <asm/acpi.h>
34 #include <asm/control_regs.h>
35 #include <asm/coreboot_tables.h>
36 #include <asm/cpu.h>
37 #include <asm/lapic.h>
38 #include <asm/microcode.h>
39 #include <asm/mp.h>
40 #include <asm/mrccache.h>
41 #include <asm/msr.h>
42 #include <asm/mtrr.h>
43 #include <asm/post.h>
44 #include <asm/processor.h>
45 #include <asm/processor-flags.h>
46 #include <asm/interrupt.h>
47 #include <asm/tables.h>
48 #include <linux/compiler.h>
49
50 DECLARE_GLOBAL_DATA_PTR;
51
52 #ifndef CONFIG_TPL_BUILD
53 static const char *const x86_vendor_name[] = {
54         [X86_VENDOR_INTEL]     = "Intel",
55         [X86_VENDOR_CYRIX]     = "Cyrix",
56         [X86_VENDOR_AMD]       = "AMD",
57         [X86_VENDOR_UMC]       = "UMC",
58         [X86_VENDOR_NEXGEN]    = "NexGen",
59         [X86_VENDOR_CENTAUR]   = "Centaur",
60         [X86_VENDOR_RISE]      = "Rise",
61         [X86_VENDOR_TRANSMETA] = "Transmeta",
62         [X86_VENDOR_NSC]       = "NSC",
63         [X86_VENDOR_SIS]       = "SiS",
64 };
65 #endif
66
67 int __weak x86_cleanup_before_linux(void)
68 {
69 #ifdef CONFIG_BOOTSTAGE_STASH
70         bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
71                         CONFIG_BOOTSTAGE_STASH_SIZE);
72 #endif
73
74         return 0;
75 }
76
77 int x86_init_cache(void)
78 {
79         enable_caches();
80
81         return 0;
82 }
83 int init_cache(void) __attribute__((weak, alias("x86_init_cache")));
84
85 void  flush_cache(unsigned long dummy1, unsigned long dummy2)
86 {
87         asm("wbinvd\n");
88 }
89
90 /* Define these functions to allow ehch-hcd to function */
91 void flush_dcache_range(unsigned long start, unsigned long stop)
92 {
93 }
94
95 void invalidate_dcache_range(unsigned long start, unsigned long stop)
96 {
97 }
98
99 void dcache_enable(void)
100 {
101         enable_caches();
102 }
103
104 void dcache_disable(void)
105 {
106         disable_caches();
107 }
108
109 void icache_enable(void)
110 {
111 }
112
113 void icache_disable(void)
114 {
115 }
116
117 int icache_status(void)
118 {
119         return 1;
120 }
121
122 #ifndef CONFIG_TPL_BUILD
123 const char *cpu_vendor_name(int vendor)
124 {
125         const char *name;
126         name = "<invalid cpu vendor>";
127         if (vendor < ARRAY_SIZE(x86_vendor_name) &&
128             x86_vendor_name[vendor])
129                 name = x86_vendor_name[vendor];
130
131         return name;
132 }
133 #endif
134
135 char *cpu_get_name(char *name)
136 {
137         unsigned int *name_as_ints = (unsigned int *)name;
138         struct cpuid_result regs;
139         char *ptr;
140         int i;
141
142         /* This bit adds up to 48 bytes */
143         for (i = 0; i < 3; i++) {
144                 regs = cpuid(0x80000002 + i);
145                 name_as_ints[i * 4 + 0] = regs.eax;
146                 name_as_ints[i * 4 + 1] = regs.ebx;
147                 name_as_ints[i * 4 + 2] = regs.ecx;
148                 name_as_ints[i * 4 + 3] = regs.edx;
149         }
150         name[CPU_MAX_NAME_LEN - 1] = '\0';
151
152         /* Skip leading spaces. */
153         ptr = name;
154         while (*ptr == ' ')
155                 ptr++;
156
157         return ptr;
158 }
159
160 int default_print_cpuinfo(void)
161 {
162         printf("CPU: %s, vendor %s, device %xh\n",
163                cpu_has_64bit() ? "x86_64" : "x86",
164                cpu_vendor_name(gd->arch.x86_vendor), gd->arch.x86_device);
165
166 #ifdef CONFIG_HAVE_ACPI_RESUME
167         debug("ACPI previous sleep state: %s\n",
168               acpi_ss_string(gd->arch.prev_sleep_state));
169 #endif
170
171         return 0;
172 }
173
174 void show_boot_progress(int val)
175 {
176         outb(val, POST_PORT);
177 }
178
179 #if !defined(CONFIG_SYS_COREBOOT) && !defined(CONFIG_EFI_STUB)
180 /*
181  * Implement a weak default function for boards that optionally
182  * need to clean up the system before jumping to the kernel.
183  */
184 __weak void board_final_cleanup(void)
185 {
186 }
187
188 int last_stage_init(void)
189 {
190         struct acpi_fadt __maybe_unused *fadt;
191
192         board_final_cleanup();
193
194 #ifdef CONFIG_HAVE_ACPI_RESUME
195         fadt = acpi_find_fadt();
196
197         if (fadt && gd->arch.prev_sleep_state == ACPI_S3)
198                 acpi_resume(fadt);
199 #endif
200
201         write_tables();
202
203 #ifdef CONFIG_GENERATE_ACPI_TABLE
204         fadt = acpi_find_fadt();
205
206         /* Don't touch ACPI hardware on HW reduced platforms */
207         if (fadt && !(fadt->flags & ACPI_FADT_HW_REDUCED_ACPI)) {
208                 /*
209                  * Other than waiting for OSPM to request us to switch to ACPI
210                  * mode, do it by ourselves, since SMI will not be triggered.
211                  */
212                 enter_acpi_mode(fadt->pm1a_cnt_blk);
213         }
214 #endif
215
216         return 0;
217 }
218 #endif
219
220 static int x86_init_cpus(void)
221 {
222 #ifdef CONFIG_SMP
223         debug("Init additional CPUs\n");
224         x86_mp_init();
225 #else
226         struct udevice *dev;
227
228         /*
229          * This causes the cpu-x86 driver to be probed.
230          * We don't check return value here as we want to allow boards
231          * which have not been converted to use cpu uclass driver to boot.
232          */
233         uclass_first_device(UCLASS_CPU, &dev);
234 #endif
235
236         return 0;
237 }
238
239 int cpu_init_r(void)
240 {
241         struct udevice *dev;
242         int ret;
243
244         if (!ll_boot_init()) {
245                 uclass_first_device(UCLASS_PCI, &dev);
246                 return 0;
247         }
248
249         ret = x86_init_cpus();
250         if (ret)
251                 return ret;
252
253         /*
254          * Set up the northbridge, PCH and LPC if available. Note that these
255          * may have had some limited pre-relocation init if they were probed
256          * before relocation, but this is post relocation.
257          */
258         uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
259         uclass_first_device(UCLASS_PCH, &dev);
260         uclass_first_device(UCLASS_LPC, &dev);
261
262         /* Set up pin control if available */
263         ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &dev);
264         debug("%s, pinctrl=%p, ret=%d\n", __func__, dev, ret);
265
266         return 0;
267 }
268
269 #ifndef CONFIG_EFI_STUB
270 int reserve_arch(void)
271 {
272 #ifdef CONFIG_ENABLE_MRC_CACHE
273         mrccache_reserve();
274 #endif
275
276 #ifdef CONFIG_SEABIOS
277         high_table_reserve();
278 #endif
279
280 #ifdef CONFIG_HAVE_ACPI_RESUME
281         acpi_s3_reserve();
282
283 #ifdef CONFIG_HAVE_FSP
284         /*
285          * Save stack address to CMOS so that at next S3 boot,
286          * we can use it as the stack address for fsp_contiue()
287          */
288         fsp_save_s3_stack();
289 #endif /* CONFIG_HAVE_FSP */
290 #endif /* CONFIG_HAVE_ACPI_RESUME */
291
292         return 0;
293 }
294 #endif
295
296 long detect_coreboot_table_at(ulong start, ulong size)
297 {
298         u32 *ptr, *end;
299
300         size /= 4;
301         for (ptr = (void *)start, end = ptr + size; ptr < end; ptr += 4) {
302                 if (*ptr == 0x4f49424c) /* "LBIO" */
303                         return (long)ptr;
304         }
305
306         return -ENOENT;
307 }
308
309 long locate_coreboot_table(void)
310 {
311         long addr;
312
313         /* We look for LBIO in the first 4K of RAM and again at 960KB */
314         addr = detect_coreboot_table_at(0x0, 0x1000);
315         if (addr < 0)
316                 addr = detect_coreboot_table_at(0xf0000, 0x1000);
317
318         return addr;
319 }