Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp362046rdb; Tue, 23 Jan 2024 01:49:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEhcYCtrhqiqIpSj6O6za6GzFIbAO2CpEqHq6egLV1IrHmWmRAsLQQzrtVovyIhuK3/fz1Q X-Received: by 2002:a05:6a00:2d1c:b0:6dd:48b:703b with SMTP id fa28-20020a056a002d1c00b006dd048b703bmr1040629pfb.45.1706003346813; Tue, 23 Jan 2024 01:49:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706003346; cv=pass; d=google.com; s=arc-20160816; b=QjJE9mRyykcvTuWkRtJ6DMy9mMd9UR1dwzYQBdXdZejfz163Xjb4Hl2BUteTjUM7ze evn1QmxTHcQGaQK9VJhH0gdWLgk36Vaiva/7iGN8Y1st0C0m2Dexobt3qMwivMw0h+qO vL31tyJCWtvvnqzwl84bBqhbJwWuBIFw4TN/CcHRcvPhkUDLkq0KhL9FC4B11ebsEhWd PSu3dLzfDENAV9vkLnzGxs4Rrs43df+HN/Fi8NSpCq7rd6Wh5c+pOPT9+NSxFnf7n5lF DUzP4LJmEnFy/8Yoze5F9MJ9hUK9XqvZklyfRmixmAJgNanc8vAOWUtMq8MUaQpBHwRd VXQw== ARC-Message-Signature: i=2; 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=hBrROWC/lRyzTMd3ODmKW2xAxrixnDdNmIHjBaaWBE0=; fh=AY1J9PDScN0xrIvhrMJy8TMRSbPeq4owI1m0lsalYQU=; b=iYv20uPGZkA/lE2SWCdVNRUpK8zDBVbkNEb8rw8MLYPmhq3cqMTPPBjK2hfURM3wl3 Oje/L9K8dwreaGEK0Rk8P6rg94ndacHDN/h4ZUer0Oi10PSgIIzoZmFFc85KV15Q3GZs u5/LxYYfh0ReuouqvEtTaswzH+ocnRoKYjcNdfQB7iK4PUNqTrryqSnnk0avGG3IvUmA ybIPX3ZKJmOzqSLXzYiPaUPh8C3OOKjQRUH7MWl9heWcz9OfLlrvmD3IoAcyGQer0J9E L0M/8my++LV/D56V0WDf4sN02vuiDGXjhRogclMi/oXy3NUFqZSE1nxVZpluaZFeWKJC vBRg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O2CmA8Jr; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-35025-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35025-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id lo14-20020a056a003d0e00b006dc7eefff99si1284324pfb.0.2024.01.23.01.49.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:49:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35025-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O2CmA8Jr; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-35025-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35025-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 790E428C12C for ; Tue, 23 Jan 2024 09:47:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A222B5C5EA; Tue, 23 Jan 2024 09:45:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="O2CmA8Jr" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94D645BAEE for ; Tue, 23 Jan 2024 09:44:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003099; cv=none; b=O0oZA4Zr618lMIiujRMS9HxtesZNE+slhHj+p6Lj+vmJpljqUsDWybbI2h7PDUxS+lSdKcuXk/KR6/qkOWjEvfoOzxYlgb5NsOA52P7wyx29+cJjM8gwlfm1fEqmYlpVe7mNoA7xpuquJpNxq0oTsMg7SXjF1rRKvX50LyK6Drk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706003099; c=relaxed/simple; bh=jCcPfvVIh+5GPg1n63+yfNtQNLoVDRUUEmfN8ZfGD/c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fBLje3z1LTtPv3rSSvnbzubamvDyNQTODznKxChbYC3ROUChbX8bqSt3szdwgXcfCrYJXhwHdqsCpIP4H/WzWqvRGktqKhspCMP8WLowU7gqTwqGoRlJXB5CVLP7QICT2D33YlIU8snrfPn5N2e8aQ3pQggvK/1uQghZQewnavE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=O2CmA8Jr; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706003096; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hBrROWC/lRyzTMd3ODmKW2xAxrixnDdNmIHjBaaWBE0=; b=O2CmA8JrS6CEvpNcAUIvCnZK1xhLjayKqnufixRvRtcqMvsxdXCscBVzYH5Ii+wnS2ytCx mteGYviCUcYQN5zY2CE1zWSY8Jxp6xkoZ63k97/rMMkYOClHrTKdnpjX6V2elCgjr3UqP9 p1qducmCXIK52ANe1Qr4wech/rr/x5k= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-Zxzo1DhYOmiBWqIZQ9DD3Q-1; Tue, 23 Jan 2024 04:44:54 -0500 X-MC-Unique: Zxzo1DhYOmiBWqIZQ9DD3Q-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7830635331bso136327385a.1 for ; Tue, 23 Jan 2024 01:44:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706003094; x=1706607894; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hBrROWC/lRyzTMd3ODmKW2xAxrixnDdNmIHjBaaWBE0=; b=tKpEnByF+cZGjMof9iPYsCutFRie5CR7AAoDmABSCFuL5J5rzYXOq/trjCls57i3Qv cYaOtmZ9uNgqFioNzAPspPxoaQ7nlUUa+Hv7KDGn76Z/LZACR1LqY7BAMeIV6p91BbYA CsRoyS0+cCnlKDcL0qcPp+xpJm+zcxDLrUEXLVf2q+OmBY2Oa70Ownu169Q8hWkzvOi6 mj8JkcY5n1f8HoxV4GN/M05xCwT2FpOWvOmMmRKDDhJc39BK8rAk8oCj6dkNf5d+6ZJC IkTyUso8fTvVyWpcoUkXR6lxAHPJSgC5utxevUI6HHpDEguFjp4Adh+O50CKiAKrVmN8 V3Ig== X-Gm-Message-State: AOJu0YzYcor7rgoEwyBwUhjdqYyv6IglH6ahJQo1zewhsfvtbVpFWXD3 fCcbGELEWZxRHusTXy/zbRRJTLy5sB7QaMMBMOHDR81J7rkLn8Y5q77Kzld6rm/g9SWjddf0lZ+ DKyFqGMUlLhlFqChSxv36+8pISgI/t+Omyi5i61q1Zt5fNhoRkzZxJ8oCU70Qug== X-Received: by 2002:a05:6214:29c9:b0:685:be18:eac7 with SMTP id gh9-20020a05621429c900b00685be18eac7mr9471506qvb.5.1706003094194; Tue, 23 Jan 2024 01:44:54 -0800 (PST) X-Received: by 2002:a05:6214:29c9:b0:685:be18:eac7 with SMTP id gh9-20020a05621429c900b00685be18eac7mr9471482qvb.5.1706003093634; Tue, 23 Jan 2024 01:44:53 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id nc5-20020a0562142dc500b00685e2ffcaf5sm2958704qvb.38.2024.01.23.01.44.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:44:53 -0800 (PST) From: Philipp Stanner To: Jonathan Corbet , Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Philipp Stanner , Sam Ravnborg , dakr@redhat.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org Subject: [PATCH v2 04/10] PCI: make devres region requests consistent Date: Tue, 23 Jan 2024 10:43:01 +0100 Message-ID: <20240123094317.15958-5-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123094317.15958-1-pstanner@redhat.com> References: <20240123094317.15958-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 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 struct pci_devres. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 49 ++++++++++++++++++++++--------------------- drivers/pci/pci.c | 50 +++++++++++++++----------------------------- drivers/pci/pci.h | 6 ------ include/linux/pci.h | 1 + 4 files changed, 43 insertions(+), 63 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 1279aac9b63e..7bff17633bc4 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -22,18 +22,15 @@ * _sometimes_ managed (e.g. pci_request_region()). * Consequently, in the new API, region requests performed by the pcim_ * functions are automatically cleaned up through the devres callback - * pcim_addr_resource_release(), while requests performed by - * pcim_enable_device() + pci_*region*() are automatically cleaned up - * through the for-loop in pcim_release(). + * pcim_addr_resource_release(). + * Users utilizing pcim_enable_device() + pci_*region*() are redirected in + * pci.c to the managed functions here in this file. This isn't exactly + * perfect, but the only alternative way would be to port ALL drivers 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); @@ -968,6 +950,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..eb3ec59e8822 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3873,7 +3873,15 @@ EXPORT_SYMBOL_GPL(pci_common_swizzle); */ void pci_release_region(struct pci_dev *pdev, int bar) { - struct pci_devres *dr; + /* + * This is done for backwards compatibility, because the old pci-devres + * API had a mode in which the function became managed if it had been + * enabled with pcim_enable_device() instead of pci_enable_device(). + */ + if (pci_is_managed(pdev)) { + pcim_release_region(pdev, bar); + return; + } 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); + + 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