edac_mc.c 26 KB

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