ghes.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * APEI Generic Hardware Error Source support
  3. *
  4. * Generic Hardware Error Source provides a way to report platform
  5. * hardware errors (such as that from chipset). It works in so called
  6. * "Firmware First" mode, that is, hardware errors are reported to
  7. * firmware firstly, then reported to Linux by firmware. This way,
  8. * some non-standard hardware error registers or non-standard hardware
  9. * link can be checked by firmware to produce more hardware error
  10. * information for Linux.
  11. *
  12. * For more information about Generic Hardware Error Source, please
  13. * refer to ACPI Specification version 4.0, section 17.3.2.6
  14. *
  15. * Copyright 2010,2011 Intel Corp.
  16. * Author: Huang Ying <ying.huang@intel.com>
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License version
  20. * 2 as published by the Free Software Foundation;
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/init.h>
  30. #include <linux/acpi.h>
  31. #include <linux/io.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/timer.h>
  34. #include <linux/cper.h>
  35. #include <linux/kdebug.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/mutex.h>
  38. #include <linux/ratelimit.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/irq_work.h>
  41. #include <linux/llist.h>
  42. #include <linux/genalloc.h>
  43. #include <linux/pci.h>
  44. #include <linux/aer.h>
  45. #include <linux/nmi.h>
  46. #include <acpi/ghes.h>
  47. #include <acpi/apei.h>
  48. #include <asm/tlbflush.h>
  49. #include "apei-internal.h"
  50. #define GHES_PFX "GHES: "
  51. #define GHES_ESTATUS_MAX_SIZE 65536
  52. #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
  53. #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
  54. /* This is just an estimation for memory pool allocation */
  55. #define GHES_ESTATUS_CACHE_AVG_SIZE 512
  56. #define GHES_ESTATUS_CACHES_SIZE 4
  57. #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
  58. /* Prevent too many caches are allocated because of RCU */
  59. #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
  60. #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
  61. (sizeof(struct ghes_estatus_cache) + (estatus_len))
  62. #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
  63. ((struct acpi_hest_generic_status *) \
  64. ((struct ghes_estatus_cache *)(estatus_cache) + 1))
  65. #define GHES_ESTATUS_NODE_LEN(estatus_len) \
  66. (sizeof(struct ghes_estatus_node) + (estatus_len))
  67. #define GHES_ESTATUS_FROM_NODE(estatus_node) \
  68. ((struct acpi_hest_generic_status *) \
  69. ((struct ghes_estatus_node *)(estatus_node) + 1))
  70. /*
  71. * This driver isn't really modular, however for the time being,
  72. * continuing to use module_param is the easiest way to remain
  73. * compatible with existing boot arg use cases.
  74. */
  75. bool ghes_disable;
  76. module_param_named(disable, ghes_disable, bool, 0);
  77. /*
  78. * All error sources notified with SCI shares one notifier function,
  79. * so they need to be linked and checked one by one. This is applied
  80. * to NMI too.
  81. *
  82. * RCU is used for these lists, so ghes_list_mutex is only used for
  83. * list changing, not for traversing.
  84. */
  85. static LIST_HEAD(ghes_sci);
  86. static DEFINE_MUTEX(ghes_list_mutex);
  87. /*
  88. * Because the memory area used to transfer hardware error information
  89. * from BIOS to Linux can be determined only in NMI, IRQ or timer
  90. * handler, but general ioremap can not be used in atomic context, so
  91. * a special version of atomic ioremap is implemented for that.
  92. */
  93. /*
  94. * Two virtual pages are used, one for IRQ/PROCESS context, the other for
  95. * NMI context (optionally).
  96. */
  97. #ifdef CONFIG_HAVE_ACPI_APEI_NMI
  98. #define GHES_IOREMAP_PAGES 2
  99. #else
  100. #define GHES_IOREMAP_PAGES 1
  101. #endif
  102. #define GHES_IOREMAP_IRQ_PAGE(base) (base)
  103. #define GHES_IOREMAP_NMI_PAGE(base) ((base) + PAGE_SIZE)
  104. /* virtual memory area for atomic ioremap */
  105. static struct vm_struct *ghes_ioremap_area;
  106. /*
  107. * These 2 spinlock is used to prevent atomic ioremap virtual memory
  108. * area from being mapped simultaneously.
  109. */
  110. static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi);
  111. static DEFINE_SPINLOCK(ghes_ioremap_lock_irq);
  112. static struct gen_pool *ghes_estatus_pool;
  113. static unsigned long ghes_estatus_pool_size_request;
  114. static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
  115. static atomic_t ghes_estatus_cache_alloced;
  116. static int ghes_ioremap_init(void)
  117. {
  118. ghes_ioremap_area = __get_vm_area(PAGE_SIZE * GHES_IOREMAP_PAGES,
  119. VM_IOREMAP, VMALLOC_START, VMALLOC_END);
  120. if (!ghes_ioremap_area) {
  121. pr_err(GHES_PFX "Failed to allocate virtual memory area for atomic ioremap.\n");
  122. return -ENOMEM;
  123. }
  124. return 0;
  125. }
  126. static void ghes_ioremap_exit(void)
  127. {
  128. free_vm_area(ghes_ioremap_area);
  129. }
  130. static void __iomem *ghes_ioremap_pfn_nmi(u64 pfn)
  131. {
  132. unsigned long vaddr;
  133. vaddr = (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area->addr);
  134. ioremap_page_range(vaddr, vaddr + PAGE_SIZE,
  135. pfn << PAGE_SHIFT, PAGE_KERNEL);
  136. return (void __iomem *)vaddr;
  137. }
  138. static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
  139. {
  140. unsigned long vaddr, paddr;
  141. pgprot_t prot;
  142. vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
  143. paddr = pfn << PAGE_SHIFT;
  144. prot = arch_apei_get_mem_attribute(paddr);
  145. ioremap_page_range(vaddr, vaddr + PAGE_SIZE, paddr, prot);
  146. return (void __iomem *)vaddr;
  147. }
  148. static void ghes_iounmap_nmi(void __iomem *vaddr_ptr)
  149. {
  150. unsigned long vaddr = (unsigned long __force)vaddr_ptr;
  151. void *base = ghes_ioremap_area->addr;
  152. BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_NMI_PAGE(base));
  153. unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
  154. arch_apei_flush_tlb_one(vaddr);
  155. }
  156. static void ghes_iounmap_irq(void __iomem *vaddr_ptr)
  157. {
  158. unsigned long vaddr = (unsigned long __force)vaddr_ptr;
  159. void *base = ghes_ioremap_area->addr;
  160. BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_IRQ_PAGE(base));
  161. unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
  162. arch_apei_flush_tlb_one(vaddr);
  163. }
  164. static int ghes_estatus_pool_init(void)
  165. {
  166. ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
  167. if (!ghes_estatus_pool)
  168. return -ENOMEM;
  169. return 0;
  170. }
  171. static void ghes_estatus_pool_free_chunk_page(struct gen_pool *pool,
  172. struct gen_pool_chunk *chunk,
  173. void *data)
  174. {
  175. free_page(chunk->start_addr);
  176. }
  177. static void ghes_estatus_pool_exit(void)
  178. {
  179. gen_pool_for_each_chunk(ghes_estatus_pool,
  180. ghes_estatus_pool_free_chunk_page, NULL);
  181. gen_pool_destroy(ghes_estatus_pool);
  182. }
  183. static int ghes_estatus_pool_expand(unsigned long len)
  184. {
  185. unsigned long i, pages, size, addr;
  186. int ret;
  187. ghes_estatus_pool_size_request += PAGE_ALIGN(len);
  188. size = gen_pool_size(ghes_estatus_pool);
  189. if (size >= ghes_estatus_pool_size_request)
  190. return 0;
  191. pages = (ghes_estatus_pool_size_request - size) / PAGE_SIZE;
  192. for (i = 0; i < pages; i++) {
  193. addr = __get_free_page(GFP_KERNEL);
  194. if (!addr)
  195. return -ENOMEM;
  196. ret = gen_pool_add(ghes_estatus_pool, addr, PAGE_SIZE, -1);
  197. if (ret)
  198. return ret;
  199. }
  200. return 0;
  201. }
  202. static struct ghes *ghes_new(struct acpi_hest_generic *generic)
  203. {
  204. struct ghes *ghes;
  205. unsigned int error_block_length;
  206. int rc;
  207. ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
  208. if (!ghes)
  209. return ERR_PTR(-ENOMEM);
  210. ghes->generic = generic;
  211. rc = apei_map_generic_address(&generic->error_status_address);
  212. if (rc)
  213. goto err_free;
  214. error_block_length = generic->error_block_length;
  215. if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
  216. pr_warning(FW_WARN GHES_PFX
  217. "Error status block length is too long: %u for "
  218. "generic hardware error source: %d.\n",
  219. error_block_length, generic->header.source_id);
  220. error_block_length = GHES_ESTATUS_MAX_SIZE;
  221. }
  222. ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
  223. if (!ghes->estatus) {
  224. rc = -ENOMEM;
  225. goto err_unmap;
  226. }
  227. return ghes;
  228. err_unmap:
  229. apei_unmap_generic_address(&generic->error_status_address);
  230. err_free:
  231. kfree(ghes);
  232. return ERR_PTR(rc);
  233. }
  234. static void ghes_fini(struct ghes *ghes)
  235. {
  236. kfree(ghes->estatus);
  237. apei_unmap_generic_address(&ghes->generic->error_status_address);
  238. }
  239. static inline int ghes_severity(int severity)
  240. {
  241. switch (severity) {
  242. case CPER_SEV_INFORMATIONAL:
  243. return GHES_SEV_NO;
  244. case CPER_SEV_CORRECTED:
  245. return GHES_SEV_CORRECTED;
  246. case CPER_SEV_RECOVERABLE:
  247. return GHES_SEV_RECOVERABLE;
  248. case CPER_SEV_FATAL:
  249. return GHES_SEV_PANIC;
  250. default:
  251. /* Unknown, go panic */
  252. return GHES_SEV_PANIC;
  253. }
  254. }
  255. static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
  256. int from_phys)
  257. {
  258. void __iomem *vaddr;
  259. unsigned long flags = 0;
  260. int in_nmi = in_nmi();
  261. u64 offset;
  262. u32 trunk;
  263. while (len > 0) {
  264. offset = paddr - (paddr & PAGE_MASK);
  265. if (in_nmi) {
  266. raw_spin_lock(&ghes_ioremap_lock_nmi);
  267. vaddr = ghes_ioremap_pfn_nmi(paddr >> PAGE_SHIFT);
  268. } else {
  269. spin_lock_irqsave(&ghes_ioremap_lock_irq, flags);
  270. vaddr = ghes_ioremap_pfn_irq(paddr >> PAGE_SHIFT);
  271. }
  272. trunk = PAGE_SIZE - offset;
  273. trunk = min(trunk, len);
  274. if (from_phys)
  275. memcpy_fromio(buffer, vaddr + offset, trunk);
  276. else
  277. memcpy_toio(vaddr + offset, buffer, trunk);
  278. len -= trunk;
  279. paddr += trunk;
  280. buffer += trunk;
  281. if (in_nmi) {
  282. ghes_iounmap_nmi(vaddr);
  283. raw_spin_unlock(&ghes_ioremap_lock_nmi);
  284. } else {
  285. ghes_iounmap_irq(vaddr);
  286. spin_unlock_irqrestore(&ghes_ioremap_lock_irq, flags);
  287. }
  288. }
  289. }
  290. static int ghes_read_estatus(struct ghes *ghes, int silent)
  291. {
  292. struct acpi_hest_generic *g = ghes->generic;
  293. u64 buf_paddr;
  294. u32 len;
  295. int rc;
  296. rc = apei_read(&buf_paddr, &g->error_status_address);
  297. if (rc) {
  298. if (!silent && printk_ratelimit())
  299. pr_warning(FW_WARN GHES_PFX
  300. "Failed to read error status block address for hardware error source: %d.\n",
  301. g->header.source_id);
  302. return -EIO;
  303. }
  304. if (!buf_paddr)
  305. return -ENOENT;
  306. ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
  307. sizeof(*ghes->estatus), 1);
  308. if (!ghes->estatus->block_status)
  309. return -ENOENT;
  310. ghes->buffer_paddr = buf_paddr;
  311. ghes->flags |= GHES_TO_CLEAR;
  312. rc = -EIO;
  313. len = cper_estatus_len(ghes->estatus);
  314. if (len < sizeof(*ghes->estatus))
  315. goto err_read_block;
  316. if (len > ghes->generic->error_block_length)
  317. goto err_read_block;
  318. if (cper_estatus_check_header(ghes->estatus))
  319. goto err_read_block;
  320. ghes_copy_tofrom_phys(ghes->estatus + 1,
  321. buf_paddr + sizeof(*ghes->estatus),
  322. len - sizeof(*ghes->estatus), 1);
  323. if (cper_estatus_check(ghes->estatus))
  324. goto err_read_block;
  325. rc = 0;
  326. err_read_block:
  327. if (rc && !silent && printk_ratelimit())
  328. pr_warning(FW_WARN GHES_PFX
  329. "Failed to read error status block!\n");
  330. return rc;
  331. }
  332. static void ghes_clear_estatus(struct ghes *ghes)
  333. {
  334. ghes->estatus->block_status = 0;
  335. if (!(ghes->flags & GHES_TO_CLEAR))
  336. return;
  337. ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
  338. sizeof(ghes->estatus->block_status), 0);
  339. ghes->flags &= ~GHES_TO_CLEAR;
  340. }
  341. static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
  342. {
  343. #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
  344. unsigned long pfn;
  345. int flags = -1;
  346. int sec_sev = ghes_severity(gdata->error_severity);
  347. struct cper_sec_mem_err *mem_err;
  348. mem_err = (struct cper_sec_mem_err *)(gdata + 1);
  349. if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
  350. return;
  351. pfn = mem_err->physical_addr >> PAGE_SHIFT;
  352. if (!pfn_valid(pfn)) {
  353. pr_warn_ratelimited(FW_WARN GHES_PFX
  354. "Invalid address in generic error data: %#llx\n",
  355. mem_err->physical_addr);
  356. return;
  357. }
  358. /* iff following two events can be handled properly by now */
  359. if (sec_sev == GHES_SEV_CORRECTED &&
  360. (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
  361. flags = MF_SOFT_OFFLINE;
  362. if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
  363. flags = 0;
  364. if (flags != -1)
  365. memory_failure_queue(pfn, 0, flags);
  366. #endif
  367. }
  368. static void ghes_do_proc(struct ghes *ghes,
  369. const struct acpi_hest_generic_status *estatus)
  370. {
  371. int sev, sec_sev;
  372. struct acpi_hest_generic_data *gdata;
  373. sev = ghes_severity(estatus->error_severity);
  374. apei_estatus_for_each_section(estatus, gdata) {
  375. sec_sev = ghes_severity(gdata->error_severity);
  376. if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  377. CPER_SEC_PLATFORM_MEM)) {
  378. struct cper_sec_mem_err *mem_err;
  379. mem_err = (struct cper_sec_mem_err *)(gdata+1);
  380. ghes_edac_report_mem_error(ghes, sev, mem_err);
  381. arch_apei_report_mem_error(sev, mem_err);
  382. ghes_handle_memory_failure(gdata, sev);
  383. }
  384. #ifdef CONFIG_ACPI_APEI_PCIEAER
  385. else if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  386. CPER_SEC_PCIE)) {
  387. struct cper_sec_pcie *pcie_err;
  388. pcie_err = (struct cper_sec_pcie *)(gdata+1);
  389. if (sev == GHES_SEV_RECOVERABLE &&
  390. sec_sev == GHES_SEV_RECOVERABLE &&
  391. pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
  392. pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
  393. unsigned int devfn;
  394. int aer_severity;
  395. devfn = PCI_DEVFN(pcie_err->device_id.device,
  396. pcie_err->device_id.function);
  397. aer_severity = cper_severity_to_aer(gdata->error_severity);
  398. /*
  399. * If firmware reset the component to contain
  400. * the error, we must reinitialize it before
  401. * use, so treat it as a fatal AER error.
  402. */
  403. if (gdata->flags & CPER_SEC_RESET)
  404. aer_severity = AER_FATAL;
  405. aer_recover_queue(pcie_err->device_id.segment,
  406. pcie_err->device_id.bus,
  407. devfn, aer_severity,
  408. (struct aer_capability_regs *)
  409. pcie_err->aer_info);
  410. }
  411. }
  412. #endif
  413. }
  414. }
  415. static void __ghes_print_estatus(const char *pfx,
  416. const struct acpi_hest_generic *generic,
  417. const struct acpi_hest_generic_status *estatus)
  418. {
  419. static atomic_t seqno;
  420. unsigned int curr_seqno;
  421. char pfx_seq[64];
  422. if (pfx == NULL) {
  423. if (ghes_severity(estatus->error_severity) <=
  424. GHES_SEV_CORRECTED)
  425. pfx = KERN_WARNING;
  426. else
  427. pfx = KERN_ERR;
  428. }
  429. curr_seqno = atomic_inc_return(&seqno);
  430. snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
  431. printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
  432. pfx_seq, generic->header.source_id);
  433. cper_estatus_print(pfx_seq, estatus);
  434. }
  435. static int ghes_print_estatus(const char *pfx,
  436. const struct acpi_hest_generic *generic,
  437. const struct acpi_hest_generic_status *estatus)
  438. {
  439. /* Not more than 2 messages every 5 seconds */
  440. static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
  441. static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
  442. struct ratelimit_state *ratelimit;
  443. if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
  444. ratelimit = &ratelimit_corrected;
  445. else
  446. ratelimit = &ratelimit_uncorrected;
  447. if (__ratelimit(ratelimit)) {
  448. __ghes_print_estatus(pfx, generic, estatus);
  449. return 1;
  450. }
  451. return 0;
  452. }
  453. /*
  454. * GHES error status reporting throttle, to report more kinds of
  455. * errors, instead of just most frequently occurred errors.
  456. */
  457. static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
  458. {
  459. u32 len;
  460. int i, cached = 0;
  461. unsigned long long now;
  462. struct ghes_estatus_cache *cache;
  463. struct acpi_hest_generic_status *cache_estatus;
  464. len = cper_estatus_len(estatus);
  465. rcu_read_lock();
  466. for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
  467. cache = rcu_dereference(ghes_estatus_caches[i]);
  468. if (cache == NULL)
  469. continue;
  470. if (len != cache->estatus_len)
  471. continue;
  472. cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
  473. if (memcmp(estatus, cache_estatus, len))
  474. continue;
  475. atomic_inc(&cache->count);
  476. now = sched_clock();
  477. if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
  478. cached = 1;
  479. break;
  480. }
  481. rcu_read_unlock();
  482. return cached;
  483. }
  484. static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
  485. struct acpi_hest_generic *generic,
  486. struct acpi_hest_generic_status *estatus)
  487. {
  488. int alloced;
  489. u32 len, cache_len;
  490. struct ghes_estatus_cache *cache;
  491. struct acpi_hest_generic_status *cache_estatus;
  492. alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
  493. if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
  494. atomic_dec(&ghes_estatus_cache_alloced);
  495. return NULL;
  496. }
  497. len = cper_estatus_len(estatus);
  498. cache_len = GHES_ESTATUS_CACHE_LEN(len);
  499. cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
  500. if (!cache) {
  501. atomic_dec(&ghes_estatus_cache_alloced);
  502. return NULL;
  503. }
  504. cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
  505. memcpy(cache_estatus, estatus, len);
  506. cache->estatus_len = len;
  507. atomic_set(&cache->count, 0);
  508. cache->generic = generic;
  509. cache->time_in = sched_clock();
  510. return cache;
  511. }
  512. static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
  513. {
  514. u32 len;
  515. len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
  516. len = GHES_ESTATUS_CACHE_LEN(len);
  517. gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
  518. atomic_dec(&ghes_estatus_cache_alloced);
  519. }
  520. static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
  521. {
  522. struct ghes_estatus_cache *cache;
  523. cache = container_of(head, struct ghes_estatus_cache, rcu);
  524. ghes_estatus_cache_free(cache);
  525. }
  526. static void ghes_estatus_cache_add(
  527. struct acpi_hest_generic *generic,
  528. struct acpi_hest_generic_status *estatus)
  529. {
  530. int i, slot = -1, count;
  531. unsigned long long now, duration, period, max_period = 0;
  532. struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
  533. new_cache = ghes_estatus_cache_alloc(generic, estatus);
  534. if (new_cache == NULL)
  535. return;
  536. rcu_read_lock();
  537. now = sched_clock();
  538. for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
  539. cache = rcu_dereference(ghes_estatus_caches[i]);
  540. if (cache == NULL) {
  541. slot = i;
  542. slot_cache = NULL;
  543. break;
  544. }
  545. duration = now - cache->time_in;
  546. if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
  547. slot = i;
  548. slot_cache = cache;
  549. break;
  550. }
  551. count = atomic_read(&cache->count);
  552. period = duration;
  553. do_div(period, (count + 1));
  554. if (period > max_period) {
  555. max_period = period;
  556. slot = i;
  557. slot_cache = cache;
  558. }
  559. }
  560. /* new_cache must be put into array after its contents are written */
  561. smp_wmb();
  562. if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
  563. slot_cache, new_cache) == slot_cache) {
  564. if (slot_cache)
  565. call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
  566. } else
  567. ghes_estatus_cache_free(new_cache);
  568. rcu_read_unlock();
  569. }
  570. static int ghes_proc(struct ghes *ghes)
  571. {
  572. int rc;
  573. rc = ghes_read_estatus(ghes, 0);
  574. if (rc)
  575. goto out;
  576. if (!ghes_estatus_cached(ghes->estatus)) {
  577. if (ghes_print_estatus(NULL, ghes->generic, ghes->estatus))
  578. ghes_estatus_cache_add(ghes->generic, ghes->estatus);
  579. }
  580. ghes_do_proc(ghes, ghes->estatus);
  581. out:
  582. ghes_clear_estatus(ghes);
  583. return rc;
  584. }
  585. static void ghes_add_timer(struct ghes *ghes)
  586. {
  587. struct acpi_hest_generic *g = ghes->generic;
  588. unsigned long expire;
  589. if (!g->notify.poll_interval) {
  590. pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
  591. g->header.source_id);
  592. return;
  593. }
  594. expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
  595. ghes->timer.expires = round_jiffies_relative(expire);
  596. add_timer(&ghes->timer);
  597. }
  598. static void ghes_poll_func(unsigned long data)
  599. {
  600. struct ghes *ghes = (void *)data;
  601. ghes_proc(ghes);
  602. if (!(ghes->flags & GHES_EXITING))
  603. ghes_add_timer(ghes);
  604. }
  605. static irqreturn_t ghes_irq_func(int irq, void *data)
  606. {
  607. struct ghes *ghes = data;
  608. int rc;
  609. rc = ghes_proc(ghes);
  610. if (rc)
  611. return IRQ_NONE;
  612. return IRQ_HANDLED;
  613. }
  614. static int ghes_notify_sci(struct notifier_block *this,
  615. unsigned long event, void *data)
  616. {
  617. struct ghes *ghes;
  618. int ret = NOTIFY_DONE;
  619. rcu_read_lock();
  620. list_for_each_entry_rcu(ghes, &ghes_sci, list) {
  621. if (!ghes_proc(ghes))
  622. ret = NOTIFY_OK;
  623. }
  624. rcu_read_unlock();
  625. return ret;
  626. }
  627. static struct notifier_block ghes_notifier_sci = {
  628. .notifier_call = ghes_notify_sci,
  629. };
  630. #ifdef CONFIG_HAVE_ACPI_APEI_NMI
  631. /*
  632. * printk is not safe in NMI context. So in NMI handler, we allocate
  633. * required memory from lock-less memory allocator
  634. * (ghes_estatus_pool), save estatus into it, put them into lock-less
  635. * list (ghes_estatus_llist), then delay printk into IRQ context via
  636. * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
  637. * required pool size by all NMI error source.
  638. */
  639. static struct llist_head ghes_estatus_llist;
  640. static struct irq_work ghes_proc_irq_work;
  641. /*
  642. * NMI may be triggered on any CPU, so ghes_in_nmi is used for
  643. * having only one concurrent reader.
  644. */
  645. static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
  646. static LIST_HEAD(ghes_nmi);
  647. static int ghes_panic_timeout __read_mostly = 30;
  648. static void ghes_proc_in_irq(struct irq_work *irq_work)
  649. {
  650. struct llist_node *llnode, *next;
  651. struct ghes_estatus_node *estatus_node;
  652. struct acpi_hest_generic *generic;
  653. struct acpi_hest_generic_status *estatus;
  654. u32 len, node_len;
  655. llnode = llist_del_all(&ghes_estatus_llist);
  656. /*
  657. * Because the time order of estatus in list is reversed,
  658. * revert it back to proper order.
  659. */
  660. llnode = llist_reverse_order(llnode);
  661. while (llnode) {
  662. next = llnode->next;
  663. estatus_node = llist_entry(llnode, struct ghes_estatus_node,
  664. llnode);
  665. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  666. len = cper_estatus_len(estatus);
  667. node_len = GHES_ESTATUS_NODE_LEN(len);
  668. ghes_do_proc(estatus_node->ghes, estatus);
  669. if (!ghes_estatus_cached(estatus)) {
  670. generic = estatus_node->generic;
  671. if (ghes_print_estatus(NULL, generic, estatus))
  672. ghes_estatus_cache_add(generic, estatus);
  673. }
  674. gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
  675. node_len);
  676. llnode = next;
  677. }
  678. }
  679. static void ghes_print_queued_estatus(void)
  680. {
  681. struct llist_node *llnode;
  682. struct ghes_estatus_node *estatus_node;
  683. struct acpi_hest_generic *generic;
  684. struct acpi_hest_generic_status *estatus;
  685. u32 len, node_len;
  686. llnode = llist_del_all(&ghes_estatus_llist);
  687. /*
  688. * Because the time order of estatus in list is reversed,
  689. * revert it back to proper order.
  690. */
  691. llnode = llist_reverse_order(llnode);
  692. while (llnode) {
  693. estatus_node = llist_entry(llnode, struct ghes_estatus_node,
  694. llnode);
  695. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  696. len = cper_estatus_len(estatus);
  697. node_len = GHES_ESTATUS_NODE_LEN(len);
  698. generic = estatus_node->generic;
  699. ghes_print_estatus(NULL, generic, estatus);
  700. llnode = llnode->next;
  701. }
  702. }
  703. /* Save estatus for further processing in IRQ context */
  704. static void __process_error(struct ghes *ghes)
  705. {
  706. #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  707. u32 len, node_len;
  708. struct ghes_estatus_node *estatus_node;
  709. struct acpi_hest_generic_status *estatus;
  710. if (ghes_estatus_cached(ghes->estatus))
  711. return;
  712. len = cper_estatus_len(ghes->estatus);
  713. node_len = GHES_ESTATUS_NODE_LEN(len);
  714. estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
  715. if (!estatus_node)
  716. return;
  717. estatus_node->ghes = ghes;
  718. estatus_node->generic = ghes->generic;
  719. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  720. memcpy(estatus, ghes->estatus, len);
  721. llist_add(&estatus_node->llnode, &ghes_estatus_llist);
  722. #endif
  723. }
  724. static void __ghes_panic(struct ghes *ghes)
  725. {
  726. oops_begin();
  727. ghes_print_queued_estatus();
  728. __ghes_print_estatus(KERN_EMERG, ghes->generic, ghes->estatus);
  729. /* reboot to log the error! */
  730. if (panic_timeout == 0)
  731. panic_timeout = ghes_panic_timeout;
  732. panic("Fatal hardware error!");
  733. }
  734. static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
  735. {
  736. struct ghes *ghes;
  737. int sev, ret = NMI_DONE;
  738. if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
  739. return ret;
  740. list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
  741. if (ghes_read_estatus(ghes, 1)) {
  742. ghes_clear_estatus(ghes);
  743. continue;
  744. } else {
  745. ret = NMI_HANDLED;
  746. }
  747. sev = ghes_severity(ghes->estatus->error_severity);
  748. if (sev >= GHES_SEV_PANIC)
  749. __ghes_panic(ghes);
  750. if (!(ghes->flags & GHES_TO_CLEAR))
  751. continue;
  752. __process_error(ghes);
  753. ghes_clear_estatus(ghes);
  754. }
  755. #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  756. if (ret == NMI_HANDLED)
  757. irq_work_queue(&ghes_proc_irq_work);
  758. #endif
  759. atomic_dec(&ghes_in_nmi);
  760. return ret;
  761. }
  762. static unsigned long ghes_esource_prealloc_size(
  763. const struct acpi_hest_generic *generic)
  764. {
  765. unsigned long block_length, prealloc_records, prealloc_size;
  766. block_length = min_t(unsigned long, generic->error_block_length,
  767. GHES_ESTATUS_MAX_SIZE);
  768. prealloc_records = max_t(unsigned long,
  769. generic->records_to_preallocate, 1);
  770. prealloc_size = min_t(unsigned long, block_length * prealloc_records,
  771. GHES_ESOURCE_PREALLOC_MAX_SIZE);
  772. return prealloc_size;
  773. }
  774. static void ghes_estatus_pool_shrink(unsigned long len)
  775. {
  776. ghes_estatus_pool_size_request -= PAGE_ALIGN(len);
  777. }
  778. static void ghes_nmi_add(struct ghes *ghes)
  779. {
  780. unsigned long len;
  781. len = ghes_esource_prealloc_size(ghes->generic);
  782. ghes_estatus_pool_expand(len);
  783. mutex_lock(&ghes_list_mutex);
  784. if (list_empty(&ghes_nmi))
  785. register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
  786. list_add_rcu(&ghes->list, &ghes_nmi);
  787. mutex_unlock(&ghes_list_mutex);
  788. }
  789. static void ghes_nmi_remove(struct ghes *ghes)
  790. {
  791. unsigned long len;
  792. mutex_lock(&ghes_list_mutex);
  793. list_del_rcu(&ghes->list);
  794. if (list_empty(&ghes_nmi))
  795. unregister_nmi_handler(NMI_LOCAL, "ghes");
  796. mutex_unlock(&ghes_list_mutex);
  797. /*
  798. * To synchronize with NMI handler, ghes can only be
  799. * freed after NMI handler finishes.
  800. */
  801. synchronize_rcu();
  802. len = ghes_esource_prealloc_size(ghes->generic);
  803. ghes_estatus_pool_shrink(len);
  804. }
  805. static void ghes_nmi_init_cxt(void)
  806. {
  807. init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
  808. }
  809. #else /* CONFIG_HAVE_ACPI_APEI_NMI */
  810. static inline void ghes_nmi_add(struct ghes *ghes)
  811. {
  812. pr_err(GHES_PFX "ID: %d, trying to add NMI notification which is not supported!\n",
  813. ghes->generic->header.source_id);
  814. BUG();
  815. }
  816. static inline void ghes_nmi_remove(struct ghes *ghes)
  817. {
  818. pr_err(GHES_PFX "ID: %d, trying to remove NMI notification which is not supported!\n",
  819. ghes->generic->header.source_id);
  820. BUG();
  821. }
  822. static inline void ghes_nmi_init_cxt(void)
  823. {
  824. }
  825. #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
  826. static int ghes_probe(struct platform_device *ghes_dev)
  827. {
  828. struct acpi_hest_generic *generic;
  829. struct ghes *ghes = NULL;
  830. int rc = -EINVAL;
  831. generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
  832. if (!generic->enabled)
  833. return -ENODEV;
  834. switch (generic->notify.type) {
  835. case ACPI_HEST_NOTIFY_POLLED:
  836. case ACPI_HEST_NOTIFY_EXTERNAL:
  837. case ACPI_HEST_NOTIFY_SCI:
  838. break;
  839. case ACPI_HEST_NOTIFY_NMI:
  840. if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
  841. pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
  842. generic->header.source_id);
  843. goto err;
  844. }
  845. break;
  846. case ACPI_HEST_NOTIFY_LOCAL:
  847. pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
  848. generic->header.source_id);
  849. goto err;
  850. default:
  851. pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
  852. generic->notify.type, generic->header.source_id);
  853. goto err;
  854. }
  855. rc = -EIO;
  856. if (generic->error_block_length <
  857. sizeof(struct acpi_hest_generic_status)) {
  858. pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
  859. generic->error_block_length,
  860. generic->header.source_id);
  861. goto err;
  862. }
  863. ghes = ghes_new(generic);
  864. if (IS_ERR(ghes)) {
  865. rc = PTR_ERR(ghes);
  866. ghes = NULL;
  867. goto err;
  868. }
  869. rc = ghes_edac_register(ghes, &ghes_dev->dev);
  870. if (rc < 0)
  871. goto err;
  872. switch (generic->notify.type) {
  873. case ACPI_HEST_NOTIFY_POLLED:
  874. ghes->timer.function = ghes_poll_func;
  875. ghes->timer.data = (unsigned long)ghes;
  876. init_timer_deferrable(&ghes->timer);
  877. ghes_add_timer(ghes);
  878. break;
  879. case ACPI_HEST_NOTIFY_EXTERNAL:
  880. /* External interrupt vector is GSI */
  881. rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
  882. if (rc) {
  883. pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
  884. generic->header.source_id);
  885. goto err_edac_unreg;
  886. }
  887. rc = request_irq(ghes->irq, ghes_irq_func, 0, "GHES IRQ", ghes);
  888. if (rc) {
  889. pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
  890. generic->header.source_id);
  891. goto err_edac_unreg;
  892. }
  893. break;
  894. case ACPI_HEST_NOTIFY_SCI:
  895. mutex_lock(&ghes_list_mutex);
  896. if (list_empty(&ghes_sci))
  897. register_acpi_hed_notifier(&ghes_notifier_sci);
  898. list_add_rcu(&ghes->list, &ghes_sci);
  899. mutex_unlock(&ghes_list_mutex);
  900. break;
  901. case ACPI_HEST_NOTIFY_NMI:
  902. ghes_nmi_add(ghes);
  903. break;
  904. default:
  905. BUG();
  906. }
  907. platform_set_drvdata(ghes_dev, ghes);
  908. return 0;
  909. err_edac_unreg:
  910. ghes_edac_unregister(ghes);
  911. err:
  912. if (ghes) {
  913. ghes_fini(ghes);
  914. kfree(ghes);
  915. }
  916. return rc;
  917. }
  918. static int ghes_remove(struct platform_device *ghes_dev)
  919. {
  920. struct ghes *ghes;
  921. struct acpi_hest_generic *generic;
  922. ghes = platform_get_drvdata(ghes_dev);
  923. generic = ghes->generic;
  924. ghes->flags |= GHES_EXITING;
  925. switch (generic->notify.type) {
  926. case ACPI_HEST_NOTIFY_POLLED:
  927. del_timer_sync(&ghes->timer);
  928. break;
  929. case ACPI_HEST_NOTIFY_EXTERNAL:
  930. free_irq(ghes->irq, ghes);
  931. break;
  932. case ACPI_HEST_NOTIFY_SCI:
  933. mutex_lock(&ghes_list_mutex);
  934. list_del_rcu(&ghes->list);
  935. if (list_empty(&ghes_sci))
  936. unregister_acpi_hed_notifier(&ghes_notifier_sci);
  937. mutex_unlock(&ghes_list_mutex);
  938. synchronize_rcu();
  939. break;
  940. case ACPI_HEST_NOTIFY_NMI:
  941. ghes_nmi_remove(ghes);
  942. break;
  943. default:
  944. BUG();
  945. break;
  946. }
  947. ghes_fini(ghes);
  948. ghes_edac_unregister(ghes);
  949. kfree(ghes);
  950. platform_set_drvdata(ghes_dev, NULL);
  951. return 0;
  952. }
  953. static struct platform_driver ghes_platform_driver = {
  954. .driver = {
  955. .name = "GHES",
  956. },
  957. .probe = ghes_probe,
  958. .remove = ghes_remove,
  959. };
  960. static int __init ghes_init(void)
  961. {
  962. int rc;
  963. if (acpi_disabled)
  964. return -ENODEV;
  965. if (hest_disable) {
  966. pr_info(GHES_PFX "HEST is not enabled!\n");
  967. return -EINVAL;
  968. }
  969. if (ghes_disable) {
  970. pr_info(GHES_PFX "GHES is not enabled!\n");
  971. return -EINVAL;
  972. }
  973. ghes_nmi_init_cxt();
  974. rc = ghes_ioremap_init();
  975. if (rc)
  976. goto err;
  977. rc = ghes_estatus_pool_init();
  978. if (rc)
  979. goto err_ioremap_exit;
  980. rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
  981. GHES_ESTATUS_CACHE_ALLOCED_MAX);
  982. if (rc)
  983. goto err_pool_exit;
  984. rc = platform_driver_register(&ghes_platform_driver);
  985. if (rc)
  986. goto err_pool_exit;
  987. rc = apei_osc_setup();
  988. if (rc == 0 && osc_sb_apei_support_acked)
  989. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
  990. else if (rc == 0 && !osc_sb_apei_support_acked)
  991. pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
  992. else if (rc && osc_sb_apei_support_acked)
  993. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
  994. else
  995. pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
  996. return 0;
  997. err_pool_exit:
  998. ghes_estatus_pool_exit();
  999. err_ioremap_exit:
  1000. ghes_ioremap_exit();
  1001. err:
  1002. return rc;
  1003. }
  1004. device_initcall(ghes_init);