ghes.c 30 KB

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