edac_mc.c 32 KB

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