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 <machine/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 TUNABLE_INT("hw.mca.enabled", &mca_enabled);
96 SYSCTL_INT(_hw_mca, OID_AUTO, enabled, CTLFLAG_RDTUN, &mca_enabled, 0,
97 "Administrative toggle for machine check support");
99 static int amd10h_L1TP = 1;
100 TUNABLE_INT("hw.mca.amd10h_L1TP", &amd10h_L1TP);
101 SYSCTL_INT(_hw_mca, OID_AUTO, amd10h_L1TP, CTLFLAG_RDTUN, &amd10h_L1TP, 0,
102 "Administrative toggle for logging of level one TLB parity (L1TP) errors");
104 static int intel6h_HSD131;
105 TUNABLE_INT("hw.mca.intel6h_hsd131", &intel6h_HSD131);
106 SYSCTL_INT(_hw_mca, OID_AUTO, intel6h_HSD131, CTLFLAG_RDTUN, &intel6h_HSD131, 0,
107 "Administrative toggle for logging of spurious corrected errors");
109 int workaround_erratum383;
110 SYSCTL_INT(_hw_mca, OID_AUTO, erratum383, CTLFLAG_RD, &workaround_erratum383, 0,
111 "Is the workaround for Erratum 383 on AMD Family 10h processors enabled?");
113 static STAILQ_HEAD(, mca_internal) mca_freelist;
114 static int mca_freecount;
115 static STAILQ_HEAD(, mca_internal) mca_records;
116 static struct callout mca_timer;
117 static int mca_ticks = 3600; /* Check hourly by default. */
118 static struct taskqueue *mca_tq;
119 static struct task mca_refill_task, mca_scan_task;
120 static struct mtx mca_lock;
123 static struct cmc_state **cmc_state; /* Indexed by cpuid, bank */
124 static int cmc_throttle = 60; /* Time in seconds to throttle CMCI. */
128 sysctl_positive_int(SYSCTL_HANDLER_ARGS)
132 value = *(int *)arg1;
133 error = sysctl_handle_int(oidp, &value, 0, req);
134 if (error || req->newptr == NULL)
138 *(int *)arg1 = value;
143 sysctl_mca_records(SYSCTL_HANDLER_ARGS)
145 int *name = (int *)arg1;
146 u_int namelen = arg2;
147 struct mca_record record;
148 struct mca_internal *rec;
154 if (name[0] < 0 || name[0] >= mca_count)
157 mtx_lock_spin(&mca_lock);
158 if (name[0] >= mca_count) {
159 mtx_unlock_spin(&mca_lock);
163 STAILQ_FOREACH(rec, &mca_records, link) {
170 mtx_unlock_spin(&mca_lock);
171 return (SYSCTL_OUT(req, &record, sizeof(record)));
175 mca_error_ttype(uint16_t mca_error)
178 switch ((mca_error & 0x000c) >> 2) {
190 mca_error_level(uint16_t mca_error)
193 switch (mca_error & 0x0003) {
207 mca_error_request(uint16_t mca_error)
210 switch ((mca_error & 0x00f0) >> 4) {
234 mca_error_mmtype(uint16_t mca_error)
237 switch ((mca_error & 0x70) >> 4) {
252 static int __nonnull(1)
253 mca_mute(const struct mca_record *rec)
257 * Skip spurious corrected parity errors generated by Intel Haswell-
258 * and Broadwell-based CPUs (see HSD131, HSM142, HSW131 and BDM48
259 * erratum respectively), unless reporting is enabled.
260 * Note that these errors also have been observed with the D0-stepping
261 * of Haswell, while at least initially the CPU specification updates
262 * suggested only the C0-stepping to be affected. Similarly, Celeron
263 * 2955U with a CPU ID of 0x45 apparently are also concerned with the
264 * same problem, with HSM142 only referring to 0x3c and 0x46.
266 if (cpu_vendor_id == CPU_VENDOR_INTEL &&
267 CPUID_TO_FAMILY(cpu_id) == 0x6 &&
268 (CPUID_TO_MODEL(cpu_id) == 0x3c || /* HSD131, HSM142, HSW131 */
269 CPUID_TO_MODEL(cpu_id) == 0x3d || /* BDM48 */
270 CPUID_TO_MODEL(cpu_id) == 0x45 ||
271 CPUID_TO_MODEL(cpu_id) == 0x46) && /* HSM142 */
273 (rec->mr_status & 0xa0000000ffffffff) == 0x80000000000f0005 &&
280 /* Dump details about a single machine check. */
281 static void __nonnull(1)
282 mca_log(const struct mca_record *rec)
289 printf("MCA: Bank %d, Status 0x%016llx\n", rec->mr_bank,
290 (long long)rec->mr_status);
291 printf("MCA: Global Cap 0x%016llx, Status 0x%016llx\n",
292 (long long)rec->mr_mcg_cap, (long long)rec->mr_mcg_status);
293 printf("MCA: Vendor \"%s\", ID 0x%x, APIC ID %d\n", cpu_vendor,
294 rec->mr_cpu_id, rec->mr_apic_id);
295 printf("MCA: CPU %d ", rec->mr_cpu);
296 if (rec->mr_status & MC_STATUS_UC)
300 if (rec->mr_mcg_cap & MCG_CAP_CMCI_P)
301 printf("(%lld) ", ((long long)rec->mr_status &
302 MC_STATUS_COR_COUNT) >> 38);
304 if (rec->mr_status & MC_STATUS_PCC)
306 if (rec->mr_status & MC_STATUS_OVER)
308 mca_error = rec->mr_status & MC_STATUS_MCA_ERROR;
310 /* Simple error codes. */
315 printf("unclassified error");
318 printf("ucode ROM parity error");
321 printf("external error");
327 printf("internal parity error");
330 printf("internal timer error");
333 if ((mca_error & 0xfc00) == 0x0400) {
334 printf("internal error %x", mca_error & 0x03ff);
338 /* Compound error codes. */
340 /* Memory hierarchy error. */
341 if ((mca_error & 0xeffc) == 0x000c) {
342 printf("%s memory error", mca_error_level(mca_error));
347 if ((mca_error & 0xeff0) == 0x0010) {
348 printf("%sTLB %s error", mca_error_ttype(mca_error),
349 mca_error_level(mca_error));
353 /* Memory controller error. */
354 if ((mca_error & 0xef80) == 0x0080) {
355 printf("%s channel ", mca_error_mmtype(mca_error));
356 if ((mca_error & 0x000f) != 0x000f)
357 printf("%d", mca_error & 0x000f);
360 printf(" memory error");
365 if ((mca_error & 0xef00) == 0x0100) {
366 printf("%sCACHE %s %s error",
367 mca_error_ttype(mca_error),
368 mca_error_level(mca_error),
369 mca_error_request(mca_error));
373 /* Bus and/or Interconnect error. */
374 if ((mca_error & 0xe800) == 0x0800) {
375 printf("BUS%s ", mca_error_level(mca_error));
376 switch ((mca_error & 0x0600) >> 9) {
390 printf(" %s ", mca_error_request(mca_error));
391 switch ((mca_error & 0x000c) >> 2) {
405 if (mca_error & 0x0100)
406 printf(" timed out");
410 printf("unknown error %x", mca_error);
414 if (rec->mr_status & MC_STATUS_ADDRV)
415 printf("MCA: Address 0x%llx\n", (long long)rec->mr_addr);
416 if (rec->mr_status & MC_STATUS_MISCV)
417 printf("MCA: Misc 0x%llx\n", (long long)rec->mr_misc);
420 static int __nonnull(2)
421 mca_check_status(int bank, struct mca_record *rec)
426 status = rdmsr(MSR_MC_STATUS(bank));
427 if (!(status & MC_STATUS_VAL))
430 /* Save exception information. */
431 rec->mr_status = status;
434 if (status & MC_STATUS_ADDRV)
435 rec->mr_addr = rdmsr(MSR_MC_ADDR(bank));
437 if (status & MC_STATUS_MISCV)
438 rec->mr_misc = rdmsr(MSR_MC_MISC(bank));
439 rec->mr_tsc = rdtsc();
440 rec->mr_apic_id = PCPU_GET(apic_id);
441 rec->mr_mcg_cap = rdmsr(MSR_MCG_CAP);
442 rec->mr_mcg_status = rdmsr(MSR_MCG_STATUS);
443 rec->mr_cpu_id = cpu_id;
444 rec->mr_cpu_vendor_id = cpu_vendor_id;
445 rec->mr_cpu = PCPU_GET(cpuid);
448 * Clear machine check. Don't do this for uncorrectable
449 * errors so that the BIOS can see them.
451 if (!(rec->mr_status & (MC_STATUS_PCC | MC_STATUS_UC))) {
452 wrmsr(MSR_MC_STATUS(bank), 0);
459 mca_fill_freelist(void)
461 struct mca_internal *rec;
465 * Ensure we have at least one record for each bank and one
468 desired = imax(mp_ncpus, mca_banks);
469 mtx_lock_spin(&mca_lock);
470 while (mca_freecount < desired) {
471 mtx_unlock_spin(&mca_lock);
472 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
473 mtx_lock_spin(&mca_lock);
474 STAILQ_INSERT_TAIL(&mca_freelist, rec, link);
477 mtx_unlock_spin(&mca_lock);
481 mca_refill(void *context, int pending)
487 static void __nonnull(2)
488 mca_record_entry(enum scan_mode mode, const struct mca_record *record)
490 struct mca_internal *rec;
492 if (mode == POLLED) {
493 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
494 mtx_lock_spin(&mca_lock);
496 mtx_lock_spin(&mca_lock);
497 rec = STAILQ_FIRST(&mca_freelist);
499 printf("MCA: Unable to allocate space for an event.\n");
501 mtx_unlock_spin(&mca_lock);
504 STAILQ_REMOVE_HEAD(&mca_freelist, link);
510 STAILQ_INSERT_TAIL(&mca_records, rec, link);
512 mtx_unlock_spin(&mca_lock);
514 taskqueue_enqueue_fast(mca_tq, &mca_refill_task);
519 * Update the interrupt threshold for a CMCI. The strategy is to use
520 * a low trigger that interrupts as soon as the first event occurs.
521 * However, if a steady stream of events arrive, the threshold is
522 * increased until the interrupts are throttled to once every
523 * cmc_throttle seconds or the periodic scan. If a periodic scan
524 * finds that the threshold is too high, it is lowered.
527 cmci_update(enum scan_mode mode, int bank, int valid, struct mca_record *rec)
529 struct cmc_state *cc;
534 /* Fetch the current limit for this bank. */
535 cc = &cmc_state[PCPU_GET(cpuid)][bank];
536 ctl = rdmsr(MSR_MC_CTL2(bank));
537 count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
538 delta = (u_int)(ticks - cc->last_intr);
541 * If an interrupt was received less than cmc_throttle seconds
542 * since the previous interrupt and the count from the current
543 * event is greater than or equal to the current threshold,
544 * double the threshold up to the max.
546 if (mode == CMCI && valid) {
547 limit = ctl & MC_CTL2_THRESHOLD;
548 if (delta < cmc_throttle && count >= limit &&
549 limit < cc->max_threshold) {
550 limit = min(limit << 1, cc->max_threshold);
551 ctl &= ~MC_CTL2_THRESHOLD;
553 wrmsr(MSR_MC_CTL2(bank), limit);
555 cc->last_intr = ticks;
560 * When the banks are polled, check to see if the threshold
566 /* If a CMCI occured recently, do nothing for now. */
567 if (delta < cmc_throttle)
571 * Compute a new limit based on the average rate of events per
572 * cmc_throttle seconds since the last interrupt.
575 count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
576 limit = count * cmc_throttle / delta;
579 else if (limit > cc->max_threshold)
580 limit = cc->max_threshold;
583 if ((ctl & MC_CTL2_THRESHOLD) != limit) {
584 ctl &= ~MC_CTL2_THRESHOLD;
586 wrmsr(MSR_MC_CTL2(bank), limit);
592 * This scans all the machine check banks of the current CPU to see if
593 * there are any machine checks. Any non-recoverable errors are
594 * reported immediately via mca_log(). The current thread must be
595 * pinned when this is called. The 'mode' parameter indicates if we
596 * are being called from the MC exception handler, the CMCI handler,
597 * or the periodic poller. In the MC exception case this function
598 * returns true if the system is restartable. Otherwise, it returns a
599 * count of the number of valid MC records found.
602 mca_scan(enum scan_mode mode)
604 struct mca_record rec;
605 uint64_t mcg_cap, ucmask;
606 int count, i, recoverable, valid;
610 ucmask = MC_STATUS_UC | MC_STATUS_PCC;
612 /* When handling a MCE#, treat the OVER flag as non-restartable. */
614 ucmask |= MC_STATUS_OVER;
615 mcg_cap = rdmsr(MSR_MCG_CAP);
616 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
619 * For a CMCI, only check banks this CPU is
622 if (mode == CMCI && !(PCPU_GET(cmci_mask) & 1 << i))
626 valid = mca_check_status(i, &rec);
629 if (rec.mr_status & ucmask) {
631 mtx_lock_spin(&mca_lock);
633 mtx_unlock_spin(&mca_lock);
635 mca_record_entry(mode, &rec);
640 * If this is a bank this CPU monitors via CMCI,
641 * update the threshold.
643 if (PCPU_GET(cmci_mask) & 1 << i)
644 cmci_update(mode, i, valid, &rec);
649 return (mode == MCE ? recoverable : count);
653 * Scan the machine check banks on all CPUs by binding to each CPU in
654 * turn. If any of the CPUs contained new machine check records, log
655 * them to the console.
658 mca_scan_cpus(void *context, int pending)
660 struct mca_internal *mca;
671 count += mca_scan(POLLED);
677 mtx_lock_spin(&mca_lock);
678 STAILQ_FOREACH(mca, &mca_records, link) {
684 mtx_unlock_spin(&mca_lock);
689 mca_periodic_scan(void *arg)
692 taskqueue_enqueue_fast(mca_tq, &mca_scan_task);
693 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
697 sysctl_mca_scan(SYSCTL_HANDLER_ARGS)
702 error = sysctl_handle_int(oidp, &i, 0, req);
706 taskqueue_enqueue_fast(mca_tq, &mca_scan_task);
711 mca_createtq(void *dummy)
716 mca_tq = taskqueue_create_fast("mca", M_WAITOK,
717 taskqueue_thread_enqueue, &mca_tq);
718 taskqueue_start_threads(&mca_tq, 1, PI_SWI(SWI_TQ), "mca taskq");
720 SYSINIT(mca_createtq, SI_SUB_CONFIGURE, SI_ORDER_ANY, mca_createtq, NULL);
723 mca_startup(void *dummy)
729 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
731 SYSINIT(mca_startup, SI_SUB_SMP, SI_ORDER_ANY, mca_startup, NULL);
739 cmc_state = malloc((mp_maxid + 1) * sizeof(struct cmc_state *), M_MCA,
741 for (i = 0; i <= mp_maxid; i++)
742 cmc_state[i] = malloc(sizeof(struct cmc_state) * mca_banks,
743 M_MCA, M_WAITOK | M_ZERO);
744 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
745 "cmc_throttle", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
746 &cmc_throttle, 0, sysctl_positive_int, "I",
747 "Interval in seconds to throttle corrected MC interrupts");
752 mca_setup(uint64_t mcg_cap)
756 * On AMD Family 10h processors, unless logging of level one TLB
757 * parity (L1TP) errors is disabled, enable the recommended workaround
760 if (cpu_vendor_id == CPU_VENDOR_AMD &&
761 CPUID_TO_FAMILY(cpu_id) == 0x10 && amd10h_L1TP)
762 workaround_erratum383 = 1;
764 mca_banks = mcg_cap & MCG_CAP_COUNT;
765 mtx_init(&mca_lock, "mca", NULL, MTX_SPIN);
766 STAILQ_INIT(&mca_records);
767 TASK_INIT(&mca_scan_task, 0, mca_scan_cpus, NULL);
768 callout_init(&mca_timer, CALLOUT_MPSAFE);
769 STAILQ_INIT(&mca_freelist);
770 TASK_INIT(&mca_refill_task, 0, mca_refill, NULL);
772 SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
773 "count", CTLFLAG_RD, (int *)(uintptr_t)&mca_count, 0,
775 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
776 "interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &mca_ticks,
777 0, sysctl_positive_int, "I",
778 "Periodic interval in seconds to scan for machine checks");
779 SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
780 "records", CTLFLAG_RD, sysctl_mca_records, "Machine check records");
781 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
782 "force_scan", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
783 sysctl_mca_scan, "I", "Force an immediate scan for machine checks");
785 if (mcg_cap & MCG_CAP_CMCI_P)
792 * See if we should monitor CMCI for this bank. If CMCI_EN is already
793 * set in MC_CTL2, then another CPU is responsible for this bank, so
794 * ignore it. If CMCI_EN returns zero after being set, then this bank
795 * does not support CMCI_EN. If this CPU sets CMCI_EN, then it should
796 * now monitor this bank.
801 struct cmc_state *cc;
804 KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
806 ctl = rdmsr(MSR_MC_CTL2(i));
807 if (ctl & MC_CTL2_CMCI_EN)
808 /* Already monitored by another CPU. */
811 /* Set the threshold to one event for now. */
812 ctl &= ~MC_CTL2_THRESHOLD;
813 ctl |= MC_CTL2_CMCI_EN | 1;
814 wrmsr(MSR_MC_CTL2(i), ctl);
815 ctl = rdmsr(MSR_MC_CTL2(i));
816 if (!(ctl & MC_CTL2_CMCI_EN))
817 /* This bank does not support CMCI. */
820 cc = &cmc_state[PCPU_GET(cpuid)][i];
822 /* Determine maximum threshold. */
823 ctl &= ~MC_CTL2_THRESHOLD;
825 wrmsr(MSR_MC_CTL2(i), ctl);
826 ctl = rdmsr(MSR_MC_CTL2(i));
827 cc->max_threshold = ctl & MC_CTL2_THRESHOLD;
829 /* Start off with a threshold of 1. */
830 ctl &= ~MC_CTL2_THRESHOLD;
832 wrmsr(MSR_MC_CTL2(i), ctl);
834 /* Mark this bank as monitored. */
835 PCPU_SET(cmci_mask, PCPU_GET(cmci_mask) | 1 << i);
839 * For resume, reset the threshold for any banks we monitor back to
840 * one and throw away the timestamp of the last interrupt.
845 struct cmc_state *cc;
848 KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
850 /* Ignore banks not monitored by this CPU. */
851 if (!(PCPU_GET(cmci_mask) & 1 << i))
854 cc = &cmc_state[PCPU_GET(cpuid)][i];
855 cc->last_intr = -ticks;
856 ctl = rdmsr(MSR_MC_CTL2(i));
857 ctl &= ~MC_CTL2_THRESHOLD;
858 ctl |= MC_CTL2_CMCI_EN | 1;
859 wrmsr(MSR_MC_CTL2(i), ctl);
864 * Initializes per-CPU machine check registers and enables corrected
865 * machine check interrupts.
874 /* MCE is required. */
875 if (!mca_enabled || !(cpu_feature & CPUID_MCE))
878 if (cpu_feature & CPUID_MCA) {
880 PCPU_SET(cmci_mask, 0);
882 mcg_cap = rdmsr(MSR_MCG_CAP);
883 if (mcg_cap & MCG_CAP_CTL_P)
884 /* Enable MCA features. */
885 wrmsr(MSR_MCG_CTL, MCG_CTL_ENABLE);
886 if (PCPU_GET(cpuid) == 0 && boot)
890 * Disable logging of level one TLB parity (L1TP) errors by
891 * the data cache as an alternative workaround for AMD Family
892 * 10h Erratum 383. Unlike the recommended workaround, there
893 * is no performance penalty to this workaround. However,
894 * L1TP errors will go unreported.
896 if (cpu_vendor_id == CPU_VENDOR_AMD &&
897 CPUID_TO_FAMILY(cpu_id) == 0x10 && !amd10h_L1TP) {
898 mask = rdmsr(MSR_MC0_CTL_MASK);
899 if ((mask & (1UL << 5)) == 0)
900 wrmsr(MSR_MC0_CTL_MASK, mask | (1UL << 5));
902 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
903 /* By default enable logging of all errors. */
904 ctl = 0xffffffffffffffffUL;
907 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
909 * For P6 models before Nehalem MC0_CTL is
910 * always enabled and reserved.
912 if (i == 0 && CPUID_TO_FAMILY(cpu_id) == 0x6
913 && CPUID_TO_MODEL(cpu_id) < 0x1a)
915 } else if (cpu_vendor_id == CPU_VENDOR_AMD) {
916 /* BKDG for Family 10h: unset GartTblWkEn. */
917 if (i == 4 && CPUID_TO_FAMILY(cpu_id) >= 0xf)
922 wrmsr(MSR_MC_CTL(i), ctl);
925 if (mcg_cap & MCG_CAP_CMCI_P) {
933 /* Clear all errors. */
934 wrmsr(MSR_MC_STATUS(i), 0);
938 if (PCPU_GET(cmci_mask) != 0 && boot)
943 load_cr4(rcr4() | CR4_MCE);
946 /* Must be executed on each CPU during boot. */
954 /* Must be executed on each CPU during resume. */
963 * The machine check registers for the BSP cannot be initialized until
964 * the local APIC is initialized. This happens at SI_SUB_CPU,
968 mca_init_bsp(void *arg __unused)
973 SYSINIT(mca_init_bsp, SI_SUB_CPU, SI_ORDER_ANY, mca_init_bsp, NULL);
975 /* Called when a machine check exception fires. */
980 int old_count, recoverable;
982 if (!(cpu_feature & CPUID_MCA)) {
984 * Just print the values of the old Pentium registers
987 printf("MC Type: 0x%jx Address: 0x%jx\n",
988 (uintmax_t)rdmsr(MSR_P5_MC_TYPE),
989 (uintmax_t)rdmsr(MSR_P5_MC_ADDR));
990 panic("Machine check");
993 /* Scan the banks and check for any non-recoverable errors. */
994 old_count = mca_count;
995 recoverable = mca_scan(MCE);
996 mcg_status = rdmsr(MSR_MCG_STATUS);
997 if (!(mcg_status & MCG_STATUS_RIPV))
1002 * Wait for at least one error to be logged before
1003 * panic'ing. Some errors will assert a machine check
1004 * on all CPUs, but only certain CPUs will find a valid
1007 while (mca_count == old_count)
1010 panic("Unrecoverable machine check exception");
1014 wrmsr(MSR_MCG_STATUS, mcg_status & ~MCG_STATUS_MCIP);
1018 /* Called for a CMCI (correctable machine check interrupt). */
1022 struct mca_internal *mca;
1026 * Serialize MCA bank scanning to prevent collisions from
1029 count = mca_scan(CMCI);
1031 /* If we found anything, log them to the console. */
1033 mtx_lock_spin(&mca_lock);
1034 STAILQ_FOREACH(mca, &mca_records, link) {
1040 mtx_unlock_spin(&mca_lock);