blob: c8392915f1dbe87ce741946970682334281d1c85 [file] [log] [blame]
Simon Glass7bddac92014-10-10 08:21:52 -06001/*
2 * Copyright (c) 2014 The Chromium OS Authors.
3 *
Bin Meng52f952b2014-11-09 22:18:56 +08004 * Part of this file is adapted from coreboot
5 * src/arch/x86/include/arch/cpu.h and
6 * src/arch/x86/lib/cpu.c
7 *
Simon Glass7bddac92014-10-10 08:21:52 -06008 * SPDX-License-Identifier: GPL-2.0+
9 */
10
Bin Meng52f952b2014-11-09 22:18:56 +080011#ifndef _ASM_CPU_H
12#define _ASM_CPU_H
Simon Glass7bddac92014-10-10 08:21:52 -060013
Bin Meng52f952b2014-11-09 22:18:56 +080014enum {
15 X86_VENDOR_INVALID = 0,
16 X86_VENDOR_INTEL,
17 X86_VENDOR_CYRIX,
18 X86_VENDOR_AMD,
19 X86_VENDOR_UMC,
20 X86_VENDOR_NEXGEN,
21 X86_VENDOR_CENTAUR,
22 X86_VENDOR_RISE,
23 X86_VENDOR_TRANSMETA,
24 X86_VENDOR_NSC,
25 X86_VENDOR_SIS,
26 X86_VENDOR_ANY = 0xfe,
27 X86_VENDOR_UNKNOWN = 0xff
28};
29
30struct cpuid_result {
31 uint32_t eax;
32 uint32_t ebx;
33 uint32_t ecx;
34 uint32_t edx;
35};
36
37/*
38 * Generic CPUID function
39 */
40static inline struct cpuid_result cpuid(int op)
41{
42 struct cpuid_result result;
43 asm volatile(
44 "mov %%ebx, %%edi;"
45 "cpuid;"
46 "mov %%ebx, %%esi;"
47 "mov %%edi, %%ebx;"
48 : "=a" (result.eax),
49 "=S" (result.ebx),
50 "=c" (result.ecx),
51 "=d" (result.edx)
52 : "0" (op)
53 : "edi");
54 return result;
55}
56
57/*
58 * Generic Extended CPUID function
59 */
60static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
61{
62 struct cpuid_result result;
63 asm volatile(
64 "mov %%ebx, %%edi;"
65 "cpuid;"
66 "mov %%ebx, %%esi;"
67 "mov %%edi, %%ebx;"
68 : "=a" (result.eax),
69 "=S" (result.ebx),
70 "=c" (result.ecx),
71 "=d" (result.edx)
72 : "0" (op), "2" (ecx)
73 : "edi");
74 return result;
75}
76
77/*
78 * CPUID functions returning a single datum
79 */
80static inline unsigned int cpuid_eax(unsigned int op)
81{
82 unsigned int eax;
83
84 __asm__("mov %%ebx, %%edi;"
85 "cpuid;"
86 "mov %%edi, %%ebx;"
87 : "=a" (eax)
88 : "0" (op)
89 : "ecx", "edx", "edi");
90 return eax;
91}
92
93static inline unsigned int cpuid_ebx(unsigned int op)
94{
95 unsigned int eax, ebx;
96
97 __asm__("mov %%ebx, %%edi;"
98 "cpuid;"
99 "mov %%ebx, %%esi;"
100 "mov %%edi, %%ebx;"
101 : "=a" (eax), "=S" (ebx)
102 : "0" (op)
103 : "ecx", "edx", "edi");
104 return ebx;
105}
106
107static inline unsigned int cpuid_ecx(unsigned int op)
108{
109 unsigned int eax, ecx;
110
111 __asm__("mov %%ebx, %%edi;"
112 "cpuid;"
113 "mov %%edi, %%ebx;"
114 : "=a" (eax), "=c" (ecx)
115 : "0" (op)
116 : "edx", "edi");
117 return ecx;
118}
119
120static inline unsigned int cpuid_edx(unsigned int op)
121{
122 unsigned int eax, edx;
123
124 __asm__("mov %%ebx, %%edi;"
125 "cpuid;"
126 "mov %%edi, %%ebx;"
127 : "=a" (eax), "=d" (edx)
128 : "0" (op)
129 : "ecx", "edi");
130 return edx;
131}
132
133/* Standard macro to see if a specific flag is changeable */
134static inline int flag_is_changeable_p(uint32_t flag)
135{
136 uint32_t f1, f2;
137
138 asm(
139 "pushfl\n\t"
140 "pushfl\n\t"
141 "popl %0\n\t"
142 "movl %0,%1\n\t"
143 "xorl %2,%0\n\t"
144 "pushl %0\n\t"
145 "popfl\n\t"
146 "pushfl\n\t"
147 "popl %0\n\t"
148 "popfl\n\t"
149 : "=&r" (f1), "=&r" (f2)
150 : "ir" (flag));
151 return ((f1^f2) & flag) != 0;
152}
153
154/**
Simon Glass7bddac92014-10-10 08:21:52 -0600155 * cpu_enable_paging_pae() - Enable PAE-paging
156 *
Bin Meng52f952b2014-11-09 22:18:56 +0800157 * @cr3: Value to set in cr3 (PDPT or PML4T)
Simon Glass7bddac92014-10-10 08:21:52 -0600158 */
159void cpu_enable_paging_pae(ulong cr3);
160
161/**
162 * cpu_disable_paging_pae() - Disable paging and PAE
163 */
164void cpu_disable_paging_pae(void);
165
Simon Glass92cc94a2014-10-10 08:21:54 -0600166/**
167 * cpu_has_64bit() - Check if the CPU has 64-bit support
168 *
169 * @return 1 if this CPU supports long mode (64-bit), 0 if not
170 */
171int cpu_has_64bit(void);
172
Simon Glass200182a2014-10-10 08:21:55 -0600173/**
Bin Meng52f952b2014-11-09 22:18:56 +0800174 * cpu_vendor_name() - Get CPU vendor name
175 *
176 * @vendor: CPU vendor enumeration number
177 *
178 * @return: Address to hold the CPU vendor name string
179 */
180const char *cpu_vendor_name(int vendor);
181
Simon Glass727c1a92014-11-10 18:00:26 -0700182#define CPU_MAX_NAME_LEN 49
183
Bin Meng52f952b2014-11-09 22:18:56 +0800184/**
Simon Glass727c1a92014-11-10 18:00:26 -0700185 * cpu_get_name() - Get the name of the current cpu
Bin Meng52f952b2014-11-09 22:18:56 +0800186 *
Simon Glass727c1a92014-11-10 18:00:26 -0700187 * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
188 * @return pointer to name, which will likely be a few bytes after the start
189 * of @name
190 * \0 terminator
Bin Meng52f952b2014-11-09 22:18:56 +0800191 */
Simon Glass727c1a92014-11-10 18:00:26 -0700192char *cpu_get_name(char *name);
Bin Meng52f952b2014-11-09 22:18:56 +0800193
194/**
Simon Glass200182a2014-10-10 08:21:55 -0600195 * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
196 *
197 * The kernel is uncompressed and the 64-bit entry point is expected to be
198 * at @target.
199 *
200 * This function is used internally - see cpu_jump_to_64bit() for a more
201 * useful function.
202 *
203 * @pgtable: Address of 24KB area containing the page table
204 * @setup_base: Pointer to the setup.bin information for the kernel
205 * @target: Pointer to the start of the kernel image
206 */
207void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
208
209/**
210 * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
211 *
212 * The kernel is uncompressed and the 64-bit entry point is expected to be
213 * at @target.
214 *
215 * @setup_base: Pointer to the setup.bin information for the kernel
216 * @target: Pointer to the start of the kernel image
217 */
218int cpu_jump_to_64bit(ulong setup_base, ulong target);
219
Simon Glass7bddac92014-10-10 08:21:52 -0600220#endif