2 * Copyright (c) 2009 Hudson River Trading 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 <x86/apicvar.h>
56 #include <machine/cpu.h>
57 #include <machine/cputypes.h>
59 #include <machine/md_var.h>
60 #include <machine/specialreg.h>
62 /* Modes for mca_scan() */
71 * State maintained for each monitored MCx bank to control the
72 * corrected machine check interrupt threshold.
81 struct mca_record rec;
83 STAILQ_ENTRY(mca_internal) link;
86 static MALLOC_DEFINE(M_MCA, "MCA", "Machine Check Architecture");
88 static volatile int mca_count; /* Number of records stored. */
89 static int mca_banks; /* Number of per-CPU register banks. */
91 static SYSCTL_NODE(_hw, OID_AUTO, mca, CTLFLAG_RD, NULL,
92 "Machine Check Architecture");
94 static int mca_enabled = 1;
95 SYSCTL_INT(_hw_mca, OID_AUTO, enabled, CTLFLAG_RDTUN, &mca_enabled, 0,
96 "Administrative toggle for machine check support");
98 static int amd10h_L1TP = 1;
99 SYSCTL_INT(_hw_mca, OID_AUTO, amd10h_L1TP, CTLFLAG_RDTUN, &amd10h_L1TP, 0,
100 "Administrative toggle for logging of level one TLB parity (L1TP) errors");
102 static int intel6h_HSD131;
103 SYSCTL_INT(_hw_mca, OID_AUTO, intel6h_HSD131, CTLFLAG_RDTUN, &intel6h_HSD131, 0,
104 "Administrative toggle for logging of spurious corrected errors");
106 int workaround_erratum383;
107 SYSCTL_INT(_hw_mca, OID_AUTO, erratum383, CTLFLAG_RDTUN,
108 &workaround_erratum383, 0,
109 "Is the workaround for Erratum 383 on AMD Family 10h processors enabled?");
111 static STAILQ_HEAD(, mca_internal) mca_freelist;
112 static int mca_freecount;
113 static STAILQ_HEAD(, mca_internal) mca_records;
114 static struct callout mca_timer;
115 static int mca_ticks = 3600; /* Check hourly by default. */
116 static struct taskqueue *mca_tq;
117 static struct task mca_refill_task, mca_scan_task;
118 static struct mtx mca_lock;
121 static struct cmc_state **cmc_state; /* Indexed by cpuid, bank */
122 static int cmc_throttle = 60; /* Time in seconds to throttle CMCI. */
126 sysctl_positive_int(SYSCTL_HANDLER_ARGS)
130 value = *(int *)arg1;
131 error = sysctl_handle_int(oidp, &value, 0, req);
132 if (error || req->newptr == NULL)
136 *(int *)arg1 = value;
141 sysctl_mca_records(SYSCTL_HANDLER_ARGS)
143 int *name = (int *)arg1;
144 u_int namelen = arg2;
145 struct mca_record record;
146 struct mca_internal *rec;
152 if (name[0] < 0 || name[0] >= mca_count)
155 mtx_lock_spin(&mca_lock);
156 if (name[0] >= mca_count) {
157 mtx_unlock_spin(&mca_lock);
161 STAILQ_FOREACH(rec, &mca_records, link) {
168 mtx_unlock_spin(&mca_lock);
169 return (SYSCTL_OUT(req, &record, sizeof(record)));
173 mca_error_ttype(uint16_t mca_error)
176 switch ((mca_error & 0x000c) >> 2) {
188 mca_error_level(uint16_t mca_error)
191 switch (mca_error & 0x0003) {
205 mca_error_request(uint16_t mca_error)
208 switch ((mca_error & 0x00f0) >> 4) {
232 mca_error_mmtype(uint16_t mca_error)
235 switch ((mca_error & 0x70) >> 4) {
251 mca_mute(const struct mca_record *rec)
255 * Skip spurious corrected parity errors generated by Intel Haswell-
256 * and Broadwell-based CPUs (see HSD131, HSM142, HSW131 and BDM48
257 * erratum respectively), unless reporting is enabled.
258 * Note that these errors also have been observed with the D0-stepping
259 * of Haswell, while at least initially the CPU specification updates
260 * suggested only the C0-stepping to be affected. Similarly, Celeron
261 * 2955U with a CPU ID of 0x45 apparently are also concerned with the
262 * same problem, with HSM142 only referring to 0x3c and 0x46.
264 if (cpu_vendor_id == CPU_VENDOR_INTEL &&
265 CPUID_TO_FAMILY(cpu_id) == 0x6 &&
266 (CPUID_TO_MODEL(cpu_id) == 0x3c || /* HSD131, HSM142, HSW131 */
267 CPUID_TO_MODEL(cpu_id) == 0x3d || /* BDM48 */
268 CPUID_TO_MODEL(cpu_id) == 0x45 ||
269 CPUID_TO_MODEL(cpu_id) == 0x46) && /* HSM142 */
271 (rec->mr_status & 0xa0000000ffffffff) == 0x80000000000f0005 &&
278 /* Dump details about a single machine check. */
280 mca_log(const struct mca_record *rec)
287 printf("MCA: Bank %d, Status 0x%016llx\n", rec->mr_bank,
288 (long long)rec->mr_status);
289 printf("MCA: Global Cap 0x%016llx, Status 0x%016llx\n",
290 (long long)rec->mr_mcg_cap, (long long)rec->mr_mcg_status);
291 printf("MCA: Vendor \"%s\", ID 0x%x, APIC ID %d\n", cpu_vendor,
292 rec->mr_cpu_id, rec->mr_apic_id);
293 printf("MCA: CPU %d ", rec->mr_cpu);
294 if (rec->mr_status & MC_STATUS_UC)
298 if (rec->mr_mcg_cap & MCG_CAP_CMCI_P)
299 printf("(%lld) ", ((long long)rec->mr_status &
300 MC_STATUS_COR_COUNT) >> 38);
302 if (rec->mr_status & MC_STATUS_PCC)
304 if (rec->mr_status & MC_STATUS_OVER)
306 mca_error = rec->mr_status & MC_STATUS_MCA_ERROR;
308 /* Simple error codes. */
313 printf("unclassified error");
316 printf("ucode ROM parity error");
319 printf("external error");
325 printf("internal parity error");
328 printf("internal timer error");
331 if ((mca_error & 0xfc00) == 0x0400) {
332 printf("internal error %x", mca_error & 0x03ff);
336 /* Compound error codes. */
338 /* Memory hierarchy error. */
339 if ((mca_error & 0xeffc) == 0x000c) {
340 printf("%s memory error", mca_error_level(mca_error));
345 if ((mca_error & 0xeff0) == 0x0010) {
346 printf("%sTLB %s error", mca_error_ttype(mca_error),
347 mca_error_level(mca_error));
351 /* Memory controller error. */
352 if ((mca_error & 0xef80) == 0x0080) {
353 printf("%s channel ", mca_error_mmtype(mca_error));
354 if ((mca_error & 0x000f) != 0x000f)
355 printf("%d", mca_error & 0x000f);
358 printf(" memory error");
363 if ((mca_error & 0xef00) == 0x0100) {
364 printf("%sCACHE %s %s error",
365 mca_error_ttype(mca_error),
366 mca_error_level(mca_error),
367 mca_error_request(mca_error));
371 /* Bus and/or Interconnect error. */
372 if ((mca_error & 0xe800) == 0x0800) {
373 printf("BUS%s ", mca_error_level(mca_error));
374 switch ((mca_error & 0x0600) >> 9) {
388 printf(" %s ", mca_error_request(mca_error));
389 switch ((mca_error & 0x000c) >> 2) {
403 if (mca_error & 0x0100)
404 printf(" timed out");
408 printf("unknown error %x", mca_error);
412 if (rec->mr_status & MC_STATUS_ADDRV)
413 printf("MCA: Address 0x%llx\n", (long long)rec->mr_addr);
414 if (rec->mr_status & MC_STATUS_MISCV)
415 printf("MCA: Misc 0x%llx\n", (long long)rec->mr_misc);
419 mca_check_status(int bank, struct mca_record *rec)
424 status = rdmsr(MSR_MC_STATUS(bank));
425 if (!(status & MC_STATUS_VAL))
428 /* Save exception information. */
429 rec->mr_status = status;
432 if (status & MC_STATUS_ADDRV)
433 rec->mr_addr = rdmsr(MSR_MC_ADDR(bank));
435 if (status & MC_STATUS_MISCV)
436 rec->mr_misc = rdmsr(MSR_MC_MISC(bank));
437 rec->mr_tsc = rdtsc();
438 rec->mr_apic_id = PCPU_GET(apic_id);
439 rec->mr_mcg_cap = rdmsr(MSR_MCG_CAP);
440 rec->mr_mcg_status = rdmsr(MSR_MCG_STATUS);
441 rec->mr_cpu_id = cpu_id;
442 rec->mr_cpu_vendor_id = cpu_vendor_id;
443 rec->mr_cpu = PCPU_GET(cpuid);
446 * Clear machine check. Don't do this for uncorrectable
447 * errors so that the BIOS can see them.
449 if (!(rec->mr_status & (MC_STATUS_PCC | MC_STATUS_UC))) {
450 wrmsr(MSR_MC_STATUS(bank), 0);
457 mca_fill_freelist(void)
459 struct mca_internal *rec;
463 * Ensure we have at least one record for each bank and one
466 desired = imax(mp_ncpus, mca_banks);
467 mtx_lock_spin(&mca_lock);
468 while (mca_freecount < desired) {
469 mtx_unlock_spin(&mca_lock);
470 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
471 mtx_lock_spin(&mca_lock);
472 STAILQ_INSERT_TAIL(&mca_freelist, rec, link);
475 mtx_unlock_spin(&mca_lock);
479 mca_refill(void *context, int pending)
486 mca_record_entry(enum scan_mode mode, const struct mca_record *record)
488 struct mca_internal *rec;
490 if (mode == POLLED) {
491 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
492 mtx_lock_spin(&mca_lock);
494 mtx_lock_spin(&mca_lock);
495 rec = STAILQ_FIRST(&mca_freelist);
497 printf("MCA: Unable to allocate space for an event.\n");
499 mtx_unlock_spin(&mca_lock);
502 STAILQ_REMOVE_HEAD(&mca_freelist, link);
508 STAILQ_INSERT_TAIL(&mca_records, rec, link);
510 mtx_unlock_spin(&mca_lock);
511 if (mode == CMCI && !cold)
512 taskqueue_enqueue(mca_tq, &mca_refill_task);
517 * Update the interrupt threshold for a CMCI. The strategy is to use
518 * a low trigger that interrupts as soon as the first event occurs.
519 * However, if a steady stream of events arrive, the threshold is
520 * increased until the interrupts are throttled to once every
521 * cmc_throttle seconds or the periodic scan. If a periodic scan
522 * finds that the threshold is too high, it is lowered.
525 cmci_update(enum scan_mode mode, int bank, int valid, struct mca_record *rec)
527 struct cmc_state *cc;
532 /* Fetch the current limit for this bank. */
533 cc = &cmc_state[PCPU_GET(cpuid)][bank];
534 ctl = rdmsr(MSR_MC_CTL2(bank));
535 count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
536 delta = (u_int)(time_uptime - cc->last_intr);
539 * If an interrupt was received less than cmc_throttle seconds
540 * since the previous interrupt and the count from the current
541 * event is greater than or equal to the current threshold,
542 * double the threshold up to the max.
544 if (mode == CMCI && valid) {
545 limit = ctl & MC_CTL2_THRESHOLD;
546 if (delta < cmc_throttle && count >= limit &&
547 limit < cc->max_threshold) {
548 limit = min(limit << 1, cc->max_threshold);
549 ctl &= ~MC_CTL2_THRESHOLD;
551 wrmsr(MSR_MC_CTL2(bank), ctl);
553 cc->last_intr = time_uptime;
558 * When the banks are polled, check to see if the threshold
564 /* If a CMCI occured recently, do nothing for now. */
565 if (delta < cmc_throttle)
569 * Compute a new limit based on the average rate of events per
570 * cmc_throttle seconds since the last interrupt.
573 count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
574 limit = count * cmc_throttle / delta;
577 else if (limit > cc->max_threshold)
578 limit = cc->max_threshold;
581 if ((ctl & MC_CTL2_THRESHOLD) != limit) {
582 ctl &= ~MC_CTL2_THRESHOLD;
584 wrmsr(MSR_MC_CTL2(bank), ctl);
590 * This scans all the machine check banks of the current CPU to see if
591 * there are any machine checks. Any non-recoverable errors are
592 * reported immediately via mca_log(). The current thread must be
593 * pinned when this is called. The 'mode' parameter indicates if we
594 * are being called from the MC exception handler, the CMCI handler,
595 * or the periodic poller. In the MC exception case this function
596 * returns true if the system is restartable. Otherwise, it returns a
597 * count of the number of valid MC records found.
600 mca_scan(enum scan_mode mode)
602 struct mca_record rec;
603 uint64_t mcg_cap, ucmask;
604 int count, i, recoverable, valid;
608 ucmask = MC_STATUS_UC | MC_STATUS_PCC;
610 /* When handling a MCE#, treat the OVER flag as non-restartable. */
612 ucmask |= MC_STATUS_OVER;
613 mcg_cap = rdmsr(MSR_MCG_CAP);
614 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
617 * For a CMCI, only check banks this CPU is
620 if (mode == CMCI && !(PCPU_GET(cmci_mask) & 1 << i))
624 valid = mca_check_status(i, &rec);
627 if (rec.mr_status & ucmask) {
629 mtx_lock_spin(&mca_lock);
631 mtx_unlock_spin(&mca_lock);
633 mca_record_entry(mode, &rec);
638 * If this is a bank this CPU monitors via CMCI,
639 * update the threshold.
641 if (PCPU_GET(cmci_mask) & 1 << i)
642 cmci_update(mode, i, valid, &rec);
647 return (mode == MCE ? recoverable : count);
651 * Scan the machine check banks on all CPUs by binding to each CPU in
652 * turn. If any of the CPUs contained new machine check records, log
653 * them to the console.
656 mca_scan_cpus(void *context, int pending)
658 struct mca_internal *mca;
669 count += mca_scan(POLLED);
675 mtx_lock_spin(&mca_lock);
676 STAILQ_FOREACH(mca, &mca_records, link) {
682 mtx_unlock_spin(&mca_lock);
687 mca_periodic_scan(void *arg)
690 taskqueue_enqueue(mca_tq, &mca_scan_task);
691 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
695 sysctl_mca_scan(SYSCTL_HANDLER_ARGS)
700 error = sysctl_handle_int(oidp, &i, 0, req);
704 taskqueue_enqueue(mca_tq, &mca_scan_task);
709 mca_createtq(void *dummy)
714 mca_tq = taskqueue_create_fast("mca", M_WAITOK,
715 taskqueue_thread_enqueue, &mca_tq);
716 taskqueue_start_threads(&mca_tq, 1, PI_SWI(SWI_TQ), "mca taskq");
718 /* CMCIs during boot may have claimed items from the freelist. */
721 SYSINIT(mca_createtq, SI_SUB_CONFIGURE, SI_ORDER_ANY, mca_createtq, NULL);
724 mca_startup(void *dummy)
730 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
732 #ifdef EARLY_AP_STARTUP
733 SYSINIT(mca_startup, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, mca_startup, NULL);
735 SYSINIT(mca_startup, SI_SUB_SMP, SI_ORDER_ANY, mca_startup, NULL);
744 cmc_state = malloc((mp_maxid + 1) * sizeof(struct cmc_state *), M_MCA,
746 for (i = 0; i <= mp_maxid; i++)
747 cmc_state[i] = malloc(sizeof(struct cmc_state) * mca_banks,
748 M_MCA, M_WAITOK | M_ZERO);
749 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
750 "cmc_throttle", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
751 &cmc_throttle, 0, sysctl_positive_int, "I",
752 "Interval in seconds to throttle corrected MC interrupts");
757 mca_setup(uint64_t mcg_cap)
761 * On AMD Family 10h processors, unless logging of level one TLB
762 * parity (L1TP) errors is disabled, enable the recommended workaround
765 if (cpu_vendor_id == CPU_VENDOR_AMD &&
766 CPUID_TO_FAMILY(cpu_id) == 0x10 && amd10h_L1TP)
767 workaround_erratum383 = 1;
769 mca_banks = mcg_cap & MCG_CAP_COUNT;
770 mtx_init(&mca_lock, "mca", NULL, MTX_SPIN);
771 STAILQ_INIT(&mca_records);
772 TASK_INIT(&mca_scan_task, 0, mca_scan_cpus, NULL);
773 callout_init(&mca_timer, 1);
774 STAILQ_INIT(&mca_freelist);
775 TASK_INIT(&mca_refill_task, 0, mca_refill, NULL);
777 SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
778 "count", CTLFLAG_RD, (int *)(uintptr_t)&mca_count, 0,
780 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
781 "interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &mca_ticks,
782 0, sysctl_positive_int, "I",
783 "Periodic interval in seconds to scan for machine checks");
784 SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
785 "records", CTLFLAG_RD, sysctl_mca_records, "Machine check records");
786 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
787 "force_scan", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
788 sysctl_mca_scan, "I", "Force an immediate scan for machine checks");
790 if (mcg_cap & MCG_CAP_CMCI_P)
797 * See if we should monitor CMCI for this bank. If CMCI_EN is already
798 * set in MC_CTL2, then another CPU is responsible for this bank, so
799 * ignore it. If CMCI_EN returns zero after being set, then this bank
800 * does not support CMCI_EN. If this CPU sets CMCI_EN, then it should
801 * now monitor this bank.
806 struct cmc_state *cc;
809 KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
811 ctl = rdmsr(MSR_MC_CTL2(i));
812 if (ctl & MC_CTL2_CMCI_EN)
813 /* Already monitored by another CPU. */
816 /* Set the threshold to one event for now. */
817 ctl &= ~MC_CTL2_THRESHOLD;
818 ctl |= MC_CTL2_CMCI_EN | 1;
819 wrmsr(MSR_MC_CTL2(i), ctl);
820 ctl = rdmsr(MSR_MC_CTL2(i));
821 if (!(ctl & MC_CTL2_CMCI_EN))
822 /* This bank does not support CMCI. */
825 cc = &cmc_state[PCPU_GET(cpuid)][i];
827 /* Determine maximum threshold. */
828 ctl &= ~MC_CTL2_THRESHOLD;
830 wrmsr(MSR_MC_CTL2(i), ctl);
831 ctl = rdmsr(MSR_MC_CTL2(i));
832 cc->max_threshold = ctl & MC_CTL2_THRESHOLD;
834 /* Start off with a threshold of 1. */
835 ctl &= ~MC_CTL2_THRESHOLD;
837 wrmsr(MSR_MC_CTL2(i), ctl);
839 /* Mark this bank as monitored. */
840 PCPU_SET(cmci_mask, PCPU_GET(cmci_mask) | 1 << i);
844 * For resume, reset the threshold for any banks we monitor back to
845 * one and throw away the timestamp of the last interrupt.
850 struct cmc_state *cc;
853 KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
855 /* Ignore banks not monitored by this CPU. */
856 if (!(PCPU_GET(cmci_mask) & 1 << i))
859 cc = &cmc_state[PCPU_GET(cpuid)][i];
861 ctl = rdmsr(MSR_MC_CTL2(i));
862 ctl &= ~MC_CTL2_THRESHOLD;
863 ctl |= MC_CTL2_CMCI_EN | 1;
864 wrmsr(MSR_MC_CTL2(i), ctl);
869 * Initializes per-CPU machine check registers and enables corrected
870 * machine check interrupts.
879 /* MCE is required. */
880 if (!mca_enabled || !(cpu_feature & CPUID_MCE))
883 if (cpu_feature & CPUID_MCA) {
885 PCPU_SET(cmci_mask, 0);
887 mcg_cap = rdmsr(MSR_MCG_CAP);
888 if (mcg_cap & MCG_CAP_CTL_P)
889 /* Enable MCA features. */
890 wrmsr(MSR_MCG_CTL, MCG_CTL_ENABLE);
891 if (PCPU_GET(cpuid) == 0 && boot)
895 * Disable logging of level one TLB parity (L1TP) errors by
896 * the data cache as an alternative workaround for AMD Family
897 * 10h Erratum 383. Unlike the recommended workaround, there
898 * is no performance penalty to this workaround. However,
899 * L1TP errors will go unreported.
901 if (cpu_vendor_id == CPU_VENDOR_AMD &&
902 CPUID_TO_FAMILY(cpu_id) == 0x10 && !amd10h_L1TP) {
903 mask = rdmsr(MSR_MC0_CTL_MASK);
904 if ((mask & (1UL << 5)) == 0)
905 wrmsr(MSR_MC0_CTL_MASK, mask | (1UL << 5));
907 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
908 /* By default enable logging of all errors. */
909 ctl = 0xffffffffffffffffUL;
912 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
914 * For P6 models before Nehalem MC0_CTL is
915 * always enabled and reserved.
917 if (i == 0 && CPUID_TO_FAMILY(cpu_id) == 0x6
918 && CPUID_TO_MODEL(cpu_id) < 0x1a)
920 } else if (cpu_vendor_id == CPU_VENDOR_AMD) {
921 /* BKDG for Family 10h: unset GartTblWkEn. */
922 if (i == 4 && CPUID_TO_FAMILY(cpu_id) >= 0xf)
927 wrmsr(MSR_MC_CTL(i), ctl);
930 if (mcg_cap & MCG_CAP_CMCI_P) {
938 /* Clear all errors. */
939 wrmsr(MSR_MC_STATUS(i), 0);
943 if (PCPU_GET(cmci_mask) != 0 && boot)
948 load_cr4(rcr4() | CR4_MCE);
951 /* Must be executed on each CPU during boot. */
959 /* Must be executed on each CPU during resume. */
968 * The machine check registers for the BSP cannot be initialized until
969 * the local APIC is initialized. This happens at SI_SUB_CPU,
973 mca_init_bsp(void *arg __unused)
978 SYSINIT(mca_init_bsp, SI_SUB_CPU, SI_ORDER_ANY, mca_init_bsp, NULL);
980 /* Called when a machine check exception fires. */
985 int old_count, recoverable;
987 if (!(cpu_feature & CPUID_MCA)) {
989 * Just print the values of the old Pentium registers
992 printf("MC Type: 0x%jx Address: 0x%jx\n",
993 (uintmax_t)rdmsr(MSR_P5_MC_TYPE),
994 (uintmax_t)rdmsr(MSR_P5_MC_ADDR));
995 panic("Machine check");
998 /* Scan the banks and check for any non-recoverable errors. */
999 old_count = mca_count;
1000 recoverable = mca_scan(MCE);
1001 mcg_status = rdmsr(MSR_MCG_STATUS);
1002 if (!(mcg_status & MCG_STATUS_RIPV))
1007 * Wait for at least one error to be logged before
1008 * panic'ing. Some errors will assert a machine check
1009 * on all CPUs, but only certain CPUs will find a valid
1012 while (mca_count == old_count)
1015 panic("Unrecoverable machine check exception");
1019 wrmsr(MSR_MCG_STATUS, mcg_status & ~MCG_STATUS_MCIP);
1023 /* Called for a CMCI (correctable machine check interrupt). */
1027 struct mca_internal *mca;
1031 * Serialize MCA bank scanning to prevent collisions from
1034 count = mca_scan(CMCI);
1036 /* If we found anything, log them to the console. */
1038 mtx_lock_spin(&mca_lock);
1039 STAILQ_FOREACH(mca, &mca_records, link) {
1045 mtx_unlock_spin(&mca_lock);