ghes.c 29 KB

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