Skip to content

Commit a8cd692

Browse files
lyan3lijinxia
authored andcommitted
hv: pirq: clean up irq handlers
There are several similar irq handlers with confusing function names and it's not friendly to call update_irq_handler() to update a proper handler after request_irq(). With this commit, a single generic irq handler is being used, in which, no lock need to be acquired because our design could guarantee there is no concurrent irq handling and irq handler request/free. A flags field is added to irq_desc struct to select the proper processing flow for an irq. Irqflags is defined as follows: IRQF_NONE (0U) IRQF_LEVEL (1U << 1U) /* 1: level trigger; 0: edge trigger */ IRQF_PT (1U << 2U) /* 1: for passthrough dev */ Because we have only one irq handler, update_irq_handler() should be replace by set_irq_trigger_mode(), whichs set trigger mode flag of a certian irq. Accordingly, the code where called update_irq_handler() need to be updated. Signed-off-by: Yan, Like <like.yan@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent 2c044e0 commit a8cd692

File tree

8 files changed

+53
-154
lines changed

8 files changed

+53
-154
lines changed

hypervisor/arch/x86/assign.c

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -139,51 +139,6 @@ lookup_entry_by_vintx(struct vm *vm, uint8_t vpin,
139139
return entry;
140140
}
141141

142-
static void
143-
ptdev_update_irq_handler(struct vm *vm, struct ptdev_remapping_info *entry)
144-
{
145-
uint32_t phys_irq = entry->allocated_pirq;
146-
struct ptdev_intx_info *intx = &entry->ptdev_intr_info.intx;
147-
148-
if (entry->type == PTDEV_INTR_MSI) {
149-
/* all other MSI and normal maskable */
150-
update_irq_handler(phys_irq, common_handler_edge);
151-
}
152-
/* update irq handler for IOAPIC */
153-
if ((entry->type == PTDEV_INTR_INTX)
154-
&& (intx->vpin_src
155-
== PTDEV_VPIN_IOAPIC)) {
156-
union ioapic_rte rte;
157-
bool trigger_lvl = false;
158-
159-
/* VPIN_IOAPIC src means we have vioapic enabled */
160-
vioapic_get_rte(vm, intx->virt_pin, &rte);
161-
if ((rte.full & IOAPIC_RTE_TRGRMOD) == IOAPIC_RTE_TRGRLVL) {
162-
trigger_lvl = true;
163-
}
164-
165-
if (trigger_lvl) {
166-
update_irq_handler(phys_irq, common_dev_handler_level);
167-
} else {
168-
update_irq_handler(phys_irq, common_handler_edge);
169-
}
170-
}
171-
/* update irq handler for PIC */
172-
if ((entry->type == PTDEV_INTR_INTX) && (phys_irq < NR_LEGACY_IRQ)
173-
&& (intx->vpin_src == PTDEV_VPIN_PIC)) {
174-
enum vpic_trigger trigger;
175-
176-
/* VPIN_PIC src means we have vpic enabled */
177-
vpic_get_irq_trigger(vm,
178-
intx->virt_pin, &trigger);
179-
if (trigger == LEVEL_TRIGGER) {
180-
update_irq_handler(phys_irq, common_dev_handler_level);
181-
} else {
182-
update_irq_handler(phys_irq, common_handler_edge);
183-
}
184-
}
185-
}
186-
187142
static bool ptdev_hv_owned_intx(struct vm *vm, struct ptdev_intx_info *info)
188143
{
189144
/* vm0 pin 4 (uart) is owned by hypervisor under debug version */
@@ -677,9 +632,6 @@ int ptdev_msix_remap(struct vm *vm, uint16_t virt_bdf,
677632
entry->ptdev_intr_info.msi.phys_vector =
678633
irq_to_vector(entry->allocated_pirq);
679634

680-
/* update irq handler according to info in guest */
681-
ptdev_update_irq_handler(vm, entry);
682-
683635
dev_dbg(ACRN_DBG_IRQ,
684636
"PCI %x:%x.%x MSI VR[%d] 0x%x->0x%x assigned to vm%d",
685637
(entry->virt_bdf >> 8) & 0xFFU,
@@ -711,6 +663,7 @@ static void activate_physical_ioapic(struct vm *vm,
711663
{
712664
union ioapic_rte rte;
713665
uint32_t phys_irq = entry->allocated_pirq;
666+
bool is_lvl_trigger = false;
714667

715668
/* disable interrupt */
716669
GSI_MASK_IRQ(phys_irq);
@@ -722,8 +675,11 @@ static void activate_physical_ioapic(struct vm *vm,
722675
rte.full |= IOAPIC_RTE_INTMSET;
723676
ioapic_set_rte(phys_irq, rte);
724677

725-
/* update irq handler according to info in guest */
726-
ptdev_update_irq_handler(vm, entry);
678+
/* update irq trigger mode according to info in guest */
679+
if ((rte.full & IOAPIC_RTE_TRGRMOD) == IOAPIC_RTE_TRGRLVL) {
680+
is_lvl_trigger = true;
681+
}
682+
set_irq_trigger_mode(phys_irq, is_lvl_trigger);
727683

728684
/* enable interrupt */
729685
GSI_UNMASK_IRQ(phys_irq);

hypervisor/arch/x86/ioapic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ static void ioapic_set_routing(uint32_t gsi, uint32_t vr)
219219
ioapic_set_rte_entry(addr, gsi_table[gsi].pin, rte);
220220

221221
if ((rte.full & IOAPIC_RTE_TRGRMOD) != 0UL) {
222-
update_irq_handler(gsi, handle_level_interrupt_common);
222+
set_irq_trigger_mode(gsi, true);
223223
} else {
224-
update_irq_handler(gsi, common_handler_edge);
224+
set_irq_trigger_mode(gsi, false);
225225
}
226226

227227
dev_dbg(ACRN_DBG_IRQ, "GSI: irq:%d pin:%hhu rte:%lx",

hypervisor/arch/x86/irq.c

Lines changed: 28 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ static uint32_t vector_to_irq[NR_MAX_VECTOR + 1];
1515

1616
spurious_handler_t spurious_handler;
1717

18+
static inline void handle_irq(struct irq_desc *desc);
19+
1820
#define NR_STATIC_MAPPINGS (2U)
1921
static uint32_t irq_static_mappings[NR_STATIC_MAPPINGS][2] = {
2022
{TIMER_IRQ, VECTOR_TIMER},
@@ -212,7 +214,8 @@ static void disable_pic_irq(void)
212214
*/
213215
int32_t request_irq(uint32_t req_irq,
214216
irq_action_t action_fn,
215-
void *priv_data)
217+
void *priv_data,
218+
uint32_t flags)
216219
{
217220
struct irq_desc *desc;
218221
uint32_t irq, vector;
@@ -234,11 +237,8 @@ int32_t request_irq(uint32_t req_irq,
234237

235238
desc = &irq_desc_array[irq];
236239
spinlock_irqsave_obtain(&desc->lock, &rflags);
237-
if (desc->irq_handler == NULL) {
238-
desc->irq_handler = common_handler_edge;
239-
}
240-
241240
if (desc->action == NULL) {
241+
desc->flags = flags;
242242
desc->priv_data = priv_data;
243243
desc->action = action_fn;
244244
spinlock_irqrestore_release(&desc->lock, rflags);
@@ -327,12 +327,12 @@ void dispatch_interrupt(struct intr_excp_ctx *ctx)
327327
goto ERR;
328328
}
329329

330-
if ((desc->used == IRQ_NOT_ASSIGNED) || (desc->irq_handler == NULL)) {
330+
if (desc->used == IRQ_NOT_ASSIGNED) {
331331
/* mask irq if possible */
332332
goto ERR;
333333
}
334334

335-
desc->irq_handler(desc, NULL);
335+
handle_irq(desc);
336336
return;
337337
ERR:
338338
handle_spurious_interrupt(vr);
@@ -361,63 +361,26 @@ void partition_mode_dispatch_interrupt(struct intr_excp_ctx *ctx)
361361
}
362362
#endif
363363

364-
int handle_level_interrupt_common(struct irq_desc *desc,
365-
__unused void *handler_data)
364+
static inline bool irq_need_mask(struct irq_desc *desc)
366365
{
367-
uint64_t rflags;
368-
irq_action_t action = desc->action;
369-
370-
spinlock_irqsave_obtain(&desc->lock, &rflags);
371-
372-
/* mask iopaic pin */
373-
if (irq_is_gsi(desc->irq)) {
374-
GSI_MASK_IRQ(desc->irq);
375-
}
376-
377-
/* Send EOI to LAPIC/IOAPIC IRR */
378-
send_lapic_eoi();
379-
380-
if (action != NULL) {
381-
action(desc->irq, desc->priv_data);
382-
}
383-
384-
if (irq_is_gsi(desc->irq)) {
385-
GSI_UNMASK_IRQ(desc->irq);
386-
}
387-
388-
spinlock_irqrestore_release(&desc->lock, rflags);
389-
390-
return 0;
366+
/* level triggered gsi should be masked */
367+
return (((desc->flags & IRQF_LEVEL) != 0U)
368+
&& irq_is_gsi(desc->irq));
391369
}
392370

393-
int common_handler_edge(struct irq_desc *desc, __unused void *handler_data)
371+
static inline bool irq_need_unmask(struct irq_desc *desc)
394372
{
395-
uint64_t rflags;
396-
irq_action_t action = desc->action;
397-
398-
spinlock_irqsave_obtain(&desc->lock, &rflags);
399-
400-
/* Send EOI to LAPIC/IOAPIC IRR */
401-
send_lapic_eoi();
402-
403-
if (action != NULL) {
404-
action(desc->irq, desc->priv_data);
405-
}
406-
407-
spinlock_irqrestore_release(&desc->lock, rflags);
408-
409-
return 0;
373+
/* level triggered gsi for non-ptdev should be unmasked */
374+
return (((desc->flags & IRQF_LEVEL) != 0U)
375+
&& ((desc->flags & IRQF_PT) == 0U)
376+
&& irq_is_gsi(desc->irq));
410377
}
411378

412-
int common_dev_handler_level(struct irq_desc *desc, __unused void *handler_data)
379+
static inline void handle_irq(struct irq_desc *desc)
413380
{
414-
uint64_t rflags;
415381
irq_action_t action = desc->action;
416382

417-
spinlock_irqsave_obtain(&desc->lock, &rflags);
418-
419-
/* mask iopaic pin */
420-
if (irq_is_gsi(desc->irq)) {
383+
if (irq_need_mask(desc)) {
421384
GSI_MASK_IRQ(desc->irq);
422385
}
423386

@@ -428,28 +391,12 @@ int common_dev_handler_level(struct irq_desc *desc, __unused void *handler_data)
428391
action(desc->irq, desc->priv_data);
429392
}
430393

431-
spinlock_irqrestore_release(&desc->lock, rflags);
432-
433-
/* we did not unmask irq until guest EOI the vector */
434-
return 0;
435-
}
436-
437-
/* no desc->lock for quick handling local interrupt like lapic timer */
438-
int quick_handler_nolock(struct irq_desc *desc, __unused void *handler_data)
439-
{
440-
irq_action_t action = desc->action;
441-
442-
/* Send EOI to LAPIC/IOAPIC IRR */
443-
send_lapic_eoi();
444-
445-
if (action != NULL) {
446-
action(desc->irq, desc->priv_data);
447-
}
448-
449-
return 0;
394+
if (irq_need_unmask(desc)) {
395+
GSI_UNMASK_IRQ(desc->irq);
396+
}
450397
}
451398

452-
void update_irq_handler(uint32_t irq, irq_handler_t func)
399+
void set_irq_trigger_mode(uint32_t irq, bool is_level_trigger)
453400
{
454401
uint64_t rflags;
455402
struct irq_desc *desc;
@@ -460,7 +407,11 @@ void update_irq_handler(uint32_t irq, irq_handler_t func)
460407

461408
desc = &irq_desc_array[irq];
462409
spinlock_irqsave_obtain(&desc->lock, &rflags);
463-
desc->irq_handler = func;
410+
if (is_level_trigger == true) {
411+
desc->flags |= IRQF_LEVEL;
412+
} else {
413+
desc->flags &= ~IRQF_LEVEL;
414+
}
464415
spinlock_irqrestore_release(&desc->lock, rflags);
465416
}
466417

@@ -483,6 +434,7 @@ void free_irq(uint32_t irq)
483434
spinlock_irqsave_obtain(&desc->lock, &rflags);
484435
desc->action = NULL;
485436
desc->priv_data = NULL;
437+
desc->flags = IRQF_NONE;
486438
spinlock_irqrestore_release(&desc->lock, rflags);
487439
}
488440

hypervisor/arch/x86/notify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int request_notification_irq(irq_action_t func, void *data)
6666
}
6767

6868
/* all cpu register the same notification vector */
69-
retval = request_irq(NOTIFY_IRQ, func, data);
69+
retval = request_irq(NOTIFY_IRQ, func, data, IRQF_NONE);
7070
if (retval < 0) {
7171
pr_err("Failed to add notify isr");
7272
return -ENODEV;

hypervisor/arch/x86/timer.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static void run_timer(struct hv_timer *timer)
2424
}
2525

2626
/* run in interrupt context */
27-
static int tsc_deadline_handler(__unused int irq, __unused void *data)
27+
static int tsc_deadline_handler(__unused uint32_t irq, __unused void *data)
2828
{
2929
fire_softirq(SOFTIRQ_TIMER);
3030
return 0;
@@ -107,21 +107,6 @@ void del_timer(struct hv_timer *timer)
107107
}
108108
}
109109

110-
static int request_timer_irq(irq_action_t func)
111-
{
112-
int32_t retval;
113-
114-
retval = request_irq(TIMER_IRQ, func, NULL);
115-
if (retval >= 0) {
116-
update_irq_handler(TIMER_IRQ, quick_handler_nolock);
117-
} else {
118-
pr_err("Failed to add timer isr");
119-
return -ENODEV;
120-
}
121-
122-
return 0;
123-
}
124-
125110
static void init_percpu_timer(uint16_t pcpu_id)
126111
{
127112
struct per_cpu_timers *cpu_timer;
@@ -186,14 +171,16 @@ static void timer_softirq(uint16_t pcpu_id)
186171
void timer_init(void)
187172
{
188173
uint16_t pcpu_id = get_cpu_id();
174+
int32_t retval;
189175

190176
init_percpu_timer(pcpu_id);
191177

192178
if (pcpu_id == BOOT_CPU_ID) {
193179
register_softirq(SOFTIRQ_TIMER, timer_softirq);
194180

195-
if (request_timer_irq((irq_action_t)tsc_deadline_handler)
196-
< 0) {
181+
retval = request_irq(TIMER_IRQ, (irq_action_t)tsc_deadline_handler,
182+
NULL, IRQF_NONE);
183+
if (retval < 0) {
197184
pr_err("Timer setup failed");
198185
return;
199186
}

hypervisor/arch/x86/vtd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,8 @@ static int dmar_setup_interrupt(struct dmar_drhd_rt *dmar_uint)
828828

829829
retval = request_irq(IRQ_INVALID,
830830
dmar_fault_handler,
831-
dmar_uint);
831+
dmar_uint,
832+
IRQF_NONE);
832833

833834
if (retval < 0 ) {
834835
pr_err("%s: fail to setup interrupt", __func__);

hypervisor/common/ptdev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ ptdev_activate_entry(struct ptdev_remapping_info *entry, uint32_t phys_irq)
136136

137137
/* register and allocate host vector/irq */
138138
retval = request_irq(phys_irq, ptdev_interrupt_handler,
139-
(void *)entry);
139+
(void *)entry, IRQF_PT);
140140

141141
ASSERT(retval >= 0, "dev register failed");
142142
entry->allocated_pirq = (uint32_t)retval;

hypervisor/include/common/irq.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#ifndef COMMON_IRQ_H
88
#define COMMON_IRQ_H
99

10+
#define IRQF_NONE (0U)
11+
#define IRQF_LEVEL (1U << 1U) /* 1: level trigger; 0: edge trigger */
12+
#define IRQF_PT (1U << 2U) /* 1: for passthrough dev */
13+
1014
enum irq_mode {
1115
IRQ_PULSE,
1216
IRQ_ASSERT,
@@ -26,20 +30,19 @@ struct irq_desc {
2630
enum irq_use_state used; /* this irq have assigned to device */
2731
uint32_t vector; /* assigned vector */
2832

29-
int (*irq_handler)(struct irq_desc *irq_desc, void *handler_data);
30-
/* callback for irq flow handling */
3133
irq_action_t action; /* callback registered from component */
3234
void *priv_data; /* irq_action private data */
35+
uint32_t flags; /* flags for trigger mode/ptdev */
3336

3437
spinlock_t lock;
3538
};
3639

3740
int32_t request_irq(uint32_t irq,
3841
irq_action_t action_fn,
39-
void *priv_data);
42+
void *priv_data,
43+
uint32_t flags);
4044

4145
void free_irq(uint32_t irq);
4246

43-
typedef int (*irq_handler_t)(struct irq_desc *desc, void *handler_data);
44-
void update_irq_handler(uint32_t irq, irq_handler_t func);
47+
void set_irq_trigger_mode(uint32_t irq, bool is_level_trigger);
4548
#endif /* COMMON_IRQ_H */

0 commit comments

Comments
 (0)