ghes.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. if (acpi_gsi_to_irq(generic->notify.vector, &ghes->irq)) {
  815. pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
  816. generic->header.source_id);
  817. goto err_edac_unreg;
  818. }
  819. if (request_irq(ghes->irq, ghes_irq_func,
  820. 0, "GHES IRQ", ghes)) {
  821. pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
  822. generic->header.source_id);
  823. goto err_edac_unreg;
  824. }
  825. break;
  826. case ACPI_HEST_NOTIFY_SCI:
  827. mutex_lock(&ghes_list_mutex);
  828. if (list_empty(&ghes_sci))
  829. register_acpi_hed_notifier(&ghes_notifier_sci);
  830. list_add_rcu(&ghes->list, &ghes_sci);
  831. mutex_unlock(&ghes_list_mutex);
  832. break;
  833. case ACPI_HEST_NOTIFY_NMI:
  834. len = ghes_esource_prealloc_size(generic);
  835. ghes_estatus_pool_expand(len);
  836. mutex_lock(&ghes_list_mutex);
  837. if (list_empty(&ghes_nmi))
  838. register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0,
  839. "ghes");
  840. list_add_rcu(&ghes->list, &ghes_nmi);
  841. mutex_unlock(&ghes_list_mutex);
  842. break;
  843. default:
  844. BUG();
  845. }
  846. platform_set_drvdata(ghes_dev, ghes);
  847. return 0;
  848. err_edac_unreg:
  849. ghes_edac_unregister(ghes);
  850. err:
  851. if (ghes) {
  852. ghes_fini(ghes);
  853. kfree(ghes);
  854. }
  855. return rc;
  856. }
  857. static int ghes_remove(struct platform_device *ghes_dev)
  858. {
  859. struct ghes *ghes;
  860. struct acpi_hest_generic *generic;
  861. unsigned long len;
  862. ghes = platform_get_drvdata(ghes_dev);
  863. generic = ghes->generic;
  864. ghes->flags |= GHES_EXITING;
  865. switch (generic->notify.type) {
  866. case ACPI_HEST_NOTIFY_POLLED:
  867. del_timer_sync(&ghes->timer);
  868. break;
  869. case ACPI_HEST_NOTIFY_EXTERNAL:
  870. free_irq(ghes->irq, ghes);
  871. break;
  872. case ACPI_HEST_NOTIFY_SCI:
  873. mutex_lock(&ghes_list_mutex);
  874. list_del_rcu(&ghes->list);
  875. if (list_empty(&ghes_sci))
  876. unregister_acpi_hed_notifier(&ghes_notifier_sci);
  877. mutex_unlock(&ghes_list_mutex);
  878. break;
  879. case ACPI_HEST_NOTIFY_NMI:
  880. mutex_lock(&ghes_list_mutex);
  881. list_del_rcu(&ghes->list);
  882. if (list_empty(&ghes_nmi))
  883. unregister_nmi_handler(NMI_LOCAL, "ghes");
  884. mutex_unlock(&ghes_list_mutex);
  885. /*
  886. * To synchronize with NMI handler, ghes can only be
  887. * freed after NMI handler finishes.
  888. */
  889. synchronize_rcu();
  890. len = ghes_esource_prealloc_size(generic);
  891. ghes_estatus_pool_shrink(len);
  892. break;
  893. default:
  894. BUG();
  895. break;
  896. }
  897. ghes_fini(ghes);
  898. ghes_edac_unregister(ghes);
  899. kfree(ghes);
  900. platform_set_drvdata(ghes_dev, NULL);
  901. return 0;
  902. }
  903. static struct platform_driver ghes_platform_driver = {
  904. .driver = {
  905. .name = "GHES",
  906. .owner = THIS_MODULE,
  907. },
  908. .probe = ghes_probe,
  909. .remove = ghes_remove,
  910. };
  911. static int __init ghes_init(void)
  912. {
  913. int rc;
  914. if (acpi_disabled)
  915. return -ENODEV;
  916. if (hest_disable) {
  917. pr_info(GHES_PFX "HEST is not enabled!\n");
  918. return -EINVAL;
  919. }
  920. if (ghes_disable) {
  921. pr_info(GHES_PFX "GHES is not enabled!\n");
  922. return -EINVAL;
  923. }
  924. init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
  925. rc = ghes_ioremap_init();
  926. if (rc)
  927. goto err;
  928. rc = ghes_estatus_pool_init();
  929. if (rc)
  930. goto err_ioremap_exit;
  931. rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
  932. GHES_ESTATUS_CACHE_ALLOCED_MAX);
  933. if (rc)
  934. goto err_pool_exit;
  935. rc = platform_driver_register(&ghes_platform_driver);
  936. if (rc)
  937. goto err_pool_exit;
  938. rc = apei_osc_setup();
  939. if (rc == 0 && osc_sb_apei_support_acked)
  940. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
  941. else if (rc == 0 && !osc_sb_apei_support_acked)
  942. pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
  943. else if (rc && osc_sb_apei_support_acked)
  944. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
  945. else
  946. pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
  947. return 0;
  948. err_pool_exit:
  949. ghes_estatus_pool_exit();
  950. err_ioremap_exit:
  951. ghes_ioremap_exit();
  952. err:
  953. return rc;
  954. }
  955. static void __exit ghes_exit(void)
  956. {
  957. platform_driver_unregister(&ghes_platform_driver);
  958. ghes_estatus_pool_exit();
  959. ghes_ioremap_exit();
  960. }
  961. module_init(ghes_init);
  962. module_exit(ghes_exit);
  963. MODULE_AUTHOR("Huang Ying");
  964. MODULE_DESCRIPTION("APEI Generic Hardware Error Source support");
  965. MODULE_LICENSE("GPL");
  966. MODULE_ALIAS("platform:GHES");