1 // SPDX-License-Identifier: GPL-2.0
3 * CPU-Measurement Counter Facility Support - Common Layer
5 * Copyright IBM Corp. 2019
8 #define KMSG_COMPONENT "cpum_cf_common"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/percpu.h>
14 #include <linux/notifier.h>
15 #include <linux/init.h>
16 #include <linux/export.h>
17 #include <asm/ctl_reg.h>
19 #include <asm/cpu_mcf.h>
21 /* Per-CPU event structure for the counter facility */
22 DEFINE_PER_CPU(struct cpu_cf_events, cpu_cf_events) = {
24 [CPUMF_CTR_SET_BASIC] = ATOMIC_INIT(0),
25 [CPUMF_CTR_SET_USER] = ATOMIC_INIT(0),
26 [CPUMF_CTR_SET_CRYPTO] = ATOMIC_INIT(0),
27 [CPUMF_CTR_SET_EXT] = ATOMIC_INIT(0),
28 [CPUMF_CTR_SET_MT_DIAG] = ATOMIC_INIT(0),
30 .alert = ATOMIC64_INIT(0),
38 /* Indicator whether the CPU-Measurement Counter Facility Support is ready */
39 static bool cpum_cf_initalized;
41 /* CPU-measurement alerts for the counter facility */
42 static void cpumf_measurement_alert(struct ext_code ext_code,
43 unsigned int alert, unsigned long unused)
45 struct cpu_cf_events *cpuhw;
47 if (!(alert & CPU_MF_INT_CF_MASK))
50 inc_irq_stat(IRQEXT_CMC);
51 cpuhw = this_cpu_ptr(&cpu_cf_events);
53 /* Measurement alerts are shared and might happen when the PMU
54 * is not reserved. Ignore these alerts in this case. */
55 if (!(cpuhw->flags & PMU_F_RESERVED))
58 /* counter authorization change alert */
59 if (alert & CPU_MF_INT_CF_CACA)
62 /* loss of counter data alert */
63 if (alert & CPU_MF_INT_CF_LCDA)
64 pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
66 /* loss of MT counter data alert */
67 if (alert & CPU_MF_INT_CF_MTDA)
68 pr_warn("CPU[%i] MT counter data was lost\n",
71 /* store alert for special handling by in-kernel users */
72 atomic64_or(alert, &cpuhw->alert);
77 static void cpum_cf_setup_cpu(void *flags)
79 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
81 switch (*((int *) flags)) {
83 memset(&cpuhw->info, 0, sizeof(cpuhw->info));
85 cpuhw->flags |= PMU_F_RESERVED;
89 cpuhw->flags &= ~PMU_F_RESERVED;
93 /* Disable CPU counter sets */
97 bool kernel_cpumcf_avail(void)
99 return cpum_cf_initalized;
101 EXPORT_SYMBOL(kernel_cpumcf_avail);
103 /* Initialize the CPU-measurement counter facility */
104 int __kernel_cpumcf_begin(void)
106 int flags = PMC_INIT;
108 on_each_cpu(cpum_cf_setup_cpu, &flags, 1);
109 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
113 EXPORT_SYMBOL(__kernel_cpumcf_begin);
115 /* Obtain the CPU-measurement alerts for the counter facility */
116 unsigned long kernel_cpumcf_alert(int clear)
118 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
121 alert = atomic64_read(&cpuhw->alert);
123 atomic64_set(&cpuhw->alert, 0);
127 EXPORT_SYMBOL(kernel_cpumcf_alert);
129 /* Release the CPU-measurement counter facility */
130 void __kernel_cpumcf_end(void)
132 int flags = PMC_RELEASE;
134 on_each_cpu(cpum_cf_setup_cpu, &flags, 1);
135 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
137 EXPORT_SYMBOL(__kernel_cpumcf_end);
139 static int cpum_cf_setup(unsigned int cpu, int flags)
142 cpum_cf_setup_cpu(&flags);
147 static int cpum_cf_online_cpu(unsigned int cpu)
149 cpum_cf_setup(cpu, PMC_INIT);
150 return cfset_online_cpu(cpu);
153 static int cpum_cf_offline_cpu(unsigned int cpu)
155 cfset_offline_cpu(cpu);
156 return cpum_cf_setup(cpu, PMC_RELEASE);
159 /* Return the maximum possible counter set size (in number of 8 byte counters)
160 * depending on type and model number.
162 size_t cpum_cf_ctrset_size(enum cpumf_ctr_set ctrset,
163 struct cpumf_ctr_info *info)
165 size_t ctrset_size = 0;
168 case CPUMF_CTR_SET_BASIC:
172 case CPUMF_CTR_SET_USER:
175 else if (info->cfvn >= 3)
178 case CPUMF_CTR_SET_CRYPTO:
179 if (info->csvn >= 1 && info->csvn <= 5)
181 else if (info->csvn == 6)
184 case CPUMF_CTR_SET_EXT:
187 else if (info->csvn == 2)
189 else if (info->csvn >= 3 && info->csvn <= 5)
191 else if (info->csvn == 6)
194 case CPUMF_CTR_SET_MT_DIAG:
198 case CPUMF_CTR_SET_MAX:
205 static int __init cpum_cf_init(void)
209 if (!cpum_cf_avail())
212 /* clear bit 15 of cr0 to unauthorize problem-state to
213 * extract measurement counters */
214 ctl_clear_bit(0, 48);
216 /* register handler for measurement-alert interruptions */
217 rc = register_external_irq(EXT_IRQ_MEASURE_ALERT,
218 cpumf_measurement_alert);
220 pr_err("Registering for CPU-measurement alerts "
221 "failed with rc=%i\n", rc);
225 rc = cpuhp_setup_state(CPUHP_AP_PERF_S390_CF_ONLINE,
226 "perf/s390/cf:online",
227 cpum_cf_online_cpu, cpum_cf_offline_cpu);
229 cpum_cf_initalized = true;
233 early_initcall(cpum_cf_init);