From patchwork Fri Jun 24 15:42:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 9197849 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 43C6F6075F for ; Fri, 24 Jun 2016 15:56:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3420E1FEDE for ; Fri, 24 Jun 2016 15:56:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 28090284B6; Fri, 24 Jun 2016 15:56:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id CDD8E1FEDE for ; Fri, 24 Jun 2016 15:56:01 +0000 (UTC) Received: from localhost ([::1]:44461 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bGTSa-00083g-Vs for patchwork-qemu-devel@patchwork.kernel.org; Fri, 24 Jun 2016 11:56:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55406) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bGTFg-0000ur-BT for qemu-devel@nongnu.org; Fri, 24 Jun 2016 11:42:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bGTFc-0002cI-2p for qemu-devel@nongnu.org; Fri, 24 Jun 2016 11:42:40 -0400 Received: from mail-sn1nam02on0068.outbound.protection.outlook.com ([104.47.36.68]:44251 helo=NAM02-SN1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bGTFb-0002c1-RE for qemu-devel@nongnu.org; Fri, 24 Jun 2016 11:42:36 -0400 Received: from SN1NAM02FT001.eop-nam02.prod.protection.outlook.com (10.152.72.52) by SN1NAM02HT233.eop-nam02.prod.protection.outlook.com (10.152.73.237) with Microsoft SMTP Server (TLS) id 15.1.523.9; Fri, 24 Jun 2016 15:42:33 +0000 Authentication-Results: spf=fail (sender IP is 149.199.60.96) smtp.mailfrom=xilinx.com; linaro.org; dkim=none (message not signed) header.d=none; linaro.org; 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 SN1NAM02FT001.mail.protection.outlook.com (10.152.72.158) with Microsoft SMTP Server (TLS) id 15.1.523.9 via Frontend Transport; Fri, 24 Jun 2016 15:42:33 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:45504 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1bGTFY-0002S9-CG; Fri, 24 Jun 2016 08:42:32 -0700 Received: from [127.0.0.1] (port=54345 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1bGTFY-0004ay-9D; Fri, 24 Jun 2016 08:42:32 -0700 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 u5OFZxRe022976; Fri, 24 Jun 2016 08:36:00 -0700 Received: from [172.19.74.182] (port=54340 helo=xsjalistai50.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1bGTFX-0004av-FM; Fri, 24 Jun 2016 08:42:31 -0700 From: Alistair Francis To: , Date: Fri, 24 Jun 2016 08:42:18 -0700 Message-ID: X-Mailer: git-send-email 2.7.4 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-22410.005 X-TM-AS-Result: No--6.545-7.0-31-10 X-imss-scan-details: No--6.545-7.0-31-10 X-TMASE-MatchedRID: gjHmqQ7feJb/uyguzMmOKARH1Nr7oERd+eBf9ovw8I1BDVeC8J7uwelz n2iMsMvuH+YDSJVIUsZlISHN0f/TWNrgdMxU+gJZFYJUGv4DL3xzd7C7BtJobpqVSxvNUw1V3V8 AphhLZjw7n0++/EjfV7GIvofqADoUcAQtjdWAtvFLc5N+0s1+DVAI6wCVrE3vV2OASbQ/wCW5ip b9yu5bwk/AWMmxU261wVpKW8VwhSpneSxzEFjVUHypjffgdgMPIF0pgClUYTffUZT83lbkEK3S8 lytVfi47x91PDtuLuTk+cMKd+/27H976qbiQKm/DkCVzVLAisFeUPAsYXzs4Jsoi2XrUn/Jn6Kd MrRsL14qtq5d3cxkNTaDH/m8mgSMaDduDV2p19XtUq/KZWIl4h7Hw7sQCu41GV4OWBvNiM4= X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:149.199.60.96; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(1109001)(1110001)(339900001)(199003)(189002)(71366001)(189998001)(4326007)(5003600100003)(356003)(586003)(7846002)(85426001)(7696003)(305945005)(36756003)(5003940100001)(47776003)(50466002)(48376002)(76176999)(50986999)(6806005)(87936001)(64026002)(81166006)(19580395003)(106466001)(33646002)(81156014)(2950100001)(92566002)(8676002)(229853001)(8936002)(19580405001)(50226002)(86362001)(118296001)(9786002)(2906002)(5001770100001)(11100500001)(105606002)(77096005)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1NAM02HT233; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; PTR:unknown-60-96.xilinx.com; A:1; MX:1; CAT:NONE; LANG:en; CAT:NONE; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 2fe6f56b-2a5c-477d-bcbe-08d39c4628ab X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN1NAM02HT233; X-Microsoft-Antispam-PRVS: <288f383ad01e4ce59cd0561d06db9273@SN1NAM02HT233.eop-nam02.prod.protection.outlook.com> X-Exchange-Antispam-Report-Test: UriScan:(192813158149592); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13023025)(13024025)(13015025)(13018025)(13017025)(8121501046)(5005006)(3002001)(10201501046)(6055026); SRVR:SN1NAM02HT233; BCL:0; PCL:0; RULEID:; SRVR:SN1NAM02HT233; X-Forefront-PRVS: 0983EAD6B2 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2016 15:42:33.0077 (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: SN1NAM02HT233 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.36.68 Subject: [Qemu-devel] [PATCH v8 4/8] register: Define REG and FIELD macros X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: edgar.iglesias@xilinx.com, alistair.francis@xilinx.com, crosthwaitepeter@gmail.com, edgar.iglesias@gmail.com, alex.bennee@linaro.org, afaerber@suse.de, fred.konrad@greensocs.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Peter Crosthwaite 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 FIELD_EX32, FIELD_DP32 etc.) can be used to generate extract and deposits without any repetition of the name stems. Signed-off-by: Peter Crosthwaite [ EI Changes: * Add Deposit macros ] Signed-off-by: Edgar E. Iglesias Signed-off-by: Alistair Francis Reviewed-by: Peter Maydell --- 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 = ARRAY_FIELD_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 "FIELD_" variants and still save 2 name stems: uint32_t foobar_val = FIELD_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. */ ARRAY_FIELD_DP32(s->regs, XYZ1, TRC, 5); /* Incorrectly set XYZ1.TRC = 16. */ ARRAY_FIELD_DP32(s->regs, XYZ1, TRC, 16); The latter assignment results in: warning: large integer implicitly truncated to unsigned type [-Woverflow] include/hw/register.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/include/hw/register.h b/include/hw/register.h index 00bbfe5..e8c58a1 100644 --- a/include/hw/register.h +++ b/include/hw/register.h @@ -152,4 +152,50 @@ void register_write_memory(void *opaque, hwaddr addr, uint64_t value, uint64_t register_read_memory(void *opaque, hwaddr addr, unsigned size); +/* Define constants for a 32 bit register */ + +/* This 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). + */ +#define REG32(reg, addr) \ + enum { A_ ## reg = (addr) }; \ + enum { R_ ## reg = (addr) / 4 }; + +/* Define SHIFT, LENGTH and MASK constants for a field within a register */ + +/* This macro will define FOO_BAR_MASK, FOO_BAR_SHIFT and FOO_BAR_LENGTH + * constants for field BAR in register FOO. + */ +#define FIELD(reg, field, shift, length) \ + enum { R_ ## reg ## _ ## field ## _SHIFT = (shift)}; \ + enum { R_ ## reg ## _ ## field ## _LENGTH = (length)}; \ + enum { R_ ## reg ## _ ## field ## _MASK = \ + MAKE_64BIT_MASK(shift, length)}; + +/* Extract a field from a register */ +#define FIELD_EX32(storage, reg, field) \ + extract32((storage), R_ ## reg ## _ ## field ## _SHIFT, \ + R_ ## reg ## _ ## field ## _LENGTH) + +/* Extract a field from an array of registers */ +#define ARRAY_FIELD_EX32(regs, reg, field) \ + FIELD_EX32((regs)[R_ ## reg], reg, field) + +/* Deposit a register field. + * Assigning values larger then the target field will result in + * compilation warnings. + */ +#define FIELD_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 ARRAY_FIELD_DP32(regs, reg, field, val) \ + (regs)[R_ ## reg] = FIELD_DP32((regs)[R_ ## reg], reg, field, val); + #endif