From patchwork Mon Apr 27 21:58:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Crosthwaite X-Patchwork-Id: 465233 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 6224714007F for ; Tue, 28 Apr 2015 08:04:37 +1000 (AEST) Received: from localhost ([::1]:57828 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ymr8l-0005ZE-Hk for incoming@patchwork.ozlabs.org; Mon, 27 Apr 2015 18:04:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59075) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ymr2c-00023F-HW for qemu-devel@nongnu.org; Mon, 27 Apr 2015 17:58:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ymr2V-0003AP-AY for qemu-devel@nongnu.org; Mon, 27 Apr 2015 17:58:14 -0400 Received: from mail-bn1bon0076.outbound.protection.outlook.com ([157.56.111.76]:64570 helo=na01-bn1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ymr2V-00039y-0n for qemu-devel@nongnu.org; Mon, 27 Apr 2015 17:58:07 -0400 Received: from BN1AFFO11FD021.protection.gbl (10.58.52.31) by BN1AFFO11HUB047.protection.gbl (10.58.52.109) with Microsoft SMTP Server (TLS) id 15.1.154.14; Mon, 27 Apr 2015 21:58:05 +0000 Authentication-Results: spf=fail (sender IP is 149.199.60.96) smtp.mailfrom=xilinx.com; gmail.com; dkim=none (message not signed) header.d=none; 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 BN1AFFO11FD021.mail.protection.outlook.com (10.58.52.81) with Microsoft SMTP Server (TLS) id 15.1.154.14 via Frontend Transport; Mon, 27 Apr 2015 21:58:03 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:46778 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1Ymqxe-0003V3-5J; Mon, 27 Apr 2015 14:53:06 -0700 Received: from [127.0.0.1] (port=56636 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1Ymr2Q-0005Mo-49; Mon, 27 Apr 2015 14:58:02 -0700 Received: from xsj-tvapsmtp02 (xsj-tvapsmtp02.xilinx.com [172.16.1.203]) by tsj-smtp-dlp1.xlnx.xilinx.com (8.13.8/8.13.1) with ESMTP id t3RLrvpn016806; Mon, 27 Apr 2015 14:53:57 -0700 Received: from [172.19.74.49] (port=48027 helo=xsjsorenbubuntu.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1Ymr2P-0005Me-Au; Mon, 27 Apr 2015 14:58:01 -0700 From: Peter Crosthwaite To: Date: Mon, 27 Apr 2015 14:58:01 -0700 Message-ID: X-Mailer: git-send-email 2.3.6.3.g2cc70ee In-Reply-To: References: X-RCIS-Action: ALLOW X-TM-AS-MML: disable X-TM-AS-Product-Ver: IMSS-7.1.0.1679-7.5.0.1018-21506.005 X-TM-AS-Result: No--10.464-7.0-31-10 X-imss-scan-details: No--10.464-7.0-31-10 X-TMASE-MatchedRID: 7NDqnukzTy03AywPeXaPs2L8FkSLppFB+eBf9ovw8I1BDVeC8J7uwelz n2iMsMvuH+YDSJVIUsZlISHN0f/TWNrgdMxU+gJZFYJUGv4DL3xSQ3ePq+MovAzoAag8Ve1fF0F OWtPeLaJNyVwAosPxSwxHHMavlU4vqAWKvlXcAJ3qsFlQXzLr6C2HcsEx5e57ERHkysQaiHnVkh DxIZrdhAdZU8pJ4X/PCeMkmiOKZfdGlhjnipkGEE4bz0lVdJ95TJDl9FKHbrlFDR0AKGX+XOD0Y 64nEJOeIq9FWHS4IZb70Aj4svCBHB8TzIzimOwPC24oEZ6SpSkj80Za3RRg8NnEWW2+xW5jKRFL 76Qx7TgrYgIcjRLhIrM9wcvWakwj3Lz21U6sOKU= X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:149.199.60.96; CTRY:US; IPV:NLI; EFV:NLI; BMV:1; SFV:NSPM; SFS:(10009020)(979002)(6009001)(339900001)(189002)(199003)(50226001)(2351001)(105606002)(50466002)(5001860100001)(92566002)(106466001)(77096005)(229853001)(19580405001)(19580395003)(77156002)(36756003)(33646002)(86362001)(62966003)(48376002)(76176999)(50986999)(85426001)(118296001)(64026002)(6806004)(87936001)(46102003)(71366001)(2950100001)(47776003)(110136001)(107986001)(217873001)(4001430100001)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1101; SCL:1; SRVR:BN1AFFO11HUB047; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; MLV:ovrnspm; MX:1; A:1; PTR:unknown-60-96.xilinx.com; LANG:en; MIME-Version: 1.0 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BN1AFFO11HUB047; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(5005006)(5002010)(3002001); SRVR:BN1AFFO11HUB047; BCL:0; PCL:0; RULEID:; SRVR:BN1AFFO11HUB047; X-Forefront-PRVS: 0559FB9674 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Apr 2015 21:58:03.6187 (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: BN1AFFO11HUB047 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 157.56.111.76 Cc: peter.maydell@linaro.org, zachp@xilinx.com, jues@xilinx.com, edgari@xilinx.com, kraxel@redhat.com, edgar.iglesias@gmail.com Subject: [Qemu-devel] [RFC PATCH v5 04/14] register: Define REG and FIELD macros 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Define some macros that can be used for defining registers and fields. The REG32 macro will define A_FOO, for the byte address of a register as well as R_FOO for the uint32_t[] register number (A_FOO / 4). The FIELD macro will define FOO_BAR_MASK, FOO_BAR_SHIFT and FOO_BAR_LENGTH constants for field BAR in register FOO. Finally, there are some shorthand helpers for extracting/depositing fields from registers based on these naming schemes. Usage can greatly reduce the verbosity of device code. The deposit and extract macros (eg F_EX32, AF_DP32 etc.) can be used to generate extract and deposits without any repetition of the name stems. E.g. Currently you have to define something like: \#define R_FOOREG (0x84/4) \#define R_FOOREG_BARFIELD_SHIFT 10 \#define R_FOOREG_BARFIELD_LENGTH 5 uint32_t foobar_val = extract32(s->regs[R_FOOREG], R_FOOREG_BARFIELD_SHIFT, R_FOOREG_BARFIELD_LENGTH); Which has: 2 macro definitions per field 3 register names ("FOOREG") per extract 2 field names ("BARFIELD") per extract With these macros this becomes: REG32(FOOREG, 0x84) FIELD(FOOREG, BARFIELD, 10, 5) uint32_t foobar_val = AF_EX32(s->regs, FOOREG, BARFIELD) Which has: 1 macro definition per field 1 register name per extract 1 field name per extract If you are not using arrays for the register data you can just use the non-array "F_" variants and still save 2 name stems: uint32_t foobar_val = F_EX32(s->fooreg, FOOREG, BARFIELD) Deposit is similar for depositing values. Deposit has compile-time overflow checking for literals. For example: REG32(XYZ1, 0x84) FIELD(XYZ1, TRC, 0, 4) /* Correctly set XYZ1.TRC = 5. */ AF_DP32(s->regs, XYZ1, TRC, 5); /* Incorrectly set XYZ1.TRC = 16. */ AF_DP32(s->regs, XYZ1, TRC, 16); The latter assignment results in: warning: large integer implicitly truncated to unsigned type [-Woverflow] Signed-off-by: Peter Crosthwaite [ EI Changes: * Add Deposit macros ] Signed-off-by: Edgar E. Iglesias --- include/hw/register.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/hw/register.h b/include/hw/register.h index 90c0185..7a8671b 100644 --- a/include/hw/register.h +++ b/include/hw/register.h @@ -169,4 +169,44 @@ void register_write_memory_le(void *opaque, hwaddr addr, uint64_t value, uint64_t register_read_memory_be(void *opaque, hwaddr addr, unsigned size); uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size); +/* Define constants for a 32 bit register */ + +#define REG32(reg, addr) \ +enum { A_ ## reg = (addr) }; \ +enum { R_ ## reg = (addr) / 4 }; + +/* Define SHIFT, LEGTH and MASK constants for a field within a register */ + +#define FIELD(reg, field, shift, length) \ +enum { R_ ## reg ## _ ## field ## _SHIFT = (shift)}; \ +enum { R_ ## reg ## _ ## field ## _LENGTH = (length)}; \ +enum { R_ ## reg ## _ ## field ## _MASK = (((1ULL << (length)) - 1) \ + << (shift)) }; + +/* Extract a field from a register */ + +#define F_EX32(storage, reg, field) \ + extract32((storage), R_ ## reg ## _ ## field ## _SHIFT, \ + R_ ## reg ## _ ## field ## _LENGTH) + +/* Extract a field from an array of registers */ + +#define AF_EX32(regs, reg, field) \ + F_EX32((regs)[R_ ## reg], reg, field) + +/* Deposit a register field. */ + +#define F_DP32(storage, reg, field, val) ({ \ + struct { \ + unsigned int v:R_ ## reg ## _ ## field ## _LENGTH; \ + } v = { .v = val }; \ + uint32_t d; \ + d = deposit32((storage), R_ ## reg ## _ ## field ## _SHIFT, \ + R_ ## reg ## _ ## field ## _LENGTH, v.v); \ + d; }) + +/* Deposit a field to array of registers. */ + +#define AF_DP32(regs, reg, field, val) \ + (regs)[R_ ## reg] = F_DP32((regs)[R_ ## reg], reg, field, val); #endif