Skip to content

Commit ac253f8

Browse files
lifeixjren1
authored andcommitted
hv: timer: add periodic timer setup support
and add MIN_TIMER_PERIOD_US for limit periodic timer frequency. Now it's set to 500 us. Signed-off-by: Li, Fei1 <fei1.li@intel.com>
1 parent 9bfa574 commit ac253f8

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

hypervisor/arch/x86/guest/vlapic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ vlapic_reset(struct vlapic *vlapic)
15761576

15771577
vlapic->svr_last = lapic->svr;
15781578

1579-
initialize_timer(&vlapic->timer, NULL, NULL, 0);
1579+
initialize_timer(&vlapic->timer, NULL, NULL, 0, 0, 0);
15801580
}
15811581

15821582
void
@@ -1974,7 +1974,7 @@ vlapic_wrmsr(struct vcpu *vcpu, uint32_t msr, uint64_t val)
19741974

19751975
initialize_timer(&vlapic->timer,
19761976
tsc_periodic_time, (void *)vcpu,
1977-
val);
1977+
val, TICK_MODE_ONESHOT, 0);
19781978

19791979
if (add_timer(&vlapic->timer) != 0) {
19801980
pr_err("failed to add timer on VM %d",

hypervisor/arch/x86/timer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define MAX_TIMER_ACTIONS 32
3838
#define TIMER_IRQ (NR_MAX_IRQS - 1)
3939
#define CAL_MS 10
40+
#define MIN_TIMER_PERIOD_US 500
4041

4142
uint64_t tsc_hz = 1000000000;
4243

@@ -228,6 +229,12 @@ int timer_softirq(int pcpu_id)
228229

229230
run_timer(timer);
230231

232+
if (timer->mode == TICK_MODE_PERIODIC) {
233+
timer->fire_tsc += max(timer->period_in_cycle,
234+
US_TO_TICKS(MIN_TIMER_PERIOD_US));
235+
add_timer(timer);
236+
}
237+
231238
/* search next one */
232239
timer = find_expired_timer(cpu_timer, rdtsc());
233240
}

hypervisor/debug/console.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,21 @@ static int console_timer_callback(__unused void *data)
221221
/* Kick HV-Shell and Uart-Console tasks */
222222
console_handler();
223223

224-
/* Restart the timer */
225-
console_setup_timer();
226-
227224
return 0;
228225
}
229226

230227
void console_setup_timer(void)
231228
{
232229
static struct timer console_timer;
233-
uint64_t fire_tsc;
230+
uint64_t period_in_cycle, fire_tsc;
234231

235-
fire_tsc = rdtsc() + CYCLES_PER_MS * CONSOLE_KICK_TIMER_TIMEOUT;
232+
period_in_cycle = CYCLES_PER_MS * CONSOLE_KICK_TIMER_TIMEOUT;
233+
fire_tsc = rdtsc() + period_in_cycle;
236234
initialize_timer(&console_timer,
237235
console_timer_callback, NULL,
238-
fire_tsc);
236+
fire_tsc, TICK_MODE_PERIODIC, period_in_cycle);
239237

240-
/* Start an one-shot timer */
238+
/* Start an periodic timer */
241239
if (add_timer(&console_timer) != 0)
242240
pr_err("Failed to add console kick timer");
243241
}

hypervisor/include/arch/x86/timer.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,34 @@
3333

3434
typedef int (*timer_handle_t)(void *);
3535

36+
enum tick_mode {
37+
TICK_MODE_ONESHOT = 0,
38+
TICK_MODE_PERIODIC,
39+
};
40+
41+
3642
struct timer {
3743
struct list_head node; /* link all timers */
44+
int mode; /* timer mode: one-shot or periodic */
3845
uint64_t fire_tsc; /* tsc deadline to interrupt */
46+
uint64_t period_in_cycle; /* period of the periodic timer in unit of TSC cycles */
3947
timer_handle_t func; /* callback if time reached */
4048
void *priv_data; /* func private data */
4149
};
4250

4351
static inline void initialize_timer(struct timer *timer,
4452
timer_handle_t func,
4553
void *priv_data,
46-
uint64_t fire_tsc)
54+
uint64_t fire_tsc,
55+
int mode,
56+
uint64_t period_in_cycle)
4757
{
4858
if (timer) {
4959
timer->func = func;
5060
timer->priv_data = priv_data;
5161
timer->fire_tsc = fire_tsc;
62+
timer->mode = mode;
63+
timer->period_in_cycle = period_in_cycle;
5264
}
5365
}
5466

0 commit comments

Comments
 (0)