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 	/*
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 	return (retval);
128 }
129 
130 struct mp_find_cpu_arg {
131 	int cpuid;		/* set by mp_cpu_configure() */
132 	dev_info_t *dip;	/* set by mp_find_cpu() */
133 };
134 
135 int
136 mp_find_cpu(dev_info_t *dip, void *arg)
137 {
138 	struct mp_find_cpu_arg *target = (struct mp_find_cpu_arg *)arg;
139 	char	*type;
140 	int	rv = DDI_WALK_CONTINUE;
141 	int	cpuid;
142 
143 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
144 	    DDI_PROP_DONTPASS, "device_type", &type))
145 		return (DDI_WALK_CONTINUE);
146 
147 	if (strcmp(type, "cpu") != 0)
148 		goto out;
149 
150 	cpuid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
151 	    DDI_PROP_DONTPASS, "reg", -1);
152 
153 	if (cpuid == -1) {
154 		cmn_err(CE_PANIC, "reg prop not found in cpu node");
155 	}
156 
157 	cpuid = PROM_CFGHDL_TO_CPUID(cpuid);
158 
159 	if (cpuid != target->cpuid)
160 		goto out;
161 
162 	/* Found it */
163 	rv = DDI_WALK_TERMINATE;
164 	target->dip = dip;
165 
166 out:
167 	ddi_prop_free(type);
168 	return (rv);
169 }
170 
171 /*
172  * Routine used to setup a newly inserted CPU in preparation for starting
173  * it running code.
174  */
175 int
176 mp_cpu_configure(int cpuid)
177 {
178 	extern void fill_cpu(md_t *, mde_cookie_t);
179 	extern int setup_cpu_common(int);
180 	extern int cleanup_cpu_common(int);
181 	extern void setup_exec_unit_mappings(md_t *);
182 
183 	md_t		*mdp;
184 	mde_cookie_t	rootnode, cpunode = MDE_INVAL_ELEM_COOKIE;
185 	int		listsz, i;
186 	mde_cookie_t	*listp = NULL;
187 	int		num_nodes;
188 	uint64_t	cpuid_prop;
189 	cpu_t		*cpu;
190 	processorid_t	id;
191 
192 	ASSERT(MUTEX_HELD(&cpu_lock));
193 
194 	if ((mdp = md_get_handle()) == NULL)
195 		return (ENODEV);
196 
197 	rootnode = md_root_node(mdp);
198 
199 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
200 
201 	num_nodes = md_node_count(mdp);
202 
203 	ASSERT(num_nodes > 0);
204 
205 	listsz = num_nodes * sizeof (mde_cookie_t);
206 	listp = kmem_zalloc(listsz, KM_SLEEP);
207 
208 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "cpu"),
209 	    md_find_name(mdp, "fwd"), listp);
210 
211 	if (num_nodes < 0)
212 		return (ENODEV);
213 
214 	for (i = 0; i < num_nodes; i++) {
215 		if (md_get_prop_val(mdp, listp[i], "id", &cpuid_prop))
216 			break;
217 		if (cpuid_prop == (uint64_t)cpuid) {
218 			cpunode = listp[i];
219 			break;
220 		}
221 	}
222 
223 	if (cpunode == MDE_INVAL_ELEM_COOKIE)
224 		return (ENODEV);
225 
226 	kmem_free(listp, listsz);
227 
228 	/*
229 	 * Note: uses cpu_lock to protect cpunodes
230 	 * which will be modified inside of fill_cpu and
231 	 * setup_exec_unit_mappings.
232 	 */
233 	fill_cpu(mdp, cpunode);
234 
235 	/*
236 	 * Adding a CPU may cause the execution unit sharing
237 	 * relationships to change. Update the mappings in
238 	 * the cpunode structures.
239 	 */
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