pnd2_edac.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. /*
  2. * Driver for Pondicherry2 memory controller.
  3. *
  4. * Copyright (c) 2016, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * [Derived from sb_edac.c]
  16. *
  17. * Translation of system physical addresses to DIMM addresses
  18. * is a two stage process:
  19. *
  20. * First the Pondicherry 2 memory controller handles slice and channel interleaving
  21. * in "sys2pmi()". This is (almost) completley common between platforms.
  22. *
  23. * Then a platform specific dunit (DIMM unit) completes the process to provide DIMM,
  24. * rank, bank, row and column using the appropriate "dunit_ops" functions/parameters.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/pci.h>
  29. #include <linux/pci_ids.h>
  30. #include <linux/slab.h>
  31. #include <linux/delay.h>
  32. #include <linux/edac.h>
  33. #include <linux/mmzone.h>
  34. #include <linux/smp.h>
  35. #include <linux/bitmap.h>
  36. #include <linux/math64.h>
  37. #include <linux/mod_devicetable.h>
  38. #include <asm/cpu_device_id.h>
  39. #include <asm/intel-family.h>
  40. #include <asm/processor.h>
  41. #include <asm/mce.h>
  42. #include "edac_mc.h"
  43. #include "edac_module.h"
  44. #include "pnd2_edac.h"
  45. #define APL_NUM_CHANNELS 4
  46. #define DNV_NUM_CHANNELS 2
  47. #define DNV_MAX_DIMMS 2 /* Max DIMMs per channel */
  48. enum type {
  49. APL,
  50. DNV, /* All requests go to PMI CH0 on each slice (CH1 disabled) */
  51. };
  52. struct dram_addr {
  53. int chan;
  54. int dimm;
  55. int rank;
  56. int bank;
  57. int row;
  58. int col;
  59. };
  60. struct pnd2_pvt {
  61. int dimm_geom[APL_NUM_CHANNELS];
  62. u64 tolm, tohm;
  63. };
  64. /*
  65. * System address space is divided into multiple regions with
  66. * different interleave rules in each. The as0/as1 regions
  67. * have no interleaving at all. The as2 region is interleaved
  68. * between two channels. The mot region is magic and may overlap
  69. * other regions, with its interleave rules taking precedence.
  70. * Addresses not in any of these regions are interleaved across
  71. * all four channels.
  72. */
  73. static struct region {
  74. u64 base;
  75. u64 limit;
  76. u8 enabled;
  77. } mot, as0, as1, as2;
  78. static struct dunit_ops {
  79. char *name;
  80. enum type type;
  81. int pmiaddr_shift;
  82. int pmiidx_shift;
  83. int channels;
  84. int dimms_per_channel;
  85. int (*rd_reg)(int port, int off, int op, void *data, size_t sz, char *name);
  86. int (*get_registers)(void);
  87. int (*check_ecc)(void);
  88. void (*mk_region)(char *name, struct region *rp, void *asym);
  89. void (*get_dimm_config)(struct mem_ctl_info *mci);
  90. int (*pmi2mem)(struct mem_ctl_info *mci, u64 pmiaddr, u32 pmiidx,
  91. struct dram_addr *daddr, char *msg);
  92. } *ops;
  93. static struct mem_ctl_info *pnd2_mci;
  94. #define PND2_MSG_SIZE 256
  95. /* Debug macros */
  96. #define pnd2_printk(level, fmt, arg...) \
  97. edac_printk(level, "pnd2", fmt, ##arg)
  98. #define pnd2_mc_printk(mci, level, fmt, arg...) \
  99. edac_mc_chipset_printk(mci, level, "pnd2", fmt, ##arg)
  100. #define MOT_CHAN_INTLV_BIT_1SLC_2CH 12
  101. #define MOT_CHAN_INTLV_BIT_2SLC_2CH 13
  102. #define SELECTOR_DISABLED (-1)
  103. #define _4GB (1ul << 32)
  104. #define PMI_ADDRESS_WIDTH 31
  105. #define PND_MAX_PHYS_BIT 39
  106. #define APL_ASYMSHIFT 28
  107. #define DNV_ASYMSHIFT 31
  108. #define CH_HASH_MASK_LSB 6
  109. #define SLICE_HASH_MASK_LSB 6
  110. #define MOT_SLC_INTLV_BIT 12
  111. #define LOG2_PMI_ADDR_GRANULARITY 5
  112. #define MOT_SHIFT 24
  113. #define GET_BITFIELD(v, lo, hi) (((v) & GENMASK_ULL(hi, lo)) >> (lo))
  114. #define U64_LSHIFT(val, s) ((u64)(val) << (s))
  115. /*
  116. * On Apollo Lake we access memory controller registers via a
  117. * side-band mailbox style interface in a hidden PCI device
  118. * configuration space.
  119. */
  120. static struct pci_bus *p2sb_bus;
  121. #define P2SB_DEVFN PCI_DEVFN(0xd, 0)
  122. #define P2SB_ADDR_OFF 0xd0
  123. #define P2SB_DATA_OFF 0xd4
  124. #define P2SB_STAT_OFF 0xd8
  125. #define P2SB_ROUT_OFF 0xda
  126. #define P2SB_EADD_OFF 0xdc
  127. #define P2SB_HIDE_OFF 0xe1
  128. #define P2SB_BUSY 1
  129. #define P2SB_READ(size, off, ptr) \
  130. pci_bus_read_config_##size(p2sb_bus, P2SB_DEVFN, off, ptr)
  131. #define P2SB_WRITE(size, off, val) \
  132. pci_bus_write_config_##size(p2sb_bus, P2SB_DEVFN, off, val)
  133. static bool p2sb_is_busy(u16 *status)
  134. {
  135. P2SB_READ(word, P2SB_STAT_OFF, status);
  136. return !!(*status & P2SB_BUSY);
  137. }
  138. static int _apl_rd_reg(int port, int off, int op, u32 *data)
  139. {
  140. int retries = 0xff, ret;
  141. u16 status;
  142. u8 hidden;
  143. /* Unhide the P2SB device, if it's hidden */
  144. P2SB_READ(byte, P2SB_HIDE_OFF, &hidden);
  145. if (hidden)
  146. P2SB_WRITE(byte, P2SB_HIDE_OFF, 0);
  147. if (p2sb_is_busy(&status)) {
  148. ret = -EAGAIN;
  149. goto out;
  150. }
  151. P2SB_WRITE(dword, P2SB_ADDR_OFF, (port << 24) | off);
  152. P2SB_WRITE(dword, P2SB_DATA_OFF, 0);
  153. P2SB_WRITE(dword, P2SB_EADD_OFF, 0);
  154. P2SB_WRITE(word, P2SB_ROUT_OFF, 0);
  155. P2SB_WRITE(word, P2SB_STAT_OFF, (op << 8) | P2SB_BUSY);
  156. while (p2sb_is_busy(&status)) {
  157. if (retries-- == 0) {
  158. ret = -EBUSY;
  159. goto out;
  160. }
  161. }
  162. P2SB_READ(dword, P2SB_DATA_OFF, data);
  163. ret = (status >> 1) & 0x3;
  164. out:
  165. /* Hide the P2SB device, if it was hidden before */
  166. if (hidden)
  167. P2SB_WRITE(byte, P2SB_HIDE_OFF, hidden);
  168. return ret;
  169. }
  170. static int apl_rd_reg(int port, int off, int op, void *data, size_t sz, char *name)
  171. {
  172. int ret = 0;
  173. edac_dbg(2, "Read %s port=%x off=%x op=%x\n", name, port, off, op);
  174. switch (sz) {
  175. case 8:
  176. ret = _apl_rd_reg(port, off + 4, op, (u32 *)(data + 4));
  177. /* fall through */
  178. case 4:
  179. ret |= _apl_rd_reg(port, off, op, (u32 *)data);
  180. pnd2_printk(KERN_DEBUG, "%s=%x%08x ret=%d\n", name,
  181. sz == 8 ? *((u32 *)(data + 4)) : 0, *((u32 *)data), ret);
  182. break;
  183. }
  184. return ret;
  185. }
  186. static u64 get_mem_ctrl_hub_base_addr(void)
  187. {
  188. struct b_cr_mchbar_lo_pci lo;
  189. struct b_cr_mchbar_hi_pci hi;
  190. struct pci_dev *pdev;
  191. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x1980, NULL);
  192. if (pdev) {
  193. pci_read_config_dword(pdev, 0x48, (u32 *)&lo);
  194. pci_read_config_dword(pdev, 0x4c, (u32 *)&hi);
  195. pci_dev_put(pdev);
  196. } else {
  197. return 0;
  198. }
  199. if (!lo.enable) {
  200. edac_dbg(2, "MMIO via memory controller hub base address is disabled!\n");
  201. return 0;
  202. }
  203. return U64_LSHIFT(hi.base, 32) | U64_LSHIFT(lo.base, 15);
  204. }
  205. static u64 get_sideband_reg_base_addr(void)
  206. {
  207. struct pci_dev *pdev;
  208. u32 hi, lo;
  209. u8 hidden;
  210. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x19dd, NULL);
  211. if (pdev) {
  212. /* Unhide the P2SB device, if it's hidden */
  213. pci_read_config_byte(pdev, 0xe1, &hidden);
  214. if (hidden)
  215. pci_write_config_byte(pdev, 0xe1, 0);
  216. pci_read_config_dword(pdev, 0x10, &lo);
  217. pci_read_config_dword(pdev, 0x14, &hi);
  218. lo &= 0xfffffff0;
  219. /* Hide the P2SB device, if it was hidden before */
  220. if (hidden)
  221. pci_write_config_byte(pdev, 0xe1, hidden);
  222. pci_dev_put(pdev);
  223. return (U64_LSHIFT(hi, 32) | U64_LSHIFT(lo, 0));
  224. } else {
  225. return 0xfd000000;
  226. }
  227. }
  228. #define DNV_MCHBAR_SIZE 0x8000
  229. #define DNV_SB_PORT_SIZE 0x10000
  230. static int dnv_rd_reg(int port, int off, int op, void *data, size_t sz, char *name)
  231. {
  232. struct pci_dev *pdev;
  233. char *base;
  234. u64 addr;
  235. unsigned long size;
  236. if (op == 4) {
  237. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x1980, NULL);
  238. if (!pdev)
  239. return -ENODEV;
  240. pci_read_config_dword(pdev, off, data);
  241. pci_dev_put(pdev);
  242. } else {
  243. /* MMIO via memory controller hub base address */
  244. if (op == 0 && port == 0x4c) {
  245. addr = get_mem_ctrl_hub_base_addr();
  246. if (!addr)
  247. return -ENODEV;
  248. size = DNV_MCHBAR_SIZE;
  249. } else {
  250. /* MMIO via sideband register base address */
  251. addr = get_sideband_reg_base_addr();
  252. if (!addr)
  253. return -ENODEV;
  254. addr += (port << 16);
  255. size = DNV_SB_PORT_SIZE;
  256. }
  257. base = ioremap((resource_size_t)addr, size);
  258. if (!base)
  259. return -ENODEV;
  260. if (sz == 8)
  261. *(u32 *)(data + 4) = *(u32 *)(base + off + 4);
  262. *(u32 *)data = *(u32 *)(base + off);
  263. iounmap(base);
  264. }
  265. edac_dbg(2, "Read %s=%.8x_%.8x\n", name,
  266. (sz == 8) ? *(u32 *)(data + 4) : 0, *(u32 *)data);
  267. return 0;
  268. }
  269. #define RD_REGP(regp, regname, port) \
  270. ops->rd_reg(port, \
  271. regname##_offset, \
  272. regname##_r_opcode, \
  273. regp, sizeof(struct regname), \
  274. #regname)
  275. #define RD_REG(regp, regname) \
  276. ops->rd_reg(regname ## _port, \
  277. regname##_offset, \
  278. regname##_r_opcode, \
  279. regp, sizeof(struct regname), \
  280. #regname)
  281. static u64 top_lm, top_hm;
  282. static bool two_slices;
  283. static bool two_channels; /* Both PMI channels in one slice enabled */
  284. static u8 sym_chan_mask;
  285. static u8 asym_chan_mask;
  286. static u8 chan_mask;
  287. static int slice_selector = -1;
  288. static int chan_selector = -1;
  289. static u64 slice_hash_mask;
  290. static u64 chan_hash_mask;
  291. static void mk_region(char *name, struct region *rp, u64 base, u64 limit)
  292. {
  293. rp->enabled = 1;
  294. rp->base = base;
  295. rp->limit = limit;
  296. edac_dbg(2, "Region:%s [%llx, %llx]\n", name, base, limit);
  297. }
  298. static void mk_region_mask(char *name, struct region *rp, u64 base, u64 mask)
  299. {
  300. if (mask == 0) {
  301. pr_info(FW_BUG "MOT mask cannot be zero\n");
  302. return;
  303. }
  304. if (mask != GENMASK_ULL(PND_MAX_PHYS_BIT, __ffs(mask))) {
  305. pr_info(FW_BUG "MOT mask not power of two\n");
  306. return;
  307. }
  308. if (base & ~mask) {
  309. pr_info(FW_BUG "MOT region base/mask alignment error\n");
  310. return;
  311. }
  312. rp->base = base;
  313. rp->limit = (base | ~mask) & GENMASK_ULL(PND_MAX_PHYS_BIT, 0);
  314. rp->enabled = 1;
  315. edac_dbg(2, "Region:%s [%llx, %llx]\n", name, base, rp->limit);
  316. }
  317. static bool in_region(struct region *rp, u64 addr)
  318. {
  319. if (!rp->enabled)
  320. return false;
  321. return rp->base <= addr && addr <= rp->limit;
  322. }
  323. static int gen_sym_mask(struct b_cr_slice_channel_hash *p)
  324. {
  325. int mask = 0;
  326. if (!p->slice_0_mem_disabled)
  327. mask |= p->sym_slice0_channel_enabled;
  328. if (!p->slice_1_disabled)
  329. mask |= p->sym_slice1_channel_enabled << 2;
  330. if (p->ch_1_disabled || p->enable_pmi_dual_data_mode)
  331. mask &= 0x5;
  332. return mask;
  333. }
  334. static int gen_asym_mask(struct b_cr_slice_channel_hash *p,
  335. struct b_cr_asym_mem_region0_mchbar *as0,
  336. struct b_cr_asym_mem_region1_mchbar *as1,
  337. struct b_cr_asym_2way_mem_region_mchbar *as2way)
  338. {
  339. const int intlv[] = { 0x5, 0xA, 0x3, 0xC };
  340. int mask = 0;
  341. if (as2way->asym_2way_interleave_enable)
  342. mask = intlv[as2way->asym_2way_intlv_mode];
  343. if (as0->slice0_asym_enable)
  344. mask |= (1 << as0->slice0_asym_channel_select);
  345. if (as1->slice1_asym_enable)
  346. mask |= (4 << as1->slice1_asym_channel_select);
  347. if (p->slice_0_mem_disabled)
  348. mask &= 0xc;
  349. if (p->slice_1_disabled)
  350. mask &= 0x3;
  351. if (p->ch_1_disabled || p->enable_pmi_dual_data_mode)
  352. mask &= 0x5;
  353. return mask;
  354. }
  355. static struct b_cr_tolud_pci tolud;
  356. static struct b_cr_touud_lo_pci touud_lo;
  357. static struct b_cr_touud_hi_pci touud_hi;
  358. static struct b_cr_asym_mem_region0_mchbar asym0;
  359. static struct b_cr_asym_mem_region1_mchbar asym1;
  360. static struct b_cr_asym_2way_mem_region_mchbar asym_2way;
  361. static struct b_cr_mot_out_base_mchbar mot_base;
  362. static struct b_cr_mot_out_mask_mchbar mot_mask;
  363. static struct b_cr_slice_channel_hash chash;
  364. /* Apollo Lake dunit */
  365. /*
  366. * Validated on board with just two DIMMs in the [0] and [2] positions
  367. * in this array. Other port number matches documentation, but caution
  368. * advised.
  369. */
  370. static const int apl_dports[APL_NUM_CHANNELS] = { 0x18, 0x10, 0x11, 0x19 };
  371. static struct d_cr_drp0 drp0[APL_NUM_CHANNELS];
  372. /* Denverton dunit */
  373. static const int dnv_dports[DNV_NUM_CHANNELS] = { 0x10, 0x12 };
  374. static struct d_cr_dsch dsch;
  375. static struct d_cr_ecc_ctrl ecc_ctrl[DNV_NUM_CHANNELS];
  376. static struct d_cr_drp drp[DNV_NUM_CHANNELS];
  377. static struct d_cr_dmap dmap[DNV_NUM_CHANNELS];
  378. static struct d_cr_dmap1 dmap1[DNV_NUM_CHANNELS];
  379. static struct d_cr_dmap2 dmap2[DNV_NUM_CHANNELS];
  380. static struct d_cr_dmap3 dmap3[DNV_NUM_CHANNELS];
  381. static struct d_cr_dmap4 dmap4[DNV_NUM_CHANNELS];
  382. static struct d_cr_dmap5 dmap5[DNV_NUM_CHANNELS];
  383. static void apl_mk_region(char *name, struct region *rp, void *asym)
  384. {
  385. struct b_cr_asym_mem_region0_mchbar *a = asym;
  386. mk_region(name, rp,
  387. U64_LSHIFT(a->slice0_asym_base, APL_ASYMSHIFT),
  388. U64_LSHIFT(a->slice0_asym_limit, APL_ASYMSHIFT) +
  389. GENMASK_ULL(APL_ASYMSHIFT - 1, 0));
  390. }
  391. static void dnv_mk_region(char *name, struct region *rp, void *asym)
  392. {
  393. struct b_cr_asym_mem_region_denverton *a = asym;
  394. mk_region(name, rp,
  395. U64_LSHIFT(a->slice_asym_base, DNV_ASYMSHIFT),
  396. U64_LSHIFT(a->slice_asym_limit, DNV_ASYMSHIFT) +
  397. GENMASK_ULL(DNV_ASYMSHIFT - 1, 0));
  398. }
  399. static int apl_get_registers(void)
  400. {
  401. int ret = -ENODEV;
  402. int i;
  403. if (RD_REG(&asym_2way, b_cr_asym_2way_mem_region_mchbar))
  404. return -ENODEV;
  405. /*
  406. * RD_REGP() will fail for unpopulated or non-existent
  407. * DIMM slots. Return success if we find at least one DIMM.
  408. */
  409. for (i = 0; i < APL_NUM_CHANNELS; i++)
  410. if (!RD_REGP(&drp0[i], d_cr_drp0, apl_dports[i]))
  411. ret = 0;
  412. return ret;
  413. }
  414. static int dnv_get_registers(void)
  415. {
  416. int i;
  417. if (RD_REG(&dsch, d_cr_dsch))
  418. return -ENODEV;
  419. for (i = 0; i < DNV_NUM_CHANNELS; i++)
  420. if (RD_REGP(&ecc_ctrl[i], d_cr_ecc_ctrl, dnv_dports[i]) ||
  421. RD_REGP(&drp[i], d_cr_drp, dnv_dports[i]) ||
  422. RD_REGP(&dmap[i], d_cr_dmap, dnv_dports[i]) ||
  423. RD_REGP(&dmap1[i], d_cr_dmap1, dnv_dports[i]) ||
  424. RD_REGP(&dmap2[i], d_cr_dmap2, dnv_dports[i]) ||
  425. RD_REGP(&dmap3[i], d_cr_dmap3, dnv_dports[i]) ||
  426. RD_REGP(&dmap4[i], d_cr_dmap4, dnv_dports[i]) ||
  427. RD_REGP(&dmap5[i], d_cr_dmap5, dnv_dports[i]))
  428. return -ENODEV;
  429. return 0;
  430. }
  431. /*
  432. * Read all the h/w config registers once here (they don't
  433. * change at run time. Figure out which address ranges have
  434. * which interleave characteristics.
  435. */
  436. static int get_registers(void)
  437. {
  438. const int intlv[] = { 10, 11, 12, 12 };
  439. if (RD_REG(&tolud, b_cr_tolud_pci) ||
  440. RD_REG(&touud_lo, b_cr_touud_lo_pci) ||
  441. RD_REG(&touud_hi, b_cr_touud_hi_pci) ||
  442. RD_REG(&asym0, b_cr_asym_mem_region0_mchbar) ||
  443. RD_REG(&asym1, b_cr_asym_mem_region1_mchbar) ||
  444. RD_REG(&mot_base, b_cr_mot_out_base_mchbar) ||
  445. RD_REG(&mot_mask, b_cr_mot_out_mask_mchbar) ||
  446. RD_REG(&chash, b_cr_slice_channel_hash))
  447. return -ENODEV;
  448. if (ops->get_registers())
  449. return -ENODEV;
  450. if (ops->type == DNV) {
  451. /* PMI channel idx (always 0) for asymmetric region */
  452. asym0.slice0_asym_channel_select = 0;
  453. asym1.slice1_asym_channel_select = 0;
  454. /* PMI channel bitmap (always 1) for symmetric region */
  455. chash.sym_slice0_channel_enabled = 0x1;
  456. chash.sym_slice1_channel_enabled = 0x1;
  457. }
  458. if (asym0.slice0_asym_enable)
  459. ops->mk_region("as0", &as0, &asym0);
  460. if (asym1.slice1_asym_enable)
  461. ops->mk_region("as1", &as1, &asym1);
  462. if (asym_2way.asym_2way_interleave_enable) {
  463. mk_region("as2way", &as2,
  464. U64_LSHIFT(asym_2way.asym_2way_base, APL_ASYMSHIFT),
  465. U64_LSHIFT(asym_2way.asym_2way_limit, APL_ASYMSHIFT) +
  466. GENMASK_ULL(APL_ASYMSHIFT - 1, 0));
  467. }
  468. if (mot_base.imr_en) {
  469. mk_region_mask("mot", &mot,
  470. U64_LSHIFT(mot_base.mot_out_base, MOT_SHIFT),
  471. U64_LSHIFT(mot_mask.mot_out_mask, MOT_SHIFT));
  472. }
  473. top_lm = U64_LSHIFT(tolud.tolud, 20);
  474. top_hm = U64_LSHIFT(touud_hi.touud, 32) | U64_LSHIFT(touud_lo.touud, 20);
  475. two_slices = !chash.slice_1_disabled &&
  476. !chash.slice_0_mem_disabled &&
  477. (chash.sym_slice0_channel_enabled != 0) &&
  478. (chash.sym_slice1_channel_enabled != 0);
  479. two_channels = !chash.ch_1_disabled &&
  480. !chash.enable_pmi_dual_data_mode &&
  481. ((chash.sym_slice0_channel_enabled == 3) ||
  482. (chash.sym_slice1_channel_enabled == 3));
  483. sym_chan_mask = gen_sym_mask(&chash);
  484. asym_chan_mask = gen_asym_mask(&chash, &asym0, &asym1, &asym_2way);
  485. chan_mask = sym_chan_mask | asym_chan_mask;
  486. if (two_slices && !two_channels) {
  487. if (chash.hvm_mode)
  488. slice_selector = 29;
  489. else
  490. slice_selector = intlv[chash.interleave_mode];
  491. } else if (!two_slices && two_channels) {
  492. if (chash.hvm_mode)
  493. chan_selector = 29;
  494. else
  495. chan_selector = intlv[chash.interleave_mode];
  496. } else if (two_slices && two_channels) {
  497. if (chash.hvm_mode) {
  498. slice_selector = 29;
  499. chan_selector = 30;
  500. } else {
  501. slice_selector = intlv[chash.interleave_mode];
  502. chan_selector = intlv[chash.interleave_mode] + 1;
  503. }
  504. }
  505. if (two_slices) {
  506. if (!chash.hvm_mode)
  507. slice_hash_mask = chash.slice_hash_mask << SLICE_HASH_MASK_LSB;
  508. if (!two_channels)
  509. slice_hash_mask |= BIT_ULL(slice_selector);
  510. }
  511. if (two_channels) {
  512. if (!chash.hvm_mode)
  513. chan_hash_mask = chash.ch_hash_mask << CH_HASH_MASK_LSB;
  514. if (!two_slices)
  515. chan_hash_mask |= BIT_ULL(chan_selector);
  516. }
  517. return 0;
  518. }
  519. /* Get a contiguous memory address (remove the MMIO gap) */
  520. static u64 remove_mmio_gap(u64 sys)
  521. {
  522. return (sys < _4GB) ? sys : sys - (_4GB - top_lm);
  523. }
  524. /* Squeeze out one address bit, shift upper part down to fill gap */
  525. static void remove_addr_bit(u64 *addr, int bitidx)
  526. {
  527. u64 mask;
  528. if (bitidx == -1)
  529. return;
  530. mask = (1ull << bitidx) - 1;
  531. *addr = ((*addr >> 1) & ~mask) | (*addr & mask);
  532. }
  533. /* XOR all the bits from addr specified in mask */
  534. static int hash_by_mask(u64 addr, u64 mask)
  535. {
  536. u64 result = addr & mask;
  537. result = (result >> 32) ^ result;
  538. result = (result >> 16) ^ result;
  539. result = (result >> 8) ^ result;
  540. result = (result >> 4) ^ result;
  541. result = (result >> 2) ^ result;
  542. result = (result >> 1) ^ result;
  543. return (int)result & 1;
  544. }
  545. /*
  546. * First stage decode. Take the system address and figure out which
  547. * second stage will deal with it based on interleave modes.
  548. */
  549. static int sys2pmi(const u64 addr, u32 *pmiidx, u64 *pmiaddr, char *msg)
  550. {
  551. u64 contig_addr, contig_base, contig_offset, contig_base_adj;
  552. int mot_intlv_bit = two_slices ? MOT_CHAN_INTLV_BIT_2SLC_2CH :
  553. MOT_CHAN_INTLV_BIT_1SLC_2CH;
  554. int slice_intlv_bit_rm = SELECTOR_DISABLED;
  555. int chan_intlv_bit_rm = SELECTOR_DISABLED;
  556. /* Determine if address is in the MOT region. */
  557. bool mot_hit = in_region(&mot, addr);
  558. /* Calculate the number of symmetric regions enabled. */
  559. int sym_channels = hweight8(sym_chan_mask);
  560. /*
  561. * The amount we need to shift the asym base can be determined by the
  562. * number of enabled symmetric channels.
  563. * NOTE: This can only work because symmetric memory is not supposed
  564. * to do a 3-way interleave.
  565. */
  566. int sym_chan_shift = sym_channels >> 1;
  567. /* Give up if address is out of range, or in MMIO gap */
  568. if (addr >= (1ul << PND_MAX_PHYS_BIT) ||
  569. (addr >= top_lm && addr < _4GB) || addr >= top_hm) {
  570. snprintf(msg, PND2_MSG_SIZE, "Error address 0x%llx is not DRAM", addr);
  571. return -EINVAL;
  572. }
  573. /* Get a contiguous memory address (remove the MMIO gap) */
  574. contig_addr = remove_mmio_gap(addr);
  575. if (in_region(&as0, addr)) {
  576. *pmiidx = asym0.slice0_asym_channel_select;
  577. contig_base = remove_mmio_gap(as0.base);
  578. contig_offset = contig_addr - contig_base;
  579. contig_base_adj = (contig_base >> sym_chan_shift) *
  580. ((chash.sym_slice0_channel_enabled >> (*pmiidx & 1)) & 1);
  581. contig_addr = contig_offset + ((sym_channels > 0) ? contig_base_adj : 0ull);
  582. } else if (in_region(&as1, addr)) {
  583. *pmiidx = 2u + asym1.slice1_asym_channel_select;
  584. contig_base = remove_mmio_gap(as1.base);
  585. contig_offset = contig_addr - contig_base;
  586. contig_base_adj = (contig_base >> sym_chan_shift) *
  587. ((chash.sym_slice1_channel_enabled >> (*pmiidx & 1)) & 1);
  588. contig_addr = contig_offset + ((sym_channels > 0) ? contig_base_adj : 0ull);
  589. } else if (in_region(&as2, addr) && (asym_2way.asym_2way_intlv_mode == 0x3ul)) {
  590. bool channel1;
  591. mot_intlv_bit = MOT_CHAN_INTLV_BIT_1SLC_2CH;
  592. *pmiidx = (asym_2way.asym_2way_intlv_mode & 1) << 1;
  593. channel1 = mot_hit ? ((bool)((addr >> mot_intlv_bit) & 1)) :
  594. hash_by_mask(contig_addr, chan_hash_mask);
  595. *pmiidx |= (u32)channel1;
  596. contig_base = remove_mmio_gap(as2.base);
  597. chan_intlv_bit_rm = mot_hit ? mot_intlv_bit : chan_selector;
  598. contig_offset = contig_addr - contig_base;
  599. remove_addr_bit(&contig_offset, chan_intlv_bit_rm);
  600. contig_addr = (contig_base >> sym_chan_shift) + contig_offset;
  601. } else {
  602. /* Otherwise we're in normal, boring symmetric mode. */
  603. *pmiidx = 0u;
  604. if (two_slices) {
  605. bool slice1;
  606. if (mot_hit) {
  607. slice_intlv_bit_rm = MOT_SLC_INTLV_BIT;
  608. slice1 = (addr >> MOT_SLC_INTLV_BIT) & 1;
  609. } else {
  610. slice_intlv_bit_rm = slice_selector;
  611. slice1 = hash_by_mask(addr, slice_hash_mask);
  612. }
  613. *pmiidx = (u32)slice1 << 1;
  614. }
  615. if (two_channels) {
  616. bool channel1;
  617. mot_intlv_bit = two_slices ? MOT_CHAN_INTLV_BIT_2SLC_2CH :
  618. MOT_CHAN_INTLV_BIT_1SLC_2CH;
  619. if (mot_hit) {
  620. chan_intlv_bit_rm = mot_intlv_bit;
  621. channel1 = (addr >> mot_intlv_bit) & 1;
  622. } else {
  623. chan_intlv_bit_rm = chan_selector;
  624. channel1 = hash_by_mask(contig_addr, chan_hash_mask);
  625. }
  626. *pmiidx |= (u32)channel1;
  627. }
  628. }
  629. /* Remove the chan_selector bit first */
  630. remove_addr_bit(&contig_addr, chan_intlv_bit_rm);
  631. /* Remove the slice bit (we remove it second because it must be lower */
  632. remove_addr_bit(&contig_addr, slice_intlv_bit_rm);
  633. *pmiaddr = contig_addr;
  634. return 0;
  635. }
  636. /* Translate PMI address to memory (rank, row, bank, column) */
  637. #define C(n) (0x10 | (n)) /* column */
  638. #define B(n) (0x20 | (n)) /* bank */
  639. #define R(n) (0x40 | (n)) /* row */
  640. #define RS (0x80) /* rank */
  641. /* addrdec values */
  642. #define AMAP_1KB 0
  643. #define AMAP_2KB 1
  644. #define AMAP_4KB 2
  645. #define AMAP_RSVD 3
  646. /* dden values */
  647. #define DEN_4Gb 0
  648. #define DEN_8Gb 2
  649. /* dwid values */
  650. #define X8 0
  651. #define X16 1
  652. static struct dimm_geometry {
  653. u8 addrdec;
  654. u8 dden;
  655. u8 dwid;
  656. u8 rowbits, colbits;
  657. u16 bits[PMI_ADDRESS_WIDTH];
  658. } dimms[] = {
  659. {
  660. .addrdec = AMAP_1KB, .dden = DEN_4Gb, .dwid = X16,
  661. .rowbits = 15, .colbits = 10,
  662. .bits = {
  663. C(2), C(3), C(4), C(5), C(6), B(0), B(1), B(2), R(0),
  664. R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8), R(9),
  665. R(10), C(7), C(8), C(9), R(11), RS, R(12), R(13), R(14),
  666. 0, 0, 0, 0
  667. }
  668. },
  669. {
  670. .addrdec = AMAP_1KB, .dden = DEN_4Gb, .dwid = X8,
  671. .rowbits = 16, .colbits = 10,
  672. .bits = {
  673. C(2), C(3), C(4), C(5), C(6), B(0), B(1), B(2), R(0),
  674. R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8), R(9),
  675. R(10), C(7), C(8), C(9), R(11), RS, R(12), R(13), R(14),
  676. R(15), 0, 0, 0
  677. }
  678. },
  679. {
  680. .addrdec = AMAP_1KB, .dden = DEN_8Gb, .dwid = X16,
  681. .rowbits = 16, .colbits = 10,
  682. .bits = {
  683. C(2), C(3), C(4), C(5), C(6), B(0), B(1), B(2), R(0),
  684. R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8), R(9),
  685. R(10), C(7), C(8), C(9), R(11), RS, R(12), R(13), R(14),
  686. R(15), 0, 0, 0
  687. }
  688. },
  689. {
  690. .addrdec = AMAP_1KB, .dden = DEN_8Gb, .dwid = X8,
  691. .rowbits = 16, .colbits = 11,
  692. .bits = {
  693. C(2), C(3), C(4), C(5), C(6), B(0), B(1), B(2), R(0),
  694. R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8), R(9),
  695. R(10), C(7), C(8), C(9), R(11), RS, C(11), R(12), R(13),
  696. R(14), R(15), 0, 0
  697. }
  698. },
  699. {
  700. .addrdec = AMAP_2KB, .dden = DEN_4Gb, .dwid = X16,
  701. .rowbits = 15, .colbits = 10,
  702. .bits = {
  703. C(2), C(3), C(4), C(5), C(6), C(7), B(0), B(1), B(2),
  704. R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8),
  705. R(9), R(10), C(8), C(9), R(11), RS, R(12), R(13), R(14),
  706. 0, 0, 0, 0
  707. }
  708. },
  709. {
  710. .addrdec = AMAP_2KB, .dden = DEN_4Gb, .dwid = X8,
  711. .rowbits = 16, .colbits = 10,
  712. .bits = {
  713. C(2), C(3), C(4), C(5), C(6), C(7), B(0), B(1), B(2),
  714. R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8),
  715. R(9), R(10), C(8), C(9), R(11), RS, R(12), R(13), R(14),
  716. R(15), 0, 0, 0
  717. }
  718. },
  719. {
  720. .addrdec = AMAP_2KB, .dden = DEN_8Gb, .dwid = X16,
  721. .rowbits = 16, .colbits = 10,
  722. .bits = {
  723. C(2), C(3), C(4), C(5), C(6), C(7), B(0), B(1), B(2),
  724. R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8),
  725. R(9), R(10), C(8), C(9), R(11), RS, R(12), R(13), R(14),
  726. R(15), 0, 0, 0
  727. }
  728. },
  729. {
  730. .addrdec = AMAP_2KB, .dden = DEN_8Gb, .dwid = X8,
  731. .rowbits = 16, .colbits = 11,
  732. .bits = {
  733. C(2), C(3), C(4), C(5), C(6), C(7), B(0), B(1), B(2),
  734. R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7), R(8),
  735. R(9), R(10), C(8), C(9), R(11), RS, C(11), R(12), R(13),
  736. R(14), R(15), 0, 0
  737. }
  738. },
  739. {
  740. .addrdec = AMAP_4KB, .dden = DEN_4Gb, .dwid = X16,
  741. .rowbits = 15, .colbits = 10,
  742. .bits = {
  743. C(2), C(3), C(4), C(5), C(6), C(7), C(8), B(0), B(1),
  744. B(2), R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
  745. R(8), R(9), R(10), C(9), R(11), RS, R(12), R(13), R(14),
  746. 0, 0, 0, 0
  747. }
  748. },
  749. {
  750. .addrdec = AMAP_4KB, .dden = DEN_4Gb, .dwid = X8,
  751. .rowbits = 16, .colbits = 10,
  752. .bits = {
  753. C(2), C(3), C(4), C(5), C(6), C(7), C(8), B(0), B(1),
  754. B(2), R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
  755. R(8), R(9), R(10), C(9), R(11), RS, R(12), R(13), R(14),
  756. R(15), 0, 0, 0
  757. }
  758. },
  759. {
  760. .addrdec = AMAP_4KB, .dden = DEN_8Gb, .dwid = X16,
  761. .rowbits = 16, .colbits = 10,
  762. .bits = {
  763. C(2), C(3), C(4), C(5), C(6), C(7), C(8), B(0), B(1),
  764. B(2), R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
  765. R(8), R(9), R(10), C(9), R(11), RS, R(12), R(13), R(14),
  766. R(15), 0, 0, 0
  767. }
  768. },
  769. {
  770. .addrdec = AMAP_4KB, .dden = DEN_8Gb, .dwid = X8,
  771. .rowbits = 16, .colbits = 11,
  772. .bits = {
  773. C(2), C(3), C(4), C(5), C(6), C(7), C(8), B(0), B(1),
  774. B(2), R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
  775. R(8), R(9), R(10), C(9), R(11), RS, C(11), R(12), R(13),
  776. R(14), R(15), 0, 0
  777. }
  778. }
  779. };
  780. static int bank_hash(u64 pmiaddr, int idx, int shft)
  781. {
  782. int bhash = 0;
  783. switch (idx) {
  784. case 0:
  785. bhash ^= ((pmiaddr >> (12 + shft)) ^ (pmiaddr >> (9 + shft))) & 1;
  786. break;
  787. case 1:
  788. bhash ^= (((pmiaddr >> (10 + shft)) ^ (pmiaddr >> (8 + shft))) & 1) << 1;
  789. bhash ^= ((pmiaddr >> 22) & 1) << 1;
  790. break;
  791. case 2:
  792. bhash ^= (((pmiaddr >> (13 + shft)) ^ (pmiaddr >> (11 + shft))) & 1) << 2;
  793. break;
  794. }
  795. return bhash;
  796. }
  797. static int rank_hash(u64 pmiaddr)
  798. {
  799. return ((pmiaddr >> 16) ^ (pmiaddr >> 10)) & 1;
  800. }
  801. /* Second stage decode. Compute rank, bank, row & column. */
  802. static int apl_pmi2mem(struct mem_ctl_info *mci, u64 pmiaddr, u32 pmiidx,
  803. struct dram_addr *daddr, char *msg)
  804. {
  805. struct d_cr_drp0 *cr_drp0 = &drp0[pmiidx];
  806. struct pnd2_pvt *pvt = mci->pvt_info;
  807. int g = pvt->dimm_geom[pmiidx];
  808. struct dimm_geometry *d = &dimms[g];
  809. int column = 0, bank = 0, row = 0, rank = 0;
  810. int i, idx, type, skiprs = 0;
  811. for (i = 0; i < PMI_ADDRESS_WIDTH; i++) {
  812. int bit = (pmiaddr >> i) & 1;
  813. if (i + skiprs >= PMI_ADDRESS_WIDTH) {
  814. snprintf(msg, PND2_MSG_SIZE, "Bad dimm_geometry[] table\n");
  815. return -EINVAL;
  816. }
  817. type = d->bits[i + skiprs] & ~0xf;
  818. idx = d->bits[i + skiprs] & 0xf;
  819. /*
  820. * On single rank DIMMs ignore the rank select bit
  821. * and shift remainder of "bits[]" down one place.
  822. */
  823. if (type == RS && (cr_drp0->rken0 + cr_drp0->rken1) == 1) {
  824. skiprs = 1;
  825. type = d->bits[i + skiprs] & ~0xf;
  826. idx = d->bits[i + skiprs] & 0xf;
  827. }
  828. switch (type) {
  829. case C(0):
  830. column |= (bit << idx);
  831. break;
  832. case B(0):
  833. bank |= (bit << idx);
  834. if (cr_drp0->bahen)
  835. bank ^= bank_hash(pmiaddr, idx, d->addrdec);
  836. break;
  837. case R(0):
  838. row |= (bit << idx);
  839. break;
  840. case RS:
  841. rank = bit;
  842. if (cr_drp0->rsien)
  843. rank ^= rank_hash(pmiaddr);
  844. break;
  845. default:
  846. if (bit) {
  847. snprintf(msg, PND2_MSG_SIZE, "Bad translation\n");
  848. return -EINVAL;
  849. }
  850. goto done;
  851. }
  852. }
  853. done:
  854. daddr->col = column;
  855. daddr->bank = bank;
  856. daddr->row = row;
  857. daddr->rank = rank;
  858. daddr->dimm = 0;
  859. return 0;
  860. }
  861. /* Pluck bit "in" from pmiaddr and return value shifted to bit "out" */
  862. #define dnv_get_bit(pmi, in, out) ((int)(((pmi) >> (in)) & 1u) << (out))
  863. static int dnv_pmi2mem(struct mem_ctl_info *mci, u64 pmiaddr, u32 pmiidx,
  864. struct dram_addr *daddr, char *msg)
  865. {
  866. /* Rank 0 or 1 */
  867. daddr->rank = dnv_get_bit(pmiaddr, dmap[pmiidx].rs0 + 13, 0);
  868. /* Rank 2 or 3 */
  869. daddr->rank |= dnv_get_bit(pmiaddr, dmap[pmiidx].rs1 + 13, 1);
  870. /*
  871. * Normally ranks 0,1 are DIMM0, and 2,3 are DIMM1, but we
  872. * flip them if DIMM1 is larger than DIMM0.
  873. */
  874. daddr->dimm = (daddr->rank >= 2) ^ drp[pmiidx].dimmflip;
  875. daddr->bank = dnv_get_bit(pmiaddr, dmap[pmiidx].ba0 + 6, 0);
  876. daddr->bank |= dnv_get_bit(pmiaddr, dmap[pmiidx].ba1 + 6, 1);
  877. daddr->bank |= dnv_get_bit(pmiaddr, dmap[pmiidx].bg0 + 6, 2);
  878. if (dsch.ddr4en)
  879. daddr->bank |= dnv_get_bit(pmiaddr, dmap[pmiidx].bg1 + 6, 3);
  880. if (dmap1[pmiidx].bxor) {
  881. if (dsch.ddr4en) {
  882. daddr->bank ^= dnv_get_bit(pmiaddr, dmap3[pmiidx].row6 + 6, 0);
  883. daddr->bank ^= dnv_get_bit(pmiaddr, dmap3[pmiidx].row7 + 6, 1);
  884. if (dsch.chan_width == 0)
  885. /* 64/72 bit dram channel width */
  886. daddr->bank ^= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca3 + 6, 2);
  887. else
  888. /* 32/40 bit dram channel width */
  889. daddr->bank ^= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca4 + 6, 2);
  890. daddr->bank ^= dnv_get_bit(pmiaddr, dmap2[pmiidx].row2 + 6, 3);
  891. } else {
  892. daddr->bank ^= dnv_get_bit(pmiaddr, dmap2[pmiidx].row2 + 6, 0);
  893. daddr->bank ^= dnv_get_bit(pmiaddr, dmap3[pmiidx].row6 + 6, 1);
  894. if (dsch.chan_width == 0)
  895. daddr->bank ^= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca3 + 6, 2);
  896. else
  897. daddr->bank ^= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca4 + 6, 2);
  898. }
  899. }
  900. daddr->row = dnv_get_bit(pmiaddr, dmap2[pmiidx].row0 + 6, 0);
  901. daddr->row |= dnv_get_bit(pmiaddr, dmap2[pmiidx].row1 + 6, 1);
  902. daddr->row |= dnv_get_bit(pmiaddr, dmap2[pmiidx].row2 + 6, 2);
  903. daddr->row |= dnv_get_bit(pmiaddr, dmap2[pmiidx].row3 + 6, 3);
  904. daddr->row |= dnv_get_bit(pmiaddr, dmap2[pmiidx].row4 + 6, 4);
  905. daddr->row |= dnv_get_bit(pmiaddr, dmap2[pmiidx].row5 + 6, 5);
  906. daddr->row |= dnv_get_bit(pmiaddr, dmap3[pmiidx].row6 + 6, 6);
  907. daddr->row |= dnv_get_bit(pmiaddr, dmap3[pmiidx].row7 + 6, 7);
  908. daddr->row |= dnv_get_bit(pmiaddr, dmap3[pmiidx].row8 + 6, 8);
  909. daddr->row |= dnv_get_bit(pmiaddr, dmap3[pmiidx].row9 + 6, 9);
  910. daddr->row |= dnv_get_bit(pmiaddr, dmap3[pmiidx].row10 + 6, 10);
  911. daddr->row |= dnv_get_bit(pmiaddr, dmap3[pmiidx].row11 + 6, 11);
  912. daddr->row |= dnv_get_bit(pmiaddr, dmap4[pmiidx].row12 + 6, 12);
  913. daddr->row |= dnv_get_bit(pmiaddr, dmap4[pmiidx].row13 + 6, 13);
  914. if (dmap4[pmiidx].row14 != 31)
  915. daddr->row |= dnv_get_bit(pmiaddr, dmap4[pmiidx].row14 + 6, 14);
  916. if (dmap4[pmiidx].row15 != 31)
  917. daddr->row |= dnv_get_bit(pmiaddr, dmap4[pmiidx].row15 + 6, 15);
  918. if (dmap4[pmiidx].row16 != 31)
  919. daddr->row |= dnv_get_bit(pmiaddr, dmap4[pmiidx].row16 + 6, 16);
  920. if (dmap4[pmiidx].row17 != 31)
  921. daddr->row |= dnv_get_bit(pmiaddr, dmap4[pmiidx].row17 + 6, 17);
  922. daddr->col = dnv_get_bit(pmiaddr, dmap5[pmiidx].ca3 + 6, 3);
  923. daddr->col |= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca4 + 6, 4);
  924. daddr->col |= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca5 + 6, 5);
  925. daddr->col |= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca6 + 6, 6);
  926. daddr->col |= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca7 + 6, 7);
  927. daddr->col |= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca8 + 6, 8);
  928. daddr->col |= dnv_get_bit(pmiaddr, dmap5[pmiidx].ca9 + 6, 9);
  929. if (!dsch.ddr4en && dmap1[pmiidx].ca11 != 0x3f)
  930. daddr->col |= dnv_get_bit(pmiaddr, dmap1[pmiidx].ca11 + 13, 11);
  931. return 0;
  932. }
  933. static int check_channel(int ch)
  934. {
  935. if (drp0[ch].dramtype != 0) {
  936. pnd2_printk(KERN_INFO, "Unsupported DIMM in channel %d\n", ch);
  937. return 1;
  938. } else if (drp0[ch].eccen == 0) {
  939. pnd2_printk(KERN_INFO, "ECC disabled on channel %d\n", ch);
  940. return 1;
  941. }
  942. return 0;
  943. }
  944. static int apl_check_ecc_active(void)
  945. {
  946. int i, ret = 0;
  947. /* Check dramtype and ECC mode for each present DIMM */
  948. for (i = 0; i < APL_NUM_CHANNELS; i++)
  949. if (chan_mask & BIT(i))
  950. ret += check_channel(i);
  951. return ret ? -EINVAL : 0;
  952. }
  953. #define DIMMS_PRESENT(d) ((d)->rken0 + (d)->rken1 + (d)->rken2 + (d)->rken3)
  954. static int check_unit(int ch)
  955. {
  956. struct d_cr_drp *d = &drp[ch];
  957. if (DIMMS_PRESENT(d) && !ecc_ctrl[ch].eccen) {
  958. pnd2_printk(KERN_INFO, "ECC disabled on channel %d\n", ch);
  959. return 1;
  960. }
  961. return 0;
  962. }
  963. static int dnv_check_ecc_active(void)
  964. {
  965. int i, ret = 0;
  966. for (i = 0; i < DNV_NUM_CHANNELS; i++)
  967. ret += check_unit(i);
  968. return ret ? -EINVAL : 0;
  969. }
  970. static int get_memory_error_data(struct mem_ctl_info *mci, u64 addr,
  971. struct dram_addr *daddr, char *msg)
  972. {
  973. u64 pmiaddr;
  974. u32 pmiidx;
  975. int ret;
  976. ret = sys2pmi(addr, &pmiidx, &pmiaddr, msg);
  977. if (ret)
  978. return ret;
  979. pmiaddr >>= ops->pmiaddr_shift;
  980. /* pmi channel idx to dimm channel idx */
  981. pmiidx >>= ops->pmiidx_shift;
  982. daddr->chan = pmiidx;
  983. ret = ops->pmi2mem(mci, pmiaddr, pmiidx, daddr, msg);
  984. if (ret)
  985. return ret;
  986. edac_dbg(0, "SysAddr=%llx PmiAddr=%llx Channel=%d DIMM=%d Rank=%d Bank=%d Row=%d Column=%d\n",
  987. addr, pmiaddr, daddr->chan, daddr->dimm, daddr->rank, daddr->bank, daddr->row, daddr->col);
  988. return 0;
  989. }
  990. static void pnd2_mce_output_error(struct mem_ctl_info *mci, const struct mce *m,
  991. struct dram_addr *daddr)
  992. {
  993. enum hw_event_mc_err_type tp_event;
  994. char *optype, msg[PND2_MSG_SIZE];
  995. bool ripv = m->mcgstatus & MCG_STATUS_RIPV;
  996. bool overflow = m->status & MCI_STATUS_OVER;
  997. bool uc_err = m->status & MCI_STATUS_UC;
  998. bool recov = m->status & MCI_STATUS_S;
  999. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  1000. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  1001. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  1002. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  1003. int rc;
  1004. tp_event = uc_err ? (ripv ? HW_EVENT_ERR_FATAL : HW_EVENT_ERR_UNCORRECTED) :
  1005. HW_EVENT_ERR_CORRECTED;
  1006. /*
  1007. * According with Table 15-9 of the Intel Architecture spec vol 3A,
  1008. * memory errors should fit in this mask:
  1009. * 000f 0000 1mmm cccc (binary)
  1010. * where:
  1011. * f = Correction Report Filtering Bit. If 1, subsequent errors
  1012. * won't be shown
  1013. * mmm = error type
  1014. * cccc = channel
  1015. * If the mask doesn't match, report an error to the parsing logic
  1016. */
  1017. if (!((errcode & 0xef80) == 0x80)) {
  1018. optype = "Can't parse: it is not a mem";
  1019. } else {
  1020. switch (optypenum) {
  1021. case 0:
  1022. optype = "generic undef request error";
  1023. break;
  1024. case 1:
  1025. optype = "memory read error";
  1026. break;
  1027. case 2:
  1028. optype = "memory write error";
  1029. break;
  1030. case 3:
  1031. optype = "addr/cmd error";
  1032. break;
  1033. case 4:
  1034. optype = "memory scrubbing error";
  1035. break;
  1036. default:
  1037. optype = "reserved";
  1038. break;
  1039. }
  1040. }
  1041. /* Only decode errors with an valid address (ADDRV) */
  1042. if (!(m->status & MCI_STATUS_ADDRV))
  1043. return;
  1044. rc = get_memory_error_data(mci, m->addr, daddr, msg);
  1045. if (rc)
  1046. goto address_error;
  1047. snprintf(msg, sizeof(msg),
  1048. "%s%s err_code:%04x:%04x channel:%d DIMM:%d rank:%d row:%d bank:%d col:%d",
  1049. overflow ? " OVERFLOW" : "", (uc_err && recov) ? " recoverable" : "", mscod,
  1050. errcode, daddr->chan, daddr->dimm, daddr->rank, daddr->row, daddr->bank, daddr->col);
  1051. edac_dbg(0, "%s\n", msg);
  1052. /* Call the helper to output message */
  1053. edac_mc_handle_error(tp_event, mci, core_err_cnt, m->addr >> PAGE_SHIFT,
  1054. m->addr & ~PAGE_MASK, 0, daddr->chan, daddr->dimm, -1, optype, msg);
  1055. return;
  1056. address_error:
  1057. edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0, -1, -1, -1, msg, "");
  1058. }
  1059. static void apl_get_dimm_config(struct mem_ctl_info *mci)
  1060. {
  1061. struct pnd2_pvt *pvt = mci->pvt_info;
  1062. struct dimm_info *dimm;
  1063. struct d_cr_drp0 *d;
  1064. u64 capacity;
  1065. int i, g;
  1066. for (i = 0; i < APL_NUM_CHANNELS; i++) {
  1067. if (!(chan_mask & BIT(i)))
  1068. continue;
  1069. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers, i, 0, 0);
  1070. if (!dimm) {
  1071. edac_dbg(0, "No allocated DIMM for channel %d\n", i);
  1072. continue;
  1073. }
  1074. d = &drp0[i];
  1075. for (g = 0; g < ARRAY_SIZE(dimms); g++)
  1076. if (dimms[g].addrdec == d->addrdec &&
  1077. dimms[g].dden == d->dden &&
  1078. dimms[g].dwid == d->dwid)
  1079. break;
  1080. if (g == ARRAY_SIZE(dimms)) {
  1081. edac_dbg(0, "Channel %d: unrecognized DIMM\n", i);
  1082. continue;
  1083. }
  1084. pvt->dimm_geom[i] = g;
  1085. capacity = (d->rken0 + d->rken1) * 8 * (1ul << dimms[g].rowbits) *
  1086. (1ul << dimms[g].colbits);
  1087. edac_dbg(0, "Channel %d: %lld MByte DIMM\n", i, capacity >> (20 - 3));
  1088. dimm->nr_pages = MiB_TO_PAGES(capacity >> (20 - 3));
  1089. dimm->grain = 32;
  1090. dimm->dtype = (d->dwid == 0) ? DEV_X8 : DEV_X16;
  1091. dimm->mtype = MEM_DDR3;
  1092. dimm->edac_mode = EDAC_SECDED;
  1093. snprintf(dimm->label, sizeof(dimm->label), "Slice#%d_Chan#%d", i / 2, i % 2);
  1094. }
  1095. }
  1096. static const int dnv_dtypes[] = {
  1097. DEV_X8, DEV_X4, DEV_X16, DEV_UNKNOWN
  1098. };
  1099. static void dnv_get_dimm_config(struct mem_ctl_info *mci)
  1100. {
  1101. int i, j, ranks_of_dimm[DNV_MAX_DIMMS], banks, rowbits, colbits, memtype;
  1102. struct dimm_info *dimm;
  1103. struct d_cr_drp *d;
  1104. u64 capacity;
  1105. if (dsch.ddr4en) {
  1106. memtype = MEM_DDR4;
  1107. banks = 16;
  1108. colbits = 10;
  1109. } else {
  1110. memtype = MEM_DDR3;
  1111. banks = 8;
  1112. }
  1113. for (i = 0; i < DNV_NUM_CHANNELS; i++) {
  1114. if (dmap4[i].row14 == 31)
  1115. rowbits = 14;
  1116. else if (dmap4[i].row15 == 31)
  1117. rowbits = 15;
  1118. else if (dmap4[i].row16 == 31)
  1119. rowbits = 16;
  1120. else if (dmap4[i].row17 == 31)
  1121. rowbits = 17;
  1122. else
  1123. rowbits = 18;
  1124. if (memtype == MEM_DDR3) {
  1125. if (dmap1[i].ca11 != 0x3f)
  1126. colbits = 12;
  1127. else
  1128. colbits = 10;
  1129. }
  1130. d = &drp[i];
  1131. /* DIMM0 is present if rank0 and/or rank1 is enabled */
  1132. ranks_of_dimm[0] = d->rken0 + d->rken1;
  1133. /* DIMM1 is present if rank2 and/or rank3 is enabled */
  1134. ranks_of_dimm[1] = d->rken2 + d->rken3;
  1135. for (j = 0; j < DNV_MAX_DIMMS; j++) {
  1136. if (!ranks_of_dimm[j])
  1137. continue;
  1138. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers, i, j, 0);
  1139. if (!dimm) {
  1140. edac_dbg(0, "No allocated DIMM for channel %d DIMM %d\n", i, j);
  1141. continue;
  1142. }
  1143. capacity = ranks_of_dimm[j] * banks * (1ul << rowbits) * (1ul << colbits);
  1144. edac_dbg(0, "Channel %d DIMM %d: %lld MByte DIMM\n", i, j, capacity >> (20 - 3));
  1145. dimm->nr_pages = MiB_TO_PAGES(capacity >> (20 - 3));
  1146. dimm->grain = 32;
  1147. dimm->dtype = dnv_dtypes[j ? d->dimmdwid0 : d->dimmdwid1];
  1148. dimm->mtype = memtype;
  1149. dimm->edac_mode = EDAC_SECDED;
  1150. snprintf(dimm->label, sizeof(dimm->label), "Chan#%d_DIMM#%d", i, j);
  1151. }
  1152. }
  1153. }
  1154. static int pnd2_register_mci(struct mem_ctl_info **ppmci)
  1155. {
  1156. struct edac_mc_layer layers[2];
  1157. struct mem_ctl_info *mci;
  1158. struct pnd2_pvt *pvt;
  1159. int rc;
  1160. rc = ops->check_ecc();
  1161. if (rc < 0)
  1162. return rc;
  1163. /* Allocate a new MC control structure */
  1164. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  1165. layers[0].size = ops->channels;
  1166. layers[0].is_virt_csrow = false;
  1167. layers[1].type = EDAC_MC_LAYER_SLOT;
  1168. layers[1].size = ops->dimms_per_channel;
  1169. layers[1].is_virt_csrow = true;
  1170. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, sizeof(*pvt));
  1171. if (!mci)
  1172. return -ENOMEM;
  1173. pvt = mci->pvt_info;
  1174. memset(pvt, 0, sizeof(*pvt));
  1175. mci->mod_name = "pnd2_edac.c";
  1176. mci->dev_name = ops->name;
  1177. mci->ctl_name = "Pondicherry2";
  1178. /* Get dimm basic config and the memory layout */
  1179. ops->get_dimm_config(mci);
  1180. if (edac_mc_add_mc(mci)) {
  1181. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  1182. edac_mc_free(mci);
  1183. return -EINVAL;
  1184. }
  1185. *ppmci = mci;
  1186. return 0;
  1187. }
  1188. static void pnd2_unregister_mci(struct mem_ctl_info *mci)
  1189. {
  1190. if (unlikely(!mci || !mci->pvt_info)) {
  1191. pnd2_printk(KERN_ERR, "Couldn't find mci handler\n");
  1192. return;
  1193. }
  1194. /* Remove MC sysfs nodes */
  1195. edac_mc_del_mc(NULL);
  1196. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  1197. edac_mc_free(mci);
  1198. }
  1199. /*
  1200. * Callback function registered with core kernel mce code.
  1201. * Called once for each logged error.
  1202. */
  1203. static int pnd2_mce_check_error(struct notifier_block *nb, unsigned long val, void *data)
  1204. {
  1205. struct mce *mce = (struct mce *)data;
  1206. struct mem_ctl_info *mci;
  1207. struct dram_addr daddr;
  1208. char *type;
  1209. if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
  1210. return NOTIFY_DONE;
  1211. mci = pnd2_mci;
  1212. if (!mci)
  1213. return NOTIFY_DONE;
  1214. /*
  1215. * Just let mcelog handle it if the error is
  1216. * outside the memory controller. A memory error
  1217. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  1218. * bit 12 has an special meaning.
  1219. */
  1220. if ((mce->status & 0xefff) >> 7 != 1)
  1221. return NOTIFY_DONE;
  1222. if (mce->mcgstatus & MCG_STATUS_MCIP)
  1223. type = "Exception";
  1224. else
  1225. type = "Event";
  1226. pnd2_mc_printk(mci, KERN_INFO, "HANDLING MCE MEMORY ERROR\n");
  1227. pnd2_mc_printk(mci, KERN_INFO, "CPU %u: Machine Check %s: %llx Bank %u: %llx\n",
  1228. mce->extcpu, type, mce->mcgstatus, mce->bank, mce->status);
  1229. pnd2_mc_printk(mci, KERN_INFO, "TSC %llx ", mce->tsc);
  1230. pnd2_mc_printk(mci, KERN_INFO, "ADDR %llx ", mce->addr);
  1231. pnd2_mc_printk(mci, KERN_INFO, "MISC %llx ", mce->misc);
  1232. pnd2_mc_printk(mci, KERN_INFO, "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  1233. mce->cpuvendor, mce->cpuid, mce->time, mce->socketid, mce->apicid);
  1234. pnd2_mce_output_error(mci, mce, &daddr);
  1235. /* Advice mcelog that the error were handled */
  1236. return NOTIFY_STOP;
  1237. }
  1238. static struct notifier_block pnd2_mce_dec = {
  1239. .notifier_call = pnd2_mce_check_error,
  1240. };
  1241. #ifdef CONFIG_EDAC_DEBUG
  1242. /*
  1243. * Write an address to this file to exercise the address decode
  1244. * logic in this driver.
  1245. */
  1246. static u64 pnd2_fake_addr;
  1247. #define PND2_BLOB_SIZE 1024
  1248. static char pnd2_result[PND2_BLOB_SIZE];
  1249. static struct dentry *pnd2_test;
  1250. static struct debugfs_blob_wrapper pnd2_blob = {
  1251. .data = pnd2_result,
  1252. .size = 0
  1253. };
  1254. static int debugfs_u64_set(void *data, u64 val)
  1255. {
  1256. struct dram_addr daddr;
  1257. struct mce m;
  1258. *(u64 *)data = val;
  1259. m.mcgstatus = 0;
  1260. /* ADDRV + MemRd + Unknown channel */
  1261. m.status = MCI_STATUS_ADDRV + 0x9f;
  1262. m.addr = val;
  1263. pnd2_mce_output_error(pnd2_mci, &m, &daddr);
  1264. snprintf(pnd2_blob.data, PND2_BLOB_SIZE,
  1265. "SysAddr=%llx Channel=%d DIMM=%d Rank=%d Bank=%d Row=%d Column=%d\n",
  1266. m.addr, daddr.chan, daddr.dimm, daddr.rank, daddr.bank, daddr.row, daddr.col);
  1267. pnd2_blob.size = strlen(pnd2_blob.data);
  1268. return 0;
  1269. }
  1270. DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  1271. static void setup_pnd2_debug(void)
  1272. {
  1273. pnd2_test = edac_debugfs_create_dir("pnd2_test");
  1274. edac_debugfs_create_file("pnd2_debug_addr", 0200, pnd2_test,
  1275. &pnd2_fake_addr, &fops_u64_wo);
  1276. debugfs_create_blob("pnd2_debug_results", 0400, pnd2_test, &pnd2_blob);
  1277. }
  1278. static void teardown_pnd2_debug(void)
  1279. {
  1280. debugfs_remove_recursive(pnd2_test);
  1281. }
  1282. #else
  1283. static void setup_pnd2_debug(void) {}
  1284. static void teardown_pnd2_debug(void) {}
  1285. #endif /* CONFIG_EDAC_DEBUG */
  1286. static int pnd2_probe(void)
  1287. {
  1288. int rc;
  1289. edac_dbg(2, "\n");
  1290. rc = get_registers();
  1291. if (rc)
  1292. return rc;
  1293. return pnd2_register_mci(&pnd2_mci);
  1294. }
  1295. static void pnd2_remove(void)
  1296. {
  1297. edac_dbg(0, "\n");
  1298. pnd2_unregister_mci(pnd2_mci);
  1299. }
  1300. static struct dunit_ops apl_ops = {
  1301. .name = "pnd2/apl",
  1302. .type = APL,
  1303. .pmiaddr_shift = LOG2_PMI_ADDR_GRANULARITY,
  1304. .pmiidx_shift = 0,
  1305. .channels = APL_NUM_CHANNELS,
  1306. .dimms_per_channel = 1,
  1307. .rd_reg = apl_rd_reg,
  1308. .get_registers = apl_get_registers,
  1309. .check_ecc = apl_check_ecc_active,
  1310. .mk_region = apl_mk_region,
  1311. .get_dimm_config = apl_get_dimm_config,
  1312. .pmi2mem = apl_pmi2mem,
  1313. };
  1314. static struct dunit_ops dnv_ops = {
  1315. .name = "pnd2/dnv",
  1316. .type = DNV,
  1317. .pmiaddr_shift = 0,
  1318. .pmiidx_shift = 1,
  1319. .channels = DNV_NUM_CHANNELS,
  1320. .dimms_per_channel = 2,
  1321. .rd_reg = dnv_rd_reg,
  1322. .get_registers = dnv_get_registers,
  1323. .check_ecc = dnv_check_ecc_active,
  1324. .mk_region = dnv_mk_region,
  1325. .get_dimm_config = dnv_get_dimm_config,
  1326. .pmi2mem = dnv_pmi2mem,
  1327. };
  1328. static const struct x86_cpu_id pnd2_cpuids[] = {
  1329. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_GOLDMONT, 0, (kernel_ulong_t)&apl_ops },
  1330. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_GOLDMONT_X, 0, (kernel_ulong_t)&dnv_ops },
  1331. { }
  1332. };
  1333. MODULE_DEVICE_TABLE(x86cpu, pnd2_cpuids);
  1334. static int __init pnd2_init(void)
  1335. {
  1336. const struct x86_cpu_id *id;
  1337. int rc;
  1338. edac_dbg(2, "\n");
  1339. id = x86_match_cpu(pnd2_cpuids);
  1340. if (!id)
  1341. return -ENODEV;
  1342. ops = (struct dunit_ops *)id->driver_data;
  1343. if (ops->type == APL) {
  1344. p2sb_bus = pci_find_bus(0, 0);
  1345. if (!p2sb_bus)
  1346. return -ENODEV;
  1347. }
  1348. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1349. opstate_init();
  1350. rc = pnd2_probe();
  1351. if (rc < 0) {
  1352. pnd2_printk(KERN_ERR, "Failed to register device with error %d.\n", rc);
  1353. return rc;
  1354. }
  1355. if (!pnd2_mci)
  1356. return -ENODEV;
  1357. mce_register_decode_chain(&pnd2_mce_dec);
  1358. setup_pnd2_debug();
  1359. return 0;
  1360. }
  1361. static void __exit pnd2_exit(void)
  1362. {
  1363. edac_dbg(2, "\n");
  1364. teardown_pnd2_debug();
  1365. mce_unregister_decode_chain(&pnd2_mce_dec);
  1366. pnd2_remove();
  1367. }
  1368. module_init(pnd2_init);
  1369. module_exit(pnd2_exit);
  1370. module_param(edac_op_state, int, 0444);
  1371. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  1372. MODULE_LICENSE("GPL v2");
  1373. MODULE_AUTHOR("Tony Luck");
  1374. MODULE_DESCRIPTION("MC Driver for Intel SoC using Pondicherry memory controller");