Simon Glass | 7bddac9 | 2014-10-10 08:21:52 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. |
| 3 | * |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 4 | * 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 Glass | 7bddac9 | 2014-10-10 08:21:52 -0600 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | */ |
| 10 | |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 11 | #ifndef _ASM_CPU_H |
| 12 | #define _ASM_CPU_H |
Simon Glass | 7bddac9 | 2014-10-10 08:21:52 -0600 | [diff] [blame] | 13 | |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 14 | enum { |
| 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 | |
| 30 | struct 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 | */ |
| 40 | static 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 | */ |
| 60 | static 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 | */ |
| 80 | static 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 | |
| 93 | static 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 | |
| 107 | static 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 | |
| 120 | static 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 */ |
| 134 | static 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 Glass | 7bddac9 | 2014-10-10 08:21:52 -0600 | [diff] [blame] | 155 | * cpu_enable_paging_pae() - Enable PAE-paging |
| 156 | * |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 157 | * @cr3: Value to set in cr3 (PDPT or PML4T) |
Simon Glass | 7bddac9 | 2014-10-10 08:21:52 -0600 | [diff] [blame] | 158 | */ |
| 159 | void cpu_enable_paging_pae(ulong cr3); |
| 160 | |
| 161 | /** |
| 162 | * cpu_disable_paging_pae() - Disable paging and PAE |
| 163 | */ |
| 164 | void cpu_disable_paging_pae(void); |
| 165 | |
Simon Glass | 92cc94a | 2014-10-10 08:21:54 -0600 | [diff] [blame] | 166 | /** |
| 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 | */ |
| 171 | int cpu_has_64bit(void); |
| 172 | |
Simon Glass | 200182a | 2014-10-10 08:21:55 -0600 | [diff] [blame] | 173 | /** |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 174 | * 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 | */ |
| 180 | const char *cpu_vendor_name(int vendor); |
| 181 | |
Simon Glass | 727c1a9 | 2014-11-10 18:00:26 -0700 | [diff] [blame^] | 182 | #define CPU_MAX_NAME_LEN 49 |
| 183 | |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 184 | /** |
Simon Glass | 727c1a9 | 2014-11-10 18:00:26 -0700 | [diff] [blame^] | 185 | * cpu_get_name() - Get the name of the current cpu |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 186 | * |
Simon Glass | 727c1a9 | 2014-11-10 18:00:26 -0700 | [diff] [blame^] | 187 | * @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 Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 191 | */ |
Simon Glass | 727c1a9 | 2014-11-10 18:00:26 -0700 | [diff] [blame^] | 192 | char *cpu_get_name(char *name); |
Bin Meng | 52f952b | 2014-11-09 22:18:56 +0800 | [diff] [blame] | 193 | |
| 194 | /** |
Simon Glass | 200182a | 2014-10-10 08:21:55 -0600 | [diff] [blame] | 195 | * 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 | */ |
| 207 | void 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 | */ |
| 218 | int cpu_jump_to_64bit(ulong setup_base, ulong target); |
| 219 | |
Simon Glass | 7bddac9 | 2014-10-10 08:21:52 -0600 | [diff] [blame] | 220 | #endif |