Skip to content

Commit 43e324d

Browse files
Merge pull request #948 from stefano-garzarella/probe-check-fwcfg
virtio/mmio: move fw_cfg check into probe_mmio_slots()
2 parents 9099275 + bab0179 commit 43e324d

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

kernel/src/svsm.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use svsm::svsm_paging::{
5757
use svsm::task::{KernelThreadStartInfo, schedule_init, start_kernel_task};
5858
use svsm::types::PAGE_SIZE;
5959
use svsm::utils::{MemoryRegion, ScopedRef, round_to_pages};
60-
#[cfg(feature = "virtio-drivers")]
60+
#[cfg(all(feature = "virtio-drivers", feature = "block"))]
6161
use svsm::virtio::probe_mmio_slots;
6262
#[cfg(all(feature = "vtpm", not(test)))]
6363
use svsm::vtpm::vtpm_init;
@@ -162,12 +162,12 @@ fn mapping_info_init(launch_info: &KernelLaunchInfo) {
162162
/// Returns Ok if initialization is successful or no virtio devices are found
163163
/// Returns an error when a virtio device is found but its driver initialization fails.
164164
#[cfg(feature = "virtio-drivers")]
165-
fn initialize_virtio_mmio() -> Result<(), SvsmError> {
166-
let mut slots = probe_mmio_slots();
167-
165+
fn initialize_virtio_mmio(_config: &SvsmConfig<'_>) -> Result<(), SvsmError> {
168166
#[cfg(feature = "block")]
169167
{
170168
use svsm::block::virtio_blk::initialize_block;
169+
170+
let mut slots = probe_mmio_slots(_config);
171171
initialize_block(&mut slots)?;
172172
}
173173

@@ -435,11 +435,7 @@ fn svsm_init(launch_info: &KernelLaunchInfo) {
435435
virt_log_usage();
436436

437437
#[cfg(feature = "virtio-drivers")]
438-
if config.has_fw_cfg_port() {
439-
// Virtio cannot exist if there is no fw_cfg, so do not bother to
440-
// attempt initialization if it is not present.
441-
initialize_virtio_mmio().expect("Failed to initialize virtio-mmio drivers");
442-
}
438+
initialize_virtio_mmio(&config).expect("Failed to initialize virtio-mmio drivers");
443439

444440
if let Err(e) = SVSM_PLATFORM.launch_fw(&config) {
445441
panic!("Failed to launch FW: {e:?}");

kernel/src/virtio/mmio.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use virtio_drivers::transport::{DeviceType, Transport, mmio::MmioTransport};
1212

1313
use crate::{
1414
address::PhysAddr,
15+
config::SvsmConfig,
1516
fw_cfg::FwCfg,
1617
mm::{GlobalRangeGuard, map_global_range_4k_shared, pagetable::PTEntryFlags},
1718
platform::SVSM_PLATFORM,
@@ -25,7 +26,7 @@ pub struct MmioSlot {
2526
pub transport: MmioTransport<SvsmHal>,
2627
}
2728

28-
#[derive(Debug)]
29+
#[derive(Debug, Default)]
2930
pub struct MmioSlots {
3031
slots: Vec<MmioSlot>,
3132
}
@@ -47,14 +48,18 @@ pub struct MmioSlots {
4748
/// Returns an [`MmioSlots`] collection containing all discovered virtio-MMIO devices.
4849
/// Returns an empty collection if no devices are found or if the fw_cfg interface
4950
/// is unavailable.
50-
pub fn probe_mmio_slots() -> MmioSlots {
51+
pub fn probe_mmio_slots(config: &SvsmConfig<'_>) -> MmioSlots {
52+
// Virtio MMIO addresses are discovered via fw_cfg, so skip probing
53+
// if it is not present.
54+
if !config.has_fw_cfg_port() {
55+
return MmioSlots::default();
56+
}
57+
5158
virtio_init();
5259

5360
let cfg = FwCfg::new(SVSM_PLATFORM.get_io_port());
5461
let Ok(dev) = cfg.get_virtio_mmio_addresses() else {
55-
return MmioSlots {
56-
slots: Vec::<MmioSlot>::new(),
57-
};
62+
return MmioSlots::default();
5863
};
5964

6065
let mut slots = Vec::with_capacity(dev.len());

0 commit comments

Comments
 (0)