Guests doens't have a separate domain space as the host, but they share
the upper half of the domain ids, so they would ask for a domain and
get a domain_id as a return.
Also as the guests doesn't know about the IOMMU topology, add
KVM_IOMMU_DOMAIN_ANY_TYPE, which would be used for guests, and the
IOMMU driver should choose the proper type for it.
Allocating a domain for guest, this HVC can need memory for:
- PGD for the domain page table.
- Domain struct for hyp.
For both we return to host to fill the guest iommu memcache.
The HVC returns the ID allocated for this domain, which the guest
can use later for map/unmap.
Bug: 357781595
Bug: 348382247
Bug: 236685427
Change-Id: I2ee84938b60337bee38f2227db53c832878140d0
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Add an owner to each domain either host or a VM which is set at alloc
time, and all other IOMMU operations would be allowed only from the
domain owner.
The new vm member is protected by kvm_iommu_domain_lock while
set/cleared in alloc/free, and the check is done only after the domain
refcount is elevated to avoid racing with alloc.
Bug: 357781595
Bug: 348382247
Bug: 236685427
Change-Id: Ie23379d958922368ec3be2127ba90393709a77bb
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Although hyp_spinlock_t and u32 have the same size, that makes the build
system duplicate the type for EL1/EL2 builds impacting KMI.
Use a single type for lock, and cast it when used at EL2.
Bug: 350677978
Bug: 357781595
Bug: 384432312
Change-Id: I13b7330dcf5c5340e3bad35eab9808388e9a84a3
Signed-off-by: Mostafa Saleh <smostafa@google.com>
KMI is going to be frozen soon, so add padding in IOMMU structs as
it can be helpful to update the driver without changing the ABI.
The updated structs are:
kvm_iommu_ops: kvm_iommu_driver: reserve 8 qwords, this is only
instantiated once, and the most likely one to change, as it
implements the callbacks between the IOMMU code and the hypervisor module.
kvm_iommu_driver: reserve 8 qwords, this is only instantiated once,
this implements the callbacks between the IOMMU code and the kernel module.
kvm_hyp_iommu: reserve 4 qwords, instantiated once per-iommu.
kvm_hyp_iommu_domain: reserve 2 qwords, this is the most instantiated
struct, so we don’t add a lot of padding.
iommu_iotlb_gather: reserve 2 qwords, in case we need to add hypervisor
specific logic in the future.
io_pgtable_ops: reserve 4 qwords, instantiated once per iommu domain.
Bug: 357781595
Bug: 384432312
Change-Id: I1c5c9e0dd4fe1b422c8a20e23df6b9e10f646071
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Snapshot the host stage-2 in the idmapped domain, this is done
differently from the Android14:
- Instead of assuming that the page table maps all the memory and
the snapshot function would only unmap the donated pages, now the
polarity is the opposite, where the page table is assumed to be
empty and the snapshot function would map all host memory, so this
slower, but done only once at boot, and that won't require the
module to know the memory map.
- Instead of calling snapshot at init, now it is exported to modules,
as some IOMMUs as SMMUv3 doens't know the format of the page table
until the device is attached.
Bug: 357781595
Bug: 384432312
Change-Id: I82dbf273eebc4e5c588c230a3a9bb18ef7b468ea
Signed-off-by: Mostafa Saleh <smostafa@google.com>
As HYP_SPIN_LOCK is used by modules, we can't include internal header
for it. Instead we assume its 4, then assert this at runtime.
Bug: 357781595
Bug: 384432312
Change-Id: I6d7b0c8fa8000c4e32c65fd8c98533e6ed17563f
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Add power domain ops to the hypervisor IOMMU driver. We currently make
these assumptions:
* The register state is retained across power off.
* The TLBs are clean on power on.
* Another privileged software (EL3 or SCP FW) handles dependencies
between SMMU and endpoints.
So we just need to make sure that the CPU does not touch the SMMU
registers while it is powered off.
Link: https://lore.kernel.org/all/20241212180423.1578358-24-smostafa@google.com/
Bug: 357781595
Bug: 384432312
Change-Id: Iefe6f06b40510cbd5945cc8acf2bba7bd82a4564
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Add attach/detach dev operations which are forwarded to the driver.
To avoid racing between alloc/free domain and attach/detach dev,
the refcount is used.
Although, as IOMMU attach/detach are per-IOMMU and would require
some sort of locking, nothing in the IOMMU core code need the lock
so delegate that to the driver to use locks when needed and the
hypervisor only guarantees no races between alloc/free domain.
Also, add a new function kvm_iommu_init_device() to initialise common
fields of the IOMMU struct, which is only the lock at the moment.
The IOMMU core code will need to use the lock next for power
management.
Link: https://lore.kernel.org/all/20241212180423.1578358-18-smostafa@google.com/
Bug: 357781595
Bug: 384432312
Change-Id: I969762bda4ab2e5229a2a86942336896b9e8f066
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
The IOMMU domain abstraction allows to share the same page tables
between multiple devices. That may be necessary due to hardware
constraints, if multiple devices cannot be isolated by the IOMMU
(conventional PCI bus for example). It may also help with optimizing
resource or TLB use. For pKVM in particular, it may be useful to reduce
the amount of memory required for page tables. All devices owned by the
host kernel could be attached to the same domain (though that requires
host changes).
There is one shared domain space with all IOMMUs holding up to 2^16
domains.
Link: https://lore.kernel.org/all/20241212180423.1578358-17-smostafa@google.com/
Bug: 357781595
Bug: 384432312
Change-Id: Ic5b0b9511e878e02bea47dc15fbdca4bc9d9af1b
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Mostafa Saleh <smostafa@google.com>