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 
8 #include <asm/cpu_device_id.h>
9 
10 #include "ifs.h"
11 
12 #define X86_MATCH(model)				\
13 	X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6,	\
14 		INTEL_FAM6_##model, X86_FEATURE_CORE_CAPABILITIES, NULL)
15 
16 static const struct x86_cpu_id ifs_cpu_ids[] __initconst = {
17 	X86_MATCH(SAPPHIRERAPIDS_X),
18 	{}
19 };
20 MODULE_DEVICE_TABLE(x86cpu, ifs_cpu_ids);
21 
22 static struct ifs_device ifs_device = {
23 	.data = {
24 		.integrity_cap_bit = MSR_INTEGRITY_CAPS_PERIODIC_BIST_BIT,
25 	},
26 	.misc = {
27 		.name = "intel_ifs_0",
28 		.nodename = "intel_ifs/0",
29 		.minor = MISC_DYNAMIC_MINOR,
30 	},
31 };
32 
ifs_init(void)33 static int __init ifs_init(void)
34 {
35 	const struct x86_cpu_id *m;
36 	u64 msrval;
37 
38 	m = x86_match_cpu(ifs_cpu_ids);
39 	if (!m)
40 		return -ENODEV;
41 
42 	if (rdmsrl_safe(MSR_IA32_CORE_CAPS, &msrval))
43 		return -ENODEV;
44 
45 	if (!(msrval & MSR_IA32_CORE_CAPS_INTEGRITY_CAPS))
46 		return -ENODEV;
47 
48 	if (rdmsrl_safe(MSR_INTEGRITY_CAPS, &msrval))
49 		return -ENODEV;
50 
51 	ifs_device.misc.groups = ifs_get_groups();
52 
53 	if ((msrval & BIT(ifs_device.data.integrity_cap_bit)) &&
54 	    !misc_register(&ifs_device.misc)) {
55 		down(&ifs_sem);
56 		ifs_load_firmware(ifs_device.misc.this_device);
57 		up(&ifs_sem);
58 		return 0;
59 	}
60 
61 	return -ENODEV;
62 }
63 
ifs_exit(void)64 static void __exit ifs_exit(void)
65 {
66 	misc_deregister(&ifs_device.misc);
67 }
68 
69 module_init(ifs_init);
70 module_exit(ifs_exit);
71 
72 MODULE_LICENSE("GPL");
73 MODULE_DESCRIPTION("Intel In Field Scan (IFS) device");
74