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