2 * APEI Error Record Serialization Table support
4 * ERST is a way provided by APEI to save and retrieve hardware error
5 * information to and from a persistent store.
7 * For more information about ERST, please refer to ACPI Specification
8 * version 4.0, section 17.4.
10 * Copyright 2010 Intel Corp.
11 * Author: Huang Ying <ying.huang@intel.com>
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.
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.
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
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
32 #include <linux/acpi.h>
33 #include <linux/uaccess.h>
34 #include <linux/cper.h>
35 #include <linux/nmi.h>
36 #include <linux/hardirq.h>
37 #include <acpi/apei.h>
39 #include "apei-internal.h"
41 #define ERST_PFX "ERST: "
43 /* ERST command status */
44 #define ERST_STATUS_SUCCESS 0x0
45 #define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
46 #define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
47 #define ERST_STATUS_FAILED 0x3
48 #define ERST_STATUS_RECORD_STORE_EMPTY 0x4
49 #define ERST_STATUS_RECORD_NOT_FOUND 0x5
51 #define ERST_TAB_ENTRY(tab) \
52 ((struct acpi_whea_header *)((char *)(tab) + \
53 sizeof(struct acpi_table_erst)))
55 #define SPIN_UNIT 100 /* 100ns */
56 /* Firmware should respond within 1 miliseconds */
57 #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
58 #define FIRMWARE_MAX_STALL 50 /* 50us */
61 EXPORT_SYMBOL_GPL(erst_disable);
63 static struct acpi_table_erst *erst_tab;
65 /* ERST Error Log Address Range atrributes */
66 #define ERST_RANGE_RESERVED 0x0001
67 #define ERST_RANGE_NVRAM 0x0002
68 #define ERST_RANGE_SLOW 0x0004
71 * ERST Error Log Address Range, used as buffer for reading/writing
74 static struct erst_erange {
82 * Prevent ERST interpreter to run simultaneously, because the
83 * corresponding firmware implementation may not work properly when
84 * invoked simultaneously.
86 * It is used to provide exclusive accessing for ERST Error Log
89 static DEFINE_SPINLOCK(erst_lock);
91 static inline int erst_errno(int command_status)
93 switch (command_status) {
94 case ERST_STATUS_SUCCESS:
96 case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
98 case ERST_STATUS_NOT_ENOUGH_SPACE:
100 case ERST_STATUS_RECORD_STORE_EMPTY:
101 case ERST_STATUS_RECORD_NOT_FOUND:
108 static int erst_timedout(u64 *t, u64 spin_unit)
110 if ((s64)*t < spin_unit) {
111 pr_warning(FW_WARN ERST_PFX
112 "Firmware does not respond in time\n");
117 touch_nmi_watchdog();
121 static int erst_exec_load_var1(struct apei_exec_context *ctx,
122 struct acpi_whea_header *entry)
124 return __apei_exec_read_register(entry, &ctx->var1);
127 static int erst_exec_load_var2(struct apei_exec_context *ctx,
128 struct acpi_whea_header *entry)
130 return __apei_exec_read_register(entry, &ctx->var2);
133 static int erst_exec_store_var1(struct apei_exec_context *ctx,
134 struct acpi_whea_header *entry)
136 return __apei_exec_write_register(entry, ctx->var1);
139 static int erst_exec_add(struct apei_exec_context *ctx,
140 struct acpi_whea_header *entry)
142 ctx->var1 += ctx->var2;
146 static int erst_exec_subtract(struct apei_exec_context *ctx,
147 struct acpi_whea_header *entry)
149 ctx->var1 -= ctx->var2;
153 static int erst_exec_add_value(struct apei_exec_context *ctx,
154 struct acpi_whea_header *entry)
159 rc = __apei_exec_read_register(entry, &val);
163 rc = __apei_exec_write_register(entry, val);
167 static int erst_exec_subtract_value(struct apei_exec_context *ctx,
168 struct acpi_whea_header *entry)
173 rc = __apei_exec_read_register(entry, &val);
177 rc = __apei_exec_write_register(entry, val);
181 static int erst_exec_stall(struct apei_exec_context *ctx,
182 struct acpi_whea_header *entry)
186 if (ctx->value > FIRMWARE_MAX_STALL) {
188 pr_warning(FW_WARN ERST_PFX
189 "Too long stall time for stall instruction: %llx.\n",
191 stall_time = FIRMWARE_MAX_STALL;
193 stall_time = ctx->value;
198 static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
199 struct acpi_whea_header *entry)
203 u64 timeout = FIRMWARE_TIMEOUT;
206 if (ctx->var1 > FIRMWARE_MAX_STALL) {
208 pr_warning(FW_WARN ERST_PFX
209 "Too long stall time for stall while true instruction: %llx.\n",
211 stall_time = FIRMWARE_MAX_STALL;
213 stall_time = ctx->var1;
216 rc = __apei_exec_read_register(entry, &val);
219 if (val != ctx->value)
221 if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
227 static int erst_exec_skip_next_instruction_if_true(
228 struct apei_exec_context *ctx,
229 struct acpi_whea_header *entry)
234 rc = __apei_exec_read_register(entry, &val);
237 if (val == ctx->value) {
239 return APEI_EXEC_SET_IP;
245 static int erst_exec_goto(struct apei_exec_context *ctx,
246 struct acpi_whea_header *entry)
248 ctx->ip = ctx->value;
249 return APEI_EXEC_SET_IP;
252 static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
253 struct acpi_whea_header *entry)
255 return __apei_exec_read_register(entry, &ctx->src_base);
258 static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
259 struct acpi_whea_header *entry)
261 return __apei_exec_read_register(entry, &ctx->dst_base);
264 static int erst_exec_move_data(struct apei_exec_context *ctx,
265 struct acpi_whea_header *entry)
270 rc = __apei_exec_read_register(entry, &offset);
273 memmove((void *)ctx->dst_base + offset,
274 (void *)ctx->src_base + offset,
280 static struct apei_exec_ins_type erst_ins_type[] = {
281 [ACPI_ERST_READ_REGISTER] = {
282 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
283 .run = apei_exec_read_register,
285 [ACPI_ERST_READ_REGISTER_VALUE] = {
286 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
287 .run = apei_exec_read_register_value,
289 [ACPI_ERST_WRITE_REGISTER] = {
290 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
291 .run = apei_exec_write_register,
293 [ACPI_ERST_WRITE_REGISTER_VALUE] = {
294 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
295 .run = apei_exec_write_register_value,
299 .run = apei_exec_noop,
301 [ACPI_ERST_LOAD_VAR1] = {
302 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
303 .run = erst_exec_load_var1,
305 [ACPI_ERST_LOAD_VAR2] = {
306 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
307 .run = erst_exec_load_var2,
309 [ACPI_ERST_STORE_VAR1] = {
310 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
311 .run = erst_exec_store_var1,
315 .run = erst_exec_add,
317 [ACPI_ERST_SUBTRACT] = {
319 .run = erst_exec_subtract,
321 [ACPI_ERST_ADD_VALUE] = {
322 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
323 .run = erst_exec_add_value,
325 [ACPI_ERST_SUBTRACT_VALUE] = {
326 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
327 .run = erst_exec_subtract_value,
329 [ACPI_ERST_STALL] = {
331 .run = erst_exec_stall,
333 [ACPI_ERST_STALL_WHILE_TRUE] = {
334 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
335 .run = erst_exec_stall_while_true,
337 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
338 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
339 .run = erst_exec_skip_next_instruction_if_true,
343 .run = erst_exec_goto,
345 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
346 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
347 .run = erst_exec_set_src_address_base,
349 [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
350 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
351 .run = erst_exec_set_dst_address_base,
353 [ACPI_ERST_MOVE_DATA] = {
354 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
355 .run = erst_exec_move_data,
359 static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
361 apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
362 ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
365 static int erst_get_erange(struct erst_erange *range)
367 struct apei_exec_context ctx;
370 erst_exec_ctx_init(&ctx);
371 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
374 range->base = apei_exec_ctx_get_output(&ctx);
375 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
378 range->size = apei_exec_ctx_get_output(&ctx);
379 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
382 range->attr = apei_exec_ctx_get_output(&ctx);
387 static ssize_t __erst_get_record_count(void)
389 struct apei_exec_context ctx;
392 erst_exec_ctx_init(&ctx);
393 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
396 return apei_exec_ctx_get_output(&ctx);
399 ssize_t erst_get_record_count(void)
407 spin_lock_irqsave(&erst_lock, flags);
408 count = __erst_get_record_count();
409 spin_unlock_irqrestore(&erst_lock, flags);
413 EXPORT_SYMBOL_GPL(erst_get_record_count);
415 static int __erst_get_next_record_id(u64 *record_id)
417 struct apei_exec_context ctx;
420 erst_exec_ctx_init(&ctx);
421 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
424 *record_id = apei_exec_ctx_get_output(&ctx);
430 * Get the record ID of an existing error record on the persistent
431 * storage. If there is no error record on the persistent storage, the
432 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
434 int erst_get_next_record_id(u64 *record_id)
442 spin_lock_irqsave(&erst_lock, flags);
443 rc = __erst_get_next_record_id(record_id);
444 spin_unlock_irqrestore(&erst_lock, flags);
448 EXPORT_SYMBOL_GPL(erst_get_next_record_id);
450 static int __erst_write_to_storage(u64 offset)
452 struct apei_exec_context ctx;
453 u64 timeout = FIRMWARE_TIMEOUT;
457 erst_exec_ctx_init(&ctx);
458 rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_WRITE);
461 apei_exec_ctx_set_input(&ctx, offset);
462 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
465 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
469 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
472 val = apei_exec_ctx_get_output(&ctx);
475 if (erst_timedout(&timeout, SPIN_UNIT))
478 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
481 val = apei_exec_ctx_get_output(&ctx);
482 rc = apei_exec_run(&ctx, ACPI_ERST_END);
486 return erst_errno(val);
489 static int __erst_read_from_storage(u64 record_id, u64 offset)
491 struct apei_exec_context ctx;
492 u64 timeout = FIRMWARE_TIMEOUT;
496 erst_exec_ctx_init(&ctx);
497 rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_READ);
500 apei_exec_ctx_set_input(&ctx, offset);
501 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
504 apei_exec_ctx_set_input(&ctx, record_id);
505 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
508 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
512 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
515 val = apei_exec_ctx_get_output(&ctx);
518 if (erst_timedout(&timeout, SPIN_UNIT))
521 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
524 val = apei_exec_ctx_get_output(&ctx);
525 rc = apei_exec_run(&ctx, ACPI_ERST_END);
529 return erst_errno(val);
532 static int __erst_clear_from_storage(u64 record_id)
534 struct apei_exec_context ctx;
535 u64 timeout = FIRMWARE_TIMEOUT;
539 erst_exec_ctx_init(&ctx);
540 rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_CLEAR);
543 apei_exec_ctx_set_input(&ctx, record_id);
544 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
547 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
551 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
554 val = apei_exec_ctx_get_output(&ctx);
557 if (erst_timedout(&timeout, SPIN_UNIT))
560 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
563 val = apei_exec_ctx_get_output(&ctx);
564 rc = apei_exec_run(&ctx, ACPI_ERST_END);
568 return erst_errno(val);
571 /* NVRAM ERST Error Log Address Range is not supported yet */
572 static void pr_unimpl_nvram(void)
574 if (printk_ratelimit())
576 "NVRAM ERST Log Address Range is not implemented yet\n");
579 static int __erst_write_to_nvram(const struct cper_record_header *record)
581 /* do not print message, because printk is not safe for NMI */
585 static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
591 static int __erst_clear_from_nvram(u64 record_id)
597 int erst_write(const struct cper_record_header *record)
601 struct cper_record_header *rcd_erange;
606 if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
609 if (erst_erange.attr & ERST_RANGE_NVRAM) {
610 if (!spin_trylock_irqsave(&erst_lock, flags))
612 rc = __erst_write_to_nvram(record);
613 spin_unlock_irqrestore(&erst_lock, flags);
617 if (record->record_length > erst_erange.size)
620 if (!spin_trylock_irqsave(&erst_lock, flags))
622 memcpy(erst_erange.vaddr, record, record->record_length);
623 rcd_erange = erst_erange.vaddr;
624 /* signature for serialization system */
625 memcpy(&rcd_erange->persistence_information, "ER", 2);
627 rc = __erst_write_to_storage(0);
628 spin_unlock_irqrestore(&erst_lock, flags);
632 EXPORT_SYMBOL_GPL(erst_write);
634 static int __erst_read_to_erange(u64 record_id, u64 *offset)
638 if (erst_erange.attr & ERST_RANGE_NVRAM)
639 return __erst_read_to_erange_from_nvram(
642 rc = __erst_read_from_storage(record_id, 0);
650 static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
655 struct cper_record_header *rcd_tmp;
657 rc = __erst_read_to_erange(record_id, &offset);
660 rcd_tmp = erst_erange.vaddr + offset;
661 len = rcd_tmp->record_length;
663 memcpy(record, rcd_tmp, len);
669 * If return value > buflen, the buffer size is not big enough,
670 * else if return value < 0, something goes wrong,
671 * else everything is OK, and return value is record length
673 ssize_t erst_read(u64 record_id, struct cper_record_header *record,
682 spin_lock_irqsave(&erst_lock, flags);
683 len = __erst_read(record_id, record, buflen);
684 spin_unlock_irqrestore(&erst_lock, flags);
687 EXPORT_SYMBOL_GPL(erst_read);
690 * If return value > buflen, the buffer size is not big enough,
691 * else if return value = 0, there is no more record to read,
692 * else if return value < 0, something goes wrong,
693 * else everything is OK, and return value is record length
695 ssize_t erst_read_next(struct cper_record_header *record, size_t buflen)
705 spin_lock_irqsave(&erst_lock, flags);
706 rc = __erst_get_next_record_id(&record_id);
708 spin_unlock_irqrestore(&erst_lock, flags);
712 if (record_id == APEI_ERST_INVALID_RECORD_ID) {
713 spin_unlock_irqrestore(&erst_lock, flags);
717 len = __erst_read(record_id, record, buflen);
718 spin_unlock_irqrestore(&erst_lock, flags);
722 EXPORT_SYMBOL_GPL(erst_read_next);
724 int erst_clear(u64 record_id)
732 spin_lock_irqsave(&erst_lock, flags);
733 if (erst_erange.attr & ERST_RANGE_NVRAM)
734 rc = __erst_clear_from_nvram(record_id);
736 rc = __erst_clear_from_storage(record_id);
737 spin_unlock_irqrestore(&erst_lock, flags);
741 EXPORT_SYMBOL_GPL(erst_clear);
743 static int __init setup_erst_disable(char *str)
749 __setup("erst_disable", setup_erst_disable);
751 static int erst_check_table(struct acpi_table_erst *erst_tab)
753 if (erst_tab->header_length != sizeof(struct acpi_table_erst))
755 if (erst_tab->header.length < sizeof(struct acpi_table_erst))
757 if (erst_tab->entries !=
758 (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
759 sizeof(struct acpi_erst_entry))
765 static int __init erst_init(void)
769 struct apei_exec_context ctx;
770 struct apei_resources erst_resources;
778 "Error Record Serialization Table (ERST) support is disabled.\n");
782 status = acpi_get_table(ACPI_SIG_ERST, 0,
783 (struct acpi_table_header **)&erst_tab);
784 if (status == AE_NOT_FOUND) {
785 pr_info(ERST_PFX "Table is not found!\n");
787 } else if (ACPI_FAILURE(status)) {
788 const char *msg = acpi_format_exception(status);
789 pr_err(ERST_PFX "Failed to get table, %s\n", msg);
794 rc = erst_check_table(erst_tab);
796 pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
800 apei_resources_init(&erst_resources);
801 erst_exec_ctx_init(&ctx);
802 rc = apei_exec_collect_resources(&ctx, &erst_resources);
805 rc = apei_resources_request(&erst_resources, "APEI ERST");
808 rc = apei_exec_pre_map_gars(&ctx);
811 rc = erst_get_erange(&erst_erange);
815 "The corresponding hardware device or firmware implementation "
816 "is not available.\n");
819 "Failed to get Error Log Address Range.\n");
823 r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
826 "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
827 (unsigned long long)erst_erange.base,
828 (unsigned long long)erst_erange.base + erst_erange.size);
833 erst_erange.vaddr = ioremap_cache(erst_erange.base,
835 if (!erst_erange.vaddr)
836 goto err_release_erange;
839 "Error Record Serialization Table (ERST) support is initialized.\n");
844 release_mem_region(erst_erange.base, erst_erange.size);
846 apei_exec_post_unmap_gars(&ctx);
848 apei_resources_release(&erst_resources);
850 apei_resources_fini(&erst_resources);
856 device_initcall(erst_init);