Message ID | 20230228042137.1941024-2-gankulkarni@os.amperecomputing.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2809671wrd; Mon, 27 Feb 2023 20:41:36 -0800 (PST) X-Google-Smtp-Source: AK7set+KiIenrB+p4PvwlKZiqXZ1KOU5oLgDJ8fD85epkCQR7E9OfDfzhEdhGYzz/csk0SnHARGZ X-Received: by 2002:a05:6402:5249:b0:4af:70a5:560b with SMTP id t9-20020a056402524900b004af70a5560bmr13839441edd.9.1677559296758; Mon, 27 Feb 2023 20:41:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1677559296; cv=pass; d=google.com; s=arc-20160816; b=XvwPmkMiAEBKuJJc+SfOxWxP2khGdwfo8hI5LRyEPpW8TDeJSvK9lQQhka5itOJSGT F55mSmjntZR6efgQGOmbi+Iv9fMauo1fqTHSkmIWS9I6u0SMKlTw+UvXNwzNA0eMLdrC AUlPYme3I2c6mhkZWbkrMxNc9O60TWwL7+VOVeqB2YXd4e65LhTPu1X965EJyBW6dNLE exo5Fhd8NP+zfiRqO0q3JSRSWca+QGxSJJ3gjcZSxolVg3iXE/lCQxPNUwrZfmyPOPAW WLUplIA73ysDKKnsa2xLrwMZxLEHGNJ3EFmdyOjhz7VhXxx96NkLNS+9NXDKpXfS5VU5 m/0g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SdyHm+N58UlbZO7Gmi6onc7rUlBRHFd8CHGNB8G2p8I=; b=j71ZvVtmR7EsLoAtTsykSQVMYnJrUt4VAYEXinzr4PFfDIimTVbh4HZtkEMSIpgdcG 8WKHoyAveE20z21Uh0wVISTdRfZxKT9eFQquFFGWonW6bLe21SFiPc/2S2kDWxcZlop3 ECerghHAZ8KKrJ4aeC6pJJposkjvex+AwmotyoYh+WTKoLJU8OaQ9w+9O3Lo2HQSEkmy uDIOXmZRNO3KAtiQkI+SOg1045T89QIBiLuDCfwNEJkJK+yDb9d2C+8QU/Nl3OWQvpe2 qtGCB3dJnYDdkvCP69VoHQPpT2/aN9iX2kr16Ko/OzZ9a04qwNzQ5mEMcrCOrWOBGSIp 6KKA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@os.amperecomputing.com header.s=selector2 header.b=W9DjsTcy; arc=pass (i=1 spf=pass spfdomain=os.amperecomputing.com dkim=pass dkdomain=os.amperecomputing.com dmarc=pass fromdomain=os.amperecomputing.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amperecomputing.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t13-20020a056402020d00b004acc42357cfsi2665269edv.364.2023.02.27.20.41.11; Mon, 27 Feb 2023 20:41:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@os.amperecomputing.com header.s=selector2 header.b=W9DjsTcy; arc=pass (i=1 spf=pass spfdomain=os.amperecomputing.com dkim=pass dkdomain=os.amperecomputing.com dmarc=pass fromdomain=os.amperecomputing.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amperecomputing.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229787AbjB1EWB (ORCPT <rfc822;liqunnana@gmail.com> + 99 others); Mon, 27 Feb 2023 23:22:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229543AbjB1EV7 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 27 Feb 2023 23:21:59 -0500 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2116.outbound.protection.outlook.com [40.107.244.116]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DEA7265BD; Mon, 27 Feb 2023 20:21:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HhCOoGoS062MXqexnIUacdbTttlCWhWuxEcI7rQP7lJseuM4hzEoyUFvWNa6UmkDR7vZaHCas0mJf98UHKJx3UnrCqUH0WWck3hVrBeArC34jQmaemKNGYitkGbcNT9PZqGY8t80XNEWKNFLH+tUPRHb5XKYO606pU08wa0OU5l+0lbDCuBW7arF2hwBGk2hyGcXJFBq0seQdUTA+k6bn+7Bz47bEz/MAtgOY7HurDkeRzn0CkqNnB3xkTd9CJd5BDgVyd1tC63unj3mo+BXX3ftasMd1DflSB8izClMfII0/UjV+5Q/8WFjYgf9W1n3yUBtww9tFBK4Pyv6nsUBhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SdyHm+N58UlbZO7Gmi6onc7rUlBRHFd8CHGNB8G2p8I=; b=SyJus1RS3EsbK0pKT0fJbdSHy3d+xBK5Gaw35PiMQnIJYZineVnvU/4kU/FYMMySZJ0kNQzxvy59bVHnuuCCICQWXqdpP7i5a7EXsucCE9pMWGJeDu2AaTFzCAUi7WgSIWdSmogI86+81jRC+5X28JuEha4vhJzb86I2G6HcoonBlM0Zri174U5Hm18RmhiFlC8QPqUl3I5XMKrnLMe9X+KXQ/DFvpmmCmdFgn6o8NMrxDL6LZxKCx2H2ZMvEnpbbGtDK8s6qo89LMO9VzC6vDrLsEc6GROaoAotkNnHN9ZKjOzVQ6ewIjBGqJval9GSAQeAoABIVS3f3X7GWo/Z3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SdyHm+N58UlbZO7Gmi6onc7rUlBRHFd8CHGNB8G2p8I=; b=W9DjsTcyIqbem95FUShNmVvei6ZCT0alGebxHZxeWu3KIHcLJxiAlp0r7NNOcrbmgJQSSeAsaZDLudBQ8NuAWoeDlsQ2UtfWhU7BxGE5H/I3C847IWeS2OIzAEY/hfhDAAKagxhyjaIwOCAFnOBFfssQOI9C5sz7yXF8hk2/ge8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from DM8PR01MB6824.prod.exchangelabs.com (2603:10b6:8:23::24) by DM6PR01MB4700.prod.exchangelabs.com (2603:10b6:5:65::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.25; Tue, 28 Feb 2023 04:21:53 +0000 Received: from DM8PR01MB6824.prod.exchangelabs.com ([fe80::fc8b:5e5e:a850:7f0a]) by DM8PR01MB6824.prod.exchangelabs.com ([fe80::fc8b:5e5e:a850:7f0a%6]) with mapi id 15.20.6134.029; Tue, 28 Feb 2023 04:21:53 +0000 From: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> To: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev, joro@8bytes.org, bhelgaas@google.com, robin.murphy@arm.com, will@kernel.org Cc: jean-philippe@linaro.org, sathyanarayanan.kuppuswamy@linux.intel.com, darren@os.amperecomputing.com, scott@os.amperecomputing.com, gankulkarni@os.amperecomputing.com Subject: [PATCH v2 1/2] PCI/ATS: Add a helper function to configure ATS STU of a PF Date: Mon, 27 Feb 2023 20:21:36 -0800 Message-Id: <20230228042137.1941024-2-gankulkarni@os.amperecomputing.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230228042137.1941024-1-gankulkarni@os.amperecomputing.com> References: <20230228042137.1941024-1-gankulkarni@os.amperecomputing.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: CH0PR04CA0109.namprd04.prod.outlook.com (2603:10b6:610:75::24) To DM8PR01MB6824.prod.exchangelabs.com (2603:10b6:8:23::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM8PR01MB6824:EE_|DM6PR01MB4700:EE_ X-MS-Office365-Filtering-Correlation-Id: a962cd65-22f7-48e2-c64b-08db19435232 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Q9dpGATL/ylPETjt2LQFOPFoQBqo5TSnCRFM/UwjMeFaVy4aHLgZnK0teURnX7GKGGpvA0GFX5PdA16ZGJ73ChKoPdMcPc5kx2vYdF5Qqp21ksRvv7WURmFAzFFucKzHDw+/V4EXSk1YqDe/2lnAggyL+SvPf3dKDizl/2seSvqPAcuyRQ4Y/1nANNBg6GuhV2ELL/Z4qdPl0BrDIdQntGJq7/xS/31FQV2qhMzy1D+YnhO5IgFQ7fD8W9cDJJkzs4W52jLsQ8K4sTMIKTZa6NlM5HWmtkH1+TOyRB4Okg4Lt0wJpQ2+6S8E8+5w1HeXmowja1n/AmG+9jubXuuvkn+S14v51zqZp+8luQHmwtEVgt6fg6wG21QQku/0e7J2H2M1z9w4V4ovNk5lIJbdoCsJzYya52McCCRrnezJAT6EyIFVINwsx0kbn+Bf5mcTsRt91KiGVZvh8DdmavD2krtY1l1QNWDpeGedRdEgzkITXxyI/qKmqUcR3SNZNzNswFdTxT9e5isMRxw8LxC/MoPJz2G/02+Q3y3PBRBct8fq/1covXN9Xyn/6NuaCliIrAl36qxVWMyb4pH9PLj4S+O8Q9hQpJPbFNui7XPeJ50RUUL2HNDVVjcoMnCo/5ort5V2NZE/gzAQTK0djx61w1ibU1DyYexq6x72A2t9Fz6qx8V4ORDQl2QRQ5IhPZm/7HUkDBYmUVJZ+Bx6uHqVPA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM8PR01MB6824.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(346002)(366004)(39850400004)(396003)(376002)(136003)(451199018)(86362001)(316002)(6486002)(186003)(8676002)(7416002)(66946007)(41300700001)(66556008)(6512007)(4326008)(8936002)(478600001)(1076003)(2616005)(6666004)(26005)(5660300002)(107886003)(6506007)(66476007)(52116002)(38350700002)(2906002)(38100700002)(83380400001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: /j9GOjpUcozFwNSp2aA4l2zd4PSOXc/7YKj1/RGG8uGE32RVVBs8OWYlBFFId4sPnvojP0QfYj8n/IN6n/Gspe3fOjA28Kq53BEq2fKX4Aw2W87b90ZBMGhD/ENKvwt0wwe3hRC2mY0JFEM7K1Ag2iQDAWZ4cYMy9K/Klwcb/WjD5BD++QH/Y2dAP4+pqrZkhxQDFgUjSRMsobBXFtKKwNiXW4NK2af81mWM6iQt5w+WPgZn0IWeW7tfnTl1o/q2T6ZJAcPuMD+n3A7Dmn1UmdvKOhbVHjzzi+vkZZKRP/2FCfJ9Q7EkX648qqhu5BQjWXXtMM6F21IySpKuuwyLMDRPoahhhDQPm5XQgZOlGu8anB0VkFYRQp2r+8pdtmpWre5jBzeeNIh6m9Kee+6w3lmFoldR8DJL/s7SeZ45EBIPbQbfy3k34qlCZXpDTeN9YTq7riYgLBFK5L5uJOv1egLALDNF0ZRUbFbcoN9omKIedVnTUpU74dhTrUMH2AKif0WEEjm2pkX0HZxrLXPj5xl+ZpKT4qBzMbf2NXUEL7kSF7UTNsAyGufb0FjMxKS7rqC3zXKVblwCbpnIdNHVRZ/yHSZVQ2Ol8n1JLqpM78fLVpUzJ9Slg4d50WcyABic4gWqJUngInDGOoE3mYPb6GgukyImRD8YLN+AjnketimYnCTiysoDvh97sbRdPA2B8Ta6+BjS01VwqxnfkOBB9R5ivWhGK2E5ao+kDORsOVXiXmux2ShbGiKfCAFcZp0zdEb2haOprb3sjwbmbuxcRt2El7tAGs+8BGDYN8qcJQynrjWBMPVlV1Iy52D1DORBWhcwR/CbxojgHpIt41MEDZhx0JrqbGlfcvuJxrH4Q95gpJnBi3CDlZIDWgPuAs1FJH95kycaSFW0YSQkY3umdf3EQrElJdxX8eSDZ43VDrxel5uQcoHS5nTYdXeM8p1nQIrd+/MamDRy5FMDLFLOD86Z3ZlfRIksx6SHwehYKGp3ZTja5QHgLCXKZIpdbEryLXnVVjzBgDcKEDyWpwsBNJGVtsZDAvcsG/GZPqfFQwUT2DtwUhzTYN3sXrxJZ/7njoC2COrfQ6nKz5wUp42OldEW5jW/BHLKM0ksGQ6iwJvRQJBVxCtvQv2HMJ1XGr4DKTcRGAzoZ49lhyJOeQo6FewnjLCchdseDYUGwC09hBF6kICdV1DcnWQWABRXkivUTFaSOBFlWSdYDdYLEI1bPbI3Wv+LxOdHqTS5wdWAXI8wTy3hXPQbcmwrzhKqN2o5DGDDc9PKycDkJh+8DcL2vGGTiap/IFUJxZ2ITWwjpaHbXYMsmj9IRqjrXvaIqUHZ9gPAT7zar25En3Sk+00C7y1YqRfz08tM7Nxq3D2xMcA6+8Fpuu1V/g5DD3SiB1RnS1XyyW9XfG6Mu78YV34jjXj+1atSUSNf1zNhZLpPc52zSzWYJEFY4+pdTtgL74t1sNc+BHgTfES/y1xIpeeZajBXTOxg+8BJxdHD6Oy0OIGedvpD6N2X5aDHfjCZT8rtpW/sSxvENHvFIxvdG8kvxrDbQks8R2yPzVhbBrMi0ORnjMt/DEwHnllhMF/94LYcE7Pqmb+q5ZxnqrhoZqnG3gfB8QiAl2pbyfuGw4QxlqA= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: a962cd65-22f7-48e2-c64b-08db19435232 X-MS-Exchange-CrossTenant-AuthSource: DM8PR01MB6824.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Feb 2023 04:21:53.6869 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qtkrA5WqKV9fGSlsuWS2l1Ta4B29MZsGp44AH1lB0cWlUvvgxM63ItBZfB7prfKQC0w6ouaMzyyKRHYsqf4C0IwipgBk5hKHQR/5N2oCgeBE+bMc3h6b84OOU7kiZvKw X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR01MB4700 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759048416967071707?= X-GMAIL-MSGID: =?utf-8?q?1759048416967071707?= |
Series |
Add support to enable ATS on VFs independently
|
|
Commit Message
Ganapatrao Kulkarni
Feb. 28, 2023, 4:21 a.m. UTC
As per PCI specification (PCI Express Base Specification Revision
6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled
independently for ATS capability, however the STU(Smallest Translation
Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and
the associated PF's value applies to VFs.
In the current code, the STU is being configured while enabling the PF ATS.
Hence, it is not able to enable ATS for VFs, if it is not enabled on the
associated PF already.
Adding a function pci_ats_stu_configure(), which can be called to
configure the STU during PF enumeration.
Latter enumerations of VFs can successfully enable ATS independently.
Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com>
---
drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++--
include/linux/pci-ats.h | 3 +++
2 files changed, 34 insertions(+), 2 deletions(-)
Comments
On 2/27/23 8:21 PM, Ganapatrao Kulkarni wrote: > As per PCI specification (PCI Express Base Specification Revision > 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled > independently for ATS capability, however the STU(Smallest Translation > Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and > the associated PF's value applies to VFs. > > In the current code, the STU is being configured while enabling the PF ATS. > Hence, it is not able to enable ATS for VFs, if it is not enabled on the > associated PF already. > > Adding a function pci_ats_stu_configure(), which can be called to > configure the STU during PF enumeration. > Latter enumerations of VFs can successfully enable ATS independently. > > Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> > --- > drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++-- > include/linux/pci-ats.h | 3 +++ > 2 files changed, 34 insertions(+), 2 deletions(-) > > diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c > index f9cc2e10b676..1611bfa1d5da 100644 > --- a/drivers/pci/ats.c > +++ b/drivers/pci/ats.c > @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) > } > EXPORT_SYMBOL_GPL(pci_ats_supported); > > +/** > + * pci_ats_stu_configure - Configure STU of a PF. > + * @dev: the PCI device > + * @ps: the IOMMU page shift > + * > + * Returns 0 on success, or negative on failure. > + */ > +int pci_ats_stu_configure(struct pci_dev *dev, int ps) > +{ > + u16 ctrl; > + > + if (dev->ats_enabled || dev->is_virtfn) > + return 0; Is PF allowed to re-configure STU if there are other active VF's which uses it? > + > + if (!pci_ats_supported(dev)) > + return -EINVAL; > + > + if (ps < PCI_ATS_MIN_STU) > + return -EINVAL; > + > + dev->ats_stu = ps; > + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); > + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); > + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); > + > /** > * pci_enable_ats - enable the ATS capability > * @dev: the PCI device > @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) > return -EINVAL; > > /* > - * Note that enabling ATS on a VF fails unless it's already enabled > - * with the same STU on the PF. > + * Note that enabling ATS on a VF fails unless it's already > + * configured with the same STU on the PF. > */ > ctrl = PCI_ATS_CTRL_ENABLE; > if (dev->is_virtfn) { > diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h > index df54cd5b15db..7d62a92aaf23 100644 > --- a/include/linux/pci-ats.h > +++ b/include/linux/pci-ats.h > @@ -8,6 +8,7 @@ > /* Address Translation Service */ > bool pci_ats_supported(struct pci_dev *dev); > int pci_enable_ats(struct pci_dev *dev, int ps); > +int pci_ats_stu_configure(struct pci_dev *dev, int ps); > void pci_disable_ats(struct pci_dev *dev); > int pci_ats_queue_depth(struct pci_dev *dev); > int pci_ats_page_aligned(struct pci_dev *dev); > @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) > { return false; } > static inline int pci_enable_ats(struct pci_dev *d, int ps) > { return -ENODEV; } > +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) > +{ return -ENODEV; } > static inline void pci_disable_ats(struct pci_dev *d) { } > static inline int pci_ats_queue_depth(struct pci_dev *d) > { return -ENODEV; }
Hi Bjorn, On 28-02-2023 09:51 am, Ganapatrao Kulkarni wrote: > As per PCI specification (PCI Express Base Specification Revision > 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled > independently for ATS capability, however the STU(Smallest Translation > Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and > the associated PF's value applies to VFs. > > In the current code, the STU is being configured while enabling the PF ATS. > Hence, it is not able to enable ATS for VFs, if it is not enabled on the > associated PF already. > > Adding a function pci_ats_stu_configure(), which can be called to > configure the STU during PF enumeration. > Latter enumerations of VFs can successfully enable ATS independently. Any comments? > > Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> > --- > drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++-- > include/linux/pci-ats.h | 3 +++ > 2 files changed, 34 insertions(+), 2 deletions(-) > > diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c > index f9cc2e10b676..1611bfa1d5da 100644 > --- a/drivers/pci/ats.c > +++ b/drivers/pci/ats.c > @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) > } > EXPORT_SYMBOL_GPL(pci_ats_supported); > > +/** > + * pci_ats_stu_configure - Configure STU of a PF. > + * @dev: the PCI device > + * @ps: the IOMMU page shift > + * > + * Returns 0 on success, or negative on failure. > + */ > +int pci_ats_stu_configure(struct pci_dev *dev, int ps) > +{ > + u16 ctrl; > + > + if (dev->ats_enabled || dev->is_virtfn) > + return 0; > + > + if (!pci_ats_supported(dev)) > + return -EINVAL; > + > + if (ps < PCI_ATS_MIN_STU) > + return -EINVAL; > + > + dev->ats_stu = ps; > + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); > + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); > + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); > + > /** > * pci_enable_ats - enable the ATS capability > * @dev: the PCI device > @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) > return -EINVAL; > > /* > - * Note that enabling ATS on a VF fails unless it's already enabled > - * with the same STU on the PF. > + * Note that enabling ATS on a VF fails unless it's already > + * configured with the same STU on the PF. > */ > ctrl = PCI_ATS_CTRL_ENABLE; > if (dev->is_virtfn) { > diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h > index df54cd5b15db..7d62a92aaf23 100644 > --- a/include/linux/pci-ats.h > +++ b/include/linux/pci-ats.h > @@ -8,6 +8,7 @@ > /* Address Translation Service */ > bool pci_ats_supported(struct pci_dev *dev); > int pci_enable_ats(struct pci_dev *dev, int ps); > +int pci_ats_stu_configure(struct pci_dev *dev, int ps); > void pci_disable_ats(struct pci_dev *dev); > int pci_ats_queue_depth(struct pci_dev *dev); > int pci_ats_page_aligned(struct pci_dev *dev); > @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) > { return false; } > static inline int pci_enable_ats(struct pci_dev *d, int ps) > { return -ENODEV; } > +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) > +{ return -ENODEV; } > static inline void pci_disable_ats(struct pci_dev *d) { } > static inline int pci_ats_queue_depth(struct pci_dev *d) > { return -ENODEV; } Thanks, Ganapat
On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: > As per PCI specification (PCI Express Base Specification Revision > 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled > independently for ATS capability, however the STU(Smallest Translation > Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and > the associated PF's value applies to VFs. > > In the current code, the STU is being configured while enabling the PF ATS. > Hence, it is not able to enable ATS for VFs, if it is not enabled on the > associated PF already. > > Adding a function pci_ats_stu_configure(), which can be called to > configure the STU during PF enumeration. > Latter enumerations of VFs can successfully enable ATS independently. s/STU(Smallest/STU (Smallest/ (add space before paren) s/Adding a function pci_ats_stu_configure()/Add pci_ats_stu_configure()/ s/Latter/Subsequent/ Add blank line between paragraphs (it looks like "Latter enumerations" is intended to start a new paragraph). > Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> Acked-by: Bjorn Helgaas <bhelgaas@google.com> Given an ack for the IOMMU patch, I'd be happy to merge both (and I can do the commit log tweaks); just let me know. One comment/question below. > --- > drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++-- > include/linux/pci-ats.h | 3 +++ > 2 files changed, 34 insertions(+), 2 deletions(-) > > diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c > index f9cc2e10b676..1611bfa1d5da 100644 > --- a/drivers/pci/ats.c > +++ b/drivers/pci/ats.c > @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) > } > EXPORT_SYMBOL_GPL(pci_ats_supported); > > +/** > + * pci_ats_stu_configure - Configure STU of a PF. > + * @dev: the PCI device > + * @ps: the IOMMU page shift > + * > + * Returns 0 on success, or negative on failure. > + */ > +int pci_ats_stu_configure(struct pci_dev *dev, int ps) > +{ > + u16 ctrl; > + > + if (dev->ats_enabled || dev->is_virtfn) > + return 0; I might return an error for the VF case on the assumption that it's likely an error in the caller. I guess one could argue that it simplifies the caller if it doesn't have to check for PF vs VF. But the fact that STU is shared between PF and VFs is an important part of understanding how ATS works, so the caller should be aware of the distinction anyway. > + > + if (!pci_ats_supported(dev)) > + return -EINVAL; > + > + if (ps < PCI_ATS_MIN_STU) > + return -EINVAL; > + > + dev->ats_stu = ps; > + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); > + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); > + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); > + > /** > * pci_enable_ats - enable the ATS capability > * @dev: the PCI device > @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) > return -EINVAL; > > /* > - * Note that enabling ATS on a VF fails unless it's already enabled > - * with the same STU on the PF. > + * Note that enabling ATS on a VF fails unless it's already > + * configured with the same STU on the PF. > */ > ctrl = PCI_ATS_CTRL_ENABLE; > if (dev->is_virtfn) { > diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h > index df54cd5b15db..7d62a92aaf23 100644 > --- a/include/linux/pci-ats.h > +++ b/include/linux/pci-ats.h > @@ -8,6 +8,7 @@ > /* Address Translation Service */ > bool pci_ats_supported(struct pci_dev *dev); > int pci_enable_ats(struct pci_dev *dev, int ps); > +int pci_ats_stu_configure(struct pci_dev *dev, int ps); > void pci_disable_ats(struct pci_dev *dev); > int pci_ats_queue_depth(struct pci_dev *dev); > int pci_ats_page_aligned(struct pci_dev *dev); > @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) > { return false; } > static inline int pci_enable_ats(struct pci_dev *d, int ps) > { return -ENODEV; } > +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) > +{ return -ENODEV; } > static inline void pci_disable_ats(struct pci_dev *d) { } > static inline int pci_ats_queue_depth(struct pci_dev *d) > { return -ENODEV; } > -- > 2.38.1 > > > _______________________________________________ > linux-arm-kernel mailing list > linux-arm-kernel@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Hi Kulkarni, On 3/13/23 2:12 PM, Bjorn Helgaas wrote: > On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >> As per PCI specification (PCI Express Base Specification Revision >> 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled >> independently for ATS capability, however the STU(Smallest Translation >> Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and >> the associated PF's value applies to VFs. >> >> In the current code, the STU is being configured while enabling the PF ATS. >> Hence, it is not able to enable ATS for VFs, if it is not enabled on the >> associated PF already. >> >> Adding a function pci_ats_stu_configure(), which can be called to >> configure the STU during PF enumeration. >> Latter enumerations of VFs can successfully enable ATS independently. > > s/STU(Smallest/STU (Smallest/ (add space before paren) > s/Adding a function pci_ats_stu_configure()/Add pci_ats_stu_configure()/ > s/Latter/Subsequent/ > > Add blank line between paragraphs (it looks like "Latter enumerations" > is intended to start a new paragraph). > >> Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> > > Acked-by: Bjorn Helgaas <bhelgaas@google.com> > > Given an ack for the IOMMU patch, I'd be happy to merge both (and I > can do the commit log tweaks); just let me know. > > One comment/question below. > >> --- >> drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++-- >> include/linux/pci-ats.h | 3 +++ >> 2 files changed, 34 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >> index f9cc2e10b676..1611bfa1d5da 100644 >> --- a/drivers/pci/ats.c >> +++ b/drivers/pci/ats.c >> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >> } >> EXPORT_SYMBOL_GPL(pci_ats_supported); >> >> +/** >> + * pci_ats_stu_configure - Configure STU of a PF. >> + * @dev: the PCI device >> + * @ps: the IOMMU page shift >> + * >> + * Returns 0 on success, or negative on failure. >> + */ >> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >> +{ >> + u16 ctrl; >> + >> + if (dev->ats_enabled || dev->is_virtfn) >> + return 0; > > I might return an error for the VF case on the assumption that it's > likely an error in the caller. I guess one could argue that it > simplifies the caller if it doesn't have to check for PF vs VF. But > the fact that STU is shared between PF and VFs is an important part of > understanding how ATS works, so the caller should be aware of the > distinction anyway. I have already asked this question. But let me repeat it. We don't have any checks for the PF case here. That means you can re-configure the STU as many times as you want until ATS is enabled in PF. So, if there are active VFs which uses this STU, can PF re-configure the STU at will? > >> + >> + if (!pci_ats_supported(dev)) >> + return -EINVAL; >> + >> + if (ps < PCI_ATS_MIN_STU) >> + return -EINVAL; >> + >> + dev->ats_stu = ps; >> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >> + >> + return 0; >> +} >> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >> + >> /** >> * pci_enable_ats - enable the ATS capability >> * @dev: the PCI device >> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >> return -EINVAL; >> >> /* >> - * Note that enabling ATS on a VF fails unless it's already enabled >> - * with the same STU on the PF. >> + * Note that enabling ATS on a VF fails unless it's already >> + * configured with the same STU on the PF. >> */ >> ctrl = PCI_ATS_CTRL_ENABLE; >> if (dev->is_virtfn) { >> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >> index df54cd5b15db..7d62a92aaf23 100644 >> --- a/include/linux/pci-ats.h >> +++ b/include/linux/pci-ats.h >> @@ -8,6 +8,7 @@ >> /* Address Translation Service */ >> bool pci_ats_supported(struct pci_dev *dev); >> int pci_enable_ats(struct pci_dev *dev, int ps); >> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >> void pci_disable_ats(struct pci_dev *dev); >> int pci_ats_queue_depth(struct pci_dev *dev); >> int pci_ats_page_aligned(struct pci_dev *dev); >> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >> { return false; } >> static inline int pci_enable_ats(struct pci_dev *d, int ps) >> { return -ENODEV; } >> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >> +{ return -ENODEV; } >> static inline void pci_disable_ats(struct pci_dev *d) { } >> static inline int pci_ats_queue_depth(struct pci_dev *d) >> { return -ENODEV; } >> -- >> 2.38.1 >> >> >> _______________________________________________ >> linux-arm-kernel mailing list >> linux-arm-kernel@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Mon, Mar 13, 2023 at 03:30:30PM -0700, Sathyanarayanan Kuppuswamy wrote: > On 3/13/23 2:12 PM, Bjorn Helgaas wrote: > > On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: > >> As per PCI specification (PCI Express Base Specification Revision > >> 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled > >> independently for ATS capability, however the STU(Smallest Translation > >> Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and > >> the associated PF's value applies to VFs. > >> > >> In the current code, the STU is being configured while enabling the PF ATS. > >> Hence, it is not able to enable ATS for VFs, if it is not enabled on the > >> associated PF already. > >> > >> Adding a function pci_ats_stu_configure(), which can be called to > >> configure the STU during PF enumeration. > >> Latter enumerations of VFs can successfully enable ATS independently. > >> + * pci_ats_stu_configure - Configure STU of a PF. > >> + * @dev: the PCI device > >> + * @ps: the IOMMU page shift > >> + * > >> + * Returns 0 on success, or negative on failure. > >> + */ > >> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) > >> +{ > >> + u16 ctrl; > >> + > >> + if (dev->ats_enabled || dev->is_virtfn) > >> + return 0; > > We don't have any checks for the PF case here. That means you can > re-configure the STU as many times as you want until ATS is enabled > in PF. So, if there are active VFs which uses this STU, can PF > re-configure the STU at will? Really good question! I withdraw my ack until this is resolved. I don't think we want PFs changing STU behind the back of VFs. Bjorn
On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: > Hi Kulkarni, > > On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>> As per PCI specification (PCI Express Base Specification Revision >>> 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled >>> independently for ATS capability, however the STU(Smallest Translation >>> Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and >>> the associated PF's value applies to VFs. >>> >>> In the current code, the STU is being configured while enabling the PF ATS. >>> Hence, it is not able to enable ATS for VFs, if it is not enabled on the >>> associated PF already. >>> >>> Adding a function pci_ats_stu_configure(), which can be called to >>> configure the STU during PF enumeration. >>> Latter enumerations of VFs can successfully enable ATS independently. >> >> s/STU(Smallest/STU (Smallest/ (add space before paren) >> s/Adding a function pci_ats_stu_configure()/Add pci_ats_stu_configure()/ >> s/Latter/Subsequent/ >> >> Add blank line between paragraphs (it looks like "Latter enumerations" >> is intended to start a new paragraph). >> >>> Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> >> >> Acked-by: Bjorn Helgaas <bhelgaas@google.com> >> >> Given an ack for the IOMMU patch, I'd be happy to merge both (and I >> can do the commit log tweaks); just let me know. >> >> One comment/question below. >> >>> --- >>> drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++-- >>> include/linux/pci-ats.h | 3 +++ >>> 2 files changed, 34 insertions(+), 2 deletions(-) >>> >>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>> index f9cc2e10b676..1611bfa1d5da 100644 >>> --- a/drivers/pci/ats.c >>> +++ b/drivers/pci/ats.c >>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>> } >>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>> >>> +/** >>> + * pci_ats_stu_configure - Configure STU of a PF. >>> + * @dev: the PCI device >>> + * @ps: the IOMMU page shift >>> + * >>> + * Returns 0 on success, or negative on failure. >>> + */ >>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>> +{ >>> + u16 ctrl; >>> + >>> + if (dev->ats_enabled || dev->is_virtfn) >>> + return 0; >> >> I might return an error for the VF case on the assumption that it's >> likely an error in the caller. I guess one could argue that it >> simplifies the caller if it doesn't have to check for PF vs VF. But >> the fact that STU is shared between PF and VFs is an important part of >> understanding how ATS works, so the caller should be aware of the >> distinction anyway. > > I have already asked this question. But let me repeat it. > > We don't have any checks for the PF case here. That means you can re-configure > the STU as many times as you want until ATS is enabled in PF. So, if there are > active VFs which uses this STU, can PF re-configure the STU at will? > IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c index 1611bfa1d5da..f7bb01068e18 100644 --- a/drivers/pci/ats.c +++ b/drivers/pci/ats.c @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) if (dev->ats_enabled || dev->is_virtfn) return 0; + /* Configured already */ + if (dev->ats_stu) + return 0; + if (!pci_ats_supported(dev)) return -EINVAL; >> >>> + >>> + if (!pci_ats_supported(dev)) >>> + return -EINVAL; >>> + >>> + if (ps < PCI_ATS_MIN_STU) >>> + return -EINVAL; >>> + >>> + dev->ats_stu = ps; >>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>> + >>> + return 0; >>> +} >>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >>> + >>> /** >>> * pci_enable_ats - enable the ATS capability >>> * @dev: the PCI device >>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >>> return -EINVAL; >>> >>> /* >>> - * Note that enabling ATS on a VF fails unless it's already enabled >>> - * with the same STU on the PF. >>> + * Note that enabling ATS on a VF fails unless it's already >>> + * configured with the same STU on the PF. >>> */ >>> ctrl = PCI_ATS_CTRL_ENABLE; >>> if (dev->is_virtfn) { >>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >>> index df54cd5b15db..7d62a92aaf23 100644 >>> --- a/include/linux/pci-ats.h >>> +++ b/include/linux/pci-ats.h >>> @@ -8,6 +8,7 @@ >>> /* Address Translation Service */ >>> bool pci_ats_supported(struct pci_dev *dev); >>> int pci_enable_ats(struct pci_dev *dev, int ps); >>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >>> void pci_disable_ats(struct pci_dev *dev); >>> int pci_ats_queue_depth(struct pci_dev *dev); >>> int pci_ats_page_aligned(struct pci_dev *dev); >>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >>> { return false; } >>> static inline int pci_enable_ats(struct pci_dev *d, int ps) >>> { return -ENODEV; } >>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >>> +{ return -ENODEV; } >>> static inline void pci_disable_ats(struct pci_dev *d) { } >>> static inline int pci_ats_queue_depth(struct pci_dev *d) >>> { return -ENODEV; } >>> -- >>> 2.38.1 >>> >>> >>> _______________________________________________ >>> linux-arm-kernel mailing list >>> linux-arm-kernel@lists.infradead.org >>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel > Thanks, Ganapat
On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: > > > On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: >> Hi Kulkarni, >> >> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>>> As per PCI specification (PCI Express Base Specification Revision >>>> 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled >>>> independently for ATS capability, however the STU(Smallest Translation >>>> Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and >>>> the associated PF's value applies to VFs. >>>> >>>> In the current code, the STU is being configured while enabling the PF ATS. >>>> Hence, it is not able to enable ATS for VFs, if it is not enabled on the >>>> associated PF already. >>>> >>>> Adding a function pci_ats_stu_configure(), which can be called to >>>> configure the STU during PF enumeration. >>>> Latter enumerations of VFs can successfully enable ATS independently. >>> >>> s/STU(Smallest/STU (Smallest/ (add space before paren) >>> s/Adding a function pci_ats_stu_configure()/Add pci_ats_stu_configure()/ >>> s/Latter/Subsequent/ >>> >>> Add blank line between paragraphs (it looks like "Latter enumerations" >>> is intended to start a new paragraph). >>> >>>> Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> >>> >>> Acked-by: Bjorn Helgaas <bhelgaas@google.com> >>> >>> Given an ack for the IOMMU patch, I'd be happy to merge both (and I >>> can do the commit log tweaks); just let me know. >>> >>> One comment/question below. >>> >>>> --- >>>> drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++-- >>>> include/linux/pci-ats.h | 3 +++ >>>> 2 files changed, 34 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>>> index f9cc2e10b676..1611bfa1d5da 100644 >>>> --- a/drivers/pci/ats.c >>>> +++ b/drivers/pci/ats.c >>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>>> } >>>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>>> +/** >>>> + * pci_ats_stu_configure - Configure STU of a PF. >>>> + * @dev: the PCI device >>>> + * @ps: the IOMMU page shift >>>> + * >>>> + * Returns 0 on success, or negative on failure. >>>> + */ >>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>> +{ >>>> + u16 ctrl; >>>> + >>>> + if (dev->ats_enabled || dev->is_virtfn) >>>> + return 0; >>> >>> I might return an error for the VF case on the assumption that it's >>> likely an error in the caller. I guess one could argue that it >>> simplifies the caller if it doesn't have to check for PF vs VF. But >>> the fact that STU is shared between PF and VFs is an important part of >>> understanding how ATS works, so the caller should be aware of the >>> distinction anyway. >> >> I have already asked this question. But let me repeat it. >> >> We don't have any checks for the PF case here. That means you can re-configure >> the STU as many times as you want until ATS is enabled in PF. So, if there are >> active VFs which uses this STU, can PF re-configure the STU at will? >> > > IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. > > diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c > index 1611bfa1d5da..f7bb01068e18 100644 > --- a/drivers/pci/ats.c > +++ b/drivers/pci/ats.c > @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) > if (dev->ats_enabled || dev->is_virtfn) > return 0; > > + /* Configured already */ > + if (dev->ats_stu) > + return 0; > + Theoretically, you can re-configure STU as long as no one is using it. Instead of this check, is there a way to check whether there are active VMs which enables ATS? > if (!pci_ats_supported(dev)) > return -EINVAL; >>> >>>> + >>>> + if (!pci_ats_supported(dev)) >>>> + return -EINVAL; >>>> + >>>> + if (ps < PCI_ATS_MIN_STU) >>>> + return -EINVAL; >>>> + >>>> + dev->ats_stu = ps; >>>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >>>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>>> + >>>> + return 0; >>>> +} >>>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >>>> + >>>> /** >>>> * pci_enable_ats - enable the ATS capability >>>> * @dev: the PCI device >>>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >>>> return -EINVAL; >>>> /* >>>> - * Note that enabling ATS on a VF fails unless it's already enabled >>>> - * with the same STU on the PF. >>>> + * Note that enabling ATS on a VF fails unless it's already >>>> + * configured with the same STU on the PF. >>>> */ >>>> ctrl = PCI_ATS_CTRL_ENABLE; >>>> if (dev->is_virtfn) { >>>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >>>> index df54cd5b15db..7d62a92aaf23 100644 >>>> --- a/include/linux/pci-ats.h >>>> +++ b/include/linux/pci-ats.h >>>> @@ -8,6 +8,7 @@ >>>> /* Address Translation Service */ >>>> bool pci_ats_supported(struct pci_dev *dev); >>>> int pci_enable_ats(struct pci_dev *dev, int ps); >>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >>>> void pci_disable_ats(struct pci_dev *dev); >>>> int pci_ats_queue_depth(struct pci_dev *dev); >>>> int pci_ats_page_aligned(struct pci_dev *dev); >>>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >>>> { return false; } >>>> static inline int pci_enable_ats(struct pci_dev *d, int ps) >>>> { return -ENODEV; } >>>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >>>> +{ return -ENODEV; } >>>> static inline void pci_disable_ats(struct pci_dev *d) { } >>>> static inline int pci_ats_queue_depth(struct pci_dev *d) >>>> { return -ENODEV; } >>>> -- >>>> 2.38.1 >>>> >>>> >>>> _______________________________________________ >>>> linux-arm-kernel mailing list >>>> linux-arm-kernel@lists.infradead.org >>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel >> > > Thanks, > Ganapat >
Hi Sathyanarayanan, On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: > > > On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: >> >> >> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: >>> Hi Kulkarni, >>> >>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>>>> As per PCI specification (PCI Express Base Specification Revision >>>>> 6.0, Section 10.5) both PF and VFs of a PCI EP are permitted to be enabled >>>>> independently for ATS capability, however the STU(Smallest Translation >>>>> Unit) is shared between PF and VFs. For VFs, it is hardwired to Zero and >>>>> the associated PF's value applies to VFs. >>>>> >>>>> In the current code, the STU is being configured while enabling the PF ATS. >>>>> Hence, it is not able to enable ATS for VFs, if it is not enabled on the >>>>> associated PF already. >>>>> >>>>> Adding a function pci_ats_stu_configure(), which can be called to >>>>> configure the STU during PF enumeration. >>>>> Latter enumerations of VFs can successfully enable ATS independently. >>>> >>>> s/STU(Smallest/STU (Smallest/ (add space before paren) >>>> s/Adding a function pci_ats_stu_configure()/Add pci_ats_stu_configure()/ >>>> s/Latter/Subsequent/ >>>> >>>> Add blank line between paragraphs (it looks like "Latter enumerations" >>>> is intended to start a new paragraph). >>>> >>>>> Signed-off-by: Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com> >>>> >>>> Acked-by: Bjorn Helgaas <bhelgaas@google.com> >>>> >>>> Given an ack for the IOMMU patch, I'd be happy to merge both (and I >>>> can do the commit log tweaks); just let me know. >>>> >>>> One comment/question below. >>>> >>>>> --- >>>>> drivers/pci/ats.c | 33 +++++++++++++++++++++++++++++++-- >>>>> include/linux/pci-ats.h | 3 +++ >>>>> 2 files changed, 34 insertions(+), 2 deletions(-) >>>>> >>>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>>>> index f9cc2e10b676..1611bfa1d5da 100644 >>>>> --- a/drivers/pci/ats.c >>>>> +++ b/drivers/pci/ats.c >>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>>>> } >>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>>>> +/** >>>>> + * pci_ats_stu_configure - Configure STU of a PF. >>>>> + * @dev: the PCI device >>>>> + * @ps: the IOMMU page shift >>>>> + * >>>>> + * Returns 0 on success, or negative on failure. >>>>> + */ >>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>> +{ >>>>> + u16 ctrl; >>>>> + >>>>> + if (dev->ats_enabled || dev->is_virtfn) >>>>> + return 0; >>>> >>>> I might return an error for the VF case on the assumption that it's >>>> likely an error in the caller. I guess one could argue that it >>>> simplifies the caller if it doesn't have to check for PF vs VF. But >>>> the fact that STU is shared between PF and VFs is an important part of >>>> understanding how ATS works, so the caller should be aware of the >>>> distinction anyway. >>> >>> I have already asked this question. But let me repeat it. >>> >>> We don't have any checks for the PF case here. That means you can re-configure >>> the STU as many times as you want until ATS is enabled in PF. So, if there are >>> active VFs which uses this STU, can PF re-configure the STU at will? >>> >> >> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. >> >> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >> index 1611bfa1d5da..f7bb01068e18 100644 >> --- a/drivers/pci/ats.c >> +++ b/drivers/pci/ats.c >> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) >> if (dev->ats_enabled || dev->is_virtfn) >> return 0; >> >> + /* Configured already */ >> + if (dev->ats_stu) >> + return 0; >> + > > Theoretically, you can re-configure STU as long as no one is using it. Instead of this check, is > there a way to check whether there are active VMs which enables ATS? Yes I agree, there is no limitation on how many times you write STU bits, but practically it is happening while PF is enumerated. The usage of function pci_ats_stu_configure is almost similar(subset) to pci_enable_ats and only difference is one does ATS enable + STU program and another does only STU program. IMO, I dont think, there is any need to find how many active VMs with attached VFs and it is not done for pci_enable_ats as well. Also the caller has the requirement to call either pci_ats_stu_configure or pci_enable_ats while enumerating the PF. > >> if (!pci_ats_supported(dev)) >> return -EINVAL; >>>> >>>>> + >>>>> + if (!pci_ats_supported(dev)) >>>>> + return -EINVAL; >>>>> + >>>>> + if (ps < PCI_ATS_MIN_STU) >>>>> + return -EINVAL; >>>>> + >>>>> + dev->ats_stu = ps; >>>>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>>>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >>>>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>>>> + >>>>> + return 0; >>>>> +} >>>>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >>>>> + >>>>> /** >>>>> * pci_enable_ats - enable the ATS capability >>>>> * @dev: the PCI device >>>>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >>>>> return -EINVAL; >>>>> /* >>>>> - * Note that enabling ATS on a VF fails unless it's already enabled >>>>> - * with the same STU on the PF. >>>>> + * Note that enabling ATS on a VF fails unless it's already >>>>> + * configured with the same STU on the PF. >>>>> */ >>>>> ctrl = PCI_ATS_CTRL_ENABLE; >>>>> if (dev->is_virtfn) { >>>>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >>>>> index df54cd5b15db..7d62a92aaf23 100644 >>>>> --- a/include/linux/pci-ats.h >>>>> +++ b/include/linux/pci-ats.h >>>>> @@ -8,6 +8,7 @@ >>>>> /* Address Translation Service */ >>>>> bool pci_ats_supported(struct pci_dev *dev); >>>>> int pci_enable_ats(struct pci_dev *dev, int ps); >>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >>>>> void pci_disable_ats(struct pci_dev *dev); >>>>> int pci_ats_queue_depth(struct pci_dev *dev); >>>>> int pci_ats_page_aligned(struct pci_dev *dev); >>>>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >>>>> { return false; } >>>>> static inline int pci_enable_ats(struct pci_dev *d, int ps) >>>>> { return -ENODEV; } >>>>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >>>>> +{ return -ENODEV; } >>>>> static inline void pci_disable_ats(struct pci_dev *d) { } >>>>> static inline int pci_ats_queue_depth(struct pci_dev *d) >>>>> { return -ENODEV; } >>>>> -- >>>>> 2.38.1 >>>>> >>>>> >>>>> _______________________________________________ >>>>> linux-arm-kernel mailing list >>>>> linux-arm-kernel@lists.infradead.org >>>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel >>> >> >> Thanks, >> Ganapat >> > Thanks, Ganapat
On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: > On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: > > On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: > > > On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: > > > > On 3/13/23 2:12 PM, Bjorn Helgaas wrote: > > > > > On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: > > > > > > As per PCI specification (PCI Express Base Specification > > > > > > Revision 6.0, Section 10.5) both PF and VFs of a PCI EP > > > > > > are permitted to be enabled independently for ATS > > > > > > capability, however the STU(Smallest Translation Unit) is > > > > > > shared between PF and VFs. For VFs, it is hardwired to > > > > > > Zero and the associated PF's value applies to VFs. > > > > > > > > > > > > In the current code, the STU is being configured while > > > > > > enabling the PF ATS. Hence, it is not able to enable ATS > > > > > > for VFs, if it is not enabled on the associated PF > > > > > > already. > > > > > > > > > > > > Adding a function pci_ats_stu_configure(), which can be > > > > > > called to configure the STU during PF enumeration. Latter > > > > > > enumerations of VFs can successfully enable ATS > > > > > > independently. > > > > > > @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) > > > > > > } > > > > > > EXPORT_SYMBOL_GPL(pci_ats_supported); > > > > > > +/** > > > > > > + * pci_ats_stu_configure - Configure STU of a PF. > > > > > > + * @dev: the PCI device > > > > > > + * @ps: the IOMMU page shift > > > > > > + * > > > > > > + * Returns 0 on success, or negative on failure. > > > > > > + */ > > > > > > +int pci_ats_stu_configure(struct pci_dev *dev, int ps) > > > > > > +{ > > > > > > + u16 ctrl; > > > > > > + > > > > > > + if (dev->ats_enabled || dev->is_virtfn) > > > > > > + return 0; > > > > > > > > > > I might return an error for the VF case on the assumption > > > > > that it's likely an error in the caller. I guess one could > > > > > argue that it simplifies the caller if it doesn't have to > > > > > check for PF vs VF. But the fact that STU is shared between > > > > > PF and VFs is an important part of understanding how ATS > > > > > works, so the caller should be aware of the distinction > > > > > anyway. > > > > > > > > I have already asked this question. But let me repeat it. > > > > > > > > We don't have any checks for the PF case here. That means you > > > > can re-configure the STU as many times as you want until ATS > > > > is enabled in PF. So, if there are active VFs which uses this > > > > STU, can PF re-configure the STU at will? > > > > > > IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. > > > > > > diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c > > > index 1611bfa1d5da..f7bb01068e18 100644 > > > --- a/drivers/pci/ats.c > > > +++ b/drivers/pci/ats.c > > > @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) > > > if (dev->ats_enabled || dev->is_virtfn) > > > return 0; > > > > > > + /* Configured already */ > > > + if (dev->ats_stu) > > > + return 0; > > > > Theoretically, you can re-configure STU as long as no one is using > > it. Instead of this check, is there a way to check whether there > > are active VMs which enables ATS? > > Yes I agree, there is no limitation on how many times you write STU > bits, but practically it is happening while PF is enumerated. > > The usage of function pci_ats_stu_configure is almost > similar(subset) to pci_enable_ats and only difference is one does > ATS enable + STU program and another does only STU program. What would you think of removing the STU update feature from pci_enable_ats() so it always fails if pci_ats_stu_configure() has not been called, even when called on the PF, e.g., if (ps != pci_physfn(dev)->ats_stu) return -EINVAL; pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); ctrl |= PCI_ATS_CTRL_ENABLE; pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); Would probably also have to set "dev->ats_stu = 0" in pci_disable_ats() to allow the possibility of calling pci_ats_stu_configure() again. > IMO, I dont think, there is any need to find how many active VMs > with attached VFs and it is not done for pci_enable_ats as well. Enabling or disabling ATS in a PF or VF has no effect on other functions. But changing STU while a VF has ATS enabled would definitely break any user of that VF, so if it's practical to verify that no VFs have ATS enabled, I think we should. > Also the caller has the requirement to call either > pci_ats_stu_configure or pci_enable_ats while enumerating the PF. > > > > if (!pci_ats_supported(dev)) > > > return -EINVAL; > > > > > > > > > > > + > > > > > > + if (!pci_ats_supported(dev)) > > > > > > + return -EINVAL; > > > > > > + > > > > > > + if (ps < PCI_ATS_MIN_STU) > > > > > > + return -EINVAL; > > > > > > + > > > > > > + dev->ats_stu = ps; > > > > > > + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); > > > > > > + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); > > > > > > + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); > > > > > > + > > > > > > + return 0; > > > > > > +} > > > > > > +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); > > > > > > + > > > > > > /** > > > > > > * pci_enable_ats - enable the ATS capability > > > > > > * @dev: the PCI device > > > > > > @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) > > > > > > return -EINVAL; > > > > > > /* > > > > > > - * Note that enabling ATS on a VF fails unless it's already enabled > > > > > > - * with the same STU on the PF. > > > > > > + * Note that enabling ATS on a VF fails unless it's already > > > > > > + * configured with the same STU on the PF. > > > > > > */ > > > > > > ctrl = PCI_ATS_CTRL_ENABLE; > > > > > > if (dev->is_virtfn) { > > > > > > diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h > > > > > > index df54cd5b15db..7d62a92aaf23 100644 > > > > > > --- a/include/linux/pci-ats.h > > > > > > +++ b/include/linux/pci-ats.h > > > > > > @@ -8,6 +8,7 @@ > > > > > > /* Address Translation Service */ > > > > > > bool pci_ats_supported(struct pci_dev *dev); > > > > > > int pci_enable_ats(struct pci_dev *dev, int ps); > > > > > > +int pci_ats_stu_configure(struct pci_dev *dev, int ps); > > > > > > void pci_disable_ats(struct pci_dev *dev); > > > > > > int pci_ats_queue_depth(struct pci_dev *dev); > > > > > > int pci_ats_page_aligned(struct pci_dev *dev); > > > > > > @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) > > > > > > { return false; } > > > > > > static inline int pci_enable_ats(struct pci_dev *d, int ps) > > > > > > { return -ENODEV; } > > > > > > +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) > > > > > > +{ return -ENODEV; } > > > > > > static inline void pci_disable_ats(struct pci_dev *d) { } > > > > > > static inline int pci_ats_queue_depth(struct pci_dev *d) > > > > > > { return -ENODEV; }
On 3/14/23 9:02 AM, Bjorn Helgaas wrote: > On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: >> On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: >>> On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: >>>> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: >>>>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >>>>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>>>>>> As per PCI specification (PCI Express Base Specification >>>>>>> Revision 6.0, Section 10.5) both PF and VFs of a PCI EP >>>>>>> are permitted to be enabled independently for ATS >>>>>>> capability, however the STU(Smallest Translation Unit) is >>>>>>> shared between PF and VFs. For VFs, it is hardwired to >>>>>>> Zero and the associated PF's value applies to VFs. >>>>>>> >>>>>>> In the current code, the STU is being configured while >>>>>>> enabling the PF ATS. Hence, it is not able to enable ATS >>>>>>> for VFs, if it is not enabled on the associated PF >>>>>>> already. >>>>>>> >>>>>>> Adding a function pci_ats_stu_configure(), which can be >>>>>>> called to configure the STU during PF enumeration. Latter >>>>>>> enumerations of VFs can successfully enable ATS >>>>>>> independently. > >>>>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>>>>>> } >>>>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>>>>>> +/** >>>>>>> + * pci_ats_stu_configure - Configure STU of a PF. >>>>>>> + * @dev: the PCI device >>>>>>> + * @ps: the IOMMU page shift >>>>>>> + * >>>>>>> + * Returns 0 on success, or negative on failure. >>>>>>> + */ >>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>>> +{ >>>>>>> + u16 ctrl; >>>>>>> + >>>>>>> + if (dev->ats_enabled || dev->is_virtfn) >>>>>>> + return 0; >>>>>> >>>>>> I might return an error for the VF case on the assumption >>>>>> that it's likely an error in the caller. I guess one could >>>>>> argue that it simplifies the caller if it doesn't have to >>>>>> check for PF vs VF. But the fact that STU is shared between >>>>>> PF and VFs is an important part of understanding how ATS >>>>>> works, so the caller should be aware of the distinction >>>>>> anyway. >>>>> >>>>> I have already asked this question. But let me repeat it. >>>>> >>>>> We don't have any checks for the PF case here. That means you >>>>> can re-configure the STU as many times as you want until ATS >>>>> is enabled in PF. So, if there are active VFs which uses this >>>>> STU, can PF re-configure the STU at will? >>>> >>>> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. >>>> >>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>>> index 1611bfa1d5da..f7bb01068e18 100644 >>>> --- a/drivers/pci/ats.c >>>> +++ b/drivers/pci/ats.c >>>> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>> if (dev->ats_enabled || dev->is_virtfn) >>>> return 0; >>>> >>>> + /* Configured already */ >>>> + if (dev->ats_stu) >>>> + return 0; >>> >>> Theoretically, you can re-configure STU as long as no one is using >>> it. Instead of this check, is there a way to check whether there >>> are active VMs which enables ATS? >> >> Yes I agree, there is no limitation on how many times you write STU >> bits, but practically it is happening while PF is enumerated. >> >> The usage of function pci_ats_stu_configure is almost >> similar(subset) to pci_enable_ats and only difference is one does >> ATS enable + STU program and another does only STU program. > > What would you think of removing the STU update feature from > pci_enable_ats() so it always fails if pci_ats_stu_configure() has not > been called, even when called on the PF, e.g., > > if (ps != pci_physfn(dev)->ats_stu) > return -EINVAL; If we are removing the STU update from pci_enable_ats(), why even allow passing "ps (page shift)" parameter? IMO, we can assume that for STU reconfigure, users will call pci_ats_stu_configure(). Since zero is a valid STU, enabling ATS can be decoupled from STU update. > > pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); > ctrl |= PCI_ATS_CTRL_ENABLE; > pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); > > Would probably also have to set "dev->ats_stu = 0" in > pci_disable_ats() to allow the possibility of calling > pci_ats_stu_configure() again. > >> IMO, I dont think, there is any need to find how many active VMs >> with attached VFs and it is not done for pci_enable_ats as well. > > Enabling or disabling ATS in a PF or VF has no effect on other > functions. > > But changing STU while a VF has ATS enabled would definitely break any > user of that VF, so if it's practical to verify that no VFs have ATS > enabled, I think we should. I also think it is better to check for a ats_enabled status of VF before configuring the STU. May be something like below (untested), static int is_ats_enabled_in_vf(struct pci_dev *dev) { struct pci_sriov *iov = dev->sriov; struct pci_dev *vdev; if (dev->is_virtfn) return -EINVAL; for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) { vdev = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), pci_iov_virtfn_bus(dev, i), pci_iov_virtfn_devfn(dev, i)); if (vdev && vdev->ats_enabled) return 1; } return 0; } int pci_ats_stu_configure(struct pci_dev *dev, int ps) { ... if (is_ats_enabled_in_vf(dev)) return -EBUSY; > >> Also the caller has the requirement to call either >> pci_ats_stu_configure or pci_enable_ats while enumerating the PF. >> >>>> if (!pci_ats_supported(dev)) >>>> return -EINVAL; >>>>>> >>>>>>> + >>>>>>> + if (!pci_ats_supported(dev)) >>>>>>> + return -EINVAL; >>>>>>> + >>>>>>> + if (ps < PCI_ATS_MIN_STU) >>>>>>> + return -EINVAL; >>>>>>> + >>>>>>> + dev->ats_stu = ps; >>>>>>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>>>>>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >>>>>>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>>>>>> + >>>>>>> + return 0; >>>>>>> +} >>>>>>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >>>>>>> + >>>>>>> /** >>>>>>> * pci_enable_ats - enable the ATS capability >>>>>>> * @dev: the PCI device >>>>>>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >>>>>>> return -EINVAL; >>>>>>> /* >>>>>>> - * Note that enabling ATS on a VF fails unless it's already enabled >>>>>>> - * with the same STU on the PF. >>>>>>> + * Note that enabling ATS on a VF fails unless it's already >>>>>>> + * configured with the same STU on the PF. >>>>>>> */ >>>>>>> ctrl = PCI_ATS_CTRL_ENABLE; >>>>>>> if (dev->is_virtfn) { >>>>>>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >>>>>>> index df54cd5b15db..7d62a92aaf23 100644 >>>>>>> --- a/include/linux/pci-ats.h >>>>>>> +++ b/include/linux/pci-ats.h >>>>>>> @@ -8,6 +8,7 @@ >>>>>>> /* Address Translation Service */ >>>>>>> bool pci_ats_supported(struct pci_dev *dev); >>>>>>> int pci_enable_ats(struct pci_dev *dev, int ps); >>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >>>>>>> void pci_disable_ats(struct pci_dev *dev); >>>>>>> int pci_ats_queue_depth(struct pci_dev *dev); >>>>>>> int pci_ats_page_aligned(struct pci_dev *dev); >>>>>>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >>>>>>> { return false; } >>>>>>> static inline int pci_enable_ats(struct pci_dev *d, int ps) >>>>>>> { return -ENODEV; } >>>>>>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >>>>>>> +{ return -ENODEV; } >>>>>>> static inline void pci_disable_ats(struct pci_dev *d) { } >>>>>>> static inline int pci_ats_queue_depth(struct pci_dev *d) >>>>>>> { return -ENODEV; }
On Tue, Mar 14, 2023 at 09:50:06AM -0700, Sathyanarayanan Kuppuswamy wrote: > On 3/14/23 9:02 AM, Bjorn Helgaas wrote: > > On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: > >> On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: > >>> On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: > >>>> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: > >>>>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: > >>>>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: > >>>>>>> As per PCI specification (PCI Express Base Specification > >>>>>>> Revision 6.0, Section 10.5) both PF and VFs of a PCI EP > >>>>>>> are permitted to be enabled independently for ATS > >>>>>>> capability, however the STU(Smallest Translation Unit) is > >>>>>>> shared between PF and VFs. For VFs, it is hardwired to > >>>>>>> Zero and the associated PF's value applies to VFs. > >>>>>>> > >>>>>>> In the current code, the STU is being configured while > >>>>>>> enabling the PF ATS. Hence, it is not able to enable ATS > >>>>>>> for VFs, if it is not enabled on the associated PF > >>>>>>> already. > >>>>>>> > >>>>>>> Adding a function pci_ats_stu_configure(), which can be > >>>>>>> called to configure the STU during PF enumeration. Latter > >>>>>>> enumerations of VFs can successfully enable ATS > >>>>>>> independently. > > > >>>>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) > >>>>>>> } > >>>>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); > >>>>>>> +/** > >>>>>>> + * pci_ats_stu_configure - Configure STU of a PF. > >>>>>>> + * @dev: the PCI device > >>>>>>> + * @ps: the IOMMU page shift > >>>>>>> + * > >>>>>>> + * Returns 0 on success, or negative on failure. > >>>>>>> + */ > >>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) > >>>>>>> +{ > >>>>>>> + u16 ctrl; > >>>>>>> + > >>>>>>> + if (dev->ats_enabled || dev->is_virtfn) > >>>>>>> + return 0; > >>>>>> > >>>>>> I might return an error for the VF case on the assumption > >>>>>> that it's likely an error in the caller. I guess one could > >>>>>> argue that it simplifies the caller if it doesn't have to > >>>>>> check for PF vs VF. But the fact that STU is shared between > >>>>>> PF and VFs is an important part of understanding how ATS > >>>>>> works, so the caller should be aware of the distinction > >>>>>> anyway. > >>>>> > >>>>> I have already asked this question. But let me repeat it. > >>>>> > >>>>> We don't have any checks for the PF case here. That means you > >>>>> can re-configure the STU as many times as you want until ATS > >>>>> is enabled in PF. So, if there are active VFs which uses this > >>>>> STU, can PF re-configure the STU at will? > >>>> > >>>> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. > >>>> > >>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c > >>>> index 1611bfa1d5da..f7bb01068e18 100644 > >>>> --- a/drivers/pci/ats.c > >>>> +++ b/drivers/pci/ats.c > >>>> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) > >>>> if (dev->ats_enabled || dev->is_virtfn) > >>>> return 0; > >>>> > >>>> + /* Configured already */ > >>>> + if (dev->ats_stu) > >>>> + return 0; > >>> > >>> Theoretically, you can re-configure STU as long as no one is using > >>> it. Instead of this check, is there a way to check whether there > >>> are active VMs which enables ATS? > >> > >> Yes I agree, there is no limitation on how many times you write STU > >> bits, but practically it is happening while PF is enumerated. > >> > >> The usage of function pci_ats_stu_configure is almost > >> similar(subset) to pci_enable_ats and only difference is one does > >> ATS enable + STU program and another does only STU program. > > > > What would you think of removing the STU update feature from > > pci_enable_ats() so it always fails if pci_ats_stu_configure() has not > > been called, even when called on the PF, e.g., > > > > if (ps != pci_physfn(dev)->ats_stu) > > return -EINVAL; > > If we are removing the STU update from pci_enable_ats(), why > even allow passing "ps (page shift)" parameter? IMO, we can assume that > for STU reconfigure, users will call pci_ats_stu_configure(). The reason to pass "ps" would be to verify that the STU the caller plans to use matches the actual STU. > Since zero is a valid STU, enabling ATS can be decoupled from STU > update. > > > pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); > > ctrl |= PCI_ATS_CTRL_ENABLE; > > pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); > > > > Would probably also have to set "dev->ats_stu = 0" in > > pci_disable_ats() to allow the possibility of calling > > pci_ats_stu_configure() again. > > > >> IMO, I dont think, there is any need to find how many active VMs > >> with attached VFs and it is not done for pci_enable_ats as well. > > > > Enabling or disabling ATS in a PF or VF has no effect on other > > functions. > > > > But changing STU while a VF has ATS enabled would definitely break any > > user of that VF, so if it's practical to verify that no VFs have ATS > > enabled, I think we should. > > I also think it is better to check for a ats_enabled status of VF before > configuring the STU. > > May be something like below (untested), > > static int is_ats_enabled_in_vf(struct pci_dev *dev) > { > struct pci_sriov *iov = dev->sriov; > struct pci_dev *vdev; > > if (dev->is_virtfn) > return -EINVAL; > > for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) { > vdev = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), > pci_iov_virtfn_bus(dev, i), > pci_iov_virtfn_devfn(dev, i)); I would try hard to avoid pci_get_domain_bus_and_slot(). That's expensive (searches *all* PCI devs with for_each_pci_dev()) and requires dealing with reference counts. Maybe an atomic count in the PF of VFs with ATS enabled. > if (vdev && vdev->ats_enabled) > return 1; > } > > return 0; > > } > > int pci_ats_stu_configure(struct pci_dev *dev, int ps) > { > ... > if (is_ats_enabled_in_vf(dev)) > return -EBUSY; > > > > >> Also the caller has the requirement to call either > >> pci_ats_stu_configure or pci_enable_ats while enumerating the PF. > >> > >>>> if (!pci_ats_supported(dev)) > >>>> return -EINVAL; > >>>>>> > >>>>>>> + > >>>>>>> + if (!pci_ats_supported(dev)) > >>>>>>> + return -EINVAL; > >>>>>>> + > >>>>>>> + if (ps < PCI_ATS_MIN_STU) > >>>>>>> + return -EINVAL; > >>>>>>> + > >>>>>>> + dev->ats_stu = ps; > >>>>>>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); > >>>>>>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); > >>>>>>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); > >>>>>>> + > >>>>>>> + return 0; > >>>>>>> +} > >>>>>>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); > >>>>>>> + > >>>>>>> /** > >>>>>>> * pci_enable_ats - enable the ATS capability > >>>>>>> * @dev: the PCI device > >>>>>>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) > >>>>>>> return -EINVAL; > >>>>>>> /* > >>>>>>> - * Note that enabling ATS on a VF fails unless it's already enabled > >>>>>>> - * with the same STU on the PF. > >>>>>>> + * Note that enabling ATS on a VF fails unless it's already > >>>>>>> + * configured with the same STU on the PF. > >>>>>>> */ > >>>>>>> ctrl = PCI_ATS_CTRL_ENABLE; > >>>>>>> if (dev->is_virtfn) { > >>>>>>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h > >>>>>>> index df54cd5b15db..7d62a92aaf23 100644 > >>>>>>> --- a/include/linux/pci-ats.h > >>>>>>> +++ b/include/linux/pci-ats.h > >>>>>>> @@ -8,6 +8,7 @@ > >>>>>>> /* Address Translation Service */ > >>>>>>> bool pci_ats_supported(struct pci_dev *dev); > >>>>>>> int pci_enable_ats(struct pci_dev *dev, int ps); > >>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); > >>>>>>> void pci_disable_ats(struct pci_dev *dev); > >>>>>>> int pci_ats_queue_depth(struct pci_dev *dev); > >>>>>>> int pci_ats_page_aligned(struct pci_dev *dev); > >>>>>>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) > >>>>>>> { return false; } > >>>>>>> static inline int pci_enable_ats(struct pci_dev *d, int ps) > >>>>>>> { return -ENODEV; } > >>>>>>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) > >>>>>>> +{ return -ENODEV; } > >>>>>>> static inline void pci_disable_ats(struct pci_dev *d) { } > >>>>>>> static inline int pci_ats_queue_depth(struct pci_dev *d) > >>>>>>> { return -ENODEV; } > > -- > Sathyanarayanan Kuppuswamy > Linux Kernel Developer > > _______________________________________________ > linux-arm-kernel mailing list > linux-arm-kernel@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On 14-03-2023 10:40 pm, Bjorn Helgaas wrote: > On Tue, Mar 14, 2023 at 09:50:06AM -0700, Sathyanarayanan Kuppuswamy wrote: >> On 3/14/23 9:02 AM, Bjorn Helgaas wrote: >>> On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: >>>> On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: >>>>> On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: >>>>>> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: >>>>>>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >>>>>>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>>>>>>>> As per PCI specification (PCI Express Base Specification >>>>>>>>> Revision 6.0, Section 10.5) both PF and VFs of a PCI EP >>>>>>>>> are permitted to be enabled independently for ATS >>>>>>>>> capability, however the STU(Smallest Translation Unit) is >>>>>>>>> shared between PF and VFs. For VFs, it is hardwired to >>>>>>>>> Zero and the associated PF's value applies to VFs. >>>>>>>>> >>>>>>>>> In the current code, the STU is being configured while >>>>>>>>> enabling the PF ATS. Hence, it is not able to enable ATS >>>>>>>>> for VFs, if it is not enabled on the associated PF >>>>>>>>> already. >>>>>>>>> >>>>>>>>> Adding a function pci_ats_stu_configure(), which can be >>>>>>>>> called to configure the STU during PF enumeration. Latter >>>>>>>>> enumerations of VFs can successfully enable ATS >>>>>>>>> independently. >>> >>>>>>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>>>>>>>> } >>>>>>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>>>>>>>> +/** >>>>>>>>> + * pci_ats_stu_configure - Configure STU of a PF. >>>>>>>>> + * @dev: the PCI device >>>>>>>>> + * @ps: the IOMMU page shift >>>>>>>>> + * >>>>>>>>> + * Returns 0 on success, or negative on failure. >>>>>>>>> + */ >>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>>>>> +{ >>>>>>>>> + u16 ctrl; >>>>>>>>> + >>>>>>>>> + if (dev->ats_enabled || dev->is_virtfn) >>>>>>>>> + return 0; >>>>>>>> >>>>>>>> I might return an error for the VF case on the assumption >>>>>>>> that it's likely an error in the caller. I guess one could >>>>>>>> argue that it simplifies the caller if it doesn't have to >>>>>>>> check for PF vs VF. But the fact that STU is shared between >>>>>>>> PF and VFs is an important part of understanding how ATS >>>>>>>> works, so the caller should be aware of the distinction >>>>>>>> anyway. >>>>>>> >>>>>>> I have already asked this question. But let me repeat it. >>>>>>> >>>>>>> We don't have any checks for the PF case here. That means you >>>>>>> can re-configure the STU as many times as you want until ATS >>>>>>> is enabled in PF. So, if there are active VFs which uses this >>>>>>> STU, can PF re-configure the STU at will? >>>>>> >>>>>> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. >>>>>> >>>>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>>>>> index 1611bfa1d5da..f7bb01068e18 100644 >>>>>> --- a/drivers/pci/ats.c >>>>>> +++ b/drivers/pci/ats.c >>>>>> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>> if (dev->ats_enabled || dev->is_virtfn) >>>>>> return 0; >>>>>> >>>>>> + /* Configured already */ >>>>>> + if (dev->ats_stu) >>>>>> + return 0; >>>>> >>>>> Theoretically, you can re-configure STU as long as no one is using >>>>> it. Instead of this check, is there a way to check whether there >>>>> are active VMs which enables ATS? >>>> >>>> Yes I agree, there is no limitation on how many times you write STU >>>> bits, but practically it is happening while PF is enumerated. >>>> >>>> The usage of function pci_ats_stu_configure is almost >>>> similar(subset) to pci_enable_ats and only difference is one does >>>> ATS enable + STU program and another does only STU program. >>> >>> What would you think of removing the STU update feature from >>> pci_enable_ats() so it always fails if pci_ats_stu_configure() has not >>> been called, even when called on the PF, e.g., >>> >>> if (ps != pci_physfn(dev)->ats_stu) >>> return -EINVAL; >> >> If we are removing the STU update from pci_enable_ats(), why >> even allow passing "ps (page shift)" parameter? IMO, we can assume that >> for STU reconfigure, users will call pci_ats_stu_configure(). > > The reason to pass "ps" would be to verify that the STU the caller > plans to use matches the actual STU. > >> Since zero is a valid STU, enabling ATS can be decoupled from STU >> update. >> >>> pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>> ctrl |= PCI_ATS_CTRL_ENABLE; >>> pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>> >>> Would probably also have to set "dev->ats_stu = 0" in >>> pci_disable_ats() to allow the possibility of calling >>> pci_ats_stu_configure() again. >>> >>>> IMO, I dont think, there is any need to find how many active VMs >>>> with attached VFs and it is not done for pci_enable_ats as well. >>> >>> Enabling or disabling ATS in a PF or VF has no effect on other >>> functions. >>> >>> But changing STU while a VF has ATS enabled would definitely break any >>> user of that VF, so if it's practical to verify that no VFs have ATS >>> enabled, I think we should. >> >> I also think it is better to check for a ats_enabled status of VF before >> configuring the STU. >> >> May be something like below (untested), >> >> static int is_ats_enabled_in_vf(struct pci_dev *dev) >> { >> struct pci_sriov *iov = dev->sriov; >> struct pci_dev *vdev; >> >> if (dev->is_virtfn) >> return -EINVAL; >> >> for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) { >> vdev = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), >> pci_iov_virtfn_bus(dev, i), >> pci_iov_virtfn_devfn(dev, i)); > > I would try hard to avoid pci_get_domain_bus_and_slot(). That's > expensive (searches *all* PCI devs with for_each_pci_dev()) and > requires dealing with reference counts. > Thanks Bjorn and Sathyanarayanan for the suggestions. > Maybe an atomic count in the PF of VFs with ATS enabled. Yes this makes simple, atomic counter helps to find the active VFs. I am also thinking, can we put this condition on caller to make sure to *not* have any active VFs before calling this helper? So that the tracking and race issues should be taken care by the caller!. SMMUv3 driver(first consumer of this helper) is already tracking the active ats count(nr_ats_masters) atomically, it can be leveraged while calling. > >> if (vdev && vdev->ats_enabled) >> return 1; >> } >> >> return 0; >> >> } >> >> int pci_ats_stu_configure(struct pci_dev *dev, int ps) >> { >> ... >> if (is_ats_enabled_in_vf(dev)) >> return -EBUSY; >> >>> >>>> Also the caller has the requirement to call either >>>> pci_ats_stu_configure or pci_enable_ats while enumerating the PF. >>>> >>>>>> if (!pci_ats_supported(dev)) >>>>>> return -EINVAL; >>>>>>>> >>>>>>>>> + >>>>>>>>> + if (!pci_ats_supported(dev)) >>>>>>>>> + return -EINVAL; >>>>>>>>> + >>>>>>>>> + if (ps < PCI_ATS_MIN_STU) >>>>>>>>> + return -EINVAL; >>>>>>>>> + >>>>>>>>> + dev->ats_stu = ps; >>>>>>>>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>>>>>>>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >>>>>>>>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>>>>>>>> + >>>>>>>>> + return 0; >>>>>>>>> +} >>>>>>>>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >>>>>>>>> + >>>>>>>>> /** >>>>>>>>> * pci_enable_ats - enable the ATS capability >>>>>>>>> * @dev: the PCI device >>>>>>>>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >>>>>>>>> return -EINVAL; >>>>>>>>> /* >>>>>>>>> - * Note that enabling ATS on a VF fails unless it's already enabled >>>>>>>>> - * with the same STU on the PF. >>>>>>>>> + * Note that enabling ATS on a VF fails unless it's already >>>>>>>>> + * configured with the same STU on the PF. >>>>>>>>> */ >>>>>>>>> ctrl = PCI_ATS_CTRL_ENABLE; >>>>>>>>> if (dev->is_virtfn) { >>>>>>>>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >>>>>>>>> index df54cd5b15db..7d62a92aaf23 100644 >>>>>>>>> --- a/include/linux/pci-ats.h >>>>>>>>> +++ b/include/linux/pci-ats.h >>>>>>>>> @@ -8,6 +8,7 @@ >>>>>>>>> /* Address Translation Service */ >>>>>>>>> bool pci_ats_supported(struct pci_dev *dev); >>>>>>>>> int pci_enable_ats(struct pci_dev *dev, int ps); >>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >>>>>>>>> void pci_disable_ats(struct pci_dev *dev); >>>>>>>>> int pci_ats_queue_depth(struct pci_dev *dev); >>>>>>>>> int pci_ats_page_aligned(struct pci_dev *dev); >>>>>>>>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >>>>>>>>> { return false; } >>>>>>>>> static inline int pci_enable_ats(struct pci_dev *d, int ps) >>>>>>>>> { return -ENODEV; } >>>>>>>>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >>>>>>>>> +{ return -ENODEV; } >>>>>>>>> static inline void pci_disable_ats(struct pci_dev *d) { } >>>>>>>>> static inline int pci_ats_queue_depth(struct pci_dev *d) >>>>>>>>> { return -ENODEV; } >> >> -- >> Sathyanarayanan Kuppuswamy >> Linux Kernel Developer >> >> _______________________________________________ >> linux-arm-kernel mailing list >> linux-arm-kernel@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel Thanks, Ganapat
On 3/14/23 10:10 AM, Bjorn Helgaas wrote: > On Tue, Mar 14, 2023 at 09:50:06AM -0700, Sathyanarayanan Kuppuswamy wrote: >> On 3/14/23 9:02 AM, Bjorn Helgaas wrote: >>> On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: >>>> On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: >>>>> On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: >>>>>> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: >>>>>>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >>>>>>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>>>>>>>> As per PCI specification (PCI Express Base Specification >>>>>>>>> Revision 6.0, Section 10.5) both PF and VFs of a PCI EP >>>>>>>>> are permitted to be enabled independently for ATS >>>>>>>>> capability, however the STU(Smallest Translation Unit) is >>>>>>>>> shared between PF and VFs. For VFs, it is hardwired to >>>>>>>>> Zero and the associated PF's value applies to VFs. >>>>>>>>> >>>>>>>>> In the current code, the STU is being configured while >>>>>>>>> enabling the PF ATS. Hence, it is not able to enable ATS >>>>>>>>> for VFs, if it is not enabled on the associated PF >>>>>>>>> already. >>>>>>>>> >>>>>>>>> Adding a function pci_ats_stu_configure(), which can be >>>>>>>>> called to configure the STU during PF enumeration. Latter >>>>>>>>> enumerations of VFs can successfully enable ATS >>>>>>>>> independently. >>> >>>>>>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>>>>>>>> } >>>>>>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>>>>>>>> +/** >>>>>>>>> + * pci_ats_stu_configure - Configure STU of a PF. >>>>>>>>> + * @dev: the PCI device >>>>>>>>> + * @ps: the IOMMU page shift >>>>>>>>> + * >>>>>>>>> + * Returns 0 on success, or negative on failure. >>>>>>>>> + */ >>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>>>>> +{ >>>>>>>>> + u16 ctrl; >>>>>>>>> + >>>>>>>>> + if (dev->ats_enabled || dev->is_virtfn) >>>>>>>>> + return 0; >>>>>>>> >>>>>>>> I might return an error for the VF case on the assumption >>>>>>>> that it's likely an error in the caller. I guess one could >>>>>>>> argue that it simplifies the caller if it doesn't have to >>>>>>>> check for PF vs VF. But the fact that STU is shared between >>>>>>>> PF and VFs is an important part of understanding how ATS >>>>>>>> works, so the caller should be aware of the distinction >>>>>>>> anyway. >>>>>>> >>>>>>> I have already asked this question. But let me repeat it. >>>>>>> >>>>>>> We don't have any checks for the PF case here. That means you >>>>>>> can re-configure the STU as many times as you want until ATS >>>>>>> is enabled in PF. So, if there are active VFs which uses this >>>>>>> STU, can PF re-configure the STU at will? >>>>>> >>>>>> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. >>>>>> >>>>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>>>>> index 1611bfa1d5da..f7bb01068e18 100644 >>>>>> --- a/drivers/pci/ats.c >>>>>> +++ b/drivers/pci/ats.c >>>>>> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>> if (dev->ats_enabled || dev->is_virtfn) >>>>>> return 0; >>>>>> >>>>>> + /* Configured already */ >>>>>> + if (dev->ats_stu) >>>>>> + return 0; >>>>> >>>>> Theoretically, you can re-configure STU as long as no one is using >>>>> it. Instead of this check, is there a way to check whether there >>>>> are active VMs which enables ATS? >>>> >>>> Yes I agree, there is no limitation on how many times you write STU >>>> bits, but practically it is happening while PF is enumerated. >>>> >>>> The usage of function pci_ats_stu_configure is almost >>>> similar(subset) to pci_enable_ats and only difference is one does >>>> ATS enable + STU program and another does only STU program. >>> >>> What would you think of removing the STU update feature from >>> pci_enable_ats() so it always fails if pci_ats_stu_configure() has not >>> been called, even when called on the PF, e.g., >>> >>> if (ps != pci_physfn(dev)->ats_stu) >>> return -EINVAL; >> >> If we are removing the STU update from pci_enable_ats(), why >> even allow passing "ps (page shift)" parameter? IMO, we can assume that >> for STU reconfigure, users will call pci_ats_stu_configure(). > > The reason to pass "ps" would be to verify that the STU the caller > plans to use matches the actual STU. Do we really need to verify it? My thinking is, by introducing pci_ats_stu_configure() we are already trying to decouple the STU config from pci_enable_ats(). So why again check for it when enabling ATS? > >> Since zero is a valid STU, enabling ATS can be decoupled from STU >> update. >> >>> pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>> ctrl |= PCI_ATS_CTRL_ENABLE; >>> pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>> >>> Would probably also have to set "dev->ats_stu = 0" in >>> pci_disable_ats() to allow the possibility of calling >>> pci_ats_stu_configure() again. >>> >>>> IMO, I dont think, there is any need to find how many active VMs >>>> with attached VFs and it is not done for pci_enable_ats as well. >>> >>> Enabling or disabling ATS in a PF or VF has no effect on other >>> functions. >>> >>> But changing STU while a VF has ATS enabled would definitely break any >>> user of that VF, so if it's practical to verify that no VFs have ATS >>> enabled, I think we should. >> >> I also think it is better to check for a ats_enabled status of VF before >> configuring the STU. >> >> May be something like below (untested), >> >> static int is_ats_enabled_in_vf(struct pci_dev *dev) >> { >> struct pci_sriov *iov = dev->sriov; >> struct pci_dev *vdev; >> >> if (dev->is_virtfn) >> return -EINVAL; >> >> for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) { >> vdev = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), >> pci_iov_virtfn_bus(dev, i), >> pci_iov_virtfn_devfn(dev, i)); > > I would try hard to avoid pci_get_domain_bus_and_slot(). That's > expensive (searches *all* PCI devs with for_each_pci_dev()) and > requires dealing with reference counts. > > Maybe an atomic count in the PF of VFs with ATS enabled. > >> if (vdev && vdev->ats_enabled) >> return 1; >> } >> >> return 0; >> >> } >> >> int pci_ats_stu_configure(struct pci_dev *dev, int ps) >> { >> ... >> if (is_ats_enabled_in_vf(dev)) >> return -EBUSY; >> >>> >>>> Also the caller has the requirement to call either >>>> pci_ats_stu_configure or pci_enable_ats while enumerating the PF. >>>> >>>>>> if (!pci_ats_supported(dev)) >>>>>> return -EINVAL; >>>>>>>> >>>>>>>>> + >>>>>>>>> + if (!pci_ats_supported(dev)) >>>>>>>>> + return -EINVAL; >>>>>>>>> + >>>>>>>>> + if (ps < PCI_ATS_MIN_STU) >>>>>>>>> + return -EINVAL; >>>>>>>>> + >>>>>>>>> + dev->ats_stu = ps; >>>>>>>>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>>>>>>>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >>>>>>>>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>>>>>>>> + >>>>>>>>> + return 0; >>>>>>>>> +} >>>>>>>>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >>>>>>>>> + >>>>>>>>> /** >>>>>>>>> * pci_enable_ats - enable the ATS capability >>>>>>>>> * @dev: the PCI device >>>>>>>>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >>>>>>>>> return -EINVAL; >>>>>>>>> /* >>>>>>>>> - * Note that enabling ATS on a VF fails unless it's already enabled >>>>>>>>> - * with the same STU on the PF. >>>>>>>>> + * Note that enabling ATS on a VF fails unless it's already >>>>>>>>> + * configured with the same STU on the PF. >>>>>>>>> */ >>>>>>>>> ctrl = PCI_ATS_CTRL_ENABLE; >>>>>>>>> if (dev->is_virtfn) { >>>>>>>>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >>>>>>>>> index df54cd5b15db..7d62a92aaf23 100644 >>>>>>>>> --- a/include/linux/pci-ats.h >>>>>>>>> +++ b/include/linux/pci-ats.h >>>>>>>>> @@ -8,6 +8,7 @@ >>>>>>>>> /* Address Translation Service */ >>>>>>>>> bool pci_ats_supported(struct pci_dev *dev); >>>>>>>>> int pci_enable_ats(struct pci_dev *dev, int ps); >>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >>>>>>>>> void pci_disable_ats(struct pci_dev *dev); >>>>>>>>> int pci_ats_queue_depth(struct pci_dev *dev); >>>>>>>>> int pci_ats_page_aligned(struct pci_dev *dev); >>>>>>>>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >>>>>>>>> { return false; } >>>>>>>>> static inline int pci_enable_ats(struct pci_dev *d, int ps) >>>>>>>>> { return -ENODEV; } >>>>>>>>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >>>>>>>>> +{ return -ENODEV; } >>>>>>>>> static inline void pci_disable_ats(struct pci_dev *d) { } >>>>>>>>> static inline int pci_ats_queue_depth(struct pci_dev *d) >>>>>>>>> { return -ENODEV; } >> >> -- >> Sathyanarayanan Kuppuswamy >> Linux Kernel Developer >> >> _______________________________________________ >> linux-arm-kernel mailing list >> linux-arm-kernel@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Tue, Mar 14, 2023 at 11:12:11AM -0700, Sathyanarayanan Kuppuswamy wrote: > On 3/14/23 10:10 AM, Bjorn Helgaas wrote: > > On Tue, Mar 14, 2023 at 09:50:06AM -0700, Sathyanarayanan Kuppuswamy wrote: > >> On 3/14/23 9:02 AM, Bjorn Helgaas wrote: > >>> On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: > >>>> On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: > >>>>> On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: > >>>>>> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: > >>>>>>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: > >>>>>>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: > >>>>>>>>> As per PCI specification (PCI Express Base Specification > >>>>>>>>> Revision 6.0, Section 10.5) both PF and VFs of a PCI EP > >>>>>>>>> are permitted to be enabled independently for ATS > >>>>>>>>> capability, however the STU(Smallest Translation Unit) is > >>>>>>>>> shared between PF and VFs. For VFs, it is hardwired to > >>>>>>>>> Zero and the associated PF's value applies to VFs. > >>>>>>>>> > >>>>>>>>> In the current code, the STU is being configured while > >>>>>>>>> enabling the PF ATS. Hence, it is not able to enable ATS > >>>>>>>>> for VFs, if it is not enabled on the associated PF > >>>>>>>>> already. > >>>>>>>>> > >>>>>>>>> Adding a function pci_ats_stu_configure(), which can be > >>>>>>>>> called to configure the STU during PF enumeration. Latter > >>>>>>>>> enumerations of VFs can successfully enable ATS > >>>>>>>>> independently. > >>> > >>>>>>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) > >>>>>>>>> } > >>>>>>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); > >>>>>>>>> +/** > >>>>>>>>> + * pci_ats_stu_configure - Configure STU of a PF. > >>>>>>>>> + * @dev: the PCI device > >>>>>>>>> + * @ps: the IOMMU page shift > >>>>>>>>> + * > >>>>>>>>> + * Returns 0 on success, or negative on failure. > >>>>>>>>> + */ > >>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) > >>>>>>>>> +{ > >>>>>>>>> + u16 ctrl; > >>>>>>>>> + > >>>>>>>>> + if (dev->ats_enabled || dev->is_virtfn) > >>>>>>>>> + return 0; > >>>>>>>> > >>>>>>>> I might return an error for the VF case on the assumption > >>>>>>>> that it's likely an error in the caller. I guess one could > >>>>>>>> argue that it simplifies the caller if it doesn't have to > >>>>>>>> check for PF vs VF. But the fact that STU is shared between > >>>>>>>> PF and VFs is an important part of understanding how ATS > >>>>>>>> works, so the caller should be aware of the distinction > >>>>>>>> anyway. > >>>>>>> > >>>>>>> I have already asked this question. But let me repeat it. > >>>>>>> > >>>>>>> We don't have any checks for the PF case here. That means you > >>>>>>> can re-configure the STU as many times as you want until ATS > >>>>>>> is enabled in PF. So, if there are active VFs which uses this > >>>>>>> STU, can PF re-configure the STU at will? > >>>>>> > >>>>>> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. > >>>>>> > >>>>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c > >>>>>> index 1611bfa1d5da..f7bb01068e18 100644 > >>>>>> --- a/drivers/pci/ats.c > >>>>>> +++ b/drivers/pci/ats.c > >>>>>> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) > >>>>>> if (dev->ats_enabled || dev->is_virtfn) > >>>>>> return 0; > >>>>>> > >>>>>> + /* Configured already */ > >>>>>> + if (dev->ats_stu) > >>>>>> + return 0; > >>>>> > >>>>> Theoretically, you can re-configure STU as long as no one is using > >>>>> it. Instead of this check, is there a way to check whether there > >>>>> are active VMs which enables ATS? > >>>> > >>>> Yes I agree, there is no limitation on how many times you write STU > >>>> bits, but practically it is happening while PF is enumerated. > >>>> > >>>> The usage of function pci_ats_stu_configure is almost > >>>> similar(subset) to pci_enable_ats and only difference is one does > >>>> ATS enable + STU program and another does only STU program. > >>> > >>> What would you think of removing the STU update feature from > >>> pci_enable_ats() so it always fails if pci_ats_stu_configure() has not > >>> been called, even when called on the PF, e.g., > >>> > >>> if (ps != pci_physfn(dev)->ats_stu) > >>> return -EINVAL; > >> > >> If we are removing the STU update from pci_enable_ats(), why > >> even allow passing "ps (page shift)" parameter? IMO, we can assume that > >> for STU reconfigure, users will call pci_ats_stu_configure(). > > > > The reason to pass "ps" would be to verify that the STU the caller > > plans to use matches the actual STU. > > Do we really need to verify it? My thinking is, by introducing > pci_ats_stu_configure() we are already trying to decouple the STU config > from pci_enable_ats(). So why again check for it when enabling ATS? Yeah, maybe we don't need to. I was thinking that STU would be configured by the host, while the caller of pci_enable_ats() for a VF might be in a guest, but I guess that's not the case, right? Bjorn
On 3/14/23 2:17 PM, Bjorn Helgaas wrote: > On Tue, Mar 14, 2023 at 11:12:11AM -0700, Sathyanarayanan Kuppuswamy wrote: >> On 3/14/23 10:10 AM, Bjorn Helgaas wrote: >>> On Tue, Mar 14, 2023 at 09:50:06AM -0700, Sathyanarayanan Kuppuswamy wrote: >>>> On 3/14/23 9:02 AM, Bjorn Helgaas wrote: >>>>> On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: >>>>>> On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: >>>>>>> On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: >>>>>>>> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: >>>>>>>>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >>>>>>>>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>>>>>>>>>> As per PCI specification (PCI Express Base Specification >>>>>>>>>>> Revision 6.0, Section 10.5) both PF and VFs of a PCI EP >>>>>>>>>>> are permitted to be enabled independently for ATS >>>>>>>>>>> capability, however the STU(Smallest Translation Unit) is >>>>>>>>>>> shared between PF and VFs. For VFs, it is hardwired to >>>>>>>>>>> Zero and the associated PF's value applies to VFs. >>>>>>>>>>> >>>>>>>>>>> In the current code, the STU is being configured while >>>>>>>>>>> enabling the PF ATS. Hence, it is not able to enable ATS >>>>>>>>>>> for VFs, if it is not enabled on the associated PF >>>>>>>>>>> already. >>>>>>>>>>> >>>>>>>>>>> Adding a function pci_ats_stu_configure(), which can be >>>>>>>>>>> called to configure the STU during PF enumeration. Latter >>>>>>>>>>> enumerations of VFs can successfully enable ATS >>>>>>>>>>> independently. >>>>> >>>>>>>>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>>>>>>>>>> } >>>>>>>>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>>>>>>>>>> +/** >>>>>>>>>>> + * pci_ats_stu_configure - Configure STU of a PF. >>>>>>>>>>> + * @dev: the PCI device >>>>>>>>>>> + * @ps: the IOMMU page shift >>>>>>>>>>> + * >>>>>>>>>>> + * Returns 0 on success, or negative on failure. >>>>>>>>>>> + */ >>>>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>>>>>>> +{ >>>>>>>>>>> + u16 ctrl; >>>>>>>>>>> + >>>>>>>>>>> + if (dev->ats_enabled || dev->is_virtfn) >>>>>>>>>>> + return 0; >>>>>>>>>> >>>>>>>>>> I might return an error for the VF case on the assumption >>>>>>>>>> that it's likely an error in the caller. I guess one could >>>>>>>>>> argue that it simplifies the caller if it doesn't have to >>>>>>>>>> check for PF vs VF. But the fact that STU is shared between >>>>>>>>>> PF and VFs is an important part of understanding how ATS >>>>>>>>>> works, so the caller should be aware of the distinction >>>>>>>>>> anyway. >>>>>>>>> >>>>>>>>> I have already asked this question. But let me repeat it. >>>>>>>>> >>>>>>>>> We don't have any checks for the PF case here. That means you >>>>>>>>> can re-configure the STU as many times as you want until ATS >>>>>>>>> is enabled in PF. So, if there are active VFs which uses this >>>>>>>>> STU, can PF re-configure the STU at will? >>>>>>>> >>>>>>>> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. >>>>>>>> >>>>>>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>>>>>>> index 1611bfa1d5da..f7bb01068e18 100644 >>>>>>>> --- a/drivers/pci/ats.c >>>>>>>> +++ b/drivers/pci/ats.c >>>>>>>> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>>>> if (dev->ats_enabled || dev->is_virtfn) >>>>>>>> return 0; >>>>>>>> >>>>>>>> + /* Configured already */ >>>>>>>> + if (dev->ats_stu) >>>>>>>> + return 0; >>>>>>> >>>>>>> Theoretically, you can re-configure STU as long as no one is using >>>>>>> it. Instead of this check, is there a way to check whether there >>>>>>> are active VMs which enables ATS? >>>>>> >>>>>> Yes I agree, there is no limitation on how many times you write STU >>>>>> bits, but practically it is happening while PF is enumerated. >>>>>> >>>>>> The usage of function pci_ats_stu_configure is almost >>>>>> similar(subset) to pci_enable_ats and only difference is one does >>>>>> ATS enable + STU program and another does only STU program. >>>>> >>>>> What would you think of removing the STU update feature from >>>>> pci_enable_ats() so it always fails if pci_ats_stu_configure() has not >>>>> been called, even when called on the PF, e.g., >>>>> >>>>> if (ps != pci_physfn(dev)->ats_stu) >>>>> return -EINVAL; >>>> >>>> If we are removing the STU update from pci_enable_ats(), why >>>> even allow passing "ps (page shift)" parameter? IMO, we can assume that >>>> for STU reconfigure, users will call pci_ats_stu_configure(). >>> >>> The reason to pass "ps" would be to verify that the STU the caller >>> plans to use matches the actual STU. >> >> Do we really need to verify it? My thinking is, by introducing >> pci_ats_stu_configure() we are already trying to decouple the STU config >> from pci_enable_ats(). So why again check for it when enabling ATS? > > Yeah, maybe we don't need to. I was thinking that STU would be > configured by the host, while the caller of pci_enable_ats() for a VF > might be in a guest, but I guess that's not the case, right? I think the idea is to configure the STU during PF enumeration in the host. So, when VF enables ATS in the VM, it does not need to worry about the STU configuration. So I don't think we need to re-check the STU in the VF case. > > Bjorn
On 14-03-2023 11:42 pm, Sathyanarayanan Kuppuswamy wrote: > > > On 3/14/23 10:10 AM, Bjorn Helgaas wrote: >> On Tue, Mar 14, 2023 at 09:50:06AM -0700, Sathyanarayanan Kuppuswamy wrote: >>> On 3/14/23 9:02 AM, Bjorn Helgaas wrote: >>>> On Tue, Mar 14, 2023 at 08:06:07PM +0530, Ganapatrao Kulkarni wrote: >>>>> On 14-03-2023 06:22 pm, Sathyanarayanan Kuppuswamy wrote: >>>>>> On 3/14/23 3:08 AM, Ganapatrao Kulkarni wrote: >>>>>>> On 14-03-2023 04:00 am, Sathyanarayanan Kuppuswamy wrote: >>>>>>>> On 3/13/23 2:12 PM, Bjorn Helgaas wrote: >>>>>>>>> On Mon, Feb 27, 2023 at 08:21:36PM -0800, Ganapatrao Kulkarni wrote: >>>>>>>>>> As per PCI specification (PCI Express Base Specification >>>>>>>>>> Revision 6.0, Section 10.5) both PF and VFs of a PCI EP >>>>>>>>>> are permitted to be enabled independently for ATS >>>>>>>>>> capability, however the STU(Smallest Translation Unit) is >>>>>>>>>> shared between PF and VFs. For VFs, it is hardwired to >>>>>>>>>> Zero and the associated PF's value applies to VFs. >>>>>>>>>> >>>>>>>>>> In the current code, the STU is being configured while >>>>>>>>>> enabling the PF ATS. Hence, it is not able to enable ATS >>>>>>>>>> for VFs, if it is not enabled on the associated PF >>>>>>>>>> already. >>>>>>>>>> >>>>>>>>>> Adding a function pci_ats_stu_configure(), which can be >>>>>>>>>> called to configure the STU during PF enumeration. Latter >>>>>>>>>> enumerations of VFs can successfully enable ATS >>>>>>>>>> independently. >>>> >>>>>>>>>> @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) >>>>>>>>>> } >>>>>>>>>> EXPORT_SYMBOL_GPL(pci_ats_supported); >>>>>>>>>> +/** >>>>>>>>>> + * pci_ats_stu_configure - Configure STU of a PF. >>>>>>>>>> + * @dev: the PCI device >>>>>>>>>> + * @ps: the IOMMU page shift >>>>>>>>>> + * >>>>>>>>>> + * Returns 0 on success, or negative on failure. >>>>>>>>>> + */ >>>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>>>>>> +{ >>>>>>>>>> + u16 ctrl; >>>>>>>>>> + >>>>>>>>>> + if (dev->ats_enabled || dev->is_virtfn) >>>>>>>>>> + return 0; >>>>>>>>> >>>>>>>>> I might return an error for the VF case on the assumption >>>>>>>>> that it's likely an error in the caller. I guess one could >>>>>>>>> argue that it simplifies the caller if it doesn't have to >>>>>>>>> check for PF vs VF. But the fact that STU is shared between >>>>>>>>> PF and VFs is an important part of understanding how ATS >>>>>>>>> works, so the caller should be aware of the distinction >>>>>>>>> anyway. >>>>>>>> >>>>>>>> I have already asked this question. But let me repeat it. >>>>>>>> >>>>>>>> We don't have any checks for the PF case here. That means you >>>>>>>> can re-configure the STU as many times as you want until ATS >>>>>>>> is enabled in PF. So, if there are active VFs which uses this >>>>>>>> STU, can PF re-configure the STU at will? >>>>>>> >>>>>>> IMO, Since STU is shared, programming it multiple times is not expected from callers code do it, however we can add below check to allow to program STU once from a PF. >>>>>>> >>>>>>> diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c >>>>>>> index 1611bfa1d5da..f7bb01068e18 100644 >>>>>>> --- a/drivers/pci/ats.c >>>>>>> +++ b/drivers/pci/ats.c >>>>>>> @@ -60,6 +60,10 @@ int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>>>>>> if (dev->ats_enabled || dev->is_virtfn) >>>>>>> return 0; >>>>>>> >>>>>>> + /* Configured already */ >>>>>>> + if (dev->ats_stu) >>>>>>> + return 0; >>>>>> >>>>>> Theoretically, you can re-configure STU as long as no one is using >>>>>> it. Instead of this check, is there a way to check whether there >>>>>> are active VMs which enables ATS? >>>>> >>>>> Yes I agree, there is no limitation on how many times you write STU >>>>> bits, but practically it is happening while PF is enumerated. >>>>> >>>>> The usage of function pci_ats_stu_configure is almost >>>>> similar(subset) to pci_enable_ats and only difference is one does >>>>> ATS enable + STU program and another does only STU program. >>>> >>>> What would you think of removing the STU update feature from >>>> pci_enable_ats() so it always fails if pci_ats_stu_configure() has not >>>> been called, even when called on the PF, e.g., >>>> >>>> if (ps != pci_physfn(dev)->ats_stu) >>>> return -EINVAL; >>> >>> If we are removing the STU update from pci_enable_ats(), why >>> even allow passing "ps (page shift)" parameter? IMO, we can assume that >>> for STU reconfigure, users will call pci_ats_stu_configure(). >> >> The reason to pass "ps" would be to verify that the STU the caller >> plans to use matches the actual STU. > > Do we really need to verify it? My thinking is, by introducing > pci_ats_stu_configure() we are already trying to decouple the STU config > from pci_enable_ats(). So why again check for it when enabling ATS? AFAIK, we are not decoupling STU from pci_enable_ats, there is a case where ATS is not enabled on PF (host kernel booted in passthrough) where as ATS gets enabled on VF while it is attached to a VM(through vfio driver). In such case, during PF enumeration, pci_ats_stu_configure is called to program STU and allow ATS to be enabled on VFs. In normal case, PF enables ATS and STU is programmed and no issue while enabling the VFs. > >> >>> Since zero is a valid STU, enabling ATS can be decoupled from STU >>> update. >>> >>>> pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>>> ctrl |= PCI_ATS_CTRL_ENABLE; >>>> pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>>> >>>> Would probably also have to set "dev->ats_stu = 0" in >>>> pci_disable_ats() to allow the possibility of calling >>>> pci_ats_stu_configure() again. >>>> >>>>> IMO, I dont think, there is any need to find how many active VMs >>>>> with attached VFs and it is not done for pci_enable_ats as well. >>>> >>>> Enabling or disabling ATS in a PF or VF has no effect on other >>>> functions. >>>> >>>> But changing STU while a VF has ATS enabled would definitely break any >>>> user of that VF, so if it's practical to verify that no VFs have ATS >>>> enabled, I think we should. >>> >>> I also think it is better to check for a ats_enabled status of VF before >>> configuring the STU. >>> >>> May be something like below (untested), >>> >>> static int is_ats_enabled_in_vf(struct pci_dev *dev) >>> { >>> struct pci_sriov *iov = dev->sriov; >>> struct pci_dev *vdev; >>> >>> if (dev->is_virtfn) >>> return -EINVAL; >>> >>> for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) { >>> vdev = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), >>> pci_iov_virtfn_bus(dev, i), >>> pci_iov_virtfn_devfn(dev, i)); >> >> I would try hard to avoid pci_get_domain_bus_and_slot(). That's >> expensive (searches *all* PCI devs with for_each_pci_dev()) and >> requires dealing with reference counts. >> >> Maybe an atomic count in the PF of VFs with ATS enabled. >> >>> if (vdev && vdev->ats_enabled) >>> return 1; >>> } >>> >>> return 0; >>> >>> } >>> >>> int pci_ats_stu_configure(struct pci_dev *dev, int ps) >>> { >>> ... >>> if (is_ats_enabled_in_vf(dev)) >>> return -EBUSY; >>> >>>> >>>>> Also the caller has the requirement to call either >>>>> pci_ats_stu_configure or pci_enable_ats while enumerating the PF. >>>>> >>>>>>> if (!pci_ats_supported(dev)) >>>>>>> return -EINVAL; >>>>>>>>> >>>>>>>>>> + >>>>>>>>>> + if (!pci_ats_supported(dev)) >>>>>>>>>> + return -EINVAL; >>>>>>>>>> + >>>>>>>>>> + if (ps < PCI_ATS_MIN_STU) >>>>>>>>>> + return -EINVAL; >>>>>>>>>> + >>>>>>>>>> + dev->ats_stu = ps; >>>>>>>>>> + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); >>>>>>>>>> + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); >>>>>>>>>> + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); >>>>>>>>>> + >>>>>>>>>> + return 0; >>>>>>>>>> +} >>>>>>>>>> +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); >>>>>>>>>> + >>>>>>>>>> /** >>>>>>>>>> * pci_enable_ats - enable the ATS capability >>>>>>>>>> * @dev: the PCI device >>>>>>>>>> @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) >>>>>>>>>> return -EINVAL; >>>>>>>>>> /* >>>>>>>>>> - * Note that enabling ATS on a VF fails unless it's already enabled >>>>>>>>>> - * with the same STU on the PF. >>>>>>>>>> + * Note that enabling ATS on a VF fails unless it's already >>>>>>>>>> + * configured with the same STU on the PF. >>>>>>>>>> */ >>>>>>>>>> ctrl = PCI_ATS_CTRL_ENABLE; >>>>>>>>>> if (dev->is_virtfn) { >>>>>>>>>> diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h >>>>>>>>>> index df54cd5b15db..7d62a92aaf23 100644 >>>>>>>>>> --- a/include/linux/pci-ats.h >>>>>>>>>> +++ b/include/linux/pci-ats.h >>>>>>>>>> @@ -8,6 +8,7 @@ >>>>>>>>>> /* Address Translation Service */ >>>>>>>>>> bool pci_ats_supported(struct pci_dev *dev); >>>>>>>>>> int pci_enable_ats(struct pci_dev *dev, int ps); >>>>>>>>>> +int pci_ats_stu_configure(struct pci_dev *dev, int ps); >>>>>>>>>> void pci_disable_ats(struct pci_dev *dev); >>>>>>>>>> int pci_ats_queue_depth(struct pci_dev *dev); >>>>>>>>>> int pci_ats_page_aligned(struct pci_dev *dev); >>>>>>>>>> @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) >>>>>>>>>> { return false; } >>>>>>>>>> static inline int pci_enable_ats(struct pci_dev *d, int ps) >>>>>>>>>> { return -ENODEV; } >>>>>>>>>> +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) >>>>>>>>>> +{ return -ENODEV; } >>>>>>>>>> static inline void pci_disable_ats(struct pci_dev *d) { } >>>>>>>>>> static inline int pci_ats_queue_depth(struct pci_dev *d) >>>>>>>>>> { return -ENODEV; } >>> >>> -- >>> Sathyanarayanan Kuppuswamy >>> Linux Kernel Developer >>> >>> _______________________________________________ >>> linux-arm-kernel mailing list >>> linux-arm-kernel@lists.infradead.org >>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel > Thanks, Ganapat
diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c index f9cc2e10b676..1611bfa1d5da 100644 --- a/drivers/pci/ats.c +++ b/drivers/pci/ats.c @@ -46,6 +46,35 @@ bool pci_ats_supported(struct pci_dev *dev) } EXPORT_SYMBOL_GPL(pci_ats_supported); +/** + * pci_ats_stu_configure - Configure STU of a PF. + * @dev: the PCI device + * @ps: the IOMMU page shift + * + * Returns 0 on success, or negative on failure. + */ +int pci_ats_stu_configure(struct pci_dev *dev, int ps) +{ + u16 ctrl; + + if (dev->ats_enabled || dev->is_virtfn) + return 0; + + if (!pci_ats_supported(dev)) + return -EINVAL; + + if (ps < PCI_ATS_MIN_STU) + return -EINVAL; + + dev->ats_stu = ps; + pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); + ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); + pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); + + return 0; +} +EXPORT_SYMBOL_GPL(pci_ats_stu_configure); + /** * pci_enable_ats - enable the ATS capability * @dev: the PCI device @@ -68,8 +97,8 @@ int pci_enable_ats(struct pci_dev *dev, int ps) return -EINVAL; /* - * Note that enabling ATS on a VF fails unless it's already enabled - * with the same STU on the PF. + * Note that enabling ATS on a VF fails unless it's already + * configured with the same STU on the PF. */ ctrl = PCI_ATS_CTRL_ENABLE; if (dev->is_virtfn) { diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h index df54cd5b15db..7d62a92aaf23 100644 --- a/include/linux/pci-ats.h +++ b/include/linux/pci-ats.h @@ -8,6 +8,7 @@ /* Address Translation Service */ bool pci_ats_supported(struct pci_dev *dev); int pci_enable_ats(struct pci_dev *dev, int ps); +int pci_ats_stu_configure(struct pci_dev *dev, int ps); void pci_disable_ats(struct pci_dev *dev); int pci_ats_queue_depth(struct pci_dev *dev); int pci_ats_page_aligned(struct pci_dev *dev); @@ -16,6 +17,8 @@ static inline bool pci_ats_supported(struct pci_dev *d) { return false; } static inline int pci_enable_ats(struct pci_dev *d, int ps) { return -ENODEV; } +static inline int pci_ats_stu_configure(struct pci_dev *d, int ps) +{ return -ENODEV; } static inline void pci_disable_ats(struct pci_dev *d) { } static inline int pci_ats_queue_depth(struct pci_dev *d) { return -ENODEV; }