From nobody Sat Nov 15 10:42:25 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1752828907; cv=none; d=zohomail.com; s=zohoarc; b=i4ohG9eeVCdWAdSnjIr1ALb/gfRapxDoAbM9aGgXUs9qFPvAXjDCR/PqG+S2nmXLi98VOiy1a5OhSjbTUEucTPKlsOKoYVeEV5yGzIgtRA16WUoobKEfXO+co419ehKkTXAklQBDXOO6BFKtsKX5SN6voXGrB6IDKIBkFD626tU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1752828907; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Q3TQfhpyStXbt0pfAiVguwtvMOM41Sx4QrHJWtkUaKM=; b=B26sjMO9LSZdQrh0LKS/CoiOkf+qO1wdt+ryYgGbtNh5dWt0YTMjQes3raH8f/GcQL7Ap/FgrY8+LzZhgPGwRuG/UafTmEbJBAEtanNWfAFLExHwJ339zELLhXPS5OZe2uMbM6kz/T09koOiIMcL3eLiZDqoM9PhW5DskSHHY4A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1752828907589664.5356527069961; Fri, 18 Jul 2025 01:55:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ucgqZ-0001Gi-KY; Fri, 18 Jul 2025 04:53:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ucgqW-0001CG-PN for qemu-devel@nongnu.org; Fri, 18 Jul 2025 04:53:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ucgqU-00018p-UQ for qemu-devel@nongnu.org; Fri, 18 Jul 2025 04:53:36 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-467-SdNu9AuyPyKxe6cM2wQOkw-1; Fri, 18 Jul 2025 04:53:30 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3F503180028C; Fri, 18 Jul 2025 08:53:28 +0000 (UTC) Received: from gerbillo.redhat.com (unknown [10.44.33.19]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EB37B1956089; Fri, 18 Jul 2025 08:53:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752828814; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q3TQfhpyStXbt0pfAiVguwtvMOM41Sx4QrHJWtkUaKM=; b=SyjricbQnkrqGrmdxc6b/UyRDybNky1FZAvXD61reezu0j+Ojhy4t6/Vzkp0sAY5k+kYJ7 vSzh6MDvfy2+wyy5x6wNOEKcvB6MNcZCSf6l2eSy4BXyLCsEXvqs9uExM4p1aBHEfCAEcu ZKfONd1J7wUd7nWeVn1IDDfilJQOAlQ= X-MC-Unique: SdNu9AuyPyKxe6cM2wQOkw-1 X-Mimecast-MFC-AGG-ID: SdNu9AuyPyKxe6cM2wQOkw_1752828808 From: Paolo Abeni To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Dmitry Fleytman , Akihiko Odaki , Jason Wang , Sriram Yagnaraman , "Michael S. Tsirkin" , Stefano Garzarella , Cornelia Huck , Luigi Rizzo , Giuseppe Lettieri , Vincenzo Maffione , Eric Blake , Markus Armbruster Subject: [PATCH RFC v3 03/13] virtio: introduce extended features type Date: Fri, 18 Jul 2025 10:52:29 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pabeni@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1752828910176116600 Content-Type: text/plain; charset="utf-8" The virtio specifications allows for up to 128 bits for the device features. Soon we are going to use some of the 'extended' bits features (bit 64 and above) for the virtio net driver. Represent the virtio features bitmask with a fixes size array, and introduce a few helpers to help manipulate them. Most drivers will keep using only 64 bits features space: use union to allow them access the lower part of the extended space without any per driver change. Signed-off-by: Paolo Abeni --- v2 -> v3: - fix preprocessor guard name - use BIT_ULL - add missing parentheses - use memcmp() - _is_empty() -> _empty() - _andnot() returns a bool, true if dst has any non zero bits - _array -> _ex v1 -> v2: - use a fixed size array for features instead of uint128 - use union with u64 to reduce the needed code churn --- include/hw/virtio/virtio-features.h | 123 ++++++++++++++++++++++++++++ include/hw/virtio/virtio.h | 7 +- 2 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 include/hw/virtio/virtio-features.h diff --git a/include/hw/virtio/virtio-features.h b/include/hw/virtio/virtio= -features.h new file mode 100644 index 0000000000..68e326e3e8 --- /dev/null +++ b/include/hw/virtio/virtio-features.h @@ -0,0 +1,123 @@ +/* + * Virtio features helpers + * + * Copyright 2025 Red Hat, Inc. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef QEMU_VIRTIO_FEATURES_H +#define QEMU_VIRTIO_FEATURES_H + +#include "qemu/bitops.h" + +#define VIRTIO_FEATURES_FMT "%016"PRIx64"%016"PRIx64 +#define VIRTIO_FEATURES_PR(f) (f)[1], (f)[0] + +#define VIRTIO_FEATURES_MAX 128 +#define VIRTIO_BIT(b) BIT_ULL((b) % 64) +#define VIRTIO_DWORD(b) ((b) >> 6) +#define VIRTIO_FEATURES_WORDS (VIRTIO_FEATURES_MAX >> 5) +#define VIRTIO_FEATURES_DWORDS (VIRTIO_FEATURES_WORDS >> 1) + +#define VIRTIO_DECLARE_FEATURES(name) \ + union { \ + uint64_t name; \ + uint64_t name##_ex[VIRTIO_FEATURES_DWORDS]; \ + } + +static inline void virtio_features_clear(uint64_t *features) +{ + memset(features, 0, sizeof(features[0]) * VIRTIO_FEATURES_DWORDS); +} + +static inline void virtio_features_from_u64(uint64_t *features, uint64_t f= rom) +{ + virtio_features_clear(features); + features[0] =3D from; +} + +static inline bool virtio_has_feature_ex(const uint64_t *features, + unsigned int fbit) +{ + assert(fbit < VIRTIO_FEATURES_MAX); + return features[VIRTIO_DWORD(fbit)] & VIRTIO_BIT(fbit); +} + +static inline void virtio_add_feature_ex(uint64_t *features, + unsigned int fbit) +{ + assert(fbit < VIRTIO_FEATURES_MAX); + features[VIRTIO_DWORD(fbit)] |=3D VIRTIO_BIT(fbit); +} + +static inline void virtio_clear_feature_ex(uint64_t *features, + unsigned int fbit) +{ + assert(fbit < VIRTIO_FEATURES_MAX); + features[VIRTIO_DWORD(fbit)] &=3D ~VIRTIO_BIT(fbit); +} + +static inline bool virtio_features_equal(const uint64_t *f1, + const uint64_t *f2) +{ + return !memcmp(f1, f2, sizeof(uint64_t) * VIRTIO_FEATURES_DWORDS); +} + +static inline bool virtio_features_use_extended(const uint64_t *features) +{ + int i; + + for (i =3D 1; i < VIRTIO_FEATURES_DWORDS; ++i) { + if (features[i]) { + return true; + } + } + return false; +} + +static inline bool virtio_features_empty(const uint64_t *features) +{ + return !virtio_features_use_extended(features) && !features[0]; +} + +static inline void virtio_features_copy(uint64_t *to, const uint64_t *from) +{ + memcpy(to, from, sizeof(to[0]) * VIRTIO_FEATURES_DWORDS); +} + +static inline bool virtio_features_andnot(uint64_t *to, const uint64_t *f1, + const uint64_t *f2) +{ + uint64_t diff =3D 0; + int i; + + for (i =3D 0; i < VIRTIO_FEATURES_DWORDS; i++) { + to[i] =3D f1[i] & ~f2[i]; + diff |=3D to[i]; + } + return diff; +} + +static inline void virtio_features_and(uint64_t *to, const uint64_t *f1, + const uint64_t *f2) +{ + int i; + + for (i =3D 0; i < VIRTIO_FEATURES_DWORDS; i++) { + to[i] =3D f1[i] & f2[i]; + } +} + +static inline void virtio_features_or(uint64_t *to, const uint64_t *f1, + const uint64_t *f2) +{ + int i; + + for (i =3D 0; i < VIRTIO_FEATURES_DWORDS; i++) { + to[i] =3D f1[i] | f2[i]; + } +} + +#endif + diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index c594764f23..39e4059a66 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -16,6 +16,7 @@ =20 #include "system/memory.h" #include "hw/qdev-core.h" +#include "hw/virtio/virtio-features.h" #include "net/net.h" #include "migration/vmstate.h" #include "qemu/event_notifier.h" @@ -121,9 +122,9 @@ struct VirtIODevice * backend (e.g. vhost) and could potentially be a subset of the * total feature set offered by QEMU. */ - uint64_t host_features; - uint64_t guest_features; - uint64_t backend_features; + VIRTIO_DECLARE_FEATURES(host_features); + VIRTIO_DECLARE_FEATURES(guest_features); + VIRTIO_DECLARE_FEATURES(backend_features); =20 size_t config_len; void *config; --=20 2.50.0