1 /*
2 * Copyright (c) 2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #pragma once
30
31 #if !defined(__ASSEMBLER__)
32
33 #include <stdbool.h>
34 #include <sys/cdefs.h>
35
36 /**
37 * Defines the core type of the executing CPU.
38 */
39 __enum_closed_decl(arm64_core_type_t, unsigned int, {
40 E_CORE = MPIDR_CORETYPE_ACC_E,
41 P_CORE = MPIDR_CORETYPE_ACC_P,
42 });
43
44 /*
45 * Get the core type of the executing CPU.
46 *
47 * @return Whether the executing CPU is an E-core, P-core, or non-PE core.
48 */
49 static inline arm64_core_type_t
arm64_core_type(void)50 arm64_core_type(void)
51 {
52 return (arm64_core_type_t)((__builtin_arm_rsr64("MPIDR_EL1") >> MPIDR_CORETYPE_SHIFT) & MPIDR_CORETYPE_MASK);
53 }
54
55 /*
56 * Convenience wrapper around arm64_core_type() which determines whether the
57 * executing CPU is an E-core.
58 *
59 * @return Whether the executing CPU is an E-core.
60 */
61 static inline bool
arm64_is_e_core(void)62 arm64_is_e_core(void)
63 {
64 return arm64_core_type() == E_CORE;
65 }
66
67
68 /*
69 * Convenience wrapper around arm64_core_type() which determines whether the
70 * executing CPU is a P-core.
71 *
72 * @return Whether the executing CPU is a P-core.
73 */
74 static inline bool
arm64_is_p_core(void)75 arm64_is_p_core(void)
76 {
77 return arm64_core_type() == P_CORE;
78 }
79
80 /*
81 * Convert a core type to a printable string.
82 *
83 * @param type The core type to convert.
84 *
85 * @return String describing whether the given core type corresponds to an
86 * E-core, P-core, or non-PE core.
87 */
88 static inline const char *
arm64_core_type_to_string(arm64_core_type_t core_type)89 arm64_core_type_to_string(arm64_core_type_t core_type)
90 {
91 switch (core_type) {
92 case E_CORE:
93 return "E-core";
94 case P_CORE:
95 return "P-core";
96 default:
97 return "<< UNKNOWN OR INVALID CORE TYPE >>";
98 }
99 }
100
101 /*
102 * Convenience wrapper around arm64_core_type_to_string() which gets a printable
103 * string describing the core type of the executing CPU.
104 *
105 * @return String describing whether the executing CPU is an E-core, P-core,
106 * or non-PE core.
107 */
108 static inline const char *
arm64_core_type_as_string(void)109 arm64_core_type_as_string(void)
110 {
111 return arm64_core_type_to_string(arm64_core_type());
112 }
113
114 #endif /* !defined(__ASSEMBLER__) */
115