Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Extended Error Log driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Intel Corp. |
| 5 | * Author: Chen, Gong <gong.chen@intel.com> |
| 6 | * |
| 7 | * This file is licensed under GPLv2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/acpi.h> |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 12 | #include <linux/cper.h> |
| 13 | #include <linux/ratelimit.h> |
Chen, Gong | 42139eb | 2013-12-06 01:17:10 -0500 | [diff] [blame] | 14 | #include <linux/edac.h> |
Chen, Gong | d6cae93 | 2014-06-11 04:34:50 -0400 | [diff] [blame] | 15 | #include <linux/ras.h> |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 16 | #include <asm/cpu.h> |
| 17 | #include <asm/mce.h> |
| 18 | |
| 19 | #include "apei/apei-internal.h" |
Chen, Gong | 2dfb7d5 | 2014-06-17 22:33:07 -0400 | [diff] [blame] | 20 | #include <ras/ras_event.h> |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 21 | |
| 22 | #define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */ |
| 23 | |
| 24 | #define EXTLOG_DSM_REV 0x0 |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 25 | #define EXTLOG_FN_ADDR 0x1 |
| 26 | |
| 27 | #define FLAG_OS_OPTIN BIT(0) |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 28 | #define ELOG_ENTRY_VALID (1ULL<<63) |
| 29 | #define ELOG_ENTRY_LEN 0x1000 |
| 30 | |
| 31 | #define EMCA_BUG \ |
| 32 | "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n" |
| 33 | |
| 34 | struct extlog_l1_head { |
| 35 | u32 ver; /* Header Version */ |
| 36 | u32 hdr_len; /* Header Length */ |
| 37 | u64 total_len; /* entire L1 Directory length including this header */ |
| 38 | u64 elog_base; /* MCA Error Log Directory base address */ |
| 39 | u64 elog_len; /* MCA Error Log Directory length */ |
| 40 | u32 flags; /* bit 0 - OS/VMM Opt-in */ |
| 41 | u8 rev0[12]; |
| 42 | u32 entries; /* Valid L1 Directory entries per logical processor */ |
| 43 | u8 rev1[12]; |
| 44 | }; |
| 45 | |
Chen, Gong | 42139eb | 2013-12-06 01:17:10 -0500 | [diff] [blame] | 46 | static int old_edac_report_status; |
| 47 | |
Jiang Liu | 7ede9f8 | 2013-12-19 20:47:46 +0800 | [diff] [blame] | 48 | static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295"; |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 49 | |
| 50 | /* L1 table related physical address */ |
| 51 | static u64 elog_base; |
| 52 | static size_t elog_size; |
| 53 | static u64 l1_dirbase; |
| 54 | static size_t l1_size; |
| 55 | |
| 56 | /* L1 table related virtual address */ |
| 57 | static void __iomem *extlog_l1_addr; |
| 58 | static void __iomem *elog_addr; |
| 59 | |
| 60 | static void *elog_buf; |
| 61 | |
| 62 | static u64 *l1_entry_base; |
| 63 | static u32 l1_percpu_entry; |
| 64 | |
| 65 | #define ELOG_IDX(cpu, bank) \ |
| 66 | (cpu_physical_id(cpu) * l1_percpu_entry + (bank)) |
| 67 | |
| 68 | #define ELOG_ENTRY_DATA(idx) \ |
| 69 | (*(l1_entry_base + (idx))) |
| 70 | |
| 71 | #define ELOG_ENTRY_ADDR(phyaddr) \ |
| 72 | (phyaddr - elog_base + (u8 *)elog_addr) |
| 73 | |
Lv Zheng | 0a00fd5 | 2014-06-03 16:32:53 +0800 | [diff] [blame] | 74 | static struct acpi_hest_generic_status *extlog_elog_entry_check(int cpu, int bank) |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 75 | { |
| 76 | int idx; |
| 77 | u64 data; |
Lv Zheng | 0a00fd5 | 2014-06-03 16:32:53 +0800 | [diff] [blame] | 78 | struct acpi_hest_generic_status *estatus; |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 79 | |
| 80 | WARN_ON(cpu < 0); |
| 81 | idx = ELOG_IDX(cpu, bank); |
| 82 | data = ELOG_ENTRY_DATA(idx); |
| 83 | if ((data & ELOG_ENTRY_VALID) == 0) |
| 84 | return NULL; |
| 85 | |
| 86 | data &= EXT_ELOG_ENTRY_MASK; |
Lv Zheng | 0a00fd5 | 2014-06-03 16:32:53 +0800 | [diff] [blame] | 87 | estatus = (struct acpi_hest_generic_status *)ELOG_ENTRY_ADDR(data); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 88 | |
| 89 | /* if no valid data in elog entry, just return */ |
| 90 | if (estatus->block_status == 0) |
| 91 | return NULL; |
| 92 | |
| 93 | return estatus; |
| 94 | } |
| 95 | |
| 96 | static void __print_extlog_rcd(const char *pfx, |
Lv Zheng | 0a00fd5 | 2014-06-03 16:32:53 +0800 | [diff] [blame] | 97 | struct acpi_hest_generic_status *estatus, int cpu) |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 98 | { |
| 99 | static atomic_t seqno; |
| 100 | unsigned int curr_seqno; |
| 101 | char pfx_seq[64]; |
| 102 | |
| 103 | if (!pfx) { |
| 104 | if (estatus->error_severity <= CPER_SEV_CORRECTED) |
| 105 | pfx = KERN_INFO; |
| 106 | else |
| 107 | pfx = KERN_ERR; |
| 108 | } |
| 109 | curr_seqno = atomic_inc_return(&seqno); |
| 110 | snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno); |
| 111 | printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu); |
| 112 | cper_estatus_print(pfx_seq, estatus); |
| 113 | } |
| 114 | |
| 115 | static int print_extlog_rcd(const char *pfx, |
Lv Zheng | 0a00fd5 | 2014-06-03 16:32:53 +0800 | [diff] [blame] | 116 | struct acpi_hest_generic_status *estatus, int cpu) |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 117 | { |
| 118 | /* Not more than 2 messages every 5 seconds */ |
| 119 | static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2); |
| 120 | static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2); |
| 121 | struct ratelimit_state *ratelimit; |
| 122 | |
| 123 | if (estatus->error_severity == CPER_SEV_CORRECTED || |
| 124 | (estatus->error_severity == CPER_SEV_INFORMATIONAL)) |
| 125 | ratelimit = &ratelimit_corrected; |
| 126 | else |
| 127 | ratelimit = &ratelimit_uncorrected; |
| 128 | if (__ratelimit(ratelimit)) { |
| 129 | __print_extlog_rcd(pfx, estatus, cpu); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | static int extlog_print(struct notifier_block *nb, unsigned long val, |
| 137 | void *data) |
| 138 | { |
| 139 | struct mce *mce = (struct mce *)data; |
| 140 | int bank = mce->bank; |
| 141 | int cpu = mce->extcpu; |
Linus Torvalds | 7725131 | 2014-08-06 20:34:19 -0700 | [diff] [blame] | 142 | struct acpi_hest_generic_status *estatus, *tmp; |
| 143 | struct acpi_hest_generic_data *gdata; |
Chen, Gong | 2dfb7d5 | 2014-06-17 22:33:07 -0400 | [diff] [blame] | 144 | const uuid_le *fru_id = &NULL_UUID_LE; |
| 145 | char *fru_text = ""; |
| 146 | uuid_le *sec_type; |
| 147 | static u32 err_seq; |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 148 | |
| 149 | estatus = extlog_elog_entry_check(cpu, bank); |
| 150 | if (estatus == NULL) |
| 151 | return NOTIFY_DONE; |
| 152 | |
| 153 | memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN); |
| 154 | /* clear record status to enable BIOS to update it again */ |
| 155 | estatus->block_status = 0; |
| 156 | |
Linus Torvalds | 7725131 | 2014-08-06 20:34:19 -0700 | [diff] [blame] | 157 | tmp = (struct acpi_hest_generic_status *)elog_buf; |
Chen, Gong | d6cae93 | 2014-06-11 04:34:50 -0400 | [diff] [blame] | 158 | |
| 159 | if (!ras_userspace_consumers()) { |
| 160 | print_extlog_rcd(NULL, tmp, cpu); |
| 161 | goto out; |
| 162 | } |
Chen, Gong | 2dfb7d5 | 2014-06-17 22:33:07 -0400 | [diff] [blame] | 163 | |
| 164 | /* log event via trace */ |
| 165 | err_seq++; |
Linus Torvalds | 7725131 | 2014-08-06 20:34:19 -0700 | [diff] [blame] | 166 | gdata = (struct acpi_hest_generic_data *)(tmp + 1); |
Chen, Gong | 2dfb7d5 | 2014-06-17 22:33:07 -0400 | [diff] [blame] | 167 | if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID) |
| 168 | fru_id = (uuid_le *)gdata->fru_id; |
| 169 | if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT) |
| 170 | fru_text = gdata->fru_text; |
| 171 | sec_type = (uuid_le *)gdata->section_type; |
| 172 | if (!uuid_le_cmp(*sec_type, CPER_SEC_PLATFORM_MEM)) { |
| 173 | struct cper_sec_mem_err *mem = (void *)(gdata + 1); |
| 174 | if (gdata->error_data_length >= sizeof(*mem)) |
| 175 | trace_extlog_mem_event(mem, err_seq, fru_id, fru_text, |
| 176 | (u8)gdata->error_severity); |
| 177 | } |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 178 | |
Chen, Gong | d6cae93 | 2014-06-11 04:34:50 -0400 | [diff] [blame] | 179 | out: |
Chen, Gong | 42139eb | 2013-12-06 01:17:10 -0500 | [diff] [blame] | 180 | return NOTIFY_STOP; |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jiang Liu | 7ede9f8 | 2013-12-19 20:47:46 +0800 | [diff] [blame] | 183 | static bool __init extlog_get_l1addr(void) |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 184 | { |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 185 | u8 uuid[16]; |
Jiang Liu | 7ede9f8 | 2013-12-19 20:47:46 +0800 | [diff] [blame] | 186 | acpi_handle handle; |
| 187 | union acpi_object *obj; |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 188 | |
| 189 | acpi_str_to_uuid(extlog_dsm_uuid, uuid); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 190 | |
| 191 | if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))) |
| 192 | return false; |
Jiang Liu | 7ede9f8 | 2013-12-19 20:47:46 +0800 | [diff] [blame] | 193 | if (!acpi_check_dsm(handle, uuid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR)) |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 194 | return false; |
Jiang Liu | 7ede9f8 | 2013-12-19 20:47:46 +0800 | [diff] [blame] | 195 | obj = acpi_evaluate_dsm_typed(handle, uuid, EXTLOG_DSM_REV, |
| 196 | EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER); |
| 197 | if (!obj) { |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 198 | return false; |
Jiang Liu | 7ede9f8 | 2013-12-19 20:47:46 +0800 | [diff] [blame] | 199 | } else { |
| 200 | l1_dirbase = obj->integer.value; |
| 201 | ACPI_FREE(obj); |
| 202 | } |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 203 | |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 204 | /* Spec says L1 directory must be 4K aligned, bail out if it isn't */ |
| 205 | if (l1_dirbase & ((1 << 12) - 1)) { |
| 206 | pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n", |
| 207 | l1_dirbase); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | return true; |
| 212 | } |
| 213 | static struct notifier_block extlog_mce_dec = { |
| 214 | .notifier_call = extlog_print, |
| 215 | }; |
| 216 | |
| 217 | static int __init extlog_init(void) |
| 218 | { |
| 219 | struct extlog_l1_head *l1_head; |
| 220 | void __iomem *extlog_l1_hdr; |
| 221 | size_t l1_hdr_size; |
| 222 | struct resource *r; |
| 223 | u64 cap; |
| 224 | int rc; |
| 225 | |
Chen, Gong | 7c76bb5 | 2014-06-11 04:34:51 -0400 | [diff] [blame] | 226 | rdmsrl(MSR_IA32_MCG_CAP, cap); |
| 227 | |
| 228 | if (!(cap & MCG_ELOG_P) || !extlog_get_l1addr()) |
| 229 | return -ENODEV; |
| 230 | |
Chen, Gong | 42139eb | 2013-12-06 01:17:10 -0500 | [diff] [blame] | 231 | if (get_edac_report_status() == EDAC_REPORTING_FORCE) { |
| 232 | pr_warn("Not loading eMCA, error reporting force-enabled through EDAC.\n"); |
| 233 | return -EPERM; |
| 234 | } |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 235 | |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 236 | rc = -EINVAL; |
| 237 | /* get L1 header to fetch necessary information */ |
| 238 | l1_hdr_size = sizeof(struct extlog_l1_head); |
| 239 | r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR"); |
| 240 | if (!r) { |
| 241 | pr_warn(FW_BUG EMCA_BUG, |
| 242 | (unsigned long long)l1_dirbase, |
| 243 | (unsigned long long)l1_dirbase + l1_hdr_size); |
| 244 | goto err; |
| 245 | } |
| 246 | |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 247 | extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 248 | l1_head = (struct extlog_l1_head *)extlog_l1_hdr; |
| 249 | l1_size = l1_head->total_len; |
| 250 | l1_percpu_entry = l1_head->entries; |
| 251 | elog_base = l1_head->elog_base; |
| 252 | elog_size = l1_head->elog_len; |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 253 | acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 254 | release_mem_region(l1_dirbase, l1_hdr_size); |
| 255 | |
| 256 | /* remap L1 header again based on completed information */ |
| 257 | r = request_mem_region(l1_dirbase, l1_size, "L1 Table"); |
| 258 | if (!r) { |
| 259 | pr_warn(FW_BUG EMCA_BUG, |
| 260 | (unsigned long long)l1_dirbase, |
| 261 | (unsigned long long)l1_dirbase + l1_size); |
| 262 | goto err; |
| 263 | } |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 264 | extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 265 | l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size); |
| 266 | |
| 267 | /* remap elog table */ |
| 268 | r = request_mem_region(elog_base, elog_size, "Elog Table"); |
| 269 | if (!r) { |
| 270 | pr_warn(FW_BUG EMCA_BUG, |
| 271 | (unsigned long long)elog_base, |
| 272 | (unsigned long long)elog_base + elog_size); |
| 273 | goto err_release_l1_dir; |
| 274 | } |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 275 | elog_addr = acpi_os_map_iomem(elog_base, elog_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 276 | |
| 277 | rc = -ENOMEM; |
| 278 | /* allocate buffer to save elog record */ |
| 279 | elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL); |
| 280 | if (elog_buf == NULL) |
| 281 | goto err_release_elog; |
| 282 | |
Chen, Gong | 42139eb | 2013-12-06 01:17:10 -0500 | [diff] [blame] | 283 | /* |
| 284 | * eMCA event report method has higher priority than EDAC method, |
| 285 | * unless EDAC event report method is mandatory. |
| 286 | */ |
| 287 | old_edac_report_status = get_edac_report_status(); |
| 288 | set_edac_report_status(EDAC_REPORTING_DISABLED); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 289 | mce_register_decode_chain(&extlog_mce_dec); |
| 290 | /* enable OS to be involved to take over management from BIOS */ |
| 291 | ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN; |
| 292 | |
| 293 | return 0; |
| 294 | |
| 295 | err_release_elog: |
| 296 | if (elog_addr) |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 297 | acpi_os_unmap_iomem(elog_addr, elog_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 298 | release_mem_region(elog_base, elog_size); |
| 299 | err_release_l1_dir: |
| 300 | if (extlog_l1_addr) |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 301 | acpi_os_unmap_iomem(extlog_l1_addr, l1_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 302 | release_mem_region(l1_dirbase, l1_size); |
| 303 | err: |
| 304 | pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n"); |
| 305 | return rc; |
| 306 | } |
| 307 | |
| 308 | static void __exit extlog_exit(void) |
| 309 | { |
Chen, Gong | 42139eb | 2013-12-06 01:17:10 -0500 | [diff] [blame] | 310 | set_edac_report_status(old_edac_report_status); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 311 | mce_unregister_decode_chain(&extlog_mce_dec); |
| 312 | ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN; |
| 313 | if (extlog_l1_addr) |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 314 | acpi_os_unmap_iomem(extlog_l1_addr, l1_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 315 | if (elog_addr) |
Lv Zheng | a238317 | 2014-05-20 15:39:41 +0800 | [diff] [blame] | 316 | acpi_os_unmap_iomem(elog_addr, elog_size); |
Chen, Gong | 4b3db70 | 2013-10-21 14:29:25 -0700 | [diff] [blame] | 317 | release_mem_region(elog_base, elog_size); |
| 318 | release_mem_region(l1_dirbase, l1_size); |
| 319 | kfree(elog_buf); |
| 320 | } |
| 321 | |
| 322 | module_init(extlog_init); |
| 323 | module_exit(extlog_exit); |
| 324 | |
| 325 | MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>"); |
| 326 | MODULE_DESCRIPTION("Extended MCA Error Log Driver"); |
| 327 | MODULE_LICENSE("GPL"); |