forked from avpatel/xvisor-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmm_smp.c
398 lines (334 loc) · 8.76 KB
/
vmm_smp.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* Copyright (c) 2013 Anup Patel.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* @file vmm_smp.c
* @author Anup Patel ([email protected])
* @brief Symetric Multiprocessor Mamagment APIs Implementation
*/
#include <vmm_error.h>
#include <vmm_limits.h>
#include <vmm_percpu.h>
#include <vmm_smp.h>
#include <vmm_cpuhp.h>
#include <vmm_delay.h>
#include <vmm_stdio.h>
#include <vmm_timer.h>
#include <vmm_completion.h>
#include <vmm_manager.h>
#include <libs/fifo.h>
/* SMP processor ID for Boot CPU */
static u32 smp_bootcpu_id = UINT_MAX;
int vmm_smp_map_cpuid(unsigned long hwid, u32 *cpu)
{
u32 c;
int rc;
unsigned long thwid;
if (!cpu)
return VMM_EINVALID;
for_each_possible_cpu(c) {
rc = vmm_smp_map_hwid(c, &thwid);
if (rc)
return rc;
if (thwid == hwid) {
*cpu = c;
return VMM_OK;
}
}
return VMM_ENOENT;
}
u32 vmm_smp_bootcpu_id(void)
{
return smp_bootcpu_id;
}
void vmm_smp_set_bootcpu(void)
{
u32 cpu = vmm_smp_processor_id();
if ((smp_bootcpu_id == UINT_MAX) &&
(cpu < CONFIG_CPU_COUNT)) {
smp_bootcpu_id = cpu;
}
}
bool vmm_smp_is_bootcpu(void)
{
if (smp_bootcpu_id == UINT_MAX) {
return FALSE;
}
return (smp_bootcpu_id == vmm_smp_processor_id()) ? TRUE : FALSE;
}
/* Theoretically, number of host CPUs making Sync IPI
* simultaneously to a host CPU should not be more than
* maximum possible hardware CPUs but, we keep minimum
* Sync IPIs per host CPU to max possible VCPUs.
*/
#define SMP_IPI_MAX_SYNC_PER_CPU (CONFIG_MAX_VCPU_COUNT)
/* Various trials show that having minimum of 64 Async IPI
* per host CPU is good enough. If we require to increase
* this limit then we should have a config option.
*/
#define SMP_IPI_MAX_ASYNC_PER_CPU (64)
#define SMP_IPI_WAIT_TRY_COUNT 100
#define SMP_IPI_WAIT_UDELAY 1000
#define IPI_VCPU_STACK_SZ CONFIG_THREAD_STACK_SIZE
#define IPI_VCPU_PRIORITY VMM_VCPU_MAX_PRIORITY
#define IPI_VCPU_TIMESLICE VMM_VCPU_DEF_TIME_SLICE
#define IPI_VCPU_DEADLINE VMM_VCPU_DEF_DEADLINE
#define IPI_VCPU_PERIODICITY VMM_VCPU_DEF_PERIODICITY
struct smp_ipi_call {
u32 src_cpu;
u32 dst_cpu;
void (*func)(void *, void *, void *);
void *arg0;
void *arg1;
void *arg2;
};
struct smp_ipi_ctrl {
struct fifo *sync_fifo;
struct fifo *async_fifo;
struct vmm_completion async_avail;
struct vmm_vcpu *async_vcpu;
};
static DEFINE_PER_CPU(struct smp_ipi_ctrl, ictl);
static void smp_ipi_sync_submit(struct smp_ipi_ctrl *ictlp,
struct smp_ipi_call *ipic)
{
int try;
if (!ipic || !ipic->func) {
return;
}
try = SMP_IPI_WAIT_TRY_COUNT;
while (!fifo_enqueue(ictlp->sync_fifo, ipic, FALSE) && try) {
arch_smp_ipi_trigger(vmm_cpumask_of(ipic->dst_cpu));
vmm_udelay(SMP_IPI_WAIT_UDELAY);
try--;
}
if (!try) {
vmm_panic("CPU%d: IPI sync fifo full\n", ipic->dst_cpu);
}
arch_smp_ipi_trigger(vmm_cpumask_of(ipic->dst_cpu));
}
static void smp_ipi_async_submit(struct smp_ipi_ctrl *ictlp,
struct smp_ipi_call *ipic)
{
int try;
if (!ipic || !ipic->func) {
return;
}
try = SMP_IPI_WAIT_TRY_COUNT;
while (!fifo_enqueue(ictlp->async_fifo, ipic, FALSE) && try) {
arch_smp_ipi_trigger(vmm_cpumask_of(ipic->dst_cpu));
vmm_udelay(SMP_IPI_WAIT_UDELAY);
try--;
}
if (!try) {
vmm_panic("CPU%d: IPI async fifo full\n", ipic->dst_cpu);
}
arch_smp_ipi_trigger(vmm_cpumask_of(ipic->dst_cpu));
}
static void smp_ipi_main(void)
{
struct smp_ipi_call ipic;
struct smp_ipi_ctrl *ictlp = &this_cpu(ictl);
while (1) {
/* Wait for some IPI to be available */
vmm_completion_wait(&ictlp->async_avail);
/* Process async IPIs */
while (fifo_dequeue(ictlp->async_fifo, &ipic)) {
if (ipic.func) {
ipic.func(ipic.arg0, ipic.arg1, ipic.arg2);
}
}
}
}
void vmm_smp_ipi_exec(void)
{
struct smp_ipi_call ipic;
struct smp_ipi_ctrl *ictlp = &this_cpu(ictl);
/* Process Sync IPIs */
while (fifo_dequeue(ictlp->sync_fifo, &ipic)) {
if (ipic.func) {
ipic.func(ipic.arg0, ipic.arg1, ipic.arg2);
}
}
/* Signal IPI available event */
if (!fifo_isempty(ictlp->async_fifo)) {
vmm_completion_complete(&ictlp->async_avail);
}
}
void vmm_smp_ipi_async_call(const struct vmm_cpumask *dest,
void (*func)(void *, void *, void *),
void *arg0, void *arg1, void *arg2)
{
u32 c, cpu = vmm_smp_processor_id();
struct smp_ipi_call ipic;
if (!dest || !func) {
return;
}
for_each_cpu(c, dest) {
if (c == cpu) {
func(arg0, arg1, arg2);
} else {
if (!vmm_cpu_online(c)) {
continue;
}
ipic.src_cpu = cpu;
ipic.dst_cpu = c;
ipic.func = func;
ipic.arg0 = arg0;
ipic.arg1 = arg1;
ipic.arg2 = arg2;
smp_ipi_async_submit(&per_cpu(ictl, c), &ipic);
}
}
}
int vmm_smp_ipi_sync_call(const struct vmm_cpumask *dest,
u32 timeout_msecs,
void (*func)(void *, void *, void *),
void *arg0, void *arg1, void *arg2)
{
int rc = VMM_OK;
u64 timeout_tstamp;
u32 c, trig_count, cpu = vmm_smp_processor_id();
struct vmm_cpumask trig_mask = VMM_CPU_MASK_NONE;
struct smp_ipi_call ipic;
struct smp_ipi_ctrl *ictlp;
if (!dest || !func) {
return VMM_EFAIL;
}
trig_count = 0;
for_each_cpu(c, dest) {
if (c == cpu) {
func(arg0, arg1, arg2);
} else {
if (!vmm_cpu_online(c)) {
continue;
}
ipic.src_cpu = cpu;
ipic.dst_cpu = c;
ipic.func = func;
ipic.arg0 = arg0;
ipic.arg1 = arg1;
ipic.arg2 = arg2;
smp_ipi_sync_submit(&per_cpu(ictl, c), &ipic);
vmm_cpumask_set_cpu(c, &trig_mask);
trig_count ;
}
}
if (trig_count && timeout_msecs) {
rc = VMM_ETIMEDOUT;
timeout_tstamp = vmm_timer_timestamp();
timeout_tstamp = (u64)timeout_msecs * 1000000ULL;
while (vmm_timer_timestamp() < timeout_tstamp) {
for_each_cpu(c, &trig_mask) {
ictlp = &per_cpu(ictl, c);
if (!fifo_avail(ictlp->sync_fifo)) {
vmm_cpumask_clear_cpu(c, &trig_mask);
trig_count--;
}
}
if (!trig_count) {
rc = VMM_OK;
break;
}
vmm_udelay(SMP_IPI_WAIT_UDELAY);
}
}
return rc;
}
static int smp_sync_ipi_startup(struct vmm_cpuhp_notify *cpuhp, u32 cpu)
{
int rc = VMM_EFAIL;
struct smp_ipi_ctrl *ictlp = &per_cpu(ictl, cpu);
/* Initialize Sync IPI FIFO */
ictlp->sync_fifo = fifo_alloc(sizeof(struct smp_ipi_call),
SMP_IPI_MAX_SYNC_PER_CPU);
if (!ictlp->sync_fifo) {
rc = VMM_ENOMEM;
goto fail;
}
/* Initialize Async IPI FIFO */
ictlp->async_fifo = fifo_alloc(sizeof(struct smp_ipi_call),
SMP_IPI_MAX_ASYNC_PER_CPU);
if (!ictlp->async_fifo) {
rc = VMM_ENOMEM;
goto fail_free_sync;
}
/* Initialize IPI available completion event */
INIT_COMPLETION(&ictlp->async_avail);
/* Clear async VCPU pointer */
ictlp->async_vcpu = NULL;
/* Arch specific IPI initialization */
if ((rc = arch_smp_ipi_init())) {
goto fail_free_async;
}
return VMM_OK;
fail_free_async:
fifo_free(ictlp->async_fifo);
fail_free_sync:
fifo_free(ictlp->sync_fifo);
fail:
return rc;
}
static struct vmm_cpuhp_notify smp_sync_ipi_cpuhp = {
.name = "SMP_SYNC_IPI",
.state = VMM_CPUHP_STATE_SMP_SYNC_IPI,
.startup = smp_sync_ipi_startup,
};
int __init vmm_smp_sync_ipi_init(void)
{
/* Setup hotplug notifier */
return vmm_cpuhp_register(&smp_sync_ipi_cpuhp, TRUE);
}
static int smp_async_ipi_startup(struct vmm_cpuhp_notify *cpuhp, u32 cpu)
{
int rc = VMM_EFAIL;
char vcpu_name[VMM_FIELD_NAME_SIZE];
struct smp_ipi_ctrl *ictlp = &per_cpu(ictl, cpu);
/* Create IPI bottom-half VCPU. (Per Host CPU) */
vmm_snprintf(vcpu_name, sizeof(vcpu_name), "ipi/%d", cpu);
ictlp->async_vcpu = vmm_manager_vcpu_orphan_create(vcpu_name,
(virtual_addr_t)&smp_ipi_main,
IPI_VCPU_STACK_SZ,
IPI_VCPU_PRIORITY,
IPI_VCPU_TIMESLICE,
IPI_VCPU_DEADLINE,
IPI_VCPU_PERIODICITY,
vmm_cpumask_of(cpu));
if (!ictlp->async_vcpu) {
rc = VMM_EFAIL;
goto fail;
}
/* Kick IPI orphan VCPU */
if ((rc = vmm_manager_vcpu_kick(ictlp->async_vcpu))) {
goto fail_free_vcpu;
}
return VMM_OK;
fail_free_vcpu:
vmm_manager_vcpu_orphan_destroy(ictlp->async_vcpu);
fail:
return rc;
}
static struct vmm_cpuhp_notify smp_async_ipi_cpuhp = {
.name = "SMP_ASYNC_IPI",
.state = VMM_CPUHP_STATE_SMP_ASYNC_IPI,
.startup = smp_async_ipi_startup,
};
int __init vmm_smp_async_ipi_init(void)
{
/* Setup hotplug notifier */
return vmm_cpuhp_register(&smp_async_ipi_cpuhp, TRUE);
}