edac_mc.c 26 KB

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