2 * Copyright (c) 2012 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 <mach/mach_types.h>
30 #include <machine/machine_routines.h>
31 #include <kern/processor.h>
32 #include <kern/kalloc.h>
33 #include <sys/errno.h>
35 #include <kperf/buffer.h>
36 #include <kern/thread.h>
37 #if defined(__arm64__) || defined(__arm__)
38 #include <arm/cpu_data_internal.h>
43 #include <kperf/kperf.h>
44 #include <kperf/sample.h>
45 #include <kperf/context.h>
46 #include <kperf/action.h>
48 #include <chud/chud_xnu.h>
50 uint32_t kpc_actionid
[KPC_MAX_COUNTERS
];
52 #define COUNTERBUF_SIZE_PER_CPU (KPC_MAX_COUNTERS * sizeof(uint64_t))
53 #define COUNTERBUF_SIZE (machine_info.logical_cpu_max * \
54 COUNTERBUF_SIZE_PER_CPU)
57 static lck_grp_attr_t
*kpc_config_lckgrp_attr
= NULL
;
58 static lck_grp_t
*kpc_config_lckgrp
= NULL
;
59 static lck_mtx_t kpc_config_lock
;
61 /* state specifying if all counters have been requested by kperf */
62 static boolean_t force_all_ctrs
= FALSE
;
65 static kpc_pm_handler_t kpc_pm_handler
;
66 static boolean_t kpc_pm_has_custom_config
;
67 static uint64_t kpc_pm_pmc_mask
;
69 static bool kpc_calling_pm
= false;
70 #endif /* MACH_ASSERT */
72 boolean_t kpc_context_switch_active
= FALSE
;
74 void kpc_common_init(void);
78 kpc_config_lckgrp_attr
= lck_grp_attr_alloc_init();
79 kpc_config_lckgrp
= lck_grp_alloc_init("kpc", kpc_config_lckgrp_attr
);
80 lck_mtx_init(&kpc_config_lock
, kpc_config_lckgrp
, LCK_ATTR_NULL
);
84 kpc_register_cpu(struct cpu_data
*cpu_data
)
87 assert(cpu_data
->cpu_kpc_buf
[0] == NULL
);
88 assert(cpu_data
->cpu_kpc_buf
[1] == NULL
);
89 assert(cpu_data
->cpu_kpc_shadow
== NULL
);
90 assert(cpu_data
->cpu_kpc_reload
== NULL
);
93 * Buffers allocated through kpc_counterbuf_alloc() are large enough to
94 * store all PMCs values from all CPUs. This mimics the userspace API.
95 * This does not suit well with the per-CPU kpc buffers, since:
96 * 1. Buffers don't need to be this large.
97 * 2. The actual number of CPUs is not known at this point.
99 * CPUs are asked to callout into kpc when being registered, we'll
100 * allocate the memory here.
103 if ((cpu_data
->cpu_kpc_buf
[0] = kalloc(COUNTERBUF_SIZE_PER_CPU
)) == NULL
)
105 if ((cpu_data
->cpu_kpc_buf
[1] = kalloc(COUNTERBUF_SIZE_PER_CPU
)) == NULL
)
107 if ((cpu_data
->cpu_kpc_shadow
= kalloc(COUNTERBUF_SIZE_PER_CPU
)) == NULL
)
109 if ((cpu_data
->cpu_kpc_reload
= kalloc(COUNTERBUF_SIZE_PER_CPU
)) == NULL
)
112 memset(cpu_data
->cpu_kpc_buf
[0], 0, COUNTERBUF_SIZE_PER_CPU
);
113 memset(cpu_data
->cpu_kpc_buf
[1], 0, COUNTERBUF_SIZE_PER_CPU
);
114 memset(cpu_data
->cpu_kpc_shadow
, 0, COUNTERBUF_SIZE_PER_CPU
);
115 memset(cpu_data
->cpu_kpc_reload
, 0, COUNTERBUF_SIZE_PER_CPU
);
121 kpc_unregister_cpu(cpu_data
);
126 kpc_unregister_cpu(struct cpu_data
*cpu_data
)
129 if (cpu_data
->cpu_kpc_buf
[0] != NULL
) {
130 kfree(cpu_data
->cpu_kpc_buf
[0], COUNTERBUF_SIZE_PER_CPU
);
131 cpu_data
->cpu_kpc_buf
[0] = NULL
;
133 if (cpu_data
->cpu_kpc_buf
[1] != NULL
) {
134 kfree(cpu_data
->cpu_kpc_buf
[1], COUNTERBUF_SIZE_PER_CPU
);
135 cpu_data
->cpu_kpc_buf
[1] = NULL
;
137 if (cpu_data
->cpu_kpc_shadow
!= NULL
) {
138 kfree(cpu_data
->cpu_kpc_shadow
, COUNTERBUF_SIZE_PER_CPU
);
139 cpu_data
->cpu_kpc_shadow
= NULL
;
141 if (cpu_data
->cpu_kpc_reload
!= NULL
) {
142 kfree(cpu_data
->cpu_kpc_reload
, COUNTERBUF_SIZE_PER_CPU
);
143 cpu_data
->cpu_kpc_reload
= NULL
;
149 kpc_task_set_forced_all_ctrs(task_t task
, boolean_t state
)
155 task
->t_chud
|= TASK_KPC_FORCED_ALL_CTRS
;
157 task
->t_chud
&= ~TASK_KPC_FORCED_ALL_CTRS
;
162 kpc_task_get_forced_all_ctrs(task_t task
)
165 return task
->t_chud
& TASK_KPC_FORCED_ALL_CTRS
? TRUE
: FALSE
;
169 kpc_force_all_ctrs(task_t task
, int val
)
171 boolean_t new_state
= val
? TRUE
: FALSE
;
172 boolean_t old_state
= kpc_get_force_all_ctrs();
175 * Refuse to do the operation if the counters are already forced by
178 if (kpc_get_force_all_ctrs() && !kpc_task_get_forced_all_ctrs(task
))
181 /* nothing to do if the state is not changing */
182 if (old_state
== new_state
)
185 /* notify the power manager */
186 if (kpc_pm_handler
) {
188 kpc_calling_pm
= true;
189 #endif /* MACH_ASSERT */
190 kpc_pm_handler( new_state
? FALSE
: TRUE
);
192 kpc_calling_pm
= false;
193 #endif /* MACH_ASSERT */
197 * This is a force -- ensure that counters are forced, even if power
198 * management fails to acknowledge it.
200 if (force_all_ctrs
!= new_state
) {
201 force_all_ctrs
= new_state
;
204 /* update the task bits */
205 kpc_task_set_forced_all_ctrs(task
, new_state
);
211 kpc_pm_acknowledge(boolean_t available_to_pm
)
214 * Force-all-counters should still be true when the counters are being
215 * made available to power management and false when counters are going
218 assert(force_all_ctrs
== available_to_pm
);
220 * Make sure power management isn't playing games with us.
222 assert(kpc_calling_pm
== true);
225 * Counters being available means no one is forcing all counters.
227 force_all_ctrs
= available_to_pm
? FALSE
: TRUE
;
231 kpc_get_force_all_ctrs(void)
233 return force_all_ctrs
;
237 kpc_multiple_clients(void)
239 return kpc_pm_handler
!= NULL
;
243 kpc_controls_fixed_counters(void)
245 return !kpc_pm_handler
|| force_all_ctrs
|| !kpc_pm_has_custom_config
;
249 kpc_controls_counter(uint32_t ctr
)
251 uint64_t pmc_mask
= 0ULL;
253 assert(ctr
< (kpc_fixed_count() + kpc_configurable_count()));
255 if (ctr
< kpc_fixed_count())
256 return kpc_controls_fixed_counters();
259 * By default kpc manages all PMCs, but if the Power Manager registered
260 * with custom_config=TRUE, the Power Manager manages its reserved PMCs.
261 * However, kpc takes ownership back if a task acquired all PMCs via
264 pmc_mask
= (1ULL << (ctr
- kpc_fixed_count()));
265 if ((pmc_mask
& kpc_pm_pmc_mask
) && kpc_pm_has_custom_config
&& !force_all_ctrs
)
272 kpc_get_running(void)
274 uint64_t pmc_mask
= 0;
275 uint32_t cur_state
= 0;
277 if (kpc_is_running_fixed())
278 cur_state
|= KPC_CLASS_FIXED_MASK
;
280 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
281 if (kpc_is_running_configurable(pmc_mask
))
282 cur_state
|= KPC_CLASS_CONFIGURABLE_MASK
;
284 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
285 if ((pmc_mask
!= 0) && kpc_is_running_configurable(pmc_mask
))
286 cur_state
|= KPC_CLASS_POWER_MASK
;
291 /* may be called from an IPI */
293 kpc_get_curcpu_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
295 int enabled
=0, offset
=0;
296 uint64_t pmc_mask
= 0ULL;
300 enabled
= ml_set_interrupts_enabled(FALSE
);
302 /* grab counters and CPU number as close as possible */
304 *curcpu
= current_processor()->cpu_id
;
306 if (classes
& KPC_CLASS_FIXED_MASK
) {
307 kpc_get_fixed_counters(&buf
[offset
]);
308 offset
+= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
311 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
312 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
313 kpc_get_configurable_counters(&buf
[offset
], pmc_mask
);
314 offset
+= kpc_popcount(pmc_mask
);
317 if (classes
& KPC_CLASS_POWER_MASK
) {
318 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
319 kpc_get_configurable_counters(&buf
[offset
], pmc_mask
);
320 offset
+= kpc_popcount(pmc_mask
);
323 ml_set_interrupts_enabled(enabled
);
328 /* generic counter reading function, public api */
330 kpc_get_cpu_counters(boolean_t all_cpus
, uint32_t classes
,
331 int *curcpu
, uint64_t *buf
)
336 * Unlike reading the current CPU counters, reading counters from all
337 * CPUs is architecture dependent. This allows kpc to make the most of
338 * the platform if memory mapped registers is supported.
341 return kpc_get_all_cpus_counters(classes
, curcpu
, buf
);
343 return kpc_get_curcpu_counters(classes
, curcpu
, buf
);
347 kpc_get_shadow_counters(boolean_t all_cpus
, uint32_t classes
,
348 int *curcpu
, uint64_t *buf
)
350 int curcpu_id
= current_processor()->cpu_id
;
351 uint32_t cfg_count
= kpc_configurable_count(), offset
= 0;
352 uint64_t pmc_mask
= 0ULL;
357 enabled
= ml_set_interrupts_enabled(FALSE
);
359 curcpu_id
= current_processor()->cpu_id
;
363 for (int cpu
= 0; cpu
< machine_info
.logical_cpu_max
; ++cpu
) {
364 /* filter if the caller did not request all cpus */
365 if (!all_cpus
&& (cpu
!= curcpu_id
))
368 if (classes
& KPC_CLASS_FIXED_MASK
) {
369 uint32_t count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
370 memcpy(&buf
[offset
], &FIXED_SHADOW_CPU(cpu
, 0), count
* sizeof(uint64_t));
374 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
375 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
377 for (uint32_t cfg_ctr
= 0; cfg_ctr
< cfg_count
; ++cfg_ctr
)
378 if ((1ULL << cfg_ctr
) & pmc_mask
)
379 buf
[offset
++] = CONFIGURABLE_SHADOW_CPU(cpu
, cfg_ctr
);
382 if (classes
& KPC_CLASS_POWER_MASK
) {
383 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
385 for (uint32_t cfg_ctr
= 0; cfg_ctr
< cfg_count
; ++cfg_ctr
)
386 if ((1ULL << cfg_ctr
) & pmc_mask
)
387 buf
[offset
++] = CONFIGURABLE_SHADOW_CPU(cpu
, cfg_ctr
);
391 ml_set_interrupts_enabled(enabled
);
397 kpc_get_counter_count(uint32_t classes
)
401 if (classes
& KPC_CLASS_FIXED_MASK
)
402 count
+= kpc_fixed_count();
404 if (classes
& (KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
)) {
405 uint64_t pmc_msk
= kpc_get_configurable_pmc_mask(classes
);
406 uint32_t pmc_cnt
= kpc_popcount(pmc_msk
);
414 kpc_get_config_count(uint32_t classes
)
418 if (classes
& KPC_CLASS_FIXED_MASK
)
419 count
+= kpc_fixed_config_count();
421 if (classes
& (KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
)) {
422 uint64_t pmc_mask
= kpc_get_configurable_pmc_mask(classes
);
423 count
+= kpc_configurable_config_count(pmc_mask
);
426 if ((classes
& KPC_CLASS_RAWPMU_MASK
) && !kpc_multiple_clients())
427 count
+= kpc_rawpmu_config_count();
433 kpc_get_config(uint32_t classes
, kpc_config_t
*current_config
)
437 assert(current_config
);
439 if (classes
& KPC_CLASS_FIXED_MASK
) {
440 kpc_get_fixed_config(¤t_config
[count
]);
441 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
444 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
445 uint64_t pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
446 kpc_get_configurable_config(¤t_config
[count
], pmc_mask
);
447 count
+= kpc_get_config_count(KPC_CLASS_CONFIGURABLE_MASK
);
450 if (classes
& KPC_CLASS_POWER_MASK
) {
451 uint64_t pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
452 kpc_get_configurable_config(¤t_config
[count
], pmc_mask
);
453 count
+= kpc_get_config_count(KPC_CLASS_POWER_MASK
);
456 if (classes
& KPC_CLASS_RAWPMU_MASK
)
458 // Client shouldn't ask for config words that aren't available.
459 // Most likely, they'd misinterpret the returned buffer if we
461 if( kpc_multiple_clients() )
465 kpc_get_rawpmu_config(¤t_config
[count
]);
466 count
+= kpc_get_config_count(KPC_CLASS_RAWPMU_MASK
);
473 kpc_set_config(uint32_t classes
, kpc_config_t
*configv
)
476 struct kpc_config_remote mp_config
= {
477 .classes
= classes
, .configv
= configv
,
478 .pmc_mask
= kpc_get_configurable_pmc_mask(classes
)
483 /* don't allow RAWPMU configuration when sharing counters */
484 if ((classes
& KPC_CLASS_RAWPMU_MASK
) && kpc_multiple_clients()) {
488 /* no clients have the right to modify both classes */
489 if ((classes
& (KPC_CLASS_CONFIGURABLE_MASK
)) &&
490 (classes
& (KPC_CLASS_POWER_MASK
)))
495 lck_mtx_lock(&kpc_config_lock
);
497 /* translate the power class for the machine layer */
498 if (classes
& KPC_CLASS_POWER_MASK
)
499 mp_config
.classes
|= KPC_CLASS_CONFIGURABLE_MASK
;
501 ret
= kpc_set_config_arch( &mp_config
);
503 lck_mtx_unlock(&kpc_config_lock
);
508 /* allocate a buffer large enough for all possible counters */
510 kpc_counterbuf_alloc(void)
512 uint64_t *buf
= NULL
;
514 buf
= kalloc(COUNTERBUF_SIZE
);
516 bzero(buf
, COUNTERBUF_SIZE
);
523 kpc_counterbuf_free(uint64_t *buf
)
526 kfree(buf
, COUNTERBUF_SIZE
);
531 kpc_sample_kperf(uint32_t actionid
)
533 struct kperf_sample sbuf
;
534 struct kperf_context ctx
;
536 BUF_DATA(PERF_KPC_HNDLR
| DBG_FUNC_START
);
539 ctx
.cur_thread
= current_thread();
540 ctx
.cur_pid
= task_pid(current_task());
542 ctx
.trigger_type
= TRIGGER_TYPE_PMI
;
545 int r
= kperf_sample(&sbuf
, &ctx
, actionid
, SAMPLE_FLAG_PEND_USER
);
547 BUF_INFO(PERF_KPC_HNDLR
| DBG_FUNC_END
, r
);
552 kpc_set_period(uint32_t classes
, uint64_t *val
)
554 struct kpc_config_remote mp_config
= {
555 .classes
= classes
, .configv
= val
,
556 .pmc_mask
= kpc_get_configurable_pmc_mask(classes
)
561 /* no clients have the right to modify both classes */
562 if ((classes
& (KPC_CLASS_CONFIGURABLE_MASK
)) &&
563 (classes
& (KPC_CLASS_POWER_MASK
)))
568 lck_mtx_lock(&kpc_config_lock
);
570 #ifdef FIXED_COUNTER_SHADOW
571 if ((classes
& KPC_CLASS_FIXED_MASK
) && !kpc_controls_fixed_counters()) {
572 lck_mtx_unlock(&kpc_config_lock
);
576 if (classes
& KPC_CLASS_FIXED_MASK
) {
577 lck_mtx_unlock(&kpc_config_lock
);
582 /* translate the power class for the machine layer */
583 if (classes
& KPC_CLASS_POWER_MASK
)
584 mp_config
.classes
|= KPC_CLASS_CONFIGURABLE_MASK
;
586 kprintf("setting period %u\n", classes
);
587 kpc_set_period_arch( &mp_config
);
589 lck_mtx_unlock(&kpc_config_lock
);
595 kpc_get_period(uint32_t classes
, uint64_t *val
)
598 uint64_t pmc_mask
= 0ULL;
602 lck_mtx_lock(&kpc_config_lock
);
604 if (classes
& KPC_CLASS_FIXED_MASK
) {
605 /* convert reload values to periods */
606 count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
607 for (uint32_t i
= 0; i
< count
; ++i
)
608 *val
++ = kpc_fixed_max() - FIXED_RELOAD(i
);
611 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
612 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
614 /* convert reload values to periods */
615 count
= kpc_configurable_count();
616 for (uint32_t i
= 0; i
< count
; ++i
)
617 if ((1ULL << i
) & pmc_mask
)
618 *val
++ = kpc_configurable_max() - CONFIGURABLE_RELOAD(i
);
621 if (classes
& KPC_CLASS_POWER_MASK
) {
622 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
624 /* convert reload values to periods */
625 count
= kpc_configurable_count();
626 for (uint32_t i
= 0; i
< count
; ++i
)
627 if ((1ULL << i
) & pmc_mask
)
628 *val
++ = kpc_configurable_max() - CONFIGURABLE_RELOAD(i
);
631 lck_mtx_unlock(&kpc_config_lock
);
637 kpc_set_actionid(uint32_t classes
, uint32_t *val
)
640 uint64_t pmc_mask
= 0ULL;
644 /* NOTE: what happens if a pmi occurs while actionids are being
645 * set is undefined. */
646 lck_mtx_lock(&kpc_config_lock
);
648 if (classes
& KPC_CLASS_FIXED_MASK
) {
649 count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
650 memcpy(&FIXED_ACTIONID(0), val
, count
*sizeof(uint32_t));
654 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
655 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
657 count
= kpc_configurable_count();
658 for (uint32_t i
= 0; i
< count
; ++i
)
659 if ((1ULL << i
) & pmc_mask
)
660 CONFIGURABLE_ACTIONID(i
) = *val
++;
663 if (classes
& KPC_CLASS_POWER_MASK
) {
664 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
666 count
= kpc_configurable_count();
667 for (uint32_t i
= 0; i
< count
; ++i
)
668 if ((1ULL << i
) & pmc_mask
)
669 CONFIGURABLE_ACTIONID(i
) = *val
++;
672 lck_mtx_unlock(&kpc_config_lock
);
677 int kpc_get_actionid(uint32_t classes
, uint32_t *val
)
680 uint64_t pmc_mask
= 0ULL;
684 lck_mtx_lock(&kpc_config_lock
);
686 if (classes
& KPC_CLASS_FIXED_MASK
) {
687 count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
688 memcpy(val
, &FIXED_ACTIONID(0), count
*sizeof(uint32_t));
692 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
693 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
695 count
= kpc_configurable_count();
696 for (uint32_t i
= 0; i
< count
; ++i
)
697 if ((1ULL << i
) & pmc_mask
)
698 *val
++ = CONFIGURABLE_ACTIONID(i
);
701 if (classes
& KPC_CLASS_POWER_MASK
) {
702 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
704 count
= kpc_configurable_count();
705 for (uint32_t i
= 0; i
< count
; ++i
)
706 if ((1ULL << i
) & pmc_mask
)
707 *val
++ = CONFIGURABLE_ACTIONID(i
);
710 lck_mtx_unlock(&kpc_config_lock
);
717 kpc_set_running(uint32_t classes
)
719 uint32_t all_cfg_classes
= KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
;
720 struct kpc_running_remote mp_config
= {
721 .classes
= classes
, .cfg_target_mask
= 0ULL, .cfg_state_mask
= 0ULL
724 /* target all available PMCs */
725 mp_config
.cfg_target_mask
= kpc_get_configurable_pmc_mask(all_cfg_classes
);
727 /* translate the power class for the machine layer */
728 if (classes
& KPC_CLASS_POWER_MASK
)
729 mp_config
.classes
|= KPC_CLASS_CONFIGURABLE_MASK
;
731 /* generate the state of each configurable PMCs */
732 mp_config
.cfg_state_mask
= kpc_get_configurable_pmc_mask(classes
);
734 return kpc_set_running_arch(&mp_config
);
738 kpc_register_pm_handler(kpc_pm_handler_t handler
)
740 return kpc_reserve_pm_counters(0x38, handler
, TRUE
);
744 kpc_reserve_pm_counters(uint64_t pmc_mask
, kpc_pm_handler_t handler
,
745 boolean_t custom_config
)
747 uint64_t all_mask
= (1ULL << kpc_configurable_count()) - 1;
748 uint64_t req_mask
= 0ULL;
751 assert(handler
!= NULL
);
752 assert(kpc_pm_handler
== NULL
);
754 /* check number of counters requested */
755 req_mask
= (pmc_mask
& all_mask
);
756 assert(kpc_popcount(req_mask
) <= kpc_configurable_count());
758 /* save the power manager states */
759 kpc_pm_has_custom_config
= custom_config
;
760 kpc_pm_pmc_mask
= req_mask
;
761 kpc_pm_handler
= handler
;
763 printf("kpc: pm registered pmc_mask=%llx custom_config=%d\n",
764 req_mask
, custom_config
);
768 uint32_t cfg_count
= kpc_get_counter_count(KPC_CLASS_CONFIGURABLE_MASK
);
769 uint32_t pwr_count
= kpc_popcount(kpc_pm_pmc_mask
);
770 #pragma unused(cfg_count, pwr_count)
771 assert((cfg_count
+ pwr_count
) == kpc_configurable_count());
774 return force_all_ctrs
? FALSE
: TRUE
;
778 kpc_release_pm_counters(void)
781 assert(kpc_pm_handler
!= NULL
);
783 /* release the counters */
784 kpc_pm_has_custom_config
= FALSE
;
785 kpc_pm_pmc_mask
= 0ULL;
786 kpc_pm_handler
= NULL
;
788 printf("kpc: pm released counters\n");
791 assert(kpc_get_counter_count(KPC_CLASS_CONFIGURABLE_MASK
) == kpc_configurable_count());
795 kpc_popcount(uint64_t value
)
797 return __builtin_popcountll(value
);
801 kpc_get_configurable_pmc_mask(uint32_t classes
)
803 uint32_t configurable_count
= kpc_configurable_count();
804 uint64_t cfg_mask
= 0ULL, pwr_mask
= 0ULL, all_cfg_pmcs_mask
= 0ULL;
806 /* not configurable classes or no configurable counters */
807 if (((classes
& (KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
)) == 0) ||
808 (configurable_count
== 0))
813 assert(configurable_count
< 64);
814 all_cfg_pmcs_mask
= (1ULL << configurable_count
) - 1;
816 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
817 if (force_all_ctrs
== TRUE
)
818 cfg_mask
|= all_cfg_pmcs_mask
;
820 cfg_mask
|= (~kpc_pm_pmc_mask
) & all_cfg_pmcs_mask
;
824 * The power class exists iff:
825 * - No tasks acquired all PMCs
826 * - PM registered and uses kpc to interact with PMCs
828 if ((force_all_ctrs
== FALSE
) &&
829 (kpc_pm_handler
!= NULL
) &&
830 (kpc_pm_has_custom_config
== FALSE
) &&
831 (classes
& KPC_CLASS_POWER_MASK
))
833 pwr_mask
|= kpc_pm_pmc_mask
& all_cfg_pmcs_mask
;
837 /* post-conditions */
838 assert( ((cfg_mask
| pwr_mask
) & (~all_cfg_pmcs_mask
)) == 0 );
839 assert( kpc_popcount(cfg_mask
| pwr_mask
) <= kpc_configurable_count() );
840 assert( (cfg_mask
& pwr_mask
) == 0ULL );
842 return cfg_mask
| pwr_mask
;