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 uint32_t kpc_actionid
[KPC_MAX_COUNTERS
];
50 #define COUNTERBUF_SIZE_PER_CPU (KPC_MAX_COUNTERS * sizeof(uint64_t))
51 #define COUNTERBUF_SIZE (machine_info.logical_cpu_max * \
52 COUNTERBUF_SIZE_PER_CPU)
55 static lck_grp_attr_t
*kpc_config_lckgrp_attr
= NULL
;
56 static lck_grp_t
*kpc_config_lckgrp
= NULL
;
57 static lck_mtx_t kpc_config_lock
;
59 /* state specifying if all counters have been requested by kperf */
60 static boolean_t force_all_ctrs
= FALSE
;
63 static kpc_pm_handler_t kpc_pm_handler
;
64 static boolean_t kpc_pm_has_custom_config
;
65 static uint64_t kpc_pm_pmc_mask
;
67 static bool kpc_calling_pm
= false;
68 #endif /* MACH_ASSERT */
70 boolean_t kpc_context_switch_active
= FALSE
;
71 bool kpc_supported
= true;
76 kpc_config_lckgrp_attr
= lck_grp_attr_alloc_init();
77 kpc_config_lckgrp
= lck_grp_alloc_init("kpc", kpc_config_lckgrp_attr
);
78 lck_mtx_init(&kpc_config_lock
, kpc_config_lckgrp
, LCK_ATTR_NULL
);
82 kpc_register_cpu(struct cpu_data
*cpu_data
)
85 assert(cpu_data
->cpu_kpc_buf
[0] == NULL
);
86 assert(cpu_data
->cpu_kpc_buf
[1] == NULL
);
87 assert(cpu_data
->cpu_kpc_shadow
== NULL
);
88 assert(cpu_data
->cpu_kpc_reload
== NULL
);
91 * Buffers allocated through kpc_counterbuf_alloc() are large enough to
92 * store all PMCs values from all CPUs. This mimics the userspace API.
93 * This does not suit well with the per-CPU kpc buffers, since:
94 * 1. Buffers don't need to be this large.
95 * 2. The actual number of CPUs is not known at this point.
97 * CPUs are asked to callout into kpc when being registered, we'll
98 * allocate the memory here.
101 if ((cpu_data
->cpu_kpc_buf
[0] = kalloc(COUNTERBUF_SIZE_PER_CPU
)) == NULL
) {
104 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
) {
110 if ((cpu_data
->cpu_kpc_reload
= kalloc(COUNTERBUF_SIZE_PER_CPU
)) == NULL
) {
114 memset(cpu_data
->cpu_kpc_buf
[0], 0, COUNTERBUF_SIZE_PER_CPU
);
115 memset(cpu_data
->cpu_kpc_buf
[1], 0, COUNTERBUF_SIZE_PER_CPU
);
116 memset(cpu_data
->cpu_kpc_shadow
, 0, COUNTERBUF_SIZE_PER_CPU
);
117 memset(cpu_data
->cpu_kpc_reload
, 0, COUNTERBUF_SIZE_PER_CPU
);
123 kpc_unregister_cpu(cpu_data
);
128 kpc_unregister_cpu(struct cpu_data
*cpu_data
)
131 if (cpu_data
->cpu_kpc_buf
[0] != NULL
) {
132 kfree(cpu_data
->cpu_kpc_buf
[0], COUNTERBUF_SIZE_PER_CPU
);
133 cpu_data
->cpu_kpc_buf
[0] = NULL
;
135 if (cpu_data
->cpu_kpc_buf
[1] != NULL
) {
136 kfree(cpu_data
->cpu_kpc_buf
[1], COUNTERBUF_SIZE_PER_CPU
);
137 cpu_data
->cpu_kpc_buf
[1] = NULL
;
139 if (cpu_data
->cpu_kpc_shadow
!= NULL
) {
140 kfree(cpu_data
->cpu_kpc_shadow
, COUNTERBUF_SIZE_PER_CPU
);
141 cpu_data
->cpu_kpc_shadow
= NULL
;
143 if (cpu_data
->cpu_kpc_reload
!= NULL
) {
144 kfree(cpu_data
->cpu_kpc_reload
, COUNTERBUF_SIZE_PER_CPU
);
145 cpu_data
->cpu_kpc_reload
= NULL
;
151 kpc_task_set_forced_all_ctrs(task_t task
, boolean_t state
)
157 task
->t_kpc
|= TASK_KPC_FORCED_ALL_CTRS
;
159 task
->t_kpc
&= ~TASK_KPC_FORCED_ALL_CTRS
;
165 kpc_task_get_forced_all_ctrs(task_t task
)
168 return task
->t_kpc
& TASK_KPC_FORCED_ALL_CTRS
? TRUE
: FALSE
;
172 kpc_force_all_ctrs(task_t task
, int val
)
174 boolean_t new_state
= val
? TRUE
: FALSE
;
175 boolean_t old_state
= kpc_get_force_all_ctrs();
178 * Refuse to do the operation if the counters are already forced by
181 if (kpc_get_force_all_ctrs() && !kpc_task_get_forced_all_ctrs(task
)) {
185 /* nothing to do if the state is not changing */
186 if (old_state
== new_state
) {
190 /* notify the power manager */
191 if (kpc_pm_handler
) {
193 kpc_calling_pm
= true;
194 #endif /* MACH_ASSERT */
195 kpc_pm_handler( new_state
? FALSE
: TRUE
);
197 kpc_calling_pm
= false;
198 #endif /* MACH_ASSERT */
202 * This is a force -- ensure that counters are forced, even if power
203 * management fails to acknowledge it.
205 if (force_all_ctrs
!= new_state
) {
206 force_all_ctrs
= new_state
;
209 /* update the task bits */
210 kpc_task_set_forced_all_ctrs(task
, new_state
);
216 kpc_pm_acknowledge(boolean_t available_to_pm
)
219 * Force-all-counters should still be true when the counters are being
220 * made available to power management and false when counters are going
223 assert(force_all_ctrs
== available_to_pm
);
225 * Make sure power management isn't playing games with us.
227 assert(kpc_calling_pm
== true);
230 * Counters being available means no one is forcing all counters.
232 force_all_ctrs
= available_to_pm
? FALSE
: TRUE
;
236 kpc_get_force_all_ctrs(void)
238 return force_all_ctrs
;
242 kpc_multiple_clients(void)
244 return kpc_pm_handler
!= NULL
;
248 kpc_controls_fixed_counters(void)
250 return !kpc_pm_handler
|| force_all_ctrs
|| !kpc_pm_has_custom_config
;
254 kpc_controls_counter(uint32_t ctr
)
256 uint64_t pmc_mask
= 0ULL;
258 assert(ctr
< (kpc_fixed_count() + kpc_configurable_count()));
260 if (ctr
< kpc_fixed_count()) {
261 return kpc_controls_fixed_counters();
265 * By default kpc manages all PMCs, but if the Power Manager registered
266 * with custom_config=TRUE, the Power Manager manages its reserved PMCs.
267 * However, kpc takes ownership back if a task acquired all PMCs via
270 pmc_mask
= (1ULL << (ctr
- kpc_fixed_count()));
271 if ((pmc_mask
& kpc_pm_pmc_mask
) && kpc_pm_has_custom_config
&& !force_all_ctrs
) {
279 kpc_get_running(void)
281 uint64_t pmc_mask
= 0;
282 uint32_t cur_state
= 0;
284 if (kpc_is_running_fixed()) {
285 cur_state
|= KPC_CLASS_FIXED_MASK
;
288 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
289 if (kpc_is_running_configurable(pmc_mask
)) {
290 cur_state
|= KPC_CLASS_CONFIGURABLE_MASK
;
293 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
294 if ((pmc_mask
!= 0) && kpc_is_running_configurable(pmc_mask
)) {
295 cur_state
|= KPC_CLASS_POWER_MASK
;
301 /* may be called from an IPI */
303 kpc_get_curcpu_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
305 int enabled
= 0, offset
= 0;
306 uint64_t pmc_mask
= 0ULL;
310 enabled
= ml_set_interrupts_enabled(FALSE
);
312 /* grab counters and CPU number as close as possible */
314 *curcpu
= current_processor()->cpu_id
;
317 if (classes
& KPC_CLASS_FIXED_MASK
) {
318 kpc_get_fixed_counters(&buf
[offset
]);
319 offset
+= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
322 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
323 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
324 kpc_get_configurable_counters(&buf
[offset
], pmc_mask
);
325 offset
+= kpc_popcount(pmc_mask
);
328 if (classes
& KPC_CLASS_POWER_MASK
) {
329 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
330 kpc_get_configurable_counters(&buf
[offset
], pmc_mask
);
331 offset
+= kpc_popcount(pmc_mask
);
334 ml_set_interrupts_enabled(enabled
);
339 /* generic counter reading function, public api */
341 kpc_get_cpu_counters(boolean_t all_cpus
, uint32_t classes
,
342 int *curcpu
, uint64_t *buf
)
347 * Unlike reading the current CPU counters, reading counters from all
348 * CPUs is architecture dependent. This allows kpc to make the most of
349 * the platform if memory mapped registers is supported.
352 return kpc_get_all_cpus_counters(classes
, curcpu
, buf
);
354 return kpc_get_curcpu_counters(classes
, curcpu
, buf
);
359 kpc_get_shadow_counters(boolean_t all_cpus
, uint32_t classes
,
360 int *curcpu
, uint64_t *buf
)
362 int curcpu_id
= current_processor()->cpu_id
;
363 uint32_t cfg_count
= kpc_configurable_count(), offset
= 0;
364 uint64_t pmc_mask
= 0ULL;
369 enabled
= ml_set_interrupts_enabled(FALSE
);
371 curcpu_id
= current_processor()->cpu_id
;
376 for (int cpu
= 0; cpu
< machine_info
.logical_cpu_max
; ++cpu
) {
377 /* filter if the caller did not request all cpus */
378 if (!all_cpus
&& (cpu
!= curcpu_id
)) {
382 if (classes
& KPC_CLASS_FIXED_MASK
) {
383 uint32_t count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
384 memcpy(&buf
[offset
], &FIXED_SHADOW_CPU(cpu
, 0), count
* sizeof(uint64_t));
388 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
389 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
391 for (uint32_t cfg_ctr
= 0; cfg_ctr
< cfg_count
; ++cfg_ctr
) {
392 if ((1ULL << cfg_ctr
) & pmc_mask
) {
393 buf
[offset
++] = CONFIGURABLE_SHADOW_CPU(cpu
, cfg_ctr
);
398 if (classes
& KPC_CLASS_POWER_MASK
) {
399 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
401 for (uint32_t cfg_ctr
= 0; cfg_ctr
< cfg_count
; ++cfg_ctr
) {
402 if ((1ULL << cfg_ctr
) & pmc_mask
) {
403 buf
[offset
++] = CONFIGURABLE_SHADOW_CPU(cpu
, cfg_ctr
);
409 ml_set_interrupts_enabled(enabled
);
415 kpc_get_counter_count(uint32_t classes
)
419 if (classes
& KPC_CLASS_FIXED_MASK
) {
420 count
+= kpc_fixed_count();
423 if (classes
& (KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
)) {
424 uint64_t pmc_msk
= kpc_get_configurable_pmc_mask(classes
);
425 uint32_t pmc_cnt
= kpc_popcount(pmc_msk
);
433 kpc_get_config_count(uint32_t classes
)
437 if (classes
& KPC_CLASS_FIXED_MASK
) {
438 count
+= kpc_fixed_config_count();
441 if (classes
& (KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
)) {
442 uint64_t pmc_mask
= kpc_get_configurable_pmc_mask(classes
);
443 count
+= kpc_configurable_config_count(pmc_mask
);
446 if ((classes
& KPC_CLASS_RAWPMU_MASK
) && !kpc_multiple_clients()) {
447 count
+= kpc_rawpmu_config_count();
454 kpc_get_config(uint32_t classes
, kpc_config_t
*current_config
)
458 assert(current_config
);
460 if (classes
& KPC_CLASS_FIXED_MASK
) {
461 kpc_get_fixed_config(¤t_config
[count
]);
462 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
465 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
466 uint64_t pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
467 kpc_get_configurable_config(¤t_config
[count
], pmc_mask
);
468 count
+= kpc_get_config_count(KPC_CLASS_CONFIGURABLE_MASK
);
471 if (classes
& KPC_CLASS_POWER_MASK
) {
472 uint64_t pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
473 kpc_get_configurable_config(¤t_config
[count
], pmc_mask
);
474 count
+= kpc_get_config_count(KPC_CLASS_POWER_MASK
);
477 if (classes
& KPC_CLASS_RAWPMU_MASK
) {
478 // Client shouldn't ask for config words that aren't available.
479 // Most likely, they'd misinterpret the returned buffer if we
481 if (kpc_multiple_clients()) {
484 kpc_get_rawpmu_config(¤t_config
[count
]);
485 count
+= kpc_get_config_count(KPC_CLASS_RAWPMU_MASK
);
492 kpc_set_config(uint32_t classes
, kpc_config_t
*configv
)
495 struct kpc_config_remote mp_config
= {
496 .classes
= classes
, .configv
= configv
,
497 .pmc_mask
= kpc_get_configurable_pmc_mask(classes
)
502 /* don't allow RAWPMU configuration when sharing counters */
503 if ((classes
& KPC_CLASS_RAWPMU_MASK
) && kpc_multiple_clients()) {
507 /* no clients have the right to modify both classes */
508 if ((classes
& (KPC_CLASS_CONFIGURABLE_MASK
)) &&
509 (classes
& (KPC_CLASS_POWER_MASK
))) {
513 lck_mtx_lock(&kpc_config_lock
);
515 /* translate the power class for the machine layer */
516 if (classes
& KPC_CLASS_POWER_MASK
) {
517 mp_config
.classes
|= KPC_CLASS_CONFIGURABLE_MASK
;
520 ret
= kpc_set_config_arch( &mp_config
);
522 lck_mtx_unlock(&kpc_config_lock
);
528 kpc_get_counterbuf_size(void)
530 return COUNTERBUF_SIZE
;
533 /* allocate a buffer large enough for all possible counters */
535 kpc_counterbuf_alloc(void)
537 uint64_t *buf
= NULL
;
539 buf
= kalloc_tag(COUNTERBUF_SIZE
, VM_KERN_MEMORY_DIAG
);
541 bzero(buf
, COUNTERBUF_SIZE
);
548 kpc_counterbuf_free(uint64_t *buf
)
551 kfree(buf
, COUNTERBUF_SIZE
);
556 kpc_sample_kperf(uint32_t actionid
)
558 struct kperf_sample sbuf
;
560 BUF_DATA(PERF_KPC_HNDLR
| DBG_FUNC_START
);
562 thread_t thread
= current_thread();
563 task_t task
= get_threadtask(thread
);
565 struct kperf_context ctx
= {
566 .cur_thread
= thread
,
568 .cur_pid
= task_pid(task
),
569 .trigger_type
= TRIGGER_TYPE_PMI
,
573 int r
= kperf_sample(&sbuf
, &ctx
, actionid
, SAMPLE_FLAG_PEND_USER
);
575 BUF_INFO(PERF_KPC_HNDLR
| DBG_FUNC_END
, r
);
580 kpc_set_period(uint32_t classes
, uint64_t *val
)
582 struct kpc_config_remote mp_config
= {
583 .classes
= classes
, .configv
= val
,
584 .pmc_mask
= kpc_get_configurable_pmc_mask(classes
)
589 /* no clients have the right to modify both classes */
590 if ((classes
& (KPC_CLASS_CONFIGURABLE_MASK
)) &&
591 (classes
& (KPC_CLASS_POWER_MASK
))) {
595 lck_mtx_lock(&kpc_config_lock
);
597 #ifdef FIXED_COUNTER_SHADOW
598 if ((classes
& KPC_CLASS_FIXED_MASK
) && !kpc_controls_fixed_counters()) {
599 lck_mtx_unlock(&kpc_config_lock
);
603 if (classes
& KPC_CLASS_FIXED_MASK
) {
604 lck_mtx_unlock(&kpc_config_lock
);
609 /* translate the power class for the machine layer */
610 if (classes
& KPC_CLASS_POWER_MASK
) {
611 mp_config
.classes
|= KPC_CLASS_CONFIGURABLE_MASK
;
614 kprintf("setting period %u\n", classes
);
615 kpc_set_period_arch( &mp_config
);
617 lck_mtx_unlock(&kpc_config_lock
);
623 kpc_get_period(uint32_t classes
, uint64_t *val
)
626 uint64_t pmc_mask
= 0ULL;
630 lck_mtx_lock(&kpc_config_lock
);
632 if (classes
& KPC_CLASS_FIXED_MASK
) {
633 /* convert reload values to periods */
634 count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
635 for (uint32_t i
= 0; i
< count
; ++i
) {
636 *val
++ = kpc_fixed_max() - FIXED_RELOAD(i
);
640 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
641 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
643 /* convert reload values to periods */
644 count
= kpc_configurable_count();
645 for (uint32_t i
= 0; i
< count
; ++i
) {
646 if ((1ULL << i
) & pmc_mask
) {
647 *val
++ = kpc_configurable_max() - CONFIGURABLE_RELOAD(i
);
652 if (classes
& KPC_CLASS_POWER_MASK
) {
653 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
655 /* convert reload values to periods */
656 count
= kpc_configurable_count();
657 for (uint32_t i
= 0; i
< count
; ++i
) {
658 if ((1ULL << i
) & pmc_mask
) {
659 *val
++ = kpc_configurable_max() - CONFIGURABLE_RELOAD(i
);
664 lck_mtx_unlock(&kpc_config_lock
);
670 kpc_set_actionid(uint32_t classes
, uint32_t *val
)
673 uint64_t pmc_mask
= 0ULL;
677 /* NOTE: what happens if a pmi occurs while actionids are being
678 * set is undefined. */
679 lck_mtx_lock(&kpc_config_lock
);
681 if (classes
& KPC_CLASS_FIXED_MASK
) {
682 count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
683 memcpy(&FIXED_ACTIONID(0), val
, count
* sizeof(uint32_t));
687 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
688 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
690 count
= kpc_configurable_count();
691 for (uint32_t i
= 0; i
< count
; ++i
) {
692 if ((1ULL << i
) & pmc_mask
) {
693 CONFIGURABLE_ACTIONID(i
) = *val
++;
698 if (classes
& KPC_CLASS_POWER_MASK
) {
699 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
701 count
= kpc_configurable_count();
702 for (uint32_t i
= 0; i
< count
; ++i
) {
703 if ((1ULL << i
) & pmc_mask
) {
704 CONFIGURABLE_ACTIONID(i
) = *val
++;
709 lck_mtx_unlock(&kpc_config_lock
);
715 kpc_get_actionid(uint32_t classes
, uint32_t *val
)
718 uint64_t pmc_mask
= 0ULL;
722 lck_mtx_lock(&kpc_config_lock
);
724 if (classes
& KPC_CLASS_FIXED_MASK
) {
725 count
= kpc_get_counter_count(KPC_CLASS_FIXED_MASK
);
726 memcpy(val
, &FIXED_ACTIONID(0), count
* sizeof(uint32_t));
730 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
731 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_CONFIGURABLE_MASK
);
733 count
= kpc_configurable_count();
734 for (uint32_t i
= 0; i
< count
; ++i
) {
735 if ((1ULL << i
) & pmc_mask
) {
736 *val
++ = CONFIGURABLE_ACTIONID(i
);
741 if (classes
& KPC_CLASS_POWER_MASK
) {
742 pmc_mask
= kpc_get_configurable_pmc_mask(KPC_CLASS_POWER_MASK
);
744 count
= kpc_configurable_count();
745 for (uint32_t i
= 0; i
< count
; ++i
) {
746 if ((1ULL << i
) & pmc_mask
) {
747 *val
++ = CONFIGURABLE_ACTIONID(i
);
752 lck_mtx_unlock(&kpc_config_lock
);
758 kpc_set_running(uint32_t classes
)
760 uint32_t all_cfg_classes
= KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
;
761 struct kpc_running_remote mp_config
= {
762 .classes
= classes
, .cfg_target_mask
= 0ULL, .cfg_state_mask
= 0ULL
765 /* target all available PMCs */
766 mp_config
.cfg_target_mask
= kpc_get_configurable_pmc_mask(all_cfg_classes
);
768 /* translate the power class for the machine layer */
769 if (classes
& KPC_CLASS_POWER_MASK
) {
770 mp_config
.classes
|= KPC_CLASS_CONFIGURABLE_MASK
;
773 /* generate the state of each configurable PMCs */
774 mp_config
.cfg_state_mask
= kpc_get_configurable_pmc_mask(classes
);
776 return kpc_set_running_arch(&mp_config
);
780 kpc_register_pm_handler(kpc_pm_handler_t handler
)
782 return kpc_reserve_pm_counters(0x38, handler
, TRUE
);
786 kpc_reserve_pm_counters(uint64_t pmc_mask
, kpc_pm_handler_t handler
,
787 boolean_t custom_config
)
789 uint64_t all_mask
= (1ULL << kpc_configurable_count()) - 1;
790 uint64_t req_mask
= 0ULL;
793 assert(handler
!= NULL
);
794 assert(kpc_pm_handler
== NULL
);
796 /* check number of counters requested */
797 req_mask
= (pmc_mask
& all_mask
);
798 assert(kpc_popcount(req_mask
) <= kpc_configurable_count());
800 /* save the power manager states */
801 kpc_pm_has_custom_config
= custom_config
;
802 kpc_pm_pmc_mask
= req_mask
;
803 kpc_pm_handler
= handler
;
805 printf("kpc: pm registered pmc_mask=%llx custom_config=%d\n",
806 req_mask
, custom_config
);
810 uint32_t cfg_count
= kpc_get_counter_count(KPC_CLASS_CONFIGURABLE_MASK
);
811 uint32_t pwr_count
= kpc_popcount(kpc_pm_pmc_mask
);
812 #pragma unused(cfg_count, pwr_count)
813 assert((cfg_count
+ pwr_count
) == kpc_configurable_count());
816 return force_all_ctrs
? FALSE
: TRUE
;
820 kpc_release_pm_counters(void)
823 assert(kpc_pm_handler
!= NULL
);
825 /* release the counters */
826 kpc_pm_has_custom_config
= FALSE
;
827 kpc_pm_pmc_mask
= 0ULL;
828 kpc_pm_handler
= NULL
;
830 printf("kpc: pm released counters\n");
833 assert(kpc_get_counter_count(KPC_CLASS_CONFIGURABLE_MASK
) == kpc_configurable_count());
837 kpc_popcount(uint64_t value
)
839 return __builtin_popcountll(value
);
843 kpc_get_configurable_pmc_mask(uint32_t classes
)
845 uint32_t configurable_count
= kpc_configurable_count();
846 uint64_t cfg_mask
= 0ULL, pwr_mask
= 0ULL, all_cfg_pmcs_mask
= 0ULL;
848 /* not configurable classes or no configurable counters */
849 if (((classes
& (KPC_CLASS_CONFIGURABLE_MASK
| KPC_CLASS_POWER_MASK
)) == 0) ||
850 (configurable_count
== 0)) {
854 assert(configurable_count
< 64);
855 all_cfg_pmcs_mask
= (1ULL << configurable_count
) - 1;
857 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
858 if (force_all_ctrs
== TRUE
) {
859 cfg_mask
|= all_cfg_pmcs_mask
;
861 cfg_mask
|= (~kpc_pm_pmc_mask
) & all_cfg_pmcs_mask
;
866 * The power class exists iff:
867 * - No tasks acquired all PMCs
868 * - PM registered and uses kpc to interact with PMCs
870 if ((force_all_ctrs
== FALSE
) &&
871 (kpc_pm_handler
!= NULL
) &&
872 (kpc_pm_has_custom_config
== FALSE
) &&
873 (classes
& KPC_CLASS_POWER_MASK
)) {
874 pwr_mask
|= kpc_pm_pmc_mask
& all_cfg_pmcs_mask
;
878 /* post-conditions */
879 assert(((cfg_mask
| pwr_mask
) & (~all_cfg_pmcs_mask
)) == 0 );
880 assert( kpc_popcount(cfg_mask
| pwr_mask
) <= kpc_configurable_count());
881 assert((cfg_mask
& pwr_mask
) == 0ULL );
883 return cfg_mask
| pwr_mask
;