ghes.c 30 KB

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