Skip to content

Commit

Permalink
config: add MMIO layout
Browse files Browse the repository at this point in the history
The new configuration file contains the address and size of MMIO space and
PCIE config space.

Signed-off-by: Jiaqi Gao <jiaqi.gao@intel.com>
  • Loading branch information
gaojiaqi7 committed Feb 17, 2025
1 parent 5a5ce31 commit b10cdbc
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 79 deletions.
59 changes: 7 additions & 52 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/mmio_layout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Mmio32Start": "0xC0000000",
"Mmio32Size": "0x20000000",
"PcieConfigBaseAddress": "0xE0000000",
"PcieConfigSize": "0x10000000",
"Mmio64Start": "0x100000000",
"Mmio64Size": "0x40000000"
}
1 change: 1 addition & 0 deletions src/devices/pci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lazy_static = { version = "1.0", features = ["spin_no_std"] }
log = "0.4.13"
spin = "0.9"
tdx-tdcall = { path = "../../../deps/td-shim/tdx-tdcall", optional = true }
td-payload = { path = "../../../deps/td-shim/td-payload" }
x86 = { version = "0.47.0", optional = true}

[features]
Expand Down
3 changes: 1 addition & 2 deletions src/devices/pci/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use bitflags::bitflags;
use core::convert::From;

use crate::mmio::{alloc_mmio32, alloc_mmio64};
use crate::{PciCommand, PciError, Result};
use crate::{PciCommand, PciError, Result, PCI_EX_BAR_BASE_ADDRESS};

pub const PCI_CONFIGURATION_ADDRESS_PORT: u16 = 0xCF8;
pub const PCI_CONFIGURATION_DATA_PORT: u16 = 0xCFC;
const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000u64;
const PCI_MEM32_BASE_ADDRESS_MASK: u32 = 0xFFFF_FFF0;
const PCI_MEM64_BASE_ADDRESS_MASK: u64 = 0xFFFF_FFFF_FFFF_FFF0;

Expand Down
10 changes: 10 additions & 0 deletions src/devices/pci/src/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2024 Intel Corporation
//
// SPDX-License-Identifier: BSD-2-Clause-Patent

pub const MMIO32_START: u32 = 0xC0000000;
pub const MMIO32_SIZE: u32 = 0x20000000;
pub const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000;
pub const PCI_EX_BAR_SIZE: u64 = 0x10000000;
pub const MMIO64_START: u64 = 0x100000000;
pub const MMIO64_SIZE: u64 = 0x40000000;
2 changes: 2 additions & 0 deletions src/devices/pci/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

mod config;
mod consts;
mod layout;
mod mmio;
pub use config::*;
pub use consts::*;
pub use layout::*;
pub use mmio::*;

#[cfg(feature = "fuzz")]
Expand Down
34 changes: 21 additions & 13 deletions src/devices/pci/src/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

use lazy_static::lazy_static;
use spin::Mutex;
use td_payload::mm::MEMORY_MAP;

use crate::{PciError, Result};

pub const MMIO32_START: u32 = 0xC000_0000;
pub const MMIO32_SIZE: u32 = 0x2000_0000;
use crate::{PciError, Result, MMIO32_SIZE, MMIO32_START, MMIO64_SIZE, MMIO64_START};

lazy_static! {
static ref MMIO32: Mutex<u32> = Mutex::new(0);
Expand All @@ -20,16 +18,24 @@ lazy_static! {
static ref MMIO_OFFSET: Mutex<u64> = Mutex::new(0);
}

pub fn init_mmio(end_of_ram: u64) {
*MMIO32.lock() = MMIO32_START;

let mmio64_start = if end_of_ram > u32::MAX as u64 {
end_of_ram
} else {
u32::MAX as u64 + 1
};
pub fn init_mmio() {
let memory_map = MEMORY_MAP.lock();

// Iterate through each region in the memory map and check if it overlaps with the MMIO32 or MMIO64 space.
// If an overlap is detected, panic with an error message indicating an invalid MMIO configuration.
// This ensures that the MMIO space does not conflict with the RAM space.
for region in memory_map.iter() {
if (region.addr < (MMIO32_START + MMIO32_SIZE) as u64
&& region.addr + region.size > MMIO32_START as u64)
|| (region.addr < MMIO64_START + MMIO64_SIZE
&& region.addr + region.size > MMIO64_START)
{
panic!("Invalid MMIO configuration: MMIO space overlaps with the RAM space.");
}
}

*MMIO64.lock() = mmio64_start;
*MMIO32.lock() = MMIO32_START;
*MMIO64.lock() = MMIO64_START;
}

#[cfg(feature = "fuzz")]
Expand All @@ -45,6 +51,8 @@ pub fn alloc_mmio32(size: u32) -> Result<u32> {

#[cfg(not(feature = "fuzz"))]
pub fn alloc_mmio32(size: u32) -> Result<u32> {
use crate::MMIO32_SIZE;

let cur = *MMIO32.lock();
let addr = align_up(cur as u64, size as u64).ok_or(PciError::InvalidParameter)?;

Expand Down
9 changes: 4 additions & 5 deletions src/migtd/src/driver/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use alloc::boxed::Box;
use core::sync::atomic::AtomicBool;
use pci::PCI_EX_BAR_BASE_ADDRESS;
use td_payload::mm::shared::{alloc_shared_pages, free_shared_pages};
use virtio_serial::*;

Expand Down Expand Up @@ -48,12 +49,12 @@ impl Timer for SerailTimer {
}
}

// #[cfg(feature = "virtio-serial")]
pub fn virtio_serial_device_init(end_of_ram: u64) {
#[cfg(feature = "virtio-serial")]
pub fn virtio_serial_device_init() {
pci_ex_bar_initialization();

// Initialize MMIO space
pci::init_mmio(end_of_ram);
pci::init_mmio();

// Enumerate the virtio device
let (_b, dev, _f) = pci::find_device(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID).unwrap();
Expand All @@ -74,8 +75,6 @@ pub fn virtio_serial_device_init(end_of_ram: u64) {
}

pub fn pci_ex_bar_initialization() {
const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000u64;

// PcdPciExpressBaseAddress TBD
let pci_exbar_base = PCI_EX_BAR_BASE_ADDRESS;

Expand Down
6 changes: 3 additions & 3 deletions src/migtd/src/driver/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ pub fn vmcall_vsock_device_init(mid: u64, cid: u64) {
}

#[cfg(feature = "virtio-vsock")]
pub fn virtio_vsock_device_init(end_of_ram: u64) {
pub fn virtio_vsock_device_init() {
pci_ex_bar_initialization();

// Initialize MMIO space
pci::init_mmio(end_of_ram);
pci::init_mmio();

// Enumerate the virtio device
let (_b, dev, _f) = pci::find_device(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID).unwrap();
Expand All @@ -95,7 +95,7 @@ pub fn virtio_vsock_device_init(end_of_ram: u64) {

#[cfg(feature = "virtio-vsock")]
pub fn pci_ex_bar_initialization() {
const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000u64;
use pci::PCI_EX_BAR_BASE_ADDRESS;

// PcdPciExpressBaseAddress TBD
let pci_exbar_base = PCI_EX_BAR_BASE_ADDRESS;
Expand Down
6 changes: 2 additions & 4 deletions src/migtd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub mod ratls;
#[cfg(target_os = "none")]
pub extern "C" fn _start(hob: u64, payload: u64) -> ! {
use td_payload::arch;
#[cfg(any(feature = "virtio-serial", feature = "virtio-vsock"))]
use td_payload::mm::end_of_ram;
use td_payload::mm::layout::*;

const STACK_SIZE: usize = 0x1_0000;
Expand Down Expand Up @@ -57,11 +55,11 @@ pub extern "C" fn _start(hob: u64, payload: u64) -> ! {
driver::timer::init_timer();

#[cfg(feature = "virtio-serial")]
driver::serial::virtio_serial_device_init(end_of_ram() as u64);
driver::serial::virtio_serial_device_init();

// Init the vsock-virtio device
#[cfg(feature = "virtio-vsock")]
driver::vsock::virtio_vsock_device_init(end_of_ram() as u64);
driver::vsock::virtio_vsock_device_init();

arch::init::init(&layout, main);
}
Expand Down
2 changes: 2 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ clap = { version = "4.0", features = ["derive"] }
xshell = "0.2"
lazy_static = "1.4.0"
anyhow = "1.0"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
Loading

0 comments on commit b10cdbc

Please sign in to comment.