1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation. */
5 #include <linux/delay.h>
8 #include <linux/slab.h>
9 #include <linux/stop_machine.h>
14 * Note all code and data in this file is protected by
15 * ifs_sem. On HT systems all threads on a core will
16 * execute together, but only the first thread on the
17 * core will update results of the test.
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/intel_ifs.h>
23 /* Max retries on the same chunk */
24 #define MAX_IFS_RETRIES 5
27 * Number of TSC cycles that a logical CPU will wait for the other
28 * logical CPU on the core in the WRMSR(ACTIVATE_SCAN).
30 #define IFS_THREAD_WAIT 100000
32 enum ifs_status_err_code {
34 IFS_OTHER_THREAD_COULD_NOT_JOIN = 1,
35 IFS_INTERRUPTED_BEFORE_RENDEZVOUS = 2,
36 IFS_POWER_MGMT_INADEQUATE_FOR_SCAN = 3,
37 IFS_INVALID_CHUNK_RANGE = 4,
38 IFS_MISMATCH_ARGUMENTS_BETWEEN_THREADS = 5,
39 IFS_CORE_NOT_CAPABLE_CURRENTLY = 6,
40 IFS_UNASSIGNED_ERROR_CODE = 7,
41 IFS_EXCEED_NUMBER_OF_THREADS_CONCURRENT = 8,
42 IFS_INTERRUPTED_DURING_EXECUTION = 9,
43 IFS_UNASSIGNED_ERROR_CODE_0xA = 0xA,
44 IFS_CORRUPTED_CHUNK = 0xB,
47 static const char * const scan_test_status[] = {
48 [IFS_NO_ERROR] = "SCAN no error",
49 [IFS_OTHER_THREAD_COULD_NOT_JOIN] = "Other thread could not join.",
50 [IFS_INTERRUPTED_BEFORE_RENDEZVOUS] = "Interrupt occurred prior to SCAN coordination.",
51 [IFS_POWER_MGMT_INADEQUATE_FOR_SCAN] =
52 "Core Abort SCAN Response due to power management condition.",
53 [IFS_INVALID_CHUNK_RANGE] = "Non valid chunks in the range",
54 [IFS_MISMATCH_ARGUMENTS_BETWEEN_THREADS] = "Mismatch in arguments between threads T0/T1.",
55 [IFS_CORE_NOT_CAPABLE_CURRENTLY] = "Core not capable of performing SCAN currently",
56 [IFS_UNASSIGNED_ERROR_CODE] = "Unassigned error code 0x7",
57 [IFS_EXCEED_NUMBER_OF_THREADS_CONCURRENT] =
58 "Exceeded number of Logical Processors (LP) allowed to run Scan-At-Field concurrently",
59 [IFS_INTERRUPTED_DURING_EXECUTION] = "Interrupt occurred prior to SCAN start",
60 [IFS_UNASSIGNED_ERROR_CODE_0xA] = "Unassigned error code 0xA",
61 [IFS_CORRUPTED_CHUNK] = "Scan operation aborted due to corrupted image. Try reloading",
64 static void message_not_tested(struct device *dev, int cpu, union ifs_status status)
66 if (status.error_code < ARRAY_SIZE(scan_test_status)) {
67 dev_info(dev, "CPU(s) %*pbl: SCAN operation did not start. %s\n",
68 cpumask_pr_args(cpu_smt_mask(cpu)),
69 scan_test_status[status.error_code]);
70 } else if (status.error_code == IFS_SW_TIMEOUT) {
71 dev_info(dev, "CPU(s) %*pbl: software timeout during scan\n",
72 cpumask_pr_args(cpu_smt_mask(cpu)));
73 } else if (status.error_code == IFS_SW_PARTIAL_COMPLETION) {
74 dev_info(dev, "CPU(s) %*pbl: %s\n",
75 cpumask_pr_args(cpu_smt_mask(cpu)),
76 "Not all scan chunks were executed. Maximum forward progress retries exceeded");
78 dev_info(dev, "CPU(s) %*pbl: SCAN unknown status %llx\n",
79 cpumask_pr_args(cpu_smt_mask(cpu)), status.data);
83 static void message_fail(struct device *dev, int cpu, union ifs_status status)
85 struct ifs_data *ifsd = ifs_get_data(dev);
88 * control_error is set when the microcode runs into a problem
89 * loading the image from the reserved BIOS memory, or it has
90 * been corrupted. Reloading the image may fix this issue.
92 if (status.control_error) {
93 dev_err(dev, "CPU(s) %*pbl: could not execute from loaded scan image. Batch: %02x version: 0x%x\n",
94 cpumask_pr_args(cpu_smt_mask(cpu)), ifsd->cur_batch, ifsd->loaded_version);
98 * signature_error is set when the output from the scan chains does not
99 * match the expected signature. This might be a transient problem (e.g.
100 * due to a bit flip from an alpha particle or neutron). If the problem
101 * repeats on a subsequent test, then it indicates an actual problem in
102 * the core being tested.
104 if (status.signature_error) {
105 dev_err(dev, "CPU(s) %*pbl: test signature incorrect. Batch: %02x version: 0x%x\n",
106 cpumask_pr_args(cpu_smt_mask(cpu)), ifsd->cur_batch, ifsd->loaded_version);
110 static bool can_restart(union ifs_status status)
112 enum ifs_status_err_code err_code = status.error_code;
114 /* Signature for chunk is bad, or scan test failed */
115 if (status.signature_error || status.control_error)
120 case IFS_OTHER_THREAD_COULD_NOT_JOIN:
121 case IFS_INTERRUPTED_BEFORE_RENDEZVOUS:
122 case IFS_POWER_MGMT_INADEQUATE_FOR_SCAN:
123 case IFS_EXCEED_NUMBER_OF_THREADS_CONCURRENT:
124 case IFS_INTERRUPTED_DURING_EXECUTION:
126 case IFS_INVALID_CHUNK_RANGE:
127 case IFS_MISMATCH_ARGUMENTS_BETWEEN_THREADS:
128 case IFS_CORE_NOT_CAPABLE_CURRENTLY:
129 case IFS_UNASSIGNED_ERROR_CODE:
130 case IFS_UNASSIGNED_ERROR_CODE_0xA:
131 case IFS_CORRUPTED_CHUNK:
138 * Execute the scan. Called "simultaneously" on all threads of a core
139 * at high priority using the stop_cpus mechanism.
141 static int doscan(void *data)
143 int cpu = smp_processor_id();
147 /* Only the first logical CPU on a core reports result */
148 first = cpumask_first(cpu_smt_mask(cpu));
151 * This WRMSR will wait for other HT threads to also write
152 * to this MSR (at most for activate.delay cycles). Then it
153 * starts scan of each requested chunk. The core scan happens
154 * during the "execution" of the WRMSR. This instruction can
155 * take up to 200 milliseconds (in the case where all chunks
156 * are processed in a single pass) before it retires.
158 wrmsrl(MSR_ACTIVATE_SCAN, msrs[0]);
161 /* Pass back the result of the scan */
162 rdmsrl(MSR_SCAN_STATUS, msrs[1]);
169 * Use stop_core_cpuslocked() to synchronize writing to MSR_ACTIVATE_SCAN
170 * on all threads of the core to be tested. Loop if necessary to complete
171 * run of all chunks. Include some defensive tests to make sure forward
172 * progress is made, and that the whole test completes in a reasonable time.
174 static void ifs_test_core(int cpu, struct device *dev)
176 union ifs_scan activate;
177 union ifs_status status;
178 unsigned long timeout;
179 struct ifs_data *ifsd;
180 int to_start, to_stop;
185 ifsd = ifs_get_data(dev);
187 activate.gen0.rsvd = 0;
188 activate.delay = IFS_THREAD_WAIT;
191 to_stop = ifsd->valid_chunks - 1;
193 if (ifsd->generation) {
194 activate.gen2.start = to_start;
195 activate.gen2.stop = to_stop;
197 activate.gen0.start = to_start;
198 activate.gen0.stop = to_stop;
201 timeout = jiffies + HZ / 2;
202 retries = MAX_IFS_RETRIES;
204 while (to_start <= to_stop) {
205 if (time_after(jiffies, timeout)) {
206 status.error_code = IFS_SW_TIMEOUT;
210 msrvals[0] = activate.data;
211 stop_core_cpuslocked(cpu, doscan, msrvals);
213 status.data = msrvals[1];
215 trace_ifs_status(cpu, to_start, to_stop, status.data);
217 /* Some cases can be retried, give up for others */
218 if (!can_restart(status))
221 status_chunk = ifsd->generation ? status.gen2.chunk_num : status.gen0.chunk_num;
222 if (status_chunk == to_start) {
223 /* Check for forward progress */
224 if (--retries == 0) {
225 if (status.error_code == IFS_NO_ERROR)
226 status.error_code = IFS_SW_PARTIAL_COMPLETION;
230 retries = MAX_IFS_RETRIES;
231 if (ifsd->generation)
232 activate.gen2.start = status_chunk;
234 activate.gen0.start = status_chunk;
235 to_start = status_chunk;
239 /* Update status for this core */
240 ifsd->scan_details = status.data;
242 if (status.control_error || status.signature_error) {
243 ifsd->status = SCAN_TEST_FAIL;
244 message_fail(dev, cpu, status);
245 } else if (status.error_code) {
246 ifsd->status = SCAN_NOT_TESTED;
247 message_not_tested(dev, cpu, status);
249 ifsd->status = SCAN_TEST_PASS;
253 #define SPINUNIT 100 /* 100 nsec */
254 static atomic_t array_cpus_out;
257 * Simplified cpu sibling rendezvous loop based on microcode loader __wait_for_cpus()
259 static void wait_for_sibling_cpu(atomic_t *t, long long timeout)
261 int cpu = smp_processor_id();
262 const struct cpumask *smt_mask = cpu_smt_mask(cpu);
263 int all_cpus = cpumask_weight(smt_mask);
266 while (atomic_read(t) < all_cpus) {
267 if (timeout < SPINUNIT)
271 touch_nmi_watchdog();
275 static int do_array_test(void *data)
277 union ifs_array *command = data;
278 int cpu = smp_processor_id();
282 * Only one logical CPU on a core needs to trigger the Array test via MSR write.
284 first = cpumask_first(cpu_smt_mask(cpu));
287 wrmsrl(MSR_ARRAY_BIST, command->data);
288 /* Pass back the result of the test */
289 rdmsrl(MSR_ARRAY_BIST, command->data);
292 /* Tests complete faster if the sibling is spinning here */
293 wait_for_sibling_cpu(&array_cpus_out, NSEC_PER_SEC);
298 static void ifs_array_test_core(int cpu, struct device *dev)
300 union ifs_array command = {};
301 bool timed_out = false;
302 struct ifs_data *ifsd;
303 unsigned long timeout;
305 ifsd = ifs_get_data(dev);
307 command.array_bitmask = ~0U;
308 timeout = jiffies + HZ / 2;
311 if (time_after(jiffies, timeout)) {
315 atomic_set(&array_cpus_out, 0);
316 stop_core_cpuslocked(cpu, do_array_test, &command);
318 if (command.ctrl_result)
320 } while (command.array_bitmask);
322 ifsd->scan_details = command.data;
324 if (command.ctrl_result)
325 ifsd->status = SCAN_TEST_FAIL;
326 else if (timed_out || command.array_bitmask)
327 ifsd->status = SCAN_NOT_TESTED;
329 ifsd->status = SCAN_TEST_PASS;
332 #define ARRAY_GEN1_TEST_ALL_ARRAYS 0x0ULL
333 #define ARRAY_GEN1_STATUS_FAIL 0x1ULL
335 static int do_array_test_gen1(void *status)
337 int cpu = smp_processor_id();
340 first = cpumask_first(cpu_smt_mask(cpu));
343 wrmsrl(MSR_ARRAY_TRIGGER, ARRAY_GEN1_TEST_ALL_ARRAYS);
344 rdmsrl(MSR_ARRAY_STATUS, *((u64 *)status));
350 static void ifs_array_test_gen1(int cpu, struct device *dev)
352 struct ifs_data *ifsd = ifs_get_data(dev);
355 stop_core_cpuslocked(cpu, do_array_test_gen1, &status);
356 ifsd->scan_details = status;
358 if (status & ARRAY_GEN1_STATUS_FAIL)
359 ifsd->status = SCAN_TEST_FAIL;
361 ifsd->status = SCAN_TEST_PASS;
365 * Initiate per core test. It wakes up work queue threads on the target cpu and
366 * its sibling cpu. Once all sibling threads wake up, the scan test gets executed and
367 * wait for all sibling threads to finish the scan test.
369 int do_core_test(int cpu, struct device *dev)
371 const struct ifs_test_caps *test = ifs_get_test_caps(dev);
372 struct ifs_data *ifsd = ifs_get_data(dev);
375 /* Prevent CPUs from being taken offline during the scan test */
378 if (!cpu_online(cpu)) {
379 dev_info(dev, "cannot test on the offline cpu %d\n", cpu);
384 switch (test->test_num) {
389 ifs_test_core(cpu, dev);
391 case IFS_TYPE_ARRAY_BIST:
392 if (ifsd->array_gen == ARRAY_GEN0)
393 ifs_array_test_core(cpu, dev);
395 ifs_array_test_gen1(cpu, dev);