1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
27bddac94SSimon Glass /*
37bddac94SSimon Glass * Copyright (c) 2014 The Chromium OS Authors.
47bddac94SSimon Glass *
552f952bfSBin Meng * Part of this file is adapted from coreboot
652f952bfSBin Meng * src/arch/x86/include/arch/cpu.h and
752f952bfSBin Meng * src/arch/x86/lib/cpu.c
87bddac94SSimon Glass */
97bddac94SSimon Glass
1052f952bfSBin Meng #ifndef _ASM_CPU_H
1152f952bfSBin Meng #define _ASM_CPU_H
1252f952bfSBin Meng
1352f952bfSBin Meng enum {
1452f952bfSBin Meng X86_VENDOR_INVALID = 0,
1552f952bfSBin Meng X86_VENDOR_INTEL,
1652f952bfSBin Meng X86_VENDOR_CYRIX,
1752f952bfSBin Meng X86_VENDOR_AMD,
1852f952bfSBin Meng X86_VENDOR_UMC,
1952f952bfSBin Meng X86_VENDOR_NEXGEN,
2052f952bfSBin Meng X86_VENDOR_CENTAUR,
2152f952bfSBin Meng X86_VENDOR_RISE,
2252f952bfSBin Meng X86_VENDOR_TRANSMETA,
2352f952bfSBin Meng X86_VENDOR_NSC,
2452f952bfSBin Meng X86_VENDOR_SIS,
2552f952bfSBin Meng X86_VENDOR_ANY = 0xfe,
2652f952bfSBin Meng X86_VENDOR_UNKNOWN = 0xff
2752f952bfSBin Meng };
2852f952bfSBin Meng
297dfe8bdeSSimon Glass /* Global descriptor table (GDT) bits */
307dfe8bdeSSimon Glass enum {
317dfe8bdeSSimon Glass GDT_4KB = 1ULL << 55,
327dfe8bdeSSimon Glass GDT_32BIT = 1ULL << 54,
337dfe8bdeSSimon Glass GDT_LONG = 1ULL << 53,
347dfe8bdeSSimon Glass GDT_PRESENT = 1ULL << 47,
357dfe8bdeSSimon Glass GDT_NOTSYS = 1ULL << 44,
367dfe8bdeSSimon Glass GDT_CODE = 1ULL << 43,
377dfe8bdeSSimon Glass GDT_LIMIT_LOW_SHIFT = 0,
387dfe8bdeSSimon Glass GDT_LIMIT_LOW_MASK = 0xffff,
397dfe8bdeSSimon Glass GDT_LIMIT_HIGH_SHIFT = 48,
407dfe8bdeSSimon Glass GDT_LIMIT_HIGH_MASK = 0xf,
417dfe8bdeSSimon Glass GDT_BASE_LOW_SHIFT = 16,
427dfe8bdeSSimon Glass GDT_BASE_LOW_MASK = 0xffff,
437dfe8bdeSSimon Glass GDT_BASE_HIGH_SHIFT = 56,
447dfe8bdeSSimon Glass GDT_BASE_HIGH_MASK = 0xf,
457dfe8bdeSSimon Glass };
467dfe8bdeSSimon Glass
4798655f3aSSimon Glass /*
4898655f3aSSimon Glass * System controllers in an x86 system. We mostly need to just find these and
4925d5352cSSimon Glass * use them on PCI. At some point these might have their own uclass (e.g.
5025d5352cSSimon Glass * UCLASS_VIDEO for the GMA device).
5198655f3aSSimon Glass */
5298655f3aSSimon Glass enum {
5398655f3aSSimon Glass X86_NONE,
5498655f3aSSimon Glass X86_SYSCON_ME, /* Intel Management Engine */
557ac99be6SSimon Glass X86_SYSCON_PINCONF, /* Intel x86 pin configuration */
56ca0d29e4SAndy Shevchenko X86_SYSCON_PMU, /* Power Management Unit */
57bb416465SFelipe Balbi X86_SYSCON_SCU, /* System Controller Unit */
5898655f3aSSimon Glass };
5998655f3aSSimon Glass
6052f952bfSBin Meng struct cpuid_result {
6152f952bfSBin Meng uint32_t eax;
6252f952bfSBin Meng uint32_t ebx;
6352f952bfSBin Meng uint32_t ecx;
6452f952bfSBin Meng uint32_t edx;
6552f952bfSBin Meng };
6652f952bfSBin Meng
6752f952bfSBin Meng /*
6852f952bfSBin Meng * Generic CPUID function
6952f952bfSBin Meng */
cpuid(int op)7052f952bfSBin Meng static inline struct cpuid_result cpuid(int op)
7152f952bfSBin Meng {
7252f952bfSBin Meng struct cpuid_result result;
7352f952bfSBin Meng asm volatile(
7452f952bfSBin Meng "mov %%ebx, %%edi;"
7552f952bfSBin Meng "cpuid;"
7652f952bfSBin Meng "mov %%ebx, %%esi;"
7752f952bfSBin Meng "mov %%edi, %%ebx;"
7852f952bfSBin Meng : "=a" (result.eax),
7952f952bfSBin Meng "=S" (result.ebx),
8052f952bfSBin Meng "=c" (result.ecx),
8152f952bfSBin Meng "=d" (result.edx)
8252f952bfSBin Meng : "0" (op)
8352f952bfSBin Meng : "edi");
8452f952bfSBin Meng return result;
8552f952bfSBin Meng }
8652f952bfSBin Meng
8752f952bfSBin Meng /*
8852f952bfSBin Meng * Generic Extended CPUID function
8952f952bfSBin Meng */
cpuid_ext(int op,unsigned ecx)9052f952bfSBin Meng static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
9152f952bfSBin Meng {
9252f952bfSBin Meng struct cpuid_result result;
9352f952bfSBin Meng asm volatile(
9452f952bfSBin Meng "mov %%ebx, %%edi;"
9552f952bfSBin Meng "cpuid;"
9652f952bfSBin Meng "mov %%ebx, %%esi;"
9752f952bfSBin Meng "mov %%edi, %%ebx;"
9852f952bfSBin Meng : "=a" (result.eax),
9952f952bfSBin Meng "=S" (result.ebx),
10052f952bfSBin Meng "=c" (result.ecx),
10152f952bfSBin Meng "=d" (result.edx)
10252f952bfSBin Meng : "0" (op), "2" (ecx)
10352f952bfSBin Meng : "edi");
10452f952bfSBin Meng return result;
10552f952bfSBin Meng }
10652f952bfSBin Meng
10752f952bfSBin Meng /*
10852f952bfSBin Meng * CPUID functions returning a single datum
10952f952bfSBin Meng */
cpuid_eax(unsigned int op)11052f952bfSBin Meng static inline unsigned int cpuid_eax(unsigned int op)
11152f952bfSBin Meng {
11252f952bfSBin Meng unsigned int eax;
11352f952bfSBin Meng
11452f952bfSBin Meng __asm__("mov %%ebx, %%edi;"
11552f952bfSBin Meng "cpuid;"
11652f952bfSBin Meng "mov %%edi, %%ebx;"
11752f952bfSBin Meng : "=a" (eax)
11852f952bfSBin Meng : "0" (op)
11952f952bfSBin Meng : "ecx", "edx", "edi");
12052f952bfSBin Meng return eax;
12152f952bfSBin Meng }
12252f952bfSBin Meng
cpuid_ebx(unsigned int op)12352f952bfSBin Meng static inline unsigned int cpuid_ebx(unsigned int op)
12452f952bfSBin Meng {
12552f952bfSBin Meng unsigned int eax, ebx;
12652f952bfSBin Meng
12752f952bfSBin Meng __asm__("mov %%ebx, %%edi;"
12852f952bfSBin Meng "cpuid;"
12952f952bfSBin Meng "mov %%ebx, %%esi;"
13052f952bfSBin Meng "mov %%edi, %%ebx;"
13152f952bfSBin Meng : "=a" (eax), "=S" (ebx)
13252f952bfSBin Meng : "0" (op)
13352f952bfSBin Meng : "ecx", "edx", "edi");
13452f952bfSBin Meng return ebx;
13552f952bfSBin Meng }
13652f952bfSBin Meng
cpuid_ecx(unsigned int op)13752f952bfSBin Meng static inline unsigned int cpuid_ecx(unsigned int op)
13852f952bfSBin Meng {
13952f952bfSBin Meng unsigned int eax, ecx;
14052f952bfSBin Meng
14152f952bfSBin Meng __asm__("mov %%ebx, %%edi;"
14252f952bfSBin Meng "cpuid;"
14352f952bfSBin Meng "mov %%edi, %%ebx;"
14452f952bfSBin Meng : "=a" (eax), "=c" (ecx)
14552f952bfSBin Meng : "0" (op)
14652f952bfSBin Meng : "edx", "edi");
14752f952bfSBin Meng return ecx;
14852f952bfSBin Meng }
14952f952bfSBin Meng
cpuid_edx(unsigned int op)15052f952bfSBin Meng static inline unsigned int cpuid_edx(unsigned int op)
15152f952bfSBin Meng {
15252f952bfSBin Meng unsigned int eax, edx;
15352f952bfSBin Meng
15452f952bfSBin Meng __asm__("mov %%ebx, %%edi;"
15552f952bfSBin Meng "cpuid;"
15652f952bfSBin Meng "mov %%edi, %%ebx;"
15752f952bfSBin Meng : "=a" (eax), "=d" (edx)
15852f952bfSBin Meng : "0" (op)
15952f952bfSBin Meng : "ecx", "edi");
16052f952bfSBin Meng return edx;
16152f952bfSBin Meng }
16252f952bfSBin Meng
1634b57414aSSimon Glass #if !CONFIG_IS_ENABLED(X86_64)
1644b57414aSSimon Glass
16552f952bfSBin Meng /* Standard macro to see if a specific flag is changeable */
flag_is_changeable_p(uint32_t flag)16652f952bfSBin Meng static inline int flag_is_changeable_p(uint32_t flag)
16752f952bfSBin Meng {
16852f952bfSBin Meng uint32_t f1, f2;
16952f952bfSBin Meng
17052f952bfSBin Meng asm(
17152f952bfSBin Meng "pushfl\n\t"
17252f952bfSBin Meng "pushfl\n\t"
17352f952bfSBin Meng "popl %0\n\t"
17452f952bfSBin Meng "movl %0,%1\n\t"
17552f952bfSBin Meng "xorl %2,%0\n\t"
17652f952bfSBin Meng "pushl %0\n\t"
17752f952bfSBin Meng "popfl\n\t"
17852f952bfSBin Meng "pushfl\n\t"
17952f952bfSBin Meng "popl %0\n\t"
18052f952bfSBin Meng "popfl\n\t"
18152f952bfSBin Meng : "=&r" (f1), "=&r" (f2)
18252f952bfSBin Meng : "ir" (flag));
18352f952bfSBin Meng return ((f1^f2) & flag) != 0;
18452f952bfSBin Meng }
1854b57414aSSimon Glass #endif
1867bddac94SSimon Glass
mfence(void)187837a136fSSimon Glass static inline void mfence(void)
188837a136fSSimon Glass {
189837a136fSSimon Glass __asm__ __volatile__("mfence" : : : "memory");
190837a136fSSimon Glass }
191837a136fSSimon Glass
1927bddac94SSimon Glass /**
1937bddac94SSimon Glass * cpu_enable_paging_pae() - Enable PAE-paging
1947bddac94SSimon Glass *
19552f952bfSBin Meng * @cr3: Value to set in cr3 (PDPT or PML4T)
1967bddac94SSimon Glass */
1977bddac94SSimon Glass void cpu_enable_paging_pae(ulong cr3);
1987bddac94SSimon Glass
1997bddac94SSimon Glass /**
2007bddac94SSimon Glass * cpu_disable_paging_pae() - Disable paging and PAE
2017bddac94SSimon Glass */
2027bddac94SSimon Glass void cpu_disable_paging_pae(void);
2037bddac94SSimon Glass
20492cc94a1SSimon Glass /**
20592cc94a1SSimon Glass * cpu_has_64bit() - Check if the CPU has 64-bit support
20692cc94a1SSimon Glass *
20792cc94a1SSimon Glass * @return 1 if this CPU supports long mode (64-bit), 0 if not
20892cc94a1SSimon Glass */
20992cc94a1SSimon Glass int cpu_has_64bit(void);
21092cc94a1SSimon Glass
211200182a7SSimon Glass /**
21252f952bfSBin Meng * cpu_vendor_name() - Get CPU vendor name
21352f952bfSBin Meng *
21452f952bfSBin Meng * @vendor: CPU vendor enumeration number
21552f952bfSBin Meng *
21652f952bfSBin Meng * @return: Address to hold the CPU vendor name string
21752f952bfSBin Meng */
21852f952bfSBin Meng const char *cpu_vendor_name(int vendor);
21952f952bfSBin Meng
220727c1a98SSimon Glass #define CPU_MAX_NAME_LEN 49
221727c1a98SSimon Glass
22252f952bfSBin Meng /**
223727c1a98SSimon Glass * cpu_get_name() - Get the name of the current cpu
22452f952bfSBin Meng *
225727c1a98SSimon Glass * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
226727c1a98SSimon Glass * @return pointer to name, which will likely be a few bytes after the start
227727c1a98SSimon Glass * of @name
228727c1a98SSimon Glass * \0 terminator
22952f952bfSBin Meng */
230727c1a98SSimon Glass char *cpu_get_name(char *name);
23152f952bfSBin Meng
23252f952bfSBin Meng /**
233200182a7SSimon Glass * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
234200182a7SSimon Glass *
235200182a7SSimon Glass * The kernel is uncompressed and the 64-bit entry point is expected to be
236200182a7SSimon Glass * at @target.
237200182a7SSimon Glass *
238200182a7SSimon Glass * This function is used internally - see cpu_jump_to_64bit() for a more
239200182a7SSimon Glass * useful function.
240200182a7SSimon Glass *
241200182a7SSimon Glass * @pgtable: Address of 24KB area containing the page table
242200182a7SSimon Glass * @setup_base: Pointer to the setup.bin information for the kernel
243200182a7SSimon Glass * @target: Pointer to the start of the kernel image
244200182a7SSimon Glass */
245200182a7SSimon Glass void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
246200182a7SSimon Glass
247200182a7SSimon Glass /**
2486f92ed8fSSimon Glass * cpu_call32() - Jump to a 32-bit entry point
2496f92ed8fSSimon Glass *
2506f92ed8fSSimon Glass * @code_seg32: 32-bit code segment to use (GDT offset, e.g. 0x20)
2516f92ed8fSSimon Glass * @target: Pointer to the start of the 32-bit U-Boot image/entry point
2526f92ed8fSSimon Glass * @table: Pointer to start of info table to pass to U-Boot
2536f92ed8fSSimon Glass */
2546f92ed8fSSimon Glass void cpu_call32(ulong code_seg32, ulong target, ulong table);
2556f92ed8fSSimon Glass
2566f92ed8fSSimon Glass /**
257200182a7SSimon Glass * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
258200182a7SSimon Glass *
259200182a7SSimon Glass * The kernel is uncompressed and the 64-bit entry point is expected to be
260200182a7SSimon Glass * at @target.
261200182a7SSimon Glass *
262200182a7SSimon Glass * @setup_base: Pointer to the setup.bin information for the kernel
263200182a7SSimon Glass * @target: Pointer to the start of the kernel image
264200182a7SSimon Glass */
265200182a7SSimon Glass int cpu_jump_to_64bit(ulong setup_base, ulong target);
266200182a7SSimon Glass
267342727acSSimon Glass /**
268fa5fcb3bSSimon Glass * cpu_jump_to_64bit_uboot() - special function to jump from SPL to U-Boot
269fa5fcb3bSSimon Glass *
270fa5fcb3bSSimon Glass * This handles calling from 32-bit SPL to 64-bit U-Boot.
271fa5fcb3bSSimon Glass *
272fa5fcb3bSSimon Glass * @target: Address of U-Boot in RAM
273fa5fcb3bSSimon Glass */
274fa5fcb3bSSimon Glass int cpu_jump_to_64bit_uboot(ulong target);
275fa5fcb3bSSimon Glass
276fa5fcb3bSSimon Glass /**
277342727acSSimon Glass * cpu_get_family_model() - Get the family and model for the CPU
278342727acSSimon Glass *
279342727acSSimon Glass * @return the CPU ID masked with 0x0fff0ff0
280342727acSSimon Glass */
281342727acSSimon Glass u32 cpu_get_family_model(void);
282342727acSSimon Glass
283342727acSSimon Glass /**
284342727acSSimon Glass * cpu_get_stepping() - Get the stepping value for the CPU
285342727acSSimon Glass *
286342727acSSimon Glass * @return the CPU ID masked with 0xf
287342727acSSimon Glass */
288342727acSSimon Glass u32 cpu_get_stepping(void);
289342727acSSimon Glass
2907bddac94SSimon Glass #endif
291