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