mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vladyslav Tymoshchuk <tvlad744@gmail.com>
To: Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konradybcio@kernel.org>
Cc: Abel Vesa <abelvesa@kernel.org>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sebastian Reichel <sre@kernel.org>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Dmitry Baryshkov <lumag@kernel.org>,
	Casey Connolly <casey.connolly@linaro.org>,
	Johan Hovold <johan@kernel.org>,
	Jishnu Prakash <jishnu.prakash@oss.qualcomm.com>,
	Kamal Wadhwa <kamal.wadhwa@oss.qualcomm.com>,
	Joonhoe Kim <26rote@gmail.com>,
	Daniel J Blueman <daniel@quora.org>,
	Neil Armstrong <neil.armstrong@linaro.org>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A . R . Silva" <gustavoars@kernel.org>,
	linux-arm-msm@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-pm@vger.kernel.org, linux-hardening@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Vladyslav Tymoshchuk <tvlad744@gmail.com>
Subject: [RFC PATCH 1/4] soc: qcom: pmic_glink: Hold back notifications over system sleep
Date: Thu, 22 Oct 2026 19:41:48 +0300	[thread overview]
Message-ID: <20261022164151.592-2-tvlad744@gmail.com> (raw)
In-Reply-To: <20261022164151.592-1-tvlad744@gmail.com>

The GLINK interrupt is not suspended, so the PMIC GLINK messages keep
reaching the clients during system suspend and resume, and while the
system is in suspend-to-idle. The clients handle most notifications in
work on the system workqueue, which is not freezable, so the work can
run before the devices it uses have resumed:

- ucsi_glink passes connector changes on to the UCSI core, whose
  connector work switches the USB role and sets the Type-C muxes and
  switches. On SM8750, unplugging the USB cable while the system is in
  suspend-to-idle makes dwc3-qcom access its registers with the clocks
  off, and the SoC resets. Other boards hit I2C transfers on suspended
  controllers from the same work, to a PS8830 retimer or an FSA4480
  switch.

- pmic_glink_altmode had the same problem with its worker, which
  commit c7c56b6ec077 ("soc: qcom: pmic_glink: Fix device access from
  worker during suspend") moved to system_freezable_wq as a stop-gap.

Rather than have each client defer its work, defer the notifications
in pmic_glink: queue them from ->prepare() on, and from ->complete()
schedule work on system_freezable_wq that passes them on to the
clients. The work runs once the tasks have been thawed, after all the
devices have resumed. Until it has run, new notifications are queued
behind the old ones, so the clients get them in order. Responses are
passed on right away, as the client that sent the request is waiting
for them.

The clients treat a notification as the latest state, or as a hint to
read the current state, so an identical notification adds nothing:
keep only the latest copy. Keep at most 64 notifications, drop the
oldest beyond that, and say how many were lost.

Hold back the service UP too, as on UP the clients start using the
remote, and pass it on after the deferred notifications. Pass DOWN on
right away, so that the clients stop using the remote, and drop the
deferred notifications with it, as they came from the remote that went
away.

ucsi_glink learns from a notification that a UCSI command has
completed, so a command still in flight at ->prepare() completes only
after the system has resumed. The timeout of the UCSI core does not
count the time spent in suspend, only the time the system takes to
suspend and resume.

qcom_battmgr calls power_supply_changed() for most notifications, which
keeps the system awake through the wakeup source of the power supply.
For a notification that is held back, this now happens after the
system has resumed, so it no longer aborts a suspend started through
/sys/power/wakeup_count.

Fixes: 58ef4ece1e41 ("soc: qcom: pmic_glink: Introduce base PMIC GLINK driver")
Signed-off-by: Vladyslav Tymoshchuk <tvlad744@gmail.com>
---
I used an AI assistant (Claude) for the analysis and to draft this
patch and its description; I reviewed the result. The testing is
described in the cover letter.

 drivers/soc/qcom/pmic_glink.c | 177 ++++++++++++++++++++++++++++++++--
 1 file changed, 168 insertions(+), 9 deletions(-)

diff --git a/drivers/soc/qcom/pmic_glink.c b/drivers/soc/qcom/pmic_glink.c
index 84fd0c82c0c4..748b335be6ef 100644
--- a/drivers/soc/qcom/pmic_glink.c
+++ b/drivers/soc/qcom/pmic_glink.c
@@ -14,8 +14,11 @@
 #include <linux/soc/qcom/pdr.h>
 #include <linux/soc/qcom/pmic_glink.h>
 #include <linux/spinlock.h>
+#include <linux/string.h>
+#include <linux/workqueue.h>
 
 #define PMIC_GLINK_SEND_TIMEOUT (5 * HZ)
+#define PMIC_GLINK_MAX_DEFERRED	64
 
 enum {
 	PMIC_GLINK_CLIENT_BATT = 0,
@@ -50,6 +53,14 @@ struct pmic_glink {
 	/* serializing clients list updates */
 	spinlock_t client_lock;
 	struct list_head clients;
+
+	/* notifications held back over system sleep */
+	spinlock_t defer_lock;
+	bool defer;
+	struct list_head deferred;
+	unsigned int num_deferred;
+	unsigned int num_dropped;
+	struct work_struct deliver_work;
 };
 
 static struct pmic_glink *__pmic_glink;
@@ -66,6 +77,12 @@ struct pmic_glink_client {
 	void *priv;
 };
 
+struct pmic_glink_deferred_msg {
+	struct list_head node;
+	size_t len;
+	u8 data[] __counted_by(len);
+};
+
 static void _devm_pmic_glink_release_client(struct device *dev, void *res)
 {
 	struct pmic_glink_client *client = (struct pmic_glink_client *)res;
@@ -149,13 +166,77 @@ int pmic_glink_send(struct pmic_glink_client *client, void *data, size_t len)
 }
 EXPORT_SYMBOL_GPL(pmic_glink_send);
 
+static void pmic_glink_dispatch(struct pmic_glink *pg, const void *data, size_t len)
+{
+	const struct pmic_glink_hdr *hdr = data;
+	struct pmic_glink_client *client;
+	unsigned long flags;
+
+	spin_lock_irqsave(&pg->client_lock, flags);
+	list_for_each_entry(client, &pg->clients, node) {
+		if (client->id == le32_to_cpu(hdr->owner))
+			client->cb(data, len, client->priv);
+	}
+	spin_unlock_irqrestore(&pg->client_lock, flags);
+}
+
+static bool pmic_glink_defer(struct pmic_glink *pg, const void *data, size_t len)
+{
+	struct pmic_glink_deferred_msg *msg;
+
+	guard(spinlock_irqsave)(&pg->defer_lock);
+
+	if (!pg->defer)
+		return false;
+
+	/* An identical notification brings nothing new, keep the latest copy */
+	list_for_each_entry(msg, &pg->deferred, node) {
+		if (msg->len == len && !memcmp(msg->data, data, len)) {
+			list_move_tail(&msg->node, &pg->deferred);
+			return true;
+		}
+	}
+
+	if (pg->num_deferred == PMIC_GLINK_MAX_DEFERRED) {
+		msg = list_first_entry(&pg->deferred, struct pmic_glink_deferred_msg, node);
+		list_del(&msg->node);
+		kfree(msg);
+		pg->num_deferred--;
+		pg->num_dropped++;
+	}
+
+	msg = kmalloc_flex(*msg, data, len, GFP_ATOMIC);
+	if (!msg) {
+		pg->num_dropped++;
+		return true;
+	}
+
+	msg->len = len;
+	memcpy(msg->data, data, len);
+	list_add_tail(&msg->node, &pg->deferred);
+	pg->num_deferred++;
+
+	return true;
+}
+
+static void pmic_glink_drop_deferred(struct pmic_glink *pg)
+{
+	struct pmic_glink_deferred_msg *msg, *tmp;
+
+	guard(spinlock_irqsave)(&pg->defer_lock);
+
+	list_for_each_entry_safe(msg, tmp, &pg->deferred, node) {
+		list_del(&msg->node);
+		kfree(msg);
+	}
+	pg->num_deferred = 0;
+}
+
 static int pmic_glink_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 				     int len, void *priv, u32 addr)
 {
-	struct pmic_glink_client *client;
 	struct pmic_glink_hdr *hdr;
 	struct pmic_glink *pg = dev_get_drvdata(&rpdev->dev);
-	unsigned long flags;
 
 	if (len < sizeof(*hdr)) {
 		dev_warn(pg->dev, "ignoring truncated message\n");
@@ -164,12 +245,12 @@ static int pmic_glink_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 
 	hdr = data;
 
-	spin_lock_irqsave(&pg->client_lock, flags);
-	list_for_each_entry(client, &pg->clients, node) {
-		if (client->id == le32_to_cpu(hdr->owner))
-			client->cb(data, len, client->priv);
-	}
-	spin_unlock_irqrestore(&pg->client_lock, flags);
+	/* The sender of a request is waiting for the response */
+	if (le32_to_cpu(hdr->type) != PMIC_GLINK_REQ_RESP &&
+	    pmic_glink_defer(pg, data, len))
+		return 0;
+
+	pmic_glink_dispatch(pg, data, len);
 
 	return 0;
 }
@@ -210,6 +291,13 @@ static void pmic_glink_del_aux_device(struct pmic_glink *pg,
 	auxiliary_device_uninit(aux);
 }
 
+static bool pmic_glink_deferring(struct pmic_glink *pg)
+{
+	guard(spinlock_irqsave)(&pg->defer_lock);
+
+	return pg->defer;
+}
+
 static void pmic_glink_state_notify_clients(struct pmic_glink *pg)
 {
 	struct pmic_glink_client *client;
@@ -217,13 +305,18 @@ static void pmic_glink_state_notify_clients(struct pmic_glink *pg)
 	unsigned long flags;
 
 	if (pg->client_state != SERVREG_SERVICE_STATE_UP) {
-		if (pg->pdr_state == SERVREG_SERVICE_STATE_UP && pg->ept)
+		/* On UP the clients use the remote, hold that back as well */
+		if (pg->pdr_state == SERVREG_SERVICE_STATE_UP && pg->ept &&
+		    !pmic_glink_deferring(pg))
 			new_state = SERVREG_SERVICE_STATE_UP;
 	} else {
 		if (pg->pdr_state == SERVREG_SERVICE_STATE_DOWN || !pg->ept)
 			new_state = SERVREG_SERVICE_STATE_DOWN;
 	}
 
+	if (pg->pdr_state == SERVREG_SERVICE_STATE_DOWN || !pg->ept)
+		pmic_glink_drop_deferred(pg);
+
 	if (new_state != pg->client_state) {
 		spin_lock_irqsave(&pg->client_lock, flags);
 		list_for_each_entry(client, &pg->clients, node)
@@ -233,6 +326,40 @@ static void pmic_glink_state_notify_clients(struct pmic_glink *pg)
 	}
 }
 
+static void pmic_glink_deliver_work(struct work_struct *work)
+{
+	struct pmic_glink *pg = container_of(work, struct pmic_glink, deliver_work);
+	struct pmic_glink_deferred_msg *msg;
+	unsigned int dropped;
+	unsigned long flags;
+
+	guard(mutex)(&pg->state_lock);
+
+	for (;;) {
+		spin_lock_irqsave(&pg->defer_lock, flags);
+		msg = list_first_entry_or_null(&pg->deferred, struct pmic_glink_deferred_msg, node);
+		if (!msg) {
+			pg->defer = false;
+			dropped = pg->num_dropped;
+			pg->num_dropped = 0;
+			spin_unlock_irqrestore(&pg->defer_lock, flags);
+			break;
+		}
+
+		list_del(&msg->node);
+		pg->num_deferred--;
+		spin_unlock_irqrestore(&pg->defer_lock, flags);
+
+		pmic_glink_dispatch(pg, msg->data, msg->len);
+		kfree(msg);
+	}
+
+	pmic_glink_state_notify_clients(pg);
+
+	if (dropped)
+		dev_warn(pg->dev, "%u notifications lost during system sleep\n", dropped);
+}
+
 static void pmic_glink_pdr_callback(int state, char *svc_path, void *priv)
 {
 	struct pmic_glink *pg = priv;
@@ -315,6 +442,10 @@ static int pmic_glink_probe(struct platform_device *pdev)
 	spin_lock_init(&pg->client_lock);
 	mutex_init(&pg->state_lock);
 
+	INIT_LIST_HEAD(&pg->deferred);
+	spin_lock_init(&pg->defer_lock);
+	INIT_WORK(&pg->deliver_work, pmic_glink_deliver_work);
+
 	pg->data = of_device_get_match_data(&pdev->dev);
 	if (!pg->data)
 		return -EINVAL;
@@ -377,6 +508,8 @@ static void pmic_glink_remove(struct platform_device *pdev)
 {
 	struct pmic_glink *pg = dev_get_drvdata(&pdev->dev);
 
+	cancel_work_sync(&pg->deliver_work);
+
 	pdr_handle_release(pg->pdr);
 
 	if (pg->data->client_mask & BIT(PMIC_GLINK_CLIENT_BATT))
@@ -386,10 +519,35 @@ static void pmic_glink_remove(struct platform_device *pdev)
 	if (pg->data->client_mask & BIT(PMIC_GLINK_CLIENT_UCSI))
 		pmic_glink_del_aux_device(pg, &pg->ucsi_aux);
 
+	pmic_glink_drop_deferred(pg);
+
 	guard(mutex)(&__pmic_glink_lock);
 	__pmic_glink = NULL;
 }
 
+static int pmic_glink_prepare(struct device *dev)
+{
+	struct pmic_glink *pg = dev_get_drvdata(dev);
+
+	guard(spinlock_irqsave)(&pg->defer_lock);
+	pg->defer = true;
+
+	return 0;
+}
+
+static void pmic_glink_complete(struct device *dev)
+{
+	struct pmic_glink *pg = dev_get_drvdata(dev);
+
+	/* Runs once the tasks are thawed, all devices have resumed by then */
+	queue_work(system_freezable_wq, &pg->deliver_work);
+}
+
+static const struct dev_pm_ops pmic_glink_pm_ops = {
+	.prepare = pmic_glink_prepare,
+	.complete = pmic_glink_complete,
+};
+
 static const struct pmic_glink_data pmic_glink_adsp_data = {
 	.client_mask = BIT(PMIC_GLINK_CLIENT_BATT) |
 		       BIT(PMIC_GLINK_CLIENT_ALTMODE) |
@@ -418,6 +576,7 @@ static struct platform_driver pmic_glink_driver = {
 	.driver = {
 		.name = "qcom_pmic_glink",
 		.of_match_table = pmic_glink_of_match,
+		.pm = pm_sleep_ptr(&pmic_glink_pm_ops),
 	},
 };
 
-- 
2.43.0


  reply	other threads:[~2026-10-22 16:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-22 16:41 [RFC PATCH 0/4] " Vladyslav Tymoshchuk
2026-10-22 16:41 ` Vladyslav Tymoshchuk [this message]
2026-10-22 16:41 ` [RFC PATCH 2/4] soc: qcom: pmic_glink: Report wakeup events on behalf of clients Vladyslav Tymoshchuk
2026-10-22 16:41 ` [RFC PATCH 3/4] usb: typec: ucsi: glink: Wake up the system on connector changes Vladyslav Tymoshchuk
2026-10-22 17:32 ` [RFC PATCH 4/4] power: supply: qcom_battmgr: Allow charger changes to wake up the system Vladyslav Tymoshchuk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261022164151.592-2-tvlad744@gmail.com \
    --to=tvlad744@gmail.com \
    --cc=26rote@gmail.com \
    --cc=abelvesa@kernel.org \
    --cc=andersson@kernel.org \
    --cc=casey.connolly@linaro.org \
    --cc=daniel@quora.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=gustavoars@kernel.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=jishnu.prakash@oss.qualcomm.com \
    --cc=johan@kernel.org \
    --cc=kamal.wadhwa@oss.qualcomm.com \
    --cc=kees@kernel.org \
    --cc=konradybcio@kernel.org \
    --cc=krzk@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=lumag@kernel.org \
    --cc=neil.armstrong@linaro.org \
    --cc=sre@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®