xref: /titanic_52/usr/src/uts/sun4v/os/mach_mp_startup.c (revision c0c79a3f09914f35651895ffc111883455b7f62d)
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 #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 int setup_cpu_common(int);
170 	extern int cleanup_cpu_common(int);
171 	extern void setup_exec_unit_mappings(md_t *);
172 	md_t *mdp;
173 	mde_cookie_t rootnode, cpunode = MDE_INVAL_ELEM_COOKIE;
174 	int listsz, i;
175 	mde_cookie_t *listp = NULL;
176 	int	num_nodes;
177 	uint64_t cpuid_prop;
178 
179 
180 	ASSERT(MUTEX_HELD(&cpu_lock));
181 
182 	if ((mdp = md_get_handle()) == NULL)
183 		return (ENODEV);
184 
185 	rootnode = md_root_node(mdp);
186 
187 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
188 
189 	num_nodes = md_node_count(mdp);
190 
191 	ASSERT(num_nodes > 0);
192 
193 	listsz = num_nodes * sizeof (mde_cookie_t);
194 	listp = kmem_zalloc(listsz, KM_SLEEP);
195 
196 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "cpu"),
197 	    md_find_name(mdp, "fwd"), listp);
198 
199 	if (num_nodes < 0)
200 		return (ENODEV);
201 
202 	for (i = 0; i < num_nodes; i++) {
203 		if (md_get_prop_val(mdp, listp[i], "id", &cpuid_prop))
204 			break;
205 		if (cpuid_prop == (uint64_t)cpuid) {
206 			cpunode = listp[i];
207 			break;
208 		}
209 	}
210 
211 	if (cpunode == MDE_INVAL_ELEM_COOKIE)
212 		return (ENODEV);
213 
214 	kmem_free(listp, listsz);
215 
216 	/*
217 	 * Note: uses cpu_lock to protect cpunodes and ncpunodes
218 	 * which will be modified inside of fill_cpu and
219 	 * setup_exec_unit_mappings.
220 	 */
221 	fill_cpu(mdp, cpunode);
222 
223 	/*
224 	 * Remap all the cpunodes' execunit mappings.
225 	 */
226 	setup_exec_unit_mappings(mdp);
227 
228 	(void) md_fini_handle(mdp);
229 
230 	if ((i = setup_cpu_common(cpuid)) != 0) {
231 		(void) cleanup_cpu_common(cpuid);
232 		return (i);
233 	}
234 	return (0);
235 }
236 
237 /*
238  * Platform-specific actions to be taken when all cpus are running
239  * in the OS.
240  */
241 void
242 cpu_mp_init(void)
243 {
244 	extern void recalc_xc_timeouts();
245 	extern int cif_cpu_mp_ready;
246 
247 	/* N.B. This must happen after xc_init() has run. */
248 	recalc_xc_timeouts();
249 
250 	if (!(domaining_capabilities & DOMAINING_ENABLED))
251 		return;
252 
253 	cif_cpu_mp_ready = 1;
254 }
255