ghes_edac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * GHES/EDAC Linux driver
  3. *
  4. * This file may be distributed under the terms of the GNU General Public
  5. * License version 2.
  6. *
  7. * Copyright (c) 2013 by Mauro Carvalho Chehab
  8. *
  9. * Red Hat Inc. http://www.redhat.com
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <acpi/ghes.h>
  13. #include <linux/edac.h>
  14. #include <linux/dmi.h>
  15. #include "edac_core.h"
  16. #include <ras/ras_event.h>
  17. #define GHES_EDAC_REVISION " Ver: 1.0.0"
  18. struct ghes_edac_pvt {
  19. struct list_head list;
  20. struct ghes *ghes;
  21. struct mem_ctl_info *mci;
  22. /* Buffers for the error handling routine */
  23. char detail_location[240];
  24. char other_detail[160];
  25. char msg[80];
  26. };
  27. static LIST_HEAD(ghes_reglist);
  28. static DEFINE_MUTEX(ghes_edac_lock);
  29. static int ghes_edac_mc_num;
  30. /* Memory Device - Type 17 of SMBIOS spec */
  31. struct memdev_dmi_entry {
  32. u8 type;
  33. u8 length;
  34. u16 handle;
  35. u16 phys_mem_array_handle;
  36. u16 mem_err_info_handle;
  37. u16 total_width;
  38. u16 data_width;
  39. u16 size;
  40. u8 form_factor;
  41. u8 device_set;
  42. u8 device_locator;
  43. u8 bank_locator;
  44. u8 memory_type;
  45. u16 type_detail;
  46. u16 speed;
  47. u8 manufacturer;
  48. u8 serial_number;
  49. u8 asset_tag;
  50. u8 part_number;
  51. u8 attributes;
  52. u32 extended_size;
  53. u16 conf_mem_clk_speed;
  54. } __attribute__((__packed__));
  55. struct ghes_edac_dimm_fill {
  56. struct mem_ctl_info *mci;
  57. unsigned count;
  58. };
  59. static void ghes_edac_count_dimms(const struct dmi_header *dh, void *arg)
  60. {
  61. int *num_dimm = arg;
  62. if (dh->type == DMI_ENTRY_MEM_DEVICE)
  63. (*num_dimm)++;
  64. }
  65. static void ghes_edac_dmidecode(const struct dmi_header *dh, void *arg)
  66. {
  67. struct ghes_edac_dimm_fill *dimm_fill = arg;
  68. struct mem_ctl_info *mci = dimm_fill->mci;
  69. if (dh->type == DMI_ENTRY_MEM_DEVICE) {
  70. struct memdev_dmi_entry *entry = (struct memdev_dmi_entry *)dh;
  71. struct dimm_info *dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms,
  72. mci->n_layers,
  73. dimm_fill->count, 0, 0);
  74. if (entry->size == 0xffff) {
  75. pr_info("Can't get DIMM%i size\n",
  76. dimm_fill->count);
  77. dimm->nr_pages = MiB_TO_PAGES(32);/* Unknown */
  78. } else if (entry->size == 0x7fff) {
  79. dimm->nr_pages = MiB_TO_PAGES(entry->extended_size);
  80. } else {
  81. if (entry->size & 1 << 15)
  82. dimm->nr_pages = MiB_TO_PAGES((entry->size &
  83. 0x7fff) << 10);
  84. else
  85. dimm->nr_pages = MiB_TO_PAGES(entry->size);
  86. }
  87. switch (entry->memory_type) {
  88. case 0x12:
  89. if (entry->type_detail & 1 << 13)
  90. dimm->mtype = MEM_RDDR;
  91. else
  92. dimm->mtype = MEM_DDR;
  93. break;
  94. case 0x13:
  95. if (entry->type_detail & 1 << 13)
  96. dimm->mtype = MEM_RDDR2;
  97. else
  98. dimm->mtype = MEM_DDR2;
  99. break;
  100. case 0x14:
  101. dimm->mtype = MEM_FB_DDR2;
  102. break;
  103. case 0x18:
  104. if (entry->type_detail & 1 << 13)
  105. dimm->mtype = MEM_RDDR3;
  106. else
  107. dimm->mtype = MEM_DDR3;
  108. break;
  109. default:
  110. if (entry->type_detail & 1 << 6)
  111. dimm->mtype = MEM_RMBS;
  112. else if ((entry->type_detail & ((1 << 7) | (1 << 13)))
  113. == ((1 << 7) | (1 << 13)))
  114. dimm->mtype = MEM_RDR;
  115. else if (entry->type_detail & 1 << 7)
  116. dimm->mtype = MEM_SDR;
  117. else if (entry->type_detail & 1 << 9)
  118. dimm->mtype = MEM_EDO;
  119. else
  120. dimm->mtype = MEM_UNKNOWN;
  121. }
  122. /*
  123. * Actually, we can only detect if the memory has bits for
  124. * checksum or not
  125. */
  126. if (entry->total_width == entry->data_width)
  127. dimm->edac_mode = EDAC_NONE;
  128. else
  129. dimm->edac_mode = EDAC_SECDED;
  130. dimm->dtype = DEV_UNKNOWN;
  131. dimm->grain = 128; /* Likely, worse case */
  132. /*
  133. * FIXME: It shouldn't be hard to also fill the DIMM labels
  134. */
  135. if (dimm->nr_pages) {
  136. edac_dbg(1, "DIMM%i: %s size = %d MB%s\n",
  137. dimm_fill->count, edac_mem_types[dimm->mtype],
  138. PAGES_TO_MiB(dimm->nr_pages),
  139. (dimm->edac_mode != EDAC_NONE) ? "(ECC)" : "");
  140. edac_dbg(2, "\ttype %d, detail 0x%02x, width %d(total %d)\n",
  141. entry->memory_type, entry->type_detail,
  142. entry->total_width, entry->data_width);
  143. }
  144. dimm_fill->count++;
  145. }
  146. }
  147. void ghes_edac_report_mem_error(struct ghes *ghes, int sev,
  148. struct cper_sec_mem_err *mem_err)
  149. {
  150. enum hw_event_mc_err_type type;
  151. struct edac_raw_error_desc *e;
  152. struct mem_ctl_info *mci;
  153. struct ghes_edac_pvt *pvt = NULL;
  154. char *p;
  155. u8 grain_bits;
  156. list_for_each_entry(pvt, &ghes_reglist, list) {
  157. if (ghes == pvt->ghes)
  158. break;
  159. }
  160. if (!pvt) {
  161. pr_err("Internal error: Can't find EDAC structure\n");
  162. return;
  163. }
  164. mci = pvt->mci;
  165. e = &mci->error_desc;
  166. /* Cleans the error report buffer */
  167. memset(e, 0, sizeof (*e));
  168. e->error_count = 1;
  169. strcpy(e->label, "unknown label");
  170. e->msg = pvt->msg;
  171. e->other_detail = pvt->other_detail;
  172. e->top_layer = -1;
  173. e->mid_layer = -1;
  174. e->low_layer = -1;
  175. *pvt->other_detail = '\0';
  176. *pvt->msg = '\0';
  177. switch (sev) {
  178. case GHES_SEV_CORRECTED:
  179. type = HW_EVENT_ERR_CORRECTED;
  180. break;
  181. case GHES_SEV_RECOVERABLE:
  182. type = HW_EVENT_ERR_UNCORRECTED;
  183. break;
  184. case GHES_SEV_PANIC:
  185. type = HW_EVENT_ERR_FATAL;
  186. break;
  187. default:
  188. case GHES_SEV_NO:
  189. type = HW_EVENT_ERR_INFO;
  190. }
  191. edac_dbg(1, "error validation_bits: 0x%08llx\n",
  192. (long long)mem_err->validation_bits);
  193. /* Error type, mapped on e->msg */
  194. if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_TYPE) {
  195. p = pvt->msg;
  196. switch (mem_err->error_type) {
  197. case 0:
  198. p += sprintf(p, "Unknown");
  199. break;
  200. case 1:
  201. p += sprintf(p, "No error");
  202. break;
  203. case 2:
  204. p += sprintf(p, "Single-bit ECC");
  205. break;
  206. case 3:
  207. p += sprintf(p, "Multi-bit ECC");
  208. break;
  209. case 4:
  210. p += sprintf(p, "Single-symbol ChipKill ECC");
  211. break;
  212. case 5:
  213. p += sprintf(p, "Multi-symbol ChipKill ECC");
  214. break;
  215. case 6:
  216. p += sprintf(p, "Master abort");
  217. break;
  218. case 7:
  219. p += sprintf(p, "Target abort");
  220. break;
  221. case 8:
  222. p += sprintf(p, "Parity Error");
  223. break;
  224. case 9:
  225. p += sprintf(p, "Watchdog timeout");
  226. break;
  227. case 10:
  228. p += sprintf(p, "Invalid address");
  229. break;
  230. case 11:
  231. p += sprintf(p, "Mirror Broken");
  232. break;
  233. case 12:
  234. p += sprintf(p, "Memory Sparing");
  235. break;
  236. case 13:
  237. p += sprintf(p, "Scrub corrected error");
  238. break;
  239. case 14:
  240. p += sprintf(p, "Scrub uncorrected error");
  241. break;
  242. case 15:
  243. p += sprintf(p, "Physical Memory Map-out event");
  244. break;
  245. default:
  246. p += sprintf(p, "reserved error (%d)",
  247. mem_err->error_type);
  248. }
  249. } else {
  250. strcpy(pvt->msg, "unknown error");
  251. }
  252. /* Error address */
  253. if (mem_err->validation_bits & CPER_MEM_VALID_PA) {
  254. e->page_frame_number = mem_err->physical_addr >> PAGE_SHIFT;
  255. e->offset_in_page = mem_err->physical_addr & ~PAGE_MASK;
  256. }
  257. /* Error grain */
  258. if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)
  259. e->grain = ~(mem_err->physical_addr_mask & ~PAGE_MASK);
  260. /* Memory error location, mapped on e->location */
  261. p = e->location;
  262. if (mem_err->validation_bits & CPER_MEM_VALID_NODE)
  263. p += sprintf(p, "node:%d ", mem_err->node);
  264. if (mem_err->validation_bits & CPER_MEM_VALID_CARD)
  265. p += sprintf(p, "card:%d ", mem_err->card);
  266. if (mem_err->validation_bits & CPER_MEM_VALID_MODULE)
  267. p += sprintf(p, "module:%d ", mem_err->module);
  268. if (mem_err->validation_bits & CPER_MEM_VALID_RANK_NUMBER)
  269. p += sprintf(p, "rank:%d ", mem_err->rank);
  270. if (mem_err->validation_bits & CPER_MEM_VALID_BANK)
  271. p += sprintf(p, "bank:%d ", mem_err->bank);
  272. if (mem_err->validation_bits & CPER_MEM_VALID_ROW)
  273. p += sprintf(p, "row:%d ", mem_err->row);
  274. if (mem_err->validation_bits & CPER_MEM_VALID_COLUMN)
  275. p += sprintf(p, "col:%d ", mem_err->column);
  276. if (mem_err->validation_bits & CPER_MEM_VALID_BIT_POSITION)
  277. p += sprintf(p, "bit_pos:%d ", mem_err->bit_pos);
  278. if (mem_err->validation_bits & CPER_MEM_VALID_MODULE_HANDLE) {
  279. const char *bank = NULL, *device = NULL;
  280. dmi_memdev_name(mem_err->mem_dev_handle, &bank, &device);
  281. if (bank != NULL && device != NULL)
  282. p += sprintf(p, "DIMM location:%s %s ", bank, device);
  283. else
  284. p += sprintf(p, "DIMM DMI handle: 0x%.4x ",
  285. mem_err->mem_dev_handle);
  286. }
  287. if (p > e->location)
  288. *(p - 1) = '\0';
  289. /* All other fields are mapped on e->other_detail */
  290. p = pvt->other_detail;
  291. if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_STATUS) {
  292. u64 status = mem_err->error_status;
  293. p += sprintf(p, "status(0x%016llx): ", (long long)status);
  294. switch ((status >> 8) & 0xff) {
  295. case 1:
  296. p += sprintf(p, "Error detected internal to the component ");
  297. break;
  298. case 16:
  299. p += sprintf(p, "Error detected in the bus ");
  300. break;
  301. case 4:
  302. p += sprintf(p, "Storage error in DRAM memory ");
  303. break;
  304. case 5:
  305. p += sprintf(p, "Storage error in TLB ");
  306. break;
  307. case 6:
  308. p += sprintf(p, "Storage error in cache ");
  309. break;
  310. case 7:
  311. p += sprintf(p, "Error in one or more functional units ");
  312. break;
  313. case 8:
  314. p += sprintf(p, "component failed self test ");
  315. break;
  316. case 9:
  317. p += sprintf(p, "Overflow or undervalue of internal queue ");
  318. break;
  319. case 17:
  320. p += sprintf(p, "Virtual address not found on IO-TLB or IO-PDIR ");
  321. break;
  322. case 18:
  323. p += sprintf(p, "Improper access error ");
  324. break;
  325. case 19:
  326. p += sprintf(p, "Access to a memory address which is not mapped to any component ");
  327. break;
  328. case 20:
  329. p += sprintf(p, "Loss of Lockstep ");
  330. break;
  331. case 21:
  332. p += sprintf(p, "Response not associated with a request ");
  333. break;
  334. case 22:
  335. p += sprintf(p, "Bus parity error - must also set the A, C, or D Bits ");
  336. break;
  337. case 23:
  338. p += sprintf(p, "Detection of a PATH_ERROR ");
  339. break;
  340. case 25:
  341. p += sprintf(p, "Bus operation timeout ");
  342. break;
  343. case 26:
  344. p += sprintf(p, "A read was issued to data that has been poisoned ");
  345. break;
  346. default:
  347. p += sprintf(p, "reserved ");
  348. break;
  349. }
  350. }
  351. if (mem_err->validation_bits & CPER_MEM_VALID_REQUESTOR_ID)
  352. p += sprintf(p, "requestorID: 0x%016llx ",
  353. (long long)mem_err->requestor_id);
  354. if (mem_err->validation_bits & CPER_MEM_VALID_RESPONDER_ID)
  355. p += sprintf(p, "responderID: 0x%016llx ",
  356. (long long)mem_err->responder_id);
  357. if (mem_err->validation_bits & CPER_MEM_VALID_TARGET_ID)
  358. p += sprintf(p, "targetID: 0x%016llx ",
  359. (long long)mem_err->responder_id);
  360. if (p > pvt->other_detail)
  361. *(p - 1) = '\0';
  362. /* Generate the trace event */
  363. grain_bits = fls_long(e->grain);
  364. snprintf(pvt->detail_location, sizeof(pvt->detail_location),
  365. "APEI location: %s %s", e->location, e->other_detail);
  366. trace_mc_event(type, e->msg, e->label, e->error_count,
  367. mci->mc_idx, e->top_layer, e->mid_layer, e->low_layer,
  368. (e->page_frame_number << PAGE_SHIFT) | e->offset_in_page,
  369. grain_bits, e->syndrome, pvt->detail_location);
  370. /* Report the error via EDAC API */
  371. edac_raw_mc_handle_error(type, mci, e);
  372. }
  373. EXPORT_SYMBOL_GPL(ghes_edac_report_mem_error);
  374. int ghes_edac_register(struct ghes *ghes, struct device *dev)
  375. {
  376. bool fake = false;
  377. int rc, num_dimm = 0;
  378. struct mem_ctl_info *mci;
  379. struct edac_mc_layer layers[1];
  380. struct ghes_edac_pvt *pvt;
  381. struct ghes_edac_dimm_fill dimm_fill;
  382. /* Get the number of DIMMs */
  383. dmi_walk(ghes_edac_count_dimms, &num_dimm);
  384. /* Check if we've got a bogus BIOS */
  385. if (num_dimm == 0) {
  386. fake = true;
  387. num_dimm = 1;
  388. }
  389. layers[0].type = EDAC_MC_LAYER_ALL_MEM;
  390. layers[0].size = num_dimm;
  391. layers[0].is_virt_csrow = true;
  392. /*
  393. * We need to serialize edac_mc_alloc() and edac_mc_add_mc(),
  394. * to avoid duplicated memory controller numbers
  395. */
  396. mutex_lock(&ghes_edac_lock);
  397. mci = edac_mc_alloc(ghes_edac_mc_num, ARRAY_SIZE(layers), layers,
  398. sizeof(*pvt));
  399. if (!mci) {
  400. pr_info("Can't allocate memory for EDAC data\n");
  401. mutex_unlock(&ghes_edac_lock);
  402. return -ENOMEM;
  403. }
  404. pvt = mci->pvt_info;
  405. memset(pvt, 0, sizeof(*pvt));
  406. list_add_tail(&pvt->list, &ghes_reglist);
  407. pvt->ghes = ghes;
  408. pvt->mci = mci;
  409. mci->pdev = dev;
  410. mci->mtype_cap = MEM_FLAG_EMPTY;
  411. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  412. mci->edac_cap = EDAC_FLAG_NONE;
  413. mci->mod_name = "ghes_edac.c";
  414. mci->mod_ver = GHES_EDAC_REVISION;
  415. mci->ctl_name = "ghes_edac";
  416. mci->dev_name = "ghes";
  417. if (!ghes_edac_mc_num) {
  418. if (!fake) {
  419. pr_info("This EDAC driver relies on BIOS to enumerate memory and get error reports.\n");
  420. pr_info("Unfortunately, not all BIOSes reflect the memory layout correctly.\n");
  421. pr_info("So, the end result of using this driver varies from vendor to vendor.\n");
  422. pr_info("If you find incorrect reports, please contact your hardware vendor\n");
  423. pr_info("to correct its BIOS.\n");
  424. pr_info("This system has %d DIMM sockets.\n",
  425. num_dimm);
  426. } else {
  427. pr_info("This system has a very crappy BIOS: It doesn't even list the DIMMS.\n");
  428. pr_info("Its SMBIOS info is wrong. It is doubtful that the error report would\n");
  429. pr_info("work on such system. Use this driver with caution\n");
  430. }
  431. }
  432. if (!fake) {
  433. /*
  434. * Fill DIMM info from DMI for the memory controller #0
  435. *
  436. * Keep it in blank for the other memory controllers, as
  437. * there's no reliable way to properly credit each DIMM to
  438. * the memory controller, as different BIOSes fill the
  439. * DMI bank location fields on different ways
  440. */
  441. if (!ghes_edac_mc_num) {
  442. dimm_fill.count = 0;
  443. dimm_fill.mci = mci;
  444. dmi_walk(ghes_edac_dmidecode, &dimm_fill);
  445. }
  446. } else {
  447. struct dimm_info *dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms,
  448. mci->n_layers, 0, 0, 0);
  449. dimm->nr_pages = 1;
  450. dimm->grain = 128;
  451. dimm->mtype = MEM_UNKNOWN;
  452. dimm->dtype = DEV_UNKNOWN;
  453. dimm->edac_mode = EDAC_SECDED;
  454. }
  455. rc = edac_mc_add_mc(mci);
  456. if (rc < 0) {
  457. pr_info("Can't register at EDAC core\n");
  458. edac_mc_free(mci);
  459. mutex_unlock(&ghes_edac_lock);
  460. return -ENODEV;
  461. }
  462. ghes_edac_mc_num++;
  463. mutex_unlock(&ghes_edac_lock);
  464. return 0;
  465. }
  466. EXPORT_SYMBOL_GPL(ghes_edac_register);
  467. void ghes_edac_unregister(struct ghes *ghes)
  468. {
  469. struct mem_ctl_info *mci;
  470. struct ghes_edac_pvt *pvt, *tmp;
  471. list_for_each_entry_safe(pvt, tmp, &ghes_reglist, list) {
  472. if (ghes == pvt->ghes) {
  473. mci = pvt->mci;
  474. edac_mc_del_mc(mci->pdev);
  475. edac_mc_free(mci);
  476. list_del(&pvt->list);
  477. }
  478. }
  479. }
  480. EXPORT_SYMBOL_GPL(ghes_edac_unregister);