edac_mc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * edac_mc kernel module
  3. * (C) 2005, 2006 Linux Networx (http://lnxi.com)
  4. * This file may be distributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * Written by Thayne Harbaugh
  8. * Based on work by Dan Hollis <goemon at anime dot net> and others.
  9. * http://www.anime.net/~goemon/linux-ecc/
  10. *
  11. * Modified by Dave Peterson and Doug Thompson
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/smp.h>
  19. #include <linux/init.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/highmem.h>
  22. #include <linux/timer.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/list.h>
  27. #include <linux/ctype.h>
  28. #include <linux/edac.h>
  29. #include <linux/bitops.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/page.h>
  32. #include "edac_mc.h"
  33. #include "edac_module.h"
  34. #include <ras/ras_event.h>
  35. #ifdef CONFIG_EDAC_ATOMIC_SCRUB
  36. #include <asm/edac.h>
  37. #else
  38. #define edac_atomic_scrub(va, size) do { } while (0)
  39. #endif
  40. int edac_op_state = EDAC_OPSTATE_INVAL;
  41. EXPORT_SYMBOL_GPL(edac_op_state);
  42. static int edac_report = EDAC_REPORTING_ENABLED;
  43. /* lock to memory controller's control array */
  44. static DEFINE_MUTEX(mem_ctls_mutex);
  45. static LIST_HEAD(mc_devices);
  46. /*
  47. * Used to lock EDAC MC to just one module, avoiding two drivers e. g.
  48. * apei/ghes and i7core_edac to be used at the same time.
  49. */
  50. static const char *edac_mc_owner;
  51. static struct bus_type mc_bus[EDAC_MAX_MCS];
  52. int edac_get_report_status(void)
  53. {
  54. return edac_report;
  55. }
  56. EXPORT_SYMBOL_GPL(edac_get_report_status);
  57. void edac_set_report_status(int new)
  58. {
  59. if (new == EDAC_REPORTING_ENABLED ||
  60. new == EDAC_REPORTING_DISABLED ||
  61. new == EDAC_REPORTING_FORCE)
  62. edac_report = new;
  63. }
  64. EXPORT_SYMBOL_GPL(edac_set_report_status);
  65. static int edac_report_set(const char *str, const struct kernel_param *kp)
  66. {
  67. if (!str)
  68. return -EINVAL;
  69. if (!strncmp(str, "on", 2))
  70. edac_report = EDAC_REPORTING_ENABLED;
  71. else if (!strncmp(str, "off", 3))
  72. edac_report = EDAC_REPORTING_DISABLED;
  73. else if (!strncmp(str, "force", 5))
  74. edac_report = EDAC_REPORTING_FORCE;
  75. return 0;
  76. }
  77. static int edac_report_get(char *buffer, const struct kernel_param *kp)
  78. {
  79. int ret = 0;
  80. switch (edac_report) {
  81. case EDAC_REPORTING_ENABLED:
  82. ret = sprintf(buffer, "on");
  83. break;
  84. case EDAC_REPORTING_DISABLED:
  85. ret = sprintf(buffer, "off");
  86. break;
  87. case EDAC_REPORTING_FORCE:
  88. ret = sprintf(buffer, "force");
  89. break;
  90. default:
  91. ret = -EINVAL;
  92. break;
  93. }
  94. return ret;
  95. }
  96. static const struct kernel_param_ops edac_report_ops = {
  97. .set = edac_report_set,
  98. .get = edac_report_get,
  99. };
  100. module_param_cb(edac_report, &edac_report_ops, &edac_report, 0644);
  101. unsigned edac_dimm_info_location(struct dimm_info *dimm, char *buf,
  102. unsigned len)
  103. {
  104. struct mem_ctl_info *mci = dimm->mci;
  105. int i, n, count = 0;
  106. char *p = buf;
  107. for (i = 0; i < mci->n_layers; i++) {
  108. n = snprintf(p, len, "%s %d ",
  109. edac_layer_name[mci->layers[i].type],
  110. dimm->location[i]);
  111. p += n;
  112. len -= n;
  113. count += n;
  114. if (!len)
  115. break;
  116. }
  117. return count;
  118. }
  119. #ifdef CONFIG_EDAC_DEBUG
  120. static void edac_mc_dump_channel(struct rank_info *chan)
  121. {
  122. edac_dbg(4, " channel->chan_idx = %d\n", chan->chan_idx);
  123. edac_dbg(4, " channel = %p\n", chan);
  124. edac_dbg(4, " channel->csrow = %p\n", chan->csrow);
  125. edac_dbg(4, " channel->dimm = %p\n", chan->dimm);
  126. }
  127. static void edac_mc_dump_dimm(struct dimm_info *dimm, int number)
  128. {
  129. char location[80];
  130. edac_dimm_info_location(dimm, location, sizeof(location));
  131. edac_dbg(4, "%s%i: %smapped as virtual row %d, chan %d\n",
  132. dimm->mci->csbased ? "rank" : "dimm",
  133. number, location, dimm->csrow, dimm->cschannel);
  134. edac_dbg(4, " dimm = %p\n", dimm);
  135. edac_dbg(4, " dimm->label = '%s'\n", dimm->label);
  136. edac_dbg(4, " dimm->nr_pages = 0x%x\n", dimm->nr_pages);
  137. edac_dbg(4, " dimm->grain = %d\n", dimm->grain);
  138. edac_dbg(4, " dimm->nr_pages = 0x%x\n", dimm->nr_pages);
  139. }
  140. static void edac_mc_dump_csrow(struct csrow_info *csrow)
  141. {
  142. edac_dbg(4, "csrow->csrow_idx = %d\n", csrow->csrow_idx);
  143. edac_dbg(4, " csrow = %p\n", csrow);
  144. edac_dbg(4, " csrow->first_page = 0x%lx\n", csrow->first_page);
  145. edac_dbg(4, " csrow->last_page = 0x%lx\n", csrow->last_page);
  146. edac_dbg(4, " csrow->page_mask = 0x%lx\n", csrow->page_mask);
  147. edac_dbg(4, " csrow->nr_channels = %d\n", csrow->nr_channels);
  148. edac_dbg(4, " csrow->channels = %p\n", csrow->channels);
  149. edac_dbg(4, " csrow->mci = %p\n", csrow->mci);
  150. }
  151. static void edac_mc_dump_mci(struct mem_ctl_info *mci)
  152. {
  153. edac_dbg(3, "\tmci = %p\n", mci);
  154. edac_dbg(3, "\tmci->mtype_cap = %lx\n", mci->mtype_cap);
  155. edac_dbg(3, "\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap);
  156. edac_dbg(3, "\tmci->edac_cap = %lx\n", mci->edac_cap);
  157. edac_dbg(4, "\tmci->edac_check = %p\n", mci->edac_check);
  158. edac_dbg(3, "\tmci->nr_csrows = %d, csrows = %p\n",
  159. mci->nr_csrows, mci->csrows);
  160. edac_dbg(3, "\tmci->nr_dimms = %d, dimms = %p\n",
  161. mci->tot_dimms, mci->dimms);
  162. edac_dbg(3, "\tdev = %p\n", mci->pdev);
  163. edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n",
  164. mci->mod_name, mci->ctl_name);
  165. edac_dbg(3, "\tpvt_info = %p\n\n", mci->pvt_info);
  166. }
  167. #endif /* CONFIG_EDAC_DEBUG */
  168. const char * const edac_mem_types[] = {
  169. [MEM_EMPTY] = "Empty",
  170. [MEM_RESERVED] = "Reserved",
  171. [MEM_UNKNOWN] = "Unknown",
  172. [MEM_FPM] = "FPM",
  173. [MEM_EDO] = "EDO",
  174. [MEM_BEDO] = "BEDO",
  175. [MEM_SDR] = "Unbuffered-SDR",
  176. [MEM_RDR] = "Registered-SDR",
  177. [MEM_DDR] = "Unbuffered-DDR",
  178. [MEM_RDDR] = "Registered-DDR",
  179. [MEM_RMBS] = "RMBS",
  180. [MEM_DDR2] = "Unbuffered-DDR2",
  181. [MEM_FB_DDR2] = "FullyBuffered-DDR2",
  182. [MEM_RDDR2] = "Registered-DDR2",
  183. [MEM_XDR] = "XDR",
  184. [MEM_DDR3] = "Unbuffered-DDR3",
  185. [MEM_RDDR3] = "Registered-DDR3",
  186. [MEM_LRDDR3] = "Load-Reduced-DDR3-RAM",
  187. [MEM_DDR4] = "Unbuffered-DDR4",
  188. [MEM_RDDR4] = "Registered-DDR4",
  189. [MEM_LRDDR4] = "Load-Reduced-DDR4-RAM",
  190. [MEM_NVDIMM] = "Non-volatile-RAM",
  191. };
  192. EXPORT_SYMBOL_GPL(edac_mem_types);
  193. /**
  194. * edac_align_ptr - Prepares the pointer offsets for a single-shot allocation
  195. * @p: pointer to a pointer with the memory offset to be used. At
  196. * return, this will be incremented to point to the next offset
  197. * @size: Size of the data structure to be reserved
  198. * @n_elems: Number of elements that should be reserved
  199. *
  200. * If 'size' is a constant, the compiler will optimize this whole function
  201. * down to either a no-op or the addition of a constant to the value of '*p'.
  202. *
  203. * The 'p' pointer is absolutely needed to keep the proper advancing
  204. * further in memory to the proper offsets when allocating the struct along
  205. * with its embedded structs, as edac_device_alloc_ctl_info() does it
  206. * above, for example.
  207. *
  208. * At return, the pointer 'p' will be incremented to be used on a next call
  209. * to this function.
  210. */
  211. void *edac_align_ptr(void **p, unsigned size, int n_elems)
  212. {
  213. unsigned align, r;
  214. void *ptr = *p;
  215. *p += size * n_elems;
  216. /*
  217. * 'p' can possibly be an unaligned item X such that sizeof(X) is
  218. * 'size'. Adjust 'p' so that its alignment is at least as
  219. * stringent as what the compiler would provide for X and return
  220. * the aligned result.
  221. * Here we assume that the alignment of a "long long" is the most
  222. * stringent alignment that the compiler will ever provide by default.
  223. * As far as I know, this is a reasonable assumption.
  224. */
  225. if (size > sizeof(long))
  226. align = sizeof(long long);
  227. else if (size > sizeof(int))
  228. align = sizeof(long);
  229. else if (size > sizeof(short))
  230. align = sizeof(int);
  231. else if (size > sizeof(char))
  232. align = sizeof(short);
  233. else
  234. return (char *)ptr;
  235. r = (unsigned long)p % align;
  236. if (r == 0)
  237. return (char *)ptr;
  238. *p += align - r;
  239. return (void *)(((unsigned long)ptr) + align - r);
  240. }
  241. static void _edac_mc_free(struct mem_ctl_info *mci)
  242. {
  243. int i, chn, row;
  244. struct csrow_info *csr;
  245. const unsigned int tot_dimms = mci->tot_dimms;
  246. const unsigned int tot_channels = mci->num_cschannel;
  247. const unsigned int tot_csrows = mci->nr_csrows;
  248. if (mci->dimms) {
  249. for (i = 0; i < tot_dimms; i++)
  250. kfree(mci->dimms[i]);
  251. kfree(mci->dimms);
  252. }
  253. if (mci->csrows) {
  254. for (row = 0; row < tot_csrows; row++) {
  255. csr = mci->csrows[row];
  256. if (csr) {
  257. if (csr->channels) {
  258. for (chn = 0; chn < tot_channels; chn++)
  259. kfree(csr->channels[chn]);
  260. kfree(csr->channels);
  261. }
  262. kfree(csr);
  263. }
  264. }
  265. kfree(mci->csrows);
  266. }
  267. kfree(mci);
  268. }
  269. struct mem_ctl_info *edac_mc_alloc(unsigned mc_num,
  270. unsigned n_layers,
  271. struct edac_mc_layer *layers,
  272. unsigned sz_pvt)
  273. {
  274. struct mem_ctl_info *mci;
  275. struct edac_mc_layer *layer;
  276. struct csrow_info *csr;
  277. struct rank_info *chan;
  278. struct dimm_info *dimm;
  279. u32 *ce_per_layer[EDAC_MAX_LAYERS], *ue_per_layer[EDAC_MAX_LAYERS];
  280. unsigned pos[EDAC_MAX_LAYERS];
  281. unsigned size, tot_dimms = 1, count = 1;
  282. unsigned tot_csrows = 1, tot_channels = 1, tot_errcount = 0;
  283. void *pvt, *p, *ptr = NULL;
  284. int i, j, row, chn, n, len, off;
  285. bool per_rank = false;
  286. BUG_ON(n_layers > EDAC_MAX_LAYERS || n_layers == 0);
  287. /*
  288. * Calculate the total amount of dimms and csrows/cschannels while
  289. * in the old API emulation mode
  290. */
  291. for (i = 0; i < n_layers; i++) {
  292. tot_dimms *= layers[i].size;
  293. if (layers[i].is_virt_csrow)
  294. tot_csrows *= layers[i].size;
  295. else
  296. tot_channels *= layers[i].size;
  297. if (layers[i].type == EDAC_MC_LAYER_CHIP_SELECT)
  298. per_rank = true;
  299. }
  300. /* Figure out the offsets of the various items from the start of an mc
  301. * structure. We want the alignment of each item to be at least as
  302. * stringent as what the compiler would provide if we could simply
  303. * hardcode everything into a single struct.
  304. */
  305. mci = edac_align_ptr(&ptr, sizeof(*mci), 1);
  306. layer = edac_align_ptr(&ptr, sizeof(*layer), n_layers);
  307. for (i = 0; i < n_layers; i++) {
  308. count *= layers[i].size;
  309. edac_dbg(4, "errcount layer %d size %d\n", i, count);
  310. ce_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count);
  311. ue_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count);
  312. tot_errcount += 2 * count;
  313. }
  314. edac_dbg(4, "allocating %d error counters\n", tot_errcount);
  315. pvt = edac_align_ptr(&ptr, sz_pvt, 1);
  316. size = ((unsigned long)pvt) + sz_pvt;
  317. edac_dbg(1, "allocating %u bytes for mci data (%d %s, %d csrows/channels)\n",
  318. size,
  319. tot_dimms,
  320. per_rank ? "ranks" : "dimms",
  321. tot_csrows * tot_channels);
  322. mci = kzalloc(size, GFP_KERNEL);
  323. if (mci == NULL)
  324. return NULL;
  325. /* Adjust pointers so they point within the memory we just allocated
  326. * rather than an imaginary chunk of memory located at address 0.
  327. */
  328. layer = (struct edac_mc_layer *)(((char *)mci) + ((unsigned long)layer));
  329. for (i = 0; i < n_layers; i++) {
  330. mci->ce_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ce_per_layer[i]));
  331. mci->ue_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ue_per_layer[i]));
  332. }
  333. pvt = sz_pvt ? (((char *)mci) + ((unsigned long)pvt)) : NULL;
  334. /* setup index and various internal pointers */
  335. mci->mc_idx = mc_num;
  336. mci->tot_dimms = tot_dimms;
  337. mci->pvt_info = pvt;
  338. mci->n_layers = n_layers;
  339. mci->layers = layer;
  340. memcpy(mci->layers, layers, sizeof(*layer) * n_layers);
  341. mci->nr_csrows = tot_csrows;
  342. mci->num_cschannel = tot_channels;
  343. mci->csbased = per_rank;
  344. /*
  345. * Alocate and fill the csrow/channels structs
  346. */
  347. mci->csrows = kcalloc(tot_csrows, sizeof(*mci->csrows), GFP_KERNEL);
  348. if (!mci->csrows)
  349. goto error;
  350. for (row = 0; row < tot_csrows; row++) {
  351. csr = kzalloc(sizeof(**mci->csrows), GFP_KERNEL);
  352. if (!csr)
  353. goto error;
  354. mci->csrows[row] = csr;
  355. csr->csrow_idx = row;
  356. csr->mci = mci;
  357. csr->nr_channels = tot_channels;
  358. csr->channels = kcalloc(tot_channels, sizeof(*csr->channels),
  359. GFP_KERNEL);
  360. if (!csr->channels)
  361. goto error;
  362. for (chn = 0; chn < tot_channels; chn++) {
  363. chan = kzalloc(sizeof(**csr->channels), GFP_KERNEL);
  364. if (!chan)
  365. goto error;
  366. csr->channels[chn] = chan;
  367. chan->chan_idx = chn;
  368. chan->csrow = csr;
  369. }
  370. }
  371. /*
  372. * Allocate and fill the dimm structs
  373. */
  374. mci->dimms = kcalloc(tot_dimms, sizeof(*mci->dimms), GFP_KERNEL);
  375. if (!mci->dimms)
  376. goto error;
  377. memset(&pos, 0, sizeof(pos));
  378. row = 0;
  379. chn = 0;
  380. for (i = 0; i < tot_dimms; i++) {
  381. chan = mci->csrows[row]->channels[chn];
  382. off = EDAC_DIMM_OFF(layer, n_layers, pos[0], pos[1], pos[2]);
  383. if (off < 0 || off >= tot_dimms) {
  384. edac_mc_printk(mci, KERN_ERR, "EDAC core bug: EDAC_DIMM_OFF is trying to do an illegal data access\n");
  385. goto error;
  386. }
  387. dimm = kzalloc(sizeof(**mci->dimms), GFP_KERNEL);
  388. if (!dimm)
  389. goto error;
  390. mci->dimms[off] = dimm;
  391. dimm->mci = mci;
  392. /*
  393. * Copy DIMM location and initialize it.
  394. */
  395. len = sizeof(dimm->label);
  396. p = dimm->label;
  397. n = snprintf(p, len, "mc#%u", mc_num);
  398. p += n;
  399. len -= n;
  400. for (j = 0; j < n_layers; j++) {
  401. n = snprintf(p, len, "%s#%u",
  402. edac_layer_name[layers[j].type],
  403. pos[j]);
  404. p += n;
  405. len -= n;
  406. dimm->location[j] = pos[j];
  407. if (len <= 0)
  408. break;
  409. }
  410. /* Link it to the csrows old API data */
  411. chan->dimm = dimm;
  412. dimm->csrow = row;
  413. dimm->cschannel = chn;
  414. /* Increment csrow location */
  415. if (layers[0].is_virt_csrow) {
  416. chn++;
  417. if (chn == tot_channels) {
  418. chn = 0;
  419. row++;
  420. }
  421. } else {
  422. row++;
  423. if (row == tot_csrows) {
  424. row = 0;
  425. chn++;
  426. }
  427. }
  428. /* Increment dimm location */
  429. for (j = n_layers - 1; j >= 0; j--) {
  430. pos[j]++;
  431. if (pos[j] < layers[j].size)
  432. break;
  433. pos[j] = 0;
  434. }
  435. }
  436. mci->op_state = OP_ALLOC;
  437. return mci;
  438. error:
  439. _edac_mc_free(mci);
  440. return NULL;
  441. }
  442. EXPORT_SYMBOL_GPL(edac_mc_alloc);
  443. void edac_mc_free(struct mem_ctl_info *mci)
  444. {
  445. edac_dbg(1, "\n");
  446. /* If we're not yet registered with sysfs free only what was allocated
  447. * in edac_mc_alloc().
  448. */
  449. if (!device_is_registered(&mci->dev)) {
  450. _edac_mc_free(mci);
  451. return;
  452. }
  453. /* the mci instance is freed here, when the sysfs object is dropped */
  454. edac_unregister_sysfs(mci);
  455. }
  456. EXPORT_SYMBOL_GPL(edac_mc_free);
  457. bool edac_has_mcs(void)
  458. {
  459. bool ret;
  460. mutex_lock(&mem_ctls_mutex);
  461. ret = list_empty(&mc_devices);
  462. mutex_unlock(&mem_ctls_mutex);
  463. return !ret;
  464. }
  465. EXPORT_SYMBOL_GPL(edac_has_mcs);
  466. /* Caller must hold mem_ctls_mutex */
  467. static struct mem_ctl_info *__find_mci_by_dev(struct device *dev)
  468. {
  469. struct mem_ctl_info *mci;
  470. struct list_head *item;
  471. edac_dbg(3, "\n");
  472. list_for_each(item, &mc_devices) {
  473. mci = list_entry(item, struct mem_ctl_info, link);
  474. if (mci->pdev == dev)
  475. return mci;
  476. }
  477. return NULL;
  478. }
  479. /**
  480. * find_mci_by_dev
  481. *
  482. * scan list of controllers looking for the one that manages
  483. * the 'dev' device
  484. * @dev: pointer to a struct device related with the MCI
  485. */
  486. struct mem_ctl_info *find_mci_by_dev(struct device *dev)
  487. {
  488. struct mem_ctl_info *ret;
  489. mutex_lock(&mem_ctls_mutex);
  490. ret = __find_mci_by_dev(dev);
  491. mutex_unlock(&mem_ctls_mutex);
  492. return ret;
  493. }
  494. EXPORT_SYMBOL_GPL(find_mci_by_dev);
  495. /*
  496. * edac_mc_workq_function
  497. * performs the operation scheduled by a workq request
  498. */
  499. static void edac_mc_workq_function(struct work_struct *work_req)
  500. {
  501. struct delayed_work *d_work = to_delayed_work(work_req);
  502. struct mem_ctl_info *mci = to_edac_mem_ctl_work(d_work);
  503. mutex_lock(&mem_ctls_mutex);
  504. if (mci->op_state != OP_RUNNING_POLL) {
  505. mutex_unlock(&mem_ctls_mutex);
  506. return;
  507. }
  508. if (edac_op_state == EDAC_OPSTATE_POLL)
  509. mci->edac_check(mci);
  510. mutex_unlock(&mem_ctls_mutex);
  511. /* Queue ourselves again. */
  512. edac_queue_work(&mci->work, msecs_to_jiffies(edac_mc_get_poll_msec()));
  513. }
  514. /*
  515. * edac_mc_reset_delay_period(unsigned long value)
  516. *
  517. * user space has updated our poll period value, need to
  518. * reset our workq delays
  519. */
  520. void edac_mc_reset_delay_period(unsigned long value)
  521. {
  522. struct mem_ctl_info *mci;
  523. struct list_head *item;
  524. mutex_lock(&mem_ctls_mutex);
  525. list_for_each(item, &mc_devices) {
  526. mci = list_entry(item, struct mem_ctl_info, link);
  527. if (mci->op_state == OP_RUNNING_POLL)
  528. edac_mod_work(&mci->work, value);
  529. }
  530. mutex_unlock(&mem_ctls_mutex);
  531. }
  532. /* Return 0 on success, 1 on failure.
  533. * Before calling this function, caller must
  534. * assign a unique value to mci->mc_idx.
  535. *
  536. * locking model:
  537. *
  538. * called with the mem_ctls_mutex lock held
  539. */
  540. static int add_mc_to_global_list(struct mem_ctl_info *mci)
  541. {
  542. struct list_head *item, *insert_before;
  543. struct mem_ctl_info *p;
  544. insert_before = &mc_devices;
  545. p = __find_mci_by_dev(mci->pdev);
  546. if (unlikely(p != NULL))
  547. goto fail0;
  548. list_for_each(item, &mc_devices) {
  549. p = list_entry(item, struct mem_ctl_info, link);
  550. if (p->mc_idx >= mci->mc_idx) {
  551. if (unlikely(p->mc_idx == mci->mc_idx))
  552. goto fail1;
  553. insert_before = item;
  554. break;
  555. }
  556. }
  557. list_add_tail_rcu(&mci->link, insert_before);
  558. return 0;
  559. fail0:
  560. edac_printk(KERN_WARNING, EDAC_MC,
  561. "%s (%s) %s %s already assigned %d\n", dev_name(p->pdev),
  562. edac_dev_name(mci), p->mod_name, p->ctl_name, p->mc_idx);
  563. return 1;
  564. fail1:
  565. edac_printk(KERN_WARNING, EDAC_MC,
  566. "bug in low-level driver: attempt to assign\n"
  567. " duplicate mc_idx %d in %s()\n", p->mc_idx, __func__);
  568. return 1;
  569. }
  570. static int del_mc_from_global_list(struct mem_ctl_info *mci)
  571. {
  572. list_del_rcu(&mci->link);
  573. /* these are for safe removal of devices from global list while
  574. * NMI handlers may be traversing list
  575. */
  576. synchronize_rcu();
  577. INIT_LIST_HEAD(&mci->link);
  578. return list_empty(&mc_devices);
  579. }
  580. struct mem_ctl_info *edac_mc_find(int idx)
  581. {
  582. struct mem_ctl_info *mci;
  583. struct list_head *item;
  584. mutex_lock(&mem_ctls_mutex);
  585. list_for_each(item, &mc_devices) {
  586. mci = list_entry(item, struct mem_ctl_info, link);
  587. if (mci->mc_idx == idx)
  588. goto unlock;
  589. }
  590. mci = NULL;
  591. unlock:
  592. mutex_unlock(&mem_ctls_mutex);
  593. return mci;
  594. }
  595. EXPORT_SYMBOL(edac_mc_find);
  596. const char *edac_get_owner(void)
  597. {
  598. return edac_mc_owner;
  599. }
  600. EXPORT_SYMBOL_GPL(edac_get_owner);
  601. /* FIXME - should a warning be printed if no error detection? correction? */
  602. int edac_mc_add_mc_with_groups(struct mem_ctl_info *mci,
  603. const struct attribute_group **groups)
  604. {
  605. int ret = -EINVAL;
  606. edac_dbg(0, "\n");
  607. if (mci->mc_idx >= EDAC_MAX_MCS) {
  608. pr_warn_once("Too many memory controllers: %d\n", mci->mc_idx);
  609. return -ENODEV;
  610. }
  611. #ifdef CONFIG_EDAC_DEBUG
  612. if (edac_debug_level >= 3)
  613. edac_mc_dump_mci(mci);
  614. if (edac_debug_level >= 4) {
  615. int i;
  616. for (i = 0; i < mci->nr_csrows; i++) {
  617. struct csrow_info *csrow = mci->csrows[i];
  618. u32 nr_pages = 0;
  619. int j;
  620. for (j = 0; j < csrow->nr_channels; j++)
  621. nr_pages += csrow->channels[j]->dimm->nr_pages;
  622. if (!nr_pages)
  623. continue;
  624. edac_mc_dump_csrow(csrow);
  625. for (j = 0; j < csrow->nr_channels; j++)
  626. if (csrow->channels[j]->dimm->nr_pages)
  627. edac_mc_dump_channel(csrow->channels[j]);
  628. }
  629. for (i = 0; i < mci->tot_dimms; i++)
  630. if (mci->dimms[i]->nr_pages)
  631. edac_mc_dump_dimm(mci->dimms[i], i);
  632. }
  633. #endif
  634. mutex_lock(&mem_ctls_mutex);
  635. if (edac_mc_owner && edac_mc_owner != mci->mod_name) {
  636. ret = -EPERM;
  637. goto fail0;
  638. }
  639. if (add_mc_to_global_list(mci))
  640. goto fail0;
  641. /* set load time so that error rate can be tracked */
  642. mci->start_time = jiffies;
  643. mci->bus = &mc_bus[mci->mc_idx];
  644. if (edac_create_sysfs_mci_device(mci, groups)) {
  645. edac_mc_printk(mci, KERN_WARNING,
  646. "failed to create sysfs device\n");
  647. goto fail1;
  648. }
  649. if (mci->edac_check) {
  650. mci->op_state = OP_RUNNING_POLL;
  651. INIT_DELAYED_WORK(&mci->work, edac_mc_workq_function);
  652. edac_queue_work(&mci->work, msecs_to_jiffies(edac_mc_get_poll_msec()));
  653. } else {
  654. mci->op_state = OP_RUNNING_INTERRUPT;
  655. }
  656. /* Report action taken */
  657. edac_mc_printk(mci, KERN_INFO,
  658. "Giving out device to module %s controller %s: DEV %s (%s)\n",
  659. mci->mod_name, mci->ctl_name, mci->dev_name,
  660. edac_op_state_to_string(mci->op_state));
  661. edac_mc_owner = mci->mod_name;
  662. mutex_unlock(&mem_ctls_mutex);
  663. return 0;
  664. fail1:
  665. del_mc_from_global_list(mci);
  666. fail0:
  667. mutex_unlock(&mem_ctls_mutex);
  668. return ret;
  669. }
  670. EXPORT_SYMBOL_GPL(edac_mc_add_mc_with_groups);
  671. struct mem_ctl_info *edac_mc_del_mc(struct device *dev)
  672. {
  673. struct mem_ctl_info *mci;
  674. edac_dbg(0, "\n");
  675. mutex_lock(&mem_ctls_mutex);
  676. /* find the requested mci struct in the global list */
  677. mci = __find_mci_by_dev(dev);
  678. if (mci == NULL) {
  679. mutex_unlock(&mem_ctls_mutex);
  680. return NULL;
  681. }
  682. /* mark MCI offline: */
  683. mci->op_state = OP_OFFLINE;
  684. if (del_mc_from_global_list(mci))
  685. edac_mc_owner = NULL;
  686. mutex_unlock(&mem_ctls_mutex);
  687. if (mci->edac_check)
  688. edac_stop_work(&mci->work);
  689. /* remove from sysfs */
  690. edac_remove_sysfs_mci_device(mci);
  691. edac_printk(KERN_INFO, EDAC_MC,
  692. "Removed device %d for %s %s: DEV %s\n", mci->mc_idx,
  693. mci->mod_name, mci->ctl_name, edac_dev_name(mci));
  694. return mci;
  695. }
  696. EXPORT_SYMBOL_GPL(edac_mc_del_mc);
  697. static void edac_mc_scrub_block(unsigned long page, unsigned long offset,
  698. u32 size)
  699. {
  700. struct page *pg;
  701. void *virt_addr;
  702. unsigned long flags = 0;
  703. edac_dbg(3, "\n");
  704. /* ECC error page was not in our memory. Ignore it. */
  705. if (!pfn_valid(page))
  706. return;
  707. /* Find the actual page structure then map it and fix */
  708. pg = pfn_to_page(page);
  709. if (PageHighMem(pg))
  710. local_irq_save(flags);
  711. virt_addr = kmap_atomic(pg);
  712. /* Perform architecture specific atomic scrub operation */
  713. edac_atomic_scrub(virt_addr + offset, size);
  714. /* Unmap and complete */
  715. kunmap_atomic(virt_addr);
  716. if (PageHighMem(pg))
  717. local_irq_restore(flags);
  718. }
  719. /* FIXME - should return -1 */
  720. int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci, unsigned long page)
  721. {
  722. struct csrow_info **csrows = mci->csrows;
  723. int row, i, j, n;
  724. edac_dbg(1, "MC%d: 0x%lx\n", mci->mc_idx, page);
  725. row = -1;
  726. for (i = 0; i < mci->nr_csrows; i++) {
  727. struct csrow_info *csrow = csrows[i];
  728. n = 0;
  729. for (j = 0; j < csrow->nr_channels; j++) {
  730. struct dimm_info *dimm = csrow->channels[j]->dimm;
  731. n += dimm->nr_pages;
  732. }
  733. if (n == 0)
  734. continue;
  735. edac_dbg(3, "MC%d: first(0x%lx) page(0x%lx) last(0x%lx) mask(0x%lx)\n",
  736. mci->mc_idx,
  737. csrow->first_page, page, csrow->last_page,
  738. csrow->page_mask);
  739. if ((page >= csrow->first_page) &&
  740. (page <= csrow->last_page) &&
  741. ((page & csrow->page_mask) ==
  742. (csrow->first_page & csrow->page_mask))) {
  743. row = i;
  744. break;
  745. }
  746. }
  747. if (row == -1)
  748. edac_mc_printk(mci, KERN_ERR,
  749. "could not look up page error address %lx\n",
  750. (unsigned long)page);
  751. return row;
  752. }
  753. EXPORT_SYMBOL_GPL(edac_mc_find_csrow_by_page);
  754. const char *edac_layer_name[] = {
  755. [EDAC_MC_LAYER_BRANCH] = "branch",
  756. [EDAC_MC_LAYER_CHANNEL] = "channel",
  757. [EDAC_MC_LAYER_SLOT] = "slot",
  758. [EDAC_MC_LAYER_CHIP_SELECT] = "csrow",
  759. [EDAC_MC_LAYER_ALL_MEM] = "memory",
  760. };
  761. EXPORT_SYMBOL_GPL(edac_layer_name);
  762. static void edac_inc_ce_error(struct mem_ctl_info *mci,
  763. bool enable_per_layer_report,
  764. const int pos[EDAC_MAX_LAYERS],
  765. const u16 count)
  766. {
  767. int i, index = 0;
  768. mci->ce_mc += count;
  769. if (!enable_per_layer_report) {
  770. mci->ce_noinfo_count += count;
  771. return;
  772. }
  773. for (i = 0; i < mci->n_layers; i++) {
  774. if (pos[i] < 0)
  775. break;
  776. index += pos[i];
  777. mci->ce_per_layer[i][index] += count;
  778. if (i < mci->n_layers - 1)
  779. index *= mci->layers[i + 1].size;
  780. }
  781. }
  782. static void edac_inc_ue_error(struct mem_ctl_info *mci,
  783. bool enable_per_layer_report,
  784. const int pos[EDAC_MAX_LAYERS],
  785. const u16 count)
  786. {
  787. int i, index = 0;
  788. mci->ue_mc += count;
  789. if (!enable_per_layer_report) {
  790. mci->ue_noinfo_count += count;
  791. return;
  792. }
  793. for (i = 0; i < mci->n_layers; i++) {
  794. if (pos[i] < 0)
  795. break;
  796. index += pos[i];
  797. mci->ue_per_layer[i][index] += count;
  798. if (i < mci->n_layers - 1)
  799. index *= mci->layers[i + 1].size;
  800. }
  801. }
  802. static void edac_ce_error(struct mem_ctl_info *mci,
  803. const u16 error_count,
  804. const int pos[EDAC_MAX_LAYERS],
  805. const char *msg,
  806. const char *location,
  807. const char *label,
  808. const char *detail,
  809. const char *other_detail,
  810. const bool enable_per_layer_report,
  811. const unsigned long page_frame_number,
  812. const unsigned long offset_in_page,
  813. long grain)
  814. {
  815. unsigned long remapped_page;
  816. char *msg_aux = "";
  817. if (*msg)
  818. msg_aux = " ";
  819. if (edac_mc_get_log_ce()) {
  820. if (other_detail && *other_detail)
  821. edac_mc_printk(mci, KERN_WARNING,
  822. "%d CE %s%son %s (%s %s - %s)\n",
  823. error_count, msg, msg_aux, label,
  824. location, detail, other_detail);
  825. else
  826. edac_mc_printk(mci, KERN_WARNING,
  827. "%d CE %s%son %s (%s %s)\n",
  828. error_count, msg, msg_aux, label,
  829. location, detail);
  830. }
  831. edac_inc_ce_error(mci, enable_per_layer_report, pos, error_count);
  832. if (mci->scrub_mode == SCRUB_SW_SRC) {
  833. /*
  834. * Some memory controllers (called MCs below) can remap
  835. * memory so that it is still available at a different
  836. * address when PCI devices map into memory.
  837. * MC's that can't do this, lose the memory where PCI
  838. * devices are mapped. This mapping is MC-dependent
  839. * and so we call back into the MC driver for it to
  840. * map the MC page to a physical (CPU) page which can
  841. * then be mapped to a virtual page - which can then
  842. * be scrubbed.
  843. */
  844. remapped_page = mci->ctl_page_to_phys ?
  845. mci->ctl_page_to_phys(mci, page_frame_number) :
  846. page_frame_number;
  847. edac_mc_scrub_block(remapped_page,
  848. offset_in_page, grain);
  849. }
  850. }
  851. static void edac_ue_error(struct mem_ctl_info *mci,
  852. const u16 error_count,
  853. const int pos[EDAC_MAX_LAYERS],
  854. const char *msg,
  855. const char *location,
  856. const char *label,
  857. const char *detail,
  858. const char *other_detail,
  859. const bool enable_per_layer_report)
  860. {
  861. char *msg_aux = "";
  862. if (*msg)
  863. msg_aux = " ";
  864. if (edac_mc_get_log_ue()) {
  865. if (other_detail && *other_detail)
  866. edac_mc_printk(mci, KERN_WARNING,
  867. "%d UE %s%son %s (%s %s - %s)\n",
  868. error_count, msg, msg_aux, label,
  869. location, detail, other_detail);
  870. else
  871. edac_mc_printk(mci, KERN_WARNING,
  872. "%d UE %s%son %s (%s %s)\n",
  873. error_count, msg, msg_aux, label,
  874. location, detail);
  875. }
  876. if (edac_mc_get_panic_on_ue()) {
  877. if (other_detail && *other_detail)
  878. panic("UE %s%son %s (%s%s - %s)\n",
  879. msg, msg_aux, label, location, detail, other_detail);
  880. else
  881. panic("UE %s%son %s (%s%s)\n",
  882. msg, msg_aux, label, location, detail);
  883. }
  884. edac_inc_ue_error(mci, enable_per_layer_report, pos, error_count);
  885. }
  886. void edac_raw_mc_handle_error(const enum hw_event_mc_err_type type,
  887. struct mem_ctl_info *mci,
  888. struct edac_raw_error_desc *e)
  889. {
  890. char detail[80];
  891. int pos[EDAC_MAX_LAYERS] = { e->top_layer, e->mid_layer, e->low_layer };
  892. /* Memory type dependent details about the error */
  893. if (type == HW_EVENT_ERR_CORRECTED) {
  894. snprintf(detail, sizeof(detail),
  895. "page:0x%lx offset:0x%lx grain:%ld syndrome:0x%lx",
  896. e->page_frame_number, e->offset_in_page,
  897. e->grain, e->syndrome);
  898. edac_ce_error(mci, e->error_count, pos, e->msg, e->location, e->label,
  899. detail, e->other_detail, e->enable_per_layer_report,
  900. e->page_frame_number, e->offset_in_page, e->grain);
  901. } else {
  902. snprintf(detail, sizeof(detail),
  903. "page:0x%lx offset:0x%lx grain:%ld",
  904. e->page_frame_number, e->offset_in_page, e->grain);
  905. edac_ue_error(mci, e->error_count, pos, e->msg, e->location, e->label,
  906. detail, e->other_detail, e->enable_per_layer_report);
  907. }
  908. }
  909. EXPORT_SYMBOL_GPL(edac_raw_mc_handle_error);
  910. void edac_mc_handle_error(const enum hw_event_mc_err_type type,
  911. struct mem_ctl_info *mci,
  912. const u16 error_count,
  913. const unsigned long page_frame_number,
  914. const unsigned long offset_in_page,
  915. const unsigned long syndrome,
  916. const int top_layer,
  917. const int mid_layer,
  918. const int low_layer,
  919. const char *msg,
  920. const char *other_detail)
  921. {
  922. char *p;
  923. int row = -1, chan = -1;
  924. int pos[EDAC_MAX_LAYERS] = { top_layer, mid_layer, low_layer };
  925. int i, n_labels = 0;
  926. u8 grain_bits;
  927. struct edac_raw_error_desc *e = &mci->error_desc;
  928. edac_dbg(3, "MC%d\n", mci->mc_idx);
  929. /* Fills the error report buffer */
  930. memset(e, 0, sizeof (*e));
  931. e->error_count = error_count;
  932. e->top_layer = top_layer;
  933. e->mid_layer = mid_layer;
  934. e->low_layer = low_layer;
  935. e->page_frame_number = page_frame_number;
  936. e->offset_in_page = offset_in_page;
  937. e->syndrome = syndrome;
  938. e->msg = msg;
  939. e->other_detail = other_detail;
  940. /*
  941. * Check if the event report is consistent and if the memory
  942. * location is known. If it is known, enable_per_layer_report will be
  943. * true, the DIMM(s) label info will be filled and the per-layer
  944. * error counters will be incremented.
  945. */
  946. for (i = 0; i < mci->n_layers; i++) {
  947. if (pos[i] >= (int)mci->layers[i].size) {
  948. edac_mc_printk(mci, KERN_ERR,
  949. "INTERNAL ERROR: %s value is out of range (%d >= %d)\n",
  950. edac_layer_name[mci->layers[i].type],
  951. pos[i], mci->layers[i].size);
  952. /*
  953. * Instead of just returning it, let's use what's
  954. * known about the error. The increment routines and
  955. * the DIMM filter logic will do the right thing by
  956. * pointing the likely damaged DIMMs.
  957. */
  958. pos[i] = -1;
  959. }
  960. if (pos[i] >= 0)
  961. e->enable_per_layer_report = true;
  962. }
  963. /*
  964. * Get the dimm label/grain that applies to the match criteria.
  965. * As the error algorithm may not be able to point to just one memory
  966. * stick, the logic here will get all possible labels that could
  967. * pottentially be affected by the error.
  968. * On FB-DIMM memory controllers, for uncorrected errors, it is common
  969. * to have only the MC channel and the MC dimm (also called "branch")
  970. * but the channel is not known, as the memory is arranged in pairs,
  971. * where each memory belongs to a separate channel within the same
  972. * branch.
  973. */
  974. p = e->label;
  975. *p = '\0';
  976. for (i = 0; i < mci->tot_dimms; i++) {
  977. struct dimm_info *dimm = mci->dimms[i];
  978. if (top_layer >= 0 && top_layer != dimm->location[0])
  979. continue;
  980. if (mid_layer >= 0 && mid_layer != dimm->location[1])
  981. continue;
  982. if (low_layer >= 0 && low_layer != dimm->location[2])
  983. continue;
  984. /* get the max grain, over the error match range */
  985. if (dimm->grain > e->grain)
  986. e->grain = dimm->grain;
  987. /*
  988. * If the error is memory-controller wide, there's no need to
  989. * seek for the affected DIMMs because the whole
  990. * channel/memory controller/... may be affected.
  991. * Also, don't show errors for empty DIMM slots.
  992. */
  993. if (e->enable_per_layer_report && dimm->nr_pages) {
  994. if (n_labels >= EDAC_MAX_LABELS) {
  995. e->enable_per_layer_report = false;
  996. break;
  997. }
  998. n_labels++;
  999. if (p != e->label) {
  1000. strcpy(p, OTHER_LABEL);
  1001. p += strlen(OTHER_LABEL);
  1002. }
  1003. strcpy(p, dimm->label);
  1004. p += strlen(p);
  1005. *p = '\0';
  1006. /*
  1007. * get csrow/channel of the DIMM, in order to allow
  1008. * incrementing the compat API counters
  1009. */
  1010. edac_dbg(4, "%s csrows map: (%d,%d)\n",
  1011. mci->csbased ? "rank" : "dimm",
  1012. dimm->csrow, dimm->cschannel);
  1013. if (row == -1)
  1014. row = dimm->csrow;
  1015. else if (row >= 0 && row != dimm->csrow)
  1016. row = -2;
  1017. if (chan == -1)
  1018. chan = dimm->cschannel;
  1019. else if (chan >= 0 && chan != dimm->cschannel)
  1020. chan = -2;
  1021. }
  1022. }
  1023. if (!e->enable_per_layer_report) {
  1024. strcpy(e->label, "any memory");
  1025. } else {
  1026. edac_dbg(4, "csrow/channel to increment: (%d,%d)\n", row, chan);
  1027. if (p == e->label)
  1028. strcpy(e->label, "unknown memory");
  1029. if (type == HW_EVENT_ERR_CORRECTED) {
  1030. if (row >= 0) {
  1031. mci->csrows[row]->ce_count += error_count;
  1032. if (chan >= 0)
  1033. mci->csrows[row]->channels[chan]->ce_count += error_count;
  1034. }
  1035. } else
  1036. if (row >= 0)
  1037. mci->csrows[row]->ue_count += error_count;
  1038. }
  1039. /* Fill the RAM location data */
  1040. p = e->location;
  1041. for (i = 0; i < mci->n_layers; i++) {
  1042. if (pos[i] < 0)
  1043. continue;
  1044. p += sprintf(p, "%s:%d ",
  1045. edac_layer_name[mci->layers[i].type],
  1046. pos[i]);
  1047. }
  1048. if (p > e->location)
  1049. *(p - 1) = '\0';
  1050. /* Sanity-check driver-supplied grain value. */
  1051. if (WARN_ON_ONCE(!e->grain))
  1052. e->grain = 1;
  1053. grain_bits = fls_long(e->grain - 1);
  1054. /* Report the error via the trace interface */
  1055. if (IS_ENABLED(CONFIG_RAS))
  1056. trace_mc_event(type, e->msg, e->label, e->error_count,
  1057. mci->mc_idx, e->top_layer, e->mid_layer,
  1058. e->low_layer,
  1059. (e->page_frame_number << PAGE_SHIFT) | e->offset_in_page,
  1060. grain_bits, e->syndrome, e->other_detail);
  1061. edac_raw_mc_handle_error(type, mci, e);
  1062. }
  1063. EXPORT_SYMBOL_GPL(edac_mc_handle_error);