]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/kpc.h
xnu-3789.70.16.tar.gz
[apple/xnu.git] / osfmk / kern / kpc.h
CommitLineData
39236c6e
A
1/*
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
39236c6e
A
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.
39037602 14 *
39236c6e
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
39236c6e
A
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.
39037602 25 *
39236c6e
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
39037602
A
29#ifndef KERN_KPC_H
30#define KERN_KPC_H
39236c6e 31
fe8ab488 32/* Kernel interfaces to KPC PMC infrastructure. */
39236c6e
A
33
34#include <machine/machine_kpc.h>
3e170ce0 35#include <kern/thread.h> /* thread_* */
39236c6e 36
39037602
A
37__BEGIN_DECLS
38
39236c6e
A
39/* cross-platform class constants */
40#define KPC_CLASS_FIXED (0)
41#define KPC_CLASS_CONFIGURABLE (1)
42#define KPC_CLASS_POWER (2)
fe8ab488 43#define KPC_CLASS_RAWPMU (3)
39236c6e 44
fe8ab488
A
45#define KPC_CLASS_FIXED_MASK (1u << KPC_CLASS_FIXED)
46#define KPC_CLASS_CONFIGURABLE_MASK (1u << KPC_CLASS_CONFIGURABLE)
47#define KPC_CLASS_POWER_MASK (1u << KPC_CLASS_POWER)
48#define KPC_CLASS_RAWPMU_MASK (1u << KPC_CLASS_RAWPMU)
39236c6e 49
3e170ce0
A
50#define KPC_PMU_ERROR (0)
51#define KPC_PMU_INTEL_V3 (1)
52#define KPC_PMU_ARM_APPLE (2)
53#define KPC_PMU_INTEL_V2 (3)
54#define KPC_PMU_ARM_V2 (4)
55
fe8ab488 56#define KPC_ALL_CPUS (1u << 31)
39236c6e 57
3e170ce0
A
58/* action id setters/getters */
59#define FIXED_ACTIONID(ctr) (kpc_actionid[(ctr)])
60#define CONFIGURABLE_ACTIONID(ctr) (kpc_actionid[(ctr) + kpc_fixed_count()])
61
62/* reload counter setters/getters */
63#define FIXED_RELOAD(ctr) (current_cpu_datap()->cpu_kpc_reload[(ctr)])
64#define FIXED_RELOAD_CPU(cpu, ctr) (cpu_datap(cpu)->cpu_kpc_reload[(ctr)])
65#define CONFIGURABLE_RELOAD(ctr) (current_cpu_datap()->cpu_kpc_reload[(ctr) + kpc_fixed_count()])
66#define CONFIGURABLE_RELOAD_CPU(cpu, ctr) (cpu_datap(cpu)->cpu_kpc_reload[(ctr) + kpc_fixed_count()])
67
68/* shadow counter setters/getters */
69#define FIXED_SHADOW(ctr) (current_cpu_datap()->cpu_kpc_shadow[(ctr)])
70#define FIXED_SHADOW_CPU(cpu, ctr) (cpu_datap(cpu)->cpu_kpc_shadow[(ctr)])
71#define CONFIGURABLE_SHADOW(ctr) (current_cpu_datap()->cpu_kpc_shadow[(ctr) + kpc_fixed_count()])
72#define CONFIGURABLE_SHADOW_CPU(cpu, ctr) (cpu_datap(cpu)->cpu_kpc_shadow[(ctr) + kpc_fixed_count()])
73
74/**
75 * Callback for notification when PMCs are acquired/released by a task. The
76 * argument is equal to TRUE if the Power Manager (PM) can use its reserved PMCs.
77 * Otherwise, the argument is equal to FALSE.
78 */
79typedef void (*kpc_pm_handler_t)(boolean_t);
80
81/*
82 * Register a CPU to kpc and allocate its buffers.
83 *
84 * @param cpu_data
85 * CPU data associated to the CPU being registered.
86 *
87 * @return
88 * TRUE if buffers are correctly allocated, FALSE otherwise.
89 */
90struct cpu_data;
91extern boolean_t kpc_register_cpu(struct cpu_data *cpu_data);
39037602 92extern void kpc_unregister_cpu(struct cpu_data *cpu_data);
3e170ce0 93
39236c6e
A
94/* bootstrap */
95extern void kpc_init(void);
96
3e170ce0
A
97/* common initialization */
98extern void kpc_common_init(void);
99
fe8ab488
A
100/* Architecture specific initialisation */
101extern void kpc_arch_init(void);
102
3e170ce0
A
103/* Thread counting initialization */
104extern void kpc_thread_init(void);
105
39236c6e
A
106/* Get the bitmask of available classes */
107extern uint32_t kpc_get_classes(void);
108
109/* Get the bitmask of currently running counter classes */
110extern uint32_t kpc_get_running(void);
111
3e170ce0
A
112/* Get the version of KPC that's being run */
113extern int kpc_get_pmu_version(void);
114
39236c6e
A
115/* Set the bitmask of currently running counter classes. Specify
116 * classes = 0 to stop counters
117 */
118extern int kpc_set_running(uint32_t classes);
119
120/* Read CPU counters */
121extern int kpc_get_cpu_counters(boolean_t all_cpus, uint32_t classes,
122 int *curcpu, uint64_t *buf);
123
124/* Read shadow counters */
125extern int kpc_get_shadow_counters( boolean_t all_cpus, uint32_t classes,
126 int *curcpu, uint64_t *buf );
127
128/* Read current thread's counter accumulations */
129extern int kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf);
130
131/* Given a config, how many counters and config registers there are */
132extern uint32_t kpc_get_counter_count(uint32_t classes);
133extern uint32_t kpc_get_config_count(uint32_t classes);
134
135/* enable/disable thread counting */
136extern uint32_t kpc_get_thread_counting(void);
137extern int kpc_set_thread_counting(uint32_t classes);
138
139/* get and set config registers */
140extern int kpc_get_config(uint32_t classes, kpc_config_t *current_config);
141extern int kpc_set_config(uint32_t classes, kpc_config_t *new_config);
142
143/* get and set PMI period */
144extern int kpc_get_period(uint32_t classes, uint64_t *period);
145extern int kpc_set_period(uint32_t classes, uint64_t *period);
146
147/* get and set kperf actionid */
148extern int kpc_get_actionid(uint32_t classes, uint32_t *actionid);
149extern int kpc_set_actionid(uint32_t classes, uint32_t *actionid);
150
151/* hooks on thread create and delete */
152extern void kpc_thread_create(thread_t thread);
153extern void kpc_thread_destroy(thread_t thread);
154
155/* allocate a buffer big enough for all counters */
156extern uint64_t *kpc_counterbuf_alloc(void);
157extern void kpc_counterbuf_free(uint64_t*);
158
159/* whether we're currently accounting into threads */
160extern int kpc_threads_counting;
161
162/* AST callback for KPC */
163extern void kpc_thread_ast_handler( thread_t thread );
3e170ce0 164
39037602
A
165#ifdef MACH_KERNEL_PRIVATE
166
167/* context switch callback for KPC */
168
169extern boolean_t kpc_off_cpu_active;
170
171extern void kpc_off_cpu_internal(thread_t thread);
172extern void kpc_off_cpu_update(void);
173
174static inline void
175kpc_off_cpu(thread_t thread)
176{
177 if (__improbable(kpc_off_cpu_active)) {
178 kpc_off_cpu_internal(thread);
179 }
180}
181
182#endif /* defined(MACH_KERNEL_PRIVATE) */
39236c6e 183
fe8ab488
A
184/* acquire/release the counters used by the Power Manager */
185extern int kpc_force_all_ctrs( task_t task, int val );
186extern int kpc_get_force_all_ctrs( void );
187
188/* arch-specific routine for acquire/release the counters used by the Power Manager */
189extern int kpc_force_all_ctrs_arch( task_t task, int val );
190
191extern int kpc_set_sw_inc( uint32_t mask );
192
39236c6e
A
193/* disable/enable whitelist of allowed events */
194extern int kpc_get_whitelist_disabled( void );
195extern int kpc_disable_whitelist( int val );
196
fe8ab488 197/*
3e170ce0
A
198 * Register the Power Manager as a PMCs user.
199 *
200 * This is a deprecated function used by old Power Managers, new Power Managers
201 * should use the @em kpc_reserve_pm_counters() function. This function actually
202 * calls @em kpc_reserve_pm_counters() with the following arguments:
203 * - handler = handler
204 * - pmc_mask = 0x83
205 * - custom_config = TRUE
206 *
207 * See @em kpc_reserve_pm_counters() for more details about the return value.
fe8ab488 208 */
39236c6e
A
209extern boolean_t kpc_register_pm_handler(void (*handler)(boolean_t));
210
3e170ce0
A
211/*
212 * Register the Power Manager as a PMCs user.
213 *
214 * @param handler
215 * Notification callback to use when PMCs are acquired/released by a task.
216 *
217 * @param pmc_mask
218 * Bitmask of the configurable PMCs used by the Power Manager. The number of bits
219 * set must less or equal than the number of configurable counters
220 * available on the SoC.
221 *
222 * @param custom_config
223 * If custom_config=TRUE, the legacy sharing mode is enabled, otherwise the
224 * Modern Sharing mode is enabled. These modes are explained in more details in
225 * the kperf documentation.
226 *
227 * @return
228 * FALSE if a task has acquired all the PMCs, otherwise TRUE and the Power
229 * Manager can start using the reserved PMCs.
230 */
231extern boolean_t kpc_reserve_pm_counters(uint64_t pmc_mask, kpc_pm_handler_t handler,
232 boolean_t custom_config);
233
234/*
235 * Unregister the Power Manager as a PMCs user, and release the previously
236 * reserved counters.
237 */
238extern void kpc_release_pm_counters(void);
239
fe8ab488
A
240/*
241 * Is the PMU used by both the power manager and userspace?
242 *
243 * This is true when the power manager has been registered. It disables certain
244 * counter configurations (like RAWPMU) that are incompatible with sharing
245 * counters.
246 */
247extern boolean_t kpc_multiple_clients(void);
248
249/*
250 * Is kpc controlling the fixed counters?
251 *
252 * This returns false when the power manager has requested custom configuration
253 * control.
254 */
255extern boolean_t kpc_controls_fixed_counters(void);
39236c6e 256
3e170ce0
A
257/*
258 * Is kpc controlling a specific PMC ?
259 */
260extern boolean_t kpc_controls_counter(uint32_t ctr);
261
262
39236c6e
A
263extern void kpc_idle(void);
264extern void kpc_idle_exit(void);
265
266
3e170ce0
A
267/*
268 * KPC PRIVATE
269 */
270
39236c6e 271extern uint32_t kpc_actionid[KPC_MAX_COUNTERS];
3e170ce0
A
272
273/* handler for mp operations */
274struct kpc_config_remote {
39236c6e
A
275 uint32_t classes;
276 kpc_config_t *configv;
3e170ce0
A
277 uint64_t pmc_mask;
278};
279
280/* handler for mp operations */
281struct kpc_running_remote {
282 uint32_t classes; /* classes to run */
283 uint64_t cfg_target_mask; /* configurable counters selected */
284 uint64_t cfg_state_mask; /* configurable counters new state */
285};
286
287/* handler for mp operations */
288struct kpc_get_counters_remote {
289 uint32_t classes;
290 uint32_t nb_counters;
291 uint32_t buf_stride;
292 uint64_t *buf;
39236c6e
A
293};
294
3e170ce0
A
295extern int kpc_get_all_cpus_counters(uint32_t classes, int *curcpu, uint64_t *buf);
296extern int kpc_get_curcpu_counters(uint32_t classes, int *curcpu, uint64_t *buf);
39236c6e 297extern int kpc_get_fixed_counters(uint64_t *counterv);
3e170ce0 298extern int kpc_get_configurable_counters(uint64_t *counterv, uint64_t pmc_mask);
39236c6e 299extern boolean_t kpc_is_running_fixed(void);
3e170ce0 300extern boolean_t kpc_is_running_configurable(uint64_t pmc_mask);
39236c6e
A
301extern uint32_t kpc_fixed_count(void);
302extern uint32_t kpc_configurable_count(void);
303extern uint32_t kpc_fixed_config_count(void);
3e170ce0 304extern uint32_t kpc_configurable_config_count(uint64_t pmc_mask);
fe8ab488 305extern uint32_t kpc_rawpmu_config_count(void);
39236c6e 306extern int kpc_get_fixed_config(kpc_config_t *configv);
3e170ce0 307extern int kpc_get_configurable_config(kpc_config_t *configv, uint64_t pmc_mask);
fe8ab488 308extern int kpc_get_rawpmu_config(kpc_config_t *configv);
39236c6e
A
309extern uint64_t kpc_fixed_max(void);
310extern uint64_t kpc_configurable_max(void);
311extern int kpc_set_config_arch(struct kpc_config_remote *mp_config);
312extern int kpc_set_period_arch(struct kpc_config_remote *mp_config);
313extern void kpc_sample_kperf(uint32_t actionid);
3e170ce0
A
314extern int kpc_set_running_arch(struct kpc_running_remote *mp_config);
315
316
317/*
318 * Helpers
319 */
320
321/* count the number of bits set */
322extern uint8_t kpc_popcount(uint64_t value);
323
324/* for a set of classes, retrieve the configurable PMCs mask */
325extern uint64_t kpc_get_configurable_pmc_mask(uint32_t classes);
326
39236c6e
A
327
328/* Interface for kexts to publish a kpc interface */
329struct kpc_driver
330{
331 uint32_t (*get_classes)(void);
332 uint32_t (*get_running)(void);
333 int (*set_running)(uint32_t classes);
334 int (*get_cpu_counters)(boolean_t all_cpus, uint32_t classes,
335 int *curcpu, uint64_t *buf);
336 int (*get_curthread_counters)(uint32_t *inoutcount, uint64_t *buf);
337 uint32_t (*get_counter_count)(uint32_t classes);
338 uint32_t (*get_config_count)(uint32_t classes);
339 int (*get_config)(uint32_t classes, kpc_config_t *current_config);
340 int (*set_config)(uint32_t classes, kpc_config_t *new_config);
341 int (*get_period)(uint32_t classes, uint64_t *period);
342 int (*set_period)(uint32_t classes, uint64_t *period);
343};
344
39037602
A
345__END_DECLS
346
347#endif /* !definde(KERN_KPC_H) */