From patchwork Fri Mar 1 11:29: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: 208801 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp1012828dyb; Fri, 1 Mar 2024 03:32:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUCq7TS60yr+0DAu0jRjbN0+b2yBrUm61HiXpj7/NNIbzLH/bHF3q+ObLqIFfhP8F0rZ22kCQr3i5TBUIkBu2Z3MrmfVA== X-Google-Smtp-Source: AGHT+IHu0nmVEKs1bjfvnEM4Vbm6snnRI1HA1To+xM5Q1mxpkOyUymgF87wt/YgUzAhbx9z/KwuV X-Received: by 2002:a05:622a:ca:b0:42e:d10c:e2b7 with SMTP id p10-20020a05622a00ca00b0042ed10ce2b7mr205139qtw.68.1709292766678; Fri, 01 Mar 2024 03:32:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709292766; cv=pass; d=google.com; s=arc-20160816; b=DSZo31Tm0U3YFd/XBJCBYoEm9bEKuyj90L89SVgXYVSWbYX3m9aVrpG2kqD7E3d8yw ldyC0bRwBUnjlQcnQbM+ybnf5iqfDL12W5qyNCB5/F1bsg3fxRIaiGyHvXk5tnhFahzO J+O8oFiXzGzZxuesBFrR69jafE1rjO6MG5NVrQfNaJ3SmVBfrjZhpq0RryG9j1rXq/FJ qvPHDOkvskcITL9CIEGNsQtgnq+D4NmvVL+IDp+w4VSijcAJ0Xcej4rPN4NZ+9Y/3+n3 GDu/3nw4BOeEBxjhetJCorgWVEyDOTmmkMSgPiLDzp0iMA/RRqCuVVwOrU42fXVDPcPG LRMQ== 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=gFrW8U6ifOIeoCJ4m2srgLpDe6q16UWzB5DewZd74Sc=; fh=x9w/4NBAULYT47yJzyZKdnJ4No9lL1xlBJw8ehMqyQ8=; b=jzHpad5Uz90Vjo0vmQHdU63adtTEeaXgwlHFXuXONUnJUxx5HIkjeLMM0nsdwXdpGx MU3lWNDLwRtOI4K9Sbz5eVv4pw2mi93laOrDxVH0hf7iYrolq0Xdif7hsc/rjYt/5NRc iCFgvI1CxCbEKAbeHL2knpOFouWiD1qwnr920JiemMLw41PiBDeP4gVxXAaIXMSgVEvy VJY8f9KbTM0VeHWRBJR2w71Y91tGno6XVHN88eqUbcSEKmG6n7gz+9O27DQ2wad6doPI FvJsrosjAqw81dC3IYPQKn1peMyaKLKzYpnMJvSz7/igdn5qz8wmbGBWLPvVesAYw3GT x0zw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=V1CW600U; 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-88352-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88352-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p9-20020ac87409000000b0042dc0be1d2bsi3246724qtq.442.2024.03.01.03.32.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 03:32:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88352-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=V1CW600U; 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-88352-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88352-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 699591C212FD for ; Fri, 1 Mar 2024 11:32:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8169C70AE4; Fri, 1 Mar 2024 11:30:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="V1CW600U" 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 698E16EF16 for ; Fri, 1 Mar 2024 11:30:29 +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=1709292631; cv=none; b=oEBlRRttHNAqoArqskfynu1oHW4JR6NCZFWP7NyG5CcOjyDMzRZ31dYLD7rC8CQ8gLJ0cuJE4Hu237SI1Z1lOPZ8cCTj/8gXFYPvT3ZfErt+fc3dnYYjHdG3noAmAyg8BOak1xTCK7s+0sM4rC0IJb8vmFPW0/BMjkxRDfIowgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709292631; c=relaxed/simple; bh=wGKLutgNDish9diSLXJl2upRYyiSWsXKdjBkv9G+Yqg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nczNrXQT0AXR63PsCV+PbxNNiVcHxPx7eeYshAQ1GdYgxEiGwoOywUBOze2FB5ojzavFSZZzJfq8inXHS+oY5VfgEGO1prX8hBGWoENjeFhrVXfneflmxMHsV1JKD6pWDbnEGlTHYgxqSxID/LrTlzd4bD3j4JTd8JuNP9n1VmM= 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=V1CW600U; 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=1709292628; 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=gFrW8U6ifOIeoCJ4m2srgLpDe6q16UWzB5DewZd74Sc=; b=V1CW600U4H3MbP3CewpfjFFWhfVN7i5MNzzCYwbOuTfSCFK9IwN8DA8Tl8AN4LPKVTzeJu M6chIPhxRsCu1UeLaGybNbqI9bDQqkBhqa6r8KVEVDIQ6tga70CamVFdV50Da0SGErxiAX NbJGIGjxn+g8KwFscJWOGwWLt2wJxbA= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-Pqr-Z_mDN8WDdDGUtn2sYg-1; Fri, 01 Mar 2024 06:30:27 -0500 X-MC-Unique: Pqr-Z_mDN8WDdDGUtn2sYg-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-42da766586cso2038631cf.1 for ; Fri, 01 Mar 2024 03:30:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709292627; x=1709897427; 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=gFrW8U6ifOIeoCJ4m2srgLpDe6q16UWzB5DewZd74Sc=; b=Lma0pf8MpPAO8V7QRWtpSOf4dR8/+7RcoDtVNqKBgQfhe0Zgdh+Wc7MWu4rPGvVWOU HiTFynvPAdLZJqNYdWKFCRmh9km6x+A+ZzfTCnziaVsidz29Mz3RYTxFF79I71VHXlIn C3IA3yKkvZGB/DiEmSJzg/Kw3FreUng2/UwBPGZyoQBlZrYO+0guPS2lr7Z6aCD6CfWL 4B8kytj+DagH6r0qvpcy61zHPTjyzqKzexhakXaYsbEQpwY7Qwws2xAsZO3z4IM6tu5f WMhLSLQy5MQlk3cVknSmEqERo3zTuaE3ima09iCluHpKu4/RMmVW2WhBNWMMGdS3ThdO u/fg== X-Forwarded-Encrypted: i=1; AJvYcCX4uXQAK9Ndi0FyWtIsk0NIMZ3Y4LoJeCW4ScmoenYC1+kOJsNFqoIFaNTZS14h8DJMvUPA18dU5wuWuJdEsktpiBxoB+rIxKqSEF8t X-Gm-Message-State: AOJu0Yz2odiTXfa4/qTYuwRYV+LgHsyrc7vkb266mYOTHpb7GnoXSlnz ePwpJmTzBYfjv4ATl/xP+DSzC5mVnzvz3emkgSPAA5zuIsYDufhAgN88jlROxQ1Bj0sQrbim1oS uhsJVEbK8kQbjW2aoWmGJII2jqIvb+16Q+N5UrPEJN5NCGaQdaIaTHUq6VdU0MA== X-Received: by 2002:a05:622a:1ba8:b0:42e:b428:808d with SMTP id bp40-20020a05622a1ba800b0042eb428808dmr1172313qtb.0.1709292626853; Fri, 01 Mar 2024 03:30:26 -0800 (PST) X-Received: by 2002:a05:622a:1ba8:b0:42e:b428:808d with SMTP id bp40-20020a05622a1ba800b0042eb428808dmr1172294qtb.0.1709292626575; Fri, 01 Mar 2024 03:30:26 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id b1-20020ac86781000000b0042eb46d15bbsm1596239qtp.88.2024.03.01.03.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 03:30:26 -0800 (PST) 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 v4 08/10] PCI: Give pci(m)_intx its own devres callback Date: Fri, 1 Mar 2024 12:29:56 +0100 Message-ID: <20240301112959.21947-9-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240301112959.21947-1-pstanner@redhat.com> References: <20240301112959.21947-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792323371729166018 X-GMAIL-MSGID: 1792323371729166018 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 other drivers via a public API. Implement pcim_intx() with its own device resource. Make pci_intx() call pcim_intx() in the managed case. Remove the struct pci_devres from pci.h. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 74 +++++++++++++++++++++++++++++++++++--------- drivers/pci/pci.c | 24 +++++++------- drivers/pci/pci.h | 17 +--------- 3 files changed, 74 insertions(+), 41 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index e417bd3ea96f..8a643f15140a 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, @@ -401,28 +406,69 @@ int pcim_set_mwi(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_set_mwi); +static void pcim_intx_restore(struct device *dev, void *data) +{ + struct pci_dev *pdev = to_pci_dev(dev); + struct pcim_intx_devres *res = data; -static void pcim_release(struct device *gendev, void *res) + pci_intx(pdev, res->orig_intx); +} + +static struct pcim_intx_devres *get_or_create_intx_devres(struct device *dev) { - struct pci_dev *dev = to_pci_dev(gendev); - struct pci_devres *this = res; + struct pcim_intx_devres *res; - if (this->restore_intx) - pci_intx(dev, this->orig_intx); + res = devres_find(dev, pcim_intx_restore, NULL, NULL); + if (res) + return res; - if (!dev->pinned) - pci_disable_device(dev); + 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 862dab87732a..8ad2ded49ab5 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3857,7 +3857,7 @@ EXPORT_SYMBOL(pci_enable_atomic_ops_to_root); void pci_release_region(struct pci_dev *pdev, int bar) { /* - * This is done for backwards compatibility, because the old pci-devres + * 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(). */ @@ -4445,11 +4445,22 @@ void pci_disable_parity(struct pci_dev *dev) * 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! + * 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) @@ -4457,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 bbe004eabcc1..c6da2b554108 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -814,22 +814,7 @@ 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 { - unsigned int orig_intx:1; - unsigned int restore_intx:1; -}; - -struct pci_devres *find_pci_dr(struct pci_dev *pdev); +int pcim_intx(struct pci_dev *dev, int enable); /* * Config Address for PCI Configuration Mechanism #1