2 * Copyright (c) 2009 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Support for x86 machine check architecture.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
43 #include <sys/interrupt.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
49 #include <sys/sched.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #include <sys/taskqueue.h>
54 #include <machine/intr_machdep.h>
55 #include <machine/apicvar.h>
56 #include <machine/cputypes.h>
58 #include <machine/md_var.h>
59 #include <machine/specialreg.h>
61 /* Modes for mca_scan() */
70 * State maintained for each monitored MCx bank to control the
71 * corrected machine check interrupt threshold.
80 struct mca_record rec;
82 STAILQ_ENTRY(mca_internal) link;
85 static MALLOC_DEFINE(M_MCA, "MCA", "Machine Check Architecture");
87 static int mca_count; /* Number of records stored. */
89 SYSCTL_NODE(_hw, OID_AUTO, mca, CTLFLAG_RD, NULL, "Machine Check Architecture");
91 static int mca_enabled = 1;
92 TUNABLE_INT("hw.mca.enabled", &mca_enabled);
93 SYSCTL_INT(_hw_mca, OID_AUTO, enabled, CTLFLAG_RDTUN, &mca_enabled, 0,
94 "Administrative toggle for machine check support");
96 static int amd10h_L1TP = 1;
97 TUNABLE_INT("hw.mca.amd10h_L1TP", &amd10h_L1TP);
98 SYSCTL_INT(_hw_mca, OID_AUTO, amd10h_L1TP, CTLFLAG_RDTUN, &amd10h_L1TP, 0,
99 "Administrative toggle for logging of level one TLB parity (L1TP) errors");
101 int workaround_erratum383;
102 SYSCTL_INT(_hw_mca, OID_AUTO, erratum383, CTLFLAG_RD, &workaround_erratum383, 0,
103 "Is the workaround for Erratum 383 on AMD Family 10h processors enabled?");
105 static STAILQ_HEAD(, mca_internal) mca_records;
106 static struct callout mca_timer;
107 static int mca_ticks = 3600; /* Check hourly by default. */
108 static struct taskqueue *mca_tq;
109 static struct task mca_task;
110 static struct mtx mca_lock;
113 static struct cmc_state **cmc_state; /* Indexed by cpuid, bank */
114 static int cmc_banks;
115 static int cmc_throttle = 60; /* Time in seconds to throttle CMCI. */
119 sysctl_positive_int(SYSCTL_HANDLER_ARGS)
123 value = *(int *)arg1;
124 error = sysctl_handle_int(oidp, &value, 0, req);
125 if (error || req->newptr == NULL)
129 *(int *)arg1 = value;
134 sysctl_mca_records(SYSCTL_HANDLER_ARGS)
136 int *name = (int *)arg1;
137 u_int namelen = arg2;
138 struct mca_record record;
139 struct mca_internal *rec;
145 if (name[0] < 0 || name[0] >= mca_count)
148 mtx_lock_spin(&mca_lock);
149 if (name[0] >= mca_count) {
150 mtx_unlock_spin(&mca_lock);
154 STAILQ_FOREACH(rec, &mca_records, link) {
161 mtx_unlock_spin(&mca_lock);
162 return (SYSCTL_OUT(req, &record, sizeof(record)));
166 mca_error_ttype(uint16_t mca_error)
169 switch ((mca_error & 0x000c) >> 2) {
181 mca_error_level(uint16_t mca_error)
184 switch (mca_error & 0x0003) {
198 mca_error_request(uint16_t mca_error)
201 switch ((mca_error & 0x00f0) >> 4) {
225 mca_error_mmtype(uint16_t mca_error)
228 switch ((mca_error & 0x70) >> 4) {
243 /* Dump details about a single machine check. */
244 static void __nonnull(1)
245 mca_log(const struct mca_record *rec)
249 printf("MCA: Bank %d, Status 0x%016llx\n", rec->mr_bank,
250 (long long)rec->mr_status);
251 printf("MCA: Global Cap 0x%016llx, Status 0x%016llx\n",
252 (long long)rec->mr_mcg_cap, (long long)rec->mr_mcg_status);
253 printf("MCA: Vendor \"%s\", ID 0x%x, APIC ID %d\n", cpu_vendor,
254 rec->mr_cpu_id, rec->mr_apic_id);
255 printf("MCA: CPU %d ", rec->mr_cpu);
256 if (rec->mr_status & MC_STATUS_UC)
260 if (rec->mr_mcg_cap & MCG_CAP_CMCI_P)
261 printf("(%lld) ", ((long long)rec->mr_status &
262 MC_STATUS_COR_COUNT) >> 38);
264 if (rec->mr_status & MC_STATUS_PCC)
266 if (rec->mr_status & MC_STATUS_OVER)
268 mca_error = rec->mr_status & MC_STATUS_MCA_ERROR;
270 /* Simple error codes. */
275 printf("unclassified error");
278 printf("ucode ROM parity error");
281 printf("external error");
287 printf("internal parity error");
290 printf("internal timer error");
293 if ((mca_error & 0xfc00) == 0x0400) {
294 printf("internal error %x", mca_error & 0x03ff);
298 /* Compound error codes. */
300 /* Memory hierarchy error. */
301 if ((mca_error & 0xeffc) == 0x000c) {
302 printf("%s memory error", mca_error_level(mca_error));
307 if ((mca_error & 0xeff0) == 0x0010) {
308 printf("%sTLB %s error", mca_error_ttype(mca_error),
309 mca_error_level(mca_error));
313 /* Memory controller error. */
314 if ((mca_error & 0xef80) == 0x0080) {
315 printf("%s channel ", mca_error_mmtype(mca_error));
316 if ((mca_error & 0x000f) != 0x000f)
317 printf("%d", mca_error & 0x000f);
320 printf(" memory error");
325 if ((mca_error & 0xef00) == 0x0100) {
326 printf("%sCACHE %s %s error",
327 mca_error_ttype(mca_error),
328 mca_error_level(mca_error),
329 mca_error_request(mca_error));
333 /* Bus and/or Interconnect error. */
334 if ((mca_error & 0xe800) == 0x0800) {
335 printf("BUS%s ", mca_error_level(mca_error));
336 switch ((mca_error & 0x0600) >> 9) {
350 printf(" %s ", mca_error_request(mca_error));
351 switch ((mca_error & 0x000c) >> 2) {
365 if (mca_error & 0x0100)
366 printf(" timed out");
370 printf("unknown error %x", mca_error);
374 if (rec->mr_status & MC_STATUS_ADDRV)
375 printf("MCA: Address 0x%llx\n", (long long)rec->mr_addr);
376 if (rec->mr_status & MC_STATUS_MISCV)
377 printf("MCA: Misc 0x%llx\n", (long long)rec->mr_misc);
380 static int __nonnull(2)
381 mca_check_status(int bank, struct mca_record *rec)
386 status = rdmsr(MSR_MC_STATUS(bank));
387 if (!(status & MC_STATUS_VAL))
390 /* Save exception information. */
391 rec->mr_status = status;
394 if (status & MC_STATUS_ADDRV)
395 rec->mr_addr = rdmsr(MSR_MC_ADDR(bank));
397 if (status & MC_STATUS_MISCV)
398 rec->mr_misc = rdmsr(MSR_MC_MISC(bank));
399 rec->mr_tsc = rdtsc();
400 rec->mr_apic_id = PCPU_GET(apic_id);
401 rec->mr_mcg_cap = rdmsr(MSR_MCG_CAP);
402 rec->mr_mcg_status = rdmsr(MSR_MCG_STATUS);
403 rec->mr_cpu_id = cpu_id;
404 rec->mr_cpu_vendor_id = cpu_vendor_id;
405 rec->mr_cpu = PCPU_GET(cpuid);
408 * Clear machine check. Don't do this for uncorrectable
409 * errors so that the BIOS can see them.
411 if (!(rec->mr_status & (MC_STATUS_PCC | MC_STATUS_UC))) {
412 wrmsr(MSR_MC_STATUS(bank), 0);
418 static void __nonnull(1)
419 mca_record_entry(const struct mca_record *record)
421 struct mca_internal *rec;
423 rec = malloc(sizeof(*rec), M_MCA, M_NOWAIT);
425 printf("MCA: Unable to allocate space for an event.\n");
432 mtx_lock_spin(&mca_lock);
433 STAILQ_INSERT_TAIL(&mca_records, rec, link);
435 mtx_unlock_spin(&mca_lock);
440 * Update the interrupt threshold for a CMCI. The strategy is to use
441 * a low trigger that interrupts as soon as the first event occurs.
442 * However, if a steady stream of events arrive, the threshold is
443 * increased until the interrupts are throttled to once every
444 * cmc_throttle seconds or the periodic scan. If a periodic scan
445 * finds that the threshold is too high, it is lowered.
448 cmci_update(enum scan_mode mode, int bank, int valid, struct mca_record *rec)
450 struct cmc_state *cc;
455 /* Fetch the current limit for this bank. */
456 cc = &cmc_state[PCPU_GET(cpuid)][bank];
457 ctl = rdmsr(MSR_MC_CTL2(bank));
458 count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
459 delta = (u_int)(ticks - cc->last_intr);
462 * If an interrupt was received less than cmc_throttle seconds
463 * since the previous interrupt and the count from the current
464 * event is greater than or equal to the current threshold,
465 * double the threshold up to the max.
467 if (mode == CMCI && valid) {
468 limit = ctl & MC_CTL2_THRESHOLD;
469 if (delta < cmc_throttle && count >= limit &&
470 limit < cc->max_threshold) {
471 limit = min(limit << 1, cc->max_threshold);
472 ctl &= ~MC_CTL2_THRESHOLD;
474 wrmsr(MSR_MC_CTL2(bank), limit);
476 cc->last_intr = ticks;
481 * When the banks are polled, check to see if the threshold
487 /* If a CMCI occured recently, do nothing for now. */
488 if (delta < cmc_throttle)
492 * Compute a new limit based on the average rate of events per
493 * cmc_throttle seconds since the last interrupt.
496 count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
497 limit = count * cmc_throttle / delta;
500 else if (limit > cc->max_threshold)
501 limit = cc->max_threshold;
504 if ((ctl & MC_CTL2_THRESHOLD) != limit) {
505 ctl &= ~MC_CTL2_THRESHOLD;
507 wrmsr(MSR_MC_CTL2(bank), limit);
513 * This scans all the machine check banks of the current CPU to see if
514 * there are any machine checks. Any non-recoverable errors are
515 * reported immediately via mca_log(). The current thread must be
516 * pinned when this is called. The 'mode' parameter indicates if we
517 * are being called from the MC exception handler, the CMCI handler,
518 * or the periodic poller. In the MC exception case this function
519 * returns true if the system is restartable. Otherwise, it returns a
520 * count of the number of valid MC records found.
523 mca_scan(enum scan_mode mode)
525 struct mca_record rec;
526 uint64_t mcg_cap, ucmask;
527 int count, i, recoverable, valid;
531 ucmask = MC_STATUS_UC | MC_STATUS_PCC;
533 /* When handling a MCE#, treat the OVER flag as non-restartable. */
535 ucmask |= MC_STATUS_OVER;
536 mcg_cap = rdmsr(MSR_MCG_CAP);
537 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
540 * For a CMCI, only check banks this CPU is
543 if (mode == CMCI && !(PCPU_GET(cmci_mask) & 1 << i))
547 valid = mca_check_status(i, &rec);
550 if (rec.mr_status & ucmask) {
554 mca_record_entry(&rec);
559 * If this is a bank this CPU monitors via CMCI,
560 * update the threshold.
562 if (PCPU_GET(cmci_mask) & 1 << i)
563 cmci_update(mode, i, valid, &rec);
566 return (mode == MCE ? recoverable : count);
570 * Scan the machine check banks on all CPUs by binding to each CPU in
571 * turn. If any of the CPUs contained new machine check records, log
572 * them to the console.
575 mca_scan_cpus(void *context, int pending)
577 struct mca_internal *mca;
587 count += mca_scan(POLLED);
593 mtx_lock_spin(&mca_lock);
594 STAILQ_FOREACH(mca, &mca_records, link) {
597 mtx_unlock_spin(&mca_lock);
599 mtx_lock_spin(&mca_lock);
602 mtx_unlock_spin(&mca_lock);
607 mca_periodic_scan(void *arg)
610 taskqueue_enqueue(mca_tq, &mca_task);
611 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
615 sysctl_mca_scan(SYSCTL_HANDLER_ARGS)
620 error = sysctl_handle_int(oidp, &i, 0, req);
624 taskqueue_enqueue(mca_tq, &mca_task);
629 mca_startup(void *dummy)
632 if (!mca_enabled || !(cpu_feature & CPUID_MCA))
635 mca_tq = taskqueue_create("mca", M_WAITOK, taskqueue_thread_enqueue,
637 taskqueue_start_threads(&mca_tq, 1, PI_SWI(SWI_TQ), "mca taskq");
638 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan,
641 SYSINIT(mca_startup, SI_SUB_SMP, SI_ORDER_ANY, mca_startup, NULL);
645 cmci_setup(uint64_t mcg_cap)
649 cmc_state = malloc((mp_maxid + 1) * sizeof(struct cmc_state **),
651 cmc_banks = mcg_cap & MCG_CAP_COUNT;
652 for (i = 0; i <= mp_maxid; i++)
653 cmc_state[i] = malloc(sizeof(struct cmc_state) * cmc_banks,
654 M_MCA, M_WAITOK | M_ZERO);
655 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
656 "cmc_throttle", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
657 &cmc_throttle, 0, sysctl_positive_int, "I",
658 "Interval in seconds to throttle corrected MC interrupts");
663 mca_setup(uint64_t mcg_cap)
667 * On AMD Family 10h processors, unless logging of level one TLB
668 * parity (L1TP) errors is disabled, enable the recommended workaround
671 if (cpu_vendor_id == CPU_VENDOR_AMD &&
672 CPUID_TO_FAMILY(cpu_id) == 0x10 && amd10h_L1TP)
673 workaround_erratum383 = 1;
675 mtx_init(&mca_lock, "mca", NULL, MTX_SPIN);
676 STAILQ_INIT(&mca_records);
677 TASK_INIT(&mca_task, 0, mca_scan_cpus, NULL);
678 callout_init(&mca_timer, CALLOUT_MPSAFE);
679 SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
680 "count", CTLFLAG_RD, &mca_count, 0, "Record count");
681 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
682 "interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &mca_ticks,
683 0, sysctl_positive_int, "I",
684 "Periodic interval in seconds to scan for machine checks");
685 SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
686 "records", CTLFLAG_RD, sysctl_mca_records, "Machine check records");
687 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
688 "force_scan", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
689 sysctl_mca_scan, "I", "Force an immediate scan for machine checks");
691 if (mcg_cap & MCG_CAP_CMCI_P)
698 * See if we should monitor CMCI for this bank. If CMCI_EN is already
699 * set in MC_CTL2, then another CPU is responsible for this bank, so
700 * ignore it. If CMCI_EN returns zero after being set, then this bank
701 * does not support CMCI_EN. If this CPU sets CMCI_EN, then it should
702 * now monitor this bank.
707 struct cmc_state *cc;
710 KASSERT(i < cmc_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
712 ctl = rdmsr(MSR_MC_CTL2(i));
713 if (ctl & MC_CTL2_CMCI_EN)
714 /* Already monitored by another CPU. */
717 /* Set the threshold to one event for now. */
718 ctl &= ~MC_CTL2_THRESHOLD;
719 ctl |= MC_CTL2_CMCI_EN | 1;
720 wrmsr(MSR_MC_CTL2(i), ctl);
721 ctl = rdmsr(MSR_MC_CTL2(i));
722 if (!(ctl & MC_CTL2_CMCI_EN))
723 /* This bank does not support CMCI. */
726 cc = &cmc_state[PCPU_GET(cpuid)][i];
728 /* Determine maximum threshold. */
729 ctl &= ~MC_CTL2_THRESHOLD;
731 wrmsr(MSR_MC_CTL2(i), ctl);
732 ctl = rdmsr(MSR_MC_CTL2(i));
733 cc->max_threshold = ctl & MC_CTL2_THRESHOLD;
735 /* Start off with a threshold of 1. */
736 ctl &= ~MC_CTL2_THRESHOLD;
738 wrmsr(MSR_MC_CTL2(i), ctl);
740 /* Mark this bank as monitored. */
741 PCPU_SET(cmci_mask, PCPU_GET(cmci_mask) | 1 << i);
745 * For resume, reset the threshold for any banks we monitor back to
746 * one and throw away the timestamp of the last interrupt.
751 struct cmc_state *cc;
754 KASSERT(i < cmc_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
756 /* Ignore banks not monitored by this CPU. */
757 if (!(PCPU_GET(cmci_mask) & 1 << i))
760 cc = &cmc_state[PCPU_GET(cpuid)][i];
761 cc->last_intr = -ticks;
762 ctl = rdmsr(MSR_MC_CTL2(i));
763 ctl &= ~MC_CTL2_THRESHOLD;
764 ctl |= MC_CTL2_CMCI_EN | 1;
765 wrmsr(MSR_MC_CTL2(i), ctl);
770 * Initializes per-CPU machine check registers and enables corrected
771 * machine check interrupts.
780 /* MCE is required. */
781 if (!mca_enabled || !(cpu_feature & CPUID_MCE))
784 if (cpu_feature & CPUID_MCA) {
786 PCPU_SET(cmci_mask, 0);
788 mcg_cap = rdmsr(MSR_MCG_CAP);
789 if (mcg_cap & MCG_CAP_CTL_P)
790 /* Enable MCA features. */
791 wrmsr(MSR_MCG_CTL, MCG_CTL_ENABLE);
792 if (PCPU_GET(cpuid) == 0 && boot)
796 * Disable logging of level one TLB parity (L1TP) errors by
797 * the data cache as an alternative workaround for AMD Family
798 * 10h Erratum 383. Unlike the recommended workaround, there
799 * is no performance penalty to this workaround. However,
800 * L1TP errors will go unreported.
802 if (cpu_vendor_id == CPU_VENDOR_AMD &&
803 CPUID_TO_FAMILY(cpu_id) == 0x10 && !amd10h_L1TP) {
804 mask = rdmsr(MSR_MC0_CTL_MASK);
805 if ((mask & (1UL << 5)) == 0)
806 wrmsr(MSR_MC0_CTL_MASK, mask | (1UL << 5));
808 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
809 /* By default enable logging of all errors. */
810 ctl = 0xffffffffffffffffUL;
813 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
815 * For P6 models before Nehalem MC0_CTL is
816 * always enabled and reserved.
818 if (i == 0 && CPUID_TO_FAMILY(cpu_id) == 0x6
819 && CPUID_TO_MODEL(cpu_id) < 0x1a)
821 } else if (cpu_vendor_id == CPU_VENDOR_AMD) {
822 /* BKDG for Family 10h: unset GartTblWkEn. */
823 if (i == 4 && CPUID_TO_FAMILY(cpu_id) >= 0xf)
828 wrmsr(MSR_MC_CTL(i), ctl);
831 if (mcg_cap & MCG_CAP_CMCI_P) {
839 /* Clear all errors. */
840 wrmsr(MSR_MC_STATUS(i), 0);
844 if (PCPU_GET(cmci_mask) != 0 && boot)
849 load_cr4(rcr4() | CR4_MCE);
852 /* Must be executed on each CPU during boot. */
860 /* Must be executed on each CPU during resume. */
869 * The machine check registers for the BSP cannot be initialized until
870 * the local APIC is initialized. This happens at SI_SUB_CPU,
874 mca_init_bsp(void *arg __unused)
879 SYSINIT(mca_init_bsp, SI_SUB_CPU, SI_ORDER_ANY, mca_init_bsp, NULL);
881 /* Called when a machine check exception fires. */
888 if (!(cpu_feature & CPUID_MCA)) {
890 * Just print the values of the old Pentium registers
893 printf("MC Type: 0x%jx Address: 0x%jx\n",
894 (uintmax_t)rdmsr(MSR_P5_MC_TYPE),
895 (uintmax_t)rdmsr(MSR_P5_MC_ADDR));
899 /* Scan the banks and check for any non-recoverable errors. */
900 recoverable = mca_scan(MCE);
901 mcg_status = rdmsr(MSR_MCG_STATUS);
902 if (!(mcg_status & MCG_STATUS_RIPV))
906 wrmsr(MSR_MCG_STATUS, mcg_status & ~MCG_STATUS_MCIP);
907 return (recoverable);
911 /* Called for a CMCI (correctable machine check interrupt). */
915 struct mca_internal *mca;
919 * Serialize MCA bank scanning to prevent collisions from
922 count = mca_scan(CMCI);
924 /* If we found anything, log them to the console. */
926 mtx_lock_spin(&mca_lock);
927 STAILQ_FOREACH(mca, &mca_records, link) {
930 mtx_unlock_spin(&mca_lock);
932 mtx_lock_spin(&mca_lock);
935 mtx_unlock_spin(&mca_lock);