From patchwork Wed Aug 25 21:22:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= X-Patchwork-Id: 12458549 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AFB4C4320A for ; Wed, 25 Aug 2021 21:23:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 12F18610E9 for ; Wed, 25 Aug 2021 21:23:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229707AbhHYVXq (ORCPT ); Wed, 25 Aug 2021 17:23:46 -0400 Received: from mail-wm1-f49.google.com ([209.85.128.49]:51727 "EHLO mail-wm1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229923AbhHYVXp (ORCPT ); Wed, 25 Aug 2021 17:23:45 -0400 Received: by mail-wm1-f49.google.com with SMTP id u15so454597wmj.1 for ; Wed, 25 Aug 2021 14:22:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Pus7lzKHrBqat0liDWmIGHtUAC+V64E1vXqi3om7Leg=; b=EqTVgW2mMTZjlp7uVWWtdmOc8DDStY/WxEBZyrqkoR5wfK2ZfGN6euVbH8IDMHoXh1 ULIoMSlIplQpKM8w/87MYue+aSHPSQWum0nPERejSUlPVF0+vSxwkbMEG0oA4SVsTsvH CJGdDLUT/4Vf6osnHPbpVXPnOAXZwSI1OEnxYE6BN/hd8M590eT/jxvKvcTSsgVa+xLS oCG4X22ZSFsonzPmsv4YGvrEzKhwOd5cUJU8pdbCHF5IR9JUM3zeb597Q+4QuRcNL97x JmUpOcI3vcCx9KuYu8to6etW+mDNnFD8mxOB3PSEEoKjnc4DT/I4PkzhHrkx9/sgNGuw fo9w== X-Gm-Message-State: AOAM530zbhaPHahHO2rZ/uppi5fIYck5FLFV9J5IL4L6QiIqocB+p0ZS CY7Hfryd9gRxhaEXVtayJmAea2EWBOI= X-Google-Smtp-Source: ABdhPJxvaQmptfTKuChrPpBKpCsRq7lrafpNdJv1K4typReumYbDg8wnJCNIhulSnx4nWmc4OYcT5g== X-Received: by 2002:a05:600c:1551:: with SMTP id f17mr11006105wmg.44.1629926578502; Wed, 25 Aug 2021 14:22:58 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id d24sm663527wmb.35.2021.08.25.14.22.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 14:22:58 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: linux-pci@vger.kernel.org Subject: [PATCH 1/4] PCI/sysfs: Add pci_dev_resource_attr_is_visible() helper Date: Wed, 25 Aug 2021 21:22:52 +0000 Message-Id: <20210825212255.878043-2-kw@linux.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210825212255.878043-1-kw@linux.com> References: <20210825212255.878043-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org This helper aims to replace functions pci_create_resource_files() and pci_create_attr() that are currently involved in the PCI resource sysfs objects dynamic creation and set up once the. After the conversion to use static sysfs objects when exposing the PCI BAR address space this helper is to be called from the .is_bin_visible() callback for each of the PCI resources attributes. Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index b70f61fbcd4b..c94ab9830932 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1237,6 +1237,46 @@ static int pci_create_resource_files(struct pci_dev *pdev) } return 0; } + +static umode_t pci_dev_resource_attr_is_visible(struct kobject *kobj, + struct bin_attribute *attr, + int bar, bool write_combine) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + resource_size_t resource_size = pci_resource_len(pdev, bar); + unsigned long flags = pci_resource_flags(pdev, bar); + + if (!resource_size) + return 0; + + if (write_combine) { + if (arch_can_pci_mmap_wc() && (flags & + (IORESOURCE_MEM | IORESOURCE_PREFETCH)) == + (IORESOURCE_MEM | IORESOURCE_PREFETCH)) + attr->mmap = pci_mmap_resource_wc; + else + return 0; + } else { + if (flags & IORESOURCE_MEM) { + attr->mmap = pci_mmap_resource_uc; + } else if (flags & IORESOURCE_IO) { + attr->read = pci_read_resource_io; + attr->write = pci_write_resource_io; + if (arch_can_pci_mmap_io()) + attr->mmap = pci_mmap_resource_uc; + } else { + return 0; + } + } + + attr->size = resource_size; + if (attr->mmap) + attr->f_mapping = iomem_get_mapping; + + attr->private = (void *)(unsigned long)bar; + + return attr->attr.mode; +} #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } From patchwork Wed Aug 25 21:22:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= X-Patchwork-Id: 12458553 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A99E2C43214 for ; Wed, 25 Aug 2021 21:23:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 85F43610CA for ; Wed, 25 Aug 2021 21:23:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231448AbhHYVXq (ORCPT ); Wed, 25 Aug 2021 17:23:46 -0400 Received: from mail-wm1-f47.google.com ([209.85.128.47]:53152 "EHLO mail-wm1-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230512AbhHYVXq (ORCPT ); Wed, 25 Aug 2021 17:23:46 -0400 Received: by mail-wm1-f47.google.com with SMTP id f10so453236wml.2 for ; Wed, 25 Aug 2021 14:23:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Zf6rkWUjaV1Tx6F+wfWGn1LuKnP4vz9zUcrgqSHDegA=; b=Zr2+VJb5mb6AVUa3XqU32+GZNhnaQmmaBtjVg6A5ZZZwo/W5dm0DjVOkL2CrU7vxK5 z7lLaxdg1D0sankXdy9itbgxAC/q4FF1dOea0l1F0jEFTX53osCFvMRMl7kyfv2jjbi+ 9puvjWVPqA6J7m36Dk/++EJQPUTxvwW/Zf9N5JZxWkGYWdsaZJdIp1UNFr8gaByGfVPZ TGdl9sweCE1a8cv1Zb6gUv4Fiu+YgbEFLwOgx+o+iNDEtfxUhI59oFyX5BHtH3dokZFw HXE6h5Rpmts/qp7Jsw4iv6JFSe0PmhnvCZImzwg9lIrb6Qqbg9gkNY1MPKTwEB6TLNEy H3fQ== X-Gm-Message-State: AOAM531OGWIOAbx2F4bNf9XY0FqGhpSc2rNQnyWAi+kLlHJECDNKLz8C 4hGVNxACnm/rEUKWizDDNbeoPHTcXOU= X-Google-Smtp-Source: ABdhPJwXoYV+D0edvzKjhf7OAUxbCeuEx1+CXOWzBjo5+z7py5grrnpCQUgdYgT6TRDY4i8gAUHooQ== X-Received: by 2002:a05:600c:22d3:: with SMTP id 19mr469551wmg.36.1629926579498; Wed, 25 Aug 2021 14:22:59 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id d24sm663527wmb.35.2021.08.25.14.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 14:22:59 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: linux-pci@vger.kernel.org Subject: [PATCH 2/4] PCI/sysfs: Add pci_dev_resource_attr() macro Date: Wed, 25 Aug 2021 21:22:53 +0000 Message-Id: <20210825212255.878043-3-kw@linux.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210825212255.878043-1-kw@linux.com> References: <20210825212255.878043-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The pci_dev_resource_attr() macro will be used to declare and define each of the PCI resource sysfs objects statically while also reducing unnecessary code repetition. Internally this macro relies on the pci_dev_resource_attr_is_visible() helper which should correctly handle different types of PCI BAR address space while also providing support for creating either normal and/or write-combine attributes as required. Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index c94ab9830932..6eba5c0887df 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1277,6 +1277,53 @@ static umode_t pci_dev_resource_attr_is_visible(struct kobject *kobj, return attr->attr.mode; } + +#define pci_dev_resource_attr(_bar) \ +static struct bin_attribute \ +pci_dev_resource##_bar##_attr = __BIN_ATTR(resource##_bar, \ + 0600, NULL, NULL, 0); \ + \ +static struct bin_attribute *pci_dev_resource##_bar##_attrs[] = { \ + &pci_dev_resource##_bar##_attr, \ + NULL, \ +}; \ + \ +static umode_t \ +pci_dev_resource##_bar##_attr_is_visible(struct kobject *kobj, \ + struct bin_attribute *a, \ + int n) \ +{ \ + return pci_dev_resource_attr_is_visible(kobj, a, _bar, false); \ +}; \ + \ +static const struct \ +attribute_group pci_dev_resource##_bar##_attr_group = { \ + .bin_attrs = pci_dev_resource##_bar##_attrs, \ + .is_bin_visible = pci_dev_resource##_bar##_attr_is_visible, \ +}; \ + \ +static struct bin_attribute \ +pci_dev_resource##_bar##_wc_attr = __BIN_ATTR(resource##_bar##_wc, \ + 0600, NULL, NULL, 0); \ + \ +static struct bin_attribute *pci_dev_resource##_bar##_wc_attrs[] = { \ + &pci_dev_resource##_bar##_wc_attr, \ + NULL, \ +}; \ + \ +static umode_t \ +pci_dev_resource##_bar##_wc_attr_is_visible(struct kobject *kobj, \ + struct bin_attribute *a, \ + int n) \ +{ \ + return pci_dev_resource_attr_is_visible(kobj, a, _bar, true); \ +}; \ + \ +static const struct \ +attribute_group pci_dev_resource##_bar##_wc_attr_group = { \ + .bin_attrs = pci_dev_resource##_bar##_wc_attrs, \ + .is_bin_visible = pci_dev_resource##_bar##_wc_attr_is_visible, \ +} #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } From patchwork Wed Aug 25 21:22:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= X-Patchwork-Id: 12458551 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98FEEC43216 for ; Wed, 25 Aug 2021 21:23:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 73153610A1 for ; Wed, 25 Aug 2021 21:23:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230512AbhHYVXr (ORCPT ); Wed, 25 Aug 2021 17:23:47 -0400 Received: from mail-wr1-f49.google.com ([209.85.221.49]:40507 "EHLO mail-wr1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229923AbhHYVXr (ORCPT ); Wed, 25 Aug 2021 17:23:47 -0400 Received: by mail-wr1-f49.google.com with SMTP id h4so1452116wro.7 for ; Wed, 25 Aug 2021 14:23:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sg4irG8b5rch04tDw1TShjMU6M88smfUkwqMmMQ/f2o=; b=cjohMt1ug2p+yy0/MhhDWfi1p+Zi5ptPs+/gw8rY6yXvZ3/xzbSfALWrSdGlpokQGz 1Hrxvfz7uYLs6nNy5zJbyw8K7zLlydLRSofyhiR8M7V2zsf5Dv7r/nQlLTESltyimNhN tRLEnCxbBCM+ifOJTqQWw3gqnoBrpBxs8Ezs1NeSXutT/8oIoekkjwsogG3U4R+oIPff gHDDXZDcNFqYsxKVzwIkAsGIX8azKINCET3V0Xv9n8Z2YZGkGb/MtYJj/a2l346pjnfA 0DdJqiqXAJQshyi4ifD1C2A6VOqA1OdRLzYVKHO3u+JSlfHdtRvdf7PKT1avtceJmEP7 Ydog== X-Gm-Message-State: AOAM532sHp5L0y4fGawsxl0Y7HHxPwBrtDBmWpgcCakJ4TspQXknj6b7 8GYOxPxjc23ifKtFhQvsvIg= X-Google-Smtp-Source: ABdhPJz0Fi5safNNBl9zcCUD8DSisdncCLE6PSweAN2Sr8o3vZ6fm43gmmoc4bk7RZIlG+JNnJMiGg== X-Received: by 2002:a5d:5107:: with SMTP id s7mr147481wrt.283.1629926580481; Wed, 25 Aug 2021 14:23:00 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id d24sm663527wmb.35.2021.08.25.14.22.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 14:23:00 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: linux-pci@vger.kernel.org Subject: [PATCH 3/4] PCI/sysfs: Add pci_dev_resource_group() macro Date: Wed, 25 Aug 2021 21:22:54 +0000 Message-Id: <20210825212255.878043-4-kw@linux.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210825212255.878043-1-kw@linux.com> References: <20210825212255.878043-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The pci_dev_resource_group() macro will be used to reduce unnecessary code repetition following the use of the pci_dev_resource_attr() macro when adding each of the many newly created resource groups to the list of other PCI sysfs objects stored in the pci_dev_groups array. Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 6eba5c0887df..97ab9da47dca 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1324,6 +1324,11 @@ attribute_group pci_dev_resource##_bar##_wc_attr_group = { \ .bin_attrs = pci_dev_resource##_bar##_wc_attrs, \ .is_bin_visible = pci_dev_resource##_bar##_wc_attr_is_visible, \ } + +#define pci_dev_resource_group(_bar) \ + &pci_dev_resource##_bar##_attr_group, \ + &pci_dev_resource##_bar##_wc_attr_group + #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } From patchwork Wed Aug 25 21:22:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= X-Patchwork-Id: 12458555 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-21.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2DC0C432BE for ; Wed, 25 Aug 2021 21:23:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AC962610A1 for ; Wed, 25 Aug 2021 21:23:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229923AbhHYVXt (ORCPT ); Wed, 25 Aug 2021 17:23:49 -0400 Received: from mail-wm1-f53.google.com ([209.85.128.53]:53936 "EHLO mail-wm1-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231873AbhHYVXs (ORCPT ); Wed, 25 Aug 2021 17:23:48 -0400 Received: by mail-wm1-f53.google.com with SMTP id i3so437354wmq.3 for ; Wed, 25 Aug 2021 14:23:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0tU8SFtgNRPp5HgGFE22Elz6Eg+LrpgBNlT4dGkj3h0=; b=pn/Jr/jcg0PNjWIgK/ae97qbXzhZVD6dDFbc2tbgC54l5JKPE87rChzqJWXr1NvGFb TVs545i8L45rHJ/GmQ7Sj2F0Mj906xNFSvn12RyDiF9R1ZEm7nPze7wMnab86wZnbndU zaKn26tTbjOgLbdldzgJd/hrvWW1ziboHxflxIygv6XKTzoA0oBlwdZDeB2YvNF8KMSn 54CKm4M3VAb985AAb6gi2o874L81TDTcXM1UQyOLCHvYnd3PSGdpcX1OXfmfu1EIcBd7 G4aeqiy6PuhL3q11bWilC22ib0SdECskVeeATD24XdV6A6UNDhJSjhJyrpw4X6SH2EAf +vxg== X-Gm-Message-State: AOAM530sanxItu7nqcPI5FFTCIfNSh57EuxznW0I0B/ovMvlaszWAdjb AzAvaxT+RJnJQpopT1PDQsmc+BUpoVw= X-Google-Smtp-Source: ABdhPJyC4jMrGIRKu0tJZFGV4T530w9OaZgfBozE1suNzlZTGo+vAc8rqb3qSR1YRJGSS/MC2XpRWg== X-Received: by 2002:a1c:98d2:: with SMTP id a201mr434293wme.89.1629926581400; Wed, 25 Aug 2021 14:23:01 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id d24sm663527wmb.35.2021.08.25.14.23.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 14:23:00 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: linux-pci@vger.kernel.org Subject: [PATCH 4/4] PCI/sysfs: Convert PCI resource files to static attributes Date: Wed, 25 Aug 2021 21:22:55 +0000 Message-Id: <20210825212255.878043-5-kw@linux.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210825212255.878043-1-kw@linux.com> References: <20210825212255.878043-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The PCI resource files are a series of sysfs objects that allow for access to the PCI BAR address space. Previously, the PCI resource files for platforms that provide either the HAVE_PCI_MMAP or ARCH_GENERIC_PCI_MMAP_RESOURCE definitions were dynamically created by pci_bus_add_device() or the pci_sysfs_init() late_initcall, but since these objects do not need to be created or removed dynamically, we can use static attributes so the device model takes care of addition and removal automatically. Thus, convert the PCI resources files to static attributes on supported platforms using the pci_dev_resource_attr() macro. This macro makes use of the .is_bin_visible() callback to correctly handle different types of PCI BAR address spaces while also providing support for creating either normal or write-combine attributes as required. Platforms that do currently do not define either the HAVE_PCI_MMAP or ARCH_GENERIC_PCI_MMAP_RESOURCE such as the Alpha platform can keep using their platform-specific methods for adding PCI resources files. Also remove pci_create_resource_files(), pci_remove_resource_files() and pci_create_attr() functions, which are no longer needed. The PCI resource files were first added in the pre-Git era: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/drivers/pci/pci-sysfs.c?id=42298be0eeb5ae98453b3374c36161b05a46c5dc Support for write-combine resources was added in commit 45aec1ae72fc ("x86: PAT export resource_wc in pci sysfs"). Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 129 ++++++++-------------------------------- include/linux/pci.h | 2 + 2 files changed, 28 insertions(+), 103 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 97ab9da47dca..9f0062bdb9ab 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1135,109 +1135,6 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, return pci_resource_io(filp, kobj, attr, buf, off, count, true); } -/** - * pci_remove_resource_files - cleanup resource files - * @pdev: dev to cleanup - * - * If we created resource files for @pdev, remove them from sysfs and - * free their resources. - */ -static void pci_remove_resource_files(struct pci_dev *pdev) -{ - int i; - - for (i = 0; i < PCI_STD_NUM_BARS; i++) { - struct bin_attribute *res_attr; - - res_attr = pdev->res_attr[i]; - if (res_attr) { - sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); - kfree(res_attr); - } - - res_attr = pdev->res_attr_wc[i]; - if (res_attr) { - sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); - kfree(res_attr); - } - } -} - -static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) -{ - /* allocate attribute structure, piggyback attribute name */ - int name_len = write_combine ? 13 : 10; - struct bin_attribute *res_attr; - char *res_attr_name; - int retval; - - res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); - if (!res_attr) - return -ENOMEM; - - res_attr_name = (char *)(res_attr + 1); - - sysfs_bin_attr_init(res_attr); - if (write_combine) { - pdev->res_attr_wc[num] = res_attr; - sprintf(res_attr_name, "resource%d_wc", num); - res_attr->mmap = pci_mmap_resource_wc; - } else { - pdev->res_attr[num] = res_attr; - sprintf(res_attr_name, "resource%d", num); - if (pci_resource_flags(pdev, num) & IORESOURCE_IO) { - res_attr->read = pci_read_resource_io; - res_attr->write = pci_write_resource_io; - if (arch_can_pci_mmap_io()) - res_attr->mmap = pci_mmap_resource_uc; - } else { - res_attr->mmap = pci_mmap_resource_uc; - } - } - if (res_attr->mmap) - res_attr->f_mapping = iomem_get_mapping; - res_attr->attr.name = res_attr_name; - res_attr->attr.mode = 0600; - res_attr->size = pci_resource_len(pdev, num); - res_attr->private = (void *)(unsigned long)num; - retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); - if (retval) - kfree(res_attr); - - return retval; -} - -/** - * pci_create_resource_files - create resource files in sysfs for @dev - * @pdev: dev in question - * - * Walk the resources in @pdev creating files for each resource available. - */ -static int pci_create_resource_files(struct pci_dev *pdev) -{ - int i; - int retval; - - /* Expose the PCI resources from this device as files */ - for (i = 0; i < PCI_STD_NUM_BARS; i++) { - - /* skip empty resources */ - if (!pci_resource_len(pdev, i)) - continue; - - retval = pci_create_attr(pdev, i, 0); - /* for prefetchable resources, create a WC mappable file */ - if (!retval && arch_can_pci_mmap_wc() && - pdev->resource[i].flags & IORESOURCE_PREFETCH) - retval = pci_create_attr(pdev, i, 1); - if (retval) { - pci_remove_resource_files(pdev); - return retval; - } - } - return 0; -} - static umode_t pci_dev_resource_attr_is_visible(struct kobject *kobj, struct bin_attribute *attr, int bar, bool write_combine) @@ -1329,6 +1226,13 @@ attribute_group pci_dev_resource##_bar##_wc_attr_group = { \ &pci_dev_resource##_bar##_attr_group, \ &pci_dev_resource##_bar##_wc_attr_group +pci_dev_resource_attr(0); +pci_dev_resource_attr(1); +pci_dev_resource_attr(2); +pci_dev_resource_attr(3); +pci_dev_resource_attr(4); +pci_dev_resource_attr(5); + #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } @@ -1470,6 +1374,10 @@ static const struct attribute_group pci_dev_reset_attr_group = { .is_visible = pci_dev_reset_attr_is_visible, }; +#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) +int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; } +void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { } +#else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) { if (!sysfs_initialized) @@ -1491,9 +1399,15 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev) pci_remove_resource_files(pdev); } +#endif static int __init pci_sysfs_init(void) { +#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) + struct pci_bus *pbus = NULL; + + sysfs_initialized = 1; +#else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ struct pci_dev *pdev = NULL; struct pci_bus *pbus = NULL; int retval; @@ -1507,6 +1421,7 @@ static int __init pci_sysfs_init(void) } } +#endif while ((pbus = pci_find_next_bus(pbus))) pci_create_legacy_files(pbus); @@ -1580,6 +1495,14 @@ static const struct attribute_group pci_dev_group = { const struct attribute_group *pci_dev_groups[] = { &pci_dev_group, +#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) + pci_dev_resource_group(0), + pci_dev_resource_group(1), + pci_dev_resource_group(2), + pci_dev_resource_group(3), + pci_dev_resource_group(4), + pci_dev_resource_group(5), +#endif &pci_dev_config_attr_group, &pci_dev_rom_attr_group, &pci_dev_reset_attr_group, diff --git a/include/linux/pci.h b/include/linux/pci.h index 540b377ca8f6..3f460ea73f8f 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -459,8 +459,10 @@ struct pci_dev { u32 saved_config_space[16]; /* Config space saved at suspend time */ struct hlist_head saved_cap_space; int rom_attr_enabled; /* Display of ROM attribute enabled? */ + #if !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */ struct bin_attribute *res_attr_wc[DEVICE_COUNT_RESOURCE]; /* sysfs file for WC mapping of resources */ + #endif #ifdef CONFIG_HOTPLUG_PCI_PCIE unsigned int broken_cmd_compl:1; /* No compl for some cmds */