From patchwork Mon Jul 7 07:08:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 33129 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pd0-f200.google.com (mail-pd0-f200.google.com [209.85.192.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7037F20C88 for ; Mon, 7 Jul 2014 07:11:55 +0000 (UTC) Received: by mail-pd0-f200.google.com with SMTP id v10sf28213217pde.7 for ; Mon, 07 Jul 2014 00:11:54 -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=7KMBRlgzUNpPUHXMPRWrBJZ3J9inn6aM2zuOaRGpaIs=; b=Ovg3wbBsZyvn+p55IEKf+zaK0upxrtIIgcRpHRnApZ7ro/UW/drfdFCEzz9woMEKnR c2vjh2Ku9BxsBKGREhGm9Hj0T6ZwZEwludB/Iq3i96l8k1dp8NmybDaRe116wr9wHQhP HxBPv5U5TMJ0Iao3G+ZsYT6fYjKoGoTgZKNVQ4kD5AKXxJMfxNFopVtw6UTm/5mnB0RR vsH8o3NKiyJ+t8eBIw8Wp6tQkw4yMBwz0knIHInkG8aly0+xGWAXHWADmEQvWn7WgAKq 5R4PYsIbAW55QuhcFrVtoSYH6tIkIOJ1+hBqvvn32k2HSPGdk6MGGG5Hyg+Qo04liBtw VA8Q== X-Gm-Message-State: ALoCoQlv/e+EEZEWgJGtlQ/pZX1b3PC7GDFVLV1N3G1Nvipi0WR9ts2u7jkwMxPoq1R1aV0XSTko X-Received: by 10.66.102.68 with SMTP id fm4mr13227075pab.27.1404717114742; Mon, 07 Jul 2014 00:11:54 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.23.210 with SMTP id 76ls1517115qgp.33.gmail; Mon, 07 Jul 2014 00:11:54 -0700 (PDT) X-Received: by 10.220.15.8 with SMTP id i8mr1695796vca.45.1404717114644; Mon, 07 Jul 2014 00:11:54 -0700 (PDT) Received: from mail-vc0-f174.google.com (mail-vc0-f174.google.com [209.85.220.174]) by mx.google.com with ESMTPS id bk6si18462742vcb.55.2014.07.07.00.11.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 00:11:54 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.174 as permitted sender) client-ip=209.85.220.174; Received: by mail-vc0-f174.google.com with SMTP id hy4so3532627vcb.33 for ; Mon, 07 Jul 2014 00:11:54 -0700 (PDT) X-Received: by 10.58.243.170 with SMTP id wz10mr10768vec.55.1404717114554; Mon, 07 Jul 2014 00:11:54 -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 tc5csp644869vcb; Mon, 7 Jul 2014 00:11:54 -0700 (PDT) X-Received: by 10.194.104.97 with SMTP id gd1mr28891129wjb.77.1404717113628; Mon, 07 Jul 2014 00:11:53 -0700 (PDT) Received: from mail-wg0-f41.google.com (mail-wg0-f41.google.com [74.125.82.41]) by mx.google.com with ESMTPS id wo1si48783448wjb.11.2014.07.07.00.11.53 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 00:11:53 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 74.125.82.41 as permitted sender) client-ip=74.125.82.41; Received: by mail-wg0-f41.google.com with SMTP id k14so1231401wgh.24 for ; Mon, 07 Jul 2014 00:11:53 -0700 (PDT) X-Received: by 10.194.236.132 with SMTP id uu4mr29541722wjc.54.1404717112981; Mon, 07 Jul 2014 00:11:52 -0700 (PDT) Received: from midway01-04-00.lavalab ([88.98.47.97]) by mx.google.com with ESMTPSA id fb15sm111830218wid.23.2014.07.07.00.11.52 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Jul 2014 00:11:52 -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, kvmarm@lists.cs.columbia.edu, alex.williamson@redhat.com, agraf@suse.de, peter.maydell@linaro.org, stuart.yoder@freescale.com, a.motakis@virtualopensystems.com, patches@linaro.org Subject: [PATCH 1/7] hw/misc/platform_devices: helpers for dynamic instantiation of platform devices Date: Mon, 7 Jul 2014 08:08:06 +0100 Message-Id: <1404716892-15600-2-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1404716892-15600-1-git-send-email-eric.auger@linaro.org> References: <1404716892-15600-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.220.174 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: , This new module implements routines which help in dynamic instantiation of sysbus devices. Machine files can use those generic routines. --- Dynamic sysbus device allocation fully written by Alex Graf. [Eric Auger] Those functions were initially in ppc e500 machine file. Now moved to a separate module. PPCE500Params is replaced by a generic struct named PlatformParams Signed-off-by: Alexander Graf Signed-off-by: Eric Auger --- hw/misc/Makefile.objs | 1 + hw/misc/platform_devices.c | 217 +++++++++++++++++++++++++++++++++++++ include/hw/misc/platform_devices.h | 61 +++++++++++ 3 files changed, 279 insertions(+) create mode 100644 hw/misc/platform_devices.c create mode 100644 include/hw/misc/platform_devices.h diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs index e47fea8..d081606 100644 --- a/hw/misc/Makefile.objs +++ b/hw/misc/Makefile.objs @@ -40,3 +40,4 @@ obj-$(CONFIG_SLAVIO) += slavio_misc.o obj-$(CONFIG_ZYNQ) += zynq_slcr.o obj-$(CONFIG_PVPANIC) += pvpanic.o +obj-y += platform_devices.o diff --git a/hw/misc/platform_devices.c b/hw/misc/platform_devices.c new file mode 100644 index 0000000..96ab272 --- /dev/null +++ b/hw/misc/platform_devices.c @@ -0,0 +1,217 @@ +#include "hw/misc/platform_devices.h" +#include "hw/sysbus.h" +#include "qemu/error-report.h" + +#define PAGE_SHIFT 12 + +int sysbus_device_create_devtree(Object *obj, void *opaque) +{ + PlatformDevtreeData *data = opaque; + Object *dev; + SysBusDevice *sbdev; + bool matched = false; + + dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE); + sbdev = (SysBusDevice *)dev; + + if (!sbdev) { + /* Container, traverse it for children */ + return object_child_foreach(obj, sysbus_device_create_devtree, data); + } + + if (!matched) { + error_report("Device %s is not supported by this machine yet.", + qdev_fw_name(DEVICE(dev))); + exit(1); + } + + return 0; +} + +void platform_bus_create_devtree(PlatformParams *params, void *fdt, + const char *mpic) +{ + gchar *node = g_strdup_printf("/platform@%"PRIx64, + params->platform_bus_base); + const char platcomp[] = "qemu,platform\0simple-bus"; + PlatformDevtreeData data; + Object *container; + uint64_t addr = params->platform_bus_base; + uint64_t size = params->platform_bus_size; + int irq_start = params->platform_bus_first_irq; + + /* Create a /platform node that we can put all devices into */ + + qemu_fdt_add_subnode(fdt, node); + qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp)); + + /* Our platform bus region is less than 32bit big, so 1 cell is enough for + address and size */ + qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1); + qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1); + qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, size); + + qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", mpic); + + /* Loop through all devices and create nodes for known ones */ + data.fdt = fdt; + data.mpic = mpic; + data.irq_start = irq_start; + data.node = node; + + container = container_get(qdev_get_machine(), "/peripheral"); + sysbus_device_create_devtree(container, &data); + container = container_get(qdev_get_machine(), "/peripheral-anon"); + sysbus_device_create_devtree(container, &data); + + g_free(node); +} + +int platform_bus_map_irq(PlatformParams *params, SysBusDevice *sbdev, + int n, unsigned long *used_irqs, + qemu_irq *platform_irqs) +{ + int max_irqs = params->platform_bus_num_irqs; + char *prop = g_strdup_printf("irq[%d]", n); + int irqn = object_property_get_int(OBJECT(sbdev), prop, NULL); + + if (irqn == SYSBUS_DYNAMIC) { + /* Find the first available IRQ */ + irqn = find_first_zero_bit(used_irqs, max_irqs); + } + + if ((irqn >= max_irqs) || test_and_set_bit(irqn, used_irqs)) { + hw_error("IRQ %d is already allocated or no free IRQ left", irqn); + } + + sysbus_connect_irq(sbdev, n, platform_irqs[irqn]); + object_property_set_int(OBJECT(sbdev), irqn, prop, NULL); + + g_free(prop); + return 0; +} + +int platform_bus_map_mmio(PlatformParams *params, SysBusDevice *sbdev, + int n, unsigned long *used_mem, + MemoryRegion *pmem) +{ + MemoryRegion *device_mem = sbdev->mmio[n].memory; + uint64_t size = memory_region_size(device_mem); + uint64_t page_size = (1 << PAGE_SHIFT); + uint64_t page_mask = page_size - 1; + uint64_t size_pages = (size + page_mask) >> PAGE_SHIFT; + uint64_t max_size = params->platform_bus_size; + uint64_t max_pages = max_size >> PAGE_SHIFT; + char *prop = g_strdup_printf("mmio[%d]", n); + hwaddr addr = object_property_get_int(OBJECT(sbdev), prop, NULL); + int page; + int i; + + page = addr >> PAGE_SHIFT; + if (addr == SYSBUS_DYNAMIC) { + uint64_t size_pages_align; + + /* Align the region to at least its own size granularity */ + if (is_power_of_2(size_pages)) { + size_pages_align = size_pages; + } else { + size_pages_align = pow2floor(size_pages) << 1; + } + + /* Find the first available region that fits */ + page = bitmap_find_next_zero_area(used_mem, max_pages, 0, size_pages, + size_pages_align); + + addr = (uint64_t)page << PAGE_SHIFT; + } + + if (page >= max_pages || test_bit(page, used_mem) || + (find_next_bit(used_mem, max_pages, page) < size_pages)) { + hw_error("Memory [%"PRIx64":%"PRIx64" is already allocated or " + "no slot left", addr, size); + } + + for (i = page; i < (page + size_pages); i++) { + set_bit(i, used_mem); + } + + memory_region_add_subregion(pmem, addr, device_mem); + sbdev->mmio[n].addr = addr; + object_property_set_int(OBJECT(sbdev), addr, prop, NULL); + + g_free(prop); + return 0; +} + +int sysbus_device_check(Object *obj, void *opaque) +{ + PlatformBusInitData *init = opaque; + Object *dev; + SysBusDevice *sbdev; + int i; + + dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE); + sbdev = (SysBusDevice *)dev; + + if (!sbdev) { + /* Container, traverse it for children */ + return object_child_foreach(obj, sysbus_device_check, opaque); + } + + /* Connect sysbus device to virtual platform bus */ + for (i = 0; i < sbdev->num_irq; i++) { + if (!sbdev->irqp[i]) { + /* This IRQ is an incoming IRQ, we can't wire those here */ + continue; + } + platform_bus_map_irq(init->params, sbdev, i, + init->used_irqs, init->irqs); + } + + for (i = 0; i < sbdev->num_mmio; i++) { + platform_bus_map_mmio(init->params, sbdev, i, + init->used_mem, init->mem); + } + + return 0; +} + +void platform_bus_init(PlatformParams *params, + MemoryRegion *address_space_mem, + qemu_irq *mpic) +{ + uint64_t max_size = params->platform_bus_size; + uint64_t max_pages = max_size >> PAGE_SHIFT; + DECLARE_BITMAP(used_irqs, params->platform_bus_num_irqs); + DECLARE_BITMAP(used_mem, max_pages); + MemoryRegion *platform_region = g_new(MemoryRegion, 1); + Object *container; + PlatformBusInitData init = { + .used_irqs = used_irqs, + .used_mem = used_mem, + .mem = platform_region, + .irqs = &mpic[params->platform_bus_first_irq], + .params = params, + }; + + memory_region_init(platform_region, NULL, "platform devices", + params->platform_bus_size); + + bitmap_clear(used_irqs, 0, params->platform_bus_num_irqs); + bitmap_clear(used_mem, 0, max_pages); + + /* Loop through all sysbus devices that were spawened outside the machine */ + container = container_get(qdev_get_machine(), "/peripheral"); + sysbus_device_check(container, &init); + container = container_get(qdev_get_machine(), "/peripheral-anon"); + sysbus_device_check(container, &init); + + memory_region_add_subregion(address_space_mem, params->platform_bus_base, + platform_region); +} + +void platform_bus_init_notify(Notifier *notifier, void *data) +{ + PlatformBusNotifier *pn = (PlatformBusNotifier *)notifier; + platform_bus_init(&pn->params, pn->address_space_mem, pn->mpic); +} diff --git a/include/hw/misc/platform_devices.h b/include/hw/misc/platform_devices.h new file mode 100644 index 0000000..ab79346 --- /dev/null +++ b/include/hw/misc/platform_devices.h @@ -0,0 +1,61 @@ +#ifndef HW_MISC_PLATFORM_DEVICES_H +#define HW_MISC_PLATFORM_DEVICES_H + +#include "qemu-common.h" +#include "sysemu/device_tree.h" +#include "hw/hw.h" +#include "hw/sysbus.h" + +typedef struct PlatformDevtreeData { + void *fdt; + const char *mpic; + int irq_start; + const char *node; +} PlatformDevtreeData; + +typedef struct { + bool has_platform_bus; + hwaddr platform_bus_base; + hwaddr platform_bus_size; + int platform_bus_first_irq; + int platform_bus_num_irqs; +} PlatformParams; + +typedef struct PlatformBusNotifier { + Notifier notifier; + MemoryRegion *address_space_mem; + qemu_irq *mpic; + PlatformParams params; +} PlatformBusNotifier; + +typedef struct PlatformBusInitData { + unsigned long *used_irqs; + unsigned long *used_mem; + MemoryRegion *mem; + qemu_irq *irqs; + int device_count; + PlatformParams *params; +} PlatformBusInitData; + + +int sysbus_device_create_devtree(Object *obj, void *opaque); + +void platform_bus_create_devtree(PlatformParams *params, void *fdt, + const char *mpic); + +int platform_bus_map_irq(PlatformParams *params, SysBusDevice *sbdev, + int n, unsigned long *used_irqs, + qemu_irq *platform_irqs); + +int platform_bus_map_mmio(PlatformParams *params, SysBusDevice *sbdev, + int n, unsigned long *used_mem, + MemoryRegion *pmem); + +int sysbus_device_check(Object *obj, void *opaque); + +void platform_bus_init(PlatformParams *params, + MemoryRegion *address_space_mem, + qemu_irq *mpic); + +void platform_bus_init_notify(Notifier *notifier, void *data); +#endif