edac_mc.c 31 KB

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