ghes.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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;
  140. vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
  141. ioremap_page_range(vaddr, vaddr + PAGE_SIZE,
  142. pfn << PAGE_SHIFT, PAGE_KERNEL);
  143. return (void __iomem *)vaddr;
  144. }
  145. static void ghes_iounmap_nmi(void __iomem *vaddr_ptr)
  146. {
  147. unsigned long vaddr = (unsigned long __force)vaddr_ptr;
  148. void *base = ghes_ioremap_area->addr;
  149. BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_NMI_PAGE(base));
  150. unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
  151. arch_apei_flush_tlb_one(vaddr);
  152. }
  153. static void ghes_iounmap_irq(void __iomem *vaddr_ptr)
  154. {
  155. unsigned long vaddr = (unsigned long __force)vaddr_ptr;
  156. void *base = ghes_ioremap_area->addr;
  157. BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_IRQ_PAGE(base));
  158. unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
  159. arch_apei_flush_tlb_one(vaddr);
  160. }
  161. static int ghes_estatus_pool_init(void)
  162. {
  163. ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
  164. if (!ghes_estatus_pool)
  165. return -ENOMEM;
  166. return 0;
  167. }
  168. static void ghes_estatus_pool_free_chunk_page(struct gen_pool *pool,
  169. struct gen_pool_chunk *chunk,
  170. void *data)
  171. {
  172. free_page(chunk->start_addr);
  173. }
  174. static void ghes_estatus_pool_exit(void)
  175. {
  176. gen_pool_for_each_chunk(ghes_estatus_pool,
  177. ghes_estatus_pool_free_chunk_page, NULL);
  178. gen_pool_destroy(ghes_estatus_pool);
  179. }
  180. static int ghes_estatus_pool_expand(unsigned long len)
  181. {
  182. unsigned long i, pages, size, addr;
  183. int ret;
  184. ghes_estatus_pool_size_request += PAGE_ALIGN(len);
  185. size = gen_pool_size(ghes_estatus_pool);
  186. if (size >= ghes_estatus_pool_size_request)
  187. return 0;
  188. pages = (ghes_estatus_pool_size_request - size) / PAGE_SIZE;
  189. for (i = 0; i < pages; i++) {
  190. addr = __get_free_page(GFP_KERNEL);
  191. if (!addr)
  192. return -ENOMEM;
  193. ret = gen_pool_add(ghes_estatus_pool, addr, PAGE_SIZE, -1);
  194. if (ret)
  195. return ret;
  196. }
  197. return 0;
  198. }
  199. static struct ghes *ghes_new(struct acpi_hest_generic *generic)
  200. {
  201. struct ghes *ghes;
  202. unsigned int error_block_length;
  203. int rc;
  204. ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
  205. if (!ghes)
  206. return ERR_PTR(-ENOMEM);
  207. ghes->generic = generic;
  208. rc = apei_map_generic_address(&generic->error_status_address);
  209. if (rc)
  210. goto err_free;
  211. error_block_length = generic->error_block_length;
  212. if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
  213. pr_warning(FW_WARN GHES_PFX
  214. "Error status block length is too long: %u for "
  215. "generic hardware error source: %d.\n",
  216. error_block_length, generic->header.source_id);
  217. error_block_length = GHES_ESTATUS_MAX_SIZE;
  218. }
  219. ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
  220. if (!ghes->estatus) {
  221. rc = -ENOMEM;
  222. goto err_unmap;
  223. }
  224. return ghes;
  225. err_unmap:
  226. apei_unmap_generic_address(&generic->error_status_address);
  227. err_free:
  228. kfree(ghes);
  229. return ERR_PTR(rc);
  230. }
  231. static void ghes_fini(struct ghes *ghes)
  232. {
  233. kfree(ghes->estatus);
  234. apei_unmap_generic_address(&ghes->generic->error_status_address);
  235. }
  236. static inline int ghes_severity(int severity)
  237. {
  238. switch (severity) {
  239. case CPER_SEV_INFORMATIONAL:
  240. return GHES_SEV_NO;
  241. case CPER_SEV_CORRECTED:
  242. return GHES_SEV_CORRECTED;
  243. case CPER_SEV_RECOVERABLE:
  244. return GHES_SEV_RECOVERABLE;
  245. case CPER_SEV_FATAL:
  246. return GHES_SEV_PANIC;
  247. default:
  248. /* Unknown, go panic */
  249. return GHES_SEV_PANIC;
  250. }
  251. }
  252. static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
  253. int from_phys)
  254. {
  255. void __iomem *vaddr;
  256. unsigned long flags = 0;
  257. int in_nmi = in_nmi();
  258. u64 offset;
  259. u32 trunk;
  260. while (len > 0) {
  261. offset = paddr - (paddr & PAGE_MASK);
  262. if (in_nmi) {
  263. raw_spin_lock(&ghes_ioremap_lock_nmi);
  264. vaddr = ghes_ioremap_pfn_nmi(paddr >> PAGE_SHIFT);
  265. } else {
  266. spin_lock_irqsave(&ghes_ioremap_lock_irq, flags);
  267. vaddr = ghes_ioremap_pfn_irq(paddr >> PAGE_SHIFT);
  268. }
  269. trunk = PAGE_SIZE - offset;
  270. trunk = min(trunk, len);
  271. if (from_phys)
  272. memcpy_fromio(buffer, vaddr + offset, trunk);
  273. else
  274. memcpy_toio(vaddr + offset, buffer, trunk);
  275. len -= trunk;
  276. paddr += trunk;
  277. buffer += trunk;
  278. if (in_nmi) {
  279. ghes_iounmap_nmi(vaddr);
  280. raw_spin_unlock(&ghes_ioremap_lock_nmi);
  281. } else {
  282. ghes_iounmap_irq(vaddr);
  283. spin_unlock_irqrestore(&ghes_ioremap_lock_irq, flags);
  284. }
  285. }
  286. }
  287. static int ghes_read_estatus(struct ghes *ghes, int silent)
  288. {
  289. struct acpi_hest_generic *g = ghes->generic;
  290. u64 buf_paddr;
  291. u32 len;
  292. int rc;
  293. rc = apei_read(&buf_paddr, &g->error_status_address);
  294. if (rc) {
  295. if (!silent && printk_ratelimit())
  296. pr_warning(FW_WARN GHES_PFX
  297. "Failed to read error status block address for hardware error source: %d.\n",
  298. g->header.source_id);
  299. return -EIO;
  300. }
  301. if (!buf_paddr)
  302. return -ENOENT;
  303. ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
  304. sizeof(*ghes->estatus), 1);
  305. if (!ghes->estatus->block_status)
  306. return -ENOENT;
  307. ghes->buffer_paddr = buf_paddr;
  308. ghes->flags |= GHES_TO_CLEAR;
  309. rc = -EIO;
  310. len = cper_estatus_len(ghes->estatus);
  311. if (len < sizeof(*ghes->estatus))
  312. goto err_read_block;
  313. if (len > ghes->generic->error_block_length)
  314. goto err_read_block;
  315. if (cper_estatus_check_header(ghes->estatus))
  316. goto err_read_block;
  317. ghes_copy_tofrom_phys(ghes->estatus + 1,
  318. buf_paddr + sizeof(*ghes->estatus),
  319. len - sizeof(*ghes->estatus), 1);
  320. if (cper_estatus_check(ghes->estatus))
  321. goto err_read_block;
  322. rc = 0;
  323. err_read_block:
  324. if (rc && !silent && printk_ratelimit())
  325. pr_warning(FW_WARN GHES_PFX
  326. "Failed to read error status block!\n");
  327. return rc;
  328. }
  329. static void ghes_clear_estatus(struct ghes *ghes)
  330. {
  331. ghes->estatus->block_status = 0;
  332. if (!(ghes->flags & GHES_TO_CLEAR))
  333. return;
  334. ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
  335. sizeof(ghes->estatus->block_status), 0);
  336. ghes->flags &= ~GHES_TO_CLEAR;
  337. }
  338. static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
  339. {
  340. #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
  341. unsigned long pfn;
  342. int flags = -1;
  343. int sec_sev = ghes_severity(gdata->error_severity);
  344. struct cper_sec_mem_err *mem_err;
  345. mem_err = (struct cper_sec_mem_err *)(gdata + 1);
  346. if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
  347. return;
  348. pfn = mem_err->physical_addr >> PAGE_SHIFT;
  349. if (!pfn_valid(pfn)) {
  350. pr_warn_ratelimited(FW_WARN GHES_PFX
  351. "Invalid address in generic error data: %#llx\n",
  352. mem_err->physical_addr);
  353. return;
  354. }
  355. /* iff following two events can be handled properly by now */
  356. if (sec_sev == GHES_SEV_CORRECTED &&
  357. (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
  358. flags = MF_SOFT_OFFLINE;
  359. if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
  360. flags = 0;
  361. if (flags != -1)
  362. memory_failure_queue(pfn, 0, flags);
  363. #endif
  364. }
  365. static void ghes_do_proc(struct ghes *ghes,
  366. const struct acpi_hest_generic_status *estatus)
  367. {
  368. int sev, sec_sev;
  369. struct acpi_hest_generic_data *gdata;
  370. sev = ghes_severity(estatus->error_severity);
  371. apei_estatus_for_each_section(estatus, gdata) {
  372. sec_sev = ghes_severity(gdata->error_severity);
  373. if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  374. CPER_SEC_PLATFORM_MEM)) {
  375. struct cper_sec_mem_err *mem_err;
  376. mem_err = (struct cper_sec_mem_err *)(gdata+1);
  377. ghes_edac_report_mem_error(ghes, sev, mem_err);
  378. arch_apei_report_mem_error(sev, mem_err);
  379. ghes_handle_memory_failure(gdata, sev);
  380. }
  381. #ifdef CONFIG_ACPI_APEI_PCIEAER
  382. else if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  383. CPER_SEC_PCIE)) {
  384. struct cper_sec_pcie *pcie_err;
  385. pcie_err = (struct cper_sec_pcie *)(gdata+1);
  386. if (sev == GHES_SEV_RECOVERABLE &&
  387. sec_sev == GHES_SEV_RECOVERABLE &&
  388. pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
  389. pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
  390. unsigned int devfn;
  391. int aer_severity;
  392. devfn = PCI_DEVFN(pcie_err->device_id.device,
  393. pcie_err->device_id.function);
  394. aer_severity = cper_severity_to_aer(sev);
  395. /*
  396. * If firmware reset the component to contain
  397. * the error, we must reinitialize it before
  398. * use, so treat it as a fatal AER error.
  399. */
  400. if (gdata->flags & CPER_SEC_RESET)
  401. aer_severity = AER_FATAL;
  402. aer_recover_queue(pcie_err->device_id.segment,
  403. pcie_err->device_id.bus,
  404. devfn, aer_severity,
  405. (struct aer_capability_regs *)
  406. pcie_err->aer_info);
  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. cper_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 = cper_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 = cper_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 = cper_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 notifier_block ghes_notifier_sci = {
  625. .notifier_call = ghes_notify_sci,
  626. };
  627. #ifdef CONFIG_HAVE_ACPI_APEI_NMI
  628. /*
  629. * printk is not safe in NMI context. So in NMI handler, we allocate
  630. * required memory from lock-less memory allocator
  631. * (ghes_estatus_pool), save estatus into it, put them into lock-less
  632. * list (ghes_estatus_llist), then delay printk into IRQ context via
  633. * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
  634. * required pool size by all NMI error source.
  635. */
  636. static struct llist_head ghes_estatus_llist;
  637. static struct irq_work ghes_proc_irq_work;
  638. /*
  639. * NMI may be triggered on any CPU, so ghes_in_nmi is used for
  640. * having only one concurrent reader.
  641. */
  642. static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
  643. static LIST_HEAD(ghes_nmi);
  644. static int ghes_panic_timeout __read_mostly = 30;
  645. static void ghes_proc_in_irq(struct irq_work *irq_work)
  646. {
  647. struct llist_node *llnode, *next;
  648. struct ghes_estatus_node *estatus_node;
  649. struct acpi_hest_generic *generic;
  650. struct acpi_hest_generic_status *estatus;
  651. u32 len, node_len;
  652. llnode = llist_del_all(&ghes_estatus_llist);
  653. /*
  654. * Because the time order of estatus in list is reversed,
  655. * revert it back to proper order.
  656. */
  657. llnode = llist_reverse_order(llnode);
  658. while (llnode) {
  659. next = llnode->next;
  660. estatus_node = llist_entry(llnode, struct ghes_estatus_node,
  661. llnode);
  662. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  663. len = cper_estatus_len(estatus);
  664. node_len = GHES_ESTATUS_NODE_LEN(len);
  665. ghes_do_proc(estatus_node->ghes, estatus);
  666. if (!ghes_estatus_cached(estatus)) {
  667. generic = estatus_node->generic;
  668. if (ghes_print_estatus(NULL, generic, estatus))
  669. ghes_estatus_cache_add(generic, estatus);
  670. }
  671. gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
  672. node_len);
  673. llnode = next;
  674. }
  675. }
  676. static void ghes_print_queued_estatus(void)
  677. {
  678. struct llist_node *llnode;
  679. struct ghes_estatus_node *estatus_node;
  680. struct acpi_hest_generic *generic;
  681. struct acpi_hest_generic_status *estatus;
  682. u32 len, node_len;
  683. llnode = llist_del_all(&ghes_estatus_llist);
  684. /*
  685. * Because the time order of estatus in list is reversed,
  686. * revert it back to proper order.
  687. */
  688. llnode = llist_reverse_order(llnode);
  689. while (llnode) {
  690. estatus_node = llist_entry(llnode, struct ghes_estatus_node,
  691. llnode);
  692. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  693. len = cper_estatus_len(estatus);
  694. node_len = GHES_ESTATUS_NODE_LEN(len);
  695. generic = estatus_node->generic;
  696. ghes_print_estatus(NULL, generic, estatus);
  697. llnode = llnode->next;
  698. }
  699. }
  700. /* Save estatus for further processing in IRQ context */
  701. static void __process_error(struct ghes *ghes)
  702. {
  703. #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  704. u32 len, node_len;
  705. struct ghes_estatus_node *estatus_node;
  706. struct acpi_hest_generic_status *estatus;
  707. if (ghes_estatus_cached(ghes->estatus))
  708. return;
  709. len = cper_estatus_len(ghes->estatus);
  710. node_len = GHES_ESTATUS_NODE_LEN(len);
  711. estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
  712. if (!estatus_node)
  713. return;
  714. estatus_node->ghes = ghes;
  715. estatus_node->generic = ghes->generic;
  716. estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
  717. memcpy(estatus, ghes->estatus, len);
  718. llist_add(&estatus_node->llnode, &ghes_estatus_llist);
  719. #endif
  720. }
  721. static void __ghes_panic(struct ghes *ghes)
  722. {
  723. oops_begin();
  724. ghes_print_queued_estatus();
  725. __ghes_print_estatus(KERN_EMERG, ghes->generic, ghes->estatus);
  726. /* reboot to log the error! */
  727. if (panic_timeout == 0)
  728. panic_timeout = ghes_panic_timeout;
  729. panic("Fatal hardware error!");
  730. }
  731. static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
  732. {
  733. struct ghes *ghes;
  734. int sev, ret = NMI_DONE;
  735. if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
  736. return ret;
  737. list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
  738. if (ghes_read_estatus(ghes, 1)) {
  739. ghes_clear_estatus(ghes);
  740. continue;
  741. }
  742. sev = ghes_severity(ghes->estatus->error_severity);
  743. if (sev >= GHES_SEV_PANIC)
  744. __ghes_panic(ghes);
  745. if (!(ghes->flags & GHES_TO_CLEAR))
  746. continue;
  747. __process_error(ghes);
  748. ghes_clear_estatus(ghes);
  749. ret = NMI_HANDLED;
  750. }
  751. #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  752. irq_work_queue(&ghes_proc_irq_work);
  753. #endif
  754. atomic_dec(&ghes_in_nmi);
  755. return ret;
  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 void ghes_estatus_pool_shrink(unsigned long len)
  770. {
  771. ghes_estatus_pool_size_request -= PAGE_ALIGN(len);
  772. }
  773. static void ghes_nmi_add(struct ghes *ghes)
  774. {
  775. unsigned long len;
  776. len = ghes_esource_prealloc_size(ghes->generic);
  777. ghes_estatus_pool_expand(len);
  778. mutex_lock(&ghes_list_mutex);
  779. if (list_empty(&ghes_nmi))
  780. register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
  781. list_add_rcu(&ghes->list, &ghes_nmi);
  782. mutex_unlock(&ghes_list_mutex);
  783. }
  784. static void ghes_nmi_remove(struct ghes *ghes)
  785. {
  786. unsigned long len;
  787. mutex_lock(&ghes_list_mutex);
  788. list_del_rcu(&ghes->list);
  789. if (list_empty(&ghes_nmi))
  790. unregister_nmi_handler(NMI_LOCAL, "ghes");
  791. mutex_unlock(&ghes_list_mutex);
  792. /*
  793. * To synchronize with NMI handler, ghes can only be
  794. * freed after NMI handler finishes.
  795. */
  796. synchronize_rcu();
  797. len = ghes_esource_prealloc_size(ghes->generic);
  798. ghes_estatus_pool_shrink(len);
  799. }
  800. static void ghes_nmi_init_cxt(void)
  801. {
  802. init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
  803. }
  804. #else /* CONFIG_HAVE_ACPI_APEI_NMI */
  805. static inline void ghes_nmi_add(struct ghes *ghes)
  806. {
  807. pr_err(GHES_PFX "ID: %d, trying to add NMI notification which is not supported!\n",
  808. ghes->generic->header.source_id);
  809. BUG();
  810. }
  811. static inline void ghes_nmi_remove(struct ghes *ghes)
  812. {
  813. pr_err(GHES_PFX "ID: %d, trying to remove NMI notification which is not supported!\n",
  814. ghes->generic->header.source_id);
  815. BUG();
  816. }
  817. static inline void ghes_nmi_init_cxt(void)
  818. {
  819. }
  820. #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
  821. static int ghes_probe(struct platform_device *ghes_dev)
  822. {
  823. struct acpi_hest_generic *generic;
  824. struct ghes *ghes = NULL;
  825. int rc = -EINVAL;
  826. generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
  827. if (!generic->enabled)
  828. return -ENODEV;
  829. switch (generic->notify.type) {
  830. case ACPI_HEST_NOTIFY_POLLED:
  831. case ACPI_HEST_NOTIFY_EXTERNAL:
  832. case ACPI_HEST_NOTIFY_SCI:
  833. break;
  834. case ACPI_HEST_NOTIFY_NMI:
  835. if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
  836. pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
  837. generic->header.source_id);
  838. goto err;
  839. }
  840. break;
  841. case ACPI_HEST_NOTIFY_LOCAL:
  842. pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
  843. generic->header.source_id);
  844. goto err;
  845. default:
  846. pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
  847. generic->notify.type, generic->header.source_id);
  848. goto err;
  849. }
  850. rc = -EIO;
  851. if (generic->error_block_length <
  852. sizeof(struct acpi_hest_generic_status)) {
  853. pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
  854. generic->error_block_length,
  855. generic->header.source_id);
  856. goto err;
  857. }
  858. ghes = ghes_new(generic);
  859. if (IS_ERR(ghes)) {
  860. rc = PTR_ERR(ghes);
  861. ghes = NULL;
  862. goto err;
  863. }
  864. rc = ghes_edac_register(ghes, &ghes_dev->dev);
  865. if (rc < 0)
  866. goto err;
  867. switch (generic->notify.type) {
  868. case ACPI_HEST_NOTIFY_POLLED:
  869. ghes->timer.function = ghes_poll_func;
  870. ghes->timer.data = (unsigned long)ghes;
  871. init_timer_deferrable(&ghes->timer);
  872. ghes_add_timer(ghes);
  873. break;
  874. case ACPI_HEST_NOTIFY_EXTERNAL:
  875. /* External interrupt vector is GSI */
  876. rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
  877. if (rc) {
  878. pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
  879. generic->header.source_id);
  880. goto err_edac_unreg;
  881. }
  882. rc = request_irq(ghes->irq, ghes_irq_func, 0, "GHES IRQ", ghes);
  883. if (rc) {
  884. pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
  885. generic->header.source_id);
  886. goto err_edac_unreg;
  887. }
  888. break;
  889. case ACPI_HEST_NOTIFY_SCI:
  890. mutex_lock(&ghes_list_mutex);
  891. if (list_empty(&ghes_sci))
  892. register_acpi_hed_notifier(&ghes_notifier_sci);
  893. list_add_rcu(&ghes->list, &ghes_sci);
  894. mutex_unlock(&ghes_list_mutex);
  895. break;
  896. case ACPI_HEST_NOTIFY_NMI:
  897. ghes_nmi_add(ghes);
  898. break;
  899. default:
  900. BUG();
  901. }
  902. platform_set_drvdata(ghes_dev, ghes);
  903. return 0;
  904. err_edac_unreg:
  905. ghes_edac_unregister(ghes);
  906. err:
  907. if (ghes) {
  908. ghes_fini(ghes);
  909. kfree(ghes);
  910. }
  911. return rc;
  912. }
  913. static int ghes_remove(struct platform_device *ghes_dev)
  914. {
  915. struct ghes *ghes;
  916. struct acpi_hest_generic *generic;
  917. ghes = platform_get_drvdata(ghes_dev);
  918. generic = ghes->generic;
  919. ghes->flags |= GHES_EXITING;
  920. switch (generic->notify.type) {
  921. case ACPI_HEST_NOTIFY_POLLED:
  922. del_timer_sync(&ghes->timer);
  923. break;
  924. case ACPI_HEST_NOTIFY_EXTERNAL:
  925. free_irq(ghes->irq, ghes);
  926. break;
  927. case ACPI_HEST_NOTIFY_SCI:
  928. mutex_lock(&ghes_list_mutex);
  929. list_del_rcu(&ghes->list);
  930. if (list_empty(&ghes_sci))
  931. unregister_acpi_hed_notifier(&ghes_notifier_sci);
  932. mutex_unlock(&ghes_list_mutex);
  933. break;
  934. case ACPI_HEST_NOTIFY_NMI:
  935. ghes_nmi_remove(ghes);
  936. break;
  937. default:
  938. BUG();
  939. break;
  940. }
  941. ghes_fini(ghes);
  942. ghes_edac_unregister(ghes);
  943. kfree(ghes);
  944. platform_set_drvdata(ghes_dev, NULL);
  945. return 0;
  946. }
  947. static struct platform_driver ghes_platform_driver = {
  948. .driver = {
  949. .name = "GHES",
  950. },
  951. .probe = ghes_probe,
  952. .remove = ghes_remove,
  953. };
  954. static int __init ghes_init(void)
  955. {
  956. int rc;
  957. if (acpi_disabled)
  958. return -ENODEV;
  959. if (hest_disable) {
  960. pr_info(GHES_PFX "HEST is not enabled!\n");
  961. return -EINVAL;
  962. }
  963. if (ghes_disable) {
  964. pr_info(GHES_PFX "GHES is not enabled!\n");
  965. return -EINVAL;
  966. }
  967. ghes_nmi_init_cxt();
  968. rc = ghes_ioremap_init();
  969. if (rc)
  970. goto err;
  971. rc = ghes_estatus_pool_init();
  972. if (rc)
  973. goto err_ioremap_exit;
  974. rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
  975. GHES_ESTATUS_CACHE_ALLOCED_MAX);
  976. if (rc)
  977. goto err_pool_exit;
  978. rc = platform_driver_register(&ghes_platform_driver);
  979. if (rc)
  980. goto err_pool_exit;
  981. rc = apei_osc_setup();
  982. if (rc == 0 && osc_sb_apei_support_acked)
  983. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
  984. else if (rc == 0 && !osc_sb_apei_support_acked)
  985. pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
  986. else if (rc && osc_sb_apei_support_acked)
  987. pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
  988. else
  989. pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
  990. return 0;
  991. err_pool_exit:
  992. ghes_estatus_pool_exit();
  993. err_ioremap_exit:
  994. ghes_ioremap_exit();
  995. err:
  996. return rc;
  997. }
  998. static void __exit ghes_exit(void)
  999. {
  1000. platform_driver_unregister(&ghes_platform_driver);
  1001. ghes_estatus_pool_exit();
  1002. ghes_ioremap_exit();
  1003. }
  1004. module_init(ghes_init);
  1005. module_exit(ghes_exit);
  1006. MODULE_AUTHOR("Huang Ying");
  1007. MODULE_DESCRIPTION("APEI Generic Hardware Error Source support");
  1008. MODULE_LICENSE("GPL");
  1009. MODULE_ALIAS("platform:GHES");