edac_mc.c 31 KB

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