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.
21 *
22 *
23 * IFS Image
24 * ---------
25 *
26 * Intel provides firmware files containing the scan tests via the webpage [#f1]_.
27 * Look under "In-Field Scan Test Images Download" section towards the
28 * end of the page. Similar to microcode, there are separate files for each
29 * family-model-stepping. IFS Images are not applicable for some test types.
30 * Wherever applicable the sysfs directory would provide a "current_batch" file
31 * (see below) for loading the image.
32 *
33 * .. [#f1] https://intel.com/InFieldScan
34 *
35 * IFS Image Loading
36 * -----------------
37 *
38 * The driver loads the tests into memory reserved BIOS local to each CPU
39 * socket in a two step process using writes to MSRs to first load the
40 * SHA hashes for the test. Then the tests themselves. Status MSRs provide
41 * feedback on the success/failure of these steps.
42 *
43 * The test files are kept in a fixed location: /lib/firmware/intel/ifs_<n>/
44 * For e.g if there are 3 test files, they would be named in the following
45 * fashion:
46 * ff-mm-ss-01.scan
47 * ff-mm-ss-02.scan
48 * ff-mm-ss-03.scan
49 * (where ff refers to family, mm indicates model and ss indicates stepping)
50 *
51 * A different test file can be loaded by writing the numerical portion
52 * (e.g 1, 2 or 3 in the above scenario) into the curent_batch file.
53 * To load ff-mm-ss-02.scan, the following command can be used::
54 *
55 * # echo 2 > /sys/devices/virtual/misc/intel_ifs_<n>/current_batch
56 *
57 * The above file can also be read to know the currently loaded image.
58 *
59 * Running tests
60 * -------------
61 *
62 * Tests are run by the driver synchronizing execution of all threads on a
63 * core and then writing to the ACTIVATE_SCAN MSR on all threads. Instruction
64 * execution continues when:
65 *
66 * 1) All tests have completed.
67 * 2) Execution was interrupted.
68 * 3) A test detected a problem.
69 *
70 * Note that ALL THREADS ON THE CORE ARE EFFECTIVELY OFFLINE FOR THE
71 * DURATION OF THE TEST. This can be up to 200 milliseconds. If the system
72 * is running latency sensitive applications that cannot tolerate an
73 * interruption of this magnitude, the system administrator must arrange
74 * to migrate those applications to other cores before running a core test.
75 * It may also be necessary to redirect interrupts to other CPUs.
76 *
77 * In all cases reading the corresponding test's STATUS MSR provides details on what
78 * happened. The driver makes the value of this MSR visible to applications
79 * via the "details" file (see below). Interrupted tests may be restarted.
80 *
81 * The IFS driver provides sysfs interfaces via /sys/devices/virtual/misc/intel_ifs_<n>/
82 * to control execution:
83 *
84 * Test a specific core::
85 *
86 * # echo <cpu#> > /sys/devices/virtual/misc/intel_ifs_<n>/run_test
87 *
88 * when HT is enabled any of the sibling cpu# can be specified to test
89 * its corresponding physical core. Since the tests are per physical core,
90 * the result of testing any thread is same. All siblings must be online
91 * to run a core test. It is only necessary to test one thread.
92 *
93 * For e.g. to test core corresponding to cpu5
94 *
95 * # echo 5 > /sys/devices/virtual/misc/intel_ifs_<n>/run_test
96 *
97 * Results of the last test is provided in /sys::
98 *
99 * $ cat /sys/devices/virtual/misc/intel_ifs_<n>/status
100 * pass
101 *
102 * Status can be one of pass, fail, untested
103 *
104 * Additional details of the last test is provided by the details file::
105 *
106 * $ cat /sys/devices/virtual/misc/intel_ifs_<n>/details
107 * 0x8081
108 *
109 * The details file reports the hex value of the test specific status MSR.
110 * Hardware defined error codes are documented in volume 4 of the Intel
111 * Software Developer's Manual but the error_code field may contain one of
112 * the following driver defined software codes:
113 *
114 * +------+--------------------+
115 * | 0xFD | Software timeout |
116 * +------+--------------------+
117 * | 0xFE | Partial completion |
118 * +------+--------------------+
119 *
120 * Driver design choices
121 * ---------------------
122 *
123 * 1) The ACTIVATE_SCAN MSR allows for running any consecutive subrange of
124 * available tests. But the driver always tries to run all tests and only
125 * uses the subrange feature to restart an interrupted test.
126 *
127 * 2) Hardware allows for some number of cores to be tested in parallel.
128 * The driver does not make use of this, it only tests one core at a time.
129 *
130 * Structural Based Functional Test at Field (SBAF):
131 * -------------------------------------------------
132 *
133 * SBAF is a new type of testing that provides comprehensive core test
134 * coverage complementing Scan at Field (SAF) testing. SBAF mimics the
135 * manufacturing screening environment and leverages the same test suite.
136 * It makes use of Design For Test (DFT) observation sites and features
137 * to maximize coverage in minimum time.
138 *
139 * Similar to the SAF test, SBAF isolates the core under test from the
140 * rest of the system during execution. Upon completion, the core
141 * seamlessly resets to its pre-test state and resumes normal operation.
142 * Any machine checks or hangs encountered during the test are confined to
143 * the isolated core, preventing disruption to the overall system.
144 *
145 * Like the SAF test, the SBAF test is also divided into multiple batches,
146 * and each batch test can take hundreds of milliseconds (100-200 ms) to
147 * complete. If such a lengthy interruption is undesirable, it is
148 * recommended to relocate the time-sensitive applications to other cores.
149 */
150 #include <linux/device.h>
151 #include <linux/miscdevice.h>
152
153 #define MSR_ARRAY_BIST 0x00000105
154
155 #define MSR_COPY_SBAF_HASHES 0x000002b8
156 #define MSR_SBAF_HASHES_STATUS 0x000002b9
157 #define MSR_AUTHENTICATE_AND_COPY_SBAF_CHUNK 0x000002ba
158 #define MSR_SBAF_CHUNKS_AUTHENTICATION_STATUS 0x000002bb
159 #define MSR_ACTIVATE_SBAF 0x000002bc
160 #define MSR_SBAF_STATUS 0x000002bd
161
162 #define MSR_COPY_SCAN_HASHES 0x000002c2
163 #define MSR_SCAN_HASHES_STATUS 0x000002c3
164 #define MSR_AUTHENTICATE_AND_COPY_CHUNK 0x000002c4
165 #define MSR_CHUNKS_AUTHENTICATION_STATUS 0x000002c5
166 #define MSR_ACTIVATE_SCAN 0x000002c6
167 #define MSR_SCAN_STATUS 0x000002c7
168 #define MSR_ARRAY_TRIGGER 0x000002d6
169 #define MSR_ARRAY_STATUS 0x000002d7
170 #define MSR_SAF_CTRL 0x000004f0
171 #define MSR_SBAF_CTRL 0x000004f8
172
173 #define SCAN_NOT_TESTED 0
174 #define SCAN_TEST_PASS 1
175 #define SCAN_TEST_FAIL 2
176
177 #define IFS_TYPE_SAF 0
178 #define IFS_TYPE_ARRAY_BIST 1
179 #define IFS_TYPE_SBAF 2
180
181 #define ARRAY_GEN0 0
182 #define ARRAY_GEN1 1
183
184 /* MSR_SCAN_HASHES_STATUS bit fields */
185 union ifs_scan_hashes_status {
186 u64 data;
187 struct {
188 u32 chunk_size :16;
189 u32 num_chunks :8;
190 u32 rsvd1 :8;
191 u32 error_code :8;
192 u32 rsvd2 :11;
193 u32 max_core_limit :12;
194 u32 valid :1;
195 };
196 };
197
198 union ifs_scan_hashes_status_gen2 {
199 u64 data;
200 struct {
201 u16 chunk_size;
202 u16 num_chunks;
203 u32 error_code :8;
204 u32 chunks_in_stride :9;
205 u32 rsvd :2;
206 u32 max_core_limit :12;
207 u32 valid :1;
208 };
209 };
210
211 /* MSR_CHUNKS_AUTH_STATUS bit fields */
212 union ifs_chunks_auth_status {
213 u64 data;
214 struct {
215 u32 valid_chunks :8;
216 u32 total_chunks :8;
217 u32 rsvd1 :16;
218 u32 error_code :8;
219 u32 rsvd2 :24;
220 };
221 };
222
223 union ifs_chunks_auth_status_gen2 {
224 u64 data;
225 struct {
226 u16 valid_chunks;
227 u16 total_chunks;
228 u32 error_code :8;
229 u32 rsvd2 :8;
230 u32 max_bundle :16;
231 };
232 };
233
234 /* MSR_ACTIVATE_SCAN bit fields */
235 union ifs_scan {
236 u64 data;
237 struct {
238 union {
239 struct {
240 u8 start;
241 u8 stop;
242 u16 rsvd;
243 } gen0;
244 struct {
245 u16 start;
246 u16 stop;
247 } gen2;
248 };
249 u32 delay :31;
250 u32 sigmce :1;
251 };
252 };
253
254 /* MSR_SCAN_STATUS bit fields */
255 union ifs_status {
256 u64 data;
257 struct {
258 union {
259 struct {
260 u8 chunk_num;
261 u8 chunk_stop_index;
262 u16 rsvd1;
263 } gen0;
264 struct {
265 u16 chunk_num;
266 u16 chunk_stop_index;
267 } gen2;
268 };
269 u32 error_code :8;
270 u32 rsvd2 :22;
271 u32 control_error :1;
272 u32 signature_error :1;
273 };
274 };
275
276 /* MSR_ARRAY_BIST bit fields */
277 union ifs_array {
278 u64 data;
279 struct {
280 u32 array_bitmask;
281 u16 array_bank;
282 u16 rsvd :15;
283 u16 ctrl_result :1;
284 };
285 };
286
287 /* MSR_ACTIVATE_SBAF bit fields */
288 union ifs_sbaf {
289 u64 data;
290 struct {
291 u32 bundle_idx :9;
292 u32 rsvd1 :5;
293 u32 pgm_idx :2;
294 u32 rsvd2 :16;
295 u32 delay :31;
296 u32 sigmce :1;
297 };
298 };
299
300 /* MSR_SBAF_STATUS bit fields */
301 union ifs_sbaf_status {
302 u64 data;
303 struct {
304 u32 bundle_idx :9;
305 u32 rsvd1 :5;
306 u32 pgm_idx :2;
307 u32 rsvd2 :16;
308 u32 error_code :8;
309 u32 rsvd3 :21;
310 u32 test_fail :1;
311 u32 sbaf_status :2;
312 };
313 };
314
315 /*
316 * Driver populated error-codes
317 * 0xFD: Test timed out before completing all the chunks.
318 * 0xFE: not all scan chunks were executed. Maximum forward progress retries exceeded.
319 */
320 #define IFS_SW_TIMEOUT 0xFD
321 #define IFS_SW_PARTIAL_COMPLETION 0xFE
322
323 #define IFS_SUFFIX_SZ 5
324
325 struct ifs_test_caps {
326 int integrity_cap_bit;
327 int test_num;
328 char image_suffix[IFS_SUFFIX_SZ];
329 };
330
331 /**
332 * struct ifs_test_msrs - MSRs used in IFS tests
333 * @copy_hashes: Copy test hash data
334 * @copy_hashes_status: Status of copied test hash data
335 * @copy_chunks: Copy chunks of the test data
336 * @copy_chunks_status: Status of the copied test data chunks
337 * @test_ctrl: Control the test attributes
338 */
339 struct ifs_test_msrs {
340 u32 copy_hashes;
341 u32 copy_hashes_status;
342 u32 copy_chunks;
343 u32 copy_chunks_status;
344 u32 test_ctrl;
345 };
346
347 /**
348 * struct ifs_data - attributes related to intel IFS driver
349 * @loaded_version: stores the currently loaded ifs image version.
350 * @loaded: If a valid test binary has been loaded into the memory
351 * @loading_error: Error occurred on another CPU while loading image
352 * @valid_chunks: number of chunks which could be validated.
353 * @status: it holds simple status pass/fail/untested
354 * @scan_details: opaque scan status code from h/w
355 * @cur_batch: number indicating the currently loaded test file
356 * @generation: IFS test generation enumerated by hardware
357 * @chunk_size: size of a test chunk
358 * @array_gen: test generation of array test
359 * @max_bundle: maximum bundle index
360 */
361 struct ifs_data {
362 int loaded_version;
363 bool loaded;
364 bool loading_error;
365 int valid_chunks;
366 int status;
367 u64 scan_details;
368 u32 cur_batch;
369 u32 generation;
370 u32 chunk_size;
371 u32 array_gen;
372 u32 max_bundle;
373 };
374
375 struct ifs_work {
376 struct work_struct w;
377 struct device *dev;
378 };
379
380 struct ifs_device {
381 const struct ifs_test_caps *test_caps;
382 const struct ifs_test_msrs *test_msrs;
383 struct ifs_data rw_data;
384 struct miscdevice misc;
385 };
386
ifs_get_data(struct device * dev)387 static inline struct ifs_data *ifs_get_data(struct device *dev)
388 {
389 struct miscdevice *m = dev_get_drvdata(dev);
390 struct ifs_device *d = container_of(m, struct ifs_device, misc);
391
392 return &d->rw_data;
393 }
394
ifs_get_test_caps(struct device * dev)395 static inline const struct ifs_test_caps *ifs_get_test_caps(struct device *dev)
396 {
397 struct miscdevice *m = dev_get_drvdata(dev);
398 struct ifs_device *d = container_of(m, struct ifs_device, misc);
399
400 return d->test_caps;
401 }
402
ifs_get_test_msrs(struct device * dev)403 static inline const struct ifs_test_msrs *ifs_get_test_msrs(struct device *dev)
404 {
405 struct miscdevice *m = dev_get_drvdata(dev);
406 struct ifs_device *d = container_of(m, struct ifs_device, misc);
407
408 return d->test_msrs;
409 }
410
411 extern bool *ifs_pkg_auth;
412 int ifs_load_firmware(struct device *dev);
413 int do_core_test(int cpu, struct device *dev);
414 extern struct attribute *plat_ifs_attrs[];
415 extern struct attribute *plat_ifs_array_attrs[];
416
417 #endif
418