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
32/* Kernel interfaces to KPC PMC infrastructure. */
33
34#include <machine/machine_kpc.h>
35
36/* cross-platform class constants */
37#define KPC_CLASS_FIXED         (0)
38#define KPC_CLASS_CONFIGURABLE  (1)
39#define KPC_CLASS_POWER         (2)
40#define KPC_CLASS_RAWPMU        (3)
41
42#define KPC_CLASS_FIXED_MASK         (1u << KPC_CLASS_FIXED)
43#define KPC_CLASS_CONFIGURABLE_MASK  (1u << KPC_CLASS_CONFIGURABLE)
44#define KPC_CLASS_POWER_MASK         (1u << KPC_CLASS_POWER)
45#define KPC_CLASS_RAWPMU_MASK        (1u << KPC_CLASS_RAWPMU)
46
47#define KPC_ALL_CPUS (1u << 31)
48
49/* bootstrap */
50extern void kpc_init(void);
51
52/* Architecture specific initialisation */
53extern void kpc_arch_init(void);
54
55/* Get the bitmask of available classes */
56extern uint32_t kpc_get_classes(void);
57
58/* Get the bitmask of currently running counter classes  */
59extern uint32_t kpc_get_running(void);
60
61/* Set the bitmask of currently running counter classes. Specify
62 * classes = 0 to stop counters
63 */
64extern int kpc_set_running(uint32_t classes);
65
66/* Read CPU counters */
67extern int kpc_get_cpu_counters(boolean_t all_cpus, uint32_t classes,
68                                int *curcpu, uint64_t *buf);
69
70/* Read shadow counters */
71extern int kpc_get_shadow_counters( boolean_t all_cpus, uint32_t classes,
72                                    int *curcpu, uint64_t *buf );
73
74/* Read current thread's counter accumulations */
75extern int kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf);
76
77/* Given a config, how many counters and config registers there are */
78extern uint32_t kpc_get_counter_count(uint32_t classes);
79extern uint32_t kpc_get_config_count(uint32_t classes);
80
81/* enable/disable thread counting */
82extern uint32_t kpc_get_thread_counting(void);
83extern int      kpc_set_thread_counting(uint32_t classes);
84
85/* get and set config registers */
86extern int kpc_get_config(uint32_t classes, kpc_config_t *current_config);
87extern int kpc_set_config(uint32_t classes, kpc_config_t *new_config);
88
89/* get and set PMI period */
90extern int kpc_get_period(uint32_t classes, uint64_t *period);
91extern int kpc_set_period(uint32_t classes, uint64_t *period);
92
93/* get and set kperf actionid */
94extern int kpc_get_actionid(uint32_t classes, uint32_t *actionid);
95extern int kpc_set_actionid(uint32_t classes, uint32_t *actionid);
96
97/* hooks on thread create and delete */
98extern void kpc_thread_create(thread_t thread);
99extern void kpc_thread_destroy(thread_t thread);
100
101/* allocate a buffer big enough for all counters */
102extern uint64_t *kpc_counterbuf_alloc(void);
103extern void      kpc_counterbuf_free(uint64_t*);
104
105/* whether we're currently accounting into threads */
106extern int kpc_threads_counting;
107
108/* AST callback for KPC */
109extern void kpc_thread_ast_handler( thread_t thread );
110
111/* context switch accounting between two threads */
112extern void kpc_switch_context( thread_t old, thread_t new );
113
114/* acquire/release the counters used by the Power Manager */
115extern int kpc_force_all_ctrs( task_t task, int val );
116extern int kpc_get_force_all_ctrs( void );
117
118/* arch-specific routine for acquire/release the counters used by the Power Manager */
119extern int kpc_force_all_ctrs_arch( task_t task, int val );
120
121extern int kpc_set_sw_inc( uint32_t mask );
122
123/* disable/enable whitelist of allowed events */
124extern int kpc_get_whitelist_disabled( void );
125extern int kpc_disable_whitelist( int val );
126
127/*
128 * Allow the Power Manager to register for KPC notification when the counters
129 * are acquired/released by a task. The argument is equal to true if the Power
130 * Manager can use the counters, otherwise it is equal to false.
131 */
132extern boolean_t kpc_register_pm_handler(void (*handler)(boolean_t));
133
134/*
135 * Is the PMU used by both the power manager and userspace?
136 *
137 * This is true when the power manager has been registered. It disables certain
138 * counter configurations (like RAWPMU) that are incompatible with sharing
139 * counters.
140 */
141extern boolean_t kpc_multiple_clients(void);
142
143/*
144 * Is kpc controlling the fixed counters?
145 *
146 * This returns false when the power manager has requested custom configuration
147 * control.
148 */
149extern boolean_t kpc_controls_fixed_counters(void);
150
151extern void kpc_idle(void);
152extern void kpc_idle_exit(void);
153
154
155/* KPC PRIVATE */
156extern uint32_t kpc_actionid[KPC_MAX_COUNTERS];
157/* mp operations */
158struct kpc_config_remote
159{
160	uint32_t classes;
161	kpc_config_t *configv;
162};
163
164extern int kpc_get_fixed_counters(uint64_t *counterv);
165extern int kpc_get_configurable_counters(uint64_t *counterv);
166extern boolean_t kpc_is_running_fixed(void);
167extern boolean_t kpc_is_running_configurable(void);
168extern uint32_t kpc_fixed_count(void);
169extern uint32_t kpc_configurable_count(void);
170extern uint32_t kpc_fixed_config_count(void);
171extern uint32_t kpc_configurable_config_count(void);
172extern uint32_t kpc_rawpmu_config_count(void);
173extern int kpc_get_fixed_config(kpc_config_t *configv);
174extern int kpc_get_configurable_config(kpc_config_t *configv);
175extern int kpc_get_rawpmu_config(kpc_config_t *configv);
176extern uint64_t kpc_fixed_max(void);
177extern uint64_t kpc_configurable_max(void);
178extern int kpc_set_config_arch(struct kpc_config_remote *mp_config);
179extern int kpc_set_period_arch(struct kpc_config_remote *mp_config);
180extern void kpc_sample_kperf(uint32_t actionid);
181
182/* Interface for kexts to publish a kpc interface */
183struct kpc_driver
184{
185	uint32_t (*get_classes)(void);
186	uint32_t (*get_running)(void);
187	int      (*set_running)(uint32_t classes);
188	int      (*get_cpu_counters)(boolean_t all_cpus, uint32_t classes,
189	                             int *curcpu, uint64_t *buf);
190	int      (*get_curthread_counters)(uint32_t *inoutcount, uint64_t *buf);
191	uint32_t (*get_counter_count)(uint32_t classes);
192	uint32_t (*get_config_count)(uint32_t classes);
193	int      (*get_config)(uint32_t classes, kpc_config_t *current_config);
194	int      (*set_config)(uint32_t classes, kpc_config_t *new_config);
195	int      (*get_period)(uint32_t classes, uint64_t *period);
196	int      (*set_period)(uint32_t classes, uint64_t *period);
197};
198
199#endif /* __KERN_KPC_H__ */
200