Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * definition for kernel virtual machines on s390
0004  *
0005  * Copyright IBM Corp. 2008, 2018
0006  *
0007  *    Author(s): Carsten Otte <cotte@de.ibm.com>
0008  */
0009 
0010 
0011 #ifndef ASM_KVM_HOST_H
0012 #define ASM_KVM_HOST_H
0013 
0014 #include <linux/types.h>
0015 #include <linux/hrtimer.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/kvm_types.h>
0018 #include <linux/kvm_host.h>
0019 #include <linux/kvm.h>
0020 #include <linux/seqlock.h>
0021 #include <linux/module.h>
0022 #include <linux/pci.h>
0023 #include <linux/mmu_notifier.h>
0024 #include <asm/debug.h>
0025 #include <asm/cpu.h>
0026 #include <asm/fpu/api.h>
0027 #include <asm/isc.h>
0028 #include <asm/guarded_storage.h>
0029 
0030 #define KVM_S390_BSCA_CPU_SLOTS 64
0031 #define KVM_S390_ESCA_CPU_SLOTS 248
0032 #define KVM_MAX_VCPUS 255
0033 
0034 /*
0035  * These seem to be used for allocating ->chip in the routing table, which we
0036  * don't use. 1 is as small as we can get to reduce the needed memory. If we
0037  * need to look at ->chip later on, we'll need to revisit this.
0038  */
0039 #define KVM_NR_IRQCHIPS 1
0040 #define KVM_IRQCHIP_NUM_PINS 1
0041 #define KVM_HALT_POLL_NS_DEFAULT 50000
0042 
0043 /* s390-specific vcpu->requests bit members */
0044 #define KVM_REQ_ENABLE_IBS  KVM_ARCH_REQ(0)
0045 #define KVM_REQ_DISABLE_IBS KVM_ARCH_REQ(1)
0046 #define KVM_REQ_ICPT_OPEREXC    KVM_ARCH_REQ(2)
0047 #define KVM_REQ_START_MIGRATION KVM_ARCH_REQ(3)
0048 #define KVM_REQ_STOP_MIGRATION  KVM_ARCH_REQ(4)
0049 #define KVM_REQ_VSIE_RESTART    KVM_ARCH_REQ(5)
0050 #define KVM_REQ_REFRESH_GUEST_PREFIX    \
0051     KVM_ARCH_REQ_FLAGS(6, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
0052 
0053 #define SIGP_CTRL_C     0x80
0054 #define SIGP_CTRL_SCN_MASK  0x3f
0055 
0056 union bsca_sigp_ctrl {
0057     __u8 value;
0058     struct {
0059         __u8 c : 1;
0060         __u8 r : 1;
0061         __u8 scn : 6;
0062     };
0063 };
0064 
0065 union esca_sigp_ctrl {
0066     __u16 value;
0067     struct {
0068         __u8 c : 1;
0069         __u8 reserved: 7;
0070         __u8 scn;
0071     };
0072 };
0073 
0074 struct esca_entry {
0075     union esca_sigp_ctrl sigp_ctrl;
0076     __u16   reserved1[3];
0077     __u64   sda;
0078     __u64   reserved2[6];
0079 };
0080 
0081 struct bsca_entry {
0082     __u8    reserved0;
0083     union bsca_sigp_ctrl    sigp_ctrl;
0084     __u16   reserved[3];
0085     __u64   sda;
0086     __u64   reserved2[2];
0087 };
0088 
0089 union ipte_control {
0090     unsigned long val;
0091     struct {
0092         unsigned long k  : 1;
0093         unsigned long kh : 31;
0094         unsigned long kg : 32;
0095     };
0096 };
0097 
0098 union sca_utility {
0099     __u16 val;
0100     struct {
0101         __u16 mtcr : 1;
0102         __u16 reserved : 15;
0103     };
0104 };
0105 
0106 struct bsca_block {
0107     union ipte_control ipte_control;
0108     __u64   reserved[5];
0109     __u64   mcn;
0110     union sca_utility utility;
0111     __u8    reserved2[6];
0112     struct bsca_entry cpu[KVM_S390_BSCA_CPU_SLOTS];
0113 };
0114 
0115 struct esca_block {
0116     union ipte_control ipte_control;
0117     __u64   reserved1[6];
0118     union sca_utility utility;
0119     __u8    reserved2[6];
0120     __u64   mcn[4];
0121     __u64   reserved3[20];
0122     struct esca_entry cpu[KVM_S390_ESCA_CPU_SLOTS];
0123 };
0124 
0125 /*
0126  * This struct is used to store some machine check info from lowcore
0127  * for machine checks that happen while the guest is running.
0128  * This info in host's lowcore might be overwritten by a second machine
0129  * check from host when host is in the machine check's high-level handling.
0130  * The size is 24 bytes.
0131  */
0132 struct mcck_volatile_info {
0133     __u64 mcic;
0134     __u64 failing_storage_address;
0135     __u32 ext_damage_code;
0136     __u32 reserved;
0137 };
0138 
0139 #define CR0_INITIAL_MASK (CR0_UNUSED_56 | CR0_INTERRUPT_KEY_SUBMASK | \
0140               CR0_MEASUREMENT_ALERT_SUBMASK)
0141 #define CR14_INITIAL_MASK (CR14_UNUSED_32 | CR14_UNUSED_33 | \
0142                CR14_EXTERNAL_DAMAGE_SUBMASK)
0143 
0144 #define SIDAD_SIZE_MASK     0xff
0145 #define sida_origin(sie_block) \
0146     ((sie_block)->sidad & PAGE_MASK)
0147 #define sida_size(sie_block) \
0148     ((((sie_block)->sidad & SIDAD_SIZE_MASK) + 1) * PAGE_SIZE)
0149 
0150 #define CPUSTAT_STOPPED    0x80000000
0151 #define CPUSTAT_WAIT       0x10000000
0152 #define CPUSTAT_ECALL_PEND 0x08000000
0153 #define CPUSTAT_STOP_INT   0x04000000
0154 #define CPUSTAT_IO_INT     0x02000000
0155 #define CPUSTAT_EXT_INT    0x01000000
0156 #define CPUSTAT_RUNNING    0x00800000
0157 #define CPUSTAT_RETAINED   0x00400000
0158 #define CPUSTAT_TIMING_SUB 0x00020000
0159 #define CPUSTAT_SIE_SUB    0x00010000
0160 #define CPUSTAT_RRF        0x00008000
0161 #define CPUSTAT_SLSV       0x00004000
0162 #define CPUSTAT_SLSR       0x00002000
0163 #define CPUSTAT_ZARCH      0x00000800
0164 #define CPUSTAT_MCDS       0x00000100
0165 #define CPUSTAT_KSS        0x00000200
0166 #define CPUSTAT_SM         0x00000080
0167 #define CPUSTAT_IBS        0x00000040
0168 #define CPUSTAT_GED2       0x00000010
0169 #define CPUSTAT_G          0x00000008
0170 #define CPUSTAT_GED        0x00000004
0171 #define CPUSTAT_J          0x00000002
0172 #define CPUSTAT_P          0x00000001
0173 
0174 struct kvm_s390_sie_block {
0175     atomic_t cpuflags;      /* 0x0000 */
0176     __u32 : 1;          /* 0x0004 */
0177     __u32 prefix : 18;
0178     __u32 : 1;
0179     __u32 ibc : 12;
0180     __u8    reserved08[4];      /* 0x0008 */
0181 #define PROG_IN_SIE (1<<0)
0182     __u32   prog0c;         /* 0x000c */
0183     union {
0184         __u8    reserved10[16];     /* 0x0010 */
0185         struct {
0186             __u64   pv_handle_cpu;
0187             __u64   pv_handle_config;
0188         };
0189     };
0190 #define PROG_BLOCK_SIE  (1<<0)
0191 #define PROG_REQUEST    (1<<1)
0192     atomic_t prog20;        /* 0x0020 */
0193     __u8    reserved24[4];      /* 0x0024 */
0194     __u64   cputm;          /* 0x0028 */
0195     __u64   ckc;            /* 0x0030 */
0196     __u64   epoch;          /* 0x0038 */
0197     __u32   svcc;           /* 0x0040 */
0198 #define LCTL_CR0    0x8000
0199 #define LCTL_CR6    0x0200
0200 #define LCTL_CR9    0x0040
0201 #define LCTL_CR10   0x0020
0202 #define LCTL_CR11   0x0010
0203 #define LCTL_CR14   0x0002
0204     __u16   lctl;           /* 0x0044 */
0205     __s16   icpua;          /* 0x0046 */
0206 #define ICTL_OPEREXC    0x80000000
0207 #define ICTL_PINT   0x20000000
0208 #define ICTL_LPSW   0x00400000
0209 #define ICTL_STCTL  0x00040000
0210 #define ICTL_ISKE   0x00004000
0211 #define ICTL_SSKE   0x00002000
0212 #define ICTL_RRBE   0x00001000
0213 #define ICTL_TPROT  0x00000200
0214     __u32   ictl;           /* 0x0048 */
0215 #define ECA_CEI     0x80000000
0216 #define ECA_IB      0x40000000
0217 #define ECA_SIGPI   0x10000000
0218 #define ECA_MVPGI   0x01000000
0219 #define ECA_AIV     0x00200000
0220 #define ECA_VX      0x00020000
0221 #define ECA_PROTEXCI    0x00002000
0222 #define ECA_APIE    0x00000008
0223 #define ECA_SII     0x00000001
0224     __u32   eca;            /* 0x004c */
0225 #define ICPT_INST   0x04
0226 #define ICPT_PROGI  0x08
0227 #define ICPT_INSTPROGI  0x0C
0228 #define ICPT_EXTREQ 0x10
0229 #define ICPT_EXTINT 0x14
0230 #define ICPT_IOREQ  0x18
0231 #define ICPT_WAIT   0x1c
0232 #define ICPT_VALIDITY   0x20
0233 #define ICPT_STOP   0x28
0234 #define ICPT_OPEREXC    0x2C
0235 #define ICPT_PARTEXEC   0x38
0236 #define ICPT_IOINST 0x40
0237 #define ICPT_KSS    0x5c
0238 #define ICPT_MCHKREQ    0x60
0239 #define ICPT_INT_ENABLE 0x64
0240 #define ICPT_PV_INSTR   0x68
0241 #define ICPT_PV_NOTIFY  0x6c
0242 #define ICPT_PV_PREF    0x70
0243     __u8    icptcode;       /* 0x0050 */
0244     __u8    icptstatus;     /* 0x0051 */
0245     __u16   ihcpu;          /* 0x0052 */
0246     __u8    reserved54;     /* 0x0054 */
0247 #define IICTL_CODE_NONE      0x00
0248 #define IICTL_CODE_MCHK      0x01
0249 #define IICTL_CODE_EXT       0x02
0250 #define IICTL_CODE_IO        0x03
0251 #define IICTL_CODE_RESTART   0x04
0252 #define IICTL_CODE_SPECIFICATION 0x10
0253 #define IICTL_CODE_OPERAND   0x11
0254     __u8    iictl;          /* 0x0055 */
0255     __u16   ipa;            /* 0x0056 */
0256     __u32   ipb;            /* 0x0058 */
0257     __u32   scaoh;          /* 0x005c */
0258 #define FPF_BPBC    0x20
0259     __u8    fpf;            /* 0x0060 */
0260 #define ECB_GS      0x40
0261 #define ECB_TE      0x10
0262 #define ECB_SPECI   0x08
0263 #define ECB_SRSI    0x04
0264 #define ECB_HOSTPROTINT 0x02
0265 #define ECB_PTF     0x01
0266     __u8    ecb;            /* 0x0061 */
0267 #define ECB2_CMMA   0x80
0268 #define ECB2_IEP    0x20
0269 #define ECB2_PFMFI  0x08
0270 #define ECB2_ESCA   0x04
0271 #define ECB2_ZPCI_LSI   0x02
0272     __u8    ecb2;                   /* 0x0062 */
0273 #define ECB3_AISI   0x20
0274 #define ECB3_AISII  0x10
0275 #define ECB3_DEA 0x08
0276 #define ECB3_AES 0x04
0277 #define ECB3_RI  0x01
0278     __u8    ecb3;           /* 0x0063 */
0279     __u32   scaol;          /* 0x0064 */
0280     __u8    sdf;            /* 0x0068 */
0281     __u8    epdx;           /* 0x0069 */
0282     __u8    cpnc;           /* 0x006a */
0283     __u8    reserved6b;     /* 0x006b */
0284     __u32   todpr;          /* 0x006c */
0285 #define GISA_FORMAT1 0x00000001
0286     __u32   gd;         /* 0x0070 */
0287     __u8    reserved74[12];     /* 0x0074 */
0288     __u64   mso;            /* 0x0080 */
0289     __u64   msl;            /* 0x0088 */
0290     psw_t   gpsw;           /* 0x0090 */
0291     __u64   gg14;           /* 0x00a0 */
0292     __u64   gg15;           /* 0x00a8 */
0293     __u8    reservedb0[8];      /* 0x00b0 */
0294 #define HPID_KVM    0x4
0295 #define HPID_VSIE   0x5
0296     __u8    hpid;           /* 0x00b8 */
0297     __u8    reservedb9[7];      /* 0x00b9 */
0298     union {
0299         struct {
0300             __u32   eiparams;   /* 0x00c0 */
0301             __u16   extcpuaddr; /* 0x00c4 */
0302             __u16   eic;        /* 0x00c6 */
0303         };
0304         __u64   mcic;           /* 0x00c0 */
0305     } __packed;
0306     __u32   reservedc8;     /* 0x00c8 */
0307     union {
0308         struct {
0309             __u16   pgmilc;     /* 0x00cc */
0310             __u16   iprcc;      /* 0x00ce */
0311         };
0312         __u32   edc;            /* 0x00cc */
0313     } __packed;
0314     union {
0315         struct {
0316             __u32   dxc;        /* 0x00d0 */
0317             __u16   mcn;        /* 0x00d4 */
0318             __u8    perc;       /* 0x00d6 */
0319             __u8    peratmid;   /* 0x00d7 */
0320         };
0321         __u64   faddr;          /* 0x00d0 */
0322     } __packed;
0323     __u64   peraddr;        /* 0x00d8 */
0324     __u8    eai;            /* 0x00e0 */
0325     __u8    peraid;         /* 0x00e1 */
0326     __u8    oai;            /* 0x00e2 */
0327     __u8    armid;          /* 0x00e3 */
0328     __u8    reservede4[4];      /* 0x00e4 */
0329     union {
0330         __u64   tecmc;      /* 0x00e8 */
0331         struct {
0332             __u16   subchannel_id;  /* 0x00e8 */
0333             __u16   subchannel_nr;  /* 0x00ea */
0334             __u32   io_int_parm;    /* 0x00ec */
0335             __u32   io_int_word;    /* 0x00f0 */
0336         };
0337     } __packed;
0338     __u8    reservedf4[8];      /* 0x00f4 */
0339 #define CRYCB_FORMAT_MASK 0x00000003
0340 #define CRYCB_FORMAT0 0x00000000
0341 #define CRYCB_FORMAT1 0x00000001
0342 #define CRYCB_FORMAT2 0x00000003
0343     __u32   crycbd;         /* 0x00fc */
0344     __u64   gcr[16];        /* 0x0100 */
0345     union {
0346         __u64   gbea;       /* 0x0180 */
0347         __u64   sidad;
0348     };
0349     __u8    reserved188[8];     /* 0x0188 */
0350     __u64   sdnxo;          /* 0x0190 */
0351     __u8    reserved198[8];     /* 0x0198 */
0352     __u32   fac;            /* 0x01a0 */
0353     __u8    reserved1a4[20];    /* 0x01a4 */
0354     __u64   cbrlo;          /* 0x01b8 */
0355     __u8    reserved1c0[8];     /* 0x01c0 */
0356 #define ECD_HOSTREGMGMT 0x20000000
0357 #define ECD_MEF     0x08000000
0358 #define ECD_ETOKENF 0x02000000
0359 #define ECD_ECC     0x00200000
0360     __u32   ecd;            /* 0x01c8 */
0361     __u8    reserved1cc[18];    /* 0x01cc */
0362     __u64   pp;         /* 0x01de */
0363     __u8    reserved1e6[2];     /* 0x01e6 */
0364     __u64   itdba;          /* 0x01e8 */
0365     __u64   riccbd;         /* 0x01f0 */
0366     __u64   gvrd;           /* 0x01f8 */
0367 } __packed __aligned(512);
0368 
0369 struct kvm_s390_itdb {
0370     __u8    data[256];
0371 };
0372 
0373 struct sie_page {
0374     struct kvm_s390_sie_block sie_block;
0375     struct mcck_volatile_info mcck_info;    /* 0x0200 */
0376     __u8 reserved218[360];      /* 0x0218 */
0377     __u64 pv_grregs[16];        /* 0x0380 */
0378     __u8 reserved400[512];      /* 0x0400 */
0379     struct kvm_s390_itdb itdb;  /* 0x0600 */
0380     __u8 reserved700[2304];     /* 0x0700 */
0381 };
0382 
0383 struct kvm_vcpu_stat {
0384     struct kvm_vcpu_stat_generic generic;
0385     u64 exit_userspace;
0386     u64 exit_null;
0387     u64 exit_external_request;
0388     u64 exit_io_request;
0389     u64 exit_external_interrupt;
0390     u64 exit_stop_request;
0391     u64 exit_validity;
0392     u64 exit_instruction;
0393     u64 exit_pei;
0394     u64 halt_no_poll_steal;
0395     u64 instruction_lctl;
0396     u64 instruction_lctlg;
0397     u64 instruction_stctl;
0398     u64 instruction_stctg;
0399     u64 exit_program_interruption;
0400     u64 exit_instr_and_program;
0401     u64 exit_operation_exception;
0402     u64 deliver_ckc;
0403     u64 deliver_cputm;
0404     u64 deliver_external_call;
0405     u64 deliver_emergency_signal;
0406     u64 deliver_service_signal;
0407     u64 deliver_virtio;
0408     u64 deliver_stop_signal;
0409     u64 deliver_prefix_signal;
0410     u64 deliver_restart_signal;
0411     u64 deliver_program;
0412     u64 deliver_io;
0413     u64 deliver_machine_check;
0414     u64 exit_wait_state;
0415     u64 inject_ckc;
0416     u64 inject_cputm;
0417     u64 inject_external_call;
0418     u64 inject_emergency_signal;
0419     u64 inject_mchk;
0420     u64 inject_pfault_init;
0421     u64 inject_program;
0422     u64 inject_restart;
0423     u64 inject_set_prefix;
0424     u64 inject_stop_signal;
0425     u64 instruction_epsw;
0426     u64 instruction_gs;
0427     u64 instruction_io_other;
0428     u64 instruction_lpsw;
0429     u64 instruction_lpswe;
0430     u64 instruction_pfmf;
0431     u64 instruction_ptff;
0432     u64 instruction_sck;
0433     u64 instruction_sckpf;
0434     u64 instruction_stidp;
0435     u64 instruction_spx;
0436     u64 instruction_stpx;
0437     u64 instruction_stap;
0438     u64 instruction_iske;
0439     u64 instruction_ri;
0440     u64 instruction_rrbe;
0441     u64 instruction_sske;
0442     u64 instruction_ipte_interlock;
0443     u64 instruction_stsi;
0444     u64 instruction_stfl;
0445     u64 instruction_tb;
0446     u64 instruction_tpi;
0447     u64 instruction_tprot;
0448     u64 instruction_tsch;
0449     u64 instruction_sie;
0450     u64 instruction_essa;
0451     u64 instruction_sthyi;
0452     u64 instruction_sigp_sense;
0453     u64 instruction_sigp_sense_running;
0454     u64 instruction_sigp_external_call;
0455     u64 instruction_sigp_emergency;
0456     u64 instruction_sigp_cond_emergency;
0457     u64 instruction_sigp_start;
0458     u64 instruction_sigp_stop;
0459     u64 instruction_sigp_stop_store_status;
0460     u64 instruction_sigp_store_status;
0461     u64 instruction_sigp_store_adtl_status;
0462     u64 instruction_sigp_arch;
0463     u64 instruction_sigp_prefix;
0464     u64 instruction_sigp_restart;
0465     u64 instruction_sigp_init_cpu_reset;
0466     u64 instruction_sigp_cpu_reset;
0467     u64 instruction_sigp_unknown;
0468     u64 instruction_diagnose_10;
0469     u64 instruction_diagnose_44;
0470     u64 instruction_diagnose_9c;
0471     u64 diag_9c_ignored;
0472     u64 diag_9c_forward;
0473     u64 instruction_diagnose_258;
0474     u64 instruction_diagnose_308;
0475     u64 instruction_diagnose_500;
0476     u64 instruction_diagnose_other;
0477     u64 pfault_sync;
0478 };
0479 
0480 #define PGM_OPERATION           0x01
0481 #define PGM_PRIVILEGED_OP       0x02
0482 #define PGM_EXECUTE         0x03
0483 #define PGM_PROTECTION          0x04
0484 #define PGM_ADDRESSING          0x05
0485 #define PGM_SPECIFICATION       0x06
0486 #define PGM_DATA            0x07
0487 #define PGM_FIXED_POINT_OVERFLOW    0x08
0488 #define PGM_FIXED_POINT_DIVIDE      0x09
0489 #define PGM_DECIMAL_OVERFLOW        0x0a
0490 #define PGM_DECIMAL_DIVIDE      0x0b
0491 #define PGM_HFP_EXPONENT_OVERFLOW   0x0c
0492 #define PGM_HFP_EXPONENT_UNDERFLOW  0x0d
0493 #define PGM_HFP_SIGNIFICANCE        0x0e
0494 #define PGM_HFP_DIVIDE          0x0f
0495 #define PGM_SEGMENT_TRANSLATION     0x10
0496 #define PGM_PAGE_TRANSLATION        0x11
0497 #define PGM_TRANSLATION_SPEC        0x12
0498 #define PGM_SPECIAL_OPERATION       0x13
0499 #define PGM_OPERAND         0x15
0500 #define PGM_TRACE_TABEL         0x16
0501 #define PGM_VECTOR_PROCESSING       0x1b
0502 #define PGM_SPACE_SWITCH        0x1c
0503 #define PGM_HFP_SQUARE_ROOT     0x1d
0504 #define PGM_PC_TRANSLATION_SPEC     0x1f
0505 #define PGM_AFX_TRANSLATION     0x20
0506 #define PGM_ASX_TRANSLATION     0x21
0507 #define PGM_LX_TRANSLATION      0x22
0508 #define PGM_EX_TRANSLATION      0x23
0509 #define PGM_PRIMARY_AUTHORITY       0x24
0510 #define PGM_SECONDARY_AUTHORITY     0x25
0511 #define PGM_LFX_TRANSLATION     0x26
0512 #define PGM_LSX_TRANSLATION     0x27
0513 #define PGM_ALET_SPECIFICATION      0x28
0514 #define PGM_ALEN_TRANSLATION        0x29
0515 #define PGM_ALE_SEQUENCE        0x2a
0516 #define PGM_ASTE_VALIDITY       0x2b
0517 #define PGM_ASTE_SEQUENCE       0x2c
0518 #define PGM_EXTENDED_AUTHORITY      0x2d
0519 #define PGM_LSTE_SEQUENCE       0x2e
0520 #define PGM_ASTE_INSTANCE       0x2f
0521 #define PGM_STACK_FULL          0x30
0522 #define PGM_STACK_EMPTY         0x31
0523 #define PGM_STACK_SPECIFICATION     0x32
0524 #define PGM_STACK_TYPE          0x33
0525 #define PGM_STACK_OPERATION     0x34
0526 #define PGM_ASCE_TYPE           0x38
0527 #define PGM_REGION_FIRST_TRANS      0x39
0528 #define PGM_REGION_SECOND_TRANS     0x3a
0529 #define PGM_REGION_THIRD_TRANS      0x3b
0530 #define PGM_MONITOR         0x40
0531 #define PGM_PER             0x80
0532 #define PGM_CRYPTO_OPERATION        0x119
0533 
0534 /* irq types in ascend order of priorities */
0535 enum irq_types {
0536     IRQ_PEND_SET_PREFIX = 0,
0537     IRQ_PEND_RESTART,
0538     IRQ_PEND_SIGP_STOP,
0539     IRQ_PEND_IO_ISC_7,
0540     IRQ_PEND_IO_ISC_6,
0541     IRQ_PEND_IO_ISC_5,
0542     IRQ_PEND_IO_ISC_4,
0543     IRQ_PEND_IO_ISC_3,
0544     IRQ_PEND_IO_ISC_2,
0545     IRQ_PEND_IO_ISC_1,
0546     IRQ_PEND_IO_ISC_0,
0547     IRQ_PEND_VIRTIO,
0548     IRQ_PEND_PFAULT_DONE,
0549     IRQ_PEND_PFAULT_INIT,
0550     IRQ_PEND_EXT_HOST,
0551     IRQ_PEND_EXT_SERVICE,
0552     IRQ_PEND_EXT_SERVICE_EV,
0553     IRQ_PEND_EXT_TIMING,
0554     IRQ_PEND_EXT_CPU_TIMER,
0555     IRQ_PEND_EXT_CLOCK_COMP,
0556     IRQ_PEND_EXT_EXTERNAL,
0557     IRQ_PEND_EXT_EMERGENCY,
0558     IRQ_PEND_EXT_MALFUNC,
0559     IRQ_PEND_EXT_IRQ_KEY,
0560     IRQ_PEND_MCHK_REP,
0561     IRQ_PEND_PROG,
0562     IRQ_PEND_SVC,
0563     IRQ_PEND_MCHK_EX,
0564     IRQ_PEND_COUNT
0565 };
0566 
0567 /* We have 2M for virtio device descriptor pages. Smallest amount of
0568  * memory per page is 24 bytes (1 queue), so (2048*1024) / 24 = 87381
0569  */
0570 #define KVM_S390_MAX_VIRTIO_IRQS 87381
0571 
0572 /*
0573  * Repressible (non-floating) machine check interrupts
0574  * subclass bits in MCIC
0575  */
0576 #define MCHK_EXTD_BIT 58
0577 #define MCHK_DEGR_BIT 56
0578 #define MCHK_WARN_BIT 55
0579 #define MCHK_REP_MASK ((1UL << MCHK_DEGR_BIT) | \
0580                (1UL << MCHK_EXTD_BIT) | \
0581                (1UL << MCHK_WARN_BIT))
0582 
0583 /* Exigent machine check interrupts subclass bits in MCIC */
0584 #define MCHK_SD_BIT 63
0585 #define MCHK_PD_BIT 62
0586 #define MCHK_EX_MASK ((1UL << MCHK_SD_BIT) | (1UL << MCHK_PD_BIT))
0587 
0588 #define IRQ_PEND_EXT_MASK ((1UL << IRQ_PEND_EXT_IRQ_KEY)    | \
0589                (1UL << IRQ_PEND_EXT_CLOCK_COMP) | \
0590                (1UL << IRQ_PEND_EXT_CPU_TIMER)  | \
0591                (1UL << IRQ_PEND_EXT_MALFUNC)    | \
0592                (1UL << IRQ_PEND_EXT_EMERGENCY)  | \
0593                (1UL << IRQ_PEND_EXT_EXTERNAL)   | \
0594                (1UL << IRQ_PEND_EXT_TIMING)     | \
0595                (1UL << IRQ_PEND_EXT_HOST)       | \
0596                (1UL << IRQ_PEND_EXT_SERVICE)    | \
0597                (1UL << IRQ_PEND_EXT_SERVICE_EV) | \
0598                (1UL << IRQ_PEND_VIRTIO)         | \
0599                (1UL << IRQ_PEND_PFAULT_INIT)    | \
0600                (1UL << IRQ_PEND_PFAULT_DONE))
0601 
0602 #define IRQ_PEND_IO_MASK ((1UL << IRQ_PEND_IO_ISC_0) | \
0603               (1UL << IRQ_PEND_IO_ISC_1) | \
0604               (1UL << IRQ_PEND_IO_ISC_2) | \
0605               (1UL << IRQ_PEND_IO_ISC_3) | \
0606               (1UL << IRQ_PEND_IO_ISC_4) | \
0607               (1UL << IRQ_PEND_IO_ISC_5) | \
0608               (1UL << IRQ_PEND_IO_ISC_6) | \
0609               (1UL << IRQ_PEND_IO_ISC_7))
0610 
0611 #define IRQ_PEND_MCHK_MASK ((1UL << IRQ_PEND_MCHK_REP) | \
0612                 (1UL << IRQ_PEND_MCHK_EX))
0613 
0614 #define IRQ_PEND_EXT_II_MASK ((1UL << IRQ_PEND_EXT_CPU_TIMER)  | \
0615                   (1UL << IRQ_PEND_EXT_CLOCK_COMP) | \
0616                   (1UL << IRQ_PEND_EXT_EMERGENCY)  | \
0617                   (1UL << IRQ_PEND_EXT_EXTERNAL)   | \
0618                   (1UL << IRQ_PEND_EXT_SERVICE)    | \
0619                   (1UL << IRQ_PEND_EXT_SERVICE_EV))
0620 
0621 struct kvm_s390_interrupt_info {
0622     struct list_head list;
0623     u64 type;
0624     union {
0625         struct kvm_s390_io_info io;
0626         struct kvm_s390_ext_info ext;
0627         struct kvm_s390_pgm_info pgm;
0628         struct kvm_s390_emerg_info emerg;
0629         struct kvm_s390_extcall_info extcall;
0630         struct kvm_s390_prefix_info prefix;
0631         struct kvm_s390_stop_info stop;
0632         struct kvm_s390_mchk_info mchk;
0633     };
0634 };
0635 
0636 struct kvm_s390_irq_payload {
0637     struct kvm_s390_io_info io;
0638     struct kvm_s390_ext_info ext;
0639     struct kvm_s390_pgm_info pgm;
0640     struct kvm_s390_emerg_info emerg;
0641     struct kvm_s390_extcall_info extcall;
0642     struct kvm_s390_prefix_info prefix;
0643     struct kvm_s390_stop_info stop;
0644     struct kvm_s390_mchk_info mchk;
0645 };
0646 
0647 struct kvm_s390_local_interrupt {
0648     spinlock_t lock;
0649     DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
0650     struct kvm_s390_irq_payload irq;
0651     unsigned long pending_irqs;
0652 };
0653 
0654 #define FIRQ_LIST_IO_ISC_0 0
0655 #define FIRQ_LIST_IO_ISC_1 1
0656 #define FIRQ_LIST_IO_ISC_2 2
0657 #define FIRQ_LIST_IO_ISC_3 3
0658 #define FIRQ_LIST_IO_ISC_4 4
0659 #define FIRQ_LIST_IO_ISC_5 5
0660 #define FIRQ_LIST_IO_ISC_6 6
0661 #define FIRQ_LIST_IO_ISC_7 7
0662 #define FIRQ_LIST_PFAULT   8
0663 #define FIRQ_LIST_VIRTIO   9
0664 #define FIRQ_LIST_COUNT   10
0665 #define FIRQ_CNTR_IO       0
0666 #define FIRQ_CNTR_SERVICE  1
0667 #define FIRQ_CNTR_VIRTIO   2
0668 #define FIRQ_CNTR_PFAULT   3
0669 #define FIRQ_MAX_COUNT     4
0670 
0671 /* mask the AIS mode for a given ISC */
0672 #define AIS_MODE_MASK(isc) (0x80 >> isc)
0673 
0674 #define KVM_S390_AIS_MODE_ALL    0
0675 #define KVM_S390_AIS_MODE_SINGLE 1
0676 
0677 struct kvm_s390_float_interrupt {
0678     unsigned long pending_irqs;
0679     unsigned long masked_irqs;
0680     spinlock_t lock;
0681     struct list_head lists[FIRQ_LIST_COUNT];
0682     int counters[FIRQ_MAX_COUNT];
0683     struct kvm_s390_mchk_info mchk;
0684     struct kvm_s390_ext_info srv_signal;
0685     int next_rr_cpu;
0686     struct mutex ais_lock;
0687     u8 simm;
0688     u8 nimm;
0689 };
0690 
0691 struct kvm_hw_wp_info_arch {
0692     unsigned long addr;
0693     unsigned long phys_addr;
0694     int len;
0695     char *old_data;
0696 };
0697 
0698 struct kvm_hw_bp_info_arch {
0699     unsigned long addr;
0700     int len;
0701 };
0702 
0703 /*
0704  * Only the upper 16 bits of kvm_guest_debug->control are arch specific.
0705  * Further KVM_GUESTDBG flags which an be used from userspace can be found in
0706  * arch/s390/include/uapi/asm/kvm.h
0707  */
0708 #define KVM_GUESTDBG_EXIT_PENDING 0x10000000
0709 
0710 #define guestdbg_enabled(vcpu) \
0711         (vcpu->guest_debug & KVM_GUESTDBG_ENABLE)
0712 #define guestdbg_sstep_enabled(vcpu) \
0713         (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
0714 #define guestdbg_hw_bp_enabled(vcpu) \
0715         (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
0716 #define guestdbg_exit_pending(vcpu) (guestdbg_enabled(vcpu) && \
0717         (vcpu->guest_debug & KVM_GUESTDBG_EXIT_PENDING))
0718 
0719 #define KVM_GUESTDBG_VALID_MASK \
0720         (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP |\
0721         KVM_GUESTDBG_USE_HW_BP | KVM_GUESTDBG_EXIT_PENDING)
0722 
0723 struct kvm_guestdbg_info_arch {
0724     unsigned long cr0;
0725     unsigned long cr9;
0726     unsigned long cr10;
0727     unsigned long cr11;
0728     struct kvm_hw_bp_info_arch *hw_bp_info;
0729     struct kvm_hw_wp_info_arch *hw_wp_info;
0730     int nr_hw_bp;
0731     int nr_hw_wp;
0732     unsigned long last_bp;
0733 };
0734 
0735 struct kvm_s390_pv_vcpu {
0736     u64 handle;
0737     unsigned long stor_base;
0738 };
0739 
0740 struct kvm_vcpu_arch {
0741     struct kvm_s390_sie_block *sie_block;
0742     /* if vsie is active, currently executed shadow sie control block */
0743     struct kvm_s390_sie_block *vsie_block;
0744     unsigned int      host_acrs[NUM_ACRS];
0745     struct gs_cb      *host_gscb;
0746     struct fpu    host_fpregs;
0747     struct kvm_s390_local_interrupt local_int;
0748     struct hrtimer    ckc_timer;
0749     struct kvm_s390_pgm_info pgm;
0750     struct gmap *gmap;
0751     /* backup location for the currently enabled gmap when scheduled out */
0752     struct gmap *enabled_gmap;
0753     struct kvm_guestdbg_info_arch guestdbg;
0754     unsigned long pfault_token;
0755     unsigned long pfault_select;
0756     unsigned long pfault_compare;
0757     bool cputm_enabled;
0758     /*
0759      * The seqcount protects updates to cputm_start and sie_block.cputm,
0760      * this way we can have non-blocking reads with consistent values.
0761      * Only the owning VCPU thread (vcpu->cpu) is allowed to change these
0762      * values and to start/stop/enable/disable cpu timer accounting.
0763      */
0764     seqcount_t cputm_seqcount;
0765     __u64 cputm_start;
0766     bool gs_enabled;
0767     bool skey_enabled;
0768     struct kvm_s390_pv_vcpu pv;
0769     union diag318_info diag318_info;
0770 };
0771 
0772 struct kvm_vm_stat {
0773     struct kvm_vm_stat_generic generic;
0774     u64 inject_io;
0775     u64 inject_float_mchk;
0776     u64 inject_pfault_done;
0777     u64 inject_service_signal;
0778     u64 inject_virtio;
0779     u64 aen_forward;
0780 };
0781 
0782 struct kvm_arch_memory_slot {
0783 };
0784 
0785 struct s390_map_info {
0786     struct list_head list;
0787     __u64 guest_addr;
0788     __u64 addr;
0789     struct page *page;
0790 };
0791 
0792 struct s390_io_adapter {
0793     unsigned int id;
0794     int isc;
0795     bool maskable;
0796     bool masked;
0797     bool swap;
0798     bool suppressible;
0799 };
0800 
0801 #define MAX_S390_IO_ADAPTERS ((MAX_ISC + 1) * 8)
0802 #define MAX_S390_ADAPTER_MAPS 256
0803 
0804 /* maximum size of facilities and facility mask is 2k bytes */
0805 #define S390_ARCH_FAC_LIST_SIZE_BYTE (1<<11)
0806 #define S390_ARCH_FAC_LIST_SIZE_U64 \
0807     (S390_ARCH_FAC_LIST_SIZE_BYTE / sizeof(u64))
0808 #define S390_ARCH_FAC_MASK_SIZE_BYTE S390_ARCH_FAC_LIST_SIZE_BYTE
0809 #define S390_ARCH_FAC_MASK_SIZE_U64 \
0810     (S390_ARCH_FAC_MASK_SIZE_BYTE / sizeof(u64))
0811 
0812 struct kvm_s390_cpu_model {
0813     /* facility mask supported by kvm & hosting machine */
0814     __u64 fac_mask[S390_ARCH_FAC_LIST_SIZE_U64];
0815     struct kvm_s390_vm_cpu_subfunc subfuncs;
0816     /* facility list requested by guest (in dma page) */
0817     __u64 *fac_list;
0818     u64 cpuid;
0819     unsigned short ibc;
0820 };
0821 
0822 typedef int (*crypto_hook)(struct kvm_vcpu *vcpu);
0823 
0824 struct kvm_s390_crypto {
0825     struct kvm_s390_crypto_cb *crycb;
0826     struct rw_semaphore pqap_hook_rwsem;
0827     crypto_hook *pqap_hook;
0828     __u32 crycbd;
0829     __u8 aes_kw;
0830     __u8 dea_kw;
0831     __u8 apie;
0832 };
0833 
0834 #define APCB0_MASK_SIZE 1
0835 struct kvm_s390_apcb0 {
0836     __u64 apm[APCB0_MASK_SIZE];     /* 0x0000 */
0837     __u64 aqm[APCB0_MASK_SIZE];     /* 0x0008 */
0838     __u64 adm[APCB0_MASK_SIZE];     /* 0x0010 */
0839     __u64 reserved18;           /* 0x0018 */
0840 };
0841 
0842 #define APCB1_MASK_SIZE 4
0843 struct kvm_s390_apcb1 {
0844     __u64 apm[APCB1_MASK_SIZE];     /* 0x0000 */
0845     __u64 aqm[APCB1_MASK_SIZE];     /* 0x0020 */
0846     __u64 adm[APCB1_MASK_SIZE];     /* 0x0040 */
0847     __u64 reserved60[4];            /* 0x0060 */
0848 };
0849 
0850 struct kvm_s390_crypto_cb {
0851     struct kvm_s390_apcb0 apcb0;        /* 0x0000 */
0852     __u8   reserved20[0x0048 - 0x0020]; /* 0x0020 */
0853     __u8   dea_wrapping_key_mask[24];   /* 0x0048 */
0854     __u8   aes_wrapping_key_mask[32];   /* 0x0060 */
0855     struct kvm_s390_apcb1 apcb1;        /* 0x0080 */
0856 };
0857 
0858 struct kvm_s390_gisa {
0859     union {
0860         struct { /* common to all formats */
0861             u32 next_alert;
0862             u8  ipm;
0863             u8  reserved01[2];
0864             u8  iam;
0865         };
0866         struct { /* format 0 */
0867             u32 next_alert;
0868             u8  ipm;
0869             u8  reserved01;
0870             u8  : 6;
0871             u8  g : 1;
0872             u8  c : 1;
0873             u8  iam;
0874             u8  reserved02[4];
0875             u32 airq_count;
0876         } g0;
0877         struct { /* format 1 */
0878             u32 next_alert;
0879             u8  ipm;
0880             u8  simm;
0881             u8  nimm;
0882             u8  iam;
0883             u8  aism[8];
0884             u8  : 6;
0885             u8  g : 1;
0886             u8  c : 1;
0887             u8  reserved03[11];
0888             u32 airq_count;
0889         } g1;
0890         struct {
0891             u64 word[4];
0892         } u64;
0893     };
0894 };
0895 
0896 struct kvm_s390_gib {
0897     u32 alert_list_origin;
0898     u32 reserved01;
0899     u8:5;
0900     u8  nisc:3;
0901     u8  reserved03[3];
0902     u32 reserved04[5];
0903 };
0904 
0905 /*
0906  * sie_page2 has to be allocated as DMA because fac_list, crycb and
0907  * gisa need 31bit addresses in the sie control block.
0908  */
0909 struct sie_page2 {
0910     __u64 fac_list[S390_ARCH_FAC_LIST_SIZE_U64];    /* 0x0000 */
0911     struct kvm_s390_crypto_cb crycb;        /* 0x0800 */
0912     struct kvm_s390_gisa gisa;          /* 0x0900 */
0913     struct kvm *kvm;                /* 0x0920 */
0914     u8 reserved928[0x1000 - 0x928];         /* 0x0928 */
0915 };
0916 
0917 struct kvm_s390_vsie {
0918     struct mutex mutex;
0919     struct radix_tree_root addr_to_page;
0920     int page_count;
0921     int next;
0922     struct page *pages[KVM_MAX_VCPUS];
0923 };
0924 
0925 struct kvm_s390_gisa_iam {
0926     u8 mask;
0927     spinlock_t ref_lock;
0928     u32 ref_count[MAX_ISC + 1];
0929 };
0930 
0931 struct kvm_s390_gisa_interrupt {
0932     struct kvm_s390_gisa *origin;
0933     struct kvm_s390_gisa_iam alert;
0934     struct hrtimer timer;
0935     u64 expires;
0936     DECLARE_BITMAP(kicked_mask, KVM_MAX_VCPUS);
0937 };
0938 
0939 struct kvm_s390_pv {
0940     u64 handle;
0941     u64 guest_len;
0942     unsigned long stor_base;
0943     void *stor_var;
0944     bool dumping;
0945     struct mmu_notifier mmu_notifier;
0946 };
0947 
0948 struct kvm_arch{
0949     void *sca;
0950     int use_esca;
0951     rwlock_t sca_lock;
0952     debug_info_t *dbf;
0953     struct kvm_s390_float_interrupt float_int;
0954     struct kvm_device *flic;
0955     struct gmap *gmap;
0956     unsigned long mem_limit;
0957     int css_support;
0958     int use_irqchip;
0959     int use_cmma;
0960     int use_pfmfi;
0961     int use_skf;
0962     int use_zpci_interp;
0963     int user_cpu_state_ctrl;
0964     int user_sigp;
0965     int user_stsi;
0966     int user_instr0;
0967     struct s390_io_adapter *adapters[MAX_S390_IO_ADAPTERS];
0968     wait_queue_head_t ipte_wq;
0969     int ipte_lock_count;
0970     struct mutex ipte_mutex;
0971     spinlock_t start_stop_lock;
0972     struct sie_page2 *sie_page2;
0973     struct kvm_s390_cpu_model model;
0974     struct kvm_s390_crypto crypto;
0975     struct kvm_s390_vsie vsie;
0976     u8 epdx;
0977     u64 epoch;
0978     int migration_mode;
0979     atomic64_t cmma_dirty_pages;
0980     /* subset of available cpu features enabled by user space */
0981     DECLARE_BITMAP(cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
0982     /* indexed by vcpu_idx */
0983     DECLARE_BITMAP(idle_mask, KVM_MAX_VCPUS);
0984     struct kvm_s390_gisa_interrupt gisa_int;
0985     struct kvm_s390_pv pv;
0986     struct list_head kzdev_list;
0987     spinlock_t kzdev_list_lock;
0988 };
0989 
0990 #define KVM_HVA_ERR_BAD     (-1UL)
0991 #define KVM_HVA_ERR_RO_BAD  (-2UL)
0992 
0993 static inline bool kvm_is_error_hva(unsigned long addr)
0994 {
0995     return IS_ERR_VALUE(addr);
0996 }
0997 
0998 #define ASYNC_PF_PER_VCPU   64
0999 struct kvm_arch_async_pf {
1000     unsigned long pfault_token;
1001 };
1002 
1003 bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu);
1004 
1005 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
1006                    struct kvm_async_pf *work);
1007 
1008 bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
1009                      struct kvm_async_pf *work);
1010 
1011 void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
1012                  struct kvm_async_pf *work);
1013 
1014 static inline void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) {}
1015 
1016 void kvm_arch_crypto_clear_masks(struct kvm *kvm);
1017 void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
1018                    unsigned long *aqm, unsigned long *adm);
1019 
1020 extern int sie64a(struct kvm_s390_sie_block *, u64 *);
1021 extern char sie_exit;
1022 
1023 extern int kvm_s390_gisc_register(struct kvm *kvm, u32 gisc);
1024 extern int kvm_s390_gisc_unregister(struct kvm *kvm, u32 gisc);
1025 
1026 static inline void kvm_arch_hardware_disable(void) {}
1027 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
1028 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
1029 static inline void kvm_arch_free_memslot(struct kvm *kvm,
1030                      struct kvm_memory_slot *slot) {}
1031 static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {}
1032 static inline void kvm_arch_flush_shadow_all(struct kvm *kvm) {}
1033 static inline void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1034         struct kvm_memory_slot *slot) {}
1035 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
1036 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
1037 
1038 #define __KVM_HAVE_ARCH_VM_FREE
1039 void kvm_arch_free_vm(struct kvm *kvm);
1040 
1041 struct zpci_kvm_hook {
1042     int (*kvm_register)(void *opaque, struct kvm *kvm);
1043     void (*kvm_unregister)(void *opaque);
1044 };
1045 
1046 extern struct zpci_kvm_hook zpci_kvm_hook;
1047 
1048 #endif