ghes.c 27 KB

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