1 /* 2 * Copyright (c) 2014 The Chromium OS Authors. 3 * 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 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #ifndef _ASM_CPU_H 12 #define _ASM_CPU_H 13 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 static inline void mfence(void) 155 { 156 __asm__ __volatile__("mfence" : : : "memory"); 157 } 158 159 /** 160 * cpu_enable_paging_pae() - Enable PAE-paging 161 * 162 * @cr3: Value to set in cr3 (PDPT or PML4T) 163 */ 164 void cpu_enable_paging_pae(ulong cr3); 165 166 /** 167 * cpu_disable_paging_pae() - Disable paging and PAE 168 */ 169 void cpu_disable_paging_pae(void); 170 171 /** 172 * cpu_has_64bit() - Check if the CPU has 64-bit support 173 * 174 * @return 1 if this CPU supports long mode (64-bit), 0 if not 175 */ 176 int cpu_has_64bit(void); 177 178 /** 179 * cpu_vendor_name() - Get CPU vendor name 180 * 181 * @vendor: CPU vendor enumeration number 182 * 183 * @return: Address to hold the CPU vendor name string 184 */ 185 const char *cpu_vendor_name(int vendor); 186 187 #define CPU_MAX_NAME_LEN 49 188 189 /** 190 * cpu_get_name() - Get the name of the current cpu 191 * 192 * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including 193 * @return pointer to name, which will likely be a few bytes after the start 194 * of @name 195 * \0 terminator 196 */ 197 char *cpu_get_name(char *name); 198 199 /** 200 * cpu_call64() - Jump to a 64-bit Linux kernel (internal function) 201 * 202 * The kernel is uncompressed and the 64-bit entry point is expected to be 203 * at @target. 204 * 205 * This function is used internally - see cpu_jump_to_64bit() for a more 206 * useful function. 207 * 208 * @pgtable: Address of 24KB area containing the page table 209 * @setup_base: Pointer to the setup.bin information for the kernel 210 * @target: Pointer to the start of the kernel image 211 */ 212 void cpu_call64(ulong pgtable, ulong setup_base, ulong target); 213 214 /** 215 * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel 216 * 217 * The kernel is uncompressed and the 64-bit entry point is expected to be 218 * at @target. 219 * 220 * @setup_base: Pointer to the setup.bin information for the kernel 221 * @target: Pointer to the start of the kernel image 222 */ 223 int cpu_jump_to_64bit(ulong setup_base, ulong target); 224 225 #endif 226