xref: /openbmc/linux/drivers/acpi/apei/erst.c (revision b9b77222)
1 /*
2  * APEI Error Record Serialization Table support
3  *
4  * ERST is a way provided by APEI to save and retrieve hardware error
5  * information to and from a persistent store.
6  *
7  * For more information about ERST, please refer to ACPI Specification
8  * version 4.0, section 17.4.
9  *
10  * Copyright 2010 Intel Corp.
11  *   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 
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/io.h>
28 #include <linux/acpi.h>
29 #include <linux/uaccess.h>
30 #include <linux/cper.h>
31 #include <linux/nmi.h>
32 #include <linux/hardirq.h>
33 #include <linux/pstore.h>
34 #include <linux/vmalloc.h>
35 #include <linux/mm.h> /* kvfree() */
36 #include <acpi/apei.h>
37 
38 #include "apei-internal.h"
39 
40 #undef pr_fmt
41 #define pr_fmt(fmt) "ERST: " fmt
42 
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
50 
51 #define ERST_TAB_ENTRY(tab)						\
52 	((struct acpi_whea_header *)((char *)(tab) +			\
53 				     sizeof(struct acpi_table_erst)))
54 
55 #define SPIN_UNIT		100			/* 100ns */
56 /* Firmware should respond within 1 milliseconds */
57 #define FIRMWARE_TIMEOUT	(1 * NSEC_PER_MSEC)
58 #define FIRMWARE_MAX_STALL	50			/* 50us */
59 
60 int erst_disable;
61 EXPORT_SYMBOL_GPL(erst_disable);
62 
63 static struct acpi_table_erst *erst_tab;
64 
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
69 
70 /*
71  * ERST Error Log Address Range, used as buffer for reading/writing
72  * error records.
73  */
74 static struct erst_erange {
75 	u64 base;
76 	u64 size;
77 	void __iomem *vaddr;
78 	u32 attr;
79 } erst_erange;
80 
81 /*
82  * Prevent ERST interpreter to run simultaneously, because the
83  * corresponding firmware implementation may not work properly when
84  * invoked simultaneously.
85  *
86  * It is used to provide exclusive accessing for ERST Error Log
87  * Address Range too.
88  */
89 static DEFINE_RAW_SPINLOCK(erst_lock);
90 
91 static inline int erst_errno(int command_status)
92 {
93 	switch (command_status) {
94 	case ERST_STATUS_SUCCESS:
95 		return 0;
96 	case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
97 		return -ENODEV;
98 	case ERST_STATUS_NOT_ENOUGH_SPACE:
99 		return -ENOSPC;
100 	case ERST_STATUS_RECORD_STORE_EMPTY:
101 	case ERST_STATUS_RECORD_NOT_FOUND:
102 		return -ENOENT;
103 	default:
104 		return -EINVAL;
105 	}
106 }
107 
108 static int erst_timedout(u64 *t, u64 spin_unit)
109 {
110 	if ((s64)*t < spin_unit) {
111 		pr_warn(FW_WARN "Firmware does not respond in time.\n");
112 		return 1;
113 	}
114 	*t -= spin_unit;
115 	ndelay(spin_unit);
116 	touch_nmi_watchdog();
117 	return 0;
118 }
119 
120 static int erst_exec_load_var1(struct apei_exec_context *ctx,
121 			       struct acpi_whea_header *entry)
122 {
123 	return __apei_exec_read_register(entry, &ctx->var1);
124 }
125 
126 static int erst_exec_load_var2(struct apei_exec_context *ctx,
127 			       struct acpi_whea_header *entry)
128 {
129 	return __apei_exec_read_register(entry, &ctx->var2);
130 }
131 
132 static int erst_exec_store_var1(struct apei_exec_context *ctx,
133 				struct acpi_whea_header *entry)
134 {
135 	return __apei_exec_write_register(entry, ctx->var1);
136 }
137 
138 static int erst_exec_add(struct apei_exec_context *ctx,
139 			 struct acpi_whea_header *entry)
140 {
141 	ctx->var1 += ctx->var2;
142 	return 0;
143 }
144 
145 static int erst_exec_subtract(struct apei_exec_context *ctx,
146 			      struct acpi_whea_header *entry)
147 {
148 	ctx->var1 -= ctx->var2;
149 	return 0;
150 }
151 
152 static int erst_exec_add_value(struct apei_exec_context *ctx,
153 			       struct acpi_whea_header *entry)
154 {
155 	int rc;
156 	u64 val;
157 
158 	rc = __apei_exec_read_register(entry, &val);
159 	if (rc)
160 		return rc;
161 	val += ctx->value;
162 	rc = __apei_exec_write_register(entry, val);
163 	return rc;
164 }
165 
166 static int erst_exec_subtract_value(struct apei_exec_context *ctx,
167 				    struct acpi_whea_header *entry)
168 {
169 	int rc;
170 	u64 val;
171 
172 	rc = __apei_exec_read_register(entry, &val);
173 	if (rc)
174 		return rc;
175 	val -= ctx->value;
176 	rc = __apei_exec_write_register(entry, val);
177 	return rc;
178 }
179 
180 static int erst_exec_stall(struct apei_exec_context *ctx,
181 			   struct acpi_whea_header *entry)
182 {
183 	u64 stall_time;
184 
185 	if (ctx->value > FIRMWARE_MAX_STALL) {
186 		if (!in_nmi())
187 			pr_warn(FW_WARN
188 			"Too long stall time for stall instruction: 0x%llx.\n",
189 				   ctx->value);
190 		stall_time = FIRMWARE_MAX_STALL;
191 	} else
192 		stall_time = ctx->value;
193 	udelay(stall_time);
194 	return 0;
195 }
196 
197 static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
198 				      struct acpi_whea_header *entry)
199 {
200 	int rc;
201 	u64 val;
202 	u64 timeout = FIRMWARE_TIMEOUT;
203 	u64 stall_time;
204 
205 	if (ctx->var1 > FIRMWARE_MAX_STALL) {
206 		if (!in_nmi())
207 			pr_warn(FW_WARN
208 		"Too long stall time for stall while true instruction: 0x%llx.\n",
209 				   ctx->var1);
210 		stall_time = FIRMWARE_MAX_STALL;
211 	} else
212 		stall_time = ctx->var1;
213 
214 	for (;;) {
215 		rc = __apei_exec_read_register(entry, &val);
216 		if (rc)
217 			return rc;
218 		if (val != ctx->value)
219 			break;
220 		if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
221 			return -EIO;
222 	}
223 	return 0;
224 }
225 
226 static int erst_exec_skip_next_instruction_if_true(
227 	struct apei_exec_context *ctx,
228 	struct acpi_whea_header *entry)
229 {
230 	int rc;
231 	u64 val;
232 
233 	rc = __apei_exec_read_register(entry, &val);
234 	if (rc)
235 		return rc;
236 	if (val == ctx->value) {
237 		ctx->ip += 2;
238 		return APEI_EXEC_SET_IP;
239 	}
240 
241 	return 0;
242 }
243 
244 static int erst_exec_goto(struct apei_exec_context *ctx,
245 			  struct acpi_whea_header *entry)
246 {
247 	ctx->ip = ctx->value;
248 	return APEI_EXEC_SET_IP;
249 }
250 
251 static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
252 					  struct acpi_whea_header *entry)
253 {
254 	return __apei_exec_read_register(entry, &ctx->src_base);
255 }
256 
257 static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
258 					  struct acpi_whea_header *entry)
259 {
260 	return __apei_exec_read_register(entry, &ctx->dst_base);
261 }
262 
263 static int erst_exec_move_data(struct apei_exec_context *ctx,
264 			       struct acpi_whea_header *entry)
265 {
266 	int rc;
267 	u64 offset;
268 	void *src, *dst;
269 
270 	/* ioremap does not work in interrupt context */
271 	if (in_interrupt()) {
272 		pr_warn("MOVE_DATA can not be used in interrupt context.\n");
273 		return -EBUSY;
274 	}
275 
276 	rc = __apei_exec_read_register(entry, &offset);
277 	if (rc)
278 		return rc;
279 
280 	src = ioremap(ctx->src_base + offset, ctx->var2);
281 	if (!src)
282 		return -ENOMEM;
283 	dst = ioremap(ctx->dst_base + offset, ctx->var2);
284 	if (!dst) {
285 		iounmap(src);
286 		return -ENOMEM;
287 	}
288 
289 	memmove(dst, src, ctx->var2);
290 
291 	iounmap(src);
292 	iounmap(dst);
293 
294 	return 0;
295 }
296 
297 static struct apei_exec_ins_type erst_ins_type[] = {
298 	[ACPI_ERST_READ_REGISTER] = {
299 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
300 		.run = apei_exec_read_register,
301 	},
302 	[ACPI_ERST_READ_REGISTER_VALUE] = {
303 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
304 		.run = apei_exec_read_register_value,
305 	},
306 	[ACPI_ERST_WRITE_REGISTER] = {
307 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
308 		.run = apei_exec_write_register,
309 	},
310 	[ACPI_ERST_WRITE_REGISTER_VALUE] = {
311 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
312 		.run = apei_exec_write_register_value,
313 	},
314 	[ACPI_ERST_NOOP] = {
315 		.flags = 0,
316 		.run = apei_exec_noop,
317 	},
318 	[ACPI_ERST_LOAD_VAR1] = {
319 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
320 		.run = erst_exec_load_var1,
321 	},
322 	[ACPI_ERST_LOAD_VAR2] = {
323 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
324 		.run = erst_exec_load_var2,
325 	},
326 	[ACPI_ERST_STORE_VAR1] = {
327 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
328 		.run = erst_exec_store_var1,
329 	},
330 	[ACPI_ERST_ADD] = {
331 		.flags = 0,
332 		.run = erst_exec_add,
333 	},
334 	[ACPI_ERST_SUBTRACT] = {
335 		.flags = 0,
336 		.run = erst_exec_subtract,
337 	},
338 	[ACPI_ERST_ADD_VALUE] = {
339 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
340 		.run = erst_exec_add_value,
341 	},
342 	[ACPI_ERST_SUBTRACT_VALUE] = {
343 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
344 		.run = erst_exec_subtract_value,
345 	},
346 	[ACPI_ERST_STALL] = {
347 		.flags = 0,
348 		.run = erst_exec_stall,
349 	},
350 	[ACPI_ERST_STALL_WHILE_TRUE] = {
351 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
352 		.run = erst_exec_stall_while_true,
353 	},
354 	[ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
355 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
356 		.run = erst_exec_skip_next_instruction_if_true,
357 	},
358 	[ACPI_ERST_GOTO] = {
359 		.flags = 0,
360 		.run = erst_exec_goto,
361 	},
362 	[ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
363 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
364 		.run = erst_exec_set_src_address_base,
365 	},
366 	[ACPI_ERST_SET_DST_ADDRESS_BASE] = {
367 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
368 		.run = erst_exec_set_dst_address_base,
369 	},
370 	[ACPI_ERST_MOVE_DATA] = {
371 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
372 		.run = erst_exec_move_data,
373 	},
374 };
375 
376 static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
377 {
378 	apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
379 			   ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
380 }
381 
382 static int erst_get_erange(struct erst_erange *range)
383 {
384 	struct apei_exec_context ctx;
385 	int rc;
386 
387 	erst_exec_ctx_init(&ctx);
388 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
389 	if (rc)
390 		return rc;
391 	range->base = apei_exec_ctx_get_output(&ctx);
392 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
393 	if (rc)
394 		return rc;
395 	range->size = apei_exec_ctx_get_output(&ctx);
396 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
397 	if (rc)
398 		return rc;
399 	range->attr = apei_exec_ctx_get_output(&ctx);
400 
401 	return 0;
402 }
403 
404 static ssize_t __erst_get_record_count(void)
405 {
406 	struct apei_exec_context ctx;
407 	int rc;
408 
409 	erst_exec_ctx_init(&ctx);
410 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
411 	if (rc)
412 		return rc;
413 	return apei_exec_ctx_get_output(&ctx);
414 }
415 
416 ssize_t erst_get_record_count(void)
417 {
418 	ssize_t count;
419 	unsigned long flags;
420 
421 	if (erst_disable)
422 		return -ENODEV;
423 
424 	raw_spin_lock_irqsave(&erst_lock, flags);
425 	count = __erst_get_record_count();
426 	raw_spin_unlock_irqrestore(&erst_lock, flags);
427 
428 	return count;
429 }
430 EXPORT_SYMBOL_GPL(erst_get_record_count);
431 
432 #define ERST_RECORD_ID_CACHE_SIZE_MIN	16
433 #define ERST_RECORD_ID_CACHE_SIZE_MAX	1024
434 
435 struct erst_record_id_cache {
436 	struct mutex lock;
437 	u64 *entries;
438 	int len;
439 	int size;
440 	int refcount;
441 };
442 
443 static struct erst_record_id_cache erst_record_id_cache = {
444 	.lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
445 	.refcount = 0,
446 };
447 
448 static int __erst_get_next_record_id(u64 *record_id)
449 {
450 	struct apei_exec_context ctx;
451 	int rc;
452 
453 	erst_exec_ctx_init(&ctx);
454 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
455 	if (rc)
456 		return rc;
457 	*record_id = apei_exec_ctx_get_output(&ctx);
458 
459 	return 0;
460 }
461 
462 int erst_get_record_id_begin(int *pos)
463 {
464 	int rc;
465 
466 	if (erst_disable)
467 		return -ENODEV;
468 
469 	rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
470 	if (rc)
471 		return rc;
472 	erst_record_id_cache.refcount++;
473 	mutex_unlock(&erst_record_id_cache.lock);
474 
475 	*pos = 0;
476 
477 	return 0;
478 }
479 EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
480 
481 /* erst_record_id_cache.lock must be held by caller */
482 static int __erst_record_id_cache_add_one(void)
483 {
484 	u64 id, prev_id, first_id;
485 	int i, rc;
486 	u64 *entries;
487 	unsigned long flags;
488 
489 	id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
490 retry:
491 	raw_spin_lock_irqsave(&erst_lock, flags);
492 	rc = __erst_get_next_record_id(&id);
493 	raw_spin_unlock_irqrestore(&erst_lock, flags);
494 	if (rc == -ENOENT)
495 		return 0;
496 	if (rc)
497 		return rc;
498 	if (id == APEI_ERST_INVALID_RECORD_ID)
499 		return 0;
500 	/* can not skip current ID, or loop back to first ID */
501 	if (id == prev_id || id == first_id)
502 		return 0;
503 	if (first_id == APEI_ERST_INVALID_RECORD_ID)
504 		first_id = id;
505 	prev_id = id;
506 
507 	entries = erst_record_id_cache.entries;
508 	for (i = 0; i < erst_record_id_cache.len; i++) {
509 		if (entries[i] == id)
510 			break;
511 	}
512 	/* record id already in cache, try next */
513 	if (i < erst_record_id_cache.len)
514 		goto retry;
515 	if (erst_record_id_cache.len >= erst_record_id_cache.size) {
516 		int new_size;
517 		u64 *new_entries;
518 
519 		new_size = erst_record_id_cache.size * 2;
520 		new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
521 				     ERST_RECORD_ID_CACHE_SIZE_MAX);
522 		if (new_size <= erst_record_id_cache.size) {
523 			if (printk_ratelimit())
524 				pr_warn(FW_WARN "too many record IDs!\n");
525 			return 0;
526 		}
527 		new_entries = kvmalloc_array(new_size, sizeof(entries[0]),
528 					     GFP_KERNEL);
529 		if (!new_entries)
530 			return -ENOMEM;
531 		memcpy(new_entries, entries,
532 		       erst_record_id_cache.len * sizeof(entries[0]));
533 		kvfree(entries);
534 		erst_record_id_cache.entries = entries = new_entries;
535 		erst_record_id_cache.size = new_size;
536 	}
537 	entries[i] = id;
538 	erst_record_id_cache.len++;
539 
540 	return 1;
541 }
542 
543 /*
544  * Get the record ID of an existing error record on the persistent
545  * storage. If there is no error record on the persistent storage, the
546  * returned record_id is APEI_ERST_INVALID_RECORD_ID.
547  */
548 int erst_get_record_id_next(int *pos, u64 *record_id)
549 {
550 	int rc = 0;
551 	u64 *entries;
552 
553 	if (erst_disable)
554 		return -ENODEV;
555 
556 	/* must be enclosed by erst_get_record_id_begin/end */
557 	BUG_ON(!erst_record_id_cache.refcount);
558 	BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
559 
560 	mutex_lock(&erst_record_id_cache.lock);
561 	entries = erst_record_id_cache.entries;
562 	for (; *pos < erst_record_id_cache.len; (*pos)++)
563 		if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
564 			break;
565 	/* found next record id in cache */
566 	if (*pos < erst_record_id_cache.len) {
567 		*record_id = entries[*pos];
568 		(*pos)++;
569 		goto out_unlock;
570 	}
571 
572 	/* Try to add one more record ID to cache */
573 	rc = __erst_record_id_cache_add_one();
574 	if (rc < 0)
575 		goto out_unlock;
576 	/* successfully add one new ID */
577 	if (rc == 1) {
578 		*record_id = erst_record_id_cache.entries[*pos];
579 		(*pos)++;
580 		rc = 0;
581 	} else {
582 		*pos = -1;
583 		*record_id = APEI_ERST_INVALID_RECORD_ID;
584 	}
585 out_unlock:
586 	mutex_unlock(&erst_record_id_cache.lock);
587 
588 	return rc;
589 }
590 EXPORT_SYMBOL_GPL(erst_get_record_id_next);
591 
592 /* erst_record_id_cache.lock must be held by caller */
593 static void __erst_record_id_cache_compact(void)
594 {
595 	int i, wpos = 0;
596 	u64 *entries;
597 
598 	if (erst_record_id_cache.refcount)
599 		return;
600 
601 	entries = erst_record_id_cache.entries;
602 	for (i = 0; i < erst_record_id_cache.len; i++) {
603 		if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
604 			continue;
605 		if (wpos != i)
606 			entries[wpos] = entries[i];
607 		wpos++;
608 	}
609 	erst_record_id_cache.len = wpos;
610 }
611 
612 void erst_get_record_id_end(void)
613 {
614 	/*
615 	 * erst_disable != 0 should be detected by invoker via the
616 	 * return value of erst_get_record_id_begin/next, so this
617 	 * function should not be called for erst_disable != 0.
618 	 */
619 	BUG_ON(erst_disable);
620 
621 	mutex_lock(&erst_record_id_cache.lock);
622 	erst_record_id_cache.refcount--;
623 	BUG_ON(erst_record_id_cache.refcount < 0);
624 	__erst_record_id_cache_compact();
625 	mutex_unlock(&erst_record_id_cache.lock);
626 }
627 EXPORT_SYMBOL_GPL(erst_get_record_id_end);
628 
629 static int __erst_write_to_storage(u64 offset)
630 {
631 	struct apei_exec_context ctx;
632 	u64 timeout = FIRMWARE_TIMEOUT;
633 	u64 val;
634 	int rc;
635 
636 	erst_exec_ctx_init(&ctx);
637 	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
638 	if (rc)
639 		return rc;
640 	apei_exec_ctx_set_input(&ctx, offset);
641 	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
642 	if (rc)
643 		return rc;
644 	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
645 	if (rc)
646 		return rc;
647 	for (;;) {
648 		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
649 		if (rc)
650 			return rc;
651 		val = apei_exec_ctx_get_output(&ctx);
652 		if (!val)
653 			break;
654 		if (erst_timedout(&timeout, SPIN_UNIT))
655 			return -EIO;
656 	}
657 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
658 	if (rc)
659 		return rc;
660 	val = apei_exec_ctx_get_output(&ctx);
661 	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
662 	if (rc)
663 		return rc;
664 
665 	return erst_errno(val);
666 }
667 
668 static int __erst_read_from_storage(u64 record_id, u64 offset)
669 {
670 	struct apei_exec_context ctx;
671 	u64 timeout = FIRMWARE_TIMEOUT;
672 	u64 val;
673 	int rc;
674 
675 	erst_exec_ctx_init(&ctx);
676 	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
677 	if (rc)
678 		return rc;
679 	apei_exec_ctx_set_input(&ctx, offset);
680 	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
681 	if (rc)
682 		return rc;
683 	apei_exec_ctx_set_input(&ctx, record_id);
684 	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
685 	if (rc)
686 		return rc;
687 	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
688 	if (rc)
689 		return rc;
690 	for (;;) {
691 		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
692 		if (rc)
693 			return rc;
694 		val = apei_exec_ctx_get_output(&ctx);
695 		if (!val)
696 			break;
697 		if (erst_timedout(&timeout, SPIN_UNIT))
698 			return -EIO;
699 	};
700 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
701 	if (rc)
702 		return rc;
703 	val = apei_exec_ctx_get_output(&ctx);
704 	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
705 	if (rc)
706 		return rc;
707 
708 	return erst_errno(val);
709 }
710 
711 static int __erst_clear_from_storage(u64 record_id)
712 {
713 	struct apei_exec_context ctx;
714 	u64 timeout = FIRMWARE_TIMEOUT;
715 	u64 val;
716 	int rc;
717 
718 	erst_exec_ctx_init(&ctx);
719 	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
720 	if (rc)
721 		return rc;
722 	apei_exec_ctx_set_input(&ctx, record_id);
723 	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
724 	if (rc)
725 		return rc;
726 	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
727 	if (rc)
728 		return rc;
729 	for (;;) {
730 		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
731 		if (rc)
732 			return rc;
733 		val = apei_exec_ctx_get_output(&ctx);
734 		if (!val)
735 			break;
736 		if (erst_timedout(&timeout, SPIN_UNIT))
737 			return -EIO;
738 	}
739 	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
740 	if (rc)
741 		return rc;
742 	val = apei_exec_ctx_get_output(&ctx);
743 	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
744 	if (rc)
745 		return rc;
746 
747 	return erst_errno(val);
748 }
749 
750 /* NVRAM ERST Error Log Address Range is not supported yet */
751 static void pr_unimpl_nvram(void)
752 {
753 	if (printk_ratelimit())
754 		pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
755 }
756 
757 static int __erst_write_to_nvram(const struct cper_record_header *record)
758 {
759 	/* do not print message, because printk is not safe for NMI */
760 	return -ENOSYS;
761 }
762 
763 static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
764 {
765 	pr_unimpl_nvram();
766 	return -ENOSYS;
767 }
768 
769 static int __erst_clear_from_nvram(u64 record_id)
770 {
771 	pr_unimpl_nvram();
772 	return -ENOSYS;
773 }
774 
775 int erst_write(const struct cper_record_header *record)
776 {
777 	int rc;
778 	unsigned long flags;
779 	struct cper_record_header *rcd_erange;
780 
781 	if (erst_disable)
782 		return -ENODEV;
783 
784 	if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
785 		return -EINVAL;
786 
787 	if (erst_erange.attr & ERST_RANGE_NVRAM) {
788 		if (!raw_spin_trylock_irqsave(&erst_lock, flags))
789 			return -EBUSY;
790 		rc = __erst_write_to_nvram(record);
791 		raw_spin_unlock_irqrestore(&erst_lock, flags);
792 		return rc;
793 	}
794 
795 	if (record->record_length > erst_erange.size)
796 		return -EINVAL;
797 
798 	if (!raw_spin_trylock_irqsave(&erst_lock, flags))
799 		return -EBUSY;
800 	memcpy(erst_erange.vaddr, record, record->record_length);
801 	rcd_erange = erst_erange.vaddr;
802 	/* signature for serialization system */
803 	memcpy(&rcd_erange->persistence_information, "ER", 2);
804 
805 	rc = __erst_write_to_storage(0);
806 	raw_spin_unlock_irqrestore(&erst_lock, flags);
807 
808 	return rc;
809 }
810 EXPORT_SYMBOL_GPL(erst_write);
811 
812 static int __erst_read_to_erange(u64 record_id, u64 *offset)
813 {
814 	int rc;
815 
816 	if (erst_erange.attr & ERST_RANGE_NVRAM)
817 		return __erst_read_to_erange_from_nvram(
818 			record_id, offset);
819 
820 	rc = __erst_read_from_storage(record_id, 0);
821 	if (rc)
822 		return rc;
823 	*offset = 0;
824 
825 	return 0;
826 }
827 
828 static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
829 			   size_t buflen)
830 {
831 	int rc;
832 	u64 offset, len = 0;
833 	struct cper_record_header *rcd_tmp;
834 
835 	rc = __erst_read_to_erange(record_id, &offset);
836 	if (rc)
837 		return rc;
838 	rcd_tmp = erst_erange.vaddr + offset;
839 	len = rcd_tmp->record_length;
840 	if (len <= buflen)
841 		memcpy(record, rcd_tmp, len);
842 
843 	return len;
844 }
845 
846 /*
847  * If return value > buflen, the buffer size is not big enough,
848  * else if return value < 0, something goes wrong,
849  * else everything is OK, and return value is record length
850  */
851 ssize_t erst_read(u64 record_id, struct cper_record_header *record,
852 		  size_t buflen)
853 {
854 	ssize_t len;
855 	unsigned long flags;
856 
857 	if (erst_disable)
858 		return -ENODEV;
859 
860 	raw_spin_lock_irqsave(&erst_lock, flags);
861 	len = __erst_read(record_id, record, buflen);
862 	raw_spin_unlock_irqrestore(&erst_lock, flags);
863 	return len;
864 }
865 EXPORT_SYMBOL_GPL(erst_read);
866 
867 int erst_clear(u64 record_id)
868 {
869 	int rc, i;
870 	unsigned long flags;
871 	u64 *entries;
872 
873 	if (erst_disable)
874 		return -ENODEV;
875 
876 	rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
877 	if (rc)
878 		return rc;
879 	raw_spin_lock_irqsave(&erst_lock, flags);
880 	if (erst_erange.attr & ERST_RANGE_NVRAM)
881 		rc = __erst_clear_from_nvram(record_id);
882 	else
883 		rc = __erst_clear_from_storage(record_id);
884 	raw_spin_unlock_irqrestore(&erst_lock, flags);
885 	if (rc)
886 		goto out;
887 	entries = erst_record_id_cache.entries;
888 	for (i = 0; i < erst_record_id_cache.len; i++) {
889 		if (entries[i] == record_id)
890 			entries[i] = APEI_ERST_INVALID_RECORD_ID;
891 	}
892 	__erst_record_id_cache_compact();
893 out:
894 	mutex_unlock(&erst_record_id_cache.lock);
895 	return rc;
896 }
897 EXPORT_SYMBOL_GPL(erst_clear);
898 
899 static int __init setup_erst_disable(char *str)
900 {
901 	erst_disable = 1;
902 	return 0;
903 }
904 
905 __setup("erst_disable", setup_erst_disable);
906 
907 static int erst_check_table(struct acpi_table_erst *erst_tab)
908 {
909 	if ((erst_tab->header_length !=
910 	     (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
911 	    && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
912 		return -EINVAL;
913 	if (erst_tab->header.length < sizeof(struct acpi_table_erst))
914 		return -EINVAL;
915 	if (erst_tab->entries !=
916 	    (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
917 	    sizeof(struct acpi_erst_entry))
918 		return -EINVAL;
919 
920 	return 0;
921 }
922 
923 static int erst_open_pstore(struct pstore_info *psi);
924 static int erst_close_pstore(struct pstore_info *psi);
925 static ssize_t erst_reader(struct pstore_record *record);
926 static int erst_writer(struct pstore_record *record);
927 static int erst_clearer(struct pstore_record *record);
928 
929 static struct pstore_info erst_info = {
930 	.owner		= THIS_MODULE,
931 	.name		= "erst",
932 	.flags		= PSTORE_FLAGS_DMESG,
933 	.open		= erst_open_pstore,
934 	.close		= erst_close_pstore,
935 	.read		= erst_reader,
936 	.write		= erst_writer,
937 	.erase		= erst_clearer
938 };
939 
940 #define CPER_CREATOR_PSTORE						\
941 	UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c,	\
942 		0x64, 0x90, 0xb8, 0x9d)
943 #define CPER_SECTION_TYPE_DMESG						\
944 	UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54,	\
945 		0x94, 0x19, 0xeb, 0x12)
946 #define CPER_SECTION_TYPE_DMESG_Z					\
947 	UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d,	\
948 		0x34, 0xdd, 0xfa, 0xc6)
949 #define CPER_SECTION_TYPE_MCE						\
950 	UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96,	\
951 		0x04, 0x4a, 0x38, 0xfc)
952 
953 struct cper_pstore_record {
954 	struct cper_record_header hdr;
955 	struct cper_section_descriptor sec_hdr;
956 	char data[];
957 } __packed;
958 
959 static int reader_pos;
960 
961 static int erst_open_pstore(struct pstore_info *psi)
962 {
963 	int rc;
964 
965 	if (erst_disable)
966 		return -ENODEV;
967 
968 	rc = erst_get_record_id_begin(&reader_pos);
969 
970 	return rc;
971 }
972 
973 static int erst_close_pstore(struct pstore_info *psi)
974 {
975 	erst_get_record_id_end();
976 
977 	return 0;
978 }
979 
980 static ssize_t erst_reader(struct pstore_record *record)
981 {
982 	int rc;
983 	ssize_t len = 0;
984 	u64 record_id;
985 	struct cper_pstore_record *rcd;
986 	size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
987 
988 	if (erst_disable)
989 		return -ENODEV;
990 
991 	rcd = kmalloc(rcd_len, GFP_KERNEL);
992 	if (!rcd) {
993 		rc = -ENOMEM;
994 		goto out;
995 	}
996 skip:
997 	rc = erst_get_record_id_next(&reader_pos, &record_id);
998 	if (rc)
999 		goto out;
1000 
1001 	/* no more record */
1002 	if (record_id == APEI_ERST_INVALID_RECORD_ID) {
1003 		rc = -EINVAL;
1004 		goto out;
1005 	}
1006 
1007 	len = erst_read(record_id, &rcd->hdr, rcd_len);
1008 	/* The record may be cleared by others, try read next record */
1009 	if (len == -ENOENT)
1010 		goto skip;
1011 	else if (len < 0 || len < sizeof(*rcd)) {
1012 		rc = -EIO;
1013 		goto out;
1014 	}
1015 	if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
1016 		goto skip;
1017 
1018 	record->buf = kmalloc(len, GFP_KERNEL);
1019 	if (record->buf == NULL) {
1020 		rc = -ENOMEM;
1021 		goto out;
1022 	}
1023 	memcpy(record->buf, rcd->data, len - sizeof(*rcd));
1024 	record->id = record_id;
1025 	record->compressed = false;
1026 	record->ecc_notice_size = 0;
1027 	if (uuid_le_cmp(rcd->sec_hdr.section_type,
1028 			CPER_SECTION_TYPE_DMESG_Z) == 0) {
1029 		record->type = PSTORE_TYPE_DMESG;
1030 		record->compressed = true;
1031 	} else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1032 			CPER_SECTION_TYPE_DMESG) == 0)
1033 		record->type = PSTORE_TYPE_DMESG;
1034 	else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1035 			     CPER_SECTION_TYPE_MCE) == 0)
1036 		record->type = PSTORE_TYPE_MCE;
1037 	else
1038 		record->type = PSTORE_TYPE_UNKNOWN;
1039 
1040 	if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1041 		record->time.tv_sec = rcd->hdr.timestamp;
1042 	else
1043 		record->time.tv_sec = 0;
1044 	record->time.tv_nsec = 0;
1045 
1046 out:
1047 	kfree(rcd);
1048 	return (rc < 0) ? rc : (len - sizeof(*rcd));
1049 }
1050 
1051 static int erst_writer(struct pstore_record *record)
1052 {
1053 	struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1054 					(erst_info.buf - sizeof(*rcd));
1055 	int ret;
1056 
1057 	memset(rcd, 0, sizeof(*rcd));
1058 	memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1059 	rcd->hdr.revision = CPER_RECORD_REV;
1060 	rcd->hdr.signature_end = CPER_SIG_END;
1061 	rcd->hdr.section_count = 1;
1062 	rcd->hdr.error_severity = CPER_SEV_FATAL;
1063 	/* timestamp valid. platform_id, partition_id are invalid */
1064 	rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1065 	rcd->hdr.timestamp = ktime_get_real_seconds();
1066 	rcd->hdr.record_length = sizeof(*rcd) + record->size;
1067 	rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1068 	rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1069 	rcd->hdr.record_id = cper_next_record_id();
1070 	rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1071 
1072 	rcd->sec_hdr.section_offset = sizeof(*rcd);
1073 	rcd->sec_hdr.section_length = record->size;
1074 	rcd->sec_hdr.revision = CPER_SEC_REV;
1075 	/* fru_id and fru_text is invalid */
1076 	rcd->sec_hdr.validation_bits = 0;
1077 	rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1078 	switch (record->type) {
1079 	case PSTORE_TYPE_DMESG:
1080 		if (record->compressed)
1081 			rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
1082 		else
1083 			rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
1084 		break;
1085 	case PSTORE_TYPE_MCE:
1086 		rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1087 		break;
1088 	default:
1089 		return -EINVAL;
1090 	}
1091 	rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1092 
1093 	ret = erst_write(&rcd->hdr);
1094 	record->id = rcd->hdr.record_id;
1095 
1096 	return ret;
1097 }
1098 
1099 static int erst_clearer(struct pstore_record *record)
1100 {
1101 	return erst_clear(record->id);
1102 }
1103 
1104 static int __init erst_init(void)
1105 {
1106 	int rc = 0;
1107 	acpi_status status;
1108 	struct apei_exec_context ctx;
1109 	struct apei_resources erst_resources;
1110 	struct resource *r;
1111 	char *buf;
1112 
1113 	if (acpi_disabled)
1114 		goto err;
1115 
1116 	if (erst_disable) {
1117 		pr_info(
1118 	"Error Record Serialization Table (ERST) support is disabled.\n");
1119 		goto err;
1120 	}
1121 
1122 	status = acpi_get_table(ACPI_SIG_ERST, 0,
1123 				(struct acpi_table_header **)&erst_tab);
1124 	if (status == AE_NOT_FOUND)
1125 		goto err;
1126 	else if (ACPI_FAILURE(status)) {
1127 		const char *msg = acpi_format_exception(status);
1128 		pr_err("Failed to get table, %s\n", msg);
1129 		rc = -EINVAL;
1130 		goto err;
1131 	}
1132 
1133 	rc = erst_check_table(erst_tab);
1134 	if (rc) {
1135 		pr_err(FW_BUG "ERST table is invalid.\n");
1136 		goto err;
1137 	}
1138 
1139 	apei_resources_init(&erst_resources);
1140 	erst_exec_ctx_init(&ctx);
1141 	rc = apei_exec_collect_resources(&ctx, &erst_resources);
1142 	if (rc)
1143 		goto err_fini;
1144 	rc = apei_resources_request(&erst_resources, "APEI ERST");
1145 	if (rc)
1146 		goto err_fini;
1147 	rc = apei_exec_pre_map_gars(&ctx);
1148 	if (rc)
1149 		goto err_release;
1150 	rc = erst_get_erange(&erst_erange);
1151 	if (rc) {
1152 		if (rc == -ENODEV)
1153 			pr_info(
1154 	"The corresponding hardware device or firmware implementation "
1155 	"is not available.\n");
1156 		else
1157 			pr_err("Failed to get Error Log Address Range.\n");
1158 		goto err_unmap_reg;
1159 	}
1160 
1161 	r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1162 	if (!r) {
1163 		pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1164 		       (unsigned long long)erst_erange.base,
1165 		       (unsigned long long)erst_erange.base + erst_erange.size - 1);
1166 		rc = -EIO;
1167 		goto err_unmap_reg;
1168 	}
1169 	rc = -ENOMEM;
1170 	erst_erange.vaddr = ioremap_cache(erst_erange.base,
1171 					  erst_erange.size);
1172 	if (!erst_erange.vaddr)
1173 		goto err_release_erange;
1174 
1175 	pr_info(
1176 	"Error Record Serialization Table (ERST) support is initialized.\n");
1177 
1178 	buf = kmalloc(erst_erange.size, GFP_KERNEL);
1179 	spin_lock_init(&erst_info.buf_lock);
1180 	if (buf) {
1181 		erst_info.buf = buf + sizeof(struct cper_pstore_record);
1182 		erst_info.bufsize = erst_erange.size -
1183 				    sizeof(struct cper_pstore_record);
1184 		rc = pstore_register(&erst_info);
1185 		if (rc) {
1186 			if (rc != -EPERM)
1187 				pr_info(
1188 				"Could not register with persistent store.\n");
1189 			erst_info.buf = NULL;
1190 			erst_info.bufsize = 0;
1191 			kfree(buf);
1192 		}
1193 	} else
1194 		pr_err(
1195 		"Failed to allocate %lld bytes for persistent store error log.\n",
1196 		erst_erange.size);
1197 
1198 	/* Cleanup ERST Resources */
1199 	apei_resources_fini(&erst_resources);
1200 
1201 	return 0;
1202 
1203 err_release_erange:
1204 	release_mem_region(erst_erange.base, erst_erange.size);
1205 err_unmap_reg:
1206 	apei_exec_post_unmap_gars(&ctx);
1207 err_release:
1208 	apei_resources_release(&erst_resources);
1209 err_fini:
1210 	apei_resources_fini(&erst_resources);
1211 err:
1212 	erst_disable = 1;
1213 	return rc;
1214 }
1215 
1216 device_initcall(erst_init);
1217