ghes.c 28 KB

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