Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Qiuxu Zhuo | fe78351 | 2019-03-21 15:13:39 -0700 | [diff] [blame] | 3 | * |
| 4 | * Shared code by both skx_edac and i10nm_edac. Originally split out |
| 5 | * from the skx_edac driver. |
| 6 | * |
| 7 | * This file is linked into both skx_edac and i10nm_edac drivers. In |
| 8 | * order to avoid link errors, this file must be like a pure library |
| 9 | * without including symbols and defines which would otherwise conflict, |
| 10 | * when linked once into a module and into a built-in object, at the |
| 11 | * same time. For example, __this_module symbol references when that |
| 12 | * file is being linked into a built-in object. |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 13 | * |
| 14 | * Copyright (c) 2018, Intel Corporation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/acpi.h> |
| 18 | #include <linux/dmi.h> |
| 19 | #include <linux/adxl.h> |
| 20 | #include <acpi/nfit.h> |
| 21 | #include <asm/mce.h> |
| 22 | #include "edac_module.h" |
| 23 | #include "skx_common.h" |
| 24 | |
| 25 | static const char * const component_names[] = { |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 26 | [INDEX_SOCKET] = "ProcessorSocketId", |
| 27 | [INDEX_MEMCTRL] = "MemoryControllerId", |
| 28 | [INDEX_CHANNEL] = "ChannelId", |
| 29 | [INDEX_DIMM] = "DimmSlotId", |
Qiuxu Zhuo | 14646de | 2022-07-22 16:33:35 -0700 | [diff] [blame] | 30 | [INDEX_CS] = "ChipSelect", |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 31 | [INDEX_NM_MEMCTRL] = "NmMemoryControllerId", |
| 32 | [INDEX_NM_CHANNEL] = "NmChannelId", |
| 33 | [INDEX_NM_DIMM] = "NmDimmSlotId", |
Qiuxu Zhuo | 14646de | 2022-07-22 16:33:35 -0700 | [diff] [blame] | 34 | [INDEX_NM_CS] = "NmChipSelect", |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static int component_indices[ARRAY_SIZE(component_names)]; |
| 38 | static int adxl_component_count; |
| 39 | static const char * const *adxl_component_names; |
| 40 | static u64 *adxl_values; |
| 41 | static char *adxl_msg; |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 42 | static unsigned long adxl_nm_bitmap; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 43 | |
| 44 | static char skx_msg[MSG_SIZE]; |
Qiuxu Zhuo | fe32f36 | 2022-09-01 12:43:08 -0700 | [diff] [blame] | 45 | static skx_decode_f driver_decode; |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 46 | static skx_show_retry_log_f skx_show_retry_rd_err_log; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 47 | static u64 skx_tolm, skx_tohm; |
| 48 | static LIST_HEAD(dev_edac_list); |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 49 | static bool skx_mem_cfg_2lm; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 50 | |
| 51 | int __init skx_adxl_get(void) |
| 52 | { |
| 53 | const char * const *names; |
| 54 | int i, j; |
| 55 | |
| 56 | names = adxl_get_component_names(); |
| 57 | if (!names) { |
| 58 | skx_printk(KERN_NOTICE, "No firmware support for address translation.\n"); |
| 59 | return -ENODEV; |
| 60 | } |
| 61 | |
| 62 | for (i = 0; i < INDEX_MAX; i++) { |
| 63 | for (j = 0; names[j]; j++) { |
| 64 | if (!strcmp(component_names[i], names[j])) { |
| 65 | component_indices[i] = j; |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 66 | |
| 67 | if (i >= INDEX_NM_FIRST) |
| 68 | adxl_nm_bitmap |= 1 << i; |
| 69 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 74 | if (!names[j] && i < INDEX_NM_FIRST) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 75 | goto err; |
| 76 | } |
| 77 | |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 78 | if (skx_mem_cfg_2lm) { |
| 79 | if (!adxl_nm_bitmap) |
| 80 | skx_printk(KERN_NOTICE, "Not enough ADXL components for 2-level memory.\n"); |
| 81 | else |
| 82 | edac_dbg(2, "adxl_nm_bitmap: 0x%lx\n", adxl_nm_bitmap); |
| 83 | } |
| 84 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 85 | adxl_component_names = names; |
| 86 | while (*names++) |
| 87 | adxl_component_count++; |
| 88 | |
| 89 | adxl_values = kcalloc(adxl_component_count, sizeof(*adxl_values), |
| 90 | GFP_KERNEL); |
| 91 | if (!adxl_values) { |
| 92 | adxl_component_count = 0; |
| 93 | return -ENOMEM; |
| 94 | } |
| 95 | |
| 96 | adxl_msg = kzalloc(MSG_SIZE, GFP_KERNEL); |
| 97 | if (!adxl_msg) { |
| 98 | adxl_component_count = 0; |
| 99 | kfree(adxl_values); |
| 100 | return -ENOMEM; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | err: |
| 105 | skx_printk(KERN_ERR, "'%s' is not matched from DSM parameters: ", |
| 106 | component_names[i]); |
| 107 | for (j = 0; names[j]; j++) |
| 108 | skx_printk(KERN_CONT, "%s ", names[j]); |
| 109 | skx_printk(KERN_CONT, "\n"); |
| 110 | |
| 111 | return -ENODEV; |
| 112 | } |
| 113 | |
| 114 | void __exit skx_adxl_put(void) |
| 115 | { |
| 116 | kfree(adxl_values); |
| 117 | kfree(adxl_msg); |
| 118 | } |
| 119 | |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 120 | static bool skx_adxl_decode(struct decoded_addr *res, bool error_in_1st_level_mem) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 121 | { |
Tony Luck | 29b8e84 | 2019-08-15 14:18:59 -0700 | [diff] [blame] | 122 | struct skx_dev *d; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 123 | int i, len = 0; |
| 124 | |
| 125 | if (res->addr >= skx_tohm || (res->addr >= skx_tolm && |
| 126 | res->addr < BIT_ULL(32))) { |
| 127 | edac_dbg(0, "Address 0x%llx out of range\n", res->addr); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if (adxl_decode(res->addr, adxl_values)) { |
| 132 | edac_dbg(0, "Failed to decode 0x%llx\n", res->addr); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | res->socket = (int)adxl_values[component_indices[INDEX_SOCKET]]; |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 137 | if (error_in_1st_level_mem) { |
| 138 | res->imc = (adxl_nm_bitmap & BIT_NM_MEMCTRL) ? |
| 139 | (int)adxl_values[component_indices[INDEX_NM_MEMCTRL]] : -1; |
| 140 | res->channel = (adxl_nm_bitmap & BIT_NM_CHANNEL) ? |
| 141 | (int)adxl_values[component_indices[INDEX_NM_CHANNEL]] : -1; |
| 142 | res->dimm = (adxl_nm_bitmap & BIT_NM_DIMM) ? |
| 143 | (int)adxl_values[component_indices[INDEX_NM_DIMM]] : -1; |
Qiuxu Zhuo | 14646de | 2022-07-22 16:33:35 -0700 | [diff] [blame] | 144 | res->cs = (adxl_nm_bitmap & BIT_NM_CS) ? |
| 145 | (int)adxl_values[component_indices[INDEX_NM_CS]] : -1; |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 146 | } else { |
| 147 | res->imc = (int)adxl_values[component_indices[INDEX_MEMCTRL]]; |
| 148 | res->channel = (int)adxl_values[component_indices[INDEX_CHANNEL]]; |
| 149 | res->dimm = (int)adxl_values[component_indices[INDEX_DIMM]]; |
Qiuxu Zhuo | 14646de | 2022-07-22 16:33:35 -0700 | [diff] [blame] | 150 | res->cs = (int)adxl_values[component_indices[INDEX_CS]]; |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 151 | } |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 152 | |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 153 | if (res->imc > NUM_IMC - 1 || res->imc < 0) { |
Tony Luck | 29b8e84 | 2019-08-15 14:18:59 -0700 | [diff] [blame] | 154 | skx_printk(KERN_ERR, "Bad imc %d\n", res->imc); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | list_for_each_entry(d, &dev_edac_list, list) { |
| 159 | if (d->imc[0].src_id == res->socket) { |
| 160 | res->dev = d; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (!res->dev) { |
| 166 | skx_printk(KERN_ERR, "No device for src_id %d imc %d\n", |
| 167 | res->socket, res->imc); |
| 168 | return false; |
| 169 | } |
| 170 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 171 | for (i = 0; i < adxl_component_count; i++) { |
| 172 | if (adxl_values[i] == ~0x0ull) |
| 173 | continue; |
| 174 | |
| 175 | len += snprintf(adxl_msg + len, MSG_SIZE - len, " %s:0x%llx", |
| 176 | adxl_component_names[i], adxl_values[i]); |
| 177 | if (MSG_SIZE - len <= 0) |
| 178 | break; |
| 179 | } |
| 180 | |
Qiuxu Zhuo | fe32f36 | 2022-09-01 12:43:08 -0700 | [diff] [blame] | 181 | res->decoded_by_adxl = true; |
| 182 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 186 | void skx_set_mem_cfg(bool mem_cfg_2lm) |
| 187 | { |
| 188 | skx_mem_cfg_2lm = mem_cfg_2lm; |
| 189 | } |
| 190 | |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 191 | void skx_set_decode(skx_decode_f decode, skx_show_retry_log_f show_retry_log) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 192 | { |
Qiuxu Zhuo | fe32f36 | 2022-09-01 12:43:08 -0700 | [diff] [blame] | 193 | driver_decode = decode; |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 194 | skx_show_retry_rd_err_log = show_retry_log; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Qiuxu Zhuo | 1dc78f1 | 2019-06-26 14:16:55 +0800 | [diff] [blame] | 197 | int skx_get_src_id(struct skx_dev *d, int off, u8 *id) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 198 | { |
| 199 | u32 reg; |
| 200 | |
Qiuxu Zhuo | 1dc78f1 | 2019-06-26 14:16:55 +0800 | [diff] [blame] | 201 | if (pci_read_config_dword(d->util_all, off, ®)) { |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 202 | skx_printk(KERN_ERR, "Failed to read src id\n"); |
| 203 | return -ENODEV; |
| 204 | } |
| 205 | |
| 206 | *id = GET_BITFIELD(reg, 12, 14); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int skx_get_node_id(struct skx_dev *d, u8 *id) |
| 211 | { |
| 212 | u32 reg; |
| 213 | |
| 214 | if (pci_read_config_dword(d->util_all, 0xf4, ®)) { |
| 215 | skx_printk(KERN_ERR, "Failed to read node id\n"); |
| 216 | return -ENODEV; |
| 217 | } |
| 218 | |
| 219 | *id = GET_BITFIELD(reg, 0, 2); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int get_width(u32 mtr) |
| 224 | { |
| 225 | switch (GET_BITFIELD(mtr, 8, 9)) { |
| 226 | case 0: |
| 227 | return DEV_X4; |
| 228 | case 1: |
| 229 | return DEV_X8; |
| 230 | case 2: |
| 231 | return DEV_X16; |
| 232 | } |
| 233 | return DEV_UNKNOWN; |
| 234 | } |
| 235 | |
| 236 | /* |
Qiuxu Zhuo | ee5340a | 2020-04-24 20:18:14 +0800 | [diff] [blame] | 237 | * We use the per-socket device @cfg->did to count how many sockets are present, |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 238 | * and to detemine which PCI buses are associated with each socket. Allocate |
| 239 | * and build the full list of all the skx_dev structures that we need here. |
| 240 | */ |
Qiuxu Zhuo | ee5340a | 2020-04-24 20:18:14 +0800 | [diff] [blame] | 241 | int skx_get_all_bus_mappings(struct res_config *cfg, struct list_head **list) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 242 | { |
| 243 | struct pci_dev *pdev, *prev; |
| 244 | struct skx_dev *d; |
| 245 | u32 reg; |
| 246 | int ndev = 0; |
| 247 | |
| 248 | prev = NULL; |
| 249 | for (;;) { |
Qiuxu Zhuo | ee5340a | 2020-04-24 20:18:14 +0800 | [diff] [blame] | 250 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, cfg->decs_did, prev); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 251 | if (!pdev) |
| 252 | break; |
| 253 | ndev++; |
| 254 | d = kzalloc(sizeof(*d), GFP_KERNEL); |
| 255 | if (!d) { |
| 256 | pci_dev_put(pdev); |
| 257 | return -ENOMEM; |
| 258 | } |
| 259 | |
Qiuxu Zhuo | ee5340a | 2020-04-24 20:18:14 +0800 | [diff] [blame] | 260 | if (pci_read_config_dword(pdev, cfg->busno_cfg_offset, ®)) { |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 261 | kfree(d); |
| 262 | pci_dev_put(pdev); |
| 263 | skx_printk(KERN_ERR, "Failed to read bus idx\n"); |
| 264 | return -ENODEV; |
| 265 | } |
| 266 | |
| 267 | d->bus[0] = GET_BITFIELD(reg, 0, 7); |
| 268 | d->bus[1] = GET_BITFIELD(reg, 8, 15); |
Qiuxu Zhuo | ee5340a | 2020-04-24 20:18:14 +0800 | [diff] [blame] | 269 | if (cfg->type == SKX) { |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 270 | d->seg = pci_domain_nr(pdev->bus); |
| 271 | d->bus[2] = GET_BITFIELD(reg, 16, 23); |
| 272 | d->bus[3] = GET_BITFIELD(reg, 24, 31); |
| 273 | } else { |
| 274 | d->seg = GET_BITFIELD(reg, 16, 23); |
| 275 | } |
| 276 | |
| 277 | edac_dbg(2, "busses: 0x%x, 0x%x, 0x%x, 0x%x\n", |
| 278 | d->bus[0], d->bus[1], d->bus[2], d->bus[3]); |
| 279 | list_add_tail(&d->list, &dev_edac_list); |
| 280 | prev = pdev; |
| 281 | } |
| 282 | |
| 283 | if (list) |
| 284 | *list = &dev_edac_list; |
| 285 | return ndev; |
| 286 | } |
| 287 | |
| 288 | int skx_get_hi_lo(unsigned int did, int off[], u64 *tolm, u64 *tohm) |
| 289 | { |
| 290 | struct pci_dev *pdev; |
| 291 | u32 reg; |
| 292 | |
| 293 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, did, NULL); |
| 294 | if (!pdev) { |
Aristeu Rozanski | 854bb48 | 2019-12-04 16:23:25 -0500 | [diff] [blame] | 295 | edac_dbg(2, "Can't get tolm/tohm\n"); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 296 | return -ENODEV; |
| 297 | } |
| 298 | |
| 299 | if (pci_read_config_dword(pdev, off[0], ®)) { |
| 300 | skx_printk(KERN_ERR, "Failed to read tolm\n"); |
| 301 | goto fail; |
| 302 | } |
| 303 | skx_tolm = reg; |
| 304 | |
| 305 | if (pci_read_config_dword(pdev, off[1], ®)) { |
| 306 | skx_printk(KERN_ERR, "Failed to read lower tohm\n"); |
| 307 | goto fail; |
| 308 | } |
| 309 | skx_tohm = reg; |
| 310 | |
| 311 | if (pci_read_config_dword(pdev, off[2], ®)) { |
| 312 | skx_printk(KERN_ERR, "Failed to read upper tohm\n"); |
| 313 | goto fail; |
| 314 | } |
| 315 | skx_tohm |= (u64)reg << 32; |
| 316 | |
| 317 | pci_dev_put(pdev); |
| 318 | *tolm = skx_tolm; |
| 319 | *tohm = skx_tohm; |
| 320 | edac_dbg(2, "tolm = 0x%llx tohm = 0x%llx\n", skx_tolm, skx_tohm); |
| 321 | return 0; |
| 322 | fail: |
| 323 | pci_dev_put(pdev); |
| 324 | return -ENODEV; |
| 325 | } |
| 326 | |
| 327 | static int skx_get_dimm_attr(u32 reg, int lobit, int hibit, int add, |
| 328 | int minval, int maxval, const char *name) |
| 329 | { |
| 330 | u32 val = GET_BITFIELD(reg, lobit, hibit); |
| 331 | |
| 332 | if (val < minval || val > maxval) { |
| 333 | edac_dbg(2, "bad %s = %d (raw=0x%x)\n", name, val, reg); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | return val + add; |
| 337 | } |
| 338 | |
| 339 | #define numrank(reg) skx_get_dimm_attr(reg, 12, 13, 0, 0, 2, "ranks") |
| 340 | #define numrow(reg) skx_get_dimm_attr(reg, 2, 4, 12, 1, 6, "rows") |
| 341 | #define numcol(reg) skx_get_dimm_attr(reg, 0, 1, 10, 0, 2, "cols") |
| 342 | |
Qiuxu Zhuo | 1032095 | 2020-05-15 20:34:06 +0800 | [diff] [blame] | 343 | int skx_get_dimm_info(u32 mtr, u32 mcmtr, u32 amap, struct dimm_info *dimm, |
Qiuxu Zhuo | 479f58d | 2020-11-17 20:49:53 +0800 | [diff] [blame] | 344 | struct skx_imc *imc, int chan, int dimmno, |
| 345 | struct res_config *cfg) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 346 | { |
Qiuxu Zhuo | 479f58d | 2020-11-17 20:49:53 +0800 | [diff] [blame] | 347 | int banks, ranks, rows, cols, npages; |
| 348 | enum mem_type mtype; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 349 | u64 size; |
| 350 | |
| 351 | ranks = numrank(mtr); |
| 352 | rows = numrow(mtr); |
Qiuxu Zhuo | c945088 | 2021-06-11 10:01:20 -0700 | [diff] [blame] | 353 | cols = imc->hbm_mc ? 6 : numcol(mtr); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 354 | |
Qiuxu Zhuo | fd07a4a | 2021-07-20 09:30:09 -0700 | [diff] [blame] | 355 | if (imc->hbm_mc) { |
| 356 | banks = 32; |
| 357 | mtype = MEM_HBM2; |
| 358 | } else if (cfg->support_ddr5 && (amap & 0x8)) { |
Qiuxu Zhuo | 479f58d | 2020-11-17 20:49:53 +0800 | [diff] [blame] | 359 | banks = 32; |
| 360 | mtype = MEM_DDR5; |
| 361 | } else { |
| 362 | banks = 16; |
| 363 | mtype = MEM_DDR4; |
| 364 | } |
| 365 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 366 | /* |
| 367 | * Compute size in 8-byte (2^3) words, then shift to MiB (2^20) |
| 368 | */ |
| 369 | size = ((1ull << (rows + cols + ranks)) * banks) >> (20 - 3); |
| 370 | npages = MiB_TO_PAGES(size); |
| 371 | |
| 372 | edac_dbg(0, "mc#%d: channel %d, dimm %d, %lld MiB (%d pages) bank: %d, rank: %d, row: 0x%x, col: 0x%x\n", |
| 373 | imc->mc, chan, dimmno, size, npages, |
| 374 | banks, 1 << ranks, rows, cols); |
| 375 | |
Qiuxu Zhuo | 1032095 | 2020-05-15 20:34:06 +0800 | [diff] [blame] | 376 | imc->chan[chan].dimms[dimmno].close_pg = GET_BITFIELD(mcmtr, 0, 0); |
| 377 | imc->chan[chan].dimms[dimmno].bank_xor_enable = GET_BITFIELD(mcmtr, 9, 9); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 378 | imc->chan[chan].dimms[dimmno].fine_grain_bank = GET_BITFIELD(amap, 0, 0); |
| 379 | imc->chan[chan].dimms[dimmno].rowbits = rows; |
| 380 | imc->chan[chan].dimms[dimmno].colbits = cols; |
| 381 | |
| 382 | dimm->nr_pages = npages; |
| 383 | dimm->grain = 32; |
| 384 | dimm->dtype = get_width(mtr); |
Qiuxu Zhuo | 479f58d | 2020-11-17 20:49:53 +0800 | [diff] [blame] | 385 | dimm->mtype = mtype; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 386 | dimm->edac_mode = EDAC_SECDED; /* likely better than this */ |
Qiuxu Zhuo | c945088 | 2021-06-11 10:01:20 -0700 | [diff] [blame] | 387 | |
| 388 | if (imc->hbm_mc) |
| 389 | snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_HBMC#%u_Chan#%u", |
| 390 | imc->src_id, imc->lmc, chan); |
| 391 | else |
| 392 | snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_MC#%u_Chan#%u_DIMM#%u", |
| 393 | imc->src_id, imc->lmc, chan, dimmno); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 394 | |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | int skx_get_nvdimm_info(struct dimm_info *dimm, struct skx_imc *imc, |
| 399 | int chan, int dimmno, const char *mod_str) |
| 400 | { |
| 401 | int smbios_handle; |
| 402 | u32 dev_handle; |
| 403 | u16 flags; |
| 404 | u64 size = 0; |
| 405 | |
| 406 | dev_handle = ACPI_NFIT_BUILD_DEVICE_HANDLE(dimmno, chan, imc->lmc, |
| 407 | imc->src_id, 0); |
| 408 | |
| 409 | smbios_handle = nfit_get_smbios_id(dev_handle, &flags); |
| 410 | if (smbios_handle == -EOPNOTSUPP) { |
| 411 | pr_warn_once("%s: Can't find size of NVDIMM. Try enabling CONFIG_ACPI_NFIT\n", mod_str); |
| 412 | goto unknown_size; |
| 413 | } |
| 414 | |
| 415 | if (smbios_handle < 0) { |
| 416 | skx_printk(KERN_ERR, "Can't find handle for NVDIMM ADR=0x%x\n", dev_handle); |
| 417 | goto unknown_size; |
| 418 | } |
| 419 | |
| 420 | if (flags & ACPI_NFIT_MEM_MAP_FAILED) { |
| 421 | skx_printk(KERN_ERR, "NVDIMM ADR=0x%x is not mapped\n", dev_handle); |
| 422 | goto unknown_size; |
| 423 | } |
| 424 | |
| 425 | size = dmi_memdev_size(smbios_handle); |
| 426 | if (size == ~0ull) |
| 427 | skx_printk(KERN_ERR, "Can't find size for NVDIMM ADR=0x%x/SMBIOS=0x%x\n", |
| 428 | dev_handle, smbios_handle); |
| 429 | |
| 430 | unknown_size: |
| 431 | dimm->nr_pages = size >> PAGE_SHIFT; |
| 432 | dimm->grain = 32; |
| 433 | dimm->dtype = DEV_UNKNOWN; |
| 434 | dimm->mtype = MEM_NVDIMM; |
| 435 | dimm->edac_mode = EDAC_SECDED; /* likely better than this */ |
| 436 | |
| 437 | edac_dbg(0, "mc#%d: channel %d, dimm %d, %llu MiB (%u pages)\n", |
| 438 | imc->mc, chan, dimmno, size >> 20, dimm->nr_pages); |
| 439 | |
| 440 | snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_MC#%u_Chan#%u_DIMM#%u", |
| 441 | imc->src_id, imc->lmc, chan, dimmno); |
| 442 | |
| 443 | return (size == 0 || size == ~0ull) ? 0 : 1; |
| 444 | } |
| 445 | |
| 446 | int skx_register_mci(struct skx_imc *imc, struct pci_dev *pdev, |
| 447 | const char *ctl_name, const char *mod_str, |
Qiuxu Zhuo | 479f58d | 2020-11-17 20:49:53 +0800 | [diff] [blame] | 448 | get_dimm_config_f get_dimm_config, |
| 449 | struct res_config *cfg) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 450 | { |
| 451 | struct mem_ctl_info *mci; |
| 452 | struct edac_mc_layer layers[2]; |
| 453 | struct skx_pvt *pvt; |
| 454 | int rc; |
| 455 | |
| 456 | /* Allocate a new MC control structure */ |
| 457 | layers[0].type = EDAC_MC_LAYER_CHANNEL; |
| 458 | layers[0].size = NUM_CHANNELS; |
| 459 | layers[0].is_virt_csrow = false; |
| 460 | layers[1].type = EDAC_MC_LAYER_SLOT; |
| 461 | layers[1].size = NUM_DIMMS; |
| 462 | layers[1].is_virt_csrow = true; |
| 463 | mci = edac_mc_alloc(imc->mc, ARRAY_SIZE(layers), layers, |
| 464 | sizeof(struct skx_pvt)); |
| 465 | |
| 466 | if (unlikely(!mci)) |
| 467 | return -ENOMEM; |
| 468 | |
| 469 | edac_dbg(0, "MC#%d: mci = %p\n", imc->mc, mci); |
| 470 | |
| 471 | /* Associate skx_dev and mci for future usage */ |
| 472 | imc->mci = mci; |
| 473 | pvt = mci->pvt_info; |
| 474 | pvt->imc = imc; |
| 475 | |
| 476 | mci->ctl_name = kasprintf(GFP_KERNEL, "%s#%d IMC#%d", ctl_name, |
| 477 | imc->node_id, imc->lmc); |
| 478 | if (!mci->ctl_name) { |
| 479 | rc = -ENOMEM; |
| 480 | goto fail0; |
| 481 | } |
| 482 | |
| 483 | mci->mtype_cap = MEM_FLAG_DDR4 | MEM_FLAG_NVDIMM; |
Qiuxu Zhuo | 479f58d | 2020-11-17 20:49:53 +0800 | [diff] [blame] | 484 | if (cfg->support_ddr5) |
| 485 | mci->mtype_cap |= MEM_FLAG_DDR5; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 486 | mci->edac_ctl_cap = EDAC_FLAG_NONE; |
| 487 | mci->edac_cap = EDAC_FLAG_NONE; |
| 488 | mci->mod_name = mod_str; |
| 489 | mci->dev_name = pci_name(pdev); |
| 490 | mci->ctl_page_to_phys = NULL; |
| 491 | |
Qiuxu Zhuo | 479f58d | 2020-11-17 20:49:53 +0800 | [diff] [blame] | 492 | rc = get_dimm_config(mci, cfg); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 493 | if (rc < 0) |
| 494 | goto fail; |
| 495 | |
| 496 | /* Record ptr to the generic device */ |
| 497 | mci->pdev = &pdev->dev; |
| 498 | |
| 499 | /* Add this new MC control structure to EDAC's list of MCs */ |
| 500 | if (unlikely(edac_mc_add_mc(mci))) { |
| 501 | edac_dbg(0, "MC: failed edac_mc_add_mc()\n"); |
| 502 | rc = -EINVAL; |
| 503 | goto fail; |
| 504 | } |
| 505 | |
| 506 | return 0; |
| 507 | |
| 508 | fail: |
| 509 | kfree(mci->ctl_name); |
| 510 | fail0: |
| 511 | edac_mc_free(mci); |
| 512 | imc->mci = NULL; |
| 513 | return rc; |
| 514 | } |
| 515 | |
| 516 | static void skx_unregister_mci(struct skx_imc *imc) |
| 517 | { |
| 518 | struct mem_ctl_info *mci = imc->mci; |
| 519 | |
| 520 | if (!mci) |
| 521 | return; |
| 522 | |
| 523 | edac_dbg(0, "MC%d: mci = %p\n", imc->mc, mci); |
| 524 | |
| 525 | /* Remove MC sysfs nodes */ |
| 526 | edac_mc_del_mc(mci->pdev); |
| 527 | |
| 528 | edac_dbg(1, "%s: free mci struct\n", mci->ctl_name); |
| 529 | kfree(mci->ctl_name); |
| 530 | edac_mc_free(mci); |
| 531 | } |
| 532 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 533 | static void skx_mce_output_error(struct mem_ctl_info *mci, |
| 534 | const struct mce *m, |
| 535 | struct decoded_addr *res) |
| 536 | { |
| 537 | enum hw_event_mc_err_type tp_event; |
Mauro Carvalho Chehab | f05390d | 2019-09-13 11:23:08 -0300 | [diff] [blame] | 538 | char *optype; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 539 | bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0); |
| 540 | bool overflow = GET_BITFIELD(m->status, 62, 62); |
| 541 | bool uncorrected_error = GET_BITFIELD(m->status, 61, 61); |
Youquan Song | cf4e6d5 | 2021-08-18 10:57:01 -0700 | [diff] [blame] | 542 | bool scrub_err = false; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 543 | bool recoverable; |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 544 | int len; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 545 | u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52); |
| 546 | u32 mscod = GET_BITFIELD(m->status, 16, 31); |
| 547 | u32 errcode = GET_BITFIELD(m->status, 0, 15); |
| 548 | u32 optypenum = GET_BITFIELD(m->status, 4, 6); |
| 549 | |
| 550 | recoverable = GET_BITFIELD(m->status, 56, 56); |
| 551 | |
| 552 | if (uncorrected_error) { |
| 553 | core_err_cnt = 1; |
| 554 | if (ripv) { |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 555 | tp_event = HW_EVENT_ERR_UNCORRECTED; |
Tony Luck | 45bc609 | 2020-07-07 12:43:24 -0700 | [diff] [blame] | 556 | } else { |
| 557 | tp_event = HW_EVENT_ERR_FATAL; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 558 | } |
| 559 | } else { |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 560 | tp_event = HW_EVENT_ERR_CORRECTED; |
| 561 | } |
| 562 | |
| 563 | /* |
Tony Luck | cbfa482 | 2019-02-05 10:21:09 -0800 | [diff] [blame] | 564 | * According to Intel Architecture spec vol 3B, |
| 565 | * Table 15-10 "IA32_MCi_Status [15:0] Compound Error Code Encoding" |
| 566 | * memory errors should fit one of these masks: |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 567 | * 000f 0000 1mmm cccc (binary) |
Tony Luck | cbfa482 | 2019-02-05 10:21:09 -0800 | [diff] [blame] | 568 | * 000f 0010 1mmm cccc (binary) [RAM used as cache] |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 569 | * where: |
| 570 | * f = Correction Report Filtering Bit. If 1, subsequent errors |
| 571 | * won't be shown |
| 572 | * mmm = error type |
| 573 | * cccc = channel |
| 574 | * If the mask doesn't match, report an error to the parsing logic |
| 575 | */ |
Tony Luck | cbfa482 | 2019-02-05 10:21:09 -0800 | [diff] [blame] | 576 | if (!((errcode & 0xef80) == 0x80 || (errcode & 0xef80) == 0x280)) { |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 577 | optype = "Can't parse: it is not a mem"; |
| 578 | } else { |
| 579 | switch (optypenum) { |
| 580 | case 0: |
| 581 | optype = "generic undef request error"; |
| 582 | break; |
| 583 | case 1: |
| 584 | optype = "memory read error"; |
| 585 | break; |
| 586 | case 2: |
| 587 | optype = "memory write error"; |
| 588 | break; |
| 589 | case 3: |
| 590 | optype = "addr/cmd error"; |
| 591 | break; |
| 592 | case 4: |
| 593 | optype = "memory scrubbing error"; |
Youquan Song | cf4e6d5 | 2021-08-18 10:57:01 -0700 | [diff] [blame] | 594 | scrub_err = true; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 595 | break; |
| 596 | default: |
| 597 | optype = "reserved"; |
| 598 | break; |
| 599 | } |
| 600 | } |
Qiuxu Zhuo | fe32f36 | 2022-09-01 12:43:08 -0700 | [diff] [blame] | 601 | if (res->decoded_by_adxl) { |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 602 | len = snprintf(skx_msg, MSG_SIZE, "%s%s err_code:0x%04x:0x%04x %s", |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 603 | overflow ? " OVERFLOW" : "", |
| 604 | (uncorrected_error && recoverable) ? " recoverable" : "", |
| 605 | mscod, errcode, adxl_msg); |
| 606 | } else { |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 607 | len = snprintf(skx_msg, MSG_SIZE, |
Qiuxu Zhuo | 627d551 | 2022-09-01 12:43:09 -0700 | [diff] [blame] | 608 | "%s%s err_code:0x%04x:0x%04x ProcessorSocketId:0x%x MemoryControllerId:0x%x PhysicalRankId:0x%x Row:0x%x Column:0x%x Bank:0x%x BankGroup:0x%x", |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 609 | overflow ? " OVERFLOW" : "", |
| 610 | (uncorrected_error && recoverable) ? " recoverable" : "", |
| 611 | mscod, errcode, |
| 612 | res->socket, res->imc, res->rank, |
Qiuxu Zhuo | 627d551 | 2022-09-01 12:43:09 -0700 | [diff] [blame] | 613 | res->row, res->column, res->bank_address, res->bank_group); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 616 | if (skx_show_retry_rd_err_log) |
Youquan Song | cf4e6d5 | 2021-08-18 10:57:01 -0700 | [diff] [blame] | 617 | skx_show_retry_rd_err_log(res, skx_msg + len, MSG_SIZE - len, scrub_err); |
Tony Luck | e80634a | 2019-08-15 14:53:28 -0700 | [diff] [blame] | 618 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 619 | edac_dbg(0, "%s\n", skx_msg); |
| 620 | |
| 621 | /* Call the helper to output message */ |
| 622 | edac_mc_handle_error(tp_event, mci, core_err_cnt, |
| 623 | m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0, |
| 624 | res->channel, res->dimm, -1, |
| 625 | optype, skx_msg); |
| 626 | } |
| 627 | |
Qiuxu Zhuo | 2f4348e | 2021-06-11 10:01:18 -0700 | [diff] [blame] | 628 | static bool skx_error_in_1st_level_mem(const struct mce *m) |
| 629 | { |
| 630 | u32 errcode; |
| 631 | |
| 632 | if (!skx_mem_cfg_2lm) |
| 633 | return false; |
| 634 | |
| 635 | errcode = GET_BITFIELD(m->status, 0, 15); |
| 636 | |
| 637 | if ((errcode & 0xef80) != 0x280) |
| 638 | return false; |
| 639 | |
| 640 | return true; |
| 641 | } |
| 642 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 643 | int skx_mce_check_error(struct notifier_block *nb, unsigned long val, |
| 644 | void *data) |
| 645 | { |
| 646 | struct mce *mce = (struct mce *)data; |
| 647 | struct decoded_addr res; |
| 648 | struct mem_ctl_info *mci; |
| 649 | char *type; |
| 650 | |
Tony Luck | 23ba710 | 2020-02-14 14:27:17 -0800 | [diff] [blame] | 651 | if (mce->kflags & MCE_HANDLED_CEC) |
| 652 | return NOTIFY_DONE; |
| 653 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 654 | /* ignore unless this is memory related with an address */ |
| 655 | if ((mce->status & 0xefff) >> 7 != 1 || !(mce->status & MCI_STATUS_ADDRV)) |
| 656 | return NOTIFY_DONE; |
| 657 | |
| 658 | memset(&res, 0, sizeof(res)); |
Youquan Song | 2738c69 | 2022-09-01 12:43:10 -0700 | [diff] [blame] | 659 | res.mce = mce; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 660 | res.addr = mce->addr; |
| 661 | |
Qiuxu Zhuo | fe32f36 | 2022-09-01 12:43:08 -0700 | [diff] [blame] | 662 | /* Try driver decoder first */ |
| 663 | if (!(driver_decode && driver_decode(&res))) { |
| 664 | /* Then try firmware decoder (ACPI DSM methods) */ |
| 665 | if (!(adxl_component_count && skx_adxl_decode(&res, skx_error_in_1st_level_mem(mce)))) |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 666 | return NOTIFY_DONE; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Tony Luck | 29b8e84 | 2019-08-15 14:18:59 -0700 | [diff] [blame] | 669 | mci = res.dev->imc[res.imc].mci; |
| 670 | |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 671 | if (!mci) |
| 672 | return NOTIFY_DONE; |
| 673 | |
| 674 | if (mce->mcgstatus & MCG_STATUS_MCIP) |
| 675 | type = "Exception"; |
| 676 | else |
| 677 | type = "Event"; |
| 678 | |
| 679 | skx_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n"); |
| 680 | |
| 681 | skx_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: 0x%llx " |
| 682 | "Bank %d: 0x%llx\n", mce->extcpu, type, |
| 683 | mce->mcgstatus, mce->bank, mce->status); |
| 684 | skx_mc_printk(mci, KERN_DEBUG, "TSC 0x%llx ", mce->tsc); |
| 685 | skx_mc_printk(mci, KERN_DEBUG, "ADDR 0x%llx ", mce->addr); |
| 686 | skx_mc_printk(mci, KERN_DEBUG, "MISC 0x%llx ", mce->misc); |
| 687 | |
| 688 | skx_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:0x%x TIME %llu SOCKET " |
| 689 | "%u APIC 0x%x\n", mce->cpuvendor, mce->cpuid, |
| 690 | mce->time, mce->socketid, mce->apicid); |
| 691 | |
| 692 | skx_mce_output_error(mci, mce, &res); |
| 693 | |
Tony Luck | 23ba710 | 2020-02-14 14:27:17 -0800 | [diff] [blame] | 694 | mce->kflags |= MCE_HANDLED_EDAC; |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 695 | return NOTIFY_DONE; |
| 696 | } |
| 697 | |
| 698 | void skx_remove(void) |
| 699 | { |
| 700 | int i, j; |
| 701 | struct skx_dev *d, *tmp; |
| 702 | |
| 703 | edac_dbg(0, "\n"); |
| 704 | |
| 705 | list_for_each_entry_safe(d, tmp, &dev_edac_list, list) { |
| 706 | list_del(&d->list); |
| 707 | for (i = 0; i < NUM_IMC; i++) { |
| 708 | if (d->imc[i].mci) |
| 709 | skx_unregister_mci(&d->imc[i]); |
| 710 | |
| 711 | if (d->imc[i].mdev) |
| 712 | pci_dev_put(d->imc[i].mdev); |
| 713 | |
| 714 | if (d->imc[i].mbase) |
| 715 | iounmap(d->imc[i].mbase); |
| 716 | |
| 717 | for (j = 0; j < NUM_CHANNELS; j++) { |
| 718 | if (d->imc[i].chan[j].cdev) |
| 719 | pci_dev_put(d->imc[i].chan[j].cdev); |
| 720 | } |
| 721 | } |
| 722 | if (d->util_all) |
| 723 | pci_dev_put(d->util_all); |
Qiuxu Zhuo | c945088 | 2021-06-11 10:01:20 -0700 | [diff] [blame] | 724 | if (d->pcu_cr3) |
| 725 | pci_dev_put(d->pcu_cr3); |
Qiuxu Zhuo | 88a242c | 2019-01-30 11:15:17 -0800 | [diff] [blame] | 726 | if (d->sad_all) |
| 727 | pci_dev_put(d->sad_all); |
| 728 | if (d->uracu) |
| 729 | pci_dev_put(d->uracu); |
| 730 | |
| 731 | kfree(d); |
| 732 | } |
| 733 | } |