edac_mc.c 31 KB

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