Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * apei-base.c - ACPI Platform Error Interface (APEI) supporting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * APEI allows to report errors (for example from the chipset) to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * the operating system. This improves NMI handling especially. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * addition it supports error serialization and error injection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * For more information about APEI, please refer to ACPI Specification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * version 4.0, chapter 17.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * This file has Common functions used by more than one APEI table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * including framework of interpreter for ERST and EINJ; resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * management for APEI registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * Copyright (C) 2009, Intel Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *	Author: Huang Ying <ying.huang@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include "apei-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define APEI_PFX "APEI: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * APEI ERST (Error Record Serialization Table) and EINJ (Error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * INJection) interpreter framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define APEI_EXEC_PRESERVE_REGISTER	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) void apei_exec_ctx_init(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			struct apei_exec_ins_type *ins_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			u32 instructions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			struct acpi_whea_header *action_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			u32 entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	ctx->ins_table = ins_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	ctx->instructions = instructions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	ctx->action_table = action_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	ctx->entries = entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) EXPORT_SYMBOL_GPL(apei_exec_ctx_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) int __apei_exec_read_register(struct acpi_whea_header *entry, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	rc = apei_read(val, &entry->register_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	*val >>= entry->register_region.bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	*val &= entry->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) int apei_exec_read_register(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			    struct acpi_whea_header *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u64 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	rc = __apei_exec_read_register(entry, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	ctx->value = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) EXPORT_SYMBOL_GPL(apei_exec_read_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) int apei_exec_read_register_value(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				  struct acpi_whea_header *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	rc = apei_exec_read_register(ctx, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	ctx->value = (ctx->value == entry->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) EXPORT_SYMBOL_GPL(apei_exec_read_register_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) int __apei_exec_write_register(struct acpi_whea_header *entry, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	val &= entry->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	val <<= entry->register_region.bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (entry->flags & APEI_EXEC_PRESERVE_REGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		u64 valr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		rc = apei_read(&valr, &entry->register_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		valr &= ~(entry->mask << entry->register_region.bit_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		val |= valr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	rc = apei_write(val, &entry->register_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int apei_exec_write_register(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			     struct acpi_whea_header *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return __apei_exec_write_register(entry, ctx->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) EXPORT_SYMBOL_GPL(apei_exec_write_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int apei_exec_write_register_value(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				   struct acpi_whea_header *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	ctx->value = entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	rc = apei_exec_write_register(ctx, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) EXPORT_SYMBOL_GPL(apei_exec_write_register_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int apei_exec_noop(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		   struct acpi_whea_header *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) EXPORT_SYMBOL_GPL(apei_exec_noop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * Interpret the specified action. Go through whole action table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * execute all instructions belong to the action.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int __apei_exec_run(struct apei_exec_context *ctx, u8 action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		    bool optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int rc = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	u32 i, ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct acpi_whea_header *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	apei_exec_ins_func_t run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	ctx->ip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 * "ip" is the instruction pointer of current instruction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	 * "ctx->ip" specifies the next instruction to executed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	 * instruction "run" function may change the "ctx->ip" to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	 * implement "goto" semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) rewind:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	ip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	for (i = 0; i < ctx->entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		entry = &ctx->action_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		if (entry->action != action)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (ip == ctx->ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			if (entry->instruction >= ctx->instructions ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			    !ctx->ins_table[entry->instruction].run) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				pr_warn(FW_WARN APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					"Invalid action table, unknown instruction type: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 					entry->instruction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			run = ctx->ins_table[entry->instruction].run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			rc = run(ctx, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			else if (rc != APEI_EXEC_SET_IP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				ctx->ip++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		ip++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		if (ctx->ip < ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			goto rewind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return !optional && rc < 0 ? rc : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) EXPORT_SYMBOL_GPL(__apei_exec_run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) typedef int (*apei_exec_entry_func_t)(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				      struct acpi_whea_header *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				      void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int apei_exec_for_each_entry(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				    apei_exec_entry_func_t func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				    void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				    int *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	u8 ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct acpi_whea_header *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct apei_exec_ins_type *ins_table = ctx->ins_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	for (i = 0; i < ctx->entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		entry = ctx->action_table + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		ins = entry->instruction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			*end = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (ins >= ctx->instructions || !ins_table[ins].run) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			pr_warn(FW_WARN APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				"Invalid action table, unknown instruction type: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		rc = func(ctx, entry, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static int pre_map_gar_callback(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				struct acpi_whea_header *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	u8 ins = entry->instruction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return apei_map_generic_address(&entry->register_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * Pre-map all GARs in action table to make it possible to access them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * in NMI handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int apei_exec_pre_map_gars(struct apei_exec_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	int rc, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	rc = apei_exec_for_each_entry(ctx, pre_map_gar_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				      NULL, &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		struct apei_exec_context ctx_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		memcpy(&ctx_unmap, ctx, sizeof(*ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		ctx_unmap.entries = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		apei_exec_post_unmap_gars(&ctx_unmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int post_unmap_gar_callback(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				   struct acpi_whea_header *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				   void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	u8 ins = entry->instruction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		apei_unmap_generic_address(&entry->register_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* Post-unmap all GAR in action table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int apei_exec_post_unmap_gars(struct apei_exec_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return apei_exec_for_each_entry(ctx, post_unmap_gar_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 					NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * Resource management for GARs in APEI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct apei_res {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /* Collect all resources requested, to avoid conflict */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct apei_resources apei_resources_all = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.iomem = LIST_HEAD_INIT(apei_resources_all.iomem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	.ioport = LIST_HEAD_INIT(apei_resources_all.ioport),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int apei_res_add(struct list_head *res_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			unsigned long start, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct apei_res *res, *resn, *res_ins = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	unsigned long end = start + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (end <= start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	list_for_each_entry_safe(res, resn, res_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (res->start > end || res->end < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		else if (end <= res->end && start >= res->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			kfree(res_ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		list_del(&res->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		res->start = start = min(res->start, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		res->end = end = max(res->end, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		kfree(res_ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		res_ins = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (res_ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		list_add(&res_ins->list, res_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		res_ins = kmalloc(sizeof(*res), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		if (!res_ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		res_ins->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		res_ins->end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		list_add(&res_ins->list, res_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static int apei_res_sub(struct list_head *res_list1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			struct list_head *res_list2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct apei_res *res1, *resn1, *res2, *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	res1 = list_entry(res_list1->next, struct apei_res, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	resn1 = list_entry(res1->list.next, struct apei_res, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	while (&res1->list != res_list1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		list_for_each_entry(res2, res_list2, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			if (res1->start >= res2->end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			    res1->end <= res2->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			else if (res1->end <= res2->end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				 res1->start >= res2->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 				list_del(&res1->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				kfree(res1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			} else if (res1->end > res2->end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 				   res1->start < res2->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				res = kmalloc(sizeof(*res), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 					return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 				res->start = res2->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 				res->end = res1->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				res1->end = res2->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				list_add(&res->list, &res1->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				resn1 = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 				if (res1->start < res2->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 					res1->end = res2->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 					res1->start = res2->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		res1 = resn1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		resn1 = list_entry(resn1->list.next, struct apei_res, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static void apei_res_clean(struct list_head *res_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	struct apei_res *res, *resn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	list_for_each_entry_safe(res, resn, res_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		list_del(&res->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		kfree(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) void apei_resources_fini(struct apei_resources *resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	apei_res_clean(&resources->iomem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	apei_res_clean(&resources->ioport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) EXPORT_SYMBOL_GPL(apei_resources_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static int apei_resources_merge(struct apei_resources *resources1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				struct apei_resources *resources2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	struct apei_res *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	list_for_each_entry(res, &resources2->iomem, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		rc = apei_res_add(&resources1->iomem, res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 				  res->end - res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	list_for_each_entry(res, &resources2->ioport, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		rc = apei_res_add(&resources1->ioport, res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 				  res->end - res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int apei_resources_add(struct apei_resources *resources,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		       unsigned long start, unsigned long size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		       bool iomem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (iomem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		return apei_res_add(&resources->iomem, start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		return apei_res_add(&resources->ioport, start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) EXPORT_SYMBOL_GPL(apei_resources_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  * EINJ has two groups of GARs (EINJ table entry and trigger table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  * entry), so common resources are subtracted from the trigger table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  * resources before the second requesting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int apei_resources_sub(struct apei_resources *resources1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		       struct apei_resources *resources2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	rc = apei_res_sub(&resources1->iomem, &resources2->iomem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	return apei_res_sub(&resources1->ioport, &resources2->ioport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) EXPORT_SYMBOL_GPL(apei_resources_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static int apei_get_res_callback(__u64 start, __u64 size, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	struct apei_resources *resources = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	return apei_res_add(&resources->iomem, start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static int apei_get_nvs_resources(struct apei_resources *resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	return acpi_nvs_for_each_region(apei_get_res_callback, resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) int (*arch_apei_filter_addr)(int (*func)(__u64 start, __u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 				     void *data), void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static int apei_get_arch_resources(struct apei_resources *resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	return arch_apei_filter_addr(apei_get_res_callback, resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * IO memory/port resource management mechanism is used to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  * whether memory/port area used by GARs conflicts with normal memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)  * or IO memory/port of devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int apei_resources_request(struct apei_resources *resources,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			   const char *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	struct apei_res *res, *res_bak = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	struct resource *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	struct apei_resources nvs_resources, arch_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	rc = apei_resources_sub(resources, &apei_resources_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	 * Some firmware uses ACPI NVS region, that has been marked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	 * busy, so exclude it from APEI resources to avoid false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	 * conflict.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	apei_resources_init(&nvs_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	rc = apei_get_nvs_resources(&nvs_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		goto nvs_res_fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	rc = apei_resources_sub(resources, &nvs_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		goto nvs_res_fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (arch_apei_filter_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		apei_resources_init(&arch_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		rc = apei_get_arch_resources(&arch_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			goto arch_res_fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		rc = apei_resources_sub(resources, &arch_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			goto arch_res_fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	list_for_each_entry(res, &resources->iomem, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		r = request_mem_region(res->start, res->end - res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 				       desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			pr_err(APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		"Can not request [mem %#010llx-%#010llx] for %s registers\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			       (unsigned long long)res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			       (unsigned long long)res->end - 1, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			res_bak = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			goto err_unmap_iomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	list_for_each_entry(res, &resources->ioport, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		r = request_region(res->start, res->end - res->start, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 			pr_err(APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		"Can not request [io  %#06llx-%#06llx] for %s registers\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			       (unsigned long long)res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			       (unsigned long long)res->end - 1, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			res_bak = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			goto err_unmap_ioport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	rc = apei_resources_merge(&apei_resources_all, resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		pr_err(APEI_PFX "Fail to merge resources!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		goto err_unmap_ioport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	goto arch_res_fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) err_unmap_ioport:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	list_for_each_entry(res, &resources->ioport, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		if (res == res_bak)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		release_region(res->start, res->end - res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	res_bak = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) err_unmap_iomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	list_for_each_entry(res, &resources->iomem, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		if (res == res_bak)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		release_mem_region(res->start, res->end - res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) arch_res_fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (arch_apei_filter_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		apei_resources_fini(&arch_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) nvs_res_fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	apei_resources_fini(&nvs_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) EXPORT_SYMBOL_GPL(apei_resources_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) void apei_resources_release(struct apei_resources *resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	struct apei_res *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	list_for_each_entry(res, &resources->iomem, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		release_mem_region(res->start, res->end - res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	list_for_each_entry(res, &resources->ioport, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		release_region(res->start, res->end - res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	rc = apei_resources_sub(&apei_resources_all, resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		pr_err(APEI_PFX "Fail to sub resources!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) EXPORT_SYMBOL_GPL(apei_resources_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 				u32 *access_bit_width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	u32 bit_width, bit_offset, access_size_code, space_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	bit_width = reg->bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	bit_offset = reg->bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	access_size_code = reg->access_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	space_id = reg->space_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	*paddr = get_unaligned(&reg->address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	if (!*paddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		pr_warn(FW_BUG APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 			"Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			*paddr, bit_width, bit_offset, access_size_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 			space_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (access_size_code < 1 || access_size_code > 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		pr_warn(FW_BUG APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 			"Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			*paddr, bit_width, bit_offset, access_size_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 			space_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	*access_bit_width = 1UL << (access_size_code + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	/* Fixup common BIOS bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	if (bit_width == 32 && bit_offset == 0 && (*paddr & 0x03) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	    *access_bit_width < 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		*access_bit_width = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	else if (bit_width == 64 && bit_offset == 0 && (*paddr & 0x07) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	    *access_bit_width < 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		*access_bit_width = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	if ((bit_width + bit_offset) > *access_bit_width) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		pr_warn(FW_BUG APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			"Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			*paddr, bit_width, bit_offset, access_size_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			space_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	    space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		pr_warn(FW_BUG APEI_PFX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 			"Invalid address space type in GAR [0x%llx/%u/%u/%u/%u]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 			*paddr, bit_width, bit_offset, access_size_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			space_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int apei_map_generic_address(struct acpi_generic_address *reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	u32 access_bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	u64 address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	rc = apei_check_gar(reg, &address, &access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	/* IO space doesn't need mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	if (!acpi_os_map_generic_address(reg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) EXPORT_SYMBOL_GPL(apei_map_generic_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) /* read GAR in interrupt (including NMI) or process context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) int apei_read(u64 *val, struct acpi_generic_address *reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	u32 access_bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	u64 address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	rc = apei_check_gar(reg, &address, &access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	*val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	switch(reg->space_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		status = acpi_os_read_memory((acpi_physical_address) address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 					       val, access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	case ACPI_ADR_SPACE_SYSTEM_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		status = acpi_os_read_port(address, (u32 *)val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 					   access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) EXPORT_SYMBOL_GPL(apei_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) /* write GAR in interrupt (including NMI) or process context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) int apei_write(u64 val, struct acpi_generic_address *reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	u32 access_bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	u64 address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	rc = apei_check_gar(reg, &address, &access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	switch (reg->space_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 		status = acpi_os_write_memory((acpi_physical_address) address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 						val, access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	case ACPI_ADR_SPACE_SYSTEM_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		status = acpi_os_write_port(address, val, access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) EXPORT_SYMBOL_GPL(apei_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) static int collect_res_callback(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 				struct acpi_whea_header *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 				void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	struct apei_resources *resources = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	struct acpi_generic_address *reg = &entry->register_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	u8 ins = entry->instruction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	u32 access_bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	u64 paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	rc = apei_check_gar(reg, &paddr, &access_bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	switch (reg->space_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		return apei_res_add(&resources->iomem, paddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 				    access_bit_width / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	case ACPI_ADR_SPACE_SYSTEM_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		return apei_res_add(&resources->ioport, paddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 				    access_bit_width / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)  * Same register may be used by multiple instructions in GARs, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)  * resources are collected before requesting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) int apei_exec_collect_resources(struct apei_exec_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 				struct apei_resources *resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	return apei_exec_for_each_entry(ctx, collect_res_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 					resources, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) EXPORT_SYMBOL_GPL(apei_exec_collect_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) struct dentry *apei_get_debugfs_dir(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	static struct dentry *dapei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	if (!dapei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		dapei = debugfs_create_dir("apei", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	return dapei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) EXPORT_SYMBOL_GPL(apei_get_debugfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) int __weak arch_apei_enable_cmcff(struct acpi_hest_header *hest_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 				  void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) EXPORT_SYMBOL_GPL(arch_apei_enable_cmcff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) void __weak arch_apei_report_mem_error(int sev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 				       struct cper_sec_mem_err *mem_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) EXPORT_SYMBOL_GPL(arch_apei_report_mem_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) int apei_osc_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	static u8 whea_uuid_str[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	acpi_handle handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	u32 capbuf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	struct acpi_osc_context context = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		.uuid_str	= whea_uuid_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 		.rev		= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 		.cap.length	= sizeof(capbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 		.cap.pointer	= capbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	capbuf[OSC_SUPPORT_DWORD] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	capbuf[OSC_CONTROL_DWORD] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	    || ACPI_FAILURE(acpi_run_osc(handle, &context)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 		kfree(context.ret.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) EXPORT_SYMBOL_GPL(apei_osc_setup);