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