1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright(c) 2022 Intel Corporation. */
3 
4 #ifndef _IFS_H_
5 #define _IFS_H_
6 
7 /**
8  * DOC: In-Field Scan
9  *
10  * =============
11  * In-Field Scan
12  * =============
13  *
14  * Introduction
15  * ------------
16  *
17  * In Field Scan (IFS) is a hardware feature to run circuit level tests on
18  * a CPU core to detect problems that are not caught by parity or ECC checks.
19  * Future CPUs will support more than one type of test which will show up
20  * with a new platform-device instance-id, for now only .0 is exposed.
21  *
22  *
23  * IFS Image
24  * ---------
25  *
26  * Intel provides a firmware file containing the scan tests via
27  * github [#f1]_.  Similar to microcode there is a separate file for each
28  * family-model-stepping.
29  *
30  * IFS Image Loading
31  * -----------------
32  *
33  * The driver loads the tests into memory reserved BIOS local to each CPU
34  * socket in a two step process using writes to MSRs to first load the
35  * SHA hashes for the test. Then the tests themselves. Status MSRs provide
36  * feedback on the success/failure of these steps. When a new test file
37  * is installed it can be loaded by writing to the driver reload file::
38  *
39  *   # echo 1 > /sys/devices/virtual/misc/intel_ifs_0/reload
40  *
41  * Similar to microcode, the current version of the scan tests is stored
42  * in a fixed location: /lib/firmware/intel/ifs.0/family-model-stepping.scan
43  *
44  * Running tests
45  * -------------
46  *
47  * Tests are run by the driver synchronizing execution of all threads on a
48  * core and then writing to the ACTIVATE_SCAN MSR on all threads. Instruction
49  * execution continues when:
50  *
51  * 1) All tests have completed.
52  * 2) Execution was interrupted.
53  * 3) A test detected a problem.
54  *
55  * Note that ALL THREADS ON THE CORE ARE EFFECTIVELY OFFLINE FOR THE
56  * DURATION OF THE TEST. This can be up to 200 milliseconds. If the system
57  * is running latency sensitive applications that cannot tolerate an
58  * interruption of this magnitude, the system administrator must arrange
59  * to migrate those applications to other cores before running a core test.
60  * It may also be necessary to redirect interrupts to other CPUs.
61  *
62  * In all cases reading the SCAN_STATUS MSR provides details on what
63  * happened. The driver makes the value of this MSR visible to applications
64  * via the "details" file (see below). Interrupted tests may be restarted.
65  *
66  * The IFS driver provides sysfs interfaces via /sys/devices/virtual/misc/intel_ifs_0/
67  * to control execution:
68  *
69  * Test a specific core::
70  *
71  *   # echo <cpu#> > /sys/devices/virtual/misc/intel_ifs_0/run_test
72  *
73  * when HT is enabled any of the sibling cpu# can be specified to test
74  * its corresponding physical core. Since the tests are per physical core,
75  * the result of testing any thread is same. All siblings must be online
76  * to run a core test. It is only necessary to test one thread.
77  *
78  * For e.g. to test core corresponding to cpu5
79  *
80  *   # echo 5 > /sys/devices/virtual/misc/intel_ifs_0/run_test
81  *
82  * Results of the last test is provided in /sys::
83  *
84  *   $ cat /sys/devices/virtual/misc/intel_ifs_0/status
85  *   pass
86  *
87  * Status can be one of pass, fail, untested
88  *
89  * Additional details of the last test is provided by the details file::
90  *
91  *   $ cat /sys/devices/virtual/misc/intel_ifs_0/details
92  *   0x8081
93  *
94  * The details file reports the hex value of the SCAN_STATUS MSR.
95  * Hardware defined error codes are documented in volume 4 of the Intel
96  * Software Developer's Manual but the error_code field may contain one of
97  * the following driver defined software codes:
98  *
99  * +------+--------------------+
100  * | 0xFD | Software timeout   |
101  * +------+--------------------+
102  * | 0xFE | Partial completion |
103  * +------+--------------------+
104  *
105  * Driver design choices
106  * ---------------------
107  *
108  * 1) The ACTIVATE_SCAN MSR allows for running any consecutive subrange of
109  * available tests. But the driver always tries to run all tests and only
110  * uses the subrange feature to restart an interrupted test.
111  *
112  * 2) Hardware allows for some number of cores to be tested in parallel.
113  * The driver does not make use of this, it only tests one core at a time.
114  *
115  * .. [#f1] https://github.com/intel/TBD
116  */
117 #include <linux/device.h>
118 #include <linux/miscdevice.h>
119 
120 #define MSR_COPY_SCAN_HASHES			0x000002c2
121 #define MSR_SCAN_HASHES_STATUS			0x000002c3
122 #define MSR_AUTHENTICATE_AND_COPY_CHUNK		0x000002c4
123 #define MSR_CHUNKS_AUTHENTICATION_STATUS	0x000002c5
124 #define MSR_ACTIVATE_SCAN			0x000002c6
125 #define MSR_SCAN_STATUS				0x000002c7
126 #define SCAN_NOT_TESTED				0
127 #define SCAN_TEST_PASS				1
128 #define SCAN_TEST_FAIL				2
129 
130 /* MSR_SCAN_HASHES_STATUS bit fields */
131 union ifs_scan_hashes_status {
132 	u64	data;
133 	struct {
134 		u32	chunk_size	:16;
135 		u32	num_chunks	:8;
136 		u32	rsvd1		:8;
137 		u32	error_code	:8;
138 		u32	rsvd2		:11;
139 		u32	max_core_limit	:12;
140 		u32	valid		:1;
141 	};
142 };
143 
144 /* MSR_CHUNKS_AUTH_STATUS bit fields */
145 union ifs_chunks_auth_status {
146 	u64	data;
147 	struct {
148 		u32	valid_chunks	:8;
149 		u32	total_chunks	:8;
150 		u32	rsvd1		:16;
151 		u32	error_code	:8;
152 		u32	rsvd2		:24;
153 	};
154 };
155 
156 /* MSR_ACTIVATE_SCAN bit fields */
157 union ifs_scan {
158 	u64	data;
159 	struct {
160 		u32	start	:8;
161 		u32	stop	:8;
162 		u32	rsvd	:16;
163 		u32	delay	:31;
164 		u32	sigmce	:1;
165 	};
166 };
167 
168 /* MSR_SCAN_STATUS bit fields */
169 union ifs_status {
170 	u64	data;
171 	struct {
172 		u32	chunk_num		:8;
173 		u32	chunk_stop_index	:8;
174 		u32	rsvd1			:16;
175 		u32	error_code		:8;
176 		u32	rsvd2			:22;
177 		u32	control_error		:1;
178 		u32	signature_error		:1;
179 	};
180 };
181 
182 /*
183  * Driver populated error-codes
184  * 0xFD: Test timed out before completing all the chunks.
185  * 0xFE: not all scan chunks were executed. Maximum forward progress retries exceeded.
186  */
187 #define IFS_SW_TIMEOUT				0xFD
188 #define IFS_SW_PARTIAL_COMPLETION		0xFE
189 
190 /**
191  * struct ifs_data - attributes related to intel IFS driver
192  * @integrity_cap_bit: MSR_INTEGRITY_CAPS bit enumerating this test
193  * @loaded_version: stores the currently loaded ifs image version.
194  * @loaded: If a valid test binary has been loaded into the memory
195  * @loading_error: Error occurred on another CPU while loading image
196  * @valid_chunks: number of chunks which could be validated.
197  * @status: it holds simple status pass/fail/untested
198  * @scan_details: opaque scan status code from h/w
199  */
200 struct ifs_data {
201 	int	integrity_cap_bit;
202 	int	loaded_version;
203 	bool	loaded;
204 	bool	loading_error;
205 	int	valid_chunks;
206 	int	status;
207 	u64	scan_details;
208 };
209 
210 struct ifs_work {
211 	struct work_struct w;
212 	struct device *dev;
213 };
214 
215 struct ifs_device {
216 	struct ifs_data data;
217 	struct miscdevice misc;
218 };
219 
ifs_get_data(struct device * dev)220 static inline struct ifs_data *ifs_get_data(struct device *dev)
221 {
222 	struct miscdevice *m = dev_get_drvdata(dev);
223 	struct ifs_device *d = container_of(m, struct ifs_device, misc);
224 
225 	return &d->data;
226 }
227 
228 void ifs_load_firmware(struct device *dev);
229 int do_core_test(int cpu, struct device *dev);
230 const struct attribute_group **ifs_get_groups(void);
231 
232 extern struct semaphore ifs_sem;
233 
234 #endif
235