xref: /illumos-gate/usr/src/uts/i86pc/cpu/generic_cpu/gcpu_main.c (revision 20c794b39650d115e17a15983b6b82e46238cf45)
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  * Generic x86 CPU Module
31  *
32  * This CPU module is used for generic x86 CPUs when Solaris has no other
33  * CPU-specific support module available.  Code in this module should be the
34  * absolute bare-bones support and must be cognizant of both Intel and AMD etc.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/cpu_module_impl.h>
39 #include <sys/cpuvar.h>
40 #include <sys/kmem.h>
41 #include <sys/modctl.h>
42 #include <sys/pghw.h>
43 
44 #include "gcpu.h"
45 
46 /*
47  * Prevent generic cpu support from loading.
48  */
49 int gcpu_disable = 0;
50 
51 #define	GCPU_MAX_CHIPID		32
52 static struct gcpu_chipshared *gcpu_shared[GCPU_MAX_CHIPID];
53 
54 /*
55  * Our cmi_init entry point, called during startup of each cpu instance.
56  */
57 int
58 gcpu_init(cmi_hdl_t hdl, void **datap)
59 {
60 	uint_t chipid = cmi_hdl_chipid(hdl);
61 	struct gcpu_chipshared *sp, *osp;
62 	gcpu_data_t *gcpu;
63 
64 	if (gcpu_disable || chipid >= GCPU_MAX_CHIPID)
65 		return (ENOTSUP);
66 
67 	/*
68 	 * Allocate the state structure for this cpu.  We will only
69 	 * allocate the bank logout areas in gcpu_mca_init once we
70 	 * know how many banks there are.
71 	 */
72 	gcpu = *datap = kmem_zalloc(sizeof (gcpu_data_t), KM_SLEEP);
73 	cmi_hdl_hold(hdl);	/* release in gcpu_fini */
74 	gcpu->gcpu_hdl = hdl;
75 
76 	/*
77 	 * Allocate a chipshared structure if no sibling cpu has already
78 	 * allocated it, but allow for the fact that a sibling core may
79 	 * be starting up in parallel.
80 	 */
81 	if ((sp = gcpu_shared[chipid]) == NULL) {
82 		sp = kmem_zalloc(sizeof (struct gcpu_chipshared), KM_SLEEP);
83 		osp = atomic_cas_ptr(&gcpu_shared[chipid], NULL, sp);
84 		if (osp == NULL) {
85 			mutex_init(&sp->gcpus_poll_lock, NULL, MUTEX_DRIVER,
86 			    NULL);
87 			mutex_init(&sp->gcpus_cfglock, NULL, MUTEX_DRIVER,
88 			    NULL);
89 		} else {
90 			kmem_free(sp, sizeof (struct gcpu_chipshared));
91 			sp = osp;
92 		}
93 	}
94 	gcpu->gcpu_shared = sp;
95 
96 	return (0);
97 }
98 
99 void
100 gcpu_post_startup(cmi_hdl_t hdl)
101 {
102 	gcpu_data_t *gcpu = cmi_hdl_getcmidata(hdl);
103 
104 	if (gcpu_disable || gcpu == NULL)
105 		return;
106 
107 	cms_post_startup(hdl);
108 }
109 
110 void
111 gcpu_post_mpstartup(cmi_hdl_t hdl)
112 {	if (!gcpu_disable) {
113 		cms_post_mpstartup(hdl);
114 		gcpu_mca_poll_start(hdl);
115 	}
116 }
117 
118 cmi_api_ver_t _cmi_api_version = CMI_API_VERSION_1;
119 
120 const cmi_ops_t _cmi_ops = {
121 	gcpu_init,				/* cmi_init */
122 	gcpu_post_startup,			/* cmi_post_startup */
123 	gcpu_post_mpstartup,			/* cmi_post_mpstartup */
124 	gcpu_faulted_enter,			/* cmi_faulted_enter */
125 	gcpu_faulted_exit,			/* cmi_faulted_exit */
126 	gcpu_mca_init,				/* cmi_mca_init */
127 	gcpu_mca_trap,				/* cmi_mca_trap */
128 	gcpu_msrinject,				/* cmi_msrinject */
129 	gcpu_hdl_poke,				/* cmi_hdl_poke */
130 	NULL,					/* cmi_fini */
131 };
132 
133 static struct modlcpu modlcpu = {
134 	&mod_cpuops,
135 	"Generic x86 CPU Module"
136 };
137 
138 static struct modlinkage modlinkage = {
139 	MODREV_1,
140 	(void *)&modlcpu,
141 	NULL
142 };
143 
144 int
145 _init(void)
146 {
147 	return (mod_install(&modlinkage));
148 }
149 
150 int
151 _info(struct modinfo *modinfop)
152 {
153 	return (mod_info(&modlinkage, modinfop));
154 }
155 
156 int
157 _fini(void)
158 {
159 	return (mod_remove(&modlinkage));
160 }
161