1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/cpuvar.h>
#include <sys/cpu_module.h>
#include <sys/machsystm.h>
#include <sys/archsystm.h>
#include <sys/prom_plat.h>
#include <sys/hypervisor_api.h>
#include <sys/hsvc.h>
extern uint64_t xc_tick_limit;
extern uint64_t xc_tick_jump_limit;
extern void cpu_intrq_unregister_powerdown(uint64_t doneflag_va);
/*
* set_idle_cpu is called from idle() when a CPU becomes idle.
*/
/*ARGSUSED*/
void
set_idle_cpu(int cpun)
{
}
/*
* unset_idle_cpu is called from idle() when a CPU is no longer idle.
*/
/*ARGSUSED*/
void
unset_idle_cpu(int cpun)
{
}
/*
* Stop a CPU based on its cpuid, using the cpu_stop hypervisor call.
* Since this requires that the hypervisor force a remote CPU to stop,
* the assumption is made that this should take roughly the same amount
* of time as a executing a cross-call. Consequently, the xcall
* timeout is used to determine when to give up waiting for the CPU to
* stop.
*
* Attempts to stop a CPU already in the stopped or error state will
* silently succeed. Zero is returned on success and a non-negative
* errno value is returned on failure.
*/
int
stopcpu_bycpuid(int cpuid)
{
uint64_t loop_cnt;
uint64_t state;
uint64_t rv;
uint64_t major = 0;
uint64_t minor = 0;
uint64_t cpu_stop_time_limit;
extern uint64_t xc_func_time_limit;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Check the state of the CPU up front to see if an
* attempt to stop it is even necessary.
*/
if (hv_cpu_state(cpuid, &state) != H_EOK)
return (EINVAL);
/* treat stopped and error state the same */
if (state != CPU_STATE_RUNNING) {
/* nothing to do */
return (0);
}
/*
* The HV API to stop a CPU is only supported in
* version 1.1 and later of the core group. If an
* older version of the HV is in use, return not
* supported.
*/
if (hsvc_version(HSVC_GROUP_CORE, &major, &minor) != 0)
return (EINVAL);
ASSERT(major != 0);
if ((major == 1) && (minor < 1))
return (ENOTSUP);
/* use the mondo timeout if it has been initialized */
cpu_stop_time_limit = xc_func_time_limit;
/*
* If called early in boot before the mondo time limit
* is set, use a reasonable timeout based on the the
* clock frequency of the current CPU.
*/
if (cpu_stop_time_limit == 0)
cpu_stop_time_limit = cpunodes[CPU->cpu_id].clock_freq;
/* should only fail if called too early in boot */
ASSERT(cpu_stop_time_limit > 0);
loop_cnt = 0;
/*
* Attempt to stop the CPU, retrying if it is busy.
*/
while (loop_cnt++ < cpu_stop_time_limit) {
if ((rv = hv_cpu_stop(cpuid)) != H_EWOULDBLOCK)
break;
}
if (loop_cnt == cpu_stop_time_limit)
return (ETIMEDOUT);
if (rv != H_EOK)
return (EINVAL);
/*
* Verify that the CPU has reached the stopped state.
*/
while (loop_cnt++ < cpu_stop_time_limit) {
if (hv_cpu_state(cpuid, &state) != H_EOK)
return (EINVAL);
/* treat stopped and error state the same */
if (state != CPU_STATE_RUNNING)
break;
}
return ((loop_cnt == cpu_stop_time_limit) ? ETIMEDOUT : 0);
}
/*
* X-trap to the target to unregister its interrupt and error queues
* and put it in a safe place just before the CPU is stopped. After
* unregistering its queues, the target CPU must not return from the
* trap to priv or user context. Ensure that the interrupt CPU unregister
* succeeded.
*/
void
xt_cpu_unreg_powerdown(struct cpu *cpup)
{
uint8_t volatile not_done;
uint64_t starttick, endtick, tick, lasttick;
processorid_t cpuid = cpup->cpu_id;
kpreempt_disable();
/*
* Sun4v uses a queue for receiving mondos. Successful
* transmission of a mondo only indicates that the mondo
* has been written into the queue.
*
* Set the not_done flag to 1 before sending the cross
* trap and wait until the other cpu resets it to 0.
*/
not_done = 1;
xt_one_unchecked(cpuid, (xcfunc_t *)cpu_intrq_unregister_powerdown,
(uint64_t)¬_done, 0);
starttick = lasttick = gettick();
endtick = starttick + xc_tick_limit;
while (not_done) {
tick = gettick();
/*
* If there is a big jump between the current tick
* count and lasttick, we have probably hit a break
* point. Adjust endtick accordingly to avoid panic.
*/
if (tick > (lasttick + xc_tick_jump_limit)) {
endtick += (tick - lasttick);
}
lasttick = tick;
if (tick > endtick) {
cmn_err(CE_CONT, "Cross trap timeout at cpu id %x\n",
cpuid);
cmn_err(CE_WARN, "xt_intrq_unreg_powerdown: timeout");
}
}
kpreempt_enable();
}
int
plat_cpu_poweroff(struct cpu *cp)
{
int rv = 0;
int status;
processorid_t cpuid = cp->cpu_id;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Capture all CPUs (except for detaching proc) to prevent
* crosscalls to the detaching proc until it has cleared its
* bit in cpu_ready_set.
*
* The CPU's remain paused and the prom_mutex is known to be free.
* This prevents the x-trap victim from blocking when doing prom
* IEEE-1275 calls at a high PIL level.
*/
promsafe_pause_cpus();
/*
* Quiesce interrupts on the target CPU. We do this by setting
* the CPU 'not ready'- (i.e. removing the CPU from cpu_ready_set)
* to prevent it from receiving cross calls and cross traps. This
* prevents the processor from receiving any new soft interrupts.
*/
mp_cpu_quiesce(cp);
/*
* Send a cross trap to the cpu to unregister its interrupt
* error queues.
*/
xt_cpu_unreg_powerdown(cp);
cp->cpu_flags = CPU_OFFLINE | CPU_QUIESCED | CPU_POWEROFF;
/* call into the Hypervisor to stop the CPU */
if ((status = stopcpu_bycpuid(cpuid)) != 0) {
rv = -1;
}
start_cpus();
if (rv != 0) {
cmn_err(CE_WARN, "failed to stop cpu %d (%d)", cpuid, status);
/* mark the CPU faulted so that it cannot be onlined */
cp->cpu_flags = CPU_OFFLINE | CPU_QUIESCED | CPU_FAULTED;
}
return (rv);
}
int
plat_cpu_poweron(struct cpu *cp)
{
extern void restart_other_cpu(int);
ASSERT(MUTEX_HELD(&cpu_lock));
cp->cpu_flags &= ~CPU_POWEROFF;
restart_other_cpu(cp->cpu_id);
return (0);
}
|