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
41#define KPC_CLASS_FIXED_MASK         (1<<KPC_CLASS_FIXED)
42#define KPC_CLASS_CONFIGURABLE_MASK  (1<<KPC_CLASS_CONFIGURABLE)
43
44#define KPC_ALL_CPUS (1 << 31)
45
46/* bootstrap */
47extern void kpc_init(void);
48
49/* Get the bitmask of available classes */
50extern uint32_t kpc_get_classes(void);
51
52/* Get the bitmask of currently running counter classes  */
53extern uint32_t kpc_get_running(void);
54
55/* Set the bitmask of currently running counter classes. Specify
56 * classes = 0 to stop counters
57 */
58extern int kpc_set_running(uint32_t classes);
59
60/* Read CPU counters */
61extern int kpc_get_cpu_counters(boolean_t all_cpus, uint32_t classes,
62                                int *curcpu, uint64_t *buf);
63
64/* Read shadow counters */
65extern int kpc_get_shadow_counters( boolean_t all_cpus, uint32_t classes,
66                                    int *curcpu, uint64_t *buf );
67
68/* Read current thread's counter accumulations */
69extern int kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf);
70
71/* Given a config, how many counters and config registers there are */
72extern uint32_t kpc_get_counter_count(uint32_t classes);
73extern uint32_t kpc_get_config_count(uint32_t classes);
74
75/* enable/disable thread counting */
76extern uint32_t kpc_get_thread_counting(void);
77extern int      kpc_set_thread_counting(uint32_t classes);
78
79/* get and set config registers */
80extern int kpc_get_config(uint32_t classes, kpc_config_t *current_config);
81extern int kpc_set_config(uint32_t classes, kpc_config_t *new_config);
82
83/* get and set PMI period */
84extern int kpc_get_period(uint32_t classes, uint64_t *period);
85extern int kpc_set_period(uint32_t classes, uint64_t *period);
86
87/* get and set kperf actionid */
88extern int kpc_get_actionid(uint32_t classes, uint32_t *actionid);
89extern int kpc_set_actionid(uint32_t classes, uint32_t *actionid);
90
91/* hooks on thread create and delete */
92extern void kpc_thread_create(thread_t thread);
93extern void kpc_thread_destroy(thread_t thread);
94
95/* allocate a buffer big enough for all counters */
96extern uint64_t *kpc_counterbuf_alloc(void);
97extern void      kpc_counterbuf_free(uint64_t*);
98
99/* whether we're currently accounting into threads */
100extern int kpc_threads_counting;
101
102/* AST callback for KPC */
103extern void kpc_thread_ast_handler( thread_t thread );
104
105/* context switch accounting between two threads */
106extern void kpc_switch_context( thread_t old, thread_t new );
107
108/* disable/enable whitelist of allowed events */
109extern int kpc_get_whitelist_disabled( void );
110extern int kpc_disable_whitelist( int val );
111
112/* allow PM to register a handler to disable it's use of perf counters */
113extern boolean_t kpc_register_pm_handler(void (*handler)(boolean_t));
114
115
116extern void kpc_idle(void);
117extern void kpc_idle_exit(void);
118
119
120/* KPC PRIVATE */
121extern uint32_t kpc_actionid[KPC_MAX_COUNTERS];
122/* mp operations */
123struct kpc_config_remote
124{
125	uint32_t classes;
126	kpc_config_t *configv;
127};
128
129extern int kpc_get_fixed_counters(uint64_t *counterv);
130extern int kpc_get_configurable_counters(uint64_t *counterv);
131extern boolean_t kpc_is_running_fixed(void);
132extern boolean_t kpc_is_running_configurable(void);
133extern uint32_t kpc_fixed_count(void);
134extern uint32_t kpc_configurable_count(void);
135extern uint32_t kpc_fixed_config_count(void);
136extern uint32_t kpc_configurable_config_count(void);
137extern int kpc_get_fixed_config(kpc_config_t *configv);
138extern int kpc_get_configurable_config(kpc_config_t *configv);
139extern uint64_t kpc_fixed_max(void);
140extern uint64_t kpc_configurable_max(void);
141extern int kpc_set_config_arch(struct kpc_config_remote *mp_config);
142extern int kpc_set_period_arch(struct kpc_config_remote *mp_config);
143extern void kpc_sample_kperf(uint32_t actionid);
144
145/* Interface for kexts to publish a kpc interface */
146struct kpc_driver
147{
148	uint32_t (*get_classes)(void);
149	uint32_t (*get_running)(void);
150	int      (*set_running)(uint32_t classes);
151	int      (*get_cpu_counters)(boolean_t all_cpus, uint32_t classes,
152	                             int *curcpu, uint64_t *buf);
153	int      (*get_curthread_counters)(uint32_t *inoutcount, uint64_t *buf);
154	uint32_t (*get_counter_count)(uint32_t classes);
155	uint32_t (*get_config_count)(uint32_t classes);
156	int      (*get_config)(uint32_t classes, kpc_config_t *current_config);
157	int      (*set_config)(uint32_t classes, kpc_config_t *new_config);
158	int      (*get_period)(uint32_t classes, uint64_t *period);
159	int      (*set_period)(uint32_t classes, uint64_t *period);
160};
161
162#endif /* __KERN_KPC_H__ */
163