ghes.c 28 KB

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