1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2022
4  */
5 
6 #include <linux/cpufeature.h>
7 #include <linux/bug.h>
8 #include <asm/elf.h>
9 
10 enum {
11 	TYPE_HWCAP,
12 	TYPE_FACILITY,
13 };
14 
15 struct s390_cpu_feature {
16 	unsigned int type	: 4;
17 	unsigned int num	: 28;
18 };
19 
20 static struct s390_cpu_feature s390_cpu_features[MAX_CPU_FEATURES] = {
21 	[S390_CPU_FEATURE_MSA]	= {.type = TYPE_HWCAP, .num = HWCAP_NR_MSA},
22 	[S390_CPU_FEATURE_VXRS]	= {.type = TYPE_HWCAP, .num = HWCAP_NR_VXRS},
23 	[S390_CPU_FEATURE_UV]	= {.type = TYPE_FACILITY, .num = 158},
24 };
25 
26 /*
27  * cpu_have_feature - Test CPU features on module initialization
28  */
cpu_have_feature(unsigned int num)29 int cpu_have_feature(unsigned int num)
30 {
31 	struct s390_cpu_feature *feature;
32 
33 	if (WARN_ON_ONCE(num >= MAX_CPU_FEATURES))
34 		return 0;
35 	feature = &s390_cpu_features[num];
36 	switch (feature->type) {
37 	case TYPE_HWCAP:
38 		return !!(elf_hwcap & BIT(feature->num));
39 	case TYPE_FACILITY:
40 		return test_facility(feature->num);
41 	default:
42 		WARN_ON_ONCE(1);
43 		return 0;
44 	}
45 }
46 EXPORT_SYMBOL(cpu_have_feature);
47