skx_edac.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. * EDAC driver for Intel(R) Xeon(R) Skylake processors
  3. * Copyright (c) 2016, Intel Corporation.
  4. *
  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.
  8. *
  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.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/pci.h>
  17. #include <linux/pci_ids.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/edac.h>
  21. #include <linux/mmzone.h>
  22. #include <linux/smp.h>
  23. #include <linux/bitmap.h>
  24. #include <linux/math64.h>
  25. #include <linux/mod_devicetable.h>
  26. #include <asm/cpu_device_id.h>
  27. #include <asm/processor.h>
  28. #include <asm/mce.h>
  29. #include "edac_core.h"
  30. #define SKX_REVISION " Ver: 1.0 "
  31. /*
  32. * Debug macros
  33. */
  34. #define skx_printk(level, fmt, arg...) \
  35. edac_printk(level, "skx", fmt, ##arg)
  36. #define skx_mc_printk(mci, level, fmt, arg...) \
  37. edac_mc_chipset_printk(mci, level, "skx", fmt, ##arg)
  38. /*
  39. * Get a bit field at register value <v>, from bit <lo> to bit <hi>
  40. */
  41. #define GET_BITFIELD(v, lo, hi) \
  42. (((v) & GENMASK_ULL((hi), (lo))) >> (lo))
  43. static LIST_HEAD(skx_edac_list);
  44. static u64 skx_tolm, skx_tohm;
  45. #define NUM_IMC 2 /* memory controllers per socket */
  46. #define NUM_CHANNELS 3 /* channels per memory controller */
  47. #define NUM_DIMMS 2 /* Max DIMMS per channel */
  48. #define MASK26 0x3FFFFFF /* Mask for 2^26 */
  49. #define MASK29 0x1FFFFFFF /* Mask for 2^29 */
  50. /*
  51. * Each cpu socket contains some pci devices that provide global
  52. * information, and also some that are local to each of the two
  53. * memory controllers on the die.
  54. */
  55. struct skx_dev {
  56. struct list_head list;
  57. u8 bus[4];
  58. struct pci_dev *sad_all;
  59. struct pci_dev *util_all;
  60. u32 mcroute;
  61. struct skx_imc {
  62. struct mem_ctl_info *mci;
  63. u8 mc; /* system wide mc# */
  64. u8 lmc; /* socket relative mc# */
  65. u8 src_id, node_id;
  66. struct skx_channel {
  67. struct pci_dev *cdev;
  68. struct skx_dimm {
  69. u8 close_pg;
  70. u8 bank_xor_enable;
  71. u8 fine_grain_bank;
  72. u8 rowbits;
  73. u8 colbits;
  74. } dimms[NUM_DIMMS];
  75. } chan[NUM_CHANNELS];
  76. } imc[NUM_IMC];
  77. };
  78. static int skx_num_sockets;
  79. struct skx_pvt {
  80. struct skx_imc *imc;
  81. };
  82. struct decoded_addr {
  83. struct skx_dev *dev;
  84. u64 addr;
  85. int socket;
  86. int imc;
  87. int channel;
  88. u64 chan_addr;
  89. int sktways;
  90. int chanways;
  91. int dimm;
  92. int rank;
  93. int channel_rank;
  94. u64 rank_address;
  95. int row;
  96. int column;
  97. int bank_address;
  98. int bank_group;
  99. };
  100. static struct skx_dev *get_skx_dev(u8 bus, u8 idx)
  101. {
  102. struct skx_dev *d;
  103. list_for_each_entry(d, &skx_edac_list, list) {
  104. if (d->bus[idx] == bus)
  105. return d;
  106. }
  107. return NULL;
  108. }
  109. enum munittype {
  110. CHAN0, CHAN1, CHAN2, SAD_ALL, UTIL_ALL, SAD
  111. };
  112. struct munit {
  113. u16 did;
  114. u16 devfn[NUM_IMC];
  115. u8 busidx;
  116. u8 per_socket;
  117. enum munittype mtype;
  118. };
  119. /*
  120. * List of PCI device ids that we need together with some device
  121. * number and function numbers to tell which memory controller the
  122. * device belongs to.
  123. */
  124. static const struct munit skx_all_munits[] = {
  125. { 0x2054, { }, 1, 1, SAD_ALL },
  126. { 0x2055, { }, 1, 1, UTIL_ALL },
  127. { 0x2040, { PCI_DEVFN(10, 0), PCI_DEVFN(12, 0) }, 2, 2, CHAN0 },
  128. { 0x2044, { PCI_DEVFN(10, 4), PCI_DEVFN(12, 4) }, 2, 2, CHAN1 },
  129. { 0x2048, { PCI_DEVFN(11, 0), PCI_DEVFN(13, 0) }, 2, 2, CHAN2 },
  130. { 0x208e, { }, 1, 0, SAD },
  131. { }
  132. };
  133. /*
  134. * We use the per-socket device 0x2016 to count how many sockets are present,
  135. * and to detemine which PCI buses are associated with each socket. Allocate
  136. * and build the full list of all the skx_dev structures that we need here.
  137. */
  138. static int get_all_bus_mappings(void)
  139. {
  140. struct pci_dev *pdev, *prev;
  141. struct skx_dev *d;
  142. u32 reg;
  143. int ndev = 0;
  144. prev = NULL;
  145. for (;;) {
  146. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x2016, prev);
  147. if (!pdev)
  148. break;
  149. ndev++;
  150. d = kzalloc(sizeof(*d), GFP_KERNEL);
  151. if (!d) {
  152. pci_dev_put(pdev);
  153. return -ENOMEM;
  154. }
  155. pci_read_config_dword(pdev, 0xCC, &reg);
  156. d->bus[0] = GET_BITFIELD(reg, 0, 7);
  157. d->bus[1] = GET_BITFIELD(reg, 8, 15);
  158. d->bus[2] = GET_BITFIELD(reg, 16, 23);
  159. d->bus[3] = GET_BITFIELD(reg, 24, 31);
  160. edac_dbg(2, "busses: %x, %x, %x, %x\n",
  161. d->bus[0], d->bus[1], d->bus[2], d->bus[3]);
  162. list_add_tail(&d->list, &skx_edac_list);
  163. skx_num_sockets++;
  164. prev = pdev;
  165. }
  166. return ndev;
  167. }
  168. static int get_all_munits(const struct munit *m)
  169. {
  170. struct pci_dev *pdev, *prev;
  171. struct skx_dev *d;
  172. u32 reg;
  173. int i = 0, ndev = 0;
  174. prev = NULL;
  175. for (;;) {
  176. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, m->did, prev);
  177. if (!pdev)
  178. break;
  179. ndev++;
  180. if (m->per_socket == NUM_IMC) {
  181. for (i = 0; i < NUM_IMC; i++)
  182. if (m->devfn[i] == pdev->devfn)
  183. break;
  184. if (i == NUM_IMC)
  185. goto fail;
  186. }
  187. d = get_skx_dev(pdev->bus->number, m->busidx);
  188. if (!d)
  189. goto fail;
  190. /* Be sure that the device is enabled */
  191. if (unlikely(pci_enable_device(pdev) < 0)) {
  192. skx_printk(KERN_ERR,
  193. "Couldn't enable %04x:%04x\n", PCI_VENDOR_ID_INTEL, m->did);
  194. goto fail;
  195. }
  196. switch (m->mtype) {
  197. case CHAN0: case CHAN1: case CHAN2:
  198. pci_dev_get(pdev);
  199. d->imc[i].chan[m->mtype].cdev = pdev;
  200. break;
  201. case SAD_ALL:
  202. pci_dev_get(pdev);
  203. d->sad_all = pdev;
  204. break;
  205. case UTIL_ALL:
  206. pci_dev_get(pdev);
  207. d->util_all = pdev;
  208. break;
  209. case SAD:
  210. /*
  211. * one of these devices per core, including cores
  212. * that don't exist on this SKU. Ignore any that
  213. * read a route table of zero, make sure all the
  214. * non-zero values match.
  215. */
  216. pci_read_config_dword(pdev, 0xB4, &reg);
  217. if (reg != 0) {
  218. if (d->mcroute == 0)
  219. d->mcroute = reg;
  220. else if (d->mcroute != reg) {
  221. skx_printk(KERN_ERR,
  222. "mcroute mismatch\n");
  223. goto fail;
  224. }
  225. }
  226. ndev--;
  227. break;
  228. }
  229. prev = pdev;
  230. }
  231. return ndev;
  232. fail:
  233. pci_dev_put(pdev);
  234. return -ENODEV;
  235. }
  236. const struct x86_cpu_id skx_cpuids[] = {
  237. { X86_VENDOR_INTEL, 6, 0x55, 0, 0 }, /* Skylake */
  238. { }
  239. };
  240. MODULE_DEVICE_TABLE(x86cpu, skx_cpuids);
  241. static u8 get_src_id(struct skx_dev *d)
  242. {
  243. u32 reg;
  244. pci_read_config_dword(d->util_all, 0xF0, &reg);
  245. return GET_BITFIELD(reg, 12, 14);
  246. }
  247. static u8 skx_get_node_id(struct skx_dev *d)
  248. {
  249. u32 reg;
  250. pci_read_config_dword(d->util_all, 0xF4, &reg);
  251. return GET_BITFIELD(reg, 0, 2);
  252. }
  253. static int get_dimm_attr(u32 reg, int lobit, int hibit, int add, int minval,
  254. int maxval, char *name)
  255. {
  256. u32 val = GET_BITFIELD(reg, lobit, hibit);
  257. if (val < minval || val > maxval) {
  258. edac_dbg(2, "bad %s = %d (raw=%x)\n", name, val, reg);
  259. return -EINVAL;
  260. }
  261. return val + add;
  262. }
  263. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD((mtr), 15, 15)
  264. #define numrank(reg) get_dimm_attr((reg), 12, 13, 0, 1, 2, "ranks")
  265. #define numrow(reg) get_dimm_attr((reg), 2, 4, 12, 1, 6, "rows")
  266. #define numcol(reg) get_dimm_attr((reg), 0, 1, 10, 0, 2, "cols")
  267. static int get_width(u32 mtr)
  268. {
  269. switch (GET_BITFIELD(mtr, 8, 9)) {
  270. case 0:
  271. return DEV_X4;
  272. case 1:
  273. return DEV_X8;
  274. case 2:
  275. return DEV_X16;
  276. }
  277. return DEV_UNKNOWN;
  278. }
  279. static int skx_get_hi_lo(void)
  280. {
  281. struct pci_dev *pdev;
  282. u32 reg;
  283. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x2034, NULL);
  284. if (!pdev) {
  285. edac_dbg(0, "Can't get tolm/tohm\n");
  286. return -ENODEV;
  287. }
  288. pci_read_config_dword(pdev, 0xD0, &reg);
  289. skx_tolm = reg;
  290. pci_read_config_dword(pdev, 0xD4, &reg);
  291. skx_tohm = reg;
  292. pci_read_config_dword(pdev, 0xD8, &reg);
  293. skx_tohm |= (u64)reg << 32;
  294. pci_dev_put(pdev);
  295. edac_dbg(2, "tolm=%llx tohm=%llx\n", skx_tolm, skx_tohm);
  296. return 0;
  297. }
  298. static int get_dimm_info(u32 mtr, u32 amap, struct dimm_info *dimm,
  299. struct skx_imc *imc, int chan, int dimmno)
  300. {
  301. int banks = 16, ranks, rows, cols, npages;
  302. u64 size;
  303. if (!IS_DIMM_PRESENT(mtr))
  304. return 0;
  305. ranks = numrank(mtr);
  306. rows = numrow(mtr);
  307. cols = numcol(mtr);
  308. /*
  309. * Compute size in 8-byte (2^3) words, then shift to MiB (2^20)
  310. */
  311. size = ((1ull << (rows + cols + ranks)) * banks) >> (20 - 3);
  312. npages = MiB_TO_PAGES(size);
  313. edac_dbg(0, "mc#%d: channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  314. imc->mc, chan, dimmno, size, npages,
  315. banks, ranks, rows, cols);
  316. imc->chan[chan].dimms[dimmno].close_pg = GET_BITFIELD(mtr, 0, 0);
  317. imc->chan[chan].dimms[dimmno].bank_xor_enable = GET_BITFIELD(mtr, 9, 9);
  318. imc->chan[chan].dimms[dimmno].fine_grain_bank = GET_BITFIELD(amap, 0, 0);
  319. imc->chan[chan].dimms[dimmno].rowbits = rows;
  320. imc->chan[chan].dimms[dimmno].colbits = cols;
  321. dimm->nr_pages = npages;
  322. dimm->grain = 32;
  323. dimm->dtype = get_width(mtr);
  324. dimm->mtype = MEM_DDR4;
  325. dimm->edac_mode = EDAC_SECDED; /* likely better than this */
  326. snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_MC#%u_Chan#%u_DIMM#%u",
  327. imc->src_id, imc->lmc, chan, dimmno);
  328. return 1;
  329. }
  330. #define SKX_GET_MTMTR(dev, reg) \
  331. pci_read_config_dword((dev), 0x87c, &reg)
  332. static bool skx_check_ecc(struct pci_dev *pdev)
  333. {
  334. u32 mtmtr;
  335. SKX_GET_MTMTR(pdev, mtmtr);
  336. return !!GET_BITFIELD(mtmtr, 2, 2);
  337. }
  338. static int skx_get_dimm_config(struct mem_ctl_info *mci)
  339. {
  340. struct skx_pvt *pvt = mci->pvt_info;
  341. struct skx_imc *imc = pvt->imc;
  342. struct dimm_info *dimm;
  343. int i, j;
  344. u32 mtr, amap;
  345. int ndimms;
  346. for (i = 0; i < NUM_CHANNELS; i++) {
  347. ndimms = 0;
  348. pci_read_config_dword(imc->chan[i].cdev, 0x8C, &amap);
  349. for (j = 0; j < NUM_DIMMS; j++) {
  350. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms,
  351. mci->n_layers, i, j, 0);
  352. pci_read_config_dword(imc->chan[i].cdev,
  353. 0x80 + 4*j, &mtr);
  354. ndimms += get_dimm_info(mtr, amap, dimm, imc, i, j);
  355. }
  356. if (ndimms && !skx_check_ecc(imc->chan[0].cdev)) {
  357. skx_printk(KERN_ERR, "ECC is disabled on imc %d\n", imc->mc);
  358. return -ENODEV;
  359. }
  360. }
  361. return 0;
  362. }
  363. static void skx_unregister_mci(struct skx_imc *imc)
  364. {
  365. struct mem_ctl_info *mci = imc->mci;
  366. if (!mci)
  367. return;
  368. edac_dbg(0, "MC%d: mci = %p\n", imc->mc, mci);
  369. /* Remove MC sysfs nodes */
  370. edac_mc_del_mc(mci->pdev);
  371. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  372. kfree(mci->ctl_name);
  373. edac_mc_free(mci);
  374. }
  375. static int skx_register_mci(struct skx_imc *imc)
  376. {
  377. struct mem_ctl_info *mci;
  378. struct edac_mc_layer layers[2];
  379. struct pci_dev *pdev = imc->chan[0].cdev;
  380. struct skx_pvt *pvt;
  381. int rc;
  382. /* allocate a new MC control structure */
  383. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  384. layers[0].size = NUM_CHANNELS;
  385. layers[0].is_virt_csrow = false;
  386. layers[1].type = EDAC_MC_LAYER_SLOT;
  387. layers[1].size = NUM_DIMMS;
  388. layers[1].is_virt_csrow = true;
  389. mci = edac_mc_alloc(imc->mc, ARRAY_SIZE(layers), layers,
  390. sizeof(struct skx_pvt));
  391. if (unlikely(!mci))
  392. return -ENOMEM;
  393. edac_dbg(0, "MC#%d: mci = %p\n", imc->mc, mci);
  394. /* Associate skx_dev and mci for future usage */
  395. imc->mci = mci;
  396. pvt = mci->pvt_info;
  397. pvt->imc = imc;
  398. mci->ctl_name = kasprintf(GFP_KERNEL, "Skylake Socket#%d IMC#%d",
  399. imc->node_id, imc->lmc);
  400. mci->mtype_cap = MEM_FLAG_DDR4;
  401. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  402. mci->edac_cap = EDAC_FLAG_NONE;
  403. mci->mod_name = "skx_edac.c";
  404. mci->dev_name = pci_name(imc->chan[0].cdev);
  405. mci->mod_ver = SKX_REVISION;
  406. mci->ctl_page_to_phys = NULL;
  407. rc = skx_get_dimm_config(mci);
  408. if (rc < 0)
  409. goto fail;
  410. /* record ptr to the generic device */
  411. mci->pdev = &pdev->dev;
  412. /* add this new MC control structure to EDAC's list of MCs */
  413. if (unlikely(edac_mc_add_mc(mci))) {
  414. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  415. rc = -EINVAL;
  416. goto fail;
  417. }
  418. return 0;
  419. fail:
  420. kfree(mci->ctl_name);
  421. edac_mc_free(mci);
  422. imc->mci = NULL;
  423. return rc;
  424. }
  425. #define SKX_MAX_SAD 24
  426. #define SKX_GET_SAD(d, i, reg) \
  427. pci_read_config_dword((d)->sad_all, 0x60 + 8 * (i), &reg)
  428. #define SKX_GET_ILV(d, i, reg) \
  429. pci_read_config_dword((d)->sad_all, 0x64 + 8 * (i), &reg)
  430. #define SKX_SAD_MOD3MODE(sad) GET_BITFIELD((sad), 30, 31)
  431. #define SKX_SAD_MOD3(sad) GET_BITFIELD((sad), 27, 27)
  432. #define SKX_SAD_LIMIT(sad) (((u64)GET_BITFIELD((sad), 7, 26) << 26) | MASK26)
  433. #define SKX_SAD_MOD3ASMOD2(sad) GET_BITFIELD((sad), 5, 6)
  434. #define SKX_SAD_ATTR(sad) GET_BITFIELD((sad), 3, 4)
  435. #define SKX_SAD_INTERLEAVE(sad) GET_BITFIELD((sad), 1, 2)
  436. #define SKX_SAD_ENABLE(sad) GET_BITFIELD((sad), 0, 0)
  437. #define SKX_ILV_REMOTE(tgt) (((tgt) & 8) == 0)
  438. #define SKX_ILV_TARGET(tgt) ((tgt) & 7)
  439. static bool skx_sad_decode(struct decoded_addr *res)
  440. {
  441. struct skx_dev *d = list_first_entry(&skx_edac_list, typeof(*d), list);
  442. u64 addr = res->addr;
  443. int i, idx, tgt, lchan, shift;
  444. u32 sad, ilv;
  445. u64 limit, prev_limit;
  446. int remote = 0;
  447. /* Simple sanity check for I/O space or out of range */
  448. if (addr >= skx_tohm || (addr >= skx_tolm && addr < BIT_ULL(32))) {
  449. edac_dbg(0, "Address %llx out of range\n", addr);
  450. return false;
  451. }
  452. restart:
  453. prev_limit = 0;
  454. for (i = 0; i < SKX_MAX_SAD; i++) {
  455. SKX_GET_SAD(d, i, sad);
  456. limit = SKX_SAD_LIMIT(sad);
  457. if (SKX_SAD_ENABLE(sad)) {
  458. if (addr >= prev_limit && addr <= limit)
  459. goto sad_found;
  460. }
  461. prev_limit = limit + 1;
  462. }
  463. edac_dbg(0, "No SAD entry for %llx\n", addr);
  464. return false;
  465. sad_found:
  466. SKX_GET_ILV(d, i, ilv);
  467. switch (SKX_SAD_INTERLEAVE(sad)) {
  468. case 0:
  469. idx = GET_BITFIELD(addr, 6, 8);
  470. break;
  471. case 1:
  472. idx = GET_BITFIELD(addr, 8, 10);
  473. break;
  474. case 2:
  475. idx = GET_BITFIELD(addr, 12, 14);
  476. break;
  477. case 3:
  478. idx = GET_BITFIELD(addr, 30, 32);
  479. break;
  480. }
  481. tgt = GET_BITFIELD(ilv, 4 * idx, 4 * idx + 3);
  482. /* If point to another node, find it and start over */
  483. if (SKX_ILV_REMOTE(tgt)) {
  484. if (remote) {
  485. edac_dbg(0, "Double remote!\n");
  486. return false;
  487. }
  488. remote = 1;
  489. list_for_each_entry(d, &skx_edac_list, list) {
  490. if (d->imc[0].src_id == SKX_ILV_TARGET(tgt))
  491. goto restart;
  492. }
  493. edac_dbg(0, "Can't find node %d\n", SKX_ILV_TARGET(tgt));
  494. return false;
  495. }
  496. if (SKX_SAD_MOD3(sad) == 0)
  497. lchan = SKX_ILV_TARGET(tgt);
  498. else {
  499. switch (SKX_SAD_MOD3MODE(sad)) {
  500. case 0:
  501. shift = 6;
  502. break;
  503. case 1:
  504. shift = 8;
  505. break;
  506. case 2:
  507. shift = 12;
  508. break;
  509. default:
  510. edac_dbg(0, "illegal mod3mode\n");
  511. return false;
  512. }
  513. switch (SKX_SAD_MOD3ASMOD2(sad)) {
  514. case 0:
  515. lchan = (addr >> shift) % 3;
  516. break;
  517. case 1:
  518. lchan = (addr >> shift) % 2;
  519. break;
  520. case 2:
  521. lchan = (addr >> shift) % 2;
  522. lchan = (lchan << 1) | ~lchan;
  523. break;
  524. case 3:
  525. lchan = ((addr >> shift) % 2) << 1;
  526. break;
  527. }
  528. lchan = (lchan << 1) | (SKX_ILV_TARGET(tgt) & 1);
  529. }
  530. res->dev = d;
  531. res->socket = d->imc[0].src_id;
  532. res->imc = GET_BITFIELD(d->mcroute, lchan * 3, lchan * 3 + 2);
  533. res->channel = GET_BITFIELD(d->mcroute, lchan * 2 + 18, lchan * 2 + 19);
  534. edac_dbg(2, "%llx: socket=%d imc=%d channel=%d\n",
  535. res->addr, res->socket, res->imc, res->channel);
  536. return true;
  537. }
  538. #define SKX_MAX_TAD 8
  539. #define SKX_GET_TADBASE(d, mc, i, reg) \
  540. pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x850 + 4 * (i), &reg)
  541. #define SKX_GET_TADWAYNESS(d, mc, i, reg) \
  542. pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x880 + 4 * (i), &reg)
  543. #define SKX_GET_TADCHNILVOFFSET(d, mc, ch, i, reg) \
  544. pci_read_config_dword((d)->imc[mc].chan[ch].cdev, 0x90 + 4 * (i), &reg)
  545. #define SKX_TAD_BASE(b) ((u64)GET_BITFIELD((b), 12, 31) << 26)
  546. #define SKX_TAD_SKT_GRAN(b) GET_BITFIELD((b), 4, 5)
  547. #define SKX_TAD_CHN_GRAN(b) GET_BITFIELD((b), 6, 7)
  548. #define SKX_TAD_LIMIT(b) (((u64)GET_BITFIELD((b), 12, 31) << 26) | MASK26)
  549. #define SKX_TAD_OFFSET(b) ((u64)GET_BITFIELD((b), 4, 23) << 26)
  550. #define SKX_TAD_SKTWAYS(b) (1 << GET_BITFIELD((b), 10, 11))
  551. #define SKX_TAD_CHNWAYS(b) (GET_BITFIELD((b), 8, 9) + 1)
  552. /* which bit used for both socket and channel interleave */
  553. static int skx_granularity[] = { 6, 8, 12, 30 };
  554. static u64 skx_do_interleave(u64 addr, int shift, int ways, u64 lowbits)
  555. {
  556. addr >>= shift;
  557. addr /= ways;
  558. addr <<= shift;
  559. return addr | (lowbits & ((1ull << shift) - 1));
  560. }
  561. static bool skx_tad_decode(struct decoded_addr *res)
  562. {
  563. int i;
  564. u32 base, wayness, chnilvoffset;
  565. int skt_interleave_bit, chn_interleave_bit;
  566. u64 channel_addr;
  567. for (i = 0; i < SKX_MAX_TAD; i++) {
  568. SKX_GET_TADBASE(res->dev, res->imc, i, base);
  569. SKX_GET_TADWAYNESS(res->dev, res->imc, i, wayness);
  570. if (SKX_TAD_BASE(base) <= res->addr && res->addr <= SKX_TAD_LIMIT(wayness))
  571. goto tad_found;
  572. }
  573. edac_dbg(0, "No TAD entry for %llx\n", res->addr);
  574. return false;
  575. tad_found:
  576. res->sktways = SKX_TAD_SKTWAYS(wayness);
  577. res->chanways = SKX_TAD_CHNWAYS(wayness);
  578. skt_interleave_bit = skx_granularity[SKX_TAD_SKT_GRAN(base)];
  579. chn_interleave_bit = skx_granularity[SKX_TAD_CHN_GRAN(base)];
  580. SKX_GET_TADCHNILVOFFSET(res->dev, res->imc, res->channel, i, chnilvoffset);
  581. channel_addr = res->addr - SKX_TAD_OFFSET(chnilvoffset);
  582. if (res->chanways == 3 && skt_interleave_bit > chn_interleave_bit) {
  583. /* Must handle channel first, then socket */
  584. channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
  585. res->chanways, channel_addr);
  586. channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
  587. res->sktways, channel_addr);
  588. } else {
  589. /* Handle socket then channel. Preserve low bits from original address */
  590. channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
  591. res->sktways, res->addr);
  592. channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
  593. res->chanways, res->addr);
  594. }
  595. res->chan_addr = channel_addr;
  596. edac_dbg(2, "%llx: chan_addr=%llx sktways=%d chanways=%d\n",
  597. res->addr, res->chan_addr, res->sktways, res->chanways);
  598. return true;
  599. }
  600. #define SKX_MAX_RIR 4
  601. #define SKX_GET_RIRWAYNESS(d, mc, ch, i, reg) \
  602. pci_read_config_dword((d)->imc[mc].chan[ch].cdev, \
  603. 0x108 + 4 * (i), &reg)
  604. #define SKX_GET_RIRILV(d, mc, ch, idx, i, reg) \
  605. pci_read_config_dword((d)->imc[mc].chan[ch].cdev, \
  606. 0x120 + 16 * idx + 4 * (i), &reg)
  607. #define SKX_RIR_VALID(b) GET_BITFIELD((b), 31, 31)
  608. #define SKX_RIR_LIMIT(b) (((u64)GET_BITFIELD((b), 1, 11) << 29) | MASK29)
  609. #define SKX_RIR_WAYS(b) (1 << GET_BITFIELD((b), 28, 29))
  610. #define SKX_RIR_CHAN_RANK(b) GET_BITFIELD((b), 16, 19)
  611. #define SKX_RIR_OFFSET(b) ((u64)(GET_BITFIELD((b), 2, 15) << 26))
  612. static bool skx_rir_decode(struct decoded_addr *res)
  613. {
  614. int i, idx, chan_rank;
  615. int shift;
  616. u32 rirway, rirlv;
  617. u64 rank_addr, prev_limit = 0, limit;
  618. if (res->dev->imc[res->imc].chan[res->channel].dimms[0].close_pg)
  619. shift = 6;
  620. else
  621. shift = 13;
  622. for (i = 0; i < SKX_MAX_RIR; i++) {
  623. SKX_GET_RIRWAYNESS(res->dev, res->imc, res->channel, i, rirway);
  624. limit = SKX_RIR_LIMIT(rirway);
  625. if (SKX_RIR_VALID(rirway)) {
  626. if (prev_limit <= res->chan_addr &&
  627. res->chan_addr <= limit)
  628. goto rir_found;
  629. }
  630. prev_limit = limit;
  631. }
  632. edac_dbg(0, "No RIR entry for %llx\n", res->addr);
  633. return false;
  634. rir_found:
  635. rank_addr = res->chan_addr >> shift;
  636. rank_addr /= SKX_RIR_WAYS(rirway);
  637. rank_addr <<= shift;
  638. rank_addr |= res->chan_addr & GENMASK_ULL(shift - 1, 0);
  639. res->rank_address = rank_addr;
  640. idx = (res->chan_addr >> shift) % SKX_RIR_WAYS(rirway);
  641. SKX_GET_RIRILV(res->dev, res->imc, res->channel, idx, i, rirlv);
  642. res->rank_address = rank_addr - SKX_RIR_OFFSET(rirlv);
  643. chan_rank = SKX_RIR_CHAN_RANK(rirlv);
  644. res->channel_rank = chan_rank;
  645. res->dimm = chan_rank / 4;
  646. res->rank = chan_rank % 4;
  647. edac_dbg(2, "%llx: dimm=%d rank=%d chan_rank=%d rank_addr=%llx\n",
  648. res->addr, res->dimm, res->rank,
  649. res->channel_rank, res->rank_address);
  650. return true;
  651. }
  652. static u8 skx_close_row[] = {
  653. 15, 16, 17, 18, 20, 21, 22, 28, 10, 11, 12, 13, 29, 30, 31, 32, 33
  654. };
  655. static u8 skx_close_column[] = {
  656. 3, 4, 5, 14, 19, 23, 24, 25, 26, 27
  657. };
  658. static u8 skx_open_row[] = {
  659. 14, 15, 16, 20, 28, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33
  660. };
  661. static u8 skx_open_column[] = {
  662. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  663. };
  664. static u8 skx_open_fine_column[] = {
  665. 3, 4, 5, 7, 8, 9, 10, 11, 12, 13
  666. };
  667. static int skx_bits(u64 addr, int nbits, u8 *bits)
  668. {
  669. int i, res = 0;
  670. for (i = 0; i < nbits; i++)
  671. res |= ((addr >> bits[i]) & 1) << i;
  672. return res;
  673. }
  674. static int skx_bank_bits(u64 addr, int b0, int b1, int do_xor, int x0, int x1)
  675. {
  676. int ret = GET_BITFIELD(addr, b0, b0) | (GET_BITFIELD(addr, b1, b1) << 1);
  677. if (do_xor)
  678. ret ^= GET_BITFIELD(addr, x0, x0) | (GET_BITFIELD(addr, x1, x1) << 1);
  679. return ret;
  680. }
  681. static bool skx_mad_decode(struct decoded_addr *r)
  682. {
  683. struct skx_dimm *dimm = &r->dev->imc[r->imc].chan[r->channel].dimms[r->dimm];
  684. int bg0 = dimm->fine_grain_bank ? 6 : 13;
  685. if (dimm->close_pg) {
  686. r->row = skx_bits(r->rank_address, dimm->rowbits, skx_close_row);
  687. r->column = skx_bits(r->rank_address, dimm->colbits, skx_close_column);
  688. r->column |= 0x400; /* C10 is autoprecharge, always set */
  689. r->bank_address = skx_bank_bits(r->rank_address, 8, 9, dimm->bank_xor_enable, 22, 28);
  690. r->bank_group = skx_bank_bits(r->rank_address, 6, 7, dimm->bank_xor_enable, 20, 21);
  691. } else {
  692. r->row = skx_bits(r->rank_address, dimm->rowbits, skx_open_row);
  693. if (dimm->fine_grain_bank)
  694. r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_fine_column);
  695. else
  696. r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_column);
  697. r->bank_address = skx_bank_bits(r->rank_address, 18, 19, dimm->bank_xor_enable, 22, 23);
  698. r->bank_group = skx_bank_bits(r->rank_address, bg0, 17, dimm->bank_xor_enable, 20, 21);
  699. }
  700. r->row &= (1u << dimm->rowbits) - 1;
  701. edac_dbg(2, "%llx: row=%x col=%x bank_addr=%d bank_group=%d\n",
  702. r->addr, r->row, r->column, r->bank_address,
  703. r->bank_group);
  704. return true;
  705. }
  706. static bool skx_decode(struct decoded_addr *res)
  707. {
  708. return skx_sad_decode(res) && skx_tad_decode(res) &&
  709. skx_rir_decode(res) && skx_mad_decode(res);
  710. }
  711. #ifdef CONFIG_EDAC_DEBUG
  712. /*
  713. * Debug feature. Make /sys/kernel/debug/skx_edac_test/addr.
  714. * Write an address to this file to exercise the address decode
  715. * logic in this driver.
  716. */
  717. static struct dentry *skx_test;
  718. static u64 skx_fake_addr;
  719. static int debugfs_u64_set(void *data, u64 val)
  720. {
  721. struct decoded_addr res;
  722. res.addr = val;
  723. skx_decode(&res);
  724. return 0;
  725. }
  726. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  727. static struct dentry *mydebugfs_create(const char *name, umode_t mode,
  728. struct dentry *parent, u64 *value)
  729. {
  730. return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
  731. }
  732. static void setup_skx_debug(void)
  733. {
  734. skx_test = debugfs_create_dir("skx_edac_test", NULL);
  735. mydebugfs_create("addr", S_IWUSR, skx_test, &skx_fake_addr);
  736. }
  737. static void teardown_skx_debug(void)
  738. {
  739. debugfs_remove_recursive(skx_test);
  740. }
  741. #else
  742. static void setup_skx_debug(void)
  743. {
  744. }
  745. static void teardown_skx_debug(void)
  746. {
  747. }
  748. #endif /*CONFIG_EDAC_DEBUG*/
  749. static void skx_mce_output_error(struct mem_ctl_info *mci,
  750. const struct mce *m,
  751. struct decoded_addr *res)
  752. {
  753. enum hw_event_mc_err_type tp_event;
  754. char *type, *optype, msg[256];
  755. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  756. bool overflow = GET_BITFIELD(m->status, 62, 62);
  757. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  758. bool recoverable;
  759. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  760. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  761. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  762. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  763. recoverable = GET_BITFIELD(m->status, 56, 56);
  764. if (uncorrected_error) {
  765. if (ripv) {
  766. type = "FATAL";
  767. tp_event = HW_EVENT_ERR_FATAL;
  768. } else {
  769. type = "NON_FATAL";
  770. tp_event = HW_EVENT_ERR_UNCORRECTED;
  771. }
  772. } else {
  773. type = "CORRECTED";
  774. tp_event = HW_EVENT_ERR_CORRECTED;
  775. }
  776. /*
  777. * According with Table 15-9 of the Intel Architecture spec vol 3A,
  778. * memory errors should fit in this mask:
  779. * 000f 0000 1mmm cccc (binary)
  780. * where:
  781. * f = Correction Report Filtering Bit. If 1, subsequent errors
  782. * won't be shown
  783. * mmm = error type
  784. * cccc = channel
  785. * If the mask doesn't match, report an error to the parsing logic
  786. */
  787. if (!((errcode & 0xef80) == 0x80)) {
  788. optype = "Can't parse: it is not a mem";
  789. } else {
  790. switch (optypenum) {
  791. case 0:
  792. optype = "generic undef request error";
  793. break;
  794. case 1:
  795. optype = "memory read error";
  796. break;
  797. case 2:
  798. optype = "memory write error";
  799. break;
  800. case 3:
  801. optype = "addr/cmd error";
  802. break;
  803. case 4:
  804. optype = "memory scrubbing error";
  805. break;
  806. default:
  807. optype = "reserved";
  808. break;
  809. }
  810. }
  811. snprintf(msg, sizeof(msg),
  812. "%s%s err_code:%04x:%04x socket:%d imc:%d rank:%d bg:%d ba:%d row:%x col:%x",
  813. overflow ? " OVERFLOW" : "",
  814. (uncorrected_error && recoverable) ? " recoverable" : "",
  815. mscod, errcode,
  816. res->socket, res->imc, res->rank,
  817. res->bank_group, res->bank_address, res->row, res->column);
  818. edac_dbg(0, "%s\n", msg);
  819. /* Call the helper to output message */
  820. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  821. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  822. res->channel, res->dimm, -1,
  823. optype, msg);
  824. }
  825. static int skx_mce_check_error(struct notifier_block *nb, unsigned long val,
  826. void *data)
  827. {
  828. struct mce *mce = (struct mce *)data;
  829. struct decoded_addr res;
  830. struct mem_ctl_info *mci;
  831. char *type;
  832. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  833. return NOTIFY_DONE;
  834. /* ignore unless this is memory related with an address */
  835. if ((mce->status & 0xefff) >> 7 != 1 || !(mce->status & MCI_STATUS_ADDRV))
  836. return NOTIFY_DONE;
  837. res.addr = mce->addr;
  838. if (!skx_decode(&res))
  839. return NOTIFY_DONE;
  840. mci = res.dev->imc[res.imc].mci;
  841. if (mce->mcgstatus & MCG_STATUS_MCIP)
  842. type = "Exception";
  843. else
  844. type = "Event";
  845. skx_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
  846. skx_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
  847. "Bank %d: %016Lx\n", mce->extcpu, type,
  848. mce->mcgstatus, mce->bank, mce->status);
  849. skx_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
  850. skx_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
  851. skx_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
  852. skx_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
  853. "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
  854. mce->time, mce->socketid, mce->apicid);
  855. skx_mce_output_error(mci, mce, &res);
  856. return NOTIFY_DONE;
  857. }
  858. static struct notifier_block skx_mce_dec = {
  859. .notifier_call = skx_mce_check_error,
  860. };
  861. static void skx_remove(void)
  862. {
  863. int i, j;
  864. struct skx_dev *d, *tmp;
  865. edac_dbg(0, "\n");
  866. list_for_each_entry_safe(d, tmp, &skx_edac_list, list) {
  867. list_del(&d->list);
  868. for (i = 0; i < NUM_IMC; i++) {
  869. skx_unregister_mci(&d->imc[i]);
  870. for (j = 0; j < NUM_CHANNELS; j++)
  871. pci_dev_put(d->imc[i].chan[j].cdev);
  872. }
  873. pci_dev_put(d->util_all);
  874. pci_dev_put(d->sad_all);
  875. kfree(d);
  876. }
  877. }
  878. /*
  879. * skx_init:
  880. * make sure we are running on the correct cpu model
  881. * search for all the devices we need
  882. * check which DIMMs are present.
  883. */
  884. int __init skx_init(void)
  885. {
  886. const struct x86_cpu_id *id;
  887. const struct munit *m;
  888. int rc = 0, i;
  889. u8 mc = 0, src_id, node_id;
  890. struct skx_dev *d;
  891. edac_dbg(2, "\n");
  892. id = x86_match_cpu(skx_cpuids);
  893. if (!id)
  894. return -ENODEV;
  895. rc = skx_get_hi_lo();
  896. if (rc)
  897. return rc;
  898. rc = get_all_bus_mappings();
  899. if (rc < 0)
  900. goto fail;
  901. if (rc == 0) {
  902. edac_dbg(2, "No memory controllers found\n");
  903. return -ENODEV;
  904. }
  905. for (m = skx_all_munits; m->did; m++) {
  906. rc = get_all_munits(m);
  907. if (rc < 0)
  908. goto fail;
  909. if (rc != m->per_socket * skx_num_sockets) {
  910. edac_dbg(2, "Expected %d, got %d of %x\n",
  911. m->per_socket * skx_num_sockets, rc, m->did);
  912. rc = -ENODEV;
  913. goto fail;
  914. }
  915. }
  916. list_for_each_entry(d, &skx_edac_list, list) {
  917. src_id = get_src_id(d);
  918. node_id = skx_get_node_id(d);
  919. edac_dbg(2, "src_id=%d node_id=%d\n", src_id, node_id);
  920. for (i = 0; i < NUM_IMC; i++) {
  921. d->imc[i].mc = mc++;
  922. d->imc[i].lmc = i;
  923. d->imc[i].src_id = src_id;
  924. d->imc[i].node_id = node_id;
  925. rc = skx_register_mci(&d->imc[i]);
  926. if (rc < 0)
  927. goto fail;
  928. }
  929. }
  930. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  931. opstate_init();
  932. setup_skx_debug();
  933. mce_register_decode_chain(&skx_mce_dec);
  934. return 0;
  935. fail:
  936. skx_remove();
  937. return rc;
  938. }
  939. static void __exit skx_exit(void)
  940. {
  941. edac_dbg(2, "\n");
  942. mce_unregister_decode_chain(&skx_mce_dec);
  943. skx_remove();
  944. teardown_skx_debug();
  945. }
  946. module_init(skx_init);
  947. module_exit(skx_exit);
  948. module_param(edac_op_state, int, 0444);
  949. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  950. MODULE_LICENSE("GPL v2");
  951. MODULE_AUTHOR("Tony Luck");
  952. MODULE_DESCRIPTION("MC Driver for Intel Skylake server processors");