@@ -47,6 +47,32 @@ int ds_iommu_pasid_free(DualStageIOMMUObject *dsi_obj, uint32_t pasid)
return -ENOENT;
}
+int ds_iommu_bind_stage1_pgtbl(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *data)
+{
+ if (!dsi_obj) {
+ return -ENOENT;
+ }
+
+ if (dsi_obj->ops && dsi_obj->ops->bind_stage1_pgtbl) {
+ return dsi_obj->ops->bind_stage1_pgtbl(dsi_obj, data);
+ }
+ return -ENOENT;
+}
+
+int ds_iommu_unbind_stage1_pgtbl(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *data)
+{
+ if (!dsi_obj) {
+ return -ENOENT;
+ }
+
+ if (dsi_obj->ops && dsi_obj->ops->unbind_stage1_pgtbl) {
+ return dsi_obj->ops->unbind_stage1_pgtbl(dsi_obj, data);
+ }
+ return -ENOENT;
+}
+
void ds_iommu_object_init(DualStageIOMMUObject *dsi_obj,
DualStageIOMMUOps *ops,
DualStageIOMMUInfo *uinfo)
@@ -1219,9 +1219,57 @@ static int vfio_ds_iommu_pasid_free(DualStageIOMMUObject *dsi_obj,
return 0;
}
+static int vfio_ds_iommu_bind_stage1_pgtbl(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *bind_data)
+{
+ VFIOContainer *container = container_of(dsi_obj, VFIOContainer, dsi_obj);
+ struct vfio_iommu_type1_bind *bind;
+ unsigned long argsz;
+ int ret = 0;
+
+ argsz = sizeof(*bind) + sizeof(bind_data->bind_data);
+ bind = g_malloc0(argsz);
+ bind->argsz = argsz;
+ bind->flags = VFIO_IOMMU_BIND_GUEST_PGTBL;
+ memcpy(&bind->data, &bind_data->bind_data, sizeof(bind_data->bind_data));
+
+ if (ioctl(container->fd, VFIO_IOMMU_BIND, bind)) {
+ error_report("%s: pasid (%u) bind failed: %d",
+ __func__, bind_data->pasid, -errno);
+ ret = -errno;
+ }
+ g_free(bind);
+ return ret;
+}
+
+static int vfio_ds_iommu_unbind_stage1_pgtbl(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *bind_data)
+{
+ VFIOContainer *container = container_of(dsi_obj, VFIOContainer, dsi_obj);
+ struct vfio_iommu_type1_bind *bind;
+ unsigned long argsz;
+ int ret = 0;
+
+ argsz = sizeof(*bind) + sizeof(bind_data->bind_data);
+ bind = g_malloc0(argsz);
+ bind->argsz = argsz;
+ bind->flags = VFIO_IOMMU_UNBIND_GUEST_PGTBL;
+ memcpy(&bind->data, &bind_data->bind_data, sizeof(bind_data->bind_data));
+
+ if (ioctl(container->fd, VFIO_IOMMU_BIND, bind)) {
+ error_report("%s: pasid (%u) unbind failed: %d",
+ __func__, bind_data->pasid, -errno);
+ ret = -errno;
+ }
+ g_free(bind);
+ return ret;
+}
+
static struct DualStageIOMMUOps vfio_ds_iommu_ops = {
.pasid_alloc = vfio_ds_iommu_pasid_alloc,
.pasid_free = vfio_ds_iommu_pasid_free,
+ .bind_stage1_pgtbl = vfio_ds_iommu_bind_stage1_pgtbl,
+ .unbind_stage1_pgtbl = vfio_ds_iommu_unbind_stage1_pgtbl,
};
static int vfio_get_iommu_info(VFIOContainer *container,
@@ -31,6 +31,7 @@
typedef struct DualStageIOMMUObject DualStageIOMMUObject;
typedef struct DualStageIOMMUOps DualStageIOMMUOps;
typedef struct DualStageIOMMUInfo DualStageIOMMUInfo;
+typedef struct DualIOMMUStage1BindData DualIOMMUStage1BindData;
struct DualStageIOMMUOps {
/* Allocate pasid from DualStageIOMMU (a.k.a. host IOMMU) */
@@ -41,6 +42,16 @@ struct DualStageIOMMUOps {
/* Reclaim a pasid from DualStageIOMMU (a.k.a. host IOMMU) */
int (*pasid_free)(DualStageIOMMUObject *dsi_obj,
uint32_t pasid);
+ /*
+ * Bind stage-1 page table to a DualStageIOMMU (a.k.a. host
+ * IOMMU which has dual stage DMA translation capability.
+ * @bind_data specifies the bind configurations.
+ */
+ int (*bind_stage1_pgtbl)(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *bind_data);
+ /* Undo a previous bind. @bind_data specifies the unbind info. */
+ int (*unbind_stage1_pgtbl)(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *bind_data);
};
struct DualStageIOMMUInfo {
@@ -55,9 +66,20 @@ struct DualStageIOMMUObject {
DualStageIOMMUInfo uinfo;
};
+struct DualIOMMUStage1BindData {
+ uint32_t pasid;
+ union {
+ struct iommu_gpasid_bind_data gpasid_bind;
+ } bind_data;
+};
+
int ds_iommu_pasid_alloc(DualStageIOMMUObject *dsi_obj, uint32_t min,
uint32_t max, uint32_t *pasid);
int ds_iommu_pasid_free(DualStageIOMMUObject *dsi_obj, uint32_t pasid);
+int ds_iommu_bind_stage1_pgtbl(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *bind_data);
+int ds_iommu_unbind_stage1_pgtbl(DualStageIOMMUObject *dsi_obj,
+ DualIOMMUStage1BindData *bind_data);
void ds_iommu_object_init(DualStageIOMMUObject *dsi_obj,
DualStageIOMMUOps *ops,