Skip to contentSkip to conversation

Virtio toolstack support for I2C and GPIO on Arm

PATCH Superseded Viresh Kumar 4 participants 20 messages Aug 22, 2022 – Sep 8, 2022

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#14 lore raw

PATCH v5 5/6 libxl: Allocate MMIO params for I2c device and update DT

In reply to Viresh Kumar

This patch allocates Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree based on Virtio I2C DT bindings [1].

This also relocates the code responsible to call make_xen_iommu_node() towards the end of the routine, so we create the node only once.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/i2c/i2c-virtio.yaml

---
 tools/libs/light/libxl_arm.c | 57 +++++++++++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 7 deletions(-)
tools/libs/light/libxl_arm.c +50 -7
index 4d0469f8166a..4c1012e56893 100644
@@ -112,6 +112,15 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         }
     }
 
+    for (i = 0; i < d_config->num_i2cs; i++) {
+        libxl_device_i2c *i2c = &d_config->i2cs[i];
+        int rc = alloc_virtio_mmio_params(gc, &i2c->base, &i2c->irq,
+                                          &virtio_mmio_base, &virtio_mmio_irq);
+
+        if (rc)
+            return rc;
+    }
+
     /*
      * Every virtio-mmio device uses one emulated SPI. If Virtio devices are
      * present, make sure that we allocate enough SPIs for them.
@@ -955,6 +964,26 @@ static int make_virtio_mmio_node_simple(libxl__gc *gc, void *fdt, uint64_t base,
     return fdt_end_node(fdt);
 }
 
+static int make_virtio_mmio_node_i2c(libxl__gc *gc, void *fdt, uint64_t base,
+                                     uint32_t irq, uint32_t backend_domid)
+{
+    int res;
+
+    res = make_virtio_mmio_node_common(gc, fdt, base, irq, backend_domid);
+    if (res) return res;
+
+    res = fdt_begin_node(fdt, "i2c");
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "virtio,device22");
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return fdt_end_node(fdt);
+}
+
 static const struct arch_info *get_arch_info(libxl__gc *gc,
                                              const struct xc_dom_image *dom)
 {
@@ -1156,7 +1185,7 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
     size_t fdt_size = 0;
     int pfdt_size = 0;
     libxl_domain_build_info *const info = &d_config->b_info;
-    bool iommu_created;
+    bool iommu_needed;
     unsigned int i;
 
     const libxl_version_info *vers;
@@ -1264,16 +1293,13 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
         if (d_config->num_pcidevs)
             FDT( make_vpci_node(gc, fdt, ainfo, dom) );
 
-        iommu_created = false;
+        iommu_needed = false;
         for (i = 0; i < d_config->num_disks; i++) {
             libxl_device_disk *disk = &d_config->disks[i];
 
             if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
-                if (disk->backend_domid != LIBXL_TOOLSTACK_DOMID &&
-                    !iommu_created) {
-                    FDT( make_xen_iommu_node(gc, fdt) );
-                    iommu_created = true;
-                }
+                if (disk->backend_domid != LIBXL_TOOLSTACK_DOMID)
+                    iommu_needed = true;
 
                 FDT( make_virtio_mmio_node_simple(gc, fdt, disk->base,
                                                   disk->irq,
@@ -1281,6 +1307,23 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
             }
         }
 
+        for (i = 0; i < d_config->num_i2cs; i++) {
+            libxl_device_i2c *i2c = &d_config->i2cs[i];
+
+            if (i2c->backend_domid != LIBXL_TOOLSTACK_DOMID)
+                iommu_needed = true;
+
+            FDT( make_virtio_mmio_node_i2c(gc, fdt, i2c->base, i2c->irq,
+                                           i2c->backend_domid) );
+        }
+
+        /*
+         * Note, this should be only called after creating all virtio-mmio
+         * device nodes
+         */
+        if (iommu_needed)
+            FDT( make_xen_iommu_node(gc, fdt) );
+
         if (pfdt)
             FDT( copy_partial_fdt(gc, fdt, pfdt) );
 
Signature · 1 line
2.31.1.272.g89b43f80a514
#8 lore raw

PATCH v5 2/6 libxl: Add support for Virtio GPIO device

In reply to Viresh Kumar

This patch adds basic support for configuring and assisting virtio-mmio based virtio-gpio backend (emualator) which is intended to run out of Qemu and could be run in any domain.

An example of domain configuration for Virtio Gpio:
gpio = [ "" ]

Please note, this patch is not enough for virtio-gpio to work on Xen (Arm), as for every Virtio device we need to allocate Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree. A subsequent patch will add these missing bits. For the current patch, the default "irq" and "base" are just written to the Xenstore.

---
 tools/golang/xenlight/helpers.gen.go      | 108 ++++++++++-
 tools/golang/xenlight/types.gen.go        |  27 +++
 tools/include/libxl.h                     |  32 +++
 tools/include/libxl_utils.h               |   3 +
 tools/libs/light/Makefile                 |   1 +
 tools/libs/light/libxl_create.c           |  13 ++
 tools/libs/light/libxl_dm.c               |  17 +-
 tools/libs/light/libxl_gpio.c             | 226 ++++++++++++++++++++++
 tools/libs/light/libxl_internal.h         |   1 +
 tools/libs/light/libxl_types.idl          |  24 +++
 tools/libs/light/libxl_types_internal.idl |   1 +
 tools/ocaml/libs/xl/genwrap.py            |   1 +
 tools/ocaml/libs/xl/xenlight_stubs.c      |   1 +
 tools/xl/Makefile                         |   2 +-
 tools/xl/xl.h                             |   3 +
 tools/xl/xl_cmdtable.c                    |  15 ++
 tools/xl/xl_gpio.c                        | 142 ++++++++++++++
 tools/xl/xl_parse.c                       |  80 ++++++++
 tools/xl/xl_parse.h                       |   1 +
 tools/xl/xl_sxp.c                         |   2 +
 20 files changed, 696 insertions(+), 4 deletions(-)
 create mode 100644 tools/libs/light/libxl_gpio.c
 create mode 100644 tools/xl/xl_gpio.c
tools/golang/xenlight/helpers.gen.go +106 -2
index 4c7b60439bf0..2f41ace71c61 100644
@@ -1221,6 +1221,9 @@ x.Usbdevice = C.GoString(tmp.usbdevice)
 if err := x.VkbDevice.fromC(&tmp.vkb_device);err != nil {
 return fmt.Errorf("converting field VkbDevice: %v", err)
 }
+if err := x.GpioDevice.fromC(&tmp.gpio_device);err != nil {
+return fmt.Errorf("converting field GpioDevice: %v", err)
+}
 if err := x.I2cDevice.fromC(&tmp.i2c_device);err != nil {
 return fmt.Errorf("converting field I2cDevice: %v", err)
 }
@@ -1541,6 +1544,9 @@ hvm.usbdevice = C.CString(tmp.Usbdevice)}
 if err := tmp.VkbDevice.toC(&hvm.vkb_device); err != nil {
 return fmt.Errorf("converting field VkbDevice: %v", err)
 }
+if err := tmp.GpioDevice.toC(&hvm.gpio_device); err != nil {
+return fmt.Errorf("converting field GpioDevice: %v", err)
+}
 if err := tmp.I2cDevice.toC(&hvm.i2c_device); err != nil {
 return fmt.Errorf("converting field I2cDevice: %v", err)
 }
@@ -1740,6 +1746,46 @@ xc.multi_touch_num_contacts = C.uint32_t(x.MultiTouchNumContacts)
  return nil
  }
 
+// NewDeviceGpio returns an instance of DeviceGpio initialized with defaults.
+func NewDeviceGpio() (*DeviceGpio, error) {
+var (
+x DeviceGpio
+xc C.libxl_device_gpio)
+
+C.libxl_device_gpio_init(&xc)
+defer C.libxl_device_gpio_dispose(&xc)
+
+if err := x.fromC(&xc); err != nil {
+return nil, err }
+
+return &x, nil}
+
+func (x *DeviceGpio) fromC(xc *C.libxl_device_gpio) error {
+ x.BackendDomid = Domid(xc.backend_domid)
+x.BackendDomname = C.GoString(xc.backend_domname)
+x.Devid = Devid(xc.devid)
+x.BackendType = GpioBackend(xc.backend_type)
+x.Irq = uint32(xc.irq)
+x.Base = uint64(xc.base)
+
+ return nil}
+
+func (x *DeviceGpio) toC(xc *C.libxl_device_gpio) (err error){defer func(){
+if err != nil{
+C.libxl_device_gpio_dispose(xc)}
+}()
+
+xc.backend_domid = C.libxl_domid(x.BackendDomid)
+if x.BackendDomname != "" {
+xc.backend_domname = C.CString(x.BackendDomname)}
+xc.devid = C.libxl_devid(x.Devid)
+xc.backend_type = C.libxl_gpio_backend(x.BackendType)
+xc.irq = C.uint32_t(x.Irq)
+xc.base = C.uint64_t(x.Base)
+
+ return nil
+ }
+
 // NewDeviceI2c returns an instance of DeviceI2c initialized with defaults.
 func NewDeviceI2c() (*DeviceI2c, error) {
 var (
@@ -2913,6 +2959,15 @@ if err := x.Vkbs[i].fromC(&v); err != nil {
 return fmt.Errorf("converting field Vkbs: %v", err) }
 }
 }
+x.Gpios = nil
+if n := int(xc.num_gpios); n > 0 {
+cGpios := (*[1<<28]C.libxl_device_gpio)(unsafe.Pointer(xc.gpios))[:n:n]
+x.Gpios = make([]DeviceGpio, n)
+for i, v := range cGpios {
+if err := x.Gpios[i].fromC(&v); err != nil {
+return fmt.Errorf("converting field Gpios: %v", err) }
+}
+}
 x.I2cs = nil
 if n := int(xc.num_i2cs); n > 0 {
 cI2cs := (*[1<<28]C.libxl_device_i2c)(unsafe.Pointer(xc.i2cs))[:n:n]
@@ -3083,6 +3138,16 @@ return fmt.Errorf("converting field Vkbs: %v", err)
 }
 }
 }
+if numGpios := len(x.Gpios); numGpios > 0 {
+xc.gpios = (*C.libxl_device_gpio)(C.malloc(C.ulong(numGpios)*C.sizeof_libxl_device_gpio))
+xc.num_gpios = C.int(numGpios)
+cGpios := (*[1<<28]C.libxl_device_gpio)(unsafe.Pointer(xc.gpios))[:numGpios:numGpios]
+for i,v := range x.Gpios {
+if err := v.toC(&cGpios[i]); err != nil {
+return fmt.Errorf("converting field Gpios: %v", err)
+}
+}
+}
 if numI2cs := len(x.I2cs); numI2cs > 0 {
 xc.i2cs = (*C.libxl_device_i2c)(C.malloc(C.ulong(numI2cs)*C.sizeof_libxl_device_i2c))
 xc.num_i2cs = C.int(numI2cs)
@@ -3798,6 +3863,47 @@ xc.rref = C.int(x.Rref)
  return nil
  }
 
+// NewGpioinfo returns an instance of Gpioinfo initialized with defaults.
+func NewGpioinfo() (*Gpioinfo, error) {
+var (
+x Gpioinfo
+xc C.libxl_gpioinfo)
+
+C.libxl_gpioinfo_init(&xc)
+defer C.libxl_gpioinfo_dispose(&xc)
+
+if err := x.fromC(&xc); err != nil {
+return nil, err }
+
+return &x, nil}
+
+func (x *Gpioinfo) fromC(xc *C.libxl_gpioinfo) error {
+ x.Backend = C.GoString(xc.backend)
+x.BackendId = uint32(xc.backend_id)
+x.Frontend = C.GoString(xc.frontend)
+x.FrontendId = uint32(xc.frontend_id)
+x.Devid = Devid(xc.devid)
+x.State = int(xc.state)
+
+ return nil}
+
+func (x *Gpioinfo) toC(xc *C.libxl_gpioinfo) (err error){defer func(){
+if err != nil{
+C.libxl_gpioinfo_dispose(xc)}
+}()
+
+if x.Backend != "" {
+xc.backend = C.CString(x.Backend)}
+xc.backend_id = C.uint32_t(x.BackendId)
+if x.Frontend != "" {
+xc.frontend = C.CString(x.Frontend)}
+xc.frontend_id = C.uint32_t(x.FrontendId)
+xc.devid = C.libxl_devid(x.Devid)
+xc.state = C.int(x.State)
+
+ return nil
+ }
+
 // NewI2cinfo returns an instance of I2cinfo initialized with defaults.
 func NewI2cinfo() (*I2cinfo, error) {
 var (
@@ -3819,8 +3925,6 @@ x.Frontend = C.GoString(xc.frontend)
 x.FrontendId = uint32(xc.frontend_id)
 x.Devid = Devid(xc.devid)
 x.State = int(xc.state)
-x.Evtch = int(xc.evtch)
-x.Rref = int(xc.rref)
 
  return nil}
 
tools/golang/xenlight/types.gen.go +27 -0
index f8d7e84d6180..5a4656035cd1 100644
@@ -255,6 +255,11 @@ VkbBackendQemu VkbBackend = 1
 VkbBackendLinux VkbBackend = 2
 )
 
+type GpioBackend int
+const(
+GpioBackendVirtio GpioBackend = 0
+)
+
 type I2cBackend int
 const(
 I2cBackendVirtio I2cBackend = 0
@@ -589,6 +594,7 @@ Usb Defbool
 Usbversion int
 Usbdevice string
 VkbDevice Defbool
+GpioDevice Defbool
 I2cDevice Defbool
 Soundhw string
 XenPlatformPci Defbool
@@ -652,6 +658,15 @@ MultiTouchHeight uint32
 MultiTouchNumContacts uint32
 }
 
+type DeviceGpio struct {
+BackendDomid Domid
+BackendDomname string
+Devid Devid
+BackendType GpioBackend
+Irq uint32
+Base uint64
+}
+
 type DeviceI2c struct {
 BackendDomid Domid
 BackendDomname string
@@ -950,6 +965,7 @@ Rdms []DeviceRdm
 Dtdevs []DeviceDtdev
 Vfbs []DeviceVfb
 Vkbs []DeviceVkb
+Gpios []DeviceGpio
 I2cs []DeviceI2c
 Vtpms []DeviceVtpm
 P9S []DeviceP9
@@ -1107,6 +1123,17 @@ Evtch int
 Rref int
 }
 
+type Gpioinfo struct {
+Backend string
+BackendId uint32
+Frontend string
+FrontendId uint32
+Devid Devid
+State int
+Evtch int
+Rref int
+}
+
 type I2cinfo struct {
 Backend string
 BackendId uint32
tools/include/libxl.h +32 -0
index ab18c0b8c794..3bbb6d031372 100644
@@ -829,6 +829,15 @@ typedef struct libxl__ctx libxl_ctx;
  */
 #define LIBXL_HAVE_BUILDINFO_VKB_DEVICE 1
 
+/*
+ * LIBXL_HAVE_BUILDINFO_GPIO_DEVICE
+ *
+ * If this is defined, then the libxl_domain_build_info structure will
+ * contain a boolean hvm.gpio_device which instructs libxl whether to include
+ * a gpio at build time or not.
+ */
+#define LIBXL_HAVE_BUILDINFO_GPIO_DEVICE 1
+
 /*
  * LIBXL_HAVE_BUILDINFO_I2C_DEVICE
  *
@@ -2406,6 +2415,29 @@ int libxl_device_vkb_getinfo(libxl_ctx *ctx, uint32_t domid,
                              libxl_vkbinfo *vkbinfo)
                              LIBXL_EXTERNAL_CALLERS_ONLY;
 
+/* GPIO */
+int libxl_device_gpio_add(libxl_ctx *ctx, uint32_t domid, libxl_device_gpio *gpio,
+                         const libxl_asyncop_how *ao_how)
+                         LIBXL_EXTERNAL_CALLERS_ONLY;
+int libxl_device_gpio_remove(libxl_ctx *ctx, uint32_t domid,
+                            libxl_device_gpio *gpio,
+                            const libxl_asyncop_how *ao_how)
+                            LIBXL_EXTERNAL_CALLERS_ONLY;
+int libxl_device_gpio_destroy(libxl_ctx *ctx, uint32_t domid,
+                             libxl_device_gpio *gpio,
+                             const libxl_asyncop_how *ao_how)
+                            LIBXL_EXTERNAL_CALLERS_ONLY;
+
+libxl_device_gpio *libxl_device_gpio_list(libxl_ctx *ctx,
+                                        uint32_t domid, int *num)
+                                        LIBXL_EXTERNAL_CALLERS_ONLY;
+void libxl_device_gpio_list_free(libxl_device_gpio* list, int num)
+                                LIBXL_EXTERNAL_CALLERS_ONLY;
+int libxl_device_gpio_getinfo(libxl_ctx *ctx, uint32_t domid,
+                             const libxl_device_gpio *gpio,
+                             libxl_gpioinfo *gpioinfo)
+                             LIBXL_EXTERNAL_CALLERS_ONLY;
+
 /* I2C */
 int libxl_device_i2c_add(libxl_ctx *ctx, uint32_t domid, libxl_device_i2c *i2c,
                          const libxl_asyncop_how *ao_how)
tools/include/libxl_utils.h +3 -0
index 72b7796b7e4b..91f5c68172cb 100644
@@ -83,6 +83,9 @@ int libxl_devid_to_device_usbctrl(libxl_ctx *ctx, uint32_t domid,
 int libxl_devid_to_device_vkb(libxl_ctx *ctx, uint32_t domid,
                               int devid, libxl_device_vkb *vkb);
 
+int libxl_devid_to_device_gpio(libxl_ctx *ctx, uint32_t domid,
+                              int devid, libxl_device_gpio *gpio);
+
 int libxl_devid_to_device_i2c(libxl_ctx *ctx, uint32_t domid,
                               int devid, libxl_device_i2c *i2c);
 
tools/libs/light/Makefile +1 -0
index 961bdd33297b..7fae83100829 100644
@@ -113,6 +113,7 @@ OBJS-y += libxl_pvcalls.o
 OBJS-y += libxl_vsnd.o
 OBJS-y += libxl_vkb.o
 OBJS-y += libxl_i2c.o
+OBJS-y += libxl_gpio.o
 OBJS-y += libxl_genid.o
 OBJS-y += _libxl_types.o
 OBJS-y += libxl_flask.o
tools/libs/light/libxl_create.c +13 -0
index 84fe9f80c8fe..98a27ff8c005 100644
@@ -375,6 +375,7 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
         libxl_defbool_setdefault(&b_info->u.hvm.altp2m,             false);
         libxl_defbool_setdefault(&b_info->u.hvm.usb,                false);
         libxl_defbool_setdefault(&b_info->u.hvm.vkb_device,         true);
+        libxl_defbool_setdefault(&b_info->u.hvm.gpio_device,         true);
         libxl_defbool_setdefault(&b_info->u.hvm.i2c_device,         true);
         libxl_defbool_setdefault(&b_info->u.hvm.xen_platform_pci,   true);
 
@@ -1754,6 +1755,7 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         libxl__device_console console;
         libxl__device device;
         libxl_device_vkb vkb;
+        libxl_device_gpio gpio;
         libxl_device_i2c i2c;
 
         init_console_info(gc, &console, 0);
@@ -1767,6 +1769,12 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
             libxl_device_vkb_dispose(&vkb);
         }
 
+        if (libxl_defbool_val(d_config->b_info.u.hvm.gpio_device)) {
+            libxl_device_gpio_init(&gpio);
+            libxl__device_add(gc, domid, &libxl__gpio_devtype, &gpio);
+            libxl_device_gpio_dispose(&gpio);
+        }
+
         if (libxl_defbool_val(d_config->b_info.u.hvm.i2c_device)) {
             libxl_device_i2c_init(&i2c);
             libxl__device_add(gc, domid, &libxl__i2c_devtype, &i2c);
@@ -1805,6 +1813,11 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
                               &d_config->vkbs[i]);
         }
 
+        for (i = 0; i < d_config->num_gpios; i++) {
+            libxl__device_add(gc, domid, &libxl__gpio_devtype,
+                              &d_config->gpios[i]);
+        }
+
         for (i = 0; i < d_config->num_i2cs; i++) {
             libxl__device_add(gc, domid, &libxl__i2c_devtype,
                               &d_config->i2cs[i]);
tools/libs/light/libxl_dm.c +16 -1
index 362c0596f497..95d5606870c1 100644
@@ -2113,6 +2113,7 @@ static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
                                         const libxl_domain_config *guest_config,
                                         libxl_device_vfb *vfb,
                                         libxl_device_vkb *vkb,
+                                        libxl_device_gpio *gpio,
                                         libxl_device_i2c *i2c)
 {
     const libxl_domain_build_info *b_info = &guest_config->b_info;
@@ -2122,6 +2123,7 @@ static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
 
     libxl_device_vfb_init(vfb);
     libxl_device_vkb_init(vkb);
+    libxl_device_gpio_init(gpio);
     libxl_device_i2c_init(i2c);
 
     vfb->backend_domid = 0;
@@ -2133,6 +2135,9 @@ static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
     vkb->backend_domid = 0;
     vkb->devid = 0;
 
+    gpio->backend_domid = 0;
+    gpio->devid = 0;
+
     i2c->backend_domid = 0;
     i2c->devid = 0;
 
@@ -2298,6 +2303,7 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     int ret;
     libxl_device_vfb *vfb;
     libxl_device_vkb *vkb;
+    libxl_device_gpio *gpio;
     libxl_device_i2c *i2c;
     char **args;
     struct xs_permissions perm[2];
@@ -2371,12 +2377,15 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
         || libxl_defbool_val(guest_config->b_info.u.hvm.sdl.enable)) {
         GCNEW(vfb);
         GCNEW(vkb);
+        GCNEW(gpio);
         GCNEW(i2c);
-        libxl__vfb_and_vkb_from_hvm_guest_config(gc, guest_config, vfb, vkb, i2c);
+        libxl__vfb_and_vkb_from_hvm_guest_config(gc, guest_config, vfb, vkb, gpio, i2c);
         dm_config->vfbs = vfb;
         dm_config->num_vfbs = 1;
         dm_config->vkbs = vkb;
         dm_config->num_vkbs = 1;
+        dm_config->gpios = gpio;
+        dm_config->num_gpios = 1;
         dm_config->i2cs = i2c;
         dm_config->num_i2cs = 1;
     }
@@ -2520,6 +2529,12 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
         if (ret) goto out;
     }
 
+    if (dm_config->num_gpios) {
+        ret = libxl__device_add(gc, dm_domid, &libxl__gpio_devtype,
+                                &dm_config->gpios[0]);
+        if (ret) goto out;
+    }
+
     if (dm_config->num_i2cs) {
         ret = libxl__device_add(gc, dm_domid, &libxl__i2c_devtype,
                                 &dm_config->i2cs[0]);
tools/libs/light/libxl_gpio.c new file +226 -0

226 lines not on this page · raw message

tools/libs/light/libxl_internal.h +1 -0
index a8904cfea427..a64d0cb63179 100644
@@ -4003,6 +4003,7 @@ static inline int *libxl__device_type_get_num(
 
 extern const libxl__device_type libxl__vfb_devtype;
 extern const libxl__device_type libxl__vkb_devtype;
+extern const libxl__device_type libxl__gpio_devtype;
 extern const libxl__device_type libxl__i2c_devtype;
 extern const libxl__device_type libxl__disk_devtype;
 extern const libxl__device_type libxl__nic_devtype;
tools/libs/light/libxl_types.idl +24 -0
index 014a3ea8364c..4c476a325e9d 100644
@@ -278,6 +278,10 @@ libxl_vkb_backend = Enumeration("vkb_backend", [
     (2, "LINUX")
     ])
 
+libxl_gpio_backend = Enumeration("gpio_backend", [
+    (0, "VIRTIO")
+    ])
+
 libxl_i2c_backend = Enumeration("i2c_backend", [
     (0, "VIRTIO")
     ])
@@ -630,6 +634,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
                                        # - "mouse" for PS/2 protocol relative mouse
                                        ("usbdevice",        string),
                                        ("vkb_device",       libxl_defbool),
+                                       ("gpio_device",       libxl_defbool),
                                        ("i2c_device",       libxl_defbool),
                                        ("soundhw",          string),
                                        ("xen_platform_pci", libxl_defbool),
@@ -710,6 +715,15 @@ libxl_device_vkb = Struct("device_vkb", [
     ("multi_touch_num_contacts", uint32)
     ])
 
+libxl_device_gpio = Struct("device_gpio", [
+    ("backend_domid", libxl_domid),
+    ("backend_domname", string),
+    ("devid", libxl_devid),
+    ("backend_type", libxl_gpio_backend),
+    ("irq", uint32),
+    ("base", uint64)
+    ])
+
 libxl_device_i2c = Struct("device_i2c", [
     ("backend_domid", libxl_domid),
     ("backend_domname", string),
@@ -996,6 +1010,7 @@ libxl_domain_config = Struct("domain_config", [
     ("dtdevs", Array(libxl_device_dtdev, "num_dtdevs")),
     ("vfbs", Array(libxl_device_vfb, "num_vfbs")),
     ("vkbs", Array(libxl_device_vkb, "num_vkbs")),
+    ("gpios", Array(libxl_device_gpio, "num_gpios")),
     ("i2cs", Array(libxl_device_i2c, "num_i2cs")),
     ("vtpms", Array(libxl_device_vtpm, "num_vtpms")),
     ("p9s", Array(libxl_device_p9, "num_p9s")),
@@ -1160,6 +1175,15 @@ libxl_vkbinfo = Struct("vkbinfo", [
     ("rref", integer)
     ], dir=DIR_OUT)
 
+libxl_gpioinfo = Struct("gpioinfo", [
+    ("backend", string),
+    ("backend_id", uint32),
+    ("frontend", string),
+    ("frontend_id", uint32),
+    ("devid", libxl_devid),
+    ("state", integer),
+    ], dir=DIR_OUT)
+
 libxl_i2cinfo = Struct("i2cinfo", [
     ("backend", string),
     ("backend_id", uint32),
tools/libs/light/libxl_types_internal.idl +1 -0
index b1a94a963dfe..b57469b010ce 100644
@@ -34,6 +34,7 @@ libxl__device_kind = Enumeration("device_kind", [
     (16, "VINPUT"),
     (17, "VIRTIO_DISK"),
     (18, "I2C"),
+    (19, "GPIO"),
     ])
 
 libxl__console_backend = Enumeration("console_backend", [
tools/ocaml/libs/xl/genwrap.py +1 -0
index a9db0b97d80f..ffab4b362d2a 100644
@@ -36,6 +36,7 @@ DEVICE_LIST =      [ ("list",           ["ctx", "domid", "t list"]),
 functions = { # ( name , [type1,type2,....] )
     "device_vfb":     DEVICE_FUNCTIONS,
     "device_vkb":     DEVICE_FUNCTIONS,
+    "device_gpio":     DEVICE_FUNCTIONS,
     "device_i2c":     DEVICE_FUNCTIONS,
     "device_disk":    DEVICE_FUNCTIONS + DEVICE_LIST +
                       [ ("insert",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
index cdf473f4ed57..0114cc22a1b6 100644
@@ -707,6 +707,7 @@ DEVICE_ADDREMOVE(disk)
 DEVICE_ADDREMOVE(nic)
 DEVICE_ADDREMOVE(vfb)
 DEVICE_ADDREMOVE(vkb)
+DEVICE_ADDREMOVE(gpio)
 DEVICE_ADDREMOVE(i2c)
 DEVICE_ADDREMOVE(pci)
 _DEVICE_ADDREMOVE(disk, cdrom, insert)
tools/xl/Makefile +1 -1
index 06801962f11e..34ff203bfd86 100644
@@ -23,7 +23,7 @@ XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
 XL_OBJS += xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o
 XL_OBJS += xl_info.o xl_console.o xl_misc.o
 XL_OBJS += xl_vmcontrol.o xl_saverestore.o xl_migrate.o
-XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o xl_i2c.o
+XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o xl_gpio.o xl_i2c.o
 
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxentoollog)
 $(XL_OBJS): CFLAGS += $(CFLAGS_XL)
tools/xl/xl.h +3 -0
index 28618326149e..dfa557077631 100644
@@ -176,6 +176,9 @@ int main_vsnddetach(int argc, char **argv);
 int main_vkbattach(int argc, char **argv);
 int main_vkblist(int argc, char **argv);
 int main_vkbdetach(int argc, char **argv);
+int main_gpioattach(int argc, char **argv);
+int main_gpiolist(int argc, char **argv);
+int main_gpiodetach(int argc, char **argv);
 int main_i2cattach(int argc, char **argv);
 int main_i2clist(int argc, char **argv);
 int main_i2cdetach(int argc, char **argv);
tools/xl/xl_cmdtable.c +15 -0
index c409ab1578d4..0c0d32b2e25b 100644
@@ -406,6 +406,21 @@ const struct cmd_spec cmd_table[] = {
       "Destroy a domain's virtual keyboard device",
       "<Domain> <DevId>",
     },
+    { "gpio-attach",
+      &main_gpioattach, 1, 1,
+      "Create a new virtual gpio device",
+      "<Domain> <gpio-spec-component(s)>...",
+    },
+    { "gpio-list",
+      &main_gpiolist, 0, 0,
+      "List virtual gpio devices for a domain",
+      "<Domain(s)>",
+    },
+    { "gpio-detach",
+      &main_gpiodetach, 0, 1,
+      "Destroy a domain's virtual gpio device",
+      "<Domain> <DevId>",
+    },
     { "i2c-attach",
       &main_i2cattach, 1, 1,
       "Create a new virtual i2c device",
tools/xl/xl_gpio.c new file +142 -0

142 lines not on this page · raw message

tools/xl/xl_parse.c +80 -0

131 lines not on this page · raw message

tools/xl/xl_parse.h +1 -0
index 4b972d525199..6e041abe5d50 100644
@@ -36,6 +36,7 @@ int parse_nic_config(libxl_device_nic *nic, XLU_Config **config, char *token);
 int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token);
 int parse_vsnd_item(libxl_device_vsnd *vsnd, const char *spec);
 int parse_vkb_config(libxl_device_vkb *vkb, char *token);
+int parse_gpio_config(libxl_device_gpio *gpio, char *token);
 int parse_i2c_config(libxl_device_i2c *i2c, char *token);
 
 int match_option_size(const char *prefix, size_t len,
tools/xl/xl_sxp.c +2 -0
index a44c765aa515..632e1f338d09 100644
@@ -140,6 +140,8 @@ void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)
         fprintf(fh, "\t\t\t(usbdevice %s)\n", b_info->u.hvm.usbdevice);
         fprintf(fh, "\t\t\t(vkb_device %s)\n",
                libxl_defbool_to_string(b_info->u.hvm.vkb_device));
+        fprintf(fh, "\t\t\t(gpio_device %s)\n",
+               libxl_defbool_to_string(b_info->u.hvm.gpio_device));
         fprintf(fh, "\t\t\t(i2c_device %s)\n",
                libxl_defbool_to_string(b_info->u.hvm.i2c_device));
         fprintf(fh, "\t\t)\n");
Signature · 1 line
2.31.1.272.g89b43f80a514
#10 lore raw

PATCH v5 3/6 libxl: arm: Create alloc_virtio_mmio_params()

In reply to Viresh Kumar

Create a separate routine to allocate base and irq for a device as the same code will be required for each device type.

---
 tools/libs/light/libxl_arm.c | 46 +++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 14 deletions(-)
tools/libs/light/libxl_arm.c +32 -14
index 1a3ac1646e94..0e3051bac5e4 100644
@@ -48,6 +48,24 @@ static uint32_t alloc_virtio_mmio_irq(libxl__gc *gc, uint32_t *virtio_mmio_irq)
     return irq;
 }
 
+static int alloc_virtio_mmio_params(libxl__gc *gc, uint64_t *base,
+                                    uint32_t *irq, uint64_t *virtio_mmio_base,
+                                    uint32_t *virtio_mmio_irq)
+{
+    *base = alloc_virtio_mmio_base(gc, virtio_mmio_base);
+    if (!*base)
+        return ERROR_FAIL;
+
+    *irq = alloc_virtio_mmio_irq(gc, virtio_mmio_irq);
+    if (!*irq)
+        return ERROR_FAIL;
+
+    LOG(DEBUG, "Allocate Virtio MMIO params: IRQ %u BASE 0x%"PRIx64, *irq,
+        *base);
+
+    return 0;
+}
+
 static const char *gicv_to_string(libxl_gic_version gic_version)
 {
     switch (gic_version) {
@@ -85,20 +103,12 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         libxl_device_disk *disk = &d_config->disks[i];
 
         if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
-            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
-            if (!disk->base)
-                return ERROR_FAIL;
-
-            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
-            if (!disk->irq)
-                return ERROR_FAIL;
+            int rc = alloc_virtio_mmio_params(gc, &disk->base, &disk->irq,
+                                              &virtio_mmio_base,
+                                              &virtio_mmio_irq);
 
-            if (virtio_irq < disk->irq)
-                virtio_irq = disk->irq;
-            virtio_enabled = true;
-
-            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
-                disk->vdev, disk->irq, disk->base);
+            if (rc)
+                return rc;
         }
     }
 
@@ -107,8 +117,16 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
      * present, make sure that we allocate enough SPIs for them.
      * The resulting "nr_spis" needs to cover the highest possible SPI.
      */
-    if (virtio_enabled)
+    if (virtio_mmio_irq != GUEST_VIRTIO_MMIO_SPI_FIRST) {
+        virtio_enabled = true;
+
+        /*
+         * Assumes that "virtio_mmio_irq" is the highest allocated irq, which is
+         * updated from alloc_virtio_mmio_irq() currently.
+         */
+        virtio_irq = virtio_mmio_irq - 1;
         nr_spis = max(nr_spis, virtio_irq - 32 + 1);
+    }
 
     for (i = 0; i < d_config->b_info.num_irqs; i++) {
         uint32_t irq = d_config->b_info.irqs[i];
Signature · 1 line
2.31.1.272.g89b43f80a514
#12 lore raw

PATCH v5 4/6 libxl: arm: Split make_virtio_mmio_node()

In reply to Viresh Kumar

make_virtio_mmio_node() creates the DT node for simple MMIO devices currently, i.e. the ones that don't require any additional properties.

In order to allow using it for other complex device types, split the functionality into two, one where the fdt node isn't closed and the other one to create a simple DT node.

---
 tools/libs/light/libxl_arm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
tools/libs/light/libxl_arm.c +16 -7
index 0e3051bac5e4..4d0469f8166a 100644
@@ -906,9 +906,9 @@ static int make_xen_iommu_node(libxl__gc *gc, void *fdt)
     return 0;
 }
 
-static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
-                                 uint64_t base, uint32_t irq,
-                                 uint32_t backend_domid)
+/* The caller is responsible to complete / close the fdt node */
+static int make_virtio_mmio_node_common(libxl__gc *gc, void *fdt, uint64_t base,
+                                        uint32_t irq, uint32_t backend_domid)
 {
     int res;
     gic_interrupt intr;
@@ -941,10 +941,18 @@ static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
         if (res) return res;
     }
 
-    res = fdt_end_node(fdt);
+    return res;
+}
+
+static int make_virtio_mmio_node_simple(libxl__gc *gc, void *fdt, uint64_t base,
+                                        uint32_t irq, uint32_t backend_domid)
+{
+    int res;
+
+    res = make_virtio_mmio_node_common(gc, fdt, base, irq, backend_domid);
     if (res) return res;
 
-    return 0;
+    return fdt_end_node(fdt);
 }
 
 static const struct arch_info *get_arch_info(libxl__gc *gc,
@@ -1267,8 +1275,9 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
                     iommu_created = true;
                 }
 
-                FDT( make_virtio_mmio_node(gc, fdt, disk->base, disk->irq,
-                                           disk->backend_domid) );
+                FDT( make_virtio_mmio_node_simple(gc, fdt, disk->base,
+                                                  disk->irq,
+                                                  disk->backend_domid) );
             }
         }
 
Signature · 1 line
2.31.1.272.g89b43f80a514
#0 lore raw

v5 cover Virtio toolstack support for I2C and GPIO on Arm

Hello,

This patchset adds toolstack support for I2C and GPIO virtio devices. This is inspired from the work done by Oleksandr for the Disk device.

This is developed as part of Linaro's Project Stratos, where we are working towards Hypervisor agnostic Rust based backend [1].

This is based of origin/staging (commit f6cd15188e09 ("amd/msr: implement VIRT_SPEC_CTRL for HVM guests using legacy SSBD")) which already has Oleksandr's patches applied.

V4->V5:
- Fixed indentation at few places.
- Removed/added blank lines.
- Added few comments.
- Added review tags from Oleksandr.
- Rebased over latest staging branch.

V3->V4:
- Update virtio_enabled independently of all devices, so we don't miss setting it to true.

- Add iommu handling for i2c/gpio and move it as part of make_virtio_mmio_node_common(), which gets backend_domid parameter as a result.

V2->V3:
- Rebased over latest tree and made changes according to changes in Oleksandr's patches from sometime back.
- Minor cleanups.

V1->V2:
- Patches 3/6 and 4/6 are new.
- Patches 5/6 and 6/6 updated based on the above two patches.
- Added link to the bindings for I2C and GPIO.
- Rebased over latest master branch.

Thanks.

--
Viresh

[1] https://lore.kernel.org/xen-devel/20220414092358.kepxbmnrtycz7mhe@vireshk-i7/

Viresh Kumar (6):
libxl: Add support for Virtio I2C device
libxl: Add support for Virtio GPIO device
libxl: arm: Create alloc_virtio_mmio_params()
libxl: arm: Split make_virtio_mmio_node()
libxl: Allocate MMIO params for I2c device and update DT
libxl: Allocate MMIO params for GPIO device and update DT

 tools/golang/xenlight/helpers.gen.go      | 212 ++++++++++++++++++++
 tools/golang/xenlight/types.gen.go        |  54 ++++++
 tools/include/libxl.h                     |  64 ++++++
 tools/include/libxl_utils.h               |   6 +
 tools/libs/light/Makefile                 |   2 +
 tools/libs/light/libxl_arm.c              | 175 ++++++++++++++---
 tools/libs/light/libxl_create.c           |  26 +++
 tools/libs/light/libxl_dm.c               |  34 +++-
 tools/libs/light/libxl_gpio.c             | 226 ++++++++++++++++++++++
 tools/libs/light/libxl_i2c.c              | 226 ++++++++++++++++++++++
 tools/libs/light/libxl_internal.h         |   2 +
 tools/libs/light/libxl_types.idl          |  48 +++++
 tools/libs/light/libxl_types_internal.idl |   2 +
 tools/ocaml/libs/xl/genwrap.py            |   2 +
 tools/ocaml/libs/xl/xenlight_stubs.c      |   2 +
 tools/xl/Makefile                         |   2 +-
 tools/xl/xl.h                             |   6 +
 tools/xl/xl_cmdtable.c                    |  30 +++
 tools/xl/xl_gpio.c                        | 142 ++++++++++++++
 tools/xl/xl_i2c.c                         | 142 ++++++++++++++
 tools/xl/xl_parse.c                       | 160 +++++++++++++++
 tools/xl/xl_parse.h                       |   2 +
 tools/xl/xl_sxp.c                         |   4 +
 23 files changed, 1539 insertions(+), 30 deletions(-)
 create mode 100644 tools/libs/light/libxl_gpio.c
 create mode 100644 tools/libs/light/libxl_i2c.c
 create mode 100644 tools/xl/xl_gpio.c
 create mode 100644 tools/xl/xl_i2c.c
Signature · 1 line
2.31.1.272.g89b43f80a514
#1 lore raw

PATCH v5 1/6 libxl: Add support for Virtio I2C device

This patch adds basic support for configuring and assisting virtio-mmio based virtio-i2c backend (emualator) which is intended to run out of Qemu and could be run in any domain.

An example of domain configuration for Virtio I2c:
i2c = [ "" ]

Please note, this patch is not enough for virtio-i2c to work on Xen (Arm), as for every Virtio device we need to allocate Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree. A subsequent patch will add these missing bits. For the current patch, the default "irq" and "base" are just written to the Xenstore.

---
 tools/golang/xenlight/helpers.gen.go      | 108 +++++++++++
 tools/golang/xenlight/types.gen.go        |  27 +++
 tools/include/libxl.h                     |  32 +++
 tools/include/libxl_utils.h               |   3 +
 tools/libs/light/Makefile                 |   1 +
 tools/libs/light/libxl_create.c           |  13 ++
 tools/libs/light/libxl_dm.c               |  19 +-
 tools/libs/light/libxl_i2c.c              | 226 ++++++++++++++++++++++
 tools/libs/light/libxl_internal.h         |   1 +
 tools/libs/light/libxl_types.idl          |  24 +++
 tools/libs/light/libxl_types_internal.idl |   1 +
 tools/ocaml/libs/xl/genwrap.py            |   1 +
 tools/ocaml/libs/xl/xenlight_stubs.c      |   1 +
 tools/xl/Makefile                         |   2 +-
 tools/xl/xl.h                             |   3 +
 tools/xl/xl_cmdtable.c                    |  15 ++
 tools/xl/xl_i2c.c                         | 142 ++++++++++++++
 tools/xl/xl_parse.c                       |  80 ++++++++
 tools/xl/xl_parse.h                       |   1 +
 tools/xl/xl_sxp.c                         |   2 +
 20 files changed, 699 insertions(+), 3 deletions(-)
 create mode 100644 tools/libs/light/libxl_i2c.c
 create mode 100644 tools/xl/xl_i2c.c
tools/golang/xenlight/helpers.gen.go +108 -0
index fa3cf2ab7658..4c7b60439bf0 100644
@@ -1221,6 +1221,9 @@ x.Usbdevice = C.GoString(tmp.usbdevice)
 if err := x.VkbDevice.fromC(&tmp.vkb_device);err != nil {
 return fmt.Errorf("converting field VkbDevice: %v", err)
 }
+if err := x.I2cDevice.fromC(&tmp.i2c_device);err != nil {
+return fmt.Errorf("converting field I2cDevice: %v", err)
+}
 x.Soundhw = C.GoString(tmp.soundhw)
 if err := x.XenPlatformPci.fromC(&tmp.xen_platform_pci);err != nil {
 return fmt.Errorf("converting field XenPlatformPci: %v", err)
@@ -1538,6 +1541,9 @@ hvm.usbdevice = C.CString(tmp.Usbdevice)}
 if err := tmp.VkbDevice.toC(&hvm.vkb_device); err != nil {
 return fmt.Errorf("converting field VkbDevice: %v", err)
 }
+if err := tmp.I2cDevice.toC(&hvm.i2c_device); err != nil {
+return fmt.Errorf("converting field I2cDevice: %v", err)
+}
 if tmp.Soundhw != "" {
 hvm.soundhw = C.CString(tmp.Soundhw)}
 if err := tmp.XenPlatformPci.toC(&hvm.xen_platform_pci); err != nil {
@@ -1734,6 +1740,46 @@ xc.multi_touch_num_contacts = C.uint32_t(x.MultiTouchNumContacts)
  return nil
  }
 
+// NewDeviceI2c returns an instance of DeviceI2c initialized with defaults.
+func NewDeviceI2c() (*DeviceI2c, error) {
+var (
+x DeviceI2c
+xc C.libxl_device_i2c)
+
+C.libxl_device_i2c_init(&xc)
+defer C.libxl_device_i2c_dispose(&xc)
+
+if err := x.fromC(&xc); err != nil {
+return nil, err }
+
+return &x, nil}
+
+func (x *DeviceI2c) fromC(xc *C.libxl_device_i2c) error {
+ x.BackendDomid = Domid(xc.backend_domid)
+x.BackendDomname = C.GoString(xc.backend_domname)
+x.Devid = Devid(xc.devid)
+x.BackendType = I2cBackend(xc.backend_type)
+x.Irq = uint32(xc.irq)
+x.Base = uint64(xc.base)
+
+ return nil}
+
+func (x *DeviceI2c) toC(xc *C.libxl_device_i2c) (err error){defer func(){
+if err != nil{
+C.libxl_device_i2c_dispose(xc)}
+}()
+
+xc.backend_domid = C.libxl_domid(x.BackendDomid)
+if x.BackendDomname != "" {
+xc.backend_domname = C.CString(x.BackendDomname)}
+xc.devid = C.libxl_devid(x.Devid)
+xc.backend_type = C.libxl_i2c_backend(x.BackendType)
+xc.irq = C.uint32_t(x.Irq)
+xc.base = C.uint64_t(x.Base)
+
+ return nil
+ }
+
 // NewDeviceDisk returns an instance of DeviceDisk initialized with defaults.
 func NewDeviceDisk() (*DeviceDisk, error) {
 var (
@@ -2867,6 +2913,15 @@ if err := x.Vkbs[i].fromC(&v); err != nil {
 return fmt.Errorf("converting field Vkbs: %v", err) }
 }
 }
+x.I2cs = nil
+if n := int(xc.num_i2cs); n > 0 {
+cI2cs := (*[1<<28]C.libxl_device_i2c)(unsafe.Pointer(xc.i2cs))[:n:n]
+x.I2cs = make([]DeviceI2c, n)
+for i, v := range cI2cs {
+if err := x.I2cs[i].fromC(&v); err != nil {
+return fmt.Errorf("converting field I2cs: %v", err) }
+}
+}
 x.Vtpms = nil
 if n := int(xc.num_vtpms); n > 0 {
 cVtpms := (*[1<<28]C.libxl_device_vtpm)(unsafe.Pointer(xc.vtpms))[:n:n]
@@ -3028,6 +3083,16 @@ return fmt.Errorf("converting field Vkbs: %v", err)
 }
 }
 }
+if numI2cs := len(x.I2cs); numI2cs > 0 {
+xc.i2cs = (*C.libxl_device_i2c)(C.malloc(C.ulong(numI2cs)*C.sizeof_libxl_device_i2c))
+xc.num_i2cs = C.int(numI2cs)
+cI2cs := (*[1<<28]C.libxl_device_i2c)(unsafe.Pointer(xc.i2cs))[:numI2cs:numI2cs]
+for i,v := range x.I2cs {
+if err := v.toC(&cI2cs[i]); err != nil {
+return fmt.Errorf("converting field I2cs: %v", err)
+}
+}
+}
 if numVtpms := len(x.Vtpms); numVtpms > 0 {
 xc.vtpms = (*C.libxl_device_vtpm)(C.malloc(C.ulong(numVtpms)*C.sizeof_libxl_device_vtpm))
 xc.num_vtpms = C.int(numVtpms)
@@ -3733,6 +3798,49 @@ xc.rref = C.int(x.Rref)
  return nil
  }
 
+// NewI2cinfo returns an instance of I2cinfo initialized with defaults.
+func NewI2cinfo() (*I2cinfo, error) {
+var (
+x I2cinfo
+xc C.libxl_i2cinfo)
+
+C.libxl_i2cinfo_init(&xc)
+defer C.libxl_i2cinfo_dispose(&xc)
+
+if err := x.fromC(&xc); err != nil {
+return nil, err }
+
+return &x, nil}
+
+func (x *I2cinfo) fromC(xc *C.libxl_i2cinfo) error {
+ x.Backend = C.GoString(xc.backend)
+x.BackendId = uint32(xc.backend_id)
+x.Frontend = C.GoString(xc.frontend)
+x.FrontendId = uint32(xc.frontend_id)
+x.Devid = Devid(xc.devid)
+x.State = int(xc.state)
+x.Evtch = int(xc.evtch)
+x.Rref = int(xc.rref)
+
+ return nil}
+
+func (x *I2cinfo) toC(xc *C.libxl_i2cinfo) (err error){defer func(){
+if err != nil{
+C.libxl_i2cinfo_dispose(xc)}
+}()
+
+if x.Backend != "" {
+xc.backend = C.CString(x.Backend)}
+xc.backend_id = C.uint32_t(x.BackendId)
+if x.Frontend != "" {
+xc.frontend = C.CString(x.Frontend)}
+xc.frontend_id = C.uint32_t(x.FrontendId)
+xc.devid = C.libxl_devid(x.Devid)
+xc.state = C.int(x.State)
+
+ return nil
+ }
+
 // NewNumainfo returns an instance of Numainfo initialized with defaults.
 func NewNumainfo() (*Numainfo, error) {
 var (
tools/golang/xenlight/types.gen.go +27 -0
index a0be7ada8c63..f8d7e84d6180 100644
@@ -255,6 +255,11 @@ VkbBackendQemu VkbBackend = 1
 VkbBackendLinux VkbBackend = 2
 )
 
+type I2cBackend int
+const(
+I2cBackendVirtio I2cBackend = 0
+)
+
 type Passthrough int
 const(
 PassthroughDefault Passthrough = 0
@@ -584,6 +589,7 @@ Usb Defbool
 Usbversion int
 Usbdevice string
 VkbDevice Defbool
+I2cDevice Defbool
 Soundhw string
 XenPlatformPci Defbool
 UsbdeviceList StringList
@@ -646,6 +652,15 @@ MultiTouchHeight uint32
 MultiTouchNumContacts uint32
 }
 
+type DeviceI2c struct {
+BackendDomid Domid
+BackendDomname string
+Devid Devid
+BackendType I2cBackend
+Irq uint32
+Base uint64
+}
+
 type DeviceDisk struct {
 BackendDomid Domid
 BackendDomname string
@@ -935,6 +950,7 @@ Rdms []DeviceRdm
 Dtdevs []DeviceDtdev
 Vfbs []DeviceVfb
 Vkbs []DeviceVkb
+I2cs []DeviceI2c
 Vtpms []DeviceVtpm
 P9S []DeviceP9
 Pvcallsifs []DevicePvcallsif
@@ -1091,6 +1107,17 @@ Evtch int
 Rref int
 }
 
+type I2cinfo struct {
+Backend string
+BackendId uint32
+Frontend string
+FrontendId uint32
+Devid Devid
+State int
+Evtch int
+Rref int
+}
+
 type Numainfo struct {
 Size uint64
 Free uint64
tools/include/libxl.h +32 -0
index 2321a648a59a..ab18c0b8c794 100644
@@ -829,6 +829,15 @@ typedef struct libxl__ctx libxl_ctx;
  */
 #define LIBXL_HAVE_BUILDINFO_VKB_DEVICE 1
 
+/*
+ * LIBXL_HAVE_BUILDINFO_I2C_DEVICE
+ *
+ * If this is defined, then the libxl_domain_build_info structure will
+ * contain a boolean hvm.i2c_device which instructs libxl whether to include
+ * a i2c at build time or not.
+ */
+#define LIBXL_HAVE_BUILDINFO_I2C_DEVICE 1
+
 /*
  * LIBXL_HAVE_BUILDINFO_USBDEVICE_LIST
  *
@@ -2397,6 +2406,29 @@ int libxl_device_vkb_getinfo(libxl_ctx *ctx, uint32_t domid,
                              libxl_vkbinfo *vkbinfo)
                              LIBXL_EXTERNAL_CALLERS_ONLY;
 
+/* I2C */
+int libxl_device_i2c_add(libxl_ctx *ctx, uint32_t domid, libxl_device_i2c *i2c,
+                         const libxl_asyncop_how *ao_how)
+                         LIBXL_EXTERNAL_CALLERS_ONLY;
+int libxl_device_i2c_remove(libxl_ctx *ctx, uint32_t domid,
+                            libxl_device_i2c *i2c,
+                            const libxl_asyncop_how *ao_how)
+                            LIBXL_EXTERNAL_CALLERS_ONLY;
+int libxl_device_i2c_destroy(libxl_ctx *ctx, uint32_t domid,
+                             libxl_device_i2c *i2c,
+                             const libxl_asyncop_how *ao_how)
+                            LIBXL_EXTERNAL_CALLERS_ONLY;
+
+libxl_device_i2c *libxl_device_i2c_list(libxl_ctx *ctx,
+                                        uint32_t domid, int *num)
+                                        LIBXL_EXTERNAL_CALLERS_ONLY;
+void libxl_device_i2c_list_free(libxl_device_i2c* list, int num)
+                                LIBXL_EXTERNAL_CALLERS_ONLY;
+int libxl_device_i2c_getinfo(libxl_ctx *ctx, uint32_t domid,
+                             const libxl_device_i2c *i2c,
+                             libxl_i2cinfo *i2cinfo)
+                             LIBXL_EXTERNAL_CALLERS_ONLY;
+
 /* Framebuffer */
 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb,
                          const libxl_asyncop_how *ao_how)
tools/include/libxl_utils.h +3 -0
index 46918aea847f..72b7796b7e4b 100644
@@ -83,6 +83,9 @@ int libxl_devid_to_device_usbctrl(libxl_ctx *ctx, uint32_t domid,
 int libxl_devid_to_device_vkb(libxl_ctx *ctx, uint32_t domid,
                               int devid, libxl_device_vkb *vkb);
 
+int libxl_devid_to_device_i2c(libxl_ctx *ctx, uint32_t domid,
+                              int devid, libxl_device_i2c *i2c);
+
 int libxl_devid_to_device_vdispl(libxl_ctx *ctx, uint32_t domid,
                                  int devid, libxl_device_vdispl *vdispl);
 
tools/libs/light/Makefile +1 -0
index 13545654c2fc..961bdd33297b 100644
@@ -112,6 +112,7 @@ OBJS-y += libxl_vdispl.o
 OBJS-y += libxl_pvcalls.o
 OBJS-y += libxl_vsnd.o
 OBJS-y += libxl_vkb.o
+OBJS-y += libxl_i2c.o
 OBJS-y += libxl_genid.o
 OBJS-y += _libxl_types.o
 OBJS-y += libxl_flask.o
tools/libs/light/libxl_create.c +13 -0
index b9dd2deedf13..84fe9f80c8fe 100644
@@ -375,6 +375,7 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
         libxl_defbool_setdefault(&b_info->u.hvm.altp2m,             false);
         libxl_defbool_setdefault(&b_info->u.hvm.usb,                false);
         libxl_defbool_setdefault(&b_info->u.hvm.vkb_device,         true);
+        libxl_defbool_setdefault(&b_info->u.hvm.i2c_device,         true);
         libxl_defbool_setdefault(&b_info->u.hvm.xen_platform_pci,   true);
 
         libxl_defbool_setdefault(&b_info->u.hvm.spice.enable, false);
@@ -1753,6 +1754,7 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         libxl__device_console console;
         libxl__device device;
         libxl_device_vkb vkb;
+        libxl_device_i2c i2c;
 
         init_console_info(gc, &console, 0);
         console.backend_domid = state->console_domid;
@@ -1765,6 +1767,12 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
             libxl_device_vkb_dispose(&vkb);
         }
 
+        if (libxl_defbool_val(d_config->b_info.u.hvm.i2c_device)) {
+            libxl_device_i2c_init(&i2c);
+            libxl__device_add(gc, domid, &libxl__i2c_devtype, &i2c);
+            libxl_device_i2c_dispose(&i2c);
+        }
+
         dcs->sdss.dm.guest_domid = domid;
         if (libxl_defbool_val(d_config->b_info.device_model_stubdomain))
             libxl__spawn_stub_dm(egc, &dcs->sdss);
@@ -1797,6 +1805,11 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
                               &d_config->vkbs[i]);
         }
 
+        for (i = 0; i < d_config->num_i2cs; i++) {
+            libxl__device_add(gc, domid, &libxl__i2c_devtype,
+                              &d_config->i2cs[i]);
+        }
+
         if (d_config->b_info.arch_arm.vuart == LIBXL_VUART_TYPE_SBSA_UART) {
             init_console_info(gc, &vuart, 0);
             vuart.backend_domid = state->console_domid;
tools/libs/light/libxl_dm.c +17 -2
index fc264a3a13a6..362c0596f497 100644
@@ -2112,7 +2112,8 @@ static void libxl__dm_vifs_from_hvm_guest_config(libxl__gc *gc,
 static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
                                         const libxl_domain_config *guest_config,
                                         libxl_device_vfb *vfb,
-                                        libxl_device_vkb *vkb)
+                                        libxl_device_vkb *vkb,
+                                        libxl_device_i2c *i2c)
 {
     const libxl_domain_build_info *b_info = &guest_config->b_info;
 
@@ -2121,6 +2122,7 @@ static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
 
     libxl_device_vfb_init(vfb);
     libxl_device_vkb_init(vkb);
+    libxl_device_i2c_init(i2c);
 
     vfb->backend_domid = 0;
     vfb->devid = 0;
@@ -2131,6 +2133,9 @@ static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
     vkb->backend_domid = 0;
     vkb->devid = 0;
 
+    i2c->backend_domid = 0;
+    i2c->devid = 0;
+
     return 0;
 }
 
@@ -2293,6 +2298,7 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     int ret;
     libxl_device_vfb *vfb;
     libxl_device_vkb *vkb;
+    libxl_device_i2c *i2c;
     char **args;
     struct xs_permissions perm[2];
     xs_transaction_t t;
@@ -2365,11 +2371,14 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
         || libxl_defbool_val(guest_config->b_info.u.hvm.sdl.enable)) {
         GCNEW(vfb);
         GCNEW(vkb);
-        libxl__vfb_and_vkb_from_hvm_guest_config(gc, guest_config, vfb, vkb);
+        GCNEW(i2c);
+        libxl__vfb_and_vkb_from_hvm_guest_config(gc, guest_config, vfb, vkb, i2c);
         dm_config->vfbs = vfb;
         dm_config->num_vfbs = 1;
         dm_config->vkbs = vkb;
         dm_config->num_vkbs = 1;
+        dm_config->i2cs = i2c;
+        dm_config->num_i2cs = 1;
     }
 
     if (guest_config->b_info.stubdomain_kernel &&
@@ -2511,6 +2520,12 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
         if (ret) goto out;
     }
 
+    if (dm_config->num_i2cs) {
+        ret = libxl__device_add(gc, dm_domid, &libxl__i2c_devtype,
+                                &dm_config->i2cs[0]);
+        if (ret) goto out;
+    }
+
     if (guest_config->b_info.u.hvm.serial) {
         num_console++;
     } else if (guest_config->b_info.u.hvm.serial_list) {
tools/libs/light/libxl_i2c.c new file +226 -0

226 lines not on this page · raw message

tools/libs/light/libxl_internal.h +1 -0
index cb9e8b3b8b5a..a8904cfea427 100644
@@ -4003,6 +4003,7 @@ static inline int *libxl__device_type_get_num(
 
 extern const libxl__device_type libxl__vfb_devtype;
 extern const libxl__device_type libxl__vkb_devtype;
+extern const libxl__device_type libxl__i2c_devtype;
 extern const libxl__device_type libxl__disk_devtype;
 extern const libxl__device_type libxl__nic_devtype;
 extern const libxl__device_type libxl__vtpm_devtype;
tools/libs/light/libxl_types.idl +24 -0
index d634f304cda2..014a3ea8364c 100644
@@ -278,6 +278,10 @@ libxl_vkb_backend = Enumeration("vkb_backend", [
     (2, "LINUX")
     ])
 
+libxl_i2c_backend = Enumeration("i2c_backend", [
+    (0, "VIRTIO")
+    ])
+
 libxl_passthrough = Enumeration("passthrough", [
     (0, "default"),
     (1, "disabled"),
@@ -626,6 +630,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
                                        # - "mouse" for PS/2 protocol relative mouse
                                        ("usbdevice",        string),
                                        ("vkb_device",       libxl_defbool),
+                                       ("i2c_device",       libxl_defbool),
                                        ("soundhw",          string),
                                        ("xen_platform_pci", libxl_defbool),
                                        ("usbdevice_list",   libxl_string_list),
@@ -705,6 +710,15 @@ libxl_device_vkb = Struct("device_vkb", [
     ("multi_touch_num_contacts", uint32)
     ])
 
+libxl_device_i2c = Struct("device_i2c", [
+    ("backend_domid", libxl_domid),
+    ("backend_domname", string),
+    ("devid", libxl_devid),
+    ("backend_type", libxl_i2c_backend),
+    ("irq", uint32),
+    ("base", uint64)
+    ])
+
 libxl_device_disk = Struct("device_disk", [
     ("backend_domid", libxl_domid),
     ("backend_domname", string),
@@ -982,6 +996,7 @@ libxl_domain_config = Struct("domain_config", [
     ("dtdevs", Array(libxl_device_dtdev, "num_dtdevs")),
     ("vfbs", Array(libxl_device_vfb, "num_vfbs")),
     ("vkbs", Array(libxl_device_vkb, "num_vkbs")),
+    ("i2cs", Array(libxl_device_i2c, "num_i2cs")),
     ("vtpms", Array(libxl_device_vtpm, "num_vtpms")),
     ("p9s", Array(libxl_device_p9, "num_p9s")),
     ("pvcallsifs", Array(libxl_device_pvcallsif, "num_pvcallsifs")),
@@ -1145,6 +1160,15 @@ libxl_vkbinfo = Struct("vkbinfo", [
     ("rref", integer)
     ], dir=DIR_OUT)
 
+libxl_i2cinfo = Struct("i2cinfo", [
+    ("backend", string),
+    ("backend_id", uint32),
+    ("frontend", string),
+    ("frontend_id", uint32),
+    ("devid", libxl_devid),
+    ("state", integer),
+    ], dir=DIR_OUT)
+
 # NUMA node characteristics: size and free are how much memory it has, and how
 # much of it is free, respectively. dists is an array of distances from this
 # node to each other node.
tools/libs/light/libxl_types_internal.idl +1 -0
index fb0f4f23d7c2..b1a94a963dfe 100644
@@ -33,6 +33,7 @@ libxl__device_kind = Enumeration("device_kind", [
     (15, "VSND"),
     (16, "VINPUT"),
     (17, "VIRTIO_DISK"),
+    (18, "I2C"),
     ])
 
 libxl__console_backend = Enumeration("console_backend", [
tools/ocaml/libs/xl/genwrap.py +1 -0
index 7bf26bdcd831..a9db0b97d80f 100644
@@ -36,6 +36,7 @@ DEVICE_LIST =      [ ("list",           ["ctx", "domid", "t list"]),
 functions = { # ( name , [type1,type2,....] )
     "device_vfb":     DEVICE_FUNCTIONS,
     "device_vkb":     DEVICE_FUNCTIONS,
+    "device_i2c":     DEVICE_FUNCTIONS,
     "device_disk":    DEVICE_FUNCTIONS + DEVICE_LIST +
                       [ ("insert",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
                         ("of_vdev",        ["ctx", "domid", "string", "t"]),
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
index 45b8af61c74a..cdf473f4ed57 100644
@@ -707,6 +707,7 @@ DEVICE_ADDREMOVE(disk)
 DEVICE_ADDREMOVE(nic)
 DEVICE_ADDREMOVE(vfb)
 DEVICE_ADDREMOVE(vkb)
+DEVICE_ADDREMOVE(i2c)
 DEVICE_ADDREMOVE(pci)
 _DEVICE_ADDREMOVE(disk, cdrom, insert)
 
tools/xl/Makefile +1 -1
index b7f439121a3a..06801962f11e 100644
@@ -23,7 +23,7 @@ XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
 XL_OBJS += xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o
 XL_OBJS += xl_info.o xl_console.o xl_misc.o
 XL_OBJS += xl_vmcontrol.o xl_saverestore.o xl_migrate.o
-XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o
+XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o xl_i2c.o
 
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxentoollog)
 $(XL_OBJS): CFLAGS += $(CFLAGS_XL)
tools/xl/xl.h +3 -0
index 7c9aff6ad7a6..28618326149e 100644
@@ -176,6 +176,9 @@ int main_vsnddetach(int argc, char **argv);
 int main_vkbattach(int argc, char **argv);
 int main_vkblist(int argc, char **argv);
 int main_vkbdetach(int argc, char **argv);
+int main_i2cattach(int argc, char **argv);
+int main_i2clist(int argc, char **argv);
+int main_i2cdetach(int argc, char **argv);
 int main_usbctrl_attach(int argc, char **argv);
 int main_usbctrl_detach(int argc, char **argv);
 int main_usbdev_attach(int argc, char **argv);
tools/xl/xl_cmdtable.c +15 -0
index 35182ca19630..c409ab1578d4 100644
@@ -406,6 +406,21 @@ const struct cmd_spec cmd_table[] = {
       "Destroy a domain's virtual keyboard device",
       "<Domain> <DevId>",
     },
+    { "i2c-attach",
+      &main_i2cattach, 1, 1,
+      "Create a new virtual i2c device",
+      "<Domain> <i2c-spec-component(s)>...",
+    },
+    { "i2c-list",
+      &main_i2clist, 0, 0,
+      "List virtual i2c devices for a domain",
+      "<Domain(s)>",
+    },
+    { "i2c-detach",
+      &main_i2cdetach, 0, 1,
+      "Destroy a domain's virtual i2c device",
+      "<Domain> <DevId>",
+    },
     { "vdispl-attach",
       &main_vdisplattach, 1, 1,
       "Create a new virtual display device",
tools/xl/xl_i2c.c new file +142 -0

142 lines not on this page · raw message

tools/xl/xl_parse.c +80 -0

129 lines not on this page · raw message

tools/xl/xl_parse.h +1 -0
index bab2861f8c3e..4b972d525199 100644
@@ -36,6 +36,7 @@ int parse_nic_config(libxl_device_nic *nic, XLU_Config **config, char *token);
 int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token);
 int parse_vsnd_item(libxl_device_vsnd *vsnd, const char *spec);
 int parse_vkb_config(libxl_device_vkb *vkb, char *token);
+int parse_i2c_config(libxl_device_i2c *i2c, char *token);
 
 int match_option_size(const char *prefix, size_t len,
                       char *arg, char **argopt);
tools/xl/xl_sxp.c +2 -0
index 359a0015709e..a44c765aa515 100644
@@ -140,6 +140,8 @@ void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)
         fprintf(fh, "\t\t\t(usbdevice %s)\n", b_info->u.hvm.usbdevice);
         fprintf(fh, "\t\t\t(vkb_device %s)\n",
                libxl_defbool_to_string(b_info->u.hvm.vkb_device));
+        fprintf(fh, "\t\t\t(i2c_device %s)\n",
+               libxl_defbool_to_string(b_info->u.hvm.i2c_device));
         fprintf(fh, "\t\t)\n");
         break;
     case LIBXL_DOMAIN_TYPE_PV:
Signature · 1 line
2.31.1.272.g89b43f80a514
#16 lore raw

PATCH v5 6/6 libxl: Allocate MMIO params for GPIO device and update DT

In reply to Viresh Kumar

This patch allocates Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree based on Virtio GPIO DT bindings [1].

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/gpio/gpio-virtio.yaml

---
 tools/libs/light/libxl_arm.c | 51 ++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
tools/libs/light/libxl_arm.c +51 -0
index 4c1012e56893..86c1e560900f 100644
@@ -121,6 +121,15 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             return rc;
     }
 
+    for (i = 0; i < d_config->num_gpios; i++) {
+        libxl_device_gpio *gpio = &d_config->gpios[i];
+    int rc = alloc_virtio_mmio_params(gc, &gpio->base, &gpio->irq,
+                                      &virtio_mmio_base, &virtio_mmio_irq);
+
+    if (rc)
+        return rc;
+    }
+
     /*
      * Every virtio-mmio device uses one emulated SPI. If Virtio devices are
      * present, make sure that we allocate enough SPIs for them.
@@ -984,6 +993,38 @@ static int make_virtio_mmio_node_i2c(libxl__gc *gc, void *fdt, uint64_t base,
     return fdt_end_node(fdt);
 }
 
+static int make_virtio_mmio_node_gpio(libxl__gc *gc, void *fdt, uint64_t base,
+                                      uint32_t irq, uint32_t backend_domid)
+{
+    int res;
+
+    res = make_virtio_mmio_node_common(gc, fdt, base, irq, backend_domid);
+    if (res) return res;
+
+    res = fdt_begin_node(fdt, "gpio");
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "virtio,device29");
+    if (res) return res;
+
+    res = fdt_property(fdt, "gpio-controller", NULL, 0);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "#gpio-cells", 2);
+    if (res) return res;
+
+    res = fdt_property(fdt, "interrupt-controller", NULL, 0);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "#interrupt-cells", 2);
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return fdt_end_node(fdt);
+}
+
 static const struct arch_info *get_arch_info(libxl__gc *gc,
                                              const struct xc_dom_image *dom)
 {
@@ -1317,6 +1358,16 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
                                            i2c->backend_domid) );
         }
 
+        for (i = 0; i < d_config->num_gpios; i++) {
+            libxl_device_gpio *gpio = &d_config->gpios[i];
+
+            if (gpio->backend_domid != LIBXL_TOOLSTACK_DOMID)
+                iommu_needed = true;
+
+            FDT( make_virtio_mmio_node_gpio(gc, fdt, gpio->base, gpio->irq,
+                                            gpio->backend_domid) );
+        }
+
         /*
          * Note, this should be only called after creating all virtio-mmio
          * device nodes
Signature · 1 line
2.31.1.272.g89b43f80a514
#9 lore raw

Re: [PATCH V5 2/6] libxl: Add support for Virtio GPIO device

In reply to Viresh Kumar
Quoted 7 lines · Viresh Kumar

This patch adds basic support for configuring and assisting virtio-mmio based virtio-gpio backend (emualator) which is intended to run out of Qemu and could be run in any domain.

An example of domain configuration for Virtio Gpio:
gpio = [ "" ]

Quoted 8 lines

Please note, this patch is not enough for virtio-gpio to work on Xen (Arm), as for every Virtio device we need to allocate Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree. A subsequent patch will add these missing bits. For the current patch, the default "irq" and "base" are just written to the Xenstore.

Most comment on the previous patch would apply to this one as well.

Cheers,

Signature · 1 line
Anthony PERARD
#2 lore raw

Re: [PATCH V5 1/6] libxl: Add support for Virtio I2C device

In reply to Viresh Kumar

Hi Viresh,

Quoted 3 lines · Viresh Kumar

This patch adds basic support for configuring and assisting virtio-mmio based virtio-i2c backend (emualator) which is intended to run out of Qemu and could be run in any domain.

"to run out of Qemu" -> this is ambiguous. It can both mean that virtio-i2c is provided by QEMU and also that virtio-i2c is provided by a different piece of software.

Quoted 1 line

An example of domain configuration for Virtio I2c:

I believe a proper spelling is "I2C", not "I2c" which looks weird to me.

Quoted 1 line

i2c = [ "" ]

Is this doing something meaningful (with the whole series applied)?

Quoted 6 lines

Please note, this patch is not enough for virtio-i2c to work on Xen (Arm), as for every Virtio device we need to allocate Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree. A subsequent patch will add these missing bits. For the current patch, the default "irq" and "base" are just written to the Xenstore.

Is having irq/base set in a different patch still useful? While it was probably useful to do this way on the virtio-disk series, it doesn't seems useful anymore.

Quoted 4 lines
---
 tools/golang/xenlight/helpers.gen.go      | 108 +++++++++++
 tools/golang/xenlight/types.gen.go        |  27 +++

Those .gen.go files aren't up-to-date. You could just add a note after a '---' line to tell the committer to regenerate them, or make sure to regenerate them before a new submission.

Quoted 4 lines
 tools/include/libxl.h                     |  32 +++
 tools/include/libxl_utils.h               |   3 +
 tools/libs/light/Makefile                 |   1 +
 tools/libs/light/libxl_create.c           |  13 ++
Quoted 8 lines
 tools/libs/light/libxl_dm.c               |  19 +-
 tools/libs/light/libxl_i2c.c              | 226 ++++++++++++++++++++++
 tools/libs/light/libxl_internal.h         |   1 +
 tools/libs/light/libxl_types.idl          |  24 +++
 tools/libs/light/libxl_types_internal.idl |   1 +
 tools/ocaml/libs/xl/genwrap.py            |   1 +
 tools/ocaml/libs/xl/xenlight_stubs.c      |   1 +
 tools/xl/Makefile                         |   2 +-

Could you take care of the change in xl in a separate patch? Also, we will want documentation, at least changes in the man pages about the new commands and configurations.

Quoted 15 lines
 tools/xl/xl.h                             |   3 +
 tools/xl/xl_cmdtable.c                    |  15 ++
 tools/xl/xl_i2c.c                         | 142 ++++++++++++++
 tools/xl/xl_parse.c                       |  80 ++++++++
 tools/xl/xl_parse.h                       |   1 +
 tools/xl/xl_sxp.c                         |   2 +
 20 files changed, 699 insertions(+), 3 deletions(-)
 create mode 100644 tools/libs/light/libxl_i2c.c
 create mode 100644 tools/xl/xl_i2c.c
tools/include/libxl.h +0 -0
index 2321a648a59a..ab18c0b8c794 100644

tools/include/libxl.h typedef struct libxl__ctx libxl_ctx; · line 837

Quoted 9 lines
tools/include/libxl.h +6 -0
@@ -829,6 +829,15 @@ typedef struct libxl__ctx libxl_ctx;
  */
 #define LIBXL_HAVE_BUILDINFO_VKB_DEVICE 1
 
+/*
+ * LIBXL_HAVE_BUILDINFO_I2C_DEVICE
+ *
+ * If this is defined, then the libxl_domain_build_info structure will
+ * contain a boolean hvm.i2c_device which instructs libxl whether to include
+ * a i2c at build time or not.

"at built time" ? Maybe "at guest creation time" would be a bit more descriptive, or "domain build time".

Could you add that "i2cs" is available in "libxl_domain_config" as well? Or maybe a more generic description and macro name speaking about the availability of I2C.

tools/include/libxl.h typedef struct libxl__ctx libxl_ctx; · line 839

Quoted 2 lines
tools/include/libxl.h +2 -0
+ */
+#define LIBXL_HAVE_BUILDINFO_I2C_DEVICE 1

Also, can this be put at the end of one of the LIBXL_HAVE_* list? Around line 1444, just before typedef **libxl_string_list would probably be better.

Quoted 25 lines
tools/include/libxl.h +1 -0
+
 /*
  * LIBXL_HAVE_BUILDINFO_USBDEVICE_LIST
  *
tools/libs/light/libxl_create.c +2 -0
index b9dd2deedf13..84fe9f80c8fe 100644
@@ -375,6 +375,7 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
         libxl_defbool_setdefault(&b_info->u.hvm.altp2m,             false);
         libxl_defbool_setdefault(&b_info->u.hvm.usb,                false);
         libxl_defbool_setdefault(&b_info->u.hvm.vkb_device,         true);
+        libxl_defbool_setdefault(&b_info->u.hvm.i2c_device,         true);
         libxl_defbool_setdefault(&b_info->u.hvm.xen_platform_pci,   true);
 
         libxl_defbool_setdefault(&b_info->u.hvm.spice.enable, false);
@@ -1753,6 +1754,7 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         libxl__device_console console;
         libxl__device device;
         libxl_device_vkb vkb;
+        libxl_device_i2c i2c;
 
         init_console_info(gc, &console, 0);
         console.backend_domid = state->console_domid;

tools/libs/light/libxl_create.c static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev, · line 1773

Quoted 7 lines
tools/libs/light/libxl_create.c +4 -0
@@ -1765,6 +1767,12 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
             libxl_device_vkb_dispose(&vkb);
         }
 
+        if (libxl_defbool_val(d_config->b_info.u.hvm.i2c_device)) {
+            libxl_device_i2c_init(&i2c);
+            libxl__device_add(gc, domid, &libxl__i2c_devtype, &i2c);
+            libxl_device_i2c_dispose(&i2c);

So, every HVM guest are going to get an I2C device? I don't think that make sense, especially on x86, or even on Arm.

Quoted 22 lines
tools/libs/light/libxl_create.c +7 -0
+        }
+
         dcs->sdss.dm.guest_domid = domid;
         if (libxl_defbool_val(d_config->b_info.device_model_stubdomain))
             libxl__spawn_stub_dm(egc, &dcs->sdss);
@@ -1797,6 +1805,11 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
                               &d_config->vkbs[i]);
         }
 
+        for (i = 0; i < d_config->num_i2cs; i++) {
+            libxl__device_add(gc, domid, &libxl__i2c_devtype,
+                              &d_config->i2cs[i]);
+        }
+
         if (d_config->b_info.arch_arm.vuart == LIBXL_VUART_TYPE_SBSA_UART) {
             init_console_info(gc, &vuart, 0);
             vuart.backend_domid = state->console_domid;
tools/libs/light/libxl_dm.c +0 -0
index fc264a3a13a6..362c0596f497 100644

tools/libs/light/libxl_dm.c static void libxl__dm_vifs_from_hvm_guest_config(libxl__gc *gc, · line 2116

Quoted 6 lines
tools/libs/light/libxl_dm.c +2 -1
@@ -2112,7 +2112,8 @@ static void libxl__dm_vifs_from_hvm_guest_config(libxl__gc *gc,
 static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
                                         const libxl_domain_config *guest_config,
                                         libxl_device_vfb *vfb,
-                                        libxl_device_vkb *vkb)
+                                        libxl_device_vkb *vkb,
+                                        libxl_device_i2c *i2c)

That function is about taking care of adding Xen PV virtual framebuffer and keyboard to a guest. What is the relationship with between that and I2C?

Quoted 30 lines
tools/libs/light/libxl_dm.c +0 -0
 {
     const libxl_domain_build_info *b_info = &guest_config->b_info;
 
tools/libs/light/libxl_i2c.c new file +21 -0
new file mode 100644
index 000000000000..2c46351ce3a4
@@ -0,0 +1,226 @@
+/*
+ * Copyright (C) 2022 Linaro Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#include "libxl_internal.h"
+
+static int libxl__device_i2c_setdefault(libxl__gc *gc, uint32_t domid,
+                                        libxl_device_i2c *i2c, bool hotplug)
+{
+    if (i2c->backend_type != LIBXL_I2C_BACKEND_VIRTIO) {
+        i2c->backend_type = LIBXL_I2C_BACKEND_VIRTIO;

tools/libs/light/libxl_i2c.c line 31

Quoted 10 lines
tools/libs/light/libxl_i2c.c new file +10 -0
+    }
+
+    return libxl__resolve_domid(gc, i2c->backend_domname, &i2c->backend_domid);
+}
+
+static int libxl__device_i2c_dm_needed(void *e, uint32_t domid)
+{
+    libxl_device_i2c *elem = e;
+
+    return elem->backend_type == LIBXL_I2C_BACKEND_VIRTIO;

I'm not sure I understand the result of this function. Is QEMU needed to present VirtIO I2C devices to a guest?

tools/libs/light/libxl_types.idl libxl_vkb_backend = Enumeration("vkb_backend", [ · line 282

Quoted 10 lines
tools/libs/light/libxl_types.idl +2 -0
index d634f304cda2..014a3ea8364c 100644
@@ -278,6 +278,10 @@ libxl_vkb_backend = Enumeration("vkb_backend", [
     (2, "LINUX")
     ])
 
+libxl_i2c_backend = Enumeration("i2c_backend", [
+    (0, "VIRTIO")

Could you start with 1 rather than 0? This will allow libxl to find out if the backend have been set or if an application let libxl choose which backend is more appropriate.

tools/libs/light/libxl_types.idl libxl_domain_build_info = Struct("domain_build_info",[ · line 633

Quoted 10 lines
tools/libs/light/libxl_types.idl +3 -0
+    ])
+
 libxl_passthrough = Enumeration("passthrough", [
     (0, "default"),
     (1, "disabled"),
@@ -626,6 +630,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
                                        # - "mouse" for PS/2 protocol relative mouse
                                        ("usbdevice",        string),
                                        ("vkb_device",       libxl_defbool),
+                                       ("i2c_device",       libxl_defbool),

How is "i2c_device" useful? Would "i2cs" in libxl_domain_config be enough?

tools/xl/Makefile XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o · line 26

Quoted 13 lines
tools/libs/light/libxl_types.idl +0 -0
                                        ("soundhw",          string),
                                        ("xen_platform_pci", libxl_defbool),
                                        ("usbdevice_list",   libxl_string_list),
tools/xl/Makefile +1 -1
index b7f439121a3a..06801962f11e 100644
@@ -23,7 +23,7 @@ XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
 XL_OBJS += xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o
 XL_OBJS += xl_info.o xl_console.o xl_misc.o
 XL_OBJS += xl_vmcontrol.o xl_saverestore.o xl_migrate.o
-XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o
+XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o xl_i2c.o

Could you add xl_i2c.o in a new line instead?

Quoted 8 lines
tools/xl/Makefile +0 -0
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxentoollog)
 $(XL_OBJS): CFLAGS += $(CFLAGS_XL)
tools/xl/xl_cmdtable.c +0 -0
index 35182ca19630..c409ab1578d4 100644

tools/xl/xl_cmdtable.c const struct cmd_spec cmd_table[] = { · line 412

Quoted 7 lines
tools/xl/xl_cmdtable.c +4 -0
@@ -406,6 +406,21 @@ const struct cmd_spec cmd_table[] = {
       "Destroy a domain's virtual keyboard device",
       "<Domain> <DevId>",
     },
+    { "i2c-attach",
+      &main_i2cattach, 1, 1,
+      "Create a new virtual i2c device",
+      "<Domain> <i2c-spec-component(s)>...",

Is i2c-attach going to work? It seems that an i2c device is presented to the guest via the device tree, but I don't think it can be modified on the fly once the guest have booted. Is there i2c device hotplug?

tools/xl/xl_parse.c void parse_config_data(const char *config_source, · line 2660

Quoted 9 lines
tools/xl/xl_parse.c +1 -0
index 1b5381cef033..b91ffba14d40 100644
@@ -2583,6 +2657,7 @@ void parse_config_data(const char *config_source,
         if (vnc_enabled) {
             libxl_device_vfb *vfb;
             libxl_device_vkb *vkb;
+            libxl_device_i2c *i2c;

How is I2C related to VNC?

tools/xl/xl_sxp.c void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh) · line 144

Quoted 13 lines
tools/xl/xl_parse.c +0 -0
             vfb = ARRAY_EXTEND_INIT(d_config->vfbs, d_config->num_vfbs,
                                     libxl_device_vfb_init);
tools/xl/xl_sxp.c +2 -0
index 359a0015709e..a44c765aa515 100644
@@ -140,6 +140,8 @@ void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)
         fprintf(fh, "\t\t\t(usbdevice %s)\n", b_info->u.hvm.usbdevice);
         fprintf(fh, "\t\t\t(vkb_device %s)\n",
                libxl_defbool_to_string(b_info->u.hvm.vkb_device));
+        fprintf(fh, "\t\t\t(i2c_device %s)\n",
+               libxl_defbool_to_string(b_info->u.hvm.i2c_device));

There is a notice on the printf_info_sexp() function:

/* In general you should not add new output to this function since it
* is intended only for legacy use.
*/
void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)

Could you explain why this function is been modified? If not, there's probably no modification needed.

tools/xl/xl_sxp.c void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh) · line 147

Quoted 3 lines
tools/xl/xl_sxp.c +0 -0
         fprintf(fh, "\t\t)\n");
         break;
     case LIBXL_DOMAIN_TYPE_PV:

Thanks,

Signature · 1 line
Anthony PERARD
#15 lore raw

Re: [PATCH V5 5/6] libxl: Allocate MMIO params for I2c device and update DT

In reply to Viresh Kumar
Quoted 2 lines · Viresh Kumar

This also relocates the code responsible to call make_xen_iommu_node() towards the end of the routine, so we create the node only once.

Could you do this part in a separate patch? Then, I believe the rest of the patch could be squashed into the patch that adds i2c support into libxl.

Thanks,

Signature · 1 line
Anthony PERARD
#17 lore raw

Re: [PATCH V5 6/6] libxl: Allocate MMIO params for GPIO device and update DT

In reply to Viresh Kumar
Quoted 17 lines · Viresh Kumar

This patch allocates Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree based on Virtio GPIO DT bindings [1].

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/gpio/gpio-virtio.yaml

---
 tools/libs/light/libxl_arm.c | 51 ++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
tools/libs/light/libxl_arm.c +0 -0
index 4c1012e56893..86c1e560900f 100644

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 126

Quoted 6 lines
tools/libs/light/libxl_arm.c +3 -0
@@ -121,6 +121,15 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             return rc;
     }
 
+    for (i = 0; i < d_config->num_gpios; i++) {
+        libxl_device_gpio *gpio = &d_config->gpios[i];
+    int rc = alloc_virtio_mmio_params(gc, &gpio->base, &gpio->irq,

Indentation seems wrong here.

Also, you could declare "rc" (without an initial value) for the whole function rather than declaring it in each for loop scope.

Then, this patch could be squash into the one that adds GPIO support to libxl.

Thanks,

Signature · 1 line
Anthony PERARD
#11 lore raw

Re: [PATCH V5 3/6] libxl: arm: Create alloc_virtio_mmio_params()

In reply to Viresh Kumar
Quoted 22 lines · Viresh Kumar

Create a separate routine to allocate base and irq for a device as the same code will be required for each device type.

---
 tools/libs/light/libxl_arm.c | 46 +++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 14 deletions(-)
tools/libs/light/libxl_arm.c +0 -4
index 1a3ac1646e94..0e3051bac5e4 100644
@@ -85,20 +103,12 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         libxl_device_disk *disk = &d_config->disks[i];
 
         if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
-            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
-            if (!disk->base)
-                return ERROR_FAIL;
-

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 98

Quoted 10 lines
tools/libs/light/libxl_arm.c +3 -6
-            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
-            if (!disk->irq)
-                return ERROR_FAIL;
+            int rc = alloc_virtio_mmio_params(gc, &disk->base, &disk->irq,
+                                              &virtio_mmio_base,
+                                              &virtio_mmio_irq);
 
-            if (virtio_irq < disk->irq)
-                virtio_irq = disk->irq;
-            virtio_enabled = true;

This change to virtio_irq and virtio_enabled, as well as the chunck below, isn't explained in the patch description.

Quoted 16 lines
tools/libs/light/libxl_arm.c +5 -4
-
-            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
-                disk->vdev, disk->irq, disk->base);
+            if (rc)
+                return rc;
         }
     }
 
@@ -107,8 +117,16 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
      * present, make sure that we allocate enough SPIs for them.
      * The resulting "nr_spis" needs to cover the highest possible SPI.
      */
-    if (virtio_enabled)
+    if (virtio_mmio_irq != GUEST_VIRTIO_MMIO_SPI_FIRST) {
+        virtio_enabled = true;
+

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 132

Quoted 10 lines
tools/libs/light/libxl_arm.c +6 -0
+        /*
+         * Assumes that "virtio_mmio_irq" is the highest allocated irq, which is
+         * updated from alloc_virtio_mmio_irq() currently.
+         */
+        virtio_irq = virtio_mmio_irq - 1;
         nr_spis = max(nr_spis, virtio_irq - 32 + 1);
+    }
 
     for (i = 0; i < d_config->b_info.num_irqs; i++) {
         uint32_t irq = d_config->b_info.irqs[i];

Thanks,

Signature · 1 line
Anthony PERARD
#13 lore raw

Re: [PATCH V5 4/6] libxl: arm: Split make_virtio_mmio_node()

In reply to Viresh Kumar
Quoted 15 lines · Viresh Kumar

make_virtio_mmio_node() creates the DT node for simple MMIO devices currently, i.e. the ones that don't require any additional properties.

In order to allow using it for other complex device types, split the functionality into two, one where the fdt node isn't closed and the other one to create a simple DT node.

---

tools/libs/light/libxl_arm.c +0 -0
index 0e3051bac5e4..4d0469f8166a 100644

tools/libs/light/libxl_arm.c static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config, · line 1278

Quoted 6 lines
tools/libs/light/libxl_arm.c +1 -2
@@ -1267,8 +1275,9 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
                     iommu_created = true;
                 }
 
-                FDT( make_virtio_mmio_node(gc, fdt, disk->base, disk->irq,
-                                           disk->backend_domid) );
+                FDT( make_virtio_mmio_node_simple(gc, fdt, disk->base,

Renaming that function to add the suffix "_simple" doesn't meaning full, could you keep the same name?

tools/libs/light/libxl_arm.c static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config, · line 1283

Quoted 5 lines
tools/libs/light/libxl_arm.c +2 -0
+                                                  disk->irq,
+                                                  disk->backend_domid) );
             }
         }
 

Thanks,

Signature · 1 line
Anthony PERARD
#3 lore raw

Re: [PATCH V5 1/6] libxl: Add support for Virtio I2C device

In reply to Anthony PERARD

Hi Anthony,

Thanks a lot for the in-depth review, it is really helpful.

I don't have much knowledge of the Xen code and wanted this code for I2C and GPIO to be tested on Xen for the work we are doing around hypervisor agnostic backends [1].

I started looking for a simple device's implementation and began by (blindly) copying code from Keyboard device and so much of wasted code in here, which isn't really required.

Quoted 5 lines · Anthony PERARD
Quoted 2 lines · Viresh Kumar

An example of domain configuration for Virtio I2c:
i2c = [ "" ]

Is this doing something meaningful (with the whole series applied)?

If I am not wrong, this is required by parse_i2c_list()'s implementation. Without this I don't get the I2C device populated in the guest.

Is there another way to achieve it ?

I have removed a lot of code now and what I have left is pasted at the end of this email, does this look better now ? And yes, I will update documentation and look again at any formatting issues before sending it.

Thanks.

Signature · 34 lines
viresh

[1] https://lore.kernel.org/all/20220414092358.kepxbmnrtycz7mhe@vireshk-i7/

-------------------------8<-------------------------

Subject: [PATCH] libxl: Add support for Virtio I2C device

This patch adds basic support for configuring and assisting virtio-mmio
based virtio-i2c backend (a hypervisor independent emulator) which could
run in any domain.

An example of domain configuration for Virtio I2C:
i2c = [ "" ]

Also to make this work on Arm, allocate Virtio MMIO params (IRQ and
memory region) and pass them to the backend. Update guest device-tree as
wellto create a DT node for the virtio I2C device.

Signed-off-by: Viresh Kumar <[email protected]>
---
 tools/libs/light/Makefile                 |   1 +
 tools/libs/light/libxl_arm.c              |  39 ++++++++
 tools/libs/light/libxl_create.c           |   5 +
 tools/libs/light/libxl_i2c.c              | 117 ++++++++++++++++++++++
 tools/libs/light/libxl_internal.h         |   1 +
 tools/libs/light/libxl_types.idl          |  22 ++++
 tools/libs/light/libxl_types_internal.idl |   1 +
 tools/ocaml/libs/xl/genwrap.py            |   1 +
 tools/ocaml/libs/xl/xenlight_stubs.c      |   1 +
 tools/xl/xl_parse.c                       |  63 ++++++++++++
 tools/xl/xl_parse.h                       |   1 +
 11 files changed, 252 insertions(+)
 create mode 100644 tools/libs/light/libxl_i2c.c
tools/libs/light/Makefile +1 -0
index 13545654c2fc..961bdd33297b 100644
@@ -112,6 +112,7 @@ OBJS-y += libxl_vdispl.o
 OBJS-y += libxl_pvcalls.o
 OBJS-y += libxl_vsnd.o
 OBJS-y += libxl_vkb.o
+OBJS-y += libxl_i2c.o
 OBJS-y += libxl_genid.o
 OBJS-y += _libxl_types.o
 OBJS-y += libxl_flask.o
tools/libs/light/libxl_arm.c +39 -0
index 2637acafa358..c23ecbcd8528 100644
@@ -113,6 +113,15 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         }
     }
 
+    for (i = 0; i < d_config->num_i2cs; i++) {
+        libxl_device_i2c *i2c = &d_config->i2cs[i];
+        rc = alloc_virtio_mmio_params(gc, &i2c->base, &i2c->irq,
+                                      &virtio_mmio_base, &virtio_mmio_irq);
+
+        if (rc)
+            return rc;
+    }
+
     /*
      * Every virtio-mmio device uses one emulated SPI. If Virtio devices are
      * present, make sure that we allocate enough SPIs for them.
@@ -956,6 +965,26 @@ static int make_virtio_mmio_node(libxl__gc *gc, void *fdt, uint64_t base,
     return fdt_end_node(fdt);
 }
 
+static int make_virtio_mmio_node_i2c(libxl__gc *gc, void *fdt, uint64_t base,
+                                     uint32_t irq, uint32_t backend_domid)
+{
+    int res;
+
+    res = make_virtio_mmio_node_common(gc, fdt, base, irq, backend_domid);
+    if (res) return res;
+
+    res = fdt_begin_node(fdt, "i2c");
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "virtio,device22");
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return fdt_end_node(fdt);
+}
+
 static const struct arch_info *get_arch_info(libxl__gc *gc,
                                              const struct xc_dom_image *dom)
 {
@@ -1278,6 +1307,16 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
             }
         }
 
+        for (i = 0; i < d_config->num_i2cs; i++) {
+            libxl_device_i2c *i2c = &d_config->i2cs[i];
+
+            if (i2c->backend_domid != LIBXL_TOOLSTACK_DOMID)
+                iommu_needed = true;
+
+            FDT( make_virtio_mmio_node_i2c(gc, fdt, i2c->base, i2c->irq,
+                                           i2c->backend_domid) );
+        }
+
         /*
          * Note, this should be only called after creating all virtio-mmio
          * device nodes
tools/libs/light/libxl_create.c +5 -0
index b9dd2deedf13..f18a7f829703 100644
@@ -1797,6 +1797,11 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
                               &d_config->vkbs[i]);
         }
 
+        for (i = 0; i < d_config->num_i2cs; i++) {
+            libxl__device_add(gc, domid, &libxl__i2c_devtype,
+                              &d_config->i2cs[i]);
+        }
+
         if (d_config->b_info.arch_arm.vuart == LIBXL_VUART_TYPE_SBSA_UART) {
             init_console_info(gc, &vuart, 0);
             vuart.backend_domid = state->console_domid;
tools/libs/light/libxl_i2c.c new file +117 -0

117 lines not on this page · raw message

tools/libs/light/libxl_internal.h +1 -0
index cb9e8b3b8b5a..a8904cfea427 100644
@@ -4003,6 +4003,7 @@ static inline int *libxl__device_type_get_num(
 
 extern const libxl__device_type libxl__vfb_devtype;
 extern const libxl__device_type libxl__vkb_devtype;
+extern const libxl__device_type libxl__i2c_devtype;
 extern const libxl__device_type libxl__disk_devtype;
 extern const libxl__device_type libxl__nic_devtype;
 extern const libxl__device_type libxl__vtpm_devtype;
tools/libs/light/libxl_types.idl +22 -0
index d634f304cda2..45e3b2755352 100644
@@ -278,6 +278,10 @@ libxl_vkb_backend = Enumeration("vkb_backend", [
     (2, "LINUX")
     ])
 
+libxl_i2c_backend = Enumeration("i2c_backend", [
+    (1, "VIRTIO")
+    ])
+
 libxl_passthrough = Enumeration("passthrough", [
     (0, "default"),
     (1, "disabled"),
@@ -705,6 +709,14 @@ libxl_device_vkb = Struct("device_vkb", [
     ("multi_touch_num_contacts", uint32)
     ])
 
+libxl_device_i2c = Struct("device_i2c", [
+    ("backend_domid", libxl_domid),
+    ("backend_domname", string),
+    ("devid", libxl_devid),
+    ("irq", uint32),
+    ("base", uint64)
+    ])
+
 libxl_device_disk = Struct("device_disk", [
     ("backend_domid", libxl_domid),
     ("backend_domname", string),
@@ -982,6 +994,7 @@ libxl_domain_config = Struct("domain_config", [
     ("dtdevs", Array(libxl_device_dtdev, "num_dtdevs")),
     ("vfbs", Array(libxl_device_vfb, "num_vfbs")),
     ("vkbs", Array(libxl_device_vkb, "num_vkbs")),
+    ("i2cs", Array(libxl_device_i2c, "num_i2cs")),
     ("vtpms", Array(libxl_device_vtpm, "num_vtpms")),
     ("p9s", Array(libxl_device_p9, "num_p9s")),
     ("pvcallsifs", Array(libxl_device_pvcallsif, "num_pvcallsifs")),
@@ -1145,6 +1158,15 @@ libxl_vkbinfo = Struct("vkbinfo", [
     ("rref", integer)
     ], dir=DIR_OUT)
 
+libxl_i2cinfo = Struct("i2cinfo", [
+    ("backend", string),
+    ("backend_id", uint32),
+    ("frontend", string),
+    ("frontend_id", uint32),
+    ("devid", libxl_devid),
+    ("state", integer),
+    ], dir=DIR_OUT)
+
 # NUMA node characteristics: size and free are how much memory it has, and how
 # much of it is free, respectively. dists is an array of distances from this
 # node to each other node.
tools/libs/light/libxl_types_internal.idl +1 -0
index fb0f4f23d7c2..b1a94a963dfe 100644
@@ -33,6 +33,7 @@ libxl__device_kind = Enumeration("device_kind", [
     (15, "VSND"),
     (16, "VINPUT"),
     (17, "VIRTIO_DISK"),
+    (18, "I2C"),
     ])
 
 libxl__console_backend = Enumeration("console_backend", [
tools/ocaml/libs/xl/genwrap.py +1 -0
index 7bf26bdcd831..a9db0b97d80f 100644
@@ -36,6 +36,7 @@ DEVICE_LIST =      [ ("list",           ["ctx", "domid", "t list"]),
 functions = { # ( name , [type1,type2,....] )
     "device_vfb":     DEVICE_FUNCTIONS,
     "device_vkb":     DEVICE_FUNCTIONS,
+    "device_i2c":     DEVICE_FUNCTIONS,
     "device_disk":    DEVICE_FUNCTIONS + DEVICE_LIST +
                       [ ("insert",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
                         ("of_vdev",        ["ctx", "domid", "string", "t"]),
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
index 45b8af61c74a..cdf473f4ed57 100644
@@ -707,6 +707,7 @@ DEVICE_ADDREMOVE(disk)
 DEVICE_ADDREMOVE(nic)
 DEVICE_ADDREMOVE(vfb)
 DEVICE_ADDREMOVE(vkb)
+DEVICE_ADDREMOVE(i2c)
 DEVICE_ADDREMOVE(pci)
 _DEVICE_ADDREMOVE(disk, cdrom, insert)
 
tools/xl/xl_parse.c +63 -0

83 lines not on this page · raw message

tools/xl/xl_parse.h +1 -0
index bab2861f8c3e..4b972d525199 100644
@@ -36,6 +36,7 @@ int parse_nic_config(libxl_device_nic *nic, XLU_Config **config, char *token);
 int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token);
 int parse_vsnd_item(libxl_device_vsnd *vsnd, const char *spec);
 int parse_vkb_config(libxl_device_vkb *vkb, char *token);
+int parse_i2c_config(libxl_device_i2c *i2c, char *token);
 
 int match_option_size(const char *prefix, size_t len,
                       char *arg, char **argopt);
#4 lore raw

Re: [PATCH V5 1/6] libxl: Add support for Virtio I2C device

Hi Viresh,

Jumping in the conversation.

Quoted 13 lines · Viresh Kumar

I don't have much knowledge of the Xen code and wanted this code for I2C and GPIO to be tested on Xen for the work we are doing around hypervisor agnostic backends [1].

I started looking for a simple device's implementation and began by (blindly) copying code from Keyboard device and so much of wasted code in here, which isn't really required.

Quoted 4 lines · Anthony PERARD
Quoted 2 lines · Viresh Kumar

An example of domain configuration for Virtio I2c:
i2c = [ "" ]

Quoted 6 lines
Quoted 1 line

Is this doing something meaningful (with the whole series applied)?

If I am not wrong, this is required by parse_i2c_list()'s implementation. Without this I don't get the I2C device populated in the guest.

Is there another way to achieve it ?

Looking at this series, you will add ~250 lines (assuming your new patch) for the i2c and then likely the same amount for GPIO.

I am assuming that for every new virtio device (e.g. gps, sound, display...), we would also need to 250 lines of code. I am worry that we will end up to bloat libxl with duplicated code and or for device that are barely used.

I think it would be better to find a generic way to add new virtio device without adding code (very limited) in libxl. The advantage is someone will be able to create a new virtio device with less effort.

The approach I can think of is something along the lines:

virtio = ["type=<compatible>,transport=<transport>,..."]

where the compatible is the one that should be written in the DT and transport is mmio or pci. the [...] refers to specific parameters that would need to be passed to the backend (it is not clear how you provide them today?).

AFAICT, the GPIO one may need some tweaking because it requires specific properties. I think it would be more acceptable as this will be only a few lines (compare to 250 lines today).

Cheers,

Signature · 1 line
Julien Grall
#18 lore raw

Re: [PATCH V5 6/6] libxl: Allocate MMIO params for GPIO device and update DT

In reply to Viresh Kumar

(+ Bertrand)

Hi Viresh,

Quoted 38 lines · Viresh Kumar

This patch allocates Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree based on Virtio GPIO DT bindings [1].

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/gpio/gpio-virtio.yaml

---
  tools/libs/light/libxl_arm.c | 51 ++++++++++++++++++++++++++++++++++++
  1 file changed, 51 insertions(+)
tools/libs/light/libxl_arm.c +11 -0
index 4c1012e56893..86c1e560900f 100644
@@ -121,6 +121,15 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
              return rc;
      }
 
+    for (i = 0; i < d_config->num_gpios; i++) {
+        libxl_device_gpio *gpio = &d_config->gpios[i];
+    int rc = alloc_virtio_mmio_params(gc, &gpio->base, &gpio->irq,
+                                      &virtio_mmio_base, &virtio_mmio_irq);
+
+    if (rc)
+        return rc;
+    }
+
      /*
       * Every virtio-mmio device uses one emulated SPI. If Virtio devices are
       * present, make sure that we allocate enough SPIs for them.
@@ -984,6 +993,38 @@ static int make_virtio_mmio_node_i2c(libxl__gc *gc, void *fdt, uint64_t base,
      return fdt_end_node(fdt);
  }
 
+static int make_virtio_mmio_node_gpio(libxl__gc *gc, void *fdt, uint64_t base,
+                                      uint32_t irq, uint32_t backend_domid)

tools/libs/light/libxl_arm.c static int make_virtio_mmio_node_i2c(libxl__gc *gc, void *fdt, uint64_t base, · line 1007

Quoted 10 lines
tools/libs/light/libxl_arm.c +10 -0
+{
+    int res;
+
+    res = make_virtio_mmio_node_common(gc, fdt, base, irq, backend_domid);
+    if (res) return res;
+
+    res = fdt_begin_node(fdt, "gpio");
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "virtio,device29");

It is a very descriptive compatible :). And yes I realize this is the compatible chosen by upstream.

Quoted 25 lines
tools/libs/light/libxl_arm.c +20 -0
+    if (res) return res;
+
+    res = fdt_property(fdt, "gpio-controller", NULL, 0);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "#gpio-cells", 2);
+    if (res) return res;
+
+    res = fdt_property(fdt, "interrupt-controller", NULL, 0);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "#interrupt-cells", 2);
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return fdt_end_node(fdt);
+}
+
  static const struct arch_info *get_arch_info(libxl__gc *gc,
                                               const struct xc_dom_image *dom)
  {
@@ -1317,6 +1358,16 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
                                             i2c->backend_domid) );

tools/libs/light/libxl_arm.c static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config, · line 1368

Quoted 10 lines
tools/libs/light/libxl_arm.c +8 -0
          }
 
+        for (i = 0; i < d_config->num_gpios; i++) {
+            libxl_device_gpio *gpio = &d_config->gpios[i];
+
+            if (gpio->backend_domid != LIBXL_TOOLSTACK_DOMID)
+                iommu_needed = true;
+
+            FDT( make_virtio_mmio_node_gpio(gc, fdt, gpio->base, gpio->irq,
+                                            gpio->backend_domid) );

So this is exposing a GPIO interrupt controller but IIUC we will also need to describe the devices in the device-tree. Can you outline how you would expect it to work?

To put some context, this is related to what I wrote in patch #1. How much code will be necessary in libxl to add new device?

I am not the maintainer of this code, but I would expect some example how this can be used once this merged. This would help us to understand whether the interface is correct.

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

Re: [PATCH V5 1/6] libxl: Add support for Virtio I2C device

In reply to Julien Grall
Quoted 7 lines · Julien Grall

Looking at this series, you will add ~250 lines (assuming your new patch) for the i2c and then likely the same amount for GPIO.

I am assuming that for every new virtio device (e.g. gps, sound, display...), we would also need to 250 lines of code. I am worry that we will end up to bloat libxl with duplicated code and or for device that are barely used.

I agree.

Quoted 7 lines

I think it would be better to find a generic way to add new virtio device without adding code (very limited) in libxl. The advantage is someone will be able to create a new virtio device with less effort.

The approach I can think of is something along the lines:

virtio = ["type=<compatible>,transport=<transport>,..."]

It sounds good and yeah it will save a lot of trouble.

Quoted 4 lines

where the compatible is the one that should be written in the DT and transport is mmio or pci. the [...] refers to specific parameters that would need to be passed to the backend (it is not clear how you provide them today?).

The backend doesn't need lot of parameters to be passed right now, the host specific ones (like which devices on a bus to share to the guests) are passed by its command line. The backends in our case are hypervisor agnostic and are run independently as daemons, they just need to know base/irq, which we get from xen libraries.

Quoted 3 lines

AFAICT, the GPIO one may need some tweaking because it requires specific properties. I think it would be more acceptable as this will be only a few lines (compare to 250 lines today).

Right.

If everyone agrees, I will be happy to work towards this.

Signature · 1 line
viresh
#19 lore raw

Re: [PATCH V5 6/6] libxl: Allocate MMIO params for GPIO device and update DT

In reply to Julien Grall
Quoted 2 lines · Julien Grall

It is a very descriptive compatible :). And yes I realize this is the compatible chosen by upstream.

:)

Quoted 3 lines

So this is exposing a GPIO interrupt controller but IIUC we will also need to describe the devices in the device-tree. Can you outline how you would expect it to work?

The same is true for busses like I2C and SPI, which may have client devices in the DT as well.

Though I don't use them currently in my setup where I need to test the backends. I am able to test both I2C and GPIO dynamically from the guest, without any client devices in the DT.

Quoted 6 lines

To put some context, this is related to what I wrote in patch #1. How much code will be necessary in libxl to add new device?

I am not the maintainer of this code, but I would expect some example how this can be used once this merged. This would help us to understand whether the interface is correct.

I understand what you are looking for and that we may be required to pass the relation of a client device with the GPIO controller via the "virtio" entry you suggested for a common device.

I don't have an answer for that at the moment.

Signature · 1 line
viresh
#6 lore raw

Re: [PATCH V5 1/6] libxl: Add support for Virtio I2C device

In reply to Viresh Kumar
Quoted 15 lines · Viresh Kumar
Quoted 7 lines · Julien Grall

Looking at this series, you will add ~250 lines (assuming your new patch) for the i2c and then likely the same amount for GPIO.

I am assuming that for every new virtio device (e.g. gps, sound, display...), we would also need to 250 lines of code. I am worry that we will end up to bloat libxl with duplicated code and or for device that are barely used.

I agree.

Quoted 4 lines

I think it would be better to find a generic way to add new virtio device without adding code (very limited) in libxl. The advantage is someone will be able to create a new virtio device with less effort.

Quoted 5 lines
Quoted 3 lines

The approach I can think of is something along the lines:

virtio = ["type=<compatible>,transport=<transport>,..."]

It sounds good and yeah it will save a lot of trouble.

Yes, I'd like that, too.

Quoted 11 lines
Quoted 5 lines

where the compatible is the one that should be written in the DT and transport is mmio or pci. the [...] refers to specific parameters that would need to be passed to the backend (it is not clear how you provide them today?).

The backend doesn't need lot of parameters to be passed right now, the host specific ones (like which devices on a bus to share to the guests) are passed by its command line. The backends in our case are hypervisor agnostic and are run independently as daemons, they just need to know base/irq, which we get from xen libraries.

When using qemu as a backend this will be quite different.

For my prototype "virtio using grants" I needed to add the following parameters when starting qemu just for one virtio disk:

-drive file=/home/vm/sle15sp1/image,if=none,id=drive-virtio-disk0,format=raw' -device virtio-blk-pci,scsi=off,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=0,disable-legacy=on

As in commit 66dd1c62b2a3c70 I'd suggest to add "backendtype=" in order to support further special handling for e.g. qemu. I guess your backendtype would probably be "standalone", but in case you need further parameters from xl/libxl you could add another type suiting your needs.

Juergen

#7 lore raw

Re: [PATCH V5 1/6] libxl: Add support for Virtio I2C device

In reply to Viresh Kumar
Quoted 13 lines · Viresh Kumar

Hi Anthony,

Thanks a lot for the in-depth review, it is really helpful.

I don't have much knowledge of the Xen code and wanted this code for I2C and GPIO to be tested on Xen for the work we are doing around hypervisor agnostic backends [1].

I started looking for a simple device's implementation and began by (blindly) copying code from Keyboard device and so much of wasted code in here, which isn't really required.

On 06-09-22, 17:15, Anthony PERARD wrote:

Quoted 8 lines
Quoted 5 lines · Viresh Kumar
Quoted 2 lines

An example of domain configuration for Virtio I2c:
i2c = [ "" ]

Is this doing something meaningful (with the whole series applied)?

If I am not wrong, this is required by parse_i2c_list()'s implementation. Without this I don't get the I2C device populated in the guest.

Sorry, I should have added more context to may question. In my mind, I2C is just a communication protocol between a device and a CPU, like USB or PCI. So to me there is missing information about which device or kind of device to present to a guest in this example. For example, for PCI we give a path to the device (via bdf=), and for USB it can be a kind of device to emulate or select a device based on the path to it (bus, addr).

Cheers,

Signature · 1 line
Anthony PERARD