2 * Copyright (c) 2012-2016 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/cpu_internal.h>
31 #include <kern/kalloc.h>
33 #include <kern/thread.h>
34 #include <kern/processor.h>
35 #include <mach/mach_types.h>
36 #include <machine/machine_routines.h>
38 #include <sys/errno.h>
41 #include <kern/monotonic.h>
42 #endif /* MONOTONIC */
45 * PMCs 8 and 9 were added to Hurricane and to maintain the existing bit
46 * positions of the other PMCs, their configuration bits start at position 32.
48 #define PMCR_PMC_8_9_OFFSET (32)
49 #define PMCR_PMC_8_9_SHIFT(PMC) (((PMC) - 8) + PMCR_PMC_8_9_OFFSET)
50 #define PMCR_PMC_SHIFT(PMC) (((PMC) <= 7) ? (PMC) : \
51 PMCR_PMC_8_9_SHIFT(PMC))
54 * PMCR0 controls enabling, interrupts, and overflow of performance counters.
58 #define PMCR0_PMC_ENABLE_MASK(PMC) (UINT64_C(0x1) << PMCR_PMC_SHIFT(PMC))
59 #define PMCR0_PMC_DISABLE_MASK(PMC) (~PMCR0_PMC_ENABLE_MASK(PMC))
61 /* how interrupts are generated on PMIs */
62 #define PMCR0_INTGEN_SHIFT (8)
63 #define PMCR0_INTGEN_MASK (UINT64_C(0x7) << PMCR0_INTGEN_SHIFT)
64 #define PMCR0_INTGEN_OFF (UINT64_C(0) << PMCR0_INTGEN_SHIFT)
65 #define PMCR0_INTGEN_PMI (UINT64_C(1) << PMCR0_INTGEN_SHIFT)
66 #define PMCR0_INTGEN_AIC (UINT64_C(2) << PMCR0_INTGEN_SHIFT)
67 #define PMCR0_INTGEN_DBG_HLT (UINT64_C(3) << PMCR0_INTGEN_SHIFT)
68 #define PMCR0_INTGEN_FIQ (UINT64_C(4) << PMCR0_INTGEN_SHIFT)
72 /* set by hardware if PMI was generated */
73 #define PMCR0_PMAI_SHIFT (11)
74 #define PMCR0_PMAI_MASK (UINT64_C(1) << PMCR0_PMAI_SHIFT)
76 /* overflow on a PMC generates an interrupt */
77 #define PMCR0_PMI_OFFSET (12)
78 #define PMCR0_PMI_SHIFT(PMC) (PMCR0_PMI_OFFSET + PMCR_PMC_SHIFT(PMC))
79 #define PMCR0_PMI_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR0_PMI_SHIFT(PMC))
80 #define PMCR0_PMI_DISABLE_MASK(PMC) (~PMCR0_PMI_ENABLE_MASK(PMC))
82 /* disable counting when a PMI is signaled (except for AIC interrupts) */
83 #define PMCR0_DISCNT_SHIFT (20)
84 #define PMCR0_DISCNT_ENABLE_MASK (UINT64_C(1) << PMCR0_DISCNT_SHIFT)
85 #define PMCR0_DISCNT_DISABLE_MASK (~PMCR0_DISCNT_ENABLE_MASK)
89 /* block PMIs until ERET retires */
90 #define PMCR0_WFRFE_SHIFT (22)
91 #define PMCR0_WFRFE_ENABLE_MASK (UINT64_C(1) << PMCR0_WFRE_SHIFT)
92 #define PMCR0_WFRFE_DISABLE_MASK (~PMCR0_WFRFE_ENABLE_MASK)
94 /* count global L2C events */
95 #define PMCR0_L2CGLOBAL_SHIFT (23)
96 #define PMCR0_L2CGLOBAL_ENABLE_MASK (UINT64_C(1) << PMCR0_L2CGLOBAL_SHIFT)
97 #define PMCR0_L2CGLOBAL_DISABLE_MASK (~PMCR0_L2CGLOBAL_ENABLE_MASK)
99 /* allow user mode access to configuration registers */
100 #define PMCR0_USEREN_SHIFT (30)
101 #define PMCR0_USEREN_ENABLE_MASK (UINT64_C(1) << PMCR0_USEREN_SHIFT)
102 #define PMCR0_USEREN_DISABLE_MASK (~PMCR0_USEREN_ENABLE_MASK)
104 /* force the CPMU clocks in case of a clocking bug */
105 #define PMCR0_CLKEN_SHIFT (31)
106 #define PMCR0_CLKEN_ENABLE_MASK (UINT64_C(1) << PMCR0_USEREN_SHIFT)
107 #define PMCR0_CLKEN_DISABLE_MASK (~PMCR0_CLKEN_ENABLE_MASK)
109 /* 32 - 44 mirror the low bits for PMCs 8 and 9 */
111 /* PMCR1 enables counters in different processor modes */
113 #define PMCR1_EL0_A32_OFFSET (0)
114 #define PMCR1_EL0_A64_OFFSET (8)
115 #define PMCR1_EL1_A64_OFFSET (16)
116 #define PMCR1_EL3_A64_OFFSET (24)
118 #define PMCR1_EL0_A32_SHIFT(PMC) (PMCR1_EL0_A32_OFFSET + PMCR_PMC_SHIFT(PMC))
119 #define PMCR1_EL0_A64_SHIFT(PMC) (PMCR1_EL0_A64_OFFSET + PMCR_PMC_SHIFT(PMC))
120 #define PMCR1_EL1_A64_SHIFT(PMC) (PMCR1_EL1_A64_OFFSET + PMCR_PMC_SHIFT(PMC))
121 #define PMCR1_EL3_A64_SHIFT(PMC) (PMCR1_EL0_A64_OFFSET + PMCR_PMC_SHIFT(PMC))
123 #define PMCR1_EL0_A32_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL0_A32_SHIFT(PMC))
124 #define PMCR1_EL0_A64_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL0_A64_SHIFT(PMC))
125 #define PMCR1_EL1_A64_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL1_A64_SHIFT(PMC))
126 /* PMCR1_EL3_A64 is not supported on PMCs 8 and 9 */
128 #define PMCR1_EL3_A64_ENABLE_MASK(PMC) UINT64_C(0)
130 #define PMCR1_EL3_A64_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL3_A64_SHIFT(PMC))
133 #define PMCR1_EL_ALL_ENABLE_MASK(PMC) (PMCR1_EL0_A32_ENABLE_MASK(PMC) | \
134 PMCR1_EL0_A64_ENABLE_MASK(PMC) | \
135 PMCR1_EL1_A64_ENABLE_MASK(PMC) | \
136 PMCR1_EL3_A64_ENABLE_MASK(PMC))
137 #define PMCR1_EL_ALL_DISABLE_MASK(PMC) (~PMCR1_EL_ALL_ENABLE_MASK(PMC))
139 /* PMESR0 and PMESR1 are event selection registers */
141 /* PMESR0 selects which event is counted on PMCs 2, 3, 4, and 5 */
142 /* PMESR1 selects which event is counted on PMCs 6, 7, 8, and 9 */
144 #define PMESR_PMC_WIDTH (8)
145 #define PMESR_PMC_MASK (UINT8_MAX)
146 #define PMESR_SHIFT(PMC, OFF) (8 * ((PMC) - (OFF)))
147 #define PMESR_EVT_MASK(PMC, OFF) (PMESR_PMC_MASK << PMESR_SHIFT(PMC, OFF))
148 #define PMESR_EVT_CLEAR(PMC, OFF) (~PMESR_EVT_MASK(PMC, OFF))
150 #define PMESR_EVT_DECODE(PMESR, PMC, OFF) \
151 (((PMESR) >> PMESR_SHIFT(PMC, OFF)) & PMESR_PMC_MASK)
152 #define PMESR_EVT_ENCODE(EVT, PMC, OFF) \
153 (((EVT) & PMESR_PMC_MASK) << PMESR_SHIFT(PMC, OFF))
155 /* system registers in the CPMU */
157 #define SREG_PMCR0 "S3_1_c15_c0_0"
158 #define SREG_PMCR1 "S3_1_c15_c1_0"
159 #define SREG_PMCR2 "S3_1_c15_c2_0"
160 #define SREG_PMCR3 "S3_1_c15_c3_0"
161 #define SREG_PMCR4 "S3_1_c15_c4_0"
162 #define SREG_PMESR0 "S3_1_c15_c5_0"
163 #define SREG_PMESR1 "S3_1_c15_c6_0"
164 #define SREG_PMSR "S3_1_c15_c13_0"
165 #define SREG_OPMAT0 "S3_1_c15_c7_0"
166 #define SREG_OPMAT1 "S3_1_c15_c8_0"
167 #define SREG_OPMSK0 "S3_1_c15_c9_0"
168 #define SREG_OPMSK1 "S3_1_c15_c10_0"
170 #define SREG_PMC0 "S3_2_c15_c0_0"
171 #define SREG_PMC1 "S3_2_c15_c1_0"
172 #define SREG_PMC2 "S3_2_c15_c2_0"
173 #define SREG_PMC3 "S3_2_c15_c3_0"
174 #define SREG_PMC4 "S3_2_c15_c4_0"
175 #define SREG_PMC5 "S3_2_c15_c5_0"
176 #define SREG_PMC6 "S3_2_c15_c6_0"
177 #define SREG_PMC7 "S3_2_c15_c7_0"
178 #define SREG_PMC8 "S3_2_c15_c9_0"
179 #define SREG_PMC9 "S3_2_c15_c10_0"
181 #if !defined(APPLECYCLONE)
182 #define SREG_PMMMAP "S3_2_c15_c15_0"
183 #define SREG_PMTRHLD2 "S3_2_c15_c14_0"
184 #define SREG_PMTRHLD4 "S3_2_c15_c13_0"
185 #define SREG_PMTRHLD6 "S3_2_c15_c12_0"
189 * The low 8 bits of a configuration words select the event to program on
190 * PMESR{0,1}. Bits 16-19 are mapped to PMCR1 bits.
192 #define CFGWORD_EL0A32EN_MASK (0x10000)
193 #define CFGWORD_EL0A64EN_MASK (0x20000)
194 #define CFGWORD_EL1EN_MASK (0x40000)
195 #define CFGWORD_EL3EN_MASK (0x80000)
196 #define CFGWORD_ALLMODES_MASK (0xf0000)
198 /* ACC offsets for PIO */
199 #define ACC_CPMU_PMC0_OFFSET (0x200)
200 #define ACC_CPMU_PMC8_OFFSET (0x280)
203 * Macros for reading and writing system registers.
205 * SR must be one of the SREG_* defines above.
207 #define SREG_WRITE(SR, V) __asm__ volatile("msr " SR ", %0 ; isb" : : "r"(V))
208 #define SREG_READ(SR) ({ uint64_t VAL; \
209 __asm__ volatile("mrs %0, " SR : "=r"(VAL)); \
213 * Configuration registers that can be controlled by RAWPMU:
215 * All: PMCR2-4, OPMAT0-1, OPMSK0-1.
216 * Typhoon/Twister/Hurricane: PMMMAP, PMTRHLD2/4/6.
218 #if defined(APPLECYCLONE)
219 #define RAWPMU_CONFIG_COUNT 7
221 #define RAWPMU_CONFIG_COUNT 11
224 /* TODO: allocate dynamically */
225 static uint64_t saved_PMCR
[MAX_CPUS
][2];
226 static uint64_t saved_PMESR
[MAX_CPUS
][2];
227 static uint64_t saved_RAWPMU
[MAX_CPUS
][RAWPMU_CONFIG_COUNT
];
228 static uint64_t saved_counter
[MAX_CPUS
][KPC_MAX_COUNTERS
];
229 static uint64_t kpc_running_cfg_pmc_mask
= 0;
230 static uint32_t kpc_running_classes
= 0;
231 static uint32_t kpc_configured
= 0;
233 static int first_time
= 1;
236 * The whitelist is disabled by default on development/debug kernel. This can
237 * be changed via the kpc.disable_whitelist sysctl. The whitelist is enabled on
238 * release kernel and cannot be disabled.
240 #if DEVELOPMENT || DEBUG
241 static boolean_t whitelist_disabled
= TRUE
;
243 static boolean_t whitelist_disabled
= FALSE
;
246 /* List of counter events that are allowed externally */
247 static kpc_config_t whitelist
[] = {
250 #if defined(APPLECYCLONE)
251 0x02, /* CORE_CYCLE */
252 0x19, /* BIU_UPSTREAM_CYCLE */
253 0x1a, /* BIU_DOWNSTREAM_CYCLE */
254 0x22, /* L2C_AGENT_LD */
255 0x23, /* L2C_AGENT_LD_MISS */
256 0x24, /* L2C_AGENT_ST */
257 0x25, /* L2C_AGENT_ST_MISS */
259 0x79, /* INST_THUMB */
261 0x7b, /* INST_BRANCH */
262 0xb4, /* SYNC_DC_LOAD_MISS */
263 0xb5, /* SYNC_DC_STORE_MISS */
264 0xb6, /* SYNC_DTLB_MISS */
265 0xb9, /* SYNC_ST_HIT_YNGR_LD */
266 0xc0, /* SYNC_BR_ANY_MISP */
267 0xce, /* FED_IC_MISS_DEM */
268 0xcf, /* FED_ITLB_MISS */
270 #elif defined(APPLETYPHOON)
271 0x02, /* CORE_CYCLE */
272 0x13, /* BIU_UPSTREAM_CYCLE */
273 0x14, /* BIU_DOWNSTREAM_CYCLE */
274 0x1a, /* L2C_AGENT_LD */
275 0x1b, /* L2C_AGENT_LD_MISS */
276 0x1c, /* L2C_AGENT_ST */
277 0x1d, /* L2C_AGENT_ST_MISS */
279 0x8b, /* INST_THUMB */
281 0x8d, /* INST_BRANCH */
282 0xbf, /* SYNC_DC_LOAD_MISS */
283 0xc0, /* SYNC_DC_STORE_MISS */
284 0xc1, /* SYNC_DTLB_MISS */
285 0xc4, /* SYNC_ST_HIT_YNGR_LD */
286 0xcb, /* SYNC_BR_ANY_MISP */
287 0xd3, /* FED_IC_MISS_DEM */
288 0xd4, /* FED_ITLB_MISS */
290 #elif defined(APPLETWISTER) || defined(APPLEHURRICANE)
291 0x02, /* CORE_CYCLE */
292 0x1a, /* L2C_AGENT_LD */
293 0x1b, /* L2C_AGENT_LD_MISS */
294 0x1c, /* L2C_AGENT_ST */
295 0x1d, /* L2C_AGENT_ST_MISS */
297 0x8b, /* INST_THUMB */
299 0x8d, /* INST_BRANCH */
300 0xbf, /* SYNC_DC_LOAD_MISS */
301 0xc0, /* SYNC_DC_STORE_MISS */
302 0xc1, /* SYNC_DTLB_MISS */
303 0xc4, /* SYNC_ST_HIT_YNGR_LD */
304 0xcb, /* SYNC_BR_ANY_MISP */
305 0xd3, /* FED_IC_MISS_DEM */
306 0xd4, /* FED_ITLB_MISS */
309 /* An unknown CPU gets a trivial { NO_EVENT } whitelist. */
312 #define WHITELIST_COUNT (sizeof(whitelist)/sizeof(*whitelist))
315 config_in_whitelist(kpc_config_t cfg
)
319 for (i
= 0; i
< WHITELIST_COUNT
; i
++) {
320 if (cfg
== whitelist
[i
]) {
329 static void dump_regs(void)
332 kprintf("PMCR0 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMCR0
));
333 kprintf("PMCR1 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMCR1
));
334 kprintf("PMCR2 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMCR2
));
335 kprintf("PMCR3 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMCR3
));
336 kprintf("PMCR4 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMCR4
));
337 kprintf("PMESR0 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMESR0
));
338 kprintf("PMESR1 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMESR1
));
340 kprintf("PMC0 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC0
));
341 kprintf("PMC1 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC1
));
342 kprintf("PMC2 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC2
));
343 kprintf("PMC3 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC3
));
344 kprintf("PMC4 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC4
));
345 kprintf("PMC5 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC5
));
346 kprintf("PMC6 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC6
));
347 kprintf("PMC7 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC7
));
349 #if (KPC_ARM64_CONFIGURABLE_COUNT > 6)
350 kprintf("PMC8 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC8
));
351 kprintf("PMC9 = 0x%" PRIx64
"\n", SREG_READ(SREG_PMC9
));
357 enable_counter(uint32_t counter
)
359 int cpuid
= cpu_number();
360 uint64_t pmcr0
= 0, intgen_type
;
361 boolean_t counter_running
, pmi_enabled
, intgen_correct
, enabled
;
363 pmcr0
= SREG_READ(SREG_PMCR0
) | 0x3 /* leave the fixed counters enabled for monotonic */;
365 counter_running
= (pmcr0
& PMCR0_PMC_ENABLE_MASK(counter
)) != 0;
366 pmi_enabled
= (pmcr0
& PMCR0_PMI_ENABLE_MASK(counter
)) != 0;
368 /* TODO this should use the PMI path rather than AIC for the interrupt
371 intgen_type
= PMCR0_INTGEN_AIC
;
372 intgen_correct
= (pmcr0
& PMCR0_INTGEN_MASK
) == intgen_type
;
374 enabled
= counter_running
&& pmi_enabled
&& intgen_correct
;
377 pmcr0
|= PMCR0_PMC_ENABLE_MASK(counter
);
378 pmcr0
|= PMCR0_PMI_ENABLE_MASK(counter
);
379 pmcr0
&= ~PMCR0_INTGEN_MASK
;
380 pmcr0
|= intgen_type
;
382 SREG_WRITE(SREG_PMCR0
, pmcr0
);
385 saved_PMCR
[cpuid
][0] = pmcr0
;
390 disable_counter(uint32_t counter
)
394 int cpuid
= cpu_number();
400 pmcr0
= SREG_READ(SREG_PMCR0
) | 0x3;
401 enabled
= (pmcr0
& PMCR0_PMC_ENABLE_MASK(counter
)) != 0;
404 pmcr0
&= PMCR0_PMC_DISABLE_MASK(counter
);
405 SREG_WRITE(SREG_PMCR0
, pmcr0
);
408 saved_PMCR
[cpuid
][0] = pmcr0
;
413 * Enable counter in processor modes determined by configuration word.
416 set_modes(uint32_t counter
, kpc_config_t cfgword
)
419 int cpuid
= cpu_number();
421 if (cfgword
& CFGWORD_EL0A32EN_MASK
) {
422 bits
|= PMCR1_EL0_A32_ENABLE_MASK(counter
);
424 if (cfgword
& CFGWORD_EL0A64EN_MASK
) {
425 bits
|= PMCR1_EL0_A64_ENABLE_MASK(counter
);
427 if (cfgword
& CFGWORD_EL1EN_MASK
) {
428 bits
|= PMCR1_EL1_A64_ENABLE_MASK(counter
);
431 if (cfgword
& CFGWORD_EL3EN_MASK
) {
432 bits
|= PMCR1_EL3_A64_ENABLE_MASK(counter
);
437 * Backwards compatibility: Writing a non-zero configuration word with
438 * all zeros in bits 16-19 is interpreted as enabling in all modes.
439 * This matches the behavior when the PMCR1 bits weren't exposed.
441 if (bits
== 0 && cfgword
!= 0) {
442 bits
= PMCR1_EL_ALL_ENABLE_MASK(counter
);
445 uint64_t pmcr1
= SREG_READ(SREG_PMCR1
);
446 pmcr1
&= PMCR1_EL_ALL_DISABLE_MASK(counter
);
448 pmcr1
|= 0x30303; /* monotonic compatibility */
449 SREG_WRITE(SREG_PMCR1
, pmcr1
);
450 saved_PMCR
[cpuid
][1] = pmcr1
;
454 read_counter(uint32_t counter
)
457 // case 0: return SREG_READ(SREG_PMC0);
458 // case 1: return SREG_READ(SREG_PMC1);
459 case 2: return SREG_READ(SREG_PMC2
);
460 case 3: return SREG_READ(SREG_PMC3
);
461 case 4: return SREG_READ(SREG_PMC4
);
462 case 5: return SREG_READ(SREG_PMC5
);
463 case 6: return SREG_READ(SREG_PMC6
);
464 case 7: return SREG_READ(SREG_PMC7
);
465 #if (KPC_ARM64_CONFIGURABLE_COUNT > 6)
466 case 8: return SREG_READ(SREG_PMC8
);
467 case 9: return SREG_READ(SREG_PMC9
);
474 write_counter(uint32_t counter
, uint64_t value
)
477 // case 0: SREG_WRITE(SREG_PMC0, value); break;
478 // case 1: SREG_WRITE(SREG_PMC1, value); break;
479 case 2: SREG_WRITE(SREG_PMC2
, value
); break;
480 case 3: SREG_WRITE(SREG_PMC3
, value
); break;
481 case 4: SREG_WRITE(SREG_PMC4
, value
); break;
482 case 5: SREG_WRITE(SREG_PMC5
, value
); break;
483 case 6: SREG_WRITE(SREG_PMC6
, value
); break;
484 case 7: SREG_WRITE(SREG_PMC7
, value
); break;
485 #if (KPC_ARM64_CONFIGURABLE_COUNT > 6)
486 case 8: SREG_WRITE(SREG_PMC8
, value
); break;
487 case 9: SREG_WRITE(SREG_PMC9
, value
); break;
494 kpc_rawpmu_config_count(void)
496 return RAWPMU_CONFIG_COUNT
;
500 kpc_get_rawpmu_config(kpc_config_t
*configv
)
502 configv
[0] = SREG_READ(SREG_PMCR2
);
503 configv
[1] = SREG_READ(SREG_PMCR3
);
504 configv
[2] = SREG_READ(SREG_PMCR4
);
505 configv
[3] = SREG_READ(SREG_OPMAT0
);
506 configv
[4] = SREG_READ(SREG_OPMAT1
);
507 configv
[5] = SREG_READ(SREG_OPMSK0
);
508 configv
[6] = SREG_READ(SREG_OPMSK1
);
509 #if RAWPMU_CONFIG_COUNT > 7
510 configv
[7] = SREG_READ(SREG_PMMMAP
);
511 configv
[8] = SREG_READ(SREG_PMTRHLD2
);
512 configv
[9] = SREG_READ(SREG_PMTRHLD4
);
513 configv
[10] = SREG_READ(SREG_PMTRHLD6
);
519 kpc_set_rawpmu_config(kpc_config_t
*configv
)
521 SREG_WRITE(SREG_PMCR2
, configv
[0]);
522 SREG_WRITE(SREG_PMCR3
, configv
[1]);
523 SREG_WRITE(SREG_PMCR4
, configv
[2]);
524 SREG_WRITE(SREG_OPMAT0
, configv
[3]);
525 SREG_WRITE(SREG_OPMAT1
, configv
[4]);
526 SREG_WRITE(SREG_OPMSK0
, configv
[5]);
527 SREG_WRITE(SREG_OPMSK1
, configv
[6]);
528 #if RAWPMU_CONFIG_COUNT > 7
529 SREG_WRITE(SREG_PMMMAP
, configv
[7]);
530 SREG_WRITE(SREG_PMTRHLD2
, configv
[8]);
531 SREG_WRITE(SREG_PMTRHLD4
, configv
[9]);
532 SREG_WRITE(SREG_PMTRHLD6
, configv
[10]);
540 int cpuid
= cpu_number();
542 __asm__
volatile("dmb ish");
544 assert(ml_get_interrupts_enabled() == FALSE
);
546 /* Save current PMCR0/1 values. PMCR2-4 are in the RAWPMU set. */
547 saved_PMCR
[cpuid
][0] = SREG_READ(SREG_PMCR0
) | 0x3;
549 /* Save event selections. */
550 saved_PMESR
[cpuid
][0] = SREG_READ(SREG_PMESR0
);
551 saved_PMESR
[cpuid
][1] = SREG_READ(SREG_PMESR1
);
553 kpc_get_rawpmu_config(saved_RAWPMU
[cpuid
]);
555 /* Disable the counters. */
556 // SREG_WRITE(SREG_PMCR0, clear);
558 /* Finally, save state for each counter*/
559 for (int i
= 2; i
< KPC_ARM64_PMC_COUNT
; i
++) {
560 saved_counter
[cpuid
][i
] = read_counter(i
);
567 int cpuid
= cpu_number();
569 /* Restore PMESR values. */
570 SREG_WRITE(SREG_PMESR0
, saved_PMESR
[cpuid
][0]);
571 SREG_WRITE(SREG_PMESR1
, saved_PMESR
[cpuid
][1]);
573 kpc_set_rawpmu_config(saved_RAWPMU
[cpuid
]);
575 /* Restore counter values */
576 for (int i
= 2; i
< KPC_ARM64_PMC_COUNT
; i
++) {
577 write_counter(i
, saved_counter
[cpuid
][i
]);
580 /* Restore PMCR0/1 values (with PMCR0 last to enable). */
581 SREG_WRITE(SREG_PMCR1
, saved_PMCR
[cpuid
][1] | 0x30303);
582 SREG_WRITE(SREG_PMCR0
, saved_PMCR
[cpuid
][0] | 0x3);
586 get_counter_config(uint32_t counter
)
591 case 2: /* FALLTHROUGH */
592 case 3: /* FALLTHROUGH */
593 case 4: /* FALLTHROUGH */
595 pmesr
= PMESR_EVT_DECODE(SREG_READ(SREG_PMESR0
), counter
, 2);
597 case 6: /* FALLTHROUGH */
599 #if (KPC_ARM64_CONFIGURABLE_COUNT > 6)
601 case 8: /* FALLTHROUGH */
604 pmesr
= PMESR_EVT_DECODE(SREG_READ(SREG_PMESR1
), counter
, 6);
611 kpc_config_t config
= pmesr
;
613 uint64_t pmcr1
= SREG_READ(SREG_PMCR1
);
615 if (pmcr1
& PMCR1_EL0_A32_ENABLE_MASK(counter
)) {
616 config
|= CFGWORD_EL0A32EN_MASK
;
618 if (pmcr1
& PMCR1_EL0_A64_ENABLE_MASK(counter
)) {
619 config
|= CFGWORD_EL0A64EN_MASK
;
621 if (pmcr1
& PMCR1_EL1_A64_ENABLE_MASK(counter
)) {
622 config
|= CFGWORD_EL1EN_MASK
;
624 config
|= CFGWORD_EL3EN_MASK
;
628 if (pmcr1
& PMCR1_EL3_A64_ENABLE_MASK(counter
)) {
629 config
|= CFGWORD_EL3EN_MASK
;
637 set_counter_config(uint32_t counter
, uint64_t config
)
639 int cpuid
= cpu_number();
643 case 2: /* FALLTHROUGH */
644 case 3: /* FALLTHROUGH */
645 case 4: /* FALLTHROUGH */
647 pmesr
= SREG_READ(SREG_PMESR0
);
648 pmesr
&= PMESR_EVT_CLEAR(counter
, 2);
649 pmesr
|= PMESR_EVT_ENCODE(config
, counter
, 2);
650 SREG_WRITE(SREG_PMESR0
, pmesr
);
651 saved_PMESR
[cpuid
][0] = pmesr
;
654 case 6: /* FALLTHROUGH */
656 #if KPC_ARM64_CONFIGURABLE_COUNT > 6
658 case 8: /* FALLTHROUGH */
661 pmesr
= SREG_READ(SREG_PMESR1
);
662 pmesr
&= PMESR_EVT_CLEAR(counter
, 6);
663 pmesr
|= PMESR_EVT_ENCODE(config
, counter
, 6);
664 SREG_WRITE(SREG_PMESR1
, pmesr
);
665 saved_PMESR
[cpuid
][1] = pmesr
;
671 set_modes(counter
, config
);
674 /* internal functions */
682 kpc_is_running_fixed(void)
684 return (kpc_running_classes
& KPC_CLASS_FIXED_MASK
) == KPC_CLASS_FIXED_MASK
;
688 kpc_is_running_configurable(uint64_t pmc_mask
)
690 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
691 return ((kpc_running_classes
& KPC_CLASS_CONFIGURABLE_MASK
) == KPC_CLASS_CONFIGURABLE_MASK
) &&
692 ((kpc_running_cfg_pmc_mask
& pmc_mask
) == pmc_mask
);
696 kpc_fixed_count(void)
698 return KPC_ARM64_FIXED_COUNT
;
702 kpc_configurable_count(void)
704 return KPC_ARM64_CONFIGURABLE_COUNT
;
708 kpc_fixed_config_count(void)
714 kpc_configurable_config_count(uint64_t pmc_mask
)
716 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
717 return kpc_popcount(pmc_mask
);
721 kpc_get_fixed_config(kpc_config_t
*configv __unused
)
729 return (1ULL << KPC_ARM64_COUNTER_WIDTH
) - 1;
733 kpc_configurable_max(void)
735 return (1ULL << KPC_ARM64_COUNTER_WIDTH
) - 1;
739 set_running_configurable(uint64_t target_mask
, uint64_t state_mask
)
741 uint32_t cfg_count
= kpc_configurable_count(), offset
= kpc_fixed_count();
744 enabled
= ml_set_interrupts_enabled(FALSE
);
746 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
747 if (((1ULL << i
) & target_mask
) == 0)
749 assert(kpc_controls_counter(offset
+ i
));
751 if ((1ULL << i
) & state_mask
) {
752 enable_counter(offset
+ i
);
754 disable_counter(offset
+ i
);
758 ml_set_interrupts_enabled(enabled
);
761 static uint32_t kpc_xcall_sync
;
763 kpc_set_running_xcall( void *vstate
)
765 struct kpc_running_remote
*mp_config
= (struct kpc_running_remote
*) vstate
;
768 set_running_configurable(mp_config
->cfg_target_mask
,
769 mp_config
->cfg_state_mask
);
771 if (hw_atomic_sub(&kpc_xcall_sync
, 1) == 0)
772 thread_wakeup((event_t
) &kpc_xcall_sync
);
775 static uint32_t kpc_xread_sync
;
777 kpc_get_curcpu_counters_xcall(void *args
)
779 struct kpc_get_counters_remote
*handler
= args
;
781 assert(handler
!= NULL
);
782 assert(handler
->buf
!= NULL
);
784 int offset
= cpu_number() * handler
->buf_stride
;
785 int r
= kpc_get_curcpu_counters(handler
->classes
, NULL
, &handler
->buf
[offset
]);
787 /* number of counters added by this CPU, needs to be atomic */
788 hw_atomic_add(&(handler
->nb_counters
), r
);
790 if (hw_atomic_sub(&kpc_xread_sync
, 1) == 0) {
791 thread_wakeup((event_t
) &kpc_xread_sync
);
796 kpc_get_all_cpus_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
800 int enabled
= ml_set_interrupts_enabled(FALSE
);
802 /* grab counters and CPU number as close as possible */
804 *curcpu
= current_processor()->cpu_id
;
807 struct kpc_get_counters_remote hdl
= {
811 .buf_stride
= kpc_get_counter_count(classes
)
814 cpu_broadcast_xcall(&kpc_xread_sync
, TRUE
, kpc_get_curcpu_counters_xcall
, &hdl
);
815 int offset
= hdl
.nb_counters
;
817 (void)ml_set_interrupts_enabled(enabled
);
823 kpc_get_fixed_counters(uint64_t *counterv
)
826 mt_fixed_counts(counterv
);
828 #else /* MONOTONIC */
829 #pragma unused(counterv)
831 #endif /* !MONOTONIC */
835 kpc_get_configurable_counters(uint64_t *counterv
, uint64_t pmc_mask
)
837 uint32_t cfg_count
= kpc_configurable_count(), offset
= kpc_fixed_count();
842 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
843 if (((1ULL << i
) & pmc_mask
) == 0)
845 ctr
= read_counter(i
+ offset
);
847 if (ctr
& KPC_ARM64_COUNTER_OVF_MASK
) {
848 ctr
= CONFIGURABLE_SHADOW(i
) +
849 (kpc_configurable_max() - CONFIGURABLE_RELOAD(i
) + 1 /* Wrap */) +
850 (ctr
& KPC_ARM64_COUNTER_MASK
);
852 ctr
= CONFIGURABLE_SHADOW(i
) +
853 (ctr
- CONFIGURABLE_RELOAD(i
));
863 kpc_get_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
865 uint32_t cfg_count
= kpc_configurable_count(), offset
= kpc_fixed_count();
869 for (uint32_t i
= 0; i
< cfg_count
; ++i
)
870 if ((1ULL << i
) & pmc_mask
)
871 *configv
++ = get_counter_config(i
+ offset
);
876 kpc_set_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
878 uint32_t cfg_count
= kpc_configurable_count(), offset
= kpc_fixed_count();
883 enabled
= ml_set_interrupts_enabled(FALSE
);
885 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
886 if (((1ULL << i
) & pmc_mask
) == 0)
888 assert(kpc_controls_counter(i
+ offset
));
890 set_counter_config(i
+ offset
, *configv
++);
893 ml_set_interrupts_enabled(enabled
);
898 static uint32_t kpc_config_sync
;
900 kpc_set_config_xcall(void *vmp_config
)
902 struct kpc_config_remote
*mp_config
= vmp_config
;
903 kpc_config_t
*new_config
= NULL
;
904 uint32_t classes
= 0ULL;
907 assert(mp_config
->configv
);
908 classes
= mp_config
->classes
;
909 new_config
= mp_config
->configv
;
911 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
912 kpc_set_configurable_config(new_config
, mp_config
->pmc_mask
);
913 new_config
+= kpc_popcount(mp_config
->pmc_mask
);
916 if (classes
& KPC_CLASS_RAWPMU_MASK
) {
917 kpc_set_rawpmu_config(new_config
);
918 new_config
+= RAWPMU_CONFIG_COUNT
;
921 if (hw_atomic_sub(&kpc_config_sync
, 1) == 0)
922 thread_wakeup((event_t
) &kpc_config_sync
);
926 kpc_reload_counter(uint32_t ctr
)
928 assert(ctr
< (kpc_configurable_count() + kpc_fixed_count()));
930 /* don't reload counters reserved for power management */
931 if (!kpc_controls_counter(ctr
))
934 uint64_t old
= read_counter(ctr
);
935 write_counter(ctr
, FIXED_RELOAD(ctr
));
936 return old
& KPC_ARM64_COUNTER_MASK
;
939 static uint32_t kpc_reload_sync
;
941 kpc_set_reload_xcall(void *vmp_config
)
943 struct kpc_config_remote
*mp_config
= vmp_config
;
944 uint32_t classes
= 0, count
= 0, offset
= kpc_fixed_count();
945 uint64_t *new_period
= NULL
, max
= kpc_configurable_max();
949 assert(mp_config
->configv
);
950 classes
= mp_config
->classes
;
951 new_period
= mp_config
->configv
;
953 enabled
= ml_set_interrupts_enabled(FALSE
);
955 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
957 * Update _all_ shadow counters, this cannot be done for only
958 * selected PMCs. Otherwise, we would corrupt the configurable
959 * shadow buffer since the PMCs are muxed according to the pmc
962 uint64_t all_cfg_mask
= (1ULL << kpc_configurable_count()) - 1;
963 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask
);
965 /* set the new period */
966 count
= kpc_configurable_count();
967 for (uint32_t i
= 0; i
< count
; ++i
) {
968 /* ignore the counter */
969 if (((1ULL << i
) & mp_config
->pmc_mask
) == 0)
971 if (*new_period
== 0)
972 *new_period
= kpc_configurable_max();
973 CONFIGURABLE_RELOAD(i
) = max
- *new_period
;
974 /* reload the counter */
975 kpc_reload_counter(offset
+ i
);
976 /* next period value */
981 ml_set_interrupts_enabled(enabled
);
983 if (hw_atomic_sub(&kpc_reload_sync
, 1) == 0)
984 thread_wakeup((event_t
) &kpc_reload_sync
);
987 void kpc_pmi_handler(cpu_id_t source
);
989 kpc_pmi_handler(cpu_id_t source __unused
)
991 uint64_t PMSR
, extra
;
995 enabled
= ml_set_interrupts_enabled(FALSE
);
997 /* The pmi must be delivered to the CPU that generated it */
998 if (source
!= getCpuDatap()->interrupt_nub
) {
999 panic("pmi from IOCPU %p delivered to IOCPU %p", source
, getCpuDatap()->interrupt_nub
);
1002 /* Get the PMSR which has the overflow bits for all the counters */
1003 __asm__
volatile("mrs %0, S3_1_c15_c13_0" : "=r"(PMSR
));
1005 for (ctr
= 0; ctr
< (KPC_ARM64_FIXED_COUNT
+ KPC_ARM64_CONFIGURABLE_COUNT
); ctr
++) {
1006 if ((1ull << ctr
) & PMSR
) {
1009 mt_cpu_pmi(getCpuDatap(), PMSR
);
1010 #endif /* MONOTONIC */
1012 extra
= kpc_reload_counter(ctr
);
1015 += (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + extra
;
1017 if (FIXED_ACTIONID(ctr
))
1018 kpc_sample_kperf(FIXED_ACTIONID(ctr
));
1023 ml_set_interrupts_enabled(enabled
);
1027 kpc_get_classes(void)
1029 return KPC_CLASS_FIXED_MASK
| KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_RAWPMU_MASK
;
1033 kpc_set_running_arch(struct kpc_running_remote
*mp_config
)
1040 PE_cpu_perfmon_interrupt_install_handler(kpc_pmi_handler
);
1041 int max_cpu
= ml_get_max_cpu_number();
1042 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
1043 cpu_data_t
*target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
1044 if (target_cpu_datap
!= NULL
)
1045 PE_cpu_perfmon_interrupt_enable(target_cpu_datap
->cpu_id
, TRUE
);
1050 /* dispatch to all CPUs */
1051 cpu_broadcast_xcall(&kpc_xcall_sync
, TRUE
, kpc_set_running_xcall
, mp_config
);
1053 kpc_running_cfg_pmc_mask
= mp_config
->cfg_state_mask
;
1054 kpc_running_classes
= mp_config
->classes
;
1061 kpc_set_period_arch(struct kpc_config_remote
*mp_config
)
1065 /* dispatch to all CPUs */
1066 cpu_broadcast_xcall(&kpc_reload_sync
, TRUE
, kpc_set_reload_xcall
, mp_config
);
1074 kpc_set_config_arch(struct kpc_config_remote
*mp_config
)
1076 uint32_t count
= kpc_popcount(mp_config
->pmc_mask
);
1079 assert(mp_config
->configv
);
1081 /* check config against whitelist for external devs */
1082 for (uint32_t i
= 0; i
< count
; ++i
) {
1083 if (!whitelist_disabled
&& !config_in_whitelist(mp_config
->configv
[i
])) {
1088 /* dispatch to all CPUs */
1089 cpu_broadcast_xcall(&kpc_config_sync
, TRUE
, kpc_set_config_xcall
, mp_config
);
1099 if (kpc_configured
) {
1107 if (kpc_configured
) {
1113 kpc_set_sw_inc( uint32_t mask __unused
)
1119 kpc_disable_whitelist( int val
)
1121 whitelist_disabled
= val
;
1126 kpc_get_whitelist_disabled( void )
1128 return whitelist_disabled
;
1132 kpc_get_pmu_version(void)
1134 return KPC_PMU_ARM_APPLE
;