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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * The CPU module for the AMD Athlon64 and Opteron processors
31  */
32 
33 #include <sys/types.h>
34 #include <sys/cmn_err.h>
35 #include <sys/sunddi.h>
36 #include <sys/cpu_module_impl.h>
37 #include <sys/cpuvar.h>
38 #include <sys/x86_archext.h>
39 #include <sys/kmem.h>
40 #include <sys/pghw.h>
41 #include <sys/modctl.h>
42 #include <sys/mc.h>
43 #include <sys/mca_x86.h>
44 
45 #include "ao.h"
46 
47 static struct ao_chipshared *ao_shared[NCPU];
48 
49 /*
50  * This cpu module supports AMD family 0xf revisions B/C/D/E/F/G.  If
51  * a family 0xf cpu beyond the rev G model limit is detected then
52  * return ENOTSUP and let the generic x86 CPU module load instead.
53  */
54 uint_t ao_model_limit = 0x6f;
55 
56 static int
57 ao_init(cpu_t *cp, void **datap)
58 {
59 	uint_t chipid = pg_plat_hw_instance_id(CPU, PGHW_CHIP);
60 	struct ao_chipshared *sp, *osp;
61 	ao_data_t *ao;
62 	uint64_t cap;
63 
64 	if (cpuid_getmodel(cp) >= ao_model_limit)
65 		return (ENOTSUP);
66 
67 	if (!(x86_feature & X86_MCA))
68 		return (ENOTSUP);
69 
70 	cap = rdmsr(IA32_MSR_MCG_CAP);
71 	if (!(cap & MCG_CAP_CTL_P))
72 		return (ENOTSUP);
73 
74 	/*
75 	 * Arrange to fallback to generic.cpu if the bank count is not
76 	 * as expected.  We're not silent about this - if we have X86_MCA
77 	 * and MCG_CAP_CTL_P then we appear not to be virtualized.
78 	 */
79 	if ((cap & MCG_CAP_COUNT_MASK) != AMD_MCA_BANK_COUNT) {
80 		cmn_err(CE_WARN, "CPU %d has %llu MCA banks; expected %u: "
81 		    "disabling AMD-specific MCA support on this CPU",
82 		    cp->cpu_id, (u_longlong_t)cap & MCG_CAP_COUNT_MASK,
83 		    AMD_MCA_BANK_COUNT);
84 		return (ENOTSUP);
85 	}
86 
87 	ao = *datap = kmem_zalloc(sizeof (ao_data_t), KM_SLEEP);
88 	ao->ao_cpu = cp;
89 
90 	/*
91 	 * Allocate the chipshared structure if it appears not to have been
92 	 * allocated already (by a sibling core).  Install the newly
93 	 * allocated pointer atomically in case a sibling core beats
94 	 * us to it.
95 	 */
96 	if ((sp = ao_shared[chipid]) == NULL) {
97 		sp = kmem_zalloc(sizeof (struct ao_chipshared), KM_SLEEP);
98 		osp = atomic_cas_ptr(&ao_shared[chipid], NULL, sp);
99 		if (osp != NULL) {
100 			kmem_free(sp, sizeof (struct ao_chipshared));
101 			sp = osp;
102 		}
103 	}
104 	ao->ao_shared = sp;
105 
106 	return (0);
107 }
108 
109 /*ARGSUSED*/
110 static void
111 ao_post_mpstartup(void *data)
112 {
113 	(void) ddi_install_driver("mc-amd");
114 }
115 
116 static void
117 ao_fini(void *data)
118 {
119 	kmem_free(data, sizeof (ao_data_t));
120 }
121 
122 const cmi_ops_t _cmi_ops = {
123 	ao_init,
124 	ao_mca_post_init,
125 	ao_post_mpstartup,
126 	ao_fini,
127 	ao_faulted_enter,
128 	ao_faulted_exit,
129 	ao_scrubber_enable,
130 	ao_mca_init,
131 	ao_mca_trap,
132 	ao_mca_inject,
133 	ao_mca_poke,
134 	ao_mc_register,
135 	ao_mc_getops
136 };
137 
138 static struct modlcpu modlcpu = {
139 	&mod_cpuops,
140 	"AMD Athlon64/Opteron CPU Module"
141 };
142 
143 static struct modlinkage modlinkage = {
144 	MODREV_1,
145 	(void *)&modlcpu,
146 	NULL
147 };
148 
149 int
150 _init(void)
151 {
152 	int err;
153 
154 	ao_mca_queue = errorq_create("ao_mca_queue",
155 	    ao_mca_drain, NULL, AO_MCA_MAX_ERRORS * (max_ncpus + 1),
156 	    sizeof (ao_cpu_logout_t), 1, ERRORQ_VITAL);
157 
158 	if (ao_mca_queue == NULL)
159 		return (EAGAIN); /* errorq_create() logs a message for us */
160 
161 	if ((err = mod_install(&modlinkage)) != 0) {
162 		errorq_destroy(ao_mca_queue);
163 		ao_mca_queue = NULL;
164 	}
165 
166 	return (err);
167 }
168 
169 int
170 _info(struct modinfo *modinfop)
171 {
172 	return (mod_info(&modlinkage, modinfop));
173 }
174 
175 int
176 _fini(void)
177 {
178 	int err;
179 
180 	if ((err = mod_remove(&modlinkage)) == 0)
181 		errorq_destroy(ao_mca_queue);
182 
183 	return (err);
184 }
185