Devices in the ioport space may have fdt bindings (e.g. 8250, rtc) and
on architectures where a fixed ioport is not defined, it is necessary
to generate fdt nodes for them.

This patch creates a new virtual bus (tree of device_headers) for ioport
devices, allowing architecture code to callback to the emulation code
when generating fdt nodes, as we do for virtio-mmio devices.

Signed-off-by: Will Deacon <will.dea...@arm.com>
---
 tools/kvm/devices.c             |  6 ++++++
 tools/kvm/include/kvm/devices.h |  2 ++
 tools/kvm/include/kvm/ioport.h  |  4 ++++
 tools/kvm/ioport.c              | 34 +++++++++++++++++++++++++++++++---
 4 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/tools/kvm/devices.c b/tools/kvm/devices.c
index 9f1941d..5b627e9 100644
--- a/tools/kvm/devices.c
+++ b/tools/kvm/devices.c
@@ -45,6 +45,12 @@ int device__register(struct device_header *dev)
        return 0;
 }
 
+void device__unregister(struct device_header *dev)
+{
+       struct device_bus *bus = &device_trees[dev->bus_type];
+       rb_erase(&dev->node, &bus->root);
+}
+
 struct device_header *device__find_dev(enum device_bus_type bus_type, u8 
dev_num)
 {
        struct rb_node *node;
diff --git a/tools/kvm/include/kvm/devices.h b/tools/kvm/include/kvm/devices.h
index c5de3de..405f195 100644
--- a/tools/kvm/include/kvm/devices.h
+++ b/tools/kvm/include/kvm/devices.h
@@ -7,6 +7,7 @@
 enum device_bus_type {
        DEVICE_BUS_PCI,
        DEVICE_BUS_MMIO,
+       DEVICE_BUS_IOPORT,
        DEVICE_BUS_MAX,
 };
 
@@ -18,6 +19,7 @@ struct device_header {
 };
 
 int device__register(struct device_header *dev);
+void device__unregister(struct device_header *dev);
 struct device_header *device__find_dev(enum device_bus_type bus_type,
                                       u8 dev_num);
 
diff --git a/tools/kvm/include/kvm/ioport.h b/tools/kvm/include/kvm/ioport.h
index 1556dd3..f639c2e 100644
--- a/tools/kvm/include/kvm/ioport.h
+++ b/tools/kvm/include/kvm/ioport.h
@@ -1,6 +1,7 @@
 #ifndef KVM__IOPORT_H
 #define KVM__IOPORT_H
 
+#include "kvm/devices.h"
 #include "kvm/rbtree-interval.h"
 
 #include <stdbool.h>
@@ -22,11 +23,14 @@ struct ioport {
        struct rb_int_node              node;
        struct ioport_operations        *ops;
        void                            *priv;
+       struct device_header            dev_hdr;
 };
 
 struct ioport_operations {
        bool (*io_in)(struct ioport *ioport, struct kvm *kvm, u16 port, void 
*data, int size);
        bool (*io_out)(struct ioport *ioport, struct kvm *kvm, u16 port, void 
*data, int size);
+       void (*generate_fdt_node)(struct ioport *ioport, void *fdt,
+                                 void (*generate_irq_prop)(void *fdt, u8 irq));
 };
 
 void ioport__setup_arch(struct kvm *kvm);
diff --git a/tools/kvm/ioport.c b/tools/kvm/ioport.c
index a4f1582..be95e49 100644
--- a/tools/kvm/ioport.c
+++ b/tools/kvm/ioport.c
@@ -55,6 +55,28 @@ static void ioport_remove(struct rb_root *root, struct 
ioport *data)
        rb_int_erase(root, &data->node);
 }
 
+#ifdef CONFIG_HAS_LIBFDT
+static void generate_ioport_fdt_node(void *fdt,
+                                    struct device_header *dev_hdr,
+                                    void (*generate_irq_prop)(void *fdt,
+                                                              u8 irq))
+{
+       struct ioport *ioport = container_of(dev_hdr, struct ioport, dev_hdr);
+       struct ioport_operations *ops = ioport->ops;
+
+       if (ops->generate_fdt_node)
+               ops->generate_fdt_node(ioport, fdt, generate_irq_prop);
+}
+#else
+static void generate_ioport_fdt_node(void *fdt,
+                                    struct device_header *dev_hdr,
+                                    void (*generate_irq_prop)(void *fdt,
+                                                              u8 irq))
+{
+       die("Unable to generate device tree nodes without libfdt\n");
+}
+#endif
+
 int ioport__register(struct kvm *kvm, u16 port, struct ioport_operations *ops, 
int count, void *param)
 {
        struct ioport *entry;
@@ -75,9 +97,13 @@ int ioport__register(struct kvm *kvm, u16 port, struct 
ioport_operations *ops, i
                return -ENOMEM;
 
        *entry = (struct ioport) {
-               .node   = RB_INT_INIT(port, port + count),
-               .ops    = ops,
-               .priv   = param,
+               .node           = RB_INT_INIT(port, port + count),
+               .ops            = ops,
+               .priv           = param,
+               .dev_hdr        = (struct device_header) {
+                       .bus_type       = DEVICE_BUS_IOPORT,
+                       .data           = generate_ioport_fdt_node,
+               },
        };
 
        r = ioport_insert(&ioport_tree, entry);
@@ -86,6 +112,8 @@ int ioport__register(struct kvm *kvm, u16 port, struct 
ioport_operations *ops, i
                br_write_unlock(kvm);
                return r;
        }
+
+       device__register(&entry->dev_hdr);
        br_write_unlock(kvm);
 
        return port;
-- 
1.8.0

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to