Skip to content

Commit

Permalink
GIC: Moving version specific bits in the correct place
Browse files Browse the repository at this point in the history
We move version-specific methods and constants inside the impl block
  • Loading branch information
bchalios committed Oct 30, 2019
1 parent be7012c commit 0e5fa62
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 63 deletions.
62 changes: 31 additions & 31 deletions arch/src/aarch64/gicv2.rs
Expand Up @@ -9,31 +9,6 @@ use super::gic::{Error, GICDevice};

type Result<T> = result::Result<T, Error>;

// Unfortunately bindgen omits defines that are based on other defines.
// See arch/arm64/include/uapi/asm/kvm.h file from the linux kernel.
const KVM_VGIC_V2_DIST_SIZE: u64 = 0x1000;
const KVM_VGIC_V2_CPU_SIZE: u64 = 0x2000;

/// Get the address of the GICv2 distributor.
const fn get_dist_addr() -> u64 {
super::layout::MAPPED_IO_START - KVM_VGIC_V2_DIST_SIZE
}

/// Get the size of the GIC_v2 distributor.
const fn get_dist_size() -> u64 {
KVM_VGIC_V2_DIST_SIZE
}

/// Get the address of the GIC_v2 CPU.
const fn get_cpu_addr() -> u64 {
get_dist_addr() - KVM_VGIC_V2_CPU_SIZE
}

/// Get the size of the GIC_v2 CPU.
const fn get_cpu_size() -> u64 {
KVM_VGIC_V2_CPU_SIZE
}

/// Represent a GIC v2 device
pub struct GICv2 {
/// The file descriptor for the KVM device
Expand All @@ -44,6 +19,31 @@ pub struct GICv2 {
}

impl GICv2 {
// Unfortunately bindgen omits defines that are based on other defines.
// See arch/arm64/include/uapi/asm/kvm.h file from the linux kernel.
const KVM_VGIC_V2_DIST_SIZE: u64 = 0x1000;
const KVM_VGIC_V2_CPU_SIZE: u64 = 0x2000;

/// Get the address of the GICv2 distributor.
const fn get_dist_addr() -> u64 {
super::layout::MAPPED_IO_START - GICv2::KVM_VGIC_V2_DIST_SIZE
}

/// Get the size of the GIC_v2 distributor.
const fn get_dist_size() -> u64 {
GICv2::KVM_VGIC_V2_DIST_SIZE
}

/// Get the address of the GIC_v2 CPU.
const fn get_cpu_addr() -> u64 {
GICv2::get_dist_addr() - GICv2::KVM_VGIC_V2_CPU_SIZE
}

/// Get the size of the GIC_v2 CPU.
const fn get_cpu_size() -> u64 {
GICv2::KVM_VGIC_V2_CPU_SIZE
}

pub fn new(vm: &VmFd) -> Result<Box<dyn GICDevice>> {
let vgic_fd =
Self::init_device(vm, kvm_bindings::kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2)?;
Expand All @@ -54,7 +54,7 @@ impl GICv2 {
&vgic_fd,
kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR,
u64::from(kvm_bindings::KVM_VGIC_V2_ADDR_TYPE_DIST),
&get_dist_addr() as *const u64 as u64,
&GICv2::get_dist_addr() as *const u64 as u64,
0,
)?;

Expand All @@ -63,7 +63,7 @@ impl GICv2 {
&vgic_fd,
kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR,
u64::from(kvm_bindings::KVM_VGIC_V2_ADDR_TYPE_CPU),
&get_cpu_addr() as *const u64 as u64,
&GICv2::get_cpu_addr() as *const u64 as u64,
0,
)?;

Expand All @@ -72,10 +72,10 @@ impl GICv2 {
let vgic_device = GICv2 {
fd: vgic_fd,
properties: [
get_dist_addr(),
get_dist_size(),
get_cpu_addr(),
get_cpu_size(),
GICv2::get_dist_addr(),
GICv2::get_dist_size(),
GICv2::get_cpu_addr(),
GICv2::get_cpu_size(),
],
};

Expand Down
64 changes: 32 additions & 32 deletions arch/src/aarch64/gicv3.rs
Expand Up @@ -9,32 +9,6 @@ use super::gic::{Error, GICDevice};

type Result<T> = result::Result<T, Error>;

// Unfortunately bindgen omits defines that are based on other defines.
// See arch/arm64/include/uapi/asm/kvm.h file from the linux kernel.
const SZ_64K: u64 = 0x0001_0000;
const KVM_VGIC_V3_DIST_SIZE: u64 = SZ_64K;
const KVM_VGIC_V3_REDIST_SIZE: u64 = (2 * SZ_64K);

/// Get the address of the GIC distributor.
fn get_dist_addr() -> u64 {
super::layout::MAPPED_IO_START - KVM_VGIC_V3_DIST_SIZE
}

/// Get the size of the GIC distributor.
fn get_dist_size() -> u64 {
KVM_VGIC_V3_DIST_SIZE
}

/// Get the address of the GIC redistributors.
fn get_redists_addr(vcpu_count: u64) -> u64 {
get_dist_addr() - get_redists_size(vcpu_count)
}

/// Get the size of the GIC redistributors.
fn get_redists_size(vcpu_count: u64) -> u64 {
vcpu_count * KVM_VGIC_V3_REDIST_SIZE
}

pub struct GICv3 {
/// The file descriptor for the KVM device
fd: DeviceFd,
Expand All @@ -44,6 +18,32 @@ pub struct GICv3 {
}

impl GICv3 {
// Unfortunately bindgen omits defines that are based on other defines.
// See arch/arm64/include/uapi/asm/kvm.h file from the linux kernel.
const SZ_64K: u64 = 0x0001_0000;
const KVM_VGIC_V3_DIST_SIZE: u64 = GICv3::SZ_64K;
const KVM_VGIC_V3_REDIST_SIZE: u64 = (2 * GICv3::SZ_64K);

/// Get the address of the GIC distributor.
fn get_dist_addr() -> u64 {
super::layout::MAPPED_IO_START - GICv3::KVM_VGIC_V3_DIST_SIZE
}

/// Get the size of the GIC distributor.
fn get_dist_size() -> u64 {
GICv3::KVM_VGIC_V3_DIST_SIZE
}

/// Get the address of the GIC redistributors.
fn get_redists_addr(vcpu_count: u64) -> u64 {
GICv3::get_dist_addr() - GICv3::get_redists_size(vcpu_count)
}

/// Get the size of the GIC redistributors.
fn get_redists_size(vcpu_count: u64) -> u64 {
vcpu_count * GICv3::KVM_VGIC_V3_REDIST_SIZE
}

pub fn new(vm: &VmFd, vcpu_count: u64) -> Result<Box<dyn GICDevice>> {
/* We are creating a V3 GIC.
As per https://static.docs.arm.com/dai0492/b/GICv3_Software_Overview_Official_Release_B.pdf,
Expand All @@ -62,7 +62,7 @@ impl GICv3 {
&vgic_fd,
kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR,
u64::from(kvm_bindings::KVM_VGIC_V3_ADDR_TYPE_DIST),
&get_dist_addr() as *const u64 as u64,
&GICv3::get_dist_addr() as *const u64 as u64,
0,
)?;

Expand All @@ -73,7 +73,7 @@ impl GICv3 {
&vgic_fd,
kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR,
u64::from(kvm_bindings::KVM_VGIC_V3_ADDR_TYPE_REDIST),
&get_redists_addr(u64::from(vcpu_count)) as *const u64 as u64,
&GICv3::get_redists_addr(u64::from(vcpu_count)) as *const u64 as u64,
0,
)?;

Expand All @@ -82,10 +82,10 @@ impl GICv3 {
let vgic_device = GICv3 {
fd: vgic_fd,
properties: [
get_dist_addr(),
get_dist_size(),
get_redists_addr(vcpu_count),
get_redists_size(vcpu_count),
GICv3::get_dist_addr(),
GICv3::get_dist_size(),
GICv3::get_redists_addr(vcpu_count),
GICv3::get_redists_size(vcpu_count),
],
};

Expand Down

0 comments on commit 0e5fa62

Please sign in to comment.