[RFC PATCH v1 27/38] coco: host: arm64: add RSI_RDEV_GET_INSTANCE_ID related exit handler

Aneesh Kumar K.V (Arm)
2025-07-28T13:55:18Z

Mapping the VDEV object that matches a specified virtual device ID
results in a REC exit, which is handled by the VDEV request exit
handler.

Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
arch/arm64/include/asm/rmi_cmds.h | 8 +++++
arch/arm64/include/asm/rmi_smc.h | 9 ++++--
drivers/virt/coco/arm-cca-host/arm-cca.c | 8 ++++-
drivers/virt/coco/arm-cca-host/rmm-da.c | 39 ++++++++++++++++++++++++
drivers/virt/coco/arm-cca-host/rmm-da.h | 2 ++
5 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/include/asm/rmi_cmds.h b/arch/arm64/include/asm/rmi_cmds.h
index eb4f67eb6b01..fcf6b319e953 100644
--- a/arch/arm64/include/asm/rmi_cmds.h
+++ b/arch/arm64/include/asm/rmi_cmds.h
@@ -629,5 +629,13 @@ static inline unsigned long rmi_vdev_destroy(unsigned long rd,
return res.a0;
}

+static inline unsigned long rmi_vdev_complete(unsigned long rec_phys, unsigned long vdev_phys)
+{
+ struct arm_smccc_res res;
+
+ arm_smccc_1_1_invoke(SMC_RMI_VDEV_COMPLETE, rec_phys, vdev_phys, &res);
+
+ return res.a0;
+}

#endif /* __ASM_RMI_CMDS_H */
diff --git a/arch/arm64/include/asm/rmi_smc.h b/arch/arm64/include/asm/rmi_smc.h
index a5ef68b62bc0..6b23afa070d1 100644
--- a/arch/arm64/include/asm/rmi_smc.h
+++ b/arch/arm64/include/asm/rmi_smc.h
@@ -57,7 +57,8 @@
#define SMC_RMI_VDEV_CREATE SMC_RMI_CALL(0x0187)
#define SMC_RMI_VDEV_DESTROY SMC_RMI_CALL(0x0188)
#define SMC_RMI_VDEV_GET_STATE SMC_RMI_CALL(0x0189)
-#define SMC_RMI_VDEV_STOP SMC_RMI_CALL(0x018A)
+#define SMC_RMI_VDEV_STOP SMC_RMI_CALL(0x018a)
+#define SMC_RMI_VDEV_COMPLETE SMC_RMI_CALL(0x018e)

#define RMI_ABI_MAJOR_VERSION 1
#define RMI_ABI_MINOR_VERSION 0
@@ -262,7 +263,11 @@ struct rec_exit {
struct {
u64 ripas_base;
u64 ripas_top;
- u64 ripas_value;
+ u8 ripas_value;
+ u8 padding8[15];
+ u64 s2ap_base;
+ u64 s2ap_top;
+ u64 vdev_id;
};
u8 padding5[0x100];
};
diff --git a/drivers/virt/coco/arm-cca-host/arm-cca.c b/drivers/virt/coco/arm-cca-host/arm-cca.c
index 3792d7b5cb99..837bd10ccd47 100644
--- a/drivers/virt/coco/arm-cca-host/arm-cca.c
+++ b/drivers/virt/coco/arm-cca-host/arm-cca.c
@@ -275,13 +275,19 @@ static void cca_tsm_remove(void *tsm_core)

static int cca_tsm_probe(struct platform_device *pdev)
{
+ int rc;
struct tsm_core_dev *tsm_core;

tsm_core = tsm_register(&pdev->dev, NULL, &cca_pci_ops);
if (IS_ERR(tsm_core))
return PTR_ERR(tsm_core);

- return devm_add_action_or_reset(&pdev->dev, cca_tsm_remove, tsm_core);
+ rc = devm_add_action_or_reset(&pdev->dev, cca_tsm_remove, tsm_core);
+ if (rc)
+ return rc;
+
+ rme_register_exit_handlers();
+ return 0;
}

static const struct platform_device_id arm_cca_host_id_table[] = {
diff --git a/drivers/virt/coco/arm-cca-host/rmm-da.c b/drivers/virt/coco/arm-cca-host/rmm-da.c
index 53072610fa67..d4f1da590b90 100644
--- a/drivers/virt/coco/arm-cca-host/rmm-da.c
+++ b/drivers/virt/coco/arm-cca-host/rmm-da.c
@@ -660,3 +660,42 @@ void rme_unbind_vdev(struct realm *realm, struct pci_dev *pdev, struct pci_dev *
return;
}
}
+
+static struct pci_tsm *find_pci_tsm_from_vdev_id(unsigned long vdev_id)
+{
+ struct pci_dev *pdev = NULL;
+ struct cca_host_tdi *host_tdi;
+
+ for_each_pci_dev(pdev) {
+ host_tdi = to_cca_host_tdi(pdev);
+ if (!host_tdi)
+ continue;
+ if (host_tdi->vdev_id == vdev_id)
+ return pdev->tsm;
+ }
+ return NULL;
+}
+
+static int rme_exit_vdev_req_handler(struct realm_rec *rec)
+{
+ struct cca_host_tdi *host_tdi = NULL;
+ unsigned long vdev_id = rec->run->exit.vdev_id;
+ struct pci_tsm *tsm = find_pci_tsm_from_vdev_id(vdev_id);
+ phys_addr_t rec_phys = virt_to_phys(rec->rec_page);
+
+ if (tsm)
+ host_tdi = to_cca_host_tdi(tsm->pdev);
+
+ if (host_tdi)
+ rmi_vdev_complete(rec_phys, virt_to_phys(host_tdi->rmm_vdev));
+ /*
+ * Return back to the guest without calling vdev complete.
+ * The Realm will treat that as an error.
+ */
+ return 1;
+}
+
+void rme_register_exit_handlers(void)
+{
+ realm_exit_vdev_req_handler = rme_exit_vdev_req_handler;
+}
diff --git a/drivers/virt/coco/arm-cca-host/rmm-da.h b/drivers/virt/coco/arm-cca-host/rmm-da.h
index 6361f7403f95..7f51b611467b 100644
--- a/drivers/virt/coco/arm-cca-host/rmm-da.h
+++ b/drivers/virt/coco/arm-cca-host/rmm-da.h
@@ -48,6 +48,7 @@ struct cca_host_dsc_pf0 {
struct cca_host_tdi {
struct pci_tdi tdi;
void *rmm_vdev;
+ unsigned long vdev_id;
};

#define PDEV_COMMUNICATE 0x1
@@ -95,4 +96,5 @@ void *rme_create_vdev(struct realm *realm, struct pci_dev *pdev,
struct pci_dev *pf0_dev, u32 guest_rid);
void rme_unbind_vdev(struct realm *realm, struct pci_dev *pdev,
struct pci_dev *pf0_dev);
+void rme_register_exit_handlers(void);
#endif
--
2.43.0