ghes.c 30 KB

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