ghes.c 29 KB

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