blob: 8c1eacc0c012590a17089ccf170abad2dbf92f39 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
Graeme Russdbf71152011-04-13 19:43:26 +10002 * (C) Copyright 2008-2011
3 * Graeme Russ, <graeme.russ@gmail.com>
4 *
wdenk2262cfe2002-11-18 00:14:45 +00005 * (C) Copyright 2002
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02006 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk8bde7f72003-06-27 21:31:46 +00007 *
wdenk2262cfe2002-11-18 00:14:45 +00008 * (C) Copyright 2002
9 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10 * Marius Groeger <mgroeger@sysgo.de>
11 *
12 * (C) Copyright 2002
13 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 * Alex Zuepke <azu@sysgo.de>
15 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020016 * SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +000017 */
18
wdenk2262cfe2002-11-18 00:14:45 +000019#include <common.h>
20#include <command.h>
Stefan Reinauer095593c2012-12-02 04:49:50 +000021#include <asm/control_regs.h>
Graeme Russc53fd2b2011-02-12 15:11:30 +110022#include <asm/processor.h>
Graeme Russ0c24c9c2011-02-12 15:11:32 +110023#include <asm/processor-flags.h>
Graeme Russ3f5f18d2008-12-07 10:29:02 +110024#include <asm/interrupt.h>
Gabe Black60a9b6b2011-11-16 23:32:50 +000025#include <linux/compiler.h>
wdenk2262cfe2002-11-18 00:14:45 +000026
Graeme Russdbf71152011-04-13 19:43:26 +100027/*
28 * Constructor for a conventional segment GDT (or LDT) entry
29 * This is a macro so it can be used in initialisers
30 */
Graeme Russ59c6d0e2010-10-07 20:03:21 +110031#define GDT_ENTRY(flags, base, limit) \
32 ((((base) & 0xff000000ULL) << (56-24)) | \
33 (((flags) & 0x0000f0ffULL) << 40) | \
34 (((limit) & 0x000f0000ULL) << (48-16)) | \
35 (((base) & 0x00ffffffULL) << 16) | \
36 (((limit) & 0x0000ffffULL)))
37
Graeme Russ59c6d0e2010-10-07 20:03:21 +110038struct gdt_ptr {
39 u16 len;
40 u32 ptr;
Graeme Russ717979f2011-11-08 02:33:13 +000041} __packed;
Graeme Russ59c6d0e2010-10-07 20:03:21 +110042
Graeme Russ74bfbe12011-12-29 21:45:33 +110043static void load_ds(u32 segment)
Graeme Russ59c6d0e2010-10-07 20:03:21 +110044{
Graeme Russ74bfbe12011-12-29 21:45:33 +110045 asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
46}
Graeme Russ59c6d0e2010-10-07 20:03:21 +110047
Graeme Russ74bfbe12011-12-29 21:45:33 +110048static void load_es(u32 segment)
49{
50 asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
51}
Graeme Russ59c6d0e2010-10-07 20:03:21 +110052
Graeme Russ74bfbe12011-12-29 21:45:33 +110053static void load_fs(u32 segment)
54{
55 asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
56}
57
58static void load_gs(u32 segment)
59{
60 asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
61}
62
63static void load_ss(u32 segment)
64{
65 asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
66}
67
68static void load_gdt(const u64 *boot_gdt, u16 num_entries)
69{
70 struct gdt_ptr gdt;
71
72 gdt.len = (num_entries * 8) - 1;
73 gdt.ptr = (u32)boot_gdt;
74
75 asm volatile("lgdtl %0\n" : : "m" (gdt));
Graeme Russ59c6d0e2010-10-07 20:03:21 +110076}
77
Graeme Russ9e6c5722011-12-31 22:58:15 +110078void setup_gdt(gd_t *id, u64 *gdt_addr)
79{
80 /* CS: code, read/execute, 4 GB, base 0 */
81 gdt_addr[X86_GDT_ENTRY_32BIT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff);
82
83 /* DS: data, read/write, 4 GB, base 0 */
84 gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
85
86 /* FS: data, read/write, 4 GB, base (Global Data Pointer) */
Simon Glass5a35e6c2012-12-13 20:48:41 +000087 id->arch.gd_addr = id;
Simon Glass0cecc3b2012-12-13 20:48:42 +000088 gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0xc093,
Simon Glass5a35e6c2012-12-13 20:48:41 +000089 (ulong)&id->arch.gd_addr, 0xfffff);
Graeme Russ9e6c5722011-12-31 22:58:15 +110090
91 /* 16-bit CS: code, read/execute, 64 kB, base 0 */
92 gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x109b, 0, 0x0ffff);
93
94 /* 16-bit DS: data, read/write, 64 kB, base 0 */
95 gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x1093, 0, 0x0ffff);
96
97 load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
98 load_ds(X86_GDT_ENTRY_32BIT_DS);
99 load_es(X86_GDT_ENTRY_32BIT_DS);
100 load_gs(X86_GDT_ENTRY_32BIT_DS);
101 load_ss(X86_GDT_ENTRY_32BIT_DS);
102 load_fs(X86_GDT_ENTRY_32BIT_FS);
103}
104
Gabe Blackf30fc4d2012-10-20 12:33:10 +0000105int __weak x86_cleanup_before_linux(void)
106{
Simon Glass79497032013-04-17 16:13:35 +0000107#ifdef CONFIG_BOOTSTAGE_STASH
108 bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH,
109 CONFIG_BOOTSTAGE_STASH_SIZE);
110#endif
111
Gabe Blackf30fc4d2012-10-20 12:33:10 +0000112 return 0;
113}
114
Graeme Russ0ea76e92011-02-12 15:11:35 +1100115int x86_cpu_init_f(void)
wdenk2262cfe2002-11-18 00:14:45 +0000116{
Graeme Russ0c24c9c2011-02-12 15:11:32 +1100117 const u32 em_rst = ~X86_CR0_EM;
118 const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
119
wdenk7a8e9bed2003-05-31 18:35:21 +0000120 /* initialize FPU, reset EM, set MP and NE */
121 asm ("fninit\n" \
Graeme Russ0c24c9c2011-02-12 15:11:32 +1100122 "movl %%cr0, %%eax\n" \
123 "andl %0, %%eax\n" \
124 "orl %1, %%eax\n" \
125 "movl %%eax, %%cr0\n" \
126 : : "i" (em_rst), "i" (mp_ne_set) : "eax");
wdenk8bde7f72003-06-27 21:31:46 +0000127
Graeme Russ1c409bc2009-11-24 20:04:21 +1100128 return 0;
129}
Graeme Russ0ea76e92011-02-12 15:11:35 +1100130int cpu_init_f(void) __attribute__((weak, alias("x86_cpu_init_f")));
Graeme Russ1c409bc2009-11-24 20:04:21 +1100131
Graeme Russ0ea76e92011-02-12 15:11:35 +1100132int x86_cpu_init_r(void)
Graeme Russ1c409bc2009-11-24 20:04:21 +1100133{
Graeme Russd6532442011-12-27 22:46:43 +1100134 /* Initialize core interrupt and exception functionality of CPU */
135 cpu_init_interrupts();
136 return 0;
137}
138int cpu_init_r(void) __attribute__((weak, alias("x86_cpu_init_r")));
139
140void x86_enable_caches(void)
141{
Stefan Reinauer095593c2012-12-02 04:49:50 +0000142 unsigned long cr0;
Graeme Russ0ea76e92011-02-12 15:11:35 +1100143
Stefan Reinauer095593c2012-12-02 04:49:50 +0000144 cr0 = read_cr0();
145 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
146 write_cr0(cr0);
147 wbinvd();
Graeme Russd6532442011-12-27 22:46:43 +1100148}
149void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
Graeme Russ0ea76e92011-02-12 15:11:35 +1100150
Stefan Reinauer095593c2012-12-02 04:49:50 +0000151void x86_disable_caches(void)
152{
153 unsigned long cr0;
154
155 cr0 = read_cr0();
156 cr0 |= X86_CR0_NW | X86_CR0_CD;
157 wbinvd();
158 write_cr0(cr0);
159 wbinvd();
160}
161void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
162
Graeme Russd6532442011-12-27 22:46:43 +1100163int x86_init_cache(void)
164{
165 enable_caches();
166
wdenk2262cfe2002-11-18 00:14:45 +0000167 return 0;
168}
Graeme Russd6532442011-12-27 22:46:43 +1100169int init_cache(void) __attribute__((weak, alias("x86_init_cache")));
wdenk2262cfe2002-11-18 00:14:45 +0000170
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200171int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk2262cfe2002-11-18 00:14:45 +0000172{
Graeme Russ717979f2011-11-08 02:33:13 +0000173 printf("resetting ...\n");
Graeme Russdbf71152011-04-13 19:43:26 +1000174
175 /* wait 50 ms */
176 udelay(50000);
wdenk2262cfe2002-11-18 00:14:45 +0000177 disable_interrupts();
178 reset_cpu(0);
179
180 /*NOTREACHED*/
181 return 0;
182}
183
Graeme Russ717979f2011-11-08 02:33:13 +0000184void flush_cache(unsigned long dummy1, unsigned long dummy2)
wdenk2262cfe2002-11-18 00:14:45 +0000185{
186 asm("wbinvd\n");
wdenk2262cfe2002-11-18 00:14:45 +0000187}
Graeme Russ3f5f18d2008-12-07 10:29:02 +1100188
189void __attribute__ ((regparm(0))) generate_gpf(void);
190
191/* segment 0x70 is an arbitrary segment which does not exist */
192asm(".globl generate_gpf\n"
Graeme Russ717979f2011-11-08 02:33:13 +0000193 ".hidden generate_gpf\n"
194 ".type generate_gpf, @function\n"
195 "generate_gpf:\n"
196 "ljmp $0x70, $0x47114711\n");
Graeme Russ3f5f18d2008-12-07 10:29:02 +1100197
198void __reset_cpu(ulong addr)
199{
Graeme Russfea25722011-04-13 19:43:28 +1000200 printf("Resetting using x86 Triple Fault\n");
Graeme Russ717979f2011-11-08 02:33:13 +0000201 set_vector(13, generate_gpf); /* general protection fault handler */
202 set_vector(8, generate_gpf); /* double fault handler */
203 generate_gpf(); /* start the show */
Graeme Russ3f5f18d2008-12-07 10:29:02 +1100204}
205void reset_cpu(ulong addr) __attribute__((weak, alias("__reset_cpu")));
Stefan Reinauer095593c2012-12-02 04:49:50 +0000206
207int dcache_status(void)
208{
209 return !(read_cr0() & 0x40000000);
210}
211
212/* Define these functions to allow ehch-hcd to function */
213void flush_dcache_range(unsigned long start, unsigned long stop)
214{
215}
216
217void invalidate_dcache_range(unsigned long start, unsigned long stop)
218{
219}
Simon Glass89371402013-02-28 19:26:11 +0000220
221void dcache_enable(void)
222{
223 enable_caches();
224}
225
226void dcache_disable(void)
227{
228 disable_caches();
229}
230
231void icache_enable(void)
232{
233}
234
235void icache_disable(void)
236{
237}
238
239int icache_status(void)
240{
241 return 1;
242}
Simon Glass7bddac92014-10-10 08:21:52 -0600243
244void cpu_enable_paging_pae(ulong cr3)
245{
246 __asm__ __volatile__(
247 /* Load the page table address */
248 "movl %0, %%cr3\n"
249 /* Enable pae */
250 "movl %%cr4, %%eax\n"
251 "orl $0x00000020, %%eax\n"
252 "movl %%eax, %%cr4\n"
253 /* Enable paging */
254 "movl %%cr0, %%eax\n"
255 "orl $0x80000000, %%eax\n"
256 "movl %%eax, %%cr0\n"
257 :
258 : "r" (cr3)
259 : "eax");
260}
261
262void cpu_disable_paging_pae(void)
263{
264 /* Turn off paging */
265 __asm__ __volatile__ (
266 /* Disable paging */
267 "movl %%cr0, %%eax\n"
268 "andl $0x7fffffff, %%eax\n"
269 "movl %%eax, %%cr0\n"
270 /* Disable pae */
271 "movl %%cr4, %%eax\n"
272 "andl $0xffffffdf, %%eax\n"
273 "movl %%eax, %%cr4\n"
274 :
275 :
276 : "eax");
277}
Simon Glass92cc94a2014-10-10 08:21:54 -0600278
279static bool has_cpuid(void)
280{
281 unsigned long flag;
282
283 asm volatile("pushf\n" \
284 "pop %%eax\n"
285 "mov %%eax, %%ecx\n" /* ecx = flags */
286 "xor %1, %%eax\n"
287 "push %%eax\n"
288 "popf\n" /* flags ^= $2 */
289 "pushf\n"
290 "pop %%eax\n" /* eax = flags */
291 "push %%ecx\n"
292 "popf\n" /* flags = ecx */
293 "xor %%ecx, %%eax\n"
294 "mov %%eax, %0"
295 : "=r" (flag)
296 : "i" (1 << 21)
297 : "eax", "ecx", "memory");
298
299 return flag != 0;
300}
301
302static bool can_detect_long_mode(void)
303{
304 unsigned long flag;
305
306 asm volatile("mov $0x80000000, %%eax\n"
307 "cpuid\n"
308 "mov %%eax, %0"
309 : "=r" (flag)
310 :
311 : "eax", "ebx", "ecx", "edx", "memory");
312
313 return flag > 0x80000000UL;
314}
315
316static bool has_long_mode(void)
317{
318 unsigned long flag;
319
320 asm volatile("mov $0x80000001, %%eax\n"
321 "cpuid\n"
322 "mov %%edx, %0"
323 : "=r" (flag)
324 :
325 : "eax", "ebx", "ecx", "edx", "memory");
326
327 return flag & (1 << 29) ? true : false;
328}
329
330int cpu_has_64bit(void)
331{
332 return has_cpuid() && can_detect_long_mode() &&
333 has_long_mode();
334}
335
336int print_cpuinfo(void)
337{
338 printf("CPU: %s\n", cpu_has_64bit() ? "x86_64" : "x86");
339
340 return 0;
341}