From patchwork Mon Dec 21 15:26:49 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 41556 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 6E0A0B6EF4 for ; Tue, 22 Dec 2009 02:31:46 +1100 (EST) Received: from localhost ([127.0.0.1]:36754 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NMkEZ-0007xW-D6 for incoming@patchwork.ozlabs.org; Mon, 21 Dec 2009 10:31:43 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NMkCa-000780-OM for qemu-devel@nongnu.org; Mon, 21 Dec 2009 10:29:40 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NMkCW-00076c-5W for qemu-devel@nongnu.org; Mon, 21 Dec 2009 10:29:40 -0500 Received: from [199.232.76.173] (port=34607 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NMkCW-00076X-2a for qemu-devel@nongnu.org; Mon, 21 Dec 2009 10:29:36 -0500 Received: from mx1.redhat.com ([209.132.183.28]:31253) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NMkCV-0007ei-Il for qemu-devel@nongnu.org; Mon, 21 Dec 2009 10:29:35 -0500 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id nBLFTX9G025172 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 21 Dec 2009 10:29:34 -0500 Received: from redhat.com (vpn-6-201.tlv.redhat.com [10.35.6.201]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id nBLFTVGc031188; Mon, 21 Dec 2009 10:29:32 -0500 Date: Mon, 21 Dec 2009 17:26:49 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org, Anthony Liguori , kraxel@redhat.com Message-ID: <20091221152649.GB10095@redhat.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.19 (2009-01-05) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Subject: [Qemu-devel] [PATCHv2-RFC 1/2] qdev: add bit property type X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This adds "bit" property type, which is a boolean stored in a 32 bit integer field, with legal values on and off. Will be used by virtio for feature bits. Signed-off-by: Michael S. Tsirkin --- hw/qdev-properties.c | 70 +++++++++++++++++++++++++++++++++++++++++++++----- hw/qdev.h | 9 ++++++ 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index fb07279..3782609 100644 --- a/hw/qdev-properties.c +++ b/hw/qdev-properties.c @@ -9,6 +9,67 @@ void *qdev_get_prop_ptr(DeviceState *dev, Property *prop) return ptr; } +static void *qdev_bit_prop_ptr(DeviceState *dev, Property *prop) +{ + void *ptr = dev; + assert(prop->info->type == PROP_TYPE_BIT); + ptr += prop->offset / 32; + return ptr; +} + +static uint32_t qdev_bit_prop_mask(Property *prop) +{ + assert(prop->info->type == PROP_TYPE_BIT); + return 0x1 << (prop->offset % 32); +} + +static void bit_prop_set(DeviceState *dev, Property *props, bool val) +{ + uint32_t *p = qdev_bit_prop_ptr(dev, props); + uint32_t mask = qdev_bit_prop_mask(props); + if (val) + *p |= ~mask; + else + *p &= ~mask; +} + +static void qdev_prop_cpy(DeviceState *dev, Property *props, void *src) +{ + if (props->info->type == PROP_TYPE_BIT) { + bool *defval = src; + bit_prop_set(dev, props, *defval); + } else { + char *dst = qdev_get_prop_ptr(dev, props); + memcpy(dst, src, props->info->size); + } +} + +/* Bit */ +static int parse_bit(DeviceState *dev, Property *prop, const char *str) +{ + if (!strncasecmp(str, "on", 2)) + bit_prop_set(dev, prop, true); + else if (!strncasecmp(str, "off", 3)) + bit_prop_set(dev, prop, false); + else + return -1; + return 0; +} + +static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len) +{ + uint8_t *p = qdev_bit_prop_ptr(dev, prop); + return snprintf(dest, len, (*p & qdev_bit_prop_mask(prop)) ? "on" : "off"); +} + +PropertyInfo qdev_prop_bit = { + .name = "on/off", + .type = PROP_TYPE_BIT, + .size = sizeof(uint32_t), + .parse = parse_bit, + .print = print_bit, +}; + /* --- 8bit integer --- */ static int parse_uint8(DeviceState *dev, Property *prop, const char *str) @@ -506,7 +567,6 @@ int qdev_prop_parse(DeviceState *dev, const char *name, const char *value) void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type) { Property *prop; - void *dst; prop = qdev_prop_find(dev, name); if (!prop) { @@ -519,8 +579,7 @@ void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyT __FUNCTION__, dev->info->name, name); abort(); } - dst = qdev_get_prop_ptr(dev, prop); - memcpy(dst, src, prop->info->size); + qdev_prop_cpy(dev, prop, src); } void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value) @@ -580,14 +639,11 @@ void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value) void qdev_prop_set_defaults(DeviceState *dev, Property *props) { - char *dst; - if (!props) return; while (props->name) { if (props->defval) { - dst = qdev_get_prop_ptr(dev, props); - memcpy(dst, props->defval, props->info->size); + qdev_prop_cpy(dev, props, props->defval); } props++; } diff --git a/hw/qdev.h b/hw/qdev.h index bbcdba1..26853e0 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -82,6 +82,7 @@ enum PropertyType { PROP_TYPE_NETDEV, PROP_TYPE_VLAN, PROP_TYPE_PTR, + PROP_TYPE_BIT, }; struct PropertyInfo { @@ -173,6 +174,7 @@ void do_device_del(Monitor *mon, const QDict *qdict); /*** qdev-properties.c ***/ +extern PropertyInfo qdev_prop_bit; extern PropertyInfo qdev_prop_uint8; extern PropertyInfo qdev_prop_uint16; extern PropertyInfo qdev_prop_uint32; @@ -202,6 +204,13 @@ extern PropertyInfo qdev_prop_pci_devfn; + type_check(_type,typeof_field(_state, _field)), \ .defval = (_type[]) { _defval }, \ } +#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \ + .name = (_name), \ + .info = &(qdev_prop_bit), \ + .offset = offsetof(_state, _field) * 32 + (_bit) \ + + type_check(uint32_t,typeof_field(_state, _field)), \ + .defval = (bool[]) { (_defval) }, \ + } #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)