| // Copyright 2021 The Fuchsia Authors | |
| // | |
| // Use of this source code is governed by a MIT-style | |
| // license that can be found in the LICENSE file or at | |
| // https://opensource.org/licenses/MIT | |
| //! Dispatch of the physboot driver handoff to the platform's drivers. | |
| use crate::amlogic_rng::amlogic_rng_init; | |
| use crate::amlogic_s912_hdcp::amlogic_s912_hdcp_init; | |
| use crate::arm_generic_timer::{arm_generic_timer_init, arm_generic_timer_init_post_vm}; | |
| use crate::arm_gicv2::{arm_gicv2_init_early, arm_gicv2_init_late, arm_gicv2_init_post_vm}; | |
| use crate::arm_gicv3::{arm_gicv3_init_early, arm_gicv3_init_late, arm_gicv3_init_post_vm}; | |
| use crate::arm_smmu; | |
| use crate::armv7_mmio_timer::Armv7MmioTimer; | |
| use crate::generic32::{ | |
| generic_32bit_watchdog_early_init, generic_32bit_watchdog_init_post_vm, | |
| generic_32bit_watchdog_late_init, | |
| }; | |
| use crate::power_iris::{iris_power_init, iris_power_init_early}; | |
| use crate::power_moonflower::{moonflower_power_init, moonflower_power_init_early}; | |
| use crate::power_motmot::motmot_power_init_early; | |
| use crate::psci::psci_init; | |
| use crate::qcom_rng::qcom_rng_init; | |
| use handoff::{ArchPhysHandoff, ArmGicDriver, Optional}; | |
| /// Early driver initialization from the handoff: GIC, generic 32-bit watchdog, ARM generic timer, | |
| /// PSCI, and platform power drivers, each only if physboot handed off a configuration for it. | |
| /// | |
| /// # Safety | |
| /// | |
| /// Must be called once during single-threaded early boot, on the boot CPU with interrupts disabled, | |
| /// before secondary CPUs are started, with the valid physboot `arch_handoff` while temporary | |
| /// handoff memory remains mapped. | |
| pub unsafe fn platform_driver_handoff_early(arch_handoff: &ArchPhysHandoff) { | |
| // Configure the GIC first so that the remaining drivers can freely register | |
| // interrupt handlers. | |
| match arch_handoff.gic_driver() { | |
| Some(ArmGicDriver::V2(config)) => { | |
| // SAFETY: The caller upholds the one-time early single-threaded boot contract. | |
| unsafe { arm_gicv2_init_early(config) }; | |
| } | |
| Some(ArmGicDriver::V3(config)) => { | |
| // SAFETY: The caller upholds the one-time early single-threaded boot contract. | |
| unsafe { arm_gicv3_init_early(config) }; | |
| } | |
| None => {} | |
| } | |
| if let Optional::Some(config) = &arch_handoff.generic32_watchdog_driver { | |
| // SAFETY: The caller upholds the one-time single-threaded early boot contract. | |
| unsafe { generic_32bit_watchdog_early_init(config) }; | |
| } | |
| if let Optional::Some(config) = &arch_handoff.generic_timer_driver { | |
| // SAFETY: The caller upholds the one-time boot-CPU contract with interrupts disabled. | |
| unsafe { arm_generic_timer_init(config) }; | |
| } | |
| // Initialize psci before the other power drivers, as they may decide to | |
| // override the psci's power registration. | |
| if let Optional::Some(config) = &arch_handoff.psci_driver { | |
| // SAFETY: The caller guarantees `arch_handoff` is valid and temporary handoff memory | |
| // remains mapped. | |
| let suspend_states = unsafe { arch_handoff.psci_cpu_suspend_driver() }; | |
| // SAFETY: The caller upholds the one-time boot-CPU contract with interrupts disabled. | |
| unsafe { psci_init(config, suspend_states) }; | |
| } | |
| if arch_handoff.motmot_power_driver { | |
| // SAFETY: `arch_handoff` is the valid physboot handoff for this hardware, so | |
| // `motmot_power_driver` is set only on Motmot hardware where EL3 implements | |
| // `SMC_CMD_PRIV_REG`. | |
| unsafe { motmot_power_init_early() }; | |
| } | |
| if arch_handoff.moonflower_power_driver { | |
| // SAFETY: `arch_handoff` is the valid physboot handoff for this hardware, so | |
| // `moonflower_power_driver` is set only on Moonflower hardware where EL3 implements the | |
| // Qualcomm TZ SiP calls and `OPP_REG_PHYS_BASE` maps the OPP register block. | |
| unsafe { moonflower_power_init_early() }; | |
| } | |
| // SAFETY: The caller guarantees `arch_handoff` is valid and temporary handoff memory remains | |
| // mapped. | |
| if !unsafe { arch_handoff.iris_power_driver() }.is_empty() { | |
| iris_power_init_early(); | |
| } | |
| } | |
| /// Post-VM driver initialization from the handoff: GIC, ARM generic timer, and generic 32-bit | |
| /// watchdog, each only if configured. | |
| /// | |
| /// # Safety | |
| /// | |
| /// Must be called once during post-VM boot, on the boot CPU after | |
| /// [`platform_driver_handoff_early`] has run with the same valid physboot `arch_handoff` and | |
| /// after the VM system is ready, before secondary CPUs are brought up, and before any `gic_*` | |
| /// hardware interface calls run. | |
| pub unsafe fn platform_driver_handoff_post_vm(arch_handoff: &ArchPhysHandoff) { | |
| // Initialize the GIC post VM so it can map its own mmio registers | |
| match arch_handoff.gic_driver() { | |
| Some(ArmGicDriver::V2(config)) => { | |
| // SAFETY: The caller upholds the one-time post-VM boot contract after | |
| // `platform_driver_handoff_early` (which initialized GICv2 early, with no prior GICv3 | |
| // initialization) and before secondary CPUs or any `gic_*` hardware interface calls | |
| // run. | |
| unsafe { arm_gicv2_init_post_vm(config) }; | |
| } | |
| Some(ArmGicDriver::V3(config)) => { | |
| // SAFETY: The caller upholds the one-time post-VM boot contract after | |
| // `platform_driver_handoff_early` (which initialized GICv3 early, with no prior GICv2 | |
| // initialization) and before secondary CPUs or any `gic_*` hardware interface calls | |
| // run. | |
| unsafe { arm_gicv3_init_post_vm(config) }; | |
| } | |
| None => {} | |
| } | |
| if let Optional::Some(config) = &arch_handoff.generic_timer_driver { | |
| arm_generic_timer_init_post_vm(config); | |
| } | |
| if let Optional::Some(config) = &arch_handoff.generic32_watchdog_driver { | |
| generic_32bit_watchdog_init_post_vm(config); | |
| } | |
| } | |
| /// Late driver initialization from the handoff: GIC, ARMv7 MMIO timer, ARM SMMU, Amlogic HDCP, | |
| /// hardware RNGs, generic 32-bit watchdog, and platform power domains, each only if configured. | |
| /// | |
| /// # Safety | |
| /// | |
| /// Must be called once during late boot, after [`platform_driver_handoff_early`] and | |
| /// [`platform_driver_handoff_post_vm`] have run with the same valid physboot `arch_handoff`, after | |
| /// resource-filter initialization, while temporary handoff memory remains mapped. The caller must | |
| /// also ensure: | |
| /// - Physical peripheral addresses supplied in `arch_handoff` correspond to valid hardware | |
| /// register blocks in the peripheral memory map. | |
| /// - This call is ordered with respect to any `hw_rng_*` calls as required by `hw_rng_register` | |
| /// (no concurrent `hw_rng_*` calls; happens-before both ways). | |
| pub unsafe fn platform_driver_handoff_late(arch_handoff: &ArchPhysHandoff) { | |
| // First, as above. | |
| match arch_handoff.gic_driver() { | |
| Some(ArmGicDriver::V2(config)) => { | |
| // SAFETY: Caller guarantees `platform_driver_handoff_early` ran with the same | |
| // `arch_handoff`, so early GICv2 initialization completed. | |
| unsafe { arm_gicv2_init_late(config) }; | |
| } | |
| Some(ArmGicDriver::V3(config)) => { | |
| // SAFETY: Caller guarantees `platform_driver_handoff_early` ran with the same | |
| // `arch_handoff`, so early GICv3 initialization completed. | |
| unsafe { arm_gicv3_init_late(config) }; | |
| } | |
| None => {} | |
| } | |
| // TODO(johngro): we are currently initing the mmio timers rather late in the | |
| // boot because we currently use the heap in the driver. Consider moving to | |
| // static allocation so we can init early instead. | |
| if let Optional::Some(config) = &arch_handoff.generic_timer_mmio_driver { | |
| Armv7MmioTimer::init(config); | |
| } | |
| // SAFETY: The caller guarantees `arch_handoff` is valid and temporary handoff memory remains | |
| // mapped. | |
| arm_smmu::init(unsafe { arch_handoff.arm_smmu_drivers() }); | |
| if let Optional::Some(config) = &arch_handoff.amlogic_hdcp_driver { | |
| // SAFETY: Caller upholds the late boot contract with the valid physboot `arch_handoff` | |
| // for this hardware, so `amlogic_hdcp_driver` is present only on Amlogic S912 hardware | |
| // (where EL3 implements SiP SMC `0x82000012`) with valid peripheral addresses in the | |
| // peripheral memory map. | |
| unsafe { amlogic_s912_hdcp_init(config) }; | |
| } | |
| if let Optional::Some(config) = &arch_handoff.amlogic_rng_driver { | |
| // SAFETY: Caller upholds the one-time late boot contract, `hw_rng_*` ordering | |
| // preconditions, and valid peripheral addresses in `arch_handoff`. | |
| unsafe { amlogic_rng_init(config) }; | |
| } | |
| if let Optional::Some(config) = &arch_handoff.qcom_rng_driver { | |
| // SAFETY: Caller upholds the one-time late boot contract, `hw_rng_*` ordering | |
| // preconditions, and valid peripheral addresses in `arch_handoff`. | |
| unsafe { qcom_rng_init(config) }; | |
| } | |
| if matches!(arch_handoff.generic32_watchdog_driver, Optional::Some(_)) { | |
| generic_32bit_watchdog_late_init(); | |
| } | |
| if arch_handoff.moonflower_power_driver { | |
| moonflower_power_init(); | |
| } | |
| // SAFETY: The caller guarantees `arch_handoff` is valid and temporary handoff memory remains | |
| // mapped. | |
| let iris_power_driver = unsafe { arch_handoff.iris_power_driver() }; | |
| if !iris_power_driver.is_empty() { | |
| iris_power_init(iris_power_driver); | |
| } | |
| } |