From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5002B59B74 for ; Tue, 23 Jan 2024 09:44:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003093; cv=none; b=k6uN9wEhskg+T1i2Sb/64NIMOlKCenrjAjPhFTRY2N0AzaakBg8ignNAVTEaUQdERghTVnauXcr1MCiYoxdn9fVlAXU73JhAVSL3wDbVKlBsTFeBwHP8Mcd75SjRCVoJj/qa3NHHm6/BB3YZDZFEgeafb/6zlwOXFg8ER1TY57w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003093; c=relaxed/simple; bh=DtFTtecM0dQLV43lG5vbayX8xanOXIfbGBAh1ORJET8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iEL1c3+1ZfTh+fTcvQGCigcpBxls5AyIpCKjkTRE9NpbELCpym6QicitmDMQ4LAx7fwjfr4ZNmE2wVeoei40ndSXYUtvkSXwPEn8zR80E5y3D7BTVBRzSarX49Ai1gw0Q8m+fGhDxljWIi2DFpIrLbXtj7rUhikt6QpAyeZwyfA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Iyb+DpsG; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Iyb+DpsG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003090; 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=OpZRO/hie2DZUp2YXXFB6DWr6YyvZQ+0hRYwdS7yjHA=; b=Iyb+DpsGzKQv61nv0gX4Vr6vD8bOgiABq2efLBtPzoZd4A9jPsHwHvvGoBVhBEo/v0xGt4 FMwEfBmwNeHqmEzkCTPjzxtY9Ny7pBiIHADFOOAG2RkkIAms8zjjDUWHGM/SDTscGo+GF0 nM6dmbmZarbfSB+qp8rlnCD4CzFYU8k= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-226--XuDlOA8NDSQdCSb46XjWA-1; Tue, 23 Jan 2024 04:44:47 -0500 X-MC-Unique: -XuDlOA8NDSQdCSb46XjWA-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-42a335c8fcfso8358511cf.0 for ; Tue, 23 Jan 2024 01:44:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003087; x=1706607887; 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=OpZRO/hie2DZUp2YXXFB6DWr6YyvZQ+0hRYwdS7yjHA=; b=PRCkHgZHy6K4IzvjBNN5FBKxWne7NQhmOl/ylxC9cMWXADm/3zuG7CjKtdgVW3/b4h RuHeRZujBNW6K5uiJnGAoWmt4oxvMJfqcOfCJF9lSv44Rug6hQxgXfg4Jm8ctl6AB3OM c5bXcdDvNTmYxwhi8OIcP3Dkd7Eu63UKuzSGAZ2RsPi8/8CsEQ9euacZE45nxLXChXkL 7p+LGp3rlVjjFC2n1Lup0IOf6kwl9M+jtrqHqhgIi90fnZWOSRE4HkNtUMcJPLRFxQ1u S8gB7F8fyNrMvzA28rc65aLTmtXeKCWwwnFUFZAOfVSdMQ9qutL2BucfjZlh8505hYbW Tifg== X-Gm-Message-State: AOJu0Yx2IE2NS51jpb3tu5twCw9HOjgylwwKFYC+PJsOn752v9eOPX0S L4R96c/Zs82LmGwevstUM3pqGBsrwi00nFvYR8FSpeW8TwGFgDpLws3aCcSbSHL4hRcQVlJ4VrY 52Ce8hdb4GKPsLqW+ZLQpMHKMwbVDbPgCxyGYh9McQTYczDmUSTbh3IYSJC3dZA== X-Received: by 2002:a05:6214:21c5:b0:686:9443:87b1 with SMTP id d5-20020a05621421c500b00686944387b1mr4376855qvh.2.1706003087158; Tue, 23 Jan 2024 01:44:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IE1SFf08MgexIBddX9oF8uSmYRaM8hwsuStyxuECGlCd9+ZZiXl7daBL1Z/0ofsLOoLxLG1rw== X-Received: by 2002:a05:6214:21c5:b0:686:9443:87b1 with SMTP id d5-20020a05621421c500b00686944387b1mr4376831qvh.2.1706003086677; Tue, 23 Jan 2024 01:44:46 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:44:46 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 01/10] PCI: add new set of devres functions Date: Tue, 23 Jan 2024 10:42:58 +0100 Message-ID: <20240123094317.15958-2-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The PCI devres API is not extensible to partial BAR mappings and has bug-provoking features. Improve that by providing better alternatives. When the original PCI devres API was implemented, priority was given to the creation of a set of "plural functions" such as pcim_request_regions(). These functions have bit masks as parameters to specify which BARs shall get mapped. Most users, however, only use those to map 1-3 BARs. A complete set of "singular functions" does not exist. As functions mapping / requesting multiple BARs at once have (almost) no mechanism in C to return the resources to the caller of the plural function, the PCI devres API utilizes the iomap-table administrated by the function pcim_iomap_table(). The entire PCI devres API was strongly tied to that table which only allows for mapping whole, complete BARs, as the BAR's index is used as table index. Consequently, it's not possible to, e.g., have a pcim_iomap_range() function with that mechanism. An additional problem is hat the PCI devres API has been ipmlemented in a sort of "hybrid-mode": Some unmanaged functions have managed counterparts (e.g.: pci_iomap() <-> pcim_iomap()), making their managed nature obvious to the programmer. However, the region-request functions in pci.c, prefixed with pci_, behave either managed or unmanaged, depending on whether pci_enable_device() or pcim_enable_device() has been called in advance. This hybrid API is confusing and should be more cleanly separated by providing always-managed functions prefixed with pcim_. Thus, the existing PCI devres API is not desirable because: a) The vast majority of the users of the plural functions only ever sets a single bit in the bit mask, consequently making them singular functions anyways. b) There is no mechanism to request / iomap only part of a BAR. c) The iomap-table mechanism is over-engineered and complicated. Even worse, some users index over the table administration function directly: void __iomem *mapping =3D pcim_iomap_table(pdev)[my_index]; This can not perform bounds checks; an invalid index won't cause return of -EINVAL or even NULL, resulting in undefined behavior. d) region-request functions being sometimes managed and sometimes not is bug-provoking. Implement a set of singular pcim_ functions that use devres directly and bypass the legacy iomap table mechanism. Add devres.c to driver-api documentation. Signed-off-by: Philipp Stanner --- Documentation/driver-api/pci/pci.rst | 3 + drivers/pci/devres.c | 473 ++++++++++++++++++++++++++- include/linux/pci.h | 11 + 3 files changed, 482 insertions(+), 5 deletions(-) diff --git a/Documentation/driver-api/pci/pci.rst b/Documentation/driver-ap= i/pci/pci.rst index 4843cfad4f60..92b11775344e 100644 --- a/Documentation/driver-api/pci/pci.rst +++ b/Documentation/driver-api/pci/pci.rst @@ -4,6 +4,9 @@ PCI Support Library .. kernel-doc:: drivers/pci/pci.c :export: =20 +.. kernel-doc:: drivers/pci/devres.c + :export: + .. kernel-doc:: drivers/pci/pci-driver.c :export: =20 diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 4bd1e125bca1..91252856aa66 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -8,10 +8,215 @@ */ #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS =20 +/* + * Legacy struct storing addresses to whole mapped BARs. + */ struct pcim_iomap_devres { void __iomem *table[PCIM_IOMAP_MAX]; }; =20 +enum pcim_addr_devres_type { + /* Default initializer. */ + PCIM_ADDR_DEVRES_TYPE_INVALID, + + /* A region spanning an entire BAR. */ + PCIM_ADDR_DEVRES_TYPE_REGION, + + /* A region spanning an entire BAR, and a mapping for that whole BAR. */ + PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING, + + /* + * A mapping within a BAR, either spanning the whole BAR or just a range. + * Without a requested region. + */ + PCIM_ADDR_DEVRES_TYPE_MAPPING, + + /* A ranged region within a BAR, with a mapping spanning that range. */ + PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING +}; + +/* + * This struct envelopes IO or MEM addresses, that means mappings and regi= on + * requests, because those are very frequently requested and released toge= ther. + */ +struct pcim_addr_devres { + enum pcim_addr_devres_type type; + void __iomem *baseaddr; + unsigned long offset; + unsigned long len; + short bar; +}; + +static inline void pcim_addr_devres_clear(struct pcim_addr_devres *res) +{ + memset(res, 0, sizeof(*res)); + res->bar =3D -1; +} + +/* + * The following functions, __pcim_*_region*, exist as counterparts to the + * versions from pci.c - which, unfortunately, can be in "hybrid mode", i.= e., + * sometimes managed, sometimes not. + * + * To separate the APIs cleanly, we define our own, simplified versions he= re. + */ + +/** + * __pcim_request_region_range - Request a ranged region + * @pdev: PCI device the region belongs to + * @bar: The BAR the region is within + * @offset: offset from the BAR's start address + * @maxlen: length in bytes, beginning at @offset + * @name: name associated with the request + * @exclusive: whether the mapping shall be exclusively for kernelspace + * + * Returns: 0 on success, a negative error code on failure. + * + * Request a ranged region within a device's PCI BAR. This function perfor= ms + * sanity checks on the input. + */ +static int __pcim_request_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long maxlen, + const char *name, int exclusive) +{ + resource_size_t start =3D pci_resource_start(pdev, bar); + resource_size_t len =3D pci_resource_len(pdev, bar); + unsigned long flags =3D pci_resource_flags(pdev, bar); + + if (start =3D=3D 0 || len =3D=3D 0) /* That's an unused BAR. */ + return 0; + if (len <=3D offset) + return -EINVAL; + + start +=3D offset; + len -=3D offset; + + if (len > maxlen && maxlen !=3D 0) + len =3D maxlen; + + if (flags & IORESOURCE_IO) { + if (!request_region(start, len, name)) + return -EBUSY; + } else if (flags & IORESOURCE_MEM) { + if (!__request_mem_region(start, len, name, exclusive)) + return -EBUSY; + } else { + /* That's not a device we can request anything on. */ + return -ENODEV; + } + + return 0; +} + +static void __pcim_release_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long maxlen) +{ + resource_size_t start =3D pci_resource_start(pdev, bar); + resource_size_t len =3D pci_resource_len(pdev, bar); + unsigned long flags =3D pci_resource_flags(pdev, bar); + + if (len <=3D offset || start =3D=3D 0) + return; + + if (len =3D=3D 0 || maxlen =3D=3D 0) /* This an unused BAR. Do nothing. */ + return; + + start +=3D offset; + len -=3D offset; + + if (len > maxlen) + len =3D maxlen; + + if (flags & IORESOURCE_IO) + release_region(start, len); + else if (flags & IORESOURCE_MEM) + release_mem_region(start, len); +} + +static int __pcim_request_region(struct pci_dev *pdev, int bar, + const char *name, int exclusive) +{ + unsigned long offset =3D 0; + unsigned long len =3D pci_resource_len(pdev, bar); + + return __pcim_request_region_range(pdev, bar, offset, len, name, exclusiv= e); +} + +static void __pcim_release_region(struct pci_dev *pdev, int bar) +{ + unsigned long offset =3D 0; + unsigned long len =3D pci_resource_len(pdev, bar); + + __pcim_release_region_range(pdev, bar, offset, len); +} + +static void pcim_addr_resource_release(struct device *dev, void *resource_= raw) +{ + struct pci_dev *pdev =3D to_pci_dev(dev); + struct pcim_addr_devres *res =3D resource_raw; + + switch (res->type) { + case PCIM_ADDR_DEVRES_TYPE_REGION: + __pcim_release_region(pdev, res->bar); + break; + case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: + pci_iounmap(pdev, res->baseaddr); + __pcim_release_region(pdev, res->bar); + break; + case PCIM_ADDR_DEVRES_TYPE_MAPPING: + pci_iounmap(pdev, res->baseaddr); + break; + case PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING: + pci_iounmap(pdev, res->baseaddr); + __pcim_release_region_range(pdev, res->bar, res->offset, res->len); + break; + default: + break; + } +} + +static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pde= v) +{ + struct pcim_addr_devres *res; + + res =3D devres_alloc_node(pcim_addr_resource_release, sizeof(*res), + GFP_KERNEL, dev_to_node(&pdev->dev)); + if (res) + pcim_addr_devres_clear(res); + return res; +} + +/* Just for consistency and readability. */ +static inline void pcim_addr_devres_free(struct pcim_addr_devres *res) +{ + devres_free(res); +} + +/* + * Used by devres to identify a pcim_addr_devres. + */ +static int pcim_addr_resources_match(struct device *dev, void *a_raw, void= *b_raw) +{ + struct pcim_addr_devres *a, *b; + + a =3D a_raw; + b =3D b_raw; + + if (a->type !=3D b->type) + return 0; + + switch (a->type) { + case PCIM_ADDR_DEVRES_TYPE_REGION: + case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: + return a->bar =3D=3D b->bar; + case PCIM_ADDR_DEVRES_TYPE_MAPPING: + return a->baseaddr =3D=3D b->baseaddr; + case PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING: + return a->bar =3D=3D b->bar && a->offset =3D=3D b->offset && a->len =3D= =3D b->len; + default: + return 0; + } +} =20 static void devm_pci_unmap_iospace(struct device *dev, void *ptr) { @@ -92,8 +297,8 @@ EXPORT_SYMBOL(devm_pci_remap_cfgspace); * * 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:: + * Returns a pointer to the remapped memory or an IOMEM_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); @@ -341,15 +546,80 @@ void pcim_iounmap(struct pci_dev *pdev, void __iomem = *addr) tbl[i] =3D NULL; return; } - WARN_ON(1); } EXPORT_SYMBOL(pcim_iounmap); =20 +/** + * pcim_iomap_region - Request and iomap a PCI BAR + * @pdev: PCI device to map IO resources for + * @bar: Index of a BAR to map + * @name: Name associated with the request + * + * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. + * + * Mapping and region will get automatically released on driver detach. If + * desired, release manually only with pcim_iounmap_region(). + */ +void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar, const char = *name) +{ + int ret; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + res->type =3D PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res->bar =3D bar; + + ret =3D __pcim_request_region(pdev, bar, name, 0); + if (ret !=3D 0) + goto err_region; + + res->baseaddr =3D pci_iomap(pdev, bar, 0); + if (!res->baseaddr) { + ret =3D -EINVAL; + goto err_iomap; + } + + devres_add(&pdev->dev, res); + return res->baseaddr; + +err_iomap: + __pcim_release_region(pdev, bar); +err_region: + pcim_addr_devres_free(res); + + return IOMEM_ERR_PTR(ret); +} +EXPORT_SYMBOL(pcim_iomap_region); + +/** + * pcim_iounmap_region - Unmap and release a PCI BAR + * @pdev: PCI device to operate on + * @bar: Index of BAR to unmap and release + * + * Unmap a BAR and release its region manually. Only pass BARs that were + * previously mapped by pcim_iomap_region(). + */ +void pcim_iounmap_region(struct pci_dev *pdev, int bar) +{ + struct pcim_addr_devres res_searched; + + pcim_addr_devres_clear(&res_searched); + res_searched.type =3D PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res_searched.bar =3D bar; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} +EXPORT_SYMBOL(pcim_iounmap_region); + /** * 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 + * @name: Name associated with the requests * * Request and iomap regions specified by @mask. */ @@ -402,7 +672,7 @@ 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 + * @name: Name associated with the requests * * Request all PCI BARs and iomap regions specified by @mask. */ @@ -448,3 +718,196 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int m= ask) } } EXPORT_SYMBOL(pcim_iounmap_regions); + +static int _pcim_request_region(struct pci_dev *pdev, int bar, const char = *name, + int request_flags) +{ + int ret; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return -ENOMEM; + res->type =3D PCIM_ADDR_DEVRES_TYPE_REGION; + res->bar =3D bar; + + ret =3D __pcim_request_region(pdev, bar, name, request_flags); + if (ret !=3D 0) { + pcim_addr_devres_free(res); + return ret; + } + + devres_add(&pdev->dev, res); + return 0; +} + +/** + * pcim_request_region - Request a PCI BAR + * @pdev: PCI device to requestion region for + * @bar: Index of BAR to request + * @name: Name associated with the request + * + * Returns: 0 on success, a negative error code on failure. + * + * Request region specified by @bar. + * + * The region will automatically be released on driver detach. If desired, + * release manually only with pcim_release_region(). + */ +int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) +{ + return _pcim_request_region(pdev, bar, name, 0); +} +EXPORT_SYMBOL(pcim_request_region); + +/** + * pcim_release_region - Release a PCI BAR + * @pdev: PCI device to operate on + * @bar: Index of BAR to release + * + * Release a region manually that was previously requested by + * pcim_request_region(). + */ +void pcim_release_region(struct pci_dev *pdev, int bar) +{ + struct pcim_addr_devres res_searched; + + pcim_addr_devres_clear(&res_searched); + res_searched.type =3D PCIM_ADDR_DEVRES_TYPE_REGION; + res_searched.bar =3D bar; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} +EXPORT_SYMBOL(pcim_release_region); + +/** + * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR + * @pdev: PCI device to map IO resources for + * @bar: Index of the BAR + * @offset: Offset from the begin of the BAR + * @len: Length in bytes for the mapping + * + * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. + * + * Creates a new IO-Mapping within the specified @bar, ranging from @offse= t to + * @offset + @len. + * + * The mapping will automatically get unmapped on driver detach. If desire= d, + * release manually only with pcim_iounmap(). + */ +void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len) +{ + void __iomem *mapping; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + mapping =3D pci_iomap_range(pdev, bar, offset, len); + if (!mapping) { + pcim_addr_devres_free(res); + return IOMEM_ERR_PTR(-EINVAL); + } + + res->type =3D PCIM_ADDR_DEVRES_TYPE_MAPPING; + res->baseaddr =3D mapping; + + /* + * Ranged mappings don't get added to the legacy-table, since the table + * only ever keeps track of whole BARs. + */ + + devres_add(&pdev->dev, res); + return mapping; +} +EXPORT_SYMBOL(pcim_iomap_range); + +/** + * pcim_iomap_region_range - Request and map a range within a PCI BAR + * @pdev: PCI device to map IO resources for + * @bar: Index of BAR to request within + * @offset: Offset from the begin of the BAR + * @len: Length in bytes for the mapping + * @name: Name associated with the request + * + * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. + * + * Request region with a range specified by @offset and @len within @bar a= nd + * iomap it. + * + * The region will automatically be released and the mapping be unmapped on + * driver detach. If desired, release manually only with + * pcim_iounmap_region_range(). + * + * You probably should only use this function if you explicitly do not wan= t to + * request the entire BAR. For most use-cases, combining pcim_request_regi= on() + * and pcim_iomap_range() should be sufficient. + */ +void __iomem *pcim_iomap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len, const char *name) +{ + int ret; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + res->type =3D PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING; + res->bar =3D bar; + res->offset =3D offset; + res->len =3D len; + + ret =3D __pcim_request_region_range(pdev, bar, offset, len, name, 0); + if (ret !=3D 0) + goto err_region; + + res->baseaddr =3D pci_iomap_range(pdev, bar, offset, len); + if (!res->baseaddr) { + ret =3D -EINVAL; + goto err_iomap; + } + + devres_add(&pdev->dev, res); + return res->baseaddr; + +err_iomap: + __pcim_release_region_range(pdev, bar, offset, len); +err_region: + pcim_addr_devres_free(res); + + return IOMEM_ERR_PTR(ret); +} +EXPORT_SYMBOL(pcim_iomap_region_range); + +/** + * pcim_iounmap_region_range - Unmap and release a range within a PCI BAR + * @pdev: PCI device to operate on + * @bar: Index of BAR containing the range + * @offset: Offset from the begin of the BAR + * @len: Length in bytes for the mapping + * + * Unmaps and releases a memory area within the specified PCI BAR. + * + * This function may not be used to free only part of a range. Only use th= is + * function with the exact parameters you previously used successfully in + * pcim_iomap_region_range(). + */ +void pcim_iounmap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len) +{ + struct pcim_addr_devres res_searched; + pcim_addr_devres_clear(&res_searched); + + res_searched.type =3D PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING; + res_searched.bar =3D bar; + res_searched.offset =3D offset; + res_searched.len =3D len; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} +EXPORT_SYMBOL(pcim_iounmap_region_range); diff --git a/include/linux/pci.h b/include/linux/pci.h index 58a4c976c39b..1b45a4888703 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2314,10 +2314,21 @@ static inline void pci_fixup_device(enum pci_fixup_= pass pass, void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxl= en); void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr); void __iomem * const *pcim_iomap_table(struct pci_dev *pdev); +void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar, const char = *name); +void pcim_iounmap_region(struct pci_dev *pdev, int bar); int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name); int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name); void pcim_iounmap_regions(struct pci_dev *pdev, int mask); +int pcim_request_region(struct pci_dev *pdev, int bar, const char *res_nam= e); +void pcim_release_region(struct pci_dev *pdev, int bar); +void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len); +void __iomem *pcim_iomap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len, + const char *res_name); +void pcim_iounmap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len); =20 extern int pci_pci_problems; #define PCIPCI_FAIL 1 /* No PCI PCI DMA */ --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BD6759B6C for ; Tue, 23 Jan 2024 09:44:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003095; cv=none; b=bEiWTHIUaysqmD7lbQnJRMKx6MxrWkhtxpcaJHzbPFWOJWeDWlqQITpYvii2FuNgbhlX/hJdpCEMtjfREVlF+tl15yPX+XHBesaDGXuU9TMTY1Up+3Xp8OPTRlxfUbxOnJQe44hG1zkDpT+bTHczpuVlk5vYXlrO5I19R1waN2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003095; c=relaxed/simple; bh=DpufObqHMp2GH2jiS2xo13V7ghDTJR3FuOoF4N0d8p4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C3yPl1muH+RVkRJfMsNs/f+XXhrenX8NNgdkbDEz9L9tytKJCQNDx8MQzWvSyme1QOIAG/oszw9YUW+fg6zenQBLD/tF1jPmwzIn3f0UF+lHLklxN7mPCsdupe/ldDHQUEAEBMeq260eJOgSvNNU9bu2BcgyfbilHsxTjZZRY4g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UOTs9jwk; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UOTs9jwk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003092; 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=DpxKmM3l8Vl7kV3GYXjRni+YGoQ2Eibk4E8EGJ3z2IA=; b=UOTs9jwkyvTTpl1cLjnk92nvioUPSlpPgkaLaGzcOPcs81DcQnmPQ+PWspYwfYaZ+4FxzX Ttl+2cTlf59FlJ1vIikCXd3ljDnOuBeaxJS26A6GRGw6Ztl+877vagdna8pJylGxU2vy69 RrnaS0ojlJI8HWFv3y2cfx8RxGLWAkY= Received: from mail-vs1-f71.google.com (mail-vs1-f71.google.com [209.85.217.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-201-HZV03QwhOT2HOAYp98aeWg-1; Tue, 23 Jan 2024 04:44:50 -0500 X-MC-Unique: HZV03QwhOT2HOAYp98aeWg-1 Received: by mail-vs1-f71.google.com with SMTP id ada2fe7eead31-46a9d50a8b4so216814137.1 for ; Tue, 23 Jan 2024 01:44:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003090; x=1706607890; 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=DpxKmM3l8Vl7kV3GYXjRni+YGoQ2Eibk4E8EGJ3z2IA=; b=O+ldRJFs97+kvV9y1lxUNwEKPBz3+c08ewNvPbIubCOlpihE+HW03sjeLNFKZR9Nw/ aYWZlDaBYwz2i3kVl7AZdGIFFlo6JFIPUFfooGEZrk8Sy8JjCu31Xz0TEKNyzmKhBsd3 8g5JuPmTPuMBDVHO7KZ1DrONY73PyRovKhXTAIqX9T+Y1jddIba80Z8RwuuZyLR+PJgx 3XGJ6P67IV1OwHt6v8uSWaLoui+uWhGVfZQOH45NQugjC+Xpnxvxs5dj88Zd5GaRtELl 6sMkIHeEZjcLLhlGGuqFPz0vCPSQ4tSWY1MkEMXJ/+2UWms0Jgym6HeaAk3RJcxOMKAt Ijfw== X-Gm-Message-State: AOJu0YydMqop4x6T3pSnl0JCxIRJJb/SCpsDWH+06Zw3R4s0eShu02ar od80IHZHxxc98GbMAITVPL3V6EH4HFAHJqtGlbjXo4iW+hFNWyd2CBJcWwlSAud7m5f/zfeQ1vG p9fp5q4C0ON70YcUK85x7SFbVa6s9PvcWvS2SaKx6c1jO3maFzjDUMu8gn7QliA== X-Received: by 2002:a05:6122:c94:b0:4b6:c04b:3a1e with SMTP id ba20-20020a0561220c9400b004b6c04b3a1emr5163538vkb.0.1706003089599; Tue, 23 Jan 2024 01:44:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IHT8H18rgKDaN7+JY0OqB/sHz+MdkaXZBz3BqTmjmuLzasT1lm67GOGTxaekMz9Tgf/+wSe9g== X-Received: by 2002:a05:6122:c94:b0:4b6:c04b:3a1e with SMTP id ba20-20020a0561220c9400b004b6c04b3a1emr5163513vkb.0.1706003089033; Tue, 23 Jan 2024 01:44:49 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:44:48 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 02/10] PCI: deprecate iomap-table functions Date: Tue, 23 Jan 2024 10:42:59 +0100 Message-ID: <20240123094317.15958-3-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The old plural devres functions tie the PCI devres API to the iomap-table mechanism which unfortunately is not extensible. As the purlal functions are almost never used with more than one bit set in their bit-mask, deprecating them and encouraging users to use the new singular functions instead is reasonable. Furthermore, to make the implementation more consistent and extensible, the plural functions should use the singular functions. Add new wrapper to request / release all BARs. Make the plural functions call into the singular functions. Mark the plural functions as deprecated. Remove as much of the iomap-table-mechanism as possible. Add comments describing the path towards a cleaned-up API. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 374 +++++++++++++++++++++++++++++++++---------- drivers/pci/pci.c | 20 +++ drivers/pci/pci.h | 5 + include/linux/pci.h | 2 + 4 files changed, 318 insertions(+), 83 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 91252856aa66..1279aac9b63e 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -4,15 +4,43 @@ #include "pci.h" =20 /* - * PCI iomap devres + * On the state of PCI's devres implementation: + * + * The older devres API for PCI has two significant problems: + * + * 1. It is very strongly tied to the statically allocated mapping table in + * struct pcim_iomap_devres below. This is mostly solved in the sense o= f the + * pcim_ functions in this file providing things like ranged mapping by + * bypassing this table, wheras the functions that were present in the = old + * API still enter the mapping addresses into the table for users of th= e old + * API. + * 2. The region-request-functions in pci.c do become managed IF the devic= e has + * been enabled with pcim_enable_device() instead of pci_enable_device(= ). + * This resulted in the API becoming inconsistent: Some functions have = an + * obviously managed counter-part (e.g., pci_iomap() <-> pcim_iomap()), + * whereas some don't and are never managed, while others don't and are + * _sometimes_ managed (e.g. pci_request_region()). + * Consequently, in the new API, region requests performed by the pcim_ + * functions are automatically cleaned up through the devres callback + * pcim_addr_resource_release(), while requests performed by + * pcim_enable_device() + pci_*region*() are automatically cleaned up + * through the for-loop in pcim_release(). + * + * TODO 1: + * Remove the legacy table entirely once all calls to pcim_iomap_table() in + * the kernel have been removed. + * + * TODO 2: + * Port everyone calling pcim_enable_device() + pci_*region*() to using the + * pcim_ functions. Then, remove all devres functionality from pci_*region= *() + * functions and remove the associated cleanups described above in point #= 2. */ -#define PCIM_IOMAP_MAX PCI_STD_NUM_BARS =20 /* * Legacy struct storing addresses to whole mapped BARs. */ struct pcim_iomap_devres { - void __iomem *table[PCIM_IOMAP_MAX]; + void __iomem *table[PCI_STD_NUM_BARS]; }; =20 enum pcim_addr_devres_type { @@ -373,6 +401,16 @@ static void pcim_release(struct device *gendev, void *= res) struct pci_devres *this =3D res; int i; =20 + /* + * This is legacy code. + * All regions requested by a pcim_ function do get released through + * pcim_addr_resource_release(). Thanks to the hybrid nature of the pci_ + * region-request functions, this for-loop has to release the regions + * if they have been requested by such a function. + * + * TODO: Remove this once all users of pcim_enable_device() PLUS + * pci-region-request-functions have been ported to pcim_ functions. + */ for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) if (this->region_mask & (1 << i)) pci_release_region(dev, i); @@ -461,19 +499,21 @@ EXPORT_SYMBOL(pcim_pin_device); =20 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]); + /* + * Do nothing. This is legacy code. + * + * Cleanup of the mappings is now done directly through the callbacks + * registered when creating them. + */ } =20 /** - * pcim_iomap_table - access iomap allocation table + * pcim_iomap_table - access iomap allocation table (DEPRECATED) * @pdev: PCI device to access iomap table for * + * Returns: + * Const pointer to array of __iomem pointers on success NULL on failure. + * * 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 @@ -482,6 +522,11 @@ static void pcim_iomap_release(struct device *gendev, = void *res) * This function might sleep when the table is first allocated but can * be safely called without context and guaranteed to succeed once * allocated. + * + * This function is DEPRECATED. Do not use it in new code. + * Instead, obtain a mapping's address directly from one of the pcim_* map= ping + * functions. For example: + * void __iomem *mappy =3D pcim_iomap(pdev, barnr, length); */ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) { @@ -500,27 +545,114 @@ void __iomem * const *pcim_iomap_table(struct pci_de= v *pdev) } EXPORT_SYMBOL(pcim_iomap_table); =20 +/* + * Fill the legacy mapping-table, so that drivers using the old API + * can still get a BAR's mapping address through pcim_iomap_table(). + */ +static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev, + void __iomem *mapping, short bar) +{ + void __iomem **legacy_iomap_table; + + if (bar >=3D PCI_STD_NUM_BARS) + return -EINVAL; + + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return -ENOMEM; + + /* The legacy mechanism doesn't allow for duplicate mappings. */ + WARN_ON(legacy_iomap_table[bar]); + + legacy_iomap_table[bar] =3D mapping; + + return 0; +} + +/* + * Removes a mapping. The table only contains whole-bar-mappings, so this = will + * never interfere with ranged mappings. + */ +static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev, + void __iomem *addr) +{ + short bar; + void __iomem **legacy_iomap_table; + + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + if (legacy_iomap_table[bar] =3D=3D addr) { + legacy_iomap_table[bar] =3D NULL; + return; + } + } +} + +/* + * The same as pcim_remove_mapping_from_legacy_table(), but identifies the + * mapping by its BAR index. + */ +static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, short = bar) +{ + void __iomem **legacy_iomap_table; + + if (bar >=3D PCI_STD_NUM_BARS) + return; + + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + legacy_iomap_table[bar] =3D NULL; +} + /** * 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. + * Returns: __iomem pointer on success, NULL on failure. + * + * Managed pci_iomap(). Map is automatically unmapped on driver detach. If + * desired, unmap manually only with pcim_iounmap(). + * + * This SHOULD only be used once per BAR. + * + * NOTE: + * Contrary to the other pcim_* functions, this function does not return an + * IOMEM_ERR_PTR() on failure, but a simple NULL. This is done for backwar= ds + * compatibility. */ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxl= en) { - void __iomem **tbl; - - BUG_ON(bar >=3D PCIM_IOMAP_MAX); + void __iomem *mapping; + struct pcim_addr_devres *res; =20 - tbl =3D (void __iomem **)pcim_iomap_table(pdev); - if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ + res =3D pcim_addr_devres_alloc(pdev); + if (!res) return NULL; + res->type =3D PCIM_ADDR_DEVRES_TYPE_MAPPING; + + mapping =3D pci_iomap(pdev, bar, maxlen); + if (!mapping) + goto err_iomap; + res->baseaddr =3D mapping; + + if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) !=3D 0) + goto err_table; =20 - tbl[bar] =3D pci_iomap(pdev, bar, maxlen); - return tbl[bar]; + devres_add(&pdev->dev, res); + return mapping; + +err_table: + pci_iounmap(pdev, mapping); +err_iomap: + pcim_addr_devres_free(res); + return NULL; } EXPORT_SYMBOL(pcim_iomap); =20 @@ -529,23 +661,24 @@ EXPORT_SYMBOL(pcim_iomap); * @pdev: PCI device to iounmap for * @addr: Address to unmap * - * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap(). + * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap() or + * pcim_iomap_range(). */ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) { - void __iomem **tbl; - int i; + struct pcim_addr_devres res_searched; =20 - pci_iounmap(pdev, addr); + pcim_addr_devres_clear(&res_searched); + res_searched.type =3D PCIM_ADDR_DEVRES_TYPE_MAPPING; + res_searched.baseaddr =3D addr; =20 - tbl =3D (void __iomem **)pcim_iomap_table(pdev); - BUG_ON(!tbl); + if (devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched) !=3D 0) { + /* Doesn't exist. User passed nonsense. */ + return; + } =20 - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) - if (tbl[i] =3D=3D addr) { - tbl[i] =3D NULL; - return; - } + pcim_remove_mapping_from_legacy_table(pdev, addr); } EXPORT_SYMBOL(pcim_iounmap); =20 @@ -615,106 +748,181 @@ void pcim_iounmap_region(struct pci_dev *pdev, int = bar) } EXPORT_SYMBOL(pcim_iounmap_region); =20 +static inline bool mask_contains_bar(int mask, int bar) +{ + return mask & BIT(bar); +} + /** - * pcim_iomap_regions - Request and iomap PCI BARs + * pcim_iomap_regions - Request and iomap PCI BARs (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to request and iomap * @name: Name associated with the requests * + * Returns: 0 on success, negative error code on failure. + * * Request and iomap regions specified by @mask. + * + * This function is DEPRECATED. Don't use it in new code. + * Use pcim_iomap_region() instead. */ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) { - void __iomem * const *iomap; - int i, rc; + int ret; + short bar; + void __iomem *mapping; =20 - iomap =3D pcim_iomap_table(pdev); - if (!iomap) - return -ENOMEM; + for (bar =3D 0; bar < DEVICE_COUNT_RESOURCE; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; =20 - for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) { - unsigned long len; + mapping =3D pcim_iomap_region(pdev, bar, name); + if (IS_ERR(mapping)) { + ret =3D PTR_ERR(mapping); + goto err; + } + ret =3D pcim_add_mapping_to_legacy_table(pdev, mapping, bar); + if (ret !=3D 0) + goto err; + } =20 - if (!(mask & (1 << i))) - continue; + return 0; =20 - rc =3D -EINVAL; - len =3D pci_resource_len(pdev, i); - if (!len) - goto err_inval; +err: + while (--bar >=3D 0) { + pcim_iounmap_region(pdev, bar); + pcim_remove_bar_from_legacy_table(pdev, bar); + } =20 - rc =3D pci_request_region(pdev, i, name); - if (rc) - goto err_inval; + return ret; +} +EXPORT_SYMBOL(pcim_iomap_regions); =20 - rc =3D -ENOMEM; - if (!pcim_iomap(pdev, i, 0)) - goto err_region; +/** + * pcim_release_all_regions - Release all regions of a PCI-device + * @pdev: the PCI device + * + * Will release all regions previously requested through pcim_request_regi= on() + * or pcim_request_all_regions(). + * + * Can be called from any context, i.e., not necessarily as a counterpart = to + * pcim_request_all_regions(). + */ +void pcim_release_all_regions(struct pci_dev *pdev) +{ + short bar; + + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) + pcim_release_region(pdev, bar); +} +EXPORT_SYMBOL(pcim_release_all_regions); + +/** + * pcim_request_all_regions - Request all regions + * @pdev: PCI device to map IO resources for + * @name: name associated with the request + * + * Returns: 0 on success, negative error code on failure. + * + * Requested regions will automatically be released at driver detach. If d= esired, + * release individual regions with pcim_release_region() or all of them at= once + * with pcim_release_all_regions(). + */ +int pcim_request_all_regions(struct pci_dev *pdev, const char *name) +{ + int ret; + short bar; + + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + ret =3D pcim_request_region(pdev, bar, name); + if (ret !=3D 0) + goto err; } =20 return 0; =20 - 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); - } +err: + pcim_release_all_regions(pdev); =20 - return rc; + return ret; } -EXPORT_SYMBOL(pcim_iomap_regions); +EXPORT_SYMBOL(pcim_request_all_regions); =20 /** - * pcim_iomap_regions_request_all - Request all BARs and iomap specified o= nes + * pcim_iomap_regions_request_all - Request all BARs and iomap specified o= nes (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to iomap * @name: Name associated with the requests * + * Returns: 0 on success, negative error code on failure. + * * Request all PCI BARs and iomap regions specified by @mask. + * + * To release these resources manually, call pcim_release_region() for the + * regions and pcim_iounmap() for the mappings. + * + * This function is DEPRECATED. Don't use it in new code. + * Use pcim_request_all_regions() + pcim_iomap*() instead. */ 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; + short bar; + int ret; + void __iomem **legacy_iomap_table; + + ret =3D pcim_request_all_regions(pdev, name); + if (ret !=3D 0) + return ret; =20 - rc =3D pci_request_selected_regions(pdev, request_mask, name); - if (rc) - return rc; + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; + if (!pcim_iomap(pdev, bar, 0)) + goto err; + } =20 - rc =3D pcim_iomap_regions(pdev, mask, name); - if (rc) - pci_release_selected_regions(pdev, request_mask); - return rc; + return 0; + +err: + /* + * Here it gets tricky: pcim_iomap() above has most likely + * failed because it got an OOM when trying to create the + * legacy-table. + * We check here if that has happened. If not, pcim_iomap() + * must have failed because of EINVAL. + */ + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + ret =3D legacy_iomap_table ? -EINVAL : -ENOMEM; + + while (--bar >=3D 0) + pcim_iounmap(pdev, legacy_iomap_table[bar]); + + pcim_release_all_regions(pdev); + + return ret; } EXPORT_SYMBOL(pcim_iomap_regions_request_all); =20 /** - * pcim_iounmap_regions - Unmap and release PCI BARs + * pcim_iounmap_regions - Unmap and release PCI BARs (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to unmap and release * * Unmap and release regions specified by @mask. + * + * This function is DEPRECATED. Don't use it in new code. */ 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; + short bar; =20 - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) { - if (!(mask & (1 << i))) + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) continue; =20 - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); + pcim_iounmap_region(pdev, bar); + pcim_remove_bar_from_legacy_table(pdev, bar); } } EXPORT_SYMBOL(pcim_iounmap_regions); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index bf5f9c3a1908..2899df77a285 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3884,6 +3884,16 @@ void pci_release_region(struct pci_dev *pdev, int ba= r) release_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)); =20 + /* + * This devres utility makes this function sometimes managed + * (when pcim_enable_device() has been called before). + * This is bad because it conflicts with the pcim_ functions being + * exclusively responsible for managed pci. Its "sometimes yes, sometimes + * no" nature can cause bugs. + * + * TODO: Remove this once all users that use pcim_enable_device() PLUS + * a region request function have been ported to using pcim_ functions. + */ dr =3D find_pci_dr(pdev); if (dr) dr->region_mask &=3D ~(1 << bar); @@ -3928,6 +3938,16 @@ static int __pci_request_region(struct pci_dev *pdev= , int bar, goto err_out; } =20 + /* + * This devres utility makes this function sometimes managed + * (when pcim_enable_device() has been called before). + * This is bad because it conflicts with the pcim_ functions being + * exclusively responsible for managed pci. Its "sometimes yes, sometimes + * no" nature can cause bugs. + * + * TODO: Remove this once all users that use pcim_enable_device() PLUS + * a region request function have been ported to using pcim_ functions. + */ dr =3D find_pci_dr(pdev); if (dr) dr->region_mask |=3D 1 << bar; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index de6d61d1e07f..4d4bcc2d850f 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -814,6 +814,11 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; + + /* + * TODO: remove the region_mask once everyone calling + * pcim_enable_device() + pci_*region*() is ported to pcim_ functions. + */ u32 region_mask; }; =20 diff --git a/include/linux/pci.h b/include/linux/pci.h index 1b45a4888703..3dcd8a5e10b5 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2322,6 +2322,8 @@ int pcim_iomap_regions_request_all(struct pci_dev *pd= ev, int mask, void pcim_iounmap_regions(struct pci_dev *pdev, int mask); int pcim_request_region(struct pci_dev *pdev, int bar, const char *res_nam= e); void pcim_release_region(struct pci_dev *pdev, int bar); +void pcim_release_all_regions(struct pci_dev *pdev); +int pcim_request_all_regions(struct pci_dev *pdev, const char *name); void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, unsigned long offset, unsigned long len); void __iomem *pcim_iomap_region_range(struct pci_dev *pdev, int bar, --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6682E5A11C for ; Tue, 23 Jan 2024 09:44:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003097; cv=none; b=LBs0G2/fg7Qunm/serlBwq5EHSDgk6yM+mJ/0aYnIt3XydXlcHgF2IO6n+w0WoWCy7l6+8UtFEGrT1kA5xUFe2e0qLz8zoCqwkHAe+oMJBkjZvHhaWWQw7FVQDp2rL7oRgYvkISgYJAnnNB/fnEwAcGUmIM/C6hOSX5L/jfmbPw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003097; c=relaxed/simple; bh=rKqKx2xzj55JuQB/eV+Awt0kJSYANwr5bj8CjGzxCrA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lDiwoNUHZItmoyXY+3BvBQ4cT8tRQlgJnYvWq6GKrtNUkVJbio564tNOTPgz0459bKErKesFN2CUjMwYgZZrBY+pW9AY1Pmx+kqWJNW9xPh57fM5IwbUVv0x+Dl2CFtvbxOFWErYyKyWiZDVjf8APXU6B9+/2JKUBG06ew9IatY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZHUuyG0w; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZHUuyG0w" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003094; 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=3EoIsZMzaud9t26sivTeCHJdwdosgvCL2VSONhiwz3c=; b=ZHUuyG0woi3ZmVxl6pijE5GKRGMPSlPTwaX+iaJtfM76UhVR2H9VwJ81XHZK8fLhCh92/g 4AEHvusqwFwyzul68/CXoer0VaNRrAGowa8cgOlLl1s3cTkaEhgbOue0yKNcjpSFpObv2h ujrrr+HYLSzeEnLUtAB9T29Sn74BXyc= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-319-p0ENHYEcM02UUw2Rx8KQzQ-1; Tue, 23 Jan 2024 04:44:52 -0500 X-MC-Unique: p0ENHYEcM02UUw2Rx8KQzQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7831a20711dso100520785a.0 for ; Tue, 23 Jan 2024 01:44:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003092; x=1706607892; 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=3EoIsZMzaud9t26sivTeCHJdwdosgvCL2VSONhiwz3c=; b=eZYUf09VBUcmC+lI38A72wUrBff1yylLhRNwaXljwDVXHY2UUtuwwzSsh2d+3OhbKS MuhwQ11U6OOO142apbptGk5FET+Zfws2RrogdAQu1PWDZ1If9o1o/5Q/CYxtRm7Krnrf lIPh/NygOmI6+4om/d3uKUSrZ6lKsfG/We/aB7symcb05+adS8HZ8MHzvtSLbdWESxB2 KakQPRGlomHLGtJ00JUWNUpv77FYuVdHeWDUmNRuHkoiZ8eFJNPTVADo4MOGrcPflHJo +AXBDVRpqNn5Qtdxz97m52bjBgeuzlfYw5CtkXxvvbMXFwkI1sruGMGo9WmSIMeZcFKT wgBA== X-Gm-Message-State: AOJu0Ywx+EayO9LIHaxMVHVEvH/FoH0dkEc9KhR93GCPmfwuZ2RpfmcH oKgcTkWh3Oo2HJMyKzZamSyC1xWV0XTTLSUm6HN3HoJP3ICmkElMmyzv05nDurEplXa3McSfHKX nuNiUXCqrsB1Cwretpa9Rj7LOVdnC5sznCExiRiMq3ttR/UM0j9TpX1WfMRxDOA== X-Received: by 2002:a05:6214:c6e:b0:686:90f0:2a33 with SMTP id t14-20020a0562140c6e00b0068690f02a33mr5644872qvj.5.1706003091671; Tue, 23 Jan 2024 01:44:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPwBecVC6FieQ1xp9U2Y6NBmb5GNR/o+qtX9gU0WfvrCMFDX+wPNNnUyTl0yQMJj1YP7+7Og== X-Received: by 2002:a05:6214:c6e:b0:686:90f0:2a33 with SMTP id t14-20020a0562140c6e00b0068690f02a33mr5644861qvj.5.1706003091343; Tue, 23 Jan 2024 01:44:51 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:44:51 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 03/10] PCI: warn users about complicated devres nature Date: Tue, 23 Jan 2024 10:43:00 +0100 Message-ID: <20240123094317.15958-4-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The PCI region-request functions become managed functions when pcim_enable_device() has been called previously instead of pci_enable_device(). This has already caused bugs by confusing users, who came to believe that all pci functions, such as pci_iomap_range(), suddenly are managed that way. This is not the case. Add comments to the relevant functions' docstrings that warn users about this behavior. Signed-off-by: Philipp Stanner --- drivers/pci/iomap.c | 18 ++++++++++++++ drivers/pci/pci.c | 60 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/drivers/pci/iomap.c b/drivers/pci/iomap.c index b7faf22ec8f5..0b7fcc2f7a49 100644 --- a/drivers/pci/iomap.c +++ b/drivers/pci/iomap.c @@ -23,6 +23,11 @@ * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR from offset to the end, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). + * If you need automatic cleanup, use pcim_iomap_range(). * */ void __iomem *pci_iomap_range(struct pci_dev *dev, int bar, @@ -63,6 +68,10 @@ EXPORT_SYMBOL(pci_iomap_range); * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR from offset to the end, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). * */ void __iomem *pci_iomap_wc_range(struct pci_dev *dev, int bar, @@ -106,6 +115,11 @@ EXPORT_SYMBOL_GPL(pci_iomap_wc_range); * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR without checking for its length first, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). + * If you need automatic cleanup, use pcim_iomap(). * */ void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) { @@ -127,6 +141,10 @@ EXPORT_SYMBOL(pci_iomap); * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR without checking for its length first, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). * */ void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long max= len) { diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 2899df77a285..7ca860acf351 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3907,6 +3907,8 @@ EXPORT_SYMBOL(pci_release_region); * @res_name: Name to be associated with resource. * @exclusive: whether the region access is exclusive or not * + * Returns: 0 on success, negative error code on failure. + * * Mark the PCI region associated with PCI device @pdev BAR @bar as * being reserved by owner @res_name. Do not access any * address inside the PCI regions unless this call returns @@ -3918,6 +3920,13 @@ EXPORT_SYMBOL(pci_release_region); * * Returns 0 on success, or %EBUSY on error. A warning * message is also printed on failure. + * + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you need to implement a new pci + * function that does automatic cleanup, write a new pcim_* function that = uses + * devres directly. */ static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name, int exclusive) @@ -3966,6 +3975,8 @@ static int __pci_request_region(struct pci_dev *pdev,= int bar, * @bar: BAR to be reserved * @res_name: Name to be associated with resource * + * Returns: 0 on success, negative error code on failure. + * * Mark the PCI region associated with PCI device @pdev BAR @bar as * being reserved by owner @res_name. Do not access any * address inside the PCI regions unless this call returns @@ -3973,6 +3984,12 @@ static int __pci_request_region(struct pci_dev *pdev= , int bar, * * Returns 0 on success, or %EBUSY on error. A warning * message is also printed on failure. + * + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name) { @@ -3998,6 +4015,13 @@ void pci_release_selected_regions(struct pci_dev *pd= ev, int bars) } EXPORT_SYMBOL(pci_release_selected_regions); =20 +/* + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. + */ static int __pci_request_selected_regions(struct pci_dev *pdev, int bars, const char *res_name, int excl) { @@ -4023,6 +4047,14 @@ static int __pci_request_selected_regions(struct pci= _dev *pdev, int bars, * @pdev: PCI device whose resources are to be reserved * @bars: Bitmask of BARs to be requested * @res_name: Name to be associated with resource + * + * Returns: 0 on success, negative error code on failure. + * + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ int pci_request_selected_regions(struct pci_dev *pdev, int bars, const char *res_name) @@ -4031,6 +4063,20 @@ int pci_request_selected_regions(struct pci_dev *pde= v, int bars, } EXPORT_SYMBOL(pci_request_selected_regions); =20 +/** + * pci_request_selected_regions_exclusive - Request regions exclusively + * @pdev: PCI device to request regions from + * @bars: bit mask of bars to request + * @res_name: name to be associated with the requests + * + * Returns: 0 on success, negative error code on failure. + * + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. + */ int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars, const char *res_name) { @@ -4048,7 +4094,6 @@ EXPORT_SYMBOL(pci_request_selected_regions_exclusive); * successful call to pci_request_regions(). Call this function only * after all use of the PCI regions has ceased. */ - void pci_release_regions(struct pci_dev *pdev) { pci_release_selected_regions(pdev, (1 << PCI_STD_NUM_BARS) - 1); @@ -4080,6 +4125,8 @@ EXPORT_SYMBOL(pci_request_regions); * @pdev: PCI device whose resources are to be reserved * @res_name: Name to be associated with resource. * + * Returns: 0 on success, negative error code on failure. + * * Mark all PCI regions associated with PCI device @pdev as being reserved * by owner @res_name. Do not access any address inside the PCI regions * unless this call returns successfully. @@ -4089,6 +4136,12 @@ EXPORT_SYMBOL(pci_request_regions); * * Returns 0 on success, or %EBUSY on error. A warning message is also * printed on failure. + * + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_na= me) { @@ -4420,6 +4473,11 @@ void pci_disable_parity(struct pci_dev *dev) * @enable: boolean: whether to enable or disable PCI INTx * * Enables/disables PCI INTx for device @pdev + * + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! */ void pci_intx(struct pci_dev *pdev, int enable) { --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94D645BAEE for ; Tue, 23 Jan 2024 09:44:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003099; cv=none; b=O0oZA4Zr618lMIiujRMS9HxtesZNE+slhHj+p6Lj+vmJpljqUsDWybbI2h7PDUxS+lSdKcuXk/KR6/qkOWjEvfoOzxYlgb5NsOA52P7wyx29+cJjM8gwlfm1fEqmYlpVe7mNoA7xpuquJpNxq0oTsMg7SXjF1rRKvX50LyK6Drk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003099; c=relaxed/simple; bh=jCcPfvVIh+5GPg1n63+yfNtQNLoVDRUUEmfN8ZfGD/c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fBLje3z1LTtPv3rSSvnbzubamvDyNQTODznKxChbYC3ROUChbX8bqSt3szdwgXcfCrYJXhwHdqsCpIP4H/WzWqvRGktqKhspCMP8WLowU7gqTwqGoRlJXB5CVLP7QICT2D33YlIU8snrfPn5N2e8aQ3pQggvK/1uQghZQewnavE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=O2CmA8Jr; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="O2CmA8Jr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003096; 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=hBrROWC/lRyzTMd3ODmKW2xAxrixnDdNmIHjBaaWBE0=; b=O2CmA8JrS6CEvpNcAUIvCnZK1xhLjayKqnufixRvRtcqMvsxdXCscBVzYH5Ii+wnS2ytCx mteGYviCUcYQN5zY2CE1zWSY8Jxp6xkoZ63k97/rMMkYOClHrTKdnpjX6V2elCgjr3UqP9 p1qducmCXIK52ANe1Qr4wech/rr/x5k= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-Zxzo1DhYOmiBWqIZQ9DD3Q-1; Tue, 23 Jan 2024 04:44:54 -0500 X-MC-Unique: Zxzo1DhYOmiBWqIZQ9DD3Q-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7830635331bso136327385a.1 for ; Tue, 23 Jan 2024 01:44:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003094; x=1706607894; 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=hBrROWC/lRyzTMd3ODmKW2xAxrixnDdNmIHjBaaWBE0=; b=tKpEnByF+cZGjMof9iPYsCutFRie5CR7AAoDmABSCFuL5J5rzYXOq/trjCls57i3Qv cYaOtmZ9uNgqFioNzAPspPxoaQ7nlUUa+Hv7KDGn76Z/LZACR1LqY7BAMeIV6p91BbYA CsRoyS0+cCnlKDcL0qcPp+xpJm+zcxDLrUEXLVf2q+OmBY2Oa70Ownu169Q8hWkzvOi6 mj8JkcY5n1f8HoxV4GN/M05xCwT2FpOWvOmMmRKDDhJc39BK8rAk8oCj6dkNf5d+6ZJC IkTyUso8fTvVyWpcoUkXR6lxAHPJSgC5utxevUI6HHpDEguFjp4Adh+O50CKiAKrVmN8 V3Ig== X-Gm-Message-State: AOJu0YzYcor7rgoEwyBwUhjdqYyv6IglH6ahJQo1zewhsfvtbVpFWXD3 fCcbGELEWZxRHusTXy/zbRRJTLy5sB7QaMMBMOHDR81J7rkLn8Y5q77Kzld6rm/g9SWjddf0lZ+ DKyFqGMUlLhlFqChSxv36+8pISgI/t+Omyi5i61q1Zt5fNhoRkzZxJ8oCU70Qug== X-Received: by 2002:a05:6214:29c9:b0:685:be18:eac7 with SMTP id gh9-20020a05621429c900b00685be18eac7mr9471506qvb.5.1706003094194; Tue, 23 Jan 2024 01:44:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IEnPwMs+aSiXOF3FSwgmRVrmKxYCfr3b5DOfusBWOHAH+nj3VPRN1MzQ9Qly0ncG45RNacBjA== X-Received: by 2002:a05:6214:29c9:b0:685:be18:eac7 with SMTP id gh9-20020a05621429c900b00685be18eac7mr9471482qvb.5.1706003093634; Tue, 23 Jan 2024 01:44:53 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:44:53 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 04/10] PCI: make devres region requests consistent Date: Tue, 23 Jan 2024 10:43:01 +0100 Message-ID: <20240123094317.15958-5-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that pure managed region request functions are available, the implementation of the hybrid-functions which are only sometimes managed can be made more consistent and readable by wrapping those always-managed functions. Implement a new pcim_ function for exclusively requested regions. Have the pci_request / release functions call their pcim_ counterparts. Remove the now surplus region_mask from struct pci_devres. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 49 ++++++++++++++++++++++--------------------- drivers/pci/pci.c | 50 +++++++++++++++----------------------------- drivers/pci/pci.h | 6 ------ include/linux/pci.h | 1 + 4 files changed, 43 insertions(+), 63 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 1279aac9b63e..7bff17633bc4 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -22,18 +22,15 @@ * _sometimes_ managed (e.g. pci_request_region()). * Consequently, in the new API, region requests performed by the pcim_ * functions are automatically cleaned up through the devres callback - * pcim_addr_resource_release(), while requests performed by - * pcim_enable_device() + pci_*region*() are automatically cleaned up - * through the for-loop in pcim_release(). + * pcim_addr_resource_release(). + * Users utilizing pcim_enable_device() + pci_*region*() are redirected= in + * pci.c to the managed functions here in this file. This isn't exactly + * perfect, but the only alternative way would be to port ALL drivers u= sing + * said combination to pcim_ functions. * - * TODO 1: + * TODO: * Remove the legacy table entirely once all calls to pcim_iomap_table() in * the kernel have been removed. - * - * TODO 2: - * Port everyone calling pcim_enable_device() + pci_*region*() to using the - * pcim_ functions. Then, remove all devres functionality from pci_*region= *() - * functions and remove the associated cleanups described above in point #= 2. */ =20 /* @@ -399,21 +396,6 @@ 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; - - /* - * This is legacy code. - * All regions requested by a pcim_ function do get released through - * pcim_addr_resource_release(). Thanks to the hybrid nature of the pci_ - * region-request functions, this for-loop has to release the regions - * if they have been requested by such a function. - * - * TODO: Remove this once all users of pcim_enable_device() PLUS - * pci-region-request-functions have been ported to pcim_ functions. - */ - for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) - if (this->region_mask & (1 << i)) - pci_release_region(dev, i); =20 if (this->mwi) pci_clear_mwi(dev); @@ -968,6 +950,25 @@ int pcim_request_region(struct pci_dev *pdev, int bar,= const char *name) } EXPORT_SYMBOL(pcim_request_region); =20 +/** + * pcim_request_region_exclusive - Request a PCI BAR exclusively + * @pdev: PCI device to requestion region for + * @bar: Index of BAR to request + * @name: Name associated with the request + * + * Returns: 0 on success, a negative error code on failure. + * + * Request region specified by @bar exclusively. + * + * The region will automatically be released on driver detach. If desired, + * release manually only with pcim_release_region(). + */ +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const cha= r *name) +{ + return _pcim_request_region(pdev, bar, name, IORESOURCE_EXCLUSIVE); +} +EXPORT_SYMBOL(pcim_request_region_exclusive); + /** * pcim_release_region - Release a PCI BAR * @pdev: PCI device to operate on diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 7ca860acf351..eb3ec59e8822 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3873,7 +3873,15 @@ EXPORT_SYMBOL_GPL(pci_common_swizzle); */ void pci_release_region(struct pci_dev *pdev, int bar) { - struct pci_devres *dr; + /* + * This is done for backwards compatibility, because the old pci-devres + * API had a mode in which the function became managed if it had been + * enabled with pcim_enable_device() instead of pci_enable_device(). + */ + if (pci_is_managed(pdev)) { + pcim_release_region(pdev, bar); + return; + } =20 if (pci_resource_len(pdev, bar) =3D=3D 0) return; @@ -3883,20 +3891,6 @@ void pci_release_region(struct pci_dev *pdev, int ba= r) else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) release_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)); - - /* - * This devres utility makes this function sometimes managed - * (when pcim_enable_device() has been called before). - * This is bad because it conflicts with the pcim_ functions being - * exclusively responsible for managed pci. Its "sometimes yes, sometimes - * no" nature can cause bugs. - * - * TODO: Remove this once all users that use pcim_enable_device() PLUS - * a region request function have been ported to using pcim_ functions. - */ - dr =3D find_pci_dr(pdev); - if (dr) - dr->region_mask &=3D ~(1 << bar); } EXPORT_SYMBOL(pci_release_region); =20 @@ -3924,14 +3918,18 @@ EXPORT_SYMBOL(pci_release_region); * NOTE: * This is a "hybrid" function: Its normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. - * This hybrid feature is DEPRECATED! If you need to implement a new pci - * function that does automatic cleanup, write a new pcim_* function that = uses - * devres directly. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name, int exclusive) { - struct pci_devres *dr; + if (pci_is_managed(pdev)) { + if (exclusive =3D=3D IORESOURCE_EXCLUSIVE) + return pcim_request_region_exclusive(pdev, bar, res_name); + + return pcim_request_region(pdev, bar, res_name); + } =20 if (pci_resource_len(pdev, bar) =3D=3D 0) return 0; @@ -3947,20 +3945,6 @@ static int __pci_request_region(struct pci_dev *pdev= , int bar, goto err_out; } =20 - /* - * This devres utility makes this function sometimes managed - * (when pcim_enable_device() has been called before). - * This is bad because it conflicts with the pcim_ functions being - * exclusively responsible for managed pci. Its "sometimes yes, sometimes - * no" nature can cause bugs. - * - * TODO: Remove this once all users that use pcim_enable_device() PLUS - * a region request function have been ported to using pcim_ functions. - */ - dr =3D find_pci_dr(pdev); - if (dr) - dr->region_mask |=3D 1 << bar; - return 0; =20 err_out: diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 4d4bcc2d850f..7c2181677760 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -814,12 +814,6 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; - - /* - * TODO: remove the region_mask once everyone calling - * pcim_enable_device() + pci_*region*() is ported to pcim_ functions. - */ - u32 region_mask; }; =20 struct pci_devres *find_pci_dr(struct pci_dev *pdev); diff --git a/include/linux/pci.h b/include/linux/pci.h index 3dcd8a5e10b5..641ee30f7d2d 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2321,6 +2321,7 @@ int pcim_iomap_regions_request_all(struct pci_dev *pd= ev, int mask, const char *name); void pcim_iounmap_regions(struct pci_dev *pdev, int mask); int pcim_request_region(struct pci_dev *pdev, int bar, const char *res_nam= e); +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const cha= r *name); void pcim_release_region(struct pci_dev *pdev, int bar); void pcim_release_all_regions(struct pci_dev *pdev); int pcim_request_all_regions(struct pci_dev *pdev, const char *name); --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B1F75C5F0 for ; Tue, 23 Jan 2024 09:44:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003100; cv=none; b=tyDfynC4qbUszQxFLIRI6kcUDUsdXaPkNGtcafS/h7R4kY7FMib4qjaRGWYI98r2WchfUjgZJbSypKZrqbwAl3gsq0/Ypu0Y0eo+UcNSsxT3dd0wpq1mQMzUGxI0fQXEMWIm/3YeKF5Qng0RFAcs5m2pjDr9JKgtjzzx32WORy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003100; c=relaxed/simple; bh=KIuP+YZwliPwVC5hbGK2gsLWRa4HN6FV/A+opHrJu2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nk5mBzJHK4br41nqnnEAU42/8/SuZxRjzAQtwS8NbY9bwmKxaUw1K05xjFmpvygS5r8iEx4wUFXj3ejKtf/tzftySpAaWIiPtr6X1GoJKdRIdcifwSFtatKYSlWAnwTO4+ha7r1wR26nu8BZajtaNVh3krZgD4g1foPghgeKYVk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Up1zjTwo; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Up1zjTwo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003098; 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=MJOcERA6WkIGWk0k/QFxozz5e8Wq1nBqoQjIfxjRAsE=; b=Up1zjTwoCeMFwANiurK+ZVr2LuwzeOTpS/w32ym81EUVzCm8+tW0Q5unyAOHL6zOaS9hdl Nu4e/TDlNpwmZ3T/R5VvFRWZ05f7wODtDbR0zGWj3Orarwpl1rFzZKrbOSqI8fdj6iDzUr q8IF03aUYQUvSwCcFJWV4UW44hTupMo= Received: from mail-vs1-f72.google.com (mail-vs1-f72.google.com [209.85.217.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-M-uDEiIVPGqwKJXmh1oBWg-1; Tue, 23 Jan 2024 04:44:56 -0500 X-MC-Unique: M-uDEiIVPGqwKJXmh1oBWg-1 Received: by mail-vs1-f72.google.com with SMTP id ada2fe7eead31-469adc951fdso231028137.1 for ; Tue, 23 Jan 2024 01:44:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003096; x=1706607896; 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=MJOcERA6WkIGWk0k/QFxozz5e8Wq1nBqoQjIfxjRAsE=; b=EIoeAf5nTy2qCdgdnjDs46S9iqBCOBJ1Cb7Vt9P2zkPy/QgJgxa1BFFbMFl+XXyivf yo3EEqmboRReK/da05Bi5wqxHa5+7gJ5WF8roJYEAVZT9Dh0oUPGHEX/T7NgC/PBUXvH zCLeAandePU9hEZ3OrOycCJJHgxW0A0JsYGI1ufmLYMwwwjf8PSyitpctmRx6mYRTCyW w2CL2fhO+5Fx6MjoaPYj0+nZCuQjTYHNcGUJjTmhjnUEM7YvVl6tlf84iRqygowdsHi2 gbAGZ5hWrIY69EqlR1INh31twFrVPMvKrOST58h35D7TRrPff25rGcI+pDtNk03tIYFu k8/Q== X-Gm-Message-State: AOJu0YxcMyqpcSvaFmacsccw94vUbI9O4L6a09hhZvKoJsWuKwVz1R5q fDXrSQ1AXoX1KVbYox/k2PJCqWqCeh4hTLaU9JxmfNcqhXasjalW24k87zuyTFMpobyBNnHoZVJ Ix0fMj2n8fslOOzwzrUhqgjT5M6OPArfNKbq+2GmEddiHM37dNbQQpp9QdJvURQ== X-Received: by 2002:a05:6102:300f:b0:469:a5a6:6d0b with SMTP id s15-20020a056102300f00b00469a5a66d0bmr5387237vsa.0.1706003096219; Tue, 23 Jan 2024 01:44:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IEfiHCQyTaU9QSY0mi169Dtu5iV731XTwQIfoBCycLdQ++32x8wjMJie+l0KlKJveytXiqnhA== X-Received: by 2002:a05:6102:300f:b0:469:a5a6:6d0b with SMTP id s15-20020a056102300f00b00469a5a66d0bmr5387218vsa.0.1706003095921; Tue, 23 Jan 2024 01:44:55 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:44:55 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 05/10] PCI: move dev-enabled status bit to struct pci_dev Date: Tue, 23 Jan 2024 10:43:02 +0100 Message-ID: <20240123094317.15958-6-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The bit describing whether the PCI device is currently enabled is stored in struct pci_devres. Besides this struct being subject of a cleanup process, struct pci_device is in general the right place to store this information, since it is not devres-specific. Move the 'enabled' boolean bit to struct pci_dev. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 11 ++++------- drivers/pci/pci.c | 17 ++++++++++------- drivers/pci/pci.h | 1 - include/linux/pci.h | 1 + 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 7bff17633bc4..696541c65c8f 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *r= es) if (this->restore_intx) pci_intx(dev, this->orig_intx); =20 - if (this->enabled && !this->pinned) + if (!this->pinned) pci_disable_device(dev); } =20 @@ -448,14 +448,11 @@ int pcim_enable_device(struct pci_dev *pdev) dr =3D get_pci_dr(pdev); if (unlikely(!dr)) return -ENOMEM; - if (dr->enabled) - return 0; =20 rc =3D pci_enable_device(pdev); - if (!rc) { + if (!rc) pdev->is_managed =3D 1; - dr->enabled =3D 1; - } + return rc; } EXPORT_SYMBOL(pcim_enable_device); @@ -473,7 +470,7 @@ void pcim_pin_device(struct pci_dev *pdev) struct pci_devres *dr; =20 dr =3D find_pci_dr(pdev); - WARN_ON(!dr || !dr->enabled); + WARN_ON(!dr || !pdev->enabled); if (dr) dr->pinned =3D 1; } diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index eb3ec59e8822..ab8d66e05662 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -1953,6 +1953,9 @@ static int do_pci_enable_device(struct pci_dev *dev, = int bars) u16 cmd; u8 pin; =20 + if (dev->enabled) + return 0; + err =3D pci_set_power_state(dev, PCI_D0); if (err < 0 && err !=3D -EIO) return err; @@ -1967,7 +1970,7 @@ static int do_pci_enable_device(struct pci_dev *dev, = int bars) pci_fixup_device(pci_fixup_enable, dev); =20 if (dev->msi_enabled || dev->msix_enabled) - return 0; + goto success_out; =20 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin); if (pin) { @@ -1977,6 +1980,8 @@ static int do_pci_enable_device(struct pci_dev *dev, = int bars) cmd & ~PCI_COMMAND_INTX_DISABLE); } =20 +success_out: + dev->enabled =3D true; return 0; } =20 @@ -2146,6 +2151,9 @@ static void do_pci_disable_device(struct pci_dev *dev) { u16 pci_command; =20 + if (!dev->enabled) + return; + pci_read_config_word(dev, PCI_COMMAND, &pci_command); if (pci_command & PCI_COMMAND_MASTER) { pci_command &=3D ~PCI_COMMAND_MASTER; @@ -2153,6 +2161,7 @@ static void do_pci_disable_device(struct pci_dev *dev) } =20 pcibios_disable_device(dev); + dev->enabled =3D false; } =20 /** @@ -2180,12 +2189,6 @@ void pci_disable_enabled_device(struct pci_dev *dev) */ void pci_disable_device(struct pci_dev *dev) { - struct pci_devres *dr; - - dr =3D find_pci_dr(dev); - if (dr) - dr->enabled =3D 0; - dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <=3D 0, "disabling already-disabled device"); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 7c2181677760..dbb76a3fb0e4 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -809,7 +809,6 @@ static inline pci_power_t mid_pci_get_power_state(struc= t pci_dev *pdev) * 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; diff --git a/include/linux/pci.h b/include/linux/pci.h index 641ee30f7d2d..a356bdcc14cc 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -367,6 +367,7 @@ struct pci_dev { this is D0-D3, D0 being fully functional, and D3 being off. */ u8 pm_cap; /* PM capability offset */ + unsigned int enabled:1; /* Whether this dev is enabled */ unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int pme_support:5; /* Bitmask of states from which PME# can be generated */ --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 652815C8EF for ; Tue, 23 Jan 2024 09:45:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003103; cv=none; b=NOWtovM83O68JV3ZyguuBYd5ldD0jeN3OQKA7XZuXrew7H7gHMTVsOlOJN0cQ7gjgyQOY1tr23tOXJEpXCLN2uODmhhnPzrFvEKO0ELDHaLRKQ8p148l7ORs8gp2S6HiELHnIECjfQsK3JZ84Pff7easUyF8xPOHB/z3oY1c2wI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003103; c=relaxed/simple; bh=4ifRbNE/UvC5FcM85a3SetEYfA018CmEKjlHBrPer30=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ExDX5Pwp4w5NmkRvR5m38RLEh87+dLhxKLN5eLyYdv41qtFYfHpCrXEZVpkLQntMFTzRbww+WqN1N+ZgGebJfRX4PgOlyChZHDE7fp1yREcLNLL6aj2HvFsgwmFxkX45ojfCJINh6Gl2cFOSatekXfjjeow684w/yGe/v3VJiZo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=PhdSC7yY; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PhdSC7yY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003100; 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=mZ38BpMLw1nmEQJiWPTNL5fM1ejJipykQi7icSpF5L4=; b=PhdSC7yYd4QK+NOcFU3S4EqVrVhxV0wmJI32PGm+qaSYOirTJC7wUjTWZ+m75ZrKFLAdZY I5B//zrRaCMvk5sGlITLZb1ZFkqqeyy8sEnYCvQjAxxaCDA+6ewR7FMWuY8NnAQ0a47Q7P 58cZvVKE3NtydRqejL9V3rcT91cLW60= Received: from mail-vs1-f69.google.com (mail-vs1-f69.google.com [209.85.217.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-692-UwaQxAeaO46g9_umUoVGTw-1; Tue, 23 Jan 2024 04:44:59 -0500 X-MC-Unique: UwaQxAeaO46g9_umUoVGTw-1 Received: by mail-vs1-f69.google.com with SMTP id ada2fe7eead31-469adc951fdso231034137.1 for ; Tue, 23 Jan 2024 01:44:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003098; x=1706607898; 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=mZ38BpMLw1nmEQJiWPTNL5fM1ejJipykQi7icSpF5L4=; b=rTp71+oVJbPs03GNiJOHb01aYYjpaMzOldaV6Mksl2mLvlkuQLYLmyiOmz2MzuS7s9 ZDkleDoTrhvDzNKIxk9MzLd2xj221Juk3Ti3y2jlVeRIkHwPuTxJYtP32rsQwUXEg2UW VJnbeaHAB/cVnVCP9c9+56Pow/zIeS+53f+HeEmYY/tSw23aJgPHEsB2zM/bTFuB+thY 0E0qF00iFxkDVNYxKJlTGZ6sd5/528iw44AVfvfl1OVrr8FwUgtYqSz2nQjb+LIY3sMU x+CVQyOjoodbgOWvrwJ2CdOj2Bs4VEFSoUDH5ISOPFv++JrCEB3k5XS7ue8wCKXdni0S FCdw== X-Gm-Message-State: AOJu0Yw1v3CpfSkyaNzFioVHTbTFENB2mwo/8mH/qH7JaJOQp3W+Nr9B kZunjl4jsCu2KjtCWOETfDQvWGgbbu6edZJChlvTTJQKB/nAryn6WS7axU63v1l4aELcKVJi++f fRO5Gy1OxsDOJ3m60RStTSuz6yxCaH4wjbptG4tvVZYP2iRHarAkvrnIiqpmEeg== X-Received: by 2002:a05:6102:5594:b0:469:a652:8df5 with SMTP id dc20-20020a056102559400b00469a6528df5mr5138870vsb.1.1706003098545; Tue, 23 Jan 2024 01:44:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IEivN7OX9YY7oVy+KgkCGjsC3T0Q8wxZ8vUmiA80b9QIyaRCXaYkhUqcQ/3h07xKSGdWjW0qA== X-Received: by 2002:a05:6102:5594:b0:469:a652:8df5 with SMTP id dc20-20020a056102559400b00469a6528df5mr5138858vsb.1.1706003098225; Tue, 23 Jan 2024 01:44:58 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:44:58 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 06/10] PCI: move pinned status bit to struct pci_dev Date: Tue, 23 Jan 2024 10:43:03 +0100 Message-ID: <20240123094317.15958-7-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The bit describing whether the PCI device is currently pinned is stored in struct pci_devres. To clean up and simplify the PCI devres API, it's better if this information is stored in struct pci_dev, because it allows for checking that device's pinned-status directly through struct pci_dev. This will later permit simplifying pcim_enable_device(). Move the 'pinned' boolean bit to struct pci_dev. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 14 ++++---------- drivers/pci/pci.h | 1 - include/linux/pci.h | 5 +++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 696541c65c8f..03336a2f00d6 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *r= es) if (this->restore_intx) pci_intx(dev, this->orig_intx); =20 - if (!this->pinned) + if (!dev->pinned) pci_disable_device(dev); } =20 @@ -461,18 +461,12 @@ 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(). + * 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 || !pdev->enabled); - if (dr) - dr->pinned =3D 1; + pdev->pinned =3D true; } EXPORT_SYMBOL(pcim_pin_device); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index dbb76a3fb0e4..3d9908a69ebf 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -809,7 +809,6 @@ static inline pci_power_t mid_pci_get_power_state(struc= t pci_dev *pdev) * when a device is enabled using managed PCI device enable interface. */ struct pci_devres { - unsigned int pinned:1; unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; diff --git a/include/linux/pci.h b/include/linux/pci.h index a356bdcc14cc..efbe2ed92343 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -367,11 +367,12 @@ struct pci_dev { this is D0-D3, D0 being fully functional, and D3 being off. */ u8 pm_cap; /* PM capability offset */ - unsigned int enabled:1; /* Whether this dev is enabled */ - unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int pme_support:5; /* Bitmask of states from which PME# can be generated */ unsigned int pme_poll:1; /* Poll device's PME status bit */ + unsigned int enabled:1; /* Whether this dev is enabled */ + unsigned int pinned:1; /* Whether this dev is pinned */ + unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int d1_support:1; /* Low power state D1 is supported */ unsigned int d2_support:1; /* Low power state D2 is supported */ unsigned int no_d1d2:1; /* D1 and D2 are forbidden */ --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD1105D8EB for ; Tue, 23 Jan 2024 09:45:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003105; cv=none; b=FRVa9ETRk86/khQsSmmkPL2ZD8+g9385k3UfSP9Nl1+CyrVM18nLEv/AjU4IlEY0Iu0EuHTkOtLpc/uBr9eStkLAIvIo3xd8Fw4wflTzHcN9rH04CmDv9gh+sionHbQprli6+juDxUEWCYWj7W+xfO7bngCEmyPQIVgI6AGXq0s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003105; c=relaxed/simple; bh=02ujaudo3KSmsF5K5ztuR/Ql8u25boOsUld4kW2EV8k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fiRV+fKDO2O8ZnPcIQOpQuRnakb8mcyggq79+7413nYT2r3NA3mIo3u6cu6NaBYSPFPiOGbUcqQhS9l9AzBDQFQxdVbOR4XQkbpn761bzJlvFmZVQzqio+jiuijUYOekrhBfjiEYla/2X68DdqAV5yYvPjpYz5l1OtVqRpWnAfs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UvnBSLBv; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UvnBSLBv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003103; 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=cXd3AZjPKhslGVevCwKeJnsSWxdJPCffQSFxc+gLUxI=; b=UvnBSLBv0X+1YozQjUoIDzYpwRHs043yZdi3CiTybDFWBfZlVRCQeGYb6/GDY5ZUdLhSsw Bh9yezYPl/kId0UkgrFMJpyV9+wR58ZAHhjHneI0LRi1XbsQkOHBxDyZQwVixbBT1EDpdx USp/9OYpYdjpuqv/FdAEvh7O7c2LpIU= Received: from mail-vs1-f72.google.com (mail-vs1-f72.google.com [209.85.217.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-610-INwfz9d9Mz2jq0z7ydNXEA-1; Tue, 23 Jan 2024 04:45:01 -0500 X-MC-Unique: INwfz9d9Mz2jq0z7ydNXEA-1 Received: by mail-vs1-f72.google.com with SMTP id ada2fe7eead31-46976c07c39so454085137.0 for ; Tue, 23 Jan 2024 01:45:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003101; x=1706607901; 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=cXd3AZjPKhslGVevCwKeJnsSWxdJPCffQSFxc+gLUxI=; b=Oixpse5c38NS/gt9O9cDAWNinZTZRST5cAbIBsTNUOG6ZoDLLl9E9kcCD+dxoyvdOS hlD3LrvGyr93IX9meqzUZ+NOFErc02GxjEUa0hyqpqw16StZtTkUUB/xJ6pCnB9Vl4kX zfiWCbz/LQw5FRhx2tZ5LDvrv4gs+td6/gHDc6m8nJSzaGafZiwDYXKyDukX2FkdCLjH IoL2tzOpwjKd6//ufUvl4ZbzuYCMu2IpMDEx8vYA0Jj8F08MLVh5ze9adVc7DL8t1BWM 0aDWk2CG2inZsrQOucRmm9VybDvmpZOhxkvbgBbCIH+Scjdf00urFUxxFJXkzUwyaP/B X/ew== X-Gm-Message-State: AOJu0YzMiVE97LCsjmqvfea4ZpsjewkD6YxryE0AvA/4WRQBy/JR3ghf havh69pMVZUxKVw6tWAahc/9fUpTO6uZJgiYxYoc/SRKKQnvQtML5ZVOwkNsI7sEAQR8uvzlqBa dadyEPwU0ECt1LhlsKGp7bL0j+C3SbUxMPHZ5kmZjdsIogZA12g+vwhqJh7T7Zw== X-Received: by 2002:a05:6122:1684:b0:4bd:4151:936f with SMTP id 4-20020a056122168400b004bd4151936fmr409258vkl.1.1706003100845; Tue, 23 Jan 2024 01:45:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGRoL1ahcR75vBLXWQC/4Tfk26KCchDZ9UlSe50TIl+tMW6CAPN/T2lwZOJ68o/lL+18tX5pQ== X-Received: by 2002:a05:6122:1684:b0:4bd:4151:936f with SMTP id 4-20020a056122168400b004bd4151936fmr409238vkl.1.1706003100567; Tue, 23 Jan 2024 01:45:00 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:45:00 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 07/10] PCI: give pcim_set_mwi() its own devres callback Date: Tue, 23 Jan 2024 10:43:04 +0100 Message-ID: <20240123094317.15958-8-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Managing pci_set_mwi() with devres can easily be done with its own callback, without the necessity to store any state about it in a device-related struct. Remove the MWI state from struct pci_devres. Give pcim_set_mwi() a separate devres-callback. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 29 ++++++++++++++++++----------- drivers/pci/pci.h | 1 - 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 03336a2f00d6..d396d672a6f4 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -370,24 +370,34 @@ void __iomem *devm_pci_remap_cfg_resource(struct devi= ce *dev, } EXPORT_SYMBOL(devm_pci_remap_cfg_resource); =20 +static void __pcim_clear_mwi(void *pdev_raw) +{ + struct pci_dev *pdev =3D pdev_raw; + + pci_clear_mwi(pdev); +} + /** * pcim_set_mwi - a device-managed pci_set_mwi() - * @dev: the PCI device for which MWI is enabled + * @pdev: 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) +int pcim_set_mwi(struct pci_dev *pdev) { - struct pci_devres *dr; + int ret; =20 - dr =3D find_pci_dr(dev); - if (!dr) - return -ENOMEM; + ret =3D devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev); + if (ret !=3D 0) + return ret; + + ret =3D pci_set_mwi(pdev); + if (ret !=3D 0) + devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev); =20 - dr->mwi =3D 1; - return pci_set_mwi(dev); + return ret; } EXPORT_SYMBOL(pcim_set_mwi); =20 @@ -397,9 +407,6 @@ static void pcim_release(struct device *gendev, void *r= es) struct pci_dev *dev =3D to_pci_dev(gendev); struct pci_devres *this =3D res; =20 - if (this->mwi) - pci_clear_mwi(dev); - if (this->restore_intx) pci_intx(dev, this->orig_intx); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 3d9908a69ebf..667bfdd61d5e 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -811,7 +811,6 @@ static inline pci_power_t mid_pci_get_power_state(struc= t pci_dev *pdev) struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; - unsigned int mwi:1; }; =20 struct pci_devres *find_pci_dr(struct pci_dev *pdev); --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C95AD5F842 for ; Tue, 23 Jan 2024 09:45:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003108; cv=none; b=BO7Fnzgj19ddJ6M6rcSRfNiBqlfrTHkzSnvx3btjlINF2C/fxE8J9lXhy46C6bYx4RvRETHkLZVPtCWdIGWqb1ekS95QNh1UXP71XyipkyiKXZQE/TQ0t8xYv1wZCTvLSc68CKR11OMNa808EvRPEX696w5PgsoyOybAOlwBgGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003108; c=relaxed/simple; bh=5SBUvOssUDD+2gGAiGFtJKi4a09NTizmqH1j3HCBM0U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c9Vt72lw7WSkVZJIXoy3txHLHdPEa1cXzLYlPeKMusuwDwetK03tkEQGeFKeCsH+0pnewLrMMCwLbrIvU3FyyfwKK/nm34xmEd/AxSCTVLJZQt8suFgMfJVxAavE/BuobSy8j3RAfSlbj+NU5gY/zfBwtzyPda8AlRsvpYpLeD8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JC4WNEc7; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JC4WNEc7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003105; 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=hPaQbXk8XLEaVElIGgvAHXLJscVCCVhCxcSL8y0kSzA=; b=JC4WNEc7jZ+5j5/pCUglqcJX3cDZONmNY+U4B6LmFN+yLYJgYkSfFSN4g6GVDpjoFns9+7 wV7EUiFLAKru+3xUk74lLhPqg4BxJC/YJGO9c9WlfTxbXeBeo6IaqupE2VsatN0qs8SOU/ /aR2sUrw2R4c5t3+Ed7kb6VMz+h24/k= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-477-tRl7XzI6Mx2ijmihY1ct5w-1; Tue, 23 Jan 2024 04:45:03 -0500 X-MC-Unique: tRl7XzI6Mx2ijmihY1ct5w-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-78356ddf3cfso127858185a.1 for ; Tue, 23 Jan 2024 01:45:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003103; x=1706607903; 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=hPaQbXk8XLEaVElIGgvAHXLJscVCCVhCxcSL8y0kSzA=; b=nd52eL9R4c7j3zy4z0d0gEJKkCxHfI7EbZH7F85AtxE8EoX8O6W0o5+exTBJoheWWH vfV4BbXCLArX9peeKW7bXAXVZWeH8aFSOje1juVzlHUYH1PqnEqomve3Q093oWtVYXE3 c4886wUKceU0UuQwDxgb8ezsZgAdViD+rVTahafW+OKUvfUq3rSZ7drinVo4adS+n/rA 1gnTMRM8bV6tLdSr9eeW6K7ehPhYFAjq537+bK6U5h5KVvv9iESxzc60mrG7j/LsFxhk +WtAdwhbaJfIEDQ2bSb2Qm0vcira7+psn9TS0EYYIEX2CvWEGajCcCBztUAA39SdWVLJ y3kQ== X-Gm-Message-State: AOJu0YzOiESQIF6DHx/26v4qGjTYgkmaU/NZflJ8VszcA1ibg4ihUUvn am1iUmDBZ4RRoQKBdqJPbZICJ8sRbAbBLiBQT6UajQuQhSmSUvzD3TzGJBFzUuXBNeJ3V6zYHuO VGoQq/TVfdAG6H4d5qRPIujYg1LnsO+xqqmrMLE24dr8RXseZGdDMm5FvtfPVAg== X-Received: by 2002:a05:6214:f26:b0:685:7cfd:34eb with SMTP id iw6-20020a0562140f2600b006857cfd34ebmr9324897qvb.4.1706003103114; Tue, 23 Jan 2024 01:45:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IFtcDj2ng1o0t8y8DdObSU32+uI7sQsarbF6v7QkK54odso969HOgDvhCFed+0S/Tvhd8Mu/g== X-Received: by 2002:a05:6214:f26:b0:685:7cfd:34eb with SMTP id iw6-20020a0562140f2600b006857cfd34ebmr9324885qvb.4.1706003102866; Tue, 23 Jan 2024 01:45:02 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.45.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:45:02 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 08/10] PCI: give pci(m)_intx its own devres callback Date: Tue, 23 Jan 2024 10:43:05 +0100 Message-ID: <20240123094317.15958-9-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" pci_intx() is one of the functions that have "hybrid mode" (i.e., sometimes managed, sometimes not). Providing a separate pcim_intx() function with its own device resource and cleanup callback allows for removing further large parts of the legacy pci-devres implementation. As in the region-request-functions, pci_intx() has to call into its managed counterpart for backwards compatibility. As pci_intx() is an outdated function, pcim_intx() shall not be made visible to other drivers via a public API. Implement pcim_intx() with its own device resource. Make pci_intx() call pcim_intx() in the managed case. Remove the struct pci_devres from pci.h. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 76 ++++++++++++++++++++++++++++++++++---------- drivers/pci/pci.c | 24 +++++++------- drivers/pci/pci.h | 21 +++--------- 3 files changed, 77 insertions(+), 44 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index d396d672a6f4..4314d0863282 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -40,6 +40,11 @@ struct pcim_iomap_devres { void __iomem *table[PCI_STD_NUM_BARS]; }; =20 +/* Used to restore the old intx state on driver detach. */ +struct pcim_intx_devres { + int orig_intx; +}; + enum pcim_addr_devres_type { /* Default initializer. */ PCIM_ADDR_DEVRES_TYPE_INVALID, @@ -401,31 +406,70 @@ int pcim_set_mwi(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_set_mwi); =20 +static void pcim_intx_restore(struct device *dev, void *data) +{ + struct pci_dev *pdev =3D to_pci_dev(dev); + struct pcim_intx_devres *res =3D data; =20 -static void pcim_release(struct device *gendev, void *res) + pci_intx(pdev, res->orig_intx); +} + +static struct pcim_intx_devres *get_or_create_intx_devres(struct device *d= ev) { - struct pci_dev *dev =3D to_pci_dev(gendev); - struct pci_devres *this =3D res; + struct pcim_intx_devres *res; =20 - if (this->restore_intx) - pci_intx(dev, this->orig_intx); + res =3D devres_find(dev, pcim_intx_restore, NULL, NULL); + if (res) + return res; =20 - if (!dev->pinned) - pci_disable_device(dev); + res =3D devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL); + if (res) + devres_add(dev, res); + + return res; } =20 -/* - * TODO: - * Once the last four callers in pci.c are ported, this function here need= s to - * be made static again. +/** + * pcim_intx - managed pci_intx() + * @pdev: the PCI device to operate on + * @enable: boolean: whether to enable or disable PCI INTx + * + * Returns: 0 on success, -ENOMEM on error. + * + * Enables/disables PCI INTx for device @pdev. + * Restores the original state on driver detach. */ -struct pci_devres *find_pci_dr(struct pci_dev *pdev) +int pcim_intx(struct pci_dev *pdev, int enable) { - if (pci_is_managed(pdev)) - return devres_find(&pdev->dev, pcim_release, NULL, NULL); - return NULL; + u16 pci_command, new; + struct pcim_intx_devres *res; + + res =3D get_or_create_intx_devres(&pdev->dev); + if (!res) + return -ENOMEM; + + res->orig_intx =3D !enable; + + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); + + if (enable) + new =3D pci_command & ~PCI_COMMAND_INTX_DISABLE; + else + new =3D pci_command | PCI_COMMAND_INTX_DISABLE; + + if (new !=3D pci_command) + pci_write_config_word(pdev, PCI_COMMAND, new); + + return 0; +} + +static void pcim_release(struct device *gendev, void *res) +{ + struct pci_dev *dev =3D to_pci_dev(gendev); + + if (!dev->pinned) + pci_disable_device(dev); } -EXPORT_SYMBOL(find_pci_dr); =20 static struct pci_devres *get_pci_dr(struct pci_dev *pdev) { diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index ab8d66e05662..323d5b52f669 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3877,7 +3877,7 @@ EXPORT_SYMBOL_GPL(pci_common_swizzle); void pci_release_region(struct pci_dev *pdev, int bar) { /* - * This is done for backwards compatibility, because the old pci-devres + * This is done for backwards compatibility, because the old PCI devres * API had a mode in which the function became managed if it had been * enabled with pcim_enable_device() instead of pci_enable_device(). */ @@ -4465,11 +4465,22 @@ void pci_disable_parity(struct pci_dev *dev) * This is a "hybrid" function: Its normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. * This hybrid feature is DEPRECATED! + * Use pcim_intx() if you need a managed version. */ void pci_intx(struct pci_dev *pdev, int enable) { u16 pci_command, new; =20 + /* + * This is done for backwards compatibility, because the old PCI devres + * API had a mode in which this function became managed if the dev had + * been enabled with pcim_enable_device() instead of pci_enable_device(). + */ + if (pci_is_managed(pdev)) { + WARN_ON_ONCE(pcim_intx(pdev, enable) !=3D 0); + return; + } + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); =20 if (enable) @@ -4477,17 +4488,8 @@ void pci_intx(struct pci_dev *pdev, int enable) else new =3D pci_command | PCI_COMMAND_INTX_DISABLE; =20 - if (new !=3D pci_command) { - struct pci_devres *dr; - + if (new !=3D pci_command) pci_write_config_word(pdev, PCI_COMMAND, new); - - dr =3D find_pci_dr(pdev); - if (dr && !dr->restore_intx) { - dr->restore_intx =3D 1; - dr->orig_intx =3D !enable; - } - } } EXPORT_SYMBOL_GPL(pci_intx); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 667bfdd61d5e..e6d4b81ff09e 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -796,24 +796,11 @@ static inline pci_power_t mid_pci_get_power_state(str= uct 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. + * This is a helper that shall only ever be called by pci_intx(). It helps + * cleaning up the PCI devres API in which pci_intx() became a managed fun= ction + * under certain conditions. */ -/* - * 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 orig_intx:1; - unsigned int restore_intx:1; -}; - -struct pci_devres *find_pci_dr(struct pci_dev *pdev); +int pcim_intx(struct pci_dev *dev, int enable); =20 /* * Config Address for PCI Configuration Mechanism #1 --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 099965F86E for ; Tue, 23 Jan 2024 09:45:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003110; cv=none; b=FWrawKfjBHqDgqINSekdVKiVKQK1rLM0kFeAYx186AAweL0edJRPDzm1lq4eXnYVsm0+2kQFirVkOxXx+tWDjZYjEAQWt0eGF1pI7bGgETl+sVKaMDqvRl0S9vnaIVao6tLpAqvE8SA8c8/Rv4d8Olj/7no7Gil1GMQz7LhK1Zc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003110; c=relaxed/simple; bh=q2R0g7Hy2U3uiquhdJnwowChrgBPdiPXucNQWzskgyA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C94nfq1Cp+eTqfOC2wFlwMt73OpMSp3PnEj0rmlkWWXFQCDVUxffZJmdhBfrdJnvAi3/Wi818Tyqw9Ma+aUl9HLbbIe54XPeqpZ2C0sM265ezjAxQdECwQpMzD3XosV2AryBkMuGqZJc7k3S2IYtNNtMeHhwPFZMA0VeoNZuPr8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=KuXEKh6P; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KuXEKh6P" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003108; 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=n8ldR1APM7FCb8Va/7W7rWsZ/5PXg9NQPlDweLxrFR8=; b=KuXEKh6Pzz35ynpJAK06nvAGnEdSMLah4Lc2r2Ig5GM8KNhvsdFSVzMg+Ubk7mXSI+KOT5 1Qd4Th7YLqgZhUGlCcnvl3OWSwKatRWrUbHUvVfyXxvUMvltFCubroPEqgrKDiVPA+Viwr dGTqCDho+YG0IhuQiOlI5l3yWw/h+IU= Received: from mail-vs1-f70.google.com (mail-vs1-f70.google.com [209.85.217.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-46-q4U8t4FvMzS7VZcTLW9rig-1; Tue, 23 Jan 2024 04:45:06 -0500 X-MC-Unique: q4U8t4FvMzS7VZcTLW9rig-1 Received: by mail-vs1-f70.google.com with SMTP id ada2fe7eead31-46a9d50a8b4so216873137.1 for ; Tue, 23 Jan 2024 01:45:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003105; x=1706607905; 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=n8ldR1APM7FCb8Va/7W7rWsZ/5PXg9NQPlDweLxrFR8=; b=S9FK4Qq1d7uDAYOzjcj6V/bgdyIW4vyilsrKgg8m/J997gD3iec6cYVay2L4O3SaW8 3KFROu5bEQGaX8q1Uo8fPt3RmKAJckfXh6y0fsZV2mTO2R+G1YzzKQuvG05ol5RBP5Ry EcUBsAsZ5pLAh7Z7/k9BFggovYgyHQsmYL5nvievZMIdyEG60FXH9jgotc5upBVKSUz3 qF3D1FuQ84tl0teLTkBfVIvwoo2zh6t+6PefhlHm7lRci6l8j6W8zV/EDn5eJiFI+R6K uaE3JWm3iusFxrszZapqR6h16MSS1858UjNANV2kvlOqJ65qacwS/pALDiqVLqNrT6U5 mkEw== X-Gm-Message-State: AOJu0YxKVRZVBLBusGhU6bsYkXJx1vD+0WFIxArgwUDpSUwXZO/MF/x9 PhOzW4qTekruTr+S0bIaPDc3Ui1ElEI99CTBEciJ1LJoqPRsGv9XrhRBkoWft0eoOvlafa3SFcl ij2fsgJWukz3Pd/Ja96U4LfVHE1gtiM6A+agComsXuUS1qGROt/sa0CWgx4Eu77FM/LMedQ== X-Received: by 2002:a05:6102:5493:b0:469:b7ba:85e2 with SMTP id bk19-20020a056102549300b00469b7ba85e2mr3802818vsb.1.1706003105398; Tue, 23 Jan 2024 01:45:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBtpt9W0N9DjO3th5WheR6cQoXtCbywpbrf12aGS9J/9nurG+TirAyDVaNm7jxUkx0ffoI5g== X-Received: by 2002:a05:6102:5493:b0:469:b7ba:85e2 with SMTP id bk19-20020a056102549300b00469b7ba85e2mr3802807vsb.1.1706003105097; Tue, 23 Jan 2024 01:45:05 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.45.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:45:04 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 09/10] PCI: remove legacy pcim_release() Date: Tue, 23 Jan 2024 10:43:06 +0100 Message-ID: <20240123094317.15958-10-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Thanks to preceding cleanup steps, pcim_release() is now not needed anymore and can be replaced by pcim_disable_device(), which is the exact counterpart to pcim_enable_device(). This permits removing further parts of the old devres API. Replace pcim_release() with pcim_disable_device(). Remove the now surplus function get_pci_dr(). Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 49 +++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 4314d0863282..f368181c6c92 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -463,48 +463,41 @@ int pcim_intx(struct pci_dev *pdev, int enable) return 0; } =20 -static void pcim_release(struct device *gendev, void *res) +static void pcim_disable_device(void *pdev_raw) { - struct pci_dev *dev =3D to_pci_dev(gendev); - - if (!dev->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; + struct pci_dev *pdev =3D pdev_raw; =20 - 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); + if (!pdev->pinned) + pci_disable_device(pdev); } =20 /** * pcim_enable_device - Managed pci_enable_device() * @pdev: PCI device to be initialized * - * Managed pci_enable_device(). + * Returns: 0 on success, negative error code on failure. + * + * Managed pci_enable_device(). Device will automatically be disabled on + * driver detach. */ int pcim_enable_device(struct pci_dev *pdev) { - struct pci_devres *dr; - int rc; + int ret; =20 - dr =3D get_pci_dr(pdev); - if (unlikely(!dr)) - return -ENOMEM; + ret =3D devm_add_action(&pdev->dev, pcim_disable_device, pdev); + if (ret !=3D 0) + return ret; =20 - rc =3D pci_enable_device(pdev); - if (!rc) - pdev->is_managed =3D 1; + /* + * We prefer removing the action in case of an error over + * devm_add_action_or_reset() because the later could theoretically be + * disturbed by users having pinned the device too soon. + */ + ret =3D pci_enable_device(pdev); + if (ret !=3D 0) + devm_remove_action(&pdev->dev, pcim_disable_device, pdev); =20 - return rc; + return ret; } EXPORT_SYMBOL(pcim_enable_device); =20 --=20 2.43.0 From nobody Wed Dec 24 21:32:40 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 642CA5FB87 for ; Tue, 23 Jan 2024 09:45:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003111; cv=none; b=bozpIfUG2h78qOLESJonJcqZxcbQ/KvvnKdjX31zNTol4+K7h25mvzefMOja7LenA3fQhq8iK7e31Db7BP28kGuX7a/dIz6JJRg2kJeCK9NvuSQ1QfwuQ8RNNqNS2JPx/9R+pSJm7U1qVq8nln3uaAQCwVn5BYw1mTkD5Gy7/bs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003111; c=relaxed/simple; bh=3Iz4WGGoq+pCd7AYX21h5ZoVleqGHZ1r8bx9B4kfr1Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PLVUYgDv8cKOoyIsA8Ef9hkErCa1PIvsVdO1hBwG7z6bUmlbEc/bk2EMEo4JEXGy0tAGig7I49hNP0sxhBzI/DxpwVaFhlhI/EMhp0mro7oQ8xslMH1aEuj8pHp9TF9dow5+/T0aMKFlmkWRAlOCbhVr0Es6RbA/XV7kQd4qgOU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=guwNTDq4; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="guwNTDq4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003109; 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=FWMdjy11RuBqd4YsQRKnY1KCsHHpGsFFOhcTWKEv36g=; b=guwNTDq4erfqWQH6KO9vEwoHtmuAE9zx99KWbcXwXGbPWZjd03JiiTyi+f9/OmWz7SMNfN AfmHRB7Wl+ZG+Fy663q4xkAquI2gMLyIMhiZ6WbTVe80ocxlxKmXfL8oa7SS3xL+qLpaWw VlI5W85c+oLAldMYy9npLBjwuZYfv9g= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-120-0_WjQzP_PyuqM4Vtd3gnTA-1; Tue, 23 Jan 2024 04:45:08 -0500 X-MC-Unique: 0_WjQzP_PyuqM4Vtd3gnTA-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-78130939196so131066085a.1 for ; Tue, 23 Jan 2024 01:45:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003107; x=1706607907; 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=FWMdjy11RuBqd4YsQRKnY1KCsHHpGsFFOhcTWKEv36g=; b=BzIsYUQIDXrqKf23wcHdMqpyvZ353hUXjv19YtXdPoq4nR4scP8kXUCNh1BECw2S7t hIQ7pIUqir0ixvK39HZI6CkyAu8aVHFe5yxMWucoYaa+nENhlqaavc8pla6DNUoFcs8J eYamB2ACdV/5LZuGBGTgU6jrC0n8AQIgocCI8qq003Q9efRTcuLt0uX1JZEYMI9bg+oF j9FhZJ+7vVBlV3nXevPlpOiFGqX9tL43Ml1gyvv8nFeI1nYLFaSnlgx2BUrIaTecpJCV uAdU7KX2S8Nq8MuqMzVqgAhbd6J20G6NqBtqLHKqvchqoxJvw3orA10LKTm6kcQxMior 5B5g== X-Gm-Message-State: AOJu0YxQCKQsp+B3XT6NmuxpTmnHRUdGvAfxyulywjrWPJJWB38yEpw6 kw66wY3ZL5ZnvTViD/Ib+KqX49THZbDEpNhsZlf99gEDkC1/Bh8rLKD4PY4urz005v6nB3JYb/y 4KG3Oa/AZ76VPRE2Ku9MvhFIBDlAtVexn0SSPQ7Tvq+QSeLnLZP6Up6Cz6cY6Tw== X-Received: by 2002:a05:6214:21c5:b0:686:9443:87b1 with SMTP id d5-20020a05621421c500b00686944387b1mr4377396qvh.2.1706003107747; Tue, 23 Jan 2024 01:45:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IFss4rceMk+iJ1mHCcS+hUWfR7kWRFY2O8zC9T9j0KLwTPnx3az+4pz7feAJyO8Yaas1sCjsw== X-Received: by 2002:a05:6214:21c5:b0:686:9443:87b1 with SMTP id d5-20020a05621421c500b00686944387b1mr4377391qvh.2.1706003107503; Tue, 23 Jan 2024 01:45:07 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.45.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:45:07 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org, stable@kernel.vger.org Subject: [PATCH v2 10/10] drm/vboxvideo: fix mapping leaks Date: Tue, 23 Jan 2024 10:43:07 +0100 Message-ID: <20240123094317.15958-11-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When the PCI devres API was introduced to this driver, it was wrongly assumed that initializing the device with pcim_enable_device() instead of pci_enable_device() will make all PCI functions managed. This is wrong and was caused by the quite confusing devres API for PCI in which some, but not all, functions become managed that way. The function pci_iomap_range() is never managed. Replace pci_iomap_range() with the actually managed function pcim_iomap_range(). Additionally, add a call to pcim_request_region() to ensure exclusive access to BAR 0. CC: # v5.10+ Fixes: 8558de401b5f ("drm/vboxvideo: use managed pci functions") Signed-off-by: Philipp Stanner --- drivers/gpu/drm/vboxvideo/vbox_main.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/vboxvideo/vbox_main.c b/drivers/gpu/drm/vboxvi= deo/vbox_main.c index 42c2d8a99509..7f686a0190e6 100644 --- a/drivers/gpu/drm/vboxvideo/vbox_main.c +++ b/drivers/gpu/drm/vboxvideo/vbox_main.c @@ -42,12 +42,11 @@ static int vbox_accel_init(struct vbox_private *vbox) /* Take a command buffer for each screen from the end of usable VRAM. */ vbox->available_vram_size -=3D vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE; =20 - vbox->vbva_buffers =3D pci_iomap_range(pdev, 0, - vbox->available_vram_size, - vbox->num_crtcs * - VBVA_MIN_BUFFER_SIZE); - if (!vbox->vbva_buffers) - return -ENOMEM; + vbox->vbva_buffers =3D pcim_iomap_range( + pdev, 0, vbox->available_vram_size, + vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE); + if (IS_ERR(vbox->vbva_buffers)) + return PTR_ERR(vbox->vbva_buffers); =20 for (i =3D 0; i < vbox->num_crtcs; ++i) { vbva_setup_buffer_context(&vbox->vbva_info[i], @@ -115,12 +114,15 @@ int vbox_hw_init(struct vbox_private *vbox) =20 DRM_INFO("VRAM %08x\n", vbox->full_vram_size); =20 + ret =3D pcim_request_region(pdev, 0, "vboxvideo"); + if (ret) + return ret; + /* Map guest-heap at end of vram */ - vbox->guest_heap =3D - pci_iomap_range(pdev, 0, GUEST_HEAP_OFFSET(vbox), - GUEST_HEAP_SIZE); - if (!vbox->guest_heap) - return -ENOMEM; + vbox->guest_heap =3D pcim_iomap_range(pdev, 0, + GUEST_HEAP_OFFSET(vbox), GUEST_HEAP_SIZE); + if (IS_ERR(vbox->guest_heap)) + return PTR_ERR(vbox->guest_heap); =20 /* Create guest-heap mem-pool use 2^4 =3D 16 byte chunks */ vbox->guest_pool =3D devm_gen_pool_create(vbox->ddev.dev, 4, -1, --=20 2.43.0