17bddac94SSimon Glass /* 27bddac94SSimon Glass * Copyright (c) 2014 The Chromium OS Authors. 37bddac94SSimon Glass * 452f952bfSBin Meng * Part of this file is adapted from coreboot 552f952bfSBin Meng * src/arch/x86/include/arch/cpu.h and 652f952bfSBin Meng * src/arch/x86/lib/cpu.c 752f952bfSBin Meng * 87bddac94SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 97bddac94SSimon Glass */ 107bddac94SSimon Glass 1152f952bfSBin Meng #ifndef _ASM_CPU_H 1252f952bfSBin Meng #define _ASM_CPU_H 1352f952bfSBin Meng 1452f952bfSBin Meng enum { 1552f952bfSBin Meng X86_VENDOR_INVALID = 0, 1652f952bfSBin Meng X86_VENDOR_INTEL, 1752f952bfSBin Meng X86_VENDOR_CYRIX, 1852f952bfSBin Meng X86_VENDOR_AMD, 1952f952bfSBin Meng X86_VENDOR_UMC, 2052f952bfSBin Meng X86_VENDOR_NEXGEN, 2152f952bfSBin Meng X86_VENDOR_CENTAUR, 2252f952bfSBin Meng X86_VENDOR_RISE, 2352f952bfSBin Meng X86_VENDOR_TRANSMETA, 2452f952bfSBin Meng X86_VENDOR_NSC, 2552f952bfSBin Meng X86_VENDOR_SIS, 2652f952bfSBin Meng X86_VENDOR_ANY = 0xfe, 2752f952bfSBin Meng X86_VENDOR_UNKNOWN = 0xff 2852f952bfSBin Meng }; 2952f952bfSBin Meng 307dfe8bdeSSimon Glass /* Global descriptor table (GDT) bits */ 317dfe8bdeSSimon Glass enum { 327dfe8bdeSSimon Glass GDT_4KB = 1ULL << 55, 337dfe8bdeSSimon Glass GDT_32BIT = 1ULL << 54, 347dfe8bdeSSimon Glass GDT_LONG = 1ULL << 53, 357dfe8bdeSSimon Glass GDT_PRESENT = 1ULL << 47, 367dfe8bdeSSimon Glass GDT_NOTSYS = 1ULL << 44, 377dfe8bdeSSimon Glass GDT_CODE = 1ULL << 43, 387dfe8bdeSSimon Glass GDT_LIMIT_LOW_SHIFT = 0, 397dfe8bdeSSimon Glass GDT_LIMIT_LOW_MASK = 0xffff, 407dfe8bdeSSimon Glass GDT_LIMIT_HIGH_SHIFT = 48, 417dfe8bdeSSimon Glass GDT_LIMIT_HIGH_MASK = 0xf, 427dfe8bdeSSimon Glass GDT_BASE_LOW_SHIFT = 16, 437dfe8bdeSSimon Glass GDT_BASE_LOW_MASK = 0xffff, 447dfe8bdeSSimon Glass GDT_BASE_HIGH_SHIFT = 56, 457dfe8bdeSSimon Glass GDT_BASE_HIGH_MASK = 0xf, 467dfe8bdeSSimon Glass }; 477dfe8bdeSSimon Glass 48*98655f3aSSimon Glass /* 49*98655f3aSSimon Glass * System controllers in an x86 system. We mostly need to just find these and 50*98655f3aSSimon Glass * use them on PCI. At some point these might have their own uclass. 51*98655f3aSSimon Glass */ 52*98655f3aSSimon Glass enum { 53*98655f3aSSimon Glass X86_NONE, 54*98655f3aSSimon Glass X86_SYSCON_ME, /* Intel Management Engine */ 55*98655f3aSSimon Glass }; 56*98655f3aSSimon Glass 5752f952bfSBin Meng struct cpuid_result { 5852f952bfSBin Meng uint32_t eax; 5952f952bfSBin Meng uint32_t ebx; 6052f952bfSBin Meng uint32_t ecx; 6152f952bfSBin Meng uint32_t edx; 6252f952bfSBin Meng }; 6352f952bfSBin Meng 6452f952bfSBin Meng /* 6552f952bfSBin Meng * Generic CPUID function 6652f952bfSBin Meng */ 6752f952bfSBin Meng static inline struct cpuid_result cpuid(int op) 6852f952bfSBin Meng { 6952f952bfSBin Meng struct cpuid_result result; 7052f952bfSBin Meng asm volatile( 7152f952bfSBin Meng "mov %%ebx, %%edi;" 7252f952bfSBin Meng "cpuid;" 7352f952bfSBin Meng "mov %%ebx, %%esi;" 7452f952bfSBin Meng "mov %%edi, %%ebx;" 7552f952bfSBin Meng : "=a" (result.eax), 7652f952bfSBin Meng "=S" (result.ebx), 7752f952bfSBin Meng "=c" (result.ecx), 7852f952bfSBin Meng "=d" (result.edx) 7952f952bfSBin Meng : "0" (op) 8052f952bfSBin Meng : "edi"); 8152f952bfSBin Meng return result; 8252f952bfSBin Meng } 8352f952bfSBin Meng 8452f952bfSBin Meng /* 8552f952bfSBin Meng * Generic Extended CPUID function 8652f952bfSBin Meng */ 8752f952bfSBin Meng static inline struct cpuid_result cpuid_ext(int op, unsigned ecx) 8852f952bfSBin Meng { 8952f952bfSBin Meng struct cpuid_result result; 9052f952bfSBin Meng asm volatile( 9152f952bfSBin Meng "mov %%ebx, %%edi;" 9252f952bfSBin Meng "cpuid;" 9352f952bfSBin Meng "mov %%ebx, %%esi;" 9452f952bfSBin Meng "mov %%edi, %%ebx;" 9552f952bfSBin Meng : "=a" (result.eax), 9652f952bfSBin Meng "=S" (result.ebx), 9752f952bfSBin Meng "=c" (result.ecx), 9852f952bfSBin Meng "=d" (result.edx) 9952f952bfSBin Meng : "0" (op), "2" (ecx) 10052f952bfSBin Meng : "edi"); 10152f952bfSBin Meng return result; 10252f952bfSBin Meng } 10352f952bfSBin Meng 10452f952bfSBin Meng /* 10552f952bfSBin Meng * CPUID functions returning a single datum 10652f952bfSBin Meng */ 10752f952bfSBin Meng static inline unsigned int cpuid_eax(unsigned int op) 10852f952bfSBin Meng { 10952f952bfSBin Meng unsigned int eax; 11052f952bfSBin Meng 11152f952bfSBin Meng __asm__("mov %%ebx, %%edi;" 11252f952bfSBin Meng "cpuid;" 11352f952bfSBin Meng "mov %%edi, %%ebx;" 11452f952bfSBin Meng : "=a" (eax) 11552f952bfSBin Meng : "0" (op) 11652f952bfSBin Meng : "ecx", "edx", "edi"); 11752f952bfSBin Meng return eax; 11852f952bfSBin Meng } 11952f952bfSBin Meng 12052f952bfSBin Meng static inline unsigned int cpuid_ebx(unsigned int op) 12152f952bfSBin Meng { 12252f952bfSBin Meng unsigned int eax, ebx; 12352f952bfSBin Meng 12452f952bfSBin Meng __asm__("mov %%ebx, %%edi;" 12552f952bfSBin Meng "cpuid;" 12652f952bfSBin Meng "mov %%ebx, %%esi;" 12752f952bfSBin Meng "mov %%edi, %%ebx;" 12852f952bfSBin Meng : "=a" (eax), "=S" (ebx) 12952f952bfSBin Meng : "0" (op) 13052f952bfSBin Meng : "ecx", "edx", "edi"); 13152f952bfSBin Meng return ebx; 13252f952bfSBin Meng } 13352f952bfSBin Meng 13452f952bfSBin Meng static inline unsigned int cpuid_ecx(unsigned int op) 13552f952bfSBin Meng { 13652f952bfSBin Meng unsigned int eax, ecx; 13752f952bfSBin Meng 13852f952bfSBin Meng __asm__("mov %%ebx, %%edi;" 13952f952bfSBin Meng "cpuid;" 14052f952bfSBin Meng "mov %%edi, %%ebx;" 14152f952bfSBin Meng : "=a" (eax), "=c" (ecx) 14252f952bfSBin Meng : "0" (op) 14352f952bfSBin Meng : "edx", "edi"); 14452f952bfSBin Meng return ecx; 14552f952bfSBin Meng } 14652f952bfSBin Meng 14752f952bfSBin Meng static inline unsigned int cpuid_edx(unsigned int op) 14852f952bfSBin Meng { 14952f952bfSBin Meng unsigned int eax, edx; 15052f952bfSBin Meng 15152f952bfSBin Meng __asm__("mov %%ebx, %%edi;" 15252f952bfSBin Meng "cpuid;" 15352f952bfSBin Meng "mov %%edi, %%ebx;" 15452f952bfSBin Meng : "=a" (eax), "=d" (edx) 15552f952bfSBin Meng : "0" (op) 15652f952bfSBin Meng : "ecx", "edi"); 15752f952bfSBin Meng return edx; 15852f952bfSBin Meng } 15952f952bfSBin Meng 16052f952bfSBin Meng /* Standard macro to see if a specific flag is changeable */ 16152f952bfSBin Meng static inline int flag_is_changeable_p(uint32_t flag) 16252f952bfSBin Meng { 16352f952bfSBin Meng uint32_t f1, f2; 16452f952bfSBin Meng 16552f952bfSBin Meng asm( 16652f952bfSBin Meng "pushfl\n\t" 16752f952bfSBin Meng "pushfl\n\t" 16852f952bfSBin Meng "popl %0\n\t" 16952f952bfSBin Meng "movl %0,%1\n\t" 17052f952bfSBin Meng "xorl %2,%0\n\t" 17152f952bfSBin Meng "pushl %0\n\t" 17252f952bfSBin Meng "popfl\n\t" 17352f952bfSBin Meng "pushfl\n\t" 17452f952bfSBin Meng "popl %0\n\t" 17552f952bfSBin Meng "popfl\n\t" 17652f952bfSBin Meng : "=&r" (f1), "=&r" (f2) 17752f952bfSBin Meng : "ir" (flag)); 17852f952bfSBin Meng return ((f1^f2) & flag) != 0; 17952f952bfSBin Meng } 1807bddac94SSimon Glass 181837a136fSSimon Glass static inline void mfence(void) 182837a136fSSimon Glass { 183837a136fSSimon Glass __asm__ __volatile__("mfence" : : : "memory"); 184837a136fSSimon Glass } 185837a136fSSimon Glass 1867bddac94SSimon Glass /** 1877bddac94SSimon Glass * cpu_enable_paging_pae() - Enable PAE-paging 1887bddac94SSimon Glass * 18952f952bfSBin Meng * @cr3: Value to set in cr3 (PDPT or PML4T) 1907bddac94SSimon Glass */ 1917bddac94SSimon Glass void cpu_enable_paging_pae(ulong cr3); 1927bddac94SSimon Glass 1937bddac94SSimon Glass /** 1947bddac94SSimon Glass * cpu_disable_paging_pae() - Disable paging and PAE 1957bddac94SSimon Glass */ 1967bddac94SSimon Glass void cpu_disable_paging_pae(void); 1977bddac94SSimon Glass 19892cc94a1SSimon Glass /** 19992cc94a1SSimon Glass * cpu_has_64bit() - Check if the CPU has 64-bit support 20092cc94a1SSimon Glass * 20192cc94a1SSimon Glass * @return 1 if this CPU supports long mode (64-bit), 0 if not 20292cc94a1SSimon Glass */ 20392cc94a1SSimon Glass int cpu_has_64bit(void); 20492cc94a1SSimon Glass 205200182a7SSimon Glass /** 20652f952bfSBin Meng * cpu_vendor_name() - Get CPU vendor name 20752f952bfSBin Meng * 20852f952bfSBin Meng * @vendor: CPU vendor enumeration number 20952f952bfSBin Meng * 21052f952bfSBin Meng * @return: Address to hold the CPU vendor name string 21152f952bfSBin Meng */ 21252f952bfSBin Meng const char *cpu_vendor_name(int vendor); 21352f952bfSBin Meng 214727c1a98SSimon Glass #define CPU_MAX_NAME_LEN 49 215727c1a98SSimon Glass 21652f952bfSBin Meng /** 217727c1a98SSimon Glass * cpu_get_name() - Get the name of the current cpu 21852f952bfSBin Meng * 219727c1a98SSimon Glass * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including 220727c1a98SSimon Glass * @return pointer to name, which will likely be a few bytes after the start 221727c1a98SSimon Glass * of @name 222727c1a98SSimon Glass * \0 terminator 22352f952bfSBin Meng */ 224727c1a98SSimon Glass char *cpu_get_name(char *name); 22552f952bfSBin Meng 22652f952bfSBin Meng /** 227200182a7SSimon Glass * cpu_call64() - Jump to a 64-bit Linux kernel (internal function) 228200182a7SSimon Glass * 229200182a7SSimon Glass * The kernel is uncompressed and the 64-bit entry point is expected to be 230200182a7SSimon Glass * at @target. 231200182a7SSimon Glass * 232200182a7SSimon Glass * This function is used internally - see cpu_jump_to_64bit() for a more 233200182a7SSimon Glass * useful function. 234200182a7SSimon Glass * 235200182a7SSimon Glass * @pgtable: Address of 24KB area containing the page table 236200182a7SSimon Glass * @setup_base: Pointer to the setup.bin information for the kernel 237200182a7SSimon Glass * @target: Pointer to the start of the kernel image 238200182a7SSimon Glass */ 239200182a7SSimon Glass void cpu_call64(ulong pgtable, ulong setup_base, ulong target); 240200182a7SSimon Glass 241200182a7SSimon Glass /** 2426f92ed8fSSimon Glass * cpu_call32() - Jump to a 32-bit entry point 2436f92ed8fSSimon Glass * 2446f92ed8fSSimon Glass * @code_seg32: 32-bit code segment to use (GDT offset, e.g. 0x20) 2456f92ed8fSSimon Glass * @target: Pointer to the start of the 32-bit U-Boot image/entry point 2466f92ed8fSSimon Glass * @table: Pointer to start of info table to pass to U-Boot 2476f92ed8fSSimon Glass */ 2486f92ed8fSSimon Glass void cpu_call32(ulong code_seg32, ulong target, ulong table); 2496f92ed8fSSimon Glass 2506f92ed8fSSimon Glass /** 251200182a7SSimon Glass * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel 252200182a7SSimon Glass * 253200182a7SSimon Glass * The kernel is uncompressed and the 64-bit entry point is expected to be 254200182a7SSimon Glass * at @target. 255200182a7SSimon Glass * 256200182a7SSimon Glass * @setup_base: Pointer to the setup.bin information for the kernel 257200182a7SSimon Glass * @target: Pointer to the start of the kernel image 258200182a7SSimon Glass */ 259200182a7SSimon Glass int cpu_jump_to_64bit(ulong setup_base, ulong target); 260200182a7SSimon Glass 2617bddac94SSimon Glass #endif 262