From patchwork Sat Jan 30 01:00:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 8169181 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 51BE0BEEE5 for ; Sat, 30 Jan 2016 01:03:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4F51820381 for ; Sat, 30 Jan 2016 01:03:47 +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 1094620380 for ; Sat, 30 Jan 2016 01:03:46 +0000 (UTC) Received: from localhost ([::1]:37182 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPJx3-0004YY-GE for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Jan 2016 20:03:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54906) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPJwq-0004Ty-0G for qemu-devel@nongnu.org; Fri, 29 Jan 2016 20:03:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aPJwl-0007zw-4r for qemu-devel@nongnu.org; Fri, 29 Jan 2016 20:03:31 -0500 Received: from mail-cys01nam02on0080.outbound.protection.outlook.com ([104.47.37.80]:37576 helo=NAM02-CY1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPJwk-0007zh-TD for qemu-devel@nongnu.org; Fri, 29 Jan 2016 20:03:27 -0500 Received: from CY1NAM02FT019.eop-nam02.prod.protection.outlook.com (10.152.74.52) by CY1NAM02HT232.eop-nam02.prod.protection.outlook.com (10.152.74.118) with Microsoft SMTP Server (TLS) id 15.1.355.15; Sat, 30 Jan 2016 01:03:25 +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 CY1NAM02FT019.mail.protection.outlook.com (10.152.75.177) with Microsoft SMTP Server (TLS) id 15.1.355.15 via Frontend Transport; Sat, 30 Jan 2016 01:03:24 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:45439 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1aPJwh-0002NK-Ro; Fri, 29 Jan 2016 17:03:23 -0800 Received: from [127.0.0.1] (port=52657 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1aPJwh-0002sr-M9; Fri, 29 Jan 2016 17:03:23 -0800 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 u0U0vlx9022004; Fri, 29 Jan 2016 16:57:47 -0800 Received: from [172.19.74.182] (port=49966 helo=xsjalistai50.xlnx.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1aPJwg-0002so-TT; Fri, 29 Jan 2016 17:03:22 -0800 From: Alistair Francis To: , , Date: Fri, 29 Jan 2016 17:00:47 -0800 Message-ID: <0e7c92d2ff8312229109245ba0c5b38ced753295.1454115217.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--18.468-7.0-31-10 X-imss-scan-details: No--18.468-7.0-31-10 X-TMASE-MatchedRID: Fxf8DVaRv943AywPeXaPsy2taMI32X8W0nXvwjW2mSXJYIv7y0tu9lUn QAOplXiG0TB48RcQxBCcAs5VJqoVZWKztShDQzFuqeBupNgLgYf54F/2i/DwjVmmz7LVVfOpoud 5cX9BENNRUSirfNnIk1vrM+4RbbUVKmzIo+yV5o9CnGIuUMP0VZIP/SO+XIBSEvoxTu3fj1ug/2 Cq6PCYoWxsZjteC2njjf93Q249wjXpzoZfJABOugPRhGX0wT3l7T7u8yAbuqNm+6Fpy57LxYqNo tXYNqA4tlzui8kfp9+gGOZ3m2LY55LgPNLaK+hFyeVujmXuYYW8k98SikuQ7GecrqZc3vab+dvh pKMyrbq0FNCiMLM5PyDrcBJ61i9agZI1n6aNUzb5wpfrIJDXezul9KNGwPY6vNk1d8kbf5q8GjE WiUn6YFtp9TGLSROq7uXsxANhR1Oyf+8sXPWRX0Zakoam9+aeDKe/CNv759Q2/UwdvFG5Ih4Gje p4mLxtWS9fn8WHyQdQtACpIb1B3rhoJJDuF0nvYwaJXrf2IXQ4WsSNiH/UXvgnJH5vm2+g0aZfZ aoruP8xtZLhhft96/92xC57U5gxEQqI6rdF4DHYxkZC4pzxSMnlJe2gk8vIBlt4RZwvTdVgL+3Z RWR7aBqEduuQPZuW8Rx/yy+r2WUM8jMXjBF+sBRFJJyf5BJerSFs54Y4wbX6C0ePs7A07QKmARN 5PTKc 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)(5001960100002)(106466001)(2950100001)(1096002)(6806005)(229853001)(71366001)(19580405001)(47776003)(64026002)(11100500001)(105606002)(50986999)(586003)(5001770100001)(5003600100002)(77096005)(1220700001)(189998001)(5008740100001)(2906002)(86362001)(4326007)(118296001)(76176999)(2201001)(50466002)(19580395003)(5003940100001)(85426001)(3470700001)(33646002)(36756003)(48376002)(50226001)(92566002)(575784001)(87936001)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1NAM02HT232; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; MLV:sfv; MX:1; A:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: dc98e4e3-b97a-4cca-05f0-08d3291127cb X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:CY1NAM02HT232; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192813158149592); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13023025)(13017025)(8121501046)(13024025)(13015025)(13018025)(5005006)(3002001)(10201501046); SRVR:CY1NAM02HT232; BCL:0; PCL:0; RULEID:; SRVR:CY1NAM02HT232; X-Forefront-PRVS: 083751FCA6 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jan 2016 01:03:24.4108 (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: CY1NAM02HT232 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.37.80 Cc: edgar.iglesias@xilinx.com, edgar.iglesias@gmail.com, crosthwaitepeter@gmail.com, afaerber@suse.de, alistair.francis@xilinx.com Subject: [Qemu-devel] [PATCH v3 02/16] register: Add Register 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 This API provides some encapsulation of registers and factors our some common functionality to common code. Bits of device state (usually MMIO registers), often have all sorts of access restrictions and semantics associated with them. This API allow you to define what those restrictions are on a bit-by-bit basis. Helper functions are then used to access the register which observe the semantics defined by the RegisterAccessInfo struct. Some features: Bits can be marked as read_only (ro field) Bits can be marked as write-1-clear (w1c field) Bits can be marked as reserved (rsvd field) Reset values can be defined (reset) Bits can throw guest errors when written certain values (ge0, ge1) Bits can throw unimp errors when written certain values (ui0, ui1) Bits can be marked clear on read (cor) Pre and post action callbacks can be added to read and write ops Verbose debugging info can be enabled/disabled Useful for defining device register spaces in a data driven way. Cuts down on a lot of the verbosity and repetition in the switch-case blocks in the standard foo_mmio_read/write functions. Also useful for automated generation of device models from hardware design sources. Signed-off-by: Peter Crosthwaite Signed-off-by: Alistair Francis --- V3: - Address some comments from Fred hw/core/Makefile.objs | 1 + hw/core/register.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 132 +++++++++++++++++++++++++++++++++++ 3 files changed, 322 insertions(+) create mode 100644 hw/core/register.c create mode 100644 include/hw/register.h diff --git a/hw/core/Makefile.objs b/hw/core/Makefile.objs index abb3560..bf95db5 100644 --- a/hw/core/Makefile.objs +++ b/hw/core/Makefile.objs @@ -14,4 +14,5 @@ common-obj-$(CONFIG_SOFTMMU) += machine.o common-obj-$(CONFIG_SOFTMMU) += null-machine.o common-obj-$(CONFIG_SOFTMMU) += loader.o common-obj-$(CONFIG_SOFTMMU) += qdev-properties-system.o +common-obj-$(CONFIG_SOFTMMU) += register.o common-obj-$(CONFIG_PLATFORM_BUS) += platform-bus.o diff --git a/hw/core/register.c b/hw/core/register.c new file mode 100644 index 0000000..f0fc39c --- /dev/null +++ b/hw/core/register.c @@ -0,0 +1,189 @@ +/* + * Register Definition API + * + * Copyright (c) 2013 Xilinx Inc. + * Copyright (c) 2013 Peter Crosthwaite + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#include "hw/register.h" +#include "qemu/log.h" + +static inline void register_write_log(RegisterInfo *reg, int dir, uint64_t val, + int mask, const char *msg, + const char *reason) +{ + qemu_log_mask(mask, "%s:%s bits %#" PRIx64 " %s write of %d%s%s\n", + reg->prefix, reg->access->name, val, msg, dir, + reason ? ": " : "", reason ? reason : ""); +} + +static inline void register_write_val(RegisterInfo *reg, uint64_t val) +{ + if (!reg->data) { + return; + } + switch (reg->data_size) { + case 1: + *(uint8_t *)reg->data = val; + break; + case 2: + *(uint16_t *)reg->data = val; + break; + case 4: + *(uint32_t *)reg->data = val; + break; + case 8: + *(uint64_t *)reg->data = val; + break; + default: + abort(); + } +} + +static inline uint64_t register_read_val(RegisterInfo *reg) +{ + switch (reg->data_size) { + case 1: + return *(uint8_t *)reg->data; + case 2: + return *(uint16_t *)reg->data; + case 4: + return *(uint32_t *)reg->data; + case 8: + return *(uint64_t *)reg->data; + default: + abort(); + } + return 0; /* unreachable */ +} + +void register_write(RegisterInfo *reg, uint64_t val, uint64_t we) +{ + uint64_t old_val, new_val, test, no_w_mask; + const RegisterAccessInfo *ac; + const RegisterAccessError *rae; + + assert(reg); + + ac = reg->access; + + if (!ac || !ac->name) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: write to undefined device state " + "(written value: %#" PRIx64 ")\n", reg->prefix, val); + return; + } + + old_val = reg->data ? register_read_val(reg) : ac->reset; + + if (reg->write_lite && !~we) { /* fast path!! */ + new_val = val; + goto register_write_fast; + } + + no_w_mask = ac->ro | ac->w1c | ~we; + + if (reg->debug) { + qemu_log("%s:%s: write of value %#" PRIx64 "\n", reg->prefix, ac->name, + val); + } + + if (qemu_loglevel_mask(LOG_GUEST_ERROR)) { + test = (old_val ^ val) & ac->rsvd; + if (test) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: change of value in reserved bit" + "fields: %#" PRIx64 ")\n", reg->prefix, test); + } + for (rae = ac->ge1; rae && rae->mask; rae++) { + test = val & rae->mask; + if (test) { + register_write_log(reg, 1, test, LOG_GUEST_ERROR, + "invalid", rae->reason); + } + } + for (rae = ac->ge0; rae && rae->mask; rae++) { + test = ~val & rae->mask; + if (test) { + register_write_log(reg, 0, test, LOG_GUEST_ERROR, + "invalid", rae->reason); + } + } + } + + if (qemu_loglevel_mask(LOG_UNIMP)) { + for (rae = ac->ui1; rae && rae->mask; rae++) { + test = val & rae->mask; + if (test) { + register_write_log(reg, 1, test, LOG_GUEST_ERROR, + "unimplmented", rae->reason); + } + } + for (rae = ac->ui0; rae && rae->mask; rae++) { + test = ~val & rae->mask; + if (test) { + register_write_log(reg, 0, test, LOG_GUEST_ERROR, + "unimplemented", rae->reason); + } + } + } + + new_val = (val & ~no_w_mask) | (old_val & no_w_mask); + new_val &= ~(val & ac->w1c); + + if (ac->pre_write) { + new_val = ac->pre_write(reg, new_val); + } + +register_write_fast: + register_write_val(reg, new_val); + if (ac->post_write) { + ac->post_write(reg, new_val); + } +} + +uint64_t register_read(RegisterInfo *reg) +{ + uint64_t ret; + const RegisterAccessInfo *ac; + + assert(reg); + + ac = reg->access; + if (!ac || !ac->name) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: read from undefined device state\n", + reg->prefix); + return 0; + } + + ret = reg->data ? register_read_val(reg) : ac->reset; + + if (!reg->read_lite) { + register_write_val(reg, ret & ~ac->cor); + } + + if (ac->post_read) { + ret = ac->post_read(reg, ret); + } + + if (!reg->read_lite) { + if (reg->debug) { + qemu_log("%s:%s: read of value %#" PRIx64 "\n", reg->prefix, + ac->name, ret); + } + } + + return ret; +} + +void register_reset(RegisterInfo *reg) +{ + assert(reg); + + if (!reg->data || !reg->access) { + return; + } + + register_write_val(reg, reg->access->reset); +} diff --git a/include/hw/register.h b/include/hw/register.h new file mode 100644 index 0000000..a80427b --- /dev/null +++ b/include/hw/register.h @@ -0,0 +1,132 @@ +/* + * Register Definition API + * + * Copyright (c) 2013 Xilinx Inc. + * Copyright (c) 2013 Peter Crosthwaite + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#ifndef REGISTER_H +#define REGISTER_H + +#include "exec/memory.h" + +typedef struct RegisterInfo RegisterInfo; +typedef struct RegisterAccessInfo RegisterAccessInfo; + +/** + * A register access error message + * @mask: Bits in the register the error applies to + * @reason: Reason why this access is an error + */ + +typedef struct RegisterAccessError { + uint64_t mask; + const char *reason; +} RegisterAccessError; + +/** + * Access description for a register that is part of guest accessible device + * state. + * + * @name: String name of the register + * @ro: whether or not the bit is read-only + * @w1c: bits with the common write 1 to clear semantic. + * @reset: reset value. + * @cor: Bits that are clear on read + * @rsvd: Bits that are reserved and should not be changed + * + * @ge1: Bits that when written 1 indicate a guest error + * @ge0: Bits that when written 0 indicate a guest error + * @ui1: Bits that when written 1 indicate use of an unimplemented feature + * @ui0: Bits that when written 0 indicate use of an unimplemented feature + * + * @pre_write: Pre write callback. Passed the value that's to be written, + * immediately before the actual write. The returned value is what is written, + * giving the handler a chance to modify the written value. + * @post_write: Post write callback. Passed the written value. Most write side + * effects should be implemented here. + * + * @post_read: Post read callback. Passes the value that is about to be returned + * for a read. The return value from this function is what is ultimately read, + * allowing this function to modify the value before return to the client. + */ + +struct RegisterAccessInfo { + const char *name; + uint64_t ro; + uint64_t w1c; + uint64_t reset; + uint64_t cor; + uint64_t rsvd; + + const RegisterAccessError *ge0; + const RegisterAccessError *ge1; + const RegisterAccessError *ui0; + const RegisterAccessError *ui1; + + uint64_t (*pre_write)(RegisterInfo *reg, uint64_t val); + void (*post_write)(RegisterInfo *reg, uint64_t val); + + uint64_t (*post_read)(RegisterInfo *reg, uint64_t val); +}; + +/** + * A register that is part of guest accessible state + * @data: pointer to the register data. Will be cast + * to the relevant uint type depending on data_size. + * @data_size: Size of the register in bytes. Must be + * 1, 2, 4 or 8 + * + * @access: Access description of this register + * + * @debug: Whether or not verbose debug is enabled + * @prefix: String prefix for log and debug messages + * + * @opaque: Opaque data for the register + */ + +struct RegisterInfo { + /* */ + bool read_lite; + bool write_lite; + + /* */ + void *data; + int data_size; + + const RegisterAccessInfo *access; + + bool debug; + const char *prefix; + + void *opaque; +}; + +/** + * write a value to a register, subject to its restrictions + * @reg: register to write to + * @val: value to write + * @we: write enable mask + */ + +void register_write(RegisterInfo *reg, uint64_t val, uint64_t we); + +/** + * read a value from a register, subject to its restrictions + * @reg: register to read from + * returns: value read + */ + +uint64_t register_read(RegisterInfo *reg); + +/** + * reset a register + * @reg: register to reset + */ + +void register_reset(RegisterInfo *reg); + +#endif