Skip to content

Commit

Permalink
iommufd: Add IOMMU_DEVICE_GET_INFO
Browse files Browse the repository at this point in the history
Under nested IOMMU translation, userspace owns the stage-1 translation
structure (e.g. the stage-1 page table of Intel VT-d or the context table
of ARM SMMUv3). Such stage-1 translation structures are vendor specific,
and needs to be compatiable with the underlying IOMMU hardware. Hence,
userspace should know the IOMMU hardware capability before creating and
configuring the stage-1 translation structure to kernel.

This adds ioctl: IOMMU_DEVICE_GET_INFO to query the IOMMU hardware
capability for a given device.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
  • Loading branch information
yiliu1765 committed Jan 19, 2023
1 parent 8bd1167 commit 5356ffe
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
77 changes: 77 additions & 0 deletions drivers/iommu/iommufd/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,83 @@ void iommufd_device_unbind(struct iommufd_device *idev)
}
EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD);

static int iommufd_zero_fill_user(u64 ptr, int bytes)
{
int index = 0;

for (; index < bytes; index++) {
if (put_user(0, (uint8_t __user *)(ptr + index)))
return -EFAULT;
}
return 0;
}

int iommufd_device_get_info(struct iommufd_ucmd *ucmd)
{
struct iommu_device_info *cmd = ucmd->cmd;
struct iommufd_object *dev_obj;
struct device *dev;
const struct iommu_ops *ops;
void *data;
unsigned int length, data_len;
int rc;

if (cmd->flags || cmd->__reserved || !cmd->data_len)
return -EOPNOTSUPP;

dev_obj = iommufd_get_object(ucmd->ictx, cmd->dev_id,
IOMMUFD_OBJ_DEVICE);
if (IS_ERR(dev_obj))
return PTR_ERR(dev_obj);

dev = container_of(dev_obj, struct iommufd_device, obj)->dev;

ops = dev_iommu_ops(dev);
if (!ops || !ops->hw_info) {
rc = -EOPNOTSUPP;
goto out_put;
}

if (ops->driver_type == IOMMU_DEVICE_DATA_NONE) {
rc = -EOPNOTSUPP;
goto out_put;
}

data = ops->hw_info(dev, &data_len);
if (IS_ERR(data)) {
rc = PTR_ERR(data);
goto out_put;
}

length = min(cmd->data_len, data_len);
if (copy_to_user(u64_to_user_ptr(cmd->data_ptr), data, length)) {
rc = -EFAULT;
goto out_free_data;
}

/*
* Zero the trailing bytes for userspace if the buffer is bigger
* than the data size kernel actually has.
*/
if (length < cmd->data_len) {
rc = iommufd_zero_fill_user(cmd->data_ptr + length,
cmd->data_len - length);
if (rc)
goto out_free_data;
}

cmd->out_device_type = ops->driver_type;
cmd->data_len = data_len;

rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));

out_free_data:
kfree(data);
out_put:
iommufd_put_object(dev_obj);
return rc;
}

static int iommufd_device_setup_msi(struct iommufd_device *idev,
struct iommufd_hw_pagetable *hwpt,
phys_addr_t sw_msi_start)
Expand Down
3 changes: 3 additions & 0 deletions drivers/iommu/iommufd/iommufd_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <linux/refcount.h>
#include <linux/uaccess.h>

#include "../iommu-priv.h"

struct iommu_domain;
struct iommu_group;
struct iommu_option;
Expand Down Expand Up @@ -256,6 +258,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
void iommufd_hw_pagetable_destroy(struct iommufd_object *obj);

void iommufd_device_destroy(struct iommufd_object *obj);
int iommufd_device_get_info(struct iommufd_ucmd *ucmd);

struct iommufd_access {
struct iommufd_object obj;
Expand Down
3 changes: 3 additions & 0 deletions drivers/iommu/iommufd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ union ucmd_buffer {
struct iommu_ioas_alloc alloc;
struct iommu_ioas_allow_iovas allow_iovas;
struct iommu_ioas_iova_ranges iova_ranges;
struct iommu_device_info info;
struct iommu_ioas_map map;
struct iommu_ioas_unmap unmap;
#ifdef CONFIG_IOMMUFD_TEST
Expand Down Expand Up @@ -286,6 +287,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
src_iova),
IOCTL_OP(IOMMU_IOAS_IOVA_RANGES, iommufd_ioas_iova_ranges,
struct iommu_ioas_iova_ranges, out_iova_alignment),
IOCTL_OP(IOMMU_DEVICE_GET_INFO, iommufd_device_get_info, struct iommu_device_info,
__reserved),
IOCTL_OP(IOMMU_IOAS_MAP, iommufd_ioas_map, struct iommu_ioas_map,
iova),
IOCTL_OP(IOMMU_IOAS_UNMAP, iommufd_ioas_unmap, struct iommu_ioas_unmap,
Expand Down
37 changes: 37 additions & 0 deletions include/uapi/linux/iommufd.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum {
IOMMUFD_CMD_IOAS_UNMAP,
IOMMUFD_CMD_OPTION,
IOMMUFD_CMD_VFIO_IOAS,
IOMMUFD_CMD_DEVICE_GET_INFO,
};

/**
Expand Down Expand Up @@ -370,4 +371,40 @@ struct iommu_device_info_vtd {
__aligned_u64 cap_reg;
__aligned_u64 ecap_reg;
};

/**
* struct iommu_device_info - ioctl(IOMMU_DEVICE_GET_INFO)
* @size: sizeof(struct iommu_device_info)
* @flags: Must be 0
* @dev_id: The device being attached to the IOMMU
* @data_len: Input the type specific data buffer length in bytes
* @data_ptr: Pointer to the type specific structure (e.g.
* struct iommu_device_info_vtd)
* @out_data_type: Output the underlying iommu hardware type, it is one
* of enum iommu_device_data_type.
* @__reserved: Must be 0
*
* Query the hardware iommu capability for given device which has been
* bound to iommufd. @data_len is set to be the size of the buffer to
* type specific data and the data will be filled. Trailing bytes are
* zeroed if the user buffer is larger than the data kernel has.
*
* The type specific data would be used to sync capability between the
* vIOMMU and the hardware IOMMU, also for the availabillity checking of
* iommu hardware feature like dirty page tracking in IOMMU page table.
*
* The @out_device_type will be filled if the ioctl succeeds. It would
* be used in multiple iommufd operations like hw_pagetable allocation,
* iommu cache invalidation.
*/
struct iommu_device_info {
__u32 size;
__u32 flags;
__u32 dev_id;
__u32 data_len;
__aligned_u64 data_ptr;
__u32 out_device_type;
__u32 __reserved;
};
#define IOMMU_DEVICE_GET_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_GET_INFO)
#endif

0 comments on commit 5356ffe

Please sign in to comment.