1 /*
2  * Copyright (c) 2022 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <cpuid.h> /* Header provided by the toolchain. */
8 
9 #include <zephyr/kernel_structs.h>
10 #include <zephyr/arch/x86/cpuid.h>
11 #include <zephyr/kernel.h>
12 
z_x86_cpuid_extended_features(void)13 uint32_t z_x86_cpuid_extended_features(void)
14 {
15 	uint32_t eax, ebx, ecx = 0U, edx;
16 
17 	if (__get_cpuid(CPUID_EXTENDED_FEATURES_LVL,
18 			&eax, &ebx, &ecx, &edx) == 0) {
19 		return 0;
20 	}
21 
22 	return edx;
23 }
24 
25 #define INITIAL_APIC_ID_SHIFT (24)
26 #define INITIAL_APIC_ID_MASK (0xFF)
27 
z_x86_cpuid_get_current_physical_apic_id(void)28 uint8_t z_x86_cpuid_get_current_physical_apic_id(void)
29 {
30 	uint32_t eax, ebx, ecx, edx;
31 
32 	if (IS_ENABLED(CONFIG_X2APIC)) {
33 		/* leaf 0x1F should be used first prior to using 0x0B */
34 		if (__get_cpuid(CPUID_EXTENDED_TOPOLOGY_ENUMERATION_V2,
35 				&eax, &ebx, &ecx, &edx) == 0) {
36 			if (__get_cpuid(CPUID_EXTENDED_TOPOLOGY_ENUMERATION,
37 					&eax, &ebx, &ecx, &edx) == 0) {
38 				return 0;
39 			}
40 		}
41 	} else {
42 		if (__get_cpuid(CPUID_BASIC_INFO_1,
43 				&eax, &ebx, &ecx, &edx) == 0) {
44 			return 0;
45 		}
46 
47 		edx = (ebx >> INITIAL_APIC_ID_SHIFT);
48 	}
49 
50 	return (uint8_t)(edx & INITIAL_APIC_ID_MASK);
51 }
52