Skip to content

Commit 3767982

Browse files
committed
feat: 更新依赖项,添加 Ioremap 结构及其 MMIO 操作实现,优化 VDeviceList 设备调用逻辑
1 parent 9829723 commit 3767982

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,43 @@ timer_list = "0.1"
2121
axerrno = "0.1.0"
2222
bitmap-allocator = "0.2.1"
2323
cpumask = "0.1.0"
24+
derive_more = {version = "2", default-features = false, features = ["full"]}
2425
kspin = "0.1"
2526
memory_addr = "0.4"
2627
page_table_entry = {version = "0.5", features = ["arm-el2"]}
2728
page_table_multiarch = "0.5"
2829
vm-allocator.workspace = true
29-
derive_more = { version = "2", default-features = false, features = ["full"] }
30+
rdif-intc = "0.12"
3031

3132
# System dependent modules provided by ArceOS-Hypervisor.
3233
axaddrspace = "0.2"
3334

3435
axhal.workspace = true
36+
axklib.workspace = true
3537
axruntime.workspace = true
3638
axstd.workspace = true
3739
axvm-types.workspace = true
3840
axvmconfig = {version = "0.1", default-features = false}
3941
fdt-edit = "0.1"
42+
mmio-api = "0.1"
43+
rdrive.workspace = true
4044

41-
axvdev = { version = "0.1" }
45+
axvdev = {version = "0.1"}
4246

43-
ringbuf = { version = "0.4", default-features = false }
47+
ringbuf = {version = "0.4", default-features = false}
4448

4549
[target.'cfg(target_arch = "x86_64")'.dependencies]
50+
axplat-x86-qemu-q35.workspace = true
4651
raw-cpuid = "11"
4752
x86_vcpu = "0.1"
48-
axplat-x86-qemu-q35.workspace = true
4953

5054
[target.'cfg(target_arch = "riscv64")'.dependencies]
5155
# riscv_vcpu = "0.1"
5256

5357
[target.'cfg(target_arch = "aarch64")'.dependencies]
5458
aarch64-cpu = "11.0"
5559
aarch64-cpu-ext = "0.1"
60+
arm-gic-driver = {version = "0.15", features = ["rdif"]}
5661
arm_vcpu = "0.3"
5762
arm_vgic = {version = "0.3"}
5863

src/arch/aarch64/plat.rs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use arm_vgic::v3;
1+
use arm_gic_driver::IntId;
2+
use arm_vgic::{IrqChipOp, v3};
3+
use axvdev::IrqNum;
4+
use rdif_intc::Intc;
25

3-
use crate::{fdt::fdt_edit, vdev::VDeviceList};
6+
use crate::{fdt::fdt_edit, hal::Ioremap, vdev::VDeviceList};
47

58
pub struct PlatData {
69
vdev: VDeviceList,
@@ -34,22 +37,52 @@ impl PlatData {
3437
let gicd = regs.get(0).ok_or(anyhow!("No GICD reg"))?;
3538
let gicr = regs.get(1).ok_or(anyhow!("No GICR reg"))?;
3639
let plat = self.vdev.new_plat();
37-
let mut gic = v3::VGic::new();
3840

39-
// self.vdev.add_device(|plat| {
40-
// let gic = v3::VGic::new(
41-
// (gicd.address as usize).into(),
42-
// (gicr.address as usize).into(),
43-
// plat.clone(),
44-
// );
45-
// Ok(gic)
46-
// });
41+
let config = arm_vgic::VGicConfig::new(crate::hal::cpu::count(), &Ioremap, IrqOp {});
42+
43+
let mut gic = v3::VGic::new(config);
4744

4845
self.vdev.add_device(|plat| {
49-
let gicd = gic.new_gicd(plat.clone(), (gicd.address as usize).into());
46+
let gicd = gic.build_gicd(plat.clone(), (gicd.address as usize).into());
5047
Ok(gicd)
5148
})?;
5249

5350
Ok(())
5451
}
5552
}
53+
54+
fn with_gicv3<F, R>(f: F) -> R
55+
where
56+
F: FnOnce(&mut arm_gic_driver::v3::Gic) -> R,
57+
{
58+
let mut g = rdrive::get_one::<Intc>().unwrap().lock().unwrap();
59+
let gic = g
60+
.typed_mut::<arm_gic_driver::v3::Gic>()
61+
.expect("GIC is not GICv3");
62+
f(gic)
63+
}
64+
65+
fn covnert_irq(irq: IrqNum) -> IntId {
66+
let id: usize = irq.into();
67+
unsafe { IntId::raw(id as _) }
68+
}
69+
70+
struct IrqOp {}
71+
72+
impl IrqChipOp for IrqOp {
73+
fn get_cfg(&self, irq: IrqNum) -> arm_vgic::Trigger {
74+
let res = with_gicv3(|gic| gic.get_cfg(covnert_irq(irq)));
75+
match res {
76+
arm_gic_driver::v3::Trigger::Level => arm_vgic::Trigger::Level,
77+
arm_gic_driver::v3::Trigger::Edge => arm_vgic::Trigger::Edge,
78+
}
79+
}
80+
81+
fn set_cfg(&self, irq: IrqNum, cfg: arm_vgic::Trigger) {
82+
let t = match cfg {
83+
arm_vgic::Trigger::Level => arm_gic_driver::v3::Trigger::Level,
84+
arm_vgic::Trigger::Edge => arm_gic_driver::v3::Trigger::Edge,
85+
};
86+
with_gicv3(|gic| gic.set_cfg(covnert_irq(irq), t));
87+
}
88+
}

src/hal/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use alloc::vec::Vec;
22
use axvdev::VDeviceManager;
33
use bitmap_allocator::BitAlloc;
4-
use core::sync::atomic::{AtomicUsize, Ordering};
4+
use core::{
5+
ptr::NonNull,
6+
sync::atomic::{AtomicUsize, Ordering},
7+
};
8+
use mmio_api::{Mmio, MmioOp};
59
use std::{
610
os::arceos::{api::task::AxCpuMask, modules::axtask::set_current_affinity},
711
thread::yield_now,
@@ -94,3 +98,21 @@ pub fn virt_to_phys(vaddr: HostVirtAddr) -> HostPhysAddr {
9498
.as_usize()
9599
.into()
96100
}
101+
102+
pub struct Ioremap;
103+
104+
impl MmioOp for Ioremap {
105+
fn ioremap(
106+
&self,
107+
addr: mmio_api::MmioAddr,
108+
size: usize,
109+
) -> Result<mmio_api::Mmio, anyhow::Error> {
110+
let paddr = addr.as_usize();
111+
let vaddr = axklib::mem::iomap(paddr.into(), size).unwrap();
112+
Ok(unsafe { Mmio::new(addr, NonNull::new_unchecked(vaddr.as_mut_ptr()), size) })
113+
}
114+
115+
fn iounmap(&self, mmio: &mmio_api::Mmio) {
116+
todo!()
117+
}
118+
}

src/vm/vdev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl VDeviceList {
147147
}
148148

149149
fn device_try_invoke(&self, id: u32) {
150-
self.get_device(*id).unwrap().try_invoke();
150+
self.get_device(id).unwrap().try_invoke();
151151
}
152152
}
153153

0 commit comments

Comments
 (0)