xref: /illumos-gate/usr/src/uts/sun4v/os/mach_mp_startup.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2019 Peter Tribble.
28  */
29 
30 #include <sys/machsystm.h>
31 #include <sys/cpu_module.h>
32 #include <sys/dtrace.h>
33 #include <sys/cpu_sgnblk_defs.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 #include <sys/mpo.h>
39 
40 /*
41  * Useful for disabling MP bring-up for an MP capable kernel
42  * (a kernel that was built with MP defined)
43  */
44 int use_mp = 1;			/* set to come up mp */
45 
46 /*
47  * Init CPU info - get CPU type info for processor_info system call.
48  */
49 void
50 init_cpu_info(struct cpu *cp)
51 {
52 	processor_info_t *pi = &cp->cpu_type_info;
53 	int cpuid = cp->cpu_id;
54 	struct cpu_node *cpunode = &cpunodes[cpuid];
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 	/*
77 	 * cpu0 is always initialized at boot time, but it can be initialized
78 	 * again if it is dynamically removed and then re-added. We check if
79 	 * we are booting by verifying cpu_list. During boot, cpu0 is already
80 	 * in cpu_list when this function is called. When a cpu is dynamically
81 	 * added (after the boot) then it is added to cpu_list after this
82 	 * function is called.
83 	 */
84 	if (cpuid == cpu0.cpu_id && ncpus == 1 && cpu_list[0].cpu_id == cpuid) {
85 		/*
86 		 * cpu0 starts out running.  Other cpus are
87 		 * still in OBP land and we will leave them
88 		 * alone for now.
89 		 */
90 		CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cpuid);
91 		/*
92 		 * On first cpu setup, tell hv we are booting
93 		 */
94 		mach_set_soft_state(SIS_TRANSITION,
95 		    &SOLARIS_SOFT_STATE_BOOT_MSG);
96 #ifdef	lint
97 		cpuid = cpuid;
98 #endif	/* lint */
99 	}
100 }
101 
102 /*
103  * Routine used to cleanup a CPU that has been powered off. This will
104  * destroy all per-cpu information related to this cpu.
105  */
106 int
107 mp_cpu_unconfigure(int cpuid)
108 {
109 	int retval;
110 	extern void empty_cpu(int);
111 	extern int cleanup_cpu_common(int);
112 
113 	ASSERT(MUTEX_HELD(&cpu_lock));
114 
115 	retval = cleanup_cpu_common(cpuid);
116 
117 	empty_cpu(cpuid);
118 
119 	mpo_cpu_remove(cpuid);
120 
121 	return (retval);
122 }
123 
124 struct mp_find_cpu_arg {
125 	int cpuid;		/* set by mp_cpu_configure() */
126 	dev_info_t *dip;	/* set by mp_find_cpu() */
127 };
128 
129 int
130 mp_find_cpu(dev_info_t *dip, void *arg)
131 {
132 	struct mp_find_cpu_arg *target = (struct mp_find_cpu_arg *)arg;
133 	char	*type;
134 	int	rv = DDI_WALK_CONTINUE;
135 	int	cpuid;
136 
137 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
138 	    DDI_PROP_DONTPASS, "device_type", &type))
139 		return (DDI_WALK_CONTINUE);
140 
141 	if (strcmp(type, "cpu") != 0)
142 		goto out;
143 
144 	cpuid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
145 	    DDI_PROP_DONTPASS, "reg", -1);
146 
147 	if (cpuid == -1) {
148 		cmn_err(CE_PANIC, "reg prop not found in cpu node");
149 	}
150 
151 	cpuid = PROM_CFGHDL_TO_CPUID(cpuid);
152 
153 	if (cpuid != target->cpuid)
154 		goto out;
155 
156 	/* Found it */
157 	rv = DDI_WALK_TERMINATE;
158 	target->dip = dip;
159 
160 out:
161 	ddi_prop_free(type);
162 	return (rv);
163 }
164 
165 /*
166  * Routine used to setup a newly inserted CPU in preparation for starting
167  * it running code.
168  */
169 int
170 mp_cpu_configure(int cpuid)
171 {
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 	cpu_t		*cpu;
179 	processorid_t	id;
180 
181 	ASSERT(MUTEX_HELD(&cpu_lock));
182 
183 	if ((mdp = md_get_handle()) == NULL)
184 		return (ENODEV);
185 
186 	rootnode = md_root_node(mdp);
187 
188 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
189 
190 	num_nodes = md_node_count(mdp);
191 
192 	ASSERT(num_nodes > 0);
193 
194 	listsz = num_nodes * sizeof (mde_cookie_t);
195 	listp = kmem_zalloc(listsz, KM_SLEEP);
196 
197 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "cpu"),
198 	    md_find_name(mdp, "fwd"), listp);
199 
200 	if (num_nodes < 0)
201 		return (ENODEV);
202 
203 	for (i = 0; i < num_nodes; i++) {
204 		if (md_get_prop_val(mdp, listp[i], "id", &cpuid_prop))
205 			break;
206 		if (cpuid_prop == (uint64_t)cpuid) {
207 			cpunode = listp[i];
208 			break;
209 		}
210 	}
211 
212 	if (cpunode == MDE_INVAL_ELEM_COOKIE)
213 		return (ENODEV);
214 
215 	kmem_free(listp, listsz);
216 
217 	mpo_cpu_add(mdp, cpuid);
218 
219 	/*
220 	 * Note: uses cpu_lock to protect cpunodes
221 	 * which will be modified inside of fill_cpu and
222 	 * setup_exec_unit_mappings.
223 	 */
224 	fill_cpu(mdp, cpunode);
225 
226 	/*
227 	 * Adding a CPU may cause the execution unit sharing
228 	 * relationships to change. Update the mappings in
229 	 * the cpunode structures.
230 	 */
231 	setup_chip_mappings(mdp);
232 	setup_exec_unit_mappings(mdp);
233 
234 	/* propagate the updated mappings to the CPU structures */
235 	for (id = 0; id < NCPU; id++) {
236 		if ((cpu = cpu_get(id)) == NULL)
237 			continue;
238 
239 		cpu_map_exec_units(cpu);
240 	}
241 
242 	(void) md_fini_handle(mdp);
243 
244 	if ((i = setup_cpu_common(cpuid)) != 0) {
245 		(void) cleanup_cpu_common(cpuid);
246 		return (i);
247 	}
248 
249 	return (0);
250 }
251 
252 /*
253  * Platform-specific actions to be taken when all cpus are running
254  * in the OS.
255  */
256 void
257 cpu_mp_init(void)
258 {
259 	extern void recalc_xc_timeouts();
260 	extern int cif_cpu_mp_ready;
261 
262 	/* N.B. This must happen after xc_init() has run. */
263 	recalc_xc_timeouts();
264 
265 	if (!domaining_enabled())
266 		return;
267 
268 	cif_cpu_mp_ready = 1;
269 }
270 
271 void
272 populate_idstr(struct cpu *cp)
273 {
274 	char buf[CPU_IDSTRLEN];
275 	struct cpu_node *cpunode;
276 	processor_info_t *pi;
277 
278 	cpunode = &cpunodes[cp->cpu_id];
279 	pi = &cp->cpu_type_info;
280 	if (cp->cpu_m.cpu_chip == CPU_CHIPID_INVALID) {
281 		(void) snprintf(buf, sizeof (buf),
282 		    "%s (cpuid %d, clock %d MHz)",
283 		    cpunode->name, cpunode->cpuid, pi->pi_clock);
284 	} else {
285 		(void) snprintf(buf, sizeof (buf),
286 		    "%s (chipid %d, clock %d MHz)",
287 		    cpunode->name, cp->cpu_m.cpu_chip, pi->pi_clock);
288 	}
289 
290 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
291 	(void) strcpy(cp->cpu_idstr, buf);
292 
293 	cp->cpu_brandstr = kmem_alloc(strlen(cpunode->name) + 1, KM_SLEEP);
294 	(void) strcpy(cp->cpu_brandstr, cpunode->name);
295 
296 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
297 }
298