iommu/arm-smmu-v3: Implement Live Update support
Subsystems (4)
cover iommu/arm-smmu-v3: Implement Live Update support
The IOMMU Live Update framework [1] preserves the IOMMU hardware state and the translation (HWPTs) of devices assigned to userspace via iommufd/VFIO across a kexec based Live Update, using KHO and the DMA allocation preservation API [2]. This series implements the arm-smmu-v3 side of it, allowing an SMMUv3 to keep translating the DMA of preserved devices, without any disruption, while the kernel underneath is replaced.
We have an alignment session planned on IOMMU Live Update at LPC 2026 [3].
Design
======
The arm-smmu-v3 operates almost entirely out of in-memory data structures (Stream Table, CD tables, page tables) that the HW registers point to. Hence, a Live Update boils down to preserving these structures, keeping the SMMU enabled across the kexec and having the incoming kernel take them over without ever reprogramming anything that the HW is walking.
1. Preservation (outgoing kernel)
a. .preserve (per SMMU): Serializes the SMMU identity (the MMIO base is
used as the token, similar to VT-d), STRTAB_BASE_CFG and the Stream Table. A linear Stream Table is preserved as a whole, for a 2-level table the L1 table is preserved, along with an array of L2 tokens indexed by the L1 index.
b. .preserve_device (per master): Preserves the L2 Stream Tables holding
the STEs of the master, since .preserve only runs for the first preserved device. For an S1 domain, the CD table of the master (linear, or L1 + active L2 tables) is preserved too. SVA domains and masters with active PASIDs are rejected, matching the granularity of the core. The ASID/VMID tagging the translation is recorded in the attachment_id of the device state.
c. For S2 and nested domains only the STE is needed. The page tables
come from the preserved domain, and a guest-owned (nested) CD table lives in guest RAM, which is preserved with the guest memory anyway.
d. The Event queue memory is preserved as well, as it stays enabled
across the kexec. Only its preservation token is serialized; its base, size, PROD and CONS are left in the EVTQ registers.
e. .unpreserve_device and .unpreserve undo the above. An L2 Stream
Table is unpreserved once no preserved master uses it.
2. Shutdown (outgoing kernel)
Instead of clearing SMMUEN, .shutdown:
a. Masks the SMMU interrupts and waits for a running EVTQ handler. b. Installs abort STEs for all the unpreserved masters and clears the L1
descriptors of L2 tables that have no preserved stream.
c. Issues CFGI_ALL + TLBI_{EL2,NSNH}_ALL.
d. Disables the CMDQ and the PRIQ, leaving SMMUEN and EVTQEN set for the
preserved devices.
If any of this fails, it falls back to the regular full disable.
3. Restoration (incoming kernel)
a. arm_smmu_init_strtab() takes over the preserved Stream Table instead
of allocating a new one. The layout read back from STRTAB_BASE{,_CFG} is validated with the common kexec helpers from the kdump series [4] and the in-use ASIDs/VMIDs are reserved, such that new domains can never alias the preserved ones.
b. arm_smmu_alloc_cd_tables() takes over the CD table of a restored
master, validated with arm_smmu_kexec_check_ste_cdtab().
c. arm_smmu_init_queues() adopts the preserved EVTQ, resuming from the
live EVTQ_BASE/PROD/CONS, so any event recorded across the Live Update is left pending for the EVTQ handler of the incoming kernel.
d. The stream tables and the EVTQ are restored with the managed (dmam_*)
variants of [2] as they're devres allocations tied to the SMMU, while CD tables use the unmanaged (dma_*) variants, as their lifetime follows the master.
4. Hitless reset
When there's incoming preserved state for the SMMU (a failed restoration fails the probe, so this implies a restored Stream Table), arm_smmu_device_reset() shares the kdump adoption path, which already solves the problem of a live Stream Table:
a. SMMUEN and ATSCHK are retained, CR1/CR2/STRTAB_BASE are left alone,
as updating them with SMMUEN=1 is CONSTRAINED UNPREDICTABLE.
b. The CMDQ and the PRIQ (already disabled by the outgoing kernel) are
reset as usual, while EVTQEN and the EVTQ registers are left alone for the adopted EVTQ.
c. Any GERROR latched across the kexec is acked before the CMDQ is
re-enabled.
d. The interrupts are re-wired to the incoming kernel. The IRQs are
masked, hence IRQ_CFGn can be reprogrammed with SMMUEN=1. The EVTQ thread is then woken up to handle the events already pending.
e. CFGI_ALL and TLBI_NSNH_ALL/TLBI_EL2_ALL flush everything cached for
the previous kernel. The preserved devices merely re-walk the preserved tables.
5. Hitless re-attach
The IOMMU core restores a preserved domain by allocating a new paging domain around the preserved page table and attaching it to the restored device [1]. A new domain comes with a new ASID/VMID though, while the device is still doing DMA through the preserved CD/STE.
Similar to the Intel driver reclaiming the preserved Domain ID, the restored domain inherits the ASID/VMID of the live CD/STE on its first attach (the ID was reserved in 3.a, so it's just handed over). The stage, the page table root and the ID recorded in 1.b are cross-checked against the live CD/STE, failing the attach if the restored domain isn't what the HW is walking. With this, the CD/STE computed for the restored domain are identical to the live ones and the re-attach is a no-op for the HW.
Relation to the kdump adoption series
=====================================
Nicolin's kdump series [4] factored the parsing and validation of the previous kernel's SMMU tables into arm-smmu-v3-kexec.c with Live Update in mind. This series builds on it: ARM_SMMU_V3_KEXEC is also enabled by IOMMU_LIVEUPDATE, the restore paths use those helpers, the kdump reset path is shared via arm_smmu_strtab_is_live(), and a small arm_smmu_kexec_check_cdtab_l1_desc() helper is factored out of the ASID scan to be shared with the CD table restoration.
Open issues and discussion points
=================================
1. Stage of the restored domain: The core restores a domain with iommu_paging_domain_alloc(), i.e. flags == 0, which gives an S1 domain on SMMUs supporting S1. This covers VFIO/iommufd paging HWPTs, but a preserved S2 (NEST_PARENT) domain can't be restored as such. The core has to preserve the allocation flags. For now, such a mismatch fails the attach instead of switching the device to a different translation.
2. Hitless domain replace: A restored domain is immutable (no map/unmap), so the VMM has to replace it with a new HWPT after the Live Update. With S1, the ASID and TTB0 live in different CD qwords, making the replace non-hitless today; [11] addresses it with a temporary ASID. S2 has the same problem with the VMID and S2TTB and needs a temporary VMID.
3. Queue drain: The shutdown doesn't drain the CMDQ before disabling it, as the final invalidations are synced, there are no other submitters left at shutdown and the incoming kernel invalidates everything again. However, the CMDQV VCMDQs assigned to guests may need to be quiesced, which is future work along with the ATS and PASID support.
4. RMR SIDs: The outgoing kernel parks the unpreserved RMR streams in abort, and the incoming kernel skips installing the RMR bypass STEs, as it does for kdump, since they're written assuming a Stream Table that isn't live yet.
5. A failed restoration fails the SMMU probe instead of falling back to a full reset (which would at least keep the system usable). Also, a partially restored CD table isn't unwound on failure yet.
6. PASID granularity, PRI and ATS state of the preserved PCI devices are not handled yet (e.g. arm_smmu_enable_pasid() at probe for a device that still has ATS enabled).
7. The kdump [4] and invalidation [5] series both define ARM_SMMU_OPT bit 5; this tree moves ARM_SMMU_OPT_KDUMP_ADOPT to bit 6.
Dependencies
============
This series depends on the following series, applied in this order on top of v7.3-rc5:
1. iommu/arm-smmu-v3: Invalidation rework (v7), by Jason [5]
2. iommupt: Generic IOMMU page table for SMMUv3 (v2), by Jason [6]
3. iommu/arm-smmu-v3: kdump Stream Table adoption (v10), by Nicolin [4]
4. dma: DMA allocation preservation (RFC v2), by Samiullah [2]
5. vfio/pci: Live Update preservation of VFIO cdev (v5), by Vipin [8]
6. iommu: IOMMU Live Update state preservation (v5), by Samiullah [1]
The luo_file internal APIs [10] and the PCI Live Update series [7] are already merged in linux-next.
The VFIO series is taken as carried in [9], along with a small build fixup for the stack.
A tree with everything applied is available at:
https://github.com/pran005/linux/tree/arm-smmu-v3-liveupdate-rfc
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lpc.events/event/20/contributions/2612/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/
[7] https://lore.kernel.org/all/[email protected]/
[8] https://lore.kernel.org/kvm/[email protected]/
[9] https://github.com/samikhawaja/linux/tree/iommu/phase1-v5
[10] https://patch.msgid.link/[email protected]
[11] https://lore.kernel.org/all/[email protected]/
Pranjal Shrivastava (9):
iommu/kho: Extend IOMMU KHO ABI for ARM SMMUv3 iommu/arm-smmu-v3: Implement KHO preservation for STEs iommu/arm-smmu-v3: Implement CD Table preservation iommu/arm-smmu-v3: Implement Live Update Stream Table restoration iommu/arm-smmu-v3: Implement Live Update CD Table restoration iommu/arm-smmu-v3: Implement Live Update shutdown iommu/arm-smmu-v3: Retain SMMUEN across a Live Update restore iommu/arm-smmu-v3: Inherit the ASID/VMID of restored domains iommu/arm-smmu-v3: Adopt the Event queue across a Live Update
drivers/iommu/arm/Kconfig | 2 +- drivers/iommu/arm/arm-smmu-v3/Makefile | 1 + .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kexec.c | 44 +- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 997 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 119 ++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 56 + include/linux/kho/abi/iommu.h | 38 + 7 files changed, 1220 insertions(+), 37 deletions(-) create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 1/9 iommu/kho: Extend IOMMU KHO ABI for ARM SMMUv3
Extend the KHO IOMMU ABI to support state preservation for Arm SMMUv3.
Introduce struct iommu_smmuv3_hw_ser to capture SMMU hardware state, including the register base, Stream Table config and Live Update state tokens for the preserved Stream Tables.
Additionally, introduce struct iommu_master_smmuv3_ser to capture per master state. For now it is used to preserve CD tables for Stage-1 translation.
--- include/linux/kho/abi/iommu.h | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
include/linux/kho/abi/iommu.h +36 -0
@@ -82,6 +82,7 @@enum iommu_type_ser {IOMMU_INVALID ,IOMMU_INTEL , +IOMMU_ARM_SMMUV3 , };#define IOMMU_SER_FLAG_DELETED (1 << 0)
@@ -142,6 +143,23 @@ struct iommu_device_intel_ser {u64 max_pasid; } __packed; +/** +* struct iommu_master_smmuv3_ser - SMMUv3 specific device state +* @l1_cdtab_lu_state: Live update state token for the linear/L1 CD table +* @num_l2_cdtables: Number of active L2 CD tables preserved +* @l2_cdtab_lu_states_phys: Physical pointer to an array of u64 LU state tokens +* for the active L2 CD tables (0 if linear) +* +* Note: Currently unused for Stage-2 Live Update. Reserved for future +* preservation of Stage-1 per-device Context Descriptor (CD) tables. +*/ +struct iommu_master_smmuv3_ser { +u64 l1_cdtab_lu_state; +u32 num_l2_cdtables; +u32 padding; +u64 l2_cdtab_lu_states_phys; +} __packed; +/** * struct iommu_device_ser - Serialized state of a device * @hdr: Common object header
@@ -159,6 +177,7 @@ struct iommu_device_ser {struct iommu_dev_map_ser domain_iommu_ser;union {struct iommu_device_intel_ser intel; +struct iommu_master_smmuv3_ser smmuv3; }; } __packed;
@@ -181,6 +200,22 @@ struct iommu_intel_ser {u64 context_tables_bitmap[VTD_PRESERVED_BITMAP_LONGS ]; }; +/** +* struct iommu_smmuv3_hw_ser - SMMUv3 specific hardware state +* @phys_addr: Physical address of the SMMU register base +* @strtab_base_cfg: STRTAB_BASE_CFG register value +* @l1_strtab_lu_state: Live update state token for the linear table OR the L1 table +* @l2_strtab_lu_states_phys: Physical pointer to an array of u64 LU state tokens +* indexed by L1 index, 0 for L2 tables that aren't +* preserved (0 if linear) +*/ +struct iommu_smmuv3_hw_ser { +u64 phys_addr; +u64 strtab_base_cfg; +u64 l1_strtab_lu_state; +u64 l2_strtab_lu_states_phys; +} __packed; +/** * struct iommu_hw_ser - Serialized state of an IOMMU instance * @hdr: Common object header
@@ -194,6 +229,7 @@ struct iommu_hw_ser {u64 type;union {struct iommu_intel_ser intel; +struct iommu_smmuv3_hw_ser smmuv3; }; } __packed;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 2/9 iommu/arm-smmu-v3: Implement KHO preservation for STEs
In reply to Pranjal ShrivastavaIntroduce the foundation for SMMUv3 Live Update (LU) using the Kexec Handover (KHO) framework. Implement the preserve and preserve_device hooks to serialize SMMU & preserved masters' state for KHO, along with the respective unpreserve hooks.
Since .preserve only runs for the first preserved device, preserve the L2 Stream Tables in .preserve_device, recording their tokens by L1 index.
--- drivers/iommu/arm/arm-smmu-v3/Makefile | 1 + .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 277 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 7 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 14 + 4 files changed, 299 insertions(+) create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c
drivers/iommu/arm/arm-smmu-v3/Makefile +1 -0
@@ -3,6 +3,7 @@ obj-$(CONFIG_ARM_SMMU_V3) += arm_smmu_v3.o arm_smmu_v3-y := arm-smmu-v3.o arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_IOMMUFD) += arm-smmu-v3-iommufd.o arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_SVA) += arm-smmu-v3-sva.o +arm_smmu_v3-$(CONFIG_IOMMU_LIVEUPDATE) += arm-smmu-v3-liveupdate.o arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_KEXEC) += arm-smmu-v3-kexec.o arm_smmu_v3-$(CONFIG_CRASH_DUMP) += arm-smmu-v3-kdump.o arm_smmu_v3-$(CONFIG_TEGRA241_CMDQV) += tegra241-cmdqv.o
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c new file +277 -0
277 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
@@ -4548,6 +4548,12 @@ static const struct iommu_ops arm_smmu_ops = { .def_domain_type = arm_smmu_def_domain_type, .get_viommu_size = arm_smmu_get_viommu_size, .viommu_init = arm_vsmmu_init, +#ifdef CONFIG_IOMMU_LIVEUPDATE + .preserve = arm_smmu_preserve, + .unpreserve = arm_smmu_unpreserve, + .preserve_device = arm_smmu_preserve_device, + .unpreserve_device = arm_smmu_unpreserve_device, +#endif .user_pasid_table =1 , .owner = THIS_MODULE, .default_domain_ops = &(const struct iommu_domain_ops) {
@@ -5807,6 +5813,7 @@ static int arm_smmu_device_probe(struct platform_device *pdev)return -EINVAL ; } ioaddr = res->start; + smmu->base_phys = ioaddr;/* * Don't map the IMPLEMENTATION DEFINED regions, since they may contain
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +14 -0
@@ -952,6 +952,7 @@ struct arm_smmu_device {void __iomem *base;void __iomem *page1; +phys_addr_t base_phys;#define ARM_SMMU_FEAT_2_LVL_STRTAB (1 << 0)#define ARM_SMMU_FEAT_2_LVL_CDTAB (1 << 1)
@@ -1078,6 +1079,8 @@ struct arm_smmu_master { bool ste_ats_enabled : 1;bool stall_enabled;bool ats_always_on; +/* Preserved for a KHO Live Update */ +bool preserved;unsigned int ssid_bits;unsigned int iopf_refcount; };
@@ -1196,6 +1199,17 @@ extern struct mutex arm_smmu_asid_lock;struct arm_smmu_domain *arm_smmu_domain_alloc (void ); +#ifdef CONFIG_IOMMU_LIVEUPDATE +int arm_smmu_preserve_device (struct device *dev, +struct iommu_device_ser *device_ser); +int arm_smmu_preserve (struct iommu_device *iommu, +struct iommu_hw_ser *iommu_ser); +void arm_smmu_unpreserve_device (struct device *dev, +struct iommu_device_ser *device_ser); +void arm_smmu_unpreserve (struct iommu_device *iommu, +struct iommu_hw_ser *iommu_ser); +#endif +static inline void arm_smmu_domain_free (struct arm_smmu_domain *smmu_domain) {/* No concurrency with invalidation is possible at this point */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 3/9 iommu/arm-smmu-v3: Implement CD Table preservation
In reply to Pranjal ShrivastavaThe core IOMMU Live Update framework preserves page tables pointed by the respective TTBs but the CD tables are expected to be preserved by the SMMUv3 driver.
Extend the arm-smmu-v3 driver to preserve CD tables across a KHO with Live Update enabled. Currently, only support for preserving S1 CD tables exists since nested CD tables are auto-preserved as they reside in the guest memory. The current implementation doesn't support preservation at a PASID granularity due to the similar limitation in IOMMU LU core.
Unpreserve the CD tables in .unpreserve_device.
--- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 173 +++++++++++++++++- 1 file changed, 164 insertions(+), 9 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c +164 -9
@@ -11,6 +11,114 @@#include "arm-smmu-v3.h" #ifdef CONFIG_IOMMU_LIVEUPDATE +static int arm_smmu_preserve_cd_table_linear (struct arm_smmu_master *master, +struct iommu_device_ser *device_ser) +{ +struct device *dev = master->smmu->dev; +struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; +u32 size = cd_table->linear.num_ents *sizeof (struct arm_smmu_cd ); +u64 state; +int ret; + + ret =dma_preserve_coherent_allocation (dev, cd_table->linear.table, + size, cd_table->cdtab_dma, &state); +if (ret) +return ret; + + device_ser->smmuv3.l1_cdtab_lu_state = state; + device_ser->smmuv3.num_l2_cdtables =0 ; + +return 0 ; +} + +static int arm_smmu_preserve_cd_table_2lvl (struct arm_smmu_master *master, +struct iommu_device_ser *device_ser) +{ +struct device *dev = master->smmu->dev; +struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; +u32 l1size = cd_table->l2.num_l1_ents *sizeof (struct arm_smmu_cdtab_l1 ); +u32 num_l2 =0 ; +u64 *l2_states; +u64 state; +int ret, i; + + ret =dma_preserve_coherent_allocation (dev, cd_table->l2.l1tab, + l1size, cd_table->cdtab_dma, &state); +if (ret) +return ret; + + device_ser->smmuv3.l1_cdtab_lu_state = state; + +for (i =0 ; i < cd_table->l2.num_l1_ents; i++) { +if (cd_table->l2.l2ptrs[i]) + num_l2++; + } + + device_ser->smmuv3.num_l2_cdtables = num_l2; +if (!num_l2) +return 0 ; + + l2_states =kho_alloc_preserve (sizeof (*l2_states) * num_l2); +if (IS_ERR (l2_states)) { + ret =PTR_ERR (l2_states); + goto err_unpreserve_cd_l1; + } + + device_ser->smmuv3.l2_cdtab_lu_states_phys =virt_to_phys (l2_states); + num_l2 =0 ; + +for (i =0 ; i < cd_table->l2.num_l1_ents; i++) { +dma_addr_t l2_dma; + +if (!cd_table->l2.l2ptrs[i]) +continue ; + + l2_dma =le64_to_cpu (cd_table->l2.l1tab[i].l2ptr) &CTXDESC_L1_DESC_L2PTR_MASK ; + ret =dma_preserve_coherent_allocation (dev, cd_table->l2.l2ptrs[i], +sizeof (struct arm_smmu_cdtab_l2 ), + l2_dma, &state); +if (ret) + goto err_free_cd_l2_states; + + l2_states[num_l2++] = state; + } + +return 0 ; + +err_free_cd_l2_states: +for (i = i -1 ; i >=0 ; i--) { +if (cd_table->l2.l2ptrs[i]) { + num_l2--; +dma_unpreserve_coherent_allocation (dev, l2_states[num_l2]); + } + } +kho_unpreserve_free (l2_states); +err_unpreserve_cd_l1: +dma_unpreserve_coherent_allocation (dev, device_ser->smmuv3.l1_cdtab_lu_state); +return ret; +} + +static void arm_smmu_unpreserve_cd_table (struct arm_smmu_master *master, +struct iommu_device_ser *device_ser) +{ +struct device *dev = master->smmu->dev; +u32 num_l2 = device_ser->smmuv3.num_l2_cdtables; +u64 *l2_states; +u32 i; + +if (!device_ser->smmuv3.l1_cdtab_lu_state) +return ; + +if (num_l2) { + l2_states =phys_to_virt (device_ser->smmuv3.l2_cdtab_lu_states_phys); +for (i =0 ; i < num_l2; i++) +dma_unpreserve_coherent_allocation (dev, l2_states[i]); +kho_unpreserve_free (l2_states); + } +dma_unpreserve_coherent_allocation (dev, device_ser->smmuv3.l1_cdtab_lu_state); +memset (&device_ser->smmuv3,0 ,sizeof (device_ser->smmuv3)); +} +static u64 *arm_smmu_l2_strtab_states (struct arm_smmu_device *smmu) {struct iommu_hw_ser *iommu_ser =iommu_preserved_state (&smmu->iommu);
@@ -105,17 +213,53 @@ int arm_smmu_preserve_device(struct device *dev, {struct arm_smmu_master *master =dev_iommu_priv_get (dev);struct iommu_domain *domain =iommu_get_domain_for_dev (dev); +struct arm_smmu_domain *smmu_domain; +struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;struct iommu_domain_ser *domain_ser; -int ret; +int ret =0 ; + +memset (&device_ser->smmuv3,0 ,sizeof (device_ser->smmuv3));/* * We'd anyway configure abort STEs for non-preserved masters. * TODO: Re-visit for identity once IOMMUFD noIOMMU is merged +* TODO: Re-visit for CXL + ATS + Identity CD Table thing +* Can use cd_table_allocated() helper here? */ if (domain->type ==IOMMU_DOMAIN_IDENTITY || domain->type ==IOMMU_DOMAIN_BLOCKED )return 0 ; +/* +* For nested domains we only need to preserve STE. +* The S2 parent domain's page tables are preserved via its own +* iommu_preserve_domain() call during IOMMUFD's HWPT preservation. +* Since the CD Table in this case lives in the guest memory, it is +* naturally preserved by default across KHO when Live Update is enabled. +* Thus, since CD isn't allocated via DMA allocator by the host driver +* we must not attempt preserving CD here. +*/ +if (domain->type ==IOMMU_DOMAIN_NESTED ) + goto skip_cd_preservation; + + smmu_domain =to_smmu_domain (domain); + +/* SVA domains cannot be preserved across KHO */ +if (smmu_domain->stage ==ARM_SMMU_DOMAIN_SVA ) { +dev_err (dev,"SVA domains are NOT preserved across KHO\n" ); +return -EOPNOTSUPP ; + } + +/* +* The IOMMU LU Core doesn't support preservation at a PASID +* granularity yet, reject preservation to prevent leaving active +* PASIDs pointing to unpreserved tables. +*/ +if (arm_smmu_ssids_in_use (&master->cd_table)) { +dev_err (dev,"Preserving devices with active PASIDS is NOT supported w/ Live Update\n" ); +return -EOPNOTSUPP ; + } +if (domain->preserved_state) { domain_ser = domain->preserved_state; }else {
@@ -128,18 +272,28 @@ int arm_smmu_preserve_device(struct device *dev,/* Link this master to the preserved IOMMU domain in the ABI */ device_ser->domain_iommu_ser.domain_phys =virt_to_phys (domain_ser); - ret =arm_smmu_preserve_l2_strtabs (master); +/* If it's not Stage-1, or the CD table isn't allocated, we're done */ +if (smmu_domain->stage !=ARM_SMMU_DOMAIN_S1 || + !arm_smmu_cdtab_allocated (&master->cd_table)) + goto skip_cd_preservation; + +if (cd_table->s1fmt ==STRTAB_STE_0_S1FMT_LINEAR ) + ret =arm_smmu_preserve_cd_table_linear (master, device_ser); +else if (cd_table->s1fmt ==STRTAB_STE_0_S1FMT_64K_L2 ) + ret =arm_smmu_preserve_cd_table_2lvl (master, device_ser); + +skip_cd_preservation:if (ret)return ret; -/* Mark the master as preserved to track state across disable */ - master->preserved = true; - -/* -* TODO: Preserve CD tables for Stage-1 domains here using -* dmam_preserve_allocation_attrs() on master->cd_table. -*/ + ret =arm_smmu_preserve_l2_strtabs (master); +if (ret) { +arm_smmu_unpreserve_cd_table (master, device_ser); +return ret; + } +/* Mark the master as preserved to track state during disable */ + master->preserved = true;return 0 ; }
@@ -153,6 +307,7 @@ void arm_smmu_unpreserve_device(struct device *dev, master->preserved = false;arm_smmu_unpreserve_l2_strtabs (master, master->num_streams); +arm_smmu_unpreserve_cd_table (master, device_ser); } static int arm_smmu_preserve_strtab_2lvl(struct arm_smmu_device *smmu,
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 4/9 iommu/arm-smmu-v3: Implement Live Update Stream Table restoration
In reply to Pranjal ShrivastavaDuring the boot phase of a Kexec Handover (KHO), the incoming kernel must adopt the preserved SMMU Stream Table to seamlessly inherit the IOMMU state from the outgoing kernel.
Restore it in arm_smmu_init_strtab() using the kexec helpers to validate the live STRTAB_BASE{,_CFG} and reserve the in-use ASIDs/VMIDs. Enable ARM_SMMU_V3_KEXEC for IOMMU_LIVEUPDATE for this.
--- drivers/iommu/arm/Kconfig | 2 +- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 142 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 + 4 files changed, 153 insertions(+), 1 deletion(-)
drivers/iommu/arm/Kconfig +1 -1
@@ -113,7 +113,7 @@ config ARM_SMMU_V3_IOMMUFD # Common helpers for a kexec'd kernel, e.g. kdump adoption and live update config ARM_SMMU_V3_KEXEC - def_bool CRASH_DUMP + def_bool CRASH_DUMP || IOMMU_LIVEUPDATE config ARM_SMMU_V3_KUNIT_TEST tristate "KUnit tests for arm-smmu-v3 driver" if !KUNIT_ALL_TESTS
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c +142 -0
146 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4 -0
@@ -4772,6 +4772,10 @@ static int arm_smmu_init_strtab(struct arm_smmu_device *smmu) !arm_smmu_kdump_adopt_strtab(smmu))return 0 ; + ret =arm_smmu_liveupdate_restore_strtab (smmu); +if (ret != -ENOENT ) +return ret; +if (smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ) ret =arm_smmu_init_strtab_2lvl (smmu);else
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
@@ -1208,6 +1208,12 @@ void arm_smmu_unpreserve_device(struct device *dev, struct iommu_device_ser *device_ser);void arm_smmu_unpreserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser); +int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu); +#else +static inline int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu) +{ +return -ENOENT ; +}#endif static inline void arm_smmu_domain_free (struct arm_smmu_domain *smmu_domain)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 5/9 iommu/arm-smmu-v3: Implement Live Update CD Table restoration
In reply to Pranjal ShrivastavaDuring the boot phase of a Kexec Handover (KHO), the incoming kernel must restore Context Descriptor (CD) tables for preserved devices to maintain Stage-1 translation contexts. Add a check within arm_smmu_alloc_cd_tables() to implement the CD table restoration.
--- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kexec.c | 44 ++++++--- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 90 +++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 ++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 ++ 4 files changed, 138 insertions(+), 11 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kexec.c +33 -11
@@ -222,6 +222,33 @@ int arm_smmu_kexec_check_ste_cdtab(struct arm_smmu_device *smmu, u64 ste0,return 0 ; } +/** +* arm_smmu_kexec_check_cdtab_l1_desc() - Check one CD table L1 descriptor +* @l1_desc: L1 descriptor value from the previous kernel's CD table +* @l2_base: pointer to return the L2 CD table's physical address +* +* Return: 1 if the descriptor is unused, 0 if it is valid with @l2_base set, or +* -EINVAL if it is malformed +*/ +int arm_smmu_kexec_check_cdtab_l1_desc (u64 l1_desc,phys_addr_t *l2_base) +{ +phys_addr_t base = l1_desc &CTXDESC_L1_DESC_L2PTR_MASK ; + +if (!(l1_desc &CTXDESC_L1_DESC_V )) +return 1 ; + +/* +* A valid descriptor never carries a null pointer. Also, an L2 table is +* always 64KB-aligned, so an unaligned pointer would make this kernel +* read a different table. +*/ +if (!base || !IS_ALIGNED (base,sizeof (struct arm_smmu_cdtab_l2 ))) +return -EINVAL ; + + *l2_base = base; +return 0 ; +} +static int arm_smmu_kexec_resv_asid (struct arm_smmu_device *smmu,u32 asid) {/* A valid CD never has ASID 0; both kernels share the same HW limit */
@@ -310,21 +337,16 @@ static int arm_smmu_kexec_resv_s1_asids(struct arm_smmu_device *smmu, u64 ste0)/* Aliased L2 tables cannot extend the walk; they only repeat a scan */ for (i =0 ; i < num_l1_ents; i++) {u64 l1_desc =le64_to_cpu (l1tab[i].l2ptr); -phys_addr_t l2_base = l1_desc &CTXDESC_L1_DESC_L2PTR_MASK ;struct arm_smmu_cdtab_l2 *l2; +phys_addr_t l2_base; -if (!(l1_desc &CTXDESC_L1_DESC_V )) + ret =arm_smmu_kexec_check_cdtab_l1_desc (l1_desc, &l2_base); +if (ret ==1 ) { + ret =0 ;continue ; - -/* -* A valid descriptor never carries a null pointer. Also, an L2 -* table is always 64KB-aligned, so an unaligned pointer would -* make this kernel read a different table. -*/ -if (!l2_base || !IS_ALIGNED (l2_base,sizeof (*l2))) { - ret = -EINVAL ; -break ; } +if (ret) +break ; l2 =memremap (l2_base, num_cds *sizeof (*l2->cds),MEMREMAP_WB );if (!l2) {
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c +90 -0
94 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9 -0
@@ -17,6 +17,7 @@#include <linux/err.h> #include <linux/interrupt.h> #include <linux/generic_pt/iommu.h> +#include <linux/iommu-liveupdate.h> #include <linux/iopoll.h> #include <linux/jump_label.h>
@@ -1723,6 +1724,14 @@ static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master)struct arm_smmu_device *smmu = master->smmu;struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; +if (dev_iommu_restored_state (master->dev)) { + ret =arm_smmu_liveupdate_restore_cd_tables (master); +if (ret) +return ret; +if (arm_smmu_cdtab_allocated (cd_table)) +return 0 ; + } + cd_table->s1cdmax = master->ssid_bits;/*
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
@@ -1209,11 +1209,16 @@ void arm_smmu_unpreserve_device(struct device *dev,void arm_smmu_unpreserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser);int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu); +int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master);#else static inline int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu) {return -ENOENT ; } +static inline int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master) +{ +return 0 ; +}#endif static inline void arm_smmu_domain_free (struct arm_smmu_domain *smmu_domain)
@@ -1369,6 +1374,7 @@ int arm_smmu_kexec_check_strtab_l1_desc(struct arm_smmu_device *smmu,int arm_smmu_kexec_check_ste_cdtab (struct arm_smmu_device *smmu,u64 ste0,phys_addr_t *cdtab,u32 *s1fmt,u32 *max_contexts); +int arm_smmu_kexec_check_cdtab_l1_desc (u64 l1_desc,phys_addr_t *l2_base);int arm_smmu_kexec_scan_and_resv_ids (struct arm_smmu_device *smmu);void arm_smmu_kexec_unresv_ids (struct arm_smmu_device *smmu);#endif /* CONFIG_ARM_SMMU_V3_KEXEC */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 6/9 iommu/arm-smmu-v3: Implement Live Update shutdown
In reply to Pranjal ShrivastavaDuring a Kexec Handover (KHO) with Live Update enabled, the SMMUv3 must not be disabled (via CR0.SMMUEN=0) during device shutdown since doing so would instantly stop active DMA traffic preserved for masters.
Modify the .shutdown hook to install abort STEs for unpreserved masters, invalidate the L1STDs of unpreserved L2 tables and flush the config and TLB caches. Mask the interrupts, wait for the EVTQ handler and disable the queues, leaving SMMUEN set. Fall back to a full disable on failure.
--- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 120 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 25 +++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 12 ++ 3 files changed, 152 insertions(+), 5 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c +120 -0
@@ -5,6 +5,7 @@*/ #include <linux/dma-mapping.h> +#include <linux/interrupt.h> #include <linux/iommu.h> #include <linux/iommu-liveupdate.h> #include <linux/kexec_handover.h>
@@ -429,6 +430,125 @@ void arm_smmu_unpreserve(struct iommu_device *iommu,arm_smmu_unpreserve_strtab_linear (smmu, iommu_ser); } +static void arm_smmu_liveupdate_clear_l1_std (struct arm_smmu_device *smmu, +unsigned long *l2_active) +{ +struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; +int i; + +for (i =0 ; i < cfg->l2.num_l1_ents; i++) { +if (!cfg->l2.l2ptrs[i] ||test_bit (i, l2_active)) +continue ; + +/* Clear L1 STD for unpreserved streams */ +WRITE_ONCE (cfg->l2.l1tab[i].l2ptr,0 ); + } +} + +int arm_smmu_liveupdate_shutdown (struct arm_smmu_device *smmu) +{ +struct arm_smmu_master *master; +struct arm_smmu_stream *stream; +struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; +struct rb_node *node; +struct arm_smmu_ste abort_ste; +struct arm_smmu_cmd cmd_cfgi, cmd_el2, cmd_nsnh; +unsigned long *l2_active =NULL ; +bool is_2lvl = smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ; +u32 cr0; +int ret; + +/* Only the incoming kernel unmasks the SMMU interrupts again */ +if (arm_smmu_disable_irqs (smmu)) +dev_warn (smmu->dev,"failed to disable irqs\n" ); + +/* Wait for a running EVTQ handler */ +if (smmu->combined_irq || smmu->evtq.q.irq) +synchronize_irq (smmu->combined_irq ?: smmu->evtq.q.irq); + +if (is_2lvl) { + l2_active =bitmap_zalloc (cfg->l2.num_l1_ents,GFP_KERNEL ); +if (!l2_active) { +dev_err (smmu->dev,"OOM: Falling back to hard disable\n" ); +return -ENOMEM ; + } + } + +/* Prepare an abort STE for unpreserved masters */ +arm_smmu_make_abort_ste (&abort_ste); + +/* +* We do not scrub unpreserved Context Descriptors (CDs) here since: +* +* 1. Each master has its own independently allocated CD table page, +* i.e. multiple masters never share a CD table. +* +* 2. We explicitly reject preserving any device with active PASIDs in +* the .preserve_device op. Thus, any preserved master is guaranteed +* to only be using CD[0]. +* +* Therefore, partial preservation within a CD table is not possible, +* and we only need to isolate unpreserved streams within shared +* Stream Tables. +*/ +mutex_lock (&smmu->streams_mutex); + +/* Install the abort STEs for unpreserved masters */ +for (node =rb_first (&smmu->streams); node; node =rb_next (node)) { + stream = rb_entry(node, struct arm_smmu_stream, node); + master = stream->master; + +if (master->preserved) { +if (is_2lvl) +set_bit (arm_smmu_strtab_l1_idx (stream->id), l2_active); + }else { +arm_smmu_write_ste (master, stream->id, +arm_smmu_get_step_for_sid (smmu, stream->id), + &abort_ste); + } + } + +/* Invalidate completely unpreserved streams */ +if (is_2lvl) { +arm_smmu_liveupdate_clear_l1_std (smmu, l2_active); +bitmap_free (l2_active); + } + +mutex_unlock (&smmu->streams_mutex); + +/* Sync hardware caches to observe updated structures */ + cmd_cfgi =arm_smmu_make_cmd_cfgi_all (); +arm_smmu_cmdq_issue_cmdlist (smmu, &smmu->cmdq, &cmd_cfgi,1 , true); + +/* +* Aggressively flush all TLBs to ensure no stale entries exist for +* unpreserved streams. The preserved streams will take a minor hit +* re-walking their page tables, but this guarantees safety. +*/ +if (smmu->features &ARM_SMMU_FEAT_HYP ) { + cmd_el2 =arm_smmu_make_cmd_op (CMDQ_OP_TLBI_EL2_ALL ); +arm_smmu_cmdq_issue_cmdlist (smmu, &smmu->cmdq, &cmd_el2,1 , true); + } + + cmd_nsnh =arm_smmu_make_cmd_op (CMDQ_OP_TLBI_NSNH_ALL ); +arm_smmu_cmdq_issue_cmdlist (smmu, &smmu->cmdq, &cmd_nsnh,1 , true); + +/* +* No need to drain the CMDQ: the invalidations above are synced, no +* other submitters are left at shutdown and the incoming kernel +* invalidates everything again. +* TODO: Quiesce the CMDQV VCMDQs assigned to guests. +*/ + +/* Disable the queues, leaving SMMUEN set for the preserved masters */ + cr0 =readl_relaxed (smmu->base +ARM_SMMU_CR0 ); + cr0 &= ~(CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ); + ret =arm_smmu_write_reg_sync (smmu, cr0,ARM_SMMU_CR0 ,ARM_SMMU_CR0ACK ); +if (ret) +dev_err (smmu->dev,"failed to disable queues\n" ); +return ret; +} +static int arm_smmu_liveupdate_restore_strtab_2lvl (struct arm_smmu_device *smmu,struct iommu_hw_ser *iommu_ser,u32 cfg_reg,phys_addr_t base)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +20 -5
@@ -1853,9 +1853,9 @@ static const struct arm_smmu_entry_writer_ops arm_smmu_ste_writer_ops = { .get_update_safe = arm_smmu_get_ste_update_safe, }; -static void arm_smmu_write_ste (struct arm_smmu_master *master,u32 sid, -struct arm_smmu_ste *ste, -const struct arm_smmu_ste *target) +void arm_smmu_write_ste (struct arm_smmu_master *master,u32 sid, +struct arm_smmu_ste *ste, +const struct arm_smmu_ste *target) {struct arm_smmu_device *smmu = master->smmu;struct arm_smmu_ste_writer ste_writer = {
@@ -4813,8 +4813,8 @@ static int arm_smmu_init_structures(struct arm_smmu_device *smmu)return 0 ; } -static int arm_smmu_write_reg_sync (struct arm_smmu_device *smmu,u32 val, -unsigned int reg_off,unsigned int ack_off) +int arm_smmu_write_reg_sync (struct arm_smmu_device *smmu,u32 val, +unsigned int reg_off,unsigned int ack_off) {u32 reg;
@@ -4998,6 +4998,12 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)return 0 ; } +int arm_smmu_disable_irqs (struct arm_smmu_device *smmu) +{ +return arm_smmu_write_reg_sync (smmu,0 ,ARM_SMMU_IRQ_CTRL , +ARM_SMMU_IRQ_CTRLACK ); +} +static int arm_smmu_device_disable (struct arm_smmu_device *smmu) {int ret;
@@ -5919,6 +5925,15 @@ static void arm_smmu_device_shutdown(struct platform_device *pdev) {struct arm_smmu_device *smmu =platform_get_drvdata (pdev); +if (iommu_preserved_state (&smmu->iommu)) { +if (!arm_smmu_liveupdate_shutdown (smmu)) +return ; + } + +/* +* Disable the SMMU on standard shutdown/reboot. +* Fallback to this path if the Live Update shutdown failed. +*/ arm_smmu_device_disable (smmu); }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +12 -0
@@ -1198,6 +1198,9 @@ to_smmu_nested_domain(struct iommu_domain *dom)extern struct mutex arm_smmu_asid_lock;struct arm_smmu_domain *arm_smmu_domain_alloc (void ); +int arm_smmu_write_reg_sync (struct arm_smmu_device *smmu,u32 val, +unsigned int reg_off,unsigned int ack_off); +int arm_smmu_disable_irqs (struct arm_smmu_device *smmu);#ifdef CONFIG_IOMMU_LIVEUPDATE int arm_smmu_preserve_device(struct device *dev,
@@ -1208,9 +1211,14 @@ void arm_smmu_unpreserve_device(struct device *dev, struct iommu_device_ser *device_ser);void arm_smmu_unpreserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser); +int arm_smmu_liveupdate_shutdown (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master);#else +static inline int arm_smmu_liveupdate_shutdown (struct arm_smmu_device *smmu) +{ +return -EOPNOTSUPP ; +}static inline int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu) {return -ENOENT ;
@@ -1242,6 +1250,10 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, struct arm_smmu_cd *cd, struct iommu_domain *old); +void arm_smmu_write_ste (struct arm_smmu_master *master,u32 sid, +struct arm_smmu_ste *ste, +const struct arm_smmu_ste *target); +void arm_smmu_domain_tlbi (struct arm_smmu_tlbi *tlbi,struct arm_smmu_domain *smmu_domain);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 7/9 iommu/arm-smmu-v3: Retain SMMUEN across a Live Update restore
In reply to Pranjal Shrivastavaarm_smmu_device_reset() clears SMMUEN and rewrites CR1, CR2 and STRTAB_BASE, aborting the DMA of the preserved devices. The kdump adoption path already avoids this for a live Stream Table.
Introduce arm_smmu_strtab_is_live() and route a Live Update restore through the kdump reset path. Also ack a stale GERROR, since the outgoing kernel masked the interrupts, and skip installing the RMR bypass STEs.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 29 ++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +20 -9
@@ -5048,6 +5048,13 @@ static void arm_smmu_write_strtab(struct arm_smmu_device *smmu)writel_relaxed (reg, smmu->base +ARM_SMMU_STRTAB_BASE_CFG ); } +/* Adopted by kdump or restored by LU (a failed restore fails the probe) */ +static bool arm_smmu_strtab_is_live (struct arm_smmu_device *smmu) +{ +return (smmu->options &ARM_SMMU_OPT_KDUMP_ADOPT ) || +iommu_get_preserved_data (smmu->base_phys,IOMMU_ARM_SMMUV3 ); +} +static int arm_smmu_device_reset (struct arm_smmu_device *smmu) {int ret;
@@ -5064,10 +5071,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)* According to spec, updating STRTAB_BASE/CR1/CR2 when CR0_SMMUEN=1 is * CONSTRAINED UNPREDICTABLE. So, skip those register updates and rely * on the adopted stream table from the crashed kernel. +* Same for a Live Update restore. */ -if (smmu->options &ARM_SMMU_OPT_KDUMP_ADOPT ) { -dev_info (smmu->dev, -"kdump: retaining SMMUEN for in-flight DMA\n" ); +if (arm_smmu_strtab_is_live (smmu)) { +dev_info (smmu->dev,"%s: retaining SMMUEN for in-flight DMA\n" , +is_kdump_kernel () ?"kdump" :"live update" ); enables = reg & ~(CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ); goto reset_queues; }
@@ -5103,7 +5111,7 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)arm_smmu_write_strtab (smmu); reset_queues: -if (smmu->options &ARM_SMMU_OPT_KDUMP_ADOPT ) { +if (arm_smmu_strtab_is_live (smmu)) {/* * Disable queues since arm_smmu_device_disable() was skipped. * CR0 fields are independent per spec, so the queue enable bits
@@ -5122,8 +5130,9 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)* errors would be visible. Ack everything prior to re-enabling the CMDQ * as a stale CMDQ_ERR would halt the CMDQ and new command will timeout. * Acking SFM_ERR is defined too, although it would not exit the SFM. +* Same for a Live Update, as the outgoing kernel masked the interrupts. */ -if (is_kdump_kernel ()) { +if (is_kdump_kernel () ||arm_smmu_strtab_is_live (smmu)) {u32 gerror =readl_relaxed (smmu->base +ARM_SMMU_GERROR );u32 gerrorn =readl_relaxed (smmu->base +ARM_SMMU_GERRORN );
@@ -5193,10 +5202,10 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu) }/* -* In a kdump adopt case, retain the crashed kernel's ATS-check policy -* captured above rather than forcing it on. +* In a kdump adopt or a Live Update restore case, retain the previous +* kernel's ATS-check policy captured above rather than forcing it on. */ -if (!(smmu->options &ARM_SMMU_OPT_KDUMP_ADOPT ) && +if (!arm_smmu_strtab_is_live (smmu) && (smmu->features &ARM_SMMU_FEAT_ATS )) { enables |=CR0_ATSCHK ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
@@ -5722,8 +5731,10 @@ static void arm_smmu_rmr_install_bypass_ste(struct arm_smmu_device *smmu)* Kdump adoption keeps the crashed kernel's table live. Rewriting the * adopted STE here could expose an in-flight fetch to a transient V=0 * entry, or change Cfg=translate to Cfg=bypass. Must skip here. +* Same for a Live Update restore. +* TODO: Re-install the bypass STEs of the unpreserved RMR SIDs. */ -if (smmu->options &ARM_SMMU_OPT_KDUMP_ADOPT ) +if (arm_smmu_strtab_is_live (smmu))return ;INIT_LIST_HEAD (&rmr_list);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 8/9 iommu/arm-smmu-v3: Inherit the ASID/VMID of restored domains
In reply to Pranjal ShrivastavaThe IOMMU core restores a preserved domain by allocating a new paging domain around the preserved page table and attaching it to the restored device. The new domain gets a new ASID/VMID though, while the device is still doing DMA through the preserved CD/STE.
Similar to the Intel driver reclaiming the preserved Domain ID, record the ASID/VMID in the attachment_id and let a restored domain inherit the live ASID/VMID on its first attach. Check the stage, the page table root and the ID against the live CD/STE, failing the attach on a mismatch.
--- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 115 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 8 ++ 3 files changed, 129 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c +115 -0
@@ -273,6 +273,11 @@ int arm_smmu_preserve_device(struct device *dev,/* Link this master to the preserved IOMMU domain in the ABI */ device_ser->domain_iommu_ser.domain_phys =virt_to_phys (domain_ser); +/* Record the ASID/VMID for the incoming kernel to cross-check */ + device_ser->domain_iommu_ser.attachment_id = + smmu_domain->stage ==ARM_SMMU_DOMAIN_S1 ? + smmu_domain->cd.asid : smmu_domain->s2_cfg.vmid; +/* If it's not Stage-1, or the CD table isn't allocated, we're done */ if (smmu_domain->stage !=ARM_SMMU_DOMAIN_S1 || !arm_smmu_cdtab_allocated (&master->cd_table))
@@ -781,4 +786,114 @@ int arm_smmu_liveupdate_restore_cd_tables(struct arm_smmu_master *master) return ret; } +/* Hand over the reserved ASID/VMID, releasing the never programmed one */ +static int arm_smmu_liveupdate_inherit_id (struct arm_smmu_domain *smmu_domain, +u32 id) +{ +struct arm_smmu_device *smmu = smmu_domain->smmu; +int ret; + +if (smmu_domain->stage ==ARM_SMMU_DOMAIN_S1 ) { +if (smmu_domain->cd.asid == id) +return 0 ; + +/* Only a reserved entry, which loads as NULL, can be taken */ +if (xa_load (&smmu->asid_map, id)) +return -EBUSY ; + ret =xa_err (xa_store (&smmu->asid_map, id, smmu_domain, +GFP_KERNEL )); +if (ret) +return ret; +xa_erase (&smmu->asid_map, smmu_domain->cd.asid); + smmu_domain->cd.asid = id; +return 0 ; + } + +if (smmu_domain->s2_cfg.vmid == id) +return 0 ; + +/* The vmid_map already holds @id, simply transfer its ownership */ +ida_free (&smmu->vmid_map, smmu_domain->s2_cfg.vmid); + smmu_domain->s2_cfg.vmid = id; +return 0 ; +} + +/* +* Inherit the live ASID/VMID of a restored master, after checking that +* @smmu_domain matches its live translation. Called before +* arm_smmu_attach_prepare() builds the invalidation array. +*/ +int arm_smmu_liveupdate_attach_restored (struct arm_smmu_master *master, +struct arm_smmu_domain *smmu_domain) +{ +struct iommu_device_ser *dev_ser =dev_iommu_restored_state (master->dev); +struct arm_smmu_device *smmu = master->smmu; +struct pt_iommu_armv8_hw_info info; +struct arm_smmu_ste *ste; +u64 ste0, ttb, live_ttb; +u32 id; + +lockdep_assert_held (&arm_smmu_asid_lock); + +if (!dev_ser || !iommu_domain_restored_state (&smmu_domain->domain)) +return 0 ; + + ste =arm_smmu_get_step_for_sid (smmu, master->streams[0 ].id); + ste0 =le64_to_cpu (ste->data[0 ]); +if (!(ste0 &STRTAB_STE_0_V )) +return 0 ; + +pt_iommu_armv8_hw_info (&smmu_domain->armv8pt, &info); + +switch (FIELD_GET (STRTAB_STE_0_CFG , ste0)) { +case STRTAB_STE_0_CFG_S1_TRANS : { +struct arm_smmu_cd *cdptr; +u64 cd0; + +if (smmu_domain->stage !=ARM_SMMU_DOMAIN_S1 ) + goto err_mismatch; + + cdptr =arm_smmu_get_cd_ptr (master,IOMMU_NO_PASID ); +if (!cdptr) + goto err_mismatch; + + cd0 =le64_to_cpu (cdptr->data[0 ]); +if (!(cd0 &CTXDESC_CD_0_V )) + goto err_mismatch; + + id =FIELD_GET (CTXDESC_CD_0_ASID , cd0); + live_ttb =le64_to_cpu (cdptr->data[1 ]) &CTXDESC_CD_1_TTB0_MASK ; + ttb = info.ttb &CTXDESC_CD_1_TTB0_MASK ; +break ; + } +case STRTAB_STE_0_CFG_S2_TRANS : +if (smmu_domain->stage !=ARM_SMMU_DOMAIN_S2 ) + goto err_mismatch; + + id =FIELD_GET (STRTAB_STE_2_S2VMID ,le64_to_cpu (ste->data[2 ])); + live_ttb =le64_to_cpu (ste->data[3 ]) &STRTAB_STE_3_S2TTB_MASK ; + ttb = info.ttb &STRTAB_STE_3_S2TTB_MASK ; +break ; +default : +/* Nothing to inherit, nested STEs aren't restored (yet) */ +return 0 ; + } + +if (live_ttb != ttb || id != dev_ser->domain_iommu_ser.attachment_id) + goto err_mismatch; + +/* A shared restored domain inherits its ID on the first attach */ +if (!list_empty (&smmu_domain->devices) && + id != (smmu_domain->stage ==ARM_SMMU_DOMAIN_S1 ? + smmu_domain->cd.asid : smmu_domain->s2_cfg.vmid)) + goto err_mismatch; + +return arm_smmu_liveupdate_inherit_id (smmu_domain, id); + +err_mismatch: +dev_err (master->dev, +"restored domain doesn't match the live translation\n" ); +return -EINVAL ; +} +#endif
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6 -0
@@ -3838,6 +3838,12 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,*/ mutex_lock (&arm_smmu_asid_lock); + ret =arm_smmu_liveupdate_attach_restored (master, smmu_domain); +if (ret) { +mutex_unlock (&arm_smmu_asid_lock); +return ret; + } + ret =arm_smmu_attach_prepare (&state, domain);if (ret) {mutex_unlock (&arm_smmu_asid_lock);
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +8 -0
@@ -1214,6 +1214,8 @@ void arm_smmu_unpreserve(struct iommu_device *iommu,int arm_smmu_liveupdate_shutdown (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master); +int arm_smmu_liveupdate_attach_restored (struct arm_smmu_master *master, +struct arm_smmu_domain *smmu_domain);#else static inline int arm_smmu_liveupdate_shutdown (struct arm_smmu_device *smmu) {
@@ -1227,6 +1229,12 @@ static inline int arm_smmu_liveupdate_restore_cd_tables(struct arm_smmu_master * {return 0 ; } +static inline int +arm_smmu_liveupdate_attach_restored (struct arm_smmu_master *master, +struct arm_smmu_domain *smmu_domain) +{ +return 0 ; +}#endif static inline void arm_smmu_domain_free (struct arm_smmu_domain *smmu_domain)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 9/9 iommu/arm-smmu-v3: Adopt the Event queue across a Live Update
In reply to Pranjal ShrivastavaThe SMMU may record events of the preserved devices across the kexec, which the incoming kernel loses by resetting the EVTQ.
Preserve the EVTQ memory and leave the EVTQ enabled on shutdown. Only store its preservation token in the ABI, as the incoming kernel reads the base, size, PROD and CONS back from the EVTQ registers. Retain EVTQEN across the reset and wake up the EVTQ thread to handle pending events.
--- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 108 +++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 39 +++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 ++ include/linux/kho/abi/iommu.h | 2 + 4 files changed, 143 insertions(+), 16 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c +103 -5
@@ -403,11 +403,48 @@ static void arm_smmu_unpreserve_strtab_linear(struct arm_smmu_device *smmu, iommu_ser->smmuv3.l1_strtab_lu_state); } +static size_t arm_smmu_evtq_size (struct arm_smmu_device *smmu) +{ +return ((1 << smmu->evtq.q.llq.max_n_shift) *EVTQ_ENT_DWORDS ) <<3 ; +} + +/* The EVTQ stays enabled across the kexec */ +static int arm_smmu_preserve_evtq (struct arm_smmu_device *smmu, +struct iommu_hw_ser *iommu_ser) +{ +struct arm_smmu_queue *q = &smmu->evtq.q; + + iommu_ser->smmuv3.evtq_lu_state =0 ; +if (!(smmu->features &ARM_SMMU_FEAT_EVTQ )) +return 0 ; + +return dmam_preserve_coherent_allocation (smmu->dev, q->base, +arm_smmu_evtq_size (smmu), + q->base_dma, + &iommu_ser->smmuv3.evtq_lu_state); +} + +static void arm_smmu_unpreserve_evtq (struct arm_smmu_device *smmu, +struct iommu_hw_ser *iommu_ser) +{ +struct arm_smmu_queue *q = &smmu->evtq.q; + +if (!iommu_ser->smmuv3.evtq_lu_state) +return ; + +dmam_unpreserve_coherent_allocation (smmu->dev, q->base, +arm_smmu_evtq_size (smmu), + q->base_dma, + iommu_ser->smmuv3.evtq_lu_state); + iommu_ser->smmuv3.evtq_lu_state =0 ; +} +int arm_smmu_preserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser) {struct arm_smmu_device *smmu = container_of(iommu, struct arm_smmu_device, iommu); +int ret;/* Basic info */ iommu_ser->smmuv3.phys_addr = smmu->base_phys;
@@ -416,11 +453,20 @@ int arm_smmu_preserve(struct iommu_device *iommu, iommu_ser->smmuv3.strtab_base_cfg =readl_relaxed (smmu->base +ARM_SMMU_STRTAB_BASE_CFG ); + ret =arm_smmu_preserve_evtq (smmu, iommu_ser); +if (ret) { +dev_err (smmu->dev,"EVTQ preservation failed\n" ); +return ret; + } +/* We always implements 2-level when supported by HW */ if (smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ) -return arm_smmu_preserve_strtab_2lvl (smmu, iommu_ser); + ret =arm_smmu_preserve_strtab_2lvl (smmu, iommu_ser);else -return arm_smmu_preserve_strtab_linear (smmu, iommu_ser); + ret =arm_smmu_preserve_strtab_linear (smmu, iommu_ser); +if (ret) +arm_smmu_unpreserve_evtq (smmu, iommu_ser); +return ret; } void arm_smmu_unpreserve(struct iommu_device *iommu,
@@ -433,6 +479,7 @@ void arm_smmu_unpreserve(struct iommu_device *iommu,arm_smmu_unpreserve_strtab_2lvl (smmu, iommu_ser); elsearm_smmu_unpreserve_strtab_linear (smmu ,iommu_ser ); +arm_smmu_unpreserve_evtq (smmu, iommu_ser); } static void arm_smmu_liveupdate_clear_l1_std(struct arm_smmu_device *smmu,
@@ -545,12 +592,12 @@ int arm_smmu_liveupdate_shutdown(struct arm_smmu_device *smmu)* TODO: Quiesce the CMDQV VCMDQs assigned to guests. */ -/* Disable the queues, leaving SMMUEN set for the preserved masters */ +/* The incoming kernel resets the CMDQ and PRIQ and adopts the EVTQ */ cr0 =readl_relaxed (smmu->base +ARM_SMMU_CR0 ); - cr0 &= ~(CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ); + cr0 &= ~(CR0_CMDQEN |CR0_PRIQEN ); ret =arm_smmu_write_reg_sync (smmu, cr0,ARM_SMMU_CR0 ,ARM_SMMU_CR0ACK );if (ret) -dev_err (smmu->dev,"failed to disable queues\n" ); +dev_err (smmu->dev,"failed to disable CMDQ/PRIQ\n" );return ret; }
@@ -696,6 +743,57 @@ int arm_smmu_liveupdate_restore_strtab(struct arm_smmu_device *smmu)return 0 ; } +/* Adopt the live EVTQ. Returns -ENOENT if it wasn't preserved */ +int arm_smmu_liveupdate_restore_evtq (struct arm_smmu_device *smmu) +{ +u64 base =readq_relaxed (smmu->base +ARM_SMMU_EVTQ_BASE ); +u32 log2size =FIELD_GET (Q_BASE_LOG2SIZE , base); +struct arm_smmu_queue *q = &smmu->evtq.q; +struct iommu_hw_ser *iommu_ser; + + iommu_ser =iommu_get_preserved_data (smmu->base_phys,IOMMU_ARM_SMMUV3 ); +if (!iommu_ser || !iommu_ser->smmuv3.evtq_lu_state) +return -ENOENT ; + +if (log2size > q->llq.max_n_shift) { +dev_err (smmu->dev,"preserved EVTQ is larger than supported\n" ); +return -EINVAL ; + } + q->llq.max_n_shift = log2size; + + q->base =dmam_restore_coherent_allocation (smmu->dev, +arm_smmu_evtq_size (smmu), &q->base_dma,GFP_KERNEL , + iommu_ser->smmuv3.evtq_lu_state); +if (!q->base) +return -ENOMEM ; + +if (q->base_dma != (base &Q_BASE_ADDR_MASK )) { +dev_err (smmu->dev,"EVTQ_BASE doesn't match the preserved EVTQ\n" ); +return -EINVAL ; + } + + q->prod_reg = smmu->page1 +ARM_SMMU_EVTQ_PROD ; + q->cons_reg = smmu->page1 +ARM_SMMU_EVTQ_CONS ; + q->ent_dwords =EVTQ_ENT_DWORDS ; + q->q_base = base; + + q->llq.prod =readl_relaxed (q->prod_reg); + q->llq.cons =readl_relaxed (q->cons_reg); + +dev_info (smmu->dev,"restored preserved evtq (%u entries)\n" , +1 << q->llq.max_n_shift); +return 0 ; +} + +/* A failed EVTQ restore fails the probe, so a preserved EVTQ implies live */ +bool arm_smmu_liveupdate_evtq_is_live (struct arm_smmu_device *smmu) +{ +struct iommu_hw_ser *iommu_ser; + + iommu_ser =iommu_get_preserved_data (smmu->base_phys,IOMMU_ARM_SMMUV3 ); +return iommu_ser && iommu_ser->smmuv3.evtq_lu_state; +} +int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master) {struct arm_smmu_device *smmu = master->smmu;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +28 -11
@@ -4683,10 +4683,13 @@ static int arm_smmu_init_queues(struct arm_smmu_device *smmu)/* evtq */ if (smmu->features &ARM_SMMU_FEAT_EVTQ ) { - ret =arm_smmu_init_one_queue (smmu, &smmu->evtq.q, smmu->page1, -ARM_SMMU_EVTQ_PROD , -ARM_SMMU_EVTQ_CONS , -EVTQ_ENT_DWORDS ,"evtq" ); + ret =arm_smmu_liveupdate_restore_evtq (smmu); +if (ret == -ENOENT ) + ret =arm_smmu_init_one_queue (smmu, &smmu->evtq.q, + smmu->page1, +ARM_SMMU_EVTQ_PROD , +ARM_SMMU_EVTQ_CONS , +EVTQ_ENT_DWORDS ,"evtq" );if (ret)return ret; }
@@ -5080,9 +5083,15 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)* Same for a Live Update restore. */ if (arm_smmu_strtab_is_live (smmu)) { +u32 qens =CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ; +dev_info (smmu->dev,"%s: retaining SMMUEN for in-flight DMA\n" ,is_kdump_kernel () ?"kdump" :"live update" ); - enables = reg & ~(CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ); + +/* An adopted EVTQ keeps running */ +if (arm_smmu_liveupdate_evtq_is_live (smmu)) + qens &= ~CR0_EVTQEN ; + enables = reg & ~qens; goto reset_queues; }
@@ -5173,12 +5182,15 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)/* Event queue */ if (smmu->features &ARM_SMMU_FEAT_EVTQ ) { -writeq_relaxed (smmu->evtq.q.q_base, - smmu->base +ARM_SMMU_EVTQ_BASE ); -writel_relaxed (smmu->evtq.q.llq.prod, - smmu->page1 +ARM_SMMU_EVTQ_PROD ); -writel_relaxed (smmu->evtq.q.llq.cons, - smmu->page1 +ARM_SMMU_EVTQ_CONS ); +/* An adopted EVTQ resumes from its live BASE/PROD/CONS */ +if (!arm_smmu_liveupdate_evtq_is_live (smmu)) { +writeq_relaxed (smmu->evtq.q.q_base, + smmu->base +ARM_SMMU_EVTQ_BASE ); +writel_relaxed (smmu->evtq.q.llq.prod, + smmu->page1 +ARM_SMMU_EVTQ_PROD ); +writel_relaxed (smmu->evtq.q.llq.cons, + smmu->page1 +ARM_SMMU_EVTQ_CONS ); + } enables |=CR0_EVTQEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
@@ -5228,6 +5240,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)return ret; } +/* Handle the events recorded across the Live Update */ +if (arm_smmu_liveupdate_evtq_is_live (smmu) && + (smmu->combined_irq || smmu->evtq.q.irq)) +irq_wake_thread (smmu->combined_irq ?: smmu->evtq.q.irq, smmu); +/* Enable the SMMU interface */ enables |=CR0_SMMUEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +10 -0
@@ -1214,6 +1214,8 @@ void arm_smmu_unpreserve(struct iommu_device *iommu,int arm_smmu_liveupdate_shutdown (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master); +int arm_smmu_liveupdate_restore_evtq (struct arm_smmu_device *smmu); +bool arm_smmu_liveupdate_evtq_is_live (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_attach_restored (struct arm_smmu_master *master,struct arm_smmu_domain *smmu_domain);#else
@@ -1229,6 +1231,14 @@ static inline int arm_smmu_liveupdate_restore_cd_tables(struct arm_smmu_master * {return 0 ; } +static inline int arm_smmu_liveupdate_restore_evtq (struct arm_smmu_device *smmu) +{ +return -ENOENT ; +} +static inline bool arm_smmu_liveupdate_evtq_is_live (struct arm_smmu_device *smmu) +{ +return false; +}static inline int arm_smmu_liveupdate_attach_restored (struct arm_smmu_master *master,struct arm_smmu_domain *smmu_domain)
include/linux/kho/abi/iommu.h +2 -0
@@ -208,12 +208,14 @@ struct iommu_intel_ser {* @l2_strtab_lu_states_phys: Physical pointer to an array of u64 LU state tokens * indexed by L1 index, 0 for L2 tables that aren't * preserved (0 if linear) +* @evtq_lu_state: Live update state token for the Event queue (0 if not preserved) */ struct iommu_smmuv3_hw_ser {u64 phys_addr;u64 strtab_base_cfg;u64 l1_strtab_lu_state;u64 l2_strtab_lu_states_phys; +u64 evtq_lu_state; } __packed;/**
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Robin Murphy
reviewerRe: [RFC PATCH v1 9/9] iommu/arm-smmu-v3: Adopt the Event queue across a Live Update
Quoted 2 lines · Pranjal Shrivastava
The SMMU may record events of the preserved devices across the kexec, which the incoming kernel loses by resetting the EVTQ.
Not really; events which matter, i.e. stalled faults, are held until the queue becomes writable again. The only events which could be lost would be those which represent unexpected programming errors left over from the previous kernel, which we really cannot do anything about other than say "well, that happened..." Plus there's already no guarantee that such events aren't lost if the queue becomes full or stops for any other reason, so do we really need the hassle here?
Tahsnk,
Robin.
Quoted 291 lines
Preserve the EVTQ memory and leave the EVTQ enabled on shutdown. Only store its preservation token in the ABI, as the incoming kernel reads the base, size, PROD and CONS back from the EVTQ registers. Retain EVTQEN across the reset and wake up the EVTQ thread to handle pending events.
Signed-off-by Pranjal Shrivastava <[email protected]>--- .../arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c | 108 +++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 39 +++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 ++ include/linux/kho/abi/iommu.h | 2 + 4 files changed, 143 insertions(+), 16 deletions(-)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-liveupdate.c +103
-5@@ -403,11 +403,48 @@ static void arm_smmu_unpreserve_strtab_linear(struct arm_smmu_device *smmu, iommu_ser->smmuv3.l1_strtab_lu_state); } +static size_t arm_smmu_evtq_size (struct arm_smmu_device *smmu) +{ +return ((1 << smmu->evtq.q.llq.max_n_shift) *EVTQ_ENT_DWORDS ) <<3 ; +} + +/* The EVTQ stays enabled across the kexec */ +static int arm_smmu_preserve_evtq (struct arm_smmu_device *smmu, +struct iommu_hw_ser *iommu_ser) +{ +struct arm_smmu_queue *q = &smmu->evtq.q; + + iommu_ser->smmuv3.evtq_lu_state =0 ; +if (!(smmu->features &ARM_SMMU_FEAT_EVTQ )) +return 0 ; + +return dmam_preserve_coherent_allocation (smmu->dev, q->base, +arm_smmu_evtq_size (smmu), + q->base_dma, + &iommu_ser->smmuv3.evtq_lu_state); +} + +static void arm_smmu_unpreserve_evtq (struct arm_smmu_device *smmu, +struct iommu_hw_ser *iommu_ser) +{ +struct arm_smmu_queue *q = &smmu->evtq.q; + +if (!iommu_ser->smmuv3.evtq_lu_state) +return ; + +dmam_unpreserve_coherent_allocation (smmu->dev, q->base, +arm_smmu_evtq_size (smmu), + q->base_dma, + iommu_ser->smmuv3.evtq_lu_state); + iommu_ser->smmuv3.evtq_lu_state =0 ; +} +int arm_smmu_preserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser) {struct arm_smmu_device *smmu = container_of(iommu, struct arm_smmu_device, iommu); +int ret;/* Basic info */ iommu_ser->smmuv3.phys_addr = smmu->base_phys;@@ -416,11 +453,20 @@ int arm_smmu_preserve(struct iommu_device *iommu, iommu_ser->smmuv3.strtab_base_cfg =readl_relaxed (smmu->base +ARM_SMMU_STRTAB_BASE_CFG ); + ret =arm_smmu_preserve_evtq (smmu, iommu_ser); +if (ret) { +dev_err (smmu->dev,"EVTQ preservation failed\n" ); +return ret; + } +/* We always implements 2-level when supported by HW */ if (smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ) -return arm_smmu_preserve_strtab_2lvl (smmu, iommu_ser); + ret =arm_smmu_preserve_strtab_2lvl (smmu, iommu_ser);else -return arm_smmu_preserve_strtab_linear (smmu, iommu_ser); + ret =arm_smmu_preserve_strtab_linear (smmu, iommu_ser); +if (ret) +arm_smmu_unpreserve_evtq (smmu, iommu_ser); +return ret; } void arm_smmu_unpreserve(struct iommu_device *iommu,@@ -433,6 +479,7 @@ void arm_smmu_unpreserve(struct iommu_device *iommu,arm_smmu_unpreserve_strtab_2lvl (smmu, iommu_ser); elsearm_smmu_unpreserve_strtab_linear (smmu ,iommu_ser ); +arm_smmu_unpreserve_evtq (smmu, iommu_ser); } static void arm_smmu_liveupdate_clear_l1_std(struct arm_smmu_device *smmu,@@ -545,12 +592,12 @@ int arm_smmu_liveupdate_shutdown(struct arm_smmu_device *smmu)* TODO: Quiesce the CMDQV VCMDQs assigned to guests. */ -/* Disable the queues, leaving SMMUEN set for the preserved masters */ +/* The incoming kernel resets the CMDQ and PRIQ and adopts the EVTQ */ cr0 =readl_relaxed (smmu->base +ARM_SMMU_CR0 ); - cr0 &= ~(CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ); + cr0 &= ~(CR0_CMDQEN |CR0_PRIQEN ); ret =arm_smmu_write_reg_sync (smmu, cr0,ARM_SMMU_CR0 ,ARM_SMMU_CR0ACK );if (ret) -dev_err (smmu->dev,"failed to disable queues\n" ); +dev_err (smmu->dev,"failed to disable CMDQ/PRIQ\n" );return ret; }@@ -696,6 +743,57 @@ int arm_smmu_liveupdate_restore_strtab(struct arm_smmu_device *smmu)return 0 ; } +/* Adopt the live EVTQ. Returns -ENOENT if it wasn't preserved */ +int arm_smmu_liveupdate_restore_evtq (struct arm_smmu_device *smmu) +{ +u64 base =readq_relaxed (smmu->base +ARM_SMMU_EVTQ_BASE ); +u32 log2size =FIELD_GET (Q_BASE_LOG2SIZE , base); +struct arm_smmu_queue *q = &smmu->evtq.q; +struct iommu_hw_ser *iommu_ser; + + iommu_ser =iommu_get_preserved_data (smmu->base_phys,IOMMU_ARM_SMMUV3 ); +if (!iommu_ser || !iommu_ser->smmuv3.evtq_lu_state) +return -ENOENT ; + +if (log2size > q->llq.max_n_shift) { +dev_err (smmu->dev,"preserved EVTQ is larger than supported\n" ); +return -EINVAL ; + } + q->llq.max_n_shift = log2size; + + q->base =dmam_restore_coherent_allocation (smmu->dev, +arm_smmu_evtq_size (smmu), &q->base_dma,GFP_KERNEL , + iommu_ser->smmuv3.evtq_lu_state); +if (!q->base) +return -ENOMEM ; + +if (q->base_dma != (base &Q_BASE_ADDR_MASK )) { +dev_err (smmu->dev,"EVTQ_BASE doesn't match the preserved EVTQ\n" ); +return -EINVAL ; + } + + q->prod_reg = smmu->page1 +ARM_SMMU_EVTQ_PROD ; + q->cons_reg = smmu->page1 +ARM_SMMU_EVTQ_CONS ; + q->ent_dwords =EVTQ_ENT_DWORDS ; + q->q_base = base; + + q->llq.prod =readl_relaxed (q->prod_reg); + q->llq.cons =readl_relaxed (q->cons_reg); + +dev_info (smmu->dev,"restored preserved evtq (%u entries)\n" , +1 << q->llq.max_n_shift); +return 0 ; +} + +/* A failed EVTQ restore fails the probe, so a preserved EVTQ implies live */ +bool arm_smmu_liveupdate_evtq_is_live (struct arm_smmu_device *smmu) +{ +struct iommu_hw_ser *iommu_ser; + + iommu_ser =iommu_get_preserved_data (smmu->base_phys,IOMMU_ARM_SMMUV3 ); +return iommu_ser && iommu_ser->smmuv3.evtq_lu_state; +} +int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master) {struct arm_smmu_device *smmu = master->smmu;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +28
-11@@ -4683,10 +4683,13 @@ static int arm_smmu_init_queues(struct arm_smmu_device *smmu)/* evtq */ if (smmu->features &ARM_SMMU_FEAT_EVTQ ) { - ret =arm_smmu_init_one_queue (smmu, &smmu->evtq.q, smmu->page1, -ARM_SMMU_EVTQ_PROD , -ARM_SMMU_EVTQ_CONS , -EVTQ_ENT_DWORDS ,"evtq" ); + ret =arm_smmu_liveupdate_restore_evtq (smmu); +if (ret == -ENOENT ) + ret =arm_smmu_init_one_queue (smmu, &smmu->evtq.q, + smmu->page1, +ARM_SMMU_EVTQ_PROD , +ARM_SMMU_EVTQ_CONS , +EVTQ_ENT_DWORDS ,"evtq" );if (ret)return ret; }@@ -5080,9 +5083,15 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)* Same for a Live Update restore. */ if (arm_smmu_strtab_is_live (smmu)) { +u32 qens =CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ; +dev_info (smmu->dev,"%s: retaining SMMUEN for in-flight DMA\n" ,is_kdump_kernel () ?"kdump" :"live update" ); - enables = reg & ~(CR0_CMDQEN |CR0_EVTQEN |CR0_PRIQEN ); + +/* An adopted EVTQ keeps running */ +if (arm_smmu_liveupdate_evtq_is_live (smmu)) + qens &= ~CR0_EVTQEN ; + enables = reg & ~qens; goto reset_queues; }@@ -5173,12 +5182,15 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)/* Event queue */ if (smmu->features &ARM_SMMU_FEAT_EVTQ ) { -writeq_relaxed (smmu->evtq.q.q_base, - smmu->base +ARM_SMMU_EVTQ_BASE ); -writel_relaxed (smmu->evtq.q.llq.prod, - smmu->page1 +ARM_SMMU_EVTQ_PROD ); -writel_relaxed (smmu->evtq.q.llq.cons, - smmu->page1 +ARM_SMMU_EVTQ_CONS ); +/* An adopted EVTQ resumes from its live BASE/PROD/CONS */ +if (!arm_smmu_liveupdate_evtq_is_live (smmu)) { +writeq_relaxed (smmu->evtq.q.q_base, + smmu->base +ARM_SMMU_EVTQ_BASE ); +writel_relaxed (smmu->evtq.q.llq.prod, + smmu->page1 +ARM_SMMU_EVTQ_PROD ); +writel_relaxed (smmu->evtq.q.llq.cons, + smmu->page1 +ARM_SMMU_EVTQ_CONS ); + } enables |=CR0_EVTQEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,@@ -5228,6 +5240,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)return ret; } +/* Handle the events recorded across the Live Update */ +if (arm_smmu_liveupdate_evtq_is_live (smmu) && + (smmu->combined_irq || smmu->evtq.q.irq)) +irq_wake_thread (smmu->combined_irq ?: smmu->evtq.q.irq, smmu); +/* Enable the SMMU interface */ enables |=CR0_SMMUEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +10
-0@@ -1214,6 +1214,8 @@ void arm_smmu_unpreserve(struct iommu_device *iommu,int arm_smmu_liveupdate_shutdown (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_strtab (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_restore_cd_tables (struct arm_smmu_master *master); +int arm_smmu_liveupdate_restore_evtq (struct arm_smmu_device *smmu); +bool arm_smmu_liveupdate_evtq_is_live (struct arm_smmu_device *smmu);int arm_smmu_liveupdate_attach_restored (struct arm_smmu_master *master,struct arm_smmu_domain *smmu_domain);#else @@ -1229,6 +1231,14 @@ static inline int arm_smmu_liveupdate_restore_cd_tables(struct arm_smmu_master * {return 0 ; } +static inline int arm_smmu_liveupdate_restore_evtq (struct arm_smmu_device *smmu) +{ +return -ENOENT ; +} +static inline bool arm_smmu_liveupdate_evtq_is_live (struct arm_smmu_device *smmu) +{ +return false; +}static inline int arm_smmu_liveupdate_attach_restored (struct arm_smmu_master *master,struct arm_smmu_domain *smmu_domain)include/linux/kho/abi/iommu.h +2
-0@@ -208,12 +208,14 @@ struct iommu_intel_ser {* @l2_strtab_lu_states_phys: Physical pointer to an array of u64 LU state tokens * indexed by L1 index, 0 for L2 tables that aren't * preserved (0 if linear) +* @evtq_lu_state: Live update state token for the Event queue (0 if not preserved) */ struct iommu_smmuv3_hw_ser {u64 phys_addr;u64 strtab_base_cfg;u64 l1_strtab_lu_state;u64 l2_strtab_lu_states_phys; +u64 evtq_lu_state; } __packed;/**