ghes.c 29 KB

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