blob: 559c1173de1c99177ba702c60d27993826510e9c [file] [log] [blame]
Huang Yinge4021342010-05-18 14:35:14 +08001/*
2 * APEI Error INJection support
3 *
4 * EINJ provides a hardware error injection mechanism, this is useful
5 * for debugging and testing of other APEI and RAS features.
6 *
7 * For more information about EINJ, please refer to ACPI Specification
8 * version 4.0, section 17.5.
9 *
Huang Ying6e320ec2010-05-18 14:35:24 +080010 * Copyright 2009-2010 Intel Corp.
Huang Yinge4021342010-05-18 14:35:14 +080011 * Author: Huang Ying <ying.huang@intel.com>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License version
15 * 2 as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Huang Yinge4021342010-05-18 14:35:14 +080021 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/io.h>
27#include <linux/debugfs.h>
28#include <linux/seq_file.h>
29#include <linux/nmi.h>
30#include <linux/delay.h>
Chen Gongc5a13032013-06-06 15:20:51 -070031#include <linux/mm.h>
Chen, Gongd3ab3ed2013-12-18 01:30:49 -050032#include <asm/unaligned.h>
Huang Yinge4021342010-05-18 14:35:14 +080033
34#include "apei-internal.h"
35
36#define EINJ_PFX "EINJ: "
37
38#define SPIN_UNIT 100 /* 100ns */
Stefan Weile8a8b252011-01-02 15:12:42 +010039/* Firmware should respond within 1 milliseconds */
Huang Yinge4021342010-05-18 14:35:14 +080040#define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
Chen Gongc5a13032013-06-06 15:20:51 -070041#define ACPI5_VENDOR_BIT BIT(31)
42#define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \
43 ACPI_EINJ_MEMORY_UNCORRECTABLE | \
44 ACPI_EINJ_MEMORY_FATAL)
Huang Yinge4021342010-05-18 14:35:14 +080045
Huang Ying6e320ec2010-05-18 14:35:24 +080046/*
Tony Luckc130bd62012-01-17 12:10:16 -080047 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
48 */
49static int acpi5;
50
51struct set_error_type_with_address {
52 u32 type;
53 u32 vendor_extension;
54 u32 flags;
55 u32 apicid;
56 u64 memory_address;
57 u64 memory_address_range;
58 u32 pcie_sbdf;
59};
60enum {
61 SETWA_FLAGS_APICID = 1,
62 SETWA_FLAGS_MEM = 2,
63 SETWA_FLAGS_PCIE_SBDF = 4,
64};
65
66/*
67 * Vendor extensions for platform specific operations
68 */
69struct vendor_error_type_extension {
70 u32 length;
71 u32 pcie_sbdf;
72 u16 vendor_id;
73 u16 device_id;
74 u8 rev_id;
75 u8 reserved[3];
76};
77
Chen Gongee490892012-03-15 16:53:36 +080078static u32 notrigger;
79
Tony Luckc130bd62012-01-17 12:10:16 -080080static u32 vendor_flags;
81static struct debugfs_blob_wrapper vendor_blob;
82static char vendor_dev[64];
83
84/*
Huang Ying6e320ec2010-05-18 14:35:24 +080085 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
86 * EINJ table through an unpublished extension. Use with caution as
87 * most will ignore the parameter and make their own choice of address
Huang Yingc3e60882011-07-20 16:09:29 +080088 * for error injection. This extension is used only if
89 * param_extension module parameter is specified.
Huang Ying6e320ec2010-05-18 14:35:24 +080090 */
91struct einj_parameter {
92 u64 type;
93 u64 reserved1;
94 u64 reserved2;
95 u64 param1;
96 u64 param2;
97};
98
Huang Yinge4021342010-05-18 14:35:14 +080099#define EINJ_OP_BUSY 0x1
100#define EINJ_STATUS_SUCCESS 0x0
101#define EINJ_STATUS_FAIL 0x1
102#define EINJ_STATUS_INVAL 0x2
103
104#define EINJ_TAB_ENTRY(tab) \
105 ((struct acpi_whea_header *)((char *)(tab) + \
106 sizeof(struct acpi_table_einj)))
107
Huang Yingc3e60882011-07-20 16:09:29 +0800108static bool param_extension;
109module_param(param_extension, bool, 0);
110
Huang Yinge4021342010-05-18 14:35:14 +0800111static struct acpi_table_einj *einj_tab;
112
113static struct apei_resources einj_resources;
114
115static struct apei_exec_ins_type einj_ins_type[] = {
116 [ACPI_EINJ_READ_REGISTER] = {
117 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
118 .run = apei_exec_read_register,
119 },
120 [ACPI_EINJ_READ_REGISTER_VALUE] = {
121 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
122 .run = apei_exec_read_register_value,
123 },
124 [ACPI_EINJ_WRITE_REGISTER] = {
125 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
126 .run = apei_exec_write_register,
127 },
128 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
129 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
130 .run = apei_exec_write_register_value,
131 },
132 [ACPI_EINJ_NOOP] = {
133 .flags = 0,
134 .run = apei_exec_noop,
135 },
136};
137
138/*
139 * Prevent EINJ interpreter to run simultaneously, because the
140 * corresponding firmware implementation may not work properly when
141 * invoked simultaneously.
142 */
143static DEFINE_MUTEX(einj_mutex);
144
Tony Luckc130bd62012-01-17 12:10:16 -0800145static void *einj_param;
146
Huang Yinge4021342010-05-18 14:35:14 +0800147static void einj_exec_ctx_init(struct apei_exec_context *ctx)
148{
149 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
150 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
151}
152
153static int __einj_get_available_error_type(u32 *type)
154{
155 struct apei_exec_context ctx;
156 int rc;
157
158 einj_exec_ctx_init(&ctx);
159 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
160 if (rc)
161 return rc;
162 *type = apei_exec_ctx_get_output(&ctx);
163
164 return 0;
165}
166
167/* Get error injection capabilities of the platform */
168static int einj_get_available_error_type(u32 *type)
169{
170 int rc;
171
172 mutex_lock(&einj_mutex);
173 rc = __einj_get_available_error_type(type);
174 mutex_unlock(&einj_mutex);
175
176 return rc;
177}
178
179static int einj_timedout(u64 *t)
180{
181 if ((s64)*t < SPIN_UNIT) {
182 pr_warning(FW_WARN EINJ_PFX
183 "Firmware does not respond in time\n");
184 return 1;
185 }
186 *t -= SPIN_UNIT;
187 ndelay(SPIN_UNIT);
188 touch_nmi_watchdog();
189 return 0;
190}
191
Tony Luckc130bd62012-01-17 12:10:16 -0800192static void check_vendor_extension(u64 paddr,
193 struct set_error_type_with_address *v5param)
194{
Luck, Tony459413d2012-01-23 15:27:56 -0800195 int offset = v5param->vendor_extension;
Tony Luckc130bd62012-01-17 12:10:16 -0800196 struct vendor_error_type_extension *v;
197 u32 sbdf;
198
199 if (!offset)
200 return;
Lv Zhenga2383172014-05-20 15:39:41 +0800201 v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
Tony Luckc130bd62012-01-17 12:10:16 -0800202 if (!v)
203 return;
Luck, Tony459413d2012-01-23 15:27:56 -0800204 sbdf = v->pcie_sbdf;
Tony Luckc130bd62012-01-17 12:10:16 -0800205 sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
206 sbdf >> 24, (sbdf >> 16) & 0xff,
207 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
Luck, Tony459413d2012-01-23 15:27:56 -0800208 v->vendor_id, v->device_id, v->rev_id);
Lv Zhenga2383172014-05-20 15:39:41 +0800209 acpi_os_unmap_iomem(v, sizeof(*v));
Tony Luckc130bd62012-01-17 12:10:16 -0800210}
211
212static void *einj_get_parameter_address(void)
Huang Ying6e320ec2010-05-18 14:35:24 +0800213{
214 int i;
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500215 u64 pa_v4 = 0, pa_v5 = 0;
Huang Ying6e320ec2010-05-18 14:35:24 +0800216 struct acpi_whea_header *entry;
217
218 entry = EINJ_TAB_ENTRY(einj_tab);
219 for (i = 0; i < einj_tab->entries; i++) {
220 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
221 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
222 entry->register_region.space_id ==
223 ACPI_ADR_SPACE_SYSTEM_MEMORY)
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500224 pa_v4 = get_unaligned(&entry->register_region.address);
Tony Luckc130bd62012-01-17 12:10:16 -0800225 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
226 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
227 entry->register_region.space_id ==
228 ACPI_ADR_SPACE_SYSTEM_MEMORY)
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500229 pa_v5 = get_unaligned(&entry->register_region.address);
Huang Ying6e320ec2010-05-18 14:35:24 +0800230 entry++;
231 }
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500232 if (pa_v5) {
Tony Luckc130bd62012-01-17 12:10:16 -0800233 struct set_error_type_with_address *v5param;
Huang Ying6e320ec2010-05-18 14:35:24 +0800234
Lv Zhenga2383172014-05-20 15:39:41 +0800235 v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
Tony Luckc130bd62012-01-17 12:10:16 -0800236 if (v5param) {
237 acpi5 = 1;
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500238 check_vendor_extension(pa_v5, v5param);
Tony Luckc130bd62012-01-17 12:10:16 -0800239 return v5param;
240 }
241 }
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500242 if (param_extension && pa_v4) {
Tony Luckc130bd62012-01-17 12:10:16 -0800243 struct einj_parameter *v4param;
244
Lv Zhenga2383172014-05-20 15:39:41 +0800245 v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
Tony Luckc130bd62012-01-17 12:10:16 -0800246 if (!v4param)
Dan Carpenter29924b92012-01-20 10:57:14 +0300247 return NULL;
Luck, Tony459413d2012-01-23 15:27:56 -0800248 if (v4param->reserved1 || v4param->reserved2) {
Lv Zhenga2383172014-05-20 15:39:41 +0800249 acpi_os_unmap_iomem(v4param, sizeof(*v4param));
Dan Carpenter29924b92012-01-20 10:57:14 +0300250 return NULL;
Tony Luckc130bd62012-01-17 12:10:16 -0800251 }
252 return v4param;
253 }
254
Dan Carpenter29924b92012-01-20 10:57:14 +0300255 return NULL;
Huang Ying6e320ec2010-05-18 14:35:24 +0800256}
257
Huang Yinge4021342010-05-18 14:35:14 +0800258/* do sanity check to trigger table */
259static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
260{
261 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
262 return -EINVAL;
263 if (trigger_tab->table_size > PAGE_SIZE ||
Niklas Söderlund4c40aed2012-01-09 16:49:44 +0100264 trigger_tab->table_size < trigger_tab->header_size)
Huang Yinge4021342010-05-18 14:35:14 +0800265 return -EINVAL;
266 if (trigger_tab->entry_count !=
267 (trigger_tab->table_size - trigger_tab->header_size) /
268 sizeof(struct acpi_einj_entry))
269 return -EINVAL;
270
271 return 0;
272}
273
Xiao, Huib4e008d2011-12-08 11:25:48 +0800274static struct acpi_generic_address *einj_get_trigger_parameter_region(
275 struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
276{
277 int i;
278 struct acpi_whea_header *entry;
279
280 entry = (struct acpi_whea_header *)
281 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
282 for (i = 0; i < trigger_tab->entry_count; i++) {
283 if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
284 entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
285 entry->register_region.space_id ==
286 ACPI_ADR_SPACE_SYSTEM_MEMORY &&
287 (entry->register_region.address & param2) == (param1 & param2))
288 return &entry->register_region;
289 entry++;
290 }
291
292 return NULL;
293}
Huang Yinge4021342010-05-18 14:35:14 +0800294/* Execute instructions in trigger error action table */
Huang Yingfdea1632011-12-08 11:25:47 +0800295static int __einj_error_trigger(u64 trigger_paddr, u32 type,
296 u64 param1, u64 param2)
Huang Yinge4021342010-05-18 14:35:14 +0800297{
298 struct acpi_einj_trigger *trigger_tab = NULL;
299 struct apei_exec_context trigger_ctx;
300 struct apei_resources trigger_resources;
301 struct acpi_whea_header *trigger_entry;
302 struct resource *r;
303 u32 table_size;
304 int rc = -EIO;
Xiao, Huib4e008d2011-12-08 11:25:48 +0800305 struct acpi_generic_address *trigger_param_region = NULL;
Huang Yinge4021342010-05-18 14:35:14 +0800306
307 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
308 "APEI EINJ Trigger Table");
309 if (!r) {
310 pr_err(EINJ_PFX
Bjorn Helgaas46b91e32011-12-08 11:25:42 +0800311 "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
Huang Yinge4021342010-05-18 14:35:14 +0800312 (unsigned long long)trigger_paddr,
Bjorn Helgaas46b91e32011-12-08 11:25:42 +0800313 (unsigned long long)trigger_paddr +
314 sizeof(*trigger_tab) - 1);
Huang Yinge4021342010-05-18 14:35:14 +0800315 goto out;
316 }
317 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
318 if (!trigger_tab) {
319 pr_err(EINJ_PFX "Failed to map trigger table!\n");
320 goto out_rel_header;
321 }
322 rc = einj_check_trigger_header(trigger_tab);
323 if (rc) {
324 pr_warning(FW_BUG EINJ_PFX
325 "The trigger error action table is invalid\n");
326 goto out_rel_header;
327 }
Niklas Söderlund4c40aed2012-01-09 16:49:44 +0100328
329 /* No action structures in the TRIGGER_ERROR table, nothing to do */
330 if (!trigger_tab->entry_count)
331 goto out_rel_header;
332
Huang Yinge4021342010-05-18 14:35:14 +0800333 rc = -EIO;
334 table_size = trigger_tab->table_size;
335 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
336 table_size - sizeof(*trigger_tab),
337 "APEI EINJ Trigger Table");
338 if (!r) {
339 pr_err(EINJ_PFX
Bjorn Helgaas46b91e32011-12-08 11:25:42 +0800340"Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
341 (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
342 (unsigned long long)trigger_paddr + table_size - 1);
Huang Yinge4021342010-05-18 14:35:14 +0800343 goto out_rel_header;
344 }
345 iounmap(trigger_tab);
346 trigger_tab = ioremap_cache(trigger_paddr, table_size);
347 if (!trigger_tab) {
348 pr_err(EINJ_PFX "Failed to map trigger table!\n");
349 goto out_rel_entry;
350 }
351 trigger_entry = (struct acpi_whea_header *)
352 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
353 apei_resources_init(&trigger_resources);
354 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
355 ARRAY_SIZE(einj_ins_type),
356 trigger_entry, trigger_tab->entry_count);
357 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
358 if (rc)
359 goto out_fini;
360 rc = apei_resources_sub(&trigger_resources, &einj_resources);
361 if (rc)
362 goto out_fini;
Huang Yingfdea1632011-12-08 11:25:47 +0800363 /*
364 * Some firmware will access target address specified in
365 * param1 to trigger the error when injecting memory error.
366 * This will cause resource conflict with regular memory. So
367 * remove it from trigger table resources.
368 */
Chen Gongc5a13032013-06-06 15:20:51 -0700369 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
Huang Yingfdea1632011-12-08 11:25:47 +0800370 struct apei_resources addr_resources;
371 apei_resources_init(&addr_resources);
Xiao, Huib4e008d2011-12-08 11:25:48 +0800372 trigger_param_region = einj_get_trigger_parameter_region(
373 trigger_tab, param1, param2);
374 if (trigger_param_region) {
375 rc = apei_resources_add(&addr_resources,
376 trigger_param_region->address,
377 trigger_param_region->bit_width/8, true);
378 if (rc)
379 goto out_fini;
380 rc = apei_resources_sub(&trigger_resources,
381 &addr_resources);
382 }
Huang Yingfdea1632011-12-08 11:25:47 +0800383 apei_resources_fini(&addr_resources);
384 if (rc)
385 goto out_fini;
386 }
Huang Yinge4021342010-05-18 14:35:14 +0800387 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
388 if (rc)
389 goto out_fini;
390 rc = apei_exec_pre_map_gars(&trigger_ctx);
391 if (rc)
392 goto out_release;
393
394 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
395
396 apei_exec_post_unmap_gars(&trigger_ctx);
397out_release:
398 apei_resources_release(&trigger_resources);
399out_fini:
400 apei_resources_fini(&trigger_resources);
401out_rel_entry:
402 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
403 table_size - sizeof(*trigger_tab));
404out_rel_header:
405 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
406out:
407 if (trigger_tab)
408 iounmap(trigger_tab);
409
410 return rc;
411}
412
Luck, Tony3482fb52013-11-06 13:30:36 -0800413static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
414 u64 param3, u64 param4)
Huang Yinge4021342010-05-18 14:35:14 +0800415{
416 struct apei_exec_context ctx;
417 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
418 int rc;
419
420 einj_exec_ctx_init(&ctx);
421
Huang Ying392913d2011-07-13 13:14:17 +0800422 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
Huang Yinge4021342010-05-18 14:35:14 +0800423 if (rc)
424 return rc;
425 apei_exec_ctx_set_input(&ctx, type);
Tony Luckc130bd62012-01-17 12:10:16 -0800426 if (acpi5) {
427 struct set_error_type_with_address *v5param = einj_param;
428
Luck, Tony459413d2012-01-23 15:27:56 -0800429 v5param->type = type;
Chen Gongc5a13032013-06-06 15:20:51 -0700430 if (type & ACPI5_VENDOR_BIT) {
Tony Luckc130bd62012-01-17 12:10:16 -0800431 switch (vendor_flags) {
432 case SETWA_FLAGS_APICID:
Luck, Tony459413d2012-01-23 15:27:56 -0800433 v5param->apicid = param1;
Tony Luckc130bd62012-01-17 12:10:16 -0800434 break;
435 case SETWA_FLAGS_MEM:
Luck, Tony459413d2012-01-23 15:27:56 -0800436 v5param->memory_address = param1;
437 v5param->memory_address_range = param2;
Tony Luckc130bd62012-01-17 12:10:16 -0800438 break;
439 case SETWA_FLAGS_PCIE_SBDF:
Luck, Tony459413d2012-01-23 15:27:56 -0800440 v5param->pcie_sbdf = param1;
Tony Luckc130bd62012-01-17 12:10:16 -0800441 break;
442 }
Luck, Tony459413d2012-01-23 15:27:56 -0800443 v5param->flags = vendor_flags;
Luck, Tony3482fb52013-11-06 13:30:36 -0800444 } else if (flags) {
445 v5param->flags = flags;
446 v5param->memory_address = param1;
447 v5param->memory_address_range = param2;
448 v5param->apicid = param3;
449 v5param->pcie_sbdf = param4;
Tony Luckc130bd62012-01-17 12:10:16 -0800450 } else {
451 switch (type) {
452 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
453 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
454 case ACPI_EINJ_PROCESSOR_FATAL:
Luck, Tony459413d2012-01-23 15:27:56 -0800455 v5param->apicid = param1;
456 v5param->flags = SETWA_FLAGS_APICID;
Tony Luckc130bd62012-01-17 12:10:16 -0800457 break;
458 case ACPI_EINJ_MEMORY_CORRECTABLE:
459 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
460 case ACPI_EINJ_MEMORY_FATAL:
Luck, Tony459413d2012-01-23 15:27:56 -0800461 v5param->memory_address = param1;
462 v5param->memory_address_range = param2;
463 v5param->flags = SETWA_FLAGS_MEM;
Tony Luckc130bd62012-01-17 12:10:16 -0800464 break;
465 case ACPI_EINJ_PCIX_CORRECTABLE:
466 case ACPI_EINJ_PCIX_UNCORRECTABLE:
467 case ACPI_EINJ_PCIX_FATAL:
Luck, Tony459413d2012-01-23 15:27:56 -0800468 v5param->pcie_sbdf = param1;
469 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
Tony Luckc130bd62012-01-17 12:10:16 -0800470 break;
471 }
472 }
473 } else {
474 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
475 if (rc)
476 return rc;
477 if (einj_param) {
478 struct einj_parameter *v4param = einj_param;
Luck, Tony459413d2012-01-23 15:27:56 -0800479 v4param->param1 = param1;
480 v4param->param2 = param2;
Tony Luckc130bd62012-01-17 12:10:16 -0800481 }
Huang Ying6e320ec2010-05-18 14:35:24 +0800482 }
Huang Yinge4021342010-05-18 14:35:14 +0800483 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
484 if (rc)
485 return rc;
486 for (;;) {
487 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
488 if (rc)
489 return rc;
490 val = apei_exec_ctx_get_output(&ctx);
491 if (!(val & EINJ_OP_BUSY))
492 break;
493 if (einj_timedout(&timeout))
494 return -EIO;
495 }
496 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
497 if (rc)
498 return rc;
499 val = apei_exec_ctx_get_output(&ctx);
500 if (val != EINJ_STATUS_SUCCESS)
501 return -EBUSY;
502
503 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
504 if (rc)
505 return rc;
506 trigger_paddr = apei_exec_ctx_get_output(&ctx);
Chen Gongee490892012-03-15 16:53:36 +0800507 if (notrigger == 0) {
508 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
509 if (rc)
510 return rc;
511 }
Huang Ying392913d2011-07-13 13:14:17 +0800512 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
Huang Yinge4021342010-05-18 14:35:14 +0800513
514 return rc;
515}
516
517/* Inject the specified hardware error */
Luck, Tony3482fb52013-11-06 13:30:36 -0800518static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
519 u64 param3, u64 param4)
Huang Yinge4021342010-05-18 14:35:14 +0800520{
521 int rc;
Toshi Kani4650bac2016-01-26 21:57:33 +0100522 u64 base_addr, size;
Huang Yinge4021342010-05-18 14:35:14 +0800523
Luck, Tony3482fb52013-11-06 13:30:36 -0800524 /* If user manually set "flags", make sure it is legal */
525 if (flags && (flags &
526 ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
527 return -EINVAL;
528
Chen Gongc5a13032013-06-06 15:20:51 -0700529 /*
530 * We need extra sanity checks for memory errors.
531 * Other types leap directly to injection.
532 */
533
534 /* ensure param1/param2 existed */
535 if (!(param_extension || acpi5))
536 goto inject;
537
538 /* ensure injection is memory related */
539 if (type & ACPI5_VENDOR_BIT) {
540 if (vendor_flags != SETWA_FLAGS_MEM)
541 goto inject;
Luck, Tony3482fb52013-11-06 13:30:36 -0800542 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
Chen Gongc5a13032013-06-06 15:20:51 -0700543 goto inject;
544
545 /*
546 * Disallow crazy address masks that give BIOS leeway to pick
547 * injection address almost anywhere. Insist on page or
Toshi Kani4650bac2016-01-26 21:57:33 +0100548 * better granularity and that target address is normal RAM or
549 * NVDIMM.
Chen Gongc5a13032013-06-06 15:20:51 -0700550 */
Toshi Kani4650bac2016-01-26 21:57:33 +0100551 base_addr = param1 & param2;
552 size = ~param2 + 1;
553
554 if (((param2 & PAGE_MASK) != PAGE_MASK) ||
555 ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
556 != REGION_INTERSECTS) &&
557 (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
558 != REGION_INTERSECTS)))
Chen Gongc5a13032013-06-06 15:20:51 -0700559 return -EINVAL;
560
561inject:
Huang Yinge4021342010-05-18 14:35:14 +0800562 mutex_lock(&einj_mutex);
Luck, Tony3482fb52013-11-06 13:30:36 -0800563 rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
Huang Yinge4021342010-05-18 14:35:14 +0800564 mutex_unlock(&einj_mutex);
565
566 return rc;
567}
568
569static u32 error_type;
Luck, Tony3482fb52013-11-06 13:30:36 -0800570static u32 error_flags;
Huang Ying6e320ec2010-05-18 14:35:24 +0800571static u64 error_param1;
572static u64 error_param2;
Luck, Tony3482fb52013-11-06 13:30:36 -0800573static u64 error_param3;
574static u64 error_param4;
Huang Yinge4021342010-05-18 14:35:14 +0800575static struct dentry *einj_debug_dir;
576
577static int available_error_type_show(struct seq_file *m, void *v)
578{
579 int rc;
580 u32 available_error_type = 0;
581
582 rc = einj_get_available_error_type(&available_error_type);
583 if (rc)
584 return rc;
585 if (available_error_type & 0x0001)
586 seq_printf(m, "0x00000001\tProcessor Correctable\n");
587 if (available_error_type & 0x0002)
588 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
589 if (available_error_type & 0x0004)
590 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
591 if (available_error_type & 0x0008)
592 seq_printf(m, "0x00000008\tMemory Correctable\n");
593 if (available_error_type & 0x0010)
594 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
595 if (available_error_type & 0x0020)
596 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
597 if (available_error_type & 0x0040)
598 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
599 if (available_error_type & 0x0080)
600 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
601 if (available_error_type & 0x0100)
602 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
603 if (available_error_type & 0x0200)
604 seq_printf(m, "0x00000200\tPlatform Correctable\n");
605 if (available_error_type & 0x0400)
606 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
607 if (available_error_type & 0x0800)
608 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
609
610 return 0;
611}
612
613static int available_error_type_open(struct inode *inode, struct file *file)
614{
615 return single_open(file, available_error_type_show, NULL);
616}
617
618static const struct file_operations available_error_type_fops = {
619 .open = available_error_type_open,
620 .read = seq_read,
621 .llseek = seq_lseek,
622 .release = single_release,
623};
624
625static int error_type_get(void *data, u64 *val)
626{
627 *val = error_type;
628
629 return 0;
630}
631
632static int error_type_set(void *data, u64 val)
633{
634 int rc;
635 u32 available_error_type = 0;
Tony Luckc130bd62012-01-17 12:10:16 -0800636 u32 tval, vendor;
637
638 /*
639 * Vendor defined types have 0x80000000 bit set, and
640 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
641 */
Chen Gongc5a13032013-06-06 15:20:51 -0700642 vendor = val & ACPI5_VENDOR_BIT;
Tony Luckc130bd62012-01-17 12:10:16 -0800643 tval = val & 0x7fffffff;
Huang Yinge4021342010-05-18 14:35:14 +0800644
645 /* Only one error type can be specified */
Tony Luckc130bd62012-01-17 12:10:16 -0800646 if (tval & (tval - 1))
Huang Yinge4021342010-05-18 14:35:14 +0800647 return -EINVAL;
Tony Luckc130bd62012-01-17 12:10:16 -0800648 if (!vendor) {
649 rc = einj_get_available_error_type(&available_error_type);
650 if (rc)
651 return rc;
652 if (!(val & available_error_type))
653 return -EINVAL;
654 }
Huang Yinge4021342010-05-18 14:35:14 +0800655 error_type = val;
656
657 return 0;
658}
659
660DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
661 error_type_set, "0x%llx\n");
662
663static int error_inject_set(void *data, u64 val)
664{
665 if (!error_type)
666 return -EINVAL;
667
Luck, Tony3482fb52013-11-06 13:30:36 -0800668 return einj_error_inject(error_type, error_flags, error_param1, error_param2,
669 error_param3, error_param4);
Huang Yinge4021342010-05-18 14:35:14 +0800670}
671
672DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
673 error_inject_set, "%llu\n");
674
675static int einj_check_table(struct acpi_table_einj *einj_tab)
676{
Huang Ying3a78f962010-09-29 19:53:51 +0800677 if ((einj_tab->header_length !=
678 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
679 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
Huang Yinge4021342010-05-18 14:35:14 +0800680 return -EINVAL;
681 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
682 return -EINVAL;
683 if (einj_tab->entries !=
684 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
685 sizeof(struct acpi_einj_entry))
686 return -EINVAL;
687
688 return 0;
689}
690
691static int __init einj_init(void)
692{
693 int rc;
694 acpi_status status;
695 struct dentry *fentry;
696 struct apei_exec_context ctx;
697
698 if (acpi_disabled)
699 return -ENODEV;
700
701 status = acpi_get_table(ACPI_SIG_EINJ, 0,
702 (struct acpi_table_header **)&einj_tab);
Huang Yingad686152011-12-08 11:25:43 +0800703 if (status == AE_NOT_FOUND)
Huang Yinge4021342010-05-18 14:35:14 +0800704 return -ENODEV;
Huang Yingad686152011-12-08 11:25:43 +0800705 else if (ACPI_FAILURE(status)) {
Huang Yinge4021342010-05-18 14:35:14 +0800706 const char *msg = acpi_format_exception(status);
707 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
708 return -EINVAL;
709 }
710
711 rc = einj_check_table(einj_tab);
712 if (rc) {
713 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
714 return -EINVAL;
715 }
716
717 rc = -ENOMEM;
718 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
719 if (!einj_debug_dir)
720 goto err_cleanup;
721 fentry = debugfs_create_file("available_error_type", S_IRUSR,
722 einj_debug_dir, NULL,
723 &available_error_type_fops);
724 if (!fentry)
725 goto err_cleanup;
726 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
727 einj_debug_dir, NULL, &error_type_fops);
728 if (!fentry)
729 goto err_cleanup;
730 fentry = debugfs_create_file("error_inject", S_IWUSR,
731 einj_debug_dir, NULL, &error_inject_fops);
732 if (!fentry)
733 goto err_cleanup;
734
735 apei_resources_init(&einj_resources);
736 einj_exec_ctx_init(&ctx);
737 rc = apei_exec_collect_resources(&ctx, &einj_resources);
738 if (rc)
739 goto err_fini;
740 rc = apei_resources_request(&einj_resources, "APEI EINJ");
741 if (rc)
742 goto err_fini;
743 rc = apei_exec_pre_map_gars(&ctx);
744 if (rc)
745 goto err_release;
Tony Luckc130bd62012-01-17 12:10:16 -0800746
Wei Yongjunb8edb642013-06-05 16:18:12 -0700747 rc = -ENOMEM;
Tony Luckc130bd62012-01-17 12:10:16 -0800748 einj_param = einj_get_parameter_address();
749 if ((param_extension || acpi5) && einj_param) {
Luck, Tony3482fb52013-11-06 13:30:36 -0800750 fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
751 einj_debug_dir, &error_flags);
752 if (!fentry)
753 goto err_unmap;
Tony Luckc130bd62012-01-17 12:10:16 -0800754 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
755 einj_debug_dir, &error_param1);
756 if (!fentry)
757 goto err_unmap;
758 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
759 einj_debug_dir, &error_param2);
760 if (!fentry)
761 goto err_unmap;
Luck, Tony3482fb52013-11-06 13:30:36 -0800762 fentry = debugfs_create_x64("param3", S_IRUSR | S_IWUSR,
763 einj_debug_dir, &error_param3);
764 if (!fentry)
765 goto err_unmap;
766 fentry = debugfs_create_x64("param4", S_IRUSR | S_IWUSR,
767 einj_debug_dir, &error_param4);
768 if (!fentry)
769 goto err_unmap;
Chen Gongee490892012-03-15 16:53:36 +0800770
771 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
772 einj_debug_dir, &notrigger);
773 if (!fentry)
774 goto err_unmap;
Tony Luckc130bd62012-01-17 12:10:16 -0800775 }
776
777 if (vendor_dev[0]) {
778 vendor_blob.data = vendor_dev;
779 vendor_blob.size = strlen(vendor_dev);
780 fentry = debugfs_create_blob("vendor", S_IRUSR,
781 einj_debug_dir, &vendor_blob);
782 if (!fentry)
783 goto err_unmap;
784 fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
785 einj_debug_dir, &vendor_flags);
786 if (!fentry)
787 goto err_unmap;
Huang Ying6e320ec2010-05-18 14:35:24 +0800788 }
Huang Yinge4021342010-05-18 14:35:14 +0800789
790 pr_info(EINJ_PFX "Error INJection is initialized.\n");
791
792 return 0;
793
Huang Ying6e320ec2010-05-18 14:35:24 +0800794err_unmap:
Luck, Tony459413d2012-01-23 15:27:56 -0800795 if (einj_param) {
796 acpi_size size = (acpi5) ?
797 sizeof(struct set_error_type_with_address) :
798 sizeof(struct einj_parameter);
799
Lv Zhenga2383172014-05-20 15:39:41 +0800800 acpi_os_unmap_iomem(einj_param, size);
Luck, Tony459413d2012-01-23 15:27:56 -0800801 }
Huang Ying6e320ec2010-05-18 14:35:24 +0800802 apei_exec_post_unmap_gars(&ctx);
Huang Yinge4021342010-05-18 14:35:14 +0800803err_release:
804 apei_resources_release(&einj_resources);
805err_fini:
806 apei_resources_fini(&einj_resources);
807err_cleanup:
808 debugfs_remove_recursive(einj_debug_dir);
809
810 return rc;
811}
812
813static void __exit einj_exit(void)
814{
815 struct apei_exec_context ctx;
816
Luck, Tony459413d2012-01-23 15:27:56 -0800817 if (einj_param) {
818 acpi_size size = (acpi5) ?
819 sizeof(struct set_error_type_with_address) :
820 sizeof(struct einj_parameter);
821
Lv Zhenga2383172014-05-20 15:39:41 +0800822 acpi_os_unmap_iomem(einj_param, size);
Luck, Tony459413d2012-01-23 15:27:56 -0800823 }
Huang Yinge4021342010-05-18 14:35:14 +0800824 einj_exec_ctx_init(&ctx);
825 apei_exec_post_unmap_gars(&ctx);
826 apei_resources_release(&einj_resources);
827 apei_resources_fini(&einj_resources);
828 debugfs_remove_recursive(einj_debug_dir);
829}
830
831module_init(einj_init);
832module_exit(einj_exit);
833
834MODULE_AUTHOR("Huang Ying");
835MODULE_DESCRIPTION("APEI Error INJection support");
836MODULE_LICENSE("GPL");