xref: /openbmc/u-boot/arch/x86/include/asm/cpu.h (revision 7ac99be6)
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 
4898655f3aSSimon Glass /*
4998655f3aSSimon Glass  * System controllers in an x86 system. We mostly need to just find these and
5025d5352cSSimon Glass  * use them on PCI. At some point these might have their own uclass (e.g.
5125d5352cSSimon Glass  * UCLASS_VIDEO for the GMA device).
5298655f3aSSimon Glass  */
5398655f3aSSimon Glass enum {
5498655f3aSSimon Glass 	X86_NONE,
5598655f3aSSimon Glass 	X86_SYSCON_ME,		/* Intel Management Engine */
5625d5352cSSimon Glass 	X86_SYSCON_GMA,		/* Intel Graphics Media Accelerator */
57*7ac99be6SSimon Glass 	X86_SYSCON_PINCONF,	/* Intel x86 pin configuration */
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  */
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  */
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  */
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 
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 
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 
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 
16352f952bfSBin Meng /* Standard macro to see if a specific flag is changeable */
16452f952bfSBin Meng static inline int flag_is_changeable_p(uint32_t flag)
16552f952bfSBin Meng {
16652f952bfSBin Meng 	uint32_t f1, f2;
16752f952bfSBin Meng 
16852f952bfSBin Meng 	asm(
16952f952bfSBin Meng 		"pushfl\n\t"
17052f952bfSBin Meng 		"pushfl\n\t"
17152f952bfSBin Meng 		"popl %0\n\t"
17252f952bfSBin Meng 		"movl %0,%1\n\t"
17352f952bfSBin Meng 		"xorl %2,%0\n\t"
17452f952bfSBin Meng 		"pushl %0\n\t"
17552f952bfSBin Meng 		"popfl\n\t"
17652f952bfSBin Meng 		"pushfl\n\t"
17752f952bfSBin Meng 		"popl %0\n\t"
17852f952bfSBin Meng 		"popfl\n\t"
17952f952bfSBin Meng 		: "=&r" (f1), "=&r" (f2)
18052f952bfSBin Meng 		: "ir" (flag));
18152f952bfSBin Meng 	return ((f1^f2) & flag) != 0;
18252f952bfSBin Meng }
1837bddac94SSimon Glass 
184837a136fSSimon Glass static inline void mfence(void)
185837a136fSSimon Glass {
186837a136fSSimon Glass 	__asm__ __volatile__("mfence" : : : "memory");
187837a136fSSimon Glass }
188837a136fSSimon Glass 
1897bddac94SSimon Glass /**
1907bddac94SSimon Glass  * cpu_enable_paging_pae() - Enable PAE-paging
1917bddac94SSimon Glass  *
19252f952bfSBin Meng  * @cr3:	Value to set in cr3 (PDPT or PML4T)
1937bddac94SSimon Glass  */
1947bddac94SSimon Glass void cpu_enable_paging_pae(ulong cr3);
1957bddac94SSimon Glass 
1967bddac94SSimon Glass /**
1977bddac94SSimon Glass  * cpu_disable_paging_pae() - Disable paging and PAE
1987bddac94SSimon Glass  */
1997bddac94SSimon Glass void cpu_disable_paging_pae(void);
2007bddac94SSimon Glass 
20192cc94a1SSimon Glass /**
20292cc94a1SSimon Glass  * cpu_has_64bit() - Check if the CPU has 64-bit support
20392cc94a1SSimon Glass  *
20492cc94a1SSimon Glass  * @return 1 if this CPU supports long mode (64-bit), 0 if not
20592cc94a1SSimon Glass  */
20692cc94a1SSimon Glass int cpu_has_64bit(void);
20792cc94a1SSimon Glass 
208200182a7SSimon Glass /**
20952f952bfSBin Meng  * cpu_vendor_name() - Get CPU vendor name
21052f952bfSBin Meng  *
21152f952bfSBin Meng  * @vendor:	CPU vendor enumeration number
21252f952bfSBin Meng  *
21352f952bfSBin Meng  * @return:	Address to hold the CPU vendor name string
21452f952bfSBin Meng  */
21552f952bfSBin Meng const char *cpu_vendor_name(int vendor);
21652f952bfSBin Meng 
217727c1a98SSimon Glass #define CPU_MAX_NAME_LEN	49
218727c1a98SSimon Glass 
21952f952bfSBin Meng /**
220727c1a98SSimon Glass  * cpu_get_name() - Get the name of the current cpu
22152f952bfSBin Meng  *
222727c1a98SSimon Glass  * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
223727c1a98SSimon Glass  * @return pointer to name, which will likely be a few bytes after the start
224727c1a98SSimon Glass  * of @name
225727c1a98SSimon Glass  * \0 terminator
22652f952bfSBin Meng  */
227727c1a98SSimon Glass char *cpu_get_name(char *name);
22852f952bfSBin Meng 
22952f952bfSBin Meng /**
230200182a7SSimon Glass  * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
231200182a7SSimon Glass  *
232200182a7SSimon Glass  * The kernel is uncompressed and the 64-bit entry point is expected to be
233200182a7SSimon Glass  * at @target.
234200182a7SSimon Glass  *
235200182a7SSimon Glass  * This function is used internally - see cpu_jump_to_64bit() for a more
236200182a7SSimon Glass  * useful function.
237200182a7SSimon Glass  *
238200182a7SSimon Glass  * @pgtable:	Address of 24KB area containing the page table
239200182a7SSimon Glass  * @setup_base:	Pointer to the setup.bin information for the kernel
240200182a7SSimon Glass  * @target:	Pointer to the start of the kernel image
241200182a7SSimon Glass  */
242200182a7SSimon Glass void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
243200182a7SSimon Glass 
244200182a7SSimon Glass /**
2456f92ed8fSSimon Glass  * cpu_call32() - Jump to a 32-bit entry point
2466f92ed8fSSimon Glass  *
2476f92ed8fSSimon Glass  * @code_seg32:	32-bit code segment to use (GDT offset, e.g. 0x20)
2486f92ed8fSSimon Glass  * @target:	Pointer to the start of the 32-bit U-Boot image/entry point
2496f92ed8fSSimon Glass  * @table:	Pointer to start of info table to pass to U-Boot
2506f92ed8fSSimon Glass  */
2516f92ed8fSSimon Glass void cpu_call32(ulong code_seg32, ulong target, ulong table);
2526f92ed8fSSimon Glass 
2536f92ed8fSSimon Glass /**
254200182a7SSimon Glass  * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
255200182a7SSimon Glass  *
256200182a7SSimon Glass  * The kernel is uncompressed and the 64-bit entry point is expected to be
257200182a7SSimon Glass  * at @target.
258200182a7SSimon Glass  *
259200182a7SSimon Glass  * @setup_base:	Pointer to the setup.bin information for the kernel
260200182a7SSimon Glass  * @target:	Pointer to the start of the kernel image
261200182a7SSimon Glass  */
262200182a7SSimon Glass int cpu_jump_to_64bit(ulong setup_base, ulong target);
263200182a7SSimon Glass 
264342727acSSimon Glass /**
265342727acSSimon Glass  * cpu_get_family_model() - Get the family and model for the CPU
266342727acSSimon Glass  *
267342727acSSimon Glass  * @return the CPU ID masked with 0x0fff0ff0
268342727acSSimon Glass  */
269342727acSSimon Glass u32 cpu_get_family_model(void);
270342727acSSimon Glass 
271342727acSSimon Glass /**
272342727acSSimon Glass  * cpu_get_stepping() - Get the stepping value for the CPU
273342727acSSimon Glass  *
274342727acSSimon Glass  * @return the CPU ID masked with 0xf
275342727acSSimon Glass  */
276342727acSSimon Glass u32 cpu_get_stepping(void);
277342727acSSimon Glass 
2787bddac94SSimon Glass #endif
279