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