From patchwork Mon Apr 8 08:44:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13620781 X-Patchwork-Delegate: bhelgaas@google.com Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37ADB41AAB for ; Mon, 8 Apr 2024 08:44:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565882; cv=none; b=oSbs5pVoN5dXs0sqwCW93NIVYxTC8+b8qsKl/uzvaleAh0f9LURC6AUM62d1Ll9GOv61YbLJT9JIakTDb2C0s+rhcbrmEDH+UGkBzM+PV6ug/Eh0uytr+fihiZTsK3Y0oMcSUf65MKlEUK/59TyH4ZTLWWqdTjP+gQsvo7IcGHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565882; c=relaxed/simple; bh=Nx8ZzfGxcQC1AGrh9J9RS74k63XDfPWg20NLbbgS/GY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hwsgKjQYGJNboxfhM0F8GKu3XY0+bT5/kf0EMccoMnxWmoI+TB4zonbgzRXMYJ8471Bplb4KGkCo3m+z4z5tMkf6Emf2vslfJyzqgUcqxMRu3OUetSbAdclwMvuGuZtm4n6LrYF20U34qBwMGch6g77s3xGUUwH0ReyOq7HBrg0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=e/dn2gSG; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="e/dn2gSG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712565879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GIYPXBXCIxl+X8+VfSMdqKBrLQLRX+Ac31hhra0G7UY=; b=e/dn2gSGtj2tbRqHXXD/cDh4thPS4aG3vxKZj3bCAoZ0ZjFbPHmxIohc1TLKL+btg9TGHx 0LlTDyCWZ30Mp1hqL1gKMi9hkhefsM5sDYX2VRVzASWUYC3fuc0oyCunOoNUUD6ZGlR0LC tNpret8Z6KwlNoxJqh6tC5CUS5kcQpQ= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-503-xVfOuCZbO3GGBeWl2K8LUg-1; Mon, 08 Apr 2024 04:44:38 -0400 X-MC-Unique: xVfOuCZbO3GGBeWl2K8LUg-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-78d5267ae96so8516685a.0 for ; Mon, 08 Apr 2024 01:44:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565877; x=1713170677; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GIYPXBXCIxl+X8+VfSMdqKBrLQLRX+Ac31hhra0G7UY=; b=ey8hiX6sYwaIJCK26+cOjL0pQu4ZsrNOL3LmIoFm7p03IXXE+6IhpFYcI/QPGNRXQJ 3gL3In/EZ8Zm907dWDj2/nODsMgQkoo/U4UgI+hrG8X9nNjLh5qkBAwWDD7SYTtZatC4 ev7lUrPy1Z1goRo694p8VJxPf3MvmSO/ylw/J/K8G6kH0zZWgRqPyjwctDxoZMR/pfFk Z8PsT3Dufuvlgg4umLNrDMU+mbs0rIKiqd8ZX4wefRoIwxtNnTZDlZ3pVwiuWsFrLEld d98OjVc7NUo0W/loz7Qv+ROCxgC4lHE6AbDlmfe+F33xx4YzbYE6fnFlOWcPnJREqtNj kyWg== X-Forwarded-Encrypted: i=1; AJvYcCWJWQucv9IQNR8B17f4O4/x56I2hIQJF0Ajn8znPq1OloQGp0TG1JINHls8yUlshXDHn81+kIqCzk1NJNi0JkmmTZP/iGmsPW0Z X-Gm-Message-State: AOJu0YxDJXR3Riyy3uTlCu+I3RkqcxvXxxcTBMcJBu4U/6WevDXsSt/U CcqO7QRrBih8YQVGlyqu2dT4er58AmmAEXrLMBc/F4OHNDj8yMKb5nhBT4cBhqKwxoTKcJqG2mi 127PGlD5E7SzYMms7kpRgNcUu4DkEOFWijoHaaMhXBdxcgZloqplPNcemmkM2X3ZUVA== X-Received: by 2002:a05:620a:4093:b0:78d:641d:3db2 with SMTP id f19-20020a05620a409300b0078d641d3db2mr3325711qko.2.1712565877366; Mon, 08 Apr 2024 01:44:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGM+65Ce5bZ9ywcqEtAlGoPOBNhPm/or5dJ5w/u7NYnrVxPZH6uzzTULD/Tr3RGSLPS//Ek5w== X-Received: by 2002:a05:620a:4093:b0:78d:641d:3db2 with SMTP id f19-20020a05620a409300b0078d641d3db2mr3325696qko.2.1712565877008; Mon, 08 Apr 2024 01:44:37 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id oo15-20020a05620a530f00b0078d54e39f6csm2036989qkn.23.2024.04.08.01.44.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:44:36 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v6 04/10] PCI: Make devres region requests consistent Date: Mon, 8 Apr 2024 10:44:16 +0200 Message-ID: <20240408084423.6697-5-pstanner@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408084423.6697-1-pstanner@redhat.com> References: <20240408084423.6697-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Now that pure managed region request functions are available, the implementation of the hybrid-functions which are only sometimes managed can be made more consistent and readable by wrapping those always-managed functions. Implement a new pcim_ function for exclusively requested regions. Have the pci_request / release functions call their pcim_ counterparts. Remove the now surplus region_mask from struct pci_devres. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 49 ++++++++++++++++++++++--------------------- drivers/pci/pci.c | 50 +++++++++++++++----------------------------- drivers/pci/pci.h | 6 ------ include/linux/pci.h | 1 + 4 files changed, 43 insertions(+), 63 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index bd24bad187d9..d9cd7f97c38c 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -22,18 +22,15 @@ * _sometimes_ managed (e.g. pci_request_region()). * Consequently, in the new API, region requests performed by the pcim_ * functions are automatically cleaned up through the devres callback - * pcim_addr_resource_release(), while requests performed by - * pcim_enable_device() + pci_*region*() are automatically cleaned up - * through the for-loop in pcim_release(). + * pcim_addr_resource_release(). + * Users utilizing pcim_enable_device() + pci_*region*() are redirected in + * pci.c to the managed functions here in this file. This isn't exactly + * perfect, but the only alternative way would be to port ALL drivers using + * said combination to pcim_ functions. * - * TODO 1: + * TODO: * Remove the legacy table entirely once all calls to pcim_iomap_table() in * the kernel have been removed. - * - * TODO 2: - * Port everyone calling pcim_enable_device() + pci_*region*() to using the - * pcim_ functions. Then, remove all devres functionality from pci_*region*() - * functions and remove the associated cleanups described above in point #2. */ /* @@ -399,21 +396,6 @@ static void pcim_release(struct device *gendev, void *res) { struct pci_dev *dev = to_pci_dev(gendev); struct pci_devres *this = res; - int i; - - /* - * This is legacy code. - * All regions requested by a pcim_ function do get released through - * pcim_addr_resource_release(). Thanks to the hybrid nature of the pci_ - * region-request functions, this for-loop has to release the regions - * if they have been requested by such a function. - * - * TODO: Remove this once all users of pcim_enable_device() PLUS - * pci-region-request-functions have been ported to pcim_ functions. - */ - for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) - if (this->region_mask & (1 << i)) - pci_release_region(dev, i); if (this->mwi) pci_clear_mwi(dev); @@ -964,6 +946,25 @@ int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) } EXPORT_SYMBOL(pcim_request_region); +/** + * pcim_request_region_exclusive - Request a PCI BAR exclusively + * @pdev: PCI device to requestion region for + * @bar: Index of BAR to request + * @name: Name associated with the request + * + * Returns: 0 on success, a negative error code on failure. + * + * Request region specified by @bar exclusively. + * + * The region will automatically be released on driver detach. If desired, + * release manually only with pcim_release_region(). + */ +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name) +{ + return _pcim_request_region(pdev, bar, name, IORESOURCE_EXCLUSIVE); +} +EXPORT_SYMBOL(pcim_request_region_exclusive); + /** * pcim_release_region - Release a PCI BAR * @pdev: PCI device to operate on diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 9d9d09534efe..c0c1ee17a06b 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3869,7 +3869,15 @@ EXPORT_SYMBOL(pci_enable_atomic_ops_to_root); */ void pci_release_region(struct pci_dev *pdev, int bar) { - struct pci_devres *dr; + /* + * This is done for backwards compatibility, because the old pci-devres + * API had a mode in which the function became managed if it had been + * enabled with pcim_enable_device() instead of pci_enable_device(). + */ + if (pci_is_managed(pdev)) { + pcim_release_region(pdev, bar); + return; + } if (pci_resource_len(pdev, bar) == 0) return; @@ -3879,20 +3887,6 @@ void pci_release_region(struct pci_dev *pdev, int bar) else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) release_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)); - - /* - * This devres utility makes this function sometimes managed - * (when pcim_enable_device() has been called before). - * This is bad because it conflicts with the pcim_ functions being - * exclusively responsible for managed pci. Its "sometimes yes, sometimes - * no" nature can cause bugs. - * - * TODO: Remove this once all users that use pcim_enable_device() PLUS - * a region request function have been ported to using pcim_ functions. - */ - dr = find_pci_dr(pdev); - if (dr) - dr->region_mask &= ~(1 << bar); } EXPORT_SYMBOL(pci_release_region); @@ -3920,14 +3914,18 @@ EXPORT_SYMBOL(pci_release_region); * NOTE: * This is a "hybrid" function: Its normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. - * This hybrid feature is DEPRECATED! If you need to implement a new pci - * function that does automatic cleanup, write a new pcim_* function that uses - * devres directly. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name, int exclusive) { - struct pci_devres *dr; + if (pci_is_managed(pdev)) { + if (exclusive == IORESOURCE_EXCLUSIVE) + return pcim_request_region_exclusive(pdev, bar, res_name); + + return pcim_request_region(pdev, bar, res_name); + } if (pci_resource_len(pdev, bar) == 0) return 0; @@ -3943,20 +3941,6 @@ static int __pci_request_region(struct pci_dev *pdev, int bar, goto err_out; } - /* - * This devres utility makes this function sometimes managed - * (when pcim_enable_device() has been called before). - * This is bad because it conflicts with the pcim_ functions being - * exclusively responsible for managed pci. Its "sometimes yes, sometimes - * no" nature can cause bugs. - * - * TODO: Remove this once all users that use pcim_enable_device() PLUS - * a region request function have been ported to using pcim_ functions. - */ - dr = find_pci_dr(pdev); - if (dr) - dr->region_mask |= 1 << bar; - return 0; err_out: diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 171884aba8e1..040ed2825554 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -828,12 +828,6 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; - - /* - * TODO: remove the region_mask once everyone calling - * pcim_enable_device() + pci_*region*() is ported to pcim_ functions. - */ - u32 region_mask; }; struct pci_devres *find_pci_dr(struct pci_dev *pdev); diff --git a/include/linux/pci.h b/include/linux/pci.h index 5782ad034178..0f203338f820 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2330,6 +2330,7 @@ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name); void pcim_iounmap_regions(struct pci_dev *pdev, int mask); int pcim_request_region(struct pci_dev *pdev, int bar, const char *res_name); +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name); void pcim_release_region(struct pci_dev *pdev, int bar); void pcim_release_all_regions(struct pci_dev *pdev); int pcim_request_all_regions(struct pci_dev *pdev, const char *name);