From nobody Sat Nov 15 08:31:44 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1754059199; cv=none; d=zohomail.com; s=zohoarc; b=WUfBDF92MTaIV7WCf/2wbYnkr/zBHAUiH9ELw/VIPUTyTyx3Xsb7ukwP3GwPPNeHQ44SPLGCQHfikJW6hd2xF6OgMVVg/eFMUm/IR/aSUYu9wBKRJWbkIF0oiG0k7zCZ59++HjPSGxCWrl/NlAq5J5RwOdCdsJx1zBMWe0TMY4k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1754059199; h=Content-Type: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=XA9J84IWz65/jFGEF4PHg1aGXOZdSUvhbvxiqHCyUZw=; b=GhZ1wccDhyVY/k7xyvV+1nyXQjsTVsgxkF+OAS43EDB/87mIZ0fZ/+v5fc4/BoEVahC3x9/csw1FmbU/EpBy7vQCODAVw3xsmGooIQrKSvxavrNTjKwOI4nlX2gikhGfmr7zT+pM1DEcE+n1s7kWVXFEFRzOYv3g121QSegmg/I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1754059199570179.88960404367572; Fri, 1 Aug 2025 07:39:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uhqnc-0007aP-ON; Fri, 01 Aug 2025 10:31:56 -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 1uhqhS-0005VV-Bl for qemu-devel@nongnu.org; Fri, 01 Aug 2025 10:25:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uhqhQ-0002n4-HE for qemu-devel@nongnu.org; Fri, 01 Aug 2025 10:25:34 -0400 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-179--xl1FdWtPei5eWUysydeBg-1; Fri, 01 Aug 2025 10:25:30 -0400 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4560f28b2b1so6780045e9.2 for ; Fri, 01 Aug 2025 07:25:30 -0700 (PDT) Received: from redhat.com ([2a0d:6fc0:1515:7300:62e6:253a:2a96:5e3]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4589536bc34sm106134815e9.2.2025.08.01.07.25.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Aug 2025 07:25:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1754058331; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=XA9J84IWz65/jFGEF4PHg1aGXOZdSUvhbvxiqHCyUZw=; b=M0AA6d/FovoeRlY0ZUbxqevcyZmC21aSfqBTa74H7jR7Po3UKCHsmbgEeFFqV+F2qmcxuA 1lQLqxgr0HD5Cwf3vrIp5dyU4LdIdRBATXt9U0C3Tmr6OG6dEwXGdAz8tR3afmby0Qv00W IRQ3V5SOm1nKBuKbWvrgmyhTU1yxJQY= X-MC-Unique: -xl1FdWtPei5eWUysydeBg-1 X-Mimecast-MFC-AGG-ID: -xl1FdWtPei5eWUysydeBg_1754058329 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754058329; x=1754663129; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XA9J84IWz65/jFGEF4PHg1aGXOZdSUvhbvxiqHCyUZw=; b=WVms4Q+C/KptirinTi2hWAg05OhLheagb82SD0ySg0cToRn4vCJR28uMVxUBLkT+7A SHXZKRmWtyGLuiVIOhNRXCQayRxCwmRGoeVdl5UaupmkFv+aSXlBxF/+LkD0LVUYBhrv 1IF+9MVrJEb4vEk/4NeJmSvVIsIk5eyWjoLgYM8eBf42kmIRa+aVjBzb3Eljh1Q0yxcp SBJGTf3rOSMQFgis1txlxfoKcJP2YykSPqvR5dprGUAWKtRWeE+rDNnxP/mfb4nWtpfC jCuBDxE4AR/tmq4GokeUxz49h+1RD+4ev+b8X9aZWCCJVAP0+ByyA7QDIpr+o5PP+I6a 430g== X-Gm-Message-State: AOJu0YwIYYr3zIKjdn1fuollE/W4tNaX1HicQZ7x/OLPmQrQIEpUK4e4 1o30Zv9vhRlXku31c9PU5Tjf2PYokq8Tvmw11L4QL/xZW8KtrSc+SDoqSriK4myJbYs7L+Nxcmg liGBv/i6rt3WZkBXbHdsmQ2jIGBHjGBuweUzxFhYoRkpFopup+QZJd4I/w8dWZSdj9EBXsaIr9D +Pj2B7j0/vhHxGJowOwM7sGh0KWPKKuzgitQ== X-Gm-Gg: ASbGncsB9BxSkuHte8eWlGAJaUGDQd3KEgOufOKK9lsDMZLFHCeb8lmXZQLJvjqT3Iw 5FzK+txJWzDfbFQUmx0e9Hbdw1XzJSwW9FKTdwQJhTO26cU/I1j1YiXKFwFKg/qHsyCqqgKhpjM tZoT3c2nvo+JLWnAJfmfSCpajnyevQhKJh53bZWSgg6jWGzTevFz4LoVPEM+swxeAYS5QZEyg03 c9ydiw+9oAo/k8GFmLK8iYaJsnuxBwasGO1hClIk0FcxbqPgoZqVGMtXNyXfp8vh1rHj6wvoGjt iWzsSUV3FsZxFEP5xPd9fFxg+VirCXzA X-Received: by 2002:a05:600c:c059:20b0:44b:eb56:1d48 with SMTP id 5b1f17b1804b1-458930a2ea5mr72424285e9.4.1754058328945; Fri, 01 Aug 2025 07:25:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGgFsLBip16NgLIYRqdPDAuPi03g4j+/ctXE9W03ABHfPZZlwshXYt704MMRORcjGcfvL40Fg== X-Received: by 2002:a05:600c:c059:20b0:44b:eb56:1d48 with SMTP id 5b1f17b1804b1-458930a2ea5mr72424015e9.4.1754058328447; Fri, 01 Aug 2025 07:25:28 -0700 (PDT) Date: Fri, 1 Aug 2025 10:25:26 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Sairaj Kodilkar , Vasant Hegde , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Marcel Apfelbaum Subject: [PULL 11/17] hw/i386/amd_iommu: Fix event log generation Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1754059202370116600 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sairaj Kodilkar Current event logging code is broken, because of following issues 1. The code uses '|' instead of '&' to test the bit field, which causes vIOMMU to generate overflow interrupt for every log entry. 2. Code does not update the eventlog tail MMIO register after adding an entry to the buffer, because of which guest cannot process new entries (as head =3D=3D tail means buffer is empty). 3. Compares eventlog tail (which is byte offset in the buffer) to eventlog length (which is number of maximum entries in the buffer). This causes vIOMMU to generate only fix number of event logs, after which it keeps on generating overflow interrupts, without actually resetting the log buffer. 4. Updates ComWaitInt instead of EventLogInt bitfield in Status register. Guest checks this field to see if there are new event log entries in the buffer. 5. Does not reset event log head and tail pointers when guest writes to eventlog base register. Fix above issues, so that guest can process event log entries. Fixes: d29a09ca68428 ("hw/i386: Introduce AMD IOMMU") Signed-off-by: Sairaj Kodilkar Reviewed-by: Vasant Hegde Message-Id: <20250801060507.3382-7-sarunkod@amd.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/i386/amd_iommu.h | 1 + hw/i386/amd_iommu.c | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h index 67078c6f1e..2476296c49 100644 --- a/hw/i386/amd_iommu.h +++ b/hw/i386/amd_iommu.h @@ -111,6 +111,7 @@ #define AMDVI_MMIO_STATUS_CMDBUF_RUN (1 << 4) #define AMDVI_MMIO_STATUS_EVT_RUN (1 << 3) #define AMDVI_MMIO_STATUS_COMP_INT (1 << 2) +#define AMDVI_MMIO_STATUS_EVENT_INT (1 << 1) #define AMDVI_MMIO_STATUS_EVT_OVF (1 << 0) =20 #define AMDVI_CMDBUF_ID_BYTE 0x07 diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c index 6925085d29..26be69bec8 100644 --- a/hw/i386/amd_iommu.c +++ b/hw/i386/amd_iommu.c @@ -160,10 +160,10 @@ static void amdvi_writeq(AMDVIState *s, hwaddr addr, = uint64_t val) (oldval_preserved | newval_write) & ~newval_w1c_set); } =20 -/* OR a 64-bit register with a 64-bit value */ +/* AND a 64-bit register with a 64-bit value */ static bool amdvi_test_mask(AMDVIState *s, hwaddr addr, uint64_t val) { - return amdvi_readq(s, addr) | val; + return amdvi_readq(s, addr) & val; } =20 /* OR a 64-bit register with a 64-bit value storing result in the register= */ @@ -192,19 +192,31 @@ static void amdvi_generate_msi_interrupt(AMDVIState *= s) } } =20 +static uint32_t get_next_eventlog_entry(AMDVIState *s) +{ + uint32_t evtlog_size =3D s->evtlog_len * AMDVI_EVENT_LEN; + return (s->evtlog_tail + AMDVI_EVENT_LEN) % evtlog_size; +} + static void amdvi_log_event(AMDVIState *s, uint64_t *evt) { + uint32_t evtlog_tail_next; + /* event logging not enabled */ if (!s->evtlog_enabled || amdvi_test_mask(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_OVF)) { return; } =20 + evtlog_tail_next =3D get_next_eventlog_entry(s); + /* event log buffer full */ - if (s->evtlog_tail >=3D s->evtlog_len) { - amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_OVF); - /* generate interrupt */ - amdvi_generate_msi_interrupt(s); + if (evtlog_tail_next =3D=3D s->evtlog_head) { + /* generate overflow interrupt */ + if (s->evtlog_intr) { + amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_O= VF); + amdvi_generate_msi_interrupt(s); + } return; } =20 @@ -213,9 +225,13 @@ static void amdvi_log_event(AMDVIState *s, uint64_t *e= vt) trace_amdvi_evntlog_fail(s->evtlog, s->evtlog_tail); } =20 - s->evtlog_tail +=3D AMDVI_EVENT_LEN; - amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_COMP_INT); - amdvi_generate_msi_interrupt(s); + s->evtlog_tail =3D evtlog_tail_next; + amdvi_writeq_raw(s, AMDVI_MMIO_EVENT_TAIL, s->evtlog_tail); + + if (s->evtlog_intr) { + amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVENT_INT= ); + amdvi_generate_msi_interrupt(s); + } } =20 static void amdvi_setevent_bits(uint64_t *buffer, uint64_t value, int star= t, @@ -731,9 +747,19 @@ static inline void amdvi_handle_excllim_write(AMDVISta= te *s) static inline void amdvi_handle_evtbase_write(AMDVIState *s) { uint64_t val =3D amdvi_readq(s, AMDVI_MMIO_EVENT_BASE); + + if (amdvi_readq(s, AMDVI_MMIO_STATUS) & AMDVI_MMIO_STATUS_EVENT_INT) + /* Do not reset if eventlog interrupt bit is set*/ + return; + s->evtlog =3D val & AMDVI_MMIO_EVTLOG_BASE_MASK; s->evtlog_len =3D 1UL << (amdvi_readq(s, AMDVI_MMIO_EVTLOG_SIZE_BYTE) & AMDVI_MMIO_EVTLOG_SIZE_MASK); + + /* clear tail and head pointer to 0 when event base is updated */ + s->evtlog_tail =3D s->evtlog_head =3D 0; + amdvi_writeq_raw(s, AMDVI_MMIO_EVENT_HEAD, s->evtlog_head); + amdvi_writeq_raw(s, AMDVI_MMIO_EVENT_TAIL, s->evtlog_tail); } =20 static inline void amdvi_handle_evttail_write(AMDVIState *s) --=20 MST