xref: /titanic_50/usr/src/uts/sun4v/os/mach_mp_startup.c (revision 63602c90af2802f2ab666ab39bb4aea7d02500c3)
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 	/*
64 	 * Current frequency in Hz.
65 	 */
66 	cp->cpu_curr_clock = cpunode->clock_freq;
67 
68 	(void) strcpy(pi->pi_processor_type, "sparcv9");
69 	(void) strcpy(pi->pi_fputypes, "sparcv9");
70 
71 	(void) snprintf(buf, sizeof (buf),
72 	    "%s (cpuid %d clock %d MHz)",
73 	    cpunode->name, cpunode->cpuid, pi->pi_clock);
74 
75 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
76 	(void) strcpy(cp->cpu_idstr, buf);
77 
78 	cmn_err(CE_CONT, "?cpu%d: %s\n", cpuid, cp->cpu_idstr);
79 
80 	cp->cpu_brandstr = kmem_alloc(strlen(cpunode->name) + 1, KM_SLEEP);
81 	(void) strcpy(cp->cpu_brandstr, cpunode->name);
82 
83 	/*
84 	 * StarFire requires the signature block stuff setup here
85 	 */
86 	CPU_SGN_MAPIN(cpuid);
87 	if (cpuid == cpu0.cpu_id) {
88 		/*
89 		 * cpu0 starts out running.  Other cpus are
90 		 * still in OBP land and we will leave them
91 		 * alone for now.
92 		 */
93 		CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cpuid);
94 		/*
95 		 * On first cpu setup, tell hv we are booting
96 		 */
97 		mach_set_soft_state(SIS_TRANSITION,
98 		    &SOLARIS_SOFT_STATE_BOOT_MSG);
99 #ifdef	lint
100 		cpuid = cpuid;
101 #endif	/* lint */
102 	}
103 }
104 
105 /*
106  * Routine used to cleanup a CPU that has been powered off. This will
107  * destroy all per-cpu information related to this cpu.
108  */
109 int
110 mp_cpu_unconfigure(int cpuid)
111 {
112 	int retval;
113 	extern void empty_cpu(int);
114 	extern int cleanup_cpu_common(int);
115 
116 	ASSERT(MUTEX_HELD(&cpu_lock));
117 
118 	retval = cleanup_cpu_common(cpuid);
119 
120 	empty_cpu(cpuid);
121 
122 	return (retval);
123 }
124 
125 struct mp_find_cpu_arg {
126 	int cpuid;		/* set by mp_cpu_configure() */
127 	dev_info_t *dip;	/* set by mp_find_cpu() */
128 };
129 
130 int
131 mp_find_cpu(dev_info_t *dip, void *arg)
132 {
133 	struct mp_find_cpu_arg *target = (struct mp_find_cpu_arg *)arg;
134 	char	*type;
135 	int	rv = DDI_WALK_CONTINUE;
136 	int	cpuid;
137 
138 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
139 	    DDI_PROP_DONTPASS, "device_type", &type))
140 		return (DDI_WALK_CONTINUE);
141 
142 	if (strcmp(type, "cpu") != 0)
143 		goto out;
144 
145 	cpuid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
146 	    DDI_PROP_DONTPASS, "reg", -1);
147 
148 	if (cpuid == -1) {
149 		cmn_err(CE_PANIC, "reg prop not found in cpu node");
150 	}
151 
152 	cpuid = PROM_CFGHDL_TO_CPUID(cpuid);
153 
154 	if (cpuid != target->cpuid)
155 		goto out;
156 
157 	/* Found it */
158 	rv = DDI_WALK_TERMINATE;
159 	target->dip = dip;
160 
161 out:
162 	ddi_prop_free(type);
163 	return (rv);
164 }
165 
166 /*
167  * Routine used to setup a newly inserted CPU in preparation for starting
168  * it running code.
169  */
170 int
171 mp_cpu_configure(int cpuid)
172 {
173 	extern void fill_cpu(md_t *, mde_cookie_t);
174 	extern int setup_cpu_common(int);
175 	extern int cleanup_cpu_common(int);
176 	extern void setup_exec_unit_mappings(md_t *);
177 
178 	md_t		*mdp;
179 	mde_cookie_t	rootnode, cpunode = MDE_INVAL_ELEM_COOKIE;
180 	int		listsz, i;
181 	mde_cookie_t	*listp = NULL;
182 	int		num_nodes;
183 	uint64_t	cpuid_prop;
184 	cpu_t		*cpu;
185 	processorid_t	id;
186 
187 	ASSERT(MUTEX_HELD(&cpu_lock));
188 
189 	if ((mdp = md_get_handle()) == NULL)
190 		return (ENODEV);
191 
192 	rootnode = md_root_node(mdp);
193 
194 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
195 
196 	num_nodes = md_node_count(mdp);
197 
198 	ASSERT(num_nodes > 0);
199 
200 	listsz = num_nodes * sizeof (mde_cookie_t);
201 	listp = kmem_zalloc(listsz, KM_SLEEP);
202 
203 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "cpu"),
204 	    md_find_name(mdp, "fwd"), listp);
205 
206 	if (num_nodes < 0)
207 		return (ENODEV);
208 
209 	for (i = 0; i < num_nodes; i++) {
210 		if (md_get_prop_val(mdp, listp[i], "id", &cpuid_prop))
211 			break;
212 		if (cpuid_prop == (uint64_t)cpuid) {
213 			cpunode = listp[i];
214 			break;
215 		}
216 	}
217 
218 	if (cpunode == MDE_INVAL_ELEM_COOKIE)
219 		return (ENODEV);
220 
221 	kmem_free(listp, listsz);
222 
223 	/*
224 	 * Note: uses cpu_lock to protect cpunodes
225 	 * which will be modified inside of fill_cpu and
226 	 * setup_exec_unit_mappings.
227 	 */
228 	fill_cpu(mdp, cpunode);
229 
230 	/*
231 	 * Adding a CPU may cause the execution unit sharing
232 	 * relationships to change. Update the mappings in
233 	 * the cpunode structures.
234 	 */
235 	setup_exec_unit_mappings(mdp);
236 
237 	/* propagate the updated mappings to the CPU structures */
238 	for (id = 0; id < NCPU; id++) {
239 		if ((cpu = cpu_get(id)) == NULL)
240 			continue;
241 
242 		cpu_map_exec_units(cpu);
243 	}
244 
245 	(void) md_fini_handle(mdp);
246 
247 	if ((i = setup_cpu_common(cpuid)) != 0) {
248 		(void) cleanup_cpu_common(cpuid);
249 		return (i);
250 	}
251 
252 	return (0);
253 }
254 
255 /*
256  * Platform-specific actions to be taken when all cpus are running
257  * in the OS.
258  */
259 void
260 cpu_mp_init(void)
261 {
262 	extern void recalc_xc_timeouts();
263 	extern int cif_cpu_mp_ready;
264 
265 	/* N.B. This must happen after xc_init() has run. */
266 	recalc_xc_timeouts();
267 
268 	if (!domaining_enabled())
269 		return;
270 
271 	cif_cpu_mp_ready = 1;
272 }
273