ghes.c 29 KB

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