edac_mc.c 32 KB

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