ghes.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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_do_proc(struct ghes *ghes,
  357. const struct acpi_hest_generic_status *estatus)
  358. {
  359. int sev, sec_sev;
  360. struct acpi_hest_generic_data *gdata;
  361. sev = ghes_severity(estatus->error_severity);
  362. apei_estatus_for_each_section(estatus, gdata) {
  363. sec_sev = ghes_severity(gdata->error_severity);
  364. if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  365. CPER_SEC_PLATFORM_MEM)) {
  366. struct cper_sec_mem_err *mem_err;
  367. mem_err = (struct cper_sec_mem_err *)(gdata+1);
  368. ghes_edac_report_mem_error(ghes, sev, mem_err);
  369. #ifdef CONFIG_X86_MCE
  370. apei_mce_report_mem_error(sev == GHES_SEV_CORRECTED,
  371. mem_err);
  372. #endif
  373. #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
  374. if (sev == GHES_SEV_RECOVERABLE &&
  375. sec_sev == GHES_SEV_RECOVERABLE &&
  376. mem_err->validation_bits & CPER_MEM_VALID_PHYSICAL_ADDRESS) {
  377. unsigned long pfn;
  378. pfn = mem_err->physical_addr >> PAGE_SHIFT;
  379. memory_failure_queue(pfn, 0, 0);
  380. }
  381. #endif
  382. }
  383. #ifdef CONFIG_ACPI_APEI_PCIEAER
  384. else if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  385. CPER_SEC_PCIE)) {
  386. struct cper_sec_pcie *pcie_err;
  387. pcie_err = (struct cper_sec_pcie *)(gdata+1);
  388. if (sev == GHES_SEV_RECOVERABLE &&
  389. sec_sev == GHES_SEV_RECOVERABLE &&
  390. pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
  391. pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
  392. unsigned int devfn;
  393. int aer_severity;
  394. devfn = PCI_DEVFN(pcie_err->device_id.device,
  395. pcie_err->device_id.function);
  396. aer_severity = cper_severity_to_aer(sev);
  397. aer_recover_queue(pcie_err->device_id.segment,
  398. pcie_err->device_id.bus,
  399. devfn, aer_severity,
  400. (struct aer_capability_regs *)
  401. pcie_err->aer_info);
  402. }
  403. }
  404. #endif
  405. }
  406. }
  407. static void __ghes_print_estatus(const char *pfx,
  408. const struct acpi_hest_generic *generic,
  409. const struct acpi_hest_generic_status *estatus)
  410. {
  411. static atomic_t seqno;
  412. unsigned int curr_seqno;
  413. char pfx_seq[64];
  414. if (pfx == NULL) {
  415. if (ghes_severity(estatus->error_severity) <=
  416. GHES_SEV_CORRECTED)
  417. pfx = KERN_WARNING;
  418. else
  419. pfx = KERN_ERR;
  420. }
  421. curr_seqno = atomic_inc_return(&seqno);
  422. snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
  423. printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
  424. pfx_seq, generic->header.source_id);
  425. apei_estatus_print(pfx_seq, estatus);
  426. }
  427. static int ghes_print_estatus(const char *pfx,
  428. const struct acpi_hest_generic *generic,
  429. const struct acpi_hest_generic_status *estatus)
  430. {
  431. /* Not more than 2 messages every 5 seconds */
  432. static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
  433. static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
  434. struct ratelimit_state *ratelimit;
  435. if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
  436. ratelimit = &ratelimit_corrected;
  437. else
  438. ratelimit = &ratelimit_uncorrected;
  439. if (__ratelimit(ratelimit)) {
  440. __ghes_print_estatus(pfx, generic, estatus);
  441. return 1;
  442. }
  443. return 0;
  444. }
  445. /*
  446. * GHES error status reporting throttle, to report more kinds of
  447. * errors, instead of just most frequently occurred errors.
  448. */
  449. static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
  450. {
  451. u32 len;
  452. int i, cached = 0;
  453. unsigned long long now;
  454. struct ghes_estatus_cache *cache;
  455. struct acpi_hest_generic_status *cache_estatus;
  456. len = apei_estatus_len(estatus);
  457. rcu_read_lock();
  458. for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
  459. cache = rcu_dereference(ghes_estatus_caches[i]);
  460. if (cache == NULL)
  461. continue;
  462. if (len != cache->estatus_len)
  463. continue;
  464. cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
  465. if (memcmp(estatus, cache_estatus, len))
  466. continue;
  467. atomic_inc(&cache->count);
  468. now = sched_clock();
  469. if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
  470. cached = 1;
  471. break;
  472. }
  473. rcu_read_unlock();
  474. return cached;
  475. }
  476. static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
  477. struct acpi_hest_generic *generic,
  478. struct acpi_hest_generic_status *estatus)
  479. {
  480. int alloced;
  481. u32 len, cache_len;
  482. struct ghes_estatus_cache *cache;
  483. struct acpi_hest_generic_status *cache_estatus;
  484. alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
  485. if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
  486. atomic_dec(&ghes_estatus_cache_alloced);
  487. return NULL;
  488. }
  489. len = apei_estatus_len(estatus);
  490. cache_len = GHES_ESTATUS_CACHE_LEN(len);
  491. cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
  492. if (!cache) {
  493. atomic_dec(&ghes_estatus_cache_alloced);
  494. return NULL;
  495. }
  496. cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
  497. memcpy(cache_estatus, estatus, len);
  498. cache->estatus_len = len;
  499. atomic_set(&cache->count, 0);
  500. cache->generic = generic;
  501. cache->time_in = sched_clock();
  502. return cache;
  503. }
  504. static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
  505. {
  506. u32 len;
  507. len = apei_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
  508. len = GHES_ESTATUS_CACHE_LEN(len);
  509. gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
  510. atomic_dec(&ghes_estatus_cache_alloced);
  511. }
  512. static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
  513. {
  514. struct ghes_estatus_cache *cache;
  515. cache = container_of(head, struct ghes_estatus_cache, rcu);
  516. ghes_estatus_cache_free(cache);
  517. }
  518. static void ghes_estatus_cache_add(
  519. struct acpi_hest_generic *generic,
  520. struct acpi_hest_generic_status *estatus)
  521. {
  522. int i, slot = -1, count;
  523. unsigned long long now, duration, period, max_period = 0;
  524. struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
  525. new_cache = ghes_estatus_cache_alloc(generic, estatus);
  526. if (new_cache == NULL)
  527. return;
  528. rcu_read_lock();
  529. now = sched_clock();
  530. for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
  531. cache = rcu_dereference(ghes_estatus_caches[i]);
  532. if (cache == NULL) {
  533. slot = i;
  534. slot_cache = NULL;
  535. break;
  536. }
  537. duration = now - cache->time_in;
  538. if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
  539. slot = i;
  540. slot_cache = cache;
  541. break;
  542. }
  543. count = atomic_read(&cache->count);
  544. period = duration;
  545. do_div(period, (count + 1));
  546. if (period > max_period) {
  547. max_period = period;
  548. slot = i;
  549. slot_cache = cache;
  550. }
  551. }
  552. /* new_cache must be put into array after its contents are written */
  553. smp_wmb();
  554. if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
  555. slot_cache, new_cache) == slot_cache) {
  556. if (slot_cache)
  557. call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
  558. } else
  559. ghes_estatus_cache_free(new_cache);
  560. rcu_read_unlock();
  561. }
  562. static int ghes_proc(struct ghes *ghes)
  563. {
  564. int rc;
  565. rc = ghes_read_estatus(ghes, 0);
  566. if (rc)
  567. goto out;
  568. if (!ghes_estatus_cached(ghes->estatus)) {
  569. if (ghes_print_estatus(NULL, ghes->generic, ghes->estatus))
  570. ghes_estatus_cache_add(ghes->generic, ghes->estatus);
  571. }
  572. ghes_do_proc(ghes, ghes->estatus);
  573. out:
  574. ghes_clear_estatus(ghes);
  575. return 0;
  576. }
  577. static void ghes_add_timer(struct ghes *ghes)
  578. {
  579. struct acpi_hest_generic *g = ghes->generic;
  580. unsigned long expire;
  581. if (!g->notify.poll_interval) {
  582. pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
  583. g->header.source_id);
  584. return;
  585. }
  586. expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
  587. ghes->timer.expires = round_jiffies_relative(expire);
  588. add_timer(&ghes->timer);
  589. }
  590. static void ghes_poll_func(unsigned long data)
  591. {
  592. struct ghes *ghes = (void *)data;
  593. ghes_proc(ghes);
  594. if (!(ghes->flags & GHES_EXITING))
  595. ghes_add_timer(ghes);
  596. }
  597. static irqreturn_t ghes_irq_func(int irq, void *data)
  598. {
  599. struct ghes *ghes = data;
  600. int rc;
  601. rc = ghes_proc(ghes);
  602. if (rc)
  603. return IRQ_NONE;
  604. return IRQ_HANDLED;
  605. }
  606. static int ghes_notify_sci(struct notifier_block *this,
  607. unsigned long event, void *data)
  608. {
  609. struct ghes *ghes;
  610. int ret = NOTIFY_DONE;
  611. rcu_read_lock();
  612. list_for_each_entry_rcu(ghes, &ghes_sci, list) {
  613. if (!ghes_proc(ghes))
  614. ret = NOTIFY_OK;
  615. }
  616. rcu_read_unlock();
  617. return ret;
  618. }
  619. static struct llist_node *llist_nodes_reverse(struct llist_node *llnode)
  620. {
  621. struct llist_node *next, *tail = NULL;
  622. while (llnode) {
  623. next = llnode->next;
  624. llnode->next = tail;
  625. tail = llnode;
  626. llnode = next;
  627. }
  628. return tail;
  629. }
  630. static void ghes_proc_in_irq(struct irq_work *irq_work)
  631. {
  632. struct llist_node *llnode, *next;
  633. struct ghes_estatus_node *estatus_node;
  634. struct acpi_hest_generic *generic;
  635. struct acpi_hest_generic_status *estatus;
  636. u32 len, node_len;
  637. llnode = llist_del_all(&ghes_estatus_llist);
  638. /*
  639. * Because the time order of estatus in list is reversed,
  640. * revert it back to proper order.
  641. */
  642. llnode = llist_nodes_reverse(llnode);
  643. while (llnode) {
  644. next = llnode->next;
  645. estatus_node = llist_entry(llnode, struct ghes_estatus_node,
  646. llnode);
  647. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  648. len = apei_estatus_len(estatus);
  649. node_len = GHES_ESTATUS_NODE_LEN(len);
  650. ghes_do_proc(estatus_node->ghes, estatus);
  651. if (!ghes_estatus_cached(estatus)) {
  652. generic = estatus_node->generic;
  653. if (ghes_print_estatus(NULL, generic, estatus))
  654. ghes_estatus_cache_add(generic, estatus);
  655. }
  656. gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
  657. node_len);
  658. llnode = next;
  659. }
  660. }
  661. static void ghes_print_queued_estatus(void)
  662. {
  663. struct llist_node *llnode;
  664. struct ghes_estatus_node *estatus_node;
  665. struct acpi_hest_generic *generic;
  666. struct acpi_hest_generic_status *estatus;
  667. u32 len, node_len;
  668. llnode = llist_del_all(&ghes_estatus_llist);
  669. /*
  670. * Because the time order of estatus in list is reversed,
  671. * revert it back to proper order.
  672. */
  673. llnode = llist_nodes_reverse(llnode);
  674. while (llnode) {
  675. estatus_node = llist_entry(llnode, struct ghes_estatus_node,
  676. llnode);
  677. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  678. len = apei_estatus_len(estatus);
  679. node_len = GHES_ESTATUS_NODE_LEN(len);
  680. generic = estatus_node->generic;
  681. ghes_print_estatus(NULL, generic, estatus);
  682. llnode = llnode->next;
  683. }
  684. }
  685. static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
  686. {
  687. struct ghes *ghes, *ghes_global = NULL;
  688. int sev, sev_global = -1;
  689. int ret = NMI_DONE;
  690. raw_spin_lock(&ghes_nmi_lock);
  691. list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
  692. if (ghes_read_estatus(ghes, 1)) {
  693. ghes_clear_estatus(ghes);
  694. continue;
  695. }
  696. sev = ghes_severity(ghes->estatus->error_severity);
  697. if (sev > sev_global) {
  698. sev_global = sev;
  699. ghes_global = ghes;
  700. }
  701. ret = NMI_HANDLED;
  702. }
  703. if (ret == NMI_DONE)
  704. goto out;
  705. if (sev_global >= GHES_SEV_PANIC) {
  706. oops_begin();
  707. ghes_print_queued_estatus();
  708. __ghes_print_estatus(KERN_EMERG, ghes_global->generic,
  709. ghes_global->estatus);
  710. /* reboot to log the error! */
  711. if (panic_timeout == 0)
  712. panic_timeout = ghes_panic_timeout;
  713. panic("Fatal hardware error!");
  714. }
  715. list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
  716. #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  717. u32 len, node_len;
  718. struct ghes_estatus_node *estatus_node;
  719. struct acpi_hest_generic_status *estatus;
  720. #endif
  721. if (!(ghes->flags & GHES_TO_CLEAR))
  722. continue;
  723. #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  724. if (ghes_estatus_cached(ghes->estatus))
  725. goto next;
  726. /* Save estatus for further processing in IRQ context */
  727. len = apei_estatus_len(ghes->estatus);
  728. node_len = GHES_ESTATUS_NODE_LEN(len);
  729. estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool,
  730. node_len);
  731. if (estatus_node) {
  732. estatus_node->ghes = ghes;
  733. estatus_node->generic = ghes->generic;
  734. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  735. memcpy(estatus, ghes->estatus, len);
  736. llist_add(&estatus_node->llnode, &ghes_estatus_llist);
  737. }
  738. next:
  739. #endif
  740. ghes_clear_estatus(ghes);
  741. }
  742. #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  743. irq_work_queue(&ghes_proc_irq_work);
  744. #endif
  745. out:
  746. raw_spin_unlock(&ghes_nmi_lock);
  747. return ret;
  748. }
  749. static struct notifier_block ghes_notifier_sci = {
  750. .notifier_call = ghes_notify_sci,
  751. };
  752. static unsigned long ghes_esource_prealloc_size(
  753. const struct acpi_hest_generic *generic)
  754. {
  755. unsigned long block_length, prealloc_records, prealloc_size;
  756. block_length = min_t(unsigned long, generic->error_block_length,
  757. GHES_ESTATUS_MAX_SIZE);
  758. prealloc_records = max_t(unsigned long,
  759. generic->records_to_preallocate, 1);
  760. prealloc_size = min_t(unsigned long, block_length * prealloc_records,
  761. GHES_ESOURCE_PREALLOC_MAX_SIZE);
  762. return prealloc_size;
  763. }
  764. static int ghes_probe(struct platform_device *ghes_dev)
  765. {
  766. struct acpi_hest_generic *generic;
  767. struct ghes *ghes = NULL;
  768. unsigned long len;
  769. int rc = -EINVAL;
  770. generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
  771. if (!generic->enabled)
  772. return -ENODEV;
  773. switch (generic->notify.type) {
  774. case ACPI_HEST_NOTIFY_POLLED:
  775. case ACPI_HEST_NOTIFY_EXTERNAL:
  776. case ACPI_HEST_NOTIFY_SCI:
  777. case ACPI_HEST_NOTIFY_NMI:
  778. break;
  779. case ACPI_HEST_NOTIFY_LOCAL:
  780. pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
  781. generic->header.source_id);
  782. goto err;
  783. default:
  784. pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
  785. generic->notify.type, generic->header.source_id);
  786. goto err;
  787. }
  788. rc = -EIO;
  789. if (generic->error_block_length <
  790. sizeof(struct acpi_hest_generic_status)) {
  791. pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
  792. generic->error_block_length,
  793. generic->header.source_id);
  794. goto err;
  795. }
  796. ghes = ghes_new(generic);
  797. if (IS_ERR(ghes)) {
  798. rc = PTR_ERR(ghes);
  799. ghes = NULL;
  800. goto err;
  801. }
  802. rc = ghes_edac_register(ghes, &ghes_dev->dev);
  803. if (rc < 0)
  804. goto err;
  805. switch (generic->notify.type) {
  806. case ACPI_HEST_NOTIFY_POLLED:
  807. ghes->timer.function = ghes_poll_func;
  808. ghes->timer.data = (unsigned long)ghes;
  809. init_timer_deferrable(&ghes->timer);
  810. ghes_add_timer(ghes);
  811. break;
  812. case ACPI_HEST_NOTIFY_EXTERNAL:
  813. /* External interrupt vector is GSI */
  814. rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
  815. if (rc) {
  816. pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
  817. generic->header.source_id);
  818. goto err_edac_unreg;
  819. }
  820. rc = request_irq(ghes->irq, ghes_irq_func, 0, "GHES IRQ", ghes);
  821. if (rc) {
  822. pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
  823. generic->header.source_id);
  824. goto err_edac_unreg;
  825. }
  826. break;
  827. case ACPI_HEST_NOTIFY_SCI:
  828. mutex_lock(&ghes_list_mutex);
  829. if (list_empty(&ghes_sci))
  830. register_acpi_hed_notifier(&ghes_notifier_sci);
  831. list_add_rcu(&ghes->list, &ghes_sci);
  832. mutex_unlock(&ghes_list_mutex);
  833. break;
  834. case ACPI_HEST_NOTIFY_NMI:
  835. len = ghes_esource_prealloc_size(generic);
  836. ghes_estatus_pool_expand(len);
  837. mutex_lock(&ghes_list_mutex);
  838. if (list_empty(&ghes_nmi))
  839. register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0,
  840. "ghes");
  841. list_add_rcu(&ghes->list, &ghes_nmi);
  842. mutex_unlock(&ghes_list_mutex);
  843. break;
  844. default:
  845. BUG();
  846. }
  847. platform_set_drvdata(ghes_dev, ghes);
  848. return 0;
  849. err_edac_unreg:
  850. ghes_edac_unregister(ghes);
  851. err:
  852. if (ghes) {
  853. ghes_fini(ghes);
  854. kfree(ghes);
  855. }
  856. return rc;
  857. }
  858. static int ghes_remove(struct platform_device *ghes_dev)
  859. {
  860. struct ghes *ghes;
  861. struct acpi_hest_generic *generic;
  862. unsigned long len;
  863. ghes = platform_get_drvdata(ghes_dev);
  864. generic = ghes->generic;
  865. ghes->flags |= GHES_EXITING;
  866. switch (generic->notify.type) {
  867. case ACPI_HEST_NOTIFY_POLLED:
  868. del_timer_sync(&ghes->timer);
  869. break;
  870. case ACPI_HEST_NOTIFY_EXTERNAL:
  871. free_irq(ghes->irq, ghes);
  872. break;
  873. case ACPI_HEST_NOTIFY_SCI:
  874. mutex_lock(&ghes_list_mutex);
  875. list_del_rcu(&ghes->list);
  876. if (list_empty(&ghes_sci))
  877. unregister_acpi_hed_notifier(&ghes_notifier_sci);
  878. mutex_unlock(&ghes_list_mutex);
  879. break;
  880. case ACPI_HEST_NOTIFY_NMI:
  881. mutex_lock(&ghes_list_mutex);
  882. list_del_rcu(&ghes->list);
  883. if (list_empty(&ghes_nmi))
  884. unregister_nmi_handler(NMI_LOCAL, "ghes");
  885. mutex_unlock(&ghes_list_mutex);
  886. /*
  887. * To synchronize with NMI handler, ghes can only be
  888. * freed after NMI handler finishes.
  889. */
  890. synchronize_rcu();
  891. len = ghes_esource_prealloc_size(generic);
  892. ghes_estatus_pool_shrink(len);
  893. break;
  894. default:
  895. BUG();
  896. break;
  897. }
  898. ghes_fini(ghes);
  899. ghes_edac_unregister(ghes);
  900. kfree(ghes);
  901. platform_set_drvdata(ghes_dev, NULL);
  902. return 0;
  903. }
  904. static struct platform_driver ghes_platform_driver = {
  905. .driver = {
  906. .name = "GHES",
  907. .owner = THIS_MODULE,
  908. },
  909. .probe = ghes_probe,
  910. .remove = ghes_remove,
  911. };
  912. static int __init ghes_init(void)
  913. {
  914. int rc;
  915. if (acpi_disabled)
  916. return -ENODEV;
  917. if (hest_disable) {
  918. pr_info(GHES_PFX "HEST is not enabled!\n");
  919. return -EINVAL;
  920. }
  921. if (ghes_disable) {
  922. pr_info(GHES_PFX "GHES is not enabled!\n");
  923. return -EINVAL;
  924. }
  925. init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
  926. rc = ghes_ioremap_init();
  927. if (rc)
  928. goto err;
  929. rc = ghes_estatus_pool_init();
  930. if (rc)
  931. goto err_ioremap_exit;
  932. rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
  933. GHES_ESTATUS_CACHE_ALLOCED_MAX);
  934. if (rc)
  935. goto err_pool_exit;
  936. rc = platform_driver_register(&ghes_platform_driver);
  937. if (rc)
  938. goto err_pool_exit;
  939. rc = apei_osc_setup();
  940. if (rc == 0 && osc_sb_apei_support_acked)
  941. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
  942. else if (rc == 0 && !osc_sb_apei_support_acked)
  943. pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
  944. else if (rc && osc_sb_apei_support_acked)
  945. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
  946. else
  947. pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
  948. return 0;
  949. err_pool_exit:
  950. ghes_estatus_pool_exit();
  951. err_ioremap_exit:
  952. ghes_ioremap_exit();
  953. err:
  954. return rc;
  955. }
  956. static void __exit ghes_exit(void)
  957. {
  958. platform_driver_unregister(&ghes_platform_driver);
  959. ghes_estatus_pool_exit();
  960. ghes_ioremap_exit();
  961. }
  962. module_init(ghes_init);
  963. module_exit(ghes_exit);
  964. MODULE_AUTHOR("Huang Ying");
  965. MODULE_DESCRIPTION("APEI Generic Hardware Error Source support");
  966. MODULE_LICENSE("GPL");
  967. MODULE_ALIAS("platform:GHES");