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