ghes.c 30 KB

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