ghes.c 28 KB

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