From patchwork Wed Jun 5 08:15:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686367 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 40D2E194A6A for ; Wed, 5 Jun 2024 08:16:42 +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=1717575403; cv=none; b=V+Fd2/ksMTqO7kaH0YZrLXna7NMZ0Ey5fiDBhebLV9VQLLZyJC6y8LJhl/UaBcFMQ01V6WdnDUIyw5mfVEpfOMwHgl/Ib78Km/TKBYmq80wWZBZgvjTr62ozbKcZExYXf2P6zC2ftH4zvzKz4hArS8jvWnH5XogPNfRn85GvAqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575403; c=relaxed/simple; bh=tV49a5+qMkVnfBuVcqqm88/YXQEYCtYEpLyZHAId3eI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dBx+mU1cB1NiLYFyogM/++MSIj4LgmcLaLHbodvPEiRhSy7xdPF1+lw7Mvzp1cZ6zdEPr5R51qNkjRxWtZrQJ8KWRh2NbNDe97UB+OOEkJebPpv6+71HoFaxbygv4WXt8M+F0/fSeZ99Z+zb1c/CxBOK+dT0N4KYafGX7oUphJ8= 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=O2jB74V1; 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="O2jB74V1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575401; 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=FprLruSafewJCGyfXmuQwDL1Jg/OKDGH6ODfzThcTic=; b=O2jB74V1BbEg7AcE8r/dvcqjL+QZRLdixpVG1kyslUOMoZu+95mWsmNIHHoJk3DSHawitv echLG8vd6g34S59K2dZllcMgeY+EriRItqEz/T7aMzXQ8DOhVnD8LHhlFiO7bLUJgyYfhG xfrlkuyOL903CNlik3wPSvIX0IeQErg= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-4B4qLlT7Nca0Qx_-ZXDMhw-1; Wed, 05 Jun 2024 04:16:39 -0400 X-MC-Unique: 4B4qLlT7Nca0Qx_-ZXDMhw-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a68aa5ca81eso17769666b.0 for ; Wed, 05 Jun 2024 01:16:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575398; x=1718180198; 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=FprLruSafewJCGyfXmuQwDL1Jg/OKDGH6ODfzThcTic=; b=F7lQQdpLlp3JBtM5cUzmz8rp08QlhwO6kXecn+PUkRi6/ew6nvxRqk9JbsBy3MMgdi I+q2hCk7z5QHU6ThZg5hiaZm6xxPsUVYdyzcf8HDUemxHh8PvbyBudnMhKw7dXuFDCxP KiTeDXq26WghcFD5vw+nF6zJWqgOBJ9vPEcR+i/eKW6tRYNPup5k/Y61fbkxR5Na4QSp vnvr7YBkCcyxSq9R7+r8IYjHPSEMp6sxid1kdpWb3OJ/48I0K99vD/g+wiRilWPdwoko ZXET92GkJlIHUGhOQ6su3zfLb9V2d3YbNPB5OqOM9RtqirFpLHjaM/hvwcEKLMw56ATq QtYQ== X-Forwarded-Encrypted: i=1; AJvYcCUOAmtcI0SsUwgRNJZS0qVwx+pIrCeCxzqyU0LWynnD5M4Yt4NuGT+OGWzo0oQyZrLhVSm33EGf/TqVexs9DhUw7kJ5NDsuVS9k X-Gm-Message-State: AOJu0YxtRytsmmnAZb+qhrB26Hw5zdPVEakj+Gxcmj5Mtsci97Aj7JbW DjUVZnCtFbWQ93MHxs+GzCySuDtoSZRKzBIZUMPu0k2+AC3BxNmdWVaAaPMu8U+hqJznCD4G4xd 9hLkzWnKFj5NvBXjff9am5+/9qGOYyELXqNlFxL2ExuASj/B+rS+FrPeH4XN9n6dOog== X-Received: by 2002:a17:906:4091:b0:a5a:89cf:489a with SMTP id a640c23a62f3a-a69a023ebfemr107141766b.4.1717575397888; Wed, 05 Jun 2024 01:16:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGH01zzkiwwO2rlaNoVMW4xlWaRAMHnpBHn33IIaKEoTGFWWRSfd/BTk6G3QT5BqGH8wLV82w== X-Received: by 2002:a5d:404d:0:b0:35e:83dc:e6ed with SMTP id ffacd0b85a97d-35e83fcb079mr1241057f8f.0.1717575377322; Wed, 05 Jun 2024 01:16:17 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:16 -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 v7 01/13] PCI: Add and use devres helper for bit masks Date: Wed, 5 Jun 2024 10:15:53 +0200 Message-ID: <20240605081605.18769-3-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The current derves implementation uses manual shift operations to check whether a bit in a mask is set. The code can be made more readable by writing a small helper function for that. Implement mask_contains_bar() and use it where applicable. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 2c562b9eaf80..f13edd4a3873 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -161,6 +161,10 @@ int pcim_set_mwi(struct pci_dev *dev) } EXPORT_SYMBOL(pcim_set_mwi); +static inline bool mask_contains_bar(int mask, int bar) +{ + return mask & BIT(bar); +} static void pcim_release(struct device *gendev, void *res) { @@ -169,7 +173,7 @@ static void pcim_release(struct device *gendev, void *res) int i; for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) - if (this->region_mask & (1 << i)) + if (mask_contains_bar(this->region_mask, i)) pci_release_region(dev, i); if (this->mwi) @@ -363,7 +367,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { unsigned long len; - if (!(mask & (1 << i))) + if (!mask_contains_bar(mask, i)) continue; rc = -EINVAL; @@ -386,7 +390,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) pci_release_region(pdev, i); err_inval: while (--i >= 0) { - if (!(mask & (1 << i))) + if (!mask_contains_bar(mask, i)) continue; pcim_iounmap(pdev, iomap[i]); pci_release_region(pdev, i); @@ -438,7 +442,7 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) return; for (i = 0; i < PCIM_IOMAP_MAX; i++) { - if (!(mask & (1 << i))) + if (!mask_contains_bar(mask, i)) continue; pcim_iounmap(pdev, iomap[i]); From patchwork Wed Jun 5 08:15:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686357 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 1B09C18FDC6 for ; Wed, 5 Jun 2024 08:16:26 +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=1717575390; cv=none; b=KXNTpivbA6xKkFGFligPAfJmo9qihPk4ySJTmD/IugKympoqO83OqHvox1ra4hZSmlR+KovGvrTf/N6HQD3gSwIAL7t3YCQ0PzL2H8tAoTWkIuZUlD/USnQK0U5Xr5zxtwtR+VT43/oS7yOalOw5lY6YIkwa1OMHIy4E5l1iPE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575390; c=relaxed/simple; bh=MUiAwXJ/IFLMsknTx0bC4h12IF78V05yZXAKWCbz8BE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fXl1JZwb7fmUpxdNnWigVTvAaBMO5tQ9w3z9gz+rwq1oE7mKcTFdA9wh7KuLYqoMEDtsqiuizH5GCuaj2sbUBORuJuTFb3wwZ/MYlUVC5RlzBl12oPwIweDdhwujJgnAWnK2mAq5lj+i5hsuDiPv9OsMf1AADVKE6scGOrgnOZk= 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=BxbcaigI; 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="BxbcaigI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575385; 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=yZrCQX9R0uMZLnmEvnk0FOyHrEpxtR6m1CEijmNoZqI=; b=BxbcaigITnCZhyeiPs5wud8rNfUmgyOWz+W60nZuMojY6OpNr344GA3wiMip4urkjgQFrg uSIm4WaNPt1l+28YUx9DBXCBiisjdeTw78soEyUr2bafbZDlP9Vaa3Fu/Iwk8Fnl8JgFf/ zsWbQp2MDefKf5qc4twtc8uw47ueKXw= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-12-hWqS6kmiNiW1IqO5AlANAg-1; Wed, 05 Jun 2024 04:16:19 -0400 X-MC-Unique: hWqS6kmiNiW1IqO5AlANAg-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-42110b33fecso7980365e9.0 for ; Wed, 05 Jun 2024 01:16:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575378; x=1718180178; 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=yZrCQX9R0uMZLnmEvnk0FOyHrEpxtR6m1CEijmNoZqI=; b=MvQCI4vUSJYHhYTrbMNGBR8PHTc0IkFYPoAwk/h0rzg6j/rnN2OFGnkUHc6niT3IVS E4GdnA8V8e76xLTjPga6IIfj8d2fgMs0XAD2LBMDi0MXDuycPuhgRkpxOkL44/zKO8uK 7XEachs/+jg4tIo+ae8qLcpo667NrZM6kS7fSMpwR7AgHNwsZM8FhzhzhOUx69bbvzbO 7tJKlq17Koa4rvrS2ZPx5cV1XjQGO46uJyppilnF7CAmtn8q+hCow+OHcEuJfcJTJP5o quQKLqlBn42pJyXaam925VtKrij8lDD69o3HNWmadMi877nKqjPnVFylXKBIFzB6A4zh VXBA== X-Forwarded-Encrypted: i=1; AJvYcCUFWjxgnqNkneVtGjzO0wTcVxoDcqlDr4zjjS3q4fO7q+HR1dQ9Xkw8NNRLmKsXiyli7qgoOUdda8yZOWheYWF93PckP4qeFwUW X-Gm-Message-State: AOJu0YxjfoMiXsRaMOsdjhI6dLYrm8XpklA4UQ8hf3Vj+PYiEop2gnY6 8+r43nniOMOb1QLo/6TDafTnv52yqbGvcwnVnua6UxWIDnvaOlZFKxA4xm9M8ILxw5JIua1YRMc FfQao50x/BAXU5ytWJVZJZpnmeuM3ozH5Vg8LkLlOwTOhudu1XlrjwrdUqQ== X-Received: by 2002:adf:fe0c:0:b0:355:291:19da with SMTP id ffacd0b85a97d-35e8ef7f1femr1115401f8f.5.1717575378577; Wed, 05 Jun 2024 01:16:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGB4zVY6+VoEzLjRsWMaLUjiRNKwhy7h7Hs/HLcCXOD9gH84CvZuj8siARFwKu4gffWqGiURA== X-Received: by 2002:adf:fe0c:0:b0:355:291:19da with SMTP id ffacd0b85a97d-35e8ef7f1femr1115390f8f.5.1717575378288; Wed, 05 Jun 2024 01:16:18 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:17 -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 v7 02/13] PCI: Add devres helpers for iomap table Date: Wed, 5 Jun 2024 10:15:54 +0200 Message-ID: <20240605081605.18769-4-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The iomap-table administrated by pcim_iomap_table() has its entries set and unset at several places throughout devres.c using manual iterations which are effectively code duplications. This can be done in a centralized, reusable manner. Providing these new functions here and using them where (already) possible will allow for using them in subsequent cleanup steps to simplify the PCI devres API. Implement helper functions to add mappings to the table and to remove them again. Use them where applicable. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 77 +++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index f13edd4a3873..5fc35a947b58 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -297,6 +297,52 @@ 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; + } + } +} + /** * pcim_iomap - Managed pcim_iomap() * @pdev: PCI device to iomap for @@ -308,16 +354,20 @@ EXPORT_SYMBOL(pcim_iomap_table); */ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen) { - void __iomem **tbl; + void __iomem *mapping; - BUG_ON(bar >= PCIM_IOMAP_MAX); - - tbl = (void __iomem **)pcim_iomap_table(pdev); - if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ + mapping = pci_iomap(pdev, bar, maxlen); + if (!mapping) return NULL; - tbl[bar] = pci_iomap(pdev, bar, maxlen); - return tbl[bar]; + if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0) + goto err_table; + + return mapping; + +err_table: + pci_iounmap(pdev, mapping); + return NULL; } EXPORT_SYMBOL(pcim_iomap); @@ -330,20 +380,9 @@ EXPORT_SYMBOL(pcim_iomap); */ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) { - void __iomem **tbl; - int i; - pci_iounmap(pdev, addr); - tbl = (void __iomem **)pcim_iomap_table(pdev); - BUG_ON(!tbl); - - for (i = 0; i < PCIM_IOMAP_MAX; i++) - if (tbl[i] == addr) { - tbl[i] = NULL; - return; - } - WARN_ON(1); + pcim_remove_mapping_from_legacy_table(pdev, addr); } EXPORT_SYMBOL(pcim_iounmap); From patchwork Wed Jun 5 08:15:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686356 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 4919A18FDCB for ; Wed, 5 Jun 2024 08:16:26 +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=1717575389; cv=none; b=nYwklJxABcdKJkWCPof+8QloUdXBTarVc5MM4epliihEhN3CRqmJx3nHTGYkzWHgr/w2R2cSvN1+ONaphMGflqOalDlAsNn960T5Re1qaA2vH+EtqluUGD5n3aFjzE4+WDET+pN30zCd3ngJkZ7XdMSDcrymF4cltHLfBjVqhMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575389; c=relaxed/simple; bh=BSnzLTAgPV95yr8mbMSKU5dgmsbz7wEWuc/mk0Nz9CI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TemaCYUMwPQGz+jgpct4ZsuNVkW44cNCXgym1lC9pbvmWANy45B72l9BMWm8Dp3BuGz2ArMdK+xXAGq3df1ifY5vcd1KqWQZkwVzOeWtgYbVAKDiw0INaWnq0Esa1D3Gz5CLze7ZH+WYVUYSljYr/mUTpZfOmhz2GO/D8MnLohQ= 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=DvRi0h+Y; 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="DvRi0h+Y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575386; 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=7QZUAm3/P6BMj8BPTKAXvmonNC7iOu5bcpOOG2NdhbE=; b=DvRi0h+YvUmFD10+bsLDqasRMqC7Ue8b3jh9VKiUBgaRhSlHGi5oeWtS4+ZvX7XG72cOMl fr/9ecl17KHpw5dCMH0qyxekqvZZ8BGD/1lN01NFCD8pzSn/JmJm0NuHcg711/R7F/67lo pr+9Uvf8ecZEe8d75TEZmlQcJug4vCQ= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-56-4WjVuLKxM9OnwU2dtaAZzg-1; Wed, 05 Jun 2024 04:16:21 -0400 X-MC-Unique: 4WjVuLKxM9OnwU2dtaAZzg-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4210e98f8d7so5392495e9.3 for ; Wed, 05 Jun 2024 01:16:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575380; x=1718180180; 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=7QZUAm3/P6BMj8BPTKAXvmonNC7iOu5bcpOOG2NdhbE=; b=UqyuLZCn94QEITZZ9o/6+iJ0KisAgywrmjS9NNumtpyiqf+DIhTOuZQwnMvSJ5g3Et BcEDpJl6G9ry3TkUQF6qw2cdS5wT8jwGetkLvsu3sU9AyxAsoHkBNIYuK/PD2BKlC/Qa 1FrDkyq42I930v92DQyqHQ+9TV1QGX9T6XqPqO/SMH2jAMvzjK7mS1vGBXbeJZAyPWEP 1Bn5uYaHZlkEV0PziyNwZcigVyusJ10nhjbynnGcNHQA1sRhmqgC4Q+T4JWGzcqtRWwO yD+W6kYBfWZbyHFrI047m+wVVFiQeldK2QstqtszDhrh+Qhf0Cio74PbsP0JceTXVBfb QSlg== X-Forwarded-Encrypted: i=1; AJvYcCXhJuywJiPCov6XIF5mjYWO2wNhvdqm0oIUq7n76T9vdy7Nih0BkNW91i1JSUVJv6XM+kfk60LitkUvCb66EquI/1Pvvk/cZYzo X-Gm-Message-State: AOJu0YzZzz8E5HTCMUEtTK7UPsPHVlgrnqkslqVS9f1NP0PzR1MYZJW7 1xKdufwS+Mx68hxQtMPAFflBQi5qXgVH/w4ZXmX4GiZnj4wS1mslkG+q4ZXfgmN42cSEIeI71jZ CLeGTlR0/TOvhKScgEdL9IuFf4cpBzF+p2uJUvXU8iop0VhN904TMW2/2SQ== X-Received: by 2002:a5d:49c5:0:b0:354:f768:aa00 with SMTP id ffacd0b85a97d-35e8ef8f11amr1107029f8f.4.1717575380003; Wed, 05 Jun 2024 01:16:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnF0pZqYH0c98RKatbysEOPbfDC7KBUtpzdTzQOa2BrLd+AU7gMeFnid8aSFNCXMb+x4uLfg== X-Received: by 2002:a5d:49c5:0:b0:354:f768:aa00 with SMTP id ffacd0b85a97d-35e8ef8f11amr1107011f8f.4.1717575379364; Wed, 05 Jun 2024 01:16:19 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:18 -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 v7 03/13] PCI: Reimplement plural devres functions Date: Wed, 5 Jun 2024 10:15:55 +0200 Message-ID: <20240605081605.18769-5-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 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 internal helper functions that don't have the problem of a hybrid nature that their counter parts in pci.c have. Write those helpers in a generic manner so that they can easily be extended to, e.g., ranged mappings and requests. Implement a set of singular functions that use devres as it's intended and use those singular functions to reimplement the plural functions. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 601 ++++++++++++++++++++++++++++++++++++++----- drivers/pci/pci.c | 20 ++ drivers/pci/pci.h | 5 + 3 files changed, 559 insertions(+), 67 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 5fc35a947b58..e6e791c9db6e 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -4,14 +4,238 @@ #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 { + /* 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, +}; + +/* + * 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; + 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; + default: + return 0; + } +} static void devm_pci_unmap_iospace(struct device *dev, void *ptr) { @@ -92,8 +316,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); @@ -172,6 +396,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 (mask_contains_bar(this->region_mask, i)) pci_release_region(dev, i); @@ -258,19 +492,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 * @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 @@ -343,30 +579,67 @@ static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev, } } +/* + * 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 *mapping; + struct pcim_addr_devres *res; + + 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) - return NULL; + goto err_iomap; + res->baseaddr = mapping; if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0) goto err_table; + 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); @@ -376,91 +649,290 @@ 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 a pcim_* mapping + * function. */ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) { - pci_iounmap(pdev, addr); + struct pcim_addr_devres res_searched; + + pcim_addr_devres_clear(&res_searched); + res_searched.type = PCIM_ADDR_DEVRES_TYPE_MAPPING; + res_searched.baseaddr = addr; + + if (devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched) != 0) { + /* Doesn't exist. User passed nonsense. */ + return; + } pcim_remove_mapping_from_legacy_table(pdev, addr); } 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(). + */ +static 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); +} + +/** + * 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(). + */ +static 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); +} + /** * 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 + * + * Returns: 0 on success, negative error code on failure. * * Request and iomap regions specified by @mask. */ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) { - void __iomem * const *iomap; - int i, rc; + 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_contains_bar(mask, 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); + } + + return ret; +} +EXPORT_SYMBOL(pcim_iomap_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; - rc = pci_request_region(pdev, i, name); - if (rc) - goto err_inval; + res = pcim_addr_devres_alloc(pdev); + if (!res) + return -ENOMEM; + res->type = PCIM_ADDR_DEVRES_TYPE_REGION; + res->bar = bar; - rc = -ENOMEM; - if (!pcim_iomap(pdev, i, 0)) - goto err_region; + 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; +} - err_region: - pci_release_region(pdev, i); - err_inval: - while (--i >= 0) { - if (!mask_contains_bar(mask, i)) - continue; - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); +/** + * 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(). + */ +static int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) +{ + return _pcim_request_region(pdev, bar, name, 0); +} + +/** + * 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(). + */ +static 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); +} + + +/** + * 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(). + */ +static 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); +} + +/** + * 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(). + */ +static 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 rc; + return 0; + +err: + pcim_release_all_regions(pdev); + + return ret; } -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 + * + * 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. */ 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; - rc = pci_request_selected_regions(pdev, request_mask, name); - if (rc) - return rc; + ret = pcim_request_all_regions(pdev, name); + if (ret != 0) + return ret; - rc = pcim_iomap_regions(pdev, mask, name); - if (rc) - pci_release_selected_regions(pdev, request_mask); - 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; + } + + 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); @@ -473,19 +945,14 @@ EXPORT_SYMBOL(pcim_iomap_regions_request_all); */ 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_contains_bar(mask, 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; }; From patchwork Wed Jun 5 08:15:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686368 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 4B6963DAC0C for ; Wed, 5 Jun 2024 08:16:46 +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=1717575407; cv=none; b=ssGLndNxcZ15kYMy8GVos9+Isx8c73T33J1pehi2KT5SIMR93mW5XNYNEObDdiP//SQL0ojbeup7wv3fCsn/LmwkXHzSc0ikh8gQEo9FXmZj0Jj6K+tLAm0G0k0mAf1+znROsh5VScMzgPDbJLMdaKcwDkVAFegjGU9kLlvvBeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575407; c=relaxed/simple; bh=bCBZE66HS4MGevuCEuGbllziHVNkZUwOrdLYjEkBs5Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JvItIii3Zmn8Dy689H7MsI6mUbmQkD2nUxl+60bwA78feyPyUsAjiQpoiOpFMsYiWc68ZeKIITJb1s5SzuA0GQuv0AfhKNSDuqkB/Tsj59nCiTbY+bKRVPrnhG+/d7313VzgdPYDgT9QrRj+tKQ8W7bqJVJs1H4fIqD3kTu+1rU= 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=ZbzBrObm; 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="ZbzBrObm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575405; 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=PQF/RRtq2M1mXHCVQZRIWcpncx1S//L9Tm5LTcwCt7g=; b=ZbzBrObm+j+iMlIWF5kabtlOGAiv40BI4MLT7jR4JLkLCnnvb6L7cgFAMZsEzp8xNOy+TX yLNwiTEQ2AmkORw07YNT7scevwPfmK8xQVAHe2aMJz2vkPPJIsP49UguIBrhRqisniURou B+ZCwWv0d215AV6aO37/6w6BzUyGQD0= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-160-v-3t3J8qNo-lSIwyRGbm9Q-1; Wed, 05 Jun 2024 04:16:41 -0400 X-MC-Unique: v-3t3J8qNo-lSIwyRGbm9Q-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a68bed163b0so13228366b.2 for ; Wed, 05 Jun 2024 01:16:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575400; x=1718180200; 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=PQF/RRtq2M1mXHCVQZRIWcpncx1S//L9Tm5LTcwCt7g=; b=QKxQfVsq7ZQw/pZVm2TKg6NY4vjWPsF17YziEEaxgzUDKhvJ2WnfzKTI5c5BklMd/l GiXMYksX4H/ZWhStMyWi67o7g1tLEqIFioKcuYi0tJKICSYpemqeZ97Stnff0OS+LjLf me3ZR5CT/2TXI1Clv5TsWeDo1JwaPa+UP9ZnMLDtwQ/B0qOW+MxthWLHudltTbckp7lj wOP9gzWEIBDgJo4xISkiOT4Yp+kzVXWkVIuZT28uKhjcJ6O979pSJ3Qmfi7ZrTcLMhDf HwlkYJrCTCBPXOu3MyerhYdrgliE0jGVrZR7FrXi8IsGKOSStPATbKUkKhzp8FbP2550 5Y/A== X-Forwarded-Encrypted: i=1; AJvYcCWCRtstvtB+wQSjAWImX9NgSBugI47EOd1MNxxLBasR75ND14Uz96/8st4IuhYojbz/1lzJ9wbhPzdsueycYNqwIxRoEXrk28sI X-Gm-Message-State: AOJu0YxRlG9LosVmNiV/rTA3AZxBXNqOGMiziI59mpBn7Ks2PQ7jZ0ds yay8UJ//eZyW3YVJo/+C8g/hPefVoM8+vVb6azpYg1zlTp0SRmuYCrDJamzNPFHIfnw/CkPzR2d CSe1D9KceS5NswJbbUiuBGjvB0duJVTBVHwphZlCKQcE1NgAAGMmGoPSyew== X-Received: by 2002:a50:9983:0:b0:57a:2069:e91 with SMTP id 4fb4d7f45d1cf-57a8b674ab5mr1282380a12.1.1717575400719; Wed, 05 Jun 2024 01:16:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnDWsmeGVuf2mez79IRf+VouIvlEPmoOV19RR5wEUe4IIflrHEB7JVXR7OyM17+DSycTI2BQ== X-Received: by 2002:a5d:62c4:0:b0:358:a09:2677 with SMTP id ffacd0b85a97d-35e84046ff8mr1214838f8f.2.1717575380210; Wed, 05 Jun 2024 01:16:20 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:19 -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 v7 04/13] PCI: Deprecate two surplus devres functions Date: Wed, 5 Jun 2024 10:15:56 +0200 Message-ID: <20240605081605.18769-6-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 pcim_iomap_table() should not be used anymore because it contributed to the PCI devres API being designed contrary to devres's design goals. pcim_iomap_regions_request_all() is a surplus, complicated function that can easily be replaced by using a pcim_* request function in combination with a pcim_* mapping function. Mark pcim_iomap_table() and pcim_iomap_regions_request_all() as deprecated in the function documentation. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index e6e791c9db6e..f199f610ae51 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -501,7 +501,7 @@ static void pcim_iomap_release(struct device *gendev, void *res) } /** - * pcim_iomap_table - access iomap allocation table + * pcim_iomap_table - access iomap allocation table (DEPRECATED) * @pdev: PCI device to access iomap table for * * Returns: @@ -515,6 +515,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) { @@ -886,7 +891,7 @@ static int pcim_request_all_regions(struct pci_dev *pdev, const char *name) } /** - * 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 @@ -897,6 +902,9 @@ static int pcim_request_all_regions(struct pci_dev *pdev, const char *name) * * 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. Instead, use one of the + * pcim_* region request functions in combination with a pcim_* mapping function. */ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name) From patchwork Wed Jun 5 08:15:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686363 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 6A54619306B for ; Wed, 5 Jun 2024 08:16:32 +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=1717575394; cv=none; b=kS2HwwldT6VMsMYNVg/C/XS6cL1DKfLRuccpBPq4d5alEYyWE3vfhtF0ubINDT995TYWOcJDDsT0etqEvje/TC5d3NFJU7aw71jWj6+3JzEHiNg8aycDhZID+AMUrf6acOiK6+BpIAevAZ6YMeZfbjXbXU1eksdjsj7BMrIDTIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575394; c=relaxed/simple; bh=AlFTmLMzlg/9siSmkDtaN7HrGXf9sRz/JBoLcrgGJpE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G1tszl+G/Kk6StZ3lPcc9x68HWFCUyhWi5qhkfTxKncXvjzV6llh3PCEx8Q9XmK1E9zZ9v+rPUcPV9lkvS0F7gZngJpX340KdcKeX4SiwmlhovB3ZiVQTDmfoaCjiz6UQJE6EvpHOEr6Q9XsPbAsrPHN1ABdRY0GmdmKvM0iJIA= 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=ej8/cys4; 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="ej8/cys4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575391; 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=3wjuhXIVsdrU0yS+eTswxcN+RcBPUQZ+Ad0UnwdbpZc=; b=ej8/cys4mDxvQErsiIoNDu65N2ZFq+MCMRfvJgaIxDToQ0TZHLjGf/hj9w7Dag0QwJU9+q zij+7Bkg/dbDRR8SbwkFVnJPVoLKOr47EuGU4gqzH78Ja7QmYl11jA6B6Yf9eeIl1xgZi9 g21kKYzUY/aC/uVlKILRz4wbVUjbtdc= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-593-6noU5jxSMVSLjksu04OMQQ-1; Wed, 05 Jun 2024 04:16:23 -0400 X-MC-Unique: 6noU5jxSMVSLjksu04OMQQ-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-35dceeb18c9so281441f8f.0 for ; Wed, 05 Jun 2024 01:16:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575381; x=1718180181; 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=3wjuhXIVsdrU0yS+eTswxcN+RcBPUQZ+Ad0UnwdbpZc=; b=RL+wccEk6NYiWx6uXEWz0IfD51l1qDIN3JsDZ0Vgd5deI43/8uVceTN2wu0hr2RF5O nunucCN6OT7PgQhrQ7EZ9lQ69W+r3n4GYl002W1x7rCftKUWWV485H362q/34JpQtU/1 x7hlqGEE95pMNNrDm7lN+0L3NZ37AOOJSy1yoU1Mr3Q3W/gqEikdx+S7PB4PptqbQM/n 9znFb+2UZlt7ZBCqOhEemSqKPB7j48tJW5jCLx+YKmDJJdgwsGxMSu7XpAvysCNKT9qE +3kMcjgAEQHi/VGzvmIeNusfIX8sfo8ZSvU0Je7EprxUgzw6Pqyym8nm2piK6oBTpPb9 pGWw== X-Forwarded-Encrypted: i=1; AJvYcCUYJowInzrci7eZWOkQIgM6DReFIQL07WmkgywAUloKKJ/2IK+DJv6gGlSlRwAQS3a49uMznPmgHxiRS2eN+OX4h/MjB2mxNrFv X-Gm-Message-State: AOJu0YyxmMovUukVbpb0U6e9zFYbw29gkuOgMNbuISezZ63l8UfmJ6/o H0aNWXhEqoqg5MY0CwnZqH/P2DpmI92d1a4apFIvbR9913CCOFAvTIvJqAP28Tk42iGMSY0HlNE w1Z/xm+0tqNiJdj6gOhQaTBXONQN1EqEMHPv+ACwXHmzGDq7F0CnWoULMVI2MlWHSvA== X-Received: by 2002:a05:6000:bd0:b0:355:3ec:c7e7 with SMTP id ffacd0b85a97d-35e84057ebemr1244195f8f.2.1717575381551; Wed, 05 Jun 2024 01:16:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHjZbVLQcTv5XWXPkHqha5v7vJWmf4YkZ6i75B+qQMTp1XC9toezuTporsM1/y1jErlQkDyeQ== X-Received: by 2002:a05:6000:bd0:b0:355:3ec:c7e7 with SMTP id ffacd0b85a97d-35e84057ebemr1244182f8f.2.1717575381207; Wed, 05 Jun 2024 01:16:21 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:20 -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 v7 05/13] PCI: Make devres region requests consistent Date: Wed, 5 Jun 2024 10:15:57 +0200 Message-ID: <20240605081605.18769-7-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 pcim_request_region_exclusive() as a PCI-internal helper. 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 | 52 ++++++++++++++++++++++---------------------- drivers/pci/pci.c | 45 +++++++++++++------------------------- drivers/pci/pci.h | 10 ++++----- 3 files changed, 45 insertions(+), 62 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index f199f610ae51..572a4e193879 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. */ /* @@ -394,21 +391,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 (mask_contains_bar(this->region_mask, i)) - pci_release_region(dev, i); if (this->mwi) pci_clear_mwi(dev); @@ -816,11 +798,29 @@ static int _pcim_request_region(struct pci_dev *pdev, int bar, const char *name, * The region will automatically be released on driver detach. If desired, * release manually only with pcim_release_region(). */ -static int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) +int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) { return _pcim_request_region(pdev, bar, name, 0); } +/** + * 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); +} + /** * pcim_release_region - Release a PCI BAR * @pdev: PCI device to operate on @@ -829,7 +829,7 @@ static int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) * Release a region manually that was previously requested by * pcim_request_region(). */ -static void pcim_release_region(struct pci_dev *pdev, int bar) +void pcim_release_region(struct pci_dev *pdev, int bar) { struct pcim_addr_devres res_searched; diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index b5d21d8207d6..e4feb093f097 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); @@ -3918,7 +3912,12 @@ EXPORT_SYMBOL(pci_release_region); 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; @@ -3934,20 +3933,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..9fd50bc99e6b 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -828,16 +828,14 @@ 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); +int pcim_request_region(struct pci_dev *pdev, int bar, const char *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); + /* * Config Address for PCI Configuration Mechanism #1 * From patchwork Wed Jun 5 08:15:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686358 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 52548190063 for ; Wed, 5 Jun 2024 08:16:28 +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=1717575390; cv=none; b=d1u1o8qVbstd56NEGLYwwo7kUa3KHANnx4zgeSX2ICbe3pv1ojZ6uE97I8ncsiN/zIs70tTqOIUs6gvEw1P6MsZR8P5RIdMQi8DM8qgM2ruZgiY+QZX501r7aI8AxVA5Sve9iK+1+Bq1/CWZvfPk0bVliZbz8HMzdDJlSFACls0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575390; c=relaxed/simple; bh=2mEh5RHmzkYaPBcMwiiWhHuqTZ5Dnp5rad7ZVzxAHNI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UPQmKATVK9kDWQCw2N5askBkpQgxR3fglXLItv+UhqZCTLDK4OjGCUVRRbaM0hR6hySrsT0sR9I15wuZBH5fxkzI5vNofJ0xCEKLATnbVEC2et6KubdptA//KSeyprgQC/015Oyviz/hKKvgRhnJfatsOfY+LQP7gRZ1WyEXauI= 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=VRsx80rd; 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="VRsx80rd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575387; 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=z8EKelm4Vr/z+BAYmppwUIfAU+aHPoZvlqPBVkyBuTo=; b=VRsx80rd1Zo5fzAVNWrifBgbtnrfswiuQdotWlIAcgrMaaouTJUVg47vzVMLft/hx7WJeY ukX+HRCwmAkcUuAxUD1K2MMwIA3YAi75gjQ9QgCf7rBiLK/4263wQaL2ZXZvAueFjFknmL y0ag/RZdJ9F0DgiT205Yoc3SZ11Iy/o= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-132-jr3gja_UN62Gnd7g2ItKcg-1; Wed, 05 Jun 2024 04:16:23 -0400 X-MC-Unique: jr3gja_UN62Gnd7g2ItKcg-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-35e0f2512ddso240529f8f.3 for ; Wed, 05 Jun 2024 01:16:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575382; x=1718180182; 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=z8EKelm4Vr/z+BAYmppwUIfAU+aHPoZvlqPBVkyBuTo=; b=JLOle9MMWMTH0UhF+zbEbR3WTEsNsTeE9QXgFgcHIR0HdvixaNB6nrCCDM1AtMNTjb ps5Z2BG7QL5h7fNisW42gBv2oeiWPUm4mUYH/r2S+g/5/Dm0bxOAQho5hKxeS76OIQ1k pxPKfGIEN0q5xdUMXnEKHO3IxJHNIunnRxsMf9EmMtOyKVhUpMW9J3nWGhHBhVgUp1s5 9NWgmYkQhrZl1oMMozvhgqDj+AmzPGSeWb9J5bw00XS3QZ1NH+VENDjOdsgKGkyEjN2L sGOLqPx7IGSozRaVgcZfK+0H13l8B7FYMqanF8v+UnJvzfsPnud9sqZtftPwDxrQlHAh beqw== X-Forwarded-Encrypted: i=1; AJvYcCU+L1DZjBRP5lD1m7+F9swoz0s2AlCu/UwsP+Om1ESNfDznZqVagfWtcZECyXo4PoxbGhldsJvOI/vJsMytgg/Z6ghGta2rPLGc X-Gm-Message-State: AOJu0YzhYIW+VQ5ueLHdwRrGxmeQHSWER66vF1GTapoD/pW52G13ztG+ X9t35i9vcFX9owXfcrvQbcoEvRGrrM2IfrMwG0FuxNeUp6ZFUDd/SOxO2Q6PYUA2O7dC5cejecl SJtfuFN2muGhNzV7B+8AOpW9/8TxGRhxYYFco6zir7YF9MknBSXKYgEHTjg== X-Received: by 2002:a5d:4a8f:0:b0:354:fa0d:1423 with SMTP id ffacd0b85a97d-35e840559afmr1138289f8f.2.1717575382410; Wed, 05 Jun 2024 01:16:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEUebdKcHHSvFhFyVCS++cJOWzefX/LKmBJWeBrOSXnXJYMyOYfKcerBpQ0n6FW3HDKgKUtbw== X-Received: by 2002:a5d:4a8f:0:b0:354:fa0d:1423 with SMTP id ffacd0b85a97d-35e840559afmr1138268f8f.2.1717575382085; Wed, 05 Jun 2024 01:16:22 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:21 -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 v7 06/13] PCI: Warn users about complicated devres nature Date: Wed, 5 Jun 2024 10:15:58 +0200 Message-ID: <20240605081605.18769-8-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 a bug (in 8558de401b5f) 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 | 16 ++++++++++++++++ drivers/pci/pci.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/drivers/pci/iomap.c b/drivers/pci/iomap.c index c9725428e387..a715a4803c95 100644 --- a/drivers/pci/iomap.c +++ b/drivers/pci/iomap.c @@ -23,6 +23,10 @@ * * @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_range(struct pci_dev *dev, int bar, @@ -63,6 +67,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 +114,10 @@ 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 +139,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 e4feb093f097..8dd711b9a291 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3897,6 +3897,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 @@ -3947,6 +3949,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 @@ -3954,6 +3958,11 @@ 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: It's 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) { @@ -4004,6 +4013,13 @@ 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: It's 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) @@ -4012,6 +4028,19 @@ 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: It's 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) { @@ -4029,7 +4058,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); @@ -4061,6 +4089,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. @@ -4070,6 +4100,11 @@ 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: It's 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) { @@ -4401,6 +4436,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: It's 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 Wed Jun 5 08:15:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686360 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 4875919148E for ; Wed, 5 Jun 2024 08:16:30 +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=1717575391; cv=none; b=Sp9qm3k1T/d/04TYRaxmE5KsQEb5fLVfUYkinMzsjX8Dk+AWgQHBvljnihwBkifOwHSUIEnMCZ9u2FL+74shVmUQQm1C3SOLIm9c2jY/ZXWBxdXO1e20kvyb7RM4H6a9l1MGGEmppJzTlgq12bf/94qo9DOflBuk23OthKqslKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575391; c=relaxed/simple; bh=MI0mRgfrILGP9+SXcTL9wGDcBl7tlRwscoQrU2TMa2E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oDSlZ3pWhH9YfaNyTxmJ/SKdYpLJM50IGuNAhwBB6dc1NdFpgzu4PWNoAEnoxLwezPE7O8jTJwsX2EY3y0u0H+4vZtglYEXJINkNW6Eq9bN58Fk7VXZhQyNO/ypOjxYpGkPSOErA8w6zjhiKHJryxlE6RbXDt6829wwhrZGWjcU= 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=IpzRLl8R; 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="IpzRLl8R" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575389; 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=vEB31yOtYoIre4MIRkuOCh7GA8J//awJNnm4ltdU4bo=; b=IpzRLl8RP1YwWfOwPhY29lJmU+hnzExNnC8GBKxQoow3bpg2KNy/PV1Pi3JwJdzXJ5H5Vu 1gvgqdjAt9QlQbZvvJgMN04mV63x2d9aD4dFQ2CektLX35QkewS4dQ+MXw0DqiEvctnJUd 3jqC38TiqfFUPfP5iNRzFpn7r8gZJlc= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-136-Aj7guTDRP9OpTskjxIvSQA-1; Wed, 05 Jun 2024 04:16:24 -0400 X-MC-Unique: Aj7guTDRP9OpTskjxIvSQA-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-354f30004c8so422184f8f.3 for ; Wed, 05 Jun 2024 01:16:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575383; x=1718180183; 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=vEB31yOtYoIre4MIRkuOCh7GA8J//awJNnm4ltdU4bo=; b=w2z8MZLP97MNeFfdavv1WDC4OlWcTLqkCVANSQNq8W6b4kxdlamMvGadVXqxwXWEww EUbjsyEuB2QhM2oC0ZFXzFmwCDURdYKObQB1oHa8Q/A8EgX3Xj55EkM/e07lMjGPN35/ hjXY7TdBLwNy/SetgQvmJ0UnVKktSP4PVNFWKX4/3bgRMeofnbyvN3NvGu1WmDgvGEkY ZQrkeHbY3EnvJTyTAnZ8M93ifVBkUjT1jYilRl0JHZBKp0KyukJD3rgb6QMjTzY6o8Sw O++cTNXvb9P89Ev8m/dWoCPfwn8yekiDnEg5/+q8t8A4480oR3ZJy5lY+DO41NrY+hK6 DhPA== X-Forwarded-Encrypted: i=1; AJvYcCXB4r+avs0KRmpqvXZn2zd3JeqHIJ50B4qlygtr7bpPjDox5OwJXIsonEYquML7SSahgpih897aJbnP3KMTUQVd6Tvmme/+nhlM X-Gm-Message-State: AOJu0Yz1G7BBMed7lt4KBesJ8ltDKoPFvWkNERnDy+h4b0tmlkyGv+fM zUcs7C3lqDwbjZg+CzJCFNwZIzlUQKLG/1J+Dyj5AzGhzm+CgHVN9FL2Px0rTMKSvTZWCge7Q4w Ygidsf4buJfVCzxWYUYMZR4b9yDmRF7EINPadmH+p4mOcmuvUYrunZpn5zg== X-Received: by 2002:a5d:58e5:0:b0:354:fa7d:dcf5 with SMTP id ffacd0b85a97d-35e83fd7bcdmr1226790f8f.0.1717575383553; Wed, 05 Jun 2024 01:16:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF6YjKkvVSUFoGcDbJ+SMXSxLwos65XaniDE58RD/+F87WgYW7Zh+OCWAsYEUg9t6JsH1PI+Q== X-Received: by 2002:a5d:58e5:0:b0:354:fa7d:dcf5 with SMTP id ffacd0b85a97d-35e83fd7bcdmr1226764f8f.0.1717575383238; Wed, 05 Jun 2024 01:16:23 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:22 -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 v7 07/13] PCI: Move dev-enabled status bit to struct pci_dev Date: Wed, 5 Jun 2024 10:15:59 +0200 Message-ID: <20240605081605.18769-9-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 572a4e193879..ea590caf8995 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -398,7 +398,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); } @@ -441,14 +441,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); @@ -466,7 +463,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 8dd711b9a291..04accdfab7ce 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 9fd50bc99e6b..e223e0f7dada 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 16493426a04f..110548f00b3b 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 Wed Jun 5 08:16:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686359 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 DEAB9191473 for ; Wed, 5 Jun 2024 08:16:28 +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=1717575390; cv=none; b=ongRaisnr1Mf9mm4fSDJybPgJVP4YfQxLUec0xTREOO7nPshJ93W2ZvFM1VWdD9GATwVow6GshWB5XzRrx/6xdU2JYQxvcsR3HdkQCkpESfgUhDhSmdfjk6yt8fNWgcS//q2FvyitI8AaUFHBg/c/H2jpVssm/B6RpmL263W/Kc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575390; c=relaxed/simple; bh=xl9J8hI645BO4dJlrgq+iFLF1dby15q/VE4JCvZfbaU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mGG9e6tKKJZCEp8sdk0lNsHX+X7HyIzKe8GMm4h8X9jIBBz5dpnBq4HS0VQlvercE9EBtSjilJU3dIO+zZiAjpi49iRZ9d+Hs6e7Lvvz0Q4vBjozahw0jE6GOJyM34d7u7oXRmukrhjVNZtKI7nW0BF94B3UbnTH4AKHq2UHUEc= 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=Hgz8oD+R; 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="Hgz8oD+R" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575387; 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=ADSvqA1AO3nQbqjqWDmd4LNpBLOwxbzjerhA1UP/7Ik=; b=Hgz8oD+RKFLss3YnHpzDmPSc5vv5+/WVDTilLt7g3rQSNZqlaYeYhRxbIvWBxqkdlasgd1 lVN+aBGrUTqmx6W2D/L39w/34MOejhepJwv0RCTqqJ93S5IonqMX077q+trBs1F8eYWv1L JUgZ3W0RRdtrot10gNWrwXY9e9fa1Qw= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-508-MfABXCxpN7asL6wpNRMr3A-1; Wed, 05 Jun 2024 04:16:25 -0400 X-MC-Unique: MfABXCxpN7asL6wpNRMr3A-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-35e532f32d8so213645f8f.2 for ; Wed, 05 Jun 2024 01:16:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575384; x=1718180184; 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=ADSvqA1AO3nQbqjqWDmd4LNpBLOwxbzjerhA1UP/7Ik=; b=Ry4DsVglhU5ZrY6/pIS4fqMzw5BV5vle0vVtAV8HUGWfJvx1+jZ9tnCZ9XCnfsJWIj LnFdqkuFHY9hOx0pWwe5PDRx67SYFAEMOSXj/Z6wke3WAJDEsuo4MLVDcTZM61jiIfhC 13cpNAKoCLNDDwK24H2+VNjQNT0x+LAZEDWtKkPdD30BanJtQBgsKEV7GL7YIQH3EcRZ wsFZYDPB0KpAgZYXryg9UOivOosGwlM3kznHVZA4XD6EOrpFyPvL+JK1p6L/cNjm6omb gdeJwozq3Mx/hagVAC2EgqV9j3rrK0xdUj5A9dYL/JGtrawLm8O9qQhIlEEIjlHDxlka DaIg== X-Forwarded-Encrypted: i=1; AJvYcCVWkUzJ/Nlh+daXIRbtoaqIYu98YDzmIW8GWYpOlboc+J7099NEp164pWrrBanBNo7frJCyIXOfCn+9fcfHW7k6gliZBnvsbxYb X-Gm-Message-State: AOJu0Yx49Ojk4SlweTy34tPmhTznV6fYtBVLQ4tTWRQdWgjC1Rw8Xofz DxpH80ra8Os2SU3zeiPC0+zjo8B8R61g3uuQb+YhSANsa9BgdHIYlLhSYXQYX6iqOljgfZWm9Qw c3bkDirkTOSQ4yooFJBI5bJqc1IxU0lgZytK0rhM0LjBmZOuaPJLF9xQxgA== X-Received: by 2002:a5d:4a8f:0:b0:354:fa0d:1423 with SMTP id ffacd0b85a97d-35e840559afmr1138371f8f.2.1717575384543; Wed, 05 Jun 2024 01:16:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHeYXXYRwMmc/wItTwuhNim5zt2S5LTm7hN7b8qr/oaCOGVDkg9BBwyql/IirFuwik1TYGpeQ== X-Received: by 2002:a5d:4a8f:0:b0:354:fa0d:1423 with SMTP id ffacd0b85a97d-35e840559afmr1138356f8f.2.1717575384289; Wed, 05 Jun 2024 01:16:24 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:23 -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 v7 08/13] PCI: Move pinned status bit to struct pci_dev Date: Wed, 5 Jun 2024 10:16:00 +0200 Message-ID: <20240605081605.18769-10-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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. 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 ea590caf8995..936369face4b 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -398,7 +398,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); } @@ -454,18 +454,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 e223e0f7dada..ff439dd05200 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 110548f00b3b..3104c0238a42 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 Wed Jun 5 08:16:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686365 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 A1F0019413F for ; Wed, 5 Jun 2024 08:16:35 +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=1717575397; cv=none; b=I9eqY/jfHBj1DTocfqhHkb0cMyK66SPqRmO89z/mNT0rFxSuf3bi+anBiLETygJsW756vXxv/u5ebugiokNxBg3yLyOC021aRuieHNQ4EmrVtC/odk7juzvYO/HFRxbDpft/2c2xhQMn6WqpDw/LuHMnnXAC752mtojn+Z/LOU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575397; c=relaxed/simple; bh=JHKzjaKvnMGmn24TasdPaKkcQ0DRRNWgFayHL5mcauY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PhfQnVhZOxKtf5l1xmdCa9/MFREiz7OkGdI2UmR+93beeZJ+vSUoPjzTiYsxg2gD8b8CZ8MAfxAc5m539G2gEsGYU2ihSmmP2yXCGiD1wN7VUFC8ZNj6CFvo16tP7qLhqwtzhDapmMGtJ8aVZj1ErnBGsUaPj6uuNfJq7Ua98RE= 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=ZFB8w8at; 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="ZFB8w8at" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575394; 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=aq3kAX8nkbuHPE3PafYDv6N/LvIBaqvSpbXV+kCTCaI=; b=ZFB8w8atV0LQ3tNEnjlagf1zcYpmGct+Aob4Rhn8PlwoNiLp4VVGPeC2MmDVwUhbqOOkAE acg6UxR/xe9HSmA0VSXLcGKOMRdPdMsgUvjLtNj//0KN+uSi5Xsq8wBCjNGI4VSvjBn3IF IN10hBQ5QS5XCCek1QGjehKwI4/mDYY= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-297-Jso3eCDiOaqlcJ_0p2rViQ-1; Wed, 05 Jun 2024 04:16:27 -0400 X-MC-Unique: Jso3eCDiOaqlcJ_0p2rViQ-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2ea91c7c801so6286941fa.2 for ; Wed, 05 Jun 2024 01:16:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575385; x=1718180185; 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=aq3kAX8nkbuHPE3PafYDv6N/LvIBaqvSpbXV+kCTCaI=; b=XUhtzxC1l3/eLNKR1ZSCGWTsGnAfNmiJerYQ0tkOfwhdv/iHEnsZ0VT/4XbAHXKLRE Rn7TbHygtZ0WasHpNQIDFjtyfZ0h5jIEClz+rwVlD1hetCr/hBUGcBUzMec/SnBCfaMo jkbOEr2wLYgoK/2rPLAZhqBlnteudqg0IoHKHwxAi0M3YkywJIKGN3XQv7F3lo1bO+08 hFFdGnfqMuiCt+e4P276gQgecEyRX5/RvzP2LcK2f1lxh2li6lPYzvVW+5UmgwYXBI+7 qsslSgHs7/tTMmeu8UMk4w0AyvsRwUzC4bE2uo+lK9e/ntxLwimnM2CnB0hhlHF+6Mkk SkNQ== X-Forwarded-Encrypted: i=1; AJvYcCUB1fkAuVb7536n/ahpKeG4/+5FM8Dqwvg7s4IJ9G2ltUFhxjnxwe9PdmVvwX4morsps0wg71j85+2k0H741iK0jJG/X3Fp8GOa X-Gm-Message-State: AOJu0YwpVwtS5063l/Zj3WfMym5MYWeHxJRymhQKpay8In8jMzQF6vLF JgMYYgnLRdE8O122G3By/Epf0KiSKurlej+OBoCLEKD8sT62B9geLhoEZJbv23XnJbABMwe6x1y SX/uxrWQC+oUtafyKTJNjVRK7NGk4bJp3pRb2NSa7BTCQhT5g84j6NOasGg== X-Received: by 2002:a2e:7307:0:b0:2ea:9449:7713 with SMTP id 38308e7fff4ca-2eac7775f64mr7590401fa.0.1717575385676; Wed, 05 Jun 2024 01:16:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFfUtE9m1ruCD6JqFnfuBOCwZgT/GH33J2kpoHwk2yAvGJmqDf2EMoDqqUDaKApN4Naw9gsyQ== X-Received: by 2002:a2e:7307:0:b0:2ea:9449:7713 with SMTP id 38308e7fff4ca-2eac7775f64mr7590191fa.0.1717575385284; Wed, 05 Jun 2024 01:16:25 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:24 -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 v7 09/13] PCI: Give pcim_set_mwi() its own devres callback Date: Wed, 5 Jun 2024 10:16:01 +0200 Message-ID: <20240605081605.18769-11-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 936369face4b..0bafb67e1886 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -361,24 +361,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); @@ -392,9 +402,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 ff439dd05200..dbf6772aaaaf 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 Wed Jun 5 08:16:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686361 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 74D58191491 for ; Wed, 5 Jun 2024 08:16:30 +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=1717575392; cv=none; b=RmPhHUJzSNMab7FJXEMkzltcVVo7tATqGW8HWviVSftCX5Tq4f8pZ+JEb7+Bwn9jTW2PRYpMwaJMhlaSVBPK/2KC4+Fji06V9dICXG4sLC/lESTW/2VMol/OcTiw932QinG1xfQf2cTVxZ/RZtmv3joxnPzVFarL5MGN5PIVZYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575392; c=relaxed/simple; bh=GTeBpuYLIPwI5Fq4sasuRQzg6LJKBh8wPFCl/+rcDMc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bEAfDHNK14P5r5R84Fu0bEIbDXRbzwEL1/XvzQ1zkL+CvpMBWUCRwj2hM35UuzTdBBN6jRsia6vr+cQ69lVFz2Fmq9uclEaGBeX08kFOMm5Jl7BJoCxaqN7Qvl6cB7gk7HLI9Ze1OKU+mTLR+amvc2kl/Tq5L9HUHNYCV7a5j4A= 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=GbtRy5Z6; 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="GbtRy5Z6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575389; 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=BD+Ne3TkytD/xWKk8A40YJpqJowHiHm8wUBRIwRMhbs=; b=GbtRy5Z6HII4WyCpxFySLUy6ScOx9RqtOwHHhWYguhmyd/PnskBHf4NpKZZ/bwz3I63NLP DfNwJZRVOyuXt5t1fPPcjj2Mac6o0x7TSy1r1RZMD9qooFIwvJfcNENi2whbGAr8ZlnWRx YxmzAFpLyKdCPKX32nITa0kW4ulKrAI= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-246-3WanoiuEP06JJaXkEc-org-1; Wed, 05 Jun 2024 04:16:28 -0400 X-MC-Unique: 3WanoiuEP06JJaXkEc-org-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-35e0f2512ddso240540f8f.3 for ; Wed, 05 Jun 2024 01:16:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575387; x=1718180187; 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=BD+Ne3TkytD/xWKk8A40YJpqJowHiHm8wUBRIwRMhbs=; b=NpbcLOFw2QcubReq9OnfXTaNpE4/p6XQ2T6eYuLzMmkZypzHMViyMr62nXN05jpLDg xB/g45V8Zz/GJh32nFUE1Xr2q5bOzGJlBrwpA9B7VhyIPpqddaf7r3WCBKF+hUjBOfpu RRw56h2vB5SCbIyspsmUzvjRH0uOuVtq69cCLanw1yKvW00yfoe1N7QIBSb9iSBi1dJb BM7jxT+VwGgI6zWULXPUBqhtrGJ5OJddpTIMssd402Npu8kNFpc3etqIZk1t0CTpEpAA F1j+LyO4yQN4hQSbAgIwLvammZ1uz8w76Bln46ae2obLNowjj8YW9bG/jSw44rtwnSdL oREQ== X-Forwarded-Encrypted: i=1; AJvYcCVli1qftMdMUB+2KBpu0aOORtqd3oRP5wjrI/ZF80Bbes/aDIgPGlQ5ARikudkUSc3G8rlm+2Ywlb1cGN1CIPfvBeCj1B0xJ/KA X-Gm-Message-State: AOJu0Yz2WEEeUGsDjZkjpuPiPFQ10TE3IVOr3L0jx7eLjnxj9cu5b2tu wRJ5TE6vQOljfbZW9uBEIVG3fE6MR+Bo+yUMAphB0lRb4M4AemE9qC/Bu2Ta+9kz2PW4z3eTse5 eQyF5SAkcHZSVdXZksnXlpr1eFSGcYVu0R3js4r+0E6fZvhFVr3gG97AaGw== X-Received: by 2002:a5d:456d:0:b0:35d:cf2b:9105 with SMTP id ffacd0b85a97d-35e8ef90f08mr1063107f8f.6.1717575386874; Wed, 05 Jun 2024 01:16:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEPnmKKMbioEu3OTnfk3Olk/HS1ljC/0uD3ilJbavzKouLbvntLOj9vIGtcz/0ZnPMjStdobg== X-Received: by 2002:a5d:456d:0:b0:35d:cf2b:9105 with SMTP id ffacd0b85a97d-35e8ef90f08mr1063089f8f.6.1717575386552; Wed, 05 Jun 2024 01:16:26 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:25 -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 v7 10/13] PCI: Give pci(m)_intx its own devres callback Date: Wed, 5 Jun 2024 10:16:02 +0200 Message-ID: <20240605081605.18769-12-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 drivers via a public API. Implement pcim_intx() with its own device resource. Make pci_intx() call pcim_intx() in the managed case. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 76 ++++++++++++++++++++++++++++++++++++-------- drivers/pci/pci.c | 23 ++++++++------ drivers/pci/pci.h | 7 ++-- 3 files changed, 80 insertions(+), 26 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 0bafb67e1886..9a997de280df 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, @@ -392,32 +397,75 @@ int pcim_set_mwi(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_set_mwi); + static inline bool mask_contains_bar(int mask, int bar) { return mask & BIT(bar); } -static void pcim_release(struct device *gendev, void *res) +static void pcim_intx_restore(struct device *dev, void *data) { - struct pci_dev *dev = to_pci_dev(gendev); - struct pci_devres *this = res; + struct pci_dev *pdev = to_pci_dev(dev); + struct pcim_intx_devres *res = data; - if (this->restore_intx) - pci_intx(dev, this->orig_intx); + pci_intx(pdev, res->orig_intx); +} - if (!dev->pinned) - pci_disable_device(dev); +static struct pcim_intx_devres *get_or_create_intx_devres(struct device *dev) +{ + struct pcim_intx_devres *res; + + res = devres_find(dev, pcim_intx_restore, NULL, NULL); + if (res) + return res; + + 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 04accdfab7ce..de58e77f0ee0 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -4444,11 +4444,23 @@ void pci_disable_parity(struct pci_dev *dev) * This is a "hybrid" function: It's 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) @@ -4456,17 +4468,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 dbf6772aaaaf..3aa57cd8b3e5 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -823,11 +823,14 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) * then remove them from here. */ struct pci_devres { - unsigned int orig_intx:1; - unsigned int restore_intx:1; + /* + * TODO: + * This struct is now surplus. Remove it by refactoring pci/devres.c + */ }; struct pci_devres *find_pci_dr(struct pci_dev *pdev); +int pcim_intx(struct pci_dev *dev, int enable); int pcim_request_region(struct pci_dev *pdev, int bar, const char *name); int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name); From patchwork Wed Jun 5 08:16:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686362 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 D4FC8190044 for ; Wed, 5 Jun 2024 08:16:31 +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=1717575393; cv=none; b=FZ8WOMOGOF8Ut8sLSbehqDbMDbLVlWAHefWp3t0cPcY/hH7qAznNFbxVSbGPXd3sRtcztVMK6fVwuRkz3zJecDvdkN337VHdOyY1ase83g4trBPR/l2BZxnLjXOIcqUqZKEbPyQfU8rDnCl62KUPFfT+bKHUW88zy+jEgw4erLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575393; c=relaxed/simple; bh=LIhWjlBnujZsrqNrxIabQocrBEUnZjpD1itpnCtbE8M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tvh37jeyyBBmobxOcXw410ipVF4bnbPb2JDY4KDQRE6tEI8gwqXF9gDq44hyGlbE4TM09Ks95gu7u5BFj0tA732decQYh/ThQgrJjV+I+ZGB2puOto+9L9TRnHS7eEYfr1FhHwmywSjgsCTwkqXonFOyvNQklP3JTa2+NEBnaQA= 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=cQMbPaWg; 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="cQMbPaWg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575391; 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=Ieun1xthf8lb2oACtUs9qte0ySrDx5Lfq1im4lgm338=; b=cQMbPaWgMNjMSsU5hgSh2EtmopJu5UVOYAdKETueF0RKIM6w9R65fR6z3ly7Oxq9GMlHxL nfStvZtt337SRRv4pEEkIvWS20djXI6P15b2fyB08j0JIGzluOVrZ/Y5CZQj7m36QXO8na rI9/1JSjC+tDoGefSJXSVVqdVQm5yGE= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-378-39QnowwJNyOQY682JD7sUA-1; Wed, 05 Jun 2024 04:16:28 -0400 X-MC-Unique: 39QnowwJNyOQY682JD7sUA-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-421599b8911so76035e9.2 for ; Wed, 05 Jun 2024 01:16:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575387; x=1718180187; 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=Ieun1xthf8lb2oACtUs9qte0ySrDx5Lfq1im4lgm338=; b=KOlbIRz7DWnLY5TChQw+qQGhQ6XKtdfbYsDw6ddyjCB0k26H1CkvepKd7X3Fb/szKr iSQQ9+rQ9KmbFOSU0JzOA60Iqoi7U0GdaS4CIhBkbiu2z/FeWUwiGrLod0gcgqpKz1XY kXrFwe2wymioi+KT5NMt9LspTlOxAxurBdz7Fc3ADpMBFf++K3zHD1LXebwy2sVRXhCz Vn+cXWmCLHUjCpgDMznZ/sq4c87M2CaD2ApqhxcehLjIBQDxup+byqlI7mIKNPkk8QjH m9puzvWLzSs64y2O2U3ExQUxOTAbhwqy9TVXfPjN7RjHfW6fSmpoAB/tFbvRdy3fLawO Qk2Q== X-Forwarded-Encrypted: i=1; AJvYcCURXrTPQj4x+z6hYi1PVjyMPIYxZP29rzHr7TzxdC3VH/VtOqSlB7ruiEhl6z4EmwJ2uzG6NAifRLbtBtX5UhK6NaNqyXTfz9GE X-Gm-Message-State: AOJu0YwGH5e+pkzJmlrzfFGYeq5uxQR8L4YBVGw76GbjTFeUTQmm5qtg QzAwXQj3vYm/wNOaBzsxZu+o9UTR5dV59ny424bwBDHGmUoyWnaz9b9b8ufHD1Wnx8qrjlv27a6 wfhHRUyqvfHxEpKAOucww6bTmHKn2LNJYfKtg4p9GeUUK71vhyOfaQrQZEA== X-Received: by 2002:a5d:49c5:0:b0:354:f768:aa00 with SMTP id ffacd0b85a97d-35e8ef8f11amr1107244f8f.4.1717575387781; Wed, 05 Jun 2024 01:16:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7QWZ+wzLRj89HwPmCo4cwm181tYKPOfVL2iFfJrmO0D+uX56tjbvKzLXtnA0YMX0uy5FvKA== X-Received: by 2002:a5d:49c5:0:b0:354:f768:aa00 with SMTP id ffacd0b85a97d-35e8ef8f11amr1107226f8f.4.1717575387480; Wed, 05 Jun 2024 01:16:27 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:27 -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 v7 11/13] PCI: Remove legacy pcim_release() Date: Wed, 5 Jun 2024 10:16:03 +0200 Message-ID: <20240605081605.18769-13-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 PCI devres implementation. Replace pcim_release() with pcim_disable_device(). Remove the now surplus function get_pci_dr(). Remove the struct pci_devres from pci.h. Remove the now surplus function find_pci_dr(). Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 53 +++++++++++++++++++++----------------------- drivers/pci/pci.h | 18 --------------- 2 files changed, 25 insertions(+), 46 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 9a997de280df..271ffd1aaf47 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -460,48 +460,45 @@ 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 ret; + } - return rc; + pdev->is_managed = true; + + return ret; } EXPORT_SYMBOL(pcim_enable_device); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 3aa57cd8b3e5..6a9c4dd77d68 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -812,24 +812,6 @@ 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 { - /* - * TODO: - * This struct is now surplus. Remove it by refactoring pci/devres.c - */ -}; - -struct pci_devres *find_pci_dr(struct pci_dev *pdev); int pcim_intx(struct pci_dev *dev, int enable); int pcim_request_region(struct pci_dev *pdev, int bar, const char *name); From patchwork Wed Jun 5 08:16:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686364 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 B66B8191491 for ; Wed, 5 Jun 2024 08:16:32 +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=1717575394; cv=none; b=DZn2mCKJfgfiegU+pgOTivtX6OlDMOQYNNTE3b6g/paOTmLGylPUoKuuZXeCpdFVUCYbA3Ps4nGpOS4bIVv7VlY5YtWQR/AK5pC8nC0UUO2u+QW4nTNfCWZKsYWYoyW/EYCdVLjDQvvIIvskFk5s0cvWtk+1uoQzZFbQiZd1uJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575394; c=relaxed/simple; bh=JMFvIAK97CVO0WYF9x4f5zI6n4vzeleom/TOQDaGhvo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EJI7EGBDLI1rcA4Gqan6P8VDGnWt9gfujZVvY77z4ayug1yjkiM5JclTi7GTCfDDctrKDCyE2pkS66v+WlF/KtajB1YRzUjGKrQoLitG6pgzB57pKf7bipE932NRRsXWt4EsNBSpSpwffM/6J4jeq6jwIiVxbnqVu5e7FQXwAbo= 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=JqAh+kbd; 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="JqAh+kbd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575391; 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=IEuAQpiyFkRTWU9yECucUjg03zi15bHltjbT7Mp+Diw=; b=JqAh+kbd2mRqMVSSEDylIENeWpKxtoeF1frMvtjPru0J0LfUxOqvrHQM0deCn0xeg8dy5q QD78NKr3uSFG+WxvEmK1+pPhPH2umv0bzJRCS893AYVC7+gLhsBOqyLsNpuM0Ty9I7LwRn DZvJ0lrR5bSP9PQNFeJwIXe/WCfyPZE= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-625-3apcepXPPyO75wE-oR2YRg-1; Wed, 05 Jun 2024 04:16:30 -0400 X-MC-Unique: 3apcepXPPyO75wE-oR2YRg-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4210e98f8d7so5392645e9.3 for ; Wed, 05 Jun 2024 01:16:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575389; x=1718180189; 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=IEuAQpiyFkRTWU9yECucUjg03zi15bHltjbT7Mp+Diw=; b=WL3WP31KPMulD6/tf4ODqoBDJjRFzDWHHM+Nh0IHLUD9555/LiXkBRtFM9Lh4MzqgC B36UPl5UuxTn/KgX+1DwwUtGfSfXfA6eW5lbHOVPUh4ZSXvT0U9TaPq8dF3+f3Ys6XHx 2VSxz+eViG/pQi7ip/PCsY9mfnEK5gVtlzA/pXKil2A1DxZkufk+L9K6vT4nmaWEEbdT vDOIdWUrj4C180aTzS6veF9ElCjPJeNCpWgMJeTqP60jLAzFcRqc6mRxwyMVWRlOXCVi PMborstLoxcDJyHLzCjD+05pejAu7Cggh3kyLbAE/WbSCW8yVmlwBuVYuNvQ+oDzM+uI SnZg== X-Forwarded-Encrypted: i=1; AJvYcCU9xISu0uj97IGJAricCXrkYGuvqYvyZ/XzM1DEzsNxm2l5RDsHTNj/Ss/P1tG4xSQoEMjwAie9xYqJGf5smi15UAmD0nUC2lll X-Gm-Message-State: AOJu0YxHjbISTOPCbaySxJ3Vy9NkiyD9wsypqFsf/rGXojTQjso6HeD6 IZYrmogT/Cv6OCAcinAMVb6PgVh1VmBZqjsmBOb5QRUp5usTDHsgmM8f9KYH+oznE2DPe9/oKWd RrvguljYKG/ud6wHv/hAp/4lTEd3AeGXecK8FH0Gh3s47fjnQZSBywyaSbg== X-Received: by 2002:a5d:49c5:0:b0:354:f768:aa00 with SMTP id ffacd0b85a97d-35e8ef8f11amr1107291f8f.4.1717575388870; Wed, 05 Jun 2024 01:16:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnjANmPeJW2fIkqitgTnO6d1I0TF2FhQLz3U4Vz0ir5jlCvF+wcPqPTc7AeKCFcFuNtzjdvA== X-Received: by 2002:a5d:49c5:0:b0:354:f768:aa00 with SMTP id ffacd0b85a97d-35e8ef8f11amr1107276f8f.4.1717575388585; Wed, 05 Jun 2024 01:16:28 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:28 -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 v7 12/13] PCI: Add pcim_iomap_range() Date: Wed, 5 Jun 2024 10:16:04 +0200 Message-ID: <20240605081605.18769-14-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The only managed mapping function currently is pcim_iomap() which doesn't allow for mapping an area starting at a certain offset, which many drivers want. Add pcim_iomap_range() as an exported function. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 2 ++ 2 files changed, 46 insertions(+) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 271ffd1aaf47..5ddcfe001d08 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -1007,3 +1007,47 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) } } EXPORT_SYMBOL(pcim_iounmap_regions); + +/** + * 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); diff --git a/include/linux/pci.h b/include/linux/pci.h index 3104c0238a42..f6918e49ea5f 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2329,6 +2329,8 @@ 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); +void __iomem *pcim_iomap_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 Wed Jun 5 08:16:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13686366 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 E0F59194147 for ; Wed, 5 Jun 2024 08:16:35 +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=1717575397; cv=none; b=PUubV8lPHN7fi6/nI/HjrKjCtAwqDyWqoyM7eY6Aoz8McPafkeHut1A0jZhAKBskggS0XXhIxJm08SYS5QYWHcl31NHzSlrxbefDzNLG0xoc23Mf39cT5+ZOuvjE/5wslFP1/ZN4uR6zBZtbIyhXMMHiBkGtUCY69lItrlbAuqs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717575397; c=relaxed/simple; bh=iGO9d2tDV8jgMXAb9ljo4BmxYhAXls7ncq8pchC9GTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZNcSB80McfdeOFG5qvFNFSXBm379/2v5CBNLmCyH+208prpzxNQWA6KTVOenZExy48wOOSJG5aIddiWQvxZ/t0m144Vpy2nWrxUQLgQR1ERhy1a+PPUBe5IgxdR1Ro2miae782ev5u7jPtH3yuzlRI/8iaRZm+ou6/Q9+SIR9Yw= 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=PS2vtOUd; 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="PS2vtOUd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717575395; 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=9WQJKGGQ63IpYpwAqks0j3zwgngy8EFW/hBQkkDLpOw=; b=PS2vtOUdeYss/n2HngzviDg6RSSXO/9F4CnyRbGU4JJkFjpQ0R/Kr0Emk58oN379Ow0QeY Te1kK4GPwPwYL1GapfsgMm6mDtDndnsuo8GLkc3gv6rhj/jlUtwftPxHvQim6kJBxmizWv fpzUMmL+RqaRCERDsoZCkKeqKpZbUSY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-665-eQnOHsO5MvetbGqOw_xD0Q-1; Wed, 05 Jun 2024 04:16:30 -0400 X-MC-Unique: eQnOHsO5MvetbGqOw_xD0Q-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-42110b33fecso7980905e9.0 for ; Wed, 05 Jun 2024 01:16:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717575390; x=1718180190; 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=9WQJKGGQ63IpYpwAqks0j3zwgngy8EFW/hBQkkDLpOw=; b=MI7CXB8J617T7V5QkBId5ykAwWTSijaxfMIWULy3Ug4Wr8yRUVNEDPh3bEGu3wjt7U Hfj0JRvICFcJypvvPJelAlJhjsnrRNqH62y1KYReq+VnrEO9GwbWlp220h/5HRoPuZG2 Eyhvr85mjidw2X1Dn+Nv5qRmOhV1qyVLbFjEu1fvgxpV2FhzIw7Ny+M0gdVC5zrCTPNv g/Sm4ZsDXEneA1bQn3BKjDN0Koe/cFubXU3zloBgsupxWMckBw97MQQYLE3dbB9wBuaB qcMh0GcN4L4PBOzb/knIqIrcVcnmAGz+sMtwef0NCvo/9V4ZVPPuUsoZy7syUS53qaBJ poww== X-Forwarded-Encrypted: i=1; AJvYcCVL7X/CzbuZLIU1dcJmoWNZHa/lmmlnrZrHE93KL9Mx1bMW3ShE/lurixQzHal7kUnBW2iik9NvmqAwP54oASB+DM7WJZFxky1B X-Gm-Message-State: AOJu0YxcvwcKgR3x/nYOwxWMbLxBKfrLS7C1ZUZ4gClmZl7Hsrrg39sO 8UFedwMCqgyQNYNZ5ypg1i7pQ5vGX7qJajTS9pK11z1OrgnpCy3qosYU7Y+5nw3UYAWeBJV+7d+ y7vFwx1PsLQBikaa04lNVXnVBssTXdzjakBcQ83F13AKBDdT95Ri5qB8azQ== X-Received: by 2002:a5d:404d:0:b0:35e:83dc:e6ed with SMTP id ffacd0b85a97d-35e83fcb079mr1241481f8f.0.1717575389877; Wed, 05 Jun 2024 01:16:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFjXIQcWcnmum91rBphrXn5QVRVx0JrE+Ei0yMho9Onfe+TO5zfd469TFQpM4baXxySxHqFTA== X-Received: by 2002:a5d:404d:0:b0:35e:83dc:e6ed with SMTP id ffacd0b85a97d-35e83fcb079mr1241464f8f.0.1717575389558; Wed, 05 Jun 2024 01:16:29 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.fritz.box ([2001:9e8:32e6:e600:c901:4daf:2476:80ad]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd0630010sm13739163f8f.76.2024.06.05.01.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:16:29 -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 v7 13/13] drm/vboxvideo: fix mapping leaks Date: Wed, 5 Jun 2024 10:16:05 +0200 Message-ID: <20240605081605.18769-15-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240605081605.18769-2-pstanner@redhat.com> References: <20240605081605.18769-2-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 Reviewed-by: Hans de Goede --- 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,