[PATCH v4 4/6] iommu: Let a driver mark an IOMMU as confidential
From: Nicolin Chen
Date: Tue Dec 01 2026 - 02:34:52 EST
An IOMMU inside a confidential VM needs private memory for its queues and
tables. Later changes also need to identify it when handling TDISP devices.
Add a cc_private marker and a helper that IOMMU drivers use to set it. The
helper also marks the IOMMU device as able to reach private memory through
DMA. Drivers must call it before iommu_device_register().
Make cc_private a bitfield, so that later flags can be packed next to it.
But keep ready as a normal bool. Both READ_ONCE() and WRITE_ONCE() access
the ready field, and they need its address. A bitfield has no address.
Reviewed-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
---
include/linux/iommu.h | 10 ++++++++++
drivers/iommu/iommu.c | 20 ++++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index ef5043752a365..523ed588a4740 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -824,6 +824,8 @@ struct iommu_domain_ops {
* @singleton_group: Used internally for drivers that have only one group
* @max_pasids: number of supported PASIDs
* @ready: set once iommu_device_register() has completed successfully
+ * @cc_private: this instance runs inside a confidential VM. Set by the driver
+ * before any dma allocation.
*/
struct iommu_device {
struct list_head list;
@@ -833,6 +835,7 @@ struct iommu_device {
struct iommu_group *singleton_group;
u32 max_pasids;
bool ready;
+ bool cc_private : 1;
};
/**
@@ -892,6 +895,8 @@ struct dev_iommu {
int iommu_device_register(struct iommu_device *iommu,
const struct iommu_ops *ops,
struct device *hwdev);
+void iommu_device_set_cc_private(struct iommu_device *iommu,
+ struct device *hwdev);
void iommu_device_unregister(struct iommu_device *iommu);
int iommu_device_sysfs_add(struct iommu_device *iommu,
struct device *parent,
@@ -1423,6 +1428,11 @@ static inline int iommu_device_register(struct iommu_device *iommu,
return -ENODEV;
}
+static inline void iommu_device_set_cc_private(struct iommu_device *iommu,
+ struct device *hwdev)
+{
+}
+
static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
{
return NULL;
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index b486b8bbd1fcd..5ddb37236da11 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -17,6 +17,7 @@
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/host1x_context_bus.h>
+#include <linux/dma-mapping.h>
#include <linux/iommu.h>
#include <linux/iommufd.h>
#include <linux/idr.h>
@@ -313,6 +314,25 @@ int iommu_device_register(struct iommu_device *iommu,
}
EXPORT_SYMBOL_GPL(iommu_device_register);
+/**
+ * iommu_device_set_cc_private - Mark an IOMMU instance as confidential
+ * @iommu: the IOMMU instance
+ * @hwdev: the struct device of @iommu, whose own DMA reaches private memory
+ *
+ * Declare that @iommu runs inside a confidential VM
+ *
+ * Call this before @iommu makes any DMA allocation of its own, and not merely
+ * before iommu_device_register(). Queues and tables allocated any earlier land
+ * in shared memory that the hypervisor can read.
+ */
+void iommu_device_set_cc_private(struct iommu_device *iommu,
+ struct device *hwdev)
+{
+ iommu->cc_private = true;
+ dma_set_cc_private(hwdev, true);
+}
+EXPORT_SYMBOL_GPL(iommu_device_set_cc_private);
+
void iommu_device_unregister(struct iommu_device *iommu)
{
for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++)
--
2.43.0