2019-06-03 07:44:50 +02:00
// SPDX-License-Identifier: GPL-2.0-only
2015-12-01 15:02:35 +01:00
/*
* Copyright ( C ) 2015 , 2016 ARM Ltd .
*/
# include <linux/uaccess.h>
# include <linux/interrupt.h>
# include <linux/cpu.h>
# include <linux/kvm_host.h>
# include <kvm/arm_vgic.h>
2019-08-23 11:34:16 +01:00
# include <asm/kvm_emulate.h>
2015-12-01 15:02:35 +01:00
# include <asm/kvm_mmu.h>
# include "vgic.h"
2015-12-21 18:09:38 +01:00
/*
* Initialization rules : there are multiple stages to the vgic
2017-03-18 13:40:37 +01:00
* initialization , both for the distributor and the CPU interfaces . The basic
* idea is that even though the VGIC is not functional or not requested from
* user space , the critical path of the run loop can still call VGIC functions
* that just won ' t do anything , without them having to check additional
* initialization flags to ensure they don ' t look at uninitialized data
* structures .
2015-12-21 18:09:38 +01:00
*
* Distributor :
*
* - kvm_vgic_early_init ( ) : initialization of static data that doesn ' t
* depend on any sizing information or emulation type . No allocation
* is allowed there .
*
* - vgic_init ( ) : allocation and initialization of the generic data
* structures that depend on sizing information ( number of CPUs ,
* number of interrupts ) . Also initializes the vcpu specific data
* structures . Can be executed lazily for GICv2 .
*
* CPU Interface :
*
2018-05-22 09:55:13 +02:00
* - kvm_vgic_vcpu_init ( ) : initialization of static data that
2015-12-21 18:09:38 +01:00
* doesn ' t depend on any sizing information or emulation type . No
* allocation is allowed there .
*/
/* EARLY INIT */
2017-03-18 13:40:37 +01:00
/**
* kvm_vgic_early_init ( ) - Initialize static VGIC VCPU data structures
* @ kvm : The VM whose VGIC districutor should be initialized
*
* Only do initialization of static structures that don ' t require any
* allocation or sizing information from userspace . vgic_init ( ) called
* kvm_vgic_dist_init ( ) which takes care of the rest .
2015-12-21 18:09:38 +01:00
*/
void kvm_vgic_early_init ( struct kvm * kvm )
{
2017-03-18 13:40:37 +01:00
struct vgic_dist * dist = & kvm - > arch . vgic ;
INIT_LIST_HEAD ( & dist - > lpi_list_head ) ;
2019-03-18 10:13:01 +00:00
INIT_LIST_HEAD ( & dist - > lpi_translation_cache ) ;
2019-01-07 15:06:16 +00:00
raw_spin_lock_init ( & dist - > lpi_list_lock ) ;
2015-12-21 18:09:38 +01:00
}
2015-12-21 14:50:50 +01:00
/* CREATION */
/**
* kvm_vgic_create : triggered by the instantiation of the VGIC device by
* user space , either through the legacy KVM_CREATE_IRQCHIP ioctl ( v2 only )
* or through the generic KVM_CREATE_DEVICE API ioctl .
* irqchip_in_kernel ( ) tells you if this function succeeded or not .
2015-12-21 18:09:38 +01:00
* @ kvm : kvm struct pointer
* @ type : KVM_DEV_TYPE_ARM_VGIC_V [ 23 ]
2015-12-21 14:50:50 +01:00
*/
int kvm_vgic_create ( struct kvm * kvm , u32 type )
{
2019-11-30 10:45:18 +08:00
int i , ret ;
2015-12-21 14:50:50 +01:00
struct kvm_vcpu * vcpu ;
2016-08-09 19:13:01 +02:00
if ( irqchip_in_kernel ( kvm ) )
return - EEXIST ;
2015-12-21 14:50:50 +01:00
/*
* This function is also called by the KVM_CREATE_IRQCHIP handler ,
* which had no chance yet to check the availability of the GICv2
* emulation . So check this here again . KVM_CREATE_DEVICE does
* the proper checks already .
*/
if ( type = = KVM_DEV_TYPE_ARM_VGIC_V2 & &
2016-08-09 19:13:01 +02:00
! kvm_vgic_global_state . can_emulate_gicv2 )
return - ENODEV ;
2015-12-21 14:50:50 +01:00
ret = - EBUSY ;
2019-11-30 10:45:18 +08:00
if ( ! lock_all_vcpus ( kvm ) )
return ret ;
2015-12-21 14:50:50 +01:00
kvm_for_each_vcpu ( i , vcpu , kvm ) {
if ( vcpu - > arch . has_run_once )
goto out_unlock ;
}
ret = 0 ;
if ( type = = KVM_DEV_TYPE_ARM_VGIC_V2 )
kvm - > arch . max_vcpus = VGIC_V2_MAX_CPUS ;
else
kvm - > arch . max_vcpus = VGIC_V3_MAX_CPUS ;
if ( atomic_read ( & kvm - > online_vcpus ) > kvm - > arch . max_vcpus ) {
ret = - E2BIG ;
goto out_unlock ;
}
kvm - > arch . vgic . in_kernel = true ;
kvm - > arch . vgic . vgic_model = type ;
kvm - > arch . vgic . vgic_dist_base = VGIC_ADDR_UNDEF ;
2018-05-22 09:55:08 +02:00
if ( type = = KVM_DEV_TYPE_ARM_VGIC_V2 )
kvm - > arch . vgic . vgic_cpu_base = VGIC_ADDR_UNDEF ;
else
INIT_LIST_HEAD ( & kvm - > arch . vgic . rd_regions ) ;
2015-12-21 14:50:50 +01:00
out_unlock :
2019-11-30 10:45:18 +08:00
unlock_all_vcpus ( kvm ) ;
2015-12-21 14:50:50 +01:00
return ret ;
}
2015-12-21 18:09:38 +01:00
/* INIT/DESTROY */
/**
* kvm_vgic_dist_init : initialize the dist data structures
* @ kvm : kvm struct pointer
* @ nr_spis : number of spis , frozen by caller
*/
static int kvm_vgic_dist_init ( struct kvm * kvm , unsigned int nr_spis )
{
struct vgic_dist * dist = & kvm - > arch . vgic ;
struct kvm_vcpu * vcpu0 = kvm_get_vcpu ( kvm , 0 ) ;
int i ;
dist - > spis = kcalloc ( nr_spis , sizeof ( struct vgic_irq ) , GFP_KERNEL ) ;
if ( ! dist - > spis )
return - ENOMEM ;
/*
* In the following code we do not take the irq struct lock since
* no other action on irq structs can happen while the VGIC is
* not initialized yet :
* If someone wants to inject an interrupt or does a MMIO access , we
* require prior initialization in case of a virtual GICv3 or trigger
* initialization when using a virtual GICv2 .
*/
for ( i = 0 ; i < nr_spis ; i + + ) {
struct vgic_irq * irq = & dist - > spis [ i ] ;
irq - > intid = i + VGIC_NR_PRIVATE_IRQS ;
INIT_LIST_HEAD ( & irq - > ap_list ) ;
2019-01-07 15:06:15 +00:00
raw_spin_lock_init ( & irq - > irq_lock ) ;
2015-12-21 18:09:38 +01:00
irq - > vcpu = NULL ;
irq - > target_vcpu = vcpu0 ;
2016-07-15 12:43:27 +01:00
kref_init ( & irq - > refcount ) ;
2019-08-23 11:34:16 +01:00
switch ( dist - > vgic_model ) {
case KVM_DEV_TYPE_ARM_VGIC_V2 :
2015-12-21 18:09:38 +01:00
irq - > targets = 0 ;
2018-07-16 15:06:21 +02:00
irq - > group = 0 ;
2019-08-23 11:34:16 +01:00
break ;
case KVM_DEV_TYPE_ARM_VGIC_V3 :
2015-12-21 18:09:38 +01:00
irq - > mpidr = 0 ;
2018-07-16 15:06:21 +02:00
irq - > group = 1 ;
2019-08-23 11:34:16 +01:00
break ;
default :
kfree ( dist - > spis ) ;
2019-11-28 14:38:48 +08:00
dist - > spis = NULL ;
2019-08-23 11:34:16 +01:00
return - EINVAL ;
2018-07-16 15:06:21 +02:00
}
2015-12-21 18:09:38 +01:00
}
return 0 ;
}
/**
2018-05-22 09:55:13 +02:00
* kvm_vgic_vcpu_init ( ) - Initialize static VGIC VCPU data
* structures and register VCPU - specific KVM iodevs
*
2017-05-08 12:30:24 +02:00
* @ vcpu : pointer to the VCPU being created and initialized
2018-05-22 09:55:13 +02:00
*
* Only do initialization , but do not actually enable the
* VGIC CPU interface
2015-12-21 18:09:38 +01:00
*/
2017-05-08 12:30:24 +02:00
int kvm_vgic_vcpu_init ( struct kvm_vcpu * vcpu )
{
2018-05-22 09:55:13 +02:00
struct vgic_cpu * vgic_cpu = & vcpu - > arch . vgic_cpu ;
2017-05-08 12:30:24 +02:00
struct vgic_dist * dist = & vcpu - > kvm - > arch . vgic ;
2018-05-22 09:55:13 +02:00
int ret = 0 ;
int i ;
2018-05-22 09:55:14 +02:00
vgic_cpu - > rd_iodev . base_addr = VGIC_ADDR_UNDEF ;
2018-05-22 09:55:13 +02:00
INIT_LIST_HEAD ( & vgic_cpu - > ap_list_head ) ;
2019-01-07 15:06:17 +00:00
raw_spin_lock_init ( & vgic_cpu - > ap_list_lock ) ;
2019-11-07 16:04:11 +00:00
atomic_set ( & vgic_cpu - > vgic_v3 . its_vpe . vlpi_count , 0 ) ;
2018-05-22 09:55:13 +02:00
/*
* Enable and configure all SGIs to be edge - triggered and
* configure all PPIs as level - triggered .
*/
for ( i = 0 ; i < VGIC_NR_PRIVATE_IRQS ; i + + ) {
struct vgic_irq * irq = & vgic_cpu - > private_irqs [ i ] ;
INIT_LIST_HEAD ( & irq - > ap_list ) ;
2019-01-07 15:06:15 +00:00
raw_spin_lock_init ( & irq - > irq_lock ) ;
2018-05-22 09:55:13 +02:00
irq - > intid = i ;
irq - > vcpu = NULL ;
irq - > target_vcpu = vcpu ;
kref_init ( & irq - > refcount ) ;
if ( vgic_irq_is_sgi ( i ) ) {
/* SGIs */
irq - > enabled = 1 ;
irq - > config = VGIC_CONFIG_EDGE ;
} else {
/* PPIs */
irq - > config = VGIC_CONFIG_LEVEL ;
}
}
2017-05-08 12:30:24 +02:00
if ( ! irqchip_in_kernel ( vcpu - > kvm ) )
return 0 ;
/*
* If we are creating a VCPU with a GICv3 we must also register the
* KVM io device for the redistributor that belongs to this VCPU .
*/
2017-05-17 13:12:51 +02:00
if ( dist - > vgic_model = = KVM_DEV_TYPE_ARM_VGIC_V3 ) {
mutex_lock ( & vcpu - > kvm - > lock ) ;
2017-05-08 12:30:24 +02:00
ret = vgic_register_redist_iodev ( vcpu ) ;
2017-05-17 13:12:51 +02:00
mutex_unlock ( & vcpu - > kvm - > lock ) ;
}
2017-05-08 12:30:24 +02:00
return ret ;
}
2017-05-08 12:09:13 +02:00
static void kvm_vgic_vcpu_enable ( struct kvm_vcpu * vcpu )
2015-12-21 18:09:38 +01:00
{
if ( kvm_vgic_global_state . type = = VGIC_V2 )
vgic_v2_enable ( vcpu ) ;
else
vgic_v3_enable ( vcpu ) ;
}
/*
* vgic_init : allocates and initializes dist and vcpu data structures
* depending on two dimensioning parameters :
* - the number of spis
* - the number of vcpus
* The function is generally called when nr_spis has been explicitly set
* by the guest through the KVM DEVICE API . If not nr_spis is set to 256.
* vgic_initialized ( ) returns true when this function has succeeded .
* Must be called with kvm - > lock held !
*/
int vgic_init ( struct kvm * kvm )
{
struct vgic_dist * dist = & kvm - > arch . vgic ;
struct kvm_vcpu * vcpu ;
2019-01-10 15:33:52 +01:00
int ret = 0 , i , idx ;
2015-12-21 18:09:38 +01:00
if ( vgic_initialized ( kvm ) )
return 0 ;
2018-07-03 22:54:14 +02:00
/* Are we also in the middle of creating a VCPU? */
if ( kvm - > created_vcpus ! = atomic_read ( & kvm - > online_vcpus ) )
return - EBUSY ;
2015-12-21 18:09:38 +01:00
/* freeze the number of spis */
if ( ! dist - > nr_spis )
dist - > nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS ;
ret = kvm_vgic_dist_init ( kvm , dist - > nr_spis ) ;
if ( ret )
goto out ;
2019-01-10 15:33:52 +01:00
/* Initialize groups on CPUs created before the VGIC type was known */
kvm_for_each_vcpu ( idx , vcpu , kvm ) {
struct vgic_cpu * vgic_cpu = & vcpu - > arch . vgic_cpu ;
for ( i = 0 ; i < VGIC_NR_PRIVATE_IRQS ; i + + ) {
struct vgic_irq * irq = & vgic_cpu - > private_irqs [ i ] ;
2019-08-23 11:34:16 +01:00
switch ( dist - > vgic_model ) {
case KVM_DEV_TYPE_ARM_VGIC_V3 :
2019-01-10 15:33:52 +01:00
irq - > group = 1 ;
2019-08-23 11:34:16 +01:00
irq - > mpidr = kvm_vcpu_get_mpidr_aff ( vcpu ) ;
break ;
case KVM_DEV_TYPE_ARM_VGIC_V2 :
2019-01-10 15:33:52 +01:00
irq - > group = 0 ;
2019-08-23 11:34:16 +01:00
irq - > targets = 1U < < idx ;
break ;
default :
ret = - EINVAL ;
goto out ;
}
2019-01-10 15:33:52 +01:00
}
}
2018-01-12 11:40:21 +01:00
if ( vgic_has_its ( kvm ) ) {
2019-03-18 10:13:01 +00:00
vgic_lpi_translation_cache_init ( kvm ) ;
2018-01-12 11:40:21 +01:00
ret = vgic_v4_init ( kvm ) ;
if ( ret )
goto out ;
}
2017-10-27 15:28:38 +01:00
2015-12-21 18:09:38 +01:00
kvm_for_each_vcpu ( i , vcpu , kvm )
2017-05-08 12:09:13 +02:00
kvm_vgic_vcpu_enable ( vcpu ) ;
2015-12-21 18:09:38 +01:00
2016-07-22 16:20:41 +00:00
ret = kvm_vgic_setup_default_irq_routing ( kvm ) ;
if ( ret )
goto out ;
2017-01-17 23:09:13 +01:00
vgic_debug_init ( kvm ) ;
2018-07-16 15:06:25 +02:00
dist - > implementation_rev = 2 ;
2015-12-21 18:09:38 +01:00
dist - > initialized = true ;
2016-03-24 11:21:04 +01:00
2015-12-21 18:09:38 +01:00
out :
return ret ;
}
static void kvm_vgic_dist_destroy ( struct kvm * kvm )
{
struct vgic_dist * dist = & kvm - > arch . vgic ;
2018-05-22 09:55:08 +02:00
struct vgic_redist_region * rdreg , * next ;
2015-12-21 18:09:38 +01:00
dist - > ready = false ;
dist - > initialized = false ;
kfree ( dist - > spis ) ;
2018-05-22 09:55:06 +02:00
dist - > spis = NULL ;
2015-12-21 18:09:38 +01:00
dist - > nr_spis = 0 ;
2017-10-27 15:28:38 +01:00
2018-05-22 09:55:08 +02:00
if ( kvm - > arch . vgic . vgic_model = = KVM_DEV_TYPE_ARM_VGIC_V3 ) {
list_for_each_entry_safe ( rdreg , next , & dist - > rd_regions , list ) {
list_del ( & rdreg - > list ) ;
kfree ( rdreg ) ;
}
INIT_LIST_HEAD ( & dist - > rd_regions ) ;
}
2019-03-18 10:13:01 +00:00
if ( vgic_has_its ( kvm ) )
vgic_lpi_translation_cache_destroy ( kvm ) ;
2017-10-27 15:28:38 +01:00
if ( vgic_supports_direct_msis ( kvm ) )
vgic_v4_teardown ( kvm ) ;
2015-12-21 18:09:38 +01:00
}
void kvm_vgic_vcpu_destroy ( struct kvm_vcpu * vcpu )
{
struct vgic_cpu * vgic_cpu = & vcpu - > arch . vgic_cpu ;
2020-04-14 11:03:47 +08:00
/*
* Retire all pending LPIs on this vcpu anyway as we ' re
* going to destroy it .
*/
vgic_flush_pending_lpis ( vcpu ) ;
2015-12-21 18:09:38 +01:00
INIT_LIST_HEAD ( & vgic_cpu - > ap_list_head ) ;
}
2017-01-12 09:21:56 +00:00
/* To be called with kvm->lock held */
static void __kvm_vgic_destroy ( struct kvm * kvm )
2015-12-21 18:09:38 +01:00
{
struct kvm_vcpu * vcpu ;
int i ;
2017-01-17 23:09:13 +01:00
vgic_debug_destroy ( kvm ) ;
2015-12-21 18:09:38 +01:00
kvm_for_each_vcpu ( i , vcpu , kvm )
kvm_vgic_vcpu_destroy ( vcpu ) ;
2020-04-14 11:03:47 +08:00
kvm_vgic_dist_destroy ( kvm ) ;
2015-12-21 18:09:38 +01:00
}
2017-01-12 09:21:56 +00:00
void kvm_vgic_destroy ( struct kvm * kvm )
{
mutex_lock ( & kvm - > lock ) ;
__kvm_vgic_destroy ( kvm ) ;
mutex_unlock ( & kvm - > lock ) ;
}
2015-12-21 18:09:38 +01:00
/**
* vgic_lazy_init : Lazy init is only allowed if the GIC exposed to the guest
* is a GICv2 . A GICv3 must be explicitly initialized by the guest using the
* KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group .
* @ kvm : kvm struct pointer
*/
int vgic_lazy_init ( struct kvm * kvm )
{
int ret = 0 ;
if ( unlikely ( ! vgic_initialized ( kvm ) ) ) {
/*
* We only provide the automatic initialization of the VGIC
* for the legacy case of a GICv2 . Any other type must
* be explicitly initialized once setup with the respective
* KVM device call .
*/
if ( kvm - > arch . vgic . vgic_model ! = KVM_DEV_TYPE_ARM_VGIC_V2 )
return - EBUSY ;
mutex_lock ( & kvm - > lock ) ;
ret = vgic_init ( kvm ) ;
mutex_unlock ( & kvm - > lock ) ;
}
return ret ;
}
2015-12-21 15:04:42 +01:00
/* RESOURCE MAPPING */
/**
* Map the MMIO regions depending on the VGIC model exposed to the guest
* called on the first VCPU run .
* Also map the virtual CPU interface into the VM .
* v2 / v3 derivatives call vgic_init if not already done .
* vgic_ready ( ) returns true if this function has succeeded .
* @ kvm : kvm struct pointer
*/
int kvm_vgic_map_resources ( struct kvm * kvm )
{
struct vgic_dist * dist = & kvm - > arch . vgic ;
int ret = 0 ;
mutex_lock ( & kvm - > lock ) ;
if ( ! irqchip_in_kernel ( kvm ) )
goto out ;
if ( dist - > vgic_model = = KVM_DEV_TYPE_ARM_VGIC_V2 )
ret = vgic_v2_map_resources ( kvm ) ;
else
ret = vgic_v3_map_resources ( kvm ) ;
2017-01-12 09:21:56 +00:00
if ( ret )
__kvm_vgic_destroy ( kvm ) ;
2015-12-21 15:04:42 +01:00
out :
mutex_unlock ( & kvm - > lock ) ;
return ret ;
}
2015-12-01 15:02:35 +01:00
/* GENERIC PROBE */
2016-07-13 17:17:02 +00:00
static int vgic_init_cpu_starting ( unsigned int cpu )
2015-12-01 15:02:35 +01:00
{
enable_percpu_irq ( kvm_vgic_global_state . maint_irq , 0 ) ;
2016-07-13 17:17:02 +00:00
return 0 ;
2015-12-01 15:02:35 +01:00
}
2016-07-13 17:17:02 +00:00
static int vgic_init_cpu_dying ( unsigned int cpu )
{
disable_percpu_irq ( kvm_vgic_global_state . maint_irq ) ;
return 0 ;
2015-12-01 15:02:35 +01:00
}
static irqreturn_t vgic_maintenance_handler ( int irq , void * data )
{
/*
* We cannot rely on the vgic maintenance interrupt to be
* delivered synchronously . This means we can only use it to
* exit the VM , and we perform the handling of EOIed
2018-05-02 11:53:03 +01:00
* interrupts on the exit path ( see vgic_fold_lr_state ) .
2015-12-01 15:02:35 +01:00
*/
return IRQ_HANDLED ;
}
2017-03-18 13:56:56 +01:00
/**
* kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
*
* For a specific CPU , initialize the GIC VE hardware .
*/
void kvm_vgic_init_cpu_hardware ( void )
{
BUG_ON ( preemptible ( ) ) ;
/*
* We want to make sure the list registers start out clear so that we
* only have the program the used registers .
*/
if ( kvm_vgic_global_state . type = = VGIC_V2 )
vgic_v2_init_lrs ( ) ;
else
kvm_call_hyp ( __vgic_v3_init_lrs ) ;
}
2015-12-01 15:02:35 +01:00
/**
* kvm_vgic_hyp_init : populates the kvm_vgic_global_state variable
* according to the host GIC model . Accordingly calls either
* vgic_v2 / v3_probe which registers the KVM_DEVICE that can be
* instantiated by a guest later on .
*/
int kvm_vgic_hyp_init ( void )
{
const struct gic_kvm_info * gic_kvm_info ;
int ret ;
gic_kvm_info = gic_get_kvm_info ( ) ;
if ( ! gic_kvm_info )
return - ENODEV ;
if ( ! gic_kvm_info - > maint_irq ) {
kvm_err ( " No vgic maintenance irq \n " ) ;
return - ENXIO ;
}
switch ( gic_kvm_info - > type ) {
case GIC_V2 :
ret = vgic_v2_probe ( gic_kvm_info ) ;
break ;
case GIC_V3 :
ret = vgic_v3_probe ( gic_kvm_info ) ;
2016-09-12 15:49:15 +01:00
if ( ! ret ) {
static_branch_enable ( & kvm_vgic_global_state . gicv3_cpuif ) ;
kvm_info ( " GIC system register CPU interface enabled \n " ) ;
}
2015-12-01 15:02:35 +01:00
break ;
default :
ret = - ENODEV ;
2019-08-25 10:44:17 +01:00
}
2015-12-01 15:02:35 +01:00
if ( ret )
return ret ;
kvm_vgic_global_state . maint_irq = gic_kvm_info - > maint_irq ;
ret = request_percpu_irq ( kvm_vgic_global_state . maint_irq ,
vgic_maintenance_handler ,
" vgic " , kvm_get_running_vcpus ( ) ) ;
if ( ret ) {
kvm_err ( " Cannot register interrupt %d \n " ,
kvm_vgic_global_state . maint_irq ) ;
return ret ;
}
2016-07-13 17:17:02 +00:00
ret = cpuhp_setup_state ( CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING ,
2016-12-21 20:19:54 +01:00
" kvm/arm/vgic:starting " ,
2016-07-13 17:17:02 +00:00
vgic_init_cpu_starting , vgic_init_cpu_dying ) ;
2015-12-01 15:02:35 +01:00
if ( ret ) {
kvm_err ( " Cannot register vgic CPU notifier \n " ) ;
goto out_free_irq ;
}
kvm_info ( " vgic interrupt IRQ%d \n " , kvm_vgic_global_state . maint_irq ) ;
return 0 ;
out_free_irq :
free_percpu_irq ( kvm_vgic_global_state . maint_irq ,
kvm_get_running_vcpus ( ) ) ;
return ret ;
}