ghes.c 30 KB

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