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 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25/*
26 * Copyright 2019 Peter Tribble.
27 */
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
34/*
35 * Useful for disabling MP bring-up for an MP capable kernel
36 * (a kernel that was built with MP defined)
37 */
38int use_mp = 1;			/* set to come up mp */
39
40/*
41 * Init CPU info - get CPU type info for processor_info system call.
42 */
43void
44init_cpu_info(struct cpu *cp)
45{
46	processor_info_t *pi = &cp->cpu_type_info;
47	int cpuid = cp->cpu_id;
48	struct cpu_node *cpunode = &cpunodes[cpuid];
49
50	cp->cpu_fpowner = NULL;		/* not used for V9 */
51
52	/*
53	 * Get clock-frequency property from cpunodes[] for the CPU.
54	 */
55	pi->pi_clock = (cpunode->clock_freq + 500000) / 1000000;
56
57	/*
58	 * Current frequency in Hz.
59	 */
60	cp->cpu_curr_clock = cpunode->clock_freq;
61
62	/*
63	 * Supported frequencies.
64	 */
65	cpu_set_supp_freqs(cp, NULL);
66
67	(void) strcpy(pi->pi_processor_type, "sparcv9");
68	(void) strcpy(pi->pi_fputypes, "sparcv9");
69
70	if (cpuid == cpu0.cpu_id) {
71		/*
72		 * cpu0 starts out running.  Other cpus are
73		 * still in OBP land and we will leave them
74		 * alone for now.
75		 */
76		CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cpuid);
77#ifdef	lint
78		cpuid = cpuid;
79#endif	/* lint */
80	}
81}
82
83/*
84 * Routine used to cleanup a CPU that has been powered off.  This will
85 * destroy all per-cpu information related to this cpu.
86 */
87int
88mp_cpu_unconfigure(int cpuid)
89{
90	int retval;
91	void empty_cpu(int);
92	extern int cleanup_cpu_common(int);
93
94	ASSERT(MUTEX_HELD(&cpu_lock));
95
96	retval = cleanup_cpu_common(cpuid);
97
98	empty_cpu(cpuid);
99
100	return (retval);
101}
102
103struct mp_find_cpu_arg {
104	int cpuid;		/* set by mp_cpu_configure() */
105	dev_info_t *dip;	/* set by mp_find_cpu() */
106};
107
108int
109mp_find_cpu(dev_info_t *dip, void *arg)
110{
111	extern int get_portid_ddi(dev_info_t *, dev_info_t **);
112	struct mp_find_cpu_arg *target = (struct mp_find_cpu_arg *)arg;
113	char *type;
114	int rv = DDI_WALK_CONTINUE;
115	int cpuid;
116
117	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
118	    "device_type", &type))
119		return (DDI_WALK_CONTINUE);
120
121	if (strcmp(type, "cpu") != 0)
122		goto out;
123
124	cpuid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
125	    DDI_PROP_DONTPASS, "cpuid", -1);
126
127	if (cpuid == -1)
128		cpuid = get_portid_ddi(dip, NULL);
129	if (cpuid != target->cpuid)
130		goto out;
131
132	/* Found it */
133	rv = DDI_WALK_TERMINATE;
134	target->dip = dip;
135
136out:
137	ddi_prop_free(type);
138	return (rv);
139}
140
141/*
142 * Routine used to setup a newly inserted CPU in preparation for starting
143 * it running code.
144 */
145int
146mp_cpu_configure(int cpuid)
147{
148	extern void fill_cpu_ddi(dev_info_t *);
149	extern int setup_cpu_common(int);
150	struct mp_find_cpu_arg target;
151
152	ASSERT(MUTEX_HELD(&cpu_lock));
153
154	target.dip = NULL;
155	target.cpuid = cpuid;
156	ddi_walk_devs(ddi_root_node(), mp_find_cpu, &target);
157
158	if (target.dip == NULL)
159		return (ENODEV);
160
161	/*
162	 * Note:  uses cpu_lock to protect cpunodes and ncpunodes
163	 * which will be modified inside of fill_cpu_ddi().
164	 */
165	fill_cpu_ddi(target.dip);
166
167	/*
168	 * sun4v cpu setup may fail. sun4u assumes cpu setup to
169	 * be always successful, so the return value is ignored.
170	 */
171	(void) setup_cpu_common(cpuid);
172
173	return (0);
174}
175
176void
177populate_idstr(struct cpu *cp)
178{
179	char buf[CPU_IDSTRLEN];
180	struct cpu_node *cpunode;
181	processor_info_t *pi;
182
183	cpunode = &cpunodes[cp->cpu_id];
184	pi = &cp->cpu_type_info;
185	(void) snprintf(buf, sizeof (buf),
186	    "%s (portid %d impl 0x%x ver 0x%x clock %d MHz)",
187	    cpunode->name, cpunode->portid, cpunode->implementation,
188	    cpunode->version, pi->pi_clock);
189	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
190	(void) strcpy(cp->cpu_idstr, buf);
191
192	cp->cpu_brandstr = kmem_alloc(strlen(cpunode->name) + 1, KM_SLEEP);
193	(void) strcpy(cp->cpu_brandstr, cpunode->name);
194
195	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
196}
197