Commit Graph

9 Commits

Author SHA1 Message Date
Mostafa Saleh 38ab8e61f9 ANDROID: KVM: arm64: pviommu: Add alloc/free_domain() HVC ops
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>
2025-04-16 13:39:55 +00:00
Mostafa Saleh 540b4b9ce3 ANDROID: KVM: arm64: iommu: Add owner to domains
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>
2025-04-16 13:39:55 +00:00
Mostafa Saleh 2e19490f3f ANDROID: KVM: arm64: iommu: Use same type for kernel/hyp
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>
2025-02-11 04:01:05 -08:00
Mostafa Saleh d4772b152b ANDROID: KVM: arm64: Pad IOMMU structs for KMI
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>
2025-01-21 13:09:26 +00:00
Mostafa Saleh b2e3ad4708 ANDROID: KVM: arm64: iommu: Snapshot host stage-2
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>
2024-12-24 10:39:44 +00:00
Mostafa Saleh 6ff074eb95 ANDROID: KVM: arm64: Remove HYP_SPIN_LOCK from generated code
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>
2024-12-24 10:39:43 +00:00
Jean-Philippe Brucker 09b0f24506 BACKPORT: FROMLIST: KVM: arm64: iommu: Support power management
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>
2024-12-20 03:25:14 -08:00
Mostafa Saleh 14364a71d0 BACKPORT: FROMLIST: KVM: arm64: iommu: Add {attach, detach}_dev
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>
2024-12-20 03:25:14 -08:00
Mostafa Saleh 50286c4984 BACKPORT: FROMLIST: KVM: arm64: iommu: Add domains
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>
2024-12-20 03:25:14 -08:00