edac_mc.c 30 KB

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