From nobody Fri Nov 14 18:21: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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1761665184; cv=none; d=zohomail.com; s=zohoarc; b=Z0bhaozIqmHzjYP7S2j8UKgBDc1HS+s8U/rejJnKRr/w8BPW4x2upROrAScKVuHAHxwDOatw6R5FqCdepKHm+p+yvAc96p29UQAEh5KQVN4+OcesKtNAIKEafTVjj3p8CfQKvraQM6AXOdJK5T/UhAYPgbhJ9q9Q4HUJJAKu2bg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761665184; 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=veIL5vzROWT+lr9Su8nHxZGlO3ufKltes8yim3GkeBY=; b=j3d/MfVjpAO785g4bbTszuzaEzCzMphrS55mkAl/0YWF6vRlTqivZ2UzTAyuPTMhtuJGN3fyqnv/xwcJVZjsnHd8Wmj7xUF1zfyyIgYR6WW6VNoEv1NV0aCUiE4LHQIJboFptlv6i+ELTc0GmpguU+mVNFPMNV852XB+cAqZEq8= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761665184696415.6339480904321; Tue, 28 Oct 2025 08:26:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vDlYi-0007Za-1c; Tue, 28 Oct 2025 11:24:28 -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 1vDlYe-0007XG-Cf for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:24 -0400 Received: from mail-lj1-x236.google.com ([2a00:1450:4864:20::236]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vDlYV-0001Md-Pm for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:23 -0400 Received: by mail-lj1-x236.google.com with SMTP id 38308e7fff4ca-378d50e1c82so61108601fa.3 for ; Tue, 28 Oct 2025 08:24:08 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-378ee0c625esm26448271fa.29.2025.10.28.08.24.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Oct 2025 08:24:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761665042; x=1762269842; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=veIL5vzROWT+lr9Su8nHxZGlO3ufKltes8yim3GkeBY=; b=NNs3juMAUbXSLxQaDNJlb/P710/vYM0Ct9U7zQAkia5hdkSVzYEuO5ZPa95DjRHqpf S0h/a3SdubouzI9bw1s0+5R6xYx5fa2iL05/d4lTyi6Z6brh9octNtrXOdAXrdPRcBJt pi8WMshO+ngGPEzOKwkGAQI8lYP+IiBEshVaOfZqssUNK/CXD+QvYHGkXhJQfmkwfc9+ BtLiY08XBEiRz3ik8briFfin8Crx6DIU2xnUa63wiSVajWQOvahZH3CkxSDLmuHkVXXN tGhMZZNqPDyQZ4g+XHFtNYTsLO3YbU+vktWJti3n8A9v4zSYsN7w3LAJG408XeZLAelW H/aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761665042; x=1762269842; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=veIL5vzROWT+lr9Su8nHxZGlO3ufKltes8yim3GkeBY=; b=QP/qLaR4JgBX0giT614NiDEoHMGaUqBxuWGM1d3RRtey2oXXV35DU0H1BBhx/1MylH I3gRK6Vvq7Masz/d7jQYuP/XKT0xjWG6R++fuQroWF06qXckHJ33oDDqTralmBSEjolP iLqPrcM5b7qqFKtiLmX+bwpagJjHlqboxs/5qSg/tv7O6U3yuFe2Hvd5n2C3SkkKvxEV Eeiu/fbC8BVLmaR8sIw1sAGyLto24iIUZRs0LvvI7r50deBdrgSFgsTOFqfnWrdGteCM E1bpsoLzEqCagBWIBeYJ7ciE2et5/ioEwP+TXyLk74otebyhkg7c56tsx3GNoNWFmm9O 8QrQ== X-Gm-Message-State: AOJu0YzQgAvN+ftjgAlzzUmp7iJsU3RizCRbjJId1+7oXrQQLAlPwRfa WnVz/FFzUH+veOZuk4RG/Nvs9zxSX3k2Izy2n40ceR8dtXyyuB5sGZA8CP/l8ud9x67QsQ== X-Gm-Gg: ASbGncu94EuJwXGhu9N47HvJyJXLvEnZOo/CLIDxaljnrYm6A7sxhkVSoB/hLcMXLiG STdslKtkT2Pkk40KgAzURDAYZpxC4+O877d1ETe02HC0UUK89Osm4aIvGXPkwKIrbxmC1aLE4on rfEUsrVGVPepq2zP78pW2TJ+JxHouUUwyDslkhBZRuzS3opFHGLBx9iKP1RJcFITrp5bYOyAs2Q uJkMw7mPKtveZBOu3VBCRE6DW476dU/3apAWf8Ga6m0MDmJ0Fw9F7+jgU1cRRgCwB7PLIRH5F2y CwdQ/3QAJ0fEZm4+SCZDLaR1axpZx+UlGURdRj6h7I6b6/QVVqLQY1rLBxVXzZcF2MuwMPVmqLC RbyUHKr3fQrzE6m8WzTD4OAIVNsgpuabJwpxMTSUbqOidjG/dcZdGGUoh+8KAPuYdWHo2Bzv3Xh tUGaJDBIKfLttdvCN0ygTnsyRIhZYJpkQrQoJ15+OM/CHGUcI= X-Google-Smtp-Source: AGHT+IE42aUwC7B+Kswtt3arvxTuEmSc+UwZlHxEm9hFoZBm3M0v0grDcvrpvvfDzF6xf/Jw1S9B8g== X-Received: by 2002:a05:651c:4394:20b0:378:cdf0:ad52 with SMTP id 38308e7fff4ca-379076c6938mr11102181fa.18.1761665041748; Tue, 28 Oct 2025 08:24:01 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org, "Michael S. Tsirkin" Cc: alex.bennee@linaro.org, bill.mills@linaro.org, edgar.iglesias@amd.com Subject: [PATCH v1 1/5] virtio: Introduce notify_queue Date: Tue, 28 Oct 2025 16:23:46 +0100 Message-ID: <20251028152351.1247812-2-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251028152351.1247812-1-edgar.iglesias@gmail.com> References: <20251028152351.1247812-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2a00:1450:4864:20::236; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-x236.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 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 @gmail.com) X-ZM-MESSAGEID: 1761665187778158500 Content-Type: text/plain; charset="utf-8" From: "Edgar E. Iglesias" Signed-off-by: Edgar E. Iglesias --- hw/virtio/virtio.c | 7 +++++++ include/hw/virtio/virtio-bus.h | 1 + 2 files changed, 8 insertions(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 153ee0a0cf..8a53fb5f93 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -2700,12 +2700,19 @@ static void virtio_irq(VirtQueue *vq) =20 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) { + BusState *qbus =3D qdev_get_parent_bus(DEVICE(vdev)); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + WITH_RCU_READ_LOCK_GUARD() { if (!virtio_should_notify(vdev, vq)) { return; } } =20 + if (k->notify_queue) { + k->notify_queue(qbus->parent, virtio_get_queue_index(vq)); + } + trace_virtio_notify(vdev, vq); virtio_irq(vq); } diff --git a/include/hw/virtio/virtio-bus.h b/include/hw/virtio/virtio-bus.h index 7ab8c9dab0..043dbeb4cf 100644 --- a/include/hw/virtio/virtio-bus.h +++ b/include/hw/virtio/virtio-bus.h @@ -39,6 +39,7 @@ DECLARE_OBJ_CHECKERS(VirtioBusState, VirtioBusClass, struct VirtioBusClass { /* This is what a VirtioBus must implement */ BusClass parent; + void (*notify_queue)(DeviceState *d, uint16_t index); void (*notify)(DeviceState *d, uint16_t vector); void (*save_config)(DeviceState *d, QEMUFile *f); void (*save_queue)(DeviceState *d, int n, QEMUFile *f); --=20 2.43.0 From nobody Fri Nov 14 18:21: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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1761665111; cv=none; d=zohomail.com; s=zohoarc; b=Yh3OnTPY+Ou5ctVeWov7QJPlvPGd/miYluIggLuNMUPVQgVqXae3qTZQvOVlldDQRB5VccvCoZfwBCmb8+b2uKhe5d2tnfr/V4CLRU1LUUUaJP30sUGWDzqdPRYzF6hvyl4SqRSav34YkLaj11mAk0qNaBYCJiOh8BoN4S+SCl8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761665111; 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=ptuQ40T8A5qNQ6R3Uee5nOxcUTyPqUa19KlSXchxpcE=; b=WTA15rK38TTjnou4b14z+7r7D1UKNFwJNC821Ydyqjed/VCDgm/q3xWbyE978je9rGeC6ur3GnwYgGyO/exV4vqlUi29dhox1KjYOod0qAqBjvumOLRBB93ZgTcu8kFnPhjE8Po8jHO+ksrJYkgYcg2C2TwzuHUT6IO3JKTlleo= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761665111086119.51564801877566; Tue, 28 Oct 2025 08:25:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vDlYg-0007YR-IT; Tue, 28 Oct 2025 11:24:26 -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 1vDlYd-0007X6-Ps for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:24 -0400 Received: from mail-lj1-x22b.google.com ([2a00:1450:4864:20::22b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vDlYS-0001Mx-GO for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:23 -0400 Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-378d61ce75aso56258521fa.1 for ; Tue, 28 Oct 2025 08:24:08 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-378ee0a56d7sm26973091fa.23.2025.10.28.08.24.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Oct 2025 08:24:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761665044; x=1762269844; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ptuQ40T8A5qNQ6R3Uee5nOxcUTyPqUa19KlSXchxpcE=; b=f0B6g+8iLGxWo/4JMOSCvx+fz9hcPLZSHCmVxJpRYUmPbiziihEnfx+hJVotm/Pzmc yoaivzpIx+TX5AY+GUb3GeFAREUV/uwRXJ1ICRuIuoOf/D0kyr/fmIhrLnfWuexAKdei 7flzmXmK7i+imOeidkvgVgL/c08HJXg+AS7bpppvhesoDgRPdAx+FuFI3S9rvQ48BtKb mWYKI+Cb2Od43p/HwmOohc1KqTEjJP3WSB4R1UgOcRnjAARhfrkOqbhJDqcSd9ig21SB wSXj0PO4iYFhdL57xqtZ2xVJAAJ6OoIqTihb3d3F5FsAX6ubOoGjBQCi8lnQhMZjj+H9 Gmeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761665044; x=1762269844; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ptuQ40T8A5qNQ6R3Uee5nOxcUTyPqUa19KlSXchxpcE=; b=ma3mcaSaQKHC5SzEGjVcJAyPUA4E68nK+aytEkMRX5Va5OuIGRRHZh7xv0EBU+JUjZ CLfIk0vInIKbOpSXNaKP4EDhOEyR6Cb2V7pCORIB6ZdA2zw6FBC7FCiWYMIAlzawg/Ca lVJZzvz4v38i9AQTWrJPseFGuwPw/d1qFeS5o4yJClWobn9MAxKhyEj3WQAJghFLwJvL OSpDy2sxjLBOzALZxkI/bD1+V+rHawILyK6KkYTZGIBo+GHfWTLgsA9h2Mu9YxbO8Ksb xLAHXX9EAqVg5H5N83N27l///ALtW48jafr2fHI0q1wyxVpGyS0EsMk7bkpnie84/s+l xpEg== X-Gm-Message-State: AOJu0YyCSgnh8b3LdbOPlApIP3O5xx9q7gasatm3o82mvxoMj8nCDge1 o0NETtzoN7gtNFzA1GUg9ChljeRgbcQ+gx+S7u1pvQ5LHQtjqccnANIyAC1aIcY92kxZJQ== X-Gm-Gg: ASbGnct0dfHL7WqUEhoNTO7OKrjkhtoWTz50lYQsndM/T8thVc6DEfOxdnIFckQwQ5z kqQz2DAqVe3PKDJyjdl92izTGmYFnzRKwe6m8JHeMcdvNM3oHrfxLsH2pazmc0r7wKdyw+Dxm8D n/jkuk4LHE9ftO6dwW3wHbkA03I98SCS/gfB0cHO1TmzcRsuX3onu0esE6C2C40dLCaHEioRZ0a SVNymZZrVkxhC8sCdZIKqlSL4+ZCvOSLmmJHs29rJQoayq+OWeosj+U24Hsv/1k/x48bA67MxEO lYNyGrE6tSaEqPC0UAbJKx4HoNBw4uWwrT/7CjJCr5JuM/+9vblVc8EKLGmiHAGdjsYCPmEcOZ8 2Tm41CCzPLbHFPX5FceLarMZm++9eSr8UHlywWalauHzlMDbHWAS9/5nWpyZPdhZrbLBQiPt4qb Cenknj6CNcxCegLIE9HM98AUCfjTgcsBol9t8/ X-Google-Smtp-Source: AGHT+IEKS8n26APkvHWTqIFdTjkhqSVWXc7IMJv5y2NEDFMZsgSJEEv0e4HOJAQzTE2HZetz7foyMA== X-Received: by 2002:a05:651c:30e2:b0:376:3a83:4296 with SMTP id 38308e7fff4ca-37907cacd94mr8081551fa.14.1761665043802; Tue, 28 Oct 2025 08:24:03 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org, "Michael S. Tsirkin" Cc: alex.bennee@linaro.org, bill.mills@linaro.org, edgar.iglesias@amd.com Subject: [PATCH v1 2/5] virtio: Add virtio_queue_get_rings Date: Tue, 28 Oct 2025 16:23:47 +0100 Message-ID: <20251028152351.1247812-3-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251028152351.1247812-1-edgar.iglesias@gmail.com> References: <20251028152351.1247812-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2a00:1450:4864:20::22b; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-x22b.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, SPF_HELO_NONE=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 @gmail.com) X-ZM-MESSAGEID: 1761665115049158500 Content-Type: text/plain; charset="utf-8" From: "Edgar E. Iglesias" Signed-off-by: Edgar E. Iglesias --- hw/virtio/virtio.c | 16 ++++++++++++++++ include/hw/virtio/virtio.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 8a53fb5f93..fe7c635390 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -2379,6 +2379,22 @@ void virtio_queue_set_rings(VirtIODevice *vdev, int = n, hwaddr desc, virtio_init_region_cache(vdev, n); } =20 +void virtio_queue_get_rings(VirtIODevice *vdev, int n, hwaddr *desc, + hwaddr *avail, hwaddr *used) +{ + assert(vdev->vq[n].vring.num); + + if (desc) { + *desc =3D vdev->vq[n].vring.desc; + } + if (avail) { + *avail =3D vdev->vq[n].vring.avail; + } + if (used) { + *used =3D vdev->vq[n].vring.used; + } +} + void virtio_queue_set_num(VirtIODevice *vdev, int n, int num) { /* Don't allow guest to flip queue between existent and diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index d97529c3f1..8bceb115a3 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -361,6 +361,8 @@ int virtio_queue_get_max_num(VirtIODevice *vdev, int n); int virtio_get_num_queues(VirtIODevice *vdev); void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc, hwaddr avail, hwaddr used); +void virtio_queue_get_rings(VirtIODevice *vdev, int n, hwaddr *desc, + hwaddr *avail, hwaddr *used); void virtio_queue_update_rings(VirtIODevice *vdev, int n); void virtio_init_region_cache(VirtIODevice *vdev, int n); void virtio_queue_set_align(VirtIODevice *vdev, int n, int align); --=20 2.43.0 From nobody Fri Nov 14 18:21: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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1761665168; cv=none; d=zohomail.com; s=zohoarc; b=krmc/TSpklo0Fk2sImlXfGctL1D//Be4ozKg0NCG6B8f3kCSqBeb/jAlH5RFW7160jfVMTLORC0GoeZRsK05ckrfWdeHvSh4j8E1FMFWraJm1VEJ5eDvjJ3Z6kpXzw5RuDGo6Y29EUuH7a1EfVpkFY5vECzq6ibBWljcsE54TMY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761665168; 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=wIsOX/4l3/jVdB7kFcqrxxVnf99gG5xQHvX6wvWatmQ=; b=YcezAEq7ahG7+jLjXLNma3sHGsv8TFpwJPpjaxmBGojL45teSx6Nr7ZRaLgKOCBigKJakM0u6w/RgrY4b9VUHJgYgZD9wQjh//LahQJ3qGykP2yt9KoN9PaUPLsVJWWoYR5j3gnrEysc1agReVnxoC+PDs2k/OqzaNy20I83gpM= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761665168057763.6703981546084; Tue, 28 Oct 2025 08:26:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vDlYj-0007aA-VT; Tue, 28 Oct 2025 11:24:30 -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 1vDlYh-0007Z1-8R for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:27 -0400 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vDlYV-0001Nk-RW for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:25 -0400 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-592fcf7a1ceso5473672e87.1 for ; Tue, 28 Oct 2025 08:24:11 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59301f50661sm3067968e87.33.2025.10.28.08.24.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Oct 2025 08:24:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761665047; x=1762269847; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wIsOX/4l3/jVdB7kFcqrxxVnf99gG5xQHvX6wvWatmQ=; b=nPoj+eNIF9RgQGjACGGDMpVaSdqQ2dDk4KzZNdn0sNaJCFgj1Eb0hnSGjNFQ3nBP25 wBBsV2fTInGY2nUHJSXdW+dEF8FAlnGtCj/XEgC7ncRrTPzfhMmLpTebZcYXeC0+X9m9 mzo/lnUnNkumGpBEfaNn+U+7ZloRdA/Xsad5Ayh+9Ildc5zftI0dsCJ86KllHiGGQuRD NjJnAnMgQJnK0DmSV7sQoQ3HphAq7mcAg0N0t07VD6FGa64klVcw15Vwjv/jW40XVvcz Xkw5+9WfJLu8rJq2MHPCSLHLspP2mc0QQniuEe1i0dtndT82ZCBs81oFbUjKiaOQy4o0 0tGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761665047; x=1762269847; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wIsOX/4l3/jVdB7kFcqrxxVnf99gG5xQHvX6wvWatmQ=; b=DQNQmPRw4Jr9nLWDVknedtpnWOzdAwtzyhcwHfEptQ7+3MxqQsikLOFiAR/TfY8sTi JNuLnXY9RD/16yEwELJRUIhBfuC4De8PiX7nWWjJiDQbz4MqVWRt3nJ3/hJhis+YhtbP DKRvua9LB9w3ok6ptAjP1qrW3/0qbnbjEONXjI51A7c6CCqt/N8/yAEeinAAGZkA/jCU 7jXjaTTj4cNhCmLr0ZXQLFU5KTXtegtSrA/nhs738mK6CHQMC+oI09PKBAG9Mmq8VmuH 1E+NGP8b8i0H0n4ZmnjvrSk7g/DfyCmg7ho1gHHBb5VWKKy5Nnb8bYgukFd8EIQM39rR 7Yjw== X-Gm-Message-State: AOJu0Yx2R+LDGRBeuNQRaq2QRt8rvrltgxISi/wnytkjn+WA3IA4GOqS jgS2QP+jBh70jEq4XAFKywzJYGOEVjl0kAmzOQRsV87sSTP4CLy3Kh6hJgUM25aoGM7WMA== X-Gm-Gg: ASbGncuDhLp4OgT1BubiX9uL/lkqM91Ztf0ws4stx15W5iNqtIVkLKdA9Mfdy4eueve APoJh7O2/BWDNozDsBzE3txRUZnEz+KKC2RA0r2pCQDQFN6tMSi1qMwaDUBzWSKpmEF2bpUEMqL 2u2gvmekNKRVQxQIw2qpXYFcbTq4zeN1Nh9cBOsjQNZiWbn+JfKeapTsuiBCkmAnzGPUXx6apzU jYksaJArNn03oV6Zq9YD3wq4UoBRhlB/1JmUao7RVm42znhpTrXU+OcMtEdxeF/Iu1pbON5x3Tf +NzYKbkUnbiWIgUzyb0qz48nN3Adwm8vxqFf9VEQBCTLAWnXHpKfKv6xPJdLMZxdWrpIk87+iaV oWjDImzkBsFOXg3Jq387tl7JGU3SPJ6dAaW4rZ+2f7bng8ojdan+mpP2dxvEO7fIeTdB6wFCcdZ m2JLF6sQJBtCLPDyoeXm6I4zlO/AC+YT9ouHj+ X-Google-Smtp-Source: AGHT+IHzW8KpLU/Tnapstgw9lqzXCoWox4BRk8FW7Gx1m+AqjwG6UmCkw/jEiyPU7Oop7a+rxdOHXA== X-Received: by 2002:a05:6512:696:b0:592:fb96:2af8 with SMTP id 2adb3069b0e04-5930e9e6d9dmr1584987e87.51.1761665046521; Tue, 28 Oct 2025 08:24:06 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org, Paolo Bonzini , "Michael S. Tsirkin" Cc: alex.bennee@linaro.org, bill.mills@linaro.org, edgar.iglesias@amd.com Subject: [PATCH v1 3/5] virtio: Add the virtio-msg transport Date: Tue, 28 Oct 2025 16:23:48 +0100 Message-ID: <20251028152351.1247812-4-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251028152351.1247812-1-edgar.iglesias@gmail.com> References: <20251028152351.1247812-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2a00:1450:4864:20::130; envelope-from=edgar.iglesias@gmail.com; helo=mail-lf1-x130.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 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 @gmail.com) X-ZM-MESSAGEID: 1761665169928154100 Content-Type: text/plain; charset="utf-8" From: "Edgar E. Iglesias" Add the new virtio-msg transport together with a framework for virtio-msg buses. Signed-off-by: Edgar E. Iglesias --- hw/virtio/Kconfig | 4 + hw/virtio/meson.build | 5 + hw/virtio/virtio-msg-bus.c | 89 +++ hw/virtio/virtio-msg.c | 695 +++++++++++++++++++++++ include/hw/virtio/virtio-msg-bus.h | 95 ++++ include/hw/virtio/virtio-msg-prot.h | 846 ++++++++++++++++++++++++++++ include/hw/virtio/virtio-msg.h | 56 ++ 7 files changed, 1790 insertions(+) create mode 100644 hw/virtio/virtio-msg-bus.c create mode 100644 hw/virtio/virtio-msg.c create mode 100644 include/hw/virtio/virtio-msg-bus.h create mode 100644 include/hw/virtio/virtio-msg-prot.h create mode 100644 include/hw/virtio/virtio-msg.h diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig index 10f5c53ac0..d2d8c475b2 100644 --- a/hw/virtio/Kconfig +++ b/hw/virtio/Kconfig @@ -26,6 +26,10 @@ config VIRTIO_MMIO bool select VIRTIO =20 +config VIRTIO_MSG + bool + select VIRTIO + config VIRTIO_CCW bool select VIRTIO diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index affd66887d..7944bbb652 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -13,6 +13,11 @@ specific_virtio_ss =3D ss.source_set() specific_virtio_ss.add(files('virtio.c')) specific_virtio_ss.add(files('virtio-qmp.c')) =20 +specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MSG', if_true: files( + 'virtio-msg.c', + 'virtio-msg-bus.c', +)) + if have_vhost system_virtio_ss.add(files('vhost.c')) system_virtio_ss.add(files('vhost-backend.c', 'vhost-iova-tree.c')) diff --git a/hw/virtio/virtio-msg-bus.c b/hw/virtio/virtio-msg-bus.c new file mode 100644 index 0000000000..e907fd64ec --- /dev/null +++ b/hw/virtio/virtio-msg-bus.c @@ -0,0 +1,89 @@ +/* + * VirtIO MSG bus. + * + * Copyright (c) 2024 Advanced Micro Devices, Inc. + * Written by Edgar E. Iglesias + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/units.h" +#include "hw/virtio/virtio-msg-bus.h" + +bool virtio_msg_bus_connect(BusState *bus, + const VirtIOMSGBusPort *port, + void *opaque) +{ + VirtIOMSGBusDevice *bd =3D virtio_msg_bus_get_device(bus); + + if (!bd) { + /* Nothing connected to this virtio-msg device. Ignore. */ + return false; + } + + bd->peer =3D port; + bd->opaque =3D opaque; + return true; +} + +void virtio_msg_bus_process(VirtIOMSGBusDevice *bd) +{ + VirtIOMSGBusDeviceClass *bdc; + bdc =3D VIRTIO_MSG_BUS_DEVICE_CLASS(object_get_class(OBJECT(bd))); + + bdc->process(bd); +} + +int virtio_msg_bus_send(BusState *bus, VirtIOMSG *msg_req) +{ + VirtIOMSGBusDevice *bd =3D virtio_msg_bus_get_device(bus); + VirtIOMSGBusDeviceClass *bdc; + int r =3D VIRTIO_MSG_NO_ERROR; + + bdc =3D VIRTIO_MSG_BUS_DEVICE_CLASS(object_get_class(OBJECT(bd))); + + if (bdc->send) { + r =3D bdc->send(bd, msg_req); + } + return r; +} + +static void virtio_msg_bus_class_init(ObjectClass *klass, const void *data) +{ + BusClass *bc =3D BUS_CLASS(klass); + + bc->max_dev =3D 1; +} + +static const TypeInfo virtio_msg_bus_info =3D { + .name =3D TYPE_VIRTIO_MSG_BUS, + .parent =3D TYPE_BUS, + .instance_size =3D sizeof(BusState), + .class_init =3D virtio_msg_bus_class_init, +}; + +static void virtio_msg_bus_device_class_init(ObjectClass *klass, + const void *data) +{ + DeviceClass *k =3D DEVICE_CLASS(klass); + + k->bus_type =3D TYPE_VIRTIO_MSG_BUS; +} + +static const TypeInfo virtio_msg_bus_device_type_info =3D { + .name =3D TYPE_VIRTIO_MSG_BUS_DEVICE, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(VirtIOMSGBusDevice), + .abstract =3D true, + .class_size =3D sizeof(VirtIOMSGBusDeviceClass), + .class_init =3D virtio_msg_bus_device_class_init, +}; + +static void virtio_msg_bus_register_types(void) +{ + type_register_static(&virtio_msg_bus_info); + type_register_static(&virtio_msg_bus_device_type_info); +} + +type_init(virtio_msg_bus_register_types) diff --git a/hw/virtio/virtio-msg.c b/hw/virtio/virtio-msg.c new file mode 100644 index 0000000000..a84da1714f --- /dev/null +++ b/hw/virtio/virtio-msg.c @@ -0,0 +1,695 @@ +/* + * Virtio MSG bindings + * + * Copyright (c) 2024 Advanced Micro Devices, Inc. + * Written by Edgar E. Iglesias . + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/units.h" +#include "qapi/error.h" +#include "hw/irq.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/virtio.h" +#include "hw/virtio/virtio-msg-bus.h" +#include "hw/virtio/virtio-msg.h" +#include "qemu/error-report.h" +#include "qemu/log.h" +#include "trace.h" + +#define VIRTIO_MSG_VENDOR_ID 0x554D4551 /* 'QEMU' */ + +static bool virtio_msg_bad(VirtIOMSGProxy *s, VirtIOMSG *msg) +{ + bool drop =3D false; + size_t min_size; + unsigned int n; + + min_size =3D virtio_msg_header_size(); + switch (msg->msg_id) { + case VIRTIO_MSG_GET_DEVICE_STATUS: + case VIRTIO_MSG_DEVICE_INFO: + break; + case VIRTIO_MSG_GET_FEATURES: + min_size +=3D sizeof msg->get_features; + break; + case VIRTIO_MSG_SET_FEATURES: + n =3D msg->set_features.num; + + /* We expect at least one feature block. */ + if (n =3D=3D 0 || n > VIRTIO_MSG_MAX_FEATURE_NUM) { + drop =3D true; + break; + } + + min_size +=3D sizeof msg->set_features + n * 4; + break; + case VIRTIO_MSG_GET_CONFIG: + min_size +=3D sizeof msg->get_config; + break; + case VIRTIO_MSG_SET_CONFIG: + if (msg->set_config.size > VIRTIO_MSG_MAX_CONFIG_BYTES) { + drop =3D true; + break; + } + + min_size +=3D sizeof msg->set_config + msg->set_config.size; + break; + case VIRTIO_MSG_SET_DEVICE_STATUS: + min_size +=3D sizeof msg->set_device_status; + break; + case VIRTIO_MSG_GET_VQUEUE: + min_size +=3D sizeof msg->get_vqueue; + break; + case VIRTIO_MSG_SET_VQUEUE: + min_size +=3D sizeof msg->set_vqueue; + break; + case VIRTIO_MSG_RESET_VQUEUE: + min_size +=3D sizeof msg->reset_vqueue; + break; + case VIRTIO_MSG_EVENT_AVAIL: + min_size +=3D sizeof msg->event_avail; + break; + default: + /* Unexpected message. */ + drop =3D true; + break; + } + + /* Accept large messages allowing future backwards compatible extensio= ns. */ + if (drop || + msg->msg_size < min_size || msg->msg_size > VIRTIO_MSG_MAX_SIZE) { + return true; + } + + if (msg->dev_num >=3D ARRAY_SIZE(s->devs)) { + return true; + } + + return false; +} + +static VirtIODevice *virtio_msg_vdev(VirtIOMSGProxy *s, uint16_t dev_num) +{ + VirtIODevice *vdev; + + vdev =3D virtio_bus_get_device(&s->devs[dev_num].bus); + return vdev; +} + +static void virtio_msg_bus_get_devices(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev; + VirtIOMSG msg_resp; + uint8_t data[VIRTIO_MSG_MAX_DEVS / 8] =3D {0}; + uint16_t num =3D MAX(msg->bus_get_devices.num, VIRTIO_MSG_MAX_DEVS); + int i; + + for (i =3D 0; i < ARRAY_SIZE(s->devs); i++) { + vdev =3D virtio_msg_vdev(s, i); + if (vdev) { + data[i / 8] |=3D 1U << (i & 7); + } + } + + virtio_msg_pack_bus_get_devices_resp(&msg_resp, + msg->bus_get_devices.offset, num,= 0, + data); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_device_info(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + uint32_t config_len =3D 0; + uint32_t device_id =3D 0; + VirtIOMSG msg_resp; + + if (vdev) { + device_id =3D vdev->device_id; + config_len =3D vdev->config_len; + } else { + error_report("%s: No virtio device on bus %s!", + __func__, BUS(&s->devs[msg->dev_num].bus)->name); + } + + virtio_msg_pack_get_device_info_resp(&msg_resp, msg->dev_num, msg->tok= en, + device_id, + VIRTIO_MSG_VENDOR_ID, + /* Feature bits */ + 64, + config_len, + VIRTIO_QUEUE_MAX, + 0, 0); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_get_features(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + VirtioDeviceClass *vdc =3D VIRTIO_DEVICE_GET_CLASS(vdev); + VirtIOMSG msg_resp; + uint32_t index =3D msg->get_features.index; + uint32_t f[VIRTIO_MSG_MAX_FEATURE_NUM] =3D { 0 }; + uint32_t num =3D msg->get_features.num; + uint64_t features; + + features =3D vdev->host_features & ~vdc->legacy_features; + + /* We only have 64 feature bits. If driver asks for more, return zeros= */ + if (index < 2) { + features >>=3D index * 32; + f[0] =3D features; + f[1] =3D features >> 32; + } + + /* If index is out of bounds, we respond with num=3D0, f=3D0. */ + virtio_msg_pack_get_features_resp(&msg_resp, msg->dev_num, msg->token, + index, num, f); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_set_features(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIOMSG msg_resp; + unsigned int i; + uint64_t f; + + f =3D s->devs[msg->dev_num].guest_features; + + for (i =3D 0; i < msg->set_features.num; i++) { + unsigned int feature_index =3D i + msg->set_features.index; + + /* We only support up to 64bits */ + if (feature_index >=3D 2) { + break; + } + + f =3D deposit64(f, feature_index * 32, 32, msg->set_features.b32[i= ]); + } + + s->devs[msg->dev_num].guest_features =3D f; + + virtio_msg_pack_set_features_resp(&msg_resp, msg->dev_num, msg->token); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_soft_reset(VirtIOMSGProxy *s, uint16_t dev_num) +{ + assert(dev_num < ARRAY_SIZE(s->devs)); + + virtio_bus_reset(&s->devs[dev_num].bus); + s->devs[dev_num].guest_features =3D 0; +} + +static void virtio_msg_set_device_status(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + uint32_t status =3D msg->set_device_status.status; + VirtIOMSG msg_resp; + + if (!vdev) { + return; + } + + if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { + virtio_bus_stop_ioeventfd(&s->devs[msg->dev_num].bus); + } + + if (status & VIRTIO_CONFIG_S_FEATURES_OK) { + virtio_set_features(vdev, s->devs[msg->dev_num].guest_features); + } + + virtio_set_status(vdev, status); + assert(vdev->status =3D=3D status); + + if (status & VIRTIO_CONFIG_S_DRIVER_OK) { + virtio_bus_start_ioeventfd(&s->devs[msg->dev_num].bus); + } + + if (status =3D=3D 0) { + virtio_msg_soft_reset(s, msg->dev_num); + } + + virtio_msg_pack_set_device_status_resp(&msg_resp, msg->dev_num, msg->t= oken, + vdev->status); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_get_device_status(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + VirtIOMSG msg_resp; + + virtio_msg_pack_get_device_status_resp(&msg_resp, msg->dev_num, msg->t= oken, + vdev->status); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_get_config(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + uint32_t size =3D msg->get_config.size; + uint32_t offset =3D msg->get_config.offset; + uint8_t data[VIRTIO_MSG_MAX_CONFIG_BYTES]; + VirtIOMSG msg_resp; + unsigned int i; + + if (size > VIRTIO_MSG_MAX_CONFIG_BYTES) { + return; + } + + for (i =3D 0; i < size; i++) { + data[i] =3D virtio_config_modern_readb(vdev, offset + i); + } + + virtio_msg_pack_get_config_resp(&msg_resp, msg->dev_num, msg->token, + size, offset, + vdev->generation, data); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_set_config(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + uint32_t offset =3D msg->set_config.offset; + uint32_t size =3D msg->set_config.size; + uint8_t *data =3D msg->set_config.data; + VirtIOMSG msg_resp; + unsigned int i; + + for (i =3D 0; i < size; i++) { + virtio_config_modern_writeb(vdev, offset + i, data[i]); + } + + virtio_msg_pack_set_config_resp(&msg_resp, msg->dev_num, msg->token, + size, offset, + vdev->generation, data); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_get_vqueue(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + uint32_t max_size =3D VIRTQUEUE_MAX_SIZE; + uint32_t index =3D msg->get_vqueue.index; + hwaddr desc, avail, used; + VirtIOMSG msg_resp; + uint32_t size; + + if (index < VIRTIO_QUEUE_MAX) { + size =3D virtio_queue_get_num(vdev, index); + if (!size) { + max_size =3D 0; + } + + virtio_queue_get_rings(vdev, index, &desc, &avail, &used); + virtio_msg_pack_get_vqueue_resp(&msg_resp, msg->dev_num, msg->toke= n, + index, max_size, size, + desc, avail, used); + } else { + /* OOB index, respond with all zeroes. */ + virtio_msg_pack_get_vqueue_resp(&msg_resp, msg->dev_num, msg->toke= n, + index, 0, 0, 0, 0, 0); + } + + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_set_vqueue(VirtIOMSGProxy *s, VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + uint32_t index =3D msg->set_vqueue.index; + VirtIOMSG msg_resp; + + if (index >=3D VIRTIO_QUEUE_MAX) { + /* OOB index, ignore. */ + return; + } + + virtio_queue_set_num(vdev, index, msg->set_vqueue.size); + virtio_queue_set_rings(vdev, index, + msg->set_vqueue.descriptor_addr, + msg->set_vqueue.driver_addr, + msg->set_vqueue.device_addr); + virtio_queue_enable(vdev, index); + + virtio_msg_pack_set_vqueue_resp(&msg_resp, msg->dev_num, msg->token); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_reset_vqueue(VirtIOMSGProxy *s, VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + VirtIOMSG msg_resp; + + virtio_queue_reset(vdev, msg->reset_vqueue.index); + + virtio_msg_pack_reset_vqueue_resp(&msg_resp, msg->dev_num, msg->token); + virtio_msg_bus_send(&s->msg_bus, &msg_resp); +} + +static void virtio_msg_event_avail(VirtIOMSGProxy *s, + VirtIOMSG *msg) +{ + VirtIODevice *vdev =3D virtio_msg_vdev(s, msg->dev_num); + uint16_t vq_idx =3D msg->event_avail.index; + + if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { + VirtIOMSG msg_ev; + + virtio_error(vdev, "Notification while driver not OK?"); + virtio_msg_pack_event_config(&msg_ev, msg->dev_num, + vdev->status, vdev->generation, + 0, 0, NULL); + virtio_msg_bus_send(&s->msg_bus, &msg_ev); + return; + } + + if (vq_idx >=3D VIRTIO_QUEUE_MAX) { + virtio_error(vdev, "Notification to bad VQ!"); + return; + } + + if (!virtio_queue_get_num(vdev, vq_idx)) { + virtio_error(vdev, "Notification to unconfigured VQ!"); + return; + } + + /* NOTIFICATION_DATA doesn't exist in QEMU 8.2.7. if (0) it */ + if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFICATION_DATA) && 0) { + VirtQueue *vq =3D virtio_get_queue(vdev, vq_idx); + uint32_t next_offset_wrap =3D msg->event_avail.next_offset_wrap; + uint16_t qsize =3D virtio_queue_get_num(vdev, vq_idx); + uint32_t offset =3D next_offset_wrap & 0x7fffffff; + bool wrap =3D next_offset_wrap & 0x80000000; + uint16_t data; + + if (offset > 0x7fff || offset >=3D qsize) { + virtio_error(vdev, "Next offset to large!"); + /* Bail out without notification??? */ + return; + } + + data =3D wrap << 15; + data |=3D offset & 0x7fff; + + virtio_queue_set_shadow_avail_idx(vq, data); + } + virtio_queue_notify(vdev, msg->event_avail.index); +} + +typedef void (*VirtIOMSGHandler)(VirtIOMSGProxy *s, + VirtIOMSG *msg); + +static const VirtIOMSGHandler msg_handlers[] =3D { + [VIRTIO_MSG_DEVICE_INFO] =3D virtio_msg_device_info, + [VIRTIO_MSG_GET_FEATURES] =3D virtio_msg_get_features, + [VIRTIO_MSG_SET_FEATURES] =3D virtio_msg_set_features, + [VIRTIO_MSG_GET_DEVICE_STATUS] =3D virtio_msg_get_device_status, + [VIRTIO_MSG_SET_DEVICE_STATUS] =3D virtio_msg_set_device_status, + [VIRTIO_MSG_GET_CONFIG] =3D virtio_msg_get_config, + [VIRTIO_MSG_SET_CONFIG] =3D virtio_msg_set_config, + [VIRTIO_MSG_GET_VQUEUE] =3D virtio_msg_get_vqueue, + [VIRTIO_MSG_SET_VQUEUE] =3D virtio_msg_set_vqueue, + [VIRTIO_MSG_RESET_VQUEUE] =3D virtio_msg_reset_vqueue, + [VIRTIO_MSG_EVENT_AVAIL] =3D virtio_msg_event_avail, +}; + +static int virtio_msg_receive_msg(VirtIOMSGBusDevice *bd, VirtIOMSG *msg) +{ + VirtIOMSGProxy *s =3D VIRTIO_MSG(bd->opaque); + VirtIOMSGHandler handler; + + /* virtio_msg_print(msg); */ + + /* We handle some generic bus messages. */ + if (msg->type & VIRTIO_MSG_TYPE_BUS) { + if (msg->msg_id =3D=3D VIRTIO_MSG_BUS_GET_DEVICES) { + virtio_msg_bus_get_devices(s, msg); + return VIRTIO_MSG_NO_ERROR; + } + } + + if (msg->msg_id > ARRAY_SIZE(msg_handlers)) { + return VIRTIO_MSG_ERROR_UNSUPPORTED_MESSAGE_ID; + } + + handler =3D msg_handlers[msg->msg_id]; + assert((msg->type & VIRTIO_MSG_TYPE_RESPONSE) =3D=3D 0); + + if (virtio_msg_bad(s, msg)) { + /* Drop bad messages. */ + return VIRTIO_MSG_ERROR_BAD_MESSAGE; + } + + if (handler) { + handler(s, msg); + } + + return VIRTIO_MSG_NO_ERROR; +} + +static const VirtIOMSGBusPort virtio_msg_port =3D { + .receive =3D virtio_msg_receive_msg, + .is_driver =3D false +}; + +static void virtio_msg_notify_queue(DeviceState *opaque, uint16_t index) +{ + VirtIOMSGDev *mdev =3D VIRTIO_MSG_DEV(opaque); + VirtIOMSGProxy *s =3D VIRTIO_MSG(mdev->proxy); + VirtIODevice *vdev =3D virtio_msg_vdev(s, mdev->dev_num); + VirtIOMSG msg; + + if (!vdev || !virtio_msg_bus_connected(&s->msg_bus)) { + return; + } + + virtio_msg_pack_event_used(&msg, mdev->dev_num, index); + virtio_msg_bus_send(&s->msg_bus, &msg); +} + +static void virtio_msg_notify(DeviceState *opaque, uint16_t vector) +{ + VirtIOMSGDev *mdev =3D VIRTIO_MSG_DEV(opaque); + VirtIOMSGProxy *s =3D VIRTIO_MSG(mdev->proxy); + VirtIODevice *vdev =3D virtio_msg_vdev(s, mdev->dev_num); + VirtIOMSG msg; + + if (!virtio_msg_bus_connected(&s->msg_bus)) { + return; + } + + /* Check if we're notifying for VQ or CONFIG updates. */ + if (vdev->isr & 2) { + virtio_msg_pack_event_config(&msg, mdev->dev_num, + vdev->status, vdev->generation, + 0, 0, NULL); + virtio_msg_bus_send(&s->msg_bus, &msg); + } +} + +static const VMStateDescription vmstate_virtio_msg_state_sub =3D { + .name =3D "virtio_msg_device", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription vmstate_virtio_msg =3D { + .name =3D "virtio_msg_proxy_backend", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (const VMStateField[]) { + VMSTATE_END_OF_LIST() + }, + .subsections =3D (const VMStateDescription * []) { + &vmstate_virtio_msg_state_sub, + NULL + } +}; + +static void virtio_msg_save_extra_state(DeviceState *opaque, QEMUFile *f) +{ + VirtIOMSGProxy *s =3D VIRTIO_MSG(opaque); + + vmstate_save_state(f, &vmstate_virtio_msg, s, NULL, &error_fatal); +} + +static int virtio_msg_load_extra_state(DeviceState *opaque, QEMUFile *f) +{ + VirtIOMSGProxy *s =3D VIRTIO_MSG(opaque); + + return vmstate_load_state(f, &vmstate_virtio_msg, s, 1, &error_fatal); +} + +static bool virtio_msg_has_extra_state(DeviceState *opaque) +{ + return true; +} + +static void virtio_msg_reset_hold(Object *obj, ResetType type) +{ + VirtIOMSGProxy *s =3D VIRTIO_MSG(obj); + VirtIODevice *vdev; + bool found_a_vdev; + int i; + + for (i =3D 0; i < ARRAY_SIZE(s->devs); i++) { + virtio_msg_soft_reset(s, i); + + vdev =3D virtio_msg_vdev(s, i); + if (vdev) { + found_a_vdev =3D true; + } + } + + /* Only connect transports with virtio-devs. */ + if (found_a_vdev) { + bool r; + + r =3D virtio_msg_bus_connect(&s->msg_bus, &virtio_msg_port, s); + if (!r) { + /* This is a user error, forgetting to setup a msg-bus. */ + error_report("%s: No bus connected!", + object_get_canonical_path(obj)); + exit(EXIT_FAILURE); + } + } +} + +static void virtio_msg_pre_plugged(DeviceState *d, Error **errp) +{ + VirtIOMSGDev *mdev =3D VIRTIO_MSG_DEV(d); + VirtIOMSGProxy *s =3D VIRTIO_MSG(mdev->proxy); + VirtIODevice *vdev =3D virtio_msg_vdev(s, mdev->dev_num); + + virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); +} + +static AddressSpace *virtio_msg_get_dma_as(DeviceState *d) +{ + VirtIOMSGProxy *s =3D VIRTIO_MSG(d); + AddressSpace *as; + + as =3D virtio_msg_bus_get_remote_as(&s->msg_bus); + return as; +} + +static void virtio_msg_realize(DeviceState *d, Error **errp) +{ + VirtIOMSGProxy *s =3D VIRTIO_MSG(d); + Object *o =3D OBJECT(d); + int i; + + for (i =3D 0; i < ARRAY_SIZE(s->devs); i++) { + g_autofree char *outer_bus_name =3D g_strdup_printf("bus%d", i); + + qbus_init(&s->devs_bus[i], sizeof(s->devs_bus[i]), + TYPE_VIRTIO_MSG_OUTER_BUS, d, outer_bus_name); + + object_initialize_child(o, "dev[*]", &s->devs[i], TYPE_VIRTIO_MSG_= DEV); + s->devs[i].proxy =3D s; + s->devs[i].dev_num =3D i; + qdev_realize(DEVICE(&s->devs[i]), BUS(&s->devs_bus[i]), &error_fat= al); + + qbus_init(&s->devs[i].bus, sizeof(s->devs[i].bus), + TYPE_VIRTIO_MSG_PROXY_BUS, DEVICE(&s->devs[i]), "bus"); + } + + qbus_init(&s->msg_bus, sizeof(s->msg_bus), + TYPE_VIRTIO_MSG_BUS, d, "msg-bus"); +} + +static void virtio_msg_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + + dc->realize =3D virtio_msg_realize; + dc->bus_type =3D TYPE_VIRTIO_MSG_OUTER_BUS; + dc->user_creatable =3D true; + rc->phases.hold =3D virtio_msg_reset_hold; +} + +static void virtio_msg_dev_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->bus_type =3D TYPE_VIRTIO_MSG_OUTER_BUS; +} + +static const TypeInfo virtio_msg_types[] =3D { + { + .name =3D TYPE_VIRTIO_MSG, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(VirtIOMSGProxy), + .class_init =3D virtio_msg_class_init, + }, + { + .name =3D TYPE_VIRTIO_MSG_DEV, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(DeviceState), + .class_init =3D virtio_msg_dev_class_init, + }, +}; +DEFINE_TYPES(virtio_msg_types); + +static char *virtio_msg_bus_get_dev_path(DeviceState *dev) +{ + BusState *bus =3D qdev_get_parent_bus(dev); + return strdup(object_get_canonical_path(OBJECT(bus->parent))); +} + +static void virtio_msg_bus_class_init(ObjectClass *klass, const void *data) +{ + BusClass *bus_class =3D BUS_CLASS(klass); + VirtioBusClass *k =3D VIRTIO_BUS_CLASS(klass); + + k->notify_queue =3D virtio_msg_notify_queue; + k->notify =3D virtio_msg_notify; + k->save_extra_state =3D virtio_msg_save_extra_state; + k->load_extra_state =3D virtio_msg_load_extra_state; + k->has_extra_state =3D virtio_msg_has_extra_state; + k->pre_plugged =3D virtio_msg_pre_plugged; + k->has_variable_vring_alignment =3D true; + k->get_dma_as =3D virtio_msg_get_dma_as; + + /* Needed for multiple devs of the same kind (virtio-net). */ + bus_class->get_dev_path =3D virtio_msg_bus_get_dev_path; +} + +static const TypeInfo virtio_msg_bus_types[] =3D { + { + /* Specialized virtio-bus with our custom callbacks. */ + .name =3D TYPE_VIRTIO_MSG_PROXY_BUS, + .parent =3D TYPE_VIRTIO_BUS, + .instance_size =3D sizeof(VirtioBusState), + .class_init =3D virtio_msg_bus_class_init, + }, + { + /* + * Outer bus to hold virtio-msg objects making them visible to the + * qom-tree. + */ + .name =3D TYPE_VIRTIO_MSG_OUTER_BUS, + .parent =3D TYPE_BUS, + .instance_size =3D sizeof(BusState), + }, +}; + +DEFINE_TYPES(virtio_msg_bus_types); diff --git a/include/hw/virtio/virtio-msg-bus.h b/include/hw/virtio/virtio-= msg-bus.h new file mode 100644 index 0000000000..12ef8030f4 --- /dev/null +++ b/include/hw/virtio/virtio-msg-bus.h @@ -0,0 +1,95 @@ +/* + * VirtIO MSG bus. + * + * Copyright (c) 2024 Advanced Micro Devices, Inc. + * Written by Edgar E. Iglesias + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef QEMU_VIRTIO_MSG_BUS_H +#define QEMU_VIRTIO_MSG_BUS_H + +#include "qom/object.h" +#include "system/dma.h" +#include "hw/qdev-core.h" +#include "hw/virtio/virtio-msg-prot.h" + +#define TYPE_VIRTIO_MSG_BUS "virtio-msg-bus" +DECLARE_INSTANCE_CHECKER(BusState, VIRTIO_MSG_BUS, + TYPE_VIRTIO_MSG_BUS) + + +#define TYPE_VIRTIO_MSG_BUS_DEVICE "virtio-msg-bus-device" +OBJECT_DECLARE_TYPE(VirtIOMSGBusDevice, VirtIOMSGBusDeviceClass, + VIRTIO_MSG_BUS_DEVICE) + +typedef struct VirtIOMSGBusPort { + int (*receive)(VirtIOMSGBusDevice *bd, VirtIOMSG *msg); + bool is_driver; +} VirtIOMSGBusPort; + +struct VirtIOMSGBusDeviceClass { + /*< private >*/ + DeviceClass parent_class; + + DeviceRealize parent_realize; + DeviceUnrealize parent_unrealize; + + /* + * Ask the bus to receive and process all messages that + * are readily available. The bus will call the registered + * VirtIOMSGBusPort.receive() function for each message. + * + * Will return immediately if no messages are available. + */ + void (*process)(VirtIOMSGBusDevice *bd); + + /* + * Called by the transport to send a message. + */ + int (*send)(VirtIOMSGBusDevice *bd, VirtIOMSG *msg_req); +}; + +typedef struct VirtIOMSGBusDevice { + DeviceState parent; + + const VirtIOMSGBusPort *peer; + void *opaque; +} VirtIOMSGBusDevice; + +static inline VirtIOMSGBusDevice *virtio_msg_bus_get_device(BusState *qbus) +{ + BusChild *kid =3D QTAILQ_FIRST(&qbus->children); + DeviceState *qdev =3D kid ? kid->child : NULL; + + return (VirtIOMSGBusDevice *)qdev; +} + +static inline bool virtio_msg_bus_connected(BusState *bus) +{ + VirtIOMSGBusDevice *bd =3D virtio_msg_bus_get_device(bus); + + return bd && bd->peer !=3D NULL; +} + +void virtio_msg_bus_process(VirtIOMSGBusDevice *bd); + +bool virtio_msg_bus_connect(BusState *bus, + const VirtIOMSGBusPort *port, + void *opaque); + +static inline void +virtio_msg_bus_receive(VirtIOMSGBusDevice *bd, VirtIOMSG *msg) +{ + virtio_msg_unpack(msg); + bd->peer->receive(bd, msg); +} + +int virtio_msg_bus_send(BusState *bus, VirtIOMSG *msg_req); + +static inline AddressSpace *virtio_msg_bus_get_remote_as(BusState *bus) +{ + return &address_space_memory; +} +#endif diff --git a/include/hw/virtio/virtio-msg-prot.h b/include/hw/virtio/virtio= -msg-prot.h new file mode 100644 index 0000000000..8b3828b2b6 --- /dev/null +++ b/include/hw/virtio/virtio-msg-prot.h @@ -0,0 +1,846 @@ +/* + * Virtio MSG - Message packing/unpacking functions. + * + * Copyright (c) 2024 Advanced Micro Devices, Inc. + * Written by Edgar E. Iglesias . + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef QEMU_VIRTIO_MSG_H +#define QEMU_VIRTIO_MSG_H + +#include +#include "standard-headers/linux/virtio_config.h" + +enum { + VIRTIO_MSG_NO_ERROR =3D 0, + VIRTIO_MSG_ERROR_RETRY, + VIRTIO_MSG_ERROR_TIMEOUT, + VIRTIO_MSG_ERROR_UNSUPPORTED_MESSAGE_ID, + VIRTIO_MSG_ERROR_BAD_MESSAGE, + VIRTIO_MSG_ERROR_MEMORY, /* General memory error. */ +}; + +enum { + VIRTIO_MSG_DEVICE_INFO =3D 0x02, + VIRTIO_MSG_GET_FEATURES =3D 0x03, + VIRTIO_MSG_SET_FEATURES =3D 0x04, + VIRTIO_MSG_GET_CONFIG =3D 0x05, + VIRTIO_MSG_SET_CONFIG =3D 0x06, + VIRTIO_MSG_GET_DEVICE_STATUS =3D 0x07, + VIRTIO_MSG_SET_DEVICE_STATUS =3D 0x08, + VIRTIO_MSG_GET_VQUEUE =3D 0x09, + VIRTIO_MSG_SET_VQUEUE =3D 0x0a, + VIRTIO_MSG_RESET_VQUEUE =3D 0x0b, + VIRTIO_MSG_GET_SHM =3D 0x0c, /* Not yet supported */ + VIRTIO_MSG_EVENT_CONFIG =3D 0x40, + VIRTIO_MSG_EVENT_AVAIL =3D 0x41, + VIRTIO_MSG_EVENT_USED =3D 0x42, + + /* Generic bus messages. */ + VIRTIO_MSG_BUS_GET_DEVICES =3D 0x02, + + VIRTIO_MSG_MAX =3D VIRTIO_MSG_EVENT_USED, +}; + +#define VIRTIO_MSG_MAX_SIZE 48 + +#define VIRTIO_MSG_TYPE_RESPONSE (1 << 0) +#define VIRTIO_MSG_TYPE_BUS (1 << 1) + +typedef struct VirtIOMSG { + uint8_t type; + uint8_t msg_id; + uint16_t dev_num; + uint16_t token; + uint16_t msg_size; + + union { + uint8_t payload_u8[40]; + + struct { + uint32_t device_id; + uint32_t vendor_id; + uint32_t num_feature_bits; + uint32_t config_size; + uint32_t max_vqs; + uint16_t admin_vq_idx; + uint16_t admin_vq_count; + } QEMU_PACKED get_device_info_resp; + struct { + uint32_t index; + uint32_t num; + } QEMU_PACKED get_features; + struct { + uint32_t index; + uint32_t num; + uint32_t b32[]; + } QEMU_PACKED get_features_resp; + struct { + uint32_t index; + uint32_t num; + uint32_t b32[]; + } QEMU_PACKED set_features; + struct { + uint32_t offset; + uint32_t size; + } QEMU_PACKED get_config; + struct { + uint32_t generation; + uint32_t offset; + uint32_t size; + uint8_t data[]; + } QEMU_PACKED get_config_resp; + struct { + uint32_t generation; + uint32_t offset; + uint32_t size; + uint8_t data[]; + } QEMU_PACKED set_config; + struct { + uint32_t generation; + uint32_t offset; + uint32_t size; + uint8_t data[]; + } QEMU_PACKED set_config_resp; + struct { + uint32_t status; + } QEMU_PACKED get_device_status_resp; + struct { + uint32_t status; + } QEMU_PACKED set_device_status; + struct { + uint32_t status; + } QEMU_PACKED set_device_status_resp; + struct { + uint32_t index; + } QEMU_PACKED get_vqueue; + struct { + uint32_t index; + uint32_t max_size; + uint32_t size; + uint32_t reserved; + uint64_t descriptor_addr; + uint64_t driver_addr; + uint64_t device_addr; + } QEMU_PACKED get_vqueue_resp; + struct { + uint32_t index; + uint32_t unused; + uint32_t size; + uint32_t reserved; + uint64_t descriptor_addr; + uint64_t driver_addr; + uint64_t device_addr; + } QEMU_PACKED set_vqueue; + struct { + uint32_t index; + } QEMU_PACKED reset_vqueue; + struct { + uint32_t status; + uint32_t generation; + uint32_t offset; + uint32_t size; + uint8_t config_value[]; + } QEMU_PACKED event_config; + struct { + uint32_t index; + uint32_t next_offset_wrap; + } QEMU_PACKED event_avail; + struct { + uint32_t index; + } QEMU_PACKED event_used; + + /* Generic bus messages. */ + struct { + uint16_t offset; + uint16_t num; + } QEMU_PACKED bus_get_devices; + struct { + uint16_t offset; + uint16_t num; + uint16_t next_offset; + uint8_t data[]; + } QEMU_PACKED bus_get_devices_resp; + }; +} QEMU_PACKED VirtIOMSG; + +/* Maximum number of 32b feature-blocks in a single message. */ +#define VIRTIO_MSG_MAX_FEATURE_NUM \ + ((VIRTIO_MSG_MAX_SIZE - offsetof(VirtIOMSG, get_features_resp.b32)) / = 4) + +/* Maximum amount of config-data in a single message, in bytes. */ +#define VIRTIO_MSG_MAX_CONFIG_BYTES \ + (VIRTIO_MSG_MAX_SIZE - offsetof(VirtIOMSG, set_config.data)) + +#define LE_TO_CPU(v) \ +{ \ + if (sizeof(v) =3D=3D 2) { \ + v =3D le16_to_cpu(v); \ + } else if (sizeof(v) =3D=3D 4) { \ + v =3D le32_to_cpu(v); \ + } else if (sizeof(v) =3D=3D 8) { \ + v =3D le64_to_cpu(v); \ + } else { \ + g_assert_not_reached(); \ + } \ +} + +static inline void virtio_msg_unpack_bus(VirtIOMSG *msg) +{ + switch (msg->msg_id) { + case VIRTIO_MSG_BUS_GET_DEVICES: + LE_TO_CPU(msg->bus_get_devices.offset); + LE_TO_CPU(msg->bus_get_devices.num); + break; + default: + break; + } +} + +/** + * virtio_msg_unpack_resp: Unpacks a wire virtio message responses into + * a host version + * @msg: the virtio message to unpack + * + * See virtio_msg_unpack(). + */ +static inline void virtio_msg_unpack_resp(VirtIOMSG *msg) +{ + int i; + + switch (msg->msg_id) { + case VIRTIO_MSG_DEVICE_INFO: + LE_TO_CPU(msg->get_device_info_resp.device_id); + LE_TO_CPU(msg->get_device_info_resp.vendor_id); + LE_TO_CPU(msg->get_device_info_resp.num_feature_bits); + LE_TO_CPU(msg->get_device_info_resp.config_size); + LE_TO_CPU(msg->get_device_info_resp.max_vqs); + LE_TO_CPU(msg->get_device_info_resp.admin_vq_idx); + LE_TO_CPU(msg->get_device_info_resp.admin_vq_count); + break; + case VIRTIO_MSG_GET_FEATURES: + LE_TO_CPU(msg->get_features_resp.index); + LE_TO_CPU(msg->get_features_resp.num); + for (i =3D 0; i < VIRTIO_MSG_MAX_FEATURE_NUM && + i < msg->get_features_resp.num; i++) { + LE_TO_CPU(msg->get_features_resp.b32[i]); + } + break; + case VIRTIO_MSG_GET_DEVICE_STATUS: + LE_TO_CPU(msg->get_device_status_resp.status); + break; + case VIRTIO_MSG_GET_CONFIG: + LE_TO_CPU(msg->get_config_resp.generation); + LE_TO_CPU(msg->get_config_resp.offset); + LE_TO_CPU(msg->get_config_resp.size); + break; + case VIRTIO_MSG_SET_CONFIG: + LE_TO_CPU(msg->set_config_resp.generation); + LE_TO_CPU(msg->set_config_resp.offset); + LE_TO_CPU(msg->set_config_resp.size); + break; + case VIRTIO_MSG_GET_VQUEUE: + LE_TO_CPU(msg->get_vqueue_resp.index); + LE_TO_CPU(msg->get_vqueue_resp.max_size); + LE_TO_CPU(msg->get_vqueue_resp.size); + LE_TO_CPU(msg->get_vqueue_resp.descriptor_addr); + LE_TO_CPU(msg->get_vqueue_resp.driver_addr); + LE_TO_CPU(msg->get_vqueue_resp.device_addr); + break; + default: + break; + } +} + +/** + * virtio_msg_unpack: Unpacks a wire virtio message into a host version + * @msg: the virtio message to unpack + * + * Virtio messages arriving on the virtio message bus have a specific + * format (little-endian, packet encoding, etc). To simplify for the + * the rest of the implementation, we have packers and unpackers that + * convert the wire messages into host versions. This includes endianess + * conversion and potentially future decoding and expansion. + * + * At the moment, we only do endian conversion, virtio_msg_unpack() should + * get completely eliminated by the compiler when targetting little-endian + * hosts. + */ +static inline void virtio_msg_unpack(VirtIOMSG *msg) +{ + int i; + + LE_TO_CPU(msg->dev_num); + LE_TO_CPU(msg->token); + LE_TO_CPU(msg->msg_size); + + if (msg->type & VIRTIO_MSG_TYPE_BUS) { + virtio_msg_unpack_bus(msg); + return; + } + + if (msg->type & VIRTIO_MSG_TYPE_RESPONSE) { + virtio_msg_unpack_resp(msg); + return; + } + + switch (msg->msg_id) { + case VIRTIO_MSG_GET_FEATURES: + LE_TO_CPU(msg->get_features.index); + LE_TO_CPU(msg->get_features.num); + break; + case VIRTIO_MSG_SET_FEATURES: + LE_TO_CPU(msg->set_features.index); + LE_TO_CPU(msg->set_features.num); + for (i =3D 0; i < VIRTIO_MSG_MAX_FEATURE_NUM && + i < msg->set_features.num; i++) { + LE_TO_CPU(msg->set_features.b32[i]); + } + break; + case VIRTIO_MSG_SET_DEVICE_STATUS: + LE_TO_CPU(msg->set_device_status.status); + break; + case VIRTIO_MSG_GET_CONFIG: + LE_TO_CPU(msg->get_config.offset); + LE_TO_CPU(msg->get_config.size); + break; + case VIRTIO_MSG_SET_CONFIG: + LE_TO_CPU(msg->set_config.generation); + LE_TO_CPU(msg->set_config.offset); + LE_TO_CPU(msg->set_config.size); + break; + case VIRTIO_MSG_GET_VQUEUE: + LE_TO_CPU(msg->get_vqueue.index); + break; + case VIRTIO_MSG_SET_VQUEUE: + LE_TO_CPU(msg->set_vqueue.index); + LE_TO_CPU(msg->set_vqueue.size); + LE_TO_CPU(msg->set_vqueue.descriptor_addr); + LE_TO_CPU(msg->set_vqueue.driver_addr); + LE_TO_CPU(msg->set_vqueue.device_addr); + break; + case VIRTIO_MSG_RESET_VQUEUE: + LE_TO_CPU(msg->reset_vqueue.index); + break; + case VIRTIO_MSG_EVENT_CONFIG: + LE_TO_CPU(msg->event_config.status); + LE_TO_CPU(msg->event_config.generation); + LE_TO_CPU(msg->event_config.offset); + LE_TO_CPU(msg->event_config.size); + break; + case VIRTIO_MSG_EVENT_AVAIL: + LE_TO_CPU(msg->event_avail.index); + LE_TO_CPU(msg->event_avail.next_offset_wrap); + break; + case VIRTIO_MSG_EVENT_USED: + LE_TO_CPU(msg->event_used.index); + break; + default: + break; + } +} + +static inline size_t virtio_msg_header_size(void) +{ + return offsetof(VirtIOMSG, payload_u8); +} + +static inline void virtio_msg_pack_header(VirtIOMSG *msg, + uint8_t msg_id, + uint8_t type, + uint16_t dev_num, + uint16_t token, + uint16_t payload_size) +{ + uint16_t msg_size =3D virtio_msg_header_size() + payload_size; + + msg->type =3D type; + msg->msg_id =3D msg_id; + msg->dev_num =3D cpu_to_le16(dev_num); + msg->token =3D cpu_to_le16(token); + msg->msg_size =3D cpu_to_le16(msg_size); + + /* Keep things predictable. */ + memset(msg->payload_u8, 0, sizeof msg->payload_u8); +} + +static inline void virtio_msg_pack_get_device_info(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_DEVICE_INFO, 0, dev_num, token,= 0); +} + +static inline void virtio_msg_pack_get_device_info_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t device_id, + uint32_t vendor_id, + uint32_t num_feature_bi= ts, + uint32_t config_size, + uint32_t max_vqs, + uint16_t admin_vq_idx, + uint16_t admin_vq_count) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_DEVICE_INFO, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, + sizeof msg->get_device_info_resp); + + msg->get_device_info_resp.device_id =3D cpu_to_le32(device_id); + msg->get_device_info_resp.vendor_id =3D cpu_to_le32(vendor_id); + msg->get_device_info_resp.num_feature_bits =3D cpu_to_le32(num_feature= _bits); + msg->get_device_info_resp.config_size =3D cpu_to_le32(config_size); + msg->get_device_info_resp.max_vqs =3D cpu_to_le32(max_vqs); + msg->get_device_info_resp.admin_vq_idx =3D cpu_to_le16(admin_vq_idx); + msg->get_device_info_resp.admin_vq_count =3D cpu_to_le16(admin_vq_coun= t); +} + +static inline void virtio_msg_pack_get_features(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t index, + uint32_t num) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_FEATURES, 0, dev_num, token, + sizeof msg->get_features); + + msg->get_features.index =3D cpu_to_le32(index); + msg->get_features.num =3D cpu_to_le32(num); +} + +static inline void virtio_msg_pack_get_features_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t index, + uint32_t num, + uint32_t *f) +{ + unsigned int i; + + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_FEATURES, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, + sizeof msg->get_features_resp + num * sizeof(*f= )); + + msg->get_features_resp.index =3D cpu_to_le32(index); + msg->get_features_resp.num =3D cpu_to_le32(num); + + assert(num <=3D VIRTIO_MSG_MAX_FEATURE_NUM); + + for (i =3D 0; i < num && i < VIRTIO_MSG_MAX_FEATURE_NUM; i++) { + msg->get_features_resp.b32[i] =3D cpu_to_le32(f[i]); + } +} + +static inline void virtio_msg_pack_set_features(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t index, + uint32_t num, + uint32_t *f) +{ + unsigned int i; + + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_FEATURES, 0, dev_num, token, + sizeof msg->set_features + num * sizeof(*f)); + + msg->set_features.index =3D cpu_to_le32(index); + msg->set_features.num =3D cpu_to_le32(num); + + assert(num <=3D VIRTIO_MSG_MAX_FEATURE_NUM); + + for (i =3D 0; i < num && i < VIRTIO_MSG_MAX_FEATURE_NUM; i++) { + msg->set_features.b32[i] =3D cpu_to_le32(f[i]); + } +} + +static inline void virtio_msg_pack_set_features_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_FEATURES, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, 0); +} + +static inline void virtio_msg_pack_set_device_status(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t status) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_DEVICE_STATUS, 0, dev_num, + token, sizeof msg->set_device_status); + + msg->set_device_status.status =3D cpu_to_le32(status); +} + +static inline void virtio_msg_pack_set_device_status_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t status) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_DEVICE_STATUS, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, + sizeof msg->set_device_status_resp); + + msg->set_device_status_resp.status =3D cpu_to_le32(status); +} + +static inline void virtio_msg_pack_get_device_status(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_DEVICE_STATUS, 0, + dev_num, token, 0); +} + +static inline void virtio_msg_pack_get_device_status_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t status) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_DEVICE_STATUS, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, + sizeof msg->get_device_status_resp); + + msg->get_device_status_resp.status =3D cpu_to_le32(status); +} + +static inline void virtio_msg_pack_get_config(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t size, + uint32_t offset) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_CONFIG, 0, dev_num, token, + sizeof msg->get_config); + + msg->get_config.offset =3D cpu_to_le32(offset); + msg->get_config.size =3D cpu_to_le32(size); +} + +static inline void virtio_msg_pack_get_config_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t size, + uint32_t offset, + uint32_t generation, + uint8_t data[]) +{ + assert(size <=3D VIRTIO_MSG_MAX_CONFIG_BYTES); + + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_CONFIG, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, + sizeof msg->get_config_resp + size); + + msg->get_config_resp.generation =3D cpu_to_le32(generation); + msg->get_config_resp.offset =3D cpu_to_le32(offset); + msg->get_config_resp.size =3D cpu_to_le32(size); + + memcpy(&msg->get_config_resp.data, data, size); +} + +static inline void virtio_msg_pack_set_config(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t size, + uint32_t offset, + uint32_t generation, + uint8_t data[]) +{ + assert(size <=3D VIRTIO_MSG_MAX_CONFIG_BYTES); + + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_CONFIG, 0, dev_num, token, + sizeof msg->set_config + size); + + msg->set_config.offset =3D cpu_to_le32(offset); + msg->set_config.size =3D cpu_to_le32(size); + msg->set_config.generation =3D cpu_to_le32(generation); + + memcpy(&msg->set_config.data, data, size); +} + +static inline void virtio_msg_pack_set_config_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t size, + uint32_t offset, + uint32_t generation, + uint8_t data[]) +{ + assert(size <=3D VIRTIO_MSG_MAX_CONFIG_BYTES); + + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_CONFIG, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, + sizeof msg->set_config_resp + size); + + msg->set_config_resp.offset =3D cpu_to_le32(offset); + msg->set_config_resp.size =3D cpu_to_le32(size); + msg->set_config_resp.generation =3D cpu_to_le32(generation); + + memcpy(&msg->set_config_resp.data, data, size); +} + +static inline void virtio_msg_pack_get_vqueue(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t index) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_VQUEUE, 0, dev_num, token, + sizeof msg->get_vqueue); + + msg->get_vqueue.index =3D cpu_to_le32(index); +} + +static inline void virtio_msg_pack_get_vqueue_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t index, + uint32_t max_size, + uint32_t size, + uint64_t descriptor_add= r, + uint64_t driver_addr, + uint64_t device_addr) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_GET_VQUEUE, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, + sizeof msg->get_vqueue_resp); + + msg->get_vqueue_resp.index =3D cpu_to_le32(index); + msg->get_vqueue_resp.max_size =3D cpu_to_le32(max_size); + msg->get_vqueue_resp.size =3D cpu_to_le32(size); + msg->get_vqueue_resp.descriptor_addr =3D cpu_to_le64(descriptor_addr); + msg->get_vqueue_resp.driver_addr =3D cpu_to_le64(driver_addr); + msg->get_vqueue_resp.device_addr =3D cpu_to_le64(device_addr); +} + +static inline void virtio_msg_pack_reset_vqueue(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t index) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_RESET_VQUEUE, 0, dev_num, token, + sizeof msg->reset_vqueue); + + msg->reset_vqueue.index =3D cpu_to_le32(index); +} + +static inline void virtio_msg_pack_reset_vqueue_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_RESET_VQUEUE, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, 0); +} + +static inline void virtio_msg_pack_set_vqueue(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token, + uint32_t index, + uint32_t size, + uint64_t descriptor_addr, + uint64_t driver_addr, + uint64_t device_addr) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_VQUEUE, 0, dev_num, token, + sizeof msg->set_vqueue); + + msg->set_vqueue.index =3D cpu_to_le32(index); + msg->set_vqueue.unused =3D 0; + msg->set_vqueue.size =3D cpu_to_le32(size); + msg->set_vqueue.descriptor_addr =3D cpu_to_le64(descriptor_addr); + msg->set_vqueue.driver_addr =3D cpu_to_le64(driver_addr); + msg->set_vqueue.device_addr =3D cpu_to_le64(device_addr); +} + +static inline void virtio_msg_pack_set_vqueue_resp(VirtIOMSG *msg, + uint16_t dev_num, + uint16_t token) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_SET_VQUEUE, + VIRTIO_MSG_TYPE_RESPONSE, dev_num, token, 0); +} + +static inline void virtio_msg_pack_event_avail(VirtIOMSG *msg, + uint16_t dev_num, + uint32_t index, + uint32_t next_offset, + bool next_wrap) +{ + uint32_t next_ow; + + virtio_msg_pack_header(msg, VIRTIO_MSG_EVENT_AVAIL, 0, dev_num, 0, + sizeof msg->event_avail); + + /* next_offset is 31b wide. */ + assert((next_offset & 0x80000000U) =3D=3D 0); + + /* Pack the next_offset_wrap field. */ + next_ow =3D next_wrap << 31; + next_ow |=3D next_offset; + + msg->event_avail.index =3D cpu_to_le32(index); + msg->event_avail.next_offset_wrap =3D cpu_to_le32(next_ow); +} + +static inline void virtio_msg_pack_event_used(VirtIOMSG *msg, + uint16_t dev_num, + uint32_t index) +{ + virtio_msg_pack_header(msg, VIRTIO_MSG_EVENT_USED, 0, dev_num, 0, + sizeof msg->event_used); + + msg->event_used.index =3D cpu_to_le32(index); +} + +static inline void virtio_msg_pack_event_config(VirtIOMSG *msg, + uint16_t dev_num, + uint32_t status, + uint32_t generation, + uint32_t offset, + uint32_t size, + uint8_t *value) +{ + unsigned int max_size; + + virtio_msg_pack_header(msg, VIRTIO_MSG_EVENT_CONFIG, 0, dev_num, 0, + sizeof msg->event_config); + + msg->event_config.status =3D cpu_to_le32(status); + msg->event_config.generation =3D cpu_to_le32(generation); + msg->event_config.offset =3D cpu_to_le32(offset); + msg->event_config.size =3D cpu_to_le32(size); + + max_size =3D VIRTIO_MSG_MAX_SIZE; + max_size -=3D offsetof(VirtIOMSG, event_config.config_value); + assert(size <=3D max_size); + + if (size > 0 && size <=3D max_size) { + memcpy(&msg->event_config.config_value[0], value, size); + } +} + +static inline void virtio_msg_pack_bus_get_devices(VirtIOMSG *msg, + uint16_t offset, + uint16_t num) +{ + virtio_msg_pack_header(msg, + VIRTIO_MSG_BUS_GET_DEVICES, VIRTIO_MSG_TYPE_BUS, + 0, 0, sizeof msg->bus_get_devices); + + msg->bus_get_devices.offset =3D cpu_to_le16(offset); + msg->bus_get_devices.num =3D cpu_to_le16(num); +} + +static inline void virtio_msg_pack_bus_get_devices_resp(VirtIOMSG *msg, + uint16_t offset, + uint16_t num, + uint16_t next_offs= et, + uint8_t *data) +{ + virtio_msg_pack_header(msg, + VIRTIO_MSG_BUS_GET_DEVICES, + VIRTIO_MSG_TYPE_BUS | VIRTIO_MSG_TYPE_RESPONSE, + 0, 0, sizeof msg->bus_get_devices_resp + num); + + msg->bus_get_devices_resp.offset =3D cpu_to_le16(offset); + msg->bus_get_devices_resp.num =3D cpu_to_le16(num); + msg->bus_get_devices_resp.next_offset =3D cpu_to_le16(next_offset); + + memcpy(msg->bus_get_devices_resp.data, data, num / 8); +} + +static inline const char *virtio_msg_id_to_str(unsigned int type) +{ +#define VIRTIO_MSG_TYPE2STR(x) [VIRTIO_MSG_ ## x] =3D stringify(x) + static const char *type2str[VIRTIO_MSG_MAX + 1] =3D { + VIRTIO_MSG_TYPE2STR(DEVICE_INFO), + VIRTIO_MSG_TYPE2STR(GET_FEATURES), + VIRTIO_MSG_TYPE2STR(SET_FEATURES), + VIRTIO_MSG_TYPE2STR(GET_CONFIG), + VIRTIO_MSG_TYPE2STR(SET_CONFIG), + VIRTIO_MSG_TYPE2STR(GET_DEVICE_STATUS), + VIRTIO_MSG_TYPE2STR(SET_DEVICE_STATUS), + VIRTIO_MSG_TYPE2STR(GET_VQUEUE), + VIRTIO_MSG_TYPE2STR(SET_VQUEUE), + VIRTIO_MSG_TYPE2STR(RESET_VQUEUE), + VIRTIO_MSG_TYPE2STR(EVENT_CONFIG), + VIRTIO_MSG_TYPE2STR(EVENT_AVAIL), + VIRTIO_MSG_TYPE2STR(EVENT_USED), + }; + + return type2str[type]; +} + +static inline void virtio_msg_print_status(uint32_t status) +{ + printf("status %x", status); + + if (status & VIRTIO_CONFIG_S_ACKNOWLEDGE) { + printf(" ACKNOWLEDGE"); + } + if (status & VIRTIO_CONFIG_S_DRIVER) { + printf(" DRIVER"); + } + if (status & VIRTIO_CONFIG_S_DRIVER_OK) { + printf(" DRIVER_OK"); + } + if (status & VIRTIO_CONFIG_S_FEATURES_OK) { + printf(" FEATURES_OK"); + } + if (status & VIRTIO_CONFIG_S_NEEDS_RESET) { + printf(" NEEDS_RESET"); + } + if (status & VIRTIO_CONFIG_S_FAILED) { + printf(" FAILED"); + } + + printf("\n"); +} + +static inline void virtio_msg_print(VirtIOMSG *msg) +{ + bool resp =3D msg->type & VIRTIO_MSG_TYPE_RESPONSE; + size_t payload_size; + int i; + + assert(msg); + printf("virtio-msg: id %s 0x%x type 0x%x dev_num 0x%x msg_size 0x%x\n", + virtio_msg_id_to_str(msg->msg_id), msg->msg_id, + msg->type, msg->dev_num, msg->msg_size); + + payload_size =3D msg->msg_size - offsetof(VirtIOMSG, payload_u8); + if (payload_size > ARRAY_SIZE(msg->payload_u8)) { + printf("Size overflow! %zu > %zu\n", + payload_size, ARRAY_SIZE(msg->payload_u8)); + payload_size =3D ARRAY_SIZE(msg->payload_u8); + } + + for (i =3D 0; i < payload_size; i++) { + printf("%2.2x ", msg->payload_u8[i]); + if (((i + 1) % 16) =3D=3D 0) { + printf("\n"); + } + } + + switch (msg->msg_id) { + case VIRTIO_MSG_GET_DEVICE_STATUS: + if (resp) { + virtio_msg_print_status(msg->get_device_status_resp.status); + } + break; + case VIRTIO_MSG_SET_DEVICE_STATUS: + virtio_msg_print_status(msg->set_device_status.status); + break; + case VIRTIO_MSG_SET_VQUEUE: + printf("set-vqueue: index=3D%d size=3D%d desc-addr=3D%lx " + "driver-addr=3D%lx device-addr=3D%lx\n", + msg->set_vqueue.index, msg->set_vqueue.size, + msg->set_vqueue.descriptor_addr, + msg->set_vqueue.driver_addr, + msg->set_vqueue.device_addr); + break; + } + printf("\n"); +} +#endif diff --git a/include/hw/virtio/virtio-msg.h b/include/hw/virtio/virtio-msg.h new file mode 100644 index 0000000000..e92bad84d2 --- /dev/null +++ b/include/hw/virtio/virtio-msg.h @@ -0,0 +1,56 @@ +/* + * Virtio MSG bindings + * + * Copyright (c) 2024 Advanced Micro Devices, Inc. + * Written by Edgar E. Iglesias . + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_VIRTIO_MSG_H +#define HW_VIRTIO_MSG_H + +#include "hw/sysbus.h" +#include "hw/virtio/virtio-bus.h" + +#define TYPE_VIRTIO_MSG_PROXY_BUS "virtio-msg-proxy-bus" +/* This is reusing the VirtioBusState typedef from TYPE_VIRTIO_BUS */ +DECLARE_OBJ_CHECKERS(VirtioBusState, VirtioBusClass, + VIRTIO_MSG_PROXY_BUS, TYPE_VIRTIO_MSG_PROXY_BUS) + +#define TYPE_VIRTIO_MSG_OUTER_BUS "virtio-msg-outer-bus" +OBJECT_DECLARE_SIMPLE_TYPE(BusState, VIRTIO_MSG_OUTER_BUS) + +#define TYPE_VIRTIO_MSG_DEV "virtio-msg-dev" +OBJECT_DECLARE_SIMPLE_TYPE(VirtIOMSGDev, VIRTIO_MSG_DEV) + +#define TYPE_VIRTIO_MSG "virtio-msg" +OBJECT_DECLARE_SIMPLE_TYPE(VirtIOMSGProxy, VIRTIO_MSG) + +struct VirtIOMSGDev { + DeviceState parent_obj; + + /* virtio-bus */ + VirtioBusState bus; + + VirtIOMSGProxy *proxy; + uint16_t dev_num; + uint64_t guest_features; +}; + +#define VIRTIO_MSG_MAX_DEVS 32 +struct VirtIOMSGProxy { + DeviceState parent_obj; + + AddressSpace dma_as; + AddressSpace *bus_as; + IOMMUMemoryRegion mr_iommu; + MemoryRegion *mr_bus; + + BusState devs_bus[VIRTIO_MSG_MAX_DEVS]; + VirtIOMSGDev devs[VIRTIO_MSG_MAX_DEVS]; + + /* virtio-msg-bus. */ + BusState msg_bus; +}; +#endif --=20 2.43.0 From nobody Fri Nov 14 18:21: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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1761665185; cv=none; d=zohomail.com; s=zohoarc; b=RQ5UcFFX9hWc0Hq+YjBTzHRvDddn57ozZ49UQ9D3ni8KVkpKpRk+HCANfAwO2AE0QpkDkmUwFwTumnvRpZKr93oPnBuZiuCQ20kwk8qlXDM6NF6EhS7R2gpnSf4jovEx215aoQq0HO5repq3E6lECslJesAMdfvA5rjJ6UkiqD4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761665185; 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=T3787ZzKPC69u3XicXj5Ql4IT5h8lzUyYkiWykTMcPY=; b=VAsXnhBwzxstBdF+v2xQFSJavrbOL6HyalXRh50y3p8CEDiHsYOiy9n7PG9SmJSyXEueFOCvQNihS9laY+hz+i5QbUPvpLKewSH54aqVyaPmu3Z6SsZ7DL+pigF6DEx2twjk4OBJzQH4dcHLibcI+d77DnJLp2QW10mAc3+nvHU= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761665185702644.1712503694243; Tue, 28 Oct 2025 08:26:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vDlYm-0007bW-0N; Tue, 28 Oct 2025 11:24:32 -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 1vDlYj-0007Zo-4p for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:29 -0400 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vDlYU-0001Nw-5V for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:28 -0400 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-378cffe5e1aso64731761fa.2 for ; Tue, 28 Oct 2025 08:24:13 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59301f74b9bsm3168093e87.88.2025.10.28.08.24.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Oct 2025 08:24:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761665049; x=1762269849; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=T3787ZzKPC69u3XicXj5Ql4IT5h8lzUyYkiWykTMcPY=; b=fGBQlB6AHQr3MCpZxh06C2/v3+TKB4MmzpbA8RQ5aZhtK8lgVOGt2n+yIG7LEcXVQe vaA70ppnw3bwhYo4GOSG6SzXhyFh5DVn4AjO43AfDkj0DMKRhk/rlJsBB28I/nrn5BQr +wZLmE4ugvx4eunX/a9BJt0dBqjI1rM9fYXXmfP67Q+WkLRxjycGMBYoI2bGCkOICP1/ 8CMfqwpVFfRKp3c1c38MSgRhG8pRIAzw05SJvlM+twQ5N0CIML4uM7c8NFnGkWMZqMwt iOjISjUztE9VX/EYa4/dPgRsrUKkHlEyEHqISWN2xFYkyjZBbjRvGzIfYIFSMZ+5gEG2 yeUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761665049; x=1762269849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T3787ZzKPC69u3XicXj5Ql4IT5h8lzUyYkiWykTMcPY=; b=b74ubbYhCEsAAafvvLHUgSmjopZudMSYUjY2ZLm22At7zSJnzvbKTZs9OuJ2uoAe1R 2P6+sTquiyUiyrNd0EPmxR4WZEKdmrbSWpH0pTfxR+zCPOIIYh9EAeS1lweW84jxXb1g GlNkr1Btricleua9ODFH8GuJF6FV9R6fzCzzn2yuavpinXmIwL6NhYQp7AMSaecJpdAZ BNF4eC3ZF8iYv5Aq79grD4+ufI3rMqFQCU0VrPtkMO2OD2Hxx/DdZ2UvcX7zK4ajcNEC xQJQ6/5ECuX4S6Ochxtua2vGdknChDhvpfm+aWIUZksj/Xnimo/TbW5H41gWAIZfylVv 65RA== X-Gm-Message-State: AOJu0Yx2Z3iwx+na5H43GDi4ftfhqHiolCPK12JNzpsWxpP0JLMLA6It 8CTVqTcgdyBaVdkSjn9wvCfxrhF5oxr98uarDGMcB8w+I/qruURqxpH4s0z23p2VGx1Tcw== X-Gm-Gg: ASbGnctbyEdv4lFbwpfID7o/74EEdm/W8b7FtL+08DxHfHEVUpWjBPynoFDTilwVajW 2A1fulFM0ICzl9UJaWuHNa9lWZLwzZ2SmTRw9YQFtWeH3wYtu57I/dRDrMPlq+ridG3lf0xWxH8 T5C3Sur5Ie1VXuYKmXj1Kj0Ukcl0Nq6vhP9jPqCqQKplrXAjCCMKUp9m8UA1jJRJb9+HQEtnPJr KNODxNOXeox5BZTCe101WzckPAkcXwb8LXrUPoMSytyP7jCg3uyQcbN3GjdpCqu2HPKZ/hae+1k E7LK8zWRJPHsMD36mMhD/Y6j/nQpfciVYT4AJVTr/X3yBAaffBE78DWsyJYhq0zC39dbp9ZTlNY nz/4OtZDWjoe3vIn2urvtRPisUZAr4IuwGmbAqapSNm9atljg3Hm+RnnyselFsjTXISWgyfsCPY bpBeZJMydjrQ5pRvfVfdHc78iQtdXsqU8ow1Vj X-Google-Smtp-Source: AGHT+IHZPN9rqoY2eWRZHDoJ+wbh6QDPwiaF3F81fdZH2SiAzZFIN/TvUhK39amc3N2heryFEOl7Iw== X-Received: by 2002:a05:651c:12c5:b0:36f:13c8:d8b1 with SMTP id 38308e7fff4ca-3790769743dmr10745721fa.1.1761665048863; Tue, 28 Oct 2025 08:24:08 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org, Paolo Bonzini , "Michael S. Tsirkin" Cc: alex.bennee@linaro.org, bill.mills@linaro.org, edgar.iglesias@amd.com Subject: [PATCH v1 4/5] hw/misc: Add generic virtio-msg AMP PCI device Date: Tue, 28 Oct 2025 16:23:49 +0100 Message-ID: <20251028152351.1247812-5-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251028152351.1247812-1-edgar.iglesias@gmail.com> References: <20251028152351.1247812-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2a00:1450:4864:20::232; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-x232.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 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 @gmail.com) X-ZM-MESSAGEID: 1761665187952154100 Content-Type: text/plain; charset="utf-8" From: "Edgar E. Iglesias" Signed-off-by: Edgar E. Iglesias --- hw/misc/Kconfig | 7 + hw/misc/meson.build | 1 + hw/misc/virtio-msg-amp-pci.c | 328 +++++++++++++++++++++++++++++++++ include/hw/virtio/spsc_queue.h | 213 +++++++++++++++++++++ 4 files changed, 549 insertions(+) create mode 100644 hw/misc/virtio-msg-amp-pci.c create mode 100644 include/hw/virtio/spsc_queue.h diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index fccd735c24..cdc318bec2 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -25,6 +25,13 @@ config PCI_TESTDEV default y if TEST_DEVICES depends on PCI =20 +config VIRTIO_MSG_AMP_PCI + bool + default y if PCI_DEVICES + depends on PCI + select VIRTIO + select VIRTIO_MSG + config EDU bool default y if TEST_DEVICES diff --git a/hw/misc/meson.build b/hw/misc/meson.build index b1d8d8e5d2..80d4886808 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -8,6 +8,7 @@ system_ss.add(when: 'CONFIG_UNIMP', if_true: files('unimp.c= ')) system_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c')) system_ss.add(when: 'CONFIG_LED', if_true: files('led.c')) system_ss.add(when: 'CONFIG_PVPANIC_COMMON', if_true: files('pvpanic.c')) +system_ss.add(when: 'CONFIG_VIRTIO_MSG_AMP_PCI', if_true: files('virtio-ms= g-amp-pci.c')) =20 # ARM devices system_ss.add(when: 'CONFIG_PL310', if_true: files('arm_l2x0.c')) diff --git a/hw/misc/virtio-msg-amp-pci.c b/hw/misc/virtio-msg-amp-pci.c new file mode 100644 index 0000000000..29fa00d931 --- /dev/null +++ b/hw/misc/virtio-msg-amp-pci.c @@ -0,0 +1,328 @@ +/* + * Model of a virtio-msg AMP capable PCI device. + * + * Copyright (C) 2025 Advanced Micro Devices, Inc. + * Written by Edgar E. Iglesias + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/units.h" +#include "qemu/log.h" + +#include "migration/vmstate.h" +#include "hw/qdev-properties.h" +#include "hw/pci/pci_device.h" +#include "hw/pci/msix.h" +#include "hw/sysbus.h" +#include "hw/register.h" + +#include "hw/virtio/virtio-msg.h" +#include "hw/virtio/virtio-msg-bus.h" +#include "hw/virtio/spsc_queue.h" + +#define TYPE_VMSG_AMP_PCI "virtio-msg-amp-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VmsgAmpPciState, VMSG_AMP_PCI) + +#define TYPE_VMSG_BUS_AMP_PCI "virtio-msg-bus-amp-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VmsgBusAmpPciState, VMSG_BUS_AMP_PCI) +#define VMSG_BUS_AMP_PCI_GET_PARENT_CLASS(obj) \ + OBJECT_GET_PARENT_CLASS(obj, TYPE_VMSG_BUS_AMP_PCI) + +REG32(VERSION, 0x00) +REG32(FEATURES, 0x04) +REG32(NOTIFY, 0x20) + +#define MAX_FIFOS 8 + +typedef struct VmsgBusAmpPciState { + VirtIOMSGBusDevice parent; + PCIDevice *pcidev; + unsigned int queue_index; + + struct { + void *va; + spsc_queue driver; + spsc_queue device; + unsigned int mapcount; + } shm; +} VmsgBusAmpPciState; + +typedef struct VmsgAmpPciState { + PCIDevice dev; + MemoryRegion mr_mmio; + MemoryRegion mr_ram; + + struct fifo_bus { + VmsgBusAmpPciState dev; + VirtIOMSGProxy proxy; + BusState bus; + } fifo[MAX_FIFOS]; + + struct { + uint32_t num_fifos; + } cfg; +} VmsgAmpPciState; + +static void vmsg_bus_amp_pci_process(VirtIOMSGBusDevice *bd); + +static uint64_t vmsg_read(void *opaque, hwaddr addr, unsigned int size) +{ + uint64_t r =3D 0; + + assert(size =3D=3D 4); + + switch (addr) { + case A_VERSION: + /* v0.1 */ + r =3D 0x0001; + break; + case A_FEATURES: + /* No features bit yet. */ + break; + default: + break; + } + + return r; +} + +static void vmsg_write(void *opaque, hwaddr addr, uint64_t val, + unsigned int size) +{ + VmsgAmpPciState *s =3D VMSG_AMP_PCI(opaque); + unsigned int q; + + assert(size =3D=3D 4); + + if (addr >=3D A_NOTIFY) { + q =3D (addr - A_NOTIFY) / 4; + if (q >=3D s->cfg.num_fifos) { + /* Fifo doesn't exist. */ + return; + } + + vmsg_bus_amp_pci_process(VIRTIO_MSG_BUS_DEVICE(&s->fifo[q].dev)); + } else { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: write to read-only reg 0x%" HWADDR_PRIx "\n", + __func__, addr); + } +} + +static const MemoryRegionOps vmsg_pci_ops =3D { + .read =3D vmsg_read, + .write =3D vmsg_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .impl =3D { + .min_access_size =3D 4, + .max_access_size =3D 4, + }, +}; + +static void vmsg_create_bus(VmsgAmpPciState *s, unsigned int i) +{ + DeviceState *dev =3D DEVICE(s); + Object *o =3D OBJECT(s); + struct fifo_bus *fifo =3D &s->fifo[i]; + g_autofree char *fifo_name =3D g_strdup_printf("fifo%d", i); + + qbus_init(&fifo->bus, sizeof(fifo->bus), TYPE_VIRTIO_MSG_OUTER_BUS, + dev, fifo_name); + + /* Create the proxy. */ + object_initialize_child(o, "proxy[*]", &fifo->proxy, TYPE_VIRTIO_MSG); + qdev_realize(DEVICE(&fifo->proxy), BUS(&fifo->bus), &error_fatal); + + object_initialize_child(o, "vmsg[*]", &fifo->dev, + TYPE_VMSG_BUS_AMP_PCI); + qdev_realize(DEVICE(&fifo->dev), &fifo->proxy.msg_bus, &error_fatal); + + msix_vector_use(PCI_DEVICE(s), i); + + /* Caches for quick lookup. */ + fifo->dev.queue_index =3D i; + fifo->dev.pcidev =3D PCI_DEVICE(s); +} + +static void vmsg_amp_pci_realizefn(PCIDevice *dev, Error **errp) +{ + VmsgAmpPciState *s =3D VMSG_AMP_PCI(dev); + int i; + + if (!s->cfg.num_fifos || s->cfg.num_fifos > MAX_FIFOS) { + error_setg(errp, "Unsupported number of FIFOs (%u)", s->cfg.num_fi= fos); + return; + } + + memory_region_init_io(&s->mr_mmio, OBJECT(s), &vmsg_pci_ops, s, + TYPE_VMSG_AMP_PCI, 16 * KiB); + + /* 16KB per FIFO. */ + memory_region_init_ram(&s->mr_ram, OBJECT(s), "ram", + s->cfg.num_fifos * 16 * KiB, &error_fatal); + + pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mr_mmio); + pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY | + PCI_BASE_ADDRESS_MEM_PREFETCH, + &s->mr_ram); + + msix_init_exclusive_bar(PCI_DEVICE(s), s->cfg.num_fifos, 2, &error_fat= al); + for (i =3D 0; i < s->cfg.num_fifos; i++) { + vmsg_create_bus(s, i); + } +} + +static const Property vmsg_properties[] =3D { + DEFINE_PROP_UINT32("num-fifos", VmsgAmpPciState, cfg.num_fifos, 1), +}; + +static const VMStateDescription vmstate_vmsg_pci =3D { + .name =3D TYPE_VMSG_AMP_PCI, + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (const VMStateField[]) { + VMSTATE_PCI_DEVICE(dev, VmsgAmpPciState), + /* TODO: Add all the sub-devs. */ + VMSTATE_END_OF_LIST() + } +}; + +static void vmsg_amp_pci_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + PCIDeviceClass *pc =3D PCI_DEVICE_CLASS(klass); + + device_class_set_props(dc, vmsg_properties); + + pc->realize =3D vmsg_amp_pci_realizefn; + pc->vendor_id =3D PCI_VENDOR_ID_XILINX; + pc->device_id =3D 0x9039; + pc->revision =3D 1; + pc->class_id =3D PCI_CLASS_SYSTEM_OTHER; + dc->vmsd =3D &vmstate_vmsg_pci; + + set_bit(DEVICE_CATEGORY_MISC, dc->categories); +} + +static bool vmsg_bus_amp_pci_map_fifo(VmsgBusAmpPciState *s) +{ + VmsgAmpPciState *pci_s =3D VMSG_AMP_PCI(s->pcidev); + void *va; + + if (s->shm.mapcount) { + s->shm.mapcount++; + return true; + } + + va =3D memory_region_get_ram_ptr(&pci_s->mr_ram); + if (!va) { + return false; + } + + if (!s->shm.driver.shm) { + int capacity =3D spsc_capacity(4 * KiB); + + /* + * Layout: + * 0 - 4KB Reserved + * 4KB - 8KB Driver queue + * 8KB - 12KB Device queue + */ + spsc_init(&s->shm.driver, "driver", capacity, va + 4 * KiB); + spsc_init(&s->shm.device, "device", capacity, va + 8 * KiB); + } + + /* Map queues. */ + s->shm.va =3D va; + s->shm.mapcount++; + return true; +} + +static void vmsg_bus_amp_pci_unmap_fifo(VmsgBusAmpPciState *s) +{ + assert(s->shm.mapcount); + if (--s->shm.mapcount) { + return; + } + + /* TODO: Actually unmap. */ +} + +static void vmsg_bus_amp_pci_process(VirtIOMSGBusDevice *bd) +{ + VmsgBusAmpPciState *s =3D VMSG_BUS_AMP_PCI(bd); + spsc_queue *q; + VirtIOMSG msg; + bool r; + + if (!vmsg_bus_amp_pci_map_fifo(s)) { + return; + } + + /* + * We process the opposite queue, i.e, a driver will want to receive + * messages on the backend queue (and send messages on the driver queu= e). + */ + q =3D bd->peer->is_driver ? &s->shm.device : &s->shm.driver; + do { + r =3D spsc_recv(q, &msg, sizeof msg); + if (r) { + virtio_msg_bus_receive(bd, &msg); + } + } while (r); + vmsg_bus_amp_pci_unmap_fifo(s); +} + +static int vmsg_bus_amp_pci_send(VirtIOMSGBusDevice *bd, VirtIOMSG *msg_re= q) +{ + VmsgAmpPciState *pci_s =3D VMSG_AMP_PCI(OBJECT(bd)->parent); + VmsgBusAmpPciState *s =3D VMSG_BUS_AMP_PCI(bd); + + if (!vmsg_bus_amp_pci_map_fifo(s)) { + return VIRTIO_MSG_ERROR_MEMORY; + } + + spsc_send(&s->shm.device, msg_req, sizeof *msg_req); + + /* Notify. */ + msix_notify(PCI_DEVICE(pci_s), s->queue_index); + + vmsg_bus_amp_pci_unmap_fifo(s); + return VIRTIO_MSG_NO_ERROR; +} + +static void vmsg_bus_amp_pci_class_init(ObjectClass *klass, + const void *data) +{ + VirtIOMSGBusDeviceClass *bdc =3D VIRTIO_MSG_BUS_DEVICE_CLASS(klass); + + bdc->process =3D vmsg_bus_amp_pci_process; + bdc->send =3D vmsg_bus_amp_pci_send; +} + +static const TypeInfo vmsg_pci_info[] =3D { + { + .name =3D TYPE_VMSG_AMP_PCI, + .parent =3D TYPE_PCI_DEVICE, + .instance_size =3D sizeof(VmsgAmpPciState), + .class_init =3D vmsg_amp_pci_class_init, + .interfaces =3D (const InterfaceInfo[]) { + { INTERFACE_CONVENTIONAL_PCI_DEVICE }, + { } + }, + }, { + .name =3D TYPE_VMSG_BUS_AMP_PCI, + .parent =3D TYPE_VIRTIO_MSG_BUS_DEVICE, + .instance_size =3D sizeof(VmsgBusAmpPciState), + .class_init =3D vmsg_bus_amp_pci_class_init, + }, +}; + +static void vmsg_pci_register_types(void) +{ + type_register_static_array(vmsg_pci_info, ARRAY_SIZE(vmsg_pci_info)); +} + +type_init(vmsg_pci_register_types); diff --git a/include/hw/virtio/spsc_queue.h b/include/hw/virtio/spsc_queue.h new file mode 100644 index 0000000000..3d88baab55 --- /dev/null +++ b/include/hw/virtio/spsc_queue.h @@ -0,0 +1,213 @@ +/* + * Hardened and lockless Single Producer Single Consumer Queue implemented + * over shared-memory. + * + * The queue implementation does not look at packet contents, it's up to u= pper + * layers to make sure data is produced and parsed safely. All data is cop= ied + * in/out from/to local private buffers so the peer cannot mess with them = while + * upper layers parse. + * + * The queue is split into a private and a shared part. + * The private part contains cached and sanitized versions of the indexes = that + * indicate our position in the ring-buffer. Peers can corrupt the shared = area + * but have no access to the private area. So whenever we copy from the sh= ared + * area into the private one, we need to sanitize indexes and make sure th= ey + * are within bounds. + * + * A malicious peer can send corrupt data, it can stop receiving or flood = the + * queue causing a sort of denial of service but it can NOT cause our side + * to copy data in or out of buffers outside of the shared memory area. + * + * This implementation expects the SHM area to be cache-coherent or uncach= ed. + * The shared area can be mapped in different ways and our peer may be any= thing + * from another thread on our same OS to an FPGA implementation on a PCI c= ard. + * So local CPU cache-lines sizes, or spin-locks and things that work on a + * single CPU cluster are not used. Instead the implementation sticks to a= tomic + * load/stores of 32b values and to using memory-barriers to guarantee ord= ering. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef SPSC_QUEUE_H__ +#define SPSC_QUEUE_H__ + +#include +#include "qemu/atomic.h" + +#define BUG_ON(x) assert(!(x)) + +#define SPSC_QUEUE_MAX_PACKET_SIZE 64 +/* + * This cache-line size is used to align fields in the hope of + * avoiding cache-line ping-pong:ing. Since the queue layout is + * used across heterogenous CPU clusters and across FPGA/HW implementation= s, + * a fixed size must be used, i.e not the local CPU's cache-line size. + */ +#define SPSC_QUEUE_CACHE_LINE_SIZE 64 + +typedef struct spsc_queue_shared { + uint32_t head __attribute__((__aligned__(SPSC_QUEUE_CACHE_LINE_SIZE))); + uint32_t tail __attribute__((__aligned__(SPSC_QUEUE_CACHE_LINE_SIZE))); + uint32_t packets[][SPSC_QUEUE_MAX_PACKET_SIZE / 4] + __attribute__((__aligned__(SPSC_QUEUE_CACHE_LINE_SIZE))); +} spsc_queue_shared; + +typedef struct spsc_queue { + uint32_t cached_tail; + uint32_t cached_head; + spsc_queue_shared *shm; + const char *name; + unsigned int capacity; +} spsc_queue; + +/* Atomically load and sanitize an index from the SHM area. */ +static inline uint32_t spsc_atomic_load(spsc_queue *q, uint32_t *ptr) +{ + uint32_t val; + + val =3D qatomic_read(ptr); + /* Make sure packet reads are done after reading the index. */ + smp_mb_acquire(); + + /* Bounds check that index is within queue size. */ + if (val >=3D q->capacity) { + val =3D val % q->capacity; + } + + return val; +} + +static inline void spsc_atomic_store(spsc_queue *q, uint32_t *ptr, uint32_= t v) +{ + /* Make sure packet-data gets written before updating the index. */ + smp_mb_release(); + qatomic_set(ptr, v); +} + +/* Returns the capacity of a queue given a specific mapsize. */ +static inline unsigned int spsc_capacity(size_t mapsize) +{ + unsigned int capacity; + spsc_queue *q =3D NULL; + + if (mapsize < sizeof(*q->shm)) { + return 0; + } + + /* Start with the size of the shared area. */ + mapsize -=3D sizeof(*q->shm); + capacity =3D mapsize / sizeof(q->shm->packets[0]); + + if (capacity < 2) { + /* Capacities of less than 2 are invalid. */ + return 0; + } + + return capacity; +} + +static inline size_t spsc_mapsize(unsigned int capacity) +{ + spsc_queue *q =3D NULL; + size_t mapsize; + + BUG_ON(capacity < 2); + + mapsize =3D sizeof(*q->shm); + mapsize +=3D sizeof(q->shm->packets[0]) * capacity; + + return mapsize; +} + +static inline void spsc_init(spsc_queue *q, const char *name, size_t capac= ity, + void *mem) +{ + BUG_ON(!mem); + + /* Initialize private queue area to all zeroes */ + memset(q, 0, sizeof *q); + + q->shm =3D (spsc_queue_shared *) mem; + q->name =3D name; + q->capacity =3D capacity; + + /* In case we're opening a pre-existing queue, pick up where we left o= ff. */ + q->cached_tail =3D spsc_atomic_load(q, &q->shm->tail); + q->cached_head =3D spsc_atomic_load(q, &q->shm->head); +} + +static inline bool spsc_queue_is_full(spsc_queue *q) +{ + uint32_t next_head; + uint32_t head; + + head =3D spsc_atomic_load(q, &q->shm->head); + + next_head =3D head + 1; + if (next_head >=3D q->capacity) { + next_head =3D 0; + } + + if (next_head =3D=3D q->cached_tail) { + q->cached_tail =3D spsc_atomic_load(q, &q->shm->tail); + if (next_head =3D=3D q->cached_tail) { + return true; + } + } + return false; +} + +static inline bool spsc_send(spsc_queue *q, void *buf, size_t size) +{ + uint32_t next_head; + uint32_t head; + + BUG_ON(size > sizeof q->shm->packets[0]); + BUG_ON(size =3D=3D 0); + + /* Is the queue full? */ + if (spsc_queue_is_full(q)) { + return false; + } + + head =3D spsc_atomic_load(q, &q->shm->head); + next_head =3D head + 1; + if (next_head >=3D q->capacity) { + next_head =3D 0; + } + + memcpy(q->shm->packets[head], buf, size); + + spsc_atomic_store(q, &q->shm->head, next_head); + return true; +} + +static inline bool spsc_recv(spsc_queue *q, void *buf, size_t size) +{ + uint32_t tail; + + BUG_ON(size > sizeof q->shm->packets[0]); + BUG_ON(size =3D=3D 0); + + tail =3D spsc_atomic_load(q, &q->shm->tail); + + /* Is the queue empty? */ + if (tail =3D=3D q->cached_head) { + q->cached_head =3D spsc_atomic_load(q, &q->shm->head); + if (tail =3D=3D q->cached_head) { + return false; + } + } + + memcpy(buf, q->shm->packets[tail], size); + + /* Update the read pointer. */ + tail++; + if (tail >=3D q->capacity) { + tail =3D 0; + } + + spsc_atomic_store(q, &q->shm->tail, tail); + return true; +} +#endif /* SPSC_QUEUE_H__ */ --=20 2.43.0 From nobody Fri Nov 14 18:21: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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1761665191; cv=none; d=zohomail.com; s=zohoarc; b=IxHPAeq2FxSXq22hfa1BjKiauvzwUP6XvEkrKZkeZLZ8yQhVTTN8wcnbc2dnLAN3pUT6vDyrzwz2PTJQ+rjwEG/fqA0Pi3cZvaN+z3osXnW+aZw/4XUgzM8WgCITpUH5Fj3L+GVhOkYPtAy/vdArU4lnLFTFxXYkQztRLAdxbfk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761665191; 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=7dLLf9GaBacUfah1oHIjm5nJTHVBfOOXl36JDtbIshE=; b=CkLiU0XWURLGFcchhwTbXBhDpaFUcmBQAD4HS9+QPuDqNCVd/GM3KeIF9C6F+pXL0MSH4FVjmqzrMejQoiYj2fJ3lB1Dg4AYkaR6EFj5vkY1ayxzi7UfHp258j9rDcanro56X0MtIpe8cCZYxxZRhQKEmUTEA7IuvWD4AZcLCwI= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761665191539457.4024462993161; Tue, 28 Oct 2025 08:26:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vDlYm-0007bX-00; Tue, 28 Oct 2025 11:24:32 -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 1vDlYk-0007aE-32 for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:30 -0400 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vDlYZ-0001OP-HA for qemu-devel@nongnu.org; Tue, 28 Oct 2025 11:24:29 -0400 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-59093250aabso6758702e87.2 for ; Tue, 28 Oct 2025 08:24:16 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59301f60af4sm3060403e87.60.2025.10.28.08.24.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Oct 2025 08:24:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761665052; x=1762269852; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7dLLf9GaBacUfah1oHIjm5nJTHVBfOOXl36JDtbIshE=; b=jUVpu9eFO32zN87tLZFTSHTUEQhvtW2Pe88GZJnEkXJP7iX30/Ua3VxFz7jrYlikQu aiVp9UoI+hmPbJTMyk+o3yCTXIhvnyb2IONAZ/nJoiLArkAwDhbvOpSJNCGwvDKsB4L/ UO7zk9m+VLyQq8nJdfWk/MEqAyMze73zQfUbONChYh3M8rLI+25zgZjYidQfELBkPxx7 r0fOdPrxAhXwGvE7dUK58UUZZIwRPbppLi6zErISN/SfhtB4HjincQX+eoIet23mbQ7F xauAuhwXPgl8jJklu53xefO2HNuVetWpd9JrwbecMlgzuOFCXzp2jSjriXNPpSxdu5ro PzUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761665053; x=1762269853; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7dLLf9GaBacUfah1oHIjm5nJTHVBfOOXl36JDtbIshE=; b=PGO3u3+6e9FZuOihRO+mDN0Ijr7LpILNsu4l68zKBZq3mu6eZAI8Vf+IgQPm63ebVx OF/WhVfR/Q01FIjiv8CI8/j58BOc1Z4ogZnF2HfwlLCaJFV5Yn9H57bjb/ksr4H/PQ+A 9N5ZvJIw6/kM15QXNKO61ssOyhEosILa7mG562WUoQM95Oo4PUEJZip5TYDml5Lam7ir 9FFyfsMB35XUVs6IaEwia2eT8UMDAnLDQd+5beNIJuSL3uZl8/zb4pnSsav7JfLoOdU4 Wp7Vf1WmmUGD82IfYZZfTNjQitsucGARIO7ZPMLJhbgrEVX5+yojSHdOzE/4Mmlcj3g+ otCQ== X-Gm-Message-State: AOJu0YxVG4SNe0zh79AvfLZ8ePCH77rCbr7+d+X5jiQfovqPaDA30slp NKvq6kbD146KAXoozm89iCUH85r11JLvvw00pg9/0PFdkwdbdg0XbaistsFZl8WVKz80tw== X-Gm-Gg: ASbGncu5rblMKepZ2vj/BXe1L8SszU6rEuRSckCLaSNU9ZCp87eesYhpY6xNWMTiBKu CQYVkUmV83ISK7+kjJiXjNG73y8Alfa/Ae6+wRIDvSg3GuHodDBBmV+pHeNZ+qWoHPKokVb/Qn8 2F+WNUqqWBoFPtxKY8aQe+3DGq6GnzOPBECoKPIU53TR+jNy+BnFGPWNJcxmP8Y0hYshipVxZAA ve5/8h85hjFPL1xJL5elErl6+sjXvRaE43CDhPaX2nZ5TcW7IEqtrlSCU4YAaLBpXhqHlSlQaIg Fc0KnGtDB6CQLtRgyKgnZe2njRDeAn2P4O9CmvjIoACxyGb7QsA/t/TNyoSrvgQ5Z1zwXbpF1lV nmotN/kh36CpWdptuTwM9xrZoIRqHh5zSH7sa4idtkE4NDlZx64BePaI9s0aEnYiR5oa3t2WZEG ERzen9hJGNKPQPMYKbXjSIlIo1Ht1Q6MGSd48D X-Google-Smtp-Source: AGHT+IHmc55O4jIX3hnbINKfeyWtRYKvJrYoXmT+ygMAnR475KdDN8y3H/tz1IS+6x7qbm64VGF3QQ== X-Received: by 2002:a05:6512:4027:b0:592:f766:a49c with SMTP id 2adb3069b0e04-5930e997932mr1297028e87.15.1761665052300; Tue, 28 Oct 2025 08:24:12 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: mst@redhat.com, alex.bennee@linaro.org, bill.mills@linaro.org, edgar.iglesias@amd.com Subject: [PATCH v1 5/5] docs: Describe virtio-msg-amp-pci Date: Tue, 28 Oct 2025 16:23:50 +0100 Message-ID: <20251028152351.1247812-6-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251028152351.1247812-1-edgar.iglesias@gmail.com> References: <20251028152351.1247812-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2a00:1450:4864:20::131; envelope-from=edgar.iglesias@gmail.com; helo=mail-lf1-x131.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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 @gmail.com) X-ZM-MESSAGEID: 1761665191844158500 Content-Type: text/plain; charset="utf-8" From: "Edgar E. Iglesias" Signed-off-by: Edgar E. Iglesias --- .../devices/virtio/virtio-msg-amp-pci.rst | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 docs/system/devices/virtio/virtio-msg-amp-pci.rst diff --git a/docs/system/devices/virtio/virtio-msg-amp-pci.rst b/docs/syste= m/devices/virtio/virtio-msg-amp-pci.rst new file mode 100644 index 0000000000..c73f7a878b --- /dev/null +++ b/docs/system/devices/virtio/virtio-msg-amp-pci.rst @@ -0,0 +1,70 @@ +Virtio-msg AMP PCI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +This document explains the setup and usage of the virtio-msg-amp-pci devic= e.. +The virtio-msg-amp-pci, is an emulated PCI device that provides a small +set of features to enable virtio-msg over shared-memory queue's. + +Usecase +------- + +Virtio-msg is a virtio transport where driver and device communicate over +messages rather than using memory accesses that get trapped and emulated. +Virtio-msg depends on a lower lever virtio-msg-bus responsible for deliver= ing +these messages. In this case, we're using the Virtio-msg AMP bus which mov= es +messages back and forth using a FIFO on top of shared-memory and interrupt= s. + +The virtio-msg-amp-pci device exposes a BAR with RAM and doorbell registers +so guests can implement the shared-memory FIFO protocol and QEMU implements +the backend side of it. + +Virtio pmem allows to bypass the guest page cache and directly use +host page cache. This reduces guest memory footprint as the host can +make efficient memory reclaim decisions under memory pressure. + +Virtio-msg-amp-pci PCI device +----------------------------- + +The virtio-msg-amp-pci device has the following layout: + +- BAR 0: Registers (Version, features and notification/doorbell regs) +- BAR 1: RAM for FIFOs + +Each FIFO gets an MSI-X interrupt reserved for it and a dedicated doorbell +register:: + + REG32(VERSION, 0x00) + REG32(FEATURES, 0x04) + REG32(NOTIFY0, 0x20) + REG32(NOTIFY1, 0x24) + REG32(NOTIFY2, 0x28) + And so on. + +How does virtio-msg-amp-pci compare to virtio-pci emulation? +------------------------------------------------------------ + +Both virtio-msg-amp-pci and virtio-pci emulate PCI devices and allow users +to plug virtio devices behind them. The main difference is in how the +guest uses virtio-msg vs virtio-pci to discover and configure the virtio d= ev. + +virtio pmem usage +----------------- + +A virtio-msg-amp-pci can be greated by adding the following to the QEMU +command-line:: + + -device virtio-msg-amp-pci + +Virtio devices can then be attached to the virtio-msg bus with for example +the following:: + + -device virtio-rng-device,bus=3D/gpex-pcihost/pcie.0/virtio-msg-amp-pc= i/fifo0/virtio-msg/bus0/virtio-msg-dev + +Multiple virtio devices can be connected by using bus1, bus2 and so on. + +Device properties +----------------- + +The virtio-msg-amp-pci device can be configured with the following proper= ties: + + * ``num-fifos`` number of fifos (default 2). --=20 2.43.0