Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp241459lqp; Mon, 10 Jun 2024 02:35:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWM8DrXWpcwMBSKsG9cF2eDARRsU2P4N/jk5RPlZZmUmwbOe3V4WmXVCiol1icsbV8yZZ6AM2fFLDgCFfmFPMyIOXaBbzFm6CEuN20Zeg== X-Google-Smtp-Source: AGHT+IHxoJPBxU+a+PROsr3l2DaXaKpcgp9crtVPPXSvnOIc30o6zK/14kwzgtxfpop37QtrpK0o X-Received: by 2002:a05:6358:6f0f:b0:19e:e48a:b6af with SMTP id e5c5f4694b2df-19f1fe194f2mr1271661255d.10.1718012122727; Mon, 10 Jun 2024 02:35:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718012122; cv=pass; d=google.com; s=arc-20160816; b=BOpW2h0I85UAoVLjJHZQSvgifPJr9b4PWa/1XeF4kfooOkKqNDNfqY5VCZNLszyRXX G6L0kWw/agWw00VQculeRQPjkbkTJ3ceYIa602jyIvkl2M+s7CdhWujel7c2I0tKa62C IBrJZaI1CB6OAMuFfw4nHIjaD7/IiUBwzBTStEp+jX+4dNfDTFknh3NrIFCVjy61QO6g 1219ugNrQsGiQFy2jby1KH/uXqgAievIyhUprYds/zxit+2lrFauSoOiQWicLeGjeLxt vHqLmTKHw0u0/ZHXBHS4Sc9hUQil24V52syLwbz85FHresRoTGqm27ky/4tLeHNZHh4E mVuQ== 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=SzRgXuNR+5R6UFNTz4a6dQ6zzeQCJg+lenadNGYN7bg=; fh=EiaTBEF9wbX42yiWZ0EG1oaDjbP2HHI6+Gkdt/x1Qmc=; b=hrWO/UD/d9GNE6v/0OtgNOG+2bQTMlQT8LE0bYUelC/uR+gIbXwGN8ylBXlTaUWixC gEqcMmERiRcYTSGvBH8s32xiSNHRu9Cae+YMs86ACJNKSHwO2LpkWHLroj3hy+VoEaUi OE63qu2ft1Hbd3WcuD1hmYRuD8lCg8nrbfxyBj8Byygj0o/UtwftNwYiTyuj0wlbE9A1 dq4d+uQYG7FdZEXNCw7hx1VKuB2cOTnnhA/3vluIDmvfJxPdAEN+NQEOcfGAwnd5/9L5 c07q5Dd57sYemMyXl/Vmi3s4SV8T4aBXaoQT8SqPQFM3lq1PsqoDHdbHplRC1xCWlUBT nbJg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zh+N+N4s; 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-207948-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207948-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. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de294f8ab7si7307890a12.720.2024.06.10.02.35.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:35:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-207948-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zh+N+N4s; 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-207948-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207948-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 58021283CF9 for ; Mon, 10 Jun 2024 09:34:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2B4467C081; Mon, 10 Jun 2024 09:32:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zh+N+N4s" 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 EFA177A715 for ; Mon, 10 Jun 2024 09:32:13 +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=1718011935; cv=none; b=CgrKaK3gH2irpa+V6Va+pVXyQgtSo+oJmC2vn5hgWf5dhUE36YL2O03LACU5oy18sXcgm8denr/SPIgnypmQHbO7DxNLD8Z5bBPM7gQqzSxA/ZleUEby+gE/AZs0zT+lbSaXtI4OW7ME4CuZ6kz/Q3UDb4NKv6R87E+Q8T6QcCE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011935; c=relaxed/simple; bh=p5aP8feMDL/KVPuUsmLl9Z6+CZ7Vvg39RrLQww3jf+8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kV0h8C0fT0hEDJ6JiEmQSGNDIIxr/C6rUw//xhYSSX3y7kA23sW3kYLwJdmu6WVPjhHP06ICJ6UmNCLdvaRznMojNTBNGYz9QG9v/CwvdMG8buo0hcambJ40Zrb7vbDleYPE5FlQZGHGXKbRRDf3U6HFZr4sAA8fungWltpKO3k= 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=Zh+N+N4s; 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=1718011932; 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=SzRgXuNR+5R6UFNTz4a6dQ6zzeQCJg+lenadNGYN7bg=; b=Zh+N+N4s6NUSkJx7K5PEYIBwoW4U4/ROpnGmxNKIKBbA2mYxbE9veoSr4l/GhHZafQvHxw Amq5QjhOSn+lvt8wWpsTn+dbQxwdSYDDmI8sNQ3r4zEnyXFKsqaWdHj3EWbaXzC9t5K5Bm LwDeEWP9qFSfUjpwfRI4Wze93KeHEGI= 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-562-a5KX4GwgNfS-u939a0Z0WA-1; Mon, 10 Jun 2024 05:32:06 -0400 X-MC-Unique: a5KX4GwgNfS-u939a0Z0WA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4210e98f8d7so6355125e9.3 for ; Mon, 10 Jun 2024 02:32:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011925; x=1718616725; 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=SzRgXuNR+5R6UFNTz4a6dQ6zzeQCJg+lenadNGYN7bg=; b=dQNXFzswP9HpiAXmfJ+lufjOLOL1ub3+Afv3qcueADMtHakb7OkIIATN0cf8/OmiVe pie6bTrJP3F3iSMx4mz9kYICfUsOrucFP/ce1wsjc/ydBjrHznQg3Fz/BJBdGx0tZ40W zh/28rSO8BZzlOzYdgdySiUNxhhTrrKXm0x5K6/dCK2EOOsZYC5HAi6Ku0OwukCgv6Xw XM+jkBvR4ifcYVadG/uEkMFXtmdF5VF/CLpWXUv/NePDpdQ1pxNud9kF11d8B2qIGCkf WHFBli0tlmXfUfZgVLBZega+VLKuAsL7KamBigJNnDBE9PnIl9z7hCsqeEsvEwiLudVa q6zA== X-Forwarded-Encrypted: i=1; AJvYcCVuzvctgWErty1RQFIuibsciGve2U0sHyWaF5MeU/3KTORkGgcRtAYkNCLfCYll+vRAIdeO2hQgnvx/82vWJJMsn5V5/hKXlhLt9SOj X-Gm-Message-State: AOJu0YwBPL11F/5aoXsWI2SoircK2TiuQkLREHihF5l5xGbqW30Gh7QX kQq86IEeIBYpEAy1bQT/TZ6UdHJfmV+bpzdoue+gmN0/5tVDK8mXAPQv/twz2jwFXUK0L82VmcL QSQDTzdQKLIAGDCyuToCAx1wxsX3+xY1uev5A7qQT6xvSQHuHQuLP7JqrlMNSkR0umEGoSg== X-Received: by 2002:a05:6000:2a1:b0:35f:2287:7b97 with SMTP id ffacd0b85a97d-35f22877d54mr1983938f8f.4.1718011925304; Mon, 10 Jun 2024 02:32:05 -0700 (PDT) X-Received: by 2002:a05:6000:2a1:b0:35f:2287:7b97 with SMTP id ffacd0b85a97d-35f22877d54mr1983925f8f.4.1718011924931; Mon, 10 Jun 2024 02:32:04 -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 ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:04 -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 v8 05/13] PCI: Make devres region requests consistent Date: Mon, 10 Jun 2024 11:31:27 +0200 Message-ID: <20240610093149.20640-6-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 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. Link: https://lore.kernel.org/r/20240605081605.18769-7-pstanner@redhat.com Signed-off-by: Philipp Stanner Signed-off-by: Bjorn Helgaas --- drivers/pci/devres.c | 53 ++++++++++++++++++++++---------------------- drivers/pci/pci.c | 47 +++++++++++++-------------------------- drivers/pci/pci.h | 10 ++++----- 3 files changed, 45 insertions(+), 65 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 54b10f5433ab..f2a1250c0679 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -24,18 +24,15 @@ * * 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,22 +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 (mask_contains_bar(this->region_mask, i)) - pci_release_region(dev, i); if (this->mwi) pci_clear_mwi(dev); @@ -823,11 +804,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 @@ -836,7 +835,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 d94445f5f882..7013699db242 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3872,7 +3872,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; @@ -3882,21 +3890,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); @@ -3922,7 +3915,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; @@ -3938,21 +3936,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 c09487f5550c..2403c5a0ff7a 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -826,16 +826,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 * -- 2.45.0