From patchwork Tue Mar 8 21:06:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 8537851 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5CBE09F372 for ; Tue, 8 Mar 2016 21:14:30 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 99BBC201E4 for ; Tue, 8 Mar 2016 21:14:29 +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 AAD10201C8 for ; Tue, 8 Mar 2016 21:14:28 +0000 (UTC) Received: from localhost ([::1]:37446 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adOxY-0004jt-1g for patchwork-qemu-devel@patchwork.kernel.org; Tue, 08 Mar 2016 16:14:28 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adOt4-0005ci-DF for qemu-devel@nongnu.org; Tue, 08 Mar 2016 16:09:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1adOt1-0002h5-14 for qemu-devel@nongnu.org; Tue, 08 Mar 2016 16:09:50 -0500 Received: from mail-bl2nam02on0052.outbound.protection.outlook.com ([104.47.38.52]:44053 helo=NAM02-BL2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adOt0-0002gj-N9 for qemu-devel@nongnu.org; Tue, 08 Mar 2016 16:09:46 -0500 Received: from CY1NAM02FT039.eop-nam02.prod.protection.outlook.com (10.152.74.51) by CY1NAM02HT026.eop-nam02.prod.protection.outlook.com (10.152.75.63) with Microsoft SMTP Server (TLS) id 15.1.427.7; Tue, 8 Mar 2016 21:09:45 +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 CY1NAM02FT039.mail.protection.outlook.com (10.152.75.140) with Microsoft SMTP Server (TLS) id 15.1.434.11 via Frontend Transport; Tue, 8 Mar 2016 21:09:43 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:47529 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1adOsx-0006CU-4r; Tue, 08 Mar 2016 13:09:43 -0800 Received: from [127.0.0.1] (port=48473 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1adOsw-0000dz-Vn; Tue, 08 Mar 2016 13:09:43 -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 u28L3ql0021875; Tue, 8 Mar 2016 13:03:52 -0800 Received: from [172.19.74.182] (port=56652 helo=xsjalistai50.xlnx.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1adOsw-0000dv-76; Tue, 08 Mar 2016 13:09:42 -0800 From: Alistair Francis To: , Date: Tue, 8 Mar 2016 13:06:42 -0800 Message-ID: <0b808193b040a4b66490ca9fb17a5ddd4e266594.1457470980.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-22178.005 X-TM-AS-Result: No--6.010-7.0-31-10 X-imss-scan-details: No--6.010-7.0-31-10 X-TMASE-MatchedRID: NKGAuCCEJLP/uyguzMmOKARH1Nr7oERd+eBf9ovw8I1BDVeC8J7uwelz n2iMsMvuH+YDSJVIUsZlISHN0f/TWNrgdMxU+gJZFYJUGv4DL3xSQ3ePq+MovNxO0k825ccrXvb ZqadMHiiQxDPca/JPfb1dtAh/jS6xNGfbF3CuxemPmEs8Jfdl0zAuMzu3eJGjmxG2oD8GDzMcRf 6rcNVSQ39d75CuOUMrZPsCj1QObi+U0uaueRR8nAPZZctd3P4Bu56wFPSkMVEB+d6VtK8rvFy7h hjvf5y4t8Ld+HAVjvOAMuqetGVetnyef22ep6XYOwBXM346/+xTBr2zTUq2dd2MNNgWMftLJTsK 51vpKo1WnNGQD1kra5yiFhfPCJTf 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)(199003)(189002)(106466001)(2906002)(105606002)(2950100001)(36756003)(5003940100001)(4326007)(64026002)(77096005)(586003)(71366001)(229853001)(1220700001)(19580395003)(50226001)(19580405001)(5003600100002)(48376002)(86362001)(5008740100001)(87936001)(85426001)(6806005)(118296001)(50986999)(11100500001)(33646002)(1096002)(5001770100001)(81166005)(76176999)(50466002)(92566002)(47776003)(189998001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1NAM02HT026; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; MLV:sfv; A:1; MX:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 366000a9-caae-46a7-a88e-08d34795f93b X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:CY1NAM02HT026; X-Microsoft-Antispam-PRVS: <88a21cecf6a543a8b458665a87e7089f@CY1NAM02HT026.eop-nam02.prod.protection.outlook.com> X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(8121501046)(13015025)(5005006)(13017025)(13018025)(13024025)(13023025)(3002001)(10201501046); SRVR:CY1NAM02HT026; BCL:0; PCL:0; RULEID:; SRVR:CY1NAM02HT026; X-Forefront-PRVS: 08756AC3C8 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2016 21:09:43.6038 (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: CY1NAM02HT026 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.38.52 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 Subject: [Qemu-devel] [PATCH v5 05/15] 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+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 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. Signed-off-by: Peter Crosthwaite [ EI Changes: * Add Deposit macros ] Signed-off-by: Edgar E. Iglesias Signed-off-by: Alistair Francis --- 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] include/hw/register.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/include/hw/register.h b/include/hw/register.h index bc2c96a..b105d76 100644 --- a/include/hw/register.h +++ b/include/hw/register.h @@ -161,4 +161,42 @@ 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