Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright(c) 2022 Intel Corporation. */
3
4#include <linux/module.h>
5#include <linux/kdev_t.h>
6#include <linux/semaphore.h>
7#include <linux/slab.h>
8
9#include <asm/cpu_device_id.h>
10
11#include "ifs.h"
12
13#define X86_MATCH(model) \
14 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, \
15 INTEL_FAM6_##model, X86_FEATURE_CORE_CAPABILITIES, NULL)
16
17static const struct x86_cpu_id ifs_cpu_ids[] __initconst = {
18 X86_MATCH(SAPPHIRERAPIDS_X),
19 {}
20};
21MODULE_DEVICE_TABLE(x86cpu, ifs_cpu_ids);
22
23static struct ifs_device ifs_device = {
24 .data = {
25 .integrity_cap_bit = MSR_INTEGRITY_CAPS_PERIODIC_BIST_BIT,
26 .test_num = 0,
27 },
28 .misc = {
29 .name = "intel_ifs_0",
30 .nodename = "intel_ifs/0",
31 .minor = MISC_DYNAMIC_MINOR,
32 },
33};
34
35static int __init ifs_init(void)
36{
37 const struct x86_cpu_id *m;
38 u64 msrval;
39 int ret;
40
41 m = x86_match_cpu(ifs_cpu_ids);
42 if (!m)
43 return -ENODEV;
44
45 if (rdmsrl_safe(MSR_IA32_CORE_CAPS, &msrval))
46 return -ENODEV;
47
48 if (!(msrval & MSR_IA32_CORE_CAPS_INTEGRITY_CAPS))
49 return -ENODEV;
50
51 if (rdmsrl_safe(MSR_INTEGRITY_CAPS, &msrval))
52 return -ENODEV;
53
54 ifs_device.misc.groups = ifs_get_groups();
55
56 if (!(msrval & BIT(ifs_device.data.integrity_cap_bit)))
57 return -ENODEV;
58
59 ifs_device.data.pkg_auth = kmalloc_array(topology_max_packages(), sizeof(bool), GFP_KERNEL);
60 if (!ifs_device.data.pkg_auth)
61 return -ENOMEM;
62
63 ret = misc_register(&ifs_device.misc);
64 if (ret) {
65 kfree(ifs_device.data.pkg_auth);
66 return ret;
67 }
68
69 return 0;
70}
71
72static void __exit ifs_exit(void)
73{
74 misc_deregister(&ifs_device.misc);
75 kfree(ifs_device.data.pkg_auth);
76}
77
78module_init(ifs_init);
79module_exit(ifs_exit);
80
81MODULE_LICENSE("GPL");
82MODULE_DESCRIPTION("Intel In Field Scan (IFS) device");
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(vfm, array_gen) \
15 X86_MATCH_VFM_FEATURE(vfm, X86_FEATURE_CORE_CAPABILITIES, array_gen)
16
17static const struct x86_cpu_id ifs_cpu_ids[] __initconst = {
18 X86_MATCH(INTEL_SAPPHIRERAPIDS_X, ARRAY_GEN0),
19 X86_MATCH(INTEL_EMERALDRAPIDS_X, ARRAY_GEN0),
20 X86_MATCH(INTEL_GRANITERAPIDS_X, ARRAY_GEN0),
21 X86_MATCH(INTEL_GRANITERAPIDS_D, ARRAY_GEN0),
22 X86_MATCH(INTEL_ATOM_CRESTMONT_X, ARRAY_GEN1),
23 X86_MATCH(INTEL_ATOM_DARKMONT_X, ARRAY_GEN1),
24 {}
25};
26MODULE_DEVICE_TABLE(x86cpu, ifs_cpu_ids);
27
28ATTRIBUTE_GROUPS(plat_ifs);
29ATTRIBUTE_GROUPS(plat_ifs_array);
30
31bool *ifs_pkg_auth;
32
33static const struct ifs_test_caps scan_test = {
34 .integrity_cap_bit = MSR_INTEGRITY_CAPS_PERIODIC_BIST_BIT,
35 .test_num = IFS_TYPE_SAF,
36 .image_suffix = "scan",
37};
38
39static const struct ifs_test_caps array_test = {
40 .integrity_cap_bit = MSR_INTEGRITY_CAPS_ARRAY_BIST_BIT,
41 .test_num = IFS_TYPE_ARRAY_BIST,
42};
43
44static const struct ifs_test_msrs scan_msrs = {
45 .copy_hashes = MSR_COPY_SCAN_HASHES,
46 .copy_hashes_status = MSR_SCAN_HASHES_STATUS,
47 .copy_chunks = MSR_AUTHENTICATE_AND_COPY_CHUNK,
48 .copy_chunks_status = MSR_CHUNKS_AUTHENTICATION_STATUS,
49 .test_ctrl = MSR_SAF_CTRL,
50};
51
52static const struct ifs_test_msrs sbaf_msrs = {
53 .copy_hashes = MSR_COPY_SBAF_HASHES,
54 .copy_hashes_status = MSR_SBAF_HASHES_STATUS,
55 .copy_chunks = MSR_AUTHENTICATE_AND_COPY_SBAF_CHUNK,
56 .copy_chunks_status = MSR_SBAF_CHUNKS_AUTHENTICATION_STATUS,
57 .test_ctrl = MSR_SBAF_CTRL,
58};
59
60static const struct ifs_test_caps sbaf_test = {
61 .integrity_cap_bit = MSR_INTEGRITY_CAPS_SBAF_BIT,
62 .test_num = IFS_TYPE_SBAF,
63 .image_suffix = "sbft",
64};
65
66static struct ifs_device ifs_devices[] = {
67 [IFS_TYPE_SAF] = {
68 .test_caps = &scan_test,
69 .test_msrs = &scan_msrs,
70 .misc = {
71 .name = "intel_ifs_0",
72 .minor = MISC_DYNAMIC_MINOR,
73 .groups = plat_ifs_groups,
74 },
75 },
76 [IFS_TYPE_ARRAY_BIST] = {
77 .test_caps = &array_test,
78 .misc = {
79 .name = "intel_ifs_1",
80 .minor = MISC_DYNAMIC_MINOR,
81 .groups = plat_ifs_array_groups,
82 },
83 },
84 [IFS_TYPE_SBAF] = {
85 .test_caps = &sbaf_test,
86 .test_msrs = &sbaf_msrs,
87 .misc = {
88 .name = "intel_ifs_2",
89 .minor = MISC_DYNAMIC_MINOR,
90 .groups = plat_ifs_groups,
91 },
92 },
93};
94
95#define IFS_NUMTESTS ARRAY_SIZE(ifs_devices)
96
97static void ifs_cleanup(void)
98{
99 int i;
100
101 for (i = 0; i < IFS_NUMTESTS; i++) {
102 if (ifs_devices[i].misc.this_device)
103 misc_deregister(&ifs_devices[i].misc);
104 }
105 kfree(ifs_pkg_auth);
106}
107
108static int __init ifs_init(void)
109{
110 const struct x86_cpu_id *m;
111 u64 msrval;
112 int i, ret;
113
114 m = x86_match_cpu(ifs_cpu_ids);
115 if (!m)
116 return -ENODEV;
117
118 if (rdmsrl_safe(MSR_IA32_CORE_CAPS, &msrval))
119 return -ENODEV;
120
121 if (!(msrval & MSR_IA32_CORE_CAPS_INTEGRITY_CAPS))
122 return -ENODEV;
123
124 if (rdmsrl_safe(MSR_INTEGRITY_CAPS, &msrval))
125 return -ENODEV;
126
127 ifs_pkg_auth = kmalloc_array(topology_max_packages(), sizeof(bool), GFP_KERNEL);
128 if (!ifs_pkg_auth)
129 return -ENOMEM;
130
131 for (i = 0; i < IFS_NUMTESTS; i++) {
132 if (!(msrval & BIT(ifs_devices[i].test_caps->integrity_cap_bit)))
133 continue;
134 ifs_devices[i].rw_data.generation = FIELD_GET(MSR_INTEGRITY_CAPS_SAF_GEN_MASK,
135 msrval);
136 ifs_devices[i].rw_data.array_gen = (u32)m->driver_data;
137 ret = misc_register(&ifs_devices[i].misc);
138 if (ret)
139 goto err_exit;
140 }
141 return 0;
142
143err_exit:
144 ifs_cleanup();
145 return ret;
146}
147
148static void __exit ifs_exit(void)
149{
150 ifs_cleanup();
151}
152
153module_init(ifs_init);
154module_exit(ifs_exit);
155
156MODULE_LICENSE("GPL");
157MODULE_DESCRIPTION("Intel In Field Scan (IFS) device");