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 	ao = *datap = kmem_zalloc(sizeof (ao_data_t), KM_SLEEP);
75 	ao->ao_cpu = cp;
76 
77 	/*
78 	 * Allocate the chipshared structure if it appears not to have been
79 	 * allocated already (by a sibling core).  Install the newly
80 	 * allocated pointer atomically in case a sibling core beats
81 	 * us to it.
82 	 */
83 	if ((sp = ao_shared[chipid]) == NULL) {
84 		sp = kmem_zalloc(sizeof (struct ao_chipshared), KM_SLEEP);
85 		osp = atomic_cas_ptr(&ao_shared[chipid], NULL, sp);
86 		if (osp != NULL) {
87 			kmem_free(sp, sizeof (struct ao_chipshared));
88 			sp = osp;
89 		}
90 	}
91 	ao->ao_shared = sp;
92 
93 	return (0);
94 }
95 
96 /*ARGSUSED*/
97 static void
98 ao_post_mpstartup(void *data)
99 {
100 	(void) ddi_install_driver("mc-amd");
101 }
102 
103 static void
104 ao_fini(void *data)
105 {
106 	kmem_free(data, sizeof (ao_data_t));
107 }
108 
109 const cmi_ops_t _cmi_ops = {
110 	ao_init,
111 	ao_mca_post_init,
112 	ao_post_mpstartup,
113 	ao_fini,
114 	ao_faulted_enter,
115 	ao_faulted_exit,
116 	ao_scrubber_enable,
117 	ao_mca_init,
118 	ao_mca_trap,
119 	ao_mca_inject,
120 	ao_mca_poke,
121 	ao_mc_register,
122 	ao_mc_getops
123 };
124 
125 static struct modlcpu modlcpu = {
126 	&mod_cpuops,
127 	"AMD Athlon64/Opteron CPU Module"
128 };
129 
130 static struct modlinkage modlinkage = {
131 	MODREV_1,
132 	(void *)&modlcpu,
133 	NULL
134 };
135 
136 int
137 _init(void)
138 {
139 	int err;
140 
141 	ao_mca_queue = errorq_create("ao_mca_queue",
142 	    ao_mca_drain, NULL, AO_MCA_MAX_ERRORS * (max_ncpus + 1),
143 	    sizeof (ao_cpu_logout_t), 1, ERRORQ_VITAL);
144 
145 	if (ao_mca_queue == NULL)
146 		return (EAGAIN); /* errorq_create() logs a message for us */
147 
148 	if ((err = mod_install(&modlinkage)) != 0) {
149 		errorq_destroy(ao_mca_queue);
150 		ao_mca_queue = NULL;
151 	}
152 
153 	return (err);
154 }
155 
156 int
157 _info(struct modinfo *modinfop)
158 {
159 	return (mod_info(&modlinkage, modinfop));
160 }
161 
162 int
163 _fini(void)
164 {
165 	int err;
166 
167 	if ((err = mod_remove(&modlinkage)) == 0)
168 		errorq_destroy(ao_mca_queue);
169 
170 	return (err);
171 }
172