blob: 1be6f55644855ac8e1a156dbb8a87aff801f20d9 [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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/io.h>
31#include <linux/debugfs.h>
32#include <linux/seq_file.h>
33#include <linux/nmi.h>
34#include <linux/delay.h>
Chen Gongc5a13032013-06-06 15:20:51 -070035#include <linux/mm.h>
Chen, Gongd3ab3ed2013-12-18 01:30:49 -050036#include <asm/unaligned.h>
Huang Yinge4021342010-05-18 14:35:14 +080037
38#include "apei-internal.h"
39
40#define EINJ_PFX "EINJ: "
41
42#define SPIN_UNIT 100 /* 100ns */
Stefan Weile8a8b252011-01-02 15:12:42 +010043/* Firmware should respond within 1 milliseconds */
Huang Yinge4021342010-05-18 14:35:14 +080044#define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
Chen Gongc5a13032013-06-06 15:20:51 -070045#define ACPI5_VENDOR_BIT BIT(31)
46#define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \
47 ACPI_EINJ_MEMORY_UNCORRECTABLE | \
48 ACPI_EINJ_MEMORY_FATAL)
Huang Yinge4021342010-05-18 14:35:14 +080049
Huang Ying6e320ec2010-05-18 14:35:24 +080050/*
Tony Luckc130bd62012-01-17 12:10:16 -080051 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
52 */
53static int acpi5;
54
55struct set_error_type_with_address {
56 u32 type;
57 u32 vendor_extension;
58 u32 flags;
59 u32 apicid;
60 u64 memory_address;
61 u64 memory_address_range;
62 u32 pcie_sbdf;
63};
64enum {
65 SETWA_FLAGS_APICID = 1,
66 SETWA_FLAGS_MEM = 2,
67 SETWA_FLAGS_PCIE_SBDF = 4,
68};
69
70/*
71 * Vendor extensions for platform specific operations
72 */
73struct vendor_error_type_extension {
74 u32 length;
75 u32 pcie_sbdf;
76 u16 vendor_id;
77 u16 device_id;
78 u8 rev_id;
79 u8 reserved[3];
80};
81
Chen Gongee490892012-03-15 16:53:36 +080082static u32 notrigger;
83
Tony Luckc130bd62012-01-17 12:10:16 -080084static u32 vendor_flags;
85static struct debugfs_blob_wrapper vendor_blob;
86static char vendor_dev[64];
87
88/*
Huang Ying6e320ec2010-05-18 14:35:24 +080089 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
90 * EINJ table through an unpublished extension. Use with caution as
91 * most will ignore the parameter and make their own choice of address
Huang Yingc3e60882011-07-20 16:09:29 +080092 * for error injection. This extension is used only if
93 * param_extension module parameter is specified.
Huang Ying6e320ec2010-05-18 14:35:24 +080094 */
95struct einj_parameter {
96 u64 type;
97 u64 reserved1;
98 u64 reserved2;
99 u64 param1;
100 u64 param2;
101};
102
Huang Yinge4021342010-05-18 14:35:14 +0800103#define EINJ_OP_BUSY 0x1
104#define EINJ_STATUS_SUCCESS 0x0
105#define EINJ_STATUS_FAIL 0x1
106#define EINJ_STATUS_INVAL 0x2
107
108#define EINJ_TAB_ENTRY(tab) \
109 ((struct acpi_whea_header *)((char *)(tab) + \
110 sizeof(struct acpi_table_einj)))
111
Huang Yingc3e60882011-07-20 16:09:29 +0800112static bool param_extension;
113module_param(param_extension, bool, 0);
114
Huang Yinge4021342010-05-18 14:35:14 +0800115static struct acpi_table_einj *einj_tab;
116
117static struct apei_resources einj_resources;
118
119static struct apei_exec_ins_type einj_ins_type[] = {
120 [ACPI_EINJ_READ_REGISTER] = {
121 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
122 .run = apei_exec_read_register,
123 },
124 [ACPI_EINJ_READ_REGISTER_VALUE] = {
125 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
126 .run = apei_exec_read_register_value,
127 },
128 [ACPI_EINJ_WRITE_REGISTER] = {
129 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
130 .run = apei_exec_write_register,
131 },
132 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
133 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
134 .run = apei_exec_write_register_value,
135 },
136 [ACPI_EINJ_NOOP] = {
137 .flags = 0,
138 .run = apei_exec_noop,
139 },
140};
141
142/*
143 * Prevent EINJ interpreter to run simultaneously, because the
144 * corresponding firmware implementation may not work properly when
145 * invoked simultaneously.
146 */
147static DEFINE_MUTEX(einj_mutex);
148
Tony Luckc130bd62012-01-17 12:10:16 -0800149static void *einj_param;
150
Huang Yinge4021342010-05-18 14:35:14 +0800151static void einj_exec_ctx_init(struct apei_exec_context *ctx)
152{
153 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
154 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
155}
156
157static int __einj_get_available_error_type(u32 *type)
158{
159 struct apei_exec_context ctx;
160 int rc;
161
162 einj_exec_ctx_init(&ctx);
163 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
164 if (rc)
165 return rc;
166 *type = apei_exec_ctx_get_output(&ctx);
167
168 return 0;
169}
170
171/* Get error injection capabilities of the platform */
172static int einj_get_available_error_type(u32 *type)
173{
174 int rc;
175
176 mutex_lock(&einj_mutex);
177 rc = __einj_get_available_error_type(type);
178 mutex_unlock(&einj_mutex);
179
180 return rc;
181}
182
183static int einj_timedout(u64 *t)
184{
185 if ((s64)*t < SPIN_UNIT) {
186 pr_warning(FW_WARN EINJ_PFX
187 "Firmware does not respond in time\n");
188 return 1;
189 }
190 *t -= SPIN_UNIT;
191 ndelay(SPIN_UNIT);
192 touch_nmi_watchdog();
193 return 0;
194}
195
Tony Luckc130bd62012-01-17 12:10:16 -0800196static void check_vendor_extension(u64 paddr,
197 struct set_error_type_with_address *v5param)
198{
Luck, Tony459413d2012-01-23 15:27:56 -0800199 int offset = v5param->vendor_extension;
Tony Luckc130bd62012-01-17 12:10:16 -0800200 struct vendor_error_type_extension *v;
201 u32 sbdf;
202
203 if (!offset)
204 return;
Luck, Tony459413d2012-01-23 15:27:56 -0800205 v = acpi_os_map_memory(paddr + offset, sizeof(*v));
Tony Luckc130bd62012-01-17 12:10:16 -0800206 if (!v)
207 return;
Luck, Tony459413d2012-01-23 15:27:56 -0800208 sbdf = v->pcie_sbdf;
Tony Luckc130bd62012-01-17 12:10:16 -0800209 sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
210 sbdf >> 24, (sbdf >> 16) & 0xff,
211 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
Luck, Tony459413d2012-01-23 15:27:56 -0800212 v->vendor_id, v->device_id, v->rev_id);
213 acpi_os_unmap_memory(v, sizeof(*v));
Tony Luckc130bd62012-01-17 12:10:16 -0800214}
215
216static void *einj_get_parameter_address(void)
Huang Ying6e320ec2010-05-18 14:35:24 +0800217{
218 int i;
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500219 u64 pa_v4 = 0, pa_v5 = 0;
Huang Ying6e320ec2010-05-18 14:35:24 +0800220 struct acpi_whea_header *entry;
221
222 entry = EINJ_TAB_ENTRY(einj_tab);
223 for (i = 0; i < einj_tab->entries; i++) {
224 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
225 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
226 entry->register_region.space_id ==
227 ACPI_ADR_SPACE_SYSTEM_MEMORY)
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500228 pa_v4 = get_unaligned(&entry->register_region.address);
Tony Luckc130bd62012-01-17 12:10:16 -0800229 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
230 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
231 entry->register_region.space_id ==
232 ACPI_ADR_SPACE_SYSTEM_MEMORY)
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500233 pa_v5 = get_unaligned(&entry->register_region.address);
Huang Ying6e320ec2010-05-18 14:35:24 +0800234 entry++;
235 }
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500236 if (pa_v5) {
Tony Luckc130bd62012-01-17 12:10:16 -0800237 struct set_error_type_with_address *v5param;
Huang Ying6e320ec2010-05-18 14:35:24 +0800238
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500239 v5param = acpi_os_map_memory(pa_v5, sizeof(*v5param));
Tony Luckc130bd62012-01-17 12:10:16 -0800240 if (v5param) {
241 acpi5 = 1;
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500242 check_vendor_extension(pa_v5, v5param);
Tony Luckc130bd62012-01-17 12:10:16 -0800243 return v5param;
244 }
245 }
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500246 if (param_extension && pa_v4) {
Tony Luckc130bd62012-01-17 12:10:16 -0800247 struct einj_parameter *v4param;
248
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500249 v4param = acpi_os_map_memory(pa_v4, sizeof(*v4param));
Tony Luckc130bd62012-01-17 12:10:16 -0800250 if (!v4param)
Dan Carpenter29924b92012-01-20 10:57:14 +0300251 return NULL;
Luck, Tony459413d2012-01-23 15:27:56 -0800252 if (v4param->reserved1 || v4param->reserved2) {
253 acpi_os_unmap_memory(v4param, sizeof(*v4param));
Dan Carpenter29924b92012-01-20 10:57:14 +0300254 return NULL;
Tony Luckc130bd62012-01-17 12:10:16 -0800255 }
256 return v4param;
257 }
258
Dan Carpenter29924b92012-01-20 10:57:14 +0300259 return NULL;
Huang Ying6e320ec2010-05-18 14:35:24 +0800260}
261
Huang Yinge4021342010-05-18 14:35:14 +0800262/* do sanity check to trigger table */
263static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
264{
265 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
266 return -EINVAL;
267 if (trigger_tab->table_size > PAGE_SIZE ||
Niklas Söderlund4c40aed2012-01-09 16:49:44 +0100268 trigger_tab->table_size < trigger_tab->header_size)
Huang Yinge4021342010-05-18 14:35:14 +0800269 return -EINVAL;
270 if (trigger_tab->entry_count !=
271 (trigger_tab->table_size - trigger_tab->header_size) /
272 sizeof(struct acpi_einj_entry))
273 return -EINVAL;
274
275 return 0;
276}
277
Xiao, Huib4e008d2011-12-08 11:25:48 +0800278static struct acpi_generic_address *einj_get_trigger_parameter_region(
279 struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
280{
281 int i;
282 struct acpi_whea_header *entry;
283
284 entry = (struct acpi_whea_header *)
285 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
286 for (i = 0; i < trigger_tab->entry_count; i++) {
287 if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
288 entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
289 entry->register_region.space_id ==
290 ACPI_ADR_SPACE_SYSTEM_MEMORY &&
291 (entry->register_region.address & param2) == (param1 & param2))
292 return &entry->register_region;
293 entry++;
294 }
295
296 return NULL;
297}
Huang Yinge4021342010-05-18 14:35:14 +0800298/* Execute instructions in trigger error action table */
Huang Yingfdea1632011-12-08 11:25:47 +0800299static int __einj_error_trigger(u64 trigger_paddr, u32 type,
300 u64 param1, u64 param2)
Huang Yinge4021342010-05-18 14:35:14 +0800301{
302 struct acpi_einj_trigger *trigger_tab = NULL;
303 struct apei_exec_context trigger_ctx;
304 struct apei_resources trigger_resources;
305 struct acpi_whea_header *trigger_entry;
306 struct resource *r;
307 u32 table_size;
308 int rc = -EIO;
Xiao, Huib4e008d2011-12-08 11:25:48 +0800309 struct acpi_generic_address *trigger_param_region = NULL;
Huang Yinge4021342010-05-18 14:35:14 +0800310
311 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
312 "APEI EINJ Trigger Table");
313 if (!r) {
314 pr_err(EINJ_PFX
Bjorn Helgaas46b91e32011-12-08 11:25:42 +0800315 "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
Huang Yinge4021342010-05-18 14:35:14 +0800316 (unsigned long long)trigger_paddr,
Bjorn Helgaas46b91e32011-12-08 11:25:42 +0800317 (unsigned long long)trigger_paddr +
318 sizeof(*trigger_tab) - 1);
Huang Yinge4021342010-05-18 14:35:14 +0800319 goto out;
320 }
321 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
322 if (!trigger_tab) {
323 pr_err(EINJ_PFX "Failed to map trigger table!\n");
324 goto out_rel_header;
325 }
326 rc = einj_check_trigger_header(trigger_tab);
327 if (rc) {
328 pr_warning(FW_BUG EINJ_PFX
329 "The trigger error action table is invalid\n");
330 goto out_rel_header;
331 }
Niklas Söderlund4c40aed2012-01-09 16:49:44 +0100332
333 /* No action structures in the TRIGGER_ERROR table, nothing to do */
334 if (!trigger_tab->entry_count)
335 goto out_rel_header;
336
Huang Yinge4021342010-05-18 14:35:14 +0800337 rc = -EIO;
338 table_size = trigger_tab->table_size;
339 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
340 table_size - sizeof(*trigger_tab),
341 "APEI EINJ Trigger Table");
342 if (!r) {
343 pr_err(EINJ_PFX
Bjorn Helgaas46b91e32011-12-08 11:25:42 +0800344"Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
345 (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
346 (unsigned long long)trigger_paddr + table_size - 1);
Huang Yinge4021342010-05-18 14:35:14 +0800347 goto out_rel_header;
348 }
349 iounmap(trigger_tab);
350 trigger_tab = ioremap_cache(trigger_paddr, table_size);
351 if (!trigger_tab) {
352 pr_err(EINJ_PFX "Failed to map trigger table!\n");
353 goto out_rel_entry;
354 }
355 trigger_entry = (struct acpi_whea_header *)
356 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
357 apei_resources_init(&trigger_resources);
358 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
359 ARRAY_SIZE(einj_ins_type),
360 trigger_entry, trigger_tab->entry_count);
361 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
362 if (rc)
363 goto out_fini;
364 rc = apei_resources_sub(&trigger_resources, &einj_resources);
365 if (rc)
366 goto out_fini;
Huang Yingfdea1632011-12-08 11:25:47 +0800367 /*
368 * Some firmware will access target address specified in
369 * param1 to trigger the error when injecting memory error.
370 * This will cause resource conflict with regular memory. So
371 * remove it from trigger table resources.
372 */
Chen Gongc5a13032013-06-06 15:20:51 -0700373 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
Huang Yingfdea1632011-12-08 11:25:47 +0800374 struct apei_resources addr_resources;
375 apei_resources_init(&addr_resources);
Xiao, Huib4e008d2011-12-08 11:25:48 +0800376 trigger_param_region = einj_get_trigger_parameter_region(
377 trigger_tab, param1, param2);
378 if (trigger_param_region) {
379 rc = apei_resources_add(&addr_resources,
380 trigger_param_region->address,
381 trigger_param_region->bit_width/8, true);
382 if (rc)
383 goto out_fini;
384 rc = apei_resources_sub(&trigger_resources,
385 &addr_resources);
386 }
Huang Yingfdea1632011-12-08 11:25:47 +0800387 apei_resources_fini(&addr_resources);
388 if (rc)
389 goto out_fini;
390 }
Huang Yinge4021342010-05-18 14:35:14 +0800391 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
392 if (rc)
393 goto out_fini;
394 rc = apei_exec_pre_map_gars(&trigger_ctx);
395 if (rc)
396 goto out_release;
397
398 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
399
400 apei_exec_post_unmap_gars(&trigger_ctx);
401out_release:
402 apei_resources_release(&trigger_resources);
403out_fini:
404 apei_resources_fini(&trigger_resources);
405out_rel_entry:
406 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
407 table_size - sizeof(*trigger_tab));
408out_rel_header:
409 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
410out:
411 if (trigger_tab)
412 iounmap(trigger_tab);
413
414 return rc;
415}
416
Luck, Tony3482fb52013-11-06 13:30:36 -0800417static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
418 u64 param3, u64 param4)
Huang Yinge4021342010-05-18 14:35:14 +0800419{
420 struct apei_exec_context ctx;
421 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
422 int rc;
423
424 einj_exec_ctx_init(&ctx);
425
Huang Ying392913d2011-07-13 13:14:17 +0800426 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
Huang Yinge4021342010-05-18 14:35:14 +0800427 if (rc)
428 return rc;
429 apei_exec_ctx_set_input(&ctx, type);
Tony Luckc130bd62012-01-17 12:10:16 -0800430 if (acpi5) {
431 struct set_error_type_with_address *v5param = einj_param;
432
Luck, Tony459413d2012-01-23 15:27:56 -0800433 v5param->type = type;
Chen Gongc5a13032013-06-06 15:20:51 -0700434 if (type & ACPI5_VENDOR_BIT) {
Tony Luckc130bd62012-01-17 12:10:16 -0800435 switch (vendor_flags) {
436 case SETWA_FLAGS_APICID:
Luck, Tony459413d2012-01-23 15:27:56 -0800437 v5param->apicid = param1;
Tony Luckc130bd62012-01-17 12:10:16 -0800438 break;
439 case SETWA_FLAGS_MEM:
Luck, Tony459413d2012-01-23 15:27:56 -0800440 v5param->memory_address = param1;
441 v5param->memory_address_range = param2;
Tony Luckc130bd62012-01-17 12:10:16 -0800442 break;
443 case SETWA_FLAGS_PCIE_SBDF:
Luck, Tony459413d2012-01-23 15:27:56 -0800444 v5param->pcie_sbdf = param1;
Tony Luckc130bd62012-01-17 12:10:16 -0800445 break;
446 }
Luck, Tony459413d2012-01-23 15:27:56 -0800447 v5param->flags = vendor_flags;
Luck, Tony3482fb52013-11-06 13:30:36 -0800448 } else if (flags) {
449 v5param->flags = flags;
450 v5param->memory_address = param1;
451 v5param->memory_address_range = param2;
452 v5param->apicid = param3;
453 v5param->pcie_sbdf = param4;
Tony Luckc130bd62012-01-17 12:10:16 -0800454 } else {
455 switch (type) {
456 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
457 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
458 case ACPI_EINJ_PROCESSOR_FATAL:
Luck, Tony459413d2012-01-23 15:27:56 -0800459 v5param->apicid = param1;
460 v5param->flags = SETWA_FLAGS_APICID;
Tony Luckc130bd62012-01-17 12:10:16 -0800461 break;
462 case ACPI_EINJ_MEMORY_CORRECTABLE:
463 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
464 case ACPI_EINJ_MEMORY_FATAL:
Luck, Tony459413d2012-01-23 15:27:56 -0800465 v5param->memory_address = param1;
466 v5param->memory_address_range = param2;
467 v5param->flags = SETWA_FLAGS_MEM;
Tony Luckc130bd62012-01-17 12:10:16 -0800468 break;
469 case ACPI_EINJ_PCIX_CORRECTABLE:
470 case ACPI_EINJ_PCIX_UNCORRECTABLE:
471 case ACPI_EINJ_PCIX_FATAL:
Luck, Tony459413d2012-01-23 15:27:56 -0800472 v5param->pcie_sbdf = param1;
473 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
Tony Luckc130bd62012-01-17 12:10:16 -0800474 break;
475 }
476 }
477 } else {
478 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
479 if (rc)
480 return rc;
481 if (einj_param) {
482 struct einj_parameter *v4param = einj_param;
Luck, Tony459413d2012-01-23 15:27:56 -0800483 v4param->param1 = param1;
484 v4param->param2 = param2;
Tony Luckc130bd62012-01-17 12:10:16 -0800485 }
Huang Ying6e320ec2010-05-18 14:35:24 +0800486 }
Huang Yinge4021342010-05-18 14:35:14 +0800487 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
488 if (rc)
489 return rc;
490 for (;;) {
491 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
492 if (rc)
493 return rc;
494 val = apei_exec_ctx_get_output(&ctx);
495 if (!(val & EINJ_OP_BUSY))
496 break;
497 if (einj_timedout(&timeout))
498 return -EIO;
499 }
500 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
501 if (rc)
502 return rc;
503 val = apei_exec_ctx_get_output(&ctx);
504 if (val != EINJ_STATUS_SUCCESS)
505 return -EBUSY;
506
507 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
508 if (rc)
509 return rc;
510 trigger_paddr = apei_exec_ctx_get_output(&ctx);
Chen Gongee490892012-03-15 16:53:36 +0800511 if (notrigger == 0) {
512 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
513 if (rc)
514 return rc;
515 }
Huang Ying392913d2011-07-13 13:14:17 +0800516 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
Huang Yinge4021342010-05-18 14:35:14 +0800517
518 return rc;
519}
520
521/* Inject the specified hardware error */
Luck, Tony3482fb52013-11-06 13:30:36 -0800522static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
523 u64 param3, u64 param4)
Huang Yinge4021342010-05-18 14:35:14 +0800524{
525 int rc;
Chen Gongc5a13032013-06-06 15:20:51 -0700526 unsigned long pfn;
Huang Yinge4021342010-05-18 14:35:14 +0800527
Luck, Tony3482fb52013-11-06 13:30:36 -0800528 /* If user manually set "flags", make sure it is legal */
529 if (flags && (flags &
530 ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
531 return -EINVAL;
532
Chen Gongc5a13032013-06-06 15:20:51 -0700533 /*
534 * We need extra sanity checks for memory errors.
535 * Other types leap directly to injection.
536 */
537
538 /* ensure param1/param2 existed */
539 if (!(param_extension || acpi5))
540 goto inject;
541
542 /* ensure injection is memory related */
543 if (type & ACPI5_VENDOR_BIT) {
544 if (vendor_flags != SETWA_FLAGS_MEM)
545 goto inject;
Luck, Tony3482fb52013-11-06 13:30:36 -0800546 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
Chen Gongc5a13032013-06-06 15:20:51 -0700547 goto inject;
548
549 /*
550 * Disallow crazy address masks that give BIOS leeway to pick
551 * injection address almost anywhere. Insist on page or
552 * better granularity and that target address is normal RAM.
553 */
554 pfn = PFN_DOWN(param1 & param2);
555 if (!page_is_ram(pfn) || ((param2 & PAGE_MASK) != PAGE_MASK))
556 return -EINVAL;
557
558inject:
Huang Yinge4021342010-05-18 14:35:14 +0800559 mutex_lock(&einj_mutex);
Luck, Tony3482fb52013-11-06 13:30:36 -0800560 rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
Huang Yinge4021342010-05-18 14:35:14 +0800561 mutex_unlock(&einj_mutex);
562
563 return rc;
564}
565
566static u32 error_type;
Luck, Tony3482fb52013-11-06 13:30:36 -0800567static u32 error_flags;
Huang Ying6e320ec2010-05-18 14:35:24 +0800568static u64 error_param1;
569static u64 error_param2;
Luck, Tony3482fb52013-11-06 13:30:36 -0800570static u64 error_param3;
571static u64 error_param4;
Huang Yinge4021342010-05-18 14:35:14 +0800572static struct dentry *einj_debug_dir;
573
574static int available_error_type_show(struct seq_file *m, void *v)
575{
576 int rc;
577 u32 available_error_type = 0;
578
579 rc = einj_get_available_error_type(&available_error_type);
580 if (rc)
581 return rc;
582 if (available_error_type & 0x0001)
583 seq_printf(m, "0x00000001\tProcessor Correctable\n");
584 if (available_error_type & 0x0002)
585 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
586 if (available_error_type & 0x0004)
587 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
588 if (available_error_type & 0x0008)
589 seq_printf(m, "0x00000008\tMemory Correctable\n");
590 if (available_error_type & 0x0010)
591 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
592 if (available_error_type & 0x0020)
593 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
594 if (available_error_type & 0x0040)
595 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
596 if (available_error_type & 0x0080)
597 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
598 if (available_error_type & 0x0100)
599 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
600 if (available_error_type & 0x0200)
601 seq_printf(m, "0x00000200\tPlatform Correctable\n");
602 if (available_error_type & 0x0400)
603 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
604 if (available_error_type & 0x0800)
605 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
606
607 return 0;
608}
609
610static int available_error_type_open(struct inode *inode, struct file *file)
611{
612 return single_open(file, available_error_type_show, NULL);
613}
614
615static const struct file_operations available_error_type_fops = {
616 .open = available_error_type_open,
617 .read = seq_read,
618 .llseek = seq_lseek,
619 .release = single_release,
620};
621
622static int error_type_get(void *data, u64 *val)
623{
624 *val = error_type;
625
626 return 0;
627}
628
629static int error_type_set(void *data, u64 val)
630{
631 int rc;
632 u32 available_error_type = 0;
Tony Luckc130bd62012-01-17 12:10:16 -0800633 u32 tval, vendor;
634
635 /*
636 * Vendor defined types have 0x80000000 bit set, and
637 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
638 */
Chen Gongc5a13032013-06-06 15:20:51 -0700639 vendor = val & ACPI5_VENDOR_BIT;
Tony Luckc130bd62012-01-17 12:10:16 -0800640 tval = val & 0x7fffffff;
Huang Yinge4021342010-05-18 14:35:14 +0800641
642 /* Only one error type can be specified */
Tony Luckc130bd62012-01-17 12:10:16 -0800643 if (tval & (tval - 1))
Huang Yinge4021342010-05-18 14:35:14 +0800644 return -EINVAL;
Tony Luckc130bd62012-01-17 12:10:16 -0800645 if (!vendor) {
646 rc = einj_get_available_error_type(&available_error_type);
647 if (rc)
648 return rc;
649 if (!(val & available_error_type))
650 return -EINVAL;
651 }
Huang Yinge4021342010-05-18 14:35:14 +0800652 error_type = val;
653
654 return 0;
655}
656
657DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
658 error_type_set, "0x%llx\n");
659
660static int error_inject_set(void *data, u64 val)
661{
662 if (!error_type)
663 return -EINVAL;
664
Luck, Tony3482fb52013-11-06 13:30:36 -0800665 return einj_error_inject(error_type, error_flags, error_param1, error_param2,
666 error_param3, error_param4);
Huang Yinge4021342010-05-18 14:35:14 +0800667}
668
669DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
670 error_inject_set, "%llu\n");
671
672static int einj_check_table(struct acpi_table_einj *einj_tab)
673{
Huang Ying3a78f962010-09-29 19:53:51 +0800674 if ((einj_tab->header_length !=
675 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
676 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
Huang Yinge4021342010-05-18 14:35:14 +0800677 return -EINVAL;
678 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
679 return -EINVAL;
680 if (einj_tab->entries !=
681 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
682 sizeof(struct acpi_einj_entry))
683 return -EINVAL;
684
685 return 0;
686}
687
688static int __init einj_init(void)
689{
690 int rc;
691 acpi_status status;
692 struct dentry *fentry;
693 struct apei_exec_context ctx;
694
695 if (acpi_disabled)
696 return -ENODEV;
697
698 status = acpi_get_table(ACPI_SIG_EINJ, 0,
699 (struct acpi_table_header **)&einj_tab);
Huang Yingad686152011-12-08 11:25:43 +0800700 if (status == AE_NOT_FOUND)
Huang Yinge4021342010-05-18 14:35:14 +0800701 return -ENODEV;
Huang Yingad686152011-12-08 11:25:43 +0800702 else if (ACPI_FAILURE(status)) {
Huang Yinge4021342010-05-18 14:35:14 +0800703 const char *msg = acpi_format_exception(status);
704 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
705 return -EINVAL;
706 }
707
708 rc = einj_check_table(einj_tab);
709 if (rc) {
710 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
711 return -EINVAL;
712 }
713
714 rc = -ENOMEM;
715 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
716 if (!einj_debug_dir)
717 goto err_cleanup;
718 fentry = debugfs_create_file("available_error_type", S_IRUSR,
719 einj_debug_dir, NULL,
720 &available_error_type_fops);
721 if (!fentry)
722 goto err_cleanup;
723 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
724 einj_debug_dir, NULL, &error_type_fops);
725 if (!fentry)
726 goto err_cleanup;
727 fentry = debugfs_create_file("error_inject", S_IWUSR,
728 einj_debug_dir, NULL, &error_inject_fops);
729 if (!fentry)
730 goto err_cleanup;
731
732 apei_resources_init(&einj_resources);
733 einj_exec_ctx_init(&ctx);
734 rc = apei_exec_collect_resources(&ctx, &einj_resources);
735 if (rc)
736 goto err_fini;
737 rc = apei_resources_request(&einj_resources, "APEI EINJ");
738 if (rc)
739 goto err_fini;
740 rc = apei_exec_pre_map_gars(&ctx);
741 if (rc)
742 goto err_release;
Tony Luckc130bd62012-01-17 12:10:16 -0800743
Wei Yongjunb8edb642013-06-05 16:18:12 -0700744 rc = -ENOMEM;
Tony Luckc130bd62012-01-17 12:10:16 -0800745 einj_param = einj_get_parameter_address();
746 if ((param_extension || acpi5) && einj_param) {
Luck, Tony3482fb52013-11-06 13:30:36 -0800747 fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
748 einj_debug_dir, &error_flags);
749 if (!fentry)
750 goto err_unmap;
Tony Luckc130bd62012-01-17 12:10:16 -0800751 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
752 einj_debug_dir, &error_param1);
753 if (!fentry)
754 goto err_unmap;
755 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
756 einj_debug_dir, &error_param2);
757 if (!fentry)
758 goto err_unmap;
Luck, Tony3482fb52013-11-06 13:30:36 -0800759 fentry = debugfs_create_x64("param3", S_IRUSR | S_IWUSR,
760 einj_debug_dir, &error_param3);
761 if (!fentry)
762 goto err_unmap;
763 fentry = debugfs_create_x64("param4", S_IRUSR | S_IWUSR,
764 einj_debug_dir, &error_param4);
765 if (!fentry)
766 goto err_unmap;
Chen Gongee490892012-03-15 16:53:36 +0800767
768 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
769 einj_debug_dir, &notrigger);
770 if (!fentry)
771 goto err_unmap;
Tony Luckc130bd62012-01-17 12:10:16 -0800772 }
773
774 if (vendor_dev[0]) {
775 vendor_blob.data = vendor_dev;
776 vendor_blob.size = strlen(vendor_dev);
777 fentry = debugfs_create_blob("vendor", S_IRUSR,
778 einj_debug_dir, &vendor_blob);
779 if (!fentry)
780 goto err_unmap;
781 fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
782 einj_debug_dir, &vendor_flags);
783 if (!fentry)
784 goto err_unmap;
Huang Ying6e320ec2010-05-18 14:35:24 +0800785 }
Huang Yinge4021342010-05-18 14:35:14 +0800786
787 pr_info(EINJ_PFX "Error INJection is initialized.\n");
788
789 return 0;
790
Huang Ying6e320ec2010-05-18 14:35:24 +0800791err_unmap:
Luck, Tony459413d2012-01-23 15:27:56 -0800792 if (einj_param) {
793 acpi_size size = (acpi5) ?
794 sizeof(struct set_error_type_with_address) :
795 sizeof(struct einj_parameter);
796
797 acpi_os_unmap_memory(einj_param, size);
798 }
Huang Ying6e320ec2010-05-18 14:35:24 +0800799 apei_exec_post_unmap_gars(&ctx);
Huang Yinge4021342010-05-18 14:35:14 +0800800err_release:
801 apei_resources_release(&einj_resources);
802err_fini:
803 apei_resources_fini(&einj_resources);
804err_cleanup:
805 debugfs_remove_recursive(einj_debug_dir);
806
807 return rc;
808}
809
810static void __exit einj_exit(void)
811{
812 struct apei_exec_context ctx;
813
Luck, Tony459413d2012-01-23 15:27:56 -0800814 if (einj_param) {
815 acpi_size size = (acpi5) ?
816 sizeof(struct set_error_type_with_address) :
817 sizeof(struct einj_parameter);
818
819 acpi_os_unmap_memory(einj_param, size);
820 }
Huang Yinge4021342010-05-18 14:35:14 +0800821 einj_exec_ctx_init(&ctx);
822 apei_exec_post_unmap_gars(&ctx);
823 apei_resources_release(&einj_resources);
824 apei_resources_fini(&einj_resources);
825 debugfs_remove_recursive(einj_debug_dir);
826}
827
828module_init(einj_init);
829module_exit(einj_exit);
830
831MODULE_AUTHOR("Huang Ying");
832MODULE_DESCRIPTION("APEI Error INJection support");
833MODULE_LICENSE("GPL");