]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/mca.c
MFV r323678: file 5.32
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / mca.c
1 /*-
2  * Copyright (c) 2009 Hudson River Trading LLC
3  * Written by: John H. Baldwin <jhb@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * Support for x86 machine check architecture.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #ifdef __amd64__
36 #define DEV_APIC
37 #else
38 #include "opt_apic.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/bus.h>
43 #include <sys/interrupt.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/sched.h>
50 #include <sys/smp.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>
58 #include <x86/mca.h>
59 #include <machine/md_var.h>
60 #include <machine/specialreg.h>
61
62 /* Modes for mca_scan() */
63 enum scan_mode {
64         POLLED,
65         MCE,
66         CMCI,
67 };
68
69 #ifdef DEV_APIC
70 /*
71  * State maintained for each monitored MCx bank to control the
72  * corrected machine check interrupt threshold.
73  */
74 struct cmc_state {
75         int     max_threshold;
76         time_t  last_intr;
77 };
78
79 struct amd_et_state {
80         int     cur_threshold;
81         time_t  last_intr;
82 };
83 #endif
84
85 struct mca_internal {
86         struct mca_record rec;
87         int             logged;
88         STAILQ_ENTRY(mca_internal) link;
89 };
90
91 static MALLOC_DEFINE(M_MCA, "MCA", "Machine Check Architecture");
92
93 static volatile int mca_count;  /* Number of records stored. */
94 static int mca_banks;           /* Number of per-CPU register banks. */
95
96 static SYSCTL_NODE(_hw, OID_AUTO, mca, CTLFLAG_RD, NULL,
97     "Machine Check Architecture");
98
99 static int mca_enabled = 1;
100 SYSCTL_INT(_hw_mca, OID_AUTO, enabled, CTLFLAG_RDTUN, &mca_enabled, 0,
101     "Administrative toggle for machine check support");
102
103 static int amd10h_L1TP = 1;
104 SYSCTL_INT(_hw_mca, OID_AUTO, amd10h_L1TP, CTLFLAG_RDTUN, &amd10h_L1TP, 0,
105     "Administrative toggle for logging of level one TLB parity (L1TP) errors");
106
107 static int intel6h_HSD131;
108 SYSCTL_INT(_hw_mca, OID_AUTO, intel6h_HSD131, CTLFLAG_RDTUN, &intel6h_HSD131, 0,
109     "Administrative toggle for logging of spurious corrected errors");
110
111 int workaround_erratum383;
112 SYSCTL_INT(_hw_mca, OID_AUTO, erratum383, CTLFLAG_RDTUN,
113     &workaround_erratum383, 0,
114     "Is the workaround for Erratum 383 on AMD Family 10h processors enabled?");
115
116 static STAILQ_HEAD(, mca_internal) mca_freelist;
117 static int mca_freecount;
118 static STAILQ_HEAD(, mca_internal) mca_records;
119 static struct callout mca_timer;
120 static int mca_ticks = 3600;    /* Check hourly by default. */
121 static struct taskqueue *mca_tq;
122 static struct task mca_refill_task, mca_scan_task;
123 static struct mtx mca_lock;
124
125 #ifdef DEV_APIC
126 static struct cmc_state **cmc_state;            /* Indexed by cpuid, bank. */
127 static struct amd_et_state *amd_et_state;       /* Indexed by cpuid. */
128 static int cmc_throttle = 60;   /* Time in seconds to throttle CMCI. */
129
130 static int amd_elvt = -1;
131
132 static inline bool
133 amd_thresholding_supported(void)
134 {
135         if (cpu_vendor_id != CPU_VENDOR_AMD)
136                 return (false);
137         /*
138          * The RASCap register is wholly reserved in families 0x10-0x15 (through model 1F).
139          *
140          * It begins to be documented in family 0x15 model 30 and family 0x16,
141          * but neither of these families documents the ScalableMca bit, which
142          * supposedly defines the presence of this feature on family 0x17.
143          */
144         if (CPUID_TO_FAMILY(cpu_id) >= 0x10 && CPUID_TO_FAMILY(cpu_id) <= 0x16)
145                 return (true);
146         if (CPUID_TO_FAMILY(cpu_id) >= 0x17)
147                 return ((amd_rascap & AMDRAS_SCALABLE_MCA) != 0);
148         return (false);
149 }
150 #endif
151
152 static inline bool
153 cmci_supported(uint64_t mcg_cap)
154 {
155         /*
156          * MCG_CAP_CMCI_P bit is reserved in AMD documentation.  Until
157          * it is defined, do not use it to check for CMCI support.
158          */
159         if (cpu_vendor_id != CPU_VENDOR_INTEL)
160                 return (false);
161         return ((mcg_cap & MCG_CAP_CMCI_P) != 0);
162 }
163
164 static int
165 sysctl_positive_int(SYSCTL_HANDLER_ARGS)
166 {
167         int error, value;
168
169         value = *(int *)arg1;
170         error = sysctl_handle_int(oidp, &value, 0, req);
171         if (error || req->newptr == NULL)
172                 return (error);
173         if (value <= 0)
174                 return (EINVAL);
175         *(int *)arg1 = value;
176         return (0);
177 }
178
179 static int
180 sysctl_mca_records(SYSCTL_HANDLER_ARGS)
181 {
182         int *name = (int *)arg1;
183         u_int namelen = arg2;
184         struct mca_record record;
185         struct mca_internal *rec;
186         int i;
187
188         if (namelen != 1)
189                 return (EINVAL);
190
191         if (name[0] < 0 || name[0] >= mca_count)
192                 return (EINVAL);
193
194         mtx_lock_spin(&mca_lock);
195         if (name[0] >= mca_count) {
196                 mtx_unlock_spin(&mca_lock);
197                 return (EINVAL);
198         }
199         i = 0;
200         STAILQ_FOREACH(rec, &mca_records, link) {
201                 if (i == name[0]) {
202                         record = rec->rec;
203                         break;
204                 }
205                 i++;
206         }
207         mtx_unlock_spin(&mca_lock);
208         return (SYSCTL_OUT(req, &record, sizeof(record)));
209 }
210
211 static const char *
212 mca_error_ttype(uint16_t mca_error)
213 {
214
215         switch ((mca_error & 0x000c) >> 2) {
216         case 0:
217                 return ("I");
218         case 1:
219                 return ("D");
220         case 2:
221                 return ("G");
222         }
223         return ("?");
224 }
225
226 static const char *
227 mca_error_level(uint16_t mca_error)
228 {
229
230         switch (mca_error & 0x0003) {
231         case 0:
232                 return ("L0");
233         case 1:
234                 return ("L1");
235         case 2:
236                 return ("L2");
237         case 3:
238                 return ("LG");
239         }
240         return ("L?");
241 }
242
243 static const char *
244 mca_error_request(uint16_t mca_error)
245 {
246
247         switch ((mca_error & 0x00f0) >> 4) {
248         case 0x0:
249                 return ("ERR");
250         case 0x1:
251                 return ("RD");
252         case 0x2:
253                 return ("WR");
254         case 0x3:
255                 return ("DRD");
256         case 0x4:
257                 return ("DWR");
258         case 0x5:
259                 return ("IRD");
260         case 0x6:
261                 return ("PREFETCH");
262         case 0x7:
263                 return ("EVICT");
264         case 0x8:
265                 return ("SNOOP");
266         }
267         return ("???");
268 }
269
270 static const char *
271 mca_error_mmtype(uint16_t mca_error)
272 {
273
274         switch ((mca_error & 0x70) >> 4) {
275         case 0x0:
276                 return ("GEN");
277         case 0x1:
278                 return ("RD");
279         case 0x2:
280                 return ("WR");
281         case 0x3:
282                 return ("AC");
283         case 0x4:
284                 return ("MS");
285         }
286         return ("???");
287 }
288
289 static int
290 mca_mute(const struct mca_record *rec)
291 {
292
293         /*
294          * Skip spurious corrected parity errors generated by Intel Haswell-
295          * and Broadwell-based CPUs (see HSD131, HSM142, HSW131 and BDM48
296          * erratum respectively), unless reporting is enabled.
297          * Note that these errors also have been observed with the D0-stepping
298          * of Haswell, while at least initially the CPU specification updates
299          * suggested only the C0-stepping to be affected.  Similarly, Celeron
300          * 2955U with a CPU ID of 0x45 apparently are also concerned with the
301          * same problem, with HSM142 only referring to 0x3c and 0x46.
302          */
303         if (cpu_vendor_id == CPU_VENDOR_INTEL &&
304             CPUID_TO_FAMILY(cpu_id) == 0x6 &&
305             (CPUID_TO_MODEL(cpu_id) == 0x3c ||  /* HSD131, HSM142, HSW131 */
306             CPUID_TO_MODEL(cpu_id) == 0x3d ||   /* BDM48 */
307             CPUID_TO_MODEL(cpu_id) == 0x45 ||
308             CPUID_TO_MODEL(cpu_id) == 0x46) &&  /* HSM142 */
309             rec->mr_bank == 0 &&
310             (rec->mr_status & 0xa0000000ffffffff) == 0x80000000000f0005 &&
311             !intel6h_HSD131)
312                 return (1);
313
314         return (0);
315 }
316
317 /* Dump details about a single machine check. */
318 static void
319 mca_log(const struct mca_record *rec)
320 {
321         uint16_t mca_error;
322
323         if (mca_mute(rec))
324                 return;
325
326         printf("MCA: Bank %d, Status 0x%016llx\n", rec->mr_bank,
327             (long long)rec->mr_status);
328         printf("MCA: Global Cap 0x%016llx, Status 0x%016llx\n",
329             (long long)rec->mr_mcg_cap, (long long)rec->mr_mcg_status);
330         printf("MCA: Vendor \"%s\", ID 0x%x, APIC ID %d\n", cpu_vendor,
331             rec->mr_cpu_id, rec->mr_apic_id);
332         printf("MCA: CPU %d ", rec->mr_cpu);
333         if (rec->mr_status & MC_STATUS_UC)
334                 printf("UNCOR ");
335         else {
336                 printf("COR ");
337                 if (cmci_supported(rec->mr_mcg_cap))
338                         printf("(%lld) ", ((long long)rec->mr_status &
339                             MC_STATUS_COR_COUNT) >> 38);
340         }
341         if (rec->mr_status & MC_STATUS_PCC)
342                 printf("PCC ");
343         if (rec->mr_status & MC_STATUS_OVER)
344                 printf("OVER ");
345         mca_error = rec->mr_status & MC_STATUS_MCA_ERROR;
346         switch (mca_error) {
347                 /* Simple error codes. */
348         case 0x0000:
349                 printf("no error");
350                 break;
351         case 0x0001:
352                 printf("unclassified error");
353                 break;
354         case 0x0002:
355                 printf("ucode ROM parity error");
356                 break;
357         case 0x0003:
358                 printf("external error");
359                 break;
360         case 0x0004:
361                 printf("FRC error");
362                 break;
363         case 0x0005:
364                 printf("internal parity error");
365                 break;
366         case 0x0400:
367                 printf("internal timer error");
368                 break;
369         default:
370                 if ((mca_error & 0xfc00) == 0x0400) {
371                         printf("internal error %x", mca_error & 0x03ff);
372                         break;
373                 }
374
375                 /* Compound error codes. */
376
377                 /* Memory hierarchy error. */
378                 if ((mca_error & 0xeffc) == 0x000c) {
379                         printf("%s memory error", mca_error_level(mca_error));
380                         break;
381                 }
382
383                 /* TLB error. */
384                 if ((mca_error & 0xeff0) == 0x0010) {
385                         printf("%sTLB %s error", mca_error_ttype(mca_error),
386                             mca_error_level(mca_error));
387                         break;
388                 }
389
390                 /* Memory controller error. */
391                 if ((mca_error & 0xef80) == 0x0080) {
392                         printf("%s channel ", mca_error_mmtype(mca_error));
393                         if ((mca_error & 0x000f) != 0x000f)
394                                 printf("%d", mca_error & 0x000f);
395                         else
396                                 printf("??");
397                         printf(" memory error");
398                         break;
399                 }
400                 
401                 /* Cache error. */
402                 if ((mca_error & 0xef00) == 0x0100) {
403                         printf("%sCACHE %s %s error",
404                             mca_error_ttype(mca_error),
405                             mca_error_level(mca_error),
406                             mca_error_request(mca_error));
407                         break;
408                 }
409
410                 /* Bus and/or Interconnect error. */
411                 if ((mca_error & 0xe800) == 0x0800) {                   
412                         printf("BUS%s ", mca_error_level(mca_error));
413                         switch ((mca_error & 0x0600) >> 9) {
414                         case 0:
415                                 printf("Source");
416                                 break;
417                         case 1:
418                                 printf("Responder");
419                                 break;
420                         case 2:
421                                 printf("Observer");
422                                 break;
423                         default:
424                                 printf("???");
425                                 break;
426                         }
427                         printf(" %s ", mca_error_request(mca_error));
428                         switch ((mca_error & 0x000c) >> 2) {
429                         case 0:
430                                 printf("Memory");
431                                 break;
432                         case 2:
433                                 printf("I/O");
434                                 break;
435                         case 3:
436                                 printf("Other");
437                                 break;
438                         default:
439                                 printf("???");
440                                 break;
441                         }
442                         if (mca_error & 0x0100)
443                                 printf(" timed out");
444                         break;
445                 }
446
447                 printf("unknown error %x", mca_error);
448                 break;
449         }
450         printf("\n");
451         if (rec->mr_status & MC_STATUS_ADDRV)
452                 printf("MCA: Address 0x%llx\n", (long long)rec->mr_addr);
453         if (rec->mr_status & MC_STATUS_MISCV)
454                 printf("MCA: Misc 0x%llx\n", (long long)rec->mr_misc);
455 }
456
457 static int
458 mca_check_status(int bank, struct mca_record *rec)
459 {
460         uint64_t status;
461         u_int p[4];
462
463         status = rdmsr(MSR_MC_STATUS(bank));
464         if (!(status & MC_STATUS_VAL))
465                 return (0);
466
467         /* Save exception information. */
468         rec->mr_status = status;
469         rec->mr_bank = bank;
470         rec->mr_addr = 0;
471         if (status & MC_STATUS_ADDRV)
472                 rec->mr_addr = rdmsr(MSR_MC_ADDR(bank));
473         rec->mr_misc = 0;
474         if (status & MC_STATUS_MISCV)
475                 rec->mr_misc = rdmsr(MSR_MC_MISC(bank));
476         rec->mr_tsc = rdtsc();
477         rec->mr_apic_id = PCPU_GET(apic_id);
478         rec->mr_mcg_cap = rdmsr(MSR_MCG_CAP);
479         rec->mr_mcg_status = rdmsr(MSR_MCG_STATUS);
480         rec->mr_cpu_id = cpu_id;
481         rec->mr_cpu_vendor_id = cpu_vendor_id;
482         rec->mr_cpu = PCPU_GET(cpuid);
483
484         /*
485          * Clear machine check.  Don't do this for uncorrectable
486          * errors so that the BIOS can see them.
487          */
488         if (!(rec->mr_status & (MC_STATUS_PCC | MC_STATUS_UC))) {
489                 wrmsr(MSR_MC_STATUS(bank), 0);
490                 do_cpuid(0, p);
491         }
492         return (1);
493 }
494
495 static void
496 mca_fill_freelist(void)
497 {
498         struct mca_internal *rec;
499         int desired;
500
501         /*
502          * Ensure we have at least one record for each bank and one
503          * record per CPU.
504          */
505         desired = imax(mp_ncpus, mca_banks);
506         mtx_lock_spin(&mca_lock);
507         while (mca_freecount < desired) {
508                 mtx_unlock_spin(&mca_lock);
509                 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
510                 mtx_lock_spin(&mca_lock);
511                 STAILQ_INSERT_TAIL(&mca_freelist, rec, link);
512                 mca_freecount++;
513         }
514         mtx_unlock_spin(&mca_lock);
515 }
516
517 static void
518 mca_refill(void *context, int pending)
519 {
520
521         mca_fill_freelist();
522 }
523
524 static void
525 mca_record_entry(enum scan_mode mode, const struct mca_record *record)
526 {
527         struct mca_internal *rec;
528
529         if (mode == POLLED) {
530                 rec = malloc(sizeof(*rec), M_MCA, M_WAITOK);
531                 mtx_lock_spin(&mca_lock);
532         } else {
533                 mtx_lock_spin(&mca_lock);
534                 rec = STAILQ_FIRST(&mca_freelist);
535                 if (rec == NULL) {
536                         printf("MCA: Unable to allocate space for an event.\n");
537                         mca_log(record);
538                         mtx_unlock_spin(&mca_lock);
539                         return;
540                 }
541                 STAILQ_REMOVE_HEAD(&mca_freelist, link);
542                 mca_freecount--;
543         }
544
545         rec->rec = *record;
546         rec->logged = 0;
547         STAILQ_INSERT_TAIL(&mca_records, rec, link);
548         mca_count++;
549         mtx_unlock_spin(&mca_lock);
550         if (mode == CMCI && !cold)
551                 taskqueue_enqueue(mca_tq, &mca_refill_task);
552 }
553
554 #ifdef DEV_APIC
555 /*
556  * Update the interrupt threshold for a CMCI.  The strategy is to use
557  * a low trigger that interrupts as soon as the first event occurs.
558  * However, if a steady stream of events arrive, the threshold is
559  * increased until the interrupts are throttled to once every
560  * cmc_throttle seconds or the periodic scan.  If a periodic scan
561  * finds that the threshold is too high, it is lowered.
562  */
563 static int
564 update_threshold(enum scan_mode mode, int valid, int last_intr, int count,
565     int cur_threshold, int max_threshold)
566 {
567         u_int delta;
568         int limit;
569
570         delta = (u_int)(time_uptime - last_intr);
571         limit = cur_threshold;
572
573         /*
574          * If an interrupt was received less than cmc_throttle seconds
575          * since the previous interrupt and the count from the current
576          * event is greater than or equal to the current threshold,
577          * double the threshold up to the max.
578          */
579         if (mode == CMCI && valid) {
580                 if (delta < cmc_throttle && count >= limit &&
581                     limit < max_threshold) {
582                         limit = min(limit << 1, max_threshold);
583                 }
584                 return (limit);
585         }
586
587         /*
588          * When the banks are polled, check to see if the threshold
589          * should be lowered.
590          */
591         if (mode != POLLED)
592                 return (limit);
593
594         /* If a CMCI occured recently, do nothing for now. */
595         if (delta < cmc_throttle)
596                 return (limit);
597
598         /*
599          * Compute a new limit based on the average rate of events per
600          * cmc_throttle seconds since the last interrupt.
601          */
602         if (valid) {
603                 limit = count * cmc_throttle / delta;
604                 if (limit <= 0)
605                         limit = 1;
606                 else if (limit > max_threshold)
607                         limit = max_threshold;
608         } else {
609                 limit = 1;
610         }
611         return (limit);
612 }
613
614 static void
615 cmci_update(enum scan_mode mode, int bank, int valid, struct mca_record *rec)
616 {
617         struct cmc_state *cc;
618         uint64_t ctl;
619         int cur_threshold, new_threshold;
620         int count;
621
622         /* Fetch the current limit for this bank. */
623         cc = &cmc_state[PCPU_GET(cpuid)][bank];
624         ctl = rdmsr(MSR_MC_CTL2(bank));
625         count = (rec->mr_status & MC_STATUS_COR_COUNT) >> 38;
626         cur_threshold = ctl & MC_CTL2_THRESHOLD;
627
628         new_threshold = update_threshold(mode, valid, cc->last_intr, count,
629             cur_threshold, cc->max_threshold);
630
631         if (mode == CMCI && valid)
632                 cc->last_intr = time_uptime;
633         if (new_threshold != cur_threshold) {
634                 ctl &= ~MC_CTL2_THRESHOLD;
635                 ctl |= new_threshold;
636                 wrmsr(MSR_MC_CTL2(bank), ctl);
637         }
638 }
639
640 static void
641 amd_thresholding_update(enum scan_mode mode, int bank, int valid)
642 {
643         struct amd_et_state *cc;
644         uint64_t misc;
645         int new_threshold;
646         int count;
647
648         KASSERT(bank == MC_AMDNB_BANK,
649             ("%s: unexpected bank %d", __func__, bank));
650         cc = &amd_et_state[PCPU_GET(cpuid)];
651         misc = rdmsr(MSR_MC_MISC(bank));
652         count = (misc & MC_MISC_AMD_CNT_MASK) >> MC_MISC_AMD_CNT_SHIFT;
653         count = count - (MC_MISC_AMD_CNT_MAX - cc->cur_threshold);
654
655         new_threshold = update_threshold(mode, valid, cc->last_intr, count,
656             cc->cur_threshold, MC_MISC_AMD_CNT_MAX);
657
658         cc->cur_threshold = new_threshold;
659         misc &= ~MC_MISC_AMD_CNT_MASK;
660         misc |= (uint64_t)(MC_MISC_AMD_CNT_MAX - cc->cur_threshold)
661             << MC_MISC_AMD_CNT_SHIFT;
662         misc &= ~MC_MISC_AMD_OVERFLOW;
663         wrmsr(MSR_MC_MISC(bank), misc);
664         if (mode == CMCI && valid)
665                 cc->last_intr = time_uptime;
666 }
667 #endif
668
669 /*
670  * This scans all the machine check banks of the current CPU to see if
671  * there are any machine checks.  Any non-recoverable errors are
672  * reported immediately via mca_log().  The current thread must be
673  * pinned when this is called.  The 'mode' parameter indicates if we
674  * are being called from the MC exception handler, the CMCI handler,
675  * or the periodic poller.  In the MC exception case this function
676  * returns true if the system is restartable.  Otherwise, it returns a
677  * count of the number of valid MC records found.
678  */
679 static int
680 mca_scan(enum scan_mode mode, int *recoverablep)
681 {
682         struct mca_record rec;
683         uint64_t mcg_cap, ucmask;
684         int count, i, recoverable, valid;
685
686         count = 0;
687         recoverable = 1;
688         ucmask = MC_STATUS_UC | MC_STATUS_PCC;
689
690         /* When handling a MCE#, treat the OVER flag as non-restartable. */
691         if (mode == MCE)
692                 ucmask |= MC_STATUS_OVER;
693         mcg_cap = rdmsr(MSR_MCG_CAP);
694         for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
695 #ifdef DEV_APIC
696                 /*
697                  * For a CMCI, only check banks this CPU is
698                  * responsible for.
699                  */
700                 if (mode == CMCI && !(PCPU_GET(cmci_mask) & 1 << i))
701                         continue;
702 #endif
703
704                 valid = mca_check_status(i, &rec);
705                 if (valid) {
706                         count++;
707                         if (rec.mr_status & ucmask) {
708                                 recoverable = 0;
709                                 mtx_lock_spin(&mca_lock);
710                                 mca_log(&rec);
711                                 mtx_unlock_spin(&mca_lock);
712                         }
713                         mca_record_entry(mode, &rec);
714                 }
715         
716 #ifdef DEV_APIC
717                 /*
718                  * If this is a bank this CPU monitors via CMCI,
719                  * update the threshold.
720                  */
721                 if (PCPU_GET(cmci_mask) & 1 << i) {
722                         if (cmc_state != NULL)
723                                 cmci_update(mode, i, valid, &rec);
724                         else
725                                 amd_thresholding_update(mode, i, valid);
726                 }
727 #endif
728         }
729         if (mode == POLLED)
730                 mca_fill_freelist();
731         if (recoverablep != NULL)
732                 *recoverablep = recoverable;
733         return (count);
734 }
735
736 /*
737  * Scan the machine check banks on all CPUs by binding to each CPU in
738  * turn.  If any of the CPUs contained new machine check records, log
739  * them to the console.
740  */
741 static void
742 mca_scan_cpus(void *context, int pending)
743 {
744         struct mca_internal *mca;
745         struct thread *td;
746         int count, cpu;
747
748         mca_fill_freelist();
749         td = curthread;
750         count = 0;
751         thread_lock(td);
752         CPU_FOREACH(cpu) {
753                 sched_bind(td, cpu);
754                 thread_unlock(td);
755                 count += mca_scan(POLLED, NULL);
756                 thread_lock(td);
757                 sched_unbind(td);
758         }
759         thread_unlock(td);
760         if (count != 0) {
761                 mtx_lock_spin(&mca_lock);
762                 STAILQ_FOREACH(mca, &mca_records, link) {
763                         if (!mca->logged) {
764                                 mca->logged = 1;
765                                 mca_log(&mca->rec);
766                         }
767                 }
768                 mtx_unlock_spin(&mca_lock);
769         }
770 }
771
772 static void
773 mca_periodic_scan(void *arg)
774 {
775
776         taskqueue_enqueue(mca_tq, &mca_scan_task);
777         callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
778 }
779
780 static int
781 sysctl_mca_scan(SYSCTL_HANDLER_ARGS)
782 {
783         int error, i;
784
785         i = 0;
786         error = sysctl_handle_int(oidp, &i, 0, req);
787         if (error)
788                 return (error);
789         if (i)
790                 taskqueue_enqueue(mca_tq, &mca_scan_task);
791         return (0);
792 }
793
794 static void
795 mca_createtq(void *dummy)
796 {
797         if (mca_banks <= 0)
798                 return;
799
800         mca_tq = taskqueue_create_fast("mca", M_WAITOK,
801             taskqueue_thread_enqueue, &mca_tq);
802         taskqueue_start_threads(&mca_tq, 1, PI_SWI(SWI_TQ), "mca taskq");
803
804         /* CMCIs during boot may have claimed items from the freelist. */
805         mca_fill_freelist();
806 }
807 SYSINIT(mca_createtq, SI_SUB_CONFIGURE, SI_ORDER_ANY, mca_createtq, NULL);
808
809 static void
810 mca_startup(void *dummy)
811 {
812
813         if (mca_banks <= 0)
814                 return;
815
816         callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
817 }
818 #ifdef EARLY_AP_STARTUP
819 SYSINIT(mca_startup, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, mca_startup, NULL);
820 #else
821 SYSINIT(mca_startup, SI_SUB_SMP, SI_ORDER_ANY, mca_startup, NULL);
822 #endif
823
824 #ifdef DEV_APIC
825 static void
826 cmci_setup(void)
827 {
828         int i;
829
830         cmc_state = malloc((mp_maxid + 1) * sizeof(struct cmc_state *), M_MCA,
831             M_WAITOK);
832         for (i = 0; i <= mp_maxid; i++)
833                 cmc_state[i] = malloc(sizeof(struct cmc_state) * mca_banks,
834                     M_MCA, M_WAITOK | M_ZERO);
835         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
836             "cmc_throttle", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
837             &cmc_throttle, 0, sysctl_positive_int, "I",
838             "Interval in seconds to throttle corrected MC interrupts");
839 }
840
841 static void
842 amd_thresholding_setup(void)
843 {
844
845         amd_et_state = malloc((mp_maxid + 1) * sizeof(struct amd_et_state),
846             M_MCA, M_WAITOK | M_ZERO);
847         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
848             "cmc_throttle", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
849             &cmc_throttle, 0, sysctl_positive_int, "I",
850             "Interval in seconds to throttle corrected MC interrupts");
851 }
852 #endif
853
854 static void
855 mca_setup(uint64_t mcg_cap)
856 {
857
858         /*
859          * On AMD Family 10h processors, unless logging of level one TLB
860          * parity (L1TP) errors is disabled, enable the recommended workaround
861          * for Erratum 383.
862          */
863         if (cpu_vendor_id == CPU_VENDOR_AMD &&
864             CPUID_TO_FAMILY(cpu_id) == 0x10 && amd10h_L1TP)
865                 workaround_erratum383 = 1;
866
867         mca_banks = mcg_cap & MCG_CAP_COUNT;
868         mtx_init(&mca_lock, "mca", NULL, MTX_SPIN);
869         STAILQ_INIT(&mca_records);
870         TASK_INIT(&mca_scan_task, 0, mca_scan_cpus, NULL);
871         callout_init(&mca_timer, 1);
872         STAILQ_INIT(&mca_freelist);
873         TASK_INIT(&mca_refill_task, 0, mca_refill, NULL);
874         mca_fill_freelist();
875         SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
876             "count", CTLFLAG_RD, (int *)(uintptr_t)&mca_count, 0,
877             "Record count");
878         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
879             "interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &mca_ticks,
880             0, sysctl_positive_int, "I",
881             "Periodic interval in seconds to scan for machine checks");
882         SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
883             "records", CTLFLAG_RD, sysctl_mca_records, "Machine check records");
884         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
885             "force_scan", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
886             sysctl_mca_scan, "I", "Force an immediate scan for machine checks");
887 #ifdef DEV_APIC
888         if (cmci_supported(mcg_cap))
889                 cmci_setup();
890         else if (amd_thresholding_supported())
891                 amd_thresholding_setup();
892 #endif
893 }
894
895 #ifdef DEV_APIC
896 /*
897  * See if we should monitor CMCI for this bank.  If CMCI_EN is already
898  * set in MC_CTL2, then another CPU is responsible for this bank, so
899  * ignore it.  If CMCI_EN returns zero after being set, then this bank
900  * does not support CMCI_EN.  If this CPU sets CMCI_EN, then it should
901  * now monitor this bank.
902  */
903 static void
904 cmci_monitor(int i)
905 {
906         struct cmc_state *cc;
907         uint64_t ctl;
908
909         KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
910
911         ctl = rdmsr(MSR_MC_CTL2(i));
912         if (ctl & MC_CTL2_CMCI_EN)
913                 /* Already monitored by another CPU. */
914                 return;
915
916         /* Set the threshold to one event for now. */
917         ctl &= ~MC_CTL2_THRESHOLD;
918         ctl |= MC_CTL2_CMCI_EN | 1;
919         wrmsr(MSR_MC_CTL2(i), ctl);
920         ctl = rdmsr(MSR_MC_CTL2(i));
921         if (!(ctl & MC_CTL2_CMCI_EN))
922                 /* This bank does not support CMCI. */
923                 return;
924
925         cc = &cmc_state[PCPU_GET(cpuid)][i];
926
927         /* Determine maximum threshold. */
928         ctl &= ~MC_CTL2_THRESHOLD;
929         ctl |= 0x7fff;
930         wrmsr(MSR_MC_CTL2(i), ctl);
931         ctl = rdmsr(MSR_MC_CTL2(i));
932         cc->max_threshold = ctl & MC_CTL2_THRESHOLD;
933
934         /* Start off with a threshold of 1. */
935         ctl &= ~MC_CTL2_THRESHOLD;
936         ctl |= 1;
937         wrmsr(MSR_MC_CTL2(i), ctl);
938
939         /* Mark this bank as monitored. */
940         PCPU_SET(cmci_mask, PCPU_GET(cmci_mask) | 1 << i);
941 }
942
943 /*
944  * For resume, reset the threshold for any banks we monitor back to
945  * one and throw away the timestamp of the last interrupt.
946  */
947 static void
948 cmci_resume(int i)
949 {
950         struct cmc_state *cc;
951         uint64_t ctl;
952
953         KASSERT(i < mca_banks, ("CPU %d has more MC banks", PCPU_GET(cpuid)));
954
955         /* Ignore banks not monitored by this CPU. */
956         if (!(PCPU_GET(cmci_mask) & 1 << i))
957                 return;
958
959         cc = &cmc_state[PCPU_GET(cpuid)][i];
960         cc->last_intr = 0;
961         ctl = rdmsr(MSR_MC_CTL2(i));
962         ctl &= ~MC_CTL2_THRESHOLD;
963         ctl |= MC_CTL2_CMCI_EN | 1;
964         wrmsr(MSR_MC_CTL2(i), ctl);
965 }
966
967 static void
968 amd_thresholding_start(struct amd_et_state *cc)
969 {
970         uint64_t misc;
971
972         KASSERT(amd_elvt >= 0, ("ELVT offset is not set"));
973         misc = rdmsr(MSR_MC_MISC(MC_AMDNB_BANK));
974         misc &= ~MC_MISC_AMD_INT_MASK;
975         misc |= MC_MISC_AMD_INT_LVT;
976         misc &= ~MC_MISC_AMD_LVT_MASK;
977         misc |= (uint64_t)amd_elvt << MC_MISC_AMD_LVT_SHIFT;
978         misc &= ~MC_MISC_AMD_CNT_MASK;
979         misc |= (uint64_t)(MC_MISC_AMD_CNT_MAX - cc->cur_threshold)
980             << MC_MISC_AMD_CNT_SHIFT;
981         misc &= ~MC_MISC_AMD_OVERFLOW;
982         misc |= MC_MISC_AMD_CNTEN;
983
984         wrmsr(MSR_MC_MISC(MC_AMDNB_BANK), misc);
985 }
986
987 static void
988 amd_thresholding_init(void)
989 {
990         struct amd_et_state *cc;
991         uint64_t misc;
992
993         /* The counter must be valid and present. */
994         misc = rdmsr(MSR_MC_MISC(MC_AMDNB_BANK));
995         if ((misc & (MC_MISC_AMD_VAL | MC_MISC_AMD_CNTP)) !=
996             (MC_MISC_AMD_VAL | MC_MISC_AMD_CNTP)) {
997                 printf("%s: 0x%jx: !valid | !present\n", __func__,
998                     (uintmax_t)misc);
999                 return;
1000         }
1001
1002         /* The register should not be locked. */
1003         if ((misc & MC_MISC_AMD_LOCK) != 0) {
1004                 printf("%s: 0x%jx: locked\n", __func__, (uintmax_t)misc);
1005                 return;
1006         }
1007
1008         /*
1009          * If counter is enabled then either the firmware or another CPU
1010          * has already claimed it.
1011          */
1012         if ((misc & MC_MISC_AMD_CNTEN) != 0) {
1013                 printf("%s: 0x%jx: count already enabled\n", __func__,
1014                     (uintmax_t)misc);
1015                 return;
1016         }
1017
1018         /*
1019          * Configure an Extended Interrupt LVT register for reporting
1020          * counter overflows if that feature is supported and the first
1021          * extended register is available.
1022          */
1023         amd_elvt = lapic_enable_mca_elvt();
1024         if (amd_elvt < 0) {
1025                 printf("%s: lapic enable mca elvt failed: %d\n", __func__, amd_elvt);
1026                 return;
1027         }
1028
1029         /* Re-use Intel CMC support infrastructure. */
1030         if (bootverbose)
1031                 printf("%s: Starting AMD thresholding\n", __func__);
1032
1033         cc = &amd_et_state[PCPU_GET(cpuid)];
1034         cc->cur_threshold = 1;
1035         amd_thresholding_start(cc);
1036
1037         /* Mark the NB bank as monitored. */
1038         PCPU_SET(cmci_mask, PCPU_GET(cmci_mask) | 1 << MC_AMDNB_BANK);
1039 }
1040
1041 static void
1042 amd_thresholding_resume(void)
1043 {
1044         struct amd_et_state *cc;
1045
1046         /* Nothing to do if this CPU doesn't monitor the NB bank. */
1047         if ((PCPU_GET(cmci_mask) & 1 << MC_AMDNB_BANK) == 0)
1048                 return;
1049
1050         cc = &amd_et_state[PCPU_GET(cpuid)];
1051         cc->last_intr = 0;
1052         cc->cur_threshold = 1;
1053         amd_thresholding_start(cc);
1054 }
1055 #endif
1056
1057 /*
1058  * Initializes per-CPU machine check registers and enables corrected
1059  * machine check interrupts.
1060  */
1061 static void
1062 _mca_init(int boot)
1063 {
1064         uint64_t mcg_cap;
1065         uint64_t ctl, mask;
1066         int i, skip;
1067
1068         /* MCE is required. */
1069         if (!mca_enabled || !(cpu_feature & CPUID_MCE))
1070                 return;
1071
1072         if (cpu_feature & CPUID_MCA) {
1073                 if (boot)
1074                         PCPU_SET(cmci_mask, 0);
1075
1076                 mcg_cap = rdmsr(MSR_MCG_CAP);
1077                 if (mcg_cap & MCG_CAP_CTL_P)
1078                         /* Enable MCA features. */
1079                         wrmsr(MSR_MCG_CTL, MCG_CTL_ENABLE);
1080                 if (PCPU_GET(cpuid) == 0 && boot)
1081                         mca_setup(mcg_cap);
1082
1083                 /*
1084                  * Disable logging of level one TLB parity (L1TP) errors by
1085                  * the data cache as an alternative workaround for AMD Family
1086                  * 10h Erratum 383.  Unlike the recommended workaround, there
1087                  * is no performance penalty to this workaround.  However,
1088                  * L1TP errors will go unreported.
1089                  */
1090                 if (cpu_vendor_id == CPU_VENDOR_AMD &&
1091                     CPUID_TO_FAMILY(cpu_id) == 0x10 && !amd10h_L1TP) {
1092                         mask = rdmsr(MSR_MC0_CTL_MASK);
1093                         if ((mask & (1UL << 5)) == 0)
1094                                 wrmsr(MSR_MC0_CTL_MASK, mask | (1UL << 5));
1095                 }
1096                 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
1097                         /* By default enable logging of all errors. */
1098                         ctl = 0xffffffffffffffffUL;
1099                         skip = 0;
1100
1101                         if (cpu_vendor_id == CPU_VENDOR_INTEL) {
1102                                 /*
1103                                  * For P6 models before Nehalem MC0_CTL is
1104                                  * always enabled and reserved.
1105                                  */
1106                                 if (i == 0 && CPUID_TO_FAMILY(cpu_id) == 0x6
1107                                     && CPUID_TO_MODEL(cpu_id) < 0x1a)
1108                                         skip = 1;
1109                         } else if (cpu_vendor_id == CPU_VENDOR_AMD) {
1110                                 /* BKDG for Family 10h: unset GartTblWkEn. */
1111                                 if (i == 4 && CPUID_TO_FAMILY(cpu_id) >= 0xf)
1112                                         ctl &= ~(1UL << 10);
1113                         }
1114
1115                         if (!skip)
1116                                 wrmsr(MSR_MC_CTL(i), ctl);
1117
1118 #ifdef DEV_APIC
1119                         if (cmci_supported(mcg_cap)) {
1120                                 if (boot)
1121                                         cmci_monitor(i);
1122                                 else
1123                                         cmci_resume(i);
1124                         }
1125 #endif
1126
1127                         /* Clear all errors. */
1128                         wrmsr(MSR_MC_STATUS(i), 0);
1129                 }
1130
1131 #ifdef DEV_APIC
1132                 /*
1133                  * AMD Processors from families 10h - 16h provide support
1134                  * for Machine Check Error Thresholding.
1135                  * The processors support counters of MC errors and they
1136                  * can be configured to generate an interrupt when a counter
1137                  * overflows.
1138                  * The counters are all associated with Bank 4 and each
1139                  * of them covers a group of errors reported via that bank.
1140                  * At the moment only the DRAM Error Threshold Group is
1141                  * supported.
1142                  */
1143                 if (amd_thresholding_supported() &&
1144                     (mcg_cap & MCG_CAP_COUNT) >= 4) {
1145                         if (boot)
1146                                 amd_thresholding_init();
1147                         else
1148                                 amd_thresholding_resume();
1149                 } else if (PCPU_GET(cmci_mask) != 0 && boot) {
1150                         lapic_enable_cmc();
1151                 }
1152 #endif
1153         }
1154
1155         load_cr4(rcr4() | CR4_MCE);
1156 }
1157
1158 /* Must be executed on each CPU during boot. */
1159 void
1160 mca_init(void)
1161 {
1162
1163         _mca_init(1);
1164 }
1165
1166 /* Must be executed on each CPU during resume. */
1167 void
1168 mca_resume(void)
1169 {
1170
1171         _mca_init(0);
1172 }
1173
1174 /*
1175  * The machine check registers for the BSP cannot be initialized until
1176  * the local APIC is initialized.  This happens at SI_SUB_CPU,
1177  * SI_ORDER_SECOND.
1178  */
1179 static void
1180 mca_init_bsp(void *arg __unused)
1181 {
1182
1183         mca_init();
1184 }
1185 SYSINIT(mca_init_bsp, SI_SUB_CPU, SI_ORDER_ANY, mca_init_bsp, NULL);
1186
1187 /* Called when a machine check exception fires. */
1188 void
1189 mca_intr(void)
1190 {
1191         uint64_t mcg_status;
1192         int recoverable, count;
1193
1194         if (!(cpu_feature & CPUID_MCA)) {
1195                 /*
1196                  * Just print the values of the old Pentium registers
1197                  * and panic.
1198                  */
1199                 printf("MC Type: 0x%jx  Address: 0x%jx\n",
1200                     (uintmax_t)rdmsr(MSR_P5_MC_TYPE),
1201                     (uintmax_t)rdmsr(MSR_P5_MC_ADDR));
1202                 panic("Machine check");
1203         }
1204
1205         /* Scan the banks and check for any non-recoverable errors. */
1206         count = mca_scan(MCE, &recoverable);
1207         mcg_status = rdmsr(MSR_MCG_STATUS);
1208         if (!(mcg_status & MCG_STATUS_RIPV))
1209                 recoverable = 0;
1210
1211         if (!recoverable) {
1212                 /*
1213                  * Only panic if the error was detected local to this CPU.
1214                  * Some errors will assert a machine check on all CPUs, but
1215                  * only certain CPUs will find a valid bank to log.
1216                  */
1217                 while (count == 0)
1218                         cpu_spinwait();
1219
1220                 panic("Unrecoverable machine check exception");
1221         }
1222
1223         /* Clear MCIP. */
1224         wrmsr(MSR_MCG_STATUS, mcg_status & ~MCG_STATUS_MCIP);
1225 }
1226
1227 #ifdef DEV_APIC
1228 /* Called for a CMCI (correctable machine check interrupt). */
1229 void
1230 cmc_intr(void)
1231 {
1232         struct mca_internal *mca;
1233         int count;
1234
1235         /*
1236          * Serialize MCA bank scanning to prevent collisions from
1237          * sibling threads.
1238          */
1239         count = mca_scan(CMCI, NULL);
1240
1241         /* If we found anything, log them to the console. */
1242         if (count != 0) {
1243                 mtx_lock_spin(&mca_lock);
1244                 STAILQ_FOREACH(mca, &mca_records, link) {
1245                         if (!mca->logged) {
1246                                 mca->logged = 1;
1247                                 mca_log(&mca->rec);
1248                         }
1249                 }
1250                 mtx_unlock_spin(&mca_lock);
1251         }
1252 }
1253 #endif