KVM: arm64: SMMUv3 driver for pKVM (trap and emulate)
Subsystems (5)
v5 cover KVM: arm64: SMMUv3 driver for pKVM (trap and emulate)
This is v5 of pKVM SMMUv3 support with trap and emulate
v1: Implements full fledged pv interface
https://lore.kernel.org/kvmarm/[email protected]/
v2: Implements full fledged pv interface (+ more features as evtq and s1)
https://lore.kernel.org/kvmarm/[email protected]/
v3: Only DMA isolation (using pv)
https://lore.kernel.org/kvmarm/[email protected]/
v4: trap and emulate
https://lore.kernel.org/all/[email protected]/
This series is based on the review feedback on v4 + some other improvements, most notably:
- Add hardening checks in MMIO donation [Will]
- Add missing CMOs for non-coherent SMMU emulation [Will]
- Rely on aux bus to probe the emulated SMMUs, and make the KVM driver a platform driver [Jason]
- Replace TLB invalidation macro with inline function [Will]
- Set carevout size from KConfig and cmdline instead of hooks [Will]
- Fix S2 TLB invalidation if SMMUs where disabled
- Re-work command queue emulation to avoid unnecessary MMIO writes to make it more efficient.
- Update GBPA emulation to reflect HW state
- Minor cleanups, file renames and rewording of commits
This series applies on iommu-next (includes recent kunit rework)
Design:
=======
Assumptions:
------------
One of the important points, is that this doesn’t emulate the full SMMUv3 architecture, but only the parts used by Linux kernel, that’s why enablement of this (ARM_SMMU_V3_PKVM) depends on (ARM_SMMU_V3=y) so we are sure of the driver behaviour.
Any new change in the driver will likely trigger a WARN_ON ending up in panic.
Most notable assumptions:
- Changing of stream table format/size or l2 pointers is not allowed after initialization.
- leaf=0 CFGI is not allowed
- CFGI_ALL with any value but 31 is not allowed
- Some commands which are not used are not allowed (ex CMD_TLBI_NH_ALL)
- Values set in ARM_SMMU_CR1 are hardcoded and don't change.
Emulation logic mainly targets:
1) Command Queue
----------------
At boot time, the hypervisor will allocate a shadow command queue (doesn’t need to match the host size) which then sets up in HW, then it will trap access to
i) ARM_SMMU_CMDQ_BASE
That can only be written when the cmdq is disabled. Then on enable, the hypervisor will put the host command queue in a shared state to avoid transition into the hypervisor or VMs. It will be unshared with the cmdq is disabled
ii) ARM_SMMU_CMDQ_PROD
Trigger emulation code, where the hypervisor will copy the commands between cons and prod, of the host queue and sanitise them (mostly WARNs if the host is malicious and issuing commands it shouldn’t) then eagerly consume them, updating the host cons.
iii) ARM_SMMU_CMDQ_CONS
No much logic, just return the emulated cons + error bits.
2) Stream table
---------------
Similar to the command queue, the first level is allocated at boot with max possible size, then the hypervisor will trap access to:
i) ARM_SMMU_STRTAB_BASE/ARM_SMMU_STRTAB_BASE_CFG: Keep track of the stream table to put it in a shared state.
On CFGI_STE, the hypervisor will read the STE in scope from the host copy, shadow L2 pointers if needed and attach stage-2.
3) GBPA
-------
The hypervisor will set GBPA to abort at boot, then any read from the host will return ABORT and writes are ignored.
If the host tries to clear GBPA, it will look like GBPA is refusing to update and time out.
Dealing with timers
-------------------
In another series Vincent adds some timer abstractions for tracing in the hypervisor, after checking and having a discussion with him, it seems there isn’t enough common base to justify having a dependency between the 2 series, but it’s possible which ever series lands first, the other one might need to adapt to it.
https://lore.kernel.org/all/[email protected]/
Bisectibility:
==============
I wrote the patches where most of them are bisectable at run time (so we can run with a prefix of the series till MMIO emulation, cmdq emulation, STE or full nested) that was very helpful in debugging, and I kept it like this to make debugging easier.
Constraints:
============
1) Discovery:
-------------
Only device trees are supported at the moment.
I don’t usually use ACPI, but I can look into adding that later. (not make this series bigger)
2) Errata:
----------
Some HW with both stage-1 and stage-2 but can’t run nested translation due to some errata, which makes the driver remove nesting for MMU_700, I believe this is too restrictive. At the moment KVM will use nesting if advertised. (Or we need other mechanism to exclude only the affected HW)
3) Shadow page table
--------------------
Uses page granularity (leaf) for memory, that’s because of the lack of split_block_unmap() logic. I am currently looking into the possibility of sharing page tables, if that turned complicated (as expected) it might be worth to re-add this logic
Boot and Probe ordering:
=======================
The main SMMUv3 MUST be only bound/probed after KVM fully initialises so it can set up the MMIO emulation.
The KVM SMMUv3 driver is loaded early before KVM init so it can register itself, during that point it will probe all the SMMUs from the platform bus and bind them to the driver.
Then at a later init call it will create an auxiliary device per SMMU, that the main driver will probe. The main driver still relies on this device(parent) for all driver activity. (Check comment in patch 14.
Future work
===========
1) Sharing page tables will be an interesting optimization, but requires dealing with stage-2 page faults (which are handled by the kernel), BBM and possibly more complexity.
2) There is currently ongoing work to enable RPM, that will possibly enable/disable the SMMU frequently, we might need some optimizations to avoid re-shadowing the CMDQ/STE unnecessarily.
3) Look into ACPI support.
4) Some optimizations (as using block mappings for memory)
Patches overview
=================
The patches are split as follows:
Patches 01-03: Core hypervisor: Add donation for NC, dealing with
MMIO and arch timer abstraction.
Patches 04-07: Refactoring of io-pgtable-arm and SMMUv3 driver Patches 09-11: Hypervisor IOMMU core: pagetable management, dabts.. Patches 12-27: KVM SMMUv3 code
Tested on Qemu(S1 only, S2 only and nested) and Morello board. Also tested with PAGE_SIZE 4k,16k, and 64k.
A development branch can be found in:
https://android-kvm.googlesource.com/linux/+/refs/heads/pkvm-smmu-v5
Jean-Philippe Brucker (1):
iommu/arm-smmu-v3-kvm: Add SMMUv3 driver
Mostafa Saleh (26):
KVM: arm64: Add a new function to donate memory with prot
KVM: arm64: Donate MMIO to the hypervisor
KVM: arm64: pkvm: Add pkvm_time_get() iommu/io-pgtable-arm: Factor kernel specific code out iommu/arm-smmu-v3: Split code with hyp iommu/arm-smmu-v3: Move TLB range invalidation into common code iommu/arm-smmu-v3: Move IDR parsing to common functions
KVM: arm64: iommu: Introduce IOMMU driver infrastructure
KVM: arm64: iommu: Shadow host stage-2 page table
KVM: arm64: iommu: Add memory pool
KVM: arm64: iommu: Support DABT for IOMMU iommu/arm-smmu-v3-kvm: Add the kernel driver iommu/arm-smmu-v3: Support probing KVM emulated devices iommu/arm-smmu-v3-kvm: Create array for hyp SMMUv3 iommu/arm-smmu-v3-kvm: Take over SMMUs iommu/arm-smmu-v3-kvm: Probe SMMU HW iommu/arm-smmu-v3-kvm: Add MMIO emulation iommu/arm-smmu-v3-kvm: Shadow the command queue iommu/arm-smmu-v3-kvm: Add CMDQ functions iommu/arm-smmu-v3-kvm: Emulate CMDQ for host iommu/arm-smmu-v3-kvm: Shadow stream table iommu/arm-smmu-v3-kvm: Shadow STEs iommu/arm-smmu-v3-kvm: Emulate GBPA iommu/arm-smmu-v3-kvm: Support io-pgtable iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table iommu/arm-smmu-v3-kvm: Enable nesting
.../admin-guide/kernel-parameters.txt | 4 + arch/arm64/include/asm/kvm_arm.h | 2 + arch/arm64/include/asm/kvm_host.h | 6 + arch/arm64/kvm/Kconfig | 7 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/hyp/include/nvhe/iommu.h | 21 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 3 + arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 2 + arch/arm64/kvm/hyp/nvhe/Makefile | 10 +- arch/arm64/kvm/hyp/nvhe/iommu/iommu.c | 130 ++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 116 +- arch/arm64/kvm/hyp/nvhe/setup.c | 23 + arch/arm64/kvm/hyp/nvhe/timer-sr.c | 32 + arch/arm64/kvm/hyp/pgtable.c | 9 +- arch/arm64/kvm/iommu.c | 44 + arch/arm64/kvm/pkvm.c | 1 + drivers/iommu/Makefile | 2 +- drivers/iommu/arm/Kconfig | 9 + drivers/iommu/arm/arm-smmu-v3/Makefile | 3 +- .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c | 114 ++ .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c | 190 +++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 400 ++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 254 ++++ .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 1068 +++++++++++++++++ .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h | 65 + .../arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.c | 68 ++ drivers/iommu/io-pgtable-arm-kernel.c | 103 ++ drivers/iommu/io-pgtable-arm.c | 103 +- drivers/iommu/io-pgtable-arm.h | 30 + 29 files changed, 2402 insertions(+), 419 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c create mode 100644 arch/arm64/kvm/iommu.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.c create mode 100644 drivers/iommu/io-pgtable-arm-kernel.c
base-commit: 3ee8acab4e5038a261a72ea2e6035cff89168010
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 1/27 KVM: arm64: Add a new function to donate memory with prot
Soon, IOMMU drivers running in the hypervisor might interact with non-coherent devices, so it needs a mechanism to map memory as non cacheable.
Add ___pkvm_host_donate_hyp() which accepts a new argument for prot, so the driver can add KVM_PGTABLE_PROT_NORMAL_NC.
--- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -0
@@ -36,6 +36,7 @@ int __pkvm_prot_finalize(void);int __pkvm_host_share_hyp (u64 pfn);int __pkvm_host_unshare_hyp (u64 pfn);int __pkvm_host_donate_hyp (u64 pfn,u64 nr_pages); +int ___pkvm_host_donate_hyp (u64 pfn,u64 nr_pages,enum kvm_pgtable_prot prot);int __pkvm_hyp_donate_host (u64 pfn,u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);
arch/arm64/kvm/hyp/nvhe/mem_protect.c +9 -2
@@ -769,13 +769,15 @@ int __pkvm_host_unshare_hyp(u64 pfn)return ret; } -int __pkvm_host_donate_hyp (u64 pfn,u64 nr_pages) +int ___pkvm_host_donate_hyp (u64 pfn,u64 nr_pages,enum kvm_pgtable_prot prot) {u64 phys =hyp_pfn_to_phys (pfn);u64 size =PAGE_SIZE * nr_pages;void *virt =__hyp_va (phys);int ret; +WARN_ON ((prot &KVM_PGTABLE_PROT_RWX ) !=KVM_PGTABLE_PROT_RW ); +host_lock_component ();hyp_lock_component ();
@@ -787,7 +789,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) goto unlock;__hyp_set_page_state_range (phys, size,PKVM_PAGE_OWNED ); -WARN_ON (pkvm_create_mappings_locked (virt, virt + size,PAGE_HYP )); +WARN_ON (pkvm_create_mappings_locked (virt, virt + size, prot));WARN_ON (host_stage2_set_owner_locked (phys, size,PKVM_ID_HYP )); unlock:
@@ -797,6 +799,11 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)return ret; } +int __pkvm_host_donate_hyp (u64 pfn,u64 nr_pages) +{ +return ___pkvm_host_donate_hyp (pfn, nr_pages,PAGE_HYP ); +} +int __pkvm_hyp_donate_host (u64 pfn,u64 nr_pages) {u64 phys =hyp_pfn_to_phys (pfn);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 2/27 KVM: arm64: Donate MMIO to the hypervisor
In reply to Mostafa SalehAdd a function to donate MMIO to the hypervisor so IOMMU hypervisor drivers can use that to protect the MMIO of IOMMU.
The initial attempt to implement this was to have a new flag to "___pkvm_host_donate_hyp" to accept MMIO. However that had many problems, it was quite intrusive for host/hyp to check/set page state to make it aware of MMIO and to encode the state in the page table in that case. Which is called in paths that can be sensitive to performance (FFA, VMs..)
As donating MMIO is very rare, and we don’t need to encode the full state, it’s reasonable to have a separate function to do this.
It will init the host s2 page table with an invalid leaf with the owner ID to prevent the host from mapping the page on faults.
Also, prevent kvm_pgtable_stage2_unmap() from removing owner ID from stage-2 PTEs, as this can be triggered from recycle logic under memory pressure. There is no code relying on this, as all ownership changes is done via kvm_pgtable_stage2_set_owner()
For error path in IOMMU drivers, add a function to donate MMIO back from hyp to host.
--- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 2 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 90 +++++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 9 +- 3 files changed, 94 insertions(+), 7 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -0
@@ -37,6 +37,8 @@ int __pkvm_host_share_hyp(u64 pfn);int __pkvm_host_unshare_hyp (u64 pfn);int __pkvm_host_donate_hyp (u64 pfn,u64 nr_pages);int ___pkvm_host_donate_hyp (u64 pfn,u64 nr_pages,enum kvm_pgtable_prot prot); +int __pkvm_host_donate_hyp_mmio (u64 pfn); +int __pkvm_hyp_donate_host_mmio (u64 pfn);int __pkvm_hyp_donate_host (u64 pfn,u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);
arch/arm64/kvm/hyp/nvhe/mem_protect.c +90 -0
96 lines not on this page · raw message
arch/arm64/kvm/hyp/pgtable.c +2 -7
@@ -1095,13 +1095,8 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,kvm_pte_t *childp =NULL ;bool need_flush = false; -if (!kvm_pte_valid (ctx->old)) { -if (stage2_pte_is_counted (ctx->old)) { -kvm_clear_pte (ctx->ptep); - mm_ops->put_page (ctx->ptep); - } -return 0 ; - } +if (!kvm_pte_valid (ctx->old)) +return stage2_pte_is_counted (ctx->old) ? -EPERM :0 ;if (kvm_pte_table (ctx->old, ctx->level)) { childp =kvm_pte_follow (ctx->old, mm_ops);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 3/27 KVM: arm64: pkvm: Add pkvm_time_get()
In reply to Mostafa SalehAdd a function to return time in us.
This can be used from IOMMU drivers while waiting for conditions as for SMMUv3 TLB invalidation waiting for sync.
--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 2 ++ arch/arm64/kvm/hyp/nvhe/setup.c | 4 ++++ arch/arm64/kvm/hyp/nvhe/timer-sr.c | 32 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+)
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +2 -0
@@ -89,4 +89,6 @@ bool kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu, u64 *exit_code);void kvm_init_pvm_id_regs (struct kvm_vcpu *vcpu);int kvm_check_pvm_sysreg_table (void ); +int pkvm_timer_init (void ); +u64 pkvm_time_get (void );#endif /* __ARM64_KVM_NVHE_PKVM_H__ */
arch/arm64/kvm/hyp/nvhe/setup.c +4 -0
@@ -312,6 +312,10 @@ void __noreturn __pkvm_init_finalise(void) }; pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops; + ret =pkvm_timer_init (); +if (ret) + goto out; + ret =fix_host_ownership ();if (ret) goto out;
arch/arm64/kvm/hyp/nvhe/timer-sr.c +32 -0
@@ -11,6 +11,10 @@#include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> +#include <nvhe/pkvm.h> + +static u32 timer_freq; +void __kvm_timer_set_cntvoff (u64 cntvoff) {write_sysreg (cntvoff, cntvoff_el2);
@@ -68,3 +72,31 @@ void __timer_enable_traps(struct kvm_vcpu *vcpu)sysreg_clear_set (cnthctl_el2, clr, set); } + +static u64 pkvm_ticks_get (void ) +{ +return __arch_counter_get_cntvct (); +} + +#define SEC_TO_US 1000000 + +int pkvm_timer_init (void ) +{ + timer_freq =read_sysreg (cntfrq_el0); + +/* +* KVM will not initialize if FW didn't set cntfrq_el0, that is already +* part of the boot protocol. +*/ +if (!timer_freq || timer_freq <SEC_TO_US ) +return -ENODEV ; +return 0 ; +} + +#define pkvm_time_ticks_to_us (ticks) ((u64)(ticks) * SEC_TO_US / timer_freq) + +/* Return time in us. */ +u64 pkvm_time_get (void ) +{ +return pkvm_time_ticks_to_us (pkvm_ticks_get ()); +}
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 4/27 iommu/io-pgtable-arm: Factor kernel specific code out
In reply to Mostafa SalehSome of the used APIs are only part of the kernel and are not available in the hypervisor, factor those out:
- alloc/free memory
- CMOs
- virt/phys conversions
Which are implemented by the kernel in io-pgtable-arm-kernel.c and similarly for the hypervisor later in this series.
va/pa conversion kept as macros.
--- drivers/iommu/Makefile | 2 +- drivers/iommu/io-pgtable-arm-kernel.c | 103 ++++++++++++++++++++++++++ drivers/iommu/io-pgtable-arm.c | 101 +++---------------------- drivers/iommu/io-pgtable-arm.h | 19 +++++ 4 files changed, 133 insertions(+), 92 deletions(-) create mode 100644 drivers/iommu/io-pgtable-arm-kernel.c
drivers/iommu/Makefile +1 -1
@@ -12,7 +12,7 @@ obj-$(CONFIG_IOMMU_DEBUGFS) += iommu-debugfs.o obj-$(CONFIG_IOMMU_DMA) += dma-iommu.o obj-$(CONFIG_IOMMU_IO_PGTABLE) += io-pgtable.o obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o -obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o +obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o io-pgtable-arm-kernel.o obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST) += io-pgtable-arm-selftests.o obj-$(CONFIG_IOMMU_IO_PGTABLE_DART) += io-pgtable-dart.o obj-$(CONFIG_IOMMU_IOVA) += iova.o
drivers/iommu/io-pgtable-arm-kernel.c new file +103 -0
103 lines not on this page · raw message
drivers/iommu/io-pgtable-arm.c +10 -91
177 lines not on this page · raw message
drivers/iommu/io-pgtable-arm.h +19 -0
@@ -2,6 +2,8 @@#ifndef IO_PGTABLE_ARM_H_ #define IO_PGTABLE_ARM_H_ +#include <linux/io-pgtable.h> +#define ARM_LPAE_TCR_TG0_4K 0#define ARM_LPAE_TCR_TG0_64K 1#define ARM_LPAE_TCR_TG0_16K 2
@@ -27,4 +29,21 @@#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL#define ARM_LPAE_TCR_PS_52_BIT 0x6ULL +typedef u64 arm_lpae_iopte ; + +void __arm_lpae_sync_pte (arm_lpae_iopte *ptep,int num_entries, +struct io_pgtable_cfg *cfg); +void __arm_lpae_free_pages (void *pages,size_t size, +struct io_pgtable_cfg *cfg, +void *cookie); +void *__arm_lpae_alloc_pages (size_t size,gfp_t gfp, +struct io_pgtable_cfg *cfg, +void *cookie); +void *__arm_lpae_alloc_data (size_t size,gfp_t gfp); +void __arm_lpae_free_data (void *p); +#ifndef __KVM_NVHE_HYPERVISOR__ +#define __arm_lpae_virt_to_phys __pa +#define __arm_lpae_phys_to_virt __va +#endif /* !__KVM_NVHE_HYPERVISOR__ */ +#endif /* IO_PGTABLE_ARM_H_ */
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 04/27] iommu/io-pgtable-arm: Factor kernel specific code out
Quoted 3 lines · Mostafa Saleh
Some of the used APIs are only part of the kernel and are not available in the hypervisor, factor those out:
- alloc/free memory
Why not provide the iommu-pages API for the hypervisor environment?
Same for virt_to_phys, that could be moved into an iommu-pages wrapper too..
I want to change other parts of the driver to use iommu-pages in the long run as well, so putting the abstraction there is probably more valuable than this.
Also the genericpt stuff is merged, should you (eventually?) be making a pKVM hypervisor specific set of page table functions? Eg if all you are doing is mirroring the host stage 2 I think you can build something much more efficient...
Jason
Re: [PATCH v5 04/27] iommu/io-pgtable-arm: Factor kernel specific code out
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Mostafa Saleh
Some of the used APIs are only part of the kernel and are not available in the hypervisor, factor those out:
- alloc/free memoryWhy not provide the iommu-pages API for the hypervisor environment?
Same for
virt_to_phys, that could be moved into an iommu-pages wrapper too..
I guess that's possible, but then we would have to stub dma_map/unmap, which might a bit ugly. I will look more into it.
Quoted 9 lines
I want to change other parts of the driver to use iommu-pages in the long run as well, so putting the abstraction there is probably more valuable than this.
Also the genericpt stuff is merged, should you (eventually?) be making a pKVM hypervisor specific set of page table functions? Eg if all you are doing is mirroring the host stage 2 I think you can build something much more efficient...
Most of the code is re-used from the io-pgtable-arm, so when that is converted to genericpt, it should get to the hypervisor also. The only hypervisor specific logic is the memory allocation, virt/phys conversion and CMOs, I can look more into painting those, we don’t have to change much.
I have some plans to add support for sharing the CPU stage-2 page table, which is going to be complicated as it requires changes to the core hypervisor hyp/pgtable.c code, which will be in a subsequent series. However, I reckon that we would keep the shadowing logic as not all SMMUv3/platforms are capable of sharing page tables.
Thanks,
Mostafa
Quoted 3 lines
Jason
Re: [PATCH v5 04/27] iommu/io-pgtable-arm: Factor kernel specific code out
Quoted 6 lines · Mostafa Saleh
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Mostafa Saleh
Some of the used APIs are only part of the kernel and are not available in the hypervisor, factor those out:
- alloc/free memory
Quoted 7 lines
Quoted 4 lines
Why not provide the iommu-pages API for the hypervisor environment?
Same for
virt_to_phys, that could be moved into an iommu-pages wrapper too..I guess that's possible, but then we would have to stub dma_map/unmap, which might a bit ugly. I will look more into it.
I am hoping to drop the dma_map/unmap and replace it with an arch cache flush call directly since the code is no longer modular and relying on the DMA API will have some problems with a future patch series I'm expecting..
Jason
Re: [PATCH v5 04/27] iommu/io-pgtable-arm: Factor kernel specific code out
Quoted 12 lines · Jason Gunthorpe
Quoted 11 lines · Mostafa Saleh
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Mostafa Saleh
Some of the used APIs are only part of the kernel and are not available in the hypervisor, factor those out:
- alloc/free memoryWhy not provide the iommu-pages API for the hypervisor environment?
Same for
virt_to_phys, that could be moved into an iommu-pages wrapper too..
Quoted 8 lines
Quoted 2 lines
I guess that's possible, but then we would have to stub dma_map/unmap, which might a bit ugly. I will look more into it.
I am hoping to drop the dma_map/unmap and replace it with an arch cache flush call directly since the code is no longer modular and relying on the DMA API will have some problems with a future patch series I'm expecting..
I see, I always wondered about that, it's because CMO are not exported for modules, I can look more into it and send a separate clean up.
Thanks,
Mostafa
Quoted 1 line
Jason
PATCH v5 5/27 iommu/arm-smmu-v3: Split code with hyp
In reply to Mostafa SalehThe KVM SMMUv3 driver would re-use some of the cmdq code inside the hypervisor, move these functions to a new common c file that is shared between the host kernel and the hypervisor.
--- drivers/iommu/arm/arm-smmu-v3/Makefile | 2 +- .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c | 114 +++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 161 ------------------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 61 +++++++ 4 files changed, 176 insertions(+), 162 deletions(-) create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c
drivers/iommu/arm/arm-smmu-v3/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_ARM_SMMU_V3) += arm_smmu_v3.o -arm_smmu_v3-y := arm-smmu-v3.o +arm_smmu_v3-y := arm-smmu-v3.o arm-smmu-v3-common-lib.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_TEGRA241_CMDQV) += tegra241-cmdqv.o
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c new file +114 -0
114 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -161
197 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +61 -0
67 lines not on this page · raw message
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 05/27] iommu/arm-smmu-v3: Split code with hyp
Quoted 10 lines · Mostafa Saleh
The KVM SMMUv3 driver would re-use some of the cmdq code inside the hypervisor, move these functions to a new common c file that is shared between the host kernel and the hypervisor.
Signed-off-by Mostafa Saleh <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/Makefile | 2 +- .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c | 114 +++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 161 ------------------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 61 +++++++
I would think these inlines should go in some -common.h instead of arm-smmu-v3.h for better clarity, and ideally pkvm stuff does not include arm-smmu-v3.h at all?
Jason
Re: [PATCH v5 05/27] iommu/arm-smmu-v3: Split code with hyp
Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Mostafa Saleh
The KVM SMMUv3 driver would re-use some of the cmdq code inside the hypervisor, move these functions to a new common c file that is shared between the host kernel and the hypervisor.
Signed-off-by Mostafa Saleh <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/Makefile | 2 +- .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c | 114 +++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 161 ------------------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 61 +++++++
Quoted 3 lines
I would think these inlines should go in some
-common.hinstead ofarm-smmu-v3.hfor better clarity, and ideally pkvm stuff does not includearm-smmu-v3.hat all?
My thought was that we use “arm-smmu-v3.h” to avoid moving a lot of code and messing up the git history, but I have no strong opinion, splitting is fine also.
Thanks,
Mostafa
Quoted 2 lines
Jason
PATCH v5 6/27 iommu/arm-smmu-v3: Move TLB range invalidation into common code
In reply to Mostafa SalehRange TLB invalidation has a very specific algorithm, instead of re-writing it for the hypervisor.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 69 ++++-------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 79 +++++++++++++++++++++ 2 files changed, 92 insertions(+), 56 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13 -56
@@ -2105,74 +2105,31 @@ static void arm_smmu_tlb_inv_context(void *cookie)arm_smmu_atc_inv_domain (smmu_domain,0 ,0 ); } +static void __arm_smmu_cmdq_batch_add (void *__opaque, +struct arm_smmu_cmdq_batch *cmds, +struct arm_smmu_cmdq_ent *cmd) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)__opaque; + +arm_smmu_cmdq_batch_add (smmu, cmds, cmd); +} +static void __arm_smmu_tlb_inv_range (struct arm_smmu_cmdq_ent *cmd,unsigned long iova,size_t size,size_t granule,struct arm_smmu_domain *smmu_domain) {struct arm_smmu_device *smmu = smmu_domain->smmu; -unsigned long end = iova + size, num_pages =0 , tg =0 ; -size_t inv_range = granule;struct arm_smmu_cmdq_batch cmds;if (!size)return ; -if (smmu->features &ARM_SMMU_FEAT_RANGE_INV ) { -/* Get the leaf page size */ - tg =__ffs (smmu_domain->domain.pgsize_bitmap); - - num_pages = size >> tg; - -/* Convert page size of 12,14,16 (log2) to 1,2,3 */ - cmd->tlbi.tg = (tg -10 ) /2 ; - -/* -* Determine what level the granule is at. For non-leaf, both -* io-pgtable and SVA pass a nominal last-level granule because -* they don't know what level(s) actually apply, so ignore that -* and leave TTL=0. However for various errata reasons we still -* want to use a range command, so avoid the SVA corner case -* where both scale and num could be 0 as well. -*/ -if (cmd->tlbi.leaf) - cmd->tlbi.ttl =4 - ((ilog2 (granule) -3 ) / (tg -3 )); -else if ((num_pages &CMDQ_TLBI_RANGE_NUM_MAX ) ==1 ) - num_pages++; - } -arm_smmu_cmdq_batch_init (smmu, &cmds, cmd); - -while (iova < end) { -if (smmu->features &ARM_SMMU_FEAT_RANGE_INV ) { -/* -* On each iteration of the loop, the range is 5 bits -* worth of the aligned size remaining. -* The range in pages is: -* -* range = (num_pages & (0x1f << __ffs(num_pages))) -*/ -unsigned long scale, num; - -/* Determine the power of 2 multiple number of pages */ - scale =__ffs (num_pages); - cmd->tlbi.scale = scale; - -/* Determine how many chunks of 2^scale size we have */ - num = (num_pages >> scale) &CMDQ_TLBI_RANGE_NUM_MAX ; - cmd->tlbi.num = num -1 ; - -/* range is num * 2^scale * pgsize */ - inv_range = num << (scale + tg); - -/* Clear out the lower order bits for the next iteration */ - num_pages -= num << scale; - } - - cmd->tlbi.addr = iova; -arm_smmu_cmdq_batch_add (smmu, &cmds, cmd); - iova += inv_range; - } +arm_smmu_tlb_inv_build (cmd, iova, size, granule, + smmu_domain->domain.pgsize_bitmap, + smmu->features &ARM_SMMU_FEAT_RANGE_INV , + smmu, __arm_smmu_cmdq_batch_add, &cmds);arm_smmu_cmdq_batch_submit (smmu, &cmds); }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +79 -0
85 lines not on this page · raw message
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 7/27 iommu/arm-smmu-v3: Move IDR parsing to common functions
In reply to Mostafa SalehMove parsing of IDRs to functions so that it can be re-used from the hypervisor.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 112 +++----------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 111 +++++++++++++++++++ 2 files changed, 126 insertions(+), 97 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +15 -97
@@ -4109,57 +4109,17 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)/* IDR0 */ reg =readl_relaxed (smmu->base +ARM_SMMU_IDR0 ); -/* 2-level structures */ -if (FIELD_GET (IDR0_ST_LVL , reg) ==IDR0_ST_LVL_2LVL ) - smmu->features |=ARM_SMMU_FEAT_2_LVL_STRTAB ; - -if (reg &IDR0_CD2L ) - smmu->features |=ARM_SMMU_FEAT_2_LVL_CDTAB ; - -/* -* Translation table endianness. -* We currently require the same endianness as the CPU, but this -* could be changed later by adding a new IO_PGTABLE_QUIRK. -*/ -switch (FIELD_GET (IDR0_TTENDIAN , reg)) { -case IDR0_TTENDIAN_MIXED : - smmu->features |=ARM_SMMU_FEAT_TT_LE |ARM_SMMU_FEAT_TT_BE ; -break ; -#ifdef __BIG_ENDIAN -case IDR0_TTENDIAN_BE : - smmu->features |=ARM_SMMU_FEAT_TT_BE ; -break ; -#else -case IDR0_TTENDIAN_LE : - smmu->features |=ARM_SMMU_FEAT_TT_LE ; -break ; -#endif -default : + smmu->features |=smmu_idr0_features (reg); +if (FIELD_GET (IDR0_TTENDIAN , reg) ==IDR0_TTENDIAN_RESERVED ) {dev_err (smmu->dev,"unknown/unsupported TT endianness!\n" );return -ENXIO ; } - -/* Boolean feature flags */ -if (IS_ENABLED (CONFIG_PCI_PRI ) && reg &IDR0_PRI ) - smmu->features |=ARM_SMMU_FEAT_PRI ; - -if (IS_ENABLED (CONFIG_PCI_ATS ) && reg &IDR0_ATS ) - smmu->features |=ARM_SMMU_FEAT_ATS ; - -if (reg &IDR0_SEV ) - smmu->features |=ARM_SMMU_FEAT_SEV ; - -if (reg &IDR0_MSI ) { - smmu->features |=ARM_SMMU_FEAT_MSI ; -if (coherent && !disable_msipolling) - smmu->options |=ARM_SMMU_OPT_MSIPOLL ; - } - -if (reg &IDR0_HYP ) { - smmu->features |=ARM_SMMU_FEAT_HYP ; -if (cpus_have_cap (ARM64_HAS_VIRT_HOST_EXTN )) - smmu->features |=ARM_SMMU_FEAT_E2H ; - } +if (coherent && !disable_msipolling && + smmu->features &ARM_SMMU_FEAT_MSI ) + smmu->options |=ARM_SMMU_OPT_MSIPOLL ; +if (smmu->features &ARM_SMMU_FEAT_HYP && +cpus_have_cap (ARM64_HAS_VIRT_HOST_EXTN )) + smmu->features |=ARM_SMMU_FEAT_E2H ;arm_smmu_get_httu (smmu, reg);
@@ -4171,21 +4131,7 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)dev_warn (smmu->dev,"IDR0.COHACC overridden by FW configuration (%s)\n" ,str_true_false (coherent)); -switch (FIELD_GET (IDR0_STALL_MODEL , reg)) { -case IDR0_STALL_MODEL_FORCE : - smmu->features |=ARM_SMMU_FEAT_STALL_FORCE ; - fallthrough; -case IDR0_STALL_MODEL_STALL : - smmu->features |=ARM_SMMU_FEAT_STALLS ; - } - -if (reg &IDR0_S1P ) - smmu->features |=ARM_SMMU_FEAT_TRANS_S1 ; - -if (reg &IDR0_S2P ) - smmu->features |=ARM_SMMU_FEAT_TRANS_S2 ; - -if (!(reg & (IDR0_S1P |IDR0_S2P ))) { +if (!(smmu->features & (ARM_SMMU_FEAT_TRANS_S1 |ARM_SMMU_FEAT_TRANS_S2 ))) {dev_err (smmu->dev,"no translation support!\n" );return -ENXIO ; }
@@ -4250,10 +4196,7 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)/* IDR3 */ reg =readl_relaxed (smmu->base +ARM_SMMU_IDR3 ); -if (FIELD_GET (IDR3_RIL , reg)) - smmu->features |=ARM_SMMU_FEAT_RANGE_INV ; -if (FIELD_GET (IDR3_FWB , reg)) - smmu->features |=ARM_SMMU_FEAT_S2FWB ; + smmu->features |=smmu_idr3_features (reg);if (FIELD_GET (IDR3_BBM , reg) ==2 ) smmu->features |=ARM_SMMU_FEAT_BBML2 ;
@@ -4265,43 +4208,18 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) smmu->evtq.max_stalls =FIELD_GET (IDR5_STALL_MAX , reg);/* Page sizes */ -if (reg &IDR5_GRAN64K ) - smmu->pgsize_bitmap |=SZ_64K |SZ_512M ; -if (reg &IDR5_GRAN16K ) - smmu->pgsize_bitmap |=SZ_16K |SZ_32M ; -if (reg &IDR5_GRAN4K ) - smmu->pgsize_bitmap |=SZ_4K |SZ_2M |SZ_1G ; + smmu->pgsize_bitmap =smmu_idr5_to_pgsize (reg);/* Input address size */ if (FIELD_GET (IDR5_VAX , reg) ==IDR5_VAX_52_BIT ) smmu->features |=ARM_SMMU_FEAT_VAX ; -/* Output address size */ -switch (FIELD_GET (IDR5_OAS , reg)) { -case IDR5_OAS_32_BIT : - smmu->oas =32 ; -break ; -case IDR5_OAS_36_BIT : - smmu->oas =36 ; -break ; -case IDR5_OAS_40_BIT : - smmu->oas =40 ; -break ; -case IDR5_OAS_42_BIT : - smmu->oas =42 ; -break ; -case IDR5_OAS_44_BIT : - smmu->oas =44 ; -break ; -case IDR5_OAS_52_BIT : - smmu->oas =52 ; + smmu->oas =smmu_idr5_to_oas (reg); +if (smmu->oas ==52 ) smmu->pgsize_bitmap |=1ULL <<42 ;/* 4TB */ -break ; -default : +else if (!smmu->oas) {dev_info (smmu->dev, -"unknown output address size. Truncating to 48-bit\n" ); - fallthrough; -case IDR5_OAS_48_BIT : +"unknown output address size. Truncating to 48-bit\n" ); smmu->oas =48 ; }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +111 -0
123 lines not on this page · raw message
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 07/27] iommu/arm-smmu-v3: Move IDR parsing to common functions
Quoted 8 lines · Mostafa Saleh
Move parsing of IDRs to functions so that it can be re-used from the hypervisor.
Signed-off-by Mostafa Saleh <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 112 +++----------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 111 +++++++++++++++++++ 2 files changed, 126 insertions(+), 97 deletions(-)
I don't see that this slow path stuff needs to be inlined?
+10 to my prior remark to not dump all the huge inlines in the main header.
Jason
Re: [PATCH v5 07/27] iommu/arm-smmu-v3: Move IDR parsing to common functions
Quoted 10 lines · Jason Gunthorpe
Quoted 8 lines · Mostafa Saleh
Move parsing of IDRs to functions so that it can be re-used from the hypervisor.
Signed-off-by Mostafa Saleh <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 112 +++----------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 111 +++++++++++++++++++ 2 files changed, 126 insertions(+), 97 deletions(-)
Quoted 4 lines
I don't see that this slow path stuff needs to be inlined?
+10 to my prior remark to not dump all the huge inlines in the main header.
They are inline as they are defined in the header file, otherwise they get defined multiple times.
I am ok with adding a new common file.
Thanks,
Mostafa
Quoted 2 lines
Jason
Re: [PATCH v5 07/27] iommu/arm-smmu-v3: Move IDR parsing to common functions
Quoted 11 lines · Mostafa Saleh
Quoted 10 lines · Jason Gunthorpe
Quoted 8 lines · Mostafa Saleh
Move parsing of IDRs to functions so that it can be re-used from the hypervisor.
Signed-off-by Mostafa Saleh <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 112 +++----------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 111 +++++++++++++++++++ 2 files changed, 126 insertions(+), 97 deletions(-)
Quoted 7 lines
Quoted 4 lines
I don't see that this slow path stuff needs to be inlined?
+10 to my prior remark to not dump all the huge inlines in the main header.
They are inline as they are defined in the header file, otherwise they get defined multiple times.
Put them in a shared C file
Jason
PATCH v5 8/27 KVM: arm64: iommu: Introduce IOMMU driver infrastructure
In reply to Mostafa SalehTo establish DMA isolation, KVM needs an IOMMU driver which provide ops implemented at EL2.
Only one driver can be used and is registered with kvm_iommu_register_driver() by passing pointer to the ops.
This must be called before module_init() which is the point KVM initializes.
--- arch/arm64/include/asm/kvm_host.h | 5 +++++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/hyp/include/nvhe/iommu.h | 13 +++++++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 3 ++- arch/arm64/kvm/hyp/nvhe/iommu/iommu.c | 18 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 5 +++++ arch/arm64/kvm/iommu.c | 15 +++++++++++++++ 7 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c create mode 100644 arch/arm64/kvm/iommu.c
arch/arm64/include/asm/kvm_host.h +5 -0
@@ -1652,4 +1652,9 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg p; \ }) +#ifndef __KVM_NVHE_HYPERVISOR__ +struct kvm_iommu_ops ; +int kvm_iommu_register_driver (struct kvm_iommu_ops *hyp_ops); +#endif +#endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \ - vgic/vgic-v5.o + vgic/vgic-v5.o iommu.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o
arch/arm64/kvm/hyp/include/nvhe/iommu.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ARM64_KVM_NVHE_IOMMU_H__ +#define __ARM64_KVM_NVHE_IOMMU_H__ + +#include <asm/kvm_host.h> + +struct kvm_iommu_ops { +int (*init)(void ); +}; + +int kvm_iommu_init (void ); + +#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
@@ -24,7 +24,8 @@ CFLAGS_switch.nvhe.o += -Wno-override-init hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \ - cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o + cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o \ + iommu/iommu.o hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o hyp-obj-y += ../../../kernel/smccc-call.o
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* IOMMU operations for pKVM +* +* Copyright (C) 2022 Linaro Ltd. +*/ +#include <nvhe/iommu.h> + +/* Only one set of ops supported */ +struct kvm_iommu_ops *kvm_iommu_ops; + +int kvm_iommu_init (void ) +{ +if (!kvm_iommu_ops || !kvm_iommu_ops->init) +return -ENODEV ; + +return kvm_iommu_ops->init (); +}
arch/arm64/kvm/hyp/nvhe/setup.c +5 -0
@@ -13,6 +13,7 @@#include <nvhe/early_alloc.h> #include <nvhe/ffa.h> #include <nvhe/gfp.h> +#include <nvhe/iommu.h> #include <nvhe/memory.h> #include <nvhe/mem_protect.h> #include <nvhe/mm.h>
@@ -328,6 +329,10 @@ void __noreturn __pkvm_init_finalise(void)if (ret) goto out; + ret =kvm_iommu_init (); +if (ret) + goto out; + ret =hyp_ffa_init (ffa_proxy_pages);if (ret) goto out;
arch/arm64/kvm/iommu.c new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2025 Google LLC +* Author: Mostafa Saleh <[email protected]> +*/ + +#include <linux/kvm_host.h> + +extern struct kvm_iommu_ops *kvm_nvhe_sym (kvm_iommu_ops ); + +int kvm_iommu_register_driver (struct kvm_iommu_ops *hyp_ops) +{ +kvm_nvhe_sym (kvm_iommu_ops) = hyp_ops; +return 0 ; +}
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 9/27 KVM: arm64: iommu: Shadow host stage-2 page table
In reply to Mostafa SalehCreate a shadow page table for the IOMMU that shadows the host CPU stage-2 into the IOMMUs to establish DMA isolation.
An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.
For some cases, an SMMUv3 may be able to share the same page table used with the host CPU stage-2 directly.
However, this is too strict and requires changes to the core hypervisor page table code, plus it would require the hypervisor to handle IOMMU page faults. This can be added later as an optimization for SMMUV3.
--- arch/arm64/kvm/hyp/include/nvhe/iommu.h | 4 ++ arch/arm64/kvm/hyp/nvhe/iommu/iommu.c | 83 ++++++++++++++++++++++++- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 5 ++ 3 files changed, 90 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +4 -0
@@ -3,11 +3,15 @@#define __ARM64_KVM_NVHE_IOMMU_H__#include <asm/kvm_host.h> +#include <asm/kvm_pgtable.h> struct kvm_iommu_ops {int (*init)(void ); +void (*host_stage2_idmap)(phys_addr_t start,phys_addr_t end,int prot); };int kvm_iommu_init (void ); +void kvm_iommu_host_stage2_idmap (phys_addr_t start,phys_addr_t end, +enum kvm_pgtable_prot prot);#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +81 -2
96 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -0
@@ -15,6 +15,7 @@#include <hyp/fault.h> #include <nvhe/gfp.h> +#include <nvhe/iommu.h> #include <nvhe/memory.h> #include <nvhe/mem_protect.h> #include <nvhe/mm.h>
@@ -529,6 +530,7 @@ static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_int host_stage2_set_owner_locked (phys_addr_t addr,u64 size,u8 owner_id) {int ret; +enum kvm_pgtable_prot prot;if (!range_is_memory (addr, addr + size))return -EPERM ;
@@ -538,6 +540,9 @@ int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)if (ret)return ret; + prot = owner_id ==PKVM_ID_HOST ?PKVM_HOST_MEM_PROT :0 ; +kvm_iommu_host_stage2_idmap (addr, addr + size, prot); +/* Don't forget to update the vmemmap tracking for the host */ if (owner_id ==PKVM_ID_HOST )__host_update_page_state (addr, size,PKVM_PAGE_OWNED );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 10/27 KVM: arm64: iommu: Add memory pool
In reply to Mostafa SalehIOMMU drivers would require to allocate memory for the shadow page table. Similar to the host stage-2 CPU page table, the IOMMU pool is allocated early from the carveout and it's memory is added in a pool which the IOMMU driver can allocate from and reclaim at run time.
As this is too early for drivers to use init calls, a default value can be set in the kernel config through IOMMU_POOL_PAGES, which then can be overridden later from the kernel command line:
"kvm-arm.hyp_iommu_pages".
Later when the driver registers, it will pass how many pages it needs, and if it was less than what was allocated, it will fail to register.
--- .../admin-guide/kernel-parameters.txt | 4 +++ arch/arm64/include/asm/kvm_host.h | 3 +- arch/arm64/kvm/Kconfig | 7 +++++ arch/arm64/kvm/hyp/include/nvhe/iommu.h | 5 ++- arch/arm64/kvm/hyp/nvhe/iommu/iommu.c | 20 +++++++++++- arch/arm64/kvm/hyp/nvhe/setup.c | 16 +++++++++- arch/arm64/kvm/iommu.c | 31 ++++++++++++++++++- arch/arm64/kvm/pkvm.c | 1 + 8 files changed, 82 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +4 -0
@@ -3059,6 +3059,10 @@ trap: set WFI instruction trap notrap: clear WFI instruction trap + kvm-arm.hyp_iommu_pages= + [KVM, ARM, EARLY] + Number of pages allocated for the IOMMU pool from the + KVM carveout when running in protected mode. kvm_cma_resv_ratio=n [PPC,EARLY] Reserves given percentage from system memory area for
arch/arm64/include/asm/kvm_host.h +2 -1
@@ -1654,7 +1654,8 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg#ifndef __KVM_NVHE_HYPERVISOR__struct kvm_iommu_ops ; -int kvm_iommu_register_driver (struct kvm_iommu_ops *hyp_ops); +int kvm_iommu_register_driver (struct kvm_iommu_ops *hyp_ops,size_t pool_pages); +size_t kvm_iommu_pages (void );#endif #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +7 -0
@@ -83,4 +83,11 @@ config PTDUMP_STAGE2_DEBUGFS If in doubt, say N. +config IOMMU_POOL_PAGES + hex "Number of pages reserved for IOMMU pool" + depends on KVM && IOMMU_SUPPORT + default 0x0 + help + IOMMU pool is used with protected mode to allocated IOMMU drivers page tables. + endif # VIRTUALIZATION
arch/arm64/kvm/hyp/include/nvhe/iommu.h +4 -1
@@ -10,8 +10,11 @@ struct kvm_iommu_ops { void (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot); }; -int kvm_iommu_init (void ); +int kvm_iommu_init (void *pool_base,size_t nr_pages);void kvm_iommu_host_stage2_idmap (phys_addr_t start,phys_addr_t end,enum kvm_pgtable_prot prot); +void *kvm_iommu_donate_pages (u8 order); +void kvm_iommu_reclaim_pages (void *ptr); +#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +19 -1
@@ -15,6 +15,7 @@ struct kvm_iommu_ops *kvm_iommu_ops;/* Protected by host_mmu.lock */ static bool kvm_idmap_initialized; +static struct hyp_pool iommu_pages_pool;static inline int pkvm_to_iommu_prot (enum kvm_pgtable_prot prot) {
@@ -72,7 +73,7 @@ static int kvm_iommu_snapshot_host_stage2(void)return ret; } -int kvm_iommu_init (void ) +int kvm_iommu_init (void *pool_base,size_t nr_pages) {int ret;
@@ -80,6 +81,13 @@ int kvm_iommu_init(void) !kvm_iommu_ops->host_stage2_idmap)return -ENODEV ; +if (nr_pages) { + ret =hyp_pool_init (&iommu_pages_pool,hyp_virt_to_pfn (pool_base), + nr_pages,0 ); +if (ret) +return ret; + } + ret = kvm_iommu_ops->init ();if (ret)return ret;
@@ -95,3 +103,13 @@ void kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,return ; kvm_iommu_ops->host_stage2_idmap (start, end,pkvm_to_iommu_prot (prot)); } + +void *kvm_iommu_donate_pages (u8 order) +{ +return hyp_alloc_pages (&iommu_pages_pool, order); +} + +void kvm_iommu_reclaim_pages (void *ptr) +{ +hyp_put_page (&iommu_pages_pool, ptr); +}
arch/arm64/kvm/hyp/nvhe/setup.c +15 -1
@@ -22,6 +22,13 @@unsigned long hyp_nr_cpus; +/* See kvm_iommu_pages() */ +#ifdef CONFIG_IOMMU_POOL_PAGES +size_t hyp_kvm_iommu_pages =CONFIG_IOMMU_POOL_PAGES ; +#else +size_t hyp_kvm_iommu_pages; +#endif +#define hyp_percpu_size ((unsigned long)__per_cpu_end - \ (unsigned long)__per_cpu_start)
@@ -33,6 +40,7 @@ static void *selftest_base;static void *ffa_proxy_pages;static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;static struct hyp_pool hpool; +static void *iommu_base;static int divide_memory_pool (void *virt,unsigned long size) {
@@ -70,6 +78,12 @@ static int divide_memory_pool(void *virt, unsigned long size)if (!ffa_proxy_pages)return -ENOMEM ; +if (hyp_kvm_iommu_pages) { + iommu_base =hyp_early_alloc_contig (hyp_kvm_iommu_pages); +if (!iommu_base) +return -ENOMEM ; + } +return 0 ; }
@@ -329,7 +343,7 @@ void __noreturn __pkvm_init_finalise(void)if (ret) goto out; - ret =kvm_iommu_init (); + ret =kvm_iommu_init (iommu_base, hyp_kvm_iommu_pages);if (ret) goto out;
arch/arm64/kvm/iommu.c +30 -1
@@ -7,9 +7,38 @@#include <linux/kvm_host.h> extern struct kvm_iommu_ops *kvm_nvhe_sym (kvm_iommu_ops ); +extern size_t kvm_nvhe_sym (hyp_kvm_iommu_pages ); -int kvm_iommu_register_driver (struct kvm_iommu_ops *hyp_ops) +int kvm_iommu_register_driver (struct kvm_iommu_ops *hyp_ops,size_t pool_pages) { +/* See kvm_iommu_pages() */ +if (pool_pages >kvm_nvhe_sym (hyp_kvm_iommu_pages)) { +kvm_err ("Missing memory for the IOMMU pool, need 0x%zx pages, check kvm-arm.hyp_iommu_pages" , + pool_pages); +return -ENOMEM ; + } +kvm_nvhe_sym (kvm_iommu_ops) = hyp_ops;return 0 ; } + +size_t kvm_iommu_pages (void ) +{ +/* +* This is called very early during setup_arch() where no initcalls, +* so this has to call specific functions per each KVM driver. +* So we allow a config option that can set the defaul value for +* the IOMMU pool that can overridden by a command line option. +* When the driver registers it will pass the number pages needed +* for it's page tables, if less that what the system has already +* allocated we fail. +*/ +return kvm_nvhe_sym (hyp_kvm_iommu_pages); +} + +/* Number of pages to reserve for iommu pool*/ +static int __init early_hyp_iommu_pages (char *arg) +{ +return kstrtoul (arg,10 , &kvm_nvhe_sym (hyp_kvm_iommu_pages)); +} +early_param ("kvm-arm.hyp_iommu_pages" , early_hyp_iommu_pages);
arch/arm64/kvm/pkvm.c +1 -0
@@ -63,6 +63,7 @@ void __init kvm_hyp_reserve(void) hyp_mem_pages +=hyp_vmemmap_pages (STRUCT_HYP_PAGE_SIZE ); hyp_mem_pages +=pkvm_selftest_pages (); hyp_mem_pages +=hyp_ffa_proxy_pages (); + hyp_mem_pages +=kvm_iommu_pages ();/* * Try to allocate a PMD-aligned region to reduce TLB pressure once
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 11/27 KVM: arm64: iommu: Support DABT for IOMMU
In reply to Mostafa SalehSMMUv3 driver need to trap and emulate access to the MMIO space to provide emulation for the kernel.
Add a handler for DABTs for IOMMU drivers to be able to do so, in case the host fault in page, check if it's part of IOMMU emulation first.
--- arch/arm64/include/asm/kvm_arm.h | 2 ++ arch/arm64/kvm/hyp/include/nvhe/iommu.h | 3 ++- arch/arm64/kvm/hyp/nvhe/iommu/iommu.c | 15 +++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 10 ++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_arm.h +2 -0
@@ -331,6 +331,8 @@/* Hyp Prefetch Fault Address Register (HPFAR/HDFAR) */ #define HPFAR_MASK (~UL(0xf)) + +#define FAR_MASK GENMASK_ULL(11, 0)/* * We have * PAR [PA_Shift - 1 : 12] = PA [PA_Shift - 1 : 12]
arch/arm64/kvm/hyp/include/nvhe/iommu.h +2 -1
@@ -8,6 +8,7 @@struct kvm_iommu_ops {int (*init)(void );void (*host_stage2_idmap)(phys_addr_t start,phys_addr_t end,int prot); +bool (*dabt_handler)(struct user_pt_regs *regs,u64 esr,u64 addr); };int kvm_iommu_init (void *pool_base,size_t nr_pages);
@@ -16,5 +17,5 @@ void kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end, enum kvm_pgtable_prot prot);void *kvm_iommu_donate_pages (u8 order);void kvm_iommu_reclaim_pages (void *ptr); - +bool kvm_iommu_host_dabt_handler (struct user_pt_regs *regs,u64 esr,u64 addr);#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +15 -0
@@ -4,6 +4,10 @@* * Copyright (C) 2022 Linaro Ltd. */ +#include <asm/kvm_hyp.h> + +#include <hyp/adjust_pc.h> +#include <linux/iommu.h> #include <nvhe/iommu.h>
@@ -113,3 +117,14 @@ void kvm_iommu_reclaim_pages(void *ptr) {hyp_put_page (&iommu_pages_pool, ptr); } + +bool kvm_iommu_host_dabt_handler (struct user_pt_regs *regs,u64 esr,u64 addr) +{ +if (kvm_iommu_ops && kvm_iommu_ops->dabt_handler && + kvm_iommu_ops->dabt_handler (regs, esr, addr)) { +/* DABT handled by the driver, skip to next instruction. */ +kvm_skip_host_instr (); +return true; + } +return false; +}
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
@@ -595,6 +595,11 @@ static int host_stage2_idmap(u64 addr)return ret; } +static bool is_dabt (u64 esr) +{ +return ESR_ELx_EC (esr) == ESR_ELx_EC_DABT_LOW; +} +void handle_host_mem_abort (struct kvm_cpu_context *host_ctxt) {struct kvm_vcpu_fault_info fault;
@@ -617,6 +622,11 @@ void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)*/ BUG_ON (!(fault.hpfar_el2 &HPFAR_EL2_NS )); addr =FIELD_GET (HPFAR_EL2_FIPA , fault.hpfar_el2) <<12 ; + addr |= fault.far_el2 &FAR_MASK ; + +if (is_dabt (esr) && !addr_is_memory (addr) && +kvm_iommu_host_dabt_handler (&host_ctxt->regs, esr, addr)) +return ; ret =host_stage2_idmap (addr);BUG_ON (ret && ret != -EAGAIN );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 12/27 iommu/arm-smmu-v3-kvm: Add SMMUv3 driver
In reply to Mostafa SalehFrom: Jean-Philippe Brucker <[email protected]>
Add the skeleton for an Arm SMMUv3 driver at EL2.
--- arch/arm64/kvm/hyp/nvhe/Makefile | 5 ++++ drivers/iommu/arm/Kconfig | 9 ++++++ .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 29 +++++++++++++++++++ .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h | 16 ++++++++++ 4 files changed, 59 insertions(+) create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h
arch/arm64/kvm/hyp/nvhe/Makefile +5 -0
@@ -32,6 +32,11 @@ hyp-obj-y += ../../../kernel/smccc-call.o hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o hyp-obj-y += $(lib-objs) +HYP_SMMU_V3_DRV_PATH = ../../../../../drivers/iommu/arm/arm-smmu-v3 + +hyp-obj-$(CONFIG_ARM_SMMU_V3_PKVM) += $(HYP_SMMU_V3_DRV_PATH)/pkvm/arm-smmu-v3.o \ + $(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o + ## ## Build rules for compiling nVHE hyp code ## Output of this folder is `kvm_nvhe.o`, a partially linked object
drivers/iommu/arm/Kconfig +9 -0
@@ -142,3 +142,12 @@ config QCOM_IOMMU select ARM_DMA_USE_IOMMU help Support for IOMMU on certain Qualcomm SoCs. + +config ARM_SMMU_V3_PKVM + bool "ARM SMMUv3 support for protected Virtual Machines" + depends on KVM && ARM64 && ARM_SMMU_V3=y + help + Enable a SMMUv3 driver in the KVM hypervisor, to protect VMs against + memory accesses from devices owned by the host. + + Say Y here if you intend to enable KVM in protected mode.
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* pKVM hyp driver for the Arm SMMUv3 +* +* Copyright (C) 2022 Linaro Ltd. +*/ +#include <asm/kvm_hyp.h> + +#include <nvhe/iommu.h> + +#include "arm_smmu_v3.h" + +size_t __ro_after_init kvm_hyp_arm_smmu_v3_count; +struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; + +static int smmu_init (void ) +{ +return -ENOSYS ; +} + +static void smmu_host_stage2_idmap (phys_addr_t start,phys_addr_t end,int prot) +{ +} + +/* Shared with the kernel driver in EL1 */ +struct kvm_iommu_ops smmu_ops = { + .init = smmu_init, + .host_stage2_idmap = smmu_host_stage2_idmap, +};
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVM_ARM_SMMU_V3_H +#define __KVM_ARM_SMMU_V3_H + +#include <asm/kvm_asm.h> + +struct hyp_arm_smmu_v3_device { +}; + +extern size_t kvm_nvhe_sym (kvm_hyp_arm_smmu_v3_count ); +#define kvm_hyp_arm_smmu_v3_count kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count) + +extern struct hyp_arm_smmu_v3_device *kvm_nvhe_sym (kvm_hyp_arm_smmu_v3_smmus ); +#define kvm_hyp_arm_smmu_v3_smmus kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_smmus) + +#endif /* __KVM_ARM_SMMU_V3_H */
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 13/27 iommu/arm-smmu-v3-kvm: Add the kernel driver
In reply to Mostafa SalehA platform driver is created to probe the SMMUs, which then creates aux devices for the emulated SMMUs.
Then the driver registers with KVM.
Next, the SMMUv3 driver will probed the aux devices.
--- drivers/iommu/arm/arm-smmu-v3/Makefile | 1 + .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c
drivers/iommu/arm/arm-smmu-v3/Makefile +1 -0
@@ -4,5 +4,6 @@ arm_smmu_v3-y := arm-smmu-v3.o arm-smmu-v3-common-lib.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_TEGRA241_CMDQV) += tegra241-cmdqv.o +arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_PKVM) += arm-smmu-v3-kvm.o obj-$(CONFIG_ARM_SMMU_V3_KUNIT_TEST) += arm-smmu-v3-test.o
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 14/27 iommu/arm-smmu-v3: Support probing KVM emulated devices
In reply to Mostafa SalehWhen KVM runs in protected mode, and CONFIG_ARM_SMMU_V3_PKVM is enabled, it will manage the SMMUv3 HW using trap and emulate and present emulated SMMUs to the host kernel.
In that case, those SMMUs will be on the aux bus, so make it possibly to the driver to probe those devices. Otherwise, everything else is the same as the KVM emulation complies with the architecutre, so the driver doesn't need to be modified.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +58 -0
@@ -11,6 +11,7 @@#include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/auxiliary_bus.h> #include <linux/bitops.h> #include <linux/crash_dump.h> #include <linux/delay.h>
@@ -4604,6 +4605,63 @@ static struct platform_driver arm_smmu_driver = {module_driver (arm_smmu_driver, platform_driver_register, arm_smmu_driver_unregister); +#ifdef CONFIG_ARM_SMMU_V3_PKVM +/* +* Now we have 2 devices, the aux device bound to this driver, and pdev +* which is the physical platform device. +* This part is a bit hairy but it works due to the fact that +* CONFIG_ARM_SMMU_V3_PKVM forces both drivers to be built in. +* The struct device for the SMMU is used in the following cases: +* 1) Printing using dev_*() +* 2) DMA memory alloc (dmam_alloc_coherent, devm_*) +* 3) Requesting resources (iomem, sysfs) +* 4) Probing firmware info (of_node, fwnode...) +* 5) Dealing with abstracted HW resources (irqs, MSIs, RPM) +* 6) Saving/reading driver data +* For point 4) and 5) we must use the platform device. +* For, 1) pdev is better for debuggability. +* For 2), 3), 6) it's better to use the bound device. +* However that doesn't really work: +* For 2) The DMA allocation using the aux device will fail, as +* we need to setup some device DMA attrs (mask), to match the +* platform. +* For 6) Some contexts from the pdev as MSI, it needs to use the +* drvdata. +* Based on the following: +* 1- Both drivers must be built-in to enable this (enforced by Kconfig), +* which means that none of them can be removed. +* 2- The KVM driver doesn't do anythng at runtime and doesn't use drvdata. +* We can keep the driver simple and to claim the platform device in all cases. +*/ +static int arm_smmu_device_probe_emu (struct auxiliary_device *auxdev, +const struct auxiliary_device_id *id) +{ +struct device *parent = auxdev->dev.parent; + +dev_info (&auxdev->dev,"Probing from %s\n" ,dev_name (parent)); +return arm_smmu_device_probe (to_platform_device (parent)); +} + +static void arm_smmu_device_shutdown_emu (struct auxiliary_device *auxdev) +{ +arm_smmu_device_shutdown (to_platform_device (auxdev->dev.parent)); +} + +const struct auxiliary_device_id arm_smmu_aux_table[] = { + { .name ="protected_kvm.smmu_v3_emu" }, + { }, +}; + +struct auxiliary_driver arm_smmu_driver_emu = { + .name ="arm-smmu-v3-emu" , + .id_table = arm_smmu_aux_table, + .probe = arm_smmu_device_probe_emu, + .shutdown = arm_smmu_device_shutdown_emu, +}; + +module_auxiliary_driver (arm_smmu_driver_emu); +#endif +MODULE_DESCRIPTION ("IOMMU API for ARM architected SMMUv3 implementations" );MODULE_AUTHOR ("Will Deacon <[email protected]>" );MODULE_ALIAS ("platform:arm-smmu-v3" );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 14/27] iommu/arm-smmu-v3: Support probing KVM emulated devices
Quoted 50 lines · Mostafa Saleh
When KVM runs in protected mode, and
CONFIG_ARM_SMMU_V3_PKVMis enabled, it will manage the SMMUv3 HW using trap and emulate and present emulated SMMUs to the host kernel.In that case, those SMMUs will be on the aux bus, so make it possibly to the driver to probe those devices. Otherwise, everything else is the same as the KVM emulation complies with the architecutre, so the driver doesn't need to be modified.
Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Mostafa Saleh <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +19
-0@@ -11,6 +11,7 @@#include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/auxiliary_bus.h> #include <linux/bitops.h> #include <linux/crash_dump.h> #include <linux/delay.h> @@ -4604,6 +4605,63 @@ static struct platform_driver arm_smmu_driver = {module_driver (arm_smmu_driver, platform_driver_register, arm_smmu_driver_unregister); +#ifdef CONFIG_ARM_SMMU_V3_PKVM +/* +* Now we have 2 devices, the aux device bound to this driver, and pdev +* which is the physical platform device. +* This part is a bit hairy but it works due to the fact that +* CONFIG_ARM_SMMU_V3_PKVM forces both drivers to be built in. +* The struct device for the SMMU is used in the following cases: +* 1) Printing using dev_*() +* 2) DMA memory alloc (dmam_alloc_coherent, devm_*) +* 3) Requesting resources (iomem, sysfs) +* 4) Probing firmware info (of_node, fwnode...) +* 5) Dealing with abstracted HW resources (irqs, MSIs, RPM) +* 6) Saving/reading driver data +* For point 4) and 5) we must use the platform device. +* For, 1) pdev is better for debuggability. +* For 2), 3), 6) it's better to use the bound device. +* However that doesn't really work: +* For 2) The DMA allocation using the aux device will fail, as
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 4635
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* we need to setup some device DMA attrs (mask), to match the +* platform. +* For 6) Some contexts from the pdev as MSI, it needs to use the +* drvdata. +* Based on the following: +* 1- Both drivers must be built-in to enable this (enforced by Kconfig), +* which means that none of them can be removed. +* 2- The KVM driver doesn't do anythng at runtime and doesn't use drvdata. +* We can keep the driver simple and to claim the platform device in all cases. +*/
It is OK I guess, I wouldn't insist you change it, but I think it is kind of gross. Registering the iommu driver against the platform device instead of the aux is pretty ugly and denies userspace the ability to see that the hypervisor is sitting in there through the sysfs topology.
Not sure why the commentary about built-in though, what does that have to do with anything? If the aux driver is not built in then it will just module load later and everything should be fine?
Jason
Re: [PATCH v5 14/27] iommu/arm-smmu-v3: Support probing KVM emulated devices
Quoted 59 lines · Jason Gunthorpe
Quoted 58 lines · Mostafa Saleh
When KVM runs in protected mode, and
CONFIG_ARM_SMMU_V3_PKVMis enabled, it will manage the SMMUv3 HW using trap and emulate and present emulated SMMUs to the host kernel.In that case, those SMMUs will be on the aux bus, so make it possibly to the driver to probe those devices. Otherwise, everything else is the same as the KVM emulation complies with the architecutre, so the driver doesn't need to be modified.
Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Mostafa Saleh <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +27
-0@@ -11,6 +11,7 @@#include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/auxiliary_bus.h> #include <linux/bitops.h> #include <linux/crash_dump.h> #include <linux/delay.h> @@ -4604,6 +4605,63 @@ static struct platform_driver arm_smmu_driver = {module_driver (arm_smmu_driver, platform_driver_register, arm_smmu_driver_unregister); +#ifdef CONFIG_ARM_SMMU_V3_PKVM +/* +* Now we have 2 devices, the aux device bound to this driver, and pdev +* which is the physical platform device. +* This part is a bit hairy but it works due to the fact that +* CONFIG_ARM_SMMU_V3_PKVM forces both drivers to be built in. +* The struct device for the SMMU is used in the following cases: +* 1) Printing using dev_*() +* 2) DMA memory alloc (dmam_alloc_coherent, devm_*) +* 3) Requesting resources (iomem, sysfs) +* 4) Probing firmware info (of_node, fwnode...) +* 5) Dealing with abstracted HW resources (irqs, MSIs, RPM) +* 6) Saving/reading driver data +* For point 4) and 5) we must use the platform device. +* For, 1) pdev is better for debuggability. +* For 2), 3), 6) it's better to use the bound device. +* However that doesn't really work: +* For 2) The DMA allocation using the aux device will fail, as +* we need to setup some device DMA attrs (mask), to match the +* platform. +* For 6) Some contexts from the pdev as MSI, it needs to use the +* drvdata. +* Based on the following: +* 1- Both drivers must be built-in to enable this (enforced by Kconfig), +* which means that none of them can be removed. +* 2- The KVM driver doesn't do anythng at runtime and doesn't use drvdata.
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 4635
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+* We can keep the driver simple and to claim the platform device in all cases. +*/ It is OK I guess, I wouldn't insist you change it, but I think it is kind of gross. Registering the iommu driver against the platform device instead of the aux is pretty ugly and denies userspace the ability to see that the hypervisor is sitting in there through the sysfs topology.
Yes, that’s why I was wondering if it’s better to keep this as a platform driver and create the aux devices for the parent(KVM) but that was really complicated to handle the probe ordering.
I will give this another though before v6.
Quoted 4 lines
Not sure why the commentary about built-in though, what does that have to do with anything? If the aux driver is not built in then it will just module load later and everything should be fine?
As at the moment the KVM driver doesn’t use drvdata(nor any device resources) and the main driver(aux) does, but if that was a module, we can’t know which version does what (if that changes in the future, although unlikely).
Thanks,
Mostafa
Quoted 2 lines
Jason
Re: [PATCH v5 14/27] iommu/arm-smmu-v3: Support probing KVM emulated devices
Quoted 9 lines · Mostafa Saleh
Quoted 5 lines
It is OK I guess, I wouldn't insist you change it, but I think it is kind of gross. Registering the iommu driver against the platform device instead of the aux is pretty ugly and denies userspace the ability to see that the hypervisor is sitting in there through the sysfs topology.
Yes, that’s why I was wondering if it’s better to keep this as a platform driver and create the aux devices for the parent(KVM) but that was really complicated to handle the probe ordering.
That sounds worse and inverts the required probe ordering.
Attach it to the aux device :\
Quoted 8 lines
Quoted 3 lines
Not sure why the commentary about built-in though, what does that have to do with anything? If the aux driver is not built in then it will just module load later and everything should be fine?
As at the moment the KVM driver doesn’t use drvdata(nor any device resources) and the main driver(aux) does, but if that was a module, we can’t know which version does what (if that changes in the future, although unlikely).
Kernel is monolithic, you don't need to worry about people doing silly games with modules.
Jason
PATCH v5 15/27 iommu/arm-smmu-v3-kvm: Create array for hyp SMMUv3
In reply to Mostafa SalehAs the hypervisor has no access to firmware tables, the device discovery is done from the kernel, where it parses firmware tables and populates a list of devices to the hypervisor, which later takes over.
--- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c | 82 ++++++++++++++++++- .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h | 13 +++ 2 files changed, 91 insertions(+), 4 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +78 -4
@@ -8,6 +8,7 @@#include <asm/kvm_pkvm.h> #include <linux/auxiliary_bus.h> +#include <linux/of_address.h> #include <linux/of_platform.h> #include <linux/platform_device.h>
@@ -16,6 +17,45 @@extern struct kvm_iommu_ops kvm_nvhe_sym (smmu_ops ); +static size_t kvm_arm_smmu_count; +static struct hyp_arm_smmu_v3_device *kvm_arm_smmu_array; +static size_t kvm_arm_smmu_cur; + +static void kvm_arm_smmu_array_free (void ) +{ +int order; + + order =get_order (kvm_arm_smmu_count *sizeof (*kvm_arm_smmu_array)); +free_pages ((unsigned long )kvm_arm_smmu_array, order); +} + +/* +* The hypervisor have to know the basic information about the SMMUs +* from the firmware. +* This has to be done before the SMMUv3 probes and does anything meaningful +* with the hardware, otherwise it becomes harder to reason about the SMMU +* state and we'd require to hand-off the state to the hypervisor at certain point +* while devices are live, which is complicated and dangerous. +* Instead, the hypervisor is interested in a very small part of the probe path, +* so just add a separate logic for it. +*/ +static int kvm_arm_smmu_array_alloc (void ) +{ +int smmu_order; +struct device_node *np; + +for_each_compatible_node (np,NULL ,"arm,smmu-v3" ) + kvm_arm_smmu_count++; + +if (!kvm_arm_smmu_count) +return -ENODEV ; + smmu_order =get_order (kvm_arm_smmu_count *sizeof (*kvm_arm_smmu_array)); + kvm_arm_smmu_array = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO, smmu_order); +if (!kvm_arm_smmu_array) +return -ENOMEM ; +return 0 ; +} +static size_t smmu_hyp_pgt_pages (void ) {/*
@@ -30,6 +70,21 @@ static size_t smmu_hyp_pgt_pages(void)static struct platform_driver smmuv3_nesting_driver;static int smmuv3_nesting_probe (struct platform_device *pdev) { +struct resource *res; +struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur]; + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); + smmu->mmio_addr = res->start; + smmu->mmio_size =resource_size (res); +if (smmu->mmio_size <SZ_128K ) { +dev_err (&pdev->dev,"MMIO region too small(%pr)\n" , &res); +return -EINVAL ; + } + +if (of_dma_is_coherent (pdev->dev.of_node)) + smmu->features |=ARM_SMMU_FEAT_COHERENCY ; + + kvm_arm_smmu_cur++;return 0 ; }
@@ -41,12 +96,31 @@ static int kvm_arm_smmu_v3_register(void)if (!is_protected_kvm_enabled () || !nr_pages)return 0 ; - ret =platform_driver_probe (&smmuv3_nesting_driver, smmuv3_nesting_probe); + ret =kvm_arm_smmu_array_alloc ();if (ret)return ret; -return kvm_iommu_register_driver (kern_hyp_va (lm_alias (&kvm_nvhe_sym (smmu_ops))), - nr_pages); + ret =platform_driver_probe (&smmuv3_nesting_driver, smmuv3_nesting_probe); +if (ret) + goto out_err; + + ret =kvm_iommu_register_driver (kern_hyp_va (lm_alias (&kvm_nvhe_sym (smmu_ops))), + nr_pages); +if (ret) + goto out_err; + +/* +* These variables are stored in the nVHE image, and won't be accessible +* after KVM initialization. Ownership of kvm_arm_smmu_array will be +* transferred to the hypervisor as well. +*/ + kvm_hyp_arm_smmu_v3_smmus = kvm_arm_smmu_array; + kvm_hyp_arm_smmu_v3_count = kvm_arm_smmu_count; +return ret; + +out_err: +kvm_arm_smmu_array_free (); +return ret; };static int smmu_create_aux_device (struct device *dev,void *data)
@@ -67,7 +141,7 @@ static int smmu_create_aux_device(struct device *dev, void *data)static struct platform_driver smmuv3_nesting_driver;static int kvm_arm_smmu_v3_post_init (void ) { -if (!is_protected_kvm_enabled ()) +if (!is_protected_kvm_enabled () || !kvm_arm_smmu_cur)return 0 ; WARN_ON(driver_for_each_device(&smmuv3_nesting_driver.driver, NULL,
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +13 -0
@@ -4,7 +4,20 @@#include <asm/kvm_asm.h> +/* +* Parameters from the trusted host: +* @mmio_addr base address of the SMMU registers +* @mmio_size size of the registers resource +* @features Features of SMMUv3, subset of the main driver +* +* Other members are filled and used at runtime by the SMMU driver. +* @base Virtual address of SMMU registers +*/ struct hyp_arm_smmu_v3_device { +phys_addr_t mmio_addr; +size_t mmio_size; +void __iomem *base; +u32 features; };extern size_t kvm_nvhe_sym (kvm_hyp_arm_smmu_v3_count );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 16/27 iommu/arm-smmu-v3-kvm: Take over SMMUs
In reply to Mostafa SalehDonate the array with SMMU description to the hypervisor as it can't be changed by the host after de-privileges.
Also, donate the SMMU resources to the hypervisor.
--- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +80 -1
@@ -7,15 +7,94 @@#include <asm/kvm_hyp.h> #include <nvhe/iommu.h> +#include <nvhe/mem_protect.h> #include "arm_smmu_v3.h" size_t __ro_after_init kvm_hyp_arm_smmu_v3_count;struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; +#define for_each_smmu (smmu) \ + for ((smmu) = kvm_hyp_arm_smmu_v3_smmus; \ + (smmu) != &kvm_hyp_arm_smmu_v3_smmus[kvm_hyp_arm_smmu_v3_count]; \ + (smmu)++) + +/* Transfer ownership of memory */ +static int smmu_take_pages (u64 phys,size_t size) +{ +WARN_ON (!PAGE_ALIGNED (phys) || !PAGE_ALIGNED (size)); +return __pkvm_host_donate_hyp (phys >>PAGE_SHIFT , size >>PAGE_SHIFT ); +} + +static void smmu_reclaim_pages (u64 phys,size_t size) +{ +WARN_ON (!PAGE_ALIGNED (phys) || !PAGE_ALIGNED (size)); +WARN_ON (__pkvm_hyp_donate_host (phys >>PAGE_SHIFT , size >>PAGE_SHIFT )); +} + +/* Put the device in a state that can be probed by the host driver. */ +static void smmu_deinit_device (struct hyp_arm_smmu_v3_device *smmu) +{ +int i; +size_t nr_pages = smmu->mmio_size >>PAGE_SHIFT ; + +for (i =0 ; i < nr_pages ; ++i) { +u64 pfn = (smmu->mmio_addr >>PAGE_SHIFT ) + i; + +WARN_ON (__pkvm_hyp_donate_host_mmio (pfn)); + } +} + +static int smmu_init_device (struct hyp_arm_smmu_v3_device *smmu) +{ +int i; +size_t nr_pages; + +if (!PAGE_ALIGNED (smmu->mmio_addr | smmu->mmio_size)) +return -EINVAL ; + + nr_pages = smmu->mmio_size >>PAGE_SHIFT ; +for (i =0 ; i < nr_pages ; ++i) { +u64 pfn = (smmu->mmio_addr >>PAGE_SHIFT ) + i; + +/* +* This should never happen, so it's fine to be strict to avoid +* complicated error handling. +*/ +WARN_ON (__pkvm_host_donate_hyp_mmio (pfn)); + } + smmu->base =hyp_phys_to_virt (smmu->mmio_addr); + +return 0 ; +} +static int smmu_init (void ) { -return -ENOSYS ; +int ret; +struct hyp_arm_smmu_v3_device *smmu; +size_t smmu_arr_size =PAGE_ALIGN (sizeof (*kvm_hyp_arm_smmu_v3_smmus) * + kvm_hyp_arm_smmu_v3_count); + + kvm_hyp_arm_smmu_v3_smmus =kern_hyp_va (kvm_hyp_arm_smmu_v3_smmus); + ret =smmu_take_pages (hyp_virt_to_phys (kvm_hyp_arm_smmu_v3_smmus), + smmu_arr_size); +if (ret) +return ret; + +for_each_smmu (smmu) { + ret =smmu_init_device (smmu); +if (ret) + goto out_reclaim_smmu; + } + +return 0 ; + +out_reclaim_smmu: +while (smmu != kvm_hyp_arm_smmu_v3_smmus) +smmu_deinit_device (--smmu); +smmu_reclaim_pages (hyp_virt_to_phys (kvm_hyp_arm_smmu_v3_smmus), + smmu_arr_size); +return ret; }static void smmu_host_stage2_idmap (phys_addr_t start,phys_addr_t end,int prot)
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 17/27 iommu/arm-smmu-v3-kvm: Probe SMMU HW
In reply to Mostafa SalehProbe SMMU features from the IDR register space, most of the logic is common with the kernel.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 57 ++++++++++++++++++- .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h | 8 +++ 3 files changed, 64 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -0
@@ -49,6 +49,7 @@ struct arm_vsmmu;#define IDR0_S2P (1 << 0)#define ARM_SMMU_IDR1 0x4 +#define IDR1_ECMDQ (1 << 31)#define IDR1_TABLES_PRESET (1 << 30)#define IDR1_QUEUES_PRESET (1 << 29)#define IDR1_REL (1 << 28)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +55 -2
78 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +8 -0
@@ -12,12 +12,20 @@* * Other members are filled and used at runtime by the SMMU driver. * @base Virtual address of SMMU registers +* @ias IPA size +* @oas PA size +* @pgsize_bitmap Supported page sizes +* @sid_bits Max number of SID bits supported */ struct hyp_arm_smmu_v3_device {phys_addr_t mmio_addr;size_t mmio_size;void __iomem *base;u32 features; +unsigned long ias; +unsigned long oas; +unsigned long pgsize_bitmap; +unsigned int sid_bits; };extern size_t kvm_nvhe_sym (kvm_hyp_arm_smmu_v3_count );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 17/27] iommu/arm-smmu-v3-kvm: Probe SMMU HW
Quoted 7 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +7
-0+/* +* Mini-probe and validation for the hypervisor. +*/ +static int smmu_probe (struct hyp_arm_smmu_v3_device *smmu) +{ +u32 reg; +
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 65
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +10
-0+/* IDR0 */ + reg =readl_relaxed (smmu->base +ARM_SMMU_IDR0 ); + smmu->features =smmu_idr0_features (reg); + +/* +* Some MMU600 and MMU700 have errata that prevent them from using nesting, +* not sure how can we identify those, so it's recommended not to enable this +* drivers on such systems. +* And preventing any of those will be too restrictive +*/
This driver is doing nesting though ??
Shouldn't you detect those IPs and not support a S1 at all so there is no nesting? Identity only?
Jason
Re: [PATCH v5 17/27] iommu/arm-smmu-v3-kvm: Probe SMMU HW
Quoted 22 lines · Jason Gunthorpe
Quoted 7 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +7
-0+/* +* Mini-probe and validation for the hypervisor. +*/ +static int smmu_probe (struct hyp_arm_smmu_v3_device *smmu) +{ +u32 reg; +drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 65
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +10
-0+/* IDR0 */ + reg =readl_relaxed (smmu->base +ARM_SMMU_IDR0 ); + smmu->features =smmu_idr0_features (reg); + +/* +* Some MMU600 and MMU700 have errata that prevent them from using nesting, +* not sure how can we identify those, so it's recommended not to enable this +* drivers on such systems. +* And preventing any of those will be too restrictive +*/ This driver is doing nesting though ??
Quoted 2 lines
Shouldn't you detect those IPs and not support a S1 at all so there is no nesting? Identity only?
I can see that the errarta are fixed in r1p1, and from the MMU-700 TRM, we can extract this information from IIDR[1], I guess we can allow nesting for all SMMUs except the one affected. Actually we can do that at the moment from the main driver too, I can prepare a patch for that.
However nesting is still mandatroy, the fallback path for the KVM driver if nesting is not supported (just S2 for example) is to fail and let the upstream driver probe the platform devices with KVM out of the picture.
The main reason for this is that the KVM driver relies on CFGI to know which SIDs to configure, for s2 only cases there won’t be trap and emulate as the hypervisor would be take control of all the SMMUs (as v3 of this series which requires HVCs)
It might be useful to support only S2 SMMUs, but I’d like to keep this series simple and then we can build on top of it.
Thanks,
Mostafa
Quoted 2 lines
Jason
PATCH v5 18/27 iommu/arm-smmu-v3-kvm: Add MMIO emulation
In reply to Mostafa SalehAt the moment most registers are just passthrough, then in the next patches CMDQ/STE emulation will be added which inserts logic to some register access.
--- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 126 ++++++++++++++++++ .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h | 10 ++ 2 files changed, 136 insertions(+)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +126 -0
@@ -8,6 +8,7 @@#include <nvhe/iommu.h> #include <nvhe/mem_protect.h> +#include <nvhe/trap_handler.h> #include "arm_smmu_v3.h" #include "../arm-smmu-v3.h"
@@ -115,6 +116,7 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu) ret =smmu_probe (smmu);if (ret) goto out_ret; +hyp_spin_lock_init (&smmu->lock);return 0 ; out_ret:smmu_deinit_device (smmu);
@@ -140,6 +142,8 @@ static int smmu_init(void) goto out_reclaim_smmu; } +BUILD_BUG_ON (sizeof (hyp_spinlock_t) !=sizeof (u32)); +return 0 ; out_reclaim_smmu:
@@ -150,6 +154,127 @@ static int smmu_init(void)return ret; } +static bool smmu_dabt_device (struct hyp_arm_smmu_v3_device *smmu, +struct user_pt_regs *regs, +u64 esr,u32 off) +{ +bool is_write = esr & ESR_ELx_WNR; +unsigned int len =BIT ((esr & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT); +int rd = (esr & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT; +const u64 read_write =-1ULL ; +const u64 no_access =0 ; +u64 mask = no_access; +const u64 read_only = is_write ? no_access : read_write; +u64 val = regs->regs[rd]; + +switch (off) { +case ARM_SMMU_IDR0 : +/* Clear stage-2 support, hide MSI to avoid write back to cmdq */ + mask = read_only & ~(IDR0_S2P |IDR0_VMID16 |IDR0_MSI |IDR0_HYP ); +WARN_ON (len !=sizeof (u32)); +break ; +/* Passthrough the register access for bisectiblity, handled later */ +case ARM_SMMU_CMDQ_BASE : +case ARM_SMMU_CMDQ_PROD : +case ARM_SMMU_CMDQ_CONS : +case ARM_SMMU_STRTAB_BASE : +case ARM_SMMU_STRTAB_BASE_CFG : +case ARM_SMMU_GBPA : + mask = read_write; +break ; +case ARM_SMMU_CR0 : + mask = read_write; +WARN_ON (len !=sizeof (u32)); +break ; +case ARM_SMMU_CR1 : { +/* Based on Linux implementation */ +u64 cr2_template =FIELD_PREP (CR1_TABLE_SH ,ARM_SMMU_SH_ISH ) | +FIELD_PREP (CR1_TABLE_OC ,CR1_CACHE_WB ) | +FIELD_PREP (CR1_TABLE_IC ,CR1_CACHE_WB ) | +FIELD_PREP (CR1_QUEUE_SH ,ARM_SMMU_SH_ISH ) | +FIELD_PREP (CR1_QUEUE_OC ,CR1_CACHE_WB ) | +FIELD_PREP (CR1_QUEUE_IC ,CR1_CACHE_WB ); +/* Don't mess with shareability/cacheability. */ +if (is_write) +WARN_ON (val != cr2_template); + mask = read_write; +WARN_ON (len !=sizeof (u32)); +break ; + } +/* +* These should be safe, just enforce RO or RW and size according to architecture. +* There are some other registers that are not used by Linux as IDR2, IDR4 +* that won't be allowed. +*/ +case ARM_SMMU_EVTQ_PROD +SZ_64K : +case ARM_SMMU_EVTQ_CONS +SZ_64K : +case ARM_SMMU_EVTQ_IRQ_CFG1 : +case ARM_SMMU_EVTQ_IRQ_CFG2 : +case ARM_SMMU_PRIQ_PROD +SZ_64K : +case ARM_SMMU_PRIQ_CONS +SZ_64K : +case ARM_SMMU_PRIQ_IRQ_CFG1 : +case ARM_SMMU_PRIQ_IRQ_CFG2 : +case ARM_SMMU_GERRORN : +case ARM_SMMU_GERROR_IRQ_CFG1 : +case ARM_SMMU_GERROR_IRQ_CFG2 : +case ARM_SMMU_IRQ_CTRLACK : +case ARM_SMMU_IRQ_CTRL : +case ARM_SMMU_CR0ACK : +case ARM_SMMU_CR2 : +/* These are 32 bit registers. */ +WARN_ON (len !=sizeof (u32)); + fallthrough; +case ARM_SMMU_EVTQ_BASE : +case ARM_SMMU_EVTQ_IRQ_CFG0 : +case ARM_SMMU_PRIQ_BASE : +case ARM_SMMU_PRIQ_IRQ_CFG0 : +case ARM_SMMU_GERROR_IRQ_CFG0 : + mask = read_write; +break ; +case ARM_SMMU_IIDR : +case ARM_SMMU_IDR5 : +case ARM_SMMU_IDR3 : +case ARM_SMMU_IDR1 : +case ARM_SMMU_GERROR : +WARN_ON (len !=sizeof (u32)); + mask = read_only; + }; + +if (WARN_ON (!mask)) + goto out_ret; + +if (is_write) { +if (len ==sizeof (u64)) +writeq_relaxed (regs->regs[rd] & mask, smmu->base + off); +else +writel_relaxed (regs->regs[rd] & mask, smmu->base + off); + }else { +if (len ==sizeof (u64)) + regs->regs[rd] =readq_relaxed (smmu->base + off) & mask; +else + regs->regs[rd] =readl_relaxed (smmu->base + off) & mask; + } + +out_ret: +return true; +} + +static bool smmu_dabt_handler (struct user_pt_regs *regs,u64 esr,u64 addr) +{ +struct hyp_arm_smmu_v3_device *smmu; +bool ret; + +for_each_smmu (smmu) { +if (addr < smmu->mmio_addr || addr >= smmu->mmio_addr + smmu->mmio_size) +continue ; +hyp_spin_lock (&smmu->lock); + ret =smmu_dabt_device (smmu, regs, esr, addr - smmu->mmio_addr); +hyp_spin_unlock (&smmu->lock); +return ret; + } +return false; +} +static void smmu_host_stage2_idmap (phys_addr_t start,phys_addr_t end,int prot) { }
@@ -158,4 +283,5 @@ static void smmu_host_stage2_idmap(phys_addr_t start, phys_addr_t end, int prot)struct kvm_iommu_ops smmu_ops = { .init = smmu_init, .host_stage2_idmap = smmu_host_stage2_idmap, + .dabt_handler = smmu_dabt_handler, };
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +10 -0
@@ -4,6 +4,10 @@#include <asm/kvm_asm.h> +#ifdef __KVM_NVHE_HYPERVISOR__ +#include <nvhe/spinlock.h> +#endif +/* * Parameters from the trusted host: * @mmio_addr base address of the SMMU registers
@@ -16,6 +20,7 @@* @oas PA size * @pgsize_bitmap Supported page sizes * @sid_bits Max number of SID bits supported +* @lock Lock to protect SMMU */ struct hyp_arm_smmu_v3_device {phys_addr_t mmio_addr;
@@ -26,6 +31,11 @@ struct hyp_arm_smmu_v3_device {unsigned long oas;unsigned long pgsize_bitmap;unsigned int sid_bits; +#ifdef __KVM_NVHE_HYPERVISOR__ +hyp_spinlock_t lock; +#else +u32 lock; +#endif };extern size_t kvm_nvhe_sym (kvm_hyp_arm_smmu_v3_count );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 19/27 iommu/arm-smmu-v3-kvm: Shadow the command queue
In reply to Mostafa SalehAt boot allocate a command queue per SMMU which is used as a shadow by the hypervisor.
The command queue size is 64K which is more than enough, as the hypervisor would consume all the entries per a command queue prod write, which means it can handle up to 4096 at a time.
Then, the host command queue needs to be pinned in a shared state, so it can't be donated to VMs, and avoid tricking the hypervisor into accessing them. This is done each time the command queue is enabled, and undone each time the command queue is disabled.
The hypervisor won’t access the host command queue when it is disabled from the host.
--- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c | 16 +++ .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 103 +++++++++++++++++- .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h | 8 ++ 3 files changed, 126 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +16 -0
@@ -15,6 +15,8 @@#include "arm-smmu-v3.h" #include "pkvm/arm_smmu_v3.h" +#define SMMU_KVM_CMDQ_ORDER 4 +extern struct kvm_iommu_ops kvm_nvhe_sym (smmu_ops );static size_t kvm_arm_smmu_count;
@@ -71,6 +73,7 @@ static struct platform_driver smmuv3_nesting_driver;static int smmuv3_nesting_probe (struct platform_device *pdev) {struct resource *res; +void *cmdq_base;struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur]; res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );
@@ -84,6 +87,19 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)if (of_dma_is_coherent (pdev->dev.of_node)) smmu->features |=ARM_SMMU_FEAT_COHERENCY ; +/* +* Allocate the shadow command queue, it doesn't have to be the same +* size as the host. +* Only populate base_dma and llq.max_n_shift, the hypervisor will init +* the rest. +*/ + cmdq_base = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO,SMMU_KVM_CMDQ_ORDER ); +if (!cmdq_base) +return -ENOMEM ; + + smmu->cmdq.base_dma =virt_to_phys (cmdq_base); + smmu->cmdq.llq.max_n_shift =SMMU_KVM_CMDQ_ORDER +PAGE_SHIFT -CMDQ_ENT_SZ_SHIFT ; + kvm_arm_smmu_cur++;return 0 ; }
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +102 -1
152 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +8 -0
@@ -8,6 +8,8 @@#include <nvhe/spinlock.h> #endif +#include "../arm-smmu-v3.h" +/* * Parameters from the trusted host: * @mmio_addr base address of the SMMU registers
@@ -21,6 +23,9 @@* @pgsize_bitmap Supported page sizes * @sid_bits Max number of SID bits supported * @lock Lock to protect SMMU +* @cmdq CMDQ as observed by HW +* @cmdq_host Host view of the CMDQ, only q_base and llq used. +* @cr0 Last value of CR0 */ struct hyp_arm_smmu_v3_device {phys_addr_t mmio_addr;
@@ -36,6 +41,9 @@ struct hyp_arm_smmu_v3_device {#else u32 lock;#endif +struct arm_smmu_queue cmdq; +struct arm_smmu_queue cmdq_host; +u32 cr0; };extern size_t kvm_nvhe_sym (kvm_hyp_arm_smmu_v3_count );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 20/27 iommu/arm-smmu-v3-kvm: Add CMDQ functions
In reply to Mostafa SalehAdd functions to access the command queue, there are 2 main usage:
- Hypervisor's own commands, as TLB invalidation, would use functions as smmu_send_cmd(), which creates and sends a command.
- Add host commands to the shadow command queue, after being filtered, these will be added with smmu_add_cmd_raw.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 14 +-- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 102 ++++++++++++++++++ 2 files changed, 110 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +8 -6
@@ -1015,19 +1015,21 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,int arm_smmu_cmdq_build_cmd (u64 *cmd,struct arm_smmu_cmdq_ent *ent);/* Queue functions shared between kernel and hyp. */ -static inline bool queue_has_space (struct arm_smmu_ll_queue *q,u32 n) +static inline u32 queue_space (struct arm_smmu_ll_queue *q) { -u32 space, prod, cons; +u32 prod, cons; prod =Q_IDX (q, q->prod); cons =Q_IDX (q, q->cons);if (Q_WRP (q, q->prod) ==Q_WRP (q, q->cons)) - space = (1 << q->max_n_shift) - (prod - cons); -else - space = cons - prod; +return (1 << q->max_n_shift) - (prod - cons); +return cons - prod; +} -return space >= n; +static inline bool queue_has_space (struct arm_smmu_ll_queue *q,u32 n) +{ +return queue_space (q) >= n; }static inline bool queue_full (struct arm_smmu_ll_queue *q)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +102 -0
114 lines not on this page · raw message
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 21/27 iommu/arm-smmu-v3-kvm: Emulate CMDQ for host
In reply to Mostafa SalehDon’t allow access to the command queue from the host:
- ARM_SMMU_CMDQ_BASE: Only allowed to be written when CMDQ is disabled, we use it to keep track of the host command queue base.
Reads return the saved value.
- ARM_SMMU_CMDQ_PROD: Writes trigger command queue emulation which sanitise and filters the whole range. Reads returns the host copy.
- ARM_SMMU_CMDQ_CONS: Writes move the sw copy of the cons, but the host can’t skip commands once submitted. Reads return the emulated value and the error bits in the actual cons.
--- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 119 +++++++++++++++++- 1 file changed, 115 insertions(+), 4 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +115 -4
@@ -60,6 +60,16 @@ static void smmu_reclaim_pages(u64 phys, size_t size)WARN_ON (__pkvm_hyp_donate_host (phys >>PAGE_SHIFT , size >>PAGE_SHIFT )); } +static void smmu_copy_from_host (struct hyp_arm_smmu_v3_device *smmu, +void *dst_hyp_va,void *src_hyp_va, +size_t size) +{ +/* Clean and inval DC as the kernel uses NC mapping. */ +if (!(smmu->features &ARM_SMMU_FEAT_COHERENCY )) +kvm_flush_dcache_to_poc (src_hyp_va, size); +memcpy (dst_hyp_va, src_hyp_va, size); +} +/* * CMDQ, STE host copies are accessed by the hypervisor, we share them to * - Prevent the host from passing protected VM memory.
@@ -89,7 +99,6 @@ static int smmu_unshare_pages(phys_addr_t addr, size_t size)return 0 ; } -__maybe_unused static bool smmu_cmdq_has_space (struct arm_smmu_queue *cmdq,u32 n) {struct arm_smmu_ll_queue *llq = &cmdq->llq;
@@ -327,6 +336,88 @@ static int smmu_init(void)return ret; } +static bool smmu_filter_command (struct hyp_arm_smmu_v3_device *smmu,u64 *command) +{ +u64 type =FIELD_GET (CMDQ_0_OP , command[0 ]); + +switch (type) { +case CMDQ_OP_CFGI_STE : +/* TBD: SHADOW_STE*/ +break ; +case CMDQ_OP_CFGI_ALL : + { +/* +* Linux doesn't use range STE invalidation, and only use this +* for CFGI_ALL, which is done on reset and not on an new STE +* being used. +* Although, this is not architectural we rely on the current Linux +* implementation. +*/ +WARN_ON ((FIELD_GET (CMDQ_CFGI_1_RANGE , command[1 ]) !=31 )); +break ; + } +case CMDQ_OP_TLBI_NH_ASID : +case CMDQ_OP_TLBI_NH_VA : +case 0x13 :/* CMD_TLBI_NH_VAA: Not used by Linux */ + { +/* Only allow VMID = 0*/ +if (FIELD_GET (CMDQ_TLBI_0_VMID , command[0 ]) ==0 ) +break ; +break ; + } +case 0x10 :/* CMD_TLBI_NH_ALL: Not used by Linux */ +case CMDQ_OP_TLBI_EL2_ALL : +case CMDQ_OP_TLBI_EL2_VA : +case CMDQ_OP_TLBI_EL2_ASID : +case CMDQ_OP_TLBI_S12_VMALL : +case 0x23 :/* CMD_TLBI_EL2_VAA: Not used by Linux */ +return WARN_ON (true); +case CMDQ_OP_CMD_SYNC : +if (FIELD_GET (CMDQ_SYNC_0_CS , command[0 ]) ==CMDQ_SYNC_0_CS_IRQ ) { +/* Allow it, but let the host timeout, as this should never happen. */ + command[0 ] &= ~CMDQ_SYNC_0_CS ; + command[0 ] |=FIELD_PREP (CMDQ_SYNC_0_CS ,CMDQ_SYNC_0_CS_SEV ); + command[1 ] &= ~CMDQ_SYNC_1_MSIADDR_MASK ; + } +break ; + } + +return false; +} + +static void smmu_emulate_cmdq_insert (struct hyp_arm_smmu_v3_device *smmu) +{ +u64 *host_cmdq =hyp_phys_to_virt (smmu->cmdq_host.q_base &Q_BASE_ADDR_MASK ); +int idx; +u64 cmd[CMDQ_ENT_DWORDS ]; +bool skip; +u32 space; +bool use_wfe = smmu->features &ARM_SMMU_FEAT_SEV ; + +if (!is_cmdq_enabled (smmu)) +return ; + + space = (1 << (smmu->cmdq_host.llq.max_n_shift)) -queue_space (&smmu->cmdq_host.llq); +/* Wait for the command queue to have some space. */ +WARN_ON (smmu_wait (use_wfe,smmu_cmdq_has_space (&smmu->cmdq, space))); + +while (space--) { + idx =Q_IDX (&smmu->cmdq_host.llq, smmu->cmdq_host.llq.cons); +queue_inc_cons (&smmu->cmdq_host.llq); + +smmu_copy_from_host (smmu, cmd, &host_cmdq[idx *CMDQ_ENT_DWORDS ], +CMDQ_ENT_DWORDS <<3 ); + skip =smmu_filter_command (smmu, cmd); +if (skip) +continue ; +smmu_add_cmd_raw (smmu, cmd); + } + +writel_relaxed (smmu->cmdq.llq.prod, smmu->cmdq.prod_reg); + +WARN_ON (smmu_wait (use_wfe,smmu_cmdq_empty (&smmu->cmdq))); +} +static void smmu_emulate_cmdq_enable (struct hyp_arm_smmu_v3_device *smmu) { smmu->cmdq_host.llq.max_n_shift = smmu->cmdq_host.q_base &Q_BASE_LOG2SIZE ;
@@ -360,17 +451,37 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, mask = read_only & ~(IDR0_S2P |IDR0_VMID16 |IDR0_MSI |IDR0_HYP );WARN_ON (len !=sizeof (u32));break ; -/* Passthrough the register access for bisectiblity, handled later */ case ARM_SMMU_CMDQ_BASE :if (is_write) {/* Not allowed by the architecture */ WARN_ON (is_cmdq_enabled (smmu)); smmu->cmdq_host.q_base = val; + }else { + regs->regs[rd] = smmu->cmdq_host.q_base; } - mask = read_write; -break ; + goto out_ret;case ARM_SMMU_CMDQ_PROD : +if (is_write) { + smmu->cmdq_host.llq.prod = val; +smmu_emulate_cmdq_insert (smmu); + }else { + regs->regs[rd] = smmu->cmdq_host.llq.prod; + } + goto out_ret;case ARM_SMMU_CMDQ_CONS : +if (is_write) { +/* Not allowed by the architecture */ +WARN_ON (is_cmdq_enabled (smmu)); + smmu->cmdq_host.llq.cons = val; + }else { +/* Propagate errors back to the host.*/ +u32 cons =readl_relaxed (smmu->base +ARM_SMMU_CMDQ_CONS ); +u32 err =CMDQ_CONS_ERR & cons; + + regs->regs[rd] = smmu->cmdq_host.llq.cons | err; + } + goto out_ret; +/* Passthrough the register access for bisectiblity, handled later */ case ARM_SMMU_STRTAB_BASE :case ARM_SMMU_STRTAB_BASE_CFG :case ARM_SMMU_GBPA :
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 22/27 iommu/arm-smmu-v3-kvm: Shadow stream table
In reply to Mostafa SalehThis patch allocates the shadow stream table per SMMU. We choose the size of that table to be 1MB which is the max size used by host in the case of 2 levels.
In this patch all the host writes are still paththrough for bisectibility, that is changed next where CFGI commands will be trapped and used to update the shadow copy hypervisor that will be used by HW.
Similar to the command queue, the host stream table is shared/unshared each time the SMMU is enabled/disabled.
Handling of L2 tables is also done in the next patch when the shadowing is added.
--- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c | 11 +- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 114 ++++++++++++++++++ .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h | 10 ++ 3 files changed, 134 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +10 -1
@@ -16,6 +16,8 @@#include "pkvm/arm_smmu_v3.h" #define SMMU_KVM_CMDQ_ORDER 4 +#define SMMU_KVM_STRTAB_ORDER (get_order(STRTAB_MAX_L1_ENTRIES * \ + sizeof(struct arm_smmu_strtab_l1)))extern struct kvm_iommu_ops kvm_nvhe_sym (smmu_ops );
@@ -73,7 +75,7 @@ static struct platform_driver smmuv3_nesting_driver;static int smmuv3_nesting_probe (struct platform_device *pdev) {struct resource *res; -void *cmdq_base; +void *cmdq_base, *strtab;struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur]; res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );
@@ -100,6 +102,13 @@ static int smmuv3_nesting_probe(struct platform_device *pdev) smmu->cmdq.base_dma =virt_to_phys (cmdq_base); smmu->cmdq.llq.max_n_shift =SMMU_KVM_CMDQ_ORDER +PAGE_SHIFT -CMDQ_ENT_SZ_SHIFT ; + strtab = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO,SMMU_KVM_STRTAB_ORDER ); +if (!strtab) +return -ENOMEM ; + + smmu->strtab_dma =virt_to_phys (strtab); + smmu->strtab_size =PAGE_SIZE <<SMMU_KVM_STRTAB_ORDER ; + kvm_arm_smmu_cur++;return 0 ; }
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +114 -0
163 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +10 -0
@@ -15,6 +15,8 @@* @mmio_addr base address of the SMMU registers * @mmio_size size of the registers resource * @features Features of SMMUv3, subset of the main driver +* @strtab_dma Phys address of stream table +* @strtab_size Stream table size * * Other members are filled and used at runtime by the SMMU driver. * @base Virtual address of SMMU registers
@@ -26,6 +28,9 @@* @cmdq CMDQ as observed by HW * @cmdq_host Host view of the CMDQ, only q_base and llq used. * @cr0 Last value of CR0 +* @host_ste_cfg Host stream table config +* @host_ste_base Host stream table base +* @strtab_cfg Stream table as seen by HW */ struct hyp_arm_smmu_v3_device {phys_addr_t mmio_addr;
@@ -44,6 +49,11 @@ struct hyp_arm_smmu_v3_device {struct arm_smmu_queue cmdq;struct arm_smmu_queue cmdq_host;u32 cr0; +dma_addr_t strtab_dma; +size_t strtab_size; +u64 host_ste_cfg; +u64 host_ste_base; +struct arm_smmu_strtab_cfg strtab_cfg; };extern size_t kvm_nvhe_sym (kvm_hyp_arm_smmu_v3_count );
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 23/27 iommu/arm-smmu-v3-kvm: Shadow STEs
In reply to Mostafa SalehThis patch adds STE emulation, this is done when the host sends the CFGI_STE command.
In this patch we copy the STE as is to the shadow owned by the hypervisor, in the next patch, stage-2 page table will be attached.
--- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 92 +++++++++++++++++-- 1 file changed, 86 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +86 -6
@@ -22,6 +22,9 @@ struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;#define strtab_split (smmu) (FIELD_GET(STRTAB_BASE_CFG_SPLIT, (smmu)->host_ste_cfg))#define strtab_l1_size (smmu) ((1 << (strtab_log2size(smmu) - strtab_split(smmu))) * \ (sizeof(struct arm_smmu_strtab_l1))) +#define strtab_hyp_base (smmu) ((smmu)->features & ARM_SMMU_FEAT_2_LVL_STRTAB ? \ + (u64 *)(smmu)->strtab_cfg.l2.l1tab :\ + (u64 *)(smmu)->strtab_cfg.linear.table)#define for_each_smmu (smmu) \ for ((smmu) = kvm_hyp_arm_smmu_v3_smmus; \
@@ -283,6 +286,80 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)return 0 ; } +/* Get an STE for a stream table base. */ +static struct arm_smmu_ste *smmu_get_ste_ptr (struct hyp_arm_smmu_v3_device *smmu, +u32 sid,u64 *strtab) +{ +struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; +struct arm_smmu_ste *table = (struct arm_smmu_ste *)strtab; + +if (smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ) { +struct arm_smmu_strtab_l1 *l1tab = (struct arm_smmu_strtab_l1 *)strtab; +u32 l1_idx =arm_smmu_strtab_l1_idx (sid); +struct arm_smmu_strtab_l2 *l2ptr; + +if (WARN_ON (l1_idx >= cfg->l2.num_l1_ents) || + !(l1tab[l1_idx].l2ptr &STRTAB_L1_DESC_SPAN )) +return NULL ; + + l2ptr =hyp_phys_to_virt (l1tab[l1_idx].l2ptr &STRTAB_L1_DESC_L2PTR_MASK ); +/* Two-level walk */ +return &l2ptr->stes[arm_smmu_strtab_l2_idx (sid)]; + } +if (WARN_ON (sid >= cfg->linear.num_ents)) +return NULL ; +return &table[sid]; +} + +static int smmu_shadow_l2_strtab (struct hyp_arm_smmu_v3_device *smmu,u32 sid) +{ +u32 idx =arm_smmu_strtab_l1_idx (sid); +u64 *host_ste_base =hyp_phys_to_virt (strtab_host_base (smmu)); +struct arm_smmu_strtab_l1 *l1_desc = &smmu->strtab_cfg.l2.l1tab[idx]; +u64 l1_desc_host; +struct arm_smmu_strtab_l2 *l2table; + + l2table =kvm_iommu_donate_pages (get_order (sizeof (*l2table))); +if (!l2table) +return -ENOMEM ; + +if (!(smmu->features &ARM_SMMU_FEAT_COHERENCY )) +kvm_flush_dcache_to_poc (&host_ste_base[idx],sizeof (*l1_desc)); + l1_desc_host = host_ste_base[idx]; + +arm_smmu_write_strtab_l1_desc (l1_desc,hyp_virt_to_phys (l2table)); +if (!(smmu->features &ARM_SMMU_FEAT_COHERENCY )) +kvm_flush_dcache_to_poc (l1_desc,sizeof (*l1_desc)); + +smmu_share_pages (l1_desc_host &STRTAB_L1_DESC_L2PTR_MASK ,sizeof (*l2table)); +return 0 ; +} + +static void smmu_reshadow_ste (struct hyp_arm_smmu_v3_device *smmu,u32 sid,bool leaf) +{ +u64 *host_ste_base =hyp_phys_to_virt (strtab_host_base (smmu)); +u64 *hyp_ste_base =strtab_hyp_base (smmu); +struct arm_smmu_ste *host_ste_ptr =smmu_get_ste_ptr (smmu, sid, host_ste_base); +struct arm_smmu_ste *hyp_ste_ptr =smmu_get_ste_ptr (smmu, sid, hyp_ste_base); + +/* +* Linux only uses leaf = 1, when leaf is 0, we need to verify that this +* is a 2 level table and reshadow of l2. +* Also Linux never clears l1 ptr, that needs to free the old shadow. +*/ +if (WARN_ON (!leaf || !host_ste_ptr)) +return ; + +/* If host is valid and hyp is not, means a new L1 installed. */ +if (!hyp_ste_ptr) { +WARN_ON (smmu_shadow_l2_strtab (smmu, sid)); + hyp_ste_ptr =smmu_get_ste_ptr (smmu, sid, hyp_ste_base); + } + +smmu_copy_from_host (smmu, hyp_ste_ptr->data, host_ste_ptr->data, +STRTAB_STE_DWORDS <<3 ); +} +static int smmu_init_strtab (struct hyp_arm_smmu_v3_device *smmu) {int ret;
@@ -402,8 +479,13 @@ static bool smmu_filter_command(struct hyp_arm_smmu_v3_device *smmu, u64 *commanswitch (type) {case CMDQ_OP_CFGI_STE : -/* TBD: SHADOW_STE*/ + { +u32 sid =FIELD_GET (CMDQ_CFGI_0_SID , command[0 ]); +u32 leaf =FIELD_GET (CMDQ_CFGI_1_LEAF , command[1 ]); + +smmu_reshadow_ste (smmu, sid, leaf);break ; + }case CMDQ_OP_CFGI_ALL : {/*
@@ -576,23 +658,21 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, regs->regs[rd] = smmu->cmdq_host.llq.cons | err; } goto out_ret; -/* Passthrough the register access for bisectiblity, handled later */ case ARM_SMMU_STRTAB_BASE :if (is_write) {/* Must only be written when SMMU_CR0.SMMUEN == 0.*/ WARN_ON (is_smmu_enabled (smmu)); smmu->host_ste_base = val; } - mask = read_write; -break ; + goto out_ret;case ARM_SMMU_STRTAB_BASE_CFG :if (is_write) {/* Must only be written when SMMU_CR0.SMMUEN == 0.*/ WARN_ON (is_smmu_enabled (smmu)); smmu->host_ste_cfg = val; } - mask = read_write; -break ; + goto out_ret; +/* Passthrough the register access for bisectiblity, handled later */ case ARM_SMMU_GBPA : mask = read_write;break ;
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 24/27 iommu/arm-smmu-v3-kvm: Emulate GBPA
In reply to Mostafa SalehThe last bit of emulation is GBPA. it must be always set to ABORT, as when the SMMU is disabled it’s not allowed for the host to bypass the SMMU.
That ‘s is done by setting the GBPA to ABORT at init time, and host writes are always ignored and host reads always return ABORT.
--- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +18 -3
@@ -115,6 +115,14 @@ static int smmu_unshare_pages(phys_addr_t addr, size_t size)return 0 ; } +static int smmu_abort_gbpa (struct hyp_arm_smmu_v3_device *smmu) +{ +writel_relaxed (GBPA_UPDATE |GBPA_ABORT , smmu->base +ARM_SMMU_GBPA ); +/* Wait till UPDATE is cleared. */ +return smmu_wait (false, +readl_relaxed (smmu->base +ARM_SMMU_GBPA ) ==GBPA_ABORT ); +} +static bool smmu_cmdq_has_space (struct arm_smmu_queue *cmdq,u32 n) {struct arm_smmu_ll_queue *llq = &cmdq->llq;
@@ -435,6 +443,10 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)if (ret) goto out_ret; + ret =smmu_abort_gbpa (smmu); +if (ret) + goto out_ret; +return 0 ; out_ret:
@@ -672,10 +684,13 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, smmu->host_ste_cfg = val; } goto out_ret; -/* Passthrough the register access for bisectiblity, handled later */ case ARM_SMMU_GBPA : - mask = read_write; -break ; +/* Ignore write, always read to abort. */ +if (!is_write) + regs->regs[rd] =GBPA_ABORT ; + +WARN_ON (len !=sizeof (u32)); + goto out_ret;case ARM_SMMU_CR0 :if (is_write) {bool last_cmdq_en =is_cmdq_enabled (smmu);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 25/27 iommu/arm-smmu-v3-kvm: Support io-pgtable
In reply to Mostafa SalehAdd hooks needed to support io-pgtable-arm, mostly about memory allocation.
Also add a function to allocate s2 64 bit page table.
--- arch/arm64/kvm/hyp/nvhe/Makefile | 4 +- .../arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.c | 68 +++++++++++++++++++ drivers/iommu/io-pgtable-arm.c | 2 +- drivers/iommu/io-pgtable-arm.h | 11 +++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.c
arch/arm64/kvm/hyp/nvhe/Makefile +3 -1
@@ -35,7 +35,9 @@ hyp-obj-y += $(lib-objs) HYP_SMMU_V3_DRV_PATH = ../../../../../drivers/iommu/arm/arm-smmu-v3 hyp-obj-$(CONFIG_ARM_SMMU_V3_PKVM) += $(HYP_SMMU_V3_DRV_PATH)/pkvm/arm-smmu-v3.o \ - $(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o + $(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o \ + $(HYP_SMMU_V3_DRV_PATH)/pkvm/io-pgtable-arm-hyp.o \ + $(HYP_SMMU_V3_DRV_PATH)/../../io-pgtable-arm.o ## ## Build rules for compiling nVHE hyp code
drivers/iommu/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.c new file +68 -0
68 lines not on this page · raw message
drivers/iommu/io-pgtable-arm.c +1 -1
@@ -980,7 +980,7 @@ arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)return NULL ; } -static struct io_pgtable * +struct io_pgtable *arm_64_lpae_alloc_pgtable_s2 (struct io_pgtable_cfg *cfg,void *cookie) {u64 sl;
drivers/iommu/io-pgtable-arm.h +11 -0
@@ -41,9 +41,20 @@ void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, void *cookie);void *__arm_lpae_alloc_data (size_t size,gfp_t gfp);void __arm_lpae_free_data (void *p); +struct io_pgtable * +arm_64_lpae_alloc_pgtable_s2 (struct io_pgtable_cfg *cfg,void *cookie);#ifndef __KVM_NVHE_HYPERVISOR__#define __arm_lpae_virt_to_phys __pa#define __arm_lpae_phys_to_virt __va +#else +#include <nvhe/memory.h> +#define __arm_lpae_virt_to_phys hyp_virt_to_phys +#define __arm_lpae_phys_to_virt hyp_phys_to_virt +#undef WARN_ONCE +#define WARN_ONCE(condition, format...) WARN_ON(1) +struct io_pgtable_ops *kvm_alloc_io_pgtable_ops (enum io_pgtable_fmt fmt, +struct io_pgtable_cfg *cfg, +void *cookie);#endif /* !__KVM_NVHE_HYPERVISOR__ */ #endif /* IO_PGTABLE_ARM_H_ */
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 26/27 iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table
In reply to Mostafa SalehBased on the callbacks from the hypervisor, update the SMMUv3 Identity mapped page table.
--- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 189 +++++++++++++++++- 1 file changed, 187 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +187 -2
@@ -12,6 +12,9 @@#include "arm_smmu_v3.h" +#include <linux/io-pgtable.h> +#include "../../../io-pgtable-arm.h" +size_t __ro_after_init kvm_hyp_arm_smmu_v3_count;struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;
@@ -53,6 +56,9 @@ struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; __ret; \ }) +/* Protected by host_mmu.lock from core code. */ +static struct io_pgtable *idmap_pgtable; +static bool is_cmdq_enabled (struct hyp_arm_smmu_v3_device *smmu) {return FIELD_GET (CR0_CMDQEN , smmu->cr0);
@@ -192,7 +198,6 @@ static int smmu_sync_cmd(struct hyp_arm_smmu_v3_device *smmu) smmu_cmdq_empty(&smmu->cmdq)); } -__maybe_unused static int smmu_send_cmd (struct hyp_arm_smmu_v3_device *smmu,struct arm_smmu_cmdq_ent *cmd) {
@@ -204,6 +209,78 @@ static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,return smmu_sync_cmd (smmu); } +static void __smmu_add_cmd (void *__opaque,struct arm_smmu_cmdq_batch *unused, +struct arm_smmu_cmdq_ent *cmd) +{ +struct hyp_arm_smmu_v3_device *smmu = (struct hyp_arm_smmu_v3_device *)__opaque; + +WARN_ON (smmu_add_cmd (smmu, cmd)); +} + +static int smmu_tlb_inv_range_smmu (struct hyp_arm_smmu_v3_device *smmu, +struct arm_smmu_cmdq_ent *cmd, +unsigned long iova,size_t size,size_t granule) +{ +arm_smmu_tlb_inv_build (cmd, iova, size, granule, + idmap_pgtable->cfg.pgsize_bitmap, + smmu->features &ARM_SMMU_FEAT_RANGE_INV , + smmu, __smmu_add_cmd,NULL ); +return smmu_sync_cmd (smmu); +} + +static void smmu_tlb_inv_range (unsigned long iova,size_t size,size_t granule, +bool leaf) +{ +struct arm_smmu_cmdq_ent cmd = { + .opcode =CMDQ_OP_TLBI_S2_IPA , + .tlbi = { + .leaf = leaf, + .vmid =0 , + }, + }; +struct arm_smmu_cmdq_ent cmd_s1 = { + .opcode =CMDQ_OP_TLBI_NH_ALL , + .tlbi = { + .vmid =0 , + }, + }; +struct hyp_arm_smmu_v3_device *smmu; + +for_each_smmu (smmu) { +hyp_spin_lock (&smmu->lock); +/* +* Don't bother if SMMU is disabled, this would be useful for the case +* when RPM is supported to avoid thouching the SMMU MMIO when disabled. +* The hypervisor also asserts CMDQEN is enabled before the SMMU is +* enabled. As otherwise the host can prevent the hypervisor from doing +* TLB invalidations. +*/ +if (is_smmu_enabled (smmu)) { +WARN_ON (smmu_tlb_inv_range_smmu (smmu, &cmd, iova, size, granule)); +WARN_ON (smmu_send_cmd (smmu, &cmd_s1)); + } +hyp_spin_unlock (&smmu->lock); + } +} + +static void smmu_tlb_flush_walk (unsigned long iova,size_t size, +size_t granule,void *cookie) +{ +smmu_tlb_inv_range (iova, size, granule, false); +} + +static void smmu_tlb_add_page (struct iommu_iotlb_gather *gather, +unsigned long iova,size_t granule, +void *cookie) +{ +smmu_tlb_inv_range (iova, granule, granule, true); +} + +static const struct iommu_flush_ops smmu_tlb_ops = { + .tlb_flush_walk = smmu_tlb_flush_walk, + .tlb_add_page = smmu_tlb_add_page, +}; +/* Put the device in a state that can be probed by the host driver. */ static void smmu_deinit_device (struct hyp_arm_smmu_v3_device *smmu) {
@@ -454,6 +531,40 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)return ret; } +static int smmu_init_pgt (void ) +{ +/* Default values overridden based on SMMUs common features. */ +struct io_pgtable_cfg cfg = (struct io_pgtable_cfg ) { + .tlb = &smmu_tlb_ops, + .pgsize_bitmap =-1 , + .ias =48 , + .oas =48 , + .coherent_walk = true, + }; +struct hyp_arm_smmu_v3_device *smmu; +struct io_pgtable_ops *ops; + +for_each_smmu (smmu) { + cfg.ias =min (cfg.ias, smmu->ias); + cfg.oas =min (cfg.oas, smmu->oas); + cfg.pgsize_bitmap &= smmu->pgsize_bitmap; + cfg.coherent_walk &= !!(smmu->features &ARM_SMMU_FEAT_COHERENCY ); + } + +/* Avoid larger input size as this is identity mapped. */ + cfg.ias =min (cfg.ias, cfg.oas); + +/* At least PAGE_SIZE must be supported by all SMMUs*/ +if ((cfg.pgsize_bitmap &PAGE_SIZE ) ==0 ) +return -EINVAL ; + + ops =kvm_alloc_io_pgtable_ops (ARM_64_LPAE_S2 , &cfg,NULL ); +if (!ops) +return -ENOMEM ; + idmap_pgtable =io_pgtable_ops_to_pgtable (ops); +return 0 ; +} +static int smmu_init (void ) {int ret;
@@ -475,7 +586,7 @@ static int smmu_init(void)BUILD_BUG_ON (sizeof (hyp_spinlock_t) !=sizeof (u32)); -return 0 ; +return smmu_init_pgt (); out_reclaim_smmu:while (smmu != kvm_hyp_arm_smmu_v3_smmus)
@@ -798,8 +909,82 @@ static bool smmu_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr)return false; } +static size_t smmu_pgsize_idmap (size_t size,u64 paddr,size_t pgsize_bitmap) +{ +size_t pgsizes; + +/* Remove page sizes that are larger than the current size */ + pgsizes = pgsize_bitmap &GENMASK_ULL (__fls (size),0 ); + +/* Remove page sizes that the address is not aligned to. */ +if (likely (paddr)) + pgsizes &=GENMASK_ULL (__ffs (paddr),0 ); + +WARN_ON (!pgsizes); + +/* Return the larget page size that fits. */ +return BIT (__fls (pgsizes)); +} +static void smmu_host_stage2_idmap (phys_addr_t start,phys_addr_t end,int prot) { +size_t size = end - start; +size_t pgsize =PAGE_SIZE , pgcount; +size_t mapped, unmapped; +int ret; +struct io_pgtable *pgtable = idmap_pgtable; +struct iommu_iotlb_gather gather; + + end =min (end,BIT (pgtable->cfg.oas)); +if (start >= end) +return ; + +if (prot) { +if (!(prot &IOMMU_MMIO )) + prot |=IOMMU_CACHE ; + +while (size) { + mapped =0 ; +/* +* We handle pages size for memory and MMIO differently: +* - memory: Map everything with PAGE_SIZE, that is guaranteed to +* find memory as we allocated enough pages to cover the entire +* memory, we do that as io-pgtable-arm doesn't support +* split_blk_unmap logic any more, so we can't break blocks once +* mapped to tables. +* - MMIO: Unlike memory, pKVM allocate 1G to for all MMIO, while +* the MMIO space can be large, as it is assumed to cover the +* whole IAS that is not memory, we have to use block mappings, +* that is fine for MMIO as it is never donated at the moment, +* so we never need to unmap MMIO at the run time triggereing +* split block logic. +*/ +if (prot &IOMMU_MMIO ) + pgsize =smmu_pgsize_idmap (size, start, pgtable->cfg.pgsize_bitmap); + + pgcount = size / pgsize; + ret = pgtable->ops.map_pages (&pgtable->ops, start, start, + pgsize, pgcount, prot,0 , &mapped); + size -= mapped; + start += mapped; +if (!mapped || ret) +return ; + } + }else { +/* Shouldn't happen. */ +WARN_ON (prot &IOMMU_MMIO ); +while (size) { + pgcount = size / pgsize; + unmapped = pgtable->ops.unmap_pages (&pgtable->ops, start, + pgsize, pgcount, &gather); + size -= unmapped; + start += unmapped; +if (!unmapped) +break ; + } +/* Some memory were not unmapped. */ +WARN_ON (size); + } }/* Shared with the kernel driver in EL1 */
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
PATCH v5 27/27 iommu/arm-smmu-v3-kvm: Enable nesting
In reply to Mostafa SalehNow, as the hypervisor controls the command queue, stream table, and shadows the stage-2 page table.
Enable stage-2 in case the host puts an STE in bypass or stage-1.
--- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +74 -1
@@ -371,6 +371,46 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)return 0 ; } +static void smmu_attach_stage_2 (struct arm_smmu_ste *ste) +{ +unsigned long vttbr; +unsigned long ts, sl, ic, oc, sh, tg, ps; +unsigned long cfg; +struct io_pgtable_cfg *pgt_cfg = &idmap_pgtable->cfg; + + cfg =FIELD_GET (STRTAB_STE_0_CFG , ste->data[0 ]); +if (!FIELD_GET (STRTAB_STE_0_V , ste->data[0 ]) || + (cfg ==STRTAB_STE_0_CFG_ABORT )) +return ; +/* S2 is not advertised, that should never be attempted. */ +if (WARN_ON (cfg ==STRTAB_STE_0_CFG_NESTED )) +return ; + vttbr = pgt_cfg->arm_lpae_s2_cfg.vttbr; + ps = pgt_cfg->arm_lpae_s2_cfg.vtcr.ps; + tg = pgt_cfg->arm_lpae_s2_cfg.vtcr.tg; + sh = pgt_cfg->arm_lpae_s2_cfg.vtcr.sh; + oc = pgt_cfg->arm_lpae_s2_cfg.vtcr.orgn; + ic = pgt_cfg->arm_lpae_s2_cfg.vtcr.irgn; + sl = pgt_cfg->arm_lpae_s2_cfg.vtcr.sl; + ts = pgt_cfg->arm_lpae_s2_cfg.vtcr.tsz; + + ste->data[1 ] |=FIELD_PREP (STRTAB_STE_1_SHCFG ,STRTAB_STE_1_SHCFG_INCOMING ); +/* The host shouldn't write dwords 2 and 3, overwrite them. */ + ste->data[2 ] =FIELD_PREP (STRTAB_STE_2_VTCR , +FIELD_PREP (STRTAB_STE_2_VTCR_S2PS , ps) | +FIELD_PREP (STRTAB_STE_2_VTCR_S2TG , tg) | +FIELD_PREP (STRTAB_STE_2_VTCR_S2SH0 , sh) | +FIELD_PREP (STRTAB_STE_2_VTCR_S2OR0 , oc) | +FIELD_PREP (STRTAB_STE_2_VTCR_S2IR0 , ic) | +FIELD_PREP (STRTAB_STE_2_VTCR_S2SL0 , sl) | +FIELD_PREP (STRTAB_STE_2_VTCR_S2T0SZ , ts)) | +FIELD_PREP (STRTAB_STE_2_S2VMID ,0 ) | +STRTAB_STE_2_S2AA64 |STRTAB_STE_2_S2R ; + ste->data[3 ] = vttbr &STRTAB_STE_3_S2TTB_MASK ; +/* Convert S1 => nested and bypass => S2 */ + ste->data[0 ] |=FIELD_PREP (STRTAB_STE_0_CFG , cfg |BIT (1 )); +} +/* Get an STE for a stream table base. */ static struct arm_smmu_ste *smmu_get_ste_ptr (struct hyp_arm_smmu_v3_device *smmu,u32 sid,u64 *strtab)
@@ -426,6 +466,15 @@ static void smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, boolu64 *hyp_ste_base =strtab_hyp_base (smmu);struct arm_smmu_ste *host_ste_ptr =smmu_get_ste_ptr (smmu, sid, host_ste_base);struct arm_smmu_ste *hyp_ste_ptr =smmu_get_ste_ptr (smmu, sid, hyp_ste_base); +struct arm_smmu_ste target = {}; +struct arm_smmu_cmdq_ent cfgi_cmd = { + .opcode =CMDQ_OP_CFGI_STE , + .cfgi = { + .sid = sid, + .leaf = true, + }, + }; +int i;/* * Linux only uses leaf = 1, when leaf is 0, we need to verify that this
@@ -441,8 +490,32 @@ static void smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool hyp_ste_ptr =smmu_get_ste_ptr (smmu, sid, hyp_ste_base); } -smmu_copy_from_host (smmu, hyp_ste_ptr->data, host_ste_ptr->data, +smmu_copy_from_host (smmu, target.data, host_ste_ptr->data,STRTAB_STE_DWORDS <<3 ); +/* +* Typically, STE update is done as the following +* 1- Write last 7 dwords, while STE is invalid +* 2- CFGI +* 3- Write first dword, making STE valid +* 4- CFGI +* As the SMMU MUST at least load 64 bits atomically +* that gurantees that there is no race between writing +* the STE and the CFGI where the SMMU observes parts +* of the STE. +* In the shadow we update the STE to enable nested translation, +* which requires updating first 4 dwords. +* That is only done if the STE is valid and not in abort. +* Which means it happens at step 4) +* So we need to also write the last 7 dwords and send CFGI +* before writing the first dword. +* There is no need for last CFGI as it's done next. +*/ +smmu_attach_stage_2 (&target); +for (i =1 ; i <STRTAB_STE_DWORDS ; i++) +WRITE_ONCE (hyp_ste_ptr->data[i], target.data[i]); + +WARN_ON (smmu_send_cmd (smmu, &cfgi_cmd)); +WRITE_ONCE (hyp_ste_ptr->data[0 ], target.data[0 ]); }static int smmu_init_strtab (struct hyp_arm_smmu_v3_device *smmu)
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 27/27] iommu/arm-smmu-v3-kvm: Enable nesting
Quoted 15 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +9
-1@@ -441,8 +490,32 @@ static void smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool hyp_ste_ptr =smmu_get_ste_ptr (smmu, sid, hyp_ste_base); } -smmu_copy_from_host (smmu, hyp_ste_ptr->data, host_ste_ptr->data, +smmu_copy_from_host (smmu, target.data, host_ste_ptr->data,STRTAB_STE_DWORDS <<3 ); +/* +* Typically, STE update is done as the following +* 1- Write last 7 dwords, while STE is invalid +* 2- CFGI +* 3- Write first dword, making STE valid +* 4- CFGI +* As the SMMU MUST at least load 64 bits atomically +* that gurantees that there is no race between writing
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool · line 512
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +10
-0+* the STE and the CFGI where the SMMU observes parts +* of the STE. +* In the shadow we update the STE to enable nested translation, +* which requires updating first 4 dwords. +* That is only done if the STE is valid and not in abort. +* Which means it happens at step 4) +* So we need to also write the last 7 dwords and send CFGI +* before writing the first dword. +* There is no need for last CFGI as it's done next. +*/
This really should share the main driver logic to do STE writes in the right order and try to avoid making it non-valid if not necessary.
This will not properly support all the real-world kernel flows around PASID with such a simplistic implementation.
Json
Re: [PATCH v5 27/27] iommu/arm-smmu-v3-kvm: Enable nesting
Quoted 30 lines · Jason Gunthorpe
Quoted 15 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +9
-1@@ -441,8 +490,32 @@ static void smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool hyp_ste_ptr =smmu_get_ste_ptr (smmu, sid, hyp_ste_base); } -smmu_copy_from_host (smmu, hyp_ste_ptr->data, host_ste_ptr->data, +smmu_copy_from_host (smmu, target.data, host_ste_ptr->data,STRTAB_STE_DWORDS <<3 ); +/* +* Typically, STE update is done as the following +* 1- Write last 7 dwords, while STE is invalid +* 2- CFGI +* 3- Write first dword, making STE valid +* 4- CFGI +* As the SMMU MUST at least load 64 bits atomically +* that gurantees that there is no race between writing drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool · line 512
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +10
-0+* the STE and the CFGI where the SMMU observes parts +* of the STE. +* In the shadow we update the STE to enable nested translation, +* which requires updating first 4 dwords. +* That is only done if the STE is valid and not in abort. +* Which means it happens at step 4) +* So we need to also write the last 7 dwords and send CFGI +* before writing the first dword. +* There is no need for last CFGI as it's done next. +*/ This really should share the main driver logic to do STE writes in the right order and try to avoid making it non-valid if not necessary.
Quoted 4 lines
This will not properly support all the real-world kernel flows around PASID with such a simplistic implementation.
I see, would it be OK to keep it as this simple now and add this later, as this is still early support? as I want to keep this series minimal.
I plan to have another seires with some improvements and optimizations, mostly about page tables (using block mappings) and I can include such optimization, specially that the new hitless STE stuff is relatively recent in the main driver.
Thanks,
Mostafa
Quoted 1 line
Json