x86: cpu: Remove unnecessary #ifdefs
[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 <irq.h>
29 #include <log.h>
30 #include <malloc.h>
31 #include <syscon.h>
32 #include <acpi/acpi_s3.h>
33 #include <acpi/acpi_table.h>
34 #include <asm/acpi.h>
35 #include <asm/control_regs.h>
36 #include <asm/coreboot_tables.h>
37 #include <asm/cpu.h>
38 #include <asm/lapic.h>
39 #include <asm/microcode.h>
40 #include <asm/mp.h>
41 #include <asm/mrccache.h>
42 #include <asm/msr.h>
43 #include <asm/mtrr.h>
44 #include <asm/post.h>
45 #include <asm/processor.h>
46 #include <asm/processor-flags.h>
47 #include <asm/interrupt.h>
48 #include <asm/tables.h>
49 #include <linux/compiler.h>
50
51 DECLARE_GLOBAL_DATA_PTR;
52
53 #ifndef CONFIG_TPL_BUILD
54 static const char *const x86_vendor_name[] = {
55         [X86_VENDOR_INTEL]     = "Intel",
56         [X86_VENDOR_CYRIX]     = "Cyrix",
57         [X86_VENDOR_AMD]       = "AMD",
58         [X86_VENDOR_UMC]       = "UMC",
59         [X86_VENDOR_NEXGEN]    = "NexGen",
60         [X86_VENDOR_CENTAUR]   = "Centaur",
61         [X86_VENDOR_RISE]      = "Rise",
62         [X86_VENDOR_TRANSMETA] = "Transmeta",
63         [X86_VENDOR_NSC]       = "NSC",
64         [X86_VENDOR_SIS]       = "SiS",
65 };
66 #endif
67
68 int __weak x86_cleanup_before_linux(void)
69 {
70         bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
71                         CONFIG_BOOTSTAGE_STASH_SIZE);
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         if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
166                 debug("ACPI previous sleep state: %s\n",
167                       acpi_ss_string(gd->arch.prev_sleep_state));
168         }
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 need to do some final init
181  * before the system is ready.
182  */
183 __weak void board_final_init(void)
184 {
185 }
186
187 int last_stage_init(void)
188 {
189         struct acpi_fadt __maybe_unused *fadt;
190
191         board_final_init();
192
193         if (IS_ENABLED(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         }
199
200         write_tables();
201
202         if (IS_ENABLED(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
209                          * to ACPI * mode, do it by ourselves, since SMI will
210                          * not be triggered.
211                          */
212                         enter_acpi_mode(fadt->pm1a_cnt_blk);
213                 }
214         }
215
216         return 0;
217 }
218 #endif
219
220 static int x86_init_cpus(void)
221 {
222         if (IS_ENABLED(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
232                  * boot.
233                  */
234                 uclass_first_device(UCLASS_CPU, &dev);
235         }
236
237         return 0;
238 }
239
240 int cpu_init_r(void)
241 {
242         struct udevice *dev;
243         int ret;
244
245         if (!ll_boot_init()) {
246                 uclass_first_device(UCLASS_PCI, &dev);
247                 return 0;
248         }
249
250         ret = x86_init_cpus();
251         if (ret)
252                 return ret;
253
254         /*
255          * Set up the northbridge, PCH and LPC if available. Note that these
256          * may have had some limited pre-relocation init if they were probed
257          * before relocation, but this is post relocation.
258          */
259         uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
260         uclass_first_device(UCLASS_PCH, &dev);
261         uclass_first_device(UCLASS_LPC, &dev);
262
263         /* Set up pin control if available */
264         ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &dev);
265         debug("%s, pinctrl=%p, ret=%d\n", __func__, dev, ret);
266
267         return 0;
268 }
269
270 #ifndef CONFIG_EFI_STUB
271 int reserve_arch(void)
272 {
273         struct udevice *itss;
274         int ret;
275
276         if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE))
277                 mrccache_reserve();
278
279         if (IS_ENABLED(CONFIG_SEABIOS))
280                 high_table_reserve();
281
282         if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
283                 acpi_s3_reserve();
284
285                 if (IS_ENABLED(CONFIG_HAVE_FSP)) {
286                         /*
287                          * Save stack address to CMOS so that at next S3 boot,
288                          * we can use it as the stack address for fsp_contiue()
289                          */
290                         fsp_save_s3_stack();
291                 }
292         }
293         ret = irq_first_device_type(X86_IRQT_ITSS, &itss);
294         if (!ret) {
295                 /*
296                  * Snapshot the current GPIO IRQ polarities. FSP-S is about to
297                  * run and will set a default policy that doesn't honour boards'
298                  * requirements
299                  */
300                 irq_snapshot_polarities(itss);
301         }
302
303         return 0;
304 }
305 #endif
306
307 long detect_coreboot_table_at(ulong start, ulong size)
308 {
309         u32 *ptr, *end;
310
311         size /= 4;
312         for (ptr = (void *)start, end = ptr + size; ptr < end; ptr += 4) {
313                 if (*ptr == 0x4f49424c) /* "LBIO" */
314                         return (long)ptr;
315         }
316
317         return -ENOENT;
318 }
319
320 long locate_coreboot_table(void)
321 {
322         long addr;
323
324         /* We look for LBIO in the first 4K of RAM and again at 960KB */
325         addr = detect_coreboot_table_at(0x0, 0x1000);
326         if (addr < 0)
327                 addr = detect_coreboot_table_at(0xf0000, 0x1000);
328
329         return addr;
330 }