2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Hudson River Trading LLC
5 * Written by: John H. Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Support for x86 machine check architecture.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
45 #include <sys/interrupt.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
51 #include <sys/sched.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 #include <sys/taskqueue.h>
56 #include <machine/intr_machdep.h>
57 #include <x86/apicvar.h>
58 #include <machine/cpu.h>
59 #include <machine/cputypes.h>
61 #include <machine/md_var.h>
62 #include <machine/specialreg.h>
64 /* Modes for mca_scan() */
73 * State maintained for each monitored MCx bank to control the
74 * corrected machine check interrupt threshold.
88 struct mca_record rec;
89 STAILQ_ENTRY(mca_internal) link;
92 struct mca_enumerator_ops {
93 unsigned int (*ctl)(int);
94 unsigned int (*status)(int);
95 unsigned int (*addr)(int);
96 unsigned int (*misc)(int);
99 static MALLOC_DEFINE(M_MCA, "MCA", "Machine Check Architecture");
101 static volatile int mca_count; /* Number of records stored. */
102 static int mca_banks; /* Number of per-CPU register banks. */
103 static int mca_maxcount = -1; /* Limit on records stored. (-1 = unlimited) */
105 static SYSCTL_NODE(_hw, OID_AUTO, mca, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
106 "Machine Check Architecture");
108 static int mca_enabled = 1;
109 SYSCTL_INT(_hw_mca, OID_AUTO, enabled, CTLFLAG_RDTUN, &mca_enabled, 0,
110 "Administrative toggle for machine check support");
112 static int amd10h_L1TP = 1;
113 SYSCTL_INT(_hw_mca, OID_AUTO, amd10h_L1TP, CTLFLAG_RDTUN, &amd10h_L1TP, 0,
114 "Administrative toggle for logging of level one TLB parity (L1TP) errors");
116 static int intel6h_HSD131;
117 SYSCTL_INT(_hw_mca, OID_AUTO, intel6h_HSD131, CTLFLAG_RDTUN, &intel6h_HSD131, 0,
118 "Administrative toggle for logging of spurious corrected errors");
120 int workaround_erratum383;
121 SYSCTL_INT(_hw_mca, OID_AUTO, erratum383, CTLFLAG_RDTUN,
122 &workaround_erratum383, 0,
123 "Is the workaround for Erratum 383 on AMD Family 10h processors enabled?");
125 static STAILQ_HEAD(, mca_internal) mca_freelist;
126 static int mca_freecount;
127 static STAILQ_HEAD(, mca_internal) mca_records;
128 static STAILQ_HEAD(, mca_internal) mca_pending;
129 static int mca_ticks = 300;
130 static struct taskqueue *mca_tq;
131 static struct task mca_resize_task;
132 static struct timeout_task mca_scan_task;
133 static struct mtx mca_lock;
136 mca_ia32_ctl_reg(int bank)
138 return (MSR_MC_CTL(bank));
142 mca_ia32_status_reg(int bank)
144 return (MSR_MC_STATUS(bank));
148 mca_ia32_addr_reg(int bank)
150 return (MSR_MC_ADDR(bank));
154 mca_ia32_misc_reg(int bank)
156 return (MSR_MC_MISC(bank));
160 mca_smca_ctl_reg(int bank)
162 return (MSR_SMCA_MC_CTL(bank));
166 mca_smca_status_reg(int bank)
168 return (MSR_SMCA_MC_STATUS(bank));
172 mca_smca_addr_reg(int bank)
174 return (MSR_SMCA_MC_ADDR(bank));
178 mca_smca_misc_reg(int bank)
180 return (MSR_SMCA_MC_MISC(bank));
183 static struct mca_enumerator_ops mca_msr_ops = {
184 .ctl = mca_ia32_ctl_reg,
185 .status = mca_ia32_status_reg,
186 .addr = mca_ia32_addr_reg,
187 .misc = mca_ia32_misc_reg
191 static struct cmc_state **cmc_state; /* Indexed by cpuid, bank. */
192 static struct amd_et_state **amd_et_state; /* Indexed by cpuid, bank. */
193 static int cmc_throttle = 60; /* Time in seconds to throttle CMCI. */
195 static int amd_elvt = -1;
198 amd_thresholding_supported(void)
200 if (cpu_vendor_id != CPU_VENDOR_AMD &&
201 cpu_vendor_id != CPU_VENDOR_HYGON)
204 * The RASCap register is wholly reserved in families 0x10-0x15 (through model 1F).
206 * It begins to be documented in family 0x15 model 30 and family 0x16,
207 * but neither of these families documents the ScalableMca bit, which
208 * supposedly defines the presence of this feature on family 0x17.
210 if (CPUID_TO_FAMILY(cpu_id) >= 0x10 && CPUID_TO_FAMILY(cpu_id) <= 0x16)
212 if (CPUID_TO_FAMILY(cpu_id) >= 0x17)
213 return ((amd_rascap & AMDRAS_SCALABLE_MCA) != 0);
219 cmci_supported(uint64_t mcg_cap)
222 * MCG_CAP_CMCI_P bit is reserved in AMD documentation. Until
223 * it is defined, do not use it to check for CMCI support.
225 if (cpu_vendor_id != CPU_VENDOR_INTEL)
227 return ((mcg_cap & MCG_CAP_CMCI_P) != 0);
231 tes_supported(uint64_t mcg_cap)
235 * MCG_CAP_TES_P bit is reserved in AMD documentation. Until
236 * it is defined, do not use it to check for TES support.
238 if (cpu_vendor_id != CPU_VENDOR_INTEL)
240 return ((mcg_cap & MCG_CAP_TES_P) != 0);
244 ser_supported(uint64_t mcg_cap)
247 return (tes_supported(mcg_cap) && (mcg_cap & MCG_CAP_SER_P) != 0);
251 sysctl_positive_int(SYSCTL_HANDLER_ARGS)
255 value = *(int *)arg1;
256 error = sysctl_handle_int(oidp, &value, 0, req);
257 if (error || req->newptr == NULL)
261 *(int *)arg1 = value;
266 sysctl_mca_records(SYSCTL_HANDLER_ARGS)
268 int *name = (int *)arg1;
269 u_int namelen = arg2;
270 struct mca_record record;
271 struct mca_internal *rec;
277 if (name[0] < 0 || name[0] >= mca_count)
280 mtx_lock_spin(&mca_lock);
281 if (name[0] >= mca_count) {
282 mtx_unlock_spin(&mca_lock);
286 STAILQ_FOREACH(rec, &mca_records, link) {
293 mtx_unlock_spin(&mca_lock);
294 return (SYSCTL_OUT(req, &record, sizeof(record)));
298 mca_error_ttype(uint16_t mca_error)
301 switch ((mca_error & 0x000c) >> 2) {
313 mca_error_level(uint16_t mca_error)
316 switch (mca_error & 0x0003) {
330 mca_error_request(uint16_t mca_error)
333 switch ((mca_error & 0x00f0) >> 4) {
357 mca_error_mmtype(uint16_t mca_error)
360 switch ((mca_error & 0x70) >> 4) {
376 mca_addres_mode(uint64_t mca_misc)
379 switch ((mca_misc & MC_MISC_ADDRESS_MODE) >> 6) {
381 return ("Segment Offset");
383 return ("Linear Address");
385 return ("Physical Address");
387 return ("Memory Address");
395 mca_mute(const struct mca_record *rec)
399 * Skip spurious corrected parity errors generated by Intel Haswell-
400 * and Broadwell-based CPUs (see HSD131, HSM142, HSW131 and BDM48
401 * erratum respectively), unless reporting is enabled.
402 * Note that these errors also have been observed with the D0-stepping
403 * of Haswell, while at least initially the CPU specification updates
404 * suggested only the C0-stepping to be affected. Similarly, Celeron
405 * 2955U with a CPU ID of 0x45 apparently are also concerned with the
406 * same problem, with HSM142 only referring to 0x3c and 0x46.
408 if (cpu_vendor_id == CPU_VENDOR_INTEL &&
409 CPUID_TO_FAMILY(cpu_id) == 0x6 &&
410 (CPUID_TO_MODEL(cpu_id) == 0x3c || /* HSD131, HSM142, HSW131 */
411 CPUID_TO_MODEL(cpu_id) == 0x3d || /* BDM48 */
412 CPUID_TO_MODEL(cpu_id) == 0x45 ||
413 CPUID_TO_MODEL(cpu_id) == 0x46) && /* HSM142 */
415 (rec->mr_status & 0xa0000000ffffffff) == 0x80000000000f0005 &&
422 /* Dump details about a single machine check. */
424 mca_log(const struct mca_record *rec)
431 printf("MCA: Bank %d, Status 0x%016llx\n", rec->mr_bank,
432 (long long)rec->mr_status);
433 printf("MCA: Global Cap 0x%016llx, Status 0x%016llx\n",
434 (long long)rec->mr_mcg_cap, (long long)rec->mr_mcg_status);
435 printf("MCA: Vendor \"%s\", ID 0x%x, APIC ID %d\n", cpu_vendor,
436 rec->mr_cpu_id, rec->mr_apic_id);
437 printf("MCA: CPU %d ", rec->mr_cpu);
438 if (rec->mr_status & MC_STATUS_UC)
442 if (cmci_supported(rec->mr_mcg_cap))
443 printf("(%lld) ", ((long long)rec->mr_status &
444 MC_STATUS_COR_COUNT) >> 38);
445 if (tes_supported(rec->mr_mcg_cap)) {
446 switch ((rec->mr_status & MC_STATUS_TES_STATUS) >> 53) {
454 if (rec->mr_status & MC_STATUS_EN)
456 if (rec->mr_status & MC_STATUS_PCC)
458 if (ser_supported(rec->mr_mcg_cap)) {
459 if (rec->mr_status & MC_STATUS_S)
461 if (rec->mr_status & MC_STATUS_AR)
464 if (rec->mr_status & MC_STATUS_OVER)
466 mca_error = rec->mr_status & MC_STATUS_MCA_ERROR;
468 /* Simple error codes. */
473 printf("unclassified error");
476 printf("ucode ROM parity error");
479 printf("external error");
485 printf("internal parity error");
488 printf("SMM handler code access violation");
491 printf("internal timer error");
494 printf("generic I/O error");
495 if (rec->mr_cpu_vendor_id == CPU_VENDOR_INTEL &&
496 (rec->mr_status & MC_STATUS_MISCV)) {
497 printf(" (pci%d:%d:%d:%d)",
498 (int)((rec->mr_misc & MC_MISC_PCIE_SEG) >> 32),
499 (int)((rec->mr_misc & MC_MISC_PCIE_BUS) >> 24),
500 (int)((rec->mr_misc & MC_MISC_PCIE_SLOT) >> 19),
501 (int)((rec->mr_misc & MC_MISC_PCIE_FUNC) >> 16));
505 if ((mca_error & 0xfc00) == 0x0400) {
506 printf("internal error %x", mca_error & 0x03ff);
510 /* Compound error codes. */
512 /* Memory hierarchy error. */
513 if ((mca_error & 0xeffc) == 0x000c) {
514 printf("%s memory error", mca_error_level(mca_error));
519 if ((mca_error & 0xeff0) == 0x0010) {
520 printf("%sTLB %s error", mca_error_ttype(mca_error),
521 mca_error_level(mca_error));
525 /* Memory controller error. */
526 if ((mca_error & 0xef80) == 0x0080) {
527 printf("%s channel ", mca_error_mmtype(mca_error));
528 if ((mca_error & 0x000f) != 0x000f)
529 printf("%d", mca_error & 0x000f);
532 printf(" memory error");
537 if ((mca_error & 0xef00) == 0x0100) {
538 printf("%sCACHE %s %s error",
539 mca_error_ttype(mca_error),
540 mca_error_level(mca_error),
541 mca_error_request(mca_error));
545 /* Extended memory error. */
546 if ((mca_error & 0xef80) == 0x0280) {
547 printf("%s channel ", mca_error_mmtype(mca_error));
548 if ((mca_error & 0x000f) != 0x000f)
549 printf("%d", mca_error & 0x000f);
552 printf(" extended memory error");
556 /* Bus and/or Interconnect error. */
557 if ((mca_error & 0xe800) == 0x0800) {
558 printf("BUS%s ", mca_error_level(mca_error));
559 switch ((mca_error & 0x0600) >> 9) {
573 printf(" %s ", mca_error_request(mca_error));
574 switch ((mca_error & 0x000c) >> 2) {
588 if (mca_error & 0x0100)
589 printf(" timed out");
593 printf("unknown error %x", mca_error);
597 if (rec->mr_status & MC_STATUS_ADDRV) {
598 printf("MCA: Address 0x%llx", (long long)rec->mr_addr);
599 if (ser_supported(rec->mr_mcg_cap) &&
600 (rec->mr_status & MC_STATUS_MISCV)) {
601 printf(" (Mode: %s, LSB: %d)",
602 mca_addres_mode(rec->mr_misc),
603 (int)(rec->mr_misc & MC_MISC_RA_LSB));
607 if (rec->mr_status & MC_STATUS_MISCV)
608 printf("MCA: Misc 0x%llx\n", (long long)rec->mr_misc);
612 mca_is_mce(uint64_t mcg_cap, uint64_t status, bool *recoverablep)
615 /* Corrected error. */
616 if ((status & MC_STATUS_UC) == 0)
619 /* Spurious MCA error. */
620 if ((status & MC_STATUS_EN) == 0)
623 /* The processor does not support software error recovery. */
624 if (!ser_supported(mcg_cap)) {
625 *recoverablep = false;
629 /* Context might have been corrupted. */
630 if (status & MC_STATUS_PCC) {
631 *recoverablep = false;
635 /* Uncorrected software recoverable. */
636 if (status & MC_STATUS_S) {
637 /* Action required vs optional. */
638 if (status & MC_STATUS_AR)
639 *recoverablep = false;
643 /* Uncorrected no action required. */
648 mca_check_status(enum scan_mode mode, uint64_t mcg_cap, int bank,
649 struct mca_record *rec, bool *recoverablep)
655 status = rdmsr(mca_msr_ops.status(bank));
656 if (!(status & MC_STATUS_VAL))
659 recover = *recoverablep;
660 mce = mca_is_mce(mcg_cap, status, &recover);
661 if (mce != (mode == MCE))
663 *recoverablep = recover;
665 /* Save exception information. */
666 rec->mr_status = status;
669 if (status & MC_STATUS_ADDRV)
670 rec->mr_addr = rdmsr(mca_msr_ops.addr(bank));
672 if (status & MC_STATUS_MISCV)
673 rec->mr_misc = rdmsr(mca_msr_ops.misc(bank));
674 rec->mr_tsc = rdtsc();
675 rec->mr_apic_id = PCPU_GET(apic_id);
676 rec->mr_mcg_cap = rdmsr(MSR_MCG_CAP);
677 rec->mr_mcg_status = rdmsr(MSR_MCG_STATUS);
678 rec->mr_cpu_id = cpu_id;
679 rec->mr_cpu_vendor_id = cpu_vendor_id;
680 rec->mr_cpu = PCPU_GET(cpuid);
683 * Clear machine check. Don't do this for uncorrectable
684 * errors so that the BIOS can see them.
686 if (!mce || recover) {
687 wrmsr(mca_msr_ops.status(bank), 0);
694 mca_resize_freelist(void)
696 struct mca_internal *next, *rec;
697 STAILQ_HEAD(, mca_internal) tmplist;
698 int count, i, desired_max, desired_min;
701 * Ensure we have at least one record for each bank and one
702 * record per CPU, but no more than twice that amount.
704 desired_min = imax(mp_ncpus, mca_banks);
705 desired_max = imax(mp_ncpus, mca_banks) * 2;
706 STAILQ_INIT(&tmplist);
707 mtx_lock_spin(&mca_lock);
708 while (mca_freecount > desired_max) {
709 rec = STAILQ_FIRST(&mca_freelist);
710 KASSERT(rec != NULL, ("mca_freecount is %d, but list is empty",
712 STAILQ_REMOVE_HEAD(&mca_freelist, link);
714 STAILQ_INSERT_TAIL(&tmplist, rec, link);
716 while (mca_freecount < desired_min) {
717 count = desired_min - mca_freecount;
718 mtx_unlock_spin(&mca_lock);
719 for (i = 0; i < count; i++) {
720 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
721 STAILQ_INSERT_TAIL(&tmplist, rec, link);
723 mtx_lock_spin(&mca_lock);
724 STAILQ_CONCAT(&mca_freelist, &tmplist);
725 mca_freecount += count;
727 mtx_unlock_spin(&mca_lock);
728 STAILQ_FOREACH_SAFE(rec, &tmplist, link, next)
733 mca_resize(void *context, int pending)
736 mca_resize_freelist();
740 mca_record_entry(enum scan_mode mode, const struct mca_record *record)
742 struct mca_internal *rec;
744 if (mode == POLLED) {
745 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
746 mtx_lock_spin(&mca_lock);
748 mtx_lock_spin(&mca_lock);
749 rec = STAILQ_FIRST(&mca_freelist);
751 printf("MCA: Unable to allocate space for an event.\n");
753 mtx_unlock_spin(&mca_lock);
756 STAILQ_REMOVE_HEAD(&mca_freelist, link);
761 STAILQ_INSERT_TAIL(&mca_pending, rec, link);
762 mtx_unlock_spin(&mca_lock);
767 * Update the interrupt threshold for a CMCI. The strategy is to use
768 * a low trigger that interrupts as soon as the first event occurs.
769 * However, if a steady stream of events arrive, the threshold is
770 * increased until the interrupts are throttled to once every
771 * cmc_throttle seconds or the periodic scan. If a periodic scan
772 * finds that the threshold is too high, it is lowered.
775 update_threshold(enum scan_mode mode, int valid, int last_intr, int count,
776 int cur_threshold, int max_threshold)
781 delta = (u_int)(time_uptime - last_intr);
782 limit = cur_threshold;
785 * If an interrupt was received less than cmc_throttle seconds
786 * since the previous interrupt and the count from the current
787 * event is greater than or equal to the current threshold,
788 * double the threshold up to the max.
790 if (mode == CMCI && valid) {
791 if (delta < cmc_throttle && count >= limit &&
792 limit < max_threshold) {
793 limit = min(limit << 1, max_threshold);
799 * When the banks are polled, check to see if the threshold
805 /* If a CMCI occured recently, do nothing for now. */
806 if (delta < cmc_throttle)
810 * Compute a new limit based on the average rate of events per
811 * cmc_throttle seconds since the last interrupt.
814 limit = count * cmc_throttle / delta;
817 else if (limit > max_threshold)
818 limit = max_threshold;
826 cmci_update(enum scan_mode mode, int bank, int valid, struct mca_record *rec)
828 struct cmc_state *cc;
830 int cur_threshold, new_threshold;
833 /* Fetch the current limit for this bank. */
834 cc = &cmc_state[PCPU_GET(cpuid)][bank];
835 ctl = rdmsr(MSR_MC_CTL2(bank));
836 count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
837 cur_threshold = ctl & MC_CTL2_THRESHOLD;
839 new_threshold = update_threshold(mode, valid, cc->last_intr, count,
840 cur_threshold, cc->max_threshold);
842 if (mode == CMCI && valid)
843 cc->last_intr = time_uptime;
844 if (new_threshold != cur_threshold) {
845 ctl &= ~MC_CTL2_THRESHOLD;
846 ctl |= new_threshold;
847 wrmsr(MSR_MC_CTL2(bank), ctl);
852 amd_thresholding_update(enum scan_mode mode, int bank, int valid)
854 struct amd_et_state *cc;
859 cc = &amd_et_state[PCPU_GET(cpuid)][bank];
860 misc = rdmsr(mca_msr_ops.misc(bank));
861 count = (misc & MC_MISC_AMD_CNT_MASK) >> MC_MISC_AMD_CNT_SHIFT;
862 count = count - (MC_MISC_AMD_CNT_MAX - cc->cur_threshold);
864 new_threshold = update_threshold(mode, valid, cc->last_intr, count,
865 cc->cur_threshold, MC_MISC_AMD_CNT_MAX);
867 cc->cur_threshold = new_threshold;
868 misc &= ~MC_MISC_AMD_CNT_MASK;
869 misc |= (uint64_t)(MC_MISC_AMD_CNT_MAX - cc->cur_threshold)
870 << MC_MISC_AMD_CNT_SHIFT;
871 misc &= ~MC_MISC_AMD_OVERFLOW;
872 wrmsr(mca_msr_ops.misc(bank), misc);
873 if (mode == CMCI && valid)
874 cc->last_intr = time_uptime;
879 * This scans all the machine check banks of the current CPU to see if
880 * there are any machine checks. Any non-recoverable errors are
881 * reported immediately via mca_log(). The current thread must be
882 * pinned when this is called. The 'mode' parameter indicates if we
883 * are being called from the MC exception handler, the CMCI handler,
884 * or the periodic poller.
887 mca_scan(enum scan_mode mode, bool *recoverablep)
889 struct mca_record rec;
891 int count = 0, i, valid;
893 mcg_cap = rdmsr(MSR_MCG_CAP);
894 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
897 * For a CMCI, only check banks this CPU is
900 if (mode == CMCI && !(PCPU_GET(cmci_mask) & 1 << i))
904 valid = mca_check_status(mode, mcg_cap, i, &rec, recoverablep);
908 mca_record_entry(mode, &rec);
915 * If this is a bank this CPU monitors via CMCI,
916 * update the threshold.
918 if (PCPU_GET(cmci_mask) & 1 << i) {
919 if (cmc_state != NULL)
920 cmci_update(mode, i, valid, &rec);
922 amd_thresholding_update(mode, i, valid);
930 * Store a new record on the mca_records list while enforcing
934 mca_store_record(struct mca_internal *mca)
938 * If we are storing no records (mca_maxcount == 0),
939 * we just free this record.
941 * If we are storing records (mca_maxcount != 0) and
942 * we have free space on the list, store the record
943 * and increment mca_count.
945 * If we are storing records and we do not have free
946 * space on the list, store the new record at the
947 * tail and free the oldest one from the head.
949 if (mca_maxcount != 0)
950 STAILQ_INSERT_TAIL(&mca_records, mca, link);
951 if (mca_maxcount < 0 || mca_count < mca_maxcount)
954 if (mca_maxcount != 0) {
955 mca = STAILQ_FIRST(&mca_records);
956 STAILQ_REMOVE_HEAD(&mca_records, link);
958 STAILQ_INSERT_TAIL(&mca_freelist, mca, link);
964 * Do the work to process machine check records which have just been
965 * gathered. Print any pending logs to the console. Queue them for storage.
966 * Trigger a resizing of the free list.
969 mca_process_records(enum scan_mode mode)
971 struct mca_internal *mca;
973 mtx_lock_spin(&mca_lock);
974 while ((mca = STAILQ_FIRST(&mca_pending)) != NULL) {
975 STAILQ_REMOVE_HEAD(&mca_pending, link);
977 mca_store_record(mca);
979 mtx_unlock_spin(&mca_lock);
981 mca_resize_freelist();
983 taskqueue_enqueue(mca_tq, &mca_resize_task);
987 * Scan the machine check banks on all CPUs by binding to each CPU in
988 * turn. If any of the CPUs contained new machine check records, log
989 * them to the console.
992 mca_scan_cpus(void *context, int pending)
996 bool recoverable = true;
998 mca_resize_freelist();
1002 sched_bind(td, cpu);
1004 mca_scan(POLLED, &recoverable);
1009 if (!STAILQ_EMPTY(&mca_pending))
1010 mca_process_records(POLLED);
1011 taskqueue_enqueue_timeout_sbt(mca_tq, &mca_scan_task,
1012 mca_ticks * SBT_1S, 0, C_PREL(1));
1016 sysctl_mca_scan(SYSCTL_HANDLER_ARGS)
1021 error = sysctl_handle_int(oidp, &i, 0, req);
1025 taskqueue_enqueue_timeout_sbt(mca_tq, &mca_scan_task,
1031 sysctl_mca_maxcount(SYSCTL_HANDLER_ARGS)
1033 struct mca_internal *mca;
1038 error = sysctl_handle_int(oidp, &i, 0, req);
1039 if (error || req->newptr == NULL)
1041 mtx_lock_spin(&mca_lock);
1044 if (mca_maxcount >= 0)
1045 while (mca_count > mca_maxcount) {
1046 mca = STAILQ_FIRST(&mca_records);
1047 STAILQ_REMOVE_HEAD(&mca_records, link);
1049 STAILQ_INSERT_TAIL(&mca_freelist, mca, link);
1053 mtx_unlock_spin(&mca_lock);
1054 if (doresize && !cold)
1055 taskqueue_enqueue(mca_tq, &mca_resize_task);
1060 mca_startup(void *dummy)
1066 /* CMCIs during boot may have claimed items from the freelist. */
1067 mca_resize_freelist();
1069 taskqueue_start_threads(&mca_tq, 1, PI_SWI(SWI_TQ), "mca taskq");
1070 taskqueue_enqueue_timeout_sbt(mca_tq, &mca_scan_task,
1071 mca_ticks * SBT_1S, 0, C_PREL(1));
1073 #ifdef EARLY_AP_STARTUP
1074 SYSINIT(mca_startup, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, mca_startup, NULL);
1076 SYSINIT(mca_startup, SI_SUB_SMP, SI_ORDER_ANY, mca_startup, NULL);
1085 cmc_state = malloc((mp_maxid + 1) * sizeof(struct cmc_state *), M_MCA,
1087 for (i = 0; i <= mp_maxid; i++)
1088 cmc_state[i] = malloc(sizeof(struct cmc_state) * mca_banks,
1089 M_MCA, M_WAITOK | M_ZERO);
1090 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
1091 "cmc_throttle", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
1092 &cmc_throttle, 0, sysctl_positive_int, "I",
1093 "Interval in seconds to throttle corrected MC interrupts");
1097 amd_thresholding_setup(void)
1101 amd_et_state = malloc((mp_maxid + 1) * sizeof(struct amd_et_state *),
1103 for (i = 0; i <= mp_maxid; i++)
1104 amd_et_state[i] = malloc(sizeof(struct amd_et_state) *
1105 mca_banks, M_MCA, M_WAITOK | M_ZERO);
1106 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
1107 "cmc_throttle", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
1108 &cmc_throttle, 0, sysctl_positive_int, "I",
1109 "Interval in seconds to throttle corrected MC interrupts");
1114 mca_setup(uint64_t mcg_cap)
1118 * On AMD Family 10h processors, unless logging of level one TLB
1119 * parity (L1TP) errors is disabled, enable the recommended workaround
1122 if (cpu_vendor_id == CPU_VENDOR_AMD &&
1123 CPUID_TO_FAMILY(cpu_id) == 0x10 && amd10h_L1TP)
1124 workaround_erratum383 = 1;
1126 mca_banks = mcg_cap & MCG_CAP_COUNT;
1127 mtx_init(&mca_lock, "mca", NULL, MTX_SPIN);
1128 STAILQ_INIT(&mca_records);
1129 STAILQ_INIT(&mca_pending);
1130 mca_tq = taskqueue_create_fast("mca", M_WAITOK,
1131 taskqueue_thread_enqueue, &mca_tq);
1132 TIMEOUT_TASK_INIT(mca_tq, &mca_scan_task, 0, mca_scan_cpus, NULL);
1133 STAILQ_INIT(&mca_freelist);
1134 TASK_INIT(&mca_resize_task, 0, mca_resize, NULL);
1135 mca_resize_freelist();
1136 SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
1137 "count", CTLFLAG_RD, (int *)(uintptr_t)&mca_count, 0,
1139 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
1140 "maxcount", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
1141 &mca_maxcount, 0, sysctl_mca_maxcount, "I",
1142 "Maximum record count (-1 is unlimited)");
1143 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
1144 "interval", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
1145 &mca_ticks, 0, sysctl_positive_int, "I",
1146 "Periodic interval in seconds to scan for machine checks");
1147 SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
1148 "records", CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mca_records,
1149 "Machine check records");
1150 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
1151 "force_scan", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1152 sysctl_mca_scan, "I", "Force an immediate scan for machine checks");
1154 if (cmci_supported(mcg_cap))
1156 else if (amd_thresholding_supported())
1157 amd_thresholding_setup();
1163 * See if we should monitor CMCI for this bank. If CMCI_EN is already
1164 * set in MC_CTL2, then another CPU is responsible for this bank, so
1165 * ignore it. If CMCI_EN returns zero after being set, then this bank
1166 * does not support CMCI_EN. If this CPU sets CMCI_EN, then it should
1167 * now monitor this bank.
1172 struct cmc_state *cc;
1175 KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
1178 * It is possible for some APs to report CMCI support even if the BSP
1179 * does not, apparently due to a BIOS bug.
1181 if (cmc_state == NULL) {
1184 "AP %d (%d,%d) reports CMCI support but the BSP does not\n",
1185 PCPU_GET(cpuid), PCPU_GET(apic_id),
1191 ctl = rdmsr(MSR_MC_CTL2(i));
1192 if (ctl & MC_CTL2_CMCI_EN)
1193 /* Already monitored by another CPU. */
1196 /* Set the threshold to one event for now. */
1197 ctl &= ~MC_CTL2_THRESHOLD;
1198 ctl |= MC_CTL2_CMCI_EN | 1;
1199 wrmsr(MSR_MC_CTL2(i), ctl);
1200 ctl = rdmsr(MSR_MC_CTL2(i));
1201 if (!(ctl & MC_CTL2_CMCI_EN))
1202 /* This bank does not support CMCI. */
1205 cc = &cmc_state[PCPU_GET(cpuid)][i];
1207 /* Determine maximum threshold. */
1208 ctl &= ~MC_CTL2_THRESHOLD;
1210 wrmsr(MSR_MC_CTL2(i), ctl);
1211 ctl = rdmsr(MSR_MC_CTL2(i));
1212 cc->max_threshold = ctl & MC_CTL2_THRESHOLD;
1214 /* Start off with a threshold of 1. */
1215 ctl &= ~MC_CTL2_THRESHOLD;
1217 wrmsr(MSR_MC_CTL2(i), ctl);
1219 /* Mark this bank as monitored. */
1220 PCPU_SET(cmci_mask, PCPU_GET(cmci_mask) | 1 << i);
1224 * For resume, reset the threshold for any banks we monitor back to
1225 * one and throw away the timestamp of the last interrupt.
1230 struct cmc_state *cc;
1233 KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
1235 /* See cmci_monitor(). */
1236 if (cmc_state == NULL)
1239 /* Ignore banks not monitored by this CPU. */
1240 if (!(PCPU_GET(cmci_mask) & 1 << i))
1243 cc = &cmc_state[PCPU_GET(cpuid)][i];
1245 ctl = rdmsr(MSR_MC_CTL2(i));
1246 ctl &= ~MC_CTL2_THRESHOLD;
1247 ctl |= MC_CTL2_CMCI_EN | 1;
1248 wrmsr(MSR_MC_CTL2(i), ctl);
1252 * Apply an AMD ET configuration to the corresponding MSR.
1255 amd_thresholding_start(struct amd_et_state *cc, int bank)
1259 KASSERT(amd_elvt >= 0, ("ELVT offset is not set"));
1261 misc = rdmsr(mca_msr_ops.misc(bank));
1263 misc &= ~MC_MISC_AMD_INT_MASK;
1264 misc |= MC_MISC_AMD_INT_LVT;
1266 misc &= ~MC_MISC_AMD_LVT_MASK;
1267 misc |= (uint64_t)amd_elvt << MC_MISC_AMD_LVT_SHIFT;
1269 misc &= ~MC_MISC_AMD_CNT_MASK;
1270 misc |= (uint64_t)(MC_MISC_AMD_CNT_MAX - cc->cur_threshold)
1271 << MC_MISC_AMD_CNT_SHIFT;
1273 misc &= ~MC_MISC_AMD_OVERFLOW;
1274 misc |= MC_MISC_AMD_CNTEN;
1276 wrmsr(mca_msr_ops.misc(bank), misc);
1280 amd_thresholding_monitor(int i)
1282 struct amd_et_state *cc;
1286 * Kludge: On 10h, banks after 4 are not thresholding but also may have
1287 * bogus Valid bits. Skip them. This is definitely fixed in 15h, but
1288 * I have not investigated whether it is fixed in earlier models.
1290 if (CPUID_TO_FAMILY(cpu_id) < 0x15 && i >= 5)
1293 /* The counter must be valid and present. */
1294 misc = rdmsr(mca_msr_ops.misc(i));
1295 if ((misc & (MC_MISC_AMD_VAL | MC_MISC_AMD_CNTP)) !=
1296 (MC_MISC_AMD_VAL | MC_MISC_AMD_CNTP))
1299 /* The register should not be locked. */
1300 if ((misc & MC_MISC_AMD_LOCK) != 0) {
1302 printf("%s: 0x%jx: Bank %d: locked\n", __func__,
1303 (uintmax_t)misc, i);
1308 * If counter is enabled then either the firmware or another CPU
1309 * has already claimed it.
1311 if ((misc & MC_MISC_AMD_CNTEN) != 0) {
1313 printf("%s: 0x%jx: Bank %d: already enabled\n",
1314 __func__, (uintmax_t)misc, i);
1319 * Configure an Extended Interrupt LVT register for reporting
1320 * counter overflows if that feature is supported and the first
1321 * extended register is available.
1323 amd_elvt = lapic_enable_mca_elvt();
1325 printf("%s: Bank %d: lapic enable mca elvt failed: %d\n",
1326 __func__, i, amd_elvt);
1330 cc = &amd_et_state[PCPU_GET(cpuid)][i];
1331 cc->cur_threshold = 1;
1332 amd_thresholding_start(cc, i);
1334 /* Mark this bank as monitored. */
1335 PCPU_SET(cmci_mask, PCPU_GET(cmci_mask) | 1 << i);
1339 amd_thresholding_resume(int i)
1341 struct amd_et_state *cc;
1343 KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
1345 /* Ignore banks not monitored by this CPU. */
1346 if (!(PCPU_GET(cmci_mask) & 1 << i))
1349 cc = &amd_et_state[PCPU_GET(cpuid)][i];
1351 cc->cur_threshold = 1;
1352 amd_thresholding_start(cc, i);
1357 * Initializes per-CPU machine check registers and enables corrected
1358 * machine check interrupts.
1365 int i, skip, family;
1367 family = CPUID_TO_FAMILY(cpu_id);
1369 /* MCE is required. */
1370 if (!mca_enabled || !(cpu_feature & CPUID_MCE))
1373 if (cpu_feature & CPUID_MCA) {
1375 PCPU_SET(cmci_mask, 0);
1377 mcg_cap = rdmsr(MSR_MCG_CAP);
1378 if (mcg_cap & MCG_CAP_CTL_P)
1379 /* Enable MCA features. */
1380 wrmsr(MSR_MCG_CTL, MCG_CTL_ENABLE);
1381 if (IS_BSP() && boot)
1385 * Disable logging of level one TLB parity (L1TP) errors by
1386 * the data cache as an alternative workaround for AMD Family
1387 * 10h Erratum 383. Unlike the recommended workaround, there
1388 * is no performance penalty to this workaround. However,
1389 * L1TP errors will go unreported.
1391 if (cpu_vendor_id == CPU_VENDOR_AMD && family == 0x10 &&
1393 mask = rdmsr(MSR_MC0_CTL_MASK);
1394 if ((mask & (1UL << 5)) == 0)
1395 wrmsr(MSR_MC0_CTL_MASK, mask | (1UL << 5));
1397 if (amd_rascap & AMDRAS_SCALABLE_MCA) {
1398 mca_msr_ops.ctl = mca_smca_ctl_reg;
1399 mca_msr_ops.status = mca_smca_status_reg;
1400 mca_msr_ops.addr = mca_smca_addr_reg;
1401 mca_msr_ops.misc = mca_smca_misc_reg;
1404 /* Enable local MCE if supported. */
1405 if (cpu_vendor_id == CPU_VENDOR_INTEL &&
1406 (mcg_cap & MCG_CAP_LMCE_P) &&
1407 (rdmsr(MSR_IA32_FEATURE_CONTROL) &
1408 IA32_FEATURE_CONTROL_LMCE_EN))
1409 wrmsr(MSR_MCG_EXT_CTL, rdmsr(MSR_MCG_EXT_CTL) | 1);
1412 * The cmci_monitor() must not be executed
1413 * simultaneously by several CPUs.
1416 mtx_lock_spin(&mca_lock);
1418 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
1419 /* By default enable logging of all errors. */
1420 ctl = 0xffffffffffffffffUL;
1423 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
1425 * For P6 models before Nehalem MC0_CTL is
1426 * always enabled and reserved.
1428 if (i == 0 && family == 0x6
1429 && CPUID_TO_MODEL(cpu_id) < 0x1a)
1431 } else if (cpu_vendor_id == CPU_VENDOR_AMD) {
1432 /* BKDG for Family 10h: unset GartTblWkEn. */
1433 if (i == MC_AMDNB_BANK && family >= 0xf &&
1435 ctl &= ~(1UL << 10);
1439 wrmsr(mca_msr_ops.ctl(i), ctl);
1442 if (cmci_supported(mcg_cap)) {
1447 } else if (amd_thresholding_supported()) {
1449 amd_thresholding_monitor(i);
1451 amd_thresholding_resume(i);
1455 /* Clear all errors. */
1456 wrmsr(mca_msr_ops.status(i), 0);
1459 mtx_unlock_spin(&mca_lock);
1462 if (cmci_supported(mcg_cap) &&
1463 PCPU_GET(cmci_mask) != 0 && boot)
1468 load_cr4(rcr4() | CR4_MCE);
1471 /* Must be executed on each CPU during boot. */
1479 /* Must be executed on each CPU during resume. */
1488 * The machine check registers for the BSP cannot be initialized until
1489 * the local APIC is initialized. This happens at SI_SUB_CPU,
1493 mca_init_bsp(void *arg __unused)
1498 SYSINIT(mca_init_bsp, SI_SUB_CPU, SI_ORDER_ANY, mca_init_bsp, NULL);
1500 /* Called when a machine check exception fires. */
1504 uint64_t mcg_status;
1506 bool lmcs, recoverable;
1508 if (!(cpu_feature & CPUID_MCA)) {
1510 * Just print the values of the old Pentium registers
1513 printf("MC Type: 0x%jx Address: 0x%jx\n",
1514 (uintmax_t)rdmsr(MSR_P5_MC_TYPE),
1515 (uintmax_t)rdmsr(MSR_P5_MC_ADDR));
1516 panic("Machine check exception");
1519 /* Scan the banks and check for any non-recoverable errors. */
1520 mcg_status = rdmsr(MSR_MCG_STATUS);
1521 recoverable = (mcg_status & MCG_STATUS_RIPV) != 0;
1522 lmcs = (cpu_vendor_id != CPU_VENDOR_INTEL ||
1523 (mcg_status & MCG_STATUS_LMCS));
1524 count = mca_scan(MCE, &recoverable);
1528 * Only panic if the error was detected local to this CPU.
1529 * Some errors will assert a machine check on all CPUs, but
1530 * only certain CPUs will find a valid bank to log.
1532 while (!lmcs && count == 0)
1535 panic("Unrecoverable machine check exception");
1539 wrmsr(MSR_MCG_STATUS, mcg_status & ~MCG_STATUS_MCIP);
1543 /* Called for a CMCI (correctable machine check interrupt). */
1547 bool recoverable = true;
1550 * Serialize MCA bank scanning to prevent collisions from
1553 * If we found anything, log them to the console.
1555 if (mca_scan(CMCI, &recoverable) != 0)
1556 mca_process_records(CMCI);