From patchwork Mon May 1 02:02:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 13227359 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9F518C7EE23 for ; Mon, 1 May 2023 02:03:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ptIsA-0003pb-14; Sun, 30 Apr 2023 22:02:38 -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 1ptIs6-0003p8-Gz for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:37 -0400 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ptIs3-0003Ek-Q7 for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:33 -0400 Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-4edcc885d8fso2507385e87.1 for ; Sun, 30 Apr 2023 19:02:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1682906550; x=1685498550; 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=Uu05r/+K3irfDIVs7fuNdmkBiFU8/kzPnfrcMrQcERY=; b=Z4iV/adCxTqovc3jUUQpRrj6SxHjxVJsC7xhMSUNFu7JIfUmwHCppfdGKC5Ihs6T3b ng3Bx40r+hJNPFuaagJx9GeF5ZHmvJ24AkxqpaI4vSptaFZ7hqCNEENKL7FKk6hH149s bNMTZiRqf2tqWr0W+X0XtMYj5dpngK2a/eT+VYEizTnqYvLEePGZqfxVsSLhRbw1QWAi Er/J/Tnoq8s24GFzqfs6K7yfNXpfq2oUJmK0ryVKu4roigp/dOIJlu8noSEJ5J0q+Kao 2yZcdF2mXN83rFmBxg0VLWwpZvnNarBLTK7oLi/UOWXgLPaElL0jCYI4JyRRBN+1v4JD edBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682906550; x=1685498550; 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=Uu05r/+K3irfDIVs7fuNdmkBiFU8/kzPnfrcMrQcERY=; b=cFPVlLUa5LuAMhqKJiziZLBv4XSXYYEMnnacVj4cc+sOiQ24KkNlwC5rNMtjcyIVTr AfVIBAwMdGcI4y/eAcGmA1sFj7SdvSktyCYBnJmQ0cFYibVwPMfAbACjxfwWTN+StaJH Rkd4GwA//ds72tZsT0tPE3+tw3KR+oerGaxsXpXU9XL688GesCuusdbWnI1azz9q/vIj yO+gT0Gw3nahk2c+QBKaz+F/J94fpAsH38pROD90+w4H38184/LQwjvRAgKYERAe19VS i6TuCnT/dk/YO/6Edsym2afYaOYjHm7QZzx+CFG6xlWaqJMd8N1/Z6TQExlek2GmEDpt vKkQ== X-Gm-Message-State: AC+VfDy/kdGSTdlr3FUVjnt8KvwKcdSR/M/4Er1vI4SGWQW5grDj9WYe bVGqbjIeVV1OrfBdgz6os7EHyg== X-Google-Smtp-Source: ACHHUZ51URhgEBgB1Z5BziqDBmOb0w3F3USll/JoB4mbowkntLDJTrI9vgeUOslBSmoLfrxTsBr/ZQ== X-Received: by 2002:ac2:446f:0:b0:4dd:9fd8:3a36 with SMTP id y15-20020ac2446f000000b004dd9fd83a36mr3246542lfl.1.1682906549953; Sun, 30 Apr 2023 19:02:29 -0700 (PDT) Received: from vp-pc.. (109-252-127-135.nat.spd-mgts.ru. [109.252.127.135]) by smtp.gmail.com with ESMTPSA id w7-20020ac254a7000000b004efe73ee01fsm3783271lfk.306.2023.04.30.19.02.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Apr 2023 19:02:29 -0700 (PDT) From: Viktor Prutyanov To: mst@redhat.com, jasowang@redhat.com, peterx@redhat.com, marcel.apfelbaum@gmail.com Cc: qemu-devel@nongnu.org, viktor@daynix.com, yan@daynix.com, yuri.benditovich@daynix.com Subject: [RFC PATCH v2 1/4] pci: add handling of Enable bit in ATS Control Register Date: Mon, 1 May 2023 05:02:18 +0300 Message-Id: <20230501020221.188376-2-viktor@daynix.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230501020221.188376-1-viktor@daynix.com> References: <20230501020221.188376-1-viktor@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2a00:1450:4864:20::129; envelope-from=viktor@daynix.com; helo=mail-lf1-x129.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org According to PCIe Address Translation Services specification 5.1.3., ATS Control Register has Enable bit to enable/disable ATS. A trigger function is called at the Enable bit change, so that PCIe devices can handle ATS enable/disable. Signed-off-by: Viktor Prutyanov --- hw/pci/pcie.c | 22 ++++++++++++++++++++++ include/hw/pci/pcie.h | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/hw/pci/pcie.c b/hw/pci/pcie.c index b8c24cf45f..14ac1b0fb9 100644 --- a/hw/pci/pcie.c +++ b/hw/pci/pcie.c @@ -1063,6 +1063,28 @@ void pcie_ats_init(PCIDevice *dev, uint16_t offset, bool aligned) pci_set_word(dev->wmask + dev->exp.ats_cap + PCI_ATS_CTRL, 0x800f); } +void pcie_ats_config_write(PCIDevice *dev, uint32_t address, + uint32_t val, int len, + void (*trigger_func)(PCIDevice *dev, bool enable)) +{ + uint32_t off; + uint16_t ats_cap = dev->exp.ats_cap; + + if (!ats_cap || address < ats_cap) { + return; + } + off = address - ats_cap; + if (off >= PCI_EXT_CAP_ATS_SIZEOF) { + return; + } + + if (range_covers_byte(off, len, PCI_ATS_CTRL + 1)) { + if (trigger_func) { + trigger_func(dev, !!(val & PCI_ATS_CTRL_ENABLE)); + } + } +} + /* ACS (Access Control Services) */ void pcie_acs_init(PCIDevice *dev, uint16_t offset) { diff --git a/include/hw/pci/pcie.h b/include/hw/pci/pcie.h index 3cc2b15957..f5571527d3 100644 --- a/include/hw/pci/pcie.h +++ b/include/hw/pci/pcie.h @@ -146,4 +146,9 @@ void pcie_cap_slot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp); void pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp); + +void pcie_ats_config_write(PCIDevice *dev, uint32_t address, + uint32_t val, int len, + void (*trigger_func)(PCIDevice *dev, bool enable)); + #endif /* QEMU_PCIE_H */ From patchwork Mon May 1 02:02:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 13227358 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8BA60C77B60 for ; Mon, 1 May 2023 02:03:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ptIsL-0003rN-Pm; Sun, 30 Apr 2023 22:02:49 -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 1ptIsJ-0003qz-Uk for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:48 -0400 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ptIs4-0003Eu-4I for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:47 -0400 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-4ec81773cf7so2428014e87.2 for ; Sun, 30 Apr 2023 19:02:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1682906550; x=1685498550; 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=WiA/euj4PFXBRIwa4M7EpDNxjGMJvdT31Ee4wXwhwlA=; b=eUDOJwOvptzIkhSKcOaZe0JfwRYZWJ3+e+2ha2Ykyaulbv0np04oVk9LydGaljfScJ l/0lBDQ/Nf1gwRLmsBXF37aBfDKl3bb+E7QOv5vWq4TirhMSUDyiJ9lgWv1kdS82wCDz /lj+E2ToSkVW+qsrRefAnPSbG8/IsKufQE6hBBJEYFsJtyeL7XH1iWoNnSdIBonOhTUP ve4elyjIBHmRsV/b7BmvJUFlGRVWK7ySNArn8XbFSKXdt4+bRSs3KBOJoX5Zw/aYWxxl 9H5b6PkX7fAhljqzdjQcdvwLbfnfOdyRrzIUrZgKArh9K9Tw+Aq3l6Ai166onk/A3sQ6 cesQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682906550; x=1685498550; 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=WiA/euj4PFXBRIwa4M7EpDNxjGMJvdT31Ee4wXwhwlA=; b=kTgmcc5K+Py+nR9wAKRTMh5a4XxYwy/1veaITgUOGMtJemkndmvyBYajaz+Vmkqa0H gEtTc0yOObnq+BzSiYWsJ4n7MKC5+oGEjAkngelemqGBDnu08tnhSfTeUVFoZbuyjFr/ luEfjrsxSNeJjKG6YZD8QDbW8YEkA+YJ5EaxcSpWIPA1cbmxy2tUkTywLK34tLzd2bdh CCMRbIB0+4g5URLwk3w9iaGK3UAkOXp0MJ+oC+SvPnJ1kKtXpFcAD/m1SDm+im25e2eG kr6Nu7mPyRgFrWUS/5l9N2oFLVf2cSVZGUPtZhcfIOIPdED9L2ywAfUC89hBYOvW5RKo UNyw== X-Gm-Message-State: AC+VfDzy6sHnG6kip/OrTzpaClo43Q7bMHbiqT9SAmTPyBR5bpE6hhYB BDsamsmROzMuDqD7bTLyrRibbISH8YwffSsmSLt55Q== X-Google-Smtp-Source: ACHHUZ7RrjBVxeiPnTBHmSCCLMfrXxm/8TnBZ0vXSriedUL3e/jPW6RvfN0do8qBW9XDI9H/K5jKMA== X-Received: by 2002:ac2:599b:0:b0:4f1:1e31:96a9 with SMTP id w27-20020ac2599b000000b004f11e3196a9mr341106lfn.51.1682906550658; Sun, 30 Apr 2023 19:02:30 -0700 (PDT) Received: from vp-pc.. (109-252-127-135.nat.spd-mgts.ru. [109.252.127.135]) by smtp.gmail.com with ESMTPSA id w7-20020ac254a7000000b004efe73ee01fsm3783271lfk.306.2023.04.30.19.02.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Apr 2023 19:02:30 -0700 (PDT) From: Viktor Prutyanov To: mst@redhat.com, jasowang@redhat.com, peterx@redhat.com, marcel.apfelbaum@gmail.com Cc: qemu-devel@nongnu.org, viktor@daynix.com, yan@daynix.com, yuri.benditovich@daynix.com Subject: [RFC PATCH v2 2/4] virtio-pci: add handling of ATS and Device-TLB enable Date: Mon, 1 May 2023 05:02:19 +0300 Message-Id: <20230501020221.188376-3-viktor@daynix.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230501020221.188376-1-viktor@daynix.com> References: <20230501020221.188376-1-viktor@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2a00:1450:4864:20::132; envelope-from=viktor@daynix.com; helo=mail-lf1-x132.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Guest may enable or disable PCI ATS and, accordingly, Device-TLB for the device. Add a flag and a trigger function to handle Device-TLB enable/disable in VirtIO devices and hook it to ATS enable/disable for PCI transport. Signed-off-by: Viktor Prutyanov --- hw/virtio/virtio-pci.c | 17 +++++++++++++++++ include/hw/virtio/virtio.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index 247325c193..ccd8c4efa1 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -716,6 +716,18 @@ virtio_address_space_read(VirtIOPCIProxy *proxy, hwaddr addr, } } +static void virtio_pci_ats_ctrl_trigger(PCIDevice *pci_dev, bool enable) +{ + VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); + VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); + VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); + + vdev->device_iotlb_enabled = enable; + + if (k->toggle_device_iotlb) + k->toggle_device_iotlb(vdev, enable); +} + static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, uint32_t val, int len) { @@ -729,6 +741,11 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, pcie_cap_flr_write_config(pci_dev, address, val, len); } + if (proxy->flags & VIRTIO_PCI_FLAG_ATS) { + pcie_ats_config_write(pci_dev, address, val, len, + virtio_pci_ats_ctrl_trigger); + } + if (range_covers_byte(address, len, PCI_COMMAND)) { if (!(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { virtio_set_disabled(vdev, true); diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index f236e94ca6..83d07bb6b7 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -155,6 +155,7 @@ struct VirtIODevice QLIST_HEAD(, VirtQueue) *vector_queues; QTAILQ_ENTRY(VirtIODevice) next; EventNotifier config_notifier; + bool device_iotlb_enabled; }; struct VirtioDeviceClass { @@ -212,6 +213,7 @@ struct VirtioDeviceClass { const VMStateDescription *vmsd; bool (*primary_unplug_pending)(void *opaque); struct vhost_dev *(*get_vhost)(VirtIODevice *vdev); + void (*toggle_device_iotlb)(VirtIODevice *vdev, bool enable); }; void virtio_instance_init_common(Object *proxy_obj, void *data, From patchwork Mon May 1 02:02:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 13227357 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 898B4C77B73 for ; Mon, 1 May 2023 02:03:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ptIsL-0003rJ-KG; Sun, 30 Apr 2023 22:02:49 -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 1ptIsK-0003rB-Np for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:48 -0400 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ptIs4-0003F2-TN for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:48 -0400 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-4f00d3f98deso20385450e87.0 for ; Sun, 30 Apr 2023 19:02:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1682906551; x=1685498551; 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=KuoPIrbz1UnCVJ8I8IFQYEA36M+uyHB848AT63FYSIs=; b=ZdCwltOjv/kuXG00GxuWlDXQ8GLJCD91tv09ilFXZt2ojpAQBvTIYXyxSIdsKGlk2N y5ZB4SIYSNU/tx3uQ5JOMXDH6bYCVUe6MxnZwPlGRyXj70QcehMLu5C85FvD8Yxv81Sw cLt+eLeIu0roGPib/EOUuvCnJc/99NGF1Z/7Evs3KreS5zDYk3dQ/Z8WG72OtHiVhIaW v7kF5Ebbdz0zyLRpV52ranf2KuGogau/tbAai0yMWVtQ5+yPqDRbp04QJRqq9lKXmc50 ZoUKn5b7puG6G+z2TE9RZEuGqzx6omRoICyzTIENNa9BOwcJ8Y/yIE0i+Iyl80Exh5kS VLzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682906551; x=1685498551; 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=KuoPIrbz1UnCVJ8I8IFQYEA36M+uyHB848AT63FYSIs=; b=gzApO2/Sv40Q540TGAu9YFUXV0DiYk2hicC6wPAT7s7hGTHGBPWrgHof+pGaQ+WLZE FdEZHB8OSzEFeJTQ/T0+vgu82+jnjGHkPED+tK3JXdzA340yMsdRiw0QgrbzVanRbOrk vCxLQi9zJcUSkZx47ze4VrBiHSFidggP3Ois9HjioUHQX+McXyc75K+AF/M6RATuvAtm FSyLoUaVizsiAJlImB7m0n2Dre8JiiUf+187v4DuPR18YgB702aOljiAZqz+dPnEyyRt mG+r7RWuWPuk5TYzrlKnG6MWpxaEiM2EW2vURfigborq5eMp6+7OS+/iC7OrpP5PiOmf coqQ== X-Gm-Message-State: AC+VfDzoJ2NI6fABzYkUn/8hoElAn30DFNJb4iFh8++m/MBNt888fWKF TsgJbs6lqZYHkK/m1ZuzKNDIVA== X-Google-Smtp-Source: ACHHUZ5PpOTuCEJTCVxyN8+K+je0fKZWw4xFI9F/zssk9yXYq619muLQwMgQRP8zOa5JuYQuxaSEXA== X-Received: by 2002:a05:6512:3906:b0:4ed:c6d5:bd0 with SMTP id a6-20020a056512390600b004edc6d50bd0mr3119134lfu.3.1682906551279; Sun, 30 Apr 2023 19:02:31 -0700 (PDT) Received: from vp-pc.. (109-252-127-135.nat.spd-mgts.ru. [109.252.127.135]) by smtp.gmail.com with ESMTPSA id w7-20020ac254a7000000b004efe73ee01fsm3783271lfk.306.2023.04.30.19.02.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Apr 2023 19:02:31 -0700 (PDT) From: Viktor Prutyanov To: mst@redhat.com, jasowang@redhat.com, peterx@redhat.com, marcel.apfelbaum@gmail.com Cc: qemu-devel@nongnu.org, viktor@daynix.com, yan@daynix.com, yuri.benditovich@daynix.com Subject: [RFC PATCH v2 3/4] vhost: register and change IOMMU flag depending on Device-TLB state Date: Mon, 1 May 2023 05:02:20 +0300 Message-Id: <20230501020221.188376-4-viktor@daynix.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230501020221.188376-1-viktor@daynix.com> References: <20230501020221.188376-1-viktor@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2a00:1450:4864:20::132; envelope-from=viktor@daynix.com; helo=mail-lf1-x132.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The guest can disable or never enable Device-TLB. In these cases, it can't be used even if enabled in QEMU. So, check Device-TLB state before registering IOMMU notifier and select unmap flag depending on that. Also, implement a way to change IOMMU notifier flag if Device-TLB state is changed. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2001312 Signed-off-by: Viktor Prutyanov --- hw/virtio/vhost-backend.c | 6 ++++++ hw/virtio/vhost.c | 26 ++++++++++++++++++++++++-- include/hw/virtio/vhost-backend.h | 4 ++++ include/hw/virtio/vhost.h | 1 + 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/hw/virtio/vhost-backend.c b/hw/virtio/vhost-backend.c index 8e581575c9..30eb71fb83 100644 --- a/hw/virtio/vhost-backend.c +++ b/hw/virtio/vhost-backend.c @@ -297,6 +297,11 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev, qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL); } +static void vhost_kernel_toggle_device_iotlb(struct vhost_dev *dev, int enable) +{ + vhost_toggle_device_iotlb(dev, enable); +} + const VhostOps kernel_ops = { .backend_type = VHOST_BACKEND_TYPE_KERNEL, .vhost_backend_init = vhost_kernel_init, @@ -328,6 +333,7 @@ const VhostOps kernel_ops = { .vhost_vsock_set_running = vhost_kernel_vsock_set_running, .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg, + .vhost_toggle_device_iotlb = vhost_kernel_toggle_device_iotlb, }; #endif diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index a266396576..1bfcc6d263 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -796,7 +796,9 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIFIED); iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify, - IOMMU_NOTIFIER_DEVIOTLB_UNMAP, + dev->vdev->device_iotlb_enabled ? + IOMMU_NOTIFIER_DEVIOTLB_UNMAP : + IOMMU_NOTIFIER_UNMAP, section->offset_within_region, int128_get64(end), iommu_idx); @@ -804,7 +806,8 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu->iommu_offset = section->offset_within_address_space - section->offset_within_region; iommu->hdev = dev; - ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, NULL); + ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, + dev->vdev->device_iotlb_enabled ? NULL : &error_fatal); if (ret) { /* * Some vIOMMUs do not support dev-iotlb yet. If so, try to use the @@ -841,6 +844,25 @@ static void vhost_iommu_region_del(MemoryListener *listener, } } +void vhost_toggle_device_iotlb(struct vhost_dev *dev, bool enable) +{ + struct vhost_iommu *iommu; + int ret; + + QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { + memory_region_unregister_iommu_notifier(iommu->mr, &iommu->n); + iommu->n.notifier_flags = enable ? + IOMMU_NOTIFIER_DEVIOTLB_UNMAP : IOMMU_NOTIFIER_UNMAP; + ret = memory_region_register_iommu_notifier(iommu->mr, &iommu->n, + enable ? NULL : &error_fatal); + if (ret) { + iommu->n.notifier_flags = IOMMU_NOTIFIER_UNMAP; + memory_region_register_iommu_notifier(iommu->mr, &iommu->n, + &error_fatal); + } + } +} + static int vhost_virtqueue_set_addr(struct vhost_dev *dev, struct vhost_virtqueue *vq, unsigned idx, bool enable_log) diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-backend.h index ec3fbae58d..f8e9660a96 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -133,6 +133,9 @@ typedef int (*vhost_set_config_call_op)(struct vhost_dev *dev, typedef void (*vhost_reset_status_op)(struct vhost_dev *dev); +typedef void (*vhost_toggle_device_iotlb_op)(struct vhost_dev *dev, + int enabled); + typedef struct VhostOps { VhostBackendType backend_type; vhost_backend_init vhost_backend_init; @@ -181,6 +184,7 @@ typedef struct VhostOps { vhost_force_iommu_op vhost_force_iommu; vhost_set_config_call_op vhost_set_config_call; vhost_reset_status_op vhost_reset_status; + vhost_toggle_device_iotlb_op vhost_toggle_device_iotlb; } VhostOps; int vhost_backend_update_device_iotlb(struct vhost_dev *dev, diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index a52f273347..b3f585c6cd 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -320,6 +320,7 @@ bool vhost_has_free_slot(void); int vhost_net_set_backend(struct vhost_dev *hdev, struct vhost_vring_file *file); +void vhost_toggle_device_iotlb(struct vhost_dev *dev, bool enable); int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write); int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, From patchwork Mon May 1 02:02:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 13227360 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DD4ECC77B7F for ; Mon, 1 May 2023 02:03:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ptIsB-0003qO-7y; Sun, 30 Apr 2023 22:02:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ptIs6-0003p9-PR for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:37 -0400 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ptIs5-0003Fn-B5 for qemu-devel@nongnu.org; Sun, 30 Apr 2023 22:02:34 -0400 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-4edc7cc6f46so2500644e87.1 for ; Sun, 30 Apr 2023 19:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1682906552; x=1685498552; 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=3Ndat+FBo1b63fMAzLq7S3YsnsowwHK/srih+mHf+b8=; b=fDkcM1jvrtIdIT8IQS+xNs70QCMxJg1d76ylWOB1YBy/H1lIhOmTxeScX2ECu3OcPb WUx5ocAy5O0rVX32jonp+TBnsGyWiyP7NMIpfoT/LaT1xFNVypP390O3tUb31UGfPjHH 4FjEXMg7ySzk4EEzp5gFMlrJw1Xs7NpiD5Vo+2DNJ1/EZRfJZRkcMaq7uJ9aK3u7duy2 KNxxYXj8oZRjatISp//bLpTfiLBJlwc5qIXRQDweXAWiXtdRxsPdHy8nzKTx8nBpmelU UcN1EOaifibY2113BltN5Vvl9jPuwQueP7KD0VbiaWQe2h9ygS1q+cgbLv8cWiJ/3Z3a LJ9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682906552; x=1685498552; 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=3Ndat+FBo1b63fMAzLq7S3YsnsowwHK/srih+mHf+b8=; b=OGu6mdIDvoJlltLKUq3+dYKREaE8goHKEQl/1iwi7ZuRytbwejCI7HBvZ2yglKtduU PPQ04/sv5T860TkkhdF+URxB7+MagUuvTMvyU+CkynDyAcAz0Cdlpt5Om72kqfpYWTZ9 IeVByaqscLB9Y7fKvAhZNapibrsyh96ibzFICUG7i+3knESe1Krw6pbUnb+MGt6j+6SH dKP1lgwZJnmtvUhNH0lxlr8rFhuYbGsZbBXYM/jtgFHaEbccY0h6486tGcfEHIEXO9vc kY6pEV4DtPb5tg7dSanXwCC023cd5tQfMMhKbAk37ZzbZk1UXFTl3jyvtxMatP3ez2cz kAEA== X-Gm-Message-State: AC+VfDxAHDIJ4aY/+m3A/OhjSvzYCCQ+NDTZ24B6Ud8+V8ACb/fbDEZP LP1hGRXDxqeVGxK0Mo/bZ+dTRw== X-Google-Smtp-Source: ACHHUZ7y2VSq9wB+JUwIzVhJxVdecJ5sd2hAvVzRHuszZhy75UUC3wG34n+Akg/zReOSLfiFMs3sew== X-Received: by 2002:ac2:5613:0:b0:4ef:f1f4:14db with SMTP id v19-20020ac25613000000b004eff1f414dbmr3531285lfd.16.1682906551977; Sun, 30 Apr 2023 19:02:31 -0700 (PDT) Received: from vp-pc.. (109-252-127-135.nat.spd-mgts.ru. [109.252.127.135]) by smtp.gmail.com with ESMTPSA id w7-20020ac254a7000000b004efe73ee01fsm3783271lfk.306.2023.04.30.19.02.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Apr 2023 19:02:31 -0700 (PDT) From: Viktor Prutyanov To: mst@redhat.com, jasowang@redhat.com, peterx@redhat.com, marcel.apfelbaum@gmail.com Cc: qemu-devel@nongnu.org, viktor@daynix.com, yan@daynix.com, yuri.benditovich@daynix.com Subject: [RFC PATCH v2 4/4] virtio-net: pass Device-TLB enable/disable events to vhost Date: Mon, 1 May 2023 05:02:21 +0300 Message-Id: <20230501020221.188376-5-viktor@daynix.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230501020221.188376-1-viktor@daynix.com> References: <20230501020221.188376-1-viktor@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2a00:1450:4864:20::132; envelope-from=viktor@daynix.com; helo=mail-lf1-x132.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org If vhost is enabled for virtio-net, Device-TLB enable/disable events must be passed to vhost for proper IOMMU unmap flag selection. Signed-off-by: Viktor Prutyanov --- hw/net/vhost_net.c | 11 +++++++++++ hw/net/virtio-net.c | 8 ++++++++ include/net/vhost_net.h | 2 ++ 3 files changed, 21 insertions(+) diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c index c4eecc6f36..2364c8de99 100644 --- a/hw/net/vhost_net.c +++ b/hw/net/vhost_net.c @@ -552,6 +552,17 @@ int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu) return vhost_ops->vhost_net_set_mtu(&net->dev, mtu); } +void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable) +{ + const VhostOps *vhost_ops = dev->vhost_ops; + + if (!vhost_ops->vhost_toggle_device_iotlb) { + return; + } + + vhost_ops->vhost_toggle_device_iotlb(dev, enable); +} + void vhost_net_virtqueue_reset(VirtIODevice *vdev, NetClientState *nc, int vq_index) { diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 53e1c32643..e6851b885c 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -3843,6 +3843,13 @@ static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev) return &net->dev; } +static void virtio_net_toggle_device_iotlb(VirtIODevice *vdev, + bool enable) +{ + if (vdev->vhost_started) + vhost_net_toggle_device_iotlb(virtio_net_get_vhost(vdev), enable); +} + static const VMStateDescription vmstate_virtio_net = { .name = "virtio-net", .minimum_version_id = VIRTIO_NET_VM_VERSION, @@ -3948,6 +3955,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data) vdc->vmsd = &vmstate_virtio_net_device; vdc->primary_unplug_pending = primary_unplug_pending; vdc->get_vhost = virtio_net_get_vhost; + vdc->toggle_device_iotlb = virtio_net_toggle_device_iotlb; } static const TypeInfo virtio_net_info = { diff --git a/include/net/vhost_net.h b/include/net/vhost_net.h index c37aba35e6..36d527f321 100644 --- a/include/net/vhost_net.h +++ b/include/net/vhost_net.h @@ -56,4 +56,6 @@ int vhost_net_virtqueue_restart(VirtIODevice *vdev, NetClientState *nc, int vq_index); void vhost_net_save_acked_features(NetClientState *nc); + +void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable); #endif