Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7695039rdb; Thu, 4 Jan 2024 05:04:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFw6f+OW7fIitWS6BAAtKWljkm7LskaM7/eX5bnedG2ZPFvCBIgdBgFxwHG70ihe3xgjnfm X-Received: by 2002:a05:600c:a45:b0:40e:3511:2c39 with SMTP id c5-20020a05600c0a4500b0040e35112c39mr303854wmq.172.1704373485769; Thu, 04 Jan 2024 05:04:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704373485; cv=none; d=google.com; s=arc-20160816; b=KFcbFDB6KFL5Srmma++0HREiQjpgpNI7hiiJk0hDKuqdwoPPTw0HS8PzqfFQ0yaoPX yODEKJWgiElKNENutyD2JxQqKDCdBQLnoF/yX0qMDvaqglZhN9IjjrOvzimkBscBwxiA Z7QMRymjOCAAQuyUJmo6yc9Q8nyTudJJA9oeNLlXCZTSH2Fd3pokVHLRdUEOKtL7ouP8 HVrWs+xDPikN5AONvjn6QVktEg4/LiCQRvJz4tCqZRaPemL6UyT1af9sxhLGyz15MxLa yjhEGX5iEJcQ13c6bjTSgCPbGdbAlIeEISBMqsRXwUf1sPa11dqwJ0+FqR4DUMVGeuzN QpHw== 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=C0/NJ7KYEulil2Xeyr0vkcKQVIe4dXTk2WHu1Aal+GM=; fh=1uFngekeZT2FVGQaKpK4gjTIex4FU0hdfexTfOUKej8=; b=MK4eVjcqsop4AzU0cZjTQQQJPw0DO/5R4gPg5/s/GdpVf07hfwg5DgQ/zMzphH56y2 PTjV0r/3NmJdEApq05gXL6OwMcRNsJMRfb3cDik2HBBuLk7zNkxtOnR9rgID+EMO3sGJ aRhvs/2T8YAuoC8erffRzanB8tkf89c/k3iR7h4mG4zA3JK4tiT+BL6hIJ5VlaiVkPKg bF+p3Rc7Myrne5yKTeP6UZ6cM4bDYZebTRS1mXWPooitcexwbN/hVon5KyuT4RskKbXX LoGzdS5HBufHsOZV+1ENUO0oK4A5QSnpcvY/IPfnstjUjjmk8XXBL+lsjl21bqR6p94r d/CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=QVSdXkKS; spf=pass (google.com: domain of linux-wireless+bounces-1488-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-wireless+bounces-1488-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id zr15-20020a170907710f00b00a28ef4b9d26si199355ejb.721.2024.01.04.05.04.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 05:04:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless+bounces-1488-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=QVSdXkKS; spf=pass (google.com: domain of linux-wireless+bounces-1488-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-wireless+bounces-1488-linux.lists.archive=gmail.com@vger.kernel.org" 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 3FC231F251D0 for ; Thu, 4 Jan 2024 13:04:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE001249F4; Thu, 4 Jan 2024 13:02:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="QVSdXkKS" X-Original-To: linux-wireless@vger.kernel.org Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1D91241F1 for ; Thu, 4 Jan 2024 13:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-336c8ab0b20so448408f8f.1 for ; Thu, 04 Jan 2024 05:02:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704373330; x=1704978130; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=C0/NJ7KYEulil2Xeyr0vkcKQVIe4dXTk2WHu1Aal+GM=; b=QVSdXkKSrGsr/Oael23krHCfZal44tTq1nlfWdZZHrbCwSePZZM9VdnKpJS0ssNA2D gTpPNYMRrg7t6O2KPZWDqj0lZD+PoNlHkUKzzKRp7GCMWa4KzBY4F5yRz9fnVno1emGL jDMQVB8/C8zsMvpY+WRj2fSdL5wPhEU0sHT81qfAOvVaxZiQKwwDM/boHYRy0ZvqXoi8 lzT0JjScoJt8MsJeeWWs3IkS+ZVdP9ZY8SmnyYuxXpqJxXvXq7K89oRhD0JCSZ+e8fnE O5UctSqi6JDo9rtbjHS0jSHddF6ZiI/tJ71PX/nPrUdUKx+A9fhqx2puXedraOtIDloe rD9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704373330; x=1704978130; 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=C0/NJ7KYEulil2Xeyr0vkcKQVIe4dXTk2WHu1Aal+GM=; b=bUnSqKokBpOd6QhJI8d5m2crPxcy3uy6S4UaAR+oXL1N2ZeV04hjzEfnTgCzwcAoSD XcYvl8QzjM+djldNIAyq4g5VftjsEDQkvTftnpiNiKuAaXKN8Q/QScYNGwHDDnmKcnPR brre4IF+//K2XivL4WzYlKrbZqeeCxARvN7tQ6P/VqxNlpvWvfGTEqyJ1K4SZFtWunTP jkdUXomRBRifObc7oQ4dlyeTdsOf3I+k1J/WK0t6DbNN+kDM7ZwGhlucp9psp0OUbm7j 3YfSdwWY7Kb82f4jQm6En/XTUMXYrNiy732/MzF/4zfcfIn+/fcaQkhgAMic7T/U1/CN oW8Q== X-Gm-Message-State: AOJu0Yz0ywMaix7fLeJ8yh6vdg2PbQdL65eM4iDbwGfEJdUu7BVMxIJE wQGaDqOKqsVjE2+Bt2qF3opVKdrHETROjA== X-Received: by 2002:adf:ce88:0:b0:336:7590:bd34 with SMTP id r8-20020adfce88000000b003367590bd34mr378455wrn.6.1704373330194; Thu, 04 Jan 2024 05:02:10 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:5b69:3768:8459:8fee]) by smtp.gmail.com with ESMTPSA id w5-20020a5d5445000000b0033660f75d08sm32887387wrv.116.2024.01.04.05.02.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 05:02:09 -0800 (PST) From: Bartosz Golaszewski To: Kalle Valo , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Bjorn Andersson , Konrad Dybcio , Catalin Marinas , Will Deacon , Bjorn Helgaas , Heiko Stuebner , Jernej Skrabec , Chris Morgan , Linus Walleij , Geert Uytterhoeven , Arnd Bergmann , Neil Armstrong , =?UTF-8?q?N=C3=ADcolas=20F=20=2E=20R=20=2E=20A=20=2E=20Prado?= , Marek Szyprowski , Peng Fan , Robert Richter , Dan Williams , Jonathan Cameron , Terry Bowman , Kuppuswamy Sathyanarayanan , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Huacai Chen , Alex Elder , Srini Kandagatla , Greg Kroah-Hartman Cc: linux-wireless@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, Bartosz Golaszewski Subject: [RFC 5/9] PCI/pwrseq: add pwrseq core code Date: Thu, 4 Jan 2024 14:01:19 +0100 Message-Id: <20240104130123.37115-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240104130123.37115-1-brgl@bgdev.pl> References: <20240104130123.37115-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-wireless@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Bartosz Golaszewski Some PCIe devices must be powered-on before they can be detected on the bus. Introduce a simple framework built around the PCIe port driver which now instantiates its child devices from device-tree. The way this works is: a DT node representing a PCIe device connected to the port can be matched against its power sequencing platform driver. If the match succeeds, the driver is responsible for powering-up the device and calling pcie_pwrseq_device_enable() which will trigger a PCI bus rescan as well as subscribe to PCI bus notifications. When the device is detected and created, we'll make it consume the same DT node that the platform device did. When the device is bound, we'll create a device link between it and the parent power sequencing device. Signed-off-by: Bartosz Golaszewski --- drivers/pci/pcie/Kconfig | 2 + drivers/pci/pcie/Makefile | 2 + drivers/pci/pcie/pwrseq/Kconfig | 8 +++ drivers/pci/pcie/pwrseq/Makefile | 3 ++ drivers/pci/pcie/pwrseq/pwrseq.c | 83 ++++++++++++++++++++++++++++++++ include/linux/pcie-pwrseq.h | 24 +++++++++ 6 files changed, 122 insertions(+) create mode 100644 drivers/pci/pcie/pwrseq/Kconfig create mode 100644 drivers/pci/pcie/pwrseq/Makefile create mode 100644 drivers/pci/pcie/pwrseq/pwrseq.c create mode 100644 include/linux/pcie-pwrseq.h diff --git a/drivers/pci/pcie/Kconfig b/drivers/pci/pcie/Kconfig index 8999fcebde6a..064843ab6866 100644 --- a/drivers/pci/pcie/Kconfig +++ b/drivers/pci/pcie/Kconfig @@ -155,3 +155,5 @@ config PCIE_EDR the PCI Firmware Specification r3.2. Enable this if you want to support hybrid DPC model which uses both firmware and OS to implement DPC. + +source "drivers/pci/pcie/pwrseq/Kconfig" diff --git a/drivers/pci/pcie/Makefile b/drivers/pci/pcie/Makefile index 8de4ed5f98f1..ace91384113d 100644 --- a/drivers/pci/pcie/Makefile +++ b/drivers/pci/pcie/Makefile @@ -13,3 +13,5 @@ obj-$(CONFIG_PCIE_PME) += pme.o obj-$(CONFIG_PCIE_DPC) += dpc.o obj-$(CONFIG_PCIE_PTM) += ptm.o obj-$(CONFIG_PCIE_EDR) += edr.o + +obj-$(CONFIG_PCIEPORTBUS) += pwrseq/ diff --git a/drivers/pci/pcie/pwrseq/Kconfig b/drivers/pci/pcie/pwrseq/Kconfig new file mode 100644 index 000000000000..010e31f432c9 --- /dev/null +++ b/drivers/pci/pcie/pwrseq/Kconfig @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 + +menuconfig PCIE_PWRSEQ + bool "PCIe Power Sequencing support" + depends on PCIEPORTBUS + help + Say yes here to enable support for PCIe power sequencing + drivers. diff --git a/drivers/pci/pcie/pwrseq/Makefile b/drivers/pci/pcie/pwrseq/Makefile new file mode 100644 index 000000000000..da99566594f6 --- /dev/null +++ b/drivers/pci/pcie/pwrseq/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_PCIE_PWRSEQ) += pwrseq.o diff --git a/drivers/pci/pcie/pwrseq/pwrseq.c b/drivers/pci/pcie/pwrseq/pwrseq.c new file mode 100644 index 000000000000..c4f7958074c9 --- /dev/null +++ b/drivers/pci/pcie/pwrseq/pwrseq.c @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Linaro Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static int pcie_pwrseq_notify(struct notifier_block *nb, unsigned long action, + void *data) +{ + struct pcie_pwrseq *pwrseq = container_of(nb, struct pcie_pwrseq, nb); + struct device *dev = data; + + if (dev_fwnode(dev) != dev_fwnode(pwrseq->dev)) + return NOTIFY_DONE; + + switch (action) { + case BUS_NOTIFY_ADD_DEVICE: + device_set_of_node_from_dev(dev, pwrseq->dev); + break; + case BUS_NOTIFY_BOUND_DRIVER: + pwrseq->link = device_link_add(dev, pwrseq->dev, + DL_FLAG_AUTOREMOVE_CONSUMER); + if (!pwrseq->link) + dev_err(pwrseq->dev, "Failed to add device link\n"); + break; + case BUS_NOTIFY_UNBOUND_DRIVER: + device_link_del(pwrseq->link); + break; + } + + return NOTIFY_DONE; +} + +int pcie_pwrseq_device_enable(struct pcie_pwrseq *pwrseq) +{ + if (!pwrseq->dev) + return -ENODEV; + + pwrseq->nb.notifier_call = pcie_pwrseq_notify; + bus_register_notifier(&pci_bus_type, &pwrseq->nb); + + pci_lock_rescan_remove(); + pci_rescan_bus(to_pci_dev(pwrseq->dev->parent)->bus); + pci_unlock_rescan_remove(); + + return 0; +} +EXPORT_SYMBOL_GPL(pcie_pwrseq_device_enable); + +void pcie_pwrseq_device_disable(struct pcie_pwrseq *pwrseq) +{ + bus_unregister_notifier(&pci_bus_type, &pwrseq->nb); +} +EXPORT_SYMBOL_GPL(pcie_pwrseq_device_disable); + +static void devm_pcie_pwrseq_device_disable(void *data) +{ + struct pcie_pwrseq *pwrseq = data; + + pcie_pwrseq_device_disable(pwrseq); +} + +int devm_pcie_pwrseq_device_enable(struct device *dev, + struct pcie_pwrseq *pwrseq) +{ + int ret; + + ret = pcie_pwrseq_device_enable(pwrseq); + if (ret) + return ret; + + return devm_add_action_or_reset(dev, devm_pcie_pwrseq_device_disable, + pwrseq); +} +EXPORT_SYMBOL_GPL(devm_pcie_pwrseq_device_enable); diff --git a/include/linux/pcie-pwrseq.h b/include/linux/pcie-pwrseq.h new file mode 100644 index 000000000000..6abb5796754e --- /dev/null +++ b/include/linux/pcie-pwrseq.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2024 Linaro Ltd. + */ + +#ifndef __PCIE_PWRSEQ_H__ +#define __PCIE_PWRSEQ_H__ + +#include + +struct device; + +struct pcie_pwrseq { + struct notifier_block nb; + struct device *dev; + struct device_link *link; +}; + +int pcie_pwrseq_device_enable(struct pcie_pwrseq *pwrseq); +void pcie_pwrseq_device_disable(struct pcie_pwrseq *pwrseq); +int devm_pcie_pwrseq_device_enable(struct device *dev, + struct pcie_pwrseq *pwrseq); + +#endif /* __PCIE_PWRSEQ_H__ */ -- 2.40.1