Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[linux/fpc-iii.git] / drivers / edac / skx_edac.c
blobfae095162c0175d860bfb9669b6036e4773282fd
1 /*
2 * EDAC driver for Intel(R) Xeon(R) Skylake processors
3 * Copyright (c) 2016, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/acpi.h>
18 #include <linux/dmi.h>
19 #include <linux/pci.h>
20 #include <linux/pci_ids.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/edac.h>
24 #include <linux/mmzone.h>
25 #include <linux/smp.h>
26 #include <linux/bitmap.h>
27 #include <linux/math64.h>
28 #include <linux/mod_devicetable.h>
29 #include <acpi/nfit.h>
30 #include <asm/cpu_device_id.h>
31 #include <asm/intel-family.h>
32 #include <asm/processor.h>
33 #include <asm/mce.h>
35 #include "edac_module.h"
37 #define EDAC_MOD_STR "skx_edac"
40 * Debug macros
42 #define skx_printk(level, fmt, arg...) \
43 edac_printk(level, "skx", fmt, ##arg)
45 #define skx_mc_printk(mci, level, fmt, arg...) \
46 edac_mc_chipset_printk(mci, level, "skx", fmt, ##arg)
49 * Get a bit field at register value <v>, from bit <lo> to bit <hi>
51 #define GET_BITFIELD(v, lo, hi) \
52 (((v) & GENMASK_ULL((hi), (lo))) >> (lo))
54 static LIST_HEAD(skx_edac_list);
56 static u64 skx_tolm, skx_tohm;
58 #define NUM_IMC 2 /* memory controllers per socket */
59 #define NUM_CHANNELS 3 /* channels per memory controller */
60 #define NUM_DIMMS 2 /* Max DIMMS per channel */
62 #define MASK26 0x3FFFFFF /* Mask for 2^26 */
63 #define MASK29 0x1FFFFFFF /* Mask for 2^29 */
66 * Each cpu socket contains some pci devices that provide global
67 * information, and also some that are local to each of the two
68 * memory controllers on the die.
70 struct skx_dev {
71 struct list_head list;
72 u8 bus[4];
73 int seg;
74 struct pci_dev *sad_all;
75 struct pci_dev *util_all;
76 u32 mcroute;
77 struct skx_imc {
78 struct mem_ctl_info *mci;
79 u8 mc; /* system wide mc# */
80 u8 lmc; /* socket relative mc# */
81 u8 src_id, node_id;
82 struct skx_channel {
83 struct pci_dev *cdev;
84 struct skx_dimm {
85 u8 close_pg;
86 u8 bank_xor_enable;
87 u8 fine_grain_bank;
88 u8 rowbits;
89 u8 colbits;
90 } dimms[NUM_DIMMS];
91 } chan[NUM_CHANNELS];
92 } imc[NUM_IMC];
94 static int skx_num_sockets;
96 struct skx_pvt {
97 struct skx_imc *imc;
100 struct decoded_addr {
101 struct skx_dev *dev;
102 u64 addr;
103 int socket;
104 int imc;
105 int channel;
106 u64 chan_addr;
107 int sktways;
108 int chanways;
109 int dimm;
110 int rank;
111 int channel_rank;
112 u64 rank_address;
113 int row;
114 int column;
115 int bank_address;
116 int bank_group;
119 static struct skx_dev *get_skx_dev(struct pci_bus *bus, u8 idx)
121 struct skx_dev *d;
123 list_for_each_entry(d, &skx_edac_list, list) {
124 if (d->seg == pci_domain_nr(bus) && d->bus[idx] == bus->number)
125 return d;
128 return NULL;
131 enum munittype {
132 CHAN0, CHAN1, CHAN2, SAD_ALL, UTIL_ALL, SAD
135 struct munit {
136 u16 did;
137 u16 devfn[NUM_IMC];
138 u8 busidx;
139 u8 per_socket;
140 enum munittype mtype;
144 * List of PCI device ids that we need together with some device
145 * number and function numbers to tell which memory controller the
146 * device belongs to.
148 static const struct munit skx_all_munits[] = {
149 { 0x2054, { }, 1, 1, SAD_ALL },
150 { 0x2055, { }, 1, 1, UTIL_ALL },
151 { 0x2040, { PCI_DEVFN(10, 0), PCI_DEVFN(12, 0) }, 2, 2, CHAN0 },
152 { 0x2044, { PCI_DEVFN(10, 4), PCI_DEVFN(12, 4) }, 2, 2, CHAN1 },
153 { 0x2048, { PCI_DEVFN(11, 0), PCI_DEVFN(13, 0) }, 2, 2, CHAN2 },
154 { 0x208e, { }, 1, 0, SAD },
159 * We use the per-socket device 0x2016 to count how many sockets are present,
160 * and to detemine which PCI buses are associated with each socket. Allocate
161 * and build the full list of all the skx_dev structures that we need here.
163 static int get_all_bus_mappings(void)
165 struct pci_dev *pdev, *prev;
166 struct skx_dev *d;
167 u32 reg;
168 int ndev = 0;
170 prev = NULL;
171 for (;;) {
172 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x2016, prev);
173 if (!pdev)
174 break;
175 ndev++;
176 d = kzalloc(sizeof(*d), GFP_KERNEL);
177 if (!d) {
178 pci_dev_put(pdev);
179 return -ENOMEM;
181 d->seg = pci_domain_nr(pdev->bus);
182 pci_read_config_dword(pdev, 0xCC, &reg);
183 d->bus[0] = GET_BITFIELD(reg, 0, 7);
184 d->bus[1] = GET_BITFIELD(reg, 8, 15);
185 d->bus[2] = GET_BITFIELD(reg, 16, 23);
186 d->bus[3] = GET_BITFIELD(reg, 24, 31);
187 edac_dbg(2, "busses: %x, %x, %x, %x\n",
188 d->bus[0], d->bus[1], d->bus[2], d->bus[3]);
189 list_add_tail(&d->list, &skx_edac_list);
190 skx_num_sockets++;
191 prev = pdev;
194 return ndev;
197 static int get_all_munits(const struct munit *m)
199 struct pci_dev *pdev, *prev;
200 struct skx_dev *d;
201 u32 reg;
202 int i = 0, ndev = 0;
204 prev = NULL;
205 for (;;) {
206 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, m->did, prev);
207 if (!pdev)
208 break;
209 ndev++;
210 if (m->per_socket == NUM_IMC) {
211 for (i = 0; i < NUM_IMC; i++)
212 if (m->devfn[i] == pdev->devfn)
213 break;
214 if (i == NUM_IMC)
215 goto fail;
217 d = get_skx_dev(pdev->bus, m->busidx);
218 if (!d)
219 goto fail;
221 /* Be sure that the device is enabled */
222 if (unlikely(pci_enable_device(pdev) < 0)) {
223 skx_printk(KERN_ERR,
224 "Couldn't enable %04x:%04x\n", PCI_VENDOR_ID_INTEL, m->did);
225 goto fail;
228 switch (m->mtype) {
229 case CHAN0: case CHAN1: case CHAN2:
230 pci_dev_get(pdev);
231 d->imc[i].chan[m->mtype].cdev = pdev;
232 break;
233 case SAD_ALL:
234 pci_dev_get(pdev);
235 d->sad_all = pdev;
236 break;
237 case UTIL_ALL:
238 pci_dev_get(pdev);
239 d->util_all = pdev;
240 break;
241 case SAD:
243 * one of these devices per core, including cores
244 * that don't exist on this SKU. Ignore any that
245 * read a route table of zero, make sure all the
246 * non-zero values match.
248 pci_read_config_dword(pdev, 0xB4, &reg);
249 if (reg != 0) {
250 if (d->mcroute == 0)
251 d->mcroute = reg;
252 else if (d->mcroute != reg) {
253 skx_printk(KERN_ERR,
254 "mcroute mismatch\n");
255 goto fail;
258 ndev--;
259 break;
262 prev = pdev;
265 return ndev;
266 fail:
267 pci_dev_put(pdev);
268 return -ENODEV;
271 static const struct x86_cpu_id skx_cpuids[] = {
272 { X86_VENDOR_INTEL, 6, INTEL_FAM6_SKYLAKE_X, 0, 0 },
275 MODULE_DEVICE_TABLE(x86cpu, skx_cpuids);
277 static u8 get_src_id(struct skx_dev *d)
279 u32 reg;
281 pci_read_config_dword(d->util_all, 0xF0, &reg);
283 return GET_BITFIELD(reg, 12, 14);
286 static u8 skx_get_node_id(struct skx_dev *d)
288 u32 reg;
290 pci_read_config_dword(d->util_all, 0xF4, &reg);
292 return GET_BITFIELD(reg, 0, 2);
295 static int get_dimm_attr(u32 reg, int lobit, int hibit, int add, int minval,
296 int maxval, char *name)
298 u32 val = GET_BITFIELD(reg, lobit, hibit);
300 if (val < minval || val > maxval) {
301 edac_dbg(2, "bad %s = %d (raw=%x)\n", name, val, reg);
302 return -EINVAL;
304 return val + add;
307 #define IS_DIMM_PRESENT(mtr) GET_BITFIELD((mtr), 15, 15)
308 #define IS_NVDIMM_PRESENT(mcddrtcfg, i) GET_BITFIELD((mcddrtcfg), (i), (i))
310 #define numrank(reg) get_dimm_attr((reg), 12, 13, 0, 0, 2, "ranks")
311 #define numrow(reg) get_dimm_attr((reg), 2, 4, 12, 1, 6, "rows")
312 #define numcol(reg) get_dimm_attr((reg), 0, 1, 10, 0, 2, "cols")
314 static int get_width(u32 mtr)
316 switch (GET_BITFIELD(mtr, 8, 9)) {
317 case 0:
318 return DEV_X4;
319 case 1:
320 return DEV_X8;
321 case 2:
322 return DEV_X16;
324 return DEV_UNKNOWN;
327 static int skx_get_hi_lo(void)
329 struct pci_dev *pdev;
330 u32 reg;
332 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x2034, NULL);
333 if (!pdev) {
334 edac_dbg(0, "Can't get tolm/tohm\n");
335 return -ENODEV;
338 pci_read_config_dword(pdev, 0xD0, &reg);
339 skx_tolm = reg;
340 pci_read_config_dword(pdev, 0xD4, &reg);
341 skx_tohm = reg;
342 pci_read_config_dword(pdev, 0xD8, &reg);
343 skx_tohm |= (u64)reg << 32;
345 pci_dev_put(pdev);
346 edac_dbg(2, "tolm=%llx tohm=%llx\n", skx_tolm, skx_tohm);
348 return 0;
351 static int get_dimm_info(u32 mtr, u32 amap, struct dimm_info *dimm,
352 struct skx_imc *imc, int chan, int dimmno)
354 int banks = 16, ranks, rows, cols, npages;
355 u64 size;
357 ranks = numrank(mtr);
358 rows = numrow(mtr);
359 cols = numcol(mtr);
362 * Compute size in 8-byte (2^3) words, then shift to MiB (2^20)
364 size = ((1ull << (rows + cols + ranks)) * banks) >> (20 - 3);
365 npages = MiB_TO_PAGES(size);
367 edac_dbg(0, "mc#%d: channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
368 imc->mc, chan, dimmno, size, npages,
369 banks, 1 << ranks, rows, cols);
371 imc->chan[chan].dimms[dimmno].close_pg = GET_BITFIELD(mtr, 0, 0);
372 imc->chan[chan].dimms[dimmno].bank_xor_enable = GET_BITFIELD(mtr, 9, 9);
373 imc->chan[chan].dimms[dimmno].fine_grain_bank = GET_BITFIELD(amap, 0, 0);
374 imc->chan[chan].dimms[dimmno].rowbits = rows;
375 imc->chan[chan].dimms[dimmno].colbits = cols;
377 dimm->nr_pages = npages;
378 dimm->grain = 32;
379 dimm->dtype = get_width(mtr);
380 dimm->mtype = MEM_DDR4;
381 dimm->edac_mode = EDAC_SECDED; /* likely better than this */
382 snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_MC#%u_Chan#%u_DIMM#%u",
383 imc->src_id, imc->lmc, chan, dimmno);
385 return 1;
388 static int get_nvdimm_info(struct dimm_info *dimm, struct skx_imc *imc,
389 int chan, int dimmno)
391 int smbios_handle;
392 u32 dev_handle;
393 u16 flags;
394 u64 size = 0;
396 dev_handle = ACPI_NFIT_BUILD_DEVICE_HANDLE(dimmno, chan, imc->lmc,
397 imc->src_id, 0);
399 smbios_handle = nfit_get_smbios_id(dev_handle, &flags);
400 if (smbios_handle == -EOPNOTSUPP) {
401 pr_warn_once(EDAC_MOD_STR ": Can't find size of NVDIMM. Try enabling CONFIG_ACPI_NFIT\n");
402 goto unknown_size;
405 if (smbios_handle < 0) {
406 skx_printk(KERN_ERR, "Can't find handle for NVDIMM ADR=%x\n", dev_handle);
407 goto unknown_size;
410 if (flags & ACPI_NFIT_MEM_MAP_FAILED) {
411 skx_printk(KERN_ERR, "NVDIMM ADR=%x is not mapped\n", dev_handle);
412 goto unknown_size;
415 size = dmi_memdev_size(smbios_handle);
416 if (size == ~0ull)
417 skx_printk(KERN_ERR, "Can't find size for NVDIMM ADR=%x/SMBIOS=%x\n",
418 dev_handle, smbios_handle);
420 unknown_size:
421 dimm->nr_pages = size >> PAGE_SHIFT;
422 dimm->grain = 32;
423 dimm->dtype = DEV_UNKNOWN;
424 dimm->mtype = MEM_NVDIMM;
425 dimm->edac_mode = EDAC_SECDED; /* likely better than this */
427 edac_dbg(0, "mc#%d: channel %d, dimm %d, %llu Mb (%u pages)\n",
428 imc->mc, chan, dimmno, size >> 20, dimm->nr_pages);
430 snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_MC#%u_Chan#%u_DIMM#%u",
431 imc->src_id, imc->lmc, chan, dimmno);
433 return (size == 0 || size == ~0ull) ? 0 : 1;
436 #define SKX_GET_MTMTR(dev, reg) \
437 pci_read_config_dword((dev), 0x87c, &reg)
439 static bool skx_check_ecc(struct pci_dev *pdev)
441 u32 mtmtr;
443 SKX_GET_MTMTR(pdev, mtmtr);
445 return !!GET_BITFIELD(mtmtr, 2, 2);
448 static int skx_get_dimm_config(struct mem_ctl_info *mci)
450 struct skx_pvt *pvt = mci->pvt_info;
451 struct skx_imc *imc = pvt->imc;
452 u32 mtr, amap, mcddrtcfg;
453 struct dimm_info *dimm;
454 int i, j;
455 int ndimms;
457 for (i = 0; i < NUM_CHANNELS; i++) {
458 ndimms = 0;
459 pci_read_config_dword(imc->chan[i].cdev, 0x8C, &amap);
460 pci_read_config_dword(imc->chan[i].cdev, 0x400, &mcddrtcfg);
461 for (j = 0; j < NUM_DIMMS; j++) {
462 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms,
463 mci->n_layers, i, j, 0);
464 pci_read_config_dword(imc->chan[i].cdev,
465 0x80 + 4*j, &mtr);
466 if (IS_DIMM_PRESENT(mtr))
467 ndimms += get_dimm_info(mtr, amap, dimm, imc, i, j);
468 else if (IS_NVDIMM_PRESENT(mcddrtcfg, j))
469 ndimms += get_nvdimm_info(dimm, imc, i, j);
471 if (ndimms && !skx_check_ecc(imc->chan[0].cdev)) {
472 skx_printk(KERN_ERR, "ECC is disabled on imc %d\n", imc->mc);
473 return -ENODEV;
477 return 0;
480 static void skx_unregister_mci(struct skx_imc *imc)
482 struct mem_ctl_info *mci = imc->mci;
484 if (!mci)
485 return;
487 edac_dbg(0, "MC%d: mci = %p\n", imc->mc, mci);
489 /* Remove MC sysfs nodes */
490 edac_mc_del_mc(mci->pdev);
492 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
493 kfree(mci->ctl_name);
494 edac_mc_free(mci);
497 static int skx_register_mci(struct skx_imc *imc)
499 struct mem_ctl_info *mci;
500 struct edac_mc_layer layers[2];
501 struct pci_dev *pdev = imc->chan[0].cdev;
502 struct skx_pvt *pvt;
503 int rc;
505 /* allocate a new MC control structure */
506 layers[0].type = EDAC_MC_LAYER_CHANNEL;
507 layers[0].size = NUM_CHANNELS;
508 layers[0].is_virt_csrow = false;
509 layers[1].type = EDAC_MC_LAYER_SLOT;
510 layers[1].size = NUM_DIMMS;
511 layers[1].is_virt_csrow = true;
512 mci = edac_mc_alloc(imc->mc, ARRAY_SIZE(layers), layers,
513 sizeof(struct skx_pvt));
515 if (unlikely(!mci))
516 return -ENOMEM;
518 edac_dbg(0, "MC#%d: mci = %p\n", imc->mc, mci);
520 /* Associate skx_dev and mci for future usage */
521 imc->mci = mci;
522 pvt = mci->pvt_info;
523 pvt->imc = imc;
525 mci->ctl_name = kasprintf(GFP_KERNEL, "Skylake Socket#%d IMC#%d",
526 imc->node_id, imc->lmc);
527 if (!mci->ctl_name) {
528 rc = -ENOMEM;
529 goto fail0;
532 mci->mtype_cap = MEM_FLAG_DDR4 | MEM_FLAG_NVDIMM;
533 mci->edac_ctl_cap = EDAC_FLAG_NONE;
534 mci->edac_cap = EDAC_FLAG_NONE;
535 mci->mod_name = EDAC_MOD_STR;
536 mci->dev_name = pci_name(imc->chan[0].cdev);
537 mci->ctl_page_to_phys = NULL;
539 rc = skx_get_dimm_config(mci);
540 if (rc < 0)
541 goto fail;
543 /* record ptr to the generic device */
544 mci->pdev = &pdev->dev;
546 /* add this new MC control structure to EDAC's list of MCs */
547 if (unlikely(edac_mc_add_mc(mci))) {
548 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
549 rc = -EINVAL;
550 goto fail;
553 return 0;
555 fail:
556 kfree(mci->ctl_name);
557 fail0:
558 edac_mc_free(mci);
559 imc->mci = NULL;
560 return rc;
563 #define SKX_MAX_SAD 24
565 #define SKX_GET_SAD(d, i, reg) \
566 pci_read_config_dword((d)->sad_all, 0x60 + 8 * (i), &reg)
567 #define SKX_GET_ILV(d, i, reg) \
568 pci_read_config_dword((d)->sad_all, 0x64 + 8 * (i), &reg)
570 #define SKX_SAD_MOD3MODE(sad) GET_BITFIELD((sad), 30, 31)
571 #define SKX_SAD_MOD3(sad) GET_BITFIELD((sad), 27, 27)
572 #define SKX_SAD_LIMIT(sad) (((u64)GET_BITFIELD((sad), 7, 26) << 26) | MASK26)
573 #define SKX_SAD_MOD3ASMOD2(sad) GET_BITFIELD((sad), 5, 6)
574 #define SKX_SAD_ATTR(sad) GET_BITFIELD((sad), 3, 4)
575 #define SKX_SAD_INTERLEAVE(sad) GET_BITFIELD((sad), 1, 2)
576 #define SKX_SAD_ENABLE(sad) GET_BITFIELD((sad), 0, 0)
578 #define SKX_ILV_REMOTE(tgt) (((tgt) & 8) == 0)
579 #define SKX_ILV_TARGET(tgt) ((tgt) & 7)
581 static bool skx_sad_decode(struct decoded_addr *res)
583 struct skx_dev *d = list_first_entry(&skx_edac_list, typeof(*d), list);
584 u64 addr = res->addr;
585 int i, idx, tgt, lchan, shift;
586 u32 sad, ilv;
587 u64 limit, prev_limit;
588 int remote = 0;
590 /* Simple sanity check for I/O space or out of range */
591 if (addr >= skx_tohm || (addr >= skx_tolm && addr < BIT_ULL(32))) {
592 edac_dbg(0, "Address %llx out of range\n", addr);
593 return false;
596 restart:
597 prev_limit = 0;
598 for (i = 0; i < SKX_MAX_SAD; i++) {
599 SKX_GET_SAD(d, i, sad);
600 limit = SKX_SAD_LIMIT(sad);
601 if (SKX_SAD_ENABLE(sad)) {
602 if (addr >= prev_limit && addr <= limit)
603 goto sad_found;
605 prev_limit = limit + 1;
607 edac_dbg(0, "No SAD entry for %llx\n", addr);
608 return false;
610 sad_found:
611 SKX_GET_ILV(d, i, ilv);
613 switch (SKX_SAD_INTERLEAVE(sad)) {
614 case 0:
615 idx = GET_BITFIELD(addr, 6, 8);
616 break;
617 case 1:
618 idx = GET_BITFIELD(addr, 8, 10);
619 break;
620 case 2:
621 idx = GET_BITFIELD(addr, 12, 14);
622 break;
623 case 3:
624 idx = GET_BITFIELD(addr, 30, 32);
625 break;
628 tgt = GET_BITFIELD(ilv, 4 * idx, 4 * idx + 3);
630 /* If point to another node, find it and start over */
631 if (SKX_ILV_REMOTE(tgt)) {
632 if (remote) {
633 edac_dbg(0, "Double remote!\n");
634 return false;
636 remote = 1;
637 list_for_each_entry(d, &skx_edac_list, list) {
638 if (d->imc[0].src_id == SKX_ILV_TARGET(tgt))
639 goto restart;
641 edac_dbg(0, "Can't find node %d\n", SKX_ILV_TARGET(tgt));
642 return false;
645 if (SKX_SAD_MOD3(sad) == 0)
646 lchan = SKX_ILV_TARGET(tgt);
647 else {
648 switch (SKX_SAD_MOD3MODE(sad)) {
649 case 0:
650 shift = 6;
651 break;
652 case 1:
653 shift = 8;
654 break;
655 case 2:
656 shift = 12;
657 break;
658 default:
659 edac_dbg(0, "illegal mod3mode\n");
660 return false;
662 switch (SKX_SAD_MOD3ASMOD2(sad)) {
663 case 0:
664 lchan = (addr >> shift) % 3;
665 break;
666 case 1:
667 lchan = (addr >> shift) % 2;
668 break;
669 case 2:
670 lchan = (addr >> shift) % 2;
671 lchan = (lchan << 1) | ~lchan;
672 break;
673 case 3:
674 lchan = ((addr >> shift) % 2) << 1;
675 break;
677 lchan = (lchan << 1) | (SKX_ILV_TARGET(tgt) & 1);
680 res->dev = d;
681 res->socket = d->imc[0].src_id;
682 res->imc = GET_BITFIELD(d->mcroute, lchan * 3, lchan * 3 + 2);
683 res->channel = GET_BITFIELD(d->mcroute, lchan * 2 + 18, lchan * 2 + 19);
685 edac_dbg(2, "%llx: socket=%d imc=%d channel=%d\n",
686 res->addr, res->socket, res->imc, res->channel);
687 return true;
690 #define SKX_MAX_TAD 8
692 #define SKX_GET_TADBASE(d, mc, i, reg) \
693 pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x850 + 4 * (i), &reg)
694 #define SKX_GET_TADWAYNESS(d, mc, i, reg) \
695 pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x880 + 4 * (i), &reg)
696 #define SKX_GET_TADCHNILVOFFSET(d, mc, ch, i, reg) \
697 pci_read_config_dword((d)->imc[mc].chan[ch].cdev, 0x90 + 4 * (i), &reg)
699 #define SKX_TAD_BASE(b) ((u64)GET_BITFIELD((b), 12, 31) << 26)
700 #define SKX_TAD_SKT_GRAN(b) GET_BITFIELD((b), 4, 5)
701 #define SKX_TAD_CHN_GRAN(b) GET_BITFIELD((b), 6, 7)
702 #define SKX_TAD_LIMIT(b) (((u64)GET_BITFIELD((b), 12, 31) << 26) | MASK26)
703 #define SKX_TAD_OFFSET(b) ((u64)GET_BITFIELD((b), 4, 23) << 26)
704 #define SKX_TAD_SKTWAYS(b) (1 << GET_BITFIELD((b), 10, 11))
705 #define SKX_TAD_CHNWAYS(b) (GET_BITFIELD((b), 8, 9) + 1)
707 /* which bit used for both socket and channel interleave */
708 static int skx_granularity[] = { 6, 8, 12, 30 };
710 static u64 skx_do_interleave(u64 addr, int shift, int ways, u64 lowbits)
712 addr >>= shift;
713 addr /= ways;
714 addr <<= shift;
716 return addr | (lowbits & ((1ull << shift) - 1));
719 static bool skx_tad_decode(struct decoded_addr *res)
721 int i;
722 u32 base, wayness, chnilvoffset;
723 int skt_interleave_bit, chn_interleave_bit;
724 u64 channel_addr;
726 for (i = 0; i < SKX_MAX_TAD; i++) {
727 SKX_GET_TADBASE(res->dev, res->imc, i, base);
728 SKX_GET_TADWAYNESS(res->dev, res->imc, i, wayness);
729 if (SKX_TAD_BASE(base) <= res->addr && res->addr <= SKX_TAD_LIMIT(wayness))
730 goto tad_found;
732 edac_dbg(0, "No TAD entry for %llx\n", res->addr);
733 return false;
735 tad_found:
736 res->sktways = SKX_TAD_SKTWAYS(wayness);
737 res->chanways = SKX_TAD_CHNWAYS(wayness);
738 skt_interleave_bit = skx_granularity[SKX_TAD_SKT_GRAN(base)];
739 chn_interleave_bit = skx_granularity[SKX_TAD_CHN_GRAN(base)];
741 SKX_GET_TADCHNILVOFFSET(res->dev, res->imc, res->channel, i, chnilvoffset);
742 channel_addr = res->addr - SKX_TAD_OFFSET(chnilvoffset);
744 if (res->chanways == 3 && skt_interleave_bit > chn_interleave_bit) {
745 /* Must handle channel first, then socket */
746 channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
747 res->chanways, channel_addr);
748 channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
749 res->sktways, channel_addr);
750 } else {
751 /* Handle socket then channel. Preserve low bits from original address */
752 channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
753 res->sktways, res->addr);
754 channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
755 res->chanways, res->addr);
758 res->chan_addr = channel_addr;
760 edac_dbg(2, "%llx: chan_addr=%llx sktways=%d chanways=%d\n",
761 res->addr, res->chan_addr, res->sktways, res->chanways);
762 return true;
765 #define SKX_MAX_RIR 4
767 #define SKX_GET_RIRWAYNESS(d, mc, ch, i, reg) \
768 pci_read_config_dword((d)->imc[mc].chan[ch].cdev, \
769 0x108 + 4 * (i), &reg)
770 #define SKX_GET_RIRILV(d, mc, ch, idx, i, reg) \
771 pci_read_config_dword((d)->imc[mc].chan[ch].cdev, \
772 0x120 + 16 * idx + 4 * (i), &reg)
774 #define SKX_RIR_VALID(b) GET_BITFIELD((b), 31, 31)
775 #define SKX_RIR_LIMIT(b) (((u64)GET_BITFIELD((b), 1, 11) << 29) | MASK29)
776 #define SKX_RIR_WAYS(b) (1 << GET_BITFIELD((b), 28, 29))
777 #define SKX_RIR_CHAN_RANK(b) GET_BITFIELD((b), 16, 19)
778 #define SKX_RIR_OFFSET(b) ((u64)(GET_BITFIELD((b), 2, 15) << 26))
780 static bool skx_rir_decode(struct decoded_addr *res)
782 int i, idx, chan_rank;
783 int shift;
784 u32 rirway, rirlv;
785 u64 rank_addr, prev_limit = 0, limit;
787 if (res->dev->imc[res->imc].chan[res->channel].dimms[0].close_pg)
788 shift = 6;
789 else
790 shift = 13;
792 for (i = 0; i < SKX_MAX_RIR; i++) {
793 SKX_GET_RIRWAYNESS(res->dev, res->imc, res->channel, i, rirway);
794 limit = SKX_RIR_LIMIT(rirway);
795 if (SKX_RIR_VALID(rirway)) {
796 if (prev_limit <= res->chan_addr &&
797 res->chan_addr <= limit)
798 goto rir_found;
800 prev_limit = limit;
802 edac_dbg(0, "No RIR entry for %llx\n", res->addr);
803 return false;
805 rir_found:
806 rank_addr = res->chan_addr >> shift;
807 rank_addr /= SKX_RIR_WAYS(rirway);
808 rank_addr <<= shift;
809 rank_addr |= res->chan_addr & GENMASK_ULL(shift - 1, 0);
811 res->rank_address = rank_addr;
812 idx = (res->chan_addr >> shift) % SKX_RIR_WAYS(rirway);
814 SKX_GET_RIRILV(res->dev, res->imc, res->channel, idx, i, rirlv);
815 res->rank_address = rank_addr - SKX_RIR_OFFSET(rirlv);
816 chan_rank = SKX_RIR_CHAN_RANK(rirlv);
817 res->channel_rank = chan_rank;
818 res->dimm = chan_rank / 4;
819 res->rank = chan_rank % 4;
821 edac_dbg(2, "%llx: dimm=%d rank=%d chan_rank=%d rank_addr=%llx\n",
822 res->addr, res->dimm, res->rank,
823 res->channel_rank, res->rank_address);
824 return true;
827 static u8 skx_close_row[] = {
828 15, 16, 17, 18, 20, 21, 22, 28, 10, 11, 12, 13, 29, 30, 31, 32, 33
830 static u8 skx_close_column[] = {
831 3, 4, 5, 14, 19, 23, 24, 25, 26, 27
833 static u8 skx_open_row[] = {
834 14, 15, 16, 20, 28, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33
836 static u8 skx_open_column[] = {
837 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
839 static u8 skx_open_fine_column[] = {
840 3, 4, 5, 7, 8, 9, 10, 11, 12, 13
843 static int skx_bits(u64 addr, int nbits, u8 *bits)
845 int i, res = 0;
847 for (i = 0; i < nbits; i++)
848 res |= ((addr >> bits[i]) & 1) << i;
849 return res;
852 static int skx_bank_bits(u64 addr, int b0, int b1, int do_xor, int x0, int x1)
854 int ret = GET_BITFIELD(addr, b0, b0) | (GET_BITFIELD(addr, b1, b1) << 1);
856 if (do_xor)
857 ret ^= GET_BITFIELD(addr, x0, x0) | (GET_BITFIELD(addr, x1, x1) << 1);
859 return ret;
862 static bool skx_mad_decode(struct decoded_addr *r)
864 struct skx_dimm *dimm = &r->dev->imc[r->imc].chan[r->channel].dimms[r->dimm];
865 int bg0 = dimm->fine_grain_bank ? 6 : 13;
867 if (dimm->close_pg) {
868 r->row = skx_bits(r->rank_address, dimm->rowbits, skx_close_row);
869 r->column = skx_bits(r->rank_address, dimm->colbits, skx_close_column);
870 r->column |= 0x400; /* C10 is autoprecharge, always set */
871 r->bank_address = skx_bank_bits(r->rank_address, 8, 9, dimm->bank_xor_enable, 22, 28);
872 r->bank_group = skx_bank_bits(r->rank_address, 6, 7, dimm->bank_xor_enable, 20, 21);
873 } else {
874 r->row = skx_bits(r->rank_address, dimm->rowbits, skx_open_row);
875 if (dimm->fine_grain_bank)
876 r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_fine_column);
877 else
878 r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_column);
879 r->bank_address = skx_bank_bits(r->rank_address, 18, 19, dimm->bank_xor_enable, 22, 23);
880 r->bank_group = skx_bank_bits(r->rank_address, bg0, 17, dimm->bank_xor_enable, 20, 21);
882 r->row &= (1u << dimm->rowbits) - 1;
884 edac_dbg(2, "%llx: row=%x col=%x bank_addr=%d bank_group=%d\n",
885 r->addr, r->row, r->column, r->bank_address,
886 r->bank_group);
887 return true;
890 static bool skx_decode(struct decoded_addr *res)
893 return skx_sad_decode(res) && skx_tad_decode(res) &&
894 skx_rir_decode(res) && skx_mad_decode(res);
897 #ifdef CONFIG_EDAC_DEBUG
899 * Debug feature. Make /sys/kernel/debug/skx_edac_test/addr.
900 * Write an address to this file to exercise the address decode
901 * logic in this driver.
903 static struct dentry *skx_test;
904 static u64 skx_fake_addr;
906 static int debugfs_u64_set(void *data, u64 val)
908 struct decoded_addr res;
910 res.addr = val;
911 skx_decode(&res);
913 return 0;
916 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
918 static struct dentry *mydebugfs_create(const char *name, umode_t mode,
919 struct dentry *parent, u64 *value)
921 return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
924 static void setup_skx_debug(void)
926 skx_test = debugfs_create_dir("skx_edac_test", NULL);
927 mydebugfs_create("addr", S_IWUSR, skx_test, &skx_fake_addr);
930 static void teardown_skx_debug(void)
932 debugfs_remove_recursive(skx_test);
934 #else
935 static void setup_skx_debug(void)
939 static void teardown_skx_debug(void)
942 #endif /*CONFIG_EDAC_DEBUG*/
944 static void skx_mce_output_error(struct mem_ctl_info *mci,
945 const struct mce *m,
946 struct decoded_addr *res)
948 enum hw_event_mc_err_type tp_event;
949 char *type, *optype, msg[256];
950 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
951 bool overflow = GET_BITFIELD(m->status, 62, 62);
952 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
953 bool recoverable;
954 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
955 u32 mscod = GET_BITFIELD(m->status, 16, 31);
956 u32 errcode = GET_BITFIELD(m->status, 0, 15);
957 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
959 recoverable = GET_BITFIELD(m->status, 56, 56);
961 if (uncorrected_error) {
962 if (ripv) {
963 type = "FATAL";
964 tp_event = HW_EVENT_ERR_FATAL;
965 } else {
966 type = "NON_FATAL";
967 tp_event = HW_EVENT_ERR_UNCORRECTED;
969 } else {
970 type = "CORRECTED";
971 tp_event = HW_EVENT_ERR_CORRECTED;
975 * According with Table 15-9 of the Intel Architecture spec vol 3A,
976 * memory errors should fit in this mask:
977 * 000f 0000 1mmm cccc (binary)
978 * where:
979 * f = Correction Report Filtering Bit. If 1, subsequent errors
980 * won't be shown
981 * mmm = error type
982 * cccc = channel
983 * If the mask doesn't match, report an error to the parsing logic
985 if (!((errcode & 0xef80) == 0x80)) {
986 optype = "Can't parse: it is not a mem";
987 } else {
988 switch (optypenum) {
989 case 0:
990 optype = "generic undef request error";
991 break;
992 case 1:
993 optype = "memory read error";
994 break;
995 case 2:
996 optype = "memory write error";
997 break;
998 case 3:
999 optype = "addr/cmd error";
1000 break;
1001 case 4:
1002 optype = "memory scrubbing error";
1003 break;
1004 default:
1005 optype = "reserved";
1006 break;
1010 snprintf(msg, sizeof(msg),
1011 "%s%s err_code:%04x:%04x socket:%d imc:%d rank:%d bg:%d ba:%d row:%x col:%x",
1012 overflow ? " OVERFLOW" : "",
1013 (uncorrected_error && recoverable) ? " recoverable" : "",
1014 mscod, errcode,
1015 res->socket, res->imc, res->rank,
1016 res->bank_group, res->bank_address, res->row, res->column);
1018 edac_dbg(0, "%s\n", msg);
1020 /* Call the helper to output message */
1021 edac_mc_handle_error(tp_event, mci, core_err_cnt,
1022 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
1023 res->channel, res->dimm, -1,
1024 optype, msg);
1027 static int skx_mce_check_error(struct notifier_block *nb, unsigned long val,
1028 void *data)
1030 struct mce *mce = (struct mce *)data;
1031 struct decoded_addr res;
1032 struct mem_ctl_info *mci;
1033 char *type;
1035 if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
1036 return NOTIFY_DONE;
1038 /* ignore unless this is memory related with an address */
1039 if ((mce->status & 0xefff) >> 7 != 1 || !(mce->status & MCI_STATUS_ADDRV))
1040 return NOTIFY_DONE;
1042 res.addr = mce->addr;
1043 if (!skx_decode(&res))
1044 return NOTIFY_DONE;
1045 mci = res.dev->imc[res.imc].mci;
1047 if (mce->mcgstatus & MCG_STATUS_MCIP)
1048 type = "Exception";
1049 else
1050 type = "Event";
1052 skx_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
1054 skx_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
1055 "Bank %d: %016Lx\n", mce->extcpu, type,
1056 mce->mcgstatus, mce->bank, mce->status);
1057 skx_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
1058 skx_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
1059 skx_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
1061 skx_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
1062 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
1063 mce->time, mce->socketid, mce->apicid);
1065 skx_mce_output_error(mci, mce, &res);
1067 return NOTIFY_DONE;
1070 static struct notifier_block skx_mce_dec = {
1071 .notifier_call = skx_mce_check_error,
1072 .priority = MCE_PRIO_EDAC,
1075 static void skx_remove(void)
1077 int i, j;
1078 struct skx_dev *d, *tmp;
1080 edac_dbg(0, "\n");
1082 list_for_each_entry_safe(d, tmp, &skx_edac_list, list) {
1083 list_del(&d->list);
1084 for (i = 0; i < NUM_IMC; i++) {
1085 skx_unregister_mci(&d->imc[i]);
1086 for (j = 0; j < NUM_CHANNELS; j++)
1087 pci_dev_put(d->imc[i].chan[j].cdev);
1089 pci_dev_put(d->util_all);
1090 pci_dev_put(d->sad_all);
1092 kfree(d);
1097 * skx_init:
1098 * make sure we are running on the correct cpu model
1099 * search for all the devices we need
1100 * check which DIMMs are present.
1102 static int __init skx_init(void)
1104 const struct x86_cpu_id *id;
1105 const struct munit *m;
1106 const char *owner;
1107 int rc = 0, i;
1108 u8 mc = 0, src_id, node_id;
1109 struct skx_dev *d;
1111 edac_dbg(2, "\n");
1113 owner = edac_get_owner();
1114 if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
1115 return -EBUSY;
1117 id = x86_match_cpu(skx_cpuids);
1118 if (!id)
1119 return -ENODEV;
1121 rc = skx_get_hi_lo();
1122 if (rc)
1123 return rc;
1125 rc = get_all_bus_mappings();
1126 if (rc < 0)
1127 goto fail;
1128 if (rc == 0) {
1129 edac_dbg(2, "No memory controllers found\n");
1130 return -ENODEV;
1133 for (m = skx_all_munits; m->did; m++) {
1134 rc = get_all_munits(m);
1135 if (rc < 0)
1136 goto fail;
1137 if (rc != m->per_socket * skx_num_sockets) {
1138 edac_dbg(2, "Expected %d, got %d of %x\n",
1139 m->per_socket * skx_num_sockets, rc, m->did);
1140 rc = -ENODEV;
1141 goto fail;
1145 list_for_each_entry(d, &skx_edac_list, list) {
1146 src_id = get_src_id(d);
1147 node_id = skx_get_node_id(d);
1148 edac_dbg(2, "src_id=%d node_id=%d\n", src_id, node_id);
1149 for (i = 0; i < NUM_IMC; i++) {
1150 d->imc[i].mc = mc++;
1151 d->imc[i].lmc = i;
1152 d->imc[i].src_id = src_id;
1153 d->imc[i].node_id = node_id;
1154 rc = skx_register_mci(&d->imc[i]);
1155 if (rc < 0)
1156 goto fail;
1160 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
1161 opstate_init();
1163 setup_skx_debug();
1165 mce_register_decode_chain(&skx_mce_dec);
1167 return 0;
1168 fail:
1169 skx_remove();
1170 return rc;
1173 static void __exit skx_exit(void)
1175 edac_dbg(2, "\n");
1176 mce_unregister_decode_chain(&skx_mce_dec);
1177 skx_remove();
1178 teardown_skx_debug();
1181 module_init(skx_init);
1182 module_exit(skx_exit);
1184 module_param(edac_op_state, int, 0444);
1185 MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
1187 MODULE_LICENSE("GPL v2");
1188 MODULE_AUTHOR("Tony Luck");
1189 MODULE_DESCRIPTION("MC Driver for Intel Skylake server processors");