ghes.c 29 KB

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