From nobody Thu Dec 18 07:10:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B6B3C2BB3F for ; Mon, 20 Nov 2023 22:00:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232483AbjKTWAT (ORCPT ); Mon, 20 Nov 2023 17:00:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232003AbjKTWAP (ORCPT ); Mon, 20 Nov 2023 17:00:15 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEBABC7 for ; Mon, 20 Nov 2023 14:00:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700517610; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5YknJrzt+mUcfRZD9fsxvrl+TzKmo/BIC31FKbthLKE=; b=g39P4wAXaqLdR++0cEPeKAVGHR6HhGaHz5p1cWabykc1HCiN0HOZ7yVrZGS+GHYV2o4Cke 4yJzmblnZUVTFyibFHmYlJR1VJBCv9d6sbCGQsY4CBlrGYH8UZkcCq/EB52sGoMcQYhUrW fn5M9FUTrudBsqyZQBtMuY8QL+I3d0k= Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-661-hYtjXP6RPCODIAcWWYxrtg-1; Mon, 20 Nov 2023 17:00:09 -0500 X-MC-Unique: hYtjXP6RPCODIAcWWYxrtg-1 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-1f93a01ef88so461184fac.1 for ; Mon, 20 Nov 2023 14:00:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700517608; x=1701122408; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5YknJrzt+mUcfRZD9fsxvrl+TzKmo/BIC31FKbthLKE=; b=Ww0m/fSbnY7AlFe6Ca/eE6f7s2jt7iclNgJJ2AoRe+D//S7GdSlDrtkVbAYN7V/EtC 42SF/P5FMdIHllcNJ5lw3aq/pOoVhA8nSnqIBkUN6Gf2t1gqaSfKpeoD1LYw/ryUpGUM bzGkTNzvIjFF1G0z3L5CBv+QZLi/slZ9nyVibrSbrOGA6g3mLjFOSO01G8FE739Yg723 zNPJ/fyb079m+KndchJI9aUH0LwSkBLvi9hSzUQRHmYvrWEpsj3c+483BfXK7ACylVoW 0p3D6BCx7m+t1PehLAeGluUgeMvhJmRqZVAovCWM8rdDxsOMPlcUPCs/lsAij9Na/7Ld lC0A== X-Gm-Message-State: AOJu0YzkFA6prAE4JlzgELeIIVSSrBUzZv3tSB0PsDuSK6+DagiQ6DKr XDIvlgNhuQyRjb12gxp16eITVbU8revZ174KtiWwc5+/2hsDExuTD52sCcYGI58qsd+JrFzedyF bRJQpeFJarL6P8sP7ZROAqgxx X-Received: by 2002:a05:6358:199b:b0:16d:abd9:f29e with SMTP id v27-20020a056358199b00b0016dabd9f29emr6547707rwn.2.1700517608425; Mon, 20 Nov 2023 14:00:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCIi4ZtnWPYH5Nbuh+PP3GwGRiVhr5nrSG3asXMxcfimwvOxj/XDAQGiRIbPn9EQ4+9RYwPg== X-Received: by 2002:a05:6358:199b:b0:16d:abd9:f29e with SMTP id v27-20020a056358199b00b0016dabd9f29emr6547661rwn.2.1700517608043; Mon, 20 Nov 2023 14:00:08 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.remote.csb ([2001:9e8:32dd:2700:227b:d2ff:fe26:2a7a]) by smtp.gmail.com with ESMTPSA id 3-20020ad45ba3000000b00677fb918337sm2762398qvq.53.2023.11.20.14.00.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 14:00:07 -0800 (PST) From: Philipp Stanner To: Bjorn Helgaas , Arnd Bergmann , Andrew Morton , Randy Dunlap , Jason Gunthorpe , Eric Auger , Kent Overstreet , Niklas Schnelle , NeilBrown , Philipp Stanner , John Sanpe , Dave Jiang , Yury Norov , Kees Cook , "Masami Hiramatsu (Google)" , David Gow , Herbert Xu , Thomas Gleixner , "wuqiang.matt" , Jason Baron , Ben Dooks , Danilo Krummrich Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH 1/4] lib: move pci_iomap.c to drivers/pci/ Date: Mon, 20 Nov 2023 22:59:43 +0100 Message-ID: <20231120215945.52027-3-pstanner@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231120215945.52027-2-pstanner@redhat.com> References: <20231120215945.52027-2-pstanner@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This file is guarded by an #ifdef CONFIG_PCI. It, consequently, does not belong to lib/ because it is not generic infrastructure. Move the file to drivers/pci/ and implement the necessary changes to Makefiles and Kconfigs. Suggested-by: Danilo Krummrich Signed-off-by: Philipp Stanner --- drivers/pci/Kconfig | 3 +++ drivers/pci/Makefile | 1 + lib/pci_iomap.c =3D> drivers/pci/iomap.c | 3 --- lib/Kconfig | 3 --- lib/Makefile | 1 - 5 files changed, 4 insertions(+), 7 deletions(-) rename lib/pci_iomap.c =3D> drivers/pci/iomap.c (99%) diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig index 74147262625b..b54cacbc1160 100644 --- a/drivers/pci/Kconfig +++ b/drivers/pci/Kconfig @@ -34,6 +34,9 @@ config PCI_DOMAINS_GENERIC config PCI_SYSCALL bool =20 +config GENERIC_PCI_IOMAP + bool + source "drivers/pci/pcie/Kconfig" =20 config PCI_MSI diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile index cc8b4e01e29d..d6d0abfe59e2 100644 --- a/drivers/pci/Makefile +++ b/drivers/pci/Makefile @@ -33,6 +33,7 @@ obj-$(CONFIG_XEN_PCIDEV_FRONTEND) +=3D xen-pcifront.o obj-$(CONFIG_VGA_ARB) +=3D vgaarb.o obj-$(CONFIG_PCI_DOE) +=3D doe.o obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) +=3D of_property.o +obj-$(CONFIG_GENERIC_PCI_IOMAP) +=3D iomap.o =20 # Endpoint library must be initialized before its users obj-$(CONFIG_PCI_ENDPOINT) +=3D endpoint/ diff --git a/lib/pci_iomap.c b/drivers/pci/iomap.c similarity index 99% rename from lib/pci_iomap.c rename to drivers/pci/iomap.c index ce39ce9f3526..0a9d503ba533 100644 --- a/lib/pci_iomap.c +++ b/drivers/pci/iomap.c @@ -9,7 +9,6 @@ =20 #include =20 -#ifdef CONFIG_PCI /** * pci_iomap_range - create a virtual mapping cookie for a PCI BAR * @dev: PCI device that owns the BAR @@ -176,5 +175,3 @@ void pci_iounmap(struct pci_dev *dev, void __iomem *p) EXPORT_SYMBOL(pci_iounmap); =20 #endif /* ARCH_WANTS_GENERIC_PCI_IOUNMAP */ - -#endif /* CONFIG_PCI */ diff --git a/lib/Kconfig b/lib/Kconfig index 3ea1c830efab..1bf859166ac7 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -70,9 +70,6 @@ source "lib/math/Kconfig" config NO_GENERIC_PCI_IOPORT_MAP bool =20 -config GENERIC_PCI_IOMAP - bool - config GENERIC_IOMAP bool select GENERIC_PCI_IOMAP diff --git a/lib/Makefile b/lib/Makefile index 6b09731d8e61..0800289ec6c5 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -153,7 +153,6 @@ CFLAGS_debug_info.o +=3D $(call cc-option, -femit-struc= t-debug-detailed=3Dany) obj-y +=3D math/ crypto/ =20 obj-$(CONFIG_GENERIC_IOMAP) +=3D iomap.o -obj-$(CONFIG_GENERIC_PCI_IOMAP) +=3D pci_iomap.o obj-$(CONFIG_HAS_IOMEM) +=3D iomap_copy.o devres.o obj-$(CONFIG_CHECK_SIGNATURE) +=3D check_signature.o obj-$(CONFIG_DEBUG_LOCKING_API_SELFTESTS) +=3D locking-selftest.o --=20 2.41.0 From nobody Thu Dec 18 07:10:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2546C5AE5E for ; Mon, 20 Nov 2023 22:00:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232662AbjKTWA3 (ORCPT ); Mon, 20 Nov 2023 17:00:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232486AbjKTWAV (ORCPT ); Mon, 20 Nov 2023 17:00:21 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2993FA for ; Mon, 20 Nov 2023 14:00:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700517616; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Eq2ZRfUhka1cgK83WvTQSH9yZbFEM+uRcjou29esFhY=; b=XAiVONMWQsl2hMfbLH/LOIUdA2fUXGLd+NFDtGjH7q4P2nOtt20ti2jv1pGxAGPm4gfDlx Mq2izdSMVLROOiUABuZOJIClVDhQNJDPanheUkQp5yIPV9K66YlfT/p9BXWJIRvTf4UcRS UbApSoiSXKAbQEJQRNgqNWp0WQX3ogY= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-509-SDacOf8vNpWG2nEUkUjrbQ-1; Mon, 20 Nov 2023 17:00:12 -0500 X-MC-Unique: SDacOf8vNpWG2nEUkUjrbQ-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-66fd88c39f6so12782306d6.0 for ; Mon, 20 Nov 2023 14:00:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700517612; x=1701122412; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Eq2ZRfUhka1cgK83WvTQSH9yZbFEM+uRcjou29esFhY=; b=Mu8wSSFBpgGUxUzTUjqkxTeQfAHQ27xHtrh/xQ1FgnKWlZo8Q6DC/jS0nteOkAOi/p YqBhvfCPNnfz+YDfdaDWXqmEGQrmycjTlk/AbqeB1FL5QUUO70mfhI/qS5QJbZ+QQUFd 2B/jtC1zfSsV1g3kVekzh/6t/3elZHdPzoTSEZRRUAFJG5ZS9v3dE3KhK+IM7pl7+gp3 685EZxJmPaioaMn6BGXf10gUkZ1aV2r50h9PdMaaXz4040sPBx99ffhl0bkHESZxDVrl 6VRTgMuhs4MpgcDZehrmWmEeug17Xw0GqrB9nVzSazIxpUSMMRDVRCZbHvxD8R1kGZmf 1oXw== X-Gm-Message-State: AOJu0YwZ8793qezq6F0m72Cv1M2TATLEFVcL3JoX16gvsDnQAdQKg9cq OBg6wJn1jIoHrAHc4cwLbV55q11Go7d/un9Jxt9EU089E7CqrN4TffSLnLZ71qSrI5MK5JOBKFa hnNSZCVOhHwScHKcMpfuDjlq4 X-Received: by 2002:a05:6214:2d12:b0:678:50:6975 with SMTP id mz18-20020a0562142d1200b0067800506975mr8227199qvb.6.1700517611827; Mon, 20 Nov 2023 14:00:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IEjdsdVZkoHMWC8MK4FWXrvJKODX8L4X1gCcINeV5ndT6Em82C4tLTR0VraLginwydJaXljEw== X-Received: by 2002:a05:6214:2d12:b0:678:50:6975 with SMTP id mz18-20020a0562142d1200b0067800506975mr8227177qvb.6.1700517611499; Mon, 20 Nov 2023 14:00:11 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.remote.csb ([2001:9e8:32dd:2700:227b:d2ff:fe26:2a7a]) by smtp.gmail.com with ESMTPSA id 3-20020ad45ba3000000b00677fb918337sm2762398qvq.53.2023.11.20.14.00.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 14:00:11 -0800 (PST) From: Philipp Stanner To: Bjorn Helgaas , Arnd Bergmann , Andrew Morton , Randy Dunlap , Jason Gunthorpe , Eric Auger , Kent Overstreet , Niklas Schnelle , NeilBrown , Philipp Stanner , John Sanpe , Dave Jiang , Yury Norov , Kees Cook , "Masami Hiramatsu (Google)" , David Gow , Herbert Xu , Thomas Gleixner , "wuqiang.matt" , Jason Baron , Ben Dooks , Danilo Krummrich Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH 2/4] lib: move pci-specific devres code to drivers/pci/ Date: Mon, 20 Nov 2023 22:59:44 +0100 Message-ID: <20231120215945.52027-4-pstanner@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231120215945.52027-2-pstanner@redhat.com> References: <20231120215945.52027-2-pstanner@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The pcim_*() functions in lib/devres.c are guarded by an #ifdef CONFIG_PCI and, thus, don't belong to this file. They are only ever used for pci and not generic infrastructure. Move all pcim_*() functions in lib/devres.c to drivers/pci/devres.c Suggested-by: Danilo Krummrich Signed-off-by: Philipp Stanner --- drivers/pci/Makefile | 2 +- drivers/pci/devres.c | 207 ++++++++++++++++++++++++++++++++++++++++++ lib/devres.c | 208 +------------------------------------------ 3 files changed, 209 insertions(+), 208 deletions(-) create mode 100644 drivers/pci/devres.c diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile index d6d0abfe59e2..e04a492d4465 100644 --- a/drivers/pci/Makefile +++ b/drivers/pci/Makefile @@ -5,7 +5,7 @@ obj-$(CONFIG_PCI) +=3D access.o bus.o probe.o host-bridge.o \ remove.o pci.o pci-driver.o search.o \ pci-sysfs.o rom.o setup-res.o irq.o vpd.o \ - setup-bus.o vc.o mmap.o setup-irq.o + setup-bus.o vc.o mmap.o setup-irq.o devres.o =20 obj-$(CONFIG_PCI) +=3D msi/ obj-$(CONFIG_PCI) +=3D pcie/ diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c new file mode 100644 index 000000000000..a3fd0d65cef1 --- /dev/null +++ b/drivers/pci/devres.c @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "pci.h" + +/* + * PCI iomap devres + */ +#define PCIM_IOMAP_MAX PCI_STD_NUM_BARS + +struct pcim_iomap_devres { + void __iomem *table[PCIM_IOMAP_MAX]; +}; + +static void pcim_iomap_release(struct device *gendev, void *res) +{ + struct pci_dev *dev =3D to_pci_dev(gendev); + struct pcim_iomap_devres *this =3D res; + int i; + + for (i =3D 0; i < PCIM_IOMAP_MAX; i++) + if (this->table[i]) + pci_iounmap(dev, this->table[i]); +} + +/** + * pcim_iomap_table - access iomap allocation table + * @pdev: PCI device to access iomap table for + * + * Access iomap allocation table for @dev. If iomap table doesn't + * exist and @pdev is managed, it will be allocated. All iomaps + * recorded in the iomap table are automatically unmapped on driver + * detach. + * + * This function might sleep when the table is first allocated but can + * be safely called without context and guaranteed to succeed once + * allocated. + */ +void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) +{ + struct pcim_iomap_devres *dr, *new_dr; + + dr =3D devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL); + if (dr) + return dr->table; + + new_dr =3D devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KER= NEL, + dev_to_node(&pdev->dev)); + if (!new_dr) + return NULL; + dr =3D devres_get(&pdev->dev, new_dr, NULL, NULL); + return dr->table; +} +EXPORT_SYMBOL(pcim_iomap_table); + +/** + * pcim_iomap - Managed pcim_iomap() + * @pdev: PCI device to iomap for + * @bar: BAR to iomap + * @maxlen: Maximum length of iomap + * + * Managed pci_iomap(). Map is automatically unmapped on driver + * detach. + */ +void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxl= en) +{ + void __iomem **tbl; + + BUG_ON(bar >=3D PCIM_IOMAP_MAX); + + tbl =3D (void __iomem **)pcim_iomap_table(pdev); + if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ + return NULL; + + tbl[bar] =3D pci_iomap(pdev, bar, maxlen); + return tbl[bar]; +} +EXPORT_SYMBOL(pcim_iomap); + +/** + * pcim_iounmap - Managed pci_iounmap() + * @pdev: PCI device to iounmap for + * @addr: Address to unmap + * + * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap(). + */ +void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) +{ + void __iomem **tbl; + int i; + + pci_iounmap(pdev, addr); + + tbl =3D (void __iomem **)pcim_iomap_table(pdev); + BUG_ON(!tbl); + + for (i =3D 0; i < PCIM_IOMAP_MAX; i++) + if (tbl[i] =3D=3D addr) { + tbl[i] =3D NULL; + return; + } + WARN_ON(1); +} +EXPORT_SYMBOL(pcim_iounmap); + +/** + * pcim_iomap_regions - Request and iomap PCI BARs + * @pdev: PCI device to map IO resources for + * @mask: Mask of BARs to request and iomap + * @name: Name used when requesting regions + * + * Request and iomap regions specified by @mask. + */ +int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) +{ + void __iomem * const *iomap; + int i, rc; + + iomap =3D pcim_iomap_table(pdev); + if (!iomap) + return -ENOMEM; + + for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) { + unsigned long len; + + if (!(mask & (1 << i))) + continue; + + rc =3D -EINVAL; + len =3D pci_resource_len(pdev, i); + if (!len) + goto err_inval; + + rc =3D pci_request_region(pdev, i, name); + if (rc) + goto err_inval; + + rc =3D -ENOMEM; + if (!pcim_iomap(pdev, i, 0)) + goto err_region; + } + + return 0; + + err_region: + pci_release_region(pdev, i); + err_inval: + while (--i >=3D 0) { + if (!(mask & (1 << i))) + continue; + pcim_iounmap(pdev, iomap[i]); + pci_release_region(pdev, i); + } + + return rc; +} +EXPORT_SYMBOL(pcim_iomap_regions); + +/** + * pcim_iomap_regions_request_all - Request all BARs and iomap specified o= nes + * @pdev: PCI device to map IO resources for + * @mask: Mask of BARs to iomap + * @name: Name used when requesting regions + * + * Request all PCI BARs and iomap regions specified by @mask. + */ +int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, + const char *name) +{ + int request_mask =3D ((1 << 6) - 1) & ~mask; + int rc; + + rc =3D pci_request_selected_regions(pdev, request_mask, name); + if (rc) + return rc; + + rc =3D pcim_iomap_regions(pdev, mask, name); + if (rc) + pci_release_selected_regions(pdev, request_mask); + return rc; +} +EXPORT_SYMBOL(pcim_iomap_regions_request_all); + +/** + * pcim_iounmap_regions - Unmap and release PCI BARs + * @pdev: PCI device to map IO resources for + * @mask: Mask of BARs to unmap and release + * + * Unmap and release regions specified by @mask. + */ +void pcim_iounmap_regions(struct pci_dev *pdev, int mask) +{ + void __iomem * const *iomap; + int i; + + iomap =3D pcim_iomap_table(pdev); + if (!iomap) + return; + + for (i =3D 0; i < PCIM_IOMAP_MAX; i++) { + if (!(mask & (1 << i))) + continue; + + pcim_iounmap(pdev, iomap[i]); + pci_release_region(pdev, i); + } +} +EXPORT_SYMBOL(pcim_iounmap_regions); diff --git a/lib/devres.c b/lib/devres.c index c44f104b58d5..fe0c63caeb68 100644 --- a/lib/devres.c +++ b/lib/devres.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include -#include #include #include #include @@ -311,212 +311,6 @@ void devm_ioport_unmap(struct device *dev, void __iom= em *addr) EXPORT_SYMBOL(devm_ioport_unmap); #endif /* CONFIG_HAS_IOPORT_MAP */ =20 -#ifdef CONFIG_PCI -/* - * PCI iomap devres - */ -#define PCIM_IOMAP_MAX PCI_STD_NUM_BARS - -struct pcim_iomap_devres { - void __iomem *table[PCIM_IOMAP_MAX]; -}; - -static void pcim_iomap_release(struct device *gendev, void *res) -{ - struct pci_dev *dev =3D to_pci_dev(gendev); - struct pcim_iomap_devres *this =3D res; - int i; - - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) - if (this->table[i]) - pci_iounmap(dev, this->table[i]); -} - -/** - * pcim_iomap_table - access iomap allocation table - * @pdev: PCI device to access iomap table for - * - * Access iomap allocation table for @dev. If iomap table doesn't - * exist and @pdev is managed, it will be allocated. All iomaps - * recorded in the iomap table are automatically unmapped on driver - * detach. - * - * This function might sleep when the table is first allocated but can - * be safely called without context and guaranteed to succeed once - * allocated. - */ -void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) -{ - struct pcim_iomap_devres *dr, *new_dr; - - dr =3D devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL); - if (dr) - return dr->table; - - new_dr =3D devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KER= NEL, - dev_to_node(&pdev->dev)); - if (!new_dr) - return NULL; - dr =3D devres_get(&pdev->dev, new_dr, NULL, NULL); - return dr->table; -} -EXPORT_SYMBOL(pcim_iomap_table); - -/** - * pcim_iomap - Managed pcim_iomap() - * @pdev: PCI device to iomap for - * @bar: BAR to iomap - * @maxlen: Maximum length of iomap - * - * Managed pci_iomap(). Map is automatically unmapped on driver - * detach. - */ -void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxl= en) -{ - void __iomem **tbl; - - BUG_ON(bar >=3D PCIM_IOMAP_MAX); - - tbl =3D (void __iomem **)pcim_iomap_table(pdev); - if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ - return NULL; - - tbl[bar] =3D pci_iomap(pdev, bar, maxlen); - return tbl[bar]; -} -EXPORT_SYMBOL(pcim_iomap); - -/** - * pcim_iounmap - Managed pci_iounmap() - * @pdev: PCI device to iounmap for - * @addr: Address to unmap - * - * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap(). - */ -void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) -{ - void __iomem **tbl; - int i; - - pci_iounmap(pdev, addr); - - tbl =3D (void __iomem **)pcim_iomap_table(pdev); - BUG_ON(!tbl); - - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) - if (tbl[i] =3D=3D addr) { - tbl[i] =3D NULL; - return; - } - WARN_ON(1); -} -EXPORT_SYMBOL(pcim_iounmap); - -/** - * pcim_iomap_regions - Request and iomap PCI BARs - * @pdev: PCI device to map IO resources for - * @mask: Mask of BARs to request and iomap - * @name: Name used when requesting regions - * - * Request and iomap regions specified by @mask. - */ -int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) -{ - void __iomem * const *iomap; - int i, rc; - - iomap =3D pcim_iomap_table(pdev); - if (!iomap) - return -ENOMEM; - - for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) { - unsigned long len; - - if (!(mask & (1 << i))) - continue; - - rc =3D -EINVAL; - len =3D pci_resource_len(pdev, i); - if (!len) - goto err_inval; - - rc =3D pci_request_region(pdev, i, name); - if (rc) - goto err_inval; - - rc =3D -ENOMEM; - if (!pcim_iomap(pdev, i, 0)) - goto err_region; - } - - return 0; - - err_region: - pci_release_region(pdev, i); - err_inval: - while (--i >=3D 0) { - if (!(mask & (1 << i))) - continue; - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); - } - - return rc; -} -EXPORT_SYMBOL(pcim_iomap_regions); - -/** - * pcim_iomap_regions_request_all - Request all BARs and iomap specified o= nes - * @pdev: PCI device to map IO resources for - * @mask: Mask of BARs to iomap - * @name: Name used when requesting regions - * - * Request all PCI BARs and iomap regions specified by @mask. - */ -int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, - const char *name) -{ - int request_mask =3D ((1 << 6) - 1) & ~mask; - int rc; - - rc =3D pci_request_selected_regions(pdev, request_mask, name); - if (rc) - return rc; - - rc =3D pcim_iomap_regions(pdev, mask, name); - if (rc) - pci_release_selected_regions(pdev, request_mask); - return rc; -} -EXPORT_SYMBOL(pcim_iomap_regions_request_all); - -/** - * pcim_iounmap_regions - Unmap and release PCI BARs - * @pdev: PCI device to map IO resources for - * @mask: Mask of BARs to unmap and release - * - * Unmap and release regions specified by @mask. - */ -void pcim_iounmap_regions(struct pci_dev *pdev, int mask) -{ - void __iomem * const *iomap; - int i; - - iomap =3D pcim_iomap_table(pdev); - if (!iomap) - return; - - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) { - if (!(mask & (1 << i))) - continue; - - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); - } -} -EXPORT_SYMBOL(pcim_iounmap_regions); -#endif /* CONFIG_PCI */ - static void devm_arch_phys_ac_add_release(struct device *dev, void *res) { arch_phys_wc_del(*((int *)res)); --=20 2.41.0 From nobody Thu Dec 18 07:10:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 576BAC197A0 for ; Mon, 20 Nov 2023 22:00:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232630AbjKTWAc (ORCPT ); Mon, 20 Nov 2023 17:00:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232543AbjKTWA0 (ORCPT ); Mon, 20 Nov 2023 17:00:26 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0EC9E8 for ; Mon, 20 Nov 2023 14:00:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700517619; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ndwg8yWgO7u2AWRVzzWdZxowrMw7/s/C0lAlAam8dBc=; b=GB1G6wtb6ZItvbaIlDMaJVJy8ByymrIQtEBkGjbUs9oz7SgfkjKw0Vlew7NwrGlrZPJfuO IHqG6degxDz961OboMpskyVdjCEQcCN7yfCYXdbOnxkqZw8cJQ1AItuyyqAK5relaI3nDb 2zVwIa28qG87TKJIj4zlH4FS4L4iexo= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-447-39_In3q7N4Skqt8NdPZ_fg-1; Mon, 20 Nov 2023 17:00:16 -0500 X-MC-Unique: 39_In3q7N4Skqt8NdPZ_fg-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-daf165b2a14so954137276.1 for ; Mon, 20 Nov 2023 14:00:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700517616; x=1701122416; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ndwg8yWgO7u2AWRVzzWdZxowrMw7/s/C0lAlAam8dBc=; b=agBglMT84rG2Qu644ES8dMz7GyPBGEG7azUcPlGOn8Jm9tfhqdSGTYGnLd2USWUdhm dB/n78PO2LmkgWZFXghobQOrWgTbQ/AgcS/FrQ7x3JdeDDRW6mN2BhkDuQCKwFKOyU+Z lswhverBBZr9ObxvVvrAxeVXFwLrzgrDPW/0d/WPbR6wMtWM6QocLfR/Df42VUxoezvJ 4FLk75hgJboq6rUxPHOacp2L8mMt6+lRCrPz8WT3fhlCcvE+MsAQUPccBSkQFMKTt34J LpZaDsycoWujtKvN7w+fU1dGa3VygrD6PIUAaHCTM3s70U5KN4sbqmViMup0AZhJYtDk Qf4w== X-Gm-Message-State: AOJu0YwBLP/e/bDlIzCjwCJjwd3NgYFmMjXEWDcmGx/A7TOqAJrskJxf ZtARJkklYduvOhRJc4Uk2Dx/H/mmPQ0nta/R9xImHFTI+q0KCLpMNtxIl4MXuP+Xlw24LAxk8Yy bDm7AgejZE4RTdHHoSkCXBcyD X-Received: by 2002:a25:6c83:0:b0:daf:f963:6c4 with SMTP id h125-20020a256c83000000b00daff96306c4mr4919298ybc.2.1700517615873; Mon, 20 Nov 2023 14:00:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IG0vWNj+eR1Wv/K+VOG8kWN8G67fBWam7JvcGLJqk0PG33fUfN5Ukx03xmSFA5ck07GyFZpUg== X-Received: by 2002:a25:6c83:0:b0:daf:f963:6c4 with SMTP id h125-20020a256c83000000b00daff96306c4mr4919198ybc.2.1700517614976; Mon, 20 Nov 2023 14:00:14 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.remote.csb ([2001:9e8:32dd:2700:227b:d2ff:fe26:2a7a]) by smtp.gmail.com with ESMTPSA id 3-20020ad45ba3000000b00677fb918337sm2762398qvq.53.2023.11.20.14.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 14:00:14 -0800 (PST) From: Philipp Stanner To: Bjorn Helgaas , Arnd Bergmann , Andrew Morton , Randy Dunlap , Jason Gunthorpe , Eric Auger , Kent Overstreet , Niklas Schnelle , NeilBrown , Philipp Stanner , John Sanpe , Dave Jiang , Yury Norov , Kees Cook , "Masami Hiramatsu (Google)" , David Gow , Herbert Xu , Thomas Gleixner , "wuqiang.matt" , Jason Baron , Ben Dooks , Danilo Krummrich Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH 3/4] pci: move devres code from pci.c to devres.c Date: Mon, 20 Nov 2023 22:59:45 +0100 Message-ID: <20231120215945.52027-5-pstanner@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231120215945.52027-2-pstanner@redhat.com> References: <20231120215945.52027-2-pstanner@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The file pci.c is very large and contains a number of devres-functions. These functions should now reside in devres.c There are a few callers left in pci.c that do devres operations. These should be ported in the future. Corresponding TODOs are added by this commit. The reason they are not moved right now in this commit is that pci's devres currently implements a sort of "hybrid-mode": pci_request_region(), for instance, does not have a corresponding pcim_ equivalent, yet. Instead, the function can be made managed by previously calling pcim_enable_device() (instead of pci_enable_device()). This makes it unreasonable to move pci_request_region() to devres.c Moving the functions would require changes to pci's API and is, therefore, left for future work. Move as much devres-specific code from pci.c to devres.c as possible. Suggested-by: Danilo Krummrich Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 243 +++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 249 ------------------------------------------- drivers/pci/pci.h | 24 +++++ 3 files changed, 267 insertions(+), 249 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index a3fd0d65cef1..55f76a2c3748 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include #include "pci.h" =20 @@ -11,6 +12,248 @@ struct pcim_iomap_devres { void __iomem *table[PCIM_IOMAP_MAX]; }; =20 + +static void devm_pci_unmap_iospace(struct device *dev, void *ptr) +{ + struct resource **res =3D ptr; + + pci_unmap_iospace(*res); +} + +/** + * devm_pci_remap_iospace - Managed pci_remap_iospace() + * @dev: Generic device to remap IO address for + * @res: Resource describing the I/O space + * @phys_addr: physical address of range to be mapped + * + * Managed pci_remap_iospace(). Map is automatically unmapped on driver + * detach. + */ +int devm_pci_remap_iospace(struct device *dev, const struct resource *res, + phys_addr_t phys_addr) +{ + const struct resource **ptr; + int error; + + ptr =3D devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + error =3D pci_remap_iospace(res, phys_addr); + if (error) { + devres_free(ptr); + } else { + *ptr =3D res; + devres_add(dev, ptr); + } + + return error; +} +EXPORT_SYMBOL(devm_pci_remap_iospace); + +/** + * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace() + * @dev: Generic device to remap IO address for + * @offset: Resource address to map + * @size: Size of map + * + * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver + * detach. + */ +void __iomem *devm_pci_remap_cfgspace(struct device *dev, + resource_size_t offset, + resource_size_t size) +{ + void __iomem **ptr, *addr; + + ptr =3D devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return NULL; + + addr =3D pci_remap_cfgspace(offset, size); + if (addr) { + *ptr =3D addr; + devres_add(dev, ptr); + } else + devres_free(ptr); + + return addr; +} +EXPORT_SYMBOL(devm_pci_remap_cfgspace); + +/** + * devm_pci_remap_cfg_resource - check, request region and ioremap cfg res= ource + * @dev: generic device to handle the resource for + * @res: configuration space resource to be handled + * + * Checks that a resource is a valid memory region, requests the memory + * region and ioremaps with pci_remap_cfgspace() API that ensures the + * proper PCI configuration space memory attributes are guaranteed. + * + * All operations are managed and will be undone on driver detach. + * + * Returns a pointer to the remapped memory or an ERR_PTR() encoded error = code + * on failure. Usage example:: + * + * res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0); + * base =3D devm_pci_remap_cfg_resource(&pdev->dev, res); + * if (IS_ERR(base)) + * return PTR_ERR(base); + */ +void __iomem *devm_pci_remap_cfg_resource(struct device *dev, + struct resource *res) +{ + resource_size_t size; + const char *name; + void __iomem *dest_ptr; + + BUG_ON(!dev); + + if (!res || resource_type(res) !=3D IORESOURCE_MEM) { + dev_err(dev, "invalid resource\n"); + return IOMEM_ERR_PTR(-EINVAL); + } + + size =3D resource_size(res); + + if (res->name) + name =3D devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev), + res->name); + else + name =3D devm_kstrdup(dev, dev_name(dev), GFP_KERNEL); + if (!name) + return IOMEM_ERR_PTR(-ENOMEM); + + if (!devm_request_mem_region(dev, res->start, size, name)) { + dev_err(dev, "can't request region for resource %pR\n", res); + return IOMEM_ERR_PTR(-EBUSY); + } + + dest_ptr =3D devm_pci_remap_cfgspace(dev, res->start, size); + if (!dest_ptr) { + dev_err(dev, "ioremap failed for resource %pR\n", res); + devm_release_mem_region(dev, res->start, size); + dest_ptr =3D IOMEM_ERR_PTR(-ENOMEM); + } + + return dest_ptr; +} +EXPORT_SYMBOL(devm_pci_remap_cfg_resource); + +/** + * pcim_set_mwi - a device-managed pci_set_mwi() + * @dev: the PCI device for which MWI is enabled + * + * Managed pci_set_mwi(). + * + * RETURNS: An appropriate -ERRNO error value on error, or zero for succes= s. + */ +int pcim_set_mwi(struct pci_dev *dev) +{ + struct pci_devres *dr; + + dr =3D find_pci_dr(dev); + if (!dr) + return -ENOMEM; + + dr->mwi =3D 1; + return pci_set_mwi(dev); +} +EXPORT_SYMBOL(pcim_set_mwi); + + +static void pcim_release(struct device *gendev, void *res) +{ + struct pci_dev *dev =3D to_pci_dev(gendev); + struct pci_devres *this =3D res; + int i; + + for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) + if (this->region_mask & (1 << i)) + pci_release_region(dev, i); + + if (this->mwi) + pci_clear_mwi(dev); + + if (this->restore_intx) + pci_intx(dev, this->orig_intx); + + if (this->enabled && !this->pinned) + pci_disable_device(dev); +} + +/* + * TODO: + * Once the last four callers in pci.c are ported, this function here need= s to + * be made static again. + */ +struct pci_devres *find_pci_dr(struct pci_dev *pdev) +{ + if (pci_is_managed(pdev)) + return devres_find(&pdev->dev, pcim_release, NULL, NULL); + return NULL; +} +EXPORT_SYMBOL(find_pci_dr); // TODO do we need this? + +static struct pci_devres *get_pci_dr(struct pci_dev *pdev) +{ + struct pci_devres *dr, *new_dr; + + dr =3D devres_find(&pdev->dev, pcim_release, NULL, NULL); + if (dr) + return dr; + + new_dr =3D devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL); + if (!new_dr) + return NULL; + return devres_get(&pdev->dev, new_dr, NULL, NULL); +} + +/** + * pcim_enable_device - Managed pci_enable_device() + * @pdev: PCI device to be initialized + * + * Managed pci_enable_device(). + */ +int pcim_enable_device(struct pci_dev *pdev) +{ + struct pci_devres *dr; + int rc; + + dr =3D get_pci_dr(pdev); + if (unlikely(!dr)) + return -ENOMEM; + if (dr->enabled) + return 0; + + rc =3D pci_enable_device(pdev); + if (!rc) { + pdev->is_managed =3D 1; + dr->enabled =3D 1; + } + return rc; +} +EXPORT_SYMBOL(pcim_enable_device); + +/** + * pcim_pin_device - Pin managed PCI device + * @pdev: PCI device to pin + * + * Pin managed PCI device @pdev. Pinned device won't be disabled on + * driver detach. @pdev must have been enabled with + * pcim_enable_device(). + */ +void pcim_pin_device(struct pci_dev *pdev) +{ + struct pci_devres *dr; + + dr =3D find_pci_dr(pdev); + WARN_ON(!dr || !dr->enabled); + if (dr) + dr->pinned =3D 1; +} +EXPORT_SYMBOL(pcim_pin_device); + static void pcim_iomap_release(struct device *gendev, void *res) { struct pci_dev *dev =3D to_pci_dev(gendev); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 55bc3576a985..742b0a6545b6 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2088,107 +2088,6 @@ int pci_enable_device(struct pci_dev *dev) } EXPORT_SYMBOL(pci_enable_device); =20 -/* - * Managed PCI resources. This manages device on/off, INTx/MSI/MSI-X - * on/off and BAR regions. pci_dev itself records MSI/MSI-X status, so - * there's no need to track it separately. pci_devres is initialized - * when a device is enabled using managed PCI device enable interface. - */ -struct pci_devres { - unsigned int enabled:1; - unsigned int pinned:1; - unsigned int orig_intx:1; - unsigned int restore_intx:1; - unsigned int mwi:1; - u32 region_mask; -}; - -static void pcim_release(struct device *gendev, void *res) -{ - struct pci_dev *dev =3D to_pci_dev(gendev); - struct pci_devres *this =3D res; - int i; - - for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) - if (this->region_mask & (1 << i)) - pci_release_region(dev, i); - - if (this->mwi) - pci_clear_mwi(dev); - - if (this->restore_intx) - pci_intx(dev, this->orig_intx); - - if (this->enabled && !this->pinned) - pci_disable_device(dev); -} - -static struct pci_devres *get_pci_dr(struct pci_dev *pdev) -{ - struct pci_devres *dr, *new_dr; - - dr =3D devres_find(&pdev->dev, pcim_release, NULL, NULL); - if (dr) - return dr; - - new_dr =3D devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL); - if (!new_dr) - return NULL; - return devres_get(&pdev->dev, new_dr, NULL, NULL); -} - -static struct pci_devres *find_pci_dr(struct pci_dev *pdev) -{ - if (pci_is_managed(pdev)) - return devres_find(&pdev->dev, pcim_release, NULL, NULL); - return NULL; -} - -/** - * pcim_enable_device - Managed pci_enable_device() - * @pdev: PCI device to be initialized - * - * Managed pci_enable_device(). - */ -int pcim_enable_device(struct pci_dev *pdev) -{ - struct pci_devres *dr; - int rc; - - dr =3D get_pci_dr(pdev); - if (unlikely(!dr)) - return -ENOMEM; - if (dr->enabled) - return 0; - - rc =3D pci_enable_device(pdev); - if (!rc) { - pdev->is_managed =3D 1; - dr->enabled =3D 1; - } - return rc; -} -EXPORT_SYMBOL(pcim_enable_device); - -/** - * pcim_pin_device - Pin managed PCI device - * @pdev: PCI device to pin - * - * Pin managed PCI device @pdev. Pinned device won't be disabled on - * driver detach. @pdev must have been enabled with - * pcim_enable_device(). - */ -void pcim_pin_device(struct pci_dev *pdev) -{ - struct pci_devres *dr; - - dr =3D find_pci_dr(pdev); - WARN_ON(!dr || !dr->enabled); - if (dr) - dr->pinned =3D 1; -} -EXPORT_SYMBOL(pcim_pin_device); - /* * pcibios_device_add - provide arch specific hooks when adding device dev * @dev: the PCI device being added @@ -4281,133 +4180,6 @@ void pci_unmap_iospace(struct resource *res) } EXPORT_SYMBOL(pci_unmap_iospace); =20 -static void devm_pci_unmap_iospace(struct device *dev, void *ptr) -{ - struct resource **res =3D ptr; - - pci_unmap_iospace(*res); -} - -/** - * devm_pci_remap_iospace - Managed pci_remap_iospace() - * @dev: Generic device to remap IO address for - * @res: Resource describing the I/O space - * @phys_addr: physical address of range to be mapped - * - * Managed pci_remap_iospace(). Map is automatically unmapped on driver - * detach. - */ -int devm_pci_remap_iospace(struct device *dev, const struct resource *res, - phys_addr_t phys_addr) -{ - const struct resource **ptr; - int error; - - ptr =3D devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL); - if (!ptr) - return -ENOMEM; - - error =3D pci_remap_iospace(res, phys_addr); - if (error) { - devres_free(ptr); - } else { - *ptr =3D res; - devres_add(dev, ptr); - } - - return error; -} -EXPORT_SYMBOL(devm_pci_remap_iospace); - -/** - * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace() - * @dev: Generic device to remap IO address for - * @offset: Resource address to map - * @size: Size of map - * - * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver - * detach. - */ -void __iomem *devm_pci_remap_cfgspace(struct device *dev, - resource_size_t offset, - resource_size_t size) -{ - void __iomem **ptr, *addr; - - ptr =3D devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); - if (!ptr) - return NULL; - - addr =3D pci_remap_cfgspace(offset, size); - if (addr) { - *ptr =3D addr; - devres_add(dev, ptr); - } else - devres_free(ptr); - - return addr; -} -EXPORT_SYMBOL(devm_pci_remap_cfgspace); - -/** - * devm_pci_remap_cfg_resource - check, request region and ioremap cfg res= ource - * @dev: generic device to handle the resource for - * @res: configuration space resource to be handled - * - * Checks that a resource is a valid memory region, requests the memory - * region and ioremaps with pci_remap_cfgspace() API that ensures the - * proper PCI configuration space memory attributes are guaranteed. - * - * All operations are managed and will be undone on driver detach. - * - * Returns a pointer to the remapped memory or an ERR_PTR() encoded error = code - * on failure. Usage example:: - * - * res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0); - * base =3D devm_pci_remap_cfg_resource(&pdev->dev, res); - * if (IS_ERR(base)) - * return PTR_ERR(base); - */ -void __iomem *devm_pci_remap_cfg_resource(struct device *dev, - struct resource *res) -{ - resource_size_t size; - const char *name; - void __iomem *dest_ptr; - - BUG_ON(!dev); - - if (!res || resource_type(res) !=3D IORESOURCE_MEM) { - dev_err(dev, "invalid resource\n"); - return IOMEM_ERR_PTR(-EINVAL); - } - - size =3D resource_size(res); - - if (res->name) - name =3D devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev), - res->name); - else - name =3D devm_kstrdup(dev, dev_name(dev), GFP_KERNEL); - if (!name) - return IOMEM_ERR_PTR(-ENOMEM); - - if (!devm_request_mem_region(dev, res->start, size, name)) { - dev_err(dev, "can't request region for resource %pR\n", res); - return IOMEM_ERR_PTR(-EBUSY); - } - - dest_ptr =3D devm_pci_remap_cfgspace(dev, res->start, size); - if (!dest_ptr) { - dev_err(dev, "ioremap failed for resource %pR\n", res); - devm_release_mem_region(dev, res->start, size); - dest_ptr =3D IOMEM_ERR_PTR(-ENOMEM); - } - - return dest_ptr; -} -EXPORT_SYMBOL(devm_pci_remap_cfg_resource); - static void __pci_set_master(struct pci_dev *dev, bool enable) { u16 old_cmd, cmd; @@ -4557,27 +4329,6 @@ int pci_set_mwi(struct pci_dev *dev) } EXPORT_SYMBOL(pci_set_mwi); =20 -/** - * pcim_set_mwi - a device-managed pci_set_mwi() - * @dev: the PCI device for which MWI is enabled - * - * Managed pci_set_mwi(). - * - * RETURNS: An appropriate -ERRNO error value on error, or zero for succes= s. - */ -int pcim_set_mwi(struct pci_dev *dev) -{ - struct pci_devres *dr; - - dr =3D find_pci_dr(dev); - if (!dr) - return -ENOMEM; - - dr->mwi =3D 1; - return pci_set_mwi(dev); -} -EXPORT_SYMBOL(pcim_set_mwi); - /** * pci_try_set_mwi - enables memory-write-invalidate PCI transaction * @dev: the PCI device for which MWI is enabled diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 5ecbcf041179..69052059dbd2 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -793,6 +793,30 @@ static inline pci_power_t mid_pci_get_power_state(stru= ct pci_dev *pdev) } #endif =20 +/* + * TODO: + * The following two components wouldn't need to be here if they weren't u= sed at + * four last places in pci.c + * Port or move these functions to devres.c and then remove the + * pci_devres-components from this header file here. + */ +/* + * Managed PCI resources. This manages device on/off, INTx/MSI/MSI-X + * on/off and BAR regions. pci_dev itself records MSI/MSI-X status, so + * there's no need to track it separately. pci_devres is initialized + * when a device is enabled using managed PCI device enable interface. + */ +struct pci_devres { + unsigned int enabled:1; + unsigned int pinned:1; + unsigned int orig_intx:1; + unsigned int restore_intx:1; + unsigned int mwi:1; + u32 region_mask; +}; + +struct pci_devres *find_pci_dr(struct pci_dev *pdev); + /* * Config Address for PCI Configuration Mechanism #1 * --=20 2.41.0 From nobody Thu Dec 18 07:10:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D27BEC197A0 for ; Mon, 20 Nov 2023 22:00:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232755AbjKTWAg (ORCPT ); Mon, 20 Nov 2023 17:00:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232598AbjKTWA0 (ORCPT ); Mon, 20 Nov 2023 17:00:26 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04CB9124 for ; Mon, 20 Nov 2023 14:00:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700517621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N6mM1O9Y4nGezPDwxGvKwG2kSdvz61Ixp5WJTexZ/DY=; b=EBeigtjL0Ans0Am+cIVSf1wGfNaVoh+XdaxfbiMzlr7qVKfhYCPQR8N8rQ+VwXwibzjWt8 hUOiEV7LPMRmdYj8K5BwuJeTmcNRQJj+NQDbWJ8h2Lb8vRvuPYjqehLxfWPsx2JoC3VQDd c0yt0G3qxktQvMo7BM9m+6YnerwQh2w= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-481-avxLPpajM3Wkv2lLH2i_wQ-1; Mon, 20 Nov 2023 17:00:19 -0500 X-MC-Unique: avxLPpajM3Wkv2lLH2i_wQ-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-677fb270eacso8332366d6.1 for ; Mon, 20 Nov 2023 14:00:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700517619; x=1701122419; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N6mM1O9Y4nGezPDwxGvKwG2kSdvz61Ixp5WJTexZ/DY=; b=IBNd/Yx1I4AdyPPsWKPpjHSRO7OpHQWZN3cJ3VoEV/7upGSq3k8VG1zNib7GfZR7ro HfDHJUToWJvIumKQSxflC+wlki6q6yHW2JmHChSOiCvp58XafEQd1FdoBTpyew7BFduH KgNsxc10+cX5SzgxAID5QRzRW5M5zgWNu9mG2cYT1ggMVOxSalJoHJ6Du49aoizWlJBd eHTnBgNAIZs05ZTRasLmvX/CqIHxHPohczbcpv6s7w2SKVNy4eiD8qUq7XDmCyfEH0V/ d9Z1RoTEB/jAXkRs/Ytf/kh9CawHQDuiGEq6i4OUAM65JH+blBXa1mIcgg2LHtG3thU5 4M5Q== X-Gm-Message-State: AOJu0YyNHwUJLqmFMHelJ70/CzJuGXD3g8Qx7Qi8XhZ7/UfhGcVIyfOa +oFdNuJvOkAvY7JWxSwpDxzGPgNsHw2fo8YD4lmezTuuK7Sj+BPWVXYYbqtLpEVYt5wPvSag5vE A+MnOfSEDlHDZWGYHkNrQLYTA X-Received: by 2002:a05:6214:2d12:b0:678:50:6975 with SMTP id mz18-20020a0562142d1200b0067800506975mr8227690qvb.6.1700517618744; Mon, 20 Nov 2023 14:00:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDNBpKfZvVPUrrZYBAMZoGj9EGU7GT1CpOL/fHM0KWf1UWxIehkkDc+4xA8TO8dDshmepQvw== X-Received: by 2002:a05:6214:2d12:b0:678:50:6975 with SMTP id mz18-20020a0562142d1200b0067800506975mr8227634qvb.6.1700517618295; Mon, 20 Nov 2023 14:00:18 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.remote.csb ([2001:9e8:32dd:2700:227b:d2ff:fe26:2a7a]) by smtp.gmail.com with ESMTPSA id 3-20020ad45ba3000000b00677fb918337sm2762398qvq.53.2023.11.20.14.00.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 14:00:18 -0800 (PST) From: Philipp Stanner To: Bjorn Helgaas , Arnd Bergmann , Andrew Morton , Randy Dunlap , Jason Gunthorpe , Eric Auger , Kent Overstreet , Niklas Schnelle , NeilBrown , Philipp Stanner , John Sanpe , Dave Jiang , Yury Norov , Kees Cook , "Masami Hiramatsu (Google)" , David Gow , Herbert Xu , Thomas Gleixner , "wuqiang.matt" , Jason Baron , Ben Dooks , Danilo Krummrich Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH 4/4] lib/iomap.c: improve comment about pci anomaly Date: Mon, 20 Nov 2023 22:59:46 +0100 Message-ID: <20231120215945.52027-6-pstanner@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231120215945.52027-2-pstanner@redhat.com> References: <20231120215945.52027-2-pstanner@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" lib/iomap.c contains one of the definitions of pci_iounmap(). The current comment above this out-of-place function does not clarify WHY the function is defined here. Linus's detailed comment above pci_iounmap() in drivers/pci/iomap.c clarifies that in a far better way. Extend the existing comment with an excerpt from Linus's and hint at the other implementation in drivers/pci/iomap.c Signed-off-by: Philipp Stanner --- lib/iomap.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/iomap.c b/lib/iomap.c index 4f8b31baa575..647aac8ea3e3 100644 --- a/lib/iomap.c +++ b/lib/iomap.c @@ -419,8 +419,17 @@ EXPORT_SYMBOL(ioport_unmap); #endif /* CONFIG_HAS_IOPORT_MAP */ =20 #ifdef CONFIG_PCI -/* Hide the details if this is a MMIO or PIO address space and just do what - * you expect in the correct way. */ +/* + * Hide the details if this is a MMIO or PIO address space and just do what + * you expect in the correct way. + * + * pci_iounmap() somewhat illogically comes from lib/iomap.c for the + * CONFIG_GENERIC_IOMAP case, because that's the code that knows about + * the different IOMAP ranges. + * + * For more details see also the pci_iounmap() implementation in + * drivers/pci/iomap.c + */ void pci_iounmap(struct pci_dev *dev, void __iomem * addr) { IO_COND(addr, /* nothing */, iounmap(addr)); --=20 2.41.0