xref: /titanic_52/usr/src/uts/sun4v/os/mach_mp_startup.c (revision 80b80bf0416a7eb4be4215b2e192cafd03ca80b7)
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 
38 /*
39  * Useful for disabling MP bring-up for an MP capable kernel
40  * (a kernel that was built with MP defined)
41  */
42 int use_mp = 1;			/* set to come up mp */
43 
44 /*
45  * Init CPU info - get CPU type info for processor_info system call.
46  */
47 void
48 init_cpu_info(struct cpu *cp)
49 {
50 	processor_info_t *pi = &cp->cpu_type_info;
51 	int cpuid = cp->cpu_id;
52 	struct cpu_node *cpunode = &cpunodes[cpuid];
53 	char buf[CPU_IDSTRLEN];
54 
55 	cp->cpu_fpowner = NULL;		/* not used for V9 */
56 
57 	/*
58 	 * Get clock-frequency property from cpunodes[] for the CPU.
59 	 */
60 	pi->pi_clock = (cpunode->clock_freq + 500000) / 1000000;
61 
62 	/*
63 	 * Current frequency in Hz.
64 	 */
65 	cp->cpu_curr_clock = cpunode->clock_freq;
66 
67 	/*
68 	 * Supported frequencies.
69 	 */
70 	cpu_set_supp_freqs(cp, NULL);
71 
72 	(void) strcpy(pi->pi_processor_type, "sparcv9");
73 	(void) strcpy(pi->pi_fputypes, "sparcv9");
74 
75 	(void) snprintf(buf, sizeof (buf),
76 	    "%s (cpuid %d clock %d MHz)",
77 	    cpunode->name, cpunode->cpuid, pi->pi_clock);
78 
79 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
80 	(void) strcpy(cp->cpu_idstr, buf);
81 
82 	cmn_err(CE_CONT, "?cpu%d: %s\n", cpuid, cp->cpu_idstr);
83 
84 	cp->cpu_brandstr = kmem_alloc(strlen(cpunode->name) + 1, KM_SLEEP);
85 	(void) strcpy(cp->cpu_brandstr, cpunode->name);
86 
87 	/*
88 	 * StarFire requires the signature block stuff setup here
89 	 */
90 	CPU_SGN_MAPIN(cpuid);
91 	if (cpuid == cpu0.cpu_id) {
92 		/*
93 		 * cpu0 starts out running.  Other cpus are
94 		 * still in OBP land and we will leave them
95 		 * alone for now.
96 		 */
97 		CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cpuid);
98 		/*
99 		 * On first cpu setup, tell hv we are booting
100 		 */
101 		mach_set_soft_state(SIS_TRANSITION,
102 		    &SOLARIS_SOFT_STATE_BOOT_MSG);
103 #ifdef	lint
104 		cpuid = cpuid;
105 #endif	/* lint */
106 	}
107 }
108 
109 /*
110  * Routine used to cleanup a CPU that has been powered off. This will
111  * destroy all per-cpu information related to this cpu.
112  */
113 int
114 mp_cpu_unconfigure(int cpuid)
115 {
116 	int retval;
117 	extern void empty_cpu(int);
118 	extern int cleanup_cpu_common(int);
119 
120 	ASSERT(MUTEX_HELD(&cpu_lock));
121 
122 	retval = cleanup_cpu_common(cpuid);
123 
124 	empty_cpu(cpuid);
125 
126 	return (retval);
127 }
128 
129 struct mp_find_cpu_arg {
130 	int cpuid;		/* set by mp_cpu_configure() */
131 	dev_info_t *dip;	/* set by mp_find_cpu() */
132 };
133 
134 int
135 mp_find_cpu(dev_info_t *dip, void *arg)
136 {
137 	struct mp_find_cpu_arg *target = (struct mp_find_cpu_arg *)arg;
138 	char	*type;
139 	int	rv = DDI_WALK_CONTINUE;
140 	int	cpuid;
141 
142 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
143 	    DDI_PROP_DONTPASS, "device_type", &type))
144 		return (DDI_WALK_CONTINUE);
145 
146 	if (strcmp(type, "cpu") != 0)
147 		goto out;
148 
149 	cpuid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
150 	    DDI_PROP_DONTPASS, "reg", -1);
151 
152 	if (cpuid == -1) {
153 		cmn_err(CE_PANIC, "reg prop not found in cpu node");
154 	}
155 
156 	cpuid = PROM_CFGHDL_TO_CPUID(cpuid);
157 
158 	if (cpuid != target->cpuid)
159 		goto out;
160 
161 	/* Found it */
162 	rv = DDI_WALK_TERMINATE;
163 	target->dip = dip;
164 
165 out:
166 	ddi_prop_free(type);
167 	return (rv);
168 }
169 
170 /*
171  * Routine used to setup a newly inserted CPU in preparation for starting
172  * it running code.
173  */
174 int
175 mp_cpu_configure(int cpuid)
176 {
177 	md_t		*mdp;
178 	mde_cookie_t	rootnode, cpunode = MDE_INVAL_ELEM_COOKIE;
179 	int		listsz, i;
180 	mde_cookie_t	*listp = NULL;
181 	int		num_nodes;
182 	uint64_t	cpuid_prop;
183 	cpu_t		*cpu;
184 	processorid_t	id;
185 
186 	ASSERT(MUTEX_HELD(&cpu_lock));
187 
188 	if ((mdp = md_get_handle()) == NULL)
189 		return (ENODEV);
190 
191 	rootnode = md_root_node(mdp);
192 
193 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
194 
195 	num_nodes = md_node_count(mdp);
196 
197 	ASSERT(num_nodes > 0);
198 
199 	listsz = num_nodes * sizeof (mde_cookie_t);
200 	listp = kmem_zalloc(listsz, KM_SLEEP);
201 
202 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "cpu"),
203 	    md_find_name(mdp, "fwd"), listp);
204 
205 	if (num_nodes < 0)
206 		return (ENODEV);
207 
208 	for (i = 0; i < num_nodes; i++) {
209 		if (md_get_prop_val(mdp, listp[i], "id", &cpuid_prop))
210 			break;
211 		if (cpuid_prop == (uint64_t)cpuid) {
212 			cpunode = listp[i];
213 			break;
214 		}
215 	}
216 
217 	if (cpunode == MDE_INVAL_ELEM_COOKIE)
218 		return (ENODEV);
219 
220 	kmem_free(listp, listsz);
221 
222 	/*
223 	 * Note: uses cpu_lock to protect cpunodes
224 	 * which will be modified inside of fill_cpu and
225 	 * setup_exec_unit_mappings.
226 	 */
227 	fill_cpu(mdp, cpunode);
228 
229 	/*
230 	 * Adding a CPU may cause the execution unit sharing
231 	 * relationships to change. Update the mappings in
232 	 * the cpunode structures.
233 	 */
234 	setup_chip_mappings(mdp);
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