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