Received: by 2002:a05:7412:8d11:b0:fa:4934:9f with SMTP id bj17csp575182rdb; Mon, 15 Jan 2024 06:48:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGvtnERDV0T5fzvm3RS4ptJpVAMtWfQwmgheuD1YDf0Cli2l/F8n51hI509cTUCmOctbtbG X-Received: by 2002:aa7:d5c7:0:b0:557:2be5:6620 with SMTP id d7-20020aa7d5c7000000b005572be56620mr5929316eds.23.1705330112484; Mon, 15 Jan 2024 06:48:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705330112; cv=none; d=google.com; s=arc-20160816; b=bmJsIP6KhLu5wJeY8UcetGqWcNCQs2aAHG2ZWBW5gBmxtfdC8v+pfbeE0l5hBGMFXX XOcniloaQoDPsEA0BtobhgsMj8PVYROZe7j0qnBbfOS6XDqqoP0iPnkSSiNXdlwMnenZ zmad/QFzNm2m5aKQPVIoIY4oNanWgStm70dhkYVbJNwQFCYGOFiO2ctr+4kwsioQ3DDW +66CRPfu9f/YcPsZz8i21/9gP6Iavlb723mvf996uLoP7oBttcFNmz95S9qLF1wwQnvM sBze+SuKAkeu8y0gkxEGwxo6p3z3qjsLR3vHpKjmrDaVLa4yeE1uOk1VF9O/ZsN+i3zj 5tqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=GuPy6o0xdtswTFzGt//PWEkxlEqwBN5Ui7YfUNE5A+E=; fh=AY1J9PDScN0xrIvhrMJy8TMRSbPeq4owI1m0lsalYQU=; b=heO14mUqAzRoF22g6x0tbJnX6nGy2YyfRhvoavJ25NeELfvQ/JUClFXTMGNCuv5A+y JJq2ORzG4H86FQfdfliNYBpGR/3o0i//o6lJ6AFq9n02cqeuSJFm3i3fQDPhOC1hvDvN PlM0sEZfYMW5irbclorSBjLGddRolrHBWgecaCXdaqBhqGco7pzySwiW8tnNDGCth1Gb hnLVfVNPlZ2/+rUs52bOzORz/dayVxiawuRtFPkgTXF4qS/+6fbyXmfh+XT5PYoowwua 3Xkd/WrS0TeDOAswxhEfgmE0ETBjlj2KoEhZ69OZRYHDVyfSeUYbrAVVxWdxWAq5Aqp0 nVWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SV7SESBy; spf=pass (google.com: domain of linux-kernel+bounces-26128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d27-20020a056402401b00b0055872d1419dsi4039966eda.230.2024.01.15.06.48.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 06:48:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-26128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SV7SESBy; spf=pass (google.com: domain of linux-kernel+bounces-26128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 175641F229C8 for ; Mon, 15 Jan 2024 14:48:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C89817BBB; Mon, 15 Jan 2024 14:47:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SV7SESBy" 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 B86E81799F for ; Mon, 15 Jan 2024 14:47:25 +0000 (UTC) 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705330044; 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=GuPy6o0xdtswTFzGt//PWEkxlEqwBN5Ui7YfUNE5A+E=; b=SV7SESByT91/P8alxXhjdOmMPktimB418QKhoVC3H4zRIo7RIZ5Ubv4Z0WNMRN1IRC3V+z xG4NveWgKebKrN/KpmLjR59PsBh2gaIFgH2K3DgDOoyP4NhOP3Ib4+GBzfAjDXsXWrBJr+ W8t/GQ87c5te2kglQNGoC4XyLXM7HCg= Received: from mail-ua1-f70.google.com (mail-ua1-f70.google.com [209.85.222.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-MwaY5k1tNu-PaaaSI2QuRA-1; Mon, 15 Jan 2024 09:47:21 -0500 X-MC-Unique: MwaY5k1tNu-PaaaSI2QuRA-1 Received: by mail-ua1-f70.google.com with SMTP id a1e0cc1a2514c-7cdaffe6b9aso377456241.0 for ; Mon, 15 Jan 2024 06:47:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705330041; x=1705934841; 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=GuPy6o0xdtswTFzGt//PWEkxlEqwBN5Ui7YfUNE5A+E=; b=FqSPvc6KqJgTrURorjBfXpeA98dp92vvhnznjuFUl6eDwxpJm5IJf2SnYoL4qDBF3M 6HITTMpbnyOgTV5BfMXQuiSr51RkCFcZ67lIxGxm7SOmggbyCDmjjMdZJ3SrX4LnpYcm FpX53ZCwoiqQMSOM2plcIJ/m97ZF7+LkI5dJK+hi/wN2/a183y+Mlks6vrG9M5GkWvqd rRa4/oIwZ2HqFqqsVYXEoN3UstCWgMsw4JKMxSzgWmM5D3nAX4cbQdcIQQ3+6csINy2b hDGPjIFnvNPUDiG7vXSONiM7B1ij0L5ZZ4QM+vjMhnO+CB5NJ60SqQ1l91DElJPt/xOA dZtg== X-Gm-Message-State: AOJu0YyuIQo5CSPrV1ngvtaisFJKxOJbqWZM4RftHpV/HJ0cHMO5YqHl kxgvZk5/Hdq0fl1NuvBA7cgvF5dxvYR6Z0dLRh2TtC5GOgt9Avc/7oTq8RViO5dw4Ki1MnjRbAF JDwEHbBlCKPJxg7yNJ+X5sIS/SrR+N5Tr X-Received: by 2002:a05:6122:200a:b0:4b6:e3fa:7599 with SMTP id l10-20020a056122200a00b004b6e3fa7599mr4678900vkd.0.1705330041139; Mon, 15 Jan 2024 06:47:21 -0800 (PST) X-Received: by 2002:a05:6122:200a:b0:4b6:e3fa:7599 with SMTP id l10-20020a056122200a00b004b6e3fa7599mr4678878vkd.0.1705330040810; Mon, 15 Jan 2024 06:47:20 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ne13-20020a056214424d00b006815cf9a644sm1020720qvb.55.2024.01.15.06.47.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 06:47:20 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH 04/10] pci: devres: make devres region requests consistent Date: Mon, 15 Jan 2024 15:46:15 +0100 Message-ID: <20240115144655.32046-6-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240115144655.32046-2-pstanner@redhat.com> References: <20240115144655.32046-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 the devres struct. 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 e221919ebbe2..1e5cf950775d 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. */ /* @@ -407,21 +404,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); @@ -971,6 +953,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 7ca860acf351..66639dd754c7 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3873,7 +3873,15 @@ EXPORT_SYMBOL_GPL(pci_common_swizzle); */ void pci_release_region(struct pci_dev *pdev, int bar) { - struct pci_devres *dr; + /* + * This is done for backwards compatibility, because the old pci-devres + * API had a mode in which the function became managed if it had been + * enabled with pcim_enable_device() instead of pci_enable_device(). + */ + if (pci_is_managed(pdev)) { + pcim_release_region(pdev, bar); + return; + } if (pci_resource_len(pdev, bar) == 0) return; @@ -3883,20 +3891,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); @@ -3924,14 +3918,18 @@ EXPORT_SYMBOL(pci_release_region); * NOTE: * This is a "hybrid" function: Its normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. - * This hybrid feature is DEPRECATED! If you need to implement a new pci - * function that does automatic cleanup, write a new pcim_* function that uses - * devres directly. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name, int exclusive) { - struct pci_devres *dr; + if (pci_is_managed(pdev)) { + if (exclusive == IORESOURCE_EXCLUSIVE) + return pcim_request_region_exclusive(pdev, bar, res_name); + else + return pcim_request_region(pdev, bar, res_name); + } if (pci_resource_len(pdev, bar) == 0) return 0; @@ -3947,20 +3945,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 4d4bcc2d850f..7c2181677760 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -814,12 +814,6 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; - - /* - * TODO: remove the region_mask once everyone calling - * pcim_enable_device() + pci_*region*() is ported to pcim_ functions. - */ - u32 region_mask; }; struct pci_devres *find_pci_dr(struct pci_dev *pdev); diff --git a/include/linux/pci.h b/include/linux/pci.h index 3dcd8a5e10b5..641ee30f7d2d 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2321,6 +2321,7 @@ int pcim_iomap_regions_request_all(struct pci_dev *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); -- 2.43.0