From patchwork Tue Jan 19 22:35:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 8065691 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CA5A4BEEE5 for ; Tue, 19 Jan 2016 22:44:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F2E192045E for ; Tue, 19 Jan 2016 22:44:19 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8D2E32052A for ; Tue, 19 Jan 2016 22:44:17 +0000 (UTC) Received: from localhost ([::1]:39528 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLf0b-000411-1j for patchwork-qemu-devel@patchwork.kernel.org; Tue, 19 Jan 2016 17:44:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49884) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLeuY-00010j-Bp for qemu-devel@nongnu.org; Tue, 19 Jan 2016 17:38:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLeuV-0003vJ-2a for qemu-devel@nongnu.org; Tue, 19 Jan 2016 17:38:02 -0500 Received: from mail-bl2nam02on0089.outbound.protection.outlook.com ([104.47.38.89]:32265 helo=NAM02-BL2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLeuU-0003vB-U6 for qemu-devel@nongnu.org; Tue, 19 Jan 2016 17:37:59 -0500 Received: from BL2NAM02FT061.eop-nam02.prod.protection.outlook.com (10.152.76.56) by BL2NAM02HT092.eop-nam02.prod.protection.outlook.com (10.152.76.174) with Microsoft SMTP Server (TLS) id 15.1.355.15; Tue, 19 Jan 2016 22:37:57 +0000 Authentication-Results: spf=fail (sender IP is 149.199.60.96) smtp.mailfrom=xilinx.com; suse.de; dkim=none (message not signed) header.d=none; suse.de; dmarc=none action=none header.from=xilinx.com; Received-SPF: Fail (protection.outlook.com: domain of xilinx.com does not designate 149.199.60.96 as permitted sender) receiver=protection.outlook.com; client-ip=149.199.60.96; helo=xsj-tvapsmtpgw01; Received: from xsj-tvapsmtpgw01 (149.199.60.96) by BL2NAM02FT061.mail.protection.outlook.com (10.152.77.7) with Microsoft SMTP Server (TLS) id 15.1.355.15 via Frontend Transport; Tue, 19 Jan 2016 22:37:56 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:34490 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1aLeuR-0008RC-8s; Tue, 19 Jan 2016 14:37:55 -0800 Received: from [127.0.0.1] (port=41709 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1aLeuR-0007iA-3Q; Tue, 19 Jan 2016 14:37:55 -0800 Received: from xsj-tvapsmtp02 (smtptest.xilinx.com [172.16.1.203]) by tsj-smtp-dlp1.xlnx.xilinx.com (8.13.8/8.13.1) with ESMTP id u0JMWLMs015299; Tue, 19 Jan 2016 14:32:22 -0800 Received: from [172.19.74.182] (port=39732 helo=xsjalistai50.xlnx.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1aLeuQ-0007i7-Bp; Tue, 19 Jan 2016 14:37:54 -0800 From: Alistair Francis To: Date: Tue, 19 Jan 2016 14:35:28 -0800 Message-ID: <8451de470a1ee42a21ddf2b5be0328415d1f34a5.1453237258.git.alistair.francis@xilinx.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: References: X-RCIS-Action: ALLOW X-TM-AS-MML: disable X-TM-AS-Product-Ver: IMSS-7.1.0.1679-8.0.0.1202-22054.006 X-TM-AS-Result: No--16.562-7.0-31-10 X-imss-scan-details: No--16.562-7.0-31-10 X-TMASE-MatchedRID: F7uwUvpJmwn/uyguzMmOKFz+axQLnAVB+eBf9ovw8I1zeKCaw73ZgQQy VB/q66frzTKJ5j6MjLVffUkQRDINxPgBWl/Ex3vB+cKX6yCQ13v/dK6JacgEu7rHn+Gm5OqdIE6 KlxNg++sXlHizt4pAW751pkQVwz+7sPJBU3ZwaVHETBJM55VRDjuvYa1v2IFh0c4jGwXkyPlmKV VGR/lKLvUtOBUnmrZfndQ6nUJWlG0tlu2PjaXfURl7FdaNVULczbQL1Pyxipk2/UwdvFG5ImCzO eIofg8foWr4Xyi5hiTKBf/S53KIpNh8848Kmd8CuS60kc5B9SwXjfGTDxEzLcA0eOJZYj7tVRRq aNz7dMLEy6OZfIZqFYAy6p60ZV62fJ5/bZ6npdiQZS2ujCtcuA== X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:149.199.60.96; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(1110001)(1109001)(339900001)(189002)(199003)(50466002)(118296001)(5003600100002)(76176999)(2906002)(50226001)(77096005)(189998001)(2351001)(105606002)(5003940100001)(1220700001)(229853001)(110136002)(81156007)(586003)(4326007)(5001960100002)(11100500001)(92566002)(71366001)(106466001)(85426001)(5890100001)(64026002)(19580405001)(2950100001)(36756003)(19580395003)(87936001)(33646002)(50986999)(575784001)(6806005)(47776003)(86362001)(48376002)(5008740100001)(1096002)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:BL2NAM02HT092; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; PTR:unknown-60-96.xilinx.com; MX:1; A:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 9aecc9a9-0ed2-4e3d-5fb9-08d321212d30 X-Exchange-Antispam-Report-Test: UriScan:; BCL:0; PCL:0; RULEID:; SRVR:BL2NAM02HT092; UriScan:(192813158149592); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13017025)(13015025)(8121501046)(13018025)(5005006)(520078)(13023025)(13024025)(3002001)(10201501046); SRVR:BL2NAM02HT092; BCL:0; PCL:0; RULEID:; SRVR:BL2NAM02HT092; X-Forefront-PRVS: 0826B2F01B X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2016 22:37:56.2029 (UTC) X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c; Ip=[149.199.60.96]; Helo=[xsj-tvapsmtpgw01] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2NAM02HT092 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.38.89 Cc: edgar.iglesias@xilinx.com, peter.maydell@linaro.org, alistair.francis@xilinx.com, crosthwaitepeter@gmail.com, edgar.iglesias@gmail.com, afaerber@suse.de Subject: [Qemu-devel] [PATCH v2 14/16] register: Add GPIO API X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Peter Crosthwaite Add GPIO functionality to the register API. This allows association and automatic connection of GPIOs to bits in registers. GPIO inputs will attach to handlers that automatically set read-only bits in registers. GPIO outputs will be updated to reflect their field value when their respective registers are written (or reset). Supports active low GPIOs. This is particularly effective for implementing system level controllers, where heterogenous collections of control signals are placed is a SoC specific peripheral then propagated all over the system. Signed-off-by: Peter Crosthwaite [ EI Changes: * register: Add a polarity field to GPIO connections Makes it possible to directly connect active low signals to generic interrupt pins. ] Signed-off-by: Edgar E. Iglesias Signed-off-by: Alistair Francis --- hw/core/register.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 26 ++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/hw/core/register.c b/hw/core/register.c index 116fd0b..0861603 100644 --- a/hw/core/register.c +++ b/hw/core/register.c @@ -135,6 +135,8 @@ void register_write(RegisterInfo *reg, uint64_t val, uint64_t we) } register_write_fast: register_write_val(reg, new_val); + register_refresh_gpios(reg, old_val); + if (ac->post_write) { ac->post_write(reg, new_val); } @@ -177,18 +179,85 @@ uint64_t register_read(RegisterInfo *reg) void register_reset(RegisterInfo *reg) { assert(reg); + uint64_t old_val; if (!reg->data || !reg->access) { return; } + old_val = register_read_val(reg); + register_write_val(reg, reg->access->reset); + register_refresh_gpios(reg, old_val); +} + +void register_refresh_gpios(RegisterInfo *reg, uint64_t old_value) +{ + const RegisterAccessInfo *ac; + const RegisterGPIOMapping *gpio; + + ac = reg->access; + for (gpio = ac->gpios; gpio && gpio->name; gpio++) { + int i; + + if (gpio->input) { + continue; + } + + for (i = 0; i < gpio->num; ++i) { + uint64_t gpio_value, gpio_value_old; + + qemu_irq gpo = qdev_get_gpio_out_named(DEVICE(reg), gpio->name, i); + gpio_value_old = extract64(old_value, + gpio->bit_pos + i * gpio->width, + gpio->width) ^ gpio->polarity; + gpio_value = extract64(register_read_val(reg), + gpio->bit_pos + i * gpio->width, + gpio->width) ^ gpio->polarity; + if (!(gpio_value_old ^ gpio_value)) { + continue; + } + if (reg->debug && gpo) { + qemu_log("refreshing gpio out %s to %" PRIx64 "\n", + gpio->name, gpio_value); + } + qemu_set_irq(gpo, gpio_value); + } + } +} + +typedef struct DeviceNamedGPIOHandlerOpaque { + DeviceState *dev; + const char *name; +} DeviceNamedGPIOHandlerOpaque; + +static void register_gpio_handler(void *opaque, int n, int level) +{ + DeviceNamedGPIOHandlerOpaque *gho = opaque; + RegisterInfo *reg = REGISTER(gho->dev); + + const RegisterAccessInfo *ac; + const RegisterGPIOMapping *gpio; + + ac = reg->access; + for (gpio = ac->gpios; gpio && gpio->name; gpio++) { + if (gpio->input && !strcmp(gho->name, gpio->name)) { + register_write_val(reg, deposit64(register_read_val(reg), + gpio->bit_pos + n * gpio->width, + gpio->width, + level ^ gpio->polarity)); + return; + } + } + + abort(); } void register_init(RegisterInfo *reg) { assert(reg); const RegisterAccessInfo *ac; + const RegisterGPIOMapping *gpio; if (!reg->data || !reg->access) { return; @@ -197,6 +266,30 @@ void register_init(RegisterInfo *reg) object_initialize((void *)reg, sizeof(*reg), TYPE_REGISTER); ac = reg->access; + for (gpio = ac->gpios; gpio && gpio->name; gpio++) { + if (!gpio->num) { + ((RegisterGPIOMapping *)gpio)->num = 1; + } + if (!gpio->width) { + ((RegisterGPIOMapping *)gpio)->width = 1; + } + if (gpio->input) { + DeviceNamedGPIOHandlerOpaque gho = { + .name = gpio->name, + .dev = DEVICE(reg), + }; + qemu_irq irq; + + qdev_init_gpio_in_named(DEVICE(reg), register_gpio_handler, + gpio->name, gpio->num); + irq = qdev_get_gpio_in_named(DEVICE(reg), gpio->name, gpio->num); + qemu_irq_set_opaque(irq, g_memdup(&gho, sizeof(gho))); + } else { + qemu_irq *gpos = g_new0(qemu_irq, gpio->num); + + qdev_init_gpio_out_named(DEVICE(reg), gpos, gpio->name, gpio->num); + } + } /* if there are no debug msgs and no RMW requirement, mark for fast write */ reg->write_lite = reg->debug || ac->ro || ac->w1c || ac->pre_write || @@ -276,6 +369,7 @@ void register_init_block32(DeviceState *owner, const RegisterAccessInfo *rae, .opaque = owner, }; register_init(r); + qdev_pass_all_gpios(DEVICE(r), owner); memory_region_init_io(&r->mem, OBJECT(owner), ops, r, r->access->name, sizeof(uint32_t)); diff --git a/include/hw/register.h b/include/hw/register.h index f3e4c2c..7a4c1c7 100644 --- a/include/hw/register.h +++ b/include/hw/register.h @@ -13,6 +13,7 @@ #include "hw/qdev-core.h" #include "exec/memory.h" +#include "hw/irq.h" typedef struct RegisterInfo RegisterInfo; typedef struct RegisterAccessInfo RegisterAccessInfo; @@ -28,6 +29,18 @@ typedef struct RegisterAccessError { const char *reason; } RegisterAccessError; +#define REG_GPIO_POL_HIGH 0 +#define REG_GPIO_POL_LOW 1 + +typedef struct RegisterGPIOMapping { + const char *name; + uint8_t bit_pos; + bool input; + bool polarity; + uint8_t num; + uint8_t width; +} RegisterGPIOMapping; + /** * Access description for a register that is part of guest accessible device * state. @@ -78,6 +91,8 @@ struct RegisterAccessInfo { uint64_t (*post_read)(RegisterInfo *reg, uint64_t val); + const RegisterGPIOMapping *gpios; + struct { hwaddr addr; uint8_t flags; @@ -161,6 +176,17 @@ void register_reset(RegisterInfo *reg); void register_init(RegisterInfo *reg); /** + * Refresh GPIO outputs based on diff between old value register current value. + * GPIOs are refreshed for fields where the old value differs to the current + * value. + * + * @reg: Register to refresh GPIO outs + * @old_value: previous value of register + */ + +void register_refresh_gpios(RegisterInfo *reg, uint64_t old_value); + +/** * Memory API MMIO write handler that will write to a Register API register. * _be for big endian variant and _le for little endian. * @opaque: RegisterInfo to write to