13b20eb23SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2edf88417SAvi Kivity /* 3edf88417SAvi Kivity * irq.h: in kernel interrupt controller related definitions 4edf88417SAvi Kivity * Copyright (c) 2007, Intel Corporation. 5edf88417SAvi Kivity * 6edf88417SAvi Kivity * Authors: 7edf88417SAvi Kivity * Yaozu (Eddie) Dong <Eddie.dong@intel.com> 8edf88417SAvi Kivity */ 9edf88417SAvi Kivity 10edf88417SAvi Kivity #ifndef __IRQ_H 11edf88417SAvi Kivity #define __IRQ_H 12edf88417SAvi Kivity 13edf88417SAvi Kivity #include <linux/mm_types.h> 14edf88417SAvi Kivity #include <linux/hrtimer.h> 15edf88417SAvi Kivity #include <linux/kvm_host.h> 163f353858SAvi Kivity #include <linux/spinlock.h> 1782470196SZhang Xiantao 18af669ac6SAndre Przywara #include <kvm/iodev.h> 1982470196SZhang Xiantao #include "lapic.h" 20edf88417SAvi Kivity 21c65bbfa1SBen-Ami Yassour #define PIC_NUM_PINS 16 2244882eedSMarcelo Tosatti #define SELECT_PIC(irq) \ 2344882eedSMarcelo Tosatti ((irq) < 8 ? KVM_IRQCHIP_PIC_MASTER : KVM_IRQCHIP_PIC_SLAVE) 24c65bbfa1SBen-Ami Yassour 25edf88417SAvi Kivity struct kvm; 26edf88417SAvi Kivity struct kvm_vcpu; 27edf88417SAvi Kivity 28edf88417SAvi Kivity struct kvm_kpic_state { 29edf88417SAvi Kivity u8 last_irr; /* edge detection */ 30edf88417SAvi Kivity u8 irr; /* interrupt request register */ 31edf88417SAvi Kivity u8 imr; /* interrupt mask register */ 32edf88417SAvi Kivity u8 isr; /* interrupt service register */ 33edf88417SAvi Kivity u8 priority_add; /* highest irq priority */ 34edf88417SAvi Kivity u8 irq_base; 35edf88417SAvi Kivity u8 read_reg_select; 36edf88417SAvi Kivity u8 poll; 37edf88417SAvi Kivity u8 special_mask; 38edf88417SAvi Kivity u8 init_state; 39edf88417SAvi Kivity u8 auto_eoi; 40edf88417SAvi Kivity u8 rotate_on_auto_eoi; 41edf88417SAvi Kivity u8 special_fully_nested_mode; 42edf88417SAvi Kivity u8 init4; /* true if 4 byte init */ 43edf88417SAvi Kivity u8 elcr; /* PIIX edge/trigger selection */ 44edf88417SAvi Kivity u8 elcr_mask; 45eebb5f31SGleb Natapov u8 isr_ack; /* interrupt ack detection */ 46edf88417SAvi Kivity struct kvm_pic *pics_state; 47edf88417SAvi Kivity }; 48edf88417SAvi Kivity 49edf88417SAvi Kivity struct kvm_pic { 50f4f51050SAvi Kivity spinlock_t lock; 5150a085bdSJan Kiszka bool wakeup_needed; 523f353858SAvi Kivity unsigned pending_acks; 533f353858SAvi Kivity struct kvm *kvm; 54edf88417SAvi Kivity struct kvm_kpic_state pics[2]; /* 0 is master pic, 1 is slave pic */ 55edf88417SAvi Kivity int output; /* intr from master PIC */ 56743eeb0bSSasha Levin struct kvm_io_device dev_master; 57743eeb0bSSasha Levin struct kvm_io_device dev_slave; 58743eeb0bSSasha Levin struct kvm_io_device dev_eclr; 59f5244726SMarcelo Tosatti void (*ack_notifier)(void *opaque, int irq); 6093b6547eSMichael S. Tsirkin unsigned long irq_states[PIC_NUM_PINS]; 61edf88417SAvi Kivity }; 62edf88417SAvi Kivity 6309941366SRadim Krčmář int kvm_pic_init(struct kvm *kvm); 6409941366SRadim Krčmář void kvm_pic_destroy(struct kvm *kvm); 65f5244726SMarcelo Tosatti int kvm_pic_read_irq(struct kvm *kvm); 66edf88417SAvi Kivity void kvm_pic_update_irq(struct kvm_pic *s); 67edf88417SAvi Kivity 6849df6397SSteve Rutherford static inline int irqchip_split(struct kvm *kvm) 6949df6397SSteve Rutherford { 70637e3f86SDavid Hildenbrand int mode = kvm->arch.irqchip_mode; 71637e3f86SDavid Hildenbrand 72637e3f86SDavid Hildenbrand /* Matches smp_wmb() when setting irqchip_mode */ 73637e3f86SDavid Hildenbrand smp_rmb(); 74637e3f86SDavid Hildenbrand return mode == KVM_IRQCHIP_SPLIT; 7549776fafSRadim Krčmář } 7649776fafSRadim Krčmář 7749776fafSRadim Krčmář static inline int irqchip_kernel(struct kvm *kvm) 7849776fafSRadim Krčmář { 79637e3f86SDavid Hildenbrand int mode = kvm->arch.irqchip_mode; 80637e3f86SDavid Hildenbrand 81637e3f86SDavid Hildenbrand /* Matches smp_wmb() when setting irqchip_mode */ 82637e3f86SDavid Hildenbrand smp_rmb(); 83637e3f86SDavid Hildenbrand return mode == KVM_IRQCHIP_KERNEL; 8449df6397SSteve Rutherford } 8549df6397SSteve Rutherford 86*88197e6aS彭浩(Richard) static inline int pic_in_kernel(struct kvm *kvm) 87*88197e6aS彭浩(Richard) { 88*88197e6aS彭浩(Richard) return irqchip_kernel(kvm); 89*88197e6aS彭浩(Richard) } 90*88197e6aS彭浩(Richard) 91edf88417SAvi Kivity static inline int irqchip_in_kernel(struct kvm *kvm) 92edf88417SAvi Kivity { 93637e3f86SDavid Hildenbrand int mode = kvm->arch.irqchip_mode; 9449df6397SSteve Rutherford 95637e3f86SDavid Hildenbrand /* Matches smp_wmb() when setting irqchip_mode */ 963ddea128SMarcelo Tosatti smp_rmb(); 975c0aea0eSDavid Hildenbrand return mode != KVM_IRQCHIP_NONE; 98edf88417SAvi Kivity } 99edf88417SAvi Kivity 100edf88417SAvi Kivity void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu); 101edf88417SAvi Kivity void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu); 1028fdb2351SJan Kiszka void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu); 1032f52d58cSAvi Kivity void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu); 1042f599714SMarcelo Tosatti void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu); 1052f599714SMarcelo Tosatti void __kvm_migrate_timers(struct kvm_vcpu *vcpu); 106edf88417SAvi Kivity 1073d80840dSMarcelo Tosatti int apic_has_pending_timer(struct kvm_vcpu *vcpu); 1083d80840dSMarcelo Tosatti 109d9565a73SEric Auger int kvm_setup_default_irq_routing(struct kvm *kvm); 110d9565a73SEric Auger int kvm_setup_empty_irq_routing(struct kvm *kvm); 11159508b30SPeter Xu int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, 11259508b30SPeter Xu struct kvm_lapic_irq *irq, 11359508b30SPeter Xu struct dest_map *dest_map); 114d9565a73SEric Auger 115edf88417SAvi Kivity #endif 116