From patchwork Mon Jul 7 12:27:23 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 33158 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qg0-f72.google.com (mail-qg0-f72.google.com [209.85.192.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 884BA20C88 for ; Mon, 7 Jul 2014 12:37:40 +0000 (UTC) Received: by mail-qg0-f72.google.com with SMTP id q107sf11011424qgd.11 for ; Mon, 07 Jul 2014 05:37:40 -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=SkK9IUThbl3CVila5srGb8KtXJxjhHhX/g0U3j9F22s=; b=beqbCVg1ZSeONwHwv85seaWsldi7yDeb6dbzPuK2AO6zLtERdn1AqC56Zcb4vZUtB5 L0yDr2V9voMS+/dZ6d+3iswdW0TJh/XJ0fswjaZaOQVfquDJoDhm9fnYVW/f0Y1g3CFA fgE5dHAV603F2qqkmT1frNRWqdv/ma6xSOpYotI40XN61IfNlDbRyiIAL6UWtoaC4vzX xmBaB22DLz/uktIF8gqLDsvoGB1iQdDElPixfKyAAqTBUAKp9iwR60rKQ6pzX29h7ti8 O5qnlBgPz/5FTePV1ZwcYqKBYYgtojEq1g+XvPXp4JWjeDmsKaUlsf4X3Gh+UXCQ5Wpr OsWQ== X-Gm-Message-State: ALoCoQkJ66+kKULPajycdX9pPRe6OBxO9uJTqqoH25Ye8IWyWxCmFY6TwxcS57nSWCEpQIMOdJY+ X-Received: by 10.58.83.133 with SMTP id q5mr14168432vey.31.1404736660365; Mon, 07 Jul 2014 05:37:40 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.37.193 with SMTP id r59ls711758qgr.35.gmail; Mon, 07 Jul 2014 05:37:40 -0700 (PDT) X-Received: by 10.220.161.8 with SMTP id p8mr27333258vcx.4.1404736660271; Mon, 07 Jul 2014 05:37:40 -0700 (PDT) Received: from mail-ve0-f182.google.com (mail-ve0-f182.google.com [209.85.128.182]) by mx.google.com with ESMTPS id o5si18740645vcj.31.2014.07.07.05.37.40 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 05:37:40 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.182 as permitted sender) client-ip=209.85.128.182; Received: by mail-ve0-f182.google.com with SMTP id oy12so4056624veb.27 for ; Mon, 07 Jul 2014 05:37:40 -0700 (PDT) X-Received: by 10.52.135.7 with SMTP id po7mr8085255vdb.50.1404736660119; Mon, 07 Jul 2014 05:37:40 -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 tc5csp662932vcb; Mon, 7 Jul 2014 05:37:39 -0700 (PDT) X-Received: by 10.180.207.48 with SMTP id lt16mr35918612wic.32.1404736658927; Mon, 07 Jul 2014 05:37:38 -0700 (PDT) Received: from mail-we0-f169.google.com (mail-we0-f169.google.com [74.125.82.169]) by mx.google.com with ESMTPS id hn8si15796176wib.23.2014.07.07.05.37.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 05:37:37 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 74.125.82.169 as permitted sender) client-ip=74.125.82.169; Received: by mail-we0-f169.google.com with SMTP id t60so4361231wes.14 for ; Mon, 07 Jul 2014 05:37:37 -0700 (PDT) X-Received: by 10.180.211.36 with SMTP id mz4mr74797703wic.20.1404736273054; Mon, 07 Jul 2014 05:31:13 -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.11 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 05:31:12 -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 13/13] hw/vfio: add an example calxeda_xgmac Date: Mon, 7 Jul 2014 13:27:23 +0100 Message-Id: <1404736043-22900-14-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.182 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: , Example of a derived VFIO Device. Illustrates how to specialize the device node addition - although it does not make sense for this peculiar device which could reuse the default VFIOPlatformDeviceClass fdt_add_device_node implementation -. The end-user does not need to specify the compat which is hard coded in the realize function. -device vfio-calxeda-xgmac,vfio_device="fff51000.ethernet" cannot be used instead of -device vfio-platform,vfio_device="fff51000.ethernet",compat="calxeda/hb-xgmac" This is an example. NOT TO BE APPLIED for this device. Not-signed-off-by: Eric Auger --- hw/vfio/Makefile.objs | 1 + hw/vfio/calxeda_xgmac.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 hw/vfio/calxeda_xgmac.c diff --git a/hw/vfio/Makefile.objs b/hw/vfio/Makefile.objs index c5c76fe..913ab14 100644 --- a/hw/vfio/Makefile.objs +++ b/hw/vfio/Makefile.objs @@ -2,4 +2,5 @@ ifeq ($(CONFIG_LINUX), y) obj-$(CONFIG_SOFTMMU) += common.o obj-$(CONFIG_PCI) += pci.o obj-$(CONFIG_SOFTMMU) += platform.o +obj-$(CONFIG_SOFTMMU) += calxeda_xgmac.o endif diff --git a/hw/vfio/calxeda_xgmac.c b/hw/vfio/calxeda_xgmac.c new file mode 100644 index 0000000..61010cd --- /dev/null +++ b/hw/vfio/calxeda_xgmac.c @@ -0,0 +1,165 @@ +/* + * calxeda xgmac example VFIO device + * + * Copyright Linaro Limited, 2014 + * + * Authors: + * Eric Auger + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ + +#include "hw/vfio/vfio-platform.h" +#include "hw/misc/platform_devices.h" +#include "qemu/error-report.h" + +#define TYPE_VFIO_CALXEDA_XGMAC "vfio-calxeda-xgmac" + +typedef struct VFIOCalxedaXgmacDevice { + VFIOPlatformDevice vdev; +} VFIOCalxedaXgmacDevice; + +typedef struct VFIOCalxedaXgmacDeviceClass { + /*< private >*/ + VFIOPlatformDeviceClass parent_class; + /*< public >*/ + DeviceRealize parent_realize; +} VFIOCalxedaXgmacDeviceClass; + +#define VFIO_CALXEDA_XGMAC_DEVICE(obj) \ + OBJECT_CHECK(VFIOCalxedaXgmacDevice, (obj), TYPE_VFIO_CALXEDA_XGMAC) +#define VFIO_CALXEDA_XGMAC_DEVICE_CLASS(klass) \ + OBJECT_CLASS_CHECK(VFIOCalxedaXgmacDeviceClass, (klass), \ + TYPE_VFIO_CALXEDA_XGMAC) +#define VFIO_CALXEDA_XGMAC_DEVICE_GET_CLASS(obj) \ + OBJECT_GET_CLASS(VFIOCalxedaXgmacDeviceClass, (obj), \ + TYPE_VFIO_CALXEDA_XGMAC) + +static void calxeda_xgmac_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 void calxeda_xgmac_realize(DeviceState *dev, Error **errp) +{ + VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev); + VFIOCalxedaXgmacDeviceClass *k = VFIO_CALXEDA_XGMAC_DEVICE_GET_CLASS(dev); + int compat_str_len; + const char compat[] = "calxeda,hb-xgmac"; + + if (vdev->compat != NULL) { + /* compat already provided by the user */ + compat_str_len = strlen(vdev->compat)+1; + snprintf(vdev->compat, compat_str_len, "%s", compat); + } else { + vdev->compat = g_strdup(compat); + } + + /* hard code the compat before calling parent realize */ + k->parent_realize(dev, errp); +} + +static const VMStateDescription vfio_platform_vmstate = { + .name = TYPE_VFIO_CALXEDA_XGMAC, + .version_id = 3, + .minimum_version_id = 2, + .fields = (VMStateField[]) { + VMSTATE_END_OF_LIST() + }, + .unmigratable = 1, +}; + +static void vfio_calxeda_xgmac_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VFIOPlatformDeviceClass *vdc = VFIO_PLATFORM_DEVICE_CLASS(klass); + VFIOCalxedaXgmacDeviceClass *vcxc = + VFIO_CALXEDA_XGMAC_DEVICE_CLASS(klass); + vdc->fdt_add_device_node = calxeda_xgmac_fdt_add_device_node; + vcxc->parent_realize = dc->realize; + dc->realize = calxeda_xgmac_realize; + dc->desc = "VFIO Calxeda XGMAC"; +} + +static const TypeInfo vfio_calxeda_xgmac_dev_info = { + .name = TYPE_VFIO_CALXEDA_XGMAC, + .parent = TYPE_VFIO_PLATFORM, + .instance_size = sizeof(VFIOCalxedaXgmacDevice), + .class_init = vfio_calxeda_xgmac_class_init, + .class_size = sizeof(VFIOCalxedaXgmacDeviceClass), +}; + +static void register_calxeda_xgmac_dev_type(void) +{ + type_register_static(&vfio_calxeda_xgmac_dev_info); +} + +type_init(register_calxeda_xgmac_dev_type)