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> |
| 14 | #include <asm/cpu.h> |
| 15 | #include <asm/mce.h> |
| 16 | |
| 17 | #include "apei/apei-internal.h" |
| 18 | |
| 19 | #define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */ |
| 20 | |
| 21 | #define EXTLOG_DSM_REV 0x0 |
| 22 | #define EXTLOG_FN_QUERY 0x0 |
| 23 | #define EXTLOG_FN_ADDR 0x1 |
| 24 | |
| 25 | #define FLAG_OS_OPTIN BIT(0) |
| 26 | #define EXTLOG_QUERY_L1_EXIST BIT(1) |
| 27 | #define ELOG_ENTRY_VALID (1ULL<<63) |
| 28 | #define ELOG_ENTRY_LEN 0x1000 |
| 29 | |
| 30 | #define EMCA_BUG \ |
| 31 | "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n" |
| 32 | |
| 33 | struct extlog_l1_head { |
| 34 | u32 ver; /* Header Version */ |
| 35 | u32 hdr_len; /* Header Length */ |
| 36 | u64 total_len; /* entire L1 Directory length including this header */ |
| 37 | u64 elog_base; /* MCA Error Log Directory base address */ |
| 38 | u64 elog_len; /* MCA Error Log Directory length */ |
| 39 | u32 flags; /* bit 0 - OS/VMM Opt-in */ |
| 40 | u8 rev0[12]; |
| 41 | u32 entries; /* Valid L1 Directory entries per logical processor */ |
| 42 | u8 rev1[12]; |
| 43 | }; |
| 44 | |
| 45 | static u8 extlog_dsm_uuid[] = "663E35AF-CC10-41A4-88EA-5470AF055295"; |
| 46 | |
| 47 | /* L1 table related physical address */ |
| 48 | static u64 elog_base; |
| 49 | static size_t elog_size; |
| 50 | static u64 l1_dirbase; |
| 51 | static size_t l1_size; |
| 52 | |
| 53 | /* L1 table related virtual address */ |
| 54 | static void __iomem *extlog_l1_addr; |
| 55 | static void __iomem *elog_addr; |
| 56 | |
| 57 | static void *elog_buf; |
| 58 | |
| 59 | static u64 *l1_entry_base; |
| 60 | static u32 l1_percpu_entry; |
| 61 | |
| 62 | #define ELOG_IDX(cpu, bank) \ |
| 63 | (cpu_physical_id(cpu) * l1_percpu_entry + (bank)) |
| 64 | |
| 65 | #define ELOG_ENTRY_DATA(idx) \ |
| 66 | (*(l1_entry_base + (idx))) |
| 67 | |
| 68 | #define ELOG_ENTRY_ADDR(phyaddr) \ |
| 69 | (phyaddr - elog_base + (u8 *)elog_addr) |
| 70 | |
| 71 | static struct acpi_generic_status *extlog_elog_entry_check(int cpu, int bank) |
| 72 | { |
| 73 | int idx; |
| 74 | u64 data; |
| 75 | struct acpi_generic_status *estatus; |
| 76 | |
| 77 | WARN_ON(cpu < 0); |
| 78 | idx = ELOG_IDX(cpu, bank); |
| 79 | data = ELOG_ENTRY_DATA(idx); |
| 80 | if ((data & ELOG_ENTRY_VALID) == 0) |
| 81 | return NULL; |
| 82 | |
| 83 | data &= EXT_ELOG_ENTRY_MASK; |
| 84 | estatus = (struct acpi_generic_status *)ELOG_ENTRY_ADDR(data); |
| 85 | |
| 86 | /* if no valid data in elog entry, just return */ |
| 87 | if (estatus->block_status == 0) |
| 88 | return NULL; |
| 89 | |
| 90 | return estatus; |
| 91 | } |
| 92 | |
| 93 | static void __print_extlog_rcd(const char *pfx, |
| 94 | struct acpi_generic_status *estatus, int cpu) |
| 95 | { |
| 96 | static atomic_t seqno; |
| 97 | unsigned int curr_seqno; |
| 98 | char pfx_seq[64]; |
| 99 | |
| 100 | if (!pfx) { |
| 101 | if (estatus->error_severity <= CPER_SEV_CORRECTED) |
| 102 | pfx = KERN_INFO; |
| 103 | else |
| 104 | pfx = KERN_ERR; |
| 105 | } |
| 106 | curr_seqno = atomic_inc_return(&seqno); |
| 107 | snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno); |
| 108 | printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu); |
| 109 | cper_estatus_print(pfx_seq, estatus); |
| 110 | } |
| 111 | |
| 112 | static int print_extlog_rcd(const char *pfx, |
| 113 | struct acpi_generic_status *estatus, int cpu) |
| 114 | { |
| 115 | /* Not more than 2 messages every 5 seconds */ |
| 116 | static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2); |
| 117 | static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2); |
| 118 | struct ratelimit_state *ratelimit; |
| 119 | |
| 120 | if (estatus->error_severity == CPER_SEV_CORRECTED || |
| 121 | (estatus->error_severity == CPER_SEV_INFORMATIONAL)) |
| 122 | ratelimit = &ratelimit_corrected; |
| 123 | else |
| 124 | ratelimit = &ratelimit_uncorrected; |
| 125 | if (__ratelimit(ratelimit)) { |
| 126 | __print_extlog_rcd(pfx, estatus, cpu); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | static int extlog_print(struct notifier_block *nb, unsigned long val, |
| 134 | void *data) |
| 135 | { |
| 136 | struct mce *mce = (struct mce *)data; |
| 137 | int bank = mce->bank; |
| 138 | int cpu = mce->extcpu; |
| 139 | struct acpi_generic_status *estatus; |
| 140 | int rc; |
| 141 | |
| 142 | estatus = extlog_elog_entry_check(cpu, bank); |
| 143 | if (estatus == NULL) |
| 144 | return NOTIFY_DONE; |
| 145 | |
| 146 | memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN); |
| 147 | /* clear record status to enable BIOS to update it again */ |
| 148 | estatus->block_status = 0; |
| 149 | |
| 150 | rc = print_extlog_rcd(NULL, (struct acpi_generic_status *)elog_buf, cpu); |
| 151 | |
| 152 | return NOTIFY_DONE; |
| 153 | } |
| 154 | |
| 155 | static int extlog_get_dsm(acpi_handle handle, int rev, int func, u64 *ret) |
| 156 | { |
| 157 | struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 158 | struct acpi_object_list input; |
| 159 | union acpi_object params[4], *obj; |
| 160 | u8 uuid[16]; |
| 161 | int i; |
| 162 | |
| 163 | acpi_str_to_uuid(extlog_dsm_uuid, uuid); |
| 164 | input.count = 4; |
| 165 | input.pointer = params; |
| 166 | params[0].type = ACPI_TYPE_BUFFER; |
| 167 | params[0].buffer.length = 16; |
| 168 | params[0].buffer.pointer = uuid; |
| 169 | params[1].type = ACPI_TYPE_INTEGER; |
| 170 | params[1].integer.value = rev; |
| 171 | params[2].type = ACPI_TYPE_INTEGER; |
| 172 | params[2].integer.value = func; |
| 173 | params[3].type = ACPI_TYPE_PACKAGE; |
| 174 | params[3].package.count = 0; |
| 175 | params[3].package.elements = NULL; |
| 176 | |
| 177 | if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DSM", &input, &buf))) |
| 178 | return -1; |
| 179 | |
| 180 | *ret = 0; |
| 181 | obj = (union acpi_object *)buf.pointer; |
| 182 | if (obj->type == ACPI_TYPE_INTEGER) { |
| 183 | *ret = obj->integer.value; |
| 184 | } else if (obj->type == ACPI_TYPE_BUFFER) { |
| 185 | if (obj->buffer.length <= 8) { |
| 186 | for (i = 0; i < obj->buffer.length; i++) |
| 187 | *ret |= (obj->buffer.pointer[i] << (i * 8)); |
| 188 | } |
| 189 | } |
| 190 | kfree(buf.pointer); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static bool extlog_get_l1addr(void) |
| 196 | { |
| 197 | acpi_handle handle; |
| 198 | u64 ret; |
| 199 | |
| 200 | if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))) |
| 201 | return false; |
| 202 | |
| 203 | if (extlog_get_dsm(handle, EXTLOG_DSM_REV, EXTLOG_FN_QUERY, &ret) || |
| 204 | !(ret & EXTLOG_QUERY_L1_EXIST)) |
| 205 | return false; |
| 206 | |
| 207 | if (extlog_get_dsm(handle, EXTLOG_DSM_REV, EXTLOG_FN_ADDR, &ret)) |
| 208 | return false; |
| 209 | |
| 210 | l1_dirbase = ret; |
| 211 | /* Spec says L1 directory must be 4K aligned, bail out if it isn't */ |
| 212 | if (l1_dirbase & ((1 << 12) - 1)) { |
| 213 | pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n", |
| 214 | l1_dirbase); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | static struct notifier_block extlog_mce_dec = { |
| 221 | .notifier_call = extlog_print, |
| 222 | }; |
| 223 | |
| 224 | static int __init extlog_init(void) |
| 225 | { |
| 226 | struct extlog_l1_head *l1_head; |
| 227 | void __iomem *extlog_l1_hdr; |
| 228 | size_t l1_hdr_size; |
| 229 | struct resource *r; |
| 230 | u64 cap; |
| 231 | int rc; |
| 232 | |
| 233 | rc = -ENODEV; |
| 234 | |
| 235 | rdmsrl(MSR_IA32_MCG_CAP, cap); |
| 236 | if (!(cap & MCG_ELOG_P)) |
| 237 | return rc; |
| 238 | |
| 239 | if (!extlog_get_l1addr()) |
| 240 | return rc; |
| 241 | |
| 242 | rc = -EINVAL; |
| 243 | /* get L1 header to fetch necessary information */ |
| 244 | l1_hdr_size = sizeof(struct extlog_l1_head); |
| 245 | r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR"); |
| 246 | if (!r) { |
| 247 | pr_warn(FW_BUG EMCA_BUG, |
| 248 | (unsigned long long)l1_dirbase, |
| 249 | (unsigned long long)l1_dirbase + l1_hdr_size); |
| 250 | goto err; |
| 251 | } |
| 252 | |
| 253 | extlog_l1_hdr = acpi_os_map_memory(l1_dirbase, l1_hdr_size); |
| 254 | l1_head = (struct extlog_l1_head *)extlog_l1_hdr; |
| 255 | l1_size = l1_head->total_len; |
| 256 | l1_percpu_entry = l1_head->entries; |
| 257 | elog_base = l1_head->elog_base; |
| 258 | elog_size = l1_head->elog_len; |
| 259 | acpi_os_unmap_memory(extlog_l1_hdr, l1_hdr_size); |
| 260 | release_mem_region(l1_dirbase, l1_hdr_size); |
| 261 | |
| 262 | /* remap L1 header again based on completed information */ |
| 263 | r = request_mem_region(l1_dirbase, l1_size, "L1 Table"); |
| 264 | if (!r) { |
| 265 | pr_warn(FW_BUG EMCA_BUG, |
| 266 | (unsigned long long)l1_dirbase, |
| 267 | (unsigned long long)l1_dirbase + l1_size); |
| 268 | goto err; |
| 269 | } |
| 270 | extlog_l1_addr = acpi_os_map_memory(l1_dirbase, l1_size); |
| 271 | l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size); |
| 272 | |
| 273 | /* remap elog table */ |
| 274 | r = request_mem_region(elog_base, elog_size, "Elog Table"); |
| 275 | if (!r) { |
| 276 | pr_warn(FW_BUG EMCA_BUG, |
| 277 | (unsigned long long)elog_base, |
| 278 | (unsigned long long)elog_base + elog_size); |
| 279 | goto err_release_l1_dir; |
| 280 | } |
| 281 | elog_addr = acpi_os_map_memory(elog_base, elog_size); |
| 282 | |
| 283 | rc = -ENOMEM; |
| 284 | /* allocate buffer to save elog record */ |
| 285 | elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL); |
| 286 | if (elog_buf == NULL) |
| 287 | goto err_release_elog; |
| 288 | |
| 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) |
| 297 | acpi_os_unmap_memory(elog_addr, elog_size); |
| 298 | release_mem_region(elog_base, elog_size); |
| 299 | err_release_l1_dir: |
| 300 | if (extlog_l1_addr) |
| 301 | acpi_os_unmap_memory(extlog_l1_addr, l1_size); |
| 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 | { |
| 310 | mce_unregister_decode_chain(&extlog_mce_dec); |
| 311 | ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN; |
| 312 | if (extlog_l1_addr) |
| 313 | acpi_os_unmap_memory(extlog_l1_addr, l1_size); |
| 314 | if (elog_addr) |
| 315 | acpi_os_unmap_memory(elog_addr, elog_size); |
| 316 | release_mem_region(elog_base, elog_size); |
| 317 | release_mem_region(l1_dirbase, l1_size); |
| 318 | kfree(elog_buf); |
| 319 | } |
| 320 | |
| 321 | module_init(extlog_init); |
| 322 | module_exit(extlog_exit); |
| 323 | |
| 324 | MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>"); |
| 325 | MODULE_DESCRIPTION("Extended MCA Error Log Driver"); |
| 326 | MODULE_LICENSE("GPL"); |