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 #include <sys/machsystm.h>
30 #include <sys/cpu_module.h>
31 #include <sys/dtrace.h>
32 #include <sys/cpu_sgnblk_defs.h>
33 #include <sys/mdesc.h>
34 #include <sys/mach_descrip.h>
35 #include <sys/ldoms.h>
36 #include <sys/hypervisor_api.h>
37 #include <sys/soft_state.h>
38 
39 /*
40  * Useful for disabling MP bring-up for an MP capable kernel
41  * (a kernel that was built with MP defined)
42  */
43 int use_mp = 1;			/* set to come up mp */
44 
45 /*
46  * Init CPU info - get CPU type info for processor_info system call.
47  */
48 void
49 init_cpu_info(struct cpu *cp)
50 {
51 	processor_info_t *pi = &cp->cpu_type_info;
52 	int cpuid = cp->cpu_id;
53 	struct cpu_node *cpunode = &cpunodes[cpuid];
54 	char buf[CPU_IDSTRLEN];
55 
56 	cp->cpu_fpowner = NULL;		/* not used for V9 */
57 
58 	/*
59 	 * Get clock-frequency property from cpunodes[] for the CPU.
60 	 */
61 	pi->pi_clock = (cpunode->clock_freq + 500000) / 1000000;
62 
63 	(void) strcpy(pi->pi_processor_type, "sparcv9");
64 	(void) strcpy(pi->pi_fputypes, "sparcv9");
65 
66 	(void) snprintf(buf, sizeof (buf),
67 	    "%s (cpuid %d clock %d MHz)",
68 	    cpunode->name, cpunode->cpuid, pi->pi_clock);
69 
70 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
71 	(void) strcpy(cp->cpu_idstr, buf);
72 
73 	cmn_err(CE_CONT, "?cpu%d: %s\n", cpuid, cp->cpu_idstr);
74 
75 	cp->cpu_brandstr = kmem_alloc(strlen(cpunode->name) + 1, KM_SLEEP);
76 	(void) strcpy(cp->cpu_brandstr, cpunode->name);
77 
78 	/*
79 	 * StarFire requires the signature block stuff setup here
80 	 */
81 	CPU_SGN_MAPIN(cpuid);
82 	if (cpuid == cpu0.cpu_id) {
83 		/*
84 		 * cpu0 starts out running.  Other cpus are
85 		 * still in OBP land and we will leave them
86 		 * alone for now.
87 		 */
88 		CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cpuid);
89 		/*
90 		 * On first cpu setup, tell hv we are booting
91 		 */
92 		mach_set_soft_state(SIS_TRANSITION,
93 				&SOLARIS_SOFT_STATE_BOOT_MSG);
94 #ifdef	lint
95 		cpuid = cpuid;
96 #endif	/* lint */
97 	}
98 }
99 
100 /*
101  * Routine used to cleanup a CPU that has been powered off. This will
102  * destroy all per-cpu information related to this cpu.
103  */
104 int
105 mp_cpu_unconfigure(int cpuid)
106 {
107 	int retval;
108 	extern void empty_cpu(int);
109 	extern int cleanup_cpu_common(int);
110 
111 	ASSERT(MUTEX_HELD(&cpu_lock));
112 
113 	retval = cleanup_cpu_common(cpuid);
114 
115 	empty_cpu(cpuid);
116 
117 	return (retval);
118 }
119 
120 struct mp_find_cpu_arg {
121 	int cpuid;		/* set by mp_cpu_configure() */
122 	dev_info_t *dip;	/* set by mp_find_cpu() */
123 };
124 
125 int
126 mp_find_cpu(dev_info_t *dip, void *arg)
127 {
128 	struct mp_find_cpu_arg *target = (struct mp_find_cpu_arg *)arg;
129 	char	*type;
130 	int	rv = DDI_WALK_CONTINUE;
131 	int	cpuid;
132 
133 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
134 	    DDI_PROP_DONTPASS, "device_type", &type))
135 		return (DDI_WALK_CONTINUE);
136 
137 	if (strcmp(type, "cpu") != 0)
138 		goto out;
139 
140 	cpuid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
141 	    DDI_PROP_DONTPASS, "reg", -1);
142 
143 	if (cpuid == -1) {
144 		cmn_err(CE_PANIC, "reg prop not found in cpu node");
145 	}
146 
147 	cpuid = PROM_CFGHDL_TO_CPUID(cpuid);
148 
149 	if (cpuid != target->cpuid)
150 		goto out;
151 
152 	/* Found it */
153 	rv = DDI_WALK_TERMINATE;
154 	target->dip = dip;
155 
156 out:
157 	ddi_prop_free(type);
158 	return (rv);
159 }
160 
161 /*
162  * Routine used to setup a newly inserted CPU in preparation for starting
163  * it running code.
164  */
165 int
166 mp_cpu_configure(int cpuid)
167 {
168 	extern void fill_cpu(md_t *, mde_cookie_t);
169 	extern void setup_cpu_common(int);
170 	extern void setup_exec_unit_mappings(md_t *);
171 	md_t *mdp;
172 	mde_cookie_t rootnode, cpunode = MDE_INVAL_ELEM_COOKIE;
173 	int listsz, i;
174 	mde_cookie_t *listp = NULL;
175 	int	num_nodes;
176 	uint64_t cpuid_prop;
177 
178 
179 	ASSERT(MUTEX_HELD(&cpu_lock));
180 
181 	if ((mdp = md_get_handle()) == NULL)
182 		return (ENODEV);
183 
184 	rootnode = md_root_node(mdp);
185 
186 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
187 
188 	num_nodes = md_node_count(mdp);
189 
190 	ASSERT(num_nodes > 0);
191 
192 	listsz = num_nodes * sizeof (mde_cookie_t);
193 	listp = kmem_zalloc(listsz, KM_SLEEP);
194 
195 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "cpu"),
196 	    md_find_name(mdp, "fwd"), listp);
197 
198 	if (num_nodes < 0)
199 		return (ENODEV);
200 
201 	for (i = 0; i < num_nodes; i++) {
202 		if (md_get_prop_val(mdp, listp[i], "id", &cpuid_prop))
203 			break;
204 		if (cpuid_prop == (uint64_t)cpuid) {
205 			cpunode = listp[i];
206 			break;
207 		}
208 	}
209 
210 	if (cpunode == MDE_INVAL_ELEM_COOKIE)
211 		return (ENODEV);
212 
213 	kmem_free(listp, listsz);
214 
215 	/*
216 	 * Note: uses cpu_lock to protect cpunodes and ncpunodes
217 	 * which will be modified inside of fill_cpu and
218 	 * setup_exec_unit_mappings.
219 	 */
220 	fill_cpu(mdp, cpunode);
221 
222 	/*
223 	 * Remap all the cpunodes' execunit mappings.
224 	 */
225 	setup_exec_unit_mappings(mdp);
226 
227 	(void) md_fini_handle(mdp);
228 
229 	setup_cpu_common(cpuid);
230 
231 	return (0);
232 }
233 
234 /*
235  * Platform-specific actions to be taken when all cpus are running
236  * in the OS.
237  */
238 void
239 cpu_mp_init(void)
240 {
241 	extern void recalc_xc_timeouts();
242 	extern int cif_cpu_mp_ready;
243 
244 	/* N.B. This must happen after xc_init() has run. */
245 	recalc_xc_timeouts();
246 
247 	if (!(domaining_capabilities & DOMAINING_ENABLED))
248 		return;
249 
250 	cif_cpu_mp_ready = 1;
251 }
252