At this point the device node has interrupts and regs, which is not enough to probe a driver in the guest, it at least needs a compatible string and other device bindings. To avoid hardcoding those, let the user supply them as a compiled device tree blob: --vfio-platform [,] Every property of the template's root node, and every subnode, is copied into the generated node with fdt__copy_node(). Signed-off-by: Mostafa Saleh --- builtin-run.c | 2 +- include/kvm/vfio.h | 2 ++ vfio/core.c | 16 ++++++++++++++- vfio/platform.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index b09fc2e..a6fd8ec 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -262,7 +262,7 @@ static int loglevel_parser(const struct option *opt, const char *arg, int unset) OPT_CALLBACK('\0', "vfio-pci", NULL, "[domain:]bus:dev.fn", \ "Assign a PCI device to the virtual machine", \ vfio_device_parser, kvm), \ - OPT_CALLBACK('\0', "vfio-platform", NULL, "device", \ + OPT_CALLBACK('\0', "vfio-platform", NULL, "device[,template.dtb]", \ "Assign a platform device to the virtual machine", \ vfio_device_parser, kvm), \ \ diff --git a/include/kvm/vfio.h b/include/kvm/vfio.h index b3034a0..de24ba1 100644 --- a/include/kvm/vfio.h +++ b/include/kvm/vfio.h @@ -121,6 +121,8 @@ struct vfio_device { struct vfio_device_params { char *name; + /* Platform devices: DTB whose root node is copied into the guest node */ + char *dt_template; const char *bus; enum vfio_device_type type; }; diff --git a/vfio/core.c b/vfio/core.c index 6823a95..3ce1ffd 100644 --- a/vfio/core.c +++ b/vfio/core.c @@ -41,16 +41,30 @@ static int vfio_device_pci_parser(const struct option *opt, char *arg, return 0; } -/* @arg is the name of the device under /sys/bus/platform/devices */ +/* + * @arg is the name of the device under /sys/bus/platform/devices, optionally + * followed by ",". + */ static int vfio_device_platform_parser(const struct option *opt, char *arg, struct vfio_device_params *dev) { + char *dt_template = strtok(NULL, ","); + dev->type = VFIO_DEVICE_PLATFORM; dev->bus = "platform"; + dev->dt_template = NULL; dev->name = strdup(arg); if (!dev->name) return -ENOMEM; + if (dt_template) { + dev->dt_template = strdup(dt_template); + if (!dev->dt_template) { + free(dev->name); + return -ENOMEM; + } + } + return 0; } diff --git a/vfio/platform.c b/vfio/platform.c index 22052a6..2d08f73 100644 --- a/vfio/platform.c +++ b/vfio/platform.c @@ -8,7 +8,11 @@ #include #include +#include +#include +#include #include "kvm/fdt.h" +#include "kvm/read-write.h" static u64 vfio_mmio_io_space_next = KVM_VFIO_MMIO_AREA; @@ -29,6 +33,48 @@ static int vfio_mmio_alloc_io_space(u64 size, u64 *addr) } #ifdef CONFIG_HAS_LIBFDT +static void vfio_platform_generate_fdt_from_template(void *fdt, + struct vfio_device *vdev) +{ + int fd; + struct stat st; + void *tmp_fdt; + int root_offset; + + if (!vdev->params->dt_template) + return; + + fd = open(vdev->params->dt_template, O_RDONLY); + if (fd < 0) + die("Failed to open device tree template '%s'", vdev->params->dt_template); + + if (fstat(fd, &st) < 0) + die("Failed to stat device tree template"); + + if (st.st_size < (off_t)sizeof(struct fdt_header)) + die("Invalid device tree blob (DTB) provided in '%s'", + vdev->params->dt_template); + + tmp_fdt = malloc(st.st_size); + if (!tmp_fdt) + die("Failed to allocate memory for device tree template"); + + if (read_in_full(fd, tmp_fdt, st.st_size) != st.st_size) + die("Failed to read device tree template"); + + if (fdt_check_header(tmp_fdt)) + die("Invalid DTB: '%s'", vdev->params->dt_template); + + root_offset = fdt_path_offset(tmp_fdt, "/"); + if (root_offset < 0) + die("Failed to parse device tree template"); + + fdt__copy_node(fdt, tmp_fdt, root_offset); + + free(tmp_fdt); + close(fd); +} + static void generate_interrupts_prop(void *fdt, struct vfio_device *vdev, fdt_irq_fn generate_irq_prop) { @@ -82,6 +128,9 @@ static void generate_mmio_fdt_node(void *fdt, vdev->params->name, vdev->regions[0].guest_phys_addr); _FDT(fdt_begin_node(fdt, node_name)); + /* Extra user supplied binding. */ + vfio_platform_generate_fdt_from_template(fdt, vdev); + _FDT(fdt_property(fdt, "reg", reg_prop, 2 * sizeof(*reg_prop) * num_regions)); generate_interrupts_prop(fdt, vdev, generate_irq_prop); @@ -264,4 +313,5 @@ void vfio_platform_teardown_device(struct kvm *kvm, struct vfio_device *vdev) device__unregister(&vdev->dev_hdr); free(pdev->irqs); + free(vdev->params->dt_template); } -- 2.56.0.307.g5fe7bd680c-goog