xref: /illumos-gate/usr/src/uts/i86pc/cpu/amd_opteron/ao_mca.c (revision aedf2b3bb56b025fcaf87b49ec6c8aeea07f16d7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/regset.h>
29 #include <sys/privregs.h>
30 #include <sys/pci_impl.h>
31 #include <sys/cpuvar.h>
32 #include <sys/x86_archext.h>
33 #include <sys/cmn_err.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/pghw.h>
37 #include <sys/cyclic.h>
38 #include <sys/sysevent.h>
39 #include <sys/smbios.h>
40 #include <sys/mca_x86.h>
41 #include <sys/mca_amd.h>
42 #include <sys/mc.h>
43 #include <sys/mc_amd.h>
44 #include <sys/psw.h>
45 #include <sys/ddi.h>
46 #include <sys/sunddi.h>
47 #include <sys/sdt.h>
48 #include <sys/fm/util.h>
49 #include <sys/fm/protocol.h>
50 #include <sys/fm/cpu/AMD.h>
51 #include <sys/fm/smb/fmsmb.h>
52 #include <sys/acpi/acpi.h>
53 #include <sys/acpi/acpi_pci.h>
54 #include <sys/acpica.h>
55 #include <sys/cpu_module.h>
56 
57 #include "ao.h"
58 #include "ao_mca_disp.h"
59 
60 #define	AO_F_REVS_FG (X86_CHIPREV_AMD_F_REV_F | X86_CHIPREV_AMD_F_REV_G)
61 
62 int ao_mca_smi_disable = 1;		/* attempt to disable SMI polling */
63 
64 extern int x86gentopo_legacy;	/* x86 generic topology support */
65 
66 struct ao_ctl_init {
67 	uint32_t ctl_revmask;	/* rev(s) to which this applies */
68 	uint64_t ctl_bits;	/* mca ctl reg bitmask to set */
69 };
70 
71 /*
72  * Additional NB MCA ctl initialization for revs F and G
73  */
74 static const struct ao_ctl_init ao_nb_ctl_init[] = {
75 	{ AO_F_REVS_FG, AMD_NB_CTL_INIT_REV_FG },
76 	{ X86_CHIPREV_UNKNOWN, 0 }
77 };
78 
79 typedef struct ao_bank_cfg {
80 	uint64_t bank_ctl_init_cmn;			/* Common init value */
81 	const struct ao_ctl_init *bank_ctl_init_extra;	/* Extra for each rev */
82 	void (*bank_misc_initfunc)(cmi_hdl_t, ao_ms_data_t *, uint32_t);
83 	uint_t bank_ctl_mask;
84 } ao_bank_cfg_t;
85 
86 static void nb_mcamisc_init(cmi_hdl_t, ao_ms_data_t *, uint32_t);
87 
88 static const ao_bank_cfg_t ao_bank_cfgs[] = {
89 	{ AMD_DC_CTL_INIT_CMN, NULL, NULL, AMD_MSR_DC_MASK },
90 	{ AMD_IC_CTL_INIT_CMN, NULL, NULL, AMD_MSR_IC_MASK },
91 	{ AMD_BU_CTL_INIT_CMN, NULL, NULL, AMD_MSR_BU_MASK },
92 	{ AMD_LS_CTL_INIT_CMN, NULL, NULL, AMD_MSR_LS_MASK },
93 	{ AMD_NB_CTL_INIT_CMN, &ao_nb_ctl_init[0], nb_mcamisc_init,
94 		AMD_MSR_NB_MASK },
95 };
96 
97 static int ao_nbanks = sizeof (ao_bank_cfgs) / sizeof (ao_bank_cfgs[0]);
98 
99 /*
100  * This is quite awful but necessary to work around x86 system vendor's view of
101  * the world.  Other operating systems (you know who you are) don't understand
102  * Opteron-specific error handling, so BIOS and system vendors often hide these
103  * conditions from them by using SMI polling to copy out any errors from the
104  * machine-check registers.  When Solaris runs on a system with this feature,
105  * we want to disable the SMI polling so we can use FMA instead.  Sadly, there
106  * isn't even a standard self-describing way to express the whole situation,
107  * so we have to resort to hard-coded values.  This should all be changed to
108  * be a self-describing vendor-specific SMBIOS structure in the future.
109  */
110 static const struct ao_smi_disable {
111 	const char *asd_sys_vendor;	/* SMB_TYPE_SYSTEM vendor prefix */
112 	const char *asd_sys_product;	/* SMB_TYPE_SYSTEM product prefix */
113 	const char *asd_bios_vendor;	/* SMB_TYPE_BIOS vendor prefix */
114 	uint8_t asd_code;		/* output code for SMI disable */
115 } ao_smi_disable[] = {
116 	{ "Sun Microsystems", "Galaxy12",
117 	    "American Megatrends", 0x59 },
118 	{ "Sun Microsystems", "Sun Fire X4100 Server",
119 	    "American Megatrends", 0x59 },
120 	{ "Sun Microsystems", "Sun Fire X4200 Server",
121 	    "American Megatrends", 0x59 },
122 	{ NULL, NULL, NULL, 0 }
123 };
124 
125 static int
126 ao_disp_match_r4(uint16_t ref, uint8_t r4)
127 {
128 	static const uint16_t ao_r4_map[] = {
129 		AO_MCA_R4_BIT_ERR,	/* MCAX86_ERRCODE_RRRR_ERR */
130 		AO_MCA_R4_BIT_RD,	/* MCAX86_ERRCODE_RRRR_RD */
131 		AO_MCA_R4_BIT_WR,	/* MCAX86_ERRCODE_RRRR_WR */
132 		AO_MCA_R4_BIT_DRD,	/* MCAX86_ERRCODE_RRRR_DRD */
133 		AO_MCA_R4_BIT_DWR,	/* MCAX86_ERRCODE_RRRR_DWR */
134 		AO_MCA_R4_BIT_IRD,	/* MCAX86_ERRCODE_RRRR_IRD */
135 		AO_MCA_R4_BIT_PREFETCH,	/* MCAX86_ERRCODE_RRRR_PREFETCH */
136 		AO_MCA_R4_BIT_EVICT,	/* MCAX86_ERRCODE_RRRR_EVICT */
137 		AO_MCA_R4_BIT_SNOOP	/* MCAX86_ERRCODE_RRRR_SNOOP */
138 	};
139 
140 	ASSERT(r4 < sizeof (ao_r4_map) / sizeof (uint16_t));
141 
142 	return ((ref & ao_r4_map[r4]) != 0);
143 }
144 
145 static int
146 ao_disp_match_pp(uint8_t ref, uint8_t pp)
147 {
148 	static const uint8_t ao_pp_map[] = {
149 		AO_MCA_PP_BIT_SRC,	/* MCAX86_ERRCODE_PP_SRC */
150 		AO_MCA_PP_BIT_RES,	/* MCAX86_ERRCODE_PP_RES */
151 		AO_MCA_PP_BIT_OBS,	/* MCAX86_ERRCODE_PP_OBS */
152 		AO_MCA_PP_BIT_GEN	/* MCAX86_ERRCODE_PP_GEN */
153 	};
154 
155 	ASSERT(pp < sizeof (ao_pp_map) / sizeof (uint8_t));
156 
157 	return ((ref & ao_pp_map[pp]) != 0);
158 }
159 
160 static int
161 ao_disp_match_ii(uint8_t ref, uint8_t ii)
162 {
163 	static const uint8_t ao_ii_map[] = {
164 		AO_MCA_II_BIT_MEM,	/* MCAX86_ERRCODE_II_MEM */
165 		0,
166 		AO_MCA_II_BIT_IO,	/* MCAX86_ERRCODE_II_IO */
167 		AO_MCA_II_BIT_GEN	/* MCAX86_ERRCODE_II_GEN */
168 	};
169 
170 	ASSERT(ii < sizeof (ao_ii_map) / sizeof (uint8_t));
171 
172 	return ((ref & ao_ii_map[ii]) != 0);
173 }
174 
175 static uint8_t
176 bit_strip(uint16_t *codep, uint16_t mask, uint16_t shift)
177 {
178 	uint8_t val = (*codep & mask) >> shift;
179 	*codep &= ~mask;
180 	return (val);
181 }
182 
183 #define	BIT_STRIP(codep, name) \
184 	bit_strip(codep, MCAX86_ERRCODE_##name##_MASK, \
185 	MCAX86_ERRCODE_##name##_SHIFT)
186 
187 /*ARGSUSED*/
188 static int
189 ao_disp_match_one(const ao_error_disp_t *aed, uint64_t status, uint32_t rev,
190     int bankno)
191 {
192 	uint16_t code = MCAX86_ERRCODE(status);
193 	uint8_t extcode = AMD_EXT_ERRCODE(status);
194 	uint64_t stat_mask = aed->aed_stat_mask;
195 	uint64_t stat_mask_res = aed->aed_stat_mask_res;
196 
197 	/*
198 	 * If the bank's status register indicates overflow, then we can no
199 	 * longer rely on the value of CECC: our experience with actual fault
200 	 * injection has shown that multiple CE's overwriting each other shows
201 	 * AMD_BANK_STAT_CECC and AMD_BANK_STAT_UECC both set to zero.  This
202 	 * should be clarified in a future BKDG or by the Revision Guide.
203 	 * This behaviour is fixed in revision F.
204 	 */
205 	if (bankno == AMD_MCA_BANK_NB &&
206 	    !X86_CHIPREV_ATLEAST(rev, X86_CHIPREV_AMD_F_REV_F) &&
207 	    status & MSR_MC_STATUS_OVER) {
208 		stat_mask &= ~AMD_BANK_STAT_CECC;
209 		stat_mask_res &= ~AMD_BANK_STAT_CECC;
210 	}
211 
212 	if ((status & stat_mask) != stat_mask_res)
213 		return (0);
214 
215 	/*
216 	 * r4 and pp bits are stored separately, so we mask off and compare them
217 	 * for the code types that use them.  Once we've taken the r4 and pp
218 	 * bits out of the equation, we can directly compare the resulting code
219 	 * with the one stored in the ao_error_disp_t.
220 	 */
221 	if (AMD_ERRCODE_ISMEM(code)) {
222 		uint8_t r4 = BIT_STRIP(&code, RRRR);
223 
224 		if (!ao_disp_match_r4(aed->aed_stat_r4_bits, r4))
225 			return (0);
226 
227 	} else if (AMD_ERRCODE_ISBUS(code)) {
228 		uint8_t r4 = BIT_STRIP(&code, RRRR);
229 		uint8_t pp = BIT_STRIP(&code, PP);
230 		uint8_t ii = BIT_STRIP(&code, II);
231 
232 		if (!ao_disp_match_r4(aed->aed_stat_r4_bits, r4) ||
233 		    !ao_disp_match_pp(aed->aed_stat_pp_bits, pp) ||
234 		    !ao_disp_match_ii(aed->aed_stat_ii_bits, ii))
235 			return (0);
236 	}
237 
238 	return (code == aed->aed_stat_code && extcode == aed->aed_stat_extcode);
239 }
240 
241 /*ARGSUSED*/
242 cms_cookie_t
243 ao_ms_disp_match(cmi_hdl_t hdl, int banknum, uint64_t status,
244     uint64_t addr, uint64_t misc, void *mslogout)
245 {
246 	ao_ms_data_t *ao = cms_hdl_getcmsdata(hdl);
247 	uint32_t rev = ao->ao_ms_shared->aos_chiprev;
248 	const ao_error_disp_t *aed;
249 
250 	for (aed = ao_error_disp[banknum]; aed->aed_stat_mask != 0; aed++) {
251 		if (ao_disp_match_one(aed, status, rev, banknum))
252 			return ((cms_cookie_t)aed);
253 	}
254 
255 	return (NULL);
256 }
257 
258 /*ARGSUSED*/
259 void
260 ao_ms_ereport_class(cmi_hdl_t hdl, cms_cookie_t mscookie,
261     const char **cpuclsp, const char **leafclsp)
262 {
263 	const ao_error_disp_t *aed = mscookie;
264 
265 	if (aed != NULL) {
266 		*cpuclsp = FM_EREPORT_CPU_AMD;
267 		*leafclsp = aed->aed_class;
268 	}
269 }
270 
271 static int
272 ao_chip_once(ao_ms_data_t *ao, enum ao_cfgonce_bitnum what)
273 {
274 	return (atomic_set_long_excl(&ao->ao_ms_shared->aos_cfgonce,
275 	    what) == 0 ?  B_TRUE : B_FALSE);
276 }
277 
278 /*
279  * This knob exists in case any platform has a problem with our default
280  * policy of disabling any interrupt registered in the NB MC4_MISC
281  * register.  Setting this may cause Solaris and external entities
282  * who also have an interest in this register to argue over available
283  * telemetry (so setting it is generally not recommended).
284  */
285 int ao_nb_cfg_mc4misc_noseize = 0;
286 
287 /*
288  * The BIOS may have setup to receive SMI on counter overflow.  It may also
289  * have locked various fields or made them read-only.  We will clear any
290  * SMI request and leave the register locked.  We will also clear the
291  * counter and enable counting - while we don't use the counter it is nice
292  * to have it enabled for verification and debug work.
293  */
294 static void
295 nb_mcamisc_init(cmi_hdl_t hdl, ao_ms_data_t *ao, uint32_t rev)
296 {
297 	uint64_t val, nval;
298 
299 	if (!X86_CHIPREV_MATCH(rev, AO_F_REVS_FG))
300 		return;
301 
302 	if (cmi_hdl_rdmsr(hdl, AMD_MSR_NB_MISC, &val) != CMI_SUCCESS)
303 		return;
304 
305 	ao->ao_ms_shared->aos_bcfg_nb_misc = val;
306 
307 	if (ao_nb_cfg_mc4misc_noseize)
308 		return;		/* stash BIOS value, but no changes */
309 
310 
311 	/*
312 	 * The Valid bit tells us whether the CtrP bit is defined; if it
313 	 * is the CtrP bit tells us whether an ErrCount field is present.
314 	 * If not then there is nothing for us to do.
315 	 */
316 	if (!(val & AMD_NB_MISC_VALID) || !(val & AMD_NB_MISC_CTRP))
317 		return;
318 
319 
320 	nval = val;
321 	nval |= AMD_NB_MISC_CNTEN;		/* enable ECC error counting */
322 	nval &= ~AMD_NB_MISC_ERRCOUNT_MASK;	/* clear ErrCount */
323 	nval &= ~AMD_NB_MISC_OVRFLW;		/* clear Ovrflw */
324 	nval &= ~AMD_NB_MISC_INTTYPE_MASK;	/* no interrupt on overflow */
325 	nval |= AMD_NB_MISC_LOCKED;
326 
327 	if (nval != val) {
328 		uint64_t locked = val & AMD_NB_MISC_LOCKED;
329 
330 		if (locked)
331 			ao_bankstatus_prewrite(hdl, ao);
332 
333 		(void) cmi_hdl_wrmsr(hdl, AMD_MSR_NB_MISC, nval);
334 
335 		if (locked)
336 			ao_bankstatus_postwrite(hdl, ao);
337 	}
338 }
339 
340 /*
341  * NorthBridge (NB) MCA Configuration.
342  *
343  * We add and remove bits from the BIOS-configured value, rather than
344  * writing an absolute value.  The variables ao_nb_cfg_{add,remove}_cmn and
345  * ap_nb_cfg_{add,remove}_revFG are available for modification via kmdb
346  * and /etc/system.  The revision-specific adds and removes are applied
347  * after the common changes, and one write is made to the config register.
348  * These are not intended for watchdog configuration via these variables -
349  * use the watchdog policy below.
350  */
351 
352 /*
353  * Bits to be added to the NB configuration register - all revs.
354  */
355 uint32_t ao_nb_cfg_add_cmn = AMD_NB_CFG_ADD_CMN;
356 
357 /*
358  * Bits to be cleared from the NB configuration register - all revs.
359  */
360 uint32_t ao_nb_cfg_remove_cmn = AMD_NB_CFG_REMOVE_CMN;
361 
362 /*
363  * Bits to be added to the NB configuration register - revs F and G.
364  */
365 uint32_t ao_nb_cfg_add_revFG = AMD_NB_CFG_ADD_REV_FG;
366 
367 /*
368  * Bits to be cleared from the NB configuration register - revs F and G.
369  */
370 uint32_t ao_nb_cfg_remove_revFG = AMD_NB_CFG_REMOVE_REV_FG;
371 
372 struct ao_nb_cfg {
373 	uint32_t cfg_revmask;
374 	uint32_t *cfg_add_p;
375 	uint32_t *cfg_remove_p;
376 };
377 
378 static const struct ao_nb_cfg ao_cfg_extra[] = {
379 	{ AO_F_REVS_FG, &ao_nb_cfg_add_revFG, &ao_nb_cfg_remove_revFG },
380 	{ X86_CHIPREV_UNKNOWN, NULL, NULL }
381 };
382 
383 /*
384  * Bits to be used if we configure the NorthBridge (NB) Watchdog.  The watchdog
385  * triggers a machine check exception when no response to an NB system access
386  * occurs within a specified time interval.
387  */
388 uint32_t ao_nb_cfg_wdog =
389     AMD_NB_CFG_WDOGTMRCNTSEL_4095 |
390     AMD_NB_CFG_WDOGTMRBASESEL_1MS;
391 
392 /*
393  * The default watchdog policy is to enable it (at the above rate) if it
394  * is disabled;  if it is enabled then we leave it enabled at the rate
395  * chosen by the BIOS.
396  */
397 enum {
398 	AO_NB_WDOG_LEAVEALONE,		/* Don't touch watchdog config */
399 	AO_NB_WDOG_DISABLE,		/* Always disable watchdog */
400 	AO_NB_WDOG_ENABLE_IF_DISABLED,	/* If disabled, enable at our rate */
401 	AO_NB_WDOG_ENABLE_FORCE_RATE	/* Enable and set our rate */
402 } ao_nb_watchdog_policy = AO_NB_WDOG_ENABLE_IF_DISABLED;
403 
404 static void
405 ao_nb_cfg(ao_ms_data_t *ao, uint32_t rev)
406 {
407 	const struct ao_nb_cfg *nbcp = &ao_cfg_extra[0];
408 	uint_t procnodeid = pg_plat_hw_instance_id(CPU, PGHW_PROCNODE);
409 	uint32_t val;
410 
411 	/*
412 	 * Read the NorthBridge (NB) configuration register in PCI space,
413 	 * modify the settings accordingly, and store the new value back.
414 	 * Note that the stashed BIOS config value aos_bcfg_nb_cfg is used
415 	 * in ereport payload population to determine ECC syndrome type for
416 	 * memory errors.
417 	 */
418 	ao->ao_ms_shared->aos_bcfg_nb_cfg = val =
419 	    ao_pcicfg_read(procnodeid, MC_FUNC_MISCCTL, MC_CTL_REG_NBCFG);
420 
421 	switch (ao_nb_watchdog_policy) {
422 	case AO_NB_WDOG_LEAVEALONE:
423 		break;
424 
425 	case AO_NB_WDOG_DISABLE:
426 		val &= ~AMD_NB_CFG_WDOGTMRBASESEL_MASK;
427 		val &= ~AMD_NB_CFG_WDOGTMRCNTSEL_MASK;
428 		val |= AMD_NB_CFG_WDOGTMRDIS;
429 		break;
430 
431 	default:
432 		cmn_err(CE_NOTE, "ao_nb_watchdog_policy=%d unrecognised, "
433 		    "using default policy", ao_nb_watchdog_policy);
434 		/*FALLTHRU*/
435 
436 	case AO_NB_WDOG_ENABLE_IF_DISABLED:
437 		if (!(val & AMD_NB_CFG_WDOGTMRDIS))
438 			break;	/* if enabled leave rate intact */
439 		/*FALLTHRU*/
440 
441 	case AO_NB_WDOG_ENABLE_FORCE_RATE:
442 		val &= ~AMD_NB_CFG_WDOGTMRBASESEL_MASK;
443 		val &= ~AMD_NB_CFG_WDOGTMRCNTSEL_MASK;
444 		val &= ~AMD_NB_CFG_WDOGTMRDIS;
445 		val |= ao_nb_cfg_wdog;
446 		break;
447 	}
448 
449 	/*
450 	 * Now apply bit adds and removes, first those common to all revs
451 	 * and then the revision-specific ones.
452 	 */
453 	val &= ~ao_nb_cfg_remove_cmn;
454 	val |= ao_nb_cfg_add_cmn;
455 
456 	while (nbcp->cfg_revmask != X86_CHIPREV_UNKNOWN) {
457 		if (X86_CHIPREV_MATCH(rev, nbcp->cfg_revmask)) {
458 			val &= ~(*nbcp->cfg_remove_p);
459 			val |= *nbcp->cfg_add_p;
460 		}
461 		nbcp++;
462 	}
463 
464 	ao_pcicfg_write(procnodeid, MC_FUNC_MISCCTL, MC_CTL_REG_NBCFG, val);
465 }
466 
467 static void
468 ao_dram_cfg(ao_ms_data_t *ao, uint32_t rev)
469 {
470 	uint_t procnodeid = pg_plat_hw_instance_id(CPU, PGHW_PROCNODE);
471 	union mcreg_dramcfg_lo dcfglo;
472 
473 	ao->ao_ms_shared->aos_bcfg_dcfg_lo = MCREG_VAL32(&dcfglo) =
474 	    ao_pcicfg_read(procnodeid, MC_FUNC_DRAMCTL, MC_DC_REG_DRAMCFGLO);
475 	ao->ao_ms_shared->aos_bcfg_dcfg_hi =
476 	    ao_pcicfg_read(procnodeid, MC_FUNC_DRAMCTL, MC_DC_REG_DRAMCFGHI);
477 #ifdef OPTERON_ERRATUM_172
478 	if (X86_CHIPREV_MATCH(rev, AO_F_REVS_FG) &&
479 	    MCREG_FIELD_F_revFG(&dcfglo, ParEn)) {
480 		MCREG_FIELD_F_revFG(&dcfglo, ParEn) = 0;
481 		ao_pcicfg_write(procnodeid, MC_FUNC_DRAMCTL,
482 		    MC_DC_REG_DRAMCFGLO, MCREG_VAL32(&dcfglo));
483 	}
484 #endif
485 }
486 
487 /*
488  * This knob exists in case any platform has a problem with our default
489  * policy of disabling any interrupt registered in the online spare
490  * control register.  Setting this may cause Solaris and external entities
491  * who also have an interest in this register to argue over available
492  * telemetry (so setting it is generally not recommended).
493  */
494 int ao_nb_cfg_sparectl_noseize = 0;
495 
496 /*
497  * Setup the online spare control register (revs F and G).  We disable
498  * any interrupt registered by the BIOS and zero all error counts.
499  */
500 static void
501 ao_sparectl_cfg(ao_ms_data_t *ao)
502 {
503 	uint_t procnodeid = pg_plat_hw_instance_id(CPU, PGHW_PROCNODE);
504 	union mcreg_sparectl sparectl;
505 	int chan, cs;
506 
507 	ao->ao_ms_shared->aos_bcfg_nb_sparectl = MCREG_VAL32(&sparectl) =
508 	    ao_pcicfg_read(procnodeid, MC_FUNC_MISCCTL, MC_CTL_REG_SPARECTL);
509 
510 	if (ao_nb_cfg_sparectl_noseize)
511 		return;	/* stash BIOS value, but no changes */
512 
513 	/*
514 	 * If the BIOS has requested SMI interrupt type for ECC count
515 	 * overflow for a chip-select or channel force those off.
516 	 */
517 	MCREG_FIELD_F_revFG(&sparectl, EccErrInt) = 0;
518 	MCREG_FIELD_F_revFG(&sparectl, SwapDoneInt) = 0;
519 
520 	/*
521 	 * Zero EccErrCnt and write this back to all chan/cs combinations.
522 	 */
523 	MCREG_FIELD_F_revFG(&sparectl, EccErrCntWrEn) = 1;
524 	MCREG_FIELD_F_revFG(&sparectl, EccErrCnt) = 0;
525 	for (chan = 0; chan < MC_CHIP_NDRAMCHAN; chan++) {
526 		MCREG_FIELD_F_revFG(&sparectl, EccErrCntDramChan) = chan;
527 
528 		for (cs = 0; cs < MC_CHIP_NCS; cs++) {
529 			MCREG_FIELD_F_revFG(&sparectl, EccErrCntDramCs) = cs;
530 			ao_pcicfg_write(procnodeid, MC_FUNC_MISCCTL,
531 			    MC_CTL_REG_SPARECTL, MCREG_VAL32(&sparectl));
532 		}
533 	}
534 }
535 
536 int ao_forgive_uc = 0;		/* For test/debug only */
537 int ao_forgive_pcc = 0;		/* For test/debug only */
538 int ao_fake_poison = 0;		/* For test/debug only */
539 
540 uint32_t
541 ao_ms_error_action(cmi_hdl_t hdl, int ismc, int banknum,
542     uint64_t status, uint64_t addr, uint64_t misc, void *mslogout)
543 {
544 	const ao_error_disp_t *aed;
545 	uint32_t retval = 0;
546 	uint8_t when;
547 	int en;
548 
549 	if (ao_forgive_uc)
550 		retval |= CMS_ERRSCOPE_CLEARED_UC;
551 
552 	if (ao_forgive_pcc)
553 		retval |= CMS_ERRSCOPE_CURCONTEXT_OK;
554 
555 	if (ao_fake_poison && status & MSR_MC_STATUS_UC)
556 		retval |= CMS_ERRSCOPE_POISONED;
557 
558 	if (retval)
559 		return (retval);
560 
561 	aed = ao_ms_disp_match(hdl, banknum, status, addr, misc, mslogout);
562 
563 	/*
564 	 * If we do not recognise the error let the cpu module apply
565 	 * the generic criteria to decide how to react.
566 	 */
567 	if (aed == NULL)
568 		return (0);
569 
570 	en = (status & MSR_MC_STATUS_EN) != 0;
571 
572 	if ((when = aed->aed_panic_when) == AO_AED_PANIC_NEVER)
573 		retval |= CMS_ERRSCOPE_IGNORE_ERR;
574 
575 	if ((when & AO_AED_PANIC_ALWAYS) ||
576 	    ((when & AO_AED_PANIC_IFMCE) && (en || ismc)))
577 		retval |= CMS_ERRSCOPE_FORCE_FATAL;
578 
579 	/*
580 	 * The original AMD implementation would panic on a machine check
581 	 * (not a poll) if the status overflow bit was set, with an
582 	 * exception for the case of rev F or later with an NB error
583 	 * indicating CECC.  This came from the perception that the
584 	 * overflow bit was not correctly managed on rev E and earlier, for
585 	 * example that repeated correctable memeory errors did not set
586 	 * OVER but somehow clear CECC.
587 	 *
588 	 * We will leave the generic support to evaluate overflow errors
589 	 * and decide to panic on their individual merits, e.g., if PCC
590 	 * is set and so on.  The AMD docs do say (as Intel does) that
591 	 * the status information is *all* from the higher-priority
592 	 * error in the case of an overflow, so it is at least as serious
593 	 * as the original and we can decide panic etc based on it.
594 	 */
595 
596 	return (retval);
597 }
598 
599 /*
600  * Will need to change for family 0x10
601  */
602 static uint_t
603 ao_ereport_synd(ao_ms_data_t *ao, uint64_t status, uint_t *typep,
604     int is_nb)
605 {
606 	if (is_nb) {
607 		if (ao->ao_ms_shared->aos_bcfg_nb_cfg &
608 		    AMD_NB_CFG_CHIPKILLECCEN) {
609 			*typep = AMD_SYNDTYPE_CHIPKILL;
610 			return (AMD_NB_STAT_CKSYND(status));
611 		} else {
612 			*typep = AMD_SYNDTYPE_ECC;
613 			return (AMD_BANK_SYND(status));
614 		}
615 	} else {
616 		*typep = AMD_SYNDTYPE_ECC;
617 		return (AMD_BANK_SYND(status));
618 	}
619 }
620 
621 static nvlist_t *
622 ao_ereport_create_resource_elem(cmi_hdl_t hdl, nv_alloc_t *nva,
623     mc_unum_t *unump, int dimmnum)
624 {
625 	nvlist_t *nvl, *snvl;
626 	nvlist_t *board_list = NULL;
627 
628 	if ((nvl = fm_nvlist_create(nva)) == NULL)	/* freed by caller */
629 		return (NULL);
630 
631 	if ((snvl = fm_nvlist_create(nva)) == NULL) {
632 		fm_nvlist_destroy(nvl, nva ? FM_NVA_RETAIN : FM_NVA_FREE);
633 		return (NULL);
634 	}
635 
636 	(void) nvlist_add_uint64(snvl, FM_FMRI_HC_SPECIFIC_OFFSET,
637 	    unump->unum_offset);
638 
639 	if (!x86gentopo_legacy) {
640 		board_list = cmi_hdl_smb_bboard(hdl);
641 
642 		if (board_list == NULL) {
643 			fm_nvlist_destroy(nvl,
644 			    nva ? FM_NVA_RETAIN : FM_NVA_FREE);
645 			fm_nvlist_destroy(snvl,
646 			    nva ? FM_NVA_RETAIN : FM_NVA_FREE);
647 			return (NULL);
648 		}
649 
650 		fm_fmri_hc_create(nvl, FM_HC_SCHEME_VERSION, NULL, snvl,
651 		    board_list, 4,
652 		    "chip", cmi_hdl_smb_chipid(hdl),
653 		    "memory-controller", unump->unum_mc,
654 		    "dimm", unump->unum_dimms[dimmnum],
655 		    "rank", unump->unum_rank);
656 	} else {
657 		fm_fmri_hc_set(nvl, FM_HC_SCHEME_VERSION, NULL, snvl, 5,
658 		    "motherboard", unump->unum_board,
659 		    "chip", unump->unum_chip,
660 		    "memory-controller", unump->unum_mc,
661 		    "dimm", unump->unum_dimms[dimmnum],
662 		    "rank", unump->unum_rank);
663 	}
664 
665 	fm_nvlist_destroy(snvl, nva ? FM_NVA_RETAIN : FM_NVA_FREE);
666 
667 	return (nvl);
668 }
669 
670 static void
671 ao_ereport_add_resource(cmi_hdl_t hdl, nvlist_t *payload, nv_alloc_t *nva,
672     mc_unum_t *unump)
673 {
674 
675 	nvlist_t *elems[MC_UNUM_NDIMM];
676 	int nelems = 0;
677 	int i;
678 
679 	for (i = 0; i < MC_UNUM_NDIMM; i++) {
680 		if (unump->unum_dimms[i] == MC_INVALNUM)
681 			break;
682 
683 		if ((elems[nelems] = ao_ereport_create_resource_elem(hdl, nva,
684 		    unump, i)) == NULL)
685 			break;
686 
687 		nelems++;
688 	}
689 
690 	if (nelems == 0)
691 		return;
692 
693 	fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_RESOURCE,
694 	    DATA_TYPE_NVLIST_ARRAY, nelems, elems, NULL);
695 
696 	for (i = 0; i < nelems; i++)
697 		fm_nvlist_destroy(elems[i], nva ? FM_NVA_RETAIN : FM_NVA_FREE);
698 }
699 
700 /*ARGSUSED*/
701 void
702 ao_ms_ereport_add_logout(cmi_hdl_t hdl, nvlist_t *ereport,
703     nv_alloc_t *nva, int banknum, uint64_t status, uint64_t addr,
704     uint64_t misc, void *mslogout, cms_cookie_t mscookie)
705 {
706 	ao_ms_data_t *ao = cms_hdl_getcmsdata(hdl);
707 	const ao_error_disp_t *aed = mscookie;
708 	uint_t synd, syndtype;
709 	uint64_t members;
710 
711 	if (aed == NULL)
712 		return;
713 
714 	members = aed->aed_ereport_members;
715 
716 	synd = ao_ereport_synd(ao, status, &syndtype,
717 	    banknum == AMD_MCA_BANK_NB);
718 
719 	if (members & FM_EREPORT_PAYLOAD_FLAG_SYND) {
720 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_NAME_SYND,
721 		    DATA_TYPE_UINT16, synd, NULL);
722 	}
723 
724 	if (members & FM_EREPORT_PAYLOAD_FLAG_SYND_TYPE) {
725 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_NAME_SYND_TYPE,
726 		    DATA_TYPE_STRING, (syndtype == AMD_SYNDTYPE_CHIPKILL ?
727 		    "C" : "E"), NULL);
728 	}
729 
730 	if (members & FM_EREPORT_PAYLOAD_FLAG_RESOURCE) {
731 		mc_unum_t unum;
732 
733 		if (((aed->aed_flags & AO_AED_FLAGS_ADDRTYPE) ==
734 		    AO_AED_F_PHYSICAL) && (status & MSR_MC_STATUS_ADDRV) &&
735 		    cmi_mc_patounum(addr, aed->aed_addrvalid_hi,
736 		    aed->aed_addrvalid_lo, synd, syndtype, &unum) ==
737 		    CMI_SUCCESS)
738 			ao_ereport_add_resource(hdl, ereport, nva, &unum);
739 	}
740 }
741 
742 /*ARGSUSED*/
743 boolean_t
744 ao_ms_ereport_includestack(cmi_hdl_t hdl, cms_cookie_t mscookie)
745 {
746 	const ao_error_disp_t *aed = mscookie;
747 
748 	if (aed == NULL)
749 		return (0);
750 
751 	return ((aed->aed_ereport_members &
752 	    FM_EREPORT_PAYLOAD_FLAG_STACK) != 0);
753 }
754 
755 cms_errno_t
756 ao_ms_msrinject(cmi_hdl_t hdl, uint_t msr, uint64_t val)
757 {
758 	ao_ms_data_t *ao = cms_hdl_getcmsdata(hdl);
759 	cms_errno_t rv = CMSERR_BADMSRWRITE;
760 
761 	ao_bankstatus_prewrite(hdl, ao);
762 	if (cmi_hdl_wrmsr(hdl, msr, val) == CMI_SUCCESS)
763 		rv = CMS_SUCCESS;
764 	ao_bankstatus_postwrite(hdl, ao);
765 
766 	return (rv);
767 }
768 
769 /*ARGSUSED*/
770 uint64_t
771 ao_ms_mcgctl_val(cmi_hdl_t hdl, int nbanks, uint64_t def)
772 {
773 	return ((1ULL << nbanks) - 1);
774 }
775 
776 boolean_t
777 ao_ms_bankctl_skipinit(cmi_hdl_t hdl, int banknum)
778 {
779 	ao_ms_data_t *ao = cms_hdl_getcmsdata(hdl);
780 
781 	if (banknum != AMD_MCA_BANK_NB)
782 		return (B_FALSE);
783 
784 	/*
785 	 * If we are the first to atomically set the "I'll do it" bit
786 	 * then return B_FALSE (do not skip), otherwise skip with B_TRUE.
787 	 */
788 	return (ao_chip_once(ao, AO_CFGONCE_NBMCA) == B_TRUE ?
789 	    B_FALSE : B_TRUE);
790 }
791 
792 uint64_t
793 ao_ms_bankctl_val(cmi_hdl_t hdl, int banknum, uint64_t def)
794 {
795 	ao_ms_data_t *ao = cms_hdl_getcmsdata(hdl);
796 	const struct ao_ctl_init *extrap;
797 	const ao_bank_cfg_t *bankcfg;
798 	uint64_t mcictl;
799 	uint32_t rev = ao->ao_ms_shared->aos_chiprev;
800 
801 	if (banknum >= sizeof (ao_bank_cfgs) / sizeof (ao_bank_cfgs[0]))
802 		return (def);
803 
804 	bankcfg = &ao_bank_cfgs[banknum];
805 	extrap = bankcfg->bank_ctl_init_extra;
806 
807 	mcictl = bankcfg->bank_ctl_init_cmn;
808 
809 	while (extrap != NULL && extrap->ctl_revmask != X86_CHIPREV_UNKNOWN) {
810 		if (X86_CHIPREV_MATCH(rev, extrap->ctl_revmask))
811 			mcictl |= extrap->ctl_bits;
812 		extrap++;
813 	}
814 
815 	return (mcictl);
816 }
817 
818 /*ARGSUSED*/
819 void
820 ao_bankstatus_prewrite(cmi_hdl_t hdl, ao_ms_data_t *ao)
821 {
822 #ifndef __xpv
823 	uint64_t hwcr;
824 
825 	if (cmi_hdl_rdmsr(hdl, MSR_AMD_HWCR, &hwcr) != CMI_SUCCESS)
826 		return;
827 
828 	ao->ao_ms_hwcr_val = hwcr;
829 
830 	if (!(hwcr & AMD_HWCR_MCI_STATUS_WREN)) {
831 		hwcr |= AMD_HWCR_MCI_STATUS_WREN;
832 		(void) cmi_hdl_wrmsr(hdl, MSR_AMD_HWCR, hwcr);
833 	}
834 #endif
835 }
836 
837 /*ARGSUSED*/
838 void
839 ao_bankstatus_postwrite(cmi_hdl_t hdl, ao_ms_data_t *ao)
840 {
841 #ifndef __xpv
842 	uint64_t hwcr = ao->ao_ms_hwcr_val;
843 
844 	if (!(hwcr & AMD_HWCR_MCI_STATUS_WREN)) {
845 		hwcr &= ~AMD_HWCR_MCI_STATUS_WREN;
846 		(void) cmi_hdl_wrmsr(hdl, MSR_AMD_HWCR, hwcr);
847 	}
848 #endif
849 }
850 
851 void
852 ao_ms_mca_init(cmi_hdl_t hdl, int nbanks)
853 {
854 	ao_ms_data_t *ao = cms_hdl_getcmsdata(hdl);
855 	uint32_t rev = ao->ao_ms_shared->aos_chiprev;
856 	ao_ms_mca_t *mca = &ao->ao_ms_mca;
857 	uint64_t *maskp;
858 	int i;
859 
860 	maskp = mca->ao_mca_bios_cfg.bcfg_bank_mask = kmem_zalloc(nbanks *
861 	    sizeof (uint64_t), KM_SLEEP);
862 
863 	/*
864 	 * Read the bank ctl mask MSRs, but only as many as we know
865 	 * certainly exist - don't calculate the register address.
866 	 * Also initialize the MCi_MISC register where required.
867 	 */
868 	for (i = 0; i < MIN(nbanks, ao_nbanks); i++) {
869 		(void) cmi_hdl_rdmsr(hdl, ao_bank_cfgs[i].bank_ctl_mask,
870 		    maskp++);
871 		if (ao_bank_cfgs[i].bank_misc_initfunc != NULL)
872 			ao_bank_cfgs[i].bank_misc_initfunc(hdl, ao, rev);
873 
874 	}
875 
876 	if (ao_chip_once(ao, AO_CFGONCE_NBCFG) == B_TRUE) {
877 		ao_nb_cfg(ao, rev);
878 
879 		if (X86_CHIPREV_MATCH(rev, AO_F_REVS_FG))
880 			ao_sparectl_cfg(ao);
881 	}
882 
883 	if (ao_chip_once(ao, AO_CFGONCE_DRAMCFG) == B_TRUE)
884 		ao_dram_cfg(ao, rev);
885 
886 	ao_procnode_scrubber_enable(hdl, ao);
887 }
888 
889 /*
890  * Note that although this cpu module is loaded before the PSMs are
891  * loaded (and hence before acpica is loaded), this function is
892  * called from post_startup(), after PSMs are initialized and acpica
893  * is loaded.
894  */
895 static int
896 ao_acpi_find_smicmd(int *asd_port)
897 {
898 	ACPI_TABLE_FADT *fadt = NULL;
899 
900 	/*
901 	 * AcpiGetTable works even if ACPI is disabled, so a failure
902 	 * here means we weren't able to retreive a pointer to the FADT.
903 	 */
904 	if (AcpiGetTable(ACPI_SIG_FADT, 1, (ACPI_TABLE_HEADER **)&fadt) !=
905 	    AE_OK)
906 		return (-1);
907 
908 	ASSERT(fadt != NULL);
909 
910 	*asd_port = fadt->SmiCommand;
911 	return (0);
912 }
913 
914 /*ARGSUSED*/
915 void
916 ao_ms_post_startup(cmi_hdl_t hdl)
917 {
918 	const struct ao_smi_disable *asd;
919 	id_t id;
920 	int rv = -1, asd_port;
921 
922 	smbios_system_t sy;
923 	smbios_bios_t sb;
924 	smbios_info_t si;
925 
926 	/*
927 	 * Fetch the System and BIOS vendor strings from SMBIOS and see if they
928 	 * match a value in our table.  If so, disable SMI error polling.  This
929 	 * is grotesque and should be replaced by self-describing vendor-
930 	 * specific SMBIOS data or a specification enhancement instead.
931 	 */
932 	if (ao_mca_smi_disable && ksmbios != NULL &&
933 	    smbios_info_bios(ksmbios, &sb) != SMB_ERR &&
934 	    (id = smbios_info_system(ksmbios, &sy)) != SMB_ERR &&
935 	    smbios_info_common(ksmbios, id, &si) != SMB_ERR) {
936 
937 		for (asd = ao_smi_disable; asd->asd_sys_vendor != NULL; asd++) {
938 			if (strncmp(asd->asd_sys_vendor, si.smbi_manufacturer,
939 			    strlen(asd->asd_sys_vendor)) != 0 ||
940 			    strncmp(asd->asd_sys_product, si.smbi_product,
941 			    strlen(asd->asd_sys_product)) != 0 ||
942 			    strncmp(asd->asd_bios_vendor, sb.smbb_vendor,
943 			    strlen(asd->asd_bios_vendor)) != 0)
944 				continue;
945 
946 			/*
947 			 * Look for the SMI_CMD port in the ACPI FADT,
948 			 * if the port is 0, this platform doesn't support
949 			 * SMM, so there is no SMI error polling to disable.
950 			 */
951 			if ((rv = ao_acpi_find_smicmd(&asd_port)) == 0 &&
952 			    asd_port != 0) {
953 				cmn_err(CE_CONT, "?SMI polling disabled in "
954 				    "favor of Solaris Fault Management for "
955 				    "AMD Processors\n");
956 
957 				outb(asd_port, asd->asd_code);
958 
959 			} else if (rv < 0) {
960 				cmn_err(CE_CONT, "?Solaris Fault Management "
961 				    "for AMD Processors could not disable SMI "
962 				    "polling because an error occurred while "
963 				    "trying to determine the SMI command port "
964 				    "from the ACPI FADT table\n");
965 			}
966 			break;
967 		}
968 	}
969 }
970