Skip to content

Commit

Permalink
iommufd/selftest: Add EVENT_VIRQ test coverage
Browse files Browse the repository at this point in the history
Trigger an IRQ giving an idev ID, to test the loopback whether receiving
or not the vdev_id that was set to the idev by the line above.

Signed-off-by: Nicolin Chen <[email protected]>
Signed-off-by: Matthew R. Ochs <[email protected]>
Acked-by: Kai-Heng Feng <[email protected]>
Acked-by: Koba Ko <[email protected]>
Signed-off-by: Matthew R. Ochs <[email protected]>
  • Loading branch information
nicolinc authored and nvmochs committed Oct 18, 2024
1 parent 670d5ef commit 2678803
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tools/testing/selftests/iommu/iommufd.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ TEST_F(iommufd_ioas, viommu_default)
uint32_t nested_hwpt_id = 0, hwpt_id = 0;
uint32_t dev_id = self->device_id;
uint32_t viommu_id = 0;
uint32_t virq_id;
uint32_t virq_fd;

if (dev_id) {
/* Negative test -- invalid hwpt */
Expand Down Expand Up @@ -595,16 +597,25 @@ TEST_F(iommufd_ioas, viommu_default)
sizeof(data));
test_cmd_mock_domain_replace(self->stdev_id, nested_hwpt_id);

test_cmd_virq_alloc(viommu_id, IOMMU_VIRQ_TYPE_SELFTEST,
&virq_id, &virq_fd);
test_err_virq_alloc(EEXIST, viommu_id, IOMMU_VIRQ_TYPE_SELFTEST,
&virq_id, &virq_fd);

/* Set vdev_id to 0x99, unset it, and set to 0x88 */
test_cmd_viommu_set_vdev_id(viommu_id, dev_id, 0x99);
test_cmd_trigger_virq(dev_id, virq_fd, 0x99);
test_err_viommu_set_vdev_id(EEXIST, viommu_id, dev_id, 0x99);
test_err_viommu_unset_vdev_id(EINVAL, viommu_id, dev_id, 0x88);
test_cmd_viommu_unset_vdev_id(viommu_id, dev_id, 0x99);
test_cmd_viommu_set_vdev_id(viommu_id, dev_id, 0x88);
test_cmd_trigger_virq(dev_id, virq_fd, 0x88);
close(virq_fd);

test_cmd_mock_domain_replace(self->stdev_id, hwpt_id);
test_ioctl_destroy(nested_hwpt_id);
test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
test_ioctl_destroy(virq_id);
test_ioctl_destroy(viommu_id);
test_ioctl_destroy(hwpt_id);
} else {
Expand Down
64 changes: 64 additions & 0 deletions tools/testing/selftests/iommu/iommufd_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sys/ioctl.h>
#include <stdint.h>
#include <assert.h>
#include <poll.h>

#include "../kselftest_harness.h"
#include "../../../../drivers/iommu/iommufd/iommufd_test.h"
Expand Down Expand Up @@ -888,3 +889,66 @@ static int _test_cmd_viommu_unset_vdev_id(int fd, __u32 viommu_id,
EXPECT_ERRNO(_errno, \
_test_cmd_viommu_unset_vdev_id(self->fd, viommu_id, \
idev_id, vdev_id))

static int _test_ioctl_virq_alloc(int fd, __u32 viommu_id, __u32 type,
__u32 *virq_id, __u32 *virq_fd)
{
struct iommu_virq_alloc cmd = {
.size = sizeof(cmd),
.type = type,
.viommu_id = viommu_id,
};
int ret;

ret = ioctl(fd, IOMMU_VIRQ_ALLOC, &cmd);
if (ret)
return ret;
if (virq_id)
*virq_id = cmd.out_virq_id;
if (virq_fd)
*virq_fd = cmd.out_virq_fd;
return 0;
}

#define test_cmd_virq_alloc(viommu_id, type, virq_id, virq_fd) \
ASSERT_EQ(0, _test_ioctl_virq_alloc(self->fd, viommu_id, type, \
virq_id, virq_fd))
#define test_err_virq_alloc(_errno, viommu_id, type, virq_id, virq_fd) \
EXPECT_ERRNO(_errno, \
_test_ioctl_virq_alloc(self->fd, viommu_id, type, \
virq_id, virq_fd))

static int _test_cmd_trigger_virq(int fd, __u32 dev_id,
__u32 event_fd, __u32 vdev_id)
{
struct iommu_test_cmd trigger_virq_cmd = {
.size = sizeof(trigger_virq_cmd),
.op = IOMMU_TEST_OP_TRIGGER_VIRQ,
.trigger_virq = {
.dev_id = dev_id,
},
};
struct pollfd pollfd = { .fd = event_fd, .events = POLLIN };
struct iommu_viommu_irq_selftest irq;
ssize_t bytes;
int ret;

ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VIRQ),
&trigger_virq_cmd);
if (ret)
return ret;

ret = poll(&pollfd, 1, 1000);
if (ret < 0)
return ret;

bytes = read(event_fd, &irq, sizeof(irq));
if (bytes <= 0)
return -EIO;

return irq.vdev_id == vdev_id ? 0 : -EINVAL;
}

#define test_cmd_trigger_virq(dev_id, event_fd, vdev_id) \
ASSERT_EQ(0, _test_cmd_trigger_virq(self->fd, dev_id, \
event_fd, vdev_id))

0 comments on commit 2678803

Please sign in to comment.