1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation. */
3
4 #include <linux/bitfield.h>
5 #include <linux/module.h>
6 #include <linux/kdev_t.h>
7 #include <linux/semaphore.h>
8 #include <linux/slab.h>
9
10 #include <asm/cpu_device_id.h>
11
12 #include "ifs.h"
13
14 #define X86_MATCH(model) \
15 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, \
16 INTEL_FAM6_##model, X86_FEATURE_CORE_CAPABILITIES, NULL)
17
18 static const struct x86_cpu_id ifs_cpu_ids[] __initconst = {
19 X86_MATCH(SAPPHIRERAPIDS_X),
20 X86_MATCH(EMERALDRAPIDS_X),
21 {}
22 };
23 MODULE_DEVICE_TABLE(x86cpu, ifs_cpu_ids);
24
25 ATTRIBUTE_GROUPS(plat_ifs);
26 ATTRIBUTE_GROUPS(plat_ifs_array);
27
28 bool *ifs_pkg_auth;
29
30 static const struct ifs_test_caps scan_test = {
31 .integrity_cap_bit = MSR_INTEGRITY_CAPS_PERIODIC_BIST_BIT,
32 .test_num = IFS_TYPE_SAF,
33 };
34
35 static const struct ifs_test_caps array_test = {
36 .integrity_cap_bit = MSR_INTEGRITY_CAPS_ARRAY_BIST_BIT,
37 .test_num = IFS_TYPE_ARRAY_BIST,
38 };
39
40 static struct ifs_device ifs_devices[] = {
41 [IFS_TYPE_SAF] = {
42 .test_caps = &scan_test,
43 .misc = {
44 .name = "intel_ifs_0",
45 .minor = MISC_DYNAMIC_MINOR,
46 .groups = plat_ifs_groups,
47 },
48 },
49 [IFS_TYPE_ARRAY_BIST] = {
50 .test_caps = &array_test,
51 .misc = {
52 .name = "intel_ifs_1",
53 .minor = MISC_DYNAMIC_MINOR,
54 .groups = plat_ifs_array_groups,
55 },
56 },
57 };
58
59 #define IFS_NUMTESTS ARRAY_SIZE(ifs_devices)
60
ifs_cleanup(void)61 static void ifs_cleanup(void)
62 {
63 int i;
64
65 for (i = 0; i < IFS_NUMTESTS; i++) {
66 if (ifs_devices[i].misc.this_device)
67 misc_deregister(&ifs_devices[i].misc);
68 }
69 kfree(ifs_pkg_auth);
70 }
71
ifs_init(void)72 static int __init ifs_init(void)
73 {
74 const struct x86_cpu_id *m;
75 u64 msrval;
76 int i, ret;
77
78 m = x86_match_cpu(ifs_cpu_ids);
79 if (!m)
80 return -ENODEV;
81
82 if (rdmsrl_safe(MSR_IA32_CORE_CAPS, &msrval))
83 return -ENODEV;
84
85 if (!(msrval & MSR_IA32_CORE_CAPS_INTEGRITY_CAPS))
86 return -ENODEV;
87
88 if (rdmsrl_safe(MSR_INTEGRITY_CAPS, &msrval))
89 return -ENODEV;
90
91 ifs_pkg_auth = kmalloc_array(topology_max_packages(), sizeof(bool), GFP_KERNEL);
92 if (!ifs_pkg_auth)
93 return -ENOMEM;
94
95 for (i = 0; i < IFS_NUMTESTS; i++) {
96 if (!(msrval & BIT(ifs_devices[i].test_caps->integrity_cap_bit)))
97 continue;
98 ifs_devices[i].rw_data.generation = FIELD_GET(MSR_INTEGRITY_CAPS_SAF_GEN_MASK,
99 msrval);
100 ret = misc_register(&ifs_devices[i].misc);
101 if (ret)
102 goto err_exit;
103 }
104 return 0;
105
106 err_exit:
107 ifs_cleanup();
108 return ret;
109 }
110
ifs_exit(void)111 static void __exit ifs_exit(void)
112 {
113 ifs_cleanup();
114 }
115
116 module_init(ifs_init);
117 module_exit(ifs_exit);
118
119 MODULE_LICENSE("GPL");
120 MODULE_DESCRIPTION("Intel In Field Scan (IFS) device");
121