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 2006 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/modctl.h>
41 #include <sys/mc.h>
42 #include <sys/mca_x86.h>
43 
44 #include "ao.h"
45 
46 /*
47  * At present this CPU module only supports the features for Athlon64 and
48  * Opteron up to and including the Rev E processor.  If we detect Rev F or
49  * later, return ENOTSUP and let the generic x86 CPU module load instead.
50  * Opteron Rev F is currently defined as Family 0xF Model [0x40 .. 0x5F].
51  */
52 uint_t ao_model_limit = 0x40;
53 
54 static int
55 ao_init(cpu_t *cp, void **datap)
56 {
57 	ao_data_t *ao;
58 	uint64_t cap;
59 
60 	if (cpuid_getmodel(cp) >= ao_model_limit)
61 		return (ENOTSUP);
62 
63 	if (!(x86_feature & X86_MCA))
64 		return (ENOTSUP);
65 
66 	cap = rdmsr(IA32_MSR_MCG_CAP);
67 	if (!(cap & MCG_CAP_CTL_P))
68 		return (ENOTSUP);
69 
70 	ao = *datap = kmem_zalloc(sizeof (ao_data_t), KM_SLEEP);
71 	ao->ao_cpu = cp;
72 
73 	return (0);
74 }
75 
76 /*ARGSUSED*/
77 static void
78 ao_post_mpstartup(void *data)
79 {
80 	(void) ddi_install_driver("mc-amd");
81 }
82 
83 static void
84 ao_fini(void *data)
85 {
86 	kmem_free(data, sizeof (ao_data_t));
87 }
88 
89 const cmi_ops_t _cmi_ops = {
90 	ao_init,
91 	ao_mca_post_init,
92 	ao_post_mpstartup,
93 	ao_fini,
94 	ao_faulted_enter,
95 	ao_faulted_exit,
96 	ao_scrubber_enable,
97 	ao_mca_init,
98 	ao_mca_trap,
99 	ao_mca_inject,
100 	ao_mca_poke,
101 	ao_mc_register,
102 	ao_mc_getops
103 };
104 
105 static struct modlcpu modlcpu = {
106 	&mod_cpuops,
107 	"AMD Athlon64/Opteron CPU Module"
108 };
109 
110 static struct modlinkage modlinkage = {
111 	MODREV_1,
112 	(void *)&modlcpu,
113 	NULL
114 };
115 
116 int
117 _init(void)
118 {
119 	int err;
120 
121 	ao_mca_queue = errorq_create("ao_mca_queue",
122 	    ao_mca_drain, NULL, AO_MCA_MAX_ERRORS * (max_ncpus + 1),
123 	    sizeof (ao_cpu_logout_t), 1, ERRORQ_VITAL);
124 
125 	if (ao_mca_queue == NULL)
126 		return (EAGAIN); /* errorq_create() logs a message for us */
127 
128 	if ((err = mod_install(&modlinkage)) != 0) {
129 		errorq_destroy(ao_mca_queue);
130 		ao_mca_queue = NULL;
131 	}
132 
133 	return (err);
134 }
135 
136 int
137 _info(struct modinfo *modinfop)
138 {
139 	return (mod_info(&modlinkage, modinfop));
140 }
141 
142 int
143 _fini(void)
144 {
145 	int err;
146 
147 	if ((err = mod_remove(&modlinkage)) == 0)
148 		errorq_destroy(ao_mca_queue);
149 
150 	return (err);
151 }
152