ghes_edac.c 14 KB

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