2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <arm/cpu_data_internal.h>
30 #include <arm/machine_routines.h>
31 #include <arm64/monotonic.h>
32 #include <kern/assert.h> /* static_assert, assert */
33 #include <kern/debug.h> /* panic */
34 #include <kern/monotonic.h>
35 #include <machine/limits.h> /* CHAR_BIT */
36 #include <stdatomic.h>
39 #include <sys/errno.h>
40 #include <sys/monotonic.h>
41 #include <pexpert/arm64/board_config.h>
42 #include <pexpert/pexpert.h>
44 #pragma mark core counters
46 bool mt_core_supported
= true;
47 void mt_fiq_internal(uint64_t upmsr
);
50 * PMC[0-1] are the 48-bit fixed counters -- PMC0 is cycles and PMC1 is
51 * instructions (see arm64/monotonic.h).
53 * PMC2+ are currently handled by kpc.
56 #define PMC0 "s3_2_c15_c0_0"
57 #define PMC1 "s3_2_c15_c1_0"
58 #define PMC2 "s3_2_c15_c2_0"
59 #define PMC3 "s3_2_c15_c3_0"
60 #define PMC4 "s3_2_c15_c4_0"
61 #define PMC5 "s3_2_c15_c5_0"
62 #define PMC6 "s3_2_c15_c6_0"
63 #define PMC7 "s3_2_c15_c7_0"
64 #define PMC8 "s3_2_c15_c9_0"
65 #define PMC9 "s3_2_c15_c10_0"
71 * PMC0's offset into a core's PIO range.
73 * This allows cores to remotely query another core's counters.
76 #define PIO_PMC0_OFFSET (0x200)
79 * The offset of the counter in the configuration registers. Post-Hurricane
80 * devices have additional counters that need a larger shift than the original
83 * XXX For now, just support the lower-numbered counters.
85 #define CTR_POS(CTR) (CTR)
88 * PMCR0 is the main control register for the performance monitor. It
89 * controls whether the counters are enabled, how they deliver interrupts, and
93 #define PMCR0 "s3_1_c15_c0_0"
95 #define PMCR0_CTR_EN(CTR) (UINT64_C(1) << CTR_POS(CTR))
96 #define PMCR0_FIXED_EN (PMCR0_CTR_EN(CYCLES) | PMCR0_CTR_EN(INSTRS))
97 /* how interrupts are delivered on a PMI */
100 PMCR0_INTGEN_PMI
= 1,
101 PMCR0_INTGEN_AIC
= 2,
102 PMCR0_INTGEN_HALT
= 3,
103 PMCR0_INTGEN_FIQ
= 4,
105 #define PMCR0_INTGEN_SET(INT) ((uint64_t)(INT) << 8)
106 /* use AIC for backwards compatibility with kpc */
107 #define PMCR0_INTGEN_INIT PMCR0_INTGEN_SET(PMCR0_INTGEN_AIC)
108 /* set by hardware if a PMI was delivered */
109 #define PMCR0_PMAI (UINT64_C(1) << 11)
110 #define PMCR0_PMI_EN(CTR) (UINT64_C(1) << (12 + CTR_POS(CTR)))
111 /* fixed counters are always counting XXX probably need to just set this to all true */
112 #define PMCR0_PMI_INIT (PMCR0_PMI_EN(CYCLES) | PMCR0_PMI_EN(INSTRS))
113 /* disable counting on a PMI (except for AIC interrupts) */
114 #define PMCR0_DISCNT_EN (UINT64_C(1) << 20)
115 /* block PMIs until ERET retires */
116 #define PMCR0_WFRFE_EN (UINT64_C(1) << 22)
117 /* count global (not just core-local) L2C events */
118 #define PMCR0_L2CGLOBAL_EN (UINT64_C(1) << 23)
119 /* user mode access to configuration registers */
120 #define PMCR0_USEREN_EN (UINT64_C(1) << 30)
122 /* XXX this needs to be synchronized with kpc... */
123 #define PMCR0_INIT (PMCR0_INTGEN_INIT | PMCR0_PMI_INIT | PMCR0_DISCNT_EN)
126 * PMCR1 controls which execution modes count events.
129 #define PMCR1 "s3_1_c15_c1_0"
131 #define PMCR1_EL0A32_EN(CTR) (UINT64_C(1) << (0 + CTR_POS(CTR)))
132 #define PMCR1_EL0A64_EN(CTR) (UINT64_C(1) << (8 + CTR_POS(CTR)))
133 #define PMCR1_EL1A64_EN(CTR) (UINT64_C(1) << (16 + CTR_POS(CTR)))
134 /* PMCR1_EL3A64 is not supported on systems with no monitor */
135 #if defined(APPLEHURRICANE)
136 #define PMCR1_EL3A64_EN(CTR) UINT64_C(0)
138 #define PMCR1_EL3A64_EN(CTR) (UINT64_C(1) << (24 + CTR_POS(CTR)))
140 #define PMCR1_ALL_EN(CTR) (PMCR1_EL0A32_EN(CTR) | PMCR1_EL0A64_EN(CTR) | \
141 PMCR1_EL1A64_EN(CTR) | PMCR1_EL3A64_EN(CTR))
143 /* fixed counters always count in all modes */
144 #define PMCR1_INIT (PMCR1_ALL_EN(CYCLES) | PMCR1_ALL_EN(INSTRS))
147 core_init_execution_modes(void)
151 pmcr1
= __builtin_arm_rsr64(PMCR1
);
153 __builtin_arm_wsr64(PMCR1
, pmcr1
);
157 * PMSR reports the overflow status of all counters.
160 #define PMSR "s3_1_c15_c13_0"
162 #define PMSR_OVF(CTR) (UINT64_C(1) << (CTR))
165 * PMCR2 controls watchpoint registers.
167 * PMCR3 controls breakpoints and address matching.
169 * PMCR4 controls opcode matching.
172 #define PMCR2 "s3_1_c15_c2_0"
173 #define PMCR3 "s3_1_c15_c3_0"
174 #define PMCR4 "s3_1_c15_c4_0"
177 * PMCR_AFFINITY does ??? XXX.
180 #define PMCR_AFFINITY "s3_1_c15_c11_0"
190 /* the dev node interface to the core counters is still unsupported */
197 return &getCpuDatap()->cpu_monotonic
;
201 mt_core_snap(unsigned int ctr
)
205 return __builtin_arm_rsr64(PMC0
);
207 return __builtin_arm_rsr64(PMC1
);
209 panic("monotonic: invalid core counter read: %u", ctr
);
215 mt_core_set_snap(unsigned int ctr
, uint64_t count
)
219 __builtin_arm_wsr64(PMC0
, count
);
222 __builtin_arm_wsr64(PMC1
, count
);
225 panic("monotonic: invalid core counter %u write %llu", ctr
, count
);
231 core_set_enabled(void)
235 pmcr0
= __builtin_arm_rsr64(PMCR0
);
236 pmcr0
|= PMCR0_INIT
| PMCR0_FIXED_EN
;
237 __builtin_arm_wsr64(PMCR0
, pmcr0
);
241 core_idle(__unused cpu_data_t
*cpu
)
244 assert(ml_get_interrupts_enabled() == FALSE
);
247 uint64_t pmcr0
= __builtin_arm_rsr64(PMCR0
);
248 if ((pmcr0
& PMCR0_FIXED_EN
) == 0) {
249 panic("monotonic: counters disabled while idling, pmcr0 = 0x%llx\n", pmcr0
);
251 uint64_t pmcr1
= __builtin_arm_rsr64(PMCR1
);
252 if ((pmcr1
& PMCR1_INIT
) == 0) {
253 panic("monotonic: counter modes disabled while idling, pmcr1 = 0x%llx\n", pmcr1
);
257 /* disable counters before updating */
258 __builtin_arm_wsr64(PMCR0
, PMCR0_INIT
);
260 mt_update_fixed_counts();
264 core_run(cpu_data_t
*cpu
)
270 assert(ml_get_interrupts_enabled() == FALSE
);
272 mtc
= &cpu
->cpu_monotonic
;
274 for (int i
= 0; i
< MT_CORE_NFIXED
; i
++) {
275 mt_core_set_snap(i
, mtc
->mtc_snaps
[i
]);
278 /* re-enable the counters */
279 core_init_execution_modes();
281 pmcr0
= __builtin_arm_rsr64(PMCR0
);
282 pmcr0
|= PMCR0_INIT
| PMCR0_FIXED_EN
;
283 __builtin_arm_wsr64(PMCR0
, pmcr0
);
287 core_up(__unused cpu_data_t
*cpu
)
289 assert(ml_get_interrupts_enabled() == FALSE
);
291 core_init_execution_modes();
294 #pragma mark uncore counters
308 uncore_fiq(uint64_t upmsr
)
310 #pragma unused(upmsr)
313 #pragma mark common hooks
316 mt_cpu_idle(cpu_data_t
*cpu
)
322 mt_cpu_run(cpu_data_t
*cpu
)
328 mt_cpu_down(cpu_data_t
*cpu
)
334 mt_cpu_up(cpu_data_t
*cpu
)
353 mt_cpu_pmi(cpu_data_t
*cpu
, uint64_t pmsr
)
355 bool found_overflow
= false;
358 assert(ml_get_interrupts_enabled() == FALSE
);
360 (void)atomic_fetch_add_explicit(&mt_pmis
, 1, memory_order_relaxed
);
362 for (int i
= 0; i
< MT_CORE_NFIXED
; i
++) {
363 if (pmsr
& PMSR_OVF(i
)) {
364 mt_cpu_update_count(cpu
, i
);
365 mt_core_set_snap(i
, 0);
366 found_overflow
= true;
370 assert(found_overflow
);
375 mt_fiq_internal(uint64_t upmsr
)
380 #pragma mark dev nodes
382 const struct monotonic_dev monotonic_devs
[] = {
384 .mtd_name
= "monotonic/core",
385 .mtd_init
= core_init
,
390 (sizeof(monotonic_devs
) / sizeof(monotonic_devs
[0])) == MT_NDEVS
,
391 "MT_NDEVS macro should be same as the length of monotonic_devs");