From patchwork Mon Jul 7 12:27:22 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 33156 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f69.google.com (mail-pa0-f69.google.com [209.85.220.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5E4C620C88 for ; Mon, 7 Jul 2014 12:31:19 +0000 (UTC) Received: by mail-pa0-f69.google.com with SMTP id kq14sf32261792pab.0 for ; Mon, 07 Jul 2014 05:31:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=oI8lQSSDS0wHvhwaFXYQqLKk1CCDdHeSDAsfLF8rx5M=; b=eHM0DeZqq/4f706TuVqvQ84mfNuDVQ7C5H+h/WXOlR2x3KCZzvyKNumOQwrb3kqqMY Tagn8WQdj4DRo3nQYlaNBgz97o/DLAksvc4APDFFDc/gMvENgrpvnHAs2xHGnaW56neq /yphcsSxplkAQNPEjNlmEN1qbAWAM5UeXMVdJ1ryxWHaCkz0E+rL/kRNWn+5Ics0TX2X seCnBoA4EjDebEi1jetAV+o+3+MDZ6JP4XoQefSmu/sfWhriJGIIJmoouJVyk1LONW73 AUL6MX0IgMGFohejanp51TCT+4VVos7hxFUm5gb+KQSf0mzzTDfkGQuwNVg82p+Z35Pv /6Qg== X-Gm-Message-State: ALoCoQkYaS4nF1eGNUYUz/hDoSF9HV+UJALnqkKay6ZOlrvF1MJczDW9H1Fj5o/MPV/luH0tTQPz X-Received: by 10.68.195.9 with SMTP id ia9mr13839752pbc.7.1404736278690; Mon, 07 Jul 2014 05:31:18 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.81.106 with SMTP id e97ls1144958qgd.43.gmail; Mon, 07 Jul 2014 05:31:18 -0700 (PDT) X-Received: by 10.58.152.234 with SMTP id vb10mr27456069veb.21.1404736278586; Mon, 07 Jul 2014 05:31:18 -0700 (PDT) Received: from mail-ve0-f171.google.com (mail-ve0-f171.google.com [209.85.128.171]) by mx.google.com with ESMTPS id x1si1338168vdo.35.2014.07.07.05.31.18 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 05:31:18 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.171 as permitted sender) client-ip=209.85.128.171; Received: by mail-ve0-f171.google.com with SMTP id jz11so3962911veb.16 for ; Mon, 07 Jul 2014 05:31:18 -0700 (PDT) X-Received: by 10.220.94.135 with SMTP id z7mr2929014vcm.46.1404736278451; Mon, 07 Jul 2014 05:31:18 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp662485vcb; Mon, 7 Jul 2014 05:31:17 -0700 (PDT) X-Received: by 10.180.105.68 with SMTP id gk4mr36178155wib.24.1404736276934; Mon, 07 Jul 2014 05:31:16 -0700 (PDT) Received: from mail-wg0-f42.google.com (mail-wg0-f42.google.com [74.125.82.42]) by mx.google.com with ESMTPS id xn13si41032486wib.13.2014.07.07.05.31.10 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 05:31:11 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 74.125.82.42 as permitted sender) client-ip=74.125.82.42; Received: by mail-wg0-f42.google.com with SMTP id n12so2508373wgh.1 for ; Mon, 07 Jul 2014 05:31:10 -0700 (PDT) X-Received: by 10.195.11.132 with SMTP id ei4mr32434372wjd.95.1404736270740; Mon, 07 Jul 2014 05:31:10 -0700 (PDT) Received: from midway01-04-00.lavalab ([88.98.47.97]) by mx.google.com with ESMTPSA id ec8sm95805182wic.10.2014.07.07.05.31.10 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 05:31:10 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, christoffer.dall@linaro.org, qemu-devel@nongnu.org, kim.phillips@freescale.com, a.rigo@virtualopensystems.com Cc: eric.auger@linaro.org, will.deacon@arm.com, kvmarm@lists.cs.columbia.edu, alex.williamson@redhat.com, Bharat.Bhushan@freescale.com, agraf@suse.de, peter.maydell@linaro.org, stuart.yoder@freescale.com, a.motakis@virtualopensystems.com, patches@linaro.org Subject: [RFC v4 12/13] hw/vfio/platform: add default dt generation for vfio device Date: Mon, 7 Jul 2014 13:27:22 +0100 Message-Id: <1404736043-22900-13-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1404736043-22900-1-git-send-email-eric.auger@linaro.org> References: <1404736043-22900-1-git-send-email-eric.auger@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: eric.auger@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.171 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , implement a default dt generation for VFIO platform device. This enables dynamic instantiation. If the device tree generation does not match the expectation it is possible to derive a component and write one's own fdt_add_device_node method. Signed-off-by: Alvise Rigo Signed-off-by: Eric Auger --- hw/vfio/platform.c | 143 ++++++++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-platform.h | 1 + 2 files changed, 144 insertions(+) diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c index fb0f7c9..447ce50 100644 --- a/hw/vfio/platform.c +++ b/hw/vfio/platform.c @@ -22,6 +22,7 @@ #include "qemu/range.h" #include "sysemu/sysemu.h" #include "hw/vfio/vfio-platform.h" +#include "hw/misc/platform_devices.h" extern const MemoryRegionOps vfio_region_ops; extern const MemoryListener vfio_memory_listener; @@ -573,6 +574,142 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp) } } +static char *format_compat(char * compat) +{ + char *str_ptr, *corrected_compat; + /* + * process compatibility property string passed by end-user + * replaces / by , and ; by NUL character + */ + corrected_compat = g_strdup(compat); + /* + * the total length of the string has to include also the last + * NUL char. + */ + + str_ptr = corrected_compat; + while ((str_ptr = strchr(str_ptr, '/')) != NULL) { + *str_ptr = ','; + } + + /* substitute ";" with the NUL char */ + str_ptr = corrected_compat; + while ((str_ptr = strchr(str_ptr, ';')) != NULL) { + *str_ptr = '\0'; + } + + return corrected_compat; +} + +static void wrap_fdt_add_node(SysBusDevice *sbdev, void *opaque) +{ + PlatformDevtreeData *data = opaque; + VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev); + VFIOPlatformDeviceClass *vdevc = VFIO_PLATFORM_DEVICE_GET_CLASS(sbdev); + VFIODevice *vbasedev = &vdev->vbasedev; + gchar irq_number_prop[8]; + Object *obj = OBJECT(sbdev); + char *corrected_compat; + uint64_t irq_number; + int compat_str_len = strlen(vdev->compat)+1; + int i; + + corrected_compat = format_compat(vdev->compat); + snprintf(vdev->compat, compat_str_len, "%s", corrected_compat); + g_free(corrected_compat); + + vdevc->fdt_add_device_node(sbdev, opaque); + + for (i = 0; i < vbasedev->num_irqs; i++) { + snprintf(irq_number_prop, sizeof(irq_number_prop), "irq[%d]", i); + irq_number = object_property_get_int(obj, irq_number_prop, NULL) + + data->irq_start; + /* + * this is a hack: this does not relate to dt creation + * for setting up irqfd we must give the virtual IRQ number + * which is the sum of irq_start and actual platform bus irq + * index. at realize point we do not have this info. + * in case of static instantiation, the irq connect is not yet + * done. In case of dynamic instantiation it was done but we + * miss irq_start which is not stored in the sysbus device. + */ + if (vdev->irqfd_allowed) { + vfio_setup_irqfd(sbdev, i, irq_number); + } + } +} + +static void default_fdt_add_device_node(SysBusDevice *sbdev, void *opaque) +{ + PlatformDevtreeData *data = opaque; + void *fdt = data->fdt; + const char *parent_node = data->node; + uint64_t platform_bus_base = data->platform_bus_base; + int compat_str_len; + char *nodename; + int i, ret; + uint32_t *irq_attr; + uint64_t *reg_attr; + uint64_t mmio_base; + uint64_t irq_number; + gchar mmio_base_prop[8]; + gchar irq_number_prop[8]; + VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev); + VFIODevice *vbasedev = &vdev->vbasedev; + Object *obj = OBJECT(sbdev); + + mmio_base = object_property_get_int(obj, "mmio[0]", NULL); + + nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node, + vbasedev->name, + mmio_base + platform_bus_base); + + qemu_fdt_add_subnode(fdt, nodename); + + compat_str_len = strlen(vdev->compat) + 1; + qemu_fdt_setprop(fdt, nodename, "compatible", + vdev->compat, compat_str_len); + + reg_attr = g_new(uint64_t, vbasedev->num_regions*4); + + for (i = 0; i < vbasedev->num_regions; i++) { + snprintf(mmio_base_prop, sizeof(mmio_base_prop), "mmio[%d]", i); + mmio_base = object_property_get_int(obj, mmio_base_prop, NULL); + reg_attr[4*i] = 2; + reg_attr[4*i+1] = mmio_base + platform_bus_base; + reg_attr[4*i+2] = 2; + reg_attr[4*i+3] = memory_region_size(&vdev->regions[i]->mem); + } + + ret = qemu_fdt_setprop_sized_cells_from_array(fdt, nodename, "reg", + vbasedev->num_regions*2, reg_attr); + if (ret < 0) { + error_report("could not set reg property of node %s", nodename); + } + + irq_attr = g_new(uint32_t, vbasedev->num_irqs*3); + + for (i = 0; i < vbasedev->num_irqs; i++) { + snprintf(irq_number_prop, sizeof(irq_number_prop), "irq[%d]", i); + irq_number = object_property_get_int(obj, irq_number_prop, NULL) + + data->irq_start; + irq_attr[3*i] = cpu_to_be32(0); + irq_attr[3*i+1] = cpu_to_be32(irq_number); + irq_attr[3*i+2] = cpu_to_be32(0x4); + } + + ret = qemu_fdt_setprop(fdt, nodename, "interrupts", + irq_attr, vbasedev->num_irqs*3*sizeof(uint32_t)); + if (ret < 0) { + error_report("could not set interrupts property of node %s", + nodename); + } + + g_free(nodename); + g_free(irq_attr); + g_free(reg_attr); +} + static const VMStateDescription vfio_platform_vmstate = { .name = TYPE_VFIO_PLATFORM, .version_id = 3, @@ -599,6 +736,12 @@ static Property vfio_platform_dev_properties[] = { static void vfio_platform_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); + SysBusDeviceClass *sbdevc = SYS_BUS_DEVICE_CLASS(klass); + VFIOPlatformDeviceClass *vdc = VFIO_PLATFORM_DEVICE_CLASS(klass); + + /* substitute wrap_fdt_add_node to parent fdt_add_node */ + sbdevc->fdt_add_node = wrap_fdt_add_node; + vdc->fdt_add_device_node = default_fdt_add_device_node; dc->realize = vfio_platform_realize; dc->props = vfio_platform_dev_properties; diff --git a/include/hw/vfio/vfio-platform.h b/include/hw/vfio/vfio-platform.h index 134fc1e..1e9a5f4 100644 --- a/include/hw/vfio/vfio-platform.h +++ b/include/hw/vfio/vfio-platform.h @@ -59,6 +59,7 @@ typedef struct VFIOPlatformDeviceClass { /*< private >*/ SysBusDeviceClass parent_class; /*< public >*/ + void (*fdt_add_device_node)(SysBusDevice *sbdev, void *opaque); } VFIOPlatformDeviceClass; #define VFIO_PLATFORM_DEVICE(obj) \