Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright 2012 Michael Ellerman, IBM Corporation.
0004  * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
0005  */
0006 
0007 #ifndef _KVM_PPC_BOOK3S_XICS_H
0008 #define _KVM_PPC_BOOK3S_XICS_H
0009 
0010 #ifdef CONFIG_KVM_XICS
0011 /*
0012  * We use a two-level tree to store interrupt source information.
0013  * There are up to 1024 ICS nodes, each of which can represent
0014  * 1024 sources.
0015  */
0016 #define KVMPPC_XICS_MAX_ICS_ID  1023
0017 #define KVMPPC_XICS_ICS_SHIFT   10
0018 #define KVMPPC_XICS_IRQ_PER_ICS (1 << KVMPPC_XICS_ICS_SHIFT)
0019 #define KVMPPC_XICS_SRC_MASK    (KVMPPC_XICS_IRQ_PER_ICS - 1)
0020 
0021 /*
0022  * Interrupt source numbers below this are reserved, for example
0023  * 0 is "no interrupt", and 2 is used for IPIs.
0024  */
0025 #define KVMPPC_XICS_FIRST_IRQ   16
0026 #define KVMPPC_XICS_NR_IRQS ((KVMPPC_XICS_MAX_ICS_ID + 1) * \
0027                  KVMPPC_XICS_IRQ_PER_ICS)
0028 
0029 /* Priority value to use for disabling an interrupt */
0030 #define MASKED  0xff
0031 
0032 #define PQ_PRESENTED    1
0033 #define PQ_QUEUED   2
0034 
0035 /* State for one irq source */
0036 struct ics_irq_state {
0037     u32 number;
0038     u32 server;
0039     u32 pq_state;
0040     u8  priority;
0041     u8  saved_priority;
0042     u8  resend;
0043     u8  masked_pending;
0044     u8  lsi;        /* level-sensitive interrupt */
0045     u8  exists;
0046     int intr_cpu;
0047     u32 host_irq;
0048 };
0049 
0050 /* Atomic ICP state, updated with a single compare & swap */
0051 union kvmppc_icp_state {
0052     unsigned long raw;
0053     struct {
0054         u8 out_ee:1;
0055         u8 need_resend:1;
0056         u8 cppr;
0057         u8 mfrr;
0058         u8 pending_pri;
0059         u32 xisr;
0060     };
0061 };
0062 
0063 /* One bit per ICS */
0064 #define ICP_RESEND_MAP_SIZE (KVMPPC_XICS_MAX_ICS_ID / BITS_PER_LONG + 1)
0065 
0066 struct kvmppc_icp {
0067     struct kvm_vcpu *vcpu;
0068     unsigned long server_num;
0069     union kvmppc_icp_state state;
0070     unsigned long resend_map[ICP_RESEND_MAP_SIZE];
0071 
0072     /* Real mode might find something too hard, here's the action
0073      * it might request from virtual mode
0074      */
0075 #define XICS_RM_KICK_VCPU   0x1
0076 #define XICS_RM_CHECK_RESEND    0x2
0077 #define XICS_RM_NOTIFY_EOI  0x8
0078     u32 rm_action;
0079     struct kvm_vcpu *rm_kick_target;
0080     struct kvmppc_icp *rm_resend_icp;
0081     u32  rm_reject;
0082     u32  rm_eoied_irq;
0083 
0084     /* Counters for each reason we exited real mode */
0085     unsigned long n_rm_kick_vcpu;
0086     unsigned long n_rm_check_resend;
0087     unsigned long n_rm_notify_eoi;
0088     /* Counters for handling ICP processing in real mode */
0089     unsigned long n_check_resend;
0090     unsigned long n_reject;
0091 
0092     /* Debug stuff for real mode */
0093     union kvmppc_icp_state rm_dbgstate;
0094     struct kvm_vcpu *rm_dbgtgt;
0095 };
0096 
0097 struct kvmppc_ics {
0098     arch_spinlock_t lock;
0099     u16 icsid;
0100     struct ics_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
0101 };
0102 
0103 struct kvmppc_xics {
0104     struct kvm *kvm;
0105     struct kvm_device *dev;
0106     struct dentry *dentry;
0107     u32 max_icsid;
0108     bool real_mode;
0109     bool real_mode_dbg;
0110     u32 err_noics;
0111     u32 err_noicp;
0112     struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1];
0113 };
0114 
0115 static inline struct kvmppc_icp *kvmppc_xics_find_server(struct kvm *kvm,
0116                              u32 nr)
0117 {
0118     struct kvm_vcpu *vcpu = NULL;
0119     unsigned long i;
0120 
0121     kvm_for_each_vcpu(i, vcpu, kvm) {
0122         if (vcpu->arch.icp && nr == vcpu->arch.icp->server_num)
0123             return vcpu->arch.icp;
0124     }
0125     return NULL;
0126 }
0127 
0128 static inline struct kvmppc_ics *kvmppc_xics_find_ics(struct kvmppc_xics *xics,
0129                               u32 irq, u16 *source)
0130 {
0131     u32 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
0132     u16 src = irq & KVMPPC_XICS_SRC_MASK;
0133     struct kvmppc_ics *ics;
0134 
0135     if (source)
0136         *source = src;
0137     if (icsid > KVMPPC_XICS_MAX_ICS_ID)
0138         return NULL;
0139     ics = xics->ics[icsid];
0140     if (!ics)
0141         return NULL;
0142     return ics;
0143 }
0144 
0145 extern unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu);
0146 extern unsigned long xics_rm_h_xirr_x(struct kvm_vcpu *vcpu);
0147 extern int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
0148              unsigned long mfrr);
0149 extern int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
0150 extern int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
0151 
0152 #endif /* CONFIG_KVM_XICS */
0153 #endif /* _KVM_PPC_BOOK3S_XICS_H */