From patchwork Mon Apr 8 08:44:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620778 X-Patchwork-Delegate: bhelgaas@google.com 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 706B73A1A1 for ; Mon, 8 Apr 2024 08:44:34 +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=1712565876; cv=none; b=gqI0atEfVLlq+5kxJIWS3IuHK6gTOcGXYhCcR/B51zViW/kufoWtTTdxWKLUQ/0WiT2x1VUynpGVklugnMidfLc3Tx4urFPu/h7bOy7LS9ASAuNebUJ4NYnYFsdQGsM6Vpw4d5+5KlUOeiIpN8JMpsnd2gOaEM9jrnaPf/qfn6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565876; c=relaxed/simple; bh=P7w1cZjwJOZ0SZNqtX5fbffVmq3Xu/1tA26MvlSmdBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=thGmdXfj7w0luPj4yUMxi9BDQUozNlOcWJhdB3K9T4Q/2zE2uHWSAEK7IpR9DEE8Y7Q9R9yLEulwp9XevGPyYojttpBOwT30saZJgcJpmHMD1mO4QxI0IjBXASDo2N/ACn0oncdFCvydp2dzm0eWsUbGXtzio7nSkyANqP5XXOU= 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=W5k5q/g5; 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="W5k5q/g5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565873; 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=GGl/WiYyl03csxXvxZlcWXagee+4QnpV5olmqS0ZP5c=; b=W5k5q/g5pmbJ/z2hxxNNM7y2/cemiXDZ+596yOf7PPPcteaZWjJpv2iZAx8TqpJirfwZEn S/xiVIt/rg9+ysGgyTQ1vvTKg/YwQTkdpdeuS7HeFquT8PiLvyn7OzoHrYk1IZMXd0yiW7 3Kv1QB3Yc4Chp0t6rGH0yGpyFJXTcbs= 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-677-HOLbmY4TP166bC8t2IZc4g-1; Mon, 08 Apr 2024 04:44:31 -0400 X-MC-Unique: HOLbmY4TP166bC8t2IZc4g-1 Received: by mail-vs1-f71.google.com with SMTP id ada2fe7eead31-479d74a7adeso386895137.0 for ; Mon, 08 Apr 2024 01:44:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565871; x=1713170671; 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=GGl/WiYyl03csxXvxZlcWXagee+4QnpV5olmqS0ZP5c=; b=jF3CpJQ1PXZoNQzBljHHy1OehwjgXikIAZKgw5UMiLI25JqSNb+0DURNqbr3BnD78O USEiRs12aH2UjL11zVQ+gAseeIL0Pf/DZKIVTpT15AFTtg2vsdbWabUL20i3HfTtdG0P hQCc3bNetPxy3L5T4SO6+Uog2WALa0BevnNd7XWwNM3f+DyKCVoryEMR6fO1Z71kx1SW DbSHaMneXpNz44D/40WyBjktPxZ4Xee1SW84mFms/82Wl2lw9RIKyyXZDTpDcZidXgqn 8OoP98i8jmOtILYjY7DI301vPIKrs0l2X4BAT3Rkjo2kqZMEeRP8iie9zQiwiahkltGo aP1A== X-Forwarded-Encrypted: i=1; AJvYcCUv+5qlaoauX9a2KBhyEPoXIXfPYum2a1m22NXwVq7FJF29oRBQZ4O1tW8Qi+nCZs3Zcpr7zFVgIXEqf6SrW93NIbrKTq0cMYmZ X-Gm-Message-State: AOJu0YwVLsBlWka81yMRTTy4akh0sYcx2VJLhUC7DIKjPp85SfFBA7RI XsI5/gH1VQaWhqxeMUPDGnChhY4BfTOkIk/wTVfoX1HMw9pvlgSfUDJm5EvAKHKZYiRgay0EYW8 w67dB5XwYBdeRaiEumDGS4A4E8Z9z9GfzvVXb9VxZxU2nBHDCS9BFWDJv7w== X-Received: by 2002:a67:e94b:0:b0:478:927e:cad7 with SMTP id p11-20020a67e94b000000b00478927ecad7mr4722685vso.2.1712565870898; Mon, 08 Apr 2024 01:44:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7pg8a34r+F0NDlPtOCbWwZQhzYztTbSbF04flxCxq4Ua5vvn37CBZZ3pHG0IEf3vQ3CmUXg== X-Received: by 2002:a67:e94b:0:b0:478:927e:cad7 with SMTP id p11-20020a67e94b000000b00478927ecad7mr4722661vso.2.1712565870375; Mon, 08 Apr 2024 01:44:30 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:30 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 01/10] PCI: Add new set of devres functions Date: Mon, 8 Apr 2024 10:44:13 +0200 Message-ID: <20240408084423.6697-2-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 = 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. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 473 ++++++++++++++++++++++++++++++++++++++++++- include/linux/pci.h | 11 + 2 files changed, 479 insertions(+), 5 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 2c562b9eaf80..6a653e1a1acb 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -8,10 +8,215 @@ */ #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS +/* + * Legacy struct storing addresses to whole mapped BARs. + */ struct pcim_iomap_devres { void __iomem *table[PCIM_IOMAP_MAX]; }; +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 region + * requests, because those are very frequently requested and released together. + */ +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 = -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 here. + */ + +/** + * __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 + * @req_flags: flags for the request. For example for kernel-exclusive requests. + * + * Returns: 0 on success, a negative error code on failure. + * + * Request a ranged region within a device's PCI BAR. This function performs + * 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 req_flags) +{ + resource_size_t start = pci_resource_start(pdev, bar); + resource_size_t len = pci_resource_len(pdev, bar); + unsigned long dev_flags = pci_resource_flags(pdev, bar); + + if (start == 0 || len == 0) /* That's an unused BAR. */ + return 0; + if (len <= offset) + return -EINVAL; + + start += offset; + len -= offset; + + if (len > maxlen && maxlen != 0) + len = maxlen; + + if (dev_flags & IORESOURCE_IO) { + if (!request_region(start, len, name)) + return -EBUSY; + } else if (dev_flags & IORESOURCE_MEM) { + if (!__request_mem_region(start, len, name, req_flags)) + 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 = pci_resource_start(pdev, bar); + resource_size_t len = pci_resource_len(pdev, bar); + unsigned long flags = pci_resource_flags(pdev, bar); + + if (len <= offset || start == 0) + return; + + if (len == 0 || maxlen == 0) /* This an unused BAR. Do nothing. */ + return; + + start += offset; + len -= offset; + + if (len > maxlen) + len = 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 flags) +{ + unsigned long offset = 0; + unsigned long len = pci_resource_len(pdev, bar); + + return __pcim_request_region_range(pdev, bar, offset, len, name, flags); +} + +static void __pcim_release_region(struct pci_dev *pdev, int bar) +{ + unsigned long offset = 0; + unsigned long len = 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 = to_pci_dev(dev); + struct pcim_addr_devres *res = 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 *pdev) +{ + struct pcim_addr_devres *res; + + res = 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 = a_raw; + b = b_raw; + + if (a->type != b->type) + return 0; + + switch (a->type) { + case PCIM_ADDR_DEVRES_TYPE_REGION: + case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: + return a->bar == b->bar; + case PCIM_ADDR_DEVRES_TYPE_MAPPING: + return a->baseaddr == b->baseaddr; + case PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING: + return a->bar == b->bar && a->offset == b->offset && a->len == b->len; + default: + return 0; + } +} 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 = platform_get_resource(pdev, IORESOURCE_MEM, 0); * base = devm_pci_remap_cfg_resource(&pdev->dev, res); @@ -339,15 +544,80 @@ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) tbl[i] = NULL; return; } - WARN_ON(1); } EXPORT_SYMBOL(pcim_iounmap); +/** + * 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 = pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + res->type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res->bar = bar; + + ret = __pcim_request_region(pdev, bar, name, 0); + if (ret != 0) + goto err_region; + + res->baseaddr = pci_iomap(pdev, bar, 0); + if (!res->baseaddr) { + ret = -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 = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res_searched.bar = 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. */ @@ -400,7 +670,7 @@ EXPORT_SYMBOL(pcim_iomap_regions); * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones * @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. */ @@ -446,3 +716,196 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) } } 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 = pcim_addr_devres_alloc(pdev); + if (!res) + return -ENOMEM; + res->type = PCIM_ADDR_DEVRES_TYPE_REGION; + res->bar = bar; + + ret = __pcim_request_region(pdev, bar, name, request_flags); + if (ret != 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 = PCIM_ADDR_DEVRES_TYPE_REGION; + res_searched.bar = 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 @offset to + * @offset + @len. + * + * The mapping will automatically get unmapped on driver detach. If desired, + * 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 = pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + mapping = pci_iomap_range(pdev, bar, offset, len); + if (!mapping) { + pcim_addr_devres_free(res); + return IOMEM_ERR_PTR(-EINVAL); + } + + res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING; + res->baseaddr = 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 and + * 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 want to + * request the entire BAR. For most use-cases, combining pcim_request_region() + * 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 = pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + res->type = PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING; + res->bar = bar; + res->offset = offset; + res->len = len; + + ret = __pcim_request_region_range(pdev, bar, offset, len, name, 0); + if (ret != 0) + goto err_region; + + res->baseaddr = pci_iomap_range(pdev, bar, offset, len); + if (!res->baseaddr) { + ret = -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 this + * 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 = PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING; + res_searched.bar = bar; + res_searched.offset = offset; + res_searched.len = 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 16493426a04f..e7df30e620c7 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2323,10 +2323,21 @@ static inline void pci_fixup_device(enum pci_fixup_pass pass, void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen); 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_name); +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); extern int pci_pci_problems; #define PCIPCI_FAIL 1 /* No PCI PCI DMA */ From patchwork Mon Apr 8 08:44:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620779 X-Patchwork-Delegate: bhelgaas@google.com 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 99EFA3BB22 for ; Mon, 8 Apr 2024 08:44:36 +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=1712565878; cv=none; b=iL42OfIpZdC9+SUr2BMy8FNnyZqkB/uMgyU7SVMzYyqptNpJQQsRq2Zh5NZlLWszuebJgJTmjp+9C5Spb3gaupj4sgVx8ghFusZDeE/hBFyBsikdcpvNELQqkaGBuj2OBrN+CElSWBK0RfXnhymp4FFCfwarWRZeexgEzkm5YIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565878; c=relaxed/simple; bh=6+dLKksdqkW+Eb17IWwpxkpx+3WmgFuFvQXc2lWxflw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qWbDxTa91S314zKcmtA7tqDVm5ucqbf5kTYW3vqtt3EtNgUgIblJEbCQL6Qkb9wGLwLR9YdwK62c5Hz5HjZuBGjghlNZ0mTivjh5KRWSaR3HU2i6wUjv5Rbbznaxf8LEj2wPe5D6+bR8gHLyv3Zaubn1Bv+QFbfnPlkt86xLCuM= 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=KfH4THhp; 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="KfH4THhp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565875; 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=zDZi8OyO2ki9S6Zh60QzK63dKBZTZry9vjBIilxrucg=; b=KfH4THhpxFzBmlsevcN1sqUIeVdIgELpDc1CFw/stAVukrCblmhoL70B6ew3j0vMZsS8rR 4XahZgrjW7mr05NqZG1gY778XD9HkhcHzqBPGJlyK6qkEEOUdKQiTneRf8djlWYdEjaIvA 9SxV2WNYUl++3HeqRksPXZ0UVqFBUqs= 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-377-4PfzdHQPOz2u3fNtq2_liw-1; Mon, 08 Apr 2024 04:44:34 -0400 X-MC-Unique: 4PfzdHQPOz2u3fNtq2_liw-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-78a5e62931cso55267385a.0 for ; Mon, 08 Apr 2024 01:44:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565873; x=1713170673; 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=zDZi8OyO2ki9S6Zh60QzK63dKBZTZry9vjBIilxrucg=; b=Pc7OjodHnsxfqPBFddowO3AYloEGweBvVZcvT5JNcGtLddboiqvICJ43f25FKWhsxE AWJdE4/PuCRDgmyExW/5cVpV7+MdqBoisJij4mtaZ+yJXq+LQWkx9jK+13+EwLiw5h5u 9EnqzjDWSzZnI7j6f/bawmdACUzbNh7H28mYbM02tdkH0P/VdwtgRlmFVNpeB70V2MDO 1IYTrVU5RH7zFtQ/utJp/vCQV4VDCkTJlYF0QgPRbSquORW4uEPQ1DIXbCGjAZHB/zZU 8wvtIa9co+lfJ7uwGb/p3yorBsM0uqjMA7WLA0Bdm2hRp17/JEffGxK+dk2fHL71mZUJ iLPg== X-Forwarded-Encrypted: i=1; AJvYcCXcmwFXw1uHkLKJt/4UjijCnoeLQlLRhyIOYjzLlrPhiMKkEZUG4MqtFmnTK8JjuqRJ9NnsCVHaAzJDP9kmyK8zX33vfce8rVZX X-Gm-Message-State: AOJu0YzmBtUL3D4fjf5hJhwbY1yqW5ejCAuGfsfN1cLJ9k5NIm1E3ijO n9iEHdGehft3Kb87ybmz2i8xmkNyoAFBTO0AIuj7X7KrZp0l8lQsjX9DwD7Dx43vy0qYPOs/ko1 8itZ9mqMAlBMk7jd6IDBRK05w3u8IjN9udGn/7zphMTrO4onQveO6vtB5HA== X-Received: by 2002:a05:620a:1793:b0:78d:67a6:4f34 with SMTP id ay19-20020a05620a179300b0078d67a64f34mr1448789qkb.2.1712565873091; Mon, 08 Apr 2024 01:44:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWgL9knBfa1ppaRo/MWzGcZu9JjIrTt+jY6ko23oRvZvI1kI+Me4Evyvhh8oQZaO4qNZqApQ== X-Received: by 2002:a05:620a:1793:b0:78d:67a6:4f34 with SMTP id ay19-20020a05620a179300b0078d67a64f34mr1448774qkb.2.1712565872646; Mon, 08 Apr 2024 01:44:32 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:32 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 02/10] PCI: Deprecate iomap-table functions Date: Mon, 8 Apr 2024 10:44:14 +0200 Message-ID: <20240408084423.6697-3-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The old plural devres functions tie the PCI devres API to the iomap-table mechanism which unfortunately is not extensible. As the plural 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 | 372 +++++++++++++++++++++++++++++++++---------- drivers/pci/pci.c | 20 +++ drivers/pci/pci.h | 5 + include/linux/pci.h | 2 + 4 files changed, 316 insertions(+), 83 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 6a653e1a1acb..bd24bad187d9 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -4,15 +4,43 @@ #include "pci.h" /* - * 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 of 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 the old + * API. + * 2. The region-request-functions in pci.c do become managed IF the device 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 /* * 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]; }; enum pcim_addr_devres_type { @@ -373,6 +401,16 @@ static void pcim_release(struct device *gendev, void *res) struct pci_devres *this = 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 = 0; i < DEVICE_COUNT_RESOURCE; i++) if (this->region_mask & (1 << i)) pci_release_region(dev, i); @@ -459,19 +497,21 @@ EXPORT_SYMBOL(pcim_pin_device); static void pcim_iomap_release(struct device *gendev, void *res) { - struct pci_dev *dev = to_pci_dev(gendev); - struct pcim_iomap_devres *this = res; - int i; - - for (i = 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. + */ } /** - * 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 @@ -480,6 +520,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_* mapping + * functions. For example: + * void __iomem *mappy = pcim_iomap(pdev, barnr, length); */ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) { @@ -498,27 +543,114 @@ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_iomap_table); +/* + * 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 >= PCI_STD_NUM_BARS) + return -EINVAL; + + legacy_iomap_table = (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] = 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 = (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + if (legacy_iomap_table[bar] == addr) { + legacy_iomap_table[bar] = 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 >= PCI_STD_NUM_BARS) + return; + + legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + legacy_iomap_table[bar] = 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 backwards + * compatibility. */ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen) { - void __iomem **tbl; - - BUG_ON(bar >= PCIM_IOMAP_MAX); + void __iomem *mapping; + struct pcim_addr_devres *res; - tbl = (void __iomem **)pcim_iomap_table(pdev); - if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ + res = pcim_addr_devres_alloc(pdev); + if (!res) return NULL; + res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING; + + mapping = pci_iomap(pdev, bar, maxlen); + if (!mapping) + goto err_iomap; + res->baseaddr = mapping; + + if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0) + goto err_table; - tbl[bar] = 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); @@ -527,23 +659,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; - pci_iounmap(pdev, addr); + pcim_addr_devres_clear(&res_searched); + res_searched.type = PCIM_ADDR_DEVRES_TYPE_MAPPING; + res_searched.baseaddr = addr; - tbl = (void __iomem **)pcim_iomap_table(pdev); - BUG_ON(!tbl); + if (devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched) != 0) { + /* Doesn't exist. User passed nonsense. */ + return; + } - for (i = 0; i < PCIM_IOMAP_MAX; i++) - if (tbl[i] == addr) { - tbl[i] = NULL; - return; - } + pcim_remove_mapping_from_legacy_table(pdev, addr); } EXPORT_SYMBOL(pcim_iounmap); @@ -613,106 +746,179 @@ void pcim_iounmap_region(struct pci_dev *pdev, int bar) } EXPORT_SYMBOL(pcim_iounmap_region); +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; - iomap = pcim_iomap_table(pdev); - if (!iomap) - return -ENOMEM; + for (bar = 0; bar < DEVICE_COUNT_RESOURCE; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; - for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { - unsigned long len; + mapping = pcim_iomap_region(pdev, bar, name); + if (IS_ERR(mapping)) { + ret = PTR_ERR(mapping); + goto err; + } + ret = pcim_add_mapping_to_legacy_table(pdev, mapping, bar); + if (ret != 0) + goto err; + } - if (!(mask & (1 << i))) - continue; + return 0; - rc = -EINVAL; - len = pci_resource_len(pdev, i); - if (!len) - goto err_inval; +err: + while (--bar >= 0) { + pcim_iounmap_region(pdev, bar); + pcim_remove_bar_from_legacy_table(pdev, bar); + } - rc = pci_request_region(pdev, i, name); - if (rc) - goto err_inval; + return ret; +} +EXPORT_SYMBOL(pcim_iomap_regions); - rc = -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_region() + * 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 = 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 desired, + * 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 = 0; bar < PCI_STD_NUM_BARS; bar++) { + ret = pcim_request_region(pdev, bar, name); + if (ret != 0) + goto err; } return 0; - err_region: - pci_release_region(pdev, i); - err_inval: - while (--i >= 0) { - if (!(mask & (1 << i))) - continue; - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); - } +err: + pcim_release_all_regions(pdev); - return rc; + return ret; } -EXPORT_SYMBOL(pcim_iomap_regions); +EXPORT_SYMBOL(pcim_request_all_regions); /** - * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones + * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones (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 = ((1 << 6) - 1) & ~mask; - int rc; + short bar; + int ret; + void __iomem **legacy_iomap_table; + + ret = pcim_request_all_regions(pdev, name); + if (ret != 0) + return ret; - rc = pci_request_selected_regions(pdev, request_mask, name); - if (rc) - return rc; + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; + if (!pcim_iomap(pdev, bar, 0)) + goto err; + } - rc = pcim_iomap_regions(pdev, mask, name); - if (rc) - pci_release_selected_regions(pdev, request_mask); - return rc; + return 0; + +err: + /* + * If bar is larger than 0, then pcim_iomap() above has most likely + * failed because of -EINVAL. If it is equal 0, most likely the table + * couldn't be created, indicating -ENOMEM. + */ + ret = bar > 0 ? -EINVAL : -ENOMEM; + legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); + + while (--bar >= 0) + pcim_iounmap(pdev, legacy_iomap_table[bar]); + + pcim_release_all_regions(pdev); + + return ret; } EXPORT_SYMBOL(pcim_iomap_regions_request_all); /** - * 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 = pcim_iomap_table(pdev); - if (!iomap) - return; + short bar; - for (i = 0; i < PCIM_IOMAP_MAX; i++) { - if (!(mask & (1 << i))) + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) continue; - 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 e5f243dd4288..b5d21d8207d6 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3880,6 +3880,16 @@ void pci_release_region(struct pci_dev *pdev, int bar) 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 = find_pci_dr(pdev); if (dr) dr->region_mask &= ~(1 << bar); @@ -3924,6 +3934,16 @@ static int __pci_request_region(struct pci_dev *pdev, int bar, goto err_out; } + /* + * 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 = find_pci_dr(pdev); if (dr) dr->region_mask |= 1 << bar; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 17fed1846847..171884aba8e1 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -828,6 +828,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; }; diff --git a/include/linux/pci.h b/include/linux/pci.h index e7df30e620c7..5782ad034178 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2331,6 +2331,8 @@ int pcim_iomap_regions_request_all(struct pci_dev *pdev, 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_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); 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, From patchwork Mon Apr 8 08:44:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620780 X-Patchwork-Delegate: bhelgaas@google.com 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 1AA163D0A3 for ; Mon, 8 Apr 2024 08:44:37 +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=1712565879; cv=none; b=aRrss3FYFPN4KZ1Vuqk+ir2fFbyNaufPRjKHAd+K+0AJIjxYkTdbIrAN0DLwgQEoPsWmuf+Lyd5/a5BwvP9tvWHwVk5ocluDD1guzhO7IYTytZGeh6k0kt2BIKCDg475D9akvN+O45xIWxq8DUamWOu8yQDjTB8Jxd+epwzGSzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565879; c=relaxed/simple; bh=nBKWG5c5FhCBx7bhOlD5JxIgz1Oj/+3VoPRHxP5kZYs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NsRhmd0TALEbTZiYwhjPHHdI/YGwB9eaDuRva+cDrqgfOcxYkje/dPF7gyGF9x1WyFmMYujCNdmsYUdGWa7Qpdx1h/gKdHreJ5mu5aAOBTkr8lV744t2HWY2NSM67fEWSaqG0P0wJKaCoyRm1beUvhXtCfagOVLmEMKsocMoPTg= 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=HYRfPEna; 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="HYRfPEna" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565877; 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=cqd0XV6NVHg28KpejWCVVeMq/IoUZp7GtZTIzYnrVC8=; b=HYRfPEnaxcGRChN3kjaSjgqyhfo87RdxRqJZHrRqkp0YZ2iIjE92+NjYGERXxHwZym+WCm AsM51mk5t6ORM5RYyhpiPOnPPovkygv32b+mTfTvzEVevbne9ReSuv4yNCb4F6JkfGVbiY nfxwHs0SZnXmuER355J0saZ0hfH4Mgo= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-43-pouIxVsLMFOQkvYoCqWfnw-1; Mon, 08 Apr 2024 04:44:35 -0400 X-MC-Unique: pouIxVsLMFOQkvYoCqWfnw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-4349ba8bbfbso3798251cf.0 for ; Mon, 08 Apr 2024 01:44:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565875; x=1713170675; 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=cqd0XV6NVHg28KpejWCVVeMq/IoUZp7GtZTIzYnrVC8=; b=xUSnss3EhtIVE2kzLgrL2ZsXoN/Tho9cFxDoWfMK5wbg5tTv4rbAj5W3d/GHyharFj Yzzd2cKuqq3joivJrtbSss6W3U4N7e2kk+uqaQQ/KuG6fVQHBKKNOXs7DwAfDZrRH4s7 yC/V7YR6wRs9pfouA29vayHVw15fFlncifsdIFg/l8j1ETi3kPw8pKettMnEKdY4n5vY BH7qHk/WJy+SQ19u9PiN/NC1WInBblf9Kr7fDEjJU6tGCB8NDj6xyC+qOlurzBIj48LT OLCeXkQsHmThOl0/uZbiB7ih4O2rydJO889K6txdx6UmKjZzeD+V+d6v6vRn5Lw77shG nc3w== X-Forwarded-Encrypted: i=1; AJvYcCXMwXuZ0+bTvXJFUl0FKC17NnP5GrDj37YEJw/CxyHzK8N3FlYtvGUqGygfFg/kBFE6HPeIGn5XM2E2Vk2KqVqS1NbgtFgysFom X-Gm-Message-State: AOJu0YwHP5i3tBrwi8oDnrzrwbVkCY4JLKVRegrehKOKx5AxjI5HNGV1 YGBb6aqcCvtUEvlwQNZ4TvJAMZgX2IyLmJQX+rWAIj7JVBI2pGzKte+mYgO+0C6ge1gdyBXpyZQ +BUjnzBTrmIUBR2lTKcRqYxgIfs5872GkyVvAawOUn6oWpHP1fJZOp5xQmA== X-Received: by 2002:a05:620a:4590:b0:78a:1ee:5c0f with SMTP id bp16-20020a05620a459000b0078a01ee5c0fmr9642456qkb.6.1712565875266; Mon, 08 Apr 2024 01:44:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFR7midRoJI1FNPA+UU8XSvGoaEgXHEXcx1Kf1HaHyevfXZ+2+YLJhdeNw61JvfoOji+zi2BQ== X-Received: by 2002:a05:620a:4590:b0:78a:1ee:5c0f with SMTP id bp16-20020a05620a459000b0078a01ee5c0fmr9642439qkb.6.1712565874920; Mon, 08 Apr 2024 01:44:34 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:34 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 03/10] PCI: Warn users about complicated devres nature Date: Mon, 8 Apr 2024 10:44:15 +0200 Message-ID: <20240408084423.6697-4-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 c9725428e387..ea3b9842132a 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 maxlen) { diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index b5d21d8207d6..9d9d09534efe 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3903,6 +3903,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 @@ -3914,6 +3916,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) @@ -3962,6 +3971,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 @@ -3969,6 +3980,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) { @@ -3994,6 +4011,13 @@ void pci_release_selected_regions(struct pci_dev *pdev, int bars) } EXPORT_SYMBOL(pci_release_selected_regions); +/* + * 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) { @@ -4019,6 +4043,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) @@ -4027,6 +4059,20 @@ int pci_request_selected_regions(struct pci_dev *pdev, int bars, } EXPORT_SYMBOL(pci_request_selected_regions); +/** + * 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) { @@ -4044,7 +4090,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); @@ -4076,6 +4121,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. @@ -4085,6 +4132,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_name) { @@ -4416,6 +4469,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) { From patchwork Mon Apr 8 08:44:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620781 X-Patchwork-Delegate: bhelgaas@google.com 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 37ADB41AAB for ; Mon, 8 Apr 2024 08:44:40 +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=1712565882; cv=none; b=oSbs5pVoN5dXs0sqwCW93NIVYxTC8+b8qsKl/uzvaleAh0f9LURC6AUM62d1Ll9GOv61YbLJT9JIakTDb2C0s+rhcbrmEDH+UGkBzM+PV6ug/Eh0uytr+fihiZTsK3Y0oMcSUf65MKlEUK/59TyH4ZTLWWqdTjP+gQsvo7IcGHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565882; c=relaxed/simple; bh=Nx8ZzfGxcQC1AGrh9J9RS74k63XDfPWg20NLbbgS/GY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hwsgKjQYGJNboxfhM0F8GKu3XY0+bT5/kf0EMccoMnxWmoI+TB4zonbgzRXMYJ8471Bplb4KGkCo3m+z4z5tMkf6Emf2vslfJyzqgUcqxMRu3OUetSbAdclwMvuGuZtm4n6LrYF20U34qBwMGch6g77s3xGUUwH0ReyOq7HBrg0= 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=e/dn2gSG; 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="e/dn2gSG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565879; 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=GIYPXBXCIxl+X8+VfSMdqKBrLQLRX+Ac31hhra0G7UY=; b=e/dn2gSGtj2tbRqHXXD/cDh4thPS4aG3vxKZj3bCAoZ0ZjFbPHmxIohc1TLKL+btg9TGHx 0LlTDyCWZ30Mp1hqL1gKMi9hkhefsM5sDYX2VRVzASWUYC3fuc0oyCunOoNUUD6ZGlR0LC tNpret8Z6KwlNoxJqh6tC5CUS5kcQpQ= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-503-xVfOuCZbO3GGBeWl2K8LUg-1; Mon, 08 Apr 2024 04:44:38 -0400 X-MC-Unique: xVfOuCZbO3GGBeWl2K8LUg-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-78d5267ae96so8516685a.0 for ; Mon, 08 Apr 2024 01:44:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565877; x=1713170677; 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=GIYPXBXCIxl+X8+VfSMdqKBrLQLRX+Ac31hhra0G7UY=; b=ey8hiX6sYwaIJCK26+cOjL0pQu4ZsrNOL3LmIoFm7p03IXXE+6IhpFYcI/QPGNRXQJ 3gL3In/EZ8Zm907dWDj2/nODsMgQkoo/U4UgI+hrG8X9nNjLh5qkBAwWDD7SYTtZatC4 ev7lUrPy1Z1goRo694p8VJxPf3MvmSO/ylw/J/K8G6kH0zZWgRqPyjwctDxoZMR/pfFk Z8PsT3Dufuvlgg4umLNrDMU+mbs0rIKiqd8ZX4wefRoIwxtNnTZDlZ3pVwiuWsFrLEld d98OjVc7NUo0W/loz7Qv+ROCxgC4lHE6AbDlmfe+F33xx4YzbYE6fnFlOWcPnJREqtNj kyWg== X-Forwarded-Encrypted: i=1; AJvYcCWJWQucv9IQNR8B17f4O4/x56I2hIQJF0Ajn8znPq1OloQGp0TG1JINHls8yUlshXDHn81+kIqCzk1NJNi0JkmmTZP/iGmsPW0Z X-Gm-Message-State: AOJu0YxDJXR3Riyy3uTlCu+I3RkqcxvXxxcTBMcJBu4U/6WevDXsSt/U CcqO7QRrBih8YQVGlyqu2dT4er58AmmAEXrLMBc/F4OHNDj8yMKb5nhBT4cBhqKwxoTKcJqG2mi 127PGlD5E7SzYMms7kpRgNcUu4DkEOFWijoHaaMhXBdxcgZloqplPNcemmkM2X3ZUVA== X-Received: by 2002:a05:620a:4093:b0:78d:641d:3db2 with SMTP id f19-20020a05620a409300b0078d641d3db2mr3325711qko.2.1712565877366; Mon, 08 Apr 2024 01:44:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGM+65Ce5bZ9ywcqEtAlGoPOBNhPm/or5dJ5w/u7NYnrVxPZH6uzzTULD/Tr3RGSLPS//Ek5w== X-Received: by 2002:a05:620a:4093:b0:78d:641d:3db2 with SMTP id f19-20020a05620a409300b0078d641d3db2mr3325696qko.2.1712565877008; Mon, 08 Apr 2024 01:44:37 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:36 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 04/10] PCI: Make devres region requests consistent Date: Mon, 8 Apr 2024 10:44:16 +0200 Message-ID: <20240408084423.6697-5-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 bd24bad187d9..d9cd7f97c38c 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 using + * 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. */ /* @@ -399,21 +396,6 @@ static void pcim_release(struct device *gendev, void *res) { struct pci_dev *dev = to_pci_dev(gendev); struct pci_devres *this = 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 = 0; i < DEVICE_COUNT_RESOURCE; i++) - if (this->region_mask & (1 << i)) - pci_release_region(dev, i); if (this->mwi) pci_clear_mwi(dev); @@ -964,6 +946,25 @@ int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) } EXPORT_SYMBOL(pcim_request_region); +/** + * 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 char *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 9d9d09534efe..c0c1ee17a06b 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3869,7 +3869,15 @@ EXPORT_SYMBOL(pci_enable_atomic_ops_to_root); */ 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; + } if (pci_resource_len(pdev, bar) == 0) return; @@ -3879,20 +3887,6 @@ void pci_release_region(struct pci_dev *pdev, int bar) 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 = find_pci_dr(pdev); - if (dr) - dr->region_mask &= ~(1 << bar); } EXPORT_SYMBOL(pci_release_region); @@ -3920,14 +3914,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 == IORESOURCE_EXCLUSIVE) + return pcim_request_region_exclusive(pdev, bar, res_name); + + return pcim_request_region(pdev, bar, res_name); + } if (pci_resource_len(pdev, bar) == 0) return 0; @@ -3943,20 +3941,6 @@ static int __pci_request_region(struct pci_dev *pdev, int bar, goto err_out; } - /* - * 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 = find_pci_dr(pdev); - if (dr) - dr->region_mask |= 1 << bar; - return 0; err_out: diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 171884aba8e1..040ed2825554 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -828,12 +828,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; }; struct pci_devres *find_pci_dr(struct pci_dev *pdev); diff --git a/include/linux/pci.h b/include/linux/pci.h index 5782ad034178..0f203338f820 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2330,6 +2330,7 @@ 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_name); +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *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); From patchwork Mon Apr 8 08:44:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620782 X-Patchwork-Delegate: bhelgaas@google.com 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 0E3CE45951 for ; Mon, 8 Apr 2024 08:44:41 +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=1712565883; cv=none; b=lpoOx563lOEqyYyOWim9+6Rn9R2tlhNIl8PgJao1r2dVBUaOd2rvabdFZkKQKVW9yjXD5w+dlIwmfdWTgnwJ5pQwysTFQw1jyuMo58M4VuZ1XUgNg1W9NArMU0r0myE6yCF5getWf3MvjzWJyMoe1sOFtOyDHbq6jr4H9tLmpkI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565883; c=relaxed/simple; bh=ph/4KcAsTi43ubN861sbaEt6JoUBoxu5rEFqsUZSaCk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Cwn6A2L7xJ9SnLV+Qz7JNZt6kgDdm06BuNbc/8S2CEUAePJgqIykkIYQ9J7oSm+KDL+YTKKPdJzxEfMrbCD5jFzv3Y6t0Nnkkdh2n/5m1a1udf72wRzjvRSbCRmb/vwglFwtSDDu6QgWSQ8fIIV9qgouWHyoJu5K0NXwxVjgQnw= 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=ZwocmTi5; 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="ZwocmTi5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565881; 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=HBYxescmGlqJZbkhxXkrYbSUutHFKaQyU2GxTiA6gSQ=; b=ZwocmTi59N1GRS+VdlBAinkZ0cAKVKFapAVvO1EHpTgxFkUeMqYJTodeTNpOcimPgvggvw CFe5ep56MsaZCu4L98Hi8/I0nnfFFI5VPVv+FMPi2kX3qh3JVoAbc84oCbVt84I1RgFrM2 dkwmnSe/cZFqptM5/XKgfnclwY2LaUE= 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-191-Ec92vJ9oOV69hBK9KNYATg-1; Mon, 08 Apr 2024 04:44:39 -0400 X-MC-Unique: Ec92vJ9oOV69hBK9KNYATg-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-78d346eeb02so182035285a.1 for ; Mon, 08 Apr 2024 01:44:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565879; x=1713170679; 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=HBYxescmGlqJZbkhxXkrYbSUutHFKaQyU2GxTiA6gSQ=; b=pYyv7UmVZ8oiB8cQjwJreUYmW8qTVsbnn6bacP5iQ85TVfCAwI0Hs8jiMxVMtUKJAL lXp2OzpKUvsImIzO/flbTNfiBMf0iXBIU4BxqyJPFZ0NqAywR96CcCb00cAGaa6WXZLL ozWgRBNpZSGueMv2s0JDCTutieC31MGhi0kIrh1X2PvoFw0dS2lkW7Zw7PE0e/0JHlzu rLiU2Vbji6YQT4No+NektC18UYGEklJln5tuAAiQSigs1P8hv6WS7Db0i9lK7UQNLvyQ x7A4pMSA+LaTPZ+qx9mTn3Hs09zSt48Vdzak0XHYZfYlSzq+R8QQxrFCqF7qaVe5qZlq SdSQ== X-Forwarded-Encrypted: i=1; AJvYcCV7nnY+fjPguIS3T7E/haxeFZL7xe54PqFRaAg5oWati+UQfeTzfR1fmdLS1CrWR7qJPxBqHFdS9if44DzoDMXqlX7wPMoZJKFo X-Gm-Message-State: AOJu0Ywo3Bty9UDuzKmjZ/4y1PH2XZYHmVoLkHFpiEeGl/Ukj4eL3FWC yd8kB2FjlP8edTzMmY63aN3GeaJ02YU51OTpNQWM/z4cSpZbJf8Qflt9gZ3nGkkJMpPQcsJHZ/3 CTRpr43VZSHpWsSLUsQ3nJUWgwmRtBYk6jJeJcEcVIEycglqSA4OhuEc7EA== X-Received: by 2002:a05:620a:4014:b0:78b:c3a2:26ce with SMTP id h20-20020a05620a401400b0078bc3a226cemr9387796qko.7.1712565879329; Mon, 08 Apr 2024 01:44:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFCnsmOGRAE/Be/m4mXdpGAm4C6IVXQDRK7QdJQ/6V7/eI39vG2eQRZ/l8WvRwG9UKSoTuiAQ== X-Received: by 2002:a05:620a:4014:b0:78b:c3a2:26ce with SMTP id h20-20020a05620a401400b0078bc3a226cemr9387787qko.7.1712565879044; Mon, 08 Apr 2024 01:44:39 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:38 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 05/10] PCI: Move dev-enabled status bit to struct pci_dev Date: Mon, 8 Apr 2024 10:44:17 +0200 Message-ID: <20240408084423.6697-6-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 d9cd7f97c38c..623e27aea2b1 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *res) if (this->restore_intx) pci_intx(dev, this->orig_intx); - if (this->enabled && !this->pinned) + if (!this->pinned) pci_disable_device(dev); } @@ -446,14 +446,11 @@ int pcim_enable_device(struct pci_dev *pdev) dr = get_pci_dr(pdev); if (unlikely(!dr)) return -ENOMEM; - if (dr->enabled) - return 0; rc = pci_enable_device(pdev); - if (!rc) { + if (!rc) pdev->is_managed = 1; - dr->enabled = 1; - } + return rc; } EXPORT_SYMBOL(pcim_enable_device); @@ -471,7 +468,7 @@ void pcim_pin_device(struct pci_dev *pdev) struct pci_devres *dr; dr = find_pci_dr(pdev); - WARN_ON(!dr || !dr->enabled); + WARN_ON(!dr || !pdev->enabled); if (dr) dr->pinned = 1; } diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index c0c1ee17a06b..9f1419bac9b9 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2011,6 +2011,9 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) u16 cmd; u8 pin; + if (dev->enabled) + return 0; + err = pci_set_power_state(dev, PCI_D0); if (err < 0 && err != -EIO) return err; @@ -2025,7 +2028,7 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) pci_fixup_device(pci_fixup_enable, dev); if (dev->msi_enabled || dev->msix_enabled) - return 0; + goto success_out; pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin); if (pin) { @@ -2035,6 +2038,8 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) cmd & ~PCI_COMMAND_INTX_DISABLE); } +success_out: + dev->enabled = true; return 0; } @@ -2193,6 +2198,9 @@ static void do_pci_disable_device(struct pci_dev *dev) { u16 pci_command; + if (!dev->enabled) + return; + pci_read_config_word(dev, PCI_COMMAND, &pci_command); if (pci_command & PCI_COMMAND_MASTER) { pci_command &= ~PCI_COMMAND_MASTER; @@ -2200,6 +2208,7 @@ static void do_pci_disable_device(struct pci_dev *dev) } pcibios_disable_device(dev); + dev->enabled = false; } /** @@ -2227,12 +2236,6 @@ void pci_disable_enabled_device(struct pci_dev *dev) */ void pci_disable_device(struct pci_dev *dev) { - struct pci_devres *dr; - - dr = find_pci_dr(dev); - if (dr) - dr->enabled = 0; - dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0, "disabling already-disabled device"); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 040ed2825554..2b6c0df133bf 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -823,7 +823,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) * then remove them from here. */ 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 0f203338f820..95cdd1bc73c4 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 */ From patchwork Mon Apr 8 08:44:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620786 X-Patchwork-Delegate: bhelgaas@google.com 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 DD2D64F1FA for ; Mon, 8 Apr 2024 08:44:53 +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=1712565895; cv=none; b=X/2itHIYIeoqkMDKfj3iBECg/nknBYudXBRdRRFr8f0ZDjw5YrOXP1rj5SaM1KWny0Sq0dmRnxkM94MecBXXOexjV9kh1aUS5hSCAOuJcs+Za5++Od4ptQbhHHApWujBEhGaZgqikUEX0dlOoUvx9fH32YfJGNe6oOw52Dws6bE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565895; c=relaxed/simple; bh=S0oq+FGKUVk50tt2PkFQpXV/fGnFNIfPoPpC31bSxTo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WLcSe6AkJEreR1RvVsq+jK9j+I+SOZDLANu+zFs4Ydxk9usu06Fj/TPHje4YYUHheiL0olOCgyDZKH3fgseoocQF6PT3MrMWHXnVklsJAWp75kBT45Ci4s/1FJlDPwWFAbRW1XevwyzcOIrtk2pJjlX/Sp1Psvj+zYqRWKdTcZ4= 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=KEYxa5JR; 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="KEYxa5JR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565893; 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=cfte4b3wPijsP9MEYyJuLZ+mSeCcAqIklzESx/bINtI=; b=KEYxa5JRbM02qH5LEroDm8lkKDCHLzdKUrmWMYQYhKwEsKXjzHg4iOp4GTqnIvhq8OOe4q NO1nUk0hQ3e7+vahTLBgQG5Z/TuMT38SEmUXADsEFeKvURRMrTL/lES06pLIpM3FYJDWsV Klt+Zaxw++pyb/eJExSHA0PUk3jSLNc= 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-480-vNkX68ImMe2L8_v8WBnAlw-1; Mon, 08 Apr 2024 04:44:42 -0400 X-MC-Unique: vNkX68ImMe2L8_v8WBnAlw-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-78d5267ae96so8517985a.0 for ; Mon, 08 Apr 2024 01:44:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565882; x=1713170682; 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=cfte4b3wPijsP9MEYyJuLZ+mSeCcAqIklzESx/bINtI=; b=oBclNSZd7aLJYQhcH/o5T/kj4hBbCxTZaHsvIzHrXAR7cHYEiolsQgNzsVS8w0vYLR 4PmkCxnb7bk79LP+IG89gnvfwy4/Yo4+vZNfihxs+7OplvAINsWjQvQPvoP9DYEwHha3 QayqkzElT5H2sCMDhxQZVqEwncx7jnxAa9bmqfioAbjz74zGsCqLekoIyPvpMfjviN7f glHJhOA/M33EY8coeT21kRmHZ6LXYjQBhEgmg+9A09H5eAvWnI6K+gQsaxuQPw8V2w3u 4oOMZgGk1gukiqY8QlTkcmD+dW3b6UA5bFeQcOxIt0cBgAjMYUGNf3Khi1jR6nwvZlWd 7hYA== X-Forwarded-Encrypted: i=1; AJvYcCVxyABAFde84WV6DnEXTTwS3BWrRFjZzKJzf5SwuazjJ/V6TBFB0+h9Ze7vTlVtBvhvp6yAC7yCwb3/sPxUkoCLCK9+vxCbKCRZ X-Gm-Message-State: AOJu0YyNe5wD6VBGai7xgX9YjRhEXznP2Ub1VN8HACV2bXWm7pGfrJ+0 dOiP9KwE48PmwcgwVoYQvWjiddea4O30WkQes79vzCDgQWda0bVAFtnXma0CZjsQo/SuQK+6H2b dhakxuhTL0N+OnRwaC/7+bFbnAy9hhIOPtYK60MeDuPEG7T9O0M5y3Q0Akg== X-Received: by 2002:a05:620a:450e:b0:78b:ea64:e0fa with SMTP id t14-20020a05620a450e00b0078bea64e0famr9523800qkp.4.1712565881730; Mon, 08 Apr 2024 01:44:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF3mwwm/R12WcmH2kE9iBLa1vhhd3RLItPquJFca7C9FUCjE4MBkJwpaP0CUP/2AT65JzFDBQ== X-Received: by 2002:a05:620a:450e:b0:78b:ea64:e0fa with SMTP id t14-20020a05620a450e00b0078bea64e0famr9523787qkp.4.1712565881410; Mon, 08 Apr 2024 01:44:41 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:41 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 06/10] PCI: Move pinned status bit to struct pci_dev Date: Mon, 8 Apr 2024 10:44:18 +0200 Message-ID: <20240408084423.6697-7-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Restructure bits in struct pci_dev so the pm / pme fields are next to each other. 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 623e27aea2b1..fb9e4ab6bcfe 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *res) if (this->restore_intx) pci_intx(dev, this->orig_intx); - if (!this->pinned) + if (!dev->pinned) pci_disable_device(dev); } @@ -459,18 +459,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 = find_pci_dr(pdev); - WARN_ON(!dr || !pdev->enabled); - if (dr) - dr->pinned = 1; + pdev->pinned = true; } EXPORT_SYMBOL(pcim_pin_device); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 2b6c0df133bf..a080efd69e85 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -823,7 +823,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) * then remove them from here. */ 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 95cdd1bc73c4..9d85d2181083 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 */ From patchwork Mon Apr 8 08:44:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620783 X-Patchwork-Delegate: bhelgaas@google.com 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 716564C631 for ; Mon, 8 Apr 2024 08:44:46 +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=1712565887; cv=none; b=Yb75o7/P77i4r9zHtsFR2aQw9ZC5cr3ssmcTpUPuyLOqPwHtapksflfGGV7gASl4pMZWNie8gUQEzGFFtVPRuBORG5aeIB9IF0WKdbxchad0QL3stJh8Zjl0kjW/zeoYJwbm8ebzhOhR7t9ZgRthZhbof+27hjWZ0j+bUN65KoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565887; c=relaxed/simple; bh=jtMfRJzrGj6Hya59/CapvknY9OFyyO9+qwkwlnNChyU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EfFPZ+a4Jb/dHhhzbIToEB1RkkLFS0kN61l5OSn0v6OTJFjpQr7DH6Oe6YDYQMO1fdNNNT0/OQDa6/n55XFu7W1RvR81yVmFxR9b9BKg5BpmzJvU3H/3N1CC+tQkGEdaeszuwqxm+eZRxCO4yLaSesOQFMKQmhW6U6KmPHF825s= 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=YVN6U0Gg; 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="YVN6U0Gg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565885; 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=FQbNzzWRGbAMJ/ZvXVtvXB/7D5Q8r5XT3mcCvFUlfNA=; b=YVN6U0Ggsr2srExWLrgjbzZxG0LHofCGu2j8R7Mni2x0bGFIdE/DWJBUBL8Tu3zfw6f1rT 8/7PmClJS7JvsJicOKZ/+U1tnGHAsRJsQXsiJDJvg9+jvnd0t25ksN3kNWSP4fqbgUu+yC ui4y+jRIn7CUNW+nbUN7wXkYrCClKGs= 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-481-LE8G0EB5MSCm-kY9RCLplA-1; Mon, 08 Apr 2024 04:44:44 -0400 X-MC-Unique: LE8G0EB5MSCm-kY9RCLplA-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-78d670996e4so2636485a.1 for ; Mon, 08 Apr 2024 01:44:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565884; x=1713170684; 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=FQbNzzWRGbAMJ/ZvXVtvXB/7D5Q8r5XT3mcCvFUlfNA=; b=nYdwLx7aznHG1zXsEK9IpAbdip0vKSxjfjMIBstWzmolb3u1xAJ4ZlqUsi3DQZZNkC g4rgPppiKvIbROya+YWxyyfNEAukDWUxQgOA83PA5nFaSTC0vLUVmF6CbSIJn3bn7RAc vIwhhemakTjeWNo2swYwRSOoKIuV/vbbUMroHgLxa2M8GJcqNoWZnbEPexKUxc4HbQac NZQocAdJMuBjcaoc4LV9aOVSZopJniPDHHKbMmYXoOSwwlhFqEMuSuaL7aJFdhueK1XP 4NzJGViEVXacznwWZAS2W/2xnFaEZtI4gYSBPr0CU645i/wzhdpDT0USu9i1Z1ColhBn wR2w== X-Forwarded-Encrypted: i=1; AJvYcCXgZeuHAGXz3kZX/qQd90AxfpVXuvZyKpEiXBAe7HELPfWOtpQNHHO9s3+JUHbpPD7O+IVnjVgqm9FYzF884/EJypeuCdl+iunM X-Gm-Message-State: AOJu0YzlkhCc/PrfSFW9+n/4x9aq2f8GGZkLkfWkaRBkNx02qjsmwoZc HnBkqoZE0ZogMdAdOqE/ArA3UhjRgyvqf8iAVbiNik4xy6EOpuFMIQrsCUHAL2usrVNPz3+3Lbo H1c1LXlN9yrOOUqIFqsJjHYAsrv/MytkKmHdf6Gr7rG6mYvpr/0VpcEK7tA== X-Received: by 2002:a05:620a:19a9:b0:78d:6439:127d with SMTP id bm41-20020a05620a19a900b0078d6439127dmr3282075qkb.0.1712565883918; Mon, 08 Apr 2024 01:44:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH0qa1JgWscwtLKGJLtJEsFGCHRLEHSbkg6kELrSv9oOSNF02qqh4zCKM10yPLZo0wFlJ4eGw== X-Received: by 2002:a05:620a:19a9:b0:78d:6439:127d with SMTP id bm41-20020a05620a19a900b0078d6439127dmr3282068qkb.0.1712565883582; Mon, 08 Apr 2024 01:44:43 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:43 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 07/10] PCI: Give pcim_set_mwi() its own devres callback Date: Mon, 8 Apr 2024 10:44:19 +0200 Message-ID: <20240408084423.6697-8-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 fb9e4ab6bcfe..e257c212cd9c 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -370,24 +370,34 @@ void __iomem *devm_pci_remap_cfg_resource(struct device *dev, } EXPORT_SYMBOL(devm_pci_remap_cfg_resource); +static void __pcim_clear_mwi(void *pdev_raw) +{ + struct pci_dev *pdev = 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 success. */ -int pcim_set_mwi(struct pci_dev *dev) +int pcim_set_mwi(struct pci_dev *pdev) { - struct pci_devres *dr; + int ret; - dr = find_pci_dr(dev); - if (!dr) - return -ENOMEM; + ret = devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev); + if (ret != 0) + return ret; + + ret = pci_set_mwi(pdev); + if (ret != 0) + devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev); - dr->mwi = 1; - return pci_set_mwi(dev); + return ret; } EXPORT_SYMBOL(pcim_set_mwi); @@ -397,9 +407,6 @@ static void pcim_release(struct device *gendev, void *res) struct pci_dev *dev = to_pci_dev(gendev); struct pci_devres *this = res; - if (this->mwi) - pci_clear_mwi(dev); - if (this->restore_intx) pci_intx(dev, this->orig_intx); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index a080efd69e85..c98de280b16e 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -825,7 +825,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; - unsigned int mwi:1; }; struct pci_devres *find_pci_dr(struct pci_dev *pdev); From patchwork Mon Apr 8 08:44:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620784 X-Patchwork-Delegate: bhelgaas@google.com 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 6658B4DA00 for ; Mon, 8 Apr 2024 08:44:49 +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=1712565891; cv=none; b=bprZOHr2rasEEizLr/2HZ2kvtUmuyeJqBq4l58KwDW1mwF2HiEvxoOyzmIqbdGcI6hX6qivgmUCyD5tmmkdOXdGrM1Z4JStiYzf8BRH+OsZ5YpdflT7+mvfnW8a5KtDwI0gBkqd7Fa0VxKHA60CWsXcXzAgxPTMgoXkP95l97jk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565891; c=relaxed/simple; bh=v7qaiBza5CJuo0crIKbY9+K6HZs5sPWVrRaHfMbRhT4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZIWqFCn5WPnIu2dQ77HAJgDriQ5XlbMxY52mFKGBYEIRFaEEooZTBWxewe+1zAu6D2zBcIOZ49xreFO8UPoXYjyRC2PL05pq2BXnX+vs5YMSktZr/j7lcUun8kXnUmXcJmQxBdkRS3SrmXRCU7AMF7miVtxLZtNd8A6ctlRPnic= 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=R8LXcaH5; 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="R8LXcaH5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565888; 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=1JY3/LNjqLYiipt/EHsApjCI6ktJry9Gfg2HWZFwX/g=; b=R8LXcaH5bf03KON/eKo9cbvucKf36564aVCXVCPaFbBpd6dnSi1xSJBPwkfoUFhow9ZFu2 S81C3RKFKHg3EA+Mq8d5S9oWsnxezGE3MDVAYlKc1Y+Sts42QmOPPjp5oquJRqCaF92neH xv4yWl3y3YHz+WVx4cwc0QljmH/MWqs= 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-544-yjERBcpWMC2Widw4ydbNYQ-1; Mon, 08 Apr 2024 04:44:47 -0400 X-MC-Unique: yjERBcpWMC2Widw4ydbNYQ-1 Received: by mail-vs1-f72.google.com with SMTP id ada2fe7eead31-479d74a7adeso386939137.0 for ; Mon, 08 Apr 2024 01:44:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565886; x=1713170686; 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=1JY3/LNjqLYiipt/EHsApjCI6ktJry9Gfg2HWZFwX/g=; b=ZnwVANHFaH4+f1+x+kQr+mTDRige6bWWSjE5G+L/sXH9URptTDy/tnFh/kekBn7BVV mTM+dzZrufx75kjXydOeHo94Rda9gI6MZ2rjOTmMhIIQ0WcHGGpRb03FcxrantybQFu/ +d/9HbZICkeuYmopJs4ZM0AYJhbK1pdTse+Hu/WtUbttW6zTSGzAilJNOPNZKdaYhfw/ YuhhR2YDjM9pyPYebAvgnVubVHC/0X+DiUZTi4PdlvZGB6XWfCL2yAcjtF0QSafZVwiv 0MlZ2bsNv/3Svgw5AKEkslwVyIPz6+sDg88jcEoYvTyUpWuBqW6xqutwGquZPIcu9CYa 5srA== X-Forwarded-Encrypted: i=1; AJvYcCVcZAbWM8xp28AaFLbQMKgxe9z5FYd3TCClTkZrLiAJ/kc8t9GqVwukrXW32qosMCOd3S9VTwxnRmDDF+CcpPOG7YFxv3ntlXzI X-Gm-Message-State: AOJu0YzQ0S51jTEyEcduOyCM17nMYFjPkIlb4Zf+mYrtkN5EzJKKF085 odfpr9ZHbST8DFNxmT/kqQ9HxubgdrAJig2qPtxF5/WfSz2TYeBR7k0Bpb/GM87lCXchdw8kDlo AzotzoqgI/AGMahYtylsJT26Gboo5Uqs7EzFYYYunjZ8Sp7zM7CmYOhLZDw== X-Received: by 2002:a05:6102:515:b0:479:de3a:d2cf with SMTP id l21-20020a056102051500b00479de3ad2cfmr4448524vsa.1.1712565886529; Mon, 08 Apr 2024 01:44:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGg2PNIk47UlCuBAj7CVpQLIdCLibVZ8sJoQWwna7z6edafKk216GeqWdnLp6oZ0KT5EWHqdQ== X-Received: by 2002:a05:6102:515:b0:479:de3a:d2cf with SMTP id l21-20020a056102051500b00479de3ad2cfmr4448501vsa.1.1712565885919; Mon, 08 Apr 2024 01:44:45 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:45 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 08/10] PCI: Give pci(m)_intx its own devres callback Date: Mon, 8 Apr 2024 10:44:20 +0200 Message-ID: <20240408084423.6697-9-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 | 74 +++++++++++++++++++++++++++++++++++--------- drivers/pci/pci.c | 24 +++++++------- drivers/pci/pci.h | 17 +--------- 3 files changed, 74 insertions(+), 41 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index e257c212cd9c..b81bbb9abe51 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]; }; +/* 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,28 +406,69 @@ int pcim_set_mwi(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_set_mwi); +static void pcim_intx_restore(struct device *dev, void *data) +{ + struct pci_dev *pdev = to_pci_dev(dev); + struct pcim_intx_devres *res = data; -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 *dev) { - struct pci_dev *dev = to_pci_dev(gendev); - struct pci_devres *this = res; + struct pcim_intx_devres *res; - if (this->restore_intx) - pci_intx(dev, this->orig_intx); + res = devres_find(dev, pcim_intx_restore, NULL, NULL); + if (res) + return res; - if (!dev->pinned) - pci_disable_device(dev); + res = devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL); + if (res) + devres_add(dev, res); + + return res; } -/* - * TODO: After the last four callers in pci.c are ported, find_pci_dr() - * needs 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 = get_or_create_intx_devres(&pdev->dev); + if (!res) + return -ENOMEM; + + res->orig_intx = !enable; + + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); + + if (enable) + new = pci_command & ~PCI_COMMAND_INTX_DISABLE; + else + new = pci_command | PCI_COMMAND_INTX_DISABLE; + + if (new != 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 = to_pci_dev(gendev); + + if (!dev->pinned) + pci_disable_device(dev); } static struct pci_devres *get_pci_dr(struct pci_dev *pdev) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 9f1419bac9b9..f59dbad69452 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3873,7 +3873,7 @@ EXPORT_SYMBOL(pci_enable_atomic_ops_to_root); 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(). */ @@ -4461,11 +4461,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; + /* + * 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) != 0); + return; + } + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); if (enable) @@ -4473,17 +4484,8 @@ void pci_intx(struct pci_dev *pdev, int enable) else new = pci_command | PCI_COMMAND_INTX_DISABLE; - if (new != pci_command) { - struct pci_devres *dr; - + if (new != pci_command) pci_write_config_word(pdev, PCI_COMMAND, new); - - dr = find_pci_dr(pdev); - if (dr && !dr->restore_intx) { - dr->restore_intx = 1; - dr->orig_intx = !enable; - } - } } EXPORT_SYMBOL_GPL(pci_intx); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index c98de280b16e..1ca591f37270 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -812,22 +812,7 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) } #endif -/* - * 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. - * - * TODO: Struct pci_devres and find_pci_dr() only need to be here because - * they're used in pci.c. Port or move these functions to devres.c and - * then remove them from here. - */ -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); /* * Config Address for PCI Configuration Mechanism #1 From patchwork Mon Apr 8 08:44:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620785 X-Patchwork-Delegate: bhelgaas@google.com 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 960FA4F1E2 for ; Mon, 8 Apr 2024 08: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=1712565895; cv=none; b=epCDoAMnSlieET8lVnCmqzhupOstGRmrVlxrxZOLT11UqkbMpubzyOZ863KGHj7AuB4NftBi+p4w1kTnLHBkdfIjXlcJ52C+VR0aKiOHWrXsBsgX+4DfO0EGp6eTCnxLEJXzFQhztcHwrgpOFIamdnoYGY+wYZvPIyZ5FFju+AU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565895; c=relaxed/simple; bh=OSX8DiwC7JJUNzRzeW1OOkPWOeyv5OusvgjKcUnS2qg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m+9VcvX/5FNj/2UdHHbR0w5wTgcUJwgBbjy8JX2Ys192VzS74P4CF51ORNhITT1Q2OGVfWx7BieRPz55UHOEDrW7XhW31mGKiUTi5UntQxjZqCS2DUgzzJAIGxCQsNpbkixTdZF6t6D4rGGMqKJlMIXVp/NTwMjETYIGyMeqgS8= 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=fj/+Twbp; 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="fj/+Twbp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565892; 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=oIKnJtHF26OJHg4kpBO8DHHyo5/AIkRLYVzYG0l0EPg=; b=fj/+TwbpMKlDFJVBxOZOear4zKdkNRYVu1MZp0ddNP/vyBlKUAUelr2NRdFZkCI9UeaseK 8K1Ri/7tJez8jKaQqX28zJlV48hfJjRL9fvc4x7sU/PUZu9Q0ots/DCc7YquXTsTQ/MduF bKJM4amv5/afZvLfrc0oqCdJfcRPgOQ= Received: from mail-ua1-f70.google.com (mail-ua1-f70.google.com [209.85.222.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-453-gE_cLU35PvKxbjxh5dDQ3Q-1; Mon, 08 Apr 2024 04:44:49 -0400 X-MC-Unique: gE_cLU35PvKxbjxh5dDQ3Q-1 Received: by mail-ua1-f70.google.com with SMTP id a1e0cc1a2514c-7dba4d44c1dso582020241.0 for ; Mon, 08 Apr 2024 01:44:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565888; x=1713170688; 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=oIKnJtHF26OJHg4kpBO8DHHyo5/AIkRLYVzYG0l0EPg=; b=tXVJk6KYj/lrPt0fZyH64lSia10pmkYnplb34IJ9sek88ZiSZYJJWPixB5eFln0X0d Qmk6LQHd8ukNeRFo5HnYyMg9Yugj5zXRWEtkmVoCQhU3F5rK7LzrzDtmiNxEuL0YEunT EA4NT1WvyifN9X8FAqfbRCMY6MRcawJsGHRVJfL40JCx9/yOYRhq+PQ7xT/Wy8czQeBH OJidOq+sLmNbyN8J6XVb7Udu7v+zkDo8ZiWM57SN1q8gFwOJuntjTCLxsGWmzwnXNnmI qBkJEXmzE6/cJ9/8S6aGhxsUaI0lDrTyApuIbU+sc3pehcLjc3Nz2cn16akMV5BwQPfm Vdlw== X-Forwarded-Encrypted: i=1; AJvYcCV386cPHdvZUgzbo8mQeayL9+9L8+NHPwm0THq38JuLNRdip7c7oqobR8mP/jG8/qCzuWcrjIOKwrFcOBVzrjuqqW3kX1mpveKq X-Gm-Message-State: AOJu0Yx1PEGpLorTNrqDqLoVqmKM0tr9nSHpHPFSi8of8fZ1ichEzS8F /eI2Lf9UBq7M9GSqKdy1Z8l7ILnnC8HcBhjWZJjAT2v+OoaWuYglO5CkDTqmdz6ehR5WToH0F5c evtrlvh3OprebBiiWUIZwxXal3SMRbxvqutHJEpCL+NC1y0r/hqI6dGTXzIzq1qzAIA== X-Received: by 2002:a67:f610:0:b0:479:dedd:565f with SMTP id k16-20020a67f610000000b00479dedd565fmr4684472vso.2.1712565888607; Mon, 08 Apr 2024 01:44:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHL+VVRQtoyNoPL7BOIfq0nBVbWMHsJxHqXPuBD7jjNvaEZvxqdDMSuVChpXM2urhBCQM1pWA== X-Received: by 2002:a67:f610:0:b0:479:dedd:565f with SMTP id k16-20020a67f610000000b00479dedd565fmr4684452vso.2.1712565888186; Mon, 08 Apr 2024 01:44:48 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:47 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 09/10] PCI: Remove legacy pcim_release() Date: Mon, 8 Apr 2024 10:44:21 +0200 Message-ID: <20240408084423.6697-10-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 b81bbb9abe51..1229704db2dc 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; } -static void pcim_release(struct device *gendev, void *res) +static void pcim_disable_device(void *pdev_raw) { - struct pci_dev *dev = 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 = devres_find(&pdev->dev, pcim_release, NULL, NULL); - if (dr) - return dr; + struct pci_dev *pdev = pdev_raw; - new_dr = 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); } /** * 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; - dr = get_pci_dr(pdev); - if (unlikely(!dr)) - return -ENOMEM; + ret = devm_add_action(&pdev->dev, pcim_disable_device, pdev); + if (ret != 0) + return ret; - rc = pci_enable_device(pdev); - if (!rc) - pdev->is_managed = 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 = pci_enable_device(pdev); + if (ret != 0) + devm_remove_action(&pdev->dev, pcim_disable_device, pdev); - return rc; + return ret; } EXPORT_SYMBOL(pcim_enable_device); From patchwork Mon Apr 8 08:44:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620787 X-Patchwork-Delegate: bhelgaas@google.com 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 3939D4F20A for ; Mon, 8 Apr 2024 08:44:54 +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=1712565895; cv=none; b=Da4shi7xKDQYkH1gNY2j+RyqsCf6MhRsiahEfITLo3KejQZG1w0C3aIWVtyQrYGUzPXKde6UYDQop9PG95n7hk1ZtbNjpD7O/uAZWU3rRINCBXKerOIzmNXoNwbjiTPZ6UyZba6VgmQrIeHBOz9/TurcqBR4eNJ4Jv6mwqxI4cE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565895; c=relaxed/simple; bh=seKAVIenEEwbzM3Iic5g38ri/d9lXst00U0PHR2Zy28=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ci5iBrLnsG/px1vFnbEfDF95/Jlv8LY3QWbAdooWwmswA9rM/Ks4YE83tl8MDORIFXDlkqea23oBbLMFzrbgv3EeTpV+U++vkjjH6rbyk3gh8DalCNjVwcpRRAxFkac0db7qT+NdGx5tLEng2kUHD0ml54Zql/+UHvtzKlCtBEc= 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=H4CxYqMZ; 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="H4CxYqMZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565893; 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=OnmS+ZQ70OmsHKGyqstMbXO2wKO+H5ag8eg75yEHt7A=; b=H4CxYqMZBMw5yYeMYh6f1zpNx/ZgBz5gceGCNMXbBcdNSFGdZmPVLgSMGeCKNaSVClBsTU J8N5wed0HM82E2dMm9m13JOsPV4NOSeKkbAqmbmjY3L8rPlLMDmV4TH1S8EYJcht2XcS9W KP770ZvaNc4kkzEjLsb9mhwQk2wKjuw= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-249--jVHdLRdNHy5a_P443pn9Q-1; Mon, 08 Apr 2024 04:44:51 -0400 X-MC-Unique: -jVHdLRdNHy5a_P443pn9Q-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-78d68cc5e1aso1290485a.1 for ; Mon, 08 Apr 2024 01:44:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565890; x=1713170690; 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=OnmS+ZQ70OmsHKGyqstMbXO2wKO+H5ag8eg75yEHt7A=; b=q3ZhBEKF/fdtgW/PjK6fD5lh7U3KVuYcKCvvTsnykNOW/pqfo2Ue/1dLqtCpY1ghOM mdO2W7vRcxTjd4ZRmOLdiZPwGU9ANk+apM5sUBCTBfpUkovw35Wkg/nO88x1mWNT6tJ4 kv+IZLePsMia9ZUNyheF4Wmp3Q7aMPVI9EZdJACOGDX2vh+K2qWS62ZL4WdrZ6lTPMWG rzoyfQAVq5UcD6HqPex8TdIAsP1DTwntycvbHTSKiOAsEf2Mi30lSK5rZ/NR5Hkw7zSn FxycknPimdh2DJbIbUTA5Q5HrRTxLh/S/eT84zlG5DQ2N7X3tHlZUpmomU+MoW4u5Zp6 1IUg== X-Forwarded-Encrypted: i=1; AJvYcCWr8PhSPbv1s6W5VNCxGcThetLvX/v3l5Mw3dH57a6CGidCUBXRI5ggXGu+QDp0qyF1QIuEUWdh9/0fZwoQmaYcZUJfY/L84odB X-Gm-Message-State: AOJu0YzVLlLb+FhQFABknTgDLQ7mokuLN6NoIbrbH9D1FzVxGtZV5qDn 9nYKjLR0YfpKH8cFW6C6V0m9qnrBSgB/JWzRx/kutBtehMmYf6c8eqKHrXCuOeE+NpvZC7wt1NN JvjGvsUUbJy5UpwpUkvJlnhkAsdBfOj8iZp7V9DnZv07PpVa5cZs4ZgwTig== X-Received: by 2002:a05:620a:319e:b0:78d:5fd5:9254 with SMTP id bi30-20020a05620a319e00b0078d5fd59254mr5644008qkb.5.1712565890764; Mon, 08 Apr 2024 01:44:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHsVOxGIi2Iuj/AVIRSbBNaD6COng8+R3PpINEdp5/DQW+HcEotDsruGHbTospw6yxnsP+ZjA== X-Received: by 2002:a05:620a:319e:b0:78d:5fd5:9254 with SMTP id bi30-20020a05620a319e00b0078d5fd59254mr5643989qkb.5.1712565890518; Mon, 08 Apr 2024 01:44:50 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:50 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 10/10] drm/vboxvideo: fix mapping leaks Date: Mon, 8 Apr 2024 10:44:22 +0200 Message-ID: <20240408084423.6697-11-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 PCI devres API 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(). Fixes: 8558de401b5f ("drm/vboxvideo: use managed pci functions") Signed-off-by: Philipp Stanner --- drivers/gpu/drm/vboxvideo/vbox_main.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/vboxvideo/vbox_main.c b/drivers/gpu/drm/vboxvideo/vbox_main.c index 42c2d8a99509..d4ade9325401 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 -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE; - vbox->vbva_buffers = 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 = 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); for (i = 0; i < vbox->num_crtcs; ++i) { vbva_setup_buffer_context(&vbox->vbva_info[i], @@ -116,11 +115,10 @@ int vbox_hw_init(struct vbox_private *vbox) DRM_INFO("VRAM %08x\n", vbox->full_vram_size); /* Map guest-heap at end of vram */ - vbox->guest_heap = - pci_iomap_range(pdev, 0, GUEST_HEAP_OFFSET(vbox), - GUEST_HEAP_SIZE); - if (!vbox->guest_heap) - return -ENOMEM; + vbox->guest_heap = pcim_iomap_range(pdev, 0, + GUEST_HEAP_OFFSET(vbox), GUEST_HEAP_SIZE); + if (IS_ERR(vbox->guest_heap)) + return PTR_ERR(vbox->guest_heap); /* Create guest-heap mem-pool use 2^4 = 16 byte chunks */ vbox->guest_pool = devm_gen_pool_create(vbox->ddev.dev, 4, -1,